@tangle-network/agent-app 0.61.4 → 0.62.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{DesignCanvas-L6J72JQ2.js → DesignCanvas-IQKYAEC4.js} +3 -2
- package/dist/{DesignCanvasEditor-QFQD63MR.js → DesignCanvasEditor-O7OCZTGC.js} +5 -4
- package/dist/{TimelineEditor-7KBPJQLT.js → TimelineEditor-VB2UJ34D.js} +4 -2
- package/dist/VaultPane-AYW7CO33.js +8 -0
- package/dist/agent-profiles/d1.js +1 -1
- package/dist/agent-profiles/index.d.ts +0 -82
- package/dist/agent-profiles/index.js +1 -21
- package/dist/agent-profiles/self-management.d.ts +0 -55
- package/dist/alerting/index.d.ts +1 -1
- package/dist/alerting/index.js +1 -3
- package/dist/alerting/slack.d.ts +6 -79
- package/dist/app-auth/drizzle.d.ts +17 -271
- package/dist/app-auth/drizzle.js +6 -2
- package/dist/app-auth/drizzle.js.map +1 -1
- package/dist/app-auth/index.js +3 -3
- package/dist/assets/types.d.ts +2 -1
- package/dist/assistant/AssistantPanel.d.ts +1 -1
- package/dist/assistant/index.js +635 -392
- package/dist/assistant/index.js.map +1 -1
- package/dist/billing/identity-locks.d.ts +5 -0
- package/dist/billing/index.js +1 -1
- package/dist/brand-extraction/index.js.map +1 -1
- package/dist/brand-extraction/types.d.ts +2 -1
- package/dist/chat-react/index.js +6 -5
- package/dist/chat-routes/attachment-write-safety.d.ts +4 -0
- package/dist/chat-routes/draft-persistence.d.ts +2 -14
- package/dist/chat-routes/index.d.ts +1 -0
- package/dist/chat-routes/index.js +853 -773
- package/dist/chat-routes/index.js.map +1 -1
- package/dist/chat-routes/sandbox-model-source.d.ts +20 -0
- package/dist/chat-routes/stored-attachments.d.ts +18 -0
- package/dist/chat-routes/turn-admission.d.ts +1 -1
- package/dist/chat-routes/turn-execution.d.ts +2 -2
- package/dist/chat-routes/turn-route-contracts.d.ts +3 -29
- package/dist/chat-store/core.d.ts +19 -1
- package/dist/chat-store/index.js.map +1 -1
- package/dist/chat-store/store.d.ts +3 -29
- package/dist/{chunk-ATQQCG4I.js → chunk-2VJLLRVV.js} +2 -2
- package/dist/{chunk-R4X5474U.js → chunk-52SDU43B.js} +12 -27
- package/dist/chunk-52SDU43B.js.map +1 -0
- package/dist/{chunk-MDRH3SDA.js → chunk-54YIL7WM.js} +3 -3
- package/dist/{chunk-6QJ3GB3A.js → chunk-5GSZGTPJ.js} +2 -2
- package/dist/{chunk-5IM2CLYX.js → chunk-6JEDSKFH.js} +232 -218
- package/dist/chunk-6JEDSKFH.js.map +1 -0
- package/dist/{chunk-6PRQ45QE.js → chunk-6OG4244Y.js} +1 -1
- package/dist/chunk-6OG4244Y.js.map +1 -0
- package/dist/{chunk-ZDPUC6CM.js → chunk-6ZVSMA4T.js} +3 -3
- package/dist/{chunk-TXUAGSFR.js → chunk-76MPX7JQ.js} +47 -1
- package/dist/chunk-76MPX7JQ.js.map +1 -0
- package/dist/{chunk-BOQ4HVVH.js → chunk-7ISHKBAD.js} +61 -52
- package/dist/chunk-7ISHKBAD.js.map +1 -0
- package/dist/chunk-7PULOECS.js +22 -0
- package/dist/chunk-7PULOECS.js.map +1 -0
- package/dist/{chunk-KJODHPWM.js → chunk-CPVGRQ43.js} +511 -339
- package/dist/chunk-CPVGRQ43.js.map +1 -0
- package/dist/{chunk-LO6VQ35O.js → chunk-CQZVJVME.js} +2 -2
- package/dist/{chunk-Y43PXON2.js → chunk-EFQUAWWF.js} +577 -546
- package/dist/chunk-EFQUAWWF.js.map +1 -0
- package/dist/{chunk-FPNZMIAW.js → chunk-ELX5YITO.js} +1 -1
- package/dist/chunk-ELX5YITO.js.map +1 -0
- package/dist/chunk-FGAROAUF.js +19 -0
- package/dist/chunk-FGAROAUF.js.map +1 -0
- package/dist/{chunk-HN6MZ2XL.js → chunk-FTNGGR37.js} +641 -466
- package/dist/chunk-FTNGGR37.js.map +1 -0
- package/dist/{chunk-G4F2NWZH.js → chunk-FZ3ZFVAC.js} +4 -4
- package/dist/{chunk-UCCSJX64.js → chunk-GIEUZBQQ.js} +1112 -644
- package/dist/chunk-GIEUZBQQ.js.map +1 -0
- package/dist/{chunk-MEKOWH5E.js → chunk-IVBREL7A.js} +785 -526
- package/dist/chunk-IVBREL7A.js.map +1 -0
- package/dist/{chunk-WSEXFTN4.js → chunk-JBXNU5WD.js} +26 -26
- package/dist/chunk-JBXNU5WD.js.map +1 -0
- package/dist/{chunk-F2VX6TTJ.js → chunk-JRCZHDRG.js} +2 -41
- package/dist/chunk-JRCZHDRG.js.map +1 -0
- package/dist/{chunk-LXYQOOXU.js → chunk-JRW6LCIA.js} +552 -302
- package/dist/chunk-JRW6LCIA.js.map +1 -0
- package/dist/{chunk-TVPH5T4F.js → chunk-KS4I7KVC.js} +16 -22
- package/dist/{chunk-TVPH5T4F.js.map → chunk-KS4I7KVC.js.map} +1 -1
- package/dist/{chunk-J2BBYVGF.js → chunk-MRZP65DZ.js} +1 -117
- package/dist/chunk-MRZP65DZ.js.map +1 -0
- package/dist/{chunk-4ZSLX3LY.js → chunk-OODZCBSM.js} +1 -1
- package/dist/chunk-OODZCBSM.js.map +1 -0
- package/dist/chunk-P4RRC4AO.js +47 -0
- package/dist/chunk-P4RRC4AO.js.map +1 -0
- package/dist/{chunk-ORQD4AUO.js → chunk-PCAY23GS.js} +1 -6
- package/dist/chunk-PCAY23GS.js.map +1 -0
- package/dist/{chunk-NMZUW6VJ.js → chunk-PEJ5OP5B.js} +3 -3
- package/dist/chunk-PEJ5OP5B.js.map +1 -0
- package/dist/{chunk-NHYRIM4I.js → chunk-PIPNDZ6H.js} +304 -194
- package/dist/chunk-PIPNDZ6H.js.map +1 -0
- package/dist/{chunk-L7LQRJGD.js → chunk-TCJ4TMOW.js} +37 -33
- package/dist/{chunk-L7LQRJGD.js.map → chunk-TCJ4TMOW.js.map} +1 -1
- package/dist/chunk-UHSUY6H6.js +79 -0
- package/dist/chunk-UHSUY6H6.js.map +1 -0
- package/dist/{chunk-PDD5A43Y.js → chunk-UTL6TEN6.js} +3 -3
- package/dist/{chunk-VJBFCNAU.js → chunk-UW2DKTEM.js} +123 -130
- package/dist/chunk-UW2DKTEM.js.map +1 -0
- package/dist/{chunk-S27DKRI6.js → chunk-VDYEVCQQ.js} +2 -2
- package/dist/{chunk-UFQM46NV.js → chunk-VK3F4N4U.js} +2 -2
- package/dist/{chunk-6ZIC6OG5.js → chunk-VYBTQK25.js} +126 -122
- package/dist/chunk-VYBTQK25.js.map +1 -0
- package/dist/{chunk-RL3Z747H.js → chunk-W2BV2XOC.js} +1491 -700
- package/dist/chunk-W2BV2XOC.js.map +1 -0
- package/dist/{chunk-FUZETA6Z.js → chunk-X6SZGQTK.js} +2 -2
- package/dist/{chunk-CD2LQXY6.js → chunk-XOHE6SKU.js} +14 -16
- package/dist/{chunk-CD2LQXY6.js.map → chunk-XOHE6SKU.js.map} +1 -1
- package/dist/{chunk-ZBB5KQ6B.js → chunk-Y3GO6SG3.js} +73 -66
- package/dist/chunk-Y3GO6SG3.js.map +1 -0
- package/dist/chunk-ZAMHLXNS.js +56 -0
- package/dist/chunk-ZAMHLXNS.js.map +1 -0
- package/dist/{chunk-RHFN7TUV.js → chunk-ZDHCV462.js} +47 -44
- package/dist/{chunk-RHFN7TUV.js.map → chunk-ZDHCV462.js.map} +1 -1
- package/dist/chunk-ZOS3D26Z.js +72 -0
- package/dist/chunk-ZOS3D26Z.js.map +1 -0
- package/dist/design-canvas/index.js +32 -32
- package/dist/design-canvas/index.js.map +1 -1
- package/dist/design-canvas-react/components/DesignCanvas.d.ts +1 -1
- package/dist/design-canvas-react/components/Workspace.d.ts +1 -1
- package/dist/design-canvas-react/components/glyphs.d.ts +31 -40
- package/dist/design-canvas-react/components/transform-math.d.ts +0 -8
- package/dist/design-canvas-react/components/use-workspace-keyboard.d.ts +2 -2
- package/dist/design-canvas-react/contracts.d.ts +2 -2
- package/dist/design-canvas-react/engine.js +1 -1
- package/dist/design-canvas-react/index.js +9 -7
- package/dist/design-canvas-react/index.js.map +1 -1
- package/dist/design-canvas-react/lazy.js +1 -1
- package/dist/eval/calibration.d.ts +1 -38
- package/dist/eval/index.d.ts +2 -4
- package/dist/eval/index.js +0 -84
- package/dist/eval/index.js.map +1 -1
- package/dist/harness/index.d.ts +1 -1
- package/dist/harness/index.js +1 -1
- package/dist/harness/session-selection.d.ts +2 -2
- package/dist/hub-approvals/index.js +1 -1
- package/dist/hub-approvals/result.d.ts +1 -1
- package/dist/hub-approvals-react/index.js +4 -4
- package/dist/hub-approvals-react/index.js.map +1 -1
- package/dist/hub-approvals-react/parts.d.ts +1 -1
- package/dist/launch-invariants/cli.js +4 -4
- package/dist/launch-invariants/index.js +3 -3
- package/dist/launch-invariants/testing.js +4 -4
- package/dist/legibility/cli.js +1 -1
- package/dist/legibility/index.js +1 -1
- package/dist/{mention-editor-B23OMFT2.js → mention-editor-4BKBMWIK.js} +83 -57
- package/dist/mention-editor-4BKBMWIK.js.map +1 -0
- package/dist/missions/index.js +144 -137
- package/dist/missions/index.js.map +1 -1
- package/dist/operator/index.js +1 -1
- package/dist/peer-floors/check.js +1 -1
- package/dist/peer-floors/cli.js +6 -14
- package/dist/peer-floors/cli.js.map +1 -1
- package/dist/peer-floors/source-entries.d.ts +6 -0
- package/dist/platform/index.js +10 -10
- package/dist/preset-cloudflare/index.js +1 -1
- package/dist/profile/index.d.ts +0 -1
- package/dist/profile/index.js +1 -422
- package/dist/profile/index.js.map +1 -1
- package/dist/profile-editor/server.d.ts +0 -1
- package/dist/profile-editor/server.js +0 -1
- package/dist/profile-editor/server.js.map +1 -1
- package/dist/public-consultation/index.d.ts +2 -2
- package/dist/public-consultation/index.js +6 -6
- package/dist/record/drizzle.js +156 -148
- package/dist/record/drizzle.js.map +1 -1
- package/dist/runtime/index.js +4 -4
- package/dist/runtime/router-turn.js +3 -3
- package/dist/sandbox/index.js +5 -5
- package/dist/sequences/drizzle.js +5 -15
- package/dist/sequences/drizzle.js.map +1 -1
- package/dist/sequences/index.d.ts +1 -0
- package/dist/sequences/index.js +18 -14
- package/dist/sequences/index.js.map +1 -1
- package/dist/sequences/row-mapping.d.ts +7 -0
- package/dist/sequences-react/components/TimelineEditor.d.ts +0 -40
- package/dist/sequences-react/components/TimelineHistory.d.ts +31 -0
- package/dist/sequences-react/components/glyphs.d.ts +12 -21
- package/dist/sequences-react/components/index.d.ts +0 -2
- package/dist/sequences-react/components/interaction-math.d.ts +3 -3
- package/dist/sequences-react/contracts.d.ts +1 -1
- package/dist/sequences-react/engine/snap.d.ts +2 -2
- package/dist/sequences-react/index.js +4 -4
- package/dist/sequences-react/index.js.map +1 -1
- package/dist/settings-react/index.js +13 -30
- package/dist/settings-react/index.js.map +1 -1
- package/dist/settings-react/timezone-picker.d.ts +0 -6
- package/dist/signoff/browser-budget.d.ts +6 -1
- package/dist/signoff/cli.d.ts +0 -7
- package/dist/signoff/cli.js +1 -2
- package/dist/signoff/cli.js.map +1 -1
- package/dist/signoff/index.js +1 -1
- package/dist/skills/index.d.ts +1 -1
- package/dist/skills/index.js +5 -25
- package/dist/skills/marketplace.d.ts +1 -75
- package/dist/skills-placement/box.js +3 -4
- package/dist/skills-placement/box.js.map +1 -1
- package/dist/skills-placement/index.js +3 -4
- package/dist/spend/cli-args.d.ts +1 -1
- package/dist/spend/cli.js +3 -3
- package/dist/spend/cli.js.map +1 -1
- package/dist/spend/index.js +1 -1
- package/dist/stories/design-canvas/fixtures.d.ts +1 -14
- package/dist/stream/index.js +17 -17
- package/dist/studio/generation.d.ts +1 -1
- package/dist/studio/index.js +1 -1
- package/dist/studio-react/index.js +353 -184
- package/dist/studio-react/index.js.map +1 -1
- package/dist/studio-react/studio-composer.d.ts +1 -5
- package/dist/studio-react/studio-history-screen.d.ts +1 -1
- package/dist/teams/invitations-api.d.ts +3 -3
- package/dist/teams/invitations-api.js +74 -72
- package/dist/teams/invitations-api.js.map +1 -1
- package/dist/teams/member-sync.d.ts +2 -0
- package/dist/teams/members-api.d.ts +2 -2
- package/dist/teams/members-api.js +1 -1
- package/dist/teams-react/index.js +28 -45
- package/dist/teams-react/index.js.map +1 -1
- package/dist/tools/index.js +1 -1
- package/dist/tools/mcp-arguments.d.ts +1 -1
- package/dist/trace/index.d.ts +1 -18
- package/dist/trace/index.js +1 -36
- package/dist/trace/index.js.map +1 -1
- package/dist/trace/mission-flow.d.ts +1 -59
- package/dist/turn-health/index.d.ts +1 -1
- package/dist/turn-health/index.js +2 -2
- package/dist/turn-health/sink.d.ts +2 -1
- package/dist/turn-trace/index.js +2 -4
- package/dist/turn-trace/recorder.d.ts +0 -2
- package/dist/vault/VaultPane.d.ts +0 -20
- package/dist/vault/VaultPaneOperations.d.ts +318 -0
- package/dist/vault/index.js +2 -1
- package/dist/vault/index.js.map +1 -1
- package/dist/vault/lazy.js +1 -1
- package/dist/web-react/agent-profile-editor.d.ts +1 -2
- package/dist/web-react/agent-profile-editor.js +6 -5
- package/dist/web-react/chat-composer.d.ts +1 -1
- package/dist/web-react/durable-plan-flow.d.ts +6 -1
- package/dist/web-react/editor-glyphs.d.ts +12 -0
- package/dist/web-react/focus-navigation.d.ts +12 -0
- package/dist/web-react/history-controls.d.ts +37 -0
- package/dist/web-react/index.js +36 -34
- package/dist/web-react/interaction-question-card.d.ts +1 -1
- package/dist/web-react/record-grid-writes.d.ts +31 -0
- package/dist/web-react/record-grid.d.ts +1 -48
- package/dist/web-react/session-history-panel.d.ts +1 -9
- package/dist/web-react/use-document-dark.d.ts +1 -0
- package/dist/workspace-export/engine.d.ts +1 -6
- package/dist/workspace-export/index.js +198 -185
- package/dist/workspace-export/index.js.map +1 -1
- package/dist/workspace-export/routes.d.ts +1 -1
- package/dist/workspace-react/index.js +9 -8
- package/dist/workspace-react/index.js.map +1 -1
- package/package.json +1 -11
- package/dist/VaultPane-RXL3WLA2.js +0 -7
- package/dist/agent-profiles/reaper.d.ts +0 -31
- package/dist/agent-profiles/reaper.js +0 -29
- package/dist/agent-profiles/reaper.js.map +0 -1
- package/dist/chunk-4ZSLX3LY.js.map +0 -1
- package/dist/chunk-5IM2CLYX.js.map +0 -1
- package/dist/chunk-6OSJNJA2.js +0 -251
- package/dist/chunk-6OSJNJA2.js.map +0 -1
- package/dist/chunk-6PRQ45QE.js.map +0 -1
- package/dist/chunk-6ZIC6OG5.js.map +0 -1
- package/dist/chunk-BDEBQ4WO.js +0 -38
- package/dist/chunk-BDEBQ4WO.js.map +0 -1
- package/dist/chunk-BOQ4HVVH.js.map +0 -1
- package/dist/chunk-F2VX6TTJ.js.map +0 -1
- package/dist/chunk-FPNZMIAW.js.map +0 -1
- package/dist/chunk-HN6MZ2XL.js.map +0 -1
- package/dist/chunk-J2BBYVGF.js.map +0 -1
- package/dist/chunk-KJODHPWM.js.map +0 -1
- package/dist/chunk-LTWJYNEC.js +0 -100
- package/dist/chunk-LTWJYNEC.js.map +0 -1
- package/dist/chunk-LXYQOOXU.js.map +0 -1
- package/dist/chunk-MEKOWH5E.js.map +0 -1
- package/dist/chunk-NHYRIM4I.js.map +0 -1
- package/dist/chunk-NMZUW6VJ.js.map +0 -1
- package/dist/chunk-ORQD4AUO.js.map +0 -1
- package/dist/chunk-R4X5474U.js.map +0 -1
- package/dist/chunk-RL3Z747H.js.map +0 -1
- package/dist/chunk-TXUAGSFR.js.map +0 -1
- package/dist/chunk-UCCSJX64.js.map +0 -1
- package/dist/chunk-VJBFCNAU.js.map +0 -1
- package/dist/chunk-WSEXFTN4.js.map +0 -1
- package/dist/chunk-Y43PXON2.js.map +0 -1
- package/dist/chunk-ZBB5KQ6B.js.map +0 -1
- package/dist/eval/parity.d.ts +0 -74
- package/dist/mention-editor-B23OMFT2.js.map +0 -1
- package/dist/profile/humanizer-skill.d.ts +0 -23
- package/dist/profile/humanizer-skill.vendored.d.ts +0 -4
- package/dist/sequences-react/components/BrandMark.d.ts +0 -10
- package/dist/skills/marketplace-drizzle.d.ts +0 -121
- package/dist/skills/marketplace-drizzle.js +0 -34
- package/dist/skills/marketplace-drizzle.js.map +0 -1
- package/dist/teams-react/components/BrandMark.d.ts +0 -10
- /package/dist/{DesignCanvas-L6J72JQ2.js.map → DesignCanvas-IQKYAEC4.js.map} +0 -0
- /package/dist/{DesignCanvasEditor-QFQD63MR.js.map → DesignCanvasEditor-O7OCZTGC.js.map} +0 -0
- /package/dist/{TimelineEditor-7KBPJQLT.js.map → TimelineEditor-VB2UJ34D.js.map} +0 -0
- /package/dist/{VaultPane-RXL3WLA2.js.map → VaultPane-AYW7CO33.js.map} +0 -0
- /package/dist/{chunk-ATQQCG4I.js.map → chunk-2VJLLRVV.js.map} +0 -0
- /package/dist/{chunk-MDRH3SDA.js.map → chunk-54YIL7WM.js.map} +0 -0
- /package/dist/{chunk-6QJ3GB3A.js.map → chunk-5GSZGTPJ.js.map} +0 -0
- /package/dist/{chunk-ZDPUC6CM.js.map → chunk-6ZVSMA4T.js.map} +0 -0
- /package/dist/{chunk-LO6VQ35O.js.map → chunk-CQZVJVME.js.map} +0 -0
- /package/dist/{chunk-G4F2NWZH.js.map → chunk-FZ3ZFVAC.js.map} +0 -0
- /package/dist/{chunk-PDD5A43Y.js.map → chunk-UTL6TEN6.js.map} +0 -0
- /package/dist/{chunk-S27DKRI6.js.map → chunk-VDYEVCQQ.js.map} +0 -0
- /package/dist/{chunk-UFQM46NV.js.map → chunk-VK3F4N4U.js.map} +0 -0
- /package/dist/{chunk-FUZETA6Z.js.map → chunk-X6SZGQTK.js.map} +0 -0
|
@@ -10,6 +10,20 @@ import {
|
|
|
10
10
|
import {
|
|
11
11
|
noteMeasuredTurnFacts
|
|
12
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";
|
|
13
27
|
import {
|
|
14
28
|
mentionInputToPart,
|
|
15
29
|
toChatMessageParts
|
|
@@ -27,20 +41,6 @@ import {
|
|
|
27
41
|
parseFileMentions,
|
|
28
42
|
turnResponseEventFor
|
|
29
43
|
} from "./chunk-G7M2C72Q.js";
|
|
30
|
-
import {
|
|
31
|
-
normalizeClientTurnId,
|
|
32
|
-
resolveChatTurn
|
|
33
|
-
} from "./chunk-YG66IHDU.js";
|
|
34
|
-
import {
|
|
35
|
-
coalesceDeltas,
|
|
36
|
-
createBufferedTurnTap,
|
|
37
|
-
replayTurnEvents,
|
|
38
|
-
stampReplaySeq
|
|
39
|
-
} from "./chunk-ISP6ZH3G.js";
|
|
40
|
-
import {
|
|
41
|
-
currentD1QueryCounter,
|
|
42
|
-
withD1QueryCount
|
|
43
|
-
} from "./chunk-MM6RLOXF.js";
|
|
44
44
|
import {
|
|
45
45
|
checkModelPolicy
|
|
46
46
|
} from "./chunk-XELWWXJJ.js";
|
|
@@ -49,7 +49,7 @@ import {
|
|
|
49
49
|
createTurnTrace,
|
|
50
50
|
formatServerTiming,
|
|
51
51
|
otlpTraceId
|
|
52
|
-
} from "./chunk-
|
|
52
|
+
} from "./chunk-PCAY23GS.js";
|
|
53
53
|
|
|
54
54
|
// src/chat-routes/draft-persistence.ts
|
|
55
55
|
var CONTENT_EVENT_TYPES = /* @__PURE__ */ new Set([
|
|
@@ -641,7 +641,10 @@ function settledTurnTiming(turnStreamId, at, trace) {
|
|
|
641
641
|
});
|
|
642
642
|
return { ...measured, trace };
|
|
643
643
|
}
|
|
644
|
-
function createAdmittedTurnRunner({
|
|
644
|
+
function createAdmittedTurnRunner(dependencies) {
|
|
645
|
+
return runAdmittedTurn.bind(null, dependencies);
|
|
646
|
+
}
|
|
647
|
+
async function runAdmittedTurn({
|
|
645
648
|
options,
|
|
646
649
|
draftStore,
|
|
647
650
|
draftTuning,
|
|
@@ -650,579 +653,607 @@ function createAdmittedTurnRunner({
|
|
|
650
653
|
traceSink,
|
|
651
654
|
startIntentLine,
|
|
652
655
|
log
|
|
653
|
-
}) {
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
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 });
|
|
670
701
|
}
|
|
671
|
-
const
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
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;
|
|
683
715
|
}
|
|
684
|
-
|
|
685
|
-
const
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
hasRunningTurn = (await trace.measure("admission.running_probe", "admission", () => options.turnStore.listRunning?.(payload.threadId), void 0, historySpan) ?? []).length > 0;
|
|
695
|
-
} catch (err) {
|
|
696
|
-
log("[chat-routes] listRunning probe failed; treating the thread as idle", {
|
|
697
|
-
threadId: payload.threadId,
|
|
698
|
-
error: err instanceof Error ? err.message : String(err)
|
|
699
|
-
});
|
|
700
|
-
}
|
|
701
|
-
if (hasRunningTurn) chatTurn = ifRunning;
|
|
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;
|
|
702
726
|
}
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
sessionId: payload.threadId,
|
|
714
|
-
turnIndex: chatTurn.turnIndex
|
|
715
|
-
});
|
|
716
|
-
const executionId = payload.retryAttemptId ? `${turnExecutionId}:retry:${payload.retryAttemptId}` : turnExecutionId;
|
|
717
|
-
if (executionId.length > 256) return errorResponse(new ChatTurnInputError("Retry execution id exceeds 256 bytes"));
|
|
718
|
-
trace.bind(executionId);
|
|
719
|
-
const turnStreamId = crypto.randomUUID();
|
|
720
|
-
const prompt = fileParts.length === 0 ? content : content ? [{ type: "text", text: content }, ...fileParts] : [...fileParts];
|
|
721
|
-
let draft;
|
|
722
|
-
let tap;
|
|
723
|
-
let completionHandoff = false;
|
|
724
|
-
let completionHandoffPromise;
|
|
725
|
-
const handoffCompletion = () => {
|
|
726
|
-
if (!completionHandoffPromise) {
|
|
727
|
-
completionHandoffPromise = (async () => {
|
|
728
|
-
completionHandoff = true;
|
|
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) {
|
|
729
737
|
try {
|
|
730
|
-
await
|
|
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
|
+
});
|
|
731
747
|
} catch (err) {
|
|
732
|
-
log("[chat-routes]
|
|
748
|
+
log("[chat-routes] lifecycle.onTurnStart failed", {
|
|
733
749
|
turnId: turnStreamId,
|
|
734
750
|
error: err instanceof Error ? err.message : String(err)
|
|
735
751
|
});
|
|
736
752
|
}
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
};
|
|
742
|
-
let produceArgs = {
|
|
743
|
-
request,
|
|
744
|
-
body: payload,
|
|
745
|
-
identity,
|
|
746
|
-
context,
|
|
747
|
-
prompt,
|
|
748
|
-
executionId,
|
|
749
|
-
turnStreamId,
|
|
750
|
-
priorMessages: chatTurn.priorMessages,
|
|
751
|
-
...chatTurn.reusedUserMessageId ? { userMessageId: chatTurn.reusedUserMessageId, userMessageInserted: false } : {},
|
|
752
|
-
handoffCompletion,
|
|
753
|
-
trace,
|
|
754
|
-
...intentLine ? { intentLine } : {},
|
|
755
|
-
...admitted.prefetched ? { prefetched: admitted.prefetched } : {},
|
|
756
|
-
...ctx?.executionLimits ? { executionLimits: ctx.executionLimits } : {}
|
|
757
|
-
};
|
|
758
|
-
const dispatched = dispatchFirstFor(options, produceArgs, trace);
|
|
759
|
-
if (dispatched) early.add(dispatched.refuse);
|
|
760
|
-
let lockAcquired = false;
|
|
761
|
-
let lockHandle;
|
|
762
|
-
let lockReleased = false;
|
|
763
|
-
const releaseLock = async () => {
|
|
764
|
-
if (completionHandoff || !lockAcquired || lockReleased) return;
|
|
765
|
-
lockReleased = true;
|
|
766
|
-
try {
|
|
767
|
-
await options.turnLock.release(lockHandle);
|
|
768
|
-
} catch (err) {
|
|
769
|
-
log("[chat-routes] turnLock.release failed", {
|
|
770
|
-
turnId: turnStreamId,
|
|
771
|
-
error: err instanceof Error ? err.message : String(err)
|
|
772
|
-
});
|
|
753
|
+
}
|
|
754
|
+
await fireTerminalLifecycle(false, void 0);
|
|
755
|
+
exportTraceInBackground();
|
|
756
|
+
return gate.response;
|
|
773
757
|
}
|
|
774
|
-
};
|
|
775
|
-
const turnLock = options.turnLock;
|
|
776
|
-
if (turnLock) {
|
|
777
|
-
const acquired = await trace.measure("admission.lock", "admission", () => turnLock.acquire(produceArgs));
|
|
778
|
-
if (!acquired.acquired) return acquired.response;
|
|
779
|
-
lockAcquired = true;
|
|
780
|
-
lockHandle = acquired.handle;
|
|
781
758
|
}
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
...handedOff ? { handedOff: true } : {},
|
|
812
|
-
...gatedTurn ? { gated: true } : {},
|
|
813
|
-
...snapshot.summary
|
|
814
|
-
}));
|
|
815
|
-
const sink = traceSink;
|
|
816
|
-
if (sink) {
|
|
817
|
-
pendingTraceExport = () => sink(snapshot, {
|
|
818
|
-
"tangle.sessionId": identity.sessionId,
|
|
819
|
-
"tangle.userId": identity.userId,
|
|
820
|
-
"tangle.tenantId": identity.tenantId,
|
|
821
|
-
"tangle.turn.failed": failed,
|
|
822
|
-
...gatedTurn ? { "tangle.turn.gated": true } : {},
|
|
823
|
-
...handedOff ? { "tangle.turn.handed_off": true } : {},
|
|
824
|
-
...failureCode ? { "tangle.turn.failure_code": failureCode } : {},
|
|
825
|
-
...traceOptions?.labels?.({ identity, executionId, context }) ?? {}
|
|
826
|
-
});
|
|
827
|
-
}
|
|
828
|
-
return snapshot;
|
|
829
|
-
};
|
|
830
|
-
const fireTerminalLifecycle = async (failed, terminalError) => {
|
|
831
|
-
if (lifecycleSettled) return;
|
|
832
|
-
lifecycleSettled = true;
|
|
833
|
-
const snapshot = endTrace(failed, terminalError);
|
|
834
|
-
const lifecycle = options.lifecycle;
|
|
835
|
-
const durationMs = Date.now() - turnStartedAtMs;
|
|
836
|
-
const timing = settledTurnTiming(turnStreamId, {
|
|
837
|
-
requestAt: trace.startedAt,
|
|
838
|
-
startedAt: turnStartedAtMs,
|
|
839
|
-
firstVisibleAt: firstVisibleAtMs,
|
|
840
|
-
firstTextAt: firstTextAtMs,
|
|
841
|
-
model: producer?.model,
|
|
842
|
-
failure: failed ? { data: lastFailureData, error: terminalError } : void 0
|
|
843
|
-
}, snapshot.summary);
|
|
844
|
-
try {
|
|
845
|
-
if (!lifecycle) {
|
|
846
|
-
} else if (failed) {
|
|
847
|
-
await lifecycle.onTurnError?.({
|
|
848
|
-
identity,
|
|
849
|
-
executionId,
|
|
850
|
-
turnStreamId,
|
|
851
|
-
context,
|
|
852
|
-
durationMs,
|
|
853
|
-
...timing,
|
|
854
|
-
error: terminalError ?? lastFailureData ?? new Error("chat turn failed")
|
|
855
|
-
});
|
|
856
|
-
} else {
|
|
857
|
-
const failoverInfo = producer?.modelFailover?.();
|
|
858
|
-
const attribution = producer?.modelAttribution?.();
|
|
859
|
-
const completed = {
|
|
860
|
-
identity,
|
|
861
|
-
executionId,
|
|
862
|
-
turnStreamId,
|
|
863
|
-
context,
|
|
864
|
-
durationMs,
|
|
865
|
-
...timing,
|
|
866
|
-
finalText: producer?.finalText() ?? "",
|
|
867
|
-
usage: producer?.usage?.() ?? {},
|
|
868
|
-
assistantMessageId: assistantRowId(),
|
|
869
|
-
...gatedTurn ? { gated: true } : {},
|
|
870
|
-
...producer?.model ? { model: producer.model } : {},
|
|
871
|
-
...modelAttributionFields(attribution),
|
|
872
|
-
...failoverInfo ? { modelFailover: failoverInfo } : {}
|
|
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 } : {}
|
|
873
788
|
};
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
}
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
};
|
|
884
|
-
const exportTrace = async () => {
|
|
885
|
-
const send = pendingTraceExport;
|
|
886
|
-
pendingTraceExport = void 0;
|
|
887
|
-
if (!send) return;
|
|
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) {
|
|
888
798
|
try {
|
|
889
|
-
await
|
|
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
|
+
});
|
|
890
808
|
} catch (err) {
|
|
891
|
-
log("[chat-routes]
|
|
809
|
+
log("[chat-routes] lifecycle.onTurnStart failed", {
|
|
892
810
|
turnId: turnStreamId,
|
|
893
811
|
error: err instanceof Error ? err.message : String(err)
|
|
894
812
|
});
|
|
895
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) });
|
|
896
861
|
};
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
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;
|
|
902
890
|
try {
|
|
903
|
-
await
|
|
891
|
+
hasRunningTurn = (await trace.measure("admission.running_probe", "admission", () => options.turnStore.listRunning?.(payload.threadId), void 0, historySpan) ?? []).length > 0;
|
|
904
892
|
} catch (err) {
|
|
905
|
-
log("[chat-routes]
|
|
906
|
-
|
|
893
|
+
log("[chat-routes] listRunning probe failed; treating the thread as idle", {
|
|
894
|
+
threadId: payload.threadId,
|
|
907
895
|
error: err instanceof Error ? err.message : String(err)
|
|
908
896
|
});
|
|
909
897
|
}
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
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 }) ?? {}
|
|
915
949
|
});
|
|
916
|
-
}
|
|
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);
|
|
917
967
|
try {
|
|
918
|
-
if (
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
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);
|
|
938
999
|
}
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
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);
|
|
956
1056
|
}
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
if (options.lifecycle?.onTurnStart) {
|
|
960
|
-
try {
|
|
961
|
-
await options.lifecycle.onTurnStart({
|
|
962
|
-
identity,
|
|
963
|
-
executionId,
|
|
964
|
-
turnStreamId,
|
|
965
|
-
context,
|
|
966
|
-
startedAt: turnStartedAtMs,
|
|
967
|
-
requestReceivedAt: trace.startedAt,
|
|
968
|
-
admissionMs: turnStartedAtMs - trace.startedAt
|
|
969
|
-
});
|
|
970
|
-
} catch (err) {
|
|
971
|
-
log("[chat-routes] lifecycle.onTurnStart failed", {
|
|
972
|
-
turnId: turnStreamId,
|
|
973
|
-
error: err instanceof Error ? err.message : String(err)
|
|
974
|
-
});
|
|
975
|
-
}
|
|
1057
|
+
if (options.onRawEvent) {
|
|
1058
|
+
source = tapRawEvents(source, (event) => options.onRawEvent(event, context), log);
|
|
976
1059
|
}
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
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);
|
|
980
1071
|
}
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
const patch = await trace.measure("admission.before_turn", "admission", (span) => beforeTurn(produceArgs, { span }));
|
|
985
|
-
if (patch && Object.keys(patch).length && dispatched) throw new Error("beforeTurn cannot patch a dispatchFirst turn: its prompt is already sent");
|
|
986
|
-
if (patch) produceArgs = { ...produceArgs, ...patch };
|
|
987
|
-
}
|
|
988
|
-
const streamSetup = trace.start("admission.stream_setup", "admission");
|
|
989
|
-
tap = createBufferedTurnTap({
|
|
990
|
-
store: options.turnStore,
|
|
991
|
-
turnId: turnStreamId,
|
|
992
|
-
scopeId: payload.threadId,
|
|
993
|
-
coalesce: options.coalesceTurnEvents ?? coalesceDeltas
|
|
994
|
-
});
|
|
995
|
-
const turnMarker = { type: "turn", turnId: turnStreamId };
|
|
996
|
-
await tap.onEvent(turnMarker);
|
|
997
|
-
if (completionHandoff) await tap.detach();
|
|
998
|
-
if (draftTuning) {
|
|
999
|
-
draft = createAssistantDraftWriter({
|
|
1000
|
-
...draftTuning,
|
|
1001
|
-
store: draftStore,
|
|
1002
|
-
threadId: payload.threadId,
|
|
1003
|
-
messageId: options.draftMessageId ? options.draftMessageId({ identity, executionId, threadId: payload.threadId }) : assistantRowIdForTurn(executionId),
|
|
1004
|
-
snapshot: () => {
|
|
1005
|
-
if (!producer) return null;
|
|
1006
|
-
return {
|
|
1007
|
-
content: producer.finalText(),
|
|
1008
|
-
...producer.draftParts ? { parts: producer.draftParts() } : {},
|
|
1009
|
-
...producer.usage ? { usage: producer.usage() } : {},
|
|
1010
|
-
...producer.model ? { model: producer.model } : {}
|
|
1011
|
-
};
|
|
1012
|
-
},
|
|
1013
|
-
...options.transformFinalText ? { transformText: options.transformFinalText } : {},
|
|
1014
|
-
log
|
|
1015
|
-
});
|
|
1016
|
-
if (completionHandoff) await draft.close();
|
|
1017
|
-
}
|
|
1018
|
-
turnStartedAtMs = Date.now();
|
|
1019
|
-
turnStarted = true;
|
|
1020
|
-
if (options.lifecycle?.onTurnStart) {
|
|
1021
|
-
try {
|
|
1022
|
-
await options.lifecycle.onTurnStart({
|
|
1023
|
-
identity,
|
|
1024
|
-
executionId,
|
|
1025
|
-
turnStreamId,
|
|
1026
|
-
context,
|
|
1027
|
-
startedAt: turnStartedAtMs,
|
|
1028
|
-
requestReceivedAt: trace.startedAt,
|
|
1029
|
-
admissionMs: turnStartedAtMs - trace.startedAt
|
|
1030
|
-
});
|
|
1031
|
-
} catch (err) {
|
|
1032
|
-
log("[chat-routes] lifecycle.onTurnStart failed", {
|
|
1033
|
-
turnId: turnStreamId,
|
|
1034
|
-
error: err instanceof Error ? err.message : String(err)
|
|
1035
|
-
});
|
|
1072
|
+
if (state.firstTextAtMs === void 0 && isVisibleText(event)) {
|
|
1073
|
+
state.firstTextAtMs = Date.now();
|
|
1074
|
+
trace.markFirstText(state.firstTextAtMs);
|
|
1036
1075
|
}
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
identity,
|
|
1041
|
-
waitUntil: ctx?.waitUntil,
|
|
1042
|
-
log,
|
|
1043
|
-
hooks: {
|
|
1044
|
-
// The engine wants a synchronous producer; box resolution is async —
|
|
1045
|
-
// defer it into the generator's first pull.
|
|
1046
|
-
produce: () => ({
|
|
1047
|
-
stream: (async function* () {
|
|
1048
|
-
producer = await (dispatched ? dispatched.take(produceArgs) : trace.measure("admission.produce", "admission", () => options.produce(produceArgs)));
|
|
1049
|
-
let source = producer.stream;
|
|
1050
|
-
if (ctx?.cancelOnDisconnect) {
|
|
1051
|
-
source = abortOnSignal(source, request.signal);
|
|
1052
|
-
}
|
|
1053
|
-
if (options.onRawEvent) {
|
|
1054
|
-
source = tapRawEvents(source, (event) => options.onRawEvent(event, context), log);
|
|
1055
|
-
}
|
|
1056
|
-
if (options.heartbeat) {
|
|
1057
|
-
source = withStreamHeartbeat(source, options.heartbeat.intervalMs, options.heartbeat.event);
|
|
1058
|
-
}
|
|
1059
|
-
for await (const event of source) yield event;
|
|
1060
|
-
})(),
|
|
1061
|
-
finalText: () => producer?.finalText() ?? ""
|
|
1062
|
-
}),
|
|
1063
|
-
onEvent: async (event) => {
|
|
1064
|
-
if (firstVisibleAtMs === void 0 && isVisibleOutput(event)) {
|
|
1065
|
-
firstVisibleAtMs = Date.now();
|
|
1066
|
-
trace.markFirstVisible(firstVisibleAtMs);
|
|
1067
|
-
}
|
|
1068
|
-
if (firstTextAtMs === void 0 && isVisibleText(event)) {
|
|
1069
|
-
firstTextAtMs = Date.now();
|
|
1070
|
-
trace.markFirstText(firstTextAtMs);
|
|
1071
|
-
}
|
|
1072
|
-
if (event.type === "session.run.failed" || event.type === "error") {
|
|
1073
|
-
runFailed = true;
|
|
1074
|
-
lastFailureData = event.data;
|
|
1075
|
-
}
|
|
1076
|
-
await tap.onEvent(event);
|
|
1077
|
-
if (!completionHandoff) draft?.notify(event);
|
|
1078
|
-
if (options.onEvent) await options.onEvent(event, context);
|
|
1079
|
-
},
|
|
1080
|
-
...options.transformFinalText ? { transformFinalText: options.transformFinalText } : {},
|
|
1081
|
-
persistAssistantMessage: ({ finalText }) => trace.measure("persist.assistant", "persist", async () => {
|
|
1082
|
-
if (completionHandoff) return;
|
|
1083
|
-
if (ctx?.cancelOnDisconnect && request.signal.aborted) {
|
|
1084
|
-
await draft?.discard();
|
|
1085
|
-
assistantMessageId = null;
|
|
1086
|
-
return;
|
|
1087
|
-
}
|
|
1088
|
-
const rawParts = producer?.assistantParts ? producer.assistantParts() : void 0;
|
|
1089
|
-
const projected = rawParts && options.transformFinalText ? await Promise.all(
|
|
1090
|
-
rawParts.map(
|
|
1091
|
-
async (part) => String(part.type ?? "") === "text" ? { ...part, text: await options.transformFinalText(String(part.text ?? "")) } : part
|
|
1092
|
-
)
|
|
1093
|
-
) : rawParts;
|
|
1094
|
-
const parts = projected ? toChatMessageParts(projected) : void 0;
|
|
1095
|
-
if (!finalText.trim() && (!parts || parts.length === 0)) {
|
|
1096
|
-
await draft?.discard();
|
|
1097
|
-
assistantMessageId = draft?.rowId() ?? null;
|
|
1098
|
-
return;
|
|
1099
|
-
}
|
|
1100
|
-
const usage = producer?.usage?.() ?? {};
|
|
1101
|
-
const attribution = producer?.modelAttribution?.();
|
|
1102
|
-
const values = {
|
|
1103
|
-
content: finalText,
|
|
1104
|
-
...parts && parts.length > 0 ? { parts } : {},
|
|
1105
|
-
...producer?.model ? { model: producer.model } : {},
|
|
1106
|
-
...modelAttributionFields(attribution),
|
|
1107
|
-
...usage.inputTokens !== void 0 ? { inputTokens: usage.inputTokens } : {},
|
|
1108
|
-
...usage.outputTokens !== void 0 ? { outputTokens: usage.outputTokens } : {},
|
|
1109
|
-
...usage.reasoningTokens !== void 0 ? { reasoningTokens: usage.reasoningTokens } : {},
|
|
1110
|
-
...usage.cacheReadTokens !== void 0 ? { cacheReadTokens: usage.cacheReadTokens } : {},
|
|
1111
|
-
...usage.cacheWriteTokens !== void 0 ? { cacheWriteTokens: usage.cacheWriteTokens } : {},
|
|
1112
|
-
...usage.costUsd !== void 0 ? { costUsd: usage.costUsd } : {}
|
|
1113
|
-
};
|
|
1114
|
-
if (draft) {
|
|
1115
|
-
await draft.finalize(values);
|
|
1116
|
-
assistantMessageId = draft.rowId() ?? null;
|
|
1117
|
-
return;
|
|
1118
|
-
}
|
|
1119
|
-
assistantMessageId = rowIdOf(
|
|
1120
|
-
await options.store.appendMessage({
|
|
1121
|
-
threadId: payload.threadId,
|
|
1122
|
-
role: "assistant",
|
|
1123
|
-
...values
|
|
1124
|
-
})
|
|
1125
|
-
);
|
|
1126
|
-
}),
|
|
1127
|
-
...options.onTurnComplete ? {
|
|
1128
|
-
// Wired into the engine's completion hook, which fires only when
|
|
1129
|
-
// the stream ended without throwing. A terminal error EVENT
|
|
1130
|
-
// (not a throw) still lands here — so surface `runFailed` so the
|
|
1131
|
-
// product skips billing an errored turn instead of marking it
|
|
1132
|
-
// complete with empty text.
|
|
1133
|
-
onTurnComplete: async ({ identity: turnIdentity, finalText }) => {
|
|
1134
|
-
if (completionHandoff) return;
|
|
1135
|
-
const failoverInfo = producer?.modelFailover?.();
|
|
1136
|
-
const attribution = producer?.modelAttribution?.();
|
|
1137
|
-
return trace.measure("persist.on_turn_complete", "persist", () => options.onTurnComplete({
|
|
1138
|
-
identity: turnIdentity,
|
|
1139
|
-
finalText,
|
|
1140
|
-
context,
|
|
1141
|
-
failed: runFailed,
|
|
1142
|
-
assistantMessageId: assistantRowId(),
|
|
1143
|
-
...runFailed ? { failureReason: failureReasonOf(lastFailureData) } : {},
|
|
1144
|
-
...runFailed && failureCodeOf(lastFailureData) ? { failureCode: failureCodeOf(lastFailureData) } : {},
|
|
1145
|
-
...producer?.model ? { model: producer.model } : {},
|
|
1146
|
-
...modelAttributionFields(attribution),
|
|
1147
|
-
...failoverInfo ? { modelFailover: failoverInfo } : {}
|
|
1148
|
-
}));
|
|
1149
|
-
}
|
|
1150
|
-
} : {},
|
|
1151
|
-
...options.traceFlush ? { traceFlush: () => options.traceFlush(context) } : {}
|
|
1076
|
+
if (event.type === "session.run.failed" || event.type === "error") {
|
|
1077
|
+
state.runFailed = true;
|
|
1078
|
+
state.lastFailureData = event.data;
|
|
1152
1079
|
}
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
} catch (err) {
|
|
1165
|
-
drainError = err;
|
|
1166
|
-
log("[chat-routes] turn drain failed", {
|
|
1167
|
-
turnId: turnStreamId,
|
|
1168
|
-
error: err instanceof Error ? err.message : String(err)
|
|
1169
|
-
});
|
|
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;
|
|
1170
1091
|
}
|
|
1171
|
-
const
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
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;
|
|
1175
1102
|
return;
|
|
1176
1103
|
}
|
|
1177
|
-
const
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
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;
|
|
1182
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;
|
|
1183
1212
|
try {
|
|
1184
|
-
await tap
|
|
1213
|
+
await state.tap?.detach();
|
|
1185
1214
|
} catch (err) {
|
|
1186
|
-
log("[chat-routes] turn buffer
|
|
1215
|
+
log("[chat-routes] turn buffer detach failed", {
|
|
1187
1216
|
turnId: turnStreamId,
|
|
1188
1217
|
error: err instanceof Error ? err.message : String(err)
|
|
1189
1218
|
});
|
|
1190
1219
|
}
|
|
1191
|
-
|
|
1192
|
-
await fireTerminalLifecycle(failed, drainError);
|
|
1193
|
-
await releaseLock();
|
|
1194
|
-
await Promise.all([exportTrace(), runAfterSettle()]);
|
|
1220
|
+
await state.draft?.close();
|
|
1195
1221
|
})();
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
controller.close();
|
|
1205
|
-
}
|
|
1206
|
-
});
|
|
1207
|
-
const body = concatStreams([marker, clientBody]);
|
|
1208
|
-
return new Response(body, {
|
|
1209
|
-
headers: {
|
|
1210
|
-
"Content-Type": result.contentType,
|
|
1211
|
-
"Cache-Control": "no-cache",
|
|
1212
|
-
"X-Turn-Id": turnStreamId,
|
|
1213
|
-
"Server-Timing": admissionServerTiming(trace)
|
|
1214
|
-
}
|
|
1215
|
-
});
|
|
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);
|
|
1216
1230
|
} catch (err) {
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
}
|
|
1221
|
-
endTrace(true, err);
|
|
1222
|
-
exportTraceInBackground();
|
|
1223
|
-
throw err;
|
|
1231
|
+
log("[chat-routes] turnLock.release failed", {
|
|
1232
|
+
turnId: turnStreamId,
|
|
1233
|
+
error: err instanceof Error ? err.message : String(err)
|
|
1234
|
+
});
|
|
1224
1235
|
}
|
|
1225
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
|
+
});
|
|
1226
1257
|
}
|
|
1227
1258
|
|
|
1228
1259
|
// src/chat-routes/minted-thread.ts
|
|
@@ -1342,7 +1373,7 @@ function createChatTurnRoutes(options) {
|
|
|
1342
1373
|
});
|
|
1343
1374
|
return Promise.race([generated, deadline]).finally(() => clearTimeout(timer));
|
|
1344
1375
|
}
|
|
1345
|
-
const
|
|
1376
|
+
const runAdmittedTurn2 = createAdmittedTurnRunner({
|
|
1346
1377
|
options,
|
|
1347
1378
|
draftStore,
|
|
1348
1379
|
draftTuning,
|
|
@@ -1462,7 +1493,7 @@ function createChatTurnRoutes(options) {
|
|
|
1462
1493
|
async function runTurn(request, ctx, admitted, progress, startedIntent) {
|
|
1463
1494
|
const early = startAfterAuthorize(request, admitted);
|
|
1464
1495
|
try {
|
|
1465
|
-
return await
|
|
1496
|
+
return await runAdmittedTurn2(request, ctx, admitted, early, progress, startedIntent);
|
|
1466
1497
|
} finally {
|
|
1467
1498
|
const freed = early.release();
|
|
1468
1499
|
if (ctx?.waitUntil) ctx.waitUntil(freed);
|
|
@@ -1715,4 +1746,4 @@ export {
|
|
|
1715
1746
|
createChatTurnRoutes,
|
|
1716
1747
|
streamChatRouteAsSandboxEvents
|
|
1717
1748
|
};
|
|
1718
|
-
//# sourceMappingURL=chunk-
|
|
1749
|
+
//# sourceMappingURL=chunk-EFQUAWWF.js.map
|