@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.
Files changed (193) hide show
  1. package/dist/commonjs/v3/ai.d.ts +34 -8
  2. package/dist/commonjs/v3/ai.js +304 -168
  3. package/dist/commonjs/v3/ai.js.map +1 -1
  4. package/dist/commonjs/v3/auth.d.ts +4 -4
  5. package/dist/commonjs/v3/auth.js +21 -75
  6. package/dist/commonjs/v3/auth.js.map +1 -1
  7. package/dist/commonjs/v3/chat-client.d.ts +1 -1
  8. package/dist/commonjs/v3/chat.d.ts +1 -1
  9. package/dist/commonjs/v3/prompt.js +6 -8
  10. package/dist/commonjs/v3/prompt.js.map +1 -1
  11. package/dist/commonjs/v3/runs.d.ts +5 -1
  12. package/dist/commonjs/v3/runs.js.map +1 -1
  13. package/dist/commonjs/v3/sessions.js +10 -7
  14. package/dist/commonjs/v3/sessions.js.map +1 -1
  15. package/dist/commonjs/v3/shared.d.ts +2 -2
  16. package/dist/commonjs/v3/shared.js +16 -15
  17. package/dist/commonjs/v3/shared.js.map +1 -1
  18. package/dist/commonjs/v3/steeringContext.d.ts +35 -10
  19. package/dist/commonjs/v3/steeringContext.js +86 -14
  20. package/dist/commonjs/v3/steeringContext.js.map +1 -1
  21. package/dist/commonjs/v3/streams.js +10 -8
  22. package/dist/commonjs/v3/streams.js.map +1 -1
  23. package/dist/commonjs/v3/tasks.d.ts +2 -2
  24. package/dist/commonjs/v3/tasks.js.map +1 -1
  25. package/dist/commonjs/v3/test/mock-chat-agent.js +24 -13
  26. package/dist/commonjs/v3/test/mock-chat-agent.js.map +1 -1
  27. package/dist/commonjs/v3/toolOutputJson.d.ts +17 -0
  28. package/dist/commonjs/v3/toolOutputJson.js +241 -0
  29. package/dist/commonjs/v3/toolOutputJson.js.map +1 -0
  30. package/dist/commonjs/v3/webhooks.d.ts +8 -2
  31. package/dist/commonjs/v3/webhooks.js +20 -11
  32. package/dist/commonjs/v3/webhooks.js.map +1 -1
  33. package/dist/commonjs/version.js +1 -1
  34. package/dist/esm/v3/ai.d.ts +34 -8
  35. package/dist/esm/v3/ai.js +306 -171
  36. package/dist/esm/v3/ai.js.map +1 -1
  37. package/dist/esm/v3/auth.d.ts +4 -4
  38. package/dist/esm/v3/auth.js +22 -76
  39. package/dist/esm/v3/auth.js.map +1 -1
  40. package/dist/esm/v3/chat-client.d.ts +1 -1
  41. package/dist/esm/v3/chat.d.ts +1 -1
  42. package/dist/esm/v3/prompt.js +6 -8
  43. package/dist/esm/v3/prompt.js.map +1 -1
  44. package/dist/esm/v3/runs.d.ts +5 -1
  45. package/dist/esm/v3/runs.js.map +1 -1
  46. package/dist/esm/v3/sessions.js +10 -7
  47. package/dist/esm/v3/sessions.js.map +1 -1
  48. package/dist/esm/v3/shared.d.ts +2 -2
  49. package/dist/esm/v3/shared.js +16 -15
  50. package/dist/esm/v3/shared.js.map +1 -1
  51. package/dist/esm/v3/steeringContext.d.ts +35 -10
  52. package/dist/esm/v3/steeringContext.js +85 -14
  53. package/dist/esm/v3/steeringContext.js.map +1 -1
  54. package/dist/esm/v3/streams.js +10 -8
  55. package/dist/esm/v3/streams.js.map +1 -1
  56. package/dist/esm/v3/tasks.d.ts +2 -2
  57. package/dist/esm/v3/tasks.js.map +1 -1
  58. package/dist/esm/v3/test/mock-chat-agent.js +25 -14
  59. package/dist/esm/v3/test/mock-chat-agent.js.map +1 -1
  60. package/dist/esm/v3/toolOutputJson.d.ts +17 -0
  61. package/dist/esm/v3/toolOutputJson.js +238 -0
  62. package/dist/esm/v3/toolOutputJson.js.map +1 -0
  63. package/dist/esm/v3/webhooks.d.ts +8 -2
  64. package/dist/esm/v3/webhooks.js +20 -11
  65. package/dist/esm/v3/webhooks.js.map +1 -1
  66. package/dist/esm/version.js +1 -1
  67. package/docs/ai/observability.mdx +25 -19
  68. package/docs/ai/prompts.mdx +25 -19
  69. package/docs/ai-chat/actions.mdx +1 -1
  70. package/docs/ai-chat/anatomy.mdx +1 -1
  71. package/docs/ai-chat/backend.mdx +24 -24
  72. package/docs/ai-chat/background-injection.mdx +5 -5
  73. package/docs/ai-chat/chat-local.mdx +2 -2
  74. package/docs/ai-chat/compaction.mdx +47 -18
  75. package/docs/ai-chat/custom-agents.mdx +4 -4
  76. package/docs/ai-chat/error-handling.mdx +4 -4
  77. package/docs/ai-chat/fast-starts.mdx +8 -7
  78. package/docs/ai-chat/frontend.mdx +6 -2
  79. package/docs/ai-chat/lifecycle-hooks.mdx +11 -11
  80. package/docs/ai-chat/migrating-from-a-route-handler.mdx +4 -4
  81. package/docs/ai-chat/migrating-from-hydrate-messages.mdx +1 -1
  82. package/docs/ai-chat/overview.mdx +1 -1
  83. package/docs/ai-chat/patterns/branching-conversations.mdx +1 -1
  84. package/docs/ai-chat/patterns/database-persistence.mdx +2 -2
  85. package/docs/ai-chat/patterns/human-in-the-loop.mdx +1 -1
  86. package/docs/ai-chat/patterns/native-compaction.mdx +29 -9
  87. package/docs/ai-chat/patterns/persistence-and-replay.mdx +1 -1
  88. package/docs/ai-chat/patterns/redacting-telemetry.mdx +2 -2
  89. package/docs/ai-chat/patterns/skills.mdx +2 -2
  90. package/docs/ai-chat/patterns/sub-agents.mdx +1 -1
  91. package/docs/ai-chat/patterns/tool-result-auditing.mdx +1 -1
  92. package/docs/ai-chat/patterns/trusted-edge-signals.mdx +2 -2
  93. package/docs/ai-chat/patterns/version-upgrades.mdx +2 -2
  94. package/docs/ai-chat/pending-messages.mdx +5 -5
  95. package/docs/ai-chat/prompt-caching.mdx +7 -7
  96. package/docs/ai-chat/quick-start.mdx +2 -2
  97. package/docs/ai-chat/testing.mdx +9 -9
  98. package/docs/ai-chat/tools.mdx +9 -5
  99. package/docs/ai-chat/transcript-storage.mdx +2 -2
  100. package/docs/ai-chat/types.mdx +10 -3
  101. package/docs/apikeys.mdx +2 -1
  102. package/docs/cli-login-commands.mdx +12 -0
  103. package/docs/concurrency.mdx +40 -0
  104. package/docs/database-connections.mdx +1 -1
  105. package/docs/deploy-environment-variables.mdx +84 -2
  106. package/docs/deployment/trusted-publishing.mdx +4 -0
  107. package/docs/errors-retrying.mdx +25 -18
  108. package/docs/github-actions.mdx +1 -1
  109. package/docs/github-integration.mdx +32 -1
  110. package/docs/how-it-works.mdx +4 -2
  111. package/docs/introduction.mdx +1 -1
  112. package/docs/mcp-introduction.mdx +7 -1
  113. package/docs/mcp-remote.mdx +90 -0
  114. package/docs/oidc/azure.mdx +81 -0
  115. package/docs/oidc/databricks.mdx +99 -0
  116. package/docs/oidc/doppler.mdx +60 -0
  117. package/docs/oidc/gcp.mdx +109 -0
  118. package/docs/oidc/hugging-face.mdx +90 -0
  119. package/docs/oidc/openrouter.mdx +91 -0
  120. package/docs/oidc/snowflake.mdx +91 -0
  121. package/docs/oidc/vault.mdx +71 -0
  122. package/docs/oidc.mdx +2 -0
  123. package/docs/runs/metadata.mdx +1 -1
  124. package/docs/self-hosting/docker.mdx +2 -0
  125. package/docs/self-hosting/env/webapp.mdx +4 -1
  126. package/docs/self-hosting/kubernetes.mdx +2 -0
  127. package/docs/tasks/schemaTask.mdx +26 -13
  128. package/docs/tasks/streams.mdx +2 -2
  129. package/docs/troubleshooting.mdx +7 -0
  130. package/docs/webhooks/channels.mdx +6 -6
  131. package/docs/webhooks/connect.mdx +9 -75
  132. package/docs/webhooks/custom-providers.mdx +87 -0
  133. package/docs/webhooks/deliveries.mdx +7 -7
  134. package/docs/webhooks/filters.mdx +2 -2
  135. package/docs/webhooks/how-it-works.mdx +58 -0
  136. package/docs/webhooks/human-in-the-loop.mdx +3 -3
  137. package/docs/webhooks/linear.mdx +5 -3
  138. package/docs/webhooks/overview.mdx +20 -71
  139. package/docs/webhooks/own-handler.mdx +62 -0
  140. package/docs/webhooks/pricing-and-limits.mdx +13 -0
  141. package/docs/webhooks/providers/attio.mdx +38 -0
  142. package/docs/webhooks/providers/autumn.mdx +40 -0
  143. package/docs/webhooks/providers/cal-com.mdx +41 -0
  144. package/docs/webhooks/providers/calendly.mdx +41 -0
  145. package/docs/webhooks/providers/clerk.mdx +40 -0
  146. package/docs/webhooks/providers/discord.mdx +45 -0
  147. package/docs/webhooks/providers/docusign.mdx +41 -0
  148. package/docs/webhooks/providers/elevenlabs.mdx +41 -0
  149. package/docs/webhooks/providers/firecrawl.mdx +54 -0
  150. package/docs/webhooks/providers/github.mdx +46 -0
  151. package/docs/webhooks/providers/gitlab.mdx +41 -0
  152. package/docs/webhooks/providers/granola.mdx +44 -0
  153. package/docs/webhooks/providers/hookdeck.mdx +98 -0
  154. package/docs/webhooks/providers/infisical.mdx +41 -0
  155. package/docs/webhooks/providers/jira.mdx +41 -0
  156. package/docs/webhooks/providers/langfuse.mdx +40 -0
  157. package/docs/webhooks/providers/linear.mdx +45 -0
  158. package/docs/webhooks/providers/loops.mdx +40 -0
  159. package/docs/webhooks/providers/metronome.mdx +42 -0
  160. package/docs/webhooks/providers/mux.mdx +40 -0
  161. package/docs/webhooks/providers/openai.mdx +40 -0
  162. package/docs/webhooks/providers/pagerduty.mdx +40 -0
  163. package/docs/webhooks/providers/plain.mdx +41 -0
  164. package/docs/webhooks/providers/planetscale.mdx +40 -0
  165. package/docs/webhooks/providers/recall-ai.mdx +54 -0
  166. package/docs/webhooks/providers/replicate.mdx +41 -0
  167. package/docs/webhooks/providers/resend.mdx +44 -0
  168. package/docs/webhooks/providers/retell.mdx +41 -0
  169. package/docs/webhooks/providers/sentry.mdx +41 -0
  170. package/docs/webhooks/providers/shopify.mdx +49 -0
  171. package/docs/webhooks/providers/slack.mdx +48 -0
  172. package/docs/webhooks/providers/square.mdx +40 -0
  173. package/docs/webhooks/providers/stripe.mdx +86 -0
  174. package/docs/webhooks/providers/supabase.mdx +41 -0
  175. package/docs/webhooks/providers/tailscale.mdx +43 -0
  176. package/docs/webhooks/providers/typeform.mdx +42 -0
  177. package/docs/webhooks/providers/vapi.mdx +47 -0
  178. package/docs/webhooks/providers/vercel.mdx +40 -0
  179. package/docs/webhooks/providers/whatsapp.mdx +45 -0
  180. package/docs/webhooks/providers/workos.mdx +41 -0
  181. package/docs/webhooks/providers/zendesk.mdx +41 -0
  182. package/docs/webhooks/providers.mdx +53 -0
  183. package/docs/webhooks/quick-start.mdx +131 -0
  184. package/docs/webhooks/session-routing.mdx +6 -6
  185. package/docs/webhooks/signing-secrets.mdx +80 -0
  186. package/docs/webhooks/slack.mdx +11 -2
  187. package/docs/webhooks/verification.mdx +139 -0
  188. package/docs/webhooks/waiters.mdx +2 -2
  189. package/docs/webhooks/webhook-tasks.mdx +46 -0
  190. package/package.json +4 -5
  191. package/docs/migrating-from-v3.mdx +0 -332
  192. package/docs/webhooks/public-beta.mdx +0 -70
  193. package/docs/webhooks/sources.mdx +0 -252
