Skip to content
Merged
4 changes: 4 additions & 0 deletions example/convex/_generated/api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@
* @module
*/

import type * as agents_approval from "../agents/approval.js";
import type * as agents_config from "../agents/config.js";
import type * as agents_fashion from "../agents/fashion.js";
import type * as agents_simple from "../agents/simple.js";
import type * as agents_story from "../agents/story.js";
import type * as agents_weather from "../agents/weather.js";
import type * as chat_approval from "../chat/approval.js";
import type * as chat_basic from "../chat/basic.js";
import type * as chat_human from "../chat/human.js";
import type * as chat_streamAbort from "../chat/streamAbort.js";
Expand Down Expand Up @@ -55,11 +57,13 @@ import type {
} from "convex/server";

declare const fullApi: ApiFromModules<{
"agents/approval": typeof agents_approval;
"agents/config": typeof agents_config;
"agents/fashion": typeof agents_fashion;
"agents/simple": typeof agents_simple;
"agents/story": typeof agents_story;
"agents/weather": typeof agents_weather;
"chat/approval": typeof chat_approval;
"chat/basic": typeof chat_basic;
"chat/human": typeof chat_human;
"chat/streamAbort": typeof chat_streamAbort;
Expand Down
58 changes: 58 additions & 0 deletions example/convex/agents/approval.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// See the docs at https://docs.convex.dev/agents/tool-approval
import { Agent, createTool, stepCountIs } from "@convex-dev/agent";
import { components } from "../_generated/api";
import { defaultConfig } from "./config";
import { z } from "zod/v4";

// Tool that always requires approval
const deleteFileTool = createTool({
description: "Delete a file from the system",
inputSchema: z.object({
filename: z.string().describe("The name of the file to delete"),
}),
needsApproval: () => true,
execute: async (_ctx, input) => {
return `Successfully deleted file: ${input.filename}`;
},
});

// Tool with conditional approval (requires approval for amounts > $100)
const transferMoneyTool = createTool({
description: "Transfer money to an account",
inputSchema: z.object({
amount: z.number().describe("The amount to transfer"),
toAccount: z.string().describe("The destination account"),
}),
needsApproval: async (_ctx, input) => {
return input.amount > 100;
},
execute: async (_ctx, input) => {
return `Transferred $${input.amount} to account ${input.toAccount}`;
},
});

// Tool that doesn't need approval
const checkBalanceTool = createTool({
description: "Check the account balance",
inputSchema: z.object({
accountId: z.string().describe("The account to check"),
}),
execute: async (_ctx, _input) => {
return `Balance: $1,234.56`;
},
});

export const approvalAgent = new Agent(components.agent, {
name: "Approval Demo Agent",
instructions:
"You are a helpful assistant that can delete files, transfer money, and check account balances. " +
"Always confirm what action you took after it completes.",
tools: {
deleteFile: deleteFileTool,
transferMoney: transferMoneyTool,
checkBalance: checkBalanceTool,
},
stopWhen: stepCountIs(5),
...defaultConfig,
callSettings: { temperature: 0 },
});
Loading
Loading