@trigger.dev/sdk 0.0.0-prerelease-20260327224514 → 0.0.0-prerelease-20260331093145

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 (57) hide show
  1. package/dist/commonjs/v3/ai.d.ts +5 -1798
  2. package/dist/commonjs/v3/ai.js +13 -2882
  3. package/dist/commonjs/v3/ai.js.map +1 -1
  4. package/dist/commonjs/v3/index.d.ts +2 -2
  5. package/dist/commonjs/v3/runs.d.ts +2 -9
  6. package/dist/commonjs/v3/runs.js +0 -1
  7. package/dist/commonjs/v3/runs.js.map +1 -1
  8. package/dist/commonjs/v3/shared.d.ts +2 -24
  9. package/dist/commonjs/v3/shared.js +1 -160
  10. package/dist/commonjs/v3/shared.js.map +1 -1
  11. package/dist/commonjs/v3/streams.js +19 -73
  12. package/dist/commonjs/v3/streams.js.map +1 -1
  13. package/dist/commonjs/v3/tasks.d.ts +1 -2
  14. package/dist/commonjs/v3/tasks.js +0 -1
  15. package/dist/commonjs/v3/tasks.js.map +1 -1
  16. package/dist/commonjs/version.js +1 -1
  17. package/dist/esm/v3/ai.d.ts +5 -1798
  18. package/dist/esm/v3/ai.js +14 -2883
  19. package/dist/esm/v3/ai.js.map +1 -1
  20. package/dist/esm/v3/index.d.ts +2 -2
  21. package/dist/esm/v3/runs.d.ts +0 -7
  22. package/dist/esm/v3/runs.js +0 -1
  23. package/dist/esm/v3/runs.js.map +1 -1
  24. package/dist/esm/v3/shared.d.ts +2 -24
  25. package/dist/esm/v3/shared.js +1 -159
  26. package/dist/esm/v3/shared.js.map +1 -1
  27. package/dist/esm/v3/streams.js +19 -73
  28. package/dist/esm/v3/streams.js.map +1 -1
  29. package/dist/esm/v3/tasks.d.ts +1 -2
  30. package/dist/esm/v3/tasks.js +1 -2
  31. package/dist/esm/v3/tasks.js.map +1 -1
  32. package/dist/esm/version.js +1 -1
  33. package/package.json +6 -41
  34. package/dist/commonjs/v3/chat-constants.d.ts +0 -10
  35. package/dist/commonjs/v3/chat-constants.js +0 -14
  36. package/dist/commonjs/v3/chat-constants.js.map +0 -1
  37. package/dist/commonjs/v3/chat-react.d.ts +0 -135
  38. package/dist/commonjs/v3/chat-react.js +0 -248
  39. package/dist/commonjs/v3/chat-react.js.map +0 -1
  40. package/dist/commonjs/v3/chat.d.ts +0 -346
  41. package/dist/commonjs/v3/chat.js +0 -616
  42. package/dist/commonjs/v3/chat.js.map +0 -1
  43. package/dist/commonjs/v3/chat.test.d.ts +0 -1
  44. package/dist/commonjs/v3/chat.test.js +0 -1855
  45. package/dist/commonjs/v3/chat.test.js.map +0 -1
  46. package/dist/esm/v3/chat-constants.d.ts +0 -10
  47. package/dist/esm/v3/chat-constants.js +0 -11
  48. package/dist/esm/v3/chat-constants.js.map +0 -1
  49. package/dist/esm/v3/chat-react.d.ts +0 -135
  50. package/dist/esm/v3/chat-react.js +0 -244
  51. package/dist/esm/v3/chat-react.js.map +0 -1
  52. package/dist/esm/v3/chat.d.ts +0 -346
  53. package/dist/esm/v3/chat.js +0 -611
  54. package/dist/esm/v3/chat.js.map +0 -1
  55. package/dist/esm/v3/chat.test.d.ts +0 -1
  56. package/dist/esm/v3/chat.test.js +0 -1853
  57. package/dist/esm/v3/chat.test.js.map +0 -1
@@ -1,105 +1,28 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.chat = exports.PENDING_MESSAGE_INJECTED_TYPE = exports.CHAT_STOP_STREAM_ID = exports.CHAT_MESSAGES_STREAM_ID = exports.CHAT_STREAM_KEY = exports.ai = void 0;
3
+ exports.ai = void 0;
4
4
  const v3_1 = require("@trigger.dev/core/v3");
5
5
  const ai_1 = require("ai");
6
- const api_1 = require("@opentelemetry/api");
7
- const auth_js_1 = require("./auth.js");
8
- const locals_js_1 = require("./locals.js");
9
6
  const metadata_js_1 = require("./metadata.js");
10
- const streams_js_1 = require("./streams.js");
11
- const shared_js_1 = require("./shared.js");
12
- const tracer_js_1 = require("./tracer.js");
13
- const chat_constants_js_1 = require("./chat-constants.js");
14
- Object.defineProperty(exports, "CHAT_MESSAGES_STREAM_ID", { enumerable: true, get: function () { return chat_constants_js_1.CHAT_MESSAGES_STREAM_ID; } });
15
- Object.defineProperty(exports, "CHAT_STOP_STREAM_ID", { enumerable: true, get: function () { return chat_constants_js_1.CHAT_STOP_STREAM_ID; } });
16
7
  const METADATA_KEY = "tool.execute.options";
17
- /**
18
- * Wrapper around `convertToModelMessages` that always passes
19
- * `ignoreIncompleteToolCalls: true` to prevent failures from
20
- * stopped/aborted conversations with partial tool parts.
21
- */
22
- function toModelMessages(messages) {
23
- return (0, ai_1.convertToModelMessages)(messages, { ignoreIncompleteToolCalls: true });
24
- }
25
- const chatTurnContextKey = locals_js_1.locals.create("chat.turnContext");
26
- function assertTaskUsableAsTool(task) {
8
+ function toolFromTask(task, options) {
27
9
  if (("schema" in task && !task.schema) || ("jsonSchema" in task && !task.jsonSchema)) {
28
10
  throw new Error("Cannot convert this task to to a tool because the task has no schema. Make sure to either use schemaTask or a task with an input jsonSchema.");
29
11
  }
30
- }
31
- /**
32
- * Shared implementation: run a task as a tool invocation (`triggerAndSubscribe` + tool metadata).
33
- * Used by {@link toolExecute} and the deprecated `ai.tool()` wrapper.
34
- */
35
- function createTaskToolExecuteHandler(task) {
36
- assertTaskUsableAsTool(task);
37
- return async function taskToolExecuteHandler(input, toolOpts) {
38
- const toolMeta = {
39
- toolCallId: toolOpts?.toolCallId ?? "",
40
- };
41
- if (toolOpts?.experimental_context !== undefined) {
42
- try {
43
- toolMeta.experimental_context = JSON.parse(JSON.stringify(toolOpts.experimental_context));
44
- }
45
- catch {
46
- /* non-serializable */
47
- }
48
- }
49
- const chatCtx = locals_js_1.locals.get(chatTurnContextKey);
50
- if (chatCtx) {
51
- toolMeta.chatId = chatCtx.chatId;
52
- toolMeta.turn = chatCtx.turn;
53
- toolMeta.continuation = chatCtx.continuation;
54
- toolMeta.clientData = chatCtx.clientData;
55
- }
56
- const chatLocals = {};
57
- for (const entry of chatLocalRegistry) {
58
- const value = locals_js_1.locals.get(entry.key);
59
- if (value !== undefined) {
60
- chatLocals[entry.id] = value;
61
- }
62
- }
63
- if (Object.keys(chatLocals).length > 0) {
64
- toolMeta.chatLocals = chatLocals;
65
- }
66
- return await task
67
- .triggerAndSubscribe(input, {
68
- metadata: {
69
- [METADATA_KEY]: toolMeta,
70
- },
71
- tags: toolOpts?.toolCallId ? [`toolCallId:${toolOpts.toolCallId}`] : undefined,
72
- signal: toolOpts?.abortSignal,
73
- })
74
- .unwrap();
75
- };
76
- }
77
- function toolExecute(task) {
78
- return createTaskToolExecuteHandler(task);
79
- }
80
- /** @deprecated Use `tool()` from `ai` with `execute: ai.toolExecute(task)`. */
81
- function toolFromTask(task, options) {
82
- const executeFromTaskInput = createTaskToolExecuteHandler(task);
83
- // Zod-backed tasks: use static `tool()` so runtime shape matches `ToolSet`. Generic task context
84
- // prevents `tool()` overloads from inferring input; `as any` is localized to this call only.
85
- if ("schema" in task && task.schema && (0, v3_1.isSchemaZodEsque)(task.schema)) {
86
- const staticTool = (0, ai_1.tool)({
87
- description: task.description ?? "",
88
- inputSchema: (0, ai_1.zodSchema)(task.schema),
89
- execute: async (input, toolOpts) => executeFromTaskInput(input, toolOpts),
90
- ...(options?.experimental_toToolResultContent !== undefined
91
- ? { experimental_toToolResultContent: options.experimental_toToolResultContent }
92
- : {}),
93
- });
94
- return staticTool;
95
- }
96
12
  const toolDefinition = (0, ai_1.dynamicTool)({
97
13
  description: task.description,
98
14
  inputSchema: convertTaskSchemaToToolParameters(task),
99
- ...(options?.experimental_toToolResultContent !== undefined
100
- ? { experimental_toToolResultContent: options.experimental_toToolResultContent }
101
- : {}),
102
- execute: async (input, toolOpts) => executeFromTaskInput(input, toolOpts),
15
+ execute: async (input, options) => {
16
+ const serializedOptions = options ? JSON.parse(JSON.stringify(options)) : undefined;
17
+ return await task
18
+ .triggerAndWait(input, {
19
+ metadata: {
20
+ [METADATA_KEY]: serializedOptions,
21
+ },
22
+ })
23
+ .unwrap();
24
+ },
25
+ ...options,
103
26
  });
104
27
  return toolDefinition;
105
28
  }
@@ -110,53 +33,6 @@ function getToolOptionsFromMetadata() {
110
33
  }
111
34
  return tool;
112
35
  }
