-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate_download_session.js
More file actions
203 lines (195 loc) · 7.78 KB
/
Copy pathupdate_download_session.js
File metadata and controls
203 lines (195 loc) · 7.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
const fs = require('fs');
const path = require('path');
const file = path.join(__dirname, 'vessel/src/components/chat/DownloadSessionButton.tsx');
let content = fs.readFileSync(file, 'utf8');
const extractLlmFn = `
function extractLlmMetadata(
normalizedRuntimeEvents: RuntimeSystemEvent[],
debugInfo?: Partial<SessionDebugExport> | null,
) {
const outcomeEvent = findLatestEvent(
normalizedRuntimeEvents,
(e) => e.type === "LLM_ROUTE_OUTCOME",
);
let winningProviderKind: string | null = outcomeEvent
? readEventString(outcomeEvent, "providerKind")
: null;
let winningModelId: string | null = outcomeEvent
? readEventString(outcomeEvent, "modelId")
: null;
if (!winningProviderKind || !winningModelId) {
const providerTraceEvent = findLatestEvent(normalizedRuntimeEvents, (e) => {
if (e.type !== "TELEMETRY_INFRASTRUCTURE_EVENT") return false;
const snapshot = readTelemetryInputSnapshot(e);
return snapshot?.category === "provider_trace";
});
if (providerTraceEvent) {
const snapshot = readTelemetryInputSnapshot(providerTraceEvent);
if (snapshot) {
const pt = asRecord(snapshot.providerTrace);
if (pt) {
winningProviderKind =
winningProviderKind ??
(typeof pt.provider === "string" ? pt.provider : null);
winningModelId =
winningModelId ?? (typeof pt.model === "string" ? pt.model : null);
}
}
}
}
if (
(!winningProviderKind || !winningModelId) &&
Array.isArray(debugInfo?.timeline)
) {
for (const frame of debugInfo!.timeline as unknown[]) {
const frameRecord = asRecord(frame);
const frameEvents = Array.isArray(asRecord(frameRecord?.data)?.events)
? (asRecord(frameRecord?.data)!.events as unknown[])
: [];
for (const ev of frameEvents) {
const evRecord = asRecord(ev);
if (!evRecord) continue;
if (evRecord.type === "LLM_ROUTE_OUTCOME") {
winningProviderKind =
winningProviderKind ??
(typeof evRecord.providerKind === "string"
? evRecord.providerKind
: null);
winningModelId =
winningModelId ??
(typeof evRecord.modelId === "string" ? evRecord.modelId : null);
} else if (evRecord.type === "TELEMETRY_INFRASTRUCTURE_EVENT") {
const snap = asRecord(
asRecord(evRecord.payload)?.inputSnapshot ?? evRecord.inputSnapshot,
);
if (snap?.category === "provider_trace") {
const pt = asRecord(snap.providerTrace);
if (pt) {
winningProviderKind =
winningProviderKind ??
(typeof pt.provider === "string" ? pt.provider : null);
winningModelId =
winningModelId ??
(typeof pt.model === "string" ? pt.model : null);
}
}
}
if (winningProviderKind && winningModelId) break;
}
if (winningProviderKind && winningModelId) break;
}
}
return { provider: winningProviderKind, model: winningModelId };
}
`;
content = content.replace(
'export function buildSessionExportPayload',
extractLlmFn + '\nexport function buildSessionExportPayload'
);
const llmExtractionBlock = `// ── LLM metadata ──────────────────────────────────────────────────
// Primary: LLM_ROUTE_OUTCOME event (newly emitted by route.ts).
// Fallback 1: provider_trace telemetry event (always emitted).
// Fallback 2: debug timeline frames — used when the live runtime event
// buffer is empty (e.g. export triggered before events propagated to
// client) but the debug timeline captured the same events server-side.
const outcomeEvent = findLatestEvent(
normalizedRuntimeEvents,
(e) => e.type === "LLM_ROUTE_OUTCOME",
);
let winningProviderKind: string | null = outcomeEvent
? readEventString(outcomeEvent, "providerKind")
: null;
let winningModelId: string | null = outcomeEvent
? readEventString(outcomeEvent, "modelId")
: null;
if (!winningProviderKind || !winningModelId) {
const providerTraceEvent = findLatestEvent(normalizedRuntimeEvents, (e) => {
if (e.type !== "TELEMETRY_INFRASTRUCTURE_EVENT") return false;
const snapshot = readTelemetryInputSnapshot(e);
return snapshot?.category === "provider_trace";
});
if (providerTraceEvent) {
const snapshot = readTelemetryInputSnapshot(providerTraceEvent);
if (snapshot) {
const pt = asRecord(snapshot.providerTrace);
if (pt) {
winningProviderKind =
winningProviderKind ??
(typeof pt.provider === "string" ? pt.provider : null);
winningModelId =
winningModelId ?? (typeof pt.model === "string" ? pt.model : null);
}
}
}
}
// Fallback 2: scan debug timeline frames for LLM_ROUTE_OUTCOME or
// provider_trace when live runtime events were unavailable.
if (
(!winningProviderKind || !winningModelId) &&
Array.isArray(input.debugInfo?.timeline)
) {
for (const frame of input.debugInfo!.timeline as unknown[]) {
const frameRecord = asRecord(frame);
const frameEvents = Array.isArray(asRecord(frameRecord?.data)?.events)
? (asRecord(frameRecord?.data)!.events as unknown[])
: [];
for (const ev of frameEvents) {
const evRecord = asRecord(ev);
if (!evRecord) continue;
if (evRecord.type === "LLM_ROUTE_OUTCOME") {
winningProviderKind =
winningProviderKind ??
(typeof evRecord.providerKind === "string"
? evRecord.providerKind
: null);
winningModelId =
winningModelId ??
(typeof evRecord.modelId === "string" ? evRecord.modelId : null);
} else if (evRecord.type === "TELEMETRY_INFRASTRUCTURE_EVENT") {
const snap = asRecord(
asRecord(evRecord.payload)?.inputSnapshot ?? evRecord.inputSnapshot,
);
if (snap?.category === "provider_trace") {
const pt = asRecord(snap.providerTrace);
if (pt) {
winningProviderKind =
winningProviderKind ??
(typeof pt.provider === "string" ? pt.provider : null);
winningModelId =
winningModelId ??
(typeof pt.model === "string" ? pt.model : null);
}
}
}
if (winningProviderKind && winningModelId) break;
}
if (winningProviderKind && winningModelId) break;
}
}`;
content = content.replace(llmExtractionBlock, 'const { provider: winningProviderKind, model: winningModelId } = extractLlmMetadata(normalizedRuntimeEvents, input.debugInfo);');
const debugMetaSearch = ` exportType: "session",
llm: {
provider: winningProviderKind ?? null,
model: winningModelId ?? null,
},`;
content = content.replace(
` exportedAt: new Date().toISOString(),
sessionStartedAt: input.sessionStartedAt,
debugVersion: "2.0",`,
` exportedAt: new Date().toISOString(),
sessionStartedAt: input.sessionStartedAt,
debugVersion: "2.0",
llm: extractLlmMetadata(normalizedRuntimeEvents, input.debugInfo),`
);
content = content.replace(
` exportedAt: new Date().toISOString(),
sessionStartedAt: input.sessionStartedAt,
debugVersion: "1.0",
includes: ["pipeline_events", "diagnostics", "expected_block"],`,
` exportedAt: new Date().toISOString(),
sessionStartedAt: input.sessionStartedAt,
debugVersion: "1.0",
includes: ["pipeline_events", "diagnostics", "expected_block"],
llm: extractLlmMetadata(input.runtimeEvents ?? [], input.debugInfo),`
);
fs.writeFileSync(file, content);