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.mjs
CHANGED
@@ -498,9 +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/utils.ts
|
502
|
-
import { customAlphabet } from "nanoid/non-secure";
|
503
|
-
|
504
501
|
// shared/stream-parts.ts
|
505
502
|
var textStreamPart = {
|
506
503
|
code: "0",
|
@@ -547,7 +544,7 @@ var errorStreamPart = {
|
|
547
544
|
return { type: "error", value };
|
548
545
|
}
|
549
546
|
};
|
550
|
-
var
|
547
|
+
var assistantMessageStreamPart = {
|
551
548
|
code: "4",
|
552
549
|
name: "assistant_message",
|
553
550
|
parse: (value) => {
|
@@ -564,7 +561,7 @@ var assistantMessage = {
|
|
564
561
|
};
|
565
562
|
}
|
566
563
|
};
|
567
|
-
var
|
564
|
+
var assistantControlDataStreamPart = {
|
568
565
|
code: "5",
|
569
566
|
name: "assistant_control_data",
|
570
567
|
parse: (value) => {
|
@@ -582,29 +579,47 @@ var assistantControlData = {
|
|
582
579
|
};
|
583
580
|
}
|
584
581
|
};
|
582
|
+
var dataMessageStreamPart = {
|
583
|
+
code: "6",
|
584
|
+
name: "data_message",
|
585
|
+
parse: (value) => {
|
586
|
+
if (value == null || typeof value !== "object" || !("role" in value) || !("data" in value) || typeof value.role !== "string" || value.role !== "data") {
|
587
|
+
throw new Error(
|
588
|
+
'"data_message" parts expect an object with a "role" and "data" property.'
|
589
|
+
);
|
590
|
+
}
|
591
|
+
return {
|
592
|
+
type: "data_message",
|
593
|
+
value
|
594
|
+
};
|
595
|
+
}
|
596
|
+
};
|
585
597
|
var streamParts = [
|
586
598
|
textStreamPart,
|
587
599
|
functionCallStreamPart,
|
588
600
|
dataStreamPart,
|
589
601
|
errorStreamPart,
|
590
|
-
|
591
|
-
|
602
|
+
assistantMessageStreamPart,
|
603
|
+
assistantControlDataStreamPart,
|
604
|
+
dataMessageStreamPart
|
592
605
|
];
|
593
606
|
var streamPartsByCode = {
|
594
607
|
[textStreamPart.code]: textStreamPart,
|
595
608
|
[functionCallStreamPart.code]: functionCallStreamPart,
|
596
609
|
[dataStreamPart.code]: dataStreamPart,
|
597
610
|
[errorStreamPart.code]: errorStreamPart,
|
598
|
-
[
|
599
|
-
[
|
611
|
+
[assistantMessageStreamPart.code]: assistantMessageStreamPart,
|
612
|
+
[assistantControlDataStreamPart.code]: assistantControlDataStreamPart,
|
613
|
+
[dataMessageStreamPart.code]: dataMessageStreamPart
|
600
614
|
};
|
601
615
|
var StreamStringPrefixes = {
|
602
616
|
[textStreamPart.name]: textStreamPart.code,
|
603
617
|
[functionCallStreamPart.name]: functionCallStreamPart.code,
|
604
618
|
[dataStreamPart.name]: dataStreamPart.code,
|
605
619
|
[errorStreamPart.name]: errorStreamPart.code,
|
606
|
-
[
|
607
|
-
[
|
620
|
+
[assistantMessageStreamPart.name]: assistantMessageStreamPart.code,
|
621
|
+
[assistantControlDataStreamPart.name]: assistantControlDataStreamPart.code,
|
622
|
+
[dataMessageStreamPart.name]: dataMessageStreamPart.code
|
608
623
|
};
|
609
624
|
var validCodes = streamParts.map((part) => part.code);
|
610
625
|
var parseStreamPart = (line) => {
|
@@ -622,7 +637,51 @@ var parseStreamPart = (line) => {
|
|
622
637
|
return streamPartsByCode[code].parse(jsonValue);
|
623
638
|
};
|
624
639
|
|
640
|
+
// shared/read-data-stream.ts
|
641
|
+
var NEWLINE = "\n".charCodeAt(0);
|
642
|
+
function concatChunks(chunks, totalLength) {
|
643
|
+
const concatenatedChunks = new Uint8Array(totalLength);
|
644
|
+
let offset = 0;
|
645
|
+
for (const chunk of chunks) {
|
646
|
+
concatenatedChunks.set(chunk, offset);
|
647
|
+
offset += chunk.length;
|
648
|
+
}
|
649
|
+
chunks.length = 0;
|
650
|
+
return concatenatedChunks;
|
651
|
+
}
|
652
|
+
async function* readDataStream(reader, {
|
653
|
+
isAborted
|
654
|
+
} = {}) {
|
655
|
+
const decoder = new TextDecoder();
|
656
|
+
const chunks = [];
|
657
|
+
let totalLength = 0;
|
658
|
+
while (true) {
|
659
|
+
const { value } = await reader.read();
|
660
|
+
if (value) {
|
661
|
+
chunks.push(value);
|
662
|
+
totalLength += value.length;
|
663
|
+
if (value[value.length - 1] !== NEWLINE) {
|
664
|
+
continue;
|
665
|
+
}
|
666
|
+
}
|
667
|
+
if (chunks.length === 0) {
|
668
|
+
break;
|
669
|
+
}
|
670
|
+
const concatenatedChunks = concatChunks(chunks, totalLength);
|
671
|
+
totalLength = 0;
|
672
|
+
const streamParts2 = decoder.decode(concatenatedChunks, { stream: true }).split("\n").filter((line) => line !== "").map(parseStreamPart);
|
673
|
+
for (const streamPart of streamParts2) {
|
674
|
+
yield streamPart;
|
675
|
+
}
|
676
|
+
if (isAborted == null ? void 0 : isAborted()) {
|
677
|
+
reader.cancel();
|
678
|
+
break;
|
679
|
+
}
|
680
|
+
}
|
681
|
+
}
|
682
|
+
|
625
683
|
// shared/utils.ts
|
684
|
+
import { customAlphabet } from "nanoid/non-secure";
|
626
685
|
var nanoid = customAlphabet(
|
627
686
|
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
|
628
687
|
7
|
@@ -653,80 +712,47 @@ async function parseComplexResponse({
|
|
653
712
|
getCurrentDate = () => /* @__PURE__ */ new Date()
|
654
713
|
}) {
|
655
714
|
const createdAt = getCurrentDate();
|
656
|
-
const decode = createChunkDecoder(true);
|
657
715
|
const prefixMap = {
|
658
716
|
data: []
|
659
717
|
};
|
660
|
-
const
|
661
|
-
|
662
|
-
|
663
|
-
|
664
|
-
|
665
|
-
|
666
|
-
|
667
|
-
|
668
|
-
|
669
|
-
|
670
|
-
|
671
|
-
}
|
672
|
-
if (chunks.length === 0) {
|
673
|
-
break;
|
674
|
-
}
|
675
|
-
let concatenatedChunks = new Uint8Array(totalLength);
|
676
|
-
let offset = 0;
|
677
|
-
for (const chunk of chunks) {
|
678
|
-
concatenatedChunks.set(chunk, offset);
|
679
|
-
offset += chunk.length;
|
680
|
-
}
|
681
|
-
chunks.length = 0;
|
682
|
-
totalLength = 0;
|
683
|
-
const lines = decode(concatenatedChunks);
|
684
|
-
if (typeof lines === "string") {
|
685
|
-
throw new Error(
|
686
|
-
"Invalid response format. Complex mode was set but the response is a string. This should never happen."
|
687
|
-
);
|
688
|
-
}
|
689
|
-
for (const { type, value: value2 } of lines) {
|
690
|
-
if (type === "text") {
|
691
|
-
if (prefixMap["text"]) {
|
692
|
-
prefixMap["text"] = {
|
693
|
-
...prefixMap["text"],
|
694
|
-
content: (prefixMap["text"].content || "") + value2
|
695
|
-
};
|
696
|
-
} else {
|
697
|
-
prefixMap["text"] = {
|
698
|
-
id: generateId(),
|
699
|
-
role: "assistant",
|
700
|
-
content: value2,
|
701
|
-
createdAt
|
702
|
-
};
|
703
|
-
}
|
704
|
-
}
|
705
|
-
let functionCallMessage = null;
|
706
|
-
if (type === "function_call") {
|
707
|
-
prefixMap["function_call"] = {
|
718
|
+
for await (const { type, value } of readDataStream(reader, {
|
719
|
+
isAborted: () => (abortControllerRef == null ? void 0 : abortControllerRef.current) === null
|
720
|
+
})) {
|
721
|
+
if (type === "text") {
|
722
|
+
if (prefixMap["text"]) {
|
723
|
+
prefixMap["text"] = {
|
724
|
+
...prefixMap["text"],
|
725
|
+
content: (prefixMap["text"].content || "") + value
|
726
|
+
};
|
727
|
+
} else {
|
728
|
+
prefixMap["text"] = {
|
708
729
|
id: generateId(),
|
709
730
|
role: "assistant",
|
710
|
-
content:
|
711
|
-
function_call: value2.function_call,
|
712
|
-
name: value2.function_call.name,
|
731
|
+
content: value,
|
713
732
|
createdAt
|
714
733
|
};
|
715
|
-
functionCallMessage = prefixMap["function_call"];
|
716
|
-
}
|
717
|
-
if (type === "data") {
|
718
|
-
prefixMap["data"].push(...value2);
|
719
|
-
}
|
720
|
-
const responseMessage = prefixMap["text"];
|
721
|
-
const merged = [functionCallMessage, responseMessage].filter(
|
722
|
-
Boolean
|
723
|
-
);
|
724
|
-
update(merged, [...prefixMap["data"]]);
|
725
|
-
if ((abortControllerRef == null ? void 0 : abortControllerRef.current) === null) {
|
726
|
-
reader.cancel();
|
727
|
-
break;
|
728
734
|
}
|
729
735
|
}
|
736
|
+
let functionCallMessage = null;
|
737
|
+
if (type === "function_call") {
|
738
|
+
prefixMap["function_call"] = {
|
739
|
+
id: generateId(),
|
740
|
+
role: "assistant",
|
741
|
+
content: "",
|
742
|
+
function_call: value.function_call,
|
743
|
+
name: value.function_call.name,
|
744
|
+
createdAt
|
745
|
+
};
|
746
|
+
functionCallMessage = prefixMap["function_call"];
|
747
|
+
}
|
748
|
+
if (type === "data") {
|
749
|
+
prefixMap["data"].push(...value);
|
750
|
+
}
|
751
|
+
const responseMessage = prefixMap["text"];
|
752
|
+
const merged = [functionCallMessage, responseMessage].filter(
|
753
|
+
Boolean
|
754
|
+
);
|
755
|
+
update(merged, [...prefixMap["data"]]);
|
730
756
|
}
|
731
757
|
onFinish == null ? void 0 : onFinish(prefixMap);
|
732
758
|
return {
|
@@ -737,8 +763,8 @@ async function parseComplexResponse({
|
|
737
763
|
};
|
738
764
|
}
|
739
765
|
|
740
|
-
// shared/call-api.ts
|
741
|
-
async function
|
766
|
+
// shared/call-chat-api.ts
|
767
|
+
async function callChatApi({
|
742
768
|
api,
|
743
769
|
messages,
|
744
770
|
body,
|
@@ -899,7 +925,7 @@ var getStreamedResponse = async (api, chatRequest, mutate, mutateStreamData, exi
|
|
899
925
|
function_call
|
900
926
|
}
|
901
927
|
}));
|
902
|
-
return await
|
928
|
+
return await callChatApi({
|
903
929
|
api,
|
904
930
|
messages: constructedMessagesPayload,
|
905
931
|
body: {
|
@@ -1103,6 +1129,115 @@ function useChat({
|
|
1103
1129
|
|
1104
1130
|
// svelte/use-completion.ts
|
1105
1131
|
import { derived as derived2, get as get2, writable as writable2 } from "svelte/store";
|
1132
|
+
|
1133
|
+
// shared/call-completion-api.ts
|
1134
|
+
async function callCompletionApi({
|
1135
|
+
api,
|
1136
|
+
prompt,
|
1137
|
+
credentials,
|
1138
|
+
headers,
|
1139
|
+
body,
|
1140
|
+
setCompletion,
|
1141
|
+
setLoading,
|
1142
|
+
setError,
|
1143
|
+
setAbortController,
|
1144
|
+
onResponse,
|
1145
|
+
onFinish,
|
1146
|
+
onError,
|
1147
|
+
onData
|
1148
|
+
}) {
|
1149
|
+
try {
|
1150
|
+
setLoading(true);
|
1151
|
+
setError(void 0);
|
1152
|
+
const abortController = new AbortController();
|
1153
|
+
setAbortController(abortController);
|
1154
|
+
setCompletion("");
|
1155
|
+
const res = await fetch(api, {
|
1156
|
+
method: "POST",
|
1157
|
+
body: JSON.stringify({
|
1158
|
+
prompt,
|
1159
|
+
...body
|
1160
|
+
}),
|
1161
|
+
credentials,
|
1162
|
+
headers: {
|
1163
|
+
"Content-Type": "application/json",
|
1164
|
+
...headers
|
1165
|
+
},
|
1166
|
+
signal: abortController.signal
|
1167
|
+
}).catch((err) => {
|
1168
|
+
throw err;
|
1169
|
+
});
|
1170
|
+
if (onResponse) {
|
1171
|
+
try {
|
1172
|
+
await onResponse(res);
|
1173
|
+
} catch (err) {
|
1174
|
+
throw err;
|
1175
|
+
}
|
1176
|
+
}
|
1177
|
+
if (!res.ok) {
|
1178
|
+
throw new Error(
|
1179
|
+
await res.text() || "Failed to fetch the chat response."
|
1180
|
+
);
|
1181
|
+
}
|
1182
|
+
if (!res.body) {
|
1183
|
+
throw new Error("The response body is empty.");
|
1184
|
+
}
|
1185
|
+
let result = "";
|
1186
|
+
const reader = res.body.getReader();
|
1187
|
+
const isComplexMode = res.headers.get(COMPLEX_HEADER) === "true";
|
1188
|
+
if (isComplexMode) {
|
1189
|
+
for await (const { type, value } of readDataStream(reader, {
|
1190
|
+
isAborted: () => abortController === null
|
1191
|
+
})) {
|
1192
|
+
switch (type) {
|
1193
|
+
case "text": {
|
1194
|
+
result += value;
|
1195
|
+
setCompletion(result);
|
1196
|
+
break;
|
1197
|
+
}
|
1198
|
+
case "data": {
|
1199
|
+
onData == null ? void 0 : onData(value);
|
1200
|
+
break;
|
1201
|
+
}
|
1202
|
+
}
|
1203
|
+
}
|
1204
|
+
} else {
|
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
|
+
setCompletion(result);
|
1213
|
+
if (abortController === null) {
|
1214
|
+
reader.cancel();
|
1215
|
+
break;
|
1216
|
+
}
|
1217
|
+
}
|
1218
|
+
}
|
1219
|
+
if (onFinish) {
|
1220
|
+
onFinish(prompt, result);
|
1221
|
+
}
|
1222
|
+
setAbortController(null);
|
1223
|
+
return result;
|
1224
|
+
} catch (err) {
|
1225
|
+
if (err.name === "AbortError") {
|
1226
|
+
setAbortController(null);
|
1227
|
+
return null;
|
1228
|
+
}
|
1229
|
+
if (err instanceof Error) {
|
1230
|
+
if (onError) {
|
1231
|
+
onError(err);
|
1232
|
+
}
|
1233
|
+
}
|
1234
|
+
setError(err);
|
1235
|
+
} finally {
|
1236
|
+
setLoading(false);
|
1237
|
+
}
|
1238
|
+
}
|
1239
|
+
|
1240
|
+
// svelte/use-completion.ts
|
1106
1241
|
var uniqueId2 = 0;
|
1107
1242
|
var store2 = {};
|
1108
1243
|
function useCompletion({
|
@@ -1127,6 +1262,7 @@ function useCompletion({
|
|
1127
1262
|
fetcher: () => store2[key] || initialCompletion,
|
1128
1263
|
fallbackData: initialCompletion
|
1129
1264
|
});
|
1265
|
+
const streamData = writable2(void 0);
|
1130
1266
|
const loading = writable2(false);
|
1131
1267
|
data.set(initialCompletion);
|
1132
1268
|
const mutate = (data2) => {
|
@@ -1136,78 +1272,33 @@ function useCompletion({
|
|
1136
1272
|
const completion = data;
|
1137
1273
|
const error = writable2(void 0);
|
1138
1274
|
let abortController = null;
|
1139
|
-
async function triggerRequest(prompt, options) {
|
1140
|
-
try {
|
1141
|
-
error.set(void 0);
|
1142
|
-
loading.set(true);
|
1143
|
-
abortController = new AbortController();
|
1144
|
-
mutate("");
|
1145
|
-
const res = await fetch(api, {
|
1146
|
-
method: "POST",
|
1147
|
-
body: JSON.stringify({
|
1148
|
-
prompt,
|
1149
|
-
...body,
|
1150
|
-
...options == null ? void 0 : options.body
|
1151
|
-
}),
|
1152
|
-
headers: {
|
1153
|
-
...headers,
|
1154
|
-
...options == null ? void 0 : options.headers
|
1155
|
-
},
|
1156
|
-
signal: abortController.signal,
|
1157
|
-
credentials
|
1158
|
-
}).catch((err) => {
|
1159
|
-
throw err;
|
1160
|
-
});
|
1161
|
-
if (onResponse) {
|
1162
|
-
try {
|
1163
|
-
await onResponse(res);
|
1164
|
-
} catch (err) {
|
1165
|
-
throw err;
|
1166
|
-
}
|
1167
|
-
}
|
1168
|
-
if (!res.ok) {
|
1169
|
-
throw new Error(
|
1170
|
-
await res.text() || "Failed to fetch the chat response."
|
1171
|
-
);
|
1172
|
-
}
|
1173
|
-
if (!res.body) {
|
1174
|
-
throw new Error("The response body is empty.");
|
1175
|
-
}
|
1176
|
-
let result = "";
|
1177
|
-
const reader = res.body.getReader();
|
1178
|
-
const decoder = createChunkDecoder();
|
1179
|
-
while (true) {
|
1180
|
-
const { done, value } = await reader.read();
|
1181
|
-
if (done) {
|
1182
|
-
break;
|
1183
|
-
}
|
1184
|
-
result += decoder(value);
|
1185
|
-
mutate(result);
|
1186
|
-
if (abortController === null) {
|
1187
|
-
reader.cancel();
|
1188
|
-
break;
|
1189
|
-
}
|
1190
|
-
}
|
1191
|
-
if (onFinish) {
|
1192
|
-
onFinish(prompt, result);
|
1193
|
-
}
|
1194
|
-
abortController = null;
|
1195
|
-
return result;
|
1196
|
-
} catch (err) {
|
1197
|
-
if (err.name === "AbortError") {
|
1198
|
-
abortController = null;
|
1199
|
-
return null;
|
1200
|
-
}
|
1201
|
-
if (onError && error instanceof Error) {
|
1202
|
-
onError(error);
|
1203
|
-
}
|
1204
|
-
error.set(err);
|
1205
|
-
} finally {
|
1206
|
-
loading.set(false);
|
1207
|
-
}
|
1208
|
-
}
|
1209
1275
|
const complete = async (prompt, options) => {
|
1210
|
-
|
1276
|
+
const existingData = get2(streamData);
|
1277
|
+
return callCompletionApi({
|
1278
|
+
api,
|
1279
|
+
prompt,
|
1280
|
+
credentials,
|
1281
|
+
headers: {
|
1282
|
+
...headers,
|
1283
|
+
...options == null ? void 0 : options.headers
|
1284
|
+
},
|
1285
|
+
body: {
|
1286
|
+
...body,
|
1287
|
+
...options == null ? void 0 : options.body
|
1288
|
+
},
|
1289
|
+
setCompletion: mutate,
|
1290
|
+
setLoading: (loadingState) => loading.set(loadingState),
|
1291
|
+
setError: (err) => error.set(err),
|
1292
|
+
setAbortController: (controller) => {
|
1293
|
+
abortController = controller;
|
1294
|
+
},
|
1295
|
+
onResponse,
|
1296
|
+
onFinish,
|
1297
|
+
onError,
|
1298
|
+
onData(data2) {
|
1299
|
+
streamData.set([...existingData || [], ...data2 || []]);
|
1300
|
+
}
|
1301
|
+
});
|
1211
1302
|
};
|
1212
1303
|
const stop = () => {
|
1213
1304
|
if (abortController) {
|
@@ -1240,10 +1331,12 @@ function useCompletion({
|
|
1240
1331
|
setCompletion,
|
1241
1332
|
input,
|
1242
1333
|
handleSubmit,
|
1243
|
-
isLoading
|
1334
|
+
isLoading,
|
1335
|
+
data: streamData
|
1244
1336
|
};
|
1245
1337
|
}
|
1246
1338
|
export {
|
1247
1339
|
useChat,
|
1248
1340
|
useCompletion
|
1249
1341
|
};
|
1342
|
+
//# sourceMappingURL=index.mjs.map
|