ai 7.0.168 → 7.0.170
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/CHANGELOG.md +22 -0
- package/dist/index.d.ts +9 -6
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +703 -525
- package/dist/index.js.map +1 -1
- package/dist/internal/index.d.ts +1 -0
- package/dist/internal/index.d.ts.map +1 -1
- package/dist/internal/index.js +1 -1
- package/docs/02-foundations/02-providers-and-models.mdx +1 -0
- package/docs/03-ai-sdk-core/15-tools-and-tool-calling.mdx +9 -0
- package/docs/03-ai-sdk-core/32-decisions.mdx +14 -10
- package/docs/07-reference/02-ai-sdk-ui/32-prune-messages.mdx +9 -0
- package/package.json +12 -12
- package/src/generate-object/output-strategy.ts +8 -0
- package/src/generate-object/stream-object.ts +22 -0
- package/src/generate-text/output.ts +22 -28
- package/src/generate-text/stream-text.ts +23 -0
- package/src/prompt/content-part.ts +1 -1
- package/src/prompt/message.ts +1 -1
- package/src/realtime/validate-realtime-setup.ts +23 -19
- package/src/types/citation.ts +7 -7
- package/src/types/json-value.ts +1 -1
- package/src/types/provider-metadata.ts +1 -1
- package/src/ui/chat.ts +64 -13
- package/src/ui/validate-ui-messages.ts +157 -155
- package/src/ui-message-stream/ui-message-chunks.ts +66 -63
- package/src/util/array-output-stream.ts +161 -0
- package/src/util/classic-zod.ts +37 -0
- package/src/util/zod.ts +25 -10
package/src/types/citation.ts
CHANGED
|
@@ -12,8 +12,8 @@ export const citationSchema: ZodType<Citation> = z.object({
|
|
|
12
12
|
sourceType: z.literal('url'),
|
|
13
13
|
id: z.string(),
|
|
14
14
|
url: z.string(),
|
|
15
|
-
title: z.
|
|
16
|
-
providerMetadata:
|
|
15
|
+
title: z.optional(z.string()),
|
|
16
|
+
providerMetadata: z.optional(providerMetadataSchema),
|
|
17
17
|
}),
|
|
18
18
|
z.object({
|
|
19
19
|
type: z.literal('source'),
|
|
@@ -21,11 +21,11 @@ export const citationSchema: ZodType<Citation> = z.object({
|
|
|
21
21
|
id: z.string(),
|
|
22
22
|
mediaType: z.string(),
|
|
23
23
|
title: z.string(),
|
|
24
|
-
filename: z.
|
|
25
|
-
providerMetadata:
|
|
24
|
+
filename: z.optional(z.string()),
|
|
25
|
+
providerMetadata: z.optional(providerMetadataSchema),
|
|
26
26
|
}),
|
|
27
27
|
]),
|
|
28
|
-
startIndex: z.number().int().
|
|
29
|
-
endIndex: z.number().int().
|
|
30
|
-
citedText: z.
|
|
28
|
+
startIndex: z.optional(z.number().check(z.int()).check(z.nonnegative())),
|
|
29
|
+
endIndex: z.optional(z.number().check(z.int()).check(z.nonnegative())),
|
|
30
|
+
citedText: z.optional(z.string()),
|
|
31
31
|
});
|
package/src/types/json-value.ts
CHANGED
package/src/ui/chat.ts
CHANGED
|
@@ -295,6 +295,10 @@ export abstract class AbstractChat<UI_MESSAGE extends UIMessage> {
|
|
|
295
295
|
// a stop or an error pauses the queue. a paused response is not waiting for
|
|
296
296
|
// the user, so resuming the queue skips that check:
|
|
297
297
|
private isQueuePaused = false;
|
|
298
|
+
private pendingAutomaticDecisions: number[] = [];
|
|
299
|
+
// a user send that has to wait to send an answered approval first, with the
|
|
300
|
+
// request options of that send:
|
|
301
|
+
private deferredApprovalSend: { options?: ChatRequestOptions } | undefined;
|
|
298
302
|
|
|
299
303
|
private pendingMessagePreparations = new Set<AbortController>();
|
|
300
304
|
private pendingApprovalMessageId: string | undefined;
|
|
@@ -428,8 +432,17 @@ export abstract class AbstractChat<UI_MESSAGE extends UIMessage> {
|
|
|
428
432
|
...this.experimental_queuedMessages.filter(queued => queued.id !== id),
|
|
429
433
|
]);
|
|
430
434
|
|
|
435
|
+
const stopGeneration = this.stopGeneration + 1;
|
|
431
436
|
await this.stop();
|
|
432
|
-
|
|
437
|
+
|
|
438
|
+
if (stopGeneration !== this.stopGeneration) {
|
|
439
|
+
return;
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
await this.resumeQueue({
|
|
443
|
+
skipWaitForUser: true,
|
|
444
|
+
options: this.queuedMessageOptions.get(id),
|
|
445
|
+
});
|
|
433
446
|
};
|
|
434
447
|
|
|
435
448
|
// the user sending a message resumes a paused queue (after a stop, an error
|
|
@@ -437,14 +450,22 @@ export abstract class AbstractChat<UI_MESSAGE extends UIMessage> {
|
|
|
437
450
|
// first, the queue continues when its response finishes:
|
|
438
451
|
private async resumeQueue({
|
|
439
452
|
skipWaitForUser,
|
|
453
|
+
options,
|
|
440
454
|
}: {
|
|
441
455
|
skipWaitForUser: boolean;
|
|
456
|
+
options?: ChatRequestOptions;
|
|
442
457
|
}): Promise<void> {
|
|
443
458
|
this.isQueuePaused = false;
|
|
444
459
|
|
|
445
|
-
if (
|
|
446
|
-
|
|
447
|
-
|
|
460
|
+
if (this.hasUnsentApprovalResponse()) {
|
|
461
|
+
if (!this.isRequestInProgress()) {
|
|
462
|
+
await this.sendMessage(undefined, options);
|
|
463
|
+
return;
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
// a pending sendAutomaticallyWhen decision can still send the approval.
|
|
467
|
+
// if it does not, the queue sends it first when it settles:
|
|
468
|
+
this.deferredApprovalSend = { options };
|
|
448
469
|
}
|
|
449
470
|
|
|
450
471
|
await this.settleQueue({ skipWaitForUser });
|
|
@@ -460,14 +481,19 @@ export abstract class AbstractChat<UI_MESSAGE extends UIMessage> {
|
|
|
460
481
|
|
|
461
482
|
this.queuedMessageOptions.clear();
|
|
462
483
|
this.setQueuedMessages([]);
|
|
484
|
+
this.deferredApprovalSend = undefined;
|
|
463
485
|
|
|
464
486
|
if (this.isRequestInProgress()) {
|
|
465
487
|
await this.stop();
|
|
466
488
|
}
|
|
467
489
|
}
|
|
468
490
|
|
|
491
|
+
// a pending sendAutomaticallyWhen decision can still start a request, so the
|
|
492
|
+
// queue waits for it like for a response in progress. a stop invalidates the
|
|
493
|
+
// decisions that were pending, so they no longer hold the queue:
|
|
469
494
|
private isRequestInProgress(): boolean {
|
|
470
495
|
return (
|
|
496
|
+
this.pendingAutomaticDecisions.includes(this.stopGeneration) ||
|
|
471
497
|
this.activeResponse != null ||
|
|
472
498
|
this.status === 'submitted' ||
|
|
473
499
|
this.status === 'streaming'
|
|
@@ -525,19 +551,28 @@ export abstract class AbstractChat<UI_MESSAGE extends UIMessage> {
|
|
|
525
551
|
stopGeneration?: number;
|
|
526
552
|
skipWaitForUser?: boolean;
|
|
527
553
|
} = {}): Promise<void> {
|
|
528
|
-
const [message, ...rest] = this.experimental_queuedMessages;
|
|
529
|
-
|
|
530
554
|
if (
|
|
531
|
-
message == null ||
|
|
532
555
|
this.isQueuePaused ||
|
|
533
556
|
this.isRequestInProgress() ||
|
|
534
557
|
this.activeStopCount > 0 ||
|
|
535
|
-
stopGeneration !== this.stopGeneration
|
|
536
|
-
(!skipWaitForUser && this.isWaitingForUser())
|
|
558
|
+
stopGeneration !== this.stopGeneration
|
|
537
559
|
) {
|
|
538
560
|
return;
|
|
539
561
|
}
|
|
540
562
|
|
|
563
|
+
const deferredApprovalSend = this.deferredApprovalSend;
|
|
564
|
+
this.deferredApprovalSend = undefined;
|
|
565
|
+
if (deferredApprovalSend != null && this.hasUnsentApprovalResponse()) {
|
|
566
|
+
await this.sendMessage(undefined, deferredApprovalSend.options);
|
|
567
|
+
return;
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
const [message, ...rest] = this.experimental_queuedMessages;
|
|
571
|
+
|
|
572
|
+
if (message == null || (!skipWaitForUser && this.isWaitingForUser())) {
|
|
573
|
+
return;
|
|
574
|
+
}
|
|
575
|
+
|
|
541
576
|
const options = this.queuedMessageOptions.get(message.id);
|
|
542
577
|
this.queuedMessageOptions.delete(message.id);
|
|
543
578
|
this.setQueuedMessages(rest);
|
|
@@ -671,7 +706,10 @@ export abstract class AbstractChat<UI_MESSAGE extends UIMessage> {
|
|
|
671
706
|
|
|
672
707
|
// a stopped response is not waiting for the user anymore, so resuming a
|
|
673
708
|
// paused queue skips that check:
|
|
674
|
-
await this.resumeQueue({
|
|
709
|
+
await this.resumeQueue({
|
|
710
|
+
skipWaitForUser: this.isQueuePaused,
|
|
711
|
+
options,
|
|
712
|
+
});
|
|
675
713
|
return;
|
|
676
714
|
}
|
|
677
715
|
|
|
@@ -1006,6 +1044,7 @@ export abstract class AbstractChat<UI_MESSAGE extends UIMessage> {
|
|
|
1006
1044
|
stop = async () => {
|
|
1007
1045
|
this.activeStopCount++;
|
|
1008
1046
|
this.stopGeneration++;
|
|
1047
|
+
this.deferredApprovalSend = undefined;
|
|
1009
1048
|
|
|
1010
1049
|
try {
|
|
1011
1050
|
const activeResumeRequest = this.activeResumeRequest;
|
|
@@ -1056,9 +1095,19 @@ export abstract class AbstractChat<UI_MESSAGE extends UIMessage> {
|
|
|
1056
1095
|
return false;
|
|
1057
1096
|
}
|
|
1058
1097
|
|
|
1059
|
-
const
|
|
1060
|
-
|
|
1061
|
-
|
|
1098
|
+
const stopGeneration = this.stopGeneration;
|
|
1099
|
+
this.pendingAutomaticDecisions.push(stopGeneration);
|
|
1100
|
+
let result: boolean;
|
|
1101
|
+
try {
|
|
1102
|
+
result = await this.sendAutomaticallyWhen({
|
|
1103
|
+
messages: this.state.messages,
|
|
1104
|
+
});
|
|
1105
|
+
} finally {
|
|
1106
|
+
this.pendingAutomaticDecisions.splice(
|
|
1107
|
+
this.pendingAutomaticDecisions.indexOf(stopGeneration),
|
|
1108
|
+
1,
|
|
1109
|
+
);
|
|
1110
|
+
}
|
|
1062
1111
|
|
|
1063
1112
|
return (
|
|
1064
1113
|
result &&
|
|
@@ -1100,6 +1149,8 @@ export abstract class AbstractChat<UI_MESSAGE extends UIMessage> {
|
|
|
1100
1149
|
messageId?: string;
|
|
1101
1150
|
messageIndex: number;
|
|
1102
1151
|
} & ChatRequestOptions) {
|
|
1152
|
+
this.deferredApprovalSend = undefined;
|
|
1153
|
+
|
|
1103
1154
|
const consumesPendingApproval =
|
|
1104
1155
|
messageId != null && messageId === this.pendingApprovalMessageId;
|
|
1105
1156
|
if (consumesPendingApproval) {
|