@tangle-network/agent-app 4.0.9 → 4.0.11
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/billing/identity-bound.d.ts +7 -0
- package/dist/billing/index.d.ts +1 -1
- package/dist/billing/index.js +3 -1
- package/dist/chat-routes/failure-wording.d.ts +38 -33
- package/dist/chat-routes/index.js +20 -20
- package/dist/chat-routes/index.js.map +1 -1
- package/dist/{chunk-R5PW3FO4.js → chunk-2IGXFHUO.js} +13 -2
- package/dist/chunk-2IGXFHUO.js.map +1 -0
- package/dist/{chunk-XZ37VCTP.js → chunk-CD4SJXYC.js} +92 -797
- package/dist/chunk-CD4SJXYC.js.map +1 -0
- package/dist/{chunk-FTGKXM7O.js → chunk-EK7A5Y7P.js} +12 -4
- package/dist/chunk-EK7A5Y7P.js.map +1 -0
- package/dist/{chunk-AYTNVTDP.js → chunk-QMQB725F.js} +3 -13
- package/dist/chunk-QMQB725F.js.map +1 -0
- package/dist/chunk-RNDWCT4O.js +1136 -0
- package/dist/chunk-RNDWCT4O.js.map +1 -0
- package/dist/{chunk-W2DIPVHW.js → chunk-TMWUWKLU.js} +6 -5
- package/dist/chunk-TMWUWKLU.js.map +1 -0
- package/dist/preset-cloudflare/index.js +7 -4
- package/dist/preset-cloudflare/index.js.map +1 -1
- package/dist/public-consultation/index.js +5 -5
- package/dist/sandbox/index.js +3 -3
- package/package.json +1 -1
- package/dist/chunk-AYTNVTDP.js.map +0 -1
- package/dist/chunk-FTGKXM7O.js.map +0 -1
- package/dist/chunk-K4YIDNX5.js +0 -263
- package/dist/chunk-K4YIDNX5.js.map +0 -1
- package/dist/chunk-R5PW3FO4.js.map +0 -1
- package/dist/chunk-W2DIPVHW.js.map +0 -1
- package/dist/chunk-XZ37VCTP.js.map +0 -1
|
@@ -0,0 +1,1136 @@
|
|
|
1
|
+
import {
|
|
2
|
+
getChatFundingRecovery
|
|
3
|
+
} from "./chunk-QUV5M7SZ.js";
|
|
4
|
+
import {
|
|
5
|
+
toChatMessageParts
|
|
6
|
+
} from "./chunk-4YZKDWMB.js";
|
|
7
|
+
import {
|
|
8
|
+
asRecord,
|
|
9
|
+
asString
|
|
10
|
+
} from "./chunk-JM6ANMOA.js";
|
|
11
|
+
import {
|
|
12
|
+
SandboxRunError
|
|
13
|
+
} from "./chunk-2IGXFHUO.js";
|
|
14
|
+
import {
|
|
15
|
+
ModelFailoverExhaustedError,
|
|
16
|
+
isUpstreamUnavailable,
|
|
17
|
+
readHttpStatusHint,
|
|
18
|
+
readUpstreamUnavailableCode,
|
|
19
|
+
runWithModelFailover
|
|
20
|
+
} from "./chunk-L3QR274L.js";
|
|
21
|
+
|
|
22
|
+
// src/chat-routes/sandbox-turn-usage.ts
|
|
23
|
+
var TURN_USAGE_KEYS = [
|
|
24
|
+
"inputTokens",
|
|
25
|
+
"outputTokens",
|
|
26
|
+
"reasoningTokens",
|
|
27
|
+
"cacheReadTokens",
|
|
28
|
+
"cacheWriteTokens",
|
|
29
|
+
"costUsd",
|
|
30
|
+
"harnessCostUsd"
|
|
31
|
+
];
|
|
32
|
+
function measuredUsage(usage) {
|
|
33
|
+
const measured = {};
|
|
34
|
+
for (const key of TURN_USAGE_KEYS) if (usage?.[key] !== void 0) measured[key] = usage[key];
|
|
35
|
+
return measured;
|
|
36
|
+
}
|
|
37
|
+
function addStepFinishUsage(part, usage) {
|
|
38
|
+
const tokens = asRecord(part.tokens);
|
|
39
|
+
if (tokens) {
|
|
40
|
+
const cache = asRecord(tokens.cache);
|
|
41
|
+
const add = (current, value) => {
|
|
42
|
+
const n = Number(value);
|
|
43
|
+
if (!Number.isFinite(n)) return current;
|
|
44
|
+
return (current ?? 0) + n;
|
|
45
|
+
};
|
|
46
|
+
usage.inputTokens = add(usage.inputTokens, tokens.input);
|
|
47
|
+
usage.outputTokens = add(usage.outputTokens, tokens.output);
|
|
48
|
+
usage.reasoningTokens = add(usage.reasoningTokens, tokens.reasoning);
|
|
49
|
+
if (cache) {
|
|
50
|
+
usage.cacheReadTokens = add(usage.cacheReadTokens, cache.read);
|
|
51
|
+
usage.cacheWriteTokens = add(usage.cacheWriteTokens, cache.write);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
const cost = Number(part.cost);
|
|
55
|
+
if (Number.isFinite(cost)) usage.harnessCostUsd = (usage.harnessCostUsd ?? 0) + cost;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// src/chat-routes/visible-text.ts
|
|
59
|
+
function transformVisibleTextParts(parts, transform) {
|
|
60
|
+
return Promise.all(parts.map(async (part) => {
|
|
61
|
+
const type = String(part.type ?? "");
|
|
62
|
+
return type === "text" || type === "reasoning" ? { ...part, text: await transform(String(part.text ?? "")) } : part;
|
|
63
|
+
}));
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// src/chat-routes/draft-persistence.ts
|
|
67
|
+
var CONTENT_EVENT_TYPES = /* @__PURE__ */ new Set([
|
|
68
|
+
"text",
|
|
69
|
+
"reasoning",
|
|
70
|
+
"tool_call",
|
|
71
|
+
"tool_result",
|
|
72
|
+
"usage",
|
|
73
|
+
"notice",
|
|
74
|
+
"error",
|
|
75
|
+
"file",
|
|
76
|
+
"interaction",
|
|
77
|
+
"interaction.cancel",
|
|
78
|
+
"plan.submitted",
|
|
79
|
+
"message.part.updated"
|
|
80
|
+
]);
|
|
81
|
+
var DEFAULT_INTERVAL_MS = 2e3;
|
|
82
|
+
var DEFAULT_BACKOFF_BYTES = 262144;
|
|
83
|
+
var DEFAULT_MAX_DRAFT_TOOL_OUTPUT_BYTES = 32768;
|
|
84
|
+
var encoder = new TextEncoder();
|
|
85
|
+
function isDraftContentEvent(event) {
|
|
86
|
+
return typeof event?.type === "string" && CONTENT_EVENT_TYPES.has(event.type);
|
|
87
|
+
}
|
|
88
|
+
function capDraftToolOutput(part, maxBytes) {
|
|
89
|
+
if (maxBytes <= 0) return part;
|
|
90
|
+
if (String(part.type ?? "") !== "tool") return part;
|
|
91
|
+
const state = part.state;
|
|
92
|
+
if (!state || typeof state !== "object") return part;
|
|
93
|
+
const record = state;
|
|
94
|
+
const output = record.output;
|
|
95
|
+
if (output === void 0) return part;
|
|
96
|
+
const text = typeof output === "string" ? output : safeStringify(output);
|
|
97
|
+
const serialized = typeof output === "string" && text.length <= maxBytes ? safeStringify(text) : text;
|
|
98
|
+
if (serialized.length <= maxBytes && encoder.encode(serialized).byteLength <= maxBytes) return part;
|
|
99
|
+
const metadata = record.metadata && typeof record.metadata === "object" ? record.metadata : {};
|
|
100
|
+
return {
|
|
101
|
+
...part,
|
|
102
|
+
state: {
|
|
103
|
+
...record,
|
|
104
|
+
output: draftOutputPreview(text, maxBytes),
|
|
105
|
+
metadata: { ...metadata, draftTruncated: true }
|
|
106
|
+
}
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
function draftOutputPreview(text, maxBytes) {
|
|
110
|
+
if (maxBytes < 2) return void 0;
|
|
111
|
+
const marker = "\u2026[draft-truncated]";
|
|
112
|
+
const suffix = serializedBytes(marker) <= maxBytes ? marker : "";
|
|
113
|
+
const prefix = (end) => {
|
|
114
|
+
if (end > 0 && (text.codePointAt(end - 1) ?? 0) > 65535) end -= 1;
|
|
115
|
+
return text.slice(0, end);
|
|
116
|
+
};
|
|
117
|
+
let low = 0;
|
|
118
|
+
let high = Math.min(text.length, Math.floor(maxBytes));
|
|
119
|
+
while (low < high) {
|
|
120
|
+
const middle = Math.ceil((low + high) / 2);
|
|
121
|
+
if (serializedBytes(prefix(middle) + suffix) <= maxBytes) low = middle;
|
|
122
|
+
else high = middle - 1;
|
|
123
|
+
}
|
|
124
|
+
return prefix(low) + suffix;
|
|
125
|
+
}
|
|
126
|
+
function serializedBytes(value) {
|
|
127
|
+
return encoder.encode(safeStringify(value)).byteLength;
|
|
128
|
+
}
|
|
129
|
+
function safeStringify(value) {
|
|
130
|
+
try {
|
|
131
|
+
return JSON.stringify(value) ?? "";
|
|
132
|
+
} catch {
|
|
133
|
+
return String(value);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
function createAssistantDraftWriter(options) {
|
|
137
|
+
const log = options.log ?? (() => {
|
|
138
|
+
});
|
|
139
|
+
const baseIntervalMs = options.intervalMs ?? DEFAULT_INTERVAL_MS;
|
|
140
|
+
const backoffBytes = options.backoffBytes ?? DEFAULT_BACKOFF_BYTES;
|
|
141
|
+
const maxToolOutput = options.maxDraftToolOutputBytes ?? DEFAULT_MAX_DRAFT_TOOL_OUTPUT_BYTES;
|
|
142
|
+
let dirty = false;
|
|
143
|
+
let closed = false;
|
|
144
|
+
let inFlight;
|
|
145
|
+
let lastWriteAt = 0;
|
|
146
|
+
let lastBlobBytes = 0;
|
|
147
|
+
let rowId;
|
|
148
|
+
let writes = 0;
|
|
149
|
+
function currentIntervalMs() {
|
|
150
|
+
if (lastBlobBytes >= backoffBytes * 10) return baseIntervalMs * 5;
|
|
151
|
+
if (lastBlobBytes >= backoffBytes) return Math.round(baseIntervalMs * 2.5);
|
|
152
|
+
return baseIntervalMs;
|
|
153
|
+
}
|
|
154
|
+
async function projectValues(snapshot) {
|
|
155
|
+
const transform = options.transformText;
|
|
156
|
+
const content = transform ? await transform(snapshot.content) : snapshot.content;
|
|
157
|
+
let parts;
|
|
158
|
+
if (snapshot.parts) {
|
|
159
|
+
const capped = snapshot.parts.map((part) => capDraftToolOutput(part, maxToolOutput));
|
|
160
|
+
const redacted = transform ? await transformVisibleTextParts(capped, transform) : capped;
|
|
161
|
+
parts = toChatMessageParts(redacted);
|
|
162
|
+
}
|
|
163
|
+
const usage = snapshot.usage ?? {};
|
|
164
|
+
return {
|
|
165
|
+
content,
|
|
166
|
+
...parts && parts.length > 0 ? { parts } : {},
|
|
167
|
+
...snapshot.model ? { model: snapshot.model } : {},
|
|
168
|
+
...measuredUsage(usage)
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
async function adoptRow() {
|
|
172
|
+
if (rowId) return rowId;
|
|
173
|
+
const existing = (await options.store.listMessages(options.threadId)).find(
|
|
174
|
+
(message) => message.id === options.messageId
|
|
175
|
+
);
|
|
176
|
+
if (existing) rowId = existing.id;
|
|
177
|
+
return rowId;
|
|
178
|
+
}
|
|
179
|
+
async function writeOnce(values) {
|
|
180
|
+
if (await adoptRow()) {
|
|
181
|
+
await options.store.updateMessage(rowId, values);
|
|
182
|
+
writes += 1;
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
const inserted = await options.store.appendMessage({
|
|
186
|
+
id: options.messageId,
|
|
187
|
+
threadId: options.threadId,
|
|
188
|
+
role: "assistant",
|
|
189
|
+
...values
|
|
190
|
+
});
|
|
191
|
+
rowId = rowIdOf(inserted) ?? options.messageId;
|
|
192
|
+
writes += 1;
|
|
193
|
+
}
|
|
194
|
+
function trigger() {
|
|
195
|
+
if (closed) return;
|
|
196
|
+
if (!dirty) return;
|
|
197
|
+
if (inFlight) return;
|
|
198
|
+
const now = Date.now();
|
|
199
|
+
if (lastWriteAt !== 0 && now - lastWriteAt < currentIntervalMs()) return;
|
|
200
|
+
const snapshot = options.snapshot();
|
|
201
|
+
if (!snapshot) return;
|
|
202
|
+
if (!snapshot.content && (!snapshot.parts || snapshot.parts.length === 0)) return;
|
|
203
|
+
dirty = false;
|
|
204
|
+
lastWriteAt = now;
|
|
205
|
+
inFlight = (async () => {
|
|
206
|
+
try {
|
|
207
|
+
const values = await projectValues(snapshot);
|
|
208
|
+
lastBlobBytes = values.parts ? serializedBytes(values.parts) : 0;
|
|
209
|
+
await writeOnce(values);
|
|
210
|
+
} catch (err) {
|
|
211
|
+
log("[chat-routes] incremental assistant persistence failed", {
|
|
212
|
+
messageId: options.messageId,
|
|
213
|
+
error: err instanceof Error ? err.message : String(err)
|
|
214
|
+
});
|
|
215
|
+
} finally {
|
|
216
|
+
inFlight = void 0;
|
|
217
|
+
if (dirty && !closed) trigger();
|
|
218
|
+
}
|
|
219
|
+
})();
|
|
220
|
+
}
|
|
221
|
+
return {
|
|
222
|
+
notify(event) {
|
|
223
|
+
if (closed) return;
|
|
224
|
+
if (isDraftContentEvent(event)) dirty = true;
|
|
225
|
+
trigger();
|
|
226
|
+
},
|
|
227
|
+
async close() {
|
|
228
|
+
closed = true;
|
|
229
|
+
if (inFlight) await inFlight;
|
|
230
|
+
},
|
|
231
|
+
async finalize(values) {
|
|
232
|
+
closed = true;
|
|
233
|
+
if (inFlight) await inFlight;
|
|
234
|
+
await writeOnce(values);
|
|
235
|
+
},
|
|
236
|
+
rowId: () => rowId,
|
|
237
|
+
async discard() {
|
|
238
|
+
closed = true;
|
|
239
|
+
if (inFlight) await inFlight;
|
|
240
|
+
if (!options.store.deleteMessage) return;
|
|
241
|
+
try {
|
|
242
|
+
if (!await adoptRow()) return;
|
|
243
|
+
await options.store.deleteMessage(rowId);
|
|
244
|
+
rowId = void 0;
|
|
245
|
+
} catch (err) {
|
|
246
|
+
log("[chat-routes] draft assistant row discard failed", {
|
|
247
|
+
messageId: options.messageId,
|
|
248
|
+
error: err instanceof Error ? err.message : String(err)
|
|
249
|
+
});
|
|
250
|
+
}
|
|
251
|
+
},
|
|
252
|
+
writeCount: () => writes
|
|
253
|
+
};
|
|
254
|
+
}
|
|
255
|
+
function storeSupportsDraftPersistence(store) {
|
|
256
|
+
return typeof store.updateMessage === "function";
|
|
257
|
+
}
|
|
258
|
+
function rowIdOf(inserted) {
|
|
259
|
+
const id = inserted?.id;
|
|
260
|
+
return typeof id === "string" && id ? id : null;
|
|
261
|
+
}
|
|
262
|
+
function assistantRowIdForTurn(turnKey) {
|
|
263
|
+
return `assistant:${turnKey}`;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
// src/chat-routes/model-failover-stream.ts
|
|
267
|
+
var TERMINAL_FAILURE_TYPES = /* @__PURE__ */ new Set(["error", "session.run.failed"]);
|
|
268
|
+
var NON_COMMITTING_PART_TYPES = /* @__PURE__ */ new Set(["step-start", "step-finish"]);
|
|
269
|
+
function isCommittingSandboxEvent(event) {
|
|
270
|
+
const record = asRecord(event);
|
|
271
|
+
if (!record) return false;
|
|
272
|
+
const type = asString(record.type) ?? "";
|
|
273
|
+
if (!type) return false;
|
|
274
|
+
if (type === "message.part.updated") {
|
|
275
|
+
const data = asRecord(record.data);
|
|
276
|
+
const part = asRecord(data?.part);
|
|
277
|
+
const partType = asString(part?.type) ?? "";
|
|
278
|
+
if (NON_COMMITTING_PART_TYPES.has(partType)) return false;
|
|
279
|
+
if (partType === "text" || partType === "reasoning") {
|
|
280
|
+
return [data?.delta, part?.text, part?.content].some((value) => typeof value === "string" && value.length > 0);
|
|
281
|
+
}
|
|
282
|
+
return true;
|
|
283
|
+
}
|
|
284
|
+
if (type === "start" || type === "execution.started" || type === "status" || type === "model-processing" || type === "model.processing" || // Public Sandbox timing metadata: a request was attempted, not answered.
|
|
285
|
+
type === "model.first_request" || type === "session.created" || type === "session.updated" || type === "session.idle" || type === "step-start" || type === "step-finish" || type === "turn" || type === "warning") {
|
|
286
|
+
return false;
|
|
287
|
+
}
|
|
288
|
+
return true;
|
|
289
|
+
}
|
|
290
|
+
function summarizeFailoverReason(reason) {
|
|
291
|
+
const collapsed = reason.replace(/\s+/g, " ").trim();
|
|
292
|
+
if (!collapsed) return "upstream unavailable";
|
|
293
|
+
if (/<!doctype html|<html[\s>]/i.test(collapsed)) {
|
|
294
|
+
const status = readHttpStatusHint(collapsed);
|
|
295
|
+
return status ? `HTTP ${status}` : "upstream returned an error page";
|
|
296
|
+
}
|
|
297
|
+
const LIMIT = 160;
|
|
298
|
+
if (collapsed.length <= LIMIT) return collapsed;
|
|
299
|
+
const clipped = collapsed.slice(0, LIMIT);
|
|
300
|
+
const lastSpace = clipped.lastIndexOf(" ");
|
|
301
|
+
return `${(lastSpace > LIMIT - 30 ? clipped.slice(0, lastSpace) : clipped).trimEnd()}\u2026`;
|
|
302
|
+
}
|
|
303
|
+
function isLiveLifecycleEvent(event) {
|
|
304
|
+
const type = asString(asRecord(event)?.type) ?? "";
|
|
305
|
+
return type === "start" || type === "execution.started" || type === "status" || type === "model-processing" || type === "model.processing" || // Public Sandbox timing metadata: a request was attempted, not answered.
|
|
306
|
+
type === "model.first_request" || type === "session.created" || type === "session.updated" || type === "session.idle";
|
|
307
|
+
}
|
|
308
|
+
function classifyTerminalFailure(event) {
|
|
309
|
+
const record = asRecord(event);
|
|
310
|
+
if (!record) return null;
|
|
311
|
+
const type = asString(record.type) ?? "";
|
|
312
|
+
if (!TERMINAL_FAILURE_TYPES.has(type)) return null;
|
|
313
|
+
const data = asRecord(record.data);
|
|
314
|
+
const outage = isUpstreamUnavailable(data) || isUpstreamUnavailable(record);
|
|
315
|
+
const reason = asString(data?.message) ?? asString(data?.error) ?? asString(data?.reason) ?? asString(record.message) ?? `sandbox stream reported ${type}`;
|
|
316
|
+
const code = readUpstreamUnavailableCode(data) ?? asString(data?.errorCode) ?? asString(data?.code) ?? readUpstreamUnavailableCode(record) ?? asString(record.errorCode) ?? asString(record.code);
|
|
317
|
+
return { outage, reason, ...code ? { code } : {} };
|
|
318
|
+
}
|
|
319
|
+
var DEFAULT_MODEL_STREAM_OPEN_TIMEOUT_MS = 12e4;
|
|
320
|
+
var DEFAULT_MODEL_FIRST_RESPONSE_TIMEOUT_MS = 6e4;
|
|
321
|
+
var ModelFailoverTimeoutError = class extends ModelFailoverExhaustedError {
|
|
322
|
+
code;
|
|
323
|
+
model;
|
|
324
|
+
timeoutMs;
|
|
325
|
+
constructor(input) {
|
|
326
|
+
super(input.attempts);
|
|
327
|
+
this.name = "ModelFailoverTimeoutError";
|
|
328
|
+
this.code = input.code;
|
|
329
|
+
this.model = input.model;
|
|
330
|
+
this.timeoutMs = input.timeoutMs;
|
|
331
|
+
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.`;
|
|
332
|
+
}
|
|
333
|
+
};
|
|
334
|
+
function resolveTimeoutMs(value, fallback, label) {
|
|
335
|
+
if (value === void 0) return fallback;
|
|
336
|
+
if (!Number.isFinite(value) || value <= 0) {
|
|
337
|
+
throw new RangeError(`${label} must be a finite number greater than 0`);
|
|
338
|
+
}
|
|
339
|
+
return Math.max(1, Math.trunc(value));
|
|
340
|
+
}
|
|
341
|
+
var TIMED_OUT = /* @__PURE__ */ Symbol("model-attempt-timed-out");
|
|
342
|
+
function deadlineAfter(timeoutMs) {
|
|
343
|
+
let timer;
|
|
344
|
+
const promise = new Promise((resolve) => {
|
|
345
|
+
timer = setTimeout(() => resolve(TIMED_OUT), timeoutMs);
|
|
346
|
+
});
|
|
347
|
+
return {
|
|
348
|
+
promise,
|
|
349
|
+
clear: () => {
|
|
350
|
+
if (timer !== void 0) {
|
|
351
|
+
clearTimeout(timer);
|
|
352
|
+
timer = void 0;
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
};
|
|
356
|
+
}
|
|
357
|
+
function isEmptyTerminalReceipt(event) {
|
|
358
|
+
const record = asRecord(event);
|
|
359
|
+
if (!record) return false;
|
|
360
|
+
const type = asString(record.type) ?? "";
|
|
361
|
+
if (type !== "result" && type !== "done") return false;
|
|
362
|
+
const data = asRecord(record.data);
|
|
363
|
+
const text = asString(data?.finalText) ?? asString(data?.text) ?? "";
|
|
364
|
+
return text.trim().length === 0;
|
|
365
|
+
}
|
|
366
|
+
var MAX_EMPTY_TURN_RETRIES = 3;
|
|
367
|
+
function resolveEmptyTurnRetries(value) {
|
|
368
|
+
if (typeof value !== "number" || !Number.isFinite(value)) return 0;
|
|
369
|
+
const truncated = Math.trunc(value);
|
|
370
|
+
if (truncated <= 0) return 0;
|
|
371
|
+
return Math.min(truncated, MAX_EMPTY_TURN_RETRIES);
|
|
372
|
+
}
|
|
373
|
+
async function closeIterator(iterator, log) {
|
|
374
|
+
try {
|
|
375
|
+
await iterator.return?.();
|
|
376
|
+
} catch (err) {
|
|
377
|
+
log?.("[chat-routes] abandoning a failed model stream threw", {
|
|
378
|
+
error: err instanceof Error ? err.message : String(err)
|
|
379
|
+
});
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
function streamWithModelFailover(options) {
|
|
383
|
+
const committing = options.isCommitting ?? isCommittingSandboxEvent;
|
|
384
|
+
const emptyTurnRetries = resolveEmptyTurnRetries(options.emptyTurnRetries);
|
|
385
|
+
const openTimeoutMs = resolveTimeoutMs(
|
|
386
|
+
options.openTimeoutMs,
|
|
387
|
+
DEFAULT_MODEL_STREAM_OPEN_TIMEOUT_MS,
|
|
388
|
+
"openTimeoutMs"
|
|
389
|
+
);
|
|
390
|
+
const firstResponseTimeoutMs = resolveTimeoutMs(
|
|
391
|
+
options.firstResponseTimeoutMs,
|
|
392
|
+
DEFAULT_MODEL_FIRST_RESPONSE_TIMEOUT_MS,
|
|
393
|
+
"firstResponseTimeoutMs"
|
|
394
|
+
);
|
|
395
|
+
const liveLifecycle = options.liveLifecycleEvents === true;
|
|
396
|
+
const progress = createProgressQueue();
|
|
397
|
+
let serving;
|
|
398
|
+
let trail = [];
|
|
399
|
+
let fellBack = false;
|
|
400
|
+
const attemptState = { index: 0 };
|
|
401
|
+
let finalTimeout;
|
|
402
|
+
const { drainOnce } = createModelAttemptDrainer(attemptState, options, openTimeoutMs, firstResponseTimeoutMs, committing, liveLifecycle, progress);
|
|
403
|
+
const probe = async (model) => {
|
|
404
|
+
finalTimeout = void 0;
|
|
405
|
+
for (let retry = 0; ; retry += 1) {
|
|
406
|
+
const pass = await drainOnce(model);
|
|
407
|
+
if (!pass.empty || retry >= emptyTurnRetries) {
|
|
408
|
+
if (!pass.outcome.committed && pass.outcome.timeoutCode) {
|
|
409
|
+
finalTimeout = {
|
|
410
|
+
code: pass.outcome.timeoutCode,
|
|
411
|
+
model,
|
|
412
|
+
timeoutMs: pass.outcome.timeoutCode === "model_stream_open_timeout" ? openTimeoutMs : firstResponseTimeoutMs
|
|
413
|
+
};
|
|
414
|
+
} else {
|
|
415
|
+
finalTimeout = void 0;
|
|
416
|
+
}
|
|
417
|
+
return pass.outcome;
|
|
418
|
+
}
|
|
419
|
+
const iterator = pass.outcome.committed ? pass.outcome.iterator : null;
|
|
420
|
+
if (pass.outcome.committed) pass.outcome.abort();
|
|
421
|
+
if (iterator) void closeIterator(iterator, options.log);
|
|
422
|
+
const info = { model, retry: retry + 1, remaining: emptyTurnRetries - retry - 1 };
|
|
423
|
+
options.log?.("[chat-routes] turn completed with no assistant text; re-running the same model", {
|
|
424
|
+
...info
|
|
425
|
+
});
|
|
426
|
+
options.onEmptyTurnRetry?.(info);
|
|
427
|
+
}
|
|
428
|
+
};
|
|
429
|
+
const events = (async function* () {
|
|
430
|
+
let handle;
|
|
431
|
+
const settled = runWithModelFailover({
|
|
432
|
+
models: options.models,
|
|
433
|
+
run: probe,
|
|
434
|
+
...options.policy ? { policy: options.policy } : {},
|
|
435
|
+
// The probe has already classified the raw payload with
|
|
436
|
+
// `isUpstreamUnavailable`; this reads its verdict rather than
|
|
437
|
+
// re-classifying a wrapper object, so the two can never disagree.
|
|
438
|
+
isUnavailableResult: (result) => result.committed === false,
|
|
439
|
+
onFallback: (attempt, nextModel) => {
|
|
440
|
+
const info = {
|
|
441
|
+
from: attempt.model,
|
|
442
|
+
to: nextModel,
|
|
443
|
+
reason: attempt.reason ?? "upstream unavailable"
|
|
444
|
+
};
|
|
445
|
+
options.log?.("[chat-routes] model upstream unavailable; falling over", { ...info });
|
|
446
|
+
options.onFallback?.(info);
|
|
447
|
+
}
|
|
448
|
+
}).finally(() => progress.close());
|
|
449
|
+
settled.catch(() => void 0);
|
|
450
|
+
for (; ; ) {
|
|
451
|
+
const item = await progress.pull();
|
|
452
|
+
if (item === PROGRESS_CLOSED) break;
|
|
453
|
+
yield item;
|
|
454
|
+
}
|
|
455
|
+
try {
|
|
456
|
+
const outcome = await settled;
|
|
457
|
+
serving = outcome.model;
|
|
458
|
+
trail = outcome.attempts;
|
|
459
|
+
fellBack = outcome.usedFallback;
|
|
460
|
+
handle = outcome.value;
|
|
461
|
+
} catch (err) {
|
|
462
|
+
const attempts = err?.attempts;
|
|
463
|
+
if (Array.isArray(attempts)) trail = attempts;
|
|
464
|
+
if (err instanceof ModelFailoverExhaustedError && finalTimeout) {
|
|
465
|
+
throw new ModelFailoverTimeoutError({
|
|
466
|
+
attempts: trail,
|
|
467
|
+
...finalTimeout
|
|
468
|
+
});
|
|
469
|
+
}
|
|
470
|
+
throw err;
|
|
471
|
+
}
|
|
472
|
+
for (const event of handle.buffered) yield event;
|
|
473
|
+
const live = handle.iterator;
|
|
474
|
+
if (!live) {
|
|
475
|
+
handle.abort();
|
|
476
|
+
return;
|
|
477
|
+
}
|
|
478
|
+
try {
|
|
479
|
+
for (; ; ) {
|
|
480
|
+
const next = await live.next();
|
|
481
|
+
if (next.done) return;
|
|
482
|
+
yield next.value;
|
|
483
|
+
}
|
|
484
|
+
} finally {
|
|
485
|
+
handle.abort();
|
|
486
|
+
await closeIterator(live, options.log);
|
|
487
|
+
}
|
|
488
|
+
})();
|
|
489
|
+
return {
|
|
490
|
+
events,
|
|
491
|
+
servingModel: () => serving,
|
|
492
|
+
attempts: () => trail,
|
|
493
|
+
usedFallback: () => fellBack
|
|
494
|
+
};
|
|
495
|
+
}
|
|
496
|
+
var PROGRESS_CLOSED = /* @__PURE__ */ Symbol("progress-closed");
|
|
497
|
+
function createProgressQueue() {
|
|
498
|
+
const items = [];
|
|
499
|
+
const waiters = [];
|
|
500
|
+
let closed = false;
|
|
501
|
+
const deliver = (value) => {
|
|
502
|
+
const waiter = waiters.shift();
|
|
503
|
+
if (waiter) waiter(value);
|
|
504
|
+
else items.push(value);
|
|
505
|
+
};
|
|
506
|
+
return {
|
|
507
|
+
push(event) {
|
|
508
|
+
if (closed) return;
|
|
509
|
+
deliver(event);
|
|
510
|
+
},
|
|
511
|
+
close() {
|
|
512
|
+
if (closed) return;
|
|
513
|
+
closed = true;
|
|
514
|
+
deliver(PROGRESS_CLOSED);
|
|
515
|
+
},
|
|
516
|
+
pull() {
|
|
517
|
+
if (items.length > 0) return Promise.resolve(items.shift());
|
|
518
|
+
if (closed) return Promise.resolve(PROGRESS_CLOSED);
|
|
519
|
+
return new Promise((resolve) => waiters.push(resolve));
|
|
520
|
+
}
|
|
521
|
+
};
|
|
522
|
+
}
|
|
523
|
+
function createModelAttemptDrainer(attemptState, options, openTimeoutMs, firstResponseTimeoutMs, committing, liveLifecycle, progress) {
|
|
524
|
+
const drainOnce = async (model) => {
|
|
525
|
+
attemptState.index += 1;
|
|
526
|
+
const controller = new AbortController();
|
|
527
|
+
const abort = () => {
|
|
528
|
+
if (!controller.signal.aborted) controller.abort();
|
|
529
|
+
};
|
|
530
|
+
const opening = Promise.resolve().then(() => options.open({
|
|
531
|
+
model,
|
|
532
|
+
attempt: attemptState.index,
|
|
533
|
+
signal: controller.signal
|
|
534
|
+
}));
|
|
535
|
+
const openDeadline = deadlineAfter(openTimeoutMs);
|
|
536
|
+
let opened;
|
|
537
|
+
try {
|
|
538
|
+
opened = await Promise.race([opening, openDeadline.promise]);
|
|
539
|
+
} catch (err) {
|
|
540
|
+
openDeadline.clear();
|
|
541
|
+
abort();
|
|
542
|
+
throw err;
|
|
543
|
+
}
|
|
544
|
+
if (opened === TIMED_OUT) {
|
|
545
|
+
openDeadline.clear();
|
|
546
|
+
abort();
|
|
547
|
+
void opening.then(
|
|
548
|
+
(lateSource) => {
|
|
549
|
+
try {
|
|
550
|
+
void closeIterator(lateSource[Symbol.asyncIterator](), options.log);
|
|
551
|
+
} catch (err) {
|
|
552
|
+
options.log?.("[chat-routes] closing a late model stream threw", {
|
|
553
|
+
error: err instanceof Error ? err.message : String(err)
|
|
554
|
+
});
|
|
555
|
+
}
|
|
556
|
+
},
|
|
557
|
+
(err) => {
|
|
558
|
+
options.log?.("[chat-routes] abandoned model stream open rejected after timeout", {
|
|
559
|
+
error: err instanceof Error ? err.message : String(err)
|
|
560
|
+
});
|
|
561
|
+
}
|
|
562
|
+
);
|
|
563
|
+
return {
|
|
564
|
+
outcome: {
|
|
565
|
+
committed: false,
|
|
566
|
+
error: `Opening the model event source for ${model} exceeded ${openTimeoutMs}ms`,
|
|
567
|
+
errorCode: "model_stream_open_timeout",
|
|
568
|
+
timeoutCode: "model_stream_open_timeout"
|
|
569
|
+
},
|
|
570
|
+
empty: false
|
|
571
|
+
};
|
|
572
|
+
}
|
|
573
|
+
const source = opened;
|
|
574
|
+
let iterator;
|
|
575
|
+
try {
|
|
576
|
+
iterator = source[Symbol.asyncIterator]();
|
|
577
|
+
} catch (err) {
|
|
578
|
+
openDeadline.clear();
|
|
579
|
+
abort();
|
|
580
|
+
throw err;
|
|
581
|
+
}
|
|
582
|
+
const buffered = [];
|
|
583
|
+
let next;
|
|
584
|
+
try {
|
|
585
|
+
next = await Promise.race([
|
|
586
|
+
Promise.resolve(iterator.next()),
|
|
587
|
+
openDeadline.promise
|
|
588
|
+
]);
|
|
589
|
+
} catch (err) {
|
|
590
|
+
openDeadline.clear();
|
|
591
|
+
abort();
|
|
592
|
+
void closeIterator(iterator, options.log);
|
|
593
|
+
throw err;
|
|
594
|
+
}
|
|
595
|
+
openDeadline.clear();
|
|
596
|
+
if (next === TIMED_OUT) {
|
|
597
|
+
abort();
|
|
598
|
+
void closeIterator(iterator, options.log);
|
|
599
|
+
return {
|
|
600
|
+
outcome: {
|
|
601
|
+
committed: false,
|
|
602
|
+
error: `Starting the model event source for ${model} exceeded ${openTimeoutMs}ms`,
|
|
603
|
+
errorCode: "model_stream_open_timeout",
|
|
604
|
+
timeoutCode: "model_stream_open_timeout"
|
|
605
|
+
},
|
|
606
|
+
empty: false
|
|
607
|
+
};
|
|
608
|
+
}
|
|
609
|
+
if (next.done) {
|
|
610
|
+
abort();
|
|
611
|
+
return { outcome: { committed: true, buffered, iterator: null, abort }, empty: true };
|
|
612
|
+
}
|
|
613
|
+
const firstResponseDeadline = deadlineAfter(firstResponseTimeoutMs);
|
|
614
|
+
for (; ; ) {
|
|
615
|
+
const event = next.value;
|
|
616
|
+
let failure;
|
|
617
|
+
let commits = false;
|
|
618
|
+
try {
|
|
619
|
+
failure = classifyTerminalFailure(event);
|
|
620
|
+
commits = !failure && committing(event);
|
|
621
|
+
} catch (err) {
|
|
622
|
+
firstResponseDeadline.clear();
|
|
623
|
+
abort();
|
|
624
|
+
void closeIterator(iterator, options.log);
|
|
625
|
+
throw err;
|
|
626
|
+
}
|
|
627
|
+
if (failure?.outage) {
|
|
628
|
+
firstResponseDeadline.clear();
|
|
629
|
+
abort();
|
|
630
|
+
void closeIterator(iterator, options.log);
|
|
631
|
+
return {
|
|
632
|
+
outcome: {
|
|
633
|
+
committed: false,
|
|
634
|
+
error: failure.reason,
|
|
635
|
+
...failure.code ? { errorCode: failure.code } : {}
|
|
636
|
+
},
|
|
637
|
+
empty: false
|
|
638
|
+
};
|
|
639
|
+
}
|
|
640
|
+
if (liveLifecycle && !failure && !commits && isLiveLifecycleEvent(event)) {
|
|
641
|
+
progress.push(event);
|
|
642
|
+
} else {
|
|
643
|
+
buffered.push(event);
|
|
644
|
+
}
|
|
645
|
+
if (failure) {
|
|
646
|
+
firstResponseDeadline.clear();
|
|
647
|
+
return { outcome: { committed: true, buffered, iterator, abort }, empty: false };
|
|
648
|
+
}
|
|
649
|
+
if (commits) {
|
|
650
|
+
firstResponseDeadline.clear();
|
|
651
|
+
return { outcome: { committed: true, buffered, iterator, abort }, empty: isEmptyTerminalReceipt(event) };
|
|
652
|
+
}
|
|
653
|
+
try {
|
|
654
|
+
next = await Promise.race([
|
|
655
|
+
Promise.resolve(iterator.next()),
|
|
656
|
+
firstResponseDeadline.promise
|
|
657
|
+
]);
|
|
658
|
+
} catch (err) {
|
|
659
|
+
firstResponseDeadline.clear();
|
|
660
|
+
abort();
|
|
661
|
+
void closeIterator(iterator, options.log);
|
|
662
|
+
throw err;
|
|
663
|
+
}
|
|
664
|
+
if (next === TIMED_OUT) {
|
|
665
|
+
firstResponseDeadline.clear();
|
|
666
|
+
abort();
|
|
667
|
+
void closeIterator(iterator, options.log);
|
|
668
|
+
return {
|
|
669
|
+
outcome: {
|
|
670
|
+
committed: false,
|
|
671
|
+
error: `Model ${model} produced no first answer-bearing event within ${firstResponseTimeoutMs}ms`,
|
|
672
|
+
errorCode: "provider_first_response_timeout",
|
|
673
|
+
timeoutCode: "provider_first_response_timeout"
|
|
674
|
+
},
|
|
675
|
+
empty: false
|
|
676
|
+
};
|
|
677
|
+
}
|
|
678
|
+
if (next.done) {
|
|
679
|
+
firstResponseDeadline.clear();
|
|
680
|
+
abort();
|
|
681
|
+
return { outcome: { committed: true, buffered, iterator: null, abort }, empty: true };
|
|
682
|
+
}
|
|
683
|
+
}
|
|
684
|
+
};
|
|
685
|
+
return { drainOnce };
|
|
686
|
+
}
|
|
687
|
+
|
|
688
|
+
// src/chat-routes/turn-event-stream.ts
|
|
689
|
+
var WITHHELD_FAILURE = /* @__PURE__ */ Symbol("withheld failure");
|
|
690
|
+
function serverFailureData(data) {
|
|
691
|
+
const original = data?.[WITHHELD_FAILURE];
|
|
692
|
+
return original !== null && typeof original === "object" ? original : data;
|
|
693
|
+
}
|
|
694
|
+
function failureReasonOf(data) {
|
|
695
|
+
if (!data) return void 0;
|
|
696
|
+
const message = data.message ?? data.error ?? data.reason;
|
|
697
|
+
if (typeof message === "string" && message.length > 0) return message;
|
|
698
|
+
return void 0;
|
|
699
|
+
}
|
|
700
|
+
function failureCodeOf(data) {
|
|
701
|
+
const code = data?.code;
|
|
702
|
+
return typeof code === "string" && code.length > 0 ? code : void 0;
|
|
703
|
+
}
|
|
704
|
+
function isVisibleText(event) {
|
|
705
|
+
const value = event;
|
|
706
|
+
if (event.type === "text") return typeof value.text === "string" ? value.text.trim().length > 0 : true;
|
|
707
|
+
if (event.type !== "message.part.updated") return false;
|
|
708
|
+
const part = event.data?.part;
|
|
709
|
+
const delta = event.data?.delta;
|
|
710
|
+
return part?.type === "text" && typeof delta === "string" && delta.trim().length > 0;
|
|
711
|
+
}
|
|
712
|
+
function isVisibleOutput(event) {
|
|
713
|
+
if (isVisibleText(event)) return true;
|
|
714
|
+
const value = event;
|
|
715
|
+
if (event.type === "reasoning") return typeof value.text === "string" && value.text.trim().length > 0;
|
|
716
|
+
if (event.type === "tool_call" || event.type === "tool.call" || event.type === "tool_result" || event.type === "tool.result" || event.type === "tool.event") return true;
|
|
717
|
+
if (event.type !== "message.part.updated") return false;
|
|
718
|
+
const part = event.data?.part;
|
|
719
|
+
if (part?.type === "tool") return true;
|
|
720
|
+
if (part?.type !== "reasoning" && part?.type !== "text") return false;
|
|
721
|
+
const text = event.data?.delta ?? part.text;
|
|
722
|
+
return typeof text === "string" && text.trim().length > 0;
|
|
723
|
+
}
|
|
724
|
+
async function* tapRawEvents(source, onRawEvent, log) {
|
|
725
|
+
for await (const event of source) {
|
|
726
|
+
try {
|
|
727
|
+
await onRawEvent(event);
|
|
728
|
+
} catch (err) {
|
|
729
|
+
log("[chat-routes] onRawEvent failed", { error: err instanceof Error ? err.message : String(err) });
|
|
730
|
+
}
|
|
731
|
+
yield event;
|
|
732
|
+
}
|
|
733
|
+
}
|
|
734
|
+
async function* withStreamHeartbeat(source, intervalMs, makeEvent) {
|
|
735
|
+
const iterator = source[Symbol.asyncIterator]();
|
|
736
|
+
try {
|
|
737
|
+
let pending = iterator.next();
|
|
738
|
+
let windowStart = Date.now();
|
|
739
|
+
let tick = 0;
|
|
740
|
+
for (; ; ) {
|
|
741
|
+
let timer;
|
|
742
|
+
let winner;
|
|
743
|
+
try {
|
|
744
|
+
const heartbeat = new Promise((resolve) => {
|
|
745
|
+
timer = setTimeout(() => resolve("heartbeat"), intervalMs);
|
|
746
|
+
});
|
|
747
|
+
winner = await Promise.race([pending.then(() => "event"), heartbeat]);
|
|
748
|
+
} finally {
|
|
749
|
+
if (timer !== void 0) clearTimeout(timer);
|
|
750
|
+
}
|
|
751
|
+
if (winner === "heartbeat") {
|
|
752
|
+
tick += 1;
|
|
753
|
+
yield makeEvent({ elapsedMs: Date.now() - windowStart, tick });
|
|
754
|
+
continue;
|
|
755
|
+
}
|
|
756
|
+
const result = await pending;
|
|
757
|
+
if (result.done) return;
|
|
758
|
+
yield result.value;
|
|
759
|
+
pending = iterator.next();
|
|
760
|
+
windowStart = Date.now();
|
|
761
|
+
tick = 0;
|
|
762
|
+
}
|
|
763
|
+
} finally {
|
|
764
|
+
await iterator.return?.();
|
|
765
|
+
}
|
|
766
|
+
}
|
|
767
|
+
function concatStreams(streams) {
|
|
768
|
+
let index = 0;
|
|
769
|
+
let reader = null;
|
|
770
|
+
return new ReadableStream({
|
|
771
|
+
async pull(controller) {
|
|
772
|
+
for (; ; ) {
|
|
773
|
+
if (!reader) {
|
|
774
|
+
const next = streams[index++];
|
|
775
|
+
if (!next) {
|
|
776
|
+
controller.close();
|
|
777
|
+
return;
|
|
778
|
+
}
|
|
779
|
+
reader = next.getReader();
|
|
780
|
+
}
|
|
781
|
+
const { done, value } = await reader.read();
|
|
782
|
+
if (done) {
|
|
783
|
+
reader = null;
|
|
784
|
+
continue;
|
|
785
|
+
}
|
|
786
|
+
controller.enqueue(value);
|
|
787
|
+
return;
|
|
788
|
+
}
|
|
789
|
+
},
|
|
790
|
+
async cancel(reason) {
|
|
791
|
+
await reader?.cancel(reason);
|
|
792
|
+
for (const stream of streams.slice(index)) await stream.cancel(reason);
|
|
793
|
+
}
|
|
794
|
+
});
|
|
795
|
+
}
|
|
796
|
+
async function* abortOnSignal(source, signal) {
|
|
797
|
+
const iterator = source[Symbol.asyncIterator]();
|
|
798
|
+
try {
|
|
799
|
+
for (; ; ) {
|
|
800
|
+
const next = await nextWithAbort(iterator, signal);
|
|
801
|
+
if (next.done) return;
|
|
802
|
+
yield next.value;
|
|
803
|
+
}
|
|
804
|
+
} finally {
|
|
805
|
+
await closeIteratorAfterAbort(iterator);
|
|
806
|
+
}
|
|
807
|
+
}
|
|
808
|
+
function nextWithAbort(iterator, signal) {
|
|
809
|
+
if (signal.aborted) return Promise.reject(new Error("chat turn cancelled"));
|
|
810
|
+
return new Promise((resolve, reject) => {
|
|
811
|
+
let settled = false;
|
|
812
|
+
const cleanup = () => signal.removeEventListener("abort", onAbort);
|
|
813
|
+
const onAbort = () => {
|
|
814
|
+
if (settled) return;
|
|
815
|
+
settled = true;
|
|
816
|
+
cleanup();
|
|
817
|
+
reject(new Error("chat turn cancelled"));
|
|
818
|
+
};
|
|
819
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
820
|
+
if (signal.aborted) {
|
|
821
|
+
onAbort();
|
|
822
|
+
return;
|
|
823
|
+
}
|
|
824
|
+
Promise.resolve().then(() => iterator.next()).then(
|
|
825
|
+
(result) => {
|
|
826
|
+
if (settled) return;
|
|
827
|
+
settled = true;
|
|
828
|
+
cleanup();
|
|
829
|
+
resolve(result);
|
|
830
|
+
},
|
|
831
|
+
(error) => {
|
|
832
|
+
if (settled) return;
|
|
833
|
+
settled = true;
|
|
834
|
+
cleanup();
|
|
835
|
+
reject(error);
|
|
836
|
+
}
|
|
837
|
+
);
|
|
838
|
+
});
|
|
839
|
+
}
|
|
840
|
+
var ABORT_CLEANUP_TIMEOUT_MS = 50;
|
|
841
|
+
async function closeIteratorAfterAbort(iterator) {
|
|
842
|
+
let closing;
|
|
843
|
+
try {
|
|
844
|
+
const result = iterator.return?.();
|
|
845
|
+
if (result) closing = Promise.resolve(result);
|
|
846
|
+
} catch {
|
|
847
|
+
return;
|
|
848
|
+
}
|
|
849
|
+
if (!closing) return;
|
|
850
|
+
let timeout;
|
|
851
|
+
try {
|
|
852
|
+
await Promise.race([
|
|
853
|
+
Promise.resolve(closing).catch(() => void 0),
|
|
854
|
+
new Promise((resolve) => {
|
|
855
|
+
timeout = setTimeout(resolve, ABORT_CLEANUP_TIMEOUT_MS);
|
|
856
|
+
})
|
|
857
|
+
]);
|
|
858
|
+
} finally {
|
|
859
|
+
if (timeout !== void 0) clearTimeout(timeout);
|
|
860
|
+
}
|
|
861
|
+
}
|
|
862
|
+
|
|
863
|
+
// src/chat-routes/failure-wording.ts
|
|
864
|
+
var utcTime = (at) => `${at.toISOString().slice(11, 19)} UTC`;
|
|
865
|
+
var FAILED_TURN_TEXT = "That answer stopped before it finished. Please send your question again.";
|
|
866
|
+
var ANSWER_COMPLETE_TEXT = "The answer above is complete.";
|
|
867
|
+
var MODEL_KEY_REFUSED_TEXT = "The assistant's model refused its access key, so this message was not answered. If the assistant has just started, send it again in a minute; if this repeats, tell the team.";
|
|
868
|
+
var BUDGET_TEXT = "This assistant's spending budget is used up. Raise its cap or wait for it to reset, then send again.";
|
|
869
|
+
var BALANCE_TEXT = "Insufficient credits to cover this request. Add funds, then send again.";
|
|
870
|
+
var MODEL_POLICY_TEXT = "That model isn't available here. Choose another model and send again.";
|
|
871
|
+
var RETRY_TEXT = "The model provider is busy, so the request is being retried.";
|
|
872
|
+
function providerRetryNotice(event) {
|
|
873
|
+
const retry = event.type === "raw" ? asRecord(asRecord(event.data)?.event) : void 0;
|
|
874
|
+
if (retry?.type !== "retry" || !(Number(retry.next) > 0)) return void 0;
|
|
875
|
+
return `The model provider is busy; retrying at ${utcTime(new Date(Number(retry.next)))}.`;
|
|
876
|
+
}
|
|
877
|
+
function providerResetSentence(message) {
|
|
878
|
+
const at = new Date(message.match(/reset at (\d{4}-\d{2}-\d{2}T[\d:.]+Z)/)?.[1] ?? NaN);
|
|
879
|
+
if (Number.isNaN(at.getTime())) return void 0;
|
|
880
|
+
return `The model provider asked us to wait. Try again after ${utcTime(at)} on ${at.toISOString().slice(0, 10)}.`;
|
|
881
|
+
}
|
|
882
|
+
var MODEL_KEY_REFUSED_CODE = "model.key_refused";
|
|
883
|
+
function modelKeyRefusedSentence(message) {
|
|
884
|
+
const lower = message.toLowerCase();
|
|
885
|
+
const refused = lower.includes("invalid api key") || /\b401\b/.test(message) && (lower.includes("unauthorized") || lower.includes("unauthorised") || lower.includes("api key"));
|
|
886
|
+
return refused ? MODEL_KEY_REFUSED_TEXT : void 0;
|
|
887
|
+
}
|
|
888
|
+
var ROUTER_BUDGET = [
|
|
889
|
+
"This key's monthly budget is exhausted",
|
|
890
|
+
"This key's lifetime budget is exhausted",
|
|
891
|
+
"This key's remaining budget, after other in-flight requests, is below",
|
|
892
|
+
"The key or product budget cannot cover"
|
|
893
|
+
];
|
|
894
|
+
var ROUTER_BALANCE = ["Your available balance, after other in-flight requests, is below", "Available credits cannot cover"];
|
|
895
|
+
var REFUSALS = /* @__PURE__ */ new Map([
|
|
896
|
+
["MODEL_REFUSED", { class: "model_policy", shown: MODEL_POLICY_TEXT }],
|
|
897
|
+
["model_policy_denied", { class: "model_policy", shown: MODEL_POLICY_TEXT }],
|
|
898
|
+
["grant_model_unsupported", {
|
|
899
|
+
class: "grant_model_unsupported",
|
|
900
|
+
shown: "This assistant's model can't run here. Ask the team to choose a supported model."
|
|
901
|
+
}],
|
|
902
|
+
["grant_activation_missing", {
|
|
903
|
+
class: "grant_activation_missing",
|
|
904
|
+
shown: "This assistant couldn't connect to its model. Tell the team; sending again won't help."
|
|
905
|
+
}],
|
|
906
|
+
["profile_unsupported", { class: "profile_unsupported", shown: "This assistant's setup can't run here. Ask the team to update it." }]
|
|
907
|
+
]);
|
|
908
|
+
var LED_CLASSES = /* @__PURE__ */ new Set([
|
|
909
|
+
"funding",
|
|
910
|
+
"balance_low",
|
|
911
|
+
"model_policy",
|
|
912
|
+
"grant_model_unsupported",
|
|
913
|
+
"grant_activation_missing",
|
|
914
|
+
"profile_unsupported"
|
|
915
|
+
]);
|
|
916
|
+
var WARNING_SENTENCES = /* @__PURE__ */ new Map([
|
|
917
|
+
["empty_visible_output_retry", "The model returned no visible answer, so it is trying once more."],
|
|
918
|
+
["OPENCODE_STEP_TRUNCATED", "A step of this answer was cut short, so the answer may be incomplete."],
|
|
919
|
+
["OPENCODE_PROVIDER_RETRY", RETRY_TEXT],
|
|
920
|
+
["CODEX_UNEXPECTED_APPROVAL", "The agent asked for an approval it does not need here, and it was declined."],
|
|
921
|
+
["CODEX_UNEXPECTED_QUESTION", "The agent asked a question it cannot ask here, and it was declined."],
|
|
922
|
+
["EXECUTION_INTERRUPTED", "This answer was interrupted before it finished."],
|
|
923
|
+
["FILE_DENIED", "The agent was refused access to a file."]
|
|
924
|
+
]);
|
|
925
|
+
var FAILOVER_TRAIL = /^All \d+ model\(s\) failed\. /;
|
|
926
|
+
function lastAttempt(reason) {
|
|
927
|
+
if (!FAILOVER_TRAIL.test(reason)) return reason;
|
|
928
|
+
const attempts = reason.replace(FAILOVER_TRAIL, "");
|
|
929
|
+
const last = attempts.slice(attempts.lastIndexOf(" | ") + 1).replace(/^\| /, "");
|
|
930
|
+
const colon = last.indexOf(": ");
|
|
931
|
+
return colon < 0 ? last : last.slice(colon + 2);
|
|
932
|
+
}
|
|
933
|
+
function classify(reason, code, status) {
|
|
934
|
+
const last = lastAttempt(reason);
|
|
935
|
+
const wait = providerResetSentence(last);
|
|
936
|
+
if (wait) return { class: "provider_wait", shown: wait };
|
|
937
|
+
if (modelKeyRefusedSentence(last)) return { class: "model_key_refused", shown: MODEL_KEY_REFUSED_TEXT };
|
|
938
|
+
if (ROUTER_BUDGET.some((phrase) => last.includes(phrase))) return { class: "budget_exhausted", shown: BUDGET_TEXT };
|
|
939
|
+
if (ROUTER_BALANCE.some((phrase) => last.includes(phrase))) return { class: "balance_low", shown: BALANCE_TEXT };
|
|
940
|
+
const refusal = REFUSALS.get(code ?? "");
|
|
941
|
+
if (refusal) return refusal;
|
|
942
|
+
const recovery = getChatFundingRecovery({ code, message: last }) ?? (Number(status) === 402 || code === "PAYMENT_REQUIRED" ? getChatFundingRecovery({ code: "insufficient_credits" }) : null);
|
|
943
|
+
if (!recovery) return void 0;
|
|
944
|
+
const paidAccess = recovery.reason === "paid_access_required" ? "Inference requires verified paid access. " : "";
|
|
945
|
+
return { class: "funding", shown: `${recovery.title}. ${paidAccess}${recovery.message}` };
|
|
946
|
+
}
|
|
947
|
+
function failureIds(source) {
|
|
948
|
+
const record = asRecord(source);
|
|
949
|
+
return Object.fromEntries(["turnId", "threadId", "sessionId", "executionId"].flatMap((key) => typeof record?.[key] === "string" && record[key] ? [[key, record[key]]] : []));
|
|
950
|
+
}
|
|
951
|
+
function failureText(view, partialAnswer) {
|
|
952
|
+
if (!LED_CLASSES.has(view.class)) return view.shown;
|
|
953
|
+
const lead = partialAnswer.trim() ? "The sandbox model stream stopped before a clean completion." : "The sandbox agent returned an error before producing a visible answer.";
|
|
954
|
+
return `${lead}
|
|
955
|
+
|
|
956
|
+
Error: ${view.shown}`;
|
|
957
|
+
}
|
|
958
|
+
var CODE_VALUE = /^[a-z0-9_.]+$/;
|
|
959
|
+
function failureReason(event) {
|
|
960
|
+
const record = asRecord(event);
|
|
961
|
+
const data = asRecord(record?.data);
|
|
962
|
+
const error = asRecord(data?.error);
|
|
963
|
+
const sessionError = asRecord(asRecord(data?.properties)?.error);
|
|
964
|
+
const raw = asRecord(data?.event);
|
|
965
|
+
const rawError = asRecord(raw?.error);
|
|
966
|
+
const prose = typeof data?.reason === "string" && !CODE_VALUE.test(data.reason) ? asString(data.reason) : void 0;
|
|
967
|
+
return asString(data?.message) ?? asString(data?.error) ?? asString(data?.detail) ?? prose ?? asString(error?.message) ?? asString(asRecord(error?.data)?.message) ?? asString(sessionError?.message) ?? asString(asRecord(sessionError?.data)?.message) ?? asString(record?.error) ?? asString(asRecord(record?.error)?.message) ?? asString(record?.message) ?? asString(record?.details) ?? asString(raw?.message) ?? asString(raw?.error) ?? asString(rawError?.message) ?? asString(asRecord(rawError?.data)?.message) ?? asString(asRecord(raw?.item)?.message) ?? "";
|
|
968
|
+
}
|
|
969
|
+
var FAILURE_TYPES = /* @__PURE__ */ new Set(["error", "warning", "session.run.failed", "session.error"]);
|
|
970
|
+
var DETAIL_KEYS = /* @__PURE__ */ new Set(["code", "statusCode", "retryable", "retryAt", "class"]);
|
|
971
|
+
function shownFailure(event, shown, reason) {
|
|
972
|
+
const data = asRecord(event.data) ?? {};
|
|
973
|
+
const visible = { ...data };
|
|
974
|
+
const shownError = (value) => typeof value === "string" || asRecord(value) ? { ...typeof asRecord(value)?.code === "string" ? { code: asRecord(value).code } : {}, message: shown } : value;
|
|
975
|
+
for (const key of ["message", "detail"]) if (key in visible) visible[key] = shown;
|
|
976
|
+
if ("error" in visible) visible.error = typeof visible.error === "string" ? shown : shownError(visible.error);
|
|
977
|
+
if (typeof visible.reason === "string" && !CODE_VALUE.test(visible.reason)) visible.reason = shown;
|
|
978
|
+
const properties = asRecord(data.properties);
|
|
979
|
+
if (properties && "error" in properties) visible.properties = { ...properties, error: shownError(properties.error) };
|
|
980
|
+
if (event.type === "error" || event.type === "session.run.failed") visible.message = shown;
|
|
981
|
+
const details = Object.entries(asRecord(data.details) ?? {}).filter(([key]) => DETAIL_KEYS.has(key));
|
|
982
|
+
delete visible.details;
|
|
983
|
+
if (details.length > 0) visible.details = Object.fromEntries(details);
|
|
984
|
+
const original = typeof data.message === "string" ? data : { ...data, message: reason };
|
|
985
|
+
const out = { ...event, data: Object.assign(visible, { [WITHHELD_FAILURE]: original }) };
|
|
986
|
+
if ("error" in event) out.error = typeof event.error === "string" ? shown : shownError(event.error);
|
|
987
|
+
if ("message" in event) out.message = shown;
|
|
988
|
+
delete out.details;
|
|
989
|
+
return out;
|
|
990
|
+
}
|
|
991
|
+
function personFacing(options) {
|
|
992
|
+
const views = /* @__PURE__ */ new Map();
|
|
993
|
+
const droppedWarnings = /* @__PURE__ */ new Set();
|
|
994
|
+
const warn = (message, meta) => (options.log ?? console.warn)(message, meta);
|
|
995
|
+
const view = (reason, code, status, meta = {}) => {
|
|
996
|
+
const key = [reason, code, Number(status) === 402].join("\n");
|
|
997
|
+
const known = views.get(key);
|
|
998
|
+
if (known) return known;
|
|
999
|
+
const settled = code?.startsWith("settlement_") ?? false;
|
|
1000
|
+
const seen = classify(reason, code, status) ?? (settled && options.hasAnswer?.() ? { class: "settlement", shown: ANSWER_COMPLETE_TEXT } : void 0) ?? { class: settled ? "settlement" : "runtime_failure", shown: options.failedTurnText };
|
|
1001
|
+
warn("[chat-routes] turn failure withheld from the person", { failureClass: seen.class, code, error: reason, ...meta });
|
|
1002
|
+
views.set(key, seen);
|
|
1003
|
+
return seen;
|
|
1004
|
+
};
|
|
1005
|
+
const warning = (code, message, meta = {}) => {
|
|
1006
|
+
const shown = WARNING_SENTENCES.get(code ?? "");
|
|
1007
|
+
if (shown || droppedWarnings.has(`${code}
|
|
1008
|
+
${message}`)) return shown;
|
|
1009
|
+
droppedWarnings.add(`${code}
|
|
1010
|
+
${message}`);
|
|
1011
|
+
warn("[chat-routes] harness warning withheld from the person", { failureClass: "harness_warning", code, error: message, ...meta });
|
|
1012
|
+
return void 0;
|
|
1013
|
+
};
|
|
1014
|
+
const event = (event2) => {
|
|
1015
|
+
const data = asRecord(event2.data);
|
|
1016
|
+
const raw = event2.type === "raw" ? asRecord(data?.event) : void 0;
|
|
1017
|
+
if (raw) {
|
|
1018
|
+
const part = asRecord(raw.part);
|
|
1019
|
+
if (part?.type === "tool") return { ...event2, data: { ...data, event: { ...raw, part: withoutToolList(part) } } };
|
|
1020
|
+
const item = asRecord(raw.item)?.type === "error" ? asRecord(raw.item) : void 0;
|
|
1021
|
+
if (raw.type !== "error" && raw.type !== "retry" && raw.type !== "turn.failed" && !("error" in raw) && !item) return event2;
|
|
1022
|
+
const shown = raw.type === "retry" ? RETRY_TEXT : view(failureReason(event2), void 0, void 0, failureIds(data)).shown;
|
|
1023
|
+
return { ...event2, data: { ...data, event: {
|
|
1024
|
+
...raw,
|
|
1025
|
+
..."message" in raw ? { message: shown } : {},
|
|
1026
|
+
..."error" in raw ? { error: { message: shown } } : {},
|
|
1027
|
+
...item ? { item: { type: "error", message: shown } } : {}
|
|
1028
|
+
} } };
|
|
1029
|
+
}
|
|
1030
|
+
const failed = (event2.type === "done" || event2.type === "status") && data?.status === "failed";
|
|
1031
|
+
if (!failed && !FAILURE_TYPES.has(String(event2.type))) return event2;
|
|
1032
|
+
const reason = failureReason(event2);
|
|
1033
|
+
if (event2.type === "warning") {
|
|
1034
|
+
const shown = warning(asString(data?.code), reason, failureIds(data));
|
|
1035
|
+
return shown ? shownFailure(event2, shown, reason) : void 0;
|
|
1036
|
+
}
|
|
1037
|
+
const status = data?.statusCode ?? (failed ? void 0 : data?.status);
|
|
1038
|
+
return shownFailure(event2, view(reason, asString(data?.code), status, failureIds(data)).shown, reason);
|
|
1039
|
+
};
|
|
1040
|
+
return { view, warning, event };
|
|
1041
|
+
}
|
|
1042
|
+
function noModelAllowed(error) {
|
|
1043
|
+
return error.attempts.length > 0 && error.attempts.every((attempt) => attempt.reason?.includes(" is not admitted by policy: "));
|
|
1044
|
+
}
|
|
1045
|
+
function sandboxStreamFailureDiagnostic(error) {
|
|
1046
|
+
const streamMessage = error?.streamMessage;
|
|
1047
|
+
const message = typeof streamMessage === "string" ? streamMessage : error instanceof Error ? error.message : "Sandbox stream failed";
|
|
1048
|
+
const diagnostics = asRecord(error?.diagnostics);
|
|
1049
|
+
let diagnosticText;
|
|
1050
|
+
if (diagnostics) {
|
|
1051
|
+
try {
|
|
1052
|
+
diagnosticText = JSON.stringify(diagnostics);
|
|
1053
|
+
} catch {
|
|
1054
|
+
diagnosticText = "[unserializable diagnostics]";
|
|
1055
|
+
}
|
|
1056
|
+
}
|
|
1057
|
+
const failureNote = diagnosticText ? `sandbox-stream: ${message}; ${diagnosticText}` : `sandbox-stream: ${message}`;
|
|
1058
|
+
const ownCode = asString(asRecord(error)?.code);
|
|
1059
|
+
let code = "sandbox.stream_failed";
|
|
1060
|
+
if (error instanceof SandboxRunError) {
|
|
1061
|
+
code = error.code;
|
|
1062
|
+
} else if (error instanceof ModelFailoverTimeoutError) {
|
|
1063
|
+
code = error.code;
|
|
1064
|
+
} else if (error instanceof ModelFailoverExhaustedError) {
|
|
1065
|
+
code = noModelAllowed(error) ? "model_policy_denied" : error.upstreamCode ?? code;
|
|
1066
|
+
} else if (ownCode && (REFUSALS.has(ownCode) || ownCode === "PAYMENT_REQUIRED" || ownCode.startsWith("settlement_"))) {
|
|
1067
|
+
code = ownCode;
|
|
1068
|
+
}
|
|
1069
|
+
if (modelKeyRefusedSentence(message)) code = MODEL_KEY_REFUSED_CODE;
|
|
1070
|
+
return { failureNote, code, reason: message };
|
|
1071
|
+
}
|
|
1072
|
+
function sandboxStreamErrorCode(data) {
|
|
1073
|
+
return asString(asRecord(data)?.code) ?? "sandbox.stream_error";
|
|
1074
|
+
}
|
|
1075
|
+
var UNAVAILABLE_TOOL_TEXT = "That tool isn't available.";
|
|
1076
|
+
function withoutToolListTail(text) {
|
|
1077
|
+
const at = text.indexOf("Available tools:");
|
|
1078
|
+
return at < 0 ? text : text.slice(0, at).trimEnd();
|
|
1079
|
+
}
|
|
1080
|
+
function withoutToolList(part) {
|
|
1081
|
+
const nested = asRecord(part.state);
|
|
1082
|
+
const state = nested ?? part;
|
|
1083
|
+
const invalid = part.tool === "invalid";
|
|
1084
|
+
const shown = { ...state };
|
|
1085
|
+
for (const key of ["error", "output"]) {
|
|
1086
|
+
if (typeof shown[key] !== "string" || key === "output" && !invalid && state.status !== "error") continue;
|
|
1087
|
+
shown[key] = invalid ? UNAVAILABLE_TOOL_TEXT : withoutToolListTail(shown[key]);
|
|
1088
|
+
}
|
|
1089
|
+
if (invalid) shown.input = { ...asRecord(state.input), error: UNAVAILABLE_TOOL_TEXT };
|
|
1090
|
+
return nested ? { ...part, state: shown } : shown;
|
|
1091
|
+
}
|
|
1092
|
+
|
|
1093
|
+
export {
|
|
1094
|
+
TURN_USAGE_KEYS,
|
|
1095
|
+
measuredUsage,
|
|
1096
|
+
addStepFinishUsage,
|
|
1097
|
+
transformVisibleTextParts,
|
|
1098
|
+
isDraftContentEvent,
|
|
1099
|
+
createAssistantDraftWriter,
|
|
1100
|
+
storeSupportsDraftPersistence,
|
|
1101
|
+
rowIdOf,
|
|
1102
|
+
assistantRowIdForTurn,
|
|
1103
|
+
WITHHELD_FAILURE,
|
|
1104
|
+
serverFailureData,
|
|
1105
|
+
failureReasonOf,
|
|
1106
|
+
failureCodeOf,
|
|
1107
|
+
isVisibleText,
|
|
1108
|
+
isVisibleOutput,
|
|
1109
|
+
tapRawEvents,
|
|
1110
|
+
withStreamHeartbeat,
|
|
1111
|
+
concatStreams,
|
|
1112
|
+
abortOnSignal,
|
|
1113
|
+
isCommittingSandboxEvent,
|
|
1114
|
+
summarizeFailoverReason,
|
|
1115
|
+
isLiveLifecycleEvent,
|
|
1116
|
+
classifyTerminalFailure,
|
|
1117
|
+
DEFAULT_MODEL_STREAM_OPEN_TIMEOUT_MS,
|
|
1118
|
+
DEFAULT_MODEL_FIRST_RESPONSE_TIMEOUT_MS,
|
|
1119
|
+
ModelFailoverTimeoutError,
|
|
1120
|
+
MAX_EMPTY_TURN_RETRIES,
|
|
1121
|
+
resolveEmptyTurnRetries,
|
|
1122
|
+
streamWithModelFailover,
|
|
1123
|
+
FAILED_TURN_TEXT,
|
|
1124
|
+
providerRetryNotice,
|
|
1125
|
+
providerResetSentence,
|
|
1126
|
+
MODEL_KEY_REFUSED_CODE,
|
|
1127
|
+
modelKeyRefusedSentence,
|
|
1128
|
+
failureIds,
|
|
1129
|
+
failureText,
|
|
1130
|
+
failureReason,
|
|
1131
|
+
personFacing,
|
|
1132
|
+
sandboxStreamFailureDiagnostic,
|
|
1133
|
+
sandboxStreamErrorCode,
|
|
1134
|
+
withoutToolList
|
|
1135
|
+
};
|
|
1136
|
+
//# sourceMappingURL=chunk-RNDWCT4O.js.map
|