|
1 | 1 | import { Command } from "commander"; |
2 | | -import { getConfigOrThrow, saveConfig, getMaxSpendPerCall } from "../config"; |
3 | | -import { output } from "../output"; |
| 2 | +import { getConfigOrThrow, saveConfig, getMaxSpendPerCall, loadConfig, getActiveProvider } from "../config"; |
| 3 | +import { output, getOutputFormat, Table, boldBlue } from "../output"; |
| 4 | +import { detectProviders, getProvider } from "../providers"; |
4 | 5 |
|
5 | 6 | export const walletCommand = new Command("wallet") |
6 | | - .description("Manage wallet settings (spend limits)") |
7 | | - .addHelpText("after", "\nExamples:\n use-agently wallet spend\n use-agently wallet spend set-max 0.5") |
| 7 | + .description("Manage wallet settings (providers, spend limits)") |
| 8 | + .addHelpText( |
| 9 | + "after", |
| 10 | + "\nExamples:\n use-agently wallet providers\n use-agently wallet set agentcash\n use-agently wallet spend\n use-agently wallet spend set-max 0.5", |
| 11 | + ) |
8 | 12 | .action(function () { |
9 | 13 | (this as Command).outputHelp(); |
10 | 14 | }); |
11 | 15 |
|
| 16 | +// ─── wallet providers ───────────────────────────────────────────────────────── |
| 17 | + |
| 18 | +const providersCommand = new Command("providers") |
| 19 | + .description("List detected wallet providers") |
| 20 | + .showHelpAfterError(true) |
| 21 | + .addHelpText("after", "\nExamples:\n use-agently wallet providers\n use-agently wallet providers -o json") |
| 22 | + .action(async (_options: unknown, command: Command) => { |
| 23 | + const config = await loadConfig(); |
| 24 | + const activeProvider = config ? getActiveProvider(config) : "local"; |
| 25 | + const detected = await detectProviders(activeProvider); |
| 26 | + const format = getOutputFormat(command); |
| 27 | + |
| 28 | + if (format === "json") { |
| 29 | + for (const p of detected) { |
| 30 | + console.log(JSON.stringify(p)); |
| 31 | + } |
| 32 | + } else { |
| 33 | + const table = new Table({ head: ["Provider", "Address", "Status"] }); |
| 34 | + for (const p of detected) { |
| 35 | + const status = p.active ? "active" : p.installed ? "installed" : "not installed"; |
| 36 | + const name = p.active ? boldBlue(p.name) : p.name; |
| 37 | + table.push([name, p.address ?? "—", status]); |
| 38 | + } |
| 39 | + console.log(table.toString()); |
| 40 | + console.log("\nSwitch provider: use-agently wallet set <provider>"); |
| 41 | + } |
| 42 | + }); |
| 43 | + |
| 44 | +// ─── wallet set ─────────────────────────────────────────────────────────────── |
| 45 | + |
| 46 | +const setCommand = new Command("set") |
| 47 | + .description("Set the active wallet provider") |
| 48 | + .argument("<provider>", 'Provider type (e.g. "agentcash", "local")') |
| 49 | + .showHelpAfterError(true) |
| 50 | + .addHelpText("after", "\nExamples:\n use-agently wallet set agentcash\n use-agently wallet set local") |
| 51 | + .action(async (providerType: string, _options: unknown, command: Command) => { |
| 52 | + const provider = getProvider(providerType); |
| 53 | + if (!provider) { |
| 54 | + const detected = await detectProviders(); |
| 55 | + const available = detected.map((p) => p.type).join(", "); |
| 56 | + throw new Error(`Unknown provider "${providerType}". Available: ${available}`); |
| 57 | + } |
| 58 | + |
| 59 | + const { installed } = await provider.detect(); |
| 60 | + if (!installed) { |
| 61 | + throw new Error(`Provider "${providerType}" (${provider.name}) is not installed.`); |
| 62 | + } |
| 63 | + |
| 64 | + let config = await loadConfig(); |
| 65 | + if (!config) { |
| 66 | + config = { wallet: { type: "none" } }; |
| 67 | + } |
| 68 | + |
| 69 | + (config.wallet as Record<string, unknown>).provider = providerType; |
| 70 | + await saveConfig(config); |
| 71 | + |
| 72 | + const { address } = await provider.detect(); |
| 73 | + output(command, { |
| 74 | + provider: providerType, |
| 75 | + address, |
| 76 | + message: `Switched to ${provider.name} wallet`, |
| 77 | + }); |
| 78 | + }); |
| 79 | + |
12 | 80 | // ─── wallet spend ──────────────────────────────────────────────────────────── |
13 | 81 |
|
14 | 82 | const spendCommand = new Command("spend") |
@@ -39,4 +107,6 @@ const setMaxCommand = new Command("set-max") |
39 | 107 | }); |
40 | 108 |
|
41 | 109 | spendCommand.addCommand(setMaxCommand); |
| 110 | +walletCommand.addCommand(providersCommand); |
| 111 | +walletCommand.addCommand(setCommand); |
42 | 112 | walletCommand.addCommand(spendCommand); |
0 commit comments