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

CodexAdapterV2.testkit.ts

192 lines6.9 KB

1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { CodexSettings, type ProviderReplayTranscript } from "@t3tools/contracts";
3import * as CodexClient from "effect-codex-app-server/client";
4import type * as CodexError from "effect-codex-app-server/errors";
5import * as CodexReplay from "effect-codex-app-server/replay";
6import * as Effect from "effect/Effect";
7import * as Layer from "effect/Layer";
8import * as Predicate from "effect/Predicate";
9import * as Schema from "effect/Schema";
10​
11import * as ServerConfig from "../../config.ts";
12import * as IdAllocator from "@t3tools/provider-core/server/IdAllocator";
13import * as ProviderAdapter from "@t3tools/provider-core/server/ProviderAdapter";
14import { ProviderAdapterDriverCreateError } from "@t3tools/provider-core/server/adapterDriver";
15import * as ProviderAdapterRegistry from "../ProviderAdapterRegistry.ts";
16import {
17 makeReplayServerConfig,
18 type OrchestratorV2ProviderReplayHarness,
19} from "../testkit/ProviderReplayHarness.ts";
20import type { ProviderReplayGate } from "@t3tools/provider-testing/replayGate";
21import * as CodexAdapterV2 from "./CodexAdapterV2.ts";
22​
23const DEFAULT_CODEX_SETTINGS = Schema.decodeSync(CodexSettings)({});
24​
25export class CodexReplayTranscriptDecodeError extends Schema.TaggedError<CodexReplayTranscriptDecodeError>()(
26 "CodexReplayTranscriptDecodeError",
27 {
28 driver: Schema.optional(Schema.String),
29 protocol: Schema.optional(Schema.String),
30 scenario: Schema.optional(Schema.String),
31 cause: Schema.Defect(),
32 },
33) {
34 override get message(): string {
35 return `Failed to decode Codex app-server replay transcript for scenario ${this.scenario ?? "<unknown>"}.`;
36 }
38​
39export const CodexOrchestratorReplayHarnessError = Schema.Union([
40 CodexReplayTranscriptDecodeError,
41 CodexReplay.CodexAppServerReplayError,
42 ProviderAdapterDriverCreateError,
43]);
44export type CodexOrchestratorReplayHarnessError = typeof CodexOrchestratorReplayHarnessError.Type;
45​
46export function withCodexReplayChildMetadata(
47 client: CodexClient.CodexAppServerClient["Service"],
48 transcript: CodexReplay.CodexAppServerReplayTranscript,
49 readMetadata: (
50 threadId: string,
51 method: "thread/read" | "thread/resume",
52 ) => Effect.Effect<unknown, CodexError.CodexAppServerError> = (threadId) =>
53 Effect.succeed({ thread: { id: threadId }, model: null }),
54): CodexClient.CodexAppServerClient["Service"] {
55 const childThreadIds = new Set(
56 transcript.entries.flatMap((entry) => {
57 if (entry.type !== "emit_inbound" || !Predicate.isObject(entry.frame)) return [];
58 const params = entry.frame.params;
59 if (!Predicate.isObject(params) || !Predicate.isObject(params.item)) return [];
60 const item = params.item;
61 if (item.type === "subAgentActivity" && typeof item.agentThreadId === "string") {
62 return [item.agentThreadId];
63 }
64 return item.type === "collabAgentToolCall" && Array.isArray(item.receiverThreadIds)
65 ? item.receiverThreadIds.filter(Predicate.isString)
66 : [];
67 }),
68 );
69 return {
70 ...client,
71 raw: {
72 ...client.raw,
73 request: (method, params) =>
74 (method === "thread/read" || method === "thread/resume") &&
75 Predicate.isObject(params) &&
76 (method === "thread/read" ? params.includeTurns === false : params.excludeTurns === true) &&
77 typeof params.threadId === "string" &&
78 childThreadIds.has(params.threadId)
79 ? readMetadata(params.threadId, method)
80 : client.raw.request(method, params),
81 },
82 };
84​
85function metadataFromTranscript(transcript: ProviderReplayTranscript): {
86 readonly provider?: string;
87 readonly protocol?: string;
88 readonly scenario?: string;
89} {
90 return {
91 provider: transcript.provider,
92 protocol: transcript.protocol,
93 scenario: transcript.scenario,
94 };
96​
97export function layer(input: {
98 readonly transcript: CodexReplay.CodexAppServerReplayTranscript;
99 readonly driver?: CodexReplay.CodexAppServerReplayDriver;
100}) {
101 const layerReplay =
102 input.driver === undefined
103 ? CodexReplay.layerReplay(input.transcript)
104 : CodexReplay.layerReplayWithDriver(input.driver);
105 const layerReplayClientFactory = Layer.succeed(CodexAdapterV2.CodexAppServerClientFactory, {
106 open: (openInput) =>
107 Effect.gen(function* () {
108 const context = yield* Layer.build(layerReplay).pipe(
109 Effect.mapError(
110 (cause) =>
111 new ProviderAdapter.ProviderAdapterOpenSessionError({
112 driver: CodexAdapterV2.CODEX_DRIVER_KIND,
113 providerSessionId: openInput.providerSessionId,
114 cause,
115 }),
116 ),
117 );
118 return yield* Effect.service(CodexClient.CodexAppServerClient).pipe(
119 Effect.map((client) => withCodexReplayChildMetadata(client, input.transcript)),
120 Effect.provide(context),
121 );
122 }),
123 });
124 const layerServerConfig = Layer.effect(
125 ServerConfig.ServerConfig,
126 makeReplayServerConfig(input.transcript.scenario).pipe(Effect.orDie),
127 ).pipe(Layer.provide(NodeServices.layer));
128 const layerRegistry = ProviderAdapterRegistry.layerFromAdaptersEffect(
129 CodexAdapterV2.createCodexAdapterV2({
130 instanceId: CodexAdapterV2.CODEX_DEFAULT_INSTANCE_ID,
131 displayName: undefined,
132 environment: [],
133 enabled: true,
134 config: DEFAULT_CODEX_SETTINGS,
135 }).pipe(Effect.map((adapter) => [adapter])),
136 ).pipe(
137 Layer.provide(
138 Layer.mergeAll(
139 layerReplayClientFactory,
140 layerServerConfig,
141 NodeServices.layer,
142 IdAllocator.layer,
143 ),
144 ),
145 );
146​
147 return layerRegistry;
149​
150const decodeCodexAppServerReplayTranscript = Schema.decodeUnknownEffect(
151 CodexReplay.CodexAppServerReplayTranscript,
152);
153​
154export const CodexOrchestratorReplayHarness: OrchestratorV2ProviderReplayHarness<
155 CodexReplay.CodexAppServerReplayTranscript,
156 CodexOrchestratorReplayHarnessError
157> = {
158 driver: CodexAdapterV2.CODEX_DRIVER_KIND,
159 decodeTranscript: (transcript) =>
160 decodeCodexAppServerReplayTranscript(transcript).pipe(
161 Effect.mapError(
162 (cause) =>
163 new CodexReplayTranscriptDecodeError({
164 ...metadataFromTranscript(transcript),
165 cause,
166 }),
167 ),
168 ),
169 makeProviderAdapterRegistryLayer: (
170 transcript,
171 options: { readonly replayGate?: ProviderReplayGate } = {},
172 ) => {
173 return Layer.effectContext(
174 Effect.gen(function* () {
175 const replayGate = options.replayGate;
176 if (replayGate !== undefined) {
177 yield* Effect.addFinalizer(() => Effect.sync(() => replayGate.releaseAll()));
178 }
179 const driver = yield* CodexReplay.makeReplayDriver(
180 transcript,
181 replayGate === undefined
182 ? {}
183 : {
184 beforeEmitInbound: (entry) =>
185 Effect.promise((signal) => replayGate.beforeEmit(entry.label, signal)),
186 },
187 );
188 return yield* Layer.build(layer({ transcript, driver }));
189 }),
190 );
191 },
192};