@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
package/dist/chunk-EFQUAWWF.js
DELETED
|
@@ -1,1749 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
redactErrorMessage
|
|
3
|
-
} from "./chunk-MH74DY2I.js";
|
|
4
|
-
import {
|
|
5
|
-
createInteractionAnswerRoute
|
|
6
|
-
} from "./chunk-KH4QSBNI.js";
|
|
7
|
-
import {
|
|
8
|
-
parseJsonObjectBody
|
|
9
|
-
} from "./chunk-WP2GYFZ4.js";
|
|
10
|
-
import {
|
|
11
|
-
noteMeasuredTurnFacts
|
|
12
|
-
} from "./chunk-6R73V3Y5.js";
|
|
13
|
-
import {
|
|
14
|
-
normalizeClientTurnId,
|
|
15
|
-
resolveChatTurn
|
|
16
|
-
} from "./chunk-YG66IHDU.js";
|
|
17
|
-
import {
|
|
18
|
-
coalesceDeltas,
|
|
19
|
-
createBufferedTurnTap,
|
|
20
|
-
replayTurnEvents,
|
|
21
|
-
stampReplaySeq
|
|
22
|
-
} from "./chunk-ISP6ZH3G.js";
|
|
23
|
-
import {
|
|
24
|
-
currentD1QueryCounter,
|
|
25
|
-
withD1QueryCount
|
|
26
|
-
} from "./chunk-MM6RLOXF.js";
|
|
27
|
-
import {
|
|
28
|
-
mentionInputToPart,
|
|
29
|
-
toChatMessageParts
|
|
30
|
-
} from "./chunk-FAXSYC2C.js";
|
|
31
|
-
import {
|
|
32
|
-
ChatTurnInputError,
|
|
33
|
-
TURN_INTENT_EVENT,
|
|
34
|
-
TURN_PROGRESS_FIRST,
|
|
35
|
-
TURN_PROGRESS_HEADER,
|
|
36
|
-
TURN_ROUTE_PHASE_MESSAGES,
|
|
37
|
-
assertPromptPartsWithinCap,
|
|
38
|
-
buildTurnPhaseEvent,
|
|
39
|
-
chatTurnRequestInit,
|
|
40
|
-
parseChatTurnParts,
|
|
41
|
-
parseFileMentions,
|
|
42
|
-
turnResponseEventFor
|
|
43
|
-
} from "./chunk-G7M2C72Q.js";
|
|
44
|
-
import {
|
|
45
|
-
checkModelPolicy
|
|
46
|
-
} from "./chunk-XELWWXJJ.js";
|
|
47
|
-
import {
|
|
48
|
-
createIntelligenceTurnTraceSink,
|
|
49
|
-
createTurnTrace,
|
|
50
|
-
formatServerTiming,
|
|
51
|
-
otlpTraceId
|
|
52
|
-
} from "./chunk-PCAY23GS.js";
|
|
53
|
-
|
|
54
|
-
// src/chat-routes/draft-persistence.ts
|
|
55
|
-
var CONTENT_EVENT_TYPES = /* @__PURE__ */ new Set([
|
|
56
|
-
"text",
|
|
57
|
-
"reasoning",
|
|
58
|
-
"tool_call",
|
|
59
|
-
"tool_result",
|
|
60
|
-
"usage",
|
|
61
|
-
"notice",
|
|
62
|
-
"error",
|
|
63
|
-
"file",
|
|
64
|
-
"interaction",
|
|
65
|
-
"interaction.cancel",
|
|
66
|
-
"plan.submitted",
|
|
67
|
-
"message.part.updated"
|
|
68
|
-
]);
|
|
69
|
-
var DEFAULT_INTERVAL_MS = 2e3;
|
|
70
|
-
var DEFAULT_BACKOFF_BYTES = 262144;
|
|
71
|
-
var DEFAULT_MAX_DRAFT_TOOL_OUTPUT_BYTES = 32768;
|
|
72
|
-
function isDraftContentEvent(event) {
|
|
73
|
-
return typeof event?.type === "string" && CONTENT_EVENT_TYPES.has(event.type);
|
|
74
|
-
}
|
|
75
|
-
function capDraftToolOutput(part, maxBytes) {
|
|
76
|
-
if (maxBytes <= 0) return part;
|
|
77
|
-
if (String(part.type ?? "") !== "tool") return part;
|
|
78
|
-
const state = part.state;
|
|
79
|
-
if (!state || typeof state !== "object") return part;
|
|
80
|
-
const record = state;
|
|
81
|
-
const output = record.output;
|
|
82
|
-
if (output === void 0 || output === null) return part;
|
|
83
|
-
const serialized = typeof output === "string" ? output : safeStringify(output);
|
|
84
|
-
if (serialized.length <= maxBytes) return part;
|
|
85
|
-
const metadata = record.metadata && typeof record.metadata === "object" ? record.metadata : {};
|
|
86
|
-
return {
|
|
87
|
-
...part,
|
|
88
|
-
state: {
|
|
89
|
-
...record,
|
|
90
|
-
output: `${serialized.slice(0, maxBytes)}\u2026[draft-truncated ${serialized.length - maxBytes} chars]`,
|
|
91
|
-
metadata: { ...metadata, draftTruncated: true }
|
|
92
|
-
}
|
|
93
|
-
};
|
|
94
|
-
}
|
|
95
|
-
function safeStringify(value) {
|
|
96
|
-
try {
|
|
97
|
-
return JSON.stringify(value) ?? "";
|
|
98
|
-
} catch {
|
|
99
|
-
return String(value);
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
function createAssistantDraftWriter(options) {
|
|
103
|
-
const log = options.log ?? (() => {
|
|
104
|
-
});
|
|
105
|
-
const baseIntervalMs = options.intervalMs ?? DEFAULT_INTERVAL_MS;
|
|
106
|
-
const backoffBytes = options.backoffBytes ?? DEFAULT_BACKOFF_BYTES;
|
|
107
|
-
const maxToolOutput = options.maxDraftToolOutputBytes ?? DEFAULT_MAX_DRAFT_TOOL_OUTPUT_BYTES;
|
|
108
|
-
let dirty = false;
|
|
109
|
-
let closed = false;
|
|
110
|
-
let inFlight;
|
|
111
|
-
let lastWriteAt = 0;
|
|
112
|
-
let lastBlobBytes = 0;
|
|
113
|
-
let rowId;
|
|
114
|
-
let writes = 0;
|
|
115
|
-
function currentIntervalMs() {
|
|
116
|
-
if (lastBlobBytes >= backoffBytes * 10) return baseIntervalMs * 5;
|
|
117
|
-
if (lastBlobBytes >= backoffBytes) return Math.round(baseIntervalMs * 2.5);
|
|
118
|
-
return baseIntervalMs;
|
|
119
|
-
}
|
|
120
|
-
async function projectValues(snapshot) {
|
|
121
|
-
const transform = options.transformText;
|
|
122
|
-
const content = transform ? await transform(snapshot.content) : snapshot.content;
|
|
123
|
-
let parts;
|
|
124
|
-
if (snapshot.parts) {
|
|
125
|
-
const capped = snapshot.parts.map((part) => capDraftToolOutput(part, maxToolOutput));
|
|
126
|
-
const redacted = transform ? await Promise.all(
|
|
127
|
-
capped.map(
|
|
128
|
-
async (part) => String(part.type ?? "") === "text" ? { ...part, text: await transform(String(part.text ?? "")) } : part
|
|
129
|
-
)
|
|
130
|
-
) : capped;
|
|
131
|
-
parts = toChatMessageParts(redacted);
|
|
132
|
-
}
|
|
133
|
-
const usage = snapshot.usage ?? {};
|
|
134
|
-
return {
|
|
135
|
-
content,
|
|
136
|
-
...parts && parts.length > 0 ? { parts } : {},
|
|
137
|
-
...snapshot.model ? { model: snapshot.model } : {},
|
|
138
|
-
...usage.inputTokens !== void 0 ? { inputTokens: usage.inputTokens } : {},
|
|
139
|
-
...usage.outputTokens !== void 0 ? { outputTokens: usage.outputTokens } : {},
|
|
140
|
-
...usage.reasoningTokens !== void 0 ? { reasoningTokens: usage.reasoningTokens } : {},
|
|
141
|
-
...usage.cacheReadTokens !== void 0 ? { cacheReadTokens: usage.cacheReadTokens } : {},
|
|
142
|
-
...usage.cacheWriteTokens !== void 0 ? { cacheWriteTokens: usage.cacheWriteTokens } : {},
|
|
143
|
-
...usage.costUsd !== void 0 ? { costUsd: usage.costUsd } : {}
|
|
144
|
-
};
|
|
145
|
-
}
|
|
146
|
-
async function adoptRow() {
|
|
147
|
-
if (rowId) return rowId;
|
|
148
|
-
const existing = (await options.store.listMessages(options.threadId)).find(
|
|
149
|
-
(message) => message.id === options.messageId
|
|
150
|
-
);
|
|
151
|
-
if (existing) rowId = existing.id;
|
|
152
|
-
return rowId;
|
|
153
|
-
}
|
|
154
|
-
async function writeOnce(values) {
|
|
155
|
-
if (await adoptRow()) {
|
|
156
|
-
await options.store.updateMessage(rowId, values);
|
|
157
|
-
writes += 1;
|
|
158
|
-
return;
|
|
159
|
-
}
|
|
160
|
-
const inserted = await options.store.appendMessage({
|
|
161
|
-
id: options.messageId,
|
|
162
|
-
threadId: options.threadId,
|
|
163
|
-
role: "assistant",
|
|
164
|
-
...values
|
|
165
|
-
});
|
|
166
|
-
rowId = rowIdOf(inserted) ?? options.messageId;
|
|
167
|
-
writes += 1;
|
|
168
|
-
}
|
|
169
|
-
function trigger() {
|
|
170
|
-
if (closed) return;
|
|
171
|
-
if (!dirty) return;
|
|
172
|
-
if (inFlight) return;
|
|
173
|
-
const now = Date.now();
|
|
174
|
-
if (lastWriteAt !== 0 && now - lastWriteAt < currentIntervalMs()) return;
|
|
175
|
-
const snapshot = options.snapshot();
|
|
176
|
-
if (!snapshot) return;
|
|
177
|
-
if (!snapshot.content && (!snapshot.parts || snapshot.parts.length === 0)) return;
|
|
178
|
-
dirty = false;
|
|
179
|
-
lastWriteAt = now;
|
|
180
|
-
inFlight = (async () => {
|
|
181
|
-
try {
|
|
182
|
-
const values = await projectValues(snapshot);
|
|
183
|
-
lastBlobBytes = values.parts ? safeStringify(values.parts).length : 0;
|
|
184
|
-
await writeOnce(values);
|
|
185
|
-
} catch (err) {
|
|
186
|
-
log("[chat-routes] incremental assistant persistence failed", {
|
|
187
|
-
messageId: options.messageId,
|
|
188
|
-
error: err instanceof Error ? err.message : String(err)
|
|
189
|
-
});
|
|
190
|
-
} finally {
|
|
191
|
-
inFlight = void 0;
|
|
192
|
-
if (dirty && !closed) trigger();
|
|
193
|
-
}
|
|
194
|
-
})();
|
|
195
|
-
}
|
|
196
|
-
return {
|
|
197
|
-
notify(event) {
|
|
198
|
-
if (closed) return;
|
|
199
|
-
if (isDraftContentEvent(event)) dirty = true;
|
|
200
|
-
trigger();
|
|
201
|
-
},
|
|
202
|
-
async close() {
|
|
203
|
-
closed = true;
|
|
204
|
-
if (inFlight) await inFlight;
|
|
205
|
-
},
|
|
206
|
-
async finalize(values) {
|
|
207
|
-
closed = true;
|
|
208
|
-
if (inFlight) await inFlight;
|
|
209
|
-
await writeOnce(values);
|
|
210
|
-
},
|
|
211
|
-
rowId: () => rowId,
|
|
212
|
-
async discard() {
|
|
213
|
-
closed = true;
|
|
214
|
-
if (inFlight) await inFlight;
|
|
215
|
-
if (!options.store.deleteMessage) return;
|
|
216
|
-
try {
|
|
217
|
-
if (!await adoptRow()) return;
|
|
218
|
-
await options.store.deleteMessage(rowId);
|
|
219
|
-
rowId = void 0;
|
|
220
|
-
} catch (err) {
|
|
221
|
-
log("[chat-routes] draft assistant row discard failed", {
|
|
222
|
-
messageId: options.messageId,
|
|
223
|
-
error: err instanceof Error ? err.message : String(err)
|
|
224
|
-
});
|
|
225
|
-
}
|
|
226
|
-
},
|
|
227
|
-
writeCount: () => writes
|
|
228
|
-
};
|
|
229
|
-
}
|
|
230
|
-
function storeSupportsDraftPersistence(store) {
|
|
231
|
-
return typeof store.updateMessage === "function";
|
|
232
|
-
}
|
|
233
|
-
function rowIdOf(inserted) {
|
|
234
|
-
const id = inserted?.id;
|
|
235
|
-
return typeof id === "string" && id ? id : null;
|
|
236
|
-
}
|
|
237
|
-
function assistantRowIdForTurn(turnKey) {
|
|
238
|
-
return `assistant:${turnKey}`;
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
// src/chat-routes/turn-route-contracts.ts
|
|
242
|
-
var INTENT_LINE_TIMEOUT_MS = 1500;
|
|
243
|
-
var INTENT_LINE_HISTORY_MESSAGES = 6;
|
|
244
|
-
var INTENT_LINE_MAX_CHARS = 240;
|
|
245
|
-
|
|
246
|
-
// src/chat-routes/turn-prefetch.ts
|
|
247
|
-
function startTurnPrefetch(prefetch, input) {
|
|
248
|
-
let started;
|
|
249
|
-
const start = (identity) => {
|
|
250
|
-
const abort = new AbortController();
|
|
251
|
-
const result = input.trace.measure("admission.prefetch", "admission", async () => prefetch({
|
|
252
|
-
...input,
|
|
253
|
-
tenantId: identity.tenantId,
|
|
254
|
-
userId: identity.userId,
|
|
255
|
-
signal: abort.signal
|
|
256
|
-
}));
|
|
257
|
-
result.catch(() => {
|
|
258
|
-
});
|
|
259
|
-
started = { identity: { ...identity }, abort, result };
|
|
260
|
-
};
|
|
261
|
-
return {
|
|
262
|
-
identified(identity) {
|
|
263
|
-
if (!started) start(identity);
|
|
264
|
-
},
|
|
265
|
-
admitted(identity) {
|
|
266
|
-
if (started && (started.identity.tenantId !== identity.tenantId || started.identity.userId !== identity.userId)) {
|
|
267
|
-
started.abort.abort();
|
|
268
|
-
started = void 0;
|
|
269
|
-
}
|
|
270
|
-
if (!started) start(identity);
|
|
271
|
-
return started.result;
|
|
272
|
-
},
|
|
273
|
-
discard() {
|
|
274
|
-
started?.abort.abort();
|
|
275
|
-
started = void 0;
|
|
276
|
-
}
|
|
277
|
-
};
|
|
278
|
-
}
|
|
279
|
-
|
|
280
|
-
// src/chat-routes/turn-admission.ts
|
|
281
|
-
function errorResponse(err) {
|
|
282
|
-
return Response.json({ code: err.code, error: err.message }, { status: err.status });
|
|
283
|
-
}
|
|
284
|
-
function admissionServerTiming(trace) {
|
|
285
|
-
const now = Date.now();
|
|
286
|
-
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 }));
|
|
287
|
-
return formatServerTiming([{ name: "admission", durMs: now - trace.startedAt }, ...steps]);
|
|
288
|
-
}
|
|
289
|
-
async function namedModelRefusal(policy, payload, trace) {
|
|
290
|
-
const namedModel = typeof payload.model === "string" ? payload.model.trim() : "";
|
|
291
|
-
if (!policy || !namedModel) return null;
|
|
292
|
-
const decision = await trace.measure("admission.model_policy", "admission", () => checkModelPolicy(namedModel, policy.catalog, policy.filter, policy.context));
|
|
293
|
-
return decision.allowed ? null : Response.json({ code: "model_policy_denied", error: decision.reason }, { status: 403 });
|
|
294
|
-
}
|
|
295
|
-
function validateTurnBody(body, maxInlinePartBytes) {
|
|
296
|
-
const threadId = typeof body.threadId === "string" ? body.threadId.trim() : "";
|
|
297
|
-
if (!threadId) throw new ChatTurnInputError("Missing threadId");
|
|
298
|
-
const rawContent = body.content ?? body.message ?? "";
|
|
299
|
-
if (typeof rawContent !== "string") throw new ChatTurnInputError("content must be a string");
|
|
300
|
-
const content = rawContent.trim();
|
|
301
|
-
const fileParts = parseChatTurnParts(body.parts);
|
|
302
|
-
const mentions = parseFileMentions(body.mentions);
|
|
303
|
-
const hasAttachments = Array.isArray(body.attachments) && body.attachments.length > 0;
|
|
304
|
-
if (!content && fileParts.length === 0 && mentions.length === 0 && !hasAttachments) {
|
|
305
|
-
throw new ChatTurnInputError("Missing content (send text, parts, mentions, attachments, or any combination)");
|
|
306
|
-
}
|
|
307
|
-
assertPromptPartsWithinCap(fileParts, maxInlinePartBytes);
|
|
308
|
-
let turnId;
|
|
309
|
-
try {
|
|
310
|
-
turnId = normalizeClientTurnId(body.turnId);
|
|
311
|
-
} catch (err) {
|
|
312
|
-
throw new ChatTurnInputError(err instanceof Error ? err.message : "Invalid turnId");
|
|
313
|
-
}
|
|
314
|
-
const retryAttemptId = body.retryAttemptId;
|
|
315
|
-
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");
|
|
316
|
-
return {
|
|
317
|
-
// The VALIDATED, deduped mention list replaces the raw one on the payload,
|
|
318
|
-
// so every downstream seam (`authorize`, `contextGate`, `beforeTurn`,
|
|
319
|
-
// `produce`) reads checked paths and never the request's own.
|
|
320
|
-
payload: { ...body, threadId, content, mentions },
|
|
321
|
-
content,
|
|
322
|
-
fileParts,
|
|
323
|
-
mentions,
|
|
324
|
-
turnId
|
|
325
|
-
};
|
|
326
|
-
}
|
|
327
|
-
function userPartsWithFiles(userParts, fileParts, mentions) {
|
|
328
|
-
return toChatMessageParts([
|
|
329
|
-
...userParts,
|
|
330
|
-
...fileParts.map((part) => ({ ...part })),
|
|
331
|
-
...mentions.map((mention) => ({ ...mentionInputToPart(mention) }))
|
|
332
|
-
]);
|
|
333
|
-
}
|
|
334
|
-
|
|
335
|
-
// src/chat-routes/turn-execution.ts
|
|
336
|
-
import { deriveExecutionId, handleChatTurn } from "@tangle-network/agent-runtime/durable";
|
|
337
|
-
|
|
338
|
-
// src/chat-routes/dispatch-first.ts
|
|
339
|
-
var TurnAdmissionRefusedError = class extends Error {
|
|
340
|
-
constructor() {
|
|
341
|
-
super("The turn was refused after its producer started; its output was discarded.");
|
|
342
|
-
this.name = "TurnAdmissionRefusedError";
|
|
343
|
-
}
|
|
344
|
-
};
|
|
345
|
-
function dispatchProducerFirst(produce, args, trace) {
|
|
346
|
-
const abort = new AbortController();
|
|
347
|
-
let admit;
|
|
348
|
-
let reject;
|
|
349
|
-
const admitted = new Promise((resolve, fail) => {
|
|
350
|
-
admit = resolve;
|
|
351
|
-
reject = fail;
|
|
352
|
-
});
|
|
353
|
-
admitted.catch(() => {
|
|
354
|
-
});
|
|
355
|
-
let settled;
|
|
356
|
-
let stopped = false;
|
|
357
|
-
const queue = [];
|
|
358
|
-
let ended = false;
|
|
359
|
-
let failure;
|
|
360
|
-
let wake;
|
|
361
|
-
const notify = () => {
|
|
362
|
-
const resume = wake;
|
|
363
|
-
wake = void 0;
|
|
364
|
-
resume?.();
|
|
365
|
-
};
|
|
366
|
-
let source;
|
|
367
|
-
const handoffCompletion = args.handoffCompletion;
|
|
368
|
-
const started = trace.measure("admission.produce", "admission", () => produce({
|
|
369
|
-
...args,
|
|
370
|
-
admitted,
|
|
371
|
-
admissionSignal: abort.signal,
|
|
372
|
-
...handoffCompletion ? { handoffCompletion: () => admitted.then(() => handoffCompletion()) } : {}
|
|
373
|
-
}));
|
|
374
|
-
const draining = started.then(async (producer) => {
|
|
375
|
-
try {
|
|
376
|
-
source = producer.stream[Symbol.asyncIterator]();
|
|
377
|
-
for (; ; ) {
|
|
378
|
-
const next = await source.next();
|
|
379
|
-
if (next.done || stopped) break;
|
|
380
|
-
queue.push(next.value);
|
|
381
|
-
notify();
|
|
382
|
-
}
|
|
383
|
-
} catch (error) {
|
|
384
|
-
if (!stopped) failure = { error };
|
|
385
|
-
} finally {
|
|
386
|
-
ended = true;
|
|
387
|
-
notify();
|
|
388
|
-
}
|
|
389
|
-
}, (error) => {
|
|
390
|
-
failure = { error };
|
|
391
|
-
ended = true;
|
|
392
|
-
notify();
|
|
393
|
-
});
|
|
394
|
-
const stop = (reason) => {
|
|
395
|
-
if (stopped) return;
|
|
396
|
-
stopped = true;
|
|
397
|
-
abort.abort(reason);
|
|
398
|
-
queue.length = 0;
|
|
399
|
-
notify();
|
|
400
|
-
void started.then(() => source?.return?.(), () => {
|
|
401
|
-
}).catch(() => {
|
|
402
|
-
});
|
|
403
|
-
};
|
|
404
|
-
async function* replay() {
|
|
405
|
-
try {
|
|
406
|
-
for (; ; ) {
|
|
407
|
-
while (queue.length) yield queue.shift();
|
|
408
|
-
if (ended) break;
|
|
409
|
-
await new Promise((resolve) => {
|
|
410
|
-
wake = resolve;
|
|
411
|
-
});
|
|
412
|
-
}
|
|
413
|
-
await draining;
|
|
414
|
-
if (failure) throw failure.error;
|
|
415
|
-
} finally {
|
|
416
|
-
if (!ended) stop(new Error("The turn stopped reading its producer."));
|
|
417
|
-
}
|
|
418
|
-
}
|
|
419
|
-
return {
|
|
420
|
-
async take(final) {
|
|
421
|
-
if (settled) throw new Error(`dispatch-first producer already ${settled}`);
|
|
422
|
-
settled = "taken";
|
|
423
|
-
admit(final);
|
|
424
|
-
const producer = await started;
|
|
425
|
-
return Object.defineProperty(Object.create(producer), "stream", { value: replay(), enumerable: true });
|
|
426
|
-
},
|
|
427
|
-
refuse() {
|
|
428
|
-
if (settled) return;
|
|
429
|
-
settled = "refused";
|
|
430
|
-
reject(new TurnAdmissionRefusedError());
|
|
431
|
-
stop(new TurnAdmissionRefusedError());
|
|
432
|
-
}
|
|
433
|
-
};
|
|
434
|
-
}
|
|
435
|
-
function dispatchFirstFor(options, args, trace) {
|
|
436
|
-
const policy = options.dispatchFirst;
|
|
437
|
-
if (!policy || !(policy.when?.(args) ?? true)) return void 0;
|
|
438
|
-
return dispatchProducerFirst(options.produce, args, trace);
|
|
439
|
-
}
|
|
440
|
-
|
|
441
|
-
// src/chat-routes/turn-event-stream.ts
|
|
442
|
-
function failureReasonOf(data) {
|
|
443
|
-
if (!data) return void 0;
|
|
444
|
-
const message = data.message ?? data.error ?? data.reason;
|
|
445
|
-
if (typeof message === "string" && message.length > 0) return message;
|
|
446
|
-
return void 0;
|
|
447
|
-
}
|
|
448
|
-
function failureCodeOf(data) {
|
|
449
|
-
const code = data?.code;
|
|
450
|
-
return typeof code === "string" && code.length > 0 ? code : void 0;
|
|
451
|
-
}
|
|
452
|
-
function isVisibleText(event) {
|
|
453
|
-
const value = event;
|
|
454
|
-
if (event.type === "text") return typeof value.text === "string" ? value.text.trim().length > 0 : true;
|
|
455
|
-
if (event.type !== "message.part.updated") return false;
|
|
456
|
-
const part = event.data?.part;
|
|
457
|
-
const delta = event.data?.delta;
|
|
458
|
-
return part?.type === "text" && typeof delta === "string" && delta.trim().length > 0;
|
|
459
|
-
}
|
|
460
|
-
function isVisibleOutput(event) {
|
|
461
|
-
if (isVisibleText(event)) return true;
|
|
462
|
-
const value = event;
|
|
463
|
-
if (event.type === "reasoning") return typeof value.text === "string" && value.text.trim().length > 0;
|
|
464
|
-
if (event.type === "tool_call" || event.type === "tool.call" || event.type === "tool_result" || event.type === "tool.result" || event.type === "tool.event") return true;
|
|
465
|
-
if (event.type !== "message.part.updated") return false;
|
|
466
|
-
const part = event.data?.part;
|
|
467
|
-
if (part?.type === "tool") return true;
|
|
468
|
-
if (part?.type !== "reasoning" && part?.type !== "text") return false;
|
|
469
|
-
const text = event.data?.delta ?? part.text;
|
|
470
|
-
return typeof text === "string" && text.trim().length > 0;
|
|
471
|
-
}
|
|
472
|
-
async function* tapRawEvents(source, onRawEvent, log) {
|
|
473
|
-
for await (const event of source) {
|
|
474
|
-
try {
|
|
475
|
-
await onRawEvent(event);
|
|
476
|
-
} catch (err) {
|
|
477
|
-
log("[chat-routes] onRawEvent failed", { error: err instanceof Error ? err.message : String(err) });
|
|
478
|
-
}
|
|
479
|
-
yield event;
|
|
480
|
-
}
|
|
481
|
-
}
|
|
482
|
-
async function* withStreamHeartbeat(source, intervalMs, makeEvent) {
|
|
483
|
-
const iterator = source[Symbol.asyncIterator]();
|
|
484
|
-
try {
|
|
485
|
-
let pending = iterator.next();
|
|
486
|
-
let windowStart = Date.now();
|
|
487
|
-
let tick = 0;
|
|
488
|
-
for (; ; ) {
|
|
489
|
-
let timer;
|
|
490
|
-
let winner;
|
|
491
|
-
try {
|
|
492
|
-
const heartbeat = new Promise((resolve) => {
|
|
493
|
-
timer = setTimeout(() => resolve("heartbeat"), intervalMs);
|
|
494
|
-
});
|
|
495
|
-
winner = await Promise.race([pending.then(() => "event"), heartbeat]);
|
|
496
|
-
} finally {
|
|
497
|
-
if (timer !== void 0) clearTimeout(timer);
|
|
498
|
-
}
|
|
499
|
-
if (winner === "heartbeat") {
|
|
500
|
-
tick += 1;
|
|
501
|
-
yield makeEvent({ elapsedMs: Date.now() - windowStart, tick });
|
|
502
|
-
continue;
|
|
503
|
-
}
|
|
504
|
-
const result = await pending;
|
|
505
|
-
if (result.done) return;
|
|
506
|
-
yield result.value;
|
|
507
|
-
pending = iterator.next();
|
|
508
|
-
windowStart = Date.now();
|
|
509
|
-
tick = 0;
|
|
510
|
-
}
|
|
511
|
-
} finally {
|
|
512
|
-
await iterator.return?.();
|
|
513
|
-
}
|
|
514
|
-
}
|
|
515
|
-
function concatStreams(streams) {
|
|
516
|
-
let index = 0;
|
|
517
|
-
let reader = null;
|
|
518
|
-
return new ReadableStream({
|
|
519
|
-
async pull(controller) {
|
|
520
|
-
for (; ; ) {
|
|
521
|
-
if (!reader) {
|
|
522
|
-
const next = streams[index++];
|
|
523
|
-
if (!next) {
|
|
524
|
-
controller.close();
|
|
525
|
-
return;
|
|
526
|
-
}
|
|
527
|
-
reader = next.getReader();
|
|
528
|
-
}
|
|
529
|
-
const { done, value } = await reader.read();
|
|
530
|
-
if (done) {
|
|
531
|
-
reader = null;
|
|
532
|
-
continue;
|
|
533
|
-
}
|
|
534
|
-
controller.enqueue(value);
|
|
535
|
-
return;
|
|
536
|
-
}
|
|
537
|
-
},
|
|
538
|
-
async cancel(reason) {
|
|
539
|
-
await reader?.cancel(reason);
|
|
540
|
-
for (const stream of streams.slice(index)) await stream.cancel(reason);
|
|
541
|
-
}
|
|
542
|
-
});
|
|
543
|
-
}
|
|
544
|
-
async function* abortOnSignal(source, signal) {
|
|
545
|
-
const iterator = source[Symbol.asyncIterator]();
|
|
546
|
-
try {
|
|
547
|
-
for (; ; ) {
|
|
548
|
-
const next = await nextWithAbort(iterator, signal);
|
|
549
|
-
if (next.done) return;
|
|
550
|
-
yield next.value;
|
|
551
|
-
}
|
|
552
|
-
} finally {
|
|
553
|
-
await closeIteratorAfterAbort(iterator);
|
|
554
|
-
}
|
|
555
|
-
}
|
|
556
|
-
function nextWithAbort(iterator, signal) {
|
|
557
|
-
if (signal.aborted) return Promise.reject(new Error("chat turn cancelled"));
|
|
558
|
-
return new Promise((resolve, reject) => {
|
|
559
|
-
let settled = false;
|
|
560
|
-
const cleanup = () => signal.removeEventListener("abort", onAbort);
|
|
561
|
-
const onAbort = () => {
|
|
562
|
-
if (settled) return;
|
|
563
|
-
settled = true;
|
|
564
|
-
cleanup();
|
|
565
|
-
reject(new Error("chat turn cancelled"));
|
|
566
|
-
};
|
|
567
|
-
signal.addEventListener("abort", onAbort, { once: true });
|
|
568
|
-
if (signal.aborted) {
|
|
569
|
-
onAbort();
|
|
570
|
-
return;
|
|
571
|
-
}
|
|
572
|
-
Promise.resolve().then(() => iterator.next()).then(
|
|
573
|
-
(result) => {
|
|
574
|
-
if (settled) return;
|
|
575
|
-
settled = true;
|
|
576
|
-
cleanup();
|
|
577
|
-
resolve(result);
|
|
578
|
-
},
|
|
579
|
-
(error) => {
|
|
580
|
-
if (settled) return;
|
|
581
|
-
settled = true;
|
|
582
|
-
cleanup();
|
|
583
|
-
reject(error);
|
|
584
|
-
}
|
|
585
|
-
);
|
|
586
|
-
});
|
|
587
|
-
}
|
|
588
|
-
var ABORT_CLEANUP_TIMEOUT_MS = 50;
|
|
589
|
-
async function closeIteratorAfterAbort(iterator) {
|
|
590
|
-
let closing;
|
|
591
|
-
try {
|
|
592
|
-
const result = iterator.return?.();
|
|
593
|
-
if (result) closing = Promise.resolve(result);
|
|
594
|
-
} catch {
|
|
595
|
-
return;
|
|
596
|
-
}
|
|
597
|
-
if (!closing) return;
|
|
598
|
-
let timeout;
|
|
599
|
-
try {
|
|
600
|
-
await Promise.race([
|
|
601
|
-
Promise.resolve(closing).catch(() => void 0),
|
|
602
|
-
new Promise((resolve) => {
|
|
603
|
-
timeout = setTimeout(resolve, ABORT_CLEANUP_TIMEOUT_MS);
|
|
604
|
-
})
|
|
605
|
-
]);
|
|
606
|
-
} finally {
|
|
607
|
-
if (timeout !== void 0) clearTimeout(timeout);
|
|
608
|
-
}
|
|
609
|
-
}
|
|
610
|
-
|
|
611
|
-
// src/chat-routes/turn-execution.ts
|
|
612
|
-
async function readTurnHistory(store, threadId) {
|
|
613
|
-
return (await store.listMessages(threadId)).map((m) => ({
|
|
614
|
-
id: m.id,
|
|
615
|
-
role: m.role,
|
|
616
|
-
content: m.content,
|
|
617
|
-
parts: m.parts ?? null
|
|
618
|
-
}));
|
|
619
|
-
}
|
|
620
|
-
function modelAttributionFields(attribution) {
|
|
621
|
-
return {
|
|
622
|
-
...attribution?.requestedModel ? { requestedModel: attribution.requestedModel } : {},
|
|
623
|
-
...attribution?.servedModel ? { servedModel: attribution.servedModel } : {},
|
|
624
|
-
...attribution?.servedProvider ? { servedProvider: attribution.servedProvider } : {},
|
|
625
|
-
...attribution?.servedSource ? { servedSource: attribution.servedSource } : {}
|
|
626
|
-
};
|
|
627
|
-
}
|
|
628
|
-
function settledTurnTiming(turnStreamId, at, trace) {
|
|
629
|
-
const measured = {
|
|
630
|
-
admissionMs: at.startedAt - at.requestAt,
|
|
631
|
-
...at.firstVisibleAt === void 0 ? {} : { firstVisibleMs: at.firstVisibleAt - at.startedAt },
|
|
632
|
-
...at.firstTextAt === void 0 ? {} : { firstTextMs: at.firstTextAt - at.startedAt }
|
|
633
|
-
};
|
|
634
|
-
const reason = at.failure ? failureReasonOf(at.failure.data) ?? redactErrorMessage(at.failure.error, "chat turn failed") : void 0;
|
|
635
|
-
const code = at.failure ? failureCodeOf(at.failure.data) : void 0;
|
|
636
|
-
const failureCode = reason ? redactErrorMessage(code && reason !== code && !reason.startsWith(`${code}:`) ? `${code}: ${reason}` : reason) : void 0;
|
|
637
|
-
noteMeasuredTurnFacts(turnStreamId, {
|
|
638
|
-
...measured,
|
|
639
|
-
...at.model ? { model: at.model } : {},
|
|
640
|
-
...failureCode ? { failureCode } : {}
|
|
641
|
-
});
|
|
642
|
-
return { ...measured, trace };
|
|
643
|
-
}
|
|
644
|
-
function createAdmittedTurnRunner(dependencies) {
|
|
645
|
-
return runAdmittedTurn.bind(null, dependencies);
|
|
646
|
-
}
|
|
647
|
-
async function runAdmittedTurn({
|
|
648
|
-
options,
|
|
649
|
-
draftStore,
|
|
650
|
-
draftTuning,
|
|
651
|
-
progressMessages,
|
|
652
|
-
traceOptions,
|
|
653
|
-
traceSink,
|
|
654
|
-
startIntentLine,
|
|
655
|
-
log
|
|
656
|
-
}, request, ctx, admitted, early, progress, startedIntent) {
|
|
657
|
-
const { parsed, auth, trace } = admitted;
|
|
658
|
-
const intentLine = startedIntent ?? startIntentLine(admitted);
|
|
659
|
-
const { payload, content, fileParts, mentions, turnId } = parsed;
|
|
660
|
-
const { tenantId, userId, context } = auth;
|
|
661
|
-
const preparedBatch = await prepareAdmittedTurn(options, trace, log, tenantId, userId, payload, context, request, progress, content, turnId, draftTuning);
|
|
662
|
-
if (preparedBatch instanceof Response) return preparedBatch;
|
|
663
|
-
const { turnStreamId, identity, executionId, chatTurn, readHistory, historyBeforeLock } = preparedBatch;
|
|
664
|
-
const prompt = fileParts.length === 0 ? content : content ? [{ type: "text", text: content }, ...fileParts] : [...fileParts];
|
|
665
|
-
const state = createAdmittedTurnState();
|
|
666
|
-
const { handoffCompletion, releaseLock } = createTurnOwnership(state, log, turnStreamId, options);
|
|
667
|
-
let produceArgs = {
|
|
668
|
-
request,
|
|
669
|
-
body: payload,
|
|
670
|
-
identity,
|
|
671
|
-
context,
|
|
672
|
-
prompt,
|
|
673
|
-
executionId,
|
|
674
|
-
turnStreamId,
|
|
675
|
-
priorMessages: chatTurn.priorMessages,
|
|
676
|
-
...chatTurn.reusedUserMessageId ? { userMessageId: chatTurn.reusedUserMessageId, userMessageInserted: false } : {},
|
|
677
|
-
handoffCompletion,
|
|
678
|
-
trace,
|
|
679
|
-
...intentLine ? { intentLine } : {},
|
|
680
|
-
...admitted.prefetched ? { prefetched: admitted.prefetched } : {},
|
|
681
|
-
...ctx?.executionLimits ? { executionLimits: ctx.executionLimits } : {}
|
|
682
|
-
};
|
|
683
|
-
const dispatched = dispatchFirstFor(options, produceArgs, trace);
|
|
684
|
-
if (dispatched) early.add(dispatched.refuse);
|
|
685
|
-
const turnLock = options.turnLock;
|
|
686
|
-
if (turnLock) {
|
|
687
|
-
const acquired = await trace.measure("admission.lock", "admission", () => turnLock.acquire(produceArgs));
|
|
688
|
-
if (!acquired.acquired) return acquired.response;
|
|
689
|
-
state.lockAcquired = true;
|
|
690
|
-
state.lockHandle = acquired.handle;
|
|
691
|
-
}
|
|
692
|
-
progress?.("preparing", progressMessages.preparing);
|
|
693
|
-
const { endTrace, exportTraceInBackground, fireTerminalLifecycle, assistantRowId, exportTrace, runAfterSettle } = createTurnSettlement(state, trace, options, executionId, traceSink, identity, traceOptions, context, turnStreamId, log, ctx);
|
|
694
|
-
try {
|
|
695
|
-
if (state.lockAcquired && JSON.stringify(await trace.measure("admission.history_recheck", "admission", readHistory)) !== historyBeforeLock) {
|
|
696
|
-
await releaseLock();
|
|
697
|
-
return Response.json({
|
|
698
|
-
code: "chat_history_changed",
|
|
699
|
-
error: "The conversation changed while this turn was acquiring its lock. Refresh the conversation before starting new work."
|
|
700
|
-
}, { status: 409 });
|
|
701
|
-
}
|
|
702
|
-
const insertUserMessage = chatTurn.shouldInsertUserMessage && (auth.insertUserMessage ?? true);
|
|
703
|
-
let userMessageId = chatTurn.reusedUserMessageId ?? null;
|
|
704
|
-
let userMessageInserted = false;
|
|
705
|
-
if (insertUserMessage) {
|
|
706
|
-
userMessageId = rowIdOf(
|
|
707
|
-
await trace.measure("admission.persist_user", "admission", () => options.store.appendMessage({
|
|
708
|
-
threadId: payload.threadId,
|
|
709
|
-
role: "user",
|
|
710
|
-
content,
|
|
711
|
-
parts: userPartsWithFiles(chatTurn.userParts, fileParts, mentions)
|
|
712
|
-
}))
|
|
713
|
-
);
|
|
714
|
-
userMessageInserted = userMessageId !== null;
|
|
715
|
-
}
|
|
716
|
-
produceArgs = { ...produceArgs, userMessageId, userMessageInserted };
|
|
717
|
-
const contextGate = options.contextGate;
|
|
718
|
-
if (contextGate) {
|
|
719
|
-
const gate = await trace.measure("admission.gate", "admission", () => contextGate(produceArgs));
|
|
720
|
-
if (gate.proceed === "handoff") {
|
|
721
|
-
await releaseLock();
|
|
722
|
-
state.legacyHandoff = true;
|
|
723
|
-
endTrace(false, void 0);
|
|
724
|
-
exportTraceInBackground();
|
|
725
|
-
return gate.response;
|
|
726
|
-
}
|
|
727
|
-
if (!gate.proceed) {
|
|
728
|
-
await releaseLock();
|
|
729
|
-
if (state.completionHandoff) {
|
|
730
|
-
endTrace(false, void 0);
|
|
731
|
-
exportTraceInBackground();
|
|
732
|
-
return gate.response;
|
|
733
|
-
}
|
|
734
|
-
state.gatedTurn = true;
|
|
735
|
-
state.turnStartedAtMs = Date.now();
|
|
736
|
-
if (options.lifecycle?.onTurnStart) {
|
|
737
|
-
try {
|
|
738
|
-
await options.lifecycle.onTurnStart({
|
|
739
|
-
identity,
|
|
740
|
-
executionId,
|
|
741
|
-
turnStreamId,
|
|
742
|
-
context,
|
|
743
|
-
startedAt: state.turnStartedAtMs,
|
|
744
|
-
requestReceivedAt: trace.startedAt,
|
|
745
|
-
admissionMs: state.turnStartedAtMs - trace.startedAt
|
|
746
|
-
});
|
|
747
|
-
} catch (err) {
|
|
748
|
-
log("[chat-routes] lifecycle.onTurnStart failed", {
|
|
749
|
-
turnId: turnStreamId,
|
|
750
|
-
error: err instanceof Error ? err.message : String(err)
|
|
751
|
-
});
|
|
752
|
-
}
|
|
753
|
-
}
|
|
754
|
-
await fireTerminalLifecycle(false, void 0);
|
|
755
|
-
exportTraceInBackground();
|
|
756
|
-
return gate.response;
|
|
757
|
-
}
|
|
758
|
-
}
|
|
759
|
-
const beforeTurn = options.beforeTurn;
|
|
760
|
-
if (beforeTurn) {
|
|
761
|
-
const patch = await trace.measure("admission.before_turn", "admission", (span) => beforeTurn(produceArgs, { span }));
|
|
762
|
-
if (patch && Object.keys(patch).length && dispatched) throw new Error("beforeTurn cannot patch a dispatchFirst turn: its prompt is already sent");
|
|
763
|
-
if (patch) produceArgs = { ...produceArgs, ...patch };
|
|
764
|
-
}
|
|
765
|
-
const streamSetup = trace.start("admission.stream_setup", "admission");
|
|
766
|
-
state.tap = createBufferedTurnTap({
|
|
767
|
-
store: options.turnStore,
|
|
768
|
-
turnId: turnStreamId,
|
|
769
|
-
scopeId: payload.threadId,
|
|
770
|
-
coalesce: options.coalesceTurnEvents ?? coalesceDeltas
|
|
771
|
-
});
|
|
772
|
-
const turnMarker = { type: "turn", turnId: turnStreamId };
|
|
773
|
-
await state.tap.onEvent(turnMarker);
|
|
774
|
-
if (state.completionHandoff) await state.tap.detach();
|
|
775
|
-
if (draftTuning) {
|
|
776
|
-
state.draft = createAssistantDraftWriter({
|
|
777
|
-
...draftTuning,
|
|
778
|
-
store: draftStore,
|
|
779
|
-
threadId: payload.threadId,
|
|
780
|
-
messageId: options.draftMessageId ? options.draftMessageId({ identity, executionId, threadId: payload.threadId }) : assistantRowIdForTurn(executionId),
|
|
781
|
-
snapshot: () => {
|
|
782
|
-
if (!state.producer) return null;
|
|
783
|
-
return {
|
|
784
|
-
content: state.producer.finalText(),
|
|
785
|
-
...state.producer.draftParts ? { parts: state.producer.draftParts() } : {},
|
|
786
|
-
...state.producer.usage ? { usage: state.producer.usage() } : {},
|
|
787
|
-
...state.producer.model ? { model: state.producer.model } : {}
|
|
788
|
-
};
|
|
789
|
-
},
|
|
790
|
-
...options.transformFinalText ? { transformText: options.transformFinalText } : {},
|
|
791
|
-
log
|
|
792
|
-
});
|
|
793
|
-
if (state.completionHandoff) await state.draft.close();
|
|
794
|
-
}
|
|
795
|
-
state.turnStartedAtMs = Date.now();
|
|
796
|
-
state.turnStarted = true;
|
|
797
|
-
if (options.lifecycle?.onTurnStart) {
|
|
798
|
-
try {
|
|
799
|
-
await options.lifecycle.onTurnStart({
|
|
800
|
-
identity,
|
|
801
|
-
executionId,
|
|
802
|
-
turnStreamId,
|
|
803
|
-
context,
|
|
804
|
-
startedAt: state.turnStartedAtMs,
|
|
805
|
-
requestReceivedAt: trace.startedAt,
|
|
806
|
-
admissionMs: state.turnStartedAtMs - trace.startedAt
|
|
807
|
-
});
|
|
808
|
-
} catch (err) {
|
|
809
|
-
log("[chat-routes] lifecycle.onTurnStart failed", {
|
|
810
|
-
turnId: turnStreamId,
|
|
811
|
-
error: err instanceof Error ? err.message : String(err)
|
|
812
|
-
});
|
|
813
|
-
}
|
|
814
|
-
}
|
|
815
|
-
streamSetup.end();
|
|
816
|
-
const { result } = createTurnEngine(identity, ctx, log, state, dispatched, produceArgs, trace, options, request, context, payload, assistantRowId);
|
|
817
|
-
early.handOff();
|
|
818
|
-
const { clientBody } = startTurnDrain(result, log, turnStreamId, state, endTrace, exportTrace, trace, ctx, request, fireTerminalLifecycle, releaseLock, runAfterSettle);
|
|
819
|
-
return createTurnReplayResponse(result.contentType, clientBody, turnMarker, trace);
|
|
820
|
-
} catch (err) {
|
|
821
|
-
if (!state.completionHandoff) {
|
|
822
|
-
if (state.turnStarted) await fireTerminalLifecycle(true, err);
|
|
823
|
-
await releaseLock();
|
|
824
|
-
}
|
|
825
|
-
endTrace(true, err);
|
|
826
|
-
exportTraceInBackground();
|
|
827
|
-
throw err;
|
|
828
|
-
}
|
|
829
|
-
}
|
|
830
|
-
function createAdmittedTurnState() {
|
|
831
|
-
return {
|
|
832
|
-
draft: void 0,
|
|
833
|
-
tap: void 0,
|
|
834
|
-
completionHandoff: false,
|
|
835
|
-
completionHandoffPromise: void 0,
|
|
836
|
-
lockAcquired: false,
|
|
837
|
-
lockHandle: void 0,
|
|
838
|
-
lockReleased: false,
|
|
839
|
-
producer: void 0,
|
|
840
|
-
assistantMessageId: void 0,
|
|
841
|
-
runFailed: false,
|
|
842
|
-
lastFailureData: void 0,
|
|
843
|
-
turnStartedAtMs: 0,
|
|
844
|
-
firstVisibleAtMs: void 0,
|
|
845
|
-
firstTextAtMs: void 0,
|
|
846
|
-
turnStarted: false,
|
|
847
|
-
lifecycleSettled: false,
|
|
848
|
-
settledTurn: void 0,
|
|
849
|
-
gatedTurn: false,
|
|
850
|
-
legacyHandoff: false,
|
|
851
|
-
pendingTraceExport: void 0,
|
|
852
|
-
traceEnded: false
|
|
853
|
-
};
|
|
854
|
-
}
|
|
855
|
-
async function prepareAdmittedTurn(options, trace, log, tenantId, userId, payload, context, request, progress, content, turnId, draftTuning) {
|
|
856
|
-
if (options.turnLock?.warm) {
|
|
857
|
-
const warmSpan = trace.start("admission.lock_warm", "admission");
|
|
858
|
-
const warmFailed = (err) => {
|
|
859
|
-
warmSpan.end(void 0, err);
|
|
860
|
-
log("[chat-routes] turnLock.warm failed", { error: err instanceof Error ? err.message : String(err) });
|
|
861
|
-
};
|
|
862
|
-
try {
|
|
863
|
-
void Promise.resolve(options.turnLock.warm({ tenantId, userId, threadId: payload.threadId, context })).then(() => warmSpan.end(), warmFailed);
|
|
864
|
-
} catch (err) {
|
|
865
|
-
warmFailed(err);
|
|
866
|
-
}
|
|
867
|
-
}
|
|
868
|
-
const prepareTurn = options.prepareTurn;
|
|
869
|
-
if (prepareTurn) {
|
|
870
|
-
const prepared = await trace.measure("admission.prepare", "admission", () => prepareTurn({
|
|
871
|
-
request,
|
|
872
|
-
body: payload,
|
|
873
|
-
tenantId,
|
|
874
|
-
userId,
|
|
875
|
-
threadId: payload.threadId,
|
|
876
|
-
context,
|
|
877
|
-
progress: (phase, message) => progress?.(phase, message)
|
|
878
|
-
}));
|
|
879
|
-
if (!prepared.ok) return prepared.response;
|
|
880
|
-
}
|
|
881
|
-
const readHistory = () => readTurnHistory(options.store, payload.threadId);
|
|
882
|
-
const historySpan = trace.start("admission.history", "admission");
|
|
883
|
-
const existingMessages = await readHistory();
|
|
884
|
-
const historyBeforeLock = options.turnLock ? JSON.stringify(existingMessages) : void 0;
|
|
885
|
-
let chatTurn = resolveChatTurn({ existingMessages, userContent: content, turnId, hasRunningTurn: false });
|
|
886
|
-
if (draftTuning && existingMessages.at(-1)?.role === "assistant") {
|
|
887
|
-
const ifRunning = resolveChatTurn({ existingMessages, userContent: content, turnId, hasRunningTurn: true });
|
|
888
|
-
if (!ifRunning.shouldInsertUserMessage) {
|
|
889
|
-
let hasRunningTurn = false;
|
|
890
|
-
try {
|
|
891
|
-
hasRunningTurn = (await trace.measure("admission.running_probe", "admission", () => options.turnStore.listRunning?.(payload.threadId), void 0, historySpan) ?? []).length > 0;
|
|
892
|
-
} catch (err) {
|
|
893
|
-
log("[chat-routes] listRunning probe failed; treating the thread as idle", {
|
|
894
|
-
threadId: payload.threadId,
|
|
895
|
-
error: err instanceof Error ? err.message : String(err)
|
|
896
|
-
});
|
|
897
|
-
}
|
|
898
|
-
if (hasRunningTurn) chatTurn = ifRunning;
|
|
899
|
-
}
|
|
900
|
-
}
|
|
901
|
-
historySpan.end({ messages: existingMessages.length });
|
|
902
|
-
const identity = {
|
|
903
|
-
tenantId,
|
|
904
|
-
sessionId: payload.threadId,
|
|
905
|
-
userId,
|
|
906
|
-
turnIndex: chatTurn.turnIndex
|
|
907
|
-
};
|
|
908
|
-
const turnExecutionId = deriveExecutionId({
|
|
909
|
-
projectId: options.projectId,
|
|
910
|
-
sessionId: payload.threadId,
|
|
911
|
-
turnIndex: chatTurn.turnIndex
|
|
912
|
-
});
|
|
913
|
-
const executionId = payload.retryAttemptId ? `${turnExecutionId}:retry:${payload.retryAttemptId}` : turnExecutionId;
|
|
914
|
-
if (executionId.length > 256) return errorResponse(new ChatTurnInputError("Retry execution id exceeds 256 bytes"));
|
|
915
|
-
trace.bind(executionId);
|
|
916
|
-
const turnStreamId = crypto.randomUUID();
|
|
917
|
-
return { turnStreamId, identity, executionId, chatTurn, readHistory, historyBeforeLock };
|
|
918
|
-
}
|
|
919
|
-
function createTurnSettlement(state, trace, options, executionId, traceSink, identity, traceOptions, context, turnStreamId, log, ctx) {
|
|
920
|
-
const assistantRowId = () => state.assistantMessageId ?? state.draft?.rowId() ?? null;
|
|
921
|
-
const endTrace = (failed, terminalError) => {
|
|
922
|
-
const failureCode = failed ? failureCodeOf(state.lastFailureData) ?? (terminalError instanceof Error ? terminalError.name : "turn_failed") : void 0;
|
|
923
|
-
const snapshot = trace.finish(failureCode === void 0 ? {} : { error: failureCode });
|
|
924
|
-
if (state.traceEnded) return snapshot;
|
|
925
|
-
state.traceEnded = true;
|
|
926
|
-
const handedOff = state.completionHandoff || state.legacyHandoff;
|
|
927
|
-
console.info(JSON.stringify({
|
|
928
|
-
event: "agent_app_turn_trace",
|
|
929
|
-
project: options.projectId,
|
|
930
|
-
executionId,
|
|
931
|
-
otlpTraceId: otlpTraceId(snapshot.traceId),
|
|
932
|
-
failed,
|
|
933
|
-
...failureCode ? { failureCode } : {},
|
|
934
|
-
...handedOff ? { handedOff: true } : {},
|
|
935
|
-
...state.gatedTurn ? { gated: true } : {},
|
|
936
|
-
...snapshot.summary
|
|
937
|
-
}));
|
|
938
|
-
const sink = traceSink;
|
|
939
|
-
if (sink) {
|
|
940
|
-
state.pendingTraceExport = () => sink(snapshot, {
|
|
941
|
-
"tangle.sessionId": identity.sessionId,
|
|
942
|
-
"tangle.userId": identity.userId,
|
|
943
|
-
"tangle.tenantId": identity.tenantId,
|
|
944
|
-
"tangle.turn.failed": failed,
|
|
945
|
-
...state.gatedTurn ? { "tangle.turn.gated": true } : {},
|
|
946
|
-
...handedOff ? { "tangle.turn.handed_off": true } : {},
|
|
947
|
-
...failureCode ? { "tangle.turn.failure_code": failureCode } : {},
|
|
948
|
-
...traceOptions?.labels?.({ identity, executionId, context }) ?? {}
|
|
949
|
-
});
|
|
950
|
-
}
|
|
951
|
-
return snapshot;
|
|
952
|
-
};
|
|
953
|
-
const fireTerminalLifecycle = async (failed, terminalError) => {
|
|
954
|
-
if (state.lifecycleSettled) return;
|
|
955
|
-
state.lifecycleSettled = true;
|
|
956
|
-
const snapshot = endTrace(failed, terminalError);
|
|
957
|
-
const lifecycle = options.lifecycle;
|
|
958
|
-
const durationMs = Date.now() - state.turnStartedAtMs;
|
|
959
|
-
const timing = settledTurnTiming(turnStreamId, {
|
|
960
|
-
requestAt: trace.startedAt,
|
|
961
|
-
startedAt: state.turnStartedAtMs,
|
|
962
|
-
firstVisibleAt: state.firstVisibleAtMs,
|
|
963
|
-
firstTextAt: state.firstTextAtMs,
|
|
964
|
-
model: state.producer?.model,
|
|
965
|
-
failure: failed ? { data: state.lastFailureData, error: terminalError } : void 0
|
|
966
|
-
}, snapshot.summary);
|
|
967
|
-
try {
|
|
968
|
-
if (!lifecycle) {
|
|
969
|
-
} else if (failed) {
|
|
970
|
-
await lifecycle.onTurnError?.({
|
|
971
|
-
identity,
|
|
972
|
-
executionId,
|
|
973
|
-
turnStreamId,
|
|
974
|
-
context,
|
|
975
|
-
durationMs,
|
|
976
|
-
...timing,
|
|
977
|
-
error: terminalError ?? state.lastFailureData ?? new Error("chat turn failed")
|
|
978
|
-
});
|
|
979
|
-
} else {
|
|
980
|
-
const failoverInfo = state.producer?.modelFailover?.();
|
|
981
|
-
const attribution = state.producer?.modelAttribution?.();
|
|
982
|
-
const completed = {
|
|
983
|
-
identity,
|
|
984
|
-
executionId,
|
|
985
|
-
turnStreamId,
|
|
986
|
-
context,
|
|
987
|
-
durationMs,
|
|
988
|
-
...timing,
|
|
989
|
-
finalText: state.producer?.finalText() ?? "",
|
|
990
|
-
usage: state.producer?.usage?.() ?? {},
|
|
991
|
-
assistantMessageId: assistantRowId(),
|
|
992
|
-
...state.gatedTurn ? { gated: true } : {},
|
|
993
|
-
...state.producer?.model ? { model: state.producer.model } : {},
|
|
994
|
-
...modelAttributionFields(attribution),
|
|
995
|
-
...failoverInfo ? { modelFailover: failoverInfo } : {}
|
|
996
|
-
};
|
|
997
|
-
if (!state.gatedTurn) state.settledTurn = completed;
|
|
998
|
-
await lifecycle.onTurnComplete?.(completed);
|
|
999
|
-
}
|
|
1000
|
-
} catch (err) {
|
|
1001
|
-
log("[chat-routes] lifecycle terminal hook failed", {
|
|
1002
|
-
turnId: turnStreamId,
|
|
1003
|
-
error: err instanceof Error ? err.message : String(err)
|
|
1004
|
-
});
|
|
1005
|
-
}
|
|
1006
|
-
};
|
|
1007
|
-
const exportTrace = async () => {
|
|
1008
|
-
const send = state.pendingTraceExport;
|
|
1009
|
-
state.pendingTraceExport = void 0;
|
|
1010
|
-
if (!send) return;
|
|
1011
|
-
try {
|
|
1012
|
-
await send();
|
|
1013
|
-
} catch (err) {
|
|
1014
|
-
log("[chat-routes] turn trace sink failed", {
|
|
1015
|
-
turnId: turnStreamId,
|
|
1016
|
-
error: err instanceof Error ? err.message : String(err)
|
|
1017
|
-
});
|
|
1018
|
-
}
|
|
1019
|
-
};
|
|
1020
|
-
const runAfterSettle = async () => {
|
|
1021
|
-
const info = state.settledTurn;
|
|
1022
|
-
state.settledTurn = void 0;
|
|
1023
|
-
const afterSettle = options.lifecycle?.afterSettle;
|
|
1024
|
-
if (!info || !afterSettle) return;
|
|
1025
|
-
try {
|
|
1026
|
-
await afterSettle(info);
|
|
1027
|
-
} catch (err) {
|
|
1028
|
-
log("[chat-routes] lifecycle.afterSettle failed", {
|
|
1029
|
-
turnId: turnStreamId,
|
|
1030
|
-
error: err instanceof Error ? err.message : String(err)
|
|
1031
|
-
});
|
|
1032
|
-
}
|
|
1033
|
-
};
|
|
1034
|
-
const exportTraceInBackground = () => {
|
|
1035
|
-
const sent = exportTrace();
|
|
1036
|
-
if (ctx?.waitUntil) ctx.waitUntil(sent);
|
|
1037
|
-
else void sent.catch(() => {
|
|
1038
|
-
});
|
|
1039
|
-
};
|
|
1040
|
-
return { endTrace, exportTraceInBackground, fireTerminalLifecycle, assistantRowId, exportTrace, runAfterSettle };
|
|
1041
|
-
}
|
|
1042
|
-
function createTurnEngine(identity, ctx, log, state, dispatched, produceArgs, trace, options, request, context, payload, assistantRowId) {
|
|
1043
|
-
const result = handleChatTurn({
|
|
1044
|
-
identity,
|
|
1045
|
-
waitUntil: ctx?.waitUntil,
|
|
1046
|
-
log,
|
|
1047
|
-
hooks: {
|
|
1048
|
-
// The engine wants a synchronous producer; box resolution is async —
|
|
1049
|
-
// defer it into the generator's first pull.
|
|
1050
|
-
produce: () => ({
|
|
1051
|
-
stream: (async function* () {
|
|
1052
|
-
state.producer = await (dispatched ? dispatched.take(produceArgs) : trace.measure("admission.produce", "admission", () => options.produce(produceArgs)));
|
|
1053
|
-
let source = state.producer.stream;
|
|
1054
|
-
if (ctx?.cancelOnDisconnect) {
|
|
1055
|
-
source = abortOnSignal(source, request.signal);
|
|
1056
|
-
}
|
|
1057
|
-
if (options.onRawEvent) {
|
|
1058
|
-
source = tapRawEvents(source, (event) => options.onRawEvent(event, context), log);
|
|
1059
|
-
}
|
|
1060
|
-
if (options.heartbeat) {
|
|
1061
|
-
source = withStreamHeartbeat(source, options.heartbeat.intervalMs, options.heartbeat.event);
|
|
1062
|
-
}
|
|
1063
|
-
for await (const event of source) yield event;
|
|
1064
|
-
})(),
|
|
1065
|
-
finalText: () => state.producer?.finalText() ?? ""
|
|
1066
|
-
}),
|
|
1067
|
-
onEvent: async (event) => {
|
|
1068
|
-
if (state.firstVisibleAtMs === void 0 && isVisibleOutput(event)) {
|
|
1069
|
-
state.firstVisibleAtMs = Date.now();
|
|
1070
|
-
trace.markFirstVisible(state.firstVisibleAtMs);
|
|
1071
|
-
}
|
|
1072
|
-
if (state.firstTextAtMs === void 0 && isVisibleText(event)) {
|
|
1073
|
-
state.firstTextAtMs = Date.now();
|
|
1074
|
-
trace.markFirstText(state.firstTextAtMs);
|
|
1075
|
-
}
|
|
1076
|
-
if (event.type === "session.run.failed" || event.type === "error") {
|
|
1077
|
-
state.runFailed = true;
|
|
1078
|
-
state.lastFailureData = event.data;
|
|
1079
|
-
}
|
|
1080
|
-
await state.tap.onEvent(event);
|
|
1081
|
-
if (!state.completionHandoff) state.draft?.notify(event);
|
|
1082
|
-
if (options.onEvent) await options.onEvent(event, context);
|
|
1083
|
-
},
|
|
1084
|
-
...options.transformFinalText ? { transformFinalText: options.transformFinalText } : {},
|
|
1085
|
-
persistAssistantMessage: ({ finalText }) => trace.measure("persist.assistant", "persist", async () => {
|
|
1086
|
-
if (state.completionHandoff) return;
|
|
1087
|
-
if (ctx?.cancelOnDisconnect && request.signal.aborted) {
|
|
1088
|
-
await state.draft?.discard();
|
|
1089
|
-
state.assistantMessageId = null;
|
|
1090
|
-
return;
|
|
1091
|
-
}
|
|
1092
|
-
const rawParts = state.producer?.assistantParts ? state.producer.assistantParts() : void 0;
|
|
1093
|
-
const projected = rawParts && options.transformFinalText ? await Promise.all(
|
|
1094
|
-
rawParts.map(
|
|
1095
|
-
async (part) => String(part.type ?? "") === "text" ? { ...part, text: await options.transformFinalText(String(part.text ?? "")) } : part
|
|
1096
|
-
)
|
|
1097
|
-
) : rawParts;
|
|
1098
|
-
const parts = projected ? toChatMessageParts(projected) : void 0;
|
|
1099
|
-
if (!finalText.trim() && (!parts || parts.length === 0)) {
|
|
1100
|
-
await state.draft?.discard();
|
|
1101
|
-
state.assistantMessageId = state.draft?.rowId() ?? null;
|
|
1102
|
-
return;
|
|
1103
|
-
}
|
|
1104
|
-
const usage = state.producer?.usage?.() ?? {};
|
|
1105
|
-
const attribution = state.producer?.modelAttribution?.();
|
|
1106
|
-
const values = {
|
|
1107
|
-
content: finalText,
|
|
1108
|
-
...parts && parts.length > 0 ? { parts } : {},
|
|
1109
|
-
...state.producer?.model ? { model: state.producer.model } : {},
|
|
1110
|
-
...modelAttributionFields(attribution),
|
|
1111
|
-
...usage.inputTokens !== void 0 ? { inputTokens: usage.inputTokens } : {},
|
|
1112
|
-
...usage.outputTokens !== void 0 ? { outputTokens: usage.outputTokens } : {},
|
|
1113
|
-
...usage.reasoningTokens !== void 0 ? { reasoningTokens: usage.reasoningTokens } : {},
|
|
1114
|
-
...usage.cacheReadTokens !== void 0 ? { cacheReadTokens: usage.cacheReadTokens } : {},
|
|
1115
|
-
...usage.cacheWriteTokens !== void 0 ? { cacheWriteTokens: usage.cacheWriteTokens } : {},
|
|
1116
|
-
...usage.costUsd !== void 0 ? { costUsd: usage.costUsd } : {}
|
|
1117
|
-
};
|
|
1118
|
-
if (state.draft) {
|
|
1119
|
-
await state.draft.finalize(values);
|
|
1120
|
-
state.assistantMessageId = state.draft.rowId() ?? null;
|
|
1121
|
-
return;
|
|
1122
|
-
}
|
|
1123
|
-
state.assistantMessageId = rowIdOf(
|
|
1124
|
-
await options.store.appendMessage({
|
|
1125
|
-
threadId: payload.threadId,
|
|
1126
|
-
role: "assistant",
|
|
1127
|
-
...values
|
|
1128
|
-
})
|
|
1129
|
-
);
|
|
1130
|
-
}),
|
|
1131
|
-
...options.onTurnComplete ? {
|
|
1132
|
-
// Wired into the engine's completion hook, which fires only when
|
|
1133
|
-
// the stream ended without throwing. A terminal error EVENT
|
|
1134
|
-
// (not a throw) still lands here — so surface `runFailed` so the
|
|
1135
|
-
// product skips billing an errored turn instead of marking it
|
|
1136
|
-
// complete with empty text.
|
|
1137
|
-
onTurnComplete: async ({ identity: turnIdentity, finalText }) => {
|
|
1138
|
-
if (state.completionHandoff) return;
|
|
1139
|
-
const failoverInfo = state.producer?.modelFailover?.();
|
|
1140
|
-
const attribution = state.producer?.modelAttribution?.();
|
|
1141
|
-
return trace.measure("persist.on_turn_complete", "persist", () => options.onTurnComplete({
|
|
1142
|
-
identity: turnIdentity,
|
|
1143
|
-
finalText,
|
|
1144
|
-
context,
|
|
1145
|
-
failed: state.runFailed,
|
|
1146
|
-
assistantMessageId: assistantRowId(),
|
|
1147
|
-
...state.runFailed ? { failureReason: failureReasonOf(state.lastFailureData) } : {},
|
|
1148
|
-
...state.runFailed && failureCodeOf(state.lastFailureData) ? { failureCode: failureCodeOf(state.lastFailureData) } : {},
|
|
1149
|
-
...state.producer?.model ? { model: state.producer.model } : {},
|
|
1150
|
-
...modelAttributionFields(attribution),
|
|
1151
|
-
...failoverInfo ? { modelFailover: failoverInfo } : {}
|
|
1152
|
-
}));
|
|
1153
|
-
}
|
|
1154
|
-
} : {},
|
|
1155
|
-
...options.traceFlush ? { traceFlush: () => options.traceFlush(context) } : {}
|
|
1156
|
-
}
|
|
1157
|
-
});
|
|
1158
|
-
return { result };
|
|
1159
|
-
}
|
|
1160
|
-
function startTurnDrain(result, log, turnStreamId, state, endTrace, exportTrace, trace, ctx, request, fireTerminalLifecycle, releaseLock, runAfterSettle) {
|
|
1161
|
-
const [clientBody, drainBody] = result.body.tee();
|
|
1162
|
-
const drained = (async () => {
|
|
1163
|
-
const reader = drainBody.getReader();
|
|
1164
|
-
let drainError;
|
|
1165
|
-
try {
|
|
1166
|
-
for (; ; ) {
|
|
1167
|
-
const { done } = await reader.read();
|
|
1168
|
-
if (done) break;
|
|
1169
|
-
}
|
|
1170
|
-
} catch (err) {
|
|
1171
|
-
drainError = err;
|
|
1172
|
-
log("[chat-routes] turn drain failed", {
|
|
1173
|
-
turnId: turnStreamId,
|
|
1174
|
-
error: err instanceof Error ? err.message : String(err)
|
|
1175
|
-
});
|
|
1176
|
-
}
|
|
1177
|
-
const failed = state.runFailed || drainError !== void 0;
|
|
1178
|
-
if (state.completionHandoff) {
|
|
1179
|
-
endTrace(failed, drainError);
|
|
1180
|
-
await Promise.all([state.tap.detach(), exportTrace()]);
|
|
1181
|
-
return;
|
|
1182
|
-
}
|
|
1183
|
-
const finalize = trace.start("persist.finalize", "persist");
|
|
1184
|
-
if (ctx?.cancelOnDisconnect && request.signal.aborted) {
|
|
1185
|
-
await state.draft?.discard();
|
|
1186
|
-
} else {
|
|
1187
|
-
await state.draft?.close();
|
|
1188
|
-
}
|
|
1189
|
-
try {
|
|
1190
|
-
await state.tap.done(failed ? "error" : "complete");
|
|
1191
|
-
} catch (err) {
|
|
1192
|
-
log("[chat-routes] turn buffer finalize failed", {
|
|
1193
|
-
turnId: turnStreamId,
|
|
1194
|
-
error: err instanceof Error ? err.message : String(err)
|
|
1195
|
-
});
|
|
1196
|
-
}
|
|
1197
|
-
finalize.end();
|
|
1198
|
-
await fireTerminalLifecycle(failed, drainError);
|
|
1199
|
-
await releaseLock();
|
|
1200
|
-
await Promise.all([exportTrace(), runAfterSettle()]);
|
|
1201
|
-
})();
|
|
1202
|
-
if (ctx?.waitUntil) ctx.waitUntil(drained);
|
|
1203
|
-
else void drained.catch(() => {
|
|
1204
|
-
});
|
|
1205
|
-
return { clientBody };
|
|
1206
|
-
}
|
|
1207
|
-
function createTurnOwnership(state, log, turnStreamId, options) {
|
|
1208
|
-
const handoffCompletion = () => {
|
|
1209
|
-
if (!state.completionHandoffPromise) {
|
|
1210
|
-
state.completionHandoffPromise = (async () => {
|
|
1211
|
-
state.completionHandoff = true;
|
|
1212
|
-
try {
|
|
1213
|
-
await state.tap?.detach();
|
|
1214
|
-
} catch (err) {
|
|
1215
|
-
log("[chat-routes] turn buffer detach failed", {
|
|
1216
|
-
turnId: turnStreamId,
|
|
1217
|
-
error: err instanceof Error ? err.message : String(err)
|
|
1218
|
-
});
|
|
1219
|
-
}
|
|
1220
|
-
await state.draft?.close();
|
|
1221
|
-
})();
|
|
1222
|
-
}
|
|
1223
|
-
return state.completionHandoffPromise;
|
|
1224
|
-
};
|
|
1225
|
-
const releaseLock = async () => {
|
|
1226
|
-
if (state.completionHandoff || !state.lockAcquired || state.lockReleased) return;
|
|
1227
|
-
state.lockReleased = true;
|
|
1228
|
-
try {
|
|
1229
|
-
await options.turnLock.release(state.lockHandle);
|
|
1230
|
-
} catch (err) {
|
|
1231
|
-
log("[chat-routes] turnLock.release failed", {
|
|
1232
|
-
turnId: turnStreamId,
|
|
1233
|
-
error: err instanceof Error ? err.message : String(err)
|
|
1234
|
-
});
|
|
1235
|
-
}
|
|
1236
|
-
};
|
|
1237
|
-
return { handoffCompletion, releaseLock };
|
|
1238
|
-
}
|
|
1239
|
-
function createTurnReplayResponse(contentType, clientBody, turnMarker, trace) {
|
|
1240
|
-
const encoder = new TextEncoder();
|
|
1241
|
-
const marker = new ReadableStream({
|
|
1242
|
-
start(controller) {
|
|
1243
|
-
controller.enqueue(encoder.encode(`${JSON.stringify(turnMarker)}
|
|
1244
|
-
`));
|
|
1245
|
-
controller.close();
|
|
1246
|
-
}
|
|
1247
|
-
});
|
|
1248
|
-
const body = concatStreams([marker, clientBody]);
|
|
1249
|
-
return new Response(body, {
|
|
1250
|
-
headers: {
|
|
1251
|
-
"Content-Type": contentType,
|
|
1252
|
-
"Cache-Control": "no-cache",
|
|
1253
|
-
"X-Turn-Id": turnMarker.turnId,
|
|
1254
|
-
"Server-Timing": admissionServerTiming(trace)
|
|
1255
|
-
}
|
|
1256
|
-
});
|
|
1257
|
-
}
|
|
1258
|
-
|
|
1259
|
-
// src/chat-routes/minted-thread.ts
|
|
1260
|
-
var MINTED_THREAD_ID = /^[0-9a-f]{32}$/;
|
|
1261
|
-
var MINTED_THREAD_TITLE_MAX = 200;
|
|
1262
|
-
function bindTurnThreadCreation(options, body) {
|
|
1263
|
-
return async (owner) => {
|
|
1264
|
-
const requested = body.newThread;
|
|
1265
|
-
const id = typeof requested === "object" && requested !== null ? requested.id : void 0;
|
|
1266
|
-
if (id !== body.threadId || !MINTED_THREAD_ID.test(body.threadId) || options.arm && !options.arm(owner)) {
|
|
1267
|
-
return { ok: false, response: Response.json({ error: "Thread not found" }, { status: 404 }) };
|
|
1268
|
-
}
|
|
1269
|
-
const rawTitle = requested.title;
|
|
1270
|
-
const trimmed = typeof rawTitle === "string" ? rawTitle.trim() : "";
|
|
1271
|
-
const title = trimmed ? trimmed.slice(0, MINTED_THREAD_TITLE_MAX) : null;
|
|
1272
|
-
const created = await options.insert({ id: body.threadId, title, ...owner });
|
|
1273
|
-
return created == null ? { ok: false, response: Response.json({ error: "Thread id already exists" }, { status: 409 }) } : { ok: true, thread: created };
|
|
1274
|
-
};
|
|
1275
|
-
}
|
|
1276
|
-
|
|
1277
|
-
// src/chat-routes/turn-routes.ts
|
|
1278
|
-
function earlyTurnWork(release, log) {
|
|
1279
|
-
let free = release;
|
|
1280
|
-
let also = [];
|
|
1281
|
-
return {
|
|
1282
|
-
handOff: () => {
|
|
1283
|
-
free = void 0;
|
|
1284
|
-
also = [];
|
|
1285
|
-
},
|
|
1286
|
-
add: (more) => {
|
|
1287
|
-
also.push(more);
|
|
1288
|
-
},
|
|
1289
|
-
release: async () => {
|
|
1290
|
-
const product = free;
|
|
1291
|
-
const route = also;
|
|
1292
|
-
free = void 0;
|
|
1293
|
-
also = [];
|
|
1294
|
-
for (const stop of route) stop();
|
|
1295
|
-
if (!product) return;
|
|
1296
|
-
try {
|
|
1297
|
-
await product();
|
|
1298
|
-
} catch (err) {
|
|
1299
|
-
log("[chat-routes] afterAuthorize release failed", { error: err instanceof Error ? err.message : String(err) });
|
|
1300
|
-
}
|
|
1301
|
-
}
|
|
1302
|
-
};
|
|
1303
|
-
}
|
|
1304
|
-
async function admitTurn(request, trace, options) {
|
|
1305
|
-
const parse = trace.start("admission.parse", "admission");
|
|
1306
|
-
const [rawBody, badBody] = await parseJsonObjectBody(request);
|
|
1307
|
-
if (badBody) return { ok: false, response: badBody };
|
|
1308
|
-
let parsed;
|
|
1309
|
-
try {
|
|
1310
|
-
parsed = validateTurnBody(rawBody, options.maxInlinePartBytes);
|
|
1311
|
-
} catch (err) {
|
|
1312
|
-
if (err instanceof ChatTurnInputError) return { ok: false, response: errorResponse(err) };
|
|
1313
|
-
throw err;
|
|
1314
|
-
}
|
|
1315
|
-
parse.end();
|
|
1316
|
-
const prefetch = options.prefetch && startTurnPrefetch(options.prefetch, { request, body: parsed.payload, trace });
|
|
1317
|
-
let handedOff = false;
|
|
1318
|
-
try {
|
|
1319
|
-
const auth = await trace.measure("admission.authorize", "admission", (span) => options.authorize({ request, intent: "turn", body: parsed.payload, trace, span, createThread: options.createThread && bindTurnThreadCreation(options.createThread, parsed.payload), ...prefetch ? { identified: prefetch.identified } : {} }));
|
|
1320
|
-
if (!auth.ok) return { ok: false, response: auth.response };
|
|
1321
|
-
const refusal = await namedModelRefusal(options.modelPolicy, parsed.payload, trace);
|
|
1322
|
-
if (refusal) return { ok: false, response: refusal };
|
|
1323
|
-
const admitted = { ok: true, parsed, auth, trace, ...prefetch ? { prefetched: prefetch.admitted(auth) } : {} };
|
|
1324
|
-
handedOff = true;
|
|
1325
|
-
return admitted;
|
|
1326
|
-
} finally {
|
|
1327
|
-
if (!handedOff) prefetch?.discard();
|
|
1328
|
-
}
|
|
1329
|
-
}
|
|
1330
|
-
function createChatTurnRoutes(options) {
|
|
1331
|
-
const log = options.log ?? ((message, meta) => console.error(message, meta ?? ""));
|
|
1332
|
-
const draftStore = options.store;
|
|
1333
|
-
if (options.incrementalPersistence && !storeSupportsDraftPersistence(draftStore)) {
|
|
1334
|
-
throw new Error(
|
|
1335
|
-
"incrementalPersistence requires a store with updateMessage() \u2014 `/chat-store`'s createChatStore has it; a product store must implement it or omit the option"
|
|
1336
|
-
);
|
|
1337
|
-
}
|
|
1338
|
-
const draftTuning = options.incrementalPersistence === false || !storeSupportsDraftPersistence(draftStore) ? null : options.incrementalPersistence ?? {};
|
|
1339
|
-
const progressMessages = { ...TURN_ROUTE_PHASE_MESSAGES, ...options.progressMessages };
|
|
1340
|
-
const traceOptions = options.turnTrace === false ? void 0 : options.turnTrace;
|
|
1341
|
-
const traceSink = options.turnTrace === false ? void 0 : traceOptions?.sink ?? createIntelligenceTurnTraceSink({ project: options.projectId });
|
|
1342
|
-
function startIntentLine(admitted) {
|
|
1343
|
-
const intentLine = options.intentLine;
|
|
1344
|
-
if (!intentLine) return void 0;
|
|
1345
|
-
const { parsed, auth } = admitted;
|
|
1346
|
-
const threadId = parsed.payload.threadId;
|
|
1347
|
-
const controller = new AbortController();
|
|
1348
|
-
let timer;
|
|
1349
|
-
const deadline = new Promise((resolve) => {
|
|
1350
|
-
timer = setTimeout(() => {
|
|
1351
|
-
controller.abort();
|
|
1352
|
-
resolve(null);
|
|
1353
|
-
}, intentLine.timeoutMs ?? INTENT_LINE_TIMEOUT_MS);
|
|
1354
|
-
});
|
|
1355
|
-
const generated = (async () => {
|
|
1356
|
-
const history = (await options.store.listMessages(threadId)).slice(-INTENT_LINE_HISTORY_MESSAGES).map((message) => ({ role: message.role, content: message.content }));
|
|
1357
|
-
const text = await intentLine.generate({
|
|
1358
|
-
content: parsed.content,
|
|
1359
|
-
history,
|
|
1360
|
-
tenantId: auth.tenantId,
|
|
1361
|
-
userId: auth.userId,
|
|
1362
|
-
threadId,
|
|
1363
|
-
context: auth.context,
|
|
1364
|
-
signal: controller.signal
|
|
1365
|
-
});
|
|
1366
|
-
const line = typeof text === "string" ? text.trim().replace(/\s+/g, " ") : "";
|
|
1367
|
-
return line ? line.slice(0, INTENT_LINE_MAX_CHARS) : null;
|
|
1368
|
-
})().catch((err) => {
|
|
1369
|
-
if (!controller.signal.aborted) {
|
|
1370
|
-
log("[chat-routes] intent line failed; showing none", { error: err instanceof Error ? err.message : String(err) });
|
|
1371
|
-
}
|
|
1372
|
-
return null;
|
|
1373
|
-
});
|
|
1374
|
-
return Promise.race([generated, deadline]).finally(() => clearTimeout(timer));
|
|
1375
|
-
}
|
|
1376
|
-
const runAdmittedTurn2 = createAdmittedTurnRunner({
|
|
1377
|
-
options,
|
|
1378
|
-
draftStore,
|
|
1379
|
-
draftTuning,
|
|
1380
|
-
progressMessages,
|
|
1381
|
-
traceOptions,
|
|
1382
|
-
traceSink,
|
|
1383
|
-
startIntentLine,
|
|
1384
|
-
log
|
|
1385
|
-
});
|
|
1386
|
-
async function turn(request, ctx) {
|
|
1387
|
-
const admitted = await admitTurn(request, createTurnTrace({ d1QueryCounter: currentD1QueryCounter() }), options);
|
|
1388
|
-
if (!admitted.ok) return admitted.response;
|
|
1389
|
-
if (request.headers.get(TURN_PROGRESS_HEADER) !== TURN_PROGRESS_FIRST) return runTurn(request, ctx, admitted);
|
|
1390
|
-
return progressFirstTurn(request, ctx, admitted);
|
|
1391
|
-
}
|
|
1392
|
-
function progressFirstTurn(request, ctx, admitted) {
|
|
1393
|
-
const receivedAt = Date.now();
|
|
1394
|
-
const encoder = new TextEncoder();
|
|
1395
|
-
const { readable, writable } = new TransformStream();
|
|
1396
|
-
const writer = writable.getWriter();
|
|
1397
|
-
let clientGone = false;
|
|
1398
|
-
const write = async (chunk) => {
|
|
1399
|
-
if (clientGone) return;
|
|
1400
|
-
try {
|
|
1401
|
-
await writer.write(chunk);
|
|
1402
|
-
} catch {
|
|
1403
|
-
clientGone = true;
|
|
1404
|
-
}
|
|
1405
|
-
};
|
|
1406
|
-
const writeEvent = (event) => write(encoder.encode(`${JSON.stringify(event)}
|
|
1407
|
-
`));
|
|
1408
|
-
const progress = (phase, message) => {
|
|
1409
|
-
void writeEvent(buildTurnPhaseEvent(phase, message, { sinceRequestMs: Date.now() - receivedAt }));
|
|
1410
|
-
};
|
|
1411
|
-
progress("accepted", progressMessages.accepted);
|
|
1412
|
-
const intent = startIntentLine(admitted);
|
|
1413
|
-
let piping = false;
|
|
1414
|
-
let atLineStart = true;
|
|
1415
|
-
let pendingIntent = null;
|
|
1416
|
-
if (intent) {
|
|
1417
|
-
void intent.then((text) => {
|
|
1418
|
-
if (!text || clientGone) return;
|
|
1419
|
-
const event = { type: TURN_INTENT_EVENT, data: { text, sinceRequestMs: Date.now() - receivedAt } };
|
|
1420
|
-
if (!piping || atLineStart) void writeEvent(event);
|
|
1421
|
-
else pendingIntent = event;
|
|
1422
|
-
});
|
|
1423
|
-
}
|
|
1424
|
-
const pump = (async () => {
|
|
1425
|
-
let inner;
|
|
1426
|
-
try {
|
|
1427
|
-
inner = await runTurn(request, ctx, admitted, progress, intent);
|
|
1428
|
-
} catch (err) {
|
|
1429
|
-
log("[chat-routes] turn failed before it streamed", { error: err instanceof Error ? err.message : String(err) });
|
|
1430
|
-
inner = Response.json({ error: "The turn failed before it started. Try again." }, { status: 500 });
|
|
1431
|
-
}
|
|
1432
|
-
if (!inner.body || !inner.headers.get("x-turn-id")) {
|
|
1433
|
-
await writeEvent(await turnResponseEventFor(inner));
|
|
1434
|
-
} else {
|
|
1435
|
-
const reader = inner.body.getReader();
|
|
1436
|
-
piping = true;
|
|
1437
|
-
try {
|
|
1438
|
-
for (; ; ) {
|
|
1439
|
-
const { done, value } = await reader.read();
|
|
1440
|
-
if (done) break;
|
|
1441
|
-
atLineStart = value.length > 0 && value[value.length - 1] === 10;
|
|
1442
|
-
await write(value);
|
|
1443
|
-
if (atLineStart && pendingIntent) {
|
|
1444
|
-
const event = pendingIntent;
|
|
1445
|
-
pendingIntent = null;
|
|
1446
|
-
await writeEvent(event);
|
|
1447
|
-
}
|
|
1448
|
-
if (clientGone) {
|
|
1449
|
-
await reader.cancel().catch(() => {
|
|
1450
|
-
});
|
|
1451
|
-
break;
|
|
1452
|
-
}
|
|
1453
|
-
}
|
|
1454
|
-
} catch (err) {
|
|
1455
|
-
log("[chat-routes] progress-first pipe failed", { error: err instanceof Error ? err.message : String(err) });
|
|
1456
|
-
}
|
|
1457
|
-
}
|
|
1458
|
-
if (!clientGone) await writer.close().catch(() => {
|
|
1459
|
-
});
|
|
1460
|
-
})();
|
|
1461
|
-
if (ctx?.waitUntil) ctx.waitUntil(pump);
|
|
1462
|
-
else void pump.catch(() => {
|
|
1463
|
-
});
|
|
1464
|
-
return new Response(readable, {
|
|
1465
|
-
headers: {
|
|
1466
|
-
"Content-Type": "application/x-ndjson",
|
|
1467
|
-
"Cache-Control": "no-cache",
|
|
1468
|
-
[TURN_PROGRESS_HEADER]: TURN_PROGRESS_FIRST
|
|
1469
|
-
}
|
|
1470
|
-
});
|
|
1471
|
-
}
|
|
1472
|
-
function startAfterAuthorize(request, admitted) {
|
|
1473
|
-
let release;
|
|
1474
|
-
const { parsed, auth, trace } = admitted;
|
|
1475
|
-
if (options.afterAuthorize) {
|
|
1476
|
-
try {
|
|
1477
|
-
const started = options.afterAuthorize({
|
|
1478
|
-
request,
|
|
1479
|
-
body: parsed.payload,
|
|
1480
|
-
tenantId: auth.tenantId,
|
|
1481
|
-
userId: auth.userId,
|
|
1482
|
-
threadId: parsed.payload.threadId,
|
|
1483
|
-
context: auth.context,
|
|
1484
|
-
trace
|
|
1485
|
-
});
|
|
1486
|
-
if (typeof started === "function") release = started;
|
|
1487
|
-
} catch (err) {
|
|
1488
|
-
log("[chat-routes] afterAuthorize failed; the turn goes on without it", { error: err instanceof Error ? err.message : String(err) });
|
|
1489
|
-
}
|
|
1490
|
-
}
|
|
1491
|
-
return earlyTurnWork(release, log);
|
|
1492
|
-
}
|
|
1493
|
-
async function runTurn(request, ctx, admitted, progress, startedIntent) {
|
|
1494
|
-
const early = startAfterAuthorize(request, admitted);
|
|
1495
|
-
try {
|
|
1496
|
-
return await runAdmittedTurn2(request, ctx, admitted, early, progress, startedIntent);
|
|
1497
|
-
} finally {
|
|
1498
|
-
const freed = early.release();
|
|
1499
|
-
if (ctx?.waitUntil) ctx.waitUntil(freed);
|
|
1500
|
-
else void freed;
|
|
1501
|
-
}
|
|
1502
|
-
}
|
|
1503
|
-
async function replay(request, params) {
|
|
1504
|
-
const turnId = params.turnId?.trim();
|
|
1505
|
-
if (!turnId) return Response.json({ error: "Missing turnId" }, { status: 400 });
|
|
1506
|
-
const auth = await options.authorize({ request, intent: "replay", turnId });
|
|
1507
|
-
if (!auth.ok) return auth.response;
|
|
1508
|
-
const fromSeqRaw = new URL(request.url).searchParams.get("fromSeq");
|
|
1509
|
-
const fromSeq = fromSeqRaw ? Math.max(0, Math.trunc(Number(fromSeqRaw)) || 0) : 0;
|
|
1510
|
-
const encoder = new TextEncoder();
|
|
1511
|
-
const events = replayTurnEvents({
|
|
1512
|
-
store: options.turnStore,
|
|
1513
|
-
turnId,
|
|
1514
|
-
fromSeq,
|
|
1515
|
-
...options.replay?.pollMs !== void 0 ? { pollMs: options.replay.pollMs } : {},
|
|
1516
|
-
...options.replay?.timeoutMs !== void 0 ? { timeoutMs: options.replay.timeoutMs } : {}
|
|
1517
|
-
});
|
|
1518
|
-
const body = new ReadableStream({
|
|
1519
|
-
async pull(controller) {
|
|
1520
|
-
const { done, value } = await events.next();
|
|
1521
|
-
if (done) {
|
|
1522
|
-
controller.close();
|
|
1523
|
-
return;
|
|
1524
|
-
}
|
|
1525
|
-
controller.enqueue(encoder.encode(`${stampReplaySeq(value)}
|
|
1526
|
-
`));
|
|
1527
|
-
},
|
|
1528
|
-
cancel() {
|
|
1529
|
-
void events.return(void 0);
|
|
1530
|
-
}
|
|
1531
|
-
});
|
|
1532
|
-
return new Response(body, {
|
|
1533
|
-
headers: {
|
|
1534
|
-
"Content-Type": "application/x-ndjson",
|
|
1535
|
-
"Cache-Control": "no-cache"
|
|
1536
|
-
}
|
|
1537
|
-
});
|
|
1538
|
-
}
|
|
1539
|
-
async function running(request) {
|
|
1540
|
-
const threadId = new URL(request.url).searchParams.get("threadId")?.trim();
|
|
1541
|
-
if (!threadId) return Response.json({ error: "Missing threadId" }, { status: 400 });
|
|
1542
|
-
const auth = await options.authorize({ request, intent: "running", threadId });
|
|
1543
|
-
if (!auth.ok) return auth.response;
|
|
1544
|
-
const ids = await options.turnStore.listRunning?.(threadId) ?? [];
|
|
1545
|
-
return Response.json({ running: ids });
|
|
1546
|
-
}
|
|
1547
|
-
return {
|
|
1548
|
-
turn: (request, ctx) => withD1QueryCount(() => turn(request, ctx)),
|
|
1549
|
-
replay,
|
|
1550
|
-
running,
|
|
1551
|
-
interactions: options.interactions ? createInteractionAnswerRoute(options.interactions) : null
|
|
1552
|
-
};
|
|
1553
|
-
}
|
|
1554
|
-
|
|
1555
|
-
// src/chat-routes/gateway-adapter.ts
|
|
1556
|
-
async function* streamChatRouteAsSandboxEvents(options) {
|
|
1557
|
-
const parentSignal = options.signal ?? options.request.signal;
|
|
1558
|
-
if (parentSignal.aborted) return;
|
|
1559
|
-
let reader;
|
|
1560
|
-
const stopForwarding = () => void reader?.cancel();
|
|
1561
|
-
parentSignal.addEventListener("abort", stopForwarding, { once: true });
|
|
1562
|
-
const request = new Request(options.request.url, {
|
|
1563
|
-
...chatTurnRequestInit(options.payload),
|
|
1564
|
-
headers: options.request.headers
|
|
1565
|
-
});
|
|
1566
|
-
request.headers.set("Content-Type", "application/json");
|
|
1567
|
-
let complete = false;
|
|
1568
|
-
try {
|
|
1569
|
-
const response = await options.routes.turn(
|
|
1570
|
-
request,
|
|
1571
|
-
{
|
|
1572
|
-
...options.waitUntil ? { waitUntil: options.waitUntil } : {},
|
|
1573
|
-
...options.executionLimits ? { executionLimits: options.executionLimits } : {}
|
|
1574
|
-
}
|
|
1575
|
-
);
|
|
1576
|
-
if (!response.ok) {
|
|
1577
|
-
await response.body?.cancel().catch(() => void 0);
|
|
1578
|
-
throw new Error(`chat route rejected the gateway turn (${response.status})`);
|
|
1579
|
-
}
|
|
1580
|
-
if (!response.body) throw new Error("chat route returned no stream");
|
|
1581
|
-
reader = response.body.getReader();
|
|
1582
|
-
if (parentSignal.aborted) {
|
|
1583
|
-
await reader.cancel().catch(() => void 0);
|
|
1584
|
-
return;
|
|
1585
|
-
}
|
|
1586
|
-
const decoder = new TextDecoder();
|
|
1587
|
-
let buffer = "";
|
|
1588
|
-
for (; ; ) {
|
|
1589
|
-
const chunk = await reader.read();
|
|
1590
|
-
if (chunk.done) {
|
|
1591
|
-
complete = true;
|
|
1592
|
-
buffer += decoder.decode();
|
|
1593
|
-
const event = toSandboxEvent(parseEvent(buffer));
|
|
1594
|
-
if (event) yield event;
|
|
1595
|
-
break;
|
|
1596
|
-
}
|
|
1597
|
-
buffer += decoder.decode(chunk.value, { stream: true });
|
|
1598
|
-
const lines = buffer.split("\n");
|
|
1599
|
-
buffer = lines.pop() ?? "";
|
|
1600
|
-
for (const line of lines) {
|
|
1601
|
-
const event = toSandboxEvent(parseEvent(line));
|
|
1602
|
-
if (event) yield event;
|
|
1603
|
-
}
|
|
1604
|
-
}
|
|
1605
|
-
} finally {
|
|
1606
|
-
if (!complete) await reader?.cancel().catch(() => void 0);
|
|
1607
|
-
reader?.releaseLock();
|
|
1608
|
-
parentSignal.removeEventListener("abort", stopForwarding);
|
|
1609
|
-
}
|
|
1610
|
-
}
|
|
1611
|
-
function parseEvent(line) {
|
|
1612
|
-
if (!line.trim()) return null;
|
|
1613
|
-
let value;
|
|
1614
|
-
try {
|
|
1615
|
-
value = JSON.parse(line);
|
|
1616
|
-
} catch {
|
|
1617
|
-
throw new Error("chat route returned invalid NDJSON");
|
|
1618
|
-
}
|
|
1619
|
-
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
|
1620
|
-
throw new Error("chat route returned a non-object event");
|
|
1621
|
-
}
|
|
1622
|
-
const event = value;
|
|
1623
|
-
if (event.type !== void 0 && typeof event.type !== "string") {
|
|
1624
|
-
throw new Error("chat route returned an invalid event type");
|
|
1625
|
-
}
|
|
1626
|
-
const data = event.data;
|
|
1627
|
-
if (data !== void 0 && (!data || typeof data !== "object" || Array.isArray(data))) {
|
|
1628
|
-
throw new Error("chat route returned invalid event data");
|
|
1629
|
-
}
|
|
1630
|
-
const dataUsage = data && data.usage;
|
|
1631
|
-
if (dataUsage !== void 0 && (!dataUsage || typeof dataUsage !== "object" || Array.isArray(dataUsage))) {
|
|
1632
|
-
throw new Error("chat route returned invalid data.usage");
|
|
1633
|
-
}
|
|
1634
|
-
if (event.text !== void 0 && typeof event.text !== "string") {
|
|
1635
|
-
throw new Error("chat route returned an invalid text event");
|
|
1636
|
-
}
|
|
1637
|
-
const usage = event.usage;
|
|
1638
|
-
if (usage !== void 0 && (!usage || typeof usage !== "object" || Array.isArray(usage))) {
|
|
1639
|
-
throw new Error("chat route returned invalid usage");
|
|
1640
|
-
}
|
|
1641
|
-
if (event.toolName !== void 0 && typeof event.toolName !== "string") {
|
|
1642
|
-
throw new Error("chat route returned an invalid tool name");
|
|
1643
|
-
}
|
|
1644
|
-
return {
|
|
1645
|
-
...typeof event.type === "string" ? { type: event.type } : {},
|
|
1646
|
-
...data ? { data } : {},
|
|
1647
|
-
...typeof event.text === "string" ? { text: event.text } : {},
|
|
1648
|
-
...usage ? { usage } : {},
|
|
1649
|
-
...typeof event.toolName === "string" ? { toolName: event.toolName } : {}
|
|
1650
|
-
};
|
|
1651
|
-
}
|
|
1652
|
-
function isNonNegativeSafeInteger(value) {
|
|
1653
|
-
return typeof value === "number" && Number.isSafeInteger(value) && value >= 0;
|
|
1654
|
-
}
|
|
1655
|
-
function aliasedSafeInteger(usage, field, alias) {
|
|
1656
|
-
const value = usage[field];
|
|
1657
|
-
const aliasValue = alias ? usage[alias] : void 0;
|
|
1658
|
-
for (const candidate of [value, aliasValue]) {
|
|
1659
|
-
if (candidate !== void 0 && !isNonNegativeSafeInteger(candidate)) {
|
|
1660
|
-
throw new Error(`chat route returned invalid usage.${field}`);
|
|
1661
|
-
}
|
|
1662
|
-
}
|
|
1663
|
-
if (value !== void 0 && aliasValue !== void 0 && value !== aliasValue) {
|
|
1664
|
-
throw new Error(`chat route returned conflicting usage.${field}`);
|
|
1665
|
-
}
|
|
1666
|
-
return value ?? aliasValue;
|
|
1667
|
-
}
|
|
1668
|
-
function aliasedNonNegativeNumber(usage, field, alias) {
|
|
1669
|
-
const value = usage[field];
|
|
1670
|
-
const aliasValue = alias ? usage[alias] : void 0;
|
|
1671
|
-
for (const candidate of [value, aliasValue]) {
|
|
1672
|
-
if (candidate !== void 0 && (typeof candidate !== "number" || !Number.isFinite(candidate) || candidate < 0)) {
|
|
1673
|
-
throw new Error(`chat route returned invalid usage.${field}`);
|
|
1674
|
-
}
|
|
1675
|
-
}
|
|
1676
|
-
if (value !== void 0 && aliasValue !== void 0 && value !== aliasValue) {
|
|
1677
|
-
throw new Error(`chat route returned conflicting usage.${field}`);
|
|
1678
|
-
}
|
|
1679
|
-
return value ?? aliasValue;
|
|
1680
|
-
}
|
|
1681
|
-
function providerUsageUpdate(value) {
|
|
1682
|
-
if (!value || typeof value !== "object" || Array.isArray(value)) return null;
|
|
1683
|
-
const source = value;
|
|
1684
|
-
const usage = {};
|
|
1685
|
-
const inputTokens = aliasedSafeInteger(source, "inputTokens", "promptTokens");
|
|
1686
|
-
const outputTokens = aliasedSafeInteger(source, "outputTokens", "completionTokens");
|
|
1687
|
-
const reasoningTokens = aliasedSafeInteger(source, "reasoningTokens");
|
|
1688
|
-
const toolTokens = aliasedSafeInteger(source, "toolTokens");
|
|
1689
|
-
const toolCallCount = aliasedSafeInteger(source, "toolCallCount");
|
|
1690
|
-
const providerCostUsd = aliasedNonNegativeNumber(source, "providerCostUsd", "costUsd");
|
|
1691
|
-
if (inputTokens !== void 0) usage.inputTokens = inputTokens;
|
|
1692
|
-
if (outputTokens !== void 0) usage.outputTokens = outputTokens;
|
|
1693
|
-
if (reasoningTokens !== void 0) usage.reasoningTokens = reasoningTokens;
|
|
1694
|
-
if (toolTokens !== void 0) usage.toolTokens = toolTokens;
|
|
1695
|
-
if (toolCallCount !== void 0) usage.toolCallCount = toolCallCount;
|
|
1696
|
-
if (providerCostUsd !== void 0) usage.providerCostUsd = providerCostUsd;
|
|
1697
|
-
if (source.budgetEnforced !== void 0) {
|
|
1698
|
-
if (typeof source.budgetEnforced !== "boolean") {
|
|
1699
|
-
throw new Error("chat route returned invalid usage.budgetEnforced");
|
|
1700
|
-
}
|
|
1701
|
-
usage.budgetEnforced = source.budgetEnforced;
|
|
1702
|
-
}
|
|
1703
|
-
return Object.keys(usage).length > 0 ? usage : null;
|
|
1704
|
-
}
|
|
1705
|
-
function toSandboxEvent(event) {
|
|
1706
|
-
if (!event) return null;
|
|
1707
|
-
if (event.type === "text") {
|
|
1708
|
-
if (event.text === void 0) throw new Error("chat route text event had no text");
|
|
1709
|
-
return {
|
|
1710
|
-
type: "message.part.updated",
|
|
1711
|
-
data: { part: { type: "text" }, delta: event.text }
|
|
1712
|
-
};
|
|
1713
|
-
}
|
|
1714
|
-
if (event.type === "reasoning") return null;
|
|
1715
|
-
if (event.type === "usage" || event.usage || event.data?.usage) {
|
|
1716
|
-
const usage = providerUsageUpdate(event.usage ?? event.data?.usage);
|
|
1717
|
-
if (!usage) return null;
|
|
1718
|
-
return { type: "usage", data: { usage } };
|
|
1719
|
-
}
|
|
1720
|
-
if (event.type === "tool_result") {
|
|
1721
|
-
if (!event.toolName) throw new Error("chat route tool result had no tool name");
|
|
1722
|
-
return { type: "tool_result", data: { tool: { name: event.toolName } } };
|
|
1723
|
-
}
|
|
1724
|
-
if (event.type === "input-required" && !event.data?.inputRequired) {
|
|
1725
|
-
const prompt = typeof event.data?.prompt === "string" ? event.data.prompt : void 0;
|
|
1726
|
-
return {
|
|
1727
|
-
type: "input-required",
|
|
1728
|
-
data: { inputRequired: prompt ? { prompt } : {} }
|
|
1729
|
-
};
|
|
1730
|
-
}
|
|
1731
|
-
return {
|
|
1732
|
-
...event.type ? { type: event.type } : {},
|
|
1733
|
-
...event.data ? { data: event.data } : {}
|
|
1734
|
-
};
|
|
1735
|
-
}
|
|
1736
|
-
|
|
1737
|
-
export {
|
|
1738
|
-
isDraftContentEvent,
|
|
1739
|
-
createAssistantDraftWriter,
|
|
1740
|
-
storeSupportsDraftPersistence,
|
|
1741
|
-
rowIdOf,
|
|
1742
|
-
assistantRowIdForTurn,
|
|
1743
|
-
INTENT_LINE_TIMEOUT_MS,
|
|
1744
|
-
INTENT_LINE_HISTORY_MESSAGES,
|
|
1745
|
-
INTENT_LINE_MAX_CHARS,
|
|
1746
|
-
createChatTurnRoutes,
|
|
1747
|
-
streamChatRouteAsSandboxEvents
|
|
1748
|
-
};
|
|
1749
|
-
//# sourceMappingURL=chunk-EFQUAWWF.js.map
|