agents 0.2.10 → 0.2.11
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/ai-chat-agent.d.ts +15 -18
- package/dist/ai-chat-agent.js +542 -673
- package/dist/ai-chat-agent.js.map +1 -1
- package/dist/ai-chat-v5-migration-gdyLiTd8.js +155 -0
- package/dist/ai-chat-v5-migration-gdyLiTd8.js.map +1 -0
- package/dist/ai-chat-v5-migration.d.ts +7 -4
- package/dist/ai-chat-v5-migration.js +3 -20
- package/dist/ai-react.d.ts +14 -24
- package/dist/ai-react.js +257 -343
- package/dist/ai-react.js.map +1 -1
- package/dist/ai-types-B0GBFDwi.js +20 -0
- package/dist/ai-types-B0GBFDwi.js.map +1 -0
- package/dist/ai-types-BWW4umHY.d.ts +95 -0
- package/dist/ai-types.d.ts +6 -92
- package/dist/ai-types.js +3 -8
- package/dist/client-C-u-lCFT.d.ts +5311 -0
- package/dist/client-CmMi85Sj.d.ts +104 -0
- package/dist/client-WbaRgKYN.js +788 -0
- package/dist/client-WbaRgKYN.js.map +1 -0
- package/dist/client-zS-OCVJA.js +117 -0
- package/dist/client-zS-OCVJA.js.map +1 -0
- package/dist/client.d.ts +11 -92
- package/dist/client.js +4 -13
- package/dist/codemode/ai.d.ts +19 -17
- package/dist/codemode/ai.js +90 -5139
- package/dist/codemode/ai.js.map +1 -1
- package/dist/do-oauth-client-provider-B2jr6UNq.js +93 -0
- package/dist/do-oauth-client-provider-B2jr6UNq.js.map +1 -0
- package/dist/do-oauth-client-provider-CCwGwnrA.d.ts +55 -0
- package/dist/index-DWcUTPtX.d.ts +558 -0
- package/dist/index-W4JUkafc.d.ts +54 -0
- package/dist/index.d.ts +55 -542
- package/dist/index.js +7 -32
- package/dist/mcp/client.d.ts +4 -12
- package/dist/mcp/client.js +3 -10
- package/dist/mcp/do-oauth-client-provider.d.ts +2 -42
- package/dist/mcp/do-oauth-client-provider.js +3 -8
- package/dist/mcp/index.d.ts +72 -76
- package/dist/mcp/index.js +830 -1098
- package/dist/mcp/index.js.map +1 -1
- package/dist/mcp/x402.d.ts +22 -27
- package/dist/mcp/x402.js +186 -3187
- package/dist/mcp/x402.js.map +1 -1
- package/dist/{mcp-BH1fJeiU.d.ts → mcp-BEwaCsxO.d.ts} +6 -3
- package/dist/observability/index.d.ts +3 -34
- package/dist/observability/index.js +7 -12
- package/dist/react-B8BT6PYZ.d.ts +113 -0
- package/dist/react.d.ts +10 -127
- package/dist/react.js +173 -200
- package/dist/react.js.map +1 -1
- package/dist/schedule.d.ts +13 -10
- package/dist/schedule.js +43 -33
- package/dist/schedule.js.map +1 -1
- package/dist/serializable-gtr9YMhp.d.ts +34 -0
- package/dist/serializable.d.ts +7 -32
- package/dist/serializable.js +1 -1
- package/dist/src-C9xZ0CrH.js +1179 -0
- package/dist/src-C9xZ0CrH.js.map +1 -0
- package/package.json +9 -3
- package/dist/_esm-LV5FJ3HK.js +0 -3922
- package/dist/_esm-LV5FJ3HK.js.map +0 -1
- package/dist/ai-chat-v5-migration.js.map +0 -1
- package/dist/ai-types.js.map +0 -1
- package/dist/ccip-CMBYN64O.js +0 -15
- package/dist/ccip-CMBYN64O.js.map +0 -1
- package/dist/chunk-2Y6KNRNP.js +0 -1343
- package/dist/chunk-2Y6KNRNP.js.map +0 -1
- package/dist/chunk-5Y6BEZDY.js +0 -276
- package/dist/chunk-5Y6BEZDY.js.map +0 -1
- package/dist/chunk-BER7KXUJ.js +0 -18
- package/dist/chunk-BER7KXUJ.js.map +0 -1
- package/dist/chunk-C2OEBJZ2.js +0 -113
- package/dist/chunk-C2OEBJZ2.js.map +0 -1
- package/dist/chunk-JJBFIGUC.js +0 -5202
- package/dist/chunk-JJBFIGUC.js.map +0 -1
- package/dist/chunk-PR4QN5HX.js +0 -43
- package/dist/chunk-PR4QN5HX.js.map +0 -1
- package/dist/chunk-QEVM4BVL.js +0 -116
- package/dist/chunk-QEVM4BVL.js.map +0 -1
- package/dist/chunk-QUOGV3WN.js +0 -949
- package/dist/chunk-QUOGV3WN.js.map +0 -1
- package/dist/chunk-TYAY6AU6.js +0 -159
- package/dist/chunk-TYAY6AU6.js.map +0 -1
- package/dist/chunk-UJVEAURM.js +0 -150
- package/dist/chunk-UJVEAURM.js.map +0 -1
- package/dist/client-C8tswVoM.d.ts +0 -5248
- 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/observability/index.js.map +0 -1
- package/dist/secp256k1-M22GZP2U.js +0 -2193
- package/dist/secp256k1-M22GZP2U.js.map +0 -1
- package/dist/serializable.js.map +0 -1
package/dist/ai-react.js
CHANGED
|
@@ -1,350 +1,264 @@
|
|
|
1
|
-
import "./
|
|
2
|
-
import "./chunk-PR4QN5HX.js";
|
|
3
|
-
|
|
4
|
-
// src/ai-react.tsx
|
|
5
|
-
import { useChat } from "@ai-sdk/react";
|
|
6
|
-
import { getToolName, isToolUIPart } from "ai";
|
|
7
|
-
import { DefaultChatTransport } from "ai";
|
|
1
|
+
import { MessageType } from "./ai-types-B0GBFDwi.js";
|
|
8
2
|
import { nanoid } from "nanoid";
|
|
3
|
+
import { DefaultChatTransport, getToolName, isToolUIPart } from "ai";
|
|
4
|
+
import { useChat } from "@ai-sdk/react";
|
|
9
5
|
import { use, useCallback, useEffect, useMemo, useRef } from "react";
|
|
10
|
-
|
|
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
|
+
*/
|
|
11
20
|
function detectToolsRequiringConfirmation(tools) {
|
|
12
|
-
|
|
13
|
-
|
|
21
|
+
if (!tools) return [];
|
|
22
|
+
return Object.entries(tools).filter(([_name, tool$1]) => !tool$1.execute).map(([name]) => name);
|
|
14
23
|
}
|
|
15
24
|
function useAgentChat(options) {
|
|
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
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
}
|
|
252
|
-
await useChatHelpers.addToolResult({
|
|
253
|
-
toolCallId: part.toolCallId,
|
|
254
|
-
tool: toolName,
|
|
255
|
-
output: toolOutput
|
|
256
|
-
});
|
|
257
|
-
}
|
|
258
|
-
}
|
|
259
|
-
if (pendingConfirmationsRef.current.toolCallIds.size === 0) {
|
|
260
|
-
useChatHelpers.sendMessage();
|
|
261
|
-
}
|
|
262
|
-
}
|
|
263
|
-
})();
|
|
264
|
-
}
|
|
265
|
-
}, [
|
|
266
|
-
useChatHelpers.messages,
|
|
267
|
-
experimental_automaticToolResolution,
|
|
268
|
-
useChatHelpers.addToolResult,
|
|
269
|
-
useChatHelpers.sendMessage,
|
|
270
|
-
toolsRequiringConfirmation
|
|
271
|
-
]);
|
|
272
|
-
useEffect(() => {
|
|
273
|
-
function onClearHistory(event) {
|
|
274
|
-
if (typeof event.data !== "string") return;
|
|
275
|
-
let data;
|
|
276
|
-
try {
|
|
277
|
-
data = JSON.parse(event.data);
|
|
278
|
-
} catch (_error) {
|
|
279
|
-
return;
|
|
280
|
-
}
|
|
281
|
-
if (data.type === "cf_agent_chat_clear" /* CF_AGENT_CHAT_CLEAR */) {
|
|
282
|
-
useChatHelpers.setMessages([]);
|
|
283
|
-
}
|
|
284
|
-
}
|
|
285
|
-
function onMessages(event) {
|
|
286
|
-
if (typeof event.data !== "string") return;
|
|
287
|
-
let data;
|
|
288
|
-
try {
|
|
289
|
-
data = JSON.parse(event.data);
|
|
290
|
-
} catch (_error) {
|
|
291
|
-
return;
|
|
292
|
-
}
|
|
293
|
-
if (data.type === "cf_agent_chat_messages" /* CF_AGENT_CHAT_MESSAGES */) {
|
|
294
|
-
useChatHelpers.setMessages(data.messages);
|
|
295
|
-
}
|
|
296
|
-
}
|
|
297
|
-
agent.addEventListener("message", onClearHistory);
|
|
298
|
-
agent.addEventListener("message", onMessages);
|
|
299
|
-
return () => {
|
|
300
|
-
agent.removeEventListener("message", onClearHistory);
|
|
301
|
-
agent.removeEventListener("message", onMessages);
|
|
302
|
-
};
|
|
303
|
-
}, [agent, useChatHelpers.setMessages]);
|
|
304
|
-
const addToolResultAndSendMessage = async (args) => {
|
|
305
|
-
const { toolCallId } = args;
|
|
306
|
-
await useChatHelpers.addToolResult(args);
|
|
307
|
-
if (!autoSendAfterAllConfirmationsResolved) {
|
|
308
|
-
useChatHelpers.sendMessage();
|
|
309
|
-
return;
|
|
310
|
-
}
|
|
311
|
-
const pending = pendingConfirmationsRef.current?.toolCallIds;
|
|
312
|
-
if (!pending) {
|
|
313
|
-
useChatHelpers.sendMessage();
|
|
314
|
-
return;
|
|
315
|
-
}
|
|
316
|
-
const wasLast = pending.size === 1 && pending.has(toolCallId);
|
|
317
|
-
if (pending.has(toolCallId)) {
|
|
318
|
-
pending.delete(toolCallId);
|
|
319
|
-
}
|
|
320
|
-
if (wasLast || pending.size === 0) {
|
|
321
|
-
useChatHelpers.sendMessage();
|
|
322
|
-
}
|
|
323
|
-
};
|
|
324
|
-
return {
|
|
325
|
-
...useChatHelpers,
|
|
326
|
-
addToolResult: addToolResultAndSendMessage,
|
|
327
|
-
clearHistory: () => {
|
|
328
|
-
useChatHelpers.setMessages([]);
|
|
329
|
-
agent.send(
|
|
330
|
-
JSON.stringify({
|
|
331
|
-
type: "cf_agent_chat_clear" /* CF_AGENT_CHAT_CLEAR */
|
|
332
|
-
})
|
|
333
|
-
);
|
|
334
|
-
},
|
|
335
|
-
setMessages: (messages) => {
|
|
336
|
-
useChatHelpers.setMessages(messages);
|
|
337
|
-
agent.send(
|
|
338
|
-
JSON.stringify({
|
|
339
|
-
messages: Array.isArray(messages) ? messages : [],
|
|
340
|
-
type: "cf_agent_chat_messages" /* CF_AGENT_CHAT_MESSAGES */
|
|
341
|
-
})
|
|
342
|
-
);
|
|
343
|
-
}
|
|
344
|
-
};
|
|
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 agentRef = useRef(agent);
|
|
31
|
+
useEffect(() => {
|
|
32
|
+
agentRef.current = agent;
|
|
33
|
+
}, [agent]);
|
|
34
|
+
async function defaultGetInitialMessagesFetch({ url }) {
|
|
35
|
+
const getMessagesUrl = new URL(url);
|
|
36
|
+
getMessagesUrl.pathname += "/get-messages";
|
|
37
|
+
const response = await fetch(getMessagesUrl.toString(), {
|
|
38
|
+
credentials: options.credentials,
|
|
39
|
+
headers: options.headers
|
|
40
|
+
});
|
|
41
|
+
if (!response.ok) {
|
|
42
|
+
console.warn(`Failed to fetch initial messages: ${response.status} ${response.statusText}`);
|
|
43
|
+
return [];
|
|
44
|
+
}
|
|
45
|
+
const text = await response.text();
|
|
46
|
+
if (!text.trim()) return [];
|
|
47
|
+
try {
|
|
48
|
+
return JSON.parse(text);
|
|
49
|
+
} catch (error) {
|
|
50
|
+
console.warn("Failed to parse initial messages JSON:", error);
|
|
51
|
+
return [];
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
const getInitialMessagesFetch = getInitialMessages || defaultGetInitialMessagesFetch;
|
|
55
|
+
function doGetInitialMessages(getInitialMessagesOptions) {
|
|
56
|
+
if (requestCache.has(agentUrlString)) return requestCache.get(agentUrlString);
|
|
57
|
+
const promise = getInitialMessagesFetch(getInitialMessagesOptions);
|
|
58
|
+
requestCache.set(agentUrlString, promise);
|
|
59
|
+
return promise;
|
|
60
|
+
}
|
|
61
|
+
const initialMessagesPromise = getInitialMessages === null ? null : doGetInitialMessages({
|
|
62
|
+
agent: agent.agent,
|
|
63
|
+
name: agent.name,
|
|
64
|
+
url: agentUrlString
|
|
65
|
+
});
|
|
66
|
+
const initialMessages = initialMessagesPromise ? use(initialMessagesPromise) : optionsInitialMessages ?? [];
|
|
67
|
+
useEffect(() => {
|
|
68
|
+
if (!initialMessagesPromise) return;
|
|
69
|
+
requestCache.set(agentUrlString, initialMessagesPromise);
|
|
70
|
+
return () => {
|
|
71
|
+
if (requestCache.get(agentUrlString) === initialMessagesPromise) requestCache.delete(agentUrlString);
|
|
72
|
+
};
|
|
73
|
+
}, [agentUrlString, initialMessagesPromise]);
|
|
74
|
+
const aiFetch = useCallback(async (request, options$1 = {}) => {
|
|
75
|
+
const { method, keepalive, headers, body, redirect, integrity, signal, credentials, mode, referrer, referrerPolicy, window } = options$1;
|
|
76
|
+
const id = nanoid(8);
|
|
77
|
+
const abortController = new AbortController();
|
|
78
|
+
let controller;
|
|
79
|
+
const currentAgent = agentRef.current;
|
|
80
|
+
signal?.addEventListener("abort", () => {
|
|
81
|
+
currentAgent.send(JSON.stringify({
|
|
82
|
+
id,
|
|
83
|
+
type: MessageType.CF_AGENT_CHAT_REQUEST_CANCEL
|
|
84
|
+
}));
|
|
85
|
+
abortController.abort();
|
|
86
|
+
controller.close();
|
|
87
|
+
});
|
|
88
|
+
currentAgent.addEventListener("message", (event) => {
|
|
89
|
+
let data;
|
|
90
|
+
try {
|
|
91
|
+
data = JSON.parse(event.data);
|
|
92
|
+
} catch (_error) {
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
if (data.type === MessageType.CF_AGENT_USE_CHAT_RESPONSE) {
|
|
96
|
+
if (data.id === id) if (data.error) {
|
|
97
|
+
controller.error(new Error(data.body));
|
|
98
|
+
abortController.abort();
|
|
99
|
+
} else {
|
|
100
|
+
if (data.body?.trim()) controller.enqueue(new TextEncoder().encode(`data: ${data.body}\n\n`));
|
|
101
|
+
if (data.done) {
|
|
102
|
+
controller.close();
|
|
103
|
+
abortController.abort();
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}, { signal: abortController.signal });
|
|
108
|
+
const stream = new ReadableStream({ start(c) {
|
|
109
|
+
controller = c;
|
|
110
|
+
} });
|
|
111
|
+
currentAgent.send(JSON.stringify({
|
|
112
|
+
id,
|
|
113
|
+
init: {
|
|
114
|
+
body,
|
|
115
|
+
credentials,
|
|
116
|
+
headers,
|
|
117
|
+
integrity,
|
|
118
|
+
keepalive,
|
|
119
|
+
method,
|
|
120
|
+
mode,
|
|
121
|
+
redirect,
|
|
122
|
+
referrer,
|
|
123
|
+
referrerPolicy,
|
|
124
|
+
window
|
|
125
|
+
},
|
|
126
|
+
type: MessageType.CF_AGENT_USE_CHAT_REQUEST,
|
|
127
|
+
url: request.toString()
|
|
128
|
+
}));
|
|
129
|
+
return new Response(stream);
|
|
130
|
+
}, []);
|
|
131
|
+
const customTransport = useMemo(() => ({
|
|
132
|
+
sendMessages: async (options$1) => {
|
|
133
|
+
return new DefaultChatTransport({
|
|
134
|
+
api: agentUrlString,
|
|
135
|
+
fetch: aiFetch
|
|
136
|
+
}).sendMessages(options$1);
|
|
137
|
+
},
|
|
138
|
+
reconnectToStream: async (options$1) => {
|
|
139
|
+
return new DefaultChatTransport({
|
|
140
|
+
api: agentUrlString,
|
|
141
|
+
fetch: aiFetch
|
|
142
|
+
}).reconnectToStream(options$1);
|
|
143
|
+
}
|
|
144
|
+
}), [agentUrlString, aiFetch]);
|
|
145
|
+
const useChatHelpers = useChat({
|
|
146
|
+
...rest,
|
|
147
|
+
messages: initialMessages,
|
|
148
|
+
transport: customTransport,
|
|
149
|
+
id: agent._pk
|
|
150
|
+
});
|
|
151
|
+
const processedToolCalls = useRef(/* @__PURE__ */ new Set());
|
|
152
|
+
const lastMessage = useChatHelpers.messages[useChatHelpers.messages.length - 1];
|
|
153
|
+
const pendingConfirmations = (() => {
|
|
154
|
+
if (!lastMessage || lastMessage.role !== "assistant") return {
|
|
155
|
+
messageId: void 0,
|
|
156
|
+
toolCallIds: /* @__PURE__ */ new Set()
|
|
157
|
+
};
|
|
158
|
+
const pendingIds = /* @__PURE__ */ new Set();
|
|
159
|
+
for (const part of lastMessage.parts ?? []) if (isToolUIPart(part) && part.state === "input-available" && toolsRequiringConfirmation.includes(getToolName(part))) pendingIds.add(part.toolCallId);
|
|
160
|
+
return {
|
|
161
|
+
messageId: lastMessage.id,
|
|
162
|
+
toolCallIds: pendingIds
|
|
163
|
+
};
|
|
164
|
+
})();
|
|
165
|
+
const pendingConfirmationsRef = useRef(pendingConfirmations);
|
|
166
|
+
pendingConfirmationsRef.current = pendingConfirmations;
|
|
167
|
+
useEffect(() => {
|
|
168
|
+
if (!experimental_automaticToolResolution) return;
|
|
169
|
+
const lastMessage$1 = useChatHelpers.messages[useChatHelpers.messages.length - 1];
|
|
170
|
+
if (!lastMessage$1 || lastMessage$1.role !== "assistant") return;
|
|
171
|
+
const toolCalls = lastMessage$1.parts.filter((part) => isToolUIPart(part) && part.state === "input-available" && !processedToolCalls.current.has(part.toolCallId));
|
|
172
|
+
if (toolCalls.length > 0) (async () => {
|
|
173
|
+
const toolCallsToResolve = toolCalls.filter((part) => isToolUIPart(part) && !toolsRequiringConfirmation.includes(getToolName(part)) && tools?.[getToolName(part)]?.execute);
|
|
174
|
+
if (toolCallsToResolve.length > 0) {
|
|
175
|
+
for (const part of toolCallsToResolve) if (isToolUIPart(part)) {
|
|
176
|
+
processedToolCalls.current.add(part.toolCallId);
|
|
177
|
+
let toolOutput = null;
|
|
178
|
+
const toolName = getToolName(part);
|
|
179
|
+
const tool$1 = tools?.[toolName];
|
|
180
|
+
if (tool$1?.execute && part.input) try {
|
|
181
|
+
toolOutput = await tool$1.execute(part.input);
|
|
182
|
+
} catch (error) {
|
|
183
|
+
toolOutput = `Error executing tool: ${error instanceof Error ? error.message : String(error)}`;
|
|
184
|
+
}
|
|
185
|
+
await useChatHelpers.addToolResult({
|
|
186
|
+
toolCallId: part.toolCallId,
|
|
187
|
+
tool: toolName,
|
|
188
|
+
output: toolOutput
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
if (pendingConfirmationsRef.current.toolCallIds.size === 0) useChatHelpers.sendMessage();
|
|
192
|
+
}
|
|
193
|
+
})();
|
|
194
|
+
}, [
|
|
195
|
+
useChatHelpers.messages,
|
|
196
|
+
experimental_automaticToolResolution,
|
|
197
|
+
useChatHelpers.addToolResult,
|
|
198
|
+
useChatHelpers.sendMessage,
|
|
199
|
+
toolsRequiringConfirmation
|
|
200
|
+
]);
|
|
201
|
+
useEffect(() => {
|
|
202
|
+
function onClearHistory(event) {
|
|
203
|
+
if (typeof event.data !== "string") return;
|
|
204
|
+
let data;
|
|
205
|
+
try {
|
|
206
|
+
data = JSON.parse(event.data);
|
|
207
|
+
} catch (_error) {
|
|
208
|
+
return;
|
|
209
|
+
}
|
|
210
|
+
if (data.type === MessageType.CF_AGENT_CHAT_CLEAR) useChatHelpers.setMessages([]);
|
|
211
|
+
}
|
|
212
|
+
function onMessages(event) {
|
|
213
|
+
if (typeof event.data !== "string") return;
|
|
214
|
+
let data;
|
|
215
|
+
try {
|
|
216
|
+
data = JSON.parse(event.data);
|
|
217
|
+
} catch (_error) {
|
|
218
|
+
return;
|
|
219
|
+
}
|
|
220
|
+
if (data.type === MessageType.CF_AGENT_CHAT_MESSAGES) useChatHelpers.setMessages(data.messages);
|
|
221
|
+
}
|
|
222
|
+
agent.addEventListener("message", onClearHistory);
|
|
223
|
+
agent.addEventListener("message", onMessages);
|
|
224
|
+
return () => {
|
|
225
|
+
agent.removeEventListener("message", onClearHistory);
|
|
226
|
+
agent.removeEventListener("message", onMessages);
|
|
227
|
+
};
|
|
228
|
+
}, [agent, useChatHelpers.setMessages]);
|
|
229
|
+
const addToolResultAndSendMessage = async (args) => {
|
|
230
|
+
const { toolCallId } = args;
|
|
231
|
+
await useChatHelpers.addToolResult(args);
|
|
232
|
+
if (!autoSendAfterAllConfirmationsResolved) {
|
|
233
|
+
useChatHelpers.sendMessage();
|
|
234
|
+
return;
|
|
235
|
+
}
|
|
236
|
+
const pending = pendingConfirmationsRef.current?.toolCallIds;
|
|
237
|
+
if (!pending) {
|
|
238
|
+
useChatHelpers.sendMessage();
|
|
239
|
+
return;
|
|
240
|
+
}
|
|
241
|
+
const wasLast = pending.size === 1 && pending.has(toolCallId);
|
|
242
|
+
if (pending.has(toolCallId)) pending.delete(toolCallId);
|
|
243
|
+
if (wasLast || pending.size === 0) useChatHelpers.sendMessage();
|
|
244
|
+
};
|
|
245
|
+
return {
|
|
246
|
+
...useChatHelpers,
|
|
247
|
+
addToolResult: addToolResultAndSendMessage,
|
|
248
|
+
clearHistory: () => {
|
|
249
|
+
useChatHelpers.setMessages([]);
|
|
250
|
+
agent.send(JSON.stringify({ type: MessageType.CF_AGENT_CHAT_CLEAR }));
|
|
251
|
+
},
|
|
252
|
+
setMessages: (messages) => {
|
|
253
|
+
useChatHelpers.setMessages(messages);
|
|
254
|
+
agent.send(JSON.stringify({
|
|
255
|
+
messages: Array.isArray(messages) ? messages : [],
|
|
256
|
+
type: MessageType.CF_AGENT_CHAT_MESSAGES
|
|
257
|
+
}));
|
|
258
|
+
}
|
|
259
|
+
};
|
|
345
260
|
}
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
};
|
|
261
|
+
|
|
262
|
+
//#endregion
|
|
263
|
+
export { detectToolsRequiringConfirmation, useAgentChat };
|
|
350
264
|
//# sourceMappingURL=ai-react.js.map
|