-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathcheck-budget.ts
More file actions
39 lines (37 loc) · 1.23 KB
/
check-budget.ts
File metadata and controls
39 lines (37 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import { tool } from "ai";
import { z } from "zod";
import { getBudgetState } from "../budget-state";
/**
* Check remaining session budget and spending breakdown.
*
* In production, this calls agentpay-mcp's `get_spending_report` MCP tool.
* This example uses an in-memory budget state for demonstration.
*
* @see https://github.com/up2itnow0822/agentpay-mcp
*/
export default tool({
description: "Check remaining session budget and spending breakdown by category",
parameters: z.object({}),
execute: async () => {
const budget = getBudgetState();
const remaining = budget.sessionCap - budget.spent;
return {
sessionCap: budget.sessionCap.toString(),
spent: budget.spent.toString(),
remaining: remaining.toString(),
perCallLimit: budget.perCallLimit.toString(),
callsMade: budget.callCount,
callsBlocked: budget.blockedCount,
byCategory: Object.fromEntries(
Object.entries(budget.categorySpent).map(([k, v]) => [
k,
{
spent: v.toString(),
cap: (budget.categoryCaps[k] ?? budget.sessionCap).toString(),
remaining: ((budget.categoryCaps[k] ?? budget.sessionCap) - v).toString(),
},
])
),
};
},
});