@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/esm/v3/ai.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { accessoryAttributes, apiClientManager,
|
|
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
|
|
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
|
-
...
|
|
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 =
|
|
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
|
-
|
|
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:
|
|
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
|
-
|
|
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: (
|
|
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
|
|
4770
|
-
*
|
|
4771
|
-
*
|
|
4772
|
-
*
|
|
4773
|
-
|
|
4774
|
-
|
|
4775
|
-
const
|
|
4776
|
-
|
|
4777
|
-
|
|
4778
|
-
|
|
4779
|
-
|
|
4780
|
-
|
|
4781
|
-
|
|
4782
|
-
|
|
4783
|
-
|
|
4784
|
-
|
|
4785
|
-
|
|
4786
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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: (
|
|
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
|
-
|
|
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
|
-
|
|
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:
|
|
10179
|
-
turn:
|
|
10180
|
-
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
|
-
|
|
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
|
-
|
|
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: (
|
|
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.
|
|
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
|
|
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
|
|
11145
|
-
const
|
|
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
|
-
|
|
11151
|
-
|
|
11152
|
-
|
|
11153
|
-
|
|
11154
|
-
|
|
11155
|
-
|
|
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
|
-
|
|
11166
|
-
|
|
11167
|
-
|
|
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 (
|
|
11174
|
-
|
|
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
|
|
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
|