@ai-sdk/react 1.0.0-canary.0 → 1.0.0-canary.2

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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,31 @@
1
1
  # @ai-sdk/react
2
2
 
3
+ ## 1.0.0-canary.2
4
+
5
+ ### Major Changes
6
+
7
+ - e117b54: chore (ui): remove deprecated useChat roundtrip options
8
+ - 7814c4b: chore (ui): remove streamMode setting from useChat & useCompletion
9
+
10
+ ### Patch Changes
11
+
12
+ - Updated dependencies [b469a7e]
13
+ - Updated dependencies [7814c4b]
14
+ - Updated dependencies [db46ce5]
15
+ - @ai-sdk/provider-utils@2.0.0-canary.0
16
+ - @ai-sdk/ui-utils@1.0.0-canary.2
17
+
18
+ ## 1.0.0-canary.1
19
+
20
+ ### Major Changes
21
+
22
+ - 8bf5756: chore: remove legacy function/tool calling
23
+
24
+ ### Patch Changes
25
+
26
+ - Updated dependencies [8bf5756]
27
+ - @ai-sdk/ui-utils@1.0.0-canary.1
28
+
3
29
  ## 1.0.0-canary.0
4
30
 
5
31
  ### Major Changes
package/dist/index.d.mts CHANGED
@@ -112,17 +112,9 @@ type UseChatHelpers = {
112
112
  /** Set the data of the chat. You can use this to transform or clear the chat data. */
113
113
  setData: (data: JSONValue[] | undefined | ((data: JSONValue[] | undefined) => JSONValue[] | undefined)) => void;
114
114
  };
