Open History to see the last commit
1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { GrokSettings } from "@t3tools/provider-grok/settings";
3import * as Effect from "effect/Effect";
4import * as FileSystem from "effect/FileSystem";
5import * as Layer from "effect/Layer";
6import * as Path from "effect/Path";
7import * as Schema from "effect/Schema";
8import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
9import * as HostProcess from "@t3tools/shared/HostProcess";
10import { resolveSelfInvocation } from "@t3tools/shared/nodeRuntime";
11
12import * as TestProviderHost from "@t3tools/provider-testing/TestProviderHost";
13import { GROK_ACP_CANCEL_META, GROK_ACP_INITIALIZE_META } from "@t3tools/provider-grok/testing";
14import { makeXAiPromptCompletionRuntime } from "@t3tools/provider-grok/testing";
15import * as IdAllocator from "@t3tools/provider-core/server/IdAllocator";
16import * as ProviderContinuationRequests from "@t3tools/provider-core/server/ProviderContinuationRequests";
17import * as ProviderAdapterRegistry from "../ProviderAdapterRegistry.ts";
18import type { ProviderReplayGate } from "@t3tools/provider-testing/replayGate";
19import type { OrchestratorV2ProviderReplayHarness } from "../testkit/ProviderReplayHarness.ts";
20import {
21 type AcpReplayTranscript,
22 AcpReplayTranscriptDecodeError,
23 decodeAcpReplayTranscript,
24 makeAcpReplayCompletenessAssertion,
25 makeAcpReplayRuntime,
26} from "./AcpAdapterV2.testkit.ts";
27import {
28 GROK_DEFAULT_INSTANCE_ID,
29 GROK_PROVIDER,
30 makeGrokAdapterV2,
31} from "@t3tools/provider-grok/testing";
32
33const DEFAULT_GROK_SETTINGS = Schema.decodeSync(GrokSettings)({});
34
35function layerGrokProviderAdapterRegistryReplay(
36 transcript: AcpReplayTranscript,
37 options: { readonly replayGate?: ProviderReplayGate } = {},
38) {
39 const layerHost = TestProviderHost.layer().pipe(Layer.provide(NodeServices.layer));
40
41 return ProviderAdapterRegistry.layerFromAdaptersEffect(
42 Effect.gen(function* () {
43 const fileSystem = yield* FileSystem.FileSystem;
44 const path = yield* Path.Path;
45 const childProcessSpawner = yield* ChildProcessSpawner.ChildProcessSpawner;
46 const hostPlatform = yield* HostProcess.Platform;
47 const continuationRequests = yield* ProviderContinuationRequests.ProviderContinuationRequests;
48 const replayGate = options.replayGate;
49 const replayDir = yield* fileSystem
50 .makeTempDirectory({
51 prefix: `t3-orchestration-v2-grok-replay-${transcript.scenario}-`,
52 })
53 .pipe(Effect.orDie);
54 const statusPath = path.join(replayDir, "status.json");
55 const scriptPath = yield* path
56 .fromFileUrl(new URL("../../../scripts/acp-replay-agent.ts", import.meta.url))
57 .pipe(Effect.orDie);
58 const adapter = yield* makeGrokAdapterV2({
59 instanceId: GROK_DEFAULT_INSTANCE_ID,
60 settings: DEFAULT_GROK_SETTINGS,
61 environment: {},
62 hostPlatform,
63 selfInvocation: yield* resolveSelfInvocation(),
64 makeRuntime: (runtimeInput) =>
65 makeAcpReplayRuntime({
66 transcript,
67 statusPath,
68 scriptPath,
69 childProcessSpawner,
70 fileSystem,
71 ...(replayGate === undefined ? {} : { replayGate }),
72 cancelMeta: GROK_ACP_CANCEL_META,
73 initializeMeta: GROK_ACP_INITIALIZE_META,
74 })(runtimeInput).pipe(Effect.flatMap(makeXAiPromptCompletionRuntime)),
75 continuationRequests,
76 assertComplete: makeAcpReplayCompletenessAssertion(fileSystem, statusPath, transcript),
77 ...(replayGate === undefined
78 ? {}
79 : {
80 testHooks: {
81 onDeferredFinalizeScheduled: (debounce) =>
82 Effect.sync(() => replayGate.recordFinishArmed(debounce)),
83 },
84 }),
85 });
86 return [adapter];
87 }),
88 ).pipe(
89 Layer.provide(Layer.mergeAll(layerHost, NodeServices.layer, IdAllocator.layer)),
90 // Held inbound lines must not outlive the scenario and wedge teardown.
91 Layer.merge(
92 Layer.effectDiscard(
93 Effect.addFinalizer(() => Effect.sync(() => options.replayGate?.releaseAll())),
94 ),
95 ),
96 );
97}
98
99export const GrokOrchestratorReplayHarness: OrchestratorV2ProviderReplayHarness<
100 AcpReplayTranscript,
101 AcpReplayTranscriptDecodeError
102> = {
103 driver: GROK_PROVIDER,
104 decodeTranscript: (transcript) => decodeAcpReplayTranscript(transcript, GROK_PROVIDER),
105 makeProviderAdapterRegistryLayer: layerGrokProviderAdapterRegistryReplay,
106};