Open History to see the last commit
1import { forkSession, getSessionMessages } from "@anthropic-ai/claude-agent-sdk";
2import * as Schema from "effect/Schema";
3
4// Nothing here may run on import: inside the executable `import.meta.main` is
5// true for the whole bundle.
6const decodeHistoryOptions = Schema.decodeSync(
7 Schema.fromJsonString(
8 Schema.Struct({
9 dir: Schema.optionalKey(Schema.String),
10 includeSystemMessages: Schema.optionalKey(Schema.Boolean),
11 upToMessageId: Schema.optionalKey(Schema.String),
12 }),
13 ),
14);
15
16export async function runClaudeHistoryWorker(
17 method: string | undefined,
18 sessionId: string | undefined,
19 rawOptions: string | undefined,
20): Promise<void> {
21 const options = decodeHistoryOptions(rawOptions ?? "{}");
22 if (!sessionId) throw new Error("Claude history session id is required.");
23 const result =
24 method === "getSessionMessages"
25 ? await getSessionMessages(sessionId, options)
26 : method === "forkSession"
27 ? await forkSession(sessionId, options).then(async (forked) => {
28 const headMessageId = await getSessionMessages(forked.sessionId, options)
29 .then((messages) => messages.at(-1)?.uuid)
30 .catch(() => undefined);
31 return headMessageId === undefined ? forked : { ...forked, headMessageId };
32 })
33 : (() => {
34 throw new Error("Unknown Claude history operation.");
35 })();
36 process.stdout.write(JSON.stringify(result));
37}