@trigger.dev/sdk 4.8.5 → 4.8.6

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 (187) hide show
  1. package/dist/commonjs/v3/ai.d.ts +29 -5
  2. package/dist/commonjs/v3/ai.js +160 -114
  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 +35 -12
  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/webhooks.d.ts +8 -2
  28. package/dist/commonjs/v3/webhooks.js +20 -11
  29. package/dist/commonjs/v3/webhooks.js.map +1 -1
  30. package/dist/commonjs/version.js +1 -1
  31. package/dist/esm/v3/ai.d.ts +29 -5
  32. package/dist/esm/v3/ai.js +162 -117
  33. package/dist/esm/v3/ai.js.map +1 -1
  34. package/dist/esm/v3/auth.d.ts +4 -4
  35. package/dist/esm/v3/auth.js +22 -76
  36. package/dist/esm/v3/auth.js.map +1 -1
  37. package/dist/esm/v3/chat-client.d.ts +1 -1
  38. package/dist/esm/v3/chat.d.ts +1 -1
  39. package/dist/esm/v3/prompt.js +6 -8
  40. package/dist/esm/v3/prompt.js.map +1 -1
  41. package/dist/esm/v3/runs.d.ts +5 -1
  42. package/dist/esm/v3/runs.js.map +1 -1
  43. package/dist/esm/v3/sessions.js +10 -7
  44. package/dist/esm/v3/sessions.js.map +1 -1
  45. package/dist/esm/v3/shared.d.ts +2 -2
  46. package/dist/esm/v3/shared.js +16 -15
  47. package/dist/esm/v3/shared.js.map +1 -1
  48. package/dist/esm/v3/steeringContext.d.ts +35 -10
  49. package/dist/esm/v3/steeringContext.js +34 -12
  50. package/dist/esm/v3/steeringContext.js.map +1 -1
  51. package/dist/esm/v3/streams.js +10 -8
  52. package/dist/esm/v3/streams.js.map +1 -1
  53. package/dist/esm/v3/tasks.d.ts +2 -2
  54. package/dist/esm/v3/tasks.js.map +1 -1
  55. package/dist/esm/v3/test/mock-chat-agent.js +25 -14
  56. package/dist/esm/v3/test/mock-chat-agent.js.map +1 -1
  57. package/dist/esm/v3/webhooks.d.ts +8 -2
  58. package/dist/esm/v3/webhooks.js +20 -11
  59. package/dist/esm/v3/webhooks.js.map +1 -1
  60. package/dist/esm/version.js +1 -1
  61. package/docs/ai/observability.mdx +25 -19
  62. package/docs/ai/prompts.mdx +25 -19
  63. package/docs/ai-chat/actions.mdx +1 -1
  64. package/docs/ai-chat/anatomy.mdx +1 -1
  65. package/docs/ai-chat/backend.mdx +24 -24
  66. package/docs/ai-chat/background-injection.mdx +5 -5
  67. package/docs/ai-chat/chat-local.mdx +2 -2
  68. package/docs/ai-chat/compaction.mdx +38 -15
  69. package/docs/ai-chat/custom-agents.mdx +4 -4
  70. package/docs/ai-chat/error-handling.mdx +4 -4
  71. package/docs/ai-chat/fast-starts.mdx +8 -7
  72. package/docs/ai-chat/frontend.mdx +6 -2
  73. package/docs/ai-chat/lifecycle-hooks.mdx +11 -11
  74. package/docs/ai-chat/migrating-from-a-route-handler.mdx +4 -4
  75. package/docs/ai-chat/migrating-from-hydrate-messages.mdx +1 -1
  76. package/docs/ai-chat/overview.mdx +1 -1
  77. package/docs/ai-chat/patterns/branching-conversations.mdx +1 -1
  78. package/docs/ai-chat/patterns/database-persistence.mdx +2 -2
  79. package/docs/ai-chat/patterns/human-in-the-loop.mdx +1 -1
  80. package/docs/ai-chat/patterns/native-compaction.mdx +29 -9
  81. package/docs/ai-chat/patterns/persistence-and-replay.mdx +1 -1
  82. package/docs/ai-chat/patterns/redacting-telemetry.mdx +2 -2
  83. package/docs/ai-chat/patterns/skills.mdx +2 -2
  84. package/docs/ai-chat/patterns/sub-agents.mdx +1 -1
  85. package/docs/ai-chat/patterns/tool-result-auditing.mdx +1 -1
  86. package/docs/ai-chat/patterns/trusted-edge-signals.mdx +2 -2
  87. package/docs/ai-chat/patterns/version-upgrades.mdx +2 -2
  88. package/docs/ai-chat/pending-messages.mdx +5 -5
  89. package/docs/ai-chat/prompt-caching.mdx +7 -7
  90. package/docs/ai-chat/quick-start.mdx +2 -2
  91. package/docs/ai-chat/testing.mdx +9 -9
  92. package/docs/ai-chat/tools.mdx +9 -5
  93. package/docs/ai-chat/transcript-storage.mdx +2 -2
  94. package/docs/ai-chat/types.mdx +10 -3
  95. package/docs/apikeys.mdx +2 -1
  96. package/docs/cli-login-commands.mdx +12 -0
  97. package/docs/concurrency.mdx +40 -0
  98. package/docs/database-connections.mdx +1 -1
  99. package/docs/deploy-environment-variables.mdx +84 -2
  100. package/docs/deployment/trusted-publishing.mdx +4 -0
  101. package/docs/errors-retrying.mdx +25 -18
  102. package/docs/github-actions.mdx +1 -1
  103. package/docs/github-integration.mdx +21 -0
  104. package/docs/how-it-works.mdx +4 -2
  105. package/docs/introduction.mdx +1 -1
  106. package/docs/mcp-introduction.mdx +7 -1
  107. package/docs/mcp-remote.mdx +90 -0
  108. package/docs/oidc/azure.mdx +81 -0
  109. package/docs/oidc/databricks.mdx +99 -0
  110. package/docs/oidc/doppler.mdx +60 -0
  111. package/docs/oidc/gcp.mdx +109 -0
  112. package/docs/oidc/hugging-face.mdx +90 -0
  113. package/docs/oidc/openrouter.mdx +91 -0
  114. package/docs/oidc/snowflake.mdx +91 -0
  115. package/docs/oidc/vault.mdx +71 -0
  116. package/docs/oidc.mdx +2 -0
  117. package/docs/runs/metadata.mdx +1 -1
  118. package/docs/self-hosting/docker.mdx +2 -0
  119. package/docs/self-hosting/env/webapp.mdx +4 -1
  120. package/docs/self-hosting/kubernetes.mdx +2 -0
  121. package/docs/tasks/schemaTask.mdx +26 -13
  122. package/docs/tasks/streams.mdx +2 -2
  123. package/docs/troubleshooting.mdx +7 -0
  124. package/docs/webhooks/channels.mdx +6 -6
  125. package/docs/webhooks/connect.mdx +9 -75
  126. package/docs/webhooks/custom-providers.mdx +87 -0
  127. package/docs/webhooks/deliveries.mdx +7 -7
  128. package/docs/webhooks/filters.mdx +2 -2
  129. package/docs/webhooks/how-it-works.mdx +58 -0
  130. package/docs/webhooks/human-in-the-loop.mdx +3 -3
  131. package/docs/webhooks/linear.mdx +5 -3
  132. package/docs/webhooks/overview.mdx +20 -71
  133. package/docs/webhooks/own-handler.mdx +62 -0
  134. package/docs/webhooks/pricing-and-limits.mdx +13 -0
  135. package/docs/webhooks/providers/attio.mdx +38 -0
  136. package/docs/webhooks/providers/autumn.mdx +40 -0
  137. package/docs/webhooks/providers/cal-com.mdx +41 -0
  138. package/docs/webhooks/providers/calendly.mdx +41 -0
  139. package/docs/webhooks/providers/clerk.mdx +40 -0
  140. package/docs/webhooks/providers/discord.mdx +45 -0
  141. package/docs/webhooks/providers/docusign.mdx +41 -0
  142. package/docs/webhooks/providers/elevenlabs.mdx +41 -0
  143. package/docs/webhooks/providers/firecrawl.mdx +54 -0
  144. package/docs/webhooks/providers/github.mdx +46 -0
  145. package/docs/webhooks/providers/gitlab.mdx +41 -0
  146. package/docs/webhooks/providers/granola.mdx +44 -0
  147. package/docs/webhooks/providers/hookdeck.mdx +98 -0
  148. package/docs/webhooks/providers/infisical.mdx +41 -0
  149. package/docs/webhooks/providers/jira.mdx +41 -0
  150. package/docs/webhooks/providers/langfuse.mdx +40 -0
  151. package/docs/webhooks/providers/linear.mdx +45 -0
  152. package/docs/webhooks/providers/loops.mdx +40 -0
  153. package/docs/webhooks/providers/metronome.mdx +42 -0
  154. package/docs/webhooks/providers/mux.mdx +40 -0
  155. package/docs/webhooks/providers/openai.mdx +40 -0
  156. package/docs/webhooks/providers/pagerduty.mdx +40 -0
  157. package/docs/webhooks/providers/plain.mdx +41 -0
  158. package/docs/webhooks/providers/planetscale.mdx +40 -0
  159. package/docs/webhooks/providers/recall-ai.mdx +54 -0
  160. package/docs/webhooks/providers/replicate.mdx +41 -0
  161. package/docs/webhooks/providers/resend.mdx +44 -0
  162. package/docs/webhooks/providers/retell.mdx +41 -0
  163. package/docs/webhooks/providers/sentry.mdx +41 -0
  164. package/docs/webhooks/providers/shopify.mdx +49 -0
  165. package/docs/webhooks/providers/slack.mdx +48 -0
  166. package/docs/webhooks/providers/square.mdx +40 -0
  167. package/docs/webhooks/providers/stripe.mdx +86 -0
  168. package/docs/webhooks/providers/supabase.mdx +41 -0
  169. package/docs/webhooks/providers/tailscale.mdx +43 -0
  170. package/docs/webhooks/providers/typeform.mdx +42 -0
  171. package/docs/webhooks/providers/vapi.mdx +47 -0
  172. package/docs/webhooks/providers/vercel.mdx +40 -0
  173. package/docs/webhooks/providers/whatsapp.mdx +45 -0
  174. package/docs/webhooks/providers/workos.mdx +41 -0
  175. package/docs/webhooks/providers/zendesk.mdx +41 -0
  176. package/docs/webhooks/providers.mdx +53 -0
  177. package/docs/webhooks/quick-start.mdx +131 -0
  178. package/docs/webhooks/session-routing.mdx +6 -6
  179. package/docs/webhooks/signing-secrets.mdx +80 -0
  180. package/docs/webhooks/slack.mdx +11 -2
  181. package/docs/webhooks/verification.mdx +139 -0
  182. package/docs/webhooks/waiters.mdx +2 -2
  183. package/docs/webhooks/webhook-tasks.mdx +46 -0
  184. package/package.json +4 -5
  185. package/docs/migrating-from-v3.mdx +0 -332
  186. package/docs/webhooks/public-beta.mdx +0 -70
  187. 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
