ai 2.2.24 → 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.
@@ -186,9 +186,6 @@ function createChunkDecoder(complex) {
186
186
  }
187
187
  var COMPLEX_HEADER = "X-Experimental-Stream-Data";
188
188
 
189
- // shared/call-api.ts
190
- var import_nanoid = require("nanoid");
191
-
192
189
  // shared/parse-complex-response.ts
193
190
  async function parseComplexResponse({
194
191
  reader,
@@ -295,7 +292,8 @@ async function callApi({
295
292
  restoreMessagesOnFailure,
296
293
  onResponse,
297
294
  onUpdate,
298
- onFinish
295
+ onFinish,
296
+ generateId
299
297
  }) {
300
298
  var _a;
301
299
  const response = await fetch(api, {
@@ -304,7 +302,10 @@ async function callApi({
304
302
  messages,
305
303
  ...body
306
304
  }),
307
- headers,
305
+ headers: {
306
+ "Content-Type": "application/json",
307
+ ...headers
308
+ },
308
309
  signal: (_a = abortController == null ? void 0 : abortController()) == null ? void 0 : _a.signal,
309
310
  credentials
310
311
  }).catch((err) => {
@@ -338,13 +339,14 @@ async function callApi({
338
339
  if (onFinish && prefixMap.text != null) {
339
340
  onFinish(prefixMap.text);
340
341
  }
341
- }
342
+ },
343
+ generateId
342
344
  });
343
345
  } else {
344
346
  const createdAt = /* @__PURE__ */ new Date();
345
347
  const decode = createChunkDecoder(false);
346
348
  let streamedResponse = "";
347
- const replyId = (0, import_nanoid.nanoid)();
349
+ const replyId = generateId();
348
350
  let responseMessage = {
349
351
  id: replyId,
350
352
  createdAt,
@@ -429,7 +431,7 @@ async function processChatStream({
429
431
  }
430
432
 
431
433
  // react/use-chat.ts
432
- var getStreamedResponse = async (api, chatRequest, mutate, mutateStreamData, existingData, extraMetadataRef, messagesRef, abortControllerRef, onFinish, onResponse, sendExtraMessageFields) => {
434
+ var getStreamedResponse = async (api, chatRequest, mutate, mutateStreamData, existingData, extraMetadataRef, messagesRef, abortControllerRef, generateId, onFinish, onResponse, sendExtraMessageFields) => {
433
435
  var _a, _b;
434
436
  const previousMessages = messagesRef.current;
435
437
  mutate(chatRequest.messages, false);
@@ -442,7 +444,7 @@ var getStreamedResponse = async (api, chatRequest, mutate, mutateStreamData, exi
442
444
  }
443
445
  }));
444
446
  if (typeof api !== "string") {
445
- const replyId = nanoid();
447
+ const replyId = generateId();
446
448
  const createdAt = /* @__PURE__ */ new Date();
447
449
  let responseMessage = {
448
450
  id: replyId,
@@ -505,7 +507,8 @@ var getStreamedResponse = async (api, chatRequest, mutate, mutateStreamData, exi
505
507
  mutate([...chatRequest.messages, ...merged], false);
506
508
  mutateStreamData([...existingData || [], ...data || []], false);
507
509
  },
508
- onFinish
510
+ onFinish,
511
+ generateId
509
512
  });
510
513
  };
511
514
  function useChat({
@@ -520,7 +523,8 @@ function useChat({
520
523
  onError,
521
524
  credentials,
522
525
  headers,
523
- body
526
+ body,
527
+ generateId = nanoid
524
528
  } = {}) {
525
529
  const hookId = (0, import_react.useId)();
526
530
  const chatId = id || hookId;
@@ -568,6 +572,7 @@ function useChat({
568
572
  extraMetadataRef,
569
573
  messagesRef,
570
574
  abortControllerRef,
575
+ generateId,
571
576
  onFinish,
572
577
  onResponse,
573
578
  sendExtraMessageFields
@@ -606,13 +611,14 @@ function useChat({
606
611
  sendExtraMessageFields,
607
612
  experimental_onFunctionCall,
608
613
  messagesRef.current,
609
- abortControllerRef.current
614
+ abortControllerRef.current,
615
+ generateId
610
616
  ]
611
617
  );
612
618
  const append = (0, import_react.useCallback)(
613
619
  async (message, { options, functions, function_call, data } = {}) => {
614
620
  if (!message.id) {
615
- message.id = nanoid();
621
+ message.id = generateId();
616
622
  }
617
623
  const chatRequest = {
618
624
  messages: messagesRef.current.concat(message),
@@ -623,7 +629,7 @@ function useChat({
623
629
  };
624
630
  return triggerRequest(chatRequest);
625
631
  },
626
- [triggerRequest]
632
+ [triggerRequest, generateId]
627
633
  );
628
634
  const reload = (0, import_react.useCallback)(
629
635
  async ({ options, functions, function_call } = {}) => {
@@ -708,6 +714,29 @@ function useChat({
708
714
  // react/use-completion.ts
709
715
  var import_react2 = require("react");
710
716
  var import_swr2 = __toESM(require("swr"));
717
+
718
+ // shared/process-message-stream.ts
719
+ async function processMessageStream(reader, processMessage) {
720
+ const decoder = new TextDecoder();
721
+ let buffer = "";
722
+ while (true) {
723
+ const { done, value } = await reader.read();
724
+ if (done) {
725
+ if (buffer.length > 0) {
726
+ processMessage(buffer);
727
+ }
728
+ break;
729
+ }
730
+ buffer += decoder.decode(value, { stream: true });
731
+ let endIndex;
732
+ while ((endIndex = buffer.indexOf("\n")) !== -1) {
733
+ processMessage(buffer.substring(0, endIndex).trim());
734
+ buffer = buffer.substring(endIndex + 1);
735
+ }
736
+ }
737
+ }
738
+
739
+ // react/use-completion.ts
711
740
  function useCompletion({
712
741
  api = "/api/completion",
713
742
  id,
@@ -785,17 +814,28 @@ function useCompletion({
785
814
  }
786
815
  let result = "";
787
816
  const reader = res.body.getReader();
788
- const decoder = createChunkDecoder();
789
- while (true) {
790
- const { done, value } = await reader.read();
791
- if (done) {
792
- break;
793
- }
794
- result += decoder(value);
795
- mutate(result, false);
796
- if (abortController2 === null) {
797
- reader.cancel();
798
- break;
817
+ const isComplexMode = res.headers.get(COMPLEX_HEADER) === "true";
818
+ if (isComplexMode) {
819
+ await processMessageStream(reader, (message) => {
820
+ const { type, value } = parseStreamPart(message);
821
+ if (type === "text") {
822
+ result += value;
823
+ mutate(result, false);
824
+ }
825
+ });
826
+ } else {
827
+ const decoder = createChunkDecoder();
828
+ while (true) {
829
+ const { done, value } = await reader.read();
830
+ if (done) {
831
+ break;
832
+ }
833
+ result += decoder(value);
834
+ mutate(result, false);
835
+ if (abortController2 === null) {
836
+ reader.cancel();
837
+ break;
838
+ }
799
839
  }
800
840
  }
801
841
  if (onFinish) {
@@ -877,29 +917,6 @@ function useCompletion({
877
917
 
878
918
  // react/use-assistant.ts
879
919
  var import_react3 = require("react");
880
-
881
- // shared/process-message-stream.ts
882
- async function processMessageStream(reader, processMessage) {
883
- const decoder = new TextDecoder();
884
- let buffer = "";
885
- while (true) {
886
- const { done, value } = await reader.read();
887
- if (done) {
888
- if (buffer.length > 0) {
889
- processMessage(buffer);
890
- }
891
- break;
892
- }
893
- buffer += decoder.decode(value, { stream: true });
894
- let endIndex;
895
- while ((endIndex = buffer.indexOf("\n")) !== -1) {
896
- processMessage(buffer.substring(0, endIndex).trim());
897
- buffer = buffer.substring(endIndex + 1);
898
- }
899
- }
900
- }
901
-
902
- // react/use-assistant.ts
903
920
  function experimental_useAssistant({
904
921
  api,
905
922
  threadId: threadIdParam
@@ -909,12 +926,12 @@ function experimental_useAssistant({
909
926
  const [threadId, setThreadId] = (0, import_react3.useState)(void 0);
910
927
  const [status, setStatus] = (0, import_react3.useState)("awaiting_message");
911
928
  const [error, setError] = (0, import_react3.useState)(void 0);
912
- const handleInputChange = (e) => {
913
- setInput(e.target.value);
929
+ const handleInputChange = (event) => {
930
+ setInput(event.target.value);
914
931
  };
915
- const submitMessage = async (e) => {
916
- var _a;
917
- e.preventDefault();
932
+ const submitMessage = async (event, requestOptions) => {
933
+ var _a, _b;
934
+ (_a = event == null ? void 0 : event.preventDefault) == null ? void 0 : _a.call(event);
918
935
  if (input === "") {
919
936
  return;
920
937
  }
@@ -929,8 +946,10 @@ function experimental_useAssistant({
929
946
  headers: { "Content-Type": "application/json" },
930
947
  body: JSON.stringify({
931
948
  // always use user-provided threadId when available:
932
- threadId: (_a = threadIdParam != null ? threadIdParam : threadId) != null ? _a : null,
933
- message: input
949
+ threadId: (_b = threadIdParam != null ? threadIdParam : threadId) != null ? _b : null,
950
+ message: input,
951
+ // optional request data:
952
+ data: requestOptions == null ? void 0 : requestOptions.data
934
953
  })
935
954
  });
936
955
  if (result.body == null) {
@@ -973,6 +992,7 @@ function experimental_useAssistant({
973
992
  };
974
993
  return {
975
994
  messages,
995
+ threadId,
976
996
  input,
977
997
  handleInputChange,
978
998
  submitMessage,
@@ -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) {
@@ -936,6 +955,7 @@ function experimental_useAssistant({
936
955
  };
937
956
  return {
938
957
  messages,
958
+ threadId,
939
959
  input,
940
960
  handleInputChange,
941
961
  submitMessage,
@@ -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),