@ppdocs/mcp 2.6.19 → 2.6.20

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.
@@ -52,6 +52,8 @@ export declare class PpdocsApiClient {
52
52
  }, creator: string): Promise<Task>;
53
53
  addTaskLog(taskId: string, logType: TaskLogType, content: string): Promise<Task | null>;
54
54
  completeTask(taskId: string, experience: TaskExperience): Promise<Task | null>;
55
+ getRulesApi(ruleType: string): Promise<string[]>;
56
+ saveRulesApi(ruleType: string, rules: string[]): Promise<boolean>;
55
57
  }
56
58
  export declare function initClient(apiUrl: string): void;
57
59
  export declare function listNodes(_projectId: string, filter?: ListNodesFilter): Promise<NodeData[]>;
@@ -92,3 +94,5 @@ export declare function createTask(_projectId: string, task: {
92
94
  }, creator: string): Promise<Task>;
93
95
  export declare function addTaskLog(_projectId: string, taskId: string, logType: TaskLogType, content: string): Promise<Task | null>;
94
96
  export declare function completeTask(_projectId: string, taskId: string, experience: TaskExperience): Promise<Task | null>;
97
+ export declare function getRules(_projectId: string, ruleType: string): Promise<string[]>;
98
+ export declare function saveRules(_projectId: string, ruleType: string, rules: string[]): Promise<boolean>;
@@ -339,6 +339,27 @@ export class PpdocsApiClient {
339
339
  return null;
340
340
  }
341
341
  }
342
+ // ============ 规则 API (独立文件存储) ============
343
+ async getRulesApi(ruleType) {
344
+ try {
345
+ return await this.request(`/rules/${ruleType}`);
346
+ }
347
+ catch {
348
+ return [];
349
+ }
350
+ }
351
+ async saveRulesApi(ruleType, rules) {
352
+ try {
353
+ await this.request(`/rules/${ruleType}`, {
354
+ method: 'PUT',
355
+ body: JSON.stringify(rules)
356
+ });
357
+ return true;
358
+ }
359
+ catch {
360
+ return false;
361
+ }
362
+ }
342
363
  }
343
364
  // ============ 模块级 API (兼容现有 tools/index.ts) ============
344
365
  let client = null;