115
- declare function useChat({ api, id, initialMessages, initialInput, sendExtraMessageFields, experimental_onFunctionCall, experimental_onToolCall, onToolCall, experimental_prepareRequestBody, experimental_maxAutomaticRoundtrips, maxAutomaticRoundtrips, maxToolRoundtrips, maxSteps, streamMode, streamProtocol, onResponse, onFinish, onError, credentials, headers, body, generateId, fetch, keepLastMessageOnError, experimental_throttle: throttleWaitMs, }?: UseChatOptions & {
115
+ declare function useChat({ api, id, initialMessages, initialInput, sendExtraMessageFields, onToolCall, experimental_prepareRequestBody, maxSteps, streamProtocol, onResponse, onFinish, onError, credentials, headers, body, generateId, fetch, keepLastMessageOnError, experimental_throttle: throttleWaitMs, }?: UseChatOptions & {
116
116
  key?: string;
117
117
  /**
118
- @deprecated Use `maxToolRoundtrips` instead.
119
- */
120
- experimental_maxAutomaticRoundtrips?: number;
121
- /**
122
- @deprecated Use `maxToolRoundtrips` instead.
123
- */
124
- maxAutomaticRoundtrips?: number;
125
- /**
126
118
  * Experimental (React only). When a function is provided, it will be used
127
119
  * to prepare the request body for the chat API. This can be useful for
128
120
  * customizing the request body based on the messages and data in the chat.
@@ -142,21 +134,6 @@ declare function useChat({ api, id, initialMessages, initialInput, sendExtraMess
142
134
  */
143
135
  experimental_throttle?: number;
144
136
  /**
145
- Maximum number of automatic roundtrips for tool calls.
146
-
147
- An automatic tool call roundtrip is a call to the server with the
148
- tool call results when all tool calls in the last assistant
149
- message have results.
150
-
151
- A maximum number is required to prevent infinite loops in the
152
- case of misconfigured tools.
153
-
154
- By default, it's set to 0, which will disable the feature.
155
-
156
- @deprecated Use `maxSteps` instead (which is `maxToolRoundtrips` + 1).
157
- */
158
- maxToolRoundtrips?: number;
159
- /**
160
137
  Maximum number of sequential LLM calls (steps), e.g. when you use tool calls. Must be at least 1.
161
138
 
162
139
  A maximum number is required to prevent infinite loops in the case of misconfigured tools.
@@ -224,7 +201,7 @@ type UseCompletionHelpers = {
224
201
  /** Additional data added on the server via StreamData */
225
202
  data?: JSONValue[];
226
203
  };
227
- declare function useCompletion({ api, id, initialCompletion, initialInput, credentials, headers, body, streamMode, streamProtocol, fetch, onResponse, onFinish, onError, experimental_throttle: throttleWaitMs, }?: UseCompletionOptions & {
204
+ declare function useCompletion({ api, id, initialCompletion, initialInput, credentials, headers, body, streamProtocol, fetch, onResponse, onFinish, onError, experimental_throttle: throttleWaitMs, }?: UseCompletionOptions & {
228
205
  /**
229
206
  * Custom throttle wait in ms for the completion and data updates.
230
207
  * Default is undefined, which disables throttling.
package/dist/index.d.ts CHANGED
@@ -112,17 +112,9 @@ type UseChatHelpers = {
112
112
  /** Set the data of the chat. You can use this to transform or clear the chat data. */
113
113
  setData: (data: JSONValue[] | undefined | ((data: JSONValue[] | undefined) => JSONValue[] | undefined)) => void;
114
114
  };
115
- declare function useChat({ api, id, initialMessages, initialInput, sendExtraMessageFields, experimental_onFunctionCall, experimental_onToolCall, onToolCall, experimental_prepareRequestBody, experimental_maxAutomaticRoundtrips, maxAutomaticRoundtrips, maxToolRoundtrips, maxSteps, streamMode, streamProtocol, onResponse, onFinish, onError, credentials, headers, body, generateId, fetch, keepLastMessageOnError, experimental_throttle: throttleWaitMs, }?: UseChatOptions & {
115
+ declare function useChat({ api, id, initialMessages, initialInput, sendExtraMessageFields, onToolCall, experimental_prepareRequestBody, maxSteps, streamProtocol, onResponse, onFinish, onError, credentials, headers, body, generateId, fetch, keepLastMessageOnError, experimental_throttle: throttleWaitMs, }?: UseChatOptions & {
116
116
  key?: string;
117
117
  /**
118
- @deprecated Use `maxToolRoundtrips` instead.
119
- */
120
- experimental_maxAutomaticRoundtrips?: number;
121
- /**
122
- @deprecated Use `maxToolRoundtrips` instead.
123
- */
124
- maxAutomaticRoundtrips?: number;
125
- /**
126
118
  * Experimental (React only). When a function is provided, it will be used
127
119
  * to prepare the request body for the chat API. This can be useful for
128
120
  * customizing the request body based on the messages and data in the chat.
@@ -142,21 +134,6 @@ declare function useChat({ api, id, initialMessages, initialInput, sendExtraMess
142
134
  */
143
135
  experimental_throttle?: number;
144
136
  /**
145
- Maximum number of automatic roundtrips for tool calls.
146
-
147
- An automatic tool call roundtrip is a call to the server with the
148
- tool call results when all tool calls in the last assistant
149
- message have results.
150
-
151
- A maximum number is required to prevent infinite loops in the
152
- case of misconfigured tools.
153
-
154
- By default, it's set to 0, which will disable the feature.
155
-
156
- @deprecated Use `maxSteps` instead (which is `maxToolRoundtrips` + 1).
157
- */
158
- maxToolRoundtrips?: number;
159
- /**
160
137
  Maximum number of sequential LLM calls (steps), e.g. when you use tool calls. Must be at least 1.
161
138
 
162
139
  A maximum number is required to prevent infinite loops in the case of misconfigured tools.
@@ -224,7 +201,7 @@ type UseCompletionHelpers = {
224
201
  /** Additional data added on the server via StreamData */
225
202
  data?: JSONValue[];
226
203
  };
227
- declare function useCompletion({ api, id, initialCompletion, initialInput, credentials, headers, body, streamMode, streamProtocol, fetch, onResponse, onFinish, onError, experimental_throttle: throttleWaitMs, }?: UseCompletionOptions & {
204
+ declare function useCompletion({ api, id, initialCompletion, initialInput, credentials, headers, body, streamProtocol, fetch, onResponse, onFinish, onError, experimental_throttle: throttleWaitMs, }?: UseCompletionOptions & {
228
205
  /**
229
206
  * Custom throttle wait in ms for the completion and data updates.
230
207
  * Default is undefined, which disables throttling.
package/dist/index.js CHANGED
@@ -223,7 +223,7 @@ function throttle(fn, waitMs) {
223
223
  }
224
224
 
225
225
  // src/use-chat.ts
226
- var getStreamedResponse = async (api, chatRequest, mutate, mutateStreamData, existingDataRef, extraMetadataRef, messagesRef, abortControllerRef, generateId2, streamProtocol, onFinish, onResponse, onToolCall, sendExtraMessageFields, experimental_prepareRequestBody, fetch2, keepLastMessageOnError) => {
226
+ var processResponseStream = async (api, chatRequest, mutate, mutateStreamData, existingDataRef, extraMetadataRef, messagesRef, abortControllerRef, generateId2, streamProtocol, onFinish, onResponse, onToolCall, sendExtraMessageFields, experimental_prepareRequestBody, fetch2, keepLastMessageOnError) => {
227
227
  var _a;
228
228
  const previousMessages = messagesRef.current;
229
229
  mutate(chatRequest.messages, false);
@@ -232,27 +232,18 @@ var getStreamedResponse = async (api, chatRequest, mutate, mutateStreamData, exi
232
232
  role,
233
233
  content,
234
234
  experimental_attachments,
235
- name,
236
235
  data,
237
236
  annotations,
238
- toolInvocations,
239
- function_call,
240
- tool_calls,
241
- tool_call_id
237
+ toolInvocations
242
238
  }) => ({
243
239
  role,
244
240
  content,
245
241
  ...experimental_attachments !== void 0 && {
246
242
  experimental_attachments
247
243
  },
248
- ...name !== void 0 && { name },
249
244
  ...data !== void 0 && { data },
250
245
  ...annotations !== void 0 && { annotations },
251
- ...toolInvocations !== void 0 && { toolInvocations },
252
- // outdated function/tool call handling (TODO deprecate):
253
- tool_call_id,
254
- ...function_call !== void 0 && { function_call },
255
- ...tool_calls !== void 0 && { tool_calls }
246
+ ...toolInvocations !== void 0 && { toolInvocations }
256
247
  })
257
248
  );
258
249
  const existingData = existingDataRef.current;
@@ -266,19 +257,7 @@ var getStreamedResponse = async (api, chatRequest, mutate, mutateStreamData, exi
266
257
  messages: constructedMessagesPayload,
267
258
  data: chatRequest.data,
268
259
  ...extraMetadataRef.current.body,
269
- ...chatRequest.body,
270
- ...chatRequest.functions !== void 0 && {
271
- functions: chatRequest.functions
272
- },
273
- ...chatRequest.function_call !== void 0 && {
274
- function_call: chatRequest.function_call
275
- },
276
- ...chatRequest.tools !== void 0 && {
277
- tools: chatRequest.tools
278
- },
279
- ...chatRequest.tool_choice !== void 0 && {
280
- tool_choice: chatRequest.tool_choice
281
- }
260
+ ...chatRequest.body
282
261
  },
283
262
  streamProtocol,
284
263
  credentials: extraMetadataRef.current.credentials,
@@ -309,16 +288,10 @@ function useChat({
309
288
  initialMessages,
310
289
  initialInput = "",
311
290
  sendExtraMessageFields,
312
- experimental_onFunctionCall,
313
- experimental_onToolCall,
314
291
  onToolCall,
315
292
  experimental_prepareRequestBody,
316
- experimental_maxAutomaticRoundtrips = 0,
317
- maxAutomaticRoundtrips = experimental_maxAutomaticRoundtrips,
318
- maxToolRoundtrips = maxAutomaticRoundtrips,
319
- maxSteps = maxToolRoundtrips != null ? maxToolRoundtrips + 1 : 1,
320
- streamMode,
321
- streamProtocol,
293
+ maxSteps = 1,
294
+ streamProtocol = "data",
322
295
  onResponse,
323
296
  onFinish,
324
297
  onError,
@@ -330,9 +303,6 @@ function useChat({
330
303
  keepLastMessageOnError = false,
331
304
  experimental_throttle: throttleWaitMs
332
305
  } = {}) {
333
- if (streamMode) {
334
- streamProtocol != null ? streamProtocol : streamProtocol = streamMode === "text" ? "text" : void 0;
335
- }
336
306
  const hookId = (0, import_react2.useId)();
337
307
  const idKey = id != null ? id : hookId;
338
308
  const chatKey = typeof api === "string" ? [api, idKey] : idKey;
@@ -377,34 +347,26 @@ function useChat({
377
347
  setError(void 0);
378
348
  const abortController = new AbortController();
379
349
  abortControllerRef.current = abortController;
380
- await (0, import_ui_utils2.processChatStream)({
381
- getStreamedResponse: () => getStreamedResponse(
382
- api,
383
- chatRequest,
384
- // throttle streamed ui updates:
385
- throttle(mutate, throttleWaitMs),
386
- throttle(mutateStreamData, throttleWaitMs),
387
- streamDataRef,
388
- extraMetadataRef,
389
- messagesRef,
390
- abortControllerRef,
391
- generateId2,
392
- streamProtocol,
393
- onFinish,
394
- onResponse,
395
- onToolCall,
396
- sendExtraMessageFields,
397
- experimental_prepareRequestBody,
398
- fetch2,
399
- keepLastMessageOnError
400
- ),
401
- experimental_onFunctionCall,
402
- experimental_onToolCall,
403
- updateChatRequest: (chatRequestParam) => {
404
- chatRequest = chatRequestParam;
405
- },
406
- getCurrentMessages: () => messagesRef.current
407
- });
350
+ await processResponseStream(
351
+ api,
352
+ chatRequest,
353
+ // throttle streamed ui updates:
354
+ throttle(mutate, throttleWaitMs),
355
+ throttle(mutateStreamData, throttleWaitMs),
356
+ streamDataRef,
357
+ extraMetadataRef,
358
+ messagesRef,
359
+ abortControllerRef,
360
+ generateId2,
361
+ streamProtocol,
362
+ onFinish,
363
+ onResponse,
364
+ onToolCall,
365
+ sendExtraMessageFields,
366
+ experimental_prepareRequestBody,
367
+ fetch2,
368
+ keepLastMessageOnError
369
+ );
408
370
  abortControllerRef.current = null;
409
371
  } catch (err) {
410
372
  if (err.name === "AbortError") {
@@ -444,8 +406,6 @@ function useChat({
444
406
  streamDataRef,
445
407
  streamProtocol,
446
408
  sendExtraMessageFields,
447
- experimental_onFunctionCall,
448
- experimental_onToolCall,
449
409
  experimental_prepareRequestBody,
450
410
  onToolCall,
451
411
  maxSteps,
@@ -459,11 +419,6 @@ function useChat({
459
419
  );
460
420
  const append = (0, import_react2.useCallback)(
461
421
  async (message, {
462
- options,
463
- functions,
464
- function_call,
465
- tools,
466
- tool_choice,
467
422
  data,
468
423
  headers: headers2,
469
424
  body: body2,
@@ -476,75 +431,29 @@ function useChat({
476
431
  const attachmentsForRequest = await prepareAttachmentsForRequest(
477
432
  experimental_attachments
478
433
  );
479
- const requestOptions = {
480
- headers: headers2 != null ? headers2 : options == null ? void 0 : options.headers,
481
- body: body2 != null ? body2 : options == null ? void 0 : options.body
482
- };
483
434
  const messages2 = messagesRef.current.concat({
484
435
  ...message,
485
436
  id: (_a = message.id) != null ? _a : generateId2(),
486
437
  createdAt: (_b = message.createdAt) != null ? _b : /* @__PURE__ */ new Date(),
487
438
  experimental_attachments: attachmentsForRequest.length > 0 ? attachmentsForRequest : void 0
488
439
  });
489
- const chatRequest = {
490
- messages: messages2,
491
- options: requestOptions,
492
- headers: requestOptions.headers,
493
- body: requestOptions.body,
494
- data,
495
- ...functions !== void 0 && { functions },
496
- ...function_call !== void 0 && { function_call },
497
- ...tools !== void 0 && { tools },
498
- ...tool_choice !== void 0 && { tool_choice }
499
- };
500
- return triggerRequest(chatRequest);
440
+ return triggerRequest({ messages: messages2, headers: headers2, body: body2, data });
501
441
  },
502
442
  [triggerRequest, generateId2]
503
443
  );
504
444
  const reload = (0, import_react2.useCallback)(
505
- async ({
506
- options,
507
- functions,
508
- function_call,
509
- tools,
510
- tool_choice,
511
- data,
512
- headers: headers2,
513
- body: body2
514
- } = {}) => {
515
- if (messagesRef.current.length === 0)
445
+ async ({ data, headers: headers2, body: body2 } = {}) => {
446
+ const messages2 = messagesRef.current;
447
+ if (messages2.length === 0) {
516
448
  return null;
517
- const requestOptions = {
518
- headers: headers2 != null ? headers2 : options == null ? void 0 : options.headers,
519
- body: body2 != null ? body2 : options == null ? void 0 : options.body
520
- };
521
- const lastMessage = messagesRef.current[messagesRef.current.length - 1];
522
- if (lastMessage.role === "assistant") {
523
- const chatRequest2 = {
524
- messages: messagesRef.current.slice(0, -1),
525
- options: requestOptions,
526
- headers: requestOptions.headers,
527
- body: requestOptions.body,
528
- data,
529
- ...functions !== void 0 && { functions },
530
- ...function_call !== void 0 && { function_call },
531
- ...tools !== void 0 && { tools },
532
- ...tool_choice !== void 0 && { tool_choice }
533
- };
534
- return triggerRequest(chatRequest2);
535
449
  }
536
- const chatRequest = {
537
- messages: messagesRef.current,
538
- options: requestOptions,
539
- headers: requestOptions.headers,
540
- body: requestOptions.body,
541
- data,
542
- ...functions !== void 0 && { functions },
543
- ...function_call !== void 0 && { function_call },
544
- ...tools !== void 0 && { tools },
545
- ...tool_choice !== void 0 && { tool_choice }
546
- };
547
- return triggerRequest(chatRequest);
450
+ const lastMessage = messages2[messages2.length - 1];
451
+ return triggerRequest({
452
+ messages: lastMessage.role === "assistant" ? messages2.slice(0, -1) : messages2,
453
+ headers: headers2,
454
+ body: body2,
455
+ data
456
+ });
548
457
  },
549
458
  [triggerRequest]
550
459
  );
@@ -577,7 +486,7 @@ function useChat({
577
486
  const [input, setInput] = (0, import_react2.useState)(initialInput);
578
487
  const handleSubmit = (0, import_react2.useCallback)(
579
488
  async (event, options = {}, metadata) => {
580
- var _a, _b, _c, _d, _e;
489
+ var _a;
581
490
  (_a = event == null ? void 0 : event.preventDefault) == null ? void 0 : _a.call(event);
582
491
  if (!input && !options.allowEmptySubmit)
583
492
  return;
@@ -590,10 +499,6 @@ function useChat({
590
499
  const attachmentsForRequest = await prepareAttachmentsForRequest(
591
500
  options.experimental_attachments
592
501
  );
593
- const requestOptions = {
594
- headers: (_c = options.headers) != null ? _c : (_b = options.options) == null ? void 0 : _b.headers,
595
- body: (_e = options.body) != null ? _e : (_d = options.options) == null ? void 0 : _d.body
596
- };
597
502
  const messages2 = !input && !attachmentsForRequest.length && options.allowEmptySubmit ? messagesRef.current : messagesRef.current.concat({
598
503
  id: generateId2(),
599
504
  createdAt: /* @__PURE__ */ new Date(),
@@ -603,9 +508,8 @@ function useChat({
603
508
  });
604
509
  const chatRequest = {
605
510
  messages: messages2,
606
- options: requestOptions,
607
- headers: requestOptions.headers,
608
- body: requestOptions.body,
511
+ headers: options.headers,
512
+ body: options.body,
609
513
  data: options.data
610
514
  };
611
515
  triggerRequest(chatRequest);
@@ -712,17 +616,13 @@ function useCompletion({
712
616
  credentials,
713
617
  headers,
714
618
  body,
715
- streamMode,
716
- streamProtocol,
619
+ streamProtocol = "data",
717
620
  fetch: fetch2,
718
621
  onResponse,
719
622
  onFinish,
720
623
  onError,
721
624
  experimental_throttle: throttleWaitMs
722
625
  } = {}) {
723
- if (streamMode) {
724
- streamProtocol != null ? streamProtocol : streamProtocol = streamMode === "text" ? "text" : void 0;
725
- }
726
626
  const hookId = (0, import_react3.useId)();
727
627
  const completionId = id || hookId;
728
628
  const { data, mutate } = (0, import_swr2.default)([api, completionId], null, {