Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1003,7 +1003,7 @@ tools: {
processPayment: tool({
description: "Process a payment",
inputSchema: z.object({
amount: z.number(),
amount: z.coerce.number(),
recipient: z.string(),
}),
needsApproval: async ({ amount }) => amount > 100,
Expand Down
22 changes: 9 additions & 13 deletions src/content/docs/agents/examples/chat-agent.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@ export class ChatAgent extends AIChatAgent {
"Perform a math calculation with two numbers. " +
"Requires user approval for large numbers.",
inputSchema: z.object({
a: z.number().describe("First number"),
b: z.number().describe("Second number"),
a: z.coerce.number().describe("First number"),
b: z.coerce.number().describe("Second number"),
operator: z
.enum(["+", "-", "*", "/", "%"])
.describe("Arithmetic operator"),
Expand Down Expand Up @@ -191,7 +191,7 @@ Create `src/client.tsx`:

```ts
import { useAgent } from "agents/react";
import { useAgentChat } from "@cloudflare/ai-chat/react";
import { useAgentChat, getToolApproval } from "@cloudflare/ai-chat/react";

function Chat() {
const agent = useAgent({ agent: "ChatAgent" });
Expand Down Expand Up @@ -225,10 +225,9 @@ function Chat() {
}

// Render approval UI for tools that need confirmation
if (
part.type === "tool" &&
part.state === "approval-required"
) {
if (part.state === "approval-requested") {
const approval = getToolApproval(part);
if (!approval) return null;
return (
<div key={part.toolCallId}>
<p>
Expand All @@ -238,7 +237,7 @@ function Chat() {
<button
onClick={() =>
addToolApprovalResponse({
id: part.toolCallId,
id: approval.id,
approved: true,
})
}
Expand All @@ -248,7 +247,7 @@ function Chat() {
<button
onClick={() =>
addToolApprovalResponse({
id: part.toolCallId,
id: approval.id,
approved: false,
})
}
Expand All @@ -260,10 +259,7 @@ function Chat() {
}

// Show completed tool results
if (
part.type === "tool" &&
part.state === "output-available"
) {
if (part.state === "output-available") {
return (
<details key={part.toolCallId}>
<summary>{part.toolName} result</summary>
Expand Down