@@ -401,3 +422,10 @@ export async function addTaskLog(_projectId, taskId, logType, content) {
401
422
  export async function completeTask(_projectId, taskId, experience) {
402
423
  return getClient().completeTask(taskId, experience);
403
424
  }
425
+ // ============ 规则管理 (独立文件存储) ============
426
+ export async function getRules(_projectId, ruleType) {
427
+ return getClient().getRulesApi(ruleType);
428
+ }
429
+ export async function saveRules(_projectId, ruleType, rules) {
430
+ return getClient().saveRulesApi(ruleType, rules);
431
+ }
@@ -1,6 +1,6 @@
1
1
  import { z } from 'zod';
2
2
  import * as storage from '../storage/httpClient.js';
3
- import { decodeUnicodeEscapes, decodeObjectStrings, wrapResult, clearStyleCache, getRules, RULE_TYPE_LABELS } from '../utils.js';
3
+ import { decodeUnicodeEscapes, decodeObjectStrings, wrapResult, getRules, RULE_TYPE_LABELS } from '../utils.js';
4
4
  // 辅助函数: 包装返回结果
5
5
  async function wrap(projectId, text) {
6
6
  const wrapped = await wrapResult(projectId, text);
@@ -90,33 +90,19 @@ export function registerTools(server, projectId, _user) {
90
90
  const node = await storage.updateNode(projectId, nodeId, updates);
91
91
  return wrap(projectId, node ? JSON.stringify(node, null, 2) : '更新失败(节点不存在或已锁定)');
92
92
  });
93
- // 3.5 更新根节点 (专用方法,支持所有规则)
94
- server.tool('kg_update_root', '更新根节点(项目规则,锁定时不可更新)', {
93
+ // 3.5 更新根节点 (项目介绍)
94
+ server.tool('kg_update_root', '更新项目介绍(根节点描述,锁定时不可更新)', {
95
95
  title: z.string().optional().describe('项目标题'),
96
- description: z.string().optional().describe('项目描述(Markdown)'),
97
- userStyles: z.array(z.string()).optional().describe('项目规则(字符串数组)'),
98
- testRules: z.array(z.string()).optional().describe('测试规则(字符串数组)'),
99
- reviewRules: z.array(z.string()).optional().describe('审查规则(字符串数组)'),
100
- codeStyle: z.array(z.string()).optional().describe('编码风格(字符串数组)')
96
+ description: z.string().optional().describe('项目介绍(Markdown)')
101
97
  }, async (args) => {
102
- // 空参数检查
103
- const hasUpdate = args.title !== undefined || args.description !== undefined ||
104
- args.userStyles !== undefined || args.testRules !== undefined ||
105
- args.reviewRules !== undefined || args.codeStyle !== undefined;
106
- if (!hasUpdate) {
107
- return wrap(projectId, '❌ 请至少提供一个更新参数');
98
+ if (args.title === undefined && args.description === undefined) {
99
+ return wrap(projectId, '❌ 请至少提供 title description');
108
100
  }
109
101
  const node = await storage.updateRoot(projectId, {
110
102
  title: args.title,
111
- description: args.description,
112
- userStyles: args.userStyles,
113
- testRules: args.testRules,
114
- reviewRules: args.reviewRules,
115
- codeStyle: args.codeStyle
103
+ description: args.description
116
104
  });
117
- if (node)
118
- clearStyleCache();
119
- return wrap(projectId, node ? JSON.stringify(node, null, 2) : '更新失败(根节点已锁定)');
105
+ return wrap(projectId, node ? '✅ 项目介绍已更新' : '更新失败(根节点已锁定)');
120
106
  });
121
107
  // 3.6 获取项目规则 (独立方法,按需调用)
122
108
  server.tool('kg_get_rules', '获取项目规则(可指定类型或获取全部)', {
@@ -130,6 +116,18 @@ export function registerTools(server, projectId, _user) {
130
116
  }
131
117
  return { content: [{ type: 'text', text: rules }] };
132
118
  });
119
+ // 3.7 保存项目规则 (独立存储)
120
+ server.tool('kg_save_rules', '保存单个类型的项目规则(独立文件存储)', {
121
+ ruleType: z.enum(['userStyles', 'testRules', 'reviewRules', 'codeStyle'])
122
+ .describe('规则类型: userStyles=项目规则, testRules=测试规则, reviewRules=审查规则, codeStyle=编码风格'),
123
+ rules: z.array(z.string()).describe('规则数组')
124
+ }, async (args) => {
125
+ const success = await storage.saveRules(projectId, args.ruleType, args.rules);
126
+ if (!success) {
127
+ return wrap(projectId, '❌ 保存失败');
128
+ }
129
+ return wrap(projectId, `✅ ${RULE_TYPE_LABELS[args.ruleType]}已保存 (${args.rules.length} 条)`);
130
+ });
133
131
  // 4. 锁定节点 (只能锁定,解锁需用户在前端手动操作)
134
132
  server.tool('kg_lock_node', '锁定节点(锁定后只能读取,解锁需用户在前端手动操作)', {
135
133
  nodeId: z.string().describe('节点ID')
@@ -425,4 +423,40 @@ export function registerTools(server, projectId, _user) {
425
423
  }
426
424
  return wrap(projectId, `任务已完成归档: ${task.title}`);
427
425
  });
426
+ // 15. 查询文件关联的节点
427
+ server.tool('kg_find_by_file', '查询哪些节点绑定了指定文件(删除/重命名前检查)', {
428
+ filePath: z.string().describe('文件相对路径,如 src/utils/format.ts')
429
+ }, async (args) => {
430
+ const nodes = await storage.listNodes(projectId);
431
+ const filePath = args.filePath.replace(/\\/g, '/'); // 统一为正斜杠
432
+ // 查找所有绑定了该文件的节点
433
+ const boundBy = [];
434
+ for (const node of nodes) {
435
+ if (node.relatedFiles && node.relatedFiles.length > 0) {
436
+ // 检查是否包含该文件 (支持部分匹配)
437
+ const hasFile = node.relatedFiles.some(f => {
438
+ const normalizedF = f.replace(/\\/g, '/');
439
+ return normalizedF === filePath ||
440
+ normalizedF.endsWith('/' + filePath) ||
441
+ filePath.endsWith('/' + normalizedF);
442
+ });
443
+ if (hasFile) {
444
+ boundBy.push({
445
+ id: node.id,
446
+ title: node.title,
447
+ type: node.type,
448
+ status: node.status
449
+ });
450
+ }
451
+ }
452
+ }
453
+ const result = {
454
+ file: filePath,
455
+ boundBy,
456
+ message: boundBy.length > 0
457
+ ? `⚠️ 该文件被 ${boundBy.length} 个节点引用,删除前请更新节点`
458
+ : '✅ 该文件无 KG 引用,可安全操作'
459
+ };
460
+ return wrap(projectId, JSON.stringify(result, null, 2));
461
+ });
428
462
  }
package/dist/utils.d.ts CHANGED
@@ -4,7 +4,7 @@
4
4
  export type RuleType = 'userStyles' | 'testRules' | 'reviewRules' | 'codeStyle';
5
5
  export declare const RULE_TYPE_LABELS: Record<RuleType, string>;
6
6
  /**
7
- * 获取指定类型的规则
7
+ * 获取指定类型的规则 (从独立文件读取)
8
8
  */
9
9
  export declare function getRules(projectId: string, ruleType?: RuleType): Promise<string>;
10
10
  /**
@@ -12,7 +12,7 @@ export declare function getRules(projectId: string, ruleType?: RuleType): Promis
12
12
  */
13
13
  export declare function getRootStyle(projectId: string): Promise<string>;
14
14
  /**
15
- * 清除规则缓存 (当根节点更新时调用)
15
+ * 清除规则缓存 (保留接口兼容性,实际无需缓存)
16
16
  */
17
17
  export declare function clearStyleCache(): void;
18
18
  /**
package/dist/utils.js CHANGED
@@ -9,9 +9,6 @@ export const RULE_TYPE_LABELS = {
9
9
  reviewRules: '审查规则',
10
10
  codeStyle: '编码风格',
11
11
  };
12
- // 缓存根节点 (避免每次调用都请求)
13
- let cachedRootNode = null;
14
- let cacheProjectId = null;
15
12
  /**
16
13
  * 将字符串数组格式化为 Markdown 列表
17
14
  */
@@ -21,32 +18,12 @@ function formatRulesList(rules) {
21
18
  return rules.map((s, i) => `${i + 1}. ${s}`).join('\n\n');
22
19
  }
23
20
  /**
24
- * 获取根节点数据 (带缓存)
25
- */
26
- async function getCachedRoot(projectId) {
27
- if (cachedRootNode !== null && cacheProjectId === projectId) {
28
- return cachedRootNode;
29
- }
30
- try {
31
- const rootNode = await storage.getNode(projectId, 'root');
32
- cachedRootNode = rootNode;
33
- cacheProjectId = projectId;
34
- return cachedRootNode;
35
- }
36
- catch {
37
- return null;
38
- }
39
- }
40
- /**
41
- * 获取指定类型的规则
21
+ * 获取指定类型的规则 (从独立文件读取)
42
22
  */
43
23
  export async function getRules(projectId, ruleType) {
44
- const rootNode = await getCachedRoot(projectId);
45
- if (!rootNode)
46
- return '';
47
24
  // 如果指定了类型,只返回该类型
48
25
  if (ruleType) {
49
- const rules = rootNode[ruleType];
26
+ const rules = await storage.getRules(projectId, ruleType);
50
27
  if (!rules || rules.length === 0)
51
28
  return '';
52
29
  return `[${RULE_TYPE_LABELS[ruleType]}]\n${formatRulesList(rules)}`;
@@ -54,7 +31,7 @@ export async function getRules(projectId, ruleType) {
54
31
  // 返回所有规则
55
32
  const allRules = [];
56
33
  for (const type of Object.keys(RULE_TYPE_LABELS)) {
57
- const rules = rootNode[type];
34
+ const rules = await storage.getRules(projectId, type);
58
35
  if (rules && rules.length > 0) {
59
36
  allRules.push(`[${RULE_TYPE_LABELS[type]}]\n${formatRulesList(rules)}`);
60
37
  }
@@ -68,11 +45,10 @@ export async function getRootStyle(projectId) {
68
45
  return getRules(projectId, 'userStyles');
69
46
  }
70
47
  /**
71
- * 清除规则缓存 (当根节点更新时调用)
48
+ * 清除规则缓存 (保留接口兼容性,实际无需缓存)
72
49
  */
73
50
  export function clearStyleCache() {
74
- cachedRootNode = null;
75
- cacheProjectId = null;
51
+ // 独立文件存储无需缓存
76
52
  }
77
53
  /**
78
54
  * 包装工具返回结果 (不再自动注入规则,使用 kg_get_rules 按需获取)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ppdocs/mcp",
3
- "version": "2.6.19",
3
+ "version": "2.6.20",
4
4
  "description": "ppdocs MCP Server - Knowledge Graph for Claude",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",