@tangle-network/agent-app 4.0.8 → 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/assistant/index.js +2 -1
- package/dist/assistant/index.js.map +1 -1
- 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/detached-turn.d.ts +2 -0
- package/dist/chat-routes/failure-wording.d.ts +61 -0
- package/dist/chat-routes/index.js +30 -22
- package/dist/chat-routes/index.js.map +1 -1
- package/dist/chat-routes/sandbox-producer.d.ts +6 -14
- package/dist/chat-routes/turn-event-stream.d.ts +5 -0
- package/dist/chat-routes/turn-route-contracts.d.ts +23 -0
- package/dist/chat-routes/turn-routes.d.ts +3 -1
- package/dist/{chunk-R5PW3FO4.js → chunk-2IGXFHUO.js} +13 -2
- package/dist/chunk-2IGXFHUO.js.map +1 -0
- package/dist/{chunk-4OR7NNYB.js → chunk-CD4SJXYC.js} +125 -730
- 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-FZOHBYGV.js → chunk-KPCNNFBO.js} +13 -2
- package/dist/chunk-KPCNNFBO.js.map +1 -0
- package/dist/{chunk-QXPOBDPU.js → chunk-M4LA7JMU.js} +1 -21
- package/dist/chunk-M4LA7JMU.js.map +1 -0
- package/dist/{chunk-ASWAPIJM.js → chunk-QMQB725F.js} +4 -14
- package/dist/chunk-QMQB725F.js.map +1 -0
- package/dist/chunk-QUV5M7SZ.js +23 -0
- package/dist/chunk-QUV5M7SZ.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/{chunk-A2M75YRP.js → chunk-VTMMTQ4P.js} +2 -2
- package/dist/chunk-VTMMTQ4P.js.map +1 -0
- package/dist/journeys/cli.js +1 -1
- package/dist/journeys/index.js +1 -1
- package/dist/preset-cloudflare/index.js +8 -5
- package/dist/preset-cloudflare/index.js.map +1 -1
- package/dist/public-consultation/index.js +6 -5
- package/dist/public-consultation/index.js.map +1 -1
- package/dist/sandbox/index.js +4 -4
- package/dist/turn-health/index.js +2 -1
- package/dist/turn-health/index.js.map +1 -1
- package/dist/turn-health/sweep.d.ts +1 -1
- package/dist/turn-stream/adapters.d.ts +14 -1
- package/dist/turn-stream/core.d.ts +2 -0
- package/dist/turn-stream/do.d.ts +17 -0
- package/dist/turn-stream/index.d.ts +1 -1
- package/dist/turn-stream/index.js +11 -2
- package/dist/turn-stream/index.js.map +1 -1
- package/dist/web-react/index.d.ts +1 -1
- package/dist/web-react/index.js +4 -2
- package/package.json +1 -1
- package/dist/chunk-4OR7NNYB.js.map +0 -1
- package/dist/chunk-A2M75YRP.js.map +0 -1
- package/dist/chunk-ASWAPIJM.js.map +0 -1
- package/dist/chunk-FTGKXM7O.js.map +0 -1
- package/dist/chunk-FZOHBYGV.js.map +0 -1
- package/dist/chunk-K4YIDNX5.js +0 -263
- package/dist/chunk-K4YIDNX5.js.map +0 -1
- package/dist/chunk-QXPOBDPU.js.map +0 -1
- package/dist/chunk-R5PW3FO4.js.map +0 -1
- package/dist/chunk-W2DIPVHW.js.map +0 -1
- /package/dist/{web-react → web}/turn-funding-recovery.d.ts +0 -0
|
@@ -13,7 +13,6 @@ import {
|
|
|
13
13
|
REDACTED
|
|
14
14
|
} from "./chunk-M4QITAQV.js";
|
|
15
15
|
import {
|
|
16
|
-
SandboxRunError,
|
|
17
16
|
createComputerStep,
|
|
18
17
|
createD1PrewarmClaimStore,
|
|
19
18
|
createEscalationSpeculator,
|
|
@@ -31,19 +30,41 @@ import {
|
|
|
31
30
|
sandboxRunFailureDetail,
|
|
32
31
|
streamSandboxPrompt,
|
|
33
32
|
turnStorageBindings
|
|
34
|
-
} from "./chunk-
|
|
33
|
+
} from "./chunk-QMQB725F.js";
|
|
35
34
|
import {
|
|
36
35
|
redactErrorMessage
|
|
37
36
|
} from "./chunk-MH74DY2I.js";
|
|
38
37
|
import {
|
|
38
|
+
FAILED_TURN_TEXT,
|
|
39
|
+
MODEL_KEY_REFUSED_CODE,
|
|
40
|
+
WITHHELD_FAILURE,
|
|
41
|
+
abortOnSignal,
|
|
39
42
|
addStepFinishUsage,
|
|
40
43
|
assistantRowIdForTurn,
|
|
44
|
+
concatStreams,
|
|
41
45
|
createAssistantDraftWriter,
|
|
46
|
+
failureCodeOf,
|
|
47
|
+
failureIds,
|
|
48
|
+
failureReason,
|
|
49
|
+
failureReasonOf,
|
|
50
|
+
failureText,
|
|
51
|
+
isVisibleOutput,
|
|
52
|
+
isVisibleText,
|
|
42
53
|
measuredUsage,
|
|
54
|
+
personFacing,
|
|
55
|
+
providerRetryNotice,
|
|
56
|
+
resolveEmptyTurnRetries,
|
|
43
57
|
rowIdOf,
|
|
58
|
+
sandboxStreamErrorCode,
|
|
59
|
+
sandboxStreamFailureDiagnostic,
|
|
60
|
+
serverFailureData,
|
|
44
61
|
storeSupportsDraftPersistence,
|
|
45
|
-
|
|
46
|
-
|
|
62
|
+
streamWithModelFailover,
|
|
63
|
+
tapRawEvents,
|
|
64
|
+
transformVisibleTextParts,
|
|
65
|
+
withStreamHeartbeat,
|
|
66
|
+
withoutToolList
|
|
67
|
+
} from "./chunk-RNDWCT4O.js";
|
|
47
68
|
import {
|
|
48
69
|
normalizeClientTurnId,
|
|
49
70
|
resolveChatTurn
|
|
@@ -75,9 +96,15 @@ import {
|
|
|
75
96
|
parseInteractionCancel,
|
|
76
97
|
parseInteractionRequest
|
|
77
98
|
} from "./chunk-TB6KNPFI.js";
|
|
99
|
+
import {
|
|
100
|
+
SandboxRunError
|
|
101
|
+
} from "./chunk-2IGXFHUO.js";
|
|
78
102
|
import {
|
|
79
103
|
parseJsonObjectBody
|
|
80
104
|
} from "./chunk-ALIWB2P3.js";
|
|
105
|
+
import {
|
|
106
|
+
forwardTurnToHost
|
|
107
|
+
} from "./chunk-KPCNNFBO.js";
|
|
81
108
|
import {
|
|
82
109
|
DEFAULT_RUNNING_TURN_RENEW_INTERVAL_MS,
|
|
83
110
|
coalesceDeltas,
|
|
@@ -106,12 +133,7 @@ import {
|
|
|
106
133
|
turnResponseEventFor
|
|
107
134
|
} from "./chunk-K2JPSXKI.js";
|
|
108
135
|
import {
|
|
109
|
-
|
|
110
|
-
buildModelChain,
|
|
111
|
-
isUpstreamUnavailable,
|
|
112
|
-
readHttpStatusHint,
|
|
113
|
-
readUpstreamUnavailableCode,
|
|
114
|
-
runWithModelFailover
|
|
136
|
+
buildModelChain
|
|
115
137
|
} from "./chunk-L3QR274L.js";
|
|
116
138
|
import {
|
|
117
139
|
assertPolicy,
|
|
@@ -146,7 +168,14 @@ function valueRedactor(values) {
|
|
|
146
168
|
const secrets = [...new Set((values ?? []).filter((v) => typeof v === "string" && v.length >= MIN_SECRET_LENGTH))].sort((a, b) => b.length - a.length);
|
|
147
169
|
if (secrets.length === 0) return void 0;
|
|
148
170
|
const text = (s) => secrets.some((v) => s.includes(v)) ? secrets.reduce((acc, v) => acc.split(v).join(REDACTED), s) : s;
|
|
149
|
-
const walk = (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
|
+
};
|
|
150
179
|
return walk;
|
|
151
180
|
}
|
|
152
181
|
async function* redactEvents(source, redact) {
|
|
@@ -948,428 +977,6 @@ ${extra}` : { ...prompt, stable: [...prompt.stable ?? [], extra] }),
|
|
|
948
977
|
}
|
|
949
978
|
}
|
|
950
979
|
|
|
951
|
-
// src/chat-routes/model-failover-stream.ts
|
|
952
|
-
var TERMINAL_FAILURE_TYPES = /* @__PURE__ */ new Set(["error", "session.run.failed"]);
|
|
953
|
-
var NON_COMMITTING_PART_TYPES = /* @__PURE__ */ new Set(["step-start", "step-finish"]);
|
|
954
|
-
function isCommittingSandboxEvent(event) {
|
|
955
|
-
const record = asRecord(event);
|
|
956
|
-
if (!record) return false;
|
|
957
|
-
const type = asString(record.type) ?? "";
|
|
958
|
-
if (!type) return false;
|
|
959
|
-
if (type === "message.part.updated") {
|
|
960
|
-
const data = asRecord(record.data);
|
|
961
|
-
const part = asRecord(data?.part);
|
|
962
|
-
const partType = asString(part?.type) ?? "";
|
|
963
|
-
if (NON_COMMITTING_PART_TYPES.has(partType)) return false;
|
|
964
|
-
if (partType === "text" || partType === "reasoning") {
|
|
965
|
-
return [data?.delta, part?.text, part?.content].some((value) => typeof value === "string" && value.length > 0);
|
|
966
|
-
}
|
|
967
|
-
return true;
|
|
968
|
-
}
|
|
969
|
-
if (type === "start" || type === "execution.started" || type === "status" || type === "model-processing" || type === "model.processing" || // Public Sandbox timing metadata: a request was attempted, not answered.
|
|
970
|
-
type === "model.first_request" || type === "session.created" || type === "session.updated" || type === "session.idle" || type === "step-start" || type === "step-finish" || type === "turn" || type === "warning") {
|
|
971
|
-
return false;
|
|
972
|
-
}
|
|
973
|
-
return true;
|
|
974
|
-
}
|
|
975
|
-
function summarizeFailoverReason(reason) {
|
|
976
|
-
const collapsed = reason.replace(/\s+/g, " ").trim();
|
|
977
|
-
if (!collapsed) return "upstream unavailable";
|
|
978
|
-
if (/<!doctype html|<html[\s>]/i.test(collapsed)) {
|
|
979
|
-
const status = readHttpStatusHint(collapsed);
|
|
980
|
-
return status ? `HTTP ${status}` : "upstream returned an error page";
|
|
981
|
-
}
|
|
982
|
-
const LIMIT = 160;
|
|
983
|
-
if (collapsed.length <= LIMIT) return collapsed;
|
|
984
|
-
const clipped = collapsed.slice(0, LIMIT);
|
|
985
|
-
const lastSpace = clipped.lastIndexOf(" ");
|
|
986
|
-
return `${(lastSpace > LIMIT - 30 ? clipped.slice(0, lastSpace) : clipped).trimEnd()}\u2026`;
|
|
987
|
-
}
|
|
988
|
-
function isLiveLifecycleEvent(event) {
|
|
989
|
-
const type = asString(asRecord(event)?.type) ?? "";
|
|
990
|
-
return type === "start" || type === "execution.started" || type === "status" || type === "model-processing" || type === "model.processing" || // Public Sandbox timing metadata: a request was attempted, not answered.
|
|
991
|
-
type === "model.first_request" || type === "session.created" || type === "session.updated" || type === "session.idle";
|
|
992
|
-
}
|
|
993
|
-
function classifyTerminalFailure(event) {
|
|
994
|
-
const record = asRecord(event);
|
|
995
|
-
if (!record) return null;
|
|
996
|
-
const type = asString(record.type) ?? "";
|
|
997
|
-
if (!TERMINAL_FAILURE_TYPES.has(type)) return null;
|
|
998
|
-
const data = asRecord(record.data);
|
|
999
|
-
const outage = isUpstreamUnavailable(data) || isUpstreamUnavailable(record);
|
|
1000
|
-
const reason = asString(data?.message) ?? asString(data?.error) ?? asString(data?.reason) ?? asString(record.message) ?? `sandbox stream reported ${type}`;
|
|
1001
|
-
const code = readUpstreamUnavailableCode(data) ?? asString(data?.errorCode) ?? asString(data?.code) ?? readUpstreamUnavailableCode(record) ?? asString(record.errorCode) ?? asString(record.code);
|
|
1002
|
-
return { outage, reason, ...code ? { code } : {} };
|
|
1003
|
-
}
|
|
1004
|
-
var DEFAULT_MODEL_STREAM_OPEN_TIMEOUT_MS = 12e4;
|
|
1005
|
-
var DEFAULT_MODEL_FIRST_RESPONSE_TIMEOUT_MS = 6e4;
|
|
1006
|
-
var ModelFailoverTimeoutError = class extends ModelFailoverExhaustedError {
|
|
1007
|
-
code;
|
|
1008
|
-
model;
|
|
1009
|
-
timeoutMs;
|
|
1010
|
-
constructor(input) {
|
|
1011
|
-
super(input.attempts);
|
|
1012
|
-
this.name = "ModelFailoverTimeoutError";
|
|
1013
|
-
this.code = input.code;
|
|
1014
|
-
this.model = input.model;
|
|
1015
|
-
this.timeoutMs = input.timeoutMs;
|
|
1016
|
-
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.`;
|
|
1017
|
-
}
|
|
1018
|
-
};
|
|
1019
|
-
function resolveTimeoutMs(value, fallback, label) {
|
|
1020
|
-
if (value === void 0) return fallback;
|
|
1021
|
-
if (!Number.isFinite(value) || value <= 0) {
|
|
1022
|
-
throw new RangeError(`${label} must be a finite number greater than 0`);
|
|
1023
|
-
}
|
|
1024
|
-
return Math.max(1, Math.trunc(value));
|
|
1025
|
-
}
|
|
1026
|
-
var TIMED_OUT = /* @__PURE__ */ Symbol("model-attempt-timed-out");
|
|
1027
|
-
function deadlineAfter(timeoutMs) {
|
|
1028
|
-
let timer;
|
|
1029
|
-
const promise = new Promise((resolve) => {
|
|
1030
|
-
timer = setTimeout(() => resolve(TIMED_OUT), timeoutMs);
|
|
1031
|
-
});
|
|
1032
|
-
return {
|
|
1033
|
-
promise,
|
|
1034
|
-
clear: () => {
|
|
1035
|
-
if (timer !== void 0) {
|
|
1036
|
-
clearTimeout(timer);
|
|
1037
|
-
timer = void 0;
|
|
1038
|
-
}
|
|
1039
|
-
}
|
|
1040
|
-
};
|
|
1041
|
-
}
|
|
1042
|
-
function isEmptyTerminalReceipt(event) {
|
|
1043
|
-
const record = asRecord(event);
|
|
1044
|
-
if (!record) return false;
|
|
1045
|
-
const type = asString(record.type) ?? "";
|
|
1046
|
-
if (type !== "result" && type !== "done") return false;
|
|
1047
|
-
const data = asRecord(record.data);
|
|
1048
|
-
const text = asString(data?.finalText) ?? asString(data?.text) ?? "";
|
|
1049
|
-
return text.trim().length === 0;
|
|
1050
|
-
}
|
|
1051
|
-
var MAX_EMPTY_TURN_RETRIES = 3;
|
|
1052
|
-
function resolveEmptyTurnRetries(value) {
|
|
1053
|
-
if (typeof value !== "number" || !Number.isFinite(value)) return 0;
|
|
1054
|
-
const truncated = Math.trunc(value);
|
|
1055
|
-
if (truncated <= 0) return 0;
|
|
1056
|
-
return Math.min(truncated, MAX_EMPTY_TURN_RETRIES);
|
|
1057
|
-
}
|
|
1058
|
-
async function closeIterator(iterator, log) {
|
|
1059
|
-
try {
|
|
1060
|
-
await iterator.return?.();
|
|
1061
|
-
} catch (err) {
|
|
1062
|
-
log?.("[chat-routes] abandoning a failed model stream threw", {
|
|
1063
|
-
error: err instanceof Error ? err.message : String(err)
|
|
1064
|
-
});
|
|
1065
|
-
}
|
|
1066
|
-
}
|
|
1067
|
-
function streamWithModelFailover(options) {
|
|
1068
|
-
const committing = options.isCommitting ?? isCommittingSandboxEvent;
|
|
1069
|
-
const emptyTurnRetries = resolveEmptyTurnRetries(options.emptyTurnRetries);
|
|
1070
|
-
const openTimeoutMs = resolveTimeoutMs(
|
|
1071
|
-
options.openTimeoutMs,
|
|
1072
|
-
DEFAULT_MODEL_STREAM_OPEN_TIMEOUT_MS,
|
|
1073
|
-
"openTimeoutMs"
|
|
1074
|
-
);
|
|
1075
|
-
const firstResponseTimeoutMs = resolveTimeoutMs(
|
|
1076
|
-
options.firstResponseTimeoutMs,
|
|
1077
|
-
DEFAULT_MODEL_FIRST_RESPONSE_TIMEOUT_MS,
|
|
1078
|
-
"firstResponseTimeoutMs"
|
|
1079
|
-
);
|
|
1080
|
-
const liveLifecycle = options.liveLifecycleEvents === true;
|
|
1081
|
-
const progress = createProgressQueue();
|
|
1082
|
-
let serving;
|
|
1083
|
-
let trail = [];
|
|
1084
|
-
let fellBack = false;
|
|
1085
|
-
const attemptState = { index: 0 };
|
|
1086
|
-
let finalTimeout;
|
|
1087
|
-
const { drainOnce } = createModelAttemptDrainer(attemptState, options, openTimeoutMs, firstResponseTimeoutMs, committing, liveLifecycle, progress);
|
|
1088
|
-
const probe = async (model) => {
|
|
1089
|
-
finalTimeout = void 0;
|
|
1090
|
-
for (let retry = 0; ; retry += 1) {
|
|
1091
|
-
const pass = await drainOnce(model);
|
|
1092
|
-
if (!pass.empty || retry >= emptyTurnRetries) {
|
|
1093
|
-
if (!pass.outcome.committed && pass.outcome.timeoutCode) {
|
|
1094
|
-
finalTimeout = {
|
|
1095
|
-
code: pass.outcome.timeoutCode,
|
|
1096
|
-
model,
|
|
1097
|
-
timeoutMs: pass.outcome.timeoutCode === "model_stream_open_timeout" ? openTimeoutMs : firstResponseTimeoutMs
|
|
1098
|
-
};
|
|
1099
|
-
} else {
|
|
1100
|
-
finalTimeout = void 0;
|
|
1101
|
-
}
|
|
1102
|
-
return pass.outcome;
|
|
1103
|
-
}
|
|
1104
|
-
const iterator = pass.outcome.committed ? pass.outcome.iterator : null;
|
|
1105
|
-
if (pass.outcome.committed) pass.outcome.abort();
|
|
1106
|
-
if (iterator) void closeIterator(iterator, options.log);
|
|
1107
|
-
const info = { model, retry: retry + 1, remaining: emptyTurnRetries - retry - 1 };
|
|
1108
|
-
options.log?.("[chat-routes] turn completed with no assistant text; re-running the same model", {
|
|
1109
|
-
...info
|
|
1110
|
-
});
|
|
1111
|
-
options.onEmptyTurnRetry?.(info);
|
|
1112
|
-
}
|
|
1113
|
-
};
|
|
1114
|
-
const events = (async function* () {
|
|
1115
|
-
let handle;
|
|
1116
|
-
const settled = runWithModelFailover({
|
|
1117
|
-
models: options.models,
|
|
1118
|
-
run: probe,
|
|
1119
|
-
...options.policy ? { policy: options.policy } : {},
|
|
1120
|
-
// The probe has already classified the raw payload with
|
|
1121
|
-
// `isUpstreamUnavailable`; this reads its verdict rather than
|
|
1122
|
-
// re-classifying a wrapper object, so the two can never disagree.
|
|
1123
|
-
isUnavailableResult: (result) => result.committed === false,
|
|
1124
|
-
onFallback: (attempt, nextModel) => {
|
|
1125
|
-
const info = {
|
|
1126
|
-
from: attempt.model,
|
|
1127
|
-
to: nextModel,
|
|
1128
|
-
reason: attempt.reason ?? "upstream unavailable"
|
|
1129
|
-
};
|
|
1130
|
-
options.log?.("[chat-routes] model upstream unavailable; falling over", { ...info });
|
|
1131
|
-
options.onFallback?.(info);
|
|
1132
|
-
}
|
|
1133
|
-
}).finally(() => progress.close());
|
|
1134
|
-
settled.catch(() => void 0);
|
|
1135
|
-
for (; ; ) {
|
|
1136
|
-
const item = await progress.pull();
|
|
1137
|
-
if (item === PROGRESS_CLOSED) break;
|
|
1138
|
-
yield item;
|
|
1139
|
-
}
|
|
1140
|
-
try {
|
|
1141
|
-
const outcome = await settled;
|
|
1142
|
-
serving = outcome.model;
|
|
1143
|
-
trail = outcome.attempts;
|
|
1144
|
-
fellBack = outcome.usedFallback;
|
|
1145
|
-
handle = outcome.value;
|
|
1146
|
-
} catch (err) {
|
|
1147
|
-
const attempts = err?.attempts;
|
|
1148
|
-
if (Array.isArray(attempts)) trail = attempts;
|
|
1149
|
-
if (err instanceof ModelFailoverExhaustedError && finalTimeout) {
|
|
1150
|
-
throw new ModelFailoverTimeoutError({
|
|
1151
|
-
attempts: trail,
|
|
1152
|
-
...finalTimeout
|
|
1153
|
-
});
|
|
1154
|
-
}
|
|
1155
|
-
throw err;
|
|
1156
|
-
}
|
|
1157
|
-
for (const event of handle.buffered) yield event;
|
|
1158
|
-
const live = handle.iterator;
|
|
1159
|
-
if (!live) {
|
|
1160
|
-
handle.abort();
|
|
1161
|
-
return;
|
|
1162
|
-
}
|
|
1163
|
-
try {
|
|
1164
|
-
for (; ; ) {
|
|
1165
|
-
const next = await live.next();
|
|
1166
|
-
if (next.done) return;
|
|
1167
|
-
yield next.value;
|
|
1168
|
-
}
|
|
1169
|
-
} finally {
|
|
1170
|
-
handle.abort();
|
|
1171
|
-
await closeIterator(live, options.log);
|
|
1172
|
-
}
|
|
1173
|
-
})();
|
|
1174
|
-
return {
|
|
1175
|
-
events,
|
|
1176
|
-
servingModel: () => serving,
|
|
1177
|
-
attempts: () => trail,
|
|
1178
|
-
usedFallback: () => fellBack
|
|
1179
|
-
};
|
|
1180
|
-
}
|
|
1181
|
-
var PROGRESS_CLOSED = /* @__PURE__ */ Symbol("progress-closed");
|
|
1182
|
-
function createProgressQueue() {
|
|
1183
|
-
const items = [];
|
|
1184
|
-
const waiters = [];
|
|
1185
|
-
let closed = false;
|
|
1186
|
-
const deliver = (value) => {
|
|
1187
|
-
const waiter = waiters.shift();
|
|
1188
|
-
if (waiter) waiter(value);
|
|
1189
|
-
else items.push(value);
|
|
1190
|
-
};
|
|
1191
|
-
return {
|
|
1192
|
-
push(event) {
|
|
1193
|
-
if (closed) return;
|
|
1194
|
-
deliver(event);
|
|
1195
|
-
},
|
|
1196
|
-
close() {
|
|
1197
|
-
if (closed) return;
|
|
1198
|
-
closed = true;
|
|
1199
|
-
deliver(PROGRESS_CLOSED);
|
|
1200
|
-
},
|
|
1201
|
-
pull() {
|
|
1202
|
-
if (items.length > 0) return Promise.resolve(items.shift());
|
|
1203
|
-
if (closed) return Promise.resolve(PROGRESS_CLOSED);
|
|
1204
|
-
return new Promise((resolve) => waiters.push(resolve));
|
|
1205
|
-
}
|
|
1206
|
-
};
|
|
1207
|
-
}
|
|
1208
|
-
function createModelAttemptDrainer(attemptState, options, openTimeoutMs, firstResponseTimeoutMs, committing, liveLifecycle, progress) {
|
|
1209
|
-
const drainOnce = async (model) => {
|
|
1210
|
-
attemptState.index += 1;
|
|
1211
|
-
const controller = new AbortController();
|
|
1212
|
-
const abort = () => {
|
|
1213
|
-
if (!controller.signal.aborted) controller.abort();
|
|
1214
|
-
};
|
|
1215
|
-
const opening = Promise.resolve().then(() => options.open({
|
|
1216
|
-
model,
|
|
1217
|
-
attempt: attemptState.index,
|
|
1218
|
-
signal: controller.signal
|
|
1219
|
-
}));
|
|
1220
|
-
const openDeadline = deadlineAfter(openTimeoutMs);
|
|
1221
|
-
let opened;
|
|
1222
|
-
try {
|
|
1223
|
-
opened = await Promise.race([opening, openDeadline.promise]);
|
|
1224
|
-
} catch (err) {
|
|
1225
|
-
openDeadline.clear();
|
|
1226
|
-
abort();
|
|
1227
|
-
throw err;
|
|
1228
|
-
}
|
|
1229
|
-
if (opened === TIMED_OUT) {
|
|
1230
|
-
openDeadline.clear();
|
|
1231
|
-
abort();
|
|
1232
|
-
void opening.then(
|
|
1233
|
-
(lateSource) => {
|
|
1234
|
-
try {
|
|
1235
|
-
void closeIterator(lateSource[Symbol.asyncIterator](), options.log);
|
|
1236
|
-
} catch (err) {
|
|
1237
|
-
options.log?.("[chat-routes] closing a late model stream threw", {
|
|
1238
|
-
error: err instanceof Error ? err.message : String(err)
|
|
1239
|
-
});
|
|
1240
|
-
}
|
|
1241
|
-
},
|
|
1242
|
-
(err) => {
|
|
1243
|
-
options.log?.("[chat-routes] abandoned model stream open rejected after timeout", {
|
|
1244
|
-
error: err instanceof Error ? err.message : String(err)
|
|
1245
|
-
});
|
|
1246
|
-
}
|
|
1247
|
-
);
|
|
1248
|
-
return {
|
|
1249
|
-
outcome: {
|
|
1250
|
-
committed: false,
|
|
1251
|
-
error: `Opening the model event source for ${model} exceeded ${openTimeoutMs}ms`,
|
|
1252
|
-
errorCode: "model_stream_open_timeout",
|
|
1253
|
-
timeoutCode: "model_stream_open_timeout"
|
|
1254
|
-
},
|
|
1255
|
-
empty: false
|
|
1256
|
-
};
|
|
1257
|
-
}
|
|
1258
|
-
const source = opened;
|
|
1259
|
-
let iterator;
|
|
1260
|
-
try {
|
|
1261
|
-
iterator = source[Symbol.asyncIterator]();
|
|
1262
|
-
} catch (err) {
|
|
1263
|
-
openDeadline.clear();
|
|
1264
|
-
abort();
|
|
1265
|
-
throw err;
|
|
1266
|
-
}
|
|
1267
|
-
const buffered = [];
|
|
1268
|
-
let next;
|
|
1269
|
-
try {
|
|
1270
|
-
next = await Promise.race([
|
|
1271
|
-
Promise.resolve(iterator.next()),
|
|
1272
|
-
openDeadline.promise
|
|
1273
|
-
]);
|
|
1274
|
-
} catch (err) {
|
|
1275
|
-
openDeadline.clear();
|
|
1276
|
-
abort();
|
|
1277
|
-
void closeIterator(iterator, options.log);
|
|
1278
|
-
throw err;
|
|
1279
|
-
}
|
|
1280
|
-
openDeadline.clear();
|
|
1281
|
-
if (next === TIMED_OUT) {
|
|
1282
|
-
abort();
|
|
1283
|
-
void closeIterator(iterator, options.log);
|
|
1284
|
-
return {
|
|
1285
|
-
outcome: {
|
|
1286
|
-
committed: false,
|
|
1287
|
-
error: `Starting the model event source for ${model} exceeded ${openTimeoutMs}ms`,
|
|
1288
|
-
errorCode: "model_stream_open_timeout",
|
|
1289
|
-
timeoutCode: "model_stream_open_timeout"
|
|
1290
|
-
},
|
|
1291
|
-
empty: false
|
|
1292
|
-
};
|
|
1293
|
-
}
|
|
1294
|
-
if (next.done) {
|
|
1295
|
-
abort();
|
|
1296
|
-
return { outcome: { committed: true, buffered, iterator: null, abort }, empty: true };
|
|
1297
|
-
}
|
|
1298
|
-
const firstResponseDeadline = deadlineAfter(firstResponseTimeoutMs);
|
|
1299
|
-
for (; ; ) {
|
|
1300
|
-
const event = next.value;
|
|
1301
|
-
let failure;
|
|
1302
|
-
let commits = false;
|
|
1303
|
-
try {
|
|
1304
|
-
failure = classifyTerminalFailure(event);
|
|
1305
|
-
commits = !failure && committing(event);
|
|
1306
|
-
} catch (err) {
|
|
1307
|
-
firstResponseDeadline.clear();
|
|
1308
|
-
abort();
|
|
1309
|
-
void closeIterator(iterator, options.log);
|
|
1310
|
-
throw err;
|
|
1311
|
-
}
|
|
1312
|
-
if (failure?.outage) {
|
|
1313
|
-
firstResponseDeadline.clear();
|
|
1314
|
-
abort();
|
|
1315
|
-
void closeIterator(iterator, options.log);
|
|
1316
|
-
return {
|
|
1317
|
-
outcome: {
|
|
1318
|
-
committed: false,
|
|
1319
|
-
error: failure.reason,
|
|
1320
|
-
...failure.code ? { errorCode: failure.code } : {}
|
|
1321
|
-
},
|
|
1322
|
-
empty: false
|
|
1323
|
-
};
|
|
1324
|
-
}
|
|
1325
|
-
if (liveLifecycle && !failure && !commits && isLiveLifecycleEvent(event)) {
|
|
1326
|
-
progress.push(event);
|
|
1327
|
-
} else {
|
|
1328
|
-
buffered.push(event);
|
|
1329
|
-
}
|
|
1330
|
-
if (failure) {
|
|
1331
|
-
firstResponseDeadline.clear();
|
|
1332
|
-
return { outcome: { committed: true, buffered, iterator, abort }, empty: false };
|
|
1333
|
-
}
|
|
1334
|
-
if (commits) {
|
|
1335
|
-
firstResponseDeadline.clear();
|
|
1336
|
-
return { outcome: { committed: true, buffered, iterator, abort }, empty: isEmptyTerminalReceipt(event) };
|
|
1337
|
-
}
|
|
1338
|
-
try {
|
|
1339
|
-
next = await Promise.race([
|
|
1340
|
-
Promise.resolve(iterator.next()),
|
|
1341
|
-
firstResponseDeadline.promise
|
|
1342
|
-
]);
|
|
1343
|
-
} catch (err) {
|
|
1344
|
-
firstResponseDeadline.clear();
|
|
1345
|
-
abort();
|
|
1346
|
-
void closeIterator(iterator, options.log);
|
|
1347
|
-
throw err;
|
|
1348
|
-
}
|
|
1349
|
-
if (next === TIMED_OUT) {
|
|
1350
|
-
firstResponseDeadline.clear();
|
|
1351
|
-
abort();
|
|
1352
|
-
void closeIterator(iterator, options.log);
|
|
1353
|
-
return {
|
|
1354
|
-
outcome: {
|
|
1355
|
-
committed: false,
|
|
1356
|
-
error: `Model ${model} produced no first answer-bearing event within ${firstResponseTimeoutMs}ms`,
|
|
1357
|
-
errorCode: "provider_first_response_timeout",
|
|
1358
|
-
timeoutCode: "provider_first_response_timeout"
|
|
1359
|
-
},
|
|
1360
|
-
empty: false
|
|
1361
|
-
};
|
|
1362
|
-
}
|
|
1363
|
-
if (next.done) {
|
|
1364
|
-
firstResponseDeadline.clear();
|
|
1365
|
-
abort();
|
|
1366
|
-
return { outcome: { committed: true, buffered, iterator: null, abort }, empty: true };
|
|
1367
|
-
}
|
|
1368
|
-
}
|
|
1369
|
-
};
|
|
1370
|
-
return { drainOnce };
|
|
1371
|
-
}
|
|
1372
|
-
|
|
1373
980
|
// src/chat-routes/sandbox-model-source.ts
|
|
1374
981
|
function parseEffectiveBackend(data) {
|
|
1375
982
|
const backend = asRecord(data.effectiveBackend);
|
|
@@ -1426,11 +1033,10 @@ function createSandboxModelSource(options, log) {
|
|
|
1426
1033
|
id: `model-fallback-${modelNoticeCount}`,
|
|
1427
1034
|
// Named models on both sides: a quality regression after a downgrade
|
|
1428
1035
|
// must be attributable to the model that actually answered. The
|
|
1429
|
-
//
|
|
1430
|
-
// error page
|
|
1431
|
-
//
|
|
1432
|
-
|
|
1433
|
-
text: `${info.from} was unavailable (${summarizeFailoverReason(info.reason)}) \u2014 answered with ${info.to} instead.`
|
|
1036
|
+
// reason stays off the transcript (an upstream's own text can be an
|
|
1037
|
+
// HTML error page or a harness report); the failover stream logs it,
|
|
1038
|
+
// and `modelFailover.attempts[].reason` keeps it for the operator.
|
|
1039
|
+
text: `The model ${info.from} stopped; trying ${info.to}.`
|
|
1434
1040
|
});
|
|
1435
1041
|
options.onModelFallback?.(info);
|
|
1436
1042
|
}
|
|
@@ -1524,85 +1130,46 @@ function applyTerminalUsage(reported, usage) {
|
|
|
1524
1130
|
if (reported.cacheWriteTokens !== void 0) usage.cacheWriteTokens = reported.cacheWriteTokens;
|
|
1525
1131
|
if (reported.harnessCostUsd !== void 0) usage.harnessCostUsd = reported.harnessCostUsd;
|
|
1526
1132
|
}
|
|
1527
|
-
var utcTime = (at) => `${at.toISOString().slice(11, 19)} UTC`;
|
|
1528
|
-
function providerRetryNotice(event) {
|
|
1529
|
-
const retry = event.type === "raw" ? asRecord(asRecord(event.data)?.event) : void 0;
|
|
1530
|
-
if (retry?.type !== "retry" || !(Number(retry.next) > 0)) return void 0;
|
|
1531
|
-
const cause = asString(retry.message)?.trim() || "no reason given";
|
|
1532
|
-
return `The model provider refused the call (${cause}), so the turn retries at ${utcTime(new Date(Number(retry.next)))}.`;
|
|
1533
|
-
}
|
|
1534
|
-
function providerResetSentence(message) {
|
|
1535
|
-
const at = new Date(message.match(/reset at (\d{4}-\d{2}-\d{2}T[\d:.]+Z)/)?.[1] ?? NaN);
|
|
1536
|
-
if (Number.isNaN(at.getTime())) return void 0;
|
|
1537
|
-
const cause = message.split("; the provider asked")[0]?.trim();
|
|
1538
|
-
return `The model provider refused the call (${cause}); try again after ${utcTime(at)} on ${at.toISOString().slice(0, 10)}.`;
|
|
1539
|
-
}
|
|
1540
|
-
var MODEL_KEY_REFUSED_CODE = "model.key_refused";
|
|
1541
|
-
function modelKeyRefusedSentence(message) {
|
|
1542
|
-
if (!/\binvalid api key\b|\b401\b[^\n]*\b(?:unauthori[sz]ed|api key)\b/i.test(message)) return void 0;
|
|
1543
|
-
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.";
|
|
1544
|
-
}
|
|
1545
|
-
function sandboxStreamErrorMessage(data) {
|
|
1546
|
-
const record = asRecord(data);
|
|
1547
|
-
const message = asString(record?.message) ?? asString(record?.error);
|
|
1548
|
-
if (message) return message;
|
|
1549
|
-
try {
|
|
1550
|
-
return JSON.stringify(data) || "Sandbox stream returned an error event without details.";
|
|
1551
|
-
} catch {
|
|
1552
|
-
return "Sandbox stream returned an error event without details.";
|
|
1553
|
-
}
|
|
1554
|
-
}
|
|
1555
|
-
function sandboxStreamErrorCode(data) {
|
|
1556
|
-
return asString(asRecord(data)?.code) ?? "sandbox.stream_error";
|
|
1557
|
-
}
|
|
1558
1133
|
function toProducerWireEvent(event) {
|
|
1559
1134
|
return event;
|
|
1560
1135
|
}
|
|
1561
|
-
function
|
|
1562
|
-
const
|
|
1563
|
-
|
|
1564
|
-
|
|
1565
|
-
|
|
1566
|
-
|
|
1567
|
-
|
|
1568
|
-
return {
|
|
1569
|
-
|
|
1570
|
-
|
|
1571
|
-
|
|
1572
|
-
|
|
1573
|
-
|
|
1574
|
-
|
|
1575
|
-
|
|
1576
|
-
|
|
1577
|
-
|
|
1578
|
-
|
|
1579
|
-
|
|
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);
|
|
1580
1158
|
}
|
|
1581
|
-
}
|
|
1582
|
-
const keyRefused = modelKeyRefusedSentence(message);
|
|
1583
|
-
const userMessage = providerResetSentence(message) ?? keyRefused ?? [
|
|
1584
|
-
"The sandbox model stream stopped before a clean completion.",
|
|
1585
|
-
`Error: ${message}`,
|
|
1586
|
-
"Please retry. If it repeats, send these support details to the team."
|
|
1587
|
-
].join("\n\n");
|
|
1588
|
-
const failureNote = diagnosticText ? `sandbox-stream: ${message}; ${diagnosticText}` : `sandbox-stream: ${message}`;
|
|
1589
|
-
let code = "sandbox.stream_failed";
|
|
1590
|
-
if (error instanceof SandboxRunError) {
|
|
1591
|
-
code = error.code;
|
|
1592
|
-
} else if (error instanceof ModelFailoverTimeoutError) {
|
|
1593
|
-
code = error.code;
|
|
1594
|
-
} else if (error instanceof ModelFailoverExhaustedError) {
|
|
1595
|
-
code = error.upstreamCode ?? code;
|
|
1596
|
-
}
|
|
1597
|
-
if (keyRefused) code = MODEL_KEY_REFUSED_CODE;
|
|
1598
|
-
return { userMessage, failureNote, code };
|
|
1159
|
+
};
|
|
1599
1160
|
}
|
|
1600
1161
|
function createSandboxChatProducer(options) {
|
|
1601
1162
|
const log = options.log ?? ((message, meta) => console.error(message, meta ?? ""));
|
|
1602
1163
|
const renderable = options.isRenderableInteraction ?? isRenderableInteractionKind;
|
|
1164
|
+
const person = personFacing({
|
|
1165
|
+
log: options.log,
|
|
1166
|
+
failedTurnText: options.failedTurnText ?? FAILED_TURN_TEXT,
|
|
1167
|
+
hasAnswer: () => projectionState.answered
|
|
1168
|
+
});
|
|
1603
1169
|
const { pendingModelNotices, source, captureEffectiveBackend, servedModel, servingModel, failover, modelState, openAttempts } = createSandboxModelSource(options, log);
|
|
1604
1170
|
const projectionState = {
|
|
1605
1171
|
fullText: "",
|
|
1172
|
+
answered: false,
|
|
1606
1173
|
terminalObserved: false,
|
|
1607
1174
|
stepCounter: 0,
|
|
1608
1175
|
danglingToolsTerminalized: false,
|
|
@@ -1701,18 +1268,14 @@ function createSandboxChatProducer(options) {
|
|
|
1701
1268
|
yield { type: "notice", id: notice.id, noticeKind: "warning", text: retry };
|
|
1702
1269
|
}
|
|
1703
1270
|
if (event.type === "warning") {
|
|
1704
|
-
const
|
|
1705
|
-
|
|
1706
|
-
if (plainFailure) log("[chat-routes] failure warning folded into its error notice", { code: asString(event.data?.code), error: message });
|
|
1707
|
-
if (message && !plainFailure) {
|
|
1271
|
+
const text = person.warning(asString(event.data?.code), failureReason(record), failureIds(event.data));
|
|
1272
|
+
if (text) {
|
|
1708
1273
|
projectionState.warningCount += 1;
|
|
1709
|
-
const code = asString(event.data?.code);
|
|
1710
|
-
const text = code ? `${code}: ${message}` : message;
|
|
1711
1274
|
const notice = noticePart("warning", `warning-${projectionState.warningCount}`, text);
|
|
1712
1275
|
recordPersistedPart(notice, void 0, noticePartKey(notice.id));
|
|
1713
1276
|
yield { type: "notice", id: notice.id, noticeKind: "warning", text };
|
|
1714
1277
|
}
|
|
1715
|
-
yield toProducerWireEvent(
|
|
1278
|
+
yield toProducerWireEvent(record);
|
|
1716
1279
|
continue;
|
|
1717
1280
|
}
|
|
1718
1281
|
if (event.type === "usage") {
|
|
@@ -1724,7 +1287,7 @@ function createSandboxChatProducer(options) {
|
|
|
1724
1287
|
if (event.type === "result") {
|
|
1725
1288
|
projectionState.terminalObserved = true;
|
|
1726
1289
|
const finalText = asString(event.data?.finalText);
|
|
1727
|
-
if (finalText) projectionState
|
|
1290
|
+
if (finalText) Object.assign(projectionState, { fullText: finalText, answered: true });
|
|
1728
1291
|
const reported = extractReportedTurnUsage(asRecord(event.data));
|
|
1729
1292
|
if (reported) {
|
|
1730
1293
|
applyTerminalUsage(reported, usage);
|
|
@@ -1749,20 +1312,16 @@ function createSandboxChatProducer(options) {
|
|
|
1749
1312
|
yield* emitFinalUsage();
|
|
1750
1313
|
}
|
|
1751
1314
|
yield* emitTerminalizedTools();
|
|
1752
|
-
yield toProducerWireEvent(
|
|
1315
|
+
yield toProducerWireEvent(record);
|
|
1753
1316
|
continue;
|
|
1754
1317
|
}
|
|
1755
1318
|
if (event.type === "error") {
|
|
1756
|
-
const
|
|
1757
|
-
const
|
|
1758
|
-
|
|
1759
|
-
const errorContent = providerResetSentence(message) ?? keyRefused ?? (projectionState.fullText.trim() ? `The sandbox model stream stopped before a clean completion.
|
|
1760
|
-
|
|
1761
|
-
Error: ${message}` : `The sandbox agent returned an error before producing a visible answer.
|
|
1762
|
-
|
|
1763
|
-
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);
|
|
1764
1322
|
if (options.errorNotice) {
|
|
1765
|
-
|
|
1323
|
+
const code = view.class === "model_key_refused" ? MODEL_KEY_REFUSED_CODE : sandboxStreamErrorCode(event.data);
|
|
1324
|
+
yield* emitErrorNotice(errorContent, code);
|
|
1766
1325
|
} else if (options.includeErrorText !== false) {
|
|
1767
1326
|
const errorDelta = projectionState.fullText ? `
|
|
1768
1327
|
|
|
@@ -1773,45 +1332,48 @@ ${errorContent}` : errorContent;
|
|
|
1773
1332
|
}
|
|
1774
1333
|
projectionState.interactionOutcome = "expired";
|
|
1775
1334
|
yield* emitTerminalizedTools();
|
|
1776
|
-
yield toProducerWireEvent(
|
|
1335
|
+
yield toProducerWireEvent(record);
|
|
1777
1336
|
continue;
|
|
1778
1337
|
}
|
|
1779
|
-
yield toProducerWireEvent(
|
|
1338
|
+
yield toProducerWireEvent(record);
|
|
1780
1339
|
}
|
|
1781
1340
|
yield* drainModelNotices();
|
|
1782
1341
|
} catch (streamErr) {
|
|
1783
1342
|
preflightRefused = !observedSourceEvent && openAttempts() === 1 && streamErr instanceof SandboxRunError && streamErr.code === "sandbox_unavailable";
|
|
1784
1343
|
const diagnostic = sandboxStreamFailureDiagnostic(streamErr);
|
|
1785
|
-
|
|
1786
|
-
|
|
1787
|
-
|
|
1788
|
-
|
|
1344
|
+
const statusCode = asRecord(streamErr)?.statusCode ?? asRecord(streamErr)?.status;
|
|
1345
|
+
const view = person.view(
|
|
1346
|
+
diagnostic.reason,
|
|
1347
|
+
diagnostic.code,
|
|
1348
|
+
statusCode,
|
|
1349
|
+
{ ...failureIds(asRecord(streamErr)?.diagnostics), failureNote: diagnostic.failureNote }
|
|
1350
|
+
);
|
|
1351
|
+
const userMessage = failureText(view, projectionState.fullText);
|
|
1789
1352
|
if (options.errorNotice) {
|
|
1790
|
-
yield* emitErrorNotice(
|
|
1353
|
+
yield* emitErrorNotice(userMessage, diagnostic.code);
|
|
1791
1354
|
} else if (options.includeErrorText !== false) {
|
|
1792
1355
|
const errorDelta = projectionState.fullText ? `
|
|
1793
1356
|
|
|
1794
1357
|
---
|
|
1795
|
-
${
|
|
1358
|
+
${userMessage}` : userMessage;
|
|
1796
1359
|
projectionState.fullText += errorDelta;
|
|
1797
1360
|
yield { type: "text", text: errorDelta };
|
|
1798
1361
|
}
|
|
1799
1362
|
projectionState.interactionOutcome = "expired";
|
|
1800
1363
|
yield* emitTerminalizedTools();
|
|
1801
|
-
yield {
|
|
1802
|
-
|
|
1803
|
-
|
|
1804
|
-
|
|
1805
|
-
|
|
1806
|
-
|
|
1807
|
-
}
|
|
1808
|
-
};
|
|
1364
|
+
yield toProducerWireEvent({ type: "error", data: {
|
|
1365
|
+
message: diagnostic.reason,
|
|
1366
|
+
code: diagnostic.code,
|
|
1367
|
+
...statusCode !== void 0 ? { statusCode } : {},
|
|
1368
|
+
details: { failureNote: diagnostic.failureNote }
|
|
1369
|
+
} });
|
|
1809
1370
|
} finally {
|
|
1810
1371
|
reasoningClock.settle();
|
|
1811
1372
|
}
|
|
1812
1373
|
}
|
|
1813
1374
|
return {
|
|
1814
|
-
|
|
1375
|
+
// Every event leaves through `person`: no failure reason or tool list reaches the person's client.
|
|
1376
|
+
stream: shownStream(stream(), person.event),
|
|
1815
1377
|
finalText: () => projectionState.fullText,
|
|
1816
1378
|
terminalObserved: () => projectionState.terminalObserved || preflightRefused,
|
|
1817
1379
|
assistantParts: () => finalizePendingInteractionParts(
|
|
@@ -1934,14 +1496,14 @@ async function* projectSandboxMessagePart(event, tracker, recordPersistedPart, r
|
|
|
1934
1496
|
if (delta) {
|
|
1935
1497
|
if (partType === "reasoning") reasoningClock.reasoning(key);
|
|
1936
1498
|
else reasoningClock.settle();
|
|
1937
|
-
if (partType === "text") projectionState.fullText
|
|
1499
|
+
if (partType === "text") Object.assign(projectionState, { fullText: projectionState.fullText + delta, answered: true });
|
|
1938
1500
|
yield { type: partType, text: delta };
|
|
1939
1501
|
}
|
|
1940
1502
|
return;
|
|
1941
1503
|
}
|
|
1942
1504
|
if (partType === "tool") {
|
|
1943
1505
|
reasoningClock.settle();
|
|
1944
|
-
recordPersistedPart(part, void 0);
|
|
1506
|
+
recordPersistedPart(withoutToolList(part), void 0);
|
|
1945
1507
|
const persisted = partMap.get(getPartKey(part));
|
|
1946
1508
|
const state = asRecord(persisted?.state);
|
|
1947
1509
|
const toolId = String(persisted?.id ?? "");
|
|
@@ -2747,176 +2309,6 @@ function dispatchFirstFor(selected, args, trace) {
|
|
|
2747
2309
|
return selected.router && !args.userMessageId ? dispatchProducerFirst(selected.produce, args, trace) : void 0;
|
|
2748
2310
|
}
|
|
2749
2311
|
|
|
2750
|
-
// src/chat-routes/turn-event-stream.ts
|
|
2751
|
-
function failureReasonOf(data) {
|
|
2752
|
-
if (!data) return void 0;
|
|
2753
|
-
const message = data.message ?? data.error ?? data.reason;
|
|
2754
|
-
if (typeof message === "string" && message.length > 0) return message;
|
|
2755
|
-
return void 0;
|
|
2756
|
-
}
|
|
2757
|
-
function failureCodeOf(data) {
|
|
2758
|
-
const code = data?.code;
|
|
2759
|
-
return typeof code === "string" && code.length > 0 ? code : void 0;
|
|
2760
|
-
}
|
|
2761
|
-
function isVisibleText(event) {
|
|
2762
|
-
const value = event;
|
|
2763
|
-
if (event.type === "text") return typeof value.text === "string" ? value.text.trim().length > 0 : true;
|
|
2764
|
-
if (event.type !== "message.part.updated") return false;
|
|
2765
|
-
const part = event.data?.part;
|
|
2766
|
-
const delta = event.data?.delta;
|
|
2767
|
-
return part?.type === "text" && typeof delta === "string" && delta.trim().length > 0;
|
|
2768
|
-
}
|
|
2769
|
-
function isVisibleOutput(event) {
|
|
2770
|
-
if (isVisibleText(event)) return true;
|
|
2771
|
-
const value = event;
|
|
2772
|
-
if (event.type === "reasoning") return typeof value.text === "string" && value.text.trim().length > 0;
|
|
2773
|
-
if (event.type === "tool_call" || event.type === "tool.call" || event.type === "tool_result" || event.type === "tool.result" || event.type === "tool.event") return true;
|
|
2774
|
-
if (event.type !== "message.part.updated") return false;
|
|
2775
|
-
const part = event.data?.part;
|
|
2776
|
-
if (part?.type === "tool") return true;
|
|
2777
|
-
if (part?.type !== "reasoning" && part?.type !== "text") return false;
|
|
2778
|
-
const text = event.data?.delta ?? part.text;
|
|
2779
|
-
return typeof text === "string" && text.trim().length > 0;
|
|
2780
|
-
}
|
|
2781
|
-
async function* tapRawEvents(source, onRawEvent, log) {
|
|
2782
|
-
for await (const event of source) {
|
|
2783
|
-
try {
|
|
2784
|
-
await onRawEvent(event);
|
|
2785
|
-
} catch (err) {
|
|
2786
|
-
log("[chat-routes] onRawEvent failed", { error: err instanceof Error ? err.message : String(err) });
|
|
2787
|
-
}
|
|
2788
|
-
yield event;
|
|
2789
|
-
}
|
|
2790
|
-
}
|
|
2791
|
-
async function* withStreamHeartbeat(source, intervalMs, makeEvent) {
|
|
2792
|
-
const iterator = source[Symbol.asyncIterator]();
|
|
2793
|
-
try {
|
|
2794
|
-
let pending = iterator.next();
|
|
2795
|
-
let windowStart = Date.now();
|
|
2796
|
-
let tick = 0;
|
|
2797
|
-
for (; ; ) {
|
|
2798
|
-
let timer;
|
|
2799
|
-
let winner;
|
|
2800
|
-
try {
|
|
2801
|
-
const heartbeat = new Promise((resolve) => {
|
|
2802
|
-
timer = setTimeout(() => resolve("heartbeat"), intervalMs);
|
|
2803
|
-
});
|
|
2804
|
-
winner = await Promise.race([pending.then(() => "event"), heartbeat]);
|
|
2805
|
-
} finally {
|
|
2806
|
-
if (timer !== void 0) clearTimeout(timer);
|
|
2807
|
-
}
|
|
2808
|
-
if (winner === "heartbeat") {
|
|
2809
|
-
tick += 1;
|
|
2810
|
-
yield makeEvent({ elapsedMs: Date.now() - windowStart, tick });
|
|
2811
|
-
continue;
|
|
2812
|
-
}
|
|
2813
|
-
const result = await pending;
|
|
2814
|
-
if (result.done) return;
|
|
2815
|
-
yield result.value;
|
|
2816
|
-
pending = iterator.next();
|
|
2817
|
-
windowStart = Date.now();
|
|
2818
|
-
tick = 0;
|
|
2819
|
-
}
|
|
2820
|
-
} finally {
|
|
2821
|
-
await iterator.return?.();
|
|
2822
|
-
}
|
|
2823
|
-
}
|
|
2824
|
-
function concatStreams(streams) {
|
|
2825
|
-
let index = 0;
|
|
2826
|
-
let reader = null;
|
|
2827
|
-
return new ReadableStream({
|
|
2828
|
-
async pull(controller) {
|
|
2829
|
-
for (; ; ) {
|
|
2830
|
-
if (!reader) {
|
|
2831
|
-
const next = streams[index++];
|
|
2832
|
-
if (!next) {
|
|
2833
|
-
controller.close();
|
|
2834
|
-
return;
|
|
2835
|
-
}
|
|
2836
|
-
reader = next.getReader();
|
|
2837
|
-
}
|
|
2838
|
-
const { done, value } = await reader.read();
|
|
2839
|
-
if (done) {
|
|
2840
|
-
reader = null;
|
|
2841
|
-
continue;
|
|
2842
|
-
}
|
|
2843
|
-
controller.enqueue(value);
|
|
2844
|
-
return;
|
|
2845
|
-
}
|
|
2846
|
-
},
|
|
2847
|
-
async cancel(reason) {
|
|
2848
|
-
await reader?.cancel(reason);
|
|
2849
|
-
for (const stream of streams.slice(index)) await stream.cancel(reason);
|
|
2850
|
-
}
|
|
2851
|
-
});
|
|
2852
|
-
}
|
|
2853
|
-
async function* abortOnSignal(source, signal) {
|
|
2854
|
-
const iterator = source[Symbol.asyncIterator]();
|
|
2855
|
-
try {
|
|
2856
|
-
for (; ; ) {
|
|
2857
|
-
const next = await nextWithAbort(iterator, signal);
|
|
2858
|
-
if (next.done) return;
|
|
2859
|
-
yield next.value;
|
|
2860
|
-
}
|
|
2861
|
-
} finally {
|
|
2862
|
-
await closeIteratorAfterAbort(iterator);
|
|
2863
|
-
}
|
|
2864
|
-
}
|
|
2865
|
-
function nextWithAbort(iterator, signal) {
|
|
2866
|
-
if (signal.aborted) return Promise.reject(new Error("chat turn cancelled"));
|
|
2867
|
-
return new Promise((resolve, reject) => {
|
|
2868
|
-
let settled = false;
|
|
2869
|
-
const cleanup = () => signal.removeEventListener("abort", onAbort);
|
|
2870
|
-
const onAbort = () => {
|
|
2871
|
-
if (settled) return;
|
|
2872
|
-
settled = true;
|
|
2873
|
-
cleanup();
|
|
2874
|
-
reject(new Error("chat turn cancelled"));
|
|
2875
|
-
};
|
|
2876
|
-
signal.addEventListener("abort", onAbort, { once: true });
|
|
2877
|
-
if (signal.aborted) {
|
|
2878
|
-
onAbort();
|
|
2879
|
-
return;
|
|
2880
|
-
}
|
|
2881
|
-
Promise.resolve().then(() => iterator.next()).then(
|
|
2882
|
-
(result) => {
|
|
2883
|
-
if (settled) return;
|
|
2884
|
-
settled = true;
|
|
2885
|
-
cleanup();
|
|
2886
|
-
resolve(result);
|
|
2887
|
-
},
|
|
2888
|
-
(error) => {
|
|
2889
|
-
if (settled) return;
|
|
2890
|
-
settled = true;
|
|
2891
|
-
cleanup();
|
|
2892
|
-
reject(error);
|
|
2893
|
-
}
|
|
2894
|
-
);
|
|
2895
|
-
});
|
|
2896
|
-
}
|
|
2897
|
-
var ABORT_CLEANUP_TIMEOUT_MS = 50;
|
|
2898
|
-
async function closeIteratorAfterAbort(iterator) {
|
|
2899
|
-
let closing;
|
|
2900
|
-
try {
|
|
2901
|
-
const result = iterator.return?.();
|
|
2902
|
-
if (result) closing = Promise.resolve(result);
|
|
2903
|
-
} catch {
|
|
2904
|
-
return;
|
|
2905
|
-
}
|
|
2906
|
-
if (!closing) return;
|
|
2907
|
-
let timeout;
|
|
2908
|
-
try {
|
|
2909
|
-
await Promise.race([
|
|
2910
|
-
Promise.resolve(closing).catch(() => void 0),
|
|
2911
|
-
new Promise((resolve) => {
|
|
2912
|
-
timeout = setTimeout(resolve, ABORT_CLEANUP_TIMEOUT_MS);
|
|
2913
|
-
})
|
|
2914
|
-
]);
|
|
2915
|
-
} finally {
|
|
2916
|
-
if (timeout !== void 0) clearTimeout(timeout);
|
|
2917
|
-
}
|
|
2918
|
-
}
|
|
2919
|
-
|
|
2920
2312
|
// src/chat-routes/turn-execution.ts
|
|
2921
2313
|
async function readTurnHistory(store, threadId) {
|
|
2922
2314
|
return (await store.listMessages(threadId)).map((m) => ({
|
|
@@ -3451,7 +2843,7 @@ function createTurnEngine(identity, ctx, log, state, dispatched, produceArgs, tr
|
|
|
3451
2843
|
}
|
|
3452
2844
|
if (event.type === "session.run.failed" || event.type === "error") {
|
|
3453
2845
|
state.runFailed = true;
|
|
3454
|
-
state.lastFailureData = event.data;
|
|
2846
|
+
state.lastFailureData = serverFailureData(event.data);
|
|
3455
2847
|
}
|
|
3456
2848
|
await state.tap.onEvent(event);
|
|
3457
2849
|
if (!state.completionHandoff) state.draft?.notify(event);
|
|
@@ -3866,6 +3258,22 @@ function createChatTurnRoutes(input) {
|
|
|
3866
3258
|
startIntentLine,
|
|
3867
3259
|
log
|
|
3868
3260
|
});
|
|
3261
|
+
const turnHost = input.turnHost && { locationHint: turnStorageBindings(input)?.region, ...input.turnHost };
|
|
3262
|
+
async function hostTurn(host, request, ctx) {
|
|
3263
|
+
try {
|
|
3264
|
+
const response = await forwardTurnToHost(host, request.clone());
|
|
3265
|
+
void request.body?.cancel().catch(() => {
|
|
3266
|
+
});
|
|
3267
|
+
return response;
|
|
3268
|
+
} catch (err) {
|
|
3269
|
+
if (err?.remote === true) throw err;
|
|
3270
|
+
log("[chat-routes] turn host unavailable; the turn runs in this request", {
|
|
3271
|
+
class: "turn_host_unavailable",
|
|
3272
|
+
error: err instanceof Error ? err.name : typeof err
|
|
3273
|
+
});
|
|
3274
|
+
return withD1QueryCount(() => turn(request, ctx));
|
|
3275
|
+
}
|
|
3276
|
+
}
|
|
3869
3277
|
async function turn(request, ctx) {
|
|
3870
3278
|
const requestContext = ctx?.waitUntil ? { ...ctx, waitUntil: ctx.waitUntil.bind(ctx) } : ctx;
|
|
3871
3279
|
const admitted = await admitTurn(request, createTurnTrace({ d1QueryCounter: currentD1QueryCounter() }), options, requestContext?.waitUntil);
|
|
@@ -4035,7 +3443,8 @@ function createChatTurnRoutes(input) {
|
|
|
4035
3443
|
}
|
|
4036
3444
|
return {
|
|
4037
3445
|
prewarm: createChatPrewarm(input, backend?.prewarm),
|
|
4038
|
-
|
|
3446
|
+
// A request with no credential runs here, where `authorize` refuses it, so it never costs an object.
|
|
3447
|
+
turn: (request, ctx) => turnHost && !ctx?.hosted && ctx?.executionLimits === void 0 && ctx?.cancelOnDisconnect !== true && (request.headers.has("cookie") || request.headers.has("authorization")) ? hostTurn(turnHost, request, ctx) : withD1QueryCount(() => turn(request, ctx)),
|
|
4039
3448
|
cancel: (request) => cancelChatTurn(request, { authorize: options.authorize, cancel: options.cancel, log }),
|
|
4040
3449
|
replay,
|
|
4041
3450
|
running,
|
|
@@ -4243,20 +3652,6 @@ export {
|
|
|
4243
3652
|
turnCarriesFiles,
|
|
4244
3653
|
routerTurnHistory,
|
|
4245
3654
|
createHybridTurnProducer,
|
|
4246
|
-
isCommittingSandboxEvent,
|
|
4247
|
-
summarizeFailoverReason,
|
|
4248
|
-
isLiveLifecycleEvent,
|
|
4249
|
-
classifyTerminalFailure,
|
|
4250
|
-
DEFAULT_MODEL_STREAM_OPEN_TIMEOUT_MS,
|
|
4251
|
-
DEFAULT_MODEL_FIRST_RESPONSE_TIMEOUT_MS,
|
|
4252
|
-
ModelFailoverTimeoutError,
|
|
4253
|
-
MAX_EMPTY_TURN_RETRIES,
|
|
4254
|
-
resolveEmptyTurnRetries,
|
|
4255
|
-
streamWithModelFailover,
|
|
4256
|
-
providerRetryNotice,
|
|
4257
|
-
providerResetSentence,
|
|
4258
|
-
MODEL_KEY_REFUSED_CODE,
|
|
4259
|
-
modelKeyRefusedSentence,
|
|
4260
3655
|
createSandboxChatProducer,
|
|
4261
3656
|
withProducerStream,
|
|
4262
3657
|
cancelChatTurn,
|
|
@@ -4270,4 +3665,4 @@ export {
|
|
|
4270
3665
|
createChatTurnRoutes,
|
|
4271
3666
|
streamChatRouteAsSandboxEvents
|
|
4272
3667
|
};
|
|
4273
|
-
//# sourceMappingURL=chunk-
|
|
3668
|
+
//# sourceMappingURL=chunk-CD4SJXYC.js.map
|