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 === "kiro"
15 ? "Kiro"
16 : runtime.driver === "acpRegistry"
17 ? "acpRegistry"
18 : runtime.driver === "fx"
19 ? "fx"
20 : undefined;
21 if (harness === undefined) return transcript;
22 const instructions = buildRuntimeInstructions({ harness, model: runtime.model });
23
24 return {
25 ...transcript,
26 entries: transcript.entries.map((entry) => {
27 if (entry.type !== "expect_outbound") return entry;
28 const frame = entry.frame;
29 if (typeof frame !== "object" || frame === null) return entry;
30 if (
31 runtime.driver === "cursor" &&
32 "type" in frame &&
33 frame.type === "run.start" &&
34 "message" in frame &&
35 typeof frame.message === "string"
36 ) {
37 return frame.message.endsWith(instructions)
38 ? entry
39 : { ...entry, frame: { ...frame, message: `${frame.message}\n\n${instructions}` } };
40 }
41 if (
42 "method" in frame &&
43 frame.method === "session/prompt" &&
44 "params" in frame &&
45 typeof frame.params === "object" &&
46 frame.params !== null &&
47 "prompt" in frame.params &&
48 Array.isArray(frame.params.prompt)
49 ) {
50 const prompt: ReadonlyArray<unknown> = frame.params.prompt;
51 const lastPart = prompt.at(-1);
52 if (
53 typeof lastPart === "object" &&
54 lastPart !== null &&
55 "type" in lastPart &&
56 lastPart.type === "text" &&
57 "text" in lastPart &&
58 typeof lastPart.text === "string" &&
59 // A recording that switched models mid-thread already carries the
60 // instructions for the model each prompt ran on.
61 (lastPart.text === instructions || lastPart.text.startsWith("<runtime_info>"))
62 ) {
63 return entry;
64 }
65 return {
66 ...entry,
67 frame: {
68 ...frame,
69 params: { ...frame.params, prompt: [...prompt, { type: "text", text: instructions }] },
70 },
71 };
72 }
73 return entry;
74 }),
75 };
76}