ai 3.1.0-canary.2 → 3.1.0-canary.4

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.
@@ -51,6 +51,10 @@ type LanguageModelV1CallSettings = {
51
51
  * by the model, calls will generate deterministic results.
52
52
  */
53
53
  seed?: number;
54
+ /**
55
+ * Abort signal for cancelling the operation.
56
+ */
57
+ abortSignal?: AbortSignal;
54
58
  };
55
59
 
56
60
  /**
@@ -106,9 +110,9 @@ interface LanguageModelV1TextPart {
106
110
  interface LanguageModelV1ImagePart {
107
111
  type: 'image';
108
112
  /**
109
- * Image data as a Uint8Array.
113
+ * Image data as a Uint8Array (e.g. from a Blob or Buffer) or a URL.
110
114
  */
111
- image: Uint8Array;
115
+ image: Uint8Array | URL;
112
116
  /**
113
117
  * Optional mime type of the image.
114
118
  */
@@ -234,6 +238,32 @@ type LanguageModelV1 = {
234
238
  * model has only generated text.
235
239
  */
236
240
  toolCalls?: Array<LanguageModelV1FunctionToolCall>;
241
+ /**
242
+ * Finish reason.
243
+ */
244
+ finishReason: LanguageModelV1FinishReason;
245
+ /**
246
+ * Usage information.
247
+ */
248
+ usage: {
249
+ promptTokens: number;
250
+ completionTokens: number;
251
+ };
252
+ /**
253
+ * Raw prompt and setting information for observability provider integration.
254
+ */
255
+ rawCall: {
256
+ /**
257
+ * Raw prompt after expansion and conversion to the format that the
258
+ * provider uses to send the information to their API.
259
+ */
260
+ rawPrompt: unknown;
261
+ /**
262
+ * Raw settings that are used for the API call. Includes provider-specific
263
+ * settings.
264
+ */
265
+ rawSettings: Record<string, unknown>;
266
+ };
237
267
  warnings?: LanguageModelV1CallWarning[];
238
268
  }>;
239
269
  /**
@@ -246,6 +276,21 @@ type LanguageModelV1 = {
246
276
  */
247
277
  doStream(options: LanguageModelV1CallOptions): PromiseLike<{
248
278
  stream: ReadableStream<LanguageModelV1StreamPart>;
279
+ /**
280
+ * Raw prompt and setting information for observability provider integration.
281
+ */
282
+ rawCall: {
283
+ /**
284
+ * Raw prompt after expansion and conversion to the format that the
285
+ * provider uses to send the information to their API.
286
+ */
287
+ rawPrompt: unknown;
288
+ /**
289
+ * Raw settings that are used for the API call. Includes provider-specific
290
+ * settings.
291
+ */
292
+ rawSettings: Record<string, unknown>;
293
+ };
249
294
  warnings?: LanguageModelV1CallWarning[];
250
295
  }>;
251
296
  };
@@ -271,6 +316,12 @@ type LanguageModelV1StreamPart = {
271
316
  error: unknown;
272
317
  };
273
318
 
319
+ type TokenUsage = {
320
+ promptTokens: number;
321
+ completionTokens: number;
322
+ totalTokens: number;
323
+ };
324
+
274
325
  type CallSettings = {
275
326
  /**
276
327
  * Maximum number of tokens to generate.
@@ -281,6 +332,8 @@ type CallSettings = {
281
332
  * 1 (very random).
282
333
  *
283
334
  * It is recommended to set either `temperature` or `topP`, but not both.
335
+ *
336
+ * Default: 0
284
337
  */
285
338
  temperature?: number;
286
339
  /**
@@ -298,6 +351,8 @@ type CallSettings = {
298
351
  *
299
352
  * The presence penalty is a number between -1 (increase repetition)
300
353
  * and 1 (maximum penalty, decrease repetition). 0 means no penalty.
354
+ *
355
+ * Default: 0
301
356
  */
302
357
  presencePenalty?: number;
303
358
  /**
@@ -306,6 +361,8 @@ type CallSettings = {
306
361
  *
307
362
  * The frequency penalty is a number between -1 (increase repetition)
308
363
  * and 1 (maximum penalty, decrease repetition). 0 means no penalty.
364
+ *
365
+ * Default: 0
309
366
  */
310
367
  frequencyPenalty?: number;
311
368
  /**
@@ -313,6 +370,16 @@ type CallSettings = {
313
370
  * by the model, calls will generate deterministic results.
314
371
  */
315
372
  seed?: number;
373
+ /**
374
+ * Maximum number of retries. Set to 0 to disable retries.
375
+ *
376
+ * Default: 2.
377
+ */
378
+ maxRetries?: number;
379
+ /**
380
+ * Abort signal.
381
+ */
382
+ abortSignal?: AbortSignal;
316
383
  };
