You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/ai-chat/custom-agents.mdx
+20Lines changed: 20 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -144,6 +144,25 @@ for await (const turn of session) {
144
144
145
145
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.
146
146
147
+
### Rotating to a new deployment
148
+
149
+
`chat.createSession()` consumes `chat.requestUpgrade()` through its managed iterator. In a fully hand-rolled custom agent, hand the Session to a fresh run with `chat.endAndContinue()`. Finish the current turn and persist its state first, then make the handoff the last operation in `run()`:
150
+
151
+
```ts
152
+
awaitchat.writeTurnComplete();
153
+
awaitpersistMessages(conversation.uiMessages);
154
+
155
+
if (shouldRotateToLatestVersion()) {
156
+
returnchat.endAndContinue();
157
+
}
158
+
```
159
+
160
+
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`.
161
+
162
+
<Warning>
163
+
Call `chat.endAndContinue()` only at a completed turn boundary, after `chat.writeTurnComplete()` and after detaching the old run's input listeners. The operation 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.
164
+
</Warning>
165
+
147
166
### turn.complete() vs manual control
148
167
149
168
`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.
@@ -217,6 +236,7 @@ For full control, skip `createSession` and compose the primitives directly:
217
236
|`chat.createStopSignal()`| Create a managed stop signal wired to the stop input stream |
218
237
|`chat.pipeAndCapture(result)`| Pipe a stream and capture the response; returns `{ message, status, error }`|
Copy file name to clipboardExpand all lines: docs/ai-chat/patterns/version-upgrades.mdx
+16-5Lines changed: 16 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,12 +1,12 @@
1
1
---
2
2
title: "Version upgrades"
3
3
sidebarTitle: "Version upgrades"
4
-
description: "Gracefully migrate suspended chat agents to a new deployment using chat.requestUpgrade() and the continuation mechanism."
4
+
description: "Gracefully migrate chat agents to a new deployment using chat.requestUpgrade(), chat.endAndContinue(), and the continuation mechanism."
5
5
---
6
6
7
7
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.
8
8
9
-
`chat.requestUpgrade()` lets the agent opt out of the current run so the transport triggers a new one on the latest version.
9
+
`chat.requestUpgrade()` lets `chat.agent()` and the `chat.createSession()` iterator opt out of the current run so the transport triggers a new one on the latest version. Fully hand-rolled custom agents use `chat.endAndContinue()` at a completed turn boundary for the same Session handoff.
10
10
11
11
## How it works
12
12
@@ -151,10 +151,21 @@ export const myChat = chat
151
151
152
152
This upgrades on **every** deploy, not just breaking changes. Good for fast-moving projects where you always want the latest code.
153
153
154
-
## Other agent types
154
+
## Custom agents
155
155
156
-
-**`chat.agent()`** and **`chat.createSession()`** — use `chat.requestUpgrade()` as shown above
157
-
-**`chat.customAgent()`** — you control the turn loop, so just `return` from `run()` when you want to exit
156
+
`chat.requestUpgrade()` is consumed by both `chat.agent()` and the `chat.createSession()` iterator. In a fully hand-rolled `chat.customAgent()` task, finish the turn, persist any application state, then call `chat.endAndContinue()` and return immediately:
157
+
158
+
```ts
159
+
awaitchat.writeTurnComplete();
160
+
awaitpersistMessages(conversation.uiMessages);
161
+
returnchat.endAndContinue();
162
+
```
163
+
164
+
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`.
165
+
166
+
<Warning>
167
+
`chat.endAndContinue()` starts the successor but does not stop the calling run. Call it only after `chat.writeTurnComplete()` and after detaching the old run's input listeners, then perform no more Session reads or writes and return from the task.
Copy file name to clipboardExpand all lines: docs/ai-chat/reference.mdx
+1Lines changed: 1 addition & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -511,6 +511,7 @@ All methods available on the `chat` object from `@trigger.dev/sdk/ai`.
511
511
|`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)`. |
512
512
|`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()`|
513
513
|`chat.requestUpgrade()`| End the current run after this turn so the next message starts on the latest agent version. Server-orchestrated handoff. |
514
+
|`chat.endAndContinue()`| In a hand-rolled custom agent, hand off the Session to a fresh continuation run. Finish the turn, detach input listeners, call this method, then return immediately. |
514
515
|`chat.setTurnTimeout(duration)`| Override turn timeout at runtime (e.g. `"2h"`) |
515
516
|`chat.setTurnTimeoutInSeconds(seconds)`| Override turn timeout at runtime (in seconds) |
516
517
|`chat.setIdleTimeoutInSeconds(seconds)`| Override idle timeout at runtime |
0 commit comments