Open History to see the last commit
1import {
2 MessageId,
3 type ModelSelection,
4 type OrchestrationV2ProviderSession,
5 type OrchestrationV2ProviderThread,
6 ProviderDriverKind,
7 ProviderInstanceId,
8 ProviderThreadId,
9 ProviderTurnId,
10 type ThreadId,
11 TurnItemId,
12} from "@t3tools/contracts";
13import * as DateTime from "effect/DateTime";
14import * as Effect from "effect/Effect";
15import * as PubSub from "effect/PubSub";
16import * as Ref from "effect/Ref";
17import * as Stream from "effect/Stream";
18
19import * as CodexAdapterV2 from "../Adapters/CodexAdapterV2.ts";
20import * as ProviderAdapter from "@t3tools/provider-core/server/ProviderAdapter";
21
22/** A turn the adapter was asked to run: its thread and the text the provider got. */
23export interface CapturedTurn {
24 readonly threadId: ThreadId;
25 readonly providerThreadId: ProviderThreadId;
26 readonly text: string;
27}
28
29const driver = ProviderDriverKind.make("codex");
30const instanceId = ProviderInstanceId.make("codex");
31
32const unimplemented = (detail: string) =>
33 Effect.fail(new ProviderAdapter.ProviderAdapterProtocolError({ driver, detail }));
34
35/**
36 * A Codex-shaped adapter that records every turn's input text and completes
37 * each turn at once with `response`. For tests of what a provider is sent.
38 */
39export const makeCapturingCodexAdapter = (
40 capturedTurns: Ref.Ref<ReadonlyArray<CapturedTurn>>,
41 options: {
42 readonly response: string;
43 readonly modelSelection: ModelSelection;
44 /** Holds each turn open until this completes, so a test can act mid-turn. */
45 readonly holdTurn?: Effect.Effect<void>;
46 },
47) =>
48 ({
49 instanceId,
50 driver,
51 getCapabilities: () => Effect.succeed(CodexAdapterV2.CodexProviderCapabilitiesV2),
52 planSelectionTransition: () => Effect.succeed({ type: "apply_on_next_turn" }),
53 openSession: (sessionInput) =>
54 Effect.gen(function* () {
55 const events = yield* PubSub.unbounded<ProviderAdapter.ProviderAdapterV2Event>();
56 const now = yield* DateTime.now;
57 const providerSession: OrchestrationV2ProviderSession = {
58 id: sessionInput.providerSessionId,
59 driver,
60 providerInstanceId: instanceId,
61 status: "ready",
62 cwd: sessionInput.runtimePolicy.cwd ?? process.cwd(),
63 model: options.modelSelection.model,
64 capabilities: CodexAdapterV2.CodexProviderCapabilitiesV2,
65 createdAt: now,
66 updatedAt: now,
67 lastError: null,
68 };
69 const providerTurnUpdate = (
70 turnInput: ProviderAdapter.ProviderAdapterV2TurnInput,
71 status: "running" | "completed",
72 startedAt: DateTime.Utc,
73 completedAt: DateTime.Utc | null,
74 ): ProviderAdapter.ProviderAdapterV2Event => ({
75 type: "provider_turn.updated",
76 driver,
77 providerTurn: {
78 id: ProviderTurnId.make(`provider-turn:${turnInput.threadId}:${turnInput.runOrdinal}`),
79 providerThreadId: turnInput.providerThread.id,
80 nodeId: turnInput.rootNodeId,
81 runAttemptId: turnInput.attemptId,
82 nativeTurnRef: {
83 driver,
84 nativeId: `native-turn:${turnInput.threadId}:${turnInput.runOrdinal}`,
85 strength: "strong",
86 },
87 ordinal: turnInput.runOrdinal,
88 status,
89 startedAt,
90 completedAt,
91 },
92 });
93 const finishTurn = (turnInput: ProviderAdapter.ProviderAdapterV2TurnInput) =>
94 Effect.gen(function* () {
95 const eventTime = yield* DateTime.now;
96 const providerTurnId = ProviderTurnId.make(
97 `provider-turn:${turnInput.threadId}:${turnInput.runOrdinal}`,
98 );
99 yield* PubSub.publishAll(events, [
100 providerTurnUpdate(turnInput, "completed", eventTime, eventTime),
101 {
102 type: "turn_item.updated",
103 driver,
104 turnItem: {
105 id: TurnItemId.make(
106 `turn-item:${turnInput.threadId}:${turnInput.runOrdinal}:assistant`,
107 ),
108 threadId: turnInput.threadId,
109 runId: turnInput.runId,
110 nodeId: turnInput.rootNodeId,
111 providerThreadId: turnInput.providerThread.id,
112 providerTurnId,
113 nativeItemRef: null,
114 parentItemId: null,
115 ordinal: turnInput.runOrdinal * 100 + 1,
116 status: "completed",
117 title: null,
118 startedAt: eventTime,
119 completedAt: eventTime,
120 updatedAt: eventTime,
121 type: "assistant_message",
122 messageId: MessageId.make(
123 `message:${turnInput.threadId}:${turnInput.runOrdinal}:assistant`,
124 ),
125 text: options.response,
126 streaming: false,
127 },
128 },
129 {
130 type: "turn.terminal",
131 driver,
132 providerThreadId: turnInput.providerThread.id,
133 providerTurnId,
134 runOrdinal: turnInput.runOrdinal,
135 status: "completed",
136 failure: null,
137 threadDisposition: "reusable",
138 },
139 ] satisfies ReadonlyArray<ProviderAdapter.ProviderAdapterV2Event>);
140 });
141 return {
142 instanceId,
143 driver,
144 providerSessionId: sessionInput.providerSessionId,
145 providerSession,
146 events: Stream.fromPubSub(events),
147 ensureThread: (threadInput) =>
148 Effect.gen(function* () {
149 const createdAt = yield* DateTime.now;
150 const nativeThreadId = `${driver}:${threadInput.threadId}`;
151 return {
152 id: ProviderThreadId.make(`provider-thread:${nativeThreadId}`),
153 driver,
154 providerInstanceId: instanceId,
155 providerSessionId: sessionInput.providerSessionId,
156 appThreadId: threadInput.threadId,
157 ownerNodeId: null,
158 nativeThreadRef: {
159 driver,
160 nativeId: nativeThreadId,
161 strength: "strong",
162 },
163 nativeConversationHeadRef: null,
164 status: "idle",
165 firstRunOrdinal: null,
166 lastRunOrdinal: null,
167 handoffIds: [],
168 forkedFrom: null,
169 createdAt,
170 updatedAt: createdAt,
171 } satisfies OrchestrationV2ProviderThread;
172 }),
173 resumeThread: ({ providerThread }) => Effect.succeed(providerThread),
174 startTurn: (turnInput) =>
175 Effect.gen(function* () {
176 yield* Ref.update(capturedTurns, (turns) => [
177 ...turns,
178 {
179 threadId: turnInput.threadId,
180 providerThreadId: turnInput.providerThread.id,
181 text: turnInput.message.text,
182 },
183 ]);
184 if (options.holdTurn !== undefined) {
185 // Ends the turn later, as a live provider does; startTurn returns now.
186 // Until then the turn is running, so Stop and steering can target it.
187 yield* PubSub.publish(
188 events,
189 providerTurnUpdate(turnInput, "running", yield* DateTime.now, null),
190 );
191 yield* Effect.forkDetach(
192 options.holdTurn.pipe(Effect.andThen(finishTurn(turnInput))),
193 );
194 return;
195 }
196 yield* finishTurn(turnInput);
197 }),
198 steerTurn: () => Effect.void,
199 interruptTurn: () => Effect.void,
200 respondToRuntimeRequest: () => Effect.void,
201 readThreadSnapshot: () => unimplemented("readThreadSnapshot unused in this test"),
202 rollbackThread: () => unimplemented("rollbackThread unused in this test"),
203 forkThread: () => unimplemented("forkThread unused in this test"),
204 };
205 }),
206 }) satisfies ProviderAdapter.ProviderAdapterV2["Service"];