317
384
 
318
385
  /**
@@ -332,9 +399,12 @@ interface TextPart {
332
399
  interface ImagePart {
333
400
  type: 'image';
334
401
  /**
335
- * Image data. Can either be a base64-encoded string, a Uint8Array, an ArrayBuffer, or a Buffer.
402
+ * Image data. Can either be:
403
+ *
404
+ * - data: a base64-encoded string, a Uint8Array, an ArrayBuffer, or a Buffer
405
+ * - URL: a URL that points to the image
336
406
  */
337
- image: DataContent;
407
+ image: DataContent | URL;
338
408
  /**
339
409
  * Optional mime type of the image.
340
410
  */
@@ -379,31 +449,38 @@ type Prompt = {
379
449
  /**
380
450
  * Generate a structured, typed object using a language model.
381
451
  */
382
- declare function generateObject<T>({ model, schema, mode, system, prompt, messages, ...settings }: CallSettings & Prompt & {
452
+ declare function generateObject<T>({ model, schema, mode, system, prompt, messages, maxRetries, abortSignal, ...settings }: CallSettings & Prompt & {
383
453
  model: LanguageModelV1;
384
454
  schema: z.Schema<T>;
385
455
  mode?: 'auto' | 'json' | 'tool' | 'grammar';
386
456
  }): Promise<GenerateObjectResult<T>>;
387
457
  declare class GenerateObjectResult<T> {
388
458
  readonly object: T;
459
+ readonly finishReason: LanguageModelV1FinishReason;
460
+ readonly usage: TokenUsage;
389
461
  constructor(options: {
390
462
  object: T;
463
+ finishReason: LanguageModelV1FinishReason;
464
+ usage: TokenUsage;
391
465
  });
392
466
  }
393
467
 
468
+ type AsyncIterableStream<T> = AsyncIterable<T> & ReadableStream<T>;
469
+
394
470
  /**
395
471
  * Stream an object as a partial object stream.
396
472
  */
397
- declare function streamObject<T>({ model, schema, mode, system, prompt, messages, ...settings }: CallSettings & Prompt & {
473
+ declare function streamObject<T>({ model, schema, mode, system, prompt, messages, maxRetries, abortSignal, ...settings }: CallSettings & Prompt & {
398
474
  model: LanguageModelV1;
399
475
  schema: z.Schema<T>;
400
476
  mode?: 'auto' | 'json' | 'tool' | 'grammar';
401
477
  }): Promise<StreamObjectResult<T>>;
402
478
  declare class StreamObjectResult<T> {
403
- readonly objectStream: AsyncIterable<PartialDeep<T, {
479
+ private readonly originalStream;
480
+ constructor(stream: ReadableStream<string | ErrorStreamPart>);
481
+ get objectStream(): AsyncIterableStream<PartialDeep<T, {
404
482
  recurseIntoArrays: true;
405
483
  }>>;
406
- constructor(modelStream: ReadableStream<string | ErrorStreamPart>);
407
484
  }
408
485
  type ErrorStreamPart = {
409
486
  type: 'error';
@@ -477,7 +554,7 @@ type ToToolResultArray<TOOLS extends Record<string, Tool>> = Array<ToToolResult<
477
554
  /**
478
555
  * Generate a text and call tools using a language model.
479
556
  */
480
- declare function generateText<TOOLS extends Record<string, Tool>>({ model, tools, system, prompt, messages, ...settings }: CallSettings & Prompt & {
557
+ declare function generateText<TOOLS extends Record<string, Tool>>({ model, tools, system, prompt, messages, maxRetries, abortSignal, ...settings }: CallSettings & Prompt & {
481
558
  model: LanguageModelV1;
482
559
  tools?: TOOLS;
483
560
  }): Promise<GenerateTextResult<TOOLS>>;
@@ -485,21 +562,45 @@ declare class GenerateTextResult<TOOLS extends Record<string, Tool>> {
485
562
  readonly text: string;
486
563
  readonly toolCalls: ToToolCallArray<TOOLS>;
487
564
  readonly toolResults: ToToolResultArray<TOOLS>;
565
+ readonly finishReason: LanguageModelV1FinishReason;
566
+ readonly usage: TokenUsage;
488
567
  constructor(options: {
489
568
  text: string;
490
569
  toolCalls: ToToolCallArray<TOOLS>;
491
570
  toolResults: ToToolResultArray<TOOLS>;
571
+ finishReason: LanguageModelV1FinishReason;
572
+ usage: TokenUsage;
492
573
  });
493
574
  }
494
575
 
495
- declare class StreamTextHttpResponse extends Response {
496
- constructor(messageStream: ReadableStream<TextStreamPart<any>>);
576
+ /**
577
+ * Configuration options and helper callback methods for AIStream stream lifecycle events.
578
+ * @interface
579
+ */
580
+ interface AIStreamCallbacksAndOptions {
581
+ /** `onStart`: Called once when the stream is initialized. */
582
+ onStart?: () => Promise<void> | void;
583
+ /** `onCompletion`: Called for each tokenized message. */
584
+ onCompletion?: (completion: string) => Promise<void> | void;
585
+ /** `onFinal`: Called once when the stream is closed with the final completion message. */
586
+ onFinal?: (completion: string) => Promise<void> | void;
587
+ /** `onToken`: Called for each tokenized message. */
588
+ onToken?: (token: string) => Promise<void> | void;
589
+ /** `onText`: Called for each text chunk. */
590
+ onText?: (text: string) => Promise<void> | void;
591
+ /**
592
+ * A flag for enabling the experimental_StreamData class and the new protocol.
593
+ * @see https://github.com/vercel-labs/ai/pull/425
594
+ *
595
+ * When StreamData is rolled out, this will be removed and the new protocol will be used by default.
596
+ */
597
+ experimental_streamData?: boolean;
497
598
  }
498
599
 
499
600
  /**
500
601
  * Stream text generated by a language model.
501
602
  */
502
- declare function streamText<TOOLS extends Record<string, Tool>>({ model, tools, system, prompt, messages, ...settings }: CallSettings & Prompt & {
603
+ declare function streamText<TOOLS extends Record<string, Tool>>({ model, tools, system, prompt, messages, maxRetries, abortSignal, ...settings }: CallSettings & Prompt & {
503
604
  model: LanguageModelV1;
504
605
  tools?: TOOLS;
505
606
  }): Promise<StreamTextResult<TOOLS>>;
@@ -515,11 +616,11 @@ type TextStreamPart<TOOLS extends Record<string, Tool>> = {
515
616
  type: 'tool-result';
516
617
  } & ToToolResult<TOOLS>);
517
618
  declare class StreamTextResult<TOOLS extends Record<string, Tool>> {
518
- private readonly rootStream;
519
- readonly textStream: AsyncIterable<string>;
520
- readonly fullStream: AsyncIterable<TextStreamPart<TOOLS>>;
619
+ private readonly originalStream;
521
620
  constructor(stream: ReadableStream<TextStreamPart<TOOLS>>);
522
- toResponse(): StreamTextHttpResponse;
621
+ get textStream(): AsyncIterableStream<string>;
622
+ get fullStream(): AsyncIterableStream<TextStreamPart<TOOLS>>;
623
+ toAIStream(callbacks?: AIStreamCallbacksAndOptions): ReadableStream<any>;
523
624
  }
524
625
 
525
- export { AssistantContent, AssistantMessage, DataContent, ErrorStreamPart, GenerateObjectResult, GenerateTextResult, ImagePart, Message, StreamObjectResult, StreamTextHttpResponse, StreamTextResult, TextPart, TextStreamPart, Tool, ToolCallPart, ToolContent, ToolMessage, ToolResultPart, UserContent, UserMessage, convertDataContentToBase64String, convertDataContentToUint8Array, generateObject, generateText, streamObject, streamText, tool };
626
+ export { AssistantContent, AssistantMessage, DataContent, ErrorStreamPart, GenerateObjectResult, GenerateTextResult, ImagePart, Message, StreamObjectResult, StreamTextResult, TextPart, TextStreamPart, Tool, ToolCallPart, ToolContent, ToolMessage, ToolResultPart, UserContent, UserMessage, convertDataContentToBase64String, convertDataContentToUint8Array, generateObject, generateText, streamObject, streamText, tool };