@@ -63,6 +63,15 @@ function toModelMessages(messages, context) {
63
63
  }), locals.get(chatSteeringInjectionsKey) ?? new Map(), context ?? locals.get(chatCurrentUIMessagesKey) ?? messages, context !== undefined || locals.get(chatCurrentUIMessagesKey) !== undefined);
64
64
  }
65
65
  const chatTurnContextKey = locals.create("chat.turnContext");
66
+ /**
67
+ * The running chat turn's `chatId`, `turn` and `clientData`: the one place every callback event
68
+ * the SDK builds takes them from, so a callback never lacks the `clientData` its siblings get.
69
+ * All three are undefined outside a turn. One locals lookup; the values are not copied.
70
+ */
71
+ function chatEventContext() {
72
+ const ctx = locals.get(chatTurnContextKey);
73
+ return { chatId: ctx?.chatId, turn: ctx?.turn, clientData: ctx?.clientData };
74
+ }
66
75
  /**
67
76
  * Per-run slot holding the Session handle that backs this chat's `.in` /
68
77
  * `.out` channels. Populated at the top of `chatAgent`'s run function from
@@ -2596,6 +2605,21 @@ function managedResponse() {
2596
2605
  }
2597
2606
  return response;
2598
2607
  }
2608
+ /**
2609
+ * The `summarize` that `chatCompact` calls between tool steps for a compaction configured on
2610
+ * `chat.agent`, a session turn or a `MessageAccumulator`: the event is built here once, so no
2611
+ * surface hands the callback less than the others do.
2612
+ * @internal
2613
+ */
2614
+ function innerSummarize(compaction, steps) {
2615
+ return (messages) => compaction.summarize({
2616
+ messages,
2617
+ usage: steps.at(-1)?.usage,
2618
+ source: "inner",
2619
+ stepNumber: steps.length - 1,
2620
+ ...chatEventContext(),
2621
+ });
2622
+ }
2599
2623
  /**
2600
2624
  * Check that no tool calls are in-flight in a step's content.
2601
2625
  * Used before compaction to avoid losing tool state mid-execution.
@@ -2667,15 +2691,9 @@ async function applyPrepareMessages(messages, reason) {
2667
2691
  const hook = locals.get(chatPrepareMessagesKey);
2668
2692
  if (!hook)
2669
2693
  return messages;
2670
- const turnCtx = locals.get(chatTurnContextKey);
2694
+ const { chatId = "", turn = 0, clientData } = chatEventContext();
2671
2695
  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
- });
2696
+ return hook({ messages, reason, chatId, turn, clientData });
2679
2697
  }, {
2680
2698
  attributes: {
2681
2699
  [SemanticInternalAttributes.STYLE_ICON]: "task-hook-onStart",
@@ -2746,9 +2764,10 @@ function getCompactionState() {
2746
2764
  *
2747
2765
  * @example
2748
2766
  * ```ts
2749
- * prepareStep: async ({ messages, steps }) => {
2767
+ * prepareStep: async ({ messages, steps, responseMessages }) => {
2750
2768
  * // your custom logic here...
2751
2769
  * const result = await chat.compact(messages, steps, {
2770
+ * responseMessages, // needed on AI SDK 7 to rebuild the steps after a compaction
2752
2771
  * threshold: 80_000,
2753
2772
  * summarize: async (msgs) => generateText({ model, messages: msgs }).then(r => r.text),
2754
2773
  * });
@@ -2772,7 +2791,10 @@ async function chatCompact(messages, steps, options) {
2772
2791
  type: "rebuilt",
2773
2792
  messages: await applyPrepareMessages([
2774
2793
  { role: "user", content: state.summary },
2775
- ...currentStep.response.messages.slice(state.baseResponseMessageCount),
2794
+ ...generationResponseMessages({
2795
+ steps,
2796
+ responseMessages: options.responseMessages,
2797
+ }).slice(state.baseResponseMessageCount),
2776
2798
  ], "compaction-rebuild"),
2777
2799
  };
2778
2800
  }
@@ -2783,7 +2805,7 @@ async function chatCompact(messages, steps, options) {
2783
2805
  const totalTokens = currentStep.usage.totalTokens;
2784
2806
  const inputTokens = currentStep.usage.inputTokens;
2785
2807
  const outputTokens = currentStep.usage.outputTokens;
2786
- const turnCtx = locals.get(chatTurnContextKey);
2808
+ const turnCtx = chatEventContext();
2787
2809
  const stepNumber = steps.length - 1;
2788
2810
  const shouldTrigger = options.shouldCompact
2789
2811
  ? await options.shouldCompact({
@@ -2795,9 +2817,7 @@ async function chatCompact(messages, steps, options) {
2795
2817
  source: "inner",
2796
2818
  stepNumber,
2797
2819
  steps,
2798
- chatId: turnCtx?.chatId,
2799
- turn: turnCtx?.turn,
2800
- clientData: turnCtx?.clientData,
2820
+ ...turnCtx,
2801
2821
  })
2802
2822
  : totalTokens != null && options.threshold != null && totalTokens > options.threshold;
2803
2823
  if (!shouldTrigger) {
@@ -2825,7 +2845,10 @@ async function chatCompact(messages, steps, options) {
2825
2845
  // Store state in locals for subsequent steps
2826
2846
  locals.set(chatCompactionStateKey, {
2827
2847
  summary,
2828
- baseResponseMessageCount: currentStep.response.messages.length,
2848
+ baseResponseMessageCount: generationResponseMessages({
2849
+ steps,
2850
+ responseMessages: options.responseMessages,
2851
+ }).length,
2829
2852
  baseResponseStepCount: steps.length,
2830
2853
  });
2831
2854
  // Set model-only override — UI messages stay intact for persistence.
@@ -2851,8 +2874,7 @@ async function chatCompact(messages, steps, options) {
2851
2874
  inputTokens,
2852
2875
  outputTokens,
2853
2876
  stepNumber,
2854
- chatId: turnCtx?.chatId,
2855
- turn: turnCtx?.turn,
2877
+ ...turnCtx,
2856
2878
  writer: { write, merge },
2857
2879
  });
2858
2880
  }
@@ -2918,8 +2940,8 @@ async function chatCompact(messages, steps, options) {
2918
2940
  * ```
2919
2941
  */
