@t2000/cli 0.20.44 → 0.21.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
|
@@ -1453,67 +1453,51 @@ function isRetryable(code) {
|
|
|
1453
1453
|
// src/commands/pay.ts
|
|
1454
1454
|
import pc9 from "picocolors";
|
|
1455
1455
|
import { T2000 as T200019 } from "@t2000/sdk";
|
|
1456
|
-
import { x402Client } from "@t2000/x402";
|
|
1457
|
-
function createX402Wallet(agent) {
|
|
1458
|
-
return {
|
|
1459
|
-
client: agent.suiClient,
|
|
1460
|
-
keypair: agent.signer,
|
|
1461
|
-
address: () => agent.address(),
|
|
1462
|
-
signAndExecute: async (tx) => {
|
|
1463
|
-
const result = await agent.suiClient.signAndExecuteTransaction({
|
|
1464
|
-
signer: agent.signer,
|
|
1465
|
-
transaction: tx
|
|
1466
|
-
});
|
|
1467
|
-
return { digest: result.digest };
|
|
1468
|
-
}
|
|
1469
|
-
};
|
|
1470
|
-
}
|
|
1471
1456
|
function registerPay(program2) {
|
|
1472
|
-
program2.command("pay <url>").description("Pay for an
|
|
1457
|
+
program2.command("pay <url>").description("Pay for an MPP-protected API resource").option("--key <path>", "Key file path").option("--method <method>", "HTTP method (GET, POST, PUT)", "GET").option("--data <json>", "Request body for POST/PUT").option("--header <key=value>", "Additional HTTP header (repeatable)", collectHeaders, {}).option("--max-price <amount>", "Max USDC price to auto-approve", "1.00").action(async (url, opts) => {
|
|
1473
1458
|
try {
|
|
1474
1459
|
const pin = await resolvePin();
|
|
1475
1460
|
const agent = await T200019.create({ pin, keyPath: opts.key });
|
|
1476
|
-
agent.enforcer.check({ operation: "pay", amount: parseFloat(opts.maxPrice) });
|
|
1477
|
-
const wallet = createX402Wallet(agent);
|
|
1478
|
-
const client = new x402Client(wallet);
|
|
1479
1461
|
const startTime = Date.now();
|
|
1462
|
+
const method = opts.data && opts.method === "GET" ? "POST" : opts.method;
|
|
1480
1463
|
if (!isJsonMode()) {
|
|
1481
1464
|
printBlank();
|
|
1482
|
-
printInfo(`\u2192 ${
|
|
1465
|
+
printInfo(`\u2192 ${method} ${url}`);
|
|
1483
1466
|
}
|
|
1484
|
-
const
|
|
1485
|
-
|
|
1467
|
+
const maxPrice = parseFloat(opts.maxPrice);
|
|
1468
|
+
if (isNaN(maxPrice) || maxPrice <= 0) {
|
|
1469
|
+
throw new Error(`Invalid --max-price: "${opts.maxPrice}". Must be a positive number.`);
|
|
1470
|
+
}
|
|
1471
|
+
const result = await agent.pay({
|
|
1472
|
+
url,
|
|
1473
|
+
method,
|
|
1486
1474
|
headers: opts.header,
|
|
1487
1475
|
body: opts.data,
|
|
1488
|
-
maxPrice
|
|
1489
|
-
timeout: parseInt(opts.timeout, 10) * 1e3,
|
|
1490
|
-
dryRun: opts.dryRun,
|
|
1491
|
-
onPayment: (details) => {
|
|
1492
|
-
if (!isJsonMode()) {
|
|
1493
|
-
printInfo(`\u2190 402 Payment Required: $${details.amount} USDC (Sui)`);
|
|
1494
|
-
printSuccess(`Paid $${details.amount} USDC (tx: ${details.txHash.slice(0, 10)}...)`);
|
|
1495
|
-
}
|
|
1496
|
-
}
|
|
1476
|
+
maxPrice
|
|
1497
1477
|
});
|
|
1498
1478
|
const elapsed = Date.now() - startTime;
|
|
1499
1479
|
if (!isJsonMode()) {
|
|
1500
|
-
|
|
1480
|
+
if (result.paid && result.receipt) {
|
|
1481
|
+
printSuccess(`Paid via MPP (tx: ${result.receipt.reference.slice(0, 10)}...)`);
|
|
1482
|
+
}
|
|
1483
|
+
printInfo(`\u2190 ${result.status} OK ${pc9.dim(`[${elapsed}ms]`)}`);
|
|
1501
1484
|
}
|
|
1502
|
-
const contentType = response.headers.get("content-type") ?? "";
|
|
1503
|
-
const body = contentType.includes("application/json") ? await response.json() : await response.text();
|
|
1504
1485
|
if (isJsonMode()) {
|
|
1505
1486
|
printJson({
|
|
1506
|
-
status:
|
|
1487
|
+
status: result.status,
|
|
1507
1488
|
url,
|
|
1508
1489
|
elapsed,
|
|
1509
|
-
|
|
1490
|
+
paid: result.paid,
|
|
1491
|
+
cost: result.cost,
|
|
1492
|
+
receipt: result.receipt,
|
|
1493
|
+
body: result.body
|
|
1510
1494
|
});
|
|
1511
1495
|
} else {
|
|
1512
1496
|
printBlank();
|
|
1513
|
-
if (typeof body === "string") {
|
|
1514
|
-
console.log(body);
|
|
1497
|
+
if (typeof result.body === "string") {
|
|
1498
|
+
console.log(result.body);
|
|
1515
1499
|
} else {
|
|
1516
|
-
console.log(JSON.stringify(body, null, 2));
|
|
1500
|
+
console.log(JSON.stringify(result.body, null, 2));
|
|
1517
1501
|
}
|
|
1518
1502
|
printBlank();
|
|
1519
1503
|
}
|
|
@@ -2053,7 +2037,7 @@ function registerMcp(program2) {
|
|
|
2053
2037
|
mcp.command("start", { isDefault: true }).description("Start MCP server (stdio transport)").option("--key <path>", "Key file path").action(async (opts) => {
|
|
2054
2038
|
let mod;
|
|
2055
2039
|
try {
|
|
2056
|
-
mod = await import("./dist-
|
|
2040
|
+
mod = await import("./dist-7OOZ6NXP.js");
|
|
2057
2041
|
} catch {
|
|
2058
2042
|
console.error(
|
|
2059
2043
|
"MCP server not installed. Run:\n npm install -g @t2000/mcp"
|