@tangle-network/agent-app 0.62.4 → 0.63.1
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
|
@@ -0,0 +1,3365 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createInteractionAnswerRoute
|
|
3
|
+
} from "./chunk-KH4QSBNI.js";
|
|
4
|
+
import {
|
|
5
|
+
parseJsonObjectBody
|
|
6
|
+
} from "./chunk-WP2GYFZ4.js";
|
|
7
|
+
import {
|
|
8
|
+
noteMeasuredTurnFacts,
|
|
9
|
+
readableTurnFailure
|
|
10
|
+
} from "./chunk-4DBHC63F.js";
|
|
11
|
+
import {
|
|
12
|
+
redactErrorMessage
|
|
13
|
+
} from "./chunk-MH74DY2I.js";
|
|
14
|
+
import {
|
|
15
|
+
currentD1QueryCounter,
|
|
16
|
+
withD1QueryCount
|
|
17
|
+
} from "./chunk-MM6RLOXF.js";
|
|
18
|
+
import {
|
|
19
|
+
createEscalationSpeculator,
|
|
20
|
+
createSandboxRunTool,
|
|
21
|
+
escalationHints
|
|
22
|
+
} from "./chunk-ND5XWLRU.js";
|
|
23
|
+
import {
|
|
24
|
+
addStepFinishUsage,
|
|
25
|
+
assistantRowIdForTurn,
|
|
26
|
+
createAssistantDraftWriter,
|
|
27
|
+
rowIdOf,
|
|
28
|
+
storeSupportsDraftPersistence
|
|
29
|
+
} from "./chunk-N6URQ6DM.js";
|
|
30
|
+
import {
|
|
31
|
+
normalizeClientTurnId,
|
|
32
|
+
resolveChatTurn
|
|
33
|
+
} from "./chunk-YG66IHDU.js";
|
|
34
|
+
import {
|
|
35
|
+
createDurableTurnLock
|
|
36
|
+
} from "./chunk-A2NQMVMF.js";
|
|
37
|
+
import {
|
|
38
|
+
coalesceDeltas,
|
|
39
|
+
createBufferedTurnTap,
|
|
40
|
+
createD1TurnEventStore,
|
|
41
|
+
replayTurnEvents,
|
|
42
|
+
stampReplaySeq
|
|
43
|
+
} from "./chunk-ISP6ZH3G.js";
|
|
44
|
+
import {
|
|
45
|
+
mentionInputToPart,
|
|
46
|
+
toChatMessageParts
|
|
47
|
+
} from "./chunk-RZLCBWGG.js";
|
|
48
|
+
import {
|
|
49
|
+
ChatTurnInputError,
|
|
50
|
+
INLINE_PARTS_MAX_BYTES,
|
|
51
|
+
TURN_INTENT_EVENT,
|
|
52
|
+
TURN_PROGRESS_FIRST,
|
|
53
|
+
TURN_PROGRESS_HEADER,
|
|
54
|
+
TURN_ROUTE_PHASE_MESSAGES,
|
|
55
|
+
assertPromptPartsWithinCap,
|
|
56
|
+
buildTurnPhaseEvent,
|
|
57
|
+
chatTurnRequestInit,
|
|
58
|
+
parseChatTurnParts,
|
|
59
|
+
parseFileMentions,
|
|
60
|
+
turnResponseEventFor
|
|
61
|
+
} from "./chunk-5TSRFF5W.js";
|
|
62
|
+
import {
|
|
63
|
+
asRecord,
|
|
64
|
+
asString,
|
|
65
|
+
draftAssistantParts,
|
|
66
|
+
finalizeAssistantParts,
|
|
67
|
+
finalizePendingInteractionParts,
|
|
68
|
+
getPartKey,
|
|
69
|
+
mergePersistedPart,
|
|
70
|
+
normalizePersistedPart,
|
|
71
|
+
normalizeToolEvent,
|
|
72
|
+
terminalizeDanglingAssistantToolUpdates
|
|
73
|
+
} from "./chunk-VUBJZEZB.js";
|
|
74
|
+
import {
|
|
75
|
+
cancelStatusFor,
|
|
76
|
+
interactionPartKey,
|
|
77
|
+
interactionToPersistedPart,
|
|
78
|
+
isRenderableInteractionKind,
|
|
79
|
+
noticePart,
|
|
80
|
+
noticePartKey,
|
|
81
|
+
parseInteractionCancel,
|
|
82
|
+
parseInteractionRequest
|
|
83
|
+
} from "./chunk-6VQCFJWP.js";
|
|
84
|
+
import {
|
|
85
|
+
ModelFailoverExhaustedError,
|
|
86
|
+
buildModelChain,
|
|
87
|
+
isUpstreamUnavailable,
|
|
88
|
+
readHttpStatusHint,
|
|
89
|
+
readUpstreamUnavailableCode,
|
|
90
|
+
runWithModelFailover
|
|
91
|
+
} from "./chunk-ZLX4BA27.js";
|
|
92
|
+
import {
|
|
93
|
+
checkModelPolicy
|
|
94
|
+
} from "./chunk-XELWWXJJ.js";
|
|
95
|
+
import {
|
|
96
|
+
composeRouterTurnPromptSections,
|
|
97
|
+
runRouterTurn
|
|
98
|
+
} from "./chunk-IDG2XR5O.js";
|
|
99
|
+
import {
|
|
100
|
+
createIntelligenceTurnTraceSink,
|
|
101
|
+
createTurnTrace,
|
|
102
|
+
formatServerTiming,
|
|
103
|
+
otlpTraceId
|
|
104
|
+
} from "./chunk-AVBNS64P.js";
|
|
105
|
+
import {
|
|
106
|
+
parsePlanSubmittedEvent,
|
|
107
|
+
planToPersistedPart
|
|
108
|
+
} from "./chunk-WL7PZK7Q.js";
|
|
109
|
+
|
|
110
|
+
// src/chat-routes/reasoning-clock.ts
|
|
111
|
+
function createReasoningClock(now = Date.now) {
|
|
112
|
+
const runs = /* @__PURE__ */ new Map();
|
|
113
|
+
let open = null;
|
|
114
|
+
const settle = (at) => {
|
|
115
|
+
if (!open) return;
|
|
116
|
+
const time = runs.get(open.run);
|
|
117
|
+
if (time && time.end === void 0) time.end = Math.max(time.start ?? 0, at ?? now());
|
|
118
|
+
open = null;
|
|
119
|
+
};
|
|
120
|
+
return {
|
|
121
|
+
reasoning(run) {
|
|
122
|
+
if (open && open.run === run) return;
|
|
123
|
+
settle();
|
|
124
|
+
const time = runs.get(run);
|
|
125
|
+
if (time) delete time.end;
|
|
126
|
+
else runs.set(run, { start: now() });
|
|
127
|
+
open = { run };
|
|
128
|
+
},
|
|
129
|
+
settle,
|
|
130
|
+
time(run, own) {
|
|
131
|
+
const time = runs.get(run);
|
|
132
|
+
if (!time) return own;
|
|
133
|
+
const start = own?.start ?? time.start;
|
|
134
|
+
const end = own?.end ?? time.end;
|
|
135
|
+
return { ...start !== void 0 ? { start } : {}, ...end !== void 0 ? { end } : {} };
|
|
136
|
+
}
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
// src/chat-routes/router-producer.ts
|
|
141
|
+
function createRouterTurnProducer(options) {
|
|
142
|
+
const abort = new AbortController();
|
|
143
|
+
const signal = options.signal ? AbortSignal.any([abort.signal, options.signal]) : abort.signal;
|
|
144
|
+
void options.admitted?.catch((error) => {
|
|
145
|
+
abort.abort(error);
|
|
146
|
+
});
|
|
147
|
+
const credential = Promise.resolve(options.apiKey);
|
|
148
|
+
let credentialSettled = false;
|
|
149
|
+
void credential.then(() => {
|
|
150
|
+
credentialSettled = true;
|
|
151
|
+
}, () => {
|
|
152
|
+
credentialSettled = true;
|
|
153
|
+
});
|
|
154
|
+
let retired;
|
|
155
|
+
const retire = () => retired ??= credential.then(async (key) => {
|
|
156
|
+
if (typeof key !== "string") await key.revoke();
|
|
157
|
+
}, () => void 0).catch((error) => {
|
|
158
|
+
console.error(JSON.stringify({ event: "router_turn_key_retire_failed", error: redactErrorMessage(error) }));
|
|
159
|
+
});
|
|
160
|
+
let stop = () => {
|
|
161
|
+
};
|
|
162
|
+
const cancelled = new Promise((_resolve, reject) => {
|
|
163
|
+
stop = reject;
|
|
164
|
+
});
|
|
165
|
+
const stopped = () => {
|
|
166
|
+
stop(signal.reason);
|
|
167
|
+
void retire();
|
|
168
|
+
};
|
|
169
|
+
signal.addEventListener("abort", stopped, { once: true });
|
|
170
|
+
if (signal.aborted) stopped();
|
|
171
|
+
const queue = [];
|
|
172
|
+
let wake = null;
|
|
173
|
+
let done = false;
|
|
174
|
+
let outcome = null;
|
|
175
|
+
let usageReported = false;
|
|
176
|
+
const parts = [];
|
|
177
|
+
const toolParts = /* @__PURE__ */ new Map();
|
|
178
|
+
let textPart = null;
|
|
179
|
+
let textSegment = 0;
|
|
180
|
+
let reasoningPart = null;
|
|
181
|
+
const reasoningClock = createReasoningClock();
|
|
182
|
+
const push = (event) => {
|
|
183
|
+
queue.push(event);
|
|
184
|
+
wake?.();
|
|
185
|
+
};
|
|
186
|
+
const run = Promise.race([Promise.all([credential, options.systemPrompt]), cancelled]).then(([key, prompt]) => {
|
|
187
|
+
signal.throwIfAborted();
|
|
188
|
+
return runRouterTurn({
|
|
189
|
+
...options,
|
|
190
|
+
apiKey: typeof key === "string" ? key : key.key,
|
|
191
|
+
fetchImpl: typeof key === "string" ? options.fetchImpl : key.fetchImpl ?? options.fetchImpl,
|
|
192
|
+
signal,
|
|
193
|
+
tools: options.tools?.map((tool) => ({ ...tool, run: async (...args) => {
|
|
194
|
+
await options.admitted;
|
|
195
|
+
signal.throwIfAborted();
|
|
196
|
+
return tool.run(...args);
|
|
197
|
+
} })),
|
|
198
|
+
systemPrompt: typeof prompt === "string" ? prompt : composeRouterTurnPromptSections(prompt.profile, prompt).text,
|
|
199
|
+
showReasoning: options.showReasoning ?? true,
|
|
200
|
+
onEvent: (event) => {
|
|
201
|
+
if (event.type === "text") {
|
|
202
|
+
if (event.segment !== textSegment || !textPart) {
|
|
203
|
+
textSegment = event.segment;
|
|
204
|
+
textPart = { type: "text", text: "" };
|
|
205
|
+
parts.push(textPart);
|
|
206
|
+
}
|
|
207
|
+
textPart.text += event.delta;
|
|
208
|
+
reasoningPart = null;
|
|
209
|
+
reasoningClock.settle();
|
|
210
|
+
push({ type: "text", text: event.delta });
|
|
211
|
+
} else if (event.type === "reasoning") {
|
|
212
|
+
if (!reasoningPart) {
|
|
213
|
+
reasoningPart = { type: "reasoning", text: "" };
|
|
214
|
+
parts.push(reasoningPart);
|
|
215
|
+
}
|
|
216
|
+
reasoningPart.text += event.delta;
|
|
217
|
+
reasoningClock.reasoning(reasoningPart);
|
|
218
|
+
push({ type: "reasoning", text: event.delta });
|
|
219
|
+
} else if (event.type === "tool") {
|
|
220
|
+
reasoningPart = null;
|
|
221
|
+
reasoningClock.settle(event.startedAt);
|
|
222
|
+
if (event.status === "running") {
|
|
223
|
+
const part = {
|
|
224
|
+
type: "tool",
|
|
225
|
+
id: event.callId,
|
|
226
|
+
callID: event.callId,
|
|
227
|
+
tool: event.toolName,
|
|
228
|
+
state: { status: "running", input: event.input, time: { start: event.startedAt } }
|
|
229
|
+
};
|
|
230
|
+
toolParts.set(event.callId, part);
|
|
231
|
+
parts.push(part);
|
|
232
|
+
push({ type: "tool_call", call: { toolCallId: event.callId, toolName: event.toolName, args: event.input } });
|
|
233
|
+
} else {
|
|
234
|
+
const part = toolParts.get(event.callId);
|
|
235
|
+
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 } };
|
|
236
|
+
if (part) part.state = { ...part.state, ...state };
|
|
237
|
+
push({
|
|
238
|
+
type: "tool_result",
|
|
239
|
+
toolCallId: event.callId,
|
|
240
|
+
toolName: event.toolName,
|
|
241
|
+
outcome: event.status === "completed" ? { ok: true, result: event.output } : { ok: false, message: event.error.message, code: event.error.code }
|
|
242
|
+
});
|
|
243
|
+
}
|
|
244
|
+
} else if (event.type === "repeat") {
|
|
245
|
+
reasoningPart = null;
|
|
246
|
+
reasoningClock.settle();
|
|
247
|
+
} else if (event.type === "usage") {
|
|
248
|
+
usageReported = true;
|
|
249
|
+
push({ type: "usage", usage: {
|
|
250
|
+
promptTokens: event.promptTokens,
|
|
251
|
+
completionTokens: event.completionTokens,
|
|
252
|
+
...event.cachedPromptTokens !== void 0 ? { cachedPromptTokens: event.cachedPromptTokens } : {}
|
|
253
|
+
} });
|
|
254
|
+
} else if (event.type === "tool_event") {
|
|
255
|
+
const part = toolParts.get(event.callId);
|
|
256
|
+
if (part) {
|
|
257
|
+
const state = part.state;
|
|
258
|
+
part.state = { ...state, events: [...state.events ?? [], { kind: event.kind, data: event.data, at: event.at }] };
|
|
259
|
+
}
|
|
260
|
+
push({ type: "tool.event", data: { toolCallId: event.callId, toolName: event.toolName, kind: event.kind, data: event.data, at: event.at } });
|
|
261
|
+
}
|
|
262
|
+
if (options.onEvent) {
|
|
263
|
+
try {
|
|
264
|
+
options.onEvent(event);
|
|
265
|
+
} catch {
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
});
|
|
270
|
+
}).then(async (result) => {
|
|
271
|
+
outcome = result;
|
|
272
|
+
const final = await options.onOutcome?.(result);
|
|
273
|
+
if (final && final.trailingText) {
|
|
274
|
+
result.text += final.trailingText;
|
|
275
|
+
parts.push({ type: "text", text: final.trailingText });
|
|
276
|
+
push({ type: "text", text: final.trailingText });
|
|
277
|
+
}
|
|
278
|
+
if (final && final.failure) result.failure = final.failure;
|
|
279
|
+
if (result.failure) {
|
|
280
|
+
push({ type: "error", data: { message: result.failure.message, code: result.failure.code } });
|
|
281
|
+
}
|
|
282
|
+
}).finally(async () => {
|
|
283
|
+
const retiring = retire();
|
|
284
|
+
if (credentialSettled) await retiring;
|
|
285
|
+
try {
|
|
286
|
+
await options.onSettled?.();
|
|
287
|
+
} catch (error) {
|
|
288
|
+
console.error(JSON.stringify({ event: "router_turn_settle_failed", error: redactErrorMessage(error) }));
|
|
289
|
+
}
|
|
290
|
+
signal.removeEventListener("abort", stopped);
|
|
291
|
+
reasoningClock.settle();
|
|
292
|
+
done = true;
|
|
293
|
+
wake?.();
|
|
294
|
+
});
|
|
295
|
+
void run.catch(() => void 0);
|
|
296
|
+
async function* stream() {
|
|
297
|
+
for (; ; ) {
|
|
298
|
+
while (queue.length) yield queue.shift();
|
|
299
|
+
if (done) break;
|
|
300
|
+
await new Promise((resolve) => {
|
|
301
|
+
wake = resolve;
|
|
302
|
+
});
|
|
303
|
+
wake = null;
|
|
304
|
+
}
|
|
305
|
+
await run;
|
|
306
|
+
}
|
|
307
|
+
const snapshot = () => parts.map((part) => part.type === "reasoning" ? { ...part, time: reasoningClock.time(part) } : { ...part });
|
|
308
|
+
const usage = () => !outcome || !usageReported ? {} : {
|
|
309
|
+
// The Router's prompt tokens include the cached ones; the sandbox lanes report input and cache reads apart, so
|
|
310
|
+
// the stored row counts them apart here too and `inputTokens + cacheReadTokens` is the prompt on either lane.
|
|
311
|
+
inputTokens: outcome.usage.promptTokens - (outcome.usage.cachedPromptTokens ?? 0),
|
|
312
|
+
outputTokens: outcome.usage.completionTokens,
|
|
313
|
+
...outcome.usage.cachedPromptTokens !== void 0 ? { cacheReadTokens: outcome.usage.cachedPromptTokens } : {}
|
|
314
|
+
};
|
|
315
|
+
const events = stream();
|
|
316
|
+
const end = events.return.bind(events);
|
|
317
|
+
const fail = events.throw.bind(events);
|
|
318
|
+
events.return = async (value) => {
|
|
319
|
+
abort.abort(new Error("The chat route stopped reading this turn"));
|
|
320
|
+
try {
|
|
321
|
+
return await end(value);
|
|
322
|
+
} finally {
|
|
323
|
+
const retiring = retire();
|
|
324
|
+
if (credentialSettled) await retiring;
|
|
325
|
+
}
|
|
326
|
+
};
|
|
327
|
+
events.throw = async (error) => {
|
|
328
|
+
abort.abort(error);
|
|
329
|
+
try {
|
|
330
|
+
return await fail(error);
|
|
331
|
+
} finally {
|
|
332
|
+
const retiring = retire();
|
|
333
|
+
if (credentialSettled) await retiring;
|
|
334
|
+
}
|
|
335
|
+
};
|
|
336
|
+
return {
|
|
337
|
+
stream: events,
|
|
338
|
+
// The route tracks this continuation even when failure is visible before a pending key arrives.
|
|
339
|
+
cleanup: run.then(retire, retire),
|
|
340
|
+
finalText: () => outcome?.text ?? "",
|
|
341
|
+
assistantParts: snapshot,
|
|
342
|
+
draftParts: snapshot,
|
|
343
|
+
usage,
|
|
344
|
+
get model() {
|
|
345
|
+
return outcome?.servedModel ?? options.model;
|
|
346
|
+
},
|
|
347
|
+
modelAttribution: () => ({
|
|
348
|
+
requestedModel: options.model,
|
|
349
|
+
...outcome?.servedModel ? { servedModel: outcome.servedModel } : {},
|
|
350
|
+
echoReceived: Boolean(outcome?.servedModel)
|
|
351
|
+
})
|
|
352
|
+
};
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
// src/chat-routes/hybrid-producer.ts
|
|
356
|
+
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.";
|
|
357
|
+
var DEFAULT_SANDBOX_RUN_TIMEOUT_MS = 4 * 6e4;
|
|
358
|
+
function startHybridBox(body, acquire) {
|
|
359
|
+
const attachments = (body.parts ?? []).map((part) => ({
|
|
360
|
+
...part.mediaType ? { mediaType: part.mediaType } : {},
|
|
361
|
+
...part.filename ? { name: part.filename } : {}
|
|
362
|
+
}));
|
|
363
|
+
if (escalationHints({ text: body.content ?? "", attachments }).length === 0) return void 0;
|
|
364
|
+
const started = acquire();
|
|
365
|
+
started.catch(() => void 0);
|
|
366
|
+
return started;
|
|
367
|
+
}
|
|
368
|
+
function turnPromptText(prompt) {
|
|
369
|
+
return typeof prompt === "string" ? prompt : prompt.map((part) => part.type === "text" ? part.text : "").join("\n");
|
|
370
|
+
}
|
|
371
|
+
function turnCarriesFiles(prompt) {
|
|
372
|
+
return typeof prompt !== "string" && prompt.some((part) => part.type !== "text");
|
|
373
|
+
}
|
|
374
|
+
function routerTurnHistory(priorMessages, limit = 40) {
|
|
375
|
+
return priorMessages.filter((message) => (message.role === "user" || message.role === "assistant") && Boolean(message.content?.trim())).slice(-limit).map(({ role, content }) => ({ role, content }));
|
|
376
|
+
}
|
|
377
|
+
function createHybridTurnProducer(options) {
|
|
378
|
+
const { box, tools = [], speculateOn, attachments, onSpeculate, guidance, onSettled, admitted, signal, ...router } = options;
|
|
379
|
+
const abort = new AbortController();
|
|
380
|
+
const stop = signal ? AbortSignal.any([abort.signal, signal]) : abort.signal;
|
|
381
|
+
void admitted?.catch((error) => {
|
|
382
|
+
abort.abort(error);
|
|
383
|
+
});
|
|
384
|
+
const timeoutMs = box.timeoutMs ?? DEFAULT_SANDBOX_RUN_TIMEOUT_MS;
|
|
385
|
+
let start = box.started?.catch((error) => {
|
|
386
|
+
start = void 0;
|
|
387
|
+
throw error;
|
|
388
|
+
});
|
|
389
|
+
start?.catch(() => void 0);
|
|
390
|
+
const acquire = async () => {
|
|
391
|
+
await admitted;
|
|
392
|
+
stop.throwIfAborted();
|
|
393
|
+
return start ??= box.acquire(stop).catch((error) => {
|
|
394
|
+
start = void 0;
|
|
395
|
+
throw error;
|
|
396
|
+
});
|
|
397
|
+
};
|
|
398
|
+
let settled;
|
|
399
|
+
const settle = () => settled ??= (async () => {
|
|
400
|
+
abort.abort(new Error("The hybrid turn settled"));
|
|
401
|
+
await (box.started ?? start)?.catch(() => void 0);
|
|
402
|
+
const results = await Promise.allSettled([Promise.resolve().then(() => box.dispose?.()), Promise.resolve().then(() => onSettled?.())]);
|
|
403
|
+
for (const result of results) if (result.status === "rejected") {
|
|
404
|
+
console.error(JSON.stringify({ event: "hybrid_turn_settle_failed", error: redactErrorMessage(result.reason) }));
|
|
405
|
+
}
|
|
406
|
+
})();
|
|
407
|
+
try {
|
|
408
|
+
const warnings = /* @__PURE__ */ new Map();
|
|
409
|
+
const withWarnings = (parts) => parts.flatMap((part) => {
|
|
410
|
+
const warning = warnings.get(String(part.callID ?? part.id ?? ""));
|
|
411
|
+
return warning ? [part, noticePart(warning.noticeKind, warning.id, warning.text, warning.code)] : [part];
|
|
412
|
+
});
|
|
413
|
+
const sandboxRun = createSandboxRunTool({
|
|
414
|
+
box: () => acquire(),
|
|
415
|
+
runTask: async (...args) => {
|
|
416
|
+
await admitted;
|
|
417
|
+
stop.throwIfAborted();
|
|
418
|
+
return box.runTask(...args);
|
|
419
|
+
},
|
|
420
|
+
memberRequest: router.userMessage,
|
|
421
|
+
timeoutMs,
|
|
422
|
+
signal: stop,
|
|
423
|
+
...box.warm ? { warm: box.warm } : {},
|
|
424
|
+
...box.onFailure ? { onFailure: box.onFailure } : {},
|
|
425
|
+
...box.name ? { name: box.name } : {},
|
|
426
|
+
...box.description ? { description: box.description } : {}
|
|
427
|
+
});
|
|
428
|
+
const speculator = createEscalationSpeculator({
|
|
429
|
+
warm: acquire,
|
|
430
|
+
escalationTools: [sandboxRun.name],
|
|
431
|
+
...onSpeculate ? { onSpeculate } : {}
|
|
432
|
+
});
|
|
433
|
+
speculator.onInput({ text: speculateOn ?? router.userMessage, ...attachments ? { attachments } : {} });
|
|
434
|
+
const extra = guidance === false ? "" : guidance ?? HYBRID_TURN_GUIDANCE;
|
|
435
|
+
const producer = createRouterTurnProducer({
|
|
436
|
+
...router,
|
|
437
|
+
systemPrompt: Promise.resolve(router.systemPrompt).then((prompt) => !extra ? prompt : typeof prompt === "string" ? `${prompt}
|
|
438
|
+
|
|
439
|
+
${extra}` : { ...prompt, stable: [...prompt.stable ?? [], extra] }),
|
|
440
|
+
tools: [...tools, sandboxRun],
|
|
441
|
+
admitted,
|
|
442
|
+
// A turn that calls the box must outlive the call it waits on.
|
|
443
|
+
budgetMs: router.budgetMs ?? timeoutMs + 6e4,
|
|
444
|
+
signal: stop,
|
|
445
|
+
onSettled: settle,
|
|
446
|
+
onEvent: (event) => {
|
|
447
|
+
speculator.onEvent(event);
|
|
448
|
+
if (event.type === "tool" && event.status === "error" && event.toolName === sandboxRun.name && !stop.aborted) {
|
|
449
|
+
const code = event.error.code;
|
|
450
|
+
if (code === "sandbox_unavailable" || code === "sandbox_failed" || code === "sandbox_empty") {
|
|
451
|
+
warnings.set(event.callId, {
|
|
452
|
+
type: "notice",
|
|
453
|
+
noticeKind: "warning",
|
|
454
|
+
id: `sandbox-warning:${event.callId}`,
|
|
455
|
+
code,
|
|
456
|
+
text: code === "sandbox_unavailable" ? "The workspace computer is unavailable. I can continue with work that does not need it." : "The computer step did not return a confirmed answer. I can continue with work that does not need it."
|
|
457
|
+
});
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
router.onEvent?.(event);
|
|
461
|
+
}
|
|
462
|
+
});
|
|
463
|
+
const events = (async function* () {
|
|
464
|
+
for await (const event of producer.stream) {
|
|
465
|
+
yield event;
|
|
466
|
+
if (event.type === "tool_result") {
|
|
467
|
+
const warning = warnings.get(event.toolCallId);
|
|
468
|
+
if (warning) yield warning;
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
})();
|
|
472
|
+
const end = events.return.bind(events);
|
|
473
|
+
const fail = events.throw.bind(events);
|
|
474
|
+
events.return = async (value) => {
|
|
475
|
+
const closing = producer.stream.return(value);
|
|
476
|
+
try {
|
|
477
|
+
return await end(value);
|
|
478
|
+
} finally {
|
|
479
|
+
await closing;
|
|
480
|
+
}
|
|
481
|
+
};
|
|
482
|
+
events.throw = async (error) => {
|
|
483
|
+
const closing = producer.stream.throw(error);
|
|
484
|
+
try {
|
|
485
|
+
return await fail(error);
|
|
486
|
+
} finally {
|
|
487
|
+
await closing;
|
|
488
|
+
}
|
|
489
|
+
};
|
|
490
|
+
return {
|
|
491
|
+
...producer,
|
|
492
|
+
stream: events,
|
|
493
|
+
assistantParts: () => withWarnings(producer.assistantParts?.() ?? []),
|
|
494
|
+
draftParts: () => withWarnings(producer.draftParts?.() ?? []),
|
|
495
|
+
get model() {
|
|
496
|
+
return producer.model;
|
|
497
|
+
}
|
|
498
|
+
};
|
|
499
|
+
} catch (error) {
|
|
500
|
+
return createRouterTurnProducer({ ...router, systemPrompt: Promise.reject(error), signal: stop, onSettled: settle });
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
// src/chat-routes/model-failover-stream.ts
|
|
505
|
+
var TERMINAL_FAILURE_TYPES = /* @__PURE__ */ new Set(["error", "session.run.failed"]);
|
|
506
|
+
var NON_COMMITTING_PART_TYPES = /* @__PURE__ */ new Set(["step-start", "step-finish"]);
|
|
507
|
+
function isCommittingSandboxEvent(event) {
|
|
508
|
+
const record = asRecord(event);
|
|
509
|
+
if (!record) return false;
|
|
510
|
+
const type = asString(record.type) ?? "";
|
|
511
|
+
if (!type) return false;
|
|
512
|
+
if (type === "message.part.updated") {
|
|
513
|
+
const data = asRecord(record.data);
|
|
514
|
+
const part = asRecord(data?.part);
|
|
515
|
+
const partType = asString(part?.type) ?? "";
|
|
516
|
+
if (NON_COMMITTING_PART_TYPES.has(partType)) return false;
|
|
517
|
+
if (partType === "text" || partType === "reasoning") {
|
|
518
|
+
return [data?.delta, part?.text, part?.content].some((value) => typeof value === "string" && value.length > 0);
|
|
519
|
+
}
|
|
520
|
+
return true;
|
|
521
|
+
}
|
|
522
|
+
if (type === "start" || type === "execution.started" || type === "status" || type === "model-processing" || type === "model.processing" || // Public Sandbox timing metadata: a request was attempted, not answered.
|
|
523
|
+
type === "model.first_request" || type === "session.created" || type === "session.updated" || type === "session.idle" || type === "step-start" || type === "step-finish" || type === "turn" || type === "warning") {
|
|
524
|
+
return false;
|
|
525
|
+
}
|
|
526
|
+
return true;
|
|
527
|
+
}
|
|
528
|
+
function summarizeFailoverReason(reason) {
|
|
529
|
+
const collapsed = reason.replace(/\s+/g, " ").trim();
|
|
530
|
+
if (!collapsed) return "upstream unavailable";
|
|
531
|
+
if (/<!doctype html|<html[\s>]/i.test(collapsed)) {
|
|
532
|
+
const status = readHttpStatusHint(collapsed);
|
|
533
|
+
return status ? `HTTP ${status}` : "upstream returned an error page";
|
|
534
|
+
}
|
|
535
|
+
const LIMIT = 160;
|
|
536
|
+
if (collapsed.length <= LIMIT) return collapsed;
|
|
537
|
+
const clipped = collapsed.slice(0, LIMIT);
|
|
538
|
+
const lastSpace = clipped.lastIndexOf(" ");
|
|
539
|
+
return `${(lastSpace > LIMIT - 30 ? clipped.slice(0, lastSpace) : clipped).trimEnd()}\u2026`;
|
|
540
|
+
}
|
|
541
|
+
function isLiveLifecycleEvent(event) {
|
|
542
|
+
const type = asString(asRecord(event)?.type) ?? "";
|
|
543
|
+
return type === "start" || type === "execution.started" || type === "status" || type === "model-processing" || type === "model.processing" || // Public Sandbox timing metadata: a request was attempted, not answered.
|
|
544
|
+
type === "model.first_request" || type === "session.created" || type === "session.updated" || type === "session.idle";
|
|
545
|
+
}
|
|
546
|
+
function classifyTerminalFailure(event) {
|
|
547
|
+
const record = asRecord(event);
|
|
548
|
+
if (!record) return null;
|
|
549
|
+
const type = asString(record.type) ?? "";
|
|
550
|
+
if (!TERMINAL_FAILURE_TYPES.has(type)) return null;
|
|
551
|
+
const data = asRecord(record.data);
|
|
552
|
+
const outage = isUpstreamUnavailable(data) || isUpstreamUnavailable(record);
|
|
553
|
+
const reason = asString(data?.message) ?? asString(data?.error) ?? asString(data?.reason) ?? asString(record.message) ?? `sandbox stream reported ${type}`;
|
|
554
|
+
const code = readUpstreamUnavailableCode(data) ?? asString(data?.errorCode) ?? asString(data?.code) ?? readUpstreamUnavailableCode(record) ?? asString(record.errorCode) ?? asString(record.code);
|
|
555
|
+
return { outage, reason, ...code ? { code } : {} };
|
|
556
|
+
}
|
|
557
|
+
var DEFAULT_MODEL_STREAM_OPEN_TIMEOUT_MS = 12e4;
|
|
558
|
+
var DEFAULT_MODEL_FIRST_RESPONSE_TIMEOUT_MS = 6e4;
|
|
559
|
+
var ModelFailoverTimeoutError = class extends ModelFailoverExhaustedError {
|
|
560
|
+
code;
|
|
561
|
+
model;
|
|
562
|
+
timeoutMs;
|
|
563
|
+
constructor(input) {
|
|
564
|
+
super(input.attempts);
|
|
565
|
+
this.name = "ModelFailoverTimeoutError";
|
|
566
|
+
this.code = input.code;
|
|
567
|
+
this.model = input.model;
|
|
568
|
+
this.timeoutMs = input.timeoutMs;
|
|
569
|
+
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.`;
|
|
570
|
+
}
|
|
571
|
+
};
|
|
572
|
+
function resolveTimeoutMs(value, fallback, label) {
|
|
573
|
+
if (value === void 0) return fallback;
|
|
574
|
+
if (!Number.isFinite(value) || value <= 0) {
|
|
575
|
+
throw new RangeError(`${label} must be a finite number greater than 0`);
|
|
576
|
+
}
|
|
577
|
+
return Math.max(1, Math.trunc(value));
|
|
578
|
+
}
|
|
579
|
+
var TIMED_OUT = /* @__PURE__ */ Symbol("model-attempt-timed-out");
|
|
580
|
+
function deadlineAfter(timeoutMs) {
|
|
581
|
+
let timer;
|
|
582
|
+
const promise = new Promise((resolve) => {
|
|
583
|
+
timer = setTimeout(() => resolve(TIMED_OUT), timeoutMs);
|
|
584
|
+
});
|
|
585
|
+
return {
|
|
586
|
+
promise,
|
|
587
|
+
clear: () => {
|
|
588
|
+
if (timer !== void 0) {
|
|
589
|
+
clearTimeout(timer);
|
|
590
|
+
timer = void 0;
|
|
591
|
+
}
|
|
592
|
+
}
|
|
593
|
+
};
|
|
594
|
+
}
|
|
595
|
+
function isEmptyTerminalReceipt(event) {
|
|
596
|
+
const record = asRecord(event);
|
|
597
|
+
if (!record) return false;
|
|
598
|
+
const type = asString(record.type) ?? "";
|
|
599
|
+
if (type !== "result" && type !== "done") return false;
|
|
600
|
+
const data = asRecord(record.data);
|
|
601
|
+
const text = asString(data?.finalText) ?? asString(data?.text) ?? "";
|
|
602
|
+
return text.trim().length === 0;
|
|
603
|
+
}
|
|
604
|
+
var MAX_EMPTY_TURN_RETRIES = 3;
|
|
605
|
+
function resolveEmptyTurnRetries(value) {
|
|
606
|
+
if (typeof value !== "number" || !Number.isFinite(value)) return 0;
|
|
607
|
+
const truncated = Math.trunc(value);
|
|
608
|
+
if (truncated <= 0) return 0;
|
|
609
|
+
return Math.min(truncated, MAX_EMPTY_TURN_RETRIES);
|
|
610
|
+
}
|
|
611
|
+
async function closeIterator(iterator, log) {
|
|
612
|
+
try {
|
|
613
|
+
await iterator.return?.();
|
|
614
|
+
} catch (err) {
|
|
615
|
+
log?.("[chat-routes] abandoning a failed model stream threw", {
|
|
616
|
+
error: err instanceof Error ? err.message : String(err)
|
|
617
|
+
});
|
|
618
|
+
}
|
|
619
|
+
}
|
|
620
|
+
function streamWithModelFailover(options) {
|
|
621
|
+
const committing = options.isCommitting ?? isCommittingSandboxEvent;
|
|
622
|
+
const emptyTurnRetries = resolveEmptyTurnRetries(options.emptyTurnRetries);
|
|
623
|
+
const openTimeoutMs = resolveTimeoutMs(
|
|
624
|
+
options.openTimeoutMs,
|
|
625
|
+
DEFAULT_MODEL_STREAM_OPEN_TIMEOUT_MS,
|
|
626
|
+
"openTimeoutMs"
|
|
627
|
+
);
|
|
628
|
+
const firstResponseTimeoutMs = resolveTimeoutMs(
|
|
629
|
+
options.firstResponseTimeoutMs,
|
|
630
|
+
DEFAULT_MODEL_FIRST_RESPONSE_TIMEOUT_MS,
|
|
631
|
+
"firstResponseTimeoutMs"
|
|
632
|
+
);
|
|
633
|
+
const liveLifecycle = options.liveLifecycleEvents === true;
|
|
634
|
+
const progress = createProgressQueue();
|
|
635
|
+
let serving;
|
|
636
|
+
let trail = [];
|
|
637
|
+
let fellBack = false;
|
|
638
|
+
const attemptState = { index: 0 };
|
|
639
|
+
let finalTimeout;
|
|
640
|
+
const { drainOnce } = createModelAttemptDrainer(attemptState, options, openTimeoutMs, firstResponseTimeoutMs, committing, liveLifecycle, progress);
|
|
641
|
+
const probe = async (model) => {
|
|
642
|
+
finalTimeout = void 0;
|
|
643
|
+
for (let retry = 0; ; retry += 1) {
|
|
644
|
+
const pass = await drainOnce(model);
|
|
645
|
+
if (!pass.empty || retry >= emptyTurnRetries) {
|
|
646
|
+
if (!pass.outcome.committed && pass.outcome.timeoutCode) {
|
|
647
|
+
finalTimeout = {
|
|
648
|
+
code: pass.outcome.timeoutCode,
|
|
649
|
+
model,
|
|
650
|
+
timeoutMs: pass.outcome.timeoutCode === "model_stream_open_timeout" ? openTimeoutMs : firstResponseTimeoutMs
|
|
651
|
+
};
|
|
652
|
+
} else {
|
|
653
|
+
finalTimeout = void 0;
|
|
654
|
+
}
|
|
655
|
+
return pass.outcome;
|
|
656
|
+
}
|
|
657
|
+
const iterator = pass.outcome.committed ? pass.outcome.iterator : null;
|
|
658
|
+
if (pass.outcome.committed) pass.outcome.abort();
|
|
659
|
+
if (iterator) void closeIterator(iterator, options.log);
|
|
660
|
+
const info = { model, retry: retry + 1, remaining: emptyTurnRetries - retry - 1 };
|
|
661
|
+
options.log?.("[chat-routes] turn completed with no assistant text; re-running the same model", {
|
|
662
|
+
...info
|
|
663
|
+
});
|
|
664
|
+
options.onEmptyTurnRetry?.(info);
|
|
665
|
+
}
|
|
666
|
+
};
|
|
667
|
+
const events = (async function* () {
|
|
668
|
+
let handle;
|
|
669
|
+
const settled = runWithModelFailover({
|
|
670
|
+
models: options.models,
|
|
671
|
+
run: probe,
|
|
672
|
+
...options.policy ? { policy: options.policy } : {},
|
|
673
|
+
// The probe has already classified the raw payload with
|
|
674
|
+
// `isUpstreamUnavailable`; this reads its verdict rather than
|
|
675
|
+
// re-classifying a wrapper object, so the two can never disagree.
|
|
676
|
+
isUnavailableResult: (result) => result.committed === false,
|
|
677
|
+
onFallback: (attempt, nextModel) => {
|
|
678
|
+
const info = {
|
|
679
|
+
from: attempt.model,
|
|
680
|
+
to: nextModel,
|
|
681
|
+
reason: attempt.reason ?? "upstream unavailable"
|
|
682
|
+
};
|
|
683
|
+
options.log?.("[chat-routes] model upstream unavailable; falling over", { ...info });
|
|
684
|
+
options.onFallback?.(info);
|
|
685
|
+
}
|
|
686
|
+
}).finally(() => progress.close());
|
|
687
|
+
settled.catch(() => void 0);
|
|
688
|
+
for (; ; ) {
|
|
689
|
+
const item = await progress.pull();
|
|
690
|
+
if (item === PROGRESS_CLOSED) break;
|
|
691
|
+
yield item;
|
|
692
|
+
}
|
|
693
|
+
try {
|
|
694
|
+
const outcome = await settled;
|
|
695
|
+
serving = outcome.model;
|
|
696
|
+
trail = outcome.attempts;
|
|
697
|
+
fellBack = outcome.usedFallback;
|
|
698
|
+
handle = outcome.value;
|
|
699
|
+
} catch (err) {
|
|
700
|
+
const attempts = err?.attempts;
|
|
701
|
+
if (Array.isArray(attempts)) trail = attempts;
|
|
702
|
+
if (err instanceof ModelFailoverExhaustedError && finalTimeout) {
|
|
703
|
+
throw new ModelFailoverTimeoutError({
|
|
704
|
+
attempts: trail,
|
|
705
|
+
...finalTimeout
|
|
706
|
+
});
|
|
707
|
+
}
|
|
708
|
+
throw err;
|
|
709
|
+
}
|
|
710
|
+
for (const event of handle.buffered) yield event;
|
|
711
|
+
const live = handle.iterator;
|
|
712
|
+
if (!live) {
|
|
713
|
+
handle.abort();
|
|
714
|
+
return;
|
|
715
|
+
}
|
|
716
|
+
try {
|
|
717
|
+
for (; ; ) {
|
|
718
|
+
const next = await live.next();
|
|
719
|
+
if (next.done) return;
|
|
720
|
+
yield next.value;
|
|
721
|
+
}
|
|
722
|
+
} finally {
|
|
723
|
+
handle.abort();
|
|
724
|
+
await closeIterator(live, options.log);
|
|
725
|
+
}
|
|
726
|
+
})();
|
|
727
|
+
return {
|
|
728
|
+
events,
|
|
729
|
+
servingModel: () => serving,
|
|
730
|
+
attempts: () => trail,
|
|
731
|
+
usedFallback: () => fellBack
|
|
732
|
+
};
|
|
733
|
+
}
|
|
734
|
+
var PROGRESS_CLOSED = /* @__PURE__ */ Symbol("progress-closed");
|
|
735
|
+
function createProgressQueue() {
|
|
736
|
+
const items = [];
|
|
737
|
+
const waiters = [];
|
|
738
|
+
let closed = false;
|
|
739
|
+
const deliver = (value) => {
|
|
740
|
+
const waiter = waiters.shift();
|
|
741
|
+
if (waiter) waiter(value);
|
|
742
|
+
else items.push(value);
|
|
743
|
+
};
|
|
744
|
+
return {
|
|
745
|
+
push(event) {
|
|
746
|
+
if (closed) return;
|
|
747
|
+
deliver(event);
|
|
748
|
+
},
|
|
749
|
+
close() {
|
|
750
|
+
if (closed) return;
|
|
751
|
+
closed = true;
|
|
752
|
+
deliver(PROGRESS_CLOSED);
|
|
753
|
+
},
|
|
754
|
+
pull() {
|
|
755
|
+
if (items.length > 0) return Promise.resolve(items.shift());
|
|
756
|
+
if (closed) return Promise.resolve(PROGRESS_CLOSED);
|
|
757
|
+
return new Promise((resolve) => waiters.push(resolve));
|
|
758
|
+
}
|
|
759
|
+
};
|
|
760
|
+
}
|
|
761
|
+
function createModelAttemptDrainer(attemptState, options, openTimeoutMs, firstResponseTimeoutMs, committing, liveLifecycle, progress) {
|
|
762
|
+
const drainOnce = async (model) => {
|
|
763
|
+
attemptState.index += 1;
|
|
764
|
+
const controller = new AbortController();
|
|
765
|
+
const abort = () => {
|
|
766
|
+
if (!controller.signal.aborted) controller.abort();
|
|
767
|
+
};
|
|
768
|
+
const opening = Promise.resolve().then(() => options.open({
|
|
769
|
+
model,
|
|
770
|
+
attempt: attemptState.index,
|
|
771
|
+
signal: controller.signal
|
|
772
|
+
}));
|
|
773
|
+
const openDeadline = deadlineAfter(openTimeoutMs);
|
|
774
|
+
let opened;
|
|
775
|
+
try {
|
|
776
|
+
opened = await Promise.race([opening, openDeadline.promise]);
|
|
777
|
+
} catch (err) {
|
|
778
|
+
openDeadline.clear();
|
|
779
|
+
abort();
|
|
780
|
+
throw err;
|
|
781
|
+
}
|
|
782
|
+
if (opened === TIMED_OUT) {
|
|
783
|
+
openDeadline.clear();
|
|
784
|
+
abort();
|
|
785
|
+
void opening.then(
|
|
786
|
+
(lateSource) => {
|
|
787
|
+
try {
|
|
788
|
+
void closeIterator(lateSource[Symbol.asyncIterator](), options.log);
|
|
789
|
+
} catch (err) {
|
|
790
|
+
options.log?.("[chat-routes] closing a late model stream threw", {
|
|
791
|
+
error: err instanceof Error ? err.message : String(err)
|
|
792
|
+
});
|
|
793
|
+
}
|
|
794
|
+
},
|
|
795
|
+
(err) => {
|
|
796
|
+
options.log?.("[chat-routes] abandoned model stream open rejected after timeout", {
|
|
797
|
+
error: err instanceof Error ? err.message : String(err)
|
|
798
|
+
});
|
|
799
|
+
}
|
|
800
|
+
);
|
|
801
|
+
return {
|
|
802
|
+
outcome: {
|
|
803
|
+
committed: false,
|
|
804
|
+
error: `Opening the model event source for ${model} exceeded ${openTimeoutMs}ms`,
|
|
805
|
+
errorCode: "model_stream_open_timeout",
|
|
806
|
+
timeoutCode: "model_stream_open_timeout"
|
|
807
|
+
},
|
|
808
|
+
empty: false
|
|
809
|
+
};
|
|
810
|
+
}
|
|
811
|
+
const source = opened;
|
|
812
|
+
let iterator;
|
|
813
|
+
try {
|
|
814
|
+
iterator = source[Symbol.asyncIterator]();
|
|
815
|
+
} catch (err) {
|
|
816
|
+
openDeadline.clear();
|
|
817
|
+
abort();
|
|
818
|
+
throw err;
|
|
819
|
+
}
|
|
820
|
+
const buffered = [];
|
|
821
|
+
let next;
|
|
822
|
+
try {
|
|
823
|
+
next = await Promise.race([
|
|
824
|
+
Promise.resolve(iterator.next()),
|
|
825
|
+
openDeadline.promise
|
|
826
|
+
]);
|
|
827
|
+
} catch (err) {
|
|
828
|
+
openDeadline.clear();
|
|
829
|
+
abort();
|
|
830
|
+
void closeIterator(iterator, options.log);
|
|
831
|
+
throw err;
|
|
832
|
+
}
|
|
833
|
+
openDeadline.clear();
|
|
834
|
+
if (next === TIMED_OUT) {
|
|
835
|
+
abort();
|
|
836
|
+
void closeIterator(iterator, options.log);
|
|
837
|
+
return {
|
|
838
|
+
outcome: {
|
|
839
|
+
committed: false,
|
|
840
|
+
error: `Starting the model event source for ${model} exceeded ${openTimeoutMs}ms`,
|
|
841
|
+
errorCode: "model_stream_open_timeout",
|
|
842
|
+
timeoutCode: "model_stream_open_timeout"
|
|
843
|
+
},
|
|
844
|
+
empty: false
|
|
845
|
+
};
|
|
846
|
+
}
|
|
847
|
+
if (next.done) {
|
|
848
|
+
abort();
|
|
849
|
+
return { outcome: { committed: true, buffered, iterator: null, abort }, empty: true };
|
|
850
|
+
}
|
|
851
|
+
const firstResponseDeadline = deadlineAfter(firstResponseTimeoutMs);
|
|
852
|
+
for (; ; ) {
|
|
853
|
+
const event = next.value;
|
|
854
|
+
let failure;
|
|
855
|
+
let commits = false;
|
|
856
|
+
try {
|
|
857
|
+
failure = classifyTerminalFailure(event);
|
|
858
|
+
commits = !failure && committing(event);
|
|
859
|
+
} catch (err) {
|
|
860
|
+
firstResponseDeadline.clear();
|
|
861
|
+
abort();
|
|
862
|
+
void closeIterator(iterator, options.log);
|
|
863
|
+
throw err;
|
|
864
|
+
}
|
|
865
|
+
if (failure?.outage) {
|
|
866
|
+
firstResponseDeadline.clear();
|
|
867
|
+
abort();
|
|
868
|
+
void closeIterator(iterator, options.log);
|
|
869
|
+
return {
|
|
870
|
+
outcome: {
|
|
871
|
+
committed: false,
|
|
872
|
+
error: failure.reason,
|
|
873
|
+
...failure.code ? { errorCode: failure.code } : {}
|
|
874
|
+
},
|
|
875
|
+
empty: false
|
|
876
|
+
};
|
|
877
|
+
}
|
|
878
|
+
if (liveLifecycle && !failure && !commits && isLiveLifecycleEvent(event)) {
|
|
879
|
+
progress.push(event);
|
|
880
|
+
} else {
|
|
881
|
+
buffered.push(event);
|
|
882
|
+
}
|
|
883
|
+
if (failure) {
|
|
884
|
+
firstResponseDeadline.clear();
|
|
885
|
+
return { outcome: { committed: true, buffered, iterator, abort }, empty: false };
|
|
886
|
+
}
|
|
887
|
+
if (commits) {
|
|
888
|
+
firstResponseDeadline.clear();
|
|
889
|
+
return { outcome: { committed: true, buffered, iterator, abort }, empty: isEmptyTerminalReceipt(event) };
|
|
890
|
+
}
|
|
891
|
+
try {
|
|
892
|
+
next = await Promise.race([
|
|
893
|
+
Promise.resolve(iterator.next()),
|
|
894
|
+
firstResponseDeadline.promise
|
|
895
|
+
]);
|
|
896
|
+
} catch (err) {
|
|
897
|
+
firstResponseDeadline.clear();
|
|
898
|
+
abort();
|
|
899
|
+
void closeIterator(iterator, options.log);
|
|
900
|
+
throw err;
|
|
901
|
+
}
|
|
902
|
+
if (next === TIMED_OUT) {
|
|
903
|
+
firstResponseDeadline.clear();
|
|
904
|
+
abort();
|
|
905
|
+
void closeIterator(iterator, options.log);
|
|
906
|
+
return {
|
|
907
|
+
outcome: {
|
|
908
|
+
committed: false,
|
|
909
|
+
error: `Model ${model} produced no first answer-bearing event within ${firstResponseTimeoutMs}ms`,
|
|
910
|
+
errorCode: "provider_first_response_timeout",
|
|
911
|
+
timeoutCode: "provider_first_response_timeout"
|
|
912
|
+
},
|
|
913
|
+
empty: false
|
|
914
|
+
};
|
|
915
|
+
}
|
|
916
|
+
if (next.done) {
|
|
917
|
+
firstResponseDeadline.clear();
|
|
918
|
+
abort();
|
|
919
|
+
return { outcome: { committed: true, buffered, iterator: null, abort }, empty: true };
|
|
920
|
+
}
|
|
921
|
+
}
|
|
922
|
+
};
|
|
923
|
+
return { drainOnce };
|
|
924
|
+
}
|
|
925
|
+
|
|
926
|
+
// src/chat-routes/sandbox-model-source.ts
|
|
927
|
+
function parseEffectiveBackend(data) {
|
|
928
|
+
const backend = asRecord(data.effectiveBackend);
|
|
929
|
+
if (!backend) return void 0;
|
|
930
|
+
const provider = asString(backend.provider);
|
|
931
|
+
const model = asString(backend.model);
|
|
932
|
+
const rawSource = asString(backend.source);
|
|
933
|
+
const source = rawSource === "request" || rawSource === "environment" || rawSource === "profile" ? rawSource : void 0;
|
|
934
|
+
if (!provider && !model && !source) return void 0;
|
|
935
|
+
return {
|
|
936
|
+
...model ? { servedModel: model } : {},
|
|
937
|
+
...provider ? { servedProvider: provider } : {},
|
|
938
|
+
...source ? { servedSource: source } : {}
|
|
939
|
+
};
|
|
940
|
+
}
|
|
941
|
+
function createSandboxModelSource(options, log) {
|
|
942
|
+
if (options.openEvents && !options.model) {
|
|
943
|
+
throw new Error(
|
|
944
|
+
"createSandboxChatProducer: `openEvents` requires `model` \u2014 failover must know which model it is running"
|
|
945
|
+
);
|
|
946
|
+
}
|
|
947
|
+
if (!options.openEvents && !options.events) {
|
|
948
|
+
throw new Error("createSandboxChatProducer: pass `openEvents` (failover-capable) or `events`");
|
|
949
|
+
}
|
|
950
|
+
if (!options.openEvents && resolveEmptyTurnRetries(options.emptyTurnRetries) > 0) {
|
|
951
|
+
throw new Error(
|
|
952
|
+
"createSandboxChatProducer: `emptyTurnRetries` requires `openEvents` \u2014 a fixed `events` stream cannot be re-opened"
|
|
953
|
+
);
|
|
954
|
+
}
|
|
955
|
+
const chain = options.model ? buildModelChain(options.model, options.modelFailover === false ? [] : options.fallbackModels ?? []) : [];
|
|
956
|
+
const pendingModelNotices = [];
|
|
957
|
+
let modelNoticeCount = 0;
|
|
958
|
+
let failover;
|
|
959
|
+
let source;
|
|
960
|
+
if (options.openEvents) {
|
|
961
|
+
failover = streamWithModelFailover({
|
|
962
|
+
models: chain,
|
|
963
|
+
open: options.openEvents,
|
|
964
|
+
log,
|
|
965
|
+
...options.modelPolicy ? { policy: options.modelPolicy } : {},
|
|
966
|
+
...options.openTimeoutMs !== void 0 ? { openTimeoutMs: options.openTimeoutMs } : {},
|
|
967
|
+
...options.firstResponseTimeoutMs !== void 0 ? { firstResponseTimeoutMs: options.firstResponseTimeoutMs } : {},
|
|
968
|
+
...options.emptyTurnRetries !== void 0 ? { emptyTurnRetries: options.emptyTurnRetries } : {},
|
|
969
|
+
...options.onEmptyTurnRetry ? { onEmptyTurnRetry: options.onEmptyTurnRetry } : {},
|
|
970
|
+
...options.liveLifecycleEvents !== void 0 ? { liveLifecycleEvents: options.liveLifecycleEvents } : {},
|
|
971
|
+
onFallback: (info) => {
|
|
972
|
+
modelNoticeCount += 1;
|
|
973
|
+
pendingModelNotices.push({
|
|
974
|
+
id: `model-fallback-${modelNoticeCount}`,
|
|
975
|
+
// Named models on both sides: a quality regression after a downgrade
|
|
976
|
+
// must be attributable to the model that actually answered. The
|
|
977
|
+
// REASON is condensed — a real edge 5xx arrives as Cloudflare's HTML
|
|
978
|
+
// error page, and the raw text put `<!DOCTYPE html><!--[if lt IE 7]>…`
|
|
979
|
+
// into the customer's transcript. The verbatim text is still on
|
|
980
|
+
// `modelFailover.attempts[].reason` for the operator.
|
|
981
|
+
text: `${info.from} was unavailable (${summarizeFailoverReason(info.reason)}) \u2014 answered with ${info.to} instead.`
|
|
982
|
+
});
|
|
983
|
+
options.onModelFallback?.(info);
|
|
984
|
+
}
|
|
985
|
+
});
|
|
986
|
+
source = failover.events;
|
|
987
|
+
} else {
|
|
988
|
+
source = options.events;
|
|
989
|
+
}
|
|
990
|
+
const servingModel = () => failover?.servingModel() ?? options.model;
|
|
991
|
+
const modelState = { effectiveBackend: void 0 };
|
|
992
|
+
let substitutionNoticeQueued = false;
|
|
993
|
+
const servedModel = () => modelState.effectiveBackend?.servedModel ?? servingModel();
|
|
994
|
+
const captureEffectiveBackend = (data) => {
|
|
995
|
+
if (!data) return;
|
|
996
|
+
const parsed = parseEffectiveBackend(data);
|
|
997
|
+
if (!parsed) return;
|
|
998
|
+
modelState.effectiveBackend = parsed;
|
|
999
|
+
const sentModel = servingModel();
|
|
1000
|
+
if (substitutionNoticeQueued || !options.model || !sentModel || !parsed.servedModel || parsed.servedModel === sentModel) return;
|
|
1001
|
+
substitutionNoticeQueued = true;
|
|
1002
|
+
pendingModelNotices.push({
|
|
1003
|
+
id: "model-substitution-1",
|
|
1004
|
+
text: `Requested ${sentModel} \u2014 the sandbox answered with ${parsed.servedModel} instead.`
|
|
1005
|
+
});
|
|
1006
|
+
};
|
|
1007
|
+
return { pendingModelNotices, source, captureEffectiveBackend, servedModel, servingModel, failover, modelState };
|
|
1008
|
+
}
|
|
1009
|
+
|
|
1010
|
+
// src/chat-routes/sandbox-producer.ts
|
|
1011
|
+
function textDelta(tracker, key, part, rawDelta) {
|
|
1012
|
+
const explicit = typeof rawDelta === "string" ? rawDelta : void 0;
|
|
1013
|
+
const previous = tracker.seen.get(key) ?? "";
|
|
1014
|
+
if (explicit !== void 0) {
|
|
1015
|
+
tracker.seen.set(key, previous + explicit);
|
|
1016
|
+
return explicit;
|
|
1017
|
+
}
|
|
1018
|
+
const snapshot = asString(part.text) ?? asString(part.content) ?? "";
|
|
1019
|
+
if (!snapshot) return "";
|
|
1020
|
+
if (snapshot.startsWith(previous)) {
|
|
1021
|
+
tracker.seen.set(key, snapshot);
|
|
1022
|
+
return snapshot.slice(previous.length);
|
|
1023
|
+
}
|
|
1024
|
+
tracker.seen.set(key, snapshot);
|
|
1025
|
+
return snapshot;
|
|
1026
|
+
}
|
|
1027
|
+
function toFiniteNumber(value) {
|
|
1028
|
+
if (typeof value === "number") return Number.isFinite(value) ? value : null;
|
|
1029
|
+
if (typeof value === "string" && value.trim() !== "") {
|
|
1030
|
+
const parsed = Number(value);
|
|
1031
|
+
return Number.isFinite(parsed) ? parsed : null;
|
|
1032
|
+
}
|
|
1033
|
+
return null;
|
|
1034
|
+
}
|
|
1035
|
+
function extractReportedTurnUsage(data) {
|
|
1036
|
+
const tokenUsage = asRecord(data?.tokenUsage);
|
|
1037
|
+
if (!tokenUsage) return null;
|
|
1038
|
+
const inputTokens = toFiniteNumber(tokenUsage.inputTokens);
|
|
1039
|
+
const outputTokens = toFiniteNumber(tokenUsage.outputTokens);
|
|
1040
|
+
if (inputTokens === null || outputTokens === null) return null;
|
|
1041
|
+
const reported = { inputTokens, outputTokens };
|
|
1042
|
+
const reasoningTokens = toFiniteNumber(tokenUsage.reasoningTokens);
|
|
1043
|
+
if (reasoningTokens !== null) reported.reasoningTokens = reasoningTokens;
|
|
1044
|
+
const cacheReadTokens = toFiniteNumber(tokenUsage.cacheReadInputTokens);
|
|
1045
|
+
if (cacheReadTokens !== null) reported.cacheReadTokens = cacheReadTokens;
|
|
1046
|
+
const cacheWriteTokens = toFiniteNumber(tokenUsage.cacheCreationInputTokens);
|
|
1047
|
+
if (cacheWriteTokens !== null) reported.cacheWriteTokens = cacheWriteTokens;
|
|
1048
|
+
const costUsd = toFiniteNumber(data?.totalCostUsd) ?? toFiniteNumber(tokenUsage.cost);
|
|
1049
|
+
if (costUsd !== null) reported.costUsd = costUsd;
|
|
1050
|
+
return reported;
|
|
1051
|
+
}
|
|
1052
|
+
function extractUsageSnapshot(value) {
|
|
1053
|
+
const inputTokens = toFiniteNumber(value?.promptTokens);
|
|
1054
|
+
const outputTokens = toFiniteNumber(value?.completionTokens);
|
|
1055
|
+
if (inputTokens === null || outputTokens === null || inputTokens < 0 || outputTokens < 0) return null;
|
|
1056
|
+
const reported = { inputTokens, outputTokens };
|
|
1057
|
+
const reasoningTokens = toFiniteNumber(value?.reasoningTokens);
|
|
1058
|
+
if (reasoningTokens !== null && reasoningTokens >= 0) reported.reasoningTokens = reasoningTokens;
|
|
1059
|
+
const costUsd = toFiniteNumber(value?.providerCostUsd);
|
|
1060
|
+
if (costUsd !== null && costUsd >= 0) reported.costUsd = costUsd;
|
|
1061
|
+
return reported;
|
|
1062
|
+
}
|
|
1063
|
+
function applyTerminalUsage(reported, usage) {
|
|
1064
|
+
usage.inputTokens = reported.inputTokens;
|
|
1065
|
+
usage.outputTokens = reported.outputTokens;
|
|
1066
|
+
if (reported.reasoningTokens !== void 0) usage.reasoningTokens = reported.reasoningTokens;
|
|
1067
|
+
if (reported.cacheReadTokens !== void 0) usage.cacheReadTokens = reported.cacheReadTokens;
|
|
1068
|
+
if (reported.cacheWriteTokens !== void 0) usage.cacheWriteTokens = reported.cacheWriteTokens;
|
|
1069
|
+
if (reported.costUsd !== void 0) usage.costUsd = reported.costUsd;
|
|
1070
|
+
}
|
|
1071
|
+
function sandboxStreamErrorMessage(data) {
|
|
1072
|
+
const record = asRecord(data);
|
|
1073
|
+
const message = asString(record?.message) ?? asString(record?.error);
|
|
1074
|
+
if (message) return message;
|
|
1075
|
+
try {
|
|
1076
|
+
return JSON.stringify(data) || "Sandbox stream returned an error event without details.";
|
|
1077
|
+
} catch {
|
|
1078
|
+
return "Sandbox stream returned an error event without details.";
|
|
1079
|
+
}
|
|
1080
|
+
}
|
|
1081
|
+
function sandboxStreamErrorCode(data) {
|
|
1082
|
+
return asString(asRecord(data)?.code) ?? "sandbox.stream_error";
|
|
1083
|
+
}
|
|
1084
|
+
function toProducerWireEvent(event) {
|
|
1085
|
+
return event;
|
|
1086
|
+
}
|
|
1087
|
+
function sandboxStreamFailureDiagnostic(error) {
|
|
1088
|
+
const streamMessage = error?.streamMessage;
|
|
1089
|
+
const message = typeof streamMessage === "string" ? streamMessage : error instanceof Error ? error.message : "Sandbox stream failed";
|
|
1090
|
+
const diagnostics = asRecord(error?.diagnostics);
|
|
1091
|
+
let diagnosticText;
|
|
1092
|
+
if (diagnostics) {
|
|
1093
|
+
try {
|
|
1094
|
+
diagnosticText = JSON.stringify(diagnostics);
|
|
1095
|
+
} catch {
|
|
1096
|
+
diagnosticText = "[unserializable diagnostics]";
|
|
1097
|
+
}
|
|
1098
|
+
}
|
|
1099
|
+
const userMessage = [
|
|
1100
|
+
"The sandbox model stream stopped before a clean completion.",
|
|
1101
|
+
`Error: ${message}`,
|
|
1102
|
+
"Please retry. If it repeats, send these support details to the team."
|
|
1103
|
+
].join("\n\n");
|
|
1104
|
+
const failureNote = diagnosticText ? `sandbox-stream: ${message}; ${diagnosticText}` : `sandbox-stream: ${message}`;
|
|
1105
|
+
let code = "sandbox.stream_failed";
|
|
1106
|
+
if (error instanceof ModelFailoverTimeoutError) {
|
|
1107
|
+
code = error.code;
|
|
1108
|
+
} else if (error instanceof ModelFailoverExhaustedError) {
|
|
1109
|
+
code = error.upstreamCode ?? code;
|
|
1110
|
+
}
|
|
1111
|
+
return { userMessage, failureNote, code };
|
|
1112
|
+
}
|
|
1113
|
+
function createSandboxChatProducer(options) {
|
|
1114
|
+
const log = options.log ?? ((message, meta) => console.error(message, meta ?? ""));
|
|
1115
|
+
const renderable = options.isRenderableInteraction ?? isRenderableInteractionKind;
|
|
1116
|
+
const { pendingModelNotices, source, captureEffectiveBackend, servedModel, servingModel, failover, modelState } = createSandboxModelSource(options, log);
|
|
1117
|
+
const projectionState = {
|
|
1118
|
+
fullText: "",
|
|
1119
|
+
terminalObserved: false,
|
|
1120
|
+
stepCounter: 0,
|
|
1121
|
+
danglingToolsTerminalized: false,
|
|
1122
|
+
interactionOutcome: "answered",
|
|
1123
|
+
warningCount: 0,
|
|
1124
|
+
errorNoticeCount: 0
|
|
1125
|
+
};
|
|
1126
|
+
const { timedParts, drainModelNotices, tracker, recordPersistedPart, reasoningClock, partMap, announcedToolArgs, settledTools, usage, promotedFileParts, emitFinalUsage, emitTerminalizedTools, emitErrorNotice, partOrder } = createSandboxProjection(projectionState, pendingModelNotices);
|
|
1127
|
+
async function* stream() {
|
|
1128
|
+
try {
|
|
1129
|
+
for await (const raw of source) {
|
|
1130
|
+
const record = asRecord(raw);
|
|
1131
|
+
captureEffectiveBackend(asRecord(record?.data));
|
|
1132
|
+
yield* drainModelNotices();
|
|
1133
|
+
if (!record || typeof record.type !== "string") continue;
|
|
1134
|
+
const normalized = normalizeToolEvent({ type: record.type, data: asRecord(record.data) });
|
|
1135
|
+
const event = normalized.type === "message.part.updated" ? normalized : { type: record.type, data: asRecord(record.data) };
|
|
1136
|
+
if (event.type === "message.part.updated") {
|
|
1137
|
+
yield* projectSandboxMessagePart(event, tracker, recordPersistedPart, reasoningClock, projectionState, partMap, announcedToolArgs, settledTools, usage, options, log, promotedFileParts);
|
|
1138
|
+
continue;
|
|
1139
|
+
}
|
|
1140
|
+
if (event.type === "interaction") {
|
|
1141
|
+
const parsed = parseInteractionRequest(asRecord(record.data));
|
|
1142
|
+
if (!parsed.succeeded) {
|
|
1143
|
+
log("[chat-routes] dropping malformed interaction event", { error: parsed.error });
|
|
1144
|
+
continue;
|
|
1145
|
+
}
|
|
1146
|
+
if (renderable(parsed.value.kind, parsed.value)) {
|
|
1147
|
+
recordPersistedPart(
|
|
1148
|
+
interactionToPersistedPart(parsed.value, "pending"),
|
|
1149
|
+
void 0,
|
|
1150
|
+
interactionPartKey(parsed.value.id)
|
|
1151
|
+
);
|
|
1152
|
+
yield toProducerWireEvent(record);
|
|
1153
|
+
continue;
|
|
1154
|
+
}
|
|
1155
|
+
let declineFailed = false;
|
|
1156
|
+
if (options.declineInteraction) {
|
|
1157
|
+
try {
|
|
1158
|
+
await options.declineInteraction(parsed.value.id);
|
|
1159
|
+
} catch (err) {
|
|
1160
|
+
declineFailed = true;
|
|
1161
|
+
log("[chat-routes] failed to auto-decline interaction", {
|
|
1162
|
+
id: parsed.value.id,
|
|
1163
|
+
error: err instanceof Error ? err.message : String(err)
|
|
1164
|
+
});
|
|
1165
|
+
}
|
|
1166
|
+
} else {
|
|
1167
|
+
declineFailed = true;
|
|
1168
|
+
log("[chat-routes] non-renderable interaction with no declineInteraction wired", {
|
|
1169
|
+
id: parsed.value.id,
|
|
1170
|
+
kind: parsed.value.kind
|
|
1171
|
+
});
|
|
1172
|
+
}
|
|
1173
|
+
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.`);
|
|
1174
|
+
const notice = noticePart("auto-declined", `auto-declined-${parsed.value.id}`, text);
|
|
1175
|
+
recordPersistedPart(notice, void 0, noticePartKey(notice.id));
|
|
1176
|
+
yield { type: "notice", id: notice.id, noticeKind: "auto-declined", text };
|
|
1177
|
+
continue;
|
|
1178
|
+
}
|
|
1179
|
+
if (event.type === "interaction.cancel") {
|
|
1180
|
+
const parsed = parseInteractionCancel(asRecord(record.data));
|
|
1181
|
+
if (!parsed.succeeded) {
|
|
1182
|
+
log("[chat-routes] dropping malformed interaction.cancel event", { error: parsed.error });
|
|
1183
|
+
continue;
|
|
1184
|
+
}
|
|
1185
|
+
const key = interactionPartKey(parsed.value.id);
|
|
1186
|
+
const existing = partMap.get(key);
|
|
1187
|
+
if (existing?.type === "interaction" && existing.status === "pending") {
|
|
1188
|
+
recordPersistedPart({
|
|
1189
|
+
...existing,
|
|
1190
|
+
status: cancelStatusFor(parsed.value.reason),
|
|
1191
|
+
...parsed.value.reason ? { cancelReason: parsed.value.reason } : {}
|
|
1192
|
+
}, void 0, key);
|
|
1193
|
+
}
|
|
1194
|
+
yield toProducerWireEvent(record);
|
|
1195
|
+
continue;
|
|
1196
|
+
}
|
|
1197
|
+
if (event.type === "plan.submitted") {
|
|
1198
|
+
const parsed = parsePlanSubmittedEvent(record);
|
|
1199
|
+
if (!parsed.succeeded) {
|
|
1200
|
+
log("[chat-routes] dropping malformed plan.submitted event", { error: parsed.error });
|
|
1201
|
+
continue;
|
|
1202
|
+
}
|
|
1203
|
+
recordPersistedPart(planToPersistedPart(parsed.value), void 0);
|
|
1204
|
+
yield toProducerWireEvent(record);
|
|
1205
|
+
continue;
|
|
1206
|
+
}
|
|
1207
|
+
if (event.type === "warning") {
|
|
1208
|
+
const message = asString(event.data?.message);
|
|
1209
|
+
if (message) {
|
|
1210
|
+
projectionState.warningCount += 1;
|
|
1211
|
+
const code = asString(event.data?.code);
|
|
1212
|
+
const text = code ? `${code}: ${message}` : message;
|
|
1213
|
+
const notice = noticePart("warning", `warning-${projectionState.warningCount}`, text);
|
|
1214
|
+
recordPersistedPart(notice, void 0, noticePartKey(notice.id));
|
|
1215
|
+
yield { type: "notice", id: notice.id, noticeKind: "warning", text };
|
|
1216
|
+
}
|
|
1217
|
+
yield toProducerWireEvent(record);
|
|
1218
|
+
continue;
|
|
1219
|
+
}
|
|
1220
|
+
if (event.type === "usage") {
|
|
1221
|
+
const reported = extractUsageSnapshot(asRecord(record.usage));
|
|
1222
|
+
if (reported) applyTerminalUsage(reported, usage);
|
|
1223
|
+
yield toProducerWireEvent(record);
|
|
1224
|
+
continue;
|
|
1225
|
+
}
|
|
1226
|
+
if (event.type === "result") {
|
|
1227
|
+
projectionState.terminalObserved = true;
|
|
1228
|
+
const finalText = asString(event.data?.finalText);
|
|
1229
|
+
if (finalText) projectionState.fullText = finalText;
|
|
1230
|
+
const reported = extractReportedTurnUsage(asRecord(event.data));
|
|
1231
|
+
if (reported) {
|
|
1232
|
+
applyTerminalUsage(reported, usage);
|
|
1233
|
+
yield* emitFinalUsage();
|
|
1234
|
+
} else {
|
|
1235
|
+
const resultUsage = asRecord(event.data?.usage);
|
|
1236
|
+
if (resultUsage) {
|
|
1237
|
+
const input = Number(resultUsage.inputTokens);
|
|
1238
|
+
const output = Number(resultUsage.outputTokens);
|
|
1239
|
+
if (Number.isFinite(input)) usage.inputTokens = input;
|
|
1240
|
+
if (Number.isFinite(output)) usage.outputTokens = output;
|
|
1241
|
+
}
|
|
1242
|
+
}
|
|
1243
|
+
yield* emitTerminalizedTools();
|
|
1244
|
+
continue;
|
|
1245
|
+
}
|
|
1246
|
+
if (event.type === "done") {
|
|
1247
|
+
projectionState.terminalObserved = true;
|
|
1248
|
+
const reported = extractReportedTurnUsage(asRecord(event.data));
|
|
1249
|
+
if (reported) {
|
|
1250
|
+
applyTerminalUsage(reported, usage);
|
|
1251
|
+
yield* emitFinalUsage();
|
|
1252
|
+
}
|
|
1253
|
+
yield* emitTerminalizedTools();
|
|
1254
|
+
yield toProducerWireEvent(record);
|
|
1255
|
+
continue;
|
|
1256
|
+
}
|
|
1257
|
+
if (event.type === "error") {
|
|
1258
|
+
const message = sandboxStreamErrorMessage(event.data);
|
|
1259
|
+
const errorContent = projectionState.fullText.trim() ? `The sandbox model stream stopped before a clean completion.
|
|
1260
|
+
|
|
1261
|
+
Error: ${message}` : `The sandbox agent returned an error before producing a visible answer.
|
|
1262
|
+
|
|
1263
|
+
Error: ${message}`;
|
|
1264
|
+
if (options.errorNotice) {
|
|
1265
|
+
yield* emitErrorNotice(errorContent, sandboxStreamErrorCode(event.data));
|
|
1266
|
+
} else if (options.includeErrorText !== false) {
|
|
1267
|
+
const errorDelta = projectionState.fullText ? `
|
|
1268
|
+
|
|
1269
|
+
---
|
|
1270
|
+
${errorContent}` : errorContent;
|
|
1271
|
+
projectionState.fullText += errorDelta;
|
|
1272
|
+
yield { type: "text", text: errorDelta };
|
|
1273
|
+
}
|
|
1274
|
+
projectionState.interactionOutcome = "expired";
|
|
1275
|
+
yield* emitTerminalizedTools();
|
|
1276
|
+
yield toProducerWireEvent(record);
|
|
1277
|
+
continue;
|
|
1278
|
+
}
|
|
1279
|
+
yield toProducerWireEvent(record);
|
|
1280
|
+
}
|
|
1281
|
+
yield* drainModelNotices();
|
|
1282
|
+
} catch (streamErr) {
|
|
1283
|
+
const diagnostic = sandboxStreamFailureDiagnostic(streamErr);
|
|
1284
|
+
log("[chat-routes] sandbox stream failed", {
|
|
1285
|
+
failureNote: diagnostic.failureNote,
|
|
1286
|
+
error: streamErr instanceof Error ? streamErr.message : String(streamErr)
|
|
1287
|
+
});
|
|
1288
|
+
if (options.errorNotice) {
|
|
1289
|
+
yield* emitErrorNotice(diagnostic.userMessage, diagnostic.code);
|
|
1290
|
+
} else if (options.includeErrorText !== false) {
|
|
1291
|
+
const errorDelta = projectionState.fullText ? `
|
|
1292
|
+
|
|
1293
|
+
---
|
|
1294
|
+
${diagnostic.userMessage}` : diagnostic.userMessage;
|
|
1295
|
+
projectionState.fullText += errorDelta;
|
|
1296
|
+
yield { type: "text", text: errorDelta };
|
|
1297
|
+
}
|
|
1298
|
+
projectionState.interactionOutcome = "expired";
|
|
1299
|
+
yield* emitTerminalizedTools();
|
|
1300
|
+
yield {
|
|
1301
|
+
type: "error",
|
|
1302
|
+
data: {
|
|
1303
|
+
message: diagnostic.userMessage,
|
|
1304
|
+
code: diagnostic.code,
|
|
1305
|
+
details: { failureNote: diagnostic.failureNote }
|
|
1306
|
+
}
|
|
1307
|
+
};
|
|
1308
|
+
} finally {
|
|
1309
|
+
reasoningClock.settle();
|
|
1310
|
+
}
|
|
1311
|
+
}
|
|
1312
|
+
return {
|
|
1313
|
+
stream: stream(),
|
|
1314
|
+
finalText: () => projectionState.fullText,
|
|
1315
|
+
terminalObserved: () => projectionState.terminalObserved,
|
|
1316
|
+
assistantParts: () => finalizePendingInteractionParts(
|
|
1317
|
+
finalizeAssistantParts(partOrder, timedParts(), projectionState.fullText),
|
|
1318
|
+
projectionState.interactionOutcome
|
|
1319
|
+
),
|
|
1320
|
+
// Mid-stream snapshot for incremental persistence: the same accumulators,
|
|
1321
|
+
// WITHOUT the two completion-time settlements (`terminalizeDanglingTool*`
|
|
1322
|
+
// and `finalizePendingInteractionParts`). A running tool and an unanswered
|
|
1323
|
+
// ask are the correct live state; settling them early would persist
|
|
1324
|
+
// phantom failures the final write then reverses.
|
|
1325
|
+
draftParts: () => draftAssistantParts(partOrder, timedParts(), projectionState.fullText),
|
|
1326
|
+
usage: () => usage,
|
|
1327
|
+
// A GETTER, not a captured value: `turn-routes` reads `producer.model` at
|
|
1328
|
+
// draft-snapshot and persist time, both of which happen after the stream
|
|
1329
|
+
// resolved which model serves. A plain property would freeze the PREFERRED
|
|
1330
|
+
// model into the row and make a downgrade unattributable — the exact
|
|
1331
|
+
// failure mode this work exists to prevent.
|
|
1332
|
+
get model() {
|
|
1333
|
+
return servedModel();
|
|
1334
|
+
},
|
|
1335
|
+
modelFailover: () => ({
|
|
1336
|
+
model: servingModel(),
|
|
1337
|
+
attempts: failover?.attempts() ?? [],
|
|
1338
|
+
usedFallback: failover?.usedFallback() ?? false
|
|
1339
|
+
}),
|
|
1340
|
+
modelAttribution: () => ({
|
|
1341
|
+
...options.model ? { requestedModel: options.model } : {},
|
|
1342
|
+
...modelState.effectiveBackend ?? {},
|
|
1343
|
+
echoReceived: modelState.effectiveBackend !== void 0
|
|
1344
|
+
})
|
|
1345
|
+
};
|
|
1346
|
+
}
|
|
1347
|
+
function createSandboxProjection(projectionState, pendingModelNotices) {
|
|
1348
|
+
const partOrder = [];
|
|
1349
|
+
const partMap = /* @__PURE__ */ new Map();
|
|
1350
|
+
const tracker = { seen: /* @__PURE__ */ new Map() };
|
|
1351
|
+
const reasoningClock = createReasoningClock();
|
|
1352
|
+
const usage = {};
|
|
1353
|
+
const announcedToolArgs = /* @__PURE__ */ new Map();
|
|
1354
|
+
const settledTools = /* @__PURE__ */ new Set();
|
|
1355
|
+
const promotedFileParts = /* @__PURE__ */ new Map();
|
|
1356
|
+
function recordPersistedPart(part, delta, keyOverride) {
|
|
1357
|
+
const persisted = normalizePersistedPart(part);
|
|
1358
|
+
if (!persisted) return;
|
|
1359
|
+
const key = keyOverride ?? getPartKey(persisted);
|
|
1360
|
+
if (!partMap.has(key)) partOrder.push(key);
|
|
1361
|
+
partMap.set(key, mergePersistedPart(partMap.get(key), persisted, delta));
|
|
1362
|
+
}
|
|
1363
|
+
function* emitTerminalizedTools() {
|
|
1364
|
+
if (projectionState.danglingToolsTerminalized) return;
|
|
1365
|
+
projectionState.danglingToolsTerminalized = true;
|
|
1366
|
+
const updates = terminalizeDanglingAssistantToolUpdates(partOrder, partMap, projectionState.fullText);
|
|
1367
|
+
for (const part of updates) {
|
|
1368
|
+
const toolId = asString(part.id);
|
|
1369
|
+
if (!toolId || settledTools.has(toolId)) continue;
|
|
1370
|
+
settledTools.add(toolId);
|
|
1371
|
+
const state = asRecord(part.state);
|
|
1372
|
+
yield {
|
|
1373
|
+
type: "tool_result",
|
|
1374
|
+
toolCallId: toolId,
|
|
1375
|
+
toolName: asString(part.tool) ?? "tool",
|
|
1376
|
+
outcome: {
|
|
1377
|
+
ok: false,
|
|
1378
|
+
...asString(state?.error) ? { message: asString(state?.error) } : {}
|
|
1379
|
+
}
|
|
1380
|
+
};
|
|
1381
|
+
}
|
|
1382
|
+
}
|
|
1383
|
+
function* emitFinalUsage() {
|
|
1384
|
+
const promptTokens = usage.inputTokens ?? 0;
|
|
1385
|
+
const completionTokens = usage.outputTokens ?? 0;
|
|
1386
|
+
if (promptTokens || completionTokens) {
|
|
1387
|
+
yield {
|
|
1388
|
+
type: "usage",
|
|
1389
|
+
usage: {
|
|
1390
|
+
promptTokens,
|
|
1391
|
+
completionTokens,
|
|
1392
|
+
...usage.reasoningTokens !== void 0 ? { reasoningTokens: usage.reasoningTokens } : {},
|
|
1393
|
+
...usage.costUsd !== void 0 ? { providerCostUsd: usage.costUsd } : {}
|
|
1394
|
+
}
|
|
1395
|
+
};
|
|
1396
|
+
}
|
|
1397
|
+
}
|
|
1398
|
+
function* drainModelNotices() {
|
|
1399
|
+
while (pendingModelNotices.length > 0) {
|
|
1400
|
+
const queued = pendingModelNotices.shift();
|
|
1401
|
+
const notice = noticePart("warning", queued.id, queued.text);
|
|
1402
|
+
recordPersistedPart(notice, void 0, noticePartKey(notice.id));
|
|
1403
|
+
yield { type: "notice", id: notice.id, noticeKind: "warning", text: queued.text };
|
|
1404
|
+
}
|
|
1405
|
+
}
|
|
1406
|
+
function* emitErrorNotice(text, code) {
|
|
1407
|
+
projectionState.errorNoticeCount += 1;
|
|
1408
|
+
const notice = noticePart("error", `error-${projectionState.errorNoticeCount}`, text, code);
|
|
1409
|
+
recordPersistedPart(notice, void 0, noticePartKey(notice.id));
|
|
1410
|
+
yield { type: "notice", id: notice.id, noticeKind: "error", text, code };
|
|
1411
|
+
}
|
|
1412
|
+
const timedParts = () => {
|
|
1413
|
+
const timed = new Map(partMap);
|
|
1414
|
+
for (const [key, part] of partMap) {
|
|
1415
|
+
if (part.type !== "reasoning") continue;
|
|
1416
|
+
const time = reasoningClock.time(key, asRecord(part.time));
|
|
1417
|
+
if (time) timed.set(key, { ...part, time });
|
|
1418
|
+
}
|
|
1419
|
+
return timed;
|
|
1420
|
+
};
|
|
1421
|
+
return { timedParts, drainModelNotices, tracker, recordPersistedPart, reasoningClock, partMap, announcedToolArgs, settledTools, usage, promotedFileParts, emitFinalUsage, emitTerminalizedTools, emitErrorNotice, partOrder };
|
|
1422
|
+
}
|
|
1423
|
+
async function* projectSandboxMessagePart(event, tracker, recordPersistedPart, reasoningClock, projectionState, partMap, announcedToolArgs, settledTools, usage, options, log, promotedFileParts) {
|
|
1424
|
+
if (event.type === "message.part.updated") {
|
|
1425
|
+
const part = asRecord(event.data?.part);
|
|
1426
|
+
if (!part) return;
|
|
1427
|
+
const rawDelta = event.data?.delta;
|
|
1428
|
+
const partType = String(part.type ?? "");
|
|
1429
|
+
if (partType === "text" || partType === "reasoning") {
|
|
1430
|
+
const key = getPartKey(part);
|
|
1431
|
+
const delta = textDelta(tracker, key, part, rawDelta);
|
|
1432
|
+
recordPersistedPart(part, delta || void 0);
|
|
1433
|
+
if (delta) {
|
|
1434
|
+
if (partType === "reasoning") reasoningClock.reasoning(key);
|
|
1435
|
+
else reasoningClock.settle();
|
|
1436
|
+
if (partType === "text") projectionState.fullText += delta;
|
|
1437
|
+
yield { type: partType, text: delta };
|
|
1438
|
+
}
|
|
1439
|
+
return;
|
|
1440
|
+
}
|
|
1441
|
+
if (partType === "tool") {
|
|
1442
|
+
reasoningClock.settle();
|
|
1443
|
+
recordPersistedPart(part, void 0);
|
|
1444
|
+
const persisted = partMap.get(getPartKey(part));
|
|
1445
|
+
const state = asRecord(persisted?.state);
|
|
1446
|
+
const toolId = String(persisted?.id ?? "");
|
|
1447
|
+
const toolName = String(persisted?.tool ?? "tool");
|
|
1448
|
+
const args = asRecord(state?.input) ?? {};
|
|
1449
|
+
const hasPopulatedArgs = Object.keys(args).length > 0;
|
|
1450
|
+
const announcedWithPopulatedArgs = announcedToolArgs.get(toolId);
|
|
1451
|
+
if (toolId && (announcedWithPopulatedArgs === void 0 || !announcedWithPopulatedArgs && hasPopulatedArgs && !settledTools.has(toolId))) {
|
|
1452
|
+
announcedToolArgs.set(toolId, hasPopulatedArgs);
|
|
1453
|
+
yield {
|
|
1454
|
+
type: "tool_call",
|
|
1455
|
+
call: { toolCallId: toolId, toolName, args }
|
|
1456
|
+
};
|
|
1457
|
+
}
|
|
1458
|
+
const status = String(state?.status ?? "");
|
|
1459
|
+
if (toolId && (status === "completed" || status === "error") && !settledTools.has(toolId)) {
|
|
1460
|
+
settledTools.add(toolId);
|
|
1461
|
+
yield {
|
|
1462
|
+
type: "tool_result",
|
|
1463
|
+
toolCallId: toolId,
|
|
1464
|
+
toolName,
|
|
1465
|
+
outcome: {
|
|
1466
|
+
ok: status === "completed",
|
|
1467
|
+
...state?.output !== void 0 ? { result: state.output } : {},
|
|
1468
|
+
...asString(state?.error) ? { message: asString(state?.error) } : {}
|
|
1469
|
+
}
|
|
1470
|
+
};
|
|
1471
|
+
}
|
|
1472
|
+
return;
|
|
1473
|
+
}
|
|
1474
|
+
if (partType === "step-finish") {
|
|
1475
|
+
addStepFinishUsage(part, usage);
|
|
1476
|
+
recordPersistedPart(part, void 0, `step-finish:#${projectionState.stepCounter++}`);
|
|
1477
|
+
const promptTokens = usage.inputTokens ?? 0;
|
|
1478
|
+
const completionTokens = usage.outputTokens ?? 0;
|
|
1479
|
+
if (promptTokens || completionTokens) {
|
|
1480
|
+
yield {
|
|
1481
|
+
type: "usage",
|
|
1482
|
+
usage: {
|
|
1483
|
+
promptTokens,
|
|
1484
|
+
completionTokens,
|
|
1485
|
+
...usage.reasoningTokens !== void 0 ? { reasoningTokens: usage.reasoningTokens } : {},
|
|
1486
|
+
...usage.costUsd !== void 0 ? { providerCostUsd: usage.costUsd } : {}
|
|
1487
|
+
}
|
|
1488
|
+
};
|
|
1489
|
+
}
|
|
1490
|
+
return;
|
|
1491
|
+
}
|
|
1492
|
+
if (partType === "step-start") {
|
|
1493
|
+
recordPersistedPart(part, void 0, `step-start:#${projectionState.stepCounter}`);
|
|
1494
|
+
return;
|
|
1495
|
+
}
|
|
1496
|
+
if (partType === "file" && options.promoteFilePart) {
|
|
1497
|
+
const promote = options.promoteFilePart;
|
|
1498
|
+
const rawId = asString(part.id);
|
|
1499
|
+
const rawUrl = asString(part.url);
|
|
1500
|
+
const memoKey = rawId ? `id:${rawId}` : rawUrl ? `url:${rawUrl}` : void 0;
|
|
1501
|
+
const attempt = () => Promise.resolve().then(() => promote(part)).catch((err) => {
|
|
1502
|
+
const reason = err instanceof Error ? err.message : String(err);
|
|
1503
|
+
log("[chat-routes] file part promotion threw", { key: memoKey ?? "(keyless)", error: reason });
|
|
1504
|
+
return { succeeded: false, reason };
|
|
1505
|
+
});
|
|
1506
|
+
let pending;
|
|
1507
|
+
if (memoKey) {
|
|
1508
|
+
pending = promotedFileParts.get(memoKey) ?? attempt();
|
|
1509
|
+
promotedFileParts.set(memoKey, pending);
|
|
1510
|
+
} else {
|
|
1511
|
+
pending = attempt();
|
|
1512
|
+
}
|
|
1513
|
+
const outcome = await pending;
|
|
1514
|
+
if (outcome.succeeded) {
|
|
1515
|
+
recordPersistedPart(outcome.part, void 0, outcome.key);
|
|
1516
|
+
} else if (outcome.part) {
|
|
1517
|
+
recordPersistedPart(outcome.part, void 0, outcome.key);
|
|
1518
|
+
} else {
|
|
1519
|
+
recordPersistedPart(part, void 0);
|
|
1520
|
+
}
|
|
1521
|
+
return;
|
|
1522
|
+
}
|
|
1523
|
+
recordPersistedPart(part, void 0);
|
|
1524
|
+
return;
|
|
1525
|
+
}
|
|
1526
|
+
}
|
|
1527
|
+
|
|
1528
|
+
// src/chat-routes/cancellation.ts
|
|
1529
|
+
var targetChanged = () => Response.json({
|
|
1530
|
+
error: "This Stop no longer matches the current turn attempt. Refresh its current state.",
|
|
1531
|
+
code: "CANCEL_TARGET_CHANGED"
|
|
1532
|
+
}, { status: 409 });
|
|
1533
|
+
var stateChanged = () => Response.json({
|
|
1534
|
+
error: "This turn changed while Stop was pending. Refresh before trying Stop again.",
|
|
1535
|
+
code: "CANCEL_STATE_CHANGED"
|
|
1536
|
+
}, { status: 409 });
|
|
1537
|
+
var success = (sandboxInterrupted) => Response.json({ succeeded: true, sandboxInterrupted });
|
|
1538
|
+
async function cancelChatTurn(request, options) {
|
|
1539
|
+
if (!options.cancel || request.method !== "POST") {
|
|
1540
|
+
return Response.json({ error: "Method not allowed" }, { status: 405 });
|
|
1541
|
+
}
|
|
1542
|
+
const [raw, invalid] = await parseJsonObjectBody(request, { maxBytes: INLINE_PARTS_MAX_BYTES });
|
|
1543
|
+
if (invalid) return invalid;
|
|
1544
|
+
const threadId = typeof raw.threadId === "string" ? raw.threadId.trim() : "";
|
|
1545
|
+
const turnId = typeof raw.turnId === "string" ? raw.turnId.trim() : "";
|
|
1546
|
+
if (!threadId || !turnId) {
|
|
1547
|
+
return Response.json({ error: "Missing workspaceId, threadId, or turnId" }, { status: 400 });
|
|
1548
|
+
}
|
|
1549
|
+
const body = {
|
|
1550
|
+
...raw,
|
|
1551
|
+
threadId,
|
|
1552
|
+
turnId,
|
|
1553
|
+
cancellationToken: typeof raw.cancellationToken === "string" ? raw.cancellationToken : void 0
|
|
1554
|
+
};
|
|
1555
|
+
const log = options.log ?? ((message, meta) => console.error(message, meta ?? {}));
|
|
1556
|
+
try {
|
|
1557
|
+
const auth = await options.authorize({ request, intent: "cancel", body, threadId });
|
|
1558
|
+
if (!auth.ok) return auth.response;
|
|
1559
|
+
if (auth.intent !== void 0 && auth.intent !== "cancel") {
|
|
1560
|
+
return Response.json({ error: "Authorization did not allow this turn cancellation.", code: "chat_authorization_intent_mismatch" }, { status: 403 });
|
|
1561
|
+
}
|
|
1562
|
+
const target = await options.cancel.target({
|
|
1563
|
+
request,
|
|
1564
|
+
body,
|
|
1565
|
+
tenantId: auth.tenantId,
|
|
1566
|
+
userId: auth.userId,
|
|
1567
|
+
..."context" in auth ? { context: auth.context } : {}
|
|
1568
|
+
});
|
|
1569
|
+
if (target instanceof Response) return target;
|
|
1570
|
+
if (target.requiresDispatchToken && (!body.cancellationToken || body.cancellationToken !== target.dispatchToken)) {
|
|
1571
|
+
return targetChanged();
|
|
1572
|
+
}
|
|
1573
|
+
if (target.status === "stopped" || target.status === "answered") return success(false);
|
|
1574
|
+
const markStopped = async () => await target.markStopped(target.capturedMetadata, (/* @__PURE__ */ new Date()).toISOString()) ? null : stateChanged();
|
|
1575
|
+
if (!target.native) return await markStopped() ?? success(false);
|
|
1576
|
+
const native = target.native;
|
|
1577
|
+
if (native.state !== "running" || !native.executionId || !native.sessionId || !native.sandboxId) {
|
|
1578
|
+
return Response.json({
|
|
1579
|
+
error: "This turn has no confirmed native execution to stop. Refresh and try again.",
|
|
1580
|
+
code: "NATIVE_CANCEL_TARGET_UNAVAILABLE"
|
|
1581
|
+
}, { status: 409 });
|
|
1582
|
+
}
|
|
1583
|
+
const source = await native.source();
|
|
1584
|
+
if (!source || source.id !== native.sandboxId) {
|
|
1585
|
+
return Response.json({ error: "The turn was not marked stopped because its original sandbox is unavailable." }, { status: 503 });
|
|
1586
|
+
}
|
|
1587
|
+
try {
|
|
1588
|
+
const session = source.session(native.sessionId);
|
|
1589
|
+
const interrupted = await session.interrupt({ executionId: native.executionId });
|
|
1590
|
+
if (!interrupted.cancelled) {
|
|
1591
|
+
const current = await session.status();
|
|
1592
|
+
const anotherExecution = typeof current?.activeExecutionId === "string" && current.activeExecutionId.length > 0 && current.activeExecutionId !== native.executionId;
|
|
1593
|
+
const terminal = current && ["completed", "failed", "cancelled"].includes(current.status ?? "");
|
|
1594
|
+
if (!current || current.id !== native.sessionId || current.activeExecutionId === native.executionId || !anotherExecution && !terminal) {
|
|
1595
|
+
throw new Error("The targeted native execution has not been confirmed stopped");
|
|
1596
|
+
}
|
|
1597
|
+
}
|
|
1598
|
+
return await markStopped() ?? success(interrupted.cancelled);
|
|
1599
|
+
} catch (error) {
|
|
1600
|
+
log("[chat-routes] native cancellation failed", { error: redactErrorMessage(error) });
|
|
1601
|
+
return Response.json({ error: "The turn was not marked stopped because its native execution could not be confirmed interrupted." }, { status: 502 });
|
|
1602
|
+
}
|
|
1603
|
+
} catch (error) {
|
|
1604
|
+
if (error instanceof Response) return error;
|
|
1605
|
+
log("[chat-routes] cancellation failed", { error: redactErrorMessage(error) });
|
|
1606
|
+
return Response.json({ error: "Turn cancellation is temporarily unavailable." }, { status: 503 });
|
|
1607
|
+
}
|
|
1608
|
+
}
|
|
1609
|
+
async function* observeTurnCancellation(source, args, cancel, abort) {
|
|
1610
|
+
if (!args.userMessageId) {
|
|
1611
|
+
yield* source;
|
|
1612
|
+
return;
|
|
1613
|
+
}
|
|
1614
|
+
const iterator = source[Symbol.asyncIterator]();
|
|
1615
|
+
let closed = false, stopped = false, drained = false, warned = false;
|
|
1616
|
+
let polling;
|
|
1617
|
+
let notifyStop;
|
|
1618
|
+
const stop = new Promise((resolve) => {
|
|
1619
|
+
notifyStop = () => {
|
|
1620
|
+
resolve({ done: true, value: void 0 });
|
|
1621
|
+
};
|
|
1622
|
+
});
|
|
1623
|
+
const body = {
|
|
1624
|
+
...args.body,
|
|
1625
|
+
threadId: args.identity.sessionId,
|
|
1626
|
+
turnId: typeof args.body.turnId === "string" ? args.body.turnId : "",
|
|
1627
|
+
cancellationToken: typeof args.body.cancellationToken === "string" ? args.body.cancellationToken : void 0
|
|
1628
|
+
};
|
|
1629
|
+
const readTarget = async () => {
|
|
1630
|
+
if (closed) return true;
|
|
1631
|
+
try {
|
|
1632
|
+
const target = await cancel.target({
|
|
1633
|
+
request: args.request,
|
|
1634
|
+
body,
|
|
1635
|
+
tenantId: args.identity.tenantId,
|
|
1636
|
+
userId: args.identity.userId,
|
|
1637
|
+
context: args.context,
|
|
1638
|
+
messageId: args.userMessageId
|
|
1639
|
+
});
|
|
1640
|
+
if (closed) return true;
|
|
1641
|
+
if (target instanceof Response) return false;
|
|
1642
|
+
if (target.status === "stopped" || target.status === "superseded") {
|
|
1643
|
+
stopped = true;
|
|
1644
|
+
abort.abort(new Error("The captured turn was stopped or superseded."));
|
|
1645
|
+
notifyStop();
|
|
1646
|
+
} else if (target.status === "answered") clearInterval(timer);
|
|
1647
|
+
return true;
|
|
1648
|
+
} catch (error) {
|
|
1649
|
+
if (!warned) {
|
|
1650
|
+
warned = true;
|
|
1651
|
+
console.error("[chat-routes] stop observation unavailable", { error: redactErrorMessage(error) });
|
|
1652
|
+
}
|
|
1653
|
+
return false;
|
|
1654
|
+
}
|
|
1655
|
+
};
|
|
1656
|
+
const poll = () => polling ??= readTarget().finally(() => {
|
|
1657
|
+
polling = void 0;
|
|
1658
|
+
});
|
|
1659
|
+
const timer = setInterval(() => {
|
|
1660
|
+
void poll();
|
|
1661
|
+
}, 1e3);
|
|
1662
|
+
try {
|
|
1663
|
+
let pending = iterator.next();
|
|
1664
|
+
void pending.catch(() => {
|
|
1665
|
+
});
|
|
1666
|
+
void poll();
|
|
1667
|
+
while (!stopped) {
|
|
1668
|
+
let next;
|
|
1669
|
+
try {
|
|
1670
|
+
next = await Promise.race([pending, stop]);
|
|
1671
|
+
} catch (error) {
|
|
1672
|
+
if (stopped) return;
|
|
1673
|
+
throw error;
|
|
1674
|
+
}
|
|
1675
|
+
if (stopped) return;
|
|
1676
|
+
if (next.done) {
|
|
1677
|
+
await polling;
|
|
1678
|
+
if (!await poll()) throw new Error("The captured turn state could not be confirmed at completion");
|
|
1679
|
+
if (stopped) return;
|
|
1680
|
+
drained = true;
|
|
1681
|
+
return;
|
|
1682
|
+
}
|
|
1683
|
+
yield next.value;
|
|
1684
|
+
pending = iterator.next();
|
|
1685
|
+
void pending.catch(() => {
|
|
1686
|
+
});
|
|
1687
|
+
}
|
|
1688
|
+
} finally {
|
|
1689
|
+
closed = true;
|
|
1690
|
+
clearInterval(timer);
|
|
1691
|
+
if (!drained) {
|
|
1692
|
+
const cleanup = iterator.return?.().catch(() => {
|
|
1693
|
+
});
|
|
1694
|
+
if (cleanup) args.waitUntil?.(cleanup);
|
|
1695
|
+
}
|
|
1696
|
+
}
|
|
1697
|
+
}
|
|
1698
|
+
|
|
1699
|
+
// src/chat-routes/turn-route-contracts.ts
|
|
1700
|
+
var INTENT_LINE_TIMEOUT_MS = 1500;
|
|
1701
|
+
var INTENT_LINE_HISTORY_MESSAGES = 6;
|
|
1702
|
+
var INTENT_LINE_MAX_CHARS = 240;
|
|
1703
|
+
|
|
1704
|
+
// src/chat-routes/turn-prefetch.ts
|
|
1705
|
+
function startTurnPrefetch(prefetch, input) {
|
|
1706
|
+
let started;
|
|
1707
|
+
const start = (identity) => {
|
|
1708
|
+
const abort = new AbortController();
|
|
1709
|
+
const result = input.trace.measure("admission.prefetch", "admission", async () => prefetch({
|
|
1710
|
+
...input,
|
|
1711
|
+
tenantId: identity.tenantId,
|
|
1712
|
+
userId: identity.userId,
|
|
1713
|
+
signal: abort.signal
|
|
1714
|
+
}));
|
|
1715
|
+
result.catch(() => {
|
|
1716
|
+
});
|
|
1717
|
+
started = { identity: { ...identity }, abort, result };
|
|
1718
|
+
};
|
|
1719
|
+
return {
|
|
1720
|
+
identified(identity) {
|
|
1721
|
+
if (!started) start(identity);
|
|
1722
|
+
},
|
|
1723
|
+
admitted(identity) {
|
|
1724
|
+
if (started && (started.identity.tenantId !== identity.tenantId || started.identity.userId !== identity.userId)) {
|
|
1725
|
+
started.abort.abort();
|
|
1726
|
+
started = void 0;
|
|
1727
|
+
}
|
|
1728
|
+
if (!started) start(identity);
|
|
1729
|
+
return started.result;
|
|
1730
|
+
},
|
|
1731
|
+
discard() {
|
|
1732
|
+
started?.abort.abort();
|
|
1733
|
+
started = void 0;
|
|
1734
|
+
}
|
|
1735
|
+
};
|
|
1736
|
+
}
|
|
1737
|
+
|
|
1738
|
+
// src/chat-routes/turn-admission.ts
|
|
1739
|
+
function errorResponse(err) {
|
|
1740
|
+
return Response.json({ code: err.code, error: err.message }, { status: err.status });
|
|
1741
|
+
}
|
|
1742
|
+
function admissionServerTiming(trace) {
|
|
1743
|
+
const now = Date.now();
|
|
1744
|
+
const steps = trace.spans(now).filter((span) => span.kind === "admission").sort((a, b) => a.startMs - b.startMs).map((span) => ({ name: span.name, durMs: span.endMs - span.startMs }));
|
|
1745
|
+
return formatServerTiming([{ name: "admission", durMs: now - trace.startedAt }, ...steps]);
|
|
1746
|
+
}
|
|
1747
|
+
async function namedModelRefusal(policy, payload, trace) {
|
|
1748
|
+
const namedModel = typeof payload.model === "string" ? payload.model.trim() : "";
|
|
1749
|
+
if (!policy || !namedModel) return null;
|
|
1750
|
+
const decision = await trace.measure("admission.model_policy", "admission", () => checkModelPolicy(namedModel, policy.catalog, policy.filter, policy.context));
|
|
1751
|
+
return decision.allowed ? null : Response.json({ code: "model_policy_denied", error: decision.reason }, { status: 403 });
|
|
1752
|
+
}
|
|
1753
|
+
function validateTurnBody(body) {
|
|
1754
|
+
const threadId = typeof body.threadId === "string" ? body.threadId.trim() : "";
|
|
1755
|
+
if (!threadId) throw new ChatTurnInputError("Missing threadId");
|
|
1756
|
+
const rawContent = body.content ?? body.message ?? "";
|
|
1757
|
+
if (typeof rawContent !== "string") throw new ChatTurnInputError("content must be a string");
|
|
1758
|
+
const content = rawContent.trim();
|
|
1759
|
+
const fileParts = parseChatTurnParts(body.parts);
|
|
1760
|
+
const mentions = parseFileMentions(body.mentions);
|
|
1761
|
+
const hasAttachments = Array.isArray(body.attachments) && body.attachments.length > 0;
|
|
1762
|
+
if (!content && fileParts.length === 0 && mentions.length === 0 && !hasAttachments) {
|
|
1763
|
+
throw new ChatTurnInputError("Missing content (send text, parts, mentions, attachments, or any combination)");
|
|
1764
|
+
}
|
|
1765
|
+
assertPromptPartsWithinCap(fileParts);
|
|
1766
|
+
let turnId;
|
|
1767
|
+
try {
|
|
1768
|
+
turnId = normalizeClientTurnId(body.turnId);
|
|
1769
|
+
} catch (err) {
|
|
1770
|
+
throw new ChatTurnInputError(err instanceof Error ? err.message : "Invalid turnId");
|
|
1771
|
+
}
|
|
1772
|
+
const retryAttemptId = body.retryAttemptId;
|
|
1773
|
+
if (retryAttemptId !== void 0 && (typeof retryAttemptId !== "string" || !/^[A-Za-z0-9_-]{1,64}$/.test(retryAttemptId) || !turnId)) throw new ChatTurnInputError("retryAttemptId requires turnId and 1\u201364 letters, digits, underscores, or hyphens");
|
|
1774
|
+
return {
|
|
1775
|
+
// The VALIDATED, deduped mention list replaces the raw one on the payload,
|
|
1776
|
+
// so every downstream seam (`authorize`, `contextGate`, `beforeTurn`,
|
|
1777
|
+
// `produce`) reads checked paths and never the request's own.
|
|
1778
|
+
payload: { ...body, threadId, content, mentions },
|
|
1779
|
+
content,
|
|
1780
|
+
fileParts,
|
|
1781
|
+
mentions,
|
|
1782
|
+
turnId
|
|
1783
|
+
};
|
|
1784
|
+
}
|
|
1785
|
+
function userPartsWithFiles(userParts, fileParts, mentions) {
|
|
1786
|
+
return toChatMessageParts([
|
|
1787
|
+
...userParts,
|
|
1788
|
+
...fileParts.map((part) => ({ ...part })),
|
|
1789
|
+
...mentions.map((mention) => ({ ...mentionInputToPart(mention) }))
|
|
1790
|
+
]);
|
|
1791
|
+
}
|
|
1792
|
+
|
|
1793
|
+
// src/chat-routes/turn-execution.ts
|
|
1794
|
+
import { deriveExecutionId, handleChatTurn } from "@tangle-network/agent-runtime/durable";
|
|
1795
|
+
|
|
1796
|
+
// src/chat-routes/native-turn-timing.ts
|
|
1797
|
+
function createNativeTimingTransfer(read, onError) {
|
|
1798
|
+
let persist;
|
|
1799
|
+
let pending = Promise.resolve();
|
|
1800
|
+
return {
|
|
1801
|
+
snapshot: () => ({ ...read() }),
|
|
1802
|
+
async register(options) {
|
|
1803
|
+
if (!options?.persistTimingFacts) return;
|
|
1804
|
+
await options.persistTimingFacts({ ...read() });
|
|
1805
|
+
persist = options.persistTimingFacts;
|
|
1806
|
+
},
|
|
1807
|
+
note() {
|
|
1808
|
+
if (!persist) return pending;
|
|
1809
|
+
const snapshot = { ...read() };
|
|
1810
|
+
const write = persist;
|
|
1811
|
+
pending = pending.then(() => write(snapshot)).catch(onError);
|
|
1812
|
+
return pending;
|
|
1813
|
+
},
|
|
1814
|
+
flush: () => pending
|
|
1815
|
+
};
|
|
1816
|
+
}
|
|
1817
|
+
|
|
1818
|
+
// src/chat-routes/turn-ownership.ts
|
|
1819
|
+
function createChatTurnOwnership(state, log, turnStreamId, options) {
|
|
1820
|
+
const handoffCompletion = (handoff) => {
|
|
1821
|
+
if (!state.completionHandoffPromise) {
|
|
1822
|
+
state.completionHandoffPromise = (async () => {
|
|
1823
|
+
state.completionHandoff = true;
|
|
1824
|
+
let registrationFailed = false;
|
|
1825
|
+
try {
|
|
1826
|
+
await state.nativeTiming?.register(handoff);
|
|
1827
|
+
} catch (error) {
|
|
1828
|
+
registrationFailed = true;
|
|
1829
|
+
throw error;
|
|
1830
|
+
} finally {
|
|
1831
|
+
try {
|
|
1832
|
+
await state.tap?.detach();
|
|
1833
|
+
} catch (err) {
|
|
1834
|
+
log("[chat-routes] turn buffer detach failed", {
|
|
1835
|
+
turnId: turnStreamId,
|
|
1836
|
+
error: err instanceof Error ? err.message : String(err)
|
|
1837
|
+
});
|
|
1838
|
+
}
|
|
1839
|
+
try {
|
|
1840
|
+
await state.draft?.close();
|
|
1841
|
+
} catch (err) {
|
|
1842
|
+
if (!registrationFailed) throw err;
|
|
1843
|
+
log("[chat-routes] draft close failed after native timing refusal", {
|
|
1844
|
+
turnId: turnStreamId,
|
|
1845
|
+
error: err instanceof Error ? err.message : String(err)
|
|
1846
|
+
});
|
|
1847
|
+
}
|
|
1848
|
+
}
|
|
1849
|
+
})();
|
|
1850
|
+
}
|
|
1851
|
+
return state.completionHandoffPromise;
|
|
1852
|
+
};
|
|
1853
|
+
const releaseLock = async () => {
|
|
1854
|
+
if (state.completionHandoff || !state.lockAcquired || state.lockReleased) return;
|
|
1855
|
+
state.lockReleased = true;
|
|
1856
|
+
try {
|
|
1857
|
+
await options.turnLock.release(state.lockHandle);
|
|
1858
|
+
} catch (err) {
|
|
1859
|
+
log("[chat-routes] turnLock.release failed", {
|
|
1860
|
+
turnId: turnStreamId,
|
|
1861
|
+
error: err instanceof Error ? err.message : String(err)
|
|
1862
|
+
});
|
|
1863
|
+
}
|
|
1864
|
+
};
|
|
1865
|
+
return { handoffCompletion, releaseLock };
|
|
1866
|
+
}
|
|
1867
|
+
|
|
1868
|
+
// src/chat-routes/route-producer.ts
|
|
1869
|
+
function isProducer(value) {
|
|
1870
|
+
return "stream" in value && typeof value.stream === "object" && value.stream !== null && Symbol.asyncIterator in value.stream;
|
|
1871
|
+
}
|
|
1872
|
+
function routerRouteProducer(value, args) {
|
|
1873
|
+
if (isProducer(value)) return value;
|
|
1874
|
+
const options = {
|
|
1875
|
+
...value,
|
|
1876
|
+
userMessage: value.userMessage ?? turnPromptText(args.prompt),
|
|
1877
|
+
history: value.history ?? routerTurnHistory(args.priorMessages),
|
|
1878
|
+
trace: args.trace,
|
|
1879
|
+
traceId: args.trace?.traceId,
|
|
1880
|
+
signal: args.admissionSignal,
|
|
1881
|
+
// Domain fences may wait for a certified profile or per-turn resource.
|
|
1882
|
+
// They can only add a requirement; they never replace route admission.
|
|
1883
|
+
admitted: Promise.all([args.admitted, value.admitted])
|
|
1884
|
+
};
|
|
1885
|
+
return "box" in value ? createHybridTurnProducer({ ...options, box: value.box }) : createRouterTurnProducer(options);
|
|
1886
|
+
}
|
|
1887
|
+
function sandboxRouteProducer(value, args) {
|
|
1888
|
+
if (isProducer(value)) return value;
|
|
1889
|
+
const openEvents = value.openEvents;
|
|
1890
|
+
return createSandboxChatProducer({ ...value, ...openEvents && args.admissionSignal ? {
|
|
1891
|
+
openEvents: (input) => openEvents({ ...input, signal: AbortSignal.any([input.signal, args.admissionSignal]) })
|
|
1892
|
+
} : {} });
|
|
1893
|
+
}
|
|
1894
|
+
|
|
1895
|
+
// src/chat-routes/producer-stream.ts
|
|
1896
|
+
function withProducerStream(producer, stream) {
|
|
1897
|
+
return new Proxy(Object.create(producer), {
|
|
1898
|
+
get(_target, property) {
|
|
1899
|
+
if (property === "stream") return stream;
|
|
1900
|
+
const value = Reflect.get(producer, property, producer);
|
|
1901
|
+
return typeof value === "function" ? value.bind(producer) : value;
|
|
1902
|
+
}
|
|
1903
|
+
});
|
|
1904
|
+
}
|
|
1905
|
+
|
|
1906
|
+
// src/chat-routes/dispatch-first.ts
|
|
1907
|
+
var TurnAdmissionRefusedError = class extends Error {
|
|
1908
|
+
constructor() {
|
|
1909
|
+
super("The turn was refused after its producer started; its output was discarded.");
|
|
1910
|
+
this.name = "TurnAdmissionRefusedError";
|
|
1911
|
+
}
|
|
1912
|
+
};
|
|
1913
|
+
function dispatchProducerFirst(produce, args, trace) {
|
|
1914
|
+
const abort = new AbortController();
|
|
1915
|
+
let admit;
|
|
1916
|
+
let reject;
|
|
1917
|
+
const admitted = new Promise((resolve, fail) => {
|
|
1918
|
+
admit = resolve;
|
|
1919
|
+
reject = fail;
|
|
1920
|
+
});
|
|
1921
|
+
admitted.catch(() => {
|
|
1922
|
+
});
|
|
1923
|
+
let settled;
|
|
1924
|
+
let stopped = false;
|
|
1925
|
+
const queue = [];
|
|
1926
|
+
let ended = false;
|
|
1927
|
+
let failure;
|
|
1928
|
+
let wake;
|
|
1929
|
+
const notify = () => {
|
|
1930
|
+
const resume = wake;
|
|
1931
|
+
wake = void 0;
|
|
1932
|
+
resume?.();
|
|
1933
|
+
};
|
|
1934
|
+
let source;
|
|
1935
|
+
const handoffCompletion = args.handoffCompletion;
|
|
1936
|
+
const started = trace.measure("admission.produce", "admission", () => produce({
|
|
1937
|
+
...args,
|
|
1938
|
+
admitted,
|
|
1939
|
+
admissionSignal: args.admissionSignal ? AbortSignal.any([args.admissionSignal, abort.signal]) : abort.signal,
|
|
1940
|
+
...handoffCompletion ? { handoffCompletion: (options) => admitted.then(() => handoffCompletion(options)) } : {}
|
|
1941
|
+
}));
|
|
1942
|
+
const cleanup = started.then((producer) => producer.cleanup, () => void 0);
|
|
1943
|
+
args.waitUntil?.(cleanup);
|
|
1944
|
+
void cleanup.catch(() => {
|
|
1945
|
+
});
|
|
1946
|
+
const draining = started.then(async (producer) => {
|
|
1947
|
+
try {
|
|
1948
|
+
source = producer.stream[Symbol.asyncIterator]();
|
|
1949
|
+
for (; ; ) {
|
|
1950
|
+
const next = await source.next();
|
|
1951
|
+
if (next.done || stopped) break;
|
|
1952
|
+
queue.push(next.value);
|
|
1953
|
+
notify();
|
|
1954
|
+
}
|
|
1955
|
+
} catch (error) {
|
|
1956
|
+
if (!stopped) failure = { error };
|
|
1957
|
+
} finally {
|
|
1958
|
+
ended = true;
|
|
1959
|
+
notify();
|
|
1960
|
+
}
|
|
1961
|
+
}, (error) => {
|
|
1962
|
+
failure = { error };
|
|
1963
|
+
ended = true;
|
|
1964
|
+
notify();
|
|
1965
|
+
});
|
|
1966
|
+
const stop = (reason) => {
|
|
1967
|
+
if (stopped) return;
|
|
1968
|
+
stopped = true;
|
|
1969
|
+
abort.abort(reason);
|
|
1970
|
+
queue.length = 0;
|
|
1971
|
+
notify();
|
|
1972
|
+
void started.then(() => source?.return?.(), () => {
|
|
1973
|
+
}).catch(() => {
|
|
1974
|
+
});
|
|
1975
|
+
};
|
|
1976
|
+
async function* replay() {
|
|
1977
|
+
try {
|
|
1978
|
+
for (; ; ) {
|
|
1979
|
+
while (queue.length) yield queue.shift();
|
|
1980
|
+
if (ended) break;
|
|
1981
|
+
await new Promise((resolve) => {
|
|
1982
|
+
wake = resolve;
|
|
1983
|
+
});
|
|
1984
|
+
}
|
|
1985
|
+
await draining;
|
|
1986
|
+
if (failure) throw failure.error;
|
|
1987
|
+
} finally {
|
|
1988
|
+
if (!ended) stop(new Error("The turn stopped reading its producer."));
|
|
1989
|
+
}
|
|
1990
|
+
}
|
|
1991
|
+
return {
|
|
1992
|
+
async take(final) {
|
|
1993
|
+
if (settled) throw new Error(`dispatch-first producer already ${settled}`);
|
|
1994
|
+
settled = "taken";
|
|
1995
|
+
admit(final);
|
|
1996
|
+
const producer = await started;
|
|
1997
|
+
return withProducerStream(producer, replay());
|
|
1998
|
+
},
|
|
1999
|
+
refuse() {
|
|
2000
|
+
if (settled) return;
|
|
2001
|
+
settled = "refused";
|
|
2002
|
+
reject(new TurnAdmissionRefusedError());
|
|
2003
|
+
stop(new TurnAdmissionRefusedError());
|
|
2004
|
+
}
|
|
2005
|
+
};
|
|
2006
|
+
}
|
|
2007
|
+
function selectTurnProducer(produce, args) {
|
|
2008
|
+
if (typeof produce === "function") return { produce, router: false };
|
|
2009
|
+
if (produce.when?.(args) ?? true) return {
|
|
2010
|
+
produce: async (final) => routerRouteProducer(await produce.router(final), final),
|
|
2011
|
+
router: true
|
|
2012
|
+
};
|
|
2013
|
+
if (!produce.sandbox) throw new Error("The turn selected the Sandbox backend without a Sandbox producer");
|
|
2014
|
+
const sandbox = produce.sandbox;
|
|
2015
|
+
return { produce: async (final) => sandboxRouteProducer(await sandbox(final), final), router: false };
|
|
2016
|
+
}
|
|
2017
|
+
function dispatchFirstFor(selected, args, trace) {
|
|
2018
|
+
return selected.router && !args.userMessageId ? dispatchProducerFirst(selected.produce, args, trace) : void 0;
|
|
2019
|
+
}
|
|
2020
|
+
|
|
2021
|
+
// src/chat-routes/turn-event-stream.ts
|
|
2022
|
+
function failureReasonOf(data) {
|
|
2023
|
+
if (!data) return void 0;
|
|
2024
|
+
const message = data.message ?? data.error ?? data.reason;
|
|
2025
|
+
if (typeof message === "string" && message.length > 0) return message;
|
|
2026
|
+
return void 0;
|
|
2027
|
+
}
|
|
2028
|
+
function failureCodeOf(data) {
|
|
2029
|
+
const code = data?.code;
|
|
2030
|
+
return typeof code === "string" && code.length > 0 ? code : void 0;
|
|
2031
|
+
}
|
|
2032
|
+
function isVisibleText(event) {
|
|
2033
|
+
const value = event;
|
|
2034
|
+
if (event.type === "text") return typeof value.text === "string" ? value.text.trim().length > 0 : true;
|
|
2035
|
+
if (event.type !== "message.part.updated") return false;
|
|
2036
|
+
const part = event.data?.part;
|
|
2037
|
+
const delta = event.data?.delta;
|
|
2038
|
+
return part?.type === "text" && typeof delta === "string" && delta.trim().length > 0;
|
|
2039
|
+
}
|
|
2040
|
+
function isVisibleOutput(event) {
|
|
2041
|
+
if (isVisibleText(event)) return true;
|
|
2042
|
+
const value = event;
|
|
2043
|
+
if (event.type === "reasoning") return typeof value.text === "string" && value.text.trim().length > 0;
|
|
2044
|
+
if (event.type === "tool_call" || event.type === "tool.call" || event.type === "tool_result" || event.type === "tool.result" || event.type === "tool.event") return true;
|
|
2045
|
+
if (event.type !== "message.part.updated") return false;
|
|
2046
|
+
const part = event.data?.part;
|
|
2047
|
+
if (part?.type === "tool") return true;
|
|
2048
|
+
if (part?.type !== "reasoning" && part?.type !== "text") return false;
|
|
2049
|
+
const text = event.data?.delta ?? part.text;
|
|
2050
|
+
return typeof text === "string" && text.trim().length > 0;
|
|
2051
|
+
}
|
|
2052
|
+
async function* tapRawEvents(source, onRawEvent, log) {
|
|
2053
|
+
for await (const event of source) {
|
|
2054
|
+
try {
|
|
2055
|
+
await onRawEvent(event);
|
|
2056
|
+
} catch (err) {
|
|
2057
|
+
log("[chat-routes] onRawEvent failed", { error: err instanceof Error ? err.message : String(err) });
|
|
2058
|
+
}
|
|
2059
|
+
yield event;
|
|
2060
|
+
}
|
|
2061
|
+
}
|
|
2062
|
+
async function* withStreamHeartbeat(source, intervalMs, makeEvent) {
|
|
2063
|
+
const iterator = source[Symbol.asyncIterator]();
|
|
2064
|
+
try {
|
|
2065
|
+
let pending = iterator.next();
|
|
2066
|
+
let windowStart = Date.now();
|
|
2067
|
+
let tick = 0;
|
|
2068
|
+
for (; ; ) {
|
|
2069
|
+
let timer;
|
|
2070
|
+
let winner;
|
|
2071
|
+
try {
|
|
2072
|
+
const heartbeat = new Promise((resolve) => {
|
|
2073
|
+
timer = setTimeout(() => resolve("heartbeat"), intervalMs);
|
|
2074
|
+
});
|
|
2075
|
+
winner = await Promise.race([pending.then(() => "event"), heartbeat]);
|
|
2076
|
+
} finally {
|
|
2077
|
+
if (timer !== void 0) clearTimeout(timer);
|
|
2078
|
+
}
|
|
2079
|
+
if (winner === "heartbeat") {
|
|
2080
|
+
tick += 1;
|
|
2081
|
+
yield makeEvent({ elapsedMs: Date.now() - windowStart, tick });
|
|
2082
|
+
continue;
|
|
2083
|
+
}
|
|
2084
|
+
const result = await pending;
|
|
2085
|
+
if (result.done) return;
|
|
2086
|
+
yield result.value;
|
|
2087
|
+
pending = iterator.next();
|
|
2088
|
+
windowStart = Date.now();
|
|
2089
|
+
tick = 0;
|
|
2090
|
+
}
|
|
2091
|
+
} finally {
|
|
2092
|
+
await iterator.return?.();
|
|
2093
|
+
}
|
|
2094
|
+
}
|
|
2095
|
+
function concatStreams(streams) {
|
|
2096
|
+
let index = 0;
|
|
2097
|
+
let reader = null;
|
|
2098
|
+
return new ReadableStream({
|
|
2099
|
+
async pull(controller) {
|
|
2100
|
+
for (; ; ) {
|
|
2101
|
+
if (!reader) {
|
|
2102
|
+
const next = streams[index++];
|
|
2103
|
+
if (!next) {
|
|
2104
|
+
controller.close();
|
|
2105
|
+
return;
|
|
2106
|
+
}
|
|
2107
|
+
reader = next.getReader();
|
|
2108
|
+
}
|
|
2109
|
+
const { done, value } = await reader.read();
|
|
2110
|
+
if (done) {
|
|
2111
|
+
reader = null;
|
|
2112
|
+
continue;
|
|
2113
|
+
}
|
|
2114
|
+
controller.enqueue(value);
|
|
2115
|
+
return;
|
|
2116
|
+
}
|
|
2117
|
+
},
|
|
2118
|
+
async cancel(reason) {
|
|
2119
|
+
await reader?.cancel(reason);
|
|
2120
|
+
for (const stream of streams.slice(index)) await stream.cancel(reason);
|
|
2121
|
+
}
|
|
2122
|
+
});
|
|
2123
|
+
}
|
|
2124
|
+
async function* abortOnSignal(source, signal) {
|
|
2125
|
+
const iterator = source[Symbol.asyncIterator]();
|
|
2126
|
+
try {
|
|
2127
|
+
for (; ; ) {
|
|
2128
|
+
const next = await nextWithAbort(iterator, signal);
|
|
2129
|
+
if (next.done) return;
|
|
2130
|
+
yield next.value;
|
|
2131
|
+
}
|
|
2132
|
+
} finally {
|
|
2133
|
+
await closeIteratorAfterAbort(iterator);
|
|
2134
|
+
}
|
|
2135
|
+
}
|
|
2136
|
+
function nextWithAbort(iterator, signal) {
|
|
2137
|
+
if (signal.aborted) return Promise.reject(new Error("chat turn cancelled"));
|
|
2138
|
+
return new Promise((resolve, reject) => {
|
|
2139
|
+
let settled = false;
|
|
2140
|
+
const cleanup = () => signal.removeEventListener("abort", onAbort);
|
|
2141
|
+
const onAbort = () => {
|
|
2142
|
+
if (settled) return;
|
|
2143
|
+
settled = true;
|
|
2144
|
+
cleanup();
|
|
2145
|
+
reject(new Error("chat turn cancelled"));
|
|
2146
|
+
};
|
|
2147
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
2148
|
+
if (signal.aborted) {
|
|
2149
|
+
onAbort();
|
|
2150
|
+
return;
|
|
2151
|
+
}
|
|
2152
|
+
Promise.resolve().then(() => iterator.next()).then(
|
|
2153
|
+
(result) => {
|
|
2154
|
+
if (settled) return;
|
|
2155
|
+
settled = true;
|
|
2156
|
+
cleanup();
|
|
2157
|
+
resolve(result);
|
|
2158
|
+
},
|
|
2159
|
+
(error) => {
|
|
2160
|
+
if (settled) return;
|
|
2161
|
+
settled = true;
|
|
2162
|
+
cleanup();
|
|
2163
|
+
reject(error);
|
|
2164
|
+
}
|
|
2165
|
+
);
|
|
2166
|
+
});
|
|
2167
|
+
}
|
|
2168
|
+
var ABORT_CLEANUP_TIMEOUT_MS = 50;
|
|
2169
|
+
async function closeIteratorAfterAbort(iterator) {
|
|
2170
|
+
let closing;
|
|
2171
|
+
try {
|
|
2172
|
+
const result = iterator.return?.();
|
|
2173
|
+
if (result) closing = Promise.resolve(result);
|
|
2174
|
+
} catch {
|
|
2175
|
+
return;
|
|
2176
|
+
}
|
|
2177
|
+
if (!closing) return;
|
|
2178
|
+
let timeout;
|
|
2179
|
+
try {
|
|
2180
|
+
await Promise.race([
|
|
2181
|
+
Promise.resolve(closing).catch(() => void 0),
|
|
2182
|
+
new Promise((resolve) => {
|
|
2183
|
+
timeout = setTimeout(resolve, ABORT_CLEANUP_TIMEOUT_MS);
|
|
2184
|
+
})
|
|
2185
|
+
]);
|
|
2186
|
+
} finally {
|
|
2187
|
+
if (timeout !== void 0) clearTimeout(timeout);
|
|
2188
|
+
}
|
|
2189
|
+
}
|
|
2190
|
+
|
|
2191
|
+
// src/chat-routes/turn-execution.ts
|
|
2192
|
+
async function readTurnHistory(store, threadId) {
|
|
2193
|
+
return (await store.listMessages(threadId)).map((m) => ({
|
|
2194
|
+
id: m.id,
|
|
2195
|
+
role: m.role,
|
|
2196
|
+
content: m.content,
|
|
2197
|
+
parts: m.parts ?? null
|
|
2198
|
+
}));
|
|
2199
|
+
}
|
|
2200
|
+
function modelAttributionFields(attribution) {
|
|
2201
|
+
return {
|
|
2202
|
+
...attribution?.requestedModel ? { requestedModel: attribution.requestedModel } : {},
|
|
2203
|
+
...attribution?.servedModel ? { servedModel: attribution.servedModel } : {},
|
|
2204
|
+
...attribution?.servedProvider ? { servedProvider: attribution.servedProvider } : {},
|
|
2205
|
+
...attribution?.servedSource ? { servedSource: attribution.servedSource } : {}
|
|
2206
|
+
};
|
|
2207
|
+
}
|
|
2208
|
+
function settledTurnTiming(turnStreamId, at, trace) {
|
|
2209
|
+
const measured = {
|
|
2210
|
+
admissionMs: at.startedAt - at.requestAt,
|
|
2211
|
+
...at.firstVisibleAt === void 0 ? {} : { firstVisibleMs: at.firstVisibleAt - at.startedAt },
|
|
2212
|
+
...at.firstTextAt === void 0 ? {} : { firstTextMs: at.firstTextAt - at.startedAt }
|
|
2213
|
+
};
|
|
2214
|
+
const failureCode = at.failure ? readableTurnFailure(at.failure.error, at.failure.data) : void 0;
|
|
2215
|
+
noteMeasuredTurnFacts(turnStreamId, {
|
|
2216
|
+
requestReceivedAt: at.requestAt,
|
|
2217
|
+
...measured,
|
|
2218
|
+
...at.model ? { model: at.model } : {},
|
|
2219
|
+
...failureCode ? { failureCode } : {}
|
|
2220
|
+
});
|
|
2221
|
+
return { ...measured, trace };
|
|
2222
|
+
}
|
|
2223
|
+
function createAdmittedTurnRunner(dependencies) {
|
|
2224
|
+
return runAdmittedTurn.bind(null, dependencies);
|
|
2225
|
+
}
|
|
2226
|
+
async function runAdmittedTurn({
|
|
2227
|
+
options,
|
|
2228
|
+
draftStore,
|
|
2229
|
+
draftTuning,
|
|
2230
|
+
progressMessages,
|
|
2231
|
+
traceOptions,
|
|
2232
|
+
traceSink,
|
|
2233
|
+
startIntentLine,
|
|
2234
|
+
log
|
|
2235
|
+
}, request, ctx, admitted, early, progress, startedIntent) {
|
|
2236
|
+
const { parsed, auth, trace } = admitted;
|
|
2237
|
+
const intentLine = startedIntent ?? startIntentLine(admitted);
|
|
2238
|
+
const { payload, content, fileParts, mentions, turnId } = parsed;
|
|
2239
|
+
const { tenantId, userId, context } = auth;
|
|
2240
|
+
const preparedBatch = await prepareAdmittedTurn(options, trace, log, tenantId, userId, payload, context, request, progress, content, turnId, draftTuning, ctx?.waitUntil);
|
|
2241
|
+
if (preparedBatch instanceof Response) return preparedBatch;
|
|
2242
|
+
const { turnStreamId, identity, executionId, chatTurn, readHistory, historyBeforeLock } = preparedBatch;
|
|
2243
|
+
const prompt = fileParts.length === 0 ? content : content ? [{ type: "text", text: content }, ...fileParts] : [...fileParts];
|
|
2244
|
+
const state = createAdmittedTurnState();
|
|
2245
|
+
if (options.cancel) state.cancellationAbort = new AbortController();
|
|
2246
|
+
state.nativeTiming = createNativeTimingTransfer(() => ({
|
|
2247
|
+
turnStreamId,
|
|
2248
|
+
requestReceivedAt: trace.startedAt,
|
|
2249
|
+
...state.turnStarted ? { lifecycleStartedAt: state.turnStartedAtMs, admissionMs: state.turnStartedAtMs - trace.startedAt } : {},
|
|
2250
|
+
...state.firstVisibleAtMs === void 0 ? {} : { firstVisibleMs: state.firstVisibleAtMs - state.turnStartedAtMs },
|
|
2251
|
+
...state.firstTextAtMs === void 0 ? {} : { firstTextMs: state.firstTextAtMs - state.turnStartedAtMs }
|
|
2252
|
+
}), (error) => log("[chat-routes] native timing persistence failed", {
|
|
2253
|
+
turnId: turnStreamId,
|
|
2254
|
+
error: error instanceof Error ? error.message : String(error)
|
|
2255
|
+
}));
|
|
2256
|
+
const { handoffCompletion, releaseLock } = createChatTurnOwnership(state, log, turnStreamId, options);
|
|
2257
|
+
let produceArgs = {
|
|
2258
|
+
request,
|
|
2259
|
+
waitUntil: ctx?.waitUntil,
|
|
2260
|
+
earlyResource: early.resource,
|
|
2261
|
+
admissionSignal: state.cancellationAbort?.signal,
|
|
2262
|
+
body: payload,
|
|
2263
|
+
identity,
|
|
2264
|
+
context,
|
|
2265
|
+
prompt,
|
|
2266
|
+
executionId,
|
|
2267
|
+
turnStreamId,
|
|
2268
|
+
priorMessages: chatTurn.priorMessages,
|
|
2269
|
+
...chatTurn.reusedUserMessageId ? { userMessageId: chatTurn.reusedUserMessageId, userMessageInserted: false } : {},
|
|
2270
|
+
handoffCompletion,
|
|
2271
|
+
timingFacts: state.nativeTiming.snapshot,
|
|
2272
|
+
trace,
|
|
2273
|
+
...intentLine ? { intentLine } : {},
|
|
2274
|
+
...admitted.prefetched ? { prefetched: admitted.prefetched } : {},
|
|
2275
|
+
...ctx?.executionLimits ? { executionLimits: ctx.executionLimits } : {}
|
|
2276
|
+
};
|
|
2277
|
+
const selected = selectTurnProducer(options.produce, produceArgs);
|
|
2278
|
+
const dispatched = dispatchFirstFor(selected, produceArgs, trace);
|
|
2279
|
+
if (dispatched) early.add(dispatched.refuse);
|
|
2280
|
+
const turnLock = options.turnLock;
|
|
2281
|
+
if (turnLock) {
|
|
2282
|
+
const acquired = await trace.measure("admission.lock", "admission", () => turnLock.acquire(produceArgs));
|
|
2283
|
+
if (!acquired.acquired) return acquired.response;
|
|
2284
|
+
state.lockAcquired = true;
|
|
2285
|
+
state.lockHandle = acquired.handle;
|
|
2286
|
+
}
|
|
2287
|
+
progress?.("preparing", progressMessages.preparing);
|
|
2288
|
+
const { endTrace, exportTraceInBackground, fireTerminalLifecycle, assistantRowId, exportTrace, runAfterSettle } = createTurnSettlement(state, trace, options, executionId, traceSink, identity, traceOptions, context, turnStreamId, log, ctx);
|
|
2289
|
+
try {
|
|
2290
|
+
if (state.lockAcquired && JSON.stringify(await trace.measure("admission.history_recheck", "admission", readHistory)) !== historyBeforeLock) {
|
|
2291
|
+
await releaseLock();
|
|
2292
|
+
return Response.json({
|
|
2293
|
+
code: "chat_history_changed",
|
|
2294
|
+
error: "The conversation changed while this turn was acquiring its lock. Refresh the conversation before starting new work."
|
|
2295
|
+
}, { status: 409 });
|
|
2296
|
+
}
|
|
2297
|
+
const insertUserMessage = chatTurn.shouldInsertUserMessage && (auth.insertUserMessage ?? true);
|
|
2298
|
+
let userMessageId = chatTurn.reusedUserMessageId ?? null;
|
|
2299
|
+
let userMessageInserted = false;
|
|
2300
|
+
if (insertUserMessage) {
|
|
2301
|
+
userMessageId = rowIdOf(
|
|
2302
|
+
await trace.measure("admission.persist_user", "admission", () => options.store.appendMessage({
|
|
2303
|
+
threadId: payload.threadId,
|
|
2304
|
+
role: "user",
|
|
2305
|
+
content,
|
|
2306
|
+
parts: userPartsWithFiles(chatTurn.userParts, fileParts, mentions)
|
|
2307
|
+
}))
|
|
2308
|
+
);
|
|
2309
|
+
userMessageInserted = userMessageId !== null;
|
|
2310
|
+
}
|
|
2311
|
+
produceArgs = { ...produceArgs, userMessageId, userMessageInserted };
|
|
2312
|
+
const contextGate = options.contextGate;
|
|
2313
|
+
if (contextGate) {
|
|
2314
|
+
const gate = await trace.measure("admission.gate", "admission", () => contextGate(produceArgs));
|
|
2315
|
+
if (gate.proceed === "handoff") {
|
|
2316
|
+
await releaseLock();
|
|
2317
|
+
state.legacyHandoff = true;
|
|
2318
|
+
endTrace(false, void 0);
|
|
2319
|
+
exportTraceInBackground();
|
|
2320
|
+
return gate.response;
|
|
2321
|
+
}
|
|
2322
|
+
if (!gate.proceed) {
|
|
2323
|
+
await releaseLock();
|
|
2324
|
+
if (state.completionHandoff) {
|
|
2325
|
+
endTrace(false, void 0);
|
|
2326
|
+
exportTraceInBackground();
|
|
2327
|
+
return gate.response;
|
|
2328
|
+
}
|
|
2329
|
+
state.gatedTurn = true;
|
|
2330
|
+
state.turnStartedAtMs = Date.now();
|
|
2331
|
+
if (options.lifecycle?.onTurnStart) {
|
|
2332
|
+
try {
|
|
2333
|
+
await options.lifecycle.onTurnStart({
|
|
2334
|
+
identity,
|
|
2335
|
+
executionId,
|
|
2336
|
+
turnStreamId,
|
|
2337
|
+
context,
|
|
2338
|
+
startedAt: state.turnStartedAtMs,
|
|
2339
|
+
requestReceivedAt: trace.startedAt,
|
|
2340
|
+
admissionMs: state.turnStartedAtMs - trace.startedAt
|
|
2341
|
+
});
|
|
2342
|
+
} catch (err) {
|
|
2343
|
+
log("[chat-routes] lifecycle.onTurnStart failed", {
|
|
2344
|
+
turnId: turnStreamId,
|
|
2345
|
+
error: err instanceof Error ? err.message : String(err)
|
|
2346
|
+
});
|
|
2347
|
+
}
|
|
2348
|
+
}
|
|
2349
|
+
await fireTerminalLifecycle(false, void 0);
|
|
2350
|
+
exportTraceInBackground();
|
|
2351
|
+
return gate.response;
|
|
2352
|
+
}
|
|
2353
|
+
}
|
|
2354
|
+
const beforeTurn = options.beforeTurn;
|
|
2355
|
+
if (beforeTurn) {
|
|
2356
|
+
const patch = await trace.measure("admission.before_turn", "admission", (span) => beforeTurn(produceArgs, { span }));
|
|
2357
|
+
if (patch && Object.keys(patch).length && dispatched) throw new Error("beforeTurn cannot patch a Router turn already dispatched: its prompt is already sent");
|
|
2358
|
+
if (patch) produceArgs = { ...produceArgs, ...patch };
|
|
2359
|
+
}
|
|
2360
|
+
const streamSetup = trace.start("admission.stream_setup", "admission");
|
|
2361
|
+
state.tap = createBufferedTurnTap({
|
|
2362
|
+
store: options.turnStore,
|
|
2363
|
+
turnId: turnStreamId,
|
|
2364
|
+
scopeId: payload.threadId,
|
|
2365
|
+
coalesce: coalesceDeltas
|
|
2366
|
+
});
|
|
2367
|
+
const turnMarker = { type: "turn", turnId: turnStreamId };
|
|
2368
|
+
await state.tap.onEvent(turnMarker);
|
|
2369
|
+
if (state.completionHandoff) await state.tap.detach();
|
|
2370
|
+
if (draftTuning) {
|
|
2371
|
+
state.draft = createAssistantDraftWriter({
|
|
2372
|
+
...draftTuning,
|
|
2373
|
+
store: draftStore,
|
|
2374
|
+
threadId: payload.threadId,
|
|
2375
|
+
messageId: assistantRowIdForTurn(executionId),
|
|
2376
|
+
snapshot: () => {
|
|
2377
|
+
if (!state.producer || state.cancellationAbort?.signal.aborted) return null;
|
|
2378
|
+
return {
|
|
2379
|
+
content: state.producer.finalText(),
|
|
2380
|
+
...state.producer.draftParts ? { parts: state.producer.draftParts() } : {},
|
|
2381
|
+
...state.producer.usage ? { usage: state.producer.usage() } : {},
|
|
2382
|
+
...state.producer.model ? { model: state.producer.model } : {}
|
|
2383
|
+
};
|
|
2384
|
+
},
|
|
2385
|
+
...options.transformFinalText ? { transformText: options.transformFinalText } : {},
|
|
2386
|
+
log
|
|
2387
|
+
});
|
|
2388
|
+
if (state.completionHandoff) await state.draft.close();
|
|
2389
|
+
}
|
|
2390
|
+
state.turnStartedAtMs = Date.now();
|
|
2391
|
+
state.turnStarted = true;
|
|
2392
|
+
if (options.lifecycle?.onTurnStart) {
|
|
2393
|
+
try {
|
|
2394
|
+
await options.lifecycle.onTurnStart({
|
|
2395
|
+
identity,
|
|
2396
|
+
executionId,
|
|
2397
|
+
turnStreamId,
|
|
2398
|
+
context,
|
|
2399
|
+
startedAt: state.turnStartedAtMs,
|
|
2400
|
+
requestReceivedAt: trace.startedAt,
|
|
2401
|
+
admissionMs: state.turnStartedAtMs - trace.startedAt
|
|
2402
|
+
});
|
|
2403
|
+
} catch (err) {
|
|
2404
|
+
log("[chat-routes] lifecycle.onTurnStart failed", {
|
|
2405
|
+
turnId: turnStreamId,
|
|
2406
|
+
error: err instanceof Error ? err.message : String(err)
|
|
2407
|
+
});
|
|
2408
|
+
}
|
|
2409
|
+
}
|
|
2410
|
+
streamSetup.end();
|
|
2411
|
+
const { result } = createTurnEngine(identity, ctx, log, state, dispatched, produceArgs, trace, options, selected.produce, request, context, payload, assistantRowId);
|
|
2412
|
+
const { clientBody } = startTurnDrain(result, log, turnStreamId, state, endTrace, exportTrace, trace, ctx, request, fireTerminalLifecycle, releaseLock, runAfterSettle, early.settle);
|
|
2413
|
+
early.handOff();
|
|
2414
|
+
return createTurnReplayResponse(result.contentType, clientBody, turnMarker, trace);
|
|
2415
|
+
} catch (err) {
|
|
2416
|
+
if (!state.completionHandoff) {
|
|
2417
|
+
if (state.turnStarted) await fireTerminalLifecycle(true, err);
|
|
2418
|
+
await releaseLock();
|
|
2419
|
+
}
|
|
2420
|
+
endTrace(true, err);
|
|
2421
|
+
exportTraceInBackground();
|
|
2422
|
+
throw err;
|
|
2423
|
+
}
|
|
2424
|
+
}
|
|
2425
|
+
function createAdmittedTurnState() {
|
|
2426
|
+
return {
|
|
2427
|
+
draft: void 0,
|
|
2428
|
+
tap: void 0,
|
|
2429
|
+
nativeTiming: void 0,
|
|
2430
|
+
completionHandoff: false,
|
|
2431
|
+
completionHandoffPromise: void 0,
|
|
2432
|
+
lockAcquired: false,
|
|
2433
|
+
lockHandle: void 0,
|
|
2434
|
+
lockReleased: false,
|
|
2435
|
+
producer: void 0,
|
|
2436
|
+
assistantMessageId: void 0,
|
|
2437
|
+
runFailed: false,
|
|
2438
|
+
lastFailureData: void 0,
|
|
2439
|
+
turnStartedAtMs: 0,
|
|
2440
|
+
firstVisibleAtMs: void 0,
|
|
2441
|
+
firstTextAtMs: void 0,
|
|
2442
|
+
turnStarted: false,
|
|
2443
|
+
lifecycleSettled: false,
|
|
2444
|
+
settledTurn: void 0,
|
|
2445
|
+
gatedTurn: false,
|
|
2446
|
+
legacyHandoff: false,
|
|
2447
|
+
pendingTraceExport: void 0,
|
|
2448
|
+
traceEnded: false
|
|
2449
|
+
};
|
|
2450
|
+
}
|
|
2451
|
+
async function prepareAdmittedTurn(options, trace, log, tenantId, userId, payload, context, request, progress, content, turnId, draftTuning, waitUntil) {
|
|
2452
|
+
if (options.turnLock?.warm) {
|
|
2453
|
+
const warmSpan = trace.start("admission.lock_warm", "admission");
|
|
2454
|
+
const warmFailed = (err) => {
|
|
2455
|
+
warmSpan.end(void 0, err);
|
|
2456
|
+
log("[chat-routes] turnLock.warm failed", { error: err instanceof Error ? err.message : String(err) });
|
|
2457
|
+
};
|
|
2458
|
+
try {
|
|
2459
|
+
void Promise.resolve(options.turnLock.warm({ tenantId, userId, threadId: payload.threadId, context })).then(() => warmSpan.end(), warmFailed);
|
|
2460
|
+
} catch (err) {
|
|
2461
|
+
warmFailed(err);
|
|
2462
|
+
}
|
|
2463
|
+
}
|
|
2464
|
+
const prepareTurn = options.prepareTurn;
|
|
2465
|
+
if (prepareTurn) {
|
|
2466
|
+
const prepared = await trace.measure("admission.prepare", "admission", () => prepareTurn({
|
|
2467
|
+
request,
|
|
2468
|
+
waitUntil,
|
|
2469
|
+
body: payload,
|
|
2470
|
+
tenantId,
|
|
2471
|
+
userId,
|
|
2472
|
+
threadId: payload.threadId,
|
|
2473
|
+
context,
|
|
2474
|
+
progress: (phase, message) => progress?.(phase, message)
|
|
2475
|
+
}));
|
|
2476
|
+
if (!prepared.ok) return prepared.response;
|
|
2477
|
+
}
|
|
2478
|
+
const readHistory = () => readTurnHistory(options.store, payload.threadId);
|
|
2479
|
+
const historySpan = trace.start("admission.history", "admission");
|
|
2480
|
+
const existingMessages = await readHistory();
|
|
2481
|
+
const historyBeforeLock = options.turnLock ? JSON.stringify(existingMessages) : void 0;
|
|
2482
|
+
let chatTurn = resolveChatTurn({ existingMessages, userContent: content, turnId, hasRunningTurn: false });
|
|
2483
|
+
if (draftTuning && existingMessages.at(-1)?.role === "assistant") {
|
|
2484
|
+
const ifRunning = resolveChatTurn({ existingMessages, userContent: content, turnId, hasRunningTurn: true });
|
|
2485
|
+
if (!ifRunning.shouldInsertUserMessage) {
|
|
2486
|
+
let hasRunningTurn = false;
|
|
2487
|
+
try {
|
|
2488
|
+
hasRunningTurn = (await trace.measure("admission.running_probe", "admission", () => options.turnStore.listRunning?.(payload.threadId), void 0, historySpan) ?? []).length > 0;
|
|
2489
|
+
} catch (err) {
|
|
2490
|
+
log("[chat-routes] listRunning probe failed; treating the thread as idle", {
|
|
2491
|
+
threadId: payload.threadId,
|
|
2492
|
+
error: err instanceof Error ? err.message : String(err)
|
|
2493
|
+
});
|
|
2494
|
+
}
|
|
2495
|
+
if (hasRunningTurn) chatTurn = ifRunning;
|
|
2496
|
+
}
|
|
2497
|
+
}
|
|
2498
|
+
historySpan.end({ messages: existingMessages.length });
|
|
2499
|
+
const identity = {
|
|
2500
|
+
tenantId,
|
|
2501
|
+
sessionId: payload.threadId,
|
|
2502
|
+
userId,
|
|
2503
|
+
turnIndex: chatTurn.turnIndex
|
|
2504
|
+
};
|
|
2505
|
+
const turnExecutionId = deriveExecutionId({
|
|
2506
|
+
projectId: options.projectId,
|
|
2507
|
+
sessionId: payload.threadId,
|
|
2508
|
+
turnIndex: chatTurn.turnIndex
|
|
2509
|
+
});
|
|
2510
|
+
const executionId = payload.retryAttemptId ? `${turnExecutionId}:retry:${payload.retryAttemptId}` : turnExecutionId;
|
|
2511
|
+
if (executionId.length > 256) return errorResponse(new ChatTurnInputError("Retry execution id exceeds 256 bytes"));
|
|
2512
|
+
trace.bind(executionId);
|
|
2513
|
+
const turnStreamId = crypto.randomUUID();
|
|
2514
|
+
return { turnStreamId, identity, executionId, chatTurn, readHistory, historyBeforeLock };
|
|
2515
|
+
}
|
|
2516
|
+
function createTurnSettlement(state, trace, options, executionId, traceSink, identity, traceOptions, context, turnStreamId, log, ctx) {
|
|
2517
|
+
const assistantRowId = () => state.assistantMessageId ?? state.draft?.rowId() ?? null;
|
|
2518
|
+
const endTrace = (failed, terminalError) => {
|
|
2519
|
+
const failureCode = failed ? failureCodeOf(state.lastFailureData) ?? (terminalError instanceof Error ? terminalError.name : "turn_failed") : void 0;
|
|
2520
|
+
const snapshot = trace.finish(failureCode === void 0 ? {} : { error: failureCode });
|
|
2521
|
+
if (state.traceEnded) return snapshot;
|
|
2522
|
+
state.traceEnded = true;
|
|
2523
|
+
const handedOff = state.completionHandoff || state.legacyHandoff;
|
|
2524
|
+
console.info(JSON.stringify({
|
|
2525
|
+
event: "agent_app_turn_trace",
|
|
2526
|
+
project: options.projectId,
|
|
2527
|
+
executionId,
|
|
2528
|
+
otlpTraceId: otlpTraceId(snapshot.traceId),
|
|
2529
|
+
failed,
|
|
2530
|
+
...failureCode ? { failureCode } : {},
|
|
2531
|
+
...handedOff ? { handedOff: true } : {},
|
|
2532
|
+
...state.gatedTurn ? { gated: true } : {},
|
|
2533
|
+
...snapshot.summary
|
|
2534
|
+
}));
|
|
2535
|
+
const sink = traceSink;
|
|
2536
|
+
if (sink) {
|
|
2537
|
+
state.pendingTraceExport = () => sink(snapshot, {
|
|
2538
|
+
"tangle.sessionId": identity.sessionId,
|
|
2539
|
+
"tangle.userId": identity.userId,
|
|
2540
|
+
"tangle.tenantId": identity.tenantId,
|
|
2541
|
+
"tangle.turn.failed": failed,
|
|
2542
|
+
...state.gatedTurn ? { "tangle.turn.gated": true } : {},
|
|
2543
|
+
...handedOff ? { "tangle.turn.handed_off": true } : {},
|
|
2544
|
+
...failureCode ? { "tangle.turn.failure_code": failureCode } : {},
|
|
2545
|
+
...traceOptions?.labels?.({ identity, executionId, context }) ?? {}
|
|
2546
|
+
});
|
|
2547
|
+
}
|
|
2548
|
+
return snapshot;
|
|
2549
|
+
};
|
|
2550
|
+
const fireTerminalLifecycle = async (failed, terminalError) => {
|
|
2551
|
+
if (state.lifecycleSettled) return;
|
|
2552
|
+
state.lifecycleSettled = true;
|
|
2553
|
+
const snapshot = endTrace(failed, terminalError);
|
|
2554
|
+
const lifecycle = options.lifecycle;
|
|
2555
|
+
const durationMs = Date.now() - state.turnStartedAtMs;
|
|
2556
|
+
const timing = settledTurnTiming(turnStreamId, {
|
|
2557
|
+
requestAt: trace.startedAt,
|
|
2558
|
+
startedAt: state.turnStartedAtMs,
|
|
2559
|
+
firstVisibleAt: state.firstVisibleAtMs,
|
|
2560
|
+
firstTextAt: state.firstTextAtMs,
|
|
2561
|
+
model: state.producer?.model,
|
|
2562
|
+
failure: failed ? { data: state.lastFailureData, error: terminalError } : void 0
|
|
2563
|
+
}, snapshot.summary);
|
|
2564
|
+
try {
|
|
2565
|
+
if (!lifecycle) {
|
|
2566
|
+
} else if (failed) {
|
|
2567
|
+
await lifecycle.onTurnError?.({
|
|
2568
|
+
identity,
|
|
2569
|
+
executionId,
|
|
2570
|
+
turnStreamId,
|
|
2571
|
+
context,
|
|
2572
|
+
durationMs,
|
|
2573
|
+
...timing,
|
|
2574
|
+
error: terminalError ?? state.lastFailureData ?? new Error("chat turn failed")
|
|
2575
|
+
});
|
|
2576
|
+
} else {
|
|
2577
|
+
const failoverInfo = state.producer?.modelFailover?.();
|
|
2578
|
+
const attribution = state.producer?.modelAttribution?.();
|
|
2579
|
+
const completed = {
|
|
2580
|
+
identity,
|
|
2581
|
+
executionId,
|
|
2582
|
+
turnStreamId,
|
|
2583
|
+
context,
|
|
2584
|
+
durationMs,
|
|
2585
|
+
...timing,
|
|
2586
|
+
finalText: state.cancellationAbort?.signal.aborted ? "" : state.producer?.finalText() ?? "",
|
|
2587
|
+
usage: state.producer?.usage?.() ?? {},
|
|
2588
|
+
assistantMessageId: assistantRowId(),
|
|
2589
|
+
...state.gatedTurn ? { gated: true } : {},
|
|
2590
|
+
...state.producer?.model ? { model: state.producer.model } : {},
|
|
2591
|
+
...modelAttributionFields(attribution),
|
|
2592
|
+
...failoverInfo ? { modelFailover: failoverInfo } : {}
|
|
2593
|
+
};
|
|
2594
|
+
if (!state.gatedTurn) state.settledTurn = completed;
|
|
2595
|
+
await lifecycle.onTurnComplete?.(completed);
|
|
2596
|
+
}
|
|
2597
|
+
} catch (err) {
|
|
2598
|
+
log("[chat-routes] lifecycle terminal hook failed", {
|
|
2599
|
+
turnId: turnStreamId,
|
|
2600
|
+
error: err instanceof Error ? err.message : String(err)
|
|
2601
|
+
});
|
|
2602
|
+
}
|
|
2603
|
+
};
|
|
2604
|
+
const exportTrace = async () => {
|
|
2605
|
+
const send = state.pendingTraceExport;
|
|
2606
|
+
state.pendingTraceExport = void 0;
|
|
2607
|
+
if (!send) return;
|
|
2608
|
+
try {
|
|
2609
|
+
await send();
|
|
2610
|
+
} catch (err) {
|
|
2611
|
+
log("[chat-routes] turn trace sink failed", {
|
|
2612
|
+
turnId: turnStreamId,
|
|
2613
|
+
error: err instanceof Error ? err.message : String(err)
|
|
2614
|
+
});
|
|
2615
|
+
}
|
|
2616
|
+
};
|
|
2617
|
+
const runAfterSettle = async () => {
|
|
2618
|
+
const info = state.settledTurn;
|
|
2619
|
+
state.settledTurn = void 0;
|
|
2620
|
+
const afterSettle = options.lifecycle?.afterSettle;
|
|
2621
|
+
if (!info || !afterSettle) return;
|
|
2622
|
+
try {
|
|
2623
|
+
await afterSettle(info);
|
|
2624
|
+
} catch (err) {
|
|
2625
|
+
log("[chat-routes] lifecycle.afterSettle failed", {
|
|
2626
|
+
turnId: turnStreamId,
|
|
2627
|
+
error: err instanceof Error ? err.message : String(err)
|
|
2628
|
+
});
|
|
2629
|
+
}
|
|
2630
|
+
};
|
|
2631
|
+
const exportTraceInBackground = () => {
|
|
2632
|
+
const sent = exportTrace();
|
|
2633
|
+
if (ctx?.waitUntil) ctx.waitUntil(sent);
|
|
2634
|
+
else void sent.catch(() => {
|
|
2635
|
+
});
|
|
2636
|
+
};
|
|
2637
|
+
return { endTrace, exportTraceInBackground, fireTerminalLifecycle, assistantRowId, exportTrace, runAfterSettle };
|
|
2638
|
+
}
|
|
2639
|
+
function createTurnEngine(identity, ctx, log, state, dispatched, produceArgs, trace, options, produce, request, context, payload, assistantRowId) {
|
|
2640
|
+
const result = handleChatTurn({
|
|
2641
|
+
identity,
|
|
2642
|
+
waitUntil: ctx?.waitUntil,
|
|
2643
|
+
log,
|
|
2644
|
+
hooks: {
|
|
2645
|
+
// The engine wants a synchronous producer; box resolution is async —
|
|
2646
|
+
// defer it into the generator's first pull.
|
|
2647
|
+
produce: () => ({
|
|
2648
|
+
stream: (async function* () {
|
|
2649
|
+
const producerStream = async function* () {
|
|
2650
|
+
const started = dispatched ? dispatched.take(produceArgs) : trace.measure("admission.produce", "admission", () => produce(produceArgs));
|
|
2651
|
+
if (!dispatched) {
|
|
2652
|
+
const cleanup = started.then((producer) => producer.cleanup, () => void 0);
|
|
2653
|
+
ctx?.waitUntil?.(cleanup);
|
|
2654
|
+
void cleanup.catch(() => {
|
|
2655
|
+
});
|
|
2656
|
+
}
|
|
2657
|
+
state.producer = await started;
|
|
2658
|
+
yield* state.producer.stream;
|
|
2659
|
+
};
|
|
2660
|
+
let source = producerStream();
|
|
2661
|
+
if (options.cancel && state.cancellationAbort) {
|
|
2662
|
+
source = observeTurnCancellation(source, produceArgs, options.cancel, state.cancellationAbort);
|
|
2663
|
+
}
|
|
2664
|
+
if (ctx?.cancelOnDisconnect) {
|
|
2665
|
+
source = abortOnSignal(source, request.signal);
|
|
2666
|
+
}
|
|
2667
|
+
if (options.onRawEvent) {
|
|
2668
|
+
source = tapRawEvents(source, (event) => options.onRawEvent(event, context), log);
|
|
2669
|
+
}
|
|
2670
|
+
if (options.heartbeat) {
|
|
2671
|
+
source = withStreamHeartbeat(source, options.heartbeat.intervalMs, options.heartbeat.event);
|
|
2672
|
+
}
|
|
2673
|
+
for await (const event of source) yield event;
|
|
2674
|
+
})(),
|
|
2675
|
+
finalText: () => state.cancellationAbort?.signal.aborted ? "" : state.producer?.finalText() ?? ""
|
|
2676
|
+
}),
|
|
2677
|
+
onEvent: async (event) => {
|
|
2678
|
+
let timingChanged = false;
|
|
2679
|
+
if (state.firstVisibleAtMs === void 0 && isVisibleOutput(event)) {
|
|
2680
|
+
state.firstVisibleAtMs = Date.now();
|
|
2681
|
+
trace.markFirstVisible(state.firstVisibleAtMs);
|
|
2682
|
+
timingChanged = true;
|
|
2683
|
+
}
|
|
2684
|
+
if (state.firstTextAtMs === void 0 && isVisibleText(event)) {
|
|
2685
|
+
state.firstTextAtMs = Date.now();
|
|
2686
|
+
trace.markFirstText(state.firstTextAtMs);
|
|
2687
|
+
timingChanged = true;
|
|
2688
|
+
}
|
|
2689
|
+
if (timingChanged) {
|
|
2690
|
+
const written = state.nativeTiming?.note();
|
|
2691
|
+
if (written && ctx?.waitUntil) ctx.waitUntil(written);
|
|
2692
|
+
}
|
|
2693
|
+
if (event.type === "session.run.failed" || event.type === "error") {
|
|
2694
|
+
state.runFailed = true;
|
|
2695
|
+
state.lastFailureData = event.data;
|
|
2696
|
+
}
|
|
2697
|
+
await state.tap.onEvent(event);
|
|
2698
|
+
if (!state.completionHandoff) state.draft?.notify(event);
|
|
2699
|
+
if (options.onEvent) await options.onEvent(event, context);
|
|
2700
|
+
},
|
|
2701
|
+
...options.transformFinalText ? { transformFinalText: options.transformFinalText } : {},
|
|
2702
|
+
persistAssistantMessage: ({ finalText }) => trace.measure("persist.assistant", "persist", async () => {
|
|
2703
|
+
if (state.completionHandoff) return;
|
|
2704
|
+
if (state.cancellationAbort?.signal.aborted || ctx?.cancelOnDisconnect && request.signal.aborted) {
|
|
2705
|
+
await state.draft?.discard();
|
|
2706
|
+
state.assistantMessageId = null;
|
|
2707
|
+
return;
|
|
2708
|
+
}
|
|
2709
|
+
const rawParts = state.producer?.assistantParts ? state.producer.assistantParts() : void 0;
|
|
2710
|
+
const projected = rawParts && options.transformFinalText ? await Promise.all(
|
|
2711
|
+
rawParts.map(
|
|
2712
|
+
async (part) => String(part.type ?? "") === "text" ? { ...part, text: await options.transformFinalText(String(part.text ?? "")) } : part
|
|
2713
|
+
)
|
|
2714
|
+
) : rawParts;
|
|
2715
|
+
const parts = projected ? toChatMessageParts(projected) : void 0;
|
|
2716
|
+
if (!finalText.trim() && (!parts || parts.length === 0)) {
|
|
2717
|
+
await state.draft?.discard();
|
|
2718
|
+
state.assistantMessageId = state.draft?.rowId() ?? null;
|
|
2719
|
+
return;
|
|
2720
|
+
}
|
|
2721
|
+
if (state.cancellationAbort?.signal.aborted) {
|
|
2722
|
+
await state.draft?.discard();
|
|
2723
|
+
state.assistantMessageId = null;
|
|
2724
|
+
return;
|
|
2725
|
+
}
|
|
2726
|
+
const usage = state.producer?.usage?.() ?? {};
|
|
2727
|
+
const attribution = state.producer?.modelAttribution?.();
|
|
2728
|
+
const values = {
|
|
2729
|
+
content: finalText,
|
|
2730
|
+
...parts && parts.length > 0 ? { parts } : {},
|
|
2731
|
+
...state.producer?.model ? { model: state.producer.model } : {},
|
|
2732
|
+
...modelAttributionFields(attribution),
|
|
2733
|
+
...usage.inputTokens !== void 0 ? { inputTokens: usage.inputTokens } : {},
|
|
2734
|
+
...usage.outputTokens !== void 0 ? { outputTokens: usage.outputTokens } : {},
|
|
2735
|
+
...usage.reasoningTokens !== void 0 ? { reasoningTokens: usage.reasoningTokens } : {},
|
|
2736
|
+
...usage.cacheReadTokens !== void 0 ? { cacheReadTokens: usage.cacheReadTokens } : {},
|
|
2737
|
+
...usage.cacheWriteTokens !== void 0 ? { cacheWriteTokens: usage.cacheWriteTokens } : {},
|
|
2738
|
+
...usage.costUsd !== void 0 ? { costUsd: usage.costUsd } : {}
|
|
2739
|
+
};
|
|
2740
|
+
if (state.draft) {
|
|
2741
|
+
await state.draft.finalize(values);
|
|
2742
|
+
state.assistantMessageId = state.draft.rowId() ?? null;
|
|
2743
|
+
return;
|
|
2744
|
+
}
|
|
2745
|
+
state.assistantMessageId = rowIdOf(
|
|
2746
|
+
await options.store.appendMessage({
|
|
2747
|
+
threadId: payload.threadId,
|
|
2748
|
+
role: "assistant",
|
|
2749
|
+
...values
|
|
2750
|
+
})
|
|
2751
|
+
);
|
|
2752
|
+
}),
|
|
2753
|
+
...options.onTurnComplete ? {
|
|
2754
|
+
// Wired into the engine's completion hook, which fires only when
|
|
2755
|
+
// the stream ended without throwing. A terminal error EVENT
|
|
2756
|
+
// (not a throw) still lands here — so surface `runFailed` so the
|
|
2757
|
+
// product skips billing an errored turn instead of marking it
|
|
2758
|
+
// complete with empty text.
|
|
2759
|
+
onTurnComplete: async ({ identity: turnIdentity, finalText }) => {
|
|
2760
|
+
if (state.completionHandoff) return;
|
|
2761
|
+
const failoverInfo = state.producer?.modelFailover?.();
|
|
2762
|
+
const attribution = state.producer?.modelAttribution?.();
|
|
2763
|
+
return trace.measure("persist.on_turn_complete", "persist", () => options.onTurnComplete({
|
|
2764
|
+
identity: turnIdentity,
|
|
2765
|
+
finalText,
|
|
2766
|
+
context,
|
|
2767
|
+
failed: state.runFailed,
|
|
2768
|
+
usage: state.producer?.usage?.(),
|
|
2769
|
+
assistantMessageId: assistantRowId(),
|
|
2770
|
+
...state.runFailed ? { failureReason: failureReasonOf(state.lastFailureData) } : {},
|
|
2771
|
+
...state.runFailed && failureCodeOf(state.lastFailureData) ? { failureCode: failureCodeOf(state.lastFailureData) } : {},
|
|
2772
|
+
...state.producer?.model ? { model: state.producer.model } : {},
|
|
2773
|
+
...modelAttributionFields(attribution),
|
|
2774
|
+
...failoverInfo ? { modelFailover: failoverInfo } : {}
|
|
2775
|
+
}));
|
|
2776
|
+
}
|
|
2777
|
+
} : {},
|
|
2778
|
+
...options.traceFlush ? { traceFlush: () => options.traceFlush(context) } : {}
|
|
2779
|
+
}
|
|
2780
|
+
});
|
|
2781
|
+
return { result };
|
|
2782
|
+
}
|
|
2783
|
+
function startTurnDrain(result, log, turnStreamId, state, endTrace, exportTrace, trace, ctx, request, fireTerminalLifecycle, releaseLock, runAfterSettle, settleResource) {
|
|
2784
|
+
const [clientBody, drainBody] = result.body.tee();
|
|
2785
|
+
const drained = (async () => {
|
|
2786
|
+
const reader = drainBody.getReader();
|
|
2787
|
+
let drainError;
|
|
2788
|
+
try {
|
|
2789
|
+
for (; ; ) {
|
|
2790
|
+
const { done } = await reader.read();
|
|
2791
|
+
if (done) break;
|
|
2792
|
+
}
|
|
2793
|
+
} catch (err) {
|
|
2794
|
+
drainError = err;
|
|
2795
|
+
log("[chat-routes] turn drain failed", {
|
|
2796
|
+
turnId: turnStreamId,
|
|
2797
|
+
error: err instanceof Error ? err.message : String(err)
|
|
2798
|
+
});
|
|
2799
|
+
}
|
|
2800
|
+
const failed = state.runFailed || drainError !== void 0;
|
|
2801
|
+
if (state.completionHandoff) {
|
|
2802
|
+
endTrace(failed, drainError);
|
|
2803
|
+
await Promise.all([state.tap.detach(), state.nativeTiming?.flush(), exportTrace()]);
|
|
2804
|
+
return;
|
|
2805
|
+
}
|
|
2806
|
+
const finalize = trace.start("persist.finalize", "persist");
|
|
2807
|
+
if (state.cancellationAbort?.signal.aborted || ctx?.cancelOnDisconnect && request.signal.aborted) {
|
|
2808
|
+
await state.draft?.discard();
|
|
2809
|
+
} else {
|
|
2810
|
+
await state.draft?.close();
|
|
2811
|
+
}
|
|
2812
|
+
try {
|
|
2813
|
+
await state.tap.done(failed ? "error" : "complete");
|
|
2814
|
+
} catch (err) {
|
|
2815
|
+
log("[chat-routes] turn buffer finalize failed", {
|
|
2816
|
+
turnId: turnStreamId,
|
|
2817
|
+
error: err instanceof Error ? err.message : String(err)
|
|
2818
|
+
});
|
|
2819
|
+
}
|
|
2820
|
+
finalize.end();
|
|
2821
|
+
await fireTerminalLifecycle(failed, drainError);
|
|
2822
|
+
await releaseLock();
|
|
2823
|
+
await Promise.all([exportTrace(), runAfterSettle()]);
|
|
2824
|
+
})().finally(settleResource);
|
|
2825
|
+
if (ctx?.waitUntil) ctx.waitUntil(drained);
|
|
2826
|
+
else void drained.catch(() => {
|
|
2827
|
+
});
|
|
2828
|
+
return { clientBody };
|
|
2829
|
+
}
|
|
2830
|
+
function createTurnReplayResponse(contentType, clientBody, turnMarker, trace) {
|
|
2831
|
+
const encoder = new TextEncoder();
|
|
2832
|
+
const marker = new ReadableStream({
|
|
2833
|
+
start(controller) {
|
|
2834
|
+
controller.enqueue(encoder.encode(`${JSON.stringify(turnMarker)}
|
|
2835
|
+
`));
|
|
2836
|
+
controller.close();
|
|
2837
|
+
}
|
|
2838
|
+
});
|
|
2839
|
+
const body = concatStreams([marker, clientBody]);
|
|
2840
|
+
return new Response(body, {
|
|
2841
|
+
headers: {
|
|
2842
|
+
"Content-Type": contentType,
|
|
2843
|
+
"Cache-Control": "no-cache",
|
|
2844
|
+
"X-Turn-Id": turnMarker.turnId,
|
|
2845
|
+
"Server-Timing": admissionServerTiming(trace)
|
|
2846
|
+
}
|
|
2847
|
+
});
|
|
2848
|
+
}
|
|
2849
|
+
|
|
2850
|
+
// src/chat-routes/early-turn-work.ts
|
|
2851
|
+
function earlyTurnWork(started, log) {
|
|
2852
|
+
let free = typeof started === "function" ? started : void 0;
|
|
2853
|
+
const owned = typeof started === "object" ? started : void 0;
|
|
2854
|
+
void owned?.resource.catch(() => {
|
|
2855
|
+
});
|
|
2856
|
+
let handedOff = false;
|
|
2857
|
+
let stopped;
|
|
2858
|
+
let also = [];
|
|
2859
|
+
const report = (error) => log("[chat-routes] afterAuthorize release failed", { error: redactErrorMessage(error) });
|
|
2860
|
+
const settle = () => stopped ??= (async () => {
|
|
2861
|
+
if (owned) await owned.dispose(await owned.resource);
|
|
2862
|
+
})().catch(report);
|
|
2863
|
+
return {
|
|
2864
|
+
resource: owned?.resource,
|
|
2865
|
+
handOff: () => {
|
|
2866
|
+
handedOff = true;
|
|
2867
|
+
free = void 0;
|
|
2868
|
+
also = [];
|
|
2869
|
+
},
|
|
2870
|
+
add: (release) => {
|
|
2871
|
+
also.push(release);
|
|
2872
|
+
},
|
|
2873
|
+
settle,
|
|
2874
|
+
release: async () => {
|
|
2875
|
+
const product = free;
|
|
2876
|
+
const route = also;
|
|
2877
|
+
free = void 0;
|
|
2878
|
+
also = [];
|
|
2879
|
+
for (const stop of route) stop();
|
|
2880
|
+
if (product) await Promise.resolve().then(product).catch(report);
|
|
2881
|
+
if (!handedOff) await settle();
|
|
2882
|
+
}
|
|
2883
|
+
};
|
|
2884
|
+
}
|
|
2885
|
+
|
|
2886
|
+
// src/chat-routes/route-storage.ts
|
|
2887
|
+
function resolveTurnStorage(options) {
|
|
2888
|
+
const store = options.turnStore;
|
|
2889
|
+
if ("append" in store) return { ...options, turnStore: store };
|
|
2890
|
+
return {
|
|
2891
|
+
...options,
|
|
2892
|
+
turnStore: createD1TurnEventStore(store.db),
|
|
2893
|
+
turnLock: options.turnLock ?? createDurableTurnLock({ ...store.lock, scopeOf: () => "thread" })
|
|
2894
|
+
};
|
|
2895
|
+
}
|
|
2896
|
+
|
|
2897
|
+
// src/chat-routes/turn-routes.ts
|
|
2898
|
+
async function admitTurn(request, trace, options, waitUntil) {
|
|
2899
|
+
const parse = trace.start("admission.parse", "admission");
|
|
2900
|
+
const [rawBody, badBody] = await parseJsonObjectBody(request);
|
|
2901
|
+
if (badBody) return { ok: false, response: badBody };
|
|
2902
|
+
let parsed;
|
|
2903
|
+
try {
|
|
2904
|
+
parsed = validateTurnBody(rawBody);
|
|
2905
|
+
} catch (err) {
|
|
2906
|
+
if (err instanceof ChatTurnInputError) return { ok: false, response: errorResponse(err) };
|
|
2907
|
+
throw err;
|
|
2908
|
+
}
|
|
2909
|
+
parse.end();
|
|
2910
|
+
const prefetch = options.prefetch && startTurnPrefetch(options.prefetch, { request, body: parsed.payload, trace });
|
|
2911
|
+
let handedOff = false;
|
|
2912
|
+
try {
|
|
2913
|
+
const auth = await trace.measure("admission.authorize", "admission", (span) => options.authorize({ request, waitUntil, intent: "turn", body: parsed.payload, trace, span, ...prefetch ? { identified: prefetch.identified } : {} }));
|
|
2914
|
+
if (!auth.ok) return { ok: false, response: auth.response };
|
|
2915
|
+
if (!("context" in auth) || auth.intent !== void 0 && auth.intent !== "turn") {
|
|
2916
|
+
return { ok: false, response: Response.json({ error: "Authorization did not allow this turn.", code: "chat_authorization_intent_mismatch" }, { status: 403 }) };
|
|
2917
|
+
}
|
|
2918
|
+
const refusal = await namedModelRefusal(options.modelPolicy, parsed.payload, trace);
|
|
2919
|
+
if (refusal) return { ok: false, response: refusal };
|
|
2920
|
+
const admitted = { ok: true, parsed, auth, trace, ...prefetch ? { prefetched: prefetch.admitted(auth) } : {} };
|
|
2921
|
+
handedOff = true;
|
|
2922
|
+
return admitted;
|
|
2923
|
+
} finally {
|
|
2924
|
+
if (!handedOff) prefetch?.discard();
|
|
2925
|
+
}
|
|
2926
|
+
}
|
|
2927
|
+
function createChatTurnRoutes(input) {
|
|
2928
|
+
const options = resolveTurnStorage(input);
|
|
2929
|
+
const log = options.log ?? ((message, meta) => console.error(message, meta ?? ""));
|
|
2930
|
+
const draftStore = options.store;
|
|
2931
|
+
const draftTuning = storeSupportsDraftPersistence(draftStore) ? {} : null;
|
|
2932
|
+
const progressMessages = { ...TURN_ROUTE_PHASE_MESSAGES, ...options.progressMessages };
|
|
2933
|
+
const traceOptions = options.turnTrace === false ? void 0 : options.turnTrace;
|
|
2934
|
+
const traceSink = options.turnTrace === false ? void 0 : traceOptions?.sink ?? createIntelligenceTurnTraceSink({ project: options.projectId });
|
|
2935
|
+
function startIntentLine(admitted) {
|
|
2936
|
+
const intentLine = options.intentLine;
|
|
2937
|
+
if (!intentLine) return void 0;
|
|
2938
|
+
const { parsed, auth } = admitted;
|
|
2939
|
+
const threadId = parsed.payload.threadId;
|
|
2940
|
+
const controller = new AbortController();
|
|
2941
|
+
let timer;
|
|
2942
|
+
const deadline = new Promise((resolve) => {
|
|
2943
|
+
timer = setTimeout(() => {
|
|
2944
|
+
controller.abort();
|
|
2945
|
+
resolve(null);
|
|
2946
|
+
}, intentLine.timeoutMs ?? INTENT_LINE_TIMEOUT_MS);
|
|
2947
|
+
});
|
|
2948
|
+
const generated = (async () => {
|
|
2949
|
+
const history = (await options.store.listMessages(threadId)).slice(-INTENT_LINE_HISTORY_MESSAGES).map((message) => ({ role: message.role, content: message.content }));
|
|
2950
|
+
const text = await intentLine.generate({
|
|
2951
|
+
content: parsed.content,
|
|
2952
|
+
history,
|
|
2953
|
+
tenantId: auth.tenantId,
|
|
2954
|
+
userId: auth.userId,
|
|
2955
|
+
threadId,
|
|
2956
|
+
context: auth.context,
|
|
2957
|
+
signal: controller.signal
|
|
2958
|
+
});
|
|
2959
|
+
const line = typeof text === "string" ? text.trim().replace(/\s+/g, " ") : "";
|
|
2960
|
+
return line ? line.slice(0, INTENT_LINE_MAX_CHARS) : null;
|
|
2961
|
+
})().catch((err) => {
|
|
2962
|
+
if (!controller.signal.aborted) {
|
|
2963
|
+
log("[chat-routes] intent line failed; showing none", { error: err instanceof Error ? err.message : String(err) });
|
|
2964
|
+
}
|
|
2965
|
+
return null;
|
|
2966
|
+
});
|
|
2967
|
+
return Promise.race([generated, deadline]).finally(() => clearTimeout(timer));
|
|
2968
|
+
}
|
|
2969
|
+
const runAdmittedTurn2 = createAdmittedTurnRunner({
|
|
2970
|
+
options,
|
|
2971
|
+
draftStore,
|
|
2972
|
+
draftTuning,
|
|
2973
|
+
progressMessages,
|
|
2974
|
+
traceOptions,
|
|
2975
|
+
traceSink,
|
|
2976
|
+
startIntentLine,
|
|
2977
|
+
log
|
|
2978
|
+
});
|
|
2979
|
+
async function turn(request, ctx) {
|
|
2980
|
+
const requestContext = ctx?.waitUntil ? { ...ctx, waitUntil: ctx.waitUntil.bind(ctx) } : ctx;
|
|
2981
|
+
const admitted = await admitTurn(request, createTurnTrace({ d1QueryCounter: currentD1QueryCounter() }), options, requestContext?.waitUntil);
|
|
2982
|
+
if (!admitted.ok) return admitted.response;
|
|
2983
|
+
if (request.headers.get(TURN_PROGRESS_HEADER) !== TURN_PROGRESS_FIRST) return runTurn(request, requestContext, admitted);
|
|
2984
|
+
return progressFirstTurn(request, requestContext, admitted);
|
|
2985
|
+
}
|
|
2986
|
+
function progressFirstTurn(request, ctx, admitted) {
|
|
2987
|
+
const receivedAt = Date.now();
|
|
2988
|
+
const encoder = new TextEncoder();
|
|
2989
|
+
const { readable, writable } = new TransformStream();
|
|
2990
|
+
const writer = writable.getWriter();
|
|
2991
|
+
let clientGone = false;
|
|
2992
|
+
const write = async (chunk) => {
|
|
2993
|
+
if (clientGone) return;
|
|
2994
|
+
try {
|
|
2995
|
+
await writer.write(chunk);
|
|
2996
|
+
} catch {
|
|
2997
|
+
clientGone = true;
|
|
2998
|
+
}
|
|
2999
|
+
};
|
|
3000
|
+
const writeEvent = (event) => write(encoder.encode(`${JSON.stringify(event)}
|
|
3001
|
+
`));
|
|
3002
|
+
const progress = (phase, message) => {
|
|
3003
|
+
void writeEvent(buildTurnPhaseEvent(phase, message, { sinceRequestMs: Date.now() - receivedAt }));
|
|
3004
|
+
};
|
|
3005
|
+
progress("accepted", progressMessages.accepted);
|
|
3006
|
+
const intent = startIntentLine(admitted);
|
|
3007
|
+
let piping = false;
|
|
3008
|
+
let atLineStart = true;
|
|
3009
|
+
let pendingIntent = null;
|
|
3010
|
+
if (intent) {
|
|
3011
|
+
void intent.then((text) => {
|
|
3012
|
+
if (!text || clientGone) return;
|
|
3013
|
+
const event = { type: TURN_INTENT_EVENT, data: { text, sinceRequestMs: Date.now() - receivedAt } };
|
|
3014
|
+
if (!piping || atLineStart) void writeEvent(event);
|
|
3015
|
+
else pendingIntent = event;
|
|
3016
|
+
});
|
|
3017
|
+
}
|
|
3018
|
+
const pump = (async () => {
|
|
3019
|
+
let inner;
|
|
3020
|
+
try {
|
|
3021
|
+
inner = await runTurn(request, ctx, admitted, progress, intent);
|
|
3022
|
+
} catch (err) {
|
|
3023
|
+
log("[chat-routes] turn failed before it streamed", { error: err instanceof Error ? err.message : String(err) });
|
|
3024
|
+
inner = err instanceof Response ? err : Response.json({ error: "The turn failed before it started. Try again." }, { status: 500 });
|
|
3025
|
+
}
|
|
3026
|
+
if (!inner.body || !inner.headers.get("x-turn-id")) {
|
|
3027
|
+
await writeEvent(await turnResponseEventFor(inner));
|
|
3028
|
+
} else {
|
|
3029
|
+
const reader = inner.body.getReader();
|
|
3030
|
+
piping = true;
|
|
3031
|
+
try {
|
|
3032
|
+
for (; ; ) {
|
|
3033
|
+
const { done, value } = await reader.read();
|
|
3034
|
+
if (done) break;
|
|
3035
|
+
atLineStart = value.length > 0 && value[value.length - 1] === 10;
|
|
3036
|
+
await write(value);
|
|
3037
|
+
if (atLineStart && pendingIntent) {
|
|
3038
|
+
const event = pendingIntent;
|
|
3039
|
+
pendingIntent = null;
|
|
3040
|
+
await writeEvent(event);
|
|
3041
|
+
}
|
|
3042
|
+
if (clientGone) {
|
|
3043
|
+
await reader.cancel().catch(() => {
|
|
3044
|
+
});
|
|
3045
|
+
break;
|
|
3046
|
+
}
|
|
3047
|
+
}
|
|
3048
|
+
} catch (err) {
|
|
3049
|
+
log("[chat-routes] progress-first pipe failed", { error: err instanceof Error ? err.message : String(err) });
|
|
3050
|
+
}
|
|
3051
|
+
}
|
|
3052
|
+
if (!clientGone) await writer.close().catch(() => {
|
|
3053
|
+
});
|
|
3054
|
+
})();
|
|
3055
|
+
if (ctx?.waitUntil) ctx.waitUntil(pump);
|
|
3056
|
+
else void pump.catch(() => {
|
|
3057
|
+
});
|
|
3058
|
+
return new Response(readable, {
|
|
3059
|
+
headers: {
|
|
3060
|
+
"Content-Type": "application/x-ndjson",
|
|
3061
|
+
"Cache-Control": "no-cache",
|
|
3062
|
+
[TURN_PROGRESS_HEADER]: TURN_PROGRESS_FIRST
|
|
3063
|
+
}
|
|
3064
|
+
});
|
|
3065
|
+
}
|
|
3066
|
+
function startAfterAuthorize(request, admitted, waitUntil) {
|
|
3067
|
+
let release;
|
|
3068
|
+
const { parsed, auth, trace } = admitted;
|
|
3069
|
+
if (options.afterAuthorize) {
|
|
3070
|
+
try {
|
|
3071
|
+
const started = options.afterAuthorize({
|
|
3072
|
+
request,
|
|
3073
|
+
waitUntil,
|
|
3074
|
+
body: parsed.payload,
|
|
3075
|
+
tenantId: auth.tenantId,
|
|
3076
|
+
userId: auth.userId,
|
|
3077
|
+
threadId: parsed.payload.threadId,
|
|
3078
|
+
context: auth.context,
|
|
3079
|
+
trace
|
|
3080
|
+
});
|
|
3081
|
+
if (started) release = started;
|
|
3082
|
+
} catch (err) {
|
|
3083
|
+
log("[chat-routes] afterAuthorize failed; the turn goes on without it", { error: err instanceof Error ? err.message : String(err) });
|
|
3084
|
+
}
|
|
3085
|
+
}
|
|
3086
|
+
return earlyTurnWork(release, log);
|
|
3087
|
+
}
|
|
3088
|
+
async function runTurn(request, ctx, admitted, progress, startedIntent) {
|
|
3089
|
+
const early = startAfterAuthorize(request, admitted, ctx?.waitUntil);
|
|
3090
|
+
try {
|
|
3091
|
+
return await runAdmittedTurn2(request, ctx, admitted, early, progress, startedIntent);
|
|
3092
|
+
} finally {
|
|
3093
|
+
const freed = early.release();
|
|
3094
|
+
if (ctx?.waitUntil) ctx.waitUntil(freed);
|
|
3095
|
+
else void freed;
|
|
3096
|
+
}
|
|
3097
|
+
}
|
|
3098
|
+
async function replay(request, params) {
|
|
3099
|
+
const turnId = params.turnId?.trim();
|
|
3100
|
+
if (!turnId) return Response.json({ error: "Missing turnId" }, { status: 400 });
|
|
3101
|
+
const auth = await options.authorize({ request, intent: "replay", turnId });
|
|
3102
|
+
if (!auth.ok) return auth.response;
|
|
3103
|
+
if (auth.intent !== void 0 && auth.intent !== "replay") {
|
|
3104
|
+
return Response.json({ error: "Authorization did not allow this replay.", code: "chat_authorization_intent_mismatch" }, { status: 403 });
|
|
3105
|
+
}
|
|
3106
|
+
const fromSeqRaw = new URL(request.url).searchParams.get("fromSeq");
|
|
3107
|
+
const fromSeq = fromSeqRaw ? Math.max(0, Math.trunc(Number(fromSeqRaw)) || 0) : 0;
|
|
3108
|
+
const encoder = new TextEncoder();
|
|
3109
|
+
const events = replayTurnEvents({
|
|
3110
|
+
store: options.turnStore,
|
|
3111
|
+
turnId,
|
|
3112
|
+
fromSeq
|
|
3113
|
+
});
|
|
3114
|
+
const body = new ReadableStream({
|
|
3115
|
+
async pull(controller) {
|
|
3116
|
+
const { done, value } = await events.next();
|
|
3117
|
+
if (done) {
|
|
3118
|
+
controller.close();
|
|
3119
|
+
return;
|
|
3120
|
+
}
|
|
3121
|
+
controller.enqueue(encoder.encode(`${stampReplaySeq(value)}
|
|
3122
|
+
`));
|
|
3123
|
+
},
|
|
3124
|
+
cancel() {
|
|
3125
|
+
void events.return(void 0);
|
|
3126
|
+
}
|
|
3127
|
+
});
|
|
3128
|
+
return new Response(body, {
|
|
3129
|
+
headers: {
|
|
3130
|
+
"Content-Type": "application/x-ndjson",
|
|
3131
|
+
"Cache-Control": "no-cache"
|
|
3132
|
+
}
|
|
3133
|
+
});
|
|
3134
|
+
}
|
|
3135
|
+
async function running(request) {
|
|
3136
|
+
const threadId = new URL(request.url).searchParams.get("threadId")?.trim();
|
|
3137
|
+
if (!threadId) return Response.json({ error: "Missing threadId" }, { status: 400 });
|
|
3138
|
+
const auth = await options.authorize({ request, intent: "running", threadId });
|
|
3139
|
+
if (!auth.ok) return auth.response;
|
|
3140
|
+
if (auth.intent !== void 0 && auth.intent !== "running") {
|
|
3141
|
+
return Response.json({ error: "Authorization did not allow this running-turn read.", code: "chat_authorization_intent_mismatch" }, { status: 403 });
|
|
3142
|
+
}
|
|
3143
|
+
const ids = await options.turnStore.listRunning?.(threadId) ?? [];
|
|
3144
|
+
return Response.json({ running: ids });
|
|
3145
|
+
}
|
|
3146
|
+
return {
|
|
3147
|
+
turn: (request, ctx) => withD1QueryCount(() => turn(request, ctx)),
|
|
3148
|
+
cancel: (request) => cancelChatTurn(request, { authorize: options.authorize, cancel: options.cancel, log }),
|
|
3149
|
+
replay,
|
|
3150
|
+
running,
|
|
3151
|
+
interactions: options.interactions ? createInteractionAnswerRoute(options.interactions) : null
|
|
3152
|
+
};
|
|
3153
|
+
}
|
|
3154
|
+
|
|
3155
|
+
// src/chat-routes/gateway-adapter.ts
|
|
3156
|
+
async function* streamChatRouteAsSandboxEvents(options) {
|
|
3157
|
+
const parentSignal = options.signal ?? options.request.signal;
|
|
3158
|
+
if (parentSignal.aborted) return;
|
|
3159
|
+
let reader;
|
|
3160
|
+
const stopForwarding = () => void reader?.cancel();
|
|
3161
|
+
parentSignal.addEventListener("abort", stopForwarding, { once: true });
|
|
3162
|
+
const request = new Request(options.request.url, {
|
|
3163
|
+
...chatTurnRequestInit(options.payload),
|
|
3164
|
+
headers: options.request.headers
|
|
3165
|
+
});
|
|
3166
|
+
request.headers.set("Content-Type", "application/json");
|
|
3167
|
+
let complete = false;
|
|
3168
|
+
try {
|
|
3169
|
+
const response = await options.routes.turn(
|
|
3170
|
+
request,
|
|
3171
|
+
{
|
|
3172
|
+
...options.waitUntil ? { waitUntil: options.waitUntil } : {},
|
|
3173
|
+
...options.executionLimits ? { executionLimits: options.executionLimits } : {}
|
|
3174
|
+
}
|
|
3175
|
+
);
|
|
3176
|
+
if (!response.ok) {
|
|
3177
|
+
await response.body?.cancel().catch(() => void 0);
|
|
3178
|
+
throw new Error(`chat route rejected the gateway turn (${response.status})`);
|
|
3179
|
+
}
|
|
3180
|
+
if (!response.body) throw new Error("chat route returned no stream");
|
|
3181
|
+
reader = response.body.getReader();
|
|
3182
|
+
if (parentSignal.aborted) {
|
|
3183
|
+
await reader.cancel().catch(() => void 0);
|
|
3184
|
+
return;
|
|
3185
|
+
}
|
|
3186
|
+
const decoder = new TextDecoder();
|
|
3187
|
+
let buffer = "";
|
|
3188
|
+
for (; ; ) {
|
|
3189
|
+
const chunk = await reader.read();
|
|
3190
|
+
if (chunk.done) {
|
|
3191
|
+
complete = true;
|
|
3192
|
+
buffer += decoder.decode();
|
|
3193
|
+
const event = toSandboxEvent(parseEvent(buffer));
|
|
3194
|
+
if (event) yield event;
|
|
3195
|
+
break;
|
|
3196
|
+
}
|
|
3197
|
+
buffer += decoder.decode(chunk.value, { stream: true });
|
|
3198
|
+
const lines = buffer.split("\n");
|
|
3199
|
+
buffer = lines.pop() ?? "";
|
|
3200
|
+
for (const line of lines) {
|
|
3201
|
+
const event = toSandboxEvent(parseEvent(line));
|
|
3202
|
+
if (event) yield event;
|
|
3203
|
+
}
|
|
3204
|
+
}
|
|
3205
|
+
} finally {
|
|
3206
|
+
if (!complete) await reader?.cancel().catch(() => void 0);
|
|
3207
|
+
reader?.releaseLock();
|
|
3208
|
+
parentSignal.removeEventListener("abort", stopForwarding);
|
|
3209
|
+
}
|
|
3210
|
+
}
|
|
3211
|
+
function parseEvent(line) {
|
|
3212
|
+
if (!line.trim()) return null;
|
|
3213
|
+
let value;
|
|
3214
|
+
try {
|
|
3215
|
+
value = JSON.parse(line);
|
|
3216
|
+
} catch {
|
|
3217
|
+
throw new Error("chat route returned invalid NDJSON");
|
|
3218
|
+
}
|
|
3219
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
|
3220
|
+
throw new Error("chat route returned a non-object event");
|
|
3221
|
+
}
|
|
3222
|
+
const event = value;
|
|
3223
|
+
if (event.type !== void 0 && typeof event.type !== "string") {
|
|
3224
|
+
throw new Error("chat route returned an invalid event type");
|
|
3225
|
+
}
|
|
3226
|
+
const data = event.data;
|
|
3227
|
+
if (data !== void 0 && (!data || typeof data !== "object" || Array.isArray(data))) {
|
|
3228
|
+
throw new Error("chat route returned invalid event data");
|
|
3229
|
+
}
|
|
3230
|
+
const dataUsage = data && data.usage;
|
|
3231
|
+
if (dataUsage !== void 0 && (!dataUsage || typeof dataUsage !== "object" || Array.isArray(dataUsage))) {
|
|
3232
|
+
throw new Error("chat route returned invalid data.usage");
|
|
3233
|
+
}
|
|
3234
|
+
if (event.text !== void 0 && typeof event.text !== "string") {
|
|
3235
|
+
throw new Error("chat route returned an invalid text event");
|
|
3236
|
+
}
|
|
3237
|
+
const usage = event.usage;
|
|
3238
|
+
if (usage !== void 0 && (!usage || typeof usage !== "object" || Array.isArray(usage))) {
|
|
3239
|
+
throw new Error("chat route returned invalid usage");
|
|
3240
|
+
}
|
|
3241
|
+
if (event.toolName !== void 0 && typeof event.toolName !== "string") {
|
|
3242
|
+
throw new Error("chat route returned an invalid tool name");
|
|
3243
|
+
}
|
|
3244
|
+
return {
|
|
3245
|
+
...typeof event.type === "string" ? { type: event.type } : {},
|
|
3246
|
+
...data ? { data } : {},
|
|
3247
|
+
...typeof event.text === "string" ? { text: event.text } : {},
|
|
3248
|
+
...usage ? { usage } : {},
|
|
3249
|
+
...typeof event.toolName === "string" ? { toolName: event.toolName } : {}
|
|
3250
|
+
};
|
|
3251
|
+
}
|
|
3252
|
+
function isNonNegativeSafeInteger(value) {
|
|
3253
|
+
return typeof value === "number" && Number.isSafeInteger(value) && value >= 0;
|
|
3254
|
+
}
|
|
3255
|
+
function aliasedSafeInteger(usage, field, alias) {
|
|
3256
|
+
const value = usage[field];
|
|
3257
|
+
const aliasValue = alias ? usage[alias] : void 0;
|
|
3258
|
+
for (const candidate of [value, aliasValue]) {
|
|
3259
|
+
if (candidate !== void 0 && !isNonNegativeSafeInteger(candidate)) {
|
|
3260
|
+
throw new Error(`chat route returned invalid usage.${field}`);
|
|
3261
|
+
}
|
|
3262
|
+
}
|
|
3263
|
+
if (value !== void 0 && aliasValue !== void 0 && value !== aliasValue) {
|
|
3264
|
+
throw new Error(`chat route returned conflicting usage.${field}`);
|
|
3265
|
+
}
|
|
3266
|
+
return value ?? aliasValue;
|
|
3267
|
+
}
|
|
3268
|
+
function aliasedNonNegativeNumber(usage, field, alias) {
|
|
3269
|
+
const value = usage[field];
|
|
3270
|
+
const aliasValue = alias ? usage[alias] : void 0;
|
|
3271
|
+
for (const candidate of [value, aliasValue]) {
|
|
3272
|
+
if (candidate !== void 0 && (typeof candidate !== "number" || !Number.isFinite(candidate) || candidate < 0)) {
|
|
3273
|
+
throw new Error(`chat route returned invalid usage.${field}`);
|
|
3274
|
+
}
|
|
3275
|
+
}
|
|
3276
|
+
if (value !== void 0 && aliasValue !== void 0 && value !== aliasValue) {
|
|
3277
|
+
throw new Error(`chat route returned conflicting usage.${field}`);
|
|
3278
|
+
}
|
|
3279
|
+
return value ?? aliasValue;
|
|
3280
|
+
}
|
|
3281
|
+
function providerUsageUpdate(value) {
|
|
3282
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) return null;
|
|
3283
|
+
const source = value;
|
|
3284
|
+
const usage = {};
|
|
3285
|
+
const inputTokens = aliasedSafeInteger(source, "inputTokens", "promptTokens");
|
|
3286
|
+
const outputTokens = aliasedSafeInteger(source, "outputTokens", "completionTokens");
|
|
3287
|
+
const reasoningTokens = aliasedSafeInteger(source, "reasoningTokens");
|
|
3288
|
+
const toolTokens = aliasedSafeInteger(source, "toolTokens");
|
|
3289
|
+
const toolCallCount = aliasedSafeInteger(source, "toolCallCount");
|
|
3290
|
+
const providerCostUsd = aliasedNonNegativeNumber(source, "providerCostUsd", "costUsd");
|
|
3291
|
+
if (inputTokens !== void 0) usage.inputTokens = inputTokens;
|
|
3292
|
+
if (outputTokens !== void 0) usage.outputTokens = outputTokens;
|
|
3293
|
+
if (reasoningTokens !== void 0) usage.reasoningTokens = reasoningTokens;
|
|
3294
|
+
if (toolTokens !== void 0) usage.toolTokens = toolTokens;
|
|
3295
|
+
if (toolCallCount !== void 0) usage.toolCallCount = toolCallCount;
|
|
3296
|
+
if (providerCostUsd !== void 0) usage.providerCostUsd = providerCostUsd;
|
|
3297
|
+
if (source.budgetEnforced !== void 0) {
|
|
3298
|
+
if (typeof source.budgetEnforced !== "boolean") {
|
|
3299
|
+
throw new Error("chat route returned invalid usage.budgetEnforced");
|
|
3300
|
+
}
|
|
3301
|
+
usage.budgetEnforced = source.budgetEnforced;
|
|
3302
|
+
}
|
|
3303
|
+
return Object.keys(usage).length > 0 ? usage : null;
|
|
3304
|
+
}
|
|
3305
|
+
function toSandboxEvent(event) {
|
|
3306
|
+
if (!event) return null;
|
|
3307
|
+
if (event.type === "text") {
|
|
3308
|
+
if (event.text === void 0) throw new Error("chat route text event had no text");
|
|
3309
|
+
return {
|
|
3310
|
+
type: "message.part.updated",
|
|
3311
|
+
data: { part: { type: "text" }, delta: event.text }
|
|
3312
|
+
};
|
|
3313
|
+
}
|
|
3314
|
+
if (event.type === "reasoning") return null;
|
|
3315
|
+
if (event.type === "usage" || event.usage || event.data?.usage) {
|
|
3316
|
+
const usage = providerUsageUpdate(event.usage ?? event.data?.usage);
|
|
3317
|
+
if (!usage) return null;
|
|
3318
|
+
return { type: "usage", data: { usage } };
|
|
3319
|
+
}
|
|
3320
|
+
if (event.type === "tool_result") {
|
|
3321
|
+
if (!event.toolName) throw new Error("chat route tool result had no tool name");
|
|
3322
|
+
return { type: "tool_result", data: { tool: { name: event.toolName } } };
|
|
3323
|
+
}
|
|
3324
|
+
if (event.type === "input-required" && !event.data?.inputRequired) {
|
|
3325
|
+
const prompt = typeof event.data?.prompt === "string" ? event.data.prompt : void 0;
|
|
3326
|
+
return {
|
|
3327
|
+
type: "input-required",
|
|
3328
|
+
data: { inputRequired: prompt ? { prompt } : {} }
|
|
3329
|
+
};
|
|
3330
|
+
}
|
|
3331
|
+
return {
|
|
3332
|
+
...event.type ? { type: event.type } : {},
|
|
3333
|
+
...event.data ? { data: event.data } : {}
|
|
3334
|
+
};
|
|
3335
|
+
}
|
|
3336
|
+
|
|
3337
|
+
export {
|
|
3338
|
+
createRouterTurnProducer,
|
|
3339
|
+
HYBRID_TURN_GUIDANCE,
|
|
3340
|
+
startHybridBox,
|
|
3341
|
+
turnPromptText,
|
|
3342
|
+
turnCarriesFiles,
|
|
3343
|
+
routerTurnHistory,
|
|
3344
|
+
createHybridTurnProducer,
|
|
3345
|
+
isCommittingSandboxEvent,
|
|
3346
|
+
summarizeFailoverReason,
|
|
3347
|
+
isLiveLifecycleEvent,
|
|
3348
|
+
classifyTerminalFailure,
|
|
3349
|
+
DEFAULT_MODEL_STREAM_OPEN_TIMEOUT_MS,
|
|
3350
|
+
DEFAULT_MODEL_FIRST_RESPONSE_TIMEOUT_MS,
|
|
3351
|
+
ModelFailoverTimeoutError,
|
|
3352
|
+
MAX_EMPTY_TURN_RETRIES,
|
|
3353
|
+
resolveEmptyTurnRetries,
|
|
3354
|
+
streamWithModelFailover,
|
|
3355
|
+
createSandboxChatProducer,
|
|
3356
|
+
withProducerStream,
|
|
3357
|
+
cancelChatTurn,
|
|
3358
|
+
observeTurnCancellation,
|
|
3359
|
+
INTENT_LINE_TIMEOUT_MS,
|
|
3360
|
+
INTENT_LINE_HISTORY_MESSAGES,
|
|
3361
|
+
INTENT_LINE_MAX_CHARS,
|
|
3362
|
+
createChatTurnRoutes,
|
|
3363
|
+
streamChatRouteAsSandboxEvents
|
|
3364
|
+
};
|
|
3365
|
+
//# sourceMappingURL=chunk-MM3GOLHO.js.map
|