ai 3.1.6 → 3.1.7

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.
@@ -498,6 +498,13 @@ var F2 = (t, e) => c.useSWR(t, e);
498
498
  // svelte/use-chat.ts
499
499
  import { derived, get, writable } from "svelte/store";
500
500
 
501
+ // shared/generate-id.ts
502
+ import { customAlphabet } from "nanoid/non-secure";
503
+ var generateId = customAlphabet(
504
+ "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
505
+ 7
506
+ );
507
+
501
508
  // shared/stream-parts.ts
502
509
  var textStreamPart = {
503
510
  code: "0",
@@ -594,7 +601,7 @@ var dataMessageStreamPart = {
594
601
  };
595
602
  }
596
603
  };
597
- var toolCallStreamPart = {
604
+ var toolCallsStreamPart = {
598
605
  code: "7",
599
606
  name: "tool_calls",
600
607
  parse: (value) => {
@@ -621,6 +628,36 @@ var messageAnnotationsStreamPart = {
621
628
  return { type: "message_annotations", value };
622
629
  }
623
630
  };
631
+ var toolCallStreamPart = {
632
+ code: "9",
633
+ name: "tool_call",
634
+ parse: (value) => {
635
+ if (value == null || typeof value !== "object" || !("toolCallId" in value) || typeof value.toolCallId !== "string" || !("toolName" in value) || typeof value.toolName !== "string" || !("args" in value) || typeof value.args !== "object") {
636
+ throw new Error(
637
+ '"tool_call" parts expect an object with a "toolCallId", "toolName", and "args" property.'
638
+ );
639
+ }
640
+ return {
641
+ type: "tool_call",
642
+ value
643
+ };
644
+ }
645
+ };
646
+ var toolResultStreamPart = {
647
+ code: "a",
648
+ name: "tool_result",
649
+ parse: (value) => {
650
+ if (value == null || typeof value !== "object" || !("toolCallId" in value) || typeof value.toolCallId !== "string" || !("toolName" in value) || typeof value.toolName !== "string" || !("args" in value) || typeof value.args !== "object" || !("result" in value)) {
651
+ throw new Error(
652
+ '"tool_result" parts expect an object with a "toolCallId", "toolName", "args", and "result" property.'
653
+ );
654
+ }
655
+ return {
656
+ type: "tool_result",
657
+ value
658
+ };
659
+ }
660
+ };
624
661
  var streamParts = [
625
662
  textStreamPart,
626
663
  functionCallStreamPart,
@@ -629,8 +666,10 @@ var streamParts = [
629
666
  assistantMessageStreamPart,
630
667
  assistantControlDataStreamPart,
631
668
  dataMessageStreamPart,
669
+ toolCallsStreamPart,
670
+ messageAnnotationsStreamPart,
632
671
  toolCallStreamPart,
633
- messageAnnotationsStreamPart
672
+ toolResultStreamPart
634
673
  ];
635
674
  var streamPartsByCode = {
636
675
  [textStreamPart.code]: textStreamPart,
@@ -640,8 +679,10 @@ var streamPartsByCode = {
640
679
  [assistantMessageStreamPart.code]: assistantMessageStreamPart,
641
680
  [assistantControlDataStreamPart.code]: assistantControlDataStreamPart,
642
681
  [dataMessageStreamPart.code]: dataMessageStreamPart,
682
+ [toolCallsStreamPart.code]: toolCallsStreamPart,
683
+ [messageAnnotationsStreamPart.code]: messageAnnotationsStreamPart,
643
684
  [toolCallStreamPart.code]: toolCallStreamPart,
644
- [messageAnnotationsStreamPart.code]: messageAnnotationsStreamPart
685
+ [toolResultStreamPart.code]: toolResultStreamPart
645
686
  };
646
687
  var StreamStringPrefixes = {
647
688
  [textStreamPart.name]: textStreamPart.code,
@@ -651,8 +692,10 @@ var StreamStringPrefixes = {
651
692
  [assistantMessageStreamPart.name]: assistantMessageStreamPart.code,
652
693
  [assistantControlDataStreamPart.name]: assistantControlDataStreamPart.code,
653
694
  [dataMessageStreamPart.name]: dataMessageStreamPart.code,
695
+ [toolCallsStreamPart.name]: toolCallsStreamPart.code,
696
+ [messageAnnotationsStreamPart.name]: messageAnnotationsStreamPart.code,
654
697
  [toolCallStreamPart.name]: toolCallStreamPart.code,
655
- [messageAnnotationsStreamPart.name]: messageAnnotationsStreamPart.code
698
+ [toolResultStreamPart.name]: toolResultStreamPart.code
656
699
  };
657
700
  var validCodes = streamParts.map((part) => part.code);
658
701
  var parseStreamPart = (line) => {
@@ -713,13 +756,6 @@ async function* readDataStream(reader, {
713
756
  }
714
757
  }
715
758
 
716
- // shared/generate-id.ts
717
- import { customAlphabet } from "nanoid/non-secure";
718
- var generateId = customAlphabet(
719
- "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
720
- 7
721
- );
722
-
723
759
  // shared/parse-complex-response.ts
724
760
  function assignAnnotationsToMessage(message, annotations) {
725
761
  if (!message || !annotations || !annotations.length)
@@ -757,6 +793,40 @@ async function parseComplexResponse({
757
793
  };
758
794
  }
759
795
  }
796
+ if (type === "tool_call") {
797
+ if (prefixMap.text == null) {
798
+ prefixMap.text = {
799
+ id: generateId2(),
800
+ role: "assistant",
801
+ content: "",
802
+ createdAt
803
+ };
804
+ }
805
+ if (prefixMap.text.toolInvocations == null) {
806
+ prefixMap.text.toolInvocations = [];
807
+ }
808
+ prefixMap.text.toolInvocations.push(value);
809
+ } else if (type === "tool_result") {
810
+ if (prefixMap.text == null) {
811
+ prefixMap.text = {
812
+ id: generateId2(),
813
+ role: "assistant",
814
+ content: "",
815
+ createdAt
816
+ };
817
+ }
818
+ if (prefixMap.text.toolInvocations == null) {
819
+ prefixMap.text.toolInvocations = [];
820
+ }
821
+ const toolInvocationIndex = prefixMap.text.toolInvocations.findIndex(
822
+ (invocation) => invocation.toolCallId === value.toolCallId
823
+ );
824
+ if (toolInvocationIndex !== -1) {
825
+ prefixMap.text.toolInvocations[toolInvocationIndex] = value;
826
+ } else {
827
+ prefixMap.text.toolInvocations.push(value);
828
+ }
829
+ }
760
830
  let functionCallMessage = null;
761
831
  if (type === "function_call") {
762
832
  prefixMap["function_call"] = {