agents 0.0.0-b275dea → 0.0.0-b299d20
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/_esm-LV5FJ3HK.js +3922 -0
- package/dist/_esm-LV5FJ3HK.js.map +1 -0
- package/dist/ai-chat-agent.d.ts +13 -9
- package/dist/ai-chat-agent.js +189 -59
- package/dist/ai-chat-agent.js.map +1 -1
- package/dist/ai-chat-v5-migration.d.ts +152 -0
- package/dist/ai-chat-v5-migration.js +20 -0
- package/dist/ai-chat-v5-migration.js.map +1 -0
- package/dist/ai-react.d.ts +70 -72
- package/dist/ai-react.js +260 -107
- package/dist/ai-react.js.map +1 -1
- package/dist/ai-types.d.ts +37 -19
- package/dist/ai-types.js +7 -0
- package/dist/ccip-CMBYN64O.js +15 -0
- package/dist/ccip-CMBYN64O.js.map +1 -0
- package/dist/chunk-5Y6BEZDY.js +276 -0
- package/dist/chunk-5Y6BEZDY.js.map +1 -0
- package/dist/chunk-BER7KXUJ.js +18 -0
- package/dist/chunk-BER7KXUJ.js.map +1 -0
- package/dist/chunk-JJBFIGUC.js +5202 -0
- package/dist/chunk-JJBFIGUC.js.map +1 -0
- package/dist/chunk-PR4QN5HX.js +43 -0
- package/dist/chunk-PR4QN5HX.js.map +1 -0
- package/dist/{chunk-Y67CHZBI.js → chunk-QEPGNUG6.js} +231 -45
- package/dist/chunk-QEPGNUG6.js.map +1 -0
- package/dist/{chunk-QSGN3REV.js → chunk-QEVM4BVL.js} +10 -17
- package/dist/chunk-QEVM4BVL.js.map +1 -0
- package/dist/chunk-RS5OCNEQ.js +1323 -0
- package/dist/chunk-RS5OCNEQ.js.map +1 -0
- package/dist/chunk-TYAY6AU6.js +159 -0
- package/dist/chunk-TYAY6AU6.js.map +1 -0
- package/dist/chunk-UJVEAURM.js +150 -0
- package/dist/chunk-UJVEAURM.js.map +1 -0
- package/dist/{chunk-BZXOAZUX.js → chunk-XFS5ERG3.js} +27 -6
- package/dist/chunk-XFS5ERG3.js.map +1 -0
- package/dist/client-BohGLma8.d.ts +5041 -0
- package/dist/client.d.ts +8 -2
- package/dist/client.js +3 -1
- package/dist/index.d.ts +187 -27
- package/dist/index.js +14 -4
- package/dist/mcp/client.d.ts +9 -781
- package/dist/mcp/client.js +2 -1
- package/dist/mcp/do-oauth-client-provider.d.ts +9 -0
- package/dist/mcp/do-oauth-client-provider.js +2 -1
- package/dist/mcp/index.d.ts +58 -64
- package/dist/mcp/index.js +957 -637
- package/dist/mcp/index.js.map +1 -1
- package/dist/mcp/x402.d.ts +39 -0
- package/dist/mcp/x402.js +3195 -0
- package/dist/mcp/x402.js.map +1 -0
- package/dist/observability/index.d.ts +46 -0
- package/dist/observability/index.js +12 -0
- package/dist/observability/index.js.map +1 -0
- package/dist/react.d.ts +55 -36
- package/dist/react.js +22 -9
- package/dist/react.js.map +1 -1
- package/dist/schedule.d.ts +81 -7
- package/dist/schedule.js +21 -6
- package/dist/schedule.js.map +1 -1
- package/dist/secp256k1-M22GZP2U.js +2193 -0
- package/dist/secp256k1-M22GZP2U.js.map +1 -0
- package/dist/serializable.d.ts +32 -0
- package/dist/serializable.js +1 -0
- package/dist/serializable.js.map +1 -0
- package/package.json +91 -71
- package/src/index.ts +941 -204
- package/dist/chunk-AXSPGBHI.js +0 -783
- package/dist/chunk-AXSPGBHI.js.map +0 -1
- package/dist/chunk-BZXOAZUX.js.map +0 -1
- package/dist/chunk-QSGN3REV.js.map +0 -1
- package/dist/chunk-Y67CHZBI.js.map +0 -1
package/dist/ai-react.d.ts
CHANGED
|
@@ -1,100 +1,98 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
3
|
-
import { useChat } from "@ai-sdk/react";
|
|
1
|
+
import { UseChatOptions, useChat } from "@ai-sdk/react";
|
|
2
|
+
import { UIMessage, ChatInit } from "ai";
|
|
4
3
|
import { useAgent } from "./react.js";
|
|
5
4
|
import "partysocket";
|
|
6
5
|
import "partysocket/react";
|
|
7
6
|
import "./index.js";
|
|
7
|
+
import "cloudflare:workers";
|
|
8
|
+
import "@modelcontextprotocol/sdk/client/index.js";
|
|
9
|
+
import "@modelcontextprotocol/sdk/types.js";
|
|
8
10
|
import "partyserver";
|
|
9
|
-
import "./
|
|
11
|
+
import "./client-BohGLma8.js";
|
|
10
12
|
import "zod";
|
|
11
|
-
import "@modelcontextprotocol/sdk/
|
|
12
|
-
import "@modelcontextprotocol/sdk/client/index.js";
|
|
13
|
+
import "@modelcontextprotocol/sdk/shared/protocol.js";
|
|
13
14
|
import "@modelcontextprotocol/sdk/client/sse.js";
|
|
15
|
+
import "@modelcontextprotocol/sdk/client/streamableHttp.js";
|
|
14
16
|
import "./mcp/do-oauth-client-provider.js";
|
|
15
17
|
import "@modelcontextprotocol/sdk/client/auth.js";
|
|
16
18
|
import "@modelcontextprotocol/sdk/shared/auth.js";
|
|
17
|
-
import "
|
|
19
|
+
import "./observability/index.js";
|
|
20
|
+
import "./ai-types.js";
|
|
18
21
|
import "./client.js";
|
|
22
|
+
import "./serializable.js";
|
|
19
23
|
|
|
24
|
+
type AITool<Input = unknown, Output = unknown> = {
|
|
25
|
+
description?: string;
|
|
26
|
+
inputSchema?: unknown;
|
|
27
|
+
execute?: (input: Input) => Output | Promise<Output>;
|
|
28
|
+
};
|
|
20
29
|
type GetInitialMessagesOptions = {
|
|
21
30
|
agent: string;
|
|
22
31
|
name: string;
|
|
23
32
|
url: string;
|
|
24
33
|
};
|
|
34
|
+
type UseChatParams<M extends UIMessage = UIMessage> = ChatInit<M> &
|
|
35
|
+
UseChatOptions<M>;
|
|
25
36
|
/**
|
|
26
37
|
* Options for the useAgentChat hook
|
|
27
38
|
*/
|
|
28
|
-
type UseAgentChatOptions<
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
+
type UseAgentChatOptions<
|
|
40
|
+
State,
|
|
41
|
+
ChatMessage extends UIMessage = UIMessage
|
|
42
|
+
> = Omit<UseChatParams<ChatMessage>, "fetch"> & {
|
|
43
|
+
/** Agent connection from useAgent */
|
|
44
|
+
agent: ReturnType<typeof useAgent<State>>;
|
|
45
|
+
getInitialMessages?:
|
|
46
|
+
| undefined
|
|
47
|
+
| null
|
|
48
|
+
| ((options: GetInitialMessagesOptions) => Promise<ChatMessage[]>);
|
|
49
|
+
/** Request credentials */
|
|
50
|
+
credentials?: RequestCredentials;
|
|
51
|
+
/** Request headers */
|
|
52
|
+
headers?: HeadersInit;
|
|
53
|
+
/**
|
|
54
|
+
* @description Whether to automatically resolve tool calls that do not require human interaction.
|
|
55
|
+
* @experimental
|
|
56
|
+
*/
|
|
57
|
+
experimental_automaticToolResolution?: boolean;
|
|
58
|
+
/**
|
|
59
|
+
* @description Tools object for automatic detection of confirmation requirements.
|
|
60
|
+
* Tools without execute function will require confirmation.
|
|
61
|
+
*/
|
|
62
|
+
tools?: Record<string, AITool<unknown, unknown>>;
|
|
63
|
+
/**
|
|
64
|
+
* @description Manual override for tools requiring confirmation.
|
|
65
|
+
* If not provided, will auto-detect from tools object.
|
|
66
|
+
*/
|
|
67
|
+
toolsRequiringConfirmation?: string[];
|
|
68
|
+
/**
|
|
69
|
+
* When true (default), automatically sends the next message only after
|
|
70
|
+
* all pending confirmation-required tool calls have been resolved.
|
|
71
|
+
* @default true
|
|
72
|
+
*/
|
|
73
|
+
autoSendAfterAllConfirmationsResolved?: boolean;
|
|
74
|
+
};
|
|
39
75
|
/**
|
|
40
76
|
* React hook for building AI chat interfaces using an Agent
|
|
41
77
|
* @param options Chat options including the agent connection
|
|
42
78
|
* @returns Chat interface controls and state with added clearHistory method
|
|
43
79
|
*/
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
80
|
+
/**
|
|
81
|
+
* Automatically detects which tools require confirmation based on their configuration.
|
|
82
|
+
* Tools require confirmation if they have no execute function AND are not server-executed.
|
|
83
|
+
* @param tools - Record of tool name to tool definition
|
|
84
|
+
* @returns Array of tool names that require confirmation
|
|
85
|
+
*/
|
|
86
|
+
declare function detectToolsRequiringConfirmation(
|
|
87
|
+
tools?: Record<string, AITool<unknown, unknown>>
|
|
88
|
+
): string[];
|
|
89
|
+
declare function useAgentChat<
|
|
90
|
+
State = unknown,
|
|
91
|
+
ChatMessage extends UIMessage = UIMessage
|
|
92
|
+
>(
|
|
93
|
+
options: UseAgentChatOptions<State, ChatMessage>
|
|
94
|
+
): ReturnType<typeof useChat<ChatMessage>> & {
|
|
55
95
|
clearHistory: () => void;
|
|
56
|
-
messages: ai.UIMessage[];
|
|
57
|
-
error: undefined | Error;
|
|
58
|
-
append: (
|
|
59
|
-
message: Message | ai.CreateMessage,
|
|
60
|
-
chatRequestOptions?: ai.ChatRequestOptions
|
|
61
|
-
) => Promise<string | null | undefined>;
|
|
62
|
-
reload: (
|
|
63
|
-
chatRequestOptions?: ai.ChatRequestOptions
|
|
64
|
-
) => Promise<string | null | undefined>;
|
|
65
|
-
stop: () => void;
|
|
66
|
-
experimental_resume: () => void;
|
|
67
|
-
input: string;
|
|
68
|
-
setInput: React.Dispatch<React.SetStateAction<string>>;
|
|
69
|
-
handleInputChange: (
|
|
70
|
-
e:
|
|
71
|
-
| React.ChangeEvent<HTMLInputElement>
|
|
72
|
-
| React.ChangeEvent<HTMLTextAreaElement>
|
|
73
|
-
) => void;
|
|
74
|
-
handleSubmit: (
|
|
75
|
-
event?: {
|
|
76
|
-
preventDefault?: () => void;
|
|
77
|
-
},
|
|
78
|
-
chatRequestOptions?: ai.ChatRequestOptions
|
|
79
|
-
) => void;
|
|
80
|
-
metadata?: Object;
|
|
81
|
-
isLoading: boolean;
|
|
82
|
-
status: "submitted" | "streaming" | "ready" | "error";
|
|
83
|
-
data?: ai.JSONValue[];
|
|
84
|
-
setData: (
|
|
85
|
-
data:
|
|
86
|
-
| ai.JSONValue[]
|
|
87
|
-
| undefined
|
|
88
|
-
| ((data: ai.JSONValue[] | undefined) => ai.JSONValue[] | undefined)
|
|
89
|
-
) => void;
|
|
90
|
-
id: string;
|
|
91
|
-
addToolResult: ({
|
|
92
|
-
toolCallId,
|
|
93
|
-
result,
|
|
94
|
-
}: {
|
|
95
|
-
toolCallId: string;
|
|
96
|
-
result: any;
|
|
97
|
-
}) => void;
|
|
98
96
|
};
|
|
99
97
|
|
|
100
|
-
export { useAgentChat };
|
|
98
|
+
export { type AITool, detectToolsRequiringConfirmation, useAgentChat };
|
package/dist/ai-react.js
CHANGED
|
@@ -1,26 +1,64 @@
|
|
|
1
|
+
import "./chunk-BER7KXUJ.js";
|
|
2
|
+
import "./chunk-PR4QN5HX.js";
|
|
3
|
+
|
|
1
4
|
// src/ai-react.tsx
|
|
2
5
|
import { useChat } from "@ai-sdk/react";
|
|
3
|
-
import {
|
|
6
|
+
import { getToolName, isToolUIPart } from "ai";
|
|
7
|
+
import { DefaultChatTransport } from "ai";
|
|
4
8
|
import { nanoid } from "nanoid";
|
|
9
|
+
import { use, useCallback, useEffect, useMemo, useRef } from "react";
|
|
5
10
|
var requestCache = /* @__PURE__ */ new Map();
|
|
11
|
+
function detectToolsRequiringConfirmation(tools) {
|
|
12
|
+
if (!tools) return [];
|
|
13
|
+
return Object.entries(tools).filter(([_name, tool]) => !tool.execute).map(([name]) => name);
|
|
14
|
+
}
|
|
6
15
|
function useAgentChat(options) {
|
|
7
|
-
const {
|
|
16
|
+
const {
|
|
17
|
+
agent,
|
|
18
|
+
getInitialMessages,
|
|
19
|
+
messages: optionsInitialMessages,
|
|
20
|
+
experimental_automaticToolResolution,
|
|
21
|
+
tools,
|
|
22
|
+
toolsRequiringConfirmation: manualToolsRequiringConfirmation,
|
|
23
|
+
autoSendAfterAllConfirmationsResolved = true,
|
|
24
|
+
...rest
|
|
25
|
+
} = options;
|
|
26
|
+
const toolsRequiringConfirmation = manualToolsRequiringConfirmation ?? detectToolsRequiringConfirmation(tools);
|
|
8
27
|
const agentUrl = new URL(
|
|
9
28
|
`${// @ts-expect-error we're using a protected _url property that includes query params
|
|
10
29
|
(agent._url || agent._pkurl)?.replace("ws://", "http://").replace("wss://", "https://")}`
|
|
11
30
|
);
|
|
12
31
|
agentUrl.searchParams.delete("_pk");
|
|
13
32
|
const agentUrlString = agentUrl.toString();
|
|
33
|
+
const agentRef = useRef(agent);
|
|
34
|
+
useEffect(() => {
|
|
35
|
+
agentRef.current = agent;
|
|
36
|
+
}, [agent]);
|
|
14
37
|
async function defaultGetInitialMessagesFetch({
|
|
15
38
|
url
|
|
16
39
|
}) {
|
|
17
40
|
const getMessagesUrl = new URL(url);
|
|
18
41
|
getMessagesUrl.pathname += "/get-messages";
|
|
19
42
|
const response = await fetch(getMessagesUrl.toString(), {
|
|
20
|
-
|
|
21
|
-
|
|
43
|
+
credentials: options.credentials,
|
|
44
|
+
headers: options.headers
|
|
22
45
|
});
|
|
23
|
-
|
|
46
|
+
if (!response.ok) {
|
|
47
|
+
console.warn(
|
|
48
|
+
`Failed to fetch initial messages: ${response.status} ${response.statusText}`
|
|
49
|
+
);
|
|
50
|
+
return [];
|
|
51
|
+
}
|
|
52
|
+
const text = await response.text();
|
|
53
|
+
if (!text.trim()) {
|
|
54
|
+
return [];
|
|
55
|
+
}
|
|
56
|
+
try {
|
|
57
|
+
return JSON.parse(text);
|
|
58
|
+
} catch (error) {
|
|
59
|
+
console.warn("Failed to parse initial messages JSON:", error);
|
|
60
|
+
return [];
|
|
61
|
+
}
|
|
24
62
|
}
|
|
25
63
|
const getInitialMessagesFetch = getInitialMessages || defaultGetInitialMessagesFetch;
|
|
26
64
|
function doGetInitialMessages(getInitialMessagesOptions) {
|
|
@@ -36,7 +74,7 @@ function useAgentChat(options) {
|
|
|
36
74
|
name: agent.name,
|
|
37
75
|
url: agentUrlString
|
|
38
76
|
});
|
|
39
|
-
const initialMessages = initialMessagesPromise ? use(initialMessagesPromise) :
|
|
77
|
+
const initialMessages = initialMessagesPromise ? use(initialMessagesPromise) : optionsInitialMessages ?? [];
|
|
40
78
|
useEffect(() => {
|
|
41
79
|
if (!initialMessagesPromise) {
|
|
42
80
|
return;
|
|
@@ -48,117 +86,217 @@ function useAgentChat(options) {
|
|
|
48
86
|
}
|
|
49
87
|
};
|
|
50
88
|
}, [agentUrlString, initialMessagesPromise]);
|
|
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
|
-
let data;
|
|
83
|
-
try {
|
|
84
|
-
data = JSON.parse(event.data);
|
|
85
|
-
} catch (error) {
|
|
86
|
-
return;
|
|
89
|
+
const aiFetch = useCallback(
|
|
90
|
+
async (request, options2 = {}) => {
|
|
91
|
+
const {
|
|
92
|
+
method,
|
|
93
|
+
keepalive,
|
|
94
|
+
headers,
|
|
95
|
+
body,
|
|
96
|
+
redirect,
|
|
97
|
+
integrity,
|
|
98
|
+
signal,
|
|
99
|
+
credentials,
|
|
100
|
+
mode,
|
|
101
|
+
referrer,
|
|
102
|
+
referrerPolicy,
|
|
103
|
+
window
|
|
104
|
+
} = options2;
|
|
105
|
+
const id = nanoid(8);
|
|
106
|
+
const abortController = new AbortController();
|
|
107
|
+
let controller;
|
|
108
|
+
let isToolCallInProgress = false;
|
|
109
|
+
const currentAgent = agentRef.current;
|
|
110
|
+
signal?.addEventListener("abort", () => {
|
|
111
|
+
currentAgent.send(
|
|
112
|
+
JSON.stringify({
|
|
113
|
+
id,
|
|
114
|
+
type: "cf_agent_chat_request_cancel" /* CF_AGENT_CHAT_REQUEST_CANCEL */
|
|
115
|
+
})
|
|
116
|
+
);
|
|
117
|
+
abortController.abort();
|
|
118
|
+
if (!isToolCallInProgress) {
|
|
119
|
+
controller.close();
|
|
87
120
|
}
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
121
|
+
});
|
|
122
|
+
currentAgent.addEventListener(
|
|
123
|
+
"message",
|
|
124
|
+
(event) => {
|
|
125
|
+
let data;
|
|
126
|
+
try {
|
|
127
|
+
data = JSON.parse(event.data);
|
|
128
|
+
} catch (_error) {
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
if (data.type === "cf_agent_use_chat_response" /* CF_AGENT_USE_CHAT_RESPONSE */) {
|
|
132
|
+
if (data.id === id) {
|
|
133
|
+
if (data.error) {
|
|
134
|
+
controller.error(new Error(data.body));
|
|
135
|
+
abortController.abort();
|
|
136
|
+
} else {
|
|
137
|
+
if (data.body?.trim()) {
|
|
138
|
+
if (data.body.includes('"tool_calls"')) {
|
|
139
|
+
isToolCallInProgress = true;
|
|
140
|
+
}
|
|
141
|
+
controller.enqueue(
|
|
142
|
+
new TextEncoder().encode(`data: ${data.body}
|
|
143
|
+
|
|
144
|
+
`)
|
|
145
|
+
);
|
|
146
|
+
}
|
|
147
|
+
if (data.done && !isToolCallInProgress) {
|
|
148
|
+
controller.close();
|
|
149
|
+
abortController.abort();
|
|
150
|
+
}
|
|
151
|
+
}
|
|
94
152
|
}
|
|
95
153
|
}
|
|
154
|
+
},
|
|
155
|
+
{ signal: abortController.signal }
|
|
156
|
+
);
|
|
157
|
+
const stream = new ReadableStream({
|
|
158
|
+
start(c) {
|
|
159
|
+
controller = c;
|
|
96
160
|
}
|
|
161
|
+
});
|
|
162
|
+
currentAgent.send(
|
|
163
|
+
JSON.stringify({
|
|
164
|
+
id,
|
|
165
|
+
init: {
|
|
166
|
+
body,
|
|
167
|
+
credentials,
|
|
168
|
+
headers,
|
|
169
|
+
integrity,
|
|
170
|
+
keepalive,
|
|
171
|
+
method,
|
|
172
|
+
mode,
|
|
173
|
+
redirect,
|
|
174
|
+
referrer,
|
|
175
|
+
referrerPolicy,
|
|
176
|
+
window
|
|
177
|
+
},
|
|
178
|
+
type: "cf_agent_use_chat_request" /* CF_AGENT_USE_CHAT_REQUEST */,
|
|
179
|
+
url: request.toString()
|
|
180
|
+
})
|
|
181
|
+
);
|
|
182
|
+
return new Response(stream);
|
|
183
|
+
},
|
|
184
|
+
[]
|
|
185
|
+
);
|
|
186
|
+
const customTransport = useMemo(
|
|
187
|
+
() => ({
|
|
188
|
+
sendMessages: async (options2) => {
|
|
189
|
+
const transport = new DefaultChatTransport({
|
|
190
|
+
api: agentUrlString,
|
|
191
|
+
fetch: aiFetch
|
|
192
|
+
});
|
|
193
|
+
return transport.sendMessages(options2);
|
|
97
194
|
},
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
195
|
+
reconnectToStream: async (options2) => {
|
|
196
|
+
const transport = new DefaultChatTransport({
|
|
197
|
+
api: agentUrlString,
|
|
198
|
+
fetch: aiFetch
|
|
199
|
+
});
|
|
200
|
+
return transport.reconnectToStream(options2);
|
|
104
201
|
}
|
|
105
|
-
})
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
type: "cf_agent_use_chat_request",
|
|
109
|
-
id,
|
|
110
|
-
url: request.toString(),
|
|
111
|
-
init: {
|
|
112
|
-
method,
|
|
113
|
-
keepalive,
|
|
114
|
-
headers,
|
|
115
|
-
body,
|
|
116
|
-
redirect,
|
|
117
|
-
integrity,
|
|
118
|
-
credentials,
|
|
119
|
-
mode,
|
|
120
|
-
referrer,
|
|
121
|
-
referrerPolicy,
|
|
122
|
-
window
|
|
123
|
-
// dispatcher,
|
|
124
|
-
// duplex
|
|
125
|
-
}
|
|
126
|
-
})
|
|
127
|
-
);
|
|
128
|
-
return new Response(stream);
|
|
129
|
-
}
|
|
202
|
+
}),
|
|
203
|
+
[agentUrlString, aiFetch]
|
|
204
|
+
);
|
|
130
205
|
const useChatHelpers = useChat({
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
206
|
+
...rest,
|
|
207
|
+
messages: initialMessages,
|
|
208
|
+
transport: customTransport,
|
|
209
|
+
id: agent._pk
|
|
135
210
|
});
|
|
211
|
+
const processedToolCalls = useRef(/* @__PURE__ */ new Set());
|
|
212
|
+
const lastMessage = useChatHelpers.messages[useChatHelpers.messages.length - 1];
|
|
213
|
+
const pendingConfirmations = (() => {
|
|
214
|
+
if (!lastMessage || lastMessage.role !== "assistant") {
|
|
215
|
+
return { messageId: void 0, toolCallIds: /* @__PURE__ */ new Set() };
|
|
216
|
+
}
|
|
217
|
+
const pendingIds = /* @__PURE__ */ new Set();
|
|
218
|
+
for (const part of lastMessage.parts ?? []) {
|
|
219
|
+
if (isToolUIPart(part) && part.state === "input-available" && toolsRequiringConfirmation.includes(getToolName(part))) {
|
|
220
|
+
pendingIds.add(part.toolCallId);
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
return { messageId: lastMessage.id, toolCallIds: pendingIds };
|
|
224
|
+
})();
|
|
225
|
+
const pendingConfirmationsRef = useRef(pendingConfirmations);
|
|
226
|
+
pendingConfirmationsRef.current = pendingConfirmations;
|
|
227
|
+
useEffect(() => {
|
|
228
|
+
if (!experimental_automaticToolResolution) {
|
|
229
|
+
return;
|
|
230
|
+
}
|
|
231
|
+
const lastMessage2 = useChatHelpers.messages[useChatHelpers.messages.length - 1];
|
|
232
|
+
if (!lastMessage2 || lastMessage2.role !== "assistant") {
|
|
233
|
+
return;
|
|
234
|
+
}
|
|
235
|
+
const toolCalls = lastMessage2.parts.filter(
|
|
236
|
+
(part) => isToolUIPart(part) && part.state === "input-available" && !processedToolCalls.current.has(part.toolCallId)
|
|
237
|
+
);
|
|
238
|
+
if (toolCalls.length > 0) {
|
|
239
|
+
(async () => {
|
|
240
|
+
const toolCallsToResolve = toolCalls.filter(
|
|
241
|
+
(part) => isToolUIPart(part) && !toolsRequiringConfirmation.includes(getToolName(part)) && tools?.[getToolName(part)]?.execute
|
|
242
|
+
// Only execute if client has execute function
|
|
243
|
+
);
|
|
244
|
+
if (toolCallsToResolve.length > 0) {
|
|
245
|
+
for (const part of toolCallsToResolve) {
|
|
246
|
+
if (isToolUIPart(part)) {
|
|
247
|
+
processedToolCalls.current.add(part.toolCallId);
|
|
248
|
+
let toolOutput = null;
|
|
249
|
+
const toolName = getToolName(part);
|
|
250
|
+
const tool = tools?.[toolName];
|
|
251
|
+
if (tool?.execute && part.input) {
|
|
252
|
+
try {
|
|
253
|
+
toolOutput = await tool.execute(part.input);
|
|
254
|
+
} catch (error) {
|
|
255
|
+
toolOutput = `Error executing tool: ${error instanceof Error ? error.message : String(error)}`;
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
await useChatHelpers.addToolResult({
|
|
259
|
+
toolCallId: part.toolCallId,
|
|
260
|
+
tool: toolName,
|
|
261
|
+
output: toolOutput
|
|
262
|
+
});
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
if (pendingConfirmationsRef.current.toolCallIds.size === 0) {
|
|
266
|
+
useChatHelpers.sendMessage();
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
})();
|
|
270
|
+
}
|
|
271
|
+
}, [
|
|
272
|
+
useChatHelpers.messages,
|
|
273
|
+
experimental_automaticToolResolution,
|
|
274
|
+
useChatHelpers.addToolResult,
|
|
275
|
+
useChatHelpers.sendMessage,
|
|
276
|
+
toolsRequiringConfirmation
|
|
277
|
+
]);
|
|
136
278
|
useEffect(() => {
|
|
137
279
|
function onClearHistory(event) {
|
|
138
|
-
if (typeof event.data !== "string")
|
|
139
|
-
return;
|
|
140
|
-
}
|
|
280
|
+
if (typeof event.data !== "string") return;
|
|
141
281
|
let data;
|
|
142
282
|
try {
|
|
143
283
|
data = JSON.parse(event.data);
|
|
144
|
-
} catch (
|
|
284
|
+
} catch (_error) {
|
|
145
285
|
return;
|
|
146
286
|
}
|
|
147
|
-
if (data.type === "cf_agent_chat_clear") {
|
|
287
|
+
if (data.type === "cf_agent_chat_clear" /* CF_AGENT_CHAT_CLEAR */) {
|
|
148
288
|
useChatHelpers.setMessages([]);
|
|
149
289
|
}
|
|
150
290
|
}
|
|
151
291
|
function onMessages(event) {
|
|
152
|
-
if (typeof event.data !== "string")
|
|
153
|
-
return;
|
|
154
|
-
}
|
|
292
|
+
if (typeof event.data !== "string") return;
|
|
155
293
|
let data;
|
|
156
294
|
try {
|
|
157
295
|
data = JSON.parse(event.data);
|
|
158
|
-
} catch (
|
|
296
|
+
} catch (_error) {
|
|
159
297
|
return;
|
|
160
298
|
}
|
|
161
|
-
if (data.type === "cf_agent_chat_messages") {
|
|
299
|
+
if (data.type === "cf_agent_chat_messages" /* CF_AGENT_CHAT_MESSAGES */) {
|
|
162
300
|
useChatHelpers.setMessages(data.messages);
|
|
163
301
|
}
|
|
164
302
|
}
|
|
@@ -169,35 +307,50 @@ function useAgentChat(options) {
|
|
|
169
307
|
agent.removeEventListener("message", onMessages);
|
|
170
308
|
};
|
|
171
309
|
}, [agent, useChatHelpers.setMessages]);
|
|
310
|
+
const addToolResultAndSendMessage = async (args) => {
|
|
311
|
+
const { toolCallId } = args;
|
|
312
|
+
await useChatHelpers.addToolResult(args);
|
|
313
|
+
if (!autoSendAfterAllConfirmationsResolved) {
|
|
314
|
+
useChatHelpers.sendMessage();
|
|
315
|
+
return;
|
|
316
|
+
}
|
|
317
|
+
const pending = pendingConfirmationsRef.current?.toolCallIds;
|
|
318
|
+
if (!pending) {
|
|
319
|
+
useChatHelpers.sendMessage();
|
|
320
|
+
return;
|
|
321
|
+
}
|
|
322
|
+
const wasLast = pending.size === 1 && pending.has(toolCallId);
|
|
323
|
+
if (pending.has(toolCallId)) {
|
|
324
|
+
pending.delete(toolCallId);
|
|
325
|
+
}
|
|
326
|
+
if (wasLast || pending.size === 0) {
|
|
327
|
+
useChatHelpers.sendMessage();
|
|
328
|
+
}
|
|
329
|
+
};
|
|
172
330
|
return {
|
|
173
331
|
...useChatHelpers,
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
*/
|
|
178
|
-
setMessages: (messages) => {
|
|
179
|
-
useChatHelpers.setMessages(messages);
|
|
332
|
+
addToolResult: addToolResultAndSendMessage,
|
|
333
|
+
clearHistory: () => {
|
|
334
|
+
useChatHelpers.setMessages([]);
|
|
180
335
|
agent.send(
|
|
181
336
|
JSON.stringify({
|
|
182
|
-
type: "
|
|
183
|
-
messages
|
|
337
|
+
type: "cf_agent_chat_clear" /* CF_AGENT_CHAT_CLEAR */
|
|
184
338
|
})
|
|
185
339
|
);
|
|
186
340
|
},
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
*/
|
|
190
|
-
clearHistory: () => {
|
|
191
|
-
useChatHelpers.setMessages([]);
|
|
341
|
+
setMessages: (messages) => {
|
|
342
|
+
useChatHelpers.setMessages(messages);
|
|
192
343
|
agent.send(
|
|
193
344
|
JSON.stringify({
|
|
194
|
-
|
|
345
|
+
messages: Array.isArray(messages) ? messages : [],
|
|
346
|
+
type: "cf_agent_chat_messages" /* CF_AGENT_CHAT_MESSAGES */
|
|
195
347
|
})
|
|
196
348
|
);
|
|
197
349
|
}
|
|
198
350
|
};
|
|
199
351
|
}
|
|
200
352
|
export {
|
|
353
|
+
detectToolsRequiringConfirmation,
|
|
201
354
|
useAgentChat
|
|
202
355
|
};
|
|
203
356
|
//# sourceMappingURL=ai-react.js.map
|