ai 2.2.24 → 2.2.26
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 +20 -1
- package/dist/index.js +79 -27
- package/dist/index.mjs +75 -27
- package/package.json +5 -1
- package/prompts/dist/index.d.ts +8 -1
- package/prompts/dist/index.js +17 -0
- package/prompts/dist/index.mjs +16 -0
- package/react/dist/index.d.ts +27 -9
- package/react/dist/index.js +75 -55
- package/react/dist/index.mjs +75 -55
- package/solid/dist/index.d.ts +7 -1
- package/solid/dist/index.js +14 -10
- package/solid/dist/index.mjs +14 -10
- package/svelte/dist/index.d.ts +12 -1
- package/svelte/dist/index.js +291 -94
- package/svelte/dist/index.mjs +291 -94
- package/vue/dist/index.d.ts +12 -1
- package/vue/dist/index.js +312 -85
- package/vue/dist/index.mjs +312 -85
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'.
|
@@ -201,6 +207,9 @@ type UseCompletionOptions = {
|
|
201
207
|
*/
|
202
208
|
body?: object;
|
203
209
|
};
|
210
|
+
type JSONValue = null | string | number | boolean | {
|
211
|
+
[x: string]: JSONValue;
|
212
|
+
} | Array<JSONValue>;
|
204
213
|
|
205
214
|
type UseChatHelpers = {
|
206
215
|
/** Current messages in the chat */
|
@@ -237,8 +246,10 @@ type UseChatHelpers = {
|
|
237
246
|
metadata?: Object;
|
238
247
|
/** Whether the API request is in progress */
|
239
248
|
isLoading: Readable<boolean | undefined>;
|
249
|
+
/** Additional data added on the server via StreamData */
|
250
|
+
data: Readable<JSONValue[] | undefined>;
|
240
251
|
};
|
241
|
-
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;
|
242
253
|
|
243
254
|
type UseCompletionHelpers = {
|
244
255
|
/** The current completion result */
|
package/svelte/dist/index.js
CHANGED
@@ -25,9 +25,6 @@ __export(svelte_exports, {
|
|
25
25
|
});
|
26
26
|
module.exports = __toCommonJS(svelte_exports);
|
27
27
|
|
28
|
-
// svelte/use-chat.ts
|
29
|
-
var import_store = require("svelte/store");
|
30
|
-
|
31
28
|
// ../../node_modules/.pnpm/swrev@4.0.0/node_modules/swrev/dist/swrev.mjs
|
32
29
|
var P = Object.defineProperty;
|
33
30
|
var F = (r, e, t) => e in r ? P(r, e, { enumerable: true, configurable: true, writable: true, value: t }) : r[e] = t;
|
@@ -525,6 +522,9 @@ var W = (t) => new O2(t);
|
|
525
522
|
var c = W();
|
526
523
|
var F2 = (t, e) => c.useSWR(t, e);
|
527
524
|
|
525
|
+
// svelte/use-chat.ts
|
526
|
+
var import_store = require("svelte/store");
|
527
|
+
|
528
528
|
// shared/utils.ts
|
529
529
|
var import_non_secure = require("nanoid/non-secure");
|
530
530
|
|
@@ -668,103 +668,297 @@ function createChunkDecoder(complex) {
|
|
668
668
|
return decoded.map(parseStreamPart).filter(Boolean);
|
669
669
|
};
|
670
670
|
}
|
671
|
+
var COMPLEX_HEADER = "X-Experimental-Stream-Data";
|
671
672
|
|
672
|
-
//
|
673
|
-
|
674
|
-
|
675
|
-
|
676
|
-
|
673
|
+
// shared/parse-complex-response.ts
|
674
|
+
async function parseComplexResponse({
|
675
|
+
reader,
|
676
|
+
abortControllerRef,
|
677
|
+
update,
|
678
|
+
onFinish,
|
679
|
+
generateId = nanoid,
|
680
|
+
getCurrentDate = () => /* @__PURE__ */ new Date()
|
681
|
+
}) {
|
682
|
+
const createdAt = getCurrentDate();
|
683
|
+
const decode = createChunkDecoder(true);
|
684
|
+
const prefixMap = {
|
685
|
+
data: []
|
686
|
+
};
|
687
|
+
const NEWLINE = "\n".charCodeAt(0);
|
688
|
+
const chunks = [];
|
689
|
+
let totalLength = 0;
|
690
|
+
while (true) {
|
691
|
+
const { value } = await reader.read();
|
692
|
+
if (value) {
|
693
|
+
chunks.push(value);
|
694
|
+
totalLength += value.length;
|
695
|
+
if (value[value.length - 1] !== NEWLINE) {
|
696
|
+
continue;
|
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"] = {
|
735
|
+
id: generateId(),
|
736
|
+
role: "assistant",
|
737
|
+
content: "",
|
738
|
+
function_call: value2.function_call,
|
739
|
+
name: value2.function_call.name,
|
740
|
+
createdAt
|
741
|
+
};
|
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
|
+
}
|
756
|
+
}
|
757
|
+
}
|
758
|
+
onFinish == null ? void 0 : onFinish(prefixMap);
|
759
|
+
return {
|
760
|
+
messages: [prefixMap.text, prefixMap.function_call].filter(
|
761
|
+
Boolean
|
762
|
+
),
|
763
|
+
data: prefixMap.data
|
764
|
+
};
|
765
|
+
}
|
766
|
+
|
767
|
+
// shared/call-api.ts
|
768
|
+
async function callApi({
|
769
|
+
api,
|
770
|
+
messages,
|
771
|
+
body,
|
772
|
+
credentials,
|
773
|
+
headers,
|
774
|
+
abortController,
|
775
|
+
appendMessage,
|
776
|
+
restoreMessagesOnFailure,
|
777
|
+
onResponse,
|
778
|
+
onUpdate,
|
779
|
+
onFinish,
|
780
|
+
generateId
|
781
|
+
}) {
|
782
|
+
var _a;
|
783
|
+
const response = await fetch(api, {
|
677
784
|
method: "POST",
|
678
785
|
body: JSON.stringify({
|
679
|
-
messages
|
680
|
-
|
681
|
-
role,
|
682
|
-
content,
|
683
|
-
...name !== void 0 && { name },
|
684
|
-
...function_call !== void 0 && {
|
685
|
-
function_call
|
686
|
-
}
|
687
|
-
})
|
688
|
-
),
|
689
|
-
...extraMetadata.body,
|
690
|
-
...(_a = chatRequest.options) == null ? void 0 : _a.body,
|
691
|
-
...chatRequest.functions !== void 0 && {
|
692
|
-
functions: chatRequest.functions
|
693
|
-
},
|
694
|
-
...chatRequest.function_call !== void 0 && {
|
695
|
-
function_call: chatRequest.function_call
|
696
|
-
}
|
786
|
+
messages,
|
787
|
+
...body
|
697
788
|
}),
|
698
|
-
credentials: extraMetadata.credentials,
|
699
789
|
headers: {
|
700
|
-
|
701
|
-
...
|
790
|
+
"Content-Type": "application/json",
|
791
|
+
...headers
|
702
792
|
},
|
703
|
-
|
704
|
-
|
705
|
-
}
|
793
|
+
signal: (_a = abortController == null ? void 0 : abortController()) == null ? void 0 : _a.signal,
|
794
|
+
credentials
|
706
795
|
}).catch((err) => {
|
707
|
-
|
796
|
+
restoreMessagesOnFailure();
|
708
797
|
throw err;
|
709
798
|
});
|
710
799
|
if (onResponse) {
|
711
800
|
try {
|
712
|
-
await onResponse(
|
801
|
+
await onResponse(response);
|
713
802
|
} catch (err) {
|
714
803
|
throw err;
|
715
804
|
}
|
716
805
|
}
|
717
|
-
if (!
|
718
|
-
|
719
|
-
throw new Error(
|
806
|
+
if (!response.ok) {
|
807
|
+
restoreMessagesOnFailure();
|
808
|
+
throw new Error(
|
809
|
+
await response.text() || "Failed to fetch the chat response."
|
810
|
+
);
|
720
811
|
}
|
721
|
-
if (!
|
812
|
+
if (!response.body) {
|
722
813
|
throw new Error("The response body is empty.");
|
723
814
|
}
|
724
|
-
|
725
|
-
const
|
726
|
-
|
727
|
-
|
728
|
-
|
729
|
-
|
730
|
-
|
731
|
-
|
732
|
-
|
733
|
-
|
734
|
-
|
735
|
-
|
736
|
-
|
737
|
-
|
738
|
-
|
815
|
+
const reader = response.body.getReader();
|
816
|
+
const isComplexMode = response.headers.get(COMPLEX_HEADER) === "true";
|
817
|
+
if (isComplexMode) {
|
818
|
+
return await parseComplexResponse({
|
819
|
+
reader,
|
820
|
+
abortControllerRef: abortController != null ? { current: abortController() } : void 0,
|
821
|
+
update: onUpdate,
|
822
|
+
onFinish(prefixMap) {
|
823
|
+
if (onFinish && prefixMap.text != null) {
|
824
|
+
onFinish(prefixMap.text);
|
825
|
+
}
|
826
|
+
},
|
827
|
+
generateId
|
828
|
+
});
|
829
|
+
} else {
|
830
|
+
const createdAt = /* @__PURE__ */ new Date();
|
831
|
+
const decode = createChunkDecoder(false);
|
832
|
+
let streamedResponse = "";
|
833
|
+
const replyId = generateId();
|
834
|
+
let responseMessage = {
|
835
|
+
id: replyId,
|
836
|
+
createdAt,
|
837
|
+
content: "",
|
838
|
+
role: "assistant"
|
839
|
+
};
|
840
|
+
while (true) {
|
841
|
+
const { done, value } = await reader.read();
|
842
|
+
if (done) {
|
843
|
+
break;
|
844
|
+
}
|
845
|
+
streamedResponse += decode(value);
|
846
|
+
if (streamedResponse.startsWith('{"function_call":')) {
|
847
|
+
responseMessage["function_call"] = streamedResponse;
|
848
|
+
} else {
|
849
|
+
responseMessage["content"] = streamedResponse;
|
850
|
+
}
|
851
|
+
appendMessage({ ...responseMessage });
|
852
|
+
if ((abortController == null ? void 0 : abortController()) === null) {
|
853
|
+
reader.cancel();
|
854
|
+
break;
|
855
|
+
}
|
739
856
|
}
|
740
|
-
|
741
|
-
|
742
|
-
|
743
|
-
|
744
|
-
streamedResponse
|
745
|
-
);
|
746
|
-
responseMessage.content = `${(_c = matches == null ? void 0 : matches[1]) != null ? _c : ""}${(_d = matches == null ? void 0 : matches[3]) != null ? _d : ""}`;
|
747
|
-
responseMessage.function_call = matches == null ? void 0 : matches[2];
|
748
|
-
} else {
|
749
|
-
responseMessage.content = streamedResponse;
|
857
|
+
if (streamedResponse.startsWith('{"function_call":')) {
|
858
|
+
const parsedFunctionCall = JSON.parse(streamedResponse).function_call;
|
859
|
+
responseMessage["function_call"] = parsedFunctionCall;
|
860
|
+
appendMessage({ ...responseMessage });
|
750
861
|
}
|
751
|
-
|
752
|
-
|
753
|
-
reader.cancel();
|
754
|
-
break;
|
862
|
+
if (onFinish) {
|
863
|
+
onFinish(responseMessage);
|
755
864
|
}
|
865
|
+
return responseMessage;
|
756
866
|
}
|
757
|
-
|
758
|
-
|
759
|
-
|
760
|
-
|
761
|
-
|
762
|
-
|
763
|
-
|
764
|
-
|
765
|
-
|
867
|
+
}
|
868
|
+
|
869
|
+
// shared/process-chat-stream.ts
|
870
|
+
async function processChatStream({
|
871
|
+
getStreamedResponse: getStreamedResponse2,
|
872
|
+
experimental_onFunctionCall,
|
873
|
+
updateChatRequest,
|
874
|
+
getCurrentMessages
|
875
|
+
}) {
|
876
|
+
while (true) {
|
877
|
+
const messagesAndDataOrJustMessage = await getStreamedResponse2();
|
878
|
+
if ("messages" in messagesAndDataOrJustMessage) {
|
879
|
+
let hasFollowingResponse = false;
|
880
|
+
for (const message of messagesAndDataOrJustMessage.messages) {
|
881
|
+
if (message.function_call === void 0 || typeof message.function_call === "string") {
|
882
|
+
continue;
|
883
|
+
}
|
884
|
+
hasFollowingResponse = true;
|
885
|
+
if (experimental_onFunctionCall) {
|
886
|
+
const functionCall = message.function_call;
|
887
|
+
const functionCallResponse = await experimental_onFunctionCall(
|
888
|
+
getCurrentMessages(),
|
889
|
+
functionCall
|
890
|
+
);
|
891
|
+
if (functionCallResponse === void 0) {
|
892
|
+
hasFollowingResponse = false;
|
893
|
+
break;
|
894
|
+
}
|
895
|
+
updateChatRequest(functionCallResponse);
|
896
|
+
}
|
897
|
+
}
|
898
|
+
if (!hasFollowingResponse) {
|
899
|
+
break;
|
900
|
+
}
|
901
|
+
} else {
|
902
|
+
const streamedResponseMessage = messagesAndDataOrJustMessage;
|
903
|
+
if (streamedResponseMessage.function_call === void 0 || typeof streamedResponseMessage.function_call === "string") {
|
904
|
+
break;
|
905
|
+
}
|
906
|
+
if (experimental_onFunctionCall) {
|
907
|
+
const functionCall = streamedResponseMessage.function_call;
|
908
|
+
const functionCallResponse = await experimental_onFunctionCall(getCurrentMessages(), functionCall);
|
909
|
+
if (functionCallResponse === void 0)
|
910
|
+
break;
|
911
|
+
updateChatRequest(functionCallResponse);
|
912
|
+
}
|
913
|
+
}
|
766
914
|
}
|
767
|
-
|
915
|
+
}
|
916
|
+
|
917
|
+
// svelte/use-chat.ts
|
918
|
+
var getStreamedResponse = async (api, chatRequest, mutate, mutateStreamData, existingData, extraMetadata, previousMessages, abortControllerRef, generateId, onFinish, onResponse, sendExtraMessageFields) => {
|
919
|
+
var _a, _b;
|
920
|
+
mutate(chatRequest.messages);
|
921
|
+
const constructedMessagesPayload = sendExtraMessageFields ? chatRequest.messages : chatRequest.messages.map(({ role, content, name, function_call }) => ({
|
922
|
+
role,
|
923
|
+
content,
|
924
|
+
...name !== void 0 && { name },
|
925
|
+
...function_call !== void 0 && {
|
926
|
+
function_call
|
927
|
+
}
|
928
|
+
}));
|
929
|
+
return await callApi({
|
930
|
+
api,
|
931
|
+
messages: constructedMessagesPayload,
|
932
|
+
body: {
|
933
|
+
...extraMetadata.body,
|
934
|
+
...(_a = chatRequest.options) == null ? void 0 : _a.body,
|
935
|
+
...chatRequest.functions !== void 0 && {
|
936
|
+
functions: chatRequest.functions
|
937
|
+
},
|
938
|
+
...chatRequest.function_call !== void 0 && {
|
939
|
+
function_call: chatRequest.function_call
|
940
|
+
}
|
941
|
+
},
|
942
|
+
credentials: extraMetadata.credentials,
|
943
|
+
headers: {
|
944
|
+
...extraMetadata.headers,
|
945
|
+
...(_b = chatRequest.options) == null ? void 0 : _b.headers
|
946
|
+
},
|
947
|
+
abortController: () => abortControllerRef,
|
948
|
+
appendMessage(message) {
|
949
|
+
mutate([...chatRequest.messages, message]);
|
950
|
+
},
|
951
|
+
restoreMessagesOnFailure() {
|
952
|
+
mutate(previousMessages);
|
953
|
+
},
|
954
|
+
onResponse,
|
955
|
+
onUpdate(merged, data) {
|
956
|
+
mutate([...chatRequest.messages, ...merged]);
|
957
|
+
mutateStreamData([...existingData || [], ...data || []]);
|
958
|
+
},
|
959
|
+
onFinish,
|
960
|
+
generateId
|
961
|
+
});
|
768
962
|
};
|
769
963
|
var uniqueId = 0;
|
770
964
|
var store = {};
|
@@ -780,7 +974,8 @@ function useChat({
|
|
780
974
|
onError,
|
781
975
|
credentials,
|
782
976
|
headers,
|
783
|
-
body
|
977
|
+
body,
|
978
|
+
generateId = nanoid
|
784
979
|
} = {}) {
|
785
980
|
const chatId = id || `chat-${uniqueId++}`;
|
786
981
|
const key = `${api}|${chatId}`;
|
@@ -792,6 +987,7 @@ function useChat({
|
|
792
987
|
fetcher: () => store[key] || initialMessages,
|
793
988
|
fallbackData: initialMessages
|
794
989
|
});
|
990
|
+
const streamData = (0, import_store.writable)(void 0);
|
795
991
|
const loading = (0, import_store.writable)(false);
|
796
992
|
data.set(initialMessages);
|
797
993
|
const mutate = (data2) => {
|
@@ -811,29 +1007,29 @@ function useChat({
|
|
811
1007
|
error.set(void 0);
|
812
1008
|
loading.set(true);
|
813
1009
|
abortController = new AbortController();
|
814
|
-
|
815
|
-
|
1010
|
+
await processChatStream({
|
1011
|
+
getStreamedResponse: () => getStreamedResponse(
|
816
1012
|
api,
|
817
1013
|
chatRequest,
|
818
1014
|
mutate,
|
1015
|
+
(data2) => {
|
1016
|
+
streamData.set(data2);
|
1017
|
+
},
|
1018
|
+
(0, import_store.get)(streamData),
|
819
1019
|
extraMetadata,
|
820
1020
|
(0, import_store.get)(messages),
|
821
1021
|
abortController,
|
1022
|
+
generateId,
|
822
1023
|
onFinish,
|
823
1024
|
onResponse,
|
824
1025
|
sendExtraMessageFields
|
825
|
-
)
|
826
|
-
|
827
|
-
|
828
|
-
|
829
|
-
|
830
|
-
|
831
|
-
|
832
|
-
if (functionCallResponse === void 0)
|
833
|
-
break;
|
834
|
-
chatRequest = functionCallResponse;
|
835
|
-
}
|
836
|
-
}
|
1026
|
+
),
|
1027
|
+
experimental_onFunctionCall,
|
1028
|
+
updateChatRequest: (chatRequestParam) => {
|
1029
|
+
chatRequest = chatRequestParam;
|
1030
|
+
},
|
1031
|
+
getCurrentMessages: () => (0, import_store.get)(messages)
|
1032
|
+
});
|
837
1033
|
abortController = null;
|
838
1034
|
return null;
|
839
1035
|
} catch (err) {
|
@@ -851,7 +1047,7 @@ function useChat({
|
|
851
1047
|
}
|
852
1048
|
const append = async (message, { options, functions, function_call } = {}) => {
|
853
1049
|
if (!message.id) {
|
854
|
-
message.id =
|
1050
|
+
message.id = generateId();
|
855
1051
|
}
|
856
1052
|
const chatRequest = {
|
857
1053
|
messages: (0, import_store.get)(messages).concat(message),
|
@@ -927,7 +1123,8 @@ function useChat({
|
|
927
1123
|
setMessages,
|
928
1124
|
input,
|
929
1125
|
handleSubmit,
|
930
|
-
isLoading
|
1126
|
+
isLoading,
|
1127
|
+
data: streamData
|
931
1128
|
};
|
932
1129
|
}
|
933
1130
|
|