eve
Version:
Filesystem-first framework for durable backend AI agents that run anywhere.
1 lines • 1.89 kB
JavaScript
import{resolveSandboxSkillReadPaths}from"#shared/skill-paths.js";const FRONTMATTER_PATTERN=/^---\r?\n[\s\S]*?\r?\n---\r?\n?/;function assertSafeSkillId(e){if(e.length===0||e.trim()!==e||e.startsWith(`.`)||e.includes(`/`)||e.includes(`\\`)||e.includes(`..`)||/^[A-Za-z]:/.test(e))throw Error(`Expected skill id to be a non-empty safe path segment without whitespace, separators, "." prefix, or "..".`)}async function loadSkillFromSandbox(e,t,n=[]){assertSafeSkillId(t);let r=await requireSandboxSession(e),i=await resolveSandboxSkillReadPaths({name:t,relativePath:`SKILL.md`,sandbox:r});for(let e of i){let t=await r.readTextFile({path:e});if(t!==null)return t.replace(FRONTMATTER_PATTERN,``)}let a=n.length>0?` Available skills: ${n.join(`, `)}.`:``;throw Error(`No skill named "${t}" at ${i[0]}.${a}`)}function createSandboxSkillHandle(e,t){return assertSafeSkillId(t),{name:t,file(n){return assertSafeSkillRelativePath(n),{async bytes(){let r=await requireSandboxSession(e),i=await resolveSandboxSkillReadPaths({name:t,relativePath:n,sandbox:r});for(let e of i){let t=await r.readBinaryFile({path:e});if(t!==null)return t}throw Error(`Skill file not found: ${i[0]}`)},async text(){let r=await requireSandboxSession(e),i=await resolveSandboxSkillReadPaths({name:t,relativePath:n,sandbox:r});for(let e of i){let t=await r.readTextFile({path:e});if(t!==null)return t}throw Error(`Skill file not found: ${i[0]}`)}}}}}function assertSafeSkillRelativePath(e){if(e.length===0||e.startsWith(`/`)||e.includes(`\\`)||e.split(`/`).some(e=>e.length===0||e===`.`||e===`..`))throw Error(`Expected skill file path to be a relative path inside the skill directory.`)}async function requireSandboxSession(e){let t=await e.get();if(t===null)throw Error(`The sandbox is not available in the current authored runtime context.`);return t}export{assertSafeSkillId,createSandboxSkillHandle,loadSkillFromSandbox};