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/svelte/dist/index.js
CHANGED
@@ -525,12 +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/call-api.ts
|
529
|
-
var import_nanoid = require("nanoid");
|
530
|
-
|
531
|
-
// shared/utils.ts
|
532
|
-
var import_non_secure = require("nanoid/non-secure");
|
533
|
-
|
534
528
|
// shared/stream-parts.ts
|
535
529
|
var textStreamPart = {
|
536
530
|
code: "0",
|
@@ -652,7 +646,51 @@ var parseStreamPart = (line) => {
|
|
652
646
|
return streamPartsByCode[code].parse(jsonValue);
|
653
647
|
};
|
654
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
|
+
|
655
692
|
// shared/utils.ts
|
693
|
+
var import_non_secure = require("nanoid/non-secure");
|
656
694
|
var nanoid = (0, import_non_secure.customAlphabet)(
|
657
695
|
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
|
658
696
|
7
|
@@ -683,80 +721,47 @@ async function parseComplexResponse({
|
|
683
721
|
getCurrentDate = () => /* @__PURE__ */ new Date()
|
684
722
|
}) {
|
685
723
|
const createdAt = getCurrentDate();
|
686
|
-
const decode = createChunkDecoder(true);
|
687
724
|
const prefixMap = {
|
688
725
|
data: []
|
689
726
|
};
|
690
|
-
const
|
691
|
-
|
692
|
-
|
693
|
-
|
694
|
-
|
695
|
-
|
696
|
-
|
697
|
-
|
698
|
-
|
699
|
-
|
700
|
-
|
701
|
-
}
|
702
|
-
if (chunks.length === 0) {
|
703
|
-
break;
|
704
|
-
}
|
705
|
-
let concatenatedChunks = new Uint8Array(totalLength);
|
706
|
-
let offset = 0;
|
707
|
-
for (const chunk of chunks) {
|
708
|
-
concatenatedChunks.set(chunk, offset);
|
709
|
-
offset += chunk.length;
|
710
|
-
}
|
711
|
-
chunks.length = 0;
|
712
|
-
totalLength = 0;
|
713
|
-
const lines = decode(concatenatedChunks);
|
714
|
-
if (typeof lines === "string") {
|
715
|
-
throw new Error(
|
716
|
-
"Invalid response format. Complex mode was set but the response is a string. This should never happen."
|
717
|
-
);
|
718
|
-
}
|
719
|
-
for (const { type, value: value2 } of lines) {
|
720
|
-
if (type === "text") {
|
721
|
-
if (prefixMap["text"]) {
|
722
|
-
prefixMap["text"] = {
|
723
|
-
...prefixMap["text"],
|
724
|
-
content: (prefixMap["text"].content || "") + value2
|
725
|
-
};
|
726
|
-
} else {
|
727
|
-
prefixMap["text"] = {
|
728
|
-
id: generateId(),
|
729
|
-
role: "assistant",
|
730
|
-
content: value2,
|
731
|
-
createdAt
|
732
|
-
};
|
733
|
-
}
|
734
|
-
}
|
735
|
-
let functionCallMessage = null;
|
736
|
-
if (type === "function_call") {
|
737
|
-
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"] = {
|
738
738
|
id: generateId(),
|
739
739
|
role: "assistant",
|
740
|
-
content:
|
741
|
-
function_call: value2.function_call,
|
742
|
-
name: value2.function_call.name,
|
740
|
+
content: value,
|
743
741
|
createdAt
|
744
742
|
};
|
745
|
-
functionCallMessage = prefixMap["function_call"];
|
746
|
-
}
|
747
|
-
if (type === "data") {
|
748
|
-
prefixMap["data"].push(...value2);
|
749
|
-
}
|
750
|
-
const responseMessage = prefixMap["text"];
|
751
|
-
const merged = [functionCallMessage, responseMessage].filter(
|
752
|
-
Boolean
|
753
|
-
);
|
754
|
-
update(merged, [...prefixMap["data"]]);
|
755
|
-
if ((abortControllerRef == null ? void 0 : abortControllerRef.current) === null) {
|
756
|
-
reader.cancel();
|
757
|
-
break;
|
758
743
|
}
|
759
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"]]);
|
760
765
|
}
|
761
766
|
onFinish == null ? void 0 : onFinish(prefixMap);
|
762
767
|
return {
|
@@ -779,7 +784,8 @@ async function callApi({
|
|
779
784
|
restoreMessagesOnFailure,
|
780
785
|
onResponse,
|
781
786
|
onUpdate,
|
782
|
-
onFinish
|
787
|
+
onFinish,
|
788
|
+
generateId
|
783
789
|
}) {
|
784
790
|
var _a;
|
785
791
|
const response = await fetch(api, {
|
@@ -788,7 +794,10 @@ async function callApi({
|
|
788
794
|
messages,
|
789
795
|
...body
|
790
796
|
}),
|
791
|
-
headers
|
797
|
+
headers: {
|
798
|
+
"Content-Type": "application/json",
|
799
|
+
...headers
|
800
|
+
},
|
792
801
|
signal: (_a = abortController == null ? void 0 : abortController()) == null ? void 0 : _a.signal,
|
793
802
|
credentials
|
794
803
|
}).catch((err) => {
|
@@ -822,13 +831,14 @@ async function callApi({
|
|
822
831
|
if (onFinish && prefixMap.text != null) {
|
823
832
|
onFinish(prefixMap.text);
|
824
833
|
}
|
825
|
-
}
|
834
|
+
},
|
835
|
+
generateId
|
826
836
|
});
|
827
837
|
} else {
|
828
838
|
const createdAt = /* @__PURE__ */ new Date();
|
829
839
|
const decode = createChunkDecoder(false);
|
830
840
|
let streamedResponse = "";
|
831
|
-
const replyId = (
|
841
|
+
const replyId = generateId();
|
832
842
|
let responseMessage = {
|
833
843
|
id: replyId,
|
834
844
|
createdAt,
|
@@ -913,7 +923,7 @@ async function processChatStream({
|
|
913
923
|
}
|
914
924
|
|
915
925
|
// svelte/use-chat.ts
|
916
|
-
var getStreamedResponse = async (api, chatRequest, mutate, mutateStreamData, existingData, extraMetadata, previousMessages, abortControllerRef, onFinish, onResponse, sendExtraMessageFields) => {
|
926
|
+
var getStreamedResponse = async (api, chatRequest, mutate, mutateStreamData, existingData, extraMetadata, previousMessages, abortControllerRef, generateId, onFinish, onResponse, sendExtraMessageFields) => {
|
917
927
|
var _a, _b;
|
918
928
|
mutate(chatRequest.messages);
|
919
929
|
const constructedMessagesPayload = sendExtraMessageFields ? chatRequest.messages : chatRequest.messages.map(({ role, content, name, function_call }) => ({
|
@@ -954,7 +964,8 @@ var getStreamedResponse = async (api, chatRequest, mutate, mutateStreamData, exi
|
|
954
964
|
mutate([...chatRequest.messages, ...merged]);
|
955
965
|
mutateStreamData([...existingData || [], ...data || []]);
|
956
966
|
},
|
957
|
-
onFinish
|
967
|
+
onFinish,
|
968
|
+
generateId
|
958
969
|
});
|
959
970
|
};
|
960
971
|
var uniqueId = 0;
|
@@ -971,7 +982,8 @@ function useChat({
|
|
971
982
|
onError,
|
972
983
|
credentials,
|
973
984
|
headers,
|
974
|
-
body
|
985
|
+
body,
|
986
|
+
generateId = nanoid
|
975
987
|
} = {}) {
|
976
988
|
const chatId = id || `chat-${uniqueId++}`;
|
977
989
|
const key = `${api}|${chatId}`;
|
@@ -1015,6 +1027,7 @@ function useChat({
|
|
1015
1027
|
extraMetadata,
|
1016
1028
|
(0, import_store.get)(messages),
|
1017
1029
|
abortController,
|
1030
|
+
generateId,
|
1018
1031
|
onFinish,
|
1019
1032
|
onResponse,
|
1020
1033
|
sendExtraMessageFields
|
@@ -1042,7 +1055,7 @@ function useChat({
|
|
1042
1055
|
}
|
1043
1056
|
const append = async (message, { options, functions, function_call } = {}) => {
|
1044
1057
|
if (!message.id) {
|
1045
|
-
message.id =
|
1058
|
+
message.id = generateId();
|
1046
1059
|
}
|
1047
1060
|
const chatRequest = {
|
1048
1061
|
messages: (0, import_store.get)(messages).concat(message),
|
@@ -1149,6 +1162,7 @@ function useCompletion({
|
|
1149
1162
|
fetcher: () => store2[key] || initialCompletion,
|
1150
1163
|
fallbackData: initialCompletion
|
1151
1164
|
});
|
1165
|
+
const streamData = (0, import_store2.writable)(void 0);
|
1152
1166
|
const loading = (0, import_store2.writable)(false);
|
1153
1167
|
data.set(initialCompletion);
|
1154
1168
|
const mutate = (data2) => {
|
@@ -1197,17 +1211,37 @@ function useCompletion({
|
|
1197
1211
|
}
|
1198
1212
|
let result = "";
|
1199
1213
|
const reader = res.body.getReader();
|
1200
|
-
const
|
1201
|
-
|
1202
|
-
|
1203
|
-
|
1204
|
-
|
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
|
+
}
|
1205
1231
|
}
|
1206
|
-
|
1207
|
-
|
1208
|
-
|
1209
|
-
reader.
|
1210
|
-
|
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
|
+
}
|
1211
1245
|
}
|
1212
1246
|
}
|
1213
1247
|
if (onFinish) {
|
@@ -1262,7 +1296,8 @@ function useCompletion({
|
|
1262
1296
|
setCompletion,
|
1263
1297
|
input,
|
1264
1298
|
handleSubmit,
|
1265
|
-
isLoading
|
1299
|
+
isLoading,
|
1300
|
+
data: streamData
|
1266
1301
|
};
|
1267
1302
|
}
|
1268
1303
|
// Annotate the CommonJS export names for ESM import in node:
|
package/svelte/dist/index.mjs
CHANGED
@@ -498,12 +498,6 @@ var F2 = (t, e) => c.useSWR(t, e);
|
|
498
498
|
// svelte/use-chat.ts
|
499
499
|
import { derived, get, writable } from "svelte/store";
|
500
500
|
|
501
|
-
// shared/call-api.ts
|
502
|
-
import { nanoid as nanoid2 } from "nanoid";
|
503
|
-
|
504
|
-
// shared/utils.ts
|
505
|
-
import { customAlphabet } from "nanoid/non-secure";
|
506
|
-
|
507
501
|
// shared/stream-parts.ts
|
508
502
|
var textStreamPart = {
|
509
503
|
code: "0",
|
@@ -625,7 +619,51 @@ var parseStreamPart = (line) => {
|
|
625
619
|
return streamPartsByCode[code].parse(jsonValue);
|
626
620
|
};
|
627
621
|
|
622
|
+
// shared/read-data-stream.ts
|
623
|
+
var NEWLINE = "\n".charCodeAt(0);
|
624
|
+
function concatChunks(chunks, totalLength) {
|
625
|
+
const concatenatedChunks = new Uint8Array(totalLength);
|
626
|
+
let offset = 0;
|
627
|
+
for (const chunk of chunks) {
|
628
|
+
concatenatedChunks.set(chunk, offset);
|
629
|
+
offset += chunk.length;
|
630
|
+
}
|
631
|
+
chunks.length = 0;
|
632
|
+
return concatenatedChunks;
|
633
|
+
}
|
634
|
+
async function* readDataStream(reader, {
|
635
|
+
isAborted
|
636
|
+
} = {}) {
|
637
|
+
const decoder = new TextDecoder();
|
638
|
+
const chunks = [];
|
639
|
+
let totalLength = 0;
|
640
|
+
while (true) {
|
641
|
+
const { value } = await reader.read();
|
642
|
+
if (value) {
|
643
|
+
chunks.push(value);
|
644
|
+
totalLength += value.length;
|
645
|
+
if (value[value.length - 1] !== NEWLINE) {
|
646
|
+
continue;
|
647
|
+
}
|
648
|
+
}
|
649
|
+
if (chunks.length === 0) {
|
650
|
+
break;
|
651
|
+
}
|
652
|
+
const concatenatedChunks = concatChunks(chunks, totalLength);
|
653
|
+
totalLength = 0;
|
654
|
+
const streamParts2 = decoder.decode(concatenatedChunks, { stream: true }).split("\n").filter((line) => line !== "").map(parseStreamPart);
|
655
|
+
for (const streamPart of streamParts2) {
|
656
|
+
yield streamPart;
|
657
|
+
}
|
658
|
+
if (isAborted == null ? void 0 : isAborted()) {
|
659
|
+
reader.cancel();
|
660
|
+
break;
|
661
|
+
}
|
662
|
+
}
|
663
|
+
}
|
664
|
+
|
628
665
|
// shared/utils.ts
|
666
|
+
import { customAlphabet } from "nanoid/non-secure";
|
629
667
|
var nanoid = customAlphabet(
|
630
668
|
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
|
631
669
|
7
|
@@ -656,80 +694,47 @@ async function parseComplexResponse({
|
|
656
694
|
getCurrentDate = () => /* @__PURE__ */ new Date()
|
657
695
|
}) {
|
658
696
|
const createdAt = getCurrentDate();
|
659
|
-
const decode = createChunkDecoder(true);
|
660
697
|
const prefixMap = {
|
661
698
|
data: []
|
662
699
|
};
|
663
|
-
const
|
664
|
-
|
665
|
-
|
666
|
-
|
667
|
-
|
668
|
-
|
669
|
-
|
670
|
-
|
671
|
-
|
672
|
-
|
673
|
-
|
674
|
-
}
|
675
|
-
if (chunks.length === 0) {
|
676
|
-
break;
|
677
|
-
}
|
678
|
-
let concatenatedChunks = new Uint8Array(totalLength);
|
679
|
-
let offset = 0;
|
680
|
-
for (const chunk of chunks) {
|
681
|
-
concatenatedChunks.set(chunk, offset);
|
682
|
-
offset += chunk.length;
|
683
|
-
}
|
684
|
-
chunks.length = 0;
|
685
|
-
totalLength = 0;
|
686
|
-
const lines = decode(concatenatedChunks);
|
687
|
-
if (typeof lines === "string") {
|
688
|
-
throw new Error(
|
689
|
-
"Invalid response format. Complex mode was set but the response is a string. This should never happen."
|
690
|
-
);
|
691
|
-
}
|
692
|
-
for (const { type, value: value2 } of lines) {
|
693
|
-
if (type === "text") {
|
694
|
-
if (prefixMap["text"]) {
|
695
|
-
prefixMap["text"] = {
|
696
|
-
...prefixMap["text"],
|
697
|
-
content: (prefixMap["text"].content || "") + value2
|
698
|
-
};
|
699
|
-
} else {
|
700
|
-
prefixMap["text"] = {
|
701
|
-
id: generateId(),
|
702
|
-
role: "assistant",
|
703
|
-
content: value2,
|
704
|
-
createdAt
|
705
|
-
};
|
706
|
-
}
|
707
|
-
}
|
708
|
-
let functionCallMessage = null;
|
709
|
-
if (type === "function_call") {
|
710
|
-
prefixMap["function_call"] = {
|
700
|
+
for await (const { type, value } of readDataStream(reader, {
|
701
|
+
isAborted: () => (abortControllerRef == null ? void 0 : abortControllerRef.current) === null
|
702
|
+
})) {
|
703
|
+
if (type === "text") {
|
704
|
+
if (prefixMap["text"]) {
|
705
|
+
prefixMap["text"] = {
|
706
|
+
...prefixMap["text"],
|
707
|
+
content: (prefixMap["text"].content || "") + value
|
708
|
+
};
|
709
|
+
} else {
|
710
|
+
prefixMap["text"] = {
|
711
711
|
id: generateId(),
|
712
712
|
role: "assistant",
|
713
|
-
content:
|
714
|
-
function_call: value2.function_call,
|
715
|
-
name: value2.function_call.name,
|
713
|
+
content: value,
|
716
714
|
createdAt
|
717
715
|
};
|
718
|
-
functionCallMessage = prefixMap["function_call"];
|
719
|
-
}
|
720
|
-
if (type === "data") {
|
721
|
-
prefixMap["data"].push(...value2);
|
722
|
-
}
|
723
|
-
const responseMessage = prefixMap["text"];
|
724
|
-
const merged = [functionCallMessage, responseMessage].filter(
|
725
|
-
Boolean
|
726
|
-
);
|
727
|
-
update(merged, [...prefixMap["data"]]);
|
728
|
-
if ((abortControllerRef == null ? void 0 : abortControllerRef.current) === null) {
|
729
|
-
reader.cancel();
|
730
|
-
break;
|
731
716
|
}
|
732
717
|
}
|
718
|
+
let functionCallMessage = null;
|
719
|
+
if (type === "function_call") {
|
720
|
+
prefixMap["function_call"] = {
|
721
|
+
id: generateId(),
|
722
|
+
role: "assistant",
|
723
|
+
content: "",
|
724
|
+
function_call: value.function_call,
|
725
|
+
name: value.function_call.name,
|
726
|
+
createdAt
|
727
|
+
};
|
728
|
+
functionCallMessage = prefixMap["function_call"];
|
729
|
+
}
|
730
|
+
if (type === "data") {
|
731
|
+
prefixMap["data"].push(...value);
|
732
|
+
}
|
733
|
+
const responseMessage = prefixMap["text"];
|
734
|
+
const merged = [functionCallMessage, responseMessage].filter(
|
735
|
+
Boolean
|
736
|
+
);
|
737
|
+
update(merged, [...prefixMap["data"]]);
|
733
738
|
}
|
734
739
|
onFinish == null ? void 0 : onFinish(prefixMap);
|
735
740
|
return {
|
@@ -752,7 +757,8 @@ async function callApi({
|
|
752
757
|
restoreMessagesOnFailure,
|
753
758
|
onResponse,
|
754
759
|
onUpdate,
|
755
|
-
onFinish
|
760
|
+
onFinish,
|
761
|
+
generateId
|
756
762
|
}) {
|
757
763
|
var _a;
|
758
764
|
const response = await fetch(api, {
|
@@ -761,7 +767,10 @@ async function callApi({
|
|
761
767
|
messages,
|
762
768
|
...body
|
763
769
|
}),
|
764
|
-
headers
|
770
|
+
headers: {
|
771
|
+
"Content-Type": "application/json",
|
772
|
+
...headers
|
773
|
+
},
|
765
774
|
signal: (_a = abortController == null ? void 0 : abortController()) == null ? void 0 : _a.signal,
|
766
775
|
credentials
|
767
776
|
}).catch((err) => {
|
@@ -795,13 +804,14 @@ async function callApi({
|
|
795
804
|
if (onFinish && prefixMap.text != null) {
|
796
805
|
onFinish(prefixMap.text);
|
797
806
|
}
|
798
|
-
}
|
807
|
+
},
|
808
|
+
generateId
|
799
809
|
});
|
800
810
|
} else {
|
801
811
|
const createdAt = /* @__PURE__ */ new Date();
|
802
812
|
const decode = createChunkDecoder(false);
|
803
813
|
let streamedResponse = "";
|
804
|
-
const replyId =
|
814
|
+
const replyId = generateId();
|
805
815
|
let responseMessage = {
|
806
816
|
id: replyId,
|
807
817
|
createdAt,
|
@@ -886,7 +896,7 @@ async function processChatStream({
|
|
886
896
|
}
|
887
897
|
|
888
898
|
// svelte/use-chat.ts
|
889
|
-
var getStreamedResponse = async (api, chatRequest, mutate, mutateStreamData, existingData, extraMetadata, previousMessages, abortControllerRef, onFinish, onResponse, sendExtraMessageFields) => {
|
899
|
+
var getStreamedResponse = async (api, chatRequest, mutate, mutateStreamData, existingData, extraMetadata, previousMessages, abortControllerRef, generateId, onFinish, onResponse, sendExtraMessageFields) => {
|
890
900
|
var _a, _b;
|
891
901
|
mutate(chatRequest.messages);
|
892
902
|
const constructedMessagesPayload = sendExtraMessageFields ? chatRequest.messages : chatRequest.messages.map(({ role, content, name, function_call }) => ({
|
@@ -927,7 +937,8 @@ var getStreamedResponse = async (api, chatRequest, mutate, mutateStreamData, exi
|
|
927
937
|
mutate([...chatRequest.messages, ...merged]);
|
928
938
|
mutateStreamData([...existingData || [], ...data || []]);
|
929
939
|
},
|
930
|
-
onFinish
|
940
|
+
onFinish,
|
941
|
+
generateId
|
931
942
|
});
|
932
943
|
};
|
933
944
|
var uniqueId = 0;
|
@@ -944,7 +955,8 @@ function useChat({
|
|
944
955
|
onError,
|
945
956
|
credentials,
|
946
957
|
headers,
|
947
|
-
body
|
958
|
+
body,
|
959
|
+
generateId = nanoid
|
948
960
|
} = {}) {
|
949
961
|
const chatId = id || `chat-${uniqueId++}`;
|
950
962
|
const key = `${api}|${chatId}`;
|
@@ -988,6 +1000,7 @@ function useChat({
|
|
988
1000
|
extraMetadata,
|
989
1001
|
get(messages),
|
990
1002
|
abortController,
|
1003
|
+
generateId,
|
991
1004
|
onFinish,
|
992
1005
|
onResponse,
|
993
1006
|
sendExtraMessageFields
|
@@ -1015,7 +1028,7 @@ function useChat({
|
|
1015
1028
|
}
|
1016
1029
|
const append = async (message, { options, functions, function_call } = {}) => {
|
1017
1030
|
if (!message.id) {
|
1018
|
-
message.id =
|
1031
|
+
message.id = generateId();
|
1019
1032
|
}
|
1020
1033
|
const chatRequest = {
|
1021
1034
|
messages: get(messages).concat(message),
|
@@ -1122,6 +1135,7 @@ function useCompletion({
|
|
1122
1135
|
fetcher: () => store2[key] || initialCompletion,
|
1123
1136
|
fallbackData: initialCompletion
|
1124
1137
|
});
|
1138
|
+
const streamData = writable2(void 0);
|
1125
1139
|
const loading = writable2(false);
|
1126
1140
|
data.set(initialCompletion);
|
1127
1141
|
const mutate = (data2) => {
|
@@ -1170,17 +1184,37 @@ function useCompletion({
|
|
1170
1184
|
}
|
1171
1185
|
let result = "";
|
1172
1186
|
const reader = res.body.getReader();
|
1173
|
-
const
|
1174
|
-
|
1175
|
-
|
1176
|
-
|
1177
|
-
|
1187
|
+
const existingData = get2(streamData);
|
1188
|
+
const isComplexMode = res.headers.get(COMPLEX_HEADER) === "true";
|
1189
|
+
if (isComplexMode) {
|
1190
|
+
for await (const { type, value } of readDataStream(reader, {
|
1191
|
+
isAborted: () => abortController === null
|
1192
|
+
})) {
|
1193
|
+
switch (type) {
|
1194
|
+
case "text": {
|
1195
|
+
result += value;
|
1196
|
+
mutate(result);
|
1197
|
+
break;
|
1198
|
+
}
|
1199
|
+
case "data": {
|
1200
|
+
streamData.set([...existingData || [], ...value || []]);
|
1201
|
+
break;
|
1202
|
+
}
|
1203
|
+
}
|
1178
1204
|
}
|
1179
|
-
|
1180
|
-
|
1181
|
-
|
1182
|
-
reader.
|
1183
|
-
|
1205
|
+
} else {
|
1206
|
+
const decoder = createChunkDecoder();
|
1207
|
+
while (true) {
|
1208
|
+
const { done, value } = await reader.read();
|
1209
|
+
if (done) {
|
1210
|
+
break;
|
1211
|
+
}
|
1212
|
+
result += decoder(value);
|
1213
|
+
mutate(result);
|
1214
|
+
if (abortController === null) {
|
1215
|
+
reader.cancel();
|
1216
|
+
break;
|
1217
|
+
}
|
1184
1218
|
}
|
1185
1219
|
}
|
1186
1220
|
if (onFinish) {
|
@@ -1235,7 +1269,8 @@ function useCompletion({
|
|
1235
1269
|
setCompletion,
|
1236
1270
|
input,
|
1237
1271
|
handleSubmit,
|
1238
|
-
isLoading
|
1272
|
+
isLoading,
|
1273
|
+
data: streamData
|
1239
1274
|
};
|
1240
1275
|
}
|
1241
1276
|
export {
|
package/vue/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
|
*/
|
@@ -109,6 +110,11 @@ type UseChatOptions = {
|
|
109
110
|
* Callback function to be called when an error is encountered.
|
110
111
|
*/
|
111
112
|
onError?: (error: Error) => void;
|
113
|
+
/**
|
114
|
+
* A way to provide a function that is going to be used for ids for messages.
|
115
|
+
* If not provided nanoid is used by default.
|
116
|
+
*/
|
117
|
+
generateId?: IdGenerator;
|
112
118
|
/**
|
113
119
|
* The credentials mode to be used for the fetch request.
|
114
120
|
* Possible values are: 'omit', 'same-origin', 'include'.
|
@@ -195,6 +201,9 @@ type UseCompletionOptions = {
|
|
195
201
|
*/
|
196
202
|
body?: object;
|
197
203
|
};
|
204
|
+
type JSONValue = null | string | number | boolean | {
|
205
|
+
[x: string]: JSONValue;
|
206
|
+
} | Array<JSONValue>;
|
198
207
|
|
199
208
|
type UseChatHelpers = {
|
200
209
|
/** Current messages in the chat */
|
@@ -228,8 +237,10 @@ type UseChatHelpers = {
|
|
228
237
|
handleSubmit: (e: any) => void;
|
229
238
|
/** Whether the API request is in progress */
|
230
239
|
isLoading: Ref<boolean | undefined>;
|
240
|
+
/** Additional data added on the server via StreamData */
|
241
|
+
data: Ref<JSONValue[] | undefined>;
|
231
242
|
};
|
232
|
-
declare function useChat({ api, id, initialMessages, initialInput, sendExtraMessageFields, onResponse, onFinish, onError, credentials, headers, body, }?: UseChatOptions): UseChatHelpers;
|
243
|
+
declare function useChat({ api, id, initialMessages, initialInput, sendExtraMessageFields, experimental_onFunctionCall, onResponse, onFinish, onError, credentials, headers, body, generateId, }?: UseChatOptions): UseChatHelpers;
|
233
244
|
|
234
245
|
type UseCompletionHelpers = {
|
235
246
|
/** The current completion result */
|
@@ -262,6 +273,8 @@ type UseCompletionHelpers = {
|
|
262
273
|
handleSubmit: (e: any) => void;
|
263
274
|
/** Whether the API request is in progress */
|
264
275
|
isLoading: Ref<boolean | undefined>;
|
276
|
+
/** Additional data added on the server via StreamData */
|
277
|
+
data: Ref<JSONValue[] | undefined>;
|
265
278
|
};
|
266
279
|
declare function useCompletion({ api, id, initialCompletion, initialInput, credentials, headers, body, onResponse, onFinish, onError, }?: UseCompletionOptions): UseCompletionHelpers;
|
267
280
|
|