@trigger.dev/sdk 0.0.0-prerelease-20260310150337 → 0.0.0-prerelease-20260324161542
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.
- package/dist/commonjs/v3/ai.d.ts +571 -27
- package/dist/commonjs/v3/ai.js +865 -56
- package/dist/commonjs/v3/ai.js.map +1 -1
- package/dist/commonjs/v3/chat.d.ts +1 -1
- package/dist/commonjs/v3/chat.js +2 -2
- package/dist/commonjs/v3/index.d.ts +1 -0
- package/dist/commonjs/v3/index.js +2 -1
- package/dist/commonjs/v3/index.js.map +1 -1
- package/dist/commonjs/v3/prompt.d.ts +38 -0
- package/dist/commonjs/v3/prompt.js +150 -0
- package/dist/commonjs/v3/prompt.js.map +1 -0
- package/dist/commonjs/v3/promptManagement.d.ts +26 -0
- package/dist/commonjs/v3/promptManagement.js +180 -0
- package/dist/commonjs/v3/promptManagement.js.map +1 -0
- package/dist/commonjs/v3/prompts.d.ts +3 -0
- package/dist/commonjs/v3/prompts.js +15 -0
- package/dist/commonjs/v3/prompts.js.map +1 -0
- package/dist/commonjs/v3/streams.js +18 -8
- package/dist/commonjs/v3/streams.js.map +1 -1
- package/dist/commonjs/version.js +1 -1
- package/dist/esm/v3/ai.d.ts +571 -27
- package/dist/esm/v3/ai.js +865 -56
- package/dist/esm/v3/ai.js.map +1 -1
- package/dist/esm/v3/chat.d.ts +1 -1
- package/dist/esm/v3/chat.js +2 -2
- package/dist/esm/v3/index.d.ts +1 -0
- package/dist/esm/v3/index.js +1 -0
- package/dist/esm/v3/index.js.map +1 -1
- package/dist/esm/v3/prompt.d.ts +38 -0
- package/dist/esm/v3/prompt.js +147 -0
- package/dist/esm/v3/prompt.js.map +1 -0
- package/dist/esm/v3/promptManagement.d.ts +26 -0
- package/dist/esm/v3/promptManagement.js +170 -0
- package/dist/esm/v3/promptManagement.js.map +1 -0
- package/dist/esm/v3/prompts.d.ts +3 -0
- package/dist/esm/v3/prompts.js +3 -0
- package/dist/esm/v3/prompts.js.map +1 -0
- package/dist/esm/v3/streams.js +18 -8
- package/dist/esm/v3/streams.js.map +1 -1
- package/dist/esm/version.js +1 -1
- package/package.json +2 -2
package/dist/commonjs/v3/ai.d.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { AnyTask, Task, type inferSchemaIn, type inferSchemaOut, type TaskIdentifier, type TaskOptions, type TaskSchema, type TaskWithSchema } from "@trigger.dev/core/v3";
|
|
2
|
-
import type { ModelMessage, UIMessage, UIMessageChunk, UIMessageStreamOptions } from "ai";
|
|
2
|
+
import type { ModelMessage, UIMessage, UIMessageChunk, UIMessageStreamOptions, LanguageModelUsage } from "ai";
|
|
3
3
|
import { Tool } from "ai";
|
|
4
4
|
import { locals } from "./locals.js";
|
|
5
|
+
import type { ResolvedPrompt } from "./prompt.js";
|
|
5
6
|
import { CHAT_MESSAGES_STREAM_ID, CHAT_STOP_STREAM_ID } from "./chat-constants.js";
|
|
6
7
|
export type ToolCallExecutionOptions = {
|
|
7
8
|
toolCallId: string;
|
|
@@ -110,8 +111,8 @@ export type ChatTaskWirePayload<TMessage extends UIMessage = UIMessage, TMetadat
|
|
|
110
111
|
continuation?: boolean;
|
|
111
112
|
/** The run ID of the previous run (only set when `continuation` is true). */
|
|
112
113
|
previousRunId?: string;
|
|
113
|
-
/** Override
|
|
114
|
-
|
|
114
|
+
/** Override idle timeout for this run (seconds). Set by transport.preload(). */
|
|
115
|
+
idleTimeoutInSeconds?: number;
|
|
115
116
|
};
|
|
116
117
|
/**
|
|
117
118
|
* The payload shape passed to the `chatTask` run function.
|
|
@@ -161,7 +162,389 @@ export type ChatTaskSignals = {
|
|
|
161
162
|
* The full payload passed to a `chatTask` run function.
|
|
162
163
|
* Extends `ChatTaskPayload` (the wire payload) with abort signals.
|
|
163
164
|
*/
|
|
164
|
-
export type ChatTaskRunPayload<TClientData = unknown> = ChatTaskPayload<TClientData> & ChatTaskSignals
|
|
165
|
+
export type ChatTaskRunPayload<TClientData = unknown> = ChatTaskPayload<TClientData> & ChatTaskSignals & {
|
|
166
|
+
/** Token usage from the previous turn. Undefined on turn 0. */
|
|
167
|
+
previousTurnUsage?: LanguageModelUsage;
|
|
168
|
+
/** Cumulative token usage across all completed turns so far. */
|
|
169
|
+
totalUsage: LanguageModelUsage;
|
|
170
|
+
};
|
|
171
|
+
/** Convenience re-export of the AI SDK's `LanguageModelUsage` type. */
|
|
172
|
+
export type ChatTurnUsage = LanguageModelUsage;
|
|
173
|
+
/**
|
|
174
|
+
* Replace the accumulated conversation messages for the current run.
|
|
175
|
+
*
|
|
176
|
+
* Call from `onTurnStart` to compact before `run()` executes, or from
|
|
177
|
+
* `onTurnComplete` to compact before the next turn. Takes `UIMessage[]`
|
|
178
|
+
* and converts to `ModelMessage[]` internally.
|
|
179
|
+
*/
|
|
180
|
+
declare function setChatMessages(uiMessages: UIMessage[]): void;
|
|
181
|
+
/** State stored in locals during prepareStep compaction. */
|
|
182
|
+
interface CompactionState {
|
|
183
|
+
summary: string;
|
|
184
|
+
baseResponseMessageCount: number;
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Event passed to `summarize` callbacks.
|
|
188
|
+
*/
|
|
189
|
+
export type SummarizeEvent = {
|
|
190
|
+
/** The current model messages to summarize. */
|
|
191
|
+
messages: ModelMessage[];
|
|
192
|
+
/** Full usage object from the triggering step/turn. */
|
|
193
|
+
usage?: LanguageModelUsage;
|
|
194
|
+
/** Cumulative token usage across all completed turns. Present in chat.task contexts. */
|
|
195
|
+
totalUsage?: LanguageModelUsage;
|
|
196
|
+
/** The chat session ID (if running inside a chat.task). */
|
|
197
|
+
chatId?: string;
|
|
198
|
+
/** The current turn number (0-indexed, if inside a chat.task). */
|
|
199
|
+
turn?: number;
|
|
200
|
+
/** Custom data from the frontend (if inside a chat.task). */
|
|
201
|
+
clientData?: unknown;
|
|
202
|
+
/**
|
|
203
|
+
* Where compaction is running:
|
|
204
|
+
* - `"inner"` — between tool-call steps (prepareStep)
|
|
205
|
+
* - `"outer"` — between turns
|
|
206
|
+
*/
|
|
207
|
+
source?: "inner" | "outer";
|
|
208
|
+
/** The step number (0-indexed). Only present when `source` is `"inner"`. */
|
|
209
|
+
stepNumber?: number;
|
|
210
|
+
};
|
|
211
|
+
/**
|
|
212
|
+
* Event passed to `compactUIMessages` and `compactModelMessages` callbacks.
|
|
213
|
+
*/
|
|
214
|
+
export type CompactMessagesEvent = {
|
|
215
|
+
/** The generated summary text. */
|
|
216
|
+
summary: string;
|
|
217
|
+
/** The current UI messages (full conversation). */
|
|
218
|
+
uiMessages: UIMessage[];
|
|
219
|
+
/** The current model messages (full conversation). */
|
|
220
|
+
modelMessages: ModelMessage[];
|
|
221
|
+
/** The chat session ID. */
|
|
222
|
+
chatId: string;
|
|
223
|
+
/** The current turn number (0-indexed). */
|
|
224
|
+
turn: number;
|
|
225
|
+
/** Custom data from the frontend. */
|
|
226
|
+
clientData?: unknown;
|
|
227
|
+
/**
|
|
228
|
+
* Where compaction is running:
|
|
229
|
+
* - `"inner"` — between tool-call steps (prepareStep)
|
|
230
|
+
* - `"outer"` — between turns
|
|
231
|
+
*/
|
|
232
|
+
source: "inner" | "outer";
|
|
233
|
+
};
|
|
234
|
+
/**
|
|
235
|
+
* Options for the `compaction` field on `chat.task()`.
|
|
236
|
+
*
|
|
237
|
+
* Handles compaction automatically in both the inner loop (prepareStep, between
|
|
238
|
+
* tool-call steps) and the outer loop (between turns, for single-step responses
|
|
239
|
+
* where prepareStep never fires).
|
|
240
|
+
*/
|
|
241
|
+
export type ChatTaskCompactionOptions = {
|
|
242
|
+
/** Decide whether to compact. Return true to trigger compaction. */
|
|
243
|
+
shouldCompact: (event: ShouldCompactEvent) => boolean | Promise<boolean>;
|
|
244
|
+
/** Generate a summary from the current messages. Return the summary text. */
|
|
245
|
+
summarize: (event: SummarizeEvent) => Promise<string>;
|
|
246
|
+
/**
|
|
247
|
+
* Transform UI messages after compaction (what gets persisted and displayed).
|
|
248
|
+
* Default: preserve all UI messages unchanged.
|
|
249
|
+
*
|
|
250
|
+
* @example
|
|
251
|
+
* ```ts
|
|
252
|
+
* // Flatten to summary
|
|
253
|
+
* compactUIMessages: ({ summary }) => [{
|
|
254
|
+
* id: generateId(), role: "assistant",
|
|
255
|
+
* parts: [{ type: "text", text: `[Summary]\n\n${summary}` }],
|
|
256
|
+
* }],
|
|
257
|
+
*
|
|
258
|
+
* // Summary + keep last 4 messages
|
|
259
|
+
* compactUIMessages: ({ uiMessages, summary }) => [
|
|
260
|
+
* { id: generateId(), role: "assistant",
|
|
261
|
+
* parts: [{ type: "text", text: `[Summary]\n\n${summary}` }] },
|
|
262
|
+
* ...uiMessages.slice(-4),
|
|
263
|
+
* ],
|
|
264
|
+
* ```
|
|
265
|
+
*/
|
|
266
|
+
compactUIMessages?: (event: CompactMessagesEvent) => UIMessage[] | Promise<UIMessage[]>;
|
|
267
|
+
/**
|
|
268
|
+
* Transform model messages after compaction (what gets sent to the LLM).
|
|
269
|
+
* Default: replace all with a single summary message.
|
|
270
|
+
*
|
|
271
|
+
* @example
|
|
272
|
+
* ```ts
|
|
273
|
+
* // Summary + keep last 2 model messages
|
|
274
|
+
* compactModelMessages: ({ modelMessages, summary }) => [
|
|
275
|
+
* { role: "user", content: summary },
|
|
276
|
+
* ...modelMessages.slice(-2),
|
|
277
|
+
* ],
|
|
278
|
+
* ```
|
|
279
|
+
*/
|
|
280
|
+
compactModelMessages?: (event: CompactMessagesEvent) => ModelMessage[] | Promise<ModelMessage[]>;
|
|
281
|
+
};
|
|
282
|
+
/**
|
|
283
|
+
* Event passed to the `prepareMessages` hook.
|
|
284
|
+
*/
|
|
285
|
+
export type PrepareMessagesEvent<TClientData = unknown> = {
|
|
286
|
+
/** The messages to transform. Return the transformed array. */
|
|
287
|
+
messages: ModelMessage[];
|
|
288
|
+
/** Why messages are being prepared. */
|
|
289
|
+
reason: "run" | "compaction-rebuild" | "compaction-result";
|
|
290
|
+
/** The chat session ID. */
|
|
291
|
+
chatId: string;
|
|
292
|
+
/** The current turn number (0-indexed). */
|
|
293
|
+
turn: number;
|
|
294
|
+
/** Custom data from the frontend. */
|
|
295
|
+
clientData?: TClientData;
|
|
296
|
+
};
|
|
297
|
+
/**
|
|
298
|
+
* Data shape for `data-compaction` stream chunks emitted during compaction.
|
|
299
|
+
* Use to type the `data` field when rendering compaction parts in the frontend.
|
|
300
|
+
*/
|
|
301
|
+
export type CompactionChunkData = {
|
|
302
|
+
status: "compacting" | "complete";
|
|
303
|
+
totalTokens: number | undefined;
|
|
304
|
+
};
|
|
305
|
+
/**
|
|
306
|
+
* Event passed to the `onCompacted` callback.
|
|
307
|
+
*/
|
|
308
|
+
export type CompactedEvent = {
|
|
309
|
+
/** The generated summary text. */
|
|
310
|
+
summary: string;
|
|
311
|
+
/** The messages that were compacted (pre-compaction). */
|
|
312
|
+
messages: ModelMessage[];
|
|
313
|
+
/** Number of messages before compaction. */
|
|
314
|
+
messageCount: number;
|
|
315
|
+
/** Token usage from the step that triggered compaction. */
|
|
316
|
+
usage: LanguageModelUsage;
|
|
317
|
+
/** Total token count that triggered compaction. */
|
|
318
|
+
totalTokens: number | undefined;
|
|
319
|
+
/** Input token count from the triggering step. */
|
|
320
|
+
inputTokens: number | undefined;
|
|
321
|
+
/** Output token count from the triggering step. */
|
|
322
|
+
outputTokens: number | undefined;
|
|
323
|
+
/** The step number where compaction occurred (0-indexed). */
|
|
324
|
+
stepNumber: number;
|
|
325
|
+
/** The chat session ID (if running inside a chat.task). */
|
|
326
|
+
chatId?: string;
|
|
327
|
+
/** The current turn number (if running inside a chat.task). */
|
|
328
|
+
turn?: number;
|
|
329
|
+
};
|
|
330
|
+
/**
|
|
331
|
+
* Event passed to `shouldCompact` callbacks.
|
|
332
|
+
*/
|
|
333
|
+
export type ShouldCompactEvent = {
|
|
334
|
+
/** The current model messages (full conversation). */
|
|
335
|
+
messages: ModelMessage[];
|
|
336
|
+
/** Total token count from the triggering step/turn. */
|
|
337
|
+
totalTokens: number | undefined;
|
|
338
|
+
/** Input token count from the triggering step/turn. */
|
|
339
|
+
inputTokens: number | undefined;
|
|
340
|
+
/** Output token count from the triggering step/turn. */
|
|
341
|
+
outputTokens: number | undefined;
|
|
342
|
+
/** Full usage object from the triggering step/turn. */
|
|
343
|
+
usage?: LanguageModelUsage;
|
|
344
|
+
/** Cumulative token usage across all completed turns. Present in chat.task contexts. */
|
|
345
|
+
totalUsage?: LanguageModelUsage;
|
|
346
|
+
/** The chat session ID (if running inside a chat.task). */
|
|
347
|
+
chatId?: string;
|
|
348
|
+
/** The current turn number (0-indexed, if inside a chat.task). */
|
|
349
|
+
turn?: number;
|
|
350
|
+
/** Custom data from the frontend (if inside a chat.task). */
|
|
351
|
+
clientData?: unknown;
|
|
352
|
+
/**
|
|
353
|
+
* Where this check is running:
|
|
354
|
+
* - `"inner"` — between tool-call steps (prepareStep)
|
|
355
|
+
* - `"outer"` — between turns (after response, before onBeforeTurnComplete)
|
|
356
|
+
*/
|
|
357
|
+
source?: "inner" | "outer";
|
|
358
|
+
/** The step number (0-indexed). Only present when `source` is `"inner"`. */
|
|
359
|
+
stepNumber?: number;
|
|
360
|
+
/** The steps array from prepareStep. Only present when `source` is `"inner"`. */
|
|
361
|
+
steps?: CompactionStep[];
|
|
362
|
+
};
|
|
363
|
+
/**
|
|
364
|
+
* Options for `chat.compaction()` — the high-level prepareStep factory.
|
|
365
|
+
*/
|
|
366
|
+
export type CompactionOptions = {
|
|
367
|
+
/** Generate a summary from the current messages. Return the summary text. */
|
|
368
|
+
summarize: (messages: ModelMessage[]) => Promise<string>;
|
|
369
|
+
/** Token threshold — compact when totalTokens exceeds this. Ignored if `shouldCompact` is provided. */
|
|
370
|
+
threshold?: number;
|
|
371
|
+
/** Custom compaction trigger. When provided, used instead of `threshold`. */
|
|
372
|
+
shouldCompact?: (event: ShouldCompactEvent) => boolean | Promise<boolean>;
|
|
373
|
+
};
|
|
374
|
+
/** A step object as received in prepareStep's `steps` array. */
|
|
375
|
+
export type CompactionStep = {
|
|
376
|
+
usage: LanguageModelUsage;
|
|
377
|
+
finishReason: string;
|
|
378
|
+
content: Array<{
|
|
379
|
+
type: string;
|
|
380
|
+
toolCallId?: string;
|
|
381
|
+
}>;
|
|
382
|
+
response: {
|
|
383
|
+
messages: Array<any>;
|
|
384
|
+
};
|
|
385
|
+
};
|
|
386
|
+
/**
|
|
387
|
+
* Result of `chat.compact()`. Discriminated union so you can inspect
|
|
388
|
+
* what happened, but also directly compatible with prepareStep's return type.
|
|
389
|
+
*
|
|
390
|
+
* - `"skipped"` — no compaction needed (first step, boundary unsafe, or under threshold). Return `undefined` to prepareStep.
|
|
391
|
+
* - `"rebuilt"` — previous compaction exists, messages rebuilt from summary + new response messages.
|
|
392
|
+
* - `"compacted"` — compaction just happened, includes the generated summary.
|
|
393
|
+
*/
|
|
394
|
+
export type CompactResult = {
|
|
395
|
+
type: "skipped";
|
|
396
|
+
} | {
|
|
397
|
+
type: "rebuilt";
|
|
398
|
+
messages: ModelMessage[];
|
|
399
|
+
} | {
|
|
400
|
+
type: "compacted";
|
|
401
|
+
messages: ModelMessage[];
|
|
402
|
+
summary: string;
|
|
403
|
+
};
|
|
404
|
+
/**
|
|
405
|
+
* Options for `chat.compact()` — the low-level compaction function.
|
|
406
|
+
*/
|
|
407
|
+
export type CompactOptions = {
|
|
408
|
+
/** Generate a summary from the current messages. Return the summary text. */
|
|
409
|
+
summarize: (messages: ModelMessage[]) => Promise<string>;
|
|
410
|
+
/** Token threshold — compact when totalTokens exceeds this. Ignored if `shouldCompact` is provided. */
|
|
411
|
+
threshold?: number;
|
|
412
|
+
/** Custom compaction trigger. When provided, used instead of `threshold`. */
|
|
413
|
+
shouldCompact?: (event: ShouldCompactEvent) => boolean | Promise<boolean>;
|
|
414
|
+
};
|
|
415
|
+
/**
|
|
416
|
+
* Read the current compaction state. Returns the summary and base message count
|
|
417
|
+
* if compaction has occurred in this turn, or `undefined` if not.
|
|
418
|
+
*
|
|
419
|
+
* Use in a custom `prepareStep` to rebuild from a previous compaction:
|
|
420
|
+
* ```ts
|
|
421
|
+
* const state = chat.getCompactionState();
|
|
422
|
+
* if (state) {
|
|
423
|
+
* return { messages: [{ role: "user", content: state.summary }, ...newMsgs] };
|
|
424
|
+
* }
|
|
425
|
+
* ```
|
|
426
|
+
*/
|
|
427
|
+
declare function getCompactionState(): CompactionState | undefined;
|
|
428
|
+
/**
|
|
429
|
+
* Low-level compaction for use inside a custom `prepareStep`.
|
|
430
|
+
*
|
|
431
|
+
* Handles the full decision tree: first step, already-compacted rebuild,
|
|
432
|
+
* boundary safety, threshold check, summarization, stream chunks, state
|
|
433
|
+
* storage, and accumulator update.
|
|
434
|
+
*
|
|
435
|
+
* Returns a `CompactResult` — inspect `result.type` to see what happened,
|
|
436
|
+
* or convert to a prepareStep return with `result.type === "skipped" ? undefined : result`.
|
|
437
|
+
*
|
|
438
|
+
* @example
|
|
439
|
+
* ```ts
|
|
440
|
+
* prepareStep: async ({ messages, steps }) => {
|
|
441
|
+
* // your custom logic here...
|
|
442
|
+
* const result = await chat.compact(messages, steps, {
|
|
443
|
+
* threshold: 80_000,
|
|
444
|
+
* summarize: async (msgs) => generateText({ model, messages: msgs }).then(r => r.text),
|
|
445
|
+
* });
|
|
446
|
+
* if (result.type === "compacted") {
|
|
447
|
+
* logger.info("Compacted!", { summary: result.summary });
|
|
448
|
+
* }
|
|
449
|
+
* return result.type === "skipped" ? undefined : result;
|
|
450
|
+
* },
|
|
451
|
+
* ```
|
|
452
|
+
*/
|
|
453
|
+
declare function chatCompact(messages: ModelMessage[], steps: CompactionStep[], options: CompactOptions): Promise<CompactResult>;
|
|
454
|
+
/**
|
|
455
|
+
* Returns a `prepareStep` function that handles context compaction automatically.
|
|
456
|
+
*
|
|
457
|
+
* Monitors token usage between tool-call steps. When `totalTokens` exceeds
|
|
458
|
+
* the threshold, generates a summary via `summarize()`, replaces the message
|
|
459
|
+
* history, and emits `data-compaction` stream chunks for the frontend.
|
|
460
|
+
*
|
|
461
|
+
* @example
|
|
462
|
+
* ```ts
|
|
463
|
+
* return streamText({
|
|
464
|
+
* ...chat.toStreamTextOptions({ registry }),
|
|
465
|
+
* messages: chat.addCacheBreaks(messages),
|
|
466
|
+
* prepareStep: chat.compactionStep({
|
|
467
|
+
* threshold: 80_000,
|
|
468
|
+
* summarize: async (messages) => {
|
|
469
|
+
* return generateText({ model, messages: [...messages, { role: "user", content: "Summarize." }] })
|
|
470
|
+
* .then((r) => r.text);
|
|
471
|
+
* },
|
|
472
|
+
* }),
|
|
473
|
+
* tools: { ... },
|
|
474
|
+
* });
|
|
475
|
+
* ```
|
|
476
|
+
*/
|
|
477
|
+
declare function chatCompactionStep(options: CompactionOptions): (args: {
|
|
478
|
+
messages: ModelMessage[];
|
|
479
|
+
steps: CompactionStep[];
|
|
480
|
+
}) => Promise<{
|
|
481
|
+
messages: ModelMessage[];
|
|
482
|
+
} | undefined>;
|
|
483
|
+
/**
|
|
484
|
+
* Checks whether it's safe to compact the message history. Returns `false`
|
|
485
|
+
* if any tool calls are in-flight (incomplete tool invocations without results).
|
|
486
|
+
*
|
|
487
|
+
* Call before `chat.setMessages()` to avoid corrupting tool-call state.
|
|
488
|
+
*/
|
|
489
|
+
declare function isCompactionSafe(messages: UIMessage[]): boolean;
|
|
490
|
+
/**
|
|
491
|
+
* A resolved prompt stored via `chat.prompt.set()`. Either a full `ResolvedPrompt`
|
|
492
|
+
* from `prompts.define().resolve()`, or a lightweight wrapper around a plain string.
|
|
493
|
+
*/
|
|
494
|
+
export type ChatPromptValue = ResolvedPrompt | {
|
|
495
|
+
text: string;
|
|
496
|
+
model: undefined;
|
|
497
|
+
config: undefined;
|
|
498
|
+
promptId: string;
|
|
499
|
+
version: number;
|
|
500
|
+
labels: string[];
|
|
501
|
+
toAISDKTelemetry: (additionalMetadata?: Record<string, string>) => {
|
|
502
|
+
experimental_telemetry: {
|
|
503
|
+
isEnabled: true;
|
|
504
|
+
metadata: Record<string, string>;
|
|
505
|
+
};
|
|
506
|
+
};
|
|
507
|
+
};
|
|
508
|
+
/**
|
|
509
|
+
* Store a resolved prompt (or plain string) for the current run.
|
|
510
|
+
* Call from any hook (`onPreload`, `onChatStart`, `onTurnStart`) or `run()`.
|
|
511
|
+
*/
|
|
512
|
+
declare function setChatPrompt(resolved: ResolvedPrompt | string): void;
|
|
513
|
+
/**
|
|
514
|
+
* Read the stored prompt. Throws if `chat.prompt.set()` has not been called.
|
|
515
|
+
*/
|
|
516
|
+
declare function getChatPrompt(): ChatPromptValue;
|
|
517
|
+
/**
|
|
518
|
+
* Options for {@link toStreamTextOptions}.
|
|
519
|
+
*/
|
|
520
|
+
export type ToStreamTextOptionsOptions = {
|
|
521
|
+
/** Additional telemetry metadata merged into `experimental_telemetry.metadata`. */
|
|
522
|
+
telemetry?: Record<string, string>;
|
|
523
|
+
/**
|
|
524
|
+
* An AI SDK provider registry (from `createProviderRegistry`) or any object
|
|
525
|
+
* with a `languageModel(id)` method. When provided and the stored prompt has
|
|
526
|
+
* a `model` string, the resolved `LanguageModel` is included in the returned
|
|
527
|
+
* options so `streamText` uses it directly.
|
|
528
|
+
*
|
|
529
|
+
* The model string should use the `"provider:model-id"` format
|
|
530
|
+
* (e.g. `"openai:gpt-4o"`, `"anthropic:claude-sonnet-4-6"`).
|
|
531
|
+
*/
|
|
532
|
+
registry?: {
|
|
533
|
+
languageModel(modelId: string): unknown;
|
|
534
|
+
};
|
|
535
|
+
};
|
|
536
|
+
/**
|
|
537
|
+
* Returns an options object ready to spread into `streamText()`.
|
|
538
|
+
*
|
|
539
|
+
* Includes `system`, `experimental_telemetry`, and any config fields
|
|
540
|
+
* (temperature, maxTokens, etc.) from the stored prompt.
|
|
541
|
+
*
|
|
542
|
+
* When a `registry` is provided and the prompt has a `model` string,
|
|
543
|
+
* the resolved `LanguageModel` is included as `model`.
|
|
544
|
+
*
|
|
545
|
+
* If no prompt has been set, returns `{}` (no-op spread).
|
|
546
|
+
*/
|
|
547
|
+
declare function toStreamTextOptions(options?: ToStreamTextOptionsOptions): Record<string, unknown>;
|
|
165
548
|
/**
|
|
166
549
|
* Options for `pipeChat`.
|
|
167
550
|
*/
|
|
@@ -312,6 +695,10 @@ export type TurnStartEvent<TClientData = unknown> = {
|
|
|
312
695
|
previousRunId?: string;
|
|
313
696
|
/** Whether this run was preloaded before the first message. */
|
|
314
697
|
preloaded: boolean;
|
|
698
|
+
/** Token usage from the previous turn. Undefined on turn 0. */
|
|
699
|
+
previousTurnUsage?: LanguageModelUsage;
|
|
700
|
+
/** Cumulative token usage across all completed turns so far. */
|
|
701
|
+
totalUsage: LanguageModelUsage;
|
|
315
702
|
};
|
|
316
703
|
/**
|
|
317
704
|
* Event passed to the `onTurnComplete` callback.
|
|
@@ -362,6 +749,10 @@ export type TurnCompleteEvent<TClientData = unknown> = {
|
|
|
362
749
|
previousRunId?: string;
|
|
363
750
|
/** Whether this run was preloaded before the first message. */
|
|
364
751
|
preloaded: boolean;
|
|
752
|
+
/** Token usage for this turn. Undefined if usage couldn't be captured (e.g. manual pipeChat). */
|
|
753
|
+
usage?: LanguageModelUsage;
|
|
754
|
+
/** Cumulative token usage across all turns in this run (including this turn). */
|
|
755
|
+
totalUsage: LanguageModelUsage;
|
|
365
756
|
};
|
|
366
757
|
export type ChatTaskOptions<TIdentifier extends string, TClientDataSchema extends TaskSchema | undefined = undefined> = Omit<TaskOptions<TIdentifier, ChatTaskWirePayload, unknown>, "run"> & {
|
|
367
758
|
/**
|
|
@@ -437,10 +828,68 @@ export type ChatTaskOptions<TIdentifier extends string, TClientDataSchema extend
|
|
|
437
828
|
*/
|
|
438
829
|
onTurnStart?: (event: TurnStartEvent<inferSchemaOut<TClientDataSchema>>) => Promise<void> | void;
|
|
439
830
|
/**
|
|
440
|
-
* Called after
|
|
441
|
-
*
|
|
831
|
+
* Called after the response is captured but before the stream closes.
|
|
832
|
+
* The stream is still open, so you can write custom chunks to the frontend
|
|
833
|
+
* (e.g. compaction progress). Use this for compaction, post-processing,
|
|
834
|
+
* or any work where the user should see real-time status updates.
|
|
442
835
|
*
|
|
443
|
-
*
|
|
836
|
+
* @example
|
|
837
|
+
* ```ts
|
|
838
|
+
* onBeforeTurnComplete: async ({ messages, uiMessages, usage }) => {
|
|
839
|
+
* if (usage?.inputTokens && usage.inputTokens > 5000) {
|
|
840
|
+
* await chat.stream.append({ type: "data-compaction", id: generateId(), data: { status: "compacting" } });
|
|
841
|
+
* // ... compact messages ...
|
|
842
|
+
* chat.setMessages(compactedMessages);
|
|
843
|
+
* await chat.stream.append({ type: "data-compaction", id: generateId(), data: { status: "complete" } });
|
|
844
|
+
* }
|
|
845
|
+
* }
|
|
846
|
+
* ```
|
|
847
|
+
*/
|
|
848
|
+
onBeforeTurnComplete?: (event: TurnCompleteEvent<inferSchemaOut<TClientDataSchema>>) => Promise<void> | void;
|
|
849
|
+
/**
|
|
850
|
+
* Called when conversation compaction occurs (via `chat.compact()` or
|
|
851
|
+
* `chat.compactionStep()`). Use for logging, billing, or persisting the summary.
|
|
852
|
+
*
|
|
853
|
+
* @example
|
|
854
|
+
* ```ts
|
|
855
|
+
* onCompacted: async ({ summary, totalTokens, chatId }) => {
|
|
856
|
+
* logger.info("Compacted", { totalTokens, chatId });
|
|
857
|
+
* await db.compactionLog.create({ data: { chatId, summary } });
|
|
858
|
+
* }
|
|
859
|
+
* ```
|
|
860
|
+
*/
|
|
861
|
+
onCompacted?: (event: CompactedEvent) => Promise<void> | void;
|
|
862
|
+
/**
|
|
863
|
+
* Automatic context compaction. When provided, compaction runs automatically
|
|
864
|
+
* in both the inner loop (prepareStep, between tool-call steps) and the
|
|
865
|
+
* outer loop (between turns, for single-step responses where prepareStep
|
|
866
|
+
* never fires).
|
|
867
|
+
*
|
|
868
|
+
* The `shouldCompact` callback decides when to compact, and `summarize`
|
|
869
|
+
* generates the summary. The prepareStep is auto-injected into
|
|
870
|
+
* `chat.toStreamTextOptions()` — if you provide your own `prepareStep`
|
|
871
|
+
* after spreading, it overrides the auto-injected one.
|
|
872
|
+
*
|
|
873
|
+
* @example
|
|
874
|
+
* ```ts
|
|
875
|
+
* chat.task({
|
|
876
|
+
* id: "my-chat",
|
|
877
|
+
* compaction: {
|
|
878
|
+
* shouldCompact: ({ totalTokens }) => (totalTokens ?? 0) > 80_000,
|
|
879
|
+
* summarize: async (messages) =>
|
|
880
|
+
* generateText({ model, messages: [...messages, { role: "user", content: "Summarize." }] })
|
|
881
|
+
* .then((r) => r.text),
|
|
882
|
+
* },
|
|
883
|
+
* run: async ({ messages, signal }) => {
|
|
884
|
+
* return streamText({ ...chat.toStreamTextOptions({ registry }), messages });
|
|
885
|
+
* },
|
|
886
|
+
* });
|
|
887
|
+
* ```
|
|
888
|
+
*/
|
|
889
|
+
compaction?: ChatTaskCompactionOptions;
|
|
890
|
+
/**
|
|
891
|
+
* Called after the stream closes for this turn. Use this to persist the
|
|
892
|
+
* conversation to your database after each assistant response.
|
|
444
893
|
*
|
|
445
894
|
* @example
|
|
446
895
|
* ```ts
|
|
@@ -466,16 +915,16 @@ export type ChatTaskOptions<TIdentifier extends string, TClientDataSchema extend
|
|
|
466
915
|
*/
|
|
467
916
|
turnTimeout?: string;
|
|
468
917
|
/**
|
|
469
|
-
* How long (in seconds)
|
|
470
|
-
*
|
|
471
|
-
*
|
|
472
|
-
*
|
|
918
|
+
* How long (in seconds) the run stays idle (active, using compute) after each
|
|
919
|
+
* turn, waiting for the next message. During this window responses are instant.
|
|
920
|
+
* After this timeout the run suspends (frees compute) and waits via
|
|
921
|
+
* `inputStream.wait()`.
|
|
473
922
|
*
|
|
474
923
|
* Set to `0` to suspend immediately after each turn.
|
|
475
924
|
*
|
|
476
925
|
* @default 30
|
|
477
926
|
*/
|
|
478
|
-
|
|
927
|
+
idleTimeoutInSeconds?: number;
|
|
479
928
|
/**
|
|
480
929
|
* How long the `chatAccessToken` (scoped to this run) remains valid.
|
|
481
930
|
* A fresh token is minted after each turn, so this only needs to cover
|
|
@@ -487,14 +936,14 @@ export type ChatTaskOptions<TIdentifier extends string, TClientDataSchema extend
|
|
|
487
936
|
*/
|
|
488
937
|
chatAccessTokenTTL?: string;
|
|
489
938
|
/**
|
|
490
|
-
* How long (in seconds)
|
|
939
|
+
* How long (in seconds) the run stays idle after `onPreload` fires,
|
|
491
940
|
* waiting for the first message before suspending.
|
|
492
941
|
*
|
|
493
942
|
* Only applies to preloaded runs (triggered via `transport.preload()`).
|
|
494
943
|
*
|
|
495
|
-
* @default Same as `
|
|
944
|
+
* @default Same as `idleTimeoutInSeconds`
|
|
496
945
|
*/
|
|
497
|
-
|
|
946
|
+
preloadIdleTimeoutInSeconds?: number;
|
|
498
947
|
/**
|
|
499
948
|
* How long to wait (suspended) for the first message after a preloaded run starts.
|
|
500
949
|
* If no message arrives within this time, the run ends.
|
|
@@ -504,6 +953,27 @@ export type ChatTaskOptions<TIdentifier extends string, TClientDataSchema extend
|
|
|
504
953
|
* @default Same as `turnTimeout`
|
|
505
954
|
*/
|
|
506
955
|
preloadTimeout?: string;
|
|
956
|
+
/**
|
|
957
|
+
* Transform model messages before they're used anywhere — in `run()`,
|
|
958
|
+
* in compaction rebuilds, and in compaction results.
|
|
959
|
+
*
|
|
960
|
+
* Define once, applied everywhere. Use for Anthropic cache breaks,
|
|
961
|
+
* injecting system context, stripping PII, etc.
|
|
962
|
+
*
|
|
963
|
+
* @example
|
|
964
|
+
* ```ts
|
|
965
|
+
* prepareMessages: async ({ messages, reason }) => {
|
|
966
|
+
* // Add Anthropic cache breaks to the last message
|
|
967
|
+
* if (messages.length === 0) return messages;
|
|
968
|
+
* const last = messages[messages.length - 1];
|
|
969
|
+
* return [...messages.slice(0, -1), {
|
|
970
|
+
* ...last,
|
|
971
|
+
* providerOptions: { ...last.providerOptions, anthropic: { cacheControl: { type: "ephemeral" } } },
|
|
972
|
+
* }];
|
|
973
|
+
* }
|
|
974
|
+
* ```
|
|
975
|
+
*/
|
|
976
|
+
prepareMessages?: (event: PrepareMessagesEvent<inferSchemaOut<TClientDataSchema>>) => ModelMessage[] | Promise<ModelMessage[]>;
|
|
507
977
|
/**
|
|
508
978
|
* Default options for `toUIMessageStream()` when auto-piping or using
|
|
509
979
|
* `turn.complete()` / `chat.pipeAndCapture()`.
|
|
@@ -596,23 +1066,23 @@ declare function setTurnTimeout(duration: string): void;
|
|
|
596
1066
|
*/
|
|
597
1067
|
declare function setTurnTimeoutInSeconds(seconds: number): void;
|
|
598
1068
|
/**
|
|
599
|
-
* Override the
|
|
1069
|
+
* Override the idle timeout for subsequent turns in the current run.
|
|
600
1070
|
*
|
|
601
|
-
* The
|
|
1071
|
+
* The idle timeout controls how long the run stays active (using compute)
|
|
602
1072
|
* after each turn, waiting for the next message. During this window,
|
|
603
1073
|
* responses are instant. After it expires, the run suspends.
|
|
604
1074
|
*
|
|
605
|
-
* @param seconds - Number of seconds to stay
|
|
1075
|
+
* @param seconds - Number of seconds to stay idle (0 to suspend immediately)
|
|
606
1076
|
*
|
|
607
1077
|
* @example
|
|
608
1078
|
* ```ts
|
|
609
1079
|
* run: async ({ messages, signal }) => {
|
|
610
|
-
* chat.
|
|
1080
|
+
* chat.setIdleTimeoutInSeconds(60);
|
|
611
1081
|
* return streamText({ model, messages, abortSignal: signal });
|
|
612
1082
|
* }
|
|
613
1083
|
* ```
|
|
614
1084
|
*/
|
|
615
|
-
declare function
|
|
1085
|
+
declare function setIdleTimeoutInSeconds(seconds: number): void;
|
|
616
1086
|
/**
|
|
617
1087
|
* Override the `toUIMessageStream()` options for the current turn.
|
|
618
1088
|
*
|
|
@@ -778,6 +1248,10 @@ declare function pipeChatAndCapture(source: UIMessageStreamable, options?: {
|
|
|
778
1248
|
declare class ChatMessageAccumulator {
|
|
779
1249
|
modelMessages: ModelMessage[];
|
|
780
1250
|
uiMessages: UIMessage[];
|
|
1251
|
+
private _compaction?;
|
|
1252
|
+
constructor(options?: {
|
|
1253
|
+
compaction?: ChatTaskCompactionOptions;
|
|
1254
|
+
});
|
|
781
1255
|
/**
|
|
782
1256
|
* Add incoming messages from the transport payload.
|
|
783
1257
|
* Returns the full accumulated model messages for `streamText`.
|
|
@@ -787,17 +1261,48 @@ declare class ChatMessageAccumulator {
|
|
|
787
1261
|
* Add the assistant's response to the accumulator.
|
|
788
1262
|
* Call after `pipeAndCapture` with the captured response.
|
|
789
1263
|
*/
|
|
1264
|
+
/**
|
|
1265
|
+
* Replace all accumulated messages (for compaction).
|
|
1266
|
+
* Converts UIMessages to ModelMessages internally.
|
|
1267
|
+
*/
|
|
1268
|
+
setMessages(uiMessages: UIMessage[]): Promise<void>;
|
|
790
1269
|
addResponse(response: UIMessage): Promise<void>;
|
|
1270
|
+
/**
|
|
1271
|
+
* Returns a `prepareStep` function for inner-loop compaction.
|
|
1272
|
+
* Only available when `compaction` was provided to the constructor.
|
|
1273
|
+
* Pass the result to `streamText({ prepareStep: conversation.prepareStep() })`.
|
|
1274
|
+
*/
|
|
1275
|
+
prepareStep(): ((args: {
|
|
1276
|
+
messages: ModelMessage[];
|
|
1277
|
+
steps: CompactionStep[];
|
|
1278
|
+
}) => Promise<{
|
|
1279
|
+
messages: ModelMessage[];
|
|
1280
|
+
} | undefined>) | undefined;
|
|
1281
|
+
/**
|
|
1282
|
+
* Run outer-loop compaction if needed. Call after adding the response
|
|
1283
|
+
* and capturing usage. Applies `compactModelMessages` and `compactUIMessages`
|
|
1284
|
+
* callbacks if configured.
|
|
1285
|
+
*
|
|
1286
|
+
* @returns `true` if compaction was performed, `false` otherwise.
|
|
1287
|
+
*/
|
|
1288
|
+
compactIfNeeded(usage: LanguageModelUsage | undefined, context?: {
|
|
1289
|
+
chatId?: string;
|
|
1290
|
+
turn?: number;
|
|
1291
|
+
clientData?: unknown;
|
|
1292
|
+
totalUsage?: LanguageModelUsage;
|
|
1293
|
+
}): Promise<boolean>;
|
|
791
1294
|
}
|
|
792
1295
|
export type ChatSessionOptions = {
|
|
793
1296
|
/** Run-level cancel signal (from task context). */
|
|
794
1297
|
signal: AbortSignal;
|
|
795
|
-
/** Seconds to stay
|
|
796
|
-
|
|
1298
|
+
/** Seconds to stay idle between turns before suspending. @default 30 */
|
|
1299
|
+
idleTimeoutInSeconds?: number;
|
|
797
1300
|
/** Duration string for suspend timeout. @default "1h" */
|
|
798
1301
|
timeout?: string;
|
|
799
1302
|
/** Max turns before ending. @default 100 */
|
|
800
1303
|
maxTurns?: number;
|
|
1304
|
+
/** Automatic context compaction — same options as `chat.task({ compaction })`. */
|
|
1305
|
+
compaction?: ChatTaskCompactionOptions;
|
|
801
1306
|
};
|
|
802
1307
|
export type ChatTurn = {
|
|
803
1308
|
/** Turn number (0-indexed). */
|
|
@@ -809,15 +1314,25 @@ export type ChatTurn = {
|
|
|
809
1314
|
/** Client data from the transport (`metadata` field on the wire payload). */
|
|
810
1315
|
clientData: unknown;
|
|
811
1316
|
/** Full accumulated model messages — pass directly to `streamText`. */
|
|
812
|
-
messages: ModelMessage[];
|
|
1317
|
+
readonly messages: ModelMessage[];
|
|
813
1318
|
/** Full accumulated UI messages — use for persistence. */
|
|
814
|
-
uiMessages: UIMessage[];
|
|
1319
|
+
readonly uiMessages: UIMessage[];
|
|
815
1320
|
/** Combined stop+cancel AbortSignal (fresh each turn). */
|
|
816
1321
|
signal: AbortSignal;
|
|
817
1322
|
/** Whether the user stopped generation this turn. */
|
|
818
1323
|
readonly stopped: boolean;
|
|
819
1324
|
/** Whether this is a continuation run. */
|
|
820
1325
|
continuation: boolean;
|
|
1326
|
+
/** Token usage from the previous turn. Undefined on turn 0. */
|
|
1327
|
+
previousTurnUsage?: LanguageModelUsage;
|
|
1328
|
+
/** Cumulative token usage across all completed turns so far. */
|
|
1329
|
+
totalUsage: LanguageModelUsage;
|
|
1330
|
+
/**
|
|
1331
|
+
* Replace accumulated messages (for compaction). Takes UIMessages and
|
|
1332
|
+
* converts to ModelMessages internally. After calling this, `turn.messages`
|
|
1333
|
+
* reflects the compacted history.
|
|
1334
|
+
*/
|
|
1335
|
+
setMessages(uiMessages: UIMessage[]): Promise<void>;
|
|
821
1336
|
/**
|
|
822
1337
|
* Easy path: pipe stream, capture response, accumulate it,
|
|
823
1338
|
* clean up aborted parts if stopped, and write turn-complete chunk.
|
|
@@ -838,7 +1353,7 @@ export type ChatTurn = {
|
|
|
838
1353
|
* Create a chat session that yields turns as an async iterator.
|
|
839
1354
|
*
|
|
840
1355
|
* Handles: preload wait, stop signals, message accumulation, turn-complete
|
|
841
|
-
* signaling, and
|
|
1356
|
+
* signaling, and idle/suspend between turns. You control: initialization,
|
|
842
1357
|
* model/tool selection, persistence, and any custom per-turn logic.
|
|
843
1358
|
*
|
|
844
1359
|
* @example
|
|
@@ -953,8 +1468,8 @@ export declare const chat: {
|
|
|
953
1468
|
setTurnTimeout: typeof setTurnTimeout;
|
|
954
1469
|
/** Override the turn timeout at runtime (seconds). See {@link setTurnTimeoutInSeconds}. */
|
|
955
1470
|
setTurnTimeoutInSeconds: typeof setTurnTimeoutInSeconds;
|
|
956
|
-
/** Override the
|
|
957
|
-
|
|
1471
|
+
/** Override the idle timeout at runtime. See {@link setIdleTimeoutInSeconds}. */
|
|
1472
|
+
setIdleTimeoutInSeconds: typeof setIdleTimeoutInSeconds;
|
|
958
1473
|
/** Override toUIMessageStream() options for the current turn. See {@link setUIMessageStreamOptions}. */
|
|
959
1474
|
setUIMessageStreamOptions: typeof setUIMessageStreamOptions;
|
|
960
1475
|
/** Check if the current turn was stopped by the user. See {@link isStopped}. */
|
|
@@ -977,4 +1492,33 @@ export declare const chat: {
|
|
|
977
1492
|
MessageAccumulator: typeof ChatMessageAccumulator;
|
|
978
1493
|
/** Create a chat session (async iterator). See {@link createChatSession}. */
|
|
979
1494
|
createSession: typeof createChatSession;
|
|
1495
|
+
/**
|
|
1496
|
+
* Store and retrieve a resolved prompt for the current run.
|
|
1497
|
+
*
|
|
1498
|
+
* - `chat.prompt.set(resolved)` — store a `ResolvedPrompt` or plain string
|
|
1499
|
+
* - `chat.prompt()` — read the stored prompt (throws if not set)
|
|
1500
|
+
*/
|
|
1501
|
+
prompt: typeof getChatPrompt & {
|
|
1502
|
+
set: typeof setChatPrompt;
|
|
1503
|
+
};
|
|
1504
|
+
/**
|
|
1505
|
+
* Returns an options object ready to spread into `streamText()`.
|
|
1506
|
+
* Reads the stored prompt and returns `{ system, experimental_telemetry, ...config }`.
|
|
1507
|
+
* Returns `{}` if no prompt has been set.
|
|
1508
|
+
*/
|
|
1509
|
+
toStreamTextOptions: typeof toStreamTextOptions;
|
|
1510
|
+
/**
|
|
1511
|
+
* Replace the accumulated conversation messages for compaction.
|
|
1512
|
+
* Call from `onTurnStart` or `onTurnComplete`. Takes `UIMessage[]` and
|
|
1513
|
+
* converts to `ModelMessage[]` internally.
|
|
1514
|
+
*/
|
|
1515
|
+
setMessages: typeof setChatMessages;
|
|
1516
|
+
/** Check if it's safe to compact messages (no in-flight tool calls). */
|
|
1517
|
+
isCompactionSafe: typeof isCompactionSafe;
|
|
1518
|
+
/** Returns a `prepareStep` function that handles context compaction automatically. */
|
|
1519
|
+
compactionStep: typeof chatCompactionStep;
|
|
1520
|
+
/** Low-level compaction for use inside a custom `prepareStep`. */
|
|
1521
|
+
compact: typeof chatCompact;
|
|
1522
|
+
/** Read the current compaction state (summary + base message count). */
|
|
1523
|
+
getCompactionState: typeof getCompactionState;
|
|
980
1524
|
};
|