@trigger.dev/sdk 0.0.0-prerelease-streamfix-20260909094302 → 0.0.0-prerelease-20260909121655

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.
Files changed (142) hide show
  1. package/dist/commonjs/imports/ai-runtime-cjs.cjs.map +1 -1
  2. package/dist/commonjs/imports/ai-runtime.js +2 -0
  3. package/dist/commonjs/v3/ai-shared.d.ts +15 -0
  4. package/dist/commonjs/v3/ai-shared.js +35 -1
  5. package/dist/commonjs/v3/ai-shared.js.map +1 -1
  6. package/dist/commonjs/v3/ai.d.ts +275 -25
  7. package/dist/commonjs/v3/ai.js +1336 -340
  8. package/dist/commonjs/v3/ai.js.map +1 -1
  9. package/dist/commonjs/v3/chat-client.d.ts +11 -2
  10. package/dist/commonjs/v3/chat-client.js +227 -111
  11. package/dist/commonjs/v3/chat-client.js.map +1 -1
  12. package/dist/commonjs/v3/chat-react.d.ts +88 -0
  13. package/dist/commonjs/v3/chat-react.js +130 -1
  14. package/dist/commonjs/v3/chat-react.js.map +1 -1
  15. package/dist/commonjs/v3/chat-server.d.ts +42 -6
  16. package/dist/commonjs/v3/chat-server.js +52 -7
  17. package/dist/commonjs/v3/chat-server.js.map +1 -1
  18. package/dist/commonjs/v3/chat.d.ts +92 -10
  19. package/dist/commonjs/v3/chat.js +328 -73
  20. package/dist/commonjs/v3/chat.js.map +1 -1
  21. package/dist/commonjs/v3/chatSnapshotIo.d.ts +2 -0
  22. package/dist/commonjs/v3/chatSnapshotIo.js +174 -0
  23. package/dist/commonjs/v3/chatSnapshotIo.js.map +1 -0
  24. package/dist/commonjs/v3/chatVersionSkew.d.ts +12 -0
  25. package/dist/commonjs/v3/chatVersionSkew.js +30 -0
  26. package/dist/commonjs/v3/chatVersionSkew.js.map +1 -0
  27. package/dist/commonjs/v3/externalDeploymentId.d.ts +23 -0
  28. package/dist/commonjs/v3/externalDeploymentId.js +43 -0
  29. package/dist/commonjs/v3/externalDeploymentId.js.map +1 -0
  30. package/dist/commonjs/v3/sessions.d.ts +15 -2
  31. package/dist/commonjs/v3/sessions.js +12 -1
  32. package/dist/commonjs/v3/sessions.js.map +1 -1
  33. package/dist/commonjs/v3/shared.js +30 -36
  34. package/dist/commonjs/v3/shared.js.map +1 -1
  35. package/dist/commonjs/v3/test/index.d.ts +1 -0
  36. package/dist/commonjs/v3/test/index.js +3 -1
  37. package/dist/commonjs/v3/test/index.js.map +1 -1
  38. package/dist/commonjs/v3/test/mock-chat-agent.d.ts +47 -4
  39. package/dist/commonjs/v3/test/mock-chat-agent.js +116 -10
  40. package/dist/commonjs/v3/test/mock-chat-agent.js.map +1 -1
  41. package/dist/commonjs/v3/test/test-session-handle.js +6 -0
  42. package/dist/commonjs/v3/test/test-session-handle.js.map +1 -1
  43. package/dist/commonjs/v3/test/transcript-storage-tests.d.ts +40 -0
  44. package/dist/commonjs/v3/test/transcript-storage-tests.js +258 -0
  45. package/dist/commonjs/v3/test/transcript-storage-tests.js.map +1 -0
  46. package/dist/commonjs/v3/transcriptStorage.d.ts +224 -0
  47. package/dist/commonjs/v3/transcriptStorage.js +291 -0
  48. package/dist/commonjs/v3/transcriptStorage.js.map +1 -0
  49. package/dist/commonjs/version.js +1 -1
  50. package/dist/esm/imports/ai-runtime.d.ts +2 -2
  51. package/dist/esm/imports/ai-runtime.js +2 -2
  52. package/dist/esm/imports/ai-runtime.js.map +1 -1
  53. package/dist/esm/v3/ai-shared.d.ts +15 -0
  54. package/dist/esm/v3/ai-shared.js +33 -0
  55. package/dist/esm/v3/ai-shared.js.map +1 -1
  56. package/dist/esm/v3/ai.d.ts +275 -25
  57. package/dist/esm/v3/ai.js +1328 -337
  58. package/dist/esm/v3/ai.js.map +1 -1
  59. package/dist/esm/v3/chat-client.d.ts +11 -2
  60. package/dist/esm/v3/chat-client.js +228 -112
  61. package/dist/esm/v3/chat-client.js.map +1 -1
  62. package/dist/esm/v3/chat-react.d.ts +88 -0
  63. package/dist/esm/v3/chat-react.js +127 -1
  64. package/dist/esm/v3/chat-react.js.map +1 -1
  65. package/dist/esm/v3/chat-server.d.ts +42 -6
  66. package/dist/esm/v3/chat-server.js +53 -8
  67. package/dist/esm/v3/chat-server.js.map +1 -1
  68. package/dist/esm/v3/chat.d.ts +92 -10
  69. package/dist/esm/v3/chat.js +329 -74
  70. package/dist/esm/v3/chat.js.map +1 -1
  71. package/dist/esm/v3/chatSnapshotIo.d.ts +2 -0
  72. package/dist/esm/v3/chatSnapshotIo.js +166 -0
  73. package/dist/esm/v3/chatSnapshotIo.js.map +1 -0
  74. package/dist/esm/v3/chatVersionSkew.d.ts +12 -0
  75. package/dist/esm/v3/chatVersionSkew.js +27 -0
  76. package/dist/esm/v3/chatVersionSkew.js.map +1 -0
  77. package/dist/esm/v3/externalDeploymentId.d.ts +23 -0
  78. package/dist/esm/v3/externalDeploymentId.js +38 -0
  79. package/dist/esm/v3/externalDeploymentId.js.map +1 -0
  80. package/dist/esm/v3/sessions.d.ts +15 -2
  81. package/dist/esm/v3/sessions.js +11 -1
  82. package/dist/esm/v3/sessions.js.map +1 -1
  83. package/dist/esm/v3/shared.js +17 -23
  84. package/dist/esm/v3/shared.js.map +1 -1
  85. package/dist/esm/v3/test/index.d.ts +1 -0
  86. package/dist/esm/v3/test/index.js +1 -0
  87. package/dist/esm/v3/test/index.js.map +1 -1
  88. package/dist/esm/v3/test/mock-chat-agent.d.ts +47 -4
  89. package/dist/esm/v3/test/mock-chat-agent.js +118 -12
  90. package/dist/esm/v3/test/mock-chat-agent.js.map +1 -1
  91. package/dist/esm/v3/test/test-session-handle.js +6 -0
  92. package/dist/esm/v3/test/test-session-handle.js.map +1 -1
  93. package/dist/esm/v3/test/transcript-storage-tests.d.ts +40 -0
  94. package/dist/esm/v3/test/transcript-storage-tests.js +255 -0
  95. package/dist/esm/v3/test/transcript-storage-tests.js.map +1 -0
  96. package/dist/esm/v3/transcriptStorage.d.ts +224 -0
  97. package/dist/esm/v3/transcriptStorage.js +280 -0
  98. package/dist/esm/v3/transcriptStorage.js.map +1 -0
  99. package/dist/esm/version.js +1 -1
  100. package/docs/ai-chat/actions.mdx +55 -23
  101. package/docs/ai-chat/anatomy.mdx +3 -3
  102. package/docs/ai-chat/backend.mdx +125 -48
  103. package/docs/ai-chat/background-injection.mdx +69 -19
  104. package/docs/ai-chat/client-protocol.mdx +5 -4
  105. package/docs/ai-chat/compaction.mdx +13 -7
  106. package/docs/ai-chat/custom-agents.mdx +23 -0
  107. package/docs/ai-chat/fast-starts.mdx +27 -20
  108. package/docs/ai-chat/frontend.mdx +19 -14
  109. package/docs/ai-chat/lifecycle-hooks.mdx +6 -2
  110. package/docs/ai-chat/migrating-from-a-route-handler.mdx +16 -14
  111. package/docs/ai-chat/patterns/database-persistence.mdx +6 -2
  112. package/docs/ai-chat/patterns/native-compaction.mdx +310 -0
  113. package/docs/ai-chat/patterns/persistence-and-replay.mdx +34 -23
  114. package/docs/ai-chat/patterns/skills.mdx +7 -10
  115. package/docs/ai-chat/patterns/version-upgrades.mdx +79 -6
  116. package/docs/ai-chat/pending-messages.mdx +3 -3
  117. package/docs/ai-chat/prompt-caching.mdx +23 -25
  118. package/docs/ai-chat/quick-start.mdx +11 -11
  119. package/docs/ai-chat/reference.mdx +67 -9
  120. package/docs/ai-chat/sessions.mdx +6 -1
  121. package/docs/ai-chat/testing.mdx +2 -1
  122. package/docs/ai-chat/tools.mdx +18 -13
  123. package/docs/ai-chat/transcript-storage.mdx +233 -0
  124. package/docs/ai-chat/upgrade-guide.mdx +2 -2
  125. package/docs/apikeys.mdx +27 -45
  126. package/docs/config/extensions/syncEnvVars.mdx +6 -0
  127. package/docs/deploy-environment-variables.mdx +23 -2
  128. package/docs/deployment/overview.mdx +4 -8
  129. package/docs/deployment/preview-branches.mdx +4 -4
  130. package/docs/deployment/version-skew-protection.mdx +62 -0
  131. package/docs/manual-setup.mdx +7 -7
  132. package/docs/mcp-tools.mdx +9 -0
  133. package/docs/quick-start.mdx +3 -3
  134. package/docs/realtime/auth.mdx +1 -1
  135. package/docs/reports.mdx +157 -0
  136. package/docs/self-hosting/security.mdx +5 -0
  137. package/docs/tasks/scheduled.mdx +24 -0
  138. package/docs/triggering.mdx +1 -1
  139. package/docs/troubleshooting-zod.mdx +158 -0
  140. package/package.json +4 -4
  141. package/skills/trigger-authoring-chat-agent/SKILL.md +41 -29
  142. package/skills/trigger-chat-agent-advanced/SKILL.md +31 -12
