ai 3.2.37 → 3.2.39
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/README.md +1 -1
- package/dist/index.d.mts +55 -2
- package/dist/index.d.ts +55 -2
- package/dist/index.js +43 -18
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +43 -18
- package/dist/index.mjs.map +1 -1
- package/package.json +6 -6
- package/rsc/dist/index.d.ts +1 -1
- package/rsc/dist/rsc-server.d.mts +1 -1
- package/rsc/dist/rsc-server.mjs.map +1 -1
- package/svelte/dist/index.d.mts +38 -5
- package/svelte/dist/index.d.ts +38 -5
- package/svelte/dist/index.js +78 -17
- package/svelte/dist/index.js.map +1 -1
- package/svelte/dist/index.mjs +78 -17
- package/svelte/dist/index.mjs.map +1 -1
package/svelte/dist/index.d.mts
CHANGED
@@ -1,7 +1,22 @@
|
|
1
|
-
import { Message, CreateMessage, ChatRequestOptions, JSONValue,
|
2
|
-
export { CreateMessage, Message,
|
1
|
+
import { UseChatOptions as UseChatOptions$1, Message, CreateMessage, ChatRequestOptions, JSONValue, RequestOptions, UseCompletionOptions, AssistantStatus, UseAssistantOptions } from '@ai-sdk/ui-utils';
|
2
|
+
export { CreateMessage, Message, UseCompletionOptions } from '@ai-sdk/ui-utils';
|
3
3
|
import { Readable, Writable } from 'svelte/store';
|
4
4
|
|
5
|
+
type UseChatOptions = UseChatOptions$1 & {
|
6
|
+
/**
|
7
|
+
Maximal number of automatic roundtrips for tool calls.
|
8
|
+
|
9
|
+
An automatic tool call roundtrip is a call to the server with the
|
10
|
+
tool call results when all tool calls in the last assistant
|
11
|
+
message have results.
|
12
|
+
|
13
|
+
A maximum number is required to prevent infinite loops in the
|
14
|
+
case of misconfigured tools.
|
15
|
+
|
16
|
+
By default, it's set to 0, which will disable the feature.
|
17
|
+
*/
|
18
|
+
maxToolRoundtrips?: number;
|
19
|
+
};
|
5
20
|
type UseChatHelpers = {
|
6
21
|
/** Current messages in the chat */
|
7
22
|
messages: Readable<Message[]>;
|
@@ -41,11 +56,29 @@ type UseChatHelpers = {
|
|
41
56
|
isLoading: Readable<boolean | undefined>;
|
42
57
|
/** Additional data added on the server via StreamData */
|
43
58
|
data: Readable<JSONValue[] | undefined>;
|
59
|
+
/**
|
60
|
+
Maximal number of automatic roundtrips for tool calls.
|
61
|
+
|
62
|
+
An automatic tool call roundtrip is a call to the server with the
|
63
|
+
tool call results when all tool calls in the last assistant
|
64
|
+
message have results.
|
65
|
+
|
66
|
+
A maximum number is required to prevent infinite loops in the
|
67
|
+
case of misconfigured tools.
|
68
|
+
|
69
|
+
By default, it's set to 0, which will disable the feature.
|
70
|
+
*/
|
71
|
+
maxToolRoundtrips?: number;
|
44
72
|
};
|
45
73
|
/**
|
46
74
|
* @deprecated Use `useChat` from `@ai-sdk/svelte` instead.
|
47
75
|
*/
|
48
|
-
declare function useChat({ api, id, initialMessages, initialInput, sendExtraMessageFields, experimental_onFunctionCall, experimental_onToolCall, streamMode, onResponse, onFinish, onError, credentials, headers, body, generateId, fetch, keepLastMessageOnError, }?: UseChatOptions): UseChatHelpers
|
76
|
+
declare function useChat({ api, id, initialMessages, initialInput, sendExtraMessageFields, experimental_onFunctionCall, experimental_onToolCall, streamMode, streamProtocol, onResponse, onFinish, onError, onToolCall, credentials, headers, body, generateId, fetch, keepLastMessageOnError, maxToolRoundtrips, }?: UseChatOptions): UseChatHelpers & {
|
77
|
+
addToolResult: ({ toolCallId, result, }: {
|
78
|
+
toolCallId: string;
|
79
|
+
result: any;
|
80
|
+
}) => void;
|
81
|
+
};
|
49
82
|
|
50
83
|
type UseCompletionHelpers = {
|
51
84
|
/** The current completion result */
|
@@ -86,7 +119,7 @@ type UseCompletionHelpers = {
|
|
86
119
|
/**
|
87
120
|
* @deprecated Use `useCompletion` from `@ai-sdk/svelte` instead.
|
88
121
|
*/
|
89
|
-
declare function useCompletion({ api, id, initialCompletion, initialInput, credentials, headers, body, streamMode, onResponse, onFinish, onError, fetch, }?: UseCompletionOptions): UseCompletionHelpers;
|
122
|
+
declare function useCompletion({ api, id, initialCompletion, initialInput, credentials, headers, body, streamMode, streamProtocol, onResponse, onFinish, onError, fetch, }?: UseCompletionOptions): UseCompletionHelpers;
|
90
123
|
|
91
124
|
type UseAssistantHelpers = {
|
92
125
|
/**
|
@@ -140,4 +173,4 @@ type UseAssistantHelpers = {
|
|
140
173
|
*/
|
141
174
|
declare function useAssistant({ api, threadId: threadIdParam, credentials, headers, body, onError, fetch, }: UseAssistantOptions): UseAssistantHelpers;
|
142
175
|
|
143
|
-
export { UseAssistantHelpers, UseChatHelpers, UseCompletionHelpers, useAssistant, useChat, useCompletion };
|
176
|
+
export { UseAssistantHelpers, UseChatHelpers, UseChatOptions, UseCompletionHelpers, useAssistant, useChat, useCompletion };
|
package/svelte/dist/index.d.ts
CHANGED
@@ -1,7 +1,22 @@
|
|
1
|
-
import { Message, CreateMessage, ChatRequestOptions, JSONValue,
|
2
|
-
export { CreateMessage, Message,
|
1
|
+
import { UseChatOptions as UseChatOptions$1, Message, CreateMessage, ChatRequestOptions, JSONValue, RequestOptions, UseCompletionOptions, AssistantStatus, UseAssistantOptions } from '@ai-sdk/ui-utils';
|
2
|
+
export { CreateMessage, Message, UseCompletionOptions } from '@ai-sdk/ui-utils';
|
3
3
|
import { Readable, Writable } from 'svelte/store';
|
4
4
|
|
5
|
+
type UseChatOptions = UseChatOptions$1 & {
|
6
|
+
/**
|
7
|
+
Maximal number of automatic roundtrips for tool calls.
|
8
|
+
|
9
|
+
An automatic tool call roundtrip is a call to the server with the
|
10
|
+
tool call results when all tool calls in the last assistant
|
11
|
+
message have results.
|
12
|
+
|
13
|
+
A maximum number is required to prevent infinite loops in the
|
14
|
+
case of misconfigured tools.
|
15
|
+
|
16
|
+
By default, it's set to 0, which will disable the feature.
|
17
|
+
*/
|
18
|
+
maxToolRoundtrips?: number;
|
19
|
+
};
|
5
20
|
type UseChatHelpers = {
|
6
21
|
/** Current messages in the chat */
|
7
22
|
messages: Readable<Message[]>;
|
@@ -41,11 +56,29 @@ type UseChatHelpers = {
|
|
41
56
|
isLoading: Readable<boolean | undefined>;
|
42
57
|
/** Additional data added on the server via StreamData */
|
43
58
|
data: Readable<JSONValue[] | undefined>;
|
59
|
+
/**
|
60
|
+
Maximal number of automatic roundtrips for tool calls.
|
61
|
+
|
62
|
+
An automatic tool call roundtrip is a call to the server with the
|
63
|
+
tool call results when all tool calls in the last assistant
|
64
|
+
message have results.
|
65
|
+
|
66
|
+
A maximum number is required to prevent infinite loops in the
|
67
|
+
case of misconfigured tools.
|
68
|
+
|
69
|
+
By default, it's set to 0, which will disable the feature.
|
70
|
+
*/
|
71
|
+
maxToolRoundtrips?: number;
|
44
72
|
};
|
45
73
|
/**
|
46
74
|
* @deprecated Use `useChat` from `@ai-sdk/svelte` instead.
|
47
75
|
*/
|
48
|
-
declare function useChat({ api, id, initialMessages, initialInput, sendExtraMessageFields, experimental_onFunctionCall, experimental_onToolCall, streamMode, onResponse, onFinish, onError, credentials, headers, body, generateId, fetch, keepLastMessageOnError, }?: UseChatOptions): UseChatHelpers
|
76
|
+
declare function useChat({ api, id, initialMessages, initialInput, sendExtraMessageFields, experimental_onFunctionCall, experimental_onToolCall, streamMode, streamProtocol, onResponse, onFinish, onError, onToolCall, credentials, headers, body, generateId, fetch, keepLastMessageOnError, maxToolRoundtrips, }?: UseChatOptions): UseChatHelpers & {
|
77
|
+
addToolResult: ({ toolCallId, result, }: {
|
78
|
+
toolCallId: string;
|
79
|
+
result: any;
|
80
|
+
}) => void;
|
81
|
+
};
|
49
82
|
|
50
83
|
type UseCompletionHelpers = {
|
51
84
|
/** The current completion result */
|
@@ -86,7 +119,7 @@ type UseCompletionHelpers = {
|
|
86
119
|
/**
|
87
120
|
* @deprecated Use `useCompletion` from `@ai-sdk/svelte` instead.
|
88
121
|
*/
|
89
|
-
declare function useCompletion({ api, id, initialCompletion, initialInput, credentials, headers, body, streamMode, onResponse, onFinish, onError, fetch, }?: UseCompletionOptions): UseCompletionHelpers;
|
122
|
+
declare function useCompletion({ api, id, initialCompletion, initialInput, credentials, headers, body, streamMode, streamProtocol, onResponse, onFinish, onError, fetch, }?: UseCompletionOptions): UseCompletionHelpers;
|
90
123
|
|
91
124
|
type UseAssistantHelpers = {
|
92
125
|
/**
|
@@ -140,4 +173,4 @@ type UseAssistantHelpers = {
|
|
140
173
|
*/
|
141
174
|
declare function useAssistant({ api, threadId: threadIdParam, credentials, headers, body, onError, fetch, }: UseAssistantOptions): UseAssistantHelpers;
|
142
175
|
|
143
|
-
export { UseAssistantHelpers, UseChatHelpers, UseCompletionHelpers, useAssistant, useChat, useCompletion };
|
176
|
+
export { UseAssistantHelpers, UseChatHelpers, UseChatOptions, UseCompletionHelpers, useAssistant, useChat, useCompletion };
|
package/svelte/dist/index.js
CHANGED
@@ -528,7 +528,7 @@ var F2 = (t, e) => c.useSWR(t, e);
|
|
528
528
|
|
529
529
|
// svelte/use-chat.ts
|
530
530
|
var import_store = require("svelte/store");
|
531
|
-
var getStreamedResponse = async (api, chatRequest, mutate, mutateStreamData, existingData, extraMetadata, previousMessages, abortControllerRef, generateId2,
|
531
|
+
var getStreamedResponse = async (api, chatRequest, mutate, mutateStreamData, existingData, extraMetadata, previousMessages, abortControllerRef, generateId2, streamProtocol, onFinish, onResponse, onToolCall, sendExtraMessageFields, fetch2, keepLastMessageOnError) => {
|
532
532
|
mutate(chatRequest.messages);
|
533
533
|
const constructedMessagesPayload = sendExtraMessageFields ? chatRequest.messages : chatRequest.messages.map(
|
534
534
|
({
|
@@ -539,13 +539,15 @@ var getStreamedResponse = async (api, chatRequest, mutate, mutateStreamData, exi
|
|
539
539
|
annotations,
|
540
540
|
function_call,
|
541
541
|
tool_calls,
|
542
|
-
tool_call_id
|
542
|
+
tool_call_id,
|
543
|
+
toolInvocations
|
543
544
|
}) => ({
|
544
545
|
role,
|
545
546
|
content,
|
546
547
|
...name !== void 0 && { name },
|
547
548
|
...data !== void 0 && { data },
|
548
549
|
...annotations !== void 0 && { annotations },
|
550
|
+
...toolInvocations !== void 0 && { toolInvocations },
|
549
551
|
// outdated function/tool call handling (TODO deprecate):
|
550
552
|
tool_call_id,
|
551
553
|
...function_call !== void 0 && { function_call },
|
@@ -572,7 +574,7 @@ var getStreamedResponse = async (api, chatRequest, mutate, mutateStreamData, exi
|
|
572
574
|
tool_choice: chatRequest.tool_choice
|
573
575
|
}
|
574
576
|
},
|
575
|
-
|
577
|
+
streamProtocol,
|
576
578
|
credentials: extraMetadata.credentials,
|
577
579
|
headers: {
|
578
580
|
...extraMetadata.headers,
|
@@ -591,13 +593,26 @@ var getStreamedResponse = async (api, chatRequest, mutate, mutateStreamData, exi
|
|
591
593
|
},
|
592
594
|
onFinish,
|
593
595
|
generateId: generateId2,
|
594
|
-
onToolCall
|
595
|
-
// not implemented yet
|
596
|
+
onToolCall,
|
596
597
|
fetch: fetch2
|
597
598
|
});
|
598
599
|
};
|
599
600
|
var uniqueId = 0;
|
600
601
|
var store = {};
|
602
|
+
function isAssistantMessageWithCompletedToolCalls(message) {
|
603
|
+
return message.role === "assistant" && message.toolInvocations && message.toolInvocations.length > 0 && message.toolInvocations.every((toolInvocation) => "result" in toolInvocation);
|
604
|
+
}
|
605
|
+
function countTrailingAssistantMessages(messages) {
|
606
|
+
let count = 0;
|
607
|
+
for (let i = messages.length - 1; i >= 0; i--) {
|
608
|
+
if (messages[i].role === "assistant") {
|
609
|
+
count++;
|
610
|
+
} else {
|
611
|
+
break;
|
612
|
+
}
|
613
|
+
}
|
614
|
+
return count;
|
615
|
+
}
|
601
616
|
function useChat({
|
602
617
|
api = "/api/chat",
|
603
618
|
id,
|
@@ -607,16 +622,22 @@ function useChat({
|
|
607
622
|
experimental_onFunctionCall,
|
608
623
|
experimental_onToolCall,
|
609
624
|
streamMode,
|
625
|
+
streamProtocol,
|
610
626
|
onResponse,
|
611
627
|
onFinish,
|
612
628
|
onError,
|
629
|
+
onToolCall,
|
613
630
|
credentials,
|
614
631
|
headers,
|
615
632
|
body,
|
616
633
|
generateId: generateId2 = import_ui_utils.generateId,
|
617
634
|
fetch: fetch2,
|
618
|
-
keepLastMessageOnError = false
|
635
|
+
keepLastMessageOnError = false,
|
636
|
+
maxToolRoundtrips = 0
|
619
637
|
} = {}) {
|
638
|
+
if (streamMode) {
|
639
|
+
streamProtocol != null ? streamProtocol : streamProtocol = streamMode === "text" ? "text" : void 0;
|
640
|
+
}
|
620
641
|
const chatId = id || `chat-${uniqueId++}`;
|
621
642
|
const key = `${api}|${chatId}`;
|
622
643
|
const {
|
@@ -643,6 +664,8 @@ function useChat({
|
|
643
664
|
};
|
644
665
|
const error = (0, import_store.writable)(void 0);
|
645
666
|
async function triggerRequest(chatRequest) {
|
667
|
+
const messagesSnapshot = (0, import_store.get)(messages);
|
668
|
+
const messageCount = messagesSnapshot.length;
|
646
669
|
try {
|
647
670
|
error.set(void 0);
|
648
671
|
loading.set(true);
|
@@ -660,9 +683,10 @@ function useChat({
|
|
660
683
|
(0, import_store.get)(messages),
|
661
684
|
abortController,
|
662
685
|
generateId2,
|
663
|
-
|
686
|
+
streamProtocol,
|
664
687
|
onFinish,
|
665
688
|
onResponse,
|
689
|
+
onToolCall,
|
666
690
|
sendExtraMessageFields,
|
667
691
|
fetch2,
|
668
692
|
keepLastMessageOnError
|
@@ -675,7 +699,6 @@ function useChat({
|
|
675
699
|
getCurrentMessages: () => (0, import_store.get)(messages)
|
676
700
|
});
|
677
701
|
abortController = null;
|
678
|
-
return null;
|
679
702
|
} catch (err) {
|
680
703
|
if (err.name === "AbortError") {
|
681
704
|
abortController = null;
|
@@ -688,6 +711,18 @@ function useChat({
|
|
688
711
|
} finally {
|
689
712
|
loading.set(false);
|
690
713
|
}
|
714
|
+
const newMessagesSnapshot = (0, import_store.get)(messages);
|
715
|
+
const lastMessage = newMessagesSnapshot[newMessagesSnapshot.length - 1];
|
716
|
+
if (
|
717
|
+
// ensure we actually have new messages (to prevent infinite loops in case of errors):
|
718
|
+
newMessagesSnapshot.length > messageCount && // ensure there is a last message:
|
719
|
+
lastMessage != null && // check if the feature is enabled:
|
720
|
+
maxToolRoundtrips > 0 && // check that roundtrip is possible:
|
721
|
+
isAssistantMessageWithCompletedToolCalls(lastMessage) && // limit the number of automatic roundtrips:
|
722
|
+
countTrailingAssistantMessages(newMessagesSnapshot) <= maxToolRoundtrips
|
723
|
+
) {
|
724
|
+
await triggerRequest({ messages: newMessagesSnapshot });
|
725
|
+
}
|
691
726
|
}
|
692
727
|
const append = async (message, {
|
693
728
|
options,
|
@@ -756,11 +791,7 @@ function useChat({
|
|
756
791
|
options: requestOptions,
|
757
792
|
headers: requestOptions.headers,
|
758
793
|
body: requestOptions.body,
|
759
|
-
data: data2
|
760
|
-
...functions !== void 0 && { functions },
|
761
|
-
...function_call !== void 0 && { function_call },
|
762
|
-
...tools !== void 0 && { tools },
|
763
|
-
...tool_choice !== void 0 && { tool_choice }
|
794
|
+
data: data2
|
764
795
|
};
|
765
796
|
return triggerRequest(chatRequest);
|
766
797
|
};
|
@@ -781,17 +812,19 @@ function useChat({
|
|
781
812
|
var _a, _b, _c, _d, _e;
|
782
813
|
(_a = event == null ? void 0 : event.preventDefault) == null ? void 0 : _a.call(event);
|
783
814
|
const inputValue = (0, import_store.get)(input);
|
815
|
+
if (!inputValue && !options.allowEmptySubmit)
|
816
|
+
return;
|
784
817
|
const requestOptions = {
|
785
818
|
headers: (_c = options.headers) != null ? _c : (_b = options.options) == null ? void 0 : _b.headers,
|
786
819
|
body: (_e = options.body) != null ? _e : (_d = options.options) == null ? void 0 : _d.body
|
787
820
|
};
|
788
821
|
const chatRequest = {
|
789
|
-
messages: inputValue ? (0, import_store.get)(messages).concat({
|
822
|
+
messages: !inputValue && options.allowEmptySubmit ? (0, import_store.get)(messages) : (0, import_store.get)(messages).concat({
|
790
823
|
id: generateId2(),
|
791
824
|
content: inputValue,
|
792
825
|
role: "user",
|
793
826
|
createdAt: /* @__PURE__ */ new Date()
|
794
|
-
})
|
827
|
+
}),
|
795
828
|
options: requestOptions,
|
796
829
|
body: requestOptions.body,
|
797
830
|
headers: requestOptions.headers,
|
@@ -806,6 +839,29 @@ function useChat({
|
|
806
839
|
return $isSWRLoading || $loading;
|
807
840
|
}
|
808
841
|
);
|
842
|
+
const addToolResult = ({
|
843
|
+
toolCallId,
|
844
|
+
result
|
845
|
+
}) => {
|
846
|
+
var _a;
|
847
|
+
const messagesSnapshot = (_a = (0, import_store.get)(messages)) != null ? _a : [];
|
848
|
+
const updatedMessages = messagesSnapshot.map(
|
849
|
+
(message, index, arr) => (
|
850
|
+
// update the tool calls in the last assistant message:
|
851
|
+
index === arr.length - 1 && message.role === "assistant" && message.toolInvocations ? {
|
852
|
+
...message,
|
853
|
+
toolInvocations: message.toolInvocations.map(
|
854
|
+
(toolInvocation) => toolInvocation.toolCallId === toolCallId ? { ...toolInvocation, result } : toolInvocation
|
855
|
+
)
|
856
|
+
} : message
|
857
|
+
)
|
858
|
+
);
|
859
|
+
messages.set(updatedMessages);
|
860
|
+
const lastMessage = updatedMessages[updatedMessages.length - 1];
|
861
|
+
if (isAssistantMessageWithCompletedToolCalls(lastMessage)) {
|
862
|
+
triggerRequest({ messages: updatedMessages });
|
863
|
+
}
|
864
|
+
};
|
809
865
|
return {
|
810
866
|
messages,
|
811
867
|
error,
|
@@ -816,7 +872,8 @@ function useChat({
|
|
816
872
|
input,
|
817
873
|
handleSubmit,
|
818
874
|
isLoading,
|
819
|
-
data: streamData
|
875
|
+
data: streamData,
|
876
|
+
addToolResult
|
820
877
|
};
|
821
878
|
}
|
822
879
|
|
@@ -834,11 +891,15 @@ function useCompletion({
|
|
834
891
|
headers,
|
835
892
|
body,
|
836
893
|
streamMode,
|
894
|
+
streamProtocol,
|
837
895
|
onResponse,
|
838
896
|
onFinish,
|
839
897
|
onError,
|
840
898
|
fetch: fetch2
|
841
899
|
} = {}) {
|
900
|
+
if (streamMode) {
|
901
|
+
streamProtocol != null ? streamProtocol : streamProtocol = streamMode === "text" ? "text" : void 0;
|
902
|
+
}
|
842
903
|
const completionId = id || `completion-${uniqueId2++}`;
|
843
904
|
const key = `${api}|${completionId}`;
|
844
905
|
const {
|
@@ -873,7 +934,7 @@ function useCompletion({
|
|
873
934
|
...body,
|
874
935
|
...options == null ? void 0 : options.body
|
875
936
|
},
|
876
|
-
|
937
|
+
streamProtocol,
|
877
938
|
setCompletion: mutate,
|
878
939
|
setLoading: (loadingState) => loading.set(loadingState),
|
879
940
|
setError: (err) => error.set(err),
|