agents 0.0.0-295f4dd → 0.0.0-29938d4
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 +121 -0
- package/dist/ai-chat-agent.d.ts +142 -17
- package/dist/ai-chat-agent.js +346 -40
- package/dist/ai-chat-agent.js.map +1 -1
- package/dist/ai-chat-v5-migration.js +154 -2
- package/dist/ai-chat-v5-migration.js.map +1 -0
- package/dist/ai-react.d.ts +136 -17
- package/dist/ai-react.js +216 -53
- package/dist/ai-react.js.map +1 -1
- package/dist/{ai-types-D_hTbf25.d.ts → ai-types-0OnT3FHg.d.ts} +26 -2
- package/dist/{ai-types-CwgHzwUb.js → ai-types-DEtF_8Km.js} +5 -1
- package/dist/ai-types-DEtF_8Km.js.map +1 -0
- package/dist/ai-types.d.ts +1 -1
- package/dist/ai-types.js +1 -1
- package/dist/{client-ClORm6f0.d.ts → client-CdM5I962.d.ts} +2 -2
- package/dist/{client-C_M5uPrn.d.ts → client-DFotUKH_.d.ts} +9 -9
- package/dist/{client-CcyhkGfN.js → client-DjTPRM8-.js} +2 -2
- package/dist/{client-CcyhkGfN.js.map → client-DjTPRM8-.js.map} +1 -1
- package/dist/client.d.ts +1 -2
- package/dist/client.js +1 -2
- package/dist/codemode/ai.js +3 -4
- package/dist/codemode/ai.js.map +1 -1
- package/dist/{context-_sPQqJWv.d.ts → context-DcbQ8o7k.d.ts} +1 -1
- package/dist/context.d.ts +1 -1
- package/dist/{do-oauth-client-provider-B-ryFIPr.d.ts → do-oauth-client-provider--To1Tsjj.d.ts} +1 -1
- package/dist/{index-DUnsVDnf.d.ts → index-CT2tCrLr.d.ts} +14 -14
- package/dist/{index-CyDpAVHZ.d.ts → index-DLuxm_9W.d.ts} +2 -2
- package/dist/index.d.ts +4 -7
- package/dist/index.js +2 -3
- package/dist/mcp/client.d.ts +1 -3
- package/dist/mcp/do-oauth-client-provider.d.ts +1 -1
- package/dist/mcp/index.d.ts +18 -12
- package/dist/mcp/index.js +21 -49
- package/dist/mcp/index.js.map +1 -1
- package/dist/{mcp-CzbSsLfc.d.ts → mcp-CPSfGUgd.d.ts} +1 -1
- package/dist/observability/index.d.ts +1 -2
- package/dist/observability/index.js +2 -3
- package/dist/react.d.ts +133 -14
- package/dist/react.js +50 -57
- package/dist/react.js.map +1 -1
- package/dist/schedule.d.ts +18 -72
- package/dist/{serializable-C4GLimgv.d.ts → serializable-Crsj26mx.d.ts} +1 -1
- package/dist/serializable.d.ts +1 -1
- package/dist/{src-BmbDclOA.js → src-BZDh910Z.js} +3 -3
- package/dist/src-BZDh910Z.js.map +1 -0
- package/package.json +28 -13
- package/dist/ai-chat-v5-migration-DguhuLKF.js +0 -155
- package/dist/ai-chat-v5-migration-DguhuLKF.js.map +0 -1
- package/dist/ai-types-CwgHzwUb.js.map +0 -1
- package/dist/react-DIGWa87z.d.ts +0 -137
- package/dist/src-BmbDclOA.js.map +0 -1
package/README.md
CHANGED
|
@@ -515,6 +515,127 @@ This creates:
|
|
|
515
515
|
- Intuitive input handling
|
|
516
516
|
- Easy conversation reset
|
|
517
517
|
|
|
518
|
+
#### Client-Defined Tools
|
|
519
|
+
|
|
520
|
+
For scenarios where each client needs to register its own tools dynamically (e.g., embeddable chat widgets), use the `tools` option with `execute` functions.
|
|
521
|
+
|
|
522
|
+
Tools with an `execute` function are automatically:
|
|
523
|
+
|
|
524
|
+
1. Sent to the server as schemas with each request
|
|
525
|
+
2. Executed on the client when the AI model calls them
|
|
526
|
+
|
|
527
|
+
##### Client-Side Tool Definition
|
|
528
|
+
|
|
529
|
+
```tsx
|
|
530
|
+
import { useAgent } from "agents/react";
|
|
531
|
+
import { useAgentChat, type AITool } from "agents/ai-react";
|
|
532
|
+
|
|
533
|
+
// Define tools outside component to avoid recreation on every render
|
|
534
|
+
const tools: Record<string, AITool> = {
|
|
535
|
+
showAlert: {
|
|
536
|
+
description: "Shows an alert dialog to the user",
|
|
537
|
+
parameters: {
|
|
538
|
+
type: "object",
|
|
539
|
+
properties: { message: { type: "string" } },
|
|
540
|
+
required: ["message"]
|
|
541
|
+
},
|
|
542
|
+
execute: async (input) => {
|
|
543
|
+
const { message } = input as { message: string };
|
|
544
|
+
alert(message);
|
|
545
|
+
return { success: true };
|
|
546
|
+
}
|
|
547
|
+
},
|
|
548
|
+
changeBackgroundColor: {
|
|
549
|
+
description: "Changes the page background color",
|
|
550
|
+
parameters: {
|
|
551
|
+
type: "object",
|
|
552
|
+
properties: { color: { type: "string" } }
|
|
553
|
+
},
|
|
554
|
+
execute: async (input) => {
|
|
555
|
+
const { color } = input as { color: string };
|
|
556
|
+
document.body.style.backgroundColor = color;
|
|
557
|
+
return { success: true, color };
|
|
558
|
+
}
|
|
559
|
+
}
|
|
560
|
+
};
|
|
561
|
+
|
|
562
|
+
function EmbeddableChat() {
|
|
563
|
+
const agent = useAgent({ agent: "chat-widget" });
|
|
564
|
+
|
|
565
|
+
const { messages, input, handleInputChange, handleSubmit } = useAgentChat({
|
|
566
|
+
agent,
|
|
567
|
+
tools // Schema + execute in one place
|
|
568
|
+
});
|
|
569
|
+
|
|
570
|
+
return (
|
|
571
|
+
<div className="chat-widget">
|
|
572
|
+
{messages.map((message) => (
|
|
573
|
+
<div key={message.id}>{/* Render message */}</div>
|
|
574
|
+
))}
|
|
575
|
+
<form onSubmit={handleSubmit}>
|
|
576
|
+
<input value={input} onChange={handleInputChange} />
|
|
577
|
+
</form>
|
|
578
|
+
</div>
|
|
579
|
+
);
|
|
580
|
+
}
|
|
581
|
+
```
|
|
582
|
+
|
|
583
|
+
##### Server-Side Tool Handling
|
|
584
|
+
|
|
585
|
+
On the server, use `createToolsFromClientSchemas` to convert client tool schemas to AI SDK format:
|
|
586
|
+
|
|
587
|
+
```typescript
|
|
588
|
+
import {
|
|
589
|
+
AIChatAgent,
|
|
590
|
+
createToolsFromClientSchemas
|
|
591
|
+
} from "agents/ai-chat-agent";
|
|
592
|
+
import { openai } from "@ai-sdk/openai";
|
|
593
|
+
import { streamText, convertToModelMessages } from "ai";
|
|
594
|
+
|
|
595
|
+
export class ChatWidget extends AIChatAgent {
|
|
596
|
+
async onChatMessage(onFinish, options) {
|
|
597
|
+
const result = streamText({
|
|
598
|
+
model: openai("gpt-4o"),
|
|
599
|
+
messages: convertToModelMessages(this.messages),
|
|
600
|
+
tools: {
|
|
601
|
+
// Server-side tools (execute on server)
|
|
602
|
+
getWeather: tool({
|
|
603
|
+
description: "Get weather for a city",
|
|
604
|
+
parameters: z.object({ city: z.string() }),
|
|
605
|
+
execute: async ({ city }) => fetchWeather(city)
|
|
606
|
+
}),
|
|
607
|
+
// Client-side tools (sent back to client for execution)
|
|
608
|
+
...createToolsFromClientSchemas(options?.clientTools)
|
|
609
|
+
},
|
|
610
|
+
onFinish
|
|
611
|
+
});
|
|
612
|
+
return result.toUIMessageStreamResponse();
|
|
613
|
+
}
|
|
614
|
+
}
|
|
615
|
+
```
|
|
616
|
+
|
|
617
|
+
##### Advanced: Custom Request Data
|
|
618
|
+
|
|
619
|
+
For additional control (custom headers, dynamic context), use `prepareSendMessagesRequest`:
|
|
620
|
+
|
|
621
|
+
```tsx
|
|
622
|
+
const { messages, handleSubmit } = useAgentChat({
|
|
623
|
+
agent,
|
|
624
|
+
tools, // Tool schemas auto-extracted and sent
|
|
625
|
+
prepareSendMessagesRequest: ({ id, messages }) => ({
|
|
626
|
+
body: {
|
|
627
|
+
// Add dynamic context alongside auto-extracted tool schemas
|
|
628
|
+
currentUrl: window.location.href,
|
|
629
|
+
userTimezone: Intl.DateTimeFormat().resolvedOptions().timeZone
|
|
630
|
+
},
|
|
631
|
+
headers: {
|
|
632
|
+
"X-Widget-Version": "1.0.0",
|
|
633
|
+
"X-Request-ID": crypto.randomUUID()
|
|
634
|
+
}
|
|
635
|
+
})
|
|
636
|
+
});
|
|
637
|
+
```
|
|
638
|
+
|
|
518
639
|
### 🔗 MCP (Model Context Protocol) Integration
|
|
519
640
|
|
|
520
641
|
Agents can seamlessly integrate with the Model Context Protocol, allowing them to act as both MCP servers (providing tools to AI assistants) and MCP clients (using tools from other services).
|
package/dist/ai-chat-agent.d.ts
CHANGED
|
@@ -1,21 +1,65 @@
|
|
|
1
|
-
import "./context-
|
|
2
|
-
import "./client-
|
|
3
|
-
import "./
|
|
4
|
-
import "./
|
|
5
|
-
import
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
1
|
+
import "./context-DcbQ8o7k.js";
|
|
2
|
+
import "./client-DFotUKH_.js";
|
|
3
|
+
import "./ai-types-0OnT3FHg.js";
|
|
4
|
+
import { n as AgentContext, t as Agent } from "./index-CT2tCrLr.js";
|
|
5
|
+
import {
|
|
6
|
+
JSONSchema7,
|
|
7
|
+
StreamTextOnFinishCallback,
|
|
8
|
+
Tool,
|
|
9
|
+
ToolSet,
|
|
10
|
+
UIMessage
|
|
11
|
+
} from "ai";
|
|
9
12
|
|
|
10
13
|
//#region src/ai-chat-agent.d.ts
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Schema for a client-defined tool sent from the browser.
|
|
17
|
+
* These tools are executed on the client, not the server.
|
|
18
|
+
*
|
|
19
|
+
* Note: Uses `parameters` (JSONSchema7) rather than AI SDK's `inputSchema` (FlexibleSchema)
|
|
20
|
+
* because this is the wire format. Zod schemas cannot be serialized.
|
|
21
|
+
*/
|
|
22
|
+
type ClientToolSchema = {
|
|
23
|
+
/** Unique name for the tool */
|
|
24
|
+
name: string;
|
|
25
|
+
/** Human-readable description of what the tool does */
|
|
26
|
+
description?: Tool["description"];
|
|
27
|
+
/** JSON Schema defining the tool's input parameters */
|
|
28
|
+
parameters?: JSONSchema7;
|
|
29
|
+
};
|
|
30
|
+
/**
|
|
31
|
+
* Options passed to the onChatMessage handler.
|
|
32
|
+
*/
|
|
33
|
+
type OnChatMessageOptions = {
|
|
34
|
+
/** AbortSignal for cancelling the request */
|
|
35
|
+
abortSignal?: AbortSignal;
|
|
36
|
+
/**
|
|
37
|
+
* Tool schemas sent from the client for dynamic tool registration.
|
|
38
|
+
* These represent tools that will be executed on the client side.
|
|
39
|
+
* Use `createToolsFromClientSchemas()` to convert these to AI SDK tool format.
|
|
40
|
+
*/
|
|
41
|
+
clientTools?: ClientToolSchema[];
|
|
42
|
+
};
|
|
43
|
+
/**
|
|
44
|
+
* Converts client tool schemas to AI SDK tool format.
|
|
45
|
+
*
|
|
46
|
+
* These tools have no `execute` function - when the AI model calls them,
|
|
47
|
+
* the tool call is sent back to the client for execution.
|
|
48
|
+
*
|
|
49
|
+
* @param clientTools - Array of tool schemas from the client
|
|
50
|
+
* @returns Record of AI SDK tools that can be spread into your tools object
|
|
51
|
+
*/
|
|
52
|
+
declare function createToolsFromClientSchemas(
|
|
53
|
+
clientTools?: ClientToolSchema[]
|
|
54
|
+
): ToolSet;
|
|
11
55
|
/**
|
|
12
56
|
* Extension of Agent with built-in chat capabilities
|
|
13
57
|
* @template Env Environment type containing bindings
|
|
14
58
|
*/
|
|
15
|
-
declare class AIChatAgent<
|
|
16
|
-
Env,
|
|
17
|
-
State
|
|
18
|
-
> {
|
|
59
|
+
declare class AIChatAgent<
|
|
60
|
+
Env extends Cloudflare.Env = Cloudflare.Env,
|
|
61
|
+
State = unknown
|
|
62
|
+
> extends Agent<Env, State> {
|
|
19
63
|
/**
|
|
20
64
|
* Map of message `id`s to `AbortController`s
|
|
21
65
|
* useful to propagate request cancellation signals for any external calls made by the agent
|
|
@@ -32,6 +76,19 @@ declare class AIChatAgent<Env = unknown, State = unknown> extends Agent<
|
|
|
32
76
|
* @internal Protected for testing purposes.
|
|
33
77
|
*/
|
|
34
78
|
protected _activeRequestId: string | null;
|
|
79
|
+
/**
|
|
80
|
+
* The message currently being streamed. Used to apply tool results
|
|
81
|
+
* before the message is persisted.
|
|
82
|
+
* @internal
|
|
83
|
+
*/
|
|
84
|
+
private _streamingMessage;
|
|
85
|
+
/**
|
|
86
|
+
* Promise that resolves when the current stream completes.
|
|
87
|
+
* Used to wait for message persistence before continuing after tool results.
|
|
88
|
+
* @internal
|
|
89
|
+
*/
|
|
90
|
+
private _streamCompletionPromise;
|
|
91
|
+
private _streamCompletionResolve;
|
|
35
92
|
/**
|
|
36
93
|
* Current chunk index for the active stream
|
|
37
94
|
*/
|
|
@@ -112,14 +169,12 @@ declare class AIChatAgent<Env = unknown, State = unknown> extends Agent<
|
|
|
112
169
|
/**
|
|
113
170
|
* Handle incoming chat messages and generate a response
|
|
114
171
|
* @param onFinish Callback to be called when the response is finished
|
|
115
|
-
* @param options
|
|
172
|
+
* @param options Options including abort signal and client-defined tools
|
|
116
173
|
* @returns Response to send to the client or undefined
|
|
117
174
|
*/
|
|
118
175
|
onChatMessage(
|
|
119
176
|
onFinish: StreamTextOnFinishCallback<ToolSet>,
|
|
120
|
-
options?:
|
|
121
|
-
abortSignal: AbortSignal | undefined;
|
|
122
|
-
}
|
|
177
|
+
options?: OnChatMessageOptions
|
|
123
178
|
): Promise<Response | undefined>;
|
|
124
179
|
/**
|
|
125
180
|
* Save messages on the server side
|
|
@@ -130,6 +185,71 @@ declare class AIChatAgent<Env = unknown, State = unknown> extends Agent<
|
|
|
130
185
|
messages: UIMessage[],
|
|
131
186
|
excludeBroadcastIds?: string[]
|
|
132
187
|
): Promise<void>;
|
|
188
|
+
/**
|
|
189
|
+
* Merges incoming messages with existing server state.
|
|
190
|
+
* This preserves tool outputs that the server has (via _applyToolResult)
|
|
191
|
+
* but the client doesn't have yet.
|
|
192
|
+
*
|
|
193
|
+
* @param incomingMessages - Messages from the client
|
|
194
|
+
* @returns Messages with server's tool outputs preserved
|
|
195
|
+
*/
|
|
196
|
+
private _mergeIncomingWithServerState;
|
|
197
|
+
/**
|
|
198
|
+
* Resolves a message for persistence, handling tool result merging.
|
|
199
|
+
* If the message contains tool parts with output-available state, checks if there's
|
|
200
|
+
* an existing message with the same toolCallId that should be updated instead of
|
|
201
|
+
* creating a duplicate. This prevents the "Duplicate item found" error from OpenAI
|
|
202
|
+
* when client-side tool results arrive in a new request.
|
|
203
|
+
*
|
|
204
|
+
* @param message - The message to potentially merge
|
|
205
|
+
* @returns The message with the correct ID (either original or merged)
|
|
206
|
+
*/
|
|
207
|
+
private _resolveMessageForToolMerge;
|
|
208
|
+
/**
|
|
209
|
+
* Finds an existing assistant message that contains a tool part with the given toolCallId.
|
|
210
|
+
* Used to detect when a tool result should update an existing message rather than
|
|
211
|
+
* creating a new one.
|
|
212
|
+
*
|
|
213
|
+
* @param toolCallId - The tool call ID to search for
|
|
214
|
+
* @returns The existing message if found, undefined otherwise
|
|
215
|
+
*/
|
|
216
|
+
private _findMessageByToolCallId;
|
|
217
|
+
/**
|
|
218
|
+
* Sanitizes a message for persistence by removing ephemeral provider-specific
|
|
219
|
+
* data that should not be stored or sent back in subsequent requests.
|
|
220
|
+
*
|
|
221
|
+
* This handles two issues with the OpenAI Responses API:
|
|
222
|
+
*
|
|
223
|
+
* 1. **Duplicate item IDs**: The AI SDK's @ai-sdk/openai provider (v2.0.x+)
|
|
224
|
+
* defaults to using OpenAI's Responses API which assigns unique itemIds
|
|
225
|
+
* to each message part. When these IDs are persisted and sent back,
|
|
226
|
+
* OpenAI rejects them as duplicates.
|
|
227
|
+
*
|
|
228
|
+
* 2. **Empty reasoning parts**: OpenAI may return reasoning parts with empty
|
|
229
|
+
* text and encrypted content. These cause "Non-OpenAI reasoning parts are
|
|
230
|
+
* not supported" warnings when sent back via convertToModelMessages().
|
|
231
|
+
*
|
|
232
|
+
* @param message - The message to sanitize
|
|
233
|
+
* @returns A new message with ephemeral provider data removed
|
|
234
|
+
*/
|
|
235
|
+
private _sanitizeMessageForPersistence;
|
|
236
|
+
/**
|
|
237
|
+
* Helper to strip OpenAI-specific ephemeral fields from a metadata object.
|
|
238
|
+
* Removes itemId and reasoningEncryptedContent while preserving other fields.
|
|
239
|
+
*/
|
|
240
|
+
private _stripOpenAIMetadata;
|
|
241
|
+
/**
|
|
242
|
+
* Applies a tool result to an existing assistant message.
|
|
243
|
+
* This is used when the client sends CF_AGENT_TOOL_RESULT for client-side tools.
|
|
244
|
+
* The server is the source of truth, so we update the message here and broadcast
|
|
245
|
+
* the update to all clients.
|
|
246
|
+
*
|
|
247
|
+
* @param toolCallId - The tool call ID this result is for
|
|
248
|
+
* @param toolName - The name of the tool
|
|
249
|
+
* @param output - The output from the tool execution
|
|
250
|
+
* @returns true if the result was applied, false if the message was not found
|
|
251
|
+
*/
|
|
252
|
+
private _applyToolResult;
|
|
133
253
|
private _reply;
|
|
134
254
|
/**
|
|
135
255
|
* Mark a stream as errored and clean up state.
|
|
@@ -162,5 +282,10 @@ declare class AIChatAgent<Env = unknown, State = unknown> extends Agent<
|
|
|
162
282
|
destroy(): Promise<void>;
|
|
163
283
|
}
|
|
164
284
|
//#endregion
|
|
165
|
-
export {
|
|
285
|
+
export {
|
|
286
|
+
AIChatAgent,
|
|
287
|
+
ClientToolSchema,
|
|
288
|
+
OnChatMessageOptions,
|
|
289
|
+
createToolsFromClientSchemas
|
|
290
|
+
};
|
|
166
291
|
//# sourceMappingURL=ai-chat-agent.d.ts.map
|