@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.
- package/dist/commonjs/v3/ai.d.ts +29 -5
- package/dist/commonjs/v3/ai.js +160 -114
- package/dist/commonjs/v3/ai.js.map +1 -1
- package/dist/commonjs/v3/auth.d.ts +4 -4
- package/dist/commonjs/v3/auth.js +21 -75
- package/dist/commonjs/v3/auth.js.map +1 -1
- package/dist/commonjs/v3/chat-client.d.ts +1 -1
- package/dist/commonjs/v3/chat.d.ts +1 -1
- package/dist/commonjs/v3/prompt.js +6 -8
- package/dist/commonjs/v3/prompt.js.map +1 -1
- package/dist/commonjs/v3/runs.d.ts +5 -1
- package/dist/commonjs/v3/runs.js.map +1 -1
- package/dist/commonjs/v3/sessions.js +10 -7
- package/dist/commonjs/v3/sessions.js.map +1 -1
- package/dist/commonjs/v3/shared.d.ts +2 -2
- package/dist/commonjs/v3/shared.js +16 -15
- package/dist/commonjs/v3/shared.js.map +1 -1
- package/dist/commonjs/v3/steeringContext.d.ts +35 -10
- package/dist/commonjs/v3/steeringContext.js +35 -12
- package/dist/commonjs/v3/steeringContext.js.map +1 -1
- package/dist/commonjs/v3/streams.js +10 -8
- package/dist/commonjs/v3/streams.js.map +1 -1
- package/dist/commonjs/v3/tasks.d.ts +2 -2
- package/dist/commonjs/v3/tasks.js.map +1 -1
- package/dist/commonjs/v3/test/mock-chat-agent.js +24 -13
- package/dist/commonjs/v3/test/mock-chat-agent.js.map +1 -1
- package/dist/commonjs/v3/webhooks.d.ts +8 -2
- package/dist/commonjs/v3/webhooks.js +20 -11
- package/dist/commonjs/v3/webhooks.js.map +1 -1
- package/dist/commonjs/version.js +1 -1
- package/dist/esm/v3/ai.d.ts +29 -5
- package/dist/esm/v3/ai.js +162 -117
- package/dist/esm/v3/ai.js.map +1 -1
- package/dist/esm/v3/auth.d.ts +4 -4
- package/dist/esm/v3/auth.js +22 -76
- package/dist/esm/v3/auth.js.map +1 -1
- package/dist/esm/v3/chat-client.d.ts +1 -1
- package/dist/esm/v3/chat.d.ts +1 -1
- package/dist/esm/v3/prompt.js +6 -8
- package/dist/esm/v3/prompt.js.map +1 -1
- package/dist/esm/v3/runs.d.ts +5 -1
- package/dist/esm/v3/runs.js.map +1 -1
- package/dist/esm/v3/sessions.js +10 -7
- package/dist/esm/v3/sessions.js.map +1 -1
- package/dist/esm/v3/shared.d.ts +2 -2
- package/dist/esm/v3/shared.js +16 -15
- package/dist/esm/v3/shared.js.map +1 -1
- package/dist/esm/v3/steeringContext.d.ts +35 -10
- package/dist/esm/v3/steeringContext.js +34 -12
- package/dist/esm/v3/steeringContext.js.map +1 -1
- package/dist/esm/v3/streams.js +10 -8
- package/dist/esm/v3/streams.js.map +1 -1
- package/dist/esm/v3/tasks.d.ts +2 -2
- package/dist/esm/v3/tasks.js.map +1 -1
- package/dist/esm/v3/test/mock-chat-agent.js +25 -14
- package/dist/esm/v3/test/mock-chat-agent.js.map +1 -1
- package/dist/esm/v3/webhooks.d.ts +8 -2
- package/dist/esm/v3/webhooks.js +20 -11
- package/dist/esm/v3/webhooks.js.map +1 -1
- package/dist/esm/version.js +1 -1
- package/docs/ai/observability.mdx +25 -19
- package/docs/ai/prompts.mdx +25 -19
- package/docs/ai-chat/actions.mdx +1 -1
- package/docs/ai-chat/anatomy.mdx +1 -1
- package/docs/ai-chat/backend.mdx +24 -24
- package/docs/ai-chat/background-injection.mdx +5 -5
- package/docs/ai-chat/chat-local.mdx +2 -2
- package/docs/ai-chat/compaction.mdx +38 -15
- package/docs/ai-chat/custom-agents.mdx +4 -4
- package/docs/ai-chat/error-handling.mdx +4 -4
- package/docs/ai-chat/fast-starts.mdx +8 -7
- package/docs/ai-chat/frontend.mdx +6 -2
- package/docs/ai-chat/lifecycle-hooks.mdx +11 -11
- package/docs/ai-chat/migrating-from-a-route-handler.mdx +4 -4
- package/docs/ai-chat/migrating-from-hydrate-messages.mdx +1 -1
- package/docs/ai-chat/overview.mdx +1 -1
- package/docs/ai-chat/patterns/branching-conversations.mdx +1 -1
- package/docs/ai-chat/patterns/database-persistence.mdx +2 -2
- package/docs/ai-chat/patterns/human-in-the-loop.mdx +1 -1
- package/docs/ai-chat/patterns/native-compaction.mdx +29 -9
- package/docs/ai-chat/patterns/persistence-and-replay.mdx +1 -1
- package/docs/ai-chat/patterns/redacting-telemetry.mdx +2 -2
- package/docs/ai-chat/patterns/skills.mdx +2 -2
- package/docs/ai-chat/patterns/sub-agents.mdx +1 -1
- package/docs/ai-chat/patterns/tool-result-auditing.mdx +1 -1
- package/docs/ai-chat/patterns/trusted-edge-signals.mdx +2 -2
- package/docs/ai-chat/patterns/version-upgrades.mdx +2 -2
- package/docs/ai-chat/pending-messages.mdx +5 -5
- package/docs/ai-chat/prompt-caching.mdx +7 -7
- package/docs/ai-chat/quick-start.mdx +2 -2
- package/docs/ai-chat/testing.mdx +9 -9
- package/docs/ai-chat/tools.mdx +9 -5
- package/docs/ai-chat/transcript-storage.mdx +2 -2
- package/docs/ai-chat/types.mdx +10 -3
- package/docs/apikeys.mdx +2 -1
- package/docs/cli-login-commands.mdx +12 -0
- package/docs/concurrency.mdx +40 -0
- package/docs/database-connections.mdx +1 -1
- package/docs/deploy-environment-variables.mdx +84 -2
- package/docs/deployment/trusted-publishing.mdx +4 -0
- package/docs/errors-retrying.mdx +25 -18
- package/docs/github-actions.mdx +1 -1
- package/docs/github-integration.mdx +21 -0
- package/docs/how-it-works.mdx +4 -2
- package/docs/introduction.mdx +1 -1
- package/docs/mcp-introduction.mdx +7 -1
- package/docs/mcp-remote.mdx +90 -0
- package/docs/oidc/azure.mdx +81 -0
- package/docs/oidc/databricks.mdx +99 -0
- package/docs/oidc/doppler.mdx +60 -0
- package/docs/oidc/gcp.mdx +109 -0
- package/docs/oidc/hugging-face.mdx +90 -0
- package/docs/oidc/openrouter.mdx +91 -0
- package/docs/oidc/snowflake.mdx +91 -0
- package/docs/oidc/vault.mdx +71 -0
- package/docs/oidc.mdx +2 -0
- package/docs/runs/metadata.mdx +1 -1
- package/docs/self-hosting/docker.mdx +2 -0
- package/docs/self-hosting/env/webapp.mdx +4 -1
- package/docs/self-hosting/kubernetes.mdx +2 -0
- package/docs/tasks/schemaTask.mdx +26 -13
- package/docs/tasks/streams.mdx +2 -2
- package/docs/troubleshooting.mdx +7 -0
- package/docs/webhooks/channels.mdx +6 -6
- package/docs/webhooks/connect.mdx +9 -75
- package/docs/webhooks/custom-providers.mdx +87 -0
- package/docs/webhooks/deliveries.mdx +7 -7
- package/docs/webhooks/filters.mdx +2 -2
- package/docs/webhooks/how-it-works.mdx +58 -0
- package/docs/webhooks/human-in-the-loop.mdx +3 -3
- package/docs/webhooks/linear.mdx +5 -3
- package/docs/webhooks/overview.mdx +20 -71
- package/docs/webhooks/own-handler.mdx +62 -0
- package/docs/webhooks/pricing-and-limits.mdx +13 -0
- package/docs/webhooks/providers/attio.mdx +38 -0
- package/docs/webhooks/providers/autumn.mdx +40 -0
- package/docs/webhooks/providers/cal-com.mdx +41 -0
- package/docs/webhooks/providers/calendly.mdx +41 -0
- package/docs/webhooks/providers/clerk.mdx +40 -0
- package/docs/webhooks/providers/discord.mdx +45 -0
- package/docs/webhooks/providers/docusign.mdx +41 -0
- package/docs/webhooks/providers/elevenlabs.mdx +41 -0
- package/docs/webhooks/providers/firecrawl.mdx +54 -0
- package/docs/webhooks/providers/github.mdx +46 -0
- package/docs/webhooks/providers/gitlab.mdx +41 -0
- package/docs/webhooks/providers/granola.mdx +44 -0
- package/docs/webhooks/providers/hookdeck.mdx +98 -0
- package/docs/webhooks/providers/infisical.mdx +41 -0
- package/docs/webhooks/providers/jira.mdx +41 -0
- package/docs/webhooks/providers/langfuse.mdx +40 -0
- package/docs/webhooks/providers/linear.mdx +45 -0
- package/docs/webhooks/providers/loops.mdx +40 -0
- package/docs/webhooks/providers/metronome.mdx +42 -0
- package/docs/webhooks/providers/mux.mdx +40 -0
- package/docs/webhooks/providers/openai.mdx +40 -0
- package/docs/webhooks/providers/pagerduty.mdx +40 -0
- package/docs/webhooks/providers/plain.mdx +41 -0
- package/docs/webhooks/providers/planetscale.mdx +40 -0
- package/docs/webhooks/providers/recall-ai.mdx +54 -0
- package/docs/webhooks/providers/replicate.mdx +41 -0
- package/docs/webhooks/providers/resend.mdx +44 -0
- package/docs/webhooks/providers/retell.mdx +41 -0
- package/docs/webhooks/providers/sentry.mdx +41 -0
- package/docs/webhooks/providers/shopify.mdx +49 -0
- package/docs/webhooks/providers/slack.mdx +48 -0
- package/docs/webhooks/providers/square.mdx +40 -0
- package/docs/webhooks/providers/stripe.mdx +86 -0
- package/docs/webhooks/providers/supabase.mdx +41 -0
- package/docs/webhooks/providers/tailscale.mdx +43 -0
- package/docs/webhooks/providers/typeform.mdx +42 -0
- package/docs/webhooks/providers/vapi.mdx +47 -0
- package/docs/webhooks/providers/vercel.mdx +40 -0
- package/docs/webhooks/providers/whatsapp.mdx +45 -0
- package/docs/webhooks/providers/workos.mdx +41 -0
- package/docs/webhooks/providers/zendesk.mdx +41 -0
- package/docs/webhooks/providers.mdx +53 -0
- package/docs/webhooks/quick-start.mdx +131 -0
- package/docs/webhooks/session-routing.mdx +6 -6
- package/docs/webhooks/signing-secrets.mdx +80 -0
- package/docs/webhooks/slack.mdx +11 -2
- package/docs/webhooks/verification.mdx +139 -0
- package/docs/webhooks/waiters.mdx +2 -2
- package/docs/webhooks/webhook-tasks.mdx +46 -0
- package/package.json +4 -5
- package/docs/migrating-from-v3.mdx +0 -332
- package/docs/webhooks/public-beta.mdx +0 -70
- package/docs/webhooks/sources.mdx +0 -252
package/dist/commonjs/v3/ai.js
CHANGED
|
@@ -18,6 +18,7 @@ exports.__makeChannelStreamTapForTests = makeChannelStreamTap;
|
|
|
18
18
|
exports.__makeChannelProgressObserverForTests = makeChannelProgressObserver;
|
|
19
19
|
exports.__makeChannelLifecycleReporterForTests = makeChannelLifecycleReporter;
|
|
20
20
|
exports.__waitForChannelEgressForTests = __waitForChannelEgressForTests;
|
|
21
|
+
exports.__waitForPostTurnHooksForTests = __waitForPostTurnHooksForTests;
|
|
21
22
|
const v3_1 = require("@trigger.dev/core/v3");
|
|
22
23
|
const webhooks_js_1 = require("./webhooks.js");
|
|
23
24
|
const modelFileDownload_js_1 = require("./modelFileDownload.js");
|
|
@@ -85,6 +86,15 @@ function toModelMessages(messages, context) {
|
|
|
85
86
|
}), locals_js_1.locals.get(chatSteeringInjectionsKey) ?? new Map(), context ?? locals_js_1.locals.get(chatCurrentUIMessagesKey) ?? messages, context !== undefined || locals_js_1.locals.get(chatCurrentUIMessagesKey) !== undefined);
|
|
86
87
|
}
|
|
87
88
|
const chatTurnContextKey = locals_js_1.locals.create("chat.turnContext");
|
|
89
|
+
/**
|
|
90
|
+
* The running chat turn's `chatId`, `turn` and `clientData`: the one place every callback event
|
|
91
|
+
* the SDK builds takes them from, so a callback never lacks the `clientData` its siblings get.
|
|
92
|
+
* All three are undefined outside a turn. One locals lookup; the values are not copied.
|
|
93
|
+
*/
|
|
94
|
+
function chatEventContext() {
|
|
95
|
+
const ctx = locals_js_1.locals.get(chatTurnContextKey);
|
|
96
|
+
return { chatId: ctx?.chatId, turn: ctx?.turn, clientData: ctx?.clientData };
|
|
97
|
+
}
|
|
88
98
|
/**
|
|
89
99
|
* Per-run slot holding the Session handle that backs this chat's `.in` /
|
|
90
100
|
* `.out` channels. Populated at the top of `chatAgent`'s run function from
|
|
@@ -2616,6 +2626,21 @@ function managedResponse() {
|
|
|
2616
2626
|
}
|
|
2617
2627
|
return response;
|
|
2618
2628
|
}
|
|
2629
|
+
/**
|
|
2630
|
+
* The `summarize` that `chatCompact` calls between tool steps for a compaction configured on
|
|
2631
|
+
* `chat.agent`, a session turn or a `MessageAccumulator`: the event is built here once, so no
|
|
2632
|
+
* surface hands the callback less than the others do.
|
|
2633
|
+
* @internal
|
|
2634
|
+
*/
|
|
2635
|
+
function innerSummarize(compaction, steps) {
|
|
2636
|
+
return (messages) => compaction.summarize({
|
|
2637
|
+
messages,
|
|
2638
|
+
usage: steps.at(-1)?.usage,
|
|
2639
|
+
source: "inner",
|
|
2640
|
+
stepNumber: steps.length - 1,
|
|
2641
|
+
...chatEventContext(),
|
|
2642
|
+
});
|
|
2643
|
+
}
|
|
2619
2644
|
/**
|
|
2620
2645
|
* Check that no tool calls are in-flight in a step's content.
|
|
2621
2646
|
* Used before compaction to avoid losing tool state mid-execution.
|
|
@@ -2687,15 +2712,9 @@ async function applyPrepareMessages(messages, reason) {
|
|
|
2687
2712
|
const hook = locals_js_1.locals.get(chatPrepareMessagesKey);
|
|
2688
2713
|
if (!hook)
|
|
2689
2714
|
return messages;
|
|
2690
|
-
const
|
|
2715
|
+
const { chatId = "", turn = 0, clientData } = chatEventContext();
|
|
2691
2716
|
const prepared = await tracer_js_1.tracer.startActiveSpan("prepareMessages()", async () => {
|
|
2692
|
-
return hook({
|
|
2693
|
-
messages,
|
|
2694
|
-
reason,
|
|
2695
|
-
chatId: turnCtx?.chatId ?? "",
|
|
2696
|
-
turn: turnCtx?.turn ?? 0,
|
|
2697
|
-
clientData: turnCtx?.clientData,
|
|
2698
|
-
});
|
|
2717
|
+
return hook({ messages, reason, chatId, turn, clientData });
|
|
2699
2718
|
}, {
|
|
2700
2719
|
attributes: {
|
|
2701
2720
|
[v3_1.SemanticInternalAttributes.STYLE_ICON]: "task-hook-onStart",
|
|
@@ -2766,9 +2785,10 @@ function getCompactionState() {
|
|
|
2766
2785
|
*
|
|
2767
2786
|
* @example
|
|
2768
2787
|
* ```ts
|
|
2769
|
-
* prepareStep: async ({ messages, steps }) => {
|
|
2788
|
+
* prepareStep: async ({ messages, steps, responseMessages }) => {
|
|
2770
2789
|
* // your custom logic here...
|
|
2771
2790
|
* const result = await chat.compact(messages, steps, {
|
|
2791
|
+
* responseMessages, // needed on AI SDK 7 to rebuild the steps after a compaction
|
|
2772
2792
|
* threshold: 80_000,
|
|
2773
2793
|
* summarize: async (msgs) => generateText({ model, messages: msgs }).then(r => r.text),
|
|
2774
2794
|
* });
|
|
@@ -2792,7 +2812,10 @@ async function chatCompact(messages, steps, options) {
|
|
|
2792
2812
|
type: "rebuilt",
|
|
2793
2813
|
messages: await applyPrepareMessages([
|
|
2794
2814
|
{ role: "user", content: state.summary },
|
|
2795
|
-
...
|
|
2815
|
+
...(0, steeringContext_js_1.generationResponseMessages)({
|
|
2816
|
+
steps,
|
|
2817
|
+
responseMessages: options.responseMessages,
|
|
2818
|
+
}).slice(state.baseResponseMessageCount),
|
|
2796
2819
|
], "compaction-rebuild"),
|
|
2797
2820
|
};
|
|
2798
2821
|
}
|
|
@@ -2803,7 +2826,7 @@ async function chatCompact(messages, steps, options) {
|
|
|
2803
2826
|
const totalTokens = currentStep.usage.totalTokens;
|
|
2804
2827
|
const inputTokens = currentStep.usage.inputTokens;
|
|
2805
2828
|
const outputTokens = currentStep.usage.outputTokens;
|
|
2806
|
-
const turnCtx =
|
|
2829
|
+
const turnCtx = chatEventContext();
|
|
2807
2830
|
const stepNumber = steps.length - 1;
|
|
2808
2831
|
const shouldTrigger = options.shouldCompact
|
|
2809
2832
|
? await options.shouldCompact({
|
|
@@ -2815,9 +2838,7 @@ async function chatCompact(messages, steps, options) {
|
|
|
2815
2838
|
source: "inner",
|
|
2816
2839
|
stepNumber,
|
|
2817
2840
|
steps,
|
|
2818
|
-
|
|
2819
|
-
turn: turnCtx?.turn,
|
|
2820
|
-
clientData: turnCtx?.clientData,
|
|
2841
|
+
...turnCtx,
|
|
2821
2842
|
})
|
|
2822
2843
|
: totalTokens != null && options.threshold != null && totalTokens > options.threshold;
|
|
2823
2844
|
if (!shouldTrigger) {
|
|
@@ -2845,7 +2866,10 @@ async function chatCompact(messages, steps, options) {
|
|
|
2845
2866
|
// Store state in locals for subsequent steps
|
|
2846
2867
|
locals_js_1.locals.set(chatCompactionStateKey, {
|
|
2847
2868
|
summary,
|
|
2848
|
-
baseResponseMessageCount:
|
|
2869
|
+
baseResponseMessageCount: (0, steeringContext_js_1.generationResponseMessages)({
|
|
2870
|
+
steps,
|
|
2871
|
+
responseMessages: options.responseMessages,
|
|
2872
|
+
}).length,
|
|
2849
2873
|
baseResponseStepCount: steps.length,
|
|
2850
2874
|
});
|
|
2851
2875
|
// Set model-only override — UI messages stay intact for persistence.
|
|
@@ -2871,8 +2895,7 @@ async function chatCompact(messages, steps, options) {
|
|
|
2871
2895
|
inputTokens,
|
|
2872
2896
|
outputTokens,
|
|
2873
2897
|
stepNumber,
|
|
2874
|
-
|
|
2875
|
-
turn: turnCtx?.turn,
|
|
2898
|
+
...turnCtx,
|
|
2876
2899
|
writer: { write, merge },
|
|
2877
2900
|
});
|
|
2878
2901
|
}
|
|
@@ -2938,8 +2961,8 @@ async function chatCompact(messages, steps, options) {
|
|
|
2938
2961
|
* ```
|
|
2939
2962
|
*/
|
|
2940
2963
|
function chatCompactionStep(options) {
|
|
2941
|
-
return async ({ messages, steps }) => {
|
|
2942
|
-
const result = await chatCompact(messages, steps, options);
|
|
2964
|
+
return async ({ messages, steps, responseMessages }) => {
|
|
2965
|
+
const result = await chatCompact(messages, steps, { ...options, responseMessages });
|
|
2943
2966
|
return result.type === "skipped" ? undefined : result;
|
|
2944
2967
|
};
|
|
2945
2968
|
}
|
|
@@ -3127,6 +3150,7 @@ async function drainSteeringQueue(config, messages, steps, queueOverride) {
|
|
|
3127
3150
|
injectedModelMessages: injected,
|
|
3128
3151
|
chatId: ctx?.chatId ?? "",
|
|
3129
3152
|
turn: ctx?.turn ?? 0,
|
|
3153
|
+
clientData: ctx?.clientData,
|
|
3130
3154
|
stepNumber,
|
|
3131
3155
|
});
|
|
3132
3156
|
}
|
|
@@ -3646,25 +3670,14 @@ function toStreamTextOptions(options, retain = true) {
|
|
|
3646
3670
|
const taskCompaction = locals_js_1.locals.get(chatAgentCompactionKey);
|
|
3647
3671
|
const taskPendingMessages = locals_js_1.locals.get(chatPendingMessagesKey);
|
|
3648
3672
|
{
|
|
3649
|
-
result.prepareStep = async ({ messages, steps, }) => {
|
|
3673
|
+
result.prepareStep = async ({ messages, steps, responseMessages, }) => {
|
|
3650
3674
|
let resultMessages;
|
|
3651
3675
|
// 1. Compaction
|
|
3652
3676
|
if (taskCompaction) {
|
|
3653
3677
|
const compactResult = await chatCompact(messages, steps, {
|
|
3678
|
+
responseMessages,
|
|
3654
3679
|
shouldCompact: taskCompaction.shouldCompact,
|
|
3655
|
-
summarize: (
|
|
3656
|
-
const ctx = locals_js_1.locals.get(chatTurnContextKey);
|
|
3657
|
-
const lastStep = steps.at(-1);
|
|
3658
|
-
return taskCompaction.summarize({
|
|
3659
|
-
messages: msgs,
|
|
3660
|
-
usage: lastStep?.usage,
|
|
3661
|
-
source: "inner",
|
|
3662
|
-
stepNumber: steps.length - 1,
|
|
3663
|
-
chatId: ctx?.chatId,
|
|
3664
|
-
turn: ctx?.turn,
|
|
3665
|
-
clientData: ctx?.clientData,
|
|
3666
|
-
});
|
|
3667
|
-
},
|
|
3680
|
+
summarize: innerSummarize(taskCompaction, steps),
|
|
3668
3681
|
});
|
|
3669
3682
|
if (compactResult.type !== "skipped") {
|
|
3670
3683
|
resultMessages = compactResult.messages;
|
|
@@ -4785,28 +4798,46 @@ function makeChannelStreamTap(editor, options = {}) {
|
|
|
4785
4798
|
return new TransformStream(transformer);
|
|
4786
4799
|
}
|
|
4787
4800
|
/**
|
|
4788
|
-
* Per-chat
|
|
4789
|
-
*
|
|
4790
|
-
*
|
|
4791
|
-
*
|
|
4792
|
-
* no open latch is quiet.
|
|
4801
|
+
* Per-chat latches for work the run loop does AFTER it writes `trigger:turn-complete`. The loop
|
|
4802
|
+
* opens a latch before writing the chunk and settles it once that work has finished, so a caller
|
|
4803
|
+
* that saw the chunk can wait for it instead of guessing with timers. One latch per chat at a
|
|
4804
|
+
* time (opening an open one keeps it); a chat with no open latch is quiet.
|
|
4793
4805
|
*/
|
|
4794
|
-
|
|
4795
|
-
|
|
4796
|
-
|
|
4797
|
-
|
|
4798
|
-
|
|
4799
|
-
|
|
4800
|
-
|
|
4801
|
-
|
|
4802
|
-
|
|
4803
|
-
|
|
4804
|
-
|
|
4805
|
-
|
|
4806
|
+
function createChatLatches() {
|
|
4807
|
+
const latches = new Map();
|
|
4808
|
+
return {
|
|
4809
|
+
open(chatId) {
|
|
4810
|
+
if (latches.has(chatId))
|
|
4811
|
+
return;
|
|
4812
|
+
let settle;
|
|
4813
|
+
const promise = new Promise((resolve) => (settle = resolve));
|
|
4814
|
+
latches.set(chatId, { promise, settle });
|
|
4815
|
+
},
|
|
4816
|
+
/** `"run-ended"` marks a settle made as the run exits, so a waiter knows the run itself is over. */
|
|
4817
|
+
settle(chatId, reason = "settled") {
|
|
4818
|
+
const latch = latches.get(chatId);
|
|
4819
|
+
if (!latch)
|
|
4820
|
+
return;
|
|
4821
|
+
latches.delete(chatId);
|
|
4822
|
+
latch.settle(reason);
|
|
4823
|
+
},
|
|
4824
|
+
/** Why the latch settled; `undefined` when nothing is open. */
|
|
4825
|
+
async wait(chatId) {
|
|
4826
|
+
return latches.get(chatId)?.promise;
|
|
4827
|
+
},
|
|
4828
|
+
};
|
|
4806
4829
|
}
|
|
4830
|
+
/** The channel egress after the chunk: the final `send()` and the done/error reactions. */
|
|
4831
|
+
const channelEgressLatches = createChatLatches();
|
|
4832
|
+
/** The post-turn hooks after the chunk: `onTurnComplete` and the turn-complete transcript save. */
|
|
4833
|
+
const postTurnHookLatches = createChatLatches();
|
|
4807
4834
|
/** Resolves once the post-turn channel egress for `chatId` has finished (immediately when none is running). */
|
|
4808
4835
|
async function __waitForChannelEgressForTests(chatId) {
|
|
4809
|
-
|
|
4836
|
+
return channelEgressLatches.wait(chatId);
|
|
4837
|
+
}
|
|
4838
|
+
/** Resolves once the post-turn hooks for `chatId` have finished (immediately when none are running). */
|
|
4839
|
+
async function __waitForPostTurnHooksForTests(chatId) {
|
|
4840
|
+
return postTurnHookLatches.wait(chatId);
|
|
4810
4841
|
}
|
|
4811
4842
|
function chatCustomAgent(options) {
|
|
4812
4843
|
const { clientDataSchema, onClientDataValidationError, clientDataReportErrorAt, run: userRun, ...restOptions } = options;
|
|
@@ -6709,6 +6740,7 @@ function chatAgent(options) {
|
|
|
6709
6740
|
message: lastUIMessage,
|
|
6710
6741
|
chatId: currentWirePayload.chatId,
|
|
6711
6742
|
turn,
|
|
6743
|
+
clientData: chatEventContext().clientData,
|
|
6712
6744
|
});
|
|
6713
6745
|
}
|
|
6714
6746
|
catch {
|
|
@@ -6924,6 +6956,7 @@ function chatAgent(options) {
|
|
|
6924
6956
|
messages: cleanedUIMessages,
|
|
6925
6957
|
chatId: currentWirePayload.chatId,
|
|
6926
6958
|
turn,
|
|
6959
|
+
clientData: chatEventContext().clientData,
|
|
6927
6960
|
trigger: currentWirePayload.trigger,
|
|
6928
6961
|
});
|
|
6929
6962
|
}, {
|
|
@@ -7873,9 +7906,7 @@ function chatAgent(options) {
|
|
|
7873
7906
|
usage: contextUsage,
|
|
7874
7907
|
turnUsage,
|
|
7875
7908
|
totalUsage: cumulativeUsage,
|
|
7876
|
-
|
|
7877
|
-
turn,
|
|
7878
|
-
clientData,
|
|
7909
|
+
...chatEventContext(),
|
|
7879
7910
|
source: "outer",
|
|
7880
7911
|
});
|
|
7881
7912
|
if (shouldTrigger) {
|
|
@@ -7895,9 +7926,7 @@ function chatAgent(options) {
|
|
|
7895
7926
|
messages: accumulatedMessages,
|
|
7896
7927
|
usage: turnUsage,
|
|
7897
7928
|
totalUsage: cumulativeUsage,
|
|
7898
|
-
|
|
7899
|
-
turn,
|
|
7900
|
-
clientData,
|
|
7929
|
+
...chatEventContext(),
|
|
7901
7930
|
source: "outer",
|
|
7902
7931
|
});
|
|
7903
7932
|
// Apply compactModelMessages/compactUIMessages callbacks, or defaults.
|
|
@@ -7943,8 +7972,7 @@ function chatAgent(options) {
|
|
|
7943
7972
|
inputTokens: turnUsage.inputTokens,
|
|
7944
7973
|
outputTokens: turnUsage.outputTokens,
|
|
7945
7974
|
stepNumber: -1, // outer loop, not a step
|
|
7946
|
-
|
|
7947
|
-
turn,
|
|
7975
|
+
...chatEventContext(),
|
|
7948
7976
|
writer: { write, merge },
|
|
7949
7977
|
});
|
|
7950
7978
|
}
|
|
@@ -8063,7 +8091,8 @@ function chatAgent(options) {
|
|
|
8063
8091
|
}
|
|
8064
8092
|
}
|
|
8065
8093
|
settleRecoveredTurn(currentWirePayload);
|
|
8066
|
-
|
|
8094
|
+
channelEgressLatches.open(currentWirePayload.chatId);
|
|
8095
|
+
postTurnHookLatches.open(currentWirePayload.chatId);
|
|
8067
8096
|
// Write turn-complete control chunk — closes the frontend stream.
|
|
8068
8097
|
const turnCompleteResult = await writeTurnCompleteChunk(currentWirePayload.chatId, turnAccessToken);
|
|
8069
8098
|
// Tool calls that have not had controls posted yet; a resume with the rest still
|
|
@@ -8262,7 +8291,7 @@ function chatAgent(options) {
|
|
|
8262
8291
|
});
|
|
8263
8292
|
}
|
|
8264
8293
|
}
|
|
8265
|
-
|
|
8294
|
+
channelEgressLatches.settle(currentWirePayload.chatId);
|
|
8266
8295
|
// Fire onTurnComplete — stream is closed, use for persistence.
|
|
8267
8296
|
if (onTurnComplete) {
|
|
8268
8297
|
await tracer_js_1.tracer.startActiveSpan("onTurnComplete()", async () => {
|
|
@@ -8361,6 +8390,7 @@ function chatAgent(options) {
|
|
|
8361
8390
|
});
|
|
8362
8391
|
}
|
|
8363
8392
|
}
|
|
8393
|
+
postTurnHookLatches.settle(currentWirePayload.chatId);
|
|
8364
8394
|
} // end if (!isAction)
|
|
8365
8395
|
// NOTE: We intentionally do NOT await deferred work from onTurnComplete here.
|
|
8366
8396
|
// Promises deferred in onTurnComplete (e.g. background self-review via
|
|
@@ -8474,12 +8504,13 @@ function chatAgent(options) {
|
|
|
8474
8504
|
if (turnError instanceof Error &&
|
|
8475
8505
|
turnError.name === "AbortError" &&
|
|
8476
8506
|
runSignal.aborted) {
|
|
8477
|
-
|
|
8507
|
+
channelEgressLatches.settle(currentWirePayload.chatId);
|
|
8508
|
+
postTurnHookLatches.settle(currentWirePayload.chatId);
|
|
8478
8509
|
channelLifecycle.dispose();
|
|
8479
8510
|
// Full run cancellation — exit immediately
|
|
8480
8511
|
throw turnError;
|
|
8481
8512
|
}
|
|
8482
|
-
|
|
8513
|
+
channelEgressLatches.settle(currentWirePayload.chatId);
|
|
8483
8514
|
// OOM errors must escape the turn loop so the task runtime can
|
|
8484
8515
|
// honor `retry.outOfMemory.machine` (set on chat.agent via
|
|
8485
8516
|
// `oomMachine`). Catching them here would keep the dead worker
|
|
@@ -8600,6 +8631,7 @@ function chatAgent(options) {
|
|
|
8600
8631
|
writer.write({ type: "error", errorText });
|
|
8601
8632
|
});
|
|
8602
8633
|
// Signal turn complete so the client knows this turn is done
|
|
8634
|
+
postTurnHookLatches.open(currentWirePayload.chatId);
|
|
8603
8635
|
errorTurnCompleteResult = await writeTurnCompleteChunk(currentWirePayload.chatId);
|
|
8604
8636
|
// A later action's snapshot reuses this cursor, so it has to move
|
|
8605
8637
|
// here too or that snapshot resumes from before the failed turn.
|
|
@@ -8800,6 +8832,7 @@ function chatAgent(options) {
|
|
|
8800
8832
|
});
|
|
8801
8833
|
}
|
|
8802
8834
|
}
|
|
8835
|
+
postTurnHookLatches.settle(currentWirePayload.chatId);
|
|
8803
8836
|
if (locals_js_1.locals.get(chatCloseRequestedKey)) {
|
|
8804
8837
|
await performChatClose();
|
|
8805
8838
|
return;
|
|
@@ -8855,6 +8888,12 @@ function chatAgent(options) {
|
|
|
8855
8888
|
// reaching one of the loop's close checks (a turn timeout, an OOM
|
|
8856
8889
|
// re-throw). `performChatClose` is idempotent, so the ordinary path
|
|
8857
8890
|
// having already run it costs nothing here.
|
|
8891
|
+
// Any exit that skips a turn's own settle (an OOM re-throw, a throw from the turn's
|
|
8892
|
+
// error handler) leaves its post-turn latches open, and the module-level entries would
|
|
8893
|
+
// outlive this run and block the next one for the same chat. Settling is a no-op when
|
|
8894
|
+
// nothing is open, and runs first so a throwing close cannot skip it.
|
|
8895
|
+
channelEgressLatches.settle(currentWirePayload.chatId, "run-ended");
|
|
8896
|
+
postTurnHookLatches.settle(currentWirePayload.chatId, "run-ended");
|
|
8858
8897
|
await performChatClose();
|
|
8859
8898
|
// `stopSub` is registered post-preload so the close-during-preload
|
|
8860
8899
|
// early-return path may exit before it ever attached. Guard the
|
|
@@ -10131,13 +10170,14 @@ class ChatMessageAccumulator {
|
|
|
10131
10170
|
const comp = this._compaction;
|
|
10132
10171
|
const pm = this._pendingMessages;
|
|
10133
10172
|
const queue = this._steeringQueue;
|
|
10134
|
-
return (0, steeringContext_js_1.retainStepMessages)(async ({ messages, steps }) => {
|
|
10173
|
+
return (0, steeringContext_js_1.retainStepMessages)(async ({ messages, steps, responseMessages, }) => {
|
|
10135
10174
|
let resultMessages;
|
|
10136
10175
|
// 1. Compaction
|
|
10137
10176
|
if (comp) {
|
|
10138
10177
|
const result = await chatCompact(messages, steps, {
|
|
10178
|
+
responseMessages,
|
|
10139
10179
|
shouldCompact: comp.shouldCompact,
|
|
10140
|
-
summarize: (
|
|
10180
|
+
summarize: innerSummarize(comp, steps),
|
|
10141
10181
|
});
|
|
10142
10182
|
if (result.type !== "skipped") {
|
|
10143
10183
|
resultMessages = result.messages;
|
|
@@ -10166,6 +10206,13 @@ class ChatMessageAccumulator {
|
|
|
10166
10206
|
async compactIfNeeded(usage, context) {
|
|
10167
10207
|
if (!this._compaction || !usage)
|
|
10168
10208
|
return false;
|
|
10209
|
+
// An explicit context wins; inside a chat turn the turn's own fills in what it leaves out.
|
|
10210
|
+
const turnContext = chatEventContext();
|
|
10211
|
+
const eventContext = {
|
|
10212
|
+
chatId: context?.chatId ?? turnContext.chatId,
|
|
10213
|
+
turn: context?.turn ?? turnContext.turn,
|
|
10214
|
+
clientData: context?.clientData ?? turnContext.clientData,
|
|
10215
|
+
};
|
|
10169
10216
|
const shouldTrigger = await this._compaction.shouldCompact({
|
|
10170
10217
|
messages: this.modelMessages,
|
|
10171
10218
|
totalTokens: usage.totalTokens,
|
|
@@ -10174,9 +10221,7 @@ class ChatMessageAccumulator {
|
|
|
10174
10221
|
usage,
|
|
10175
10222
|
turnUsage: context?.turnUsage,
|
|
10176
10223
|
totalUsage: context?.totalUsage,
|
|
10177
|
-
|
|
10178
|
-
turn: context?.turn,
|
|
10179
|
-
clientData: context?.clientData,
|
|
10224
|
+
...eventContext,
|
|
10180
10225
|
source: "outer",
|
|
10181
10226
|
});
|
|
10182
10227
|
if (!shouldTrigger)
|
|
@@ -10185,18 +10230,16 @@ class ChatMessageAccumulator {
|
|
|
10185
10230
|
messages: this.modelMessages,
|
|
10186
10231
|
usage,
|
|
10187
10232
|
totalUsage: context?.totalUsage,
|
|
10188
|
-
|
|
10189
|
-
turn: context?.turn,
|
|
10190
|
-
clientData: context?.clientData,
|
|
10233
|
+
...eventContext,
|
|
10191
10234
|
source: "outer",
|
|
10192
10235
|
});
|
|
10193
10236
|
const compactEvent = {
|
|
10194
10237
|
summary,
|
|
10195
10238
|
uiMessages: this.uiMessages,
|
|
10196
10239
|
modelMessages: this.modelMessages,
|
|
10197
|
-
chatId:
|
|
10198
|
-
turn:
|
|
10199
|
-
clientData:
|
|
10240
|
+
chatId: eventContext.chatId ?? "",
|
|
10241
|
+
turn: eventContext.turn ?? 0,
|
|
10242
|
+
clientData: eventContext.clientData,
|
|
10200
10243
|
source: "outer",
|
|
10201
10244
|
};
|
|
10202
10245
|
this.modelMessages = this._compaction.compactModelMessages
|
|
@@ -10494,6 +10537,7 @@ function createChatSession(payload, options) {
|
|
|
10494
10537
|
message: lastUIMessage,
|
|
10495
10538
|
chatId: currentPayload.chatId,
|
|
10496
10539
|
turn,
|
|
10540
|
+
clientData: chatEventContext().clientData,
|
|
10497
10541
|
});
|
|
10498
10542
|
}
|
|
10499
10543
|
catch {
|
|
@@ -10673,9 +10717,7 @@ function createChatSession(payload, options) {
|
|
|
10673
10717
|
usage: contextUsage,
|
|
10674
10718
|
turnUsage,
|
|
10675
10719
|
totalUsage: cumulativeUsage,
|
|
10676
|
-
|
|
10677
|
-
turn,
|
|
10678
|
-
clientData: currentPayload.metadata,
|
|
10720
|
+
...chatEventContext(),
|
|
10679
10721
|
source: "outer",
|
|
10680
10722
|
});
|
|
10681
10723
|
if (shouldTrigger) {
|
|
@@ -10683,9 +10725,7 @@ function createChatSession(payload, options) {
|
|
|
10683
10725
|
messages: accumulator.modelMessages,
|
|
10684
10726
|
usage: turnUsage,
|
|
10685
10727
|
totalUsage: cumulativeUsage,
|
|
10686
|
-
|
|
10687
|
-
turn,
|
|
10688
|
-
clientData: currentPayload.metadata,
|
|
10728
|
+
...chatEventContext(),
|
|
10689
10729
|
source: "outer",
|
|
10690
10730
|
});
|
|
10691
10731
|
const compactEvent = {
|
|
@@ -10729,12 +10769,13 @@ function createChatSession(payload, options) {
|
|
|
10729
10769
|
const hasPending = !!sessionPendingMessages;
|
|
10730
10770
|
if (!hasCompaction && !hasPending)
|
|
10731
10771
|
return undefined;
|
|
10732
|
-
return (0, steeringContext_js_1.retainStepMessages)(async ({ messages: stepMsgs, steps, }) => {
|
|
10772
|
+
return (0, steeringContext_js_1.retainStepMessages)(async ({ messages: stepMsgs, steps, responseMessages, }) => {
|
|
10733
10773
|
let resultMessages;
|
|
10734
10774
|
if (sessionCompaction) {
|
|
10735
10775
|
const compactResult = await chatCompact(stepMsgs, steps, {
|
|
10776
|
+
responseMessages,
|
|
10736
10777
|
shouldCompact: sessionCompaction.shouldCompact,
|
|
10737
|
-
summarize: (
|
|
10778
|
+
summarize: innerSummarize(sessionCompaction, steps),
|
|
10738
10779
|
});
|
|
10739
10780
|
if (compactResult.type !== "skipped") {
|
|
10740
10781
|
resultMessages = compactResult.messages;
|
|
@@ -11130,7 +11171,7 @@ function overrideRequestHeaders(authHeaders) {
|
|
|
11130
11171
|
async function callSessionsCreateWithOverride(args) {
|
|
11131
11172
|
const apiClient = v3_1.apiClientManager.client;
|
|
11132
11173
|
if (!apiClient) {
|
|
11133
|
-
throw new Error("chat.createStartSessionAction: no API access token configured.
|
|
11174
|
+
throw new Error("chat.createStartSessionAction: no API access token configured. Call configure({ accessToken }) or pass apiClient: { accessToken } to createStartSessionAction before invoking the action.");
|
|
11134
11175
|
}
|
|
11135
11176
|
const authHeaders = apiClient.getAuthenticationHeaders("createSession");
|
|
11136
11177
|
const ctx = { endpoint: "sessions", chatId: args.chatId };
|
|
@@ -11154,55 +11195,60 @@ async function callSessionsCreateWithOverride(args) {
|
|
|
11154
11195
|
async function mintPublicTokenWithOverride(args) {
|
|
11155
11196
|
const apiClient = v3_1.apiClientManager.client;
|
|
11156
11197
|
if (!apiClient) {
|
|
11157
|
-
throw new Error("chat.createStartSessionAction: no API access token configured for
|
|
11198
|
+
throw new Error("chat.createStartSessionAction: no API access token configured for public-token minting. Call configure({ accessToken }) or pass apiClient: { accessToken } to createStartSessionAction.");
|
|
11158
11199
|
}
|
|
11159
11200
|
const authHeaders = apiClient.getAuthenticationHeaders("createPublicToken");
|
|
11160
|
-
const accessToken = authHeaders.Authorization.slice("Bearer ".length);
|
|
11161
11201
|
const ctx = { endpoint: "auth", chatId: args.chatId };
|
|
11162
11202
|
const scopes = [`read:sessions:${args.chatId}`, `write:sessions:${args.chatId}`];
|
|
11163
|
-
const
|
|
11164
|
-
const
|
|
11165
|
-
const url = `${resolveChatStartBaseURL("auth", args.chatId, args.baseURLOption)}${endpoint}`;
|
|
11203
|
+
const baseURL = resolveChatStartBaseURL("auth", args.chatId, args.baseURLOption);
|
|
11204
|
+
const url = `${baseURL}/api/v1/auth/public-tokens`;
|
|
11166
11205
|
const init = {
|
|
11167
11206
|
method: "POST",
|
|
11168
11207
|
headers: overrideRequestHeaders(authHeaders),
|
|
11169
|
-
|
|
11170
|
-
|
|
11171
|
-
|
|
11172
|
-
|
|
11173
|
-
|
|
11174
|
-
|
|
11175
|
-
: args.expirationTime,
|
|
11176
|
-
}),
|
|
11177
|
-
}
|
|
11178
|
-
: {}),
|
|
11208
|
+
body: JSON.stringify({
|
|
11209
|
+
scopes,
|
|
11210
|
+
expirationTime: args.expirationTime instanceof Date
|
|
11211
|
+
? Math.floor(args.expirationTime.getTime() / 1000)
|
|
11212
|
+
: args.expirationTime,
|
|
11213
|
+
}),
|
|
11179
11214
|
};
|
|
11180
11215
|
const response = args.fetchOverride
|
|
11181
11216
|
? await args.fetchOverride(url, init, ctx)
|
|
11182
11217
|
: await fetch(url, init);
|
|
11183
11218
|
if (!response.ok) {
|
|
11184
|
-
|
|
11185
|
-
|
|
11186
|
-
|
|
11219
|
+
const accessToken = authHeaders.Authorization.slice("Bearer ".length);
|
|
11220
|
+
if ((0, v3_1.canFallbackToLocalPublicToken)(accessToken, scopes, response.status)) {
|
|
11221
|
+
await response.arrayBuffer();
|
|
11222
|
+
const claimsURL = `${baseURL}/api/v1/auth/jwt/claims`;
|
|
11223
|
+
const claimsInit = {
|
|
11224
|
+
method: "POST",
|
|
11225
|
+
headers: overrideRequestHeaders(authHeaders),
|
|
11226
|
+
};
|
|
11227
|
+
const claimsResponse = args.fetchOverride
|
|
11228
|
+
? await args.fetchOverride(claimsURL, claimsInit, ctx)
|
|
11229
|
+
: await fetch(claimsURL, claimsInit);
|
|
11230
|
+
if (!claimsResponse.ok) {
|
|
11231
|
+
const text = await claimsResponse.text().catch(() => "");
|
|
11232
|
+
throw new Error(`auth.generateJWTClaims failed: ${claimsResponse.status} ${text}`);
|
|
11233
|
+
}
|
|
11234
|
+
const claims = (await claimsResponse.json());
|
|
11235
|
+
return (0, v3_1.generateJWT)({
|
|
11236
|
+
secretKey: accessToken,
|
|
11237
|
+
payload: { ...claims, scopes },
|
|
11238
|
+
expirationTime: args.expirationTime,
|
|
11239
|
+
});
|
|
11240
|
+
}
|
|
11241
|
+
if (response.status === 404) {
|
|
11242
|
+
throw new Error("The server does not support public-token minting for this credential. Upgrade your self-hosted server.");
|
|
11187
11243
|
}
|
|
11188
11244
|
const text = await response.text().catch(() => "");
|
|
11189
11245
|
throw new Error(`auth.createPublicToken failed: ${response.status} ${text}`);
|
|
11190
11246
|
}
|
|
11191
11247
|
const responseBody = (await response.json());
|
|
11192
|
-
if (
|
|
11193
|
-
|
|
11194
|
-
throw new Error("auth.createPublicToken failed: server response did not include a token");
|
|
11195
|
-
}
|
|
11196
|
-
return responseBody.token;
|
|
11248
|
+
if (typeof responseBody.token !== "string") {
|
|
11249
|
+
throw new Error("auth.createPublicToken failed: server response did not include a token");
|
|
11197
11250
|
}
|
|
11198
|
-
return
|
|
11199
|
-
secretKey: accessToken,
|
|
11200
|
-
payload: {
|
|
11201
|
-
...responseBody,
|
|
11202
|
-
scopes,
|
|
11203
|
-
},
|
|
11204
|
-
expirationTime: args.expirationTime,
|
|
11205
|
-
});
|
|
11251
|
+
return responseBody.token;
|
|
11206
11252
|
}
|
|
11207
11253
|
/**
|
|
11208
11254
|
* Creates a server-side helper that reads a conversation from a transcript
|