ai 2.2.25 → 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.
- package/dist/index.d.ts +8 -1
- package/dist/index.js +78 -66
- package/dist/index.mjs +78 -66
- package/package.json +14 -11
- package/react/dist/index.d.ts +32 -9
- package/react/dist/index.js +150 -125
- package/react/dist/index.mjs +150 -125
- package/solid/dist/index.d.ts +9 -1
- package/solid/dist/index.js +127 -90
- package/solid/dist/index.mjs +127 -90
- package/svelte/dist/index.d.ts +9 -1
- package/svelte/dist/index.js +125 -90
- package/svelte/dist/index.mjs +125 -90
- package/vue/dist/index.d.ts +14 -1
- package/vue/dist/index.js +358 -99
- package/vue/dist/index.mjs +358 -99
package/solid/dist/index.js
CHANGED
@@ -30,12 +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
|
-
// shared/utils.ts
|
37
|
-
var import_non_secure = require("nanoid/non-secure");
|
38
|
-
|
39
33
|
// shared/stream-parts.ts
|
40
34
|
var textStreamPart = {
|
41
35
|
code: "0",
|
@@ -157,7 +151,51 @@ var parseStreamPart = (line) => {
|
|
157
151
|
return streamPartsByCode[code].parse(jsonValue);
|
158
152
|
};
|
159
153
|
|
154
|
+
// shared/read-data-stream.ts
|
155
|
+
var NEWLINE = "\n".charCodeAt(0);
|
156
|
+
function concatChunks(chunks, totalLength) {
|
157
|
+
const concatenatedChunks = new Uint8Array(totalLength);
|
158
|
+
let offset = 0;
|
159
|
+
for (const chunk of chunks) {
|
160
|
+
concatenatedChunks.set(chunk, offset);
|
161
|
+
offset += chunk.length;
|
162
|
+
}
|
163
|
+
chunks.length = 0;
|
164
|
+
return concatenatedChunks;
|
165
|
+
}
|
166
|
+
async function* readDataStream(reader, {
|
167
|
+
isAborted
|
168
|
+
} = {}) {
|
169
|
+
const decoder = new TextDecoder();
|
170
|
+
const chunks = [];
|
171
|
+
let totalLength = 0;
|
172
|
+
while (true) {
|
173
|
+
const { value } = await reader.read();
|
174
|
+
if (value) {
|
175
|
+
chunks.push(value);
|
176
|
+
totalLength += value.length;
|
177
|
+
if (value[value.length - 1] !== NEWLINE) {
|
178
|
+
continue;
|
179
|
+
}
|
180
|
+
}
|
181
|
+
if (chunks.length === 0) {
|
182
|
+
break;
|
183
|
+
}
|
184
|
+
const concatenatedChunks = concatChunks(chunks, totalLength);
|
185
|
+
totalLength = 0;
|
186
|
+
const streamParts2 = decoder.decode(concatenatedChunks, { stream: true }).split("\n").filter((line) => line !== "").map(parseStreamPart);
|
187
|
+
for (const streamPart of streamParts2) {
|
188
|
+
yield streamPart;
|
189
|
+
}
|
190
|
+
if (isAborted == null ? void 0 : isAborted()) {
|
191
|
+
reader.cancel();
|
192
|
+
break;
|
193
|
+
}
|
194
|
+
}
|
195
|
+
}
|
196
|
+
|
160
197
|
// shared/utils.ts
|
198
|
+
var import_non_secure = require("nanoid/non-secure");
|
161
199
|
var nanoid = (0, import_non_secure.customAlphabet)(
|
162
200
|
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
|
163
201
|
7
|
@@ -188,80 +226,47 @@ async function parseComplexResponse({
|
|
188
226
|
getCurrentDate = () => /* @__PURE__ */ new Date()
|
189
227
|
}) {
|
190
228
|
const createdAt = getCurrentDate();
|
191
|
-
const decode = createChunkDecoder(true);
|
192
229
|
const prefixMap = {
|
193
230
|
data: []
|
194
231
|
};
|
195
|
-
const
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
}
|
207
|
-
if (chunks.length === 0) {
|
208
|
-
break;
|
209
|
-
}
|
210
|
-
let concatenatedChunks = new Uint8Array(totalLength);
|
211
|
-
let offset = 0;
|
212
|
-
for (const chunk of chunks) {
|
213
|
-
concatenatedChunks.set(chunk, offset);
|
214
|
-
offset += chunk.length;
|
215
|
-
}
|
216
|
-
chunks.length = 0;
|
217
|
-
totalLength = 0;
|
218
|
-
const lines = decode(concatenatedChunks);
|
219
|
-
if (typeof lines === "string") {
|
220
|
-
throw new Error(
|
221
|
-
"Invalid response format. Complex mode was set but the response is a string. This should never happen."
|
222
|
-
);
|
223
|
-
}
|
224
|
-
for (const { type, value: value2 } of lines) {
|
225
|
-
if (type === "text") {
|
226
|
-
if (prefixMap["text"]) {
|
227
|
-
prefixMap["text"] = {
|
228
|
-
...prefixMap["text"],
|
229
|
-
content: (prefixMap["text"].content || "") + value2
|
230
|
-
};
|
231
|
-
} else {
|
232
|
-
prefixMap["text"] = {
|
233
|
-
id: generateId(),
|
234
|
-
role: "assistant",
|
235
|
-
content: value2,
|
236
|
-
createdAt
|
237
|
-
};
|
238
|
-
}
|
239
|
-
}
|
240
|
-
let functionCallMessage = null;
|
241
|
-
if (type === "function_call") {
|
242
|
-
prefixMap["function_call"] = {
|
232
|
+
for await (const { type, value } of readDataStream(reader, {
|
233
|
+
isAborted: () => (abortControllerRef == null ? void 0 : abortControllerRef.current) === null
|
234
|
+
})) {
|
235
|
+
if (type === "text") {
|
236
|
+
if (prefixMap["text"]) {
|
237
|
+
prefixMap["text"] = {
|
238
|
+
...prefixMap["text"],
|
239
|
+
content: (prefixMap["text"].content || "") + value
|
240
|
+
};
|
241
|
+
} else {
|
242
|
+
prefixMap["text"] = {
|
243
243
|
id: generateId(),
|
244
244
|
role: "assistant",
|
245
|
-
content:
|
246
|
-
function_call: value2.function_call,
|
247
|
-
name: value2.function_call.name,
|
245
|
+
content: value,
|
248
246
|
createdAt
|
249
247
|
};
|
250
|
-
functionCallMessage = prefixMap["function_call"];
|
251
|
-
}
|
252
|
-
if (type === "data") {
|
253
|
-
prefixMap["data"].push(...value2);
|
254
|
-
}
|
255
|
-
const responseMessage = prefixMap["text"];
|
256
|
-
const merged = [functionCallMessage, responseMessage].filter(
|
257
|
-
Boolean
|
258
|
-
);
|
259
|
-
update(merged, [...prefixMap["data"]]);
|
260
|
-
if ((abortControllerRef == null ? void 0 : abortControllerRef.current) === null) {
|
261
|
-
reader.cancel();
|
262
|
-
break;
|
263
248
|
}
|
264
249
|
}
|
250
|
+
let functionCallMessage = null;
|
251
|
+
if (type === "function_call") {
|
252
|
+
prefixMap["function_call"] = {
|
253
|
+
id: generateId(),
|
254
|
+
role: "assistant",
|
255
|
+
content: "",
|
256
|
+
function_call: value.function_call,
|
257
|
+
name: value.function_call.name,
|
258
|
+
createdAt
|
259
|
+
};
|
260
|
+
functionCallMessage = prefixMap["function_call"];
|
261
|
+
}
|
262
|
+
if (type === "data") {
|
263
|
+
prefixMap["data"].push(...value);
|
264
|
+
}
|
265
|
+
const responseMessage = prefixMap["text"];
|
266
|
+
const merged = [functionCallMessage, responseMessage].filter(
|
267
|
+
Boolean
|
268
|
+
);
|
269
|
+
update(merged, [...prefixMap["data"]]);
|
265
270
|
}
|
266
271
|
onFinish == null ? void 0 : onFinish(prefixMap);
|
267
272
|
return {
|
@@ -284,7 +289,8 @@ async function callApi({
|
|
284
289
|
restoreMessagesOnFailure,
|
285
290
|
onResponse,
|
286
291
|
onUpdate,
|
287
|
-
onFinish
|
292
|
+
onFinish,
|
293
|
+
generateId
|
288
294
|
}) {
|
289
295
|
var _a;
|
290
296
|
const response = await fetch(api, {
|
@@ -293,7 +299,10 @@ async function callApi({
|
|
293
299
|
messages,
|
294
300
|
...body
|
295
301
|
}),
|
296
|
-
headers
|
302
|
+
headers: {
|
303
|
+
"Content-Type": "application/json",
|
304
|
+
...headers
|
305
|
+
},
|
297
306
|
signal: (_a = abortController == null ? void 0 : abortController()) == null ? void 0 : _a.signal,
|
298
307
|
credentials
|
299
308
|
}).catch((err) => {
|
@@ -327,13 +336,14 @@ async function callApi({
|
|
327
336
|
if (onFinish && prefixMap.text != null) {
|
328
337
|
onFinish(prefixMap.text);
|
329
338
|
}
|
330
|
-
}
|
339
|
+
},
|
340
|
+
generateId
|
331
341
|
});
|
332
342
|
} else {
|
333
343
|
const createdAt = /* @__PURE__ */ new Date();
|
334
344
|
const decode = createChunkDecoder(false);
|
335
345
|
let streamedResponse = "";
|
336
|
-
const replyId = (
|
346
|
+
const replyId = generateId();
|
337
347
|
let responseMessage = {
|
338
348
|
id: replyId,
|
339
349
|
createdAt,
|
@@ -438,7 +448,8 @@ function useChat({
|
|
438
448
|
onError,
|
439
449
|
credentials,
|
440
450
|
headers,
|
441
|
-
body
|
451
|
+
body,
|
452
|
+
generateId = nanoid
|
442
453
|
} = {}) {
|
443
454
|
const chatId = id || `chat-${uniqueId++}`;
|
444
455
|
const key = `${api}|${chatId}`;
|
@@ -511,7 +522,8 @@ function useChat({
|
|
511
522
|
if (previousMessages.status === "success") {
|
512
523
|
mutate(previousMessages.data);
|
513
524
|
}
|
514
|
-
}
|
525
|
+
},
|
526
|
+
generateId
|
515
527
|
});
|
516
528
|
},
|
517
529
|
experimental_onFunctionCall,
|
@@ -537,7 +549,7 @@ function useChat({
|
|
537
549
|
const append = async (message, options) => {
|
538
550
|
var _a;
|
539
551
|
if (!message.id) {
|
540
|
-
message.id =
|
552
|
+
message.id = generateId();
|
541
553
|
}
|
542
554
|
return triggerRequest(
|
543
555
|
((_a = messages()) != null ? _a : []).concat(message),
|
@@ -593,8 +605,8 @@ function useChat({
|
|
593
605
|
|
594
606
|
// solid/use-completion.ts
|
595
607
|
var import_solid_js2 = require("solid-js");
|
596
|
-
var import_swr_store2 = require("swr-store");
|
597
608
|
var import_solid_swr_store2 = require("solid-swr-store");
|
609
|
+
var import_swr_store2 = require("swr-store");
|
598
610
|
var uniqueId2 = 0;
|
599
611
|
var store2 = {};
|
600
612
|
var completionApiStore = (0, import_swr_store2.createSWRStore)({
|
@@ -629,9 +641,13 @@ function useCompletion({
|
|
629
641
|
};
|
630
642
|
const completion = data;
|
631
643
|
const [error, setError] = (0, import_solid_js2.createSignal)(void 0);
|
644
|
+
const [streamData, setStreamData] = (0, import_solid_js2.createSignal)(
|
645
|
+
void 0
|
646
|
+
);
|
632
647
|
const [isLoading, setIsLoading] = (0, import_solid_js2.createSignal)(false);
|
633
648
|
let abortController = null;
|
634
649
|
async function triggerRequest(prompt, options) {
|
650
|
+
var _a;
|
635
651
|
try {
|
636
652
|
setError(void 0);
|
637
653
|
setIsLoading(true);
|
@@ -670,17 +686,37 @@ function useCompletion({
|
|
670
686
|
}
|
671
687
|
let result = "";
|
672
688
|
const reader = res.body.getReader();
|
673
|
-
const
|
674
|
-
|
675
|
-
const
|
676
|
-
|
677
|
-
|
689
|
+
const isComplexMode = res.headers.get(COMPLEX_HEADER) === "true";
|
690
|
+
if (isComplexMode) {
|
691
|
+
const existingData = (_a = streamData()) != null ? _a : [];
|
692
|
+
for await (const { type, value } of readDataStream(reader, {
|
693
|
+
isAborted: () => abortController === null
|
694
|
+
})) {
|
695
|
+
switch (type) {
|
696
|
+
case "text": {
|
697
|
+
result += value;
|
698
|
+
mutate(result);
|
699
|
+
break;
|
700
|
+
}
|
701
|
+
case "data": {
|
702
|
+
setStreamData([...existingData, ...value != null ? value : []]);
|
703
|
+
break;
|
704
|
+
}
|
705
|
+
}
|
678
706
|
}
|
679
|
-
|
680
|
-
|
681
|
-
|
682
|
-
reader.
|
683
|
-
|
707
|
+
} else {
|
708
|
+
const decoder = createChunkDecoder();
|
709
|
+
while (true) {
|
710
|
+
const { done, value } = await reader.read();
|
711
|
+
if (done) {
|
712
|
+
break;
|
713
|
+
}
|
714
|
+
result += decoder(value);
|
715
|
+
mutate(result);
|
716
|
+
if (abortController === null) {
|
717
|
+
reader.cancel();
|
718
|
+
break;
|
719
|
+
}
|
684
720
|
}
|
685
721
|
}
|
686
722
|
if (onFinish) {
|
@@ -730,7 +766,8 @@ function useCompletion({
|
|
730
766
|
input,
|
731
767
|
setInput,
|
732
768
|
handleSubmit,
|
733
|
-
isLoading
|
769
|
+
isLoading,
|
770
|
+
data: streamData
|
734
771
|
};
|
735
772
|
}
|
736
773
|
// Annotate the CommonJS export names for ESM import in node:
|
package/solid/dist/index.mjs
CHANGED
@@ -3,12 +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
|
-
// shared/utils.ts
|
10
|
-
import { customAlphabet } from "nanoid/non-secure";
|
11
|
-
|
12
6
|
// shared/stream-parts.ts
|
13
7
|
var textStreamPart = {
|
14
8
|
code: "0",
|
@@ -130,7 +124,51 @@ var parseStreamPart = (line) => {
|
|
130
124
|
return streamPartsByCode[code].parse(jsonValue);
|
131
125
|
};
|
132
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
|
+
|
133
170
|
// shared/utils.ts
|
171
|
+
import { customAlphabet } from "nanoid/non-secure";
|
134
172
|
var nanoid = customAlphabet(
|
135
173
|
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
|
136
174
|
7
|
@@ -161,80 +199,47 @@ async function parseComplexResponse({
|
|
161
199
|
getCurrentDate = () => /* @__PURE__ */ new Date()
|
162
200
|
}) {
|
163
201
|
const createdAt = getCurrentDate();
|
164
|
-
const decode = createChunkDecoder(true);
|
165
202
|
const prefixMap = {
|
166
203
|
data: []
|
167
204
|
};
|
168
|
-
const
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
}
|
180
|
-
if (chunks.length === 0) {
|
181
|
-
break;
|
182
|
-
}
|
183
|
-
let concatenatedChunks = new Uint8Array(totalLength);
|
184
|
-
let offset = 0;
|
185
|
-
for (const chunk of chunks) {
|
186
|
-
concatenatedChunks.set(chunk, offset);
|
187
|
-
offset += chunk.length;
|
188
|
-
}
|
189
|
-
chunks.length = 0;
|
190
|
-
totalLength = 0;
|
191
|
-
const lines = decode(concatenatedChunks);
|
192
|
-
if (typeof lines === "string") {
|
193
|
-
throw new Error(
|
194
|
-
"Invalid response format. Complex mode was set but the response is a string. This should never happen."
|
195
|
-
);
|
196
|
-
}
|
197
|
-
for (const { type, value: value2 } of lines) {
|
198
|
-
if (type === "text") {
|
199
|
-
if (prefixMap["text"]) {
|
200
|
-
prefixMap["text"] = {
|
201
|
-
...prefixMap["text"],
|
202
|
-
content: (prefixMap["text"].content || "") + value2
|
203
|
-
};
|
204
|
-
} else {
|
205
|
-
prefixMap["text"] = {
|
206
|
-
id: generateId(),
|
207
|
-
role: "assistant",
|
208
|
-
content: value2,
|
209
|
-
createdAt
|
210
|
-
};
|
211
|
-
}
|
212
|
-
}
|
213
|
-
let functionCallMessage = null;
|
214
|
-
if (type === "function_call") {
|
215
|
-
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"] = {
|
216
216
|
id: generateId(),
|
217
217
|
role: "assistant",
|
218
|
-
content:
|
219
|
-
function_call: value2.function_call,
|
220
|
-
name: value2.function_call.name,
|
218
|
+
content: value,
|
221
219
|
createdAt
|
222
220
|
};
|
223
|
-
functionCallMessage = prefixMap["function_call"];
|
224
|
-
}
|
225
|
-
if (type === "data") {
|
226
|
-
prefixMap["data"].push(...value2);
|
227
|
-
}
|
228
|
-
const responseMessage = prefixMap["text"];
|
229
|
-
const merged = [functionCallMessage, responseMessage].filter(
|
230
|
-
Boolean
|
231
|
-
);
|
232
|
-
update(merged, [...prefixMap["data"]]);
|
233
|
-
if ((abortControllerRef == null ? void 0 : abortControllerRef.current) === null) {
|
234
|
-
reader.cancel();
|
235
|
-
break;
|
236
221
|
}
|
237
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"]]);
|
238
243
|
}
|
239
244
|
onFinish == null ? void 0 : onFinish(prefixMap);
|
240
245
|
return {
|
@@ -257,7 +262,8 @@ async function callApi({
|
|
257
262
|
restoreMessagesOnFailure,
|
258
263
|
onResponse,
|
259
264
|
onUpdate,
|
260
|
-
onFinish
|
265
|
+
onFinish,
|
266
|
+
generateId
|
261
267
|
}) {
|
262
268
|
var _a;
|
263
269
|
const response = await fetch(api, {
|
@@ -266,7 +272,10 @@ async function callApi({
|
|
266
272
|
messages,
|
267
273
|
...body
|
268
274
|
}),
|
269
|
-
headers
|
275
|
+
headers: {
|
276
|
+
"Content-Type": "application/json",
|
277
|
+
...headers
|
278
|
+
},
|
270
279
|
signal: (_a = abortController == null ? void 0 : abortController()) == null ? void 0 : _a.signal,
|
271
280
|
credentials
|
272
281
|
}).catch((err) => {
|
@@ -300,13 +309,14 @@ async function callApi({
|
|
300
309
|
if (onFinish && prefixMap.text != null) {
|
301
310
|
onFinish(prefixMap.text);
|
302
311
|
}
|
303
|
-
}
|
312
|
+
},
|
313
|
+
generateId
|
304
314
|
});
|
305
315
|
} else {
|
306
316
|
const createdAt = /* @__PURE__ */ new Date();
|
307
317
|
const decode = createChunkDecoder(false);
|
308
318
|
let streamedResponse = "";
|
309
|
-
const replyId =
|
319
|
+
const replyId = generateId();
|
310
320
|
let responseMessage = {
|
311
321
|
id: replyId,
|
312
322
|
createdAt,
|
@@ -411,7 +421,8 @@ function useChat({
|
|
411
421
|
onError,
|
412
422
|
credentials,
|
413
423
|
headers,
|
414
|
-
body
|
424
|
+
body,
|
425
|
+
generateId = nanoid
|
415
426
|
} = {}) {
|
416
427
|
const chatId = id || `chat-${uniqueId++}`;
|
417
428
|
const key = `${api}|${chatId}`;
|
@@ -484,7 +495,8 @@ function useChat({
|
|
484
495
|
if (previousMessages.status === "success") {
|
485
496
|
mutate(previousMessages.data);
|
486
497
|
}
|
487
|
-
}
|
498
|
+
},
|
499
|
+
generateId
|
488
500
|
});
|
489
501
|
},
|
490
502
|
experimental_onFunctionCall,
|
@@ -510,7 +522,7 @@ function useChat({
|
|
510
522
|
const append = async (message, options) => {
|
511
523
|
var _a;
|
512
524
|
if (!message.id) {
|
513
|
-
message.id =
|
525
|
+
message.id = generateId();
|
514
526
|
}
|
515
527
|
return triggerRequest(
|
516
528
|
((_a = messages()) != null ? _a : []).concat(message),
|
@@ -566,8 +578,8 @@ function useChat({
|
|
566
578
|
|
567
579
|
// solid/use-completion.ts
|
568
580
|
import { createSignal as createSignal2 } from "solid-js";
|
569
|
-
import { createSWRStore as createSWRStore2 } from "swr-store";
|
570
581
|
import { useSWRStore as useSWRStore2 } from "solid-swr-store";
|
582
|
+
import { createSWRStore as createSWRStore2 } from "swr-store";
|
571
583
|
var uniqueId2 = 0;
|
572
584
|
var store2 = {};
|
573
585
|
var completionApiStore = createSWRStore2({
|
@@ -602,9 +614,13 @@ function useCompletion({
|
|
602
614
|
};
|
603
615
|
const completion = data;
|
604
616
|
const [error, setError] = createSignal2(void 0);
|
617
|
+
const [streamData, setStreamData] = createSignal2(
|
618
|
+
void 0
|
619
|
+
);
|
605
620
|
const [isLoading, setIsLoading] = createSignal2(false);
|
606
621
|
let abortController = null;
|
607
622
|
async function triggerRequest(prompt, options) {
|
623
|
+
var _a;
|
608
624
|
try {
|
609
625
|
setError(void 0);
|
610
626
|
setIsLoading(true);
|
@@ -643,17 +659,37 @@ function useCompletion({
|
|
643
659
|
}
|
644
660
|
let result = "";
|
645
661
|
const reader = res.body.getReader();
|
646
|
-
const
|
647
|
-
|
648
|
-
const
|
649
|
-
|
650
|
-
|
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
|
+
}
|
651
679
|
}
|
652
|
-
|
653
|
-
|
654
|
-
|
655
|
-
reader.
|
656
|
-
|
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
|
+
}
|
657
693
|
}
|
658
694
|
}
|
659
695
|
if (onFinish) {
|
@@ -703,7 +739,8 @@ function useCompletion({
|
|
703
739
|
input,
|
704
740
|
setInput,
|
705
741
|
handleSubmit,
|
706
|
-
isLoading
|
742
|
+
isLoading,
|
743
|
+
data: streamData
|
707
744
|
};
|
708
745
|
}
|
709
746
|
export {
|
package/svelte/dist/index.d.ts
CHANGED
@@ -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 */
|
@@ -276,6 +282,8 @@ type UseCompletionHelpers = {
|
|
276
282
|
handleSubmit: (e: any) => void;
|
277
283
|
/** Whether the API request is in progress */
|
278
284
|
isLoading: Readable<boolean | undefined>;
|
285
|
+
/** Additional data added on the server via StreamData */
|
286
|
+
data: Readable<JSONValue[] | undefined>;
|
279
287
|
};
|
280
288
|
declare function useCompletion({ api, id, initialCompletion, initialInput, credentials, headers, body, onResponse, onFinish, onError, }?: UseCompletionOptions): UseCompletionHelpers;
|
281
289
|
|