ai 2.2.29 → 2.2.30

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.
@@ -380,7 +380,7 @@ var H = class {
380
380
  }
381
381
  };
382
382
 
383
- // ../../node_modules/.pnpm/sswr@2.0.0_svelte@4.0.1/node_modules/sswr/dist/sswr.mjs
383
+ // ../../node_modules/.pnpm/sswr@2.0.0_svelte@4.2.3/node_modules/sswr/dist/sswr.mjs
384
384
  import { beforeUpdate as w, onDestroy as E2 } from "svelte";
385
385
  function p() {
386
386
  }
@@ -594,6 +594,23 @@ var dataMessageStreamPart = {
594
594
  };
595
595
  }
596
596
  };
597
+ var toolCallStreamPart = {
598
+ code: "7",
599
+ name: "tool_calls",
600
+ parse: (value) => {
601
+ if (value == null || typeof value !== "object" || !("tool_calls" in value) || typeof value.tool_calls !== "object" || value.tool_calls == null || !Array.isArray(value.tool_calls) || value.tool_calls.some((tc) => {
602
+ tc == null || typeof tc !== "object" || !("id" in tc) || typeof tc.id !== "string" || !("type" in tc) || typeof tc.type !== "string" || !("function" in tc) || tc.function == null || typeof tc.function !== "object" || !("arguments" in tc.function) || typeof tc.function.name !== "string" || typeof tc.function.arguments !== "string";
603
+ })) {
604
+ throw new Error(
605
+ '"tool_calls" parts expect an object with a ToolCallPayload.'
606
+ );
607
+ }
608
+ return {
609
+ type: "tool_calls",
610
+ value
611
+ };
612
+ }
613
+ };
597
614
  var streamParts = [
598
615
  textStreamPart,
599
616
  functionCallStreamPart,
@@ -601,7 +618,8 @@ var streamParts = [
601
618
  errorStreamPart,
602
619
  assistantMessageStreamPart,
603
620
  assistantControlDataStreamPart,
604
- dataMessageStreamPart
621
+ dataMessageStreamPart,
622
+ toolCallStreamPart
605
623
  ];
606
624
  var streamPartsByCode = {
607
625
  [textStreamPart.code]: textStreamPart,
@@ -610,7 +628,8 @@ var streamPartsByCode = {
610
628
  [errorStreamPart.code]: errorStreamPart,
611
629
  [assistantMessageStreamPart.code]: assistantMessageStreamPart,
612
630
  [assistantControlDataStreamPart.code]: assistantControlDataStreamPart,
613
- [dataMessageStreamPart.code]: dataMessageStreamPart
631
+ [dataMessageStreamPart.code]: dataMessageStreamPart,
632
+ [toolCallStreamPart.code]: toolCallStreamPart
614
633
  };
615
634
  var StreamStringPrefixes = {
616
635
  [textStreamPart.name]: textStreamPart.code,
@@ -619,7 +638,8 @@ var StreamStringPrefixes = {
619
638
  [errorStreamPart.name]: errorStreamPart.code,
620
639
  [assistantMessageStreamPart.name]: assistantMessageStreamPart.code,
621
640
  [assistantControlDataStreamPart.name]: assistantControlDataStreamPart.code,
622
- [dataMessageStreamPart.name]: dataMessageStreamPart.code
641
+ [dataMessageStreamPart.name]: dataMessageStreamPart.code,
642
+ [toolCallStreamPart.name]: toolCallStreamPart.code
623
643
  };
624
644
  var validCodes = streamParts.map((part) => part.code);
625
645
  var parseStreamPart = (line) => {
@@ -745,20 +765,35 @@ async function parseComplexResponse({
745
765
  };
746
766
  functionCallMessage = prefixMap["function_call"];
747
767
  }
768
+ let toolCallMessage = null;
769
+ if (type === "tool_calls") {
770
+ prefixMap["tool_calls"] = {
771
+ id: generateId(),
772
+ role: "assistant",
773
+ content: "",
774
+ tool_calls: value.tool_calls,
775
+ createdAt
776
+ };
777
+ toolCallMessage = prefixMap["tool_calls"];
778
+ }
748
779
  if (type === "data") {
749
780
  prefixMap["data"].push(...value);
750
781
  }
751
782
  const responseMessage = prefixMap["text"];
752
- const merged = [functionCallMessage, responseMessage].filter(
753
- Boolean
754
- );
783
+ const merged = [
784
+ functionCallMessage,
785
+ toolCallMessage,
786
+ responseMessage
787
+ ].filter(Boolean);
755
788
  update(merged, [...prefixMap["data"]]);
756
789
  }
757
790
  onFinish == null ? void 0 : onFinish(prefixMap);
758
791
  return {
759
- messages: [prefixMap.text, prefixMap.function_call].filter(
760
- Boolean
761
- ),
792
+ messages: [
793
+ prefixMap.text,
794
+ prefixMap.function_call,
795
+ prefixMap.tool_calls
796
+ ].filter(Boolean),
762
797
  data: prefixMap.data
763
798
  };
764
799
  }
@@ -844,6 +879,8 @@ async function callChatApi({
844
879
  streamedResponse += decode(value);
845
880
  if (streamedResponse.startsWith('{"function_call":')) {
846
881
  responseMessage["function_call"] = streamedResponse;
882
+ } else if (streamedResponse.startsWith('{"tool_calls":')) {
883
+ responseMessage["tool_calls"] = streamedResponse;
847
884
  } else {
848
885
  responseMessage["content"] = streamedResponse;
849
886
  }
@@ -858,6 +895,11 @@ async function callChatApi({
858
895
  responseMessage["function_call"] = parsedFunctionCall;
859
896
  appendMessage({ ...responseMessage });
860
897
  }
898
+ if (streamedResponse.startsWith('{"tool_calls":')) {
899
+ const parsedToolCalls = JSON.parse(streamedResponse).tool_calls;
900
+ responseMessage["tool_calls"] = parsedToolCalls;
901
+ appendMessage({ ...responseMessage });
902
+ }
861
903
  if (onFinish) {
862
904
  onFinish(responseMessage);
863
905
  }
@@ -869,6 +911,7 @@ async function callChatApi({
869
911
  async function processChatStream({
870
912
  getStreamedResponse: getStreamedResponse2,
871
913
  experimental_onFunctionCall,
914
+ experimental_onToolCall,
872
915
  updateChatRequest,
873
916
  getCurrentMessages
874
917
  }) {
@@ -877,12 +920,18 @@ async function processChatStream({
877
920
  if ("messages" in messagesAndDataOrJustMessage) {
878
921
  let hasFollowingResponse = false;
879
922
  for (const message of messagesAndDataOrJustMessage.messages) {
880
- if (message.function_call === void 0 || typeof message.function_call === "string") {
923
+ if ((message.function_call === void 0 || typeof message.function_call === "string") && (message.tool_calls === void 0 || typeof message.tool_calls === "string")) {
881
924
  continue;
882
925
  }
883
926
  hasFollowingResponse = true;
884
927
  if (experimental_onFunctionCall) {
885
928
  const functionCall = message.function_call;
929
+ if (typeof functionCall !== "object") {
930
+ console.warn(
931
+ "experimental_onFunctionCall should not be defined when using tools"
932
+ );
933
+ continue;
934
+ }
886
935
  const functionCallResponse = await experimental_onFunctionCall(
887
936
  getCurrentMessages(),
888
937
  functionCall
@@ -893,22 +942,83 @@ async function processChatStream({
893
942
  }
894
943
  updateChatRequest(functionCallResponse);
895
944
  }
945
+ if (experimental_onToolCall) {
946
+ const toolCalls = message.tool_calls;
947
+ if (!Array.isArray(toolCalls) || toolCalls.some((toolCall) => typeof toolCall !== "object")) {
948
+ console.warn(
949
+ "experimental_onToolCall should not be defined when using tools"
950
+ );
951
+ continue;
952
+ }
953
+ const toolCallResponse = await experimental_onToolCall(getCurrentMessages(), toolCalls);
954
+ if (toolCallResponse === void 0) {
955
+ hasFollowingResponse = false;
956
+ break;
957
+ }
958
+ updateChatRequest(toolCallResponse);
959
+ }
896
960
  }
897
961
  if (!hasFollowingResponse) {
898
962
  break;
899
963
  }
900
964
  } else {
965
+ let fixFunctionCallArguments2 = function(response) {
966
+ for (const message of response.messages) {
967
+ if (message.tool_calls !== void 0) {
968
+ for (const toolCall of message.tool_calls) {
969
+ if (typeof toolCall === "object") {
970
+ if (toolCall.function.arguments && typeof toolCall.function.arguments !== "string") {
971
+ toolCall.function.arguments = JSON.stringify(
972
+ toolCall.function.arguments
973
+ );
974
+ }
975
+ }
976
+ }
977
+ }
978
+ if (message.function_call !== void 0) {
979
+ if (typeof message.function_call === "object") {
980
+ if (message.function_call.arguments && typeof message.function_call.arguments !== "string") {
981
+ message.function_call.arguments = JSON.stringify(
982
+ message.function_call.arguments
983
+ );
984
+ }
985
+ }
986
+ }
987
+ }
988
+ };
989
+ var fixFunctionCallArguments = fixFunctionCallArguments2;
901
990
  const streamedResponseMessage = messagesAndDataOrJustMessage;
902
- if (streamedResponseMessage.function_call === void 0 || typeof streamedResponseMessage.function_call === "string") {
991
+ if ((streamedResponseMessage.function_call === void 0 || typeof streamedResponseMessage.function_call === "string") && (streamedResponseMessage.tool_calls === void 0 || typeof streamedResponseMessage.tool_calls === "string")) {
903
992
  break;
904
993
  }
905
994
  if (experimental_onFunctionCall) {
906
995
  const functionCall = streamedResponseMessage.function_call;
996
+ if (!(typeof functionCall === "object")) {
997
+ console.warn(
998
+ "experimental_onFunctionCall should not be defined when using tools"
999
+ );
1000
+ continue;
1001
+ }
907
1002
  const functionCallResponse = await experimental_onFunctionCall(getCurrentMessages(), functionCall);
908
1003
  if (functionCallResponse === void 0)
909
1004
  break;
1005
+ fixFunctionCallArguments2(functionCallResponse);
910
1006
  updateChatRequest(functionCallResponse);
911
1007
  }
1008
+ if (experimental_onToolCall) {
1009
+ const toolCalls = streamedResponseMessage.tool_calls;
1010
+ if (!(typeof toolCalls === "object")) {
1011
+ console.warn(
1012
+ "experimental_onToolCall should not be defined when using functions"
1013
+ );
1014
+ continue;
1015
+ }
1016
+ const toolCallResponse = await experimental_onToolCall(getCurrentMessages(), toolCalls);
1017
+ if (toolCallResponse === void 0)
1018
+ break;
1019
+ fixFunctionCallArguments2(toolCallResponse);
1020
+ updateChatRequest(toolCallResponse);
1021
+ }
912
1022
  }
913
1023
  }
914
1024
  }
@@ -917,14 +1027,20 @@ async function processChatStream({
917
1027
  var getStreamedResponse = async (api, chatRequest, mutate, mutateStreamData, existingData, extraMetadata, previousMessages, abortControllerRef, generateId, onFinish, onResponse, sendExtraMessageFields) => {
918
1028
  var _a, _b;
919
1029
  mutate(chatRequest.messages);
920
- const constructedMessagesPayload = sendExtraMessageFields ? chatRequest.messages : chatRequest.messages.map(({ role, content, name, function_call }) => ({
921
- role,
922
- content,
923
- ...name !== void 0 && { name },
924
- ...function_call !== void 0 && {
925
- function_call
926
- }
927
- }));
1030
+ const constructedMessagesPayload = sendExtraMessageFields ? chatRequest.messages : chatRequest.messages.map(
1031
+ ({ role, content, name, function_call, tool_calls, tool_call_id }) => ({
1032
+ role,
1033
+ content,
1034
+ tool_call_id,
1035
+ ...name !== void 0 && { name },
1036
+ ...function_call !== void 0 && {
1037
+ function_call
1038
+ },
1039
+ ...tool_calls !== void 0 && {
1040
+ tool_calls
1041
+ }
1042
+ })
1043
+ );
928
1044
  return await callChatApi({
929
1045
  api,
930
1046
  messages: constructedMessagesPayload,
@@ -936,6 +1052,12 @@ var getStreamedResponse = async (api, chatRequest, mutate, mutateStreamData, exi
936
1052
  },
937
1053
  ...chatRequest.function_call !== void 0 && {
938
1054
  function_call: chatRequest.function_call
1055
+ },
1056
+ ...chatRequest.tools !== void 0 && {
1057
+ tools: chatRequest.tools
1058
+ },
1059
+ ...chatRequest.tool_choice !== void 0 && {
1060
+ tool_choice: chatRequest.tool_choice
939
1061
  }
940
1062
  },
941
1063
  credentials: extraMetadata.credentials,
@@ -968,6 +1090,7 @@ function useChat({
968
1090
  initialInput = "",
969
1091
  sendExtraMessageFields,
970
1092
  experimental_onFunctionCall,
1093
+ experimental_onToolCall,
971
1094
  onResponse,
972
1095
  onFinish,
973
1096
  onError,
@@ -1024,6 +1147,7 @@ function useChat({
1024
1147
  sendExtraMessageFields
1025
1148
  ),
1026
1149
  experimental_onFunctionCall,
1150
+ experimental_onToolCall,
1027
1151
  updateChatRequest: (chatRequestParam) => {
1028
1152
  chatRequest = chatRequestParam;
1029
1153
  },
@@ -1044,7 +1168,13 @@ function useChat({
1044
1168
  loading.set(false);
1045
1169
  }
1046
1170
  }
1047
- const append = async (message, { options, functions, function_call } = {}) => {
1171
+ const append = async (message, {
1172
+ options,
1173
+ functions,
1174
+ function_call,
1175
+ tools,
1176
+ tool_choice
1177
+ } = {}) => {
1048
1178
  if (!message.id) {
1049
1179
  message.id = generateId();
1050
1180
  }
@@ -1052,14 +1182,18 @@ function useChat({
1052
1182
  messages: get(messages).concat(message),
1053
1183
  options,
1054
1184
  ...functions !== void 0 && { functions },
1055
- ...function_call !== void 0 && { function_call }
1185
+ ...function_call !== void 0 && { function_call },
1186
+ ...tools !== void 0 && { tools },
1187
+ ...tool_choice !== void 0 && { tool_choice }
1056
1188
  };
1057
1189
  return triggerRequest(chatRequest);
1058
1190
  };
1059
1191
  const reload = async ({
1060
1192
  options,
1061
1193
  functions,
1062
- function_call
1194
+ function_call,
1195
+ tools,
1196
+ tool_choice
1063
1197
  } = {}) => {
1064
1198
  const messagesSnapshot = get(messages);
1065
1199
  if (messagesSnapshot.length === 0)
@@ -1070,7 +1204,9 @@ function useChat({
1070
1204
  messages: messagesSnapshot.slice(0, -1),
1071
1205
  options,
1072
1206
  ...functions !== void 0 && { functions },
1073
- ...function_call !== void 0 && { function_call }
1207
+ ...function_call !== void 0 && { function_call },
1208
+ ...tools !== void 0 && { tools },
1209
+ ...tool_choice !== void 0 && { tool_choice }
1074
1210
  };
1075
1211
  return triggerRequest(chatRequest2);
1076
1212
  }
@@ -1078,7 +1214,9 @@ function useChat({
1078
1214
  messages: messagesSnapshot,
1079
1215
  options,
1080
1216
  ...functions !== void 0 && { functions },
1081
- ...function_call !== void 0 && { function_call }
1217
+ ...function_call !== void 0 && { function_call },
1218
+ ...tools !== void 0 && { tools },
1219
+ ...tool_choice !== void 0 && { tool_choice }
1082
1220
  };
1083
1221
  return triggerRequest(chatRequest);
1084
1222
  };