sailfork / t3code-github / packages/ provider-fx/ src/ server/ skills.ts

/**
 * Discovers fx skills for the `

	
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
	packages/provider-fx/src/server/skills.ts at main · sailfork/t3code-github · Sailfork
	
		
picker by scanning the SKILL.md folders fx * reads (fx 0.0.13, src/builtins/skills.zig), so no `fx acp` session starts * for a composer menu. * * @module provider-fx/server/skills */ import type { ServerProviderSkill } from "@t3tools/contracts"; import * as Effect from "effect/Effect"; import * as FileSystem from "effect/FileSystem"; import * as Option from "effect/Option"; import * as Path from "effect/Path"; import type * as PlatformError from "effect/PlatformError"; import * as Stream from "effect/Stream"; /** * fx's menu order: managed `~/.fx/skills`, then the workspace's `.fx/skills` * and `skills/`, then every compatibility root (`skillGroupRank` in * src/core/skills/skill_runtime.zig). Symlinked skill folders are followed * everywhere except the managed root. */ type SkillRoot = { readonly relative: string; readonly rank: 0 | 1 | 2; readonly followsLinks?: false; }; const FX_WORKSPACE_ROOTS: ReadonlyArray<SkillRoot> = [ { relative: ".fx/skills", rank: 1 }, { relative: "skills", rank: 1 }, { relative: ".opencode/skills", rank: 2 }, { relative: ".codex/skills", rank: 2 }, { relative: ".claude/skills", rank: 2 }, { relative: ".agents/skills", rank: 2 }, { relative: ".claw/skills", rank: 2 }, ]; const FX_HOME_ROOTS: ReadonlyArray<SkillRoot> = [ { relative: ".fx/skills", rank: 0, followsLinks: false }, { relative: ".config/opencode/skills", rank: 2 }, { relative: ".codex/skills", rank: 2 }, { relative: ".claude/skills", rank: 2 }, { relative: ".agents/skills", rank: 2 }, { relative: ".claw/skills", rank: 2 }, ]; const MAX_FRONTMATTER_BYTES = 64 * 1024; const MAX_NAME_BYTES = 256; const BLOCK_DESCRIPTION_STYLES = new Set([">", ">-", "|"]); const encoder = new TextEncoder(); const strictDecoder = new TextDecoder("utf-8", { fatal: true, ignoreBOM: true }); const lenientDecoder = new TextDecoder("utf-8", { ignoreBOM: true }); /** * fx reads bytes: a BOM means no frontmatter, and invalid UTF-8 in a value * makes the skill invalid while a literal U+FFFD is fine. So the BOM is kept, * and a line with invalid UTF-8 ends in a NUL, which the parser rejects. */ function decodeSkillFile(bytes: Uint8Array): string { const lines: Array<string> = []; for (let start = 0; ;) { const newline = bytes.indexOf(0x0a, start); const line = bytes.subarray(start, newline === -1 ? bytes.length : newline); try { lines.push(strictDecoder.decode(line)); } catch { lines.push(`${lenientDecoder.decode(line)}\u0000`); } if (newline === -1) return lines.join("\n"); start = newline + 1; } } function concatBytes(chunks: Iterable<Uint8Array>): Uint8Array { const parts = [...chunks]; const bytes = new Uint8Array(parts.reduce((total, part) => total + part.length, 0)); let offset = 0; for (const part of parts) { bytes.set(part, offset); offset += part.length; } return bytes; } function compareUtf8(left: string, right: string): number { const a = encoder.encode(left); const b = encoder.encode(right); for (let index = 0; index < Math.min(a.length, b.length); index++) { if (a[index] !== b[index]) return (a[index] ?? 0) - (b[index] ?? 0); } return a.length - b.length; } /** fx trims only spaces and tabs. */ const trimSpaceTab = (value: string) => value.replace(/^[ \t]+|[ \t]+$/g, ""); /** fx's `invalidTextCause`: no ASCII control bytes (invalid UTF-8 shows up as one). */ const hasControlByte = (value: string) => [...value].some((char) => char < " " || char === "\x7f"); /** fx's `parseRecognizedValue`: one quoted or bare scalar, no YAML. */ function recognizedValue(raw: string): string | undefined { if (raw.startsWith("|") || raw.startsWith(">")) return undefined; const quoted = /^['"]/.test(raw) || /['"]$/.test(raw); if (!quoted) return raw; return raw.length >= 2 && raw[0] === raw.at(-1) ? raw.slice(1, -1) : undefined; } /** * Reads SKILL.md the way fx 0.0.13 does (`parseSkillFile` in * src/core/skills/skill_contract.zig): a line-based header, not YAML. No * frontmatter names the skill by its folder; unreadable frontmatter (no * closing `---`, missing or repeated `name`, a continued value) skips it. */ function parseSkillMetadata( contents: string, folderName: string, ): { readonly name: string; readonly description?: string } | undefined { const opening = /^---(\r?\n|$)/.exec(contents); if (opening === null) { return validSkillName(folderName) ? { name: folderName } : undefined; } // fx drops a line's trailing `\r` only before a newline, and bounds the // file up to the line after the closing `---`. const lines = contents.slice(opening[0].length).split("\n"); const unterminated = (index: number) => index === lines.length - 1; const closing = lines.findIndex( (line, index) => (unterminated(index) ? line : line.replace(/\r$/, "")) === "---", ); if (closing === -1) return undefined; const throughClosing = opening[0] + lines.slice(0, closing + 1).join("\n"); const bodyStart = throughClosing.length + (unterminated(closing) ? 0 : 1); if (encoder.encode(contents.slice(0, bodyStart)).length > MAX_FRONTMATTER_BYTES) { return undefined; } const header = lines.slice(0, closing).map((line) => line.replace(/\r$/, "")); let name: string | undefined; let description: string | undefined; let sawName = false; let sawDescription = false; let previousRecognized = false; for (let index = 0; index < header.length; index++) { const line = header[index] ?? ""; const trimmed = trimSpaceTab(line); if (trimmed.length === 0 || trimmed.startsWith("#")) continue; if (previousRecognized && /^[ \t]/.test(line)) return undefined; previousRecognized = false; const colon = trimmed.indexOf(":"); if (colon === -1) continue; const key = trimSpaceTab(trimmed.slice(0, colon)); const raw = trimSpaceTab(trimmed.slice(colon + 1)); if (key === "name") { if (sawName) return undefined; sawName = true; name = recognizedValue(raw); if (name === undefined) return undefined; previousRecognized = true; } else if (key === "description") { if (sawDescription) return undefined; sawDescription = true; if (BLOCK_DESCRIPTION_STYLES.has(raw)) { // A block description runs over the following space-indented lines; // fx rejects a tab or a shrinking indent inside it. // Blank lines stay part of the block (`undefined` below). const block: Array<string | undefined> = []; let baseIndent: number | undefined; for (; index + 1 < header.length; index++) { const next = header[index + 1] ?? ""; if (trimSpaceTab(next).length === 0) { block.push(undefined); continue; } const indent = /^ */.exec(next)?.[0].length ?? 0; if (next[indent] === "\t") return undefined; if (indent === 0) break; if (baseIndent !== undefined && indent < baseIndent) return undefined; baseIndent ??= indent; const content = next.slice(baseIndent); if (hasControlByte(content)) return undefined; block.push(content); } description = decodeBlockDescription(block, raw); } else { description = recognizedValue(raw); if (description === undefined || hasControlByte(description)) return undefined; previousRecognized = true; } } } if (name === undefined || !validSkillName(name)) return undefined; return { name, ...(description ? { description } : {}) }; } /** * fx's `decode_block_description`: `|` keeps line breaks; `>` and `>-` join * neighbouring lines with a space and turn a blank line into a break. Only * `>-` drops the final newline, which the menu trims anyway. */ function decodeBlockDescription(lines: ReadonlyArray<string | undefined>, style: string): string { const lastLine = lines.findLastIndex((line) => line !== undefined); let text = ""; for (let index = 0; index <= lastLine; index++) { const line = lines[index]; if (index > 0) { text += style !== "|" && lines[index - 1] !== undefined && line !== undefined ? " " : "\n"; } text += line ?? ""; } return text.trim(); } /** fx's `invalidSkillNameCause`. */ function validSkillName(name: string): boolean { return ( name.length > 0 && encoder.encode(name).length <= MAX_NAME_BYTES && !hasControlByte(name) && name !== "." && name !== ".." && !name.includes("/") && !name.includes("\\") ); } /** * fx scans the workspace roots of each directory from the workspace up to, * not including, HOME, then its managed root, then the home roots. */ function skillRoots(path: Path.Path, cwd: string, home: string) { const roots: Array<{ readonly directory: string; readonly scope: string; readonly rank: number; readonly followsLinks: boolean; }> = []; for (let current = cwd; current !== home;) { for (const root of FX_WORKSPACE_ROOTS) { roots.push({ directory: path.join(current, root.relative), scope: "project", rank: root.rank, followsLinks: root.followsLinks ?? true, }); } const parent = path.dirname(current); if (parent === current) break; current = parent; } for (const root of FX_HOME_ROOTS) { roots.push({ directory: path.join(home, root.relative), scope: "user", rank: root.rank, followsLinks: root.followsLinks ?? true, }); } return roots; } const skipUnreadable = <A, R>( path: string, effect: Effect.Effect<A, PlatformError.PlatformError, R>, ): Effect.Effect<A | undefined, never, R> => effect.pipe( Effect.catchTags({ PlatformError: (error) => error.reason._tag === "NotFound" ? Effect.void.pipe(Effect.as(undefined)) : Effect.logWarning("fx skill scan skipped an unreadable path.", { path, errorTag: error.reason._tag, }).pipe(Effect.as(undefined)), }), ); const discoverSkillsInRoot = Effect.fn("discoverFxSkillsInRoot")(function* (input: { readonly directory: string; readonly scope: string; readonly followsLinks: boolean; }): Effect.fn.Return< ReadonlyArray<{ readonly skill: ServerProviderSkill; readonly folder: string }>, never, FileSystem.FileSystem | Path.Path > { const fileSystem = yield* FileSystem.FileSystem; const path = yield* Path.Path; const isLink = (target: string) => Effect.option(fileSystem.readLink(target)).pipe(Effect.map(Option.isSome)); // fx gives its managed root no read authority: it loads nothing through a // link to the root or to a folder or SKILL.md inside it (HOME is already // a real path here, so a linked root changes its real path). if ( !input.followsLinks && (yield* fileSystem.realPath(input.directory).pipe(Effect.option)).pipe( Option.exists((real) => real !== input.directory), ) ) { return []; } const entries = yield* skipUnreadable(input.directory, fileSystem.readDirectory(input.directory)); const skills: Array<{ readonly skill: ServerProviderSkill; readonly folder: string }> = []; // fx reads a root's folders in UTF-8 byte order; readDirectory promises none. for (const entry of [...(entries ?? [])].sort(compareUtf8)) { if (!input.followsLinks && (yield* isLink(path.join(input.directory, entry)))) continue; // A stray file (`.DS_Store`, a README) is no skill folder. const entryInfo = yield* Effect.option(fileSystem.stat(path.join(input.directory, entry))); if (Option.isNone(entryInfo) || entryInfo.value.type !== "Directory") continue; const skillPath = path.join(input.directory, entry, "SKILL.md"); if (!input.followsLinks && (yield* isLink(skillPath))) continue; const skillInfo = yield* skipUnreadable(skillPath, fileSystem.stat(skillPath)); if (skillInfo?.type !== "File") continue; // fx reads only the metadata prefix, so a long body never hides a skill. const prefix = yield* skipUnreadable( skillPath, fileSystem .stream(skillPath, { bytesToRead: MAX_FRONTMATTER_BYTES + 1 }) .pipe(Stream.runCollect, Effect.map(concatBytes)), ); const metadata = prefix === undefined ? undefined : parseSkillMetadata(decodeSkillFile(prefix), entry); if (metadata === undefined) continue; const folder = yield* skipUnreadable( skillPath, fileSystem.realPath(path.join(input.directory, entry)), ); skills.push({ folder: folder ?? path.join(input.directory, entry), skill: { name: metadata.name, path: skillPath, scope: input.scope, enabled: true, ...(metadata.description === undefined ? {} : { description: metadata.description }), }, }); } return skills; }); /** * Skills in fx's menu order. Like fx, a folder reached twice (a symlinked * root) counts once. When two different skills share a name, `$name` lets * the model choose from fx's catalog, which lists them in scan order; it * takes the first copy, so that is the one listed here. */ export const discoverFxSkills = Effect.fn("discoverFxSkills")(function* (input: { readonly cwd: string; readonly environment: NodeJS.ProcessEnv; }): Effect.fn.Return<ReadonlyArray<ServerProviderSkill>, never, FileSystem.FileSystem | Path.Path> { // fx reads skills only under a set HOME, and compares HOME and the // workspace by their real paths. const configuredHome = input.environment.HOME?.trim(); if (!configuredHome) return []; const fileSystem = yield* FileSystem.FileSystem; const realPath = (target: string) => fileSystem.realPath(target).pipe(Effect.orElseSucceed(() => target)); const home = yield* realPath(configuredHome); const cwd = yield* realPath(input.cwd); const path = yield* Path.Path; const seenFolders = new Set<string>(); // `$review` and `$Review` each load the skill whose name matches the typed // case, so only an exact repeat of a name is dropped. const seenNames = new Set<string>(); const found: Array<{ readonly skill: ServerProviderSkill; readonly rank: number }> = []; for (const root of skillRoots(path, cwd, home)) { for (const { skill, folder } of yield* discoverSkillsInRoot(root)) { if (seenFolders.has(folder)) continue; seenFolders.add(folder); if (seenNames.has(skill.name)) continue; seenNames.add(skill.name); found.push({ skill, rank: root.rank }); } } return found.sort((left, right) => left.rank - right.rank).map(({ skill }) => skill); });
packages/provider-fx/src/server/skills.ts at main · sailfork/t3code-github · Sailfork
picker by scanning the SKILL.md folders fx\n * reads (fx 0.0.13, src/builtins/skills.zig), so no `fx acp` session starts\n * for a composer menu.\n *\n * @module provider-fx/server/skills\n */\nimport type { ServerProviderSkill } from \"@t3tools/contracts\";\nimport * as Effect from \"effect/Effect\";\nimport * as FileSystem from \"effect/FileSystem\";\nimport * as Option from \"effect/Option\";\nimport * as Path from \"effect/Path\";\nimport type * as PlatformError from \"effect/PlatformError\";\nimport * as Stream from \"effect/Stream\";\n\n/**\n * fx's menu order: managed `~/.fx/skills`, then the workspace's `.fx/skills`\n * and `skills/`, then every compatibility root (`skillGroupRank` in\n * src/core/skills/skill_runtime.zig). Symlinked skill folders are followed\n * everywhere except the managed root.\n */\ntype SkillRoot = {\n readonly relative: string;\n readonly rank: 0 | 1 | 2;\n readonly followsLinks?: false;\n};\n\nconst FX_WORKSPACE_ROOTS: ReadonlyArray\u003cSkillRoot> = [\n { relative: \".fx/skills\", rank: 1 },\n { relative: \"skills\", rank: 1 },\n { relative: \".opencode/skills\", rank: 2 },\n { relative: \".codex/skills\", rank: 2 },\n { relative: \".claude/skills\", rank: 2 },\n { relative: \".agents/skills\", rank: 2 },\n { relative: \".claw/skills\", rank: 2 },\n];\nconst FX_HOME_ROOTS: ReadonlyArray\u003cSkillRoot> = [\n { relative: \".fx/skills\", rank: 0, followsLinks: false },\n { relative: \".config/opencode/skills\", rank: 2 },\n { relative: \".codex/skills\", rank: 2 },\n { relative: \".claude/skills\", rank: 2 },\n { relative: \".agents/skills\", rank: 2 },\n { relative: \".claw/skills\", rank: 2 },\n];\n\nconst MAX_FRONTMATTER_BYTES = 64 * 1024;\nconst MAX_NAME_BYTES = 256;\nconst BLOCK_DESCRIPTION_STYLES = new Set([\">\", \">-\", \"|\"]);\nconst encoder = new TextEncoder();\nconst strictDecoder = new TextDecoder(\"utf-8\", { fatal: true, ignoreBOM: true });\nconst lenientDecoder = new TextDecoder(\"utf-8\", { ignoreBOM: true });\n\n/**\n * fx reads bytes: a BOM means no frontmatter, and invalid UTF-8 in a value\n * makes the skill invalid while a literal U+FFFD is fine. So the BOM is kept,\n * and a line with invalid UTF-8 ends in a NUL, which the parser rejects.\n */\nfunction decodeSkillFile(bytes: Uint8Array): string {\n const lines: Array\u003cstring> = [];\n for (let start = 0; ;) {\n const newline = bytes.indexOf(0x0a, start);\n const line = bytes.subarray(start, newline === -1 ? bytes.length : newline);\n try {\n lines.push(strictDecoder.decode(line));\n } catch {\n lines.push(`${lenientDecoder.decode(line)}\\u0000`);\n }\n if (newline === -1) return lines.join(\"\\n\");\n start = newline + 1;\n }\n}\n\nfunction concatBytes(chunks: Iterable\u003cUint8Array>): Uint8Array {\n const parts = [...chunks];\n const bytes = new Uint8Array(parts.reduce((total, part) => total + part.length, 0));\n let offset = 0;\n for (const part of parts) {\n bytes.set(part, offset);\n offset += part.length;\n }\n return bytes;\n}\nfunction compareUtf8(left: string, right: string): number {\n const a = encoder.encode(left);\n const b = encoder.encode(right);\n for (let index = 0; index \u003c Math.min(a.length, b.length); index++) {\n if (a[index] !== b[index]) return (a[index] ?? 0) - (b[index] ?? 0);\n }\n return a.length - b.length;\n}\n\n/** fx trims only spaces and tabs. */\nconst trimSpaceTab = (value: string) => value.replace(/^[ \\t]+|[ \\t]+$/g, \"\");\n\n/** fx's `invalidTextCause`: no ASCII control bytes (invalid UTF-8 shows up as one). */\nconst hasControlByte = (value: string) => [...value].some((char) => char \u003c \" \" || char === \"\\x7f\");\n\n/** fx's `parseRecognizedValue`: one quoted or bare scalar, no YAML. */\nfunction recognizedValue(raw: string): string | undefined {\n if (raw.startsWith(\"|\") || raw.startsWith(\">\")) return undefined;\n const quoted = /^['\"]/.test(raw) || /['\"]$/.test(raw);\n if (!quoted) return raw;\n return raw.length >= 2 && raw[0] === raw.at(-1) ? raw.slice(1, -1) : undefined;\n}\n\n/**\n * Reads SKILL.md the way fx 0.0.13 does (`parseSkillFile` in\n * src/core/skills/skill_contract.zig): a line-based header, not YAML. No\n * frontmatter names the skill by its folder; unreadable frontmatter (no\n * closing `---`, missing or repeated `name`, a continued value) skips it.\n */\nfunction parseSkillMetadata(\n contents: string,\n folderName: string,\n): { readonly name: string; readonly description?: string } | undefined {\n const opening = /^---(\\r?\\n|$)/.exec(contents);\n if (opening === null) {\n return validSkillName(folderName) ? { name: folderName } : undefined;\n }\n // fx drops a line's trailing `\\r` only before a newline, and bounds the\n // file up to the line after the closing `---`.\n const lines = contents.slice(opening[0].length).split(\"\\n\");\n const unterminated = (index: number) => index === lines.length - 1;\n const closing = lines.findIndex(\n (line, index) => (unterminated(index) ? line : line.replace(/\\r$/, \"\")) === \"---\",\n );\n if (closing === -1) return undefined;\n const throughClosing = opening[0] + lines.slice(0, closing + 1).join(\"\\n\");\n const bodyStart = throughClosing.length + (unterminated(closing) ? 0 : 1);\n if (encoder.encode(contents.slice(0, bodyStart)).length > MAX_FRONTMATTER_BYTES) {\n return undefined;\n }\n const header = lines.slice(0, closing).map((line) => line.replace(/\\r$/, \"\"));\n\n let name: string | undefined;\n let description: string | undefined;\n let sawName = false;\n let sawDescription = false;\n let previousRecognized = false;\n for (let index = 0; index \u003c header.length; index++) {\n const line = header[index] ?? \"\";\n const trimmed = trimSpaceTab(line);\n if (trimmed.length === 0 || trimmed.startsWith(\"#\")) continue;\n if (previousRecognized && /^[ \\t]/.test(line)) return undefined;\n previousRecognized = false;\n const colon = trimmed.indexOf(\":\");\n if (colon === -1) continue;\n const key = trimSpaceTab(trimmed.slice(0, colon));\n const raw = trimSpaceTab(trimmed.slice(colon + 1));\n if (key === \"name\") {\n if (sawName) return undefined;\n sawName = true;\n name = recognizedValue(raw);\n if (name === undefined) return undefined;\n previousRecognized = true;\n } else if (key === \"description\") {\n if (sawDescription) return undefined;\n sawDescription = true;\n if (BLOCK_DESCRIPTION_STYLES.has(raw)) {\n // A block description runs over the following space-indented lines;\n // fx rejects a tab or a shrinking indent inside it.\n // Blank lines stay part of the block (`undefined` below).\n const block: Array\u003cstring | undefined> = [];\n let baseIndent: number | undefined;\n for (; index + 1 \u003c header.length; index++) {\n const next = header[index + 1] ?? \"\";\n if (trimSpaceTab(next).length === 0) {\n block.push(undefined);\n continue;\n }\n const indent = /^ */.exec(next)?.[0].length ?? 0;\n if (next[indent] === \"\\t\") return undefined;\n if (indent === 0) break;\n if (baseIndent !== undefined && indent \u003c baseIndent) return undefined;\n baseIndent ??= indent;\n const content = next.slice(baseIndent);\n if (hasControlByte(content)) return undefined;\n block.push(content);\n }\n description = decodeBlockDescription(block, raw);\n } else {\n description = recognizedValue(raw);\n if (description === undefined || hasControlByte(description)) return undefined;\n previousRecognized = true;\n }\n }\n }\n if (name === undefined || !validSkillName(name)) return undefined;\n return { name, ...(description ? { description } : {}) };\n}\n\n/**\n * fx's `decode_block_description`: `|` keeps line breaks; `>` and `>-` join\n * neighbouring lines with a space and turn a blank line into a break. Only\n * `>-` drops the final newline, which the menu trims anyway.\n */\nfunction decodeBlockDescription(lines: ReadonlyArray\u003cstring | undefined>, style: string): string {\n const lastLine = lines.findLastIndex((line) => line !== undefined);\n let text = \"\";\n for (let index = 0; index \u003c= lastLine; index++) {\n const line = lines[index];\n if (index > 0) {\n text += style !== \"|\" && lines[index - 1] !== undefined && line !== undefined ? \" \" : \"\\n\";\n }\n text += line ?? \"\";\n }\n return text.trim();\n}\n\n/** fx's `invalidSkillNameCause`. */\nfunction validSkillName(name: string): boolean {\n return (\n name.length > 0 &&\n encoder.encode(name).length \u003c= MAX_NAME_BYTES &&\n !hasControlByte(name) &&\n name !== \".\" &&\n name !== \"..\" &&\n !name.includes(\"/\") &&\n !name.includes(\"\\\\\")\n );\n}\n\n/**\n * fx scans the workspace roots of each directory from the workspace up to,\n * not including, HOME, then its managed root, then the home roots.\n */\nfunction skillRoots(path: Path.Path, cwd: string, home: string) {\n const roots: Array\u003c{\n readonly directory: string;\n readonly scope: string;\n readonly rank: number;\n readonly followsLinks: boolean;\n }> = [];\n for (let current = cwd; current !== home;) {\n for (const root of FX_WORKSPACE_ROOTS) {\n roots.push({\n directory: path.join(current, root.relative),\n scope: \"project\",\n rank: root.rank,\n followsLinks: root.followsLinks ?? true,\n });\n }\n const parent = path.dirname(current);\n if (parent === current) break;\n current = parent;\n }\n for (const root of FX_HOME_ROOTS) {\n roots.push({\n directory: path.join(home, root.relative),\n scope: \"user\",\n rank: root.rank,\n followsLinks: root.followsLinks ?? true,\n });\n }\n return roots;\n}\n\nconst skipUnreadable = \u003cA, R>(\n path: string,\n effect: Effect.Effect\u003cA, PlatformError.PlatformError, R>,\n): Effect.Effect\u003cA | undefined, never, R> =>\n effect.pipe(\n Effect.catchTags({\n PlatformError: (error) =>\n error.reason._tag === \"NotFound\"\n ? Effect.void.pipe(Effect.as(undefined))\n : Effect.logWarning(\"fx skill scan skipped an unreadable path.\", {\n path,\n errorTag: error.reason._tag,\n }).pipe(Effect.as(undefined)),\n }),\n );\n\nconst discoverSkillsInRoot = Effect.fn(\"discoverFxSkillsInRoot\")(function* (input: {\n readonly directory: string;\n readonly scope: string;\n readonly followsLinks: boolean;\n}): Effect.fn.Return\u003c\n ReadonlyArray\u003c{ readonly skill: ServerProviderSkill; readonly folder: string }>,\n never,\n FileSystem.FileSystem | Path.Path\n> {\n const fileSystem = yield* FileSystem.FileSystem;\n const path = yield* Path.Path;\n const isLink = (target: string) =>\n Effect.option(fileSystem.readLink(target)).pipe(Effect.map(Option.isSome));\n // fx gives its managed root no read authority: it loads nothing through a\n // link to the root or to a folder or SKILL.md inside it (HOME is already\n // a real path here, so a linked root changes its real path).\n if (\n !input.followsLinks &&\n (yield* fileSystem.realPath(input.directory).pipe(Effect.option)).pipe(\n Option.exists((real) => real !== input.directory),\n )\n ) {\n return [];\n }\n const entries = yield* skipUnreadable(input.directory, fileSystem.readDirectory(input.directory));\n const skills: Array\u003c{ readonly skill: ServerProviderSkill; readonly folder: string }> = [];\n // fx reads a root's folders in UTF-8 byte order; readDirectory promises none.\n for (const entry of [...(entries ?? [])].sort(compareUtf8)) {\n if (!input.followsLinks && (yield* isLink(path.join(input.directory, entry)))) continue;\n // A stray file (`.DS_Store`, a README) is no skill folder.\n const entryInfo = yield* Effect.option(fileSystem.stat(path.join(input.directory, entry)));\n if (Option.isNone(entryInfo) || entryInfo.value.type !== \"Directory\") continue;\n const skillPath = path.join(input.directory, entry, \"SKILL.md\");\n if (!input.followsLinks && (yield* isLink(skillPath))) continue;\n const skillInfo = yield* skipUnreadable(skillPath, fileSystem.stat(skillPath));\n if (skillInfo?.type !== \"File\") continue;\n // fx reads only the metadata prefix, so a long body never hides a skill.\n const prefix = yield* skipUnreadable(\n skillPath,\n fileSystem\n .stream(skillPath, { bytesToRead: MAX_FRONTMATTER_BYTES + 1 })\n .pipe(Stream.runCollect, Effect.map(concatBytes)),\n );\n const metadata =\n prefix === undefined ? undefined : parseSkillMetadata(decodeSkillFile(prefix), entry);\n if (metadata === undefined) continue;\n const folder = yield* skipUnreadable(\n skillPath,\n fileSystem.realPath(path.join(input.directory, entry)),\n );\n skills.push({\n folder: folder ?? path.join(input.directory, entry),\n skill: {\n name: metadata.name,\n path: skillPath,\n scope: input.scope,\n enabled: true,\n ...(metadata.description === undefined ? {} : { description: metadata.description }),\n },\n });\n }\n return skills;\n});\n\n/**\n * Skills in fx's menu order. Like fx, a folder reached twice (a symlinked\n * root) counts once. When two different skills share a name, `$name` lets\n * the model choose from fx's catalog, which lists them in scan order; it\n * takes the first copy, so that is the one listed here.\n */\nexport const discoverFxSkills = Effect.fn(\"discoverFxSkills\")(function* (input: {\n readonly cwd: string;\n readonly environment: NodeJS.ProcessEnv;\n}): Effect.fn.Return\u003cReadonlyArray\u003cServerProviderSkill>, never, FileSystem.FileSystem | Path.Path> {\n // fx reads skills only under a set HOME, and compares HOME and the\n // workspace by their real paths.\n const configuredHome = input.environment.HOME?.trim();\n if (!configuredHome) return [];\n const fileSystem = yield* FileSystem.FileSystem;\n const realPath = (target: string) =>\n fileSystem.realPath(target).pipe(Effect.orElseSucceed(() => target));\n const home = yield* realPath(configuredHome);\n const cwd = yield* realPath(input.cwd);\n const path = yield* Path.Path;\n const seenFolders = new Set\u003cstring>();\n // `$review` and `$Review` each load the skill whose name matches the typed\n // case, so only an exact repeat of a name is dropped.\n const seenNames = new Set\u003cstring>();\n const found: Array\u003c{ readonly skill: ServerProviderSkill; readonly rank: number }> = [];\n for (const root of skillRoots(path, cwd, home)) {\n for (const { skill, folder } of yield* discoverSkillsInRoot(root)) {\n if (seenFolders.has(folder)) continue;\n seenFolders.add(folder);\n if (seenNames.has(skill.name)) continue;\n seenNames.add(skill.name);\n found.push({ skill, rank: root.rank });\n }\n }\n return found.sort((left, right) => left.rank - right.rank).map(({ skill }) => skill);\n});\n","preview":null,"history":null,"version":"96251b14cae2bc49be7d218818a531d4da46b10b"},"/_session?shell=1":null}