@absol-labs/agent 0.9.1 → 0.11.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.
- package/README.md +184 -26
- package/dist/capability/invocation-capability.d.ts +18 -6
- package/dist/capability/invocation-capability.d.ts.map +1 -1
- package/dist/capability/invocation-capability.js.map +1 -1
- package/dist/cli/amounts.d.ts +26 -0
- package/dist/cli/amounts.d.ts.map +1 -0
- package/dist/cli/amounts.js +71 -0
- package/dist/cli/amounts.js.map +1 -0
- package/dist/cli/args.d.ts +32 -0
- package/dist/cli/args.d.ts.map +1 -0
- package/dist/cli/args.js +119 -0
- package/dist/cli/args.js.map +1 -0
- package/dist/cli/attesters.d.ts +51 -0
- package/dist/cli/attesters.d.ts.map +1 -0
- package/dist/cli/attesters.js +58 -0
- package/dist/cli/attesters.js.map +1 -0
- package/dist/cli/bin.d.ts +3 -0
- package/dist/cli/bin.d.ts.map +1 -0
- package/dist/cli/bin.js +5 -0
- package/dist/cli/bin.js.map +1 -0
- package/dist/cli/commands/claim.d.ts +13 -0
- package/dist/cli/commands/claim.d.ts.map +1 -0
- package/dist/cli/commands/claim.js +102 -0
- package/dist/cli/commands/claim.js.map +1 -0
- package/dist/cli/commands/close.d.ts +3 -0
- package/dist/cli/commands/close.d.ts.map +1 -0
- package/dist/cli/commands/close.js +55 -0
- package/dist/cli/commands/close.js.map +1 -0
- package/dist/cli/commands/discover.d.ts +3 -0
- package/dist/cli/commands/discover.d.ts.map +1 -0
- package/dist/cli/commands/discover.js +183 -0
- package/dist/cli/commands/discover.js.map +1 -0
- package/dist/cli/commands/invoke.d.ts +3 -0
- package/dist/cli/commands/invoke.d.ts.map +1 -0
- package/dist/cli/commands/invoke.js +142 -0
- package/dist/cli/commands/invoke.js.map +1 -0
- package/dist/cli/commands/mcp.d.ts +3 -0
- package/dist/cli/commands/mcp.d.ts.map +1 -0
- package/dist/cli/commands/mcp.js +113 -0
- package/dist/cli/commands/mcp.js.map +1 -0
- package/dist/cli/commands/open.d.ts +3 -0
- package/dist/cli/commands/open.d.ts.map +1 -0
- package/dist/cli/commands/open.js +331 -0
- package/dist/cli/commands/open.js.map +1 -0
- package/dist/cli/commands/reclaim-unverified.d.ts +3 -0
- package/dist/cli/commands/reclaim-unverified.d.ts.map +1 -0
- package/dist/cli/commands/reclaim-unverified.js +172 -0
- package/dist/cli/commands/reclaim-unverified.js.map +1 -0
- package/dist/cli/commands/reclaim.d.ts +33 -0
- package/dist/cli/commands/reclaim.d.ts.map +1 -0
- package/dist/cli/commands/reclaim.js +308 -0
- package/dist/cli/commands/reclaim.js.map +1 -0
- package/dist/cli/commands/status.d.ts +3 -0
- package/dist/cli/commands/status.d.ts.map +1 -0
- package/dist/cli/commands/status.js +43 -0
- package/dist/cli/commands/status.js.map +1 -0
- package/dist/cli/commands/streams.d.ts +3 -0
- package/dist/cli/commands/streams.d.ts.map +1 -0
- package/dist/cli/commands/streams.js +257 -0
- package/dist/cli/commands/streams.js.map +1 -0
- package/dist/cli/commands/watch.d.ts +3 -0
- package/dist/cli/commands/watch.d.ts.map +1 -0
- package/dist/cli/commands/watch.js +55 -0
- package/dist/cli/commands/watch.js.map +1 -0
- package/dist/cli/config.d.ts +61 -0
- package/dist/cli/config.d.ts.map +1 -0
- package/dist/cli/config.js +140 -0
- package/dist/cli/config.js.map +1 -0
- package/dist/cli/context.d.ts +29 -0
- package/dist/cli/context.d.ts.map +1 -0
- package/dist/cli/context.js +2 -0
- package/dist/cli/context.js.map +1 -0
- package/dist/cli/deps.d.ts +191 -0
- package/dist/cli/deps.d.ts.map +1 -0
- package/dist/cli/deps.js +277 -0
- package/dist/cli/deps.js.map +1 -0
- package/dist/cli/descriptor.d.ts +61 -0
- package/dist/cli/descriptor.d.ts.map +1 -0
- package/dist/cli/descriptor.js +78 -0
- package/dist/cli/descriptor.js.map +1 -0
- package/dist/cli/errors.d.ts +48 -0
- package/dist/cli/errors.d.ts.map +1 -0
- package/dist/cli/errors.js +129 -0
- package/dist/cli/errors.js.map +1 -0
- package/dist/cli/escape-hatch.d.ts +50 -0
- package/dist/cli/escape-hatch.d.ts.map +1 -0
- package/dist/cli/escape-hatch.js +45 -0
- package/dist/cli/escape-hatch.js.map +1 -0
- package/dist/cli/index.d.ts +8 -0
- package/dist/cli/index.d.ts.map +1 -0
- package/dist/cli/index.js +7 -0
- package/dist/cli/index.js.map +1 -0
- package/dist/cli/listings.d.ts +15 -0
- package/dist/cli/listings.d.ts.map +1 -0
- package/dist/cli/listings.js +56 -0
- package/dist/cli/listings.js.map +1 -0
- package/dist/cli/mandate.d.ts +66 -0
- package/dist/cli/mandate.d.ts.map +1 -0
- package/dist/cli/mandate.js +61 -0
- package/dist/cli/mandate.js.map +1 -0
- package/dist/cli/output.d.ts +33 -0
- package/dist/cli/output.d.ts.map +1 -0
- package/dist/cli/output.js +34 -0
- package/dist/cli/output.js.map +1 -0
- package/dist/cli/preflight.d.ts +38 -0
- package/dist/cli/preflight.d.ts.map +1 -0
- package/dist/cli/preflight.js +72 -0
- package/dist/cli/preflight.js.map +1 -0
- package/dist/cli/privy.d.ts +83 -0
- package/dist/cli/privy.d.ts.map +1 -0
- package/dist/cli/privy.js +230 -0
- package/dist/cli/privy.js.map +1 -0
- package/dist/cli/progress.d.ts +36 -0
- package/dist/cli/progress.d.ts.map +1 -0
- package/dist/cli/progress.js +40 -0
- package/dist/cli/progress.js.map +1 -0
- package/dist/cli/run.d.ts +25 -0
- package/dist/cli/run.d.ts.map +1 -0
- package/dist/cli/run.js +173 -0
- package/dist/cli/run.js.map +1 -0
- package/dist/cli/scan-range.d.ts +55 -0
- package/dist/cli/scan-range.d.ts.map +1 -0
- package/dist/cli/scan-range.js +57 -0
- package/dist/cli/scan-range.js.map +1 -0
- package/dist/cli/spend-policy.d.ts +60 -0
- package/dist/cli/spend-policy.d.ts.map +1 -0
- package/dist/cli/spend-policy.js +65 -0
- package/dist/cli/spend-policy.js.map +1 -0
- package/dist/cli/status.d.ts +90 -0
- package/dist/cli/status.d.ts.map +1 -0
- package/dist/cli/status.js +252 -0
- package/dist/cli/status.js.map +1 -0
- package/dist/cli/timeout.d.ts +30 -0
- package/dist/cli/timeout.d.ts.map +1 -0
- package/dist/cli/timeout.js +55 -0
- package/dist/cli/timeout.js.map +1 -0
- package/dist/cli/version.d.ts +12 -0
- package/dist/cli/version.d.ts.map +1 -0
- package/dist/cli/version.js +22 -0
- package/dist/cli/version.js.map +1 -0
- package/dist/cli/wallet.d.ts +164 -0
- package/dist/cli/wallet.d.ts.map +1 -0
- package/dist/cli/wallet.js +234 -0
- package/dist/cli/wallet.js.map +1 -0
- package/dist/frameworks/crewai.d.ts +10 -0
- package/dist/frameworks/crewai.d.ts.map +1 -1
- package/dist/frameworks/crewai.js +12 -2
- package/dist/frameworks/crewai.js.map +1 -1
- package/dist/gateway/caller-auth-gateway.d.ts +38 -3
- package/dist/gateway/caller-auth-gateway.d.ts.map +1 -1
- package/dist/gateway/caller-auth-gateway.js +25 -1
- package/dist/gateway/caller-auth-gateway.js.map +1 -1
- package/dist/gateway/http-server.d.ts +53 -0
- package/dist/gateway/http-server.d.ts.map +1 -1
- package/dist/gateway/http-server.js +189 -4
- package/dist/gateway/http-server.js.map +1 -1
- package/dist/gateway/server-entry.d.ts +1 -0
- package/dist/gateway/server-entry.js +1 -0
- package/dist/gateway/server-entry.js.map +1 -1
- package/dist/gateway/x402-mode.d.ts +123 -0
- package/dist/gateway/x402-mode.d.ts.map +1 -0
- package/dist/gateway/x402-mode.js +212 -0
- package/dist/gateway/x402-mode.js.map +1 -0
- package/dist/index.d.ts +6 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +8 -3
- package/dist/index.js.map +1 -1
- package/dist/mandates/env.d.ts +3 -2
- package/dist/mandates/env.d.ts.map +1 -1
- package/dist/mandates/env.js +3 -2
- package/dist/mandates/env.js.map +1 -1
- package/dist/mcp/http-server.d.ts +1 -0
- package/dist/mcp/http-server.js +1 -0
- package/dist/mcp/http-server.js.map +1 -1
- package/dist/mcp/server.d.ts +49 -0
- package/dist/mcp/server.d.ts.map +1 -1
- package/dist/mcp/server.js +533 -25
- package/dist/mcp/server.js.map +1 -1
- package/dist/mcp/service-invoker.d.ts +97 -0
- package/dist/mcp/service-invoker.d.ts.map +1 -0
- package/dist/mcp/service-invoker.js +167 -0
- package/dist/mcp/service-invoker.js.map +1 -0
- package/dist/mcp/stdio.d.ts +1 -0
- package/dist/mcp/stdio.js +1 -0
- package/dist/mcp/stdio.js.map +1 -1
- package/dist/sdk/client.d.ts +25 -1
- package/dist/sdk/client.d.ts.map +1 -1
- package/dist/sdk/client.js +20 -1
- package/dist/sdk/client.js.map +1 -1
- package/dist/sdk/invoke.d.ts +19 -4
- package/dist/sdk/invoke.d.ts.map +1 -1
- package/dist/sdk/invoke.js +69 -5
- package/dist/sdk/invoke.js.map +1 -1
- package/dist/x402/delivery-oracle.d.ts +108 -0
- package/dist/x402/delivery-oracle.d.ts.map +1 -0
- package/dist/x402/delivery-oracle.js +174 -0
- package/dist/x402/delivery-oracle.js.map +1 -0
- package/dist/x402/facilitator-server-entry.d.ts +3 -0
- package/dist/x402/facilitator-server-entry.d.ts.map +1 -0
- package/dist/x402/facilitator-server-entry.js +27 -0
- package/dist/x402/facilitator-server-entry.js.map +1 -0
- package/dist/x402/facilitator-server.d.ts +73 -0
- package/dist/x402/facilitator-server.d.ts.map +1 -0
- package/dist/x402/facilitator-server.js +305 -0
- package/dist/x402/facilitator-server.js.map +1 -0
- package/dist/x402/facilitator.d.ts +69 -17
- package/dist/x402/facilitator.d.ts.map +1 -1
- package/dist/x402/facilitator.js +76 -33
- package/dist/x402/facilitator.js.map +1 -1
- package/dist/x402/payment-requirements.d.ts +106 -0
- package/dist/x402/payment-requirements.d.ts.map +1 -0
- package/dist/x402/payment-requirements.js +138 -0
- package/dist/x402/payment-requirements.js.map +1 -0
- package/dist/x402/relayer.d.ts +41 -0
- package/dist/x402/relayer.d.ts.map +1 -0
- package/dist/x402/relayer.js +64 -0
- package/dist/x402/relayer.js.map +1 -0
- package/dist/x402/resource-index.d.ts +75 -0
- package/dist/x402/resource-index.d.ts.map +1 -0
- package/dist/x402/resource-index.js +126 -0
- package/dist/x402/resource-index.js.map +1 -0
- package/dist/x402/verified-delivery-facilitator.d.ts +285 -0
- package/dist/x402/verified-delivery-facilitator.d.ts.map +1 -0
- package/dist/x402/verified-delivery-facilitator.js +423 -0
- package/dist/x402/verified-delivery-facilitator.js.map +1 -0
- package/dist/zktls/t2-delivery-proof.d.ts +6 -6
- package/docs/agent-layer.md +14 -9
- package/docs/cli.md +705 -0
- package/docs/crewai.md +6 -2
- package/docs/eliza.md +6 -1
- package/docs/mcp-hosted.md +7 -5
- package/docs/mcp-reference.md +175 -0
- package/docs/quickstart.md +36 -9
- package/docs/threat-model.md +4 -3
- package/docs/x402.md +648 -0
- package/package.json +33 -10
- package/src/capability/invocation-capability.ts +17 -1
- package/src/cli/amounts.ts +102 -0
- package/src/cli/args.ts +150 -0
- package/src/cli/attesters.ts +86 -0
- package/src/cli/bin.ts +5 -0
- package/src/cli/commands/claim.ts +126 -0
- package/src/cli/commands/close.ts +69 -0
- package/src/cli/commands/discover.ts +228 -0
- package/src/cli/commands/invoke.ts +178 -0
- package/src/cli/commands/mcp.ts +135 -0
- package/src/cli/commands/open.ts +397 -0
- package/src/cli/commands/reclaim-unverified.ts +218 -0
- package/src/cli/commands/reclaim.ts +377 -0
- package/src/cli/commands/status.ts +56 -0
- package/src/cli/commands/streams.ts +344 -0
- package/src/cli/commands/watch.ts +70 -0
- package/src/cli/config.ts +208 -0
- package/src/cli/context.ts +30 -0
- package/src/cli/deps.ts +528 -0
- package/src/cli/descriptor.ts +133 -0
- package/src/cli/errors.ts +162 -0
- package/src/cli/escape-hatch.ts +92 -0
- package/src/cli/index.ts +27 -0
- package/src/cli/listings.ts +77 -0
- package/src/cli/mandate.ts +126 -0
- package/src/cli/output.ts +63 -0
- package/src/cli/preflight.ts +88 -0
- package/src/cli/privy.ts +396 -0
- package/src/cli/progress.ts +75 -0
- package/src/cli/run.ts +209 -0
- package/src/cli/scan-range.ts +76 -0
- package/src/cli/spend-policy.ts +139 -0
- package/src/cli/status.ts +381 -0
- package/src/cli/timeout.ts +64 -0
- package/src/cli/version.ts +21 -0
- package/src/cli/wallet.ts +372 -0
- package/src/frameworks/crewai.ts +13 -2
- package/src/gateway/caller-auth-gateway.ts +77 -6
- package/src/gateway/http-server.ts +280 -6
- package/src/gateway/server-entry.ts +1 -0
- package/src/gateway/x402-mode.ts +350 -0
- package/src/index.ts +40 -5
- package/src/mandates/env.ts +3 -2
- package/src/mcp/http-server.ts +1 -0
- package/src/mcp/server.ts +706 -30
- package/src/mcp/service-invoker.ts +285 -0
- package/src/mcp/stdio.ts +1 -0
- package/src/sdk/client.ts +33 -1
- package/src/sdk/invoke.ts +90 -12
- package/src/x402/delivery-oracle.ts +285 -0
- package/src/x402/facilitator-server-entry.ts +35 -0
- package/src/x402/facilitator-server.ts +445 -0
- package/src/x402/facilitator.ts +125 -46
- package/src/x402/payment-requirements.ts +231 -0
- package/src/x402/relayer.ts +122 -0
- package/src/x402/resource-index.ts +228 -0
- package/src/x402/verified-delivery-facilitator.ts +744 -0
package/src/cli/privy.ts
ADDED
|
@@ -0,0 +1,396 @@
|
|
|
1
|
+
import {
|
|
2
|
+
custom,
|
|
3
|
+
isAddress,
|
|
4
|
+
getAddress,
|
|
5
|
+
type Account,
|
|
6
|
+
type Transport,
|
|
7
|
+
} from "viem";
|
|
8
|
+
|
|
9
|
+
import {
|
|
10
|
+
AutonomousWalletError,
|
|
11
|
+
EncryptedFileCredentialStore,
|
|
12
|
+
HttpAutonomousWalletBroker,
|
|
13
|
+
METRIK_AUTONOMOUS_WALLET_BROKER_URL,
|
|
14
|
+
METRIK_WALLET_ENCRYPTION_KEY_ENV,
|
|
15
|
+
createAutonomousWalletProvider,
|
|
16
|
+
createDefaultAutonomousWalletStore,
|
|
17
|
+
type AutonomousWalletCredentialStore,
|
|
18
|
+
type AutonomousWalletRecord,
|
|
19
|
+
} from "../wallet/autonomous-wallet.js";
|
|
20
|
+
import { createPrivySessionProvider } from "../wallet/privy-session-provider.js";
|
|
21
|
+
import {
|
|
22
|
+
createPrivyEmbeddedAccount,
|
|
23
|
+
type PrivyEip1193Provider,
|
|
24
|
+
} from "../wallet/provider.js";
|
|
25
|
+
import { configError } from "./errors.js";
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* The CLI's two Privy wallet shapes.
|
|
29
|
+
*
|
|
30
|
+
* Privy differs from the private-key and CDP paths in ONE structural way: the
|
|
31
|
+
* account it yields has no local `signTransaction`. It signs and broadcasts by
|
|
32
|
+
* sending `eth_sendTransaction` / `eth_signTypedData_v4` to a provider, so the
|
|
33
|
+
* viem wallet client's TRANSPORT must be that provider rather than a plain HTTP
|
|
34
|
+
* RPC. That is the whole difference, and it is what `transport` below carries.
|
|
35
|
+
*
|
|
36
|
+
* Neither shape puts a wallet key on this machine. The signing key lives at
|
|
37
|
+
* Privy; the broker holds the Privy authorization key. What the CLI holds is
|
|
38
|
+
* either a short-lived session token (`session`) or a P-256 authorization key
|
|
39
|
+
* in the host credential store (`autonomous`) — a key that authorizes a
|
|
40
|
+
* policy-bounded request, not a key that can move funds by itself.
|
|
41
|
+
*/
|
|
42
|
+
export type PrivyCliWalletSource = "privy-autonomous" | "privy-session";
|
|
43
|
+
|
|
44
|
+
/** The store key naming the agent-owned wallet in the host credential store. */
|
|
45
|
+
export const PRIVY_WALLET_KEY_ENV = "METRIK_AGENT_PRIVY_WALLET_KEY";
|
|
46
|
+
/** Broker base URL. Optional for `autonomous`, REQUIRED for `session`. */
|
|
47
|
+
export const PRIVY_BROKER_URL_ENV = "METRIK_AGENT_PRIVY_BROKER_URL";
|
|
48
|
+
/** A scoped session token minted by the broker's `POST /v1/sessions`. */
|
|
49
|
+
export const PRIVY_SESSION_TOKEN_ENV = "METRIK_AGENT_PRIVY_SESSION_TOKEN";
|
|
50
|
+
/** The embedded wallet address the session is bound to. */
|
|
51
|
+
export const PRIVY_ADDRESS_ENV = "METRIK_AGENT_PRIVY_ADDRESS";
|
|
52
|
+
/** The PUBLIC Privy app id. Not a secret — the broker serves it at `/v1/config`. */
|
|
53
|
+
export const PRIVY_APP_ID_ENV = "METRIK_AGENT_PRIVY_APP_ID";
|
|
54
|
+
|
|
55
|
+
/** Every variable either Privy shape reads. Named in errors, never printed. */
|
|
56
|
+
export const PRIVY_ENV_VARS = [
|
|
57
|
+
PRIVY_WALLET_KEY_ENV,
|
|
58
|
+
PRIVY_BROKER_URL_ENV,
|
|
59
|
+
PRIVY_SESSION_TOKEN_ENV,
|
|
60
|
+
PRIVY_ADDRESS_ENV,
|
|
61
|
+
PRIVY_APP_ID_ENV,
|
|
62
|
+
] as const;
|
|
63
|
+
|
|
64
|
+
/** The variables the user-owned session shape needs, all of them. */
|
|
65
|
+
export const PRIVY_SESSION_ENV_VARS = [
|
|
66
|
+
PRIVY_BROKER_URL_ENV,
|
|
67
|
+
PRIVY_SESSION_TOKEN_ENV,
|
|
68
|
+
PRIVY_ADDRESS_ENV,
|
|
69
|
+
PRIVY_APP_ID_ENV,
|
|
70
|
+
] as const;
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* The spend ceiling a broker enforces server-side, read from `GET /v1/config`.
|
|
74
|
+
*
|
|
75
|
+
* These are the SAME numbers the broker checks `openStream` against
|
|
76
|
+
* (`deposit <= maxTotalUsdc`, `ratePerSecond <= maxRatePerSecondUsdc`,
|
|
77
|
+
* `duration <= maxDurationSeconds`), which is why the CLI can use them to
|
|
78
|
+
* refuse an over-ceiling hire locally instead of paying gas to learn a knowable
|
|
79
|
+
* fact. It reads them rather than hardcoding them: the ceiling belongs to the
|
|
80
|
+
* broker, and a copy in this file would be a second source of truth that goes
|
|
81
|
+
* stale silently.
|
|
82
|
+
*/
|
|
83
|
+
export interface PrivySpendPolicy {
|
|
84
|
+
readonly brokerUrl: string;
|
|
85
|
+
readonly chainId: number;
|
|
86
|
+
readonly maxTotalUsdc: bigint;
|
|
87
|
+
readonly maxRatePerSecondUsdc: bigint;
|
|
88
|
+
readonly maxDurationSeconds: number;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** Reads a broker's public policy. No credential is sent; nothing secret is served. */
|
|
92
|
+
export async function fetchPrivySpendPolicy(
|
|
93
|
+
brokerUrl: string,
|
|
94
|
+
fetchImpl: typeof fetch = fetch,
|
|
95
|
+
): Promise<PrivySpendPolicy> {
|
|
96
|
+
const base = brokerUrl.replace(/\/$/, "");
|
|
97
|
+
const response = await fetchImpl(`${base}/v1/config`, {
|
|
98
|
+
headers: { accept: "application/json" },
|
|
99
|
+
}).catch((error: unknown) => {
|
|
100
|
+
throw configError(
|
|
101
|
+
`the Privy broker at ${base} did not answer its public policy`,
|
|
102
|
+
`${describe(error)} The CLI reads GET /v1/config to know the ceiling your wallet is capped at; without it, it cannot tell you before broadcasting whether a hire fits.`,
|
|
103
|
+
);
|
|
104
|
+
});
|
|
105
|
+
const body = (await response.json().catch(() => null)) as Record<
|
|
106
|
+
string,
|
|
107
|
+
unknown
|
|
108
|
+
> | null;
|
|
109
|
+
if (!response.ok || body === null) {
|
|
110
|
+
throw configError(
|
|
111
|
+
`the Privy broker at ${base} rejected the public policy read (${response.status})`,
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
const chainId = Number(body.chainId);
|
|
115
|
+
const maxTotalUsdc = asBigint(body.maxTotalUsdcAtomic);
|
|
116
|
+
const maxRatePerSecondUsdc = asBigint(body.maxRatePerSecondAtomic);
|
|
117
|
+
const maxDurationSeconds = Number(body.maxDurationSeconds);
|
|
118
|
+
if (
|
|
119
|
+
!Number.isSafeInteger(chainId) ||
|
|
120
|
+
maxTotalUsdc === null ||
|
|
121
|
+
maxRatePerSecondUsdc === null ||
|
|
122
|
+
!Number.isSafeInteger(maxDurationSeconds)
|
|
123
|
+
) {
|
|
124
|
+
throw configError(
|
|
125
|
+
`the Privy broker at ${base} returned a policy this CLI cannot read`,
|
|
126
|
+
"expected chainId, maxTotalUsdcAtomic, maxRatePerSecondAtomic and maxDurationSeconds.",
|
|
127
|
+
);
|
|
128
|
+
}
|
|
129
|
+
return {
|
|
130
|
+
brokerUrl: base,
|
|
131
|
+
chainId,
|
|
132
|
+
maxTotalUsdc,
|
|
133
|
+
maxRatePerSecondUsdc,
|
|
134
|
+
maxDurationSeconds,
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
function asBigint(value: unknown): bigint | null {
|
|
139
|
+
if (typeof value === "bigint") return value;
|
|
140
|
+
if (typeof value === "number" && Number.isSafeInteger(value))
|
|
141
|
+
return BigInt(value);
|
|
142
|
+
if (typeof value === "string" && /^[0-9]+$/.test(value)) return BigInt(value);
|
|
143
|
+
return null;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
export interface ResolvedPrivyCliWallet {
|
|
147
|
+
readonly source: PrivyCliWalletSource;
|
|
148
|
+
/** A JSON-RPC account: `signTypedData` works, `signTransaction` does not exist. */
|
|
149
|
+
readonly account: Account;
|
|
150
|
+
/** The provider-as-transport. Without this every write goes to a plain RPC and fails. */
|
|
151
|
+
readonly transport: Transport;
|
|
152
|
+
/** One line naming the wallet's origin. Safe to print: no credential in it. */
|
|
153
|
+
readonly description: string;
|
|
154
|
+
/** Reads the broker ceiling this wallet is capped at. Bound to its broker URL. */
|
|
155
|
+
readonly readSpendPolicy: () => Promise<PrivySpendPolicy>;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
export interface ResolvePrivyCliWalletOptions {
|
|
159
|
+
/** `--rpc-url`, so a Privy wallet's READS use the same endpoint as everything else. */
|
|
160
|
+
readonly rpcUrl?: string;
|
|
161
|
+
readonly note?: (line: string) => void;
|
|
162
|
+
/** Test seam. Defaults to the real credential store selected from `env`. */
|
|
163
|
+
readonly store?: AutonomousWalletCredentialStore;
|
|
164
|
+
/** Test seam for the broker HTTP contract. */
|
|
165
|
+
readonly fetchImpl?: typeof fetch;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Resolves a Privy wallet from the environment, or `null` when no Privy
|
|
170
|
+
* variable is set at all.
|
|
171
|
+
*
|
|
172
|
+
* A PARTIALLY configured Privy setup is an error, not a `null` — "you set the
|
|
173
|
+
* session token but not the address" and "you configured no Privy wallet" are
|
|
174
|
+
* different facts, and rendering the first as the second sends the reader
|
|
175
|
+
* looking in the wrong place. This mirrors the CDP path exactly.
|
|
176
|
+
*/
|
|
177
|
+
export async function resolvePrivyCliWallet(
|
|
178
|
+
env: NodeJS.ProcessEnv,
|
|
179
|
+
options: ResolvePrivyCliWalletOptions = {},
|
|
180
|
+
): Promise<ResolvedPrivyCliWallet | null> {
|
|
181
|
+
const walletKey = read(env, PRIVY_WALLET_KEY_ENV);
|
|
182
|
+
const brokerUrl = read(env, PRIVY_BROKER_URL_ENV);
|
|
183
|
+
const sessionToken = read(env, PRIVY_SESSION_TOKEN_ENV);
|
|
184
|
+
const address = read(env, PRIVY_ADDRESS_ENV);
|
|
185
|
+
const appId = read(env, PRIVY_APP_ID_ENV);
|
|
186
|
+
|
|
187
|
+
if (
|
|
188
|
+
walletKey === null &&
|
|
189
|
+
brokerUrl === null &&
|
|
190
|
+
sessionToken === null &&
|
|
191
|
+
address === null &&
|
|
192
|
+
appId === null
|
|
193
|
+
) {
|
|
194
|
+
return null;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
if (walletKey !== null) {
|
|
198
|
+
if (sessionToken !== null) {
|
|
199
|
+
options.note?.(
|
|
200
|
+
`warning: both ${PRIVY_WALLET_KEY_ENV} and ${PRIVY_SESSION_TOKEN_ENV} are set; using the agent-owned wallet named by ${PRIVY_WALLET_KEY_ENV}. Unset it to sign with the session token.`,
|
|
201
|
+
);
|
|
202
|
+
}
|
|
203
|
+
return await resolveAutonomous(walletKey, brokerUrl, env, options);
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
const missing = PRIVY_SESSION_ENV_VARS.filter(
|
|
207
|
+
(name) => read(env, name) === null,
|
|
208
|
+
);
|
|
209
|
+
if (missing.length > 0) {
|
|
210
|
+
throw configError(
|
|
211
|
+
`the Privy wallet is partially configured: ${missing.join(", ")} ${missing.length === 1 ? "is" : "are"} not set`,
|
|
212
|
+
`a Privy wallet is configured one of two ways: set ${PRIVY_WALLET_KEY_ENV} to the store key of an agent-owned wallet you provisioned with provisionMetrikAutonomousWallet(), or set all of ${PRIVY_SESSION_ENV_VARS.join(", ")} for a broker session. Set the missing one${missing.length === 1 ? "" : "s"}, or unset the others to use a different wallet.`,
|
|
213
|
+
);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
return resolveSession({
|
|
217
|
+
brokerUrl: brokerUrl as string,
|
|
218
|
+
sessionToken: sessionToken as string,
|
|
219
|
+
address: address as string,
|
|
220
|
+
appId: appId as string,
|
|
221
|
+
...(options.rpcUrl === undefined ? {} : { rpcUrl: options.rpcUrl }),
|
|
222
|
+
...(options.fetchImpl === undefined
|
|
223
|
+
? {}
|
|
224
|
+
: { fetchImpl: options.fetchImpl }),
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
async function resolveAutonomous(
|
|
229
|
+
walletKey: string,
|
|
230
|
+
brokerUrlOverride: string | null,
|
|
231
|
+
env: NodeJS.ProcessEnv,
|
|
232
|
+
options: ResolvePrivyCliWalletOptions,
|
|
233
|
+
): Promise<ResolvedPrivyCliWallet> {
|
|
234
|
+
const store = options.store ?? selectCredentialStore(env);
|
|
235
|
+
let record: AutonomousWalletRecord | null;
|
|
236
|
+
try {
|
|
237
|
+
record = await store.load(walletKey);
|
|
238
|
+
} catch (error) {
|
|
239
|
+
throw configError(
|
|
240
|
+
`could not read the agent-owned Privy wallet "${walletKey}" from the credential store`,
|
|
241
|
+
`${describe(error)} On a headless host the OS credential store is often unavailable; set ${METRIK_WALLET_ENCRYPTION_KEY_ENV} to use the encrypted-file store instead.`,
|
|
242
|
+
);
|
|
243
|
+
}
|
|
244
|
+
if (record === null) {
|
|
245
|
+
throw configError(
|
|
246
|
+
`no agent-owned Privy wallet is stored under the key "${walletKey}"`,
|
|
247
|
+
`the CLI never provisions a wallet on your behalf — a fund-moving command is the wrong place to mint one. Provision it once with provisionMetrikAutonomousWallet({ storeKey: "${walletKey}" }) (see docs/autonomous-privy-wallet.md), then re-run this command.`,
|
|
248
|
+
);
|
|
249
|
+
}
|
|
250
|
+
if (record.walletId === undefined || record.address === undefined) {
|
|
251
|
+
throw configError(
|
|
252
|
+
`the agent-owned Privy wallet "${walletKey}" was never finished provisioning`,
|
|
253
|
+
"the local key exists but the broker never returned a wallet id and address. Re-run provisionMetrikAutonomousWallet() with the same store key — it retries with the same key and idempotency nonce.",
|
|
254
|
+
);
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
const brokerUrl = brokerUrlOverride ?? record.brokerUrl;
|
|
258
|
+
if (brokerUrlOverride !== null && brokerUrlOverride !== record.brokerUrl) {
|
|
259
|
+
options.note?.(
|
|
260
|
+
`note: ${PRIVY_BROKER_URL_ENV} (${brokerUrlOverride}) overrides the broker this wallet was provisioned against (${record.brokerUrl}); only a broker holding this wallet's Privy authorization key can authorize it.`,
|
|
261
|
+
);
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
let broker: HttpAutonomousWalletBroker;
|
|
265
|
+
try {
|
|
266
|
+
broker = new HttpAutonomousWalletBroker(brokerUrl, {
|
|
267
|
+
...(options.fetchImpl === undefined
|
|
268
|
+
? {}
|
|
269
|
+
: { fetchImpl: options.fetchImpl }),
|
|
270
|
+
});
|
|
271
|
+
} catch (error) {
|
|
272
|
+
throw configError(
|
|
273
|
+
`the autonomous wallet broker URL is not usable: ${describe(error)}`,
|
|
274
|
+
`set ${PRIVY_BROKER_URL_ENV} to an HTTPS broker you control (the default is ${METRIK_AUTONOMOUS_WALLET_BROKER_URL}).`,
|
|
275
|
+
);
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
const provider = createAutonomousWalletProvider({
|
|
279
|
+
wallet: record,
|
|
280
|
+
broker,
|
|
281
|
+
...(options.rpcUrl === undefined ? {} : { rpcUrl: options.rpcUrl }),
|
|
282
|
+
...(options.fetchImpl === undefined
|
|
283
|
+
? {}
|
|
284
|
+
: { fetchImpl: options.fetchImpl }),
|
|
285
|
+
});
|
|
286
|
+
const account = await embeddedAccount(provider, record.appId, record.address);
|
|
287
|
+
|
|
288
|
+
return {
|
|
289
|
+
source: "privy-autonomous",
|
|
290
|
+
account,
|
|
291
|
+
transport: custom(provider),
|
|
292
|
+
description: `${record.address} (agent-owned Privy wallet "${walletKey}" via ${brokerUrl})`,
|
|
293
|
+
readSpendPolicy: async () =>
|
|
294
|
+
await fetchPrivySpendPolicy(
|
|
295
|
+
brokerUrl,
|
|
296
|
+
options.fetchImpl ?? globalThis.fetch.bind(globalThis),
|
|
297
|
+
),
|
|
298
|
+
};
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
function resolveSession(input: {
|
|
302
|
+
readonly brokerUrl: string;
|
|
303
|
+
readonly sessionToken: string;
|
|
304
|
+
readonly address: string;
|
|
305
|
+
readonly appId: string;
|
|
306
|
+
readonly rpcUrl?: string;
|
|
307
|
+
readonly fetchImpl?: typeof fetch;
|
|
308
|
+
}): Promise<ResolvedPrivyCliWallet> {
|
|
309
|
+
if (!isAddress(input.address)) {
|
|
310
|
+
throw configError(
|
|
311
|
+
`${PRIVY_ADDRESS_ENV} must be a 0x-prefixed 20-byte address`,
|
|
312
|
+
);
|
|
313
|
+
}
|
|
314
|
+
const address = getAddress(input.address);
|
|
315
|
+
let provider: PrivyEip1193Provider;
|
|
316
|
+
try {
|
|
317
|
+
provider = createPrivySessionProvider({
|
|
318
|
+
brokerUrl: input.brokerUrl,
|
|
319
|
+
sessionToken: input.sessionToken,
|
|
320
|
+
address,
|
|
321
|
+
...(input.rpcUrl === undefined ? {} : { rpcUrl: input.rpcUrl }),
|
|
322
|
+
...(input.fetchImpl === undefined ? {} : { fetchImpl: input.fetchImpl }),
|
|
323
|
+
});
|
|
324
|
+
} catch (error) {
|
|
325
|
+
throw configError(
|
|
326
|
+
`the Privy session broker URL is not usable: ${describe(error)}`,
|
|
327
|
+
`set ${PRIVY_BROKER_URL_ENV} to the HTTPS broker that minted ${PRIVY_SESSION_TOKEN_ENV}. A session token is only valid against the broker that issued it.`,
|
|
328
|
+
);
|
|
329
|
+
}
|
|
330
|
+
return embeddedAccount(provider, input.appId, address).then((account) => ({
|
|
331
|
+
source: "privy-session" as const,
|
|
332
|
+
account,
|
|
333
|
+
transport: custom(provider),
|
|
334
|
+
description: `${address} (Privy broker session via ${input.brokerUrl})`,
|
|
335
|
+
readSpendPolicy: async () =>
|
|
336
|
+
await fetchPrivySpendPolicy(
|
|
337
|
+
input.brokerUrl,
|
|
338
|
+
input.fetchImpl ?? globalThis.fetch.bind(globalThis),
|
|
339
|
+
),
|
|
340
|
+
}));
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
/**
|
|
344
|
+
* Wraps a provider in the package's existing Privy EOA adapter.
|
|
345
|
+
*
|
|
346
|
+
* Both CLI providers answer `eth_chainId` and `eth_accounts` locally, so the
|
|
347
|
+
* adapter's session assertion is a pure local consistency check here — no
|
|
348
|
+
* network call happens while the wallet is being resolved.
|
|
349
|
+
*/
|
|
350
|
+
async function embeddedAccount(
|
|
351
|
+
provider: PrivyEip1193Provider,
|
|
352
|
+
appId: string,
|
|
353
|
+
address: `0x${string}`,
|
|
354
|
+
): Promise<Account> {
|
|
355
|
+
try {
|
|
356
|
+
return await createPrivyEmbeddedAccount({
|
|
357
|
+
appId,
|
|
358
|
+
address,
|
|
359
|
+
provider,
|
|
360
|
+
chainId: 84532,
|
|
361
|
+
});
|
|
362
|
+
} catch (error) {
|
|
363
|
+
throw configError(
|
|
364
|
+
`the Privy wallet could not be prepared: ${describe(error)}`,
|
|
365
|
+
`check ${PRIVY_ENV_VARS.join(", ")}. Metrik Privy wallets are Base Sepolia (84532) only.`,
|
|
366
|
+
);
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
/**
|
|
371
|
+
* The credential store the autonomous wallet was provisioned into.
|
|
372
|
+
*
|
|
373
|
+
* Selection mirrors `provisionMetrikAutonomousWallet`'s documented options:
|
|
374
|
+
* setting `METRIK_WALLET_ENCRYPTION_KEY` means the encrypted-file store (the
|
|
375
|
+
* durable option for a headless host), otherwise the host OS store. Picking a
|
|
376
|
+
* different store than provisioning used simply finds no record, and the
|
|
377
|
+
* "no wallet is stored under this key" error above says so.
|
|
378
|
+
*/
|
|
379
|
+
function selectCredentialStore(
|
|
380
|
+
env: NodeJS.ProcessEnv,
|
|
381
|
+
): AutonomousWalletCredentialStore {
|
|
382
|
+
if (read(env, METRIK_WALLET_ENCRYPTION_KEY_ENV) !== null) {
|
|
383
|
+
return new EncryptedFileCredentialStore({ env });
|
|
384
|
+
}
|
|
385
|
+
return createDefaultAutonomousWalletStore();
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
function read(env: NodeJS.ProcessEnv, name: string): string | null {
|
|
389
|
+
const value = env[name];
|
|
390
|
+
return value === undefined || value.length === 0 ? null : value;
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
function describe(error: unknown): string {
|
|
394
|
+
if (error instanceof AutonomousWalletError) return error.message;
|
|
395
|
+
return error instanceof Error ? error.message : String(error);
|
|
396
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A heartbeat on stderr for commands that can take a while.
|
|
3
|
+
*
|
|
4
|
+
* A live acceptance run saw `close` take 8s once and 142s the next time, and
|
|
5
|
+
* `status` calls run past two minutes with nothing on screen. With no output at
|
|
6
|
+
* all, a slow RPC and a wedged process look identical, and the only safe reading
|
|
7
|
+
* of that ambiguity — on a command that may have already broadcast a
|
|
8
|
+
* transaction — is the expensive one: wait, or kill it and risk double-sending.
|
|
9
|
+
*
|
|
10
|
+
* So the CLI narrates. Not progress towards a known total (there isn't one), but
|
|
11
|
+
* proof of life plus elapsed time, which is what distinguishes "slow" from
|
|
12
|
+
* "stuck". stderr in BOTH modes, so `--json` stdout stays a single parseable
|
|
13
|
+
* document.
|
|
14
|
+
*/
|
|
15
|
+
export interface Heartbeat {
|
|
16
|
+
/** Stops the ticker. Safe to call more than once. */
|
|
17
|
+
readonly stop: () => void;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** Silence before the first tick — below this, a command is simply fast. */
|
|
21
|
+
export const HEARTBEAT_DELAY_MS = 8_000;
|
|
22
|
+
/** Gap between ticks once it has started. */
|
|
23
|
+
export const HEARTBEAT_INTERVAL_MS = 10_000;
|
|
24
|
+
|
|
25
|
+
export interface HeartbeatOptions {
|
|
26
|
+
readonly delayMs?: number;
|
|
27
|
+
readonly intervalMs?: number;
|
|
28
|
+
/** Injectable for tests; defaults to the global timers. */
|
|
29
|
+
readonly setTimer?: typeof setTimeout;
|
|
30
|
+
readonly clearTimer?: typeof clearTimeout;
|
|
31
|
+
readonly now?: () => number;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Start narrating `label`. Nothing is printed unless the work outlasts
|
|
36
|
+
* `delayMs`, so a fast command is silent and a slow one is legible.
|
|
37
|
+
*/
|
|
38
|
+
export function startHeartbeat(
|
|
39
|
+
note: (line: string) => void,
|
|
40
|
+
label: string,
|
|
41
|
+
options: HeartbeatOptions = {},
|
|
42
|
+
): Heartbeat {
|
|
43
|
+
const delayMs = options.delayMs ?? HEARTBEAT_DELAY_MS;
|
|
44
|
+
const intervalMs = options.intervalMs ?? HEARTBEAT_INTERVAL_MS;
|
|
45
|
+
const setTimer = options.setTimer ?? setTimeout;
|
|
46
|
+
const clearTimer = options.clearTimer ?? clearTimeout;
|
|
47
|
+
const now = options.now ?? Date.now;
|
|
48
|
+
|
|
49
|
+
const startedAt = now();
|
|
50
|
+
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
51
|
+
let stopped = false;
|
|
52
|
+
|
|
53
|
+
const tick = (): void => {
|
|
54
|
+
if (stopped) return;
|
|
55
|
+
const elapsed = Math.round((now() - startedAt) / 1000);
|
|
56
|
+
note(
|
|
57
|
+
`… still working: ${label} (${elapsed}s elapsed). Slow RPC or oracle, not a hang — nothing is retried silently.`,
|
|
58
|
+
);
|
|
59
|
+
schedule(intervalMs);
|
|
60
|
+
};
|
|
61
|
+
const schedule = (ms: number): void => {
|
|
62
|
+
timer = setTimer(tick, ms);
|
|
63
|
+
// Never hold the process open on account of the narration.
|
|
64
|
+
(timer as { unref?: () => void }).unref?.();
|
|
65
|
+
};
|
|
66
|
+
schedule(delayMs);
|
|
67
|
+
|
|
68
|
+
return {
|
|
69
|
+
stop() {
|
|
70
|
+
stopped = true;
|
|
71
|
+
if (timer !== undefined) clearTimer(timer);
|
|
72
|
+
timer = undefined;
|
|
73
|
+
},
|
|
74
|
+
};
|
|
75
|
+
}
|
package/src/cli/run.ts
ADDED
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
import { assertKnownFlags, booleanFlag, parseArgs } from "./args.js";
|
|
2
|
+
import {
|
|
3
|
+
CONFIG_FLAGS,
|
|
4
|
+
GLOBAL_FLAGS,
|
|
5
|
+
TESTNET_CHAIN_IDS,
|
|
6
|
+
resolveConfig,
|
|
7
|
+
} from "./config.js";
|
|
8
|
+
import { CliError, describeError, usageError } from "./errors.js";
|
|
9
|
+
import { createPrinter, type CliStreams } from "./output.js";
|
|
10
|
+
import { startHeartbeat } from "./progress.js";
|
|
11
|
+
import { defaultCliDependencies, type CliDependencies } from "./deps.js";
|
|
12
|
+
import { packageVersion } from "./version.js";
|
|
13
|
+
import { CDP_ENV_VARS, PRIVATE_KEY_ENV } from "./wallet.js";
|
|
14
|
+
import type { CommandSpec } from "./context.js";
|
|
15
|
+
|
|
16
|
+
import { claimCommand } from "./commands/claim.js";
|
|
17
|
+
import { closeCommand } from "./commands/close.js";
|
|
18
|
+
import { discoverCommand } from "./commands/discover.js";
|
|
19
|
+
import { invokeCommand } from "./commands/invoke.js";
|
|
20
|
+
import { mcpCommand } from "./commands/mcp.js";
|
|
21
|
+
import { openCommand } from "./commands/open.js";
|
|
22
|
+
import { reclaimCommand } from "./commands/reclaim.js";
|
|
23
|
+
import { reclaimUnverifiedCommand } from "./commands/reclaim-unverified.js";
|
|
24
|
+
import { statusCommand } from "./commands/status.js";
|
|
25
|
+
import { streamsCommand } from "./commands/streams.js";
|
|
26
|
+
import { watchCommand } from "./commands/watch.js";
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* The command surface, in the order the product loop runs:
|
|
30
|
+
* discover -> open -> invoke -> watch/status -> close -> reclaim, then the
|
|
31
|
+
* operator's own redemption (`claim`) and the buyer's last-resort exit
|
|
32
|
+
* (`reclaim-unverified`). `streams` (which of them are mine?) sits inside that
|
|
33
|
+
* loop; `mcp` (run the same loop from an MCP host) sits after it.
|
|
34
|
+
*/
|
|
35
|
+
export const COMMANDS: readonly CommandSpec[] = [
|
|
36
|
+
discoverCommand,
|
|
37
|
+
openCommand,
|
|
38
|
+
statusCommand,
|
|
39
|
+
streamsCommand,
|
|
40
|
+
watchCommand,
|
|
41
|
+
invokeCommand,
|
|
42
|
+
closeCommand,
|
|
43
|
+
reclaimCommand,
|
|
44
|
+
reclaimUnverifiedCommand,
|
|
45
|
+
claimCommand,
|
|
46
|
+
mcpCommand,
|
|
47
|
+
];
|
|
48
|
+
|
|
49
|
+
export interface RunCliOptions {
|
|
50
|
+
readonly argv: readonly string[];
|
|
51
|
+
readonly env?: NodeJS.ProcessEnv;
|
|
52
|
+
readonly streams?: CliStreams;
|
|
53
|
+
readonly deps?: CliDependencies;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Runs one CLI invocation and returns the process exit code. Pure in its
|
|
58
|
+
* inputs: argv, env, output sinks and side-effecting dependencies are all
|
|
59
|
+
* injected, so the whole surface is testable without a key or an RPC.
|
|
60
|
+
*/
|
|
61
|
+
export async function runCli(options: RunCliOptions): Promise<number> {
|
|
62
|
+
const env = options.env ?? process.env;
|
|
63
|
+
const streams = options.streams ?? {
|
|
64
|
+
stdout: (line) => process.stdout.write(`${line}\n`),
|
|
65
|
+
stderr: (line) => process.stderr.write(`${line}\n`),
|
|
66
|
+
};
|
|
67
|
+
const deps = options.deps ?? defaultCliDependencies;
|
|
68
|
+
|
|
69
|
+
let json = false;
|
|
70
|
+
try {
|
|
71
|
+
const args = parseArgs(options.argv);
|
|
72
|
+
json = booleanFlag(args, "json");
|
|
73
|
+
const out = createPrinter(streams, json);
|
|
74
|
+
|
|
75
|
+
// `--version` is checked before the bare-invocation help branch: `metrik
|
|
76
|
+
// --version` parses to no command at all.
|
|
77
|
+
if (args.command === "version" || booleanFlag(args, "version")) {
|
|
78
|
+
streams.stdout(packageVersion());
|
|
79
|
+
return 0;
|
|
80
|
+
}
|
|
81
|
+
if (args.command === null || args.command === "help") {
|
|
82
|
+
// A misspelled TOP-LEVEL flag must fail like a misspelled per-command one
|
|
83
|
+
// does. Printing help and exiting 0 tells a script the invocation
|
|
84
|
+
// succeeded, which is how a typo goes unnoticed.
|
|
85
|
+
assertKnownFlags(args, [...GLOBAL_FLAGS, "version"]);
|
|
86
|
+
streams.stdout(renderHelp(args.positionals[0]));
|
|
87
|
+
return 0;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const command = COMMANDS.find((spec) => spec.name === args.command);
|
|
91
|
+
if (command === undefined) {
|
|
92
|
+
throw usageError(
|
|
93
|
+
`unknown command: ${args.command}`,
|
|
94
|
+
`commands: ${COMMANDS.map((spec) => spec.name).join(", ")}`,
|
|
95
|
+
);
|
|
96
|
+
}
|
|
97
|
+
if (booleanFlag(args, "help")) {
|
|
98
|
+
streams.stdout(renderHelp(command.name));
|
|
99
|
+
return 0;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
assertKnownFlags(args, [
|
|
103
|
+
...GLOBAL_FLAGS,
|
|
104
|
+
...CONFIG_FLAGS,
|
|
105
|
+
...command.flags,
|
|
106
|
+
]);
|
|
107
|
+
const config = resolveConfig(args, env);
|
|
108
|
+
// Every command gets the heartbeat, in one place rather than per command:
|
|
109
|
+
// any of them can be the one waiting on a slow endpoint, and a rule with an
|
|
110
|
+
// exception is a rule someone forgets to apply to the next command.
|
|
111
|
+
const heartbeat = startHeartbeat(out.note, `metrik ${command.name}`);
|
|
112
|
+
try {
|
|
113
|
+
await command.run({ args, config, env, out, deps });
|
|
114
|
+
} finally {
|
|
115
|
+
heartbeat.stop();
|
|
116
|
+
}
|
|
117
|
+
return 0;
|
|
118
|
+
} catch (error) {
|
|
119
|
+
if (error instanceof CliError) {
|
|
120
|
+
emitFailure(streams, json, error.message, error.hint, error.exitCode);
|
|
121
|
+
return error.exitCode;
|
|
122
|
+
}
|
|
123
|
+
emitFailure(streams, json, describeError(error), undefined, 4);
|
|
124
|
+
return 4;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
function emitFailure(
|
|
129
|
+
streams: CliStreams,
|
|
130
|
+
json: boolean,
|
|
131
|
+
message: string,
|
|
132
|
+
hint: string | undefined,
|
|
133
|
+
exitCode: number,
|
|
134
|
+
): void {
|
|
135
|
+
if (json) {
|
|
136
|
+
// The failure goes to stderr even in --json mode so stdout stays either a
|
|
137
|
+
// result document or nothing at all.
|
|
138
|
+
streams.stderr(
|
|
139
|
+
JSON.stringify({ error: message, hint: hint ?? null, exitCode }),
|
|
140
|
+
);
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
streams.stderr(`error: ${message}`);
|
|
144
|
+
if (hint !== undefined) streams.stderr(` ${hint}`);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
export function renderHelp(commandName?: string): string {
|
|
148
|
+
const command = COMMANDS.find((spec) => spec.name === commandName);
|
|
149
|
+
if (command !== undefined) {
|
|
150
|
+
return [
|
|
151
|
+
`metrik ${command.name} — ${command.summary}`,
|
|
152
|
+
"",
|
|
153
|
+
`Usage: ${command.usage}`,
|
|
154
|
+
"",
|
|
155
|
+
command.flags.length === 0
|
|
156
|
+
? "Command flags: (none)"
|
|
157
|
+
: `Command flags: ${command.flags.map((flag) => `--${flag}`).join(" ")}`,
|
|
158
|
+
`Settlement flags: ${CONFIG_FLAGS.map((flag) => `--${flag}`).join(" ")}`,
|
|
159
|
+
"Output flags: --json",
|
|
160
|
+
...(command.signs
|
|
161
|
+
? [
|
|
162
|
+
"",
|
|
163
|
+
`Broadcasts a transaction. Requires a wallet in the environment: ${PRIVATE_KEY_ENV}, or ${CDP_ENV_VARS.join(" + ")}.`,
|
|
164
|
+
]
|
|
165
|
+
: command.requiresKey === true
|
|
166
|
+
? [
|
|
167
|
+
"",
|
|
168
|
+
`Broadcasts nothing, but signs an off-chain EIP-712 capability, so it requires a wallet in the environment: ${PRIVATE_KEY_ENV}, or ${CDP_ENV_VARS.join(" + ")}.`,
|
|
169
|
+
]
|
|
170
|
+
: []),
|
|
171
|
+
].join("\n");
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
const width = COMMANDS.reduce(
|
|
175
|
+
(max, spec) => Math.max(max, spec.name.length),
|
|
176
|
+
0,
|
|
177
|
+
);
|
|
178
|
+
return [
|
|
179
|
+
`metrik ${packageVersion()} — pay for verified delivery, metered by the second`,
|
|
180
|
+
"",
|
|
181
|
+
"You rent a verified-live endpoint BY TIME, not per call: the oracle probes it and the",
|
|
182
|
+
"meter runs on availability that verified. One request and a thousand cost the same",
|
|
183
|
+
"second, and the clock keeps running while you send none. `metrik close` stops it.",
|
|
184
|
+
"",
|
|
185
|
+
"Usage: metrik <command> [options]",
|
|
186
|
+
"",
|
|
187
|
+
"Commands:",
|
|
188
|
+
...COMMANDS.map(
|
|
189
|
+
(spec) =>
|
|
190
|
+
` ${spec.name.padEnd(width)} ${spec.summary}${spec.signs ? " [broadcasts a tx]" : spec.requiresKey === true ? " [needs a key]" : ""}`,
|
|
191
|
+
),
|
|
192
|
+
"",
|
|
193
|
+
"Global flags:",
|
|
194
|
+
" --json machine-readable output on stdout",
|
|
195
|
+
" --help this text, or `metrik <command> --help`",
|
|
196
|
+
"",
|
|
197
|
+
"Settlement flags (all default to Metrik's Base Sepolia deployment):",
|
|
198
|
+
" --chain-id --rpc-url --escrow --usdc --oracle-url --registry-url",
|
|
199
|
+
"",
|
|
200
|
+
"Wallet (read from the environment only; never accepted as a flag):",
|
|
201
|
+
` ${PRIVATE_KEY_ENV} a Base Sepolia test key. Checked first.`,
|
|
202
|
+
` ${CDP_ENV_VARS.join(", ")}`,
|
|
203
|
+
" a Coinbase CDP Server Wallet v2 account. Used when no private key is set.",
|
|
204
|
+
" A credential on the command line lands in shell history; these never are.",
|
|
205
|
+
"",
|
|
206
|
+
`TESTNET ONLY: this CLI refuses any chain outside ${TESTNET_CHAIN_IDS.join(", ")} (Base Sepolia),`,
|
|
207
|
+
"and re-checks the chain id the RPC actually serves before signing. There is no override.",
|
|
208
|
+
].join("\n");
|
|
209
|
+
}
|