@moneytools-dev/ai-sdk 0.1.0 → 0.2.1

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/dist/index.js CHANGED
@@ -1,9 +1,17 @@
1
1
  import { tool } from "ai";
2
2
  import { z } from "zod";
3
3
  import { TranscriptsGateway, SettleClient } from "@moneytools-dev/transcripts";
4
+ import { wrapFetchWithPayment, x402Client } from "@x402/fetch";
5
+ import { registerExactEvmScheme } from "@x402/evm/exact/client";
6
+ export const TOOLS_URL = "https://tools.moneytools.dev";
7
+ export const PRECHECK_URL = "https://api.moneytools.dev";
8
+ const BASE_MAINNET_CAIP2 = "eip155:8453";
9
+ const USDC_BASE = "0x833589fcd6edb6e08f4c7c32d4f71b54bda02913";
4
10
  /**
5
11
  * MoneyTools tools for the Vercel AI SDK (`generateText({ tools: moneytoolsTools({ account }) })`).
6
- * Transcripts: 0.002 USDC per started audio minute. Settle: 0.002–0.003 USDC per call. Paid via x402 on Base.
12
+ * Payment safety: precheck and token checks at 0.005 USDC, wallet risk card at 0.02 USDC.
13
+ * Transcripts: 0.005 USDC per started audio minute. Settle: 0.005 USDC per call.
14
+ * Paid via x402 on Base.
7
15
  */
