@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 534 lines 18.3 KB
Raw
1 import * as NodeCrypto from "@effect/platform-node/NodeCrypto";
2 import * as NodeServices from "@effect/platform-node/NodeServices";
3 import { assert, describe, it } from "@effect/vitest";
4 import {
5 MessageId,
6 NodeId,
7 ProjectId,
8 ProviderInstanceId,
9 ProviderSessionId,
10 RunAttemptId,
11 RunId,
12 type RuntimeMode,
13 ThreadId,
14 } from "@t3tools/contracts";
15 import { ACP_PROTOCOL } from "@t3tools/provider-acp/server/adapter";
16 import { FxSettings } from "@t3tools/provider-fx/settings";
17 import { FX_DRIVER_KIND, makeFxAdapterV2 } from "@t3tools/provider-fx/testing";
18 import * as IdAllocator from "@t3tools/provider-core/server/IdAllocator";
19 import * as McpProviderSessions from "@t3tools/provider-core/server/McpProviderSessions";
20 import { ProviderAdapterV2RuntimePolicy } from "@t3tools/provider-core/server/ProviderAdapter";
21 import * as TestProviderHost from "@t3tools/provider-testing/TestProviderHost";
22 import { resolveSelfInvocation } from "@t3tools/shared/nodeRuntime";
23 import * as DateTime from "effect/DateTime";
24 import * as Cause from "effect/Cause";
25 import * as Effect from "effect/Effect";
26 import * as Exit from "effect/Exit";
27 import * as FileSystem from "effect/FileSystem";
28 import * as Layer from "effect/Layer";
29 import * as Path from "effect/Path";
30 import * as Schema from "effect/Schema";
31 import * as Stream from "effect/Stream";
32 import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
33
34 import {
35 decodeAcpReplayTranscript,
36 makeAcpReplayCompletenessAssertion,
37 makeAcpReplayRuntime,
38 } from "./AcpAdapterV2.testkit.ts";
39
40 const testLayer = Layer.mergeAll(
41 NodeServices.layer,
42 NodeCrypto.layer,
43 IdAllocator.layer,
44 McpProviderSessions.layer,
45 TestProviderHost.layer().pipe(
46 Layer.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer)),
47 ),
48 );
49
50 const SESSION_ID = "fx-session-01";
51 const ENABLED_FX_SETTINGS = Schema.decodeSync(FxSettings)({ enabled: true });
52 /** The model `fx status --json` reports in the thread's folder, on the gateway. */
53 const FX_DEFAULT_MODEL = "anthropic/claude-opus-5.5";
54 const NAMED_MODEL = "anthropic/claude-sonnet-5.5";
55 const CUSTOM_MODEL = "openai/gpt-6.1-sol";
56 const CODEX_MODEL = "gpt-6.1-sol";
57 const FX_0_0_13_MODES = ["code", "ask"];
58 /** fx 0.0.14+, after vercel-labs/fx#1189. */
59 const FX_MAIN_MODES = ["auto", "ask", "full-access"];
60
61 type Frame = Record<string, unknown>;
62 const outbound = (method: string, params: unknown = "<any>"): Frame => ({
63 type: "expect_outbound",
64 label: method,
65 frame: { kind: "request", method, params },
66 });
67 const answer = (method: string, result: unknown): Frame => ({
68 type: "emit_inbound",
69 label: `${method}.result`,
70 frame: { kind: "response", method, result },
71 });
72
73 const select = (
74 id: string,
75 category: string,
76 currentValue: string,
77 values: ReadonlyArray<string>,
78 ) => ({
79 id,
80 name: id,
81 category,
82 type: "select",
83 currentValue,
84 options: values.map((value) => ({ value, name: value })),
85 });
86
87 /**
88 * fx's session config as recorded from 0.0.13 (fixtures/simple), models
89 * trimmed. fx lists its backend `provider` before `model`, both with
90 * category `model`.
91 */
92 const fxConfig = (input: {
93 readonly model: string;
94 readonly modes: ReadonlyArray<string>;
95 readonly mode?: string;
96 readonly backend?: string;
97 /** The session's own model list, on a backend other than the gateway. */
98 readonly models?: ReadonlyArray<string>;
99 }) => [
100 select("provider", "model", input.backend ?? "gateway", ["gateway", "codex", "grok"]),
101 select(
102 "model",
103 "model",
104 input.model,
105 input.models ?? [NAMED_MODEL, FX_DEFAULT_MODEL, "openai/gpt-5.6-sol"],
106 ),
107 select("mode", "mode", input.mode ?? "ask", input.modes),
108 select("effort", "thought_level", "xhigh", ["auto", "low", "medium", "high", "xhigh", "max"]),
109 ];
110
111 /**
112 * fx's reply to `session/new` or `session/load`. fx 0.0.13 reports `ask`
113 * while it enforces its saved default until a client writes the mode.
114 */
115 const fxSession = (input: {
116 readonly model: string;
117 readonly modes: ReadonlyArray<string>;
118 readonly backend?: string;
119 readonly models?: ReadonlyArray<string>;
120 }) => ({
121 sessionId: SESSION_ID,
122 configOptions: fxConfig(input),
123 modes: { currentModeId: "ask", availableModes: input.modes.map((id) => ({ id, name: id })) },
124 });
125
126 const initialize: ReadonlyArray<Frame> = [
127 outbound("initialize"),
128 answer("initialize", {
129 protocolVersion: 1,
130 agentCapabilities: {
131 loadSession: true,
132 promptCapabilities: { image: true, audio: false, embeddedContext: true },
133 },
134 agentInfo: { name: "fx", title: "fx", version: "0.0.13" },
135 authMethods: [],
136 }),
137 ];
138
139 /** A config write and fx's reply, which lists the session's config after it. */
140 const setConfig = (
141 configId: string,
142 value: string,
143 configAfter: ReadonlyArray<unknown>,
144 ): ReadonlyArray<Frame> => [
145 outbound("session/set_config_option", { sessionId: SESSION_ID, configId, value }),
146 answer("session/set_config_option", { configOptions: configAfter }),
147 ];
148
149 /**
150 * Opens an fx session against a scripted ACP agent, which fails on any
151 * frame the script does not expect. Every scripted frame must be used.
152 */
153 const openFxSession = Effect.fn("openFxSession")(function* (input: {
154 readonly scenario: string;
155 readonly frames: ReadonlyArray<Frame>;
156 readonly runtimeMode: RuntimeMode;
157 readonly model?: string;
158 readonly defaultBackend?: string;
159 readonly modeOptions?: ReadonlyArray<{ readonly id: string; readonly value: string }>;
160 /** Loads this fx session instead of creating one. */
161 readonly loadSessionId?: string;
162 /** Then starts a turn on this model. */
163 readonly turnModel?: string;
164 }) {
165 const childProcessSpawner = yield* ChildProcessSpawner.ChildProcessSpawner;
166 const fileSystem = yield* FileSystem.FileSystem;
167 const path = yield* Path.Path;
168 const replayDir = yield* fileSystem.makeTempDirectoryScoped({
169 prefix: `t3-fx-${input.scenario}-`,
170 });
171 const statusPath = path.join(replayDir, "status.json");
172 const transcript = yield* decodeAcpReplayTranscript(
173 {
174 provider: FX_DRIVER_KIND,
175 protocol: ACP_PROTOCOL,
176 version: "1",
177 scenario: input.scenario,
178 entries: input.frames as never,
179 },
180 FX_DRIVER_KIND,
181 );
182 const instanceId = ProviderInstanceId.make(`fx-${input.scenario}`);
183 const adapter = yield* makeFxAdapterV2({
184 instanceId,
185 settings: ENABLED_FX_SETTINGS,
186 environment: {},
187 selfInvocation: yield* resolveSelfInvocation(),
188 defaultChoice: (cwd) =>
189 // "Default" resolves in the thread's own folder.
190 Effect.succeed(
191 cwd === replayDir
192 ? { backend: input.defaultBackend ?? "gateway", model: FX_DEFAULT_MODEL }
193 : undefined,
194 ),
195 makeRuntime: makeAcpReplayRuntime({
196 transcript,
197 statusPath,
198 scriptPath: yield* path.fromFileUrl(
199 new URL("../../../scripts/acp-replay-agent.ts", import.meta.url),
200 ),
201 childProcessSpawner,
202 fileSystem,
203 }),
204 });
205 const threadId = ThreadId.make(`thread-fx-${input.scenario}`);
206 // The thread's effort matches the session's, so no effort write is scripted.
207 const selectionOn = (model: string) => ({
208 instanceId,
209 model,
210 options: [{ id: "effort", value: "xhigh" }, ...(input.modeOptions ?? [])],
211 });
212 const modelSelection = selectionOn(input.model ?? "default");
213 const runtimePolicy = ProviderAdapterV2RuntimePolicy.make({
214 runtimeMode: input.runtimeMode,
215 interactionMode: "default",
216 cwd: replayDir,
217 });
218 yield* Effect.gen(function* () {
219 const session = yield* adapter.openSession({
220 threadId,
221 providerSessionId: ProviderSessionId.make(`provider-session-fx-${input.scenario}`),
222 modelSelection,
223 runtimePolicy,
224 ...(input.loadSessionId === undefined ? {} : { initialNativeThreadId: input.loadSessionId }),
225 });
226 if (input.turnModel === undefined) return;
227 const providerThread = yield* session.ensureThread({ threadId, modelSelection, runtimePolicy });
228 const now = yield* DateTime.now;
229 const turnSelection = selectionOn(input.turnModel);
230 yield* session.startTurn({
231 appThread: {
232 createdBy: "user",
233 creationSource: "web",
234 id: threadId,
235 projectId: ProjectId.make(`project:${threadId}`),
236 title: "fx adapter test",
237 providerInstanceId: instanceId,
238 modelSelection: turnSelection,
239 runtimeMode: runtimePolicy.runtimeMode,
240 interactionMode: "default",
241 branch: null,
242 worktreePath: null,
243 activeProviderThreadId: providerThread.id,
244 lineage: { parentThreadId: null, relationshipToParent: null, rootThreadId: threadId },
245 forkedFrom: null,
246 createdAt: now,
247 updatedAt: now,
248 archivedAt: null,
249 settledOverride: null,
250 settledAt: null,
251 lastVisitedAt: null,
252 deletedAt: null,
253 },
254 threadId,
255 runId: RunId.make(`run:${threadId}:1`),
256 runOrdinal: 1,
257 providerTurnOrdinal: 1,
258 attemptId: RunAttemptId.make(`attempt:${threadId}:1`),
259 rootNodeId: NodeId.make(`node:${threadId}:1`),
260 providerThread,
261 message: {
262 createdBy: "user",
263 creationSource: "web",
264 messageId: MessageId.make(`message:${threadId}:1`),
265 text: "Say hello",
266 attachments: [],
267 },
268 modelSelection: turnSelection,
269 runtimePolicy,
270 });
271 yield* session.events.pipe(
272 Stream.takeUntil((event) => event.type === "turn.terminal"),
273 Stream.runDrain,
274 );
275 }).pipe(Effect.scoped);
276 yield* makeAcpReplayCompletenessAssertion(fileSystem, statusPath, transcript);
277 });
278
279 describe("FxAdapterV2", () => {
280 it.effect("runs Auto as `code` on 0.0.13, and Auto and Full access as fx names them later", () =>
281 Effect.forEach(
282 [
283 { scenario: "auto-0-0-13", runtimeMode: "auto", modes: FX_0_0_13_MODES, written: "code" },
284 { scenario: "auto-main", runtimeMode: "auto", modes: FX_MAIN_MODES, written: "auto" },
285 {
286 scenario: "full-access-main",
287 runtimeMode: "full-access",
288 modes: FX_MAIN_MODES,
289 written: "full-access",
290 },
291 ] as const,
292 ({ scenario, runtimeMode, modes, written }) =>
293 openFxSession({
294 scenario,
295 runtimeMode,
296 frames: [
297 ...initialize,
298 outbound("session/new", {
299 cwd: "<workspace>",
300 mcpServers: "<any>",
301 _meta: { fx: { profileMcpServers: true } },
302 }),
303 answer("session/new", fxSession({ model: FX_DEFAULT_MODEL, modes })),
304 ...setConfig(
305 "mode",
306 written,
307 fxConfig({ model: FX_DEFAULT_MODEL, modes, mode: written }),
308 ),
309 ],
310 }),
311 { discard: true },
312 ).pipe(Effect.provide(testLayer)),
313 );
314
315 it.effect("keeps supervised sessions in the mode set by their runtime policy", () =>
316 openFxSession({
317 scenario: "supervised-mode-policy",
318 runtimeMode: "approval-required",
319 modeOptions: [
320 { id: "_t3/session-mode", value: "auto" },
321 { id: "mode", value: "auto" },
322 ],
323 frames: [
324 ...initialize,
325 outbound("session/new", {
326 cwd: "<workspace>",
327 mcpServers: "<any>",
328 _meta: { fx: { profileMcpServers: true } },
329 }),
330 answer("session/new", fxSession({ model: FX_DEFAULT_MODEL, modes: FX_MAIN_MODES })),
331 ...setConfig("mode", "ask", fxConfig({ model: FX_DEFAULT_MODEL, modes: FX_MAIN_MODES })),
332 ],
333 }).pipe(Effect.provide(testLayer)),
334 );
335
336 it.effect("restores Default only when its backend is selectable", () =>
337 Effect.forEach(
338 [
339 { backend: "gateway", hasBackendOption: true, restore: true },
340 { backend: "unavailable", hasBackendOption: true, restore: false },
341 { backend: "gateway", hasBackendOption: false, restore: false },
342 ],
343 ({ backend, hasBackendOption, restore }) =>
344 openFxSession({
345 scenario: `default-model-${backend}-${hasBackendOption}`,
346 runtimeMode: "approval-required",
347 model: "default",
348 defaultBackend: backend,
349 loadSessionId: SESSION_ID,
350 frames: [
351 ...initialize,
352 outbound("session/load", {
353 sessionId: SESSION_ID,
354 cwd: "<workspace>",
355 mcpServers: "<any>",
356 _meta: { fx: { profileMcpServers: true } },
357 }),
358 answer("session/load", {
359 ...fxSession({ model: NAMED_MODEL, modes: FX_0_0_13_MODES }),
360 configOptions: fxConfig({ model: NAMED_MODEL, modes: FX_0_0_13_MODES }).filter(
361 (option) => hasBackendOption || option.id !== "provider",
362 ),
363 }),
364 ...(restore
365 ? setConfig(
366 "model",
367 FX_DEFAULT_MODEL,
368 fxConfig({ model: FX_DEFAULT_MODEL, modes: FX_0_0_13_MODES }),
369 )
370 : []),
371 ...setConfig(
372 "mode",
373 "ask",
374 fxConfig({ model: restore ? FX_DEFAULT_MODEL : NAMED_MODEL, modes: FX_0_0_13_MODES }),
375 ),
376 ],
377 }),
378 { discard: true },
379 ).pipe(Effect.provide(testLayer)),
380 );
381
382 // `fx status` names a gateway model, but this loaded session runs on Codex,
383 // whose catalog lacks it: the backend goes back first.
384 it.effect("moves a loaded session on another backend back to fx's default backend", () =>
385 Effect.gen(function* () {
386 for (const modelAvailable of [true, false]) {
387 const result = yield* openFxSession({
388 scenario: `default-other-backend-${modelAvailable}`,
389 runtimeMode: "approval-required",
390 model: "default",
391 loadSessionId: SESSION_ID,
392 frames: [
393 ...initialize,
394 outbound("session/load"),
395 answer(
396 "session/load",
397 fxSession({
398 model: CODEX_MODEL,
399 backend: "codex",
400 models: [CODEX_MODEL],
401 modes: FX_0_0_13_MODES,
402 }),
403 ),
404 // fx then runs the gateway model it saved for that backend.
405 ...setConfig(
406 "provider",
407 "gateway",
408 fxConfig({
409 model: NAMED_MODEL,
410 ...(modelAvailable ? {} : { models: [NAMED_MODEL] }),
411 modes: FX_0_0_13_MODES,
412 }),
413 ),
414 ...(modelAvailable
415 ? [
416 ...setConfig(
417 "model",
418 FX_DEFAULT_MODEL,
419 fxConfig({ model: FX_DEFAULT_MODEL, modes: FX_0_0_13_MODES }),
420 ),
421 ...setConfig(
422 "mode",
423 "ask",
424 fxConfig({ model: FX_DEFAULT_MODEL, modes: FX_0_0_13_MODES }),
425 ),
426 ]
427 : setConfig(
428 "provider",
429 "codex",
430 fxConfig({
431 backend: "codex",
432 model: CODEX_MODEL,
433 models: [CODEX_MODEL],
434 modes: FX_0_0_13_MODES,
435 }),
436 )),
437 ],
438 }).pipe(Effect.exit);
439 if (modelAvailable) {
440 assert.isTrue(Exit.isSuccess(result));
441 } else if (Exit.isFailure(result)) {
442 assert.include(
443 Cause.pretty(result.cause),
444 "The default fx model is unavailable on its configured backend.",
445 );
446 } else {
447 assert.fail("Expected Default selection to reject the unavailable model");
448 }
449 }
450 }).pipe(Effect.provide(testLayer)),
451 );
452
453 it.effect("switches the session's backend, then its model, for a backend's model", () =>
454 openFxSession({
455 scenario: "backend-model",
456 runtimeMode: "approval-required",
457 model: `codex:${CODEX_MODEL}`,
458 turnModel: "gateway:openai/gpt-5.6-sol",
459 frames: [
460 ...initialize,
461 outbound("session/new"),
462 answer("session/new", fxSession({ model: FX_DEFAULT_MODEL, modes: FX_0_0_13_MODES })),
463 // fx already picks the thread's model on Codex, so only the backend is written.
464 ...setConfig(
465 "provider",
466 "codex",
467 fxConfig({
468 model: CODEX_MODEL,
469 backend: "codex",
470 models: [CODEX_MODEL, "gpt-5.4-mini"],
471 modes: FX_0_0_13_MODES,
472 }),
473 ),
474 ...setConfig(
475 "mode",
476 "ask",
477 fxConfig({
478 model: CODEX_MODEL,
479 backend: "codex",
480 models: [CODEX_MODEL, "gpt-5.4-mini"],
481 modes: FX_0_0_13_MODES,
482 }),
483 ),
484 // The next turn moves the same session to a gateway model; the id
485 // after the backend goes to fx whole, slash included.
486 ...setConfig(
487 "provider",
488 "gateway",
489 fxConfig({ model: NAMED_MODEL, modes: FX_0_0_13_MODES }),
490 ),
491 ...setConfig(
492 "model",
493 "openai/gpt-5.6-sol",
494 fxConfig({ model: "openai/gpt-5.6-sol", modes: FX_0_0_13_MODES }),
495 ),
496 outbound("session/prompt"),
497 answer("session/prompt", { stopReason: "end_turn" }),
498 ],
499 }).pipe(Effect.provide(testLayer)),
500 );
501
502 // fx 0.0.13's gateway takes any model id (src/acp/server.zig
503 // handleSetConfigOption) and lists the current one even when its catalog
504 // does not, so a custom model reaches fx instead of running on another,
505 // and switching back afterwards leaves it again.
506 it.effect("sends a custom model fx does not list, and leaves it for Default", () =>
507 openFxSession({
508 scenario: "custom-model",
509 runtimeMode: "approval-required",
510 model: CUSTOM_MODEL,
511 turnModel: "default",
512 frames: [
513 ...initialize,
514 outbound("session/new"),
515 answer("session/new", fxSession({ model: FX_DEFAULT_MODEL, modes: FX_0_0_13_MODES })),
516 ...setConfig(
517 "model",
518 CUSTOM_MODEL,
519 fxConfig({ model: CUSTOM_MODEL, modes: FX_0_0_13_MODES }),
520 ),
521 ...setConfig("mode", "ask", fxConfig({ model: CUSTOM_MODEL, modes: FX_0_0_13_MODES })),
522 // Back on Default, which the session started on: fx runs the custom
523 // model now, so the write must go out again.
524 ...setConfig(
525 "model",
526 FX_DEFAULT_MODEL,
527 fxConfig({ model: FX_DEFAULT_MODEL, modes: FX_0_0_13_MODES }),
528 ),
529 outbound("session/prompt"),
530 answer("session/prompt", { stopReason: "end_turn" }),
531 ],
532 }).pipe(Effect.provide(testLayer)),
533 );
534 });