ai 2.2.21 → 2.2.23

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/dist/index.d.ts CHANGED
@@ -64,6 +64,7 @@ type ChatRequest = {
64
64
  options?: RequestOptions;
65
65
  functions?: Array<Function>;
66
66
  function_call?: FunctionCall;
67
+ data?: Record<string, string>;
67
68
  };
68
69
  type FunctionCallHandler = (chatMessages: Message[], functionCall: FunctionCall) => Promise<ChatRequest | void>;
69
70
  type RequestOptions = {
@@ -74,6 +75,7 @@ type ChatRequestOptions = {
74
75
  options?: RequestOptions;
75
76
  functions?: Array<Function>;
76
77
  function_call?: FunctionCall;
78
+ data?: Record<string, string>;
77
79
  };
78
80
  type UseChatOptions = {
79
81
  /**
@@ -574,7 +576,9 @@ interface Prediction {
574
576
  * return new StreamingTextResponse(stream)
575
577
  *
576
578
  */
577
- declare function ReplicateStream(res: Prediction, cb?: AIStreamCallbacksAndOptions): Promise<ReadableStream>;
579
+ declare function ReplicateStream(res: Prediction, cb?: AIStreamCallbacksAndOptions, options?: {
580
+ headers?: Record<string, string>;
581
+ }): Promise<ReadableStream>;
578
582
 
579
583
  declare const nanoid: (size?: number | undefined) => string;
580
584
  declare function createChunkDecoder(): (chunk: Uint8Array | undefined) => string;
@@ -621,7 +625,7 @@ declare const getStreamString: (type: keyof typeof StreamStringPrefixes, value:
621
625
  type StreamString = `${(typeof StreamStringPrefixes)[keyof typeof StreamStringPrefixes]}:${string}\n`;
622
626
  declare const getStreamStringTypeAndValue: (line: string) => {
623
627
  type: keyof typeof StreamStringPrefixes;
624
- value: string;
628
+ value: JSONValue;
625
629
  };
626
630
  /**
627
631
  * A header sent to the client so it knows how to handle parsing the stream (as a deprecated text response or using the new prefixed protocol)
@@ -637,6 +641,7 @@ declare const COMPLEX_HEADER = "X-Experimental-Stream-Data";
637
641
  * It is naive as unlike the StreamingTextResponse, it does not send the diff
638
642
  * between the rows, but flushing the full payload on each row.
639
643
  */
644
+
640
645
  type UINode = string | JSX.Element | JSX.Element[] | null | undefined;
641
646
  type Payload = {
642
647
  ui: UINode | Promise<UINode>;
@@ -652,7 +657,9 @@ declare class experimental_StreamingReactResponse {
652
657
  constructor(res: ReadableStream, options?: {
653
658
  ui?: (message: {
654
659
  content: string;
660
+ data?: JSONValue[] | undefined;
655
661
  }) => UINode | Promise<UINode>;
662
+ data?: experimental_StreamData;
656
663
  });
657
664
  }
658
665
 
package/dist/index.js CHANGED
@@ -193,6 +193,9 @@ var getStreamString = (type, value) => `${StreamStringPrefixes[type]}:${JSON.str
193
193
  `;
194
194
  var getStreamStringTypeAndValue = (line) => {
195
195
  const firstSeperatorIndex = line.indexOf(":");
196
+ if (firstSeperatorIndex === -1) {
197
+ throw new Error("Failed to parse stream string");
198
+ }
196
199
  const prefix = line.slice(0, firstSeperatorIndex);
197
200
  const type = Object.keys(StreamStringPrefixes).find(
198
201
  (key) => StreamStringPrefixes[key] === Number(prefix)
@@ -691,7 +694,7 @@ function LangChainStream(callbacks) {
691
694
  }
692
695
 
693
696
  // streams/replicate-stream.ts
694
- async function ReplicateStream(res, cb) {
697
+ async function ReplicateStream(res, cb, options) {
695
698
  var _a;
696
699
  const url = (_a = res.urls) == null ? void 0 : _a.stream;
697
700
  if (!url) {
@@ -703,7 +706,8 @@ async function ReplicateStream(res, cb) {
703
706
  const eventStream = await fetch(url, {
704
707
  method: "GET",
705
708
  headers: {
706
- Accept: "text/event-stream"
709
+ Accept: "text/event-stream",
710
+ ...options == null ? void 0 : options.headers
707
711
  }
708
712
  });
709
713
  return AIStream(eventStream, void 0, cb).pipeThrough(
@@ -711,6 +715,104 @@ async function ReplicateStream(res, cb) {
711
715
  );
712
716
  }
713
717
 
718
+ // react/parse-complex-response.ts
719
+ async function parseComplexResponse({
720
+ reader,
721
+ abortControllerRef,
722
+ update,
723
+ onFinish
724
+ }) {
725
+ const decode = createChunkDecoder(true);
726
+ const createdAt = /* @__PURE__ */ new Date();
727
+ const prefixMap = {};
728
+ const NEWLINE = "\n".charCodeAt(0);
729
+ let chunks = [];
730
+ let totalLength = 0;
731
+ while (true) {
732
+ const { value } = await reader.read();
733
+ if (value) {
734
+ chunks.push(value);
735
+ totalLength += value.length;
736
+ if (value[value.length - 1] !== NEWLINE) {
737
+ continue;
738
+ }
739
+ }
740
+ if (chunks.length === 0) {
741
+ break;
742
+ }
743
+ let concatenatedChunks = new Uint8Array(totalLength);
744
+ let offset = 0;
745
+ for (const chunk of chunks) {
746
+ concatenatedChunks.set(chunk, offset);
747
+ offset += chunk.length;
748
+ }
749
+ chunks.length = 0;
750
+ totalLength = 0;
751
+ const lines = decode(concatenatedChunks);
752
+ if (typeof lines === "string") {
753
+ throw new Error(
754
+ "Invalid response format. Complex mode was set but the response is a string. This should never happen."
755
+ );
756
+ }
757
+ for (const { type, value: value2 } of lines) {
758
+ if (type === "text") {
759
+ if (prefixMap["text"]) {
760
+ prefixMap["text"] = {
761
+ ...prefixMap["text"],
762
+ content: (prefixMap["text"].content || "") + value2
763
+ };
764
+ } else {
765
+ prefixMap["text"] = {
766
+ id: nanoid(),
767
+ role: "assistant",
768
+ content: value2,
769
+ createdAt
770
+ };
771
+ }
772
+ }
773
+ let functionCallMessage = null;
774
+ if (type === "function_call") {
775
+ prefixMap["function_call"] = value2;
776
+ let functionCall = prefixMap["function_call"];
777
+ if (functionCall && typeof functionCall === "string") {
778
+ const parsedFunctionCall = JSON.parse(
779
+ functionCall
780
+ ).function_call;
781
+ functionCallMessage = {
782
+ id: nanoid(),
783
+ role: "assistant",
784
+ content: "",
785
+ function_call: parsedFunctionCall,
786
+ name: parsedFunctionCall.name,
787
+ createdAt
788
+ };
789
+ prefixMap["function_call"] = functionCallMessage;
790
+ }
791
+ }
792
+ if (type === "data") {
793
+ const parsedValue = JSON.parse(value2);
794
+ if (prefixMap["data"]) {
795
+ prefixMap["data"] = [...prefixMap["data"], ...parsedValue];
796
+ } else {
797
+ prefixMap["data"] = parsedValue;
798
+ }
799
+ }
800
+ const data = prefixMap["data"];
801
+ const responseMessage = prefixMap["text"];
802
+ const merged = [functionCallMessage, responseMessage].filter(
803
+ Boolean
804
+ );
805
+ update(merged, data);
806
+ if ((abortControllerRef == null ? void 0 : abortControllerRef.current) === null) {
807
+ reader.cancel();
808
+ break;
809
+ }
810
+ }
811
+ }
812
+ onFinish == null ? void 0 : onFinish(prefixMap);
813
+ return prefixMap;
814
+ }
815
+
714
816
  // streams/streaming-react-response.ts
715
817
  var experimental_StreamingReactResponse = class {
716
818
  constructor(res, options) {
@@ -719,6 +821,39 @@ var experimental_StreamingReactResponse = class {
719
821
  let next = new Promise((resolve) => {
720
822
  resolveFunc = resolve;
721
823
  });
824
+ if (options == null ? void 0 : options.data) {
825
+ const processedStream = res.pipeThrough(
826
+ options.data.stream
827
+ );
828
+ let lastPayload = void 0;
829
+ parseComplexResponse({
830
+ reader: processedStream.getReader(),
831
+ update: (merged, data) => {
832
+ var _a, _b, _c;
833
+ const content2 = (_b = (_a = merged[0]) == null ? void 0 : _a.content) != null ? _b : "";
834
+ const ui = ((_c = options == null ? void 0 : options.ui) == null ? void 0 : _c.call(options, { content: content2, data })) || content2;
835
+ const payload = { ui, content: content2 };
836
+ const resolvePrevious = resolveFunc;
837
+ const nextRow = new Promise((resolve) => {
838
+ resolveFunc = resolve;
839
+ });
840
+ resolvePrevious({
841
+ next: nextRow,
842
+ ...payload
843
+ });
844
+ lastPayload = payload;
845
+ },
846
+ onFinish: () => {
847
+ if (lastPayload !== void 0) {
848
+ resolveFunc({
849
+ next: null,
850
+ ...lastPayload
851
+ });
852
+ }
853
+ }
854
+ });
855
+ return next;
856
+ }
722
857
  let content = "";
723
858
  const decode = createChunkDecoder();
724
859
  const reader = res.getReader();
package/dist/index.mjs CHANGED
@@ -147,6 +147,9 @@ var getStreamString = (type, value) => `${StreamStringPrefixes[type]}:${JSON.str
147
147
  `;
148
148
  var getStreamStringTypeAndValue = (line) => {
149
149
  const firstSeperatorIndex = line.indexOf(":");
150
+ if (firstSeperatorIndex === -1) {
151
+ throw new Error("Failed to parse stream string");
152
+ }
150
153
  const prefix = line.slice(0, firstSeperatorIndex);
151
154
  const type = Object.keys(StreamStringPrefixes).find(
152
155
  (key) => StreamStringPrefixes[key] === Number(prefix)
@@ -645,7 +648,7 @@ function LangChainStream(callbacks) {
645
648
  }
646
649
 
647
650
  // streams/replicate-stream.ts
648
- async function ReplicateStream(res, cb) {
651
+ async function ReplicateStream(res, cb, options) {
649
652
  var _a;
650
653
  const url = (_a = res.urls) == null ? void 0 : _a.stream;
651
654
  if (!url) {
@@ -657,7 +660,8 @@ async function ReplicateStream(res, cb) {
657
660
  const eventStream = await fetch(url, {
658
661
  method: "GET",
659
662
  headers: {
660
- Accept: "text/event-stream"
663
+ Accept: "text/event-stream",
664
+ ...options == null ? void 0 : options.headers
661
665
  }
662
666
  });
663
667
  return AIStream(eventStream, void 0, cb).pipeThrough(
@@ -665,6 +669,104 @@ async function ReplicateStream(res, cb) {
665
669
  );
666
670
  }
667
671
 
672
+ // react/parse-complex-response.ts
673
+ async function parseComplexResponse({
674
+ reader,
675
+ abortControllerRef,
676
+ update,
677
+ onFinish
678
+ }) {
679
+ const decode = createChunkDecoder(true);
680
+ const createdAt = /* @__PURE__ */ new Date();
681
+ const prefixMap = {};
682
+ const NEWLINE = "\n".charCodeAt(0);
683
+ let chunks = [];
684
+ let totalLength = 0;
685
+ while (true) {
686
+ const { value } = await reader.read();
687
+ if (value) {
688
+ chunks.push(value);
689
+ totalLength += value.length;
690
+ if (value[value.length - 1] !== NEWLINE) {
691
+ continue;
692
+ }
693
+ }
694
+ if (chunks.length === 0) {
695
+ break;
696
+ }
697
+ let concatenatedChunks = new Uint8Array(totalLength);
698
+ let offset = 0;
699
+ for (const chunk of chunks) {
700
+ concatenatedChunks.set(chunk, offset);
701
+ offset += chunk.length;
702
+ }
703
+ chunks.length = 0;
704
+ totalLength = 0;
705
+ const lines = decode(concatenatedChunks);
706
+ if (typeof lines === "string") {
707
+ throw new Error(
708
+ "Invalid response format. Complex mode was set but the response is a string. This should never happen."
709
+ );
710
+ }
711
+ for (const { type, value: value2 } of lines) {
712
+ if (type === "text") {
713
+ if (prefixMap["text"]) {
714
+ prefixMap["text"] = {
715
+ ...prefixMap["text"],
716
+ content: (prefixMap["text"].content || "") + value2
717
+ };
718
+ } else {
719
+ prefixMap["text"] = {
720
+ id: nanoid(),
721
+ role: "assistant",
722
+ content: value2,
723
+ createdAt
724
+ };
725
+ }
726
+ }
727
+ let functionCallMessage = null;
728
+ if (type === "function_call") {
729
+ prefixMap["function_call"] = value2;
730
+ let functionCall = prefixMap["function_call"];
731
+ if (functionCall && typeof functionCall === "string") {
732
+ const parsedFunctionCall = JSON.parse(
733
+ functionCall
734
+ ).function_call;
735
+ functionCallMessage = {
736
+ id: nanoid(),
737
+ role: "assistant",
738
+ content: "",
739
+ function_call: parsedFunctionCall,
740
+ name: parsedFunctionCall.name,
741
+ createdAt
742
+ };
743
+ prefixMap["function_call"] = functionCallMessage;
744
+ }
745
+ }
746
+ if (type === "data") {
747
+ const parsedValue = JSON.parse(value2);
748
+ if (prefixMap["data"]) {
749
+ prefixMap["data"] = [...prefixMap["data"], ...parsedValue];
750
+ } else {
751
+ prefixMap["data"] = parsedValue;
752
+ }
753
+ }
754
+ const data = prefixMap["data"];
755
+ const responseMessage = prefixMap["text"];
756
+ const merged = [functionCallMessage, responseMessage].filter(
757
+ Boolean
758
+ );
759
+ update(merged, data);
760
+ if ((abortControllerRef == null ? void 0 : abortControllerRef.current) === null) {
761
+ reader.cancel();
762
+ break;
763
+ }
764
+ }
765
+ }
766
+ onFinish == null ? void 0 : onFinish(prefixMap);
767
+ return prefixMap;
768
+ }
769
+
668
770
  // streams/streaming-react-response.ts
669
771
  var experimental_StreamingReactResponse = class {
670
772
  constructor(res, options) {
@@ -673,6 +775,39 @@ var experimental_StreamingReactResponse = class {
673
775
  let next = new Promise((resolve) => {
674
776
  resolveFunc = resolve;
675
777
  });
778
+ if (options == null ? void 0 : options.data) {
779
+ const processedStream = res.pipeThrough(
780
+ options.data.stream
781
+ );
782
+ let lastPayload = void 0;
783
+ parseComplexResponse({
784
+ reader: processedStream.getReader(),
785
+ update: (merged, data) => {
786
+ var _a, _b, _c;
787
+ const content2 = (_b = (_a = merged[0]) == null ? void 0 : _a.content) != null ? _b : "";
788
+ const ui = ((_c = options == null ? void 0 : options.ui) == null ? void 0 : _c.call(options, { content: content2, data })) || content2;
789
+ const payload = { ui, content: content2 };
790
+ const resolvePrevious = resolveFunc;
791
+ const nextRow = new Promise((resolve) => {
792
+ resolveFunc = resolve;
793
+ });
794
+ resolvePrevious({
795
+ next: nextRow,
796
+ ...payload
797
+ });
798
+ lastPayload = payload;
799
+ },
800
+ onFinish: () => {
801
+ if (lastPayload !== void 0) {
802
+ resolveFunc({
803
+ next: null,
804
+ ...lastPayload
805
+ });
806
+ }
807
+ }
808
+ });
809
+ return next;
810
+ }
676
811
  let content = "";
677
812
  const decode = createChunkDecoder();
678
813
  const reader = res.getReader();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ai",
3
- "version": "2.2.21",
3
+ "version": "2.2.23",
4
4
  "license": "Apache-2.0",
5
5
  "sideEffects": false,
6
6
  "main": "./dist/index.js",
@@ -54,10 +54,6 @@
54
54
  "require": "./solid/dist/index.js"
55
55
  }
56
56
  },
57
- "jest": {
58
- "preset": "ts-jest",
59
- "testEnvironment": "node"
60
- },
61
57
  "dependencies": {
62
58
  "eventsource-parser": "1.0.0",
63
59
  "nanoid": "3.3.6",
@@ -70,12 +66,17 @@
70
66
  "devDependencies": {
71
67
  "@edge-runtime/jest-environment": "1.1.0-beta.31",
72
68
  "@huggingface/inference": "2.6.4",
69
+ "@testing-library/jest-dom": "^6.1.4",
70
+ "@testing-library/react": "^14.0.0",
71
+ "@testing-library/user-event": "^14.5.1",
73
72
  "@types/jest": "29.2.0",
74
73
  "@types/node": "^17.0.12",
75
74
  "@types/react": "^18.2.8",
76
75
  "@types/react-dom": "^18.2.0",
77
76
  "eslint": "^7.32.0",
78
77
  "jest": "29.2.1",
78
+ "jest-environment-jsdom": "^29.7.0",
79
+ "jsdom": "^22.1.0",
79
80
  "langchain": "0.0.172",
80
81
  "openai": "4.16.1",
81
82
  "ts-jest": "29.0.3",
@@ -132,6 +133,9 @@
132
133
  "lint": "eslint \"./**/*.ts*\"",
133
134
  "type-check": "tsc --noEmit",
134
135
  "prettier-check": "prettier --check \"./**/*.ts*\"",
135
- "test": "jest --forceExit --env @edge-runtime/jest-environment .test.ts && jest --forceExit --env node .test.ts"
136
+ "test": "pnpm test:node && pnpm test:edge && pnpm test:ui",
137
+ "test:edge": "jest --forceExit --config jest.node.config.js --env @edge-runtime/jest-environment .test.ts ",
138
+ "test:node": "jest --forceExit --config jest.node.config.js --env node .test.ts",
139
+ "test:ui": "jest --forceExit --config jest.ui.config.js .ui.test.ts"
136
140
  }
137
141
  }
@@ -62,6 +62,7 @@ type ChatRequest = {
62
62
  options?: RequestOptions;
63
63
  functions?: Array<Function>;
64
64
  function_call?: FunctionCall;
65
+ data?: Record<string, string>;
65
66
  };
66
67
  type FunctionCallHandler = (chatMessages: Message[], functionCall: FunctionCall) => Promise<ChatRequest | void>;
67
68
  type RequestOptions = {
@@ -72,6 +73,7 @@ type ChatRequestOptions = {
72
73
  options?: RequestOptions;
73
74
  functions?: Array<Function>;
74
75
  function_call?: FunctionCall;
76
+ data?: Record<string, string>;
75
77
  };
76
78
  type UseChatOptions = {
77
79
  /**
@@ -197,6 +199,25 @@ type UseCompletionOptions = {
197
199
  */
198
200
  body?: object;
199
201
  };
202
+ type JSONValue = null | string | number | boolean | {
203
+ [x: string]: JSONValue;
204
+ } | Array<JSONValue>;
205
+
206
+ /**
207
+ * A stream wrapper to send custom JSON-encoded data back to the client.
208
+ */
209
+ declare class experimental_StreamData {
210
+ private encoder;
211
+ private controller;
212
+ stream: TransformStream<Uint8Array, Uint8Array>;
213
+ private isClosedPromise;
214
+ private isClosedPromiseResolver;
215
+ private isClosed;
216
+ private data;
217
+ constructor();
218
+ close(): Promise<void>;
219
+ append(value: JSONValue): void;
220
+ }
200
221
 
201
222
  /**
202
223
  * This is a naive implementation of the streaming React response API.
@@ -207,6 +228,7 @@ type UseCompletionOptions = {
207
228
  * It is naive as unlike the StreamingTextResponse, it does not send the diff
208
229
  * between the rows, but flushing the full payload on each row.
209
230
  */
231
+
210
232
  type UINode = string | JSX.Element | JSX.Element[] | null | undefined;
211
233
  /**
212
234
  * A utility class for streaming React responses.
@@ -215,7 +237,9 @@ declare class experimental_StreamingReactResponse {
215
237
  constructor(res: ReadableStream, options?: {
216
238
  ui?: (message: {
217
239
  content: string;
240
+ data?: JSONValue[] | undefined;
218
241
  }) => UINode | Promise<UINode>;
242
+ data?: experimental_StreamData;
219
243
  });
220
244
  }
221
245
 
@@ -263,8 +287,9 @@ type UseChatHelpers = {
263
287
  };
264
288
  type StreamingReactResponseAction = (payload: {
265
289
  messages: Message[];
290
+ data?: Record<string, string>;
266
291
  }) => Promise<experimental_StreamingReactResponse>;
267
- declare function useChat({ api, id, initialMessages: initialMessagesParam, initialInput, sendExtraMessageFields, experimental_onFunctionCall, onResponse, onFinish, onError, credentials, headers, body, }?: Omit<UseChatOptions, 'api'> & {
292
+ declare function useChat({ api, id, initialMessages, initialInput, sendExtraMessageFields, experimental_onFunctionCall, onResponse, onFinish, onError, credentials, headers, body, }?: Omit<UseChatOptions, 'api'> & {
268
293
  api?: string | StreamingReactResponseAction;
269
294
  }): UseChatHelpers;
270
295
 
@@ -68,6 +68,9 @@ var StreamStringPrefixes = {
68
68
  };
69
69
  var getStreamStringTypeAndValue = (line) => {
70
70
  const firstSeperatorIndex = line.indexOf(":");
71
+ if (firstSeperatorIndex === -1) {
72
+ throw new Error("Failed to parse stream string");
73
+ }
71
74
  const prefix = line.slice(0, firstSeperatorIndex);
72
75
  const type = Object.keys(StreamStringPrefixes).find(
73
76
  (key) => StreamStringPrefixes[key] === Number(prefix)
@@ -90,7 +93,8 @@ var COMPLEX_HEADER = "X-Experimental-Stream-Data";
90
93
  async function parseComplexResponse({
91
94
  reader,
92
95
  abortControllerRef,
93
- update
96
+ update,
97
+ onFinish
94
98
  }) {
95
99
  const decode = createChunkDecoder(true);
96
100
  const createdAt = /* @__PURE__ */ new Date();
@@ -173,12 +177,13 @@ async function parseComplexResponse({
173
177
  Boolean
174
178
  );
175
179
  update(merged, data);
176
- if (abortControllerRef.current === null) {
180
+ if ((abortControllerRef == null ? void 0 : abortControllerRef.current) === null) {
177
181
  reader.cancel();
178
182
  break;
179
183
  }
180
184
  }
181
185
  }
186
+ onFinish == null ? void 0 : onFinish(prefixMap);
182
187
  return prefixMap;
183
188
  }
184
189
 
@@ -215,7 +220,8 @@ var getStreamedResponse = async (api, chatRequest, mutate, mutateStreamData, exi
215
220
  }
216
221
  try {
217
222
  const promise = api({
218
- messages: constructedMessagesPayload
223
+ messages: constructedMessagesPayload,
224
+ data: chatRequest.data
219
225
  });
220
226
  await readRow(promise);
221
227
  } catch (e) {
@@ -231,6 +237,7 @@ var getStreamedResponse = async (api, chatRequest, mutate, mutateStreamData, exi
231
237
  method: "POST",
232
238
  body: JSON.stringify({
233
239
  messages: constructedMessagesPayload,
240
+ data: chatRequest.data,
234
241
  ...extraMetadataRef.current.body,
235
242
  ...(_a = chatRequest.options) == null ? void 0 : _a.body,
236
243
  ...chatRequest.functions !== void 0 && {
@@ -332,7 +339,7 @@ var getStreamedResponse = async (api, chatRequest, mutate, mutateStreamData, exi
332
339
  function useChat({
333
340
  api = "/api/chat",
334
341
  id,
335
- initialMessages: initialMessagesParam,
342
+ initialMessages,
336
343
  initialInput = "",
337
344
  sendExtraMessageFields,
338
345
  experimental_onFunctionCall,
@@ -345,9 +352,9 @@ function useChat({
345
352
  } = {}) {
346
353
  const hookId = (0, import_react.useId)();
347
354
  const chatId = id || hookId;
348
- const [initialMessages] = (0, import_react.useState)(initialMessagesParam != null ? initialMessagesParam : []);
355
+ const [initialMessagesFallback] = (0, import_react.useState)([]);
349
356
  const { data: messages, mutate } = (0, import_swr.default)([api, chatId], null, {
350
- fallbackData: initialMessages
357
+ fallbackData: initialMessages != null ? initialMessages : initialMessagesFallback
351
358
  });
352
359
  const { data: isLoading = false, mutate: mutateLoading } = (0, import_swr.default)(
353
360
  [chatId, "loading"],
@@ -468,13 +475,14 @@ function useChat({
468
475
  ]
469
476
  );
470
477
  const append = (0, import_react.useCallback)(
471
- async (message, { options, functions, function_call } = {}) => {
478
+ async (message, { options, functions, function_call, data } = {}) => {
472
479
  if (!message.id) {
473
480
  message.id = nanoid();
474
481
  }
475
482
  const chatRequest = {
476
483
  messages: messagesRef.current.concat(message),
477
484
  options,
485
+ data,
478
486
  ...functions !== void 0 && { functions },
479
487
  ...function_call !== void 0 && { function_call }
480
488
  };
@@ -521,7 +529,7 @@ function useChat({
521
529
  );
522
530
  const [input, setInput] = (0, import_react.useState)(initialInput);
523
531
  const handleSubmit = (0, import_react.useCallback)(
524
- (e, { options, functions, function_call } = {}, metadata) => {
532
+ (e, options = {}, metadata) => {
525
533
  if (metadata) {
526
534
  extraMetadataRef.current = {
527
535
  ...extraMetadataRef.current,
@@ -537,7 +545,7 @@ function useChat({
537
545
  role: "user",
538
546
  createdAt: /* @__PURE__ */ new Date()
539
547
  },
540
- { options, functions, function_call }
548
+ options
541
549
  );
542
550
  setInput("");
543
551
  },
@@ -32,6 +32,9 @@ var StreamStringPrefixes = {
32
32
  };
33
33
  var getStreamStringTypeAndValue = (line) => {
34
34
  const firstSeperatorIndex = line.indexOf(":");
35
+ if (firstSeperatorIndex === -1) {
36
+ throw new Error("Failed to parse stream string");
37
+ }
35
38
  const prefix = line.slice(0, firstSeperatorIndex);
36
39
  const type = Object.keys(StreamStringPrefixes).find(
37
40
  (key) => StreamStringPrefixes[key] === Number(prefix)
@@ -54,7 +57,8 @@ var COMPLEX_HEADER = "X-Experimental-Stream-Data";
54
57
  async function parseComplexResponse({
55
58
  reader,
56
59
  abortControllerRef,
57
- update
60
+ update,
61
+ onFinish
58
62
  }) {
59
63
  const decode = createChunkDecoder(true);
60
64
  const createdAt = /* @__PURE__ */ new Date();
@@ -137,12 +141,13 @@ async function parseComplexResponse({
137
141
  Boolean
138
142
  );
139
143
  update(merged, data);
140
- if (abortControllerRef.current === null) {
144
+ if ((abortControllerRef == null ? void 0 : abortControllerRef.current) === null) {
141
145
  reader.cancel();
142
146
  break;
143
147
  }
144
148
  }
145
149
  }
150
+ onFinish == null ? void 0 : onFinish(prefixMap);
146
151
  return prefixMap;
147
152
  }
148
153
 
@@ -179,7 +184,8 @@ var getStreamedResponse = async (api, chatRequest, mutate, mutateStreamData, exi
179
184
  }
180
185
  try {
181
186
  const promise = api({
182
- messages: constructedMessagesPayload
187
+ messages: constructedMessagesPayload,
188
+ data: chatRequest.data
183
189
  });
184
190
  await readRow(promise);
185
191
  } catch (e) {
@@ -195,6 +201,7 @@ var getStreamedResponse = async (api, chatRequest, mutate, mutateStreamData, exi
195
201
  method: "POST",
196
202
  body: JSON.stringify({
197
203
  messages: constructedMessagesPayload,
204
+ data: chatRequest.data,
198
205
  ...extraMetadataRef.current.body,
199
206
  ...(_a = chatRequest.options) == null ? void 0 : _a.body,
200
207
  ...chatRequest.functions !== void 0 && {
@@ -296,7 +303,7 @@ var getStreamedResponse = async (api, chatRequest, mutate, mutateStreamData, exi
296
303
  function useChat({
297
304
  api = "/api/chat",
298
305
  id,
299
- initialMessages: initialMessagesParam,
306
+ initialMessages,
300
307
  initialInput = "",
301
308
  sendExtraMessageFields,
302
309
  experimental_onFunctionCall,
@@ -309,9 +316,9 @@ function useChat({
309
316
  } = {}) {
310
317
  const hookId = useId();
311
318
  const chatId = id || hookId;
312
- const [initialMessages] = useState(initialMessagesParam != null ? initialMessagesParam : []);
319
+ const [initialMessagesFallback] = useState([]);
313
320
  const { data: messages, mutate } = useSWR([api, chatId], null, {
314
- fallbackData: initialMessages
321
+ fallbackData: initialMessages != null ? initialMessages : initialMessagesFallback
315
322
  });
316
323
  const { data: isLoading = false, mutate: mutateLoading } = useSWR(
317
324
  [chatId, "loading"],
@@ -432,13 +439,14 @@ function useChat({
432
439
  ]
433
440
  );
434
441
  const append = useCallback(
435
- async (message, { options, functions, function_call } = {}) => {
442
+ async (message, { options, functions, function_call, data } = {}) => {
436
443
  if (!message.id) {
437
444
  message.id = nanoid();
438
445
  }
439
446
  const chatRequest = {
440
447
  messages: messagesRef.current.concat(message),
441
448
  options,
449
+ data,
442
450
  ...functions !== void 0 && { functions },
443
451
  ...function_call !== void 0 && { function_call }
444
452
  };
@@ -485,7 +493,7 @@ function useChat({
485
493
  );
486
494
  const [input, setInput] = useState(initialInput);
487
495
  const handleSubmit = useCallback(
488
- (e, { options, functions, function_call } = {}, metadata) => {
496
+ (e, options = {}, metadata) => {
489
497
  if (metadata) {
490
498
  extraMetadataRef.current = {
491
499
  ...extraMetadataRef.current,
@@ -501,7 +509,7 @@ function useChat({
501
509
  role: "user",
502
510
  createdAt: /* @__PURE__ */ new Date()
503
511
  },
504
- { options, functions, function_call }
512
+ options
505
513
  );
506
514
  setInput("");
507
515
  },
@@ -64,6 +64,7 @@ type ChatRequest = {
64
64
  options?: RequestOptions;
65
65
  functions?: Array<Function>;
66
66
  function_call?: FunctionCall;
67
+ data?: Record<string, string>;
67
68
  };
68
69
  type FunctionCallHandler = (chatMessages: Message[], functionCall: FunctionCall) => Promise<ChatRequest | void>;
69
70
  type RequestOptions = {
@@ -58,6 +58,9 @@ var StreamStringPrefixes = {
58
58
  };
59
59
  var getStreamStringTypeAndValue = (line) => {
60
60
  const firstSeperatorIndex = line.indexOf(":");
61
+ if (firstSeperatorIndex === -1) {
62
+ throw new Error("Failed to parse stream string");
63
+ }
61
64
  const prefix = line.slice(0, firstSeperatorIndex);
62
65
  const type = Object.keys(StreamStringPrefixes).find(
63
66
  (key) => StreamStringPrefixes[key] === Number(prefix)
@@ -31,6 +31,9 @@ var StreamStringPrefixes = {
31
31
  };
32
32
  var getStreamStringTypeAndValue = (line) => {
33
33
  const firstSeperatorIndex = line.indexOf(":");
34
+ if (firstSeperatorIndex === -1) {
35
+ throw new Error("Failed to parse stream string");
36
+ }
34
37
  const prefix = line.slice(0, firstSeperatorIndex);
35
38
  const type = Object.keys(StreamStringPrefixes).find(
36
39
  (key) => StreamStringPrefixes[key] === Number(prefix)
@@ -64,6 +64,7 @@ type ChatRequest = {
64
64
  options?: RequestOptions;
65
65
  functions?: Array<Function>;
66
66
  function_call?: FunctionCall;
67
+ data?: Record<string, string>;
67
68
  };
68
69
  type FunctionCallHandler = (chatMessages: Message[], functionCall: FunctionCall) => Promise<ChatRequest | void>;
69
70
  type RequestOptions = {
@@ -74,6 +75,7 @@ type ChatRequestOptions = {
74
75
  options?: RequestOptions;
75
76
  functions?: Array<Function>;
76
77
  function_call?: FunctionCall;
78
+ data?: Record<string, string>;
77
79
  };
78
80
  type UseChatOptions = {
79
81
  /**
@@ -553,6 +553,9 @@ var StreamStringPrefixes = {
553
553
  };
554
554
  var getStreamStringTypeAndValue = (line) => {
555
555
  const firstSeperatorIndex = line.indexOf(":");
556
+ if (firstSeperatorIndex === -1) {
557
+ throw new Error("Failed to parse stream string");
558
+ }
556
559
  const prefix = line.slice(0, firstSeperatorIndex);
557
560
  const type = Object.keys(StreamStringPrefixes).find(
558
561
  (key) => StreamStringPrefixes[key] === Number(prefix)
@@ -526,6 +526,9 @@ var StreamStringPrefixes = {
526
526
  };
527
527
  var getStreamStringTypeAndValue = (line) => {
528
528
  const firstSeperatorIndex = line.indexOf(":");
529
+ if (firstSeperatorIndex === -1) {
530
+ throw new Error("Failed to parse stream string");
531
+ }
529
532
  const prefix = line.slice(0, firstSeperatorIndex);
530
533
  const type = Object.keys(StreamStringPrefixes).find(
531
534
  (key) => StreamStringPrefixes[key] === Number(prefix)
@@ -64,6 +64,7 @@ type ChatRequest = {
64
64
  options?: RequestOptions;
65
65
  functions?: Array<Function>;
66
66
  function_call?: FunctionCall;
67
+ data?: Record<string, string>;
67
68
  };
68
69
  type FunctionCallHandler = (chatMessages: Message[], functionCall: FunctionCall) => Promise<ChatRequest | void>;
69
70
  type RequestOptions = {
package/vue/dist/index.js CHANGED
@@ -67,6 +67,9 @@ var StreamStringPrefixes = {
67
67
  };
68
68
  var getStreamStringTypeAndValue = (line) => {
69
69
  const firstSeperatorIndex = line.indexOf(":");
70
+ if (firstSeperatorIndex === -1) {
71
+ throw new Error("Failed to parse stream string");
72
+ }
70
73
  const prefix = line.slice(0, firstSeperatorIndex);
71
74
  const type = Object.keys(StreamStringPrefixes).find(
72
75
  (key) => StreamStringPrefixes[key] === Number(prefix)
@@ -30,6 +30,9 @@ var StreamStringPrefixes = {
30
30
  };
31
31
  var getStreamStringTypeAndValue = (line) => {
32
32
  const firstSeperatorIndex = line.indexOf(":");
33
+ if (firstSeperatorIndex === -1) {
34
+ throw new Error("Failed to parse stream string");
35
+ }
33
36
  const prefix = line.slice(0, firstSeperatorIndex);
34
37
  const type = Object.keys(StreamStringPrefixes).find(
35
38
  (key) => StreamStringPrefixes[key] === Number(prefix)