openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
3 lines • 12.7 kB
JavaScript
import{l as normalizeOptionalString}from"./string-coerce-CpTFcSwY.mjs";import{a as asOptionalRecord}from"./record-coerce-BTbqdJ3-.mjs";import{c as isSecretRef,p as isValidSecretRef}from"./ref-contract-CdpCUlb6.mjs";import{t as WorkerProviderError}from"./capability-provider.types-Bir_t_Rt.mjs";import"./string-coerce-runtime-BYDH9Quw.mjs";import"./secret-input-FyvkT2mt.mjs";import"./plugin-entry-DhKiQ4Uu.mjs";import{r as CRABBOX_HEARTBEAT_TIMEOUT_MS}from"./crabbox-worker-timeouts-C91C79jR.mjs";import path from"node:path";import{createHash}from"node:crypto";
//!extensions/crabbox/src/crabbox-worker-profile.ts
const PROFILE_KEYS=/* @__PURE__ */ new Set([`azureSandboxDiskId`,`binary`,`class`,`desktop`,`idleTimeout`,`provider`,`runSetupScript`,`setup`,`setupEnv`,`setupSecrets`,`ttl`,`target`,`warmImage`]);const GO_DURATION_PATTERN=/^\+?(?:(?:\d+(?:\.\d*)?|\.\d+)(?:ns|us|µs|μs|ms|s|m|h))+$/u;const GO_DURATION_TOKEN_PATTERN=/(\d+(?:\.\d*)?|\.\d+)(ns|us|µs|μs|ms|s|m|h)/gu;const MAX_GO_DURATION_NANOSECONDS=9223372036854775807n;const CRABBOX_LEASE_ID_DOMAIN=`openclaw:crabbox-worker-lease-id:v1\0`;const LEGACY_PROVISION_OPERATION_ID_PATTERN=/^provision:[a-f0-9]{64}$/u;const DURATION_UNIT_NANOSECONDS={h:3600000000000n,m:60000000000n,s:1000000000n,ms:1000000n,us:1000n,µs:1000n,μs:1000n,ns:1n};const MAX_CRABBOX_MACHINE_CLASS_LENGTH=128;const MAX_CRABBOX_MACHINE_OPTIONS=64;const CRABBOX_ENROLLABLE_TARGETS=[`linux`,`windows/wsl2`,`windows/normal`,`macos`];const CRABBOX_OS_LABELS={linux:`Linux`,"windows/wsl2":`Windows (WSL2)`,"windows/normal":`Windows`,macos:`macOS`};function parseCrabboxOperatingSystem(e){if(e===void 0)return`linux`;const t=normalizeOptionalString(e);const n=CRABBOX_ENROLLABLE_TARGETS.find(e=>e===t);if(n)return n;throw new WorkerProviderError(`Crabbox target must be ${CRABBOX_ENROLLABLE_TARGETS.join(` or `)}`)}const CRABBOX_DESKTOP_PROVIDERS=/* @__PURE__ */ new Set([`aws`,`azure`,`hetzner`]);const CRABBOX_WORKER_PROVIDER_ID=`crabbox`;function requirePositiveDuration(e,t){const n=normalizeOptionalString(e);const r=n?parsePositiveGoDurationNanoseconds(n):void 0;if(!n||r===void 0)throw new WorkerProviderError(`Crabbox profile ${t} must be a positive Go duration such as 60m`);return{duration:n,milliseconds:Number(r)/1e6}}function parseCrabboxDurationMs(e){const t=normalizeOptionalString(e);const n=t?parsePositiveGoDurationNanoseconds(t):void 0;return n===void 0?void 0:Number(n)/1e6}function parsePositiveGoDurationNanoseconds(e){if(!GO_DURATION_PATTERN.test(e))return;let t=0n;for(const n of e.matchAll(GO_DURATION_TOKEN_PATTERN)){const e=n[1];const r=n[2]?DURATION_UNIT_NANOSECONDS[n[2]]:void 0;if(!e||r===void 0)return;const[i=``,a=``]=e.split(`.`,2);const o=i.replace(/^0+/u,``)||`0`;if(o.length>19)return;t+=BigInt(o)*r;const s=a.slice(0,18);if(s)t+=BigInt(s)*r/10n**BigInt(s.length);if(t>MAX_GO_DURATION_NANOSECONDS)return}return t>0n?t:void 0}function heartbeatIntervalMs(e){const t=Math.max(5e3,Math.min(6e4,e/3));return Math.min(t,Math.max(1,Math.floor(e/2)))}function parseCrabboxProfile(e){for(const t of Object.keys(e))if(!PROFILE_KEYS.has(t))throw new WorkerProviderError(`unknown Crabbox profile setting: ${t}`);const t=normalizeOptionalString(e.provider)?.toLowerCase();const n=normalizeOptionalString(e.class);const r=parseCrabboxOperatingSystem(e.target);if(!t)throw new WorkerProviderError(`Crabbox profile provider must be a non-empty string`);if(e.class!==void 0&&!n)throw new WorkerProviderError(`Crabbox profile class must be a non-empty string`);const{duration:i}=requirePositiveDuration(e.ttl,`ttl`);const{duration:a,milliseconds:o}=requirePositiveDuration(e.idleTimeout,`idleTimeout`);const s=normalizeOptionalString(e.binary);if(e.binary!==void 0&&!s)throw new WorkerProviderError(`Crabbox profile binary must be a non-empty string`);if(s&&!path.isAbsolute(s))throw new WorkerProviderError(`Crabbox profile binary must be an absolute path`);const c=normalizeOptionalString(e.setup);if(e.setup!==void 0&&!c)throw new WorkerProviderError(`Crabbox profile setup must be a non-empty command string`);if(e.setupEnv!==void 0)throw new WorkerProviderError(`Crabbox profile setupEnv is retired; run openclaw doctor --fix to migrate protected, destination-bound store entries to setupSecrets. Repair any reported entries before provisioning; plaintext forwarding is unavailable.`);const l=parseCrabboxSetupSecrets(e.setupSecrets);const u=normalizeOptionalString(e.azureSandboxDiskId);if(e.azureSandboxDiskId!==void 0&&(t!==`azure-sandbox`||!u||u.length>256))throw new WorkerProviderError(`Crabbox Azure Sandbox disk ID requires an azure-sandbox profile and a non-empty string of at most 256 characters`);const d=e.desktop;if(d!==void 0&&typeof d!==`boolean`)throw new WorkerProviderError(`Crabbox profile desktop must be a boolean`);if(d&&!CRABBOX_DESKTOP_PROVIDERS.has(t))throw new WorkerProviderError(`Crabbox desktop profiles support only AWS, Azure, and coordinator-backed Hetzner`);const f=e.warmImage;if(f!==void 0&&typeof f!==`boolean`)throw new WorkerProviderError(`Crabbox profile warmImage must be a boolean`);return{...u?{azureSandboxDiskId:u}:{},binary:s,class:n,desktop:d,heartbeatIntervalMs:heartbeatIntervalMs(o),heartbeatTimeoutMs:Math.min(CRABBOX_HEARTBEAT_TIMEOUT_MS,Math.max(1,Math.floor(o/2))),idleTimeout:a,idleTimeoutMs:o,provider:t,setup:c,setupSecrets:l,ttl:i,target:r,warmImage:f}}function parseCrabboxCleanupProfile(e){const{setupEnv:t,...n}=e;const r=parseCrabboxProfile(n);if(t!==void 0)r.warmImage=false;return r}function isReservedCrabboxSetupSecretName(e){return[`CRABBOX_ENV_ALLOW`,`HOME`,`PATH`,`LANG`,`NODE_EXTRA_CA_CERTS`,`SSL_CERT_FILE`,`CURL_CA_BUNDLE`,`GIT_SSL_CAINFO`,`REQUESTS_CA_BUNDLE`,`NODE_USE_ENV_PROXY`,`CRABBOX_SETUP_PROXY`].includes(e)||/^(?:https?|all|no)_proxy$/iu.test(e)||e.startsWith(`OPENCLAW_`)}function parseCrabboxSetupSecrets(e){if(e===void 0)return;const t=asOptionalRecord(e);if(!t)throw new WorkerProviderError(`Crabbox profile setupSecrets must be a map of store SecretRefs`);if(Object.keys(t).length>16)throw new WorkerProviderError(`Crabbox profile setupSecrets must contain at most 16 names`);const n={};for(const[e,r]of Object.entries(t)){if(!/^[A-Za-z_][A-Za-z0-9_]*$/u.test(e))throw new WorkerProviderError(`Crabbox profile setupSecrets keys must be valid POSIX environment variable names`);if(isReservedCrabboxSetupSecretName(e))throw new WorkerProviderError(`Crabbox profile setupSecrets name ${e} is reserved`);if(!isSecretRef(r)||!isValidSecretRef(r)||r.source!==`store`)throw new WorkerProviderError(`Crabbox profile setupSecrets entry ${e} must use a store SecretRef { source: "store", provider, id }; inline values and other sources are unavailable`);n[e]={source:`store`,provider:r.provider,id:r.id}}return n}function resolveCrabboxWarmImageProfile(e,t=e.class,n=e.target){if(n===`windows/wsl2`&&e.desktop)throw new WorkerProviderError(`Crabbox WSL2 does not support desktops; select native Windows for a desktop viewer`);return{...e,class:t,target:n,warmImage:n===`linux`&&(e.warmImage??t!==void 0)}}function resolveCrabboxWarmImageProfileKey(e,t){return createHash(`sha256`).update(JSON.stringify({backendProvider:e.provider,...e.azureSandboxDiskId?{azureSandboxDiskId:e.azureSandboxDiskId}:{},...e.target!==`linux`?{target:e.target}:{},setup:e.setup??``,setupSecretMode:`protected-egress-v1`,setupSecrets:Object.entries(e.setupSecrets??{}).toSorted(([e],[t])=>e.localeCompare(t)),desktop:e.desktop??false,machineClass:e.class,...t?{projectKey:t}:{}})).digest(`hex`)}function resolveCrabboxPreparationTarget(e,t,n){const r=parseCrabboxProfile(e);const i=resolveCrabboxWarmImageProfile(r,t??r.class,n===void 0?r.target:parseCrabboxOperatingSystem(n));return supportsResolvedCrabboxProjectPreparation(i)&&i.class&&i.target===`linux`?{machineClass:i.class,platform:i.target,profileKey:resolveCrabboxWarmImageProfileKey(i)}:void 0}function supportsCrabboxProjectPreparation(e,t,n){const r=parseCrabboxProfile(e);return supportsResolvedCrabboxProjectPreparation(resolveCrabboxWarmImageProfile(r,t??r.class,n===void 0?r.target:parseCrabboxOperatingSystem(n)))}function supportsResolvedCrabboxProjectPreparation(e){return Boolean(e.warmImage)||Object.keys(e.setupSecrets??{}).length>0||Boolean(e.provider===`azure-sandbox`&&e.azureSandboxDiskId&&e.target===`linux`&&e.class)}function resolveCrabboxPreparedIdleTimeoutMs(e){const t=parseCrabboxProfile(e);return t.warmImage===false&&!t.azureSandboxDiskId||t.target!==`linux`?void 0:t.idleTimeoutMs}function isCrabboxSetupTargetCompatible(e,t){return!e.setup||e.target===`windows/normal`===(t===`windows/normal`)}function resolveCrabboxProvisionProfile(e,t,n){const r=parseCrabboxProfile(e);const i=normalizeOptionalString(t);if(t!==void 0&&(!i||i.length>MAX_CRABBOX_MACHINE_CLASS_LENGTH))throw new WorkerProviderError(`Crabbox machine class must be a non-empty string of at most 128 characters`);const a=resolveCrabboxWarmImageProfile(r,i??r.class,n===void 0?r.target:parseCrabboxOperatingSystem(n));if(!isCrabboxSetupTargetCompatible(r,a.target)){const e=a.target===`windows/normal`?`PowerShell`:`POSIX`;throw new WorkerProviderError(`Crabbox profile setup for ${r.target} cannot run on ${a.target}; select a profile with settings.target=${a.target} and a compatible ${e} setup command`)}let o;if(!a.warmImage)o={...a,warmImage:false};else{if(!a.class)throw new WorkerProviderError(`Crabbox warmImage requires a configured class or a placement machine class`);o={...a,class:a.class,warmImage:true}}return{profile:o}}function listCrabboxMachineOptions(e,t=[]){const n=/* @__PURE__ */ new Set;const r=[];for(const i of CRABBOX_ENROLLABLE_TARGETS){const a=t.filter(e=>{const t=`${e.os}:${e.class}`;if(e.os!==i||e.class.length>MAX_CRABBOX_MACHINE_CLASS_LENGTH||n.has(t))return false;n.add(t);return true});if(a.length===0)continue;const o=MAX_CRABBOX_MACHINE_OPTIONS-r.length;const s=e!==void 0&&!a.slice(0,o).some(t=>t.class===e);for(const t of a.slice(0,Math.max(0,o-Number(s))))r.push({id:t.class,os:i,label:t.class.replace(/^./u,e=>e.toUpperCase()),...t.cpu!==void 0?{cpu:t.cpu}:{},...t.memoryGb!==void 0?{memoryGb:t.memoryGb}:{},...t.class===e?{default:true}:{}});if(s&&o>0&&e!==void 0)r.push({id:e,os:i,label:e,default:true})}return r}function buildCrabboxAllocationArgs(e,t,n,r){const i=[`--provider`,e.provider,...r?[`--azure-sandbox-retention`,`session-disk`,`--azure-sandbox-owner-binding`,r.ownerBinding]:[],...e.azureSandboxDiskId?[`--azure-sandbox-disk-id`,e.azureSandboxDiskId]:[],`--network`,`public`,`--tailscale=false`,...e.class?[`--class`,e.class]:[],...e.target===`linux`?[`--target`,`linux`]:[],...e.target===`windows/wsl2`?[`--target`,`windows`,`--windows-mode`,`wsl2`]:[],...e.target===`windows/normal`?[`--target`,`windows`,`--windows-mode`,`normal`]:[],...e.target===`macos`?[`--target`,`macos`,`--market`,`on-demand`]:[],`--ttl`,r?`${r.computeWindowSeconds}s`:e.ttl,`--idle-timeout`,e.idleTimeout,`--lease-id`,t,`--slug`,n,`--keep=true`];if(e.desktop){i.push(`--desktop`);if(e.target===`linux`)i.push(`--browser`,`--desktop-env`,`xfce`)}return i}function resolveOpenClawRoot(e){if(!e)return process.cwd();const t=path.dirname(e);if(path.basename(t)!==`extensions`)return process.cwd();const n=path.dirname(t);return path.basename(n)===`dist`||path.basename(n)===`dist-runtime`?path.dirname(n):n}function operationSlug(e){return`openclaw-${createHash(`sha256`).update(e).digest(`hex`).slice(0,32)}`}const LEASE_ID_PATTERN=/^(?:cbx_|tbx_)[A-Za-z0-9][A-Za-z0-9_-]{0,127}$/u;function assertCrabboxLeaseId(e){if(!LEASE_ID_PATTERN.test(e))throw new Error(`Crabbox lease id is invalid`)}function operationLeaseId(e){if(!e.trim())throw new Error(`Crabbox provision requires an operation id`);if(LEGACY_PROVISION_OPERATION_ID_PATTERN.test(e))throw new Error(`Legacy Crabbox provision state cannot be replayed safely; clean up any prior lease and dispatch again`);return`cbx_${createHash(`sha256`).update(CRABBOX_LEASE_ID_DOMAIN).update(e).digest(`hex`).slice(0,12)}`}export{supportsCrabboxProjectPreparation as S,resolveCrabboxPreparedIdleTimeoutMs as _,buildCrabboxAllocationArgs as a,resolveCrabboxWarmImageProfileKey as b,isReservedCrabboxSetupSecretName as c,operationSlug as d,parseCrabboxCleanupProfile as f,resolveCrabboxPreparationTarget as g,parseCrabboxSetupSecrets as h,assertCrabboxLeaseId as i,listCrabboxMachineOptions as l,parseCrabboxProfile as m,CRABBOX_OS_LABELS as n,heartbeatIntervalMs as o,parseCrabboxDurationMs as p,CRABBOX_WORKER_PROVIDER_ID as r,isCrabboxSetupTargetCompatible as s,CRABBOX_ENROLLABLE_TARGETS as t,operationLeaseId as u,resolveCrabboxProvisionProfile as v,resolveOpenClawRoot as x,resolveCrabboxWarmImageProfile as y};