openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
3 lines • 3.32 kB
JavaScript
import{r as resolvePreferredOpenClawTmpDir}from"./tmp-openclaw-dir-DapBruir.mjs";import{t as SkillLibraryError}from"./skill-library-error-CRoBQyXe.mjs";import{r as SKILL_LIBRARY_MAX_FILE_BYTES,t as SKILL_LIBRARY_MAX_BUNDLE_BYTES}from"./skill-library-QNfZhm1-.mjs";import{a as prepareSkillLibraryBundle,l as readSkillLibraryTree,r as decodeSkillLibraryFile,t as SKILL_LIBRARY_MAX_TREE_ENTRIES}from"./bundle-CzmF1asv.mjs";import{t as captureSkillLibraryAccess}from"./store-access-B9gW2MOW.mjs";import{n as withExtractedArchiveRoot}from"./install-flow-wXl8VgRG.mjs";import{t as installSkillFromClawHub}from"./clawhub-B8Bo_teO.mjs";import{t as skillLibraryReceipt}from"./receipt-BQe095X1.mjs";import{a as saveSkillLibrary}from"./service-B7cl4s-z.mjs";import path from"node:path";import{createHash}from"node:crypto";import fs from"node:fs/promises";import{withTempWorkspace}from"@openclaw/fs-safe/temp";
//!src/skills/library/import.ts
async function publishDirectory(e,t,n,r,i){const a=await readSkillLibraryTree(n);prepareSkillLibraryBundle(a);const o=a.find(e=>e.path===`SKILL.md`);return saveSkillLibrary(e,{slug:t,expectedRevision:null,content:decodeSkillLibraryFile(o).toString(`utf8`),files:a.filter(e=>e!==o&&!/^\.(?:clawhub|clawdhub)\//u.test(e.path))},r,i)}async function importSkillLibrary(e,t,n={}){const r=captureSkillLibraryAccess(e,n);await r.read(`profile`,void 0);return withTempWorkspace({rootDir:resolvePreferredOpenClawTmpDir(),prefix:`openclaw-library-source-`},async({dir:n})=>{const i=await installSkillFromClawHub({workspaceDir:n,slug:t.source.slug,version:t.source.version,config:e.getConfig()});r.assertCurrent();if(!i.ok)throw new SkillLibraryError(`POLICY_BLOCKED`,i.error);return publishDirectory({...e,assertCurrent:r.assertCurrent},t.slug,i.targetDir,r.options)})}async function uploadSkillLibrary(e,t,n={}){const r=captureSkillLibraryAccess(e,n);if(t.action!==`commit`)return r.write(`skillLibrary.upload`,{params:t});const i=await r.read(`upload`,{uploadId:t.uploadId});const a=i.value;if(a.published_skill_id)return skillLibraryReceipt((await r.read(`entry`,{skillId:a.published_skill_id})).value,`unchanged`);const o=Buffer.from(a.archive_blob);if(o.length!==a.size_bytes||createHash(`sha256`).update(o).digest(`hex`)!==a.sha256)throw new SkillLibraryError(`INVALID_BUNDLE`,`Upload is incomplete or its SHA-256 does not match.`);return withTempWorkspace({rootDir:resolvePreferredOpenClawTmpDir(),prefix:`openclaw-library-import-`},async({dir:t})=>{const n=path.join(t,`skill.zip`);e.assertFileMutationAllowed?.();await fs.writeFile(n,o,{mode:384,flag:`wx`});const s=await withExtractedArchiveRoot({archivePath:n,tempDirPrefix:`openclaw-library-extract-`,timeoutMs:12e4,rootMarkers:[`SKILL.md`],limits:{maxArchiveBytes:SKILL_LIBRARY_MAX_BUNDLE_BYTES,maxExtractedBytes:SKILL_LIBRARY_MAX_BUNDLE_BYTES,maxEntryBytes:SKILL_LIBRARY_MAX_FILE_BYTES,maxEntries:SKILL_LIBRARY_MAX_TREE_ENTRIES+1,maxEntryPathComponents:16+1},onExtracted:async t=>({ok:true,receipt:await publishDirectory({...e,assertCurrent:()=>{i.assertCurrent();if(a.expires_at<=Date.now())throw new SkillLibraryError(`NOT_FOUND`,`Upload expired. Start a new import.`)}},a.slug,t,r.options,a.upload_id)})});if(!s.ok)throw new SkillLibraryError(`INVALID_BUNDLE`,s.error);return s.receipt})}export{importSkillLibrary,uploadSkillLibrary};