ai 2.2.26 → 2.2.28
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 +15 -6
- package/dist/index.js +106 -71
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +106 -71
- package/dist/index.mjs.map +1 -0
- package/package.json +22 -12
- package/prompts/dist/index.d.ts +5 -1
- package/prompts/dist/index.js +1 -0
- package/prompts/dist/index.js.map +1 -0
- package/prompts/dist/index.mjs +1 -0
- package/prompts/dist/index.mjs.map +1 -0
- package/react/dist/index.d.ts +12 -4
- package/react/dist/index.js +273 -196
- package/react/dist/index.js.map +1 -0
- package/react/dist/index.mjs +273 -196
- package/react/dist/index.mjs.map +1 -0
- package/react/dist/index.server.js +1 -0
- package/react/dist/index.server.js.map +1 -0
- package/react/dist/index.server.mjs +1 -0
- package/react/dist/index.server.mjs.map +1 -0
- package/solid/dist/index.d.ts +4 -1
- package/solid/dist/index.js +248 -152
- package/solid/dist/index.js.map +1 -0
- package/solid/dist/index.mjs +248 -152
- package/solid/dist/index.mjs.map +1 -0
- package/svelte/dist/index.d.ts +4 -1
- package/svelte/dist/index.js +244 -151
- package/svelte/dist/index.js.map +1 -0
- package/svelte/dist/index.mjs +244 -151
- package/svelte/dist/index.mjs.map +1 -0
- package/vue/dist/index.d.ts +4 -1
- package/vue/dist/index.js +246 -147
- package/vue/dist/index.js.map +1 -0
- package/vue/dist/index.mjs +246 -147
- package/vue/dist/index.mjs.map +1 -0
package/svelte/dist/index.js
CHANGED
@@ -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",
|
@@ -574,7 +571,7 @@ var errorStreamPart = {
|
|
574
571
|
return { type: "error", value };
|
575
572
|
}
|
576
573
|
};
|
577
|
-
var
|
574
|
+
var assistantMessageStreamPart = {
|
578
575
|
code: "4",
|
579
576
|
name: "assistant_message",
|
580
577
|
parse: (value) => {
|
@@ -591,7 +588,7 @@ var assistantMessage = {
|
|
591
588
|
};
|
592
589
|
}
|
593
590
|
};
|
594
|
-
var
|
591
|
+
var assistantControlDataStreamPart = {
|
595
592
|
code: "5",
|
596
593
|
name: "assistant_control_data",
|
597
594
|
parse: (value) => {
|
@@ -609,29 +606,47 @@ var assistantControlData = {
|
|
609
606
|
};
|
610
607
|
}
|
611
608
|
};
|
609
|
+
var dataMessageStreamPart = {
|
610
|
+
code: "6",
|
611
|
+
name: "data_message",
|
612
|
+
parse: (value) => {
|
613
|
+
if (value == null || typeof value !== "object" || !("role" in value) || !("data" in value) || typeof value.role !== "string" || value.role !== "data") {
|
614
|
+
throw new Error(
|
615
|
+
'"data_message" parts expect an object with a "role" and "data" property.'
|
616
|
+
);
|
617
|
+
}
|
618
|
+
return {
|
619
|
+
type: "data_message",
|
620
|
+
value
|
621
|
+
};
|
622
|
+
}
|
623
|
+
};
|
612
624
|
var streamParts = [
|
613
625
|
textStreamPart,
|
614
626
|
functionCallStreamPart,
|
615
627
|
dataStreamPart,
|
616
628
|
errorStreamPart,
|
617
|
-
|
618
|
-
|
629
|
+
assistantMessageStreamPart,
|
630
|
+
assistantControlDataStreamPart,
|
631
|
+
dataMessageStreamPart
|
619
632
|
];
|
620
633
|
var streamPartsByCode = {
|
621
634
|
[textStreamPart.code]: textStreamPart,
|
622
635
|
[functionCallStreamPart.code]: functionCallStreamPart,
|
623
636
|
[dataStreamPart.code]: dataStreamPart,
|
624
637
|
[errorStreamPart.code]: errorStreamPart,
|
625
|
-
[
|
626
|
-
[
|
638
|
+
[assistantMessageStreamPart.code]: assistantMessageStreamPart,
|
639
|
+
[assistantControlDataStreamPart.code]: assistantControlDataStreamPart,
|
640
|
+
[dataMessageStreamPart.code]: dataMessageStreamPart
|
627
641
|
};
|
628
642
|
var StreamStringPrefixes = {
|
629
643
|
[textStreamPart.name]: textStreamPart.code,
|
630
644
|
[functionCallStreamPart.name]: functionCallStreamPart.code,
|
631
645
|
[dataStreamPart.name]: dataStreamPart.code,
|
632
646
|
[errorStreamPart.name]: errorStreamPart.code,
|
633
|
-
[
|
634
|
-
[
|
647
|
+
[assistantMessageStreamPart.name]: assistantMessageStreamPart.code,
|
648
|
+
[assistantControlDataStreamPart.name]: assistantControlDataStreamPart.code,
|
649
|
+
[dataMessageStreamPart.name]: dataMessageStreamPart.code
|
635
650
|
};
|
636
651
|
var validCodes = streamParts.map((part) => part.code);
|
637
652
|
var parseStreamPart = (line) => {
|
@@ -649,7 +664,51 @@ var parseStreamPart = (line) => {
|
|
649
664
|
return streamPartsByCode[code].parse(jsonValue);
|
650
665
|
};
|
651
666
|
|
667
|
+
// shared/read-data-stream.ts
|
668
|
+
var NEWLINE = "\n".charCodeAt(0);
|
669
|
+
function concatChunks(chunks, totalLength) {
|
670
|
+
const concatenatedChunks = new Uint8Array(totalLength);
|
671
|
+
let offset = 0;
|
672
|
+
for (const chunk of chunks) {
|
673
|
+
concatenatedChunks.set(chunk, offset);
|
674
|
+
offset += chunk.length;
|
675
|
+
}
|
676
|
+
chunks.length = 0;
|
677
|
+
return concatenatedChunks;
|
678
|
+
}
|
679
|
+
async function* readDataStream(reader, {
|
680
|
+
isAborted
|
681
|
+
} = {}) {
|
682
|
+
const decoder = new TextDecoder();
|
683
|
+
const 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
|
+
const concatenatedChunks = concatChunks(chunks, totalLength);
|
698
|
+
totalLength = 0;
|
699
|
+
const streamParts2 = decoder.decode(concatenatedChunks, { stream: true }).split("\n").filter((line) => line !== "").map(parseStreamPart);
|
700
|
+
for (const streamPart of streamParts2) {
|
701
|
+
yield streamPart;
|
702
|
+
}
|
703
|
+
if (isAborted == null ? void 0 : isAborted()) {
|
704
|
+
reader.cancel();
|
705
|
+
break;
|
706
|
+
}
|
707
|
+
}
|
708
|
+
}
|
709
|
+
|
652
710
|
// shared/utils.ts
|
711
|
+
var import_non_secure = require("nanoid/non-secure");
|
653
712
|
var nanoid = (0, import_non_secure.customAlphabet)(
|
654
713
|
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
|
655
714
|
7
|
@@ -680,80 +739,47 @@ async function parseComplexResponse({
|
|
680
739
|
getCurrentDate = () => /* @__PURE__ */ new Date()
|
681
740
|
}) {
|
682
741
|
const createdAt = getCurrentDate();
|
683
|
-
const decode = createChunkDecoder(true);
|
684
742
|
const prefixMap = {
|
685
743
|
data: []
|
686
744
|
};
|
687
|
-
const
|
688
|
-
|
689
|
-
|
690
|
-
|
691
|
-
|
692
|
-
|
693
|
-
|
694
|
-
|
695
|
-
|
696
|
-
|
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"] = {
|
745
|
+
for await (const { type, value } of readDataStream(reader, {
|
746
|
+
isAborted: () => (abortControllerRef == null ? void 0 : abortControllerRef.current) === null
|
747
|
+
})) {
|
748
|
+
if (type === "text") {
|
749
|
+
if (prefixMap["text"]) {
|
750
|
+
prefixMap["text"] = {
|
751
|
+
...prefixMap["text"],
|
752
|
+
content: (prefixMap["text"].content || "") + value
|
753
|
+
};
|
754
|
+
} else {
|
755
|
+
prefixMap["text"] = {
|
735
756
|
id: generateId(),
|
736
757
|
role: "assistant",
|
737
|
-
content:
|
738
|
-
function_call: value2.function_call,
|
739
|
-
name: value2.function_call.name,
|
758
|
+
content: value,
|
740
759
|
createdAt
|
741
760
|
};
|
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
761
|
}
|
756
762
|
}
|
763
|
+
let functionCallMessage = null;
|
764
|
+
if (type === "function_call") {
|
765
|
+
prefixMap["function_call"] = {
|
766
|
+
id: generateId(),
|
767
|
+
role: "assistant",
|
768
|
+
content: "",
|
769
|
+
function_call: value.function_call,
|
770
|
+
name: value.function_call.name,
|
771
|
+
createdAt
|
772
|
+
};
|
773
|
+
functionCallMessage = prefixMap["function_call"];
|
774
|
+
}
|
775
|
+
if (type === "data") {
|
776
|
+
prefixMap["data"].push(...value);
|
777
|
+
}
|
778
|
+
const responseMessage = prefixMap["text"];
|
779
|
+
const merged = [functionCallMessage, responseMessage].filter(
|
780
|
+
Boolean
|
781
|
+
);
|
782
|
+
update(merged, [...prefixMap["data"]]);
|
757
783
|
}
|
758
784
|
onFinish == null ? void 0 : onFinish(prefixMap);
|
759
785
|
return {
|
@@ -764,8 +790,8 @@ async function parseComplexResponse({
|
|
764
790
|
};
|
765
791
|
}
|
766
792
|
|
767
|
-
// shared/call-api.ts
|
768
|
-
async function
|
793
|
+
// shared/call-chat-api.ts
|
794
|
+
async function callChatApi({
|
769
795
|
api,
|
770
796
|
messages,
|
771
797
|
body,
|
@@ -926,7 +952,7 @@ var getStreamedResponse = async (api, chatRequest, mutate, mutateStreamData, exi
|
|
926
952
|
function_call
|
927
953
|
}
|
928
954
|
}));
|
929
|
-
return await
|
955
|
+
return await callChatApi({
|
930
956
|
api,
|
931
957
|
messages: constructedMessagesPayload,
|
932
958
|
body: {
|
@@ -1130,6 +1156,115 @@ function useChat({
|
|
1130
1156
|
|
1131
1157
|
// svelte/use-completion.ts
|
1132
1158
|
var import_store2 = require("svelte/store");
|
1159
|
+
|
1160
|
+
// shared/call-completion-api.ts
|
1161
|
+
async function callCompletionApi({
|
1162
|
+
api,
|
1163
|
+
prompt,
|
1164
|
+
credentials,
|
1165
|
+
headers,
|
1166
|
+
body,
|
1167
|
+
setCompletion,
|
1168
|
+
setLoading,
|
1169
|
+
setError,
|
1170
|
+
setAbortController,
|
1171
|
+
onResponse,
|
1172
|
+
onFinish,
|
1173
|
+
onError,
|
1174
|
+
onData
|
1175
|
+
}) {
|
1176
|
+
try {
|
1177
|
+
setLoading(true);
|
1178
|
+
setError(void 0);
|
1179
|
+
const abortController = new AbortController();
|
1180
|
+
setAbortController(abortController);
|
1181
|
+
setCompletion("");
|
1182
|
+
const res = await fetch(api, {
|
1183
|
+
method: "POST",
|
1184
|
+
body: JSON.stringify({
|
1185
|
+
prompt,
|
1186
|
+
...body
|
1187
|
+
}),
|
1188
|
+
credentials,
|
1189
|
+
headers: {
|
1190
|
+
"Content-Type": "application/json",
|
1191
|
+
...headers
|
1192
|
+
},
|
1193
|
+
signal: abortController.signal
|
1194
|
+
}).catch((err) => {
|
1195
|
+
throw err;
|
1196
|
+
});
|
1197
|
+
if (onResponse) {
|
1198
|
+
try {
|
1199
|
+
await onResponse(res);
|
1200
|
+
} catch (err) {
|
1201
|
+
throw err;
|
1202
|
+
}
|
1203
|
+
}
|
1204
|
+
if (!res.ok) {
|
1205
|
+
throw new Error(
|
1206
|
+
await res.text() || "Failed to fetch the chat response."
|
1207
|
+
);
|
1208
|
+
}
|
1209
|
+
if (!res.body) {
|
1210
|
+
throw new Error("The response body is empty.");
|
1211
|
+
}
|
1212
|
+
let result = "";
|
1213
|
+
const reader = res.body.getReader();
|
1214
|
+
const isComplexMode = res.headers.get(COMPLEX_HEADER) === "true";
|
1215
|
+
if (isComplexMode) {
|
1216
|
+
for await (const { type, value } of readDataStream(reader, {
|
1217
|
+
isAborted: () => abortController === null
|
1218
|
+
})) {
|
1219
|
+
switch (type) {
|
1220
|
+
case "text": {
|
1221
|
+
result += value;
|
1222
|
+
setCompletion(result);
|
1223
|
+
break;
|
1224
|
+
}
|
1225
|
+
case "data": {
|
1226
|
+
onData == null ? void 0 : onData(value);
|
1227
|
+
break;
|
1228
|
+
}
|
1229
|
+
}
|
1230
|
+
}
|
1231
|
+
} else {
|
1232
|
+
const decoder = createChunkDecoder();
|
1233
|
+
while (true) {
|
1234
|
+
const { done, value } = await reader.read();
|
1235
|
+
if (done) {
|
1236
|
+
break;
|
1237
|
+
}
|
1238
|
+
result += decoder(value);
|
1239
|
+
setCompletion(result);
|
1240
|
+
if (abortController === null) {
|
1241
|
+
reader.cancel();
|
1242
|
+
break;
|
1243
|
+
}
|
1244
|
+
}
|
1245
|
+
}
|
1246
|
+
if (onFinish) {
|
1247
|
+
onFinish(prompt, result);
|
1248
|
+
}
|
1249
|
+
setAbortController(null);
|
1250
|
+
return result;
|
1251
|
+
} catch (err) {
|
1252
|
+
if (err.name === "AbortError") {
|
1253
|
+
setAbortController(null);
|
1254
|
+
return null;
|
1255
|
+
}
|
1256
|
+
if (err instanceof Error) {
|
1257
|
+
if (onError) {
|
1258
|
+
onError(err);
|
1259
|
+
}
|
1260
|
+
}
|
1261
|
+
setError(err);
|
1262
|
+
} finally {
|
1263
|
+
setLoading(false);
|
1264
|
+
}
|
1265
|
+
}
|
1266
|
+
|
1267
|
+
// svelte/use-completion.ts
|
1133
1268
|
var uniqueId2 = 0;
|
1134
1269
|
var store2 = {};
|
1135
1270
|
function useCompletion({
|
@@ -1154,6 +1289,7 @@ function useCompletion({
|
|
1154
1289
|
fetcher: () => store2[key] || initialCompletion,
|
1155
1290
|
fallbackData: initialCompletion
|
1156
1291
|
});
|
1292
|
+
const streamData = (0, import_store2.writable)(void 0);
|
1157
1293
|
const loading = (0, import_store2.writable)(false);
|
1158
1294
|
data.set(initialCompletion);
|
1159
1295
|
const mutate = (data2) => {
|
@@ -1163,78 +1299,33 @@ function useCompletion({
|
|
1163
1299
|
const completion = data;
|
1164
1300
|
const error = (0, import_store2.writable)(void 0);
|
1165
1301
|
let abortController = null;
|
1166
|
-
async function triggerRequest(prompt, options) {
|
1167
|
-
try {
|
1168
|
-
error.set(void 0);
|
1169
|
-
loading.set(true);
|
1170
|
-
abortController = new AbortController();
|
1171
|
-
mutate("");
|
1172
|
-
const res = await fetch(api, {
|
1173
|
-
method: "POST",
|
1174
|
-
body: JSON.stringify({
|
1175
|
-
prompt,
|
1176
|
-
...body,
|
1177
|
-
...options == null ? void 0 : options.body
|
1178
|
-
}),
|
1179
|
-
headers: {
|
1180
|
-
...headers,
|
1181
|
-
...options == null ? void 0 : options.headers
|
1182
|
-
},
|
1183
|
-
signal: abortController.signal,
|
1184
|
-
credentials
|
1185
|
-
}).catch((err) => {
|
1186
|
-
throw err;
|
1187
|
-
});
|
1188
|
-
if (onResponse) {
|
1189
|
-
try {
|
1190
|
-
await onResponse(res);
|
1191
|
-
} catch (err) {
|
1192
|
-
throw err;
|
1193
|
-
}
|
1194
|
-
}
|
1195
|
-
if (!res.ok) {
|
1196
|
-
throw new Error(
|
1197
|
-
await res.text() || "Failed to fetch the chat response."
|
1198
|
-
);
|
1199
|
-
}
|
1200
|
-
if (!res.body) {
|
1201
|
-
throw new Error("The response body is empty.");
|
1202
|
-
}
|
1203
|
-
let result = "";
|
1204
|
-
const reader = res.body.getReader();
|
1205
|
-
const decoder = createChunkDecoder();
|
1206
|
-
while (true) {
|
1207
|
-
const { done, value } = await reader.read();
|
1208
|
-
if (done) {
|
1209
|
-
break;
|
1210
|
-
}
|
1211
|
-
result += decoder(value);
|
1212
|
-
mutate(result);
|
1213
|
-
if (abortController === null) {
|
1214
|
-
reader.cancel();
|
1215
|
-
break;
|
1216
|
-
}
|
1217
|
-
}
|
1218
|
-
if (onFinish) {
|
1219
|
-
onFinish(prompt, result);
|
1220
|
-
}
|
1221
|
-
abortController = null;
|
1222
|
-
return result;
|
1223
|
-
} catch (err) {
|
1224
|
-
if (err.name === "AbortError") {
|
1225
|
-
abortController = null;
|
1226
|
-
return null;
|
1227
|
-
}
|
1228
|
-
if (onError && error instanceof Error) {
|
1229
|
-
onError(error);
|
1230
|
-
}
|
1231
|
-
error.set(err);
|
1232
|
-
} finally {
|
1233
|
-
loading.set(false);
|
1234
|
-
}
|
1235
|
-
}
|
1236
1302
|
const complete = async (prompt, options) => {
|
1237
|
-
|
1303
|
+
const existingData = (0, import_store2.get)(streamData);
|
1304
|
+
return callCompletionApi({
|
1305
|
+
api,
|
1306
|
+
prompt,
|
1307
|
+
credentials,
|
1308
|
+
headers: {
|
1309
|
+
...headers,
|
1310
|
+
...options == null ? void 0 : options.headers
|
1311
|
+
},
|
1312
|
+
body: {
|
1313
|
+
...body,
|
1314
|
+
...options == null ? void 0 : options.body
|
1315
|
+
},
|
1316
|
+
setCompletion: mutate,
|
1317
|
+
setLoading: (loadingState) => loading.set(loadingState),
|
1318
|
+
setError: (err) => error.set(err),
|
1319
|
+
setAbortController: (controller) => {
|
1320
|
+
abortController = controller;
|
1321
|
+
},
|
1322
|
+
onResponse,
|
1323
|
+
onFinish,
|
1324
|
+
onError,
|
1325
|
+
onData(data2) {
|
1326
|
+
streamData.set([...existingData || [], ...data2 || []]);
|
1327
|
+
}
|
1328
|
+
});
|
1238
1329
|
};
|
1239
1330
|
const stop = () => {
|
1240
1331
|
if (abortController) {
|
@@ -1267,7 +1358,8 @@ function useCompletion({
|
|
1267
1358
|
setCompletion,
|
1268
1359
|
input,
|
1269
1360
|
handleSubmit,
|
1270
|
-
isLoading
|
1361
|
+
isLoading,
|
1362
|
+
data: streamData
|
1271
1363
|
};
|
1272
1364
|
}
|
1273
1365
|
// Annotate the CommonJS export names for ESM import in node:
|
@@ -1275,3 +1367,4 @@ function useCompletion({
|
|
1275
1367
|
useChat,
|
1276
1368
|
useCompletion
|
1277
1369
|
});
|
1370
|
+
//# sourceMappingURL=index.js.map
|