@sema-agent/core
Version:
Stateless, task-oriented AI agent core
79 lines (78 loc) • 5.33 kB
TypeScript
/**
* RB-410 件1 — the Agent Skills (`SKILL.md`) directory loader.
*
* The open skills format (published at agentskills.io, reference implementation `anthropics/skills`) is
* a directory per skill: `skill-name/SKILL.md` holding a `---` frontmatter block plus a Markdown body,
* with optional `scripts/` / `references/` / `assets/` resource directories beside it. sema's
* {@link SkillSpec} is already the same shape — frontmatter → `name`/`description`, body → `content`,
* sibling resources → `files[]` — so the only missing piece was this reader. No engine mechanism
* changes: the output is an ordinary `SkillSpec[]` a deployment hands to `TaskSpec.skills`.
*
* **Deployment-half helper, called BEFORE a task starts.** It reads the filesystem synchronously and is
* NOT reachable from the run loop — the engine still never resolves a skill path at tool time, and never
* writes. An embedder serving skills from a remote/sandboxed filesystem should read through its own
* `ExecutionEnv` instead and construct `SkillSpec` objects directly; this helper is the local-disk case.
*
* **Trust contract is unchanged and is the caller's:** a skill IS instructions, so everything read here
* (body and attachments alike) enters the model's context as CALLER-TRUSTED material. Pointing this at a
* directory third parties can write to is the same as pasting their text into the system prompt. The
* loader validates SHAPE (spec conformance), never intent.
*
* **One reader of a `SKILL.md` text.** Each directory's file is read by `readSkillFile` (`@sema-agent/primitives`), the
* function an editor or a browser asks too: what it reads (the head, the tool lists copied as written, the body), what it
* skips and what it refuses, and the sentence each warning carries, are that reader's. This module adds what only a disk has:
* listing the root, following and containing links, the attachments.
*/
import { type SkillFileRefusalReason } from "@sema-agent/primitives";
import type { SkillSpec } from "./types.js";
/** Why one skill directory (or one of its parts) was not taken as-declared. */
export type SkillsDirectoryWarningCode =
/** The `SKILL.md` was not read as a skill — the skill file reader's reason (`SKILL_FILE_REFUSAL_REASONS`: `no_frontmatter`,
* `missing_name`, `invalid_name`, `name_mismatch`, `missing_description`, `description_too_long`). */
SkillFileRefusalReason
/** The directory holds no `SKILL.md`. */
| "no_skill_file"
/** An attachment was left out (non-text bytes, over budget, unreadable, or resolving outside the skill). */
| "attachment_skipped"
/** A file or directory entry existed but could not be read/resolved. */
| "read_failed"
/** A skill directory or its `SKILL.md` resolves outside {@link SkillsDirectoryOptions.containWithin} — skipped. */
| "outside_root";
/** One non-fatal diagnostic from a load. Safe to log: no file contents, only names/reasons. */
export interface SkillsDirectoryWarning {
code: SkillsDirectoryWarningCode;
/** Directory name of the skill this concerns, relative to the scanned root. */
skill: string;
/** Human-readable detail (names the offending field/file and what was done about it). */
detail: string;
}
export interface SkillsDirectoryOptions {
/** Sink for non-fatal diagnostics. Absent ⇒ warnings are dropped and the load still proceeds. */
onWarning?: (warning: SkillsDirectoryWarning) => void;
/**
* Total decoded-text budget for ONE skill's attachments, in bytes (default 256 KiB). Files are taken in
* path order until the budget is spent; the rest are skipped with a disclosed warning. Attachments also
* ride a separate delivery cap at invoke time, so a generous value here does not enlarge the prompt.
*/
maxAttachmentBytes?: number;
/**
* The directory this root's skills must physically lie inside. Declared ⇒ every skill directory, and every
* `SKILL.md`, resolves (links followed) inside it or is skipped with an `outside_root` warning; the attachments
* already lie inside their skill directory, so they are inside it too. Absent ⇒ each root entry is a mount the
* deployment declared, trusted like the root itself. A root a third party writes (a repository's skills
* directory) declares the repository root here, so a link in it cannot load a skill from elsewhere on disk.
* Must name an existing directory; anything else throws, like an unreadable root.
*/
containWithin?: string;
}
/**
* Load every `SKILL.md`-bearing subdirectory of `dir` into a {@link SkillSpec}, ready for
* `TaskSpec.skills`. Results are ordered by directory name (deterministic across filesystems).
*
* Per-skill problems are NON-FATAL: a directory whose frontmatter breaks the spec (missing/invalid
* `name`, missing/over-long `description`, no fenced block, no `SKILL.md`) is SKIPPED and reported
* through {@link SkillsDirectoryOptions.onWarning}, so one malformed skill cannot take down a whole
* library. The root itself is different — an unreadable/non-directory root THROWS, because silently
* returning `[]` for a mistyped path would look exactly like "this deployment has no skills".
*/
export declare function createSkillsFromDirectory(dir: string, options?: SkillsDirectoryOptions): SkillSpec[];