@tangle-network/agent-app 4.0.36 → 4.0.37
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/assistant/index.js +5 -5
- package/dist/chat-react/index.js +3 -3
- package/dist/chat-routes/index.js +16 -8
- package/dist/chat-routes/index.js.map +1 -1
- package/dist/chat-routes/turn-event-stream.d.ts +10 -1
- package/dist/chat-routes/turn-execution.d.ts +34 -3
- package/dist/chat-routes/turn-intent.d.ts +5 -0
- package/dist/chat-routes/turn-progress.d.ts +25 -1
- package/dist/chat-routes/turn-route-contracts.d.ts +31 -2
- package/dist/chat-routes/turn-settlement.d.ts +23 -0
- package/dist/chat-store/index.js +2 -2
- package/dist/{chunk-NAMSFODC.js → chunk-4YBNKXUE.js} +54 -4
- package/dist/chunk-4YBNKXUE.js.map +1 -0
- package/dist/{chunk-K2JPSXKI.js → chunk-6YLVFV3F.js} +39 -3
- package/dist/chunk-6YLVFV3F.js.map +1 -0
- package/dist/{chunk-23YED7HQ.js → chunk-77UNTK66.js} +355 -288
- package/dist/chunk-77UNTK66.js.map +1 -0
- package/dist/{chunk-KPCNNFBO.js → chunk-AOSQ5JCO.js} +41 -2
- package/dist/{chunk-KPCNNFBO.js.map → chunk-AOSQ5JCO.js.map} +1 -1
- package/dist/{chunk-GOKFD7ZZ.js → chunk-BMRKOPL6.js} +2 -2
- package/dist/{chunk-E633CJFH.js → chunk-COUT2WD5.js} +2 -2
- package/dist/{chunk-7VJCMEEE.js → chunk-DPD7LY3T.js} +11 -6
- package/dist/chunk-DPD7LY3T.js.map +1 -0
- package/dist/{chunk-MQCAO5VW.js → chunk-MGW25KMR.js} +3 -3
- package/dist/{chunk-IAK227OO.js → chunk-Q7NELHRU.js} +2 -2
- package/dist/{chunk-4YZKDWMB.js → chunk-SADNHPAY.js} +2 -2
- package/dist/operator/index.js +2 -2
- package/dist/preset-cloudflare/index.js +5 -5
- package/dist/public-consultation/index.d.ts +1 -1
- package/dist/public-consultation/index.js +6 -6
- package/dist/sandbox/index.js +3 -3
- package/dist/turn-stream/cancellation.d.ts +16 -0
- package/dist/turn-stream/core.d.ts +4 -0
- package/dist/turn-stream/do.d.ts +4 -2
- package/dist/turn-stream/hosted-turn.d.ts +20 -0
- package/dist/turn-stream/index.js +122 -5
- package/dist/turn-stream/index.js.map +1 -1
- package/dist/web-react/chat-stream.d.ts +4 -2
- package/dist/web-react/index.js +13 -5
- package/package.json +1 -1
- package/dist/chunk-23YED7HQ.js.map +0 -1
- package/dist/chunk-7VJCMEEE.js.map +0 -1
- package/dist/chunk-K2JPSXKI.js.map +0 -1
- package/dist/chunk-NAMSFODC.js.map +0 -1
- /package/dist/{chunk-GOKFD7ZZ.js.map → chunk-BMRKOPL6.js.map} +0 -0
- /package/dist/{chunk-E633CJFH.js.map → chunk-COUT2WD5.js.map} +0 -0
- /package/dist/{chunk-MQCAO5VW.js.map → chunk-MGW25KMR.js.map} +0 -0
- /package/dist/{chunk-IAK227OO.js.map → chunk-Q7NELHRU.js.map} +0 -0
- /package/dist/{chunk-4YZKDWMB.js.map → chunk-SADNHPAY.js.map} +0 -0
|
@@ -30,7 +30,7 @@ import {
|
|
|
30
30
|
sandboxRunFailureDetail,
|
|
31
31
|
streamSandboxPrompt,
|
|
32
32
|
turnStorageBindings
|
|
33
|
-
} from "./chunk-
|
|
33
|
+
} from "./chunk-MGW25KMR.js";
|
|
34
34
|
import {
|
|
35
35
|
redactErrorMessage
|
|
36
36
|
} from "./chunk-MH74DY2I.js";
|
|
@@ -42,6 +42,7 @@ import {
|
|
|
42
42
|
addStepFinishUsage,
|
|
43
43
|
concatStreams,
|
|
44
44
|
createAssistantDraftWriter,
|
|
45
|
+
createTurnView,
|
|
45
46
|
failureCode,
|
|
46
47
|
failureCodeOf,
|
|
47
48
|
failureIds,
|
|
@@ -61,9 +62,10 @@ import {
|
|
|
61
62
|
streamWithModelFailover,
|
|
62
63
|
tapRawEvents,
|
|
63
64
|
transformVisibleTextParts,
|
|
65
|
+
turnControlResponse,
|
|
64
66
|
withStreamHeartbeat,
|
|
65
67
|
withoutToolList
|
|
66
|
-
} from "./chunk-
|
|
68
|
+
} from "./chunk-4YBNKXUE.js";
|
|
67
69
|
import {
|
|
68
70
|
assistantRowIdForTurn,
|
|
69
71
|
turnTraceUrl
|
|
@@ -76,7 +78,7 @@ import {
|
|
|
76
78
|
historyContentWithAttachments,
|
|
77
79
|
mentionInputToPart,
|
|
78
80
|
toChatMessageParts
|
|
79
|
-
} from "./chunk-
|
|
81
|
+
} from "./chunk-SADNHPAY.js";
|
|
80
82
|
import {
|
|
81
83
|
asRecord,
|
|
82
84
|
asString,
|
|
@@ -107,7 +109,7 @@ import {
|
|
|
107
109
|
} from "./chunk-ALIWB2P3.js";
|
|
108
110
|
import {
|
|
109
111
|
forwardTurnToHost
|
|
110
|
-
} from "./chunk-
|
|
112
|
+
} from "./chunk-AOSQ5JCO.js";
|
|
111
113
|
import {
|
|
112
114
|
DEFAULT_RUNNING_TURN_RENEW_INTERVAL_MS,
|
|
113
115
|
coalesceDeltas,
|
|
@@ -121,6 +123,9 @@ import {
|
|
|
121
123
|
DISPATCH_REQUEST_MAX_BYTES,
|
|
122
124
|
DISPATCH_STRUCTURAL_RESERVE_BYTES,
|
|
123
125
|
INLINE_PARTS_MAX_BYTES,
|
|
126
|
+
TURN_HOST_CONTROL_EVENT,
|
|
127
|
+
TURN_HOST_CONTROL_FIRST,
|
|
128
|
+
TURN_HOST_CONTROL_HEADER,
|
|
124
129
|
TURN_INTENT_EVENT,
|
|
125
130
|
TURN_PROGRESS_FIRST,
|
|
126
131
|
TURN_PROGRESS_HEADER,
|
|
@@ -134,7 +139,7 @@ import {
|
|
|
134
139
|
parseChatTurnParts,
|
|
135
140
|
parseMentions,
|
|
136
141
|
turnResponseEventFor
|
|
137
|
-
} from "./chunk-
|
|
142
|
+
} from "./chunk-6YLVFV3F.js";
|
|
138
143
|
import {
|
|
139
144
|
buildModelChain
|
|
140
145
|
} from "./chunk-L3QR274L.js";
|
|
@@ -2389,15 +2394,7 @@ function dispatchFirstFor(selected, args, trace) {
|
|
|
2389
2394
|
return selected.router && !args.userMessageId ? dispatchProducerFirst(selected.produce, args, trace) : void 0;
|
|
2390
2395
|
}
|
|
2391
2396
|
|
|
2392
|
-
// src/chat-routes/turn-
|
|
2393
|
-
async function readTurnHistory(store, threadId) {
|
|
2394
|
-
return (await store.listMessages(threadId)).map((m) => ({
|
|
2395
|
-
id: m.id,
|
|
2396
|
-
role: m.role,
|
|
2397
|
-
content: m.content,
|
|
2398
|
-
parts: m.parts ?? null
|
|
2399
|
-
}));
|
|
2400
|
-
}
|
|
2397
|
+
// src/chat-routes/turn-settlement.ts
|
|
2401
2398
|
function modelAttributionFields(attribution) {
|
|
2402
2399
|
return {
|
|
2403
2400
|
...attribution?.requestedModel ? { requestedModel: attribution.requestedModel } : {},
|
|
@@ -2421,6 +2418,171 @@ function settledTurnTiming(turnStreamId, at, trace) {
|
|
|
2421
2418
|
});
|
|
2422
2419
|
return { ...measured, trace };
|
|
2423
2420
|
}
|
|
2421
|
+
function createTurnSettlement(state, trace, options, executionId, traceSink, identity, traceOptions, context, turnStreamId, log, ctx, userMessageId) {
|
|
2422
|
+
const assistantRowId = () => state.assistantMessageId ?? state.draft?.rowId() ?? null;
|
|
2423
|
+
const endTrace = (failed, terminalError) => {
|
|
2424
|
+
const failureCode2 = failed ? failureCodeOf(state.lastFailureData) ?? (terminalError instanceof Error ? terminalError.name : "turn_failed") : void 0;
|
|
2425
|
+
const snapshot = trace.finish(failureCode2 === void 0 ? {} : { error: failureCode2 });
|
|
2426
|
+
if (state.traceEnded) return snapshot;
|
|
2427
|
+
state.traceEnded = true;
|
|
2428
|
+
const handedOff = state.completionHandoff || state.legacyHandoff;
|
|
2429
|
+
console.info(JSON.stringify({
|
|
2430
|
+
event: "agent_app_turn_trace",
|
|
2431
|
+
project: options.projectId,
|
|
2432
|
+
executionId,
|
|
2433
|
+
otlpTraceId: otlpTraceId(snapshot.traceId),
|
|
2434
|
+
failed,
|
|
2435
|
+
...failureCode2 ? { failureCode: failureCode2 } : {},
|
|
2436
|
+
...handedOff ? { handedOff: true } : {},
|
|
2437
|
+
...state.gatedTurn ? { gated: true } : {},
|
|
2438
|
+
...snapshot.summary
|
|
2439
|
+
}));
|
|
2440
|
+
const sink = traceSink;
|
|
2441
|
+
if (sink) {
|
|
2442
|
+
state.pendingTraceExport = () => sink(snapshot, {
|
|
2443
|
+
"tangle.sessionId": identity.sessionId,
|
|
2444
|
+
"tangle.userId": identity.userId,
|
|
2445
|
+
"tangle.tenantId": identity.tenantId,
|
|
2446
|
+
"tangle.turn.failed": failed,
|
|
2447
|
+
...state.gatedTurn ? { "tangle.turn.gated": true } : {},
|
|
2448
|
+
...handedOff ? { "tangle.turn.handed_off": true } : {},
|
|
2449
|
+
...failureCode2 ? { "tangle.turn.failure_code": failureCode2 } : {},
|
|
2450
|
+
...traceOptions?.labels?.({ identity, executionId, context }) ?? {}
|
|
2451
|
+
});
|
|
2452
|
+
}
|
|
2453
|
+
return snapshot;
|
|
2454
|
+
};
|
|
2455
|
+
const startLifecycle = (gated = false) => {
|
|
2456
|
+
if (gated) state.gatedTurn = true;
|
|
2457
|
+
state.turnStartedAtMs = Date.now();
|
|
2458
|
+
if (!gated) state.turnStarted = true;
|
|
2459
|
+
const lifecycle = options.lifecycle;
|
|
2460
|
+
if (!lifecycle?.onTurnStart) return;
|
|
2461
|
+
return (async () => {
|
|
2462
|
+
try {
|
|
2463
|
+
await lifecycle.onTurnStart({
|
|
2464
|
+
identity,
|
|
2465
|
+
executionId,
|
|
2466
|
+
turnStreamId,
|
|
2467
|
+
context,
|
|
2468
|
+
startedAt: state.turnStartedAtMs,
|
|
2469
|
+
requestReceivedAt: trace.startedAt,
|
|
2470
|
+
admissionMs: state.turnStartedAtMs - trace.startedAt
|
|
2471
|
+
});
|
|
2472
|
+
} catch (err) {
|
|
2473
|
+
log("[chat-routes] lifecycle.onTurnStart failed", {
|
|
2474
|
+
turnId: turnStreamId,
|
|
2475
|
+
error: err instanceof Error ? err.message : String(err)
|
|
2476
|
+
});
|
|
2477
|
+
}
|
|
2478
|
+
})();
|
|
2479
|
+
};
|
|
2480
|
+
const fireTerminalLifecycle = async (failed, terminalError) => {
|
|
2481
|
+
if (state.lifecycleSettled) return;
|
|
2482
|
+
state.lifecycleSettled = true;
|
|
2483
|
+
terminalError ??= state.producerError;
|
|
2484
|
+
const snapshot = endTrace(failed, terminalError);
|
|
2485
|
+
const lifecycle = options.lifecycle;
|
|
2486
|
+
const backend = {
|
|
2487
|
+
userMessageId: userMessageId(),
|
|
2488
|
+
backendTerminal: Boolean(state.producer) && (state.producer?.terminalObserved?.() ?? true)
|
|
2489
|
+
};
|
|
2490
|
+
const durationMs = Date.now() - state.turnStartedAtMs;
|
|
2491
|
+
const timing = settledTurnTiming(turnStreamId, {
|
|
2492
|
+
requestAt: trace.startedAt,
|
|
2493
|
+
startedAt: state.turnStartedAtMs,
|
|
2494
|
+
firstVisibleAt: state.firstVisibleAtMs,
|
|
2495
|
+
firstTextAt: state.firstTextAtMs,
|
|
2496
|
+
model: state.producer?.model,
|
|
2497
|
+
failure: failed ? { data: state.lastFailureData, error: terminalError } : void 0
|
|
2498
|
+
}, snapshot.summary);
|
|
2499
|
+
try {
|
|
2500
|
+
if (!lifecycle) {
|
|
2501
|
+
} else if (failed) {
|
|
2502
|
+
await lifecycle.onTurnError?.({
|
|
2503
|
+
identity,
|
|
2504
|
+
executionId,
|
|
2505
|
+
turnStreamId,
|
|
2506
|
+
context,
|
|
2507
|
+
durationMs,
|
|
2508
|
+
...timing,
|
|
2509
|
+
...backend,
|
|
2510
|
+
error: terminalError ?? state.lastFailureData ?? new Error("chat turn failed")
|
|
2511
|
+
});
|
|
2512
|
+
} else {
|
|
2513
|
+
const failoverInfo = state.producer?.modelFailover?.();
|
|
2514
|
+
const attribution = state.producer?.modelAttribution?.();
|
|
2515
|
+
const completed = {
|
|
2516
|
+
identity,
|
|
2517
|
+
executionId,
|
|
2518
|
+
turnStreamId,
|
|
2519
|
+
context,
|
|
2520
|
+
durationMs,
|
|
2521
|
+
...timing,
|
|
2522
|
+
...backend,
|
|
2523
|
+
finalText: state.cancellationAbort?.signal.aborted ? "" : state.producer?.finalText() ?? "",
|
|
2524
|
+
usage: state.producer?.usage?.() ?? {},
|
|
2525
|
+
assistantMessageId: assistantRowId(),
|
|
2526
|
+
...state.gatedTurn ? { gated: true } : {},
|
|
2527
|
+
...state.producer?.model ? { model: state.producer.model } : {},
|
|
2528
|
+
...modelAttributionFields(attribution),
|
|
2529
|
+
...failoverInfo ? { modelFailover: failoverInfo } : {}
|
|
2530
|
+
};
|
|
2531
|
+
if (!state.gatedTurn) state.settledTurn = completed;
|
|
2532
|
+
await lifecycle.onTurnComplete?.(completed);
|
|
2533
|
+
}
|
|
2534
|
+
} catch (err) {
|
|
2535
|
+
log("[chat-routes] lifecycle terminal hook failed", {
|
|
2536
|
+
turnId: turnStreamId,
|
|
2537
|
+
error: err instanceof Error ? err.message : String(err)
|
|
2538
|
+
});
|
|
2539
|
+
}
|
|
2540
|
+
};
|
|
2541
|
+
const exportTrace = async () => {
|
|
2542
|
+
const send = state.pendingTraceExport;
|
|
2543
|
+
state.pendingTraceExport = void 0;
|
|
2544
|
+
if (!send) return;
|
|
2545
|
+
try {
|
|
2546
|
+
await send();
|
|
2547
|
+
} catch (err) {
|
|
2548
|
+
log("[chat-routes] turn trace sink failed", {
|
|
2549
|
+
turnId: turnStreamId,
|
|
2550
|
+
error: err instanceof Error ? err.message : String(err)
|
|
2551
|
+
});
|
|
2552
|
+
}
|
|
2553
|
+
};
|
|
2554
|
+
const runAfterSettle = async () => {
|
|
2555
|
+
const info = state.settledTurn;
|
|
2556
|
+
state.settledTurn = void 0;
|
|
2557
|
+
const afterSettle = options.lifecycle?.afterSettle;
|
|
2558
|
+
if (!info || !afterSettle) return;
|
|
2559
|
+
try {
|
|
2560
|
+
await afterSettle(info);
|
|
2561
|
+
} catch (err) {
|
|
2562
|
+
log("[chat-routes] lifecycle.afterSettle failed", {
|
|
2563
|
+
turnId: turnStreamId,
|
|
2564
|
+
error: err instanceof Error ? err.message : String(err)
|
|
2565
|
+
});
|
|
2566
|
+
}
|
|
2567
|
+
};
|
|
2568
|
+
const exportTraceInBackground = () => {
|
|
2569
|
+
const sent = exportTrace();
|
|
2570
|
+
if (ctx?.waitUntil) ctx.waitUntil(sent);
|
|
2571
|
+
else void sent.catch(() => {
|
|
2572
|
+
});
|
|
2573
|
+
};
|
|
2574
|
+
return { endTrace, exportTraceInBackground, startLifecycle, fireTerminalLifecycle, assistantRowId, exportTrace, runAfterSettle };
|
|
2575
|
+
}
|
|
2576
|
+
|
|
2577
|
+
// src/chat-routes/turn-execution.ts
|
|
2578
|
+
async function readTurnHistory(store, threadId) {
|
|
2579
|
+
return (await store.listMessages(threadId)).map((m) => ({
|
|
2580
|
+
id: m.id,
|
|
2581
|
+
role: m.role,
|
|
2582
|
+
content: m.content,
|
|
2583
|
+
parts: m.parts ?? null
|
|
2584
|
+
}));
|
|
2585
|
+
}
|
|
2424
2586
|
function createAdmittedTurnRunner(dependencies) {
|
|
2425
2587
|
return runAdmittedTurn.bind(null, dependencies);
|
|
2426
2588
|
}
|
|
@@ -2436,16 +2598,25 @@ async function runAdmittedTurn({
|
|
|
2436
2598
|
log
|
|
2437
2599
|
}, request, ctx, admitted, early, progress, startedIntent) {
|
|
2438
2600
|
const { parsed, auth, trace } = admitted;
|
|
2439
|
-
const
|
|
2601
|
+
const hostStop = ctx?.hostControl?.signal;
|
|
2602
|
+
hostStop?.throwIfAborted();
|
|
2603
|
+
const intentLine = startedIntent ?? startIntentLine(admitted, ctx?.hostControl?.signal);
|
|
2440
2604
|
const { payload, content, fileParts, mentions, turnId } = parsed;
|
|
2441
2605
|
const { tenantId, userId, context } = auth;
|
|
2442
2606
|
const preparedBatch = await prepareAdmittedTurn(options, trace, log, tenantId, userId, payload, context, request, progress, content, turnId, draftTuning, ctx?.waitUntil);
|
|
2443
2607
|
if (preparedBatch instanceof Response) return preparedBatch;
|
|
2608
|
+
hostStop?.throwIfAborted();
|
|
2444
2609
|
const { turnStreamId, identity, executionId, chatTurn, readHistory, historyBeforeLock } = preparedBatch;
|
|
2445
2610
|
const redact = options.redactValues ? valueRedactor(await options.redactValues({ tenantId, userId, threadId: payload.threadId })) : void 0;
|
|
2611
|
+
hostStop?.throwIfAborted();
|
|
2446
2612
|
const prompt = fileParts.length === 0 ? content : content ? [{ type: "text", text: content }, ...fileParts] : [...fileParts];
|
|
2447
2613
|
const state = createAdmittedTurnState();
|
|
2448
|
-
if (options.cancel) state.cancellationAbort = new AbortController();
|
|
2614
|
+
if (options.cancel || ctx?.hostControl) state.cancellationAbort = new AbortController();
|
|
2615
|
+
if (hostStop) {
|
|
2616
|
+
const abort = () => state.cancellationAbort.abort(hostStop.reason);
|
|
2617
|
+
if (hostStop.aborted) abort();
|
|
2618
|
+
else hostStop.addEventListener("abort", abort, { once: true });
|
|
2619
|
+
}
|
|
2449
2620
|
state.nativeTiming = createNativeTimingTransfer(() => ({
|
|
2450
2621
|
turnStreamId,
|
|
2451
2622
|
requestReceivedAt: trace.startedAt,
|
|
@@ -2484,31 +2655,41 @@ async function runAdmittedTurn({
|
|
|
2484
2655
|
};
|
|
2485
2656
|
const addressed = await addressAgents({ options, mentions, identity, context, produceArgs, trace });
|
|
2486
2657
|
if (addressed instanceof Response) return addressed;
|
|
2658
|
+
hostStop?.throwIfAborted();
|
|
2487
2659
|
produceArgs = addressed;
|
|
2488
|
-
const
|
|
2489
|
-
const dispatched = produceArgs.agents ? void 0 : dispatchFirstFor(selected, produceArgs, trace);
|
|
2490
|
-
if (dispatched) early.add(dispatched.refuse);
|
|
2491
|
-
const turnLock = options.turnLock;
|
|
2492
|
-
if (turnLock) {
|
|
2493
|
-
const acquired = await trace.measure("admission.lock", "admission", () => turnLock.acquire(produceArgs));
|
|
2494
|
-
if (!acquired.acquired) return acquired.response;
|
|
2495
|
-
state.lockAcquired = true;
|
|
2496
|
-
state.lockHandle = acquired.handle;
|
|
2497
|
-
if (acquired.cancellation) {
|
|
2498
|
-
const cancellation = acquired.cancellation;
|
|
2499
|
-
const abort = () => state.executionAbort.abort(cancellation.signal.reason);
|
|
2500
|
-
if (cancellation.signal.aborted) abort();
|
|
2501
|
-
else cancellation.signal.addEventListener("abort", abort, { once: true });
|
|
2502
|
-
state.closeCancellation = async () => {
|
|
2503
|
-
cancellation.signal.removeEventListener("abort", abort);
|
|
2504
|
-
await cancellation.close();
|
|
2505
|
-
};
|
|
2506
|
-
}
|
|
2507
|
-
}
|
|
2508
|
-
progress?.("preparing", progressMessages.preparing);
|
|
2509
|
-
const { endTrace, exportTraceInBackground, fireTerminalLifecycle, assistantRowId, exportTrace, runAfterSettle } = createTurnSettlement(state, trace, options, executionId, traceSink, identity, traceOptions, context, turnStreamId, log, ctx, () => produceArgs.userMessageId);
|
|
2660
|
+
const { endTrace, exportTraceInBackground, startLifecycle, fireTerminalLifecycle, assistantRowId, exportTrace, runAfterSettle } = createTurnSettlement(state, trace, options, executionId, traceSink, identity, traceOptions, context, turnStreamId, log, ctx, () => produceArgs.userMessageId);
|
|
2510
2661
|
try {
|
|
2511
|
-
const
|
|
2662
|
+
const selected = selectTurnProducer(options.produce, produceArgs);
|
|
2663
|
+
hostStop?.throwIfAborted();
|
|
2664
|
+
const dispatched = produceArgs.agents ? void 0 : dispatchFirstFor(selected, produceArgs, trace);
|
|
2665
|
+
if (dispatched) early.add(dispatched.refuse);
|
|
2666
|
+
const turnLock = options.turnLock;
|
|
2667
|
+
if (turnLock) {
|
|
2668
|
+
const acquired = await trace.measure("admission.lock", "admission", () => {
|
|
2669
|
+
hostStop?.throwIfAborted();
|
|
2670
|
+
return turnLock.acquire(produceArgs);
|
|
2671
|
+
});
|
|
2672
|
+
if (!acquired.acquired) return acquired.response;
|
|
2673
|
+
state.lockAcquired = true;
|
|
2674
|
+
state.lockHandle = acquired.handle;
|
|
2675
|
+
if (acquired.cancellation) {
|
|
2676
|
+
const cancellation = acquired.cancellation;
|
|
2677
|
+
const abort = () => state.executionAbort.abort(cancellation.signal.reason);
|
|
2678
|
+
if (cancellation.signal.aborted) abort();
|
|
2679
|
+
else cancellation.signal.addEventListener("abort", abort, { once: true });
|
|
2680
|
+
state.closeCancellation = async () => {
|
|
2681
|
+
cancellation.signal.removeEventListener("abort", abort);
|
|
2682
|
+
await cancellation.close();
|
|
2683
|
+
};
|
|
2684
|
+
}
|
|
2685
|
+
}
|
|
2686
|
+
hostStop?.throwIfAborted();
|
|
2687
|
+
progress?.("preparing", progressMessages.preparing);
|
|
2688
|
+
const [history] = state.lockAcquired ? await trace.measure("admission.history_recheck", "admission", () => {
|
|
2689
|
+
hostStop?.throwIfAborted();
|
|
2690
|
+
return Promise.all([readHistory(), markRunning()]);
|
|
2691
|
+
}) : [];
|
|
2692
|
+
hostStop?.throwIfAborted();
|
|
2512
2693
|
if (state.lockAcquired && JSON.stringify(history) !== historyBeforeLock) {
|
|
2513
2694
|
await releaseBeforeDrain("error");
|
|
2514
2695
|
return Response.json({
|
|
@@ -2521,16 +2702,20 @@ async function runAdmittedTurn({
|
|
|
2521
2702
|
let userMessageInserted = false;
|
|
2522
2703
|
if (insertUserMessage) {
|
|
2523
2704
|
userMessageId = rowIdOf(
|
|
2524
|
-
await trace.measure("admission.persist_user", "admission", () =>
|
|
2525
|
-
|
|
2526
|
-
|
|
2527
|
-
|
|
2528
|
-
|
|
2529
|
-
|
|
2705
|
+
await trace.measure("admission.persist_user", "admission", () => {
|
|
2706
|
+
hostStop?.throwIfAborted();
|
|
2707
|
+
return options.store.appendMessage({
|
|
2708
|
+
threadId: payload.threadId,
|
|
2709
|
+
role: "user",
|
|
2710
|
+
content,
|
|
2711
|
+
parts: userPartsWithFiles(chatTurn.userParts, fileParts, mentions)
|
|
2712
|
+
});
|
|
2713
|
+
})
|
|
2530
2714
|
);
|
|
2531
2715
|
userMessageInserted = userMessageId !== null;
|
|
2532
2716
|
}
|
|
2533
2717
|
produceArgs = { ...produceArgs, userMessageId, userMessageInserted };
|
|
2718
|
+
hostStop?.throwIfAborted();
|
|
2534
2719
|
const contextGate = options.contextGate;
|
|
2535
2720
|
if (contextGate) {
|
|
2536
2721
|
const gate = await trace.measure("admission.gate", "admission", () => contextGate(produceArgs));
|
|
@@ -2548,37 +2733,21 @@ async function runAdmittedTurn({
|
|
|
2548
2733
|
exportTraceInBackground();
|
|
2549
2734
|
return gate.response;
|
|
2550
2735
|
}
|
|
2551
|
-
|
|
2552
|
-
|
|
2553
|
-
if (options.lifecycle?.onTurnStart) {
|
|
2554
|
-
try {
|
|
2555
|
-
await options.lifecycle.onTurnStart({
|
|
2556
|
-
identity,
|
|
2557
|
-
executionId,
|
|
2558
|
-
turnStreamId,
|
|
2559
|
-
context,
|
|
2560
|
-
startedAt: state.turnStartedAtMs,
|
|
2561
|
-
requestReceivedAt: trace.startedAt,
|
|
2562
|
-
admissionMs: state.turnStartedAtMs - trace.startedAt
|
|
2563
|
-
});
|
|
2564
|
-
} catch (err) {
|
|
2565
|
-
log("[chat-routes] lifecycle.onTurnStart failed", {
|
|
2566
|
-
turnId: turnStreamId,
|
|
2567
|
-
error: err instanceof Error ? err.message : String(err)
|
|
2568
|
-
});
|
|
2569
|
-
}
|
|
2570
|
-
}
|
|
2736
|
+
const lifecycleStarted2 = startLifecycle(true);
|
|
2737
|
+
if (lifecycleStarted2) await lifecycleStarted2;
|
|
2571
2738
|
await fireTerminalLifecycle(false, void 0);
|
|
2572
2739
|
exportTraceInBackground();
|
|
2573
2740
|
return gate.response;
|
|
2574
2741
|
}
|
|
2575
2742
|
}
|
|
2743
|
+
hostStop?.throwIfAborted();
|
|
2576
2744
|
const beforeTurn = options.beforeTurn;
|
|
2577
2745
|
if (beforeTurn) {
|
|
2578
2746
|
const patch = await trace.measure("admission.before_turn", "admission", (span) => beforeTurn(produceArgs, { span }));
|
|
2579
2747
|
if (patch && Object.keys(patch).length && dispatched) throw new Error("beforeTurn cannot patch a Router turn already dispatched: its prompt is already sent");
|
|
2580
2748
|
if (patch) produceArgs = { ...produceArgs, ...patch };
|
|
2581
2749
|
}
|
|
2750
|
+
hostStop?.throwIfAborted();
|
|
2582
2751
|
const streamSetup = trace.start("admission.stream_setup", "admission");
|
|
2583
2752
|
state.tap = createBufferedTurnTap({
|
|
2584
2753
|
store: options.turnStore,
|
|
@@ -2611,27 +2780,10 @@ async function runAdmittedTurn({
|
|
|
2611
2780
|
});
|
|
2612
2781
|
if (state.completionHandoff) await state.draft.close();
|
|
2613
2782
|
}
|
|
2614
|
-
|
|
2615
|
-
|
|
2616
|
-
if (options.lifecycle?.onTurnStart) {
|
|
2617
|
-
try {
|
|
2618
|
-
await options.lifecycle.onTurnStart({
|
|
2619
|
-
identity,
|
|
2620
|
-
executionId,
|
|
2621
|
-
turnStreamId,
|
|
2622
|
-
context,
|
|
2623
|
-
startedAt: state.turnStartedAtMs,
|
|
2624
|
-
requestReceivedAt: trace.startedAt,
|
|
2625
|
-
admissionMs: state.turnStartedAtMs - trace.startedAt
|
|
2626
|
-
});
|
|
2627
|
-
} catch (err) {
|
|
2628
|
-
log("[chat-routes] lifecycle.onTurnStart failed", {
|
|
2629
|
-
turnId: turnStreamId,
|
|
2630
|
-
error: err instanceof Error ? err.message : String(err)
|
|
2631
|
-
});
|
|
2632
|
-
}
|
|
2633
|
-
}
|
|
2783
|
+
const lifecycleStarted = startLifecycle();
|
|
2784
|
+
if (lifecycleStarted) await lifecycleStarted;
|
|
2634
2785
|
streamSetup.end();
|
|
2786
|
+
hostStop?.throwIfAborted();
|
|
2635
2787
|
const { result } = createTurnEngine(identity, ctx, log, state, dispatched, produceArgs, trace, options, selected.produce, request, context, payload, assistantRowId, redact);
|
|
2636
2788
|
const { clientBody } = startTurnDrain(result, log, turnStreamId, state, endTrace, exportTrace, trace, ctx, request, fireTerminalLifecycle, releaseLock, runAfterSettle, early.settle);
|
|
2637
2789
|
early.handOff();
|
|
@@ -2659,6 +2811,7 @@ function createAdmittedTurnState() {
|
|
|
2659
2811
|
lockHandle: void 0,
|
|
2660
2812
|
lockReleased: false,
|
|
2661
2813
|
producer: void 0,
|
|
2814
|
+
producerError: void 0,
|
|
2662
2815
|
assistantMessageId: void 0,
|
|
2663
2816
|
runFailed: false,
|
|
2664
2817
|
lastFailureData: void 0,
|
|
@@ -2739,135 +2892,6 @@ async function prepareAdmittedTurn(options, trace, log, tenantId, userId, payloa
|
|
|
2739
2892
|
const turnStreamId = crypto.randomUUID();
|
|
2740
2893
|
return { turnStreamId, identity, executionId, chatTurn, readHistory, historyBeforeLock };
|
|
2741
2894
|
}
|
|
2742
|
-
function createTurnSettlement(state, trace, options, executionId, traceSink, identity, traceOptions, context, turnStreamId, log, ctx, userMessageId) {
|
|
2743
|
-
const assistantRowId = () => state.assistantMessageId ?? state.draft?.rowId() ?? null;
|
|
2744
|
-
const endTrace = (failed, terminalError) => {
|
|
2745
|
-
const failureCode2 = failed ? failureCodeOf(state.lastFailureData) ?? (terminalError instanceof Error ? terminalError.name : "turn_failed") : void 0;
|
|
2746
|
-
const snapshot = trace.finish(failureCode2 === void 0 ? {} : { error: failureCode2 });
|
|
2747
|
-
if (state.traceEnded) return snapshot;
|
|
2748
|
-
state.traceEnded = true;
|
|
2749
|
-
const handedOff = state.completionHandoff || state.legacyHandoff;
|
|
2750
|
-
console.info(JSON.stringify({
|
|
2751
|
-
event: "agent_app_turn_trace",
|
|
2752
|
-
project: options.projectId,
|
|
2753
|
-
executionId,
|
|
2754
|
-
otlpTraceId: otlpTraceId(snapshot.traceId),
|
|
2755
|
-
failed,
|
|
2756
|
-
...failureCode2 ? { failureCode: failureCode2 } : {},
|
|
2757
|
-
...handedOff ? { handedOff: true } : {},
|
|
2758
|
-
...state.gatedTurn ? { gated: true } : {},
|
|
2759
|
-
...snapshot.summary
|
|
2760
|
-
}));
|
|
2761
|
-
const sink = traceSink;
|
|
2762
|
-
if (sink) {
|
|
2763
|
-
state.pendingTraceExport = () => sink(snapshot, {
|
|
2764
|
-
"tangle.sessionId": identity.sessionId,
|
|
2765
|
-
"tangle.userId": identity.userId,
|
|
2766
|
-
"tangle.tenantId": identity.tenantId,
|
|
2767
|
-
"tangle.turn.failed": failed,
|
|
2768
|
-
...state.gatedTurn ? { "tangle.turn.gated": true } : {},
|
|
2769
|
-
...handedOff ? { "tangle.turn.handed_off": true } : {},
|
|
2770
|
-
...failureCode2 ? { "tangle.turn.failure_code": failureCode2 } : {},
|
|
2771
|
-
...traceOptions?.labels?.({ identity, executionId, context }) ?? {}
|
|
2772
|
-
});
|
|
2773
|
-
}
|
|
2774
|
-
return snapshot;
|
|
2775
|
-
};
|
|
2776
|
-
const fireTerminalLifecycle = async (failed, terminalError) => {
|
|
2777
|
-
if (state.lifecycleSettled) return;
|
|
2778
|
-
state.lifecycleSettled = true;
|
|
2779
|
-
const snapshot = endTrace(failed, terminalError);
|
|
2780
|
-
const lifecycle = options.lifecycle;
|
|
2781
|
-
const backend = {
|
|
2782
|
-
userMessageId: userMessageId(),
|
|
2783
|
-
backendTerminal: Boolean(state.producer) && (state.producer?.terminalObserved?.() ?? true)
|
|
2784
|
-
};
|
|
2785
|
-
const durationMs = Date.now() - state.turnStartedAtMs;
|
|
2786
|
-
const timing = settledTurnTiming(turnStreamId, {
|
|
2787
|
-
requestAt: trace.startedAt,
|
|
2788
|
-
startedAt: state.turnStartedAtMs,
|
|
2789
|
-
firstVisibleAt: state.firstVisibleAtMs,
|
|
2790
|
-
firstTextAt: state.firstTextAtMs,
|
|
2791
|
-
model: state.producer?.model,
|
|
2792
|
-
failure: failed ? { data: state.lastFailureData, error: terminalError } : void 0
|
|
2793
|
-
}, snapshot.summary);
|
|
2794
|
-
try {
|
|
2795
|
-
if (!lifecycle) {
|
|
2796
|
-
} else if (failed) {
|
|
2797
|
-
await lifecycle.onTurnError?.({
|
|
2798
|
-
identity,
|
|
2799
|
-
executionId,
|
|
2800
|
-
turnStreamId,
|
|
2801
|
-
context,
|
|
2802
|
-
durationMs,
|
|
2803
|
-
...timing,
|
|
2804
|
-
...backend,
|
|
2805
|
-
error: terminalError ?? state.lastFailureData ?? new Error("chat turn failed")
|
|
2806
|
-
});
|
|
2807
|
-
} else {
|
|
2808
|
-
const failoverInfo = state.producer?.modelFailover?.();
|
|
2809
|
-
const attribution = state.producer?.modelAttribution?.();
|
|
2810
|
-
const completed = {
|
|
2811
|
-
identity,
|
|
2812
|
-
executionId,
|
|
2813
|
-
turnStreamId,
|
|
2814
|
-
context,
|
|
2815
|
-
durationMs,
|
|
2816
|
-
...timing,
|
|
2817
|
-
...backend,
|
|
2818
|
-
finalText: state.cancellationAbort?.signal.aborted ? "" : state.producer?.finalText() ?? "",
|
|
2819
|
-
usage: state.producer?.usage?.() ?? {},
|
|
2820
|
-
assistantMessageId: assistantRowId(),
|
|
2821
|
-
...state.gatedTurn ? { gated: true } : {},
|
|
2822
|
-
...state.producer?.model ? { model: state.producer.model } : {},
|
|
2823
|
-
...modelAttributionFields(attribution),
|
|
2824
|
-
...failoverInfo ? { modelFailover: failoverInfo } : {}
|
|
2825
|
-
};
|
|
2826
|
-
if (!state.gatedTurn) state.settledTurn = completed;
|
|
2827
|
-
await lifecycle.onTurnComplete?.(completed);
|
|
2828
|
-
}
|
|
2829
|
-
} catch (err) {
|
|
2830
|
-
log("[chat-routes] lifecycle terminal hook failed", {
|
|
2831
|
-
turnId: turnStreamId,
|
|
2832
|
-
error: err instanceof Error ? err.message : String(err)
|
|
2833
|
-
});
|
|
2834
|
-
}
|
|
2835
|
-
};
|
|
2836
|
-
const exportTrace = async () => {
|
|
2837
|
-
const send = state.pendingTraceExport;
|
|
2838
|
-
state.pendingTraceExport = void 0;
|
|
2839
|
-
if (!send) return;
|
|
2840
|
-
try {
|
|
2841
|
-
await send();
|
|
2842
|
-
} catch (err) {
|
|
2843
|
-
log("[chat-routes] turn trace sink failed", {
|
|
2844
|
-
turnId: turnStreamId,
|
|
2845
|
-
error: err instanceof Error ? err.message : String(err)
|
|
2846
|
-
});
|
|
2847
|
-
}
|
|
2848
|
-
};
|
|
2849
|
-
const runAfterSettle = async () => {
|
|
2850
|
-
const info = state.settledTurn;
|
|
2851
|
-
state.settledTurn = void 0;
|
|
2852
|
-
const afterSettle = options.lifecycle?.afterSettle;
|
|
2853
|
-
if (!info || !afterSettle) return;
|
|
2854
|
-
try {
|
|
2855
|
-
await afterSettle(info);
|
|
2856
|
-
} catch (err) {
|
|
2857
|
-
log("[chat-routes] lifecycle.afterSettle failed", {
|
|
2858
|
-
turnId: turnStreamId,
|
|
2859
|
-
error: err instanceof Error ? err.message : String(err)
|
|
2860
|
-
});
|
|
2861
|
-
}
|
|
2862
|
-
};
|
|
2863
|
-
const exportTraceInBackground = () => {
|
|
2864
|
-
const sent = exportTrace();
|
|
2865
|
-
if (ctx?.waitUntil) ctx.waitUntil(sent);
|
|
2866
|
-
else void sent.catch(() => {
|
|
2867
|
-
});
|
|
2868
|
-
};
|
|
2869
|
-
return { endTrace, exportTraceInBackground, fireTerminalLifecycle, assistantRowId, exportTrace, runAfterSettle };
|
|
2870
|
-
}
|
|
2871
2895
|
function createTurnEngine(identity, ctx, log, state, dispatched, produceArgs, trace, options, produce, request, context, payload, assistantRowId, redact) {
|
|
2872
2896
|
const result = handleChatTurn({
|
|
2873
2897
|
identity,
|
|
@@ -2896,13 +2920,19 @@ function createTurnEngine(identity, ctx, log, state, dispatched, produceArgs, tr
|
|
|
2896
2920
|
if (ctx?.cancelOnDisconnect) {
|
|
2897
2921
|
source = abortOnSignal(source, request.signal);
|
|
2898
2922
|
}
|
|
2923
|
+
if (ctx?.hostControl) source = abortOnSignal(source, ctx.hostControl.signal);
|
|
2899
2924
|
if (options.onRawEvent) {
|
|
2900
2925
|
source = tapRawEvents(source, (event) => options.onRawEvent(event, context), log);
|
|
2901
2926
|
}
|
|
2902
2927
|
if (options.heartbeat) {
|
|
2903
2928
|
source = withStreamHeartbeat(source, options.heartbeat.intervalMs, options.heartbeat.event);
|
|
2904
2929
|
}
|
|
2905
|
-
|
|
2930
|
+
try {
|
|
2931
|
+
for await (const event of source) yield event;
|
|
2932
|
+
} catch (error) {
|
|
2933
|
+
state.producerError = state.lastFailureData ?? error;
|
|
2934
|
+
throw error;
|
|
2935
|
+
}
|
|
2906
2936
|
})(),
|
|
2907
2937
|
finalText: () => state.cancellationAbort?.signal.aborted ? "" : state.producer?.finalText() ?? ""
|
|
2908
2938
|
}),
|
|
@@ -3251,19 +3281,63 @@ function createChatPrewarm(options, box) {
|
|
|
3251
3281
|
return (request, ctx) => route(request, ctx);
|
|
3252
3282
|
}
|
|
3253
3283
|
|
|
3284
|
+
// src/chat-routes/turn-intent.ts
|
|
3285
|
+
function startTurnIntentLine(options, log, admitted, stop) {
|
|
3286
|
+
const intentLine = options.intentLine;
|
|
3287
|
+
if (!intentLine) return void 0;
|
|
3288
|
+
const { parsed, auth } = admitted;
|
|
3289
|
+
const threadId = parsed.payload.threadId;
|
|
3290
|
+
const controller = new AbortController();
|
|
3291
|
+
const signal = stop ? AbortSignal.any([controller.signal, stop]) : controller.signal;
|
|
3292
|
+
let timer;
|
|
3293
|
+
const deadline = new Promise((resolve) => {
|
|
3294
|
+
timer = setTimeout(() => {
|
|
3295
|
+
controller.abort();
|
|
3296
|
+
resolve(null);
|
|
3297
|
+
}, intentLine.timeoutMs ?? INTENT_LINE_TIMEOUT_MS);
|
|
3298
|
+
});
|
|
3299
|
+
const generated = (async () => {
|
|
3300
|
+
signal.throwIfAborted();
|
|
3301
|
+
const history = (await options.store.listMessages(threadId)).slice(-INTENT_LINE_HISTORY_MESSAGES).map((message) => ({ role: message.role, content: message.content }));
|
|
3302
|
+
signal.throwIfAborted();
|
|
3303
|
+
const text = await intentLine.generate({
|
|
3304
|
+
content: parsed.content,
|
|
3305
|
+
history,
|
|
3306
|
+
tenantId: auth.tenantId,
|
|
3307
|
+
userId: auth.userId,
|
|
3308
|
+
threadId,
|
|
3309
|
+
context: auth.context,
|
|
3310
|
+
signal
|
|
3311
|
+
});
|
|
3312
|
+
const line = typeof text === "string" ? text.trim().replace(/\s+/g, " ") : "";
|
|
3313
|
+
return line ? line.slice(0, INTENT_LINE_MAX_CHARS) : null;
|
|
3314
|
+
})().catch((err) => {
|
|
3315
|
+
if (!signal.aborted) {
|
|
3316
|
+
log("[chat-routes] intent line failed; showing none", { error: err instanceof Error ? err.message : String(err) });
|
|
3317
|
+
}
|
|
3318
|
+
return null;
|
|
3319
|
+
});
|
|
3320
|
+
return Promise.race([generated, deadline]).finally(() => clearTimeout(timer));
|
|
3321
|
+
}
|
|
3322
|
+
|
|
3254
3323
|
// src/chat-routes/turn-routes.ts
|
|
3255
|
-
async function
|
|
3324
|
+
async function parseTurn(request, trace) {
|
|
3256
3325
|
const parse = trace.start("admission.parse", "admission");
|
|
3257
3326
|
const [rawBody, badBody] = await parseJsonObjectBody(request);
|
|
3258
|
-
if (badBody) return
|
|
3327
|
+
if (badBody) return badBody;
|
|
3259
3328
|
let parsed;
|
|
3260
3329
|
try {
|
|
3261
3330
|
parsed = validateTurnBody(rawBody);
|
|
3262
3331
|
} catch (err) {
|
|
3263
|
-
if (err instanceof ChatTurnInputError) return
|
|
3332
|
+
if (err instanceof ChatTurnInputError) return errorResponse(err);
|
|
3264
3333
|
throw err;
|
|
3265
3334
|
}
|
|
3266
3335
|
parse.end();
|
|
3336
|
+
return parsed;
|
|
3337
|
+
}
|
|
3338
|
+
async function admitTurn(request, trace, options, waitUntil, parsedBody) {
|
|
3339
|
+
const parsed = parsedBody ?? await parseTurn(request, trace);
|
|
3340
|
+
if (parsed instanceof Response) return { ok: false, response: parsed };
|
|
3267
3341
|
const prefetch = options.prefetch && startTurnPrefetch(options.prefetch, { request, body: parsed.payload, trace });
|
|
3268
3342
|
let handedOff = false;
|
|
3269
3343
|
try {
|
|
@@ -3294,40 +3368,7 @@ function createChatTurnRoutes(input) {
|
|
|
3294
3368
|
const draftTuning = storeSupportsDraftPersistence(draftStore) ? {} : null;
|
|
3295
3369
|
const progressMessages = { ...TURN_ROUTE_PHASE_MESSAGES, ...options.progressMessages };
|
|
3296
3370
|
const { traceOptions, traceSink, traceReader, traceReceipt } = turnTraceWiring(options.projectId, options.turnTrace);
|
|
3297
|
-
|
|
3298
|
-
const intentLine = options.intentLine;
|
|
3299
|
-
if (!intentLine) return void 0;
|
|
3300
|
-
const { parsed, auth } = admitted;
|
|
3301
|
-
const threadId = parsed.payload.threadId;
|
|
3302
|
-
const controller = new AbortController();
|
|
3303
|
-
let timer;
|
|
3304
|
-
const deadline = new Promise((resolve) => {
|
|
3305
|
-
timer = setTimeout(() => {
|
|
3306
|
-
controller.abort();
|
|
3307
|
-
resolve(null);
|
|
3308
|
-
}, intentLine.timeoutMs ?? INTENT_LINE_TIMEOUT_MS);
|
|
3309
|
-
});
|
|
3310
|
-
const generated = (async () => {
|
|
3311
|
-
const history = (await options.store.listMessages(threadId)).slice(-INTENT_LINE_HISTORY_MESSAGES).map((message) => ({ role: message.role, content: message.content }));
|
|
3312
|
-
const text = await intentLine.generate({
|
|
3313
|
-
content: parsed.content,
|
|
3314
|
-
history,
|
|
3315
|
-
tenantId: auth.tenantId,
|
|
3316
|
-
userId: auth.userId,
|
|
3317
|
-
threadId,
|
|
3318
|
-
context: auth.context,
|
|
3319
|
-
signal: controller.signal
|
|
3320
|
-
});
|
|
3321
|
-
const line = typeof text === "string" ? text.trim().replace(/\s+/g, " ") : "";
|
|
3322
|
-
return line ? line.slice(0, INTENT_LINE_MAX_CHARS) : null;
|
|
3323
|
-
})().catch((err) => {
|
|
3324
|
-
if (!controller.signal.aborted) {
|
|
3325
|
-
log("[chat-routes] intent line failed; showing none", { error: err instanceof Error ? err.message : String(err) });
|
|
3326
|
-
}
|
|
3327
|
-
return null;
|
|
3328
|
-
});
|
|
3329
|
-
return Promise.race([generated, deadline]).finally(() => clearTimeout(timer));
|
|
3330
|
-
}
|
|
3371
|
+
const startIntentLine = (admitted, stop) => startTurnIntentLine(options, log, admitted, stop);
|
|
3331
3372
|
const runAdmittedTurn2 = createAdmittedTurnRunner({
|
|
3332
3373
|
options,
|
|
3333
3374
|
draftStore,
|
|
@@ -3348,6 +3389,9 @@ function createChatTurnRoutes(input) {
|
|
|
3348
3389
|
return response;
|
|
3349
3390
|
} catch (err) {
|
|
3350
3391
|
if (err?.remote === true) throw err;
|
|
3392
|
+
if (request.headers.get(TURN_HOST_CONTROL_HEADER) === TURN_HOST_CONTROL_FIRST) {
|
|
3393
|
+
return Response.json({ error: "The turn host is unavailable.", code: "turn_control_unavailable" }, { status: 503 });
|
|
3394
|
+
}
|
|
3351
3395
|
log("[chat-routes] turn host unavailable; the turn runs in this request", {
|
|
3352
3396
|
class: "turn_host_unavailable",
|
|
3353
3397
|
error: err instanceof Error ? err.name : typeof err
|
|
@@ -3357,53 +3401,75 @@ function createChatTurnRoutes(input) {
|
|
|
3357
3401
|
}
|
|
3358
3402
|
async function turn(request, ctx) {
|
|
3359
3403
|
const requestContext = ctx?.waitUntil ? { ...ctx, waitUntil: ctx.waitUntil.bind(ctx) } : ctx;
|
|
3360
|
-
const
|
|
3404
|
+
const trace = createTurnTrace({ d1QueryCounter: currentD1QueryCounter() });
|
|
3405
|
+
if (request.headers.get(TURN_HOST_CONTROL_HEADER) === TURN_HOST_CONTROL_FIRST) {
|
|
3406
|
+
const control = requestContext?.hosted && requestContext.hostControl;
|
|
3407
|
+
if (!options.authorizeHostControl) {
|
|
3408
|
+
return Response.json({ error: "Early hosted control is unavailable.", code: "turn_control_unavailable" }, { status: 503 });
|
|
3409
|
+
}
|
|
3410
|
+
const parsed = await parseTurn(request, trace);
|
|
3411
|
+
if (parsed instanceof Response) return parsed;
|
|
3412
|
+
const identity = await options.authorizeHostControl({ request, body: parsed.payload });
|
|
3413
|
+
if (!identity.ok) return identity.response;
|
|
3414
|
+
if (!control) return Response.json({ error: "The turn host is unavailable.", code: "turn_control_unavailable" }, { status: 503 });
|
|
3415
|
+
const namespace = turnHost?.namespace;
|
|
3416
|
+
if (!namespace || typeof namespace.idFromString !== "function") {
|
|
3417
|
+
return Response.json({ error: "The host control binding is unavailable.", code: "turn_control_unavailable" }, { status: 503 });
|
|
3418
|
+
}
|
|
3419
|
+
if (!identity.tenantId.trim()) return new Response("Invalid control identity", { status: 403 });
|
|
3420
|
+
const target = {
|
|
3421
|
+
hostId: control.hostId,
|
|
3422
|
+
workspaceId: identity.tenantId,
|
|
3423
|
+
threadId: parsed.payload.threadId,
|
|
3424
|
+
...parsed.payload.turnId ? { clientTurnId: parsed.payload.turnId } : {},
|
|
3425
|
+
...parsed.payload.retryAttemptId ? { retryAttemptId: parsed.payload.retryAttemptId } : {}
|
|
3426
|
+
};
|
|
3427
|
+
await control.bind(target, { namespace, auth: turnHost.auth });
|
|
3428
|
+
const admission = admitTurn(request, trace, options, requestContext?.waitUntil, parsed).then((result) => result.ok ? result : result.response);
|
|
3429
|
+
return progressFirstTurn(request, requestContext, admission, target);
|
|
3430
|
+
}
|
|
3431
|
+
const admitted = await admitTurn(request, trace, options, requestContext?.waitUntil);
|
|
3361
3432
|
if (!admitted.ok) return admitted.response;
|
|
3362
3433
|
if (request.headers.get(TURN_PROGRESS_HEADER) !== TURN_PROGRESS_FIRST) return runTurn(request, requestContext, admitted);
|
|
3363
3434
|
return progressFirstTurn(request, requestContext, admitted);
|
|
3364
3435
|
}
|
|
3365
|
-
function progressFirstTurn(request, ctx,
|
|
3436
|
+
function progressFirstTurn(request, ctx, admission, control) {
|
|
3366
3437
|
const receivedAt = Date.now();
|
|
3367
3438
|
const encoder = new TextEncoder();
|
|
3368
|
-
const
|
|
3369
|
-
const
|
|
3370
|
-
let clientGone = false;
|
|
3371
|
-
const write = async (chunk) => {
|
|
3372
|
-
if (clientGone) return;
|
|
3373
|
-
try {
|
|
3374
|
-
await writer.write(chunk);
|
|
3375
|
-
} catch {
|
|
3376
|
-
clientGone = true;
|
|
3377
|
-
}
|
|
3378
|
-
};
|
|
3379
|
-
const writeEvent = (event) => write(encoder.encode(`${JSON.stringify(event)}
|
|
3439
|
+
const view = createTurnView();
|
|
3440
|
+
const writeEvent = (event) => view.write(encoder.encode(`${JSON.stringify(event)}
|
|
3380
3441
|
`));
|
|
3381
3442
|
const progress = (phase, message) => {
|
|
3382
|
-
|
|
3443
|
+
writeEvent(buildTurnPhaseEvent(phase, message, { sinceRequestMs: Date.now() - receivedAt }));
|
|
3383
3444
|
};
|
|
3384
|
-
|
|
3385
|
-
const intent = startIntentLine(admitted);
|
|
3445
|
+
if (control) writeEvent({ type: TURN_HOST_CONTROL_EVENT, data: { ...control } });
|
|
3386
3446
|
let piping = false;
|
|
3387
3447
|
let atLineStart = true;
|
|
3388
3448
|
let pendingIntent = null;
|
|
3389
|
-
if (intent) {
|
|
3390
|
-
void intent.then((text) => {
|
|
3391
|
-
if (!text || clientGone) return;
|
|
3392
|
-
const event = { type: TURN_INTENT_EVENT, data: { text, sinceRequestMs: Date.now() - receivedAt } };
|
|
3393
|
-
if (!piping || atLineStart) void writeEvent(event);
|
|
3394
|
-
else pendingIntent = event;
|
|
3395
|
-
});
|
|
3396
|
-
}
|
|
3397
3449
|
const pump = (async () => {
|
|
3398
3450
|
let inner;
|
|
3399
3451
|
try {
|
|
3400
|
-
|
|
3452
|
+
const admitted = await admission;
|
|
3453
|
+
if (admitted instanceof Response) inner = admitted;
|
|
3454
|
+
else if (control && admitted.auth.tenantId !== control.workspaceId) inner = new Response("Control identity changed", { status: 403 });
|
|
3455
|
+
else if (ctx?.hostControl?.signal.aborted) inner = turnControlResponse(ctx.hostControl.signal);
|
|
3456
|
+
else {
|
|
3457
|
+
progress("accepted", progressMessages.accepted);
|
|
3458
|
+
const intent = startIntentLine(admitted, ctx?.hostControl?.signal);
|
|
3459
|
+
if (intent) void intent.then((text) => {
|
|
3460
|
+
if (!text || view.disconnected) return;
|
|
3461
|
+
const event = { type: TURN_INTENT_EVENT, data: { text, sinceRequestMs: Date.now() - receivedAt } };
|
|
3462
|
+
if (!piping || atLineStart) writeEvent(event);
|
|
3463
|
+
else pendingIntent = event;
|
|
3464
|
+
});
|
|
3465
|
+
inner = await runTurn(request, ctx, admitted, progress, intent);
|
|
3466
|
+
}
|
|
3401
3467
|
} catch (err) {
|
|
3402
3468
|
log("[chat-routes] turn failed before it streamed", { error: err instanceof Error ? err.message : String(err) });
|
|
3403
|
-
inner = err instanceof Response ? err : Response.json({ error: "The turn failed before it started. Try again." }, { status: 500 });
|
|
3469
|
+
inner = ctx?.hostControl?.signal.aborted && err === ctx.hostControl.signal.reason ? turnControlResponse(ctx.hostControl.signal) : err instanceof Response ? err : Response.json({ error: "The turn failed before it started. Try again." }, { status: 500 });
|
|
3404
3470
|
}
|
|
3405
3471
|
if (!inner.body || !inner.headers.get("x-turn-id")) {
|
|
3406
|
-
|
|
3472
|
+
writeEvent(await turnResponseEventFor(inner));
|
|
3407
3473
|
} else {
|
|
3408
3474
|
const reader = inner.body.getReader();
|
|
3409
3475
|
piping = true;
|
|
@@ -3412,13 +3478,13 @@ function createChatTurnRoutes(input) {
|
|
|
3412
3478
|
const { done, value } = await reader.read();
|
|
3413
3479
|
if (done) break;
|
|
3414
3480
|
atLineStart = value.length > 0 && value[value.length - 1] === 10;
|
|
3415
|
-
|
|
3481
|
+
view.write(value);
|
|
3416
3482
|
if (atLineStart && pendingIntent) {
|
|
3417
3483
|
const event = pendingIntent;
|
|
3418
3484
|
pendingIntent = null;
|
|
3419
|
-
|
|
3485
|
+
writeEvent(event);
|
|
3420
3486
|
}
|
|
3421
|
-
if (
|
|
3487
|
+
if (view.disconnected) {
|
|
3422
3488
|
await reader.cancel().catch(() => {
|
|
3423
3489
|
});
|
|
3424
3490
|
break;
|
|
@@ -3428,17 +3494,17 @@ function createChatTurnRoutes(input) {
|
|
|
3428
3494
|
log("[chat-routes] progress-first pipe failed", { error: err instanceof Error ? err.message : String(err) });
|
|
3429
3495
|
}
|
|
3430
3496
|
}
|
|
3431
|
-
|
|
3432
|
-
});
|
|
3497
|
+
view.close();
|
|
3433
3498
|
})();
|
|
3434
3499
|
if (ctx?.waitUntil) ctx.waitUntil(pump);
|
|
3435
3500
|
else void pump.catch(() => {
|
|
3436
3501
|
});
|
|
3437
|
-
return new Response(readable, {
|
|
3502
|
+
return new Response(view.readable, {
|
|
3438
3503
|
headers: {
|
|
3439
3504
|
"Content-Type": "application/x-ndjson",
|
|
3440
3505
|
"Cache-Control": "no-cache",
|
|
3441
|
-
[TURN_PROGRESS_HEADER]: TURN_PROGRESS_FIRST
|
|
3506
|
+
[TURN_PROGRESS_HEADER]: TURN_PROGRESS_FIRST,
|
|
3507
|
+
...control ? { [TURN_HOST_CONTROL_HEADER]: TURN_HOST_CONTROL_FIRST } : {}
|
|
3442
3508
|
}
|
|
3443
3509
|
});
|
|
3444
3510
|
}
|
|
@@ -3465,6 +3531,7 @@ function createChatTurnRoutes(input) {
|
|
|
3465
3531
|
return earlyTurnWork(release, log);
|
|
3466
3532
|
}
|
|
3467
3533
|
async function runTurn(request, ctx, admitted, progress, startedIntent) {
|
|
3534
|
+
if (ctx?.hostControl?.signal.aborted) return turnControlResponse(ctx.hostControl.signal);
|
|
3468
3535
|
const early = startAfterAuthorize(request, admitted, ctx?.waitUntil);
|
|
3469
3536
|
try {
|
|
3470
3537
|
return await runAdmittedTurn2(request, ctx, admitted, early, progress, startedIntent);
|
|
@@ -3747,4 +3814,4 @@ export {
|
|
|
3747
3814
|
createChatTurnRoutes,
|
|
3748
3815
|
streamChatRouteAsSandboxEvents
|
|
3749
3816
|
};
|
|
3750
|
-
//# sourceMappingURL=chunk-
|
|
3817
|
+
//# sourceMappingURL=chunk-77UNTK66.js.map
|