openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
6 lines • 13.7 kB
JavaScript
import{r as defaultRuntime}from"./runtime-DIcLIHcp.mjs";import{t as captureRuntimeConfigAsyncReader}from"./io.runtime-BvSeVFIY.mjs";import{r as theme}from"./theme-DS2CtI9X.mjs";import{n as info}from"./globals-l9I53ZKA.mjs";import"./runtime-config-snapshot-N0DER8v5.mjs";import{a as resolveFirstExtensionProfileName,i as resolveBrowserConfig,s as resolveProfile}from"./config-BC-f8ygh.mjs";import{t as ensureExtensionRelayToken}from"./relay-auth-lWbvjInH.mjs";import{g as relayKeyIdFromHex,n as BROWSER_RELAY_AUTH_COMPLETE_PATH,t as BROWSER_RELAY_AUTH_CHALLENGE_PATH,v as RELAY_AUTH_LABEL}from"./auth-v2-C3c_XWtq.mjs";import{a as repairChromeExtensionNativeHosts,c as FOUNDATION_CHROME_WEB_STORE_URL,i as resolveChromeExtensionLoadPath,l as removeChromeStoreInstallRequests,n as runBrowserExtensionSetup,o as resolveNativeHostPath,r as normalizeExtensionInstallWaitMs,s as uninstallChromeExtensionNativeHosts,t as observeBrowserExtensionSetup,u as NativeHostSetupContextError}from"./extension-setup-Cx-Vsi0y.mjs";import"./runtime-env-Qb8DG6sI.mjs";import{n as runCommandWithRuntime}from"./cli-utils-Dm67QQ71.mjs";import"./cli-runtime-BLVbyFtb.mjs";import{t as buildBrowserExtensionPairing}from"./extension-pairing-B3EwkI3K.mjs";import{c as runBrowserCliCommand}from"./browser-cli-shared-5N8-HcJT.mjs";import{fileURLToPath,pathToFileURL}from"node:url";import path from"node:path";
//!extensions/browser/src/cli/browser-cli-extension.ts
async function buildCdpEndpoint(e){const t=await captureRuntimeConfigAsyncReader()();const n=resolveBrowserConfig(t.browser,t);const r=await ensureExtensionRelayToken();const i=resolveFirstExtensionProfileName(n);const a=(i?resolveProfile(n,i):null)?.cdpPort??n.extensionRelayDefaultPort;const o=`http://127.0.0.1:${a}`;const s={browserUrl:o,wsEndpoint:`ws://127.0.0.1:${a}/cdp`,auth:{label:RELAY_AUTH_LABEL,version:2,keyId:relayKeyIdFromHex(r),challengeUrl:new URL(BROWSER_RELAY_AUTH_CHALLENGE_PATH,o).toString(),completeUrl:new URL(BROWSER_RELAY_AUTH_COMPLETE_PATH,o).toString(),role:`cdp`,transport:`connection`,method:`SEQUENCE`,resource:`/json/version -> /cdp`,flow:`cdp`}};if(e&&!n.extensionRelay.allowLegacyAuth)throw new Error(`Legacy browser relay auth is disabled; remove --legacy-bearer and use Browser Relay Authentication v2.`);return{...s,...e?{headers:{Authorization:`Bearer ${r}`}}:{}}}function registerBrowserExtensionCommands(e,t,n){const r=path.resolve(path.dirname(fileURLToPath(import.meta.url)),`..`,`..`);const i=n??r;const a=path.join(n||r,`chrome-extension`);const o=e.command(`extension`).description(`Install and inspect the OpenClaw Chrome extension bootstrap`);o.command(`native-host`,{hidden:true}).allowUnknownOption(true).allowExcessArguments(true).action(async()=>{try{const e=await resolveNativeHostPath(i);await import(pathToFileURL(e).href)}catch{process.exitCode=1}});o.command(`setup`).description(`Inspect, prepare, or verify automatic Chrome setup on this host`).option(`--action <action>`,`inspect, install, or verify`,`inspect`).option(`--native-host-executable <path>`,`Local self-contained Windows bootstrap executable`).option(`--browser-profile <name>`,`Local extension profile`).option(`--wait-ms <ms>`,`Bounded Chrome discovery wait`,`1000`).option(`--json`,`Print the redacted setup result`).action(async(e,n)=>{await runCommandWithRuntime(defaultRuntime,async()=>{if(e.action!==`inspect`&&e.action!==`install`&&e.action!==`verify`)throw new Error(`--action must be inspect, install, or verify`);const r=await runBrowserExtensionSetup({action:e.action,nativeHostExecutable:e.nativeHostExecutable,bundledDir:a,pluginRoot:i,cfg:await captureRuntimeConfigAsyncReader()(),profile:e.browserProfile??t(n).browserProfile,waitMs:normalizeExtensionInstallWaitMs(e.waitMs)});if(e.json||t(n).json)defaultRuntime.writeJson(r);else defaultRuntime.log(`${r.target.hostname} · ${r.target.profile}: ${r.phase} (${r.reason}); next: ${r.nextAction}`)},e=>{defaultRuntime.error(e instanceof NativeHostSetupContextError?e.message:`Chrome setup could not finish. Check the action, local profile, and native host installation. If automatic Windows selection is unverified, repair the intended existing profile with --browser-profile <name> --action install.`);defaultRuntime.exit(1)})});o.command(`path`).description(`Print the unpacked Chrome extension directory (Load unpacked)`).action(async()=>{await runCommandWithRuntime(defaultRuntime,async()=>{defaultRuntime.log(await resolveChromeExtensionLoadPath(a))})});o.command(`install`).description(`Set up the Chrome extension and request supported Store installation`).option(`--browser-profile <name>`,`Local extension profile; repair preserves an owned selector`).option(`--native-host-executable <path>`,`Local self-contained Windows bootstrap executable`).option(`--no-store`,`Prepare native bootstrap and development files without requesting Store installation`).option(`--json`,`Print a machine-readable status report`).option(`--wait-ms <ms>`,`How long to wait after pre-registration for Chrome to verify the extension`,String(3e4)).action(async(e,n)=>{await runBrowserCliCommand(async()=>{const r=e.json===true||t(n).json===true;const o=normalizeExtensionInstallWaitMs(e.waitMs);if(!r)defaultRuntime.log(info(`Preparing the OpenClaw Chrome extension…`));const s=await observeBrowserExtensionSetup({action:`install`,nativeHostExecutable:e.nativeHostExecutable,bundledDir:a,pluginRoot:i,waitMs:o,requestStoreInstall:e.store!==false,profile:e.browserProfile??t(n).browserProfile,onProgress:r?void 0:e=>defaultRuntime.log(info(e))});if(r)defaultRuntime.writeJson(s);else{for(const e of s.issues)defaultRuntime.error(theme.warn(e));defaultRuntime.log(s.manualSetupRequired?theme.warn(s.platformSupport===`manual_required`?`Automatic native bootstrap is not supported on this platform; use Settings for manual pairing.`:s.storeInstallRequests.some(e=>e.state===`requested`)?`Store installation requested. Enable OpenClaw in chrome://extensions and approve Chrome's prompt. If it has not appeared, restart Chrome when convenient or add it from ${FOUNDATION_CHROME_WEB_STORE_URL}. Run extension status to check setup again.`:`Setup needs attention. Add OpenClaw from ${FOUNDATION_CHROME_WEB_STORE_URL} after native registration succeeds. For development, load the printed unpacked path. If the extension attempted setup before the native host existed, restart Chrome once.`):info(`Native host and extension identity verified for ${s.discovered.length+s.storeDiscovered.length} profile registration(s). Check the extension popup for Connected before using browser automation.`))}if(s.manualSetupRequired)defaultRuntime.exit(1)})});o.command(`repair`).description(`Inspect or repair existing native hosts without browser profile discovery`).option(`--from <entrypoint>`,`Repair only registrations referencing this exact absolute entrypoint`).option(`--dry-run`,`Inspect registered targets without changing files`,false).option(`--json`,`Print a machine-readable repair report`).action(async(e,n)=>{await runCommandWithRuntime(defaultRuntime,async()=>{const r=await repairChromeExtensionNativeHosts({bundledDir:a,pluginRoot:i,fromNativeHostPath:e.from,dryRun:e.dryRun===true});if(e.json===true||t(n).json===true)defaultRuntime.writeJson(r);else{for(const e of[...r.changes,...r.warnings])defaultRuntime.log(e);defaultRuntime.log(`Registered native entries: ${r.retainedNativeHostPaths.join(`, `)||`none`}`)}if(r.manualRequired||!r.retentionSafe||r.warnings.length>0)defaultRuntime.exit(1)})});o.command(`status`).description(`Inspect extension copies, Chrome IDs, and native-host registrations`).option(`--native-host-executable <path>`,`Local self-contained Windows bootstrap executable`).option(`--browser-profile <name>`,`Local extension profile`).option(`--json`,`Print a machine-readable status report`).action(async(e,n)=>{await runCommandWithRuntime(defaultRuntime,async()=>{const r=e.json===true||t(n).json===true;const o=await observeBrowserExtensionSetup({action:`inspect`,profile:e.browserProfile??t(n).browserProfile,nativeHostExecutable:e.nativeHostExecutable,pluginRoot:i,bundledDir:a});if(r){defaultRuntime.writeJson(o);return}defaultRuntime.log([`Extension copy: ${o.installedCopy.owned?`installed`:`bundled fallback`}`,`Store request: ${o.storeInstallRequests.length>0?o.storeInstallRequests.map(e=>`${e.browser}: ${e.state??`unknown`}`).join(`, `):`use the Chrome Web Store`}`,`Store: ${o.storeDiscovered.length>0?o.storeDiscovered.map(e=>`${e.extensionId} (${e.browser}/${e.profile}; ${e.enabled?`enabled`:e.awaitingApproval?`awaiting approval`:`disabled`})`).join(`, `):`not detected`}`,`Development: ${o.discovered.length>0?o.discovered.map(e=>`${e.extensionId} (${e.browser}/${e.profile})`).join(`, `):`none detected`}`,`Load unpacked: ${o.installedCopy.owned?o.installedCopy.path:o.bundledPath}`,`Native hosts: ${o.registrations.filter(e=>e.state===`owned`).length} owned`,`Setup: ${o.manualSetupRequired?`manual action required`:`automatic bootstrap ready`}`].join(`
`))})});o.command(`uninstall-store`).description(`Remove OpenClaw-owned Store install requests; Chrome may remove the extension on restart`).option(`--json`,`Print a machine-readable removal report`).action(async(e,n)=>{await runCommandWithRuntime(defaultRuntime,async()=>{if(process.platform===`win32`)throw new Error(`Windows Store removal belongs to uninstall-host --remove-store; no standalone Store writer is available.`);const r=await removeChromeStoreInstallRequests();if(e.json===true||t(n).json===true)defaultRuntime.writeJson(r);else{defaultRuntime.log(info(`Removed ${r.removed.length} owned Store install request(s). Chrome may remove externally installed copies on its next start. Native hosts are unchanged.`));for(const e of r.refused)defaultRuntime.error(theme.warn(`Refused foreign Store registration: ${e}`))}if(r.refused.length>0)defaultRuntime.exit(1)})});o.command(`uninstall-host`).description(`Remove only OpenClaw-owned Chrome native-host registrations`).option(`--native-host-executable <path>`,`Local self-contained Windows bootstrap executable`).option(`--browser-profile <name>`,`Local extension profile`).option(`--remove-store`,`Remove owned Windows Store requests before native registration`).option(`--json`,`Print a machine-readable removal report`).action(async(e,n)=>{await runCommandWithRuntime(defaultRuntime,async()=>{const r=e.json===true||t(n).json===true;const a=await uninstallChromeExtensionNativeHosts({pluginRoot:i,nativeHostExecutable:e.nativeHostExecutable,browserProfile:e.browserProfile??t(n).browserProfile,removeStore:e.removeStore===true});if(r)defaultRuntime.writeJson(a);else{defaultRuntime.log(a.manualRequired?theme.warn(`Windows native-host removal is manual; no registry key was changed.`):info(`Removed ${a.removed.length} owned native-host artifact(s).`));for(const e of a.refused)defaultRuntime.error(theme.warn(`Refused registration removal: ${e}`))}if(a.refused.length)defaultRuntime.exit(1)})});o.command(`pair`).description(`Print an advanced manual pairing string`).option(`--json`,`Print the pairing string as JSON`).option(`--local-gateway`,`Pair through this host’s local Gateway for desktop native helpers`).option(`--gateway-url <url>`,`Print a remote pairing string for a Chrome on another machine (e.g. wss://gateway.example.com)`).action(async(e,n)=>{await runBrowserCliCommand(async()=>{const r=e.json===true||t(n).json===true;const i=await captureRuntimeConfigAsyncReader()();const o=e.localGateway===true;if(o&&e.gatewayUrl!==void 0)throw new Error(`--local-gateway cannot be combined with --gateway-url`);if(o&&i.gateway?.mode===`remote`)throw new Error(`--local-gateway requires a local Gateway configuration`);const s=await buildBrowserExtensionPairing({cfg:i,gatewayUrl:e.gatewayUrl,localTransport:o?`gateway`:void 0});const c=s.topology===`direct-remote`;if(r){defaultRuntime.writeJson({pairingString:s.pairingString,relayPort:s.relayPort,remote:c});return}const l=c?info(`Remote pairing: load and pair the extension on the machine running Chrome; it connects to this gateway over wss://.`):info(`Run this on the machine that hosts the browser (gateway host or browser node).`);defaultRuntime.log([l,info(`1. Load the extension: chrome://extensions → Developer mode → Load unpacked →`),` ${a}`,info(`2. Open the OpenClaw popup and paste this pairing string:`),``,theme.heading(s.pairingString),``,info(`The relay key is a host-local secret; keep it private.`)].join(`
`))})});o.command(`cdp`).description(`Print non-secret Browser Relay Authentication v2 CDP metadata`).option(`--json`,`Print the endpoint as JSON`).option(`--legacy-bearer`,`Print the legacy Bearer header while browser.extensionRelay.allowLegacyAuth is enabled`).action(async(e,n)=>{await runBrowserCliCommand(async()=>{const r=e.json===true||t(n).json===true;const i=e.legacyBearer===true;const a=await buildCdpEndpoint(i);if(i)defaultRuntime.error(theme.warn(`Warning: --legacy-bearer reveals the relay key in an authorization header. Migrate this client to Browser Relay Authentication v2.`));if(r){defaultRuntime.writeJson(a);return}const o=[info(`Relay CDP endpoint (pair the extension first):`),`browserUrl: ${a.browserUrl}`,`wsEndpoint: ${a.wsEndpoint}`,`auth: ${a.auth.label} v${a.auth.version}`,`keyId: ${a.auth.keyId}`,`challenge: POST ${a.auth.challengeUrl}`,`complete: POST ${a.auth.completeUrl}`,`sequence: ${a.auth.resource}`];if(a.headers)o.push(`legacy: Authorization: ${a.headers.Authorization}`);else o.push(``,info(`No relay key or authorization header is printed.`));defaultRuntime.log(o.join(`
`))})})}export{registerBrowserExtensionCommands};