@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
@@ -1,4 +1,4 @@
1
- import { requireFlag } from '../core/args.mjs';
1
+ import { parseJsonFlag, requireFlag } from '../core/args.mjs';
2
2
  import { createCliError, exitAfterOutput } from '../core/envelope.mjs';
3
3
  import {
4
4
  getEffectiveRunMode,
@@ -7,6 +7,13 @@ import {
7
7
  parseBooleanFlag,
8
8
  } from '../core/runMode.mjs';
9
9
  import { apiGet } from '../core/http.mjs';
10
+ import { applySvmLane } from '../runtime/execution/svmLane.mjs';
11
+ import { isReadOnlyResolvedAction } from '../core/readOnlyActions.mjs';
12
+ import { resolveWalletExecuteChainId } from '../core/walletExecuteChainId.mjs';
13
+ import { resolveExecuteEstimatedAmountUsd } from '../runtime/execution/payloads.mjs';
14
+ import { assertPreviewSized, buildPreviewOrder, shapeOrderPreview } from '../runtime/risk/orderPreview.mjs';
15
+ import { actionNameForApi } from '../runtime/actions/selection.mjs';
16
+ import { runNotionalWarnings } from '../runtime/risk/notional.mjs';
10
17
  import { findTypedReading, formatTypedReadingLead } from './signals/typedReading.mjs';
11
18
  import { createRequire } from 'module';
12
19
 
@@ -25,6 +32,7 @@ export function createExecuteCommands(deps) {
25
32
  applyAvantisAgentWalletAddressDefault,
26
33
  runPreflightByMode,
27
34
  runExecutionByMode,
35
+ resolveManagedExecutionPreflight,
28
36
  applyExecutionFallbackHints,
29
37
  performSelfBroadcast,
30
38
  executeManagedWalletRun,
@@ -39,7 +47,8 @@ async function commandPreflight(ctx, flags, capabilities) {
39
47
  const resolved = await resolveActionSelection(ctx, flags);
40
48
  prepareActionParamsForFlags(flags, resolved.resolvedAction);
41
49
  enforceCatalogActionParams(resolved, flags);
42
- const runMode = getEffectiveRunMode(ctx, flags);
50
+ // Solana-only rows default to chain 101; Solana runs only on agent_wallet.
51
+ const runMode = applySvmLane(flags, resolved, getEffectiveRunMode(ctx, flags));
43
52
  enforceOffchainVenueLaneCompatibility(resolved.resolvedAction, runMode);
44
53
  const hybridWalletMode = runMode === 'hybrid' ? normalizeHybridWalletMode(flags.hybridWalletMode) : null;
45
54
  enforceChainCompatibility(capabilities, flags, runMode);
@@ -89,11 +98,88 @@ async function commandPreflight(ctx, flags, capabilities) {
89
98
  }
90
99
 
91
100
 
101
+ /**
102
+ * `preview`: whether Thirdfy would sign a write, with its order and risk preview, before it is sent.
103
+ * Same verdict as thirdfy-mcp `previewOrder`. Read only: nothing is signed, billed or reserved. A
104
+ * blocked order is the answer (success, exit 0); `data.wouldSign` says whether `run` would sign it.
105
+ */
106
+ async function commandPreview(ctx, flags, capabilities) {
107
+ const previewFlags = { ...flags, runMode: 'agent_wallet' };
108
+ const resolved = await resolveActionSelection(ctx, previewFlags);
109
+ prepareActionParamsForFlags(previewFlags, resolved.resolvedAction);
110
+ enforceCatalogActionParams(resolved, previewFlags);
111
+ if (isReadOnlyResolvedAction(resolved)) {
112
+ throw createCliError(
113
+ 'PREVIEW_ORDER_WRITE_ONLY',
114
+ `${resolved.resolvedAction} is a read; preview previews writes. Run the read with \`thirdfy-agent run\`.`,
115
+ { resolvedAction: resolved.resolvedAction, requestedAction: resolved.requestedAction },
116
+ );
117
+ }
118
+ applySvmLane(previewFlags, resolved, 'agent_wallet', { locked: true });
119
+ enforceChainCompatibility(capabilities, previewFlags, 'agent_wallet');
120
+ const managedPreflight =
121
+ (await applyHyperliquidAgentWalletAddressDefault(ctx, previewFlags, resolved, 'agent_wallet'))
122
+ || (await applyLighterAgentWalletAddressDefault(ctx, previewFlags, resolved, 'agent_wallet'))
123
+ || (await applyAvantisAgentWalletAddressDefault(ctx, previewFlags, resolved, 'agent_wallet'));
124
+ // The action-aware preflight, with no CLI funding or gas gate: the verdict reads the API's answer.
125
+ const preflight = await resolveManagedExecutionPreflight(ctx, previewFlags, resolved, {
126
+ runMode: 'agent_wallet',
127
+ requireFundingCheck: false,
128
+ preflightSeed: managedPreflight,
129
+ evaluateAction: true,
130
+ });
131
+ // Anything but an answer about the order (no identity, a wallet mismatch) is a failed command.
132
+ if (!preflight.success && preflight.preflightBlocked !== true) {
133
+ throw createCliError(preflight.blockedReason || 'PREVIEW_FAILED', preflight.error || 'Preview failed', {
134
+ blockedStage: preflight.blockedStage || 'routing',
135
+ });
136
+ }
137
+ // The catalog key drives the CLI's own rules; the verdict and the order name the action the API
138
+ // was asked about (the requested name), as thirdfy-mcp previewOrder does.
139
+ const action = resolved.resolvedAction;
140
+ const apiAction = actionNameForApi(resolved);
141
+ const chainId = Number(preflight.chainId) || resolveWalletExecuteChainId({
142
+ action,
143
+ chainId: previewFlags.chainId,
144
+ params: parseJsonFlag(previewFlags, 'params') || {},
145
+ });
146
+ const estimatedAmountUsd = resolveExecuteEstimatedAmountUsd(action, previewFlags);
147
+ const behavior = resolved.resolvedActionMeta?.behavior;
148
+ const shaped = shapeOrderPreview({ action: apiAction, chainId, preflight });
149
+ assertPreviewSized({ shaped, estimatedAmountUsd, behavior });
150
+ const order = buildPreviewOrder({
151
+ shaped,
152
+ params: parseJsonFlag(flags, 'params') || {},
153
+ estimatedAmountUsd,
154
+ paramsSchema: previewFlags.__actionParamsSchema,
155
+ });
156
+ printEnvelope({
157
+ success: true,
158
+ code: shaped.wouldSign ? 'PREVIEW_WOULD_SIGN' : 'PREVIEW_BLOCKED',
159
+ message: shaped.wouldSign
160
+ ? 'Thirdfy would sign this order'
161
+ : shaped.message || `Thirdfy would not sign this order (${shaped.blockedReason || 'blocked'})`,
162
+ data: { ...shaped, ...(order ? { order } : {}) },
163
+ meta: {
164
+ apiBase: ctx.apiBase,
165
+ requestedAction: resolved.requestedAction,
166
+ resolvedAction: action,
167
+ runMode: 'agent_wallet',
168
+ executionWalletAddress: preflight.executionWalletAddress || null,
169
+ profile: ctx.profile || null,
170
+ swapInput: previewFlags.__swapInput || null,
171
+ capabilitiesVersion: capabilities?.contractVersion || null,
172
+ },
173
+ });
174
+ }
175
+
176
+
92
177
  async function commandRun(ctx, flags, capabilities) {
93
178
  const resolved = await resolveActionSelection(ctx, flags);
94
179
  prepareActionParamsForFlags(flags, resolved.resolvedAction);
95
180
  enforceCatalogActionParams(resolved, flags);
96
- const runMode = getEffectiveRunMode(ctx, flags);
181
+ // Solana-only rows default to chain 101; Solana runs only on agent_wallet.
182
+ const runMode = applySvmLane(flags, resolved, getEffectiveRunMode(ctx, flags));
97
183
  enforceOffchainVenueLaneCompatibility(resolved.resolvedAction, runMode);
98
184
  const hybridWalletMode = runMode === 'hybrid' ? normalizeHybridWalletMode(flags.hybridWalletMode) : null;
99
185
  enforceChainCompatibility(capabilities, flags, runMode);
@@ -102,6 +188,8 @@ async function commandRun(ctx, flags, capabilities) {
102
188
  || (await applyLighterAgentWalletAddressDefault(ctx, flags, resolved, runMode))
103
189
  || (await applyAvantisAgentWalletAddressDefault(ctx, flags, resolved, runMode));
104
190
  const skipPreflight = Boolean(flags.skipPreflight);
191
+ // A write with no --estimated-amount-usd: a warning, or a refusal before anything is sent.
192
+ const warnings = runNotionalWarnings(resolved, flags, runMode);
105
193
  const backendResult = await runExecutionByMode(runMode, ctx, flags, resolved, {
106
194
  skipPreflight,
107
195
  hybridWalletMode,
@@ -164,6 +252,9 @@ async function commandRun(ctx, flags, capabilities) {
164
252
  skippedPreflight: skipPreflight,
165
253
  swapInput: flags.__swapInput || null,
166
254
  capabilitiesVersion: capabilities?.contractVersion || null,
255
+ ...(flags.__runModeDefaultedForSvm ? { runModeDefaultedForSvm: true } : {}),
256
+ ...(flags.__chainIdInferred ? { chainIdInferred: flags.__chainIdInferred } : {}),
257
+ ...(warnings.length ? { warnings } : {}),
167
258
  },
168
259
  },
169
260
  typedReading ? { humanLead: formatTypedReadingLead(typedReading) } : {},
@@ -238,6 +329,7 @@ async function commandSelfExec(ctx, flags, capabilities) {
238
329
  const resolved = await resolveActionSelection(ctx, flags);
239
330
  prepareActionParamsForFlags(flags, resolved.resolvedAction);
240
331
  enforceCatalogActionParams(resolved, flags);
332
+ applySvmLane(flags, resolved, 'self', { locked: true });
241
333
  enforceOffchainVenueLaneCompatibility(resolved.resolvedAction, 'self');
242
334
  enforceChainCompatibility(capabilities, flags, 'self');
243
335
  const selfResult = await executeSelfRun(ctx, flags, resolved, {
@@ -278,6 +370,7 @@ async function commandSelfExec(ctx, flags, capabilities) {
278
370
 
279
371
  return {
280
372
  commandPreflight,
373
+ commandPreview,
281
374
  commandRun,
282
375
  commandAgentRun,
283
376
  commandIntentStatus,
@@ -1,5 +1,6 @@
1
1
  import { requireFlag } from '../core/args.mjs';
2
2
  import { apiPost } from '../core/http.mjs';
3
+ import { getEffectiveRunMode } from '../core/runMode.mjs';
3
4
  export function createPromptCommands({ printEnvelope }) {
4
5
  async function commandPrompt(ctx, flags, capabilities, promptTokens) {
5
6
  const prompt = String(flags.prompt || promptTokens.join(' ') || '').trim();
@@ -9,12 +10,13 @@ async function commandPrompt(ctx, flags, capabilities, promptTokens) {
9
10
  printEnvelope({
10
11
  success: true,
11
12
  code: 'PROMPT_RECEIVED',
12
- message: 'Prompt captured; execute with `run` using --run-mode.',
13
+ message: 'Prompt captured; find the action with `actions`, check it with `preview`, then send it with `run`.',
13
14
  data: {
14
15
  prompt,
15
16
  suggestedCommands: [
16
- 'thirdfy-agent run --run-mode self --agent-api-key ... --action ... --params ...',
17
- 'thirdfy-agent run --run-mode hybrid --agent-api-key ... --action ... --params ...',
17
+ 'thirdfy-agent actions --provider <provider> --json',
18
+ 'thirdfy-agent preview --action <action> --params <json> --estimated-amount-usd <usd> --json',
19
+ 'thirdfy-agent run --run-mode agent_wallet --action <action> --params <json> --estimated-amount-usd <usd> --json',
18
20
  ],
19
21
  },
20
22
  meta: {
@@ -1,8 +1,14 @@
1
- import { requireFlag } from '../core/args.mjs';
1
+ import { requireFlag, usageError } from '../core/args.mjs';
2
2
  import { createCliError, exitAfterOutput } from '../core/envelope.mjs';
3
- import { loadProfileConfig } from '../core/context.mjs';
4
- import { DEFAULT_CHAIN_ID, parseChainIdFlag, resolveChainRpcUrl } from '../core/chainRegistry.mjs';
3
+ import { findAgentApiKey, loadProfileConfig } from '../core/context.mjs';
4
+ import { DEFAULT_CHAIN_ID, chainGasNote, isSvmChainId, parseChainIdFlag, resolveChainRpcUrl } from '../core/chainRegistry.mjs';
5
+ import { apiGet } from '../core/http.mjs';
6
+ import { getEffectiveRunMode } from '../core/runMode.mjs';
7
+ import { readWalletControl } from '../runtime/execution/walletControl.mjs';
8
+ import { assertSvmChainRunByApi } from '../runtime/execution/svmLane.mjs';
9
+ import { applySvmLane } from '../runtime/execution/svmLane.mjs';
5
10
  import { readSignedTxChainId } from '../core/signedTx.mjs';
11
+ import { runNotionalWarnings } from '../runtime/risk/notional.mjs';
6
12
 
7
13
  export function createWalletCommands(deps) {
8
14
  const {
@@ -18,6 +24,9 @@ export function createWalletCommands(deps) {
18
24
  executeSelfRun,
19
25
  submitSignedTx,
20
26
  printEnvelope,
27
+ resolveEffectiveUserDid,
28
+ resolveAgentKey,
29
+ buildOwnerAuthHeaders,
21
30
  } = deps;
22
31
 
23
32
 
@@ -26,12 +35,15 @@ async function commandWalletExecute(ctx, flags, capabilities) {
26
35
  const resolved = await resolveActionSelection(ctx, normalizedFlags);
27
36
  prepareActionParamsForFlags(normalizedFlags, resolved.resolvedAction);
28
37
  enforceCatalogActionParams(resolved, normalizedFlags);
38
+ applySvmLane(normalizedFlags, resolved, 'agent_wallet');
29
39
  enforceChainCompatibility(capabilities, normalizedFlags, 'agent_wallet');
30
40
  const managedPreflight =
31
41
  (await applyHyperliquidAgentWalletAddressDefault(ctx, normalizedFlags, resolved, 'agent_wallet'))
32
42
  || (await applyLighterAgentWalletAddressDefault(ctx, normalizedFlags, resolved, 'agent_wallet'))
33
43
  || (await applyAvantisAgentWalletAddressDefault(ctx, normalizedFlags, resolved, 'agent_wallet'));
34
44
  const skipPreflight = Boolean(normalizedFlags.skipPreflight);
45
+ // A write with no --estimated-amount-usd: a warning, or a refusal before anything is sent.
46
+ const warnings = runNotionalWarnings(resolved, normalizedFlags, 'agent_wallet');
35
47
  const result = await executeManagedWalletRun(ctx, normalizedFlags, resolved, { skipPreflight, managedPreflight });
36
48
  printEnvelope({
37
49
  success: result.success,
@@ -47,6 +59,7 @@ async function commandWalletExecute(ctx, flags, capabilities) {
47
59
  executionWalletAddress: result.executionWalletAddress || null,
48
60
  profile: ctx.profile || null,
49
61
  capabilitiesVersion: capabilities?.contractVersion || null,
62
+ ...(warnings.length ? { warnings } : {}),
50
63
  },
51
64
  });
52
65
  if (!result.success) await exitAfterOutput(1);
@@ -58,6 +71,7 @@ async function commandWalletSign(ctx, flags, capabilities) {
58
71
  const resolved = await resolveActionSelection(ctx, normalizedFlags);
59
72
  prepareActionParamsForFlags(normalizedFlags, resolved.resolvedAction);
60
73
  enforceCatalogActionParams(resolved, normalizedFlags);
74
+ applySvmLane(normalizedFlags, resolved, 'self', { locked: true });
61
75
  enforceOffchainVenueLaneCompatibility(resolved.resolvedAction, 'self');
62
76
  enforceChainCompatibility(capabilities, normalizedFlags, 'self');
63
77
  const selfResult = await executeSelfRun(ctx, normalizedFlags, resolved, {
@@ -91,6 +105,13 @@ async function commandWalletSubmit(ctx, flags, capabilities) {
91
105
  if (Number.isNaN(flagChainId)) {
92
106
  throw createCliError('INVALID_CHAIN_ID', `wallet submit --chain-id must be a positive integer (got ${flags.chainId})`);
93
107
  }
108
+ if (isSvmChainId(flagChainId)) {
109
+ throw createCliError(
110
+ 'SVM_REQUIRES_AGENT_WALLET',
111
+ `wallet submit broadcasts signed EVM transactions only. Solana (chain ${flagChainId}) actions run with --run-mode agent_wallet on the managed Solana agent wallet.`,
112
+ { chainId: flagChainId, chainFamily: 'svm', recommendedRunModes: ['agent_wallet'] },
113
+ );
114
+ }
94
115
  // The signed tx carries its chain id (EIP-155 / typed tx). Use it when --chain-id is absent,
95
116
  // and refuse a mismatch so a DogeOS-signed tx is never sent to a Base RPC (or vice versa).
96
117
  const txChainId = readSignedTxChainId(signedTxHex);
@@ -147,7 +168,102 @@ async function commandWalletList(ctx, _flags) {
147
168
  });
148
169
  }
149
170
 
171
+ /** The chain a readiness check uses, saying so when it assumed Base (thirdfy-mcp `resolveReadinessChain`). */
172
+ function resolveReadinessChain(flags) {
173
+ const chainId = parseChainIdFlag(flags.chainId);
174
+ // A bad --chain-id is an error, not a silent check of Base.
175
+ if (Number.isNaN(chainId)) throw usageError('--chain-id must be a positive integer chain id', 'chainId');
176
+ if (chainId) return { chainId };
177
+ return {
178
+ chainId: DEFAULT_CHAIN_ID,
179
+ chainIdAssumed: true,
180
+ chainIdNote: `--chain-id was not provided; assumed Base (${DEFAULT_CHAIN_ID}). Pass --chain-id for any other chain (for example DogeOS testnet 6281971, Solana 101).`,
181
+ };
182
+ }
183
+
184
+ /**
185
+ * `readiness`: one verdict on whether the agent can act, as thirdfy-mcp `getSelfReadiness`.
186
+ * - agent_wallet: the agent's execution wallet on the chain (`GET /agent/execution-wallet` with the
187
+ * agent's own key): its address, funding, gas, and wallet control (API 4.0), with `ready`,
188
+ * `walletControl` and `nextAction` at the top. No owner auth.
189
+ * - self / hybrid / thirdfy: the delegation check (`GET /agent/delegation/check`, owner auth).
190
+ */
191
+ async function commandReadiness(ctx, flags, capabilities) {
192
+ const runMode = getEffectiveRunMode(ctx, flags);
193
+ const chain = resolveReadinessChain(flags);
194
+ const meta = { apiBase: ctx.apiBase, runMode, capabilitiesVersion: capabilities?.contractVersion || null };
195
+ const echo = {
196
+ runMode,
197
+ custodyMode: ctx.custodyMode || null,
198
+ chainId: chain.chainId,
199
+ ...(chain.chainIdAssumed ? { chainIdAssumed: true, chainIdNote: chain.chainIdNote } : {}),
200
+ walletAddress: flags.walletAddress ? String(flags.walletAddress) : null,
201
+ };
202
+
203
+ if (runMode !== 'agent_wallet') {
204
+ const headers = buildOwnerAuthHeaders(flags, `readiness with --run-mode ${runMode} needs owner auth (--auth-token or --owner-session-token).`);
205
+ const agentKey = requireFlag({ agentKey: resolveAgentKey(flags) }, 'agentKey', 'Missing --agent-key (or THIRDFY_AGENT_KEY / config.agent.agentKey)');
206
+ const query = new URLSearchParams({ agentKey, chainId: String(chain.chainId) });
207
+ const readiness = await apiGet(ctx, `/api/v1/agent/delegation/check?${query.toString()}`, headers);
208
+ printEnvelope({
209
+ success: true,
210
+ code: 'READINESS_OK',
211
+ message: `Delegation readiness for ${runMode}`,
212
+ data: { ...echo, ...(chainGasNote(chain.chainId) ? { gas: chainGasNote(chain.chainId) } : {}), source: '/api/v1/agent/delegation/check', readiness },
213
+ meta,
214
+ });
215
+ return;
216
+ }
217
+
218
+ // A Solana chain the API does not run is refused as `run` refuses it (thirdfy-mcp unsupportedChainFields).
219
+ assertSvmChainRunByApi(chain.chainId);
220
+ // Only the agent's own key: readiness acts for the caller's agent.
221
+ const agentApiKey = findAgentApiKey(flags);
222
+ if (!agentApiKey) {
223
+ throw createCliError(
224
+ 'AGENT_API_KEY_REQUIRED',
225
+ "readiness with --run-mode agent_wallet needs the agent's own API key (--agent-api-key, THIRDFY_AGENT_API_KEY or config).",
226
+ { blockedStage: 'auth', runMode, chainId: chain.chainId },
227
+ );
228
+ }
229
+ const userDid = resolveEffectiveUserDid(flags, { runMode: 'agent_wallet' });
230
+ const query = new URLSearchParams({ chainId: String(chain.chainId), runMode: 'agent_wallet' });
231
+ if (userDid) query.set('userDid', userDid);
232
+ const data = await apiGet(ctx, `/api/v1/agent/execution-wallet?${query.toString()}`, { 'x-agent-api-key': agentApiKey });
233
+ // The API says per chain whether it sponsors the fee (Solana `gasSponsored`); that answer wins.
234
+ const gas = chainGasNote(chain.chainId, { gasSponsored: data?.gasSponsored });
235
+ const control = readWalletControl(data, { svm: isSvmChainId(chain.chainId) });
236
+ const raw = String(data?.fundingTokenBalance?.raw ?? '').trim();
237
+ const fundingReady = /^(0x[0-9a-fA-F]+|[0-9]+)$/.test(raw) ? BigInt(raw) > 0n : undefined;
238
+ const ready = Boolean(data?.executionWalletAddress) && data?.success !== false && !control.walletControlBlocked;
239
+ printEnvelope({
240
+ success: true,
241
+ code: ready ? 'READY' : 'NOT_READY',
242
+ message: ready ? 'The agent can act on this chain' : control.userMessage || 'The agent is not ready on this chain',
243
+ data: {
244
+ ...echo,
245
+ ...(gas ? { gas } : {}),
246
+ source: '/api/v1/agent/execution-wallet',
247
+ ready,
248
+ // `ready` says the wallet can sign; whether it holds the funding token is this separate field.
249
+ ...(fundingReady !== undefined ? { fundingReady } : {}),
250
+ ...(control.walletControl ? { walletControl: control.walletControl } : {}),
251
+ ...(control.nextAction ? { nextAction: control.nextAction, userMessage: control.userMessage } : {}),
252
+ readiness: {
253
+ executionWalletAddress: data?.executionWalletAddress || null,
254
+ userDid: userDid || data?.userDid || null,
255
+ ready,
256
+ ...(fundingReady !== undefined ? { fundingReady } : {}),
257
+ ...(control.walletControlBlocked ? { blockedBy: 'wallet_control', blocker: control.walletControlBlocker, signerActive: false } : {}),
258
+ data,
259
+ },
260
+ },
261
+ meta,
262
+ });
263
+ }
264
+
150
265
  return {
266
+ commandReadiness,
151
267
  commandWalletExecute,
152
268
  commandWalletSign,
153
269
  commandWalletSubmit,
@@ -0,0 +1,66 @@
1
+ import { loadProfileConfig } from '../core/context.mjs';
2
+ import { parseJsonFlag, requireFlag } from '../core/args.mjs';
3
+ import { apiGet, apiPost } from '../core/http.mjs';
4
+
5
+ /**
6
+ * The owner's side of API 4.0 wallet control (`/api/v1/agent/:agentKey/wallet-control/*`), for a
7
+ * user-owned agent wallet. Owner auth only (`--owner-session-token` or a Privy `--auth-token`); the
8
+ * API refuses agent API keys. Thirdfy never signs these: `request` returns the unsigned Privy
9
+ * request(s), the owner signs `signatureInput` with their own key, and `submit` relays it. This is
10
+ * how an owner answers AGENT_POLICY_UPDATE_REQUIRED (`--action update_policy`) from the CLI.
11
+ * Same routes as Console and thirdfy-mcp `walletControlRequest` / `walletControlSubmit`.
12
+ */
13
+ export function createWalletControlCommands({ printEnvelope, buildOwnerAuthHeaders }) {
14
+ const ownerHeaders = (flags, command) =>
15
+ buildOwnerAuthHeaders(flags, `Missing owner auth for wallet-control ${command}: provide --auth-token (THIRDFY_AUTH_TOKEN) or --owner-session-token`);
16
+
17
+ function agentKeyOf(flags) {
18
+ const key = String(flags.agentKey || loadProfileConfig()?.agent?.agentKey || '').trim();
19
+ if (!key) throw new Error('Missing --agent-key (or a configured agent)');
20
+ return key;
21
+ }
22
+
23
+ const base = (agentKey) => `/api/v1/agent/${encodeURIComponent(agentKey)}/wallet-control`;
24
+ const meta = (ctx, capabilities) => ({ apiBase: ctx.apiBase, capabilitiesVersion: capabilities?.contractVersion || null });
25
+
26
+ async function commandWalletControlState(ctx, flags, capabilities) {
27
+ const headers = ownerHeaders(flags, 'state');
28
+ const agentKey = agentKeyOf(flags);
29
+ const query = flags.walletAddress ? `?walletAddress=${encodeURIComponent(String(flags.walletAddress).trim())}` : '';
30
+ const response = await apiGet(ctx, `${base(agentKey)}/state${query}`, headers);
31
+ printEnvelope({ success: true, code: 'WALLET_CONTROL_STATE', message: 'Owner wallet state', data: response?.data ?? response, meta: meta(ctx, capabilities) });
32
+ }
33
+
34
+ async function commandWalletControlRequest(ctx, flags, capabilities) {
35
+ const headers = ownerHeaders(flags, 'request');
36
+ const action = requireFlag(flags, 'action', 'Missing --action (update_policy, stop_agent, resume_agent, change_payout, export)');
37
+ const agentKey = agentKeyOf(flags);
38
+ const payload = { action: String(action).trim() };
39
+ if (flags.walletAddress) payload.walletAddress = String(flags.walletAddress).trim();
40
+ if (flags.payoutAddress) payload.payoutAddress = String(flags.payoutAddress).trim();
41
+ if (flags.recipientPublicKey) payload.recipientPublicKey = String(flags.recipientPublicKey).trim();
42
+ const response = await apiPost(ctx, `${base(agentKey)}/requests`, payload, headers);
43
+ printEnvelope({
44
+ success: true,
45
+ code: 'WALLET_CONTROL_REQUESTS',
46
+ message: 'Unsigned owner request(s): sign each signatureInput with the owner key, then run wallet-control submit',
47
+ data: response?.data ?? response,
48
+ meta: meta(ctx, capabilities),
49
+ });
50
+ }
51
+
52
+ async function commandWalletControlSubmit(ctx, flags, capabilities) {
53
+ const headers = ownerHeaders(flags, 'submit');
54
+ const action = requireFlag(flags, 'action', 'Missing --action');
55
+ const signatureInput = parseJsonFlag(flags, 'signatureInput');
56
+ if (!signatureInput || typeof signatureInput !== 'object') throw new Error('Missing --signature-input (the signatureInput JSON of the request the owner signed)');
57
+ const signature = requireFlag(flags, 'signature', 'Missing --signature (the owner signature of signatureInput)');
58
+ const agentKey = agentKeyOf(flags);
59
+ const payload = { action: String(action).trim(), signatureInput, signature: String(signature).trim() };
60
+ if (flags.payoutAddress) payload.payoutAddress = String(flags.payoutAddress).trim();
61
+ const response = await apiPost(ctx, `${base(agentKey)}/submit`, payload, headers);
62
+ printEnvelope({ success: true, code: 'WALLET_CONTROL_SUBMITTED', message: 'Owner action relayed', data: response?.data ?? response, meta: meta(ctx, capabilities) });
63
+ }
64
+
65
+ return { commandWalletControlState, commandWalletControlRequest, commandWalletControlSubmit };
66
+ }
@@ -44,6 +44,51 @@
44
44
  },
45
45
  "additionalProperties": true
46
46
  }
47
+ },
48
+ "pagination": {
49
+ "type": "object",
50
+ "description": "Present with --search, --limit, --offset or --view (thirdfy-mcp getAgentActions paging).",
51
+ "required": [
52
+ "total",
53
+ "matched",
54
+ "offset",
55
+ "limit",
56
+ "returned",
57
+ "hasMore"
58
+ ],
59
+ "properties": {
60
+ "total": {
61
+ "type": "integer"
62
+ },
63
+ "matched": {
64
+ "type": "integer"
65
+ },
66
+ "offset": {
67
+ "type": "integer"
68
+ },
69
+ "limit": {
70
+ "type": "integer"
71
+ },
72
+ "returned": {
73
+ "type": "integer"
74
+ },
75
+ "hasMore": {
76
+ "type": "boolean"
77
+ }
78
+ },
79
+ "additionalProperties": true
80
+ },
81
+ "view": {
82
+ "enum": [
83
+ "concise",
84
+ "detailed"
85
+ ]
86
+ },
87
+ "hint": {
88
+ "type": "string"
89
+ },
90
+ "providerHint": {
91
+ "type": "object"
47
92
  }
48
93
  },
49
94
  "additionalProperties": true
@@ -0,0 +1,76 @@
1
+ {
2
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
3
+ "title": "CLI response data: chains --non-evm (thirdfy-mcp getNonEvmCapabilities parity)",
4
+ "type": "object",
5
+ "required": [
6
+ "supportedChains",
7
+ "executionEnabled",
8
+ "contractVersion",
9
+ "solana",
10
+ "notes"
11
+ ],
12
+ "properties": {
13
+ "supportedChains": {
14
+ "type": "array",
15
+ "items": {
16
+ "type": "string"
17
+ }
18
+ },
19
+ "executionEnabled": {
20
+ "type": "boolean"
21
+ },
22
+ "contractVersion": {
23
+ "type": "string"
24
+ },
25
+ "chainRegistrySource": {
26
+ "enum": [
27
+ "api",
28
+ "default"
29
+ ]
30
+ },
31
+ "solana": {
32
+ "type": "object",
33
+ "required": [
34
+ "enabled",
35
+ "chainFamily",
36
+ "chainIds",
37
+ "requiredRunMode"
38
+ ],
39
+ "properties": {
40
+ "enabled": {
41
+ "type": "boolean"
42
+ },
43
+ "chainFamily": {
44
+ "enum": [
45
+ "svm"
46
+ ]
47
+ },
48
+ "chainIds": {
49
+ "type": "array",
50
+ "items": {
51
+ "type": "integer"
52
+ }
53
+ },
54
+ "caip2": {
55
+ "type": "object"
56
+ },
57
+ "requiredRunMode": {
58
+ "enum": [
59
+ "agent_wallet"
60
+ ]
61
+ },
62
+ "providers": {
63
+ "type": "array",
64
+ "items": {
65
+ "type": "string"
66
+ }
67
+ }
68
+ },
69
+ "additionalProperties": true
70
+ },
71
+ "notes": {
72
+ "type": "string"
73
+ }
74
+ },
75
+ "additionalProperties": true
76
+ }