@agentis-hq/cli 0.4.0 → 0.4.2
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 +95 -10
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -463,6 +463,9 @@ async function solToUsd(sol) {
|
|
|
463
463
|
const res = await fetch(`https://api.jup.ag/price/v3?ids=${SOL_MINT}`);
|
|
464
464
|
const data = await res.json();
|
|
465
465
|
const price = data[SOL_MINT]?.usdPrice ?? 0;
|
|
466
|
+
if (!Number.isFinite(price) || price <= 0) {
|
|
467
|
+
throw new Error("Unable to determine SOL/USD price for policy enforcement");
|
|
468
|
+
}
|
|
466
469
|
cachedSolPrice = { usd: price, fetchedAt: now };
|
|
467
470
|
return sol * price;
|
|
468
471
|
}
|
|
@@ -538,7 +541,13 @@ async function sendLocalSol(nameOrId, to, amountSol, displayAmount) {
|
|
|
538
541
|
console.error(`Agent or local wallet not found: ${nameOrId}`);
|
|
539
542
|
process.exit(1);
|
|
540
543
|
}
|
|
541
|
-
|
|
544
|
+
let amountUsd;
|
|
545
|
+
try {
|
|
546
|
+
amountUsd = await solToUsd(amountSol);
|
|
547
|
+
} catch (err) {
|
|
548
|
+
console.error(err instanceof Error ? err.message : "Unable to determine SOL/USD price for policy enforcement");
|
|
549
|
+
process.exit(1);
|
|
550
|
+
}
|
|
542
551
|
try {
|
|
543
552
|
checkPolicy({ ...wallet.policy, allowedDomains: [] }, amountUsd, `solana:${to}`, wallet.spendHistory);
|
|
544
553
|
} catch (err) {
|
|
@@ -1730,6 +1739,83 @@ Recurring order cancelled for ${agent.name}`);
|
|
|
1730
1739
|
console.log("Usage: agentis <tokens|swap|portfolio|recurring> ...");
|
|
1731
1740
|
}
|
|
1732
1741
|
|
|
1742
|
+
// src/lib/command-validation.ts
|
|
1743
|
+
var commandTree = {
|
|
1744
|
+
login: null,
|
|
1745
|
+
logout: null,
|
|
1746
|
+
whoami: null,
|
|
1747
|
+
version: null,
|
|
1748
|
+
wallet: ["create", "list"],
|
|
1749
|
+
agent: ["create", "send", "balance"],
|
|
1750
|
+
fetch: null,
|
|
1751
|
+
earn: ["deposit", "withdraw", "positions", "sweep"],
|
|
1752
|
+
tokens: ["search"],
|
|
1753
|
+
swap: ["quote", "execute"],
|
|
1754
|
+
portfolio: null,
|
|
1755
|
+
recurring: ["list", "create", "cancel"],
|
|
1756
|
+
facilitator: ["create", "list", "publish"],
|
|
1757
|
+
privacy: ["status", "register", "balance", "deposit", "withdraw", "create-utxo", "scan", "claim-latest"],
|
|
1758
|
+
policy: ["get", "set", "init-onchain"]
|
|
1759
|
+
};
|
|
1760
|
+
var CliCommandError = class extends Error {
|
|
1761
|
+
constructor(message, helpCommand = "agentis --help") {
|
|
1762
|
+
super(message);
|
|
1763
|
+
this.helpCommand = helpCommand;
|
|
1764
|
+
this.name = "CliCommandError";
|
|
1765
|
+
}
|
|
1766
|
+
helpCommand;
|
|
1767
|
+
};
|
|
1768
|
+
function editDistance(left, right) {
|
|
1769
|
+
const previous = Array.from({ length: right.length + 1 }, (_, index) => index);
|
|
1770
|
+
for (let i = 1; i <= left.length; i++) {
|
|
1771
|
+
const current = [i];
|
|
1772
|
+
for (let j = 1; j <= right.length; j++) {
|
|
1773
|
+
current[j] = Math.min(
|
|
1774
|
+
current[j - 1] + 1,
|
|
1775
|
+
previous[j] + 1,
|
|
1776
|
+
previous[j - 1] + (left[i - 1] === right[j - 1] ? 0 : 1)
|
|
1777
|
+
);
|
|
1778
|
+
}
|
|
1779
|
+
previous.splice(0, previous.length, ...current);
|
|
1780
|
+
}
|
|
1781
|
+
return previous[right.length];
|
|
1782
|
+
}
|
|
1783
|
+
function closestMatch(value, choices) {
|
|
1784
|
+
const ranked = choices.map((choice) => ({ choice, distance: editDistance(value, choice) })).sort((a, b) => a.distance - b.distance);
|
|
1785
|
+
const best = ranked[0];
|
|
1786
|
+
if (!best) return null;
|
|
1787
|
+
const threshold = Math.max(1, Math.floor(Math.max(value.length, best.choice.length) / 3));
|
|
1788
|
+
return best.distance <= threshold ? best.choice : null;
|
|
1789
|
+
}
|
|
1790
|
+
function unknownMessage(kind, value, suggestion, parent) {
|
|
1791
|
+
const context = kind === "subcommand" ? ` for "${parent}"` : "";
|
|
1792
|
+
return `Unknown ${kind} "${value}"${context}.${suggestion ? ` Did you mean "${suggestion}"?` : ""}`;
|
|
1793
|
+
}
|
|
1794
|
+
function validateCommand(args2) {
|
|
1795
|
+
const command = args2[0];
|
|
1796
|
+
if (!command || command === "--version" || command === "-v" || command === "--help" || command === "-h") return;
|
|
1797
|
+
const subcommands = commandTree[command];
|
|
1798
|
+
if (subcommands === void 0) {
|
|
1799
|
+
throw new CliCommandError(
|
|
1800
|
+
unknownMessage("command", command, closestMatch(command, Object.keys(commandTree)))
|
|
1801
|
+
);
|
|
1802
|
+
}
|
|
1803
|
+
if (!subcommands) return;
|
|
1804
|
+
const subcommand = args2[1];
|
|
1805
|
+
const hasHelp = args2.includes("--help") || args2.includes("-h");
|
|
1806
|
+
if (!subcommand) {
|
|
1807
|
+
if (hasHelp) return;
|
|
1808
|
+
throw new CliCommandError(`Missing command for "${command}".`, `agentis ${command} --help`);
|
|
1809
|
+
}
|
|
1810
|
+
if (subcommand.startsWith("-") && hasHelp) return;
|
|
1811
|
+
if (!subcommands.includes(subcommand)) {
|
|
1812
|
+
throw new CliCommandError(
|
|
1813
|
+
unknownMessage("subcommand", subcommand, closestMatch(subcommand, subcommands), command),
|
|
1814
|
+
`agentis ${command} --help`
|
|
1815
|
+
);
|
|
1816
|
+
}
|
|
1817
|
+
}
|
|
1818
|
+
|
|
1733
1819
|
// src/index.ts
|
|
1734
1820
|
var args = process.argv.slice(2);
|
|
1735
1821
|
var cmd = args[0];
|
|
@@ -2155,6 +2241,7 @@ function showCommandHelp(path) {
|
|
|
2155
2241
|
console.log();
|
|
2156
2242
|
}
|
|
2157
2243
|
async function main() {
|
|
2244
|
+
validateCommand(args);
|
|
2158
2245
|
if (cmd === "--version" || cmd === "-v" || cmd === "version") {
|
|
2159
2246
|
console.log(version);
|
|
2160
2247
|
return;
|
|
@@ -2181,8 +2268,6 @@ async function main() {
|
|
|
2181
2268
|
case "list":
|
|
2182
2269
|
await walletList(args.slice(2));
|
|
2183
2270
|
break;
|
|
2184
|
-
default:
|
|
2185
|
-
console.log("Usage: agentis wallet <create|list>");
|
|
2186
2271
|
}
|
|
2187
2272
|
break;
|
|
2188
2273
|
case "agent":
|
|
@@ -2196,8 +2281,6 @@ async function main() {
|
|
|
2196
2281
|
case "balance":
|
|
2197
2282
|
await agentBalance(args[2]);
|
|
2198
2283
|
break;
|
|
2199
|
-
default:
|
|
2200
|
-
console.log("Usage: agentis agent <create|send|balance>");
|
|
2201
2284
|
}
|
|
2202
2285
|
break;
|
|
2203
2286
|
case "policy":
|
|
@@ -2211,8 +2294,6 @@ async function main() {
|
|
|
2211
2294
|
case "init-onchain":
|
|
2212
2295
|
await policyInitOnchain(args[2]);
|
|
2213
2296
|
break;
|
|
2214
|
-
default:
|
|
2215
|
-
console.log("Usage: agentis policy <get|set|init-onchain>");
|
|
2216
2297
|
}
|
|
2217
2298
|
break;
|
|
2218
2299
|
case "fetch":
|
|
@@ -2233,11 +2314,15 @@ async function main() {
|
|
|
2233
2314
|
case "recurring":
|
|
2234
2315
|
await financialCommand(args);
|
|
2235
2316
|
break;
|
|
2236
|
-
default:
|
|
2237
|
-
showHelp();
|
|
2238
2317
|
}
|
|
2239
2318
|
}
|
|
2240
2319
|
main().catch((err) => {
|
|
2241
|
-
|
|
2320
|
+
if (err instanceof CliCommandError) {
|
|
2321
|
+
console.error(`Error: ${err.message}
|
|
2322
|
+
|
|
2323
|
+
Run \`${err.helpCommand}\` for usage.`);
|
|
2324
|
+
} else {
|
|
2325
|
+
console.error("Error:", err instanceof Error ? err.message : String(err));
|
|
2326
|
+
}
|
|
2242
2327
|
process.exit(1);
|
|
2243
2328
|
});
|