bfi-cli 0.1.95 → 0.1.96

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,13 @@
1
+ #!/usr/bin/env node
2
+
3
+ import module from 'node:module';
4
+
5
+ if (module.enableCompileCache && !process.env.NODE_DISABLE_COMPILE_CACHE) {
6
+ try {
7
+ module.enableCompileCache();
8
+ } catch {
9
+ // Ignore errors
10
+ }
11
+ }
12
+
13
+ await import('../dist/mcp-weknora/main.mjs');
package/dist/cli.mjs CHANGED
@@ -8118,17 +8118,6 @@ const ALLOWED_FLAGS = {
8118
8118
  "--app",
8119
8119
  "--type"
8120
8120
  ],
8121
- cases: [
8122
- "--type",
8123
- "--finger",
8124
- "--service",
8125
- "--kw",
8126
- "--limit",
8127
- "--file",
8128
- "--env",
8129
- "--confidence",
8130
- "--title"
8131
- ],
8132
8121
  wiki: [
8133
8122
  "--limit",
8134
8123
  "--source",
@@ -8571,68 +8560,6 @@ async function runYmc(args, globalOpts) {
8571
8560
  emit(await api.call("ymc/tool/list", body), jsonMode, rowsSummary("诊断文件"));
8572
8561
  break;
8573
8562
  }
8574
- case "cases": {
8575
- const sub2 = rest[0] || "search";
8576
- const rest2 = rest.slice(1);
8577
- if (sub2 === "search") {
8578
- const body = {};
8579
- for (const [k, name] of [
8580
- ["type", "--type"],
8581
- ["fingerprint", "--finger"],
8582
- ["service", "--service"],
8583
- ["keyword", "--kw"]
8584
- ]) {
8585
- const v = opt(rest2, name);
8586
- if (v) body[k] = v;
8587
- }
8588
- if (opt(rest2, "--limit")) body.limit = Number(opt(rest2, "--limit"));
8589
- emit(await api.call("cases/search", body), jsonMode, (data) => {
8590
- const d = data;
8591
- const list = d.cases || [];
8592
- if (!list.length) return "(无命中案例)";
8593
- return `✓ 命中 ${d.count} 条:\n` + list.map((x) => ` [${x.case_id}] (${x.type}/${x.confidence}) ${x.title}\n 指纹: ${JSON.stringify(x.fingerprint)} ${x.env || ""} ${x.service || ""} by ${x.author}`).join("\n");
8594
- });
8595
- } else if (sub2 === "show") {
8596
- const id = positionals(rest2)[0];
8597
- if (!id) {
8598
- console.error("用法:bfi ymc cases show <case_id>");
8599
- process.exit(1);
8600
- }
8601
- emit(await api.get(`cases/${id}`), jsonMode);
8602
- } else if (sub2 === "upload") {
8603
- const file = opt(rest2, "--file");
8604
- if (!file) {
8605
- console.error("用法:bfi ymc cases upload --file <case.md> [--type] [--finger \"a,b\"] [--env] [--service]");
8606
- process.exit(1);
8607
- }
8608
- const { readFileSync } = await import("node:fs");
8609
- const body = {
8610
- type: opt(rest2, "--type") || "review",
8611
- fingerprint: (opt(rest2, "--finger") || "").split(",").map((s) => s.trim()).filter(Boolean),
8612
- content: readFileSync(file, "utf-8")
8613
- };
8614
- for (const [k, name] of [
8615
- ["env", "--env"],
8616
- ["service", "--service"],
8617
- ["confidence", "--confidence"],
8618
- ["title", "--title"]
8619
- ]) {
8620
- const v = opt(rest2, name);
8621
- if (v) body[k] = v;
8622
- }
8623
- emit(await api.call("cases", body), jsonMode, (data) => `✓ 案例已上传: ${data.case_id}`);
8624
- } else if (sub2 === "index") emit(await api.get("cases/index/overview"), jsonMode, (data) => {
8625
- const d = data;
8626
- const byType = Object.entries(d.by_type || {}).map(([k, v]) => `${k}=${v}`).join(", ");
8627
- const fps = (d.top_fingerprints || []).map((x) => `${x.fingerprint}(${x.count})`).join(", ");
8628
- return `✓ 案例总数: ${d.total}\n 类型分布: ${byType}\n 高频指纹: ${fps}`;
8629
- });
8630
- else {
8631
- console.error("用法:bfi ymc cases <search|show|upload|index>");
8632
- process.exit(1);
8633
- }
8634
- break;
8635
- }
8636
8563
  case "wiki": {
8637
8564
  const sub2 = rest[0] || "search";
8638
8565
  const rest2 = rest.slice(1);
@@ -8869,6 +8796,7 @@ async function runYmc(args, globalOpts) {
8869
8796
  break;
8870
8797
  }
8871
8798
  default: {
8799
+ if (sub === "cases" || sub.startsWith("cases")) console.error("案例库已迁移 WeKnora:检索/归档改用 weknora-bfi MCP 工具(kb_search / kb_archive_case)。");
8872
8800
  console.error(`未知子命令:${sub}`);
8873
8801
  const hint = SUBCOMMAND_HINTS[sub];
8874
8802
  if (hint) console.error(`提示:${hint}`);
@@ -0,0 +1 @@
1
+ export {}
@@ -0,0 +1,308 @@
1
+ import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
2
+ import { Server } from "@modelcontextprotocol/sdk/server/index.js";
3
+ import { CallToolRequestSchema, CallToolResultSchema, ListToolsRequestSchema, ListToolsResultSchema } from "@modelcontextprotocol/sdk/types.js";
4
+ import { Client } from "@modelcontextprotocol/sdk/client/index.js";
5
+ import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js";
6
+ import fs from "node:fs";
7
+ import path from "node:path";
8
+ import os from "node:os";
9
+ const DEFAULT_WEKNORA_URL = process.env.WEKNORA_MCP_URL || "https://bfi.yyuap.com/weknora/mcp/e8e50146-81f6-4aca-b5cf-cce711bd2b99";
10
+ const CASE_KB_NAME = "案例库";
11
+ var WeknoraError = class extends Error {
12
+ errorKind;
13
+ constructor(errorKind, message) {
14
+ super(message);
15
+ this.errorKind = errorKind;
16
+ }
17
+ };
18
+ var WeknoraRemote = class {
19
+ url;
20
+ token;
21
+ client = null;
22
+ constructor(url, token) {
23
+ this.url = url;
24
+ this.token = token;
25
+ }
26
+ async listTools() {
27
+ const client = await this.ensure();
28
+ try {
29
+ return (await client.request({ method: "tools/list" }, ListToolsResultSchema)).tools;
30
+ } catch (e) {
31
+ this.handleFailure(e);
32
+ }
33
+ }
34
+ async callTool(name, args) {
35
+ if (!this.token()) throw new WeknoraError("unauthorized", this.loginHint());
36
+ const client = await this.ensure();
37
+ try {
38
+ return await client.request({
39
+ method: "tools/call",
40
+ params: {
41
+ name,
42
+ arguments: args ?? {}
43
+ }
44
+ }, CallToolResultSchema);
45
+ } catch (e) {
46
+ this.handleFailure(e);
47
+ }
48
+ }
49
+ async ensure() {
50
+ if (this.client) return this.client;
51
+ const token = this.token();
52
+ if (!token) throw new WeknoraError("unauthorized", this.loginHint());
53
+ const client = new Client({
54
+ name: "bfi-weknora-mcp",
55
+ version: "0.1.0"
56
+ });
57
+ const transport = new StreamableHTTPClientTransport(new URL(this.url), { requestInit: { headers: { Authorization: `Bearer ${token}` } } });
58
+ try {
59
+ await client.connect(transport);
60
+ } catch (e) {
61
+ throw new WeknoraError("weknora_unreachable", `WeKnora 服务不可达(${this.url}): ${e instanceof Error ? e.message : String(e)}`);
62
+ }
63
+ this.client = client;
64
+ return client;
65
+ }
66
+ handleFailure(e) {
67
+ this.client = null;
68
+ const message = e instanceof Error ? e.message : String(e);
69
+ if (/401|unauthorized|Unauthorized/i.test(message)) throw new WeknoraError("unauthorized", `WeKnora token 无效或过期。${this.loginHint()}`);
70
+ throw new WeknoraError("weknora_error", `WeKnora 远端调用失败: ${message}`);
71
+ }
72
+ loginHint() {
73
+ return "未找到 bfi token:请先执行 `bfi auth login --token <sk-token>`(token 获取地址 https://bfi.yyuap.com/user/profile),或设置 BFI_TOKEN 环境变量";
74
+ }
75
+ };
76
+ const GLOBAL_CONFIG_DIR = path.join(os.homedir(), ".bfi");
77
+ const GLOBAL_CONFIG_FILE = path.join(GLOBAL_CONFIG_DIR, "config.json");
78
+ path.join(".bfi", "lock.json");
79
+ function loadGlobalConfig() {
80
+ try {
81
+ if (fs.existsSync(GLOBAL_CONFIG_FILE)) {
82
+ const content = fs.readFileSync(GLOBAL_CONFIG_FILE, "utf8");
83
+ return JSON.parse(content);
84
+ }
85
+ } catch (error) {
86
+ console.warn(`Warning: Failed to load global config: ${error.message}`);
87
+ }
88
+ return null;
89
+ }
90
+ function resolveToken() {
91
+ if (process.env.BFI_TOKEN) return process.env.BFI_TOKEN;
92
+ return loadGlobalConfig()?.token ?? null;
93
+ }
94
+ function textResult(payload, isError = false) {
95
+ return {
96
+ content: [{
97
+ type: "text",
98
+ text: JSON.stringify(payload, null, 2)
99
+ }],
100
+ isError
101
+ };
102
+ }
103
+ const TOOLS = [
104
+ {
105
+ name: "kb_search",
106
+ description: "跨库语义检索 BFI 知识库(案例库/wiki/jira-支持案例/供应链/领域知识全部覆盖)——诊断先验的主通道:查历史同类问题、已解决方案、领域知识。返回最相关文段(hybrid)。",
107
+ inputSchema: {
108
+ type: "object",
109
+ properties: {
110
+ query: {
111
+ type: "string",
112
+ description: "检索问题(症状/组件/错误关键词组合均可)"
113
+ },
114
+ top_k: {
115
+ type: "number",
116
+ description: "返回条数(默认 5)"
117
+ }
118
+ },
119
+ required: ["query"]
120
+ }
121
+ },
122
+ {
123
+ name: "kb_ask",
124
+ description: "向知识库提问,返回合成答案与引用来源——需要结论而非文段列表时用。",
125
+ inputSchema: {
126
+ type: "object",
127
+ properties: { question: {
128
+ type: "string",
129
+ description: "问题"
130
+ } },
131
+ required: ["question"]
132
+ }
133
+ },
134
+ {
135
+ name: "kb_grep",
136
+ description: "按正则/关键词精确搜索知识库文本块(大小写不敏感)——已知确切关键词时用。",
137
+ inputSchema: {
138
+ type: "object",
139
+ properties: {
140
+ pattern: {
141
+ type: "string",
142
+ description: "正则或关键词"
143
+ },
144
+ kb: {
145
+ type: "string",
146
+ description: "知识库名或 id(可选,默认全部在 scope 库)"
147
+ }
148
+ },
149
+ required: ["pattern"]
150
+ }
151
+ },
152
+ {
153
+ name: "kb_archive_case",
154
+ description: `把诊断结论归档到「${CASE_KB_NAME}」知识库(唯一写通道)。content_md 建议结构:# 标题 / ## 现象 / ## 根因链 / ## 修复 / ## 证据与复现 / 指纹一行(供先验检索)。归档后先验检索即可命中,避免同类问题重复排查。`,
155
+ inputSchema: {
156
+ type: "object",
157
+ properties: {
158
+ title: {
159
+ type: "string",
160
+ description: "案例标题(症状+组件,如「OOM OCR 切图重复解码」)"
161
+ },
162
+ content_md: {
163
+ type: "string",
164
+ description: "案例正文(Markdown)"
165
+ }
166
+ },
167
+ required: ["title", "content_md"]
168
+ }
169
+ },
170
+ {
171
+ name: "kb_list",
172
+ description: "列出全部可访问知识库(id/名称/描述)。",
173
+ inputSchema: {
174
+ type: "object",
175
+ properties: {}
176
+ }
177
+ },
178
+ {
179
+ name: "kb_docs",
180
+ description: "列出一个知识库内的文档(分页)。",
181
+ inputSchema: {
182
+ type: "object",
183
+ properties: { kb: {
184
+ type: "string",
185
+ description: "知识库名或 id"
186
+ } },
187
+ required: ["kb"]
188
+ }
189
+ },
190
+ {
191
+ name: "kb_read",
192
+ description: "读一篇知识文档的元数据与正文分块。",
193
+ inputSchema: {
194
+ type: "object",
195
+ properties: {
196
+ doc_id: {
197
+ type: "string",
198
+ description: "文档 id"
199
+ },
200
+ kb: {
201
+ type: "string",
202
+ description: "知识库名或 id"
203
+ }
204
+ },
205
+ required: ["doc_id"]
206
+ }
207
+ }
208
+ ];
209
+ async function callSemantic(remote, name, args) {
210
+ const a = args ?? {};
211
+ switch (name) {
212
+ case "kb_search": return {
213
+ tool: "search_knowledge",
214
+ arguments: {
215
+ query: a.query,
216
+ match_count: Number(a.top_k ?? 5)
217
+ }
218
+ };
219
+ case "kb_ask": return {
220
+ tool: "ask",
221
+ arguments: { question: a.question }
222
+ };
223
+ case "kb_grep": return {
224
+ tool: "grep_chunks",
225
+ arguments: {
226
+ pattern: a.pattern,
227
+ ...a.kb ? { kb_id: a.kb } : {}
228
+ }
229
+ };
230
+ case "kb_list": return {
231
+ tool: "list_knowledge_bases",
232
+ arguments: {}
233
+ };
234
+ case "kb_docs": return {
235
+ tool: "list_documents",
236
+ arguments: { kb_id: a.kb }
237
+ };
238
+ case "kb_read": return {
239
+ tool: "read_document",
240
+ arguments: {
241
+ knowledge_id: a.doc_id,
242
+ ...a.kb ? { kb_id: a.kb } : {}
243
+ }
244
+ };
245
+ default: return null;
246
+ }
247
+ }
248
+ function buildServer(remote) {
249
+ const weknora = remote ?? new WeknoraRemote(process.env.WEKNORA_MCP_URL || DEFAULT_WEKNORA_URL, resolveToken);
250
+ const server = new Server({
251
+ name: "bfi-weknora-mcp",
252
+ version: "0.1.0"
253
+ }, {
254
+ capabilities: { tools: {} },
255
+ instructions: [
256
+ `BFI 知识库/案例库通道(bfi cli 登录态鉴权,零配置)。知识库在 scope:`,
257
+ `案例库(诊断案例归档,唯一写目标)+ wiki×4 + jira-支持案例 + 供应链/领域知识。`,
258
+ `· 先验查询:kb_search(跨库语义检索,诊断前必查——历史同类问题/已解决方案)`,
259
+ ` kb_ask(要结论不要文段时)/ kb_grep(确切关键词)`,
260
+ `· 归档:kb_archive_case(诊断完成后把根因链+修复写回案例库,供下次先验命中)`,
261
+ `· 阅读:kb_list/kb_docs/kb_read + wiki_*(生成 wiki 浏览)`
262
+ ].join("\n")
263
+ });
264
+ server.setRequestHandler(ListToolsRequestSchema, async () => ({ tools: TOOLS }));
265
+ server.setRequestHandler(CallToolRequestSchema, async (request) => {
266
+ const { name, arguments: args } = request.params;
267
+ try {
268
+ if (name === "kb_archive_case") {
269
+ const title = String(args?.title ?? "").trim();
270
+ const content = String(args?.content_md ?? "").trim();
271
+ if (!title || !content) return textResult({
272
+ ok: false,
273
+ error_kind: "invalid_params",
274
+ message: "title 与 content_md 必填"
275
+ }, true);
276
+ const md = `# ${title}\n\n${content}\n`;
277
+ return await weknora.callTool("add_document", {
278
+ kb_id: CASE_KB_NAME,
279
+ title,
280
+ content: md
281
+ });
282
+ }
283
+ if (name === "wiki_index" || name === "wiki_read_page" || name === "wiki_search") return await weknora.callTool(name, args ?? {});
284
+ const mapped = await callSemantic(weknora, name, args);
285
+ if (!mapped) return textResult({
286
+ ok: false,
287
+ error_kind: "unknown_tool",
288
+ message: `未知工具:${name}`
289
+ }, true);
290
+ return await weknora.callTool(mapped.tool, mapped.arguments);
291
+ } catch (e) {
292
+ return textResult({
293
+ ok: false,
294
+ error_kind: e instanceof WeknoraError ? e.errorKind : "internal_error",
295
+ message: e instanceof Error ? e.message : String(e)
296
+ }, true);
297
+ }
298
+ });
299
+ return server;
300
+ }
301
+ async function main() {
302
+ await buildServer().connect(new StdioServerTransport());
303
+ }
304
+ main().catch((e) => {
305
+ console.error("[bfi-weknora-mcp] 启动失败:", e);
306
+ process.exit(1);
307
+ });
308
+ export {};
package/package.json CHANGED
@@ -1,12 +1,13 @@
1
1
  {
2
2
  "name": "bfi-cli",
3
- "version": "0.1.95",
3
+ "version": "0.1.96",
4
4
  "description": "用友大业财BFI统一命令行工具,支持技能,技能包,元数据,UI元数据等",
5
5
  "type": "module",
6
6
  "bin": {
7
7
  "bfi": "bin/bfi.js",
8
8
  "bfi-db-mcp": "bin/bfi-db-mcp.js",
9
- "bfi-codesearch-mcp": "bin/bfi-codesearch-mcp.js"
9
+ "bfi-codesearch-mcp": "bin/bfi-codesearch-mcp.js",
10
+ "bfi-weknora-mcp": "bin/bfi-weknora-mcp.js"
10
11
  },
11
12
  "files": [
12
13
  "dist",