Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/chat-custom-agent-end-and-continue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@trigger.dev/sdk": patch
---

Add `chat.endAndContinue()` so fully hand-rolled custom chat agents can hand a conversation off to a fresh run on the latest deployed task version while preserving unconsumed Session input.
24 changes: 24 additions & 0 deletions docs/ai-chat/custom-agents.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,29 @@ for await (const turn of session) {

Without this, a resumed chat silently loses its history: the model sees only the message that triggered the continuation. In a hand-rolled loop, seed by passing the stored history into the turn-0 `addIncoming` call — shown in the example below.

### Rotating to a new deployment

With `chat.createSession()`, use `chat.requestUpgrade()` to leave the current run after the turn. In a fully hand-rolled custom agent, use `chat.endAndContinue()` to immediately hand the Session to a fresh run.

Call it between turns, after detaching the old run's input listeners. If the old run completed its current turn, persist its state and write the turn-complete boundary before the handoff:

```ts
// Detach any chat.messages.on() subscriptions you created.
stop.cleanup();
await persistMessages(conversation.uiMessages);
await chat.writeTurnComplete();
await chat.endAndContinue();
return;
```
Comment thread
coderabbitai[bot] marked this conversation as resolved.

The server starts a continuation run using the Session's existing trigger configuration and atomically makes it the current run. The Session and its streams stay open, so input that the old run has not consumed remains on `.in` for the continuation run. The new run uses the latest deployed task version unless the Session's trigger configuration sets `lockToVersion`.

If input has been dispatched to the old run but should be processed by the continuation, detach the listeners and do not write another turn-complete boundary before handing off. `chat.writeTurnComplete()` acknowledges the latest input dispatched to the old run; writing it after that dispatch would make the continuation resume after the input.

<Warning>
`chat.endAndContinue()` starts the new run but does not stop the caller. Await it and return from `run()` immediately; continuing to read or write can race the new run on the same Session. If the handoff fails, the promise rejects.
</Warning>

### turn.complete() vs manual control

`turn.complete(result)` is the one-call path — it handles piping, capturing the response, accumulating messages, cleaning up aborted parts on a stop, and writing the turn-complete chunk.
Expand Down Expand Up @@ -217,6 +240,7 @@ For full control, skip `createSession` and compose the primitives directly:
| `chat.createStopSignal()` | Create a managed stop signal wired to the stop input stream |
| `chat.pipeAndCapture(result)` | Pipe a stream and capture the response; returns `{ message, status, error }` |
| `chat.writeTurnComplete()` | Signal turn complete; returns `{ lastEventId, sessionInEventId }` resume cursors |
| `chat.endAndContinue()` | Hand off the Session to a continuation run; call between turns, then return |
| `chat.MessageAccumulator` | Accumulates conversation messages across turns |
| `chat.pipe(stream)` | Pipe a stream to the frontend (no response capture) |
| `chat.cleanupAbortedParts(msg)` | Clean up incomplete parts from a stopped response |
Expand Down
30 changes: 24 additions & 6 deletions docs/ai-chat/patterns/version-upgrades.mdx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
---
title: "Version upgrades"
sidebarTitle: "Version upgrades"
description: "Gracefully migrate suspended chat agents to a new deployment using chat.requestUpgrade() and the continuation mechanism."
description: "Gracefully migrate chat agents to a new deployment using chat.requestUpgrade(), chat.endAndContinue(), and the continuation mechanism."
---

Chat agent runs are pinned to the worker version they started on. When you deploy a new version, suspended runs resume on the **old** code. If your deploy includes breaking changes (new tools, changed schemas, updated API contracts), this can cause issues.

`chat.requestUpgrade()` lets the agent opt out of the current run so the transport triggers a new one on the latest version.
`chat.requestUpgrade()` is the managed upgrade signal for `chat.agent()` and the `chat.createSession()` iterator. Fully hand-rolled custom agents use `chat.endAndContinue()` between turns to immediately hand the Session to a new run.

## How it works

Expand Down Expand Up @@ -151,14 +151,32 @@ export const myChat = chat

This upgrades on **every** deploy, not just breaking changes. Good for fast-moving projects where you always want the latest code.

## Other agent types
## Custom agents

- **`chat.agent()`** and **`chat.createSession()`** — use `chat.requestUpgrade()` as shown above
- **`chat.customAgent()`** — you control the turn loop, so just `return` from `run()` when you want to exit
Use `chat.requestUpgrade()` with `chat.agent()` and the `chat.createSession()` iterator. In a fully hand-rolled `chat.customAgent()` task, detach input listeners, persist the completed turn, write its boundary, then call `chat.endAndContinue()` and return immediately:

```ts
// Detach any chat.messages.on() subscriptions you created.
stop.cleanup();
await persistMessages(conversation.uiMessages);
await chat.writeTurnComplete();
await chat.endAndContinue();
return;
```

The continuation uses the same durable Session and receives `.in` records that the old run has not consumed. It starts on the latest deployed task version unless the Session's trigger configuration sets `lockToVersion`.

If input has been dispatched to the old run but should be processed by the continuation, detach the old listeners and skip the final `chat.writeTurnComplete()`. A turn-complete boundary acknowledges the latest input dispatched to the old run, so writing one after that dispatch would cause the continuation to resume past the input.

<Warning>
`chat.endAndContinue()` starts the successor but does not stop the calling run. Perform no more Session reads or writes after calling it, and return from the task. If the handoff fails, the promise rejects.
</Warning>

## Interaction with recovery boot

`chat.requestUpgrade()` is a graceful exit — the old run returns cleanly, never writing a partial assistant. The new continuation run boots with an empty `session.out` tail and the upgrade-trigger message on `session.in`. The trigger message dispatches as turn 1 on the new version via the normal continuation-wait path. [`onRecoveryBoot`](/ai-chat/patterns/recovery-boot) does NOT fire on this path — the hook is reserved for mid-stream interruptions (cancel / crash / OOM) where a partial assistant exists on the tail.
When `chat.requestUpgrade()` is handled before a turn starts, the SDK immediately hands the Session to a new run, which processes the same input on the latest version. When it is requested during a turn, including through `chat.createSession()`, the current turn finishes and the old run exits; the next input starts the continuation run.

Both are graceful exits. [`onRecoveryBoot`](/ai-chat/patterns/recovery-boot) does not fire — the hook is reserved for mid-stream interruptions (cancel, crash, or OOM) where a partial assistant exists on the tail.

## See also

Expand Down
1 change: 1 addition & 0 deletions docs/ai-chat/reference.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,7 @@ All methods available on the `chat` object from `@trigger.dev/sdk/ai`.
| `chat.createStartSessionAction(taskId, options?)` | Returns a server action that creates a chat Session + triggers the first run + returns a session-scoped PAT. Idempotent on `(env, externalId)`. |
| `chat.waitForHandover(options)` | Wait for a [`chat.headStart`](/ai-chat/fast-starts#handover-with-custom-agents) handover signal in a custom loop. Returns the signal or `null`. `chat.MessageAccumulator` wraps this as `consumeHandover()` / `applyHandover()` |
| `chat.requestUpgrade()` | End the current run after this turn so the next message starts on the latest agent version. Server-orchestrated handoff. |
| `chat.endAndContinue()` | In a hand-rolled custom agent, hand off the Session to a fresh continuation run. Call between turns after detaching input listeners, then return immediately. The promise rejects if the handoff fails. |
| `chat.setTurnTimeout(duration)` | Override turn timeout at runtime (e.g. `"2h"`) |
| `chat.setTurnTimeoutInSeconds(seconds)` | Override turn timeout at runtime (in seconds) |
| `chat.setIdleTimeoutInSeconds(seconds)` | Override idle timeout at runtime |
Expand Down
81 changes: 62 additions & 19 deletions packages/trigger-sdk/src/v3/ai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2538,6 +2538,8 @@ const chatOnCompactedKey =
locals.create<(event: CompactedEvent) => Promise<void> | void>("chat.onCompacted");
/** @internal Full task `ctx` for the active `chat.agent` run (for hooks invoked from nested compaction). */
const chatAgentRunContextKey = locals.create<TaskRunContext>("chat.agentRunContext");
/** @internal Marks the root run created by `chat.customAgent()`. */
const chatCustomAgentRunKey = locals.create<boolean>("chat.customAgentRun");
const chatPrepareMessagesKey =
locals.create<(event: PrepareMessagesEvent<unknown>) => ModelMessage[] | Promise<ModelMessage[]>>(
"chat.prepareMessages"
Expand Down Expand Up @@ -5362,6 +5364,7 @@ function chatCustomAgent<
locals.set(chatSessionHandleKey, sessions.open(payload.chatId));
locals.set(chatExternalIdKey, payload.chatId);
locals.set(chatAgentRunContextKey, runOptions.ctx);
locals.set(chatCustomAgentRunKey, true);
// Initialize the turn-complete trim slot so `chat.writeTurnComplete`
// trims `session.out` back to the previous turn boundary. Without
// this the slot is undefined and the trim never runs, so `.out`
Expand Down Expand Up @@ -5456,6 +5459,7 @@ function chatAgent<
{ signal: runSignal, ctx }
) => {
locals.set(chatAgentRunContextKey, ctx);
locals.set(chatCustomAgentRunKey, false);

// On AI SDK 7, register the `@ai-sdk/otel` integration (once per process)
// so `experimental_telemetry` spans flow into the run trace. Awaited here
Expand Down Expand Up @@ -8705,6 +8709,59 @@ function requestUpgrade(): void {
locals.set(chatUpgradeRequestedKey, true);
}

/**
* Hand off the current custom agent Session to a fresh run.
*
* This is the low-level handoff for a fully hand-rolled
* `chat.customAgent()` loop. (Use {@link requestUpgrade} with
* `chat.createSession()` instead.) Call only between turns and after detaching
* input listeners for the old run. If the old run completed its current turn,
* persist its state and call {@link chatWriteTurnComplete} before handing off.
* Do not write a new turn boundary after input that the continuation run should
* process has been dispatched: the boundary acknowledges that input.
*
* The server starts the continuation run but does not stop this run, so return
* from the task immediately after awaiting this function. The promise rejects
* if the server cannot complete the handoff.
*
* Pending Session input that the old run has not consumed remains on the
* durable `.in` stream and is delivered to the continuation run.
*
* @example
* ```ts
* // Detach any chat.messages.on() subscriptions you created.
* await persistMessages();
* await chat.writeTurnComplete();
* await chat.endAndContinue();
* return;
* ```
*/
async function endAndContinue(): Promise<void> {
if (locals.get(chatCustomAgentRunKey) !== true) {
throw new Error(
"chat.endAndContinue() can only be called from inside a chat.customAgent() run"
);
}

await performEndAndContinue();
}

/** @internal Shared server handoff used by managed and custom agent loops. */
async function performEndAndContinue(): Promise<void> {
const chatId = locals.get(chatExternalIdKey);
const callingRunId = locals.get(chatAgentRunContextKey)?.run.id;

if (!chatId || !callingRunId) {
throw new Error("Cannot end and continue without an active chat agent run");
}

const apiClient = apiClientManager.clientOrThrow();
await apiClient.endAndContinueSession(chatId, {
callingRunId,
reason: "upgrade",
});
}

/**
* Exit the run after the current turn completes, without waiting for the
* next message. Unlike {@link requestUpgrade}, no upgrade-required signal
Expand Down Expand Up @@ -10697,6 +10754,8 @@ export const chat = {
isStopped,
/** Request that the run exits after the current turn so the next message starts on the latest version. See {@link requestUpgrade}. */
requestUpgrade,
/** Hand off a custom agent Session to a fresh run. See {@link endAndContinue}. */
endAndContinue,
/** Exit the run after the current turn completes, without any upgrade signal. See {@link endRun}. */
endRun,
/** Clean up aborted parts from a UIMessage. See {@link cleanupAbortedParts}. */
Expand Down Expand Up @@ -10891,17 +10950,12 @@ async function writeTurnCompleteChunk(
* @internal
*/
async function writeUpgradeRequiredChunk(): Promise<StreamWriteResult> {
const ctx = taskContext.ctx;
const chatId = ctx?.run.id ? getChatIdFromContext() : undefined;
const callingRunId = ctx?.run.id;
const chatId = locals.get(chatExternalIdKey);
const callingRunId = locals.get(chatAgentRunContextKey)?.run.id;
Comment on lines 10952 to +10954

@devin-ai-integration devin-ai-integration Bot Aug 17, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Info: chatId/callingRunId resolution swap is behaviour-preserving

writeUpgradeRequiredChunk now resolves chatId from chatExternalIdKey instead of the session handle's id (the deleted getChatIdFromContext). Both are equivalent at every call site: chatSessionHandleKey is set as sessions.open(payload.chatId) in packages/trigger-sdk/src/v3/ai.ts:5364 and :5480, and SessionHandle.id is exactly the constructor argument (packages/trigger-sdk/src/v3/sessions.ts:248-254), i.e. payload.chatId — the same value stored in chatExternalIdKey. Similarly, chatAgentRunContextKey.run.id is runOptions.ctx.run.id, matching the previous taskContext.ctx?.run.id. The one contextual difference (the subtask/tool fallback in getChatSession() sets chatSessionHandleKey but not chatExternalIdKey) is not reachable from either writeUpgradeRequiredChunk call site (:7088, :9825), both of which live inside the agent loops that seed both keys.

Open in Devin Review

Was this helpful? React with 👍 or 👎 to provide feedback.


if (chatId && callingRunId) {
const apiClient = apiClientManager.clientOrThrow();
try {
await apiClient.endAndContinueSession(chatId, {
callingRunId,
reason: "upgrade",
});
await performEndAndContinue();
} catch (error) {
// Non-fatal: the next `.in/append` re-triggers via the probe.
// Swallow rather than throw so we still emit the chunk + exit.
Expand All @@ -10917,17 +10971,6 @@ async function writeUpgradeRequiredChunk(): Promise<StreamWriteResult> {
return session.out.writeControl(TRIGGER_CONTROL_SUBTYPE.UPGRADE_REQUIRED);
}

/**
* Resolves the current chat's `chatId` (used as session externalId) from
* the bound session handle. Returns `undefined` if no agent is bound —
* shouldn't happen at the call sites that invoke
* `writeUpgradeRequiredChunk`, but defensive against misuse.
* @internal
*/
function getChatIdFromContext(): string | undefined {
return locals.get(chatSessionHandleKey)?.id;
}

/**
* Extracts the text content of the last user message from a UIMessage array.
* Returns undefined if no user message is found.
Expand Down
Loading
Loading