Skip to content
Source
Sign inSign up
Open History to see the last commit

claudeHistoryWorker.ts

36 lines1.4 KB

1import { forkSession, getSessionMessages } from "@anthropic-ai/claude-agent-sdk";
2import * as Schema from "effect/Schema";
3​
4// Bundled import.meta.main is always true; imports must have no side effects.
5const 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​
15export 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));