| 1 | import { forkSession, getSessionMessages } from "@anthropic-ai/claude-agent-sdk"; |
| 2 | import * as Schema from "effect/Schema"; |
| 3 | |
| 4 | // A separate process gives SDK history helpers the provider's environment without |
| 5 | // mutating the server's environment. `claude-history-worker.ts` is the |
| 6 | // standalone entry bundled beside the server for npm installs; the |
| 7 | // single-executable hosts the same function as its `__claude-history` |
| 8 | // subcommand, which has no Node to run a sibling script. Nothing here may run |
| 9 | // on import: inside the executable `import.meta.main` is true for the whole |
| 10 | // bundle. |
| 11 | const decodeHistoryOptions = Schema.decodeSync( |
| 12 | Schema.fromJsonString( |
| 13 | Schema.Struct({ |
| 14 | dir: Schema.optionalKey(Schema.String), |
| 15 | includeSystemMessages: Schema.optionalKey(Schema.Boolean), |
| 16 | upToMessageId: Schema.optionalKey(Schema.String), |
| 17 | }), |
| 18 | ), |
| 19 | ); |
| 20 | |
| 21 | export async function runClaudeHistoryWorker( |
| 22 | method: string | undefined, |
| 23 | sessionId: string | undefined, |
| 24 | rawOptions: string | undefined, |
| 25 | ): Promise<void> { |
| 26 | const options = decodeHistoryOptions(rawOptions ?? "{}"); |
| 27 | if (!sessionId) throw new Error("Claude history session id is required."); |
| 28 | const result = |
| 29 | method === "getSessionMessages" |
| 30 | ? await getSessionMessages(sessionId, options) |
| 31 | : method === "forkSession" |
| 32 | ? await forkSession(sessionId, options) |
| 33 | : (() => { |
| 34 | throw new Error("Unknown Claude history operation."); |
| 35 | })(); |
| 36 | process.stdout.write(JSON.stringify(result)); |
| 37 | } |