2920
2942
  function chatCompactionStep(options) {
2921
- return async ({ messages, steps }) => {
2922
- const result = await chatCompact(messages, steps, options);
2943
+ return async ({ messages, steps, responseMessages }) => {
2944
+ const result = await chatCompact(messages, steps, { ...options, responseMessages });
2923
2945
  return result.type === "skipped" ? undefined : result;
2924
2946
  };
2925
2947
  }
@@ -3107,6 +3129,7 @@ async function drainSteeringQueue(config, messages, steps, queueOverride) {
3107
3129
  injectedModelMessages: injected,
3108
3130
  chatId: ctx?.chatId ?? "",
3109
3131
  turn: ctx?.turn ?? 0,
3132
+ clientData: ctx?.clientData,
3110
3133
  stepNumber,
3111
3134
  });
3112
3135
  }
@@ -3626,25 +3649,14 @@ function toStreamTextOptions(options, retain = true) {
3626
3649
  const taskCompaction = locals.get(chatAgentCompactionKey);
3627
3650
  const taskPendingMessages = locals.get(chatPendingMessagesKey);
3628
3651
  {
3629
- result.prepareStep = async ({ messages, steps, }) => {
3652
+ result.prepareStep = async ({ messages, steps, responseMessages, }) => {
3630
3653
  let resultMessages;
3631
3654
  // 1. Compaction
3632
3655
  if (taskCompaction) {
3633
3656
  const compactResult = await chatCompact(messages, steps, {
3657
+ responseMessages,
3634
3658
  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
- },
3659
+ summarize: innerSummarize(taskCompaction, steps),
3648
3660
  });
3649
3661
  if (compactResult.type !== "skipped") {
3650
3662
  resultMessages = compactResult.messages;
@@ -4766,28 +4778,46 @@ function makeChannelStreamTap(editor, options = {}) {
4766
4778
  }
4767
4779
  export { makeChannelStreamEditor as __makeChannelStreamEditorForTests, makeChannelStreamTap as __makeChannelStreamTapForTests, makeChannelProgressObserver as __makeChannelProgressObserverForTests, makeChannelLifecycleReporter as __makeChannelLifecycleReporterForTests, };
4768
4780
  /**
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();
4781
+ * Per-chat latches for work the run loop does AFTER it writes `trigger:turn-complete`. The loop
4782
+ * opens a latch before writing the chunk and settles it once that work has finished, so a caller
4783
+ * that saw the chunk can wait for it instead of guessing with timers. One latch per chat at a
4784
+ * time (opening an open one keeps it); a chat with no open latch is quiet.
4785
+ */
4786
+ function createChatLatches() {
4787
+ const latches = new Map();
4788
+ return {
4789
+ open(chatId) {
4790
+ if (latches.has(chatId))
4791
+ return;
4792
+ let settle;
4793
+ const promise = new Promise((resolve) => (settle = resolve));
4794
+ latches.set(chatId, { promise, settle });
4795
+ },
4796
+ /** `"run-ended"` marks a settle made as the run exits, so a waiter knows the run itself is over. */
4797
+ settle(chatId, reason = "settled") {
4798
+ const latch = latches.get(chatId);
4799
+ if (!latch)
4800
+ return;
4801
+ latches.delete(chatId);
4802
+ latch.settle(reason);
4803
+ },
4804
+ /** Why the latch settled; `undefined` when nothing is open. */
4805
+ async wait(chatId) {
4806
+ return latches.get(chatId)?.promise;
4807
+ },
4808
+ };
4787
4809
  }
4810
+ /** The channel egress after the chunk: the final `send()` and the done/error reactions. */
4811
+ const channelEgressLatches = createChatLatches();
4812
+ /** The post-turn hooks after the chunk: `onTurnComplete` and the turn-complete transcript save. */
4813
+ const postTurnHookLatches = createChatLatches();
4788
4814
  /** Resolves once the post-turn channel egress for `chatId` has finished (immediately when none is running). */
4789
4815
  export async function __waitForChannelEgressForTests(chatId) {
4790
- await channelEgressLatches.get(chatId)?.promise;
4816
+ return channelEgressLatches.wait(chatId);
4817
+ }
4818
+ /** Resolves once the post-turn hooks for `chatId` have finished (immediately when none are running). */
4819
+ export async function __waitForPostTurnHooksForTests(chatId) {
4820
+ return postTurnHookLatches.wait(chatId);
4791
4821
  }
4792
4822
  function chatCustomAgent(options) {
4793
4823
  const { clientDataSchema, onClientDataValidationError, clientDataReportErrorAt, run: userRun, ...restOptions } = options;
@@ -6690,6 +6720,7 @@ function chatAgent(options) {
6690
6720
  message: lastUIMessage,
6691
6721
  chatId: currentWirePayload.chatId,
6692
6722
  turn,
6723
+ clientData: chatEventContext().clientData,
6693
6724
  });
6694
6725
  }
6695
6726
  catch {
@@ -6905,6 +6936,7 @@ function chatAgent(options) {
6905
6936
  messages: cleanedUIMessages,
6906
6937
  chatId: currentWirePayload.chatId,
6907
6938
  turn,
6939
+ clientData: chatEventContext().clientData,
6908
6940
  trigger: currentWirePayload.trigger,
6909
6941
  });
6910
6942
  }, {
@@ -7854,9 +7886,7 @@ function chatAgent(options) {
7854
7886
  usage: contextUsage,
7855
7887
  turnUsage,
7856
7888
  totalUsage: cumulativeUsage,
7857
- chatId: currentWirePayload.chatId,
7858
- turn,
7859
- clientData,
7889
+ ...chatEventContext(),
7860
7890
  source: "outer",
7861
7891
  });
7862
7892
  if (shouldTrigger) {
@@ -7876,9 +7906,7 @@ function chatAgent(options) {
7876
7906
  messages: accumulatedMessages,
7877
7907
  usage: turnUsage,
7878
7908
  totalUsage: cumulativeUsage,
7879
- chatId: currentWirePayload.chatId,
7880
- turn,
7881
- clientData,
7909
+ ...chatEventContext(),
7882
7910
  source: "outer",
7883
7911
  });
7884
7912
  // Apply compactModelMessages/compactUIMessages callbacks, or defaults.
@@ -7924,8 +7952,7 @@ function chatAgent(options) {
7924
7952
  inputTokens: turnUsage.inputTokens,
7925
7953
  outputTokens: turnUsage.outputTokens,
7926
7954
  stepNumber: -1, // outer loop, not a step
7927
- chatId: currentWirePayload.chatId,
7928
- turn,
7955
+ ...chatEventContext(),
7929
7956
  writer: { write, merge },
7930
7957
  });
7931
7958
  }
@@ -8044,7 +8071,8 @@ function chatAgent(options) {
8044
8071
  }
8045
8072
  }
