ai 3.1.5 → 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.
@@ -4,6 +4,13 @@
4
4
  import { useCallback, useEffect, useId, useRef, useState } from "react";
5
5
  import useSWR from "swr";
6
6
 
7
+ // shared/generate-id.ts
8
+ import { customAlphabet } from "nanoid/non-secure";
9
+ var generateId = customAlphabet(
10
+ "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
11
+ 7
12
+ );
13
+
7
14
  // shared/stream-parts.ts
8
15
  var textStreamPart = {
9
16
  code: "0",
@@ -100,7 +107,7 @@ var dataMessageStreamPart = {
100
107
  };
101
108
  }
102
109
  };
103
- var toolCallStreamPart = {
110
+ var toolCallsStreamPart = {
104
111
  code: "7",
105
112
  name: "tool_calls",
106
113
  parse: (value) => {
@@ -127,6 +134,36 @@ var messageAnnotationsStreamPart = {
127
134
  return { type: "message_annotations", value };
128
135
  }
129
136
  };
137
+ var toolCallStreamPart = {
138
+ code: "9",
139
+ name: "tool_call",
140
+ parse: (value) => {
141
+ 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") {
142
+ throw new Error(
143
+ '"tool_call" parts expect an object with a "toolCallId", "toolName", and "args" property.'
144
+ );
145
+ }
146
+ return {
147
+ type: "tool_call",
148
+ value
149
+ };
150
+ }
151
+ };
152
+ var toolResultStreamPart = {
153
+ code: "a",
154
+ name: "tool_result",
155
+ parse: (value) => {
156
+ 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)) {
157
+ throw new Error(
158
+ '"tool_result" parts expect an object with a "toolCallId", "toolName", "args", and "result" property.'
159
+ );
160
+ }
161
+ return {
162
+ type: "tool_result",
163
+ value
164
+ };
165
+ }
166
+ };
130
167
  var streamParts = [
131
168
  textStreamPart,
132
169
  functionCallStreamPart,
@@ -135,8 +172,10 @@ var streamParts = [
135
172
  assistantMessageStreamPart,
136
173
  assistantControlDataStreamPart,
137
174
  dataMessageStreamPart,
175
+ toolCallsStreamPart,
176
+ messageAnnotationsStreamPart,
138
177
  toolCallStreamPart,
139
- messageAnnotationsStreamPart
178
+ toolResultStreamPart
140
179
  ];
141
180
  var streamPartsByCode = {
142
181
  [textStreamPart.code]: textStreamPart,
@@ -146,8 +185,10 @@ var streamPartsByCode = {
146
185
  [assistantMessageStreamPart.code]: assistantMessageStreamPart,
147
186
  [assistantControlDataStreamPart.code]: assistantControlDataStreamPart,
148
187
  [dataMessageStreamPart.code]: dataMessageStreamPart,
188
+ [toolCallsStreamPart.code]: toolCallsStreamPart,
189
+ [messageAnnotationsStreamPart.code]: messageAnnotationsStreamPart,
149
190
  [toolCallStreamPart.code]: toolCallStreamPart,
150
- [messageAnnotationsStreamPart.code]: messageAnnotationsStreamPart
191
+ [toolResultStreamPart.code]: toolResultStreamPart
151
192
  };
152
193
  var StreamStringPrefixes = {
153
194
  [textStreamPart.name]: textStreamPart.code,
@@ -157,8 +198,10 @@ var StreamStringPrefixes = {
157
198
  [assistantMessageStreamPart.name]: assistantMessageStreamPart.code,
158
199
  [assistantControlDataStreamPart.name]: assistantControlDataStreamPart.code,
159
200
  [dataMessageStreamPart.name]: dataMessageStreamPart.code,
201
+ [toolCallsStreamPart.name]: toolCallsStreamPart.code,
202
+ [messageAnnotationsStreamPart.name]: messageAnnotationsStreamPart.code,
160
203
  [toolCallStreamPart.name]: toolCallStreamPart.code,
161
- [messageAnnotationsStreamPart.name]: messageAnnotationsStreamPart.code
204
+ [toolResultStreamPart.name]: toolResultStreamPart.code
162
205
  };
163
206
  var validCodes = streamParts.map((part) => part.code);
164
207
  var parseStreamPart = (line) => {
@@ -219,13 +262,6 @@ async function* readDataStream(reader, {
219
262
  }
220
263
  }
221
264
 
222
- // shared/generate-id.ts
223
- import { customAlphabet } from "nanoid/non-secure";
224
- var generateId = customAlphabet(
225
- "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
226
- 7
227
- );
228
-
229
265
  // shared/parse-complex-response.ts
230
266
  function assignAnnotationsToMessage(message, annotations) {
231
267
  if (!message || !annotations || !annotations.length)
@@ -263,6 +299,40 @@ async function parseComplexResponse({
263
299
  };
264
300
  }
265
301
  }
302
+ if (type === "tool_call") {
303
+ if (prefixMap.text == null) {
304
+ prefixMap.text = {
305
+ id: generateId2(),
306
+ role: "assistant",
307
+ content: "",
308
+ createdAt
309
+ };
310
+ }
311
+ if (prefixMap.text.toolInvocations == null) {
312
+ prefixMap.text.toolInvocations = [];
313
+ }
314
+ prefixMap.text.toolInvocations.push(value);
315
+ } else if (type === "tool_result") {
316
+ if (prefixMap.text == null) {
317
+ prefixMap.text = {
318
+ id: generateId2(),
319
+ role: "assistant",
320
+ content: "",
321
+ createdAt
322
+ };
323
+ }
324
+ if (prefixMap.text.toolInvocations == null) {
325
+ prefixMap.text.toolInvocations = [];
326
+ }
327
+ const toolInvocationIndex = prefixMap.text.toolInvocations.findIndex(
328
+ (invocation) => invocation.toolCallId === value.toolCallId
329
+ );
330
+ if (toolInvocationIndex !== -1) {
331
+ prefixMap.text.toolInvocations[toolInvocationIndex] = value;
332
+ } else {
333
+ prefixMap.text.toolInvocations.push(value);
334
+ }
335
+ }
266
336
  let functionCallMessage = null;
267
337
  if (type === "function_call") {
268
338
  prefixMap["function_call"] = {
@@ -572,17 +642,23 @@ var getStreamedResponse = async (api, chatRequest, mutate, mutateStreamData, exi
572
642
  const previousMessages = messagesRef.current;
573
643
  mutate(chatRequest.messages, false);
574
644
  const constructedMessagesPayload = sendExtraMessageFields ? chatRequest.messages : chatRequest.messages.map(
575
- ({ role, content, name, function_call, tool_calls, tool_call_id }) => ({
645
+ ({
646
+ role,
647
+ content,
648
+ name,
649
+ toolInvocations,
650
+ function_call,
651
+ tool_calls,
652
+ tool_call_id
653
+ }) => ({
576
654
  role,
577
655
  content,
578
- tool_call_id,
579
656
  ...name !== void 0 && { name },
580
- ...function_call !== void 0 && {
581
- function_call
582
- },
583
- ...tool_calls !== void 0 && {
584
- tool_calls
585
- }
657
+ ...toolInvocations !== void 0 && { toolInvocations },
658
+ // outdated function/tool call handling (TODO deprecate):
659
+ tool_call_id,
660
+ ...function_call !== void 0 && { function_call },
661
+ ...tool_calls !== void 0 && { tool_calls }
586
662
  })
587
663
  );
588
664
  if (typeof api !== "string") {
@@ -881,7 +957,30 @@ function useChat({
881
957
  handleInputChange,
882
958
  handleSubmit,
883
959
  isLoading,
884
- data: streamData
960
+ data: streamData,
961
+ experimental_addToolResult: ({
962
+ toolCallId,
963
+ result
964
+ }) => {
965
+ const updatedMessages = messagesRef.current.map(
966
+ (message, index, arr) => (
967
+ // update the tool calls in the last assistant message:
968
+ index === arr.length - 1 && message.role === "assistant" && message.toolInvocations ? {
969
+ ...message,
970
+ toolInvocations: message.toolInvocations.map(
971
+ (toolInvocation) => toolInvocation.toolCallId === toolCallId ? { ...toolInvocation, result } : toolInvocation
972
+ )
973
+ } : message
974
+ )
975
+ );
976
+ mutate(updatedMessages, false);
977
+ const lastMessage = updatedMessages[updatedMessages.length - 1];
978
+ if (lastMessage.role === "assistant" && lastMessage.toolInvocations && lastMessage.toolInvocations.length > 0 && lastMessage.toolInvocations.every(
979
+ (toolInvocation) => "result" in toolInvocation
980
+ )) {
981
+ triggerRequest({ messages: updatedMessages });
982
+ }
983
+ }
885
984
  };
886
985
  }
887
986