113
- /**
114
- * Get the current tool call ID from inside a subtask invoked via `ai.toolExecute()` (or legacy `ai.tool()`).
115
- * Returns `undefined` if not running as a tool subtask.
116
- */
117
- function getToolCallId() {
118
- return getToolOptionsFromMetadata()?.toolCallId;
119
- }
120
- /**
121
- * Get the chat context from inside a subtask invoked via `ai.toolExecute()` (or legacy `ai.tool()`) within a `chat.task`.
122
- * Pass `typeof yourChatTask` as the type parameter to get typed `clientData`.
123
- * Returns `undefined` if the parent is not a chat task.
124
- *
125
- * @example
126
- * ```ts
127
- * const ctx = ai.chatContext<typeof myChat>();
128
- * // ctx?.clientData is typed based on myChat's clientDataSchema
129
- * ```
130
- */
131
- function getToolChatContext() {
132
- const opts = getToolOptionsFromMetadata();
133
- if (!opts?.chatId)
134
- return undefined;
135
- return {
136
- chatId: opts.chatId,
137
- turn: opts.turn ?? 0,
138
- continuation: opts.continuation ?? false,
139
- clientData: opts.clientData,
140
- };
141
- }
142
- /**
143
- * Get the chat context from inside a subtask, throwing if not in a chat context.
144
- * Pass `typeof yourChatTask` as the type parameter to get typed `clientData`.
145
- *
146
- * @example
147
- * ```ts
148
- * const ctx = ai.chatContextOrThrow<typeof myChat>();
149
- * // ctx.chatId, ctx.clientData are guaranteed non-null
150
- * ```
151
- */
152
- function getToolChatContextOrThrow() {
153
- const ctx = getToolChatContext();
154
- if (!ctx) {
155
- throw new Error("ai.chatContextOrThrow() called outside of a chat.task context. " +
156
- "This helper can only be used inside a subtask invoked via ai.toolExecute() (or legacy ai.tool()) from a chat.task.");
157
- }
158
- return ctx;
159
- }
160
36
  function convertTaskSchemaToToolParameters(task) {
161
37
  if ("schema" in task) {
162
38
  // If TaskSchema is ArkTypeEsque, use ai.jsonSchema to convert it to a Schema
@@ -174,2752 +50,7 @@ function convertTaskSchemaToToolParameters(task) {
174
50
  throw new Error("Cannot convert task to a tool. Make sure to use a task with a schema or jsonSchema.");
175
51
  }
176
52
  exports.ai = {
177
- /**
178
- * @deprecated Use `tool()` from the `ai` package with `execute: ai.toolExecute(task)` instead.
179
- */
180
53
  tool: toolFromTask,
181
- /**
182
- * Preferred: return value for the `execute` field of AI SDK `tool()`. Keeps Trigger subtask and
183
- * metadata behavior without coupling to a specific `ai` version’s `Tool` / `ToolSet` types.
184
- */
185
- toolExecute,
186
54
  currentToolOptions: getToolOptionsFromMetadata,
187
- /** Get the tool call ID from inside a subtask invoked via `ai.toolExecute()` (or legacy `ai.tool()`). */
188
- toolCallId: getToolCallId,
189
- /** Get chat context (chatId, turn, clientData, etc.) from inside a subtask of a `chat.task`. Returns undefined if not in a chat context. */
190
- chatContext: getToolChatContext,
191
- /** Get chat context or throw if not in a chat context. Pass `typeof yourChatTask` for typed clientData. */
192
- chatContextOrThrow: getToolChatContextOrThrow,
193
- };
194
- /**
195
- * Creates a public access token for a chat task.
196
- *
197
- * This is a convenience helper that creates a multi-use trigger public token
198
- * scoped to the given task. Use it in a server action to provide the frontend
199
- * `TriggerChatTransport` with an `accessToken`.
200
- *
201
- * @example
202
- * ```ts
203
- * // actions.ts
204
- * "use server";
205
- * import { chat } from "@trigger.dev/sdk/ai";
206
- * import type { myChat } from "@/trigger/chat";
207
- *
208
- * export const getChatToken = () => chat.createAccessToken<typeof myChat>("my-chat");
209
- * ```
210
- */
211
- function createChatAccessToken(taskId) {
212
- return auth_js_1.auth.createTriggerPublicToken(taskId, { expirationTime: "24h" });
213
- }
214
- // ---------------------------------------------------------------------------
215
- // Chat transport helpers — backend side
216
- // ---------------------------------------------------------------------------
217
- /**
218
- * The default stream key used for chat transport communication.
219
- * Both `TriggerChatTransport` (frontend) and `pipeChat`/`chatTask` (backend)
220
- * use this key by default.
221
- */
222
- exports.CHAT_STREAM_KEY = chat_constants_js_1.CHAT_STREAM_KEY;
223
- /**
224
- * Typed chat output stream. Provides `.writer()`, `.pipe()`, `.append()`,
225
- * and `.read()` methods pre-bound to the chat stream key and typed to `UIMessageChunk`.
226
- *
227
- * Use from within a `chat.task` run to write custom chunks:
228
- * ```ts
229
- * const { waitUntilComplete } = chat.stream.writer({
230
- * execute: ({ write }) => {
231
- * write({ type: "text-start", id: "status-1" });
232
- * write({ type: "text-delta", id: "status-1", delta: "Processing..." });
233
- * write({ type: "text-end", id: "status-1" });
234
- * },
235
- * });
236
- * await waitUntilComplete();
237
- * ```
238
- *
239
- * Use from a subtask to stream back to the parent chat:
240
- * ```ts
241
- * chat.stream.pipe(myStream, { target: "root" });
242
- * ```
243
- */
244
- const chatStream = streams_js_1.streams.define({ id: chat_constants_js_1.CHAT_STREAM_KEY });
245
- /**
246
- * Creates a lazy ChatWriter that only opens a realtime stream on first use.
247
- * Call `flush()` after the callback returns to await stream completion.
248
- * @internal
249
- */
250
- function createLazyChatWriter() {
251
- let writeImpl = null;
252
- let mergeImpl = null;
253
- let waitPromise = null;
254
- let resolveExecute = null;
255
- function ensureInitialized() {
256
- if (writeImpl)
257
- return;
258
- const executePromise = new Promise((resolve) => {
259
- resolveExecute = resolve;
260
- });
261
- const { waitUntilComplete } = chatStream.writer({
262
- collapsed: true,
263
- spanName: "callback writer",
264
- execute: ({ write, merge }) => {
265
- writeImpl = write;
266
- mergeImpl = merge;
267
- return executePromise; // Keep execute alive until flush()
268
- },
269
- });
270
- waitPromise = waitUntilComplete;
271
- }
272
- return {
273
- writer: {
274
- write(part) {
275
- ensureInitialized();
276
- writeImpl(part);
277
- },
278
- merge(stream) {
279
- ensureInitialized();
280
- mergeImpl(stream);
281
- },
282
- },
283
- async flush() {
284
- if (resolveExecute) {
285
- resolveExecute(); // Signal execute to complete
286
- await waitPromise(); // Wait for stream to finish piping
287
- }
288
- },
289
- };
290
- }
291
- /**
292
- * Runs a callback with a lazy ChatWriter, flushing the stream after completion.
293
- * @internal
294
- */
295
- async function withChatWriter(fn) {
296
- const { writer, flush } = createLazyChatWriter();
297
- const result = await fn(writer);
298
- await flush();
299
- return result;
300
- }
301
- // Input streams for bidirectional chat communication
302
- const messagesInput = streams_js_1.streams.input({ id: chat_constants_js_1.CHAT_MESSAGES_STREAM_ID });
303
- const stopInput = streams_js_1.streams.input({ id: chat_constants_js_1.CHAT_STOP_STREAM_ID });
304
- /**
305
- * Per-turn deferred promises. Registered via `chat.defer()`, awaited
306
- * before `onTurnComplete` fires. Reset each turn.
307
- * @internal
308
- */
309
- const chatDeferKey = locals_js_1.locals.create("chat.defer");
310
- /**
311
- * Per-turn background context queue. Messages added via `chat.backgroundWork.inject()`
312
- * are drained at the next `prepareStep` boundary and appended to the model messages.
313
- * @internal
314
- */
315
- const chatBackgroundQueueKey = locals_js_1.locals.create("chat.backgroundQueue");
316
- /**
317
- * Run-scoped pipe counter. Stored in locals so concurrent runs in the
318
- * same worker don't share state.
319
- * @internal
320
- */
321
- const chatPipeCountKey = locals_js_1.locals.create("chat.pipeCount");
322
- const chatStopControllerKey = locals_js_1.locals.create("chat.stopController");
323
- /** Static (task-level) UIMessageStream options, set once during chatTask setup. @internal */
324
- const chatUIStreamStaticKey = locals_js_1.locals.create("chat.uiMessageStreamOptions.static");
325
- /** Per-turn UIMessageStream options, set via chat.setUIMessageStreamOptions(). @internal */
326
- const chatUIStreamPerTurnKey = locals_js_1.locals.create("chat.uiMessageStreamOptions.perTurn");
327
- function emptyUsage() {
328
- return {
329
- inputTokens: undefined,
330
- outputTokens: undefined,
331
- totalTokens: undefined,
332
- inputTokenDetails: {
333
- noCacheTokens: undefined,
334
- cacheReadTokens: undefined,
335
- cacheWriteTokens: undefined,
336
- },
337
- outputTokenDetails: { textTokens: undefined, reasoningTokens: undefined },
338
- };
339
- }
340
- function addUsage(a, b) {
341
- const add = (x, y) => x != null || y != null ? (x ?? 0) + (y ?? 0) : undefined;
342
- return {
343
- inputTokens: add(a.inputTokens, b.inputTokens),
344
- outputTokens: add(a.outputTokens, b.outputTokens),
345
- totalTokens: add(a.totalTokens, b.totalTokens),
346
- inputTokenDetails: {
347
- noCacheTokens: add(a.inputTokenDetails?.noCacheTokens, b.inputTokenDetails?.noCacheTokens),
348
- cacheReadTokens: add(a.inputTokenDetails?.cacheReadTokens, b.inputTokenDetails?.cacheReadTokens),
349
- cacheWriteTokens: add(a.inputTokenDetails?.cacheWriteTokens, b.inputTokenDetails?.cacheWriteTokens),
350
- },
351
- outputTokenDetails: {
352
- textTokens: add(a.outputTokenDetails?.textTokens, b.outputTokenDetails?.textTokens),
353
- reasoningTokens: add(a.outputTokenDetails?.reasoningTokens, b.outputTokenDetails?.reasoningTokens),
354
- },
355
- };
356
- }
357
- // ---------------------------------------------------------------------------
358
- // chat.setMessages — replace accumulated messages for compaction
359
- // ---------------------------------------------------------------------------
360
- /** @internal */
361
- const chatOverrideMessagesKey = locals_js_1.locals.create("chat.overrideMessages");
362
- /**
363
- * Replace the accumulated conversation messages for the current run.
364
- *
365
- * Call from `onTurnStart` to compact before `run()` executes, or from
366
- * `onTurnComplete` to compact before the next turn. Takes `UIMessage[]`
367
- * and converts to `ModelMessage[]` internally.
368
- */
369
- function setChatMessages(uiMessages) {
370
- locals_js_1.locals.set(chatOverrideMessagesKey, uiMessages);
371
- }
372
- /**
373
- * Model-only message override. Set by compaction to replace only the model
374
- * messages (what goes to the LLM) without affecting UI messages (what gets
375
- * persisted and displayed). This preserves full conversation history for the
376
- * user while keeping LLM context compact.
377
- * @internal
378
- */
379
- const chatOverrideModelMessagesKey = locals_js_1.locals.create("chat.overrideModelMessages");
380
- /** @internal */
381
- const chatCompactionStateKey = locals_js_1.locals.create("chat.compaction");
382
- const chatOnCompactedKey = locals_js_1.locals.create("chat.onCompacted");
383
- /** @internal Full task `ctx` for the active `chat.task` run (for hooks invoked from nested compaction). */
384
- const chatTaskRunContextKey = locals_js_1.locals.create("chat.taskRunContext");
385
- const chatPrepareMessagesKey = locals_js_1.locals.create("chat.prepareMessages");
386
- /** @internal */
387
- const chatTaskCompactionKey = locals_js_1.locals.create("chat.taskCompaction");
388
- /**
389
- * The data part type used to signal that pending messages were injected
390
- * between tool-call steps. The frontend can match on this to render
391
- * injection points inline in the assistant response.
392
- */
393
- exports.PENDING_MESSAGE_INJECTED_TYPE = "data-pending-message-injected";
394
- /** @internal */
395
- const chatPendingMessagesKey = locals_js_1.locals.create("chat.pendingMessages");
396
- /** @internal */
397
- const chatSteeringQueueKey = locals_js_1.locals.create("chat.steeringQueue");
398
- /** @internal — IDs of messages that were successfully injected via prepareStep */
399
- const chatInjectedMessageIdsKey = locals_js_1.locals.create("chat.injectedMessageIds");
400
- /**
401
- * Check that no tool calls are in-flight in a step's content.
402
- * Used before compaction to avoid losing tool state mid-execution.
403
- * @internal
404
- */
405
- function isStepBoundarySafe(step) {
406
- if (step.finishReason === "error")
407
- return false;
408
- const callIds = new Set(step.content.filter((p) => p.type === "tool-call").map((p) => p.toolCallId));
409
- const settledIds = new Set(step.content
410
- .filter((p) => p.type === "tool-result" || p.type === "tool-error")
411
- .map((p) => p.toolCallId));
412
- return ![...callIds].some((id) => !settledIds.has(id));
413
- }
414
- /**
415
- * Apply the prepareMessages hook if one is set in locals.
416
- * @internal
417
- */
418
- async function applyPrepareMessages(messages, reason) {
419
- const hook = locals_js_1.locals.get(chatPrepareMessagesKey);
420
- if (!hook)
421
- return messages;
422
- const turnCtx = locals_js_1.locals.get(chatTurnContextKey);
423
- return tracer_js_1.tracer.startActiveSpan("prepareMessages()", async () => {
424
- return hook({
425
- messages,
426
- reason,
427
- chatId: turnCtx?.chatId ?? "",
428
- turn: turnCtx?.turn ?? 0,
429
- clientData: turnCtx?.clientData,
430
- });
431
- }, {
432
- attributes: {
433
- [v3_1.SemanticInternalAttributes.STYLE_ICON]: "task-hook-onStart",
434
- [v3_1.SemanticInternalAttributes.COLLAPSED]: true,
435
- "chat.prepareMessages.reason": reason,
436
- "chat.prepareMessages.messageCount": messages.length,
437
- },
438
- });
439
- }
440
- /**
441
- * Read the current compaction state. Returns the summary and base message count
442
- * if compaction has occurred in this turn, or `undefined` if not.
443
- *
444
- * Use in a custom `prepareStep` to rebuild from a previous compaction:
445
- * ```ts
446
- * const state = chat.getCompactionState();
447
- * if (state) {
448
- * return { messages: [{ role: "user", content: state.summary }, ...newMsgs] };
449
- * }
450
- * ```
451
- */
452
- function getCompactionState() {
453
- return locals_js_1.locals.get(chatCompactionStateKey);
454
- }
455
- /**
456
- * Low-level compaction for use inside a custom `prepareStep`.
457
- *
458
- * Handles the full decision tree: first step, already-compacted rebuild,
459
- * boundary safety, threshold check, summarization, stream chunks, state
460
- * storage, and accumulator update.
461
- *
462
- * Returns a `CompactResult` — inspect `result.type` to see what happened,
463
- * or convert to a prepareStep return with `result.type === "skipped" ? undefined : result`.
464
- *
465
- * @example
466
- * ```ts
467
- * prepareStep: async ({ messages, steps }) => {
468
- * // your custom logic here...
469
- * const result = await chat.compact(messages, steps, {
470
- * threshold: 80_000,
471
- * summarize: async (msgs) => generateText({ model, messages: msgs }).then(r => r.text),
472
- * });
473
- * if (result.type === "compacted") {
474
- * logger.info("Compacted!", { summary: result.summary });
475
- * }
476
- * return result.type === "skipped" ? undefined : result;
477
- * },
478
- * ```
479
- */
480
- async function chatCompact(messages, steps, options) {
481
- const currentStep = steps.at(-1);
482
- // First step — nothing to check
483
- if (!currentStep) {
484
- return { type: "skipped" };
485
- }
486
- // Already compacted — rebuild from summary + new response messages
487
- const state = locals_js_1.locals.get(chatCompactionStateKey);
488
- if (state && isStepBoundarySafe(currentStep)) {
489
- return {
490
- type: "rebuilt",
491
- messages: await applyPrepareMessages([
492
- { role: "user", content: state.summary },
493
- ...currentStep.response.messages.slice(state.baseResponseMessageCount),
494
- ], "compaction-rebuild"),
495
- };
496
- }
497
- // Boundary unsafe — skip
498
- if (!isStepBoundarySafe(currentStep)) {
499
- return { type: "skipped" };
500
- }
501
- const totalTokens = currentStep.usage.totalTokens;
502
- const inputTokens = currentStep.usage.inputTokens;
503
- const outputTokens = currentStep.usage.outputTokens;
504
- const turnCtx = locals_js_1.locals.get(chatTurnContextKey);
505
- const stepNumber = steps.length - 1;
506
- const shouldTrigger = options.shouldCompact
507
- ? await options.shouldCompact({
508
- messages,
509
- totalTokens,
510
- inputTokens,
511
- outputTokens,
512
- usage: currentStep.usage,
513
- source: "inner",
514
- stepNumber,
515
- steps,
516
- chatId: turnCtx?.chatId,
517
- turn: turnCtx?.turn,
518
- clientData: turnCtx?.clientData,
519
- })
520
- : totalTokens != null && options.threshold != null && totalTokens > options.threshold;
521
- if (!shouldTrigger) {
522
- return { type: "skipped" };
523
- }
524
- const result = await tracer_js_1.tracer.startActiveSpan("context compaction", async (span) => {
525
- const compactionId = (0, ai_1.generateId)();
526
- let summary;
527
- const { waitUntilComplete } = streams_js_1.streams.writer(exports.CHAT_STREAM_KEY, {
528
- spanName: "stream compaction chunks",
529
- collapsed: true,
530
- execute: async ({ write, merge }) => {
531
- write({ type: "step-start" });
532
- write({
533
- type: "data-compaction",
534
- id: compactionId,
535
- data: { status: "compacting", totalTokens },
536
- });
537
- // Generate summary
538
- summary = await options.summarize(messages);
539
- // Store state in locals for subsequent steps
540
- locals_js_1.locals.set(chatCompactionStateKey, {
541
- summary,
542
- baseResponseMessageCount: currentStep.response.messages.length,
543
- });
544
- // Set model-only override — UI messages stay intact for persistence.
545
- // The summary becomes the model message history for the next turn,
546
- // while accumulatedUIMessages keeps the full conversation for display.
547
- locals_js_1.locals.set(chatOverrideModelMessagesKey, [
548
- {
549
- role: "assistant",
550
- content: [{ type: "text", text: `[Conversation summary]\n\n${summary}` }],
551
- },
552
- ]);
553
- // Fire onCompacted hook — pass the existing writer so the callback
554
- // can write custom chunks without creating a separate stream.
555
- const onCompactedHook = locals_js_1.locals.get(chatOnCompactedKey);
556
- if (onCompactedHook) {
557
- await onCompactedHook({
558
- ctx: locals_js_1.locals.get(chatTaskRunContextKey),
559
- summary,
560
- messages,
561
- messageCount: messages.length,
562
- usage: currentStep.usage,
563
- totalTokens,
564
- inputTokens,
565
- outputTokens,
566
- stepNumber,
567
- chatId: turnCtx?.chatId,
568
- turn: turnCtx?.turn,
569
- writer: { write, merge },
570
- });
571
- }
572
- write({
573
- type: "data-compaction",
574
- id: compactionId,
575
- data: { status: "complete", totalTokens },
576
- });
577
- write({ type: "finish-step" });
578
- },
579
- });
580
- await waitUntilComplete();
581
- // Set attributes after we have the summary
582
- span.setAttribute("compaction.summary_length", summary.length);
583
- return {
584
- type: "compacted",
585
- messages: await applyPrepareMessages([{ role: "user", content: summary }], "compaction-result"),
586
- summary,
587
- };
588
- }, {
589
- attributes: {
590
- [v3_1.SemanticInternalAttributes.STYLE_ICON]: "tabler-scissors",
591
- "compaction.threshold": options.threshold,
592
- "compaction.total_tokens": totalTokens ?? 0,
593
- "compaction.input_tokens": inputTokens ?? 0,
594
- "compaction.message_count": messages.length,
595
- "compaction.step_number": stepNumber,
596
- ...(turnCtx?.chatId ? { "compaction.chat_id": turnCtx.chatId } : {}),
597
- ...(turnCtx?.turn != null ? { "compaction.turn": turnCtx.turn } : {}),
598
- ...(0, v3_1.accessoryAttributes)({
599
- items: [
600
- { text: `${totalTokens ?? 0} tokens`, variant: "normal" },
601
- { text: `${messages.length} msgs`, variant: "normal" },
602
- ],
603
- style: "codepath",
604
- }),
605
- },
606
- });
607
- return result;
608
- }
609
- /**
610
- * Returns a `prepareStep` function that handles context compaction automatically.
611
- *
612
- * Monitors token usage between tool-call steps. When `totalTokens` exceeds
613
- * the threshold, generates a summary via `summarize()`, replaces the message
614
- * history, and emits `data-compaction` stream chunks for the frontend.
615
- *
616
- * @example
617
- * ```ts
618
- * return streamText({
619
- * ...chat.toStreamTextOptions({ registry }),
620
- * messages: chat.addCacheBreaks(messages),
621
- * prepareStep: chat.compactionStep({
622
- * threshold: 80_000,
623
- * summarize: async (messages) => {
624
- * return generateText({ model, messages: [...messages, { role: "user", content: "Summarize." }] })
625
- * .then((r) => r.text);
626
- * },
627
- * }),
628
- * tools: { ... },
629
- * });
630
- * ```
631
- */
632
- function chatCompactionStep(options) {
633
- return async ({ messages, steps }) => {
634
- const result = await chatCompact(messages, steps, options);
635
- return result.type === "skipped" ? undefined : result;
636
- };
637
- }
638
- // ---------------------------------------------------------------------------
639
- // Steering queue drain — shared by toStreamTextOptions, session, accumulator
640
- // ---------------------------------------------------------------------------
641
- /**
642
- * Drain the steering queue as a batch. Calls `shouldInject` once with all
643
- * pending messages. If it returns true, calls `prepareMessages` once to
644
- * transform the batch, then clears the queue.
645
- * Returns the model messages to inject (empty if none).
646
- * @internal
647
- */
648
- async function drainSteeringQueue(config, messages, steps, queueOverride) {
649
- const queue = queueOverride ?? locals_js_1.locals.get(chatSteeringQueueKey);
650
- if (!queue || queue.length === 0)
651
- return [];
652
- const ctx = locals_js_1.locals.get(chatTurnContextKey);
653
- const stepNumber = steps.length - 1;
654
- const uiMessages = queue.map((e) => e.uiMessage);
655
- const batchEvent = {
656
- messages: uiMessages,
657
- modelMessages: messages,
658
- steps,
659
- stepNumber,
660
- chatId: ctx?.chatId ?? "",
661
- turn: ctx?.turn ?? 0,
662
- clientData: ctx?.clientData,
663
- };
664
- // Call shouldInject once for the whole batch
665
- const shouldInject = config.shouldInject ? await config.shouldInject(batchEvent) : false;
666
- if (!shouldInject)
667
- return [];
668
- // Extract message texts for span attributes
669
- const messageTexts = uiMessages.map((m) => (m.parts ?? [])
670
- .filter((p) => p.type === "text")
671
- .map((p) => p.text)
672
- .join("") || "");
673
- const previewText = messageTexts.length === 1 ? messageTexts[0].slice(0, 80) : `${queue.length} messages`;
674
- return tracer_js_1.tracer.startActiveSpan("pending message injected", async () => {
675
- // Transform the batch — default: concatenate all pre-converted model messages
676
- const injected = config.prepare
677
- ? await config.prepare(batchEvent)
678
- : queue.flatMap((e) => e.modelMessages);
679
- // Clear the queue and record injected IDs
680
- queue.length = 0;
681
- const injectedIds = locals_js_1.locals.get(chatInjectedMessageIdsKey);
682
- if (injectedIds) {
683
- for (const m of uiMessages)
684
- injectedIds.add(m.id);
685
- }
686
- // Write injection confirmation chunk to the stream so the frontend
687
- // knows which messages were injected and where in the response.
688
- if (injected.length > 0) {
689
- try {
690
- const { waitUntilComplete } = streams_js_1.streams.writer(exports.CHAT_STREAM_KEY, {
691
- collapsed: true,
692
- execute: ({ write }) => {
693
- write({
694
- type: exports.PENDING_MESSAGE_INJECTED_TYPE,
695
- id: (0, ai_1.generateId)(),
696
- data: {
697
- messageIds: uiMessages.map((m) => m.id),
698
- messages: uiMessages.map((m, idx) => ({
699
- id: m.id,
700
- text: messageTexts[idx] ?? "",
701
- })),
702
- },
703
- });
704
- },
705
- });
706
- await waitUntilComplete();
707
- }
708
- catch {
709
- /* non-fatal — stream write failed */
710
- }
711
- }
712
- // Fire onInjected callback
713
- if (config.onInjected && injected.length > 0) {
714
- try {
715
- await config.onInjected({
716
- messages: uiMessages,
717
- injectedModelMessages: injected,
718
- chatId: ctx?.chatId ?? "",
719
- turn: ctx?.turn ?? 0,
720
- stepNumber,
721
- });
722
- }
723
- catch {
724
- /* non-fatal */
725
- }
726
- }
727
- return injected;
728
- }, {
729
- attributes: {
730
- [v3_1.SemanticInternalAttributes.STYLE_ICON]: "tabler-message-forward",
731
- "pending.message_count": uiMessages.length,
732
- "pending.step_number": stepNumber,
733
- "pending.messages": messageTexts,
734
- ...(ctx?.chatId ? { "pending.chat_id": ctx.chatId } : {}),
735
- ...(ctx?.turn != null ? { "pending.turn": ctx.turn } : {}),
736
- ...(0, v3_1.accessoryAttributes)({
737
- items: [
738
- {
739
- text: `${uiMessages.length} message${uiMessages.length === 1 ? "" : "s"}`,
740
- variant: "normal",
741
- },
742
- { text: `between steps ${stepNumber} and ${stepNumber + 1}`, variant: "normal" },
743
- ],
744
- style: "codepath",
745
- }),
746
- },
747
- });
748
- }
749
- // ---------------------------------------------------------------------------
750
- // chat.isCompactionSafe — check if it's safe to compact messages
751
- // ---------------------------------------------------------------------------
752
- /**
753
- * Checks whether it's safe to compact the message history. Returns `false`
754
- * if any tool calls are in-flight (incomplete tool invocations without results).
755
- *
756
- * Call before `chat.setMessages()` to avoid corrupting tool-call state.
757
- */
758
- function isCompactionSafe(messages) {
759
- for (const msg of messages) {
760
- if (msg.role !== "assistant")
761
- continue;
762
- for (const part of msg.parts) {
763
- if (part.type === "tool-invocation") {
764
- const state = part.toolInvocation?.state ?? part.state;
765
- if (state !== "result" && state !== "error") {
766
- return false;
767
- }
768
- }
769
- }
770
- }
771
- return true;
772
- }
773
- /** @internal */
774
- const chatPromptKey = locals_js_1.locals.create("chat.prompt");
775
- /**
776
- * Store a resolved prompt (or plain string) for the current run.
777
- * Call from any hook (`onPreload`, `onChatStart`, `onTurnStart`) or `run()`.
778
- */
779
- function setChatPrompt(resolved) {
780
- if (typeof resolved === "string") {
781
- locals_js_1.locals.set(chatPromptKey, {
782
- text: resolved,
783
- model: undefined,
784
- config: undefined,
785
- promptId: "",
786
- version: 0,
787
- labels: [],
788
- toAISDKTelemetry: () => ({
789
- experimental_telemetry: { isEnabled: true, metadata: {} },
790
- }),
791
- });
792
- }
793
- else {
794
- locals_js_1.locals.set(chatPromptKey, resolved);
795
- }
796
- }
797
- /**
798
- * Read the stored prompt. Throws if `chat.prompt.set()` has not been called.
799
- */
800
- function getChatPrompt() {
801
- const prompt = locals_js_1.locals.get(chatPromptKey);
802
- if (!prompt) {
803
- throw new Error("chat.prompt() called before chat.prompt.set(). Set a prompt in onPreload, onChatStart, onTurnStart, or run() first.");
804
- }
805
- return prompt;
806
- }
807
- /**
808
- * Returns an options object ready to spread into `streamText()`.
809
- *
810
- * Includes `system`, `experimental_telemetry`, and any config fields
811
- * (temperature, maxTokens, etc.) from the stored prompt.
812
- *
813
- * When a `registry` is provided and the prompt has a `model` string,
814
- * the resolved `LanguageModel` is included as `model`.
815
- *
816
- * If no prompt has been set, returns `{}` (no-op spread).
817
- */
818
- function toStreamTextOptions(options) {
819
- const prompt = locals_js_1.locals.get(chatPromptKey);
820
- if (!prompt)
821
- return {};
822
- const result = {
823
- system: prompt.text,
824
- };
825
- // Resolve model via registry if both are present
826
- if (options?.registry && prompt.model) {
827
- result.model = options.registry.languageModel(prompt.model);
828
- }
829
- // Spread config (temperature, maxTokens, etc.)
830
- if (prompt.config) {
831
- Object.assign(result, prompt.config);
832
- }
833
- // Add telemetry (forward additional metadata from caller)
834
- const telemetry = prompt.toAISDKTelemetry(options?.telemetry);
835
- Object.assign(result, telemetry);
836
- // Auto-inject prepareStep for compaction, pending messages, and background context injection.
837
- const taskCompaction = locals_js_1.locals.get(chatTaskCompactionKey);
838
- const taskPendingMessages = locals_js_1.locals.get(chatPendingMessagesKey);
839
- {
840
- result.prepareStep = async ({ messages, steps, }) => {
841
- let resultMessages;
842
- // 1. Compaction
843
- if (taskCompaction) {
844
- const compactResult = await chatCompact(messages, steps, {
845
- shouldCompact: taskCompaction.shouldCompact,
846
- summarize: (msgs) => {
847
- const ctx = locals_js_1.locals.get(chatTurnContextKey);
848
- const lastStep = steps.at(-1);
849
- return taskCompaction.summarize({
850
- messages: msgs,
851
- usage: lastStep?.usage,
852
- source: "inner",
853
- stepNumber: steps.length - 1,
854
- chatId: ctx?.chatId,
855
- turn: ctx?.turn,
856
- clientData: ctx?.clientData,
857
- });
858
- },
859
- });
860
- if (compactResult.type !== "skipped") {
861
- resultMessages = compactResult.messages;
862
- }
863
- }
864
- // 2. Pending message injection (steering)
865
- if (taskPendingMessages) {
866
- const injected = await drainSteeringQueue(taskPendingMessages, resultMessages ?? messages, steps);
867
- if (injected.length > 0) {
868
- resultMessages = [...(resultMessages ?? messages), ...injected];
869
- }
870
- }
871
- // 3. Background context injection
872
- const bgQueue = locals_js_1.locals.get(chatBackgroundQueueKey);
873
- if (bgQueue && bgQueue.length > 0) {
874
- const injected = bgQueue.splice(0); // drain
875
- resultMessages = [...(resultMessages ?? messages), ...injected];
876
- }
877
- return resultMessages ? { messages: resultMessages } : undefined;
878
- };
879
- }
880
- return result;
881
- }
882
- function isUIMessageStreamable(value) {
883
- return (typeof value === "object" &&
884
- value !== null &&
885
- "toUIMessageStream" in value &&
886
- typeof value.toUIMessageStream === "function");
887
- }
888
- function isAsyncIterable(value) {
889
- return typeof value === "object" && value !== null && Symbol.asyncIterator in value;
890
- }
891
- function isReadableStream(value) {
892
- return (typeof value === "object" && value !== null && typeof value.getReader === "function");
893
- }
894
- /**
895
- * Pipes a chat stream to the realtime stream, making it available to the
896
- * `TriggerChatTransport` on the frontend.
897
- *
898
- * Accepts:
899
- * - A `StreamTextResult` from `streamText()` (has `.toUIMessageStream()`)
900
- * - An `AsyncIterable` of `UIMessageChunk`s
901
- * - A `ReadableStream` of `UIMessageChunk`s
902
- *
903
- * Must be called from inside a Trigger.dev task's `run` function.
904
- *
905
- * @example
906
- * ```ts
907
- * import { task } from "@trigger.dev/sdk";
908
- * import { chat, type ChatTaskPayload } from "@trigger.dev/sdk/ai";
909
- * import { streamText, convertToModelMessages } from "ai";
910
- *
911
- * export const myChatTask = task({
912
- * id: "my-chat-task",
913
- * run: async (payload: ChatTaskPayload) => {
914
- * const result = streamText({
915
- * model: openai("gpt-4o"),
916
- * messages: payload.messages,
917
- * });
918
- *
919
- * await chat.pipe(result);
920
- * },
921
- * });
922
- * ```
923
- *
924
- * @example
925
- * ```ts
926
- * // Works from anywhere inside a task — even deep in your agent code
927
- * async function runAgentLoop(messages: CoreMessage[]) {
928
- * const result = streamText({ model, messages });
929
- * await chat.pipe(result);
930
- * }
931
- * ```
932
- */
933
- async function pipeChat(source, options) {
934
- locals_js_1.locals.set(chatPipeCountKey, (locals_js_1.locals.get(chatPipeCountKey) ?? 0) + 1);
935
- const streamKey = options?.streamKey ?? exports.CHAT_STREAM_KEY;
936
- let stream;
937
- if (isUIMessageStreamable(source)) {
938
- stream = source.toUIMessageStream();
939
- }
940
- else if (isAsyncIterable(source) || isReadableStream(source)) {
941
- stream = source;
942
- }
943
- else {
944
- throw new Error("pipeChat: source must be a StreamTextResult (with .toUIMessageStream()), " +
945
- "an AsyncIterable, or a ReadableStream");
946
- }
947
- const pipeOptions = {};
948
- if (options?.signal) {
949
- pipeOptions.signal = options.signal;
950
- }
951
- if (options?.target) {
952
- pipeOptions.target = options.target;
953
- }
954
- if (options?.spanName) {
955
- pipeOptions.spanName = options.spanName;
956
- }
957
- const { waitUntilComplete } = streams_js_1.streams.pipe(streamKey, stream, pipeOptions);
958
- await waitUntilComplete();
959
- }
960
- /**
961
- * Creates a Trigger.dev task pre-configured for AI SDK chat.
962
- *
963
- * - **Pre-types the payload** as `ChatTaskRunPayload` — includes abort signals
964
- * - **Auto-pipes the stream** if `run` returns a `StreamTextResult`
965
- * - **Multi-turn**: keeps the conversation in a single run using input streams
966
- * - **Stop support**: frontend can stop generation mid-stream via the stop input stream
967
- * - For complex flows, use `pipeChat()` from anywhere inside your task code
968
- *
969
- * @example
970
- * ```ts
971
- * import { chat } from "@trigger.dev/sdk/ai";
972
- * import { streamText, convertToModelMessages } from "ai";
973
- * import { openai } from "@ai-sdk/openai";
974
- *
975
- * export const myChat = chat.task({
976
- * id: "my-chat",
977
- * run: async ({ messages, signal }) => {
978
- * return streamText({
979
- * model: openai("gpt-4o"),
980
- * messages, // already converted via convertToModelMessages
981
- * abortSignal: signal,
982
- * });
983
- * },
984
- * });
985
- * ```
986
- */
987
- function chatTask(options) {
988
- const { run: userRun, clientDataSchema, onPreload, onChatStart, onTurnStart, onBeforeTurnComplete, onCompacted, compaction, pendingMessages: pendingMessagesConfig, prepareMessages, onTurnComplete, maxTurns = 100, turnTimeout = "1h", idleTimeoutInSeconds = 30, chatAccessTokenTTL = "1h", preloadIdleTimeoutInSeconds, preloadTimeout, uiMessageStreamOptions, ...restOptions } = options;
989
- const parseClientData = clientDataSchema ? (0, v3_1.getSchemaParseFn)(clientDataSchema) : undefined;
990
- return (0, shared_js_1.createTask)({
991
- ...restOptions,
992
- run: async (payload, { signal: runSignal, ctx }) => {
993
- locals_js_1.locals.set(chatTaskRunContextKey, ctx);
994
- // Set gen_ai.conversation.id on the run-level span for dashboard context
995
- const activeSpan = api_1.trace.getActiveSpan();
996
- if (activeSpan) {
997
- activeSpan.setAttribute("gen_ai.conversation.id", payload.chatId);
998
- }
999
- // Store static UIMessageStream options in locals so resolveUIMessageStreamOptions() can read them
1000
- if (uiMessageStreamOptions) {
1001
- locals_js_1.locals.set(chatUIStreamStaticKey, uiMessageStreamOptions);
1002
- }
1003
- // Store onCompacted hook in locals so chat.compact() can call it
1004
- if (onCompacted) {
1005
- locals_js_1.locals.set(chatOnCompactedKey, onCompacted);
1006
- }
1007
- if (prepareMessages) {
1008
- locals_js_1.locals.set(chatPrepareMessagesKey, prepareMessages);
1009
- }
1010
- if (compaction) {
1011
- locals_js_1.locals.set(chatTaskCompactionKey, compaction);
1012
- }
1013
- if (pendingMessagesConfig) {
1014
- locals_js_1.locals.set(chatPendingMessagesKey, pendingMessagesConfig);
1015
- }
1016
- let currentWirePayload = payload;
1017
- const continuation = payload.continuation ?? false;
1018
- const previousRunId = payload.previousRunId;
1019
- const preloaded = payload.trigger === "preload";
1020
- // Accumulated model messages across turns. Turn 1 initialises from the
1021
- // full history the frontend sends; subsequent turns append only the new
1022
- // user message(s) and the captured assistant response.
1023
- let accumulatedMessages = [];
1024
- // Accumulated UI messages for persistence. Mirrors the model accumulator
1025
- // but in frontend-friendly UIMessage format (with parts, id, etc.).
1026
- let accumulatedUIMessages = [];
1027
- // Token usage tracking across turns
1028
- let previousTurnUsage;
1029
- let cumulativeUsage = emptyUsage();
1030
- // Mutable reference to the current turn's stop controller so the
1031
- // stop input stream listener (registered once) can abort the right turn.
1032
- let currentStopController;
1033
- // Listen for stop signals for the lifetime of the run
1034
- const stopSub = stopInput.on((data) => {
1035
- currentStopController?.abort(data?.message || "stopped");
1036
- });
1037
- try {
1038
- // Handle preloaded runs — fire onPreload, then wait for the first real message
1039
- if (preloaded) {
1040
- if (activeSpan) {
1041
- activeSpan.setAttribute("chat.preloaded", true);
1042
- }
1043
- const currentRunId = ctx.run.id;
1044
- let preloadAccessToken = "";
1045
- if (currentRunId) {
1046
- try {
1047
- preloadAccessToken = await auth_js_1.auth.createPublicToken({
1048
- scopes: {
1049
- read: { runs: currentRunId },
1050
- write: { inputStreams: currentRunId },
1051
- },
1052
- expirationTime: chatAccessTokenTTL,
1053
- });
1054
- }
1055
- catch {
1056
- // Token creation failed
1057
- }
1058
- }
1059
- // Parse client data for the preload hook
1060
- const preloadClientData = (parseClientData ? await parseClientData(payload.metadata) : payload.metadata);
1061
- // Fire onPreload hook
1062
- if (onPreload) {
1063
- await tracer_js_1.tracer.startActiveSpan("onPreload()", async () => {
1064
- await withChatWriter(async (writer) => {
1065
- await onPreload({
1066
- ctx,
1067
- chatId: payload.chatId,
1068
- runId: currentRunId,
1069
- chatAccessToken: preloadAccessToken,
1070
- clientData: preloadClientData,
1071
- writer,
1072
- });
1073
- });
1074
- }, {
1075
- attributes: {
1076
- [v3_1.SemanticInternalAttributes.STYLE_ICON]: "task-hook-onStart",
1077
- [v3_1.SemanticInternalAttributes.COLLAPSED]: true,
1078
- "chat.id": payload.chatId,
1079
- "chat.preloaded": true,
1080
- },
1081
- });
1082
- }
1083
- // Wait for the first real message — use preload-specific timeouts if configured
1084
- const effectivePreloadIdleTimeout = payload.idleTimeoutInSeconds ?? preloadIdleTimeoutInSeconds ?? idleTimeoutInSeconds;
1085
- const effectivePreloadTimeout = metadata_js_1.metadata.get(TURN_TIMEOUT_METADATA_KEY) ??
1086
- preloadTimeout ??
1087
- turnTimeout;
1088
- const preloadResult = await messagesInput.waitWithIdleTimeout({
1089
- idleTimeoutInSeconds: effectivePreloadIdleTimeout,
1090
- timeout: effectivePreloadTimeout,
1091
- spanName: "waiting for first message",
1092
- });
1093
- if (!preloadResult.ok) {
1094
- return; // Timed out waiting for first message — end run
1095
- }
1096
- let firstMessage = preloadResult.output;
1097
- currentWirePayload = firstMessage;
1098
- }
1099
- for (let turn = 0; turn < maxTurns; turn++) {
1100
- // Extract turn-level context before entering the span
1101
- const { metadata: wireMetadata, messages: uiMessages, ...restWire } = currentWirePayload;
1102
- const clientData = (parseClientData ? await parseClientData(wireMetadata) : wireMetadata);
1103
- const lastUserMessage = extractLastUserMessageText(uiMessages);
1104
- const turnAttributes = {
1105
- "turn.number": turn + 1,
1106
- "gen_ai.conversation.id": currentWirePayload.chatId,
1107
- "gen_ai.operation.name": "chat",
1108
- "chat.trigger": currentWirePayload.trigger,
1109
- [v3_1.SemanticInternalAttributes.STYLE_ICON]: "tabler-message-chatbot",
1110
- [v3_1.SemanticInternalAttributes.ENTITY_TYPE]: "chat-turn",
1111
- };
1112
- if (lastUserMessage) {
1113
- turnAttributes["chat.user_message"] = lastUserMessage;
1114
- // Show a truncated preview of the user message as an accessory
1115
- const preview = lastUserMessage.length > 80 ? lastUserMessage.slice(0, 80) + "..." : lastUserMessage;
1116
- Object.assign(turnAttributes, (0, v3_1.accessoryAttributes)({
1117
- items: [{ text: preview, variant: "normal" }],
1118
- style: "codepath",
1119
- }));
1120
- }
1121
- if (wireMetadata !== undefined) {
1122
- turnAttributes["chat.client_data"] =
1123
- typeof wireMetadata === "string" ? wireMetadata : JSON.stringify(wireMetadata);
1124
- }
1125
- const turnResult = await tracer_js_1.tracer.startActiveSpan(`chat turn ${turn + 1}`, async (turnSpan) => {
1126
- locals_js_1.locals.set(chatPipeCountKey, 0);
1127
- locals_js_1.locals.set(chatDeferKey, new Set());
1128
- locals_js_1.locals.set(chatCompactionStateKey, undefined);
1129
- locals_js_1.locals.set(chatSteeringQueueKey, []);
1130
- // NOTE: chatBackgroundQueueKey is NOT reset here — messages injected
1131
- // by deferred work from the previous turn's onTurnComplete need to
1132
- // survive into the next turn. The queue is drained before run().
1133
- locals_js_1.locals.set(chatInjectedMessageIdsKey, new Set());
1134
- // Store chat context for auto-detection by task-tool subtasks (ai.toolExecute / legacy ai.tool)
1135
- locals_js_1.locals.set(chatTurnContextKey, {
1136
- chatId: currentWirePayload.chatId,
1137
- turn,
1138
- continuation,
1139
- clientData,
1140
- });
1141
- // Per-turn stop controller (reset each turn)
1142
- const stopController = new AbortController();
1143
- currentStopController = stopController;
1144
- locals_js_1.locals.set(chatStopControllerKey, stopController);
1145
- // Three signals for the user's run function
1146
- const stopSignal = stopController.signal;
1147
- const cancelSignal = runSignal;
1148
- const combinedSignal = AbortSignal.any([runSignal, stopController.signal]);
1149
- // Buffer messages that arrive during streaming
1150
- const pendingMessages = [];
1151
- const pmConfig = locals_js_1.locals.get(chatPendingMessagesKey);
1152
- const msgSub = messagesInput.on(async (msg) => {
1153
- // If pendingMessages is configured, route to the steering queue
1154
- // instead of the wire buffer. The frontend handles re-sending
1155
- // non-injected messages via sendMessage on turn complete.
1156
- if (pmConfig) {
1157
- const lastUIMessage = msg.messages?.[msg.messages.length - 1];
1158
- if (lastUIMessage) {
1159
- if (pmConfig.onReceived) {
1160
- try {
1161
- await pmConfig.onReceived({
1162
- message: lastUIMessage,
1163
- chatId: currentWirePayload.chatId,
1164
- turn,
1165
- });
1166
- }
1167
- catch {
1168
- /* non-fatal */
1169
- }
1170
- }
1171
- try {
1172
- const queue = locals_js_1.locals.get(chatSteeringQueueKey) ?? [];
1173
- // Deduplicate by message ID — guards against double-sends
1174
- if (lastUIMessage.id &&
1175
- queue.some((e) => e.uiMessage.id === lastUIMessage.id)) {
1176
- return;
1177
- }
1178
- const modelMsgs = await toModelMessages([lastUIMessage]);
1179
- queue.push({
1180
- uiMessage: lastUIMessage,
1181
- modelMessages: modelMsgs,
1182
- });
1183
- locals_js_1.locals.set(chatSteeringQueueKey, queue);
1184
- }
1185
- catch {
1186
- /* conversion failed — skip steering queue */
1187
- }
1188
- }
1189
- return; // Don't add to wire buffer — frontend handles non-injected case
1190
- }
1191
- // No pendingMessages config — standard wire buffer for next turn
1192
- pendingMessages.push(msg);
1193
- });
1194
- // Clean up any incomplete tool parts in the incoming history.
1195
- // When a previous run was stopped mid-tool-call, the frontend's
1196
- // useChat state may still contain assistant messages with tool parts
1197
- // in partial/input-available state. These cause API errors (e.g.
1198
- // Anthropic requires every tool_use to have a matching tool_result).
1199
- const cleanedUIMessages = uiMessages.map((msg) => msg.role === "assistant" ? cleanupAbortedParts(msg) : msg);
1200
- // Convert the incoming UIMessages to model messages and update the accumulator.
1201
- // Turn 1: full history from the frontend → replaces the accumulator.
1202
- // Turn 2+: only the new message(s) → appended to the accumulator.
1203
- const incomingModelMessages = await toModelMessages(cleanedUIMessages);
1204
- // Track new messages for this turn (user input + assistant response).
1205
- const turnNewModelMessages = [];
1206
- const turnNewUIMessages = [];
1207
- if (turn === 0) {
1208
- accumulatedMessages = incomingModelMessages;
1209
- accumulatedUIMessages = [...cleanedUIMessages];
1210
- // On first turn, the "new" messages are just the last user message
1211
- // (the rest is history). We'll add the response after streaming.
1212
- if (cleanedUIMessages.length > 0) {
1213
- turnNewUIMessages.push(cleanedUIMessages[cleanedUIMessages.length - 1]);
1214
- const lastModel = incomingModelMessages[incomingModelMessages.length - 1];
1215
- if (lastModel)
1216
- turnNewModelMessages.push(lastModel);
1217
- }
1218
- }
1219
- else if (currentWirePayload.trigger === "regenerate-message") {
1220
- // Regenerate: frontend sent full history with last assistant message
1221
- // removed. Reset the accumulator to match.
1222
- accumulatedMessages = incomingModelMessages;
1223
- accumulatedUIMessages = [...cleanedUIMessages];
1224
- // No new user messages for regenerate — just the response (added below)
1225
- }
1226
- else {
1227
- // Submit: frontend sent only the new user message(s). Append to accumulator.
1228
- accumulatedMessages.push(...incomingModelMessages);
1229
- accumulatedUIMessages.push(...cleanedUIMessages);
1230
- turnNewModelMessages.push(...incomingModelMessages);
1231
- turnNewUIMessages.push(...cleanedUIMessages);
1232
- }
1233
- // Mint a scoped public access token once per turn, reused for
1234
- // onChatStart, onTurnStart, onTurnComplete, and the turn-complete chunk.
1235
- const currentRunId = ctx.run.id;
1236
- let turnAccessToken = "";
1237
- if (currentRunId) {
1238
- try {
1239
- turnAccessToken = await auth_js_1.auth.createPublicToken({
1240
- scopes: {
1241
- read: { runs: currentRunId },
1242
- write: { inputStreams: currentRunId },
1243
- },
1244
- expirationTime: chatAccessTokenTTL,
1245
- });
1246
- }
1247
- catch {
1248
- // Token creation failed
1249
- }
1250
- }
1251
- // Fire onChatStart on the first turn
1252
- if (turn === 0 && onChatStart) {
1253
- await tracer_js_1.tracer.startActiveSpan("onChatStart()", async () => {
1254
- await withChatWriter(async (writer) => {
1255
- await onChatStart({
1256
- ctx,
1257
- chatId: currentWirePayload.chatId,
1258
- messages: accumulatedMessages,
1259
- clientData,
1260
- runId: currentRunId,
1261
- chatAccessToken: turnAccessToken,
1262
- continuation,
1263
- previousRunId,
1264
- preloaded,
1265
- writer,
1266
- });
1267
- });
1268
- }, {
1269
- attributes: {
1270
- [v3_1.SemanticInternalAttributes.STYLE_ICON]: "task-hook-onStart",
1271
- [v3_1.SemanticInternalAttributes.COLLAPSED]: true,
1272
- "chat.id": currentWirePayload.chatId,
1273
- "chat.messages.count": accumulatedMessages.length,
1274
- "chat.continuation": continuation,
1275
- "chat.preloaded": preloaded,
1276
- ...(previousRunId ? { "chat.previous_run_id": previousRunId } : {}),
1277
- },
1278
- });
1279
- }
1280
- // Fire onTurnStart before running user code — persist messages
1281
- // so a mid-stream page refresh still shows the user's message.
1282
- if (onTurnStart) {
1283
- await tracer_js_1.tracer.startActiveSpan("onTurnStart()", async () => {
1284
- await withChatWriter(async (writer) => {
1285
- await onTurnStart({
1286
- ctx,
1287
- chatId: currentWirePayload.chatId,
1288
- messages: accumulatedMessages,
1289
- uiMessages: accumulatedUIMessages,
1290
- turn,
1291
- runId: currentRunId,
1292
- chatAccessToken: turnAccessToken,
1293
- clientData,
1294
- continuation,
1295
- previousRunId,
1296
- preloaded,
1297
- previousTurnUsage,
1298
- totalUsage: cumulativeUsage,
1299
- writer,
1300
- });
1301
- });
1302
- // Check if onTurnStart replaced messages (compaction)
1303
- const turnStartOverride = locals_js_1.locals.get(chatOverrideMessagesKey);
1304
- if (turnStartOverride) {
1305
- locals_js_1.locals.set(chatOverrideMessagesKey, undefined);
1306
- accumulatedUIMessages = [...turnStartOverride];
1307
- accumulatedMessages = await toModelMessages(turnStartOverride);
1308
- }
1309
- }, {
1310
- attributes: {
1311
- [v3_1.SemanticInternalAttributes.STYLE_ICON]: "task-hook-onStart",
1312
- [v3_1.SemanticInternalAttributes.COLLAPSED]: true,
1313
- "chat.id": currentWirePayload.chatId,
1314
- "chat.turn": turn + 1,
1315
- "chat.messages.count": accumulatedMessages.length,
1316
- "chat.trigger": currentWirePayload.trigger,
1317
- "chat.continuation": continuation,
1318
- "chat.preloaded": preloaded,
1319
- ...(previousRunId ? { "chat.previous_run_id": previousRunId } : {}),
1320
- },
1321
- });
1322
- }
1323
- // Captured by the onFinish callback below — works even on abort/stop.
1324
- let capturedResponseMessage;
1325
- // Promise that resolves when the AI SDK's onFinish fires.
1326
- // On abort, the stream's cancel() handler calls onFinish
1327
- // asynchronously AFTER pipeChat resolves, so we must await
1328
- // this to avoid a race where we check capturedResponseMessage
1329
- // before it's been set.
1330
- let resolveOnFinish;
1331
- const onFinishPromise = new Promise((r) => {
1332
- resolveOnFinish = r;
1333
- });
1334
- let onFinishAttached = false;
1335
- let runResult;
1336
- try {
1337
- // Drain any messages injected by background work (e.g. self-review from previous turn)
1338
- const bgQueue = locals_js_1.locals.get(chatBackgroundQueueKey);
1339
- if (bgQueue && bgQueue.length > 0) {
1340
- accumulatedMessages.push(...bgQueue.splice(0));
1341
- }
1342
- runResult = await userRun({
1343
- ...restWire,
1344
- messages: await applyPrepareMessages(accumulatedMessages, "run"),
1345
- clientData,
1346
- continuation,
1347
- previousRunId,
1348
- preloaded,
1349
- previousTurnUsage,
1350
- totalUsage: cumulativeUsage,
1351
- ctx,
1352
- signal: combinedSignal,
1353
- cancelSignal,
1354
- stopSignal,
1355
- });
1356
- // Auto-pipe if the run function returned a StreamTextResult or similar,
1357
- // but only if pipeChat() wasn't already called manually during this turn.
1358
- // We call toUIMessageStream ourselves to attach onFinish for response capture.
1359
- if ((locals_js_1.locals.get(chatPipeCountKey) ?? 0) === 0 && isUIMessageStreamable(runResult)) {
1360
- onFinishAttached = true;
1361
- const uiStream = runResult.toUIMessageStream({
1362
- ...resolveUIMessageStreamOptions(),
1363
- onFinish: ({ responseMessage }) => {
1364
- capturedResponseMessage = responseMessage;
1365
- resolveOnFinish();
1366
- },
1367
- });
1368
- await pipeChat(uiStream, { signal: combinedSignal, spanName: "stream response" });
1369
- }
1370
- }
1371
- catch (error) {
1372
- // Handle AbortError from streamText gracefully
1373
- if (error instanceof Error && error.name === "AbortError") {
1374
- if (runSignal.aborted) {
1375
- return "exit"; // Full run cancellation — exit
1376
- }
1377
- // Stop generation — fall through to continue the loop
1378
- }
1379
- else {
1380
- throw error;
1381
- }
1382
- }
1383
- finally {
1384
- msgSub.off();
1385
- }
1386
- // Wait for onFinish to fire — on abort this may resolve slightly
1387
- // after pipeChat, since the stream's cancel() handler is async.
1388
- if (onFinishAttached) {
1389
- await onFinishPromise;
1390
- }
1391
- // Capture token usage from the streamText result (if available).
1392
- // totalUsage is a PromiseLike that resolves after the stream is consumed.
1393
- let turnUsage;
1394
- if (runResult != null && typeof runResult.totalUsage?.then === "function") {
1395
- try {
1396
- turnUsage = await runResult.totalUsage;
1397
- }
1398
- catch {
1399
- /* non-fatal — usage capture failed */
1400
- }
1401
- }
1402
- if (turnUsage) {
1403
- cumulativeUsage = addUsage(cumulativeUsage, turnUsage);
1404
- previousTurnUsage = turnUsage;
1405
- // Add usage attributes to the turn span
1406
- if (turnUsage.inputTokens != null) {
1407
- turnSpan.setAttribute("gen_ai.usage.input_tokens", turnUsage.inputTokens);
1408
- }
1409
- if (turnUsage.outputTokens != null) {
1410
- turnSpan.setAttribute("gen_ai.usage.output_tokens", turnUsage.outputTokens);
1411
- }
1412
- if (turnUsage.totalTokens != null) {
1413
- turnSpan.setAttribute("gen_ai.usage.total_tokens", turnUsage.totalTokens);
1414
- }
1415
- if (cumulativeUsage.totalTokens != null) {
1416
- turnSpan.setAttribute("gen_ai.usage.cumulative_total_tokens", cumulativeUsage.totalTokens);
1417
- }
1418
- if (cumulativeUsage.inputTokens != null) {
1419
- turnSpan.setAttribute("gen_ai.usage.cumulative_input_tokens", cumulativeUsage.inputTokens);
1420
- }
1421
- if (cumulativeUsage.outputTokens != null) {
1422
- turnSpan.setAttribute("gen_ai.usage.cumulative_output_tokens", cumulativeUsage.outputTokens);
1423
- }
1424
- }
1425
- // Check if run() (e.g. via prepareStep) replaced messages during this turn.
1426
- // This supports intra-turn compaction — the compacted messages become the
1427
- // new base, and the response gets appended on top.
1428
- const runOverride = locals_js_1.locals.get(chatOverrideMessagesKey);
1429
- if (runOverride) {
1430
- locals_js_1.locals.set(chatOverrideMessagesKey, undefined);
1431
- accumulatedUIMessages = [...runOverride];
1432
- accumulatedMessages = await toModelMessages(runOverride);
1433
- }
1434
- // Check if compaction set a model-only override (preserves UI messages).
1435
- // Apply compactUIMessages/compactModelMessages callbacks if configured.
1436
- const modelOnlyOverride = locals_js_1.locals.get(chatOverrideModelMessagesKey);
1437
- if (modelOnlyOverride) {
1438
- const compactionSummary = locals_js_1.locals.get(chatCompactionStateKey)?.summary ?? "";
1439
- const taskCompactionConfig = locals_js_1.locals.get(chatTaskCompactionKey);
1440
- locals_js_1.locals.set(chatOverrideModelMessagesKey, undefined);
1441
- const compactEvent = {
1442
- summary: compactionSummary,
1443
- uiMessages: accumulatedUIMessages,
1444
- modelMessages: accumulatedMessages,
1445
- chatId: currentWirePayload.chatId,
1446
- turn,
1447
- clientData,
1448
- source: "inner",
1449
- };
1450
- // Apply model messages: callback or default (use override)
1451
- accumulatedMessages = taskCompactionConfig?.compactModelMessages
1452
- ? await taskCompactionConfig.compactModelMessages(compactEvent)
1453
- : modelOnlyOverride;
1454
- // Apply UI messages: callback or default (preserve all)
1455
- if (taskCompactionConfig?.compactUIMessages) {
1456
- accumulatedUIMessages = (await taskCompactionConfig.compactUIMessages(compactEvent));
1457
- }
1458
- }
1459
- // Determine if the user stopped generation this turn (not a full run cancel).
1460
- const wasStopped = stopController.signal.aborted && !runSignal.aborted;
1461
- // Append the assistant's response (partial or complete) to the accumulator.
1462
- // The onFinish callback fires even on abort/stop, so partial responses
1463
- // from stopped generation are captured correctly.
1464
- let rawResponseMessage;
1465
- if (capturedResponseMessage) {
1466
- // Keep the raw message before cleanup for users who want custom handling
1467
- rawResponseMessage = capturedResponseMessage;
1468
- // Clean up aborted parts (streaming tool calls, reasoning) when stopped
1469
- if (wasStopped) {
1470
- capturedResponseMessage = cleanupAbortedParts(capturedResponseMessage);
1471
- }
1472
- // Ensure the response message has an ID (the stream's onFinish
1473
- // may produce a message with an empty ID since IDs are normally
1474
- // assigned by the frontend's useChat).
1475
- if (!capturedResponseMessage.id) {
1476
- capturedResponseMessage = { ...capturedResponseMessage, id: (0, ai_1.generateId)() };
1477
- }
1478
- accumulatedUIMessages.push(capturedResponseMessage);
1479
- turnNewUIMessages.push(capturedResponseMessage);
1480
- try {
1481
- const responseModelMessages = await toModelMessages([
1482
- stripProviderMetadata(capturedResponseMessage),
1483
- ]);
1484
- accumulatedMessages.push(...responseModelMessages);
1485
- turnNewModelMessages.push(...responseModelMessages);
1486
- }
1487
- catch {
1488
- // Conversion failed — skip accumulation for this turn
1489
- }
1490
- }
1491
- // TODO: When the user calls `pipeChat` manually instead of returning a
1492
- // StreamTextResult, we don't have access to onFinish. A future iteration
1493
- // should let manual-mode users report back response messages for
1494
- // accumulation (e.g. via a `chat.addMessages()` helper).
1495
- if (runSignal.aborted)
1496
- return "exit";
1497
- // Await deferred background work (e.g. DB writes from onTurnStart)
1498
- // before firing hooks so they can rely on the work being done.
1499
- const deferredWork = locals_js_1.locals.get(chatDeferKey);
1500
- if (deferredWork && deferredWork.size > 0) {
1501
- await Promise.race([
1502
- Promise.allSettled(deferredWork),
1503
- new Promise((r) => setTimeout(r, 5_000)),
1504
- ]);
1505
- }
1506
- // Outer-loop compaction: runs between turns for single-step responses
1507
- // where prepareStep never fires (no tool calls = no step boundaries).
1508
- // Only triggers when: task has compaction configured, prepareStep didn't
1509
- // already compact this turn, and shouldCompact returns true.
1510
- const outerCompaction = locals_js_1.locals.get(chatTaskCompactionKey);
1511
- const innerCompactionState = locals_js_1.locals.get(chatCompactionStateKey);
1512
- if (outerCompaction && !innerCompactionState && turnUsage && !wasStopped) {
1513
- const shouldTrigger = await outerCompaction.shouldCompact({
1514
- messages: accumulatedMessages,
1515
- totalTokens: turnUsage.totalTokens,
1516
- inputTokens: turnUsage.inputTokens,
1517
- outputTokens: turnUsage.outputTokens,
1518
- usage: turnUsage,
1519
- totalUsage: cumulativeUsage,
1520
- chatId: currentWirePayload.chatId,
1521
- turn,
1522
- clientData,
1523
- source: "outer",
1524
- });
1525
- if (shouldTrigger) {
1526
- await tracer_js_1.tracer.startActiveSpan("context compaction (outer loop)", async (compactionSpan) => {
1527
- const compactionId = (0, ai_1.generateId)();
1528
- const { waitUntilComplete } = streams_js_1.streams.writer(exports.CHAT_STREAM_KEY, {
1529
- spanName: "stream compaction chunks",
1530
- collapsed: true,
1531
- execute: async ({ write, merge }) => {
1532
- write({
1533
- type: "data-compaction",
1534
- id: compactionId,
1535
- data: { status: "compacting", totalTokens: turnUsage.totalTokens },
1536
- });
1537
- const summary = await outerCompaction.summarize({
1538
- messages: accumulatedMessages,
1539
- usage: turnUsage,
1540
- totalUsage: cumulativeUsage,
1541
- chatId: currentWirePayload.chatId,
1542
- turn,
1543
- clientData,
1544
- source: "outer",
1545
- });
1546
- // Apply compactModelMessages/compactUIMessages callbacks, or defaults.
1547
- const outerCompactEvent = {
1548
- summary,
1549
- uiMessages: accumulatedUIMessages,
1550
- modelMessages: accumulatedMessages,
1551
- chatId: currentWirePayload.chatId,
1552
- turn,
1553
- clientData,
1554
- source: "outer",
1555
- };
1556
- // Model messages: callback or default (replace with summary)
1557
- accumulatedMessages = outerCompaction.compactModelMessages
1558
- ? await outerCompaction.compactModelMessages(outerCompactEvent)
1559
- : [
1560
- {
1561
- role: "assistant",
1562
- content: [
1563
- {
1564
- type: "text",
1565
- text: `[Conversation summary]\n\n${summary}`,
1566
- },
1567
- ],
1568
- },
1569
- ];
1570
- // UI messages: callback or default (preserve all)
1571
- if (outerCompaction.compactUIMessages) {
1572
- accumulatedUIMessages = (await outerCompaction.compactUIMessages(outerCompactEvent));
1573
- }
1574
- // Fire onCompacted hook
1575
- const onCompactedHook = locals_js_1.locals.get(chatOnCompactedKey);
1576
- if (onCompactedHook) {
1577
- await onCompactedHook({
1578
- ctx,
1579
- summary,
1580
- messages: accumulatedMessages,
1581
- messageCount: accumulatedMessages.length,
1582
- usage: turnUsage,
1583
- totalTokens: turnUsage.totalTokens,
1584
- inputTokens: turnUsage.inputTokens,
1585
- outputTokens: turnUsage.outputTokens,
1586
- stepNumber: -1, // outer loop, not a step
1587
- chatId: currentWirePayload.chatId,
1588
- turn,
1589
- writer: { write, merge },
1590
- });
1591
- }
1592
- compactionSpan.setAttribute("compaction.summary_length", summary.length);
1593
- write({
1594
- type: "data-compaction",
1595
- id: compactionId,
1596
- data: { status: "complete", totalTokens: turnUsage.totalTokens },
1597
- });
1598
- },
1599
- });
1600
- await waitUntilComplete();
1601
- }, {
1602
- attributes: {
1603
- [v3_1.SemanticInternalAttributes.STYLE_ICON]: "tabler-scissors",
1604
- "compaction.total_tokens": turnUsage.totalTokens ?? 0,
1605
- "compaction.input_tokens": turnUsage.inputTokens ?? 0,
1606
- "compaction.message_count": accumulatedMessages.length,
1607
- "compaction.outer_loop": true,
1608
- "compaction.turn": turn,
1609
- ...(currentWirePayload.chatId
1610
- ? { "compaction.chat_id": currentWirePayload.chatId }
1611
- : {}),
1612
- ...(0, v3_1.accessoryAttributes)({
1613
- items: [
1614
- { text: `${turnUsage.totalTokens ?? 0} tokens`, variant: "normal" },
1615
- { text: `${accumulatedMessages.length} msgs`, variant: "normal" },
1616
- { text: "outer loop", variant: "normal" },
1617
- ],
1618
- style: "codepath",
1619
- }),
1620
- },
1621
- });
1622
- }
1623
- }
1624
- const turnCompleteEvent = {
1625
- ctx,
1626
- chatId: currentWirePayload.chatId,
1627
- messages: accumulatedMessages,
1628
- uiMessages: accumulatedUIMessages,
1629
- newMessages: turnNewModelMessages,
1630
- newUIMessages: turnNewUIMessages,
1631
- responseMessage: capturedResponseMessage,
1632
- rawResponseMessage,
1633
- turn,
1634
- runId: currentRunId,
1635
- chatAccessToken: turnAccessToken,
1636
- clientData,
1637
- stopped: wasStopped,
1638
- continuation,
1639
- previousRunId,
1640
- preloaded,
1641
- usage: turnUsage,
1642
- totalUsage: cumulativeUsage,
1643
- };
1644
- // Fire onBeforeTurnComplete — stream is still open so the hook
1645
- // can write custom chunks to the frontend (e.g. compaction progress).
1646
- if (onBeforeTurnComplete) {
1647
- await tracer_js_1.tracer.startActiveSpan("onBeforeTurnComplete()", async () => {
1648
- await withChatWriter(async (writer) => {
1649
- await onBeforeTurnComplete({ ...turnCompleteEvent, writer });
1650
- });
1651
- // Check if the hook replaced messages (compaction)
1652
- const override = locals_js_1.locals.get(chatOverrideMessagesKey);
1653
- if (override) {
1654
- locals_js_1.locals.set(chatOverrideMessagesKey, undefined);
1655
- accumulatedUIMessages = [...override];
1656
- accumulatedMessages = await toModelMessages(override);
1657
- // Update event so onTurnComplete sees compacted messages
1658
- turnCompleteEvent.messages = accumulatedMessages;
1659
- turnCompleteEvent.uiMessages = accumulatedUIMessages;
1660
- }
1661
- }, {
1662
- attributes: {
1663
- [v3_1.SemanticInternalAttributes.STYLE_ICON]: "task-hook-onComplete",
1664
- [v3_1.SemanticInternalAttributes.COLLAPSED]: true,
1665
- "chat.id": currentWirePayload.chatId,
1666
- "chat.turn": turn + 1,
1667
- },
1668
- });
1669
- }
1670
- // Write turn-complete control chunk — closes the frontend stream.
1671
- const turnCompleteResult = await writeTurnCompleteChunk(currentWirePayload.chatId, turnAccessToken);
1672
- // Fire onTurnComplete — stream is closed, use for persistence.
1673
- if (onTurnComplete) {
1674
- await tracer_js_1.tracer.startActiveSpan("onTurnComplete()", async () => {
1675
- await onTurnComplete({
1676
- ...turnCompleteEvent,
1677
- lastEventId: turnCompleteResult.lastEventId,
1678
- });
1679
- // Check if onTurnComplete replaced messages (compaction)
1680
- const turnCompleteOverride = locals_js_1.locals.get(chatOverrideMessagesKey);
1681
- if (turnCompleteOverride) {
1682
- locals_js_1.locals.set(chatOverrideMessagesKey, undefined);
1683
- accumulatedUIMessages = [...turnCompleteOverride];
1684
- accumulatedMessages = await toModelMessages(turnCompleteOverride);
1685
- }
1686
- }, {
1687
- attributes: {
1688
- [v3_1.SemanticInternalAttributes.STYLE_ICON]: "task-hook-onComplete",
1689
- [v3_1.SemanticInternalAttributes.COLLAPSED]: true,
1690
- "chat.id": currentWirePayload.chatId,
1691
- "chat.turn": turn + 1,
1692
- "chat.stopped": wasStopped,
1693
- "chat.continuation": continuation,
1694
- "chat.preloaded": preloaded,
1695
- ...(previousRunId ? { "chat.previous_run_id": previousRunId } : {}),
1696
- "chat.messages.count": accumulatedMessages.length,
1697
- "chat.response.parts.count": capturedResponseMessage?.parts?.length ?? 0,
1698
- "chat.new_messages.count": turnNewUIMessages.length,
1699
- ...(turnUsage?.inputTokens != null
1700
- ? { "gen_ai.usage.input_tokens": turnUsage.inputTokens }
1701
- : {}),
1702
- ...(turnUsage?.outputTokens != null
1703
- ? { "gen_ai.usage.output_tokens": turnUsage.outputTokens }
1704
- : {}),
1705
- ...(turnUsage?.totalTokens != null
1706
- ? { "gen_ai.usage.total_tokens": turnUsage.totalTokens }
1707
- : {}),
1708
- ...(cumulativeUsage.totalTokens != null
1709
- ? { "gen_ai.usage.cumulative_total_tokens": cumulativeUsage.totalTokens }
1710
- : {}),
1711
- },
1712
- });
1713
- }
1714
- // NOTE: We intentionally do NOT await deferred work from onTurnComplete here.
1715
- // Promises deferred in onTurnComplete (e.g. background self-review via
1716
- // chat.defer + chat.inject) run during the idle wait. If they complete
1717
- // before the next message, their injected context is picked up in prepareStep.
1718
- // The pre-onBeforeTurnComplete drain handles promises from onTurnStart/run().
1719
- // If messages arrived during streaming (without pendingMessages config),
1720
- // use the first one immediately as the next turn.
1721
- if (pendingMessages.length > 0) {
1722
- currentWirePayload = pendingMessages[0];
1723
- return "continue";
1724
- }
1725
- // Wait for the next message — stay idle briefly, then suspend
1726
- const effectiveIdleTimeout = metadata_js_1.metadata.get(IDLE_TIMEOUT_METADATA_KEY) ??
1727
- idleTimeoutInSeconds;
1728
- const effectiveTurnTimeout = metadata_js_1.metadata.get(TURN_TIMEOUT_METADATA_KEY) ?? turnTimeout;
1729
- const next = await messagesInput.waitWithIdleTimeout({
1730
- idleTimeoutInSeconds: effectiveIdleTimeout,
1731
- timeout: effectiveTurnTimeout,
1732
- spanName: "waiting for next message",
1733
- });
1734
- if (!next.ok) {
1735
- return "exit";
1736
- }
1737
- currentWirePayload = next.output;
1738
- return "continue";
1739
- }, {
1740
- attributes: turnAttributes,
1741
- });
1742
- if (turnResult === "exit")
1743
- return;
1744
- // "continue" means proceed to next iteration
1745
- }
1746
- }
1747
- finally {
1748
- stopSub.off();
1749
- }
1750
- },
1751
- });
1752
- }
1753
- /**
1754
- * Fix the UI message type for a chat task (AI SDK `UIMessage` generics) while
1755
- * keeping `id` and `clientDataSchema` inference on the inner {@link chat.task} call.
1756
- *
1757
- * @example
1758
- * ```ts
1759
- * type AgentUiMessage = UIMessage<unknown, UIDataTypes, UITools>;
1760
- *
1761
- * export const myChat = chat.withUIMessage<AgentUiMessage>({
1762
- * streamOptions: { sendReasoning: true },
1763
- * }).task({
1764
- * id: "my-chat",
1765
- * run: async ({ messages, signal }) => { ... },
1766
- * });
1767
- * ```
1768
- */
1769
- function withUIMessage(config) {
1770
- function taskWithUiMessage(options) {
1771
- const mergedUiStream = config?.streamOptions && options.uiMessageStreamOptions
1772
- ? { ...config.streamOptions, ...options.uiMessageStreamOptions }
1773
- : options.uiMessageStreamOptions ?? config?.streamOptions;
1774
- return chatTask({
1775
- ...options,
1776
- uiMessageStreamOptions: mergedUiStream,
1777
- });
1778
- }
1779
- return { task: taskWithUiMessage };
1780
- }
1781
- /**
1782
- * Namespace for AI SDK chat integration.
1783
- *
1784
- * @example
1785
- * ```ts
1786
- * import { chat } from "@trigger.dev/sdk/ai";
1787
- *
1788
- * // Define a chat task
1789
- * export const myChat = chat.task({
1790
- * id: "my-chat",
1791
- * run: async ({ messages, signal }) => {
1792
- * return streamText({ model, messages, abortSignal: signal });
1793
- * },
1794
- * });
1795
- *
1796
- * // Pipe a stream manually (from inside a task)
1797
- * await chat.pipe(streamTextResult);
1798
- *
1799
- * // Create an access token (from a server action)
1800
- * const token = await chat.createAccessToken("my-chat");
1801
- * ```
1802
- */
1803
- // ---------------------------------------------------------------------------
1804
- // Runtime configuration helpers
1805
- // ---------------------------------------------------------------------------
1806
- const TURN_TIMEOUT_METADATA_KEY = "chat.turnTimeout";
1807
- const IDLE_TIMEOUT_METADATA_KEY = "chat.idleTimeout";
1808
- /**
1809
- * Override the turn timeout for subsequent turns in the current run.
1810
- *
1811
- * The turn timeout controls how long the run stays suspended (freeing compute)
1812
- * waiting for the next user message. When it expires, the run completes
1813
- * gracefully and the next message starts a fresh run.
1814
- *
1815
- * Call from inside a `chatTask` run function to adjust based on context.
1816
- *
1817
- * @param duration - A duration string (e.g. `"5m"`, `"1h"`, `"30s"`)
1818
- *
1819
- * @example
1820
- * ```ts
1821
- * run: async ({ messages, signal }) => {
1822
- * chat.setTurnTimeout("2h");
1823
- * return streamText({ model, messages, abortSignal: signal });
1824
- * }
1825
- * ```
1826
- */
1827
- function setTurnTimeout(duration) {
1828
- metadata_js_1.metadata.set(TURN_TIMEOUT_METADATA_KEY, duration);
1829
- }
1830
- /**
1831
- * Override the turn timeout in seconds for subsequent turns in the current run.
1832
- *
1833
- * @param seconds - Number of seconds to wait for the next message before ending the run
1834
- *
1835
- * @example
1836
- * ```ts
1837
- * run: async ({ messages, signal }) => {
1838
- * chat.setTurnTimeoutInSeconds(3600); // 1 hour
1839
- * return streamText({ model, messages, abortSignal: signal });
1840
- * }
1841
- * ```
1842
- */
1843
- function setTurnTimeoutInSeconds(seconds) {
1844
- metadata_js_1.metadata.set(TURN_TIMEOUT_METADATA_KEY, `${seconds}s`);
1845
- }
1846
- /**
1847
- * Override the idle timeout for subsequent turns in the current run.
1848
- *
1849
- * The idle timeout controls how long the run stays active (using compute)
1850
- * after each turn, waiting for the next message. During this window,
1851
- * responses are instant. After it expires, the run suspends.
1852
- *
1853
- * @param seconds - Number of seconds to stay idle (0 to suspend immediately)
1854
- *
1855
- * @example
1856
- * ```ts
1857
- * run: async ({ messages, signal }) => {
1858
- * chat.setIdleTimeoutInSeconds(60);
1859
- * return streamText({ model, messages, abortSignal: signal });
1860
- * }
1861
- * ```
1862
- */
1863
- function setIdleTimeoutInSeconds(seconds) {
1864
- metadata_js_1.metadata.set(IDLE_TIMEOUT_METADATA_KEY, seconds);
1865
- }
1866
- /**
1867
- * Override the `toUIMessageStream()` options for the current turn.
1868
- *
1869
- * These options control how the `StreamTextResult` is converted to a
1870
- * `UIMessageChunk` stream — error handling, reasoning/source visibility,
1871
- * message metadata, etc.
1872
- *
1873
- * Per-turn options are merged on top of the static `uiMessageStreamOptions`
1874
- * set on `chat.task()`. Per-turn values win on conflicts.
1875
- *
1876
- * @example
1877
- * ```ts
1878
- * run: async ({ messages, signal }) => {
1879
- * chat.setUIMessageStreamOptions({
1880
- * sendReasoning: true,
1881
- * onError: (error) => error instanceof Error ? error.message : "An error occurred.",
1882
- * });
1883
- * return streamText({ model, messages, abortSignal: signal });
1884
- * }
1885
- * ```
1886
- */
1887
- function setUIMessageStreamOptions(options) {
1888
- locals_js_1.locals.set(chatUIStreamPerTurnKey, options);
1889
- }
1890
- /**
1891
- * Resolve the effective UIMessageStream options by merging:
1892
- * 1. Static task-level options (from `chat.task({ uiMessageStreamOptions })`)
1893
- * 2. Per-turn overrides (from `chat.setUIMessageStreamOptions()`)
1894
- *
1895
- * Per-turn values win on conflicts. Clears the per-turn override after reading
1896
- * so it doesn't leak into subsequent turns.
1897
- * @internal
1898
- */
1899
- function resolveUIMessageStreamOptions() {
1900
- const staticOptions = locals_js_1.locals.get(chatUIStreamStaticKey) ?? {};
1901
- const perTurnOptions = locals_js_1.locals.get(chatUIStreamPerTurnKey) ?? {};
1902
- // Clear per-turn override so it doesn't leak into subsequent turns
1903
- locals_js_1.locals.set(chatUIStreamPerTurnKey, undefined);
1904
- return { ...staticOptions, ...perTurnOptions };
1905
- }
1906
- // ---------------------------------------------------------------------------
1907
- // Stop detection
1908
- // ---------------------------------------------------------------------------
1909
- /**
1910
- * Check whether the user stopped generation during the current turn.
1911
- *
1912
- * Works from **anywhere** inside a `chat.task` run — including inside
1913
- * `streamText`'s `onFinish` callback — without needing to thread the
1914
- * `stopSignal` through closures.
1915
- *
1916
- * This is especially useful when the AI SDK's `isAborted` flag is unreliable
1917
- * (e.g. when using `createUIMessageStream` + `writer.merge()`).
1918
- *
1919
- * @example
1920
- * ```ts
1921
- * onFinish: ({ isAborted }) => {
1922
- * const wasStopped = isAborted || chat.isStopped();
1923
- * if (wasStopped) {
1924
- * // handle stop
1925
- * }
1926
- * }
1927
- * ```
1928
- */
1929
- function isStopped() {
1930
- const controller = locals_js_1.locals.get(chatStopControllerKey);
1931
- return controller?.signal.aborted ?? false;
1932
- }
1933
- // ---------------------------------------------------------------------------
1934
- // Per-turn deferred work
1935
- // ---------------------------------------------------------------------------
1936
- /**
1937
- * Register a promise that runs in the background during the current turn.
1938
- *
1939
- * Use this to move non-blocking work (DB writes, analytics, etc.) out of
1940
- * the critical path. The promise runs in parallel with streaming and is
1941
- * awaited (with a 5 s timeout) before `onTurnComplete` fires.
1942
- *
1943
- * @example
1944
- * ```ts
1945
- * onTurnStart: async ({ chatId, uiMessages }) => {
1946
- * // Persist messages without blocking the LLM call
1947
- * chat.defer(db.chat.update({ where: { id: chatId }, data: { messages: uiMessages } }));
1948
- * },
1949
- * ```
1950
- */
1951
- function chatDefer(promise) {
1952
- const promises = locals_js_1.locals.get(chatDeferKey);
1953
- if (promises) {
1954
- promises.add(promise);
1955
- }
1956
- }
1957
- // ---------------------------------------------------------------------------
1958
- // Background context injection
1959
- // ---------------------------------------------------------------------------
1960
- /**
1961
- * Queue model messages for injection at the next `prepareStep` boundary.
1962
- *
1963
- * Use this to inject context from background work into the agent's conversation.
1964
- * Messages are appended to the model messages before the next LLM inference call.
1965
- *
1966
- * Combine with `chat.defer()` to run background analysis and inject results:
1967
- *
1968
- * @example
1969
- * ```ts
1970
- * onTurnComplete: async ({ messages }) => {
1971
- * chat.defer((async () => {
1972
- * const review = await generateObject({
1973
- * model: openai("gpt-4o-mini"),
1974
- * messages: [...messages, { role: "user", content: "Review the last response." }],
1975
- * schema: z.object({ suggestions: z.array(z.string()) }),
1976
- * });
1977
- * if (review.object.suggestions.length > 0) {
1978
- * chat.inject([{
1979
- * role: "system",
1980
- * content: `Improvements for next response:\n${review.object.suggestions.join("\n")}`,
1981
- * }]);
1982
- * }
1983
- * })());
1984
- * },
1985
- * ```
1986
- */
1987
- function injectBackgroundContext(messages) {
1988
- const queue = locals_js_1.locals.get(chatBackgroundQueueKey) ?? [];
1989
- queue.push(...messages);
1990
- locals_js_1.locals.set(chatBackgroundQueueKey, queue);
1991
- }
1992
- // ---------------------------------------------------------------------------
1993
- // Aborted message cleanup
1994
- // ---------------------------------------------------------------------------
1995
- /**
1996
- * Clean up a UIMessage that was captured during an aborted/stopped turn.
1997
- *
1998
- * When generation is stopped mid-stream, the captured message may contain:
1999
- * - Tool parts stuck in incomplete states (`partial-call`, `input-available`,
2000
- * `input-streaming`) that cause permanent UI spinners
2001
- * - Reasoning parts with `state: "streaming"` instead of `"done"`
2002
- * - Text parts with `state: "streaming"` instead of `"done"`
2003
- *
2004
- * This function returns a cleaned copy with:
2005
- * - Incomplete tool parts removed entirely
2006
- * - Reasoning and text parts marked as `"done"`
2007
- *
2008
- * `chat.task` calls this automatically when stop is detected before passing
2009
- * the response to `onTurnComplete`. Use this manually when calling `pipeChat`
2010
- * directly and capturing response messages yourself.
2011
- *
2012
- * @example
2013
- * ```ts
2014
- * onTurnComplete: async ({ responseMessage, stopped }) => {
2015
- * // Already cleaned automatically by chat.task — but if you captured
2016
- * // your own message via pipeChat, clean it manually:
2017
- * const cleaned = chat.cleanupAbortedParts(myMessage);
2018
- * await db.messages.save(cleaned);
2019
- * }
2020
- * ```
2021
- */
2022
- function cleanupAbortedParts(message) {
2023
- if (!message.parts)
2024
- return message;
2025
- const isToolPart = (part) => part.type === "tool-invocation" ||
2026
- part.type?.startsWith("tool-") ||
2027
- part.type === "dynamic-tool";
2028
- return {
2029
- ...message,
2030
- parts: message.parts
2031
- .filter((part) => {
2032
- if (!isToolPart(part))
2033
- return true;
2034
- // Remove tool parts that never completed execution.
2035
- // partial-call: input was still streaming when aborted.
2036
- // input-available: input was complete but tool never ran.
2037
- // input-streaming: input was mid-stream.
2038
- const state = part.toolInvocation?.state ?? part.state;
2039
- return (state !== "partial-call" && state !== "input-available" && state !== "input-streaming");
2040
- })
2041
- .map((part) => {
2042
- // Mark streaming reasoning as done
2043
- if (part.type === "reasoning" && part.state === "streaming") {
2044
- return { ...part, state: "done" };
2045
- }
2046
- // Mark streaming text as done
2047
- if (part.type === "text" && part.state === "streaming") {
2048
- return { ...part, state: "done" };
2049
- }
2050
- return part;
2051
- }),
2052
- };
2053
- }
2054
- // ---------------------------------------------------------------------------
2055
- // Composable primitives for raw task chat
2056
- // ---------------------------------------------------------------------------
2057
- /**
2058
- * Create a managed stop signal wired to the chat stop input stream.
2059
- *
2060
- * Call once at the start of your run. Use `signal` as the abort signal for
2061
- * `streamText`. Call `reset()` at the start of each turn to get a fresh
2062
- * per-turn signal. Call `cleanup()` when the run ends.
2063
- *
2064
- * @example
2065
- * ```ts
2066
- * const stop = chat.createStopSignal();
2067
- * for (let turn = 0; turn < 100; turn++) {
2068
- * stop.reset();
2069
- * const result = streamText({ model, messages, abortSignal: stop.signal });
2070
- * await chat.pipe(result);
2071
- * // ...
2072
- * }
2073
- * stop.cleanup();
2074
- * ```
2075
- */
2076
- function createStopSignal() {
2077
- let controller = new AbortController();
2078
- const sub = stopInput.on((data) => {
2079
- controller.abort(data?.message || "stopped");
2080
- });
2081
- return {
2082
- get signal() {
2083
- return controller.signal;
2084
- },
2085
- reset() {
2086
- controller = new AbortController();
2087
- },
2088
- cleanup() {
2089
- sub.off();
2090
- },
2091
- };
2092
- }
2093
- /**
2094
- * Signal the frontend that the current turn is complete.
2095
- *
2096
- * The `TriggerChatTransport` intercepts this to close the ReadableStream
2097
- * for the current turn. Call after piping the response stream.
2098
- *
2099
- * @example
2100
- * ```ts
2101
- * await chat.pipe(result);
2102
- * await chat.writeTurnComplete();
2103
- * ```
2104
- */
2105
- async function chatWriteTurnComplete(options) {
2106
- await writeTurnCompleteChunk(undefined, options?.publicAccessToken);
2107
- }
2108
- /**
2109
- * Pipe a `StreamTextResult` (or similar) to the chat stream and capture
2110
- * the assistant's response message via `onFinish`.
2111
- *
2112
- * Combines `toUIMessageStream()` + `onFinish` callback + `chat.pipe()`.
2113
- * Returns the captured `UIMessage`, or `undefined` if capture failed.
2114
- *
2115
- * @example
2116
- * ```ts
2117
- * const result = streamText({ model, messages, abortSignal: signal });
2118
- * const response = await chat.pipeAndCapture(result, { signal });
2119
- * if (response) conversation.addResponse(response);
2120
- * ```
2121
- */
2122
- async function pipeChatAndCapture(source, options) {
2123
- let captured;
2124
- let resolveOnFinish;
2125
- const onFinishPromise = new Promise((r) => {
2126
- resolveOnFinish = r;
2127
- });
2128
- const uiStream = source.toUIMessageStream({
2129
- ...resolveUIMessageStreamOptions(),
2130
- onFinish: ({ responseMessage }) => {
2131
- captured = responseMessage;
2132
- resolveOnFinish();
2133
- },
2134
- });
2135
- await pipeChat(uiStream, {
2136
- signal: options?.signal,
2137
- spanName: options?.spanName ?? "stream response",
2138
- });
2139
- await onFinishPromise;
2140
- return captured;
2141
- }
2142
- /**
2143
- * Accumulates conversation messages across turns.
2144
- *
2145
- * Handles the transport protocol: turn 0 sends full history (replace),
2146
- * subsequent turns send only new messages (append), regenerate sends
2147
- * full history minus last assistant message (replace).
2148
- *
2149
- * @example
2150
- * ```ts
2151
- * const conversation = new chat.MessageAccumulator();
2152
- * for (let turn = 0; turn < 100; turn++) {
2153
- * const messages = await conversation.addIncoming(payload.messages, payload.trigger, turn);
2154
- * const result = streamText({ model, messages });
2155
- * const response = await chat.pipeAndCapture(result);
2156
- * if (response) await conversation.addResponse(response);
2157
- * }
2158
- * ```
2159
- */
2160
- class ChatMessageAccumulator {
2161
- modelMessages = [];
2162
- uiMessages = [];
2163
- _compaction;
2164
- _pendingMessages;
2165
- _steeringQueue = [];
2166
- constructor(options) {
2167
- this._compaction = options?.compaction;
2168
- this._pendingMessages = options?.pendingMessages;
2169
- }
2170
- /**
2171
- * Add incoming messages from the transport payload.
2172
- * Returns the full accumulated model messages for `streamText`.
2173
- */
2174
- async addIncoming(messages, trigger, turn) {
2175
- const cleaned = messages.map((m) => (m.role === "assistant" ? cleanupAbortedParts(m) : m));
2176
- const model = await toModelMessages(cleaned);
2177
- if (turn === 0 || trigger === "regenerate-message") {
2178
- this.modelMessages = model;
2179
- this.uiMessages = [...cleaned];
2180
- }
2181
- else {
2182
- this.modelMessages.push(...model);
2183
- this.uiMessages.push(...cleaned);
2184
- }
2185
- return this.modelMessages;
2186
- }
2187
- /**
2188
- * Add the assistant's response to the accumulator.
2189
- * Call after `pipeAndCapture` with the captured response.
2190
- */
2191
- /**
2192
- * Replace all accumulated messages (for compaction).
2193
- * Converts UIMessages to ModelMessages internally.
2194
- */
2195
- async setMessages(uiMessages) {
2196
- this.uiMessages = [...uiMessages];
2197
- this.modelMessages = await toModelMessages(uiMessages);
2198
- }
2199
- async addResponse(response) {
2200
- if (!response.id) {
2201
- response = { ...response, id: (0, ai_1.generateId)() };
2202
- }
2203
- this.uiMessages.push(response);
2204
- try {
2205
- const msgs = await toModelMessages([stripProviderMetadata(response)]);
2206
- this.modelMessages.push(...msgs);
2207
- }
2208
- catch {
2209
- // Conversion failed — skip model message accumulation for this response
2210
- }
2211
- }
2212
- /**
2213
- * Queue a message for injection via `prepareStep`. Call from a
2214
- * `messagesInput.on()` listener when a message arrives during streaming.
2215
- */
2216
- steer(message, modelMessages) {
2217
- if (modelMessages) {
2218
- this._steeringQueue.push({ uiMessage: message, modelMessages });
2219
- }
2220
- else {
2221
- // Defer conversion — will be done in prepareStep if needed
2222
- this._steeringQueue.push({ uiMessage: message, modelMessages: [] });
2223
- }
2224
- }
2225
- /**
2226
- * Queue a message for injection, converting to model messages automatically.
2227
- */
2228
- async steerAsync(message) {
2229
- const modelMsgs = await toModelMessages([message]);
2230
- this._steeringQueue.push({ uiMessage: message, modelMessages: modelMsgs });
2231
- }
2232
- /**
2233
- * Get and clear unconsumed steering messages.
2234
- */
2235
- drainSteering() {
2236
- const result = this._steeringQueue.map((e) => e.uiMessage);
2237
- this._steeringQueue = [];
2238
- return result;
2239
- }
2240
- /**
2241
- * Returns a `prepareStep` function that handles both compaction and
2242
- * pending message injection. Pass to `streamText({ prepareStep: conversation.prepareStep() })`.
2243
- */
2244
- prepareStep() {
2245
- if (!this._compaction && !this._pendingMessages)
2246
- return undefined;
2247
- const comp = this._compaction;
2248
- const pm = this._pendingMessages;
2249
- const queue = this._steeringQueue;
2250
- return async ({ messages, steps }) => {
2251
- let resultMessages;
2252
- // 1. Compaction
2253
- if (comp) {
2254
- const result = await chatCompact(messages, steps, {
2255
- shouldCompact: comp.shouldCompact,
2256
- summarize: (msgs) => comp.summarize({ messages: msgs, source: "inner" }),
2257
- });
2258
- if (result.type !== "skipped") {
2259
- resultMessages = result.messages;
2260
- }
2261
- }
2262
- // 2. Pending message injection
2263
- if (pm && queue.length > 0) {
2264
- const injected = await drainSteeringQueue(pm, resultMessages ?? messages, steps, queue);
2265
- if (injected.length > 0) {
2266
- resultMessages = [...(resultMessages ?? messages), ...injected];
2267
- }
2268
- }
2269
- return resultMessages ? { messages: resultMessages } : undefined;
2270
- };
2271
- }
2272
- /**
2273
- * Run outer-loop compaction if needed. Call after adding the response
2274
- * and capturing usage. Applies `compactModelMessages` and `compactUIMessages`
2275
- * callbacks if configured.
2276
- *
2277
- * @returns `true` if compaction was performed, `false` otherwise.
2278
- */
2279
- async compactIfNeeded(usage, context) {
2280
- if (!this._compaction || !usage)
2281
- return false;
2282
- const shouldTrigger = await this._compaction.shouldCompact({
2283
- messages: this.modelMessages,
2284
- totalTokens: usage.totalTokens,
2285
- inputTokens: usage.inputTokens,
2286
- outputTokens: usage.outputTokens,
2287
- usage,
2288
- totalUsage: context?.totalUsage,
2289
- chatId: context?.chatId,
2290
- turn: context?.turn,
2291
- clientData: context?.clientData,
2292
- source: "outer",
2293
- });
2294
- if (!shouldTrigger)
2295
- return false;
2296
- const summary = await this._compaction.summarize({
2297
- messages: this.modelMessages,
2298
- usage,
2299
- totalUsage: context?.totalUsage,
2300
- chatId: context?.chatId,
2301
- turn: context?.turn,
2302
- clientData: context?.clientData,
2303
- source: "outer",
2304
- });
2305
- const compactEvent = {
2306
- summary,
2307
- uiMessages: this.uiMessages,
2308
- modelMessages: this.modelMessages,
2309
- chatId: context?.chatId ?? "",
2310
- turn: context?.turn ?? 0,
2311
- clientData: context?.clientData,
2312
- source: "outer",
2313
- };
2314
- this.modelMessages = this._compaction.compactModelMessages
2315
- ? await this._compaction.compactModelMessages(compactEvent)
2316
- : [
2317
- {
2318
- role: "assistant",
2319
- content: [{ type: "text", text: `[Conversation summary]\n\n${summary}` }],
2320
- },
2321
- ];
2322
- if (this._compaction.compactUIMessages) {
2323
- this.uiMessages = await this._compaction.compactUIMessages(compactEvent);
2324
- }
2325
- return true;
2326
- }
2327
- }
2328
- /**
2329
- * Create a chat session that yields turns as an async iterator.
2330
- *
2331
- * Handles: preload wait, stop signals, message accumulation, turn-complete
2332
- * signaling, and idle/suspend between turns. You control: initialization,
2333
- * model/tool selection, persistence, and any custom per-turn logic.
2334
- *
2335
- * @example
2336
- * ```ts
2337
- * import { task } from "@trigger.dev/sdk";
2338
- * import { chat, type ChatTaskWirePayload } from "@trigger.dev/sdk/ai";
2339
- * import { streamText } from "ai";
2340
- * import { openai } from "@ai-sdk/openai";
2341
- *
2342
- * export const myChat = task({
2343
- * id: "my-chat",
2344
- * run: async (payload: ChatTaskWirePayload, { signal }) => {
2345
- * const session = chat.createSession(payload, { signal });
2346
- *
2347
- * for await (const turn of session) {
2348
- * const result = streamText({
2349
- * model: openai("gpt-4o"),
2350
- * messages: turn.messages,
2351
- * abortSignal: turn.signal,
2352
- * });
2353
- * await turn.complete(result);
2354
- * }
2355
- * },
2356
- * });
2357
- * ```
2358
- */
2359
- function createChatSession(payload, options) {
2360
- const { signal: runSignal, idleTimeoutInSeconds = 30, timeout = "1h", maxTurns = 100, compaction: sessionCompaction, pendingMessages: sessionPendingMessages, } = options;
2361
- return {
2362
- [Symbol.asyncIterator]() {
2363
- let currentPayload = payload;
2364
- let turn = -1;
2365
- const stop = createStopSignal();
2366
- const accumulator = new ChatMessageAccumulator();
2367
- let previousTurnUsage;
2368
- let cumulativeUsage = emptyUsage();
2369
- return {
2370
- async next() {
2371
- turn++;
2372
- // First turn: handle preload — wait for the first real message
2373
- if (turn === 0 && currentPayload.trigger === "preload") {
2374
- const result = await messagesInput.waitWithIdleTimeout({
2375
- idleTimeoutInSeconds: currentPayload.idleTimeoutInSeconds ?? idleTimeoutInSeconds,
2376
- timeout,
2377
- spanName: "waiting for first message",
2378
- });
2379
- if (!result.ok || runSignal.aborted) {
2380
- stop.cleanup();
2381
- return { done: true, value: undefined };
2382
- }
2383
- currentPayload = result.output;
2384
- }
2385
- // Subsequent turns: wait for the next message
2386
- if (turn > 0) {
2387
- const next = await messagesInput.waitWithIdleTimeout({
2388
- idleTimeoutInSeconds,
2389
- timeout,
2390
- spanName: "waiting for next message",
2391
- });
2392
- if (!next.ok || runSignal.aborted) {
2393
- stop.cleanup();
2394
- return { done: true, value: undefined };
2395
- }
2396
- currentPayload = next.output;
2397
- }
2398
- // Check limits
2399
- if (turn >= maxTurns || runSignal.aborted) {
2400
- stop.cleanup();
2401
- return { done: true, value: undefined };
2402
- }
2403
- // Reset stop signal for this turn
2404
- stop.reset();
2405
- // Set up steering queue and pending messages config in locals
2406
- // so toStreamTextOptions() auto-injects prepareStep for steering
2407
- const turnSteeringQueue = [];
2408
- locals_js_1.locals.set(chatSteeringQueueKey, turnSteeringQueue);
2409
- if (sessionPendingMessages) {
2410
- locals_js_1.locals.set(chatPendingMessagesKey, sessionPendingMessages);
2411
- }
2412
- locals_js_1.locals.set(chatTurnContextKey, {
2413
- chatId: currentPayload.chatId,
2414
- turn,
2415
- continuation: currentPayload.continuation ?? false,
2416
- clientData: currentPayload.metadata,
2417
- });
2418
- // Listen for messages during streaming (steering + next-turn buffer)
2419
- const sessionPendingWire = [];
2420
- const sessionMsgSub = messagesInput.on(async (msg) => {
2421
- sessionPendingWire.push(msg);
2422
- if (sessionPendingMessages) {
2423
- const lastUIMessage = msg.messages?.[msg.messages.length - 1];
2424
- if (lastUIMessage) {
2425
- if (sessionPendingMessages.onReceived) {
2426
- try {
2427
- await sessionPendingMessages.onReceived({
2428
- message: lastUIMessage,
2429
- chatId: currentPayload.chatId,
2430
- turn,
2431
- });
2432
- }
2433
- catch {
2434
- /* non-fatal */
2435
- }
2436
- }
2437
- try {
2438
- const modelMsgs = await toModelMessages([lastUIMessage]);
2439
- turnSteeringQueue.push({ uiMessage: lastUIMessage, modelMessages: modelMsgs });
2440
- }
2441
- catch {
2442
- /* non-fatal */
2443
- }
2444
- }
2445
- }
2446
- });
2447
- // Accumulate messages
2448
- const messages = await accumulator.addIncoming(currentPayload.messages, currentPayload.trigger, turn);
2449
- const combinedSignal = AbortSignal.any([runSignal, stop.signal]);
2450
- const turnObj = {
2451
- number: turn,
2452
- chatId: currentPayload.chatId,
2453
- trigger: currentPayload.trigger,
2454
- clientData: currentPayload.metadata,
2455
- get messages() {
2456
- return accumulator.modelMessages;
2457
- },
2458
- get uiMessages() {
2459
- return accumulator.uiMessages;
2460
- },
2461
- signal: combinedSignal,
2462
- get stopped() {
2463
- return stop.signal.aborted && !runSignal.aborted;
2464
- },
2465
- continuation: currentPayload.continuation ?? false,
2466
- previousTurnUsage,
2467
- totalUsage: cumulativeUsage,
2468
- async setMessages(uiMessages) {
2469
- await accumulator.setMessages(uiMessages);
2470
- },
2471
- async complete(source) {
2472
- let response;
2473
- try {
2474
- response = await pipeChatAndCapture(source, { signal: combinedSignal });
2475
- }
2476
- catch (error) {
2477
- if (error instanceof Error && error.name === "AbortError") {
2478
- if (runSignal.aborted) {
2479
- // Full cancel — don't accumulate
2480
- sessionMsgSub.off();
2481
- await chatWriteTurnComplete();
2482
- return undefined;
2483
- }
2484
- // Stop — fall through to accumulate partial response
2485
- }
2486
- else {
2487
- throw error;
2488
- }
2489
- }
2490
- if (response) {
2491
- const cleaned = stop.signal.aborted && !runSignal.aborted
2492
- ? cleanupAbortedParts(response)
2493
- : response;
2494
- await accumulator.addResponse(cleaned);
2495
- }
2496
- // Capture token usage from the streamText result
2497
- let turnUsage;
2498
- if (typeof source.totalUsage?.then === "function") {
2499
- try {
2500
- const usage = await source.totalUsage;
2501
- turnUsage = usage;
2502
- previousTurnUsage = usage;
2503
- cumulativeUsage = addUsage(cumulativeUsage, usage);
2504
- }
2505
- catch {
2506
- /* non-fatal */
2507
- }
2508
- }
2509
- // Outer-loop compaction (same logic as chat.task)
2510
- if (sessionCompaction && turnUsage && !turnObj.stopped) {
2511
- const shouldTrigger = await sessionCompaction.shouldCompact({
2512
- messages: accumulator.modelMessages,
2513
- totalTokens: turnUsage.totalTokens,
2514
- inputTokens: turnUsage.inputTokens,
2515
- outputTokens: turnUsage.outputTokens,
2516
- usage: turnUsage,
2517
- totalUsage: cumulativeUsage,
2518
- chatId: currentPayload.chatId,
2519
- turn,
2520
- clientData: currentPayload.metadata,
2521
- source: "outer",
2522
- });
2523
- if (shouldTrigger) {
2524
- const summary = await sessionCompaction.summarize({
2525
- messages: accumulator.modelMessages,
2526
- usage: turnUsage,
2527
- totalUsage: cumulativeUsage,
2528
- chatId: currentPayload.chatId,
2529
- turn,
2530
- clientData: currentPayload.metadata,
2531
- source: "outer",
2532
- });
2533
- const compactEvent = {
2534
- summary,
2535
- uiMessages: accumulator.uiMessages,
2536
- modelMessages: accumulator.modelMessages,
2537
- chatId: currentPayload.chatId,
2538
- turn,
2539
- clientData: currentPayload.metadata,
2540
- source: "outer",
2541
- };
2542
- accumulator.modelMessages = sessionCompaction.compactModelMessages
2543
- ? await sessionCompaction.compactModelMessages(compactEvent)
2544
- : [
2545
- {
2546
- role: "assistant",
2547
- content: [
2548
- { type: "text", text: `[Conversation summary]\n\n${summary}` },
2549
- ],
2550
- },
2551
- ];
2552
- if (sessionCompaction.compactUIMessages) {
2553
- accumulator.uiMessages = await sessionCompaction.compactUIMessages(compactEvent);
2554
- }
2555
- }
2556
- }
2557
- sessionMsgSub.off();
2558
- await chatWriteTurnComplete();
2559
- return response;
2560
- },
2561
- async addResponse(response) {
2562
- await accumulator.addResponse(response);
2563
- },
2564
- async done() {
2565
- sessionMsgSub.off();
2566
- await chatWriteTurnComplete();
2567
- },
2568
- prepareStep() {
2569
- const hasCompaction = !!sessionCompaction;
2570
- const hasPending = !!sessionPendingMessages;
2571
- if (!hasCompaction && !hasPending)
2572
- return undefined;
2573
- return async ({ messages: stepMsgs, steps, }) => {
2574
- let resultMessages;
2575
- if (sessionCompaction) {
2576
- const compactResult = await chatCompact(stepMsgs, steps, {
2577
- shouldCompact: sessionCompaction.shouldCompact,
2578
- summarize: (msgs) => sessionCompaction.summarize({ messages: msgs, source: "inner" }),
2579
- });
2580
- if (compactResult.type !== "skipped") {
2581
- resultMessages = compactResult.messages;
2582
- }
2583
- }
2584
- if (sessionPendingMessages) {
2585
- const injected = await drainSteeringQueue(sessionPendingMessages, resultMessages ?? stepMsgs, steps, turnSteeringQueue);
2586
- if (injected.length > 0) {
2587
- resultMessages = [...(resultMessages ?? stepMsgs), ...injected];
2588
- }
2589
- }
2590
- return resultMessages ? { messages: resultMessages } : undefined;
2591
- };
2592
- },
2593
- };
2594
- return { done: false, value: turnObj };
2595
- },
2596
- async return() {
2597
- stop.cleanup();
2598
- return { done: true, value: undefined };
2599
- },
2600
- };
2601
- },
2602
- };
2603
- }
2604
- // ---------------------------------------------------------------------------
2605
- // chat.local — per-run typed data with Proxy access
2606
- // ---------------------------------------------------------------------------
2607
- /** @internal Symbol for storing the locals key on the proxy target. */
2608
- const CHAT_LOCAL_KEY = Symbol("chatLocalKey");
2609
- /** @internal Symbol for storing the dirty-tracking locals key. */
2610
- const CHAT_LOCAL_DIRTY_KEY = Symbol("chatLocalDirtyKey");
2611
- const chatLocalRegistry = new Set();
2612
- /** @internal Run-scoped flag to ensure hydration happens at most once per run. */
2613
- const chatLocalsHydratedKey = locals_js_1.locals.create("chat.locals.hydrated");
2614
- /**
2615
- * Hydrate chat.local values from subtask metadata (set by `ai.toolExecute()` or legacy `ai.tool()`).
2616
- * Runs once per run — subsequent calls are no-ops.
2617
- * @internal
2618
- */
2619
- function hydrateLocalsFromMetadata() {
2620
- if (locals_js_1.locals.get(chatLocalsHydratedKey))
2621
- return;
2622
- locals_js_1.locals.set(chatLocalsHydratedKey, true);
2623
- const opts = metadata_js_1.metadata.get(METADATA_KEY);
2624
- if (!opts?.chatLocals)
2625
- return;
2626
- for (const [id, value] of Object.entries(opts.chatLocals)) {
2627
- locals_js_1.locals.set(locals_js_1.locals.create(id), value);
2628
- }
2629
- }
2630
- /**
2631
- * Creates a per-run typed data object accessible from anywhere during task execution.
2632
- *
2633
- * Declare at module level, then initialize inside a lifecycle hook (e.g. `onChatStart`)
2634
- * using `chat.initLocal()`. Properties are accessible directly via the Proxy.
2635
- *
2636
- * Multiple locals can coexist — each gets its own isolated run-scoped storage.
2637
- *
2638
- * The `id` is required and must be unique across all `chat.local()` calls in
2639
- * your project. It's used to serialize values into subtask metadata so that
2640
- * `ai.toolExecute()` (or legacy `ai.tool()`) subtasks can auto-hydrate parent locals (read-only).
2641
- *
2642
- * @example
2643
- * ```ts
2644
- * import { chat } from "@trigger.dev/sdk/ai";
2645
- *
2646
- * const userPrefs = chat.local<{ theme: string; language: string }>({ id: "userPrefs" });
2647
- * const gameState = chat.local<{ score: number; streak: number }>({ id: "gameState" });
2648
- *
2649
- * export const myChat = chat.task({
2650
- * id: "my-chat",
2651
- * onChatStart: async ({ clientData }) => {
2652
- * const prefs = await db.prefs.findUnique({ where: { userId: clientData.userId } });
2653
- * userPrefs.init(prefs ?? { theme: "dark", language: "en" });
2654
- * gameState.init({ score: 0, streak: 0 });
2655
- * },
2656
- * onTurnComplete: async ({ chatId }) => {
2657
- * if (gameState.hasChanged()) {
2658
- * await db.save({ where: { chatId }, data: gameState.get() });
2659
- * }
2660
- * },
2661
- * run: async ({ messages }) => {
2662
- * gameState.score++;
2663
- * return streamText({
2664
- * system: `User prefers ${userPrefs.theme} theme. Score: ${gameState.score}`,
2665
- * messages,
2666
- * });
2667
- * },
2668
- * });
2669
- * ```
2670
- */
2671
- function chatLocal(options) {
2672
- const id = `chat.local.${options.id}`;
2673
- const localKey = locals_js_1.locals.create(id);
2674
- const dirtyKey = locals_js_1.locals.create(`${id}.dirty`);
2675
- chatLocalRegistry.add({ key: localKey, id });
2676
- const target = {};
2677
- target[CHAT_LOCAL_KEY] = localKey;
2678
- target[CHAT_LOCAL_DIRTY_KEY] = dirtyKey;
2679
- return new Proxy(target, {
2680
- get(_target, prop, _receiver) {
2681
- // Internal Symbol properties
2682
- if (prop === CHAT_LOCAL_KEY)
2683
- return _target[CHAT_LOCAL_KEY];
2684
- if (prop === CHAT_LOCAL_DIRTY_KEY)
2685
- return _target[CHAT_LOCAL_DIRTY_KEY];
2686
- // Instance methods
2687
- if (prop === "init") {
2688
- return (value) => {
2689
- locals_js_1.locals.set(localKey, value);
2690
- locals_js_1.locals.set(dirtyKey, false);
2691
- };
2692
- }
2693
- if (prop === "hasChanged") {
2694
- return () => {
2695
- const dirty = locals_js_1.locals.get(dirtyKey) ?? false;
2696
- locals_js_1.locals.set(dirtyKey, false);
2697
- return dirty;
2698
- };
2699
- }
2700
- if (prop === "get") {
2701
- return () => {
2702
- let current = locals_js_1.locals.get(localKey);
2703
- if (current === undefined) {
2704
- hydrateLocalsFromMetadata();
2705
- current = locals_js_1.locals.get(localKey);
2706
- }
2707
- if (current === undefined) {
2708
- throw new Error("local.get() called before initialization. Call local.init() first.");
2709
- }
2710
- return { ...current };
2711
- };
2712
- }
2713
- // toJSON for serialization (JSON.stringify(local))
2714
- if (prop === "toJSON") {
2715
- return () => {
2716
- let current = locals_js_1.locals.get(localKey);
2717
- if (current === undefined) {
2718
- hydrateLocalsFromMetadata();
2719
- current = locals_js_1.locals.get(localKey);
2720
- }
2721
- return current ? { ...current } : undefined;
2722
- };
2723
- }
2724
- let current = locals_js_1.locals.get(localKey);
2725
- if (current === undefined) {
2726
- // Auto-hydrate from parent metadata in subtask context
2727
- hydrateLocalsFromMetadata();
2728
- current = locals_js_1.locals.get(localKey);
2729
- }
2730
- if (current === undefined)
2731
- return undefined;
2732
- return current[prop];
2733
- },
2734
- set(_target, prop, value) {
2735
- // Don't allow setting internal Symbols
2736
- if (typeof prop === "symbol")
2737
- return false;
2738
- const current = locals_js_1.locals.get(localKey);
2739
- if (current === undefined) {
2740
- throw new Error("chat.local can only be modified after initialization. " +
2741
- "Call local.init() in onChatStart or run() first.");
2742
- }
2743
- locals_js_1.locals.set(localKey, { ...current, [prop]: value });
2744
- locals_js_1.locals.set(dirtyKey, true);
2745
- return true;
2746
- },
2747
- has(_target, prop) {
2748
- if (typeof prop === "symbol")
2749
- return prop in _target;
2750
- let current = locals_js_1.locals.get(localKey);
2751
- if (current === undefined) {
2752
- hydrateLocalsFromMetadata();
2753
- current = locals_js_1.locals.get(localKey);
2754
- }
2755
- return current !== undefined && prop in current;
2756
- },
2757
- ownKeys() {
2758
- let current = locals_js_1.locals.get(localKey);
2759
- if (current === undefined) {
2760
- hydrateLocalsFromMetadata();
2761
- current = locals_js_1.locals.get(localKey);
2762
- }
2763
- return current ? Reflect.ownKeys(current) : [];
2764
- },
2765
- getOwnPropertyDescriptor(_target, prop) {
2766
- if (typeof prop === "symbol")
2767
- return undefined;
2768
- let current = locals_js_1.locals.get(localKey);
2769
- if (current === undefined) {
2770
- hydrateLocalsFromMetadata();
2771
- current = locals_js_1.locals.get(localKey);
2772
- }
2773
- if (current === undefined || !(prop in current))
2774
- return undefined;
2775
- return {
2776
- configurable: true,
2777
- enumerable: true,
2778
- writable: true,
2779
- value: current[prop],
2780
- };
2781
- },
2782
- });
2783
- }
2784
- exports.chat = {
2785
- /** Create a chat task. See {@link chatTask}. */
2786
- task: chatTask,
2787
- /** Create a chat task with a fixed {@link UIMessage} subtype and optional default stream options. See {@link withUIMessage}. */
2788
- withUIMessage,
2789
- /** Pipe a stream to the chat transport. See {@link pipeChat}. */
2790
- pipe: pipeChat,
2791
- /** Create a per-run typed local. See {@link chatLocal}. */
2792
- local: chatLocal,
2793
- /** Create a public access token for a chat task. See {@link createChatAccessToken}. */
2794
- createAccessToken: createChatAccessToken,
2795
- /** Override the turn timeout at runtime (duration string). See {@link setTurnTimeout}. */
2796
- setTurnTimeout,
2797
- /** Override the turn timeout at runtime (seconds). See {@link setTurnTimeoutInSeconds}. */
2798
- setTurnTimeoutInSeconds,
2799
- /** Override the idle timeout at runtime. See {@link setIdleTimeoutInSeconds}. */
2800
- setIdleTimeoutInSeconds,
2801
- /** Override toUIMessageStream() options for the current turn. See {@link setUIMessageStreamOptions}. */
2802
- setUIMessageStreamOptions,
2803
- /** Check if the current turn was stopped by the user. See {@link isStopped}. */
2804
- isStopped,
2805
- /** Clean up aborted parts from a UIMessage. See {@link cleanupAbortedParts}. */
2806
- cleanupAbortedParts,
2807
- /** Register background work that runs in parallel with streaming. See {@link chatDefer}. */
2808
- defer: chatDefer,
2809
- /** Queue model messages for injection at the next `prepareStep` boundary. See {@link injectBackgroundContext}. */
2810
- inject: injectBackgroundContext,
2811
- /** Typed chat output stream for writing custom chunks or piping from subtasks. */
2812
- stream: chatStream,
2813
- /** Pre-built input stream for receiving messages from the transport. */
2814
- messages: messagesInput,
2815
- /** Create a managed stop signal wired to the stop input stream. See {@link createStopSignal}. */
2816
- createStopSignal,
2817
- /** Signal the frontend that the current turn is complete. See {@link chatWriteTurnComplete}. */
2818
- writeTurnComplete: chatWriteTurnComplete,
2819
- /** Pipe a stream and capture the response message. See {@link pipeChatAndCapture}. */
2820
- pipeAndCapture: pipeChatAndCapture,
2821
- /** Message accumulator class for raw task chat. See {@link ChatMessageAccumulator}. */
2822
- MessageAccumulator: ChatMessageAccumulator,
2823
- /** Create a chat session (async iterator). See {@link createChatSession}. */
2824
- createSession: createChatSession,
2825
- /**
2826
- * Store and retrieve a resolved prompt for the current run.
2827
- *
2828
- * - `chat.prompt.set(resolved)` — store a `ResolvedPrompt` or plain string
2829
- * - `chat.prompt()` — read the stored prompt (throws if not set)
2830
- */
2831
- prompt: Object.assign(getChatPrompt, { set: setChatPrompt }),
2832
- /**
2833
- * Returns an options object ready to spread into `streamText()`.
2834
- * Reads the stored prompt and returns `{ system, experimental_telemetry, ...config }`.
2835
- * Returns `{}` if no prompt has been set.
2836
- */
2837
- toStreamTextOptions,
2838
- /**
2839
- * Replace the accumulated conversation messages for compaction.
2840
- * Call from `onTurnStart` or `onTurnComplete`. Takes `UIMessage[]` and
2841
- * converts to `ModelMessage[]` internally.
2842
- */
2843
- setMessages: setChatMessages,
2844
- /** Check if it's safe to compact messages (no in-flight tool calls). */
2845
- isCompactionSafe,
2846
- /** Returns a `prepareStep` function that handles context compaction automatically. */
2847
- compactionStep: chatCompactionStep,
2848
- /** Low-level compaction for use inside a custom `prepareStep`. */
2849
- compact: chatCompact,
2850
- /** Read the current compaction state (summary + base message count). */
2851
- getCompactionState,
2852
55
  };
2853
- /**
2854
- * Writes a turn-complete control chunk to the chat output stream.
2855
- * The frontend transport intercepts this to close the ReadableStream for the current turn.
2856
- * @internal
2857
- */
2858
- async function writeTurnCompleteChunk(chatId, publicAccessToken) {
2859
- const { waitUntilComplete } = streams_js_1.streams.writer(exports.CHAT_STREAM_KEY, {
2860
- spanName: "turn complete",
2861
- collapsed: true,
2862
- execute: ({ write }) => {
2863
- write({
2864
- type: "__trigger_turn_complete",
2865
- ...(publicAccessToken ? { publicAccessToken } : {}),
2866
- });
2867
- },
2868
- });
2869
- return await waitUntilComplete();
2870
- }
2871
- /**
2872
- * Extracts the text content of the last user message from a UIMessage array.
2873
- * Returns undefined if no user message is found.
2874
- * @internal
2875
- */
2876
- function extractLastUserMessageText(messages) {
2877
- for (let i = messages.length - 1; i >= 0; i--) {
2878
- const msg = messages[i];
2879
- if (msg.role !== "user")
2880
- continue;
2881
- // UIMessage uses parts array
2882
- if (msg.parts) {
2883
- const textParts = msg.parts
2884
- .filter((p) => p.type === "text" && p.text)
2885
- .map((p) => p.text);
2886
- if (textParts.length > 0) {
2887
- return textParts.join("\n");
2888
- }
2889
- }
2890
- break;
2891
- }
2892
- return undefined;
2893
- }
2894
- /**
2895
- * Strips ephemeral OpenAI Responses API `itemId` from a UIMessage's parts.
2896
- *
2897
- * The OpenAI Responses provider attaches `itemId` to message parts via
2898
- * `providerMetadata.openai.itemId`. These IDs are ephemeral — sending them
2899
- * back in a subsequent `streamText` call causes 404s because the provider
2900
- * can't find the referenced item (especially for stopped/partial responses).
2901
- *
2902
- * @internal
2903
- */
2904
- function stripProviderMetadata(message) {
2905
- if (!message.parts)
2906
- return message;
2907
- return {
2908
- ...message,
2909
- parts: message.parts.map((part) => {
2910
- const openai = part.providerMetadata?.openai;
2911
- if (!openai?.itemId)
2912
- return part;
2913
- const { itemId, ...restOpenai } = openai;
2914
- const { openai: _, ...restProviders } = part.providerMetadata;
2915
- return {
2916
- ...part,
2917
- providerMetadata: {
2918
- ...restProviders,
2919
- ...(Object.keys(restOpenai).length > 0 ? { openai: restOpenai } : {}),
2920
- },
2921
- };
2922
- }),
2923
- };
2924
- }
2925
56
  //# sourceMappingURL=ai.js.map