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