package/dist/esm/v3/ai.js CHANGED
@@ -1,4 +1,4 @@
1
- import { accessoryAttributes, apiClientManager, controlSubtype, generateJWT, getSchemaParseFn, headerValue, InputStreamOncePromise, isAdditionalApiKey, isRunToken, isSchemaZodEsque, logger, ManualWaitpointPromise, OutOfMemoryError, resourceCatalog, SemanticInternalAttributes, SESSION_IN_CONSUMED_ID_HEADER, SESSION_IN_EVENT_ID_HEADER, sessionStreams, SessionChannelRouter, InputStreamTimeoutError, taskContext, TRIGGER_CONTROL_SUBTYPE, SESSION_CLOSED_HEADER, SESSION_CLOSED_REASON_HEADER, tryCatch, } from "@trigger.dev/core/v3";
1
+ import { accessoryAttributes, apiClientManager, canFallbackToLocalPublicToken, generateJWT, controlSubtype, getSchemaParseFn, headerValue, InputStreamOncePromise, isSchemaZodEsque, logger, ManualWaitpointPromise, OutOfMemoryError, resourceCatalog, SemanticInternalAttributes, SESSION_IN_CONSUMED_ID_HEADER, SESSION_IN_EVENT_ID_HEADER, sessionStreams, SessionChannelRouter, InputStreamTimeoutError, taskContext, TRIGGER_CONTROL_SUBTYPE, SESSION_CLOSED_HEADER, SESSION_CLOSED_REASON_HEADER, tryCatch, } from "@trigger.dev/core/v3";
2
2
  import { chatEvent, normalizeKeyString } from "./webhooks.js";
3
3
  import { fetchPublicFile } from "./modelFileDownload.js";
4
4
  // Runtime VALUES go through the ESM/CJS shim so the CJS build can `require`
@@ -13,7 +13,7 @@ import { withToolResultsInCallOrder } from "./toolResultOrder.js";
13
13
  import { ManagedChatResponse, createOrderedChatWriter } from "./managedChatResponse.js";
14
14
  import { reduceUIMessageChunks } from "./uiMessageChunks.js";
15
15
  import { forwardSubAgentChunks } from "./subAgentStream.js";
16
- import { convertSteeredMessages, retainStepMessages, steeringMarkers, withoutInjectionMarkers, } from "./steeringContext.js";
16
+ import { convertSteeredMessages, generationResponseMessages, retainStepMessages, steeringMarkers, withoutInjectionMarkers, } from "./steeringContext.js";
17
17
  let transcriptStorageOverride;
