@trigger.dev/sdk 0.0.0-prerelease-20260310150337 → 0.0.0-prerelease-20260325143642
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 +692 -27
- package/dist/commonjs/v3/ai.js +1148 -59
- package/dist/commonjs/v3/ai.js.map +1 -1
- package/dist/commonjs/v3/chat-react.d.ts +89 -1
- package/dist/commonjs/v3/chat-react.js +164 -0
- package/dist/commonjs/v3/chat-react.js.map +1 -1
- package/dist/commonjs/v3/chat.d.ts +18 -1
- package/dist/commonjs/v3/chat.js +40 -2
- package/dist/commonjs/v3/chat.js.map +1 -1
- 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 +692 -27
- package/dist/esm/v3/ai.js +1147 -58
- package/dist/esm/v3/ai.js.map +1 -1
- package/dist/esm/v3/chat-react.d.ts +89 -1
- package/dist/esm/v3/chat-react.js +164 -1
- package/dist/esm/v3/chat-react.js.map +1 -1
- package/dist/esm/v3/chat.d.ts +18 -1
- package/dist/esm/v3/chat.js +40 -2
- package/dist/esm/v3/chat.js.map +1 -1
- 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,467 @@ 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 `shouldInject` and `prepareMessages` callbacks.
|
|
284
|
+
*/
|
|
285
|
+
export type PendingMessagesBatchEvent = {
|
|
286
|
+
/** All pending UI messages that arrived during streaming (batch). */
|
|
287
|
+
messages: UIMessage[];
|
|
288
|
+
/** Current model messages in the conversation. */
|
|
289
|
+
modelMessages: ModelMessage[];
|
|
290
|
+
/** Completed steps so far. */
|
|
291
|
+
steps: CompactionStep[];
|
|
292
|
+
/** Current step number (0-indexed). */
|
|
293
|
+
stepNumber: number;
|
|
294
|
+
/** Chat session ID. */
|
|
295
|
+
chatId: string;
|
|
296
|
+
/** Current turn number (0-indexed). */
|
|
297
|
+
turn: number;
|
|
298
|
+
/** Custom data from the frontend. */
|
|
299
|
+
clientData?: unknown;
|
|
300
|
+
};
|
|
301
|
+
/**
|
|
302
|
+
* Event passed to `onReceived` callback (per-message, as they arrive).
|
|
303
|
+
*/
|
|
304
|
+
export type PendingMessageReceivedEvent = {
|
|
305
|
+
/** The UI message that arrived during streaming. */
|
|
306
|
+
message: UIMessage;
|
|
307
|
+
/** Chat session ID. */
|
|
308
|
+
chatId: string;
|
|
309
|
+
/** Current turn number (0-indexed). */
|
|
310
|
+
turn: number;
|
|
311
|
+
};
|
|
312
|
+
/**
|
|
313
|
+
* Event passed to `onInjected` callback (batch, after injection).
|
|
314
|
+
*/
|
|
315
|
+
export type PendingMessagesInjectedEvent = {
|
|
316
|
+
/** All UI messages that were injected. */
|
|
317
|
+
messages: UIMessage[];
|
|
318
|
+
/** The model messages that were injected. */
|
|
319
|
+
injectedModelMessages: ModelMessage[];
|
|
320
|
+
/** Chat session ID. */
|
|
321
|
+
chatId: string;
|
|
322
|
+
/** Current turn number (0-indexed). */
|
|
323
|
+
turn: number;
|
|
324
|
+
/** Step number where injection occurred. */
|
|
325
|
+
stepNumber: number;
|
|
326
|
+
};
|
|
327
|
+
/**
|
|
328
|
+
* Options for the `pendingMessages` field on `chat.task()`, `chat.createSession()`,
|
|
329
|
+
* or `ChatMessageAccumulator`.
|
|
330
|
+
*
|
|
331
|
+
* Configures how messages that arrive during streaming are handled. When
|
|
332
|
+
* `shouldInject` is provided and returns `true`, the full batch of pending
|
|
333
|
+
* messages is injected between tool-call steps via `prepareStep`.
|
|
334
|
+
* Otherwise, messages queue for the next turn.
|
|
335
|
+
*/
|
|
336
|
+
export type PendingMessagesOptions = {
|
|
337
|
+
/**
|
|
338
|
+
* Decide whether to inject pending messages between tool-call steps.
|
|
339
|
+
* Called once per step boundary with the full batch of pending messages.
|
|
340
|
+
* If absent, no injection happens — messages only queue for the next turn.
|
|
341
|
+
*/
|
|
342
|
+
shouldInject?: (event: PendingMessagesBatchEvent) => boolean | Promise<boolean>;
|
|
343
|
+
/**
|
|
344
|
+
* Transform the batch of pending messages before injection.
|
|
345
|
+
* Return the model messages to inject.
|
|
346
|
+
* Default: convert each UI message via `convertToModelMessages`.
|
|
347
|
+
*/
|
|
348
|
+
prepare?: (event: PendingMessagesBatchEvent) => ModelMessage[] | Promise<ModelMessage[]>;
|
|
349
|
+
/** Called when a message arrives during streaming (per-message). */
|
|
350
|
+
onReceived?: (event: PendingMessageReceivedEvent) => void | Promise<void>;
|
|
351
|
+
/** Called after a batch of messages is injected via `prepareStep`. */
|
|
352
|
+
onInjected?: (event: PendingMessagesInjectedEvent) => void | Promise<void>;
|
|
353
|
+
};
|
|
354
|
+
/**
|
|
355
|
+
* The data part type used to signal that pending messages were injected
|
|
356
|
+
* between tool-call steps. The frontend can match on this to render
|
|
357
|
+
* injection points inline in the assistant response.
|
|
358
|
+
*/
|
|
359
|
+
export declare const PENDING_MESSAGE_INJECTED_TYPE: "data-pending-message-injected";
|
|
360
|
+
/**
|
|
361
|
+
* Event passed to the `prepareMessages` hook.
|
|
362
|
+
*/
|
|
363
|
+
export type PrepareMessagesEvent<TClientData = unknown> = {
|
|
364
|
+
/** The messages to transform. Return the transformed array. */
|
|
365
|
+
messages: ModelMessage[];
|
|
366
|
+
/** Why messages are being prepared. */
|
|
367
|
+
reason: "run" | "compaction-rebuild" | "compaction-result";
|
|
368
|
+
/** The chat session ID. */
|
|
369
|
+
chatId: string;
|
|
370
|
+
/** The current turn number (0-indexed). */
|
|
371
|
+
turn: number;
|
|
372
|
+
/** Custom data from the frontend. */
|
|
373
|
+
clientData?: TClientData;
|
|
374
|
+
};
|
|
375
|
+
/**
|
|
376
|
+
* Data shape for `data-compaction` stream chunks emitted during compaction.
|
|
377
|
+
* Use to type the `data` field when rendering compaction parts in the frontend.
|
|
378
|
+
*/
|
|
379
|
+
export type CompactionChunkData = {
|
|
380
|
+
status: "compacting" | "complete";
|
|
381
|
+
totalTokens: number | undefined;
|
|
382
|
+
};
|
|
383
|
+
/**
|
|
384
|
+
* Event passed to the `onCompacted` callback.
|
|
385
|
+
*/
|
|
386
|
+
export type CompactedEvent = {
|
|
387
|
+
/** The generated summary text. */
|
|
388
|
+
summary: string;
|
|
389
|
+
/** The messages that were compacted (pre-compaction). */
|
|
390
|
+
messages: ModelMessage[];
|
|
391
|
+
/** Number of messages before compaction. */
|
|
392
|
+
messageCount: number;
|
|
393
|
+
/** Token usage from the step that triggered compaction. */
|
|
394
|
+
usage: LanguageModelUsage;
|
|
395
|
+
/** Total token count that triggered compaction. */
|
|
396
|
+
totalTokens: number | undefined;
|
|
397
|
+
/** Input token count from the triggering step. */
|
|
398
|
+
inputTokens: number | undefined;
|
|
399
|
+
/** Output token count from the triggering step. */
|
|
400
|
+
outputTokens: number | undefined;
|
|
401
|
+
/** The step number where compaction occurred (0-indexed). */
|
|
402
|
+
stepNumber: number;
|
|
403
|
+
/** The chat session ID (if running inside a chat.task). */
|
|
404
|
+
chatId?: string;
|
|
405
|
+
/** The current turn number (if running inside a chat.task). */
|
|
406
|
+
turn?: number;
|
|
407
|
+
};
|
|
408
|
+
/**
|
|
409
|
+
* Event passed to `shouldCompact` callbacks.
|
|
410
|
+
*/
|
|
411
|
+
export type ShouldCompactEvent = {
|
|
412
|
+
/** The current model messages (full conversation). */
|
|
413
|
+
messages: ModelMessage[];
|
|
414
|
+
/** Total token count from the triggering step/turn. */
|
|
415
|
+
totalTokens: number | undefined;
|
|
416
|
+
/** Input token count from the triggering step/turn. */
|
|
417
|
+
inputTokens: number | undefined;
|
|
418
|
+
/** Output token count from the triggering step/turn. */
|
|
419
|
+
outputTokens: number | undefined;
|
|
420
|
+
/** Full usage object from the triggering step/turn. */
|
|
421
|
+
usage?: LanguageModelUsage;
|
|
422
|
+
/** Cumulative token usage across all completed turns. Present in chat.task contexts. */
|
|
423
|
+
totalUsage?: LanguageModelUsage;
|
|
424
|
+
/** The chat session ID (if running inside a chat.task). */
|
|
425
|
+
chatId?: string;
|
|
426
|
+
/** The current turn number (0-indexed, if inside a chat.task). */
|
|
427
|
+
turn?: number;
|
|
428
|
+
/** Custom data from the frontend (if inside a chat.task). */
|
|
429
|
+
clientData?: unknown;
|
|
430
|
+
/**
|
|
431
|
+
* Where this check is running:
|
|
432
|
+
* - `"inner"` — between tool-call steps (prepareStep)
|
|
433
|
+
* - `"outer"` — between turns (after response, before onBeforeTurnComplete)
|
|
434
|
+
*/
|
|
435
|
+
source?: "inner" | "outer";
|
|
436
|
+
/** The step number (0-indexed). Only present when `source` is `"inner"`. */
|
|
437
|
+
stepNumber?: number;
|
|
438
|
+
/** The steps array from prepareStep. Only present when `source` is `"inner"`. */
|
|
439
|
+
steps?: CompactionStep[];
|
|
440
|
+
};
|
|
441
|
+
/**
|
|
442
|
+
* Options for `chat.compaction()` — the high-level prepareStep factory.
|
|
443
|
+
*/
|
|
444
|
+
export type CompactionOptions = {
|
|
445
|
+
/** Generate a summary from the current messages. Return the summary text. */
|
|
446
|
+
summarize: (messages: ModelMessage[]) => Promise<string>;
|
|
447
|
+
/** Token threshold — compact when totalTokens exceeds this. Ignored if `shouldCompact` is provided. */
|
|
448
|
+
threshold?: number;
|
|
449
|
+
/** Custom compaction trigger. When provided, used instead of `threshold`. */
|
|
450
|
+
shouldCompact?: (event: ShouldCompactEvent) => boolean | Promise<boolean>;
|
|
451
|
+
};
|
|
452
|
+
/** A step object as received in prepareStep's `steps` array. */
|
|
453
|
+
export type CompactionStep = {
|
|
454
|
+
usage: LanguageModelUsage;
|
|
455
|
+
finishReason: string;
|
|
456
|
+
content: Array<{
|
|
457
|
+
type: string;
|
|
458
|
+
toolCallId?: string;
|
|
459
|
+
}>;
|
|
460
|
+
response: {
|
|
461
|
+
messages: Array<any>;
|
|
462
|
+
};
|
|
463
|
+
};
|
|
464
|
+
/**
|
|
465
|
+
* Result of `chat.compact()`. Discriminated union so you can inspect
|
|
466
|
+
* what happened, but also directly compatible with prepareStep's return type.
|
|
467
|
+
*
|
|
468
|
+
* - `"skipped"` — no compaction needed (first step, boundary unsafe, or under threshold). Return `undefined` to prepareStep.
|
|
469
|
+
* - `"rebuilt"` — previous compaction exists, messages rebuilt from summary + new response messages.
|
|
470
|
+
* - `"compacted"` — compaction just happened, includes the generated summary.
|
|
471
|
+
*/
|
|
472
|
+
export type CompactResult = {
|
|
473
|
+
type: "skipped";
|
|
474
|
+
} | {
|
|
475
|
+
type: "rebuilt";
|
|
476
|
+
messages: ModelMessage[];
|
|
477
|
+
} | {
|
|
478
|
+
type: "compacted";
|
|
479
|
+
messages: ModelMessage[];
|
|
480
|
+
summary: string;
|
|
481
|
+
};
|
|
482
|
+
/**
|
|
483
|
+
* Options for `chat.compact()` — the low-level compaction function.
|
|
484
|
+
*/
|
|
485
|
+
export type CompactOptions = {
|
|
486
|
+
/** Generate a summary from the current messages. Return the summary text. */
|
|
487
|
+
summarize: (messages: ModelMessage[]) => Promise<string>;
|
|
488
|
+
/** Token threshold — compact when totalTokens exceeds this. Ignored if `shouldCompact` is provided. */
|
|
489
|
+
threshold?: number;
|
|
490
|
+
/** Custom compaction trigger. When provided, used instead of `threshold`. */
|
|
491
|
+
shouldCompact?: (event: ShouldCompactEvent) => boolean | Promise<boolean>;
|
|
492
|
+
};
|
|
493
|
+
/**
|
|
494
|
+
* Read the current compaction state. Returns the summary and base message count
|
|
495
|
+
* if compaction has occurred in this turn, or `undefined` if not.
|
|
496
|
+
*
|
|
497
|
+
* Use in a custom `prepareStep` to rebuild from a previous compaction:
|
|
498
|
+
* ```ts
|
|
499
|
+
* const state = chat.getCompactionState();
|
|
500
|
+
* if (state) {
|
|
501
|
+
* return { messages: [{ role: "user", content: state.summary }, ...newMsgs] };
|
|
502
|
+
* }
|
|
503
|
+
* ```
|
|
504
|
+
*/
|
|
505
|
+
declare function getCompactionState(): CompactionState | undefined;
|
|
506
|
+
/**
|
|
507
|
+
* Low-level compaction for use inside a custom `prepareStep`.
|
|
508
|
+
*
|
|
509
|
+
* Handles the full decision tree: first step, already-compacted rebuild,
|
|
510
|
+
* boundary safety, threshold check, summarization, stream chunks, state
|
|
511
|
+
* storage, and accumulator update.
|
|
512
|
+
*
|
|
513
|
+
* Returns a `CompactResult` — inspect `result.type` to see what happened,
|
|
514
|
+
* or convert to a prepareStep return with `result.type === "skipped" ? undefined : result`.
|
|
515
|
+
*
|
|
516
|
+
* @example
|
|
517
|
+
* ```ts
|
|
518
|
+
* prepareStep: async ({ messages, steps }) => {
|
|
519
|
+
* // your custom logic here...
|
|
520
|
+
* const result = await chat.compact(messages, steps, {
|
|
521
|
+
* threshold: 80_000,
|
|
522
|
+
* summarize: async (msgs) => generateText({ model, messages: msgs }).then(r => r.text),
|
|
523
|
+
* });
|
|
524
|
+
* if (result.type === "compacted") {
|
|
525
|
+
* logger.info("Compacted!", { summary: result.summary });
|
|
526
|
+
* }
|
|
527
|
+
* return result.type === "skipped" ? undefined : result;
|
|
528
|
+
* },
|
|
529
|
+
* ```
|
|
530
|
+
*/
|
|
531
|
+
declare function chatCompact(messages: ModelMessage[], steps: CompactionStep[], options: CompactOptions): Promise<CompactResult>;
|
|
532
|
+
/**
|
|
533
|
+
* Returns a `prepareStep` function that handles context compaction automatically.
|
|
534
|
+
*
|
|
535
|
+
* Monitors token usage between tool-call steps. When `totalTokens` exceeds
|
|
536
|
+
* the threshold, generates a summary via `summarize()`, replaces the message
|
|
537
|
+
* history, and emits `data-compaction` stream chunks for the frontend.
|
|
538
|
+
*
|
|
539
|
+
* @example
|
|
540
|
+
* ```ts
|
|
541
|
+
* return streamText({
|
|
542
|
+
* ...chat.toStreamTextOptions({ registry }),
|
|
543
|
+
* messages: chat.addCacheBreaks(messages),
|
|
544
|
+
* prepareStep: chat.compactionStep({
|
|
545
|
+
* threshold: 80_000,
|
|
546
|
+
* summarize: async (messages) => {
|
|
547
|
+
* return generateText({ model, messages: [...messages, { role: "user", content: "Summarize." }] })
|
|
548
|
+
* .then((r) => r.text);
|
|
549
|
+
* },
|
|
550
|
+
* }),
|
|
551
|
+
* tools: { ... },
|
|
552
|
+
* });
|
|
553
|
+
* ```
|
|
554
|
+
*/
|
|
555
|
+
declare function chatCompactionStep(options: CompactionOptions): (args: {
|
|
556
|
+
messages: ModelMessage[];
|
|
557
|
+
steps: CompactionStep[];
|
|
558
|
+
}) => Promise<{
|
|
559
|
+
messages: ModelMessage[];
|
|
560
|
+
} | undefined>;
|
|
561
|
+
/**
|
|
562
|
+
* Checks whether it's safe to compact the message history. Returns `false`
|
|
563
|
+
* if any tool calls are in-flight (incomplete tool invocations without results).
|
|
564
|
+
*
|
|
565
|
+
* Call before `chat.setMessages()` to avoid corrupting tool-call state.
|
|
566
|
+
*/
|
|
567
|
+
declare function isCompactionSafe(messages: UIMessage[]): boolean;
|
|
568
|
+
/**
|
|
569
|
+
* A resolved prompt stored via `chat.prompt.set()`. Either a full `ResolvedPrompt`
|
|
570
|
+
* from `prompts.define().resolve()`, or a lightweight wrapper around a plain string.
|
|
571
|
+
*/
|
|
572
|
+
export type ChatPromptValue = ResolvedPrompt | {
|
|
573
|
+
text: string;
|
|
574
|
+
model: undefined;
|
|
575
|
+
config: undefined;
|
|
576
|
+
promptId: string;
|
|
577
|
+
version: number;
|
|
578
|
+
labels: string[];
|
|
579
|
+
toAISDKTelemetry: (additionalMetadata?: Record<string, string>) => {
|
|
580
|
+
experimental_telemetry: {
|
|
581
|
+
isEnabled: true;
|
|
582
|
+
metadata: Record<string, string>;
|
|
583
|
+
};
|
|
584
|
+
};
|
|
585
|
+
};
|
|
586
|
+
/**
|
|
587
|
+
* Store a resolved prompt (or plain string) for the current run.
|
|
588
|
+
* Call from any hook (`onPreload`, `onChatStart`, `onTurnStart`) or `run()`.
|
|
589
|
+
*/
|
|
590
|
+
declare function setChatPrompt(resolved: ResolvedPrompt | string): void;
|
|
591
|
+
/**
|
|
592
|
+
* Read the stored prompt. Throws if `chat.prompt.set()` has not been called.
|
|
593
|
+
*/
|
|
594
|
+
declare function getChatPrompt(): ChatPromptValue;
|
|
595
|
+
/**
|
|
596
|
+
* Options for {@link toStreamTextOptions}.
|
|
597
|
+
*/
|
|
598
|
+
export type ToStreamTextOptionsOptions = {
|
|
599
|
+
/** Additional telemetry metadata merged into `experimental_telemetry.metadata`. */
|
|
600
|
+
telemetry?: Record<string, string>;
|
|
601
|
+
/**
|
|
602
|
+
* An AI SDK provider registry (from `createProviderRegistry`) or any object
|
|
603
|
+
* with a `languageModel(id)` method. When provided and the stored prompt has
|
|
604
|
+
* a `model` string, the resolved `LanguageModel` is included in the returned
|
|
605
|
+
* options so `streamText` uses it directly.
|
|
606
|
+
*
|
|
607
|
+
* The model string should use the `"provider:model-id"` format
|
|
608
|
+
* (e.g. `"openai:gpt-4o"`, `"anthropic:claude-sonnet-4-6"`).
|
|
609
|
+
*/
|
|
610
|
+
registry?: {
|
|
611
|
+
languageModel(modelId: string): unknown;
|
|
612
|
+
};
|
|
613
|
+
};
|
|
614
|
+
/**
|
|
615
|
+
* Returns an options object ready to spread into `streamText()`.
|
|
616
|
+
*
|
|
617
|
+
* Includes `system`, `experimental_telemetry`, and any config fields
|
|
618
|
+
* (temperature, maxTokens, etc.) from the stored prompt.
|
|
619
|
+
*
|
|
620
|
+
* When a `registry` is provided and the prompt has a `model` string,
|
|
621
|
+
* the resolved `LanguageModel` is included as `model`.
|
|
622
|
+
*
|
|
623
|
+
* If no prompt has been set, returns `{}` (no-op spread).
|
|
624
|
+
*/
|
|
625
|
+
declare function toStreamTextOptions(options?: ToStreamTextOptionsOptions): Record<string, unknown>;
|
|
165
626
|
/**
|
|
166
627
|
* Options for `pipeChat`.
|
|
167
628
|
*/
|
|
@@ -312,6 +773,10 @@ export type TurnStartEvent<TClientData = unknown> = {
|
|
|
312
773
|
previousRunId?: string;
|
|
313
774
|
/** Whether this run was preloaded before the first message. */
|
|
314
775
|
preloaded: boolean;
|
|
776
|
+
/** Token usage from the previous turn. Undefined on turn 0. */
|
|
777
|
+
previousTurnUsage?: LanguageModelUsage;
|
|
778
|
+
/** Cumulative token usage across all completed turns so far. */
|
|
779
|
+
totalUsage: LanguageModelUsage;
|
|
315
780
|
};
|
|
316
781
|
/**
|
|
317
782
|
* Event passed to the `onTurnComplete` callback.
|
|
@@ -362,6 +827,10 @@ export type TurnCompleteEvent<TClientData = unknown> = {
|
|
|
362
827
|
previousRunId?: string;
|
|
363
828
|
/** Whether this run was preloaded before the first message. */
|
|
364
829
|
preloaded: boolean;
|
|
830
|
+
/** Token usage for this turn. Undefined if usage couldn't be captured (e.g. manual pipeChat). */
|
|
831
|
+
usage?: LanguageModelUsage;
|
|
832
|
+
/** Cumulative token usage across all turns in this run (including this turn). */
|
|
833
|
+
totalUsage: LanguageModelUsage;
|
|
365
834
|
};
|
|
366
835
|
export type ChatTaskOptions<TIdentifier extends string, TClientDataSchema extends TaskSchema | undefined = undefined> = Omit<TaskOptions<TIdentifier, ChatTaskWirePayload, unknown>, "run"> & {
|
|
367
836
|
/**
|
|
@@ -437,10 +906,83 @@ export type ChatTaskOptions<TIdentifier extends string, TClientDataSchema extend
|
|
|
437
906
|
*/
|
|
438
907
|
onTurnStart?: (event: TurnStartEvent<inferSchemaOut<TClientDataSchema>>) => Promise<void> | void;
|
|
439
908
|
/**
|
|
440
|
-
* Called after
|
|
441
|
-
*
|
|
909
|
+
* Called after the response is captured but before the stream closes.
|
|
910
|
+
* The stream is still open, so you can write custom chunks to the frontend
|
|
911
|
+
* (e.g. compaction progress). Use this for compaction, post-processing,
|
|
912
|
+
* or any work where the user should see real-time status updates.
|
|
913
|
+
*
|
|
914
|
+
* @example
|
|
915
|
+
* ```ts
|
|
916
|
+
* onBeforeTurnComplete: async ({ messages, uiMessages, usage }) => {
|
|
917
|
+
* if (usage?.inputTokens && usage.inputTokens > 5000) {
|
|
918
|
+
* await chat.stream.append({ type: "data-compaction", id: generateId(), data: { status: "compacting" } });
|
|
919
|
+
* // ... compact messages ...
|
|
920
|
+
* chat.setMessages(compactedMessages);
|
|
921
|
+
* await chat.stream.append({ type: "data-compaction", id: generateId(), data: { status: "complete" } });
|
|
922
|
+
* }
|
|
923
|
+
* }
|
|
924
|
+
* ```
|
|
925
|
+
*/
|
|
926
|
+
onBeforeTurnComplete?: (event: TurnCompleteEvent<inferSchemaOut<TClientDataSchema>>) => Promise<void> | void;
|
|
927
|
+
/**
|
|
928
|
+
* Called when conversation compaction occurs (via `chat.compact()` or
|
|
929
|
+
* `chat.compactionStep()`). Use for logging, billing, or persisting the summary.
|
|
930
|
+
*
|
|
931
|
+
* @example
|
|
932
|
+
* ```ts
|
|
933
|
+
* onCompacted: async ({ summary, totalTokens, chatId }) => {
|
|
934
|
+
* logger.info("Compacted", { totalTokens, chatId });
|
|
935
|
+
* await db.compactionLog.create({ data: { chatId, summary } });
|
|
936
|
+
* }
|
|
937
|
+
* ```
|
|
938
|
+
*/
|
|
939
|
+
onCompacted?: (event: CompactedEvent) => Promise<void> | void;
|
|
940
|
+
/**
|
|
941
|
+
* Automatic context compaction. When provided, compaction runs automatically
|
|
942
|
+
* in both the inner loop (prepareStep, between tool-call steps) and the
|
|
943
|
+
* outer loop (between turns, for single-step responses where prepareStep
|
|
944
|
+
* never fires).
|
|
442
945
|
*
|
|
443
|
-
*
|
|
946
|
+
* The `shouldCompact` callback decides when to compact, and `summarize`
|
|
947
|
+
* generates the summary. The prepareStep is auto-injected into
|
|
948
|
+
* `chat.toStreamTextOptions()` — if you provide your own `prepareStep`
|
|
949
|
+
* after spreading, it overrides the auto-injected one.
|
|
950
|
+
*
|
|
951
|
+
* @example
|
|
952
|
+
* ```ts
|
|
953
|
+
* chat.task({
|
|
954
|
+
* id: "my-chat",
|
|
955
|
+
* compaction: {
|
|
956
|
+
* shouldCompact: ({ totalTokens }) => (totalTokens ?? 0) > 80_000,
|
|
957
|
+
* summarize: async (messages) =>
|
|
958
|
+
* generateText({ model, messages: [...messages, { role: "user", content: "Summarize." }] })
|
|
959
|
+
* .then((r) => r.text),
|
|
960
|
+
* },
|
|
961
|
+
* run: async ({ messages, signal }) => {
|
|
962
|
+
* return streamText({ ...chat.toStreamTextOptions({ registry }), messages });
|
|
963
|
+
* },
|
|
964
|
+
* });
|
|
965
|
+
* ```
|
|
966
|
+
*/
|
|
967
|
+
compaction?: ChatTaskCompactionOptions;
|
|
968
|
+
/**
|
|
969
|
+
* Configure how messages that arrive during streaming are handled.
|
|
970
|
+
*
|
|
971
|
+
* By default, messages queue for the next turn. When `shouldInject` is provided
|
|
972
|
+
* and returns `true`, messages are injected between tool-call steps via
|
|
973
|
+
* `prepareStep` — allowing users to steer the agent mid-execution.
|
|
974
|
+
*
|
|
975
|
+
* @example
|
|
976
|
+
* ```ts
|
|
977
|
+
* pendingMessages: {
|
|
978
|
+
* shouldInject: ({ steps }) => steps.length > 0,
|
|
979
|
+
* onReceived: ({ message }) => logger.info("Steering message received"),
|
|
980
|
+
* },
|
|
981
|
+
* ```
|
|
982
|
+
*/
|
|
983
|
+
pendingMessages?: PendingMessagesOptions;
|
|
984
|
+
/**
|
|
985
|
+
* conversation to your database after each assistant response.
|
|
444
986
|
*
|
|
445
987
|
* @example
|
|
446
988
|
* ```ts
|
|
@@ -466,16 +1008,16 @@ export type ChatTaskOptions<TIdentifier extends string, TClientDataSchema extend
|
|
|
466
1008
|
*/
|
|
467
1009
|
turnTimeout?: string;
|
|
468
1010
|
/**
|
|
469
|
-
* How long (in seconds)
|
|
470
|
-
*
|
|
471
|
-
*
|
|
472
|
-
*
|
|
1011
|
+
* How long (in seconds) the run stays idle (active, using compute) after each
|
|
1012
|
+
* turn, waiting for the next message. During this window responses are instant.
|
|
1013
|
+
* After this timeout the run suspends (frees compute) and waits via
|
|
1014
|
+
* `inputStream.wait()`.
|
|
473
1015
|
*
|
|
474
1016
|
* Set to `0` to suspend immediately after each turn.
|
|
475
1017
|
*
|
|
476
1018
|
* @default 30
|
|
477
1019
|
*/
|
|
478
|
-
|
|
1020
|
+
idleTimeoutInSeconds?: number;
|
|
479
1021
|
/**
|
|
480
1022
|
* How long the `chatAccessToken` (scoped to this run) remains valid.
|
|
481
1023
|
* A fresh token is minted after each turn, so this only needs to cover
|
|
@@ -487,14 +1029,14 @@ export type ChatTaskOptions<TIdentifier extends string, TClientDataSchema extend
|
|
|
487
1029
|
*/
|
|
488
1030
|
chatAccessTokenTTL?: string;
|
|
489
1031
|
/**
|
|
490
|
-
* How long (in seconds)
|
|
1032
|
+
* How long (in seconds) the run stays idle after `onPreload` fires,
|
|
491
1033
|
* waiting for the first message before suspending.
|
|
492
1034
|
*
|
|
493
1035
|
* Only applies to preloaded runs (triggered via `transport.preload()`).
|
|
494
1036
|
*
|
|
495
|
-
* @default Same as `
|
|
1037
|
+
* @default Same as `idleTimeoutInSeconds`
|
|
496
1038
|
*/
|
|
497
|
-
|
|
1039
|
+
preloadIdleTimeoutInSeconds?: number;
|
|
498
1040
|
/**
|
|
499
1041
|
* How long to wait (suspended) for the first message after a preloaded run starts.
|
|
500
1042
|
* If no message arrives within this time, the run ends.
|
|
@@ -504,6 +1046,27 @@ export type ChatTaskOptions<TIdentifier extends string, TClientDataSchema extend
|
|
|
504
1046
|
* @default Same as `turnTimeout`
|
|
505
1047
|
*/
|
|
506
1048
|
preloadTimeout?: string;
|
|
1049
|
+
/**
|
|
1050
|
+
* Transform model messages before they're used anywhere — in `run()`,
|
|
1051
|
+
* in compaction rebuilds, and in compaction results.
|
|
1052
|
+
*
|
|
1053
|
+
* Define once, applied everywhere. Use for Anthropic cache breaks,
|
|
1054
|
+
* injecting system context, stripping PII, etc.
|
|
1055
|
+
*
|
|
1056
|
+
* @example
|
|
1057
|
+
* ```ts
|
|
1058
|
+
* prepareMessages: async ({ messages, reason }) => {
|
|
1059
|
+
* // Add Anthropic cache breaks to the last message
|
|
1060
|
+
* if (messages.length === 0) return messages;
|
|
1061
|
+
* const last = messages[messages.length - 1];
|
|
1062
|
+
* return [...messages.slice(0, -1), {
|
|
1063
|
+
* ...last,
|
|
1064
|
+
* providerOptions: { ...last.providerOptions, anthropic: { cacheControl: { type: "ephemeral" } } },
|
|
1065
|
+
* }];
|
|
1066
|
+
* }
|
|
1067
|
+
* ```
|
|
1068
|
+
*/
|
|
1069
|
+
prepareMessages?: (event: PrepareMessagesEvent<inferSchemaOut<TClientDataSchema>>) => ModelMessage[] | Promise<ModelMessage[]>;
|
|
507
1070
|
/**
|
|
508
1071
|
* Default options for `toUIMessageStream()` when auto-piping or using
|
|
509
1072
|
* `turn.complete()` / `chat.pipeAndCapture()`.
|
|
@@ -596,23 +1159,23 @@ declare function setTurnTimeout(duration: string): void;
|
|
|
596
1159
|
*/
|
|
597
1160
|
declare function setTurnTimeoutInSeconds(seconds: number): void;
|
|
598
1161
|
/**
|
|
599
|
-
* Override the
|
|
1162
|
+
* Override the idle timeout for subsequent turns in the current run.
|
|
600
1163
|
*
|
|
601
|
-
* The
|
|
1164
|
+
* The idle timeout controls how long the run stays active (using compute)
|
|
602
1165
|
* after each turn, waiting for the next message. During this window,
|
|
603
1166
|
* responses are instant. After it expires, the run suspends.
|
|
604
1167
|
*
|
|
605
|
-
* @param seconds - Number of seconds to stay
|
|
1168
|
+
* @param seconds - Number of seconds to stay idle (0 to suspend immediately)
|
|
606
1169
|
*
|
|
607
1170
|
* @example
|
|
608
1171
|
* ```ts
|
|
609
1172
|
* run: async ({ messages, signal }) => {
|
|
610
|
-
* chat.
|
|
1173
|
+
* chat.setIdleTimeoutInSeconds(60);
|
|
611
1174
|
* return streamText({ model, messages, abortSignal: signal });
|
|
612
1175
|
* }
|
|
613
1176
|
* ```
|
|
614
1177
|
*/
|
|
615
|
-
declare function
|
|
1178
|
+
declare function setIdleTimeoutInSeconds(seconds: number): void;
|
|
616
1179
|
/**
|
|
617
1180
|
* Override the `toUIMessageStream()` options for the current turn.
|
|
618
1181
|
*
|
|
@@ -778,6 +1341,13 @@ declare function pipeChatAndCapture(source: UIMessageStreamable, options?: {
|
|
|
778
1341
|
declare class ChatMessageAccumulator {
|
|
779
1342
|
modelMessages: ModelMessage[];
|
|
780
1343
|
uiMessages: UIMessage[];
|
|
1344
|
+
private _compaction?;
|
|
1345
|
+
private _pendingMessages?;
|
|
1346
|
+
private _steeringQueue;
|
|
1347
|
+
constructor(options?: {
|
|
1348
|
+
compaction?: ChatTaskCompactionOptions;
|
|
1349
|
+
pendingMessages?: PendingMessagesOptions;
|
|
1350
|
+
});
|
|
781
1351
|
/**
|
|
782
1352
|
* Add incoming messages from the transport payload.
|
|
783
1353
|
* Returns the full accumulated model messages for `streamText`.
|
|
@@ -787,17 +1357,62 @@ declare class ChatMessageAccumulator {
|
|
|
787
1357
|
* Add the assistant's response to the accumulator.
|
|
788
1358
|
* Call after `pipeAndCapture` with the captured response.
|
|
789
1359
|
*/
|
|
1360
|
+
/**
|
|
1361
|
+
* Replace all accumulated messages (for compaction).
|
|
1362
|
+
* Converts UIMessages to ModelMessages internally.
|
|
1363
|
+
*/
|
|
1364
|
+
setMessages(uiMessages: UIMessage[]): Promise<void>;
|
|
790
1365
|
addResponse(response: UIMessage): Promise<void>;
|
|
1366
|
+
/**
|
|
1367
|
+
* Queue a message for injection via `prepareStep`. Call from a
|
|
1368
|
+
* `messagesInput.on()` listener when a message arrives during streaming.
|
|
1369
|
+
*/
|
|
1370
|
+
steer(message: UIMessage, modelMessages?: ModelMessage[]): void;
|
|
1371
|
+
/**
|
|
1372
|
+
* Queue a message for injection, converting to model messages automatically.
|
|
1373
|
+
*/
|
|
1374
|
+
steerAsync(message: UIMessage): Promise<void>;
|
|
1375
|
+
/**
|
|
1376
|
+
* Get and clear unconsumed steering messages.
|
|
1377
|
+
*/
|
|
1378
|
+
drainSteering(): UIMessage[];
|
|
1379
|
+
/**
|
|
1380
|
+
* Returns a `prepareStep` function that handles both compaction and
|
|
1381
|
+
* pending message injection. Pass to `streamText({ prepareStep: conversation.prepareStep() })`.
|
|
1382
|
+
*/
|
|
1383
|
+
prepareStep(): ((args: {
|
|
1384
|
+
messages: ModelMessage[];
|
|
1385
|
+
steps: CompactionStep[];
|
|
1386
|
+
}) => Promise<{
|
|
1387
|
+
messages: ModelMessage[];
|
|
1388
|
+
} | undefined>) | undefined;
|
|
1389
|
+
/**
|
|
1390
|
+
* Run outer-loop compaction if needed. Call after adding the response
|
|
1391
|
+
* and capturing usage. Applies `compactModelMessages` and `compactUIMessages`
|
|
1392
|
+
* callbacks if configured.
|
|
1393
|
+
*
|
|
1394
|
+
* @returns `true` if compaction was performed, `false` otherwise.
|
|
1395
|
+
*/
|
|
1396
|
+
compactIfNeeded(usage: LanguageModelUsage | undefined, context?: {
|
|
1397
|
+
chatId?: string;
|
|
1398
|
+
turn?: number;
|
|
1399
|
+
clientData?: unknown;
|
|
1400
|
+
totalUsage?: LanguageModelUsage;
|
|
1401
|
+
}): Promise<boolean>;
|
|
791
1402
|
}
|
|
792
1403
|
export type ChatSessionOptions = {
|
|
793
1404
|
/** Run-level cancel signal (from task context). */
|
|
794
1405
|
signal: AbortSignal;
|
|
795
|
-
/** Seconds to stay
|
|
796
|
-
|
|
1406
|
+
/** Seconds to stay idle between turns before suspending. @default 30 */
|
|
1407
|
+
idleTimeoutInSeconds?: number;
|
|
797
1408
|
/** Duration string for suspend timeout. @default "1h" */
|
|
798
1409
|
timeout?: string;
|
|
799
1410
|
/** Max turns before ending. @default 100 */
|
|
800
1411
|
maxTurns?: number;
|
|
1412
|
+
/** Automatic context compaction — same options as `chat.task({ compaction })`. */
|
|
1413
|
+
compaction?: ChatTaskCompactionOptions;
|
|
1414
|
+
/** Configure mid-execution message injection — same options as `chat.task({ pendingMessages })`. */
|
|
1415
|
+
pendingMessages?: PendingMessagesOptions;
|
|
801
1416
|
};
|
|
802
1417
|
export type ChatTurn = {
|
|
803
1418
|
/** Turn number (0-indexed). */
|
|
@@ -809,15 +1424,25 @@ export type ChatTurn = {
|
|
|
809
1424
|
/** Client data from the transport (`metadata` field on the wire payload). */
|
|
810
1425
|
clientData: unknown;
|
|
811
1426
|
/** Full accumulated model messages — pass directly to `streamText`. */
|
|
812
|
-
messages: ModelMessage[];
|
|
1427
|
+
readonly messages: ModelMessage[];
|
|
813
1428
|
/** Full accumulated UI messages — use for persistence. */
|
|
814
|
-
uiMessages: UIMessage[];
|
|
1429
|
+
readonly uiMessages: UIMessage[];
|
|
815
1430
|
/** Combined stop+cancel AbortSignal (fresh each turn). */
|
|
816
1431
|
signal: AbortSignal;
|
|
817
1432
|
/** Whether the user stopped generation this turn. */
|
|
818
1433
|
readonly stopped: boolean;
|
|
819
1434
|
/** Whether this is a continuation run. */
|
|
820
1435
|
continuation: boolean;
|
|
1436
|
+
/** Token usage from the previous turn. Undefined on turn 0. */
|
|
1437
|
+
previousTurnUsage?: LanguageModelUsage;
|
|
1438
|
+
/** Cumulative token usage across all completed turns so far. */
|
|
1439
|
+
totalUsage: LanguageModelUsage;
|
|
1440
|
+
/**
|
|
1441
|
+
* Replace accumulated messages (for compaction). Takes UIMessages and
|
|
1442
|
+
* converts to ModelMessages internally. After calling this, `turn.messages`
|
|
1443
|
+
* reflects the compacted history.
|
|
1444
|
+
*/
|
|
1445
|
+
setMessages(uiMessages: UIMessage[]): Promise<void>;
|
|
821
1446
|
/**
|
|
822
1447
|
* Easy path: pipe stream, capture response, accumulate it,
|
|
823
1448
|
* clean up aborted parts if stopped, and write turn-complete chunk.
|
|
@@ -833,12 +1458,23 @@ export type ChatTurn = {
|
|
|
833
1458
|
* Use with `chat.pipeAndCapture` when you need control between pipe and done.
|
|
834
1459
|
*/
|
|
835
1460
|
addResponse(response: UIMessage): Promise<void>;
|
|
1461
|
+
/**
|
|
1462
|
+
* Returns a `prepareStep` function that handles both compaction and
|
|
1463
|
+
* pending message injection. Pass to `streamText({ prepareStep: turn.prepareStep() })`.
|
|
1464
|
+
* Only needed when not using `chat.toStreamTextOptions()` (which auto-injects it).
|
|
1465
|
+
*/
|
|
1466
|
+
prepareStep(): ((args: {
|
|
1467
|
+
messages: ModelMessage[];
|
|
1468
|
+
steps: CompactionStep[];
|
|
1469
|
+
}) => Promise<{
|
|
1470
|
+
messages: ModelMessage[];
|
|
1471
|
+
} | undefined>) | undefined;
|
|
836
1472
|
};
|
|
837
1473
|
/**
|
|
838
1474
|
* Create a chat session that yields turns as an async iterator.
|
|
839
1475
|
*
|
|
840
1476
|
* Handles: preload wait, stop signals, message accumulation, turn-complete
|
|
841
|
-
* signaling, and
|
|
1477
|
+
* signaling, and idle/suspend between turns. You control: initialization,
|
|
842
1478
|
* model/tool selection, persistence, and any custom per-turn logic.
|
|
843
1479
|
*
|
|
844
1480
|
* @example
|
|
@@ -953,8 +1589,8 @@ export declare const chat: {
|
|
|
953
1589
|
setTurnTimeout: typeof setTurnTimeout;
|
|
954
1590
|
/** Override the turn timeout at runtime (seconds). See {@link setTurnTimeoutInSeconds}. */
|
|
955
1591
|
setTurnTimeoutInSeconds: typeof setTurnTimeoutInSeconds;
|
|
956
|
-
/** Override the
|
|
957
|
-
|
|
1592
|
+
/** Override the idle timeout at runtime. See {@link setIdleTimeoutInSeconds}. */
|
|
1593
|
+
setIdleTimeoutInSeconds: typeof setIdleTimeoutInSeconds;
|
|
958
1594
|
/** Override toUIMessageStream() options for the current turn. See {@link setUIMessageStreamOptions}. */
|
|
959
1595
|
setUIMessageStreamOptions: typeof setUIMessageStreamOptions;
|
|
960
1596
|
/** Check if the current turn was stopped by the user. See {@link isStopped}. */
|
|
@@ -977,4 +1613,33 @@ export declare const chat: {
|
|
|
977
1613
|
MessageAccumulator: typeof ChatMessageAccumulator;
|
|
978
1614
|
/** Create a chat session (async iterator). See {@link createChatSession}. */
|
|
979
1615
|
createSession: typeof createChatSession;
|
|
1616
|
+
/**
|
|
1617
|
+
* Store and retrieve a resolved prompt for the current run.
|
|
1618
|
+
*
|
|
1619
|
+
* - `chat.prompt.set(resolved)` — store a `ResolvedPrompt` or plain string
|
|
1620
|
+
* - `chat.prompt()` — read the stored prompt (throws if not set)
|
|
1621
|
+
*/
|
|
1622
|
+
prompt: typeof getChatPrompt & {
|
|
1623
|
+
set: typeof setChatPrompt;
|
|
1624
|
+
};
|
|
1625
|
+
/**
|
|
1626
|
+
* Returns an options object ready to spread into `streamText()`.
|
|
1627
|
+
* Reads the stored prompt and returns `{ system, experimental_telemetry, ...config }`.
|
|
1628
|
+
* Returns `{}` if no prompt has been set.
|
|
1629
|
+
*/
|
|
1630
|
+
toStreamTextOptions: typeof toStreamTextOptions;
|
|
1631
|
+
/**
|
|
1632
|
+
* Replace the accumulated conversation messages for compaction.
|
|
1633
|
+
* Call from `onTurnStart` or `onTurnComplete`. Takes `UIMessage[]` and
|
|
1634
|
+
* converts to `ModelMessage[]` internally.
|
|
1635
|
+
*/
|
|
1636
|
+
setMessages: typeof setChatMessages;
|
|
1637
|
+
/** Check if it's safe to compact messages (no in-flight tool calls). */
|
|
1638
|
+
isCompactionSafe: typeof isCompactionSafe;
|
|
1639
|
+
/** Returns a `prepareStep` function that handles context compaction automatically. */
|
|
1640
|
+
compactionStep: typeof chatCompactionStep;
|
|
1641
|
+
/** Low-level compaction for use inside a custom `prepareStep`. */
|
|
1642
|
+
compact: typeof chatCompact;
|
|
1643
|
+
/** Read the current compaction state (summary + base message count). */
|
|
1644
|
+
getCompactionState: typeof getCompactionState;
|
|
980
1645
|
};
|