- 增强 `src/domains/docs/tools.ts` 的搜索评分引擎,支持自然多词组合与分词(如 "员工 手机"、"报销 审批") - 扩充正文段落、请求参数表格、返回报文 JSON 与 CURL 示例的多层级深度匹配 - 新增 `matchReason` 匹配依据与上下文片段(Snippet)生成,在 CLI 表格及 JSON 结果中清晰呈现命中理由 - 完善 `src/domains/docs/docs.test.ts` 跨正文分词检索测试用例
250 lines
7.8 KiB
TypeScript
250 lines
7.8 KiB
TypeScript
import { booleanOption, numberOption, stringOption } from "../parser.js";
|
|
import { printJson, printKeyValue, printTable } from "../printer.js";
|
|
import type { CliParsedArgs, CliResult, CliRuntime } from "../types.js";
|
|
import {
|
|
getDocDetail,
|
|
getDocsCatalog,
|
|
listCategories,
|
|
searchDocs,
|
|
syncDocsCatalog,
|
|
} from "../../domains/docs/tools.js";
|
|
|
|
export async function handleDocsCommand(
|
|
parsed: CliParsedArgs,
|
|
runtime: CliRuntime
|
|
): Promise<CliResult> {
|
|
const action = parsed.action?.toLowerCase() || "";
|
|
const forceRefresh =
|
|
booleanOption(parsed.options, ["refresh", "force", "sync"]) ||
|
|
action === "sync";
|
|
|
|
// 1. 同步命令: hose-use docs sync
|
|
if (action === "sync") {
|
|
const res = await syncDocsCatalog();
|
|
if (runtime.jsonMode) {
|
|
return {
|
|
stdout: printJson(res, runtime.outputOptions),
|
|
stderr: "",
|
|
exitCode: 0,
|
|
};
|
|
}
|
|
return {
|
|
stdout: `====== 开放平台文档同步完成 ======\n文档总数: ${res.totalDocs}\n接口端点: ${res.totalEndpoints}\n数据源: ${res.source}\n更新时间: ${res.updatedAt}\n`,
|
|
stderr: "",
|
|
exitCode: 0,
|
|
};
|
|
}
|
|
|
|
// 2. 分类列表: hose-use docs categories
|
|
if (["categories", "category", "cats", "modules"].includes(action)) {
|
|
const cats = await listCategories({ forceRefresh });
|
|
if (runtime.jsonMode) {
|
|
return {
|
|
stdout: printJson({ count: cats.length, categories: cats }, runtime.outputOptions),
|
|
stderr: "",
|
|
exitCode: 0,
|
|
};
|
|
}
|
|
const rows = cats.map((c, i) => [
|
|
i + 1,
|
|
c.name,
|
|
`${c.count} 篇`,
|
|
`${c.endpointsCount} 个`,
|
|
]);
|
|
const table = printTable(["#", "业务分类", "文档篇数", "HTTP 端点数"], rows);
|
|
return {
|
|
stdout: `====== 合思开放平台业务模块分类 (共 ${cats.length} 个) ======\n\n${table}\n\n提示: 运行 hose-use docs search --category=<模块名> 可查看该模块下的所有接口\n`,
|
|
stderr: "",
|
|
exitCode: 0,
|
|
};
|
|
}
|
|
|
|
// 3. 详情命令: hose-use docs show <target>
|
|
if (["show", "get", "info", "cat", "read"].includes(action)) {
|
|
const target = parsed.args[0] || stringOption(parsed.options, ["path", "url", "id", "name", "target"]);
|
|
if (!target) {
|
|
throw new Error("请指定要查看的接口路径、文档 URL 或名称: hose-use docs show <接口或URL>");
|
|
}
|
|
|
|
const doc = await getDocDetail(target, { forceRefresh });
|
|
if (!doc) {
|
|
if (runtime.jsonMode) {
|
|
return {
|
|
stdout: printJson({ found: false, target }, runtime.outputOptions),
|
|
stderr: "",
|
|
exitCode: 1,
|
|
};
|
|
}
|
|
return {
|
|
stdout: "",
|
|
stderr: `[NOT FOUND] 未找到匹配的文档或接口: ${target}\n提示: 可以先运行 hose-use docs search ${target} 检索相近接口。\n`,
|
|
exitCode: 1,
|
|
};
|
|
}
|
|
|
|
if (runtime.jsonMode) {
|
|
return {
|
|
stdout: printJson(doc, runtime.outputOptions),
|
|
stderr: "",
|
|
exitCode: 0,
|
|
};
|
|
}
|
|
|
|
let out = `====== ${doc.title} ======\n`;
|
|
out += printKeyValue({
|
|
"业务模块": doc.category,
|
|
"HTTP 方法": doc.method || "非端点/指南页面",
|
|
"接口路径": doc.path || "-",
|
|
"在线文档": doc.permalink,
|
|
"文档标识": `#${doc.id}`,
|
|
});
|
|
|
|
if (doc.parameters) {
|
|
out += `\n\n### 参数规格与定义\n${doc.parameters}\n`;
|
|
}
|
|
|
|
if (doc.curl) {
|
|
out += `\n### CURL 请求示范\n\`\`\`bash\n${doc.curl}\n\`\`\`\n`;
|
|
}
|
|
|
|
if (doc.responseSample) {
|
|
out += `\n### 成功响应示例\n\`\`\`json\n${doc.responseSample}\n\`\`\`\n`;
|
|
}
|
|
|
|
return { stdout: `${out}\n`, stderr: "", exitCode: 0 };
|
|
}
|
|
|
|
// 4. 搜索命令 (默认行为): hose-use docs search <keyword> 或 hose-use docs <keyword>
|
|
const isSearchAction = ["search", "find", "list", "ls", ""].includes(action);
|
|
if (isSearchAction) {
|
|
const keyword =
|
|
(action === "search" || action === "find" ? parsed.args[0] : action || parsed.args[0]) ||
|
|
stringOption(parsed.options, ["keyword", "q", "query"]);
|
|
|
|
const category = stringOption(parsed.options, ["category", "cat", "module"]);
|
|
const method = stringOption(parsed.options, ["method", "m"]);
|
|
const path = stringOption(parsed.options, ["path", "p"]);
|
|
const limit = numberOption(parsed.options, ["limit", "n", "count"], 25);
|
|
|
|
// 如果没有任何搜索条件且没有关键词,展示全局概览
|
|
if (!keyword && !category && !method && !path && !action) {
|
|
const catalog = await getDocsCatalog({ forceRefresh });
|
|
if (runtime.jsonMode) {
|
|
return {
|
|
stdout: printJson(
|
|
{
|
|
totalDocs: catalog.totalDocs,
|
|
totalEndpoints: catalog.totalEndpoints,
|
|
sourceUrl: catalog.sourceUrl,
|
|
updatedAt: catalog.updatedAt,
|
|
},
|
|
runtime.outputOptions
|
|
),
|
|
stderr: "",
|
|
exitCode: 0,
|
|
};
|
|
}
|
|
return {
|
|
stdout: `====== 合思开放平台官方文档中心 ======
|
|
文档总数: ${catalog.totalDocs} 篇
|
|
端点总数: ${catalog.totalEndpoints} 个
|
|
数据来源: ${catalog.sourceUrl}
|
|
更新时间: ${catalog.updatedAt}
|
|
|
|
常用子命令:
|
|
hose-use docs search <关键词> 检索开放平台 HTTP 接口与文档
|
|
hose-use docs show <路径|名称|URL> 查看单篇接口规格、参数表格与 CURL 响应
|
|
hose-use docs categories 查看 26+ 个业务分类及其端点数
|
|
hose-use docs sync 强制与线上最新文档中心全量同步
|
|
`,
|
|
stderr: "",
|
|
exitCode: 0,
|
|
};
|
|
}
|
|
|
|
const searchRes = await searchDocs(
|
|
{
|
|
keyword,
|
|
category,
|
|
method,
|
|
path,
|
|
limit,
|
|
},
|
|
{ forceRefresh }
|
|
);
|
|
|
|
if (runtime.jsonMode) {
|
|
const summaryItems = searchRes.items.map((item) => ({
|
|
id: item.id,
|
|
title: item.title,
|
|
category: item.category,
|
|
method: item.method,
|
|
path: item.path,
|
|
url: item.url,
|
|
permalink: item.permalink,
|
|
matchReason: item.matchReason || undefined,
|
|
}));
|
|
return {
|
|
stdout: printJson(
|
|
{
|
|
total: searchRes.total,
|
|
count: summaryItems.length,
|
|
keyword: searchRes.keyword,
|
|
category: searchRes.category,
|
|
method: searchRes.method,
|
|
items: summaryItems,
|
|
},
|
|
runtime.outputOptions
|
|
),
|
|
stderr: "",
|
|
exitCode: 0,
|
|
};
|
|
}
|
|
|
|
if (searchRes.items.length === 0) {
|
|
return {
|
|
stdout: `未找到匹配的文档或接口 (关键词: ${keyword || "-"}, 分类: ${category || "-"}, 方法: ${method || "-"})\n`,
|
|
stderr: "",
|
|
exitCode: 0,
|
|
};
|
|
}
|
|
|
|
const hasKeyword = Boolean(keyword);
|
|
const headers = hasKeyword
|
|
? ["Method", "Path / URL", "标题", "分类", "匹配依据"]
|
|
: ["Method", "Path / URL", "标题", "分类"];
|
|
|
|
const rows = searchRes.items.map((item) => {
|
|
const base = [
|
|
item.method || "DOC",
|
|
item.path || item.url,
|
|
item.title,
|
|
item.category,
|
|
];
|
|
if (hasKeyword) {
|
|
base.push(item.matchReason || "-");
|
|
}
|
|
return base;
|
|
});
|
|
|
|
const table = printTable(headers, rows);
|
|
const filterDesc = [
|
|
keyword ? `关键词: '${keyword}'` : "",
|
|
category ? `分类: '${category}'` : "",
|
|
method ? `方法: '${method}'` : "",
|
|
]
|
|
.filter(Boolean)
|
|
.join(", ");
|
|
|
|
return {
|
|
stdout: `====== 开放平台接口搜索结果 (找到 ${searchRes.total} 条${filterDesc ? ` | ${filterDesc}` : ""}) ======\n\n${table}\n\n提示: 运行 hose-use docs show <Path> 可查看对应接口的完整参数和 CURL 示范\n`,
|
|
stderr: "",
|
|
exitCode: 0,
|
|
};
|
|
}
|
|
|
|
throw new Error(
|
|
`未知的 docs 子命令: ${action}。支持: search <关键词>, show <路径|URL>, categories, sync`
|
|
);
|
|
}
|