8046
8073
  settleRecoveredTurn(currentWirePayload);
8047
- openChannelEgressLatch(currentWirePayload.chatId);
8074
+ channelEgressLatches.open(currentWirePayload.chatId);
8075
+ postTurnHookLatches.open(currentWirePayload.chatId);
8048
8076
  // Write turn-complete control chunk — closes the frontend stream.
8049
8077
  const turnCompleteResult = await writeTurnCompleteChunk(currentWirePayload.chatId, turnAccessToken);
8050
8078
  // Tool calls that have not had controls posted yet; a resume with the rest still
@@ -8243,7 +8271,7 @@ function chatAgent(options) {
8243
8271
  });
8244
8272
  }
8245
8273
  }
8246
- settleChannelEgressLatch(currentWirePayload.chatId);
8274
+ channelEgressLatches.settle(currentWirePayload.chatId);
8247
8275
  // Fire onTurnComplete — stream is closed, use for persistence.
8248
8276
  if (onTurnComplete) {
8249
8277
  await tracer.startActiveSpan("onTurnComplete()", async () => {
@@ -8342,6 +8370,7 @@ function chatAgent(options) {
8342
8370
  });
8343
8371
  }
8344
8372
  }
8373
+ postTurnHookLatches.settle(currentWirePayload.chatId);
8345
8374
  } // end if (!isAction)
