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

ClaudeAgentSdkQueryRunner.ts

50 lines2.3 KB

1import { cacheInOwnerScope } from "@t3tools/shared/lazyService";
2import type { ForkSessionResult } from "@anthropic-ai/claude-agent-sdk";
3import type {
4 ClaudeAgentSdkQueryRunnerError,
5 ClaudeAgentSdkQueryOpenInput,
6 ClaudeAgentSdkQuerySession,
7 ClaudeAgentSdkSessionForkInput,
8 ClaudeAgentSdkSubagentLookupInput,
9} from "../orchestration-v2/Adapters/ClaudeAdapterV2.ts";
10import * as Context from "effect/Context";
11import * as Effect from "effect/Effect";
12import * as Layer from "effect/Layer";
13​
14export class ClaudeAgentSdkQueryRunner extends Context.Service<
15 ClaudeAgentSdkQueryRunner,
16 {
17 readonly allocateSessionId: Effect.Effect<string, ClaudeAgentSdkQueryRunnerError>;
18 readonly open: (
19 input: ClaudeAgentSdkQueryOpenInput,
20 ) => Effect.Effect<ClaudeAgentSdkQuerySession, ClaudeAgentSdkQueryRunnerError>;
21 readonly forkSession: (
22 input: ClaudeAgentSdkSessionForkInput,
23 ) => Effect.Effect<ForkSessionResult, ClaudeAgentSdkQueryRunnerError>;
24 /** The tool call that launched a subagent, read from the CLI's session storage. */
25 readonly subagentLaunchToolUseId: (
26 input: ClaudeAgentSdkSubagentLookupInput,
27 ) => Effect.Effect<string | null, ClaudeAgentSdkQueryRunnerError>;
28 readonly assertComplete: Effect.Effect<void, ClaudeAgentSdkQueryRunnerError>;
29 }
30>()("t3/provider/ClaudeAgentSdkQueryRunner") {}
31​
32export const layer = Layer.effect(
33 ClaudeAgentSdkQueryRunner,
34 Effect.gen(function* () {
35 const service = yield* cacheInOwnerScope(
36 Effect.promise(() => import("../orchestration-v2/Adapters/ClaudeAdapterV2.ts")).pipe(
37 Effect.flatMap((adapter) => Layer.build(adapter.layerQueryRunner)),
38 Effect.map((services) => Context.get(services, ClaudeAgentSdkQueryRunner)),
39 ),
40 );
41 return ClaudeAgentSdkQueryRunner.of({
42 allocateSessionId: service.pipe(Effect.flatMap((value) => value.allocateSessionId)),
43 open: (input) => service.pipe(Effect.flatMap((value) => value.open(input))),
44 forkSession: (input) => service.pipe(Effect.flatMap((value) => value.forkSession(input))),
45 subagentLaunchToolUseId: (input) =>
46 service.pipe(Effect.flatMap((value) => value.subagentLaunchToolUseId(input))),
47 assertComplete: service.pipe(Effect.flatMap((value) => value.assertComplete)),
48 });
49 }),
50);