@@ -0,0 +1,224 @@
1
+ import { type TaskRunContext, type TranscriptSnapshotEntry } from "@trigger.dev/core/v3";
2
+ import type { ModelMessage, UIMessage } from "ai";
3
+ /**
4
+ * One change to a transcript. Ops address messages by id; position is the
5
+ * storage's own concern.
6
+ *
7
+ * - `put` upserts by `message.id`: an unknown id appends, a known id is
8
+ * replaced in place. `final` is false when the runtime captured a partial
9
+ * assistant message from an errored or stopped turn; it defaults to true.
10
+ * - `remove` deletes by id and is a no-op for an unknown id.
11
+ * - `truncateAfter` drops every message ordered after `afterId` (a rollback)
12
+ * and is a no-op when `afterId` is unknown.
13
+ * - `state` replaces the opaque runtime record; `null` clears it.
14
+ */
15
+ export type TranscriptChange = {
16
+ op: "put";
17
+ message: UIMessage;
18
+ final?: boolean;
19
+ } | {
20
+ op: "remove";
21
+ id: string;
22
+ } | {
23
+ op: "truncateAfter";
24
+ afterId: string;
25
+ } | {
26
+ op: "state";
27
+ value: unknown | null;
28
+ };
29
+ export type TranscriptChangeReason = "turn-complete" | "turn-error" | "action" | "compaction" | "recovery";
30
+ export type TranscriptCursors = {
31
+ lastOutEventId?: string;
32
+ lastInEventId?: string;
33
+ };
34
+ /**
35
+ * What the runtime hands to `save`: the ordered changes since the last save
36
+ * (one transaction where the backend supports it), the whole transcript as
37
+ * it stands after those changes, and the stream cursors the next boot should
38
+ * resume from. A row-per-message store applies `changes`; a store that keeps
39
+ * the conversation as one document writes `transcript` as-is and needs no
40
+ * state of its own. Cursors are runtime-computed and persisted opaquely; a
41
+ * storage never interprets them.
42
+ */
43
+ export type TranscriptChangeset = {
44
+ reason: TranscriptChangeReason;
45
+ changes: TranscriptChange[];
46
+ transcript: TranscriptState;
47
+ cursors?: TranscriptCursors;
48
+ };
49
+ /** The tenant scope of a read. A render read has no run, so this is all `load` gets. */
50
+ export type TranscriptScope<TClientData = unknown> = {
51
+ chatId: string;
52
+ clientData: TClientData;
53
+ };
54
+ /** The run context of a write. Supplied by the runtime; always in-run. */
55
+ export type TranscriptStorageContext<TClientData = unknown> = TranscriptScope<TClientData> & {
56
+ turn: number;
57
+ trigger: "submit-message" | "regenerate-message" | "action";
58
+ runId: string;
59
+ ctx: TaskRunContext;
60
+ };
61
+ export type TranscriptLoadOptions = {
62
+ /** Return at most this many messages, the most recent ones. */
63
+ limit?: number;
64
+ /** Return messages ordered before this message id (a `nextCursor` from a previous page). */
65
+ before?: string;
66
+ };
67
+ export type TranscriptLoadResult<TUIMessage extends UIMessage = UIMessage> = {
68
+ messages: TUIMessage[];
69
+ state: unknown | null;
70
+ cursors?: TranscriptCursors;
71
+ /** The id to pass as `before` for the previous page; absent on the last page. */
72
+ nextCursor?: string;
73
+ /**
74
+ * Ids among `messages` that were saved with `final: false` (a partial
75
+ * answer). A storage that keeps `final` returns them so a partial stays
76
+ * partial when a continuation saves it again unchanged; a storage that
77
+ * does not keep `final` may leave this out.
78
+ */
79
+ nonFinalIds?: string[];
80
+ };
81
+ /** What `loadContext` receives on every turn and action. */
82
+ export type LoadContextEvent<TClientData = unknown, TUIMessage extends UIMessage = UIMessage> = {
83
+ chatId: string;
84
+ /** The turn number (0-indexed). */
85
+ turn: number;
86
+ trigger: "submit-message" | "regenerate-message" | "action";
87
+ /** The messages the frontend sent for this turn. Empty for actions. */
88
+ incomingMessages: TUIMessage[];
89
+ /** The runtime's transcript before this turn, including any tail it recovered. */
90
+ previousMessages: TUIMessage[];
91
+ clientData?: TClientData;
92
+ continuation: boolean;
93
+ previousRunId?: string;
94
+ };
95
+ /**
96
+ * A persistence adapter for a `chat.agent` transcript. The runtime calls
97
+ * `load` once at a continuation boot and `save` after every change to the
98
+ * conversation. Both are best-effort from the runtime's point of view: an
99
+ * error is logged and the turn continues.
100
+ *
101
+ * `loadContext` is optional. Its presence declares that the application
102
+ * owns the model's context: the runtime calls it on every turn and action
103
+ * and uses what it returns as the conversation, instead of the transcript
104
+ * it accumulated. Tail recovery still runs and `save` is still called.
105
+ */
106
+ export type TranscriptStorage<TClientData = unknown> = {
107
+ load<TUIMessage extends UIMessage = UIMessage>(scope: TranscriptScope<TClientData>, opts?: TranscriptLoadOptions): Promise<TranscriptLoadResult<TUIMessage>>;
108
+ save(ctx: TranscriptStorageContext<TClientData>, changeset: TranscriptChangeset): Promise<void>;
109
+ loadContext?<TUIMessage extends UIMessage = UIMessage>(scope: TranscriptScope<TClientData>, event: LoadContextEvent<TClientData, TUIMessage>): Promise<TUIMessage[]> | TUIMessage[];
110
+ };
111
+ /** An in-memory transcript: ordered entries plus the opaque state record. */
112
+ export type TranscriptState<TUIMessage extends UIMessage = UIMessage> = {
113
+ entries: TranscriptSnapshotEntry<TUIMessage>[];
114
+ state: unknown | null;
115
+ };
116
+ export declare function emptyTranscriptState<TUIMessage extends UIMessage = UIMessage>(): TranscriptState<TUIMessage>;
117
+ /**
118
+ * Apply changes to a transcript, in order. Pure: returns a new state and
119
+ * never mutates the input. Replaying the same changes converges, which is
120
+ * what lets a storage retry a failed write without checking what landed.
121
+ */
122
+ export declare function reduceTranscriptChanges<TUIMessage extends UIMessage = UIMessage>(prev: TranscriptState<TUIMessage>, changes: readonly TranscriptChange[]): TranscriptState<TUIMessage>;
123
+ /**
124
+ * What the runtime remembers about the transcript it last handed to
125
+ * `save`: the ids in order and a fingerprint per message, so the next
126
+ * changeset can be derived from the accumulator without asking the storage.
127
+ */
128
+ export type TranscriptShadow = {
129
+ ids: string[];
130
+ fingerprints: Map<string, string>;
131
+ /** Ids last saved with `final: false`. Everything else was saved final. */
132
+ nonFinal: Set<string>;
133
+ };
134
+ /**
135
+ * Build the shadow for `messages`. An id is non-final when this save says so
136
+ * (`nonFinalIds`) or when it was non-final before and its content has not
137
+ * changed: a partial answer stays partial until the message itself changes.
138
+ */
139
+ export declare function createTranscriptShadow(messages: readonly UIMessage[], nonFinalIds?: ReadonlySet<string>, previous?: TranscriptShadow): TranscriptShadow;
140
+ /**
141
+ * Derive the changes that take `shadow` to `next`.
142
+ *
143
+ * The common prefix (by id) is compared message by message and a changed
144
+ * message becomes an in-place `put`. Past the prefix, everything the shadow
145
+ * still had is dropped with one `truncateAfter` on the last common id (or
146
+ * one `remove` per message when there is no common prefix) and everything
147
+ * `next` has is appended with `put`, in order. Applying the result with
148
+ * {@link reduceTranscriptChanges} reproduces `next` exactly, including
149
+ * order, for any two lists.
150
+ */
151
+ export declare function diffTranscript(shadow: TranscriptShadow, next: readonly UIMessage[], options?: {
152
+ nonFinalIds?: ReadonlySet<string>;
153
+ }): {
154
+ changes: TranscriptChange[];
155
+ shadow: TranscriptShadow;
156
+ };
157
+ /**
158
+ * The built-in storage: the whole transcript as one versioned blob in the
159
+ * platform's object store, the same blob the Sessions dashboard renders.
160
+ *
161
+ * Stateless. Every changeset carries the whole transcript as it stands after
162
+ * the changes, so `save` serialises that and rewrites the blob: one PUT per
163
+ * turn, no read and nothing kept in memory between saves.
164
+ */
165
+ export declare function snapshotTranscriptStorage(): TranscriptStorage<unknown>;
166
+ /** The storage `chat.agent` uses when none is configured: {@link snapshotTranscriptStorage}. */
167
+ export declare const defaultStorage: TranscriptStorage<unknown>;
168
+ export type MemoryTranscriptStorage = TranscriptStorage<unknown> & {
169
+ /** The stored transcript for a chat, or `undefined` when nothing has been saved. */
170
+ transcript(chatId: string): (TranscriptState & {
171
+ cursors?: TranscriptCursors;
172
+ }) | undefined;
173
+ /** Every changeset `save` received, in order. */
174
+ readonly changesets: Array<{
175
+ ctx: TranscriptStorageContext<unknown>;
176
+ changeset: TranscriptChangeset;
177
+ }>;
178
+ };
179
+ /**
180
+ * A storage that keeps every transcript in process memory. The reference
181
+ * implementation for the conformance tests, and a way to inspect exactly
182
+ * what the runtime hands a storage.
183
+ */
184
+ export declare function memoryTranscriptStorage(): MemoryTranscriptStorage;
185
+ type ModelLaneInjection = {
186
+ afterId: string;
187
+ messages: ModelMessage[];
188
+ };
189
+ /**
190
+ * What the runtime keeps in the storage's `state` slot: the parts of the
191
+ * model's context that cannot be rebuilt from the transcript. Opaque to a
192
+ * storage; only the runtime reads it.
193
+ *
194
+ * `compaction` is the whole model lane after a compaction, covering the
195
+ * transcript prefix ending at `throughId`. It is used as long as `throughId`
196
+ * still exists in the transcript, so a rollback or truncation that removes it
197
+ * rebuilds the lane from the transcript instead. Editing a message before
198
+ * `throughId` in place is unsupported and does not invalidate the lane.
199
+ * `injections` are conversational messages `chat.inject` added, anchored
200
+ * after the transcript message they followed.
201
+ */
202
+ export type TranscriptRuntimeState = {
203
+ v: 1;
204
+ compaction?: {
205
+ modelMessages: ModelMessage[];
206
+ throughId: string;
207
+ };
208
+ injections?: ModelLaneInjection[];
209
+ /** `chat.inject` messages queued but not yet drained into a turn when the save happened. */
210
+ queued?: ModelMessage[];
211
+ };
212
+ export declare function parseTranscriptRuntimeState(value: unknown): TranscriptRuntimeState | undefined;
213
+ /**
214
+ * Rebuild the model lane for a transcript at boot. Uses the persisted
215
+ * compacted lane while the transcript still contains its `throughId`
216
+ * boundary, then converts the rest of the transcript, re-inserting persisted
217
+ * injections after the messages they followed.
218
+ */
219
+ export declare function restoreModelLane<TUIMessage extends UIMessage>(messages: TUIMessage[], state: TranscriptRuntimeState | undefined, convert: (messages: TUIMessage[]) => Promise<ModelMessage[]>): Promise<{
220
+ messages: ModelMessage[];
221
+ compacted: boolean;
222
+ injections: ModelLaneInjection[];
223
+ }>;
224
+ export {};
@@ -0,0 +1,280 @@
1
+ import { apiClientManager, pageTranscriptEntries, } from "@trigger.dev/core/v3";
2
+ import { readChatSnapshot, writeChatSnapshot } from "./chatSnapshotIo.js";
3
+ export function emptyTranscriptState() {
4
+ return { entries: [], state: null };
5
+ }
6
+ /**
7
+ * Apply changes to a transcript, in order. Pure: returns a new state and
8
+ * never mutates the input. Replaying the same changes converges, which is
9
+ * what lets a storage retry a failed write without checking what landed.
10
+ */
11
+ export function reduceTranscriptChanges(prev, changes) {
12
+ let entries = prev.entries.slice();
13
+ let state = prev.state;
14
+ for (const change of changes) {
15
+ switch (change.op) {
16
+ case "put": {
17
+ const message = change.message;
18
+ const entry = { id: message.id, final: change.final ?? true, message };
19
+ const idx = entries.findIndex((e) => e.id === message.id);
20
+ if (idx === -1)
21
+ entries.push(entry);
22
+ else
23
+ entries[idx] = entry;
24
+ break;
25
+ }
26
+ case "remove": {
27
+ entries = entries.filter((e) => e.id !== change.id);
28
+ break;
29
+ }
30
+ case "truncateAfter": {
31
+ const idx = entries.findIndex((e) => e.id === change.afterId);
32
+ if (idx !== -1)
33
+ entries = entries.slice(0, idx + 1);
34
+ break;
35
+ }
36
+ case "state": {
37
+ state = change.value ?? null;
38
+ break;
39
+ }
40
+ }
41
+ }
42
+ return { entries, state };
43
+ }
44
+ function fingerprintMessage(message) {
45
+ return JSON.stringify(message);
46
+ }
47
+ /**
48
+ * Build the shadow for `messages`. An id is non-final when this save says so
49
+ * (`nonFinalIds`) or when it was non-final before and its content has not
50
+ * changed: a partial answer stays partial until the message itself changes.
51
+ */
52
+ export function createTranscriptShadow(messages, nonFinalIds, previous) {
53
+ const ids = [];
54
+ const fingerprints = new Map();
55
+ const nonFinal = new Set();
56
+ for (const message of messages) {
57
+ const fingerprint = fingerprintMessage(message);
58
+ ids.push(message.id);
59
+ fingerprints.set(message.id, fingerprint);
60
+ if (nonFinalIds?.has(message.id) ||
61
+ (previous?.nonFinal.has(message.id) && previous.fingerprints.get(message.id) === fingerprint)) {
62
+ nonFinal.add(message.id);
63
+ }
64
+ }
65
+ return { ids, fingerprints, nonFinal };
66
+ }
67
+ /**
68
+ * Derive the changes that take `shadow` to `next`.
69
+ *
70
+ * The common prefix (by id) is compared message by message and a changed
71
+ * message becomes an in-place `put`. Past the prefix, everything the shadow
72
+ * still had is dropped with one `truncateAfter` on the last common id (or
73
+ * one `remove` per message when there is no common prefix) and everything
74
+ * `next` has is appended with `put`, in order. Applying the result with
75
+ * {@link reduceTranscriptChanges} reproduces `next` exactly, including
76
+ * order, for any two lists.
77
+ */
78
+ export function diffTranscript(shadow, next, options = {}) {
79
+ const nextShadow = createTranscriptShadow(next, options.nonFinalIds, shadow);
80
+ const changes = [];
81
+ const put = (message) => {
82
+ const final = nextShadow.nonFinal.has(message.id) ? false : undefined;
83
+ changes.push(final === undefined ? { op: "put", message } : { op: "put", message, final });
84
+ };
85
+ let lcp = 0;
86
+ while (lcp < shadow.ids.length && lcp < next.length && shadow.ids[lcp] === next[lcp].id) {
87
+ lcp++;
88
+ }
89
+ for (let i = 0; i < lcp; i++) {
90
+ const message = next[i];
91
+ if (shadow.fingerprints.get(message.id) !== nextShadow.fingerprints.get(message.id) ||
92
+ shadow.nonFinal.has(message.id) !== nextShadow.nonFinal.has(message.id)) {
93
+ put(message);
94
+ }
95
+ }
96
+ if (shadow.ids.length > lcp) {
97
+ if (lcp > 0) {
98
+ changes.push({ op: "truncateAfter", afterId: shadow.ids[lcp - 1] });
99
+ }
100
+ else {
101
+ for (const id of shadow.ids)
102
+ changes.push({ op: "remove", id });
103
+ }
104
+ }
105
+ for (let i = lcp; i < next.length; i++) {
106
+ put(next[i]);
107
+ }
108
+ return { changes, shadow: nextShadow };
109
+ }
110
+ /**
111
+ * The built-in storage: the whole transcript as one versioned blob in the
112
+ * platform's object store, the same blob the Sessions dashboard renders.
113
+ *
114
+ * Stateless. Every changeset carries the whole transcript as it stands after
115
+ * the changes, so `save` serialises that and rewrites the blob: one PUT per
116
+ * turn, no read and nothing kept in memory between saves.
117
+ */
118
+ export function snapshotTranscriptStorage() {
119
+ return {
120
+ async load(scope, opts) {
121
+ if (opts?.limit !== undefined || opts?.before !== undefined) {
122
+ const page = await readTranscriptPage(scope.chatId, opts);
123
+ if (page)
124
+ return page;
125
+ }
126
+ const snapshot = await readChatSnapshot(scope.chatId);
127
+ const full = snapshot
128
+ ? { entries: snapshot.messages, state: snapshot.state }
129
+ : emptyTranscriptState();
130
+ return {
131
+ ...pageEntries(full.entries, opts),
132
+ state: full.state,
133
+ cursors: snapshot
134
+ ? { lastOutEventId: snapshot.lastOutEventId, lastInEventId: snapshot.lastInEventId }
135
+ : undefined,
136
+ };
137
+ },
138
+ async save(ctx, changeset) {
139
+ await writeChatSnapshot(ctx.chatId, {
140
+ version: 2,
141
+ savedAt: Date.now(),
142
+ messages: changeset.transcript.entries,
143
+ state: changeset.transcript.state,
144
+ lastOutEventId: changeset.cursors?.lastOutEventId,
145
+ lastInEventId: changeset.cursors?.lastInEventId,
146
+ });
147
+ },
148
+ };
149
+ }
150
+ /**
151
+ * A page of the platform transcript read server-side, for a render read with
152
+ * `limit`/`before` from an app server holding a secret key. `undefined` when
153
+ * the call is not possible from here (a run's public token cannot use the
154
+ * endpoint), so the caller falls back to reading the whole blob.
155
+ */
156
+ async function readTranscriptPage(chatId, opts) {
157
+ try {
158
+ const page = await apiClientManager.clientOrThrow().getSessionTranscript(chatId, opts);
159
+ return {
160
+ messages: page.messages,
161
+ state: page.state ?? null,
162
+ cursors: page.cursors,
163
+ nextCursor: page.nextCursor,
164
+ };
165
+ }
166
+ catch {
167
+ return undefined;
168
+ }
169
+ }
170
+ /** The storage `chat.agent` uses when none is configured: {@link snapshotTranscriptStorage}. */
171
+ export const defaultStorage = snapshotTranscriptStorage();
172
+ function pageEntries(all, opts) {
173
+ const page = pageTranscriptEntries(all, opts);
174
+ return {
175
+ messages: page.entries.map((e) => e.message),
176
+ nextCursor: page.nextCursor,
177
+ nonFinalIds: page.entries.filter((e) => !e.final).map((e) => e.id),
178
+ };
179
+ }
180
+ /**
181
+ * A storage that keeps every transcript in process memory. The reference
182
+ * implementation for the conformance tests, and a way to inspect exactly
183
+ * what the runtime hands a storage.
184
+ */
185
+ export function memoryTranscriptStorage() {
186
+ const transcripts = new Map();
187
+ const changesets = [];
188
+ return {
189
+ changesets,
190
+ transcript(chatId) {
191
+ return transcripts.get(chatId);
192
+ },
193
+ async load(scope, opts) {
194
+ const stored = transcripts.get(scope.chatId);
195
+ if (!stored)
196
+ return { messages: [], state: null, cursors: undefined, nextCursor: undefined };
197
+ return {
198
+ ...pageEntries(stored.entries, opts),
199
+ state: stored.state,
200
+ cursors: stored.cursors,
201
+ };
202
+ },
203
+ async save(ctx, changeset) {
204
+ changesets.push({ ctx, changeset });
205
+ const prev = transcripts.get(ctx.chatId);
206
+ const next = reduceTranscriptChanges(prev ?? emptyTranscriptState(), changeset.changes);
207
+ transcripts.set(ctx.chatId, { ...next, cursors: changeset.cursors ?? prev?.cursors });
208
+ },
209
+ };
210
+ }
211
+ export function parseTranscriptRuntimeState(value) {
212
+ if (!value || typeof value !== "object")
213
+ return undefined;
214
+ const record = value;
215
+ if (record.v !== 1)
216
+ return undefined;
217
+ const out = { v: 1 };
218
+ const compaction = record.compaction;
219
+ if (compaction &&
220
+ typeof compaction === "object" &&
221
+ Array.isArray(compaction.modelMessages) &&
222
+ typeof compaction.throughId === "string") {
223
+ out.compaction = {
224
+ modelMessages: compaction.modelMessages,
225
+ throughId: compaction.throughId,
226
+ };
227
+ }
228
+ if (Array.isArray(record.injections)) {
229
+ out.injections = record.injections.flatMap((entry) => {
230
+ const inj = entry;
231
+ return inj && typeof inj.afterId === "string" && Array.isArray(inj.messages)
232
+ ? [{ afterId: inj.afterId, messages: inj.messages }]
233
+ : [];
234
+ });
235
+ }
236
+ if (Array.isArray(record.queued) && record.queued.length > 0) {
237
+ out.queued = record.queued;
238
+ }
239
+ return out;
240
+ }
241
+ /**
242
+ * Rebuild the model lane for a transcript at boot. Uses the persisted
243
+ * compacted lane while the transcript still contains its `throughId`
244
+ * boundary, then converts the rest of the transcript, re-inserting persisted
245
+ * injections after the messages they followed.
246
+ */
247
+ export async function restoreModelLane(messages, state, convert) {
248
+ const lane = [];
249
+ let start = 0;
250
+ let compacted = false;
251
+ if (state?.compaction) {
252
+ const throughId = state.compaction.throughId;
253
+ const idx = throughId === "" ? -1 : messages.findIndex((m) => m.id === throughId);
254
+ if (idx !== -1 || throughId === "") {
255
+ lane.push(...state.compaction.modelMessages);
256
+ start = idx + 1;
257
+ compacted = true;
258
+ }
259
+ }
260
+ const anchored = (state?.injections ?? [])
261
+ .map((inj) => ({
262
+ inj,
263
+ idx: inj.afterId === "" ? -1 : messages.findIndex((m) => m.id === inj.afterId),
264
+ }))
265
+ .filter(({ inj, idx }) => (inj.afterId === "" ? start === 0 : idx >= start))
266
+ .sort((a, b) => a.idx - b.idx);
267
+ let cursor = start;
268
+ for (const { inj, idx } of anchored) {
269
+ if (idx + 1 > cursor) {
270
+ lane.push(...(await convert(messages.slice(cursor, idx + 1))));
271
+ cursor = idx + 1;
272
+ }
273
+ lane.push(...inj.messages);
274
+ }
275
+ if (cursor < messages.length) {
276
+ lane.push(...(await convert(messages.slice(cursor))));
277
+ }
278
+ return { messages: lane, compacted, injections: anchored.map(({ inj }) => inj) };
279
+ }
280
+ //# sourceMappingURL=transcriptStorage.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"transcriptStorage.js","sourceRoot":"","sources":["../../../src/v3/transcriptStorage.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,gBAAgB,EAChB,qBAAqB,GAGtB,MAAM,sBAAsB,CAAC;AAE9B,OAAO,EAAE,gBAAgB,EAAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAC;AAgI1E,MAAM,UAAU,oBAAoB;IAGlC,OAAO,EAAE,OAAO,EAAE,EAAE,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC;AACtC,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,uBAAuB,CACrC,IAAiC,EACjC,OAAoC;IAEpC,IAAI,OAAO,GAAG,IAAI,CAAC,OAAO,CAAC,KAAK,EAAE,CAAC;IACnC,IAAI,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC;IACvB,KAAK,MAAM,MAAM,IAAI,OA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AAO,EAAE,QAAQ,EAAE,IAAI,EAAE,SAAS,EAAE,UAAU,EAAE,QAAQ,CAAC,GAAG,CAAC,CAAC,EAAE,GAAG,EAAE,EAAE,EAAE,CAAC,GAAG,CAAC,EAAE,CAAC;AACnF,CAAC"}
@@ -1,2 +1,2 @@
1
- export const VERSION = "0.0.0-prerelease-streamfix-20260909094302";
1
+ export const VERSION = "0.0.0-prerelease-20260909121655";
2
2
  //# sourceMappingURL=version.js.map
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  title: "Actions"
3
3
  sidebarTitle: "Actions"
