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.
@@ -525,6 +525,13 @@ var F2 = (t, e) => c.useSWR(t, e);
525
525
  // svelte/use-chat.ts
526
526
  var import_store = require("svelte/store");
527
527
 
528
+ // shared/generate-id.ts
529
+ var import_non_secure = require("nanoid/non-secure");
530
+ var generateId = (0, import_non_secure.customAlphabet)(
531
+ "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
532
+ 7
533
+ );
534
+
528
535
  // shared/stream-parts.ts
529
536
  var textStreamPart = {
530
537
  code: "0",
@@ -621,7 +628,7 @@ var dataMessageStreamPart = {
621
628
  };
622
629
  }
623
630
  };
624
- var toolCallStreamPart = {
631
+ var toolCallsStreamPart = {
625
632
  code: "7",
626
633
  name: "tool_calls",
627
634
  parse: (value) => {
@@ -648,6 +655,36 @@ var messageAnnotationsStreamPart = {
648
655
  return { type: "message_annotations", value };
649
656
  }
650
657
  };
658
+ var toolCallStreamPart = {
659
+ code: "9",
660
+ name: "tool_call",
661
+ parse: (value) => {
662
+ 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") {
663
+ throw new Error(
664
+ '"tool_call" parts expect an object with a "toolCallId", "toolName", and "args" property.'
665
+ );
666
+ }
667
+ return {
668
+ type: "tool_call",
669
+ value
670
+ };
671
+ }
672
+ };
673
+ var toolResultStreamPart = {
674
+ code: "a",
675
+ name: "tool_result",
676
+ parse: (value) => {
677
+ 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)) {
678
+ throw new Error(
679
+ '"tool_result" parts expect an object with a "toolCallId", "toolName", "args", and "result" property.'
680
+ );
681
+ }
682
+ return {
683
+ type: "tool_result",
684
+ value
685
+ };
686
+ }
687
+ };
651
688
  var streamParts = [
652
689
  textStreamPart,
653
690
  functionCallStreamPart,
@@ -656,8 +693,10 @@ var streamParts = [
656
693
  assistantMessageStreamPart,
657
694
  assistantControlDataStreamPart,
658
695
  dataMessageStreamPart,
696
+ toolCallsStreamPart,
697
+ messageAnnotationsStreamPart,
659
698
  toolCallStreamPart,
660
- messageAnnotationsStreamPart
699
+ toolResultStreamPart
661
700
  ];
662
701
  var streamPartsByCode = {
663
702
  [textStreamPart.code]: textStreamPart,
@@ -667,8 +706,10 @@ var streamPartsByCode = {
667
706
  [assistantMessageStreamPart.code]: assistantMessageStreamPart,
668
707
  [assistantControlDataStreamPart.code]: assistantControlDataStreamPart,
669
708
  [dataMessageStreamPart.code]: dataMessageStreamPart,
709
+ [toolCallsStreamPart.code]: toolCallsStreamPart,
710
+ [messageAnnotationsStreamPart.code]: messageAnnotationsStreamPart,
670
711
  [toolCallStreamPart.code]: toolCallStreamPart,
671
- [messageAnnotationsStreamPart.code]: messageAnnotationsStreamPart
712
+ [toolResultStreamPart.code]: toolResultStreamPart
672
713
  };
673
714
  var StreamStringPrefixes = {
674
715
  [textStreamPart.name]: textStreamPart.code,
@@ -678,8 +719,10 @@ var StreamStringPrefixes = {
678
719
  [assistantMessageStreamPart.name]: assistantMessageStreamPart.code,
679
720
  [assistantControlDataStreamPart.name]: assistantControlDataStreamPart.code,
680
721
  [dataMessageStreamPart.name]: dataMessageStreamPart.code,
722
+ [toolCallsStreamPart.name]: toolCallsStreamPart.code,
723
+ [messageAnnotationsStreamPart.name]: messageAnnotationsStreamPart.code,
681
724
  [toolCallStreamPart.name]: toolCallStreamPart.code,
682
- [messageAnnotationsStreamPart.name]: messageAnnotationsStreamPart.code
725
+ [toolResultStreamPart.name]: toolResultStreamPart.code
683
726
  };
684
727
  var validCodes = streamParts.map((part) => part.code);
685
728
  var parseStreamPart = (line) => {
@@ -740,13 +783,6 @@ async function* readDataStream(reader, {
740
783
  }
741
784
  }
742
785
 
743
- // shared/generate-id.ts
744
- var import_non_secure = require("nanoid/non-secure");
745
- var generateId = (0, import_non_secure.customAlphabet)(
746
- "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
747
- 7
748
- );
749
-
750
786
  // shared/parse-complex-response.ts
751
787
  function assignAnnotationsToMessage(message, annotations) {
752
788
  if (!message || !annotations || !annotations.length)
@@ -784,6 +820,40 @@ async function parseComplexResponse({
784
820
  };
785
821
  }
786
822
  }
823
+ if (type === "tool_call") {
824
+ if (prefixMap.text == null) {
825
+ prefixMap.text = {
826
+ id: generateId2(),
827
+ role: "assistant",
828
+ content: "",
829
+ createdAt
830
+ };
831
+ }
832
+ if (prefixMap.text.toolInvocations == null) {
833
+ prefixMap.text.toolInvocations = [];
834
+ }
835
+ prefixMap.text.toolInvocations.push(value);
836
+ } else if (type === "tool_result") {
837
+ if (prefixMap.text == null) {
838
+ prefixMap.text = {
839
+ id: generateId2(),
840
+ role: "assistant",
841
+ content: "",
842
+ createdAt
843
+ };
844
+ }
845
+ if (prefixMap.text.toolInvocations == null) {
846
+ prefixMap.text.toolInvocations = [];
847
+ }
848
+ const toolInvocationIndex = prefixMap.text.toolInvocations.findIndex(
849
+ (invocation) => invocation.toolCallId === value.toolCallId
850
+ );
851
+ if (toolInvocationIndex !== -1) {
852
+ prefixMap.text.toolInvocations[toolInvocationIndex] = value;
853
+ } else {
854
+ prefixMap.text.toolInvocations.push(value);
855
+ }
856
+ }
787
857
  let functionCallMessage = null;
788
858
  if (type === "function_call") {
789
859
  prefixMap["function_call"] = {