@tangle-network/agent-app 0.62.4 → 0.63.0
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/README.md +1 -1
- package/dist/agent-profiles/d1.js +1 -1
- package/dist/agent-profiles/index.d.ts +4 -1
- package/dist/agent-profiles/index.js +1 -1
- package/dist/app-auth/index.js +2 -2
- package/dist/assistant/index.js +7 -7
- package/dist/chat-react/index.js +3 -3
- package/dist/chat-routes/cancellation.d.ts +61 -0
- package/dist/chat-routes/dispatch-first.d.ts +14 -25
- package/dist/chat-routes/early-turn-work.d.ts +10 -0
- package/dist/chat-routes/hybrid-producer.d.ts +4 -3
- package/dist/chat-routes/index.d.ts +2 -1
- package/dist/chat-routes/index.js +86 -1874
- package/dist/chat-routes/index.js.map +1 -1
- package/dist/chat-routes/native-turn-timing.d.ts +15 -0
- package/dist/chat-routes/producer-stream.d.ts +5 -0
- package/dist/chat-routes/route-producer.d.ts +3 -0
- package/dist/chat-routes/route-storage.d.ts +7 -0
- package/dist/chat-routes/router-producer.d.ts +31 -5
- package/dist/chat-routes/turn-admission.d.ts +2 -1
- package/dist/chat-routes/turn-execution.d.ts +6 -11
- package/dist/chat-routes/turn-ownership.d.ts +22 -0
- package/dist/chat-routes/turn-route-contracts.d.ts +112 -105
- package/dist/chat-routes/turn-routes.d.ts +4 -4
- package/dist/chat-routes/wire.d.ts +0 -7
- package/dist/chat-store/index.d.ts +1 -1
- package/dist/chat-store/index.js +21 -3
- package/dist/chat-store/index.js.map +1 -1
- package/dist/chat-store/schema.d.ts +3 -2
- package/dist/chat-store/store.d.ts +8 -0
- package/dist/{chunk-GIEUZBQQ.js → chunk-4CALWPN5.js} +3 -7
- package/dist/chunk-4CALWPN5.js.map +1 -0
- package/dist/chunk-4DBHC63F.js +54 -0
- package/dist/chunk-4DBHC63F.js.map +1 -0
- package/dist/{chunk-G7M2C72Q.js → chunk-5TSRFF5W.js} +1 -1
- package/dist/chunk-5TSRFF5W.js.map +1 -0
- package/dist/chunk-A2NQMVMF.js +485 -0
- package/dist/chunk-A2NQMVMF.js.map +1 -0
- package/dist/{chunk-PCAY23GS.js → chunk-AVBNS64P.js} +172 -2
- package/dist/chunk-AVBNS64P.js.map +1 -0
- package/dist/{chunk-4P5NFBWX.js → chunk-BI5AZAOO.js} +4 -4
- package/dist/chunk-BV3VZEKB.js +483 -0
- package/dist/chunk-BV3VZEKB.js.map +1 -0
- package/dist/{chunk-UW2DKTEM.js → chunk-IDG2XR5O.js} +20 -10
- package/dist/chunk-IDG2XR5O.js.map +1 -0
- package/dist/{chunk-KS4I7KVC.js → chunk-JRTWTADT.js} +184 -123
- package/dist/chunk-JRTWTADT.js.map +1 -0
- package/dist/chunk-KG2Y44XT.js +1 -0
- package/dist/chunk-KG2Y44XT.js.map +1 -0
- package/dist/chunk-MM3GOLHO.js +3365 -0
- package/dist/chunk-MM3GOLHO.js.map +1 -0
- package/dist/chunk-N6URQ6DM.js +225 -0
- package/dist/chunk-N6URQ6DM.js.map +1 -0
- package/dist/{chunk-ISRPOPMM.js → chunk-ND5XWLRU.js} +21 -18
- package/dist/chunk-ND5XWLRU.js.map +1 -0
- package/dist/{chunk-IUVDR3KJ.js → chunk-NHVSO47Q.js} +2 -2
- package/dist/{chunk-FAXSYC2C.js → chunk-RZLCBWGG.js} +2 -2
- package/dist/{chunk-MRZP65DZ.js → chunk-SRWQCWLR.js} +14 -3
- package/dist/chunk-SRWQCWLR.js.map +1 -0
- package/dist/{chunk-JRW6LCIA.js → chunk-V6YGLAQC.js} +2 -2
- package/dist/{chunk-54YIL7WM.js → chunk-WPPDNS63.js} +4 -4
- package/dist/{chunk-CQZVJVME.js → chunk-YCMVNXAX.js} +2 -2
- package/dist/design-canvas/index.js +3 -3
- package/dist/openui/index.js +3 -3
- package/dist/operator/index.js +2 -2
- package/dist/platform/billable-turn-admission.d.ts +13 -9
- package/dist/platform/delegated-keys.d.ts +4 -3
- package/dist/platform/delegated-turn-keys.d.ts +17 -6
- package/dist/platform/index.js +80 -23
- package/dist/platform/index.js.map +1 -1
- package/dist/preset-cloudflare/index.d.ts +2 -0
- package/dist/preset-cloudflare/index.js +344 -0
- package/dist/preset-cloudflare/index.js.map +1 -1
- package/dist/preset-cloudflare/native-completion-owner.d.ts +90 -0
- package/dist/preset-cloudflare/native-completion-store.d.ts +56 -0
- package/dist/preset-cloudflare/native-completion-workflow.d.ts +10 -0
- package/dist/public-consultation/index.js +28 -7
- package/dist/public-consultation/index.js.map +1 -1
- package/dist/rollout/d1.d.ts +103 -3
- package/dist/rollout/d1.js +152 -5
- package/dist/rollout/d1.js.map +1 -1
- package/dist/runtime/index.js +3 -4
- package/dist/runtime/index.js.map +1 -1
- package/dist/runtime/router-turn.d.ts +4 -0
- package/dist/runtime/router-turn.js +2 -3
- package/dist/sandbox/index.js +3 -4
- package/dist/sandbox/sandbox-run-tool.d.ts +5 -0
- package/dist/sequences/index.js +5 -5
- package/dist/signoff/cli.js +6 -1
- package/dist/signoff/cli.js.map +1 -1
- package/dist/signoff/config.d.ts +3 -1
- package/dist/signoff/index.d.ts +2 -1
- package/dist/signoff/index.js +3 -1
- package/dist/signoff/phases.d.ts +3 -0
- package/dist/signoff/proof.js +3 -0
- package/dist/signoff/proof.js.map +1 -1
- package/dist/signoff/run.d.ts +3 -1
- package/dist/signoff/types.d.ts +13 -0
- package/dist/skills-placement/box.js +5 -5
- package/dist/skills-placement/index.js +2 -2
- package/dist/tools/index.js +1 -1
- package/dist/turn-stream/adapters.d.ts +10 -4
- package/dist/turn-stream/index.js +33 -356
- package/dist/turn-stream/index.js.map +1 -1
- package/dist/turn-trace/index.d.ts +1 -0
- package/dist/turn-trace/index.js +4 -5
- package/dist/turn-trace/phase-timing.d.ts +20 -0
- package/dist/turn-trace/recorder.d.ts +3 -0
- package/dist/turn-trace/turn-facts.d.ts +17 -0
- package/dist/web-react/agent-profile-editor.js +1 -1
- package/dist/web-react/chat-stream.d.ts +0 -7
- package/dist/web-react/index.js +7 -9
- package/dist/work-product/index.js +7 -7
- package/package.json +1 -1
- package/dist/chat-routes/minted-thread.d.ts +0 -63
- package/dist/chunk-6R73V3Y5.js +0 -17
- package/dist/chunk-6R73V3Y5.js.map +0 -1
- package/dist/chunk-CCVWR33L.js +0 -99
- package/dist/chunk-CCVWR33L.js.map +0 -1
- package/dist/chunk-EFQUAWWF.js +0 -1749
- package/dist/chunk-EFQUAWWF.js.map +0 -1
- package/dist/chunk-G7M2C72Q.js.map +0 -1
- package/dist/chunk-GIEUZBQQ.js.map +0 -1
- package/dist/chunk-ISRPOPMM.js.map +0 -1
- package/dist/chunk-KS4I7KVC.js.map +0 -1
- package/dist/chunk-MRZP65DZ.js.map +0 -1
- package/dist/chunk-PCAY23GS.js.map +0 -1
- package/dist/chunk-UW2DKTEM.js.map +0 -1
- package/dist/chunk-VK3F4N4U.js +0 -133
- package/dist/chunk-VK3F4N4U.js.map +0 -1
- /package/dist/{chunk-4P5NFBWX.js.map → chunk-BI5AZAOO.js.map} +0 -0
- /package/dist/{chunk-IUVDR3KJ.js.map → chunk-NHVSO47Q.js.map} +0 -0
- /package/dist/{chunk-FAXSYC2C.js.map → chunk-RZLCBWGG.js.map} +0 -0
- /package/dist/{chunk-JRW6LCIA.js.map → chunk-V6YGLAQC.js.map} +0 -0
- /package/dist/{chunk-54YIL7WM.js.map → chunk-WPPDNS63.js.map} +0 -0
- /package/dist/{chunk-CQZVJVME.js.map → chunk-YCMVNXAX.js.map} +0 -0
|
@@ -27,84 +27,90 @@ import {
|
|
|
27
27
|
sanitizeAttachmentFileName,
|
|
28
28
|
sniffBinary,
|
|
29
29
|
sniffedMimeImpliedByName
|
|
30
|
-
} from "../chunk-
|
|
31
|
-
import {
|
|
32
|
-
DEFAULT_STALE_TURN_LOCK_GRACE_MS,
|
|
33
|
-
DEFAULT_TERMINAL_TURN_LOCK_GRACE_MS,
|
|
34
|
-
reconcileStaleTurnLock
|
|
35
|
-
} from "../chunk-CCVWR33L.js";
|
|
30
|
+
} from "../chunk-NHVSO47Q.js";
|
|
36
31
|
import {
|
|
37
32
|
assertSafeKeySegment
|
|
38
33
|
} from "../chunk-6DOQ6T2O.js";
|
|
39
34
|
import {
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
buildModelChain,
|
|
50
|
-
isUpstreamUnavailable,
|
|
51
|
-
readHttpStatusHint,
|
|
52
|
-
readUpstreamUnavailableCode,
|
|
53
|
-
runWithModelFailover
|
|
54
|
-
} from "../chunk-ZLX4BA27.js";
|
|
55
|
-
import "../chunk-DSMGS4IV.js";
|
|
56
|
-
import "../chunk-5BJHKQ33.js";
|
|
57
|
-
import "../chunk-6A7MYOUI.js";
|
|
58
|
-
import "../chunk-O7UX55EU.js";
|
|
59
|
-
import "../chunk-S5SRJJQG.js";
|
|
60
|
-
import "../chunk-XEAZWJ3W.js";
|
|
61
|
-
import "../chunk-7TZA2TAM.js";
|
|
62
|
-
import "../chunk-UGWQLQDS.js";
|
|
63
|
-
import "../chunk-WSQFNMIA.js";
|
|
35
|
+
NATIVE_COMPLETION_RECEIPT_MAX_BYTES,
|
|
36
|
+
RUNTIME_RESTARTED_ERROR,
|
|
37
|
+
TOOL_ABORTED_ERROR,
|
|
38
|
+
aggregateNativeCompletionReceipts,
|
|
39
|
+
boundNativeCompletionReceipt,
|
|
40
|
+
observeNativeCompletion,
|
|
41
|
+
readCompletedSandboxTurn,
|
|
42
|
+
recoverSandboxAssistantMessage
|
|
43
|
+
} from "../chunk-BV3VZEKB.js";
|
|
64
44
|
import {
|
|
65
45
|
runProtectedRouterTurn
|
|
66
|
-
} from "../chunk-
|
|
67
|
-
import {
|
|
68
|
-
runRouterTurn
|
|
69
|
-
} from "../chunk-UW2DKTEM.js";
|
|
70
|
-
import "../chunk-VK3F4N4U.js";
|
|
71
|
-
import "../chunk-D6UJEOZQ.js";
|
|
72
|
-
import "../chunk-AMUXRAUO.js";
|
|
46
|
+
} from "../chunk-YCMVNXAX.js";
|
|
73
47
|
import {
|
|
48
|
+
DEFAULT_MODEL_FIRST_RESPONSE_TIMEOUT_MS,
|
|
49
|
+
DEFAULT_MODEL_STREAM_OPEN_TIMEOUT_MS,
|
|
50
|
+
HYBRID_TURN_GUIDANCE,
|
|
74
51
|
INTENT_LINE_HISTORY_MESSAGES,
|
|
75
52
|
INTENT_LINE_MAX_CHARS,
|
|
76
53
|
INTENT_LINE_TIMEOUT_MS,
|
|
77
|
-
|
|
78
|
-
|
|
54
|
+
MAX_EMPTY_TURN_RETRIES,
|
|
55
|
+
ModelFailoverTimeoutError,
|
|
56
|
+
cancelChatTurn,
|
|
57
|
+
classifyTerminalFailure,
|
|
79
58
|
createChatTurnRoutes,
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
59
|
+
createHybridTurnProducer,
|
|
60
|
+
createRouterTurnProducer,
|
|
61
|
+
createSandboxChatProducer,
|
|
62
|
+
isCommittingSandboxEvent,
|
|
63
|
+
isLiveLifecycleEvent,
|
|
64
|
+
observeTurnCancellation,
|
|
65
|
+
resolveEmptyTurnRetries,
|
|
66
|
+
routerTurnHistory,
|
|
67
|
+
startHybridBox,
|
|
68
|
+
streamChatRouteAsSandboxEvents,
|
|
69
|
+
streamWithModelFailover,
|
|
70
|
+
summarizeFailoverReason,
|
|
71
|
+
turnCarriesFiles,
|
|
72
|
+
turnPromptText,
|
|
73
|
+
withProducerStream
|
|
74
|
+
} from "../chunk-MM3GOLHO.js";
|
|
88
75
|
import "../chunk-KH4QSBNI.js";
|
|
89
76
|
import {
|
|
90
77
|
sniffMimeFromName
|
|
91
78
|
} from "../chunk-CQGEGPY7.js";
|
|
92
79
|
import "../chunk-RN4WPTBH.js";
|
|
93
80
|
import "../chunk-WP2GYFZ4.js";
|
|
94
|
-
import "../chunk-
|
|
81
|
+
import "../chunk-4DBHC63F.js";
|
|
82
|
+
import {
|
|
83
|
+
redactErrorMessage
|
|
84
|
+
} from "../chunk-MH74DY2I.js";
|
|
85
|
+
import "../chunk-MM6RLOXF.js";
|
|
86
|
+
import {
|
|
87
|
+
flattenHistory
|
|
88
|
+
} from "../chunk-ND5XWLRU.js";
|
|
89
|
+
import "../chunk-F4TUZYMX.js";
|
|
90
|
+
import {
|
|
91
|
+
assistantRowIdForTurn,
|
|
92
|
+
createAssistantDraftWriter,
|
|
93
|
+
isDraftContentEvent,
|
|
94
|
+
rowIdOf,
|
|
95
|
+
storeSupportsDraftPersistence
|
|
96
|
+
} from "../chunk-N6URQ6DM.js";
|
|
95
97
|
import "../chunk-Z4RD5UF7.js";
|
|
96
98
|
import "../chunk-YG66IHDU.js";
|
|
99
|
+
import {
|
|
100
|
+
DEFAULT_STALE_TURN_LOCK_GRACE_MS,
|
|
101
|
+
DEFAULT_TERMINAL_TURN_LOCK_GRACE_MS,
|
|
102
|
+
reconcileStaleTurnLock
|
|
103
|
+
} from "../chunk-A2NQMVMF.js";
|
|
97
104
|
import {
|
|
98
105
|
coalesceDeltas,
|
|
99
106
|
createBufferedTurnTap
|
|
100
107
|
} from "../chunk-ISP6ZH3G.js";
|
|
101
|
-
import "../chunk-MM6RLOXF.js";
|
|
102
108
|
import {
|
|
103
109
|
attachmentInputToPart,
|
|
104
110
|
attachmentKindForMime,
|
|
105
111
|
historyContentWithAttachments,
|
|
106
112
|
toChatMessageParts
|
|
107
|
-
} from "../chunk-
|
|
113
|
+
} from "../chunk-RZLCBWGG.js";
|
|
108
114
|
import "../chunk-ZVEEWGDK.js";
|
|
109
115
|
import {
|
|
110
116
|
ChatTurnInputError,
|
|
@@ -138,32 +144,28 @@ import {
|
|
|
138
144
|
settleTurnResponse,
|
|
139
145
|
turnResponseEventFor,
|
|
140
146
|
validateSandboxMentionPath
|
|
141
|
-
} from "../chunk-
|
|
147
|
+
} from "../chunk-5TSRFF5W.js";
|
|
142
148
|
import {
|
|
143
|
-
asRecord,
|
|
144
|
-
asString,
|
|
145
|
-
collapseRedundantTextParts,
|
|
146
|
-
draftAssistantParts,
|
|
147
|
-
finalizeAssistantParts,
|
|
148
|
-
finalizePendingInteractionParts,
|
|
149
149
|
getPartKey,
|
|
150
|
-
mergePersistedPart
|
|
151
|
-
normalizePersistedPart,
|
|
152
|
-
normalizeToolEvent,
|
|
153
|
-
terminalizeDanglingAssistantToolUpdates
|
|
150
|
+
mergePersistedPart
|
|
154
151
|
} from "../chunk-VUBJZEZB.js";
|
|
155
|
-
import
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
interactionToPersistedPart,
|
|
159
|
-
isRenderableInteractionKind,
|
|
160
|
-
noticePart,
|
|
161
|
-
noticePartKey,
|
|
162
|
-
parseInteractionCancel,
|
|
163
|
-
parseInteractionRequest
|
|
164
|
-
} from "../chunk-6VQCFJWP.js";
|
|
152
|
+
import "../chunk-6VQCFJWP.js";
|
|
153
|
+
import "../chunk-PK65B6HP.js";
|
|
154
|
+
import "../chunk-ZLX4BA27.js";
|
|
165
155
|
import "../chunk-XELWWXJJ.js";
|
|
166
|
-
import "../chunk-
|
|
156
|
+
import "../chunk-DSMGS4IV.js";
|
|
157
|
+
import "../chunk-5BJHKQ33.js";
|
|
158
|
+
import "../chunk-6A7MYOUI.js";
|
|
159
|
+
import "../chunk-O7UX55EU.js";
|
|
160
|
+
import "../chunk-XEAZWJ3W.js";
|
|
161
|
+
import "../chunk-S5SRJJQG.js";
|
|
162
|
+
import "../chunk-7TZA2TAM.js";
|
|
163
|
+
import "../chunk-UGWQLQDS.js";
|
|
164
|
+
import "../chunk-WSQFNMIA.js";
|
|
165
|
+
import "../chunk-IDG2XR5O.js";
|
|
166
|
+
import "../chunk-D6UJEOZQ.js";
|
|
167
|
+
import "../chunk-AVBNS64P.js";
|
|
168
|
+
import "../chunk-AMUXRAUO.js";
|
|
167
169
|
import {
|
|
168
170
|
readSandboxBinaryBytes,
|
|
169
171
|
statSandboxFileSize
|
|
@@ -174,1325 +176,12 @@ import {
|
|
|
174
176
|
isWorkspaceFileExportable
|
|
175
177
|
} from "../chunk-TXD5HXLE.js";
|
|
176
178
|
import {
|
|
177
|
-
parsePlanSubmittedEvent,
|
|
178
179
|
persistedPartToPlan,
|
|
179
|
-
planFollowUpTurnId
|
|
180
|
-
planToPersistedPart
|
|
180
|
+
planFollowUpTurnId
|
|
181
181
|
} from "../chunk-WL7PZK7Q.js";
|
|
182
182
|
import "../chunk-6Z7TZVKZ.js";
|
|
183
183
|
import "../chunk-57XPEL7X.js";
|
|
184
184
|
|
|
185
|
-
// src/chat-routes/model-failover-stream.ts
|
|
186
|
-
var TERMINAL_FAILURE_TYPES = /* @__PURE__ */ new Set(["error", "session.run.failed"]);
|
|
187
|
-
var NON_COMMITTING_PART_TYPES = /* @__PURE__ */ new Set(["step-start", "step-finish"]);
|
|
188
|
-
function isCommittingSandboxEvent(event) {
|
|
189
|
-
const record2 = asRecord(event);
|
|
190
|
-
if (!record2) return false;
|
|
191
|
-
const type = asString(record2.type) ?? "";
|
|
192
|
-
if (!type) return false;
|
|
193
|
-
if (type === "message.part.updated") {
|
|
194
|
-
const part = asRecord(asRecord(record2.data)?.part);
|
|
195
|
-
const partType = asString(part?.type) ?? "";
|
|
196
|
-
if (NON_COMMITTING_PART_TYPES.has(partType)) return false;
|
|
197
|
-
if (partType === "text" || partType === "reasoning") {
|
|
198
|
-
const text = asString(part?.text) ?? asString(part?.content) ?? "";
|
|
199
|
-
return text.length > 0;
|
|
200
|
-
}
|
|
201
|
-
return true;
|
|
202
|
-
}
|
|
203
|
-
if (type === "start" || type === "execution.started" || type === "status" || type === "model-processing" || type === "model.processing" || // Public Sandbox timing metadata: a request was attempted, not answered.
|
|
204
|
-
type === "model.first_request" || type === "session.created" || type === "session.updated" || type === "session.idle" || type === "step-start" || type === "step-finish" || type === "turn" || type === "warning") {
|
|
205
|
-
return false;
|
|
206
|
-
}
|
|
207
|
-
return true;
|
|
208
|
-
}
|
|
209
|
-
function summarizeFailoverReason(reason) {
|
|
210
|
-
const collapsed = reason.replace(/\s+/g, " ").trim();
|
|
211
|
-
if (!collapsed) return "upstream unavailable";
|
|
212
|
-
if (/<!doctype html|<html[\s>]/i.test(collapsed)) {
|
|
213
|
-
const status = readHttpStatusHint(collapsed);
|
|
214
|
-
return status ? `HTTP ${status}` : "upstream returned an error page";
|
|
215
|
-
}
|
|
216
|
-
const LIMIT = 160;
|
|
217
|
-
if (collapsed.length <= LIMIT) return collapsed;
|
|
218
|
-
const clipped = collapsed.slice(0, LIMIT);
|
|
219
|
-
const lastSpace = clipped.lastIndexOf(" ");
|
|
220
|
-
return `${(lastSpace > LIMIT - 30 ? clipped.slice(0, lastSpace) : clipped).trimEnd()}\u2026`;
|
|
221
|
-
}
|
|
222
|
-
function isLiveLifecycleEvent(event) {
|
|
223
|
-
const type = asString(asRecord(event)?.type) ?? "";
|
|
224
|
-
return type === "start" || type === "execution.started" || type === "status" || type === "model-processing" || type === "model.processing" || // Public Sandbox timing metadata: a request was attempted, not answered.
|
|
225
|
-
type === "model.first_request" || type === "session.created" || type === "session.updated" || type === "session.idle";
|
|
226
|
-
}
|
|
227
|
-
function classifyTerminalFailure(event) {
|
|
228
|
-
const record2 = asRecord(event);
|
|
229
|
-
if (!record2) return null;
|
|
230
|
-
const type = asString(record2.type) ?? "";
|
|
231
|
-
if (!TERMINAL_FAILURE_TYPES.has(type)) return null;
|
|
232
|
-
const data = asRecord(record2.data);
|
|
233
|
-
const outage = isUpstreamUnavailable(data) || isUpstreamUnavailable(record2);
|
|
234
|
-
const reason = asString(data?.message) ?? asString(data?.error) ?? asString(data?.reason) ?? asString(record2.message) ?? `sandbox stream reported ${type}`;
|
|
235
|
-
const code = readUpstreamUnavailableCode(data) ?? asString(data?.errorCode) ?? asString(data?.code) ?? readUpstreamUnavailableCode(record2) ?? asString(record2.errorCode) ?? asString(record2.code);
|
|
236
|
-
return { outage, reason, ...code ? { code } : {} };
|
|
237
|
-
}
|
|
238
|
-
var DEFAULT_MODEL_STREAM_OPEN_TIMEOUT_MS = 12e4;
|
|
239
|
-
var DEFAULT_MODEL_FIRST_RESPONSE_TIMEOUT_MS = 6e4;
|
|
240
|
-
var ModelFailoverTimeoutError = class extends ModelFailoverExhaustedError {
|
|
241
|
-
code;
|
|
242
|
-
model;
|
|
243
|
-
timeoutMs;
|
|
244
|
-
constructor(input) {
|
|
245
|
-
super(input.attempts);
|
|
246
|
-
this.name = "ModelFailoverTimeoutError";
|
|
247
|
-
this.code = input.code;
|
|
248
|
-
this.model = input.model;
|
|
249
|
-
this.timeoutMs = input.timeoutMs;
|
|
250
|
-
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.`;
|
|
251
|
-
}
|
|
252
|
-
};
|
|
253
|
-
function resolveTimeoutMs(value, fallback, label) {
|
|
254
|
-
if (value === void 0) return fallback;
|
|
255
|
-
if (!Number.isFinite(value) || value <= 0) {
|
|
256
|
-
throw new RangeError(`${label} must be a finite number greater than 0`);
|
|
257
|
-
}
|
|
258
|
-
return Math.max(1, Math.trunc(value));
|
|
259
|
-
}
|
|
260
|
-
var TIMED_OUT = /* @__PURE__ */ Symbol("model-attempt-timed-out");
|
|
261
|
-
function deadlineAfter(timeoutMs) {
|
|
262
|
-
let timer;
|
|
263
|
-
const promise = new Promise((resolve) => {
|
|
264
|
-
timer = setTimeout(() => resolve(TIMED_OUT), timeoutMs);
|
|
265
|
-
});
|
|
266
|
-
return {
|
|
267
|
-
promise,
|
|
268
|
-
clear: () => {
|
|
269
|
-
if (timer !== void 0) {
|
|
270
|
-
clearTimeout(timer);
|
|
271
|
-
timer = void 0;
|
|
272
|
-
}
|
|
273
|
-
}
|
|
274
|
-
};
|
|
275
|
-
}
|
|
276
|
-
function isEmptyTerminalReceipt(event) {
|
|
277
|
-
const record2 = asRecord(event);
|
|
278
|
-
if (!record2) return false;
|
|
279
|
-
const type = asString(record2.type) ?? "";
|
|
280
|
-
if (type !== "result" && type !== "done") return false;
|
|
281
|
-
const data = asRecord(record2.data);
|
|
282
|
-
const text = asString(data?.finalText) ?? asString(data?.text) ?? "";
|
|
283
|
-
return text.trim().length === 0;
|
|
284
|
-
}
|
|
285
|
-
var MAX_EMPTY_TURN_RETRIES = 3;
|
|
286
|
-
function resolveEmptyTurnRetries(value) {
|
|
287
|
-
if (typeof value !== "number" || !Number.isFinite(value)) return 0;
|
|
288
|
-
const truncated = Math.trunc(value);
|
|
289
|
-
if (truncated <= 0) return 0;
|
|
290
|
-
return Math.min(truncated, MAX_EMPTY_TURN_RETRIES);
|
|
291
|
-
}
|
|
292
|
-
async function closeIterator(iterator, log) {
|
|
293
|
-
try {
|
|
294
|
-
await iterator.return?.();
|
|
295
|
-
} catch (err) {
|
|
296
|
-
log?.("[chat-routes] abandoning a failed model stream threw", {
|
|
297
|
-
error: err instanceof Error ? err.message : String(err)
|
|
298
|
-
});
|
|
299
|
-
}
|
|
300
|
-
}
|
|
301
|
-
function streamWithModelFailover(options) {
|
|
302
|
-
const committing = options.isCommitting ?? isCommittingSandboxEvent;
|
|
303
|
-
const emptyTurnRetries = resolveEmptyTurnRetries(options.emptyTurnRetries);
|
|
304
|
-
const openTimeoutMs = resolveTimeoutMs(
|
|
305
|
-
options.openTimeoutMs,
|
|
306
|
-
DEFAULT_MODEL_STREAM_OPEN_TIMEOUT_MS,
|
|
307
|
-
"openTimeoutMs"
|
|
308
|
-
);
|
|
309
|
-
const firstResponseTimeoutMs = resolveTimeoutMs(
|
|
310
|
-
options.firstResponseTimeoutMs,
|
|
311
|
-
DEFAULT_MODEL_FIRST_RESPONSE_TIMEOUT_MS,
|
|
312
|
-
"firstResponseTimeoutMs"
|
|
313
|
-
);
|
|
314
|
-
const liveLifecycle = options.liveLifecycleEvents === true;
|
|
315
|
-
const progress = createProgressQueue();
|
|
316
|
-
let serving;
|
|
317
|
-
let trail = [];
|
|
318
|
-
let fellBack = false;
|
|
319
|
-
const attemptState = { index: 0 };
|
|
320
|
-
let finalTimeout;
|
|
321
|
-
const { drainOnce } = createModelAttemptDrainer(attemptState, options, openTimeoutMs, firstResponseTimeoutMs, committing, liveLifecycle, progress);
|
|
322
|
-
const probe = async (model) => {
|
|
323
|
-
finalTimeout = void 0;
|
|
324
|
-
for (let retry = 0; ; retry += 1) {
|
|
325
|
-
const pass = await drainOnce(model);
|
|
326
|
-
if (!pass.empty || retry >= emptyTurnRetries) {
|
|
327
|
-
if (!pass.outcome.committed && pass.outcome.timeoutCode) {
|
|
328
|
-
finalTimeout = {
|
|
329
|
-
code: pass.outcome.timeoutCode,
|
|
330
|
-
model,
|
|
331
|
-
timeoutMs: pass.outcome.timeoutCode === "model_stream_open_timeout" ? openTimeoutMs : firstResponseTimeoutMs
|
|
332
|
-
};
|
|
333
|
-
} else {
|
|
334
|
-
finalTimeout = void 0;
|
|
335
|
-
}
|
|
336
|
-
return pass.outcome;
|
|
337
|
-
}
|
|
338
|
-
const iterator = pass.outcome.committed ? pass.outcome.iterator : null;
|
|
339
|
-
if (pass.outcome.committed) pass.outcome.abort();
|
|
340
|
-
if (iterator) void closeIterator(iterator, options.log);
|
|
341
|
-
const info = { model, retry: retry + 1, remaining: emptyTurnRetries - retry - 1 };
|
|
342
|
-
options.log?.("[chat-routes] turn completed with no assistant text; re-running the same model", {
|
|
343
|
-
...info
|
|
344
|
-
});
|
|
345
|
-
options.onEmptyTurnRetry?.(info);
|
|
346
|
-
}
|
|
347
|
-
};
|
|
348
|
-
const events = (async function* () {
|
|
349
|
-
let handle;
|
|
350
|
-
const settled = runWithModelFailover({
|
|
351
|
-
models: options.models,
|
|
352
|
-
run: probe,
|
|
353
|
-
...options.policy ? { policy: options.policy } : {},
|
|
354
|
-
// The probe has already classified the raw payload with
|
|
355
|
-
// `isUpstreamUnavailable`; this reads its verdict rather than
|
|
356
|
-
// re-classifying a wrapper object, so the two can never disagree.
|
|
357
|
-
isUnavailableResult: (result) => result.committed === false,
|
|
358
|
-
onFallback: (attempt, nextModel) => {
|
|
359
|
-
const info = {
|
|
360
|
-
from: attempt.model,
|
|
361
|
-
to: nextModel,
|
|
362
|
-
reason: attempt.reason ?? "upstream unavailable"
|
|
363
|
-
};
|
|
364
|
-
options.log?.("[chat-routes] model upstream unavailable; falling over", { ...info });
|
|
365
|
-
options.onFallback?.(info);
|
|
366
|
-
}
|
|
367
|
-
}).finally(() => progress.close());
|
|
368
|
-
settled.catch(() => void 0);
|
|
369
|
-
for (; ; ) {
|
|
370
|
-
const item = await progress.pull();
|
|
371
|
-
if (item === PROGRESS_CLOSED) break;
|
|
372
|
-
yield item;
|
|
373
|
-
}
|
|
374
|
-
try {
|
|
375
|
-
const outcome = await settled;
|
|
376
|
-
serving = outcome.model;
|
|
377
|
-
trail = outcome.attempts;
|
|
378
|
-
fellBack = outcome.usedFallback;
|
|
379
|
-
handle = outcome.value;
|
|
380
|
-
} catch (err) {
|
|
381
|
-
const attempts = err?.attempts;
|
|
382
|
-
if (Array.isArray(attempts)) trail = attempts;
|
|
383
|
-
if (err instanceof ModelFailoverExhaustedError && finalTimeout) {
|
|
384
|
-
throw new ModelFailoverTimeoutError({
|
|
385
|
-
attempts: trail,
|
|
386
|
-
...finalTimeout
|
|
387
|
-
});
|
|
388
|
-
}
|
|
389
|
-
throw err;
|
|
390
|
-
}
|
|
391
|
-
for (const event of handle.buffered) yield event;
|
|
392
|
-
const live = handle.iterator;
|
|
393
|
-
if (!live) {
|
|
394
|
-
handle.abort();
|
|
395
|
-
return;
|
|
396
|
-
}
|
|
397
|
-
try {
|
|
398
|
-
for (; ; ) {
|
|
399
|
-
const next = await live.next();
|
|
400
|
-
if (next.done) return;
|
|
401
|
-
yield next.value;
|
|
402
|
-
}
|
|
403
|
-
} finally {
|
|
404
|
-
handle.abort();
|
|
405
|
-
await closeIterator(live, options.log);
|
|
406
|
-
}
|
|
407
|
-
})();
|
|
408
|
-
return {
|
|
409
|
-
events,
|
|
410
|
-
servingModel: () => serving,
|
|
411
|
-
attempts: () => trail,
|
|
412
|
-
usedFallback: () => fellBack
|
|
413
|
-
};
|
|
414
|
-
}
|
|
415
|
-
var PROGRESS_CLOSED = /* @__PURE__ */ Symbol("progress-closed");
|
|
416
|
-
function createProgressQueue() {
|
|
417
|
-
const items = [];
|
|
418
|
-
const waiters = [];
|
|
419
|
-
let closed = false;
|
|
420
|
-
const deliver = (value) => {
|
|
421
|
-
const waiter = waiters.shift();
|
|
422
|
-
if (waiter) waiter(value);
|
|
423
|
-
else items.push(value);
|
|
424
|
-
};
|
|
425
|
-
return {
|
|
426
|
-
push(event) {
|
|
427
|
-
if (closed) return;
|
|
428
|
-
deliver(event);
|
|
429
|
-
},
|
|
430
|
-
close() {
|
|
431
|
-
if (closed) return;
|
|
432
|
-
closed = true;
|
|
433
|
-
deliver(PROGRESS_CLOSED);
|
|
434
|
-
},
|
|
435
|
-
pull() {
|
|
436
|
-
if (items.length > 0) return Promise.resolve(items.shift());
|
|
437
|
-
if (closed) return Promise.resolve(PROGRESS_CLOSED);
|
|
438
|
-
return new Promise((resolve) => waiters.push(resolve));
|
|
439
|
-
}
|
|
440
|
-
};
|
|
441
|
-
}
|
|
442
|
-
function createModelAttemptDrainer(attemptState, options, openTimeoutMs, firstResponseTimeoutMs, committing, liveLifecycle, progress) {
|
|
443
|
-
const drainOnce = async (model) => {
|
|
444
|
-
attemptState.index += 1;
|
|
445
|
-
const controller = new AbortController();
|
|
446
|
-
const abort = () => {
|
|
447
|
-
if (!controller.signal.aborted) controller.abort();
|
|
448
|
-
};
|
|
449
|
-
const opening = Promise.resolve().then(() => options.open({
|
|
450
|
-
model,
|
|
451
|
-
attempt: attemptState.index,
|
|
452
|
-
signal: controller.signal
|
|
453
|
-
}));
|
|
454
|
-
const openDeadline = deadlineAfter(openTimeoutMs);
|
|
455
|
-
let opened;
|
|
456
|
-
try {
|
|
457
|
-
opened = await Promise.race([opening, openDeadline.promise]);
|
|
458
|
-
} catch (err) {
|
|
459
|
-
openDeadline.clear();
|
|
460
|
-
abort();
|
|
461
|
-
throw err;
|
|
462
|
-
}
|
|
463
|
-
if (opened === TIMED_OUT) {
|
|
464
|
-
openDeadline.clear();
|
|
465
|
-
abort();
|
|
466
|
-
void opening.then(
|
|
467
|
-
(lateSource) => {
|
|
468
|
-
try {
|
|
469
|
-
void closeIterator(lateSource[Symbol.asyncIterator](), options.log);
|
|
470
|
-
} catch (err) {
|
|
471
|
-
options.log?.("[chat-routes] closing a late model stream threw", {
|
|
472
|
-
error: err instanceof Error ? err.message : String(err)
|
|
473
|
-
});
|
|
474
|
-
}
|
|
475
|
-
},
|
|
476
|
-
(err) => {
|
|
477
|
-
options.log?.("[chat-routes] abandoned model stream open rejected after timeout", {
|
|
478
|
-
error: err instanceof Error ? err.message : String(err)
|
|
479
|
-
});
|
|
480
|
-
}
|
|
481
|
-
);
|
|
482
|
-
return {
|
|
483
|
-
outcome: {
|
|
484
|
-
committed: false,
|
|
485
|
-
error: `Opening the model event source for ${model} exceeded ${openTimeoutMs}ms`,
|
|
486
|
-
errorCode: "model_stream_open_timeout",
|
|
487
|
-
timeoutCode: "model_stream_open_timeout"
|
|
488
|
-
},
|
|
489
|
-
empty: false
|
|
490
|
-
};
|
|
491
|
-
}
|
|
492
|
-
const source = opened;
|
|
493
|
-
let iterator;
|
|
494
|
-
try {
|
|
495
|
-
iterator = source[Symbol.asyncIterator]();
|
|
496
|
-
} catch (err) {
|
|
497
|
-
openDeadline.clear();
|
|
498
|
-
abort();
|
|
499
|
-
throw err;
|
|
500
|
-
}
|
|
501
|
-
const buffered = [];
|
|
502
|
-
let next;
|
|
503
|
-
try {
|
|
504
|
-
next = await Promise.race([
|
|
505
|
-
Promise.resolve(iterator.next()),
|
|
506
|
-
openDeadline.promise
|
|
507
|
-
]);
|
|
508
|
-
} catch (err) {
|
|
509
|
-
openDeadline.clear();
|
|
510
|
-
abort();
|
|
511
|
-
void closeIterator(iterator, options.log);
|
|
512
|
-
throw err;
|
|
513
|
-
}
|
|
514
|
-
openDeadline.clear();
|
|
515
|
-
if (next === TIMED_OUT) {
|
|
516
|
-
abort();
|
|
517
|
-
void closeIterator(iterator, options.log);
|
|
518
|
-
return {
|
|
519
|
-
outcome: {
|
|
520
|
-
committed: false,
|
|
521
|
-
error: `Starting the model event source for ${model} exceeded ${openTimeoutMs}ms`,
|
|
522
|
-
errorCode: "model_stream_open_timeout",
|
|
523
|
-
timeoutCode: "model_stream_open_timeout"
|
|
524
|
-
},
|
|
525
|
-
empty: false
|
|
526
|
-
};
|
|
527
|
-
}
|
|
528
|
-
if (next.done) {
|
|
529
|
-
abort();
|
|
530
|
-
return { outcome: { committed: true, buffered, iterator: null, abort }, empty: true };
|
|
531
|
-
}
|
|
532
|
-
const firstResponseDeadline = deadlineAfter(firstResponseTimeoutMs);
|
|
533
|
-
for (; ; ) {
|
|
534
|
-
const event = next.value;
|
|
535
|
-
let failure;
|
|
536
|
-
let commits = false;
|
|
537
|
-
try {
|
|
538
|
-
failure = classifyTerminalFailure(event);
|
|
539
|
-
commits = !failure && committing(event);
|
|
540
|
-
} catch (err) {
|
|
541
|
-
firstResponseDeadline.clear();
|
|
542
|
-
abort();
|
|
543
|
-
void closeIterator(iterator, options.log);
|
|
544
|
-
throw err;
|
|
545
|
-
}
|
|
546
|
-
if (failure?.outage) {
|
|
547
|
-
firstResponseDeadline.clear();
|
|
548
|
-
abort();
|
|
549
|
-
void closeIterator(iterator, options.log);
|
|
550
|
-
return {
|
|
551
|
-
outcome: {
|
|
552
|
-
committed: false,
|
|
553
|
-
error: failure.reason,
|
|
554
|
-
...failure.code ? { errorCode: failure.code } : {}
|
|
555
|
-
},
|
|
556
|
-
empty: false
|
|
557
|
-
};
|
|
558
|
-
}
|
|
559
|
-
if (liveLifecycle && !failure && !commits && isLiveLifecycleEvent(event)) {
|
|
560
|
-
progress.push(event);
|
|
561
|
-
} else {
|
|
562
|
-
buffered.push(event);
|
|
563
|
-
}
|
|
564
|
-
if (failure) {
|
|
565
|
-
firstResponseDeadline.clear();
|
|
566
|
-
return { outcome: { committed: true, buffered, iterator, abort }, empty: false };
|
|
567
|
-
}
|
|
568
|
-
if (commits) {
|
|
569
|
-
firstResponseDeadline.clear();
|
|
570
|
-
return { outcome: { committed: true, buffered, iterator, abort }, empty: isEmptyTerminalReceipt(event) };
|
|
571
|
-
}
|
|
572
|
-
try {
|
|
573
|
-
next = await Promise.race([
|
|
574
|
-
Promise.resolve(iterator.next()),
|
|
575
|
-
firstResponseDeadline.promise
|
|
576
|
-
]);
|
|
577
|
-
} catch (err) {
|
|
578
|
-
firstResponseDeadline.clear();
|
|
579
|
-
abort();
|
|
580
|
-
void closeIterator(iterator, options.log);
|
|
581
|
-
throw err;
|
|
582
|
-
}
|
|
583
|
-
if (next === TIMED_OUT) {
|
|
584
|
-
firstResponseDeadline.clear();
|
|
585
|
-
abort();
|
|
586
|
-
void closeIterator(iterator, options.log);
|
|
587
|
-
return {
|
|
588
|
-
outcome: {
|
|
589
|
-
committed: false,
|
|
590
|
-
error: `Model ${model} produced no first answer-bearing event within ${firstResponseTimeoutMs}ms`,
|
|
591
|
-
errorCode: "provider_first_response_timeout",
|
|
592
|
-
timeoutCode: "provider_first_response_timeout"
|
|
593
|
-
},
|
|
594
|
-
empty: false
|
|
595
|
-
};
|
|
596
|
-
}
|
|
597
|
-
if (next.done) {
|
|
598
|
-
firstResponseDeadline.clear();
|
|
599
|
-
abort();
|
|
600
|
-
return { outcome: { committed: true, buffered, iterator: null, abort }, empty: true };
|
|
601
|
-
}
|
|
602
|
-
}
|
|
603
|
-
};
|
|
604
|
-
return { drainOnce };
|
|
605
|
-
}
|
|
606
|
-
|
|
607
|
-
// src/chat-routes/sandbox-model-source.ts
|
|
608
|
-
function parseEffectiveBackend(data) {
|
|
609
|
-
const backend = asRecord(data.effectiveBackend);
|
|
610
|
-
if (!backend) return void 0;
|
|
611
|
-
const provider = asString(backend.provider);
|
|
612
|
-
const model = asString(backend.model);
|
|
613
|
-
const rawSource = asString(backend.source);
|
|
614
|
-
const source = rawSource === "request" || rawSource === "environment" || rawSource === "profile" ? rawSource : void 0;
|
|
615
|
-
if (!provider && !model && !source) return void 0;
|
|
616
|
-
return {
|
|
617
|
-
...model ? { servedModel: model } : {},
|
|
618
|
-
...provider ? { servedProvider: provider } : {},
|
|
619
|
-
...source ? { servedSource: source } : {}
|
|
620
|
-
};
|
|
621
|
-
}
|
|
622
|
-
function createSandboxModelSource(options, log) {
|
|
623
|
-
if (options.openEvents && !options.model) {
|
|
624
|
-
throw new Error(
|
|
625
|
-
"createSandboxChatProducer: `openEvents` requires `model` \u2014 failover must know which model it is running"
|
|
626
|
-
);
|
|
627
|
-
}
|
|
628
|
-
if (!options.openEvents && !options.events) {
|
|
629
|
-
throw new Error("createSandboxChatProducer: pass `openEvents` (failover-capable) or `events`");
|
|
630
|
-
}
|
|
631
|
-
if (!options.openEvents && resolveEmptyTurnRetries(options.emptyTurnRetries) > 0) {
|
|
632
|
-
throw new Error(
|
|
633
|
-
"createSandboxChatProducer: `emptyTurnRetries` requires `openEvents` \u2014 a fixed `events` stream cannot be re-opened"
|
|
634
|
-
);
|
|
635
|
-
}
|
|
636
|
-
const chain = options.model ? buildModelChain(options.model, options.modelFailover === false ? [] : options.fallbackModels ?? []) : [];
|
|
637
|
-
const pendingModelNotices = [];
|
|
638
|
-
let modelNoticeCount = 0;
|
|
639
|
-
let failover;
|
|
640
|
-
let source;
|
|
641
|
-
if (options.openEvents) {
|
|
642
|
-
failover = streamWithModelFailover({
|
|
643
|
-
models: chain,
|
|
644
|
-
open: options.openEvents,
|
|
645
|
-
log,
|
|
646
|
-
...options.modelPolicy ? { policy: options.modelPolicy } : {},
|
|
647
|
-
...options.openTimeoutMs !== void 0 ? { openTimeoutMs: options.openTimeoutMs } : {},
|
|
648
|
-
...options.firstResponseTimeoutMs !== void 0 ? { firstResponseTimeoutMs: options.firstResponseTimeoutMs } : {},
|
|
649
|
-
...options.emptyTurnRetries !== void 0 ? { emptyTurnRetries: options.emptyTurnRetries } : {},
|
|
650
|
-
...options.onEmptyTurnRetry ? { onEmptyTurnRetry: options.onEmptyTurnRetry } : {},
|
|
651
|
-
...options.liveLifecycleEvents !== void 0 ? { liveLifecycleEvents: options.liveLifecycleEvents } : {},
|
|
652
|
-
onFallback: (info) => {
|
|
653
|
-
modelNoticeCount += 1;
|
|
654
|
-
pendingModelNotices.push({
|
|
655
|
-
id: `model-fallback-${modelNoticeCount}`,
|
|
656
|
-
// Named models on both sides: a quality regression after a downgrade
|
|
657
|
-
// must be attributable to the model that actually answered. The
|
|
658
|
-
// REASON is condensed — a real edge 5xx arrives as Cloudflare's HTML
|
|
659
|
-
// error page, and the raw text put `<!DOCTYPE html><!--[if lt IE 7]>…`
|
|
660
|
-
// into the customer's transcript. The verbatim text is still on
|
|
661
|
-
// `modelFailover.attempts[].reason` for the operator.
|
|
662
|
-
text: `${info.from} was unavailable (${summarizeFailoverReason(info.reason)}) \u2014 answered with ${info.to} instead.`
|
|
663
|
-
});
|
|
664
|
-
options.onModelFallback?.(info);
|
|
665
|
-
}
|
|
666
|
-
});
|
|
667
|
-
source = failover.events;
|
|
668
|
-
} else {
|
|
669
|
-
source = options.events;
|
|
670
|
-
}
|
|
671
|
-
const servingModel = () => failover?.servingModel() ?? options.model;
|
|
672
|
-
const modelState = { effectiveBackend: void 0 };
|
|
673
|
-
let substitutionNoticeQueued = false;
|
|
674
|
-
const servedModel = () => modelState.effectiveBackend?.servedModel ?? servingModel();
|
|
675
|
-
const captureEffectiveBackend = (data) => {
|
|
676
|
-
if (!data) return;
|
|
677
|
-
const parsed = parseEffectiveBackend(data);
|
|
678
|
-
if (!parsed) return;
|
|
679
|
-
modelState.effectiveBackend = parsed;
|
|
680
|
-
const sentModel = servingModel();
|
|
681
|
-
if (substitutionNoticeQueued || !options.model || !sentModel || !parsed.servedModel || parsed.servedModel === sentModel) return;
|
|
682
|
-
substitutionNoticeQueued = true;
|
|
683
|
-
pendingModelNotices.push({
|
|
684
|
-
id: "model-substitution-1",
|
|
685
|
-
text: `Requested ${sentModel} \u2014 the sandbox answered with ${parsed.servedModel} instead.`
|
|
686
|
-
});
|
|
687
|
-
};
|
|
688
|
-
return { pendingModelNotices, source, captureEffectiveBackend, servedModel, servingModel, failover, modelState };
|
|
689
|
-
}
|
|
690
|
-
|
|
691
|
-
// src/chat-routes/reasoning-clock.ts
|
|
692
|
-
function createReasoningClock(now = Date.now) {
|
|
693
|
-
const runs = /* @__PURE__ */ new Map();
|
|
694
|
-
let open = null;
|
|
695
|
-
const settle = (at) => {
|
|
696
|
-
if (!open) return;
|
|
697
|
-
const time = runs.get(open.run);
|
|
698
|
-
if (time && time.end === void 0) time.end = Math.max(time.start ?? 0, at ?? now());
|
|
699
|
-
open = null;
|
|
700
|
-
};
|
|
701
|
-
return {
|
|
702
|
-
reasoning(run) {
|
|
703
|
-
if (open && open.run === run) return;
|
|
704
|
-
settle();
|
|
705
|
-
const time = runs.get(run);
|
|
706
|
-
if (time) delete time.end;
|
|
707
|
-
else runs.set(run, { start: now() });
|
|
708
|
-
open = { run };
|
|
709
|
-
},
|
|
710
|
-
settle,
|
|
711
|
-
time(run, own) {
|
|
712
|
-
const time = runs.get(run);
|
|
713
|
-
if (!time) return own;
|
|
714
|
-
const start = own?.start ?? time.start;
|
|
715
|
-
const end = own?.end ?? time.end;
|
|
716
|
-
return { ...start !== void 0 ? { start } : {}, ...end !== void 0 ? { end } : {} };
|
|
717
|
-
}
|
|
718
|
-
};
|
|
719
|
-
}
|
|
720
|
-
|
|
721
|
-
// src/chat-routes/sandbox-turn-usage.ts
|
|
722
|
-
function addStepFinishUsage(part, usage) {
|
|
723
|
-
const tokens = asRecord(part.tokens);
|
|
724
|
-
if (tokens) {
|
|
725
|
-
const cache = asRecord(tokens.cache);
|
|
726
|
-
const add = (current, value) => {
|
|
727
|
-
const n = Number(value);
|
|
728
|
-
if (!Number.isFinite(n)) return current;
|
|
729
|
-
return (current ?? 0) + n;
|
|
730
|
-
};
|
|
731
|
-
usage.inputTokens = add(usage.inputTokens, tokens.input);
|
|
732
|
-
usage.outputTokens = add(usage.outputTokens, tokens.output);
|
|
733
|
-
usage.reasoningTokens = add(usage.reasoningTokens, tokens.reasoning);
|
|
734
|
-
if (cache) {
|
|
735
|
-
usage.cacheReadTokens = add(usage.cacheReadTokens, cache.read);
|
|
736
|
-
usage.cacheWriteTokens = add(usage.cacheWriteTokens, cache.write);
|
|
737
|
-
}
|
|
738
|
-
}
|
|
739
|
-
const cost = Number(part.cost);
|
|
740
|
-
if (Number.isFinite(cost)) usage.costUsd = (usage.costUsd ?? 0) + cost;
|
|
741
|
-
}
|
|
742
|
-
|
|
743
|
-
// src/chat-routes/sandbox-producer.ts
|
|
744
|
-
function textDelta(tracker, key, part, rawDelta) {
|
|
745
|
-
const explicit = typeof rawDelta === "string" ? rawDelta : void 0;
|
|
746
|
-
const previous = tracker.seen.get(key) ?? "";
|
|
747
|
-
if (explicit !== void 0) {
|
|
748
|
-
tracker.seen.set(key, previous + explicit);
|
|
749
|
-
return explicit;
|
|
750
|
-
}
|
|
751
|
-
const snapshot = asString(part.text) ?? asString(part.content) ?? "";
|
|
752
|
-
if (!snapshot) return "";
|
|
753
|
-
if (snapshot.startsWith(previous)) {
|
|
754
|
-
tracker.seen.set(key, snapshot);
|
|
755
|
-
return snapshot.slice(previous.length);
|
|
756
|
-
}
|
|
757
|
-
tracker.seen.set(key, snapshot);
|
|
758
|
-
return snapshot;
|
|
759
|
-
}
|
|
760
|
-
function toFiniteNumber(value) {
|
|
761
|
-
if (typeof value === "number") return Number.isFinite(value) ? value : null;
|
|
762
|
-
if (typeof value === "string" && value.trim() !== "") {
|
|
763
|
-
const parsed = Number(value);
|
|
764
|
-
return Number.isFinite(parsed) ? parsed : null;
|
|
765
|
-
}
|
|
766
|
-
return null;
|
|
767
|
-
}
|
|
768
|
-
function extractReportedTurnUsage(data) {
|
|
769
|
-
const tokenUsage = asRecord(data?.tokenUsage);
|
|
770
|
-
if (!tokenUsage) return null;
|
|
771
|
-
const inputTokens = toFiniteNumber(tokenUsage.inputTokens);
|
|
772
|
-
const outputTokens = toFiniteNumber(tokenUsage.outputTokens);
|
|
773
|
-
if (inputTokens === null || outputTokens === null) return null;
|
|
774
|
-
const reported = { inputTokens, outputTokens };
|
|
775
|
-
const reasoningTokens = toFiniteNumber(tokenUsage.reasoningTokens);
|
|
776
|
-
if (reasoningTokens !== null) reported.reasoningTokens = reasoningTokens;
|
|
777
|
-
const cacheReadTokens = toFiniteNumber(tokenUsage.cacheReadInputTokens);
|
|
778
|
-
if (cacheReadTokens !== null) reported.cacheReadTokens = cacheReadTokens;
|
|
779
|
-
const cacheWriteTokens = toFiniteNumber(tokenUsage.cacheCreationInputTokens);
|
|
780
|
-
if (cacheWriteTokens !== null) reported.cacheWriteTokens = cacheWriteTokens;
|
|
781
|
-
const costUsd = toFiniteNumber(data?.totalCostUsd) ?? toFiniteNumber(tokenUsage.cost);
|
|
782
|
-
if (costUsd !== null) reported.costUsd = costUsd;
|
|
783
|
-
return reported;
|
|
784
|
-
}
|
|
785
|
-
function extractUsageSnapshot(value) {
|
|
786
|
-
const inputTokens = toFiniteNumber(value?.promptTokens);
|
|
787
|
-
const outputTokens = toFiniteNumber(value?.completionTokens);
|
|
788
|
-
if (inputTokens === null || outputTokens === null || inputTokens < 0 || outputTokens < 0) return null;
|
|
789
|
-
const reported = { inputTokens, outputTokens };
|
|
790
|
-
const reasoningTokens = toFiniteNumber(value?.reasoningTokens);
|
|
791
|
-
if (reasoningTokens !== null && reasoningTokens >= 0) reported.reasoningTokens = reasoningTokens;
|
|
792
|
-
const costUsd = toFiniteNumber(value?.providerCostUsd);
|
|
793
|
-
if (costUsd !== null && costUsd >= 0) reported.costUsd = costUsd;
|
|
794
|
-
return reported;
|
|
795
|
-
}
|
|
796
|
-
function applyTerminalUsage(reported, usage) {
|
|
797
|
-
usage.inputTokens = reported.inputTokens;
|
|
798
|
-
usage.outputTokens = reported.outputTokens;
|
|
799
|
-
if (reported.reasoningTokens !== void 0) usage.reasoningTokens = reported.reasoningTokens;
|
|
800
|
-
if (reported.cacheReadTokens !== void 0) usage.cacheReadTokens = reported.cacheReadTokens;
|
|
801
|
-
if (reported.cacheWriteTokens !== void 0) usage.cacheWriteTokens = reported.cacheWriteTokens;
|
|
802
|
-
if (reported.costUsd !== void 0) usage.costUsd = reported.costUsd;
|
|
803
|
-
}
|
|
804
|
-
function sandboxStreamErrorMessage(data) {
|
|
805
|
-
const record2 = asRecord(data);
|
|
806
|
-
const message = asString(record2?.message) ?? asString(record2?.error);
|
|
807
|
-
if (message) return message;
|
|
808
|
-
try {
|
|
809
|
-
return JSON.stringify(data) || "Sandbox stream returned an error event without details.";
|
|
810
|
-
} catch {
|
|
811
|
-
return "Sandbox stream returned an error event without details.";
|
|
812
|
-
}
|
|
813
|
-
}
|
|
814
|
-
function sandboxStreamErrorCode(data) {
|
|
815
|
-
return asString(asRecord(data)?.code) ?? "sandbox.stream_error";
|
|
816
|
-
}
|
|
817
|
-
function toProducerWireEvent(event) {
|
|
818
|
-
return event;
|
|
819
|
-
}
|
|
820
|
-
function sandboxStreamFailureDiagnostic(error) {
|
|
821
|
-
const streamMessage = error?.streamMessage;
|
|
822
|
-
const message = typeof streamMessage === "string" ? streamMessage : error instanceof Error ? error.message : "Sandbox stream failed";
|
|
823
|
-
const diagnostics = asRecord(error?.diagnostics);
|
|
824
|
-
let diagnosticText;
|
|
825
|
-
if (diagnostics) {
|
|
826
|
-
try {
|
|
827
|
-
diagnosticText = JSON.stringify(diagnostics);
|
|
828
|
-
} catch {
|
|
829
|
-
diagnosticText = "[unserializable diagnostics]";
|
|
830
|
-
}
|
|
831
|
-
}
|
|
832
|
-
const userMessage = [
|
|
833
|
-
"The sandbox model stream stopped before a clean completion.",
|
|
834
|
-
`Error: ${message}`,
|
|
835
|
-
"Please retry. If it repeats, send these support details to the team."
|
|
836
|
-
].join("\n\n");
|
|
837
|
-
const failureNote = diagnosticText ? `sandbox-stream: ${message}; ${diagnosticText}` : `sandbox-stream: ${message}`;
|
|
838
|
-
let code = "sandbox.stream_failed";
|
|
839
|
-
if (error instanceof ModelFailoverTimeoutError) {
|
|
840
|
-
code = error.code;
|
|
841
|
-
} else if (error instanceof ModelFailoverExhaustedError) {
|
|
842
|
-
code = error.upstreamCode ?? code;
|
|
843
|
-
}
|
|
844
|
-
return { userMessage, failureNote, code };
|
|
845
|
-
}
|
|
846
|
-
function createSandboxChatProducer(options) {
|
|
847
|
-
const log = options.log ?? ((message, meta) => console.error(message, meta ?? ""));
|
|
848
|
-
const renderable = options.isRenderableInteraction ?? isRenderableInteractionKind;
|
|
849
|
-
const { pendingModelNotices, source, captureEffectiveBackend, servedModel, servingModel, failover, modelState } = createSandboxModelSource(options, log);
|
|
850
|
-
const projectionState = {
|
|
851
|
-
fullText: "",
|
|
852
|
-
terminalObserved: false,
|
|
853
|
-
stepCounter: 0,
|
|
854
|
-
danglingToolsTerminalized: false,
|
|
855
|
-
interactionOutcome: "answered",
|
|
856
|
-
warningCount: 0,
|
|
857
|
-
errorNoticeCount: 0
|
|
858
|
-
};
|
|
859
|
-
const { timedParts, drainModelNotices, tracker, recordPersistedPart, reasoningClock, partMap, announcedToolArgs, settledTools, usage, promotedFileParts, emitFinalUsage, emitTerminalizedTools, emitErrorNotice, partOrder } = createSandboxProjection(projectionState, pendingModelNotices);
|
|
860
|
-
async function* stream() {
|
|
861
|
-
try {
|
|
862
|
-
for await (const raw of source) {
|
|
863
|
-
const record2 = asRecord(raw);
|
|
864
|
-
captureEffectiveBackend(asRecord(record2?.data));
|
|
865
|
-
yield* drainModelNotices();
|
|
866
|
-
if (!record2 || typeof record2.type !== "string") continue;
|
|
867
|
-
const normalized = normalizeToolEvent({ type: record2.type, data: asRecord(record2.data) });
|
|
868
|
-
const event = normalized.type === "message.part.updated" ? normalized : { type: record2.type, data: asRecord(record2.data) };
|
|
869
|
-
if (event.type === "message.part.updated") {
|
|
870
|
-
yield* projectSandboxMessagePart(event, tracker, recordPersistedPart, reasoningClock, projectionState, partMap, announcedToolArgs, settledTools, usage, options, log, promotedFileParts);
|
|
871
|
-
continue;
|
|
872
|
-
}
|
|
873
|
-
if (event.type === "interaction") {
|
|
874
|
-
const parsed = parseInteractionRequest(asRecord(record2.data));
|
|
875
|
-
if (!parsed.succeeded) {
|
|
876
|
-
log("[chat-routes] dropping malformed interaction event", { error: parsed.error });
|
|
877
|
-
continue;
|
|
878
|
-
}
|
|
879
|
-
if (renderable(parsed.value.kind, parsed.value)) {
|
|
880
|
-
recordPersistedPart(
|
|
881
|
-
interactionToPersistedPart(parsed.value, "pending"),
|
|
882
|
-
void 0,
|
|
883
|
-
interactionPartKey(parsed.value.id)
|
|
884
|
-
);
|
|
885
|
-
yield toProducerWireEvent(record2);
|
|
886
|
-
continue;
|
|
887
|
-
}
|
|
888
|
-
let declineFailed = false;
|
|
889
|
-
if (options.declineInteraction) {
|
|
890
|
-
try {
|
|
891
|
-
await options.declineInteraction(parsed.value.id);
|
|
892
|
-
} catch (err) {
|
|
893
|
-
declineFailed = true;
|
|
894
|
-
log("[chat-routes] failed to auto-decline interaction", {
|
|
895
|
-
id: parsed.value.id,
|
|
896
|
-
error: err instanceof Error ? err.message : String(err)
|
|
897
|
-
});
|
|
898
|
-
}
|
|
899
|
-
} else {
|
|
900
|
-
declineFailed = true;
|
|
901
|
-
log("[chat-routes] non-renderable interaction with no declineInteraction wired", {
|
|
902
|
-
id: parsed.value.id,
|
|
903
|
-
kind: parsed.value.kind
|
|
904
|
-
});
|
|
905
|
-
}
|
|
906
|
-
const text = options.declinedNotice?.(parsed.value, declineFailed) ?? (declineFailed ? `The agent requested ${parsed.value.kind} approval; declining it failed \u2014 it will expire on its own.` : `The agent requested ${parsed.value.kind} approval \u2014 auto-declined by policy.`);
|
|
907
|
-
const notice = noticePart("auto-declined", `auto-declined-${parsed.value.id}`, text);
|
|
908
|
-
recordPersistedPart(notice, void 0, noticePartKey(notice.id));
|
|
909
|
-
yield { type: "notice", id: notice.id, noticeKind: "auto-declined", text };
|
|
910
|
-
continue;
|
|
911
|
-
}
|
|
912
|
-
if (event.type === "interaction.cancel") {
|
|
913
|
-
const parsed = parseInteractionCancel(asRecord(record2.data));
|
|
914
|
-
if (!parsed.succeeded) {
|
|
915
|
-
log("[chat-routes] dropping malformed interaction.cancel event", { error: parsed.error });
|
|
916
|
-
continue;
|
|
917
|
-
}
|
|
918
|
-
const key = interactionPartKey(parsed.value.id);
|
|
919
|
-
const existing = partMap.get(key);
|
|
920
|
-
if (existing?.type === "interaction" && existing.status === "pending") {
|
|
921
|
-
recordPersistedPart({
|
|
922
|
-
...existing,
|
|
923
|
-
status: cancelStatusFor(parsed.value.reason),
|
|
924
|
-
...parsed.value.reason ? { cancelReason: parsed.value.reason } : {}
|
|
925
|
-
}, void 0, key);
|
|
926
|
-
}
|
|
927
|
-
yield toProducerWireEvent(record2);
|
|
928
|
-
continue;
|
|
929
|
-
}
|
|
930
|
-
if (event.type === "plan.submitted") {
|
|
931
|
-
const parsed = parsePlanSubmittedEvent(record2);
|
|
932
|
-
if (!parsed.succeeded) {
|
|
933
|
-
log("[chat-routes] dropping malformed plan.submitted event", { error: parsed.error });
|
|
934
|
-
continue;
|
|
935
|
-
}
|
|
936
|
-
recordPersistedPart(planToPersistedPart(parsed.value), void 0);
|
|
937
|
-
yield toProducerWireEvent(record2);
|
|
938
|
-
continue;
|
|
939
|
-
}
|
|
940
|
-
if (event.type === "warning") {
|
|
941
|
-
const message = asString(event.data?.message);
|
|
942
|
-
if (message) {
|
|
943
|
-
projectionState.warningCount += 1;
|
|
944
|
-
const code = asString(event.data?.code);
|
|
945
|
-
const text = code ? `${code}: ${message}` : message;
|
|
946
|
-
const notice = noticePart("warning", `warning-${projectionState.warningCount}`, text);
|
|
947
|
-
recordPersistedPart(notice, void 0, noticePartKey(notice.id));
|
|
948
|
-
yield { type: "notice", id: notice.id, noticeKind: "warning", text };
|
|
949
|
-
}
|
|
950
|
-
yield toProducerWireEvent(record2);
|
|
951
|
-
continue;
|
|
952
|
-
}
|
|
953
|
-
if (event.type === "usage") {
|
|
954
|
-
const reported = extractUsageSnapshot(asRecord(record2.usage));
|
|
955
|
-
if (reported) applyTerminalUsage(reported, usage);
|
|
956
|
-
yield toProducerWireEvent(record2);
|
|
957
|
-
continue;
|
|
958
|
-
}
|
|
959
|
-
if (event.type === "result") {
|
|
960
|
-
projectionState.terminalObserved = true;
|
|
961
|
-
const finalText = asString(event.data?.finalText);
|
|
962
|
-
if (finalText) projectionState.fullText = finalText;
|
|
963
|
-
const reported = extractReportedTurnUsage(asRecord(event.data));
|
|
964
|
-
if (reported) {
|
|
965
|
-
applyTerminalUsage(reported, usage);
|
|
966
|
-
yield* emitFinalUsage();
|
|
967
|
-
} else {
|
|
968
|
-
const resultUsage = asRecord(event.data?.usage);
|
|
969
|
-
if (resultUsage) {
|
|
970
|
-
const input = Number(resultUsage.inputTokens);
|
|
971
|
-
const output = Number(resultUsage.outputTokens);
|
|
972
|
-
if (Number.isFinite(input)) usage.inputTokens = input;
|
|
973
|
-
if (Number.isFinite(output)) usage.outputTokens = output;
|
|
974
|
-
}
|
|
975
|
-
}
|
|
976
|
-
yield* emitTerminalizedTools();
|
|
977
|
-
continue;
|
|
978
|
-
}
|
|
979
|
-
if (event.type === "done") {
|
|
980
|
-
projectionState.terminalObserved = true;
|
|
981
|
-
const reported = extractReportedTurnUsage(asRecord(event.data));
|
|
982
|
-
if (reported) {
|
|
983
|
-
applyTerminalUsage(reported, usage);
|
|
984
|
-
yield* emitFinalUsage();
|
|
985
|
-
}
|
|
986
|
-
yield* emitTerminalizedTools();
|
|
987
|
-
yield toProducerWireEvent(record2);
|
|
988
|
-
continue;
|
|
989
|
-
}
|
|
990
|
-
if (event.type === "error") {
|
|
991
|
-
const message = sandboxStreamErrorMessage(event.data);
|
|
992
|
-
const errorContent = projectionState.fullText.trim() ? `The sandbox model stream stopped before a clean completion.
|
|
993
|
-
|
|
994
|
-
Error: ${message}` : `The sandbox agent returned an error before producing a visible answer.
|
|
995
|
-
|
|
996
|
-
Error: ${message}`;
|
|
997
|
-
if (options.errorNotice) {
|
|
998
|
-
yield* emitErrorNotice(errorContent, sandboxStreamErrorCode(event.data));
|
|
999
|
-
} else if (options.includeErrorText !== false) {
|
|
1000
|
-
const errorDelta = projectionState.fullText ? `
|
|
1001
|
-
|
|
1002
|
-
---
|
|
1003
|
-
${errorContent}` : errorContent;
|
|
1004
|
-
projectionState.fullText += errorDelta;
|
|
1005
|
-
yield { type: "text", text: errorDelta };
|
|
1006
|
-
}
|
|
1007
|
-
projectionState.interactionOutcome = "expired";
|
|
1008
|
-
yield* emitTerminalizedTools();
|
|
1009
|
-
yield toProducerWireEvent(record2);
|
|
1010
|
-
continue;
|
|
1011
|
-
}
|
|
1012
|
-
yield toProducerWireEvent(record2);
|
|
1013
|
-
}
|
|
1014
|
-
yield* drainModelNotices();
|
|
1015
|
-
} catch (streamErr) {
|
|
1016
|
-
const diagnostic = sandboxStreamFailureDiagnostic(streamErr);
|
|
1017
|
-
log("[chat-routes] sandbox stream failed", {
|
|
1018
|
-
failureNote: diagnostic.failureNote,
|
|
1019
|
-
error: streamErr instanceof Error ? streamErr.message : String(streamErr)
|
|
1020
|
-
});
|
|
1021
|
-
if (options.errorNotice) {
|
|
1022
|
-
yield* emitErrorNotice(diagnostic.userMessage, diagnostic.code);
|
|
1023
|
-
} else if (options.includeErrorText !== false) {
|
|
1024
|
-
const errorDelta = projectionState.fullText ? `
|
|
1025
|
-
|
|
1026
|
-
---
|
|
1027
|
-
${diagnostic.userMessage}` : diagnostic.userMessage;
|
|
1028
|
-
projectionState.fullText += errorDelta;
|
|
1029
|
-
yield { type: "text", text: errorDelta };
|
|
1030
|
-
}
|
|
1031
|
-
projectionState.interactionOutcome = "expired";
|
|
1032
|
-
yield* emitTerminalizedTools();
|
|
1033
|
-
yield {
|
|
1034
|
-
type: "error",
|
|
1035
|
-
data: {
|
|
1036
|
-
message: diagnostic.userMessage,
|
|
1037
|
-
code: diagnostic.code,
|
|
1038
|
-
details: { failureNote: diagnostic.failureNote }
|
|
1039
|
-
}
|
|
1040
|
-
};
|
|
1041
|
-
} finally {
|
|
1042
|
-
reasoningClock.settle();
|
|
1043
|
-
}
|
|
1044
|
-
}
|
|
1045
|
-
return {
|
|
1046
|
-
stream: stream(),
|
|
1047
|
-
finalText: () => projectionState.fullText,
|
|
1048
|
-
terminalObserved: () => projectionState.terminalObserved,
|
|
1049
|
-
assistantParts: () => finalizePendingInteractionParts(
|
|
1050
|
-
finalizeAssistantParts(partOrder, timedParts(), projectionState.fullText),
|
|
1051
|
-
projectionState.interactionOutcome
|
|
1052
|
-
),
|
|
1053
|
-
// Mid-stream snapshot for incremental persistence: the same accumulators,
|
|
1054
|
-
// WITHOUT the two completion-time settlements (`terminalizeDanglingTool*`
|
|
1055
|
-
// and `finalizePendingInteractionParts`). A running tool and an unanswered
|
|
1056
|
-
// ask are the correct live state; settling them early would persist
|
|
1057
|
-
// phantom failures the final write then reverses.
|
|
1058
|
-
draftParts: () => draftAssistantParts(partOrder, timedParts(), projectionState.fullText),
|
|
1059
|
-
usage: () => usage,
|
|
1060
|
-
// A GETTER, not a captured value: `turn-routes` reads `producer.model` at
|
|
1061
|
-
// draft-snapshot and persist time, both of which happen after the stream
|
|
1062
|
-
// resolved which model serves. A plain property would freeze the PREFERRED
|
|
1063
|
-
// model into the row and make a downgrade unattributable — the exact
|
|
1064
|
-
// failure mode this work exists to prevent.
|
|
1065
|
-
get model() {
|
|
1066
|
-
return servedModel();
|
|
1067
|
-
},
|
|
1068
|
-
modelFailover: () => ({
|
|
1069
|
-
model: servingModel(),
|
|
1070
|
-
attempts: failover?.attempts() ?? [],
|
|
1071
|
-
usedFallback: failover?.usedFallback() ?? false
|
|
1072
|
-
}),
|
|
1073
|
-
modelAttribution: () => ({
|
|
1074
|
-
...options.model ? { requestedModel: options.model } : {},
|
|
1075
|
-
...modelState.effectiveBackend ?? {},
|
|
1076
|
-
echoReceived: modelState.effectiveBackend !== void 0
|
|
1077
|
-
})
|
|
1078
|
-
};
|
|
1079
|
-
}
|
|
1080
|
-
function createSandboxProjection(projectionState, pendingModelNotices) {
|
|
1081
|
-
const partOrder = [];
|
|
1082
|
-
const partMap = /* @__PURE__ */ new Map();
|
|
1083
|
-
const tracker = { seen: /* @__PURE__ */ new Map() };
|
|
1084
|
-
const reasoningClock = createReasoningClock();
|
|
1085
|
-
const usage = {};
|
|
1086
|
-
const announcedToolArgs = /* @__PURE__ */ new Map();
|
|
1087
|
-
const settledTools = /* @__PURE__ */ new Set();
|
|
1088
|
-
const promotedFileParts = /* @__PURE__ */ new Map();
|
|
1089
|
-
function recordPersistedPart(part, delta, keyOverride) {
|
|
1090
|
-
const persisted = normalizePersistedPart(part);
|
|
1091
|
-
if (!persisted) return;
|
|
1092
|
-
const key = keyOverride ?? getPartKey(persisted);
|
|
1093
|
-
if (!partMap.has(key)) partOrder.push(key);
|
|
1094
|
-
partMap.set(key, mergePersistedPart(partMap.get(key), persisted, delta));
|
|
1095
|
-
}
|
|
1096
|
-
function* emitTerminalizedTools() {
|
|
1097
|
-
if (projectionState.danglingToolsTerminalized) return;
|
|
1098
|
-
projectionState.danglingToolsTerminalized = true;
|
|
1099
|
-
const updates = terminalizeDanglingAssistantToolUpdates(partOrder, partMap, projectionState.fullText);
|
|
1100
|
-
for (const part of updates) {
|
|
1101
|
-
const toolId = asString(part.id);
|
|
1102
|
-
if (!toolId || settledTools.has(toolId)) continue;
|
|
1103
|
-
settledTools.add(toolId);
|
|
1104
|
-
const state = asRecord(part.state);
|
|
1105
|
-
yield {
|
|
1106
|
-
type: "tool_result",
|
|
1107
|
-
toolCallId: toolId,
|
|
1108
|
-
toolName: asString(part.tool) ?? "tool",
|
|
1109
|
-
outcome: {
|
|
1110
|
-
ok: false,
|
|
1111
|
-
...asString(state?.error) ? { message: asString(state?.error) } : {}
|
|
1112
|
-
}
|
|
1113
|
-
};
|
|
1114
|
-
}
|
|
1115
|
-
}
|
|
1116
|
-
function* emitFinalUsage() {
|
|
1117
|
-
const promptTokens = usage.inputTokens ?? 0;
|
|
1118
|
-
const completionTokens = usage.outputTokens ?? 0;
|
|
1119
|
-
if (promptTokens || completionTokens) {
|
|
1120
|
-
yield {
|
|
1121
|
-
type: "usage",
|
|
1122
|
-
usage: {
|
|
1123
|
-
promptTokens,
|
|
1124
|
-
completionTokens,
|
|
1125
|
-
...usage.reasoningTokens !== void 0 ? { reasoningTokens: usage.reasoningTokens } : {},
|
|
1126
|
-
...usage.costUsd !== void 0 ? { providerCostUsd: usage.costUsd } : {}
|
|
1127
|
-
}
|
|
1128
|
-
};
|
|
1129
|
-
}
|
|
1130
|
-
}
|
|
1131
|
-
function* drainModelNotices() {
|
|
1132
|
-
while (pendingModelNotices.length > 0) {
|
|
1133
|
-
const queued = pendingModelNotices.shift();
|
|
1134
|
-
const notice = noticePart("warning", queued.id, queued.text);
|
|
1135
|
-
recordPersistedPart(notice, void 0, noticePartKey(notice.id));
|
|
1136
|
-
yield { type: "notice", id: notice.id, noticeKind: "warning", text: queued.text };
|
|
1137
|
-
}
|
|
1138
|
-
}
|
|
1139
|
-
function* emitErrorNotice(text, code) {
|
|
1140
|
-
projectionState.errorNoticeCount += 1;
|
|
1141
|
-
const notice = noticePart("error", `error-${projectionState.errorNoticeCount}`, text, code);
|
|
1142
|
-
recordPersistedPart(notice, void 0, noticePartKey(notice.id));
|
|
1143
|
-
yield { type: "notice", id: notice.id, noticeKind: "error", text, code };
|
|
1144
|
-
}
|
|
1145
|
-
const timedParts = () => {
|
|
1146
|
-
const timed = new Map(partMap);
|
|
1147
|
-
for (const [key, part] of partMap) {
|
|
1148
|
-
if (part.type !== "reasoning") continue;
|
|
1149
|
-
const time = reasoningClock.time(key, asRecord(part.time));
|
|
1150
|
-
if (time) timed.set(key, { ...part, time });
|
|
1151
|
-
}
|
|
1152
|
-
return timed;
|
|
1153
|
-
};
|
|
1154
|
-
return { timedParts, drainModelNotices, tracker, recordPersistedPart, reasoningClock, partMap, announcedToolArgs, settledTools, usage, promotedFileParts, emitFinalUsage, emitTerminalizedTools, emitErrorNotice, partOrder };
|
|
1155
|
-
}
|
|
1156
|
-
async function* projectSandboxMessagePart(event, tracker, recordPersistedPart, reasoningClock, projectionState, partMap, announcedToolArgs, settledTools, usage, options, log, promotedFileParts) {
|
|
1157
|
-
if (event.type === "message.part.updated") {
|
|
1158
|
-
const part = asRecord(event.data?.part);
|
|
1159
|
-
if (!part) return;
|
|
1160
|
-
const rawDelta = event.data?.delta;
|
|
1161
|
-
const partType = String(part.type ?? "");
|
|
1162
|
-
if (partType === "text" || partType === "reasoning") {
|
|
1163
|
-
const key = getPartKey(part);
|
|
1164
|
-
const delta = textDelta(tracker, key, part, rawDelta);
|
|
1165
|
-
recordPersistedPart(part, delta || void 0);
|
|
1166
|
-
if (delta) {
|
|
1167
|
-
if (partType === "reasoning") reasoningClock.reasoning(key);
|
|
1168
|
-
else reasoningClock.settle();
|
|
1169
|
-
if (partType === "text") projectionState.fullText += delta;
|
|
1170
|
-
yield { type: partType, text: delta };
|
|
1171
|
-
}
|
|
1172
|
-
return;
|
|
1173
|
-
}
|
|
1174
|
-
if (partType === "tool") {
|
|
1175
|
-
reasoningClock.settle();
|
|
1176
|
-
recordPersistedPart(part, void 0);
|
|
1177
|
-
const persisted = partMap.get(getPartKey(part));
|
|
1178
|
-
const state = asRecord(persisted?.state);
|
|
1179
|
-
const toolId = String(persisted?.id ?? "");
|
|
1180
|
-
const toolName = String(persisted?.tool ?? "tool");
|
|
1181
|
-
const args = asRecord(state?.input) ?? {};
|
|
1182
|
-
const hasPopulatedArgs = Object.keys(args).length > 0;
|
|
1183
|
-
const announcedWithPopulatedArgs = announcedToolArgs.get(toolId);
|
|
1184
|
-
if (toolId && (announcedWithPopulatedArgs === void 0 || !announcedWithPopulatedArgs && hasPopulatedArgs && !settledTools.has(toolId))) {
|
|
1185
|
-
announcedToolArgs.set(toolId, hasPopulatedArgs);
|
|
1186
|
-
yield {
|
|
1187
|
-
type: "tool_call",
|
|
1188
|
-
call: { toolCallId: toolId, toolName, args }
|
|
1189
|
-
};
|
|
1190
|
-
}
|
|
1191
|
-
const status = String(state?.status ?? "");
|
|
1192
|
-
if (toolId && (status === "completed" || status === "error") && !settledTools.has(toolId)) {
|
|
1193
|
-
settledTools.add(toolId);
|
|
1194
|
-
yield {
|
|
1195
|
-
type: "tool_result",
|
|
1196
|
-
toolCallId: toolId,
|
|
1197
|
-
toolName,
|
|
1198
|
-
outcome: {
|
|
1199
|
-
ok: status === "completed",
|
|
1200
|
-
...state?.output !== void 0 ? { result: state.output } : {},
|
|
1201
|
-
...asString(state?.error) ? { message: asString(state?.error) } : {}
|
|
1202
|
-
}
|
|
1203
|
-
};
|
|
1204
|
-
}
|
|
1205
|
-
return;
|
|
1206
|
-
}
|
|
1207
|
-
if (partType === "step-finish") {
|
|
1208
|
-
addStepFinishUsage(part, usage);
|
|
1209
|
-
recordPersistedPart(part, void 0, `step-finish:#${projectionState.stepCounter++}`);
|
|
1210
|
-
const promptTokens = usage.inputTokens ?? 0;
|
|
1211
|
-
const completionTokens = usage.outputTokens ?? 0;
|
|
1212
|
-
if (promptTokens || completionTokens) {
|
|
1213
|
-
yield {
|
|
1214
|
-
type: "usage",
|
|
1215
|
-
usage: {
|
|
1216
|
-
promptTokens,
|
|
1217
|
-
completionTokens,
|
|
1218
|
-
...usage.reasoningTokens !== void 0 ? { reasoningTokens: usage.reasoningTokens } : {},
|
|
1219
|
-
...usage.costUsd !== void 0 ? { providerCostUsd: usage.costUsd } : {}
|
|
1220
|
-
}
|
|
1221
|
-
};
|
|
1222
|
-
}
|
|
1223
|
-
return;
|
|
1224
|
-
}
|
|
1225
|
-
if (partType === "step-start") {
|
|
1226
|
-
recordPersistedPart(part, void 0, `step-start:#${projectionState.stepCounter}`);
|
|
1227
|
-
return;
|
|
1228
|
-
}
|
|
1229
|
-
if (partType === "file" && options.promoteFilePart) {
|
|
1230
|
-
const promote = options.promoteFilePart;
|
|
1231
|
-
const rawId = asString(part.id);
|
|
1232
|
-
const rawUrl = asString(part.url);
|
|
1233
|
-
const memoKey = rawId ? `id:${rawId}` : rawUrl ? `url:${rawUrl}` : void 0;
|
|
1234
|
-
const attempt = () => Promise.resolve().then(() => promote(part)).catch((err) => {
|
|
1235
|
-
const reason = err instanceof Error ? err.message : String(err);
|
|
1236
|
-
log("[chat-routes] file part promotion threw", { key: memoKey ?? "(keyless)", error: reason });
|
|
1237
|
-
return { succeeded: false, reason };
|
|
1238
|
-
});
|
|
1239
|
-
let pending;
|
|
1240
|
-
if (memoKey) {
|
|
1241
|
-
pending = promotedFileParts.get(memoKey) ?? attempt();
|
|
1242
|
-
promotedFileParts.set(memoKey, pending);
|
|
1243
|
-
} else {
|
|
1244
|
-
pending = attempt();
|
|
1245
|
-
}
|
|
1246
|
-
const outcome = await pending;
|
|
1247
|
-
if (outcome.succeeded) {
|
|
1248
|
-
recordPersistedPart(outcome.part, void 0, outcome.key);
|
|
1249
|
-
} else if (outcome.part) {
|
|
1250
|
-
recordPersistedPart(outcome.part, void 0, outcome.key);
|
|
1251
|
-
} else {
|
|
1252
|
-
recordPersistedPart(part, void 0);
|
|
1253
|
-
}
|
|
1254
|
-
return;
|
|
1255
|
-
}
|
|
1256
|
-
recordPersistedPart(part, void 0);
|
|
1257
|
-
return;
|
|
1258
|
-
}
|
|
1259
|
-
}
|
|
1260
|
-
|
|
1261
|
-
// src/chat-routes/router-producer.ts
|
|
1262
|
-
function createRouterTurnProducer(options) {
|
|
1263
|
-
const queue = [];
|
|
1264
|
-
let wake = null;
|
|
1265
|
-
let done = false;
|
|
1266
|
-
let outcome = null;
|
|
1267
|
-
let usageReported = false;
|
|
1268
|
-
const parts = [];
|
|
1269
|
-
const toolParts = /* @__PURE__ */ new Map();
|
|
1270
|
-
let textPart = null;
|
|
1271
|
-
let textSegment = 0;
|
|
1272
|
-
let reasoningPart = null;
|
|
1273
|
-
const reasoningClock = createReasoningClock();
|
|
1274
|
-
const push = (event) => {
|
|
1275
|
-
queue.push(event);
|
|
1276
|
-
wake?.();
|
|
1277
|
-
};
|
|
1278
|
-
const run = runRouterTurn({
|
|
1279
|
-
...options,
|
|
1280
|
-
showReasoning: options.showReasoning ?? true,
|
|
1281
|
-
onEvent: (event) => {
|
|
1282
|
-
if (event.type === "text") {
|
|
1283
|
-
if (event.segment !== textSegment || !textPart) {
|
|
1284
|
-
textSegment = event.segment;
|
|
1285
|
-
textPart = { type: "text", text: "" };
|
|
1286
|
-
parts.push(textPart);
|
|
1287
|
-
}
|
|
1288
|
-
textPart.text += event.delta;
|
|
1289
|
-
reasoningPart = null;
|
|
1290
|
-
reasoningClock.settle();
|
|
1291
|
-
push({ type: "text", text: event.delta });
|
|
1292
|
-
} else if (event.type === "reasoning") {
|
|
1293
|
-
if (!reasoningPart) {
|
|
1294
|
-
reasoningPart = { type: "reasoning", text: "" };
|
|
1295
|
-
parts.push(reasoningPart);
|
|
1296
|
-
}
|
|
1297
|
-
reasoningPart.text += event.delta;
|
|
1298
|
-
reasoningClock.reasoning(reasoningPart);
|
|
1299
|
-
push({ type: "reasoning", text: event.delta });
|
|
1300
|
-
} else if (event.type === "tool") {
|
|
1301
|
-
reasoningPart = null;
|
|
1302
|
-
reasoningClock.settle(event.startedAt);
|
|
1303
|
-
if (event.status === "running") {
|
|
1304
|
-
const part = {
|
|
1305
|
-
type: "tool",
|
|
1306
|
-
id: event.callId,
|
|
1307
|
-
callID: event.callId,
|
|
1308
|
-
tool: event.toolName,
|
|
1309
|
-
state: { status: "running", input: event.input, time: { start: event.startedAt } }
|
|
1310
|
-
};
|
|
1311
|
-
toolParts.set(event.callId, part);
|
|
1312
|
-
parts.push(part);
|
|
1313
|
-
push({ type: "tool_call", call: { toolCallId: event.callId, toolName: event.toolName, args: event.input } });
|
|
1314
|
-
} else {
|
|
1315
|
-
const part = toolParts.get(event.callId);
|
|
1316
|
-
const state = event.status === "completed" ? { status: "completed", output: event.output, time: { start: event.startedAt, end: event.endedAt } } : { status: "error", error: event.error.message, time: { start: event.startedAt, end: event.endedAt } };
|
|
1317
|
-
if (part) part.state = { ...part.state, ...state };
|
|
1318
|
-
push({
|
|
1319
|
-
type: "tool_result",
|
|
1320
|
-
toolCallId: event.callId,
|
|
1321
|
-
toolName: event.toolName,
|
|
1322
|
-
outcome: event.status === "completed" ? { ok: true, result: event.output } : { ok: false, message: event.error.message, code: event.error.code }
|
|
1323
|
-
});
|
|
1324
|
-
}
|
|
1325
|
-
} else if (event.type === "repeat") {
|
|
1326
|
-
reasoningPart = null;
|
|
1327
|
-
reasoningClock.settle();
|
|
1328
|
-
} else if (event.type === "usage") {
|
|
1329
|
-
usageReported = true;
|
|
1330
|
-
push({ type: "usage", usage: {
|
|
1331
|
-
promptTokens: event.promptTokens,
|
|
1332
|
-
completionTokens: event.completionTokens,
|
|
1333
|
-
...event.cachedPromptTokens !== void 0 ? { cachedPromptTokens: event.cachedPromptTokens } : {}
|
|
1334
|
-
} });
|
|
1335
|
-
} else if (event.type === "tool_event") {
|
|
1336
|
-
const part = toolParts.get(event.callId);
|
|
1337
|
-
if (part) {
|
|
1338
|
-
const state = part.state;
|
|
1339
|
-
part.state = { ...state, events: [...state.events ?? [], { kind: event.kind, data: event.data, at: event.at }] };
|
|
1340
|
-
}
|
|
1341
|
-
push({ type: "tool.event", data: { toolCallId: event.callId, toolName: event.toolName, kind: event.kind, data: event.data, at: event.at } });
|
|
1342
|
-
}
|
|
1343
|
-
if (options.onEvent) {
|
|
1344
|
-
try {
|
|
1345
|
-
options.onEvent(event);
|
|
1346
|
-
} catch {
|
|
1347
|
-
}
|
|
1348
|
-
}
|
|
1349
|
-
}
|
|
1350
|
-
}).then((result) => {
|
|
1351
|
-
outcome = result;
|
|
1352
|
-
options.onOutcome?.(result);
|
|
1353
|
-
if (result.failure) {
|
|
1354
|
-
push({ type: "error", data: { message: result.failure.message, code: result.failure.code } });
|
|
1355
|
-
}
|
|
1356
|
-
}).finally(() => {
|
|
1357
|
-
reasoningClock.settle();
|
|
1358
|
-
done = true;
|
|
1359
|
-
wake?.();
|
|
1360
|
-
});
|
|
1361
|
-
async function* stream() {
|
|
1362
|
-
for (; ; ) {
|
|
1363
|
-
while (queue.length) yield queue.shift();
|
|
1364
|
-
if (done) break;
|
|
1365
|
-
await new Promise((resolve) => {
|
|
1366
|
-
wake = resolve;
|
|
1367
|
-
});
|
|
1368
|
-
wake = null;
|
|
1369
|
-
}
|
|
1370
|
-
await run;
|
|
1371
|
-
}
|
|
1372
|
-
const snapshot = () => parts.map((part) => part.type === "reasoning" ? { ...part, time: reasoningClock.time(part) } : { ...part });
|
|
1373
|
-
const usage = () => !outcome || !usageReported ? {} : {
|
|
1374
|
-
// The Router's prompt tokens include the cached ones; the sandbox lanes report input and cache reads apart, so
|
|
1375
|
-
// the stored row counts them apart here too and `inputTokens + cacheReadTokens` is the prompt on either lane.
|
|
1376
|
-
inputTokens: outcome.usage.promptTokens - (outcome.usage.cachedPromptTokens ?? 0),
|
|
1377
|
-
outputTokens: outcome.usage.completionTokens,
|
|
1378
|
-
...outcome.usage.cachedPromptTokens !== void 0 ? { cacheReadTokens: outcome.usage.cachedPromptTokens } : {}
|
|
1379
|
-
};
|
|
1380
|
-
return {
|
|
1381
|
-
stream: stream(),
|
|
1382
|
-
finalText: () => outcome?.text ?? "",
|
|
1383
|
-
assistantParts: snapshot,
|
|
1384
|
-
draftParts: snapshot,
|
|
1385
|
-
usage,
|
|
1386
|
-
get model() {
|
|
1387
|
-
return outcome?.servedModel ?? options.model;
|
|
1388
|
-
},
|
|
1389
|
-
modelAttribution: () => ({
|
|
1390
|
-
requestedModel: options.model,
|
|
1391
|
-
...outcome?.servedModel ? { servedModel: outcome.servedModel } : {},
|
|
1392
|
-
echoReceived: Boolean(outcome?.servedModel)
|
|
1393
|
-
})
|
|
1394
|
-
};
|
|
1395
|
-
}
|
|
1396
|
-
|
|
1397
|
-
// src/chat-routes/hybrid-producer.ts
|
|
1398
|
-
var HYBRID_TURN_GUIDANCE = "Start your reply with text: before your first tool call, write one short sentence telling the member what you are about to do, so they never wait in silence. Use sandbox_run only for work that needs code execution or files on the workspace computer. Give the sandbox agent the member's requirements, paths and exact field names, not model-written code; the sandbox agent writes and runs the code. Report what it returns.";
|
|
1399
|
-
var DEFAULT_SANDBOX_RUN_TIMEOUT_MS = 4 * 6e4;
|
|
1400
|
-
function startHybridBox(body, acquire) {
|
|
1401
|
-
const attachments = (body.parts ?? []).map((part) => ({
|
|
1402
|
-
...part.mediaType ? { mediaType: part.mediaType } : {},
|
|
1403
|
-
...part.filename ? { name: part.filename } : {}
|
|
1404
|
-
}));
|
|
1405
|
-
if (escalationHints({ text: body.content ?? "", attachments }).length === 0) return void 0;
|
|
1406
|
-
const started = acquire();
|
|
1407
|
-
started.catch(() => void 0);
|
|
1408
|
-
return started;
|
|
1409
|
-
}
|
|
1410
|
-
function turnPromptText(prompt) {
|
|
1411
|
-
return typeof prompt === "string" ? prompt : prompt.map((part) => part.type === "text" ? part.text : "").join("\n");
|
|
1412
|
-
}
|
|
1413
|
-
function turnCarriesFiles(prompt) {
|
|
1414
|
-
return typeof prompt !== "string" && prompt.some((part) => part.type !== "text");
|
|
1415
|
-
}
|
|
1416
|
-
function routerTurnHistory(priorMessages, limit = 40) {
|
|
1417
|
-
return priorMessages.filter((message) => (message.role === "user" || message.role === "assistant") && Boolean(message.content?.trim())).slice(-limit).map(({ role, content }) => ({ role, content }));
|
|
1418
|
-
}
|
|
1419
|
-
function createHybridTurnProducer(options) {
|
|
1420
|
-
const { box, tools = [], speculateOn, attachments, onSpeculate, guidance, onSettled, signal, ...router } = options;
|
|
1421
|
-
const abort = new AbortController();
|
|
1422
|
-
const stop = signal ? AbortSignal.any([abort.signal, signal]) : abort.signal;
|
|
1423
|
-
const timeoutMs = box.timeoutMs ?? DEFAULT_SANDBOX_RUN_TIMEOUT_MS;
|
|
1424
|
-
let start = box.started?.catch((error) => {
|
|
1425
|
-
start = void 0;
|
|
1426
|
-
throw error;
|
|
1427
|
-
});
|
|
1428
|
-
start?.catch(() => void 0);
|
|
1429
|
-
const acquire = () => start ??= box.acquire(stop).catch((error) => {
|
|
1430
|
-
start = void 0;
|
|
1431
|
-
throw error;
|
|
1432
|
-
});
|
|
1433
|
-
const sandboxRun = createSandboxRunTool({
|
|
1434
|
-
box: () => acquire(),
|
|
1435
|
-
runTask: box.runTask,
|
|
1436
|
-
memberRequest: router.userMessage,
|
|
1437
|
-
timeoutMs,
|
|
1438
|
-
signal: stop,
|
|
1439
|
-
...box.warm ? { warm: box.warm } : {},
|
|
1440
|
-
...box.onFailure ? { onFailure: box.onFailure } : {},
|
|
1441
|
-
...box.name ? { name: box.name } : {},
|
|
1442
|
-
...box.description ? { description: box.description } : {}
|
|
1443
|
-
});
|
|
1444
|
-
const speculator = createEscalationSpeculator({
|
|
1445
|
-
warm: acquire,
|
|
1446
|
-
escalationTools: [sandboxRun.name],
|
|
1447
|
-
...onSpeculate ? { onSpeculate } : {}
|
|
1448
|
-
});
|
|
1449
|
-
speculator.onInput({ text: speculateOn ?? router.userMessage, ...attachments ? { attachments } : {} });
|
|
1450
|
-
const extra = guidance === false ? "" : guidance ?? HYBRID_TURN_GUIDANCE;
|
|
1451
|
-
const producer = createRouterTurnProducer({
|
|
1452
|
-
...router,
|
|
1453
|
-
systemPrompt: extra ? `${router.systemPrompt}
|
|
1454
|
-
|
|
1455
|
-
${extra}` : router.systemPrompt,
|
|
1456
|
-
tools: [...tools, sandboxRun],
|
|
1457
|
-
// A turn that calls the box must outlive the call it waits on.
|
|
1458
|
-
budgetMs: router.budgetMs ?? timeoutMs + 6e4,
|
|
1459
|
-
signal: stop,
|
|
1460
|
-
onEvent: (event) => {
|
|
1461
|
-
speculator.onEvent(event);
|
|
1462
|
-
router.onEvent?.(event);
|
|
1463
|
-
}
|
|
1464
|
-
});
|
|
1465
|
-
let settled = false;
|
|
1466
|
-
const settle = async () => {
|
|
1467
|
-
if (settled) return;
|
|
1468
|
-
settled = true;
|
|
1469
|
-
try {
|
|
1470
|
-
await onSettled?.();
|
|
1471
|
-
} catch (error) {
|
|
1472
|
-
console.error(JSON.stringify({
|
|
1473
|
-
event: "hybrid_turn_settle_failed",
|
|
1474
|
-
error: error instanceof Error ? error.message.slice(0, 300) : String(error).slice(0, 300)
|
|
1475
|
-
}));
|
|
1476
|
-
}
|
|
1477
|
-
};
|
|
1478
|
-
return {
|
|
1479
|
-
...producer,
|
|
1480
|
-
stream: (async function* () {
|
|
1481
|
-
let finished = false;
|
|
1482
|
-
try {
|
|
1483
|
-
for await (const event of producer.stream) yield event;
|
|
1484
|
-
finished = true;
|
|
1485
|
-
} finally {
|
|
1486
|
-
if (!finished) abort.abort(new Error("The chat route stopped reading this turn"));
|
|
1487
|
-
await settle();
|
|
1488
|
-
}
|
|
1489
|
-
})(),
|
|
1490
|
-
get model() {
|
|
1491
|
-
return producer.model;
|
|
1492
|
-
}
|
|
1493
|
-
};
|
|
1494
|
-
}
|
|
1495
|
-
|
|
1496
185
|
// src/chat-routes/protected-runtime-producer.ts
|
|
1497
186
|
import { profileMaterializationAxes } from "@tangle-network/agent-interface";
|
|
1498
187
|
var PROTECTED_TURN_AXES = /* @__PURE__ */ new Set([
|
|
@@ -1876,465 +565,6 @@ async function runDetachedTurn(opts) {
|
|
|
1876
565
|
return await settleRow({ state: "completed", text, parts, usage, cached: false });
|
|
1877
566
|
}
|
|
1878
567
|
|
|
1879
|
-
// src/chat-routes/completed-sandbox-turn.ts
|
|
1880
|
-
function finiteNumber(value) {
|
|
1881
|
-
if (typeof value === "number") return Number.isFinite(value) ? value : void 0;
|
|
1882
|
-
if (typeof value !== "string" || !value.trim()) return void 0;
|
|
1883
|
-
const parsed = Number(value);
|
|
1884
|
-
return Number.isFinite(parsed) ? parsed : void 0;
|
|
1885
|
-
}
|
|
1886
|
-
function firstNumber(record2, keys) {
|
|
1887
|
-
for (const key of keys) {
|
|
1888
|
-
const value = finiteNumber(record2?.[key]);
|
|
1889
|
-
if (value !== void 0) return value;
|
|
1890
|
-
}
|
|
1891
|
-
return void 0;
|
|
1892
|
-
}
|
|
1893
|
-
function usageFromResult(result) {
|
|
1894
|
-
const raw = asRecord(result?.usage) ?? asRecord(result?.tokenUsage);
|
|
1895
|
-
const inputTokens = firstNumber(raw, ["inputTokens", "promptTokens", "input"]);
|
|
1896
|
-
const outputTokens = firstNumber(raw, ["outputTokens", "completionTokens", "output"]);
|
|
1897
|
-
const reasoningTokens = firstNumber(raw, ["reasoningTokens", "reasoning"]);
|
|
1898
|
-
const cacheReadTokens = firstNumber(raw, [
|
|
1899
|
-
"cacheReadTokens",
|
|
1900
|
-
"cacheReadInputTokens",
|
|
1901
|
-
"cacheRead"
|
|
1902
|
-
]);
|
|
1903
|
-
const cacheWriteTokens = firstNumber(raw, [
|
|
1904
|
-
"cacheWriteTokens",
|
|
1905
|
-
"cacheCreationInputTokens",
|
|
1906
|
-
"cacheWrite"
|
|
1907
|
-
]);
|
|
1908
|
-
const costUsd = firstNumber(result, ["costUsd", "totalCostUsd", "cost"]) ?? firstNumber(raw, ["costUsd", "cost"]);
|
|
1909
|
-
return {
|
|
1910
|
-
...inputTokens !== void 0 ? { inputTokens } : {},
|
|
1911
|
-
...outputTokens !== void 0 ? { outputTokens } : {},
|
|
1912
|
-
...reasoningTokens !== void 0 ? { reasoningTokens } : {},
|
|
1913
|
-
...cacheReadTokens !== void 0 ? { cacheReadTokens } : {},
|
|
1914
|
-
...cacheWriteTokens !== void 0 ? { cacheWriteTokens } : {},
|
|
1915
|
-
...costUsd !== void 0 ? { costUsd } : {}
|
|
1916
|
-
};
|
|
1917
|
-
}
|
|
1918
|
-
function nonEmptyString(value) {
|
|
1919
|
-
return typeof value === "string" && value.trim() ? value : void 0;
|
|
1920
|
-
}
|
|
1921
|
-
function attributionFromResult(result) {
|
|
1922
|
-
const metadata = asRecord(result?.metadata);
|
|
1923
|
-
const modelAttribution = asRecord(result?.modelAttribution) ?? asRecord(metadata?.modelAttribution);
|
|
1924
|
-
const backend = asRecord(result?.effectiveBackend) ?? asRecord(metadata?.effectiveBackend);
|
|
1925
|
-
const source = nonEmptyString(result?.servedSource) ?? nonEmptyString(modelAttribution?.servedSource) ?? nonEmptyString(backend?.source);
|
|
1926
|
-
const servedSource = source === "request" || source === "environment" || source === "profile" ? source : void 0;
|
|
1927
|
-
const servedModel = nonEmptyString(result?.servedModel) ?? nonEmptyString(modelAttribution?.servedModel) ?? nonEmptyString(backend?.model);
|
|
1928
|
-
const servedProvider = nonEmptyString(result?.servedProvider) ?? nonEmptyString(modelAttribution?.servedProvider) ?? nonEmptyString(backend?.provider);
|
|
1929
|
-
return {
|
|
1930
|
-
...servedModel ? { servedModel } : {},
|
|
1931
|
-
...servedProvider ? { servedProvider } : {},
|
|
1932
|
-
...servedSource ? { servedSource } : {}
|
|
1933
|
-
};
|
|
1934
|
-
}
|
|
1935
|
-
function textFromResult(result) {
|
|
1936
|
-
return asString(result?.response) ?? asString(result?.finalText) ?? asString(result?.text) ?? asString(result?.output);
|
|
1937
|
-
}
|
|
1938
|
-
function completedMessagesForTurn(messages, turnId) {
|
|
1939
|
-
return messages.filter(
|
|
1940
|
-
(message) => message.role === "assistant" && message.metadata?.turnId === turnId && message.metadata?.interrupted !== true && (message.metadata?.completed === true || message.metadata?.status === "completed")
|
|
1941
|
-
);
|
|
1942
|
-
}
|
|
1943
|
-
function normalizedMessageParts(message) {
|
|
1944
|
-
const normalized = [];
|
|
1945
|
-
const usage = {};
|
|
1946
|
-
for (const raw of message.parts) {
|
|
1947
|
-
const record2 = asRecord(raw);
|
|
1948
|
-
if (!record2) continue;
|
|
1949
|
-
const part = normalizePersistedPart(record2);
|
|
1950
|
-
if (!part) continue;
|
|
1951
|
-
normalized.push(part);
|
|
1952
|
-
if (part.type === "step-finish") addStepFinishUsage(part, usage);
|
|
1953
|
-
}
|
|
1954
|
-
const collapsed = collapseRedundantTextParts(normalized);
|
|
1955
|
-
const derivedText = collapsed.filter((part) => part.type === "text").map((part) => String(part.text ?? "")).join("");
|
|
1956
|
-
return { parts: normalized, usage, derivedText };
|
|
1957
|
-
}
|
|
1958
|
-
function projectMessageParts(parts, finalText) {
|
|
1959
|
-
const order = [];
|
|
1960
|
-
const map = /* @__PURE__ */ new Map();
|
|
1961
|
-
for (let index = 0; index < parts.length; index += 1) {
|
|
1962
|
-
const part = parts[index];
|
|
1963
|
-
const type = String(part.type ?? "");
|
|
1964
|
-
const key = type === "step-start" || type === "step-finish" ? `${type}:#${index}` : getPartKey(part);
|
|
1965
|
-
if (!map.has(key)) order.push(key);
|
|
1966
|
-
map.set(key, mergePersistedPart(map.get(key), part));
|
|
1967
|
-
}
|
|
1968
|
-
return finalizeAssistantParts(order, map, finalText);
|
|
1969
|
-
}
|
|
1970
|
-
function recoverSandboxAssistantMessage(message) {
|
|
1971
|
-
const recovered = normalizedMessageParts(message);
|
|
1972
|
-
return {
|
|
1973
|
-
...recovered.derivedText ? { text: recovered.derivedText } : {},
|
|
1974
|
-
...hasUsage2(recovered.usage) ? { usage: recovered.usage } : {},
|
|
1975
|
-
parts: projectMessageParts(recovered.parts, recovered.derivedText)
|
|
1976
|
-
};
|
|
1977
|
-
}
|
|
1978
|
-
function hasUsage2(usage) {
|
|
1979
|
-
return Object.values(usage).some(
|
|
1980
|
-
(value) => typeof value === "number" && Number.isFinite(value)
|
|
1981
|
-
);
|
|
1982
|
-
}
|
|
1983
|
-
async function readCompletedSandboxTurn(box, options) {
|
|
1984
|
-
const { turnId, sessionId, log } = options;
|
|
1985
|
-
const session = box.session(sessionId);
|
|
1986
|
-
const [cacheOutcome, messagesOutcome] = await Promise.allSettled([
|
|
1987
|
-
box.findCompletedTurn(turnId, { sessionId }),
|
|
1988
|
-
session.messages({ limit: 1e3, ...options.since !== void 0 ? { since: options.since } : {} })
|
|
1989
|
-
]);
|
|
1990
|
-
if (cacheOutcome.status === "rejected") {
|
|
1991
|
-
log?.("[chat-routes] completed Sandbox turn cache lookup failed", {
|
|
1992
|
-
turnId,
|
|
1993
|
-
sessionId,
|
|
1994
|
-
error: String(cacheOutcome.reason)
|
|
1995
|
-
});
|
|
1996
|
-
}
|
|
1997
|
-
if (messagesOutcome.status === "rejected") {
|
|
1998
|
-
log?.("[chat-routes] completed Sandbox session message lookup failed", {
|
|
1999
|
-
turnId,
|
|
2000
|
-
sessionId,
|
|
2001
|
-
error: String(messagesOutcome.reason)
|
|
2002
|
-
});
|
|
2003
|
-
}
|
|
2004
|
-
const rawCached = cacheOutcome.status === "fulfilled" ? cacheOutcome.value : null;
|
|
2005
|
-
const cached = rawCached && rawCached.turnId === turnId && rawCached.sessionId === sessionId ? rawCached : null;
|
|
2006
|
-
if (rawCached && !cached) {
|
|
2007
|
-
log?.("[chat-routes] ignored mismatched completed Sandbox turn cache record", {
|
|
2008
|
-
turnId,
|
|
2009
|
-
sessionId
|
|
2010
|
-
});
|
|
2011
|
-
}
|
|
2012
|
-
const messages = messagesOutcome.status === "fulfilled" ? messagesOutcome.value : [];
|
|
2013
|
-
const matchingMessages = completedMessagesForTurn(messages, turnId);
|
|
2014
|
-
const matchingCount = matchingMessages.length;
|
|
2015
|
-
const message = matchingCount === 1 ? matchingMessages[0] : null;
|
|
2016
|
-
if (matchingCount > 1) {
|
|
2017
|
-
log?.("[chat-routes] ignored ambiguous completed Sandbox session messages", {
|
|
2018
|
-
turnId,
|
|
2019
|
-
sessionId,
|
|
2020
|
-
matchingCount
|
|
2021
|
-
});
|
|
2022
|
-
}
|
|
2023
|
-
if (!cached && !message) {
|
|
2024
|
-
if (cacheOutcome.status === "rejected" || messagesOutcome.status === "rejected") {
|
|
2025
|
-
throw new Error("Completed Sandbox turn could not be verified; retry the exact read");
|
|
2026
|
-
}
|
|
2027
|
-
if (rawCached || matchingCount > 1) {
|
|
2028
|
-
throw new Error("Completed Sandbox turn identity is inconsistent; reconciliation is required");
|
|
2029
|
-
}
|
|
2030
|
-
return null;
|
|
2031
|
-
}
|
|
2032
|
-
const result = cached ? asRecord(cached.result) : void 0;
|
|
2033
|
-
const recovered = message ? normalizedMessageParts(message) : null;
|
|
2034
|
-
const text = textFromResult(result) ?? recovered?.derivedText;
|
|
2035
|
-
const resultUsage = usageFromResult(result);
|
|
2036
|
-
const usage = { ...recovered?.usage ?? {}, ...resultUsage };
|
|
2037
|
-
const resultParts = Array.isArray(result?.parts) ? result.parts.map((part) => asRecord(part)).filter((part) => Boolean(part)).map((part) => normalizePersistedPart(part)).filter((part) => Boolean(part)) : null;
|
|
2038
|
-
const sourceParts = recovered?.parts ?? resultParts;
|
|
2039
|
-
const parts = sourceParts ? projectMessageParts(sourceParts, text ?? recovered?.derivedText ?? "") : void 0;
|
|
2040
|
-
return {
|
|
2041
|
-
...text !== void 0 ? { text } : {},
|
|
2042
|
-
...hasUsage2(usage) ? { usage } : {},
|
|
2043
|
-
...parts ? { parts } : {},
|
|
2044
|
-
...attributionFromResult(result)
|
|
2045
|
-
};
|
|
2046
|
-
}
|
|
2047
|
-
|
|
2048
|
-
// src/chat-routes/native-completion.ts
|
|
2049
|
-
var DEFAULT_ABSENT_DISPATCH_DEADLINE_MS = 10 * 6e4;
|
|
2050
|
-
var DEFAULT_RECEIPT_DEADLINE_MS = 10 * 6e4;
|
|
2051
|
-
var MESSAGE_READ_SKEW_MS = 10 * 6e4;
|
|
2052
|
-
var INTERRUPTED_RESULT_REPLAY_MAX_EVENTS = 2e3;
|
|
2053
|
-
var RUNTIME_RESTART_TERMINAL_EVENT_ID = "interrupted-done";
|
|
2054
|
-
var RUNTIME_RESTARTED_ERROR = "Execution interrupted: the agent runtime restarted before the run produced a terminal event. The run cannot be resumed; retry it.";
|
|
2055
|
-
function replayableExecution(run) {
|
|
2056
|
-
return typeof run.eventCount === "number" && run.eventCount <= INTERRUPTED_RESULT_REPLAY_MAX_EVENTS;
|
|
2057
|
-
}
|
|
2058
|
-
var NATIVE_COMPLETION_RECEIPT_MAX_BYTES = 900 * 1024;
|
|
2059
|
-
var RECEIPT_STRING_CAPS = [16384, 4096, 1024, 256];
|
|
2060
|
-
var encoder = new TextEncoder();
|
|
2061
|
-
function serializedBytes(value) {
|
|
2062
|
-
return encoder.encode(JSON.stringify(value)).length;
|
|
2063
|
-
}
|
|
2064
|
-
function capString(value, cap) {
|
|
2065
|
-
if (value.length <= cap) return value;
|
|
2066
|
-
return `${value.slice(0, cap)}\u2026[truncated ${value.length - cap} characters; the Sandbox session keeps the full value]`;
|
|
2067
|
-
}
|
|
2068
|
-
function capStrings(value, cap) {
|
|
2069
|
-
if (typeof value === "string") return capString(value, cap);
|
|
2070
|
-
if (Array.isArray(value)) return value.map((item) => capStrings(item, cap));
|
|
2071
|
-
const object = record(value);
|
|
2072
|
-
if (!object) return value;
|
|
2073
|
-
return Object.fromEntries(Object.entries(object).map(([key, item]) => [key, capStrings(item, cap)]));
|
|
2074
|
-
}
|
|
2075
|
-
function capReceiptPart(part, cap) {
|
|
2076
|
-
const type = String(part.type ?? "");
|
|
2077
|
-
if (type === "tool") {
|
|
2078
|
-
const state = record(part.state);
|
|
2079
|
-
if (!state) return part;
|
|
2080
|
-
const capped = capStrings(state, cap);
|
|
2081
|
-
if (serializedBytes(capped) === serializedBytes(state)) return part;
|
|
2082
|
-
return { ...part, state: { ...capped, metadata: { ...record(capped.metadata), receiptTruncated: true } } };
|
|
2083
|
-
}
|
|
2084
|
-
if (type === "reasoning" && typeof part.text === "string" && part.text.length > cap) {
|
|
2085
|
-
return { ...part, text: capString(part.text, cap), receiptTruncated: true };
|
|
2086
|
-
}
|
|
2087
|
-
return part;
|
|
2088
|
-
}
|
|
2089
|
-
function skeletonPart(part) {
|
|
2090
|
-
const type = String(part.type ?? "");
|
|
2091
|
-
if (type === "reasoning") return null;
|
|
2092
|
-
if (type !== "tool") return part;
|
|
2093
|
-
const state = record(part.state);
|
|
2094
|
-
return {
|
|
2095
|
-
type: "tool",
|
|
2096
|
-
...part.id !== void 0 ? { id: part.id } : {},
|
|
2097
|
-
...part.tool !== void 0 ? { tool: part.tool } : {},
|
|
2098
|
-
state: { status: state?.status ?? "completed", metadata: { receiptTruncated: true } }
|
|
2099
|
-
};
|
|
2100
|
-
}
|
|
2101
|
-
function boundNativeCompletionReceipt(receipt, maxBytes = NATIVE_COMPLETION_RECEIPT_MAX_BYTES) {
|
|
2102
|
-
if (serializedBytes(receipt) <= maxBytes) return receipt;
|
|
2103
|
-
for (const cap of RECEIPT_STRING_CAPS) {
|
|
2104
|
-
const bounded = { ...receipt, parts: receipt.parts.map((part) => capReceiptPart(part, cap)) };
|
|
2105
|
-
if (serializedBytes(bounded) <= maxBytes) return bounded;
|
|
2106
|
-
}
|
|
2107
|
-
const skeleton = {
|
|
2108
|
-
...receipt,
|
|
2109
|
-
parts: receipt.parts.map(skeletonPart).filter((part) => part !== null)
|
|
2110
|
-
};
|
|
2111
|
-
if (serializedBytes(skeleton) <= maxBytes) return skeleton;
|
|
2112
|
-
const textBudget = Math.max(0, Math.floor(maxBytes / 4));
|
|
2113
|
-
return {
|
|
2114
|
-
...skeleton,
|
|
2115
|
-
text: capString(skeleton.text, textBudget),
|
|
2116
|
-
parts: skeleton.parts.map((part) => part.type === "text" && typeof part.text === "string" ? { ...part, text: capString(part.text, textBudget) } : part)
|
|
2117
|
-
};
|
|
2118
|
-
}
|
|
2119
|
-
function terminal(receipt) {
|
|
2120
|
-
const bounded = boundNativeCompletionReceipt(receipt);
|
|
2121
|
-
return bounded.state === "completed" ? { state: "completed", receipt: bounded } : { state: "failed", receipt: bounded };
|
|
2122
|
-
}
|
|
2123
|
-
function timestamp(value) {
|
|
2124
|
-
if (value instanceof Date) return value.getTime();
|
|
2125
|
-
return typeof value === "number" && Number.isFinite(value) ? value : void 0;
|
|
2126
|
-
}
|
|
2127
|
-
function record(value) {
|
|
2128
|
-
return value !== null && typeof value === "object" && !Array.isArray(value) ? value : void 0;
|
|
2129
|
-
}
|
|
2130
|
-
function isSandboxNotFoundError(value) {
|
|
2131
|
-
const error = record(value);
|
|
2132
|
-
return error?.name === "NotFoundError" || error?.code === "NOT_FOUND" || error?.status === 404;
|
|
2133
|
-
}
|
|
2134
|
-
function isCanonicalUserStop(error) {
|
|
2135
|
-
return error === "Execution cancelled by user" || error === "Execution cancelled by the user";
|
|
2136
|
-
}
|
|
2137
|
-
function terminalFailureMessage(error, provenUserStop) {
|
|
2138
|
-
if (provenUserStop) return "Execution cancelled by the user";
|
|
2139
|
-
if (/\bENOSPC\b|no space left on device/i.test(error)) {
|
|
2140
|
-
return "The Sandbox ran out of disk space before the execution finished.";
|
|
2141
|
-
}
|
|
2142
|
-
if (error === RUNTIME_RESTARTED_ERROR) return RUNTIME_RESTARTED_ERROR;
|
|
2143
|
-
if (/\bcancelled\b|\bcanceled\b/i.test(error)) return "Native Sandbox execution was cancelled.";
|
|
2144
|
-
if (/MCP.*(?:did not become ready|bootstrap)|(?:MCP|runtime) bootstrap failed/i.test(error)) {
|
|
2145
|
-
return "The Sandbox could not start the agent runtime.";
|
|
2146
|
-
}
|
|
2147
|
-
return "Native Sandbox execution failed before producing an answer.";
|
|
2148
|
-
}
|
|
2149
|
-
function exactFailedResult(value, turnId, sessionId) {
|
|
2150
|
-
const result = record(value);
|
|
2151
|
-
if (result?.executionId !== turnId || result.sessionId !== void 0 && result.sessionId !== sessionId) return void 0;
|
|
2152
|
-
if (result.success !== false || !["error", "failed", "cancelled"].includes(String(result.status))) return void 0;
|
|
2153
|
-
return typeof result.error === "string" && result.error.trim() ? result : void 0;
|
|
2154
|
-
}
|
|
2155
|
-
function interruptedMessages(messages, turnId) {
|
|
2156
|
-
return messages.filter(
|
|
2157
|
-
(message) => message.role === "assistant" && message.metadata?.turnId === turnId && (message.metadata.interrupted === true || message.metadata.status === "interrupted")
|
|
2158
|
-
);
|
|
2159
|
-
}
|
|
2160
|
-
function turnAssistantMessages(messages, turnId) {
|
|
2161
|
-
return messages.filter((message) => message.role === "assistant" && message.metadata?.turnId === turnId);
|
|
2162
|
-
}
|
|
2163
|
-
function consistent(values) {
|
|
2164
|
-
if (values.length === 0 || values.some((value) => value === void 0)) return void 0;
|
|
2165
|
-
const first = values[0];
|
|
2166
|
-
return values.every((value) => value === first) ? first : void 0;
|
|
2167
|
-
}
|
|
2168
|
-
function sum(values) {
|
|
2169
|
-
if (values.some((value) => value === void 0)) return void 0;
|
|
2170
|
-
return values.reduce((total, value) => total + value, 0);
|
|
2171
|
-
}
|
|
2172
|
-
function aggregateNativeCompletionReceipts(turns) {
|
|
2173
|
-
const failed = turns.some((turn) => turn.state === "failed");
|
|
2174
|
-
const usage = {};
|
|
2175
|
-
for (const key of ["inputTokens", "outputTokens", "reasoningTokens", "cacheReadTokens", "cacheWriteTokens", "costUsd"]) {
|
|
2176
|
-
const total = sum(turns.map((turn) => turn.usage[key]));
|
|
2177
|
-
if (total !== void 0) usage[key] = total;
|
|
2178
|
-
}
|
|
2179
|
-
const error = turns.find(
|
|
2180
|
-
(turn) => turn.error?.startsWith("Admitted native execution did not produce") || turn.error?.startsWith("Native Sandbox execution ledger is missing")
|
|
2181
|
-
)?.error ?? turns.find((turn) => turn.error)?.error;
|
|
2182
|
-
return {
|
|
2183
|
-
state: failed ? "failed" : "completed",
|
|
2184
|
-
text: turns.map((turn) => turn.text).join(""),
|
|
2185
|
-
parts: turns.flatMap((turn) => turn.parts),
|
|
2186
|
-
usage,
|
|
2187
|
-
...consistent(turns.map((turn) => turn.servedModel)) ? { servedModel: consistent(turns.map((turn) => turn.servedModel)) } : {},
|
|
2188
|
-
...consistent(turns.map((turn) => turn.servedProvider)) ? { servedProvider: consistent(turns.map((turn) => turn.servedProvider)) } : {},
|
|
2189
|
-
...consistent(turns.map((turn) => turn.servedSource)) ? { servedSource: consistent(turns.map((turn) => turn.servedSource)) } : {},
|
|
2190
|
-
...error ? { error } : {},
|
|
2191
|
-
completedTurnIds: turns.map((turn) => turn.turnId)
|
|
2192
|
-
};
|
|
2193
|
-
}
|
|
2194
|
-
var TOOL_ABORTED_ERROR = "The Sandbox aborted a tool call before the turn finished, as when its runtime restarts mid-turn";
|
|
2195
|
-
function abortedToolCall(parts) {
|
|
2196
|
-
for (const part of parts) {
|
|
2197
|
-
if (part.type !== "tool") continue;
|
|
2198
|
-
const state = part.state;
|
|
2199
|
-
if (state?.status === "error" && /tool execution aborted/i.test(String(state.error ?? ""))) {
|
|
2200
|
-
return typeof part.tool === "string" ? part.tool : "tool";
|
|
2201
|
-
}
|
|
2202
|
-
}
|
|
2203
|
-
return void 0;
|
|
2204
|
-
}
|
|
2205
|
-
function admittedWorkIsLive(status, runsByTurnId, admittedTurnIds, dispatchMayBePending) {
|
|
2206
|
-
if (status.status !== "queued" && status.status !== "running") return false;
|
|
2207
|
-
if (status.raw?.state === "stale") return false;
|
|
2208
|
-
return admittedTurnIds.some((turnId) => {
|
|
2209
|
-
const run = runsByTurnId.get(turnId);
|
|
2210
|
-
return run ? run.status === "active" : dispatchMayBePending;
|
|
2211
|
-
});
|
|
2212
|
-
}
|
|
2213
|
-
function missingTurnReceipt(turnId, error) {
|
|
2214
|
-
return { turnId, state: "failed", text: "", parts: [], usage: {}, error };
|
|
2215
|
-
}
|
|
2216
|
-
async function observeNativeCompletion(options) {
|
|
2217
|
-
const now = options.now ?? Date.now();
|
|
2218
|
-
let admission = await options.admissionStore.read(options.executionId);
|
|
2219
|
-
if (!admission) throw new Error("Native completion admission is missing");
|
|
2220
|
-
if (admission.executionId !== options.executionId) throw new Error("Native completion admission identity conflict");
|
|
2221
|
-
const leaseUntil = timestamp(admission.ownerLeaseUntil) ?? 0;
|
|
2222
|
-
const source = options.source;
|
|
2223
|
-
const session = source?.session(options.sessionId);
|
|
2224
|
-
let status;
|
|
2225
|
-
if (!session) {
|
|
2226
|
-
status = null;
|
|
2227
|
-
} else {
|
|
2228
|
-
try {
|
|
2229
|
-
status = await session.status();
|
|
2230
|
-
} catch (error) {
|
|
2231
|
-
if (!isSandboxNotFoundError(error)) throw error;
|
|
2232
|
-
status = null;
|
|
2233
|
-
}
|
|
2234
|
-
}
|
|
2235
|
-
if (!status || !session || !source) {
|
|
2236
|
-
if (admission.state === "open" && leaseUntil <= now) {
|
|
2237
|
-
admission = await options.admissionStore.closeExpired(options.executionId, new Date(now)) ?? admission;
|
|
2238
|
-
}
|
|
2239
|
-
if (admission.state === "open" || now - options.registeredAt < (options.absentDispatchDeadlineMs ?? DEFAULT_ABSENT_DISPATCH_DEADLINE_MS)) {
|
|
2240
|
-
return { state: "running" };
|
|
2241
|
-
}
|
|
2242
|
-
return terminal(aggregateNativeCompletionReceipts([
|
|
2243
|
-
missingTurnReceipt(options.turnId, "Native Sandbox dispatch was not observed before the admission deadline")
|
|
2244
|
-
]));
|
|
2245
|
-
}
|
|
2246
|
-
const exactSession = session;
|
|
2247
|
-
const dispatchMayBePending = now - options.registeredAt < (options.absentDispatchDeadlineMs ?? DEFAULT_ABSENT_DISPATCH_DEADLINE_MS);
|
|
2248
|
-
let runsByTurnId;
|
|
2249
|
-
const readRuns = async () => runsByTurnId ??= new Map((await exactSession.runs()).map((run) => [run.executionId, run]));
|
|
2250
|
-
if (admission.state === "open") {
|
|
2251
|
-
if (admittedWorkIsLive(status, await readRuns(), admission.admittedTurnIds, dispatchMayBePending)) {
|
|
2252
|
-
admission = await options.admissionStore.renew(options.executionId, new Date(now)) ?? admission;
|
|
2253
|
-
} else if (leaseUntil <= now) {
|
|
2254
|
-
admission = await options.admissionStore.closeExpired(options.executionId, new Date(now)) ?? admission;
|
|
2255
|
-
}
|
|
2256
|
-
if (admission.state === "open") return { state: "running" };
|
|
2257
|
-
}
|
|
2258
|
-
const admittedTurnIds = [...admission.admittedTurnIds];
|
|
2259
|
-
if (admittedTurnIds.length === 0 || admittedTurnIds[0] !== options.turnId) {
|
|
2260
|
-
throw new Error("Native completion admission has an invalid turn sequence");
|
|
2261
|
-
}
|
|
2262
|
-
const ledger = await readRuns();
|
|
2263
|
-
const missingExecution = admittedTurnIds.find((turnId) => !ledger.has(turnId));
|
|
2264
|
-
if (missingExecution && dispatchMayBePending) return { state: "running" };
|
|
2265
|
-
if (admittedTurnIds.some((turnId) => ledger.get(turnId)?.status === "active")) return { state: "running" };
|
|
2266
|
-
const since = Math.max(0, options.registeredAt - MESSAGE_READ_SKEW_MS);
|
|
2267
|
-
let messages;
|
|
2268
|
-
const recovered = [];
|
|
2269
|
-
for (const turnId of admittedTurnIds) {
|
|
2270
|
-
const run = ledger.get(turnId);
|
|
2271
|
-
if (!run) {
|
|
2272
|
-
recovered.push(missingTurnReceipt(turnId, "Native Sandbox execution ledger is missing the admitted turn"));
|
|
2273
|
-
continue;
|
|
2274
|
-
}
|
|
2275
|
-
const completed = run.status === "completed" ? await readCompletedSandboxTurn(source, { turnId, sessionId: options.sessionId, since }) : null;
|
|
2276
|
-
if (run.status === "completed" && completed) {
|
|
2277
|
-
const aborted = abortedToolCall(completed.parts ?? []);
|
|
2278
|
-
recovered.push({
|
|
2279
|
-
...completed,
|
|
2280
|
-
turnId,
|
|
2281
|
-
// A run the runtime ended while a tool was still running reports
|
|
2282
|
-
// completed, but its reply stops mid-work. It is a failed turn.
|
|
2283
|
-
state: aborted ? "failed" : "completed",
|
|
2284
|
-
text: completed.text ?? "",
|
|
2285
|
-
parts: completed.parts ?? [],
|
|
2286
|
-
usage: completed.usage ?? {},
|
|
2287
|
-
...aborted ? { error: `${TOOL_ABORTED_ERROR} (${aborted})` } : {}
|
|
2288
|
-
});
|
|
2289
|
-
continue;
|
|
2290
|
-
}
|
|
2291
|
-
const restarted = run.status === "failed" && run.lastEventId === RUNTIME_RESTART_TERMINAL_EVENT_ID;
|
|
2292
|
-
let interrupted = run.status === "failed" || run.status === "cancelled" ? interruptedMessages(messages ??= await exactSession.messages({ limit: 1e3, since }), turnId) : [];
|
|
2293
|
-
if (restarted && interrupted.length === 0) interrupted = turnAssistantMessages(messages ?? [], turnId);
|
|
2294
|
-
if (restarted && interrupted.length !== 1) {
|
|
2295
|
-
recovered.push({ turnId, state: "failed", text: "", parts: [], usage: {}, error: RUNTIME_RESTARTED_ERROR });
|
|
2296
|
-
continue;
|
|
2297
|
-
}
|
|
2298
|
-
if ((run.status === "failed" || run.status === "cancelled") && interrupted.length === 0 && turnAssistantMessages(messages ?? [], turnId).length === 0 && run.sessionId === options.sessionId && replayableExecution(run)) {
|
|
2299
|
-
const result = exactFailedResult(await exactSession.result({ executionId: turnId }), turnId, options.sessionId);
|
|
2300
|
-
if (result) {
|
|
2301
|
-
recovered.push({
|
|
2302
|
-
turnId,
|
|
2303
|
-
state: "failed",
|
|
2304
|
-
text: "",
|
|
2305
|
-
parts: [],
|
|
2306
|
-
usage: usageFromResult(result),
|
|
2307
|
-
...attributionFromResult(result),
|
|
2308
|
-
error: terminalFailureMessage(result.error, run.status === "cancelled" && isCanonicalUserStop(result.error))
|
|
2309
|
-
});
|
|
2310
|
-
continue;
|
|
2311
|
-
}
|
|
2312
|
-
}
|
|
2313
|
-
if ((run.status === "failed" || run.status === "cancelled") && interrupted.length === 1) {
|
|
2314
|
-
const message = recoverSandboxAssistantMessage(interrupted[0]);
|
|
2315
|
-
const result = replayableExecution(run) ? await exactSession.result({ executionId: turnId }) : void 0;
|
|
2316
|
-
const provenUserStop = run.status === "cancelled" && (isCanonicalUserStop(interrupted[0].metadata?.interruptReason) || isCanonicalUserStop(exactFailedResult(result, turnId, options.sessionId)?.error));
|
|
2317
|
-
recovered.push({
|
|
2318
|
-
turnId,
|
|
2319
|
-
state: "failed",
|
|
2320
|
-
text: message.text ?? (typeof result?.response === "string" ? result.response : void 0) ?? "",
|
|
2321
|
-
parts: message.parts ?? [],
|
|
2322
|
-
usage: { ...message.usage, ...usageFromResult(result) },
|
|
2323
|
-
...attributionFromResult(result),
|
|
2324
|
-
error: terminalFailureMessage((typeof result?.error === "string" ? result.error : void 0) ?? interrupted[0].metadata?.interruptReason ?? (restarted ? RUNTIME_RESTARTED_ERROR : void 0) ?? status.failureReason?.message ?? "Native Sandbox execution failed.", provenUserStop)
|
|
2325
|
-
});
|
|
2326
|
-
continue;
|
|
2327
|
-
}
|
|
2328
|
-
const closedAt = timestamp(admission.closedAt) ?? timestamp(admission.updatedAt) ?? options.registeredAt;
|
|
2329
|
-
if (now - closedAt < (options.receiptDeadlineMs ?? DEFAULT_RECEIPT_DEADLINE_MS)) return { state: "running" };
|
|
2330
|
-
recovered.push(missingTurnReceipt(
|
|
2331
|
-
turnId,
|
|
2332
|
-
`Admitted native execution did not produce an exact completion: ${turnId}`
|
|
2333
|
-
));
|
|
2334
|
-
}
|
|
2335
|
-
return terminal(aggregateNativeCompletionReceipts(recovered));
|
|
2336
|
-
}
|
|
2337
|
-
|
|
2338
568
|
// src/chat-routes/plan-follow-up.ts
|
|
2339
569
|
import { createHash, randomUUID } from "crypto";
|
|
2340
570
|
var PLAN_FOLLOW_UP_STALE_AFTER_MS = 60 * 60 * 1e3;
|
|
@@ -2345,11 +575,11 @@ function planFollowUpExecutionId(sessionId, turnId) {
|
|
|
2345
575
|
}
|
|
2346
576
|
function parsePlanFollowUpAttach(value) {
|
|
2347
577
|
if (!value || typeof value !== "object" || Array.isArray(value)) return null;
|
|
2348
|
-
const
|
|
2349
|
-
const planId = typeof
|
|
2350
|
-
const turnId = typeof
|
|
2351
|
-
const revision =
|
|
2352
|
-
const outcome =
|
|
578
|
+
const record = value;
|
|
579
|
+
const planId = typeof record.planId === "string" ? record.planId.trim() : "";
|
|
580
|
+
const turnId = typeof record.turnId === "string" ? record.turnId.trim() : "";
|
|
581
|
+
const revision = record.revision;
|
|
582
|
+
const outcome = record.outcome;
|
|
2353
583
|
if (!planId || !turnId || outcome !== "approved" && outcome !== "rejected") return null;
|
|
2354
584
|
if (typeof revision !== "number" || !Number.isInteger(revision) || revision < 1) return null;
|
|
2355
585
|
if (turnId !== planFollowUpTurnId(planId, revision, outcome)) return null;
|
|
@@ -2709,7 +939,7 @@ async function resolveChatAttachments(value, options) {
|
|
|
2709
939
|
const parsed = parseChatAttachmentInputs(value, options);
|
|
2710
940
|
if (!parsed.succeeded) return parsed;
|
|
2711
941
|
const inputs = parsed.value;
|
|
2712
|
-
const advisoryTotal = inputs.reduce((
|
|
942
|
+
const advisoryTotal = inputs.reduce((sum, input) => sum + input.size, 0);
|
|
2713
943
|
if (advisoryTotal > maxTotalBytes) {
|
|
2714
944
|
return { succeeded: false, error: attachmentTotalSizeErrorMessage(advisoryTotal, maxTotalBytes) };
|
|
2715
945
|
}
|
|
@@ -3281,7 +1511,7 @@ function createAttachmentUploadRoute(options) {
|
|
|
3281
1511
|
`Too many files \u2014 the ${maxCount}-file limit was exceeded`
|
|
3282
1512
|
);
|
|
3283
1513
|
}
|
|
3284
|
-
const advisoryTotal = files.reduce((
|
|
1514
|
+
const advisoryTotal = files.reduce((sum, file) => sum + file.size, 0);
|
|
3285
1515
|
if (advisoryTotal > maxTotalBytes) {
|
|
3286
1516
|
return attachmentUploadError(
|
|
3287
1517
|
413,
|
|
@@ -3921,28 +2151,8 @@ function observeChatTurnStream(source, options) {
|
|
|
3921
2151
|
async function produceChatTurnWithIntelligence(options) {
|
|
3922
2152
|
const snapshot = { ...options };
|
|
3923
2153
|
const producer = await options.produce();
|
|
3924
|
-
|
|
3925
|
-
|
|
3926
|
-
finalText: () => producer.finalText(),
|
|
3927
|
-
get assistantParts() {
|
|
3928
|
-
return producer.assistantParts?.bind(producer);
|
|
3929
|
-
},
|
|
3930
|
-
get draftParts() {
|
|
3931
|
-
return producer.draftParts?.bind(producer);
|
|
3932
|
-
},
|
|
3933
|
-
get usage() {
|
|
3934
|
-
return producer.usage?.bind(producer);
|
|
3935
|
-
},
|
|
3936
|
-
get model() {
|
|
3937
|
-
return producer.model;
|
|
3938
|
-
},
|
|
3939
|
-
get modelFailover() {
|
|
3940
|
-
return producer.modelFailover?.bind(producer);
|
|
3941
|
-
},
|
|
3942
|
-
get modelAttribution() {
|
|
3943
|
-
return producer.modelAttribution?.bind(producer);
|
|
3944
|
-
}
|
|
3945
|
-
};
|
|
2154
|
+
const stream = observeChatTurnStream(producer.stream, snapshot);
|
|
2155
|
+
return withProducerStream(producer, stream);
|
|
3946
2156
|
}
|
|
3947
2157
|
|
|
3948
2158
|
// src/chat-routes/stored-attachments.ts
|
|
@@ -4058,6 +2268,7 @@ export {
|
|
|
4058
2268
|
buildMentionPromptBlock,
|
|
4059
2269
|
buildTurnPhaseEvent,
|
|
4060
2270
|
bytesToBase64,
|
|
2271
|
+
cancelChatTurn,
|
|
4061
2272
|
chatTurnRequestInit,
|
|
4062
2273
|
checkAttachmentType,
|
|
4063
2274
|
classifyTerminalFailure,
|
|
@@ -4088,6 +2299,7 @@ export {
|
|
|
4088
2299
|
normalizeChatPromptForSandbox,
|
|
4089
2300
|
observeChatTurnStream,
|
|
4090
2301
|
observeNativeCompletion,
|
|
2302
|
+
observeTurnCancellation,
|
|
4091
2303
|
parseChatAttachmentInputs,
|
|
4092
2304
|
parseChatTurnParts,
|
|
4093
2305
|
parseFileMentions,
|