The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 03:37 UTC
| 1 | import { forkSession, getSessionMessages } from "@anthropic-ai/claude-agent-sdk"; |
| 2 | import * as Schema from "effect/Schema"; |
| 3 | |
| 4 | // Bundled import.meta.main is always true; imports must have no side effects. |
| 5 | const decodeHistoryOptions = Schema.decodeSync( |
| 6 | Schema.fromJsonString( |
| 7 | Schema.Struct({ |
| 8 | dir: Schema.optionalKey(Schema.String), |
| 9 | includeSystemMessages: Schema.optionalKey(Schema.Boolean), |
| 10 | upToMessageId: Schema.optionalKey(Schema.String), |
| 11 | }), |
| 12 | ), |
| 13 | ); |
| 14 | |
| 15 | export async function runClaudeHistoryWorker( |
| 16 | method: string | undefined, |
| 17 | sessionId: string | undefined, |
| 18 | rawOptions: string | undefined, |
| 19 | ): Promise<void> { |
| 20 | const options = decodeHistoryOptions(rawOptions ?? "{}"); |
| 21 | if (!sessionId) throw new Error("Claude history session id is required."); |
| 22 | const result = |
| 23 | method === "getSessionMessages" |
| 24 | ? await getSessionMessages(sessionId, options) |
| 25 | : method === "forkSession" |
| 26 | ? await forkSession(sessionId, options).then(async (forked) => { |
| 27 | const headMessageId = await getSessionMessages(forked.sessionId, options) |
| 28 | .then((messages) => messages.at(-1)?.uuid) |
| 29 | .catch(() => undefined); |
| 30 | return headMessageId === undefined ? forked : { ...forked, headMessageId }; |
| 31 | }) |
| 32 | : (() => { |
| 33 | throw new Error("Unknown Claude history operation."); |
| 34 | })(); |
| 35 | process.stdout.write(JSON.stringify(result)); |
| 36 | } |