4
- description: "Custom commands sent from the frontend that mutate chat state without consuming a turn — undo, rollback, edit, regenerate."
4
+ description: "Custom commands sent from the frontend that mutate chat state without consuming a turn: undo, rollback, edit, regenerate."
5
5
  ---
6
6
 
7
7
  ## Overview
@@ -44,7 +44,7 @@ export const myChat = chat.agent({
44
44
  // returning void → side-effect-only, no model call
45
45
  },
46
46
 
47
- run: async ({ messages, signal }) => {
47
+ run: async ({ messages, signal, streamText }) => {
48
48
  return streamText({ model: anthropic("claude-sonnet-4-5"), messages, abortSignal: signal });
49
49
  },
50
50
  });
@@ -52,36 +52,68 @@ export const myChat = chat.agent({
52
52
 
53
53
  **Lifecycle flow:** Wake → parse action against `actionSchema` → `hydrateMessages` (if set) → **`onAction`** → apply `chat.history` mutations → emit `trigger:turn-complete` → wait for next message.
54
54
 
55
- ## Returning a model response from an action
55
+ When `onAction` returns `chat.turn()`, the flow continues instead of emitting `trigger:turn-complete`: the edit is snapshotted, then a turn runs on the edited history with `trigger: "action-turn"`, so `onTurnStart`, `run()`, `onBeforeTurnComplete` and `onTurnComplete` all fire and the answer is persisted like any turn's. See [Answering after an action](#answering-after-an-action).
56
56
 
57
- `onAction` can return a `StreamTextResult`, `string`, or `UIMessage` to produce a response. The returned stream is auto-piped to the frontend just like a normal turn, but the rest of the turn machinery (`onTurnStart`, `onTurnComplete`, etc.) still does not fire.
57
+ ## Answering after an action
58
+
59
+ An action is a state edit. To answer after the edit, return `chat.turn()`: the edit is applied and snapshotted, then a turn runs on the edited history exactly as a message turn does. `onTurnStart`, `run()`, `onBeforeTurnComplete` and `onTurnComplete` fire, the turn counter advances, and the answer gets everything a turn has: the agent's system prompt and tools, steering, compaction, injected instructions and persistence.
58
60
 
59
61
  ```ts
60
- onAction: async ({ action, messages }) => {
61
- if (action.type === "regenerate") {
62
- chat.history.slice(0, -1); // drop the last assistant
63
- return streamText({
64
- model: anthropic("claude-sonnet-4-5"),
65
- messages,
66
- stopWhen: stepCountIs(15),
67
- });
62
+ onAction: async ({ action }) => {
63
+ switch (action.type) {
64
+ case "undo":
65
+ chat.history.slice(0, -2);
66
+ return; // edit only, no turn
67
+
68
+ case "regenerate":
69
+ chat.history.slice(0, -1);
70
+ return chat.turn(); // answer the edited history
71
+
72
+ case "retry-formal":
73
+ chat.history.slice(0, -1);
74
+ chat.inject([{ role: "system", content: "Answer formally this time." }]);
75
+ return chat.turn(); // with a one-shot instruction
68
76
  }
69
- // other actions return void → side-effect only
70
77
  }
71
78
  ```
72
79
 
73
- This is useful for actions that both mutate state and want a fresh model response (regenerate-from-here, retry-with-different-style). Persistence is your responsibility inside `onAction` itself; you have access to the streamed response object.
80
+ `run()` receives the edited history with no incoming user message, the same shape as a `regenerate-message` turn, and its `trigger` is `"action-turn"`, so a `run()` that returns early on `"action"` (the pre-May behaviour, when actions invoked `run()` directly) still answers. Returning anything other than `chat.turn()` or nothing is an error; a response can no longer be returned from `onAction` directly.
81
+
82
+ ### Actions and persistence
83
+
84
+ An action that returns nothing does not fire `onTurnComplete`, and that is where an app that owns its own transcript normally writes. What that means depends on which persistence model you use.
85
+
86
+ **Transcript storage** (the default, or your own `storage`): nothing to do. After an action that changed the conversation, the runtime hands the storage a changeset with `reason: "action"`. An undo is one `truncateAfter`; a regenerate is a `truncateAfter` followed by the new answer's `put` when the turn completes; an edit is a `put` for the edited id. The changeset carries the same resume cursors as the last turn. See [Transcript storage](/ai-chat/transcript-storage#what-the-runtime-saves). An action that returns `chat.turn()` is followed by a turn, which persists its answer the way every turn does.
87
+
88
+ **Your own store through the deprecated `hydrateMessages`**: the runtime deliberately does not write, because your store is the source of truth. A history edit lives only in the running worker until you persist it, and a continuation rehydrates from your store, not from what the worker had in memory. Mirror each edit in your store, not only additions: a regenerate is a delete *and* an insert. The answer that follows `chat.turn()` reaches your store through `onTurnComplete`, like any turn's answer.
89
+
90
+ ```ts
91
+ onAction: async ({ action, chatId }) => {
92
+ if (action.type === "undo") {
93
+ chat.history.slice(0, -2);
94
+ await db.deleteLastExchange(chatId); // the rollback is yours to persist
95
+ }
96
+ if (action.type === "regenerate") {
97
+ chat.history.slice(0, -1);
98
+ await db.deleteLastAssistant(chatId); // the delete half
99
+ return chat.turn(); // the insert half arrives in onTurnComplete
100
+ }
101
+ },
102
+ onTurnComplete: async ({ chatId, newUIMessages }) => {
103
+ await db.saveMessages(chatId, newUIMessages);
104
+ },
105
+ ```
74
106
 
75
107
  ## Gating actions on HITL state
76
108
 
77
109
  If you have a [human-in-the-loop](/ai-chat/patterns/human-in-the-loop) tool waiting on `addToolOutput`, you usually want to refuse competing actions like `regenerate` until the answer arrives. [`chat.history.getPendingToolCalls()`](/ai-chat/backend#chat-history) gives you exactly that signal:
78
110
 
79
111
  ```ts
80
- onAction: async ({ action, messages, signal }) => {
112
+ onAction: async ({ action }) => {
81
113
  if (action.type === "regenerate") {
82
114
  if (chat.history.getPendingToolCalls().length > 0) return; // gated
83
115
  chat.history.slice(0, -1);
84
- return streamText({ model: anthropic("claude-sonnet-4-5"), messages, abortSignal: signal });
116
+ return chat.turn();
85
117
  }
86
118
  },
87
119
  ```
@@ -89,10 +121,10 @@ onAction: async ({ action, messages, signal }) => {
89
121
  ## Sending actions from the frontend
90
122
 
91
123
  ```ts
92
- // Browser — TriggerChatTransport
124
+ // Browser: TriggerChatTransport
93
125
  const stream = await transport.sendAction(chatId, { type: "undo" });
94
126
 
95
- // Server — AgentChat
127
+ // Server: AgentChat
96
128
  const stream = await agentChat.sendAction({ type: "rollback", targetMessageId: "msg-3" });
97
129
  ```
98
130
 
@@ -104,8 +136,8 @@ The action payload is validated against `actionSchema` on the backend; invalid a
104
136
 
105
137
  ## See also
106
138
 
107
- - [`chat.history`](/ai-chat/backend#chat-history) — the imperative API actions use to mutate state
108
- - [Sending actions from the frontend](/ai-chat/frontend#sending-actions) — `transport.sendAction` ergonomics
109
- - [`hydrateMessages`](/ai-chat/lifecycle-hooks#hydratemessages) — fires before `onAction` when set
110
- - [Branching conversations](/ai-chat/patterns/branching-conversations) — pairs action handlers with backend-controlled history
111
- - [Human-in-the-loop](/ai-chat/patterns/human-in-the-loop) — gating fresh actions while a tool is waiting
139
+ - [`chat.history`](/ai-chat/backend#chat-history): the imperative API actions use to mutate state
140
+ - [Sending actions from the frontend](/ai-chat/frontend#sending-actions): sending actions through `useChat` so a turn that follows one renders like any turn
141
+ - [`hydrateMessages`](/ai-chat/lifecycle-hooks#hydratemessages): fires before `onAction` when set
142
+ - [Branching conversations](/ai-chat/patterns/branching-conversations): pairs action handlers with backend-controlled history
143
+ - [Human-in-the-loop](/ai-chat/patterns/human-in-the-loop): gating fresh actions while a tool is waiting