@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.
@@ -1,6 +1,3 @@
1
- import {
2
- getChatFundingRecovery
3
- } from "./chunk-QUV5M7SZ.js";
4
1
  import {
5
2
  createInteractionAnswerRoute
6
3
  } from "./chunk-SBHFMDR7.js";
@@ -16,7 +13,6 @@ import {
16
13
  REDACTED
17
14
  } from "./chunk-M4QITAQV.js";
18
15
  import {
19
- SandboxRunError,
20
16
  createComputerStep,
21
17
  createD1PrewarmClaimStore,
22
18
  createEscalationSpeculator,
@@ -34,19 +30,41 @@ import {
34
30
  sandboxRunFailureDetail,
35
31
  streamSandboxPrompt,
36
32
  turnStorageBindings
37
- } from "./chunk-AYTNVTDP.js";
33
+ } from "./chunk-QMQB725F.js";
38
34
  import {
39
35
  redactErrorMessage
40
36
  } from "./chunk-MH74DY2I.js";
41
37
  import {
38
+ FAILED_TURN_TEXT,
39
+ MODEL_KEY_REFUSED_CODE,
40
+ WITHHELD_FAILURE,
41
+ abortOnSignal,
42
42
  addStepFinishUsage,
43
43
  assistantRowIdForTurn,
44
+ concatStreams,
44
45
  createAssistantDraftWriter,
46
+ failureCodeOf,
47
+ failureIds,
48
+ failureReason,
49
+ failureReasonOf,
50
+ failureText,
51
+ isVisibleOutput,
52
+ isVisibleText,
45
53
  measuredUsage,
54
+ personFacing,
55
+ providerRetryNotice,
56
+ resolveEmptyTurnRetries,
46
57
  rowIdOf,
58
+ sandboxStreamErrorCode,
59
+ sandboxStreamFailureDiagnostic,
60
+ serverFailureData,
47
61
  storeSupportsDraftPersistence,
48
- transformVisibleTextParts
49
- } from "./chunk-K4YIDNX5.js";
62
+ streamWithModelFailover,
63
+ tapRawEvents,
64
+ transformVisibleTextParts,
65
+ withStreamHeartbeat,
66
+ withoutToolList
67
+ } from "./chunk-RNDWCT4O.js";
50
68
  import {
51
69
  normalizeClientTurnId,
52
70
  resolveChatTurn
@@ -78,6 +96,9 @@ import {
78
96
  parseInteractionCancel,
79
97
  parseInteractionRequest
80
98
  } from "./chunk-TB6KNPFI.js";
99
+ import {
100
+ SandboxRunError
101
+ } from "./chunk-2IGXFHUO.js";
81
102
  import {
82
103
  parseJsonObjectBody
83
104
  } from "./chunk-ALIWB2P3.js";
@@ -112,12 +133,7 @@ import {
112
133
  turnResponseEventFor
113
134
  } from "./chunk-K2JPSXKI.js";
114
135
  import {
115
- ModelFailoverExhaustedError,
116
- buildModelChain,
117
- isUpstreamUnavailable,
118
- readHttpStatusHint,
119
- readUpstreamUnavailableCode,
120
- runWithModelFailover
136
+ buildModelChain
121
137
  } from "./chunk-L3QR274L.js";
122
138
  import {
123
139
  assertPolicy,
@@ -152,7 +168,14 @@ function valueRedactor(values) {
152
168
  const secrets = [...new Set((values ?? []).filter((v) => typeof v === "string" && v.length >= MIN_SECRET_LENGTH))].sort((a, b) => b.length - a.length);
153
169
  if (secrets.length === 0) return void 0;
154
170
  const text = (s) => secrets.some((v) => s.includes(v)) ? secrets.reduce((acc, v) => acc.split(v).join(REDACTED), s) : s;
155
- const walk = (v) => typeof v === "string" ? text(v) : Array.isArray(v) ? v.map(walk) : v !== null && typeof v === "object" ? Object.fromEntries(Object.entries(v).map(([k, x]) => [text(k), walk(x)])) : v;
171
+ const walk = (v) => {
172
+ if (typeof v === "string") return text(v);
173
+ if (Array.isArray(v)) return v.map(walk);
174
+ if (v === null || typeof v !== "object") return v;
175
+ const out = Object.fromEntries(Object.entries(v).map(([k, x]) => [text(k), walk(x)]));
176
+ const withheld = v[WITHHELD_FAILURE];
177
+ return withheld === void 0 ? out : Object.assign(out, { [WITHHELD_FAILURE]: walk(withheld) });
178
+ };
156
179
  return walk;
157
180
  }
158
181
  async function* redactEvents(source, redact) {
@@ -954,738 +977,6 @@ ${extra}` : { ...prompt, stable: [...prompt.stable ?? [], extra] }),
954
977
  }
955
978
  }
956
979
 
957
- // src/chat-routes/model-failover-stream.ts
958
- var TERMINAL_FAILURE_TYPES = /* @__PURE__ */ new Set(["error", "session.run.failed"]);
959
- var NON_COMMITTING_PART_TYPES = /* @__PURE__ */ new Set(["step-start", "step-finish"]);
960
- function isCommittingSandboxEvent(event) {
961
- const record = asRecord(event);
962
- if (!record) return false;
963
- const type = asString(record.type) ?? "";
964
- if (!type) return false;
965
- if (type === "message.part.updated") {
966
- const data = asRecord(record.data);
967
- const part = asRecord(data?.part);
968
- const partType = asString(part?.type) ?? "";
969
- if (NON_COMMITTING_PART_TYPES.has(partType)) return false;
970
- if (partType === "text" || partType === "reasoning") {
971
- return [data?.delta, part?.text, part?.content].some((value) => typeof value === "string" && value.length > 0);
972
- }
973
- return true;
974
- }
975
- if (type === "start" || type === "execution.started" || type === "status" || type === "model-processing" || type === "model.processing" || // Public Sandbox timing metadata: a request was attempted, not answered.
976
- type === "model.first_request" || type === "session.created" || type === "session.updated" || type === "session.idle" || type === "step-start" || type === "step-finish" || type === "turn" || type === "warning") {
977
- return false;
978
- }
979
- return true;
980
- }
981
- function summarizeFailoverReason(reason) {
982
- const collapsed = reason.replace(/\s+/g, " ").trim();
983
- if (!collapsed) return "upstream unavailable";
984
- if (/<!doctype html|<html[\s>]/i.test(collapsed)) {
985
- const status = readHttpStatusHint(collapsed);
986
- return status ? `HTTP ${status}` : "upstream returned an error page";
987
- }
988
- const LIMIT = 160;
989
- if (collapsed.length <= LIMIT) return collapsed;
990
- const clipped = collapsed.slice(0, LIMIT);
991
- const lastSpace = clipped.lastIndexOf(" ");
992
- return `${(lastSpace > LIMIT - 30 ? clipped.slice(0, lastSpace) : clipped).trimEnd()}\u2026`;
993
- }
994
- function isLiveLifecycleEvent(event) {
995
- const type = asString(asRecord(event)?.type) ?? "";
996
- return type === "start" || type === "execution.started" || type === "status" || type === "model-processing" || type === "model.processing" || // Public Sandbox timing metadata: a request was attempted, not answered.
997
- type === "model.first_request" || type === "session.created" || type === "session.updated" || type === "session.idle";
998
- }
999
- function classifyTerminalFailure(event) {
1000
- const record = asRecord(event);
1001
- if (!record) return null;
1002
- const type = asString(record.type) ?? "";
1003
- if (!TERMINAL_FAILURE_TYPES.has(type)) return null;
1004
- const data = asRecord(record.data);
1005
- const outage = isUpstreamUnavailable(data) || isUpstreamUnavailable(record);
1006
- const reason = asString(data?.message) ?? asString(data?.error) ?? asString(data?.reason) ?? asString(record.message) ?? `sandbox stream reported ${type}`;
1007
- const code = readUpstreamUnavailableCode(data) ?? asString(data?.errorCode) ?? asString(data?.code) ?? readUpstreamUnavailableCode(record) ?? asString(record.errorCode) ?? asString(record.code);
1008
- return { outage, reason, ...code ? { code } : {} };
1009
- }
1010
- var DEFAULT_MODEL_STREAM_OPEN_TIMEOUT_MS = 12e4;
1011
- var DEFAULT_MODEL_FIRST_RESPONSE_TIMEOUT_MS = 6e4;
1012
- var ModelFailoverTimeoutError = class extends ModelFailoverExhaustedError {
1013
- code;
1014
- model;
1015
- timeoutMs;
1016
- constructor(input) {
1017
- super(input.attempts);
1018
- this.name = "ModelFailoverTimeoutError";
1019
- this.code = input.code;
1020
- this.model = input.model;
1021
- this.timeoutMs = input.timeoutMs;
1022
- 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.`;
1023
- }
1024
- };
1025
- function resolveTimeoutMs(value, fallback, label) {
1026
- if (value === void 0) return fallback;
1027
- if (!Number.isFinite(value) || value <= 0) {
1028
- throw new RangeError(`${label} must be a finite number greater than 0`);
1029
- }
1030
- return Math.max(1, Math.trunc(value));
1031
- }
1032
- var TIMED_OUT = /* @__PURE__ */ Symbol("model-attempt-timed-out");
1033
- function deadlineAfter(timeoutMs) {
1034
- let timer;
1035
- const promise = new Promise((resolve) => {
1036
- timer = setTimeout(() => resolve(TIMED_OUT), timeoutMs);
1037
- });
1038
- return {
1039
- promise,
1040
- clear: () => {
1041
- if (timer !== void 0) {
1042
- clearTimeout(timer);
1043
- timer = void 0;
1044
- }
1045
- }
1046
- };
1047
- }
1048
- function isEmptyTerminalReceipt(event) {
1049
- const record = asRecord(event);
1050
- if (!record) return false;
1051
- const type = asString(record.type) ?? "";
1052
- if (type !== "result" && type !== "done") return false;
1053
- const data = asRecord(record.data);
1054
- const text = asString(data?.finalText) ?? asString(data?.text) ?? "";
1055
- return text.trim().length === 0;
1056
- }
1057
- var MAX_EMPTY_TURN_RETRIES = 3;
1058
- function resolveEmptyTurnRetries(value) {
1059
- if (typeof value !== "number" || !Number.isFinite(value)) return 0;
1060
- const truncated = Math.trunc(value);
1061
- if (truncated <= 0) return 0;
1062
- return Math.min(truncated, MAX_EMPTY_TURN_RETRIES);
1063
- }
1064
- async function closeIterator(iterator, log) {
1065
- try {
1066
- await iterator.return?.();
1067
- } catch (err) {
1068
- log?.("[chat-routes] abandoning a failed model stream threw", {
1069
- error: err instanceof Error ? err.message : String(err)
1070
- });
1071
- }
1072
- }
1073
- function streamWithModelFailover(options) {
1074
- const committing = options.isCommitting ?? isCommittingSandboxEvent;
1075
- const emptyTurnRetries = resolveEmptyTurnRetries(options.emptyTurnRetries);
1076
- const openTimeoutMs = resolveTimeoutMs(
1077
- options.openTimeoutMs,
1078
- DEFAULT_MODEL_STREAM_OPEN_TIMEOUT_MS,
1079
- "openTimeoutMs"
1080
- );
1081
- const firstResponseTimeoutMs = resolveTimeoutMs(
1082
- options.firstResponseTimeoutMs,
1083
- DEFAULT_MODEL_FIRST_RESPONSE_TIMEOUT_MS,
1084
- "firstResponseTimeoutMs"
1085
- );
1086
- const liveLifecycle = options.liveLifecycleEvents === true;
1087
- const progress = createProgressQueue();
1088
- let serving;
1089
- let trail = [];
1090
- let fellBack = false;
1091
- const attemptState = { index: 0 };
1092
- let finalTimeout;
1093
- const { drainOnce } = createModelAttemptDrainer(attemptState, options, openTimeoutMs, firstResponseTimeoutMs, committing, liveLifecycle, progress);
1094
- const probe = async (model) => {
1095
- finalTimeout = void 0;
1096
- for (let retry = 0; ; retry += 1) {
1097
- const pass = await drainOnce(model);
1098
- if (!pass.empty || retry >= emptyTurnRetries) {
1099
- if (!pass.outcome.committed && pass.outcome.timeoutCode) {
1100
- finalTimeout = {
1101
- code: pass.outcome.timeoutCode,
1102
- model,
1103
- timeoutMs: pass.outcome.timeoutCode === "model_stream_open_timeout" ? openTimeoutMs : firstResponseTimeoutMs
1104
- };
1105
- } else {
1106
- finalTimeout = void 0;
1107
- }
1108
- return pass.outcome;
1109
- }
1110
- const iterator = pass.outcome.committed ? pass.outcome.iterator : null;
1111
- if (pass.outcome.committed) pass.outcome.abort();
1112
- if (iterator) void closeIterator(iterator, options.log);
1113
- const info = { model, retry: retry + 1, remaining: emptyTurnRetries - retry - 1 };
1114
- options.log?.("[chat-routes] turn completed with no assistant text; re-running the same model", {
1115
- ...info
1116
- });
1117
- options.onEmptyTurnRetry?.(info);
1118
- }
1119
- };
1120
- const events = (async function* () {
1121
- let handle;
1122
- const settled = runWithModelFailover({
1123
- models: options.models,
1124
- run: probe,
1125
- ...options.policy ? { policy: options.policy } : {},
1126
- // The probe has already classified the raw payload with
1127
- // `isUpstreamUnavailable`; this reads its verdict rather than
1128
- // re-classifying a wrapper object, so the two can never disagree.
1129
- isUnavailableResult: (result) => result.committed === false,
1130
- onFallback: (attempt, nextModel) => {
1131
- const info = {
1132
- from: attempt.model,
1133
- to: nextModel,
1134
- reason: attempt.reason ?? "upstream unavailable"
1135
- };
1136
- options.log?.("[chat-routes] model upstream unavailable; falling over", { ...info });
1137
- options.onFallback?.(info);
1138
- }
1139
- }).finally(() => progress.close());
1140
- settled.catch(() => void 0);
1141
- for (; ; ) {
1142
- const item = await progress.pull();
1143
- if (item === PROGRESS_CLOSED) break;
1144
- yield item;
1145
- }
1146
- try {
1147
- const outcome = await settled;
1148
- serving = outcome.model;
1149
- trail = outcome.attempts;
1150
- fellBack = outcome.usedFallback;
1151
- handle = outcome.value;
1152
- } catch (err) {
1153
- const attempts = err?.attempts;
1154
- if (Array.isArray(attempts)) trail = attempts;
1155
- if (err instanceof ModelFailoverExhaustedError && finalTimeout) {
1156
- throw new ModelFailoverTimeoutError({
1157
- attempts: trail,
1158
- ...finalTimeout
1159
- });
1160
- }
1161
- throw err;
1162
- }
1163
- for (const event of handle.buffered) yield event;
1164
- const live = handle.iterator;
1165
- if (!live) {
1166
- handle.abort();
1167
- return;
1168
- }
1169
- try {
1170
- for (; ; ) {
1171
- const next = await live.next();
1172
- if (next.done) return;
1173
- yield next.value;
1174
- }
1175
- } finally {
1176
- handle.abort();
1177
- await closeIterator(live, options.log);
1178
- }
1179
- })();
1180
- return {
1181
- events,
1182
- servingModel: () => serving,
1183
- attempts: () => trail,
1184
- usedFallback: () => fellBack
1185
- };
1186
- }
1187
- var PROGRESS_CLOSED = /* @__PURE__ */ Symbol("progress-closed");
1188
- function createProgressQueue() {
1189
- const items = [];
1190
- const waiters = [];
1191
- let closed = false;
1192
- const deliver = (value) => {
1193
- const waiter = waiters.shift();
1194
- if (waiter) waiter(value);
1195
- else items.push(value);
1196
- };
1197
- return {
1198
- push(event) {
1199
- if (closed) return;
1200
- deliver(event);
1201
- },
1202
- close() {
1203
- if (closed) return;
1204
- closed = true;
1205
- deliver(PROGRESS_CLOSED);
1206
- },
1207
- pull() {
1208
- if (items.length > 0) return Promise.resolve(items.shift());
1209
- if (closed) return Promise.resolve(PROGRESS_CLOSED);
1210
- return new Promise((resolve) => waiters.push(resolve));
1211
- }
1212
- };
1213
- }
1214
- function createModelAttemptDrainer(attemptState, options, openTimeoutMs, firstResponseTimeoutMs, committing, liveLifecycle, progress) {
1215
- const drainOnce = async (model) => {
1216
- attemptState.index += 1;
1217
- const controller = new AbortController();
1218
- const abort = () => {
1219
- if (!controller.signal.aborted) controller.abort();
1220
- };
1221
- const opening = Promise.resolve().then(() => options.open({
1222
- model,
1223
- attempt: attemptState.index,
1224
- signal: controller.signal
1225
- }));
1226
- const openDeadline = deadlineAfter(openTimeoutMs);
1227
- let opened;
1228
- try {
1229
- opened = await Promise.race([opening, openDeadline.promise]);
1230
- } catch (err) {
1231
- openDeadline.clear();
1232
- abort();
1233
- throw err;
1234
- }
1235
- if (opened === TIMED_OUT) {
1236
- openDeadline.clear();
1237
- abort();
1238
- void opening.then(
1239
- (lateSource) => {
1240
- try {
1241
- void closeIterator(lateSource[Symbol.asyncIterator](), options.log);
1242
- } catch (err) {
1243
- options.log?.("[chat-routes] closing a late model stream threw", {
1244
- error: err instanceof Error ? err.message : String(err)
1245
- });
1246
- }
1247
- },
1248
- (err) => {
1249
- options.log?.("[chat-routes] abandoned model stream open rejected after timeout", {
1250
- error: err instanceof Error ? err.message : String(err)
1251
- });
1252
- }
1253
- );
1254
- return {
1255
- outcome: {
1256
- committed: false,
1257
- error: `Opening the model event source for ${model} exceeded ${openTimeoutMs}ms`,
1258
- errorCode: "model_stream_open_timeout",
1259
- timeoutCode: "model_stream_open_timeout"
1260
- },
1261
- empty: false
1262
- };
1263
- }
1264
- const source = opened;
1265
- let iterator;
1266
- try {
1267
- iterator = source[Symbol.asyncIterator]();
1268
- } catch (err) {
1269
- openDeadline.clear();
1270
- abort();
1271
- throw err;
1272
- }
1273
- const buffered = [];
1274
- let next;
1275
- try {
1276
- next = await Promise.race([
1277
- Promise.resolve(iterator.next()),
1278
- openDeadline.promise
1279
- ]);
1280
- } catch (err) {
1281
- openDeadline.clear();
1282
- abort();
1283
- void closeIterator(iterator, options.log);
1284
- throw err;
1285
- }
1286
- openDeadline.clear();
1287
- if (next === TIMED_OUT) {
1288
- abort();
1289
- void closeIterator(iterator, options.log);
1290
- return {
1291
- outcome: {
1292
- committed: false,
1293
- error: `Starting the model event source for ${model} exceeded ${openTimeoutMs}ms`,
1294
- errorCode: "model_stream_open_timeout",
1295
- timeoutCode: "model_stream_open_timeout"
1296
- },
1297
- empty: false
1298
- };
1299
- }
1300
- if (next.done) {
1301
- abort();
1302
- return { outcome: { committed: true, buffered, iterator: null, abort }, empty: true };
1303
- }
1304
- const firstResponseDeadline = deadlineAfter(firstResponseTimeoutMs);
1305
- for (; ; ) {
1306
- const event = next.value;
1307
- let failure;
1308
- let commits = false;
1309
- try {
1310
- failure = classifyTerminalFailure(event);
1311
- commits = !failure && committing(event);
1312
- } catch (err) {
1313
- firstResponseDeadline.clear();
1314
- abort();
1315
- void closeIterator(iterator, options.log);
1316
- throw err;
1317
- }
1318
- if (failure?.outage) {
1319
- firstResponseDeadline.clear();
1320
- abort();
1321
- void closeIterator(iterator, options.log);
1322
- return {
1323
- outcome: {
1324
- committed: false,
1325
- error: failure.reason,
1326
- ...failure.code ? { errorCode: failure.code } : {}
1327
- },
1328
- empty: false
1329
- };
1330
- }
1331
- if (liveLifecycle && !failure && !commits && isLiveLifecycleEvent(event)) {
1332
- progress.push(event);
1333
- } else {
1334
- buffered.push(event);
1335
- }
1336
- if (failure) {
1337
- firstResponseDeadline.clear();
1338
- return { outcome: { committed: true, buffered, iterator, abort }, empty: false };
1339
- }
1340
- if (commits) {
1341
- firstResponseDeadline.clear();
1342
- return { outcome: { committed: true, buffered, iterator, abort }, empty: isEmptyTerminalReceipt(event) };
1343
- }
1344
- try {
1345
- next = await Promise.race([
1346
- Promise.resolve(iterator.next()),
1347
- firstResponseDeadline.promise
1348
- ]);
1349
- } catch (err) {
1350
- firstResponseDeadline.clear();
1351
- abort();
1352
- void closeIterator(iterator, options.log);
1353
- throw err;
1354
- }
1355
- if (next === TIMED_OUT) {
1356
- firstResponseDeadline.clear();
1357
- abort();
1358
- void closeIterator(iterator, options.log);
1359
- return {
1360
- outcome: {
1361
- committed: false,
1362
- error: `Model ${model} produced no first answer-bearing event within ${firstResponseTimeoutMs}ms`,
1363
- errorCode: "provider_first_response_timeout",
1364
- timeoutCode: "provider_first_response_timeout"
1365
- },
1366
- empty: false
1367
- };
1368
- }
1369
- if (next.done) {
1370
- firstResponseDeadline.clear();
1371
- abort();
1372
- return { outcome: { committed: true, buffered, iterator: null, abort }, empty: true };
1373
- }
1374
- }
1375
- };
1376
- return { drainOnce };
1377
- }
1378
-
1379
- // src/chat-routes/turn-event-stream.ts
1380
- var WITHHELD_FAILURE = /* @__PURE__ */ Symbol("withheld failure");
1381
- function serverFailureData(data) {
1382
- const original = data?.[WITHHELD_FAILURE];
1383
- return original !== null && typeof original === "object" ? original : data;
1384
- }
1385
- function failureReasonOf(data) {
1386
- if (!data) return void 0;
1387
- const message = data.message ?? data.error ?? data.reason;
1388
- if (typeof message === "string" && message.length > 0) return message;
1389
- return void 0;
1390
- }
1391
- function failureCodeOf(data) {
1392
- const code = data?.code;
1393
- return typeof code === "string" && code.length > 0 ? code : void 0;
1394
- }
1395
- function isVisibleText(event) {
1396
- const value = event;
1397
- if (event.type === "text") return typeof value.text === "string" ? value.text.trim().length > 0 : true;
1398
- if (event.type !== "message.part.updated") return false;
1399
- const part = event.data?.part;
1400
- const delta = event.data?.delta;
1401
- return part?.type === "text" && typeof delta === "string" && delta.trim().length > 0;
1402
- }
1403
- function isVisibleOutput(event) {
1404
- if (isVisibleText(event)) return true;
1405
- const value = event;
1406
- if (event.type === "reasoning") return typeof value.text === "string" && value.text.trim().length > 0;
1407
- if (event.type === "tool_call" || event.type === "tool.call" || event.type === "tool_result" || event.type === "tool.result" || event.type === "tool.event") return true;
1408
- if (event.type !== "message.part.updated") return false;
1409
- const part = event.data?.part;
1410
- if (part?.type === "tool") return true;
1411
- if (part?.type !== "reasoning" && part?.type !== "text") return false;
1412
- const text = event.data?.delta ?? part.text;
1413
- return typeof text === "string" && text.trim().length > 0;
1414
- }
1415
- async function* tapRawEvents(source, onRawEvent, log) {
1416
- for await (const event of source) {
1417
- try {
1418
- await onRawEvent(event);
1419
- } catch (err) {
1420
- log("[chat-routes] onRawEvent failed", { error: err instanceof Error ? err.message : String(err) });
1421
- }
1422
- yield event;
1423
- }
1424
- }
1425
- async function* withStreamHeartbeat(source, intervalMs, makeEvent) {
1426
- const iterator = source[Symbol.asyncIterator]();
1427
- try {
1428
- let pending = iterator.next();
1429
- let windowStart = Date.now();
1430
- let tick = 0;
1431
- for (; ; ) {
1432
- let timer;
1433
- let winner;
1434
- try {
1435
- const heartbeat = new Promise((resolve) => {
1436
- timer = setTimeout(() => resolve("heartbeat"), intervalMs);
1437
- });
1438
- winner = await Promise.race([pending.then(() => "event"), heartbeat]);
1439
- } finally {
1440
- if (timer !== void 0) clearTimeout(timer);
1441
- }
1442
- if (winner === "heartbeat") {
1443
- tick += 1;
1444
- yield makeEvent({ elapsedMs: Date.now() - windowStart, tick });
1445
- continue;
1446
- }
1447
- const result = await pending;
1448
- if (result.done) return;
1449
- yield result.value;
1450
- pending = iterator.next();
1451
- windowStart = Date.now();
1452
- tick = 0;
1453
- }
1454
- } finally {
1455
- await iterator.return?.();
1456
- }
1457
- }
1458
- function concatStreams(streams) {
1459
- let index = 0;
1460
- let reader = null;
1461
- return new ReadableStream({
1462
- async pull(controller) {
1463
- for (; ; ) {
1464
- if (!reader) {
1465
- const next = streams[index++];
1466
- if (!next) {
1467
- controller.close();
1468
- return;
1469
- }
1470
- reader = next.getReader();
1471
- }
1472
- const { done, value } = await reader.read();
1473
- if (done) {
1474
- reader = null;
1475
- continue;
1476
- }
1477
- controller.enqueue(value);
1478
- return;
1479
- }
1480
- },
1481
- async cancel(reason) {
1482
- await reader?.cancel(reason);
1483
- for (const stream of streams.slice(index)) await stream.cancel(reason);
1484
- }
1485
- });
1486
- }
1487
- async function* abortOnSignal(source, signal) {
1488
- const iterator = source[Symbol.asyncIterator]();
1489
- try {
1490
- for (; ; ) {
1491
- const next = await nextWithAbort(iterator, signal);
1492
- if (next.done) return;
1493
- yield next.value;
1494
- }
1495
- } finally {
1496
- await closeIteratorAfterAbort(iterator);
1497
- }
1498
- }
1499
- function nextWithAbort(iterator, signal) {
1500
- if (signal.aborted) return Promise.reject(new Error("chat turn cancelled"));
1501
- return new Promise((resolve, reject) => {
1502
- let settled = false;
1503
- const cleanup = () => signal.removeEventListener("abort", onAbort);
1504
- const onAbort = () => {
1505
- if (settled) return;
1506
- settled = true;
1507
- cleanup();
1508
- reject(new Error("chat turn cancelled"));
1509
- };
1510
- signal.addEventListener("abort", onAbort, { once: true });
1511
- if (signal.aborted) {
1512
- onAbort();
1513
- return;
1514
- }
1515
- Promise.resolve().then(() => iterator.next()).then(
1516
- (result) => {
1517
- if (settled) return;
1518
- settled = true;
1519
- cleanup();
1520
- resolve(result);
1521
- },
1522
- (error) => {
1523
- if (settled) return;
1524
- settled = true;
1525
- cleanup();
1526
- reject(error);
1527
- }
1528
- );
1529
- });
1530
- }
1531
- var ABORT_CLEANUP_TIMEOUT_MS = 50;
1532
- async function closeIteratorAfterAbort(iterator) {
1533
- let closing;
1534
- try {
1535
- const result = iterator.return?.();
1536
- if (result) closing = Promise.resolve(result);
1537
- } catch {
1538
- return;
1539
- }
1540
- if (!closing) return;
1541
- let timeout;
1542
- try {
1543
- await Promise.race([
1544
- Promise.resolve(closing).catch(() => void 0),
1545
- new Promise((resolve) => {
1546
- timeout = setTimeout(resolve, ABORT_CLEANUP_TIMEOUT_MS);
1547
- })
1548
- ]);
1549
- } finally {
1550
- if (timeout !== void 0) clearTimeout(timeout);
1551
- }
1552
- }
1553
-
1554
- // src/chat-routes/failure-wording.ts
1555
- var utcTime = (at) => `${at.toISOString().slice(11, 19)} UTC`;
1556
- function providerRetryNotice(event) {
1557
- const retry = event.type === "raw" ? asRecord(asRecord(event.data)?.event) : void 0;
1558
- if (retry?.type !== "retry" || !(Number(retry.next) > 0)) return void 0;
1559
- const cause = asString(retry.message)?.trim() || "no reason given";
1560
- return `The model provider refused the call (${cause}), so the turn retries at ${utcTime(new Date(Number(retry.next)))}.`;
1561
- }
1562
- function providerResetSentence(message) {
1563
- const at = new Date(message.match(/reset at (\d{4}-\d{2}-\d{2}T[\d:.]+Z)/)?.[1] ?? NaN);
1564
- if (Number.isNaN(at.getTime())) return void 0;
1565
- const cause = message.split("; the provider asked")[0]?.trim();
1566
- return `The model provider refused the call (${cause}); try again after ${utcTime(at)} on ${at.toISOString().slice(0, 10)}.`;
1567
- }
1568
- var MODEL_KEY_REFUSED_CODE = "model.key_refused";
1569
- function modelKeyRefusedSentence(message) {
1570
- if (!/\binvalid api key\b|\b401\b[^\n]*\b(?:unauthori[sz]ed|api key)\b/i.test(message)) return void 0;
1571
- return "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.";
1572
- }
1573
- var ROUTER_REFUSAL = new RegExp([
1574
- "This key's (?:monthly|lifetime) budget is exhausted",
1575
- "(?:This key's remaining budget|Your available balance), after other in-flight requests, is below \\$"
1576
- ].join("|"));
1577
- function routerRefusalSentence(message) {
1578
- const at = message.search(ROUTER_REFUSAL);
1579
- return at < 0 ? void 0 : message.slice(at).split("\n")[0].replace(/\s*\((?:exit code|killed by)[^)]*\)\s*$/, "").trim();
1580
- }
1581
- function plainFailureSentence(message) {
1582
- return providerResetSentence(message) ?? modelKeyRefusedSentence(message) ?? routerRefusalSentence(message);
1583
- }
1584
- function sandboxStreamErrorMessage(data) {
1585
- const record = asRecord(data);
1586
- const message = asString(record?.message) ?? asString(record?.error);
1587
- if (message) return message;
1588
- try {
1589
- return JSON.stringify(data) || "Sandbox stream returned an error event without details.";
1590
- } catch {
1591
- return "Sandbox stream returned an error event without details.";
1592
- }
1593
- }
1594
- function sandboxStreamErrorCode(data) {
1595
- return asString(asRecord(data)?.code) ?? "sandbox.stream_error";
1596
- }
1597
- function plainFailureEvent(event) {
1598
- const data = asRecord(event.data);
1599
- if (!data || !["error", "warning", "done", "session.run.failed"].includes(String(event.type))) return event;
1600
- const reasons = ["message", "error", "reason"].filter((key) => typeof data[key] === "string" && data[key]);
1601
- const raw = reasons.map((key) => data[key]).join(" | ");
1602
- const plain = reasons.map((key) => plainFailureSentence(data[key])).find((sentence) => sentence !== void 0);
1603
- if (!plain) return event;
1604
- return { ...event, data: { ...data, ...Object.fromEntries(reasons.map((key) => [key, plain])), details: { ...asRecord(data.details), raw } } };
1605
- }
1606
- var FAILED_TURN_TEXT = "That answer stopped before it finished. Please send your question again.";
1607
- var REFUSAL_CODES = /* @__PURE__ */ new Set([
1608
- "PAYMENT_REQUIRED",
1609
- "MODEL_REFUSED",
1610
- "model_policy_denied",
1611
- "grant_model_unsupported",
1612
- "grant_activation_missing",
1613
- "profile_unsupported"
1614
- ]);
1615
- function withheld(log, reason, code, status, meta = {}) {
1616
- const passes = Number(status) === 402 || REFUSAL_CODES.has(code ?? "") || plainFailureSentence(reason) || getChatFundingRecovery({ code, message: reason });
1617
- if (!passes) {
1618
- (log ?? console.warn)(
1619
- "[chat-routes] turn failure withheld from the person",
1620
- { failureClass: "runtime_failure", code, error: reason, ...meta }
1621
- );
1622
- }
1623
- return !passes;
1624
- }
1625
- function failureIds(source) {
1626
- const record = asRecord(source);
1627
- return Object.fromEntries(["turnId", "threadId", "sessionId", "executionId"].flatMap((key) => typeof record?.[key] === "string" && record[key] ? [[key, record[key]]] : []));
1628
- }
1629
- var TEXT_DETAILS = /* @__PURE__ */ new Set(["supportDetails", "hint", "failureNote", "raw"]);
1630
- function withheldFailureEvent(event, text) {
1631
- const data = asRecord(event.data) ?? {};
1632
- const details = Object.entries(asRecord(data.details) ?? {}).filter(([key]) => !TEXT_DETAILS.has(key));
1633
- const shown = { ...data, message: text };
1634
- delete shown.details;
1635
- for (const key of ["error", "reason"]) if (typeof shown[key] === "string") shown[key] = text;
1636
- if (details.length > 0) shown.details = Object.fromEntries(details);
1637
- return { ...event, data: Object.assign(shown, { [WITHHELD_FAILURE]: data }) };
1638
- }
1639
- function noModelAllowed(error) {
1640
- return error.attempts.length > 0 && error.attempts.every((attempt) => attempt.reason?.includes(" is not admitted by policy: "));
1641
- }
1642
- function sandboxStreamFailureDiagnostic(error) {
1643
- const streamMessage = error?.streamMessage;
1644
- const message = typeof streamMessage === "string" ? streamMessage : error instanceof Error ? error.message : "Sandbox stream failed";
1645
- const diagnostics = asRecord(error?.diagnostics);
1646
- let diagnosticText;
1647
- if (diagnostics) {
1648
- try {
1649
- diagnosticText = JSON.stringify(diagnostics);
1650
- } catch {
1651
- diagnosticText = "[unserializable diagnostics]";
1652
- }
1653
- }
1654
- const failureNote = diagnosticText ? `sandbox-stream: ${message}; ${diagnosticText}` : `sandbox-stream: ${message}`;
1655
- const ownCode = asString(asRecord(error)?.code);
1656
- let code = "sandbox.stream_failed";
1657
- if (error instanceof SandboxRunError) {
1658
- code = error.code;
1659
- } else if (error instanceof ModelFailoverTimeoutError) {
1660
- code = error.code;
1661
- } else if (error instanceof ModelFailoverExhaustedError) {
1662
- code = noModelAllowed(error) ? "model_policy_denied" : error.upstreamCode ?? code;
1663
- } else if (ownCode && REFUSAL_CODES.has(ownCode)) {
1664
- code = ownCode;
1665
- }
1666
- if (modelKeyRefusedSentence(message)) code = MODEL_KEY_REFUSED_CODE;
1667
- const userMessage = plainFailureSentence(message) ?? (REFUSAL_CODES.has(code) ? message : [
1668
- "The sandbox model stream stopped before a clean completion.",
1669
- `Error: ${message}`,
1670
- "Please retry. If it repeats, send these support details to the team."
1671
- ].join("\n\n"));
1672
- return { userMessage, failureNote, code, reason: message };
1673
- }
1674
- var UNAVAILABLE_TOOL_TEXT = "That tool isn't available.";
1675
- function withoutToolListTail(value) {
1676
- if (typeof value === "string") return value.includes("Available tools:") ? value.replace(/\s*Available tools:[\s\S]*$/, "") : value;
1677
- if (Array.isArray(value)) return value.map(withoutToolListTail);
1678
- const record = asRecord(value);
1679
- return record ? Object.fromEntries(Object.entries(record).map(([key, item]) => [key, withoutToolListTail(item)])) : value;
1680
- }
1681
- function withoutToolList(part) {
1682
- const shown = withoutToolListTail(part);
1683
- if (shown.tool !== "invalid") return shown;
1684
- const state = { ...asRecord(shown.state), input: { ...asRecord(asRecord(shown.state)?.input), error: UNAVAILABLE_TOOL_TEXT } };
1685
- for (const key of ["output", "error"]) if (typeof state[key] === "string") state[key] = UNAVAILABLE_TOOL_TEXT;
1686
- return { ...shown, state };
1687
- }
1688
-
1689
980
  // src/chat-routes/sandbox-model-source.ts
