eve
Version:
Filesystem-first framework for durable backend AI agents that run anywhere.
3 lines • 3.47 kB
JavaScript
import{isObject}from"#shared/guards.js";import{defineSkill}from"#public/definitions/skill.js";import{normalizeInstructionsDefinition,normalizeScheduleDefinition,normalizeSkillDefinition}from"#internal/authored-definition/core.js";import{hasFrontmatter,parseFrontmatter}from"#internal/helpers/gray-matter.js";import{defineSchedule}from"#public/definitions/schedule.js";const CLOSED_FRONTMATTER_PATTERN=/^---\r?\n[\s\S]*?\r?\n---(?:\r?\n|$)/;function parseMarkdownDocument(e){if(!hasFrontmatter(e))return{hasFrontmatter:!1,frontmatter:{},markdown:e};let t;try{t=parseFrontmatter(e)}catch(t){throw startsWithFrontmatterFence(e)&&!hasClosedFrontmatterFence(e)?Error(`Markdown frontmatter is missing a closing delimiter.`):t}if(!isObject(t.data))throw Error(`Markdown frontmatter must parse to an object.`);return{hasFrontmatter:!0,frontmatter:t.data,markdown:normalizeFrontmatterMarkdownBody(t.content)}}function lowerInstructionsMarkdown(e){return normalizeInstructionsDefinition({content:e,role:`system`},`Expected authored instructions markdown to match the public eve shape.`)}function lowerScheduleMarkdown(e){let t=parseMarkdownDocument(e);if(!t.hasFrontmatter)throw Error(`Schedule markdown must start with YAML frontmatter declaring "cron".`);if(`run`in t.frontmatter)throw Error('Markdown-form schedules do not support the "run" frontmatter key. Use a TypeScript schedule (`<name>.ts`) to author a handler.');let n={...t.frontmatter,markdown:t.markdown};return defineSchedule(normalizeScheduleDefinition(n,`Expected authored schedule markdown to match the public eve shape.`))}function lowerSkillMarkdown(e,t={}){let n=parseMarkdownDocument(e),r=t.slug;if(r===void 0&&!n.hasFrontmatter)throw Error(`Skill markdown must start with YAML frontmatter.`);let i=n.frontmatter,a=toOptionalString(i.description,`description`),o={description:r===void 0?requireStringFrontmatter(i.description,`description`):a??t.description??deriveFlatSkillDescription(n.markdown,r),markdown:n.markdown};return applyOptionalSkillFrontmatter(o,i),defineSkill(normalizeSkillDefinition(o,`Expected authored skill markdown to match the public eve shape.`))}function startsWithFrontmatterFence(e){return e.startsWith(`---
`)||e.startsWith(`---\r
`)}function hasClosedFrontmatterFence(e){return CLOSED_FRONTMATTER_PATTERN.test(e)}function normalizeFrontmatterMarkdownBody(e){return e.replace(/^\r?\n/u,``)}function applyOptionalSkillFrontmatter(e,t){let n=toOptionalString(t.license,`license`);n!==void 0&&(e.license=n);let r=toOptionalStringRecord(t.metadata,`metadata`);r!==void 0&&(e.metadata=r)}function toOptionalString(e,t){if(e!=null){if(typeof e!=`string`)throw Error(`Expected "${t}" frontmatter to be a string.`);return e}}function requireStringFrontmatter(e,t){let n=toOptionalString(e,t);if(n===void 0)throw Error(`Missing required "${t}" frontmatter.`);return n}function toOptionalStringRecord(e,t){if(e==null)return;if(!isObject(e))throw Error(`Expected "${t}" frontmatter to be an object.`);let n=Object.entries(e).map(([e,n])=>{if(typeof n!=`string`)throw Error(`Expected "${t}.${e}" frontmatter to be a string.`);return[e,n]});return Object.fromEntries(n)}function deriveFlatSkillDescription(e,t){let n=e.split(/\r?\n/u).map(e=>e.trim()).find(e=>e!==``&&!e.startsWith("```"));return n===void 0?`Instructions for the ${t} skill.`:n.replace(/^[#>*\-\s]+/u,``).trim()||`Instructions for the ${t} skill.`}export{lowerInstructionsMarkdown,lowerScheduleMarkdown,lowerSkillMarkdown};