iter(g177): 补充配方查询子命令

This commit is contained in:
HIsheR
2026-10-26 16:29:26 +08:00
parent ee758817d3
commit c6fd8b8afb
5 changed files with 44 additions and 9 deletions
+35 -3
View File
@@ -452,6 +452,37 @@ function parseRecipes() {
return out;
}
/*** 从配方正文里认出用到的组件 id(词边界匹配,避免 toast 命中 toastError),CLI 与 MCP 共用一份 ***/
function recipeComps(r) {
const ids = collectComponents().map((x) => x.comp.id);
const body = `${r.code}\n${r.text}`; // 代码块 + 正文一起扫:要点里也会点名组件
return ids.filter((id) => new RegExp('\\b' + id + '\\b').test(body)).sort();
}
/*** 列出组合配方(编号 / 标题 / 用到的组件),或按编号打印单条配方全文;MCP 没注册时也能查配方 ***/
function cmdRecipes(args = []) {
const id = args.find((a) => !a.startsWith('--'));
const list = parseRecipes();
if (!list.length) return console.error('recipes.md 里没有配方');
const asJson = args.includes('--json');
if (id) {
const hit = list.find((r) => r.id === id);
if (!hit) {
console.error(`recipe not found: ${id}(可用 ${list.map((r) => r.id).join(' / ')})`);
return process.exit(1);
}
if (asJson) return console.log(JSON.stringify({ id: hit.id, title: hit.title, components: recipeComps(hit), code: hit.code, notes: hit.notes }, null, 2));
return console.log(`# 配方 ${hit.id}:${hit.title}
用到的组件:${recipeComps(hit).join(' / ')}
${hit.text}`);
}
if (asJson) return console.log(JSON.stringify(list.map((r) => ({ id: r.id, title: r.title, components: recipeComps(r) })), null, 2));
for (const r of list) console.log(`${String(r.id).padEnd(4)} ${r.title} [${recipeComps(r).join(' / ')}]`);
console.log(`
共 ${list.length} 条配方;看骨架与要点:ua recipes <编号>(MCP 里对应 get_recipe)`);
}
/*** 渲染 Agent 文档(llms-full.txt):无图形纯文本,逐组件参数 / 回调 / 步骤 / 约定全量收录 ***/
function renderAgentDocs(index, tokens) {
const lines = [
@@ -636,9 +667,7 @@ function cmdMcp() {
throw new Error(`MCP 工具注册表与 MCP_TOOL_NAMES 不一致:${tools.map((t) => t.name).join(',')}`); // 常量是文档校验的基准,先自证
}
/*** 从配方正文里认出用到的组件 id(按词边界匹配,避免 toast 命中 toastError) ***/
const recipeComponents = (r) =>
compIds.filter((id) => new RegExp(`\\b${id}\\b`).test(`${r.code}\n${r.text}`)).sort();
const recipeComponents = (r) => recipeComps(r).filter((id) => compIds.includes(id)); // 与 CLI recipes 共用同一抽取逻辑
const send = (obj) => process.stdout.write(JSON.stringify(obj) + '\n');
@@ -1378,6 +1407,9 @@ switch (cmd) {
}
cmdInfo(args[0], args.slice(1));
break;
case 'recipes':
cmdRecipes(args);
break;
case 'docs':
cmdDocs();
break;