18
18
  /**
19
19
  * Test-only override for the storage `chat.agent` persists through, so a
@@ -41,6 +41,7 @@ import { withResolvedExternalDeploymentId } from "./externalDeploymentId.js";
41
41
  import { resolvePinToFollow } from "./chatVersionSkew.js";
42
42
  import { sessions, } from "./sessions.js";
43
43
  import { createTask } from "./shared.js";
44
+ import { withJsonToolResults } from "./toolOutputJson.js";
44
45
  import { triggerConcurrencyBody } from "./concurrency-shared.js";
45
46
  import { markChatAgentRunForStreamsWarning } from "./streams.js";
46
47
  import { tracer } from "./tracer.js";
@@ -57,12 +58,21 @@ function toModelMessages(messages, context) {
57
58
  // conditional spread keeps the options object byte-identical to the no-tools
58
59
  // path when nothing was declared.
59
60
  const tools = locals.get(chatResolvedToolsKey);
60
- return convertSteeredMessages(messages, async (batch) => convertToModelMessages(batch, {
61
+ return convertSteeredMessages(messages, async (batch) => withJsonToolResults(await convertToModelMessages(batch, {
61
62
  ignoreIncompleteToolCalls: true,
62
63
  ...(tools ? { tools } : {}),
63
- }), locals.get(chatSteeringInjectionsKey) ?? new Map(), context ?? locals.get(chatCurrentUIMessagesKey) ?? messages, context !== undefined || locals.get(chatCurrentUIMessagesKey) !== undefined);
64
+ })), locals.get(chatSteeringInjectionsKey) ?? new Map(), context ?? locals.get(chatCurrentUIMessagesKey) ?? messages, context !== undefined || locals.get(chatCurrentUIMessagesKey) !== undefined);
64
65
  }
65
66
  const chatTurnContextKey = locals.create("chat.turnContext");
67
+ /**
68
+ * The running chat turn's `chatId`, `turn` and `clientData`: the one place every callback event
69
+ * the SDK builds takes them from, so a callback never lacks the `clientData` its siblings get.
70
+ * All three are undefined outside a turn. One locals lookup; the values are not copied.
71
+ */
72
+ function chatEventContext() {
73
+ const ctx = locals.get(chatTurnContextKey);
74
+ return { chatId: ctx?.chatId, turn: ctx?.turn, clientData: ctx?.clientData };
75
+ }
66
76
  /**
67
77
  * Per-run slot holding the Session handle that backs this chat's `.in` /
68
78
  * `.out` channels. Populated at the top of `chatAgent`'s run function from
@@ -2050,6 +2060,33 @@ function emptyUsage() {
2050
2060
  outputTokenDetails: { textTokens: undefined, reasoningTokens: undefined },
2051
2061
  };
2052
2062
  }
2063
+ /**
2064
+ * The last step's usage of a `streamText` result: what the model's context window held on
2065
+ * its final call. It comes from `steps` because the result's `usage` is the last step's on
2066
+ * AI SDK 6 but the turn's sum on AI SDK 7. A custom stream source with no `steps` falls
2067
+ * back to its `usage`, which such a source supplies as the last step's. Raced against a 2s
2068
+ * timeout like the turn's `totalUsage`, since the promises can hang on a stop-abort.
2069
+ * Returns undefined when neither resolves in time.
2070
+ */
2071
+ async function readLastStepUsage(result) {
2072
+ const stepsPromise = result != null ? result.steps : undefined;
2073
+ const hasSteps = typeof stepsPromise?.then === "function";
2074
+ const source = hasSteps ? stepsPromise : result != null ? result.usage : undefined;
2075
+ if (typeof source?.then !== "function")
2076
+ return undefined;
2077
+ try {
2078
+ const resolved = await Promise.race([
2079
+ source,
2080
+ new Promise((r) => setTimeout(() => r(undefined), 2_000)),
2081
+ ]);
2082
+ return hasSteps
2083
+ ? resolved?.at(-1)?.usage
2084
+ : resolved;
2085
+ }
2086
+ catch {
2087
+ return undefined;
2088
+ }
2089
+ }
2053
2090
  function addUsage(a, b) {
2054
2091
  const add = (x, y) => x != null || y != null ? (x ?? 0) + (y ?? 0) : undefined;
2055
2092
  return {
@@ -2596,6 +2633,21 @@ function managedResponse() {
2596
2633
  }
2597
2634
  return response;
2598
2635
  }
2636
+ /**
2637
+ * The `summarize` that `chatCompact` calls between tool steps for a compaction configured on
2638
+ * `chat.agent`, a session turn or a `MessageAccumulator`: the event is built here once, so no
2639
+ * surface hands the callback less than the others do.
2640
+ * @internal
2641
+ */
2642
+ function innerSummarize(compaction, steps) {
2643
+ return (messages) => compaction.summarize({
2644
+ messages,
2645
+ usage: steps.at(-1)?.usage,
2646
+ source: "inner",
2647
+ stepNumber: steps.length - 1,
2648
+ ...chatEventContext(),
2649
+ });
2650
+ }
2599
2651
  /**
2600
2652
  * Check that no tool calls are in-flight in a step's content.
2601
2653
  * Used before compaction to avoid losing tool state mid-execution.
@@ -2667,15 +2719,9 @@ async function applyPrepareMessages(messages, reason) {
2667
2719
  const hook = locals.get(chatPrepareMessagesKey);
2668
2720
  if (!hook)
2669
2721
  return messages;
2670
- const turnCtx = locals.get(chatTurnContextKey);
2722
+ const { chatId = "", turn = 0, clientData } = chatEventContext();
2671
2723
  const prepared = await tracer.startActiveSpan("prepareMessages()", async () => {
2672
- return hook({
2673
- messages,
2674
- reason,
2675
- chatId: turnCtx?.chatId ?? "",
2676
- turn: turnCtx?.turn ?? 0,
2677
- clientData: turnCtx?.clientData,
2678
- });
2724
+ return hook({ messages, reason, chatId, turn, clientData });
2679
2725
  }, {
2680
2726
  attributes: {
2681
2727
  [SemanticInternalAttributes.STYLE_ICON]: "task-hook-onStart",
@@ -2746,9 +2792,10 @@ function getCompactionState() {
2746
2792
  *
2747
2793
  * @example
2748
2794
  * ```ts
2749
- * prepareStep: async ({ messages, steps }) => {
2795
+ * prepareStep: async ({ messages, steps, responseMessages }) => {
2750
2796
  * // your custom logic here...
2751
2797
  * const result = await chat.compact(messages, steps, {
2798
+ * responseMessages, // needed on AI SDK 7 to rebuild the steps after a compaction
2752
2799
  * threshold: 80_000,
2753
2800
  * summarize: async (msgs) => generateText({ model, messages: msgs }).then(r => r.text),
2754
2801
  * });
@@ -2772,7 +2819,10 @@ async function chatCompact(messages, steps, options) {
2772
2819
  type: "rebuilt",
2773
2820
  messages: await applyPrepareMessages([
2774
2821
  { role: "user", content: state.summary },
2775
- ...currentStep.response.messages.slice(state.baseResponseMessageCount),
2822
+ ...generationResponseMessages({
2823
+ steps,
2824
+ responseMessages: options.responseMessages,
2825
+ }).slice(state.baseResponseMessageCount),
2776
2826
  ], "compaction-rebuild"),
2777
2827
  };
2778
2828
  }
@@ -2783,7 +2833,7 @@ async function chatCompact(messages, steps, options) {
2783
2833
  const totalTokens = currentStep.usage.totalTokens;
2784
2834
  const inputTokens = currentStep.usage.inputTokens;
2785
2835
  const outputTokens = currentStep.usage.outputTokens;
2786
- const turnCtx = locals.get(chatTurnContextKey);
2836
+ const turnCtx = chatEventContext();
2787
2837
  const stepNumber = steps.length - 1;
2788
2838
  const shouldTrigger = options.shouldCompact
2789
2839
  ? await options.shouldCompact({
@@ -2795,9 +2845,7 @@ async function chatCompact(messages, steps, options) {
2795
2845
  source: "inner",
2796
2846
  stepNumber,
2797
2847
  steps,
2798
- chatId: turnCtx?.chatId,
2799
- turn: turnCtx?.turn,
2800
- clientData: turnCtx?.clientData,
2848
+ ...turnCtx,
2801
2849
  })
2802
2850
  : totalTokens != null && options.threshold != null && totalTokens > options.threshold;
2803
2851
  if (!shouldTrigger) {
@@ -2825,7 +2873,10 @@ async function chatCompact(messages, steps, options) {
2825
2873
  // Store state in locals for subsequent steps
2826
2874
  locals.set(chatCompactionStateKey, {
2827
2875
  summary,
2828
- baseResponseMessageCount: currentStep.response.messages.length,
2876
+ baseResponseMessageCount: generationResponseMessages({
2877
+ steps,
2878
+ responseMessages: options.responseMessages,
2879
+ }).length,
2829
2880
  baseResponseStepCount: steps.length,
2830
2881
  });
2831
2882
  // Set model-only override — UI messages stay intact for persistence.
@@ -2851,8 +2902,7 @@ async function chatCompact(messages, steps, options) {
2851
2902
  inputTokens,
2852
2903
  outputTokens,
2853
2904
  stepNumber,
2854
- chatId: turnCtx?.chatId,
2855
- turn: turnCtx?.turn,
2905
+ ...turnCtx,
2856
2906
  writer: { write, merge },
2857
2907
  });
2858
2908
  }
@@ -2918,8 +2968,8 @@ async function chatCompact(messages, steps, options) {
2918
2968
  * ```
2919
2969
  */
2920
2970
  function chatCompactionStep(options) {
2921
- return async ({ messages, steps }) => {
2922
- const result = await chatCompact(messages, steps, options);
2971
+ return async ({ messages, steps, responseMessages }) => {
2972
+ const result = await chatCompact(messages, steps, { ...options, responseMessages });
2923
2973
  return result.type === "skipped" ? undefined : result;
2924
2974
  };
2925
2975
  }
@@ -3107,6 +3157,7 @@ async function drainSteeringQueue(config, messages, steps, queueOverride) {
3107
3157
  injectedModelMessages: injected,
3108
3158
  chatId: ctx?.chatId ?? "",
3109
3159
  turn: ctx?.turn ?? 0,
3160
+ clientData: ctx?.clientData,
3110
3161
  stepNumber,
3111
3162
  });
3112
3163
  }
