@thirdfy/agent-cli 0.3.0 → 0.4.0

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.
Files changed (64) hide show
  1. package/CHANGELOG.md +110 -0
  2. package/README.md +60 -18
  3. package/package.json +2 -2
  4. package/scripts/validate-cli-contract-schemas.mjs +5 -0
  5. package/src/cli/errors.mjs +58 -8
  6. package/src/cli/manifest.mjs +54 -5
  7. package/src/cli/options/bootstrap.mjs +15 -3
  8. package/src/cli/options/chains.mjs +5 -0
  9. package/src/cli/options/credits.mjs +9 -0
  10. package/src/cli/options/data.mjs +13 -1
  11. package/src/cli/options/index.mjs +10 -4
  12. package/src/cli/options/walletControl.mjs +8 -0
  13. package/src/commands/bootstrap.mjs +44 -5
  14. package/src/commands/chat.mjs +3 -0
  15. package/src/commands/credits.mjs +39 -4
  16. package/src/commands/delegation.mjs +3 -1
  17. package/src/commands/discovery.mjs +156 -13
  18. package/src/commands/doctor.mjs +5 -2
  19. package/src/commands/execute.mjs +96 -3
  20. package/src/commands/prompt.mjs +5 -3
  21. package/src/commands/wallet.mjs +119 -3
  22. package/src/commands/walletControl.mjs +66 -0
  23. package/src/contracts/cli/schemas/actions.response.schema.json +45 -0
  24. package/src/contracts/cli/schemas/chains-non-evm.response.schema.json +76 -0
  25. package/src/contracts/cli/schemas/chains.response.schema.json +151 -0
  26. package/src/contracts/cli/schemas/error.response.schema.json +61 -0
  27. package/src/contracts/cli/schemas/preview.response.schema.json +179 -0
  28. package/src/contracts/cli/schemas/readiness.response.schema.json +94 -0
  29. package/src/core/args.mjs +10 -2
  30. package/src/core/chainRegistry.mjs +223 -1
  31. package/src/core/context.mjs +37 -14
  32. package/src/core/errorEnvelope.mjs +209 -0
  33. package/src/core/fundingTokenIn.mjs +24 -1
  34. package/src/core/http.mjs +6 -1
  35. package/src/core/readOnlyActions.mjs +10 -0
  36. package/src/core/walletExecuteChainId.mjs +25 -0
  37. package/src/lib/creditsUsdSummary.mjs +48 -4
  38. package/src/runtime/actions/actionList.mjs +87 -0
  39. package/src/runtime/actions/selection.mjs +17 -22
  40. package/src/runtime/execution/actionPreflight.mjs +50 -14
  41. package/src/runtime/execution/payloads.mjs +36 -28
  42. package/src/runtime/execution/runners.mjs +75 -21
  43. package/src/runtime/execution/svmLane.mjs +106 -0
  44. package/src/runtime/execution/walletControl.mjs +104 -0
  45. package/src/runtime/handlers.mjs +16 -2
  46. package/src/runtime/index.mjs +1 -1
  47. package/src/runtime/main.mjs +21 -5
  48. package/src/runtime/providerHints/apps.mjs +36 -0
  49. package/src/runtime/providerHints/catalogHint.mjs +55 -0
  50. package/src/runtime/providerHints/cex.mjs +130 -0
  51. package/src/runtime/providerHints/data.mjs +144 -0
  52. package/src/runtime/providerHints/dispatch.mjs +177 -0
  53. package/src/runtime/providerHints/dogeos.mjs +38 -0
  54. package/src/runtime/providerHints/earn.mjs +201 -0
  55. package/src/runtime/providerHints/perps.mjs +346 -0
  56. package/src/runtime/providerHints/prediction.mjs +128 -0
  57. package/src/runtime/providerHints/solana.mjs +85 -0
  58. package/src/runtime/providerHints/trading.mjs +78 -0
  59. package/src/runtime/providerHints/walletControl.mjs +39 -0
  60. package/src/runtime/risk/notional.mjs +59 -0
  61. package/src/runtime/risk/orderPreview.mjs +154 -0
  62. package/src/runtime/risk/riskRefusal.mjs +74 -0
  63. package/src/runtime/validation/actionParamsGate.mjs +2 -0
  64. package/src/runtime/providerHints.mjs +0 -1017
@@ -13,3 +13,12 @@ export function attachCreditsOptions(cmd) {
13
13
  )
14
14
  .option('--surface <surface>', 'billing surface (console, automation, cli, agent_api)', 'cli');
15
15
  }
