@trigger.dev/sdk 4.5.0-rc.7 → 4.5.0
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-shared.js +1 -4
- package/dist/commonjs/v3/ai-shared.js.map +1 -1
- package/dist/commonjs/v3/ai.d.ts +7 -1
- package/dist/commonjs/v3/ai.js +110 -44
- package/dist/commonjs/v3/ai.js.map +1 -1
- package/dist/commonjs/v3/chat-client.js +18 -14
- package/dist/commonjs/v3/chat-client.js.map +1 -1
- package/dist/commonjs/v3/chat-react.js +1 -3
- package/dist/commonjs/v3/chat-react.js.map +1 -1
- package/dist/commonjs/v3/chat-server.d.ts +69 -1
- package/dist/commonjs/v3/chat-server.js +162 -14
- package/dist/commonjs/v3/chat-server.js.map +1 -1
- package/dist/commonjs/v3/chat-server.test.js +156 -12
- package/dist/commonjs/v3/chat-server.test.js.map +1 -1
- package/dist/commonjs/v3/chat.js +4 -3
- package/dist/commonjs/v3/chat.js.map +1 -1
- package/dist/commonjs/v3/chat.test.js +6 -18
- package/dist/commonjs/v3/chat.test.js.map +1 -1
- package/dist/commonjs/v3/idempotencyKeys.js.map +1 -1
- package/dist/commonjs/v3/prompt.js +2 -6
- package/dist/commonjs/v3/prompt.js.map +1 -1
- package/dist/commonjs/v3/promptManagement.js.map +1 -1
- package/dist/commonjs/v3/retry.js.map +1 -1
- package/dist/commonjs/v3/runs.d.ts +1 -1
- package/dist/commonjs/v3/sessions.js +1 -0
- package/dist/commonjs/v3/sessions.js.map +1 -1
- package/dist/commonjs/v3/shared.js.map +1 -1
- package/dist/commonjs/v3/skill.js.map +1 -1
- package/dist/commonjs/v3/streams.js +2 -3
- package/dist/commonjs/v3/streams.js.map +1 -1
- package/dist/commonjs/v3/streams.test.js.map +1 -1
- package/dist/commonjs/v3/test/mock-chat-agent.js +1 -4
- package/dist/commonjs/v3/test/mock-chat-agent.js.map +1 -1
- package/dist/commonjs/v3/test/test-session-handle.js.map +1 -1
- package/dist/commonjs/v3/triggerClient.test.js +1 -1
- package/dist/commonjs/v3/triggerClient.test.js.map +1 -1
- package/dist/commonjs/v3/triggerClient.types.test.js +11 -11
- package/dist/commonjs/v3/triggerClient.types.test.js.map +1 -1
- package/dist/commonjs/version.js +1 -1
- package/dist/esm/v3/ai-shared.js +1 -4
- package/dist/esm/v3/ai-shared.js.map +1 -1
- package/dist/esm/v3/ai.d.ts +7 -1
- package/dist/esm/v3/ai.js +110 -44
- package/dist/esm/v3/ai.js.map +1 -1
- package/dist/esm/v3/chat-client.js +18 -14
- package/dist/esm/v3/chat-client.js.map +1 -1
- package/dist/esm/v3/chat-react.js +1 -3
- package/dist/esm/v3/chat-react.js.map +1 -1
- package/dist/esm/v3/chat-server.d.ts +69 -1
- package/dist/esm/v3/chat-server.js +162 -14
- package/dist/esm/v3/chat-server.js.map +1 -1
- package/dist/esm/v3/chat-server.test.js +156 -12
- package/dist/esm/v3/chat-server.test.js.map +1 -1
- package/dist/esm/v3/chat.js +4 -3
- package/dist/esm/v3/chat.js.map +1 -1
- package/dist/esm/v3/chat.test.js +6 -18
- package/dist/esm/v3/chat.test.js.map +1 -1
- package/dist/esm/v3/idempotencyKeys.js +1 -1
- package/dist/esm/v3/idempotencyKeys.js.map +1 -1
- package/dist/esm/v3/prompt.js +2 -6
- package/dist/esm/v3/prompt.js.map +1 -1
- package/dist/esm/v3/promptManagement.js.map +1 -1
- package/dist/esm/v3/retry.js.map +1 -1
- package/dist/esm/v3/sessions.js +1 -0
- package/dist/esm/v3/sessions.js.map +1 -1
- package/dist/esm/v3/shared.js.map +1 -1
- package/dist/esm/v3/skill.js.map +1 -1
- package/dist/esm/v3/streams.js +2 -3
- package/dist/esm/v3/streams.js.map +1 -1
- package/dist/esm/v3/streams.test.js.map +1 -1
- package/dist/esm/v3/test/mock-chat-agent.js +4 -7
- package/dist/esm/v3/test/mock-chat-agent.js.map +1 -1
- package/dist/esm/v3/test/test-session-handle.js.map +1 -1
- package/dist/esm/v3/triggerClient.test.js +1 -1
- package/dist/esm/v3/triggerClient.test.js.map +1 -1
- package/dist/esm/v3/triggerClient.types.test.js +11 -11
- package/dist/esm/v3/triggerClient.types.test.js.map +1 -1
- package/dist/esm/version.js +1 -1
- package/docs/ai/prompts.mdx +0 -4
- package/docs/ai-chat/actions.mdx +0 -4
- package/docs/ai-chat/anatomy.mdx +0 -4
- package/docs/ai-chat/backend.mdx +0 -4
- package/docs/ai-chat/background-injection.mdx +0 -4
- package/docs/ai-chat/changelog.mdx +109 -1
- package/docs/ai-chat/chat-local.mdx +0 -4
- package/docs/ai-chat/client-protocol.mdx +0 -4
- package/docs/ai-chat/compaction.mdx +0 -4
- package/docs/ai-chat/custom-agents.mdx +32 -4
- package/docs/ai-chat/error-handling.mdx +0 -4
- package/docs/ai-chat/fast-starts.mdx +86 -4
- package/docs/ai-chat/frontend.mdx +0 -4
- package/docs/ai-chat/how-it-works.mdx +0 -4
- package/docs/ai-chat/lifecycle-hooks.mdx +0 -4
- package/docs/ai-chat/mcp.mdx +0 -4
- package/docs/ai-chat/overview.mdx +0 -4
- package/docs/ai-chat/patterns/branching-conversations.mdx +0 -4
- package/docs/ai-chat/patterns/code-sandbox.mdx +0 -4
- package/docs/ai-chat/patterns/database-persistence.mdx +0 -4
- package/docs/ai-chat/patterns/human-in-the-loop.mdx +9 -5
- package/docs/ai-chat/patterns/large-payloads.mdx +0 -4
- package/docs/ai-chat/patterns/oom-resilience.mdx +0 -4
- package/docs/ai-chat/patterns/persistence-and-replay.mdx +0 -4
- package/docs/ai-chat/patterns/recovery-boot.mdx +0 -4
- package/docs/ai-chat/patterns/skills.mdx +0 -4
- package/docs/ai-chat/patterns/sub-agents.mdx +0 -4
- package/docs/ai-chat/patterns/tool-result-auditing.mdx +0 -4
- package/docs/ai-chat/patterns/trusted-edge-signals.mdx +0 -4
- package/docs/ai-chat/patterns/version-upgrades.mdx +0 -4
- package/docs/ai-chat/pending-messages.mdx +0 -4
- package/docs/ai-chat/prompt-caching.mdx +4 -4
- package/docs/ai-chat/quick-start.mdx +0 -4
- package/docs/ai-chat/reference.mdx +1 -5
- package/docs/ai-chat/server-chat.mdx +0 -4
- package/docs/ai-chat/sessions.mdx +0 -4
- package/docs/ai-chat/testing.mdx +0 -4
- package/docs/ai-chat/tools.mdx +0 -4
- package/docs/ai-chat/types.mdx +0 -4
- package/docs/ai-chat/upgrade-guide.mdx +0 -4
- package/docs/building-with-ai.mdx +1 -1
- package/docs/deploy-environment-variables.mdx +1 -1
- package/docs/deployment/dev-branches.mdx +92 -0
- package/docs/deployment/overview.mdx +1 -1
- package/docs/help-slack.mdx +1 -0
- package/docs/how-to-reduce-your-spend.mdx +14 -7
- package/docs/introduction.mdx +13 -13
- package/docs/mcp-agent-rules.mdx +1 -1
- package/docs/open-source-self-hosting.mdx +1 -1
- package/docs/self-hosting/overview.mdx +2 -1
- package/docs/tasks/scheduled.mdx +2 -2
- package/docs/troubleshooting-debugging-in-vscode.mdx +1 -0
- package/docs/troubleshooting-github-issues.mdx +1 -1
- package/docs/troubleshooting-uptime-status.mdx +1 -0
- package/docs/video-walkthrough.mdx +1 -1
- package/docs/wait-for.mdx +1 -1
- package/package.json +2 -2
- package/docs/cli-dev.mdx +0 -8
|
@@ -128,10 +128,7 @@ function slimSubmitMessageForWire(message) {
|
|
|
128
128
|
if (message.role !== "assistant")
|
|
129
129
|
return message;
|
|
130
130
|
const parts = (message.parts ?? []);
|
|
131
|
-
const advancedToolParts = parts.filter((p) => p &&
|
|
132
|
-
typeof p === "object" &&
|
|
133
|
-
isToolPartType(p.type) &&
|
|
134
|
-
isWireAdvanceableToolState(p.state));
|
|
131
|
+
const advancedToolParts = parts.filter((p) => p && typeof p === "object" && isToolPartType(p.type) && isWireAdvanceableToolState(p.state));
|
|
135
132
|
if (advancedToolParts.length === 0)
|
|
136
133
|
return message;
|
|
137
134
|
const slimParts = advancedToolParts.map((p) => {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ai-shared.js","sourceRoot":"","sources":["../../../src/v3/ai-shared.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;GAeG;;;
|
|
1
|
+
{"version":3,"file":"ai-shared.js","sourceRoot":"","sources":["../../../src/v3/ai-shared.ts"],"names":[],"mappings":"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"}
|
package/dist/commonjs/v3/ai.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { AnyTask, type MachinePresetName, type RealtimeDefinedInputStream, type RealtimeDefinedStream, Task, type inferSchemaIn, type inferSchemaOut, type TaskIdentifier, type TaskOptions, type TaskSchema, type TaskRunContext, type TaskWithSchema } from "@trigger.dev/core/v3";
|
|
1
|
+
import { AnyTask, type MachinePresetName, type RealtimeDefinedInputStream, type RealtimeDefinedStream, type ApiClientConfiguration, Task, type inferSchemaIn, type inferSchemaOut, type TaskIdentifier, type TaskOptions, type TaskSchema, type TaskRunContext, type TaskWithSchema } from "@trigger.dev/core/v3";
|
|
2
2
|
import type { FinishReason, LanguageModelUsage, ModelMessage, ProviderMetadata, Tool, ToolSet, UIMessage, UIMessageChunk, UIMessageStreamOptions } from "ai";
|
|
3
3
|
type ToolCallOptions = {
|
|
4
4
|
toolCallId: string;
|
|
@@ -2839,6 +2839,12 @@ export type CreateChatStartSessionActionOptions = {
|
|
|
2839
2839
|
* custom retry. Applies to both session-create and JWT-claims POSTs.
|
|
2840
2840
|
*/
|
|
2841
2841
|
fetch?: ChatStartSessionFetchOverride;
|
|
2842
|
+
/**
|
|
2843
|
+
* API client config (baseURL / accessToken) to scope this action to a specific
|
|
2844
|
+
* environment, for callers that can't set a global `TRIGGER_SECRET_KEY`. The
|
|
2845
|
+
* returned action runs under this config.
|
|
2846
|
+
*/
|
|
2847
|
+
apiClient?: ApiClientConfiguration;
|
|
2842
2848
|
};
|
|
2843
2849
|
/**
|
|
2844
2850
|
* Params for the function returned by {@link createChatStartSessionAction}.
|
package/dist/commonjs/v3/ai.js
CHANGED
|
@@ -155,7 +155,9 @@ async function seedSessionInResumeCursorForCustomLoop(payload) {
|
|
|
155
155
|
}
|
|
156
156
|
}
|
|
157
157
|
catch (error) {
|
|
158
|
-
v3_1.logger.warn("chat session: session.in resume cursor lookup failed; old messages may replay", {
|
|
158
|
+
v3_1.logger.warn("chat session: session.in resume cursor lookup failed; old messages may replay", {
|
|
159
|
+
error: error instanceof Error ? error.message : String(error),
|
|
160
|
+
});
|
|
159
161
|
}
|
|
160
162
|
}
|
|
161
163
|
let readChatSnapshotImpl;
|
|
@@ -1241,8 +1243,7 @@ const handoverInput = {
|
|
|
1241
1243
|
const result = await getChatSession().in.waitWithIdleTimeout(options);
|
|
1242
1244
|
if (!result.ok)
|
|
1243
1245
|
return result;
|
|
1244
|
-
if (result.output.kind === "handover" ||
|
|
1245
|
-
result.output.kind === "handover-skip") {
|
|
1246
|
+
if (result.output.kind === "handover" || result.output.kind === "handover-skip") {
|
|
1246
1247
|
return { ok: true, output: result.output };
|
|
1247
1248
|
}
|
|
1248
1249
|
// Other kinds (message, stop) are not expected during handover-prepare.
|
|
@@ -1955,6 +1956,55 @@ function isStepBoundarySafe(step) {
|
|
|
1955
1956
|
.map((p) => p.toolCallId));
|
|
1956
1957
|
return ![...callIds].some((id) => !settledIds.has(id));
|
|
1957
1958
|
}
|
|
1959
|
+
/**
|
|
1960
|
+
* True when a model message is a `tool` message carrying a
|
|
1961
|
+
* `tool-approval-response` part — the trailing row a head-start handover
|
|
1962
|
+
* reshapes a pending first-turn tool call into. AI SDK's `collectToolApprovals`
|
|
1963
|
+
* only inspects the conversation's last message, so this row must survive to
|
|
1964
|
+
* `streamText` intact for the agent to execute the handed-over call.
|
|
1965
|
+
* @internal
|
|
1966
|
+
*/
|
|
1967
|
+
function hasToolApprovalResponse(message) {
|
|
1968
|
+
return (message?.role === "tool" &&
|
|
1969
|
+
Array.isArray(message.content) &&
|
|
1970
|
+
message.content.some((part) => part != null &&
|
|
1971
|
+
typeof part === "object" &&
|
|
1972
|
+
part.type === "tool-approval-response"));
|
|
1973
|
+
}
|
|
1974
|
+
/**
|
|
1975
|
+
* Keep a head-start handover's tool-approval tail intact across `prepareMessages`.
|
|
1976
|
+
*
|
|
1977
|
+
* The handover reshapes the warm step-1's pending tool call into AI SDK's
|
|
1978
|
+
* tool-approval round: a `tool-approval-request` on the assistant plus a
|
|
1979
|
+
* trailing `tool` message with `tool-approval-response { approved: true }`. The
|
|
1980
|
+
* agent's next `streamText` runs `collectToolApprovals`, which ONLY looks at the
|
|
1981
|
+
* last message — so that tool row must stay last and unmodified for the pending
|
|
1982
|
+
* call to execute. A user `prepareMessages` hook that rewrites or drops the last
|
|
1983
|
+
* message (e.g. rolling a provider cache breakpoint onto it) silently breaks the
|
|
1984
|
+
* resume: the agent sends a bare `tool_use` and the turn dies with
|
|
1985
|
+
* "tool_use ids were found without tool_result". If the hook's input ended with
|
|
1986
|
+
* that approval tail, re-assert the original tail as the last message.
|
|
1987
|
+
*
|
|
1988
|
+
* No-op for every normal turn — only fires when the input genuinely ended with a
|
|
1989
|
+
* pending tool-approval response (i.e. a head-start resume).
|
|
1990
|
+
* @internal
|
|
1991
|
+
*/
|
|
1992
|
+
function preserveToolApprovalTail(original, prepared) {
|
|
1993
|
+
const originalTail = original[original.length - 1];
|
|
1994
|
+
if (!hasToolApprovalResponse(originalTail))
|
|
1995
|
+
return prepared;
|
|
1996
|
+
// Hook left the exact tail object in place — nothing to do.
|
|
1997
|
+
if (prepared[prepared.length - 1] === originalTail)
|
|
1998
|
+
return prepared;
|
|
1999
|
+
// Otherwise drop only the trailing approval tail the hook produced (the
|
|
2000
|
+
// original moved, or a rewritten copy) and re-append the original so it is
|
|
2001
|
+
// last and intact. Older approval rounds deeper in history must survive.
|
|
2002
|
+
const withoutMovedOriginal = prepared.filter((m) => m !== originalTail);
|
|
2003
|
+
while (hasToolApprovalResponse(withoutMovedOriginal[withoutMovedOriginal.length - 1])) {
|
|
2004
|
+
withoutMovedOriginal.pop();
|
|
2005
|
+
}
|
|
2006
|
+
return [...withoutMovedOriginal, originalTail];
|
|
2007
|
+
}
|
|
1958
2008
|
/**
|
|
1959
2009
|
* Apply the prepareMessages hook if one is set in locals.
|
|
1960
2010
|
* @internal
|
|
@@ -1964,7 +2014,7 @@ async function applyPrepareMessages(messages, reason) {
|
|
|
1964
2014
|
if (!hook)
|
|
1965
2015
|
return messages;
|
|
1966
2016
|
const turnCtx = locals_js_1.locals.get(chatTurnContextKey);
|
|
1967
|
-
|
|
2017
|
+
const prepared = await tracer_js_1.tracer.startActiveSpan("prepareMessages()", async () => {
|
|
1968
2018
|
return hook({
|
|
1969
2019
|
messages,
|
|
1970
2020
|
reason,
|
|
@@ -1980,6 +2030,9 @@ async function applyPrepareMessages(messages, reason) {
|
|
|
1980
2030
|
"chat.prepareMessages.messageCount": messages.length,
|
|
1981
2031
|
},
|
|
1982
2032
|
});
|
|
2033
|
+
// A user hook must never be able to break the head-start handover resume by
|
|
2034
|
+
// disturbing the trailing tool-approval row (see preserveToolApprovalTail).
|
|
2035
|
+
return preserveToolApprovalTail(messages, prepared);
|
|
1983
2036
|
}
|
|
1984
2037
|
/**
|
|
1985
2038
|
* Resolve the `tools` option into a concrete `ToolSet` and cache it in locals so
|
|
@@ -2928,7 +2981,9 @@ function chatAgent(options) {
|
|
|
2928
2981
|
const replayOutPhase = async () => {
|
|
2929
2982
|
const replayOutStart = Date.now();
|
|
2930
2983
|
try {
|
|
2931
|
-
const replayResult = await replaySessionOutTail(sessionIdForSnapshot, {
|
|
2984
|
+
const replayResult = await replaySessionOutTail(sessionIdForSnapshot, {
|
|
2985
|
+
lastEventId: bootSnapshot?.lastOutEventId,
|
|
2986
|
+
});
|
|
2932
2987
|
replayedSettled = replayResult.settled;
|
|
2933
2988
|
replayedPartial = replayResult.partial;
|
|
2934
2989
|
replayedPartialRaw = replayResult.partialRaw;
|
|
@@ -3025,9 +3080,7 @@ function chatAgent(options) {
|
|
|
3025
3080
|
// crashed / was canceled / requested upgrade
|
|
3026
3081
|
// - Snapshot exists at all (catches edge cases where the wire
|
|
3027
3082
|
// didn't set `continuation` but a snapshot indicates prior turns)
|
|
3028
|
-
const needsResumeCursor = ctx.attempt.number > 1 ||
|
|
3029
|
-
payload.continuation === true ||
|
|
3030
|
-
bootSnapshot !== undefined;
|
|
3083
|
+
const needsResumeCursor = ctx.attempt.number > 1 || payload.continuation === true || bootSnapshot !== undefined;
|
|
3031
3084
|
if (needsResumeCursor) {
|
|
3032
3085
|
try {
|
|
3033
3086
|
// Reuse the cursor the boot block already resolved (snapshot
|
|
@@ -3374,9 +3427,7 @@ function chatAgent(options) {
|
|
|
3374
3427
|
// Wait for the first real message — task-level idle settings win over
|
|
3375
3428
|
// `transport.preload(..., { idleTimeoutInSeconds })` / wire payload so
|
|
3376
3429
|
// `chat.agent({ idleTimeoutInSeconds, preloadIdleTimeoutInSeconds })` is authoritative.
|
|
3377
|
-
const effectivePreloadIdleTimeout = preloadIdleTimeoutInSeconds ??
|
|
3378
|
-
idleTimeoutInSeconds ??
|
|
3379
|
-
payload.idleTimeoutInSeconds;
|
|
3430
|
+
const effectivePreloadIdleTimeout = preloadIdleTimeoutInSeconds ?? idleTimeoutInSeconds ?? payload.idleTimeoutInSeconds;
|
|
3380
3431
|
const effectivePreloadTimeout = metadata_js_1.metadata.get(TURN_TIMEOUT_METADATA_KEY) ??
|
|
3381
3432
|
preloadTimeout ??
|
|
3382
3433
|
turnTimeout;
|
|
@@ -3621,7 +3672,9 @@ function chatAgent(options) {
|
|
|
3621
3672
|
if (lastUserMessage) {
|
|
3622
3673
|
turnAttributes["chat.user_message"] = lastUserMessage;
|
|
3623
3674
|
// Show a truncated preview of the user message as an accessory
|
|
3624
|
-
const preview = lastUserMessage.length > 80
|
|
3675
|
+
const preview = lastUserMessage.length > 80
|
|
3676
|
+
? lastUserMessage.slice(0, 80) + "..."
|
|
3677
|
+
: lastUserMessage;
|
|
3625
3678
|
Object.assign(turnAttributes, (0, v3_1.accessoryAttributes)({
|
|
3626
3679
|
items: [{ text: preview, variant: "normal" }],
|
|
3627
3680
|
style: "codepath",
|
|
@@ -3908,8 +3961,7 @@ function chatAgent(options) {
|
|
|
3908
3961
|
if (currentWirePayload.trigger === "submit-message" &&
|
|
3909
3962
|
cleanedUIMessages.length > 0) {
|
|
3910
3963
|
const lastUI = cleanedUIMessages[cleanedUIMessages.length - 1];
|
|
3911
|
-
const matchedExisting = lastUI.id !== undefined &&
|
|
3912
|
-
previouslyKnownMessageIds.has(lastUI.id);
|
|
3964
|
+
const matchedExisting = lastUI.id !== undefined && previouslyKnownMessageIds.has(lastUI.id);
|
|
3913
3965
|
if (!matchedExisting) {
|
|
3914
3966
|
turnNewUIMessages.push(lastUI);
|
|
3915
3967
|
const lastModel = (await toModelMessages([lastUI]))[0];
|
|
@@ -4244,7 +4296,8 @@ function chatAgent(options) {
|
|
|
4244
4296
|
// We call toUIMessageStream ourselves to attach onFinish for response capture.
|
|
4245
4297
|
// Pass originalMessages so the AI SDK reuses message IDs across turns
|
|
4246
4298
|
// (e.g. for tool approval continuations / HITL flows).
|
|
4247
|
-
if ((locals_js_1.locals.get(chatPipeCountKey) ?? 0) === 0 &&
|
|
4299
|
+
if ((locals_js_1.locals.get(chatPipeCountKey) ?? 0) === 0 &&
|
|
4300
|
+
isUIMessageStreamable(runResult)) {
|
|
4248
4301
|
onFinishAttached = true;
|
|
4249
4302
|
const resolvedOptions = resolveUIMessageStreamOptions();
|
|
4250
4303
|
// For action turns, don't pass originalMessages: the response
|
|
@@ -4257,9 +4310,7 @@ function chatAgent(options) {
|
|
|
4257
4310
|
// Pass originalMessages so the AI SDK reuses message IDs across
|
|
4258
4311
|
// turns (e.g. for tool approval continuations / HITL flows).
|
|
4259
4312
|
// Omit for action turns to force a fresh response ID.
|
|
4260
|
-
...(isActionTurn
|
|
4261
|
-
? {}
|
|
4262
|
-
: { originalMessages: accumulatedUIMessages }),
|
|
4313
|
+
...(isActionTurn ? {} : { originalMessages: accumulatedUIMessages }),
|
|
4263
4314
|
// Always provide generateMessageId so the start chunk carries a
|
|
4264
4315
|
// messageId. Without this, the frontend and backend generate IDs
|
|
4265
4316
|
// independently and they won't match for ID-based dedup.
|
|
@@ -4270,7 +4321,10 @@ function chatAgent(options) {
|
|
|
4270
4321
|
resolveOnFinish();
|
|
4271
4322
|
},
|
|
4272
4323
|
});
|
|
4273
|
-
await pipeChat(uiStream, {
|
|
4324
|
+
await pipeChat(uiStream, {
|
|
4325
|
+
signal: combinedSignal,
|
|
4326
|
+
spanName: "stream response",
|
|
4327
|
+
});
|
|
4274
4328
|
}
|
|
4275
4329
|
}
|
|
4276
4330
|
catch (error) {
|
|
@@ -4305,7 +4359,8 @@ function chatAgent(options) {
|
|
|
4305
4359
|
// never reports final usage), which would block the turn loop
|
|
4306
4360
|
// from ever firing onTurnComplete / writeTurnComplete.
|
|
4307
4361
|
let turnUsage;
|
|
4308
|
-
if (runResult != null &&
|
|
4362
|
+
if (runResult != null &&
|
|
4363
|
+
typeof runResult.totalUsage?.then === "function") {
|
|
4309
4364
|
try {
|
|
4310
4365
|
turnUsage = (await Promise.race([
|
|
4311
4366
|
runResult.totalUsage,
|
|
@@ -4391,7 +4446,10 @@ function chatAgent(options) {
|
|
|
4391
4446
|
// may produce a message with an empty ID since IDs are normally
|
|
4392
4447
|
// assigned by the frontend's useChat).
|
|
4393
4448
|
if (!capturedResponseMessage.id) {
|
|
4394
|
-
capturedResponseMessage = {
|
|
4449
|
+
capturedResponseMessage = {
|
|
4450
|
+
...capturedResponseMessage,
|
|
4451
|
+
id: (0, ai_runtime_js_1.generateId)(),
|
|
4452
|
+
};
|
|
4395
4453
|
}
|
|
4396
4454
|
// Append any non-transient data parts queued via chat.response or writer.write()
|
|
4397
4455
|
const queuedParts = locals_js_1.locals.get(chatResponsePartsKey);
|
|
@@ -4692,7 +4750,9 @@ function chatAgent(options) {
|
|
|
4692
4750
|
? { "gen_ai.usage.total_tokens": turnUsage.totalTokens }
|
|
4693
4751
|
: {}),
|
|
4694
4752
|
...(cumulativeUsage.totalTokens != null
|
|
4695
|
-
? {
|
|
4753
|
+
? {
|
|
4754
|
+
"gen_ai.usage.cumulative_total_tokens": cumulativeUsage.totalTokens,
|
|
4755
|
+
}
|
|
4696
4756
|
: {}),
|
|
4697
4757
|
},
|
|
4698
4758
|
});
|
|
@@ -4724,9 +4784,7 @@ function chatAgent(options) {
|
|
|
4724
4784
|
savedAt: Date.now(),
|
|
4725
4785
|
messages: accumulatedUIMessages,
|
|
4726
4786
|
lastOutEventId: turnCompleteResult?.lastEventId,
|
|
4727
|
-
lastInEventId: snapshotInCursor !== undefined
|
|
4728
|
-
? String(snapshotInCursor)
|
|
4729
|
-
: undefined,
|
|
4787
|
+
lastInEventId: snapshotInCursor !== undefined ? String(snapshotInCursor) : undefined,
|
|
4730
4788
|
});
|
|
4731
4789
|
}, {
|
|
4732
4790
|
attributes: {
|
|
@@ -4768,8 +4826,7 @@ function chatAgent(options) {
|
|
|
4768
4826
|
// chat.requestUpgrade() was called — exit the loop so the
|
|
4769
4827
|
// transport triggers a new run on the latest version.
|
|
4770
4828
|
// chat.endRun() — same exit, no upgrade semantics.
|
|
4771
|
-
if (locals_js_1.locals.get(chatUpgradeRequestedKey) ||
|
|
4772
|
-
locals_js_1.locals.get(chatEndRunRequestedKey)) {
|
|
4829
|
+
if (locals_js_1.locals.get(chatUpgradeRequestedKey) || locals_js_1.locals.get(chatEndRunRequestedKey)) {
|
|
4773
4830
|
return "exit";
|
|
4774
4831
|
}
|
|
4775
4832
|
// Wait for the next message — stay idle briefly, then suspend
|
|
@@ -4849,7 +4906,9 @@ function chatAgent(options) {
|
|
|
4849
4906
|
// Turn error handler: write an error chunk + turn-complete to the stream
|
|
4850
4907
|
// so the client sees the error, then wait for the next message instead
|
|
4851
4908
|
// of killing the entire run. This keeps the conversation alive.
|
|
4852
|
-
if (turnError instanceof Error &&
|
|
4909
|
+
if (turnError instanceof Error &&
|
|
4910
|
+
turnError.name === "AbortError" &&
|
|
4911
|
+
runSignal.aborted) {
|
|
4853
4912
|
// Full run cancellation — exit immediately
|
|
4854
4913
|
throw turnError;
|
|
4855
4914
|
}
|
|
@@ -4945,9 +5004,7 @@ function chatAgent(options) {
|
|
|
4945
5004
|
savedAt: Date.now(),
|
|
4946
5005
|
messages: erroredUIMessages,
|
|
4947
5006
|
lastOutEventId: errorTurnCompleteResult?.lastEventId,
|
|
4948
|
-
lastInEventId: errorSnapshotInCursor !== undefined
|
|
4949
|
-
? String(errorSnapshotInCursor)
|
|
4950
|
-
: undefined,
|
|
5007
|
+
lastInEventId: errorSnapshotInCursor !== undefined ? String(errorSnapshotInCursor) : undefined,
|
|
4951
5008
|
});
|
|
4952
5009
|
}
|
|
4953
5010
|
catch (error) {
|
|
@@ -4958,8 +5015,7 @@ function chatAgent(options) {
|
|
|
4958
5015
|
}
|
|
4959
5016
|
}
|
|
4960
5017
|
// chat.requestUpgrade() / chat.endRun() — exit after error turn too
|
|
4961
|
-
if (locals_js_1.locals.get(chatUpgradeRequestedKey) ||
|
|
4962
|
-
locals_js_1.locals.get(chatEndRunRequestedKey)) {
|
|
5018
|
+
if (locals_js_1.locals.get(chatUpgradeRequestedKey) || locals_js_1.locals.get(chatEndRunRequestedKey)) {
|
|
4963
5019
|
return;
|
|
4964
5020
|
}
|
|
4965
5021
|
// Drain remaining recovered turns before idling — a thrown
|
|
@@ -4992,7 +5048,7 @@ function chatAgent(options) {
|
|
|
4992
5048
|
// cleanup so a missing subscription doesn't throw.
|
|
4993
5049
|
stopSub?.off();
|
|
4994
5050
|
}
|
|
4995
|
-
}
|
|
5051
|
+
},
|
|
4996
5052
|
});
|
|
4997
5053
|
// Register clientDataSchema so the CLI converts it to JSONSchema
|
|
4998
5054
|
// and stores it as payloadSchema — used by the Playground UI
|
|
@@ -5084,7 +5140,7 @@ function createChatBuilder(config) {
|
|
|
5084
5140
|
agent(options) {
|
|
5085
5141
|
const mergedUiStream = config.uiStreamOptions && options.uiMessageStreamOptions
|
|
5086
5142
|
? { ...config.uiStreamOptions, ...options.uiMessageStreamOptions }
|
|
5087
|
-
: options.uiMessageStreamOptions ?? config.uiStreamOptions;
|
|
5143
|
+
: (options.uiMessageStreamOptions ?? config.uiStreamOptions);
|
|
5088
5144
|
return chatAgent({
|
|
5089
5145
|
...options,
|
|
5090
5146
|
...(config.clientDataSchema ? { clientDataSchema: config.clientDataSchema } : {}),
|
|
@@ -5912,7 +5968,9 @@ function createChatSession(payload, options) {
|
|
|
5912
5968
|
if (turn === 0 && currentPayload.trigger === "handover-prepare") {
|
|
5913
5969
|
const signal = await waitForHandover({
|
|
5914
5970
|
payload: currentPayload,
|
|
5915
|
-
idleTimeoutInSeconds: sessionIdleTimeoutOpt ??
|
|
5971
|
+
idleTimeoutInSeconds: sessionIdleTimeoutOpt ??
|
|
5972
|
+
currentPayload.idleTimeoutInSeconds ??
|
|
5973
|
+
idleTimeoutInSeconds,
|
|
5916
5974
|
timeout,
|
|
5917
5975
|
});
|
|
5918
5976
|
if (!signal || signal.kind === "handover-skip" || runSignal.aborted) {
|
|
@@ -5931,7 +5989,8 @@ function createChatSession(payload, options) {
|
|
|
5931
5989
|
// arrive with the sticky boot-payload fields stripped, so running
|
|
5932
5990
|
// a turn immediately would invoke the model with no user input.
|
|
5933
5991
|
const isMessagelessContinuationBoot = currentPayload.continuation === true && !currentPayload.message;
|
|
5934
|
-
if (turn === 0 &&
|
|
5992
|
+
if (turn === 0 &&
|
|
5993
|
+
(currentPayload.trigger === "preload" || isMessagelessContinuationBoot)) {
|
|
5935
5994
|
const result = await messagesInput.waitWithIdleTimeout({
|
|
5936
5995
|
idleTimeoutInSeconds: sessionIdleTimeoutOpt ?? currentPayload.idleTimeoutInSeconds ?? 30,
|
|
5937
5996
|
timeout,
|
|
@@ -5955,8 +6014,7 @@ function createChatSession(payload, options) {
|
|
|
5955
6014
|
// Subsequent turns: wait for the next message
|
|
5956
6015
|
if (turn > 0) {
|
|
5957
6016
|
// chat.requestUpgrade() / chat.endRun() — exit before waiting
|
|
5958
|
-
if (locals_js_1.locals.get(chatUpgradeRequestedKey) ||
|
|
5959
|
-
locals_js_1.locals.get(chatEndRunRequestedKey)) {
|
|
6017
|
+
if (locals_js_1.locals.get(chatUpgradeRequestedKey) || locals_js_1.locals.get(chatEndRunRequestedKey)) {
|
|
5960
6018
|
stop.cleanup();
|
|
5961
6019
|
return { done: true, value: undefined };
|
|
5962
6020
|
}
|
|
@@ -6203,7 +6261,8 @@ function createChatSession(payload, options) {
|
|
|
6203
6261
|
},
|
|
6204
6262
|
];
|
|
6205
6263
|
if (sessionCompaction.compactUIMessages) {
|
|
6206
|
-
accumulator.uiMessages =
|
|
6264
|
+
accumulator.uiMessages =
|
|
6265
|
+
await sessionCompaction.compactUIMessages(compactEvent);
|
|
6207
6266
|
}
|
|
6208
6267
|
}
|
|
6209
6268
|
}
|
|
@@ -6215,7 +6274,10 @@ function createChatSession(payload, options) {
|
|
|
6215
6274
|
// Append any non-transient data parts queued via chat.response or writer.write()
|
|
6216
6275
|
const queuedParts = locals_js_1.locals.get(chatResponsePartsKey);
|
|
6217
6276
|
if (queuedParts && queuedParts.length > 0) {
|
|
6218
|
-
response = {
|
|
6277
|
+
response = {
|
|
6278
|
+
...response,
|
|
6279
|
+
parts: [...(response.parts ?? []), ...queuedParts],
|
|
6280
|
+
};
|
|
6219
6281
|
locals_js_1.locals.set(chatResponsePartsKey, []);
|
|
6220
6282
|
}
|
|
6221
6283
|
await accumulator.addResponse(response);
|
|
@@ -6484,6 +6546,12 @@ function createChatStartSessionAction(taskId, options) {
|
|
|
6484
6546
|
if (!params.chatId) {
|
|
6485
6547
|
throw new Error("chat.createStartSessionAction: params.chatId is required — used as the session externalId.");
|
|
6486
6548
|
}
|
|
6549
|
+
// Scope the action to `apiClient`'s env: re-enter without it so the body
|
|
6550
|
+
// runs once, under that config (read via apiClientManager.accessToken/.baseURL).
|
|
6551
|
+
if (options?.apiClient) {
|
|
6552
|
+
const { apiClient, ...rest } = options;
|
|
6553
|
+
return v3_1.apiClientManager.runWithConfig(apiClient, () => createChatStartSessionAction(taskId, rest)(params));
|
|
6554
|
+
}
|
|
6487
6555
|
// The first run boots before the user's first message lands on
|
|
6488
6556
|
// `.in/append`, so it sees an empty `messages` array and `trigger:
|
|
6489
6557
|
// "preload"`. This matches the pre-Sessions preload semantics:
|
|
@@ -6591,9 +6659,7 @@ function createChatStartSessionAction(taskId, options) {
|
|
|
6591
6659
|
}
|
|
6592
6660
|
function resolveChatStartBaseURL(endpoint, chatId, option) {
|
|
6593
6661
|
const fallback = v3_1.apiClientManager.baseURL ?? "https://api.trigger.dev";
|
|
6594
|
-
const raw = typeof option === "function"
|
|
6595
|
-
? option({ endpoint, chatId })
|
|
6596
|
-
: option ?? fallback;
|
|
6662
|
+
const raw = typeof option === "function" ? option({ endpoint, chatId }) : (option ?? fallback);
|
|
6597
6663
|
return raw.replace(/\/$/, "");
|
|
6598
6664
|
}
|
|
6599
6665
|
function overrideRequestHeaders(accessToken) {
|