@@ -3626,25 +3677,14 @@ function toStreamTextOptions(options, retain = true) {
3626
3677
  const taskCompaction = locals.get(chatAgentCompactionKey);
3627
3678
  const taskPendingMessages = locals.get(chatPendingMessagesKey);
3628
3679
  {
3629
- result.prepareStep = async ({ messages, steps, }) => {
3680
+ result.prepareStep = async ({ messages, steps, responseMessages, }) => {
3630
3681
  let resultMessages;
3631
3682
  // 1. Compaction
3632
3683
  if (taskCompaction) {
3633
3684
  const compactResult = await chatCompact(messages, steps, {
3685
+ responseMessages,
3634
3686
  shouldCompact: taskCompaction.shouldCompact,
3635
- summarize: (msgs) => {
3636
- const ctx = locals.get(chatTurnContextKey);
3637
- const lastStep = steps.at(-1);
3638
- return taskCompaction.summarize({
3639
- messages: msgs,
3640
- usage: lastStep?.usage,
3641
- source: "inner",
3642
- stepNumber: steps.length - 1,
3643
- chatId: ctx?.chatId,
3644
- turn: ctx?.turn,
3645
- clientData: ctx?.clientData,
3646
- });
3647
- },
3687
+ summarize: innerSummarize(taskCompaction, steps),
3648
3688
  });
3649
3689
  if (compactResult.type !== "skipped") {
3650
3690
  resultMessages = compactResult.messages;
@@ -3667,13 +3707,27 @@ function toStreamTextOptions(options, retain = true) {
3667
3707
  // 4. Parallel tool results in call order, the order the next turn's history
3668
3708
  // will list them in. See `withToolResultsInCallOrder`.
3669
3709
  const base = resultMessages ?? messages;
3670
- const ordered = withToolResultsInCallOrder(base);
3671
- return resultMessages || ordered !== base ? { messages: ordered } : undefined;
3710
+ // 5. Tool results as plain JSON, which AI SDK 7 before 7.0.101 checks on
3711
+ // every step. See `withJsonToolResults`.
3712
+ const prepared = await withJsonToolResults(withToolResultsInCallOrder(base));
3713
+ return resultMessages || prepared !== base ? { messages: prepared } : undefined;
3672
3714
  };
3673
3715
  }
3674
3716
  if (retain && typeof result.prepareStep === "function") {
3675
3717
  result.prepareStep = retainStepMessages(result.prepareStep);
3676
3718
  }
3719
+ /**
3720
+ * A steer that `pendingMessages.prepare` turns into a `system` message lands in
3721
+ * the model lane in that form, and so replays in every later turn's `messages`.
3722
+ * AI SDK 7 rejects a system message in `messages` unless this is set (its default
3723
+ * is `false`), so without it the step fails with an invalid-prompt error on the
3724
+ * turn that was steered and on every turn after it. Routing the note to
3725
+ * `instructions` for one step would not help the later turns, because the lane
3726
+ * is what the caller's own `streamText({ messages })` reads. A caller that wants
3727
+ * the strict behaviour passes `allowSystemInMessages: false` after the spread.
3728
+ * AI SDK 5 and 6 ignore the key and accept system messages already.
3729
+ */
3730
+ result.allowSystemInMessages = true;
3677
3731
  const channelDownload = locals.get(chatChannelDownloadKey);
3678
3732
  if (channelDownload) {
3679
3733
  result.experimental_download = channelAwareDownload(channelDownload);
@@ -3727,17 +3781,35 @@ function isActionTurn(value) {
3727
3781
  * tail does not match the old message's conversion, nothing is changed and
3728
3782
  * `false` is returned so the caller can fall back to a full reconversion.
3729
3783
  */
3730
- async function replaceModelRun(lane, oldUi, newUi, tailAfter, knownOldRun) {
3784
+ async function replaceModelRun(lane, oldUi, newUi, tailAfter, knownOldRun, oldIsLastUiMessage = false) {
3731
3785
  const oldRun = knownOldRun ?? (await toModelMessages([stripProviderMetadata(oldUi)]));
3732
3786
  const newRun = await toModelMessages([stripProviderMetadata(newUi)]);
3733
- // A message that converts to nothing (a pending tool call with no output yet,
3734
- // which `ignoreIncompleteToolCalls` drops) locates no run in the lane. Matching
3735
- // an empty slice would splice the new run in without removing what the message
3736
- // actually contributed, such as a spliced head-start partial, and the lane would
3737
- // then carry the same tool call twice.
3738
- if (oldRun.length === 0)
3739
- return false;
3740
3787
  const end = lane.length - tailAfter;
3788
+ // A message that converts to nothing (a tool call awaiting approval on AI SDK 7,
3789
+ // or a pending one with no output yet, which `ignoreIncompleteToolCalls` drops)
3790
+ // locates no run in the lane. When it is the last UI message and the lane holds
3791
+ // none of its tool calls, it contributed nothing, so the new run goes in at the
3792
+ // tail. A message that is not the last one has later turns after its place, which
3793
+ // the tail is not. When the lane does hold one of its calls, such as a spliced
3794
+ // head-start partial, matching an empty slice would leave the old form behind and
3795
+ // the lane would carry the same tool call twice.
3796
+ if (oldRun.length === 0) {
3797
+ if (!oldIsLastUiMessage || end < 0 || end > lane.length)
3798
+ return false;
3799
+ const oldCallIds = new Set();
3800
+ for (const part of oldUi.parts) {
3801
+ const id = part.toolCallId;
3802
+ if (typeof id === "string")
3803
+ oldCallIds.add(id);
3804
+ }
3805
+ // Tool-call, tool-result and approval parts of a lane message all carry `toolCallId`.
3806
+ const holdsOldCall = lane.some((message) => Array.isArray(message.content) &&
3807
+ message.content.some((part) => oldCallIds.has(part.toolCallId)));
3808
+ if (holdsOldCall)
3809
+ return false;
3810
+ lane.splice(end, 0, ...newRun);
3811
+ return true;
3812
+ }
3741
3813
  const start = end - oldRun.length;
3742
3814
  if (start < 0 || end > lane.length)
3743
3815
  return false;
@@ -4031,6 +4103,8 @@ function makeChannelDownloader(connectors, handleFor) {
4031
4103
  return null;
4032
4104
  };
4033
4105
  return (url) => {
4106
+ if (isInlineDataUrl(url))
4107
+ return Promise.resolve(null);
4034
4108
  const key = url.href;
4035
4109
  const cached = cache.get(key);
4036
4110
  if (cached)
@@ -4053,17 +4127,32 @@ function makeChannelDownloader(connectors, handleFor) {
4053
4127
  }
4054
4128
  function fileUrlOf(part) {
4055
4129
  const p = part;
4056
- const raw = p?.type === "file" ? p.data : p?.type === "image" ? p.image : undefined;
4057
- if (raw instanceof URL)
4058
- return raw;
4059
- if (typeof raw !== "string" || !/^https?:\/\//i.test(raw))
4060
- return undefined;
4061
- try {
4062
- return new URL(raw);
4130
+ let raw = p?.type === "file" ? p.data : p?.type === "image" ? p.image : undefined;
4131
+ // AI SDK 7 tags the data (`{ type: "url", url }`); AI SDK 6 puts a URL or string there directly.
4132
+ if (raw && typeof raw === "object" && !(raw instanceof URL)) {
4133
+ const tagged = raw;
4134
+ raw = tagged.type === "url" ? tagged.url : undefined;
4063
4135
  }
4064
- catch {
4136
+ let url;
4137
+ if (raw instanceof URL) {
4138
+ url = raw;
4139
+ }
4140
+ else if (typeof raw === "string" && /^https?:\/\//i.test(raw)) {
4141
+ try {
4142
+ url = new URL(raw);
4143
+ }
4144
+ catch {
4145
+ return undefined;
4146
+ }
4147
+ }
4148
+ else {
4065
4149
  return undefined;
4066
4150
  }
4151
+ return isInlineDataUrl(url) ? undefined : url;
4152
+ }
4153
+ /** An inline `data:` upload carries its bytes in the URL, so it is never handed to a connector, cached or logged. */
4154
+ function isInlineDataUrl(url) {
4155
+ return url.protocol === "data:";
4067
4156
  }
4068
4157
  /**
4069
4158
  * Fetch the channel-owned files in the messages a turn hands to `run()`, so the model call reads
@@ -4130,7 +4219,8 @@ async function resolveChannelFiles(messages, download) {
4130
4219
  /**
4131
4220
  * The AI SDK `experimental_download` for a turn: channel-owned URLs come from the connectors, and
4132
4221
  * every other URL goes to `fallback`, or to the AI SDK's default behaviour (left for a model that
4133
- * reads the URL itself, fetched from a public host otherwise).
4222
+ * reads the URL itself, fetched from a public host otherwise). An inline `data:` URL is neither: it
4223
+ * is left to the AI SDK to decode, and reaches no connector, fallback or fetch.
4134
4224
  */
4135
4225
  function channelAwareDownload(download, fallback) {
4136
4226
  return async (requests) => {
@@ -4138,7 +4228,9 @@ function channelAwareDownload(download, fallback) {
4138
4228
  const file = await download(request.url);
4139
4229
  return file ? { data: file.data, mediaType: file.mediaType } : null;
4140
4230
  }));
4141
- const misses = requests.flatMap((_, i) => (results[i] ? [] : [i]));
4231
+ // An inline `data:` file is left to the AI SDK, which decodes it itself: it is no connector's
4232
+ // file, and `fetchPublicFile` would refuse it with the whole upload in the error message.
4233
+ const misses = requests.flatMap((request, i) => results[i] || request.url.protocol === "data:" ? [] : [i]);
4142
4234
  if (misses.length === 0)
4143
4235
  return results;
4144
4236
  const rest = fallback
@@ -4766,28 +4858,46 @@ function makeChannelStreamTap(editor, options = {}) {
4766
4858
  }
4767
4859
  export { makeChannelStreamEditor as __makeChannelStreamEditorForTests, makeChannelStreamTap as __makeChannelStreamTapForTests, makeChannelProgressObserver as __makeChannelProgressObserverForTests, makeChannelLifecycleReporter as __makeChannelLifecycleReporterForTests, };
4768
4860
  /**
4769
- * Per-chat latch for the channel egress that runs AFTER `trigger:turn-complete` is written (the
4770
- * final `send()` and the done/error reactions). The run loop opens it before writing the chunk and
4771
- * settles it once that egress has finished, so a caller that saw the chunk can wait for the
4772
- * connector to be quiet instead of guessing with timers. One latch per chat at a time; a chat with
4773
- * no open latch is quiet.
4774
- */
4775
- const channelEgressLatches = new Map();
4776
- function openChannelEgressLatch(chatId) {
4777
- let settle;
4778
- const promise = new Promise((resolve) => (settle = resolve));
4779
- channelEgressLatches.set(chatId, { promise, settle });
4780
- }
4781
- function settleChannelEgressLatch(chatId) {
4782
- const latch = channelEgressLatches.get(chatId);
4783
- if (!latch)
4784
- return;
4785
- channelEgressLatches.delete(chatId);
4786
- latch.settle();
4861
+ * Per-chat latches for work the run loop does AFTER it writes `trigger:turn-complete`. The loop
4862
+ * opens a latch before writing the chunk and settles it once that work has finished, so a caller
4863
+ * that saw the chunk can wait for it instead of guessing with timers. One latch per chat at a
4864
+ * time (opening an open one keeps it); a chat with no open latch is quiet.
4865
+ */
4866
+ function createChatLatches() {
4867
+ const latches = new Map();
4868
+ return {
4869
+ open(chatId) {
4870
+ if (latches.has(chatId))
4871
+ return;
4872
+ let settle;
4873
+ const promise = new Promise((resolve) => (settle = resolve));
4874
+ latches.set(chatId, { promise, settle });
4875
+ },
4876
+ /** `"run-ended"` marks a settle made as the run exits, so a waiter knows the run itself is over. */
4877
+ settle(chatId, reason = "settled") {
4878
+ const latch = latches.get(chatId);
4879
+ if (!latch)
4880
+ return;
4881
+ latches.delete(chatId);
4882
+ latch.settle(reason);
4883
+ },
4884
+ /** Why the latch settled; `undefined` when nothing is open. */
4885
+ async wait(chatId) {
4886
+ return latches.get(chatId)?.promise;
4887
+ },
4888
+ };
4787
4889
  }
4890
+ /** The channel egress after the chunk: the final `send()` and the done/error reactions. */
4891
+ const channelEgressLatches = createChatLatches();
4892
+ /** The post-turn hooks after the chunk: `onTurnComplete` and the turn-complete transcript save. */
4893
+ const postTurnHookLatches = createChatLatches();
4788
4894
  /** Resolves once the post-turn channel egress for `chatId` has finished (immediately when none is running). */
4789
4895
  export async function __waitForChannelEgressForTests(chatId) {
4790
- await channelEgressLatches.get(chatId)?.promise;
4896
+ return channelEgressLatches.wait(chatId);
4897
+ }
4898
+ /** Resolves once the post-turn hooks for `chatId` have finished (immediately when none are running). */
4899
+ export async function __waitForPostTurnHooksForTests(chatId) {
4900
+ return postTurnHookLatches.wait(chatId);
4791
4901
  }
4792
4902
  function chatCustomAgent(options) {
4793
4903
  const { clientDataSchema, onClientDataValidationError, clientDataReportErrorAt, run: userRun, ...restOptions } = options;
@@ -6690,6 +6800,7 @@ function chatAgent(options) {
6690
6800
  message: lastUIMessage,
6691
6801
  chatId: currentWirePayload.chatId,
6692
6802
  turn,
6803
+ clientData: chatEventContext().clientData,
6693
6804
  });
6694
6805
  }
6695
6806
  catch {
@@ -6905,6 +7016,7 @@ function chatAgent(options) {
6905
7016
  messages: cleanedUIMessages,
6906
7017
  chatId: currentWirePayload.chatId,
6907
7018
  turn,
7019
+ clientData: chatEventContext().clientData,
6908
7020
  trigger: currentWirePayload.trigger,
6909
7021
  });
6910
7022
  }, {
@@ -7044,7 +7156,11 @@ function chatAgent(options) {
7044
7156
  if (idx !== -1) {
7045
7157
  const previous = accumulatedUIMessages[idx];
7046
7158
  accumulatedUIMessages[idx] = mergeIncomingIntoHydrated(previous, incoming);
7047
- replacedPairs.push({ previous, merged: accumulatedUIMessages[idx] });
7159
+ replacedPairs.push({
7160
+ previous,
7161
+ merged: accumulatedUIMessages[idx],
7162
+ isLast: idx === accumulatedUIMessages.length - 1,
7163
+ });
7048
7164
  replaced = true;
7049
7165
  }
7050
7166
  else {
@@ -7054,8 +7170,8 @@ function chatAgent(options) {
7054
7170
  }
7055
7171
  if (replaced) {
7056
7172
  let inPlace = true;
7057
- for (const { previous, merged } of replacedPairs) {
7058
- if (!(await replaceModelRun(accumulatedMessages, previous, merged, 0))) {
7173
+ for (const { previous, merged, isLast } of replacedPairs) {
7174
+ if (!(await replaceModelRun(accumulatedMessages, previous, merged, 0, undefined, isLast))) {
7059
7175
  inPlace = false;
7060
7176
  break;
7061
7177
  }
@@ -7589,6 +7705,21 @@ function chatAgent(options) {
7589
7705
  new Promise((r) => setTimeout(r, 2_000)),
7590
7706
  ]);
7591
7707
  }
7708
+ /**
7709
+ * A stream-level failure (an `error` chunk) is the turn's finish
7710
+ * reason. A step rejected before the model runs (an invalid prompt,
7711
+ * a throwing `prepareStep`) ends the stream with one but no finish
7712
+ * reason of its own, or the previous step's. Tool errors do not
7713
+ * count: they are `tool-input-error` / `tool-output-error` parts
7714
+ * that go back to the model, and the turn finishes normally.
7715
+ */
7716
+ // Looked up once per turn; the buffer is only scanned again from
7717
+ // `errorScanLength` on, in case a chunk lands after this point.
7718
+ const errorScanLength = turnBufferedChunks.length;
7719
+ const earlyErrorChunk = turnBufferedChunks.find(isErrorUIMessageChunk);
7720
+ if (earlyErrorChunk) {
7721
+ capturedFinishReason = "error";
7722
+ }
7592
7723
  capturedResponseMessage =
7593
7724
  (await locals.get(chatManagedResponseKey)?.snapshot()) ?? capturedResponseMessage;
7594
7725
  if (capturedResponseMessage)
@@ -7613,18 +7744,7 @@ function chatAgent(options) {
7613
7744
  /* non-fatal — usage capture failed */
7614
7745
  }
7615
7746
  }
7616
- const lastStepUsagePromise = runResult != null ? runResult.usage : undefined;
7617
- if (typeof lastStepUsagePromise?.then === "function") {
7618
- try {
7619
- lastStepUsage = (await Promise.race([
7620
- lastStepUsagePromise,
7621
- new Promise((r) => setTimeout(() => r(undefined), 2_000)),
7622
- ]));
7623
- }
7624
- catch {
7625
- /* non-fatal — usage capture failed */
7626
- }
7627
- }
7747
+ lastStepUsage = await readLastStepUsage(runResult);
7628
7748
  if (turnUsage) {
7629
7749
  cumulativeUsage = addUsage(cumulativeUsage, turnUsage);
7630
7750
  previousTurnUsage = turnUsage;
@@ -7815,7 +7935,8 @@ function chatAgent(options) {
7815
7935
  if (capturedResponseMessage) {
7816
7936
  responseCommitted = true;
7817
7937
  capturedPartialResponse = capturedResponseMessage;
7818
- const errorChunk = turnBufferedChunks.find((chunk) => chunk.type === "error");
7938
+ const errorChunk = (earlyErrorChunk ??
7939
+ turnBufferedChunks.slice(errorScanLength).find(isErrorUIMessageChunk));
7819
7940
  channelStreamErrorText = errorChunk?.errorText;
7820
7941
  channelTurnText = turnBufferedChunks
7821
7942
  .map((chunk) => {
@@ -7854,9 +7975,7 @@ function chatAgent(options) {
7854
7975
  usage: contextUsage,
7855
7976
  turnUsage,
7856
7977
  totalUsage: cumulativeUsage,
7857
- chatId: currentWirePayload.chatId,
7858
- turn,
7859
- clientData,
7978
+ ...chatEventContext(),
7860
7979
  source: "outer",
7861
7980
  });
7862
7981
  if (shouldTrigger) {
@@ -7876,9 +7995,7 @@ function chatAgent(options) {
7876
7995
  messages: accumulatedMessages,
7877
7996
  usage: turnUsage,
7878
7997
  totalUsage: cumulativeUsage,
7879
- chatId: currentWirePayload.chatId,
7880
- turn,
7881
- clientData,
7998
+ ...chatEventContext(),
7882
7999
  source: "outer",
7883
8000
  });
7884
8001
  // Apply compactModelMessages/compactUIMessages callbacks, or defaults.
@@ -7924,8 +8041,7 @@ function chatAgent(options) {
7924
8041
  inputTokens: turnUsage.inputTokens,
7925
8042
  outputTokens: turnUsage.outputTokens,
7926
8043
  stepNumber: -1, // outer loop, not a step
7927
- chatId: currentWirePayload.chatId,
7928
- turn,
8044
+ ...chatEventContext(),
7929
8045
  writer: { write, merge },
7930
8046
  });
7931
8047
  }
@@ -8044,7 +8160,8 @@ function chatAgent(options) {
8044
8160
  }
8045
8161
  }
8046
8162
  settleRecoveredTurn(currentWirePayload);
8047
- openChannelEgressLatch(currentWirePayload.chatId);
8163
+ channelEgressLatches.open(currentWirePayload.chatId);
8164
+ postTurnHookLatches.open(currentWirePayload.chatId);
8048
8165
  // Write turn-complete control chunk — closes the frontend stream.
8049
8166
  const turnCompleteResult = await writeTurnCompleteChunk(currentWirePayload.chatId, turnAccessToken);
8050
8167
  // Tool calls that have not had controls posted yet; a resume with the rest still
@@ -8243,7 +8360,7 @@ function chatAgent(options) {
8243
8360
  });
8244
8361
  }
8245
8362
  }
8246
- settleChannelEgressLatch(currentWirePayload.chatId);
8363
+ channelEgressLatches.settle(currentWirePayload.chatId);
8247
8364
  // Fire onTurnComplete — stream is closed, use for persistence.
8248
8365
  if (onTurnComplete) {
8249
8366
  await tracer.startActiveSpan("onTurnComplete()", async () => {
@@ -8342,6 +8459,7 @@ function chatAgent(options) {
8342
8459
  });
8343
8460
  }
8344
8461
  }
8462
+ postTurnHookLatches.settle(currentWirePayload.chatId);
8345
8463
  } // end if (!isAction)
8346
8464
  // NOTE: We intentionally do NOT await deferred work from onTurnComplete here.
8347
8465
  // Promises deferred in onTurnComplete (e.g. background self-review via
@@ -8455,12 +8573,13 @@ function chatAgent(options) {
8455
8573
  if (turnError instanceof Error &&
8456
8574
  turnError.name === "AbortError" &&
8457
8575
  runSignal.aborted) {
8458
- settleChannelEgressLatch(currentWirePayload.chatId);
8576
+ channelEgressLatches.settle(currentWirePayload.chatId);
8577
+ postTurnHookLatches.settle(currentWirePayload.chatId);
8459
8578
  channelLifecycle.dispose();
8460
8579
  // Full run cancellation — exit immediately
8461
8580
  throw turnError;
8462
8581
  }
8463
- settleChannelEgressLatch(currentWirePayload.chatId);
8582
+ channelEgressLatches.settle(currentWirePayload.chatId);
8464
8583
  // OOM errors must escape the turn loop so the task runtime can
8465
8584
  // honor `retry.outOfMemory.machine` (set on chat.agent via
8466
8585
  // `oomMachine`). Catching them here would keep the dead worker
@@ -8581,6 +8700,7 @@ function chatAgent(options) {
8581
8700
  writer.write({ type: "error", errorText });
8582
8701
  });
8583
8702
  // Signal turn complete so the client knows this turn is done
8703
+ postTurnHookLatches.open(currentWirePayload.chatId);
8584
8704
  errorTurnCompleteResult = await writeTurnCompleteChunk(currentWirePayload.chatId);
8585
8705
  // A later action's snapshot reuses this cursor, so it has to move
8586
8706
  // here too or that snapshot resumes from before the failed turn.
@@ -8781,6 +8901,7 @@ function chatAgent(options) {
8781
8901
  });
8782
8902
  }
8783
8903
  }
8904
+ postTurnHookLatches.settle(currentWirePayload.chatId);
8784
8905
  if (locals.get(chatCloseRequestedKey)) {
8785
8906
  await performChatClose();
8786
8907
  return;
@@ -8836,6 +8957,12 @@ function chatAgent(options) {
8836
8957
  // reaching one of the loop's close checks (a turn timeout, an OOM
8837
8958
  // re-throw). `performChatClose` is idempotent, so the ordinary path
8838
8959
  // having already run it costs nothing here.
8960
+ // Any exit that skips a turn's own settle (an OOM re-throw, a throw from the turn's
8961
+ // error handler) leaves its post-turn latches open, and the module-level entries would
8962
+ // outlive this run and block the next one for the same chat. Settling is a no-op when
8963
+ // nothing is open, and runs first so a throwing close cannot skip it.
8964
+ channelEgressLatches.settle(currentWirePayload.chatId, "run-ended");
8965
+ postTurnHookLatches.settle(currentWirePayload.chatId, "run-ended");
8839
8966
  await performChatClose();
8840
8967
  // `stopSub` is registered post-preload so the close-during-preload
8841
8968
  // early-return path may exit before it ever attached. Guard the
@@ -9740,6 +9867,10 @@ async function flushDeferredChatCustomAgentClientDataErrors() {
9740
9867
  }
9741
9868
  }
9742
9869
  }
9870
+ /** Whether a buffered chunk is a stream-level `error` chunk. */
9871
+ function isErrorUIMessageChunk(chunk) {
9872
+ return chunk.type === "error";
9873
+ }
9743
9874
  /**
9744
9875
  * Pass every chunk through untouched while recording it in `buffer`. Handles
9745
9876
  * both the `AsyncIterable` and `ReadableStream` shapes `toUIMessageStream()`
@@ -9921,6 +10052,7 @@ class ChatMessageAccumulator {
9921
10052
  const ui = replace ? [] : [...this.uiMessages];
9922
10053
  const lane = replace ? [] : [...this.modelMessages];
9923
10054
  let appendedModelCount = 0;
10055
+ let appendedUiCount = 0;
9924
10056
  let rebuild = replace;
9925
10057
  for (const raw of messages) {
9926
10058
  const message = raw.role === "assistant" ? cleanupAbortedParts(raw) : raw;
@@ -9933,13 +10065,18 @@ class ChatMessageAccumulator {
9933
10065
  const idx = ui.findIndex((m) => m.id === resolved.id);
9934
10066
  const previous = ui[idx];
9935
10067
  ui[idx] = mergeIncomingIntoHydrated(previous, resolved);
9936
- if (!rebuild && !(await replaceModelRun(lane, previous, ui[idx], appendedModelCount))) {
10068
+ // The lane's tail is only this message's place when nothing came after it
10069
+ // before this call's own appends.
10070
+ const isLast = idx === ui.length - 1 - appendedUiCount;
10071
+ if (!rebuild &&
10072
+ !(await replaceModelRun(lane, previous, ui[idx], appendedModelCount, undefined, isLast))) {
9937
10073
  rebuild = true;
9938
10074
  }
9939
10075
  continue;
9940
10076
  }
9941
10077
  ui.push(message);
9942
10078
  if (!rebuild) {
10079
+ appendedUiCount++;
9943
10080
  const converted = await toModelMessages([message]);
9944
10081
  lane.push(...converted);
9945
10082
  appendedModelCount += converted.length;
@@ -10112,13 +10249,14 @@ class ChatMessageAccumulator {
10112
10249
  const comp = this._compaction;
10113
10250
  const pm = this._pendingMessages;
10114
10251
  const queue = this._steeringQueue;
10115
- return retainStepMessages(async ({ messages, steps }) => {
10252
+ return retainStepMessages(async ({ messages, steps, responseMessages, }) => {
10116
10253
  let resultMessages;
10117
10254
  // 1. Compaction
10118
10255
  if (comp) {
10119
10256
  const result = await chatCompact(messages, steps, {
10257
+ responseMessages,
10120
10258
  shouldCompact: comp.shouldCompact,
10121
- summarize: (msgs) => comp.summarize({ messages: msgs, source: "inner" }),
10259
+ summarize: innerSummarize(comp, steps),
10122
10260
  });
10123
10261
  if (result.type !== "skipped") {
10124
10262
  resultMessages = result.messages;
@@ -10137,9 +10275,11 @@ class ChatMessageAccumulator {
10137
10275
  }
10138
10276
  /**
10139
10277
  * Run outer-loop compaction if needed. Call after adding the response
10140
- * and capturing usage. Pass the LAST step's usage (`result.usage`), which is
10141
- * the context the model held on its final call; `result.totalUsage` sums every
10142
- * step of a tool-using turn and belongs in `context.turnUsage`. Applies
10278
+ * and capturing usage. Pass the LAST step's usage
10279
+ * (`(await result.steps).at(-1)?.usage`), which is the context the model held on
10280
+ * its final call. `result.usage` is that on AI SDK 6 but the turn's sum on AI SDK 7,
10281
+ * so read `steps`. The turn's sum (`result.totalUsage` on AI SDK 6, `result.usage`
10282
+ * on AI SDK 7) belongs in `context.turnUsage`. Applies
10143
10283
  * `compactModelMessages` and `compactUIMessages` callbacks if configured.
10144
10284
  *
10145
10285
  * @returns `true` if compaction was performed, `false` otherwise.
@@ -10147,6 +10287,13 @@ class ChatMessageAccumulator {
10147
10287
  async compactIfNeeded(usage, context) {
10148
10288
  if (!this._compaction || !usage)
10149
10289
  return false;
10290
+ // An explicit context wins; inside a chat turn the turn's own fills in what it leaves out.
10291
+ const turnContext = chatEventContext();
10292
+ const eventContext = {
10293
+ chatId: context?.chatId ?? turnContext.chatId,
10294
+ turn: context?.turn ?? turnContext.turn,
10295
+ clientData: context?.clientData ?? turnContext.clientData,
10296
+ };
10150
10297
  const shouldTrigger = await this._compaction.shouldCompact({
10151
10298
  messages: this.modelMessages,
10152
10299
  totalTokens: usage.totalTokens,
@@ -10155,9 +10302,7 @@ class ChatMessageAccumulator {
10155
10302
  usage,
10156
10303
  turnUsage: context?.turnUsage,
10157
10304
  totalUsage: context?.totalUsage,
10158
- chatId: context?.chatId,
10159
- turn: context?.turn,
10160
- clientData: context?.clientData,
10305
+ ...eventContext,
10161
10306
  source: "outer",
10162
10307
  });
10163
10308
  if (!shouldTrigger)
@@ -10166,18 +10311,16 @@ class ChatMessageAccumulator {
10166
10311
  messages: this.modelMessages,
10167
10312
  usage,
10168
10313
  totalUsage: context?.totalUsage,
10169
- chatId: context?.chatId,
10170
- turn: context?.turn,
10171
- clientData: context?.clientData,
10314
+ ...eventContext,
10172
10315
  source: "outer",
10173
10316
  });
10174
10317
  const compactEvent = {
10175
10318
  summary,
10176
10319
  uiMessages: this.uiMessages,
10177
10320
  modelMessages: this.modelMessages,
10178
- chatId: context?.chatId ?? "",
10179
- turn: context?.turn ?? 0,
10180
- clientData: context?.clientData,
10321
+ chatId: eventContext.chatId ?? "",
10322
+ turn: eventContext.turn ?? 0,
10323
+ clientData: eventContext.clientData,
10181
10324
  source: "outer",
10182
10325
  };
10183
10326
  this.modelMessages = this._compaction.compactModelMessages
@@ -10475,6 +10618,7 @@ function createChatSession(payload, options) {
10475
10618
  message: lastUIMessage,
10476
10619
  chatId: currentPayload.chatId,
10477
10620
  turn,
10621
+ clientData: chatEventContext().clientData,
10478
10622
  });
10479
10623
  }
10480
10624
  catch {
@@ -10631,18 +10775,7 @@ function createChatSession(payload, options) {
10631
10775
  /* non-fatal */
10632
10776
  }
10633
10777
  }
10634
- const lastStepUsagePromise = source.usage;
10635
- if (typeof lastStepUsagePromise?.then === "function") {
10636
- try {
10637
- lastStepUsage = (await Promise.race([
10638
- lastStepUsagePromise,
10639
- new Promise((r) => setTimeout(() => r(undefined), 2_000)),
10640
- ]));
10641
- }
10642
- catch {
10643
- /* non-fatal */
10644
- }
10645
- }
10778
+ lastStepUsage = await readLastStepUsage(source);
10646
10779
  // Outer-loop compaction (same logic as chat.agent)
10647
10780
  if (sessionCompaction && turnUsage && !turnObj.stopped) {
10648
10781
  const contextUsage = lastStepUsage ?? turnUsage;
@@ -10654,9 +10787,7 @@ function createChatSession(payload, options) {
10654
10787
  usage: contextUsage,
10655
10788
  turnUsage,
10656
10789
  totalUsage: cumulativeUsage,
10657
- chatId: currentPayload.chatId,
10658
- turn,
10659
- clientData: currentPayload.metadata,
10790
+ ...chatEventContext(),
10660
10791
  source: "outer",
10661
10792
  });
10662
10793
  if (shouldTrigger) {
@@ -10664,9 +10795,7 @@ function createChatSession(payload, options) {
10664
10795
  messages: accumulator.modelMessages,
10665
10796
  usage: turnUsage,
10666
10797
  totalUsage: cumulativeUsage,
10667
- chatId: currentPayload.chatId,
10668
- turn,
10669
- clientData: currentPayload.metadata,
10798
+ ...chatEventContext(),
10670
10799
  source: "outer",
10671
10800
  });
10672
10801
  const compactEvent = {
@@ -10710,12 +10839,13 @@ function createChatSession(payload, options) {
10710
10839
  const hasPending = !!sessionPendingMessages;
10711
10840
  if (!hasCompaction && !hasPending)
10712
10841
  return undefined;
10713
- return retainStepMessages(async ({ messages: stepMsgs, steps, }) => {
10842
+ return retainStepMessages(async ({ messages: stepMsgs, steps, responseMessages, }) => {
10714
10843
  let resultMessages;
10715
10844
  if (sessionCompaction) {
10716
10845
  const compactResult = await chatCompact(stepMsgs, steps, {
10846
+ responseMessages,
10717
10847
  shouldCompact: sessionCompaction.shouldCompact,
10718
- summarize: (msgs) => sessionCompaction.summarize({ messages: msgs, source: "inner" }),
10848
+ summarize: innerSummarize(sessionCompaction, steps),
10719
10849
  });
10720
10850
  if (compactResult.type !== "skipped") {
10721
10851
  resultMessages = compactResult.messages;
@@ -11111,7 +11241,7 @@ function overrideRequestHeaders(authHeaders) {
11111
11241
  async function callSessionsCreateWithOverride(args) {
11112
11242
  const apiClient = apiClientManager.client;
11113
11243
  if (!apiClient) {
11114
- throw new Error("chat.createStartSessionAction: no API access token configured. Set TRIGGER_SECRET_KEY or call apiClientManager.setGlobalAPIClientConfiguration before invoking the action.");
11244
+ throw new Error("chat.createStartSessionAction: no API access token configured. Call configure({ accessToken }) or pass apiClient: { accessToken } to createStartSessionAction before invoking the action.");
11115
11245
  }
11116
11246
  const authHeaders = apiClient.getAuthenticationHeaders("createSession");
11117
11247
  const ctx = { endpoint: "sessions", chatId: args.chatId };
@@ -11135,55 +11265,60 @@ async function callSessionsCreateWithOverride(args) {
11135
11265
  async function mintPublicTokenWithOverride(args) {
11136
11266
  const apiClient = apiClientManager.client;
11137
11267
  if (!apiClient) {
11138
- throw new Error("chat.createStartSessionAction: no API access token configured for JWT mint.");
11268
+ throw new Error("chat.createStartSessionAction: no API access token configured for public-token minting. Call configure({ accessToken }) or pass apiClient: { accessToken } to createStartSessionAction.");
11139
11269
  }
11140
11270
  const authHeaders = apiClient.getAuthenticationHeaders("createPublicToken");
11141
- const accessToken = authHeaders.Authorization.slice("Bearer ".length);
11142
11271
  const ctx = { endpoint: "auth", chatId: args.chatId };
11143
11272
  const scopes = [`read:sessions:${args.chatId}`, `write:sessions:${args.chatId}`];
11144
- const serverMint = isAdditionalApiKey(accessToken) || isRunToken(accessToken);
11145
- const endpoint = serverMint ? "/api/v1/auth/public-tokens" : "/api/v1/auth/jwt/claims";
11146
- const url = `${resolveChatStartBaseURL("auth", args.chatId, args.baseURLOption)}${endpoint}`;
11273
+ const baseURL = resolveChatStartBaseURL("auth", args.chatId, args.baseURLOption);
11274
+ const url = `${baseURL}/api/v1/auth/public-tokens`;
11147
11275
  const init = {
11148
11276
  method: "POST",
11149
11277
  headers: overrideRequestHeaders(authHeaders),
11150
- ...(serverMint
11151
- ? {
11152
- body: JSON.stringify({
11153
- scopes,
11154
- expirationTime: args.expirationTime instanceof Date
11155
- ? Math.floor(args.expirationTime.getTime() / 1000)
11156
- : args.expirationTime,
11157
- }),
11158
- }
11159
- : {}),
11278
+ body: JSON.stringify({
11279
+ scopes,
11280
+ expirationTime: args.expirationTime instanceof Date
11281
+ ? Math.floor(args.expirationTime.getTime() / 1000)
11282
+ : args.expirationTime,
11283
+ }),
11160
11284
  };
11161
11285
  const response = args.fetchOverride
11162
11286
  ? await args.fetchOverride(url, init, ctx)
11163
11287
  : await fetch(url, init);
11164
11288
  if (!response.ok) {
11165
- if (serverMint && response.status === 404) {
11166
- const credentialType = isRunToken(accessToken) ? "run token" : "additional API key";
11167
- throw new Error(`This ${credentialType} cannot self-sign public tokens, and the server does not support public-token minting. Upgrade the server or use the root API key.`);
11289
+ const accessToken = authHeaders.Authorization.slice("Bearer ".length);
11290
+ if (canFallbackToLocalPublicToken(accessToken, scopes, response.status)) {
11291
+ await response.arrayBuffer();
11292
+ const claimsURL = `${baseURL}/api/v1/auth/jwt/claims`;
11293
+ const claimsInit = {
11294
+ method: "POST",
11295
+ headers: overrideRequestHeaders(authHeaders),
11296
+ };
11297
+ const claimsResponse = args.fetchOverride
11298
+ ? await args.fetchOverride(claimsURL, claimsInit, ctx)
11299
+ : await fetch(claimsURL, claimsInit);
11300
+ if (!claimsResponse.ok) {
11301
+ const text = await claimsResponse.text().catch(() => "");
11302
+ throw new Error(`auth.generateJWTClaims failed: ${claimsResponse.status} ${text}`);
11303
+ }
11304
+ const claims = (await claimsResponse.json());
11305
+ return generateJWT({
11306
+ secretKey: accessToken,
11307
+ payload: { ...claims, scopes },
11308
+ expirationTime: args.expirationTime,
11309
+ });
11310
+ }
11311
+ if (response.status === 404) {
11312
+ throw new Error("The server does not support public-token minting for this credential. Upgrade your self-hosted server.");
11168
11313
  }
11169
11314
  const text = await response.text().catch(() => "");
11170
11315
  throw new Error(`auth.createPublicToken failed: ${response.status} ${text}`);
11171
11316
  }
11172
11317
  const responseBody = (await response.json());
11173
- if (serverMint) {
11174
- if (typeof responseBody.token !== "string") {
11175
- throw new Error("auth.createPublicToken failed: server response did not include a token");
11176
- }
11177
- return responseBody.token;
11318
+ if (typeof responseBody.token !== "string") {
11319
+ throw new Error("auth.createPublicToken failed: server response did not include a token");
11178
11320
  }
11179
- return generateJWT({
11180
- secretKey: accessToken,
11181
- payload: {
11182
- ...responseBody,
11183
- scopes,
11184
- },
11185
- expirationTime: args.expirationTime,
11186
- });
11321
+ return responseBody.token;
11187
11322
  }
11188
11323
  /**
11189
11324
  * Creates a server-side helper that reads a conversation from a transcript