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