16
+
17
+ /** `credits buy` only: no catalog or pricing flags, and no --action / --params to override. */
18
+ export function attachCreditsBuyOptions(cmd) {
19
+ return cmd
20
+ .option('--usd <dollars>', 'whole US dollars of credits to buy with USDC from the agent wallet')
21
+ .option('--tx-hash <hash>', 'resume a purchase whose USDC payment was already sent (after WRITE_OUTCOME_UNKNOWN)')
22
+ .option('--chain-id <id>', 'chain the USDC is paid on (default 8453 Base; 84532 Base Sepolia on test APIs)')
23
+ .option('--idempotency-key <key>', 'idempotency key for this purchase');
24
+ }
@@ -1,4 +1,4 @@
1
- /** Options of the data commands: `signals`, and the data-category filter of `actions`. */
1
+ /** Options of the discovery and data commands: `actions` filters and `signals`. */
2
2
 
3
3
  export function attachSignalsOptions(cmd) {
4
4
  return cmd
@@ -12,3 +12,15 @@ export function attachDataCategoryOptions(cmd) {
12
12
  'only data reads of this category (market_data, signal, research, wallet); comma-separated for several',
13
13
  );
14
14
  }
15
+
16
+ /** `actions` filters. It lists actions only, so it takes none of the `run` / `preflight` options. */
17
+ export function attachActionListOptions(cmd) {
18
+ return cmd
19
+ .option('--provider <id>', 'only this provider (hyperliquid, polymarket, earn, ...)')
20
+ .option('--chain-id <id>', 'only actions on this chain')
21
+ .option('--catalog <id>', 'only this actions catalog')
22
+ .option('--search <text>', 'only actions whose name, description or aliases contain this text (one page)')
23
+ .option('--limit <n>', 'page size (concise: 50 by default, at most 100; detailed: 10, at most 25)')
24
+ .option('--offset <n>', 'skip this many matching actions (default 0)')
25
+ .option('--view <view>', 'concise (default with paging) or detailed (full entries with paramsSchema)');
26
+ }
@@ -4,20 +4,21 @@ import { attachExecutionOptions } from './execution.mjs';
4
4
  import { attachDelegationOptions } from './delegation.mjs';
5
5
  import { attachTrackingOptions } from './tracking.mjs';
6
6
  import { attachWalletOptions } from './wallet.mjs';
7
- import { attachBootstrapOptions } from './bootstrap.mjs';
7
+ import { attachBootstrapOptions, attachOnboardingHelpOptions } from './bootstrap.mjs';
8
8
  import { attachCredentialsOptions } from './credentials.mjs';
9
9
  import { attachAnalyticsOptions } from './analytics.mjs';
10
10
  import { attachPolymarketOptions } from './polymarket.mjs';
11
11
  import { attachHummingbotOptions } from './hummingbot.mjs';
12
12
  import { attachCondorOptions } from './condor.mjs';
13
13
  import { attachAgentRegisterOptions } from './agent.mjs';
14
- import { attachChainOptions } from './chains.mjs';
15
- import { attachCreditsOptions } from './credits.mjs';
14
+ import { attachChainListOptions, attachChainOptions } from './chains.mjs';
15
+ import { attachCreditsBuyOptions, attachCreditsOptions } from './credits.mjs';
16
16
  import { attachChatOptions } from './chat.mjs';
17
17
  import { attachVenueOptions } from './venue.mjs';
18
18
  import { attachBrainOptions } from './brain.mjs';
19
19
  import { attachCertificationOptions } from './certification.mjs';
20
- import { attachDataCategoryOptions, attachSignalsOptions } from './data.mjs';
20
+ import { attachActionListOptions, attachDataCategoryOptions, attachSignalsOptions } from './data.mjs';
21
+ import { attachWalletControlOptions } from './walletControl.mjs';
21
22
 
