ai 2.2.29 → 2.2.31

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.
@@ -137,6 +137,23 @@ var dataMessageStreamPart = {
137
137
  };
138
138
  }
139
139
  };
140
+ var toolCallStreamPart = {
141
+ code: "7",
142
+ name: "tool_calls",
143
+ parse: (value) => {
144
+ 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) => {
145
+ 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";
146
+ })) {
147
+ throw new Error(
148
+ '"tool_calls" parts expect an object with a ToolCallPayload.'
149
+ );
150
+ }
151
+ return {
152
+ type: "tool_calls",
153
+ value
154
+ };
155
+ }
156
+ };
140
157
  var streamParts = [
141
158
  textStreamPart,
142
159
  functionCallStreamPart,
@@ -144,7 +161,8 @@ var streamParts = [
144
161
  errorStreamPart,
145
162
  assistantMessageStreamPart,
146
163
  assistantControlDataStreamPart,
147
- dataMessageStreamPart
164
+ dataMessageStreamPart,
165
+ toolCallStreamPart
148
166
  ];
149
167
  var streamPartsByCode = {
150
168
  [textStreamPart.code]: textStreamPart,
@@ -153,7 +171,8 @@ var streamPartsByCode = {
153
171
  [errorStreamPart.code]: errorStreamPart,
154
172
  [assistantMessageStreamPart.code]: assistantMessageStreamPart,
155
173
  [assistantControlDataStreamPart.code]: assistantControlDataStreamPart,
156
- [dataMessageStreamPart.code]: dataMessageStreamPart
174
+ [dataMessageStreamPart.code]: dataMessageStreamPart,
175
+ [toolCallStreamPart.code]: toolCallStreamPart
157
176
  };
158
177
  var StreamStringPrefixes = {
159
178
  [textStreamPart.name]: textStreamPart.code,
@@ -162,7 +181,8 @@ var StreamStringPrefixes = {
162
181
  [errorStreamPart.name]: errorStreamPart.code,
163
182
  [assistantMessageStreamPart.name]: assistantMessageStreamPart.code,
164
183
  [assistantControlDataStreamPart.name]: assistantControlDataStreamPart.code,
165
- [dataMessageStreamPart.name]: dataMessageStreamPart.code
184
+ [dataMessageStreamPart.name]: dataMessageStreamPart.code,
185
+ [toolCallStreamPart.name]: toolCallStreamPart.code
166
186
  };
167
187
  var validCodes = streamParts.map((part) => part.code);
168
188
  var parseStreamPart = (line) => {
@@ -288,20 +308,35 @@ async function parseComplexResponse({
288
308
  };
289
309
  functionCallMessage = prefixMap["function_call"];
290
310
  }
311
+ let toolCallMessage = null;
312
+ if (type === "tool_calls") {
313
+ prefixMap["tool_calls"] = {
314
+ id: generateId(),
315
+ role: "assistant",
316
+ content: "",
317
+ tool_calls: value.tool_calls,
318
+ createdAt
319
+ };
320
+ toolCallMessage = prefixMap["tool_calls"];
321
+ }
291
322
  if (type === "data") {
292
323
  prefixMap["data"].push(...value);
293
324
  }
294
325
  const responseMessage = prefixMap["text"];
295
- const merged = [functionCallMessage, responseMessage].filter(
296
- Boolean
297
- );
326
+ const merged = [
327
+ functionCallMessage,
328
+ toolCallMessage,
329
+ responseMessage
330
+ ].filter(Boolean);
298
331
  update(merged, [...prefixMap["data"]]);
299
332
  }
300
333
  onFinish == null ? void 0 : onFinish(prefixMap);
301
334
  return {
302
- messages: [prefixMap.text, prefixMap.function_call].filter(
303
- Boolean
304
- ),
335
+ messages: [
336
+ prefixMap.text,
337
+ prefixMap.function_call,
338
+ prefixMap.tool_calls
339
+ ].filter(Boolean),
305
340
  data: prefixMap.data
306
341
  };
307
342
  }
@@ -387,6 +422,8 @@ async function callChatApi({
387
422
  streamedResponse += decode(value);
388
423
  if (streamedResponse.startsWith('{"function_call":')) {
389
424
  responseMessage["function_call"] = streamedResponse;
425
+ } else if (streamedResponse.startsWith('{"tool_calls":')) {
426
+ responseMessage["tool_calls"] = streamedResponse;
390
427
  } else {
391
428
  responseMessage["content"] = streamedResponse;
392
429
  }
@@ -401,6 +438,11 @@ async function callChatApi({
401
438
  responseMessage["function_call"] = parsedFunctionCall;
402
439
  appendMessage({ ...responseMessage });
403
440
  }
441
+ if (streamedResponse.startsWith('{"tool_calls":')) {
442
+ const parsedToolCalls = JSON.parse(streamedResponse).tool_calls;
443
+ responseMessage["tool_calls"] = parsedToolCalls;
444
+ appendMessage({ ...responseMessage });
445
+ }
404
446
  if (onFinish) {
405
447
  onFinish(responseMessage);
406
448
  }
@@ -412,6 +454,7 @@ async function callChatApi({
412
454
  async function processChatStream({
413
455
  getStreamedResponse: getStreamedResponse2,
414
456
  experimental_onFunctionCall,
457
+ experimental_onToolCall,
415
458
  updateChatRequest,
416
459
  getCurrentMessages
417
460
  }) {
@@ -420,12 +463,18 @@ async function processChatStream({
420
463
  if ("messages" in messagesAndDataOrJustMessage) {
421
464
  let hasFollowingResponse = false;
422
465
  for (const message of messagesAndDataOrJustMessage.messages) {
423
- if (message.function_call === void 0 || typeof message.function_call === "string") {
466
+ if ((message.function_call === void 0 || typeof message.function_call === "string") && (message.tool_calls === void 0 || typeof message.tool_calls === "string")) {
424
467
  continue;
425
468
  }
426
469
  hasFollowingResponse = true;
427
470
  if (experimental_onFunctionCall) {
428
471
  const functionCall = message.function_call;
472
+ if (typeof functionCall !== "object") {
473
+ console.warn(
474
+ "experimental_onFunctionCall should not be defined when using tools"
475
+ );
476
+ continue;
477
+ }
429
478
  const functionCallResponse = await experimental_onFunctionCall(
430
479
  getCurrentMessages(),
431
480
  functionCall
@@ -436,22 +485,83 @@ async function processChatStream({
436
485
  }
437
486
  updateChatRequest(functionCallResponse);
438
487
  }
488
+ if (experimental_onToolCall) {
489
+ const toolCalls = message.tool_calls;
490
+ if (!Array.isArray(toolCalls) || toolCalls.some((toolCall) => typeof toolCall !== "object")) {
491
+ console.warn(
492
+ "experimental_onToolCall should not be defined when using tools"
493
+ );
494
+ continue;
495
+ }
496
+ const toolCallResponse = await experimental_onToolCall(getCurrentMessages(), toolCalls);
497
+ if (toolCallResponse === void 0) {
498
+ hasFollowingResponse = false;
499
+ break;
500
+ }
501
+ updateChatRequest(toolCallResponse);
502
+ }
439
503
  }
440
504
  if (!hasFollowingResponse) {
441
505
  break;
442
506
  }
443
507
  } else {
508
+ let fixFunctionCallArguments2 = function(response) {
509
+ for (const message of response.messages) {
510
+ if (message.tool_calls !== void 0) {
511
+ for (const toolCall of message.tool_calls) {
512
+ if (typeof toolCall === "object") {
513
+ if (toolCall.function.arguments && typeof toolCall.function.arguments !== "string") {
514
+ toolCall.function.arguments = JSON.stringify(
515
+ toolCall.function.arguments
516
+ );
517
+ }
518
+ }
519
+ }
520
+ }
521
+ if (message.function_call !== void 0) {
522
+ if (typeof message.function_call === "object") {
523
+ if (message.function_call.arguments && typeof message.function_call.arguments !== "string") {
524
+ message.function_call.arguments = JSON.stringify(
525
+ message.function_call.arguments
526
+ );
527
+ }
528
+ }
529
+ }
530
+ }
531
+ };
532
+ var fixFunctionCallArguments = fixFunctionCallArguments2;
444
533
  const streamedResponseMessage = messagesAndDataOrJustMessage;
445
- if (streamedResponseMessage.function_call === void 0 || typeof streamedResponseMessage.function_call === "string") {
534
+ if ((streamedResponseMessage.function_call === void 0 || typeof streamedResponseMessage.function_call === "string") && (streamedResponseMessage.tool_calls === void 0 || typeof streamedResponseMessage.tool_calls === "string")) {
446
535
  break;
447
536
  }
448
537
  if (experimental_onFunctionCall) {
449
538
  const functionCall = streamedResponseMessage.function_call;
539
+ if (!(typeof functionCall === "object")) {
540
+ console.warn(
541
+ "experimental_onFunctionCall should not be defined when using tools"
542
+ );
543
+ continue;
544
+ }
450
545
  const functionCallResponse = await experimental_onFunctionCall(getCurrentMessages(), functionCall);
451
546
  if (functionCallResponse === void 0)
452
547
  break;
548
+ fixFunctionCallArguments2(functionCallResponse);
453
549
  updateChatRequest(functionCallResponse);
454
550
  }
551
+ if (experimental_onToolCall) {
552
+ const toolCalls = streamedResponseMessage.tool_calls;
553
+ if (!(typeof toolCalls === "object")) {
554
+ console.warn(
555
+ "experimental_onToolCall should not be defined when using functions"
556
+ );
557
+ continue;
558
+ }
559
+ const toolCallResponse = await experimental_onToolCall(getCurrentMessages(), toolCalls);
560
+ if (toolCallResponse === void 0)
561
+ break;
562
+ fixFunctionCallArguments2(toolCallResponse);
563
+ updateChatRequest(toolCallResponse);
564
+ }
455
565
  }
456
566
  }
457
567
  }
@@ -461,14 +571,20 @@ var getStreamedResponse = async (api, chatRequest, mutate, mutateStreamData, exi
461
571
  var _a, _b;
462
572
  const previousMessages = messagesRef.current;
463
573
  mutate(chatRequest.messages, false);
464
- const constructedMessagesPayload = sendExtraMessageFields ? chatRequest.messages : chatRequest.messages.map(({ role, content, name, function_call }) => ({
465
- role,
466
- content,
467
- ...name !== void 0 && { name },
468
- ...function_call !== void 0 && {
469
- function_call
470
- }
471
- }));
574
+ const constructedMessagesPayload = sendExtraMessageFields ? chatRequest.messages : chatRequest.messages.map(
575
+ ({ role, content, name, function_call, tool_calls, tool_call_id }) => ({
576
+ role,
577
+ content,
578
+ tool_call_id,
579
+ ...name !== void 0 && { name },
580
+ ...function_call !== void 0 && {
581
+ function_call
582
+ },
583
+ ...tool_calls !== void 0 && {
584
+ tool_calls
585
+ }
586
+ })
587
+ );
472
588
  if (typeof api !== "string") {
473
589
  const replyId = generateId();
474
590
  const createdAt = /* @__PURE__ */ new Date();
@@ -514,6 +630,12 @@ var getStreamedResponse = async (api, chatRequest, mutate, mutateStreamData, exi
514
630
  },
515
631
  ...chatRequest.function_call !== void 0 && {
516
632
  function_call: chatRequest.function_call
633
+ },
634
+ ...chatRequest.tools !== void 0 && {
635
+ tools: chatRequest.tools
636
+ },
637
+ ...chatRequest.tool_choice !== void 0 && {
638
+ tool_choice: chatRequest.tool_choice
517
639
  }
518
640
  },
519
641
  credentials: extraMetadataRef.current.credentials,
@@ -544,6 +666,7 @@ function useChat({
544
666
  initialInput = "",
545
667
  sendExtraMessageFields,
546
668
  experimental_onFunctionCall,
669
+ experimental_onToolCall,
547
670
  onResponse,
548
671
  onFinish,
549
672
  onError,
@@ -607,6 +730,7 @@ function useChat({
607
730
  sendExtraMessageFields
608
731
  ),
609
732
  experimental_onFunctionCall,
733
+ experimental_onToolCall,
610
734
  updateChatRequest: (chatRequestParam) => {
611
735
  chatRequest = chatRequestParam;
612
736
  },
@@ -639,13 +763,21 @@ function useChat({
639
763
  streamData,
640
764
  sendExtraMessageFields,
641
765
  experimental_onFunctionCall,
766
+ experimental_onToolCall,
642
767
  messagesRef,
643
768
  abortControllerRef,
644
769
  generateId
645
770
  ]
646
771
  );
647
772
  const append = (0, import_react.useCallback)(
648
- async (message, { options, functions, function_call, data } = {}) => {
773
+ async (message, {
774
+ options,
775
+ functions,
776
+ function_call,
777
+ tools,
778
+ tool_choice,
779
+ data
780
+ } = {}) => {
649
781
  if (!message.id) {
650
782
  message.id = generateId();
651
783
  }
@@ -654,14 +786,22 @@ function useChat({
654
786
  options,
655
787
  data,
656
788
  ...functions !== void 0 && { functions },
657
- ...function_call !== void 0 && { function_call }
789
+ ...function_call !== void 0 && { function_call },
790
+ ...tools !== void 0 && { tools },
791
+ ...tool_choice !== void 0 && { tool_choice }
658
792
  };
659
793
  return triggerRequest(chatRequest);
660
794
  },
661
795
  [triggerRequest, generateId]
662
796
  );
663
797
  const reload = (0, import_react.useCallback)(
664
- async ({ options, functions, function_call } = {}) => {
798
+ async ({
799
+ options,
800
+ functions,
801
+ function_call,
802
+ tools,
803
+ tool_choice
804
+ } = {}) => {
665
805
  if (messagesRef.current.length === 0)
666
806
  return null;
667
807
  const lastMessage = messagesRef.current[messagesRef.current.length - 1];
@@ -670,7 +810,9 @@ function useChat({
670
810
  messages: messagesRef.current.slice(0, -1),
671
811
  options,
672
812
  ...functions !== void 0 && { functions },
673
- ...function_call !== void 0 && { function_call }
813
+ ...function_call !== void 0 && { function_call },
814
+ ...tools !== void 0 && { tools },
815
+ ...tool_choice !== void 0 && { tool_choice }
674
816
  };
675
817
  return triggerRequest(chatRequest2);
676
818
  }
@@ -678,7 +820,9 @@ function useChat({
678
820
  messages: messagesRef.current,
679
821
  options,
680
822
  ...functions !== void 0 && { functions },
681
- ...function_call !== void 0 && { function_call }
823
+ ...function_call !== void 0 && { function_call },
824
+ ...tools !== void 0 && { tools },
825
+ ...tool_choice !== void 0 && { tool_choice }
682
826
  };
683
827
  return triggerRequest(chatRequest);
684
828
  },
@@ -1070,6 +1214,7 @@ function experimental_useAssistant({
1070
1214
  messages,
1071
1215
  threadId,
1072
1216
  input,
1217
+ setInput,
1073
1218
  handleInputChange,
1074
1219
  submitMessage,
1075
1220
  status,