openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
3 lines • 4.22 kB
JavaScript
import{l as normalizeOptionalString}from"./string-coerce-CpTFcSwY.mjs";import{l as isRecord}from"./record-coerce-BTbqdJ3-.mjs";import{t as formatErrorMessage}from"./errors-D37BJTfI.mjs";import{i as ErrorCodes}from"./missing-scope-error-CkgBgh6d.mjs";import{p as errorShape}from"./error-codes-CQNGhXT-.mjs";import"./string-coerce-runtime-BYDH9Quw.mjs";import"./error-runtime-DW-sj0iq.mjs";import"./gateway-runtime-CCd3DVdi.mjs";import{m as parseCrabboxProfile,r as CRABBOX_WORKER_PROVIDER_ID,y as resolveCrabboxWarmImageProfile}from"./crabbox-worker-profile-5DxAeX_h.mjs";import{l as listCrabboxLegacyWarmLeases,p as recoverCrabboxWarmImageCapture,t as CrabboxWarmImageRequestError,u as listCrabboxWarmImages}from"./crabbox-worker-warm-image-store-Bkmwt6Qm.mjs";import{a as crabboxWarmImageRecoveryHint,o as isCrabboxWarmImageHeld}from"./crabbox-worker-warm-image-records-B8HI4d8j.mjs";
//!extensions/crabbox/src/crabbox-gateway-methods.ts
async function mutateCrabboxImage(e,t,n,{params:r,respond:i}){const a=isRecord(r)?normalizeOptionalString(r.checkpointId):void 0;if(!a||!isRecord(r)||n===`pin`&&typeof r.pinned!==`boolean`||Object.keys(r).some(e=>e!==`checkpointId`&&!(n===`pin`&&e===`pinned`))){sendError(i,/* @__PURE__ */ new Error(`crabbox.images.${n} requires checkpointId${n===`pin`?` and pinned: boolean`:``}.`),true);return}try{if(n===`pin`)i(true,snapshotSummary(await t.pin(a,r.pinned===true)));else if(n===`rollback`)i(true,snapshotSummary(await t.rollback(a)));else{const n=Object.values(e.runtime.config.current().cloudWorkers?.profiles??{}).filter(e=>e.provider.trim().toLowerCase()===CRABBOX_WORKER_PROVIDER_ID).map(e=>e.settings??{});i(true,await t.delete(a,n))}}catch(e){sendError(i,e,e instanceof CrabboxWarmImageRequestError)}}function sendError(e,t,n=false){const r={error:formatErrorMessage(t)};e(false,r,errorShape(n?ErrorCodes.INVALID_REQUEST:ErrorCodes.UNAVAILABLE,r.error,{details:r}))}function profileStatus(e){try{const t=parseCrabboxProfile(e);const n=resolveCrabboxWarmImageProfile(t);const r=n.warmImage&&n.class?`on`:`off`;const i=t.warmImage===false?`Disabled in this profile.`:n.target!==`linux`?`Warm images require Linux.`:!n.class?`Requires a configured or placement machine class.`:t.warmImage===true?`Explicitly enabled in this profile.`:`Enabled by default for Linux with a known machine class.`;return{backend:t.provider,machineClass:t.class,os:t.target,warmImages:r,reason:i}}catch(e){return{warmImages:`off`,reason:formatErrorMessage(e)}}}function snapshotSummary(e){const t=Object.entries(e.allocations).toSorted(([e],[t])=>e.localeCompare(t));return{...e,held:Boolean(e.checkpointId&&isCrabboxWarmImageHeld(e,e.checkpointId)),allocationCount:t.length,allocations:Object.fromEntries(t.slice(0,20))}}async function snapshotStatus(e){return{images:(await listCrabboxWarmImages(e)).map(snapshotSummary),legacyLeases:(await listCrabboxLegacyWarmLeases(e)).map(e=>Object.assign(e,{recoveryHint:crabboxWarmImageRecoveryHint(e.selector)}))}}async function listCrabboxImages(e,{params:t,respond:n}){if(t!==void 0&&(!isRecord(t)||Object.keys(t).length>0)){sendError(n,/* @__PURE__ */ new Error(`crabbox.images.list takes no parameters.`),true);return}try{const t=Object.entries(e.runtime.config.current().cloudWorkers?.profiles??{}).filter(([,e])=>e.provider.trim().toLowerCase()===CRABBOX_WORKER_PROVIDER_ID).toSorted(([e],[t])=>e.localeCompare(t)).map(([e,t])=>Object.assign({id:e},profileStatus(t.settings??{})));n(true,{...await snapshotStatus(e.runtime.state),profiles:t})}catch(e){sendError(n,e)}}async function recoverCrabboxImage(e,{params:t,respond:n}){const r=isRecord(t)?normalizeOptionalString(t.selector):void 0;if(!r||!isRecord(t)||t.acknowledgeProviderCleanup!==true||Object.keys(t).some(e=>e!==`selector`&&e!==`acknowledgeProviderCleanup`)){sendError(n,/* @__PURE__ */ new Error(`Recovery requires a selector and acknowledgeProviderCleanup: true.`),true);return}try{await recoverCrabboxWarmImageCapture(e,r,true);n(true,{...await snapshotStatus(e),recoveredCapture:r,nextSteps:`Restart the Gateway after manual reconciliation; the next eligible worker can capture again.`})}catch(e){sendError(n,e)}}export{listCrabboxImages,mutateCrabboxImage,recoverCrabboxImage};