@trigger.dev/sdk 4.8.5 → 4.8.7
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/commonjs/v3/ai.d.ts +34 -8
- package/dist/commonjs/v3/ai.js +304 -168
- package/dist/commonjs/v3/ai.js.map +1 -1
- package/dist/commonjs/v3/auth.d.ts +4 -4
- package/dist/commonjs/v3/auth.js +21 -75
- package/dist/commonjs/v3/auth.js.map +1 -1
- package/dist/commonjs/v3/chat-client.d.ts +1 -1
- package/dist/commonjs/v3/chat.d.ts +1 -1
- package/dist/commonjs/v3/prompt.js +6 -8
- package/dist/commonjs/v3/prompt.js.map +1 -1
- package/dist/commonjs/v3/runs.d.ts +5 -1
- package/dist/commonjs/v3/runs.js.map +1 -1
- package/dist/commonjs/v3/sessions.js +10 -7
- package/dist/commonjs/v3/sessions.js.map +1 -1
- package/dist/commonjs/v3/shared.d.ts +2 -2
- package/dist/commonjs/v3/shared.js +16 -15
- package/dist/commonjs/v3/shared.js.map +1 -1
- package/dist/commonjs/v3/steeringContext.d.ts +35 -10
- package/dist/commonjs/v3/steeringContext.js +86 -14
- package/dist/commonjs/v3/steeringContext.js.map +1 -1
- package/dist/commonjs/v3/streams.js +10 -8
- package/dist/commonjs/v3/streams.js.map +1 -1
- package/dist/commonjs/v3/tasks.d.ts +2 -2
- package/dist/commonjs/v3/tasks.js.map +1 -1
- package/dist/commonjs/v3/test/mock-chat-agent.js +24 -13
- package/dist/commonjs/v3/test/mock-chat-agent.js.map +1 -1
- package/dist/commonjs/v3/toolOutputJson.d.ts +17 -0
- package/dist/commonjs/v3/toolOutputJson.js +241 -0
- package/dist/commonjs/v3/toolOutputJson.js.map +1 -0
- package/dist/commonjs/v3/webhooks.d.ts +8 -2
- package/dist/commonjs/v3/webhooks.js +20 -11
- package/dist/commonjs/v3/webhooks.js.map +1 -1
- package/dist/commonjs/version.js +1 -1
- package/dist/esm/v3/ai.d.ts +34 -8
- package/dist/esm/v3/ai.js +306 -171
- package/dist/esm/v3/ai.js.map +1 -1
- package/dist/esm/v3/auth.d.ts +4 -4
- package/dist/esm/v3/auth.js +22 -76
- package/dist/esm/v3/auth.js.map +1 -1
- package/dist/esm/v3/chat-client.d.ts +1 -1
- package/dist/esm/v3/chat.d.ts +1 -1
- package/dist/esm/v3/prompt.js +6 -8
- package/dist/esm/v3/prompt.js.map +1 -1
- package/dist/esm/v3/runs.d.ts +5 -1
- package/dist/esm/v3/runs.js.map +1 -1
- package/dist/esm/v3/sessions.js +10 -7
- package/dist/esm/v3/sessions.js.map +1 -1
- package/dist/esm/v3/shared.d.ts +2 -2
- package/dist/esm/v3/shared.js +16 -15
- package/dist/esm/v3/shared.js.map +1 -1
- package/dist/esm/v3/steeringContext.d.ts +35 -10
- package/dist/esm/v3/steeringContext.js +85 -14
- package/dist/esm/v3/steeringContext.js.map +1 -1
- package/dist/esm/v3/streams.js +10 -8
- package/dist/esm/v3/streams.js.map +1 -1
- package/dist/esm/v3/tasks.d.ts +2 -2
- package/dist/esm/v3/tasks.js.map +1 -1
- package/dist/esm/v3/test/mock-chat-agent.js +25 -14
- package/dist/esm/v3/test/mock-chat-agent.js.map +1 -1
- package/dist/esm/v3/toolOutputJson.d.ts +17 -0
- package/dist/esm/v3/toolOutputJson.js +238 -0
- package/dist/esm/v3/toolOutputJson.js.map +1 -0
- package/dist/esm/v3/webhooks.d.ts +8 -2
- package/dist/esm/v3/webhooks.js +20 -11
- package/dist/esm/v3/webhooks.js.map +1 -1
- package/dist/esm/version.js +1 -1
- package/docs/ai/observability.mdx +25 -19
- package/docs/ai/prompts.mdx +25 -19
- package/docs/ai-chat/actions.mdx +1 -1
- package/docs/ai-chat/anatomy.mdx +1 -1
- package/docs/ai-chat/backend.mdx +24 -24
- package/docs/ai-chat/background-injection.mdx +5 -5
- package/docs/ai-chat/chat-local.mdx +2 -2
- package/docs/ai-chat/compaction.mdx +47 -18
- package/docs/ai-chat/custom-agents.mdx +4 -4
- package/docs/ai-chat/error-handling.mdx +4 -4
- package/docs/ai-chat/fast-starts.mdx +8 -7
- package/docs/ai-chat/frontend.mdx +6 -2
- package/docs/ai-chat/lifecycle-hooks.mdx +11 -11
- package/docs/ai-chat/migrating-from-a-route-handler.mdx +4 -4
- package/docs/ai-chat/migrating-from-hydrate-messages.mdx +1 -1
- package/docs/ai-chat/overview.mdx +1 -1
- package/docs/ai-chat/patterns/branching-conversations.mdx +1 -1
- package/docs/ai-chat/patterns/database-persistence.mdx +2 -2
- package/docs/ai-chat/patterns/human-in-the-loop.mdx +1 -1
- package/docs/ai-chat/patterns/native-compaction.mdx +29 -9
- package/docs/ai-chat/patterns/persistence-and-replay.mdx +1 -1
- package/docs/ai-chat/patterns/redacting-telemetry.mdx +2 -2
- package/docs/ai-chat/patterns/skills.mdx +2 -2
- package/docs/ai-chat/patterns/sub-agents.mdx +1 -1
- package/docs/ai-chat/patterns/tool-result-auditing.mdx +1 -1
- package/docs/ai-chat/patterns/trusted-edge-signals.mdx +2 -2
- package/docs/ai-chat/patterns/version-upgrades.mdx +2 -2
- package/docs/ai-chat/pending-messages.mdx +5 -5
- package/docs/ai-chat/prompt-caching.mdx +7 -7
- package/docs/ai-chat/quick-start.mdx +2 -2
- package/docs/ai-chat/testing.mdx +9 -9
- package/docs/ai-chat/tools.mdx +9 -5
- package/docs/ai-chat/transcript-storage.mdx +2 -2
- package/docs/ai-chat/types.mdx +10 -3
- package/docs/apikeys.mdx +2 -1
- package/docs/cli-login-commands.mdx +12 -0
- package/docs/concurrency.mdx +40 -0
- package/docs/database-connections.mdx +1 -1
- package/docs/deploy-environment-variables.mdx +84 -2
- package/docs/deployment/trusted-publishing.mdx +4 -0
- package/docs/errors-retrying.mdx +25 -18
- package/docs/github-actions.mdx +1 -1
- package/docs/github-integration.mdx +32 -1
- package/docs/how-it-works.mdx +4 -2
- package/docs/introduction.mdx +1 -1
- package/docs/mcp-introduction.mdx +7 -1
- package/docs/mcp-remote.mdx +90 -0
- package/docs/oidc/azure.mdx +81 -0
- package/docs/oidc/databricks.mdx +99 -0
- package/docs/oidc/doppler.mdx +60 -0
- package/docs/oidc/gcp.mdx +109 -0
- package/docs/oidc/hugging-face.mdx +90 -0
- package/docs/oidc/openrouter.mdx +91 -0
- package/docs/oidc/snowflake.mdx +91 -0
- package/docs/oidc/vault.mdx +71 -0
- package/docs/oidc.mdx +2 -0
- package/docs/runs/metadata.mdx +1 -1
- package/docs/self-hosting/docker.mdx +2 -0
- package/docs/self-hosting/env/webapp.mdx +4 -1
- package/docs/self-hosting/kubernetes.mdx +2 -0
- package/docs/tasks/schemaTask.mdx +26 -13
- package/docs/tasks/streams.mdx +2 -2
- package/docs/troubleshooting.mdx +7 -0
- package/docs/webhooks/channels.mdx +6 -6
- package/docs/webhooks/connect.mdx +9 -75
- package/docs/webhooks/custom-providers.mdx +87 -0
- package/docs/webhooks/deliveries.mdx +7 -7
- package/docs/webhooks/filters.mdx +2 -2
- package/docs/webhooks/how-it-works.mdx +58 -0
- package/docs/webhooks/human-in-the-loop.mdx +3 -3
- package/docs/webhooks/linear.mdx +5 -3
- package/docs/webhooks/overview.mdx +20 -71
- package/docs/webhooks/own-handler.mdx +62 -0
- package/docs/webhooks/pricing-and-limits.mdx +13 -0
- package/docs/webhooks/providers/attio.mdx +38 -0
- package/docs/webhooks/providers/autumn.mdx +40 -0
- package/docs/webhooks/providers/cal-com.mdx +41 -0
- package/docs/webhooks/providers/calendly.mdx +41 -0
- package/docs/webhooks/providers/clerk.mdx +40 -0
- package/docs/webhooks/providers/discord.mdx +45 -0
- package/docs/webhooks/providers/docusign.mdx +41 -0
- package/docs/webhooks/providers/elevenlabs.mdx +41 -0
- package/docs/webhooks/providers/firecrawl.mdx +54 -0
- package/docs/webhooks/providers/github.mdx +46 -0
- package/docs/webhooks/providers/gitlab.mdx +41 -0
- package/docs/webhooks/providers/granola.mdx +44 -0
- package/docs/webhooks/providers/hookdeck.mdx +98 -0
- package/docs/webhooks/providers/infisical.mdx +41 -0
- package/docs/webhooks/providers/jira.mdx +41 -0
- package/docs/webhooks/providers/langfuse.mdx +40 -0
- package/docs/webhooks/providers/linear.mdx +45 -0
- package/docs/webhooks/providers/loops.mdx +40 -0
- package/docs/webhooks/providers/metronome.mdx +42 -0
- package/docs/webhooks/providers/mux.mdx +40 -0
- package/docs/webhooks/providers/openai.mdx +40 -0
- package/docs/webhooks/providers/pagerduty.mdx +40 -0
- package/docs/webhooks/providers/plain.mdx +41 -0
- package/docs/webhooks/providers/planetscale.mdx +40 -0
- package/docs/webhooks/providers/recall-ai.mdx +54 -0
- package/docs/webhooks/providers/replicate.mdx +41 -0
- package/docs/webhooks/providers/resend.mdx +44 -0
- package/docs/webhooks/providers/retell.mdx +41 -0
- package/docs/webhooks/providers/sentry.mdx +41 -0
- package/docs/webhooks/providers/shopify.mdx +49 -0
- package/docs/webhooks/providers/slack.mdx +48 -0
- package/docs/webhooks/providers/square.mdx +40 -0
- package/docs/webhooks/providers/stripe.mdx +86 -0
- package/docs/webhooks/providers/supabase.mdx +41 -0
- package/docs/webhooks/providers/tailscale.mdx +43 -0
- package/docs/webhooks/providers/typeform.mdx +42 -0
- package/docs/webhooks/providers/vapi.mdx +47 -0
- package/docs/webhooks/providers/vercel.mdx +40 -0
- package/docs/webhooks/providers/whatsapp.mdx +45 -0
- package/docs/webhooks/providers/workos.mdx +41 -0
- package/docs/webhooks/providers/zendesk.mdx +41 -0
- package/docs/webhooks/providers.mdx +53 -0
- package/docs/webhooks/quick-start.mdx +131 -0
- package/docs/webhooks/session-routing.mdx +6 -6
- package/docs/webhooks/signing-secrets.mdx +80 -0
- package/docs/webhooks/slack.mdx +11 -2
- package/docs/webhooks/verification.mdx +139 -0
- package/docs/webhooks/waiters.mdx +2 -2
- package/docs/webhooks/webhook-tasks.mdx +46 -0
- package/package.json +4 -5
- package/docs/migrating-from-v3.mdx +0 -332
- package/docs/webhooks/public-beta.mdx +0 -70
- package/docs/webhooks/sources.mdx +0 -252
package/dist/commonjs/v3/ai.js
CHANGED
|
@@ -18,6 +18,7 @@ exports.__makeChannelStreamTapForTests = makeChannelStreamTap;
|
|
|
18
18
|
exports.__makeChannelProgressObserverForTests = makeChannelProgressObserver;
|
|
19
19
|
exports.__makeChannelLifecycleReporterForTests = makeChannelLifecycleReporter;
|
|
20
20
|
exports.__waitForChannelEgressForTests = __waitForChannelEgressForTests;
|
|
21
|
+
exports.__waitForPostTurnHooksForTests = __waitForPostTurnHooksForTests;
|
|
21
22
|
const v3_1 = require("@trigger.dev/core/v3");
|
|
22
23
|
const webhooks_js_1 = require("./webhooks.js");
|
|
23
24
|
const modelFileDownload_js_1 = require("./modelFileDownload.js");
|
|
@@ -63,6 +64,7 @@ const externalDeploymentId_js_1 = require("./externalDeploymentId.js");
|
|
|
63
64
|
const chatVersionSkew_js_1 = require("./chatVersionSkew.js");
|
|
64
65
|
const sessions_js_1 = require("./sessions.js");
|
|
65
66
|
const shared_js_1 = require("./shared.js");
|
|
67
|
+
const toolOutputJson_js_1 = require("./toolOutputJson.js");
|
|
66
68
|
const concurrency_shared_js_1 = require("./concurrency-shared.js");
|
|
67
69
|
const streams_js_1 = require("./streams.js");
|
|
68
70
|
const tracer_js_1 = require("./tracer.js");
|
|
@@ -79,12 +81,21 @@ function toModelMessages(messages, context) {
|
|
|
79
81
|
// conditional spread keeps the options object byte-identical to the no-tools
|
|
80
82
|
// path when nothing was declared.
|
|
81
83
|
const tools = locals_js_1.locals.get(chatResolvedToolsKey);
|
|
82
|
-
return (0, steeringContext_js_1.convertSteeredMessages)(messages, async (batch) => (0, ai_runtime_js_1.convertToModelMessages)(batch, {
|
|
84
|
+
return (0, steeringContext_js_1.convertSteeredMessages)(messages, async (batch) => (0, toolOutputJson_js_1.withJsonToolResults)(await (0, ai_runtime_js_1.convertToModelMessages)(batch, {
|
|
83
85
|
ignoreIncompleteToolCalls: true,
|
|
84
86
|
...(tools ? { tools } : {}),
|
|
85
|
-
}), locals_js_1.locals.get(chatSteeringInjectionsKey) ?? new Map(), context ?? locals_js_1.locals.get(chatCurrentUIMessagesKey) ?? messages, context !== undefined || locals_js_1.locals.get(chatCurrentUIMessagesKey) !== undefined);
|
|
87
|
+
})), locals_js_1.locals.get(chatSteeringInjectionsKey) ?? new Map(), context ?? locals_js_1.locals.get(chatCurrentUIMessagesKey) ?? messages, context !== undefined || locals_js_1.locals.get(chatCurrentUIMessagesKey) !== undefined);
|
|
86
88
|
}
|
|
87
89
|
const chatTurnContextKey = locals_js_1.locals.create("chat.turnContext");
|
|
90
|
+
/**
|
|
91
|
+
* The running chat turn's `chatId`, `turn` and `clientData`: the one place every callback event
|
|
92
|
+
* the SDK builds takes them from, so a callback never lacks the `clientData` its siblings get.
|
|
93
|
+
* All three are undefined outside a turn. One locals lookup; the values are not copied.
|
|
94
|
+
*/
|
|
95
|
+
function chatEventContext() {
|
|
96
|
+
const ctx = locals_js_1.locals.get(chatTurnContextKey);
|
|
97
|
+
return { chatId: ctx?.chatId, turn: ctx?.turn, clientData: ctx?.clientData };
|
|
98
|
+
}
|
|
88
99
|
/**
|
|
89
100
|
* Per-run slot holding the Session handle that backs this chat's `.in` /
|
|
90
101
|
* `.out` channels. Populated at the top of `chatAgent`'s run function from
|
|
@@ -2080,6 +2091,33 @@ function emptyUsage() {
|
|
|
2080
2091
|
outputTokenDetails: { textTokens: undefined, reasoningTokens: undefined },
|
|
2081
2092
|
};
|
|
2082
2093
|
}
|
|
2094
|
+
/**
|
|
2095
|
+
* The last step's usage of a `streamText` result: what the model's context window held on
|
|
2096
|
+
* its final call. It comes from `steps` because the result's `usage` is the last step's on
|
|
2097
|
+
* AI SDK 6 but the turn's sum on AI SDK 7. A custom stream source with no `steps` falls
|
|
2098
|
+
* back to its `usage`, which such a source supplies as the last step's. Raced against a 2s
|
|
2099
|
+
* timeout like the turn's `totalUsage`, since the promises can hang on a stop-abort.
|
|
2100
|
+
* Returns undefined when neither resolves in time.
|
|
2101
|
+
*/
|
|
2102
|
+
async function readLastStepUsage(result) {
|
|
2103
|
+
const stepsPromise = result != null ? result.steps : undefined;
|
|
2104
|
+
const hasSteps = typeof stepsPromise?.then === "function";
|
|
2105
|
+
const source = hasSteps ? stepsPromise : result != null ? result.usage : undefined;
|
|
2106
|
+
if (typeof source?.then !== "function")
|
|
2107
|
+
return undefined;
|
|
2108
|
+
try {
|
|
2109
|
+
const resolved = await Promise.race([
|
|
2110
|
+
source,
|
|
2111
|
+
new Promise((r) => setTimeout(() => r(undefined), 2_000)),
|
|
2112
|
+
]);
|
|
2113
|
+
return hasSteps
|
|
2114
|
+
? resolved?.at(-1)?.usage
|
|
2115
|
+
: resolved;
|
|
2116
|
+
}
|
|
2117
|
+
catch {
|
|
2118
|
+
return undefined;
|
|
2119
|
+
}
|
|
2120
|
+
}
|
|
2083
2121
|
function addUsage(a, b) {
|
|
2084
2122
|
const add = (x, y) => x != null || y != null ? (x ?? 0) + (y ?? 0) : undefined;
|
|
2085
2123
|
return {
|
|
@@ -2616,6 +2654,21 @@ function managedResponse() {
|
|
|
2616
2654
|
}
|
|
2617
2655
|
return response;
|
|
2618
2656
|
}
|
|
2657
|
+
/**
|
|
2658
|
+
* The `summarize` that `chatCompact` calls between tool steps for a compaction configured on
|
|
2659
|
+
* `chat.agent`, a session turn or a `MessageAccumulator`: the event is built here once, so no
|
|
2660
|
+
* surface hands the callback less than the others do.
|
|
2661
|
+
* @internal
|
|
2662
|
+
*/
|
|
2663
|
+
function innerSummarize(compaction, steps) {
|
|
2664
|
+
return (messages) => compaction.summarize({
|
|
2665
|
+
messages,
|
|
2666
|
+
usage: steps.at(-1)?.usage,
|
|
2667
|
+
source: "inner",
|
|
2668
|
+
stepNumber: steps.length - 1,
|
|
2669
|
+
...chatEventContext(),
|
|
2670
|
+
});
|
|
2671
|
+
}
|
|
2619
2672
|
/**
|
|
2620
2673
|
* Check that no tool calls are in-flight in a step's content.
|
|
2621
2674
|
* Used before compaction to avoid losing tool state mid-execution.
|
|
@@ -2687,15 +2740,9 @@ async function applyPrepareMessages(messages, reason) {
|
|
|
2687
2740
|
const hook = locals_js_1.locals.get(chatPrepareMessagesKey);
|
|
2688
2741
|
if (!hook)
|
|
2689
2742
|
return messages;
|
|
2690
|
-
const
|
|
2743
|
+
const { chatId = "", turn = 0, clientData } = chatEventContext();
|
|
2691
2744
|
const prepared = await tracer_js_1.tracer.startActiveSpan("prepareMessages()", async () => {
|
|
2692
|
-
return hook({
|
|
2693
|
-
messages,
|
|
2694
|
-
reason,
|
|
2695
|
-
chatId: turnCtx?.chatId ?? "",
|
|
2696
|
-
turn: turnCtx?.turn ?? 0,
|
|
2697
|
-
clientData: turnCtx?.clientData,
|
|
2698
|
-
});
|
|
2745
|
+
return hook({ messages, reason, chatId, turn, clientData });
|
|
2699
2746
|
}, {
|
|
2700
2747
|
attributes: {
|
|
2701
2748
|
[v3_1.SemanticInternalAttributes.STYLE_ICON]: "task-hook-onStart",
|
|
@@ -2766,9 +2813,10 @@ function getCompactionState() {
|
|
|
2766
2813
|
*
|
|
2767
2814
|
* @example
|
|
2768
2815
|
* ```ts
|
|
2769
|
-
* prepareStep: async ({ messages, steps }) => {
|
|
2816
|
+
* prepareStep: async ({ messages, steps, responseMessages }) => {
|
|
2770
2817
|
* // your custom logic here...
|
|
2771
2818
|
* const result = await chat.compact(messages, steps, {
|
|
2819
|
+
* responseMessages, // needed on AI SDK 7 to rebuild the steps after a compaction
|
|
2772
2820
|
* threshold: 80_000,
|
|
2773
2821
|
* summarize: async (msgs) => generateText({ model, messages: msgs }).then(r => r.text),
|
|
2774
2822
|
* });
|
|
@@ -2792,7 +2840,10 @@ async function chatCompact(messages, steps, options) {
|
|
|
2792
2840
|
type: "rebuilt",
|
|
2793
2841
|
messages: await applyPrepareMessages([
|
|
2794
2842
|
{ role: "user", content: state.summary },
|
|
2795
|
-
...
|
|
2843
|
+
...(0, steeringContext_js_1.generationResponseMessages)({
|
|
2844
|
+
steps,
|
|
2845
|
+
responseMessages: options.responseMessages,
|
|
2846
|
+
}).slice(state.baseResponseMessageCount),
|
|
2796
2847
|
], "compaction-rebuild"),
|
|
2797
2848
|
};
|
|
2798
2849
|
}
|
|
@@ -2803,7 +2854,7 @@ async function chatCompact(messages, steps, options) {
|
|
|
2803
2854
|
const totalTokens = currentStep.usage.totalTokens;
|
|
2804
2855
|
const inputTokens = currentStep.usage.inputTokens;
|
|
2805
2856
|
const outputTokens = currentStep.usage.outputTokens;
|
|
2806
|
-
const turnCtx =
|
|
2857
|
+
const turnCtx = chatEventContext();
|
|
2807
2858
|
const stepNumber = steps.length - 1;
|
|
2808
2859
|
const shouldTrigger = options.shouldCompact
|
|
2809
2860
|
? await options.shouldCompact({
|
|
@@ -2815,9 +2866,7 @@ async function chatCompact(messages, steps, options) {
|
|
|
2815
2866
|
source: "inner",
|
|
2816
2867
|
stepNumber,
|
|
2817
2868
|
steps,
|
|
2818
|
-
|
|
2819
|
-
turn: turnCtx?.turn,
|
|
2820
|
-
clientData: turnCtx?.clientData,
|
|
2869
|
+
...turnCtx,
|
|
2821
2870
|
})
|
|
2822
2871
|
: totalTokens != null && options.threshold != null && totalTokens > options.threshold;
|
|
2823
2872
|
if (!shouldTrigger) {
|
|
@@ -2845,7 +2894,10 @@ async function chatCompact(messages, steps, options) {
|
|
|
2845
2894
|
// Store state in locals for subsequent steps
|
|
2846
2895
|
locals_js_1.locals.set(chatCompactionStateKey, {
|
|
2847
2896
|
summary,
|
|
2848
|
-
baseResponseMessageCount:
|
|
2897
|
+
baseResponseMessageCount: (0, steeringContext_js_1.generationResponseMessages)({
|
|
2898
|
+
steps,
|
|
2899
|
+
responseMessages: options.responseMessages,
|
|
2900
|
+
}).length,
|
|
2849
2901
|
baseResponseStepCount: steps.length,
|
|
2850
2902
|
});
|
|
2851
2903
|
// Set model-only override — UI messages stay intact for persistence.
|
|
@@ -2871,8 +2923,7 @@ async function chatCompact(messages, steps, options) {
|
|
|
2871
2923
|
inputTokens,
|
|
2872
2924
|
outputTokens,
|
|
2873
2925
|
stepNumber,
|
|
2874
|
-
|
|
2875
|
-
turn: turnCtx?.turn,
|
|
2926
|
+
...turnCtx,
|
|
2876
2927
|
writer: { write, merge },
|
|
2877
2928
|
});
|
|
2878
2929
|
}
|
|
@@ -2938,8 +2989,8 @@ async function chatCompact(messages, steps, options) {
|
|
|
2938
2989
|
* ```
|
|
2939
2990
|
*/
|
|
2940
2991
|
function chatCompactionStep(options) {
|
|
2941
|
-
return async ({ messages, steps }) => {
|
|
2942
|
-
const result = await chatCompact(messages, steps, options);
|
|
2992
|
+
return async ({ messages, steps, responseMessages }) => {
|
|
2993
|
+
const result = await chatCompact(messages, steps, { ...options, responseMessages });
|
|
2943
2994
|
return result.type === "skipped" ? undefined : result;
|
|
2944
2995
|
};
|
|
2945
2996
|
}
|
|
@@ -3127,6 +3178,7 @@ async function drainSteeringQueue(config, messages, steps, queueOverride) {
|
|
|
3127
3178
|
injectedModelMessages: injected,
|
|
3128
3179
|
chatId: ctx?.chatId ?? "",
|
|
3129
3180
|
turn: ctx?.turn ?? 0,
|
|
3181
|
+
clientData: ctx?.clientData,
|
|
3130
3182
|
stepNumber,
|
|
3131
3183
|
});
|
|
3132
3184
|
}
|
|
@@ -3646,25 +3698,14 @@ function toStreamTextOptions(options, retain = true) {
|
|
|
3646
3698
|
const taskCompaction = locals_js_1.locals.get(chatAgentCompactionKey);
|
|
3647
3699
|
const taskPendingMessages = locals_js_1.locals.get(chatPendingMessagesKey);
|
|
3648
3700
|
{
|
|
3649
|
-
result.prepareStep = async ({ messages, steps, }) => {
|
|
3701
|
+
result.prepareStep = async ({ messages, steps, responseMessages, }) => {
|
|
3650
3702
|
let resultMessages;
|
|
3651
3703
|
// 1. Compaction
|
|
3652
3704
|
if (taskCompaction) {
|
|
3653
3705
|
const compactResult = await chatCompact(messages, steps, {
|
|
3706
|
+
responseMessages,
|
|
3654
3707
|
shouldCompact: taskCompaction.shouldCompact,
|
|
3655
|
-
summarize: (
|
|
3656
|
-
const ctx = locals_js_1.locals.get(chatTurnContextKey);
|
|
3657
|
-
const lastStep = steps.at(-1);
|
|
3658
|
-
return taskCompaction.summarize({
|
|
3659
|
-
messages: msgs,
|
|
3660
|
-
usage: lastStep?.usage,
|
|
3661
|
-
source: "inner",
|
|
3662
|
-
stepNumber: steps.length - 1,
|
|
3663
|
-
chatId: ctx?.chatId,
|
|
3664
|
-
turn: ctx?.turn,
|
|
3665
|
-
clientData: ctx?.clientData,
|
|
3666
|
-
});
|
|
3667
|
-
},
|
|
3708
|
+
summarize: innerSummarize(taskCompaction, steps),
|
|
3668
3709
|
});
|
|
3669
3710
|
if (compactResult.type !== "skipped") {
|
|
3670
3711
|
resultMessages = compactResult.messages;
|
|
@@ -3687,13 +3728,27 @@ function toStreamTextOptions(options, retain = true) {
|
|
|
3687
3728
|
// 4. Parallel tool results in call order, the order the next turn's history
|
|
3688
3729
|
// will list them in. See `withToolResultsInCallOrder`.
|
|
3689
3730
|
const base = resultMessages ?? messages;
|
|
3690
|
-
|
|
3691
|
-
|
|
3731
|
+
// 5. Tool results as plain JSON, which AI SDK 7 before 7.0.101 checks on
|
|
3732
|
+
// every step. See `withJsonToolResults`.
|
|
3733
|
+
const prepared = await (0, toolOutputJson_js_1.withJsonToolResults)((0, toolResultOrder_js_1.withToolResultsInCallOrder)(base));
|
|
3734
|
+
return resultMessages || prepared !== base ? { messages: prepared } : undefined;
|
|
3692
3735
|
};
|
|
3693
3736
|
}
|
|
3694
3737
|
if (retain && typeof result.prepareStep === "function") {
|
|
3695
3738
|
result.prepareStep = (0, steeringContext_js_1.retainStepMessages)(result.prepareStep);
|
|
3696
3739
|
}
|
|
3740
|
+
/**
|
|
3741
|
+
* A steer that `pendingMessages.prepare` turns into a `system` message lands in
|
|
3742
|
+
* the model lane in that form, and so replays in every later turn's `messages`.
|
|
3743
|
+
* AI SDK 7 rejects a system message in `messages` unless this is set (its default
|
|
3744
|
+
* is `false`), so without it the step fails with an invalid-prompt error on the
|
|
3745
|
+
* turn that was steered and on every turn after it. Routing the note to
|
|
3746
|
+
* `instructions` for one step would not help the later turns, because the lane
|
|
3747
|
+
* is what the caller's own `streamText({ messages })` reads. A caller that wants
|
|
3748
|
+
* the strict behaviour passes `allowSystemInMessages: false` after the spread.
|
|
3749
|
+
* AI SDK 5 and 6 ignore the key and accept system messages already.
|
|
3750
|
+
*/
|
|
3751
|
+
result.allowSystemInMessages = true;
|
|
3697
3752
|
const channelDownload = locals_js_1.locals.get(chatChannelDownloadKey);
|
|
3698
3753
|
if (channelDownload) {
|
|
3699
3754
|
result.experimental_download = channelAwareDownload(channelDownload);
|
|
@@ -3747,17 +3802,35 @@ function isActionTurn(value) {
|
|
|
3747
3802
|
* tail does not match the old message's conversion, nothing is changed and
|
|
3748
3803
|
* `false` is returned so the caller can fall back to a full reconversion.
|
|
3749
3804
|
*/
|
|
3750
|
-
async function replaceModelRun(lane, oldUi, newUi, tailAfter, knownOldRun) {
|
|
3805
|
+
async function replaceModelRun(lane, oldUi, newUi, tailAfter, knownOldRun, oldIsLastUiMessage = false) {
|
|
3751
3806
|
const oldRun = knownOldRun ?? (await toModelMessages([stripProviderMetadata(oldUi)]));
|
|
3752
3807
|
const newRun = await toModelMessages([stripProviderMetadata(newUi)]);
|
|
3753
|
-
// A message that converts to nothing (a pending tool call with no output yet,
|
|
3754
|
-
// which `ignoreIncompleteToolCalls` drops) locates no run in the lane. Matching
|
|
3755
|
-
// an empty slice would splice the new run in without removing what the message
|
|
3756
|
-
// actually contributed, such as a spliced head-start partial, and the lane would
|
|
3757
|
-
// then carry the same tool call twice.
|
|
3758
|
-
if (oldRun.length === 0)
|
|
3759
|
-
return false;
|
|
3760
3808
|
const end = lane.length - tailAfter;
|
|
3809
|
+
// A message that converts to nothing (a tool call awaiting approval on AI SDK 7,
|
|
3810
|
+
// or a pending one with no output yet, which `ignoreIncompleteToolCalls` drops)
|
|
3811
|
+
// locates no run in the lane. When it is the last UI message and the lane holds
|
|
3812
|
+
// none of its tool calls, it contributed nothing, so the new run goes in at the
|
|
3813
|
+
// tail. A message that is not the last one has later turns after its place, which
|
|
3814
|
+
// the tail is not. When the lane does hold one of its calls, such as a spliced
|
|
3815
|
+
// head-start partial, matching an empty slice would leave the old form behind and
|
|
3816
|
+
// the lane would carry the same tool call twice.
|
|
3817
|
+
if (oldRun.length === 0) {
|
|
3818
|
+
if (!oldIsLastUiMessage || end < 0 || end > lane.length)
|
|
3819
|
+
return false;
|
|
3820
|
+
const oldCallIds = new Set();
|
|
3821
|
+
for (const part of oldUi.parts) {
|
|
3822
|
+
const id = part.toolCallId;
|
|
3823
|
+
if (typeof id === "string")
|
|
3824
|
+
oldCallIds.add(id);
|
|
3825
|
+
}
|
|
3826
|
+
// Tool-call, tool-result and approval parts of a lane message all carry `toolCallId`.
|
|
3827
|
+
const holdsOldCall = lane.some((message) => Array.isArray(message.content) &&
|
|
3828
|
+
message.content.some((part) => oldCallIds.has(part.toolCallId)));
|
|
3829
|
+
if (holdsOldCall)
|
|
3830
|
+
return false;
|
|
3831
|
+
lane.splice(end, 0, ...newRun);
|
|
3832
|
+
return true;
|
|
3833
|
+
}
|
|
3761
3834
|
const start = end - oldRun.length;
|
|
3762
3835
|
if (start < 0 || end > lane.length)
|
|
3763
3836
|
return false;
|
|
@@ -4051,6 +4124,8 @@ function makeChannelDownloader(connectors, handleFor) {
|
|
|
4051
4124
|
return null;
|
|
4052
4125
|
};
|
|
4053
4126
|
return (url) => {
|
|
4127
|
+
if (isInlineDataUrl(url))
|
|
4128
|
+
return Promise.resolve(null);
|
|
4054
4129
|
const key = url.href;
|
|
4055
4130
|
const cached = cache.get(key);
|
|
4056
4131
|
if (cached)
|
|
@@ -4073,17 +4148,32 @@ function makeChannelDownloader(connectors, handleFor) {
|
|
|
4073
4148
|
}
|
|
4074
4149
|
function fileUrlOf(part) {
|
|
4075
4150
|
const p = part;
|
|
4076
|
-
|
|
4077
|
-
|
|
4078
|
-
|
|
4079
|
-
|
|
4080
|
-
|
|
4081
|
-
try {
|
|
4082
|
-
return new URL(raw);
|
|
4151
|
+
let raw = p?.type === "file" ? p.data : p?.type === "image" ? p.image : undefined;
|
|
4152
|
+
// AI SDK 7 tags the data (`{ type: "url", url }`); AI SDK 6 puts a URL or string there directly.
|
|
4153
|
+
if (raw && typeof raw === "object" && !(raw instanceof URL)) {
|
|
4154
|
+
const tagged = raw;
|
|
4155
|
+
raw = tagged.type === "url" ? tagged.url : undefined;
|
|
4083
4156
|
}
|
|
4084
|
-
|
|
4157
|
+
let url;
|
|
4158
|
+
if (raw instanceof URL) {
|
|
4159
|
+
url = raw;
|
|
4160
|
+
}
|
|
4161
|
+
else if (typeof raw === "string" && /^https?:\/\//i.test(raw)) {
|
|
4162
|
+
try {
|
|
4163
|
+
url = new URL(raw);
|
|
4164
|
+
}
|
|
4165
|
+
catch {
|
|
4166
|
+
return undefined;
|
|
4167
|
+
}
|
|
4168
|
+
}
|
|
4169
|
+
else {
|
|
4085
4170
|
return undefined;
|
|
4086
4171
|
}
|
|
4172
|
+
return isInlineDataUrl(url) ? undefined : url;
|
|
4173
|
+
}
|
|
4174
|
+
/** An inline `data:` upload carries its bytes in the URL, so it is never handed to a connector, cached or logged. */
|
|
4175
|
+
function isInlineDataUrl(url) {
|
|
4176
|
+
return url.protocol === "data:";
|
|
4087
4177
|
}
|
|
4088
4178
|
/**
|
|
4089
4179
|
* Fetch the channel-owned files in the messages a turn hands to `run()`, so the model call reads
|
|
@@ -4150,7 +4240,8 @@ async function resolveChannelFiles(messages, download) {
|
|
|
4150
4240
|
/**
|
|
4151
4241
|
* The AI SDK `experimental_download` for a turn: channel-owned URLs come from the connectors, and
|
|
4152
4242
|
* every other URL goes to `fallback`, or to the AI SDK's default behaviour (left for a model that
|
|
4153
|
-
* reads the URL itself, fetched from a public host otherwise).
|
|
4243
|
+
* reads the URL itself, fetched from a public host otherwise). An inline `data:` URL is neither: it
|
|
4244
|
+
* is left to the AI SDK to decode, and reaches no connector, fallback or fetch.
|
|
4154
4245
|
*/
|
|
4155
4246
|
function channelAwareDownload(download, fallback) {
|
|
4156
4247
|
return async (requests) => {
|
|
@@ -4158,7 +4249,9 @@ function channelAwareDownload(download, fallback) {
|
|
|
4158
4249
|
const file = await download(request.url);
|
|
4159
4250
|
return file ? { data: file.data, mediaType: file.mediaType } : null;
|
|
4160
4251
|
}));
|
|
4161
|
-
|
|
4252
|
+
// An inline `data:` file is left to the AI SDK, which decodes it itself: it is no connector's
|
|
4253
|
+
// file, and `fetchPublicFile` would refuse it with the whole upload in the error message.
|
|
4254
|
+
const misses = requests.flatMap((request, i) => results[i] || request.url.protocol === "data:" ? [] : [i]);
|
|
4162
4255
|
if (misses.length === 0)
|
|
4163
4256
|
return results;
|
|
4164
4257
|
const rest = fallback
|
|
@@ -4785,28 +4878,46 @@ function makeChannelStreamTap(editor, options = {}) {
|
|
|
4785
4878
|
return new TransformStream(transformer);
|
|
4786
4879
|
}
|
|
4787
4880
|
/**
|
|
4788
|
-
* Per-chat
|
|
4789
|
-
*
|
|
4790
|
-
*
|
|
4791
|
-
*
|
|
4792
|
-
* no open latch is quiet.
|
|
4881
|
+
* Per-chat latches for work the run loop does AFTER it writes `trigger:turn-complete`. The loop
|
|
4882
|
+
* opens a latch before writing the chunk and settles it once that work has finished, so a caller
|
|
4883
|
+
* that saw the chunk can wait for it instead of guessing with timers. One latch per chat at a
|
|
4884
|
+
* time (opening an open one keeps it); a chat with no open latch is quiet.
|
|
4793
4885
|
*/
|
|
4794
|
-
|
|
4795
|
-
|
|
4796
|
-
|
|
4797
|
-
|
|
4798
|
-
|
|
4799
|
-
|
|
4800
|
-
|
|
4801
|
-
|
|
4802
|
-
|
|
4803
|
-
|
|
4804
|
-
|
|
4805
|
-
|
|
4886
|
+
function createChatLatches() {
|
|
4887
|
+
const latches = new Map();
|
|
4888
|
+
return {
|
|
4889
|
+
open(chatId) {
|
|
4890
|
+
if (latches.has(chatId))
|
|
4891
|
+
return;
|
|
4892
|
+
let settle;
|
|
4893
|
+
const promise = new Promise((resolve) => (settle = resolve));
|
|
4894
|
+
latches.set(chatId, { promise, settle });
|
|
4895
|
+
},
|
|
4896
|
+
/** `"run-ended"` marks a settle made as the run exits, so a waiter knows the run itself is over. */
|
|
4897
|
+
settle(chatId, reason = "settled") {
|
|
4898
|
+
const latch = latches.get(chatId);
|
|
4899
|
+
if (!latch)
|
|
4900
|
+
return;
|
|
4901
|
+
latches.delete(chatId);
|
|
4902
|
+
latch.settle(reason);
|
|
4903
|
+
},
|
|
4904
|
+
/** Why the latch settled; `undefined` when nothing is open. */
|
|
4905
|
+
async wait(chatId) {
|
|
4906
|
+
return latches.get(chatId)?.promise;
|
|
4907
|
+
},
|
|
4908
|
+
};
|
|
4806
4909
|
}
|
|
4910
|
+
/** The channel egress after the chunk: the final `send()` and the done/error reactions. */
|
|
4911
|
+
const channelEgressLatches = createChatLatches();
|
|
4912
|
+
/** The post-turn hooks after the chunk: `onTurnComplete` and the turn-complete transcript save. */
|
|
4913
|
+
const postTurnHookLatches = createChatLatches();
|
|
4807
4914
|
/** Resolves once the post-turn channel egress for `chatId` has finished (immediately when none is running). */
|
|
4808
4915
|
async function __waitForChannelEgressForTests(chatId) {
|
|
4809
|
-
|
|
4916
|
+
return channelEgressLatches.wait(chatId);
|
|
4917
|
+
}
|
|
4918
|
+
/** Resolves once the post-turn hooks for `chatId` have finished (immediately when none are running). */
|
|
4919
|
+
async function __waitForPostTurnHooksForTests(chatId) {
|
|
4920
|
+
return postTurnHookLatches.wait(chatId);
|
|
4810
4921
|
}
|
|
4811
4922
|
function chatCustomAgent(options) {
|
|
4812
4923
|
const { clientDataSchema, onClientDataValidationError, clientDataReportErrorAt, run: userRun, ...restOptions } = options;
|
|
@@ -6709,6 +6820,7 @@ function chatAgent(options) {
|
|
|
6709
6820
|
message: lastUIMessage,
|
|
6710
6821
|
chatId: currentWirePayload.chatId,
|
|
6711
6822
|
turn,
|
|
6823
|
+
clientData: chatEventContext().clientData,
|
|
6712
6824
|
});
|
|
6713
6825
|
}
|
|
6714
6826
|
catch {
|
|
@@ -6924,6 +7036,7 @@ function chatAgent(options) {
|
|
|
6924
7036
|
messages: cleanedUIMessages,
|
|
6925
7037
|
chatId: currentWirePayload.chatId,
|
|
6926
7038
|
turn,
|
|
7039
|
+
clientData: chatEventContext().clientData,
|
|
6927
7040
|
trigger: currentWirePayload.trigger,
|
|
6928
7041
|
});
|
|
6929
7042
|
}, {
|
|
@@ -7063,7 +7176,11 @@ function chatAgent(options) {
|
|
|
7063
7176
|
if (idx !== -1) {
|
|
7064
7177
|
const previous = accumulatedUIMessages[idx];
|
|
7065
7178
|
accumulatedUIMessages[idx] = mergeIncomingIntoHydrated(previous, incoming);
|
|
7066
|
-
replacedPairs.push({
|
|
7179
|
+
replacedPairs.push({
|
|
7180
|
+
previous,
|
|
7181
|
+
merged: accumulatedUIMessages[idx],
|
|
7182
|
+
isLast: idx === accumulatedUIMessages.length - 1,
|
|
7183
|
+
});
|
|
7067
7184
|
replaced = true;
|
|
7068
7185
|
}
|
|
7069
7186
|
else {
|
|
@@ -7073,8 +7190,8 @@ function chatAgent(options) {
|
|
|
7073
7190
|
}
|
|
7074
7191
|
if (replaced) {
|
|
7075
7192
|
let inPlace = true;
|
|
7076
|
-
for (const { previous, merged } of replacedPairs) {
|
|
7077
|
-
if (!(await replaceModelRun(accumulatedMessages, previous, merged, 0))) {
|
|
7193
|
+
for (const { previous, merged, isLast } of replacedPairs) {
|
|
7194
|
+
if (!(await replaceModelRun(accumulatedMessages, previous, merged, 0, undefined, isLast))) {
|
|
7078
7195
|
inPlace = false;
|
|
7079
7196
|
break;
|
|
7080
7197
|
}
|
|
@@ -7608,6 +7725,21 @@ function chatAgent(options) {
|
|
|
7608
7725
|
new Promise((r) => setTimeout(r, 2_000)),
|
|
7609
7726
|
]);
|
|
7610
7727
|
}
|
|
7728
|
+
/**
|
|
7729
|
+
* A stream-level failure (an `error` chunk) is the turn's finish
|
|
7730
|
+
* reason. A step rejected before the model runs (an invalid prompt,
|
|
7731
|
+
* a throwing `prepareStep`) ends the stream with one but no finish
|
|
7732
|
+
* reason of its own, or the previous step's. Tool errors do not
|
|
7733
|
+
* count: they are `tool-input-error` / `tool-output-error` parts
|
|
7734
|
+
* that go back to the model, and the turn finishes normally.
|
|
7735
|
+
*/
|
|
7736
|
+
// Looked up once per turn; the buffer is only scanned again from
|
|
7737
|
+
// `errorScanLength` on, in case a chunk lands after this point.
|
|
7738
|
+
const errorScanLength = turnBufferedChunks.length;
|
|
7739
|
+
const earlyErrorChunk = turnBufferedChunks.find(isErrorUIMessageChunk);
|
|
7740
|
+
if (earlyErrorChunk) {
|
|
7741
|
+
capturedFinishReason = "error";
|
|
7742
|
+
}
|
|
7611
7743
|
capturedResponseMessage =
|
|
7612
7744
|
(await locals_js_1.locals.get(chatManagedResponseKey)?.snapshot()) ?? capturedResponseMessage;
|
|
7613
7745
|
if (capturedResponseMessage)
|
|
@@ -7632,18 +7764,7 @@ function chatAgent(options) {
|
|
|
7632
7764
|
/* non-fatal — usage capture failed */
|
|
7633
7765
|
}
|
|
7634
7766
|
}
|
|
7635
|
-
|
|
7636
|
-
if (typeof lastStepUsagePromise?.then === "function") {
|
|
7637
|
-
try {
|
|
7638
|
-
lastStepUsage = (await Promise.race([
|
|
7639
|
-
lastStepUsagePromise,
|
|
7640
|
-
new Promise((r) => setTimeout(() => r(undefined), 2_000)),
|
|
7641
|
-
]));
|
|
7642
|
-
}
|
|
7643
|
-
catch {
|
|
7644
|
-
/* non-fatal — usage capture failed */
|
|
7645
|
-
}
|
|
7646
|
-
}
|
|
7767
|
+
lastStepUsage = await readLastStepUsage(runResult);
|
|
7647
7768
|
if (turnUsage) {
|
|
7648
7769
|
cumulativeUsage = addUsage(cumulativeUsage, turnUsage);
|
|
7649
7770
|
previousTurnUsage = turnUsage;
|
|
@@ -7834,7 +7955,8 @@ function chatAgent(options) {
|
|
|
7834
7955
|
if (capturedResponseMessage) {
|
|
7835
7956
|
responseCommitted = true;
|
|
7836
7957
|
capturedPartialResponse = capturedResponseMessage;
|
|
7837
|
-
const errorChunk =
|
|
7958
|
+
const errorChunk = (earlyErrorChunk ??
|
|
7959
|
+
turnBufferedChunks.slice(errorScanLength).find(isErrorUIMessageChunk));
|
|
7838
7960
|
channelStreamErrorText = errorChunk?.errorText;
|
|
7839
7961
|
channelTurnText = turnBufferedChunks
|
|
7840
7962
|
.map((chunk) => {
|
|
@@ -7873,9 +7995,7 @@ function chatAgent(options) {
|
|
|
7873
7995
|
usage: contextUsage,
|
|
7874
7996
|
turnUsage,
|
|
7875
7997
|
totalUsage: cumulativeUsage,
|
|
7876
|
-
|
|
7877
|
-
turn,
|
|
7878
|
-
clientData,
|
|
7998
|
+
...chatEventContext(),
|
|
7879
7999
|
source: "outer",
|
|
7880
8000
|
});
|
|
7881
8001
|
if (shouldTrigger) {
|
|
@@ -7895,9 +8015,7 @@ function chatAgent(options) {
|
|
|
7895
8015
|
messages: accumulatedMessages,
|
|
7896
8016
|
usage: turnUsage,
|
|
7897
8017
|
totalUsage: cumulativeUsage,
|
|
7898
|
-
|
|
7899
|
-
turn,
|
|
7900
|
-
clientData,
|
|
8018
|
+
...chatEventContext(),
|
|
7901
8019
|
source: "outer",
|
|
7902
8020
|
});
|
|
7903
8021
|
// Apply compactModelMessages/compactUIMessages callbacks, or defaults.
|
|
@@ -7943,8 +8061,7 @@ function chatAgent(options) {
|
|
|
7943
8061
|
inputTokens: turnUsage.inputTokens,
|
|
7944
8062
|
outputTokens: turnUsage.outputTokens,
|
|
7945
8063
|
stepNumber: -1, // outer loop, not a step
|
|
7946
|
-
|
|
7947
|
-
turn,
|
|
8064
|
+
...chatEventContext(),
|
|
7948
8065
|
writer: { write, merge },
|
|
7949
8066
|
});
|
|
7950
8067
|
}
|
|
@@ -8063,7 +8180,8 @@ function chatAgent(options) {
|
|
|
8063
8180
|
}
|
|
8064
8181
|
}
|
|
8065
8182
|
settleRecoveredTurn(currentWirePayload);
|
|
8066
|
-
|
|
8183
|
+
channelEgressLatches.open(currentWirePayload.chatId);
|
|
8184
|
+
postTurnHookLatches.open(currentWirePayload.chatId);
|
|
8067
8185
|
// Write turn-complete control chunk — closes the frontend stream.
|
|
8068
8186
|
const turnCompleteResult = await writeTurnCompleteChunk(currentWirePayload.chatId, turnAccessToken);
|
|
8069
8187
|
// Tool calls that have not had controls posted yet; a resume with the rest still
|
|
@@ -8262,7 +8380,7 @@ function chatAgent(options) {
|
|
|
8262
8380
|
});
|
|
8263
8381
|
}
|
|
8264
8382
|
}
|
|
8265
|
-
|
|
8383
|
+
channelEgressLatches.settle(currentWirePayload.chatId);
|
|
8266
8384
|
// Fire onTurnComplete — stream is closed, use for persistence.
|
|
8267
8385
|
if (onTurnComplete) {
|
|
8268
8386
|
await tracer_js_1.tracer.startActiveSpan("onTurnComplete()", async () => {
|
|
@@ -8361,6 +8479,7 @@ function chatAgent(options) {
|
|
|
8361
8479
|
});
|
|
8362
8480
|
}
|
|
8363
8481
|
}
|
|
8482
|
+
postTurnHookLatches.settle(currentWirePayload.chatId);
|
|
8364
8483
|
} // end if (!isAction)
|
|
8365
8484
|
// NOTE: We intentionally do NOT await deferred work from onTurnComplete here.
|
|
8366
8485
|
// Promises deferred in onTurnComplete (e.g. background self-review via
|
|
@@ -8474,12 +8593,13 @@ function chatAgent(options) {
|
|
|
8474
8593
|
if (turnError instanceof Error &&
|
|
8475
8594
|
turnError.name === "AbortError" &&
|
|
8476
8595
|
runSignal.aborted) {
|
|
8477
|
-
|
|
8596
|
+
channelEgressLatches.settle(currentWirePayload.chatId);
|
|
8597
|
+
postTurnHookLatches.settle(currentWirePayload.chatId);
|
|
8478
8598
|
channelLifecycle.dispose();
|
|
8479
8599
|
// Full run cancellation — exit immediately
|
|
8480
8600
|
throw turnError;
|
|
8481
8601
|
}
|
|
8482
|
-
|
|
8602
|
+
channelEgressLatches.settle(currentWirePayload.chatId);
|
|
8483
8603
|
// OOM errors must escape the turn loop so the task runtime can
|
|
8484
8604
|
// honor `retry.outOfMemory.machine` (set on chat.agent via
|
|
8485
8605
|
// `oomMachine`). Catching them here would keep the dead worker
|
|
@@ -8600,6 +8720,7 @@ function chatAgent(options) {
|
|
|
8600
8720
|
writer.write({ type: "error", errorText });
|
|
8601
8721
|
});
|
|
8602
8722
|
// Signal turn complete so the client knows this turn is done
|
|
8723
|
+
postTurnHookLatches.open(currentWirePayload.chatId);
|
|
8603
8724
|
errorTurnCompleteResult = await writeTurnCompleteChunk(currentWirePayload.chatId);
|
|
8604
8725
|
// A later action's snapshot reuses this cursor, so it has to move
|
|
8605
8726
|
// here too or that snapshot resumes from before the failed turn.
|
|
@@ -8800,6 +8921,7 @@ function chatAgent(options) {
|
|
|
8800
8921
|
});
|
|
8801
8922
|
}
|
|
8802
8923
|
}
|
|
8924
|
+
postTurnHookLatches.settle(currentWirePayload.chatId);
|
|
8803
8925
|
if (locals_js_1.locals.get(chatCloseRequestedKey)) {
|
|
8804
8926
|
await performChatClose();
|
|
8805
8927
|
return;
|
|
@@ -8855,6 +8977,12 @@ function chatAgent(options) {
|
|
|
8855
8977
|
// reaching one of the loop's close checks (a turn timeout, an OOM
|
|
8856
8978
|
// re-throw). `performChatClose` is idempotent, so the ordinary path
|
|
8857
8979
|
// having already run it costs nothing here.
|
|
8980
|
+
// Any exit that skips a turn's own settle (an OOM re-throw, a throw from the turn's
|
|
8981
|
+
// error handler) leaves its post-turn latches open, and the module-level entries would
|
|
8982
|
+
// outlive this run and block the next one for the same chat. Settling is a no-op when
|
|
8983
|
+
// nothing is open, and runs first so a throwing close cannot skip it.
|
|
8984
|
+
channelEgressLatches.settle(currentWirePayload.chatId, "run-ended");
|
|
8985
|
+
postTurnHookLatches.settle(currentWirePayload.chatId, "run-ended");
|
|
8858
8986
|
await performChatClose();
|
|
8859
8987
|
// `stopSub` is registered post-preload so the close-during-preload
|
|
8860
8988
|
// early-return path may exit before it ever attached. Guard the
|
|
@@ -9759,6 +9887,10 @@ async function flushDeferredChatCustomAgentClientDataErrors() {
|
|
|
9759
9887
|
}
|
|
9760
9888
|
}
|
|
9761
9889
|
}
|
|
9890
|
+
/** Whether a buffered chunk is a stream-level `error` chunk. */
|
|
9891
|
+
function isErrorUIMessageChunk(chunk) {
|
|
9892
|
+
return chunk.type === "error";
|
|
9893
|
+
}
|
|
9762
9894
|
/**
|
|
9763
9895
|
* Pass every chunk through untouched while recording it in `buffer`. Handles
|
|
9764
9896
|
* both the `AsyncIterable` and `ReadableStream` shapes `toUIMessageStream()`
|
|
@@ -9940,6 +10072,7 @@ class ChatMessageAccumulator {
|
|
|
9940
10072
|
const ui = replace ? [] : [...this.uiMessages];
|
|
9941
10073
|
const lane = replace ? [] : [...this.modelMessages];
|
|
9942
10074
|
let appendedModelCount = 0;
|
|
10075
|
+
let appendedUiCount = 0;
|
|
9943
10076
|
let rebuild = replace;
|
|
9944
10077
|
for (const raw of messages) {
|
|
9945
10078
|
const message = raw.role === "assistant" ? cleanupAbortedParts(raw) : raw;
|
|
@@ -9952,13 +10085,18 @@ class ChatMessageAccumulator {
|
|
|
9952
10085
|
const idx = ui.findIndex((m) => m.id === resolved.id);
|
|
9953
10086
|
const previous = ui[idx];
|
|
9954
10087
|
ui[idx] = mergeIncomingIntoHydrated(previous, resolved);
|
|
9955
|
-
|
|
10088
|
+
// The lane's tail is only this message's place when nothing came after it
|
|
10089
|
+
// before this call's own appends.
|
|
10090
|
+
const isLast = idx === ui.length - 1 - appendedUiCount;
|
|
10091
|
+
if (!rebuild &&
|
|
10092
|
+
!(await replaceModelRun(lane, previous, ui[idx], appendedModelCount, undefined, isLast))) {
|
|
9956
10093
|
rebuild = true;
|
|
9957
10094
|
}
|
|
9958
10095
|
continue;
|
|
9959
10096
|
}
|
|
9960
10097
|
ui.push(message);
|
|
9961
10098
|
if (!rebuild) {
|
|
10099
|
+
appendedUiCount++;
|
|
9962
10100
|
const converted = await toModelMessages([message]);
|
|
9963
10101
|
lane.push(...converted);
|
|
9964
10102
|
appendedModelCount += converted.length;
|
|
@@ -10131,13 +10269,14 @@ class ChatMessageAccumulator {
|
|
|
10131
10269
|
const comp = this._compaction;
|
|
10132
10270
|
const pm = this._pendingMessages;
|
|
10133
10271
|
const queue = this._steeringQueue;
|
|
10134
|
-
return (0, steeringContext_js_1.retainStepMessages)(async ({ messages, steps }) => {
|
|
10272
|
+
return (0, steeringContext_js_1.retainStepMessages)(async ({ messages, steps, responseMessages, }) => {
|
|
10135
10273
|
let resultMessages;
|
|
10136
10274
|
// 1. Compaction
|
|
10137
10275
|
if (comp) {
|
|
10138
10276
|
const result = await chatCompact(messages, steps, {
|
|
10277
|
+
responseMessages,
|
|
10139
10278
|
shouldCompact: comp.shouldCompact,
|
|
10140
|
-
summarize: (
|
|
10279
|
+
summarize: innerSummarize(comp, steps),
|
|
10141
10280
|
});
|
|
10142
10281
|
if (result.type !== "skipped") {
|
|
10143
10282
|
resultMessages = result.messages;
|
|
@@ -10156,9 +10295,11 @@ class ChatMessageAccumulator {
|
|
|
10156
10295
|
}
|
|
10157
10296
|
/**
|
|
10158
10297
|
* Run outer-loop compaction if needed. Call after adding the response
|
|
10159
|
-
* and capturing usage. Pass the LAST step's usage
|
|
10160
|
-
* the context the model held on
|
|
10161
|
-
*
|
|
10298
|
+
* and capturing usage. Pass the LAST step's usage
|
|
10299
|
+
* (`(await result.steps).at(-1)?.usage`), which is the context the model held on
|
|
10300
|
+
* its final call. `result.usage` is that on AI SDK 6 but the turn's sum on AI SDK 7,
|
|
10301
|
+
* so read `steps`. The turn's sum (`result.totalUsage` on AI SDK 6, `result.usage`
|
|
10302
|
+
* on AI SDK 7) belongs in `context.turnUsage`. Applies
|
|
10162
10303
|
* `compactModelMessages` and `compactUIMessages` callbacks if configured.
|
|
10163
10304
|
*
|
|
10164
10305
|
* @returns `true` if compaction was performed, `false` otherwise.
|
|
@@ -10166,6 +10307,13 @@ class ChatMessageAccumulator {
|
|
|
10166
10307
|
async compactIfNeeded(usage, context) {
|
|
10167
10308
|
if (!this._compaction || !usage)
|
|
10168
10309
|
return false;
|
|
10310
|
+
// An explicit context wins; inside a chat turn the turn's own fills in what it leaves out.
|
|
10311
|
+
const turnContext = chatEventContext();
|
|
10312
|
+
const eventContext = {
|
|
10313
|
+
chatId: context?.chatId ?? turnContext.chatId,
|
|
10314
|
+
turn: context?.turn ?? turnContext.turn,
|
|
10315
|
+
clientData: context?.clientData ?? turnContext.clientData,
|
|
10316
|
+
};
|
|
10169
10317
|
const shouldTrigger = await this._compaction.shouldCompact({
|
|
10170
10318
|
messages: this.modelMessages,
|
|
10171
10319
|
totalTokens: usage.totalTokens,
|
|
@@ -10174,9 +10322,7 @@ class ChatMessageAccumulator {
|
|
|
10174
10322
|
usage,
|
|
10175
10323
|
turnUsage: context?.turnUsage,
|
|
10176
10324
|
totalUsage: context?.totalUsage,
|
|
10177
|
-
|
|
10178
|
-
turn: context?.turn,
|
|
10179
|
-
clientData: context?.clientData,
|
|
10325
|
+
...eventContext,
|
|
10180
10326
|
source: "outer",
|
|
10181
10327
|
});
|
|
10182
10328
|
if (!shouldTrigger)
|
|
@@ -10185,18 +10331,16 @@ class ChatMessageAccumulator {
|
|
|
10185
10331
|
messages: this.modelMessages,
|
|
10186
10332
|
usage,
|
|
10187
10333
|
totalUsage: context?.totalUsage,
|
|
10188
|
-
|
|
10189
|
-
turn: context?.turn,
|
|
10190
|
-
clientData: context?.clientData,
|
|
10334
|
+
...eventContext,
|
|
10191
10335
|
source: "outer",
|
|
10192
10336
|
});
|
|
10193
10337
|
const compactEvent = {
|
|
10194
10338
|
summary,
|
|
10195
10339
|
uiMessages: this.uiMessages,
|
|
10196
10340
|
modelMessages: this.modelMessages,
|
|
10197
|
-
chatId:
|
|
10198
|
-
turn:
|
|
10199
|
-
clientData:
|
|
10341
|
+
chatId: eventContext.chatId ?? "",
|
|
10342
|
+
turn: eventContext.turn ?? 0,
|
|
10343
|
+
clientData: eventContext.clientData,
|
|
10200
10344
|
source: "outer",
|
|
10201
10345
|
};
|
|
10202
10346
|
this.modelMessages = this._compaction.compactModelMessages
|
|
@@ -10494,6 +10638,7 @@ function createChatSession(payload, options) {
|
|
|
10494
10638
|
message: lastUIMessage,
|
|
10495
10639
|
chatId: currentPayload.chatId,
|
|
10496
10640
|
turn,
|
|
10641
|
+
clientData: chatEventContext().clientData,
|
|
10497
10642
|
});
|
|
10498
10643
|
}
|
|
10499
10644
|
catch {
|
|
@@ -10650,18 +10795,7 @@ function createChatSession(payload, options) {
|
|
|
10650
10795
|
/* non-fatal */
|
|
10651
10796
|
}
|
|
10652
10797
|
}
|
|
10653
|
-
|
|
10654
|
-
if (typeof lastStepUsagePromise?.then === "function") {
|
|
10655
|
-
try {
|
|
10656
|
-
lastStepUsage = (await Promise.race([
|
|
10657
|
-
lastStepUsagePromise,
|
|
10658
|
-
new Promise((r) => setTimeout(() => r(undefined), 2_000)),
|
|
10659
|
-
]));
|
|
10660
|
-
}
|
|
10661
|
-
catch {
|
|
10662
|
-
/* non-fatal */
|
|
10663
|
-
}
|
|
10664
|
-
}
|
|
10798
|
+
lastStepUsage = await readLastStepUsage(source);
|
|
10665
10799
|
// Outer-loop compaction (same logic as chat.agent)
|
|
10666
10800
|
if (sessionCompaction && turnUsage && !turnObj.stopped) {
|
|
10667
10801
|
const contextUsage = lastStepUsage ?? turnUsage;
|
|
@@ -10673,9 +10807,7 @@ function createChatSession(payload, options) {
|
|
|
10673
10807
|
usage: contextUsage,
|
|
10674
10808
|
turnUsage,
|
|
10675
10809
|
totalUsage: cumulativeUsage,
|
|
10676
|
-
|
|
10677
|
-
turn,
|
|
10678
|
-
clientData: currentPayload.metadata,
|
|
10810
|
+
...chatEventContext(),
|
|
10679
10811
|
source: "outer",
|
|
10680
10812
|
});
|
|
10681
10813
|
if (shouldTrigger) {
|
|
@@ -10683,9 +10815,7 @@ function createChatSession(payload, options) {
|
|
|
10683
10815
|
messages: accumulator.modelMessages,
|
|
10684
10816
|
usage: turnUsage,
|
|
10685
10817
|
totalUsage: cumulativeUsage,
|
|
10686
|
-
|
|
10687
|
-
turn,
|
|
10688
|
-
clientData: currentPayload.metadata,
|
|
10818
|
+
...chatEventContext(),
|
|
10689
10819
|
source: "outer",
|
|
10690
10820
|
});
|
|
10691
10821
|
const compactEvent = {
|
|
@@ -10729,12 +10859,13 @@ function createChatSession(payload, options) {
|
|
|
10729
10859
|
const hasPending = !!sessionPendingMessages;
|
|
10730
10860
|
if (!hasCompaction && !hasPending)
|
|
10731
10861
|
return undefined;
|
|
10732
|
-
return (0, steeringContext_js_1.retainStepMessages)(async ({ messages: stepMsgs, steps, }) => {
|
|
10862
|
+
return (0, steeringContext_js_1.retainStepMessages)(async ({ messages: stepMsgs, steps, responseMessages, }) => {
|
|
10733
10863
|
let resultMessages;
|
|
10734
10864
|
if (sessionCompaction) {
|
|
10735
10865
|
const compactResult = await chatCompact(stepMsgs, steps, {
|
|
10866
|
+
responseMessages,
|
|
10736
10867
|
shouldCompact: sessionCompaction.shouldCompact,
|
|
10737
|
-
summarize: (
|
|
10868
|
+
summarize: innerSummarize(sessionCompaction, steps),
|
|
10738
10869
|
});
|
|
10739
10870
|
if (compactResult.type !== "skipped") {
|
|
10740
10871
|
resultMessages = compactResult.messages;
|
|
@@ -11130,7 +11261,7 @@ function overrideRequestHeaders(authHeaders) {
|
|
|
11130
11261
|
async function callSessionsCreateWithOverride(args) {
|
|
11131
11262
|
const apiClient = v3_1.apiClientManager.client;
|
|
11132
11263
|
if (!apiClient) {
|
|
11133
|
-
throw new Error("chat.createStartSessionAction: no API access token configured.
|
|
11264
|
+
throw new Error("chat.createStartSessionAction: no API access token configured. Call configure({ accessToken }) or pass apiClient: { accessToken } to createStartSessionAction before invoking the action.");
|
|
11134
11265
|
}
|
|
11135
11266
|
const authHeaders = apiClient.getAuthenticationHeaders("createSession");
|
|
11136
11267
|
const ctx = { endpoint: "sessions", chatId: args.chatId };
|
|
@@ -11154,55 +11285,60 @@ async function callSessionsCreateWithOverride(args) {
|
|
|
11154
11285
|
async function mintPublicTokenWithOverride(args) {
|
|
11155
11286
|
const apiClient = v3_1.apiClientManager.client;
|
|
11156
11287
|
if (!apiClient) {
|
|
11157
|
-
throw new Error("chat.createStartSessionAction: no API access token configured for
|
|
11288
|
+
throw new Error("chat.createStartSessionAction: no API access token configured for public-token minting. Call configure({ accessToken }) or pass apiClient: { accessToken } to createStartSessionAction.");
|
|
11158
11289
|
}
|
|
11159
11290
|
const authHeaders = apiClient.getAuthenticationHeaders("createPublicToken");
|
|
11160
|
-
const accessToken = authHeaders.Authorization.slice("Bearer ".length);
|
|
11161
11291
|
const ctx = { endpoint: "auth", chatId: args.chatId };
|
|
11162
11292
|
const scopes = [`read:sessions:${args.chatId}`, `write:sessions:${args.chatId}`];
|
|
11163
|
-
const
|
|
11164
|
-
const
|
|
11165
|
-
const url = `${resolveChatStartBaseURL("auth", args.chatId, args.baseURLOption)}${endpoint}`;
|
|
11293
|
+
const baseURL = resolveChatStartBaseURL("auth", args.chatId, args.baseURLOption);
|
|
11294
|
+
const url = `${baseURL}/api/v1/auth/public-tokens`;
|
|
11166
11295
|
const init = {
|
|
11167
11296
|
method: "POST",
|
|
11168
11297
|
headers: overrideRequestHeaders(authHeaders),
|
|
11169
|
-
|
|
11170
|
-
|
|
11171
|
-
|
|
11172
|
-
|
|
11173
|
-
|
|
11174
|
-
|
|
11175
|
-
: args.expirationTime,
|
|
11176
|
-
}),
|
|
11177
|
-
}
|
|
11178
|
-
: {}),
|
|
11298
|
+
body: JSON.stringify({
|
|
11299
|
+
scopes,
|
|
11300
|
+
expirationTime: args.expirationTime instanceof Date
|
|
11301
|
+
? Math.floor(args.expirationTime.getTime() / 1000)
|
|
11302
|
+
: args.expirationTime,
|
|
11303
|
+
}),
|
|
11179
11304
|
};
|
|
11180
11305
|
const response = args.fetchOverride
|
|
11181
11306
|
? await args.fetchOverride(url, init, ctx)
|
|
11182
11307
|
: await fetch(url, init);
|
|
11183
11308
|
if (!response.ok) {
|
|
11184
|
-
|
|
11185
|
-
|
|
11186
|
-
|
|
11309
|
+
const accessToken = authHeaders.Authorization.slice("Bearer ".length);
|
|
11310
|
+
if ((0, v3_1.canFallbackToLocalPublicToken)(accessToken, scopes, response.status)) {
|
|
11311
|
+
await response.arrayBuffer();
|
|
11312
|
+
const claimsURL = `${baseURL}/api/v1/auth/jwt/claims`;
|
|
11313
|
+
const claimsInit = {
|
|
11314
|
+
method: "POST",
|
|
11315
|
+
headers: overrideRequestHeaders(authHeaders),
|
|
11316
|
+
};
|
|
11317
|
+
const claimsResponse = args.fetchOverride
|
|
11318
|
+
? await args.fetchOverride(claimsURL, claimsInit, ctx)
|
|
11319
|
+
: await fetch(claimsURL, claimsInit);
|
|
11320
|
+
if (!claimsResponse.ok) {
|
|
11321
|
+
const text = await claimsResponse.text().catch(() => "");
|
|
11322
|
+
throw new Error(`auth.generateJWTClaims failed: ${claimsResponse.status} ${text}`);
|
|
11323
|
+
}
|
|
11324
|
+
const claims = (await claimsResponse.json());
|
|
11325
|
+
return (0, v3_1.generateJWT)({
|
|
11326
|
+
secretKey: accessToken,
|
|
11327
|
+
payload: { ...claims, scopes },
|
|
11328
|
+
expirationTime: args.expirationTime,
|
|
11329
|
+
});
|
|
11330
|
+
}
|
|
11331
|
+
if (response.status === 404) {
|
|
11332
|
+
throw new Error("The server does not support public-token minting for this credential. Upgrade your self-hosted server.");
|
|
11187
11333
|
}
|
|
11188
11334
|
const text = await response.text().catch(() => "");
|
|
11189
11335
|
throw new Error(`auth.createPublicToken failed: ${response.status} ${text}`);
|
|
11190
11336
|
}
|
|
11191
11337
|
const responseBody = (await response.json());
|
|
11192
|
-
if (
|
|
11193
|
-
|
|
11194
|
-
throw new Error("auth.createPublicToken failed: server response did not include a token");
|
|
11195
|
-
}
|
|
11196
|
-
return responseBody.token;
|
|
11338
|
+
if (typeof responseBody.token !== "string") {
|
|
11339
|
+
throw new Error("auth.createPublicToken failed: server response did not include a token");
|
|
11197
11340
|
}
|
|
11198
|
-
return
|
|
11199
|
-
secretKey: accessToken,
|
|
11200
|
-
payload: {
|
|
11201
|
-
...responseBody,
|
|
11202
|
-
scopes,
|
|
11203
|
-
},
|
|
11204
|
-
expirationTime: args.expirationTime,
|
|
11205
|
-
});
|
|
11341
|
+
return responseBody.token;
|
|
11206
11342
|
}
|
|
11207
11343
|
/**
|
|
11208
11344
|
* Creates a server-side helper that reads a conversation from a transcript
|