openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
4 lines • 11.7 kB
JavaScript
import{S as resolveNodeWindowsTaskName,b as resolveNodeServiceIdentityEnvironment,x as resolveNodeSystemdServiceName,y as resolveNodeLaunchAgentLabel}from"./constants-tH5Q8GoI.mjs";import{r as defaultRuntime}from"./runtime-DIcLIHcp.mjs";import{t as formatErrorMessage}from"./errors-D37BJTfI.mjs";import{t as formatCliCommand}from"./command-format-D7KhNLP0.mjs";import{t as VERSION}from"./version-DrZ9Nfde.mjs";import{t as quoteCliArg}from"./quote-cli-arg-BUHoGELl.mjs";import{t as colorize}from"./theme-DS2CtI9X.mjs";import{s as resolveManagedGatewayServiceCommand}from"./service-types-B7cjFijA.mjs";import{r as resolveSystemdUserServiceAccount}from"./systemd-user-transport-JBcQeydq.mjs";import{_ as isSystemdUserServiceAvailable}from"./systemd-busctl-query-CVbse9JI.mjs";import{n as loadDeviceIdentityIfPresentAsync}from"./device-identity-async-BkJXqwph.mjs";import{o as buildPlatformRuntimeLogHints,s as buildPlatformServiceStartHints,t as formatRuntimeStatus}from"./runtime-format-hFkv-6vU.mjs";import{_ as resolveRuntimeStatusColor,m as resolveDaemonInstallBlockMessage,n as createCliStatusTextStyles,r as createDaemonInstallActionContext,u as projectDaemonServiceForJson,x as installDaemonServiceAndEmit,y as buildDaemonServiceSnapshot}from"./shared-BctPQR6n.mjs";import{s as resolvePinnedDaemonRuntimePath,u as resolveRecordedDaemonRuntime}from"./runtime-paths-BOio2Rt6.mjs";import{c as withDaemonRuntimePinMigration,o as readDaemonRuntimePinForInstall}from"./runtime-pin-state-DeLfcMhS.mjs";import{i as resolveNodeProgramArguments}from"./program-args-Dls9y3ku.mjs";import{n as readSystemdUserLingerStatus}from"./systemd-BLmKdJhz.mjs";import{r as installDurableServiceCli}from"./durable-service-entrypoint-CYy_WbI9.mjs";import{n as formatInvalidPortOption,t as formatInvalidConfigPort}from"./error-format-D9Q6t3gZ.mjs";import{a as loadNodeHostConfig}from"./config-W6i7-yzD.mjs";import{t as canReuseNodeHostDeviceToken}from"./gateway-auth-BpMkgYfW.mjs";import{n as resolveDaemonInstallPlanInputs,r as resolveDaemonRuntimeBinDir,t as emitNodeRuntimeWarning}from"./daemon-install-runtime-warning-ySUnuFLs.mjs";import{i as resolveGatewayDaemonRuntime,r as isGatewayDaemonRuntime}from"./daemon-runtime-CM-TWmEx.mjs";import{t as resolveNodeService}from"./node-service-Bgxq6G60.mjs";import{n as buildNodeServiceEnvironment}from"./service-env-DR7NRpBz.mjs";import{n as resolveRestoreServiceCli,t as assertExpectedRuntimePin}from"./install-restore-cli-Djkq7_5B.mjs";import{a as runServiceUninstall,i as runServiceStop,n as runServiceRestart,r as runServiceStart}from"./lifecycle-core-CfwSEwvB.mjs";import{n as inspectServiceRuntimeIntent}from"./status.runtime-intent-CIrt2oKF.mjs";import{t as resolveNodeGatewayOptions}from"./gateway-options-CK0ICeEV.mjs";
//!src/cli/node-cli/daemon.ts
function renderNodeServiceStartHints(){return buildPlatformServiceStartHints({installHint:formatCliCommand(`openclaw node install`),startCommand:formatCliCommand(`openclaw node start`),launchAgentPlistPath:`~/Library/LaunchAgents/${resolveNodeLaunchAgentLabel(process.env.OPENCLAW_PROFILE)}.plist`,systemdServiceName:resolveNodeSystemdServiceName(process.env.OPENCLAW_PROFILE),windowsTaskName:resolveNodeWindowsTaskName(process.env.OPENCLAW_PROFILE)})}async function warnIfSystemdUserLingerDisabled(e){if(process.platform!==`linux`)return;if(!await isSystemdUserServiceAvailable())return;const t=resolveSystemdUserServiceAccount(process.env);if(!t)return;const n=await readSystemdUserLingerStatus({env:process.env,user:t});if(!n||n.linger===`yes`)return;e(`Systemd lingering is disabled for ${n.user}. The node service will stop when you log out. Run: sudo loginctl enable-linger ${n.user}`)}async function runNodeDaemonInstall(e){const{json:t,stdout:n,warnings:r,warn:i,emit:a,emitMessage:o,fail:s}=createDaemonInstallActionContext(e.json);const c=resolveDaemonInstallBlockMessage(`node`);if(c){s(c);return}const l=await loadNodeHostConfig();let u;try{u=resolveNodeGatewayOptions(e,l)}catch(e){s(e instanceof Error?e.message:String(e));return}const{host:d,port:f,contextPath:p,tls:m,tlsFingerprint:h,cloudflareAccess:g,gatewayTarget:_}=u;if(f===null||!Number.isFinite(f)||f<=0||f>65535){s(e.port!==void 0?formatInvalidPortOption(`--port`):formatInvalidConfigPort(`node.gateway.port`));return}if(e.tls===false&&e.tlsFingerprint!==void 0){s(`--no-tls cannot be combined with --tls-fingerprint`);return}if(g&&m!==true){s(`Cloudflare Access credentials require --tls for the node Gateway connection`);return}const v=resolveNodeService();let y;try{y=await v.readCommand(process.env)}catch(e){s(`Node service inspection failed: ${formatErrorMessage(e)}`);return}const b=resolveManagedGatewayServiceCommand(y);const x={...process.env,OPENCLAW_WRAPPER:process.env.OPENCLAW_WRAPPER??b?.environment?.OPENCLAW_WRAPPER};let S;try{S=await withDaemonRuntimePinMigration({...x,...resolveNodeServiceIdentityEnvironment(x)},()=>readDaemonRuntimePinForInstall({kind:`node`,env:x},y,e.runtime!==void 0||e.runtimePath!==void 0))}catch(e){s(`Runtime pin inspection failed: ${formatErrorMessage(e)}`);return}let C;let w;try{if(e.expectedRuntimePin!==void 0)assertExpectedRuntimePin(e.expectedRuntimePin,S,`Node`);if(e.restoreServiceCli!==void 0)({serviceCli:C,runtimePath:w}=await resolveRestoreServiceCli(e.restoreServiceCli,e,x))}catch(e){s(e instanceof Error?e.message:String(e));return}let T=e.runtimePath??(e.runtime?void 0:S.pin?.path);const E=e.runtime||S.pin?.runtime||resolveGatewayDaemonRuntime([T??``]);if(!isGatewayDaemonRuntime(E)){s(`Invalid --runtime (use "node", "bun", or "standalone")`);return}try{if(!x.OPENCLAW_WRAPPER?.trim()||e.runtimePath!==void 0)T=await resolvePinnedDaemonRuntimePath(T,E,x)}catch(e){s(`Invalid runtime pin: ${formatErrorMessage(e)}`);return}let D;try{D=await v.isLoaded({env:process.env})}catch(e){s(`Node service check failed: ${formatErrorMessage(e)}`);return}if(D&&!e.force){await warnIfSystemdUserLingerDisabled(i);o({ok:true,result:`already-installed`,message:`Node service already ${v.loadedText}.`,service:buildDaemonServiceSnapshot(v,D),warnings:r.length?r:void 0});if(!t)defaultRuntime.log(`Reinstall with: ${formatCliCommand(`openclaw node install --force`)}`);return}const O=e.runtime===void 0&&!T&&!x.OPENCLAW_WRAPPER?.trim()?await resolveRecordedDaemonRuntime(b?.programArguments[0],x):void 0;const k=O?.status===`supported`?O:void 0;const A=x.OPENCLAW_WRAPPER;const{devMode:j,runtime:M,runtimePath:N,cliEntrypoint:P,durableCliReplacement:F}=await resolveDaemonInstallPlanInputs({service:`node`,env:x,runtime:k?.runtime??E,runtimeExplicit:e.runtime!==void 0||e.runtimePath!==void 0,runtimePath:w??k?.path,pinnedRuntimePath:T,serviceCli:C,wrapperPath:A,installDurableCli:true,warn:i});const{programArguments:I,workingDirectory:L}=await resolveNodeProgramArguments({cliEntrypoint:P,resumeSavedTarget:_?.kind===`openclaw-id`,host:d,port:f,contextPath:p,tls:Boolean(m),tlsFingerprint:h,nodeId:e.nodeId,displayName:e.displayName,installedAppsSharing:e.shareInstalledApps,commands:e.commands,allCommands:e.allCommands,dev:j,runtime:M,runtimePath:N,wrapperPath:A});if(e.authFromEnv&&_?.kind===`openclaw-id`){s(`OpenClaw ID nodes use their enrolled key, not --auth-from-env`);return}if(e.authFromEnv)I.push(`--auth-from-env`);await emitNodeRuntimeWarning({env:x,runtime:M,nodeProgram:I[0],warn:i,title:`Node daemon runtime`});const R=buildNodeServiceEnvironment({env:x,execPath:C?.executable,runtime:M,extraPathDirs:resolveDaemonRuntimeBinDir(N)});if(_?.kind===`openclaw-id`){delete R.OPENCLAW_GATEWAY_TOKEN;delete R.OPENCLAW_GATEWAY_PASSWORD;delete R.CF_ACCESS_CLIENT_ID;delete R.CF_ACCESS_CLIENT_SECRET}else if(!e.authFromEnv){const e=(await loadNodeHostConfig(x))?.gateway;const t=e?await loadDeviceIdentityIfPresentAsync({env:x}):null;if(t&&await canReuseNodeHostDeviceToken({savedGateway:e,gatewayCandidates:[{host:d,port:f,contextPath:p,tls:Boolean(m)}],deviceId:t.deviceId,env:x})){delete R.OPENCLAW_GATEWAY_TOKEN;delete R.OPENCLAW_GATEWAY_PASSWORD}}const z=[`OpenClaw ${VERSION}`,`Runtime: ${M} (${I[0]})`,`Service command: ${I.map(quoteCliArg).join(` `)}`,`Update this node: ${formatCliCommand(`openclaw node install --force`,x).replace(`openclaw`,`npx -y openclaw@latest`)}`].join(`
`);if(!t)defaultRuntime.log(z);await installDaemonServiceAndEmit({serviceNoun:`Node`,service:v,warnings:r,emit:e=>a({...e,message:z}),fail:s,install:()=>installDurableServiceCli({env:x,replacement:F,verify:()=>v.isLoaded({env:process.env}),warn:i,install:(t,r)=>v.install({beforeMutation:t,beforeRecovery:r,runtimePinUpdate:{expected:S,pin:T?{runtime:E,path:T}:void 0,...e.expectedRuntimePin!==void 0?{requireDefinitionMatch:true,...!C&&S.definition!==void 0?{requireRunning:true}:{}}:{}},env:x,stdout:n,warn:i,programArguments:I,workingDirectory:L,environment:R,environmentValueSources:{OPENCLAW_GATEWAY_TOKEN:`file`,OPENCLAW_GATEWAY_PASSWORD:`file`,CF_ACCESS_CLIENT_ID:`file`,CF_ACCESS_CLIENT_SECRET:`file`},description:`OpenClaw Node Host`,...C?{workingDirectory:b?.workingDirectory}:{}})}),onVerified:()=>warnIfSystemdUserLingerDisabled(i)})}async function runNodeDaemonLifecycle(e,t={}){const n={serviceNoun:`Node`,service:resolveNodeService(),renderFailureHints:()=>[`Run ${formatCliCommand(`openclaw node status`)} to inspect the service, then retry.`],opts:t};if(e===`uninstall`)return await runServiceUninstall({...n,stopBeforeUninstall:false,assertNotLoadedAfterUninstall:false});if(e===`stop`)return await runServiceStop(n);await(e===`start`?runServiceStart:runServiceRestart)({...n,renderStartHints:renderNodeServiceStartHints})}async function runNodeDaemonStatus(e={}){const t=Boolean(e.json);const n=resolveNodeService();let r;try{r=await n.isLoaded({env:process.env})}catch(e){const n=`Node service check failed: ${formatErrorMessage(e)}`;if(t)throw new Error(n,{cause:e});defaultRuntime.error(n);defaultRuntime.exit(1);return}let i=true;const[a,o]=await Promise.all([n.readCommand(process.env).catch(()=>{i=false;return null}),n.readRuntime(process.env).catch(e=>({status:`unknown`,detail:formatErrorMessage(e)}))]);if(t){const e=await inspectServiceRuntimeIntent({kind:`node`,service:n,command:a,env:process.env,serviceEnv:{...process.env,...a?.environment},inspectionKnown:i});defaultRuntime.writeJson({service:projectDaemonServiceForJson({...buildDaemonServiceSnapshot(n,r),command:a,runtime:o,...e},{includeDefinitionPaths:true})});return}const{rich:s,label:c,accent:l,infoText:u,okText:d,warnText:f,errorText:p}=createCliStatusTextStyles();const m=r?d(n.loadedText):f(n.notLoadedText);defaultRuntime.log(`${c(`Service:`)} ${l(n.label)} (${m})`);if(a?.programArguments?.length)defaultRuntime.log(`${c(`Command:`)} ${u(a.programArguments.join(` `))}`);if(a?.sourcePath)defaultRuntime.log(`${c(`Service file:`)} ${u(a.sourcePath)}`);if(a?.workingDirectory)defaultRuntime.log(`${c(`Working dir:`)} ${u(a.workingDirectory)}`);const h=formatRuntimeStatus(o);if(h){const e=resolveRuntimeStatusColor(o?.status);defaultRuntime.log(`${c(`Runtime:`)} ${colorize(s,e,h)}`)}if(!r){defaultRuntime.log(``);for(const e of renderNodeServiceStartHints())defaultRuntime.log(`${f(`Start with:`)} ${u(e)}`);return}const g={...process.env,...a?.environment};const _={...g,OPENCLAW_LOG_PREFIX:g.OPENCLAW_LOG_PREFIX??`node`};if(o?.missingUnit||o?.status===`stopped`){defaultRuntime.error(p(o.missingUnit?`Service unit not found.`:`Service is loaded but not running.`));for(const e of buildPlatformRuntimeLogHints({env:_,systemdServiceName:resolveNodeSystemdServiceName(process.env.OPENCLAW_PROFILE),windowsTaskName:resolveNodeWindowsTaskName(process.env.OPENCLAW_PROFILE)}))defaultRuntime.log(p(e))}}export{runNodeDaemonLifecycle as n,runNodeDaemonStatus as r,runNodeDaemonInstall as t};