8
16
  export function moneytoolsTools(o) {
9
17
  const gw = new TranscriptsGateway({ account: o.account, maxUsdPerRequest: o.maxUsdPerRequest, gatewayUrl: o.transcriptsUrl, fetch: o.fetch });
@@ -15,9 +23,46 @@ export function moneytoolsTools(o) {
15
23
  catch (e) {
16
24
  return { error: e?.message ?? String(e), status: e?.status };
17
25
  } };
26
+ const toolsUrl = (o.toolsUrl ?? TOOLS_URL).replace(/\/+$/, "");
27
+ const precheckUrl = (o.precheckUrl ?? PRECHECK_URL).replace(/\/+$/, "");
28
+ const maxPaymentUsdc = o.maxPaymentUsdc ?? Math.min(o.maxUsdPerRequest ?? 0.02, 0.02);
29
+ const safety = (path, query, freeTier = false) => paidGet(o.account, `${path.includes("precheck") ? precheckUrl : toolsUrl}${path}?${new URLSearchParams(query).toString()}`, maxPaymentUsdc, freeTier);
18
30
  return {
31
+ precheck: tool({
32
+ description: "Check an x402 seller URL before paying (402 terms and OFAC screen of payTo). Free daily tier, then 0.005 USDC on Base over x402. Returns a verdict (allow, warn, block).",
33
+ inputSchema: z.object({
34
+ url: z.string().url().describe("Seller URL the agent is about to pay"),
35
+ method: z.string().optional().describe("HTTP method the agent will use (default GET)"),
36
+ }),
37
+ execute: async ({ url, method }) => wrap(() => safety("/v1/precheck", { url, method: (method || "GET").toUpperCase() })),
38
+ }),
39
+ walletRiskCard: tool({
40
+ description: "Score an EVM wallet before paying, trusting or interacting with it. Returns a 0-100 risk score, A-F grade, OFAC SDN match, address checks and open ERC-20 approvals. Costs 0.02 USDC on Base over x402. Read-only.",
41
+ inputSchema: z.object({
42
+ address: z.string().describe("0x address or ENS name"),
43
+ chain: z.enum(["base", "ethereum", "arbitrum", "optimism", "polygon", "bsc", "avalanche"]).optional().describe("Chain to read (default base). Payment always settles in USDC on Base."),
44
+ }),
45
+ execute: async ({ address, chain }) => wrap(() => safety("/v1/wallet-risk", { address, chain: chain ?? "base" })),
46
+ }),
47
+ tokenSafetyCheck: tool({
48
+ description: "Check an ERC-20 before buying it: USDC -> token -> USDC round-trip simulation plus owner / proxy / paused flags (honeypot, sell tax). Free daily tier, then 0.005 USDC on Base over x402.",
49
+ inputSchema: z.object({
50
+ token: z.string().regex(/^0x[a-fA-F0-9]{40}$/).describe("ERC-20 address"),
51
+ chain: z.enum(["base", "ethereum", "arbitrum", "optimism", "polygon", "bsc", "avalanche"]).optional(),
52
+ usd: z.number().positive().max(10000).optional().describe("Notional USD size (default 10)"),
53
+ }),
54
+ execute: async ({ token, chain, usd }) => wrap(() => safety("/v1/token-safety", { token, chain: chain ?? "base", usd: String(usd ?? 10) }, true)),
55
+ }),
56
+ tokenContractInfo: tool({
57
+ description: "Read ERC-20 facts: name, symbol, decimals, supply, owner, paused, upgradeable proxy. Free daily tier, then 0.005 USDC on Base over x402.",
58
+ inputSchema: z.object({
59
+ token: z.string().regex(/^0x[a-fA-F0-9]{40}$/).describe("ERC-20 address"),
60
+ chain: z.enum(["base", "ethereum", "arbitrum", "optimism", "polygon", "bsc", "avalanche"]).optional(),
61
+ }),
62
+ execute: async ({ token, chain }) => wrap(() => safety("/v1/token", { token, chain: chain ?? "base" }, true)),
63
+ }),
19
64
  transcribeAudio: tool({
20
- description: "Transcribe speech in a public audio/video URL (mp3, wav, m4a, ogg, webm, flac, mp4; up to 3 h). Costs 0.002 USDC per started minute, paid automatically via x402. Returns text, language and timestamped segments, or a jobId to check later with getTranscription.",
65
+ description: "Transcribe speech in a public audio/video URL (mp3, wav, m4a, ogg, webm, flac, mp4; up to 3 h). Costs 0.005 USDC per started minute, paid automatically via x402. Returns text, language and timestamped segments, or a jobId to check later with getTranscription.",
21
66
  inputSchema: z.object({
22
67
  url: z.string().url().describe("https URL of the audio or video file"),
23
68
  language: z.string().optional().describe("ISO-639-1 code, e.g. 'en'; omit to auto-detect"),
@@ -42,20 +87,76 @@ export function moneytoolsTools(o) {
42
87
  execute: async ({ jobId, format }) => wrap(async () => (format && format !== "json" ? { jobId, format, content: await gw.getFileTranscriptionText(jobId, format) } : gw.getFileTranscription(jobId))),
43
88
  }),
44
89
  spendCheck: tool({
45
- description: "Before paying an x402 402, check the payment against this wallet's spend caps (per call / per day / allowed hosts). Costs 0.002 USDC. Returns decision allow|deny with reasons.",
90
+ description: "Before paying an x402 402, check the payment against this wallet's spend caps (per call / per day / allowed hosts). Costs 0.005 USDC. Returns decision allow|deny with reasons.",
46
91
  inputSchema: z.object({ amountUsd: z.string().describe("e.g. '0.25'"), host: z.string().describe("host asking for payment"), payTo: z.string().optional() }),
47
92
  execute: async (a) => wrap(() => settle.spendCheck({ payer, ...a })),
48
93
  }),
49
94
  setSpendCap: tool({
50
- description: "Set spend caps for this wallet (per call, per UTC day, allowed hosts). Costs 0.002 USDC.",
95
+ description: "Set spend caps for this wallet (per call, per UTC day, allowed hosts). Costs 0.005 USDC.",
51
96
  inputSchema: z.object({ perCallUsd: z.string().optional(), perDayUsd: z.string().optional(), allowHosts: z.array(z.string()).optional() }),
52
97
  execute: async (a) => wrap(() => settle.spendCapSet({ payer, ...a })),
53
98
  }),
54
99
  verifyPayment: tool({
55
- description: "Verify an x402 USDC payment on Base by tx hash or authorization nonce (who paid whom, how much, settled or not). Costs 0.003 USDC.",
100
+ description: "Verify an x402 USDC payment on Base by tx hash or authorization nonce (who paid whom, how much, settled or not). Costs 0.005 USDC.",
56
101
  inputSchema: z.object({ tx: z.string().optional(), nonce: z.string().optional() }),
57
102
  execute: async (a) => (a.tx || a.nonce ? wrap(() => settle.verify(a)) : { error: "provide tx or nonce" }),
58
103
  }),
59
104
  };
60
105
  }
106
+ async function paidGet(account, url, maxPaymentUsdc, freeTier = false) {
107
+ const headers = { Accept: "application/json" };
108
+ if (freeTier)
109
+ headers["X-MoneyTools-Free"] = "1";
110
+ const first = await fetch(url, { headers });
111
+ if (first.status !== 402)
112
+ return formatSafety(first);
113
+ const terms = readTerms(first, await first.json().catch(() => ({})));
114
+ const refusal = checkTerms(terms, maxPaymentUsdc);
115
+ if (refusal)
116
+ return { success: false, paymentRequired: true, terms, error: refusal };
117
+ const client = new x402Client();
118
+ registerExactEvmScheme(client, { signer: account });
119
+ const paidFetch = wrapFetchWithPayment(fetch, client);
120
+ return formatSafety(await paidFetch(url, { headers: { Accept: "application/json" } }));
121
+ }
122
+ function readTerms(res, body) {
123
+ let req = body;
124
+ const header = res.headers.get("payment-required");
125
+ if (header) {
126
+ try {
127
+ req = JSON.parse(Buffer.from(header, "base64").toString("utf8"));
128
+ }
129
+ catch {
130
+ // fall back to the JSON body
131
+ }
132
+ }
133
+ const a = req?.accepts?.[0];
134
+ if (!a)
135
+ return undefined;
136
+ return {
137
+ amount: a.amount ?? a.maxAmountRequired,
138
+ network: a.network,
139
+ asset: a.asset,
140
+ payTo: a.payTo,
141
+ };
142
+ }
143
+ function checkTerms(terms, maxPaymentUsdc) {
144
+ if (!terms)
145
+ return "could not read the x402 payment terms";
146
+ if (terms.network !== BASE_MAINNET_CAIP2 || terms.asset?.toLowerCase() !== USDC_BASE) {
147
+ return "quote is not USDC on Base; not paying";
148
+ }
149
+ const usdc = Number(terms.amount) / 1e6;
150
+ if (!Number.isFinite(usdc) || usdc > maxPaymentUsdc) {
151
+ return `quoted ${usdc} USDC exceeds maxPaymentUsdc ${maxPaymentUsdc}; not paying`;
152
+ }
153
+ return undefined;
154
+ }
155
+ async function formatSafety(res) {
156
+ const data = (await res.json().catch(() => ({})));
157
+ delete data._sponsored;
158
+ if (res.ok)
159
+ return { success: true, data };
160
+ return { success: false, status: res.status, error: data.error ?? `HTTP ${res.status}`, details: data };
161
+ }
61
162
  export default moneytoolsTools;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@moneytools-dev/ai-sdk",
3
- "version": "0.1.0",
4
- "description": "Vercel AI SDK tools for MoneyTools: pay-per-file speech-to-text (Transcripts) and x402 spend controls (Settle). x402 USDC on Base, no API keys.",
3
+ "version": "0.2.1",
4
+ "description": "Vercel AI SDK tools for MoneyTools: payment-safety (precheck, wallet risk card, token checks), pay-per-file speech-to-text (Transcripts), and x402 spend controls (Settle). x402 USDC on Base, no API keys.",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
7
7
  "types": "dist/index.d.ts",
@@ -38,7 +38,9 @@
38
38
  "zod": ">=3.25"
39
39
  },
40
40
  "dependencies": {
41
- "@moneytools-dev/transcripts": "^0.1.0"
41
+ "@moneytools-dev/transcripts": "^0.1.2",
42
+ "@x402/evm": "^2.31.0",
43
+ "@x402/fetch": "^2.31.0"
42
44
  },
43
45
  "devDependencies": {
44
46
  "@types/node": "^26.6.6",