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.
@@ -2,9 +2,6 @@
2
2
  import swrv from "swrv";
3
3
  import { ref, unref } from "vue";
4
4
 
5
- // shared/utils.ts
6
- import { customAlphabet } from "nanoid/non-secure";
7
-
8
5
  // shared/stream-parts.ts
9
6
  var textStreamPart = {
10
7
  code: "0",
@@ -126,7 +123,51 @@ var parseStreamPart = (line) => {
126
123
  return streamPartsByCode[code].parse(jsonValue);
127
124
  };
128
125
 
126
+ // shared/read-data-stream.ts
127
+ var NEWLINE = "\n".charCodeAt(0);
128
+ function concatChunks(chunks, totalLength) {
129
+ const concatenatedChunks = new Uint8Array(totalLength);
130
+ let offset = 0;
131
+ for (const chunk of chunks) {
132
+ concatenatedChunks.set(chunk, offset);
133
+ offset += chunk.length;
134
+ }
135
+ chunks.length = 0;
136
+ return concatenatedChunks;
137
+ }
138
+ async function* readDataStream(reader, {
139
+ isAborted
140
+ } = {}) {
141
+ const decoder = new TextDecoder();
142
+ const chunks = [];
143
+ let totalLength = 0;
144
+ while (true) {
145
+ const { value } = await reader.read();
146
+ if (value) {
147
+ chunks.push(value);
148
+ totalLength += value.length;
149
+ if (value[value.length - 1] !== NEWLINE) {
150
+ continue;
151
+ }
152
+ }
153
+ if (chunks.length === 0) {
154
+ break;
155
+ }
156
+ const concatenatedChunks = concatChunks(chunks, totalLength);
157
+ totalLength = 0;
158
+ const streamParts2 = decoder.decode(concatenatedChunks, { stream: true }).split("\n").filter((line) => line !== "").map(parseStreamPart);
159
+ for (const streamPart of streamParts2) {
160
+ yield streamPart;
161
+ }
162
+ if (isAborted == null ? void 0 : isAborted()) {
163
+ reader.cancel();
164
+ break;
165
+ }
166
+ }
167
+ }
168
+
129
169
  // shared/utils.ts
170
+ import { customAlphabet } from "nanoid/non-secure";
130
171
  var nanoid = customAlphabet(
131
172
  "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
132
173
  7
@@ -157,80 +198,47 @@ async function parseComplexResponse({
157
198
  getCurrentDate = () => /* @__PURE__ */ new Date()
158
199
  }) {
159
200
  const createdAt = getCurrentDate();
160
- const decode = createChunkDecoder(true);
161
201
  const prefixMap = {
162
202
  data: []
163
203
  };
164
- const NEWLINE = "\n".charCodeAt(0);
165
- const chunks = [];
166
- let totalLength = 0;
167
- while (true) {
168
- const { value } = await reader.read();
169
- if (value) {
170
- chunks.push(value);
171
- totalLength += value.length;
172
- if (value[value.length - 1] !== NEWLINE) {
173
- continue;
174
- }
175
- }
176
- if (chunks.length === 0) {
177
- break;
178
- }
179
- let concatenatedChunks = new Uint8Array(totalLength);
180
- let offset = 0;
181
- for (const chunk of chunks) {
182
- concatenatedChunks.set(chunk, offset);
183
- offset += chunk.length;
184
- }
185
- chunks.length = 0;
186
- totalLength = 0;
187
- const lines = decode(concatenatedChunks);
188
- if (typeof lines === "string") {
189
- throw new Error(
190
- "Invalid response format. Complex mode was set but the response is a string. This should never happen."
191
- );
192
- }
193
- for (const { type, value: value2 } of lines) {
194
- if (type === "text") {
195
- if (prefixMap["text"]) {
196
- prefixMap["text"] = {
197
- ...prefixMap["text"],
198
- content: (prefixMap["text"].content || "") + value2
199
- };
200
- } else {
201
- prefixMap["text"] = {
202
- id: generateId(),
203
- role: "assistant",
204
- content: value2,
205
- createdAt
206
- };
207
- }
208
- }
209
- let functionCallMessage = null;
210
- if (type === "function_call") {
211
- prefixMap["function_call"] = {
204
+ for await (const { type, value } of readDataStream(reader, {
205
+ isAborted: () => (abortControllerRef == null ? void 0 : abortControllerRef.current) === null
206
+ })) {
207
+ if (type === "text") {
208
+ if (prefixMap["text"]) {
209
+ prefixMap["text"] = {
210
+ ...prefixMap["text"],
211
+ content: (prefixMap["text"].content || "") + value
212
+ };
213
+ } else {
214
+ prefixMap["text"] = {
212
215
  id: generateId(),
213
216
  role: "assistant",
214
- content: "",
215
- function_call: value2.function_call,
216
- name: value2.function_call.name,
217
+ content: value,
217
218
  createdAt
218
219
  };
219
- functionCallMessage = prefixMap["function_call"];
220
- }
221
- if (type === "data") {
222
- prefixMap["data"].push(...value2);
223
- }
224
- const responseMessage = prefixMap["text"];
225
- const merged = [functionCallMessage, responseMessage].filter(
226
- Boolean
227
- );
228
- update(merged, [...prefixMap["data"]]);
229
- if ((abortControllerRef == null ? void 0 : abortControllerRef.current) === null) {
230
- reader.cancel();
231
- break;
232
220
  }
233
221
  }
222
+ let functionCallMessage = null;
223
+ if (type === "function_call") {
224
+ prefixMap["function_call"] = {
225
+ id: generateId(),
226
+ role: "assistant",
227
+ content: "",
228
+ function_call: value.function_call,
229
+ name: value.function_call.name,
230
+ createdAt
231
+ };
232
+ functionCallMessage = prefixMap["function_call"];
233
+ }
234
+ if (type === "data") {
235
+ prefixMap["data"].push(...value);
236
+ }
237
+ const responseMessage = prefixMap["text"];
238
+ const merged = [functionCallMessage, responseMessage].filter(
239
+ Boolean
240
+ );
241
+ update(merged, [...prefixMap["data"]]);
234
242
  }
235
243
  onFinish == null ? void 0 : onFinish(prefixMap);
236
244
  return {
@@ -588,6 +596,7 @@ function useCompletion({
588
596
  null
589
597
  );
590
598
  (_a = isLoading.value) != null ? _a : isLoading.value = false;
599
+ const { data: streamData, mutate: mutateStreamData } = useSWRV2(`${completionId}-data`, null);
591
600
  data.value || (data.value = initialCompletion);
592
601
  const mutate = (data2) => {
593
602
  store2[key] = data2;
@@ -597,6 +606,7 @@ function useCompletion({
597
606
  const error = ref2(void 0);
598
607
  let abortController = null;
599
608
  async function triggerRequest(prompt, options) {
609
+ var _a2;
600
610
  try {
601
611
  error.value = void 0;
602
612
  mutateLoading(() => true);
@@ -610,6 +620,7 @@ function useCompletion({
610
620
  ...options == null ? void 0 : options.body
611
621
  }),
612
622
  headers: {
623
+ "Content-Type": "application/json",
613
624
  ...headers,
614
625
  ...options == null ? void 0 : options.headers
615
626
  },
@@ -635,17 +646,37 @@ function useCompletion({
635
646
  }
636
647
  let result = "";
637
648
  const reader = res.body.getReader();
638
- const decoder = createChunkDecoder();
639
- while (true) {
640
- const { done, value } = await reader.read();
641
- if (done) {
642
- break;
649
+ const existingData = (_a2 = streamData.value) != null ? _a2 : [];
650
+ const isComplexMode = res.headers.get(COMPLEX_HEADER) === "true";
651
+ if (isComplexMode) {
652
+ for await (const { type, value } of readDataStream(reader, {
653
+ isAborted: () => abortController === null
654
+ })) {
655
+ switch (type) {
656
+ case "text": {
657
+ result += value;
658
+ mutate(result);
659
+ break;
660
+ }
661
+ case "data": {
662
+ streamData.value = [...existingData, ...value != null ? value : []];
663
+ break;
664
+ }
665
+ }
643
666
  }
644
- result += decoder(value);
645
- mutate(result);
646
- if (abortController === null) {
647
- reader.cancel();
648
- break;
667
+ } else {
668
+ const decoder = createChunkDecoder();
669
+ while (true) {
670
+ const { done, value } = await reader.read();
671
+ if (done) {
672
+ break;
673
+ }
674
+ result += decoder(value);
675
+ mutate(result);
676
+ if (abortController === null) {
677
+ reader.cancel();
678
+ break;
679
+ }
649
680
  }
650
681
  }
651
682
  if (onFinish) {
@@ -694,7 +725,8 @@ function useCompletion({
694
725
  setCompletion,
695
726
  input,
696
727
  handleSubmit,
697
- isLoading
728
+ isLoading,
729
+ data: streamData
698
730
  };
699
731
  }
700
732
  export {