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.
@@ -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.string().optional(),
16
- providerMetadata: providerMetadataSchema.optional(),
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.string().optional(),
25
- providerMetadata: providerMetadataSchema.optional(),
24
+ filename: z.optional(z.string()),
25
+ providerMetadata: z.optional(providerMetadataSchema),
26
26
  }),
27
27
  ]),
28
- startIndex: z.number().int().nonnegative().optional(),
29
- endIndex: z.number().int().nonnegative().optional(),
30
- citedText: z.string().optional(),
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
  });
@@ -1,5 +1,5 @@
1
1
  import type { JSONValue as OriginalJSONValue } from '@ai-sdk/provider';
2
- import { z, type ZodType } from '../util/zod';
2
+ import { z, type ZodType } from '../util/classic-zod';
3
3
 
4
4
  export const jsonValueSchema: ZodType<JSONValue> = z.lazy(() =>
5
5
  z.union([
@@ -1,5 +1,5 @@
1
1
  import type { SharedV4ProviderMetadata } from '@ai-sdk/provider';
2
- import { z, type ZodType } from '../util/zod';
2
+ import { z, type ZodType } from '../util/classic-zod';
3
3
  import { jsonValueSchema } from './json-value';
4
4
 
5
5
  /**
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
- await this.resumeQueue({ skipWaitForUser: true });
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 (!this.isRequestInProgress() && this.hasUnsentApprovalResponse()) {
446
- await this.sendMessage();
447
- return;
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({ skipWaitForUser: this.isQueuePaused });
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 result = await this.sendAutomaticallyWhen({
1060
- messages: this.state.messages,
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) {