openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
3 lines • 2.74 kB
JavaScript
import{c as readFileDescriptorBoundedSync,o as openRootFileSync,r as isRootFileMissingFailure}from"./boundary-file-read-bB9An35K.mjs";import{t as pruneMapToMaxSize}from"./map-size-DLQqorvK.mjs";import{t as parseSkillFrontmatter}from"./frontmatter-CvGHA96F.mjs";import{t as materializeSkill}from"./skill-materializer-Csds0NPO.mjs";import fs from"node:fs";import path from"node:path";
//!src/skills/loading/local-loader.ts
const MAX_CACHED_SKILL_CONTENT_CHARS=16384;const MAX_CACHED_SKILL_CONTENTS=256;const localSkillContentCache=/* @__PURE__ */ new Map;function readSkillFileSync(e){const t=openRootFileSync({absolutePath:e.filePath,rootPath:e.rootRealPath,rootRealPath:e.rootRealPath,boundaryLabel:`skill root`,rejectSymlinks:false,rejectHardlinks:e.rejectHardlinks!==false});if(!t.ok){if(!isRootFileMissingFailure(t)){const n=t.error instanceof Error?t.error.message:`failed to open skill file (${t.reason})`;e.onDiagnostic?.({kind:t.reason===`validation`?`invalid`:`read`,path:e.filePath,message:n})}return null}try{return e.maxBytes===void 0?fs.readFileSync(t.fd,`utf8`):readFileDescriptorBoundedSync(t.fd,e.maxBytes).toString(`utf8`)}catch(t){const n=t instanceof Error?t.message:`failed to read skill file`;e.onDiagnostic?.({kind:t instanceof RangeError?`invalid`:`read`,path:e.filePath,message:n});return null}finally{fs.closeSync(t.fd)}}function loadSingleSkillDirectory(e){const t=path.join(e.skillDir,`SKILL.md`);const n=readSkillFileSync({rootRealPath:e.rootRealPath,filePath:t,maxBytes:e.maxBytes,rejectHardlinks:e.rejectHardlinks,onDiagnostic:e.onDiagnostic});if(n===null)return null;const r=path.basename(e.skillDir).trim();const i=path.resolve(t);const a=path.resolve(e.skillDir);let o=localSkillContentCache.get(n);if(o&&!o.frontmatter.name?.trim()&&o.skill.name!==r)o=void 0;if(!o){let s;try{s=parseSkillFrontmatter(n)}catch(n){const r=n instanceof Error?n.message:`failed to parse skill frontmatter`;e.onDiagnostic?.({kind:`invalid`,path:t,message:r});return null}const c=s.name?.trim()||r;const l=s.description?.trim();if(!c||!l){e.onDiagnostic?.({kind:`invalid`,path:t,message:!c?`name is required`:`description is required`});return null}o={skill:materializeSkill({content:n,frontmatter:s,name:c,description:l,filePath:i,baseDir:a,source:e.source,sourceOptions:{source:e.source,scope:`project`,origin:`top-level`}}),frontmatter:s}}if(n.length<=MAX_CACHED_SKILL_CONTENT_CHARS){localSkillContentCache.delete(n);localSkillContentCache.set(n,o);pruneMapToMaxSize(localSkillContentCache,MAX_CACHED_SKILL_CONTENTS)}return{skill:{...o.skill,filePath:i,baseDir:a,source:e.source,sourceInfo:{...o.skill.sourceInfo,path:i,baseDir:a,source:e.source}},frontmatter:{...o.frontmatter},content:n}}export{loadSingleSkillDirectory as t};