ai 2.2.25 → 2.2.26

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.
@@ -149,9 +149,6 @@ function createChunkDecoder(complex) {
149
149
  }
150
150
  var COMPLEX_HEADER = "X-Experimental-Stream-Data";
151
151
 
152
- // shared/call-api.ts
153
- import { nanoid as nanoid2 } from "nanoid";
154
-
155
152
  // shared/parse-complex-response.ts
156
153
  async function parseComplexResponse({
157
154
  reader,
@@ -258,7 +255,8 @@ async function callApi({
258
255
  restoreMessagesOnFailure,
259
256
  onResponse,
260
257
  onUpdate,
261
- onFinish
258
+ onFinish,
259
+ generateId
262
260
  }) {
263
261
  var _a;
264
262
  const response = await fetch(api, {
@@ -267,7 +265,10 @@ async function callApi({
267
265
  messages,
268
266
  ...body
269
267
  }),
270
- headers,
268
+ headers: {
269
+ "Content-Type": "application/json",
270
+ ...headers
271
+ },
271
272
  signal: (_a = abortController == null ? void 0 : abortController()) == null ? void 0 : _a.signal,
272
273
  credentials
273
274
  }).catch((err) => {
@@ -301,13 +302,14 @@ async function callApi({
301
302
  if (onFinish && prefixMap.text != null) {
302
303
  onFinish(prefixMap.text);
303
304
  }
304
- }
305
+ },
306
+ generateId
305
307
  });
306
308
  } else {
307
309
  const createdAt = /* @__PURE__ */ new Date();
308
310
  const decode = createChunkDecoder(false);
309
311
  let streamedResponse = "";
310
- const replyId = nanoid2();
312
+ const replyId = generateId();
311
313
  let responseMessage = {
312
314
  id: replyId,
313
315
  createdAt,
@@ -392,7 +394,7 @@ async function processChatStream({
392
394
  }
393
395
 
394
396
  // react/use-chat.ts
395
- var getStreamedResponse = async (api, chatRequest, mutate, mutateStreamData, existingData, extraMetadataRef, messagesRef, abortControllerRef, onFinish, onResponse, sendExtraMessageFields) => {
397
+ var getStreamedResponse = async (api, chatRequest, mutate, mutateStreamData, existingData, extraMetadataRef, messagesRef, abortControllerRef, generateId, onFinish, onResponse, sendExtraMessageFields) => {
396
398
  var _a, _b;
397
399
  const previousMessages = messagesRef.current;
398
400
  mutate(chatRequest.messages, false);
@@ -405,7 +407,7 @@ var getStreamedResponse = async (api, chatRequest, mutate, mutateStreamData, exi
405
407
  }
406
408
  }));
407
409
  if (typeof api !== "string") {
408
- const replyId = nanoid();
410
+ const replyId = generateId();
409
411
  const createdAt = /* @__PURE__ */ new Date();
410
412
  let responseMessage = {
411
413
  id: replyId,
@@ -468,7 +470,8 @@ var getStreamedResponse = async (api, chatRequest, mutate, mutateStreamData, exi
468
470
  mutate([...chatRequest.messages, ...merged], false);
469
471
  mutateStreamData([...existingData || [], ...data || []], false);
470
472
  },
471
- onFinish
473
+ onFinish,
474
+ generateId
472
475
  });
473
476
  };
474
477
  function useChat({
@@ -483,7 +486,8 @@ function useChat({
483
486
  onError,
484
487
  credentials,
485
488
  headers,
486
- body
489
+ body,
490
+ generateId = nanoid
487
491
  } = {}) {
488
492
  const hookId = useId();
489
493
  const chatId = id || hookId;
@@ -531,6 +535,7 @@ function useChat({
531
535
  extraMetadataRef,
532
536
  messagesRef,
533
537
  abortControllerRef,
538
+ generateId,
534
539
  onFinish,
535
540
  onResponse,
536
541
  sendExtraMessageFields
@@ -569,13 +574,14 @@ function useChat({
569
574
  sendExtraMessageFields,
570
575
  experimental_onFunctionCall,
571
576
  messagesRef.current,
572
- abortControllerRef.current
577
+ abortControllerRef.current,
578
+ generateId
573
579
  ]
574
580
  );
575
581
  const append = useCallback(
576
582
  async (message, { options, functions, function_call, data } = {}) => {
577
583
  if (!message.id) {
578
- message.id = nanoid();
584
+ message.id = generateId();
579
585
  }
580
586
  const chatRequest = {
581
587
  messages: messagesRef.current.concat(message),
@@ -586,7 +592,7 @@ function useChat({
586
592
  };
587
593
  return triggerRequest(chatRequest);
588
594
  },
589
- [triggerRequest]
595
+ [triggerRequest, generateId]
590
596
  );
591
597
  const reload = useCallback(
592
598
  async ({ options, functions, function_call } = {}) => {
@@ -671,6 +677,29 @@ function useChat({
671
677
  // react/use-completion.ts
672
678
  import { useCallback as useCallback2, useEffect as useEffect2, useId as useId2, useRef as useRef2, useState as useState2 } from "react";
673
679
  import useSWR2 from "swr";
680
+
681
+ // shared/process-message-stream.ts
682
+ async function processMessageStream(reader, processMessage) {
683
+ const decoder = new TextDecoder();
684
+ let buffer = "";
685
+ while (true) {
686
+ const { done, value } = await reader.read();
687
+ if (done) {
688
+ if (buffer.length > 0) {
689
+ processMessage(buffer);
690
+ }
691
+ break;
692
+ }
693
+ buffer += decoder.decode(value, { stream: true });
694
+ let endIndex;
695
+ while ((endIndex = buffer.indexOf("\n")) !== -1) {
696
+ processMessage(buffer.substring(0, endIndex).trim());
697
+ buffer = buffer.substring(endIndex + 1);
698
+ }
699
+ }
700
+ }
701
+
702
+ // react/use-completion.ts
674
703
  function useCompletion({
675
704
  api = "/api/completion",
676
705
  id,
@@ -748,17 +777,28 @@ function useCompletion({
748
777
  }
749
778
  let result = "";
750
779
  const reader = res.body.getReader();
751
- const decoder = createChunkDecoder();
752
- while (true) {
753
- const { done, value } = await reader.read();
754
- if (done) {
755
- break;
756
- }
757
- result += decoder(value);
758
- mutate(result, false);
759
- if (abortController2 === null) {
760
- reader.cancel();
761
- break;
780
+ const isComplexMode = res.headers.get(COMPLEX_HEADER) === "true";
781
+ if (isComplexMode) {
782
+ await processMessageStream(reader, (message) => {
783
+ const { type, value } = parseStreamPart(message);
784
+ if (type === "text") {
785
+ result += value;
786
+ mutate(result, false);
787
+ }
788
+ });
789
+ } else {
790
+ const decoder = createChunkDecoder();
791
+ while (true) {
792
+ const { done, value } = await reader.read();
793
+ if (done) {
794
+ break;
795
+ }
796
+ result += decoder(value);
797
+ mutate(result, false);
798
+ if (abortController2 === null) {
799
+ reader.cancel();
800
+ break;
801
+ }
762
802
  }
763
803
  }
764
804
  if (onFinish) {
@@ -840,29 +880,6 @@ function useCompletion({
840
880
 
841
881
  // react/use-assistant.ts
842
882
  import { useState as useState3 } from "react";
843
-
844
- // shared/process-message-stream.ts
845
- async function processMessageStream(reader, processMessage) {
846
- const decoder = new TextDecoder();
847
- let buffer = "";
848
- while (true) {
849
- const { done, value } = await reader.read();
850
- if (done) {
851
- if (buffer.length > 0) {
852
- processMessage(buffer);
853
- }
854
- break;
855
- }
856
- buffer += decoder.decode(value, { stream: true });
857
- let endIndex;
858
- while ((endIndex = buffer.indexOf("\n")) !== -1) {
859
- processMessage(buffer.substring(0, endIndex).trim());
860
- buffer = buffer.substring(endIndex + 1);
861
- }
862
- }
863
- }
864
-
865
- // react/use-assistant.ts
866
883
  function experimental_useAssistant({
867
884
  api,
868
885
  threadId: threadIdParam
@@ -872,12 +889,12 @@ function experimental_useAssistant({
872
889
  const [threadId, setThreadId] = useState3(void 0);
873
890
  const [status, setStatus] = useState3("awaiting_message");
874
891
  const [error, setError] = useState3(void 0);
875
- const handleInputChange = (e) => {
876
- setInput(e.target.value);
892
+ const handleInputChange = (event) => {
893
+ setInput(event.target.value);
877
894
  };
878
- const submitMessage = async (e) => {
879
- var _a;
880
- e.preventDefault();
895
+ const submitMessage = async (event, requestOptions) => {
896
+ var _a, _b;
897
+ (_a = event == null ? void 0 : event.preventDefault) == null ? void 0 : _a.call(event);
881
898
  if (input === "") {
882
899
  return;
883
900
  }
@@ -892,8 +909,10 @@ function experimental_useAssistant({
892
909
  headers: { "Content-Type": "application/json" },
893
910
  body: JSON.stringify({
894
911
  // always use user-provided threadId when available:
895
- threadId: (_a = threadIdParam != null ? threadIdParam : threadId) != null ? _a : null,
896
- message: input
912
+ threadId: (_b = threadIdParam != null ? threadIdParam : threadId) != null ? _b : null,
913
+ message: input,
914
+ // optional request data:
915
+ data: requestOptions == null ? void 0 : requestOptions.data
897
916
  })
898
917
  });
899
918
  if (result.body == null) {
@@ -35,6 +35,7 @@ interface Function {
35
35
  */
36
36
  description?: string;
37
37
  }
38
+ type IdGenerator = () => string;
38
39
  /**
39
40
  * Shared types between the API and UI packages.
40
41
  */
@@ -109,6 +110,11 @@ type UseChatOptions = {
109
110
  * Callback function to be called when an error is encountered.
110
111
  */
111
112
  onError?: (error: Error) => void;
113
+ /**
114
+ * A way to provide a function that is going to be used for ids for messages.
115
+ * If not provided nanoid is used by default.
116
+ */
117
+ generateId?: IdGenerator;
112
118
  /**
113
119
  * The credentials mode to be used for the fetch request.
114
120
  * Possible values are: 'omit', 'same-origin', 'include'.
@@ -238,7 +244,7 @@ type UseChatHelpers = {
238
244
  /** Additional data added on the server via StreamData */
239
245
  data: Accessor<JSONValue[] | undefined>;
240
246
  };
241
- declare function useChat({ api, id, initialMessages, initialInput, sendExtraMessageFields, experimental_onFunctionCall, onResponse, onFinish, onError, credentials, headers, body, }?: UseChatOptions): UseChatHelpers;
247
+ declare function useChat({ api, id, initialMessages, initialInput, sendExtraMessageFields, experimental_onFunctionCall, onResponse, onFinish, onError, credentials, headers, body, generateId, }?: UseChatOptions): UseChatHelpers;
242
248
 
243
249
  type UseCompletionHelpers = {
244
250
  /** The current completion result */
@@ -30,9 +30,6 @@ var import_solid_js = require("solid-js");
30
30
  var import_solid_swr_store = require("solid-swr-store");
31
31
  var import_swr_store = require("swr-store");
32
32
 
33
- // shared/call-api.ts
34
- var import_nanoid = require("nanoid");
35
-
36
33
  // shared/utils.ts
37
34
  var import_non_secure = require("nanoid/non-secure");
38
35
 
@@ -284,7 +281,8 @@ async function callApi({
284
281
  restoreMessagesOnFailure,
285
282
  onResponse,
286
283
  onUpdate,
287
- onFinish
284
+ onFinish,
285
+ generateId
288
286
  }) {
289
287
  var _a;
290
288
  const response = await fetch(api, {
@@ -293,7 +291,10 @@ async function callApi({
293
291
  messages,
294
292
  ...body
295
293
  }),
296
- headers,
294
+ headers: {
295
+ "Content-Type": "application/json",
296
+ ...headers
297
+ },
297
298
  signal: (_a = abortController == null ? void 0 : abortController()) == null ? void 0 : _a.signal,
298
299
  credentials
299
300
  }).catch((err) => {
@@ -327,13 +328,14 @@ async function callApi({
327
328
  if (onFinish && prefixMap.text != null) {
328
329
  onFinish(prefixMap.text);
329
330
  }
330
- }
331
+ },
332
+ generateId
331
333
  });
332
334
  } else {
333
335
  const createdAt = /* @__PURE__ */ new Date();
334
336
  const decode = createChunkDecoder(false);
335
337
  let streamedResponse = "";
336
- const replyId = (0, import_nanoid.nanoid)();
338
+ const replyId = generateId();
337
339
  let responseMessage = {
338
340
  id: replyId,
339
341
  createdAt,
@@ -438,7 +440,8 @@ function useChat({
438
440
  onError,
439
441
  credentials,
440
442
  headers,
441
- body
443
+ body,
444
+ generateId = nanoid
442
445
  } = {}) {
443
446
  const chatId = id || `chat-${uniqueId++}`;
444
447
  const key = `${api}|${chatId}`;
@@ -511,7 +514,8 @@ function useChat({
511
514
  if (previousMessages.status === "success") {
512
515
  mutate(previousMessages.data);
513
516
  }
514
- }
517
+ },
518
+ generateId
515
519
  });
516
520
  },
517
521
  experimental_onFunctionCall,
@@ -537,7 +541,7 @@ function useChat({
537
541
  const append = async (message, options) => {
538
542
  var _a;
539
543
  if (!message.id) {
540
- message.id = nanoid();
544
+ message.id = generateId();
541
545
  }
542
546
  return triggerRequest(
543
547
  ((_a = messages()) != null ? _a : []).concat(message),
@@ -3,9 +3,6 @@ import { createSignal } from "solid-js";
3
3
  import { useSWRStore } from "solid-swr-store";
4
4
  import { createSWRStore } from "swr-store";
5
5
 
6
- // shared/call-api.ts
7
- import { nanoid as nanoid2 } from "nanoid";
8
-
9
6
  // shared/utils.ts
10
7
  import { customAlphabet } from "nanoid/non-secure";
11
8
 
@@ -257,7 +254,8 @@ async function callApi({
257
254
  restoreMessagesOnFailure,
258
255
  onResponse,
259
256
  onUpdate,
260
- onFinish
257
+ onFinish,
258
+ generateId
261
259
  }) {
262
260
  var _a;
263
261
  const response = await fetch(api, {
@@ -266,7 +264,10 @@ async function callApi({
266
264
  messages,
267
265
  ...body
268
266
  }),
269
- headers,
267
+ headers: {
268
+ "Content-Type": "application/json",
269
+ ...headers
270
+ },
270
271
  signal: (_a = abortController == null ? void 0 : abortController()) == null ? void 0 : _a.signal,
271
272
  credentials
272
273
  }).catch((err) => {
@@ -300,13 +301,14 @@ async function callApi({
300
301
  if (onFinish && prefixMap.text != null) {
301
302
  onFinish(prefixMap.text);
302
303
  }
303
- }
304
+ },
305
+ generateId
304
306
  });
305
307
  } else {
306
308
  const createdAt = /* @__PURE__ */ new Date();
307
309
  const decode = createChunkDecoder(false);
308
310
  let streamedResponse = "";
309
- const replyId = nanoid2();
311
+ const replyId = generateId();
310
312
  let responseMessage = {
311
313
  id: replyId,
312
314
  createdAt,
@@ -411,7 +413,8 @@ function useChat({
411
413
  onError,
412
414
  credentials,
413
415
  headers,
414
- body
416
+ body,
417
+ generateId = nanoid
415
418
  } = {}) {
416
419
  const chatId = id || `chat-${uniqueId++}`;
417
420
  const key = `${api}|${chatId}`;
@@ -484,7 +487,8 @@ function useChat({
484
487
  if (previousMessages.status === "success") {
485
488
  mutate(previousMessages.data);
486
489
  }
487
- }
490
+ },
491
+ generateId
488
492
  });
489
493
  },
490
494
  experimental_onFunctionCall,
@@ -510,7 +514,7 @@ function useChat({
510
514
  const append = async (message, options) => {
511
515
  var _a;
512
516
  if (!message.id) {
513
- message.id = nanoid();
517
+ message.id = generateId();
514
518
  }
515
519
  return triggerRequest(
516
520
  ((_a = messages()) != null ? _a : []).concat(message),
@@ -35,6 +35,7 @@ interface Function {
35
35
  */
36
36
  description?: string;
37
37
  }
38
+ type IdGenerator = () => string;
38
39
  /**
39
40
  * Shared types between the API and UI packages.
40
41
  */
@@ -115,6 +116,11 @@ type UseChatOptions = {
115
116
  * Callback function to be called when an error is encountered.
116
117
  */
117
118
  onError?: (error: Error) => void;
119
+ /**
120
+ * A way to provide a function that is going to be used for ids for messages.
121
+ * If not provided nanoid is used by default.
122
+ */
123
+ generateId?: IdGenerator;
118
124
  /**
119
125
  * The credentials mode to be used for the fetch request.
120
126
  * Possible values are: 'omit', 'same-origin', 'include'.
@@ -243,7 +249,7 @@ type UseChatHelpers = {
243
249
  /** Additional data added on the server via StreamData */
244
250
  data: Readable<JSONValue[] | undefined>;
245
251
  };
246
- declare function useChat({ api, id, initialMessages, initialInput, sendExtraMessageFields, experimental_onFunctionCall, onResponse, onFinish, onError, credentials, headers, body, }?: UseChatOptions): UseChatHelpers;
252
+ declare function useChat({ api, id, initialMessages, initialInput, sendExtraMessageFields, experimental_onFunctionCall, onResponse, onFinish, onError, credentials, headers, body, generateId, }?: UseChatOptions): UseChatHelpers;
247
253
 
248
254
  type UseCompletionHelpers = {
249
255
  /** The current completion result */
@@ -525,9 +525,6 @@ var F2 = (t, e) => c.useSWR(t, e);
525
525
  // svelte/use-chat.ts
526
526
  var import_store = require("svelte/store");
527
527
 
528
- // shared/call-api.ts
529
- var import_nanoid = require("nanoid");
530
-
531
528
  // shared/utils.ts
532
529
  var import_non_secure = require("nanoid/non-secure");
533
530
 
@@ -779,7 +776,8 @@ async function callApi({
779
776
  restoreMessagesOnFailure,
780
777
  onResponse,
781
778
  onUpdate,
782
- onFinish
779
+ onFinish,
780
+ generateId
783
781
  }) {
784
782
  var _a;
785
783
  const response = await fetch(api, {
@@ -788,7 +786,10 @@ async function callApi({
788
786
  messages,
789
787
  ...body
790
788
  }),
791
- headers,
789
+ headers: {
790
+ "Content-Type": "application/json",
791
+ ...headers
792
+ },
792
793
  signal: (_a = abortController == null ? void 0 : abortController()) == null ? void 0 : _a.signal,
793
794
  credentials
794
795
  }).catch((err) => {
@@ -822,13 +823,14 @@ async function callApi({
822
823
  if (onFinish && prefixMap.text != null) {
823
824
  onFinish(prefixMap.text);
824
825
  }
825
- }
826
+ },
827
+ generateId
826
828
  });
827
829
  } else {
828
830
  const createdAt = /* @__PURE__ */ new Date();
829
831
  const decode = createChunkDecoder(false);
830
832
  let streamedResponse = "";
831
- const replyId = (0, import_nanoid.nanoid)();
833
+ const replyId = generateId();
832
834
  let responseMessage = {
833
835
  id: replyId,
834
836
  createdAt,
@@ -913,7 +915,7 @@ async function processChatStream({
913
915
  }
914
916
 
915
917
  // svelte/use-chat.ts
916
- var getStreamedResponse = async (api, chatRequest, mutate, mutateStreamData, existingData, extraMetadata, previousMessages, abortControllerRef, onFinish, onResponse, sendExtraMessageFields) => {
918
+ var getStreamedResponse = async (api, chatRequest, mutate, mutateStreamData, existingData, extraMetadata, previousMessages, abortControllerRef, generateId, onFinish, onResponse, sendExtraMessageFields) => {
917
919
  var _a, _b;
918
920
  mutate(chatRequest.messages);
919
921
  const constructedMessagesPayload = sendExtraMessageFields ? chatRequest.messages : chatRequest.messages.map(({ role, content, name, function_call }) => ({
@@ -954,7 +956,8 @@ var getStreamedResponse = async (api, chatRequest, mutate, mutateStreamData, exi
954
956
  mutate([...chatRequest.messages, ...merged]);
955
957
  mutateStreamData([...existingData || [], ...data || []]);
956
958
  },
957
- onFinish
959
+ onFinish,
960
+ generateId
958
961
  });
959
962
  };
960
963
  var uniqueId = 0;
@@ -971,7 +974,8 @@ function useChat({
971
974
  onError,
972
975
  credentials,
973
976
  headers,
974
- body
977
+ body,
978
+ generateId = nanoid
975
979
  } = {}) {
976
980
  const chatId = id || `chat-${uniqueId++}`;
977
981
  const key = `${api}|${chatId}`;
@@ -1015,6 +1019,7 @@ function useChat({
1015
1019
  extraMetadata,
1016
1020
  (0, import_store.get)(messages),
1017
1021
  abortController,
1022
+ generateId,
1018
1023
  onFinish,
1019
1024
  onResponse,
1020
1025
  sendExtraMessageFields
@@ -1042,7 +1047,7 @@ function useChat({
1042
1047
  }
1043
1048
  const append = async (message, { options, functions, function_call } = {}) => {
1044
1049
  if (!message.id) {
1045
- message.id = nanoid();
1050
+ message.id = generateId();
1046
1051
  }
1047
1052
  const chatRequest = {
1048
1053
  messages: (0, import_store.get)(messages).concat(message),
@@ -498,9 +498,6 @@ var F2 = (t, e) => c.useSWR(t, e);
498
498
  // svelte/use-chat.ts
499
499
  import { derived, get, writable } from "svelte/store";
500
500
 
501
- // shared/call-api.ts
502
- import { nanoid as nanoid2 } from "nanoid";
503
-
504
501
  // shared/utils.ts
505
502
  import { customAlphabet } from "nanoid/non-secure";
506
503
 
@@ -752,7 +749,8 @@ async function callApi({
752
749
  restoreMessagesOnFailure,
753
750
  onResponse,
754
751
  onUpdate,
755
- onFinish
752
+ onFinish,
753
+ generateId
756
754
  }) {
757
755
  var _a;
758
756
  const response = await fetch(api, {
@@ -761,7 +759,10 @@ async function callApi({
761
759
  messages,
762
760
  ...body
763
761
  }),
764
- headers,
762
+ headers: {
763
+ "Content-Type": "application/json",
764
+ ...headers
765
+ },
765
766
  signal: (_a = abortController == null ? void 0 : abortController()) == null ? void 0 : _a.signal,
766
767
  credentials
767
768
  }).catch((err) => {
@@ -795,13 +796,14 @@ async function callApi({
795
796
  if (onFinish && prefixMap.text != null) {
796
797
  onFinish(prefixMap.text);
797
798
  }
798
- }
799
+ },
800
+ generateId
799
801
  });
800
802
  } else {
801
803
  const createdAt = /* @__PURE__ */ new Date();
802
804
  const decode = createChunkDecoder(false);
803
805
  let streamedResponse = "";
804
- const replyId = nanoid2();
806
+ const replyId = generateId();
805
807
  let responseMessage = {
806
808
  id: replyId,
807
809
  createdAt,
@@ -886,7 +888,7 @@ async function processChatStream({
886
888
  }
887
889
 
888
890
  // svelte/use-chat.ts
889
- var getStreamedResponse = async (api, chatRequest, mutate, mutateStreamData, existingData, extraMetadata, previousMessages, abortControllerRef, onFinish, onResponse, sendExtraMessageFields) => {
891
+ var getStreamedResponse = async (api, chatRequest, mutate, mutateStreamData, existingData, extraMetadata, previousMessages, abortControllerRef, generateId, onFinish, onResponse, sendExtraMessageFields) => {
890
892
  var _a, _b;
891
893
  mutate(chatRequest.messages);
892
894
  const constructedMessagesPayload = sendExtraMessageFields ? chatRequest.messages : chatRequest.messages.map(({ role, content, name, function_call }) => ({
@@ -927,7 +929,8 @@ var getStreamedResponse = async (api, chatRequest, mutate, mutateStreamData, exi
927
929
  mutate([...chatRequest.messages, ...merged]);
928
930
  mutateStreamData([...existingData || [], ...data || []]);
929
931
  },
930
- onFinish
932
+ onFinish,
933
+ generateId
931
934
  });
932
935
  };
933
936
  var uniqueId = 0;
@@ -944,7 +947,8 @@ function useChat({
944
947
  onError,
945
948
  credentials,
946
949
  headers,
947
- body
950
+ body,
951
+ generateId = nanoid
948
952
  } = {}) {
949
953
  const chatId = id || `chat-${uniqueId++}`;
950
954
  const key = `${api}|${chatId}`;
@@ -988,6 +992,7 @@ function useChat({
988
992
  extraMetadata,
989
993
  get(messages),
990
994
  abortController,
995
+ generateId,
991
996
  onFinish,
992
997
  onResponse,
993
998
  sendExtraMessageFields
@@ -1015,7 +1020,7 @@ function useChat({
1015
1020
  }
1016
1021
  const append = async (message, { options, functions, function_call } = {}) => {
1017
1022
  if (!message.id) {
1018
- message.id = nanoid();
1023
+ message.id = generateId();
1019
1024
  }
1020
1025
  const chatRequest = {
1021
1026
  messages: get(messages).concat(message),
@@ -35,6 +35,7 @@ interface Function {
35
35
  */
36
36
  description?: string;
37
37
  }
38
+ type IdGenerator = () => string;
38
39
  /**
39
40
  * Shared types between the API and UI packages.
40
41
  */
@@ -109,6 +110,11 @@ type UseChatOptions = {
109
110
  * Callback function to be called when an error is encountered.
110
111
  */
111
112
  onError?: (error: Error) => void;
113
+ /**
114
+ * A way to provide a function that is going to be used for ids for messages.
115
+ * If not provided nanoid is used by default.
116
+ */
117
+ generateId?: IdGenerator;
112
118
  /**
113
119
  * The credentials mode to be used for the fetch request.
114
120
  * Possible values are: 'omit', 'same-origin', 'include'.
@@ -195,6 +201,9 @@ type UseCompletionOptions = {
195
201
  */
196
202
  body?: object;
197
203
  };
204
+ type JSONValue = null | string | number | boolean | {
205
+ [x: string]: JSONValue;
206
+ } | Array<JSONValue>;
198
207
 
199
208
  type UseChatHelpers = {
200
209
  /** Current messages in the chat */
@@ -228,8 +237,10 @@ type UseChatHelpers = {
228
237
  handleSubmit: (e: any) => void;
229
238
  /** Whether the API request is in progress */
230
239
  isLoading: Ref<boolean | undefined>;
240
+ /** Additional data added on the server via StreamData */
241
+ data: Ref<JSONValue[] | undefined>;
231
242
  };
232
- declare function useChat({ api, id, initialMessages, initialInput, sendExtraMessageFields, onResponse, onFinish, onError, credentials, headers, body, }?: UseChatOptions): UseChatHelpers;
243
+ declare function useChat({ api, id, initialMessages, initialInput, sendExtraMessageFields, experimental_onFunctionCall, onResponse, onFinish, onError, credentials, headers, body, generateId, }?: UseChatOptions): UseChatHelpers;
233
244
 
234
245
  type UseCompletionHelpers = {
235
246
  /** The current completion result */