8346
8375
  // NOTE: We intentionally do NOT await deferred work from onTurnComplete here.
8347
8376
  // Promises deferred in onTurnComplete (e.g. background self-review via
@@ -8455,12 +8484,13 @@ function chatAgent(options) {
8455
8484
  if (turnError instanceof Error &&
8456
8485
  turnError.name === "AbortError" &&
8457
8486
  runSignal.aborted) {
8458
- settleChannelEgressLatch(currentWirePayload.chatId);
8487
+ channelEgressLatches.settle(currentWirePayload.chatId);
8488
+ postTurnHookLatches.settle(currentWirePayload.chatId);
8459
8489
  channelLifecycle.dispose();
8460
8490
  // Full run cancellation — exit immediately
8461
8491
  throw turnError;
8462
8492
  }
8463
- settleChannelEgressLatch(currentWirePayload.chatId);
8493
+ channelEgressLatches.settle(currentWirePayload.chatId);
8464
8494
  // OOM errors must escape the turn loop so the task runtime can
8465
8495
  // honor `retry.outOfMemory.machine` (set on chat.agent via
8466
8496
  // `oomMachine`). Catching them here would keep the dead worker
@@ -8581,6 +8611,7 @@ function chatAgent(options) {
8581
8611
  writer.write({ type: "error", errorText });
8582
8612
  });
