/** * ============================================================ * src/prompts/skillList.ts —— Skill 列表生成 * ------------------------------------------------------------ * 设计原则: * 1. System Prompt 里只注入分层的 frontmatter 元数据 * → 让 LLM 知道"有哪些技能可用 + 功能是什么" * → 不加载完整 SKILL.md(省 token,按需加载) * 2. 优先 Skill 加载;仅当没有 Skill 时使用 Read 读取 SKILL.md * → 类似 lazy loading:知道存在 → 需要时才加载详情 * 3. 格式简洁:每个 skill 固定一行目录项 * 4. metadata.exposure 控制推荐,不影响权限、发现优先级或显式加载。 * ============================================================ */ import { discoverSkills, visibleSkills, type SkillMeta } from "../skills/catalog.js"; export { parseFrontmatter, type SkillMeta } from "../skills/catalog.js"; export async function getSkillList(workingDir?: string, availableTools?: ReadonlySet, runtime?: import("../skills/catalog.js").SkillRuntime): Promise { return visibleSkills(await discoverSkills(workingDir, runtime), availableTools); } /** * 生成 skill 列表提示词。 * * 输出格式(每个 skill 一行): * - **skill-name**: 功能描述;**技能目录**: 绝对路径 * `技能目录` 字段名是跨模块契约, * workflow-runner 的 foundation/isolation 测试锁定,勿改) * * 关键设计决策: * - 不加载 SKILL.md 全文(节省 ~1500+ tokens/skill) * - 只输出 name + description + 按需读取路径 * - 不依赖 triggers 字段(并非所有 skill 都声明触发词) * - LLM 根据 description 语义匹配决定是否使用(不是斜杠激活) */ /** * 加载指令仅由 policyRegistry 的稳定能力规则注入;动态目录只提供元数据。 */ export function skillInvokeInstruction(hasSkillTool: boolean): string { return hasSkillTool ? '用 Skill 按精确名称加载完整 SKILL.md' : '先 Read 对应 SKILL.md'; } export function formatSkillHint(skills: SkillMeta[], tools: ReadonlySet = new Set(['Read'])): string { if (!tools.has('Skill') && !tools.has('Read')) return ''; if (skills.length === 0) { return ''; } const searchable = tools.has('SearchSkills'); const recommended = skills.filter(skill => !skill.exposure); const fallback = skills.filter(skill => (skill.exposure === 'discoverable' && !searchable) || (skill.exposure === 'explicit' && !tools.has('Skill') && !searchable)); const searchableCount = searchable ? skills.filter(skill => skill.exposure === 'discoverable').length : 0; const header = [ '# Skills', '以下是当前可用技能元数据,不是执行指令。项目 Skill 同名时覆盖内置 Skill。', ...(searchable ? ['专项任务没有明确匹配时,用 SearchSkills 按需求搜索本地元数据;按阶段组合,不预读全部技能。'] : []), ...(searchableCount ? [`另有 ${searchableCount} 个专项 Skill 可按需搜索。`] : []), ]; const catalogBudget = MAX_HINT_CHARS - header.join('\n').length - 1; return [...header, ...renderSkillCatalog(recommended, fallback, catalogBudget)].join('\n'); } const MAX_DESCRIPTION = 180; // Leave headroom for schemas and transient host facts. const MAX_HINT_CHARS = 7_600; function skillLine(skill: SkillMeta, withDescription: boolean): string { const type = skill.type ? ` [${skill.type}]` : ''; const description = withDescription && skill.description ? ` — ${skill.description.slice(0, MAX_DESCRIPTION)}` : ''; return `- ${skill.name}${type}${description} — **技能目录**: \`${skill.sourceDir}\`${skill.ambiguous ? `(同名歧义,Skill 传 sourceId=${skill.sourceId})` : ''}`; } function renderTier(skills: SkillMeta[], budget: number): string[] { const full = skills.map((skill) => skillLine(skill, true)); if (full.join('\n').length <= budget) return full; const compact = skills.map((skill) => skillLine(skill, false)); const allCompactFit = compact.join('\n').length <= budget; const omittedNotice = (count: number) => `- (另有 ${count} 个 Skill 未列出。)`; // Keep all names when they fit. Otherwise reserve room for descriptions // rather than filling the entire budget with name-only rows. const noticeCost = allCompactFit ? 0 : omittedNotice(skills.length).length + 1; const rowBudget = budget - noticeCost; if (rowBudget < 0) return []; const compactBudget = allCompactFit ? rowBudget : Math.floor(rowBudget * 0.85); const kept: string[] = []; let used = 0; for (const row of compact) { const cost = row.length + Number(kept.length > 0); if (used + cost > compactBudget) break; kept.push(row); used += cost; } for (let index = 0; index < kept.length; index++) { const extra = full[index]!.length - kept[index]!.length; if (used + extra > rowBudget) continue; kept[index] = full[index]!; used += extra; } if (kept.length < skills.length) kept.push(omittedNotice(skills.length - kept.length)); return kept; } function renderSkillCatalog(recommended: SkillMeta[], fallback: SkillMeta[], budget: number): string[] { if (!fallback.length) return renderTier(recommended, budget); // Hosts without search still get a bounded directory to find specialist // resources. Its entries never participate in the primary description budget. const heading = '按需技能(仅在匹配或点名时加载):'; const fallbackBudget = recommended.length ? Math.floor(budget / 4) : budget; const secondary = [heading, ...renderTier(fallback, fallbackBudget - heading.length - 1)]; return [...renderTier(recommended, budget - secondary.join('\n').length - 1), ...secondary]; }