agents 0.0.0-eeb70e2 → 0.0.0-ef38e84
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 +255 -27
- package/dist/ai-chat-agent.d.ts +20 -17
- package/dist/ai-chat-agent.js +532 -222
- package/dist/ai-chat-agent.js.map +1 -1
- package/dist/ai-chat-v5-migration-DBHGW4Hv.js +155 -0
- package/dist/ai-chat-v5-migration-DBHGW4Hv.js.map +1 -0
- package/dist/ai-chat-v5-migration.d.ts +155 -0
- package/dist/ai-chat-v5-migration.js +3 -0
- package/dist/ai-react.d.ts +73 -85
- package/dist/ai-react.js +266 -199
- package/dist/ai-react.js.map +1 -1
- package/dist/ai-types-B3aQaFv3.js +20 -0
- package/dist/ai-types-B3aQaFv3.js.map +1 -0
- package/dist/ai-types-D5YoPrBZ.d.ts +95 -0
- package/dist/ai-types.d.ts +6 -74
- package/dist/ai-types.js +3 -1
- package/dist/cli.d.ts +8 -0
- package/dist/cli.js +27 -0
- package/dist/cli.js.map +1 -0
- package/dist/client-BfiZ3HQd.js +117 -0
- package/dist/client-BfiZ3HQd.js.map +1 -0
- package/dist/client-Bws9q38u.d.ts +5427 -0
- package/dist/client-CbWe9FBd.d.ts +104 -0
- package/dist/client-DvK0u9vD.js +898 -0
- package/dist/client-DvK0u9vD.js.map +1 -0
- package/dist/client.d.ts +12 -93
- package/dist/client.js +4 -11
- package/dist/codemode/ai.d.ts +27 -0
- package/dist/codemode/ai.js +151 -0
- package/dist/codemode/ai.js.map +1 -0
- package/dist/do-oauth-client-provider-CcTno-25.d.ts +142 -0
- package/dist/do-oauth-client-provider-D2P1lSft.js +93 -0
- package/dist/do-oauth-client-provider-D2P1lSft.js.map +1 -0
- package/dist/index-Bdzb2feZ.d.ts +572 -0
- package/dist/index-DhJCaDWd.d.ts +58 -0
- package/dist/index.d.ts +61 -402
- package/dist/index.js +7 -22
- package/dist/mcp/client.d.ts +4 -783
- package/dist/mcp/client.js +4 -9
- package/dist/mcp/do-oauth-client-provider.d.ts +2 -41
- package/dist/mcp/do-oauth-client-provider.js +3 -7
- package/dist/mcp/index.d.ts +190 -81
- package/dist/mcp/index.js +1428 -770
- package/dist/mcp/index.js.map +1 -1
- package/dist/mcp/x402.d.ts +34 -0
- package/dist/mcp/x402.js +194 -0
- package/dist/mcp/x402.js.map +1 -0
- package/dist/mcp-Dw5vDrY8.d.ts +61 -0
- package/dist/observability/index.d.ts +3 -0
- package/dist/observability/index.js +7 -0
- package/dist/react-DFNXTQzy.d.ts +113 -0
- package/dist/react.d.ts +10 -119
- package/dist/react.js +183 -110
- package/dist/react.js.map +1 -1
- package/dist/schedule.d.ts +89 -12
- package/dist/schedule.js +46 -21
- package/dist/schedule.js.map +1 -1
- package/dist/serializable-CymX8ovI.d.ts +39 -0
- package/dist/serializable.d.ts +7 -32
- package/dist/serializable.js +1 -1
- package/dist/src-C1WWpbRo.js +1247 -0
- package/dist/src-C1WWpbRo.js.map +1 -0
- package/package.json +125 -71
- package/dist/ai-types.js.map +0 -1
- package/dist/chunk-BZXOAZUX.js +0 -106
- package/dist/chunk-BZXOAZUX.js.map +0 -1
- package/dist/chunk-OYJXQRRH.js +0 -465
- package/dist/chunk-OYJXQRRH.js.map +0 -1
- package/dist/chunk-P3RZJ72N.js +0 -783
- package/dist/chunk-P3RZJ72N.js.map +0 -1
- package/dist/chunk-VCSB47AK.js +0 -116
- package/dist/chunk-VCSB47AK.js.map +0 -1
- package/dist/client.js.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/mcp/client.js.map +0 -1
- package/dist/mcp/do-oauth-client-provider.js.map +0 -1
- package/dist/serializable.js.map +0 -1
- package/src/index.ts +0 -1252
package/dist/ai-react.d.ts
CHANGED
|
@@ -1,101 +1,89 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import
|
|
5
|
-
import "
|
|
6
|
-
import "
|
|
7
|
-
import "./
|
|
8
|
-
import "
|
|
9
|
-
import "
|
|
10
|
-
import "
|
|
11
|
-
import "
|
|
12
|
-
import "@modelcontextprotocol/sdk/client/index.js";
|
|
13
|
-
import "@modelcontextprotocol/sdk/client/sse.js";
|
|
14
|
-
import "./mcp/do-oauth-client-provider.js";
|
|
15
|
-
import "@modelcontextprotocol/sdk/client/auth.js";
|
|
16
|
-
import "@modelcontextprotocol/sdk/shared/auth.js";
|
|
17
|
-
import "@modelcontextprotocol/sdk/shared/protocol.js";
|
|
18
|
-
import "./client.js";
|
|
19
|
-
import "./serializable.js";
|
|
1
|
+
import "./client-Bws9q38u.js";
|
|
2
|
+
import "./mcp-Dw5vDrY8.js";
|
|
3
|
+
import "./do-oauth-client-provider-CcTno-25.js";
|
|
4
|
+
import "./index-DhJCaDWd.js";
|
|
5
|
+
import "./ai-types-D5YoPrBZ.js";
|
|
6
|
+
import "./index-Bdzb2feZ.js";
|
|
7
|
+
import "./serializable-CymX8ovI.js";
|
|
8
|
+
import "./client-CbWe9FBd.js";
|
|
9
|
+
import { n as useAgent } from "./react-DFNXTQzy.js";
|
|
10
|
+
import { UseChatOptions, useChat } from "@ai-sdk/react";
|
|
11
|
+
import { ChatInit, UIMessage } from "ai";
|
|
20
12
|
|
|
13
|
+
//#region src/ai-react.d.ts
|
|
14
|
+
type AITool<Input = unknown, Output = unknown> = {
|
|
15
|
+
description?: string;
|
|
16
|
+
inputSchema?: unknown;
|
|
17
|
+
execute?: (input: Input) => Output | Promise<Output>;
|
|
18
|
+
};
|
|
21
19
|
type GetInitialMessagesOptions = {
|
|
22
20
|
agent: string;
|
|
23
21
|
name: string;
|
|
24
22
|
url: string;
|
|
25
23
|
};
|
|
24
|
+
type UseChatParams<M extends UIMessage = UIMessage> = ChatInit<M> &
|
|
25
|
+
UseChatOptions<M>;
|
|
26
26
|
/**
|
|
27
27
|
* Options for the useAgentChat hook
|
|
28
28
|
*/
|
|
29
|
-
type UseAgentChatOptions<
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
29
|
+
type UseAgentChatOptions<
|
|
30
|
+
State,
|
|
31
|
+
ChatMessage extends UIMessage = UIMessage
|
|
32
|
+
> = Omit<UseChatParams<ChatMessage>, "fetch"> & {
|
|
33
|
+
/** Agent connection from useAgent */
|
|
34
|
+
agent: ReturnType<typeof useAgent<State>>;
|
|
35
|
+
getInitialMessages?:
|
|
36
|
+
| undefined
|
|
37
|
+
| null
|
|
38
|
+
| ((options: GetInitialMessagesOptions) => Promise<ChatMessage[]>);
|
|
39
|
+
/** Request credentials */
|
|
40
|
+
credentials?: RequestCredentials;
|
|
41
|
+
/** Request headers */
|
|
42
|
+
headers?: HeadersInit;
|
|
43
|
+
/**
|
|
44
|
+
* @description Whether to automatically resolve tool calls that do not require human interaction.
|
|
45
|
+
* @experimental
|
|
46
|
+
*/
|
|
47
|
+
experimental_automaticToolResolution?: boolean;
|
|
48
|
+
/**
|
|
49
|
+
* @description Tools object for automatic detection of confirmation requirements.
|
|
50
|
+
* Tools without execute function will require confirmation.
|
|
51
|
+
*/
|
|
52
|
+
tools?: Record<string, AITool<unknown, unknown>>;
|
|
53
|
+
/**
|
|
54
|
+
* @description Manual override for tools requiring confirmation.
|
|
55
|
+
* If not provided, will auto-detect from tools object.
|
|
56
|
+
*/
|
|
57
|
+
toolsRequiringConfirmation?: string[];
|
|
58
|
+
/**
|
|
59
|
+
* When true (default), automatically sends the next message only after
|
|
60
|
+
* all pending confirmation-required tool calls have been resolved.
|
|
61
|
+
* @default true
|
|
62
|
+
*/
|
|
63
|
+
autoSendAfterAllConfirmationsResolved?: boolean;
|
|
64
|
+
};
|
|
40
65
|
/**
|
|
41
66
|
* React hook for building AI chat interfaces using an Agent
|
|
42
67
|
* @param options Chat options including the agent connection
|
|
43
68
|
* @returns Chat interface controls and state with added clearHistory method
|
|
44
69
|
*/
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
70
|
+
/**
|
|
71
|
+
* Automatically detects which tools require confirmation based on their configuration.
|
|
72
|
+
* Tools require confirmation if they have no execute function AND are not server-executed.
|
|
73
|
+
* @param tools - Record of tool name to tool definition
|
|
74
|
+
* @returns Array of tool names that require confirmation
|
|
75
|
+
*/
|
|
76
|
+
declare function detectToolsRequiringConfirmation(
|
|
77
|
+
tools?: Record<string, AITool<unknown, unknown>>
|
|
78
|
+
): string[];
|
|
79
|
+
declare function useAgentChat<
|
|
80
|
+
State = unknown,
|
|
81
|
+
ChatMessage extends UIMessage = UIMessage
|
|
82
|
+
>(
|
|
83
|
+
options: UseAgentChatOptions<State, ChatMessage>
|
|
84
|
+
): ReturnType<typeof useChat<ChatMessage>> & {
|
|
56
85
|
clearHistory: () => void;
|
|
57
|
-
messages: ai.UIMessage[];
|
|
58
|
-
error: undefined | Error;
|
|
59
|
-
append: (
|
|
60
|
-
message: Message | ai.CreateMessage,
|
|
61
|
-
chatRequestOptions?: ai.ChatRequestOptions
|
|
62
|
-
) => Promise<string | null | undefined>;
|
|
63
|
-
reload: (
|
|
64
|
-
chatRequestOptions?: ai.ChatRequestOptions
|
|
65
|
-
) => Promise<string | null | undefined>;
|
|
66
|
-
stop: () => void;
|
|
67
|
-
experimental_resume: () => void;
|
|
68
|
-
input: string;
|
|
69
|
-
setInput: React.Dispatch<React.SetStateAction<string>>;
|
|
70
|
-
handleInputChange: (
|
|
71
|
-
e:
|
|
72
|
-
| React.ChangeEvent<HTMLInputElement>
|
|
73
|
-
| React.ChangeEvent<HTMLTextAreaElement>
|
|
74
|
-
) => void;
|
|
75
|
-
handleSubmit: (
|
|
76
|
-
event?: {
|
|
77
|
-
preventDefault?: () => void;
|
|
78
|
-
},
|
|
79
|
-
chatRequestOptions?: ai.ChatRequestOptions
|
|
80
|
-
) => void;
|
|
81
|
-
metadata?: Object;
|
|
82
|
-
isLoading: boolean;
|
|
83
|
-
status: "submitted" | "streaming" | "ready" | "error";
|
|
84
|
-
data?: ai.JSONValue[];
|
|
85
|
-
setData: (
|
|
86
|
-
data:
|
|
87
|
-
| ai.JSONValue[]
|
|
88
|
-
| undefined
|
|
89
|
-
| ((data: ai.JSONValue[] | undefined) => ai.JSONValue[] | undefined)
|
|
90
|
-
) => void;
|
|
91
|
-
id: string;
|
|
92
|
-
addToolResult: ({
|
|
93
|
-
toolCallId,
|
|
94
|
-
result,
|
|
95
|
-
}: {
|
|
96
|
-
toolCallId: string;
|
|
97
|
-
result: any;
|
|
98
|
-
}) => void;
|
|
99
86
|
};
|
|
100
|
-
|
|
101
|
-
export { useAgentChat };
|
|
87
|
+
//#endregion
|
|
88
|
+
export { AITool, detectToolsRequiringConfirmation, useAgentChat };
|
|
89
|
+
//# sourceMappingURL=ai-react.d.ts.map
|
package/dist/ai-react.js
CHANGED
|
@@ -1,203 +1,270 @@
|
|
|
1
|
-
|
|
2
|
-
import { useChat } from "@ai-sdk/react";
|
|
3
|
-
import { use, useEffect } from "react";
|
|
1
|
+
import { t as MessageType } from "./ai-types-B3aQaFv3.js";
|
|
4
2
|
import { nanoid } from "nanoid";
|
|
5
|
-
|
|
3
|
+
import { useChat } from "@ai-sdk/react";
|
|
4
|
+
import { DefaultChatTransport, getToolName, isToolUIPart } from "ai";
|
|
5
|
+
import { use, useCallback, useEffect, useMemo, useRef } from "react";
|
|
6
|
+
|
|
7
|
+
//#region src/ai-react.tsx
|
|
8
|
+
const requestCache = /* @__PURE__ */ new Map();
|
|
9
|
+
/**
|
|
10
|
+
* React hook for building AI chat interfaces using an Agent
|
|
11
|
+
* @param options Chat options including the agent connection
|
|
12
|
+
* @returns Chat interface controls and state with added clearHistory method
|
|
13
|
+
*/
|
|
14
|
+
/**
|
|
15
|
+
* Automatically detects which tools require confirmation based on their configuration.
|
|
16
|
+
* Tools require confirmation if they have no execute function AND are not server-executed.
|
|
17
|
+
* @param tools - Record of tool name to tool definition
|
|
18
|
+
* @returns Array of tool names that require confirmation
|
|
19
|
+
*/
|
|
20
|
+
function detectToolsRequiringConfirmation(tools) {
|
|
21
|
+
if (!tools) return [];
|
|
22
|
+
return Object.entries(tools).filter(([_name, tool$1]) => !tool$1.execute).map(([name]) => name);
|
|
23
|
+
}
|
|
6
24
|
function useAgentChat(options) {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
25
|
+
const { agent, getInitialMessages, messages: optionsInitialMessages, experimental_automaticToolResolution, tools, toolsRequiringConfirmation: manualToolsRequiringConfirmation, autoSendAfterAllConfirmationsResolved = true,...rest } = options;
|
|
26
|
+
const toolsRequiringConfirmation = manualToolsRequiringConfirmation ?? detectToolsRequiringConfirmation(tools);
|
|
27
|
+
const agentUrl = new URL(`${(agent._url || agent._pkurl)?.replace("ws://", "http://").replace("wss://", "https://")}`);
|
|
28
|
+
agentUrl.searchParams.delete("_pk");
|
|
29
|
+
const agentUrlString = agentUrl.toString();
|
|
30
|
+
const initialMessagesCacheKey = `${agentUrlString}|${agent.agent ?? ""}|${agent.name ?? ""}`;
|
|
31
|
+
const agentRef = useRef(agent);
|
|
32
|
+
useEffect(() => {
|
|
33
|
+
agentRef.current = agent;
|
|
34
|
+
}, [agent]);
|
|
35
|
+
async function defaultGetInitialMessagesFetch({ url }) {
|
|
36
|
+
const getMessagesUrl = new URL(url);
|
|
37
|
+
getMessagesUrl.pathname += "/get-messages";
|
|
38
|
+
const response = await fetch(getMessagesUrl.toString(), {
|
|
39
|
+
credentials: options.credentials,
|
|
40
|
+
headers: options.headers
|
|
41
|
+
});
|
|
42
|
+
if (!response.ok) {
|
|
43
|
+
console.warn(`Failed to fetch initial messages: ${response.status} ${response.statusText}`);
|
|
44
|
+
return [];
|
|
45
|
+
}
|
|
46
|
+
const text = await response.text();
|
|
47
|
+
if (!text.trim()) return [];
|
|
48
|
+
try {
|
|
49
|
+
return JSON.parse(text);
|
|
50
|
+
} catch (error) {
|
|
51
|
+
console.warn("Failed to parse initial messages JSON:", error);
|
|
52
|
+
return [];
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
const getInitialMessagesFetch = getInitialMessages || defaultGetInitialMessagesFetch;
|
|
56
|
+
function doGetInitialMessages(getInitialMessagesOptions, cacheKey) {
|
|
57
|
+
if (requestCache.has(cacheKey)) return requestCache.get(cacheKey);
|
|
58
|
+
const promise = getInitialMessagesFetch(getInitialMessagesOptions);
|
|
59
|
+
requestCache.set(cacheKey, promise);
|
|
60
|
+
return promise;
|
|
61
|
+
}
|
|
62
|
+
const initialMessagesPromise = getInitialMessages === null ? null : doGetInitialMessages({
|
|
63
|
+
agent: agent.agent,
|
|
64
|
+
name: agent.name,
|
|
65
|
+
url: agentUrlString
|
|
66
|
+
}, initialMessagesCacheKey);
|
|
67
|
+
const initialMessages = initialMessagesPromise ? use(initialMessagesPromise) : optionsInitialMessages ?? [];
|
|
68
|
+
useEffect(() => {
|
|
69
|
+
if (!initialMessagesPromise) return;
|
|
70
|
+
requestCache.set(initialMessagesCacheKey, initialMessagesPromise);
|
|
71
|
+
return () => {
|
|
72
|
+
if (requestCache.get(initialMessagesCacheKey) === initialMessagesPromise) requestCache.delete(initialMessagesCacheKey);
|
|
73
|
+
};
|
|
74
|
+
}, [initialMessagesCacheKey, initialMessagesPromise]);
|
|
75
|
+
const aiFetch = useCallback(async (request, options$1 = {}) => {
|
|
76
|
+
const { method, keepalive, headers, body, redirect, integrity, signal, credentials, mode, referrer, referrerPolicy, window } = options$1;
|
|
77
|
+
const id = nanoid(8);
|
|
78
|
+
const abortController = new AbortController();
|
|
79
|
+
let controller;
|
|
80
|
+
const currentAgent = agentRef.current;
|
|
81
|
+
signal?.addEventListener("abort", () => {
|
|
82
|
+
currentAgent.send(JSON.stringify({
|
|
83
|
+
id,
|
|
84
|
+
type: MessageType.CF_AGENT_CHAT_REQUEST_CANCEL
|
|
85
|
+
}));
|
|
86
|
+
abortController.abort();
|
|
87
|
+
controller.close();
|
|
88
|
+
});
|
|
89
|
+
currentAgent.addEventListener("message", (event) => {
|
|
90
|
+
let data;
|
|
91
|
+
try {
|
|
92
|
+
data = JSON.parse(event.data);
|
|
93
|
+
} catch (_error) {
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
if (data.type === MessageType.CF_AGENT_USE_CHAT_RESPONSE) {
|
|
97
|
+
if (data.id === id) if (data.error) {
|
|
98
|
+
controller.error(new Error(data.body));
|
|
99
|
+
abortController.abort();
|
|
100
|
+
} else {
|
|
101
|
+
if (data.body?.trim()) controller.enqueue(new TextEncoder().encode(`data: ${data.body}\n\n`));
|
|
102
|
+
if (data.done) {
|
|
103
|
+
controller.close();
|
|
104
|
+
abortController.abort();
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
}, { signal: abortController.signal });
|
|
109
|
+
const stream = new ReadableStream({
|
|
110
|
+
start(c) {
|
|
111
|
+
controller = c;
|
|
112
|
+
},
|
|
113
|
+
cancel(reason) {
|
|
114
|
+
console.warn("[agents/ai-react] cancelling stream", id, reason || "no reason");
|
|
115
|
+
}
|
|
116
|
+
});
|
|
117
|
+
currentAgent.send(JSON.stringify({
|
|
118
|
+
id,
|
|
119
|
+
init: {
|
|
120
|
+
body,
|
|
121
|
+
credentials,
|
|
122
|
+
headers,
|
|
123
|
+
integrity,
|
|
124
|
+
keepalive,
|
|
125
|
+
method,
|
|
126
|
+
mode,
|
|
127
|
+
redirect,
|
|
128
|
+
referrer,
|
|
129
|
+
referrerPolicy,
|
|
130
|
+
window
|
|
131
|
+
},
|
|
132
|
+
type: MessageType.CF_AGENT_USE_CHAT_REQUEST,
|
|
133
|
+
url: request.toString()
|
|
134
|
+
}));
|
|
135
|
+
return new Response(stream);
|
|
136
|
+
}, []);
|
|
137
|
+
const customTransport = useMemo(() => ({
|
|
138
|
+
sendMessages: async (options$1) => {
|
|
139
|
+
return new DefaultChatTransport({
|
|
140
|
+
api: agentUrlString,
|
|
141
|
+
fetch: aiFetch
|
|
142
|
+
}).sendMessages(options$1);
|
|
143
|
+
},
|
|
144
|
+
reconnectToStream: async (options$1) => {
|
|
145
|
+
return new DefaultChatTransport({
|
|
146
|
+
api: agentUrlString,
|
|
147
|
+
fetch: aiFetch
|
|
148
|
+
}).reconnectToStream(options$1);
|
|
149
|
+
}
|
|
150
|
+
}), [agentUrlString, aiFetch]);
|
|
151
|
+
const useChatHelpers = useChat({
|
|
152
|
+
...rest,
|
|
153
|
+
messages: initialMessages,
|
|
154
|
+
transport: customTransport,
|
|
155
|
+
id: agent._pk
|
|
156
|
+
});
|
|
157
|
+
const processedToolCalls = useRef(/* @__PURE__ */ new Set());
|
|
158
|
+
const lastMessage = useChatHelpers.messages[useChatHelpers.messages.length - 1];
|
|
159
|
+
const pendingConfirmations = (() => {
|
|
160
|
+
if (!lastMessage || lastMessage.role !== "assistant") return {
|
|
161
|
+
messageId: void 0,
|
|
162
|
+
toolCallIds: /* @__PURE__ */ new Set()
|
|
163
|
+
};
|
|
164
|
+
const pendingIds = /* @__PURE__ */ new Set();
|
|
165
|
+
for (const part of lastMessage.parts ?? []) if (isToolUIPart(part) && part.state === "input-available" && toolsRequiringConfirmation.includes(getToolName(part))) pendingIds.add(part.toolCallId);
|
|
166
|
+
return {
|
|
167
|
+
messageId: lastMessage.id,
|
|
168
|
+
toolCallIds: pendingIds
|
|
169
|
+
};
|
|
170
|
+
})();
|
|
171
|
+
const pendingConfirmationsRef = useRef(pendingConfirmations);
|
|
172
|
+
pendingConfirmationsRef.current = pendingConfirmations;
|
|
173
|
+
useEffect(() => {
|
|
174
|
+
if (!experimental_automaticToolResolution) return;
|
|
175
|
+
const lastMessage$1 = useChatHelpers.messages[useChatHelpers.messages.length - 1];
|
|
176
|
+
if (!lastMessage$1 || lastMessage$1.role !== "assistant") return;
|
|
177
|
+
const toolCalls = lastMessage$1.parts.filter((part) => isToolUIPart(part) && part.state === "input-available" && !processedToolCalls.current.has(part.toolCallId));
|
|
178
|
+
if (toolCalls.length > 0) (async () => {
|
|
179
|
+
const toolCallsToResolve = toolCalls.filter((part) => isToolUIPart(part) && !toolsRequiringConfirmation.includes(getToolName(part)) && tools?.[getToolName(part)]?.execute);
|
|
180
|
+
if (toolCallsToResolve.length > 0) {
|
|
181
|
+
for (const part of toolCallsToResolve) if (isToolUIPart(part)) {
|
|
182
|
+
processedToolCalls.current.add(part.toolCallId);
|
|
183
|
+
let toolOutput = null;
|
|
184
|
+
const toolName = getToolName(part);
|
|
185
|
+
const tool$1 = tools?.[toolName];
|
|
186
|
+
if (tool$1?.execute && part.input) try {
|
|
187
|
+
toolOutput = await tool$1.execute(part.input);
|
|
188
|
+
} catch (error) {
|
|
189
|
+
toolOutput = `Error executing tool: ${error instanceof Error ? error.message : String(error)}`;
|
|
190
|
+
}
|
|
191
|
+
await useChatHelpers.addToolResult({
|
|
192
|
+
toolCallId: part.toolCallId,
|
|
193
|
+
tool: toolName,
|
|
194
|
+
output: toolOutput
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
if (pendingConfirmationsRef.current.toolCallIds.size === 0) useChatHelpers.sendMessage();
|
|
198
|
+
}
|
|
199
|
+
})();
|
|
200
|
+
}, [
|
|
201
|
+
useChatHelpers.messages,
|
|
202
|
+
experimental_automaticToolResolution,
|
|
203
|
+
useChatHelpers.addToolResult,
|
|
204
|
+
useChatHelpers.sendMessage,
|
|
205
|
+
toolsRequiringConfirmation
|
|
206
|
+
]);
|
|
207
|
+
useEffect(() => {
|
|
208
|
+
function onClearHistory(event) {
|
|
209
|
+
if (typeof event.data !== "string") return;
|
|
210
|
+
let data;
|
|
211
|
+
try {
|
|
212
|
+
data = JSON.parse(event.data);
|
|
213
|
+
} catch (_error) {
|
|
214
|
+
return;
|
|
215
|
+
}
|
|
216
|
+
if (data.type === MessageType.CF_AGENT_CHAT_CLEAR) useChatHelpers.setMessages([]);
|
|
217
|
+
}
|
|
218
|
+
function onMessages(event) {
|
|
219
|
+
if (typeof event.data !== "string") return;
|
|
220
|
+
let data;
|
|
221
|
+
try {
|
|
222
|
+
data = JSON.parse(event.data);
|
|
223
|
+
} catch (_error) {
|
|
224
|
+
return;
|
|
225
|
+
}
|
|
226
|
+
if (data.type === MessageType.CF_AGENT_CHAT_MESSAGES) useChatHelpers.setMessages(data.messages);
|
|
227
|
+
}
|
|
228
|
+
agent.addEventListener("message", onClearHistory);
|
|
229
|
+
agent.addEventListener("message", onMessages);
|
|
230
|
+
return () => {
|
|
231
|
+
agent.removeEventListener("message", onClearHistory);
|
|
232
|
+
agent.removeEventListener("message", onMessages);
|
|
233
|
+
};
|
|
234
|
+
}, [agent, useChatHelpers.setMessages]);
|
|
235
|
+
const addToolResultAndSendMessage = async (args) => {
|
|
236
|
+
const { toolCallId } = args;
|
|
237
|
+
await useChatHelpers.addToolResult(args);
|
|
238
|
+
if (!autoSendAfterAllConfirmationsResolved) {
|
|
239
|
+
useChatHelpers.sendMessage();
|
|
240
|
+
return;
|
|
241
|
+
}
|
|
242
|
+
const pending = pendingConfirmationsRef.current?.toolCallIds;
|
|
243
|
+
if (!pending) {
|
|
244
|
+
useChatHelpers.sendMessage();
|
|
245
|
+
return;
|
|
246
|
+
}
|
|
247
|
+
const wasLast = pending.size === 1 && pending.has(toolCallId);
|
|
248
|
+
if (pending.has(toolCallId)) pending.delete(toolCallId);
|
|
249
|
+
if (wasLast || pending.size === 0) useChatHelpers.sendMessage();
|
|
250
|
+
};
|
|
251
|
+
return {
|
|
252
|
+
...useChatHelpers,
|
|
253
|
+
addToolResult: addToolResultAndSendMessage,
|
|
254
|
+
clearHistory: () => {
|
|
255
|
+
useChatHelpers.setMessages([]);
|
|
256
|
+
agent.send(JSON.stringify({ type: MessageType.CF_AGENT_CHAT_CLEAR }));
|
|
257
|
+
},
|
|
258
|
+
setMessages: (messages) => {
|
|
259
|
+
useChatHelpers.setMessages(messages);
|
|
260
|
+
agent.send(JSON.stringify({
|
|
261
|
+
messages: Array.isArray(messages) ? messages : [],
|
|
262
|
+
type: MessageType.CF_AGENT_CHAT_MESSAGES
|
|
263
|
+
}));
|
|
264
|
+
}
|
|
265
|
+
};
|
|
199
266
|
}
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
};
|
|
267
|
+
|
|
268
|
+
//#endregion
|
|
269
|
+
export { detectToolsRequiringConfirmation, useAgentChat };
|
|
203
270
|
//# sourceMappingURL=ai-react.js.map
|