22
23
  const BUNDLES = {
23
24
  auth: attachAuthOptions,
@@ -26,6 +27,7 @@ const BUNDLES = {
26
27
  tracking: attachTrackingOptions,
27
28
  wallet: attachWalletOptions,
28
29
  bootstrap: attachBootstrapOptions,
30
+ onboardingHelp: attachOnboardingHelpOptions,
29
31
  credentials: attachCredentialsOptions,
30
32
  analytics: attachAnalyticsOptions,
31
33
  polymarket: attachPolymarketOptions,
@@ -33,13 +35,17 @@ const BUNDLES = {
33
35
  condor: attachCondorOptions,
34
36
  agent: attachAgentRegisterOptions,
35
37
  chains: attachChainOptions,
38
+ chainList: attachChainListOptions,
36
39
  credits: attachCreditsOptions,
40
+ creditsBuy: attachCreditsBuyOptions,
37
41
  chat: attachChatOptions,
38
42
  venue: attachVenueOptions,
39
43
  brain: attachBrainOptions,
40
44
  certification: attachCertificationOptions,
41
45
  signals: attachSignalsOptions,
42
46
  dataCategory: attachDataCategoryOptions,
47
+ actionList: attachActionListOptions,
48
+ walletControl: attachWalletControlOptions,
43
49
  };
44
50
 
45
51
  /** @param {import('commander').Command} cmd */
@@ -0,0 +1,8 @@
1
+ /** Options of `wallet-control request|submit` (owner actions on a user-owned agent wallet). */
2
+ export function attachWalletControlOptions(cmd) {
3
+ return cmd
4
+ .option('--action <action>', 'owner action: update_policy, stop_agent, resume_agent, change_payout, export')
5
+ .option('--payout-address <addr>', 'new owner payout address (change_payout)')
6
+ .option('--recipient-public-key <key>', 'HPKE recipient public key (export)')
7
+ .option('--signature-input <json>', 'the signatureInput of the request the owner signed (submit)');
8
+ }
@@ -1,4 +1,4 @@
1
- import { requireFlag, isNonInteractive } from '../core/args.mjs';
1
+ import { requireFlag, isNonInteractive, usageError } from '../core/args.mjs';
2
2
  import { createCliError } from '../core/envelope.mjs';
3
3
  import { PROFILE_DEFAULTS } from '../core/constants.mjs';
4
4
  import {
@@ -16,6 +16,7 @@ import {
16
16
  getProfileConfigPath,
17
17
  } from '../core/context.mjs';
18
18
  import { apiGet, apiPost, requestWithRouteFallback } from '../core/http.mjs';
19
+ import { isEvmAddress, isSolanaAddress } from '../core/chainRegistry.mjs';
19
20
  import { createRequire } from 'module';
20
21
 
21
22
  const require = createRequire(import.meta.url);
@@ -153,12 +154,24 @@ async function commandConfigSet(ctx, flags) {
153
154
  }
154
155
 
155
156
 
157
+ const ONBOARDING_PATHS = ['email_otp', 'wallet_sign', 'existing_credentials', 'mcp_bootstrap'];
158
+
159
+ /** `help onboarding [--path <id>]`: every path, or one with `selectedPath` (thirdfy-mcp `describeOnboardingPath`). */
156
160
  async function commandHelpOnboarding(ctx, flags) {
161
+ const help = buildOnboardingHelp(ctx, flags);
162
+ const selectedPath = String(flags.path || 'all').trim().toLowerCase();
163
+ if (selectedPath !== 'all' && !ONBOARDING_PATHS.includes(selectedPath)) {
164
+ throw usageError(`--path must be one of ${ONBOARDING_PATHS.join(', ')} or all`, 'path');
165
+ }
166
+ const data =
167
+ selectedPath === 'all'
168
+ ? { selectedPath, ...help }
169
+ : { selectedPath, ...help.paths.find((entry) => entry.id === selectedPath), nonInteractive: help.nonInteractive, gas: help.gas, apiBase: help.apiBase };
157
170
  printEnvelope({
158
171
  success: true,
159
172
  code: 'ONBOARDING_HELP_OK',
160
- message: 'Thirdfy onboarding help',
161
- data: buildOnboardingHelp(ctx, flags),
173
+ message: selectedPath === 'all' ? 'Thirdfy onboarding help' : `Thirdfy onboarding help: ${selectedPath}`,
174
+ data,
162
175
  meta: { apiBase: ctx.apiBase },
163
176
  });
164
177
  }
@@ -289,6 +302,16 @@ async function commandLogin(ctx, flags) {
289
302
 
290
303
  let bootstrapAgentApiKey = '';
291
304
  let bootstrapPerformed = false;
305
+ // API 4.0: where exits from a wallet under wallet control may pay, and the agent's action
306
+ // template. The API validates both and answers ownerPayout / actionTemplate (thirdfy-mcp
307
+ // bootstrapSelfIdentity parity).
308
+ const ownerPayoutAddress = String(flags.ownerPayoutAddress || '').trim();
309
+ const allowedActions = String(flags.allowedActions || '')
310
+ .split(',')
311
+ .map((entry) => entry.trim())
312
+ .filter(Boolean);
313
+ let ownerPayout;
314
+ let actionTemplate;
292
315
  const requestedAgentApiKey = String(flags.agentApiKey || '').trim();
293
316
  const bootstrapAgentKey = String(flags.agentKey || '').trim();
294
317
  if (!requestedAgentApiKey && bootstrapAgentKey) {
@@ -308,11 +331,15 @@ async function commandLogin(ctx, flags) {
308
331
  custodyMode,
309
332
  ...(flags.walletAddress ? { walletAddress: String(flags.walletAddress).trim() } : {}),
310
333
  rotate: parseBooleanFlag(flags.rotateAgentKey, false),
334
+ ...(ownerPayoutAddress ? { ownerPayoutAddress } : {}),
335
+ ...(allowedActions.length ? { allowedActions } : {}),
311
336
  },
312
337
  });
313
338
  bootstrapAgentApiKey = String(
314
339
  bootstrapResponse?.agentApiKey || bootstrapResponse?.data?.agentApiKey || ''
315
340
  ).trim();
341
+ ownerPayout = bootstrapResponse?.ownerPayout || bootstrapResponse?.data?.ownerPayout;
342
+ actionTemplate = bootstrapResponse?.actionTemplate || bootstrapResponse?.data?.actionTemplate;
316
343
  bootstrapPerformed = true;
317
344
  }
318
345
  }
@@ -355,6 +382,8 @@ async function commandLogin(ctx, flags) {
355
382
  hasOwnerSessionToken: Boolean(ownerSessionToken),
356
383
  hasAgentApiKey: Boolean(requestedAgentApiKey || bootstrapAgentApiKey),
357
384
  bootstrapPerformed,
385
+ ...(ownerPayout ? { ownerPayout } : {}),
386
+ ...(actionTemplate ? { actionTemplate } : {}),
358
387
  runMode: loginRunMode,
359
388
  custodyMode,
360
389
  configPath: getProfileConfigPath(),
@@ -499,12 +528,15 @@ async function commandLoginEmail(ctx, flags) {
499
528
  // Persist first so operators keep session tokens even when wallet provisioning failed.
500
529
  persistProfileConfig(next);
501
530
 
531
+ // An EVM (0x) or Solana (base58) execution wallet counts; an agent may have only a Solana one.
502
532
  const executionWalletAddresses = Object.values(executionWallets)
503
533
  .map((row) => (row && typeof row === 'object' ? String(row.walletAddress || '').trim() : ''))
504
- .filter((addr) => /^0x[a-fA-F0-9]{40}$/.test(addr));
534
+ .filter((addr) => isEvmAddress(addr) || isSolanaAddress(addr));
505
535
  const hasExecutionWallet = executionWalletAddresses.length > 0;
506
536
  if (runMode === 'agent_wallet' && (executionWalletsError || !hasExecutionWallet)) {
507
- const fundHint = executionWalletAddresses[0] || 'the managed execution wallet address for each chain';
537
+ const fundHint = executionWalletAddresses.length
538
+ ? executionWalletAddresses.join(', ')
539
+ : 'the managed execution wallet address for each chain';
508
540
  throw createCliError(
509
541
  'LOGIN_EMAIL_INCOMPLETE',
510
542
  executionWalletsError
@@ -538,6 +570,13 @@ async function commandLoginEmail(ctx, flags) {
538
570
  ? {
539
571
  fundingReminder:
540
572
  'Fund executionWallets addresses for agent_wallet. Do not fund the owner embedded wallet for Path A trading.',
573
+ ...(executionWalletAddresses.some((addr) => isSolanaAddress(addr))
574
+ ? {
575
+ solanaFundingReminder: `Fund the Solana execution wallet ${executionWalletAddresses
576
+ .filter((addr) => isSolanaAddress(addr))
577
+ .join(', ')} (base58; give the user this address, not the EVM one) with the SPL tokens (such as USDC) the agent trades. Thirdfy sponsors Solana network fees; keep a little SOL only for rent on new token accounts (about 0.002 SOL each).`,
578
+ }
579
+ : {}),
541
580
  }
542
581
  : {}),
543
582
  },
@@ -16,6 +16,9 @@ const NETWORK_CHAIN_IDS = {
16
16
  'arc-testnet': 5042002,
17
17
  'arc-mainnet': 5042,
18
18
  'arc': 5042,
19
+ // Solana (svm). Without these, --network sol-mainnet fell through to the Base default.
20
+ 'sol-mainnet': 101,
21
+ 'sol-devnet': 103,
19
22
  };
20
23
 
21
24
  const DEFAULT_CHAT_CHAIN_ID = 8453;
@@ -1,9 +1,10 @@
1
- import { requireFlag } from '../core/args.mjs';
1
+ import { requireFlag, usageError } from '../core/args.mjs';
2
2
  import { apiGet, apiPost } from '../core/http.mjs';
3
3
  import { toAvantisSpelling } from '../core/venueBrand.mjs';
4
4
  import {
5
5
  formatCreditsEstimateSummary,
6
6
  formatCreditsModelsSummary,
7
+ stripUsdFields,
7
8
  } from '../lib/creditsUsdSummary.mjs';
8
9
 
9
10
  function resolveToolCallsEstimate(flags) {
@@ -32,7 +33,38 @@ function resolveToolCallsEstimate(flags) {
32
33
  };
33
34
  }
34
35
 
35
- export function createCreditsCommands({ printEnvelope, getAuthToken }) {
36
+ /** Thirdfy credits are bought on Base with USDC from the agent's wallet (thirdfy-api-v2 `buy-agent-credits`). */
37
+ const CREDITS_PURCHASE_CHAIN_ID = 8453;
38
+
39
+ export function createCreditsCommands({ printEnvelope, getAuthToken, commandWalletExecute }) {
40
+ /**
41
+ * `credits buy --usd <n>`: buy the agent's Thirdfy credits with USDC from its execution wallet on
42
+ * Base (gas sponsored), as thirdfy-mcp's `walletExecute buy-agent-credits` does. A shortcut for
43
+ * `wallet execute --action buy-agent-credits --params '{"usdAmount":n}' --chain-id 8453`; the API
44
+ * sets the allowed range (whole dollars, $2 to $1,000 today). A purchase whose claim did not
45
+ * finish answers WRITE_OUTCOME_UNKNOWN: check `credits balance`, do not buy again; `--tx-hash`
46
+ * resumes that purchase without paying twice. `--chain-id` defaults to Base (84532 on test APIs).
47
+ */
48
+ async function commandCreditsBuy(ctx, flags, capabilities) {
49
+ const raw = String(requireFlag(flags, 'usd', 'Missing --usd (whole US dollars, for example --usd 10)')).trim();
50
+ if (!/^\d+$/.test(raw) || Number(raw) <= 0) throw usageError('--usd must be a whole number of US dollars, for example 10', 'usd');
51
+ const usdAmount = Number(raw);
52
+ const txHash = String(flags.txHash || '').trim();
53
+ await commandWalletExecute(
54
+ ctx,
55
+ {
56
+ ...flags,
57
+ action: 'buy-agent-credits',
58
+ // txHash resumes a purchase whose payment was already sent (the catalog row takes it).
59
+ params: JSON.stringify({ usdAmount, ...(txHash ? { txHash } : {}) }),
60
+ chainId: String(flags.chainId || CREDITS_PURCHASE_CHAIN_ID),
61
+ estimatedAmountUsd: String(usdAmount),
62
+ runMode: 'agent_wallet',
63
+ },
64
+ capabilities,
65
+ );
66
+ }
67
+
36
68
  async function commandCreditsTransactions(ctx, flags) {
37
69
  const authToken = getAuthToken(flags, 'Missing --auth-token (or THIRDFY_AUTH_TOKEN) for credits transactions');
38
70
  const page = Number(flags.page || 1);
@@ -127,13 +159,16 @@ export function createCreditsCommands({ printEnvelope, getAuthToken }) {
127
159
  printEnvelope({
128
160
  success: true,
129
161
  code: 'OK',
130
- message: 'Action pricing quote generated',
131
- data: response,
162
+ // wouldBePaidBy (agent_credits | free_request | owner_credits | none), when the API sends it.
163
+ message: response?.wouldBePaidBy ? `Action pricing quote generated; would be paid by ${response.wouldBePaidBy}` : 'Action pricing quote generated',
164
+ // An action's cost is in credits only, never dollars (thirdfy-mcp quoteActionPricing).
165
+ data: stripUsdFields(response),
132
166
  meta: { apiBase: ctx.apiBase, action },
133
167
  });
134
168
  }
135
169
 
136
170
  return {
171
+ commandCreditsBuy,
137
172
  commandCreditsTransactions,
138
173
  commandCreditsUsage,
139
174
  commandModelsList,
@@ -426,9 +426,11 @@ async function commandDelegationRedeem(ctx, flags, capabilities) {
426
426
  if (!['thirdfy', 'hybrid', 'agent_wallet'].includes(selectedRunMode)) {
427
427
  throw createCliError('DELEGATION_REDEEM_MODE_INVALID', 'delegation redeem supports only --run-mode thirdfy|hybrid|agent_wallet');
428
428
  }
429
+ // Pass the run mode on only when it was given: a profile / config default must still be able to
430
+ // switch to agent_wallet on a Solana action (commandRun resolves the effective mode itself).
429
431
  const delegatedFlags = {
430
432
  ...flags,
431
- runMode: selectedRunMode,
433
+ ...(flags.runMode ? { runMode: selectedRunMode } : {}),
432
434
  };
433
435
  if (!delegatedFlags.estimatedAmountUsd) {
434
436
  delegatedFlags.estimatedAmountUsd = '5';
@@ -1,10 +1,58 @@
1
1
  import { requireFlag, parseJsonFlag } from '../core/args.mjs';
2
2
  import { apiGet, apiPost } from '../core/http.mjs';
3
- import { extractActions, getCachedActionsCatalog } from '../core/context.mjs';
3
+ import { extractActions, findAgentApiKey, getCachedActionsCatalogSnapshot, loadProfileConfig } from '../core/context.mjs';
4
4
  import { getEffectiveRunMode } from '../core/runMode.mjs';
5
- import { formatCreditsBalanceSummary } from '../lib/creditsUsdSummary.mjs';
5
+ import { creditsFreeQuotaNote, formatCreditsBalanceSummary, stripUsdFields } from '../lib/creditsUsdSummary.mjs';
6
6
  import { filterActionsByChain } from '../runtime/actions/chainFilter.mjs';
7
- export function createDiscoveryCommands({ printEnvelope, applyActionFilters, getTradingProviderHint, resolveChainSupport, getAuthToken, resolveAgentApiKey }) {
7
+ import { pageActionList, readActionListPaging } from '../runtime/actions/actionList.mjs';
8
+ import {
9
+ caip2ForChainId,
10
+ chainRegistrySource,
11
+ configuredEvmChainIds,
12
+ configuredSvmChainIds,
13
+ getApiChainRegistry,
14
+ isSvmChainId,
15
+ } from '../core/chainRegistry.mjs';
16
+ import { SOLANA_PROVIDER_HINTS } from '../runtime/providerHints/solana.mjs';
17
+
18
+ /** Execution lanes when the API's capabilities name none (thirdfy-mcp SUPPORTED_EXECUTION_LANES). */
19
+ const DEFAULT_EXECUTION_LANES = Object.freeze(['thirdfy', 'hybrid', 'self', 'agent_wallet', 'validation_only']);
20
+
21
+ /** Chain ids the catalog rows name (`supportedChains` and the like), EVM only. Same as thirdfy-mcp `inferCatalogEvmChainIds`. */
22
+ function catalogEvmChainIds(rows) {
23
+ const found = new Set();
24
+ const collect = (value) => {
25
+ if (typeof value === 'number' && Number.isInteger(value) && value > 0) found.add(value);
26
+ else if (typeof value === 'string' && /^\d+$/.test(value)) found.add(Number(value));
27
+ else if (Array.isArray(value)) value.forEach(collect);
28
+ };
29
+ for (const row of Array.isArray(rows) ? rows : []) {
30
+ for (const key of ['chainId', 'chainIds', 'supportedChainIds', 'supportedChains', 'chains', 'networks']) collect(row?.[key]);
31
+ }
32
+ return [...found].filter((chainId) => !isSvmChainId(chainId)).sort((a, b) => a - b);
33
+ }
34
+
35
+ /** The Solana execution contract, as thirdfy-mcp `solanaExecutionContract` with CLI commands. */
36
+ function solanaExecutionContract() {
37
+ const chainIds = configuredSvmChainIds();
38
+ return {
39
+ enabled: chainIds.length > 0,
40
+ chainFamily: 'svm',
41
+ chainIds,
42
+ caip2: Object.fromEntries(chainIds.map((id) => [id, caip2ForChainId(id)]).filter(([, caip2]) => caip2)),
43
+ requiredRunMode: 'agent_wallet',
44
+ supportedExecutionLanes: ['agent_wallet'],
45
+ wallet: "The agent's managed Privy Solana wallet. executionWalletAddress is base58 and case-sensitive.",
46
+ commands: ['preview', 'run', 'wallet execute', 'agent run'],
47
+ evmOnlyCommands: ['self-exec', 'wallet sign', 'wallet submit'],
48
+ providers: Object.keys(SOLANA_PROVIDER_HINTS),
49
+ flow: '`thirdfy-agent login email` (executionWallets) or `preflight --chain-id 101 --run-mode agent_wallet` gives the base58 executionWalletAddress -> give the user that address (not the EVM one) and fund it with the token to spend -> `run --chain-id 101 --run-mode agent_wallet` with base58 params (for example jupiter_swap {inputMint, outputMint, amount}).',
50
+ gas: 'Thirdfy sponsors Solana network fees (the execution-wallet reply says gasSponsored): never ask the user to buy SOL for gas. The wallet pays only rent for new token accounts (about 0.002 SOL each) and SOL the action spends; a write short of it is refused with INSUFFICIENT_NATIVE_GAS.',
51
+ notes:
52
+ 'Solana execution runs only with --run-mode agent_wallet on chain 101 (103 devnet where a provider supports it). self, hybrid and thirdfy stay EVM-only. Check `actions --chain-id 101` (supportedChains) for each action\'s chains.',
53
+ };
54
+ }
55
+ export function createDiscoveryCommands({ printEnvelope, applyActionFilters, getProviderHintForActions, resolveChainSupport, getAuthToken, resolveAgentApiKey }) {
8
56
  async function commandCatalogsList(ctx, flags, capabilities) {
9
57
  const response = await apiGet(ctx, '/api/v1/agent/actions/catalog');
10
58
  const actions = extractActions(response);
@@ -32,20 +80,32 @@ async function commandCatalogsList(ctx, flags, capabilities) {
32
80
  }
33
81
 
34
82
  async function commandActions(ctx, flags, capabilities) {
35
- const policyAware = Boolean(flags.agentApiKey);
83
+ // The agent's own list whenever execution would run with a key (flag, env or config), so `actions`
84
+ // lists what `run` / `preflight` accept, as thirdfy-mcp `getAgentActions` does.
85
+ const paging = readActionListPaging(flags);
86
+ const snapshot = await getCachedActionsCatalogSnapshot(ctx, flags);
36
87
  const chainIdFilter = Number(flags.chainId || 0) || undefined;
37
- const actions = filterActionsByChain(applyActionFilters(await getCachedActionsCatalog(ctx, flags), flags), chainIdFilter);
38
- const providerHint = getTradingProviderHint(flags.provider);
88
+ const actions = filterActionsByChain(applyActionFilters(snapshot.actions, flags), chainIdFilter);
89
+ const providerHint = getProviderHintForActions(flags.provider, actions);
90
+ // --search / --limit / --offset / --view: one page, as thirdfy-mcp getAgentActions answers.
91
+ const page = paging ? pageActionList(actions, paging) : null;
39
92
  const effectiveRunMode = getEffectiveRunMode(ctx, flags);
40
93
  const chainSupport = resolveChainSupport(capabilities, chainIdFilter, effectiveRunMode);
41
94
  printEnvelope({
42
95
  success: true,
43
96
  code: 'OK',
44
97
  message: 'Actions loaded',
45
- data: { actions, ...(providerHint ? { providerHint } : {}) },
98
+ data: {
99
+ ...(page
100
+ ? { actions: page.actions, pagination: page.pagination, view: page.view, ...(page.hint ? { hint: page.hint } : {}) }
101
+ : { actions }),
102
+ ...(providerHint ? { providerHint } : {}),
103
+ },
46
104
  meta: {
47
105
  apiBase: ctx.apiBase,
48
- policyAware,
106
+ // A key was sent; policyScoped: the API answered with the agent policy's allowlist.
107
+ policyAware: snapshot.keyed,
108
+ policyScoped: snapshot.policyScoped,
49
109
  runMode: effectiveRunMode,
50
110
  profile: ctx.profile || null,
51
111
  chainCompatibility: chainSupport,
@@ -54,19 +114,101 @@ async function commandActions(ctx, flags, capabilities) {
54
114
  });
55
115
  }
56
116
 
57
- async function commandCreditsBalance(ctx, flags, capabilities) {
58
- const authToken = getAuthToken(flags, 'Missing --auth-token (or THIRDFY_AUTH_TOKEN) for credits balance');
59
- const response = await apiGet(ctx, '/api/v1/credits/balance', {
60
- Authorization: `Bearer ${authToken}`,
117
+ /**
118
+ * `chains`: which chains Thirdfy acts on, from the API's capabilities and catalog (thirdfy-mcp
119
+ * `getChainCapabilities`). `--non-evm`: the Solana execution contract (`getNonEvmCapabilities`).
120
+ */
121
+ async function commandChains(ctx, flags, capabilities) {
122
+ const svmChainIds = configuredSvmChainIds();
123
+ const nonEvmChains = svmChainIds.length > 0 ? ['solana'] : [];
124
+ const meta = { apiBase: ctx.apiBase, capabilitiesVersion: capabilities?.contractVersion || null };
125
+ if (flags.nonEvm) {
126
+ const executionEnabled = svmChainIds.length > 0;
127
+ printEnvelope({
128
+ success: true,
129
+ code: 'OK',
130
+ message: executionEnabled ? 'Solana execution is enabled' : 'Solana execution is not enabled on this API',
131
+ data: {
132
+ supportedChains: nonEvmChains,
133
+ executionEnabled,
134
+ contractVersion: 'v2',
135
+ chainRegistrySource: chainRegistrySource(),
136
+ solana: solanaExecutionContract(),
137
+ notes: executionEnabled
138
+ ? 'Solana execution is enabled through `run` / `wallet execute` / `agent run` with --run-mode agent_wallet on chain 101 (the agent\'s managed Privy Solana wallet). self-exec, wallet sign and wallet submit stay EVM-only.'
139
+ : 'Solana execution is not enabled on this Thirdfy API: its capabilities list no svm chain. Chains 101/103 are refused with SOLANA_NOT_ENABLED.',
140
+ },
141
+ meta,
142
+ });
143
+ return;
144
+ }
145
+ const configured = configuredEvmChainIds();
146
+ let catalogChainIds = [];
147
+ let catalogSource = 'live';
148
+ let catalogError;
149
+ try {
150
+ catalogChainIds = catalogEvmChainIds((await getCachedActionsCatalogSnapshot(ctx, flags)).actions);
151
+ } catch (error) {
152
+ catalogSource = 'unavailable';
153
+ catalogError = error?.message || String(error);
154
+ }
155
+ const apiChains = getApiChainRegistry()?.chains;
156
+ printEnvelope({
157
+ success: true,
158
+ code: 'OK',
159
+ message: 'Chain capabilities loaded',
160
+ data: {
161
+ supportedEvmChainIds: [...new Set([...configured, ...catalogChainIds])].sort((a, b) => a - b),
162
+ configuredEvmChainIds: configured,
163
+ catalogEvmChainIds: catalogChainIds,
164
+ catalogSource,
165
+ ...(catalogError ? { catalogError } : {}),
166
+ supportedNonEvmChains: nonEvmChains,
167
+ supportedSvmChainIds: svmChainIds,
168
+ chainRegistrySource: chainRegistrySource(),
169
+ caip2: Object.fromEntries(
170
+ [...configured, ...svmChainIds].map((id) => [id, caip2ForChainId(id)]).filter(([, caip2]) => caip2),
171
+ ),
172
+ supportedExecutionLanes: Array.isArray(capabilities?.runModes) && capabilities.runModes.length
173
+ ? capabilities.runModes
174
+ : [...DEFAULT_EXECUTION_LANES],
175
+ defaultChainId: 8453,
176
+ defaultExecutionLane: getEffectiveRunMode(ctx, flags),
177
+ nonEvmExecutionContract: solanaExecutionContract(),
178
+ // Each chain the API lists, with its execution lanes (CLI only; the MCP has no per-chain lanes).
179
+ ...(apiChains ? { chains: apiChains } : {}),
180
+ },
181
+ meta,
61
182
  });
183
+ }
184
+
185
+ /**
186
+ * `credits balance`. Owner auth reads the owner's balance (`/credits/balance`). An explicit
187
+ * --agent-api-key, or no owner auth but an agent key in env or config, reads the agent's own ledger
188
+ * (`/agent/credits/balance`: its credits, free requests today and the owner-credits fallback), as
189
+ * thirdfy-mcp getCreditsBalance. Credits only, never dollars.
190
+ */
191
+ async function commandCreditsBalance(ctx, flags, capabilities) {
192
+ const config = loadProfileConfig();
193
+ const ownerToken = String(flags.authToken || process.env.THIRDFY_AUTH_TOKEN || config?.auth?.authToken || '').trim();
194
+ const agentApiKey = findAgentApiKey(flags);
195
+ const agentLedger = Boolean(String(flags.agentApiKey || '').trim()) || (!ownerToken && Boolean(agentApiKey));
196
+ const response = agentLedger
197
+ ? await apiGet(ctx, '/api/v1/agent/credits/balance', { 'x-agent-api-key': agentApiKey })
198
+ : await apiGet(ctx, '/api/v1/credits/balance', {
199
+ Authorization: `Bearer ${getAuthToken(flags, 'Missing --auth-token (or THIRDFY_AUTH_TOKEN), or an agent key for the agent\'s own credits, for credits balance')}`,
200
+ });
62
201
  const summary = formatCreditsBalanceSummary(response);
202
+ const note = creditsFreeQuotaNote(response);
63
203
  printEnvelope({
64
204
  success: true,
65
205
  code: 'OK',
66
206
  message: flags.json ? 'Credits balance loaded' : `Credits balance loaded: ${summary}`,
67
- data: response,
207
+ // Credits only: no USD price per credit (thirdfy-mcp getCreditsBalance).
208
+ data: { ...stripUsdFields(response), ...(note ? { note } : {}) },
68
209
  meta: {
69
210
  apiBase: ctx.apiBase,
211
+ ledger: agentLedger ? 'agent' : 'owner',
70
212
  capabilitiesVersion: capabilities?.contractVersion || null,
71
213
  ...(flags.json ? {} : { summary }),
72
214
  },
@@ -76,6 +218,7 @@ async function commandCreditsBalance(ctx, flags, capabilities) {
76
218
  return {
77
219
  commandCatalogsList,
78
220
  commandActions,
221
+ commandChains,
79
222
  commandCreditsBalance,
80
223
  };
81
224
  }
@@ -124,11 +124,14 @@ function buildOnboardingHelp(ctx, flags = {}) {
124
124
  },
125
125
  {
126
126
  id: 'wallet_sign',
127
- description: 'Headless wallet proof for operators with an EVM signer.',
127
+ description: 'An agent with its own EVM key and no email: prove the key, then create the agent. The agent wallet owns the agent.',
128
128
  commands: [
129
129
  'thirdfy-agent agent auth challenge --agent-key 0xYOUR_AGENT_KEY --json',
130
- 'thirdfy-agent login --agent-key 0xYOUR_AGENT_KEY --challenge-id <id> --signature <hex> --json',
130
+ '# sign data.message exactly as given with the private key of 0xYOUR_AGENT_KEY (EIP-191 personal_sign)',
131
+ 'thirdfy-agent agent auth verify --agent-key 0xYOUR_AGENT_KEY --challenge-id <id> --signature <hex> --json',
132
+ 'thirdfy-agent agent register --agent-key 0xYOUR_AGENT_KEY --owner-session-token <ownerSessionToken> --name "My Agent" --json',
131
133
  ],
134
+ note: 'verify returns an owner session; register with --name creates the agent and its API key. An existing agent logs in with `thirdfy-agent login --agent-key 0xYOUR_AGENT_KEY --challenge-id <id> --signature <hex>`. No owner session is stored until the challenge completes.',
132
135
  },
133
136
  {
134
137
  id: 'existing_credentials',