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

GrokAdapterV2.testkit.ts

122 lines5.1 KB

1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { GrokSettings } from "@t3tools/contracts";
3import * as Effect from "effect/Effect";
4import * as Crypto from "effect/Crypto";
5import * as FileSystem from "effect/FileSystem";
6import * as Layer from "effect/Layer";
7import * as Path from "effect/Path";
8import * as Schema from "effect/Schema";
9import { ChildProcessSpawner } from "effect/unstable/process";
10import { HostProcessPlatform } from "@t3tools/shared/hostProcess";
11import { resolveSelfInvocation } from "@t3tools/shared/nodeRuntime";
12​
13import * as ServerConfig from "../../config.ts";
14import {
15 GROK_ACP_CANCEL_META,
16 GROK_ACP_INITIALIZE_META,
17} from "../../provider/acp/GrokAcpSupport.ts";
18import { makeXAiPromptCompletionRuntime } from "../../provider/acp/XAiAcpExtension.ts";
19import * as IdAllocator from "../IdAllocator.ts";
20import * as ProviderContinuationRequests from "../ProviderContinuationRequests.ts";
21import * as ProviderAdapterRegistry from "../ProviderAdapterRegistry.ts";
22import type { ProviderReplayGate } from "../testkit/ProviderReplayGate.testkit.ts";
23import type { OrchestratorV2ProviderReplayHarness } from "../testkit/ProviderReplayHarness.ts";
24import { makeReplayServerConfig } from "../testkit/ProviderReplayHarness.ts";
25import {
26 type AcpReplayTranscript,
27 AcpReplayTranscriptDecodeError,
28 decodeAcpReplayTranscript,
29 makeAcpReplayCompletenessAssertion,
30 makeAcpReplayRuntime,
31} from "./AcpAdapterV2.testkit.ts";
32import { GROK_DEFAULT_INSTANCE_ID, GROK_PROVIDER, makeGrokAdapterV2 } from "./GrokAdapterV2.ts";
33​
34const DEFAULT_GROK_SETTINGS = Schema.decodeUnknownSync(GrokSettings)({});
35​
36function makeGrokProviderAdapterRegistryReplayLayer(
37 transcript: AcpReplayTranscript,
38 options: { readonly replayGate?: ProviderReplayGate } = {},
39) {
40 const serverConfigLayer = Layer.effect(
41 ServerConfig.ServerConfig,
42 makeReplayServerConfig(`grok-${transcript.scenario}`).pipe(Effect.orDie),
43 ).pipe(Layer.provide(NodeServices.layer));
44​
45 return ProviderAdapterRegistry.makeLayerEffect(
46 Effect.gen(function* () {
47 const fileSystem = yield* FileSystem.FileSystem;
48 const path = yield* Path.Path;
49 const childProcessSpawner = yield* ChildProcessSpawner.ChildProcessSpawner;
50 const crypto = yield* Crypto.Crypto;
51 const hostPlatform = yield* HostProcessPlatform;
52 const idAllocator = yield* IdAllocator.IdAllocatorV2;
53 const serverConfig = yield* ServerConfig.ServerConfig;
54 // Same queue the continuation worker drains when the fixture runs it.
55 const continuationRequests = yield* ProviderContinuationRequests.ProviderContinuationRequests;
56 const replayGate = options.replayGate;
57 const replayDir = yield* fileSystem
58 .makeTempDirectory({
59 prefix: `t3-orchestration-v2-grok-replay-${transcript.scenario}-`,
60 })
61 .pipe(Effect.orDie);
62 const statusPath = path.join(replayDir, "status.json");
63 const scriptPath = yield* path
64 .fromFileUrl(new URL("../../../scripts/acp-replay-agent.ts", import.meta.url))
65 .pipe(Effect.orDie);
66 const adapter = makeGrokAdapterV2({
67 instanceId: GROK_DEFAULT_INSTANCE_ID,
68 settings: DEFAULT_GROK_SETTINGS,
69 environment: {},
70 hostPlatform,
71 childProcessSpawner,
72 crypto,
73 fileSystem,
74 idAllocator,
75 serverConfig,
76 selfInvocation: yield* resolveSelfInvocation(),
77 // Same wrapping as makeGrokAcpRuntime: client type and Ctrl+C cancel
78 // metadata and the x.ai prompt-completion race, so replay sends what
79 // production sends.
80 makeRuntime: (runtimeInput) =>
81 makeAcpReplayRuntime({
82 transcript,
83 statusPath,
84 scriptPath,
85 childProcessSpawner,
86 fileSystem,
87 ...(replayGate === undefined ? {} : { replayGate }),
88 cancelMeta: GROK_ACP_CANCEL_META,
89 initializeMeta: GROK_ACP_INITIALIZE_META,
90 })(runtimeInput).pipe(Effect.flatMap(makeXAiPromptCompletionRuntime)),
91 continuationRequests,
92 assertComplete: makeAcpReplayCompletenessAssertion(fileSystem, statusPath, transcript),
93 ...(replayGate === undefined
94 ? {}
95 : {
96 testHooks: {
97 onDeferredFinalizeScheduled: (debounce) =>
98 Effect.sync(() => replayGate.recordFinishArmed(debounce)),
99 },
100 }),
101 });
102 return [adapter];
103 }),
104 ).pipe(
105 Layer.provide(Layer.mergeAll(serverConfigLayer, NodeServices.layer, IdAllocator.layer)),
106 // Held inbound lines must not outlive the scenario and wedge teardown.
107 Layer.merge(
108 Layer.effectDiscard(
109 Effect.addFinalizer(() => Effect.sync(() => options.replayGate?.releaseAll())),
110 ),
111 ),
112 );
114​
115export const GrokOrchestratorReplayHarness: OrchestratorV2ProviderReplayHarness<
116 AcpReplayTranscript,
117 AcpReplayTranscriptDecodeError
118> = {
119 driver: GROK_PROVIDER,
120 decodeTranscript: (transcript) => decodeAcpReplayTranscript(transcript, GROK_PROVIDER),
121 makeProviderAdapterRegistryLayer: makeGrokProviderAdapterRegistryReplayLayer,
122};