@tangle-network/agent-app 0.63.61 → 0.63.62
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{DesignCanvasEditor-YWDEFWYQ.js → DesignCanvasEditor-6XNHSREG.js} +3 -3
- package/dist/agent-profiles/d1.js +3 -3
- package/dist/agent-profiles/index.js +3 -3
- package/dist/assistant/index.js +5 -5
- package/dist/chat-react/index.js +3 -3
- package/dist/chat-routes/agent-producers.d.ts +11 -10
- package/dist/chat-routes/dispatch-parts.d.ts +24 -2
- package/dist/chat-routes/hybrid-producer.d.ts +6 -2
- package/dist/chat-routes/index.js +13 -11
- package/dist/chat-routes/index.js.map +1 -1
- package/dist/chat-routes/turn-route-contracts.d.ts +4 -3
- package/dist/chat-routes/wire.d.ts +6 -0
- package/dist/chat-store/index.js +2 -2
- package/dist/{chunk-6XSQCP6W.js → chunk-43CV4JIT.js} +2 -2
- package/dist/{chunk-SFZELD6V.js → chunk-4JTEPK2G.js} +2 -2
- package/dist/{chunk-N5KBDRSX.js → chunk-5ERXADSW.js} +2 -2
- package/dist/{chunk-F2N55QQX.js → chunk-73AVOSH6.js} +136 -80
- package/dist/chunk-73AVOSH6.js.map +1 -0
- package/dist/{chunk-RNFVAAJJ.js → chunk-7O62NYGT.js} +2 -2
- package/dist/{chunk-DPZXCVVS.js → chunk-AHBVQRJA.js} +2 -2
- package/dist/{chunk-UTF7H6BK.js → chunk-EUQ46YWK.js} +15 -15
- package/dist/chunk-EUQ46YWK.js.map +1 -0
- package/dist/{chunk-PIGW5LRM.js → chunk-FJRQCF43.js} +3 -3
- package/dist/{chunk-MIQL36JS.js → chunk-GAYWV565.js} +2 -2
- package/dist/{chunk-5C3RXQQ7.js → chunk-H3DAXUKV.js} +2 -2
- package/dist/{chunk-SRA7VAVK.js → chunk-HJYEK4SU.js} +2 -2
- package/dist/{chunk-75I4RXIE.js → chunk-IAK227OO.js} +2 -2
- package/dist/{chunk-F6UC2T6U.js → chunk-K2JPSXKI.js} +6 -1
- package/dist/{chunk-F6UC2T6U.js.map → chunk-K2JPSXKI.js.map} +1 -1
- package/dist/{chunk-QFIVY6WA.js → chunk-NJW4JCSF.js} +2 -2
- package/dist/{chunk-O2TYB3DH.js → chunk-Q55WW6DO.js} +15 -15
- package/dist/{chunk-RVC7EHJI.js → chunk-RYKANV3P.js} +2056 -1983
- package/dist/chunk-RYKANV3P.js.map +1 -0
- package/dist/{chunk-TFV4QOCG.js → chunk-VQIH5NAB.js} +2 -2
- package/dist/{chunk-DCGATWZ5.js → chunk-XPB2UYHU.js} +5 -5
- package/dist/{chunk-DCGATWZ5.js.map → chunk-XPB2UYHU.js.map} +1 -1
- package/dist/design-canvas-react/index.js +3 -3
- package/dist/design-canvas-react/lazy.js +1 -1
- package/dist/operator/index.js +2 -2
- package/dist/preset-cloudflare/index.js +6 -6
- package/dist/public-consultation/index.js +9 -9
- package/dist/sandbox/index.js +6 -6
- package/dist/theme/brand.generated.d.ts +15 -15
- package/dist/theme/dom.js +2 -2
- package/dist/theme/index.js +1 -1
- package/dist/theme/tokens.css +210 -356
- package/dist/tools/index.js +4 -4
- package/dist/web-react/document.js +2 -2
- package/dist/web-react/index.d.ts +3 -0
- package/dist/web-react/index.js +15 -5
- package/dist/web-react/message-projection.d.ts +19 -0
- package/dist/work-product/index.js +2 -2
- package/package.json +4 -4
- package/dist/chunk-F2N55QQX.js.map +0 -1
- package/dist/chunk-RVC7EHJI.js.map +0 -1
- package/dist/chunk-UTF7H6BK.js.map +0 -1
- /package/dist/{DesignCanvasEditor-YWDEFWYQ.js.map → DesignCanvasEditor-6XNHSREG.js.map} +0 -0
- /package/dist/{chunk-6XSQCP6W.js.map → chunk-43CV4JIT.js.map} +0 -0
- /package/dist/{chunk-SFZELD6V.js.map → chunk-4JTEPK2G.js.map} +0 -0
- /package/dist/{chunk-N5KBDRSX.js.map → chunk-5ERXADSW.js.map} +0 -0
- /package/dist/{chunk-RNFVAAJJ.js.map → chunk-7O62NYGT.js.map} +0 -0
- /package/dist/{chunk-DPZXCVVS.js.map → chunk-AHBVQRJA.js.map} +0 -0
- /package/dist/{chunk-PIGW5LRM.js.map → chunk-FJRQCF43.js.map} +0 -0
- /package/dist/{chunk-MIQL36JS.js.map → chunk-GAYWV565.js.map} +0 -0
- /package/dist/{chunk-5C3RXQQ7.js.map → chunk-H3DAXUKV.js.map} +0 -0
- /package/dist/{chunk-SRA7VAVK.js.map → chunk-HJYEK4SU.js.map} +0 -0
- /package/dist/{chunk-75I4RXIE.js.map → chunk-IAK227OO.js.map} +0 -0
- /package/dist/{chunk-QFIVY6WA.js.map → chunk-NJW4JCSF.js.map} +0 -0
- /package/dist/{chunk-O2TYB3DH.js.map → chunk-Q55WW6DO.js.map} +0 -0
- /package/dist/{chunk-TFV4QOCG.js.map → chunk-VQIH5NAB.js.map} +0 -0
|
@@ -27,7 +27,7 @@ import {
|
|
|
27
27
|
sandboxRunFailureDetail,
|
|
28
28
|
streamSandboxPrompt,
|
|
29
29
|
turnStorageBindings
|
|
30
|
-
} from "./chunk-
|
|
30
|
+
} from "./chunk-Q55WW6DO.js";
|
|
31
31
|
import {
|
|
32
32
|
redactErrorMessage
|
|
33
33
|
} from "./chunk-MH74DY2I.js";
|
|
@@ -37,7 +37,7 @@ import {
|
|
|
37
37
|
createAssistantDraftWriter,
|
|
38
38
|
rowIdOf,
|
|
39
39
|
storeSupportsDraftPersistence
|
|
40
|
-
} from "./chunk-
|
|
40
|
+
} from "./chunk-7O62NYGT.js";
|
|
41
41
|
import {
|
|
42
42
|
normalizeClientTurnId,
|
|
43
43
|
resolveChatTurn
|
|
@@ -46,7 +46,7 @@ import {
|
|
|
46
46
|
historyContentWithAttachments,
|
|
47
47
|
mentionInputToPart,
|
|
48
48
|
toChatMessageParts
|
|
49
|
-
} from "./chunk-
|
|
49
|
+
} from "./chunk-GAYWV565.js";
|
|
50
50
|
import {
|
|
51
51
|
asRecord,
|
|
52
52
|
asString,
|
|
@@ -97,7 +97,7 @@ import {
|
|
|
97
97
|
parseChatTurnParts,
|
|
98
98
|
parseMentions,
|
|
99
99
|
turnResponseEventFor
|
|
100
|
-
} from "./chunk-
|
|
100
|
+
} from "./chunk-K2JPSXKI.js";
|
|
101
101
|
import {
|
|
102
102
|
ModelFailoverExhaustedError,
|
|
103
103
|
buildModelChain,
|
|
@@ -132,2108 +132,2181 @@ import {
|
|
|
132
132
|
isWorkspaceFileExportable
|
|
133
133
|
} from "./chunk-TXD5HXLE.js";
|
|
134
134
|
|
|
135
|
-
// src/chat-routes/
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
}
|
|
145
|
-
return {
|
|
146
|
-
reasoning(run) {
|
|
147
|
-
if (open && open.run === run) return;
|
|
148
|
-
settle();
|
|
149
|
-
const time = runs.get(run);
|
|
150
|
-
if (time) delete time.end;
|
|
151
|
-
else runs.set(run, { start: now() });
|
|
152
|
-
open = { run };
|
|
153
|
-
},
|
|
154
|
-
settle,
|
|
155
|
-
time(run, own) {
|
|
156
|
-
const time = runs.get(run);
|
|
157
|
-
if (!time) return own;
|
|
158
|
-
const start = own?.start ?? time.start;
|
|
159
|
-
const end = own?.end ?? time.end;
|
|
160
|
-
return { ...start !== void 0 ? { start } : {}, ...end !== void 0 ? { end } : {} };
|
|
161
|
-
}
|
|
162
|
-
};
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
// src/chat-routes/router-producer.ts
|
|
166
|
-
function createRouterTurnProducer(options) {
|
|
167
|
-
const abort = new AbortController();
|
|
168
|
-
const signal = options.signal ? AbortSignal.any([abort.signal, options.signal]) : abort.signal;
|
|
169
|
-
void options.admitted?.catch((error) => {
|
|
170
|
-
abort.abort(error);
|
|
171
|
-
});
|
|
172
|
-
const credential = Promise.resolve(options.apiKey);
|
|
173
|
-
let credentialSettled = false;
|
|
174
|
-
void credential.then(() => {
|
|
175
|
-
credentialSettled = true;
|
|
176
|
-
}, () => {
|
|
177
|
-
credentialSettled = true;
|
|
178
|
-
});
|
|
179
|
-
let retired;
|
|
180
|
-
const retire = () => retired ??= credential.then(async (key) => {
|
|
181
|
-
if (typeof key !== "string") await key.revoke();
|
|
182
|
-
}, () => void 0).catch((error) => {
|
|
183
|
-
console.error(JSON.stringify({ event: "router_turn_key_retire_failed", error: redactErrorMessage(error) }));
|
|
184
|
-
});
|
|
185
|
-
let stop = () => {
|
|
186
|
-
};
|
|
187
|
-
const cancelled = new Promise((_resolve, reject) => {
|
|
188
|
-
stop = reject;
|
|
189
|
-
});
|
|
190
|
-
const stopped = () => {
|
|
191
|
-
stop(signal.reason);
|
|
192
|
-
void retire();
|
|
193
|
-
};
|
|
194
|
-
signal.addEventListener("abort", stopped, { once: true });
|
|
195
|
-
if (signal.aborted) stopped();
|
|
196
|
-
const queue = [];
|
|
197
|
-
let wake = null;
|
|
198
|
-
let done = false;
|
|
199
|
-
let outcome = null;
|
|
200
|
-
let usageReported = false;
|
|
201
|
-
const parts = [];
|
|
202
|
-
const toolParts = /* @__PURE__ */ new Map();
|
|
203
|
-
let textPart = null;
|
|
204
|
-
let textSegment = 0;
|
|
205
|
-
let reasoningPart = null;
|
|
206
|
-
const reasoningClock = createReasoningClock();
|
|
207
|
-
const push = (event) => {
|
|
208
|
-
queue.push(event);
|
|
209
|
-
wake?.();
|
|
210
|
-
};
|
|
211
|
-
const run = Promise.race([Promise.all([credential, options.systemPrompt]), cancelled]).then(([key, prompt]) => {
|
|
212
|
-
signal.throwIfAborted();
|
|
213
|
-
return runRouterTurn({
|
|
214
|
-
...options,
|
|
215
|
-
apiKey: typeof key === "string" ? key : key.key,
|
|
216
|
-
fetchImpl: typeof key === "string" ? options.fetchImpl : key.fetchImpl ?? options.fetchImpl,
|
|
217
|
-
signal,
|
|
218
|
-
tools: options.tools?.map((tool) => ({ ...tool, run: async (...args) => {
|
|
219
|
-
await options.admitted;
|
|
220
|
-
signal.throwIfAborted();
|
|
221
|
-
return tool.run(...args);
|
|
222
|
-
} })),
|
|
223
|
-
systemPrompt: typeof prompt === "string" ? prompt : composeRouterTurnPromptSections(prompt.profile, prompt).text,
|
|
224
|
-
showReasoning: options.showReasoning ?? true,
|
|
225
|
-
onEvent: (event) => {
|
|
226
|
-
if (event.type === "text") {
|
|
227
|
-
if (event.segment !== textSegment || !textPart) {
|
|
228
|
-
textSegment = event.segment;
|
|
229
|
-
textPart = { type: "text", text: "" };
|
|
230
|
-
parts.push(textPart);
|
|
231
|
-
}
|
|
232
|
-
textPart.text += event.delta;
|
|
233
|
-
reasoningPart = null;
|
|
234
|
-
reasoningClock.settle();
|
|
235
|
-
push({ type: "text", text: event.delta });
|
|
236
|
-
} else if (event.type === "reasoning") {
|
|
237
|
-
if (!reasoningPart) {
|
|
238
|
-
reasoningPart = { type: "reasoning", text: "" };
|
|
239
|
-
parts.push(reasoningPart);
|
|
240
|
-
}
|
|
241
|
-
reasoningPart.text += event.delta;
|
|
242
|
-
reasoningClock.reasoning(reasoningPart);
|
|
243
|
-
push({ type: "reasoning", text: event.delta });
|
|
244
|
-
} else if (event.type === "tool") {
|
|
245
|
-
reasoningPart = null;
|
|
246
|
-
reasoningClock.settle(event.startedAt);
|
|
247
|
-
if (event.status === "running") {
|
|
248
|
-
const part = {
|
|
249
|
-
type: "tool",
|
|
250
|
-
id: event.callId,
|
|
251
|
-
callID: event.callId,
|
|
252
|
-
tool: event.toolName,
|
|
253
|
-
state: { status: "running", input: event.input, time: { start: event.startedAt } }
|
|
254
|
-
};
|
|
255
|
-
toolParts.set(event.callId, part);
|
|
256
|
-
parts.push(part);
|
|
257
|
-
push({ type: "tool_call", call: { toolCallId: event.callId, toolName: event.toolName, args: event.input } });
|
|
258
|
-
} else {
|
|
259
|
-
const part = toolParts.get(event.callId);
|
|
260
|
-
const state = event.status === "completed" ? { status: "completed", output: event.output, time: { start: event.startedAt, end: event.endedAt } } : { status: "error", error: event.error.message, time: { start: event.startedAt, end: event.endedAt } };
|
|
261
|
-
if (part) part.state = { ...part.state, ...state };
|
|
262
|
-
push({
|
|
263
|
-
type: "tool_result",
|
|
264
|
-
toolCallId: event.callId,
|
|
265
|
-
toolName: event.toolName,
|
|
266
|
-
outcome: event.status === "completed" ? { ok: true, result: event.output } : { ok: false, message: event.error.message, code: event.error.code }
|
|
267
|
-
});
|
|
268
|
-
}
|
|
269
|
-
} else if (event.type === "repeat") {
|
|
270
|
-
reasoningPart = null;
|
|
271
|
-
reasoningClock.settle();
|
|
272
|
-
} else if (event.type === "usage") {
|
|
273
|
-
usageReported = true;
|
|
274
|
-
push({ type: "usage", usage: {
|
|
275
|
-
promptTokens: event.promptTokens,
|
|
276
|
-
completionTokens: event.completionTokens,
|
|
277
|
-
...event.cachedPromptTokens !== void 0 ? { cachedPromptTokens: event.cachedPromptTokens } : {}
|
|
278
|
-
} });
|
|
279
|
-
} else if (event.type === "tool_event") {
|
|
280
|
-
const part = toolParts.get(event.callId);
|
|
281
|
-
if (part) {
|
|
282
|
-
const state = part.state;
|
|
283
|
-
part.state = { ...state, events: [...state.events ?? [], { kind: event.kind, data: event.data, at: event.at }] };
|
|
284
|
-
}
|
|
285
|
-
push({ type: "tool.event", data: { toolCallId: event.callId, toolName: event.toolName, kind: event.kind, data: event.data, at: event.at } });
|
|
286
|
-
}
|
|
287
|
-
if (options.onEvent) {
|
|
288
|
-
try {
|
|
289
|
-
options.onEvent(event);
|
|
290
|
-
} catch {
|
|
291
|
-
}
|
|
292
|
-
}
|
|
293
|
-
}
|
|
294
|
-
});
|
|
295
|
-
}).then(async (result) => {
|
|
296
|
-
if (result.continuation && !result.failure) {
|
|
297
|
-
result.failure = { code: "turn_deadline", message: "The reply was stopped before it finished." };
|
|
298
|
-
}
|
|
299
|
-
outcome = result;
|
|
300
|
-
const final = await options.onOutcome?.(result);
|
|
301
|
-
if (final && final.trailingText) {
|
|
302
|
-
result.text += final.trailingText;
|
|
303
|
-
parts.push({ type: "text", text: final.trailingText });
|
|
304
|
-
push({ type: "text", text: final.trailingText });
|
|
305
|
-
}
|
|
306
|
-
if (final && final.failure) result.failure = final.failure;
|
|
307
|
-
if (result.failure) {
|
|
308
|
-
parts.push(noticePart("error", "router-error", result.failure.message, result.failure.code));
|
|
309
|
-
push({ type: "error", data: { message: result.failure.message, code: result.failure.code } });
|
|
310
|
-
}
|
|
311
|
-
}).finally(async () => {
|
|
312
|
-
const retiring = retire();
|
|
313
|
-
if (credentialSettled) await retiring;
|
|
314
|
-
try {
|
|
315
|
-
await options.onSettled?.();
|
|
316
|
-
} catch (error) {
|
|
317
|
-
console.error(JSON.stringify({ event: "router_turn_settle_failed", error: redactErrorMessage(error) }));
|
|
318
|
-
}
|
|
319
|
-
signal.removeEventListener("abort", stopped);
|
|
320
|
-
reasoningClock.settle();
|
|
321
|
-
done = true;
|
|
322
|
-
wake?.();
|
|
323
|
-
});
|
|
324
|
-
void run.catch(() => void 0);
|
|
325
|
-
async function* stream() {
|
|
326
|
-
for (; ; ) {
|
|
327
|
-
while (queue.length) yield queue.shift();
|
|
328
|
-
if (done) break;
|
|
329
|
-
await new Promise((resolve) => {
|
|
330
|
-
wake = resolve;
|
|
331
|
-
});
|
|
332
|
-
wake = null;
|
|
333
|
-
}
|
|
334
|
-
await run;
|
|
335
|
-
}
|
|
336
|
-
const snapshot = () => parts.map((part) => part.type === "reasoning" ? { ...part, time: reasoningClock.time(part) } : { ...part });
|
|
337
|
-
const usage = () => !outcome || !usageReported ? {} : {
|
|
338
|
-
// The Router's prompt tokens include the cached ones; the sandbox lanes report input and cache reads apart, so
|
|
339
|
-
// the stored row counts them apart here too and `inputTokens + cacheReadTokens` is the prompt on either lane.
|
|
340
|
-
inputTokens: outcome.usage.promptTokens - (outcome.usage.cachedPromptTokens ?? 0),
|
|
341
|
-
outputTokens: outcome.usage.completionTokens,
|
|
342
|
-
...outcome.usage.cachedPromptTokens !== void 0 ? { cacheReadTokens: outcome.usage.cachedPromptTokens } : {}
|
|
343
|
-
};
|
|
344
|
-
const events = stream();
|
|
345
|
-
const end = events.return.bind(events);
|
|
346
|
-
const fail = events.throw.bind(events);
|
|
347
|
-
events.return = async (value) => {
|
|
348
|
-
abort.abort(new Error("The chat route stopped reading this turn"));
|
|
349
|
-
try {
|
|
350
|
-
return await end(value);
|
|
351
|
-
} finally {
|
|
352
|
-
const retiring = retire();
|
|
353
|
-
if (credentialSettled) await retiring;
|
|
354
|
-
}
|
|
355
|
-
};
|
|
356
|
-
events.throw = async (error) => {
|
|
357
|
-
abort.abort(error);
|
|
358
|
-
try {
|
|
359
|
-
return await fail(error);
|
|
360
|
-
} finally {
|
|
361
|
-
const retiring = retire();
|
|
362
|
-
if (credentialSettled) await retiring;
|
|
363
|
-
}
|
|
364
|
-
};
|
|
365
|
-
return {
|
|
366
|
-
stream: events,
|
|
367
|
-
// The route tracks this continuation even when failure is visible before a pending key arrives.
|
|
368
|
-
cleanup: run.then(retire, retire),
|
|
369
|
-
finalText: () => outcome?.text ?? "",
|
|
370
|
-
assistantParts: snapshot,
|
|
371
|
-
draftParts: snapshot,
|
|
372
|
-
usage,
|
|
373
|
-
get model() {
|
|
374
|
-
return outcome?.servedModel ?? void 0;
|
|
375
|
-
},
|
|
376
|
-
modelAttribution: () => ({
|
|
377
|
-
requestedModel: options.model,
|
|
378
|
-
...outcome?.servedModel ? { servedModel: outcome.servedModel } : {},
|
|
379
|
-
echoReceived: Boolean(outcome?.servedModel)
|
|
380
|
-
})
|
|
381
|
-
};
|
|
382
|
-
}
|
|
383
|
-
|
|
384
|
-
// src/chat-routes/hybrid-producer.ts
|
|
385
|
-
var HYBRID_TURN_GUIDANCE = "Start your reply with text: before your first tool call, write one short sentence telling the member what you are about to do, so they never wait in silence. Use sandbox_run only for work that needs code execution or files on the workspace computer. Give the sandbox agent the member's requirements, paths and exact field names, not model-written code; the sandbox agent writes and runs the code. Report what it returns.";
|
|
386
|
-
var DEFAULT_SANDBOX_RUN_TIMEOUT_MS = 4 * 6e4;
|
|
387
|
-
function startHybridBox(body, acquire) {
|
|
388
|
-
const attachments = (body.parts ?? []).map((part) => ({
|
|
389
|
-
...part.mediaType ? { mediaType: part.mediaType } : {},
|
|
390
|
-
...part.filename ? { name: part.filename } : {}
|
|
391
|
-
}));
|
|
392
|
-
if (escalationHints({ text: body.content ?? "", attachments }).length === 0) return void 0;
|
|
393
|
-
const started = acquire();
|
|
394
|
-
started.catch(() => void 0);
|
|
395
|
-
return started;
|
|
396
|
-
}
|
|
397
|
-
function turnPromptText(prompt) {
|
|
398
|
-
return typeof prompt === "string" ? prompt : prompt.map((part) => part.type === "text" ? part.text : "").join("\n");
|
|
399
|
-
}
|
|
400
|
-
function turnCarriesFiles(prompt) {
|
|
401
|
-
return typeof prompt !== "string" && prompt.some((part) => part.type !== "text");
|
|
402
|
-
}
|
|
403
|
-
function routerTurnHistory(priorMessages, limit = 40) {
|
|
404
|
-
return priorMessages.filter((message) => (message.role === "user" || message.role === "assistant") && Boolean(message.content?.trim())).slice(-limit).map(({ role, content }) => ({ role, content }));
|
|
405
|
-
}
|
|
406
|
-
function createHybridTurnProducer(options) {
|
|
407
|
-
const { box, tools = [], speculateOn, attachments, onSpeculate, guidance, onSettled, admitted, signal, ...router } = options;
|
|
408
|
-
const abort = new AbortController();
|
|
409
|
-
const stop = signal ? AbortSignal.any([abort.signal, signal]) : abort.signal;
|
|
410
|
-
void admitted?.catch((error) => {
|
|
411
|
-
abort.abort(error);
|
|
412
|
-
});
|
|
413
|
-
const timeoutMs = box.timeoutMs ?? DEFAULT_SANDBOX_RUN_TIMEOUT_MS;
|
|
414
|
-
let activeTasks = 0;
|
|
415
|
-
let unconfirmed = false;
|
|
416
|
-
let start = box.started?.catch((error) => {
|
|
417
|
-
start = void 0;
|
|
418
|
-
throw error;
|
|
419
|
-
});
|
|
420
|
-
start?.catch(() => void 0);
|
|
421
|
-
const acquire = async () => {
|
|
422
|
-
await admitted;
|
|
423
|
-
stop.throwIfAborted();
|
|
424
|
-
return start ??= box.acquire(stop).catch((error) => {
|
|
425
|
-
start = void 0;
|
|
426
|
-
throw error;
|
|
427
|
-
});
|
|
428
|
-
};
|
|
429
|
-
let settled;
|
|
430
|
-
const settle = () => settled ??= (async () => {
|
|
431
|
-
abort.abort(new Error("The hybrid turn settled"));
|
|
432
|
-
await (box.started ?? start)?.catch(() => void 0);
|
|
433
|
-
const results = await Promise.allSettled([Promise.resolve().then(() => box.dispose?.()), Promise.resolve().then(() => onSettled?.())]);
|
|
434
|
-
for (const result of results) if (result.status === "rejected") {
|
|
435
|
-
console.error(JSON.stringify({ event: "hybrid_turn_settle_failed", error: redactErrorMessage(result.reason) }));
|
|
436
|
-
}
|
|
437
|
-
})();
|
|
438
|
-
try {
|
|
439
|
-
const step = box.computer ? createComputerStep({ ...box.computer, parentExecutionId: box.computer.parentExecutionId ?? router.traceId }) : void 0;
|
|
440
|
-
const runBoxTask = box.runTask ?? step?.runTask;
|
|
441
|
-
if (!runBoxTask) throw new Error("A hybrid box needs runTask or computer");
|
|
442
|
-
const warnings = /* @__PURE__ */ new Map();
|
|
443
|
-
const details = /* @__PURE__ */ new Map();
|
|
444
|
-
const withWarnings = (parts) => parts.flatMap((part) => {
|
|
445
|
-
const warning = warnings.get(String(part.callID ?? part.id ?? ""));
|
|
446
|
-
return warning ? [part, noticePart(warning.noticeKind, warning.id, warning.text, warning.code, warning.detail)] : [part];
|
|
447
|
-
});
|
|
448
|
-
const sandboxRun = createSandboxRunTool({
|
|
449
|
-
box: () => acquire(),
|
|
450
|
-
runTask: async (...args) => {
|
|
451
|
-
await admitted;
|
|
452
|
-
stop.throwIfAborted();
|
|
453
|
-
if (unconfirmed || activeTasks > 0) throw new SandboxRunError("sandbox_unconfirmed", "An earlier computer step may still be running; no further task was dispatched.");
|
|
454
|
-
activeTasks++;
|
|
455
|
-
try {
|
|
456
|
-
return await runBoxTask(...args);
|
|
457
|
-
} catch (error) {
|
|
458
|
-
if (!(error instanceof SandboxRunError) || error.code === "sandbox_unconfirmed") unconfirmed = true;
|
|
459
|
-
throw error;
|
|
460
|
-
} finally {
|
|
461
|
-
activeTasks--;
|
|
462
|
-
}
|
|
463
|
-
},
|
|
464
|
-
memberRequest: router.userMessage,
|
|
465
|
-
timeoutMs,
|
|
466
|
-
signal: stop,
|
|
467
|
-
...box.warm ? { warm: box.warm } : {},
|
|
468
|
-
onFailure: (error, call) => {
|
|
469
|
-
const detail = box.failureDetail ? box.failureDetail(error) : sandboxRunFailureDetail(error);
|
|
470
|
-
if (call.callId && detail) details.set(call.callId, detail);
|
|
471
|
-
if (box.onFailure) box.onFailure(error, call);
|
|
472
|
-
else console.error(JSON.stringify({
|
|
473
|
-
event: "agent_app_sandbox_run_failed",
|
|
474
|
-
callId: call.callId ?? null,
|
|
475
|
-
code: error.code,
|
|
476
|
-
detail: sandboxRunFailureDetail(error)
|
|
477
|
-
}));
|
|
478
|
-
},
|
|
479
|
-
...box.name ? { name: box.name } : {},
|
|
480
|
-
...box.description ? { description: box.description } : {}
|
|
481
|
-
});
|
|
482
|
-
const speculator = createEscalationSpeculator({
|
|
483
|
-
warm: acquire,
|
|
484
|
-
escalationTools: [sandboxRun.name],
|
|
485
|
-
...onSpeculate ? { onSpeculate } : {}
|
|
486
|
-
});
|
|
487
|
-
speculator.onInput({ text: speculateOn ?? router.userMessage, ...attachments ? { attachments } : {} });
|
|
488
|
-
const extra = guidance === false ? "" : [guidance ?? HYBRID_TURN_GUIDANCE, step?.guidance].filter(Boolean).join("\n\n");
|
|
489
|
-
const producer = createRouterTurnProducer({
|
|
490
|
-
...router,
|
|
491
|
-
systemPrompt: Promise.resolve(router.systemPrompt).then((prompt) => !extra ? prompt : typeof prompt === "string" ? `${prompt}
|
|
492
|
-
|
|
493
|
-
${extra}` : { ...prompt, stable: [...prompt.stable ?? [], extra] }),
|
|
494
|
-
tools: [...tools, sandboxRun],
|
|
495
|
-
admitted,
|
|
496
|
-
// A turn that calls the box must outlive the call it waits on.
|
|
497
|
-
budgetMs: router.budgetMs ?? timeoutMs + 6e4,
|
|
498
|
-
signal: stop,
|
|
499
|
-
onSettled: settle,
|
|
500
|
-
onEvent: (event) => {
|
|
501
|
-
speculator.onEvent(event);
|
|
502
|
-
if (event.type === "tool" && event.status === "error" && event.toolName === sandboxRun.name && !stop.aborted) {
|
|
503
|
-
const code = event.error.code;
|
|
504
|
-
if (code === "sandbox_unconfirmed") unconfirmed = true;
|
|
505
|
-
if (code === "sandbox_unavailable" || code === "sandbox_failed" || code === "sandbox_unconfirmed" || code === "sandbox_empty") {
|
|
506
|
-
const detail = details.get(event.callId);
|
|
507
|
-
warnings.set(event.callId, {
|
|
508
|
-
type: "notice",
|
|
509
|
-
noticeKind: "warning",
|
|
510
|
-
id: `sandbox-warning:${event.callId}`,
|
|
511
|
-
code,
|
|
512
|
-
text: code === "sandbox_unavailable" ? "The workspace computer is unavailable. I can continue with work that does not need it." : "The computer step did not return a confirmed answer. I can continue with work that does not need it.",
|
|
513
|
-
...detail ? { detail } : {}
|
|
514
|
-
});
|
|
515
|
-
}
|
|
516
|
-
}
|
|
517
|
-
router.onEvent?.(event);
|
|
518
|
-
}
|
|
519
|
-
});
|
|
520
|
-
const events = (async function* () {
|
|
521
|
-
for await (const event of producer.stream) {
|
|
522
|
-
yield event;
|
|
523
|
-
const side = event.type === "tool.event" ? event.data : void 0;
|
|
524
|
-
if (side?.kind === "computer_interaction" && side.data && typeof side.data === "object") yield side.data;
|
|
525
|
-
if (event.type === "tool_result") {
|
|
526
|
-
const warning = warnings.get(event.toolCallId);
|
|
527
|
-
if (warning) yield warning;
|
|
528
|
-
}
|
|
529
|
-
}
|
|
530
|
-
})();
|
|
531
|
-
const end = events.return.bind(events);
|
|
532
|
-
const fail = events.throw.bind(events);
|
|
533
|
-
events.return = async (value) => {
|
|
534
|
-
const closing = producer.stream.return(value);
|
|
535
|
-
try {
|
|
536
|
-
return await end(value);
|
|
537
|
-
} finally {
|
|
538
|
-
await closing;
|
|
539
|
-
}
|
|
540
|
-
};
|
|
541
|
-
events.throw = async (error) => {
|
|
542
|
-
const closing = producer.stream.throw(error);
|
|
543
|
-
try {
|
|
544
|
-
return await fail(error);
|
|
545
|
-
} finally {
|
|
546
|
-
await closing;
|
|
547
|
-
}
|
|
548
|
-
};
|
|
549
|
-
return {
|
|
550
|
-
...producer,
|
|
551
|
-
stream: events,
|
|
552
|
-
terminalObserved: () => activeTasks === 0 && !unconfirmed,
|
|
553
|
-
assistantParts: () => withWarnings(producer.assistantParts?.() ?? []),
|
|
554
|
-
draftParts: () => withWarnings(producer.draftParts?.() ?? []),
|
|
555
|
-
get model() {
|
|
556
|
-
return producer.model;
|
|
557
|
-
}
|
|
558
|
-
};
|
|
559
|
-
} catch (error) {
|
|
560
|
-
return createRouterTurnProducer({ ...router, systemPrompt: Promise.reject(error), signal: stop, onSettled: settle });
|
|
561
|
-
}
|
|
562
|
-
}
|
|
563
|
-
|
|
564
|
-
// src/chat-routes/model-failover-stream.ts
|
|
565
|
-
var TERMINAL_FAILURE_TYPES = /* @__PURE__ */ new Set(["error", "session.run.failed"]);
|
|
566
|
-
var NON_COMMITTING_PART_TYPES = /* @__PURE__ */ new Set(["step-start", "step-finish"]);
|
|
567
|
-
function isCommittingSandboxEvent(event) {
|
|
568
|
-
const record = asRecord(event);
|
|
569
|
-
if (!record) return false;
|
|
570
|
-
const type = asString(record.type) ?? "";
|
|
571
|
-
if (!type) return false;
|
|
572
|
-
if (type === "message.part.updated") {
|
|
573
|
-
const data = asRecord(record.data);
|
|
574
|
-
const part = asRecord(data?.part);
|
|
575
|
-
const partType = asString(part?.type) ?? "";
|
|
576
|
-
if (NON_COMMITTING_PART_TYPES.has(partType)) return false;
|
|
577
|
-
if (partType === "text" || partType === "reasoning") {
|
|
578
|
-
return [data?.delta, part?.text, part?.content].some((value) => typeof value === "string" && value.length > 0);
|
|
579
|
-
}
|
|
580
|
-
return true;
|
|
581
|
-
}
|
|
582
|
-
if (type === "start" || type === "execution.started" || type === "status" || type === "model-processing" || type === "model.processing" || // Public Sandbox timing metadata: a request was attempted, not answered.
|
|
583
|
-
type === "model.first_request" || type === "session.created" || type === "session.updated" || type === "session.idle" || type === "step-start" || type === "step-finish" || type === "turn" || type === "warning") {
|
|
584
|
-
return false;
|
|
585
|
-
}
|
|
586
|
-
return true;
|
|
587
|
-
}
|
|
588
|
-
function summarizeFailoverReason(reason) {
|
|
589
|
-
const collapsed = reason.replace(/\s+/g, " ").trim();
|
|
590
|
-
if (!collapsed) return "upstream unavailable";
|
|
591
|
-
if (/<!doctype html|<html[\s>]/i.test(collapsed)) {
|
|
592
|
-
const status = readHttpStatusHint(collapsed);
|
|
593
|
-
return status ? `HTTP ${status}` : "upstream returned an error page";
|
|
594
|
-
}
|
|
595
|
-
const LIMIT = 160;
|
|
596
|
-
if (collapsed.length <= LIMIT) return collapsed;
|
|
597
|
-
const clipped = collapsed.slice(0, LIMIT);
|
|
598
|
-
const lastSpace = clipped.lastIndexOf(" ");
|
|
599
|
-
return `${(lastSpace > LIMIT - 30 ? clipped.slice(0, lastSpace) : clipped).trimEnd()}\u2026`;
|
|
600
|
-
}
|
|
601
|
-
function isLiveLifecycleEvent(event) {
|
|
602
|
-
const type = asString(asRecord(event)?.type) ?? "";
|
|
603
|
-
return type === "start" || type === "execution.started" || type === "status" || type === "model-processing" || type === "model.processing" || // Public Sandbox timing metadata: a request was attempted, not answered.
|
|
604
|
-
type === "model.first_request" || type === "session.created" || type === "session.updated" || type === "session.idle";
|
|
135
|
+
// src/chat-routes/upload.ts
|
|
136
|
+
var UPLOAD_INLINE_MAX_BYTES = 700 * 1024;
|
|
137
|
+
var UPLOAD_MAX_FILE_BYTES = 8 * 1024 * 1024;
|
|
138
|
+
var DEFAULT_UPLOAD_DIRECTORY = "/workspace/uploads";
|
|
139
|
+
function normalizeUploadDirectory(value) {
|
|
140
|
+
const withoutTrailingSlash = value.trim().replace(/\/+$/, "");
|
|
141
|
+
if (!withoutTrailingSlash.startsWith("/") || withoutTrailingSlash.length === 0) return null;
|
|
142
|
+
const segments = withoutTrailingSlash.slice(1).split("/");
|
|
143
|
+
if (segments.some((segment) => segment.length === 0 || segment === "." || segment === "..")) return null;
|
|
144
|
+
return `/${segments.join("/")}`;
|
|
605
145
|
}
|
|
606
|
-
function
|
|
607
|
-
const
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
if (!TERMINAL_FAILURE_TYPES.has(type)) return null;
|
|
611
|
-
const data = asRecord(record.data);
|
|
612
|
-
const outage = isUpstreamUnavailable(data) || isUpstreamUnavailable(record);
|
|
613
|
-
const reason = asString(data?.message) ?? asString(data?.error) ?? asString(data?.reason) ?? asString(record.message) ?? `sandbox stream reported ${type}`;
|
|
614
|
-
const code = readUpstreamUnavailableCode(data) ?? asString(data?.errorCode) ?? asString(data?.code) ?? readUpstreamUnavailableCode(record) ?? asString(record.errorCode) ?? asString(record.code);
|
|
615
|
-
return { outage, reason, ...code ? { code } : {} };
|
|
146
|
+
function sanitizeUploadFilename(name) {
|
|
147
|
+
const base = name.split(/[\\/]/).pop() ?? "file";
|
|
148
|
+
const safe = base.replace(/[^A-Za-z0-9._-]+/g, "_").replace(/^\.+/, "_");
|
|
149
|
+
return (safe || "file").slice(0, 120);
|
|
616
150
|
}
|
|
617
|
-
var
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
timeoutMs;
|
|
623
|
-
constructor(input) {
|
|
624
|
-
super(input.attempts);
|
|
625
|
-
this.name = "ModelFailoverTimeoutError";
|
|
626
|
-
this.code = input.code;
|
|
627
|
-
this.model = input.model;
|
|
628
|
-
this.timeoutMs = input.timeoutMs;
|
|
629
|
-
this.message = input.code === "model_stream_open_timeout" ? `Opening the model event source for ${input.model} exceeded ${input.timeoutMs}ms.` : `Model ${input.model} produced no first answer-bearing event within ${input.timeoutMs}ms.`;
|
|
630
|
-
}
|
|
631
|
-
};
|
|
632
|
-
function resolveTimeoutMs(value, fallback, label) {
|
|
633
|
-
if (value === void 0) return fallback;
|
|
634
|
-
if (!Number.isFinite(value) || value <= 0) {
|
|
635
|
-
throw new RangeError(`${label} must be a finite number greater than 0`);
|
|
151
|
+
var BASE64_CHUNK = 32768;
|
|
152
|
+
function bytesToBase64(bytes) {
|
|
153
|
+
let binary = "";
|
|
154
|
+
for (let offset = 0; offset < bytes.length; offset += BASE64_CHUNK) {
|
|
155
|
+
binary += String.fromCharCode(...bytes.subarray(offset, offset + BASE64_CHUNK));
|
|
636
156
|
}
|
|
637
|
-
return
|
|
638
|
-
}
|
|
639
|
-
var TIMED_OUT = /* @__PURE__ */ Symbol("model-attempt-timed-out");
|
|
640
|
-
function deadlineAfter(timeoutMs) {
|
|
641
|
-
let timer;
|
|
642
|
-
const promise = new Promise((resolve) => {
|
|
643
|
-
timer = setTimeout(() => resolve(TIMED_OUT), timeoutMs);
|
|
644
|
-
});
|
|
645
|
-
return {
|
|
646
|
-
promise,
|
|
647
|
-
clear: () => {
|
|
648
|
-
if (timer !== void 0) {
|
|
649
|
-
clearTimeout(timer);
|
|
650
|
-
timer = void 0;
|
|
651
|
-
}
|
|
652
|
-
}
|
|
653
|
-
};
|
|
654
|
-
}
|
|
655
|
-
function isEmptyTerminalReceipt(event) {
|
|
656
|
-
const record = asRecord(event);
|
|
657
|
-
if (!record) return false;
|
|
658
|
-
const type = asString(record.type) ?? "";
|
|
659
|
-
if (type !== "result" && type !== "done") return false;
|
|
660
|
-
const data = asRecord(record.data);
|
|
661
|
-
const text = asString(data?.finalText) ?? asString(data?.text) ?? "";
|
|
662
|
-
return text.trim().length === 0;
|
|
663
|
-
}
|
|
664
|
-
var MAX_EMPTY_TURN_RETRIES = 3;
|
|
665
|
-
function resolveEmptyTurnRetries(value) {
|
|
666
|
-
if (typeof value !== "number" || !Number.isFinite(value)) return 0;
|
|
667
|
-
const truncated = Math.trunc(value);
|
|
668
|
-
if (truncated <= 0) return 0;
|
|
669
|
-
return Math.min(truncated, MAX_EMPTY_TURN_RETRIES);
|
|
157
|
+
return btoa(binary);
|
|
670
158
|
}
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
await iterator.return?.();
|
|
674
|
-
} catch (err) {
|
|
675
|
-
log?.("[chat-routes] abandoning a failed model stream threw", {
|
|
676
|
-
error: err instanceof Error ? err.message : String(err)
|
|
677
|
-
});
|
|
678
|
-
}
|
|
159
|
+
function uploadError(status, code, error) {
|
|
160
|
+
return Response.json({ code, error }, { status });
|
|
679
161
|
}
|
|
680
|
-
function
|
|
681
|
-
const
|
|
682
|
-
const
|
|
683
|
-
|
|
684
|
-
options.
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
const liveLifecycle = options.liveLifecycleEvents === true;
|
|
694
|
-
const progress = createProgressQueue();
|
|
695
|
-
let serving;
|
|
696
|
-
let trail = [];
|
|
697
|
-
let fellBack = false;
|
|
698
|
-
const attemptState = { index: 0 };
|
|
699
|
-
let finalTimeout;
|
|
700
|
-
const { drainOnce } = createModelAttemptDrainer(attemptState, options, openTimeoutMs, firstResponseTimeoutMs, committing, liveLifecycle, progress);
|
|
701
|
-
const probe = async (model) => {
|
|
702
|
-
finalTimeout = void 0;
|
|
703
|
-
for (let retry = 0; ; retry += 1) {
|
|
704
|
-
const pass = await drainOnce(model);
|
|
705
|
-
if (!pass.empty || retry >= emptyTurnRetries) {
|
|
706
|
-
if (!pass.outcome.committed && pass.outcome.timeoutCode) {
|
|
707
|
-
finalTimeout = {
|
|
708
|
-
code: pass.outcome.timeoutCode,
|
|
709
|
-
model,
|
|
710
|
-
timeoutMs: pass.outcome.timeoutCode === "model_stream_open_timeout" ? openTimeoutMs : firstResponseTimeoutMs
|
|
711
|
-
};
|
|
712
|
-
} else {
|
|
713
|
-
finalTimeout = void 0;
|
|
714
|
-
}
|
|
715
|
-
return pass.outcome;
|
|
716
|
-
}
|
|
717
|
-
const iterator = pass.outcome.committed ? pass.outcome.iterator : null;
|
|
718
|
-
if (pass.outcome.committed) pass.outcome.abort();
|
|
719
|
-
if (iterator) void closeIterator(iterator, options.log);
|
|
720
|
-
const info = { model, retry: retry + 1, remaining: emptyTurnRetries - retry - 1 };
|
|
721
|
-
options.log?.("[chat-routes] turn completed with no assistant text; re-running the same model", {
|
|
722
|
-
...info
|
|
723
|
-
});
|
|
724
|
-
options.onEmptyTurnRetry?.(info);
|
|
162
|
+
function createUploadRoute(options) {
|
|
163
|
+
const inlineMaxBytes = options.inlineMaxBytes ?? UPLOAD_INLINE_MAX_BYTES;
|
|
164
|
+
const maxFileBytes = options.maxFileBytes ?? UPLOAD_MAX_FILE_BYTES;
|
|
165
|
+
return async function upload(request) {
|
|
166
|
+
const auth = await options.authorize({ request });
|
|
167
|
+
if (!auth.ok) return auth.response;
|
|
168
|
+
const sink = auth.sink ?? null;
|
|
169
|
+
const configuredUploadDir = auth.uploadDir ?? options.uploadDir ?? DEFAULT_UPLOAD_DIRECTORY;
|
|
170
|
+
let form;
|
|
171
|
+
try {
|
|
172
|
+
form = await request.formData();
|
|
173
|
+
} catch {
|
|
174
|
+
return uploadError(400, "INVALID_UPLOAD", "Expected a multipart/form-data body with file fields");
|
|
725
175
|
}
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
...options.policy ? { policy: options.policy } : {},
|
|
733
|
-
// The probe has already classified the raw payload with
|
|
734
|
-
// `isUpstreamUnavailable`; this reads its verdict rather than
|
|
735
|
-
// re-classifying a wrapper object, so the two can never disagree.
|
|
736
|
-
isUnavailableResult: (result) => result.committed === false,
|
|
737
|
-
onFallback: (attempt, nextModel) => {
|
|
738
|
-
const info = {
|
|
739
|
-
from: attempt.model,
|
|
740
|
-
to: nextModel,
|
|
741
|
-
reason: attempt.reason ?? "upstream unavailable"
|
|
742
|
-
};
|
|
743
|
-
options.log?.("[chat-routes] model upstream unavailable; falling over", { ...info });
|
|
744
|
-
options.onFallback?.(info);
|
|
745
|
-
}
|
|
746
|
-
}).finally(() => progress.close());
|
|
747
|
-
settled.catch(() => void 0);
|
|
748
|
-
for (; ; ) {
|
|
749
|
-
const item = await progress.pull();
|
|
750
|
-
if (item === PROGRESS_CLOSED) break;
|
|
751
|
-
yield item;
|
|
176
|
+
const files = [];
|
|
177
|
+
form.forEach((value) => {
|
|
178
|
+
if (value instanceof File) files.push(value);
|
|
179
|
+
});
|
|
180
|
+
if (files.length === 0) {
|
|
181
|
+
return uploadError(400, "INVALID_UPLOAD", "No files in the upload body");
|
|
752
182
|
}
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
183
|
+
const uploaded = [];
|
|
184
|
+
for (const file of files) {
|
|
185
|
+
const name = sanitizeUploadFilename(file.name);
|
|
186
|
+
const mediaType = file.type || "application/octet-stream";
|
|
187
|
+
const partType = mediaType.startsWith("image/") ? "image" : "file";
|
|
188
|
+
if (file.size > maxFileBytes) {
|
|
189
|
+
return uploadError(
|
|
190
|
+
413,
|
|
191
|
+
"FILE_TOO_LARGE",
|
|
192
|
+
`${name} is ${file.size}B, over the ${maxFileBytes}B per-file cap`
|
|
193
|
+
);
|
|
194
|
+
}
|
|
195
|
+
const id = crypto.randomUUID();
|
|
196
|
+
if (file.size <= inlineMaxBytes) {
|
|
197
|
+
const base642 = bytesToBase64(new Uint8Array(await file.arrayBuffer()));
|
|
198
|
+
uploaded.push({
|
|
199
|
+
id,
|
|
200
|
+
name,
|
|
201
|
+
size: file.size,
|
|
202
|
+
mediaType,
|
|
203
|
+
inline: true,
|
|
204
|
+
part: {
|
|
205
|
+
type: partType,
|
|
206
|
+
filename: name,
|
|
207
|
+
mediaType,
|
|
208
|
+
url: `data:${mediaType};base64,${base642}`
|
|
209
|
+
}
|
|
766
210
|
});
|
|
211
|
+
continue;
|
|
767
212
|
}
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
return;
|
|
775
|
-
}
|
|
776
|
-
try {
|
|
777
|
-
for (; ; ) {
|
|
778
|
-
const next = await live.next();
|
|
779
|
-
if (next.done) return;
|
|
780
|
-
yield next.value;
|
|
213
|
+
if (!sink) {
|
|
214
|
+
return uploadError(
|
|
215
|
+
413,
|
|
216
|
+
"SANDBOX_REQUIRED",
|
|
217
|
+
`${name} is ${file.size}B, over the ${inlineMaxBytes}B inline cap, and no sandbox is available to hold it`
|
|
218
|
+
);
|
|
781
219
|
}
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
220
|
+
const uploadDir = normalizeUploadDirectory(configuredUploadDir);
|
|
221
|
+
if (!uploadDir) {
|
|
222
|
+
return uploadError(
|
|
223
|
+
500,
|
|
224
|
+
"INVALID_UPLOAD_DIRECTORY",
|
|
225
|
+
'uploadDir must be an absolute path without empty, "." or ".." segments'
|
|
226
|
+
);
|
|
227
|
+
}
|
|
228
|
+
const path = `${uploadDir}/${id}-${name}`;
|
|
229
|
+
const base64 = bytesToBase64(new Uint8Array(await file.arrayBuffer()));
|
|
230
|
+
await sink.write(path, base64, { encoding: "base64" });
|
|
231
|
+
uploaded.push({
|
|
232
|
+
id,
|
|
233
|
+
name,
|
|
234
|
+
size: file.size,
|
|
235
|
+
mediaType,
|
|
236
|
+
inline: false,
|
|
237
|
+
part: { type: partType, filename: name, mediaType, path }
|
|
238
|
+
});
|
|
785
239
|
}
|
|
786
|
-
|
|
787
|
-
return {
|
|
788
|
-
events,
|
|
789
|
-
servingModel: () => serving,
|
|
790
|
-
attempts: () => trail,
|
|
791
|
-
usedFallback: () => fellBack
|
|
240
|
+
return Response.json({ files: uploaded });
|
|
792
241
|
};
|
|
793
242
|
}
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
const deliver = (value) => {
|
|
800
|
-
const waiter = waiters.shift();
|
|
801
|
-
if (waiter) waiter(value);
|
|
802
|
-
else items.push(value);
|
|
803
|
-
};
|
|
804
|
-
return {
|
|
805
|
-
push(event) {
|
|
806
|
-
if (closed) return;
|
|
807
|
-
deliver(event);
|
|
808
|
-
},
|
|
809
|
-
close() {
|
|
810
|
-
if (closed) return;
|
|
811
|
-
closed = true;
|
|
812
|
-
deliver(PROGRESS_CLOSED);
|
|
813
|
-
},
|
|
814
|
-
pull() {
|
|
815
|
-
if (items.length > 0) return Promise.resolve(items.shift());
|
|
816
|
-
if (closed) return Promise.resolve(PROGRESS_CLOSED);
|
|
817
|
-
return new Promise((resolve) => waiters.push(resolve));
|
|
818
|
-
}
|
|
819
|
-
};
|
|
243
|
+
|
|
244
|
+
// src/chat-routes/dispatch-parts.ts
|
|
245
|
+
import { pathToFileURL } from "url";
|
|
246
|
+
function byteLen(value) {
|
|
247
|
+
return new TextEncoder().encode(value).length;
|
|
820
248
|
}
|
|
821
|
-
function
|
|
822
|
-
const
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
249
|
+
async function readSandboxMention(box, absolutePath, options) {
|
|
250
|
+
const stat = await statSandboxFileSize(box, absolutePath);
|
|
251
|
+
if (!stat.succeeded) {
|
|
252
|
+
return { succeeded: false, error: `mentioned sandbox file missing or unreadable: ${absolutePath} \u2014 ${stat.error}` };
|
|
253
|
+
}
|
|
254
|
+
if (!options.readBytes) return { succeeded: true, value: { size: stat.value } };
|
|
255
|
+
const read = await readSandboxBinaryBytes(box, absolutePath, stat.value);
|
|
256
|
+
if (!read.succeeded) {
|
|
257
|
+
return { succeeded: false, error: `mentioned sandbox file read failed: ${absolutePath} \u2014 ${read.error}` };
|
|
258
|
+
}
|
|
259
|
+
return { succeeded: true, value: { size: stat.value, base64: bytesToBase64(read.value.bytes) } };
|
|
260
|
+
}
|
|
261
|
+
function violatesUrlPathXor(part) {
|
|
262
|
+
if (part.type === "text") return false;
|
|
263
|
+
const hasUrl = typeof part.url === "string" && (part.url.startsWith("data:") || part.url.startsWith("file://"));
|
|
264
|
+
const hasPath = "path" in part && typeof part.path === "string" && part.path.startsWith("/");
|
|
265
|
+
if (part.type === "file") return !hasUrl || hasPath;
|
|
266
|
+
return hasUrl === hasPath;
|
|
267
|
+
}
|
|
268
|
+
function sandboxFileUrl(absolutePath) {
|
|
269
|
+
if (!absolutePath.startsWith("/")) return void 0;
|
|
270
|
+
try {
|
|
271
|
+
return pathToFileURL(absolutePath).href;
|
|
272
|
+
} catch {
|
|
273
|
+
return void 0;
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
function normalizeChatPromptForSandbox(prompt) {
|
|
277
|
+
if (typeof prompt === "string") return prompt;
|
|
278
|
+
return prompt.map((part) => {
|
|
279
|
+
if (part.type === "text") return part;
|
|
280
|
+
if ("content" in part) {
|
|
281
|
+
throw new ChatTurnInputError("Sandbox prompt parts do not accept inline content; provide a URL or path");
|
|
841
282
|
}
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
error: err instanceof Error ? err.message : String(err)
|
|
852
|
-
});
|
|
853
|
-
}
|
|
854
|
-
},
|
|
855
|
-
(err) => {
|
|
856
|
-
options.log?.("[chat-routes] abandoned model stream open rejected after timeout", {
|
|
857
|
-
error: err instanceof Error ? err.message : String(err)
|
|
858
|
-
});
|
|
859
|
-
}
|
|
860
|
-
);
|
|
283
|
+
const url = typeof part.url === "string" && part.url.length > 0 ? part.url : void 0;
|
|
284
|
+
const path = typeof part.path === "string" && part.path.length > 0 ? part.path : void 0;
|
|
285
|
+
if (Boolean(url) === Boolean(path)) {
|
|
286
|
+
throw new ChatTurnInputError(`Sandbox ${part.type} parts require exactly one URL or path`);
|
|
287
|
+
}
|
|
288
|
+
if (path && !path.startsWith("/")) {
|
|
289
|
+
throw new ChatTurnInputError(`Sandbox ${part.type} paths must be absolute: ${path}`);
|
|
290
|
+
}
|
|
291
|
+
if (part.type === "image") {
|
|
861
292
|
return {
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
timeoutCode: "model_stream_open_timeout"
|
|
867
|
-
},
|
|
868
|
-
empty: false
|
|
293
|
+
type: "image",
|
|
294
|
+
...part.filename ? { filename: part.filename } : {},
|
|
295
|
+
...part.mediaType ? { mediaType: part.mediaType } : {},
|
|
296
|
+
...url ? { url } : { path }
|
|
869
297
|
};
|
|
870
298
|
}
|
|
871
|
-
const
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
299
|
+
const filename = part.filename?.trim();
|
|
300
|
+
if (!filename) {
|
|
301
|
+
throw new ChatTurnInputError("Sandbox file parts require a filename");
|
|
302
|
+
}
|
|
303
|
+
const fileUrl = url ?? sandboxFileUrl(path);
|
|
304
|
+
if (!fileUrl) {
|
|
305
|
+
throw new ChatTurnInputError(`Sandbox file paths must be absolute: ${path}`);
|
|
306
|
+
}
|
|
307
|
+
return {
|
|
308
|
+
type: "file",
|
|
309
|
+
filename,
|
|
310
|
+
...part.mediaType ? { mediaType: part.mediaType } : {},
|
|
311
|
+
url: fileUrl
|
|
312
|
+
};
|
|
313
|
+
});
|
|
314
|
+
}
|
|
315
|
+
function projectDispatchHistory(messages, view = {}) {
|
|
316
|
+
const out = [];
|
|
317
|
+
const textOf = (message) => view.attachments === false ? message.content : historyContentWithAttachments(message);
|
|
318
|
+
for (const message of messages) {
|
|
319
|
+
if (message.role !== "assistant") {
|
|
320
|
+
out.push({ role: "user", content: textOf(message) });
|
|
321
|
+
continue;
|
|
879
322
|
}
|
|
880
|
-
const
|
|
881
|
-
|
|
323
|
+
const slices = speakerSlices(message);
|
|
324
|
+
if (slices.length === 0) {
|
|
325
|
+
const owner = view.owner;
|
|
326
|
+
const foreign = view.pov !== void 0 && owner !== void 0 && owner.id !== view.pov;
|
|
327
|
+
out.push({
|
|
328
|
+
role: foreign ? "user" : "assistant",
|
|
329
|
+
content: foreign ? tagged(owner.name, textOf(message)) : textOf(message)
|
|
330
|
+
});
|
|
331
|
+
continue;
|
|
332
|
+
}
|
|
333
|
+
for (const slice of slices) {
|
|
334
|
+
const text = textOf({ role: "assistant", content: slice.text, parts: slice.parts });
|
|
335
|
+
if (view.pov === void 0) out.push({ role: "assistant", content: tagged(slice.name, text) });
|
|
336
|
+
else if (slice.id === view.pov) out.push({ role: "assistant", content: text });
|
|
337
|
+
else out.push({ role: "user", content: tagged(slice.name, text) });
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
return out;
|
|
341
|
+
}
|
|
342
|
+
var tagged = (name, text) => `[${name}] ${text}`;
|
|
343
|
+
function speakerSlices(message) {
|
|
344
|
+
const parts = message.parts ?? [];
|
|
345
|
+
const slices = [];
|
|
346
|
+
let current = null;
|
|
347
|
+
for (const part of parts) {
|
|
348
|
+
if (part.type === "speaker" && typeof part.id === "string" && typeof part.name === "string") {
|
|
349
|
+
current = { id: part.id, name: part.name, text: "", parts: [] };
|
|
350
|
+
slices.push(current);
|
|
351
|
+
} else if (current) {
|
|
352
|
+
current.parts.push(part);
|
|
353
|
+
if (part.type === "text" && typeof part.text === "string") current.text += part.text;
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
return slices;
|
|
357
|
+
}
|
|
358
|
+
function readResultToBase64(read) {
|
|
359
|
+
if (typeof read.base64 === "string") return read.base64;
|
|
360
|
+
if (read.bytes) return bytesToBase64(read.bytes);
|
|
361
|
+
return void 0;
|
|
362
|
+
}
|
|
363
|
+
async function buildDispatchParts(input) {
|
|
364
|
+
const readMention = input.readSandboxMention ?? readSandboxMention;
|
|
365
|
+
const resolveMentionPath = input.resolveMentionPath ?? input.resolveAttachmentPath;
|
|
366
|
+
const forcePath = input.forcePath ?? false;
|
|
367
|
+
const mentions = (input.mentions ?? []).filter((mention) => mention.mentionKind !== "agent");
|
|
368
|
+
const requestMaxBytes = input.requestMaxBytes ?? DISPATCH_REQUEST_MAX_BYTES;
|
|
369
|
+
const structuralReserveBytes = input.structuralReserveBytes ?? DISPATCH_STRUCTURAL_RESERVE_BYTES;
|
|
370
|
+
const maxParts = input.maxParts ?? DISPATCH_MAX_PARTS;
|
|
371
|
+
const parts = [{ type: "text", text: input.text }];
|
|
372
|
+
const emittedAbsPaths = /* @__PURE__ */ new Set();
|
|
373
|
+
const flattenedForSizing = flattenHistory(input.text, input.history);
|
|
374
|
+
const inlineBudget = requestMaxBytes - base64WireLen(byteLen(flattenedForSizing)) - byteLen(JSON.stringify(input.systemPrompt)) - input.profileWireBytes - structuralReserveBytes;
|
|
375
|
+
let runningInline = 0;
|
|
376
|
+
for (const attachment of input.attachments) {
|
|
377
|
+
if (!attachment.path) {
|
|
378
|
+
return { succeeded: false, error: `attachment path must be non-empty: ${attachment.name}` };
|
|
379
|
+
}
|
|
380
|
+
let read;
|
|
882
381
|
try {
|
|
883
|
-
|
|
884
|
-
Promise.resolve(iterator.next()),
|
|
885
|
-
openDeadline.promise
|
|
886
|
-
]);
|
|
382
|
+
read = await input.readAttachment(input.scopeId, attachment.path);
|
|
887
383
|
} catch (err) {
|
|
888
|
-
openDeadline.clear();
|
|
889
|
-
abort();
|
|
890
|
-
void closeIterator(iterator, options.log);
|
|
891
|
-
throw err;
|
|
892
|
-
}
|
|
893
|
-
openDeadline.clear();
|
|
894
|
-
if (next === TIMED_OUT) {
|
|
895
|
-
abort();
|
|
896
|
-
void closeIterator(iterator, options.log);
|
|
897
384
|
return {
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
error: `Starting the model event source for ${model} exceeded ${openTimeoutMs}ms`,
|
|
901
|
-
errorCode: "model_stream_open_timeout",
|
|
902
|
-
timeoutCode: "model_stream_open_timeout"
|
|
903
|
-
},
|
|
904
|
-
empty: false
|
|
385
|
+
succeeded: false,
|
|
386
|
+
error: `attachment store read failed: ${attachment.path} \u2014 ${err instanceof Error ? err.message : String(err)}`
|
|
905
387
|
};
|
|
906
388
|
}
|
|
907
|
-
if (
|
|
908
|
-
|
|
909
|
-
|
|
389
|
+
if (!read.ok) return { succeeded: false, error: read.reason };
|
|
390
|
+
const base64 = readResultToBase64(read);
|
|
391
|
+
if (base64 === void 0) {
|
|
392
|
+
return { succeeded: false, error: `attachment store read produced no content: ${attachment.path}` };
|
|
910
393
|
}
|
|
911
|
-
const
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
if (
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
void closeIterator(iterator, options.log);
|
|
929
|
-
return {
|
|
930
|
-
outcome: {
|
|
931
|
-
committed: false,
|
|
932
|
-
error: failure.reason,
|
|
933
|
-
...failure.code ? { errorCode: failure.code } : {}
|
|
934
|
-
},
|
|
935
|
-
empty: false
|
|
936
|
-
};
|
|
937
|
-
}
|
|
938
|
-
if (liveLifecycle && !failure && !commits && isLiveLifecycleEvent(event)) {
|
|
939
|
-
progress.push(event);
|
|
394
|
+
const mediaType = attachment.mediaType ?? read.mediaType;
|
|
395
|
+
if (attachment.type === "image" && !mediaType) {
|
|
396
|
+
return { succeeded: false, error: `attachment is missing a mediaType required for an image data URI: ${attachment.path}` };
|
|
397
|
+
}
|
|
398
|
+
const absPath = input.resolveAttachmentPath(attachment.path);
|
|
399
|
+
emittedAbsPaths.add(absPath);
|
|
400
|
+
if (attachment.type === "image") {
|
|
401
|
+
const inlinePart2 = {
|
|
402
|
+
type: "image",
|
|
403
|
+
filename: attachment.name,
|
|
404
|
+
mediaType,
|
|
405
|
+
url: `data:${mediaType};base64,${base64}`
|
|
406
|
+
};
|
|
407
|
+
const cost2 = byteLen(JSON.stringify(inlinePart2));
|
|
408
|
+
if (!forcePath && runningInline + cost2 <= inlineBudget) {
|
|
409
|
+
parts.push(inlinePart2);
|
|
410
|
+
runningInline += cost2;
|
|
940
411
|
} else {
|
|
941
|
-
|
|
942
|
-
}
|
|
943
|
-
if (failure) {
|
|
944
|
-
firstResponseDeadline.clear();
|
|
945
|
-
return { outcome: { committed: true, buffered, iterator, abort }, empty: false };
|
|
412
|
+
parts.push({ type: "image", filename: attachment.name, mediaType, path: absPath });
|
|
946
413
|
}
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
414
|
+
continue;
|
|
415
|
+
}
|
|
416
|
+
const fileMediaType = mediaType ?? "application/octet-stream";
|
|
417
|
+
const inlinePart = {
|
|
418
|
+
type: "file",
|
|
419
|
+
filename: attachment.name,
|
|
420
|
+
mediaType: fileMediaType,
|
|
421
|
+
url: `data:${fileMediaType};base64,${base64}`
|
|
422
|
+
};
|
|
423
|
+
const cost = byteLen(JSON.stringify(inlinePart));
|
|
424
|
+
if (!forcePath && runningInline + cost <= inlineBudget) {
|
|
425
|
+
parts.push(inlinePart);
|
|
426
|
+
runningInline += cost;
|
|
427
|
+
} else {
|
|
428
|
+
const url = sandboxFileUrl(absPath);
|
|
429
|
+
if (!url) {
|
|
430
|
+
return { succeeded: false, error: `resolved attachment path must be absolute: ${absPath}` };
|
|
950
431
|
}
|
|
432
|
+
parts.push({ type: "file", filename: attachment.name, mediaType: fileMediaType, url });
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
if (mentions.length > 0 && !input.box) {
|
|
436
|
+
return { succeeded: false, error: "internal error: sandbox mentions require a box to read from" };
|
|
437
|
+
}
|
|
438
|
+
for (const mention of mentions) {
|
|
439
|
+
if (!mention.path) {
|
|
440
|
+
return { succeeded: false, error: `mention path must be non-empty: ${mention.name}` };
|
|
441
|
+
}
|
|
442
|
+
if (!isWorkspaceFileExportable(mention.path)) {
|
|
443
|
+
return { succeeded: false, error: `mention file is not exportable: ${mention.path}` };
|
|
444
|
+
}
|
|
445
|
+
const absPath = resolveMentionPath(mention.path);
|
|
446
|
+
if (emittedAbsPaths.has(absPath)) continue;
|
|
447
|
+
emittedAbsPaths.add(absPath);
|
|
448
|
+
const isImage = mention.mentionKind === "image";
|
|
449
|
+
const mediaType = isImage ? mediaTypeForMentionPath(mention.path) : void 0;
|
|
450
|
+
let stat;
|
|
451
|
+
try {
|
|
452
|
+
stat = await readMention(input.box, absPath, { readBytes: false });
|
|
453
|
+
} catch (err) {
|
|
454
|
+
return { succeeded: false, error: `mention read failed: ${absPath} \u2014 ${err instanceof Error ? err.message : String(err)}` };
|
|
455
|
+
}
|
|
456
|
+
if (!stat.succeeded) return { succeeded: false, error: stat.error };
|
|
457
|
+
const projectedInlineCost = base64WireLen(stat.value.size) + byteLen(JSON.stringify({ type: "image", filename: mention.name, mediaType: mediaType ?? "", url: "" }));
|
|
458
|
+
if (isImage && mediaType && !forcePath && runningInline + projectedInlineCost <= inlineBudget) {
|
|
459
|
+
let read;
|
|
951
460
|
try {
|
|
952
|
-
|
|
953
|
-
Promise.resolve(iterator.next()),
|
|
954
|
-
firstResponseDeadline.promise
|
|
955
|
-
]);
|
|
461
|
+
read = await readMention(input.box, absPath, { readBytes: true });
|
|
956
462
|
} catch (err) {
|
|
957
|
-
|
|
958
|
-
abort();
|
|
959
|
-
void closeIterator(iterator, options.log);
|
|
960
|
-
throw err;
|
|
463
|
+
return { succeeded: false, error: `mention read failed: ${absPath} \u2014 ${err instanceof Error ? err.message : String(err)}` };
|
|
961
464
|
}
|
|
962
|
-
if (
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
465
|
+
if (!read.succeeded) return { succeeded: false, error: read.error };
|
|
466
|
+
if (!read.value.base64) return { succeeded: false, error: `mentioned image produced no bytes: ${absPath}` };
|
|
467
|
+
const inlinePart = {
|
|
468
|
+
type: "image",
|
|
469
|
+
filename: mention.name,
|
|
470
|
+
mediaType,
|
|
471
|
+
url: `data:${mediaType};base64,${read.value.base64}`
|
|
472
|
+
};
|
|
473
|
+
const cost = byteLen(JSON.stringify(inlinePart));
|
|
474
|
+
if (runningInline + cost <= inlineBudget) {
|
|
475
|
+
parts.push(inlinePart);
|
|
476
|
+
runningInline += cost;
|
|
477
|
+
continue;
|
|
975
478
|
}
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
479
|
+
}
|
|
480
|
+
if (isImage && mediaType) {
|
|
481
|
+
parts.push({ type: "image", filename: mention.name, mediaType, path: absPath });
|
|
482
|
+
} else {
|
|
483
|
+
const url = sandboxFileUrl(absPath);
|
|
484
|
+
if (!url) {
|
|
485
|
+
return { succeeded: false, error: `resolved mention path must be absolute: ${absPath}` };
|
|
980
486
|
}
|
|
487
|
+
parts.push({ type: "file", filename: mention.name, url });
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
for (const part of parts) {
|
|
491
|
+
if (violatesUrlPathXor(part)) {
|
|
492
|
+
return { succeeded: false, error: "internal error: emitted media part violates the url/path exclusivity invariant" };
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
const textPartSize = base64WireLen(byteLen(flattenedForSizing));
|
|
496
|
+
const mediaPartsSize = parts.slice(1).reduce((total, part) => total + byteLen(JSON.stringify(part)), 0);
|
|
497
|
+
const systemPromptSize = byteLen(JSON.stringify(input.systemPrompt));
|
|
498
|
+
if (textPartSize + mediaPartsSize + systemPromptSize + input.profileWireBytes + structuralReserveBytes > requestMaxBytes) {
|
|
499
|
+
return { succeeded: false, error: "dispatch parts exceed the sandbox proxy request cap even after path demotion" };
|
|
500
|
+
}
|
|
501
|
+
if (parts.length > maxParts) {
|
|
502
|
+
return { succeeded: false, error: `dispatch parts exceed the sidecar per-request cap of ${maxParts}` };
|
|
503
|
+
}
|
|
504
|
+
return { succeeded: true, value: parts };
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
// src/chat-routes/reasoning-clock.ts
|
|
508
|
+
function createReasoningClock(now = Date.now) {
|
|
509
|
+
const runs = /* @__PURE__ */ new Map();
|
|
510
|
+
let open = null;
|
|
511
|
+
const settle = (at) => {
|
|
512
|
+
if (!open) return;
|
|
513
|
+
const time = runs.get(open.run);
|
|
514
|
+
if (time && time.end === void 0) time.end = Math.max(time.start ?? 0, at ?? now());
|
|
515
|
+
open = null;
|
|
516
|
+
};
|
|
517
|
+
return {
|
|
518
|
+
reasoning(run) {
|
|
519
|
+
if (open && open.run === run) return;
|
|
520
|
+
settle();
|
|
521
|
+
const time = runs.get(run);
|
|
522
|
+
if (time) delete time.end;
|
|
523
|
+
else runs.set(run, { start: now() });
|
|
524
|
+
open = { run };
|
|
525
|
+
},
|
|
526
|
+
settle,
|
|
527
|
+
time(run, own) {
|
|
528
|
+
const time = runs.get(run);
|
|
529
|
+
if (!time) return own;
|
|
530
|
+
const start = own?.start ?? time.start;
|
|
531
|
+
const end = own?.end ?? time.end;
|
|
532
|
+
return { ...start !== void 0 ? { start } : {}, ...end !== void 0 ? { end } : {} };
|
|
981
533
|
}
|
|
982
534
|
};
|
|
983
|
-
return { drainOnce };
|
|
984
535
|
}
|
|
985
536
|
|
|
986
|
-
// src/chat-routes/
|
|
987
|
-
function
|
|
988
|
-
const
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
const
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
537
|
+
// src/chat-routes/router-producer.ts
|
|
538
|
+
function createRouterTurnProducer(options) {
|
|
539
|
+
const abort = new AbortController();
|
|
540
|
+
const signal = options.signal ? AbortSignal.any([abort.signal, options.signal]) : abort.signal;
|
|
541
|
+
void options.admitted?.catch((error) => {
|
|
542
|
+
abort.abort(error);
|
|
543
|
+
});
|
|
544
|
+
const credential = Promise.resolve(options.apiKey);
|
|
545
|
+
let credentialSettled = false;
|
|
546
|
+
void credential.then(() => {
|
|
547
|
+
credentialSettled = true;
|
|
548
|
+
}, () => {
|
|
549
|
+
credentialSettled = true;
|
|
550
|
+
});
|
|
551
|
+
let retired;
|
|
552
|
+
const retire = () => retired ??= credential.then(async (key) => {
|
|
553
|
+
if (typeof key !== "string") await key.revoke();
|
|
554
|
+
}, () => void 0).catch((error) => {
|
|
555
|
+
console.error(JSON.stringify({ event: "router_turn_key_retire_failed", error: redactErrorMessage(error) }));
|
|
556
|
+
});
|
|
557
|
+
let stop = () => {
|
|
999
558
|
};
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
);
|
|
1006
|
-
}
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
const
|
|
1016
|
-
|
|
1017
|
-
let
|
|
1018
|
-
let
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
559
|
+
const cancelled = new Promise((_resolve, reject) => {
|
|
560
|
+
stop = reject;
|
|
561
|
+
});
|
|
562
|
+
const stopped = () => {
|
|
563
|
+
stop(signal.reason);
|
|
564
|
+
void retire();
|
|
565
|
+
};
|
|
566
|
+
signal.addEventListener("abort", stopped, { once: true });
|
|
567
|
+
if (signal.aborted) stopped();
|
|
568
|
+
const queue = [];
|
|
569
|
+
let wake = null;
|
|
570
|
+
let done = false;
|
|
571
|
+
let outcome = null;
|
|
572
|
+
let usageReported = false;
|
|
573
|
+
const parts = [];
|
|
574
|
+
const toolParts = /* @__PURE__ */ new Map();
|
|
575
|
+
let textPart = null;
|
|
576
|
+
let textSegment = 0;
|
|
577
|
+
let reasoningPart = null;
|
|
578
|
+
const reasoningClock = createReasoningClock();
|
|
579
|
+
const push = (event) => {
|
|
580
|
+
queue.push(event);
|
|
581
|
+
wake?.();
|
|
582
|
+
};
|
|
583
|
+
const run = Promise.race([Promise.all([credential, options.systemPrompt]), cancelled]).then(([key, prompt]) => {
|
|
584
|
+
signal.throwIfAborted();
|
|
585
|
+
return runRouterTurn({
|
|
586
|
+
...options,
|
|
587
|
+
apiKey: typeof key === "string" ? key : key.key,
|
|
588
|
+
fetchImpl: typeof key === "string" ? options.fetchImpl : key.fetchImpl ?? options.fetchImpl,
|
|
589
|
+
signal,
|
|
590
|
+
tools: options.tools?.map((tool) => ({ ...tool, run: async (...args) => {
|
|
591
|
+
await options.admitted;
|
|
592
|
+
signal.throwIfAborted();
|
|
593
|
+
return tool.run(...args);
|
|
594
|
+
} })),
|
|
595
|
+
systemPrompt: typeof prompt === "string" ? prompt : composeRouterTurnPromptSections(prompt.profile, prompt).text,
|
|
596
|
+
showReasoning: options.showReasoning ?? true,
|
|
597
|
+
onEvent: (event) => {
|
|
598
|
+
if (event.type === "text") {
|
|
599
|
+
if (event.segment !== textSegment || !textPart) {
|
|
600
|
+
textSegment = event.segment;
|
|
601
|
+
textPart = { type: "text", text: "" };
|
|
602
|
+
parts.push(textPart);
|
|
603
|
+
}
|
|
604
|
+
textPart.text += event.delta;
|
|
605
|
+
reasoningPart = null;
|
|
606
|
+
reasoningClock.settle();
|
|
607
|
+
push({ type: "text", text: event.delta });
|
|
608
|
+
} else if (event.type === "reasoning") {
|
|
609
|
+
if (!reasoningPart) {
|
|
610
|
+
reasoningPart = { type: "reasoning", text: "" };
|
|
611
|
+
parts.push(reasoningPart);
|
|
612
|
+
}
|
|
613
|
+
reasoningPart.text += event.delta;
|
|
614
|
+
reasoningClock.reasoning(reasoningPart);
|
|
615
|
+
push({ type: "reasoning", text: event.delta });
|
|
616
|
+
} else if (event.type === "tool") {
|
|
617
|
+
reasoningPart = null;
|
|
618
|
+
reasoningClock.settle(event.startedAt);
|
|
619
|
+
if (event.status === "running") {
|
|
620
|
+
const part = {
|
|
621
|
+
type: "tool",
|
|
622
|
+
id: event.callId,
|
|
623
|
+
callID: event.callId,
|
|
624
|
+
tool: event.toolName,
|
|
625
|
+
state: { status: "running", input: event.input, time: { start: event.startedAt } }
|
|
626
|
+
};
|
|
627
|
+
toolParts.set(event.callId, part);
|
|
628
|
+
parts.push(part);
|
|
629
|
+
push({ type: "tool_call", call: { toolCallId: event.callId, toolName: event.toolName, args: event.input } });
|
|
630
|
+
} else {
|
|
631
|
+
const part = toolParts.get(event.callId);
|
|
632
|
+
const state = event.status === "completed" ? { status: "completed", output: event.output, time: { start: event.startedAt, end: event.endedAt } } : { status: "error", error: event.error.message, time: { start: event.startedAt, end: event.endedAt } };
|
|
633
|
+
if (part) part.state = { ...part.state, ...state };
|
|
634
|
+
push({
|
|
635
|
+
type: "tool_result",
|
|
636
|
+
toolCallId: event.callId,
|
|
637
|
+
toolName: event.toolName,
|
|
638
|
+
outcome: event.status === "completed" ? { ok: true, result: event.output } : { ok: false, message: event.error.message, code: event.error.code }
|
|
639
|
+
});
|
|
640
|
+
}
|
|
641
|
+
} else if (event.type === "repeat") {
|
|
642
|
+
reasoningPart = null;
|
|
643
|
+
reasoningClock.settle();
|
|
644
|
+
} else if (event.type === "usage") {
|
|
645
|
+
usageReported = true;
|
|
646
|
+
push({ type: "usage", usage: {
|
|
647
|
+
promptTokens: event.promptTokens,
|
|
648
|
+
completionTokens: event.completionTokens,
|
|
649
|
+
...event.cachedPromptTokens !== void 0 ? { cachedPromptTokens: event.cachedPromptTokens } : {}
|
|
650
|
+
} });
|
|
651
|
+
} else if (event.type === "tool_event") {
|
|
652
|
+
const part = toolParts.get(event.callId);
|
|
653
|
+
if (part) {
|
|
654
|
+
const state = part.state;
|
|
655
|
+
part.state = { ...state, events: [...state.events ?? [], { kind: event.kind, data: event.data, at: event.at }] };
|
|
656
|
+
}
|
|
657
|
+
push({ type: "tool.event", data: { toolCallId: event.callId, toolName: event.toolName, kind: event.kind, data: event.data, at: event.at } });
|
|
658
|
+
}
|
|
659
|
+
if (options.onEvent) {
|
|
660
|
+
try {
|
|
661
|
+
options.onEvent(event);
|
|
662
|
+
} catch {
|
|
663
|
+
}
|
|
664
|
+
}
|
|
1049
665
|
}
|
|
1050
666
|
});
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
if (
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
667
|
+
}).then(async (result) => {
|
|
668
|
+
if (result.continuation && !result.failure) {
|
|
669
|
+
result.failure = { code: "turn_deadline", message: "The reply was stopped before it finished." };
|
|
670
|
+
}
|
|
671
|
+
outcome = result;
|
|
672
|
+
const final = await options.onOutcome?.(result);
|
|
673
|
+
if (final && final.trailingText) {
|
|
674
|
+
result.text += final.trailingText;
|
|
675
|
+
parts.push({ type: "text", text: final.trailingText });
|
|
676
|
+
push({ type: "text", text: final.trailingText });
|
|
677
|
+
}
|
|
678
|
+
if (final && final.failure) result.failure = final.failure;
|
|
679
|
+
if (result.failure) {
|
|
680
|
+
parts.push(noticePart("error", "router-error", result.failure.message, result.failure.code));
|
|
681
|
+
push({ type: "error", data: { message: result.failure.message, code: result.failure.code } });
|
|
682
|
+
}
|
|
683
|
+
}).finally(async () => {
|
|
684
|
+
const retiring = retire();
|
|
685
|
+
if (credentialSettled) await retiring;
|
|
686
|
+
try {
|
|
687
|
+
await options.onSettled?.();
|
|
688
|
+
} catch (error) {
|
|
689
|
+
console.error(JSON.stringify({ event: "router_turn_settle_failed", error: redactErrorMessage(error) }));
|
|
690
|
+
}
|
|
691
|
+
signal.removeEventListener("abort", stopped);
|
|
692
|
+
reasoningClock.settle();
|
|
693
|
+
done = true;
|
|
694
|
+
wake?.();
|
|
695
|
+
});
|
|
696
|
+
void run.catch(() => void 0);
|
|
697
|
+
async function* stream() {
|
|
698
|
+
for (; ; ) {
|
|
699
|
+
while (queue.length) yield queue.shift();
|
|
700
|
+
if (done) break;
|
|
701
|
+
await new Promise((resolve) => {
|
|
702
|
+
wake = resolve;
|
|
703
|
+
});
|
|
704
|
+
wake = null;
|
|
705
|
+
}
|
|
706
|
+
await run;
|
|
707
|
+
}
|
|
708
|
+
const snapshot = () => parts.map((part) => part.type === "reasoning" ? { ...part, time: reasoningClock.time(part) } : { ...part });
|
|
709
|
+
const usage = () => !outcome || !usageReported ? {} : {
|
|
710
|
+
// The Router's prompt tokens include the cached ones; the sandbox lanes report input and cache reads apart, so
|
|
711
|
+
// the stored row counts them apart here too and `inputTokens + cacheReadTokens` is the prompt on either lane.
|
|
712
|
+
inputTokens: outcome.usage.promptTokens - (outcome.usage.cachedPromptTokens ?? 0),
|
|
713
|
+
outputTokens: outcome.usage.completionTokens,
|
|
714
|
+
...outcome.usage.cachedPromptTokens !== void 0 ? { cacheReadTokens: outcome.usage.cachedPromptTokens } : {}
|
|
715
|
+
};
|
|
716
|
+
const events = stream();
|
|
717
|
+
const end = events.return.bind(events);
|
|
718
|
+
const fail = events.throw.bind(events);
|
|
719
|
+
events.return = async (value) => {
|
|
720
|
+
abort.abort(new Error("The chat route stopped reading this turn"));
|
|
721
|
+
try {
|
|
722
|
+
return await end(value);
|
|
723
|
+
} finally {
|
|
724
|
+
const retiring = retire();
|
|
725
|
+
if (credentialSettled) await retiring;
|
|
726
|
+
}
|
|
727
|
+
};
|
|
728
|
+
events.throw = async (error) => {
|
|
729
|
+
abort.abort(error);
|
|
730
|
+
try {
|
|
731
|
+
return await fail(error);
|
|
732
|
+
} finally {
|
|
733
|
+
const retiring = retire();
|
|
734
|
+
if (credentialSettled) await retiring;
|
|
735
|
+
}
|
|
736
|
+
};
|
|
737
|
+
return {
|
|
738
|
+
stream: events,
|
|
739
|
+
// The route tracks this continuation even when failure is visible before a pending key arrives.
|
|
740
|
+
cleanup: run.then(retire, retire),
|
|
741
|
+
finalText: () => outcome?.text ?? "",
|
|
742
|
+
assistantParts: snapshot,
|
|
743
|
+
draftParts: snapshot,
|
|
744
|
+
usage,
|
|
745
|
+
get model() {
|
|
746
|
+
return outcome?.servedModel ?? void 0;
|
|
747
|
+
},
|
|
748
|
+
modelAttribution: () => ({
|
|
749
|
+
requestedModel: options.model,
|
|
750
|
+
...outcome?.servedModel ? { servedModel: outcome.servedModel } : {},
|
|
751
|
+
echoReceived: Boolean(outcome?.servedModel)
|
|
752
|
+
})
|
|
1071
753
|
};
|
|
1072
|
-
return { pendingModelNotices, source, captureEffectiveBackend, servedModel, servingModel, failover, modelState, openAttempts: () => openAttempts };
|
|
1073
|
-
}
|
|
1074
|
-
function sameModel(a, b) {
|
|
1075
|
-
const bare = (id) => id.slice(id.lastIndexOf("/") + 1).trim().toLowerCase();
|
|
1076
|
-
return bare(a) === bare(b);
|
|
1077
754
|
}
|
|
1078
755
|
|
|
1079
|
-
// src/chat-routes/
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
}
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
return snapshot.slice(previous.length);
|
|
1092
|
-
}
|
|
1093
|
-
tracker.seen.set(key, snapshot);
|
|
1094
|
-
return snapshot;
|
|
1095
|
-
}
|
|
1096
|
-
function toFiniteNumber(value) {
|
|
1097
|
-
if (typeof value === "number") return Number.isFinite(value) ? value : null;
|
|
1098
|
-
if (typeof value === "string" && value.trim() !== "") {
|
|
1099
|
-
const parsed = Number(value);
|
|
1100
|
-
return Number.isFinite(parsed) ? parsed : null;
|
|
1101
|
-
}
|
|
1102
|
-
return null;
|
|
1103
|
-
}
|
|
1104
|
-
function extractReportedTurnUsage(data) {
|
|
1105
|
-
const tokenUsage = asRecord(data?.tokenUsage);
|
|
1106
|
-
if (!tokenUsage) return null;
|
|
1107
|
-
const inputTokens = toFiniteNumber(tokenUsage.inputTokens);
|
|
1108
|
-
const outputTokens = toFiniteNumber(tokenUsage.outputTokens);
|
|
1109
|
-
if (inputTokens === null || outputTokens === null) return null;
|
|
1110
|
-
const reported = { inputTokens, outputTokens };
|
|
1111
|
-
const reasoningTokens = toFiniteNumber(tokenUsage.reasoningTokens);
|
|
1112
|
-
if (reasoningTokens !== null) reported.reasoningTokens = reasoningTokens;
|
|
1113
|
-
const cacheReadTokens = toFiniteNumber(tokenUsage.cacheReadInputTokens);
|
|
1114
|
-
if (cacheReadTokens !== null) reported.cacheReadTokens = cacheReadTokens;
|
|
1115
|
-
const cacheWriteTokens = toFiniteNumber(tokenUsage.cacheCreationInputTokens);
|
|
1116
|
-
if (cacheWriteTokens !== null) reported.cacheWriteTokens = cacheWriteTokens;
|
|
1117
|
-
const costUsd = toFiniteNumber(data?.totalCostUsd) ?? toFiniteNumber(tokenUsage.cost);
|
|
1118
|
-
if (costUsd !== null) reported.costUsd = costUsd;
|
|
1119
|
-
return reported;
|
|
1120
|
-
}
|
|
1121
|
-
function extractUsageSnapshot(value) {
|
|
1122
|
-
const inputTokens = toFiniteNumber(value?.promptTokens);
|
|
1123
|
-
const outputTokens = toFiniteNumber(value?.completionTokens);
|
|
1124
|
-
if (inputTokens === null || outputTokens === null || inputTokens < 0 || outputTokens < 0) return null;
|
|
1125
|
-
const reported = { inputTokens, outputTokens };
|
|
1126
|
-
const reasoningTokens = toFiniteNumber(value?.reasoningTokens);
|
|
1127
|
-
if (reasoningTokens !== null && reasoningTokens >= 0) reported.reasoningTokens = reasoningTokens;
|
|
1128
|
-
const costUsd = toFiniteNumber(value?.providerCostUsd);
|
|
1129
|
-
if (costUsd !== null && costUsd >= 0) reported.costUsd = costUsd;
|
|
1130
|
-
return reported;
|
|
1131
|
-
}
|
|
1132
|
-
function applyTerminalUsage(reported, usage) {
|
|
1133
|
-
usage.inputTokens = reported.inputTokens;
|
|
1134
|
-
usage.outputTokens = reported.outputTokens;
|
|
1135
|
-
if (reported.reasoningTokens !== void 0) usage.reasoningTokens = reported.reasoningTokens;
|
|
1136
|
-
if (reported.cacheReadTokens !== void 0) usage.cacheReadTokens = reported.cacheReadTokens;
|
|
1137
|
-
if (reported.cacheWriteTokens !== void 0) usage.cacheWriteTokens = reported.cacheWriteTokens;
|
|
1138
|
-
if (reported.costUsd !== void 0) usage.costUsd = reported.costUsd;
|
|
1139
|
-
}
|
|
1140
|
-
function sandboxStreamErrorMessage(data) {
|
|
1141
|
-
const record = asRecord(data);
|
|
1142
|
-
const message = asString(record?.message) ?? asString(record?.error);
|
|
1143
|
-
if (message) return message;
|
|
1144
|
-
try {
|
|
1145
|
-
return JSON.stringify(data) || "Sandbox stream returned an error event without details.";
|
|
1146
|
-
} catch {
|
|
1147
|
-
return "Sandbox stream returned an error event without details.";
|
|
1148
|
-
}
|
|
756
|
+
// src/chat-routes/hybrid-producer.ts
|
|
757
|
+
var HYBRID_TURN_GUIDANCE = "Start your reply with text: before your first tool call, write one short sentence telling the member what you are about to do, so they never wait in silence. Use sandbox_run only for work that needs code execution or files on the workspace computer. Give the sandbox agent the member's requirements, paths and exact field names, not model-written code; the sandbox agent writes and runs the code. Report what it returns.";
|
|
758
|
+
var DEFAULT_SANDBOX_RUN_TIMEOUT_MS = 4 * 6e4;
|
|
759
|
+
function startHybridBox(body, acquire) {
|
|
760
|
+
const attachments = (body.parts ?? []).map((part) => ({
|
|
761
|
+
...part.mediaType ? { mediaType: part.mediaType } : {},
|
|
762
|
+
...part.filename ? { name: part.filename } : {}
|
|
763
|
+
}));
|
|
764
|
+
if (escalationHints({ text: body.content ?? "", attachments }).length === 0) return void 0;
|
|
765
|
+
const started = acquire();
|
|
766
|
+
started.catch(() => void 0);
|
|
767
|
+
return started;
|
|
1149
768
|
}
|
|
1150
|
-
function
|
|
1151
|
-
return
|
|
769
|
+
function turnPromptText(prompt) {
|
|
770
|
+
return typeof prompt === "string" ? prompt : prompt.map((part) => part.type === "text" ? part.text : "").join("\n");
|
|
1152
771
|
}
|
|
1153
|
-
function
|
|
1154
|
-
return
|
|
772
|
+
function turnCarriesFiles(prompt) {
|
|
773
|
+
return typeof prompt !== "string" && prompt.some((part) => part.type !== "text");
|
|
1155
774
|
}
|
|
1156
|
-
function
|
|
1157
|
-
const
|
|
1158
|
-
|
|
1159
|
-
const diagnostics = asRecord(error?.diagnostics);
|
|
1160
|
-
let diagnosticText;
|
|
1161
|
-
if (diagnostics) {
|
|
1162
|
-
try {
|
|
1163
|
-
diagnosticText = JSON.stringify(diagnostics);
|
|
1164
|
-
} catch {
|
|
1165
|
-
diagnosticText = "[unserializable diagnostics]";
|
|
1166
|
-
}
|
|
1167
|
-
}
|
|
1168
|
-
const userMessage = [
|
|
1169
|
-
"The sandbox model stream stopped before a clean completion.",
|
|
1170
|
-
`Error: ${message}`,
|
|
1171
|
-
"Please retry. If it repeats, send these support details to the team."
|
|
1172
|
-
].join("\n\n");
|
|
1173
|
-
const failureNote = diagnosticText ? `sandbox-stream: ${message}; ${diagnosticText}` : `sandbox-stream: ${message}`;
|
|
1174
|
-
let code = "sandbox.stream_failed";
|
|
1175
|
-
if (error instanceof SandboxRunError) {
|
|
1176
|
-
code = error.code;
|
|
1177
|
-
} else if (error instanceof ModelFailoverTimeoutError) {
|
|
1178
|
-
code = error.code;
|
|
1179
|
-
} else if (error instanceof ModelFailoverExhaustedError) {
|
|
1180
|
-
code = error.upstreamCode ?? code;
|
|
1181
|
-
}
|
|
1182
|
-
return { userMessage, failureNote, code };
|
|
775
|
+
function routerTurnHistory(priorMessages, limit = 40, view = {}) {
|
|
776
|
+
const rows = priorMessages.filter((message) => message.role === "user" || message.role === "assistant");
|
|
777
|
+
return projectDispatchHistory(rows, { ...view, attachments: false }).filter((message) => Boolean(message.content.trim())).slice(-limit);
|
|
1183
778
|
}
|
|
1184
|
-
function
|
|
1185
|
-
const
|
|
1186
|
-
const
|
|
1187
|
-
const
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
779
|
+
function createHybridTurnProducer(options) {
|
|
780
|
+
const { box, tools = [], speculateOn, attachments, onSpeculate, guidance, onSettled, admitted, signal, ...router } = options;
|
|
781
|
+
const abort = new AbortController();
|
|
782
|
+
const stop = signal ? AbortSignal.any([abort.signal, signal]) : abort.signal;
|
|
783
|
+
void admitted?.catch((error) => {
|
|
784
|
+
abort.abort(error);
|
|
785
|
+
});
|
|
786
|
+
const timeoutMs = box.timeoutMs ?? DEFAULT_SANDBOX_RUN_TIMEOUT_MS;
|
|
787
|
+
let activeTasks = 0;
|
|
788
|
+
let unconfirmed = false;
|
|
789
|
+
let start = box.started?.catch((error) => {
|
|
790
|
+
start = void 0;
|
|
791
|
+
throw error;
|
|
792
|
+
});
|
|
793
|
+
start?.catch(() => void 0);
|
|
794
|
+
const acquire = async () => {
|
|
795
|
+
await admitted;
|
|
796
|
+
stop.throwIfAborted();
|
|
797
|
+
return start ??= box.acquire(stop).catch((error) => {
|
|
798
|
+
start = void 0;
|
|
799
|
+
throw error;
|
|
800
|
+
});
|
|
1196
801
|
};
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
802
|
+
let settled;
|
|
803
|
+
const settle = () => settled ??= (async () => {
|
|
804
|
+
abort.abort(new Error("The hybrid turn settled"));
|
|
805
|
+
await (box.started ?? start)?.catch(() => void 0);
|
|
806
|
+
const results = await Promise.allSettled([Promise.resolve().then(() => box.dispose?.()), Promise.resolve().then(() => onSettled?.())]);
|
|
807
|
+
for (const result of results) if (result.status === "rejected") {
|
|
808
|
+
console.error(JSON.stringify({ event: "hybrid_turn_settle_failed", error: redactErrorMessage(result.reason) }));
|
|
809
|
+
}
|
|
810
|
+
})();
|
|
811
|
+
try {
|
|
812
|
+
const step = box.computer ? createComputerStep({ ...box.computer, parentExecutionId: box.computer.parentExecutionId ?? router.traceId }) : void 0;
|
|
813
|
+
const runBoxTask = box.runTask ?? step?.runTask;
|
|
814
|
+
if (!runBoxTask) throw new Error("A hybrid box needs runTask or computer");
|
|
815
|
+
const warnings = /* @__PURE__ */ new Map();
|
|
816
|
+
const details = /* @__PURE__ */ new Map();
|
|
817
|
+
const withWarnings = (parts) => parts.flatMap((part) => {
|
|
818
|
+
const warning = warnings.get(String(part.callID ?? part.id ?? ""));
|
|
819
|
+
return warning ? [part, noticePart(warning.noticeKind, warning.id, warning.text, warning.code, warning.detail)] : [part];
|
|
820
|
+
});
|
|
821
|
+
const sandboxRun = createSandboxRunTool({
|
|
822
|
+
box: () => acquire(),
|
|
823
|
+
runTask: async (...args) => {
|
|
824
|
+
await admitted;
|
|
825
|
+
stop.throwIfAborted();
|
|
826
|
+
if (unconfirmed || activeTasks > 0) throw new SandboxRunError("sandbox_unconfirmed", "An earlier computer step may still be running; no further task was dispatched.");
|
|
827
|
+
activeTasks++;
|
|
828
|
+
try {
|
|
829
|
+
return await runBoxTask(...args);
|
|
830
|
+
} catch (error) {
|
|
831
|
+
if (!(error instanceof SandboxRunError) || error.code === "sandbox_unconfirmed") unconfirmed = true;
|
|
832
|
+
throw error;
|
|
833
|
+
} finally {
|
|
834
|
+
activeTasks--;
|
|
1213
835
|
}
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
|
|
836
|
+
},
|
|
837
|
+
memberRequest: router.userMessage,
|
|
838
|
+
timeoutMs,
|
|
839
|
+
signal: stop,
|
|
840
|
+
...box.warm ? { warm: box.warm } : {},
|
|
841
|
+
onFailure: (error, call) => {
|
|
842
|
+
const detail = box.failureDetail ? box.failureDetail(error) : sandboxRunFailureDetail(error);
|
|
843
|
+
if (call.callId && detail) details.set(call.callId, detail);
|
|
844
|
+
if (box.onFailure) box.onFailure(error, call);
|
|
845
|
+
else console.error(JSON.stringify({
|
|
846
|
+
event: "agent_app_sandbox_run_failed",
|
|
847
|
+
callId: call.callId ?? null,
|
|
848
|
+
code: error.code,
|
|
849
|
+
detail: sandboxRunFailureDetail(error)
|
|
850
|
+
}));
|
|
851
|
+
},
|
|
852
|
+
...box.name ? { name: box.name } : {},
|
|
853
|
+
...box.description ? { description: box.description } : {}
|
|
854
|
+
});
|
|
855
|
+
const speculator = createEscalationSpeculator({
|
|
856
|
+
warm: acquire,
|
|
857
|
+
escalationTools: [sandboxRun.name],
|
|
858
|
+
...onSpeculate ? { onSpeculate } : {}
|
|
859
|
+
});
|
|
860
|
+
speculator.onInput({ text: speculateOn ?? router.userMessage, ...attachments ? { attachments } : {} });
|
|
861
|
+
const extra = guidance === false ? "" : [guidance ?? HYBRID_TURN_GUIDANCE, step?.guidance].filter(Boolean).join("\n\n");
|
|
862
|
+
const producer = createRouterTurnProducer({
|
|
863
|
+
...router,
|
|
864
|
+
systemPrompt: Promise.resolve(router.systemPrompt).then((prompt) => !extra ? prompt : typeof prompt === "string" ? `${prompt}
|
|
865
|
+
|
|
866
|
+
${extra}` : { ...prompt, stable: [...prompt.stable ?? [], extra] }),
|
|
867
|
+
tools: [...tools, sandboxRun],
|
|
868
|
+
admitted,
|
|
869
|
+
// A turn that calls the box must outlive the call it waits on.
|
|
870
|
+
budgetMs: router.budgetMs ?? timeoutMs + 6e4,
|
|
871
|
+
signal: stop,
|
|
872
|
+
onSettled: settle,
|
|
873
|
+
onEvent: (event) => {
|
|
874
|
+
speculator.onEvent(event);
|
|
875
|
+
if (event.type === "tool" && event.status === "error" && event.toolName === sandboxRun.name && !stop.aborted) {
|
|
876
|
+
const code = event.error.code;
|
|
877
|
+
if (code === "sandbox_unconfirmed") unconfirmed = true;
|
|
878
|
+
if (code === "sandbox_unavailable" || code === "sandbox_failed" || code === "sandbox_unconfirmed" || code === "sandbox_empty") {
|
|
879
|
+
const detail = details.get(event.callId);
|
|
880
|
+
warnings.set(event.callId, {
|
|
881
|
+
type: "notice",
|
|
882
|
+
noticeKind: "warning",
|
|
883
|
+
id: `sandbox-warning:${event.callId}`,
|
|
884
|
+
code,
|
|
885
|
+
text: code === "sandbox_unavailable" ? "The workspace computer is unavailable. I can continue with work that does not need it." : "The computer step did not return a confirmed answer. I can continue with work that does not need it.",
|
|
886
|
+
...detail ? { detail } : {}
|
|
1245
887
|
});
|
|
1246
888
|
}
|
|
1247
|
-
const text = options.declinedNotice?.(parsed.value, declineFailed) ?? (declineFailed ? `The agent requested ${parsed.value.kind} approval; declining it failed \u2014 it will expire on its own.` : `The agent requested ${parsed.value.kind} approval \u2014 auto-declined by policy.`);
|
|
1248
|
-
const notice = noticePart("auto-declined", `auto-declined-${parsed.value.id}`, text);
|
|
1249
|
-
recordPersistedPart(notice, void 0, noticePartKey(notice.id));
|
|
1250
|
-
yield { type: "notice", id: notice.id, noticeKind: "auto-declined", text };
|
|
1251
|
-
continue;
|
|
1252
|
-
}
|
|
1253
|
-
if (event.type === "interaction.cancel") {
|
|
1254
|
-
const parsed = parseInteractionCancel(asRecord(record.data));
|
|
1255
|
-
if (!parsed.succeeded) {
|
|
1256
|
-
log("[chat-routes] dropping malformed interaction.cancel event", { error: parsed.error });
|
|
1257
|
-
continue;
|
|
1258
|
-
}
|
|
1259
|
-
const key = interactionPartKey(parsed.value.id);
|
|
1260
|
-
const existing = partMap.get(key);
|
|
1261
|
-
if (existing?.type === "interaction" && existing.status === "pending") {
|
|
1262
|
-
recordPersistedPart({
|
|
1263
|
-
...existing,
|
|
1264
|
-
status: cancelStatusFor(parsed.value.reason),
|
|
1265
|
-
...parsed.value.reason ? { cancelReason: parsed.value.reason } : {}
|
|
1266
|
-
}, void 0, key);
|
|
1267
|
-
}
|
|
1268
|
-
yield toProducerWireEvent(record);
|
|
1269
|
-
continue;
|
|
1270
|
-
}
|
|
1271
|
-
if (event.type === "plan.submitted") {
|
|
1272
|
-
const parsed = parsePlanSubmittedEvent(record);
|
|
1273
|
-
if (!parsed.succeeded) {
|
|
1274
|
-
log("[chat-routes] dropping malformed plan.submitted event", { error: parsed.error });
|
|
1275
|
-
continue;
|
|
1276
|
-
}
|
|
1277
|
-
recordPersistedPart(planToPersistedPart(parsed.value), void 0);
|
|
1278
|
-
yield toProducerWireEvent(record);
|
|
1279
|
-
continue;
|
|
1280
|
-
}
|
|
1281
|
-
if (event.type === "warning") {
|
|
1282
|
-
const message = asString(event.data?.message);
|
|
1283
|
-
if (message) {
|
|
1284
|
-
projectionState.warningCount += 1;
|
|
1285
|
-
const code = asString(event.data?.code);
|
|
1286
|
-
const text = code ? `${code}: ${message}` : message;
|
|
1287
|
-
const notice = noticePart("warning", `warning-${projectionState.warningCount}`, text);
|
|
1288
|
-
recordPersistedPart(notice, void 0, noticePartKey(notice.id));
|
|
1289
|
-
yield { type: "notice", id: notice.id, noticeKind: "warning", text };
|
|
1290
|
-
}
|
|
1291
|
-
yield toProducerWireEvent(record);
|
|
1292
|
-
continue;
|
|
1293
|
-
}
|
|
1294
|
-
if (event.type === "usage") {
|
|
1295
|
-
const reported = extractUsageSnapshot(asRecord(record.usage));
|
|
1296
|
-
if (reported) applyTerminalUsage(reported, usage);
|
|
1297
|
-
yield toProducerWireEvent(record);
|
|
1298
|
-
continue;
|
|
1299
|
-
}
|
|
1300
|
-
if (event.type === "result") {
|
|
1301
|
-
projectionState.terminalObserved = true;
|
|
1302
|
-
const finalText = asString(event.data?.finalText);
|
|
1303
|
-
if (finalText) projectionState.fullText = finalText;
|
|
1304
|
-
const reported = extractReportedTurnUsage(asRecord(event.data));
|
|
1305
|
-
if (reported) {
|
|
1306
|
-
applyTerminalUsage(reported, usage);
|
|
1307
|
-
yield* emitFinalUsage();
|
|
1308
|
-
} else {
|
|
1309
|
-
const resultUsage = asRecord(event.data?.usage);
|
|
1310
|
-
if (resultUsage) {
|
|
1311
|
-
const input = Number(resultUsage.inputTokens);
|
|
1312
|
-
const output = Number(resultUsage.outputTokens);
|
|
1313
|
-
if (Number.isFinite(input)) usage.inputTokens = input;
|
|
1314
|
-
if (Number.isFinite(output)) usage.outputTokens = output;
|
|
1315
|
-
}
|
|
1316
|
-
}
|
|
1317
|
-
yield* emitTerminalizedTools();
|
|
1318
|
-
continue;
|
|
1319
889
|
}
|
|
1320
|
-
|
|
1321
|
-
|
|
1322
|
-
|
|
1323
|
-
|
|
1324
|
-
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
890
|
+
router.onEvent?.(event);
|
|
891
|
+
}
|
|
892
|
+
});
|
|
893
|
+
const events = (async function* () {
|
|
894
|
+
for await (const event of producer.stream) {
|
|
895
|
+
yield event;
|
|
896
|
+
const side = event.type === "tool.event" ? event.data : void 0;
|
|
897
|
+
if (side?.kind === "computer_interaction" && side.data && typeof side.data === "object") yield side.data;
|
|
898
|
+
if (event.type === "tool_result") {
|
|
899
|
+
const warning = warnings.get(event.toolCallId);
|
|
900
|
+
if (warning) yield warning;
|
|
1330
901
|
}
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
|
|
1336
|
-
|
|
1337
|
-
|
|
1338
|
-
|
|
1339
|
-
|
|
1340
|
-
|
|
1341
|
-
|
|
902
|
+
}
|
|
903
|
+
})();
|
|
904
|
+
const end = events.return.bind(events);
|
|
905
|
+
const fail = events.throw.bind(events);
|
|
906
|
+
events.return = async (value) => {
|
|
907
|
+
const closing = producer.stream.return(value);
|
|
908
|
+
try {
|
|
909
|
+
return await end(value);
|
|
910
|
+
} finally {
|
|
911
|
+
await closing;
|
|
912
|
+
}
|
|
913
|
+
};
|
|
914
|
+
events.throw = async (error) => {
|
|
915
|
+
const closing = producer.stream.throw(error);
|
|
916
|
+
try {
|
|
917
|
+
return await fail(error);
|
|
918
|
+
} finally {
|
|
919
|
+
await closing;
|
|
920
|
+
}
|
|
921
|
+
};
|
|
922
|
+
return {
|
|
923
|
+
...producer,
|
|
924
|
+
stream: events,
|
|
925
|
+
terminalObserved: () => activeTasks === 0 && !unconfirmed,
|
|
926
|
+
assistantParts: () => withWarnings(producer.assistantParts?.() ?? []),
|
|
927
|
+
draftParts: () => withWarnings(producer.draftParts?.() ?? []),
|
|
928
|
+
get model() {
|
|
929
|
+
return producer.model;
|
|
930
|
+
}
|
|
931
|
+
};
|
|
932
|
+
} catch (error) {
|
|
933
|
+
return createRouterTurnProducer({ ...router, systemPrompt: Promise.reject(error), signal: stop, onSettled: settle });
|
|
934
|
+
}
|
|
935
|
+
}
|
|
1342
936
|
|
|
1343
|
-
|
|
1344
|
-
|
|
1345
|
-
|
|
1346
|
-
|
|
1347
|
-
|
|
1348
|
-
|
|
1349
|
-
|
|
1350
|
-
|
|
1351
|
-
|
|
937
|
+
// src/chat-routes/model-failover-stream.ts
|
|
938
|
+
var TERMINAL_FAILURE_TYPES = /* @__PURE__ */ new Set(["error", "session.run.failed"]);
|
|
939
|
+
var NON_COMMITTING_PART_TYPES = /* @__PURE__ */ new Set(["step-start", "step-finish"]);
|
|
940
|
+
function isCommittingSandboxEvent(event) {
|
|
941
|
+
const record = asRecord(event);
|
|
942
|
+
if (!record) return false;
|
|
943
|
+
const type = asString(record.type) ?? "";
|
|
944
|
+
if (!type) return false;
|
|
945
|
+
if (type === "message.part.updated") {
|
|
946
|
+
const data = asRecord(record.data);
|
|
947
|
+
const part = asRecord(data?.part);
|
|
948
|
+
const partType = asString(part?.type) ?? "";
|
|
949
|
+
if (NON_COMMITTING_PART_TYPES.has(partType)) return false;
|
|
950
|
+
if (partType === "text" || partType === "reasoning") {
|
|
951
|
+
return [data?.delta, part?.text, part?.content].some((value) => typeof value === "string" && value.length > 0);
|
|
952
|
+
}
|
|
953
|
+
return true;
|
|
954
|
+
}
|
|
955
|
+
if (type === "start" || type === "execution.started" || type === "status" || type === "model-processing" || type === "model.processing" || // Public Sandbox timing metadata: a request was attempted, not answered.
|
|
956
|
+
type === "model.first_request" || type === "session.created" || type === "session.updated" || type === "session.idle" || type === "step-start" || type === "step-finish" || type === "turn" || type === "warning") {
|
|
957
|
+
return false;
|
|
958
|
+
}
|
|
959
|
+
return true;
|
|
960
|
+
}
|
|
961
|
+
function summarizeFailoverReason(reason) {
|
|
962
|
+
const collapsed = reason.replace(/\s+/g, " ").trim();
|
|
963
|
+
if (!collapsed) return "upstream unavailable";
|
|
964
|
+
if (/<!doctype html|<html[\s>]/i.test(collapsed)) {
|
|
965
|
+
const status = readHttpStatusHint(collapsed);
|
|
966
|
+
return status ? `HTTP ${status}` : "upstream returned an error page";
|
|
967
|
+
}
|
|
968
|
+
const LIMIT = 160;
|
|
969
|
+
if (collapsed.length <= LIMIT) return collapsed;
|
|
970
|
+
const clipped = collapsed.slice(0, LIMIT);
|
|
971
|
+
const lastSpace = clipped.lastIndexOf(" ");
|
|
972
|
+
return `${(lastSpace > LIMIT - 30 ? clipped.slice(0, lastSpace) : clipped).trimEnd()}\u2026`;
|
|
973
|
+
}
|
|
974
|
+
function isLiveLifecycleEvent(event) {
|
|
975
|
+
const type = asString(asRecord(event)?.type) ?? "";
|
|
976
|
+
return type === "start" || type === "execution.started" || type === "status" || type === "model-processing" || type === "model.processing" || // Public Sandbox timing metadata: a request was attempted, not answered.
|
|
977
|
+
type === "model.first_request" || type === "session.created" || type === "session.updated" || type === "session.idle";
|
|
978
|
+
}
|
|
979
|
+
function classifyTerminalFailure(event) {
|
|
980
|
+
const record = asRecord(event);
|
|
981
|
+
if (!record) return null;
|
|
982
|
+
const type = asString(record.type) ?? "";
|
|
983
|
+
if (!TERMINAL_FAILURE_TYPES.has(type)) return null;
|
|
984
|
+
const data = asRecord(record.data);
|
|
985
|
+
const outage = isUpstreamUnavailable(data) || isUpstreamUnavailable(record);
|
|
986
|
+
const reason = asString(data?.message) ?? asString(data?.error) ?? asString(data?.reason) ?? asString(record.message) ?? `sandbox stream reported ${type}`;
|
|
987
|
+
const code = readUpstreamUnavailableCode(data) ?? asString(data?.errorCode) ?? asString(data?.code) ?? readUpstreamUnavailableCode(record) ?? asString(record.errorCode) ?? asString(record.code);
|
|
988
|
+
return { outage, reason, ...code ? { code } : {} };
|
|
989
|
+
}
|
|
990
|
+
var DEFAULT_MODEL_STREAM_OPEN_TIMEOUT_MS = 12e4;
|
|
991
|
+
var DEFAULT_MODEL_FIRST_RESPONSE_TIMEOUT_MS = 6e4;
|
|
992
|
+
var ModelFailoverTimeoutError = class extends ModelFailoverExhaustedError {
|
|
993
|
+
code;
|
|
994
|
+
model;
|
|
995
|
+
timeoutMs;
|
|
996
|
+
constructor(input) {
|
|
997
|
+
super(input.attempts);
|
|
998
|
+
this.name = "ModelFailoverTimeoutError";
|
|
999
|
+
this.code = input.code;
|
|
1000
|
+
this.model = input.model;
|
|
1001
|
+
this.timeoutMs = input.timeoutMs;
|
|
1002
|
+
this.message = input.code === "model_stream_open_timeout" ? `Opening the model event source for ${input.model} exceeded ${input.timeoutMs}ms.` : `Model ${input.model} produced no first answer-bearing event within ${input.timeoutMs}ms.`;
|
|
1003
|
+
}
|
|
1004
|
+
};
|
|
1005
|
+
function resolveTimeoutMs(value, fallback, label) {
|
|
1006
|
+
if (value === void 0) return fallback;
|
|
1007
|
+
if (!Number.isFinite(value) || value <= 0) {
|
|
1008
|
+
throw new RangeError(`${label} must be a finite number greater than 0`);
|
|
1009
|
+
}
|
|
1010
|
+
return Math.max(1, Math.trunc(value));
|
|
1011
|
+
}
|
|
1012
|
+
var TIMED_OUT = /* @__PURE__ */ Symbol("model-attempt-timed-out");
|
|
1013
|
+
function deadlineAfter(timeoutMs) {
|
|
1014
|
+
let timer;
|
|
1015
|
+
const promise = new Promise((resolve) => {
|
|
1016
|
+
timer = setTimeout(() => resolve(TIMED_OUT), timeoutMs);
|
|
1017
|
+
});
|
|
1018
|
+
return {
|
|
1019
|
+
promise,
|
|
1020
|
+
clear: () => {
|
|
1021
|
+
if (timer !== void 0) {
|
|
1022
|
+
clearTimeout(timer);
|
|
1023
|
+
timer = void 0;
|
|
1024
|
+
}
|
|
1025
|
+
}
|
|
1026
|
+
};
|
|
1027
|
+
}
|
|
1028
|
+
function isEmptyTerminalReceipt(event) {
|
|
1029
|
+
const record = asRecord(event);
|
|
1030
|
+
if (!record) return false;
|
|
1031
|
+
const type = asString(record.type) ?? "";
|
|
1032
|
+
if (type !== "result" && type !== "done") return false;
|
|
1033
|
+
const data = asRecord(record.data);
|
|
1034
|
+
const text = asString(data?.finalText) ?? asString(data?.text) ?? "";
|
|
1035
|
+
return text.trim().length === 0;
|
|
1036
|
+
}
|
|
1037
|
+
var MAX_EMPTY_TURN_RETRIES = 3;
|
|
1038
|
+
function resolveEmptyTurnRetries(value) {
|
|
1039
|
+
if (typeof value !== "number" || !Number.isFinite(value)) return 0;
|
|
1040
|
+
const truncated = Math.trunc(value);
|
|
1041
|
+
if (truncated <= 0) return 0;
|
|
1042
|
+
return Math.min(truncated, MAX_EMPTY_TURN_RETRIES);
|
|
1043
|
+
}
|
|
1044
|
+
async function closeIterator(iterator, log) {
|
|
1045
|
+
try {
|
|
1046
|
+
await iterator.return?.();
|
|
1047
|
+
} catch (err) {
|
|
1048
|
+
log?.("[chat-routes] abandoning a failed model stream threw", {
|
|
1049
|
+
error: err instanceof Error ? err.message : String(err)
|
|
1050
|
+
});
|
|
1051
|
+
}
|
|
1052
|
+
}
|
|
1053
|
+
function streamWithModelFailover(options) {
|
|
1054
|
+
const committing = options.isCommitting ?? isCommittingSandboxEvent;
|
|
1055
|
+
const emptyTurnRetries = resolveEmptyTurnRetries(options.emptyTurnRetries);
|
|
1056
|
+
const openTimeoutMs = resolveTimeoutMs(
|
|
1057
|
+
options.openTimeoutMs,
|
|
1058
|
+
DEFAULT_MODEL_STREAM_OPEN_TIMEOUT_MS,
|
|
1059
|
+
"openTimeoutMs"
|
|
1060
|
+
);
|
|
1061
|
+
const firstResponseTimeoutMs = resolveTimeoutMs(
|
|
1062
|
+
options.firstResponseTimeoutMs,
|
|
1063
|
+
DEFAULT_MODEL_FIRST_RESPONSE_TIMEOUT_MS,
|
|
1064
|
+
"firstResponseTimeoutMs"
|
|
1065
|
+
);
|
|
1066
|
+
const liveLifecycle = options.liveLifecycleEvents === true;
|
|
1067
|
+
const progress = createProgressQueue();
|
|
1068
|
+
let serving;
|
|
1069
|
+
let trail = [];
|
|
1070
|
+
let fellBack = false;
|
|
1071
|
+
const attemptState = { index: 0 };
|
|
1072
|
+
let finalTimeout;
|
|
1073
|
+
const { drainOnce } = createModelAttemptDrainer(attemptState, options, openTimeoutMs, firstResponseTimeoutMs, committing, liveLifecycle, progress);
|
|
1074
|
+
const probe = async (model) => {
|
|
1075
|
+
finalTimeout = void 0;
|
|
1076
|
+
for (let retry = 0; ; retry += 1) {
|
|
1077
|
+
const pass = await drainOnce(model);
|
|
1078
|
+
if (!pass.empty || retry >= emptyTurnRetries) {
|
|
1079
|
+
if (!pass.outcome.committed && pass.outcome.timeoutCode) {
|
|
1080
|
+
finalTimeout = {
|
|
1081
|
+
code: pass.outcome.timeoutCode,
|
|
1082
|
+
model,
|
|
1083
|
+
timeoutMs: pass.outcome.timeoutCode === "model_stream_open_timeout" ? openTimeoutMs : firstResponseTimeoutMs
|
|
1084
|
+
};
|
|
1085
|
+
} else {
|
|
1086
|
+
finalTimeout = void 0;
|
|
1352
1087
|
}
|
|
1353
|
-
|
|
1088
|
+
return pass.outcome;
|
|
1354
1089
|
}
|
|
1355
|
-
|
|
1356
|
-
|
|
1357
|
-
|
|
1358
|
-
const
|
|
1359
|
-
log("[chat-routes]
|
|
1360
|
-
|
|
1361
|
-
error: streamErr instanceof Error ? streamErr.message : String(streamErr)
|
|
1090
|
+
const iterator = pass.outcome.committed ? pass.outcome.iterator : null;
|
|
1091
|
+
if (pass.outcome.committed) pass.outcome.abort();
|
|
1092
|
+
if (iterator) void closeIterator(iterator, options.log);
|
|
1093
|
+
const info = { model, retry: retry + 1, remaining: emptyTurnRetries - retry - 1 };
|
|
1094
|
+
options.log?.("[chat-routes] turn completed with no assistant text; re-running the same model", {
|
|
1095
|
+
...info
|
|
1362
1096
|
});
|
|
1363
|
-
|
|
1364
|
-
|
|
1365
|
-
|
|
1366
|
-
|
|
1367
|
-
|
|
1368
|
-
|
|
1369
|
-
|
|
1370
|
-
|
|
1371
|
-
|
|
1097
|
+
options.onEmptyTurnRetry?.(info);
|
|
1098
|
+
}
|
|
1099
|
+
};
|
|
1100
|
+
const events = (async function* () {
|
|
1101
|
+
let handle;
|
|
1102
|
+
const settled = runWithModelFailover({
|
|
1103
|
+
models: options.models,
|
|
1104
|
+
run: probe,
|
|
1105
|
+
...options.policy ? { policy: options.policy } : {},
|
|
1106
|
+
// The probe has already classified the raw payload with
|
|
1107
|
+
// `isUpstreamUnavailable`; this reads its verdict rather than
|
|
1108
|
+
// re-classifying a wrapper object, so the two can never disagree.
|
|
1109
|
+
isUnavailableResult: (result) => result.committed === false,
|
|
1110
|
+
onFallback: (attempt, nextModel) => {
|
|
1111
|
+
const info = {
|
|
1112
|
+
from: attempt.model,
|
|
1113
|
+
to: nextModel,
|
|
1114
|
+
reason: attempt.reason ?? "upstream unavailable"
|
|
1115
|
+
};
|
|
1116
|
+
options.log?.("[chat-routes] model upstream unavailable; falling over", { ...info });
|
|
1117
|
+
options.onFallback?.(info);
|
|
1118
|
+
}
|
|
1119
|
+
}).finally(() => progress.close());
|
|
1120
|
+
settled.catch(() => void 0);
|
|
1121
|
+
for (; ; ) {
|
|
1122
|
+
const item = await progress.pull();
|
|
1123
|
+
if (item === PROGRESS_CLOSED) break;
|
|
1124
|
+
yield item;
|
|
1125
|
+
}
|
|
1126
|
+
try {
|
|
1127
|
+
const outcome = await settled;
|
|
1128
|
+
serving = outcome.model;
|
|
1129
|
+
trail = outcome.attempts;
|
|
1130
|
+
fellBack = outcome.usedFallback;
|
|
1131
|
+
handle = outcome.value;
|
|
1132
|
+
} catch (err) {
|
|
1133
|
+
const attempts = err?.attempts;
|
|
1134
|
+
if (Array.isArray(attempts)) trail = attempts;
|
|
1135
|
+
if (err instanceof ModelFailoverExhaustedError && finalTimeout) {
|
|
1136
|
+
throw new ModelFailoverTimeoutError({
|
|
1137
|
+
attempts: trail,
|
|
1138
|
+
...finalTimeout
|
|
1139
|
+
});
|
|
1140
|
+
}
|
|
1141
|
+
throw err;
|
|
1142
|
+
}
|
|
1143
|
+
for (const event of handle.buffered) yield event;
|
|
1144
|
+
const live = handle.iterator;
|
|
1145
|
+
if (!live) {
|
|
1146
|
+
handle.abort();
|
|
1147
|
+
return;
|
|
1148
|
+
}
|
|
1149
|
+
try {
|
|
1150
|
+
for (; ; ) {
|
|
1151
|
+
const next = await live.next();
|
|
1152
|
+
if (next.done) return;
|
|
1153
|
+
yield next.value;
|
|
1372
1154
|
}
|
|
1373
|
-
projectionState.interactionOutcome = "expired";
|
|
1374
|
-
yield* emitTerminalizedTools();
|
|
1375
|
-
yield {
|
|
1376
|
-
type: "error",
|
|
1377
|
-
data: {
|
|
1378
|
-
message: diagnostic.userMessage,
|
|
1379
|
-
code: diagnostic.code,
|
|
1380
|
-
details: { failureNote: diagnostic.failureNote }
|
|
1381
|
-
}
|
|
1382
|
-
};
|
|
1383
1155
|
} finally {
|
|
1384
|
-
|
|
1156
|
+
handle.abort();
|
|
1157
|
+
await closeIterator(live, options.log);
|
|
1385
1158
|
}
|
|
1386
|
-
}
|
|
1159
|
+
})();
|
|
1387
1160
|
return {
|
|
1388
|
-
|
|
1389
|
-
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
|
|
1394
|
-
|
|
1395
|
-
|
|
1396
|
-
|
|
1397
|
-
|
|
1398
|
-
|
|
1399
|
-
|
|
1400
|
-
|
|
1401
|
-
|
|
1402
|
-
|
|
1403
|
-
|
|
1404
|
-
|
|
1405
|
-
|
|
1406
|
-
|
|
1407
|
-
|
|
1408
|
-
return servedModel();
|
|
1161
|
+
events,
|
|
1162
|
+
servingModel: () => serving,
|
|
1163
|
+
attempts: () => trail,
|
|
1164
|
+
usedFallback: () => fellBack
|
|
1165
|
+
};
|
|
1166
|
+
}
|
|
1167
|
+
var PROGRESS_CLOSED = /* @__PURE__ */ Symbol("progress-closed");
|
|
1168
|
+
function createProgressQueue() {
|
|
1169
|
+
const items = [];
|
|
1170
|
+
const waiters = [];
|
|
1171
|
+
let closed = false;
|
|
1172
|
+
const deliver = (value) => {
|
|
1173
|
+
const waiter = waiters.shift();
|
|
1174
|
+
if (waiter) waiter(value);
|
|
1175
|
+
else items.push(value);
|
|
1176
|
+
};
|
|
1177
|
+
return {
|
|
1178
|
+
push(event) {
|
|
1179
|
+
if (closed) return;
|
|
1180
|
+
deliver(event);
|
|
1409
1181
|
},
|
|
1410
|
-
|
|
1411
|
-
|
|
1412
|
-
|
|
1413
|
-
|
|
1414
|
-
}
|
|
1415
|
-
|
|
1416
|
-
|
|
1417
|
-
|
|
1418
|
-
|
|
1419
|
-
}
|
|
1182
|
+
close() {
|
|
1183
|
+
if (closed) return;
|
|
1184
|
+
closed = true;
|
|
1185
|
+
deliver(PROGRESS_CLOSED);
|
|
1186
|
+
},
|
|
1187
|
+
pull() {
|
|
1188
|
+
if (items.length > 0) return Promise.resolve(items.shift());
|
|
1189
|
+
if (closed) return Promise.resolve(PROGRESS_CLOSED);
|
|
1190
|
+
return new Promise((resolve) => waiters.push(resolve));
|
|
1191
|
+
}
|
|
1420
1192
|
};
|
|
1421
1193
|
}
|
|
1422
|
-
function
|
|
1423
|
-
const
|
|
1424
|
-
|
|
1425
|
-
|
|
1426
|
-
|
|
1427
|
-
|
|
1428
|
-
|
|
1429
|
-
|
|
1430
|
-
|
|
1431
|
-
|
|
1432
|
-
|
|
1433
|
-
|
|
1434
|
-
const
|
|
1435
|
-
|
|
1436
|
-
|
|
1437
|
-
|
|
1438
|
-
|
|
1439
|
-
|
|
1440
|
-
|
|
1441
|
-
|
|
1442
|
-
for (const part of updates) {
|
|
1443
|
-
const toolId = asString(part.id);
|
|
1444
|
-
if (!toolId || settledTools.has(toolId)) continue;
|
|
1445
|
-
settledTools.add(toolId);
|
|
1446
|
-
const state = asRecord(part.state);
|
|
1447
|
-
yield {
|
|
1448
|
-
type: "tool_result",
|
|
1449
|
-
toolCallId: toolId,
|
|
1450
|
-
toolName: asString(part.tool) ?? "tool",
|
|
1451
|
-
outcome: {
|
|
1452
|
-
ok: false,
|
|
1453
|
-
...asString(state?.error) ? { message: asString(state?.error) } : {}
|
|
1454
|
-
}
|
|
1455
|
-
};
|
|
1194
|
+
function createModelAttemptDrainer(attemptState, options, openTimeoutMs, firstResponseTimeoutMs, committing, liveLifecycle, progress) {
|
|
1195
|
+
const drainOnce = async (model) => {
|
|
1196
|
+
attemptState.index += 1;
|
|
1197
|
+
const controller = new AbortController();
|
|
1198
|
+
const abort = () => {
|
|
1199
|
+
if (!controller.signal.aborted) controller.abort();
|
|
1200
|
+
};
|
|
1201
|
+
const opening = Promise.resolve().then(() => options.open({
|
|
1202
|
+
model,
|
|
1203
|
+
attempt: attemptState.index,
|
|
1204
|
+
signal: controller.signal
|
|
1205
|
+
}));
|
|
1206
|
+
const openDeadline = deadlineAfter(openTimeoutMs);
|
|
1207
|
+
let opened;
|
|
1208
|
+
try {
|
|
1209
|
+
opened = await Promise.race([opening, openDeadline.promise]);
|
|
1210
|
+
} catch (err) {
|
|
1211
|
+
openDeadline.clear();
|
|
1212
|
+
abort();
|
|
1213
|
+
throw err;
|
|
1456
1214
|
}
|
|
1457
|
-
|
|
1458
|
-
|
|
1459
|
-
|
|
1460
|
-
|
|
1461
|
-
|
|
1462
|
-
|
|
1463
|
-
|
|
1464
|
-
|
|
1465
|
-
|
|
1466
|
-
|
|
1467
|
-
|
|
1468
|
-
|
|
1215
|
+
if (opened === TIMED_OUT) {
|
|
1216
|
+
openDeadline.clear();
|
|
1217
|
+
abort();
|
|
1218
|
+
void opening.then(
|
|
1219
|
+
(lateSource) => {
|
|
1220
|
+
try {
|
|
1221
|
+
void closeIterator(lateSource[Symbol.asyncIterator](), options.log);
|
|
1222
|
+
} catch (err) {
|
|
1223
|
+
options.log?.("[chat-routes] closing a late model stream threw", {
|
|
1224
|
+
error: err instanceof Error ? err.message : String(err)
|
|
1225
|
+
});
|
|
1226
|
+
}
|
|
1227
|
+
},
|
|
1228
|
+
(err) => {
|
|
1229
|
+
options.log?.("[chat-routes] abandoned model stream open rejected after timeout", {
|
|
1230
|
+
error: err instanceof Error ? err.message : String(err)
|
|
1231
|
+
});
|
|
1469
1232
|
}
|
|
1233
|
+
);
|
|
1234
|
+
return {
|
|
1235
|
+
outcome: {
|
|
1236
|
+
committed: false,
|
|
1237
|
+
error: `Opening the model event source for ${model} exceeded ${openTimeoutMs}ms`,
|
|
1238
|
+
errorCode: "model_stream_open_timeout",
|
|
1239
|
+
timeoutCode: "model_stream_open_timeout"
|
|
1240
|
+
},
|
|
1241
|
+
empty: false
|
|
1470
1242
|
};
|
|
1471
1243
|
}
|
|
1472
|
-
|
|
1473
|
-
|
|
1474
|
-
|
|
1475
|
-
|
|
1476
|
-
|
|
1477
|
-
|
|
1478
|
-
|
|
1244
|
+
const source = opened;
|
|
1245
|
+
let iterator;
|
|
1246
|
+
try {
|
|
1247
|
+
iterator = source[Symbol.asyncIterator]();
|
|
1248
|
+
} catch (err) {
|
|
1249
|
+
openDeadline.clear();
|
|
1250
|
+
abort();
|
|
1251
|
+
throw err;
|
|
1479
1252
|
}
|
|
1480
|
-
|
|
1481
|
-
|
|
1482
|
-
|
|
1483
|
-
|
|
1484
|
-
|
|
1485
|
-
|
|
1486
|
-
|
|
1487
|
-
|
|
1488
|
-
|
|
1489
|
-
|
|
1490
|
-
|
|
1491
|
-
|
|
1492
|
-
if (time) timed.set(key, { ...part, time });
|
|
1253
|
+
const buffered = [];
|
|
1254
|
+
let next;
|
|
1255
|
+
try {
|
|
1256
|
+
next = await Promise.race([
|
|
1257
|
+
Promise.resolve(iterator.next()),
|
|
1258
|
+
openDeadline.promise
|
|
1259
|
+
]);
|
|
1260
|
+
} catch (err) {
|
|
1261
|
+
openDeadline.clear();
|
|
1262
|
+
abort();
|
|
1263
|
+
void closeIterator(iterator, options.log);
|
|
1264
|
+
throw err;
|
|
1493
1265
|
}
|
|
1494
|
-
|
|
1495
|
-
|
|
1496
|
-
|
|
1497
|
-
|
|
1498
|
-
|
|
1499
|
-
|
|
1500
|
-
|
|
1501
|
-
|
|
1502
|
-
|
|
1503
|
-
|
|
1504
|
-
|
|
1505
|
-
|
|
1506
|
-
|
|
1507
|
-
recordPersistedPart(part, delta || void 0);
|
|
1508
|
-
if (delta) {
|
|
1509
|
-
if (partType === "reasoning") reasoningClock.reasoning(key);
|
|
1510
|
-
else reasoningClock.settle();
|
|
1511
|
-
if (partType === "text") projectionState.fullText += delta;
|
|
1512
|
-
yield { type: partType, text: delta };
|
|
1513
|
-
}
|
|
1514
|
-
return;
|
|
1266
|
+
openDeadline.clear();
|
|
1267
|
+
if (next === TIMED_OUT) {
|
|
1268
|
+
abort();
|
|
1269
|
+
void closeIterator(iterator, options.log);
|
|
1270
|
+
return {
|
|
1271
|
+
outcome: {
|
|
1272
|
+
committed: false,
|
|
1273
|
+
error: `Starting the model event source for ${model} exceeded ${openTimeoutMs}ms`,
|
|
1274
|
+
errorCode: "model_stream_open_timeout",
|
|
1275
|
+
timeoutCode: "model_stream_open_timeout"
|
|
1276
|
+
},
|
|
1277
|
+
empty: false
|
|
1278
|
+
};
|
|
1515
1279
|
}
|
|
1516
|
-
if (
|
|
1517
|
-
|
|
1518
|
-
|
|
1519
|
-
|
|
1520
|
-
|
|
1521
|
-
|
|
1522
|
-
const
|
|
1523
|
-
|
|
1524
|
-
|
|
1525
|
-
|
|
1526
|
-
|
|
1527
|
-
|
|
1528
|
-
|
|
1529
|
-
|
|
1530
|
-
|
|
1280
|
+
if (next.done) {
|
|
1281
|
+
abort();
|
|
1282
|
+
return { outcome: { committed: true, buffered, iterator: null, abort }, empty: true };
|
|
1283
|
+
}
|
|
1284
|
+
const firstResponseDeadline = deadlineAfter(firstResponseTimeoutMs);
|
|
1285
|
+
for (; ; ) {
|
|
1286
|
+
const event = next.value;
|
|
1287
|
+
let failure;
|
|
1288
|
+
let commits = false;
|
|
1289
|
+
try {
|
|
1290
|
+
failure = classifyTerminalFailure(event);
|
|
1291
|
+
commits = !failure && committing(event);
|
|
1292
|
+
} catch (err) {
|
|
1293
|
+
firstResponseDeadline.clear();
|
|
1294
|
+
abort();
|
|
1295
|
+
void closeIterator(iterator, options.log);
|
|
1296
|
+
throw err;
|
|
1297
|
+
}
|
|
1298
|
+
if (failure?.outage) {
|
|
1299
|
+
firstResponseDeadline.clear();
|
|
1300
|
+
abort();
|
|
1301
|
+
void closeIterator(iterator, options.log);
|
|
1302
|
+
return {
|
|
1303
|
+
outcome: {
|
|
1304
|
+
committed: false,
|
|
1305
|
+
error: failure.reason,
|
|
1306
|
+
...failure.code ? { errorCode: failure.code } : {}
|
|
1307
|
+
},
|
|
1308
|
+
empty: false
|
|
1531
1309
|
};
|
|
1532
1310
|
}
|
|
1533
|
-
|
|
1534
|
-
|
|
1535
|
-
|
|
1536
|
-
|
|
1537
|
-
|
|
1538
|
-
|
|
1539
|
-
|
|
1311
|
+
if (liveLifecycle && !failure && !commits && isLiveLifecycleEvent(event)) {
|
|
1312
|
+
progress.push(event);
|
|
1313
|
+
} else {
|
|
1314
|
+
buffered.push(event);
|
|
1315
|
+
}
|
|
1316
|
+
if (failure) {
|
|
1317
|
+
firstResponseDeadline.clear();
|
|
1318
|
+
return { outcome: { committed: true, buffered, iterator, abort }, empty: false };
|
|
1319
|
+
}
|
|
1320
|
+
if (commits) {
|
|
1321
|
+
firstResponseDeadline.clear();
|
|
1322
|
+
return { outcome: { committed: true, buffered, iterator, abort }, empty: isEmptyTerminalReceipt(event) };
|
|
1323
|
+
}
|
|
1324
|
+
try {
|
|
1325
|
+
next = await Promise.race([
|
|
1326
|
+
Promise.resolve(iterator.next()),
|
|
1327
|
+
firstResponseDeadline.promise
|
|
1328
|
+
]);
|
|
1329
|
+
} catch (err) {
|
|
1330
|
+
firstResponseDeadline.clear();
|
|
1331
|
+
abort();
|
|
1332
|
+
void closeIterator(iterator, options.log);
|
|
1333
|
+
throw err;
|
|
1334
|
+
}
|
|
1335
|
+
if (next === TIMED_OUT) {
|
|
1336
|
+
firstResponseDeadline.clear();
|
|
1337
|
+
abort();
|
|
1338
|
+
void closeIterator(iterator, options.log);
|
|
1339
|
+
return {
|
|
1540
1340
|
outcome: {
|
|
1541
|
-
|
|
1542
|
-
|
|
1543
|
-
|
|
1544
|
-
|
|
1341
|
+
committed: false,
|
|
1342
|
+
error: `Model ${model} produced no first answer-bearing event within ${firstResponseTimeoutMs}ms`,
|
|
1343
|
+
errorCode: "provider_first_response_timeout",
|
|
1344
|
+
timeoutCode: "provider_first_response_timeout"
|
|
1345
|
+
},
|
|
1346
|
+
empty: false
|
|
1545
1347
|
};
|
|
1546
1348
|
}
|
|
1547
|
-
|
|
1548
|
-
|
|
1549
|
-
|
|
1550
|
-
|
|
1551
|
-
recordPersistedPart(part, void 0, `step-finish:#${projectionState.stepCounter++}`);
|
|
1552
|
-
const promptTokens = usage.inputTokens ?? 0;
|
|
1553
|
-
const completionTokens = usage.outputTokens ?? 0;
|
|
1554
|
-
if (promptTokens || completionTokens) {
|
|
1555
|
-
yield {
|
|
1556
|
-
type: "usage",
|
|
1557
|
-
usage: {
|
|
1558
|
-
promptTokens,
|
|
1559
|
-
completionTokens,
|
|
1560
|
-
...usage.reasoningTokens !== void 0 ? { reasoningTokens: usage.reasoningTokens } : {},
|
|
1561
|
-
...usage.costUsd !== void 0 ? { providerCostUsd: usage.costUsd } : {}
|
|
1562
|
-
}
|
|
1563
|
-
};
|
|
1349
|
+
if (next.done) {
|
|
1350
|
+
firstResponseDeadline.clear();
|
|
1351
|
+
abort();
|
|
1352
|
+
return { outcome: { committed: true, buffered, iterator: null, abort }, empty: true };
|
|
1564
1353
|
}
|
|
1565
|
-
return;
|
|
1566
|
-
}
|
|
1567
|
-
if (partType === "step-start") {
|
|
1568
|
-
recordPersistedPart(part, void 0, `step-start:#${projectionState.stepCounter}`);
|
|
1569
|
-
return;
|
|
1570
1354
|
}
|
|
1571
|
-
|
|
1572
|
-
|
|
1573
|
-
|
|
1574
|
-
|
|
1575
|
-
|
|
1576
|
-
|
|
1577
|
-
|
|
1578
|
-
|
|
1579
|
-
|
|
1580
|
-
|
|
1581
|
-
|
|
1582
|
-
|
|
1583
|
-
|
|
1584
|
-
|
|
1585
|
-
|
|
1586
|
-
|
|
1587
|
-
|
|
1588
|
-
|
|
1589
|
-
|
|
1590
|
-
|
|
1591
|
-
|
|
1592
|
-
|
|
1593
|
-
|
|
1594
|
-
|
|
1355
|
+
};
|
|
1356
|
+
return { drainOnce };
|
|
1357
|
+
}
|
|
1358
|
+
|
|
1359
|
+
// src/chat-routes/sandbox-model-source.ts
|
|
1360
|
+
function parseEffectiveBackend(data) {
|
|
1361
|
+
const backend = asRecord(data.effectiveBackend);
|
|
1362
|
+
if (!backend) return void 0;
|
|
1363
|
+
const provider = asString(backend.provider);
|
|
1364
|
+
const model = asString(backend.model);
|
|
1365
|
+
const rawSource = asString(backend.source);
|
|
1366
|
+
const source = rawSource === "request" || rawSource === "environment" || rawSource === "profile" ? rawSource : void 0;
|
|
1367
|
+
if (!provider && !model && !source) return void 0;
|
|
1368
|
+
return {
|
|
1369
|
+
...model ? { servedModel: model } : {},
|
|
1370
|
+
...provider ? { servedProvider: provider } : {},
|
|
1371
|
+
...source ? { servedSource: source } : {}
|
|
1372
|
+
};
|
|
1373
|
+
}
|
|
1374
|
+
function createSandboxModelSource(options, log) {
|
|
1375
|
+
if (options.openEvents && !options.model) {
|
|
1376
|
+
throw new Error(
|
|
1377
|
+
"createSandboxChatProducer: `openEvents` requires `model` \u2014 failover must know which model it is running"
|
|
1378
|
+
);
|
|
1379
|
+
}
|
|
1380
|
+
if (!options.openEvents && !options.events) {
|
|
1381
|
+
throw new Error("createSandboxChatProducer: pass `openEvents` (failover-capable) or `events`");
|
|
1382
|
+
}
|
|
1383
|
+
if (!options.openEvents && resolveEmptyTurnRetries(options.emptyTurnRetries) > 0) {
|
|
1384
|
+
throw new Error(
|
|
1385
|
+
"createSandboxChatProducer: `emptyTurnRetries` requires `openEvents` \u2014 a fixed `events` stream cannot be re-opened"
|
|
1386
|
+
);
|
|
1387
|
+
}
|
|
1388
|
+
const chain = options.model ? buildModelChain(options.model, options.modelFailover === false ? [] : options.fallbackModels ?? []) : [];
|
|
1389
|
+
const pendingModelNotices = [];
|
|
1390
|
+
let modelNoticeCount = 0;
|
|
1391
|
+
let openAttempts = 0;
|
|
1392
|
+
let failover;
|
|
1393
|
+
let source;
|
|
1394
|
+
if (options.openEvents) {
|
|
1395
|
+
const open = options.openEvents;
|
|
1396
|
+
failover = streamWithModelFailover({
|
|
1397
|
+
models: chain,
|
|
1398
|
+
open: (args) => {
|
|
1399
|
+
openAttempts += 1;
|
|
1400
|
+
return open(args);
|
|
1401
|
+
},
|
|
1402
|
+
log,
|
|
1403
|
+
...options.modelPolicy ? { policy: options.modelPolicy } : {},
|
|
1404
|
+
...options.openTimeoutMs !== void 0 ? { openTimeoutMs: options.openTimeoutMs } : {},
|
|
1405
|
+
...options.firstResponseTimeoutMs !== void 0 ? { firstResponseTimeoutMs: options.firstResponseTimeoutMs } : {},
|
|
1406
|
+
...options.emptyTurnRetries !== void 0 ? { emptyTurnRetries: options.emptyTurnRetries } : {},
|
|
1407
|
+
...options.onEmptyTurnRetry ? { onEmptyTurnRetry: options.onEmptyTurnRetry } : {},
|
|
1408
|
+
...options.liveLifecycleEvents !== void 0 ? { liveLifecycleEvents: options.liveLifecycleEvents } : {},
|
|
1409
|
+
onFallback: (info) => {
|
|
1410
|
+
modelNoticeCount += 1;
|
|
1411
|
+
pendingModelNotices.push({
|
|
1412
|
+
id: `model-fallback-${modelNoticeCount}`,
|
|
1413
|
+
// Named models on both sides: a quality regression after a downgrade
|
|
1414
|
+
// must be attributable to the model that actually answered. The
|
|
1415
|
+
// REASON is condensed — a real edge 5xx arrives as Cloudflare's HTML
|
|
1416
|
+
// error page, and the raw text put `<!DOCTYPE html><!--[if lt IE 7]>…`
|
|
1417
|
+
// into the customer's transcript. The verbatim text is still on
|
|
1418
|
+
// `modelFailover.attempts[].reason` for the operator.
|
|
1419
|
+
text: `${info.from} was unavailable (${summarizeFailoverReason(info.reason)}) \u2014 answered with ${info.to} instead.`
|
|
1420
|
+
});
|
|
1421
|
+
options.onModelFallback?.(info);
|
|
1595
1422
|
}
|
|
1596
|
-
|
|
1597
|
-
|
|
1598
|
-
|
|
1599
|
-
|
|
1423
|
+
});
|
|
1424
|
+
source = failover.events;
|
|
1425
|
+
} else {
|
|
1426
|
+
source = options.events;
|
|
1427
|
+
}
|
|
1428
|
+
const servingModel = () => failover?.servingModel() ?? options.model;
|
|
1429
|
+
const modelState = { effectiveBackend: void 0 };
|
|
1430
|
+
let substitutionNoticeQueued = false;
|
|
1431
|
+
const servedModel = () => modelState.effectiveBackend?.servedModel ?? servingModel();
|
|
1432
|
+
const captureEffectiveBackend = (data) => {
|
|
1433
|
+
if (!data) return;
|
|
1434
|
+
const parsed = parseEffectiveBackend(data);
|
|
1435
|
+
if (!parsed) return;
|
|
1436
|
+
modelState.effectiveBackend = parsed;
|
|
1437
|
+
const sentModel = servingModel();
|
|
1438
|
+
if (substitutionNoticeQueued || !options.model || !sentModel || !parsed.servedModel || sameModel(parsed.servedModel, sentModel)) return;
|
|
1439
|
+
substitutionNoticeQueued = true;
|
|
1440
|
+
pendingModelNotices.push({
|
|
1441
|
+
id: "model-substitution-1",
|
|
1442
|
+
text: `Requested ${sentModel} \u2014 the sandbox answered with ${parsed.servedModel} instead.`
|
|
1443
|
+
});
|
|
1444
|
+
};
|
|
1445
|
+
return { pendingModelNotices, source, captureEffectiveBackend, servedModel, servingModel, failover, modelState, openAttempts: () => openAttempts };
|
|
1446
|
+
}
|
|
1447
|
+
function sameModel(a, b) {
|
|
1448
|
+
const bare = (id) => id.slice(id.lastIndexOf("/") + 1).trim().toLowerCase();
|
|
1449
|
+
return bare(a) === bare(b);
|
|
1450
|
+
}
|
|
1451
|
+
|
|
1452
|
+
// src/chat-routes/sandbox-producer.ts
|
|
1453
|
+
function textDelta(tracker, key, part, rawDelta) {
|
|
1454
|
+
const explicit = typeof rawDelta === "string" ? rawDelta : void 0;
|
|
1455
|
+
const previous = tracker.seen.get(key) ?? "";
|
|
1456
|
+
if (explicit !== void 0) {
|
|
1457
|
+
tracker.seen.set(key, previous + explicit);
|
|
1458
|
+
return explicit;
|
|
1459
|
+
}
|
|
1460
|
+
const snapshot = asString(part.text) ?? asString(part.content) ?? "";
|
|
1461
|
+
if (!snapshot) return "";
|
|
1462
|
+
if (snapshot.startsWith(previous)) {
|
|
1463
|
+
tracker.seen.set(key, snapshot);
|
|
1464
|
+
return snapshot.slice(previous.length);
|
|
1465
|
+
}
|
|
1466
|
+
tracker.seen.set(key, snapshot);
|
|
1467
|
+
return snapshot;
|
|
1468
|
+
}
|
|
1469
|
+
function toFiniteNumber(value) {
|
|
1470
|
+
if (typeof value === "number") return Number.isFinite(value) ? value : null;
|
|
1471
|
+
if (typeof value === "string" && value.trim() !== "") {
|
|
1472
|
+
const parsed = Number(value);
|
|
1473
|
+
return Number.isFinite(parsed) ? parsed : null;
|
|
1474
|
+
}
|
|
1475
|
+
return null;
|
|
1476
|
+
}
|
|
1477
|
+
function extractReportedTurnUsage(data) {
|
|
1478
|
+
const tokenUsage = asRecord(data?.tokenUsage);
|
|
1479
|
+
if (!tokenUsage) return null;
|
|
1480
|
+
const inputTokens = toFiniteNumber(tokenUsage.inputTokens);
|
|
1481
|
+
const outputTokens = toFiniteNumber(tokenUsage.outputTokens);
|
|
1482
|
+
if (inputTokens === null || outputTokens === null) return null;
|
|
1483
|
+
const reported = { inputTokens, outputTokens };
|
|
1484
|
+
const reasoningTokens = toFiniteNumber(tokenUsage.reasoningTokens);
|
|
1485
|
+
if (reasoningTokens !== null) reported.reasoningTokens = reasoningTokens;
|
|
1486
|
+
const cacheReadTokens = toFiniteNumber(tokenUsage.cacheReadInputTokens);
|
|
1487
|
+
if (cacheReadTokens !== null) reported.cacheReadTokens = cacheReadTokens;
|
|
1488
|
+
const cacheWriteTokens = toFiniteNumber(tokenUsage.cacheCreationInputTokens);
|
|
1489
|
+
if (cacheWriteTokens !== null) reported.cacheWriteTokens = cacheWriteTokens;
|
|
1490
|
+
const costUsd = toFiniteNumber(data?.totalCostUsd) ?? toFiniteNumber(tokenUsage.cost);
|
|
1491
|
+
if (costUsd !== null) reported.costUsd = costUsd;
|
|
1492
|
+
return reported;
|
|
1493
|
+
}
|
|
1494
|
+
function extractUsageSnapshot(value) {
|
|
1495
|
+
const inputTokens = toFiniteNumber(value?.promptTokens);
|
|
1496
|
+
const outputTokens = toFiniteNumber(value?.completionTokens);
|
|
1497
|
+
if (inputTokens === null || outputTokens === null || inputTokens < 0 || outputTokens < 0) return null;
|
|
1498
|
+
const reported = { inputTokens, outputTokens };
|
|
1499
|
+
const reasoningTokens = toFiniteNumber(value?.reasoningTokens);
|
|
1500
|
+
if (reasoningTokens !== null && reasoningTokens >= 0) reported.reasoningTokens = reasoningTokens;
|
|
1501
|
+
const costUsd = toFiniteNumber(value?.providerCostUsd);
|
|
1502
|
+
if (costUsd !== null && costUsd >= 0) reported.costUsd = costUsd;
|
|
1503
|
+
return reported;
|
|
1504
|
+
}
|
|
1505
|
+
function applyTerminalUsage(reported, usage) {
|
|
1506
|
+
usage.inputTokens = reported.inputTokens;
|
|
1507
|
+
usage.outputTokens = reported.outputTokens;
|
|
1508
|
+
if (reported.reasoningTokens !== void 0) usage.reasoningTokens = reported.reasoningTokens;
|
|
1509
|
+
if (reported.cacheReadTokens !== void 0) usage.cacheReadTokens = reported.cacheReadTokens;
|
|
1510
|
+
if (reported.cacheWriteTokens !== void 0) usage.cacheWriteTokens = reported.cacheWriteTokens;
|
|
1511
|
+
if (reported.costUsd !== void 0) usage.costUsd = reported.costUsd;
|
|
1512
|
+
}
|
|
1513
|
+
function sandboxStreamErrorMessage(data) {
|
|
1514
|
+
const record = asRecord(data);
|
|
1515
|
+
const message = asString(record?.message) ?? asString(record?.error);
|
|
1516
|
+
if (message) return message;
|
|
1517
|
+
try {
|
|
1518
|
+
return JSON.stringify(data) || "Sandbox stream returned an error event without details.";
|
|
1519
|
+
} catch {
|
|
1520
|
+
return "Sandbox stream returned an error event without details.";
|
|
1600
1521
|
}
|
|
1601
1522
|
}
|
|
1602
|
-
|
|
1603
|
-
|
|
1604
|
-
|
|
1605
|
-
|
|
1606
|
-
|
|
1607
|
-
}
|
|
1608
|
-
|
|
1609
|
-
|
|
1610
|
-
|
|
1611
|
-
|
|
1612
|
-
|
|
1613
|
-
|
|
1614
|
-
|
|
1615
|
-
|
|
1523
|
+
function sandboxStreamErrorCode(data) {
|
|
1524
|
+
return asString(asRecord(data)?.code) ?? "sandbox.stream_error";
|
|
1525
|
+
}
|
|
1526
|
+
function toProducerWireEvent(event) {
|
|
1527
|
+
return event;
|
|
1528
|
+
}
|
|
1529
|
+
function sandboxStreamFailureDiagnostic(error) {
|
|
1530
|
+
const streamMessage = error?.streamMessage;
|
|
1531
|
+
const message = typeof streamMessage === "string" ? streamMessage : error instanceof Error ? error.message : "Sandbox stream failed";
|
|
1532
|
+
const diagnostics = asRecord(error?.diagnostics);
|
|
1533
|
+
let diagnosticText;
|
|
1534
|
+
if (diagnostics) {
|
|
1535
|
+
try {
|
|
1536
|
+
diagnosticText = JSON.stringify(diagnostics);
|
|
1537
|
+
} catch {
|
|
1538
|
+
diagnosticText = "[unserializable diagnostics]";
|
|
1539
|
+
}
|
|
1616
1540
|
}
|
|
1617
|
-
const
|
|
1618
|
-
|
|
1619
|
-
|
|
1620
|
-
|
|
1621
|
-
|
|
1622
|
-
|
|
1541
|
+
const userMessage = [
|
|
1542
|
+
"The sandbox model stream stopped before a clean completion.",
|
|
1543
|
+
`Error: ${message}`,
|
|
1544
|
+
"Please retry. If it repeats, send these support details to the team."
|
|
1545
|
+
].join("\n\n");
|
|
1546
|
+
const failureNote = diagnosticText ? `sandbox-stream: ${message}; ${diagnosticText}` : `sandbox-stream: ${message}`;
|
|
1547
|
+
let code = "sandbox.stream_failed";
|
|
1548
|
+
if (error instanceof SandboxRunError) {
|
|
1549
|
+
code = error.code;
|
|
1550
|
+
} else if (error instanceof ModelFailoverTimeoutError) {
|
|
1551
|
+
code = error.code;
|
|
1552
|
+
} else if (error instanceof ModelFailoverExhaustedError) {
|
|
1553
|
+
code = error.upstreamCode ?? code;
|
|
1623
1554
|
}
|
|
1624
|
-
|
|
1625
|
-
|
|
1626
|
-
|
|
1627
|
-
|
|
1628
|
-
|
|
1555
|
+
return { userMessage, failureNote, code };
|
|
1556
|
+
}
|
|
1557
|
+
function createSandboxChatProducer(options) {
|
|
1558
|
+
const log = options.log ?? ((message, meta) => console.error(message, meta ?? ""));
|
|
1559
|
+
const renderable = options.isRenderableInteraction ?? isRenderableInteractionKind;
|
|
1560
|
+
const { pendingModelNotices, source, captureEffectiveBackend, servedModel, servingModel, failover, modelState, openAttempts } = createSandboxModelSource(options, log);
|
|
1561
|
+
const projectionState = {
|
|
1562
|
+
fullText: "",
|
|
1563
|
+
terminalObserved: false,
|
|
1564
|
+
stepCounter: 0,
|
|
1565
|
+
danglingToolsTerminalized: false,
|
|
1566
|
+
interactionOutcome: "answered",
|
|
1567
|
+
warningCount: 0,
|
|
1568
|
+
errorNoticeCount: 0
|
|
1629
1569
|
};
|
|
1630
|
-
const
|
|
1631
|
-
|
|
1632
|
-
|
|
1633
|
-
|
|
1634
|
-
if (auth.intent !== void 0 && auth.intent !== "cancel") {
|
|
1635
|
-
return Response.json({ error: "Authorization did not allow this turn cancellation.", code: "chat_authorization_intent_mismatch" }, { status: 403 });
|
|
1636
|
-
}
|
|
1637
|
-
const target = await options.cancel.target({
|
|
1638
|
-
request,
|
|
1639
|
-
body,
|
|
1640
|
-
tenantId: auth.tenantId,
|
|
1641
|
-
userId: auth.userId,
|
|
1642
|
-
..."context" in auth ? { context: auth.context } : {}
|
|
1643
|
-
});
|
|
1644
|
-
if (target instanceof Response) return target;
|
|
1645
|
-
if (target.requiresDispatchToken && (!body.cancellationToken || body.cancellationToken !== target.dispatchToken)) {
|
|
1646
|
-
return targetChanged();
|
|
1647
|
-
}
|
|
1648
|
-
if (target.status === "stopped" || target.status === "answered") return success(false);
|
|
1649
|
-
if (target.control) {
|
|
1650
|
-
const intent = await target.control.request(target.capturedMetadata);
|
|
1651
|
-
return intent.requested ? Response.json({ succeeded: true, sandboxInterrupted: false, cancellationRequested: true }) : targetChanged();
|
|
1652
|
-
}
|
|
1653
|
-
const markStopped = async () => await target.markStopped(target.capturedMetadata, (/* @__PURE__ */ new Date()).toISOString()) ? null : stateChanged();
|
|
1654
|
-
if (!target.native) return await markStopped() ?? success(false);
|
|
1655
|
-
const native = target.native;
|
|
1656
|
-
if (native.state !== "running" || !native.executionId || !native.sessionId || !native.sandboxId) {
|
|
1657
|
-
return Response.json({
|
|
1658
|
-
error: "This turn has no confirmed native execution to stop. Refresh and try again.",
|
|
1659
|
-
code: "NATIVE_CANCEL_TARGET_UNAVAILABLE"
|
|
1660
|
-
}, { status: 409 });
|
|
1661
|
-
}
|
|
1662
|
-
const source = await native.source();
|
|
1663
|
-
if (!source || source.id !== native.sandboxId) {
|
|
1664
|
-
return Response.json({ error: "The turn was not marked stopped because its original sandbox is unavailable." }, { status: 503 });
|
|
1665
|
-
}
|
|
1570
|
+
const { timedParts, drainModelNotices, tracker, recordPersistedPart, reasoningClock, partMap, announcedToolArgs, settledTools, usage, promotedFileParts, emitFinalUsage, emitTerminalizedTools, emitErrorNotice, partOrder } = createSandboxProjection(projectionState, pendingModelNotices);
|
|
1571
|
+
let observedSourceEvent = false;
|
|
1572
|
+
let preflightRefused = false;
|
|
1573
|
+
async function* stream() {
|
|
1666
1574
|
try {
|
|
1667
|
-
const
|
|
1668
|
-
|
|
1669
|
-
|
|
1670
|
-
|
|
1671
|
-
|
|
1672
|
-
|
|
1673
|
-
|
|
1674
|
-
|
|
1575
|
+
for await (const raw of source) {
|
|
1576
|
+
observedSourceEvent = true;
|
|
1577
|
+
const record = asRecord(raw);
|
|
1578
|
+
captureEffectiveBackend(asRecord(record?.data));
|
|
1579
|
+
yield* drainModelNotices();
|
|
1580
|
+
if (!record || typeof record.type !== "string") continue;
|
|
1581
|
+
const normalized = normalizeToolEvent({ type: record.type, data: asRecord(record.data) });
|
|
1582
|
+
const event = normalized.type === "message.part.updated" ? normalized : { type: record.type, data: asRecord(record.data) };
|
|
1583
|
+
if (event.type === "message.part.updated") {
|
|
1584
|
+
yield* projectSandboxMessagePart(event, tracker, recordPersistedPart, reasoningClock, projectionState, partMap, announcedToolArgs, settledTools, usage, options, log, promotedFileParts);
|
|
1585
|
+
continue;
|
|
1586
|
+
}
|
|
1587
|
+
if (event.type === "interaction") {
|
|
1588
|
+
const parsed = parseInteractionRequest(asRecord(record.data));
|
|
1589
|
+
if (!parsed.succeeded) {
|
|
1590
|
+
log("[chat-routes] dropping malformed interaction event", { error: parsed.error });
|
|
1591
|
+
continue;
|
|
1592
|
+
}
|
|
1593
|
+
if (renderable(parsed.value.kind, parsed.value)) {
|
|
1594
|
+
recordPersistedPart(
|
|
1595
|
+
interactionToPersistedPart(parsed.value, "pending"),
|
|
1596
|
+
void 0,
|
|
1597
|
+
interactionPartKey(parsed.value.id)
|
|
1598
|
+
);
|
|
1599
|
+
yield toProducerWireEvent(record);
|
|
1600
|
+
continue;
|
|
1601
|
+
}
|
|
1602
|
+
let declineFailed = false;
|
|
1603
|
+
if (options.declineInteraction) {
|
|
1604
|
+
try {
|
|
1605
|
+
await options.declineInteraction(parsed.value.id);
|
|
1606
|
+
} catch (err) {
|
|
1607
|
+
declineFailed = true;
|
|
1608
|
+
log("[chat-routes] failed to auto-decline interaction", {
|
|
1609
|
+
id: parsed.value.id,
|
|
1610
|
+
error: err instanceof Error ? err.message : String(err)
|
|
1611
|
+
});
|
|
1612
|
+
}
|
|
1613
|
+
} else {
|
|
1614
|
+
declineFailed = true;
|
|
1615
|
+
log("[chat-routes] non-renderable interaction with no declineInteraction wired", {
|
|
1616
|
+
id: parsed.value.id,
|
|
1617
|
+
kind: parsed.value.kind
|
|
1618
|
+
});
|
|
1619
|
+
}
|
|
1620
|
+
const text = options.declinedNotice?.(parsed.value, declineFailed) ?? (declineFailed ? `The agent requested ${parsed.value.kind} approval; declining it failed \u2014 it will expire on its own.` : `The agent requested ${parsed.value.kind} approval \u2014 auto-declined by policy.`);
|
|
1621
|
+
const notice = noticePart("auto-declined", `auto-declined-${parsed.value.id}`, text);
|
|
1622
|
+
recordPersistedPart(notice, void 0, noticePartKey(notice.id));
|
|
1623
|
+
yield { type: "notice", id: notice.id, noticeKind: "auto-declined", text };
|
|
1624
|
+
continue;
|
|
1625
|
+
}
|
|
1626
|
+
if (event.type === "interaction.cancel") {
|
|
1627
|
+
const parsed = parseInteractionCancel(asRecord(record.data));
|
|
1628
|
+
if (!parsed.succeeded) {
|
|
1629
|
+
log("[chat-routes] dropping malformed interaction.cancel event", { error: parsed.error });
|
|
1630
|
+
continue;
|
|
1631
|
+
}
|
|
1632
|
+
const key = interactionPartKey(parsed.value.id);
|
|
1633
|
+
const existing = partMap.get(key);
|
|
1634
|
+
if (existing?.type === "interaction" && existing.status === "pending") {
|
|
1635
|
+
recordPersistedPart({
|
|
1636
|
+
...existing,
|
|
1637
|
+
status: cancelStatusFor(parsed.value.reason),
|
|
1638
|
+
...parsed.value.reason ? { cancelReason: parsed.value.reason } : {}
|
|
1639
|
+
}, void 0, key);
|
|
1640
|
+
}
|
|
1641
|
+
yield toProducerWireEvent(record);
|
|
1642
|
+
continue;
|
|
1643
|
+
}
|
|
1644
|
+
if (event.type === "plan.submitted") {
|
|
1645
|
+
const parsed = parsePlanSubmittedEvent(record);
|
|
1646
|
+
if (!parsed.succeeded) {
|
|
1647
|
+
log("[chat-routes] dropping malformed plan.submitted event", { error: parsed.error });
|
|
1648
|
+
continue;
|
|
1649
|
+
}
|
|
1650
|
+
recordPersistedPart(planToPersistedPart(parsed.value), void 0);
|
|
1651
|
+
yield toProducerWireEvent(record);
|
|
1652
|
+
continue;
|
|
1653
|
+
}
|
|
1654
|
+
if (event.type === "warning") {
|
|
1655
|
+
const message = asString(event.data?.message);
|
|
1656
|
+
if (message) {
|
|
1657
|
+
projectionState.warningCount += 1;
|
|
1658
|
+
const code = asString(event.data?.code);
|
|
1659
|
+
const text = code ? `${code}: ${message}` : message;
|
|
1660
|
+
const notice = noticePart("warning", `warning-${projectionState.warningCount}`, text);
|
|
1661
|
+
recordPersistedPart(notice, void 0, noticePartKey(notice.id));
|
|
1662
|
+
yield { type: "notice", id: notice.id, noticeKind: "warning", text };
|
|
1663
|
+
}
|
|
1664
|
+
yield toProducerWireEvent(record);
|
|
1665
|
+
continue;
|
|
1666
|
+
}
|
|
1667
|
+
if (event.type === "usage") {
|
|
1668
|
+
const reported = extractUsageSnapshot(asRecord(record.usage));
|
|
1669
|
+
if (reported) applyTerminalUsage(reported, usage);
|
|
1670
|
+
yield toProducerWireEvent(record);
|
|
1671
|
+
continue;
|
|
1672
|
+
}
|
|
1673
|
+
if (event.type === "result") {
|
|
1674
|
+
projectionState.terminalObserved = true;
|
|
1675
|
+
const finalText = asString(event.data?.finalText);
|
|
1676
|
+
if (finalText) projectionState.fullText = finalText;
|
|
1677
|
+
const reported = extractReportedTurnUsage(asRecord(event.data));
|
|
1678
|
+
if (reported) {
|
|
1679
|
+
applyTerminalUsage(reported, usage);
|
|
1680
|
+
yield* emitFinalUsage();
|
|
1681
|
+
} else {
|
|
1682
|
+
const resultUsage = asRecord(event.data?.usage);
|
|
1683
|
+
if (resultUsage) {
|
|
1684
|
+
const input = Number(resultUsage.inputTokens);
|
|
1685
|
+
const output = Number(resultUsage.outputTokens);
|
|
1686
|
+
if (Number.isFinite(input)) usage.inputTokens = input;
|
|
1687
|
+
if (Number.isFinite(output)) usage.outputTokens = output;
|
|
1688
|
+
}
|
|
1689
|
+
}
|
|
1690
|
+
yield* emitTerminalizedTools();
|
|
1691
|
+
continue;
|
|
1692
|
+
}
|
|
1693
|
+
if (event.type === "done") {
|
|
1694
|
+
if (isTerminalPromptEvent(record)) projectionState.terminalObserved = true;
|
|
1695
|
+
const reported = extractReportedTurnUsage(asRecord(event.data));
|
|
1696
|
+
if (reported) {
|
|
1697
|
+
applyTerminalUsage(reported, usage);
|
|
1698
|
+
yield* emitFinalUsage();
|
|
1699
|
+
}
|
|
1700
|
+
yield* emitTerminalizedTools();
|
|
1701
|
+
yield toProducerWireEvent(record);
|
|
1702
|
+
continue;
|
|
1703
|
+
}
|
|
1704
|
+
if (event.type === "error") {
|
|
1705
|
+
const message = sandboxStreamErrorMessage(event.data);
|
|
1706
|
+
const errorContent = projectionState.fullText.trim() ? `The sandbox model stream stopped before a clean completion.
|
|
1707
|
+
|
|
1708
|
+
Error: ${message}` : `The sandbox agent returned an error before producing a visible answer.
|
|
1709
|
+
|
|
1710
|
+
Error: ${message}`;
|
|
1711
|
+
if (options.errorNotice) {
|
|
1712
|
+
yield* emitErrorNotice(errorContent, sandboxStreamErrorCode(event.data));
|
|
1713
|
+
} else if (options.includeErrorText !== false) {
|
|
1714
|
+
const errorDelta = projectionState.fullText ? `
|
|
1715
|
+
|
|
1716
|
+
---
|
|
1717
|
+
${errorContent}` : errorContent;
|
|
1718
|
+
projectionState.fullText += errorDelta;
|
|
1719
|
+
yield { type: "text", text: errorDelta };
|
|
1720
|
+
}
|
|
1721
|
+
projectionState.interactionOutcome = "expired";
|
|
1722
|
+
yield* emitTerminalizedTools();
|
|
1723
|
+
yield toProducerWireEvent(record);
|
|
1724
|
+
continue;
|
|
1675
1725
|
}
|
|
1726
|
+
yield toProducerWireEvent(record);
|
|
1676
1727
|
}
|
|
1677
|
-
|
|
1678
|
-
} catch (
|
|
1679
|
-
|
|
1680
|
-
|
|
1681
|
-
|
|
1682
|
-
|
|
1683
|
-
|
|
1684
|
-
log("[chat-routes] cancellation failed", { error: redactErrorMessage(error) });
|
|
1685
|
-
return Response.json({ error: "Turn cancellation is temporarily unavailable." }, { status: 503 });
|
|
1686
|
-
}
|
|
1687
|
-
}
|
|
1688
|
-
async function* observeTurnCancellation(source, args, cancel, abort) {
|
|
1689
|
-
if (!args.userMessageId) {
|
|
1690
|
-
yield* source;
|
|
1691
|
-
return;
|
|
1692
|
-
}
|
|
1693
|
-
const iterator = source[Symbol.asyncIterator]();
|
|
1694
|
-
let closed = false, stopped = false, drained = false, warned = false;
|
|
1695
|
-
let polling;
|
|
1696
|
-
let notifyStop;
|
|
1697
|
-
const stop = new Promise((resolve) => {
|
|
1698
|
-
notifyStop = () => {
|
|
1699
|
-
resolve({ done: true, value: void 0 });
|
|
1700
|
-
};
|
|
1701
|
-
});
|
|
1702
|
-
const body = {
|
|
1703
|
-
...args.body,
|
|
1704
|
-
threadId: args.identity.sessionId,
|
|
1705
|
-
turnId: typeof args.body.turnId === "string" ? args.body.turnId : "",
|
|
1706
|
-
cancellationToken: typeof args.body.cancellationToken === "string" ? args.body.cancellationToken : void 0
|
|
1707
|
-
};
|
|
1708
|
-
const readTarget = async () => {
|
|
1709
|
-
if (closed) return true;
|
|
1710
|
-
try {
|
|
1711
|
-
const target = await cancel.target({
|
|
1712
|
-
request: args.request,
|
|
1713
|
-
body,
|
|
1714
|
-
tenantId: args.identity.tenantId,
|
|
1715
|
-
userId: args.identity.userId,
|
|
1716
|
-
context: args.context,
|
|
1717
|
-
messageId: args.userMessageId
|
|
1728
|
+
yield* drainModelNotices();
|
|
1729
|
+
} catch (streamErr) {
|
|
1730
|
+
preflightRefused = !observedSourceEvent && openAttempts() === 1 && streamErr instanceof SandboxRunError && streamErr.code === "sandbox_unavailable";
|
|
1731
|
+
const diagnostic = sandboxStreamFailureDiagnostic(streamErr);
|
|
1732
|
+
log("[chat-routes] sandbox stream failed", {
|
|
1733
|
+
failureNote: diagnostic.failureNote,
|
|
1734
|
+
error: streamErr instanceof Error ? streamErr.message : String(streamErr)
|
|
1718
1735
|
});
|
|
1719
|
-
if (
|
|
1720
|
-
|
|
1721
|
-
if (
|
|
1722
|
-
|
|
1723
|
-
|
|
1724
|
-
|
|
1725
|
-
|
|
1726
|
-
|
|
1727
|
-
|
|
1728
|
-
if (!warned) {
|
|
1729
|
-
warned = true;
|
|
1730
|
-
console.error("[chat-routes] stop observation unavailable", { error: redactErrorMessage(error) });
|
|
1731
|
-
}
|
|
1732
|
-
return false;
|
|
1733
|
-
}
|
|
1734
|
-
};
|
|
1735
|
-
const poll = () => polling ??= readTarget().finally(() => {
|
|
1736
|
-
polling = void 0;
|
|
1737
|
-
});
|
|
1738
|
-
const timer = setInterval(() => {
|
|
1739
|
-
void poll();
|
|
1740
|
-
}, 1e3);
|
|
1741
|
-
try {
|
|
1742
|
-
let pending = iterator.next();
|
|
1743
|
-
void pending.catch(() => {
|
|
1744
|
-
});
|
|
1745
|
-
void poll();
|
|
1746
|
-
while (!stopped) {
|
|
1747
|
-
let next;
|
|
1748
|
-
try {
|
|
1749
|
-
next = await Promise.race([pending, stop]);
|
|
1750
|
-
} catch (error) {
|
|
1751
|
-
if (stopped) return;
|
|
1752
|
-
throw error;
|
|
1753
|
-
}
|
|
1754
|
-
if (stopped) return;
|
|
1755
|
-
if (next.done) {
|
|
1756
|
-
await polling;
|
|
1757
|
-
if (!await poll()) throw new Error("The captured turn state could not be confirmed at completion");
|
|
1758
|
-
if (stopped) return;
|
|
1759
|
-
drained = true;
|
|
1760
|
-
return;
|
|
1736
|
+
if (options.errorNotice) {
|
|
1737
|
+
yield* emitErrorNotice(diagnostic.userMessage, diagnostic.code);
|
|
1738
|
+
} else if (options.includeErrorText !== false) {
|
|
1739
|
+
const errorDelta = projectionState.fullText ? `
|
|
1740
|
+
|
|
1741
|
+
---
|
|
1742
|
+
${diagnostic.userMessage}` : diagnostic.userMessage;
|
|
1743
|
+
projectionState.fullText += errorDelta;
|
|
1744
|
+
yield { type: "text", text: errorDelta };
|
|
1761
1745
|
}
|
|
1762
|
-
|
|
1763
|
-
|
|
1764
|
-
|
|
1765
|
-
|
|
1766
|
-
|
|
1767
|
-
|
|
1768
|
-
|
|
1769
|
-
|
|
1770
|
-
|
|
1771
|
-
|
|
1772
|
-
|
|
1773
|
-
|
|
1746
|
+
projectionState.interactionOutcome = "expired";
|
|
1747
|
+
yield* emitTerminalizedTools();
|
|
1748
|
+
yield {
|
|
1749
|
+
type: "error",
|
|
1750
|
+
data: {
|
|
1751
|
+
message: diagnostic.userMessage,
|
|
1752
|
+
code: diagnostic.code,
|
|
1753
|
+
details: { failureNote: diagnostic.failureNote }
|
|
1754
|
+
}
|
|
1755
|
+
};
|
|
1756
|
+
} finally {
|
|
1757
|
+
reasoningClock.settle();
|
|
1774
1758
|
}
|
|
1775
1759
|
}
|
|
1776
|
-
}
|
|
1777
|
-
|
|
1778
|
-
// src/chat-routes/turn-admission.ts
|
|
1779
|
-
function errorResponse(err) {
|
|
1780
|
-
return Response.json({ code: err.code, error: err.message }, { status: err.status });
|
|
1781
|
-
}
|
|
1782
|
-
function admissionServerTiming(trace) {
|
|
1783
|
-
const now = Date.now();
|
|
1784
|
-
const steps = trace.spans(now).filter((span) => span.kind === "admission").sort((a, b) => a.startMs - b.startMs).map((span) => ({ name: span.name, durMs: span.endMs - span.startMs }));
|
|
1785
|
-
return formatServerTiming([{ name: "admission", durMs: now - trace.startedAt }, ...steps]);
|
|
1786
|
-
}
|
|
1787
|
-
async function namedModelRefusal(policy, payload, trace) {
|
|
1788
|
-
const namedModel = typeof payload.model === "string" ? payload.model.trim() : "";
|
|
1789
|
-
if (!policy || !namedModel) return null;
|
|
1790
|
-
const decision = await trace.measure("admission.model_policy", "admission", () => checkModelPolicy(namedModel, policy.catalog, policy.filter, policy.context));
|
|
1791
|
-
return decision.allowed ? null : Response.json({ code: "model_policy_denied", error: decision.reason }, { status: 403 });
|
|
1792
|
-
}
|
|
1793
|
-
function validateTurnBody(body) {
|
|
1794
|
-
const threadId = typeof body.threadId === "string" ? body.threadId.trim() : "";
|
|
1795
|
-
if (!threadId) throw new ChatTurnInputError("Missing threadId");
|
|
1796
|
-
const rawContent = body.content ?? body.message ?? "";
|
|
1797
|
-
if (typeof rawContent !== "string") throw new ChatTurnInputError("content must be a string");
|
|
1798
|
-
const content = rawContent.trim();
|
|
1799
|
-
const fileParts = parseChatTurnParts(body.parts);
|
|
1800
|
-
const mentions = parseMentions(body.mentions);
|
|
1801
|
-
const hasAttachments = Array.isArray(body.attachments) && body.attachments.length > 0;
|
|
1802
|
-
if (!content && fileParts.length === 0 && mentions.length === 0 && !hasAttachments) {
|
|
1803
|
-
throw new ChatTurnInputError("Missing content (send text, parts, mentions, attachments, or any combination)");
|
|
1804
|
-
}
|
|
1805
|
-
assertPromptPartsWithinCap(fileParts);
|
|
1806
|
-
let turnId;
|
|
1807
|
-
try {
|
|
1808
|
-
turnId = normalizeClientTurnId(body.turnId);
|
|
1809
|
-
} catch (err) {
|
|
1810
|
-
throw new ChatTurnInputError(err instanceof Error ? err.message : "Invalid turnId");
|
|
1811
|
-
}
|
|
1812
|
-
const retryAttemptId = body.retryAttemptId;
|
|
1813
|
-
if (retryAttemptId !== void 0 && (typeof retryAttemptId !== "string" || !/^[A-Za-z0-9_-]{1,64}$/.test(retryAttemptId) || !turnId)) throw new ChatTurnInputError("retryAttemptId requires turnId and 1\u201364 letters, digits, underscores, or hyphens");
|
|
1814
|
-
return {
|
|
1815
|
-
// The VALIDATED, deduped mention list replaces the raw one on the payload,
|
|
1816
|
-
// so every downstream seam (`authorize`, `contextGate`, `beforeTurn`,
|
|
1817
|
-
// `produce`) reads checked paths and never the request's own.
|
|
1818
|
-
payload: { ...body, threadId, content, mentions },
|
|
1819
|
-
content,
|
|
1820
|
-
fileParts,
|
|
1821
|
-
mentions,
|
|
1822
|
-
turnId
|
|
1823
|
-
};
|
|
1824
|
-
}
|
|
1825
|
-
function userPartsWithFiles(userParts, fileParts, mentions) {
|
|
1826
|
-
return toChatMessageParts([
|
|
1827
|
-
...userParts,
|
|
1828
|
-
...fileParts.map((part) => ({ ...part })),
|
|
1829
|
-
...mentions.map((mention) => ({ ...mentionInputToPart(mention) }))
|
|
1830
|
-
]);
|
|
1831
|
-
}
|
|
1832
|
-
|
|
1833
|
-
// src/chat-routes/agent-producers.ts
|
|
1834
|
-
async function addressAgents(input) {
|
|
1835
|
-
const addressed = agentMentionsOf(input.mentions);
|
|
1836
|
-
if (addressed.length === 0) return input.produceArgs;
|
|
1837
|
-
const { agents } = input.options;
|
|
1838
|
-
if (!agents) {
|
|
1839
|
-
return errorResponse(new ChatTurnInputError("This chat does not address agents by mention", 400, "agents_unsupported"));
|
|
1840
|
-
}
|
|
1841
|
-
const targets = await input.trace.measure("admission.agents", "admission", () => agents.resolve({ identity: input.identity, context: input.context, mentions: addressed }));
|
|
1842
|
-
const byId = new Map(targets.map((target) => [target.id, target]));
|
|
1843
|
-
const unknown = addressed.find((mention) => !byId.has(mention.agentId));
|
|
1844
|
-
if (unknown) return errorResponse(new ChatTurnInputError(`Unknown agent @${unknown.handle}`, 400, "unknown_agent"));
|
|
1845
|
-
return { ...input.produceArgs, agents: addressed.map((mention) => byId.get(mention.agentId)) };
|
|
1846
|
-
}
|
|
1847
|
-
var SPEAKER_EVENT = "speaker";
|
|
1848
|
-
function speakerPart(agent) {
|
|
1849
|
-
return { type: "speaker", id: agent.id, name: agent.name, handle: agent.handle };
|
|
1850
|
-
}
|
|
1851
|
-
async function composeAgentProducers(args, produce) {
|
|
1852
|
-
const agents = args.agents ?? [];
|
|
1853
|
-
if (agents.length === 0) throw new Error("composeAgentProducers needs at least one responder");
|
|
1854
|
-
const producers = [];
|
|
1855
|
-
const cleanups = [];
|
|
1856
|
-
const stream = (async function* () {
|
|
1857
|
-
for (const agent of agents) {
|
|
1858
|
-
if (args.admissionSignal?.aborted) return;
|
|
1859
|
-
const producer = await produce({ ...args, agent });
|
|
1860
|
-
producers.push({ agent, producer });
|
|
1861
|
-
if (producer.cleanup) cleanups.push(producer.cleanup);
|
|
1862
|
-
yield { type: SPEAKER_EVENT, id: agent.id, name: agent.name, handle: agent.handle };
|
|
1863
|
-
for await (const event of producer.stream) yield event;
|
|
1864
|
-
}
|
|
1865
|
-
})();
|
|
1866
|
-
const partsOf = (entry, read) => {
|
|
1867
|
-
const own = entry.producer[read]?.();
|
|
1868
|
-
const text = entry.producer.finalText();
|
|
1869
|
-
const parts = own ?? (text ? [{ type: "text", text }] : []);
|
|
1870
|
-
return [speakerPart(entry.agent), ...parts];
|
|
1871
|
-
};
|
|
1872
|
-
const last = () => producers[producers.length - 1]?.producer;
|
|
1873
1760
|
return {
|
|
1874
|
-
stream,
|
|
1875
|
-
finalText: () =>
|
|
1876
|
-
|
|
1877
|
-
|
|
1878
|
-
|
|
1879
|
-
|
|
1880
|
-
|
|
1881
|
-
|
|
1882
|
-
|
|
1883
|
-
|
|
1884
|
-
|
|
1885
|
-
|
|
1761
|
+
stream: stream(),
|
|
1762
|
+
finalText: () => projectionState.fullText,
|
|
1763
|
+
terminalObserved: () => projectionState.terminalObserved || preflightRefused,
|
|
1764
|
+
assistantParts: () => finalizePendingInteractionParts(
|
|
1765
|
+
finalizeAssistantParts(partOrder, timedParts(), projectionState.fullText),
|
|
1766
|
+
projectionState.interactionOutcome
|
|
1767
|
+
),
|
|
1768
|
+
// Mid-stream snapshot for incremental persistence: the same accumulators,
|
|
1769
|
+
// WITHOUT the two completion-time settlements (`terminalizeDanglingTool*`
|
|
1770
|
+
// and `finalizePendingInteractionParts`). A running tool and an unanswered
|
|
1771
|
+
// ask are the correct live state; settling them early would persist
|
|
1772
|
+
// phantom failures the final write then reverses.
|
|
1773
|
+
draftParts: () => draftAssistantParts(partOrder, timedParts(), projectionState.fullText),
|
|
1774
|
+
usage: () => usage,
|
|
1775
|
+
// A GETTER, not a captured value: `turn-routes` reads `producer.model` at
|
|
1776
|
+
// draft-snapshot and persist time, both of which happen after the stream
|
|
1777
|
+
// resolved which model serves. A plain property would freeze the PREFERRED
|
|
1778
|
+
// model into the row and make a downgrade unattributable — the exact
|
|
1779
|
+
// failure mode this work exists to prevent.
|
|
1886
1780
|
get model() {
|
|
1887
|
-
return
|
|
1781
|
+
return servedModel();
|
|
1888
1782
|
},
|
|
1889
|
-
modelFailover: () =>
|
|
1890
|
-
|
|
1891
|
-
|
|
1892
|
-
|
|
1893
|
-
})
|
|
1783
|
+
modelFailover: () => ({
|
|
1784
|
+
model: servingModel(),
|
|
1785
|
+
attempts: failover?.attempts() ?? [],
|
|
1786
|
+
usedFallback: failover?.usedFallback() ?? false
|
|
1787
|
+
}),
|
|
1788
|
+
modelAttribution: () => ({
|
|
1789
|
+
...options.model ? { requestedModel: options.model } : {},
|
|
1790
|
+
...modelState.effectiveBackend ?? {},
|
|
1791
|
+
echoReceived: modelState.effectiveBackend !== void 0
|
|
1792
|
+
})
|
|
1894
1793
|
};
|
|
1895
1794
|
}
|
|
1896
|
-
|
|
1897
|
-
|
|
1898
|
-
|
|
1899
|
-
|
|
1900
|
-
|
|
1901
|
-
|
|
1902
|
-
|
|
1903
|
-
|
|
1904
|
-
|
|
1905
|
-
|
|
1906
|
-
|
|
1907
|
-
|
|
1908
|
-
|
|
1909
|
-
|
|
1910
|
-
|
|
1911
|
-
return `/${segments.join("/")}`;
|
|
1912
|
-
}
|
|
1913
|
-
function sanitizeUploadFilename(name) {
|
|
1914
|
-
const base = name.split(/[\\/]/).pop() ?? "file";
|
|
1915
|
-
const safe = base.replace(/[^A-Za-z0-9._-]+/g, "_").replace(/^\.+/, "_");
|
|
1916
|
-
return (safe || "file").slice(0, 120);
|
|
1917
|
-
}
|
|
1918
|
-
var BASE64_CHUNK = 32768;
|
|
1919
|
-
function bytesToBase64(bytes) {
|
|
1920
|
-
let binary = "";
|
|
1921
|
-
for (let offset = 0; offset < bytes.length; offset += BASE64_CHUNK) {
|
|
1922
|
-
binary += String.fromCharCode(...bytes.subarray(offset, offset + BASE64_CHUNK));
|
|
1795
|
+
function createSandboxProjection(projectionState, pendingModelNotices) {
|
|
1796
|
+
const partOrder = [];
|
|
1797
|
+
const partMap = /* @__PURE__ */ new Map();
|
|
1798
|
+
const tracker = { seen: /* @__PURE__ */ new Map() };
|
|
1799
|
+
const reasoningClock = createReasoningClock();
|
|
1800
|
+
const usage = {};
|
|
1801
|
+
const announcedToolArgs = /* @__PURE__ */ new Map();
|
|
1802
|
+
const settledTools = /* @__PURE__ */ new Set();
|
|
1803
|
+
const promotedFileParts = /* @__PURE__ */ new Map();
|
|
1804
|
+
function recordPersistedPart(part, delta, keyOverride) {
|
|
1805
|
+
const persisted = normalizePersistedPart(part);
|
|
1806
|
+
if (!persisted) return;
|
|
1807
|
+
const key = keyOverride ?? getPartKey(persisted);
|
|
1808
|
+
if (!partMap.has(key)) partOrder.push(key);
|
|
1809
|
+
partMap.set(key, mergePersistedPart(partMap.get(key), persisted, delta));
|
|
1923
1810
|
}
|
|
1924
|
-
|
|
1925
|
-
|
|
1926
|
-
|
|
1927
|
-
|
|
1928
|
-
|
|
1929
|
-
|
|
1930
|
-
|
|
1931
|
-
|
|
1932
|
-
|
|
1933
|
-
|
|
1934
|
-
|
|
1935
|
-
|
|
1936
|
-
|
|
1937
|
-
|
|
1938
|
-
|
|
1939
|
-
|
|
1940
|
-
|
|
1941
|
-
|
|
1811
|
+
function* emitTerminalizedTools() {
|
|
1812
|
+
if (projectionState.danglingToolsTerminalized) return;
|
|
1813
|
+
projectionState.danglingToolsTerminalized = true;
|
|
1814
|
+
const updates = terminalizeDanglingAssistantToolUpdates(partOrder, partMap, projectionState.fullText);
|
|
1815
|
+
for (const part of updates) {
|
|
1816
|
+
const toolId = asString(part.id);
|
|
1817
|
+
if (!toolId || settledTools.has(toolId)) continue;
|
|
1818
|
+
settledTools.add(toolId);
|
|
1819
|
+
const state = asRecord(part.state);
|
|
1820
|
+
yield {
|
|
1821
|
+
type: "tool_result",
|
|
1822
|
+
toolCallId: toolId,
|
|
1823
|
+
toolName: asString(part.tool) ?? "tool",
|
|
1824
|
+
outcome: {
|
|
1825
|
+
ok: false,
|
|
1826
|
+
...asString(state?.error) ? { message: asString(state?.error) } : {}
|
|
1827
|
+
}
|
|
1828
|
+
};
|
|
1942
1829
|
}
|
|
1943
|
-
|
|
1944
|
-
|
|
1945
|
-
|
|
1946
|
-
|
|
1947
|
-
if (
|
|
1948
|
-
|
|
1830
|
+
}
|
|
1831
|
+
function* emitFinalUsage() {
|
|
1832
|
+
const promptTokens = usage.inputTokens ?? 0;
|
|
1833
|
+
const completionTokens = usage.outputTokens ?? 0;
|
|
1834
|
+
if (promptTokens || completionTokens) {
|
|
1835
|
+
yield {
|
|
1836
|
+
type: "usage",
|
|
1837
|
+
usage: {
|
|
1838
|
+
promptTokens,
|
|
1839
|
+
completionTokens,
|
|
1840
|
+
...usage.reasoningTokens !== void 0 ? { reasoningTokens: usage.reasoningTokens } : {},
|
|
1841
|
+
...usage.costUsd !== void 0 ? { providerCostUsd: usage.costUsd } : {}
|
|
1842
|
+
}
|
|
1843
|
+
};
|
|
1949
1844
|
}
|
|
1950
|
-
|
|
1951
|
-
|
|
1952
|
-
|
|
1953
|
-
const
|
|
1954
|
-
const
|
|
1955
|
-
|
|
1956
|
-
|
|
1957
|
-
|
|
1958
|
-
|
|
1959
|
-
|
|
1960
|
-
|
|
1845
|
+
}
|
|
1846
|
+
function* drainModelNotices() {
|
|
1847
|
+
while (pendingModelNotices.length > 0) {
|
|
1848
|
+
const queued = pendingModelNotices.shift();
|
|
1849
|
+
const notice = noticePart("warning", queued.id, queued.text);
|
|
1850
|
+
recordPersistedPart(notice, void 0, noticePartKey(notice.id));
|
|
1851
|
+
yield { type: "notice", id: notice.id, noticeKind: "warning", text: queued.text };
|
|
1852
|
+
}
|
|
1853
|
+
}
|
|
1854
|
+
function* emitErrorNotice(text, code) {
|
|
1855
|
+
projectionState.errorNoticeCount += 1;
|
|
1856
|
+
const notice = noticePart("error", `error-${projectionState.errorNoticeCount}`, text, code);
|
|
1857
|
+
recordPersistedPart(notice, void 0, noticePartKey(notice.id));
|
|
1858
|
+
yield { type: "notice", id: notice.id, noticeKind: "error", text, code };
|
|
1859
|
+
}
|
|
1860
|
+
const timedParts = () => {
|
|
1861
|
+
const timed = new Map(partMap);
|
|
1862
|
+
for (const [key, part] of partMap) {
|
|
1863
|
+
if (part.type !== "reasoning") continue;
|
|
1864
|
+
const time = reasoningClock.time(key, asRecord(part.time));
|
|
1865
|
+
if (time) timed.set(key, { ...part, time });
|
|
1866
|
+
}
|
|
1867
|
+
return timed;
|
|
1868
|
+
};
|
|
1869
|
+
return { timedParts, drainModelNotices, tracker, recordPersistedPart, reasoningClock, partMap, announcedToolArgs, settledTools, usage, promotedFileParts, emitFinalUsage, emitTerminalizedTools, emitErrorNotice, partOrder };
|
|
1870
|
+
}
|
|
1871
|
+
async function* projectSandboxMessagePart(event, tracker, recordPersistedPart, reasoningClock, projectionState, partMap, announcedToolArgs, settledTools, usage, options, log, promotedFileParts) {
|
|
1872
|
+
if (event.type === "message.part.updated") {
|
|
1873
|
+
const part = asRecord(event.data?.part);
|
|
1874
|
+
if (!part) return;
|
|
1875
|
+
const rawDelta = event.data?.delta;
|
|
1876
|
+
const partType = String(part.type ?? "");
|
|
1877
|
+
if (partType === "text" || partType === "reasoning") {
|
|
1878
|
+
const key = getPartKey(part);
|
|
1879
|
+
const delta = textDelta(tracker, key, part, rawDelta);
|
|
1880
|
+
recordPersistedPart(part, delta || void 0);
|
|
1881
|
+
if (delta) {
|
|
1882
|
+
if (partType === "reasoning") reasoningClock.reasoning(key);
|
|
1883
|
+
else reasoningClock.settle();
|
|
1884
|
+
if (partType === "text") projectionState.fullText += delta;
|
|
1885
|
+
yield { type: partType, text: delta };
|
|
1961
1886
|
}
|
|
1962
|
-
|
|
1963
|
-
|
|
1964
|
-
|
|
1965
|
-
|
|
1966
|
-
|
|
1967
|
-
|
|
1968
|
-
|
|
1969
|
-
|
|
1970
|
-
|
|
1971
|
-
|
|
1972
|
-
|
|
1973
|
-
|
|
1974
|
-
|
|
1975
|
-
|
|
1976
|
-
|
|
1977
|
-
|
|
1978
|
-
|
|
1887
|
+
return;
|
|
1888
|
+
}
|
|
1889
|
+
if (partType === "tool") {
|
|
1890
|
+
reasoningClock.settle();
|
|
1891
|
+
recordPersistedPart(part, void 0);
|
|
1892
|
+
const persisted = partMap.get(getPartKey(part));
|
|
1893
|
+
const state = asRecord(persisted?.state);
|
|
1894
|
+
const toolId = String(persisted?.id ?? "");
|
|
1895
|
+
const toolName = String(persisted?.tool ?? "tool");
|
|
1896
|
+
const args = asRecord(state?.input) ?? {};
|
|
1897
|
+
const hasPopulatedArgs = Object.keys(args).length > 0;
|
|
1898
|
+
const announcedWithPopulatedArgs = announcedToolArgs.get(toolId);
|
|
1899
|
+
if (toolId && (announcedWithPopulatedArgs === void 0 || !announcedWithPopulatedArgs && hasPopulatedArgs && !settledTools.has(toolId))) {
|
|
1900
|
+
announcedToolArgs.set(toolId, hasPopulatedArgs);
|
|
1901
|
+
yield {
|
|
1902
|
+
type: "tool_call",
|
|
1903
|
+
call: { toolCallId: toolId, toolName, args }
|
|
1904
|
+
};
|
|
1979
1905
|
}
|
|
1980
|
-
|
|
1981
|
-
|
|
1982
|
-
|
|
1983
|
-
|
|
1984
|
-
|
|
1985
|
-
|
|
1906
|
+
const status = String(state?.status ?? "");
|
|
1907
|
+
if (toolId && (status === "completed" || status === "error") && !settledTools.has(toolId)) {
|
|
1908
|
+
settledTools.add(toolId);
|
|
1909
|
+
yield {
|
|
1910
|
+
type: "tool_result",
|
|
1911
|
+
toolCallId: toolId,
|
|
1912
|
+
toolName,
|
|
1913
|
+
outcome: {
|
|
1914
|
+
ok: status === "completed",
|
|
1915
|
+
...state?.output !== void 0 ? { result: state.output } : {},
|
|
1916
|
+
...asString(state?.error) ? { message: asString(state?.error) } : {}
|
|
1917
|
+
}
|
|
1918
|
+
};
|
|
1986
1919
|
}
|
|
1987
|
-
|
|
1988
|
-
|
|
1989
|
-
|
|
1990
|
-
|
|
1991
|
-
|
|
1992
|
-
|
|
1993
|
-
|
|
1920
|
+
return;
|
|
1921
|
+
}
|
|
1922
|
+
if (partType === "step-finish") {
|
|
1923
|
+
addStepFinishUsage(part, usage);
|
|
1924
|
+
recordPersistedPart(part, void 0, `step-finish:#${projectionState.stepCounter++}`);
|
|
1925
|
+
const promptTokens = usage.inputTokens ?? 0;
|
|
1926
|
+
const completionTokens = usage.outputTokens ?? 0;
|
|
1927
|
+
if (promptTokens || completionTokens) {
|
|
1928
|
+
yield {
|
|
1929
|
+
type: "usage",
|
|
1930
|
+
usage: {
|
|
1931
|
+
promptTokens,
|
|
1932
|
+
completionTokens,
|
|
1933
|
+
...usage.reasoningTokens !== void 0 ? { reasoningTokens: usage.reasoningTokens } : {},
|
|
1934
|
+
...usage.costUsd !== void 0 ? { providerCostUsd: usage.costUsd } : {}
|
|
1935
|
+
}
|
|
1936
|
+
};
|
|
1994
1937
|
}
|
|
1995
|
-
|
|
1996
|
-
|
|
1997
|
-
|
|
1998
|
-
|
|
1999
|
-
|
|
2000
|
-
|
|
2001
|
-
|
|
2002
|
-
|
|
2003
|
-
|
|
2004
|
-
|
|
1938
|
+
return;
|
|
1939
|
+
}
|
|
1940
|
+
if (partType === "step-start") {
|
|
1941
|
+
recordPersistedPart(part, void 0, `step-start:#${projectionState.stepCounter}`);
|
|
1942
|
+
return;
|
|
1943
|
+
}
|
|
1944
|
+
if (partType === "file" && options.promoteFilePart) {
|
|
1945
|
+
const promote = options.promoteFilePart;
|
|
1946
|
+
const rawId = asString(part.id);
|
|
1947
|
+
const rawUrl = asString(part.url);
|
|
1948
|
+
const memoKey = rawId ? `id:${rawId}` : rawUrl ? `url:${rawUrl}` : void 0;
|
|
1949
|
+
const attempt = () => Promise.resolve().then(() => promote(part)).catch((err) => {
|
|
1950
|
+
const reason = err instanceof Error ? err.message : String(err);
|
|
1951
|
+
log("[chat-routes] file part promotion threw", { key: memoKey ?? "(keyless)", error: reason });
|
|
1952
|
+
return { succeeded: false, reason };
|
|
2005
1953
|
});
|
|
1954
|
+
let pending;
|
|
1955
|
+
if (memoKey) {
|
|
1956
|
+
pending = promotedFileParts.get(memoKey) ?? attempt();
|
|
1957
|
+
promotedFileParts.set(memoKey, pending);
|
|
1958
|
+
} else {
|
|
1959
|
+
pending = attempt();
|
|
1960
|
+
}
|
|
1961
|
+
const outcome = await pending;
|
|
1962
|
+
if (outcome.succeeded) {
|
|
1963
|
+
recordPersistedPart(outcome.part, void 0, outcome.key);
|
|
1964
|
+
} else if (outcome.part) {
|
|
1965
|
+
recordPersistedPart(outcome.part, void 0, outcome.key);
|
|
1966
|
+
} else {
|
|
1967
|
+
recordPersistedPart(part, void 0);
|
|
1968
|
+
}
|
|
1969
|
+
return;
|
|
2006
1970
|
}
|
|
2007
|
-
|
|
2008
|
-
|
|
1971
|
+
recordPersistedPart(part, void 0);
|
|
1972
|
+
return;
|
|
1973
|
+
}
|
|
2009
1974
|
}
|
|
2010
1975
|
|
|
2011
|
-
// src/chat-routes/
|
|
2012
|
-
|
|
2013
|
-
|
|
2014
|
-
|
|
2015
|
-
}
|
|
2016
|
-
|
|
2017
|
-
|
|
2018
|
-
|
|
2019
|
-
|
|
1976
|
+
// src/chat-routes/cancellation.ts
|
|
1977
|
+
var targetChanged = () => Response.json({
|
|
1978
|
+
error: "This Stop no longer matches the current turn attempt. Refresh its current state.",
|
|
1979
|
+
code: "CANCEL_TARGET_CHANGED"
|
|
1980
|
+
}, { status: 409 });
|
|
1981
|
+
var stateChanged = () => Response.json({
|
|
1982
|
+
error: "This turn changed while Stop was pending. Refresh before trying Stop again.",
|
|
1983
|
+
code: "CANCEL_STATE_CHANGED"
|
|
1984
|
+
}, { status: 409 });
|
|
1985
|
+
var success = (sandboxInterrupted) => Response.json({ succeeded: true, sandboxInterrupted });
|
|
1986
|
+
async function cancelChatTurn(request, options) {
|
|
1987
|
+
if (!options.cancel || request.method !== "POST") {
|
|
1988
|
+
return Response.json({ error: "Method not allowed" }, { status: 405 });
|
|
2020
1989
|
}
|
|
2021
|
-
|
|
2022
|
-
|
|
2023
|
-
|
|
2024
|
-
|
|
1990
|
+
const [raw, invalid] = await parseJsonObjectBody(request, { maxBytes: INLINE_PARTS_MAX_BYTES });
|
|
1991
|
+
if (invalid) return invalid;
|
|
1992
|
+
const threadId = typeof raw.threadId === "string" ? raw.threadId.trim() : "";
|
|
1993
|
+
const turnId = typeof raw.turnId === "string" ? raw.turnId.trim() : "";
|
|
1994
|
+
if (!threadId || !turnId) {
|
|
1995
|
+
return Response.json({ error: "Missing workspaceId, threadId, or turnId" }, { status: 400 });
|
|
2025
1996
|
}
|
|
2026
|
-
|
|
2027
|
-
|
|
2028
|
-
|
|
2029
|
-
|
|
2030
|
-
|
|
2031
|
-
|
|
2032
|
-
|
|
2033
|
-
return hasUrl === hasPath;
|
|
2034
|
-
}
|
|
2035
|
-
function sandboxFileUrl(absolutePath) {
|
|
2036
|
-
if (!absolutePath.startsWith("/")) return void 0;
|
|
1997
|
+
const body = {
|
|
1998
|
+
...raw,
|
|
1999
|
+
threadId,
|
|
2000
|
+
turnId,
|
|
2001
|
+
cancellationToken: typeof raw.cancellationToken === "string" ? raw.cancellationToken : void 0
|
|
2002
|
+
};
|
|
2003
|
+
const log = options.log ?? ((message, meta) => console.error(message, meta ?? {}));
|
|
2037
2004
|
try {
|
|
2038
|
-
|
|
2039
|
-
|
|
2040
|
-
|
|
2041
|
-
|
|
2042
|
-
}
|
|
2043
|
-
function normalizeChatPromptForSandbox(prompt) {
|
|
2044
|
-
if (typeof prompt === "string") return prompt;
|
|
2045
|
-
return prompt.map((part) => {
|
|
2046
|
-
if (part.type === "text") return part;
|
|
2047
|
-
if ("content" in part) {
|
|
2048
|
-
throw new ChatTurnInputError("Sandbox prompt parts do not accept inline content; provide a URL or path");
|
|
2049
|
-
}
|
|
2050
|
-
const url = typeof part.url === "string" && part.url.length > 0 ? part.url : void 0;
|
|
2051
|
-
const path = typeof part.path === "string" && part.path.length > 0 ? part.path : void 0;
|
|
2052
|
-
if (Boolean(url) === Boolean(path)) {
|
|
2053
|
-
throw new ChatTurnInputError(`Sandbox ${part.type} parts require exactly one URL or path`);
|
|
2054
|
-
}
|
|
2055
|
-
if (path && !path.startsWith("/")) {
|
|
2056
|
-
throw new ChatTurnInputError(`Sandbox ${part.type} paths must be absolute: ${path}`);
|
|
2005
|
+
const auth = await options.authorize({ request, intent: "cancel", body, threadId });
|
|
2006
|
+
if (!auth.ok) return auth.response;
|
|
2007
|
+
if (auth.intent !== void 0 && auth.intent !== "cancel") {
|
|
2008
|
+
return Response.json({ error: "Authorization did not allow this turn cancellation.", code: "chat_authorization_intent_mismatch" }, { status: 403 });
|
|
2057
2009
|
}
|
|
2058
|
-
|
|
2059
|
-
|
|
2060
|
-
|
|
2061
|
-
|
|
2062
|
-
|
|
2063
|
-
|
|
2064
|
-
|
|
2010
|
+
const target = await options.cancel.target({
|
|
2011
|
+
request,
|
|
2012
|
+
body,
|
|
2013
|
+
tenantId: auth.tenantId,
|
|
2014
|
+
userId: auth.userId,
|
|
2015
|
+
..."context" in auth ? { context: auth.context } : {}
|
|
2016
|
+
});
|
|
2017
|
+
if (target instanceof Response) return target;
|
|
2018
|
+
if (target.requiresDispatchToken && (!body.cancellationToken || body.cancellationToken !== target.dispatchToken)) {
|
|
2019
|
+
return targetChanged();
|
|
2065
2020
|
}
|
|
2066
|
-
|
|
2067
|
-
if (
|
|
2068
|
-
|
|
2021
|
+
if (target.status === "stopped" || target.status === "answered") return success(false);
|
|
2022
|
+
if (target.control) {
|
|
2023
|
+
const intent = await target.control.request(target.capturedMetadata);
|
|
2024
|
+
return intent.requested ? Response.json({ succeeded: true, sandboxInterrupted: false, cancellationRequested: true }) : targetChanged();
|
|
2069
2025
|
}
|
|
2070
|
-
const
|
|
2071
|
-
if (!
|
|
2072
|
-
|
|
2026
|
+
const markStopped = async () => await target.markStopped(target.capturedMetadata, (/* @__PURE__ */ new Date()).toISOString()) ? null : stateChanged();
|
|
2027
|
+
if (!target.native) return await markStopped() ?? success(false);
|
|
2028
|
+
const native = target.native;
|
|
2029
|
+
if (native.state !== "running" || !native.executionId || !native.sessionId || !native.sandboxId) {
|
|
2030
|
+
return Response.json({
|
|
2031
|
+
error: "This turn has no confirmed native execution to stop. Refresh and try again.",
|
|
2032
|
+
code: "NATIVE_CANCEL_TARGET_UNAVAILABLE"
|
|
2033
|
+
}, { status: 409 });
|
|
2073
2034
|
}
|
|
2074
|
-
|
|
2075
|
-
|
|
2076
|
-
|
|
2077
|
-
...part.mediaType ? { mediaType: part.mediaType } : {},
|
|
2078
|
-
url: fileUrl
|
|
2079
|
-
};
|
|
2080
|
-
});
|
|
2081
|
-
}
|
|
2082
|
-
function projectDispatchHistory(messages) {
|
|
2083
|
-
return messages.map((message) => ({
|
|
2084
|
-
role: message.role === "assistant" ? "assistant" : "user",
|
|
2085
|
-
content: historyContentWithAttachments(message)
|
|
2086
|
-
}));
|
|
2087
|
-
}
|
|
2088
|
-
function readResultToBase64(read) {
|
|
2089
|
-
if (typeof read.base64 === "string") return read.base64;
|
|
2090
|
-
if (read.bytes) return bytesToBase64(read.bytes);
|
|
2091
|
-
return void 0;
|
|
2092
|
-
}
|
|
2093
|
-
async function buildDispatchParts(input) {
|
|
2094
|
-
const readMention = input.readSandboxMention ?? readSandboxMention;
|
|
2095
|
-
const resolveMentionPath = input.resolveMentionPath ?? input.resolveAttachmentPath;
|
|
2096
|
-
const forcePath = input.forcePath ?? false;
|
|
2097
|
-
const mentions = (input.mentions ?? []).filter((mention) => mention.mentionKind !== "agent");
|
|
2098
|
-
const requestMaxBytes = input.requestMaxBytes ?? DISPATCH_REQUEST_MAX_BYTES;
|
|
2099
|
-
const structuralReserveBytes = input.structuralReserveBytes ?? DISPATCH_STRUCTURAL_RESERVE_BYTES;
|
|
2100
|
-
const maxParts = input.maxParts ?? DISPATCH_MAX_PARTS;
|
|
2101
|
-
const parts = [{ type: "text", text: input.text }];
|
|
2102
|
-
const emittedAbsPaths = /* @__PURE__ */ new Set();
|
|
2103
|
-
const flattenedForSizing = flattenHistory(input.text, input.history);
|
|
2104
|
-
const inlineBudget = requestMaxBytes - base64WireLen(byteLen(flattenedForSizing)) - byteLen(JSON.stringify(input.systemPrompt)) - input.profileWireBytes - structuralReserveBytes;
|
|
2105
|
-
let runningInline = 0;
|
|
2106
|
-
for (const attachment of input.attachments) {
|
|
2107
|
-
if (!attachment.path) {
|
|
2108
|
-
return { succeeded: false, error: `attachment path must be non-empty: ${attachment.name}` };
|
|
2035
|
+
const source = await native.source();
|
|
2036
|
+
if (!source || source.id !== native.sandboxId) {
|
|
2037
|
+
return Response.json({ error: "The turn was not marked stopped because its original sandbox is unavailable." }, { status: 503 });
|
|
2109
2038
|
}
|
|
2110
|
-
let read;
|
|
2111
2039
|
try {
|
|
2112
|
-
|
|
2113
|
-
|
|
2114
|
-
|
|
2115
|
-
|
|
2116
|
-
|
|
2117
|
-
|
|
2118
|
-
|
|
2119
|
-
|
|
2120
|
-
|
|
2121
|
-
if (base64 === void 0) {
|
|
2122
|
-
return { succeeded: false, error: `attachment store read produced no content: ${attachment.path}` };
|
|
2123
|
-
}
|
|
2124
|
-
const mediaType = attachment.mediaType ?? read.mediaType;
|
|
2125
|
-
if (attachment.type === "image" && !mediaType) {
|
|
2126
|
-
return { succeeded: false, error: `attachment is missing a mediaType required for an image data URI: ${attachment.path}` };
|
|
2127
|
-
}
|
|
2128
|
-
const absPath = input.resolveAttachmentPath(attachment.path);
|
|
2129
|
-
emittedAbsPaths.add(absPath);
|
|
2130
|
-
if (attachment.type === "image") {
|
|
2131
|
-
const inlinePart2 = {
|
|
2132
|
-
type: "image",
|
|
2133
|
-
filename: attachment.name,
|
|
2134
|
-
mediaType,
|
|
2135
|
-
url: `data:${mediaType};base64,${base64}`
|
|
2136
|
-
};
|
|
2137
|
-
const cost2 = byteLen(JSON.stringify(inlinePart2));
|
|
2138
|
-
if (!forcePath && runningInline + cost2 <= inlineBudget) {
|
|
2139
|
-
parts.push(inlinePart2);
|
|
2140
|
-
runningInline += cost2;
|
|
2141
|
-
} else {
|
|
2142
|
-
parts.push({ type: "image", filename: attachment.name, mediaType, path: absPath });
|
|
2143
|
-
}
|
|
2144
|
-
continue;
|
|
2145
|
-
}
|
|
2146
|
-
const fileMediaType = mediaType ?? "application/octet-stream";
|
|
2147
|
-
const inlinePart = {
|
|
2148
|
-
type: "file",
|
|
2149
|
-
filename: attachment.name,
|
|
2150
|
-
mediaType: fileMediaType,
|
|
2151
|
-
url: `data:${fileMediaType};base64,${base64}`
|
|
2152
|
-
};
|
|
2153
|
-
const cost = byteLen(JSON.stringify(inlinePart));
|
|
2154
|
-
if (!forcePath && runningInline + cost <= inlineBudget) {
|
|
2155
|
-
parts.push(inlinePart);
|
|
2156
|
-
runningInline += cost;
|
|
2157
|
-
} else {
|
|
2158
|
-
const url = sandboxFileUrl(absPath);
|
|
2159
|
-
if (!url) {
|
|
2160
|
-
return { succeeded: false, error: `resolved attachment path must be absolute: ${absPath}` };
|
|
2040
|
+
const session = source.session(native.sessionId);
|
|
2041
|
+
const interrupted = await session.interrupt({ executionId: native.executionId });
|
|
2042
|
+
if (!interrupted.cancelled) {
|
|
2043
|
+
const current = await session.status();
|
|
2044
|
+
const anotherExecution = typeof current?.activeExecutionId === "string" && current.activeExecutionId.length > 0 && current.activeExecutionId !== native.executionId;
|
|
2045
|
+
const terminal = current && ["completed", "failed", "cancelled"].includes(current.status ?? "");
|
|
2046
|
+
if (!current || current.id !== native.sessionId || current.activeExecutionId === native.executionId || !anotherExecution && !terminal) {
|
|
2047
|
+
throw new Error("The targeted native execution has not been confirmed stopped");
|
|
2048
|
+
}
|
|
2161
2049
|
}
|
|
2162
|
-
|
|
2050
|
+
return await markStopped() ?? success(interrupted.cancelled);
|
|
2051
|
+
} catch (error) {
|
|
2052
|
+
log("[chat-routes] native cancellation failed", { error: redactErrorMessage(error) });
|
|
2053
|
+
return Response.json({ error: "The turn was not marked stopped because its native execution could not be confirmed interrupted." }, { status: 502 });
|
|
2163
2054
|
}
|
|
2055
|
+
} catch (error) {
|
|
2056
|
+
if (error instanceof Response) return error;
|
|
2057
|
+
log("[chat-routes] cancellation failed", { error: redactErrorMessage(error) });
|
|
2058
|
+
return Response.json({ error: "Turn cancellation is temporarily unavailable." }, { status: 503 });
|
|
2164
2059
|
}
|
|
2165
|
-
|
|
2166
|
-
|
|
2060
|
+
}
|
|
2061
|
+
async function* observeTurnCancellation(source, args, cancel, abort) {
|
|
2062
|
+
if (!args.userMessageId) {
|
|
2063
|
+
yield* source;
|
|
2064
|
+
return;
|
|
2167
2065
|
}
|
|
2168
|
-
|
|
2169
|
-
|
|
2170
|
-
|
|
2171
|
-
|
|
2172
|
-
|
|
2173
|
-
|
|
2174
|
-
|
|
2175
|
-
|
|
2176
|
-
|
|
2177
|
-
|
|
2178
|
-
|
|
2179
|
-
|
|
2180
|
-
|
|
2066
|
+
const iterator = source[Symbol.asyncIterator]();
|
|
2067
|
+
let closed = false, stopped = false, drained = false, warned = false;
|
|
2068
|
+
let polling;
|
|
2069
|
+
let notifyStop;
|
|
2070
|
+
const stop = new Promise((resolve) => {
|
|
2071
|
+
notifyStop = () => {
|
|
2072
|
+
resolve({ done: true, value: void 0 });
|
|
2073
|
+
};
|
|
2074
|
+
});
|
|
2075
|
+
const body = {
|
|
2076
|
+
...args.body,
|
|
2077
|
+
threadId: args.identity.sessionId,
|
|
2078
|
+
turnId: typeof args.body.turnId === "string" ? args.body.turnId : "",
|
|
2079
|
+
cancellationToken: typeof args.body.cancellationToken === "string" ? args.body.cancellationToken : void 0
|
|
2080
|
+
};
|
|
2081
|
+
const readTarget = async () => {
|
|
2082
|
+
if (closed) return true;
|
|
2181
2083
|
try {
|
|
2182
|
-
|
|
2183
|
-
|
|
2184
|
-
|
|
2084
|
+
const target = await cancel.target({
|
|
2085
|
+
request: args.request,
|
|
2086
|
+
body,
|
|
2087
|
+
tenantId: args.identity.tenantId,
|
|
2088
|
+
userId: args.identity.userId,
|
|
2089
|
+
context: args.context,
|
|
2090
|
+
messageId: args.userMessageId
|
|
2091
|
+
});
|
|
2092
|
+
if (closed) return true;
|
|
2093
|
+
if (target instanceof Response) return false;
|
|
2094
|
+
if (target.status === "stopped" || target.status === "superseded") {
|
|
2095
|
+
stopped = true;
|
|
2096
|
+
abort.abort(new Error("The captured turn was stopped or superseded."));
|
|
2097
|
+
notifyStop();
|
|
2098
|
+
} else if (target.status === "answered") clearInterval(timer);
|
|
2099
|
+
return true;
|
|
2100
|
+
} catch (error) {
|
|
2101
|
+
if (!warned) {
|
|
2102
|
+
warned = true;
|
|
2103
|
+
console.error("[chat-routes] stop observation unavailable", { error: redactErrorMessage(error) });
|
|
2104
|
+
}
|
|
2105
|
+
return false;
|
|
2185
2106
|
}
|
|
2186
|
-
|
|
2187
|
-
|
|
2188
|
-
|
|
2189
|
-
|
|
2107
|
+
};
|
|
2108
|
+
const poll = () => polling ??= readTarget().finally(() => {
|
|
2109
|
+
polling = void 0;
|
|
2110
|
+
});
|
|
2111
|
+
const timer = setInterval(() => {
|
|
2112
|
+
void poll();
|
|
2113
|
+
}, 1e3);
|
|
2114
|
+
try {
|
|
2115
|
+
let pending = iterator.next();
|
|
2116
|
+
void pending.catch(() => {
|
|
2117
|
+
});
|
|
2118
|
+
void poll();
|
|
2119
|
+
while (!stopped) {
|
|
2120
|
+
let next;
|
|
2190
2121
|
try {
|
|
2191
|
-
|
|
2192
|
-
} catch (
|
|
2193
|
-
|
|
2122
|
+
next = await Promise.race([pending, stop]);
|
|
2123
|
+
} catch (error) {
|
|
2124
|
+
if (stopped) return;
|
|
2125
|
+
throw error;
|
|
2194
2126
|
}
|
|
2195
|
-
if (
|
|
2196
|
-
if (
|
|
2197
|
-
|
|
2198
|
-
|
|
2199
|
-
|
|
2200
|
-
|
|
2201
|
-
|
|
2202
|
-
};
|
|
2203
|
-
const cost = byteLen(JSON.stringify(inlinePart));
|
|
2204
|
-
if (runningInline + cost <= inlineBudget) {
|
|
2205
|
-
parts.push(inlinePart);
|
|
2206
|
-
runningInline += cost;
|
|
2207
|
-
continue;
|
|
2127
|
+
if (stopped) return;
|
|
2128
|
+
if (next.done) {
|
|
2129
|
+
await polling;
|
|
2130
|
+
if (!await poll()) throw new Error("The captured turn state could not be confirmed at completion");
|
|
2131
|
+
if (stopped) return;
|
|
2132
|
+
drained = true;
|
|
2133
|
+
return;
|
|
2208
2134
|
}
|
|
2135
|
+
yield next.value;
|
|
2136
|
+
pending = iterator.next();
|
|
2137
|
+
void pending.catch(() => {
|
|
2138
|
+
});
|
|
2209
2139
|
}
|
|
2210
|
-
|
|
2211
|
-
|
|
2212
|
-
|
|
2213
|
-
|
|
2214
|
-
|
|
2215
|
-
|
|
2216
|
-
|
|
2217
|
-
parts.push({ type: "file", filename: mention.name, url });
|
|
2140
|
+
} finally {
|
|
2141
|
+
closed = true;
|
|
2142
|
+
clearInterval(timer);
|
|
2143
|
+
if (!drained) {
|
|
2144
|
+
const cleanup = iterator.return?.().catch(() => {
|
|
2145
|
+
});
|
|
2146
|
+
if (cleanup) args.waitUntil?.(cleanup);
|
|
2218
2147
|
}
|
|
2219
2148
|
}
|
|
2220
|
-
|
|
2221
|
-
|
|
2222
|
-
|
|
2223
|
-
|
|
2149
|
+
}
|
|
2150
|
+
|
|
2151
|
+
// src/chat-routes/turn-admission.ts
|
|
2152
|
+
function errorResponse(err) {
|
|
2153
|
+
return Response.json({ code: err.code, error: err.message }, { status: err.status });
|
|
2154
|
+
}
|
|
2155
|
+
function admissionServerTiming(trace) {
|
|
2156
|
+
const now = Date.now();
|
|
2157
|
+
const steps = trace.spans(now).filter((span) => span.kind === "admission").sort((a, b) => a.startMs - b.startMs).map((span) => ({ name: span.name, durMs: span.endMs - span.startMs }));
|
|
2158
|
+
return formatServerTiming([{ name: "admission", durMs: now - trace.startedAt }, ...steps]);
|
|
2159
|
+
}
|
|
2160
|
+
async function namedModelRefusal(policy, payload, trace) {
|
|
2161
|
+
const namedModel = typeof payload.model === "string" ? payload.model.trim() : "";
|
|
2162
|
+
if (!policy || !namedModel) return null;
|
|
2163
|
+
const decision = await trace.measure("admission.model_policy", "admission", () => checkModelPolicy(namedModel, policy.catalog, policy.filter, policy.context));
|
|
2164
|
+
return decision.allowed ? null : Response.json({ code: "model_policy_denied", error: decision.reason }, { status: 403 });
|
|
2165
|
+
}
|
|
2166
|
+
function validateTurnBody(body) {
|
|
2167
|
+
const threadId = typeof body.threadId === "string" ? body.threadId.trim() : "";
|
|
2168
|
+
if (!threadId) throw new ChatTurnInputError("Missing threadId");
|
|
2169
|
+
const rawContent = body.content ?? body.message ?? "";
|
|
2170
|
+
if (typeof rawContent !== "string") throw new ChatTurnInputError("content must be a string");
|
|
2171
|
+
const content = rawContent.trim();
|
|
2172
|
+
const fileParts = parseChatTurnParts(body.parts);
|
|
2173
|
+
const mentions = parseMentions(body.mentions);
|
|
2174
|
+
const hasAttachments = Array.isArray(body.attachments) && body.attachments.length > 0;
|
|
2175
|
+
if (!content && fileParts.length === 0 && mentions.length === 0 && !hasAttachments) {
|
|
2176
|
+
throw new ChatTurnInputError("Missing content (send text, parts, mentions, attachments, or any combination)");
|
|
2224
2177
|
}
|
|
2225
|
-
|
|
2226
|
-
|
|
2227
|
-
|
|
2228
|
-
|
|
2229
|
-
|
|
2178
|
+
assertPromptPartsWithinCap(fileParts);
|
|
2179
|
+
let turnId;
|
|
2180
|
+
try {
|
|
2181
|
+
turnId = normalizeClientTurnId(body.turnId);
|
|
2182
|
+
} catch (err) {
|
|
2183
|
+
throw new ChatTurnInputError(err instanceof Error ? err.message : "Invalid turnId");
|
|
2230
2184
|
}
|
|
2231
|
-
|
|
2232
|
-
|
|
2185
|
+
const retryAttemptId = body.retryAttemptId;
|
|
2186
|
+
if (retryAttemptId !== void 0 && (typeof retryAttemptId !== "string" || !/^[A-Za-z0-9_-]{1,64}$/.test(retryAttemptId) || !turnId)) throw new ChatTurnInputError("retryAttemptId requires turnId and 1\u201364 letters, digits, underscores, or hyphens");
|
|
2187
|
+
return {
|
|
2188
|
+
// The VALIDATED, deduped mention list replaces the raw one on the payload,
|
|
2189
|
+
// so every downstream seam (`authorize`, `contextGate`, `beforeTurn`,
|
|
2190
|
+
// `produce`) reads checked paths and never the request's own.
|
|
2191
|
+
payload: { ...body, threadId, content, mentions },
|
|
2192
|
+
content,
|
|
2193
|
+
fileParts,
|
|
2194
|
+
mentions,
|
|
2195
|
+
turnId
|
|
2196
|
+
};
|
|
2197
|
+
}
|
|
2198
|
+
function userPartsWithFiles(userParts, fileParts, mentions) {
|
|
2199
|
+
return toChatMessageParts([
|
|
2200
|
+
...userParts,
|
|
2201
|
+
...fileParts.map((part) => ({ ...part })),
|
|
2202
|
+
...mentions.map((mention) => ({ ...mentionInputToPart(mention) }))
|
|
2203
|
+
]);
|
|
2204
|
+
}
|
|
2205
|
+
|
|
2206
|
+
// src/chat-routes/agent-producers.ts
|
|
2207
|
+
async function addressAgents(input) {
|
|
2208
|
+
const addressed = agentMentionsOf(input.mentions);
|
|
2209
|
+
if (addressed.length === 0) return input.produceArgs;
|
|
2210
|
+
const { agents } = input.options;
|
|
2211
|
+
if (!agents) {
|
|
2212
|
+
return errorResponse(new ChatTurnInputError("This chat does not address agents by mention", 400, "agents_unsupported"));
|
|
2233
2213
|
}
|
|
2234
|
-
|
|
2214
|
+
const targets = await input.trace.measure("admission.agents", "admission", () => agents.resolve({ identity: input.identity, context: input.context, mentions: addressed }));
|
|
2215
|
+
const byId = new Map(targets.map((target) => [target.id, target]));
|
|
2216
|
+
const unknown = addressed.find((mention) => !byId.has(mention.agentId));
|
|
2217
|
+
if (unknown) return errorResponse(new ChatTurnInputError(`Unknown agent @${unknown.handle}`, 400, "unknown_agent"));
|
|
2218
|
+
return { ...input.produceArgs, agents: addressed.map((mention) => byId.get(mention.agentId)) };
|
|
2219
|
+
}
|
|
2220
|
+
var SPEAKER_EVENT = "speaker";
|
|
2221
|
+
function speakerPart(agent) {
|
|
2222
|
+
return { type: "speaker", id: agent.id, name: agent.name, handle: agent.handle };
|
|
2223
|
+
}
|
|
2224
|
+
async function composeAgentProducers(args, produce) {
|
|
2225
|
+
const agents = args.agents ?? [];
|
|
2226
|
+
if (agents.length === 0) throw new Error("composeAgentProducers needs at least one responder");
|
|
2227
|
+
const producers = agents.map(() => void 0);
|
|
2228
|
+
const cleanups = [];
|
|
2229
|
+
const slots = agents.map(() => ({ queue: [], done: false, error: void 0, waiters: [] }));
|
|
2230
|
+
const notify = (slot) => {
|
|
2231
|
+
for (const wake of slot.waiters.splice(0)) wake();
|
|
2232
|
+
};
|
|
2233
|
+
agents.forEach((agent, index) => {
|
|
2234
|
+
const slot = slots[index];
|
|
2235
|
+
void (async () => {
|
|
2236
|
+
try {
|
|
2237
|
+
const producer = await produce({ ...args, agent, executionId: `${args.executionId}-${index + 1}` });
|
|
2238
|
+
producers[index] = producer;
|
|
2239
|
+
if (producer.cleanup) cleanups.push(producer.cleanup);
|
|
2240
|
+
for await (const event of producer.stream) {
|
|
2241
|
+
slot.queue.push(event);
|
|
2242
|
+
notify(slot);
|
|
2243
|
+
}
|
|
2244
|
+
} catch (error) {
|
|
2245
|
+
slot.error = error;
|
|
2246
|
+
} finally {
|
|
2247
|
+
slot.done = true;
|
|
2248
|
+
notify(slot);
|
|
2249
|
+
}
|
|
2250
|
+
})();
|
|
2251
|
+
});
|
|
2252
|
+
const stream = (async function* () {
|
|
2253
|
+
for (const [index, agent] of agents.entries()) {
|
|
2254
|
+
if (args.admissionSignal?.aborted) return;
|
|
2255
|
+
yield { type: SPEAKER_EVENT, id: agent.id, name: agent.name, handle: agent.handle };
|
|
2256
|
+
const slot = slots[index];
|
|
2257
|
+
for (; ; ) {
|
|
2258
|
+
if (slot.queue.length > 0) {
|
|
2259
|
+
yield slot.queue.shift();
|
|
2260
|
+
continue;
|
|
2261
|
+
}
|
|
2262
|
+
if (slot.done) {
|
|
2263
|
+
if (slot.error !== void 0) throw slot.error;
|
|
2264
|
+
break;
|
|
2265
|
+
}
|
|
2266
|
+
await new Promise((wake) => {
|
|
2267
|
+
slot.waiters.push(wake);
|
|
2268
|
+
if (slot.queue.length > 0 || slot.done) notify(slot);
|
|
2269
|
+
});
|
|
2270
|
+
}
|
|
2271
|
+
}
|
|
2272
|
+
})();
|
|
2273
|
+
const entries = () => agents.flatMap((agent, index) => producers[index] ? [{ agent, producer: producers[index] }] : []);
|
|
2274
|
+
const partsOf = (entry, read) => {
|
|
2275
|
+
const own = entry.producer[read]?.();
|
|
2276
|
+
const text = entry.producer.finalText();
|
|
2277
|
+
const parts = own ?? (text ? [{ type: "text", text }] : []);
|
|
2278
|
+
return [speakerPart(entry.agent), ...parts];
|
|
2279
|
+
};
|
|
2280
|
+
const last = () => entries().at(-1)?.producer;
|
|
2281
|
+
return {
|
|
2282
|
+
stream,
|
|
2283
|
+
finalText: () => entries().map(({ producer }) => producer.finalText()).filter((text) => text.trim() !== "").join("\n\n"),
|
|
2284
|
+
assistantParts: () => entries().flatMap((entry) => partsOf(entry, "assistantParts")),
|
|
2285
|
+
draftParts: () => entries().flatMap((entry) => partsOf(entry, "draftParts")),
|
|
2286
|
+
usage: () => entries().reduce((sum, { producer }) => {
|
|
2287
|
+
const usage = producer.usage?.() ?? {};
|
|
2288
|
+
for (const [key, value] of Object.entries(usage)) {
|
|
2289
|
+
if (typeof value === "number") sum[key] = (sum[key] ?? 0) + value;
|
|
2290
|
+
}
|
|
2291
|
+
return sum;
|
|
2292
|
+
}, {}),
|
|
2293
|
+
terminalObserved: () => entries().every(({ producer }) => producer.terminalObserved?.() ?? true),
|
|
2294
|
+
get model() {
|
|
2295
|
+
return last()?.model;
|
|
2296
|
+
},
|
|
2297
|
+
modelFailover: () => last()?.modelFailover?.(),
|
|
2298
|
+
modelAttribution: () => last()?.modelAttribution?.(),
|
|
2299
|
+
cleanup: (async () => {
|
|
2300
|
+
await Promise.all(cleanups);
|
|
2301
|
+
})()
|
|
2302
|
+
};
|
|
2235
2303
|
}
|
|
2236
2304
|
|
|
2305
|
+
// src/chat-routes/turn-route-contracts.ts
|
|
2306
|
+
var INTENT_LINE_TIMEOUT_MS = 1500;
|
|
2307
|
+
var INTENT_LINE_HISTORY_MESSAGES = 6;
|
|
2308
|
+
var INTENT_LINE_MAX_CHARS = 240;
|
|
2309
|
+
|
|
2237
2310
|
// src/chat-routes/turn-prefetch.ts
|
|
2238
2311
|
function startTurnPrefetch(prefetch, input) {
|
|
2239
2312
|
let started;
|
|
@@ -2353,7 +2426,7 @@ function routerRouteProducer(value, args) {
|
|
|
2353
2426
|
const options = {
|
|
2354
2427
|
...value,
|
|
2355
2428
|
userMessage: value.userMessage ?? turnPromptText(args.prompt),
|
|
2356
|
-
history: value.history ?? routerTurnHistory(args.priorMessages),
|
|
2429
|
+
history: value.history ?? routerTurnHistory(args.priorMessages, void 0, { pov: args.agent?.id }),
|
|
2357
2430
|
trace: args.trace,
|
|
2358
2431
|
traceId: args.trace?.traceId,
|
|
2359
2432
|
signal: args.admissionSignal,
|
|
@@ -3969,6 +4042,14 @@ function toSandboxEvent(event) {
|
|
|
3969
4042
|
}
|
|
3970
4043
|
|
|
3971
4044
|
export {
|
|
4045
|
+
UPLOAD_INLINE_MAX_BYTES,
|
|
4046
|
+
UPLOAD_MAX_FILE_BYTES,
|
|
4047
|
+
sanitizeUploadFilename,
|
|
4048
|
+
bytesToBase64,
|
|
4049
|
+
createUploadRoute,
|
|
4050
|
+
normalizeChatPromptForSandbox,
|
|
4051
|
+
projectDispatchHistory,
|
|
4052
|
+
buildDispatchParts,
|
|
3972
4053
|
createRouterTurnProducer,
|
|
3973
4054
|
HYBRID_TURN_GUIDANCE,
|
|
3974
4055
|
startHybridBox,
|
|
@@ -3996,15 +4077,7 @@ export {
|
|
|
3996
4077
|
INTENT_LINE_TIMEOUT_MS,
|
|
3997
4078
|
INTENT_LINE_HISTORY_MESSAGES,
|
|
3998
4079
|
INTENT_LINE_MAX_CHARS,
|
|
3999
|
-
UPLOAD_INLINE_MAX_BYTES,
|
|
4000
|
-
UPLOAD_MAX_FILE_BYTES,
|
|
4001
|
-
sanitizeUploadFilename,
|
|
4002
|
-
bytesToBase64,
|
|
4003
|
-
createUploadRoute,
|
|
4004
|
-
normalizeChatPromptForSandbox,
|
|
4005
|
-
projectDispatchHistory,
|
|
4006
|
-
buildDispatchParts,
|
|
4007
4080
|
createChatTurnRoutes,
|
|
4008
4081
|
streamChatRouteAsSandboxEvents
|
|
4009
4082
|
};
|
|
4010
|
-
//# sourceMappingURL=chunk-
|
|
4083
|
+
//# sourceMappingURL=chunk-RYKANV3P.js.map
|