UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

23 lines • 4.55 kB
import{D as parseStrictPositiveInteger}from"./number-coercion-B3CfI447.mjs";import{n as parseModelRef}from"./model-selection-normalize-DU37Sfvg.mjs";import{t as shellEscape}from"./shell-quote-BethZJ3T.mjs";import{n as resolveCrabboxBinary}from"./crabbox-binary-Bo1EgOsf.mjs";import"./number-runtime-G0nhUVZu.mjs";import"./model-ref-parse-2lRfktql.mjs";import{t as withConfiguredModelEgress}from"./secret-egress-runtime-DQILxEJh.mjs";import{h as runCrabboxProcess}from"./crabbox-worker-command-DPZFykVB.mjs";import{r as ensureManagedCrabboxBinary}from"./crabbox-managed-binary-D_OJtHhk.mjs";import{randomBytes}from"node:crypto"; //!extensions/crabbox/src/crabbox-model-run-cli.ts const UPSTREAM_PROXY_ENV=`CRABBOX_MODEL_PROXY`;const MAX_OUTPUT_BYTES=1048576;function shellQuote(e){if(e.includes(`\0`))throw new Error(`Crabbox model command cannot contain NUL bytes`);return shellEscape(e)}function remoteCommandScript(e){const t=`MODEL_EGRESS_CA_${randomBytes(12).toString(`hex`)}`;return`#!/bin/bash set -euo pipefail umask 077 egress_dir=$(mktemp -d) trap 'rm -rf -- "$egress_dir"' EXIT cat > "$egress_dir/ca.pem" <<'${t}' ${e.caBundle} ${t} export OPENAI_API_KEY=${shellQuote(e.sentinel)} export OPENAI_BASE_URL=${shellQuote(e.baseUrl)} export OPENAI_MODEL=${shellQuote(e.model)} export HTTPS_PROXY=http://127.0.0.1:3128 HTTP_PROXY=http://127.0.0.1:3128 export https_proxy="$HTTPS_PROXY" http_proxy="$HTTP_PROXY" export NO_PROXY=localhost,127.0.0.1,::1 no_proxy=localhost,127.0.0.1,::1 unset ALL_PROXY all_proxy export NODE_USE_ENV_PROXY=1 export NODE_EXTRA_CA_CERTS="$egress_dir/ca.pem" SSL_CERT_FILE="$egress_dir/ca.pem" export CURL_CA_BUNDLE="$egress_dir/ca.pem" REQUESTS_CA_BUNDLE="$egress_dir/ca.pem" export GIT_SSL_CAINFO="$egress_dir/ca.pem" "$@" `}function registerCrabboxModelRunCommand({program:e,config:t}){const n=e.commands.find(e=>e.name()===`crabbox`)??e.command(`crabbox`);n.description(`Run model-backed commands and manage Crabbox warm images`);n.command(`run`).description(`Run a foreground command on an exclusive Linux lease using a protected model credential`).requiredOption(`--id <lease>`,`Existing, exclusively owned Crabbox lease`).requiredOption(`--model <provider/model>`,`Configured OpenAI-compatible API-key model`).option(`--provider <provider>`,`Crabbox backend override; otherwise use the lease's provider`).option(`--binary <path>`,`Preferred Crabbox binary (managed release used if outdated)`).option(`--timeout <seconds>`,`Total command and setup deadline in seconds`,`600`).argument(`<command...>`,`Remote executable and arguments, following --`).action(async(e,n)=>{const r=parseStrictPositiveInteger(n.timeout);if(r===void 0||r>86400)throw new Error(`--timeout must be an integer between 1 and 86400 seconds`);const i=process.cwd();const a=new AbortController;const cancel=()=>a.abort(/* @__PURE__ */ new Error(`Crabbox model command interrupted`));process.once(`SIGINT`,cancel);process.once(`SIGTERM`,cancel);try{const o=resolveCrabboxBinary({explicit:n.binary,pathEnv:process.env.PATH});const{id:s,model:c,provider:l}=n;const u=r*1e3;const d=parseModelRef(c,``);if(!c.includes(`/`)||!d?.provider||!d.model)throw new Error(`--model must name an explicit provider/model`);const f=AbortSignal.any([AbortSignal.timeout(u),a.signal]);const{binary:p}=await ensureManagedCrabboxBinary({binary:o,cwd:i,signal:f});const command=(e,t={})=>runCrabboxProcess([p,`egress`,`run`,...e],{cwd:i,timeoutMs:u,maxOutputBytes:MAX_OUTPUT_BYTES,killProcessTree:true,requireProcessTreeExtinction:true,killGraceMs:75e3,signal:f,...t,env:{CRABBOX_ENV_ALLOW:`,`,[UPSTREAM_PROXY_ENV]:void 0,...t.env}});const m=await command([`--help`]);if(m.termination!==`exit`||m.code!==0||!/(?:^|\n)\s+-{1,2}upstream-proxy-env(?:\s|$)/u.test(`${m.stdout}\n${m.stderr}`))throw new Error(`This Crabbox binary lacks native egress run support; update Crabbox first`);const h=await withConfiguredModelEgress({config:t,...d,signal:f,onOutput:(e,t)=>{process[t].write(e)}},async t=>{const n=await command([`--id`,s,...l?[`--provider`,l]:[],`--allow`,t.allowedHosts.join(`,`),`--upstream-proxy-env`,UPSTREAM_PROXY_ENV,`--no-sync`,`--no-hydrate`,`--script-stdin`,`--`,...e],{env:{[UPSTREAM_PROXY_ENV]:t.hostEnv.HTTPS_PROXY},input:remoteCommandScript(t),onOutputChunk:t.onOutputChunk});if(n.cleanup===`uncertain`)throw new Error(`Crabbox could not confirm that the command stopped`);return n});process.exitCode=h.termination===`exit`?h.code??1:1}finally{process.off(`SIGINT`,cancel);process.off(`SIGTERM`,cancel)}})}export{registerCrabboxModelRunCommand};