@onecraft/wallet 0.0.0-stage → 0.1.145
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/.claude-plugin/marketplace.json +20 -0
- package/.openclaw/AGENTS.md +100 -0
- package/.openclaw/extensions/agent-wallet/README.md +195 -0
- package/.openclaw/extensions/agent-wallet/dist/index.js +2198 -0
- package/.openclaw/extensions/agent-wallet/index.ts +2198 -0
- package/.openclaw/extensions/agent-wallet/openclaw.plugin.json +251 -0
- package/.openclaw/extensions/agent-wallet/package.json +49 -0
- package/.openclaw/extensions/agent-wallet/skills/cards/SKILL.md +211 -0
- package/.openclaw/extensions/agent-wallet/skills/wallet-operator/SKILL.md +30 -0
- package/CHANGELOG.md +1232 -0
- package/LICENSE +104 -0
- package/README.md +193 -2
- package/VERSION +1 -0
- package/agent-wallet/.env.example +63 -0
- package/agent-wallet/AGENTS.md +130 -0
- package/agent-wallet/CARD_VAULT.md +640 -0
- package/agent-wallet/README.md +615 -0
- package/agent-wallet/UPGRADE_COMPATIBILITY.md +66 -0
- package/agent-wallet/agent_wallet/__init__.py +16 -0
- package/agent-wallet/agent_wallet/approval.py +165 -0
- package/agent-wallet/agent_wallet/autonomous_permissions.py +281 -0
- package/agent-wallet/agent_wallet/autonomous_policy.py +305 -0
- package/agent-wallet/agent_wallet/autonomous_session.py +236 -0
- package/agent-wallet/agent_wallet/boot_key_migration.py +155 -0
- package/agent-wallet/agent_wallet/boot_key_recovery.py +57 -0
- package/agent-wallet/agent_wallet/bootstrap.py +194 -0
- package/agent-wallet/agent_wallet/card_catalog.py +243 -0
- package/agent-wallet/agent_wallet/card_runtime_secrets.py +108 -0
- package/agent-wallet/agent_wallet/card_vault.py +331 -0
- package/agent-wallet/agent_wallet/card_vault_cli.py +174 -0
- package/agent-wallet/agent_wallet/card_vault_import.py +65 -0
- package/agent-wallet/agent_wallet/card_vault_keychain.py +239 -0
- package/agent-wallet/agent_wallet/config.py +719 -0
- package/agent-wallet/agent_wallet/connector_cli.py +198 -0
- package/agent-wallet/agent_wallet/connectors/__init__.py +28 -0
- package/agent-wallet/agent_wallet/connectors/catalog.py +68 -0
- package/agent-wallet/agent_wallet/connectors/client.py +308 -0
- package/agent-wallet/agent_wallet/connectors/intent_policy.py +243 -0
- package/agent-wallet/agent_wallet/connectors/manifest.py +192 -0
- package/agent-wallet/agent_wallet/connectors/registry.py +341 -0
- package/agent-wallet/agent_wallet/encrypted_storage.py +314 -0
- package/agent-wallet/agent_wallet/evm_user_wallets.py +910 -0
- package/agent-wallet/agent_wallet/exceptions.py +10 -0
- package/agent-wallet/agent_wallet/file_ops.py +34 -0
- package/agent-wallet/agent_wallet/http_client.py +25 -0
- package/agent-wallet/agent_wallet/kernel_browser.py +479 -0
- package/agent-wallet/agent_wallet/kernel_cli.py +57 -0
- package/agent-wallet/agent_wallet/kernel_gateway.py +95 -0
- package/agent-wallet/agent_wallet/keystore.py +503 -0
- package/agent-wallet/agent_wallet/laso_card_vault.py +246 -0
- package/agent-wallet/agent_wallet/models.py +66 -0
- package/agent-wallet/agent_wallet/morpho_monitor.py +298 -0
- package/agent-wallet/agent_wallet/nonce_registry.py +59 -0
- package/agent-wallet/agent_wallet/openclaw_adapter.py +9129 -0
- package/agent-wallet/agent_wallet/openclaw_cli.py +658 -0
- package/agent-wallet/agent_wallet/openclaw_runtime.py +194 -0
- package/agent-wallet/agent_wallet/plugin_bundle.py +44 -0
- package/agent-wallet/agent_wallet/providers/__init__.py +1 -0
- package/agent-wallet/agent_wallet/providers/bags.py +202 -0
- package/agent-wallet/agent_wallet/providers/dexscreener.py +147 -0
- package/agent-wallet/agent_wallet/providers/evm_activity.py +257 -0
- package/agent-wallet/agent_wallet/providers/evm_portfolio.py +459 -0
- package/agent-wallet/agent_wallet/providers/imd.py +720 -0
- package/agent-wallet/agent_wallet/providers/jupiter.py +519 -0
- package/agent-wallet/agent_wallet/providers/kamino.py +404 -0
- package/agent-wallet/agent_wallet/providers/lifi.py +302 -0
- package/agent-wallet/agent_wallet/providers/solana_rpc.py +447 -0
- package/agent-wallet/agent_wallet/providers/tokenized_stocks.py +189 -0
- package/agent-wallet/agent_wallet/providers/wdk_evm_local.py +346 -0
- package/agent-wallet/agent_wallet/providers/x402.py +1972 -0
- package/agent-wallet/agent_wallet/sealed_keys.py +118 -0
- package/agent-wallet/agent_wallet/solana_stake.py +103 -0
- package/agent-wallet/agent_wallet/solana_tx.py +93 -0
- package/agent-wallet/agent_wallet/spending_limits.py +119 -0
- package/agent-wallet/agent_wallet/telemetry.py +481 -0
- package/agent-wallet/agent_wallet/transaction_policy.py +573 -0
- package/agent-wallet/agent_wallet/update_check.py +190 -0
- package/agent-wallet/agent_wallet/user_wallets.py +573 -0
- package/agent-wallet/agent_wallet/validation.py +31 -0
- package/agent-wallet/agent_wallet/vault_executor.py +456 -0
- package/agent-wallet/agent_wallet/wallet_layer/__init__.py +1 -0
- package/agent-wallet/agent_wallet/wallet_layer/base.py +1204 -0
- package/agent-wallet/agent_wallet/wallet_layer/base58.py +44 -0
- package/agent-wallet/agent_wallet/wallet_layer/factory.py +117 -0
- package/agent-wallet/agent_wallet/wallet_layer/solana.py +5246 -0
- package/agent-wallet/agent_wallet/wallet_layer/wdk_evm.py +2705 -0
- package/agent-wallet/examples/bootstrap_wallet.py +21 -0
- package/agent-wallet/examples/openclaw_runtime_onboarding.py +28 -0
- package/agent-wallet/examples/openclaw_user_wallet_example.py +31 -0
- package/agent-wallet/examples/openclaw_wallet_adapter_example.py +33 -0
- package/agent-wallet/openclaw.plugin.json +118 -0
- package/agent-wallet/pyproject.toml +36 -0
- package/agent-wallet/scripts/bootstrap_openclaw_evm.py +312 -0
- package/agent-wallet/scripts/build_release_bundle.py +210 -0
- package/agent-wallet/scripts/finalize_openclaw_local_wallet_config.py +121 -0
- package/agent-wallet/scripts/install_agent_wallet.py +1357 -0
- package/agent-wallet/scripts/install_openclaw_local_config.py +421 -0
- package/agent-wallet/scripts/install_openclaw_sealed_keys.py +117 -0
- package/agent-wallet/scripts/manage_openclaw_evm_wallet.py +333 -0
- package/agent-wallet/scripts/security_utils.py +37 -0
- package/agent-wallet/scripts/setup_evm_wallet.sh +140 -0
- package/agent-wallet/scripts/switch_openclaw_wallet_network.py +109 -0
- package/agent-wallet/skills/wallet-operator/SKILL.md +157 -0
- package/bin/lib/boot-key.mjs +184 -0
- package/bin/lib/evm-daemon.mjs +389 -0
- package/bin/lib/host-detection.mjs +259 -0
- package/bin/lib/integrations.mjs +578 -0
- package/bin/lib/release-python.mjs +9 -0
- package/bin/lib/update-transaction.mjs +235 -0
- package/bin/openclaw-agent-wallet.mjs +3354 -0
- package/claude-code/plugins/agent-wallet/.claude-plugin/plugin.json +19 -0
- package/claude-code/plugins/agent-wallet/.mcp.json +14 -0
- package/claude-code/plugins/agent-wallet/AGENTLAYER_AGENT_GUIDE.md +282 -0
- package/claude-code/plugins/agent-wallet/README.md +134 -0
- package/claude-code/plugins/agent-wallet/commands/agentlayer-autonomous-approve.md +39 -0
- package/claude-code/plugins/agent-wallet/commands/agentlayer-autonomous-revoke.md +17 -0
- package/claude-code/plugins/agent-wallet/commands/cards.md +129 -0
- package/claude-code/plugins/agent-wallet/commands/guide.md +40 -0
- package/claude-code/plugins/agent-wallet/commands/imd.md +125 -0
- package/claude-code/plugins/agent-wallet/commands/wallet-base.md +60 -0
- package/claude-code/plugins/agent-wallet/commands/wallet-ethereum.md +37 -0
- package/claude-code/plugins/agent-wallet/commands/wallet-evm.md +39 -0
- package/claude-code/plugins/agent-wallet/commands/wallet-setup.md +25 -0
- package/claude-code/plugins/agent-wallet/commands/wallet-sol.md +51 -0
- package/claude-code/plugins/agent-wallet/commands/wallet-statusline.md +34 -0
- package/claude-code/plugins/agent-wallet/commands/x402.md +121 -0
- package/claude-code/plugins/agent-wallet/commands/yield.md +62 -0
- package/claude-code/plugins/agent-wallet/hooks/hooks.json +16 -0
- package/claude-code/plugins/agent-wallet/scripts/bootstrap_backend.sh +136 -0
- package/claude-code/plugins/agent-wallet/scripts/enable_statusline.py +52 -0
- package/claude-code/plugins/agent-wallet/scripts/resolve_wallet_addresses.py +69 -0
- package/claude-code/plugins/agent-wallet/scripts/run_mcp.sh +66 -0
- package/claude-code/plugins/agent-wallet/scripts/wallet_statusline.py +130 -0
- package/claude-code/plugins/agent-wallet/scripts/wallet_statusline_setup.sh +40 -0
- package/claude-code/plugins/agent-wallet/skills/wallet-operator/SKILL.md +21 -0
- package/codex/plugins/agent-wallet/.codex-plugin/plugin.json +37 -0
- package/codex/plugins/agent-wallet/.mcp.json +15 -0
- package/codex/plugins/agent-wallet/README.md +158 -0
- package/codex/plugins/agent-wallet/scripts/run_mcp.sh +47 -0
- package/codex/plugins/agent-wallet/server.py +1797 -0
- package/codex/plugins/agent-wallet/skills/cards/SKILL.md +225 -0
- package/codex/plugins/agent-wallet/skills/imd/SKILL.md +145 -0
- package/codex/plugins/agent-wallet/skills/wallet-base/SKILL.md +56 -0
- package/codex/plugins/agent-wallet/skills/wallet-operator/SKILL.md +85 -0
- package/codex/plugins/agent-wallet/skills/wallet-sol/SKILL.md +29 -0
- package/codex/plugins/agent-wallet/skills/x402/SKILL.md +159 -0
- package/codex/plugins/agent-wallet/skills/yield/SKILL.md +65 -0
- package/connectors/AGENTS.md +37 -0
- package/connectors/DEVELOPER_GUIDE.md +131 -0
- package/connectors/README.md +139 -0
- package/connectors/READ_ONLY_BETA.md +54 -0
- package/connectors/RELEASING.md +48 -0
- package/connectors/conformance/README.md +31 -0
- package/connectors/conformance/package.json +54 -0
- package/connectors/conformance/scripts/sync-license.mjs +3 -0
- package/connectors/conformance/scripts/sync-spec.mjs +16 -0
- package/connectors/conformance/src/cli.ts +43 -0
- package/connectors/conformance/src/index.ts +7 -0
- package/connectors/conformance/src/runner.ts +314 -0
- package/connectors/conformance/src/types.ts +27 -0
- package/connectors/conformance/test/fixtures/invalid-response-identity.json +14 -0
- package/connectors/conformance/test/fixtures/missing-tool-fixture.json +4 -0
- package/connectors/conformance/test/runner.test.mjs +151 -0
- package/connectors/conformance/tsconfig.json +17 -0
- package/connectors/examples/reference-crypto-data/Dockerfile +21 -0
- package/connectors/examples/reference-crypto-data/README.md +51 -0
- package/connectors/examples/reference-crypto-data/conformance.json +11 -0
- package/connectors/examples/reference-crypto-data/connector.json +95 -0
- package/connectors/examples/reference-crypto-data/package.json +21 -0
- package/connectors/examples/reference-crypto-data/railway.toml +9 -0
- package/connectors/examples/reference-crypto-data/src/connector.ts +137 -0
- package/connectors/examples/reference-crypto-data/src/server.ts +13 -0
- package/connectors/examples/reference-crypto-data/test/connector.test.mjs +97 -0
- package/connectors/examples/reference-crypto-data/tsconfig.json +17 -0
- package/connectors/package-lock.json +561 -0
- package/connectors/package.json +19 -0
- package/connectors/scripts/smoke-packed-packages.mjs +121 -0
- package/connectors/sdk-typescript/README.md +47 -0
- package/connectors/sdk-typescript/package.json +49 -0
- package/connectors/sdk-typescript/scripts/sync-license.mjs +3 -0
- package/connectors/sdk-typescript/src/connector.ts +204 -0
- package/connectors/sdk-typescript/src/errors.ts +11 -0
- package/connectors/sdk-typescript/src/http.ts +116 -0
- package/connectors/sdk-typescript/src/index.ts +26 -0
- package/connectors/sdk-typescript/src/types.ts +101 -0
- package/connectors/sdk-typescript/test/sdk.test.mjs +181 -0
- package/connectors/sdk-typescript/tsconfig.json +21 -0
- package/connectors/spec/connector-manifest.schema.json +340 -0
- package/connectors/spec/connector-protocol.md +159 -0
- package/connectors/spec/transaction-intent.schema.json +281 -0
- package/connectors/templates/read-only/.env.example +1 -0
- package/connectors/templates/read-only/Dockerfile +11 -0
- package/connectors/templates/read-only/README.md +33 -0
- package/connectors/templates/read-only/conformance.json +11 -0
- package/connectors/templates/read-only/connector.json +64 -0
- package/connectors/templates/read-only/package.json +21 -0
- package/connectors/templates/read-only/railway.toml +9 -0
- package/connectors/templates/read-only/src/connector.ts +61 -0
- package/connectors/templates/read-only/src/server.ts +14 -0
- package/connectors/templates/read-only/test/connector.test.mjs +62 -0
- package/connectors/templates/read-only/tsconfig.json +17 -0
- package/cursor/plugins/agent-wallet/.cursor-plugin/plugin.json +14 -0
- package/cursor/plugins/agent-wallet/README.md +62 -0
- package/cursor/plugins/agent-wallet/mcp.json +16 -0
- package/cursor/plugins/agent-wallet/scripts/run_mcp.sh +33 -0
- package/cursor/plugins/agent-wallet/server.py +33 -0
- package/cursor/plugins/agent-wallet/skills/cards/SKILL.md +210 -0
- package/cursor/plugins/agent-wallet/skills/wallet-base/SKILL.md +56 -0
- package/cursor/plugins/agent-wallet/skills/wallet-operator/SKILL.md +23 -0
- package/cursor/plugins/agent-wallet/skills/wallet-sol/SKILL.md +29 -0
- package/cursor/plugins/agent-wallet/skills/x402/SKILL.md +130 -0
- package/hermes/plugins/agent_wallet/README.md +54 -0
- package/hermes/plugins/agent_wallet/__init__.py +55 -0
- package/hermes/plugins/agent_wallet/plugin.yaml +9 -0
- package/hermes/plugins/agent_wallet/schemas.py +211 -0
- package/hermes/plugins/agent_wallet/tools.py +622 -0
- package/install-from-github.sh +139 -0
- package/integrations/hedera/treasury-guard/README.md +92 -0
- package/integrations/hedera/treasury-guard/examples/treasury-policy.example.json +9 -0
- package/integrations/hedera/treasury-guard/package-lock.json +880 -0
- package/integrations/hedera/treasury-guard/package.json +8 -0
- package/integrations/hedera/treasury-guard/skills/onecraft-hedera-treasury/SKILL.md +22 -0
- package/integrations/hedera/treasury-guard/src/hedera-mirror-client.mjs +106 -0
- package/integrations/hedera/treasury-guard/src/hedera-signer.mjs +120 -0
- package/integrations/hedera/treasury-guard/src/treasury-cli.mjs +177 -0
- package/integrations/hedera/treasury-guard/src/treasury-policy.mjs +155 -0
- package/mcp/README.md +39 -0
- package/mcp/scripts/run_mcp.sh +37 -0
- package/mcp/server.py +33 -0
- package/package.json +106 -4
- package/scripts/check_release_version.mjs +72 -0
- package/scripts/version_targets.mjs +60 -0
- package/setup.sh +87 -0
- package/wdk-evm-wallet/.env.example +38 -0
- package/wdk-evm-wallet/README.md +254 -0
- package/wdk-evm-wallet/bootstrap.sh +8 -0
- package/wdk-evm-wallet/package-lock.json +2359 -0
- package/wdk-evm-wallet/package.json +43 -0
- package/wdk-evm-wallet/run-local.sh +12 -0
- package/wdk-evm-wallet/src/config.js +461 -0
- package/wdk-evm-wallet/src/imd.js +231 -0
- package/wdk-evm-wallet/src/json.js +35 -0
- package/wdk-evm-wallet/src/local_vault.js +407 -0
- package/wdk-evm-wallet/src/network_state.js +96 -0
- package/wdk-evm-wallet/src/pending_transactions.js +148 -0
- package/wdk-evm-wallet/src/server.js +849 -0
- package/wdk-evm-wallet/src/shutdown.js +63 -0
- package/wdk-evm-wallet/src/wdk_evm_wallet.js +10036 -0
|
@@ -0,0 +1,3354 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import { spawn, spawnSync } from "node:child_process";
|
|
4
|
+
import crypto from "node:crypto";
|
|
5
|
+
import fs from "node:fs";
|
|
6
|
+
import os from "node:os";
|
|
7
|
+
import path from "node:path";
|
|
8
|
+
import readline from "node:readline/promises";
|
|
9
|
+
import { fileURLToPath } from "node:url";
|
|
10
|
+
import { createBootKeyManager } from "./lib/boot-key.mjs";
|
|
11
|
+
import { releaseCheckPython } from "./lib/release-python.mjs";
|
|
12
|
+
import {
|
|
13
|
+
buildInstallPlan,
|
|
14
|
+
detectHosts,
|
|
15
|
+
stripUniversalInstallerArgs,
|
|
16
|
+
} from "./lib/host-detection.mjs";
|
|
17
|
+
import { stopLocalEvmDaemonSync } from "./lib/evm-daemon.mjs";
|
|
18
|
+
import { createHostIntegrationManager, createIntegrationManager, syncCursorPluginBundle } from "./lib/integrations.mjs";
|
|
19
|
+
import { createUpdateTransactionManager } from "./lib/update-transaction.mjs";
|
|
20
|
+
|
|
21
|
+
const cliPath = fileURLToPath(import.meta.url);
|
|
22
|
+
const packageRoot = path.resolve(path.dirname(cliPath), "..");
|
|
23
|
+
const setupPath = path.join(packageRoot, "setup.sh");
|
|
24
|
+
const packageJsonPath = path.join(packageRoot, "package.json");
|
|
25
|
+
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, "utf8"));
|
|
26
|
+
const packageVersion = packageJson.version;
|
|
27
|
+
const npmCommand = `npx --yes ${packageJson.name}@latest`;
|
|
28
|
+
const UPDATE_CLI_PATH_ENV = "OPENCLAW_AGENT_WALLET_UPDATE_CLI_PATH";
|
|
29
|
+
const UPDATE_PACKAGE_SPEC_ENV = "OPENCLAW_AGENT_WALLET_UPDATE_PACKAGE_SPEC";
|
|
30
|
+
const DEFAULT_PROVIDER_GATEWAY_URL = "https://agent-layer-production.up.railway.app";
|
|
31
|
+
const TELEMETRY_SPOOL_NAME = "telemetry_spool.jsonl";
|
|
32
|
+
const TELEMETRY_ID_NAME = "telemetry_id";
|
|
33
|
+
const TELEMETRY_LAST_FLUSH_NAME = "telemetry_last_flush";
|
|
34
|
+
const TELEMETRY_FLUSH_THROTTLE_SECONDS = 20;
|
|
35
|
+
const TELEMETRY_FORCE_LINES = 25;
|
|
36
|
+
const TELEMETRY_MAX_EVENTS_PER_FLUSH = 100;
|
|
37
|
+
const TELEMETRY_HTTP_TIMEOUT_MS = 1500;
|
|
38
|
+
const KEYSTORE_BRIDGE_TIMEOUT_MS = 30000;
|
|
39
|
+
|
|
40
|
+
function printHelp() {
|
|
41
|
+
console.log(`OneCraft Wallet
|
|
42
|
+
|
|
43
|
+
Usage:
|
|
44
|
+
wallet install [options]
|
|
45
|
+
wallet detect [--json]
|
|
46
|
+
wallet hermes install [options]
|
|
47
|
+
wallet codex install [options]
|
|
48
|
+
wallet cursor install [options]
|
|
49
|
+
wallet claude-code install [options]
|
|
50
|
+
wallet mcp serve
|
|
51
|
+
wallet mcp path
|
|
52
|
+
wallet mcp config
|
|
53
|
+
wallet update [options]
|
|
54
|
+
wallet status
|
|
55
|
+
wallet rollback [--to <version>]
|
|
56
|
+
wallet doctor
|
|
57
|
+
wallet connectors <command>
|
|
58
|
+
wallet hedera-treasury <command>
|
|
59
|
+
wallet vault <import|status|list|show|reveal|refresh|remove|migrate|kernel-connect|kernel-status|kernel-disconnect> [reference] [--user-id <id>]
|
|
60
|
+
wallet --version
|
|
61
|
+
|
|
62
|
+
Common install options:
|
|
63
|
+
--yes Generate local runtime secrets when missing.
|
|
64
|
+
--hosts <list> Hosts to install: detected, all, none, or comma-separated names.
|
|
65
|
+
--exclude <list> Detected/managed hosts to leave untouched.
|
|
66
|
+
--runtime-only Install/update the shared runtime without adding host plugins.
|
|
67
|
+
--no-prompt Accept the automatic host selection in interactive terminals.
|
|
68
|
+
--no-auto-secrets Do not generate runtime secrets automatically.
|
|
69
|
+
--backend <backend> solana_local, wdk_evm_local, or none.
|
|
70
|
+
--network <network> devnet, mainnet, base, ethereum, bitcoin, etc.
|
|
71
|
+
--invite <code> Bind a welcome invite to the local Base address.
|
|
72
|
+
|
|
73
|
+
Examples:
|
|
74
|
+
${npmCommand} install
|
|
75
|
+
${npmCommand} install --yes --hosts codex,claude-code
|
|
76
|
+
${npmCommand} detect --json
|
|
77
|
+
${npmCommand} install --yes --invite alw_...
|
|
78
|
+
${npmCommand} hermes install --yes
|
|
79
|
+
${npmCommand} codex install --yes
|
|
80
|
+
${npmCommand} cursor install --yes
|
|
81
|
+
${npmCommand} claude-code install --yes
|
|
82
|
+
wallet mcp config
|
|
83
|
+
wallet mcp serve
|
|
84
|
+
${npmCommand} install --backend none
|
|
85
|
+
${npmCommand} update --yes
|
|
86
|
+
${npmCommand} update --yes --dry-run
|
|
87
|
+
${npmCommand} status
|
|
88
|
+
wallet connectors list
|
|
89
|
+
wallet connectors inspect ./connector.json
|
|
90
|
+
wallet connectors install ./connector.json --enable --yes
|
|
91
|
+
|
|
92
|
+
The installer writes a versioned runtime under:
|
|
93
|
+
~/.openclaw/agent-wallet-runtime/releases/<version>
|
|
94
|
+
|
|
95
|
+
After a successful install it switches:
|
|
96
|
+
~/.openclaw/agent-wallet-runtime/current
|
|
97
|
+
|
|
98
|
+
Wallet files and sealed secrets remain under OPENCLAW_HOME and are not replaced
|
|
99
|
+
by updates. The update command fetches the latest published npm package and
|
|
100
|
+
reuses shared dependency snapshots when possible.
|
|
101
|
+
|
|
102
|
+
The runtime checks the npm registry in the background (at most once/day) and
|
|
103
|
+
surfaces a notice when a newer version is published — to the agent via the MCP
|
|
104
|
+
server instructions and to you via 'status' / 'doctor'. The check never blocks
|
|
105
|
+
startup. Disable it with AGENT_WALLET_DISABLE_UPDATE_CHECK=1.`);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function primaryBinCommand(pkg = packageJson) {
|
|
109
|
+
const bin = pkg?.bin;
|
|
110
|
+
if (!bin) return "wallet";
|
|
111
|
+
if (typeof bin === "string") {
|
|
112
|
+
const packageName = String(pkg?.name || "").trim();
|
|
113
|
+
if (packageName) {
|
|
114
|
+
const parts = packageName.split("/");
|
|
115
|
+
return parts[parts.length - 1] || "wallet";
|
|
116
|
+
}
|
|
117
|
+
return "wallet";
|
|
118
|
+
}
|
|
119
|
+
const names = Object.keys(bin);
|
|
120
|
+
return names[0] || "wallet";
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function expandHome(value) {
|
|
124
|
+
if (!value) return value;
|
|
125
|
+
if (value === "~") return os.homedir();
|
|
126
|
+
if (value.startsWith("~/")) return path.join(os.homedir(), value.slice(2));
|
|
127
|
+
return value;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function resolveOpenclawHome(env = process.env) {
|
|
131
|
+
return path.resolve(expandHome(env.OPENCLAW_HOME || "~/.openclaw"));
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function resolveRuntimeBase(env = process.env) {
|
|
135
|
+
if (env.OPENCLAW_INSTALL_ROOT) {
|
|
136
|
+
return path.resolve(expandHome(env.OPENCLAW_INSTALL_ROOT));
|
|
137
|
+
}
|
|
138
|
+
return path.join(resolveOpenclawHome(env), "agent-wallet-runtime");
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
function updateCheckDisabled(env = process.env) {
|
|
142
|
+
const raw = String(env.AGENT_WALLET_DISABLE_UPDATE_CHECK || "").trim().toLowerCase();
|
|
143
|
+
return ["1", "true", "yes", "on"].includes(raw);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
function telemetryDisabled(env = process.env) {
|
|
147
|
+
const raw = String(env.AGENT_WALLET_NO_TELEMETRY || "").trim().toLowerCase();
|
|
148
|
+
return ["1", "true", "yes", "on"].includes(raw);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
function telemetryPath(name, env = process.env) {
|
|
152
|
+
return path.join(resolveOpenclawHome(env), name);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
function telemetryInstallId(env = process.env) {
|
|
156
|
+
const idPath = telemetryPath(TELEMETRY_ID_NAME, env);
|
|
157
|
+
try {
|
|
158
|
+
const existing = fs.readFileSync(idPath, "utf8").trim();
|
|
159
|
+
if (existing) return existing;
|
|
160
|
+
} catch {
|
|
161
|
+
// Create below.
|
|
162
|
+
}
|
|
163
|
+
const next = crypto.randomUUID ? crypto.randomUUID().replaceAll("-", "") : crypto.randomBytes(16).toString("hex");
|
|
164
|
+
try {
|
|
165
|
+
fs.mkdirSync(path.dirname(idPath), { recursive: true });
|
|
166
|
+
fs.writeFileSync(idPath, next, { mode: 0o600 });
|
|
167
|
+
} catch {
|
|
168
|
+
// Telemetry must not affect install flow.
|
|
169
|
+
}
|
|
170
|
+
return next;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
function telemetryHost(host = "", env = process.env) {
|
|
174
|
+
const raw = String(host || env.AGENT_WALLET_HOST || "").trim().toLowerCase();
|
|
175
|
+
return ["claude-code", "codex", "cursor", "hermes", "mcp", "openclaw"].includes(raw) ? raw : "unknown";
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
function telemetrySource(env = process.env) {
|
|
179
|
+
const explicit = String(env.AGENT_WALLET_INSTALL_SOURCE || "").trim().toLowerCase();
|
|
180
|
+
if (explicit) return explicit.replace(/[^a-z0-9_]/g, "_").slice(0, 48);
|
|
181
|
+
return env.npm_execpath || String(env.npm_config_user_agent || "").includes("npm")
|
|
182
|
+
? "npx"
|
|
183
|
+
: "global_cli";
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
function positiveIntEnv(name, fallback, env = process.env) {
|
|
187
|
+
const parsed = Number.parseInt(String(env[name] || ""), 10);
|
|
188
|
+
return Number.isFinite(parsed) && parsed > 0 ? parsed : fallback;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
function telemetryGatewayUrl(env = process.env) {
|
|
192
|
+
return String(env.PROVIDER_GATEWAY_URL || DEFAULT_PROVIDER_GATEWAY_URL).trim().replace(/\/+$/, "");
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
function telemetryAppend(payload, env = process.env) {
|
|
196
|
+
const spool = telemetryPath(TELEMETRY_SPOOL_NAME, env);
|
|
197
|
+
fs.mkdirSync(path.dirname(spool), { recursive: true });
|
|
198
|
+
fs.appendFileSync(spool, `${JSON.stringify(payload)}\n`, { encoding: "utf8" });
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
function telemetrySpoolLineCount(env = process.env) {
|
|
202
|
+
try {
|
|
203
|
+
const spool = telemetryPath(TELEMETRY_SPOOL_NAME, env);
|
|
204
|
+
return fs.readFileSync(spool, "utf8").split(/\r?\n/).filter(Boolean).length;
|
|
205
|
+
} catch {
|
|
206
|
+
return 0;
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
function telemetryShouldFlush(env = process.env) {
|
|
211
|
+
const count = telemetrySpoolLineCount(env);
|
|
212
|
+
if (count === 0) return false;
|
|
213
|
+
if (count >= TELEMETRY_FORCE_LINES) return true;
|
|
214
|
+
try {
|
|
215
|
+
const last = Number(fs.readFileSync(telemetryPath(TELEMETRY_LAST_FLUSH_NAME, env), "utf8").trim() || "0");
|
|
216
|
+
return Date.now() / 1000 - last >= TELEMETRY_FLUSH_THROTTLE_SECONDS;
|
|
217
|
+
} catch {
|
|
218
|
+
return true;
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
function telemetryMarkFlush(env = process.env) {
|
|
223
|
+
try {
|
|
224
|
+
fs.writeFileSync(telemetryPath(TELEMETRY_LAST_FLUSH_NAME, env), String(Date.now() / 1000));
|
|
225
|
+
} catch {
|
|
226
|
+
// ignored
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
function telemetrySpawnFlush(env = process.env, force = false) {
|
|
231
|
+
if (!force && !telemetryShouldFlush(env)) return;
|
|
232
|
+
telemetryMarkFlush(env);
|
|
233
|
+
try {
|
|
234
|
+
const child = spawn(process.execPath, [cliPath, "--telemetry-flush"], {
|
|
235
|
+
detached: true,
|
|
236
|
+
stdio: "ignore",
|
|
237
|
+
env,
|
|
238
|
+
});
|
|
239
|
+
child.unref();
|
|
240
|
+
} catch {
|
|
241
|
+
// ignored
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
function recordCliTelemetry(event, { commandName, host = "", ok = true, args = [], flush = true } = {}) {
|
|
246
|
+
try {
|
|
247
|
+
if (telemetryDisabled()) return;
|
|
248
|
+
if (args && hasFlag(args, "--dry-run")) return;
|
|
249
|
+
const payload = {
|
|
250
|
+
event,
|
|
251
|
+
install_id: telemetryInstallId(),
|
|
252
|
+
host: telemetryHost(host),
|
|
253
|
+
tool: "",
|
|
254
|
+
backend: "",
|
|
255
|
+
plugin_version: packageVersion,
|
|
256
|
+
ok: Boolean(ok),
|
|
257
|
+
ts: Math.floor(Date.now() / 1000),
|
|
258
|
+
source: telemetrySource(),
|
|
259
|
+
command: String(commandName || "").trim().toLowerCase().replace(/-/g, "_").slice(0, 48),
|
|
260
|
+
};
|
|
261
|
+
telemetryAppend(payload);
|
|
262
|
+
if (flush) telemetrySpawnFlush(process.env, true);
|
|
263
|
+
} catch {
|
|
264
|
+
// Telemetry must never affect CLI behavior.
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
async function telemetryPost(url, line) {
|
|
269
|
+
const controller = new AbortController();
|
|
270
|
+
const timer = setTimeout(() => controller.abort(), TELEMETRY_HTTP_TIMEOUT_MS);
|
|
271
|
+
try {
|
|
272
|
+
const response = await fetch(url, {
|
|
273
|
+
method: "POST",
|
|
274
|
+
body: line,
|
|
275
|
+
headers: { "Content-Type": "application/json" },
|
|
276
|
+
signal: controller.signal,
|
|
277
|
+
});
|
|
278
|
+
return response.ok;
|
|
279
|
+
} catch {
|
|
280
|
+
return false;
|
|
281
|
+
} finally {
|
|
282
|
+
clearTimeout(timer);
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
async function telemetryFlushMain(env = process.env) {
|
|
287
|
+
if (telemetryDisabled(env)) return 0;
|
|
288
|
+
const spool = telemetryPath(TELEMETRY_SPOOL_NAME, env);
|
|
289
|
+
const claim = `${spool}.flushing.${process.pid}`;
|
|
290
|
+
try {
|
|
291
|
+
fs.renameSync(spool, claim);
|
|
292
|
+
} catch {
|
|
293
|
+
return 0;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
let lines = [];
|
|
297
|
+
try {
|
|
298
|
+
lines = fs.readFileSync(claim, "utf8").split(/\r?\n/).filter(Boolean);
|
|
299
|
+
} catch {
|
|
300
|
+
lines = [];
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
const url = `${telemetryGatewayUrl(env)}/v1/telemetry`;
|
|
304
|
+
const failed = [];
|
|
305
|
+
let sent = 0;
|
|
306
|
+
for (const line of lines) {
|
|
307
|
+
if (sent >= TELEMETRY_MAX_EVENTS_PER_FLUSH) {
|
|
308
|
+
failed.push(line);
|
|
309
|
+
continue;
|
|
310
|
+
}
|
|
311
|
+
if (await telemetryPost(url, line)) {
|
|
312
|
+
sent += 1;
|
|
313
|
+
} else {
|
|
314
|
+
failed.push(line);
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
if (failed.length) {
|
|
319
|
+
try {
|
|
320
|
+
fs.appendFileSync(spool, `${failed.join("\n")}\n`, { encoding: "utf8" });
|
|
321
|
+
} catch {
|
|
322
|
+
// ignored
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
try {
|
|
326
|
+
fs.unlinkSync(claim);
|
|
327
|
+
} catch {
|
|
328
|
+
// ignored
|
|
329
|
+
}
|
|
330
|
+
return 0;
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
function runWithCliTelemetry(fn, { startEvent, successEvent, failedEvent, commandName, host = "", args = [] }) {
|
|
334
|
+
recordCliTelemetry(startEvent, { commandName, host, ok: true, args, flush: false });
|
|
335
|
+
let code;
|
|
336
|
+
try {
|
|
337
|
+
code = fn();
|
|
338
|
+
} catch (error) {
|
|
339
|
+
recordCliTelemetry(failedEvent, {
|
|
340
|
+
commandName,
|
|
341
|
+
host,
|
|
342
|
+
ok: false,
|
|
343
|
+
args,
|
|
344
|
+
});
|
|
345
|
+
throw error;
|
|
346
|
+
}
|
|
347
|
+
recordCliTelemetry(code === 0 ? successEvent : failedEvent, {
|
|
348
|
+
commandName,
|
|
349
|
+
host,
|
|
350
|
+
ok: code === 0,
|
|
351
|
+
args,
|
|
352
|
+
});
|
|
353
|
+
return code;
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
async function runWithCliTelemetryAsync(
|
|
357
|
+
fn,
|
|
358
|
+
{ startEvent, successEvent, failedEvent, commandName, host = "", args = [] },
|
|
359
|
+
) {
|
|
360
|
+
recordCliTelemetry(startEvent, { commandName, host, ok: true, args, flush: false });
|
|
361
|
+
let code;
|
|
362
|
+
try {
|
|
363
|
+
code = await fn();
|
|
364
|
+
} catch (error) {
|
|
365
|
+
recordCliTelemetry(failedEvent, {
|
|
366
|
+
commandName,
|
|
367
|
+
host,
|
|
368
|
+
ok: false,
|
|
369
|
+
args,
|
|
370
|
+
});
|
|
371
|
+
throw error;
|
|
372
|
+
}
|
|
373
|
+
recordCliTelemetry(code === 0 ? successEvent : failedEvent, {
|
|
374
|
+
commandName,
|
|
375
|
+
host,
|
|
376
|
+
ok: code === 0,
|
|
377
|
+
args,
|
|
378
|
+
});
|
|
379
|
+
return code;
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
// Shared with the Python runtime (agent_wallet/update_check.py): the cache lives
|
|
383
|
+
// under OPENCLAW_HOME/agent-wallet-runtime regardless of OPENCLAW_INSTALL_ROOT.
|
|
384
|
+
function updateCheckCachePath(env = process.env) {
|
|
385
|
+
return path.join(resolveOpenclawHome(env), "agent-wallet-runtime", "update-check.json");
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
function isNewerVersion(latest, current) {
|
|
389
|
+
const parse = (v) =>
|
|
390
|
+
String(v)
|
|
391
|
+
.trim()
|
|
392
|
+
.split(".")
|
|
393
|
+
.map((chunk) => parseInt((chunk.match(/^\d+/) || ["0"])[0], 10) || 0);
|
|
394
|
+
const a = parse(latest);
|
|
395
|
+
const b = parse(current);
|
|
396
|
+
for (let i = 0; i < Math.max(a.length, b.length); i++) {
|
|
397
|
+
const x = a[i] || 0;
|
|
398
|
+
const y = b[i] || 0;
|
|
399
|
+
if (x !== y) return x > y;
|
|
400
|
+
}
|
|
401
|
+
return false;
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
function computeUpdateAvailability(env = process.env) {
|
|
405
|
+
const current = activeVersion(env) || packageVersion;
|
|
406
|
+
if (updateCheckDisabled(env)) {
|
|
407
|
+
return { available: false, latest: null, current };
|
|
408
|
+
}
|
|
409
|
+
try {
|
|
410
|
+
const cache = readJsonFile(updateCheckCachePath(env)) || {};
|
|
411
|
+
const latest = cache.latest_version || null;
|
|
412
|
+
const available = Boolean(latest && isNewerVersion(latest, current));
|
|
413
|
+
return { available, latest, current };
|
|
414
|
+
} catch {
|
|
415
|
+
// Fail-open: a malformed cache never breaks status/doctor.
|
|
416
|
+
return { available: false, latest: null, current };
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
// Compare the active installed runtime (what all editors exec via current/)
|
|
421
|
+
// against the version of the CLI being run (the repo/canonical version during
|
|
422
|
+
// local development). A mismatch means a local bump was not reinstalled with
|
|
423
|
+
// release:local. in_sync is null when no runtime is installed yet.
|
|
424
|
+
function computeRuntimeInSync(env = process.env) {
|
|
425
|
+
const active = activeVersion(env);
|
|
426
|
+
const cli = packageVersion;
|
|
427
|
+
const in_sync = active === null ? null : active === cli;
|
|
428
|
+
return { in_sync, active_version: active, cli_version: cli };
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
function resolveHermesHome(env = process.env) {
|
|
432
|
+
return path.resolve(expandHome(env.HERMES_HOME || "~/.hermes"));
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
function resolveCodexHome(env = process.env) {
|
|
436
|
+
return path.resolve(expandHome(env.CODEX_HOME || "~/.codex"));
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
function resolveCursorPluginInstallRoot(env = process.env) {
|
|
440
|
+
return path.resolve(expandHome(
|
|
441
|
+
env.AGENT_WALLET_CURSOR_PLUGIN_ROOT || path.join(env.CURSOR_HOME || "~/.cursor", "plugins", "local"),
|
|
442
|
+
));
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
function releaseRootFor(version, env = process.env) {
|
|
446
|
+
return path.join(resolveRuntimeBase(env), "releases", version);
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
function stagingRootFor(version, env = process.env) {
|
|
450
|
+
return path.join(
|
|
451
|
+
resolveRuntimeBase(env),
|
|
452
|
+
"releases",
|
|
453
|
+
`.staging-${version}-${process.pid}-${Date.now()}`,
|
|
454
|
+
);
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
function updateTransactions(env = process.env) {
|
|
458
|
+
return createUpdateTransactionManager({
|
|
459
|
+
runtimeBase: resolveRuntimeBase(env),
|
|
460
|
+
packageVersion,
|
|
461
|
+
env,
|
|
462
|
+
});
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
function acquireUpdateLock(env = process.env) {
|
|
466
|
+
return updateTransactions(env).acquireLock();
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
function releaseUpdateLock(lock) {
|
|
470
|
+
return createUpdateTransactionManager({
|
|
471
|
+
runtimeBase: path.dirname(lock.path),
|
|
472
|
+
packageVersion,
|
|
473
|
+
}).releaseLock(lock);
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
function holdUpdateLockForTest(env = process.env) {
|
|
477
|
+
updateTransactions(env).holdLockForTest();
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
function readUpdateJournal(env = process.env) {
|
|
481
|
+
return updateTransactions(env).readJournal();
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
function integrations(env = process.env) {
|
|
485
|
+
return createIntegrationManager({
|
|
486
|
+
runtimeBase: resolveRuntimeBase(env),
|
|
487
|
+
packageVersion,
|
|
488
|
+
activeVersion: () => activeVersion(env),
|
|
489
|
+
});
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
function recordManagedIntegration(name, details = {}, env = process.env) {
|
|
493
|
+
return integrations(env).record(name, details);
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
function integrationSyncStatus(env = process.env) {
|
|
497
|
+
return integrations(env).status();
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
function hostIntegrations(env = process.env) {
|
|
501
|
+
return createHostIntegrationManager({
|
|
502
|
+
packageVersion,
|
|
503
|
+
env,
|
|
504
|
+
packageRoot,
|
|
505
|
+
registry: integrations(env),
|
|
506
|
+
currentRuntimePath: currentRuntimePath(env),
|
|
507
|
+
openclawHome: resolveOpenclawHome(env),
|
|
508
|
+
hermesHome: resolveHermesHome(env),
|
|
509
|
+
codexHome: resolveCodexHome(env),
|
|
510
|
+
cursorPluginRoot: resolveCursorPluginInstallRoot(env),
|
|
511
|
+
codexPluginRoot: resolveCodexPluginInstallRoot(env),
|
|
512
|
+
codexMarketplacePath: resolveCodexMarketplacePath(env),
|
|
513
|
+
claudeMarketplaceDir: resolveClaudeCodeMarketplaceDir(env),
|
|
514
|
+
claudeMarketplaceName: CLAUDE_CODE_MARKETPLACE_NAME,
|
|
515
|
+
expandHome,
|
|
516
|
+
resolveVenvPython,
|
|
517
|
+
commandPath,
|
|
518
|
+
repairRuntimeSymlink,
|
|
519
|
+
repairHermesEnv,
|
|
520
|
+
ensureCodexMarketplaceEntry,
|
|
521
|
+
ensureClaudeCodeMarketplace,
|
|
522
|
+
pinClaudeCacheCopies,
|
|
523
|
+
});
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
function managedHostNames(env = process.env) {
|
|
527
|
+
const registry = integrations(env).readRegistry();
|
|
528
|
+
return Object.entries(registry.integrations || {})
|
|
529
|
+
.filter(([, entry]) => entry?.managed === true)
|
|
530
|
+
.map(([name]) => name);
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
function runtimeInstalled(env = process.env) {
|
|
534
|
+
try {
|
|
535
|
+
const stat = fs.lstatSync(currentRuntimePath(env));
|
|
536
|
+
return stat.isSymbolicLink() || stat.isDirectory();
|
|
537
|
+
} catch (error) {
|
|
538
|
+
if (error?.code === "ENOENT") return false;
|
|
539
|
+
throw error;
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
function universalInstallPlan(args, env = process.env) {
|
|
544
|
+
const detections = detectHosts({
|
|
545
|
+
env,
|
|
546
|
+
commandPath,
|
|
547
|
+
});
|
|
548
|
+
const managedHosts = managedHostNames(env);
|
|
549
|
+
const plan = buildInstallPlan({
|
|
550
|
+
args,
|
|
551
|
+
detections: detections.map((entry) => ({
|
|
552
|
+
...entry,
|
|
553
|
+
managed: managedHosts.includes(entry.name),
|
|
554
|
+
})),
|
|
555
|
+
managedHosts,
|
|
556
|
+
runtimeInstalled: runtimeInstalled(env),
|
|
557
|
+
});
|
|
558
|
+
const universalSelectionExplicit =
|
|
559
|
+
hasFlag(args, "--hosts") ||
|
|
560
|
+
hasFlag(args, "--exclude") ||
|
|
561
|
+
hasFlag(args, "--runtime-only") ||
|
|
562
|
+
hasFlag(args, "--managed-only");
|
|
563
|
+
if (
|
|
564
|
+
!plan.runtime_installed_before &&
|
|
565
|
+
!universalSelectionExplicit &&
|
|
566
|
+
hasFlag(args, "--config-path")
|
|
567
|
+
) {
|
|
568
|
+
plan.selected_hosts = ["openclaw"];
|
|
569
|
+
plan.selection_reason = "explicit_openclaw_config";
|
|
570
|
+
}
|
|
571
|
+
return plan;
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
function runDetect(args = [], env = process.env) {
|
|
575
|
+
let plan;
|
|
576
|
+
try {
|
|
577
|
+
plan = universalInstallPlan(args, env);
|
|
578
|
+
} catch (error) {
|
|
579
|
+
console.error(error.message);
|
|
580
|
+
return 2;
|
|
581
|
+
}
|
|
582
|
+
console.log(
|
|
583
|
+
JSON.stringify(
|
|
584
|
+
{
|
|
585
|
+
schema_version: 1,
|
|
586
|
+
ok: true,
|
|
587
|
+
runtime_base: resolveRuntimeBase(env),
|
|
588
|
+
runtime_installed: plan.runtime_installed_before,
|
|
589
|
+
detected_hosts: plan.detected_hosts,
|
|
590
|
+
managed_hosts: plan.managed_hosts,
|
|
591
|
+
default_selected_hosts: plan.selected_hosts,
|
|
592
|
+
selection_reason: plan.selection_reason,
|
|
593
|
+
hosts: plan.detections,
|
|
594
|
+
},
|
|
595
|
+
null,
|
|
596
|
+
2,
|
|
597
|
+
),
|
|
598
|
+
);
|
|
599
|
+
return 0;
|
|
600
|
+
}
|
|
601
|
+
|
|
602
|
+
async function promptForInstallPlan(plan, args) {
|
|
603
|
+
const shouldPrompt =
|
|
604
|
+
!plan.runtime_installed_before &&
|
|
605
|
+
plan.selection_reason === "fresh_install_detected" &&
|
|
606
|
+
!hasFlag(args, "--yes") &&
|
|
607
|
+
!hasFlag(args, "--no-prompt") &&
|
|
608
|
+
process.stdin.isTTY &&
|
|
609
|
+
process.stdout.isTTY;
|
|
610
|
+
if (!shouldPrompt) return plan;
|
|
611
|
+
|
|
612
|
+
const excluded = new Set(plan.excluded_hosts);
|
|
613
|
+
const selected = new Set(plan.selected_hosts);
|
|
614
|
+
const terminal = readline.createInterface({
|
|
615
|
+
input: process.stdin,
|
|
616
|
+
output: process.stdout,
|
|
617
|
+
});
|
|
618
|
+
try {
|
|
619
|
+
console.log("OneCraft detected these agent frameworks:");
|
|
620
|
+
for (const host of plan.detections.filter((entry) => entry.detected && !excluded.has(entry.name))) {
|
|
621
|
+
const answer = (
|
|
622
|
+
await terminal.question(
|
|
623
|
+
` Install OneCraft for ${host.display_name}? ${selected.has(host.name) ? "[Y/n]" : "[y/N]"} `,
|
|
624
|
+
)
|
|
625
|
+
).trim().toLowerCase();
|
|
626
|
+
if (answer) {
|
|
627
|
+
if (["y", "yes"].includes(answer)) selected.add(host.name);
|
|
628
|
+
if (["n", "no"].includes(answer)) selected.delete(host.name);
|
|
629
|
+
}
|
|
630
|
+
}
|
|
631
|
+
} finally {
|
|
632
|
+
terminal.close();
|
|
633
|
+
}
|
|
634
|
+
return {
|
|
635
|
+
...plan,
|
|
636
|
+
selection_reason: "interactive",
|
|
637
|
+
selected_hosts: plan.detections
|
|
638
|
+
.map((entry) => entry.name)
|
|
639
|
+
.filter((name) => selected.has(name)),
|
|
640
|
+
};
|
|
641
|
+
}
|
|
642
|
+
|
|
643
|
+
async function runUniversalInstall(args, options = {}) {
|
|
644
|
+
try {
|
|
645
|
+
const initialPlan = universalInstallPlan(args);
|
|
646
|
+
const installPlan = await promptForInstallPlan(initialPlan, args);
|
|
647
|
+
return runInstall(args, { ...options, installPlan });
|
|
648
|
+
} catch (error) {
|
|
649
|
+
console.error(error.message);
|
|
650
|
+
return 2;
|
|
651
|
+
}
|
|
652
|
+
}
|
|
653
|
+
|
|
654
|
+
function writeUpdateJournal(state, details = {}, env = process.env) {
|
|
655
|
+
updateTransactions(env).writeJournal(state, details);
|
|
656
|
+
}
|
|
657
|
+
|
|
658
|
+
function recoverInterruptedUpdate(env = process.env) {
|
|
659
|
+
return updateTransactions(env).recover();
|
|
660
|
+
}
|
|
661
|
+
|
|
662
|
+
function updateRecoveryStatus(env = process.env) {
|
|
663
|
+
return updateTransactions(env).status();
|
|
664
|
+
}
|
|
665
|
+
|
|
666
|
+
function writeReleaseState(runtimeRoot, state, details = {}) {
|
|
667
|
+
writeJsonFile(path.join(runtimeRoot, ".agent-wallet-release.json"), {
|
|
668
|
+
schema_version: 1,
|
|
669
|
+
version: packageVersion,
|
|
670
|
+
state,
|
|
671
|
+
updated_at: new Date().toISOString(),
|
|
672
|
+
...details,
|
|
673
|
+
});
|
|
674
|
+
}
|
|
675
|
+
|
|
676
|
+
function failStagingRuntime(stagingRoot, error) {
|
|
677
|
+
if (!stagingRoot || !fs.existsSync(stagingRoot)) return null;
|
|
678
|
+
writeReleaseState(stagingRoot, "failed", { error: String(error || "install failed") });
|
|
679
|
+
const failedRoot = path.join(
|
|
680
|
+
path.dirname(stagingRoot),
|
|
681
|
+
`.failed-${packageVersion}-${Date.now()}`,
|
|
682
|
+
);
|
|
683
|
+
fs.renameSync(stagingRoot, failedRoot);
|
|
684
|
+
return failedRoot;
|
|
685
|
+
}
|
|
686
|
+
|
|
687
|
+
function commitStagedRuntime(stagingRoot, releaseRoot, replacedRoot = null, env = process.env) {
|
|
688
|
+
if (!stagingRoot) return null;
|
|
689
|
+
if (fs.existsSync(releaseRoot)) {
|
|
690
|
+
replacedRoot ||= uniquePathWithSuffix(
|
|
691
|
+
path.join(path.dirname(releaseRoot), `${path.basename(releaseRoot)}-replaced`),
|
|
692
|
+
);
|
|
693
|
+
fs.renameSync(releaseRoot, replacedRoot);
|
|
694
|
+
if (env.AGENT_WALLET_TEST_EXIT_AFTER_RELEASE_RENAME === "1") process.exit(86);
|
|
695
|
+
}
|
|
696
|
+
try {
|
|
697
|
+
fs.renameSync(stagingRoot, releaseRoot);
|
|
698
|
+
} catch (error) {
|
|
699
|
+
if (replacedRoot && !fs.existsSync(releaseRoot)) {
|
|
700
|
+
fs.renameSync(replacedRoot, releaseRoot);
|
|
701
|
+
}
|
|
702
|
+
throw error;
|
|
703
|
+
}
|
|
704
|
+
return replacedRoot;
|
|
705
|
+
}
|
|
706
|
+
|
|
707
|
+
function currentRuntimePath(env = process.env) {
|
|
708
|
+
return path.join(resolveRuntimeBase(env), "current");
|
|
709
|
+
}
|
|
710
|
+
|
|
711
|
+
function logicalCurrentRuntimeRoot(env = process.env) {
|
|
712
|
+
const currentPath = currentRuntimePath(env);
|
|
713
|
+
try {
|
|
714
|
+
const stat = fs.lstatSync(currentPath);
|
|
715
|
+
if (stat.isSymbolicLink() || stat.isDirectory()) return currentPath;
|
|
716
|
+
} catch (error) {
|
|
717
|
+
if (error?.code !== "ENOENT") throw error;
|
|
718
|
+
}
|
|
719
|
+
return "";
|
|
720
|
+
}
|
|
721
|
+
|
|
722
|
+
function resolvedCurrentRuntimeRoot(env = process.env) {
|
|
723
|
+
const currentPath = currentRuntimePath(env);
|
|
724
|
+
const currentTarget = readLinkOrNull(currentPath);
|
|
725
|
+
if (currentTarget) {
|
|
726
|
+
return path.resolve(path.dirname(currentPath), currentTarget);
|
|
727
|
+
}
|
|
728
|
+
try {
|
|
729
|
+
const stat = fs.statSync(currentPath);
|
|
730
|
+
if (stat.isDirectory()) return currentPath;
|
|
731
|
+
} catch (error) {
|
|
732
|
+
if (error?.code !== "ENOENT") throw error;
|
|
733
|
+
}
|
|
734
|
+
return "";
|
|
735
|
+
}
|
|
736
|
+
|
|
737
|
+
function previousRuntimePath(env = process.env) {
|
|
738
|
+
return path.join(resolveRuntimeBase(env), "previous");
|
|
739
|
+
}
|
|
740
|
+
|
|
741
|
+
function hasCommand(name) {
|
|
742
|
+
const result = spawnSync("command", ["-v", name], {
|
|
743
|
+
shell: true,
|
|
744
|
+
stdio: "ignore",
|
|
745
|
+
});
|
|
746
|
+
return result.status === 0;
|
|
747
|
+
}
|
|
748
|
+
|
|
749
|
+
function commandPath(name) {
|
|
750
|
+
const result = spawnSync("command", ["-v", name], {
|
|
751
|
+
shell: true,
|
|
752
|
+
encoding: "utf8",
|
|
753
|
+
});
|
|
754
|
+
if (result.status !== 0) return "";
|
|
755
|
+
return result.stdout.trim();
|
|
756
|
+
}
|
|
757
|
+
|
|
758
|
+
function pythonVersion(pythonBin) {
|
|
759
|
+
if (!pythonBin) return null;
|
|
760
|
+
const result = spawnSync(
|
|
761
|
+
pythonBin,
|
|
762
|
+
["-c", "import sys,json; print(json.dumps({'version': sys.version.split()[0], 'major': sys.version_info[0], 'minor': sys.version_info[1]}))"],
|
|
763
|
+
{ encoding: "utf8" },
|
|
764
|
+
);
|
|
765
|
+
if (result.status !== 0) return null;
|
|
766
|
+
try {
|
|
767
|
+
return JSON.parse(result.stdout);
|
|
768
|
+
} catch {
|
|
769
|
+
return null;
|
|
770
|
+
}
|
|
771
|
+
}
|
|
772
|
+
|
|
773
|
+
function pythonOk(version) {
|
|
774
|
+
return Boolean(version && (version.major > 3 || (version.major === 3 && version.minor >= 10)));
|
|
775
|
+
}
|
|
776
|
+
|
|
777
|
+
function pythonVenvOk(pythonBin) {
|
|
778
|
+
if (!pythonBin) return false;
|
|
779
|
+
const tempRoot = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-python-check-"));
|
|
780
|
+
try {
|
|
781
|
+
const result = spawnSync(pythonBin, ["-m", "venv", path.join(tempRoot, "venv")], {
|
|
782
|
+
stdio: "ignore",
|
|
783
|
+
});
|
|
784
|
+
return result.status === 0;
|
|
785
|
+
} finally {
|
|
786
|
+
fs.rmSync(tempRoot, { recursive: true, force: true });
|
|
787
|
+
}
|
|
788
|
+
}
|
|
789
|
+
|
|
790
|
+
function pythonProbe(pythonBin) {
|
|
791
|
+
const version = pythonVersion(pythonBin);
|
|
792
|
+
const version_ok = pythonOk(version);
|
|
793
|
+
const venv_ok = version_ok ? pythonVenvOk(pythonBin) : false;
|
|
794
|
+
return {
|
|
795
|
+
path: pythonBin || "",
|
|
796
|
+
version: version?.version || null,
|
|
797
|
+
version_ok,
|
|
798
|
+
venv_ok,
|
|
799
|
+
ok: version_ok && venv_ok,
|
|
800
|
+
};
|
|
801
|
+
}
|
|
802
|
+
|
|
803
|
+
function selectedPythonProbe() {
|
|
804
|
+
const candidates = [];
|
|
805
|
+
if (process.env.OPENCLAW_AGENT_WALLET_PYTHON) {
|
|
806
|
+
candidates.push(process.env.OPENCLAW_AGENT_WALLET_PYTHON);
|
|
807
|
+
} else {
|
|
808
|
+
for (const name of ["python3.14", "python3.13", "python3.12", "python3.11", "python3.10", "python3"]) {
|
|
809
|
+
const found = commandPath(name);
|
|
810
|
+
if (found && !candidates.includes(found)) candidates.push(found);
|
|
811
|
+
}
|
|
812
|
+
}
|
|
813
|
+
|
|
814
|
+
const probes = candidates.map((candidate) => pythonProbe(candidate));
|
|
815
|
+
return probes.find((probe) => probe.ok) || probes[0] || pythonProbe("");
|
|
816
|
+
}
|
|
817
|
+
|
|
818
|
+
function readLinkOrNull(target) {
|
|
819
|
+
try {
|
|
820
|
+
const stat = fs.lstatSync(target);
|
|
821
|
+
if (!stat.isSymbolicLink()) return null;
|
|
822
|
+
return fs.readlinkSync(target);
|
|
823
|
+
} catch (error) {
|
|
824
|
+
if (error?.code === "ENOENT") return null;
|
|
825
|
+
throw error;
|
|
826
|
+
}
|
|
827
|
+
}
|
|
828
|
+
|
|
829
|
+
function listReleases(env = process.env) {
|
|
830
|
+
const releasesDir = path.join(resolveRuntimeBase(env), "releases");
|
|
831
|
+
try {
|
|
832
|
+
return fs
|
|
833
|
+
.readdirSync(releasesDir, { withFileTypes: true })
|
|
834
|
+
.filter((entry) => entry.isDirectory() && !entry.name.startsWith("."))
|
|
835
|
+
.map((entry) => entry.name)
|
|
836
|
+
.sort();
|
|
837
|
+
} catch (error) {
|
|
838
|
+
if (error?.code === "ENOENT") return [];
|
|
839
|
+
throw error;
|
|
840
|
+
}
|
|
841
|
+
}
|
|
842
|
+
|
|
843
|
+
function listFailedReleases(env = process.env) {
|
|
844
|
+
const releasesDir = path.join(resolveRuntimeBase(env), "releases");
|
|
845
|
+
try {
|
|
846
|
+
return fs
|
|
847
|
+
.readdirSync(releasesDir, { withFileTypes: true })
|
|
848
|
+
.filter((entry) => entry.isDirectory() && entry.name.startsWith(".failed-"))
|
|
849
|
+
.map((entry) => entry.name)
|
|
850
|
+
.sort();
|
|
851
|
+
} catch (error) {
|
|
852
|
+
if (error?.code === "ENOENT") return [];
|
|
853
|
+
throw error;
|
|
854
|
+
}
|
|
855
|
+
}
|
|
856
|
+
|
|
857
|
+
function activeVersion(env = process.env) {
|
|
858
|
+
const current = currentRuntimePath(env);
|
|
859
|
+
const link = readLinkOrNull(current);
|
|
860
|
+
if (!link) return null;
|
|
861
|
+
return path.basename(path.resolve(path.dirname(current), link));
|
|
862
|
+
}
|
|
863
|
+
|
|
864
|
+
function detectRuntimeVersion(runtimeRoot) {
|
|
865
|
+
try {
|
|
866
|
+
const packageJsonText = fs.readFileSync(path.join(runtimeRoot, "package.json"), "utf8");
|
|
867
|
+
const pkg = JSON.parse(packageJsonText);
|
|
868
|
+
return String(pkg.version || "").trim() || null;
|
|
869
|
+
} catch {
|
|
870
|
+
// ignored
|
|
871
|
+
}
|
|
872
|
+
try {
|
|
873
|
+
const pyprojectText = fs.readFileSync(path.join(runtimeRoot, "agent-wallet", "pyproject.toml"), "utf8");
|
|
874
|
+
const match = pyprojectText.match(/^version = "([^"]+)"/m);
|
|
875
|
+
return match?.[1] || null;
|
|
876
|
+
} catch {
|
|
877
|
+
return null;
|
|
878
|
+
}
|
|
879
|
+
}
|
|
880
|
+
|
|
881
|
+
function uniquePathWithSuffix(targetPath) {
|
|
882
|
+
if (!fs.existsSync(targetPath)) return targetPath;
|
|
883
|
+
let counter = 2;
|
|
884
|
+
while (true) {
|
|
885
|
+
const candidate = `${targetPath}-${counter}`;
|
|
886
|
+
if (!fs.existsSync(candidate)) return candidate;
|
|
887
|
+
counter += 1;
|
|
888
|
+
}
|
|
889
|
+
}
|
|
890
|
+
|
|
891
|
+
function migrateDirectoryRuntimePointer(linkPath) {
|
|
892
|
+
const stat = fs.lstatSync(linkPath);
|
|
893
|
+
if (!stat.isDirectory()) return null;
|
|
894
|
+
const runtimeBase = path.dirname(linkPath);
|
|
895
|
+
const releasesDir = path.join(runtimeBase, "releases");
|
|
896
|
+
fs.mkdirSync(releasesDir, { recursive: true });
|
|
897
|
+
const detectedVersion = detectRuntimeVersion(linkPath);
|
|
898
|
+
const baseName = detectedVersion ? `${detectedVersion}-migrated` : `legacy-current-${Date.now()}`;
|
|
899
|
+
const destination = uniquePathWithSuffix(path.join(releasesDir, baseName));
|
|
900
|
+
fs.renameSync(linkPath, destination);
|
|
901
|
+
return destination;
|
|
902
|
+
}
|
|
903
|
+
|
|
904
|
+
function existingRuntimePointerTarget(linkPath) {
|
|
905
|
+
const link = readLinkOrNull(linkPath);
|
|
906
|
+
if (link) {
|
|
907
|
+
return path.resolve(path.dirname(linkPath), link);
|
|
908
|
+
}
|
|
909
|
+
try {
|
|
910
|
+
const stat = fs.lstatSync(linkPath);
|
|
911
|
+
if (stat.isDirectory()) {
|
|
912
|
+
return migrateDirectoryRuntimePointer(linkPath);
|
|
913
|
+
}
|
|
914
|
+
} catch (error) {
|
|
915
|
+
if (error?.code !== "ENOENT") throw error;
|
|
916
|
+
}
|
|
917
|
+
return null;
|
|
918
|
+
}
|
|
919
|
+
|
|
920
|
+
function listDirectories(rootPath) {
|
|
921
|
+
try {
|
|
922
|
+
return fs
|
|
923
|
+
.readdirSync(rootPath, { withFileTypes: true })
|
|
924
|
+
.filter((entry) => entry.isDirectory())
|
|
925
|
+
.map((entry) => entry.name)
|
|
926
|
+
.sort();
|
|
927
|
+
} catch (error) {
|
|
928
|
+
if (error?.code === "ENOENT") return [];
|
|
929
|
+
throw error;
|
|
930
|
+
}
|
|
931
|
+
}
|
|
932
|
+
|
|
933
|
+
function activePythonRuntimeInfo(env = process.env) {
|
|
934
|
+
const currentRoot = resolvedCurrentRuntimeRoot(env);
|
|
935
|
+
if (!currentRoot) return null;
|
|
936
|
+
const linkPath = path.join(currentRoot, "agent-wallet", ".runtime-venv");
|
|
937
|
+
const exists = fs.existsSync(linkPath);
|
|
938
|
+
const symlinkTarget = readLinkOrNull(linkPath);
|
|
939
|
+
const resolvedTarget = exists ? path.resolve(path.dirname(linkPath), symlinkTarget || ".") : null;
|
|
940
|
+
return {
|
|
941
|
+
link_path: linkPath,
|
|
942
|
+
exists,
|
|
943
|
+
symlink: Boolean(symlinkTarget),
|
|
944
|
+
target: symlinkTarget || null,
|
|
945
|
+
resolved_target: resolvedTarget,
|
|
946
|
+
shared: Boolean(resolvedTarget && resolvedTarget.includes(`${path.sep}shared${path.sep}python${path.sep}`)),
|
|
947
|
+
};
|
|
948
|
+
}
|
|
949
|
+
|
|
950
|
+
function activeNodeRuntimeInfo(env = process.env) {
|
|
951
|
+
const currentRoot = resolvedCurrentRuntimeRoot(env);
|
|
952
|
+
if (!currentRoot) return [];
|
|
953
|
+
const projects = [path.join(currentRoot, "wdk-evm-wallet")];
|
|
954
|
+
return projects
|
|
955
|
+
.filter((projectRoot) => fs.existsSync(path.join(projectRoot, "package.json")))
|
|
956
|
+
.map((projectRoot) => {
|
|
957
|
+
const linkPath = path.join(projectRoot, "node_modules");
|
|
958
|
+
const exists = fs.existsSync(linkPath);
|
|
959
|
+
const symlinkTarget = readLinkOrNull(linkPath);
|
|
960
|
+
const resolvedTarget = exists ? path.resolve(path.dirname(linkPath), symlinkTarget || ".") : null;
|
|
961
|
+
return {
|
|
962
|
+
project_root: projectRoot,
|
|
963
|
+
project_name: path.basename(projectRoot),
|
|
964
|
+
link_path: linkPath,
|
|
965
|
+
exists,
|
|
966
|
+
symlink: Boolean(symlinkTarget),
|
|
967
|
+
target: symlinkTarget || null,
|
|
968
|
+
resolved_target: resolvedTarget,
|
|
969
|
+
shared: Boolean(resolvedTarget && resolvedTarget.includes(`${path.sep}shared${path.sep}node${path.sep}`)),
|
|
970
|
+
};
|
|
971
|
+
});
|
|
972
|
+
}
|
|
973
|
+
|
|
974
|
+
function sharedSnapshotInventory(env = process.env) {
|
|
975
|
+
const runtimeBase = resolveRuntimeBase(env);
|
|
976
|
+
const sharedRoot = path.join(runtimeBase, "shared");
|
|
977
|
+
const pythonRoot = path.join(sharedRoot, "python");
|
|
978
|
+
const nodeRoot = path.join(sharedRoot, "node");
|
|
979
|
+
const nodeProjects = listDirectories(nodeRoot).map((projectName) => ({
|
|
980
|
+
project_name: projectName,
|
|
981
|
+
snapshots: listDirectories(path.join(nodeRoot, projectName)),
|
|
982
|
+
}));
|
|
983
|
+
return {
|
|
984
|
+
shared_root: sharedRoot,
|
|
985
|
+
python_snapshots: listDirectories(pythonRoot),
|
|
986
|
+
node_projects: nodeProjects,
|
|
987
|
+
};
|
|
988
|
+
}
|
|
989
|
+
|
|
990
|
+
function switchSymlink(linkPath, targetPath) {
|
|
991
|
+
const absoluteTarget = path.resolve(targetPath);
|
|
992
|
+
if (!fs.existsSync(absoluteTarget)) {
|
|
993
|
+
throw new Error(`Cannot switch runtime: target does not exist: ${absoluteTarget}`);
|
|
994
|
+
}
|
|
995
|
+
|
|
996
|
+
fs.mkdirSync(path.dirname(linkPath), { recursive: true });
|
|
997
|
+
const tempLink = `${linkPath}.tmp-${process.pid}`;
|
|
998
|
+
try {
|
|
999
|
+
fs.rmSync(tempLink, { force: true, recursive: true });
|
|
1000
|
+
} catch {
|
|
1001
|
+
// ignored
|
|
1002
|
+
}
|
|
1003
|
+
fs.symlinkSync(absoluteTarget, tempLink, "dir");
|
|
1004
|
+
|
|
1005
|
+
try {
|
|
1006
|
+
const existing = fs.lstatSync(linkPath);
|
|
1007
|
+
if (!existing.isSymbolicLink()) {
|
|
1008
|
+
fs.rmSync(tempLink, { force: true, recursive: true });
|
|
1009
|
+
throw new Error(`${linkPath} exists and is not a symlink. Refusing to replace it.`);
|
|
1010
|
+
}
|
|
1011
|
+
} catch (error) {
|
|
1012
|
+
if (error?.code !== "ENOENT") throw error;
|
|
1013
|
+
}
|
|
1014
|
+
|
|
1015
|
+
fs.renameSync(tempLink, linkPath);
|
|
1016
|
+
}
|
|
1017
|
+
|
|
1018
|
+
// Remove a runtime pointer symlink (current/previous). recursive+force tolerate
|
|
1019
|
+
// a stray directory in that slot; on a symlink this unlinks the pointer only and
|
|
1020
|
+
// never deletes the release directory it targets.
|
|
1021
|
+
function removeRuntimePointer(pointerPath) {
|
|
1022
|
+
try {
|
|
1023
|
+
fs.rmSync(pointerPath, { recursive: true, force: true });
|
|
1024
|
+
} catch (error) {
|
|
1025
|
+
if (error?.code !== "ENOENT") throw error;
|
|
1026
|
+
}
|
|
1027
|
+
}
|
|
1028
|
+
|
|
1029
|
+
function parseFlagValue(args, name) {
|
|
1030
|
+
const prefix = `${name}=`;
|
|
1031
|
+
for (let index = 0; index < args.length; index += 1) {
|
|
1032
|
+
const value = args[index];
|
|
1033
|
+
if (value === name) return args[index + 1] || "";
|
|
1034
|
+
if (value.startsWith(prefix)) return value.slice(prefix.length);
|
|
1035
|
+
}
|
|
1036
|
+
return "";
|
|
1037
|
+
}
|
|
1038
|
+
|
|
1039
|
+
function hasFlag(args, name) {
|
|
1040
|
+
return args.includes(name) || args.some((value) => value.startsWith(`${name}=`));
|
|
1041
|
+
}
|
|
1042
|
+
|
|
1043
|
+
function withoutCliOnlyArgs(args) {
|
|
1044
|
+
const output = [];
|
|
1045
|
+
for (let index = 0; index < args.length; index += 1) {
|
|
1046
|
+
const value = args[index];
|
|
1047
|
+
if (
|
|
1048
|
+
value === "--yes" ||
|
|
1049
|
+
value === "--auto-secrets" ||
|
|
1050
|
+
value === "--no-auto-secrets" ||
|
|
1051
|
+
value === "--force" ||
|
|
1052
|
+
value === "--skip-enable"
|
|
1053
|
+
) {
|
|
1054
|
+
continue;
|
|
1055
|
+
}
|
|
1056
|
+
if (value === "--to") {
|
|
1057
|
+
index += 1;
|
|
1058
|
+
continue;
|
|
1059
|
+
}
|
|
1060
|
+
if (value.startsWith("--to=")) {
|
|
1061
|
+
continue;
|
|
1062
|
+
}
|
|
1063
|
+
output.push(value);
|
|
1064
|
+
}
|
|
1065
|
+
return stripUniversalInstallerArgs(output);
|
|
1066
|
+
}
|
|
1067
|
+
|
|
1068
|
+
function extractTrailingJson(text) {
|
|
1069
|
+
const raw = String(text || "");
|
|
1070
|
+
const newlineStart = raw.lastIndexOf("\n{");
|
|
1071
|
+
const start = newlineStart >= 0 ? newlineStart + 1 : raw.indexOf("{");
|
|
1072
|
+
if (start < 0) {
|
|
1073
|
+
throw new Error("Could not find JSON payload in command output.");
|
|
1074
|
+
}
|
|
1075
|
+
return JSON.parse(raw.slice(start));
|
|
1076
|
+
}
|
|
1077
|
+
|
|
1078
|
+
function pathVersionFromRuntimeRoot(runtimeRoot) {
|
|
1079
|
+
if (!runtimeRoot) return null;
|
|
1080
|
+
const normalized = path.resolve(String(runtimeRoot));
|
|
1081
|
+
if (path.basename(path.dirname(normalized)) !== "releases") return null;
|
|
1082
|
+
return path.basename(normalized);
|
|
1083
|
+
}
|
|
1084
|
+
|
|
1085
|
+
function resolveCliPackageMeta(cliPath) {
|
|
1086
|
+
try {
|
|
1087
|
+
const root = path.resolve(path.dirname(cliPath), "..");
|
|
1088
|
+
const pkg = JSON.parse(fs.readFileSync(path.join(root, "package.json"), "utf8"));
|
|
1089
|
+
return {
|
|
1090
|
+
name: String(pkg.name || packageJson.name),
|
|
1091
|
+
version: String(pkg.version || ""),
|
|
1092
|
+
root,
|
|
1093
|
+
};
|
|
1094
|
+
} catch {
|
|
1095
|
+
return {
|
|
1096
|
+
name: packageJson.name,
|
|
1097
|
+
version: "",
|
|
1098
|
+
root: "",
|
|
1099
|
+
};
|
|
1100
|
+
}
|
|
1101
|
+
}
|
|
1102
|
+
|
|
1103
|
+
function summarizeDependencyPlan(payload) {
|
|
1104
|
+
const python = payload?.python_runtime && typeof payload.python_runtime === "object"
|
|
1105
|
+
? {
|
|
1106
|
+
action: payload.python_runtime.action || "unknown",
|
|
1107
|
+
shared: Boolean(payload.python_runtime.shared),
|
|
1108
|
+
fingerprint: payload.python_runtime.fingerprint || null,
|
|
1109
|
+
}
|
|
1110
|
+
: null;
|
|
1111
|
+
const nodeProjects = Array.isArray(payload?.node_runtime?.projects)
|
|
1112
|
+
? payload.node_runtime.projects.map((project) => ({
|
|
1113
|
+
project_root: project.project_root,
|
|
1114
|
+
action: project.action || "unknown",
|
|
1115
|
+
shared: Boolean(project.shared),
|
|
1116
|
+
fingerprint: project.fingerprint || null,
|
|
1117
|
+
}))
|
|
1118
|
+
: [];
|
|
1119
|
+
return { python, node_projects: nodeProjects };
|
|
1120
|
+
}
|
|
1121
|
+
|
|
1122
|
+
function token() {
|
|
1123
|
+
return crypto.randomBytes(32).toString("base64url");
|
|
1124
|
+
}
|
|
1125
|
+
|
|
1126
|
+
function envFileSet(pathname, updates) {
|
|
1127
|
+
fs.mkdirSync(path.dirname(pathname), { recursive: true });
|
|
1128
|
+
let lines = [];
|
|
1129
|
+
try {
|
|
1130
|
+
lines = fs.readFileSync(pathname, "utf8").split(/\r?\n/);
|
|
1131
|
+
} catch (error) {
|
|
1132
|
+
if (error?.code !== "ENOENT") throw error;
|
|
1133
|
+
}
|
|
1134
|
+
|
|
1135
|
+
const pending = new Map(Object.entries(updates));
|
|
1136
|
+
const next = [];
|
|
1137
|
+
for (const line of lines) {
|
|
1138
|
+
const match = line.match(/^([A-Za-z_][A-Za-z0-9_]*)=/);
|
|
1139
|
+
if (!match || !pending.has(match[1])) {
|
|
1140
|
+
if (line.length > 0) next.push(line);
|
|
1141
|
+
continue;
|
|
1142
|
+
}
|
|
1143
|
+
next.push(`${match[1]}=${pending.get(match[1])}`);
|
|
1144
|
+
pending.delete(match[1]);
|
|
1145
|
+
}
|
|
1146
|
+
for (const [key, value] of pending) {
|
|
1147
|
+
next.push(`${key}=${value}`);
|
|
1148
|
+
}
|
|
1149
|
+
fs.writeFileSync(pathname, `${next.join("\n")}\n`, { mode: 0o600 });
|
|
1150
|
+
try {
|
|
1151
|
+
fs.chmodSync(pathname, 0o600);
|
|
1152
|
+
} catch {
|
|
1153
|
+
// chmod can fail on some filesystems; the write mode above is the primary path.
|
|
1154
|
+
}
|
|
1155
|
+
}
|
|
1156
|
+
|
|
1157
|
+
function envFileUnset(pathname, keys) {
|
|
1158
|
+
let lines = [];
|
|
1159
|
+
try {
|
|
1160
|
+
lines = fs.readFileSync(pathname, "utf8").split(/\r?\n/);
|
|
1161
|
+
} catch (error) {
|
|
1162
|
+
if (error?.code === "ENOENT") return;
|
|
1163
|
+
throw error;
|
|
1164
|
+
}
|
|
1165
|
+
const blocked = new Set(keys);
|
|
1166
|
+
const next = [];
|
|
1167
|
+
for (const line of lines) {
|
|
1168
|
+
const match = line.match(/^([A-Za-z_][A-Za-z0-9_]*)=/);
|
|
1169
|
+
if (match && blocked.has(match[1])) {
|
|
1170
|
+
continue;
|
|
1171
|
+
}
|
|
1172
|
+
if (line.length > 0) next.push(line);
|
|
1173
|
+
}
|
|
1174
|
+
fs.writeFileSync(pathname, `${next.join("\n")}\n`, { mode: 0o600 });
|
|
1175
|
+
try {
|
|
1176
|
+
fs.chmodSync(pathname, 0o600);
|
|
1177
|
+
} catch {
|
|
1178
|
+
// ignored
|
|
1179
|
+
}
|
|
1180
|
+
}
|
|
1181
|
+
|
|
1182
|
+
function readEnvFile(pathname) {
|
|
1183
|
+
try {
|
|
1184
|
+
const result = {};
|
|
1185
|
+
for (const line of fs.readFileSync(pathname, "utf8").split(/\r?\n/)) {
|
|
1186
|
+
const match = line.match(/^([A-Za-z_][A-Za-z0-9_]*)=(.*)$/);
|
|
1187
|
+
if (match) result[match[1]] = match[2];
|
|
1188
|
+
}
|
|
1189
|
+
return result;
|
|
1190
|
+
} catch (error) {
|
|
1191
|
+
if (error?.code === "ENOENT") return {};
|
|
1192
|
+
throw error;
|
|
1193
|
+
}
|
|
1194
|
+
}
|
|
1195
|
+
|
|
1196
|
+
function readJsonFile(pathname) {
|
|
1197
|
+
try {
|
|
1198
|
+
return JSON.parse(fs.readFileSync(pathname, "utf8"));
|
|
1199
|
+
} catch (error) {
|
|
1200
|
+
if (error?.code === "ENOENT") return null;
|
|
1201
|
+
throw error;
|
|
1202
|
+
}
|
|
1203
|
+
}
|
|
1204
|
+
|
|
1205
|
+
function writeJsonFile(pathname, value) {
|
|
1206
|
+
fs.mkdirSync(path.dirname(pathname), { recursive: true });
|
|
1207
|
+
fs.writeFileSync(pathname, `${JSON.stringify(value, null, 2)}\n`);
|
|
1208
|
+
}
|
|
1209
|
+
|
|
1210
|
+
function writeJsonFileAtomic(pathname, value, mode = 0o600) {
|
|
1211
|
+
fs.mkdirSync(path.dirname(pathname), { recursive: true });
|
|
1212
|
+
const tempPath = `${pathname}.tmp-${process.pid}-${Date.now()}`;
|
|
1213
|
+
try {
|
|
1214
|
+
fs.writeFileSync(tempPath, `${JSON.stringify(value, null, 2)}\n`, { mode });
|
|
1215
|
+
fs.renameSync(tempPath, pathname);
|
|
1216
|
+
fs.chmodSync(pathname, mode);
|
|
1217
|
+
} finally {
|
|
1218
|
+
try {
|
|
1219
|
+
fs.rmSync(tempPath, { force: true });
|
|
1220
|
+
} catch {
|
|
1221
|
+
// ignored
|
|
1222
|
+
}
|
|
1223
|
+
}
|
|
1224
|
+
}
|
|
1225
|
+
|
|
1226
|
+
function bootKeys(env = process.env) {
|
|
1227
|
+
return createBootKeyManager({
|
|
1228
|
+
runtimeBase: resolveRuntimeBase(env),
|
|
1229
|
+
openclawHome: resolveOpenclawHome(env),
|
|
1230
|
+
currentRuntimeRoot: () => resolvedCurrentRuntimeRoot(env),
|
|
1231
|
+
resolveVenvPython,
|
|
1232
|
+
expandHome,
|
|
1233
|
+
bridgeTimeoutMs: positiveIntEnv(
|
|
1234
|
+
"AGENT_WALLET_KEYSTORE_BRIDGE_TIMEOUT_MS",
|
|
1235
|
+
KEYSTORE_BRIDGE_TIMEOUT_MS,
|
|
1236
|
+
env,
|
|
1237
|
+
),
|
|
1238
|
+
env,
|
|
1239
|
+
});
|
|
1240
|
+
}
|
|
1241
|
+
|
|
1242
|
+
function ensureBootKeyFile(env = process.env) {
|
|
1243
|
+
return bootKeys(env).ensureFile();
|
|
1244
|
+
}
|
|
1245
|
+
|
|
1246
|
+
function readBootKeyFromRuntimeResolver(env = process.env) {
|
|
1247
|
+
return bootKeys(env).resolveFromRuntime();
|
|
1248
|
+
}
|
|
1249
|
+
|
|
1250
|
+
function resolveLegacyInstallerBootKey(env = process.env) {
|
|
1251
|
+
return bootKeys(env).resolveLegacy();
|
|
1252
|
+
}
|
|
1253
|
+
|
|
1254
|
+
function provisionBootKeyToKeystore(releaseRoot, env, bootKey) {
|
|
1255
|
+
return bootKeys(env).provision(releaseRoot, bootKey);
|
|
1256
|
+
}
|
|
1257
|
+
|
|
1258
|
+
function printMacOSKeychainNotice(env = process.env) {
|
|
1259
|
+
if (process.platform !== "darwin") return;
|
|
1260
|
+
const preference = String(env.AGENT_WALLET_KEYSTORE_BACKEND || "auto").trim().toLowerCase();
|
|
1261
|
+
if (["plain", "plaintext", "plaintext-file", "file"].includes(preference)) return;
|
|
1262
|
+
console.error(
|
|
1263
|
+
"Security notice: OneCraft uses your macOS login Keychain for the wallet boot key by default. macOS may ask for Keychain access; approve it only if you initiated this install or update.",
|
|
1264
|
+
);
|
|
1265
|
+
}
|
|
1266
|
+
|
|
1267
|
+
function resolveVenvPython(releaseRoot) {
|
|
1268
|
+
const candidates = [
|
|
1269
|
+
path.join(releaseRoot, "agent-wallet", ".venv", "bin", "python"),
|
|
1270
|
+
path.join(releaseRoot, "agent-wallet", ".runtime-venv", "bin", "python"),
|
|
1271
|
+
];
|
|
1272
|
+
for (const candidate of candidates) {
|
|
1273
|
+
if (fs.existsSync(candidate)) return candidate;
|
|
1274
|
+
}
|
|
1275
|
+
return null;
|
|
1276
|
+
}
|
|
1277
|
+
|
|
1278
|
+
function resolveUniversalMcpLauncher(env = process.env) {
|
|
1279
|
+
// Keep the lexical `current` path in generated MCP config. Resolving the
|
|
1280
|
+
// symlink here would pin clients to one release and defeat seamless updates.
|
|
1281
|
+
const currentRoot = currentRuntimePath(env);
|
|
1282
|
+
const candidates = [currentRoot ? path.join(currentRoot, "mcp", "scripts", "run_mcp.sh") : null];
|
|
1283
|
+
for (const candidate of candidates) {
|
|
1284
|
+
if (candidate && fs.existsSync(candidate)) return candidate;
|
|
1285
|
+
}
|
|
1286
|
+
return null;
|
|
1287
|
+
}
|
|
1288
|
+
|
|
1289
|
+
function universalMcpConfig(env = process.env) {
|
|
1290
|
+
const launcher = resolveUniversalMcpLauncher(env);
|
|
1291
|
+
if (!launcher) {
|
|
1292
|
+
throw new Error("Universal MCP launcher is missing. Run: wallet install --yes");
|
|
1293
|
+
}
|
|
1294
|
+
return {
|
|
1295
|
+
mcpServers: {
|
|
1296
|
+
"agent-wallet": {
|
|
1297
|
+
command: "sh",
|
|
1298
|
+
args: [launcher],
|
|
1299
|
+
env: {
|
|
1300
|
+
AGENT_WALLET_HOST: "mcp",
|
|
1301
|
+
FASTMCP_LOG_LEVEL: "ERROR",
|
|
1302
|
+
FASTMCP_SHOW_SERVER_BANNER: "false",
|
|
1303
|
+
OPENCLAW_HOME: resolveOpenclawHome(env),
|
|
1304
|
+
},
|
|
1305
|
+
},
|
|
1306
|
+
},
|
|
1307
|
+
};
|
|
1308
|
+
}
|
|
1309
|
+
|
|
1310
|
+
function runUniversalMcpCommand(args = [], env = process.env) {
|
|
1311
|
+
const subcommand = args[0] || "config";
|
|
1312
|
+
if (subcommand === "config") {
|
|
1313
|
+
if (args.length > 1 && !(args.length === 2 && args[1] === "--json")) {
|
|
1314
|
+
console.error("Usage: wallet mcp config [--json]");
|
|
1315
|
+
return 2;
|
|
1316
|
+
}
|
|
1317
|
+
try {
|
|
1318
|
+
console.log(JSON.stringify(universalMcpConfig(env), null, 2));
|
|
1319
|
+
return 0;
|
|
1320
|
+
} catch (error) {
|
|
1321
|
+
console.error(JSON.stringify({ ok: false, error: error.message }));
|
|
1322
|
+
return 1;
|
|
1323
|
+
}
|
|
1324
|
+
}
|
|
1325
|
+
if (subcommand === "path") {
|
|
1326
|
+
if (args.length !== 1) {
|
|
1327
|
+
console.error("Usage: wallet mcp path");
|
|
1328
|
+
return 2;
|
|
1329
|
+
}
|
|
1330
|
+
const launcher = resolveUniversalMcpLauncher(env);
|
|
1331
|
+
if (!launcher) {
|
|
1332
|
+
console.error(JSON.stringify({ ok: false, error: "Universal MCP launcher is missing. Run: wallet install --yes" }));
|
|
1333
|
+
return 1;
|
|
1334
|
+
}
|
|
1335
|
+
console.log(launcher);
|
|
1336
|
+
return 0;
|
|
1337
|
+
}
|
|
1338
|
+
if (subcommand === "serve") {
|
|
1339
|
+
if (args.length !== 1) {
|
|
1340
|
+
console.error("Usage: wallet mcp serve");
|
|
1341
|
+
return 2;
|
|
1342
|
+
}
|
|
1343
|
+
const launcher = resolveUniversalMcpLauncher(env);
|
|
1344
|
+
if (!launcher) {
|
|
1345
|
+
console.error(JSON.stringify({ ok: false, error: "Universal MCP launcher is missing. Run: wallet install --yes" }));
|
|
1346
|
+
return 1;
|
|
1347
|
+
}
|
|
1348
|
+
const result = spawnSync("sh", [launcher], {
|
|
1349
|
+
env: { ...env, AGENT_WALLET_HOST: env.AGENT_WALLET_HOST || "mcp" },
|
|
1350
|
+
stdio: "inherit",
|
|
1351
|
+
});
|
|
1352
|
+
if (result.error) {
|
|
1353
|
+
console.error(JSON.stringify({ ok: false, error: result.error.message }));
|
|
1354
|
+
return 1;
|
|
1355
|
+
}
|
|
1356
|
+
return result.status ?? 1;
|
|
1357
|
+
}
|
|
1358
|
+
console.error(`Unknown mcp command: ${subcommand}`);
|
|
1359
|
+
console.error("Use: wallet mcp serve | path | config");
|
|
1360
|
+
return 2;
|
|
1361
|
+
}
|
|
1362
|
+
|
|
1363
|
+
// Classify a verification failure so the caller can route the right guidance:
|
|
1364
|
+
// broken_release -> our shipped code is bad; user cannot fix, stay on previous.
|
|
1365
|
+
// local_env -> user's machine/runtime is fixable (python/venv/corrupt unpack).
|
|
1366
|
+
// unknown -> fall back to generic guidance.
|
|
1367
|
+
function classifyVerifyError(detail) {
|
|
1368
|
+
const text = String(detail || "");
|
|
1369
|
+
if (/SyntaxError|IndentationError|ImportError|ModuleNotFoundError|TabError|NameError/.test(text)) {
|
|
1370
|
+
return "broken_release";
|
|
1371
|
+
}
|
|
1372
|
+
if (/ENOENT|python|venv|ensurepip|not found|No such file|Permission denied|spawn/i.test(text)) {
|
|
1373
|
+
return "local_env";
|
|
1374
|
+
}
|
|
1375
|
+
return "unknown";
|
|
1376
|
+
}
|
|
1377
|
+
|
|
1378
|
+
function verifyRuntime(releaseRoot, env = process.env) {
|
|
1379
|
+
if (String(env.AGENT_WALLET_VERIFY_DISABLE || "") === "1") {
|
|
1380
|
+
return { ok: true, skipped: true };
|
|
1381
|
+
}
|
|
1382
|
+
if (String(env.AGENT_WALLET_VERIFY_FORCE_FAIL || "") === "1") {
|
|
1383
|
+
return { ok: false, error: "verify forced to fail (AGENT_WALLET_VERIFY_FORCE_FAIL)", category: "broken_release" };
|
|
1384
|
+
}
|
|
1385
|
+
const serverPy = path.join(releaseRoot, "codex", "plugins", "agent-wallet", "server.py");
|
|
1386
|
+
if (!fs.existsSync(serverPy)) {
|
|
1387
|
+
return { ok: false, error: `server.py missing at ${serverPy}`, category: "local_env" };
|
|
1388
|
+
}
|
|
1389
|
+
const python =
|
|
1390
|
+
releaseCheckPython(resolveVenvPython(releaseRoot), env) || commandPath("python3") || "python3";
|
|
1391
|
+
const initLine = JSON.stringify({
|
|
1392
|
+
jsonrpc: "2.0",
|
|
1393
|
+
id: 1,
|
|
1394
|
+
method: "initialize",
|
|
1395
|
+
params: { protocolVersion: "2024-11-05", capabilities: {}, clientInfo: { name: "verify", version: "0" } },
|
|
1396
|
+
});
|
|
1397
|
+
const probe = spawnSync(python, [serverPy], {
|
|
1398
|
+
input: initLine + "\n",
|
|
1399
|
+
encoding: "utf8",
|
|
1400
|
+
timeout: Number(env.AGENT_WALLET_VERIFY_TIMEOUT_MS || 25000),
|
|
1401
|
+
env: { ...env, FASTMCP_SHOW_SERVER_BANNER: "false", FASTMCP_LOG_LEVEL: "ERROR" },
|
|
1402
|
+
});
|
|
1403
|
+
if (probe.error) {
|
|
1404
|
+
const isTimeout = String(probe.error.message || "").includes("ETIMEDOUT");
|
|
1405
|
+
return {
|
|
1406
|
+
ok: false,
|
|
1407
|
+
error: `handshake ${isTimeout ? "timed out (server did not respond)" : "spawn failed"}: ${probe.error.message}`,
|
|
1408
|
+
category: isTimeout ? "broken_release" : "local_env",
|
|
1409
|
+
};
|
|
1410
|
+
}
|
|
1411
|
+
const out = String(probe.stdout || "");
|
|
1412
|
+
if (out.includes('"serverInfo"')) {
|
|
1413
|
+
return { ok: true };
|
|
1414
|
+
}
|
|
1415
|
+
const detail = (probe.stderr || out || "").trim().split("\n").slice(-3).join(" ");
|
|
1416
|
+
return {
|
|
1417
|
+
ok: false,
|
|
1418
|
+
error: `MCP initialize handshake failed: ${detail || "no serverInfo in response"}`,
|
|
1419
|
+
category: classifyVerifyError(detail),
|
|
1420
|
+
};
|
|
1421
|
+
}
|
|
1422
|
+
|
|
1423
|
+
function verifyBootKeyWithRuntime(releaseRoot, env = process.env) {
|
|
1424
|
+
return bootKeys(env).verifyWithRuntime(releaseRoot);
|
|
1425
|
+
}
|
|
1426
|
+
|
|
1427
|
+
function resolveEditorServerChecks(env = process.env) {
|
|
1428
|
+
const checks = [];
|
|
1429
|
+
// Claude Code's launcher (run_mcp.sh) falls back to the runtime codex
|
|
1430
|
+
// server.py when its own plugin-cache copy lacks one, so a present runtime
|
|
1431
|
+
// server.py is exactly what that launcher will exec. We check its presence
|
|
1432
|
+
// as the proxy for "Claude can resolve a server" (we do not inspect the
|
|
1433
|
+
// version-specific cache copy directly).
|
|
1434
|
+
const root = resolvedCurrentRuntimeRoot(env);
|
|
1435
|
+
const runtimeCodex = root ? path.join(root, "codex", "plugins", "agent-wallet", "server.py") : null;
|
|
1436
|
+
const claudeCacheRoot = expandHome("~/.claude/plugins/cache");
|
|
1437
|
+
if (fs.existsSync(claudeCacheRoot)) {
|
|
1438
|
+
const reachable = Boolean(runtimeCodex && fs.existsSync(runtimeCodex));
|
|
1439
|
+
checks.push({
|
|
1440
|
+
name: "editor:claude-code",
|
|
1441
|
+
ok: reachable,
|
|
1442
|
+
error: reachable ? "" : "Claude cache copy cannot resolve server.py from runtime",
|
|
1443
|
+
fix: reachable ? "" : `${npmCommand} claude-code install --yes`,
|
|
1444
|
+
});
|
|
1445
|
+
}
|
|
1446
|
+
// Codex: plugin symlink target under the codex plugin install root.
|
|
1447
|
+
const codexInstallRoot = resolveCodexPluginInstallRoot(env);
|
|
1448
|
+
const codexTarget = path.join(codexInstallRoot, "agent-wallet", "server.py");
|
|
1449
|
+
if (fs.existsSync(codexInstallRoot)) {
|
|
1450
|
+
const ok = fs.existsSync(codexTarget);
|
|
1451
|
+
checks.push({
|
|
1452
|
+
name: "editor:codex",
|
|
1453
|
+
ok,
|
|
1454
|
+
error: ok ? "" : `codex plugin server.py missing at ${codexTarget}`,
|
|
1455
|
+
fix: ok ? "" : `${npmCommand} codex install --yes`,
|
|
1456
|
+
});
|
|
1457
|
+
}
|
|
1458
|
+
const cursorEntry = integrations(env).managed("cursor");
|
|
1459
|
+
if (cursorEntry) {
|
|
1460
|
+
const cursorTarget = path.resolve(expandHome(
|
|
1461
|
+
cursorEntry.plugin_target || path.join(resolveCursorPluginInstallRoot(env), "agent-wallet"),
|
|
1462
|
+
));
|
|
1463
|
+
const manifest = readJsonFile(path.join(cursorTarget, ".cursor-plugin", "plugin.json"));
|
|
1464
|
+
const marker = readJsonFile(path.join(cursorTarget, ".agentlayer-managed.json"));
|
|
1465
|
+
const ok = manifest?.name === "agent-wallet" && marker?.manager === "agentlayer" &&
|
|
1466
|
+
fs.existsSync(path.join(cursorTarget, "server.py"));
|
|
1467
|
+
checks.push({
|
|
1468
|
+
name: "editor:cursor",
|
|
1469
|
+
ok,
|
|
1470
|
+
error: ok ? "" : `Cursor plugin is missing or unmanaged at ${cursorTarget}`,
|
|
1471
|
+
fix: ok ? "" : `${npmCommand} cursor install --yes`,
|
|
1472
|
+
});
|
|
1473
|
+
}
|
|
1474
|
+
return checks;
|
|
1475
|
+
}
|
|
1476
|
+
|
|
1477
|
+
function runDoctor(args = []) {
|
|
1478
|
+
const deep = hasFlag(args, "--deep");
|
|
1479
|
+
const env = process.env;
|
|
1480
|
+
const checks = [];
|
|
1481
|
+
const fixInstall = `${npmCommand} install --yes`;
|
|
1482
|
+
|
|
1483
|
+
for (const command of ["node", "npm"]) {
|
|
1484
|
+
const ok = hasCommand(command);
|
|
1485
|
+
checks.push({
|
|
1486
|
+
name: `command:${command}`,
|
|
1487
|
+
ok,
|
|
1488
|
+
error: ok ? "" : `${command} not found on PATH`,
|
|
1489
|
+
fix: ok ? "" : `install ${command}`,
|
|
1490
|
+
});
|
|
1491
|
+
}
|
|
1492
|
+
const python = selectedPythonProbe();
|
|
1493
|
+
const pythonOk = Boolean(python.path && python.version_ok && python.venv_ok);
|
|
1494
|
+
checks.push({
|
|
1495
|
+
name: "python_version",
|
|
1496
|
+
ok: pythonOk,
|
|
1497
|
+
error: !python.path ? "python3 not found"
|
|
1498
|
+
: !python.version_ok ? `selected python ${python.version} < 3.10`
|
|
1499
|
+
: !python.venv_ok ? `python ${python.version} lacks venv/ensurepip` : "",
|
|
1500
|
+
fix: pythonOk ? "" : "install python>=3.10 with venv",
|
|
1501
|
+
});
|
|
1502
|
+
|
|
1503
|
+
const currentPath = currentRuntimePath(env);
|
|
1504
|
+
const currentRoot = resolvedCurrentRuntimeRoot(env);
|
|
1505
|
+
const symlinkOk = Boolean(currentRoot && fs.existsSync(currentRoot));
|
|
1506
|
+
checks.push({
|
|
1507
|
+
name: "current_symlink",
|
|
1508
|
+
ok: symlinkOk,
|
|
1509
|
+
target: readLinkOrNull(currentPath),
|
|
1510
|
+
error: symlinkOk ? "" : `current does not resolve to an existing release (${currentPath})`,
|
|
1511
|
+
fix: symlinkOk ? "" : fixInstall,
|
|
1512
|
+
});
|
|
1513
|
+
|
|
1514
|
+
const venvPython = currentRoot ? resolveVenvPython(currentRoot) : null;
|
|
1515
|
+
checks.push({
|
|
1516
|
+
name: "runtime_venv_python",
|
|
1517
|
+
ok: Boolean(venvPython),
|
|
1518
|
+
path: venvPython,
|
|
1519
|
+
error: venvPython ? "" : "runtime .runtime-venv/bin/python missing",
|
|
1520
|
+
fix: venvPython ? "" : fixInstall,
|
|
1521
|
+
});
|
|
1522
|
+
|
|
1523
|
+
const serverPy = currentRoot
|
|
1524
|
+
? path.join(currentRoot, "codex", "plugins", "agent-wallet", "server.py")
|
|
1525
|
+
: null;
|
|
1526
|
+
const serverExists = Boolean(serverPy && fs.existsSync(serverPy));
|
|
1527
|
+
let parseOk = false;
|
|
1528
|
+
if (serverExists && venvPython) {
|
|
1529
|
+
const compiled = spawnSync(venvPython, ["-m", "py_compile", serverPy], { encoding: "utf8" });
|
|
1530
|
+
parseOk = compiled.status === 0;
|
|
1531
|
+
}
|
|
1532
|
+
checks.push({
|
|
1533
|
+
name: "server_py_parses",
|
|
1534
|
+
ok: parseOk,
|
|
1535
|
+
error: !serverExists ? "runtime codex server.py missing"
|
|
1536
|
+
: !venvPython ? "server.py parse skipped (runtime venv missing)"
|
|
1537
|
+
: parseOk ? "" : "server.py present but failed to parse",
|
|
1538
|
+
fix: parseOk ? "" : fixInstall,
|
|
1539
|
+
});
|
|
1540
|
+
|
|
1541
|
+
if (deep) {
|
|
1542
|
+
const verify = currentRoot
|
|
1543
|
+
? verifyRuntime(currentRoot, env)
|
|
1544
|
+
: { ok: false, error: "no runtime to handshake", category: "local_env" };
|
|
1545
|
+
checks.push({
|
|
1546
|
+
name: "mcp_initialize_handshake",
|
|
1547
|
+
ok: verify.ok,
|
|
1548
|
+
error: verify.ok ? "" : verify.error,
|
|
1549
|
+
fix: verify.ok ? "" : `${fixInstall} (or: ${npmCommand} rollback)`,
|
|
1550
|
+
});
|
|
1551
|
+
}
|
|
1552
|
+
|
|
1553
|
+
for (const editorCheck of resolveEditorServerChecks(env)) {
|
|
1554
|
+
checks.push(editorCheck);
|
|
1555
|
+
}
|
|
1556
|
+
|
|
1557
|
+
// Informational only: never flips doctor's overall ok. Surfaces a newer
|
|
1558
|
+
// published version when the background runtime check has cached one.
|
|
1559
|
+
const update = computeUpdateAvailability(env);
|
|
1560
|
+
checks.push({
|
|
1561
|
+
name: "update_available",
|
|
1562
|
+
ok: true,
|
|
1563
|
+
available: update.available,
|
|
1564
|
+
latest: update.latest,
|
|
1565
|
+
current: update.current,
|
|
1566
|
+
error: "",
|
|
1567
|
+
fix: update.available ? `${npmCommand} update --yes` : "",
|
|
1568
|
+
});
|
|
1569
|
+
|
|
1570
|
+
// Informational only: flags when the installed runtime lags the CLI/repo
|
|
1571
|
+
// version (a local bump that was not reinstalled into the frameworks).
|
|
1572
|
+
const rsync = computeRuntimeInSync(env);
|
|
1573
|
+
checks.push({
|
|
1574
|
+
name: "runtime_in_sync",
|
|
1575
|
+
ok: true,
|
|
1576
|
+
in_sync: rsync.in_sync,
|
|
1577
|
+
active_version: rsync.active_version,
|
|
1578
|
+
cli_version: rsync.cli_version,
|
|
1579
|
+
error: "",
|
|
1580
|
+
fix: rsync.in_sync === false ? "npm run release:local" : "",
|
|
1581
|
+
});
|
|
1582
|
+
|
|
1583
|
+
const integrationSync = integrationSyncStatus(env);
|
|
1584
|
+
checks.push({
|
|
1585
|
+
name: "framework_integrations_in_sync",
|
|
1586
|
+
ok: true,
|
|
1587
|
+
in_sync: integrationSync.in_sync,
|
|
1588
|
+
integrations: integrationSync.integrations,
|
|
1589
|
+
error: "",
|
|
1590
|
+
fix: integrationSync.in_sync ? "" : "wallet update --yes",
|
|
1591
|
+
});
|
|
1592
|
+
|
|
1593
|
+
const recoveryStatus = updateRecoveryStatus(env);
|
|
1594
|
+
checks.push({
|
|
1595
|
+
name: "update_recovery_state",
|
|
1596
|
+
ok: true,
|
|
1597
|
+
...recoveryStatus,
|
|
1598
|
+
error: "",
|
|
1599
|
+
fix: recoveryStatus.needs_recovery ? "wallet install --yes" : "",
|
|
1600
|
+
});
|
|
1601
|
+
|
|
1602
|
+
const ok = checks.every((c) => c.ok);
|
|
1603
|
+
console.log(
|
|
1604
|
+
JSON.stringify(
|
|
1605
|
+
{
|
|
1606
|
+
schema_version: 1,
|
|
1607
|
+
ok,
|
|
1608
|
+
package_name: packageJson.name,
|
|
1609
|
+
package_version: packageVersion,
|
|
1610
|
+
openclaw_home: resolveOpenclawHome(),
|
|
1611
|
+
current_runtime: currentPath,
|
|
1612
|
+
active_version: activeVersion(),
|
|
1613
|
+
releases: listReleases(),
|
|
1614
|
+
deep,
|
|
1615
|
+
checks,
|
|
1616
|
+
},
|
|
1617
|
+
null,
|
|
1618
|
+
2,
|
|
1619
|
+
),
|
|
1620
|
+
);
|
|
1621
|
+
return ok ? 0 : 1;
|
|
1622
|
+
}
|
|
1623
|
+
|
|
1624
|
+
function runStatus(args = []) {
|
|
1625
|
+
const payload = {
|
|
1626
|
+
schema_version: 1,
|
|
1627
|
+
ok: true,
|
|
1628
|
+
package_name: packageJson.name,
|
|
1629
|
+
package_version: packageVersion,
|
|
1630
|
+
openclaw_home: resolveOpenclawHome(),
|
|
1631
|
+
runtime_base: resolveRuntimeBase(),
|
|
1632
|
+
current_runtime: currentRuntimePath(),
|
|
1633
|
+
previous_runtime: readLinkOrNull(previousRuntimePath()),
|
|
1634
|
+
active_version: activeVersion(),
|
|
1635
|
+
available_releases: listReleases(),
|
|
1636
|
+
failed_releases: listFailedReleases(),
|
|
1637
|
+
update_available: computeUpdateAvailability(),
|
|
1638
|
+
runtime_in_sync: computeRuntimeInSync(),
|
|
1639
|
+
framework_integrations: integrationSyncStatus(),
|
|
1640
|
+
update_recovery: updateRecoveryStatus(),
|
|
1641
|
+
};
|
|
1642
|
+
if (hasFlag(args, "--verbose")) {
|
|
1643
|
+
payload.verbose = true;
|
|
1644
|
+
payload.active_python_runtime = activePythonRuntimeInfo();
|
|
1645
|
+
payload.active_node_runtimes = activeNodeRuntimeInfo();
|
|
1646
|
+
payload.shared_snapshot_inventory = sharedSnapshotInventory();
|
|
1647
|
+
}
|
|
1648
|
+
console.log(JSON.stringify(payload, null, 2));
|
|
1649
|
+
return 0;
|
|
1650
|
+
}
|
|
1651
|
+
|
|
1652
|
+
function buildInstallerEnv(args) {
|
|
1653
|
+
const env = { ...process.env };
|
|
1654
|
+
const sealedKeysPath = path.join(resolveOpenclawHome(env), "sealed_keys.json");
|
|
1655
|
+
const sealedKeysExist = fs.existsSync(sealedKeysPath);
|
|
1656
|
+
const dryRun = hasFlag(args, "--dry-run");
|
|
1657
|
+
let bootKeySource = env.AGENT_WALLET_BOOT_KEY ? "environment" : "none";
|
|
1658
|
+
const runtimeResolution = readBootKeyFromRuntimeResolver(env);
|
|
1659
|
+
if (runtimeResolution.supported) {
|
|
1660
|
+
bootKeySource = runtimeResolution.key ? "runtime_verified" : "runtime_rejected";
|
|
1661
|
+
if (runtimeResolution.key) {
|
|
1662
|
+
env.AGENT_WALLET_BOOT_KEY = runtimeResolution.key;
|
|
1663
|
+
} else {
|
|
1664
|
+
delete env.AGENT_WALLET_BOOT_KEY;
|
|
1665
|
+
}
|
|
1666
|
+
} else if (!env.AGENT_WALLET_BOOT_KEY) {
|
|
1667
|
+
const fallback = resolveLegacyInstallerBootKey(env);
|
|
1668
|
+
bootKeySource = fallback.source;
|
|
1669
|
+
if (fallback.key) env.AGENT_WALLET_BOOT_KEY = fallback.key;
|
|
1670
|
+
}
|
|
1671
|
+
|
|
1672
|
+
const shouldGenerateSecrets =
|
|
1673
|
+
!dryRun &&
|
|
1674
|
+
!hasFlag(args, "--no-auto-secrets") &&
|
|
1675
|
+
(hasFlag(args, "--yes") || !env.AGENT_WALLET_BOOT_KEY);
|
|
1676
|
+
|
|
1677
|
+
const generated = {};
|
|
1678
|
+
if (sealedKeysExist && shouldGenerateSecrets && !env.AGENT_WALLET_BOOT_KEY) {
|
|
1679
|
+
throw new Error(
|
|
1680
|
+
`Found ${sealedKeysPath}, but no AGENT_WALLET_BOOT_KEY was provided and no current runtime .env contains one. Refusing to generate a new boot key for existing sealed secrets.`,
|
|
1681
|
+
);
|
|
1682
|
+
}
|
|
1683
|
+
if (!sealedKeysExist && shouldGenerateSecrets && !env.AGENT_WALLET_BOOT_KEY) {
|
|
1684
|
+
generated.AGENT_WALLET_BOOT_KEY = token();
|
|
1685
|
+
env.AGENT_WALLET_BOOT_KEY = generated.AGENT_WALLET_BOOT_KEY;
|
|
1686
|
+
bootKeySource = "generated";
|
|
1687
|
+
}
|
|
1688
|
+
if (!sealedKeysExist && shouldGenerateSecrets && !env.AGENT_WALLET_MASTER_KEY) {
|
|
1689
|
+
generated.AGENT_WALLET_MASTER_KEY = token();
|
|
1690
|
+
env.AGENT_WALLET_MASTER_KEY = generated.AGENT_WALLET_MASTER_KEY;
|
|
1691
|
+
}
|
|
1692
|
+
if (!sealedKeysExist && shouldGenerateSecrets && !env.AGENT_WALLET_APPROVAL_SECRET) {
|
|
1693
|
+
generated.AGENT_WALLET_APPROVAL_SECRET = token();
|
|
1694
|
+
env.AGENT_WALLET_APPROVAL_SECRET = generated.AGENT_WALLET_APPROVAL_SECRET;
|
|
1695
|
+
}
|
|
1696
|
+
return { env, generated, bootKeySource };
|
|
1697
|
+
}
|
|
1698
|
+
|
|
1699
|
+
function openclawHostCommandArgs(args, env = process.env) {
|
|
1700
|
+
const runtimeRoot = currentRuntimePath(env);
|
|
1701
|
+
const walletRoot = path.join(runtimeRoot, "agent-wallet");
|
|
1702
|
+
const pythonBin = resolveVenvPython(runtimeRoot) || resolveAgentWalletPython(walletRoot);
|
|
1703
|
+
const commandArgs = [
|
|
1704
|
+
path.join(walletRoot, "scripts", "install_openclaw_local_config.py"),
|
|
1705
|
+
"--config-path",
|
|
1706
|
+
path.resolve(
|
|
1707
|
+
expandHome(parseFlagValue(args, "--config-path") || path.join(resolveOpenclawHome(env), "openclaw.json")),
|
|
1708
|
+
),
|
|
1709
|
+
"--plugin-id",
|
|
1710
|
+
parseFlagValue(args, "--plugin-id") || "agent-wallet",
|
|
1711
|
+
"--user-id",
|
|
1712
|
+
parseFlagValue(args, "--user-id") || "",
|
|
1713
|
+
"--backend",
|
|
1714
|
+
parseFlagValue(args, "--backend") || "solana_local",
|
|
1715
|
+
"--network",
|
|
1716
|
+
parseFlagValue(args, "--network") || "mainnet",
|
|
1717
|
+
hasFlag(args, "--sign-only") ? "--sign-only" : "--no-sign-only",
|
|
1718
|
+
"--extension-path",
|
|
1719
|
+
path.join(runtimeRoot, ".openclaw", "extensions", "agent-wallet"),
|
|
1720
|
+
"--package-root",
|
|
1721
|
+
walletRoot,
|
|
1722
|
+
"--python-bin",
|
|
1723
|
+
pythonBin,
|
|
1724
|
+
];
|
|
1725
|
+
for (const [flag, targetFlag] of [
|
|
1726
|
+
["--rpc-url", "--rpc-url"],
|
|
1727
|
+
["--rpc-urls", "--rpc-urls"],
|
|
1728
|
+
["--wdk-evm-service-url", "--wdk-evm-service-url"],
|
|
1729
|
+
]) {
|
|
1730
|
+
const value = parseFlagValue(args, flag);
|
|
1731
|
+
if (value) commandArgs.push(targetFlag, value);
|
|
1732
|
+
}
|
|
1733
|
+
return { pythonBin, commandArgs, configPath: commandArgs[2] };
|
|
1734
|
+
}
|
|
1735
|
+
|
|
1736
|
+
function runOpenclawHostInstall(args, env = process.env) {
|
|
1737
|
+
const { pythonBin, commandArgs, configPath } = openclawHostCommandArgs(args, env);
|
|
1738
|
+
const previousConfig = fs.existsSync(configPath) ? fs.readFileSync(configPath) : null;
|
|
1739
|
+
if (previousConfig === null) {
|
|
1740
|
+
writeJsonFileAtomic(configPath, {
|
|
1741
|
+
plugins: { entries: {} },
|
|
1742
|
+
tools: { alsoAllow: [] },
|
|
1743
|
+
});
|
|
1744
|
+
}
|
|
1745
|
+
const result = spawnSync(pythonBin, commandArgs, {
|
|
1746
|
+
cwd: packageRoot,
|
|
1747
|
+
encoding: "utf8",
|
|
1748
|
+
env,
|
|
1749
|
+
});
|
|
1750
|
+
if (result.error || (result.status ?? 1) !== 0) {
|
|
1751
|
+
if (previousConfig === null) {
|
|
1752
|
+
fs.rmSync(configPath, { force: true });
|
|
1753
|
+
} else {
|
|
1754
|
+
fs.writeFileSync(configPath, previousConfig, { mode: 0o600 });
|
|
1755
|
+
}
|
|
1756
|
+
return {
|
|
1757
|
+
name: "openclaw",
|
|
1758
|
+
attempted: true,
|
|
1759
|
+
ok: false,
|
|
1760
|
+
error: result.error?.message || (result.stderr || result.stdout || "").trim(),
|
|
1761
|
+
rolled_back: true,
|
|
1762
|
+
restart_required: false,
|
|
1763
|
+
};
|
|
1764
|
+
}
|
|
1765
|
+
let details = {};
|
|
1766
|
+
try {
|
|
1767
|
+
details = extractTrailingJson(result.stdout);
|
|
1768
|
+
} catch {
|
|
1769
|
+
details = { config_path: configPath };
|
|
1770
|
+
}
|
|
1771
|
+
recordManagedIntegration(
|
|
1772
|
+
"openclaw",
|
|
1773
|
+
{
|
|
1774
|
+
config_path: configPath,
|
|
1775
|
+
extension_path: path.join(currentRuntimePath(env), ".openclaw", "extensions", "agent-wallet"),
|
|
1776
|
+
package_root: path.join(currentRuntimePath(env), "agent-wallet"),
|
|
1777
|
+
restart_required: true,
|
|
1778
|
+
},
|
|
1779
|
+
env,
|
|
1780
|
+
);
|
|
1781
|
+
return {
|
|
1782
|
+
name: "openclaw",
|
|
1783
|
+
attempted: true,
|
|
1784
|
+
ok: true,
|
|
1785
|
+
...details,
|
|
1786
|
+
restart_required: true,
|
|
1787
|
+
};
|
|
1788
|
+
}
|
|
1789
|
+
|
|
1790
|
+
function runEditorHostInstall(name, args, env = process.env) {
|
|
1791
|
+
const binaryName = name === "claude-code" ? "claude" : name;
|
|
1792
|
+
const hostArgs = [
|
|
1793
|
+
name,
|
|
1794
|
+
"install",
|
|
1795
|
+
...(hasFlag(args, "--force") ? ["--force"] : []),
|
|
1796
|
+
...(name !== "cursor" && (!commandPath(binaryName) || hasFlag(args, "--skip-enable")) ? ["--skip-enable"] : []),
|
|
1797
|
+
];
|
|
1798
|
+
const result = spawnSync(process.execPath, [cliPath, ...hostArgs], {
|
|
1799
|
+
cwd: packageRoot,
|
|
1800
|
+
encoding: "utf8",
|
|
1801
|
+
env,
|
|
1802
|
+
});
|
|
1803
|
+
let details = {};
|
|
1804
|
+
try {
|
|
1805
|
+
details = extractTrailingJson(result.stdout);
|
|
1806
|
+
} catch {
|
|
1807
|
+
// Preserve the host adapter's error below when it did not emit JSON.
|
|
1808
|
+
}
|
|
1809
|
+
return {
|
|
1810
|
+
name,
|
|
1811
|
+
attempted: true,
|
|
1812
|
+
ok: !result.error && (result.status ?? 1) === 0,
|
|
1813
|
+
...details,
|
|
1814
|
+
error:
|
|
1815
|
+
result.error?.message ||
|
|
1816
|
+
((result.status ?? 1) === 0 ? "" : (result.stderr || result.stdout || "").trim()),
|
|
1817
|
+
};
|
|
1818
|
+
}
|
|
1819
|
+
|
|
1820
|
+
function applyHostInstallPlan(plan, args, env = process.env) {
|
|
1821
|
+
const selected = new Set(plan.selected_hosts);
|
|
1822
|
+
const managed = new Set(plan.managed_hosts);
|
|
1823
|
+
const managedSelected = plan.selected_hosts.filter((name) => managed.has(name));
|
|
1824
|
+
const adoptLegacy =
|
|
1825
|
+
plan.runtime_installed_before &&
|
|
1826
|
+
["managed_only", "existing_runtime_managed_only"].includes(plan.selection_reason);
|
|
1827
|
+
const refreshed = repairInstalledEditorIntegrations(
|
|
1828
|
+
env,
|
|
1829
|
+
adoptLegacy ? null : managedSelected,
|
|
1830
|
+
);
|
|
1831
|
+
const installed = [];
|
|
1832
|
+
|
|
1833
|
+
for (const name of plan.selected_hosts) {
|
|
1834
|
+
if (managed.has(name)) continue;
|
|
1835
|
+
installed.push(
|
|
1836
|
+
name === "openclaw"
|
|
1837
|
+
? runOpenclawHostInstall(args, env)
|
|
1838
|
+
: runEditorHostInstall(name, args, env),
|
|
1839
|
+
);
|
|
1840
|
+
}
|
|
1841
|
+
|
|
1842
|
+
return {
|
|
1843
|
+
ok: [...refreshed, ...installed].every((entry) => entry?.ok !== false),
|
|
1844
|
+
selected_hosts: [...selected],
|
|
1845
|
+
refreshed,
|
|
1846
|
+
installed,
|
|
1847
|
+
};
|
|
1848
|
+
}
|
|
1849
|
+
|
|
1850
|
+
function runInstallUnlocked(args, { commandName = "install", installPlan = null } = {}) {
|
|
1851
|
+
if (!fs.existsSync(setupPath)) {
|
|
1852
|
+
console.error(`Missing bundled setup.sh at ${setupPath}`);
|
|
1853
|
+
return 1;
|
|
1854
|
+
}
|
|
1855
|
+
|
|
1856
|
+
const explicitRuntimeRoot = parseFlagValue(args, "--runtime-root");
|
|
1857
|
+
const releaseRoot = explicitRuntimeRoot
|
|
1858
|
+
? path.resolve(expandHome(explicitRuntimeRoot))
|
|
1859
|
+
: releaseRootFor(packageVersion);
|
|
1860
|
+
const currentPath = currentRuntimePath();
|
|
1861
|
+
const previousPath = previousRuntimePath();
|
|
1862
|
+
const hostPlan = installPlan || universalInstallPlan(args);
|
|
1863
|
+
const installerArgs = withoutCliOnlyArgs(args);
|
|
1864
|
+
const dryRun = hasFlag(args, "--dry-run");
|
|
1865
|
+
if (!dryRun) printMacOSKeychainNotice(process.env);
|
|
1866
|
+
const recovery = dryRun
|
|
1867
|
+
? { attempted: false, ok: true, reason: "dry run" }
|
|
1868
|
+
: recoverInterruptedUpdate(process.env);
|
|
1869
|
+
if (!recovery.ok) {
|
|
1870
|
+
console.error(`Could not recover interrupted update: ${recovery.reason || recovery.error}`);
|
|
1871
|
+
return 1;
|
|
1872
|
+
}
|
|
1873
|
+
const stagingRoot = !explicitRuntimeRoot && !dryRun ? stagingRootFor(packageVersion) : null;
|
|
1874
|
+
const installRoot = stagingRoot || releaseRoot;
|
|
1875
|
+
|
|
1876
|
+
if (!hasFlag(installerArgs, "--runtime-root")) {
|
|
1877
|
+
installerArgs.push("--runtime-root", installRoot);
|
|
1878
|
+
}
|
|
1879
|
+
if (!hasFlag(installerArgs, "--install-from-runtime")) {
|
|
1880
|
+
installerArgs.push("--install-from-runtime");
|
|
1881
|
+
}
|
|
1882
|
+
if (!hasFlag(installerArgs, "--configure-openclaw") && !hasFlag(installerArgs, "--no-configure-openclaw")) {
|
|
1883
|
+
installerArgs.push("--no-configure-openclaw");
|
|
1884
|
+
}
|
|
1885
|
+
|
|
1886
|
+
let installerEnv;
|
|
1887
|
+
try {
|
|
1888
|
+
installerEnv = buildInstallerEnv(args);
|
|
1889
|
+
} catch (error) {
|
|
1890
|
+
console.error(error.message);
|
|
1891
|
+
return 1;
|
|
1892
|
+
}
|
|
1893
|
+
const { env, generated, bootKeySource } = installerEnv;
|
|
1894
|
+
if (stagingRoot) {
|
|
1895
|
+
env.OPENCLAW_INSTALL_FINAL_ROOT = releaseRoot;
|
|
1896
|
+
writeUpdateJournal(
|
|
1897
|
+
"preparing",
|
|
1898
|
+
{
|
|
1899
|
+
transaction_id: crypto.randomUUID(),
|
|
1900
|
+
staging_root: stagingRoot,
|
|
1901
|
+
release_root: releaseRoot,
|
|
1902
|
+
current_target_before: readLinkOrNull(currentPath),
|
|
1903
|
+
previous_target_before: readLinkOrNull(previousPath),
|
|
1904
|
+
},
|
|
1905
|
+
env,
|
|
1906
|
+
);
|
|
1907
|
+
}
|
|
1908
|
+
const result = spawnSync("sh", [setupPath, ...installerArgs], {
|
|
1909
|
+
cwd: packageRoot,
|
|
1910
|
+
stdio: dryRun ? "pipe" : "inherit",
|
|
1911
|
+
encoding: dryRun ? "utf8" : undefined,
|
|
1912
|
+
env,
|
|
1913
|
+
});
|
|
1914
|
+
|
|
1915
|
+
if (result.error) {
|
|
1916
|
+
const failedRoot = failStagingRuntime(stagingRoot, result.error.message);
|
|
1917
|
+
if (!dryRun) {
|
|
1918
|
+
writeUpdateJournal("failed", { failed_runtime: failedRoot, error: result.error.message }, env);
|
|
1919
|
+
}
|
|
1920
|
+
console.error(result.error.message);
|
|
1921
|
+
return 1;
|
|
1922
|
+
}
|
|
1923
|
+
if ((result.status ?? 1) !== 0) {
|
|
1924
|
+
if (dryRun) {
|
|
1925
|
+
if (result.stderr) process.stderr.write(result.stderr);
|
|
1926
|
+
if (result.stdout) process.stderr.write(result.stdout);
|
|
1927
|
+
}
|
|
1928
|
+
const failedRoot = failStagingRuntime(stagingRoot, `installer exited with ${result.status ?? 1}`);
|
|
1929
|
+
if (!dryRun) {
|
|
1930
|
+
writeUpdateJournal(
|
|
1931
|
+
"failed",
|
|
1932
|
+
{ failed_runtime: failedRoot, error: `installer exited with ${result.status ?? 1}` },
|
|
1933
|
+
env,
|
|
1934
|
+
);
|
|
1935
|
+
}
|
|
1936
|
+
return result.status ?? 1;
|
|
1937
|
+
}
|
|
1938
|
+
|
|
1939
|
+
if (dryRun) {
|
|
1940
|
+
if (result.stderr) process.stderr.write(result.stderr);
|
|
1941
|
+
let installerPlan;
|
|
1942
|
+
try {
|
|
1943
|
+
installerPlan = extractTrailingJson(result.stdout);
|
|
1944
|
+
} catch (error) {
|
|
1945
|
+
if (result.stdout) process.stderr.write(result.stdout);
|
|
1946
|
+
console.error(error.message);
|
|
1947
|
+
return 1;
|
|
1948
|
+
}
|
|
1949
|
+
console.log(
|
|
1950
|
+
JSON.stringify(
|
|
1951
|
+
{
|
|
1952
|
+
...installerPlan,
|
|
1953
|
+
command: commandName,
|
|
1954
|
+
dry_run: true,
|
|
1955
|
+
host_plan: hostPlan,
|
|
1956
|
+
},
|
|
1957
|
+
null,
|
|
1958
|
+
2,
|
|
1959
|
+
),
|
|
1960
|
+
);
|
|
1961
|
+
return 0;
|
|
1962
|
+
}
|
|
1963
|
+
|
|
1964
|
+
// Installs that pass --skip-python-setup may have no venv, so this handshake
|
|
1965
|
+
// would fail and trigger a spurious rollback; such flows must set
|
|
1966
|
+
// AGENT_WALLET_VERIFY_DISABLE=1 (verifyRuntime then skips).
|
|
1967
|
+
const currentTarget = existingRuntimePointerTarget(currentPath);
|
|
1968
|
+
const verification = verifyRuntime(installRoot, env);
|
|
1969
|
+
if (!verification.ok && !verification.skipped) {
|
|
1970
|
+
const failedRoot = failStagingRuntime(stagingRoot, verification.error);
|
|
1971
|
+
const rolledBack = Boolean(currentTarget);
|
|
1972
|
+
const previousVersion = rolledBack
|
|
1973
|
+
? path.basename(currentTarget)
|
|
1974
|
+
: null;
|
|
1975
|
+
|
|
1976
|
+
let human;
|
|
1977
|
+
let fix;
|
|
1978
|
+
if (!rolledBack) {
|
|
1979
|
+
human =
|
|
1980
|
+
verification.category === "broken_release"
|
|
1981
|
+
? `Release ${packageVersion} is broken and there is no previous working version to fall back to. Nothing is active. This is a bad release — please report it; a patched version will follow.`
|
|
1982
|
+
: `Release ${packageVersion} failed to verify and there is no previous version. Your local environment looks incomplete: ${verification.error}.`;
|
|
1983
|
+
fix =
|
|
1984
|
+
verification.category === "local_env"
|
|
1985
|
+
? `Ensure python>=3.10 with venv is installed, then: ${npmCommand} install --yes`
|
|
1986
|
+
: `${npmCommand} install --version <known-good-version> --yes`;
|
|
1987
|
+
} else if (verification.category === "broken_release") {
|
|
1988
|
+
human = `Release ${packageVersion} is broken; kept you on the working version ${previousVersion}. This is on our side — you are safe. Re-run update when a patched release ships.`;
|
|
1989
|
+
fix = `${npmCommand} update --yes`;
|
|
1990
|
+
} else if (verification.category === "local_env") {
|
|
1991
|
+
human = `Release ${packageVersion} could not start on this machine; kept you on ${previousVersion}. This looks fixable locally: ${verification.error}.`;
|
|
1992
|
+
fix = `Fix python>=3.10/venv, then: ${npmCommand} install --yes`;
|
|
1993
|
+
} else {
|
|
1994
|
+
human = `Release ${packageVersion} failed verification; kept you on ${previousVersion}.`;
|
|
1995
|
+
fix = `${npmCommand} doctor --deep (then install --yes once resolved)`;
|
|
1996
|
+
}
|
|
1997
|
+
|
|
1998
|
+
console.error(
|
|
1999
|
+
JSON.stringify(
|
|
2000
|
+
{
|
|
2001
|
+
ok: false,
|
|
2002
|
+
command: commandName,
|
|
2003
|
+
version: packageVersion,
|
|
2004
|
+
category: verification.category || "unknown",
|
|
2005
|
+
error: `runtime verification failed: ${verification.error}`,
|
|
2006
|
+
rolled_back: rolledBack,
|
|
2007
|
+
switched_current: false,
|
|
2008
|
+
kept_version: previousVersion,
|
|
2009
|
+
failed_runtime: failedRoot,
|
|
2010
|
+
current_runtime_target: readLinkOrNull(currentPath),
|
|
2011
|
+
message: human,
|
|
2012
|
+
fix,
|
|
2013
|
+
},
|
|
2014
|
+
null,
|
|
2015
|
+
2,
|
|
2016
|
+
),
|
|
2017
|
+
);
|
|
2018
|
+
writeUpdateJournal(
|
|
2019
|
+
"failed",
|
|
2020
|
+
{ failed_runtime: failedRoot, error: verification.error, kept_version: previousVersion },
|
|
2021
|
+
env,
|
|
2022
|
+
);
|
|
2023
|
+
return 1;
|
|
2024
|
+
}
|
|
2025
|
+
|
|
2026
|
+
const bootKeyVerification = verifyBootKeyWithRuntime(installRoot, env);
|
|
2027
|
+
if (!bootKeyVerification.ok) {
|
|
2028
|
+
const failedRoot = failStagingRuntime(stagingRoot, bootKeyVerification.error);
|
|
2029
|
+
writeUpdateJournal(
|
|
2030
|
+
"failed",
|
|
2031
|
+
{ failed_runtime: failedRoot, error: bootKeyVerification.error },
|
|
2032
|
+
env,
|
|
2033
|
+
);
|
|
2034
|
+
console.error(
|
|
2035
|
+
JSON.stringify(
|
|
2036
|
+
{
|
|
2037
|
+
ok: false,
|
|
2038
|
+
command: commandName,
|
|
2039
|
+
version: packageVersion,
|
|
2040
|
+
category: "boot_key_rejected",
|
|
2041
|
+
error: bootKeyVerification.error,
|
|
2042
|
+
switched_current: false,
|
|
2043
|
+
failed_runtime: failedRoot,
|
|
2044
|
+
current_runtime_target: readLinkOrNull(currentPath),
|
|
2045
|
+
fix: "Remove stale AGENT_WALLET_BOOT_KEY overrides and retry the update.",
|
|
2046
|
+
},
|
|
2047
|
+
null,
|
|
2048
|
+
2,
|
|
2049
|
+
),
|
|
2050
|
+
);
|
|
2051
|
+
return 1;
|
|
2052
|
+
}
|
|
2053
|
+
|
|
2054
|
+
if (env.AGENT_WALLET_BOOT_KEY) {
|
|
2055
|
+
const envPath = path.join(installRoot, "agent-wallet", ".env");
|
|
2056
|
+
// Prefer the OS keystore: provision the boot key there and keep the plaintext
|
|
2057
|
+
// out of the release .env entirely. Only fall back to the legacy .env write when
|
|
2058
|
+
// no keystore round-trip is possible, so the runtime can still resolve the key.
|
|
2059
|
+
if (provisionBootKeyToKeystore(installRoot, env, env.AGENT_WALLET_BOOT_KEY)) {
|
|
2060
|
+
envFileUnset(envPath, ["AGENT_WALLET_BOOT_KEY"]);
|
|
2061
|
+
} else {
|
|
2062
|
+
envFileSet(envPath, {
|
|
2063
|
+
AGENT_WALLET_BOOT_KEY: env.AGENT_WALLET_BOOT_KEY,
|
|
2064
|
+
});
|
|
2065
|
+
}
|
|
2066
|
+
}
|
|
2067
|
+
|
|
2068
|
+
writeReleaseState(installRoot, "verified", {
|
|
2069
|
+
verification_skipped: Boolean(verification.skipped),
|
|
2070
|
+
});
|
|
2071
|
+
writeUpdateJournal(
|
|
2072
|
+
"verified",
|
|
2073
|
+
{ ...readUpdateJournal(env), staging_root: stagingRoot, release_root: releaseRoot },
|
|
2074
|
+
env,
|
|
2075
|
+
);
|
|
2076
|
+
|
|
2077
|
+
let replacedRoot = null;
|
|
2078
|
+
try {
|
|
2079
|
+
replacedRoot = fs.existsSync(releaseRoot)
|
|
2080
|
+
? uniquePathWithSuffix(
|
|
2081
|
+
path.join(path.dirname(releaseRoot), `${path.basename(releaseRoot)}-replaced`),
|
|
2082
|
+
)
|
|
2083
|
+
: null;
|
|
2084
|
+
writeUpdateJournal(
|
|
2085
|
+
"committing",
|
|
2086
|
+
{
|
|
2087
|
+
...readUpdateJournal(env),
|
|
2088
|
+
staging_root: stagingRoot,
|
|
2089
|
+
release_root: releaseRoot,
|
|
2090
|
+
replaced_root: replacedRoot,
|
|
2091
|
+
current_target_before: readLinkOrNull(currentPath),
|
|
2092
|
+
previous_target_before: readLinkOrNull(previousPath),
|
|
2093
|
+
},
|
|
2094
|
+
env,
|
|
2095
|
+
);
|
|
2096
|
+
replacedRoot = commitStagedRuntime(stagingRoot, releaseRoot, replacedRoot, env);
|
|
2097
|
+
} catch (error) {
|
|
2098
|
+
const failedRoot = failStagingRuntime(stagingRoot, error.message);
|
|
2099
|
+
writeUpdateJournal("failed", { failed_runtime: failedRoot, error: error.message }, env);
|
|
2100
|
+
console.error(`Could not commit staged runtime: ${error.message}`);
|
|
2101
|
+
return 1;
|
|
2102
|
+
}
|
|
2103
|
+
|
|
2104
|
+
const previousTarget =
|
|
2105
|
+
currentTarget && path.resolve(currentTarget) === path.resolve(releaseRoot) && replacedRoot
|
|
2106
|
+
? replacedRoot
|
|
2107
|
+
: currentTarget;
|
|
2108
|
+
if (previousTarget) {
|
|
2109
|
+
switchSymlink(previousPath, previousTarget);
|
|
2110
|
+
}
|
|
2111
|
+
switchSymlink(currentPath, releaseRoot);
|
|
2112
|
+
writeUpdateJournal(
|
|
2113
|
+
"committed",
|
|
2114
|
+
{ ...readUpdateJournal(env), release_root: releaseRoot, previous_runtime: previousTarget },
|
|
2115
|
+
env,
|
|
2116
|
+
);
|
|
2117
|
+
// Daemon restart is advisory lifecycle cleanup, not part of the atomic
|
|
2118
|
+
// runtime commit. Record the successful update before waiting on it.
|
|
2119
|
+
const evmDaemonStop = stopLocalEvmDaemonSync({ env });
|
|
2120
|
+
|
|
2121
|
+
const integrationRegistryRecovery = integrations(env).recoverCorruptRegistry();
|
|
2122
|
+
const hostInstallation = applyHostInstallPlan(hostPlan, args, env);
|
|
2123
|
+
const hostInstallFailed = hostInstallation.installed.some((entry) => entry?.ok === false);
|
|
2124
|
+
const globalCliRefresh = safelyRefreshIntegration(
|
|
2125
|
+
"global-cli",
|
|
2126
|
+
() => refreshGlobalCliIfNeeded(env),
|
|
2127
|
+
);
|
|
2128
|
+
|
|
2129
|
+
const pythonInfo = activePythonRuntimeInfo(env);
|
|
2130
|
+
const nodeInfo = activeNodeRuntimeInfo(env)
|
|
2131
|
+
.map((item) => `${item.project_name}:${item.shared ? "shared" : item.exists ? "local" : "missing"}`)
|
|
2132
|
+
.join(", ");
|
|
2133
|
+
console.error(
|
|
2134
|
+
`Update summary: version=${packageVersion} active=${activeVersion(env) || packageVersion} python=${pythonInfo?.shared ? "shared" : pythonInfo?.exists ? "local" : "missing"} node=[${nodeInfo}]`,
|
|
2135
|
+
);
|
|
2136
|
+
|
|
2137
|
+
console.error(
|
|
2138
|
+
JSON.stringify(
|
|
2139
|
+
{
|
|
2140
|
+
ok: !hostInstallFailed,
|
|
2141
|
+
command: commandName,
|
|
2142
|
+
version: packageVersion,
|
|
2143
|
+
runtime_root: releaseRoot,
|
|
2144
|
+
current_runtime: currentPath,
|
|
2145
|
+
previous_runtime: readLinkOrNull(previousPath),
|
|
2146
|
+
generated_runtime_secrets: Object.keys(generated),
|
|
2147
|
+
boot_key_source: bootKeySource,
|
|
2148
|
+
staged: Boolean(stagingRoot),
|
|
2149
|
+
release_state: "verified",
|
|
2150
|
+
recovery,
|
|
2151
|
+
host_plan: hostPlan,
|
|
2152
|
+
host_installation: hostInstallation,
|
|
2153
|
+
integration_registry_recovery: integrationRegistryRecovery,
|
|
2154
|
+
integration_refresh: hostInstallation.refreshed,
|
|
2155
|
+
global_cli_refresh: globalCliRefresh,
|
|
2156
|
+
evm_daemon_stop: evmDaemonStop,
|
|
2157
|
+
...(hostInstallFailed
|
|
2158
|
+
? {
|
|
2159
|
+
category: "host_install_failed",
|
|
2160
|
+
message: "The shared runtime is active, but one or more selected host plugins failed to install.",
|
|
2161
|
+
}
|
|
2162
|
+
: {}),
|
|
2163
|
+
},
|
|
2164
|
+
null,
|
|
2165
|
+
2,
|
|
2166
|
+
),
|
|
2167
|
+
);
|
|
2168
|
+
return hostInstallFailed ? 1 : 0;
|
|
2169
|
+
}
|
|
2170
|
+
|
|
2171
|
+
function runInstall(args, options = {}) {
|
|
2172
|
+
if (hasFlag(args, "--dry-run")) return runInstallUnlocked(args, options);
|
|
2173
|
+
const lock = acquireUpdateLock(process.env);
|
|
2174
|
+
if (!lock.ok) {
|
|
2175
|
+
console.error(
|
|
2176
|
+
JSON.stringify(
|
|
2177
|
+
{
|
|
2178
|
+
ok: false,
|
|
2179
|
+
category: "update_locked",
|
|
2180
|
+
error: "Another wallet install or update is already running.",
|
|
2181
|
+
lock_path: lock.path,
|
|
2182
|
+
lock_owner: lock.owner,
|
|
2183
|
+
fix: "Wait for the active update to finish, then retry.",
|
|
2184
|
+
},
|
|
2185
|
+
null,
|
|
2186
|
+
2,
|
|
2187
|
+
),
|
|
2188
|
+
);
|
|
2189
|
+
return 1;
|
|
2190
|
+
}
|
|
2191
|
+
try {
|
|
2192
|
+
holdUpdateLockForTest(process.env);
|
|
2193
|
+
return runInstallUnlocked(args, options);
|
|
2194
|
+
} finally {
|
|
2195
|
+
releaseUpdateLock(lock);
|
|
2196
|
+
}
|
|
2197
|
+
}
|
|
2198
|
+
|
|
2199
|
+
function resolveUpdatePackageSpec(env = process.env) {
|
|
2200
|
+
const explicit = String(env[UPDATE_PACKAGE_SPEC_ENV] || "").trim();
|
|
2201
|
+
if (explicit) return explicit;
|
|
2202
|
+
return `${packageJson.name}@latest`;
|
|
2203
|
+
}
|
|
2204
|
+
|
|
2205
|
+
function runDelegatedInstallForUpdate(args, { captureOutput = false } = {}) {
|
|
2206
|
+
const installArgs = hasFlag(args, "--managed-only") ? args : [...args, "--managed-only"];
|
|
2207
|
+
const localCliPath = String(process.env[UPDATE_CLI_PATH_ENV] || "").trim();
|
|
2208
|
+
if (localCliPath) {
|
|
2209
|
+
const meta = resolveCliPackageMeta(localCliPath);
|
|
2210
|
+
const result = spawnSync("node", [localCliPath, "install", ...installArgs], {
|
|
2211
|
+
cwd: packageRoot,
|
|
2212
|
+
stdio: captureOutput ? "pipe" : "inherit",
|
|
2213
|
+
encoding: captureOutput ? "utf8" : undefined,
|
|
2214
|
+
env: process.env,
|
|
2215
|
+
});
|
|
2216
|
+
return {
|
|
2217
|
+
result,
|
|
2218
|
+
delegated_via: "cli_path",
|
|
2219
|
+
target_package_spec: meta.name || packageJson.name,
|
|
2220
|
+
target_version_hint: meta.version || null,
|
|
2221
|
+
};
|
|
2222
|
+
}
|
|
2223
|
+
|
|
2224
|
+
const npmBin = commandPath("npm");
|
|
2225
|
+
if (!npmBin) {
|
|
2226
|
+
throw new Error(`npm is required for wallet updates. Install npm, then run ${npmCommand} install --yes.`);
|
|
2227
|
+
}
|
|
2228
|
+
|
|
2229
|
+
const packageSpec = resolveUpdatePackageSpec();
|
|
2230
|
+
const binCommand = primaryBinCommand();
|
|
2231
|
+
const result = spawnSync(
|
|
2232
|
+
npmBin,
|
|
2233
|
+
["exec", "--yes", `--package=${packageSpec}`, binCommand, "--", "install", ...installArgs],
|
|
2234
|
+
{
|
|
2235
|
+
cwd: packageRoot,
|
|
2236
|
+
stdio: captureOutput ? "pipe" : "inherit",
|
|
2237
|
+
encoding: captureOutput ? "utf8" : undefined,
|
|
2238
|
+
env: process.env,
|
|
2239
|
+
},
|
|
2240
|
+
);
|
|
2241
|
+
return {
|
|
2242
|
+
result,
|
|
2243
|
+
delegated_via: "npm_exec",
|
|
2244
|
+
target_package_spec: packageSpec,
|
|
2245
|
+
target_version_hint: null,
|
|
2246
|
+
};
|
|
2247
|
+
}
|
|
2248
|
+
|
|
2249
|
+
function runUpdate(args) {
|
|
2250
|
+
const dryRun = hasFlag(args, "--dry-run");
|
|
2251
|
+
if (dryRun) {
|
|
2252
|
+
try {
|
|
2253
|
+
const delegated = runDelegatedInstallForUpdate(args, { captureOutput: true });
|
|
2254
|
+
const { result } = delegated;
|
|
2255
|
+
if (result.error) {
|
|
2256
|
+
console.error(result.error.message);
|
|
2257
|
+
return 1;
|
|
2258
|
+
}
|
|
2259
|
+
if ((result.status ?? 1) !== 0) {
|
|
2260
|
+
const stderr = String(result.stderr || "").trim();
|
|
2261
|
+
const stdout = String(result.stdout || "").trim();
|
|
2262
|
+
if (stderr) process.stderr.write(`${stderr}\n`);
|
|
2263
|
+
if (stdout) process.stdout.write(`${stdout}\n`);
|
|
2264
|
+
return result.status ?? 1;
|
|
2265
|
+
}
|
|
2266
|
+
const payload = extractTrailingJson(result.stdout || "");
|
|
2267
|
+
const targetVersion =
|
|
2268
|
+
delegated.target_version_hint ||
|
|
2269
|
+
pathVersionFromRuntimeRoot(payload.runtime_root) ||
|
|
2270
|
+
null;
|
|
2271
|
+
console.log(
|
|
2272
|
+
JSON.stringify(
|
|
2273
|
+
{
|
|
2274
|
+
ok: true,
|
|
2275
|
+
command: "update",
|
|
2276
|
+
dry_run: true,
|
|
2277
|
+
current_version: activeVersion(),
|
|
2278
|
+
installed_cli_version: packageVersion,
|
|
2279
|
+
target_package_spec: delegated.target_package_spec,
|
|
2280
|
+
target_version: targetVersion,
|
|
2281
|
+
delegated_via: delegated.delegated_via,
|
|
2282
|
+
runtime_base: resolveRuntimeBase(),
|
|
2283
|
+
current_runtime: currentRuntimePath(),
|
|
2284
|
+
target_runtime_root: payload.runtime_root,
|
|
2285
|
+
dependency_plan: summarizeDependencyPlan(payload),
|
|
2286
|
+
install_plan: payload,
|
|
2287
|
+
},
|
|
2288
|
+
null,
|
|
2289
|
+
2,
|
|
2290
|
+
),
|
|
2291
|
+
);
|
|
2292
|
+
return 0;
|
|
2293
|
+
} catch (error) {
|
|
2294
|
+
console.error(error.message);
|
|
2295
|
+
return 1;
|
|
2296
|
+
}
|
|
2297
|
+
}
|
|
2298
|
+
|
|
2299
|
+
const currentVersionBefore = activeVersion();
|
|
2300
|
+
let delegated;
|
|
2301
|
+
try {
|
|
2302
|
+
delegated = runDelegatedInstallForUpdate(args, { captureOutput: true });
|
|
2303
|
+
} catch (error) {
|
|
2304
|
+
console.error(error.message);
|
|
2305
|
+
return 1;
|
|
2306
|
+
}
|
|
2307
|
+
if (delegated.result.error) {
|
|
2308
|
+
console.error(delegated.result.error.message);
|
|
2309
|
+
return 1;
|
|
2310
|
+
}
|
|
2311
|
+
const { result } = delegated;
|
|
2312
|
+
if ((result.status ?? 1) !== 0) {
|
|
2313
|
+
const stderr = String(result.stderr || "").trim();
|
|
2314
|
+
const stdout = String(result.stdout || "").trim();
|
|
2315
|
+
if (stderr) process.stderr.write(`${stderr}\n`);
|
|
2316
|
+
if (stdout) process.stderr.write(`${stdout}\n`);
|
|
2317
|
+
return result.status ?? 1;
|
|
2318
|
+
}
|
|
2319
|
+
|
|
2320
|
+
let installPayload;
|
|
2321
|
+
try {
|
|
2322
|
+
installPayload = extractTrailingJson(result.stderr || "") || extractTrailingJson(result.stdout || "");
|
|
2323
|
+
} catch {
|
|
2324
|
+
try {
|
|
2325
|
+
installPayload = extractTrailingJson(result.stdout || "");
|
|
2326
|
+
} catch (error) {
|
|
2327
|
+
const stderr = String(result.stderr || "").trim();
|
|
2328
|
+
const stdout = String(result.stdout || "").trim();
|
|
2329
|
+
if (stderr) process.stderr.write(`${stderr}\n`);
|
|
2330
|
+
if (stdout) process.stderr.write(`${stdout}\n`);
|
|
2331
|
+
console.error(error.message);
|
|
2332
|
+
return 1;
|
|
2333
|
+
}
|
|
2334
|
+
}
|
|
2335
|
+
|
|
2336
|
+
const stderr = String(result.stderr || "");
|
|
2337
|
+
const summaryLine = stderr
|
|
2338
|
+
.split(/\r?\n/)
|
|
2339
|
+
.map((line) => line.trimEnd())
|
|
2340
|
+
.find((line) => line.startsWith("Update summary:"));
|
|
2341
|
+
if (summaryLine) {
|
|
2342
|
+
console.error(summaryLine);
|
|
2343
|
+
}
|
|
2344
|
+
|
|
2345
|
+
console.log(
|
|
2346
|
+
JSON.stringify(
|
|
2347
|
+
{
|
|
2348
|
+
...installPayload,
|
|
2349
|
+
command: "update",
|
|
2350
|
+
delegated_via: delegated.delegated_via,
|
|
2351
|
+
target_package_spec: delegated.target_package_spec,
|
|
2352
|
+
target_version:
|
|
2353
|
+
delegated.target_version_hint ||
|
|
2354
|
+
pathVersionFromRuntimeRoot(installPayload?.runtime_root) ||
|
|
2355
|
+
installPayload?.version ||
|
|
2356
|
+
null,
|
|
2357
|
+
previous_version: currentVersionBefore,
|
|
2358
|
+
active_version: activeVersion(),
|
|
2359
|
+
},
|
|
2360
|
+
null,
|
|
2361
|
+
2,
|
|
2362
|
+
),
|
|
2363
|
+
);
|
|
2364
|
+
return 0;
|
|
2365
|
+
}
|
|
2366
|
+
|
|
2367
|
+
function runRollback(args) {
|
|
2368
|
+
const requested = parseFlagValue(args, "--to");
|
|
2369
|
+
const current = activeVersion();
|
|
2370
|
+
let target = "";
|
|
2371
|
+
if (requested) {
|
|
2372
|
+
target = releaseRootFor(requested);
|
|
2373
|
+
} else {
|
|
2374
|
+
const previous = readLinkOrNull(previousRuntimePath());
|
|
2375
|
+
if (previous) target = path.resolve(path.dirname(previousRuntimePath()), previous);
|
|
2376
|
+
}
|
|
2377
|
+
|
|
2378
|
+
if (!target) {
|
|
2379
|
+
console.error("No previous runtime is recorded. Pass --to <version> to choose a release.");
|
|
2380
|
+
return 1;
|
|
2381
|
+
}
|
|
2382
|
+
if (!fs.existsSync(target)) {
|
|
2383
|
+
console.error(`Rollback target does not exist: ${target}`);
|
|
2384
|
+
return 1;
|
|
2385
|
+
}
|
|
2386
|
+
|
|
2387
|
+
const currentPath = currentRuntimePath();
|
|
2388
|
+
if (current) {
|
|
2389
|
+
switchSymlink(previousRuntimePath(), releaseRootFor(current));
|
|
2390
|
+
}
|
|
2391
|
+
switchSymlink(currentPath, target);
|
|
2392
|
+
const evmDaemonStop = stopLocalEvmDaemonSync();
|
|
2393
|
+
console.log(
|
|
2394
|
+
JSON.stringify(
|
|
2395
|
+
{
|
|
2396
|
+
ok: true,
|
|
2397
|
+
active_version: activeVersion(),
|
|
2398
|
+
current_runtime: currentPath,
|
|
2399
|
+
evm_daemon_stop: evmDaemonStop,
|
|
2400
|
+
},
|
|
2401
|
+
null,
|
|
2402
|
+
2,
|
|
2403
|
+
),
|
|
2404
|
+
);
|
|
2405
|
+
return 0;
|
|
2406
|
+
}
|
|
2407
|
+
|
|
2408
|
+
function resolveHermesPluginSource() {
|
|
2409
|
+
const currentRoot = logicalCurrentRuntimeRoot();
|
|
2410
|
+
const candidates = [];
|
|
2411
|
+
if (currentRoot) {
|
|
2412
|
+
candidates.push(path.join(currentRoot, "hermes", "plugins", "agent_wallet"));
|
|
2413
|
+
}
|
|
2414
|
+
candidates.push(path.join(packageRoot, "hermes", "plugins", "agent_wallet"));
|
|
2415
|
+
for (const source of candidates) {
|
|
2416
|
+
if (fs.existsSync(path.join(source, "plugin.yaml"))) {
|
|
2417
|
+
return source;
|
|
2418
|
+
}
|
|
2419
|
+
}
|
|
2420
|
+
throw new Error(`Missing Hermes plugin bundle. Checked: ${candidates.join(", ")}`);
|
|
2421
|
+
}
|
|
2422
|
+
|
|
2423
|
+
function resolveCodexPluginSource() {
|
|
2424
|
+
// test/CI override: inject a staged bundle dir
|
|
2425
|
+
const override = String(process.env.AGENT_WALLET_CODEX_PLUGIN_SOURCE || "").trim();
|
|
2426
|
+
if (override) return path.resolve(expandHome(override));
|
|
2427
|
+
const currentRoot = logicalCurrentRuntimeRoot();
|
|
2428
|
+
const candidates = [];
|
|
2429
|
+
if (currentRoot) {
|
|
2430
|
+
candidates.push(path.join(currentRoot, "codex", "plugins", "agent-wallet"));
|
|
2431
|
+
}
|
|
2432
|
+
candidates.push(path.join(packageRoot, "codex", "plugins", "agent-wallet"));
|
|
2433
|
+
for (const source of candidates) {
|
|
2434
|
+
if (fs.existsSync(path.join(source, ".codex-plugin", "plugin.json"))) {
|
|
2435
|
+
return source;
|
|
2436
|
+
}
|
|
2437
|
+
}
|
|
2438
|
+
throw new Error(`Missing Codex plugin bundle. Checked: ${candidates.join(", ")}`);
|
|
2439
|
+
}
|
|
2440
|
+
|
|
2441
|
+
function resolveClaudeCodePluginSource() {
|
|
2442
|
+
// test/CI override: inject a staged bundle dir
|
|
2443
|
+
const override = String(process.env.AGENT_WALLET_CLAUDE_CODE_PLUGIN_SOURCE || "").trim();
|
|
2444
|
+
if (override) return path.resolve(expandHome(override));
|
|
2445
|
+
const currentRoot = logicalCurrentRuntimeRoot();
|
|
2446
|
+
const candidates = [];
|
|
2447
|
+
if (currentRoot) {
|
|
2448
|
+
candidates.push(path.join(currentRoot, "claude-code", "plugins", "agent-wallet"));
|
|
2449
|
+
}
|
|
2450
|
+
candidates.push(path.join(packageRoot, "claude-code", "plugins", "agent-wallet"));
|
|
2451
|
+
for (const source of candidates) {
|
|
2452
|
+
if (fs.existsSync(path.join(source, ".claude-plugin", "plugin.json"))) {
|
|
2453
|
+
return source;
|
|
2454
|
+
}
|
|
2455
|
+
}
|
|
2456
|
+
throw new Error(`Missing Claude Code plugin bundle. Checked: ${candidates.join(", ")}`);
|
|
2457
|
+
}
|
|
2458
|
+
|
|
2459
|
+
function resolveCursorPluginSource() {
|
|
2460
|
+
const override = String(process.env.AGENT_WALLET_CURSOR_PLUGIN_SOURCE || "").trim();
|
|
2461
|
+
if (override) return path.resolve(expandHome(override));
|
|
2462
|
+
const currentRoot = logicalCurrentRuntimeRoot();
|
|
2463
|
+
const candidates = [
|
|
2464
|
+
...(currentRoot ? [path.join(currentRoot, "cursor", "plugins", "agent-wallet")] : []),
|
|
2465
|
+
path.join(packageRoot, "cursor", "plugins", "agent-wallet"),
|
|
2466
|
+
];
|
|
2467
|
+
for (const source of candidates) {
|
|
2468
|
+
if (fs.existsSync(path.join(source, ".cursor-plugin", "plugin.json"))) return source;
|
|
2469
|
+
}
|
|
2470
|
+
throw new Error(`Missing Cursor plugin bundle. Checked: ${candidates.join(", ")}`);
|
|
2471
|
+
}
|
|
2472
|
+
|
|
2473
|
+
function resolveCodexPluginInstallRoot(env = process.env) {
|
|
2474
|
+
return path.resolve(expandHome(env.AGENT_WALLET_CODEX_PLUGIN_ROOT || "~/plugins"));
|
|
2475
|
+
}
|
|
2476
|
+
|
|
2477
|
+
function resolveCodexMarketplacePath(env = process.env) {
|
|
2478
|
+
return path.resolve(
|
|
2479
|
+
expandHome(env.AGENT_WALLET_CODEX_MARKETPLACE_PATH || "~/.agents/plugins/marketplace.json"),
|
|
2480
|
+
);
|
|
2481
|
+
}
|
|
2482
|
+
|
|
2483
|
+
function ensureCodexMarketplaceEntry({ marketplacePath, pluginName }) {
|
|
2484
|
+
const existing = readJsonFile(marketplacePath);
|
|
2485
|
+
const payload = existing && typeof existing === "object"
|
|
2486
|
+
? existing
|
|
2487
|
+
: {
|
|
2488
|
+
name: "local",
|
|
2489
|
+
interface: {
|
|
2490
|
+
displayName: "Local Plugins",
|
|
2491
|
+
},
|
|
2492
|
+
plugins: [],
|
|
2493
|
+
};
|
|
2494
|
+
|
|
2495
|
+
if (typeof payload.name !== "string" || !payload.name.trim()) {
|
|
2496
|
+
payload.name = "local";
|
|
2497
|
+
}
|
|
2498
|
+
if (!payload.interface || typeof payload.interface !== "object") {
|
|
2499
|
+
payload.interface = { displayName: "Local Plugins" };
|
|
2500
|
+
}
|
|
2501
|
+
if (typeof payload.interface.displayName !== "string" || !payload.interface.displayName.trim()) {
|
|
2502
|
+
payload.interface.displayName = "Local Plugins";
|
|
2503
|
+
}
|
|
2504
|
+
if (!Array.isArray(payload.plugins)) {
|
|
2505
|
+
payload.plugins = [];
|
|
2506
|
+
}
|
|
2507
|
+
|
|
2508
|
+
const entry = {
|
|
2509
|
+
name: pluginName,
|
|
2510
|
+
source: {
|
|
2511
|
+
source: "local",
|
|
2512
|
+
path: `./plugins/${pluginName}`,
|
|
2513
|
+
},
|
|
2514
|
+
policy: {
|
|
2515
|
+
installation: "AVAILABLE",
|
|
2516
|
+
authentication: "ON_INSTALL",
|
|
2517
|
+
},
|
|
2518
|
+
category: "Coding",
|
|
2519
|
+
};
|
|
2520
|
+
const index = payload.plugins.findIndex((item) => item && item.name === pluginName);
|
|
2521
|
+
if (index >= 0) {
|
|
2522
|
+
payload.plugins[index] = entry;
|
|
2523
|
+
} else {
|
|
2524
|
+
payload.plugins.push(entry);
|
|
2525
|
+
}
|
|
2526
|
+
writeJsonFile(marketplacePath, payload);
|
|
2527
|
+
return {
|
|
2528
|
+
marketplace_name: payload.name,
|
|
2529
|
+
marketplace_path: marketplacePath,
|
|
2530
|
+
entry,
|
|
2531
|
+
};
|
|
2532
|
+
}
|
|
2533
|
+
|
|
2534
|
+
function resolveAgentWalletPackageRoot(env = process.env) {
|
|
2535
|
+
const currentRoot = logicalCurrentRuntimeRoot(env);
|
|
2536
|
+
if (currentRoot) {
|
|
2537
|
+
const runtimePackage = path.join(currentRoot, "agent-wallet");
|
|
2538
|
+
if (fs.existsSync(path.join(runtimePackage, "agent_wallet", "__init__.py"))) {
|
|
2539
|
+
return runtimePackage;
|
|
2540
|
+
}
|
|
2541
|
+
}
|
|
2542
|
+
return path.join(packageRoot, "agent-wallet");
|
|
2543
|
+
}
|
|
2544
|
+
|
|
2545
|
+
function resolveAgentWalletPython(packageRootPath) {
|
|
2546
|
+
for (const candidate of [
|
|
2547
|
+
process.env.AGENT_WALLET_PYTHON,
|
|
2548
|
+
process.env.OPENCLAW_AGENT_WALLET_PYTHON,
|
|
2549
|
+
path.join(packageRootPath, ".venv", "bin", "python"),
|
|
2550
|
+
path.join(packageRootPath, ".runtime-venv", "bin", "python"),
|
|
2551
|
+
commandPath("python3"),
|
|
2552
|
+
]) {
|
|
2553
|
+
if (!candidate) continue;
|
|
2554
|
+
if (path.isAbsolute(candidate) && !fs.existsSync(candidate)) continue;
|
|
2555
|
+
return candidate;
|
|
2556
|
+
}
|
|
2557
|
+
return "python3";
|
|
2558
|
+
}
|
|
2559
|
+
|
|
2560
|
+
function runHermesInstall(args) {
|
|
2561
|
+
const hermesHome = resolveHermesHome();
|
|
2562
|
+
const userPluginsDir = path.join(hermesHome, "plugins");
|
|
2563
|
+
const pluginSource = resolveHermesPluginSource();
|
|
2564
|
+
const pluginTarget = path.join(userPluginsDir, "agent_wallet");
|
|
2565
|
+
const force = hasFlag(args, "--force");
|
|
2566
|
+
const skipEnable = hasFlag(args, "--skip-enable");
|
|
2567
|
+
const hermesBin = commandPath("hermes");
|
|
2568
|
+
const agentWalletPackageRoot = resolveAgentWalletPackageRoot();
|
|
2569
|
+
const agentWalletPython = resolveAgentWalletPython(agentWalletPackageRoot);
|
|
2570
|
+
const hermesEnvPath = path.join(hermesHome, ".env");
|
|
2571
|
+
const existingHermesEnv = readEnvFile(hermesEnvPath);
|
|
2572
|
+
const bootKeyFile = ensureBootKeyFile({ ...process.env, ...existingHermesEnv });
|
|
2573
|
+
|
|
2574
|
+
fs.mkdirSync(userPluginsDir, { recursive: true });
|
|
2575
|
+
try {
|
|
2576
|
+
const existing = fs.lstatSync(pluginTarget);
|
|
2577
|
+
if (!existing.isSymbolicLink()) {
|
|
2578
|
+
if (!force) {
|
|
2579
|
+
throw new Error(`${pluginTarget} exists and is not a symlink. Pass --force to replace it.`);
|
|
2580
|
+
}
|
|
2581
|
+
fs.rmSync(pluginTarget, { recursive: true, force: true });
|
|
2582
|
+
} else {
|
|
2583
|
+
fs.unlinkSync(pluginTarget);
|
|
2584
|
+
}
|
|
2585
|
+
} catch (error) {
|
|
2586
|
+
if (error?.code !== "ENOENT") throw error;
|
|
2587
|
+
}
|
|
2588
|
+
fs.symlinkSync(pluginSource, pluginTarget, "dir");
|
|
2589
|
+
|
|
2590
|
+
envFileSet(hermesEnvPath, {
|
|
2591
|
+
AGENT_WALLET_PACKAGE_ROOT: agentWalletPackageRoot,
|
|
2592
|
+
AGENT_WALLET_PYTHON: agentWalletPython,
|
|
2593
|
+
AGENT_WALLET_BOOT_KEY_FILE: bootKeyFile.path,
|
|
2594
|
+
});
|
|
2595
|
+
if (bootKeyFile.status !== "missing") {
|
|
2596
|
+
envFileUnset(hermesEnvPath, ["AGENT_WALLET_BOOT_KEY"]);
|
|
2597
|
+
}
|
|
2598
|
+
|
|
2599
|
+
let enable = { attempted: false, ok: false, skipped: skipEnable, error: "" };
|
|
2600
|
+
if (!skipEnable) {
|
|
2601
|
+
if (!hermesBin) {
|
|
2602
|
+
enable = {
|
|
2603
|
+
attempted: false,
|
|
2604
|
+
ok: false,
|
|
2605
|
+
skipped: false,
|
|
2606
|
+
error: "Hermes CLI was not found on PATH. Run `hermes plugins enable agent-wallet` after installing Hermes.",
|
|
2607
|
+
};
|
|
2608
|
+
} else {
|
|
2609
|
+
const result = spawnSync(hermesBin, ["plugins", "enable", "agent-wallet"], {
|
|
2610
|
+
cwd: packageRoot,
|
|
2611
|
+
encoding: "utf8",
|
|
2612
|
+
env: { ...process.env, HERMES_HOME: hermesHome },
|
|
2613
|
+
});
|
|
2614
|
+
enable = {
|
|
2615
|
+
attempted: true,
|
|
2616
|
+
ok: result.status === 0,
|
|
2617
|
+
skipped: false,
|
|
2618
|
+
error: result.status === 0 ? "" : (result.stderr || result.stdout || "").trim(),
|
|
2619
|
+
};
|
|
2620
|
+
}
|
|
2621
|
+
}
|
|
2622
|
+
|
|
2623
|
+
recordManagedIntegration("hermes", {
|
|
2624
|
+
hermes_home: hermesHome,
|
|
2625
|
+
plugin_target: pluginTarget,
|
|
2626
|
+
env_path: hermesEnvPath,
|
|
2627
|
+
restart_required: true,
|
|
2628
|
+
...(enable.skipped ? {} : { registration_ok: enable.ok }),
|
|
2629
|
+
});
|
|
2630
|
+
|
|
2631
|
+
console.log(
|
|
2632
|
+
JSON.stringify(
|
|
2633
|
+
{
|
|
2634
|
+
ok: enable.skipped || enable.ok,
|
|
2635
|
+
hermes_home: hermesHome,
|
|
2636
|
+
plugin_source: pluginSource,
|
|
2637
|
+
plugin_target: pluginTarget,
|
|
2638
|
+
env_path: hermesEnvPath,
|
|
2639
|
+
agent_wallet_package_root: agentWalletPackageRoot,
|
|
2640
|
+
agent_wallet_python: agentWalletPython,
|
|
2641
|
+
boot_key_file: bootKeyFile.path,
|
|
2642
|
+
boot_key_file_status: bootKeyFile.status,
|
|
2643
|
+
hermes_enable: enable,
|
|
2644
|
+
restart_required: true,
|
|
2645
|
+
},
|
|
2646
|
+
null,
|
|
2647
|
+
2,
|
|
2648
|
+
),
|
|
2649
|
+
);
|
|
2650
|
+
return enable.skipped || enable.ok ? 0 : 1;
|
|
2651
|
+
}
|
|
2652
|
+
|
|
2653
|
+
function runCodexInstall(args) {
|
|
2654
|
+
const codexHome = resolveCodexHome();
|
|
2655
|
+
const pluginSource = resolveCodexPluginSource();
|
|
2656
|
+
const pinnedEnv = { pinned: false, reason: "plugin source is immutable" };
|
|
2657
|
+
const pluginRoot = resolveCodexPluginInstallRoot();
|
|
2658
|
+
const pluginTarget = path.join(pluginRoot, "agent-wallet");
|
|
2659
|
+
const marketplacePath = resolveCodexMarketplacePath();
|
|
2660
|
+
const force = hasFlag(args, "--force");
|
|
2661
|
+
const skipEnable = hasFlag(args, "--skip-enable");
|
|
2662
|
+
const codexBin = commandPath("codex");
|
|
2663
|
+
|
|
2664
|
+
fs.mkdirSync(pluginRoot, { recursive: true });
|
|
2665
|
+
try {
|
|
2666
|
+
const existing = fs.lstatSync(pluginTarget);
|
|
2667
|
+
if (!existing.isSymbolicLink()) {
|
|
2668
|
+
if (!force) {
|
|
2669
|
+
throw new Error(`${pluginTarget} exists and is not a symlink. Pass --force to replace it.`);
|
|
2670
|
+
}
|
|
2671
|
+
fs.rmSync(pluginTarget, { recursive: true, force: true });
|
|
2672
|
+
} else {
|
|
2673
|
+
fs.unlinkSync(pluginTarget);
|
|
2674
|
+
}
|
|
2675
|
+
} catch (error) {
|
|
2676
|
+
if (error?.code !== "ENOENT") throw error;
|
|
2677
|
+
}
|
|
2678
|
+
fs.symlinkSync(pluginSource, pluginTarget, "dir");
|
|
2679
|
+
|
|
2680
|
+
const marketplace = ensureCodexMarketplaceEntry({
|
|
2681
|
+
marketplacePath,
|
|
2682
|
+
pluginName: "agent-wallet",
|
|
2683
|
+
});
|
|
2684
|
+
|
|
2685
|
+
let add = { attempted: false, ok: false, skipped: skipEnable, error: "" };
|
|
2686
|
+
if (!skipEnable) {
|
|
2687
|
+
if (!codexBin) {
|
|
2688
|
+
add = {
|
|
2689
|
+
attempted: false,
|
|
2690
|
+
ok: false,
|
|
2691
|
+
skipped: false,
|
|
2692
|
+
error: "Codex CLI was not found on PATH. Run `codex plugin add agent-wallet@local` after installing Codex.",
|
|
2693
|
+
};
|
|
2694
|
+
} else {
|
|
2695
|
+
const result = spawnSync(
|
|
2696
|
+
codexBin,
|
|
2697
|
+
["plugin", "add", `agent-wallet@${marketplace.marketplace_name}`],
|
|
2698
|
+
{
|
|
2699
|
+
cwd: packageRoot,
|
|
2700
|
+
encoding: "utf8",
|
|
2701
|
+
env: {
|
|
2702
|
+
...process.env,
|
|
2703
|
+
CODEX_HOME: codexHome,
|
|
2704
|
+
},
|
|
2705
|
+
},
|
|
2706
|
+
);
|
|
2707
|
+
add = {
|
|
2708
|
+
attempted: true,
|
|
2709
|
+
ok: result.status === 0,
|
|
2710
|
+
skipped: false,
|
|
2711
|
+
error: result.status === 0 ? "" : (result.stderr || result.stdout || "").trim(),
|
|
2712
|
+
};
|
|
2713
|
+
}
|
|
2714
|
+
}
|
|
2715
|
+
|
|
2716
|
+
recordManagedIntegration("codex", {
|
|
2717
|
+
codex_home: codexHome,
|
|
2718
|
+
plugin_target: pluginTarget,
|
|
2719
|
+
marketplace_path: marketplace.marketplace_path,
|
|
2720
|
+
marketplace_name: marketplace.marketplace_name,
|
|
2721
|
+
restart_required: true,
|
|
2722
|
+
...(add.skipped ? {} : { registration_ok: add.ok }),
|
|
2723
|
+
});
|
|
2724
|
+
|
|
2725
|
+
console.log(
|
|
2726
|
+
JSON.stringify(
|
|
2727
|
+
{
|
|
2728
|
+
ok: add.skipped || add.ok,
|
|
2729
|
+
codex_home: codexHome,
|
|
2730
|
+
plugin_source: pluginSource,
|
|
2731
|
+
plugin_target: pluginTarget,
|
|
2732
|
+
marketplace_path: marketplace.marketplace_path,
|
|
2733
|
+
marketplace_name: marketplace.marketplace_name,
|
|
2734
|
+
codex_add: add,
|
|
2735
|
+
restart_required: true,
|
|
2736
|
+
pinned_env: pinnedEnv,
|
|
2737
|
+
},
|
|
2738
|
+
null,
|
|
2739
|
+
2,
|
|
2740
|
+
),
|
|
2741
|
+
);
|
|
2742
|
+
return add.skipped || add.ok ? 0 : 1;
|
|
2743
|
+
}
|
|
2744
|
+
|
|
2745
|
+
function runCursorInstall(args, env = process.env) {
|
|
2746
|
+
const pluginSource = resolveCursorPluginSource();
|
|
2747
|
+
const pluginTarget = path.join(resolveCursorPluginInstallRoot(env), "agent-wallet");
|
|
2748
|
+
const synced = syncCursorPluginBundle({
|
|
2749
|
+
source: pluginSource,
|
|
2750
|
+
target: pluginTarget,
|
|
2751
|
+
packageVersion,
|
|
2752
|
+
force: hasFlag(args, "--force"),
|
|
2753
|
+
runtimePackageRoot: path.join(currentRuntimePath(env), "agent-wallet"),
|
|
2754
|
+
openclawHome: resolveOpenclawHome(env),
|
|
2755
|
+
defaultOpenclawHome: path.resolve(expandHome("~/.openclaw")),
|
|
2756
|
+
});
|
|
2757
|
+
recordManagedIntegration("cursor", { plugin_target: pluginTarget, restart_required: true }, env);
|
|
2758
|
+
console.log(JSON.stringify({
|
|
2759
|
+
ok: true,
|
|
2760
|
+
plugin_source: pluginSource,
|
|
2761
|
+
plugin_target: pluginTarget,
|
|
2762
|
+
...synced,
|
|
2763
|
+
note: "Restart Cursor or run Developer: Reload Window to activate the plugin.",
|
|
2764
|
+
}, null, 2));
|
|
2765
|
+
return 0;
|
|
2766
|
+
}
|
|
2767
|
+
|
|
2768
|
+
const CLAUDE_CODE_MARKETPLACE_NAME = "agentlayer-local";
|
|
2769
|
+
|
|
2770
|
+
function resolveClaudeCodeMarketplaceDir(env = process.env) {
|
|
2771
|
+
return path.resolve(
|
|
2772
|
+
expandHome(env.AGENT_WALLET_CLAUDE_CODE_MARKETPLACE_DIR || "~/.claude/agentlayer-local"),
|
|
2773
|
+
);
|
|
2774
|
+
}
|
|
2775
|
+
|
|
2776
|
+
// Pin OPENCLAW_HOME into one .mcp.json so run_mcp.sh uses the install-time home
|
|
2777
|
+
// instead of re-deriving the ~/.openclaw default. We deliberately do NOT pin
|
|
2778
|
+
// AGENT_WALLET_PYTHON: the launcher resolves the venv from OPENCLAW_HOME->current
|
|
2779
|
+
// dynamically, so a pinned python would go stale (and wrongly win) after upgrade.
|
|
2780
|
+
function pinHomeIntoMcpFile(mcpPath, env = process.env) {
|
|
2781
|
+
if (!fs.existsSync(mcpPath)) return { pinned: false, reason: "no .mcp.json", path: mcpPath };
|
|
2782
|
+
let doc;
|
|
2783
|
+
try {
|
|
2784
|
+
doc = JSON.parse(fs.readFileSync(mcpPath, "utf8"));
|
|
2785
|
+
} catch (error) {
|
|
2786
|
+
return { pinned: false, reason: `unreadable .mcp.json: ${error.message}`, path: mcpPath };
|
|
2787
|
+
}
|
|
2788
|
+
const entry = (doc.mcpServers || {})["agent-wallet"];
|
|
2789
|
+
if (!entry) return { pinned: false, reason: "no agent-wallet server entry", path: mcpPath };
|
|
2790
|
+
const home = resolveOpenclawHome(env);
|
|
2791
|
+
// When the install home is the default ~/.openclaw, run_mcp.sh already derives
|
|
2792
|
+
// it (`${OPENCLAW_HOME:-"$HOME/.openclaw"}`), so pinning is redundant and would
|
|
2793
|
+
// dirty version-controlled bundle files. Skip it — and drop any stale pin so the
|
|
2794
|
+
// file self-heals back to a clean, distributable state.
|
|
2795
|
+
const defaultHome = path.resolve(expandHome("~/.openclaw"));
|
|
2796
|
+
if (home === defaultHome) {
|
|
2797
|
+
if (entry.env && "OPENCLAW_HOME" in entry.env) {
|
|
2798
|
+
delete entry.env.OPENCLAW_HOME;
|
|
2799
|
+
if (Object.keys(entry.env).length === 0) delete entry.env;
|
|
2800
|
+
writeJsonFile(mcpPath, doc);
|
|
2801
|
+
return { pinned: false, reason: "home is default; removed redundant pin", path: mcpPath };
|
|
2802
|
+
}
|
|
2803
|
+
return { pinned: false, reason: "home is default; run_mcp.sh derives it", path: mcpPath };
|
|
2804
|
+
}
|
|
2805
|
+
entry.env = { ...(entry.env || {}), OPENCLAW_HOME: home };
|
|
2806
|
+
writeJsonFile(mcpPath, doc);
|
|
2807
|
+
return { pinned: true, openclaw_home: home, path: mcpPath };
|
|
2808
|
+
}
|
|
2809
|
+
|
|
2810
|
+
// Claude Code copies the plugin into a version-keyed cache and reads THAT copy,
|
|
2811
|
+
// so the bundle pin alone is ineffective once a cache exists. Pin every cached
|
|
2812
|
+
// copy too. Cache root is overridable for tests.
|
|
2813
|
+
function pinClaudeCacheCopies(env = process.env) {
|
|
2814
|
+
const cacheRoot = path.resolve(
|
|
2815
|
+
expandHome(env.AGENT_WALLET_CLAUDE_CODE_CACHE_ROOT || "~/.claude/plugins/cache"),
|
|
2816
|
+
);
|
|
2817
|
+
const pluginCacheDir = path.join(cacheRoot, CLAUDE_CODE_MARKETPLACE_NAME, "agent-wallet");
|
|
2818
|
+
const results = [];
|
|
2819
|
+
if (!fs.existsSync(pluginCacheDir)) return results;
|
|
2820
|
+
let versions;
|
|
2821
|
+
try {
|
|
2822
|
+
versions = fs.readdirSync(pluginCacheDir);
|
|
2823
|
+
} catch {
|
|
2824
|
+
return results;
|
|
2825
|
+
}
|
|
2826
|
+
for (const version of versions) {
|
|
2827
|
+
const mcpPath = path.join(pluginCacheDir, version, ".mcp.json");
|
|
2828
|
+
if (fs.existsSync(mcpPath)) results.push(pinHomeIntoMcpFile(mcpPath, env));
|
|
2829
|
+
}
|
|
2830
|
+
return results;
|
|
2831
|
+
}
|
|
2832
|
+
|
|
2833
|
+
function ensureClaudeCodeMarketplace(marketplaceDir, pluginSource, force) {
|
|
2834
|
+
const pluginsDir = path.join(marketplaceDir, "plugins");
|
|
2835
|
+
const pluginLink = path.join(pluginsDir, "agent-wallet");
|
|
2836
|
+
const manifestDir = path.join(marketplaceDir, ".claude-plugin");
|
|
2837
|
+
const manifestPath = path.join(manifestDir, "marketplace.json");
|
|
2838
|
+
|
|
2839
|
+
fs.mkdirSync(pluginsDir, { recursive: true });
|
|
2840
|
+
fs.mkdirSync(manifestDir, { recursive: true });
|
|
2841
|
+
|
|
2842
|
+
// Symlink plugin source into marketplace plugins dir.
|
|
2843
|
+
try {
|
|
2844
|
+
const existing = fs.lstatSync(pluginLink);
|
|
2845
|
+
if (!existing.isSymbolicLink()) {
|
|
2846
|
+
if (!force) {
|
|
2847
|
+
throw new Error(
|
|
2848
|
+
`${pluginLink} exists and is not a symlink. Pass --force to replace it.`,
|
|
2849
|
+
);
|
|
2850
|
+
}
|
|
2851
|
+
fs.rmSync(pluginLink, { recursive: true, force: true });
|
|
2852
|
+
} else {
|
|
2853
|
+
fs.unlinkSync(pluginLink);
|
|
2854
|
+
}
|
|
2855
|
+
} catch (error) {
|
|
2856
|
+
if (error?.code !== "ENOENT") throw error;
|
|
2857
|
+
}
|
|
2858
|
+
fs.symlinkSync(pluginSource, pluginLink, "dir");
|
|
2859
|
+
|
|
2860
|
+
// Write marketplace manifest (Claude Code requires owner + plugins[].source as relative path).
|
|
2861
|
+
const manifest = {
|
|
2862
|
+
name: CLAUDE_CODE_MARKETPLACE_NAME,
|
|
2863
|
+
description: "Local OneCraft plugins",
|
|
2864
|
+
owner: { name: "OneCraft" },
|
|
2865
|
+
plugins: [
|
|
2866
|
+
{
|
|
2867
|
+
name: "agent-wallet",
|
|
2868
|
+
displayName: "OneCraft Wallet",
|
|
2869
|
+
description:
|
|
2870
|
+
"Bridge to the existing local OneCraft wallet runtime (Solana, Bitcoin, EVM).",
|
|
2871
|
+
category: "development",
|
|
2872
|
+
source: "./plugins/agent-wallet",
|
|
2873
|
+
},
|
|
2874
|
+
],
|
|
2875
|
+
};
|
|
2876
|
+
writeJsonFile(manifestPath, manifest);
|
|
2877
|
+
return { pluginLink, manifestPath };
|
|
2878
|
+
}
|
|
2879
|
+
|
|
2880
|
+
function runClaudeCodeInstall(args) {
|
|
2881
|
+
const pluginSource = resolveClaudeCodePluginSource();
|
|
2882
|
+
const pinnedEnv = { pinned: false, reason: "plugin source is immutable" };
|
|
2883
|
+
const force = hasFlag(args, "--force");
|
|
2884
|
+
const skipEnable = hasFlag(args, "--skip-enable");
|
|
2885
|
+
const claudeBin = commandPath("claude");
|
|
2886
|
+
const marketplaceDir = resolveClaudeCodeMarketplaceDir();
|
|
2887
|
+
|
|
2888
|
+
const { pluginLink } = ensureClaudeCodeMarketplace(marketplaceDir, pluginSource, force);
|
|
2889
|
+
|
|
2890
|
+
const pluginDirFlag = `claude --plugin-dir ${pluginLink}`;
|
|
2891
|
+
|
|
2892
|
+
// Without the Claude CLI we can only set up the files; the user must register manually.
|
|
2893
|
+
let marketplaceAdd = { attempted: false, ok: false, skipped: skipEnable, error: "" };
|
|
2894
|
+
let enable = { attempted: false, ok: false, skipped: skipEnable, error: "" };
|
|
2895
|
+
|
|
2896
|
+
if (!skipEnable) {
|
|
2897
|
+
if (!claudeBin) {
|
|
2898
|
+
const msg =
|
|
2899
|
+
"Claude Code CLI was not found on PATH. Load the plugin manually with: " + pluginDirFlag;
|
|
2900
|
+
marketplaceAdd = { attempted: false, ok: false, skipped: false, error: msg };
|
|
2901
|
+
enable = { attempted: false, ok: false, skipped: false, error: msg };
|
|
2902
|
+
} else {
|
|
2903
|
+
// Register the local marketplace (idempotent — safe to re-run).
|
|
2904
|
+
const addResult = spawnSync(
|
|
2905
|
+
claudeBin,
|
|
2906
|
+
["plugin", "marketplace", "add", marketplaceDir, "--scope", "user"],
|
|
2907
|
+
{ encoding: "utf8", stdio: "pipe" },
|
|
2908
|
+
);
|
|
2909
|
+
marketplaceAdd = {
|
|
2910
|
+
attempted: true,
|
|
2911
|
+
ok: addResult.status === 0,
|
|
2912
|
+
skipped: false,
|
|
2913
|
+
error: addResult.status === 0 ? "" : (addResult.stderr || addResult.stdout || "").trim(),
|
|
2914
|
+
};
|
|
2915
|
+
|
|
2916
|
+
if (marketplaceAdd.ok) {
|
|
2917
|
+
// Install plugin from the now-registered local marketplace.
|
|
2918
|
+
const installResult = spawnSync(
|
|
2919
|
+
claudeBin,
|
|
2920
|
+
["plugin", "install", `agent-wallet@${CLAUDE_CODE_MARKETPLACE_NAME}`, "--scope", "user"],
|
|
2921
|
+
{ encoding: "utf8", stdio: "pipe" },
|
|
2922
|
+
);
|
|
2923
|
+
enable = {
|
|
2924
|
+
attempted: true,
|
|
2925
|
+
ok: installResult.status === 0,
|
|
2926
|
+
skipped: false,
|
|
2927
|
+
error:
|
|
2928
|
+
installResult.status === 0
|
|
2929
|
+
? ""
|
|
2930
|
+
: (installResult.stderr || installResult.stdout || "").trim(),
|
|
2931
|
+
};
|
|
2932
|
+
} else {
|
|
2933
|
+
enable = {
|
|
2934
|
+
attempted: false,
|
|
2935
|
+
ok: false,
|
|
2936
|
+
skipped: false,
|
|
2937
|
+
error: "Skipped plugin install because marketplace registration failed.",
|
|
2938
|
+
};
|
|
2939
|
+
}
|
|
2940
|
+
}
|
|
2941
|
+
}
|
|
2942
|
+
|
|
2943
|
+
recordManagedIntegration("claude-code", {
|
|
2944
|
+
marketplace_dir: marketplaceDir,
|
|
2945
|
+
plugin_target: pluginLink,
|
|
2946
|
+
cache_root: path.resolve(
|
|
2947
|
+
expandHome(process.env.AGENT_WALLET_CLAUDE_CODE_CACHE_ROOT || "~/.claude/plugins/cache"),
|
|
2948
|
+
),
|
|
2949
|
+
restart_required: true,
|
|
2950
|
+
...(enable.skipped ? {} : { registration_ok: enable.ok }),
|
|
2951
|
+
});
|
|
2952
|
+
|
|
2953
|
+
const ok = enable.skipped || enable.ok;
|
|
2954
|
+
const pinnedCache = pinClaudeCacheCopies();
|
|
2955
|
+
const pluginDirFlagFull = `claude --plugin-dir ${pluginSource}`;
|
|
2956
|
+
console.log(
|
|
2957
|
+
JSON.stringify(
|
|
2958
|
+
{
|
|
2959
|
+
ok,
|
|
2960
|
+
plugin_source: pluginSource,
|
|
2961
|
+
marketplace_dir: marketplaceDir,
|
|
2962
|
+
marketplace_add: marketplaceAdd,
|
|
2963
|
+
claude_code_install: enable,
|
|
2964
|
+
manual_load: pluginDirFlagFull,
|
|
2965
|
+
restart_required: true,
|
|
2966
|
+
pinned_env: pinnedEnv,
|
|
2967
|
+
pinned_cache: pinnedCache,
|
|
2968
|
+
note: ok
|
|
2969
|
+
? "Plugin registered. Restart Claude Code to activate."
|
|
2970
|
+
: `If automatic registration failed, load the plugin with: ${pluginDirFlagFull}`,
|
|
2971
|
+
},
|
|
2972
|
+
null,
|
|
2973
|
+
2,
|
|
2974
|
+
),
|
|
2975
|
+
);
|
|
2976
|
+
return enable.skipped || enable.ok ? 0 : 1;
|
|
2977
|
+
}
|
|
2978
|
+
|
|
2979
|
+
function runtimeReleasePath(value, env = process.env) {
|
|
2980
|
+
if (!value) return false;
|
|
2981
|
+
try {
|
|
2982
|
+
const releasesRoot = path.join(resolveRuntimeBase(env), "releases");
|
|
2983
|
+
const relative = path.relative(releasesRoot, path.resolve(expandHome(String(value))));
|
|
2984
|
+
return Boolean(relative) && relative !== ".." && !relative.startsWith(`..${path.sep}`);
|
|
2985
|
+
} catch {
|
|
2986
|
+
return false;
|
|
2987
|
+
}
|
|
2988
|
+
}
|
|
2989
|
+
|
|
2990
|
+
function pathEntryExists(pathname) {
|
|
2991
|
+
try {
|
|
2992
|
+
fs.lstatSync(pathname);
|
|
2993
|
+
return true;
|
|
2994
|
+
} catch (error) {
|
|
2995
|
+
if (error?.code === "ENOENT") return false;
|
|
2996
|
+
throw error;
|
|
2997
|
+
}
|
|
2998
|
+
}
|
|
2999
|
+
|
|
3000
|
+
function repairRuntimeSymlink(name, linkPath, desiredTarget, env = process.env, { allowExternal = false } = {}) {
|
|
3001
|
+
let rawTarget;
|
|
3002
|
+
try {
|
|
3003
|
+
const stat = fs.lstatSync(linkPath);
|
|
3004
|
+
if (!stat.isSymbolicLink()) {
|
|
3005
|
+
return { name, attempted: false, ok: true, repaired: false, reason: "not a symlink" };
|
|
3006
|
+
}
|
|
3007
|
+
rawTarget = fs.readlinkSync(linkPath);
|
|
3008
|
+
} catch (error) {
|
|
3009
|
+
if (error?.code === "ENOENT") {
|
|
3010
|
+
return { name, attempted: false, ok: true, repaired: false, reason: "not installed" };
|
|
3011
|
+
}
|
|
3012
|
+
return { name, attempted: true, ok: false, repaired: false, error: error.message };
|
|
3013
|
+
}
|
|
3014
|
+
|
|
3015
|
+
const absoluteTarget = path.resolve(path.dirname(linkPath), rawTarget);
|
|
3016
|
+
const logicalCurrent = currentRuntimePath(env);
|
|
3017
|
+
if (absoluteTarget === path.resolve(desiredTarget) || absoluteTarget.startsWith(`${logicalCurrent}${path.sep}`)) {
|
|
3018
|
+
return { name, attempted: true, ok: true, repaired: false, reason: "already current" };
|
|
3019
|
+
}
|
|
3020
|
+
if (!allowExternal && !runtimeReleasePath(absoluteTarget, env)) {
|
|
3021
|
+
return { name, attempted: false, ok: true, repaired: false, reason: "external target preserved" };
|
|
3022
|
+
}
|
|
3023
|
+
if (!fs.existsSync(desiredTarget)) {
|
|
3024
|
+
return { name, attempted: true, ok: false, repaired: false, error: `missing ${desiredTarget}` };
|
|
3025
|
+
}
|
|
3026
|
+
switchSymlink(linkPath, desiredTarget);
|
|
3027
|
+
return { name, attempted: true, ok: true, repaired: true, target: desiredTarget };
|
|
3028
|
+
}
|
|
3029
|
+
|
|
3030
|
+
function repairHermesEnv(envPath, env = process.env) {
|
|
3031
|
+
const existing = readEnvFile(envPath);
|
|
3032
|
+
const repaired = [];
|
|
3033
|
+
const currentPackage = path.join(currentRuntimePath(env), "agent-wallet");
|
|
3034
|
+
if (runtimeReleasePath(existing.AGENT_WALLET_PACKAGE_ROOT, env)) {
|
|
3035
|
+
envFileSet(envPath, { AGENT_WALLET_PACKAGE_ROOT: currentPackage });
|
|
3036
|
+
repaired.push("AGENT_WALLET_PACKAGE_ROOT");
|
|
3037
|
+
}
|
|
3038
|
+
if (runtimeReleasePath(existing.AGENT_WALLET_PYTHON, env)) {
|
|
3039
|
+
const currentPython = resolveVenvPython(currentRuntimePath(env));
|
|
3040
|
+
if (currentPython) {
|
|
3041
|
+
envFileSet(envPath, { AGENT_WALLET_PYTHON: currentPython });
|
|
3042
|
+
} else {
|
|
3043
|
+
envFileUnset(envPath, ["AGENT_WALLET_PYTHON"]);
|
|
3044
|
+
}
|
|
3045
|
+
repaired.push("AGENT_WALLET_PYTHON");
|
|
3046
|
+
}
|
|
3047
|
+
return repaired;
|
|
3048
|
+
}
|
|
3049
|
+
|
|
3050
|
+
function globalNpmPackageInfo(env = process.env) {
|
|
3051
|
+
const npmBin = commandPath("npm");
|
|
3052
|
+
if (!npmBin) return null;
|
|
3053
|
+
const rootResult = spawnSync(npmBin, ["root", "--global"], { encoding: "utf8", env });
|
|
3054
|
+
if (rootResult.status !== 0) return null;
|
|
3055
|
+
const packageRoot = path.join(rootResult.stdout.trim(), ...packageJson.name.split("/"));
|
|
3056
|
+
try {
|
|
3057
|
+
const manifest = readJsonFile(path.join(packageRoot, "package.json"));
|
|
3058
|
+
if (manifest?.name !== packageJson.name) return null;
|
|
3059
|
+
return { npm_bin: npmBin, package_root: packageRoot, version: manifest.version || null };
|
|
3060
|
+
} catch {
|
|
3061
|
+
return null;
|
|
3062
|
+
}
|
|
3063
|
+
}
|
|
3064
|
+
|
|
3065
|
+
function refreshGlobalCliIfNeeded(env = process.env) {
|
|
3066
|
+
const installed = globalNpmPackageInfo(env);
|
|
3067
|
+
if (!installed) {
|
|
3068
|
+
return { attempted: false, ok: true, reason: "global package is not installed" };
|
|
3069
|
+
}
|
|
3070
|
+
if (installed.version === packageVersion) {
|
|
3071
|
+
return { attempted: false, ok: true, reason: "already current", version: packageVersion };
|
|
3072
|
+
}
|
|
3073
|
+
const fromNpmCache = path.resolve(packageRoot).split(path.sep).includes("_npx");
|
|
3074
|
+
const forced = env.AGENT_WALLET_FORCE_GLOBAL_CLI_REFRESH === "1";
|
|
3075
|
+
if (!fromNpmCache && !forced) {
|
|
3076
|
+
return {
|
|
3077
|
+
attempted: false,
|
|
3078
|
+
ok: false,
|
|
3079
|
+
reason: "installer is not running from npm exec",
|
|
3080
|
+
installed_version: installed.version,
|
|
3081
|
+
target_version: packageVersion,
|
|
3082
|
+
fix: `npm install --global ${packageJson.name}@${packageVersion}`,
|
|
3083
|
+
};
|
|
3084
|
+
}
|
|
3085
|
+
const packageSpec = `${packageJson.name}@${packageVersion}`;
|
|
3086
|
+
const result = spawnSync(
|
|
3087
|
+
installed.npm_bin,
|
|
3088
|
+
["install", "--global", "--no-audit", "--no-fund", packageSpec],
|
|
3089
|
+
{ encoding: "utf8", env },
|
|
3090
|
+
);
|
|
3091
|
+
return {
|
|
3092
|
+
attempted: true,
|
|
3093
|
+
ok: result.status === 0,
|
|
3094
|
+
previous_version: installed.version,
|
|
3095
|
+
target_version: packageVersion,
|
|
3096
|
+
error: result.status === 0 ? "" : (result.stderr || result.stdout || "").trim(),
|
|
3097
|
+
fix: result.status === 0 ? "" : `npm install --global ${packageSpec}`,
|
|
3098
|
+
};
|
|
3099
|
+
}
|
|
3100
|
+
|
|
3101
|
+
function safelyRefreshIntegration(name, callback) {
|
|
3102
|
+
return integrations().safelyRefresh(name, callback);
|
|
3103
|
+
}
|
|
3104
|
+
|
|
3105
|
+
function repairInstalledEditorIntegrations(env = process.env, names = null) {
|
|
3106
|
+
return hostIntegrations(env).refreshAll(names);
|
|
3107
|
+
}
|
|
3108
|
+
|
|
3109
|
+
function runConnectorsCommand(args, env = process.env) {
|
|
3110
|
+
const activeRoot = resolvedCurrentRuntimeRoot(env);
|
|
3111
|
+
const runtimeRoot = activeRoot && fs.existsSync(path.join(activeRoot, "agent-wallet", "agent_wallet"))
|
|
3112
|
+
? activeRoot
|
|
3113
|
+
: packageRoot;
|
|
3114
|
+
const walletRoot = path.join(runtimeRoot, "agent-wallet");
|
|
3115
|
+
const pythonBin = resolveVenvPython(runtimeRoot) || resolveAgentWalletPython(walletRoot);
|
|
3116
|
+
const pythonPath = String(env.PYTHONPATH || "").trim();
|
|
3117
|
+
const childEnv = {
|
|
3118
|
+
...env,
|
|
3119
|
+
PYTHONPATH: pythonPath ? `${walletRoot}${path.delimiter}${pythonPath}` : walletRoot,
|
|
3120
|
+
};
|
|
3121
|
+
const result = spawnSync(
|
|
3122
|
+
pythonBin,
|
|
3123
|
+
["-m", "agent_wallet.connector_cli", ...args],
|
|
3124
|
+
{
|
|
3125
|
+
cwd: walletRoot,
|
|
3126
|
+
env: childEnv,
|
|
3127
|
+
encoding: "utf8",
|
|
3128
|
+
},
|
|
3129
|
+
);
|
|
3130
|
+
if (result.stdout) process.stdout.write(result.stdout);
|
|
3131
|
+
if (result.stderr) process.stderr.write(result.stderr);
|
|
3132
|
+
if (result.error) {
|
|
3133
|
+
console.error(JSON.stringify({ ok: false, error: result.error.message }));
|
|
3134
|
+
return 1;
|
|
3135
|
+
}
|
|
3136
|
+
return result.status ?? 1;
|
|
3137
|
+
}
|
|
3138
|
+
|
|
3139
|
+
function runVaultCommand(args, env = process.env) {
|
|
3140
|
+
const activeRoot = resolvedCurrentRuntimeRoot(env);
|
|
3141
|
+
const runtimeRoot = activeRoot && fs.existsSync(path.join(activeRoot, "agent-wallet", "agent_wallet"))
|
|
3142
|
+
? activeRoot : packageRoot;
|
|
3143
|
+
const walletRoot = path.join(runtimeRoot, "agent-wallet");
|
|
3144
|
+
const pythonBin = resolveVenvPython(runtimeRoot) || resolveAgentWalletPython(walletRoot);
|
|
3145
|
+
const pythonPath = String(env.PYTHONPATH || "").trim();
|
|
3146
|
+
const result = spawnSync(pythonBin, ["-m", "agent_wallet.card_vault_cli", ...args], {
|
|
3147
|
+
cwd: walletRoot,
|
|
3148
|
+
env: { ...env, PYTHONPATH: pythonPath ? `${walletRoot}${path.delimiter}${pythonPath}` : walletRoot },
|
|
3149
|
+
// Preserve the owner's terminal; never buffer secrets in the installer or telemetry.
|
|
3150
|
+
stdio: "inherit",
|
|
3151
|
+
});
|
|
3152
|
+
if (result.error) console.error("Could not start card vault CLI. Check the installed Python runtime.");
|
|
3153
|
+
return result.status ?? 1;
|
|
3154
|
+
}
|
|
3155
|
+
|
|
3156
|
+
const args = process.argv.slice(2);
|
|
3157
|
+
const command = args[0] || "install";
|
|
3158
|
+
|
|
3159
|
+
if (command === "--telemetry-flush") {
|
|
3160
|
+
process.exit(await telemetryFlushMain());
|
|
3161
|
+
}
|
|
3162
|
+
|
|
3163
|
+
if (command === "--help" || command === "-h" || command === "help") {
|
|
3164
|
+
printHelp();
|
|
3165
|
+
process.exit(0);
|
|
3166
|
+
}
|
|
3167
|
+
|
|
3168
|
+
if (command === "--version" || command === "-v" || command === "version") {
|
|
3169
|
+
console.log(packageVersion);
|
|
3170
|
+
process.exit(0);
|
|
3171
|
+
}
|
|
3172
|
+
|
|
3173
|
+
if (command === "doctor") {
|
|
3174
|
+
process.exit(runDoctor(args.slice(1)));
|
|
3175
|
+
}
|
|
3176
|
+
|
|
3177
|
+
if (command === "--self-verify") {
|
|
3178
|
+
const releaseRoot = args[1] ? path.resolve(expandHome(args[1])) : resolvedCurrentRuntimeRoot();
|
|
3179
|
+
const result = releaseRoot
|
|
3180
|
+
? verifyRuntime(releaseRoot)
|
|
3181
|
+
: { ok: false, error: "no runtime to verify", category: "local_env" };
|
|
3182
|
+
console.log(JSON.stringify(result));
|
|
3183
|
+
process.exit(result.ok ? 0 : 1);
|
|
3184
|
+
}
|
|
3185
|
+
|
|
3186
|
+
if (command === "status") {
|
|
3187
|
+
process.exit(runStatus(args.slice(1)));
|
|
3188
|
+
}
|
|
3189
|
+
|
|
3190
|
+
if (command === "vault") {
|
|
3191
|
+
process.exit(runVaultCommand(args.slice(1)));
|
|
3192
|
+
}
|
|
3193
|
+
|
|
3194
|
+
if (command === "connectors") {
|
|
3195
|
+
process.exit(runConnectorsCommand(args.slice(1)));
|
|
3196
|
+
}
|
|
3197
|
+
|
|
3198
|
+
if (command === "hedera-treasury") {
|
|
3199
|
+
const { runTreasuryCli } = await import("../integrations/hedera/treasury-guard/src/treasury-cli.mjs");
|
|
3200
|
+
process.exit(await runTreasuryCli(args.slice(1), { home: resolveOpenclawHome() }));
|
|
3201
|
+
}
|
|
3202
|
+
|
|
3203
|
+
if (command === "mcp") {
|
|
3204
|
+
process.exit(runUniversalMcpCommand(args.slice(1)));
|
|
3205
|
+
}
|
|
3206
|
+
|
|
3207
|
+
if (command === "detect") {
|
|
3208
|
+
process.exit(runDetect(args.slice(1)));
|
|
3209
|
+
}
|
|
3210
|
+
|
|
3211
|
+
if (command === "install" || command === "setup") {
|
|
3212
|
+
const commandArgs = args.slice(1);
|
|
3213
|
+
process.exit(
|
|
3214
|
+
await runWithCliTelemetryAsync(
|
|
3215
|
+
() => runUniversalInstall(commandArgs, { commandName: "install" }),
|
|
3216
|
+
{
|
|
3217
|
+
startEvent: "install_start",
|
|
3218
|
+
successEvent: "install_success",
|
|
3219
|
+
failedEvent: "install_failed",
|
|
3220
|
+
commandName: "install",
|
|
3221
|
+
args: commandArgs,
|
|
3222
|
+
},
|
|
3223
|
+
),
|
|
3224
|
+
);
|
|
3225
|
+
}
|
|
3226
|
+
|
|
3227
|
+
if (command === "update") {
|
|
3228
|
+
const commandArgs = args.slice(1);
|
|
3229
|
+
process.exit(
|
|
3230
|
+
runWithCliTelemetry(
|
|
3231
|
+
() => runUpdate(commandArgs),
|
|
3232
|
+
{
|
|
3233
|
+
startEvent: "update_start",
|
|
3234
|
+
successEvent: "update_success",
|
|
3235
|
+
failedEvent: "update_failed",
|
|
3236
|
+
commandName: "update",
|
|
3237
|
+
args: commandArgs,
|
|
3238
|
+
},
|
|
3239
|
+
),
|
|
3240
|
+
);
|
|
3241
|
+
}
|
|
3242
|
+
|
|
3243
|
+
if (command === "rollback") {
|
|
3244
|
+
process.exit(runRollback(args.slice(1)));
|
|
3245
|
+
}
|
|
3246
|
+
|
|
3247
|
+
if (command === "hermes") {
|
|
3248
|
+
const subcommand = args[1] || "install";
|
|
3249
|
+
if (subcommand === "install" || subcommand === "setup") {
|
|
3250
|
+
const commandArgs = args.slice(2);
|
|
3251
|
+
process.exit(
|
|
3252
|
+
runWithCliTelemetry(
|
|
3253
|
+
() => runHermesInstall(commandArgs),
|
|
3254
|
+
{
|
|
3255
|
+
startEvent: "plugin_install_start",
|
|
3256
|
+
successEvent: "plugin_install_success",
|
|
3257
|
+
failedEvent: "plugin_install_failed",
|
|
3258
|
+
commandName: "hermes_install",
|
|
3259
|
+
host: "hermes",
|
|
3260
|
+
args: commandArgs,
|
|
3261
|
+
},
|
|
3262
|
+
),
|
|
3263
|
+
);
|
|
3264
|
+
}
|
|
3265
|
+
console.error(`Unknown hermes command: ${subcommand}`);
|
|
3266
|
+
console.error("Run `openclaw-agent-wallet hermes install --yes` to connect Hermes Agent.");
|
|
3267
|
+
process.exit(2);
|
|
3268
|
+
}
|
|
3269
|
+
|
|
3270
|
+
if (command === "codex") {
|
|
3271
|
+
const subcommand = args[1] || "install";
|
|
3272
|
+
if (subcommand === "install" || subcommand === "setup") {
|
|
3273
|
+
const commandArgs = args.slice(2);
|
|
3274
|
+
process.exit(
|
|
3275
|
+
runWithCliTelemetry(
|
|
3276
|
+
() => runCodexInstall(commandArgs),
|
|
3277
|
+
{
|
|
3278
|
+
startEvent: "plugin_install_start",
|
|
3279
|
+
successEvent: "plugin_install_success",
|
|
3280
|
+
failedEvent: "plugin_install_failed",
|
|
3281
|
+
commandName: "codex_install",
|
|
3282
|
+
host: "codex",
|
|
3283
|
+
args: commandArgs,
|
|
3284
|
+
},
|
|
3285
|
+
),
|
|
3286
|
+
);
|
|
3287
|
+
}
|
|
3288
|
+
console.error(`Unknown codex command: ${subcommand}`);
|
|
3289
|
+
console.error("Run `openclaw-agent-wallet codex install --yes` to connect Codex.");
|
|
3290
|
+
process.exit(2);
|
|
3291
|
+
}
|
|
3292
|
+
|
|
3293
|
+
if (command === "claude-code") {
|
|
3294
|
+
const subcommand = args[1] || "install";
|
|
3295
|
+
if (subcommand === "install" || subcommand === "setup") {
|
|
3296
|
+
const commandArgs = args.slice(2);
|
|
3297
|
+
process.exit(
|
|
3298
|
+
runWithCliTelemetry(
|
|
3299
|
+
() => runClaudeCodeInstall(commandArgs),
|
|
3300
|
+
{
|
|
3301
|
+
startEvent: "plugin_install_start",
|
|
3302
|
+
successEvent: "plugin_install_success",
|
|
3303
|
+
failedEvent: "plugin_install_failed",
|
|
3304
|
+
commandName: "claude_code_install",
|
|
3305
|
+
host: "claude-code",
|
|
3306
|
+
args: commandArgs,
|
|
3307
|
+
},
|
|
3308
|
+
),
|
|
3309
|
+
);
|
|
3310
|
+
}
|
|
3311
|
+
console.error(`Unknown claude-code command: ${subcommand}`);
|
|
3312
|
+
console.error("Run `openclaw-agent-wallet claude-code install --yes` to connect Claude Code.");
|
|
3313
|
+
process.exit(2);
|
|
3314
|
+
}
|
|
3315
|
+
|
|
3316
|
+
if (command === "cursor") {
|
|
3317
|
+
const subcommand = args[1] || "install";
|
|
3318
|
+
if (subcommand === "install" || subcommand === "setup") {
|
|
3319
|
+
const commandArgs = args.slice(2);
|
|
3320
|
+
process.exit(runWithCliTelemetry(
|
|
3321
|
+
() => runCursorInstall(commandArgs),
|
|
3322
|
+
{
|
|
3323
|
+
startEvent: "plugin_install_start",
|
|
3324
|
+
successEvent: "plugin_install_success",
|
|
3325
|
+
failedEvent: "plugin_install_failed",
|
|
3326
|
+
commandName: "cursor_install",
|
|
3327
|
+
host: "cursor",
|
|
3328
|
+
args: commandArgs,
|
|
3329
|
+
},
|
|
3330
|
+
));
|
|
3331
|
+
}
|
|
3332
|
+
console.error(`Unknown cursor command: ${subcommand}`);
|
|
3333
|
+
console.error("Run `openclaw-agent-wallet cursor install --yes` to connect Cursor.");
|
|
3334
|
+
process.exit(2);
|
|
3335
|
+
}
|
|
3336
|
+
|
|
3337
|
+
if (command.startsWith("-")) {
|
|
3338
|
+
process.exit(
|
|
3339
|
+
await runWithCliTelemetryAsync(
|
|
3340
|
+
() => runUniversalInstall(args, { commandName: "install" }),
|
|
3341
|
+
{
|
|
3342
|
+
startEvent: "install_start",
|
|
3343
|
+
successEvent: "install_success",
|
|
3344
|
+
failedEvent: "install_failed",
|
|
3345
|
+
commandName: "install",
|
|
3346
|
+
args,
|
|
3347
|
+
},
|
|
3348
|
+
),
|
|
3349
|
+
);
|
|
3350
|
+
}
|
|
3351
|
+
|
|
3352
|
+
console.error(`Unknown command: ${command}`);
|
|
3353
|
+
console.error("Run `openclaw-agent-wallet --help` for usage.");
|
|
3354
|
+
process.exit(2);
|