agents 0.16.1 → 0.17.0
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 +11 -8
- package/dist/{agent-tool-types-NofdbL9X.d.ts → agent-tool-types-Cd1TZPfB.d.ts} +595 -137
- package/dist/agent-tool-types.d.ts +34 -18
- package/dist/agent-tool-types.js +20 -1
- package/dist/agent-tool-types.js.map +1 -0
- package/dist/agent-tools-BXlsuX0d.js +304 -0
- package/dist/agent-tools-BXlsuX0d.js.map +1 -0
- package/dist/agent-tools-_E8wxUIK.d.ts +119 -0
- package/dist/agent-tools.d.ts +24 -18
- package/dist/agent-tools.js.map +1 -1
- package/dist/ai-chat-agent.d.ts +1 -1
- package/dist/ai-chat-agent.js +1 -2
- package/dist/ai-chat-agent.js.map +1 -1
- package/dist/ai-chat-v5-migration.d.ts +1 -1
- package/dist/ai-chat-v5-migration.js +1 -2
- package/dist/ai-chat-v5-migration.js.map +1 -1
- package/dist/ai-react.d.ts +1 -1
- package/dist/ai-react.js +1 -2
- package/dist/ai-react.js.map +1 -1
- package/dist/ai-types.d.ts +1 -7
- package/dist/ai-types.js +2 -8
- package/dist/ai-types.js.map +1 -1
- package/dist/browser/ai.js +1 -1
- package/dist/browser/index.js +1 -1
- package/dist/chat/index.d.ts +1923 -76
- package/dist/chat/index.js +1784 -278
- package/dist/chat/index.js.map +1 -1
- package/dist/chat/react.d.ts +602 -0
- package/dist/chat/react.js +1506 -0
- package/dist/chat/react.js.map +1 -0
- package/dist/chat-sdk/index.d.ts +4 -4
- package/dist/{classPrivateFieldGet2-CZ7QjTXN.js → classPrivateFieldGet2-DZBYAB34.js} +5 -5
- package/dist/{classPrivateMethodInitSpec-D-0__zd9.js → classPrivateMethodInitSpec-qMjJ6sHQ.js} +2 -2
- package/dist/{client-FUizKzj2.js → client-BZ-B3NhC.js} +19 -3
- package/dist/client-BZ-B3NhC.js.map +1 -0
- package/dist/client-tools-aIBO0Fk7.d.ts +53 -0
- package/dist/client.d.ts +76 -57
- package/dist/client.js +33 -5
- package/dist/client.js.map +1 -1
- package/dist/{compaction-helpers-DVcu5lPN.d.ts → compaction-helpers-wUz6M3us.d.ts} +20 -1
- package/dist/{connector-CrKhowfD.js → connector-CdldGF3h.js} +5 -5
- package/dist/{connector-CrKhowfD.js.map → connector-CdldGF3h.js.map} +1 -1
- package/dist/email.js +1 -1
- package/dist/email.js.map +1 -1
- package/dist/experimental/memory/session/index.d.ts +1 -1
- package/dist/experimental/memory/session/index.js +20 -2
- package/dist/experimental/memory/session/index.js.map +1 -1
- package/dist/experimental/memory/utils/index.d.ts +1 -1
- package/dist/{index-B7IbEeze.d.ts → index-CcbnKkNh.d.ts} +188 -14
- package/dist/index.d.ts +91 -71
- package/dist/index.js +562 -24
- package/dist/index.js.map +1 -1
- package/dist/mcp/client.d.ts +18 -14
- package/dist/mcp/client.js +1 -1
- package/dist/mcp/index.d.ts +30 -30
- package/dist/mcp/index.js +7 -7
- package/dist/mcp/index.js.map +1 -1
- package/dist/{agent-tools-3zLG7MgA.js → message-builder-BymO4N_D.js} +45 -126
- package/dist/message-builder-BymO4N_D.js.map +1 -0
- package/dist/observability/index.d.ts +1 -1
- package/dist/observability/index.js +4 -2
- package/dist/observability/index.js.map +1 -1
- package/dist/react.d.ts +123 -110
- package/dist/react.js +44 -11
- package/dist/react.js.map +1 -1
- package/dist/serializable.d.ts +1 -1
- package/dist/skills/compile.js +1 -1
- package/dist/skills/compile.js.map +1 -1
- package/dist/skills/index.js +5 -5
- package/dist/skills/index.js.map +1 -1
- package/dist/sub-routing.d.ts +6 -6
- package/dist/utils.js +1 -1
- package/dist/utils.js.map +1 -1
- package/dist/vite.js +5 -5
- package/dist/vite.js.map +1 -1
- package/dist/wire-types-nflOzNuU.js +240 -0
- package/dist/wire-types-nflOzNuU.js.map +1 -0
- package/dist/{workflow-types-SrZK_o9p.d.ts → workflow-types-Baz_PO5v.d.ts} +42 -22
- package/dist/workflow-types.d.ts +25 -21
- package/dist/workflow-types.js.map +1 -1
- package/dist/workflows.d.ts +22 -21
- package/dist/workflows.js +31 -7
- package/dist/workflows.js.map +1 -1
- package/docs/adding-to-existing-project.md +450 -0
- package/docs/agent-class.md +503 -0
- package/docs/agent-tools.md +552 -0
- package/docs/browse-the-web.md +430 -0
- package/docs/callable-methods.md +627 -0
- package/docs/chat-agents.md +1687 -0
- package/docs/chat-sdk.md +181 -0
- package/docs/client-sdk.md +520 -0
- package/docs/client-tools-continuation.md +177 -0
- package/docs/codemode.md +440 -0
- package/docs/configuration.md +775 -0
- package/docs/cross-domain-authentication.md +171 -0
- package/docs/durable-execution.md +537 -0
- package/docs/email.md +663 -0
- package/docs/get-current-agent.md +204 -0
- package/docs/getting-started.md +305 -0
- package/docs/http-websockets.md +668 -0
- package/docs/human-in-the-loop.md +661 -0
- package/docs/index.md +151 -0
- package/docs/long-running-agents.md +730 -0
- package/docs/mcp-client.md +620 -0
- package/docs/mcp-servers.md +526 -0
- package/docs/mcp-transports.md +308 -0
- package/docs/migration-to-ai-sdk-v5.md +96 -0
- package/docs/migration-to-ai-sdk-v6.md +163 -0
- package/docs/observability.md +261 -0
- package/docs/push-notifications.md +367 -0
- package/docs/queue.md +329 -0
- package/docs/readonly-connections.md +278 -0
- package/docs/resumable-streaming.md +127 -0
- package/docs/retries.md +444 -0
- package/docs/routing.md +749 -0
- package/docs/scheduling.md +898 -0
- package/docs/securing-mcp-servers.md +359 -0
- package/docs/server-driven-messages.md +477 -0
- package/docs/sessions.md +1024 -0
- package/docs/state.md +512 -0
- package/docs/sub-agents.md +389 -0
- package/docs/webhooks.md +604 -0
- package/docs/workflows.md +877 -0
- package/package.json +47 -15
- package/dist/agent-tools-3zLG7MgA.js.map +0 -1
- package/dist/agent-tools-DLquv-dp.d.ts +0 -14
- package/dist/client-FUizKzj2.js.map +0 -1
|
@@ -0,0 +1,602 @@
|
|
|
1
|
+
import { n as ClientToolSchema } from "../client-tools-aIBO0Fk7.js";
|
|
2
|
+
import {
|
|
3
|
+
ChatInit,
|
|
4
|
+
ChatTransport,
|
|
5
|
+
JSONSchema7,
|
|
6
|
+
Tool,
|
|
7
|
+
UIMessage,
|
|
8
|
+
UIMessageChunk
|
|
9
|
+
} from "ai";
|
|
10
|
+
import { UseChatOptions, useChat } from "@ai-sdk/react";
|
|
11
|
+
|
|
12
|
+
//#region src/chat/ws-chat-transport.d.ts
|
|
13
|
+
/**
|
|
14
|
+
* Agent-like interface for sending/receiving WebSocket messages.
|
|
15
|
+
* Matches the shape returned by useAgent from agents/react.
|
|
16
|
+
*/
|
|
17
|
+
interface AgentConnection {
|
|
18
|
+
send: (data: string) => void;
|
|
19
|
+
addEventListener: (
|
|
20
|
+
type: string,
|
|
21
|
+
listener: (event: MessageEvent) => void,
|
|
22
|
+
options?: {
|
|
23
|
+
signal?: AbortSignal;
|
|
24
|
+
}
|
|
25
|
+
) => void;
|
|
26
|
+
removeEventListener: (
|
|
27
|
+
type: string,
|
|
28
|
+
listener: (event: MessageEvent) => void
|
|
29
|
+
) => void;
|
|
30
|
+
}
|
|
31
|
+
type WebSocketChatTransportOptions<ChatMessage extends UIMessage = UIMessage> =
|
|
32
|
+
{
|
|
33
|
+
/** The agent connection from useAgent */ agent: AgentConnection;
|
|
34
|
+
/**
|
|
35
|
+
* Callback to prepare the request body before sending.
|
|
36
|
+
* Can add custom headers, body fields, or credentials.
|
|
37
|
+
*/
|
|
38
|
+
prepareBody?: (options: {
|
|
39
|
+
messages: ChatMessage[];
|
|
40
|
+
trigger: "submit-message" | "regenerate-message";
|
|
41
|
+
messageId?: string;
|
|
42
|
+
}) => Promise<Record<string, unknown>> | Record<string, unknown>;
|
|
43
|
+
/**
|
|
44
|
+
* Optional set to track active request IDs.
|
|
45
|
+
* IDs are added when a request starts and removed when it completes.
|
|
46
|
+
* Used by the onAgentMessage handler to skip messages already handled by the transport.
|
|
47
|
+
*/
|
|
48
|
+
activeRequestIds?: Set<string>;
|
|
49
|
+
/**
|
|
50
|
+
* Whether generic client-side abort/cancel lifecycle should cancel the
|
|
51
|
+
* server turn. Explicit cancellation via cancelActiveServerTurn() always
|
|
52
|
+
* sends CF_AGENT_CHAT_REQUEST_CANCEL.
|
|
53
|
+
* @default false
|
|
54
|
+
*/
|
|
55
|
+
cancelOnClientAbort?: boolean;
|
|
56
|
+
};
|
|
57
|
+
/**
|
|
58
|
+
* ChatTransport that sends messages over WebSocket and returns a
|
|
59
|
+
* ReadableStream<UIMessageChunk> that the AI SDK's useChat consumes directly.
|
|
60
|
+
* No fake fetch, no Response reconstruction, no double SSE parsing.
|
|
61
|
+
*/
|
|
62
|
+
declare class WebSocketChatTransport<
|
|
63
|
+
ChatMessage extends UIMessage = UIMessage
|
|
64
|
+
> implements ChatTransport<ChatMessage> {
|
|
65
|
+
agent: AgentConnection;
|
|
66
|
+
private prepareBody?;
|
|
67
|
+
private activeRequestIds?;
|
|
68
|
+
private cancelOnClientAbort;
|
|
69
|
+
private _resumeResolver;
|
|
70
|
+
private _resumeNoneResolver;
|
|
71
|
+
private _onStreamPending;
|
|
72
|
+
private _expectToolContinuation;
|
|
73
|
+
private _abortToolContinuation;
|
|
74
|
+
private _activeServerTurnId;
|
|
75
|
+
private _cancelAttachedStream;
|
|
76
|
+
constructor(options: WebSocketChatTransportOptions<ChatMessage>);
|
|
77
|
+
setCancelOnClientAbort(cancelOnClientAbort: boolean): void;
|
|
78
|
+
/**
|
|
79
|
+
* Explicitly cancel the active server turn, if any.
|
|
80
|
+
* This is separate from generic client-side abort/cancel lifecycle so
|
|
81
|
+
* clients can detach locally without stopping server work.
|
|
82
|
+
*/
|
|
83
|
+
cancelActiveServerTurn(): boolean;
|
|
84
|
+
private sendCancelFrame;
|
|
85
|
+
private setActiveServerTurn;
|
|
86
|
+
private clearActiveServerTurn;
|
|
87
|
+
/**
|
|
88
|
+
* Mark that the next reconnectToStream() call should attach to a
|
|
89
|
+
* server-initiated tool continuation rather than a page-load resume.
|
|
90
|
+
*/
|
|
91
|
+
expectToolContinuation(): void;
|
|
92
|
+
/**
|
|
93
|
+
* Abort the active client-side tool continuation stream, if one is attached
|
|
94
|
+
* to a server request id.
|
|
95
|
+
*/
|
|
96
|
+
abortActiveToolContinuation(): boolean;
|
|
97
|
+
/**
|
|
98
|
+
* True when the transport is waiting for a resume handshake.
|
|
99
|
+
*/
|
|
100
|
+
isAwaitingResume(): boolean;
|
|
101
|
+
/**
|
|
102
|
+
* Called by onAgentMessage when it receives CF_AGENT_STREAM_RESUMING.
|
|
103
|
+
* If reconnectToStream is waiting, this handles the resume handshake
|
|
104
|
+
* (ACK + stream creation) and returns true. Otherwise returns false
|
|
105
|
+
* so the caller can use its own fallback path.
|
|
106
|
+
*/
|
|
107
|
+
handleStreamResuming(data: { id: string }): boolean;
|
|
108
|
+
/**
|
|
109
|
+
* Called by onAgentMessage when it receives CF_AGENT_STREAM_RESUME_NONE.
|
|
110
|
+
* If reconnectToStream is waiting, resolves the promise with null
|
|
111
|
+
* immediately (no 5-second timeout). Returns true if handled.
|
|
112
|
+
*/
|
|
113
|
+
handleStreamResumeNone(): boolean;
|
|
114
|
+
/**
|
|
115
|
+
* Called by onAgentMessage when it receives CF_AGENT_STREAM_PENDING (#1784):
|
|
116
|
+
* the server accepted a turn but its stream has not started yet. If a resume
|
|
117
|
+
* path is awaiting, extend its probe timeout (so it keeps waiting for the
|
|
118
|
+
* eventual STREAM_RESUMING / STREAM_RESUME_NONE instead of resolving null
|
|
119
|
+
* after the short window). Returns true if a waiting path consumed it.
|
|
120
|
+
*/
|
|
121
|
+
handleStreamPending(): boolean;
|
|
122
|
+
/**
|
|
123
|
+
* Called by the hook's shared message handler when a server turn finishes
|
|
124
|
+
* outside the currently attached transport stream, such as after local-only
|
|
125
|
+
* client cleanup.
|
|
126
|
+
*/
|
|
127
|
+
handleServerTurnCompleted(requestId: string): void;
|
|
128
|
+
/**
|
|
129
|
+
* Register a server turn that is being rendered outside a transport-owned
|
|
130
|
+
* stream, such as the hook's fallback cross-tab/resume observer path.
|
|
131
|
+
*/
|
|
132
|
+
observeServerTurn(requestId: string): void;
|
|
133
|
+
sendMessages(options: {
|
|
134
|
+
chatId: string;
|
|
135
|
+
messages: ChatMessage[];
|
|
136
|
+
abortSignal: AbortSignal | undefined;
|
|
137
|
+
trigger: "submit-message" | "regenerate-message";
|
|
138
|
+
messageId?: string;
|
|
139
|
+
body?: object;
|
|
140
|
+
headers?: Record<string, string> | Headers;
|
|
141
|
+
metadata?: unknown;
|
|
142
|
+
}): Promise<ReadableStream<UIMessageChunk>>;
|
|
143
|
+
reconnectToStream(_options: {
|
|
144
|
+
chatId: string;
|
|
145
|
+
}): Promise<ReadableStream<UIMessageChunk> | null>;
|
|
146
|
+
/**
|
|
147
|
+
* Creates a deferred ReadableStream for client-side tool continuations.
|
|
148
|
+
* The stream is returned immediately so AI SDK status becomes "submitted"
|
|
149
|
+
* right after addToolOutput()/addToolApprovalResponse(), then it waits for
|
|
150
|
+
* the server to announce the continuation via STREAM_RESUMING.
|
|
151
|
+
*/
|
|
152
|
+
private _createToolContinuationStream;
|
|
153
|
+
/**
|
|
154
|
+
* Creates a ReadableStream that receives resumed stream chunks
|
|
155
|
+
* and forwards them to useChat as UIMessageChunk objects.
|
|
156
|
+
*/
|
|
157
|
+
private _createResumeStream;
|
|
158
|
+
}
|
|
159
|
+
//#endregion
|
|
160
|
+
//#region src/chat/react.d.ts
|
|
161
|
+
type AgentConnectionErrorLike = Error & {
|
|
162
|
+
code: number;
|
|
163
|
+
reason: string;
|
|
164
|
+
wasClean: boolean;
|
|
165
|
+
};
|
|
166
|
+
/**
|
|
167
|
+
* JSON Schema type for tool parameters.
|
|
168
|
+
* Re-exported from the AI SDK for convenience.
|
|
169
|
+
* @deprecated Import JSONSchema7 directly from "ai" instead. Will be removed in the next major version.
|
|
170
|
+
*/
|
|
171
|
+
type JSONSchemaType = JSONSchema7;
|
|
172
|
+
/**
|
|
173
|
+
* Definition for a tool that can be executed on the client.
|
|
174
|
+
* Tools with an `execute` function are automatically registered with the server.
|
|
175
|
+
*
|
|
176
|
+
* **For most apps**, define tools on the server with `tool()` from `"ai"` —
|
|
177
|
+
* you get full Zod type safety and simpler code. Use `onToolCall` in
|
|
178
|
+
* `useAgentChat` for tools that need browser-side execution.
|
|
179
|
+
*
|
|
180
|
+
* **For SDKs and platforms** where the tool surface is determined dynamically
|
|
181
|
+
* by the embedding application at runtime, this type lets the client register
|
|
182
|
+
* tools the server does not know about at deploy time.
|
|
183
|
+
*
|
|
184
|
+
* Note: Uses `parameters` (JSONSchema7) because client tools must be
|
|
185
|
+
* serializable for the wire format. Zod schemas cannot be serialized.
|
|
186
|
+
*/
|
|
187
|
+
type AITool<Input = unknown, Output = unknown> = {
|
|
188
|
+
/** Human-readable description of what the tool does */ description?: Tool["description"] /** JSON Schema defining the tool's input parameters */;
|
|
189
|
+
parameters?: JSONSchema7;
|
|
190
|
+
/**
|
|
191
|
+
* @deprecated Use `parameters` instead. Will be removed in a future version.
|
|
192
|
+
*/
|
|
193
|
+
inputSchema?: JSONSchema7;
|
|
194
|
+
/**
|
|
195
|
+
* Function to execute the tool on the client.
|
|
196
|
+
* If provided, the tool schema is automatically sent to the server.
|
|
197
|
+
*/
|
|
198
|
+
execute?: (input: Input) => Output | Promise<Output>;
|
|
199
|
+
};
|
|
200
|
+
/**
|
|
201
|
+
* Extracts tool schemas from tools that have client-side execute functions.
|
|
202
|
+
* These schemas are automatically sent to the server with each request.
|
|
203
|
+
*
|
|
204
|
+
* Called internally by `useAgentChat` when `tools` are provided.
|
|
205
|
+
* Most apps do not need to call this directly.
|
|
206
|
+
*
|
|
207
|
+
* @param tools - Record of tool name to tool definition
|
|
208
|
+
* @returns Array of tool schemas to send to server, or undefined if none
|
|
209
|
+
*/
|
|
210
|
+
declare function extractClientToolSchemas(
|
|
211
|
+
tools?: Record<string, AITool<unknown, unknown>>
|
|
212
|
+
): ClientToolSchema[] | undefined;
|
|
213
|
+
/**
|
|
214
|
+
* Map internal tool part states to simplified UI-relevant states.
|
|
215
|
+
*
|
|
216
|
+
* @example
|
|
217
|
+
* ```tsx
|
|
218
|
+
* import { isToolUIPart } from "ai";
|
|
219
|
+
* import { getToolPartState } from "@cloudflare/ai-chat/react";
|
|
220
|
+
*
|
|
221
|
+
* if (isToolUIPart(part)) {
|
|
222
|
+
* const state = getToolPartState(part);
|
|
223
|
+
* if (state === "complete") { ... }
|
|
224
|
+
* if (state === "waiting-approval") { ... }
|
|
225
|
+
* }
|
|
226
|
+
* ```
|
|
227
|
+
*/
|
|
228
|
+
declare function getToolPartState(
|
|
229
|
+
part: UIMessage["parts"][number]
|
|
230
|
+
):
|
|
231
|
+
| "loading"
|
|
232
|
+
| "streaming"
|
|
233
|
+
| "waiting-approval"
|
|
234
|
+
| "approved"
|
|
235
|
+
| "complete"
|
|
236
|
+
| "error"
|
|
237
|
+
| "denied";
|
|
238
|
+
/** Get the tool call ID from a tool UI part. */
|
|
239
|
+
declare function getToolCallId(part: UIMessage["parts"][number]): string;
|
|
240
|
+
/** Get the tool input from a tool UI part (if available). */
|
|
241
|
+
declare function getToolInput(
|
|
242
|
+
part: UIMessage["parts"][number]
|
|
243
|
+
): unknown | undefined;
|
|
244
|
+
/** Get the tool output from a tool UI part (if available). */
|
|
245
|
+
declare function getToolOutput(
|
|
246
|
+
part: UIMessage["parts"][number]
|
|
247
|
+
): unknown | undefined;
|
|
248
|
+
/** Get the approval info from a tool UI part (if in approval state). */
|
|
249
|
+
declare function getToolApproval(part: UIMessage["parts"][number]):
|
|
250
|
+
| {
|
|
251
|
+
id: string;
|
|
252
|
+
approved?: boolean;
|
|
253
|
+
}
|
|
254
|
+
| undefined;
|
|
255
|
+
/**
|
|
256
|
+
* Fetch messages from an agent's `/get-messages` HTTP endpoint.
|
|
257
|
+
*
|
|
258
|
+
* Use in framework route loaders to prefetch messages before the component
|
|
259
|
+
* tree mounts, or anywhere you need messages outside a React hook.
|
|
260
|
+
*
|
|
261
|
+
* @example Standard routing
|
|
262
|
+
* ```typescript
|
|
263
|
+
* import { getAgentMessages } from "@cloudflare/ai-chat/react";
|
|
264
|
+
*
|
|
265
|
+
* const messages = await getAgentMessages({
|
|
266
|
+
* host: "https://my-app.workers.dev",
|
|
267
|
+
* agent: "ChatAgent",
|
|
268
|
+
* name: "session-123"
|
|
269
|
+
* });
|
|
270
|
+
* ```
|
|
271
|
+
*
|
|
272
|
+
* @example With basePath (custom URL)
|
|
273
|
+
* ```typescript
|
|
274
|
+
* const messages = await getAgentMessages({
|
|
275
|
+
* url: "https://my-app.workers.dev/custom/path/get-messages"
|
|
276
|
+
* });
|
|
277
|
+
* ```
|
|
278
|
+
*/
|
|
279
|
+
declare function getAgentMessages<M extends UIMessage = UIMessage>(
|
|
280
|
+
options:
|
|
281
|
+
| {
|
|
282
|
+
host: string;
|
|
283
|
+
agent: string;
|
|
284
|
+
name: string;
|
|
285
|
+
credentials?: RequestCredentials;
|
|
286
|
+
headers?: HeadersInit;
|
|
287
|
+
}
|
|
288
|
+
| {
|
|
289
|
+
url: string;
|
|
290
|
+
credentials?: RequestCredentials;
|
|
291
|
+
headers?: HeadersInit;
|
|
292
|
+
}
|
|
293
|
+
): Promise<M[]>;
|
|
294
|
+
type GetInitialMessagesOptions = {
|
|
295
|
+
agent: string;
|
|
296
|
+
name: string;
|
|
297
|
+
url?: string;
|
|
298
|
+
};
|
|
299
|
+
type UseChatParams<M extends UIMessage = UIMessage> = ChatInit<M> &
|
|
300
|
+
UseChatOptions<M>;
|
|
301
|
+
/**
|
|
302
|
+
* Options for preparing the send messages request.
|
|
303
|
+
* Used by prepareSendMessagesRequest callback.
|
|
304
|
+
*/
|
|
305
|
+
type PrepareSendMessagesRequestOptions<
|
|
306
|
+
ChatMessage extends UIMessage = UIMessage
|
|
307
|
+
> = {
|
|
308
|
+
/** The chat ID */ id: string /** Messages to send */;
|
|
309
|
+
messages: ChatMessage[] /** What triggered this request */;
|
|
310
|
+
trigger:
|
|
311
|
+
| "submit-message"
|
|
312
|
+
| "regenerate-message" /** ID of the message being sent (if applicable) */;
|
|
313
|
+
messageId?: string /** Request metadata */;
|
|
314
|
+
requestMetadata?: unknown /** Current body (if any) */;
|
|
315
|
+
body?: Record<string, unknown> /** Current credentials (if any) */;
|
|
316
|
+
credentials?: RequestCredentials /** Current headers (if any) */;
|
|
317
|
+
headers?: HeadersInit /** API endpoint */;
|
|
318
|
+
api?: string;
|
|
319
|
+
};
|
|
320
|
+
/**
|
|
321
|
+
* Return type for prepareSendMessagesRequest callback.
|
|
322
|
+
* Allows customizing headers, body, and credentials for each request.
|
|
323
|
+
* All fields are optional; only specify what you need to customize.
|
|
324
|
+
*/
|
|
325
|
+
type PrepareSendMessagesRequestResult = {
|
|
326
|
+
/** Custom headers to send with the request */ headers?: HeadersInit /** Custom body data to merge with the request */;
|
|
327
|
+
body?: Record<string, unknown> /** Custom credentials option */;
|
|
328
|
+
credentials?: RequestCredentials /** Custom API endpoint */;
|
|
329
|
+
api?: string;
|
|
330
|
+
};
|
|
331
|
+
/**
|
|
332
|
+
* Options for addToolOutput function
|
|
333
|
+
*/
|
|
334
|
+
type AddToolOutputOptions = {
|
|
335
|
+
/** The ID of the tool call to provide output for */ toolCallId: string /** The name of the tool (optional, for type safety) */;
|
|
336
|
+
toolName?: string /** The output to provide */;
|
|
337
|
+
output?: unknown /** Override the tool part state (e.g. "output-error" for custom denial) */;
|
|
338
|
+
state?:
|
|
339
|
+
| "output-available"
|
|
340
|
+
| "output-error" /** Error message when state is "output-error" */;
|
|
341
|
+
errorText?: string;
|
|
342
|
+
};
|
|
343
|
+
/**
|
|
344
|
+
* Callback for handling client-side tool execution.
|
|
345
|
+
* Called when a tool without server-side execute is invoked.
|
|
346
|
+
*/
|
|
347
|
+
type OnToolCallCallback = (options: {
|
|
348
|
+
/** The tool call that needs to be handled */ toolCall: {
|
|
349
|
+
toolCallId: string;
|
|
350
|
+
toolName: string;
|
|
351
|
+
input: unknown;
|
|
352
|
+
} /** Function to provide the tool output (or signal an error/denial) */;
|
|
353
|
+
addToolOutput: (options: Omit<AddToolOutputOptions, "toolName">) => void;
|
|
354
|
+
}) => void | Promise<void>;
|
|
355
|
+
/**
|
|
356
|
+
* Options for the useAgentChat hook
|
|
357
|
+
*/
|
|
358
|
+
type UseAgentChatOptions<
|
|
359
|
+
State = unknown,
|
|
360
|
+
ChatMessage extends UIMessage = UIMessage
|
|
361
|
+
> = Omit<UseChatParams<ChatMessage>, "fetch" | "onToolCall"> & {
|
|
362
|
+
/** Agent connection from useAgent (accepts both typed and untyped agents) */ agent: AgentConnection & {
|
|
363
|
+
agent: string;
|
|
364
|
+
name: string;
|
|
365
|
+
path?: ReadonlyArray<{
|
|
366
|
+
agent: string;
|
|
367
|
+
name: string;
|
|
368
|
+
}>;
|
|
369
|
+
connectionError?: AgentConnectionErrorLike | null;
|
|
370
|
+
getHttpUrl: () => string;
|
|
371
|
+
};
|
|
372
|
+
getInitialMessages?:
|
|
373
|
+
| undefined
|
|
374
|
+
| null
|
|
375
|
+
| ((
|
|
376
|
+
options: GetInitialMessagesOptions
|
|
377
|
+
) => Promise<ChatMessage[]>) /** Request credentials */;
|
|
378
|
+
credentials?: RequestCredentials /** Request headers */;
|
|
379
|
+
headers?: HeadersInit;
|
|
380
|
+
/**
|
|
381
|
+
* Callback for handling client-side tool execution.
|
|
382
|
+
* Called when a tool without server-side `execute` is invoked by the LLM.
|
|
383
|
+
*
|
|
384
|
+
* Use this for:
|
|
385
|
+
* - Tools that need browser APIs (geolocation, camera, etc.)
|
|
386
|
+
* - Tools that need user interaction before providing a result
|
|
387
|
+
* - Tools requiring approval before execution
|
|
388
|
+
*
|
|
389
|
+
* @example
|
|
390
|
+
* ```typescript
|
|
391
|
+
* onToolCall: async ({ toolCall, addToolOutput }) => {
|
|
392
|
+
* if (toolCall.toolName === 'getLocation') {
|
|
393
|
+
* const position = await navigator.geolocation.getCurrentPosition();
|
|
394
|
+
* addToolOutput({
|
|
395
|
+
* toolCallId: toolCall.toolCallId,
|
|
396
|
+
* output: { lat: position.coords.latitude, lng: position.coords.longitude }
|
|
397
|
+
* });
|
|
398
|
+
* }
|
|
399
|
+
* }
|
|
400
|
+
* ```
|
|
401
|
+
*/
|
|
402
|
+
onToolCall?: OnToolCallCallback;
|
|
403
|
+
/**
|
|
404
|
+
* @deprecated Use `onToolCall` callback instead for automatic tool execution.
|
|
405
|
+
* @description Whether to automatically resolve tool calls that do not require human interaction.
|
|
406
|
+
* @experimental
|
|
407
|
+
*/
|
|
408
|
+
experimental_automaticToolResolution?: boolean;
|
|
409
|
+
/**
|
|
410
|
+
* Tools that can be executed on the client. Tool schemas are automatically
|
|
411
|
+
* sent to the server and tool calls are routed back for client execution.
|
|
412
|
+
*
|
|
413
|
+
* **For most apps**, define tools on the server with `tool()` from `"ai"`
|
|
414
|
+
* and handle client-side execution via `onToolCall`. This gives you full
|
|
415
|
+
* Zod type safety and keeps tool definitions in one place.
|
|
416
|
+
*
|
|
417
|
+
* **For SDKs and platforms** where tools are defined dynamically by the
|
|
418
|
+
* embedding application at runtime, this option lets the client register
|
|
419
|
+
* tools the server does not know about at deploy time.
|
|
420
|
+
*/
|
|
421
|
+
tools?: Record<string, AITool<unknown, unknown>>;
|
|
422
|
+
/**
|
|
423
|
+
* @deprecated Use `needsApproval` on server-side tools instead.
|
|
424
|
+
* @description Manual override for tools requiring confirmation.
|
|
425
|
+
* If not provided, will auto-detect from tools object (tools without execute require confirmation).
|
|
426
|
+
*/
|
|
427
|
+
toolsRequiringConfirmation?: string[];
|
|
428
|
+
/**
|
|
429
|
+
* When true (default), the server automatically continues the conversation
|
|
430
|
+
* after receiving client-side tool results or approvals, similar to how
|
|
431
|
+
* server-executed tools work with maxSteps in streamText. The continuation
|
|
432
|
+
* is merged into the same assistant message.
|
|
433
|
+
*
|
|
434
|
+
* When false, the client must call sendMessage() after tool results
|
|
435
|
+
* to continue the conversation, which creates a new assistant message.
|
|
436
|
+
*
|
|
437
|
+
* @default true
|
|
438
|
+
*/
|
|
439
|
+
autoContinueAfterToolResult?: boolean;
|
|
440
|
+
/**
|
|
441
|
+
* @deprecated Use `sendAutomaticallyWhen` from AI SDK instead.
|
|
442
|
+
*
|
|
443
|
+
* When true (default), automatically sends the next message only after
|
|
444
|
+
* all pending confirmation-required tool calls have been resolved.
|
|
445
|
+
* When false, sends immediately after each tool result.
|
|
446
|
+
*
|
|
447
|
+
* Only applies when `autoContinueAfterToolResult` is false.
|
|
448
|
+
*
|
|
449
|
+
* @default true
|
|
450
|
+
*/
|
|
451
|
+
autoSendAfterAllConfirmationsResolved?: boolean;
|
|
452
|
+
/**
|
|
453
|
+
* Set to false to disable automatic stream resumption.
|
|
454
|
+
* @default true
|
|
455
|
+
*/
|
|
456
|
+
resume?: boolean;
|
|
457
|
+
/**
|
|
458
|
+
* Whether generic client-side stream abort/cleanup should cancel the server
|
|
459
|
+
* turn. By default, client cleanup is local-only so the server turn can
|
|
460
|
+
* continue and be resumed on reconnect. Explicit stop() always cancels the
|
|
461
|
+
* server turn.
|
|
462
|
+
*
|
|
463
|
+
* @default false
|
|
464
|
+
*/
|
|
465
|
+
cancelOnClientAbort?: boolean;
|
|
466
|
+
/**
|
|
467
|
+
* Whether `setMessages` should also send the full client transcript to the
|
|
468
|
+
* server as `CF_AGENT_CHAT_MESSAGES`. This is useful for flat transcript
|
|
469
|
+
* stores such as `AIChatAgent`, but should be disabled for server-authoritative
|
|
470
|
+
* hosts whose client messages are only a projection of richer storage.
|
|
471
|
+
*
|
|
472
|
+
* @default true
|
|
473
|
+
*/
|
|
474
|
+
syncMessagesToServer?: boolean;
|
|
475
|
+
/**
|
|
476
|
+
* Custom data to include in every chat request body.
|
|
477
|
+
* Accepts a static object or a function that returns one (for dynamic values).
|
|
478
|
+
* These fields are available in `onChatMessage` via `options.body`.
|
|
479
|
+
*
|
|
480
|
+
* @example
|
|
481
|
+
* ```typescript
|
|
482
|
+
* // Static
|
|
483
|
+
* body: { timezone: "America/New_York", userId: "abc" }
|
|
484
|
+
*
|
|
485
|
+
* // Dynamic (called on each send)
|
|
486
|
+
* body: () => ({ token: getAuthToken(), timestamp: Date.now() })
|
|
487
|
+
* ```
|
|
488
|
+
*/
|
|
489
|
+
body?:
|
|
490
|
+
| Record<string, unknown>
|
|
491
|
+
| (() => Record<string, unknown> | Promise<Record<string, unknown>>);
|
|
492
|
+
/**
|
|
493
|
+
* Callback to customize the request before sending messages.
|
|
494
|
+
* For most cases, use the `body` option instead.
|
|
495
|
+
* Use this for advanced scenarios that need access to the messages or trigger type.
|
|
496
|
+
*
|
|
497
|
+
* Note: Client tool schemas are automatically sent when tools have `execute` functions.
|
|
498
|
+
* This callback can add additional data alongside the auto-extracted schemas.
|
|
499
|
+
*/
|
|
500
|
+
prepareSendMessagesRequest?: (
|
|
501
|
+
options: PrepareSendMessagesRequestOptions<ChatMessage>
|
|
502
|
+
) =>
|
|
503
|
+
| PrepareSendMessagesRequestResult
|
|
504
|
+
| Promise<PrepareSendMessagesRequestResult>;
|
|
505
|
+
};
|
|
506
|
+
/**
|
|
507
|
+
* React hook for building AI chat interfaces using an Agent
|
|
508
|
+
* @param options Chat options including the agent connection
|
|
509
|
+
* @returns Chat interface controls and state with added clearHistory method
|
|
510
|
+
*/
|
|
511
|
+
/**
|
|
512
|
+
* Automatically detects which tools require confirmation based on their configuration.
|
|
513
|
+
* Tools require confirmation if they have no execute function AND are not server-executed.
|
|
514
|
+
* @param tools - Record of tool name to tool definition
|
|
515
|
+
* @returns Array of tool names that require confirmation
|
|
516
|
+
*
|
|
517
|
+
* @deprecated Use `needsApproval` on server-side tools instead.
|
|
518
|
+
*/
|
|
519
|
+
declare function detectToolsRequiringConfirmation(
|
|
520
|
+
tools?: Record<string, AITool<unknown, unknown>>
|
|
521
|
+
): string[];
|
|
522
|
+
declare function useAgentChat<
|
|
523
|
+
State = unknown,
|
|
524
|
+
ChatMessage extends UIMessage = UIMessage
|
|
525
|
+
>(
|
|
526
|
+
options: UseAgentChatOptions<State, ChatMessage>
|
|
527
|
+
): Omit<ReturnType<typeof useChat<ChatMessage>>, "addToolOutput"> & {
|
|
528
|
+
clearHistory: () => void;
|
|
529
|
+
/**
|
|
530
|
+
* Provide output for a tool call. Use this for tools that require user interaction
|
|
531
|
+
* or client-side execution.
|
|
532
|
+
*/
|
|
533
|
+
addToolOutput: (opts: AddToolOutputOptions) => void;
|
|
534
|
+
/**
|
|
535
|
+
* Whether a server-initiated stream (e.g. from `saveMessages`,
|
|
536
|
+
* auto-continuation, or another tab) is currently active, OR a
|
|
537
|
+
* client-side tool call is awaiting resolution via `onToolCall`.
|
|
538
|
+
* Covers the full "turn-in-progress" window from the consumer's
|
|
539
|
+
* perspective, including the gap between the model emitting a
|
|
540
|
+
* client-tool call and the server pushing a continuation after
|
|
541
|
+
* `addToolOutput`. This is independent of the AI SDK's `status`
|
|
542
|
+
* which only tracks client-initiated request/response cycles.
|
|
543
|
+
*/
|
|
544
|
+
isServerStreaming: boolean;
|
|
545
|
+
/**
|
|
546
|
+
* Convenience flag: `true` when either the client-initiated stream
|
|
547
|
+
* (`status === "streaming"`) or a server-initiated stream is active.
|
|
548
|
+
* Use this for showing a universal streaming indicator.
|
|
549
|
+
*/
|
|
550
|
+
isStreaming: boolean;
|
|
551
|
+
/**
|
|
552
|
+
* `true` while a durable chat turn is being recovered (interrupted by a
|
|
553
|
+
* deploy/eviction or a stream-stall watchdog abort and now resuming, #1620).
|
|
554
|
+
* Distinct from `isStreaming` — a recovering turn isn't producing tokens yet,
|
|
555
|
+
* so a client can show a "recovering…" hint instead of looking frozen. Most
|
|
556
|
+
* UIs treat `isStreaming || isRecovering` as "busy". Driven by the server's
|
|
557
|
+
* `CF_AGENT_CHAT_RECOVERING` frames (also replayed on connect for
|
|
558
|
+
* `@cloudflare/think`); cleared automatically on the next stream or terminal.
|
|
559
|
+
*/
|
|
560
|
+
isRecovering: boolean;
|
|
561
|
+
/**
|
|
562
|
+
* `true` when the current `status`/`isServerStreaming` activity is
|
|
563
|
+
* driven by a server-pushed tool continuation (i.e. the server is
|
|
564
|
+
* auto-continuing the conversation after `addToolOutput` or
|
|
565
|
+
* `addToolApprovalResponse`) rather than a fresh user submission.
|
|
566
|
+
*
|
|
567
|
+
* Use this to disambiguate "user just sent a new message, awaiting
|
|
568
|
+
* first token" from "mid-turn tool round-trip" — e.g. when you want
|
|
569
|
+
* a typing indicator only for the former:
|
|
570
|
+
*
|
|
571
|
+
* ```tsx
|
|
572
|
+
* const showTypingIndicator = status === "submitted" && !isToolContinuation;
|
|
573
|
+
* ```
|
|
574
|
+
*
|
|
575
|
+
* See issue #1365.
|
|
576
|
+
*/
|
|
577
|
+
isToolContinuation: boolean;
|
|
578
|
+
connectionError: AgentConnectionErrorLike | null;
|
|
579
|
+
};
|
|
580
|
+
//#endregion
|
|
581
|
+
export {
|
|
582
|
+
AITool,
|
|
583
|
+
type AgentConnection,
|
|
584
|
+
type ClientToolSchema,
|
|
585
|
+
JSONSchemaType,
|
|
586
|
+
OnToolCallCallback,
|
|
587
|
+
PrepareSendMessagesRequestOptions,
|
|
588
|
+
PrepareSendMessagesRequestResult,
|
|
589
|
+
UseAgentChatOptions,
|
|
590
|
+
WebSocketChatTransport,
|
|
591
|
+
type WebSocketChatTransportOptions,
|
|
592
|
+
detectToolsRequiringConfirmation,
|
|
593
|
+
extractClientToolSchemas,
|
|
594
|
+
getAgentMessages,
|
|
595
|
+
getToolApproval,
|
|
596
|
+
getToolCallId,
|
|
597
|
+
getToolInput,
|
|
598
|
+
getToolOutput,
|
|
599
|
+
getToolPartState,
|
|
600
|
+
useAgentChat
|
|
601
|
+
};
|
|
602
|
+
//# sourceMappingURL=react.d.ts.map
|