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.
@@ -13,6 +13,38 @@ interface FunctionCall {
13
13
  */
14
14
  name?: string;
15
15
  }
16
+ /**
17
+ * The tool calls generated by the model, such as function calls.
18
+ */
19
+ interface ToolCall {
20
+ id: string;
21
+ type: string;
22
+ function: {
23
+ name: string;
24
+ arguments: string;
25
+ };
26
+ }
27
+ /**
28
+ * Controls which (if any) function is called by the model.
29
+ * - none means the model will not call a function and instead generates a message.
30
+ * - auto means the model can pick between generating a message or calling a function.
31
+ * - Specifying a particular function via {"type: "function", "function": {"name": "my_function"}} forces the model to call that function.
32
+ * none is the default when no functions are present. auto is the default if functions are present.
33
+ */
34
+ type ToolChoice = 'none' | 'auto' | {
35
+ type: 'function';
36
+ function: {
37
+ name: string;
38
+ };
39
+ };
40
+ /**
41
+ * A list of tools the model may call. Currently, only functions are supported as a tool.
42
+ * Use this to provide a list of functions the model may generate JSON inputs for.
43
+ */
44
+ interface Tool {
45
+ type: 'function';
46
+ function: Function;
47
+ }
16
48
  interface Function {
17
49
  /**
18
50
  * The name of the function to be called. Must be a-z, A-Z, 0-9, or contain
@@ -41,10 +73,11 @@ type IdGenerator = () => string;
41
73
  */
42
74
  interface Message {
43
75
  id: string;
76
+ tool_call_id?: string;
44
77
  createdAt?: Date;
45
78
  content: string;
46
79
  ui?: string | JSX.Element | JSX.Element[] | null | undefined;
47
- role: 'system' | 'user' | 'assistant' | 'function' | 'data';
80
+ role: 'system' | 'user' | 'assistant' | 'function' | 'data' | 'tool';
48
81
  /**
49
82
  * If the message has a role of `function`, the `name` field is the name of the function.
50
83
  * Otherwise, the name field should not be set.
@@ -53,10 +86,15 @@ interface Message {
53
86
  /**
54
87
  * If the assistant role makes a function call, the `function_call` field
55
88
  * contains the function call name and arguments. Otherwise, the field should
56
- * not be set.
89
+ * not be set. (Deprecated and replaced by tool_calls.)
57
90
  */
58
91
  function_call?: string | FunctionCall;
59
92
  data?: JSONValue;
93
+ /**
94
+ * If the assistant role makes a tool call, the `tool_calls` field contains
95
+ * the tool call name and arguments. Otherwise, the field should not be set.
96
+ */
97
+ tool_calls?: string | ToolCall[];
60
98
  }
61
99
  type CreateMessage = Omit<Message, 'id'> & {
62
100
  id?: Message['id'];
@@ -67,8 +105,11 @@ type ChatRequest = {
67
105
  functions?: Array<Function>;
68
106
  function_call?: FunctionCall;
69
107
  data?: Record<string, string>;
108
+ tools?: Array<Tool>;
109
+ tool_choice?: ToolChoice;
70
110
  };
71
111
  type FunctionCallHandler = (chatMessages: Message[], functionCall: FunctionCall) => Promise<ChatRequest | void>;
112
+ type ToolCallHandler = (chatMessages: Message[], toolCalls: ToolCall[]) => Promise<ChatRequest | void>;
72
113
  type RequestOptions = {
73
114
  headers?: Record<string, string> | Headers;
74
115
  body?: object;
@@ -77,6 +118,8 @@ type ChatRequestOptions = {
77
118
  options?: RequestOptions;
78
119
  functions?: Array<Function>;
79
120
  function_call?: FunctionCall;
121
+ tools?: Array<Tool>;
122
+ tool_choice?: ToolChoice;
80
123
  data?: Record<string, string>;
81
124
  };
82
125
  type UseChatOptions = {
@@ -105,6 +148,12 @@ type UseChatOptions = {
105
148
  * automatically to the API and will be used to update the chat.
106
149
  */
107
150
  experimental_onFunctionCall?: FunctionCallHandler;
151
+ /**
152
+ * Callback function to be called when a tool call is received.
153
+ * If the function returns a `ChatRequest` object, the request will be sent
154
+ * automatically to the API and will be used to update the chat.
155
+ */
156
+ experimental_onToolCall?: ToolCallHandler;
108
157
  /**
109
158
  * Callback function to be called when the API response is received.
110
159
  */
@@ -250,7 +299,7 @@ type UseChatHelpers = {
250
299
  /** Additional data added on the server via StreamData */
251
300
  data: Readable<JSONValue[] | undefined>;
252
301
  };
253
- declare function useChat({ api, id, initialMessages, initialInput, sendExtraMessageFields, experimental_onFunctionCall, onResponse, onFinish, onError, credentials, headers, body, generateId, }?: UseChatOptions): UseChatHelpers;
302
+ declare function useChat({ api, id, initialMessages, initialInput, sendExtraMessageFields, experimental_onFunctionCall, experimental_onToolCall, onResponse, onFinish, onError, credentials, headers, body, generateId, }?: UseChatOptions): UseChatHelpers;
254
303
 
255
304
  type UseCompletionHelpers = {
256
305
  /** The current completion result */
@@ -407,7 +407,7 @@ var H = class {
407
407
  }
408
408
  };
409
409
 
410
- // ../../node_modules/.pnpm/sswr@2.0.0_svelte@4.0.1/node_modules/sswr/dist/sswr.mjs
410
+ // ../../node_modules/.pnpm/sswr@2.0.0_svelte@4.2.3/node_modules/sswr/dist/sswr.mjs
411
411
  var import_svelte = require("svelte");
412
412
  function p() {
413
413
  }
@@ -621,6 +621,23 @@ var dataMessageStreamPart = {
621
621
  };
622
622
  }
623
623
  };
624
+ var toolCallStreamPart = {
625
+ code: "7",
626
+ name: "tool_calls",
627
+ parse: (value) => {
628
+ 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) => {
629
+ 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";
630
+ })) {
631
+ throw new Error(
632
+ '"tool_calls" parts expect an object with a ToolCallPayload.'
633
+ );
634
+ }
635
+ return {
636
+ type: "tool_calls",
637
+ value
638
+ };
639
+ }
640
+ };
624
641
  var streamParts = [
625
642
  textStreamPart,
626
643
  functionCallStreamPart,
@@ -628,7 +645,8 @@ var streamParts = [
628
645
  errorStreamPart,
629
646
  assistantMessageStreamPart,
630
647
  assistantControlDataStreamPart,
631
- dataMessageStreamPart
648
+ dataMessageStreamPart,
649
+ toolCallStreamPart
632
650
  ];
633
651
  var streamPartsByCode = {
634
652
  [textStreamPart.code]: textStreamPart,
@@ -637,7 +655,8 @@ var streamPartsByCode = {
637
655
  [errorStreamPart.code]: errorStreamPart,
638
656
  [assistantMessageStreamPart.code]: assistantMessageStreamPart,
639
657
  [assistantControlDataStreamPart.code]: assistantControlDataStreamPart,
640
- [dataMessageStreamPart.code]: dataMessageStreamPart
658
+ [dataMessageStreamPart.code]: dataMessageStreamPart,
659
+ [toolCallStreamPart.code]: toolCallStreamPart
641
660
  };
642
661
  var StreamStringPrefixes = {
643
662
  [textStreamPart.name]: textStreamPart.code,
@@ -646,7 +665,8 @@ var StreamStringPrefixes = {
646
665
  [errorStreamPart.name]: errorStreamPart.code,
647
666
  [assistantMessageStreamPart.name]: assistantMessageStreamPart.code,
648
667
  [assistantControlDataStreamPart.name]: assistantControlDataStreamPart.code,
649
- [dataMessageStreamPart.name]: dataMessageStreamPart.code
668
+ [dataMessageStreamPart.name]: dataMessageStreamPart.code,
669
+ [toolCallStreamPart.name]: toolCallStreamPart.code
650
670
  };
651
671
  var validCodes = streamParts.map((part) => part.code);
652
672
  var parseStreamPart = (line) => {
@@ -772,20 +792,35 @@ async function parseComplexResponse({
772
792
  };
773
793
  functionCallMessage = prefixMap["function_call"];
774
794
  }
795
+ let toolCallMessage = null;
796
+ if (type === "tool_calls") {
797
+ prefixMap["tool_calls"] = {
798
+ id: generateId(),
799
+ role: "assistant",
800
+ content: "",
801
+ tool_calls: value.tool_calls,
802
+ createdAt
803
+ };
804
+ toolCallMessage = prefixMap["tool_calls"];
805
+ }
775
806
  if (type === "data") {
776
807
  prefixMap["data"].push(...value);
777
808
  }
778
809
  const responseMessage = prefixMap["text"];
779
- const merged = [functionCallMessage, responseMessage].filter(
780
- Boolean
781
- );
810
+ const merged = [
811
+ functionCallMessage,
812
+ toolCallMessage,
813
+ responseMessage
814
+ ].filter(Boolean);
782
815
  update(merged, [...prefixMap["data"]]);
783
816
  }
784
817
  onFinish == null ? void 0 : onFinish(prefixMap);
785
818
  return {
786
- messages: [prefixMap.text, prefixMap.function_call].filter(
787
- Boolean
788
- ),
819
+ messages: [
820
+ prefixMap.text,
821
+ prefixMap.function_call,
822
+ prefixMap.tool_calls
823
+ ].filter(Boolean),
789
824
  data: prefixMap.data
790
825
  };
791
826
  }
@@ -871,6 +906,8 @@ async function callChatApi({
871
906
  streamedResponse += decode(value);
872
907
  if (streamedResponse.startsWith('{"function_call":')) {
873
908
  responseMessage["function_call"] = streamedResponse;
909
+ } else if (streamedResponse.startsWith('{"tool_calls":')) {
910
+ responseMessage["tool_calls"] = streamedResponse;
874
911
  } else {
875
912
  responseMessage["content"] = streamedResponse;
876
913
  }
@@ -885,6 +922,11 @@ async function callChatApi({
885
922
  responseMessage["function_call"] = parsedFunctionCall;
886
923
  appendMessage({ ...responseMessage });
887
924
  }
925
+ if (streamedResponse.startsWith('{"tool_calls":')) {
926
+ const parsedToolCalls = JSON.parse(streamedResponse).tool_calls;
927
+ responseMessage["tool_calls"] = parsedToolCalls;
928
+ appendMessage({ ...responseMessage });
929
+ }
888
930
  if (onFinish) {
889
931
  onFinish(responseMessage);
890
932
  }
@@ -896,6 +938,7 @@ async function callChatApi({
896
938
  async function processChatStream({
897
939
  getStreamedResponse: getStreamedResponse2,
898
940
  experimental_onFunctionCall,
941
+ experimental_onToolCall,
899
942
  updateChatRequest,
900
943
  getCurrentMessages
901
944
  }) {
@@ -904,12 +947,18 @@ async function processChatStream({
904
947
  if ("messages" in messagesAndDataOrJustMessage) {
905
948
  let hasFollowingResponse = false;
906
949
  for (const message of messagesAndDataOrJustMessage.messages) {
907
- if (message.function_call === void 0 || typeof message.function_call === "string") {
950
+ if ((message.function_call === void 0 || typeof message.function_call === "string") && (message.tool_calls === void 0 || typeof message.tool_calls === "string")) {
908
951
  continue;
909
952
  }
910
953
  hasFollowingResponse = true;
911
954
  if (experimental_onFunctionCall) {
912
955
  const functionCall = message.function_call;
956
+ if (typeof functionCall !== "object") {
957
+ console.warn(
958
+ "experimental_onFunctionCall should not be defined when using tools"
959
+ );
960
+ continue;
961
+ }
913
962
  const functionCallResponse = await experimental_onFunctionCall(
914
963
  getCurrentMessages(),
915
964
  functionCall
@@ -920,22 +969,83 @@ async function processChatStream({
920
969
  }
921
970
  updateChatRequest(functionCallResponse);
922
971
  }
972
+ if (experimental_onToolCall) {
973
+ const toolCalls = message.tool_calls;
974
+ if (!Array.isArray(toolCalls) || toolCalls.some((toolCall) => typeof toolCall !== "object")) {
975
+ console.warn(
976
+ "experimental_onToolCall should not be defined when using tools"
977
+ );
978
+ continue;
979
+ }
980
+ const toolCallResponse = await experimental_onToolCall(getCurrentMessages(), toolCalls);
981
+ if (toolCallResponse === void 0) {
982
+ hasFollowingResponse = false;
983
+ break;
984
+ }
985
+ updateChatRequest(toolCallResponse);
986
+ }
923
987
  }
924
988
  if (!hasFollowingResponse) {
925
989
  break;
926
990
  }
927
991
  } else {
992
+ let fixFunctionCallArguments2 = function(response) {
993
+ for (const message of response.messages) {
994
+ if (message.tool_calls !== void 0) {
995
+ for (const toolCall of message.tool_calls) {
996
+ if (typeof toolCall === "object") {
997
+ if (toolCall.function.arguments && typeof toolCall.function.arguments !== "string") {
998
+ toolCall.function.arguments = JSON.stringify(
999
+ toolCall.function.arguments
1000
+ );
1001
+ }
1002
+ }
1003
+ }
1004
+ }
1005
+ if (message.function_call !== void 0) {
1006
+ if (typeof message.function_call === "object") {
1007
+ if (message.function_call.arguments && typeof message.function_call.arguments !== "string") {
1008
+ message.function_call.arguments = JSON.stringify(
1009
+ message.function_call.arguments
1010
+ );
1011
+ }
1012
+ }
1013
+ }
1014
+ }
1015
+ };
1016
+ var fixFunctionCallArguments = fixFunctionCallArguments2;
928
1017
  const streamedResponseMessage = messagesAndDataOrJustMessage;
929
- if (streamedResponseMessage.function_call === void 0 || typeof streamedResponseMessage.function_call === "string") {
1018
+ if ((streamedResponseMessage.function_call === void 0 || typeof streamedResponseMessage.function_call === "string") && (streamedResponseMessage.tool_calls === void 0 || typeof streamedResponseMessage.tool_calls === "string")) {
930
1019
  break;
931
1020
  }
932
1021
  if (experimental_onFunctionCall) {
933
1022
  const functionCall = streamedResponseMessage.function_call;
1023
+ if (!(typeof functionCall === "object")) {
1024
+ console.warn(
1025
+ "experimental_onFunctionCall should not be defined when using tools"
1026
+ );
1027
+ continue;
1028
+ }
934
1029
  const functionCallResponse = await experimental_onFunctionCall(getCurrentMessages(), functionCall);
935
1030
  if (functionCallResponse === void 0)
936
1031
  break;
1032
+ fixFunctionCallArguments2(functionCallResponse);
937
1033
  updateChatRequest(functionCallResponse);
938
1034
  }
1035
+ if (experimental_onToolCall) {
1036
+ const toolCalls = streamedResponseMessage.tool_calls;
1037
+ if (!(typeof toolCalls === "object")) {
1038
+ console.warn(
1039
+ "experimental_onToolCall should not be defined when using functions"
1040
+ );
1041
+ continue;
1042
+ }
1043
+ const toolCallResponse = await experimental_onToolCall(getCurrentMessages(), toolCalls);
1044
+ if (toolCallResponse === void 0)
1045
+ break;
1046
+ fixFunctionCallArguments2(toolCallResponse);
1047
+ updateChatRequest(toolCallResponse);
1048
+ }
939
1049
  }
940
1050
  }
941
1051
  }
@@ -944,14 +1054,20 @@ async function processChatStream({
944
1054
  var getStreamedResponse = async (api, chatRequest, mutate, mutateStreamData, existingData, extraMetadata, previousMessages, abortControllerRef, generateId, onFinish, onResponse, sendExtraMessageFields) => {
945
1055
  var _a, _b;
946
1056
  mutate(chatRequest.messages);
947
- const constructedMessagesPayload = sendExtraMessageFields ? chatRequest.messages : chatRequest.messages.map(({ role, content, name, function_call }) => ({
948
- role,
949
- content,
950
- ...name !== void 0 && { name },
951
- ...function_call !== void 0 && {
952
- function_call
953
- }
954
- }));
1057
+ const constructedMessagesPayload = sendExtraMessageFields ? chatRequest.messages : chatRequest.messages.map(
1058
+ ({ role, content, name, function_call, tool_calls, tool_call_id }) => ({
1059
+ role,
1060
+ content,
1061
+ tool_call_id,
1062
+ ...name !== void 0 && { name },
1063
+ ...function_call !== void 0 && {
1064
+ function_call
1065
+ },
1066
+ ...tool_calls !== void 0 && {
1067
+ tool_calls
1068
+ }
1069
+ })
1070
+ );
955
1071
  return await callChatApi({
956
1072
  api,
957
1073
  messages: constructedMessagesPayload,
@@ -963,6 +1079,12 @@ var getStreamedResponse = async (api, chatRequest, mutate, mutateStreamData, exi
963
1079
  },
964
1080
  ...chatRequest.function_call !== void 0 && {
965
1081
  function_call: chatRequest.function_call
1082
+ },
1083
+ ...chatRequest.tools !== void 0 && {
1084
+ tools: chatRequest.tools
1085
+ },
1086
+ ...chatRequest.tool_choice !== void 0 && {
1087
+ tool_choice: chatRequest.tool_choice
966
1088
  }
967
1089
  },
968
1090
  credentials: extraMetadata.credentials,
@@ -995,6 +1117,7 @@ function useChat({
995
1117
  initialInput = "",
996
1118
  sendExtraMessageFields,
997
1119
  experimental_onFunctionCall,
1120
+ experimental_onToolCall,
998
1121
  onResponse,
999
1122
  onFinish,
1000
1123
  onError,
@@ -1051,6 +1174,7 @@ function useChat({
1051
1174
  sendExtraMessageFields
1052
1175
  ),
1053
1176
  experimental_onFunctionCall,
1177
+ experimental_onToolCall,
1054
1178
  updateChatRequest: (chatRequestParam) => {
1055
1179
  chatRequest = chatRequestParam;
1056
1180
  },
@@ -1071,7 +1195,13 @@ function useChat({
1071
1195
  loading.set(false);
1072
1196
  }
1073
1197
  }
1074
- const append = async (message, { options, functions, function_call } = {}) => {
1198
+ const append = async (message, {
1199
+ options,
1200
+ functions,
1201
+ function_call,
1202
+ tools,
1203
+ tool_choice
1204
+ } = {}) => {
1075
1205
  if (!message.id) {
1076
1206
  message.id = generateId();
1077
1207
  }
@@ -1079,14 +1209,18 @@ function useChat({
1079
1209
  messages: (0, import_store.get)(messages).concat(message),
1080
1210
  options,
1081
1211
  ...functions !== void 0 && { functions },
1082
- ...function_call !== void 0 && { function_call }
1212
+ ...function_call !== void 0 && { function_call },
1213
+ ...tools !== void 0 && { tools },
1214
+ ...tool_choice !== void 0 && { tool_choice }
1083
1215
  };
1084
1216
  return triggerRequest(chatRequest);
1085
1217
  };
1086
1218
  const reload = async ({
1087
1219
  options,
1088
1220
  functions,
1089
- function_call
1221
+ function_call,
1222
+ tools,
1223
+ tool_choice
1090
1224
  } = {}) => {
1091
1225
  const messagesSnapshot = (0, import_store.get)(messages);
1092
1226
  if (messagesSnapshot.length === 0)
@@ -1097,7 +1231,9 @@ function useChat({
1097
1231
  messages: messagesSnapshot.slice(0, -1),
1098
1232
  options,
1099
1233
  ...functions !== void 0 && { functions },
1100
- ...function_call !== void 0 && { function_call }
1234
+ ...function_call !== void 0 && { function_call },
1235
+ ...tools !== void 0 && { tools },
1236
+ ...tool_choice !== void 0 && { tool_choice }
1101
1237
  };
1102
1238
  return triggerRequest(chatRequest2);
1103
1239
  }
@@ -1105,7 +1241,9 @@ function useChat({
1105
1241
  messages: messagesSnapshot,
1106
1242
  options,
1107
1243
  ...functions !== void 0 && { functions },
1108
- ...function_call !== void 0 && { function_call }
1244
+ ...function_call !== void 0 && { function_call },
1245
+ ...tools !== void 0 && { tools },
1246
+ ...tool_choice !== void 0 && { tool_choice }
1109
1247
  };
1110
1248
  return triggerRequest(chatRequest);
1111
1249
  };