1690
981
  function parseEffectiveBackend(data) {
1691
982
  const backend = asRecord(data.effectiveBackend);
@@ -1842,13 +1133,43 @@ function applyTerminalUsage(reported, usage) {
1842
1133
  function toProducerWireEvent(event) {
1843
1134
  return event;
1844
1135
  }
1136
+ function shownStream(source, shown) {
1137
+ const map = async (step) => {
1138
+ const result = await step;
1139
+ if (result.done) return result;
1140
+ const value = shown(result.value);
1141
+ return value ? { done: false, value: toProducerWireEvent(value) } : map(source.next());
1142
+ };
1143
+ return {
1144
+ [Symbol.asyncIterator]() {
1145
+ return this;
1146
+ },
1147
+ next(...args) {
1148
+ return map(source.next(...args));
1149
+ },
1150
+ return(value) {
1151
+ return map(source.return(value));
1152
+ },
1153
+ throw(error) {
1154
+ return map(source.throw(error));
1155
+ },
1156
+ async [Symbol.asyncDispose]() {
1157
+ await this.return(void 0);
1158
+ }
1159
+ };
1160
+ }
1845
1161
  function createSandboxChatProducer(options) {
1846
1162
  const log = options.log ?? ((message, meta) => console.error(message, meta ?? ""));
1847
1163
  const renderable = options.isRenderableInteraction ?? isRenderableInteractionKind;
1848
- const failedTurnText = options.failedTurnText ?? FAILED_TURN_TEXT;
1164
+ const person = personFacing({
1165
+ log: options.log,
1166
+ failedTurnText: options.failedTurnText ?? FAILED_TURN_TEXT,
1167
+ hasAnswer: () => projectionState.answered
1168
+ });
1849
1169
  const { pendingModelNotices, source, captureEffectiveBackend, servedModel, servingModel, failover, modelState, openAttempts } = createSandboxModelSource(options, log);
1850
1170
  const projectionState = {
1851
1171
  fullText: "",
1172
+ answered: false,
1852
1173
  terminalObserved: false,
1853
1174
  stepCounter: 0,
1854
1175
  danglingToolsTerminalized: false,
@@ -1947,18 +1268,14 @@ function createSandboxChatProducer(options) {
1947
1268
  yield { type: "notice", id: notice.id, noticeKind: "warning", text: retry };
1948
1269
  }
1949
1270
  if (event.type === "warning") {
1950
- const message = asString(event.data?.message);
1951
- const plainFailure = message && plainFailureSentence(message);
1952
- if (plainFailure) log("[chat-routes] failure warning folded into its error notice", { code: asString(event.data?.code), error: message });
1953
- if (message && !plainFailure) {
1271
+ const text = person.warning(asString(event.data?.code), failureReason(record), failureIds(event.data));
1272
+ if (text) {
1954
1273
  projectionState.warningCount += 1;
1955
- const code = asString(event.data?.code);
1956
- const text = code ? `${code}: ${message}` : message;
1957
1274
  const notice = noticePart("warning", `warning-${projectionState.warningCount}`, text);
1958
1275
  recordPersistedPart(notice, void 0, noticePartKey(notice.id));
1959
1276
  yield { type: "notice", id: notice.id, noticeKind: "warning", text };
1960
1277
  }
1961
- yield toProducerWireEvent(plainFailureEvent(record));
1278
+ yield toProducerWireEvent(record);
1962
1279
  continue;
1963
1280
  }
1964
1281
  if (event.type === "usage") {
@@ -1970,7 +1287,7 @@ function createSandboxChatProducer(options) {
1970
1287
  if (event.type === "result") {
1971
1288
  projectionState.terminalObserved = true;
1972
1289
  const finalText = asString(event.data?.finalText);
1973
- if (finalText) projectionState.fullText = finalText;
1290
+ if (finalText) Object.assign(projectionState, { fullText: finalText, answered: true });
1974
1291
  const reported = extractReportedTurnUsage(asRecord(event.data));
1975
1292
  if (reported) {
1976
1293
  applyTerminalUsage(reported, usage);
@@ -1995,21 +1312,16 @@ function createSandboxChatProducer(options) {
1995
1312
  yield* emitFinalUsage();
1996
1313
  }
1997
1314
  yield* emitTerminalizedTools();
1998
- yield toProducerWireEvent(plainFailureEvent(record));
1315
+ yield toProducerWireEvent(record);
1999
1316
  continue;
2000
1317
  }
2001
1318
  if (event.type === "error") {
2002
- const message = sandboxStreamErrorMessage(event.data);
2003
- const keyRefused = modelKeyRefusedSentence(message);
2004
- if (keyRefused) log("[chat-routes] model key refused", { code: MODEL_KEY_REFUSED_CODE, error: message });
2005
- const hidden = withheld(options.log, message, asString(event.data?.code), event.data?.statusCode ?? event.data?.status, failureIds(event.data));
2006
- const errorContent = hidden ? failedTurnText : plainFailureSentence(message) ?? (projectionState.fullText.trim() ? `The sandbox model stream stopped before a clean completion.
2007
-
2008
- Error: ${message}` : `The sandbox agent returned an error before producing a visible answer.
2009
-
2010
- Error: ${message}`);
1319
+ const reason = failureReason(record);
1320
+ const view = person.view(reason, asString(event.data?.code), event.data?.statusCode ?? event.data?.status, failureIds(event.data));
1321
+ const errorContent = failureText(view, projectionState.fullText);
2011
1322
  if (options.errorNotice) {
2012
- yield* emitErrorNotice(errorContent, keyRefused ? MODEL_KEY_REFUSED_CODE : sandboxStreamErrorCode(event.data));
1323
+ const code = view.class === "model_key_refused" ? MODEL_KEY_REFUSED_CODE : sandboxStreamErrorCode(event.data);
1324
+ yield* emitErrorNotice(errorContent, code);
2013
1325
  } else if (options.includeErrorText !== false) {
2014
1326
  const errorDelta = projectionState.fullText ? `
2015
1327
 
@@ -2020,27 +1332,23 @@ ${errorContent}` : errorContent;
2020
1332
  }
2021
1333
  projectionState.interactionOutcome = "expired";
2022
1334
  yield* emitTerminalizedTools();
2023
- yield toProducerWireEvent(hidden ? withheldFailureEvent(record, failedTurnText) : plainFailureEvent(record));
1335
+ yield toProducerWireEvent(record);
2024
1336
  continue;
2025
1337
  }
2026
- yield toProducerWireEvent(plainFailureEvent(record));
1338
+ yield toProducerWireEvent(record);
2027
1339
  }
2028
1340
  yield* drainModelNotices();
2029
1341
  } catch (streamErr) {
2030
1342
  preflightRefused = !observedSourceEvent && openAttempts() === 1 && streamErr instanceof SandboxRunError && streamErr.code === "sandbox_unavailable";
2031
1343
  const diagnostic = sandboxStreamFailureDiagnostic(streamErr);
2032
- const hidden = withheld(
2033
- options.log,
1344
+ const statusCode = asRecord(streamErr)?.statusCode ?? asRecord(streamErr)?.status;
1345
+ const view = person.view(
2034
1346
  diagnostic.reason,
2035
1347
  diagnostic.code,
2036
- asRecord(streamErr)?.statusCode ?? asRecord(streamErr)?.status,
1348
+ statusCode,
2037
1349
  { ...failureIds(asRecord(streamErr)?.diagnostics), failureNote: diagnostic.failureNote }
2038
1350
  );
2039
- if (!hidden) log("[chat-routes] sandbox stream failed", {
2040
- failureNote: diagnostic.failureNote,
2041
- error: streamErr instanceof Error ? streamErr.message : String(streamErr)
2042
- });
2043
- const userMessage = hidden ? failedTurnText : diagnostic.userMessage;
1351
+ const userMessage = failureText(view, projectionState.fullText);
2044
1352
  if (options.errorNotice) {
2045
1353
  yield* emitErrorNotice(userMessage, diagnostic.code);
2046
1354
  } else if (options.includeErrorText !== false) {
@@ -2053,18 +1361,19 @@ ${userMessage}` : userMessage;
2053
1361
  }
2054
1362
  projectionState.interactionOutcome = "expired";
2055
1363
  yield* emitTerminalizedTools();
2056
- const failure = { type: "error", data: {
2057
- message: hidden ? diagnostic.reason : userMessage,
1364
+ yield toProducerWireEvent({ type: "error", data: {
1365
+ message: diagnostic.reason,
2058
1366
  code: diagnostic.code,
1367
+ ...statusCode !== void 0 ? { statusCode } : {},
2059
1368
  details: { failureNote: diagnostic.failureNote }
2060
- } };
2061
- yield toProducerWireEvent(hidden ? withheldFailureEvent(failure, failedTurnText) : failure);
1369
+ } });
2062
1370
  } finally {
2063
1371
  reasoningClock.settle();
2064
1372
  }
2065
1373
  }
2066
1374
  return {
2067
- stream: stream(),
1375
+ // Every event leaves through `person`: no failure reason or tool list reaches the person's client.
1376
+ stream: shownStream(stream(), person.event),
2068
1377
  finalText: () => projectionState.fullText,
2069
1378
  terminalObserved: () => projectionState.terminalObserved || preflightRefused,
2070
1379
  assistantParts: () => finalizePendingInteractionParts(
@@ -2187,7 +1496,7 @@ async function* projectSandboxMessagePart(event, tracker, recordPersistedPart, r
2187
1496
  if (delta) {
2188
1497
  if (partType === "reasoning") reasoningClock.reasoning(key);
2189
1498
  else reasoningClock.settle();
2190
- if (partType === "text") projectionState.fullText += delta;
1499
+ if (partType === "text") Object.assign(projectionState, { fullText: projectionState.fullText + delta, answered: true });
2191
1500
  yield { type: partType, text: delta };
2192
1501
  }
2193
1502
  return;
@@ -4343,20 +3652,6 @@ export {
4343
3652
  turnCarriesFiles,
4344
3653
  routerTurnHistory,
4345
3654
  createHybridTurnProducer,
4346
- isCommittingSandboxEvent,
4347
- summarizeFailoverReason,
4348
- isLiveLifecycleEvent,
4349
- classifyTerminalFailure,
4350
- DEFAULT_MODEL_STREAM_OPEN_TIMEOUT_MS,
4351
- DEFAULT_MODEL_FIRST_RESPONSE_TIMEOUT_MS,
4352
- ModelFailoverTimeoutError,
4353
- MAX_EMPTY_TURN_RETRIES,
4354
- resolveEmptyTurnRetries,
4355
- streamWithModelFailover,
4356
- providerRetryNotice,
4357
- providerResetSentence,
4358
- MODEL_KEY_REFUSED_CODE,
4359
- modelKeyRefusedSentence,
4360
3655
  createSandboxChatProducer,
4361
3656
  withProducerStream,
4362
3657
  cancelChatTurn,
@@ -4370,4 +3665,4 @@ export {
4370
3665
  createChatTurnRoutes,
4371
3666
  streamChatRouteAsSandboxEvents
4372
3667
  };
4373
- //# sourceMappingURL=chunk-XZ37VCTP.js.map
3668
+ //# sourceMappingURL=chunk-CD4SJXYC.js.map