8583
8613
  // Signal turn complete so the client knows this turn is done
8614
+ postTurnHookLatches.open(currentWirePayload.chatId);
8584
8615
  errorTurnCompleteResult = await writeTurnCompleteChunk(currentWirePayload.chatId);
8585
8616
  // A later action's snapshot reuses this cursor, so it has to move
8586
8617
  // here too or that snapshot resumes from before the failed turn.
@@ -8781,6 +8812,7 @@ function chatAgent(options) {
8781
8812
  });
8782
8813
  }
8783
8814
  }
8815
+ postTurnHookLatches.settle(currentWirePayload.chatId);
8784
8816
  if (locals.get(chatCloseRequestedKey)) {
8785
8817
  await performChatClose();
8786
8818
  return;
@@ -8836,6 +8868,12 @@ function chatAgent(options) {
8836
8868
  // reaching one of the loop's close checks (a turn timeout, an OOM
8837
8869
  // re-throw). `performChatClose` is idempotent, so the ordinary path
8838
8870
  // having already run it costs nothing here.
8871
+ // Any exit that skips a turn's own settle (an OOM re-throw, a throw from the turn's
8872
+ // error handler) leaves its post-turn latches open, and the module-level entries would
8873
+ // outlive this run and block the next one for the same chat. Settling is a no-op when
8874
+ // nothing is open, and runs first so a throwing close cannot skip it.
8875
+ channelEgressLatches.settle(currentWirePayload.chatId, "run-ended");
8876
+ postTurnHookLatches.settle(currentWirePayload.chatId, "run-ended");
8839
8877
  await performChatClose();
8840
8878
  // `stopSub` is registered post-preload so the close-during-preload
8841
8879
  // early-return path may exit before it ever attached. Guard the
@@ -10112,13 +10150,14 @@ class ChatMessageAccumulator {
10112
10150
  const comp = this._compaction;
10113
10151
  const pm = this._pendingMessages;
10114
10152
  const queue = this._steeringQueue;
10115
- return retainStepMessages(async ({ messages, steps }) => {
10153
+ return retainStepMessages(async ({ messages, steps, responseMessages, }) => {
10116
10154
  let resultMessages;
10117
10155
  // 1. Compaction
10118
10156
  if (comp) {
10119
10157
  const result = await chatCompact(messages, steps, {
10158
+ responseMessages,
10120
10159
  shouldCompact: comp.shouldCompact,
10121
- summarize: (msgs) => comp.summarize({ messages: msgs, source: "inner" }),
10160
+ summarize: innerSummarize(comp, steps),
10122
10161
  });
10123
10162
  if (result.type !== "skipped") {
10124
10163
  resultMessages = result.messages;
@@ -10147,6 +10186,13 @@ class ChatMessageAccumulator {
10147
10186
  async compactIfNeeded(usage, context) {
10148
10187
  if (!this._compaction || !usage)
10149
10188
  return false;
10189
+ // An explicit context wins; inside a chat turn the turn's own fills in what it leaves out.
10190
+ const turnContext = chatEventContext();
10191
+ const eventContext = {
10192
+ chatId: context?.chatId ?? turnContext.chatId,
10193
+ turn: context?.turn ?? turnContext.turn,
10194
+ clientData: context?.clientData ?? turnContext.clientData,
10195
+ };
10150
10196
  const shouldTrigger = await this._compaction.shouldCompact({
10151
10197
  messages: this.modelMessages,
10152
10198
  totalTokens: usage.totalTokens,
@@ -10155,9 +10201,7 @@ class ChatMessageAccumulator {
10155
10201
  usage,
10156
10202
  turnUsage: context?.turnUsage,
10157
10203
  totalUsage: context?.totalUsage,
10158
- chatId: context?.chatId,
10159
- turn: context?.turn,
10160
- clientData: context?.clientData,
10204
+ ...eventContext,
10161
10205
  source: "outer",
10162
10206
  });
10163
10207
  if (!shouldTrigger)
@@ -10166,18 +10210,16 @@ class ChatMessageAccumulator {
10166
10210
  messages: this.modelMessages,
10167
10211
  usage,
10168
10212
  totalUsage: context?.totalUsage,
10169
- chatId: context?.chatId,
10170
- turn: context?.turn,
10171
- clientData: context?.clientData,
10213
+ ...eventContext,
10172
10214
  source: "outer",
10173
10215
  });
10174
10216
  const compactEvent = {
10175
10217
  summary,
10176
10218
  uiMessages: this.uiMessages,
10177
10219
  modelMessages: this.modelMessages,
10178
- chatId: context?.chatId ?? "",
10179
- turn: context?.turn ?? 0,
10180
- clientData: context?.clientData,
10220
+ chatId: eventContext.chatId ?? "",
10221
+ turn: eventContext.turn ?? 0,
10222
+ clientData: eventContext.clientData,
10181
10223
  source: "outer",
10182
10224
  };
10183
10225
  this.modelMessages = this._compaction.compactModelMessages
@@ -10475,6 +10517,7 @@ function createChatSession(payload, options) {
10475
10517
  message: lastUIMessage,
10476
10518
  chatId: currentPayload.chatId,
10477
10519
  turn,
10520
+ clientData: chatEventContext().clientData,
10478
10521
  });
10479
10522
  }
10480
10523
  catch {
@@ -10654,9 +10697,7 @@ function createChatSession(payload, options) {
10654
10697
  usage: contextUsage,
10655
10698
  turnUsage,
10656
10699
  totalUsage: cumulativeUsage,
10657
- chatId: currentPayload.chatId,
10658
- turn,
10659
- clientData: currentPayload.metadata,
10700
+ ...chatEventContext(),
10660
10701
  source: "outer",
10661
10702
  });
10662
10703
  if (shouldTrigger) {
@@ -10664,9 +10705,7 @@ function createChatSession(payload, options) {
10664
10705
  messages: accumulator.modelMessages,
10665
10706
  usage: turnUsage,
10666
10707
  totalUsage: cumulativeUsage,
10667
- chatId: currentPayload.chatId,
10668
- turn,
10669
- clientData: currentPayload.metadata,
10708
+ ...chatEventContext(),
10670
10709
  source: "outer",
10671
10710
  });
10672
10711
  const compactEvent = {
@@ -10710,12 +10749,13 @@ function createChatSession(payload, options) {
10710
10749
  const hasPending = !!sessionPendingMessages;
10711
10750
  if (!hasCompaction && !hasPending)
10712
10751
  return undefined;
10713
- return retainStepMessages(async ({ messages: stepMsgs, steps, }) => {
10752
+ return retainStepMessages(async ({ messages: stepMsgs, steps, responseMessages, }) => {
10714
10753
  let resultMessages;
10715
10754
  if (sessionCompaction) {
10716
10755
  const compactResult = await chatCompact(stepMsgs, steps, {
10756
+ responseMessages,
10717
10757
  shouldCompact: sessionCompaction.shouldCompact,
10718
- summarize: (msgs) => sessionCompaction.summarize({ messages: msgs, source: "inner" }),
10758
+ summarize: innerSummarize(sessionCompaction, steps),
10719
10759
  });
10720
10760
  if (compactResult.type !== "skipped") {
10721
10761
  resultMessages = compactResult.messages;
@@ -11111,7 +11151,7 @@ function overrideRequestHeaders(authHeaders) {
11111
11151
  async function callSessionsCreateWithOverride(args) {
11112
11152
  const apiClient = apiClientManager.client;
11113
11153
  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.");
11154
+ throw new Error("chat.createStartSessionAction: no API access token configured. Call configure({ accessToken }) or pass apiClient: { accessToken } to createStartSessionAction before invoking the action.");
11115
11155
  }
11116
11156
  const authHeaders = apiClient.getAuthenticationHeaders("createSession");
11117
11157
  const ctx = { endpoint: "sessions", chatId: args.chatId };
@@ -11135,55 +11175,60 @@ async function callSessionsCreateWithOverride(args) {
11135
11175
  async function mintPublicTokenWithOverride(args) {
11136
11176
  const apiClient = apiClientManager.client;
11137
11177
  if (!apiClient) {
11138
- throw new Error("chat.createStartSessionAction: no API access token configured for JWT mint.");
11178
+ throw new Error("chat.createStartSessionAction: no API access token configured for public-token minting. Call configure({ accessToken }) or pass apiClient: { accessToken } to createStartSessionAction.");
11139
11179
  }
11140
11180
  const authHeaders = apiClient.getAuthenticationHeaders("createPublicToken");
11141
- const accessToken = authHeaders.Authorization.slice("Bearer ".length);
11142
11181
  const ctx = { endpoint: "auth", chatId: args.chatId };
11143
11182
  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}`;
11183
+ const baseURL = resolveChatStartBaseURL("auth", args.chatId, args.baseURLOption);
11184
+ const url = `${baseURL}/api/v1/auth/public-tokens`;
11147
11185
  const init = {
11148
11186
  method: "POST",
11149
11187
  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
- : {}),
11188
+ body: JSON.stringify({
11189
+ scopes,
11190
+ expirationTime: args.expirationTime instanceof Date
11191
+ ? Math.floor(args.expirationTime.getTime() / 1000)
11192
+ : args.expirationTime,
11193
+ }),
11160
11194
  };
11161
11195
  const response = args.fetchOverride
11162
11196
  ? await args.fetchOverride(url, init, ctx)
11163
11197
  : await fetch(url, init);
11164
11198
  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.`);
11199
+ const accessToken = authHeaders.Authorization.slice("Bearer ".length);
11200
+ if (canFallbackToLocalPublicToken(accessToken, scopes, response.status)) {
11201
+ await response.arrayBuffer();
11202
+ const claimsURL = `${baseURL}/api/v1/auth/jwt/claims`;
11203
+ const claimsInit = {
11204
+ method: "POST",
11205
+ headers: overrideRequestHeaders(authHeaders),
11206
+ };
11207
+ const claimsResponse = args.fetchOverride
11208
+ ? await args.fetchOverride(claimsURL, claimsInit, ctx)
11209
+ : await fetch(claimsURL, claimsInit);
11210
+ if (!claimsResponse.ok) {
11211
+ const text = await claimsResponse.text().catch(() => "");
11212
+ throw new Error(`auth.generateJWTClaims failed: ${claimsResponse.status} ${text}`);
11213
+ }
11214
+ const claims = (await claimsResponse.json());
11215
+ return generateJWT({
11216
+ secretKey: accessToken,
11217
+ payload: { ...claims, scopes },
11218
+ expirationTime: args.expirationTime,
11219
+ });
11220
+ }
11221
+ if (response.status === 404) {
11222
+ throw new Error("The server does not support public-token minting for this credential. Upgrade your self-hosted server.");
11168
11223
  }
11169
11224
  const text = await response.text().catch(() => "");
11170
11225
  throw new Error(`auth.createPublicToken failed: ${response.status} ${text}`);
11171
11226
  }
11172
11227
  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;
11228
+ if (typeof responseBody.token !== "string") {
11229
+ throw new Error("auth.createPublicToken failed: server response did not include a token");
11178
11230
  }
11179
- return generateJWT({
11180
- secretKey: accessToken,
11181
- payload: {
11182
- ...responseBody,
11183
- scopes,
11184
- },
11185
- expirationTime: args.expirationTime,
11186
- });
11231
+ return responseBody.token;
11187
11232
  }
11188
11233
  /**
11189
11234
  * Creates a server-side helper that reads a conversation from a transcript