Open History to see the last commit
1import type { ProviderDriverKind, ProviderReplayTranscript } from "@t3tools/contracts";
2
3import { buildRuntimeInstructions } from "@t3tools/provider-core/server/runtimeInstructions";
4
5export function materializeReplayTranscriptRuntimeInstructions(
6 transcript: ProviderReplayTranscript,
7 runtime: { readonly driver: ProviderDriverKind; readonly model: string },
8): ProviderReplayTranscript {
9 const harness =
10 runtime.driver === "cursor"
11 ? "Cursor"
12 : runtime.driver === "grok"
13 ? "Grok"
14 : runtime.driver === "acpRegistry"
15 ? "acpRegistry"
16 : undefined;
17 if (harness === undefined) return transcript;
18 const instructions = buildRuntimeInstructions({ harness, model: runtime.model });
19
20 return {
21 ...transcript,
22 entries: transcript.entries.map((entry) => {
23 if (entry.type !== "expect_outbound") return entry;
24 const frame = entry.frame;
25 if (typeof frame !== "object" || frame === null) return entry;
26 if (
27 runtime.driver === "cursor" &&
28 "type" in frame &&
29 frame.type === "run.start" &&
30 "message" in frame &&
31 typeof frame.message === "string"
32 ) {
33 return frame.message.endsWith(instructions)
34 ? entry
35 : { ...entry, frame: { ...frame, message: `${frame.message}\n\n${instructions}` } };
36 }
37 if (
38 "method" in frame &&
39 frame.method === "session/prompt" &&
40 "params" in frame &&
41 typeof frame.params === "object" &&
42 frame.params !== null &&
43 "prompt" in frame.params &&
44 Array.isArray(frame.params.prompt)
45 ) {
46 const prompt: ReadonlyArray<unknown> = frame.params.prompt;
47 const lastPart = prompt.at(-1);
48 if (
49 typeof lastPart === "object" &&
50 lastPart !== null &&
51 "type" in lastPart &&
52 lastPart.type === "text" &&
53 "text" in lastPart &&
54 lastPart.text === instructions
55 ) {
56 return entry;
57 }
58 return {
59 ...entry,
60 frame: {
61 ...frame,
62 params: { ...frame.params, prompt: [...prompt, { type: "text", text: instructions }] },
63 },
64 };
65 }
66 return entry;
67 }),
68 };
69}