ai 2.2.26 → 2.2.27

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.
@@ -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/utils.ts
7
- import { customAlphabet } from "nanoid/non-secure";
8
-
9
6
  // shared/stream-parts.ts
10
7
  var textStreamPart = {
11
8
  code: "0",
@@ -127,7 +124,51 @@ var parseStreamPart = (line) => {
127
124
  return streamPartsByCode[code].parse(jsonValue);
128
125
  };
129
126
 
127
+ // shared/read-data-stream.ts
128
+ var NEWLINE = "\n".charCodeAt(0);
129
+ function concatChunks(chunks, totalLength) {
130
+ const concatenatedChunks = new Uint8Array(totalLength);
131
+ let offset = 0;
132
+ for (const chunk of chunks) {
133
+ concatenatedChunks.set(chunk, offset);
134
+ offset += chunk.length;
135
+ }
136
+ chunks.length = 0;
137
+ return concatenatedChunks;
138
+ }
139
+ async function* readDataStream(reader, {
140
+ isAborted
141
+ } = {}) {
142
+ const decoder = new TextDecoder();
143
+ const chunks = [];
144
+ let totalLength = 0;
145
+ while (true) {
146
+ const { value } = await reader.read();
147
+ if (value) {
148
+ chunks.push(value);
149
+ totalLength += value.length;
150
+ if (value[value.length - 1] !== NEWLINE) {
151
+ continue;
152
+ }
153
+ }
154
+ if (chunks.length === 0) {
155
+ break;
156
+ }
157
+ const concatenatedChunks = concatChunks(chunks, totalLength);
158
+ totalLength = 0;
159
+ const streamParts2 = decoder.decode(concatenatedChunks, { stream: true }).split("\n").filter((line) => line !== "").map(parseStreamPart);
160
+ for (const streamPart of streamParts2) {
161
+ yield streamPart;
162
+ }
163
+ if (isAborted == null ? void 0 : isAborted()) {
164
+ reader.cancel();
165
+ break;
166
+ }
167
+ }
168
+ }
169
+
130
170
  // shared/utils.ts
171
+ import { customAlphabet } from "nanoid/non-secure";
131
172
  var nanoid = customAlphabet(
132
173
  "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
133
174
  7
@@ -158,80 +199,47 @@ async function parseComplexResponse({
158
199
  getCurrentDate = () => /* @__PURE__ */ new Date()
159
200
  }) {
160
201
  const createdAt = getCurrentDate();
161
- const decode = createChunkDecoder(true);
162
202
  const prefixMap = {
163
203
  data: []
164
204
  };
165
- const NEWLINE = "\n".charCodeAt(0);
166
- const chunks = [];
167
- let totalLength = 0;
168
- while (true) {
169
- const { value } = await reader.read();
170
- if (value) {
171
- chunks.push(value);
172
- totalLength += value.length;
173
- if (value[value.length - 1] !== NEWLINE) {
174
- continue;
175
- }
176
- }
177
- if (chunks.length === 0) {
178
- break;
179
- }
180
- let concatenatedChunks = new Uint8Array(totalLength);
181
- let offset = 0;
182
- for (const chunk of chunks) {
183
- concatenatedChunks.set(chunk, offset);
184
- offset += chunk.length;
185
- }
186
- chunks.length = 0;
187
- totalLength = 0;
188
- const lines = decode(concatenatedChunks);
189
- if (typeof lines === "string") {
190
- throw new Error(
191
- "Invalid response format. Complex mode was set but the response is a string. This should never happen."
192
- );
193
- }
194
- for (const { type, value: value2 } of lines) {
195
- if (type === "text") {
196
- if (prefixMap["text"]) {
197
- prefixMap["text"] = {
198
- ...prefixMap["text"],
199
- content: (prefixMap["text"].content || "") + value2
200
- };
201
- } else {
202
- prefixMap["text"] = {
203
- id: generateId(),
204
- role: "assistant",
205
- content: value2,
206
- createdAt
207
- };
208
- }
209
- }
210
- let functionCallMessage = null;
211
- if (type === "function_call") {
212
- prefixMap["function_call"] = {
205
+ for await (const { type, value } of readDataStream(reader, {
206
+ isAborted: () => (abortControllerRef == null ? void 0 : abortControllerRef.current) === null
207
+ })) {
208
+ if (type === "text") {
209
+ if (prefixMap["text"]) {
210
+ prefixMap["text"] = {
211
+ ...prefixMap["text"],
212
+ content: (prefixMap["text"].content || "") + value
213
+ };
214
+ } else {
215
+ prefixMap["text"] = {
213
216
  id: generateId(),
214
217
  role: "assistant",
215
- content: "",
216
- function_call: value2.function_call,
217
- name: value2.function_call.name,
218
+ content: value,
218
219
  createdAt
219
220
  };
220
- functionCallMessage = prefixMap["function_call"];
221
- }
222
- if (type === "data") {
223
- prefixMap["data"].push(...value2);
224
- }
225
- const responseMessage = prefixMap["text"];
226
- const merged = [functionCallMessage, responseMessage].filter(
227
- Boolean
228
- );
229
- update(merged, [...prefixMap["data"]]);
230
- if ((abortControllerRef == null ? void 0 : abortControllerRef.current) === null) {
231
- reader.cancel();
232
- break;
233
221
  }
234
222
  }
223
+ let functionCallMessage = null;
224
+ if (type === "function_call") {
225
+ prefixMap["function_call"] = {
226
+ id: generateId(),
227
+ role: "assistant",
228
+ content: "",
229
+ function_call: value.function_call,
230
+ name: value.function_call.name,
231
+ createdAt
232
+ };
233
+ functionCallMessage = prefixMap["function_call"];
234
+ }
235
+ if (type === "data") {
236
+ prefixMap["data"].push(...value);
237
+ }
238
+ const responseMessage = prefixMap["text"];
239
+ const merged = [functionCallMessage, responseMessage].filter(
240
+ Boolean
241
+ );
242
+ update(merged, [...prefixMap["data"]]);
235
243
  }
236
244
  onFinish == null ? void 0 : onFinish(prefixMap);
237
245
  return {
@@ -570,8 +578,8 @@ function useChat({
570
578
 
571
579
  // solid/use-completion.ts
572
580
  import { createSignal as createSignal2 } from "solid-js";
573
- import { createSWRStore as createSWRStore2 } from "swr-store";
574
581
  import { useSWRStore as useSWRStore2 } from "solid-swr-store";
582
+ import { createSWRStore as createSWRStore2 } from "swr-store";
575
583
  var uniqueId2 = 0;
576
584
  var store2 = {};
577
585
  var completionApiStore = createSWRStore2({
@@ -606,9 +614,13 @@ function useCompletion({
606
614
  };
607
615
  const completion = data;
608
616
  const [error, setError] = createSignal2(void 0);
617
+ const [streamData, setStreamData] = createSignal2(
618
+ void 0
619
+ );
609
620
  const [isLoading, setIsLoading] = createSignal2(false);
610
621
  let abortController = null;
611
622
  async function triggerRequest(prompt, options) {
623
+ var _a;
612
624
  try {
613
625
  setError(void 0);
614
626
  setIsLoading(true);
@@ -647,17 +659,37 @@ function useCompletion({
647
659
  }
648
660
  let result = "";
649
661
  const reader = res.body.getReader();
650
- const decoder = createChunkDecoder();
651
- while (true) {
652
- const { done, value } = await reader.read();
653
- if (done) {
654
- break;
662
+ const isComplexMode = res.headers.get(COMPLEX_HEADER) === "true";
663
+ if (isComplexMode) {
664
+ const existingData = (_a = streamData()) != null ? _a : [];
665
+ for await (const { type, value } of readDataStream(reader, {
666
+ isAborted: () => abortController === null
667
+ })) {
668
+ switch (type) {
669
+ case "text": {
670
+ result += value;
671
+ mutate(result);
672
+ break;
673
+ }
674
+ case "data": {
675
+ setStreamData([...existingData, ...value != null ? value : []]);
676
+ break;
677
+ }
678
+ }
655
679
  }
656
- result += decoder(value);
657
- mutate(result);
658
- if (abortController === null) {
659
- reader.cancel();
660
- break;
680
+ } else {
681
+ const decoder = createChunkDecoder();
682
+ while (true) {
683
+ const { done, value } = await reader.read();
684
+ if (done) {
685
+ break;
686
+ }
687
+ result += decoder(value);
688
+ mutate(result);
689
+ if (abortController === null) {
690
+ reader.cancel();
691
+ break;
692
+ }
661
693
  }
662
694
  }
663
695
  if (onFinish) {
@@ -707,7 +739,8 @@ function useCompletion({
707
739
  input,
708
740
  setInput,
709
741
  handleSubmit,
710
- isLoading
742
+ isLoading,
743
+ data: streamData
711
744
  };
712
745
  }
713
746
  export {
@@ -282,6 +282,8 @@ type UseCompletionHelpers = {
282
282
  handleSubmit: (e: any) => void;
283
283
  /** Whether the API request is in progress */
284
284
  isLoading: Readable<boolean | undefined>;
285
+ /** Additional data added on the server via StreamData */
286
+ data: Readable<JSONValue[] | undefined>;
285
287
  };
286
288
  declare function useCompletion({ api, id, initialCompletion, initialInput, credentials, headers, body, onResponse, onFinish, onError, }?: UseCompletionOptions): UseCompletionHelpers;
287
289
 
@@ -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/utils.ts
529
- var import_non_secure = require("nanoid/non-secure");
530
-
531
528
  // shared/stream-parts.ts
532
529
  var textStreamPart = {
533
530
  code: "0",
@@ -649,7 +646,51 @@ var parseStreamPart = (line) => {
649
646
  return streamPartsByCode[code].parse(jsonValue);
650
647
  };
651
648
 
649
+ // shared/read-data-stream.ts
650
+ var NEWLINE = "\n".charCodeAt(0);
651
+ function concatChunks(chunks, totalLength) {
652
+ const concatenatedChunks = new Uint8Array(totalLength);
653
+ let offset = 0;
654
+ for (const chunk of chunks) {
655
+ concatenatedChunks.set(chunk, offset);
656
+ offset += chunk.length;
657
+ }
658
+ chunks.length = 0;
659
+ return concatenatedChunks;
660
+ }
661
+ async function* readDataStream(reader, {
662
+ isAborted
663
+ } = {}) {
664
+ const decoder = new TextDecoder();
665
+ const chunks = [];
666
+ let totalLength = 0;
667
+ while (true) {
668
+ const { value } = await reader.read();
669
+ if (value) {
670
+ chunks.push(value);
671
+ totalLength += value.length;
672
+ if (value[value.length - 1] !== NEWLINE) {
673
+ continue;
674
+ }
675
+ }
676
+ if (chunks.length === 0) {
677
+ break;
678
+ }
679
+ const concatenatedChunks = concatChunks(chunks, totalLength);
680
+ totalLength = 0;
681
+ const streamParts2 = decoder.decode(concatenatedChunks, { stream: true }).split("\n").filter((line) => line !== "").map(parseStreamPart);
682
+ for (const streamPart of streamParts2) {
683
+ yield streamPart;
684
+ }
685
+ if (isAborted == null ? void 0 : isAborted()) {
686
+ reader.cancel();
687
+ break;
688
+ }
689
+ }
690
+ }
691
+
652
692
  // shared/utils.ts
693
+ var import_non_secure = require("nanoid/non-secure");
653
694
  var nanoid = (0, import_non_secure.customAlphabet)(
654
695
  "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
655
696
  7
@@ -680,80 +721,47 @@ async function parseComplexResponse({
680
721
  getCurrentDate = () => /* @__PURE__ */ new Date()
681
722
  }) {
682
723
  const createdAt = getCurrentDate();
683
- const decode = createChunkDecoder(true);
684
724
  const prefixMap = {
685
725
  data: []
686
726
  };
687
- const NEWLINE = "\n".charCodeAt(0);
688
- const chunks = [];
689
- let totalLength = 0;
690
- while (true) {
691
- const { value } = await reader.read();
692
- if (value) {
693
- chunks.push(value);
694
- totalLength += value.length;
695
- if (value[value.length - 1] !== NEWLINE) {
696
- continue;
697
- }
698
- }
699
- if (chunks.length === 0) {
700
- break;
701
- }
702
- let concatenatedChunks = new Uint8Array(totalLength);
703
- let offset = 0;
704
- for (const chunk of chunks) {
705
- concatenatedChunks.set(chunk, offset);
706
- offset += chunk.length;
707
- }
708
- chunks.length = 0;
709
- totalLength = 0;
710
- const lines = decode(concatenatedChunks);
711
- if (typeof lines === "string") {
712
- throw new Error(
713
- "Invalid response format. Complex mode was set but the response is a string. This should never happen."
714
- );
715
- }
716
- for (const { type, value: value2 } of lines) {
717
- if (type === "text") {
718
- if (prefixMap["text"]) {
719
- prefixMap["text"] = {
720
- ...prefixMap["text"],
721
- content: (prefixMap["text"].content || "") + value2
722
- };
723
- } else {
724
- prefixMap["text"] = {
725
- id: generateId(),
726
- role: "assistant",
727
- content: value2,
728
- createdAt
729
- };
730
- }
731
- }
732
- let functionCallMessage = null;
733
- if (type === "function_call") {
734
- prefixMap["function_call"] = {
727
+ for await (const { type, value } of readDataStream(reader, {
728
+ isAborted: () => (abortControllerRef == null ? void 0 : abortControllerRef.current) === null
729
+ })) {
730
+ if (type === "text") {
731
+ if (prefixMap["text"]) {
732
+ prefixMap["text"] = {
733
+ ...prefixMap["text"],
734
+ content: (prefixMap["text"].content || "") + value
735
+ };
736
+ } else {
737
+ prefixMap["text"] = {
735
738
  id: generateId(),
736
739
  role: "assistant",
737
- content: "",
738
- function_call: value2.function_call,
739
- name: value2.function_call.name,
740
+ content: value,
740
741
  createdAt
741
742
  };
742
- functionCallMessage = prefixMap["function_call"];
743
- }
744
- if (type === "data") {
745
- prefixMap["data"].push(...value2);
746
- }
747
- const responseMessage = prefixMap["text"];
748
- const merged = [functionCallMessage, responseMessage].filter(
749
- Boolean
750
- );
751
- update(merged, [...prefixMap["data"]]);
752
- if ((abortControllerRef == null ? void 0 : abortControllerRef.current) === null) {
753
- reader.cancel();
754
- break;
755
743
  }
756
744
  }
745
+ let functionCallMessage = null;
746
+ if (type === "function_call") {
747
+ prefixMap["function_call"] = {
748
+ id: generateId(),
749
+ role: "assistant",
750
+ content: "",
751
+ function_call: value.function_call,
752
+ name: value.function_call.name,
753
+ createdAt
754
+ };
755
+ functionCallMessage = prefixMap["function_call"];
756
+ }
757
+ if (type === "data") {
758
+ prefixMap["data"].push(...value);
759
+ }
760
+ const responseMessage = prefixMap["text"];
761
+ const merged = [functionCallMessage, responseMessage].filter(
762
+ Boolean
763
+ );
764
+ update(merged, [...prefixMap["data"]]);
757
765
  }
758
766
  onFinish == null ? void 0 : onFinish(prefixMap);
759
767
  return {
@@ -1154,6 +1162,7 @@ function useCompletion({
1154
1162
  fetcher: () => store2[key] || initialCompletion,
1155
1163
  fallbackData: initialCompletion
1156
1164
  });
1165
+ const streamData = (0, import_store2.writable)(void 0);
1157
1166
  const loading = (0, import_store2.writable)(false);
1158
1167
  data.set(initialCompletion);
1159
1168
  const mutate = (data2) => {
@@ -1202,17 +1211,37 @@ function useCompletion({
1202
1211
  }
1203
1212
  let result = "";
1204
1213
  const reader = res.body.getReader();
1205
- const decoder = createChunkDecoder();
1206
- while (true) {
1207
- const { done, value } = await reader.read();
1208
- if (done) {
1209
- break;
1214
+ const existingData = (0, import_store2.get)(streamData);
1215
+ const isComplexMode = res.headers.get(COMPLEX_HEADER) === "true";
1216
+ if (isComplexMode) {
1217
+ for await (const { type, value } of readDataStream(reader, {
1218
+ isAborted: () => abortController === null
1219
+ })) {
1220
+ switch (type) {
1221
+ case "text": {
1222
+ result += value;
1223
+ mutate(result);
1224
+ break;
1225
+ }
1226
+ case "data": {
1227
+ streamData.set([...existingData || [], ...value || []]);
1228
+ break;
1229
+ }
1230
+ }
1210
1231
  }
1211
- result += decoder(value);
1212
- mutate(result);
1213
- if (abortController === null) {
1214
- reader.cancel();
1215
- break;
1232
+ } else {
1233
+ const decoder = createChunkDecoder();
1234
+ while (true) {
1235
+ const { done, value } = await reader.read();
1236
+ if (done) {
1237
+ break;
1238
+ }
1239
+ result += decoder(value);
1240
+ mutate(result);
1241
+ if (abortController === null) {
1242
+ reader.cancel();
1243
+ break;
1244
+ }
1216
1245
  }
1217
1246
  }
1218
1247
  if (onFinish) {
@@ -1267,7 +1296,8 @@ function useCompletion({
1267
1296
  setCompletion,
1268
1297
  input,
1269
1298
  handleSubmit,
1270
- isLoading
1299
+ isLoading,
1300
+ data: streamData
1271
1301
  };
1272
1302
  }
1273
1303
  // Annotate the CommonJS export names for ESM import in node: