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.
@@ -100,6 +100,23 @@ var dataMessageStreamPart = {
100
100
  };
101
101
  }
102
102
  };
103
+ var toolCallStreamPart = {
104
+ code: "7",
105
+ name: "tool_calls",
106
+ parse: (value) => {
107
+ 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) => {
108
+ 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";
109
+ })) {
110
+ throw new Error(
111
+ '"tool_calls" parts expect an object with a ToolCallPayload.'
112
+ );
113
+ }
114
+ return {
115
+ type: "tool_calls",
116
+ value
117
+ };
118
+ }
119
+ };
103
120
  var streamParts = [
104
121
  textStreamPart,
105
122
  functionCallStreamPart,
@@ -107,7 +124,8 @@ var streamParts = [
107
124
  errorStreamPart,
108
125
  assistantMessageStreamPart,
109
126
  assistantControlDataStreamPart,
110
- dataMessageStreamPart
127
+ dataMessageStreamPart,
128
+ toolCallStreamPart
111
129
  ];
112
130
  var streamPartsByCode = {
113
131
  [textStreamPart.code]: textStreamPart,
@@ -116,7 +134,8 @@ var streamPartsByCode = {
116
134
  [errorStreamPart.code]: errorStreamPart,
117
135
  [assistantMessageStreamPart.code]: assistantMessageStreamPart,
118
136
  [assistantControlDataStreamPart.code]: assistantControlDataStreamPart,
119
- [dataMessageStreamPart.code]: dataMessageStreamPart
137
+ [dataMessageStreamPart.code]: dataMessageStreamPart,
138
+ [toolCallStreamPart.code]: toolCallStreamPart
120
139
  };
121
140
  var StreamStringPrefixes = {
122
141
  [textStreamPart.name]: textStreamPart.code,
@@ -125,7 +144,8 @@ var StreamStringPrefixes = {
125
144
  [errorStreamPart.name]: errorStreamPart.code,
126
145
  [assistantMessageStreamPart.name]: assistantMessageStreamPart.code,
127
146
  [assistantControlDataStreamPart.name]: assistantControlDataStreamPart.code,
128
- [dataMessageStreamPart.name]: dataMessageStreamPart.code
147
+ [dataMessageStreamPart.name]: dataMessageStreamPart.code,
148
+ [toolCallStreamPart.name]: toolCallStreamPart.code
129
149
  };
130
150
  var validCodes = streamParts.map((part) => part.code);
131
151
  var parseStreamPart = (line) => {
@@ -251,20 +271,35 @@ async function parseComplexResponse({
251
271
  };
252
272
  functionCallMessage = prefixMap["function_call"];
253
273
  }
274
+ let toolCallMessage = null;
275
+ if (type === "tool_calls") {
276
+ prefixMap["tool_calls"] = {
277
+ id: generateId(),
278
+ role: "assistant",
279
+ content: "",
280
+ tool_calls: value.tool_calls,
281
+ createdAt
282
+ };
283
+ toolCallMessage = prefixMap["tool_calls"];
284
+ }
254
285
  if (type === "data") {
255
286
  prefixMap["data"].push(...value);
256
287
  }
257
288
  const responseMessage = prefixMap["text"];
258
- const merged = [functionCallMessage, responseMessage].filter(
259
- Boolean
260
- );
289
+ const merged = [
290
+ functionCallMessage,
291
+ toolCallMessage,
292
+ responseMessage
293
+ ].filter(Boolean);
261
294
  update(merged, [...prefixMap["data"]]);
262
295
  }
263
296
  onFinish == null ? void 0 : onFinish(prefixMap);
264
297
  return {
265
- messages: [prefixMap.text, prefixMap.function_call].filter(
266
- Boolean
267
- ),
298
+ messages: [
299
+ prefixMap.text,
300
+ prefixMap.function_call,
301
+ prefixMap.tool_calls
302
+ ].filter(Boolean),
268
303
  data: prefixMap.data
269
304
  };
270
305
  }
@@ -350,6 +385,8 @@ async function callChatApi({
350
385
  streamedResponse += decode(value);
351
386
  if (streamedResponse.startsWith('{"function_call":')) {
352
387
  responseMessage["function_call"] = streamedResponse;
388
+ } else if (streamedResponse.startsWith('{"tool_calls":')) {
389
+ responseMessage["tool_calls"] = streamedResponse;
353
390
  } else {
354
391
  responseMessage["content"] = streamedResponse;
355
392
  }
@@ -364,6 +401,11 @@ async function callChatApi({
364
401
  responseMessage["function_call"] = parsedFunctionCall;
365
402
  appendMessage({ ...responseMessage });
366
403
  }
404
+ if (streamedResponse.startsWith('{"tool_calls":')) {
405
+ const parsedToolCalls = JSON.parse(streamedResponse).tool_calls;
406
+ responseMessage["tool_calls"] = parsedToolCalls;
407
+ appendMessage({ ...responseMessage });
408
+ }
367
409
  if (onFinish) {
368
410
  onFinish(responseMessage);
369
411
  }
@@ -375,6 +417,7 @@ async function callChatApi({
375
417
  async function processChatStream({
376
418
  getStreamedResponse: getStreamedResponse2,
377
419
  experimental_onFunctionCall,
420
+ experimental_onToolCall,
378
421
  updateChatRequest,
379
422
  getCurrentMessages
380
423
  }) {
@@ -383,12 +426,18 @@ async function processChatStream({
383
426
  if ("messages" in messagesAndDataOrJustMessage) {
384
427
  let hasFollowingResponse = false;
385
428
  for (const message of messagesAndDataOrJustMessage.messages) {
386
- if (message.function_call === void 0 || typeof message.function_call === "string") {
429
+ if ((message.function_call === void 0 || typeof message.function_call === "string") && (message.tool_calls === void 0 || typeof message.tool_calls === "string")) {
387
430
  continue;
388
431
  }
389
432
  hasFollowingResponse = true;
390
433
  if (experimental_onFunctionCall) {
391
434
  const functionCall = message.function_call;
435
+ if (typeof functionCall !== "object") {
436
+ console.warn(
437
+ "experimental_onFunctionCall should not be defined when using tools"
438
+ );
439
+ continue;
440
+ }
392
441
  const functionCallResponse = await experimental_onFunctionCall(
393
442
  getCurrentMessages(),
394
443
  functionCall
@@ -399,22 +448,83 @@ async function processChatStream({
399
448
  }
400
449
  updateChatRequest(functionCallResponse);
401
450
  }
451
+ if (experimental_onToolCall) {
452
+ const toolCalls = message.tool_calls;
453
+ if (!Array.isArray(toolCalls) || toolCalls.some((toolCall) => typeof toolCall !== "object")) {
454
+ console.warn(
455
+ "experimental_onToolCall should not be defined when using tools"
456
+ );
457
+ continue;
458
+ }
459
+ const toolCallResponse = await experimental_onToolCall(getCurrentMessages(), toolCalls);
460
+ if (toolCallResponse === void 0) {
461
+ hasFollowingResponse = false;
462
+ break;
463
+ }
464
+ updateChatRequest(toolCallResponse);
465
+ }
402
466
  }
403
467
  if (!hasFollowingResponse) {
404
468
  break;
405
469
  }
406
470
  } else {
471
+ let fixFunctionCallArguments2 = function(response) {
472
+ for (const message of response.messages) {
473
+ if (message.tool_calls !== void 0) {
474
+ for (const toolCall of message.tool_calls) {
475
+ if (typeof toolCall === "object") {
476
+ if (toolCall.function.arguments && typeof toolCall.function.arguments !== "string") {
477
+ toolCall.function.arguments = JSON.stringify(
478
+ toolCall.function.arguments
479
+ );
480
+ }
481
+ }
482
+ }
483
+ }
484
+ if (message.function_call !== void 0) {
485
+ if (typeof message.function_call === "object") {
486
+ if (message.function_call.arguments && typeof message.function_call.arguments !== "string") {
487
+ message.function_call.arguments = JSON.stringify(
488
+ message.function_call.arguments
489
+ );
490
+ }
491
+ }
492
+ }
493
+ }
494
+ };
495
+ var fixFunctionCallArguments = fixFunctionCallArguments2;
407
496
  const streamedResponseMessage = messagesAndDataOrJustMessage;
408
- if (streamedResponseMessage.function_call === void 0 || typeof streamedResponseMessage.function_call === "string") {
497
+ if ((streamedResponseMessage.function_call === void 0 || typeof streamedResponseMessage.function_call === "string") && (streamedResponseMessage.tool_calls === void 0 || typeof streamedResponseMessage.tool_calls === "string")) {
409
498
  break;
410
499
  }
411
500
  if (experimental_onFunctionCall) {
412
501
  const functionCall = streamedResponseMessage.function_call;
502
+ if (!(typeof functionCall === "object")) {
503
+ console.warn(
504
+ "experimental_onFunctionCall should not be defined when using tools"
505
+ );
506
+ continue;
507
+ }
413
508
  const functionCallResponse = await experimental_onFunctionCall(getCurrentMessages(), functionCall);
414
509
  if (functionCallResponse === void 0)
415
510
  break;
511
+ fixFunctionCallArguments2(functionCallResponse);
416
512
  updateChatRequest(functionCallResponse);
417
513
  }
514
+ if (experimental_onToolCall) {
515
+ const toolCalls = streamedResponseMessage.tool_calls;
516
+ if (!(typeof toolCalls === "object")) {
517
+ console.warn(
518
+ "experimental_onToolCall should not be defined when using functions"
519
+ );
520
+ continue;
521
+ }
522
+ const toolCallResponse = await experimental_onToolCall(getCurrentMessages(), toolCalls);
523
+ if (toolCallResponse === void 0)
524
+ break;
525
+ fixFunctionCallArguments2(toolCallResponse);
526
+ updateChatRequest(toolCallResponse);
527
+ }
418
528
  }
419
529
  }
420
530
  }
@@ -424,14 +534,20 @@ var getStreamedResponse = async (api, chatRequest, mutate, mutateStreamData, exi
424
534
  var _a, _b;
425
535
  const previousMessages = messagesRef.current;
426
536
  mutate(chatRequest.messages, false);
427
- const constructedMessagesPayload = sendExtraMessageFields ? chatRequest.messages : chatRequest.messages.map(({ role, content, name, function_call }) => ({
428
- role,
429
- content,
430
- ...name !== void 0 && { name },
431
- ...function_call !== void 0 && {
432
- function_call
433
- }
434
- }));
537
+ const constructedMessagesPayload = sendExtraMessageFields ? chatRequest.messages : chatRequest.messages.map(
538
+ ({ role, content, name, function_call, tool_calls, tool_call_id }) => ({
539
+ role,
540
+ content,
541
+ tool_call_id,
542
+ ...name !== void 0 && { name },
543
+ ...function_call !== void 0 && {
544
+ function_call
545
+ },
546
+ ...tool_calls !== void 0 && {
547
+ tool_calls
548
+ }
549
+ })
550
+ );
435
551
  if (typeof api !== "string") {
436
552
  const replyId = generateId();
437
553
  const createdAt = /* @__PURE__ */ new Date();
@@ -477,6 +593,12 @@ var getStreamedResponse = async (api, chatRequest, mutate, mutateStreamData, exi
477
593
  },
478
594
  ...chatRequest.function_call !== void 0 && {
479
595
  function_call: chatRequest.function_call
596
+ },
597
+ ...chatRequest.tools !== void 0 && {
598
+ tools: chatRequest.tools
599
+ },
600
+ ...chatRequest.tool_choice !== void 0 && {
601
+ tool_choice: chatRequest.tool_choice
480
602
  }
481
603
  },
482
604
  credentials: extraMetadataRef.current.credentials,
@@ -507,6 +629,7 @@ function useChat({
507
629
  initialInput = "",
508
630
  sendExtraMessageFields,
509
631
  experimental_onFunctionCall,
632
+ experimental_onToolCall,
510
633
  onResponse,
511
634
  onFinish,
512
635
  onError,
@@ -570,6 +693,7 @@ function useChat({
570
693
  sendExtraMessageFields
571
694
  ),
572
695
  experimental_onFunctionCall,
696
+ experimental_onToolCall,
573
697
  updateChatRequest: (chatRequestParam) => {
574
698
  chatRequest = chatRequestParam;
575
699
  },
@@ -602,13 +726,21 @@ function useChat({
602
726
  streamData,
603
727
  sendExtraMessageFields,
604
728
  experimental_onFunctionCall,
729
+ experimental_onToolCall,
605
730
  messagesRef,
606
731
  abortControllerRef,
607
732
  generateId
608
733
  ]
609
734
  );
610
735
  const append = useCallback(
611
- async (message, { options, functions, function_call, data } = {}) => {
736
+ async (message, {
737
+ options,
738
+ functions,
739
+ function_call,
740
+ tools,
741
+ tool_choice,
742
+ data
743
+ } = {}) => {
612
744
  if (!message.id) {
613
745
  message.id = generateId();
614
746
  }
@@ -617,14 +749,22 @@ function useChat({
617
749
  options,
618
750
  data,
619
751
  ...functions !== void 0 && { functions },
620
- ...function_call !== void 0 && { function_call }
752
+ ...function_call !== void 0 && { function_call },
753
+ ...tools !== void 0 && { tools },
754
+ ...tool_choice !== void 0 && { tool_choice }
621
755
  };
622
756
  return triggerRequest(chatRequest);
623
757
  },
624
758
  [triggerRequest, generateId]
625
759
  );
626
760
  const reload = useCallback(
627
- async ({ options, functions, function_call } = {}) => {
761
+ async ({
762
+ options,
763
+ functions,
764
+ function_call,
765
+ tools,
766
+ tool_choice
767
+ } = {}) => {
628
768
  if (messagesRef.current.length === 0)
629
769
  return null;
630
770
  const lastMessage = messagesRef.current[messagesRef.current.length - 1];
@@ -633,7 +773,9 @@ function useChat({
633
773
  messages: messagesRef.current.slice(0, -1),
634
774
  options,
635
775
  ...functions !== void 0 && { functions },
636
- ...function_call !== void 0 && { function_call }
776
+ ...function_call !== void 0 && { function_call },
777
+ ...tools !== void 0 && { tools },
778
+ ...tool_choice !== void 0 && { tool_choice }
637
779
  };
638
780
  return triggerRequest(chatRequest2);
639
781
  }
@@ -641,7 +783,9 @@ function useChat({
641
783
  messages: messagesRef.current,
642
784
  options,
643
785
  ...functions !== void 0 && { functions },
644
- ...function_call !== void 0 && { function_call }
786
+ ...function_call !== void 0 && { function_call },
787
+ ...tools !== void 0 && { tools },
788
+ ...tool_choice !== void 0 && { tool_choice }
645
789
  };
646
790
  return triggerRequest(chatRequest);
647
791
  },
@@ -1033,6 +1177,7 @@ function experimental_useAssistant({
1033
1177
  messages,
1034
1178
  threadId,
1035
1179
  input,
1180
+ setInput,
1036
1181
  handleInputChange,
1037
1182
  submitMessage,
1038
1183
  status,