@hupan56/wlkj 3.1.32 → 3.3.0

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.
Files changed (178) hide show
  1. package/bin/cli.js +117 -0
  2. package/package.json +1 -1
  3. package/templates/qoder/agents/insight-planning.md +67 -67
  4. package/templates/qoder/agents/prd-reference.md +47 -47
  5. package/templates/qoder/commands/optional/wl-insight.md +4 -4
  6. package/templates/qoder/commands/optional/wl-report.md +1 -1
  7. package/templates/qoder/commands/optional/wl-spec.md +23 -3
  8. package/templates/qoder/commands/optional/wl-status.md +12 -1
  9. package/templates/qoder/commands/wl-code.md +138 -7
  10. package/templates/qoder/commands/wl-commit.md +12 -1
  11. package/templates/qoder/commands/wl-design.md +70 -6
  12. package/templates/qoder/commands/wl-init.md +27 -0
  13. package/templates/qoder/commands/wl-prd.md +230 -15
  14. package/templates/qoder/commands/wl-req.md +10 -3
  15. package/templates/qoder/commands/wl-search.md +74 -20
  16. package/templates/qoder/commands/wl-task.md +3 -3
  17. package/templates/qoder/commands/wl-test.md +17 -2
  18. package/templates/qoder/contracts/CHANGELOG.md +418 -0
  19. package/templates/qoder/contracts/README.md +180 -0
  20. package/templates/qoder/contracts/code.md +82 -0
  21. package/templates/qoder/contracts/commit.md +86 -0
  22. package/templates/qoder/contracts/contract-header.md +76 -0
  23. package/templates/qoder/contracts/design.md +106 -0
  24. package/templates/qoder/contracts/fallback.md +126 -0
  25. package/templates/qoder/contracts/isolation.md +119 -0
  26. package/templates/qoder/contracts/prd.md +118 -0
  27. package/templates/qoder/contracts/schemas/design-spec.schema.json +46 -0
  28. package/templates/qoder/contracts/schemas/prd.schema.json +36 -0
  29. package/templates/qoder/contracts/schemas/test-cases.schema.json +40 -0
  30. package/templates/qoder/contracts/spec.md +116 -0
  31. package/templates/qoder/contracts/task.md +125 -0
  32. package/templates/qoder/contracts/test.md +112 -0
  33. package/templates/qoder/hooks/post-tool-use.py +61 -0
  34. package/templates/qoder/hooks/session-start.py +34 -66
  35. package/templates/qoder/hooks/stop-eval.py +47 -0
  36. package/templates/qoder/rules/wl-pipeline.md +37 -0
  37. package/templates/qoder/scripts/capability/__pycache__/__init__.cpython-39.pyc +0 -0
  38. package/templates/qoder/scripts/capability/__pycache__/registry.cpython-39.pyc +0 -0
  39. package/templates/qoder/scripts/capability/__pycache__/registry_mcp.cpython-39.pyc +0 -0
  40. package/templates/qoder/scripts/capability/adapters/__init__.py +1 -1
  41. package/templates/qoder/scripts/capability/adapters/__pycache__/__init__.cpython-39.pyc +0 -0
  42. package/templates/qoder/scripts/capability/adapters/__pycache__/cli.cpython-39.pyc +0 -0
  43. package/templates/qoder/scripts/capability/adapters/__pycache__/mcp.cpython-39.pyc +0 -0
  44. package/templates/qoder/scripts/capability/adapters/__pycache__/qw.cpython-39.pyc +0 -0
  45. package/templates/qoder/scripts/capability/adapters/mcp.py +76 -100
  46. package/templates/qoder/scripts/capability/adapters/qw.py +295 -295
  47. package/templates/qoder/scripts/capability/caps/__init__.py +1 -1
  48. package/templates/qoder/scripts/capability/caps/__pycache__/__init__.cpython-39.pyc +0 -0
  49. package/templates/qoder/scripts/capability/caps/__pycache__/context.cpython-39.pyc +0 -0
  50. package/templates/qoder/scripts/capability/caps/__pycache__/cron.cpython-39.pyc +0 -0
  51. package/templates/qoder/scripts/capability/caps/__pycache__/identity.cpython-39.pyc +0 -0
  52. package/templates/qoder/scripts/capability/caps/__pycache__/memory.cpython-39.pyc +0 -0
  53. package/templates/qoder/scripts/capability/caps/__pycache__/notify.cpython-39.pyc +0 -0
  54. package/templates/qoder/scripts/capability/caps/__pycache__/present.cpython-39.pyc +0 -0
  55. package/templates/qoder/scripts/capability/caps/__pycache__/repo.cpython-39.pyc +0 -0
  56. package/templates/qoder/scripts/capability/caps/__pycache__/sandbox.cpython-39.pyc +0 -0
  57. package/templates/qoder/scripts/capability/caps/memory.py +1 -1
  58. package/templates/qoder/scripts/capability/registry.py +21 -23
  59. package/templates/qoder/scripts/capability/registry_mcp.py +69 -5
  60. package/templates/qoder/scripts/capability/smoke_test_report.json +34 -20
  61. package/templates/qoder/scripts/deployment/setup/carriers.py +3 -1
  62. package/templates/qoder/scripts/deployment/setup/init_doctor.py +10 -3
  63. package/templates/qoder/scripts/deployment/setup/install_qoderwork.py +11 -0
  64. package/templates/qoder/scripts/deployment/setup/wlkj_shim.py +104 -0
  65. package/templates/qoder/scripts/domain/__pycache__/__init__.cpython-39.pyc +0 -0
  66. package/templates/qoder/scripts/domain/deployment/deploy_to_test.py +298 -0
  67. package/templates/qoder/scripts/domain/integration/__init__.py +0 -0
  68. package/templates/qoder/scripts/domain/integration/__pycache__/__init__.cpython-39.pyc +0 -0
  69. package/templates/qoder/scripts/domain/integration/__pycache__/return_to_platform.cpython-39.pyc +0 -0
  70. package/templates/qoder/scripts/domain/integration/return_to_platform.py +392 -0
  71. package/templates/qoder/scripts/domain/integration/spec_upload.py +209 -0
  72. package/templates/qoder/scripts/domain/kg/build/kg_build.py +268 -25
  73. package/templates/qoder/scripts/domain/kg/build/kg_incremental.py +108 -3
  74. package/templates/qoder/scripts/domain/kg/build/kg_signatures.py +169 -0
  75. package/templates/qoder/scripts/domain/kg/extract/asset/__init__.py +10 -0
  76. package/templates/qoder/scripts/domain/kg/extract/asset/asset_tree.py +57 -0
  77. package/templates/qoder/scripts/domain/kg/extract/asset/discussion_importer.py +62 -0
  78. package/templates/qoder/scripts/domain/kg/extract/asset/prd_importer.py +146 -0
  79. package/templates/qoder/scripts/domain/kg/extract/asset/prototype_importer.py +64 -0
  80. package/templates/qoder/scripts/domain/kg/extract/asset/returns_importer.py +52 -0
  81. package/templates/qoder/scripts/domain/kg/extract/build_goal3.py +104 -0
  82. package/templates/qoder/scripts/domain/kg/extract/build_goal4.py +55 -0
  83. package/templates/qoder/scripts/domain/kg/extract/build_goal5.py +95 -0
  84. package/templates/qoder/scripts/domain/kg/extract/db/__init__.py +8 -0
  85. package/templates/qoder/scripts/domain/kg/extract/db/data_profile.py +22 -0
  86. package/templates/qoder/scripts/domain/kg/extract/db/fk_extractor.py +55 -0
  87. package/templates/qoder/scripts/domain/kg/extract/db/schema_extractor.py +90 -0
  88. package/templates/qoder/scripts/domain/kg/extract/extract.py +84 -0
  89. package/templates/qoder/scripts/domain/kg/extract/extract.py.bak +430 -0
  90. package/templates/qoder/scripts/domain/kg/extract/inference/__init__.py +9 -0
  91. package/templates/qoder/scripts/domain/kg/extract/inference/community_summarizer.py +206 -0
  92. package/templates/qoder/scripts/domain/kg/extract/inference/embed_builder.py +132 -0
  93. package/templates/qoder/scripts/domain/kg/extract/inference/naming_matcher.py +80 -0
  94. package/templates/qoder/scripts/domain/kg/extract/inference/promote.py +59 -0
  95. package/templates/qoder/scripts/domain/kg/extract/inference/recompute.py +93 -0
  96. package/templates/qoder/scripts/domain/kg/extract/inference/weak_link.py +421 -0
  97. package/templates/qoder/scripts/domain/kg/extract/java/__init__.py +15 -0
  98. package/templates/qoder/scripts/domain/kg/extract/java/_parser.py +271 -0
  99. package/templates/qoder/scripts/domain/kg/extract/java/all.py +145 -0
  100. package/templates/qoder/scripts/domain/kg/extract/java/build_java_to_pg.py +102 -0
  101. package/templates/qoder/scripts/domain/kg/extract/java/call_chain.py +49 -0
  102. package/templates/qoder/scripts/domain/kg/extract/java/class_extractor.py +141 -0
  103. package/templates/qoder/scripts/domain/kg/extract/java/domain_extractor.py +148 -0
  104. package/templates/qoder/scripts/domain/kg/extract/java/dubbo_extractor.py +33 -0
  105. package/templates/qoder/scripts/domain/kg/extract/java/endpoint_extractor.py +36 -0
  106. package/templates/qoder/scripts/domain/kg/extract/java/javadoc_extractor.py +110 -0
  107. package/templates/qoder/scripts/domain/kg/extract/java/llm_cn_filler.py +150 -0
  108. package/templates/qoder/scripts/domain/kg/extract/java/member_extractor.py +157 -0
  109. package/templates/qoder/scripts/domain/kg/extract/java/mybatisplus_extractor.py +34 -0
  110. package/templates/qoder/scripts/domain/kg/extract/java/pg_upsert.py +165 -0
  111. package/templates/qoder/scripts/domain/kg/extract/java/satoken_extractor.py +30 -0
  112. package/templates/qoder/scripts/domain/kg/extract/java/spring_extractor.py +39 -0
  113. package/templates/qoder/scripts/domain/kg/extract/java/validation_extractor.py +33 -0
  114. package/templates/qoder/scripts/domain/kg/extract/mybatis/__init__.py +9 -0
  115. package/templates/qoder/scripts/domain/kg/extract/mybatis/all.py +79 -0
  116. package/templates/qoder/scripts/domain/kg/extract/mybatis/mapper_parser.py +99 -0
  117. package/templates/qoder/scripts/domain/kg/extract/mybatis/relation_builder.py +69 -0
  118. package/templates/qoder/scripts/domain/kg/extract/mybatis/sql_extractor.py +78 -0
  119. package/templates/qoder/scripts/domain/kg/extract/prd/__init__.py +8 -0
  120. package/templates/qoder/scripts/domain/kg/extract/prd/prd_chunk_embed.py +105 -0
  121. package/templates/qoder/scripts/domain/kg/extract/prd/prd_llm_extract.py +153 -0
  122. package/templates/qoder/scripts/domain/kg/extract/prd/req_anchor.py +120 -0
  123. package/templates/qoder/scripts/domain/kg/extract/ts_extract.py +111 -0
  124. package/templates/qoder/scripts/domain/kg/graph/kg_semantic.py +4 -2
  125. package/templates/qoder/scripts/domain/kg/kg.py +42 -5
  126. package/templates/qoder/scripts/domain/kg/search/_remote.py +187 -0
  127. package/templates/qoder/scripts/domain/kg/search/context_pack.py +32 -2
  128. package/templates/qoder/scripts/domain/kg/search/search_index.py +74 -20
  129. package/templates/qoder/scripts/domain/kg/storage/kg_duckdb.py +43 -0
  130. package/templates/qoder/scripts/domain/kg/switch_project.py +159 -0
  131. package/templates/qoder/scripts/domain/kg/sync_repowiki.py +109 -0
  132. package/templates/qoder/scripts/domain/requirement/req.py +134 -28
  133. package/templates/qoder/scripts/domain/task/__pycache__/wlkj_panel.cpython-39.pyc +0 -0
  134. package/templates/qoder/scripts/domain/task/wlkj_panel.py +1348 -0
  135. package/templates/qoder/scripts/engine/poller.py +219 -0
  136. package/templates/qoder/scripts/foundation/__pycache__/__init__.cpython-39.pyc +0 -0
  137. package/templates/qoder/scripts/foundation/core/__pycache__/__init__.cpython-39.pyc +0 -0
  138. package/templates/qoder/scripts/foundation/core/__pycache__/paths.cpython-39.pyc +0 -0
  139. package/templates/qoder/scripts/foundation/core/paths.py +102 -0
  140. package/templates/qoder/scripts/foundation/integrations/active_task.py +2 -1
  141. package/templates/qoder/scripts/orchestration/wlkj.py +4 -0
  142. package/templates/qoder/scripts/protocol/__pycache__/__init__.cpython-39.pyc +0 -0
  143. package/templates/qoder/scripts/protocol/mcp/zentao_mcp_server.py +24 -11
  144. package/templates/qoder/scripts/protocol/transports/__pycache__/__init__.cpython-39.pyc +0 -0
  145. package/templates/qoder/scripts/protocol/transports/__pycache__/base.cpython-39.pyc +0 -0
  146. package/templates/qoder/scripts/protocol/transports/__pycache__/cli.cpython-39.pyc +0 -0
  147. package/templates/qoder/scripts/protocol/transports/__pycache__/http.cpython-39.pyc +0 -0
  148. package/templates/qoder/scripts/protocol/transports/__pycache__/stdio.cpython-39.pyc +0 -0
  149. package/templates/qoder/scripts/protocol/transports/http.py +7 -1
  150. package/templates/qoder/scripts/validation/eval/qwork_harness.py +1 -1
  151. package/templates/qoder/scripts/validation/eval/report-commands.md +2 -2
  152. package/templates/qoder/settings.json +27 -9
  153. package/templates/qoder/skills/design-import/SKILL.md +3 -3
  154. package/templates/qoder/skills/design-review/SKILL.md +1 -1
  155. package/templates/qoder/skills/prd-generator/SKILL.md +4 -4
  156. package/templates/qoder/skills/prd-review/SKILL.md +1 -1
  157. package/templates/qoder/skills/prototype-generator/SKILL.md +3 -3
  158. package/templates/qoder/skills/spec-coder/SKILL.md +1 -1
  159. package/templates/qoder/skills/spec-generator/SKILL.md +80 -23
  160. package/templates/qoder/skills/test-generator/SKILL.md +1 -1
  161. package/templates/qoder/skills/wl-code/SKILL.md +13 -1
  162. package/templates/qoder/skills/wl-commit/SKILL.md +1 -1
  163. package/templates/qoder/skills/wl-design/SKILL.md +6 -6
  164. package/templates/qoder/skills/wl-init/SKILL.md +2 -2
  165. package/templates/qoder/skills/wl-insight/SKILL.md +5 -5
  166. package/templates/qoder/skills/wl-prd/SKILL.md +60 -0
  167. package/templates/qoder/skills/wl-report/SKILL.md +2 -2
  168. package/templates/qoder/skills/wl-search/SKILL.md +1 -1
  169. package/templates/qoder/skills/wl-spec/SKILL.md +2 -2
  170. package/templates/qoder/skills/wl-status/SKILL.md +2 -2
  171. package/templates/qoder/skills/wl-task/SKILL.md +3 -3
  172. package/templates/qoder/skills/wl-test/SKILL.md +2 -2
  173. package/templates/qoder/templates/spec-template.md +124 -0
  174. package/templates/root/AGENTS.md +41 -14
  175. package/templates/qoder/scripts/domain/task/zentao_panel.py +0 -451
  176. package/templates/qoder/skills/wl-prd-full/SKILL.md +0 -121
  177. package/templates/qoder/skills/wl-prd-quick/SKILL.md +0 -50
  178. package/templates/qoder/skills/wl-prd-review/SKILL.md +0 -47
@@ -0,0 +1,69 @@
1
+ # -*- coding: utf-8 -*-
2
+ """Mapper ↔ Entity ↔ Table 三向关联构建器。
3
+
4
+ 把 mapper_parser 的结构化信息 + sql_extractor 的表名 →
5
+ 生成 entities (TABLE/COLUMN) + edges (queries/maps_to/returns)。
6
+ """
7
+ from __future__ import annotations
8
+ from . import mapper_parser as mp
9
+ from . import sql_extractor as sq
10
+
11
+
12
+ def build_relations(parsed: dict, repo_id: str, filepath: str):
13
+ """从单个 mapper XML 的解析结果构建实体 + 边。
14
+
15
+ Returns: (entities, edges)
16
+ entities: TABLE / COLUMN 实体
17
+ edges: queries (Mapper→Table) / column_maps (COLUMN 关联)
18
+ """
19
+ entities, edges = [], []
20
+ ns_short = parsed.get('namespace_short', '')
21
+ ns_full = parsed.get('namespace', '')
22
+ if not ns_short:
23
+ return entities, edges
24
+
25
+ # Mapper 实体 id (对齐 GOAL 2 的 entity_id 规则: MAP:repo:Name)
26
+ mapper_id = 'MAP:%s:%s' % (repo_id, ns_short)
27
+
28
+ # ===== queries 边: Mapper → 每个被访问的 TABLE =====
29
+ all_tables = set()
30
+ for stmt in parsed.get('statements', []):
31
+ for tbl in stmt.get('tables', []):
32
+ all_tables.add(tbl)
33
+
34
+ for tbl in all_tables:
35
+ tbl_id = 'TBL:%s:%s' % (repo_id, tbl)
36
+ # TABLE 实体 (如果 GOAL 2 没建过, 这里补建)
37
+ entities.append({
38
+ 'id': tbl_id, 'repo_id': repo_id, 'type': 'TABLE',
39
+ 'canonical': tbl, 'cn': '',
40
+ 'props': {'from': 'mapper_xml', 'file': filepath},
41
+ })
42
+ # queries 边
43
+ edges.append({
44
+ 'from_id': mapper_id, 'to_id': tbl_id,
45
+ 'edge_type': 'queries',
46
+ 'from_repo': repo_id, 'to_repo': repo_id,
47
+ 'confidence': 1.0, 'source': 'mapper_xml',
48
+ 'props': {'via': 'mybatis_xml'},
49
+ })
50
+
51
+ # ===== COLUMN 实体 (从 resultMap 提取) =====
52
+ for rm_id, cols in parsed.get('result_maps', {}).items():
53
+ for col_map in cols:
54
+ col_name = col_map.get('column', '')
55
+ if not col_name or '$' in col_name:
56
+ continue
57
+ # COLUMN 实体 (关联到第一个被访问的表, 兜底)
58
+ hint_table = list(all_tables)[:1]
59
+ tbl_for_col = hint_table[0] if hint_table else 'unknown'
60
+ col_id = 'COL:%s:%s.%s' % (repo_id, tbl_for_col, col_name)
61
+ entities.append({
62
+ 'id': col_id, 'repo_id': repo_id, 'type': 'COLUMN',
63
+ 'canonical': col_name, 'cn': '',
64
+ 'props': {'table': tbl_for_col,
65
+ 'property': col_map.get('property', ''),
66
+ 'from': 'result_map', 'file': filepath},
67
+ })
68
+
69
+ return entities, edges
@@ -0,0 +1,78 @@
1
+ # -*- coding: utf-8 -*-
2
+ """SQL 文本提取器: 从 SQL 提取表名 + 列引用 + 操作类型。
3
+
4
+ 正则匹配 MyBatis 动态 SQL 里残留的 ${ew.customSqlSegment} 等,
5
+ 但 FROM/INTO/UPDATE/JOIN 后的表名通常稳定可提。
6
+ """
7
+ from __future__ import annotations
8
+ import re
9
+
10
+ # 表名提取 (FROM/JOIN/INTO/UPDATE 后跟标识符)
11
+ _TABLE_PATTERNS = [
12
+ re.compile(r'\bFROM\s+`?([a-z_][a-z0-9_]*)`?', re.IGNORECASE),
13
+ re.compile(r'\bJOIN\s+`?([a-z_][a-z0-9_]*)`?', re.IGNORECASE),
14
+ re.compile(r'\bINTO\s+`?([a-z_][a-z0-9_]*)`?', re.IGNORECASE),
15
+ re.compile(r'\bUPDATE\s+`?([a-z_][a-z0-9_]*)`?\s+SET', re.IGNORECASE),
16
+ ]
17
+
18
+ # 排除: 显然不是表名的 (变量/占位符/SQL关键字)
19
+ _NOT_TABLES = {'dual', 'select', 'where', 'set', 'values', 'from', 'join',
20
+ 'and', 'or', 'not', 'in', 'as', 'on', 'left', 'right', 'inner',
21
+ 'outer', 'group', 'order', 'limit', 'distinct', 'case', 'when',
22
+ 'then', 'else', 'end', 'null', 'true', 'false'}
23
+
24
+
25
+ def extract_table_names(sql: str) -> list:
26
+ """从 SQL 文本提取所有被访问的表名。
27
+
28
+ Returns: ['test_demo', 'sys_user'] (去重保序)
29
+ """
30
+ if not sql:
31
+ return []
32
+ found = []
33
+ seen = set()
34
+ for pat in _TABLE_PATTERNS:
35
+ for m in pat.finditer(sql):
36
+ tbl = m.group(1).lower()
37
+ if tbl in _NOT_TABLES or tbl.startswith('$') or tbl.startswith('#'):
38
+ continue
39
+ # 排除太短或含特殊字符的 (变量插值残留)
40
+ if len(tbl) < 2 or '$' in tbl or '{' in tbl:
41
+ continue
42
+ if tbl not in seen:
43
+ seen.add(tbl)
44
+ found.append(tbl)
45
+ return found
46
+
47
+
48
+ def extract_column_refs(sql: str) -> list:
49
+ """提取 SQL 里引用的列名 (粗粒度, 主要看 SELECT 字段列表)。
50
+
51
+ Returns: ['id', 'user_id', 'name'] (去重)
52
+ """
53
+ if not sql:
54
+ return []
55
+ cols = set()
56
+ # SELECT field1, field2 FROM ... (字段列表)
57
+ m = re.search(r'SELECT\s+(DISTINCT\s+)?(.*?)\s+FROM', sql, re.IGNORECASE | re.DOTALL)
58
+ if m:
59
+ field_part = m.group(2)
60
+ if field_part.strip() != '*':
61
+ for token in re.split(r'[,\s]+', field_part):
62
+ token = token.strip().strip('`')
63
+ # 别名 as xx → 取前半
64
+ token = token.split(' as ')[0].split(' AS ')[0].strip()
65
+ if re.match(r'^[a-z_][a-z0-9_]*$', token, re.I) and len(token) >= 2:
66
+ cols.add(token.lower())
67
+ # xx.column 形式 (表别名.列)
68
+ for m in re.finditer(r'\b[a-z]+\s*\.\s*([a-z_][a-z0-9_]*)', sql, re.IGNORECASE):
69
+ cols.add(m.group(1).lower())
70
+ return sorted(cols)
71
+
72
+
73
+ def classify_operation(stmt_type: str) -> str:
74
+ """MyBatis statement 类型 → 操作分类。"""
75
+ return {
76
+ 'select': 'READ', 'insert': 'WRITE',
77
+ 'update': 'WRITE', 'delete': 'WRITE',
78
+ }.get(stmt_type.lower(), 'UNKNOWN')
@@ -0,0 +1,8 @@
1
+ # -*- coding: utf-8 -*-
2
+ """PRD 业务知识提取 (GOAL 4)。
3
+
4
+ 子模块:
5
+ prd_llm_extract PRD 正文 → LLM 抽功能点/字段/规则/端点/权限
6
+ prd_chunk_embed PRD 分块 + embedding (LightRAG vector 路)
7
+ req_anchor REQ-ID 强关联 (PRD/commit/代码注释 → anchors 表)
8
+ """
@@ -0,0 +1,105 @@
1
+ # -*- coding: utf-8 -*-
2
+ """PRD 分块 + embedding → LightRAG vector 路。
3
+
4
+ 让 rag_search 能搜到 PRD 正文 (语义召回), 不再只搜标题。
5
+ 复用工作台 rag.py 的 embed_texts (DashScope text-embedding-v3, 1024维)。
6
+ """
7
+ from __future__ import annotations
8
+ import os
9
+ import sys
10
+
11
+ _THIS = os.path.dirname(os.path.abspath(__file__))
12
+ for _i in range(8):
13
+ _p = os.path.dirname(_THIS)
14
+ if os.path.isfile(os.path.join(_p, 'foundation', 'bootstrap.py')):
15
+ sys.path.insert(0, _p); break
16
+ _THIS = _p
17
+
18
+
19
+ def chunk_text(text: str, size: int = 500, overlap: int = 50) -> list:
20
+ """滑窗分块。Returns: [chunk1, chunk2, ...]"""
21
+ if not text:
22
+ return []
23
+ chunks = []
24
+ i = 0
25
+ while i < len(text):
26
+ chunk = text[i:i + size]
27
+ chunks.append(chunk)
28
+ i += size - overlap
29
+ return chunks
30
+
31
+
32
+ def embed_prd(prd_id: str, content_md: str, repo_id: str = ''):
33
+ """PRD 分块 + embedding → 写 PG embeddings 表。
34
+
35
+ Returns: {chunks, embedded} 失败返回 {'chunks':0,'embedded':0}
36
+ """
37
+ chunks = chunk_text(content_md)
38
+ if not chunks:
39
+ return {'chunks': 0, 'embedded': 0}
40
+
41
+ # 复用工作台 rag.py 的 embed_texts
42
+ try:
43
+ ws_backend = os.path.join(os.getcwd(), 'wlinkj-workspace', 'backend')
44
+ sys.path.insert(0, ws_backend)
45
+ from app.rag import embed_texts
46
+ except Exception:
47
+ # 兜底: 直接调 DashScope embedding API
48
+ return _embed_fallback(prd_id, chunks, repo_id)
49
+
50
+ vecs = embed_texts(chunks)
51
+ if not vecs:
52
+ return {'chunks': len(chunks), 'embedded': 0}
53
+
54
+ # 写 PG embeddings 表
55
+ _upsert_embeddings(prd_id, chunks, vecs, repo_id)
56
+ return {'chunks': len(chunks), 'embedded': len(vecs)}
57
+
58
+
59
+ def _embed_fallback(prd_id, chunks, repo_id):
60
+ """直接调 DashScope text-embedding-v3 (rag.py 不可用时)。"""
61
+ import json, urllib.request
62
+ key = os.environ.get('DASHSCOPE_API_KEY', '')
63
+ if not key:
64
+ return {'chunks': len(chunks), 'embedded': 0}
65
+ # DashScope embedding API
66
+ url = 'https://dashscope.aliyuncs.com/compatible-mode/v1/embeddings'
67
+ try:
68
+ req = urllib.request.Request(
69
+ url,
70
+ data=json.dumps({
71
+ 'model': 'text-embedding-v3',
72
+ 'input': chunks[:10], # 单次上限10条
73
+ 'dimensions': 1024,
74
+ }).encode('utf-8'),
75
+ headers={'Authorization': 'Bearer ' + key, 'Content-Type': 'application/json'},
76
+ )
77
+ with urllib.request.urlopen(req, timeout=30) as resp:
78
+ r = json.loads(resp.read())
79
+ vecs = [d['embedding'] for d in r['data']]
80
+ _upsert_embeddings(prd_id, chunks[:len(vecs)], vecs, repo_id)
81
+ return {'chunks': len(chunks), 'embedded': len(vecs)}
82
+ except Exception:
83
+ return {'chunks': len(chunks), 'embedded': 0}
84
+
85
+
86
+ def _upsert_embeddings(prd_id, chunks, vecs, repo_id):
87
+ """写 PG embeddings 表 (pgvector)。"""
88
+ try:
89
+ from domain.kg.extract.java import pg_upsert
90
+ engine, _ = pg_upsert._get_pg_engine()
91
+ if engine is None:
92
+ return
93
+ from sqlalchemy import text
94
+ import json
95
+ with engine.begin() as conn:
96
+ for chunk, vec in zip(chunks, vecs):
97
+ vec_str = '[' + ','.join(str(v) for v in vec) + ']'
98
+ eid = 'PRDEMB:%s:%d' % (prd_id[:12], hash(chunk) & 0xffff)
99
+ conn.execute(text("""
100
+ INSERT INTO embeddings (entity_id, repo_id, kind, content, vec, embedded_at)
101
+ VALUES (:id, :repo, 'prd', :content, CAST(:vec AS VECTOR), now())
102
+ ON CONFLICT (entity_id) DO UPDATE SET content=EXCLUDED.content, vec=EXCLUDED.vec, embedded_at=now()
103
+ """), {'id': eid, 'repo': repo_id, 'content': chunk[:500], 'vec': vec_str})
104
+ except Exception as e:
105
+ print(' [prd-embed] 写入跳过:', str(e)[:60])
@@ -0,0 +1,153 @@
1
+ # -*- coding: utf-8 -*-
2
+ """PRD 正文 → LLM 抽取功能点/字段/规则/端点/权限。
3
+
4
+ 输入: PRD markdown 正文
5
+ 输出: 结构化知识 (FEATURE/CONSTRAINT/PERMISSION 实体)
6
+ 用 DashScope qwen-plus (已接入) 做结构化抽取。
7
+
8
+ 被 asset/prd_importer.py 调用 (灌资产中心 prds 表时, 每个PRD正文过一遍LLM)。
9
+ """
10
+ from __future__ import annotations
11
+ import os
12
+ import sys
13
+ import json
14
+ import re
15
+ import urllib.request
16
+
17
+ _THIS = os.path.dirname(os.path.abspath(__file__))
18
+ for _i in range(8):
19
+ _p = os.path.dirname(_THIS)
20
+ if os.path.isfile(os.path.join(_p, 'foundation', 'bootstrap.py')):
21
+ sys.path.insert(0, _p); break
22
+ _THIS = _p
23
+
24
+ PRD_EXTRACT_PROMPT = """从以下PRD正文提取结构化知识, 只返回严格JSON(不要markdown代码块, 不要解释), 格式:
25
+ {"features":[{"name":"资产列表分页","desc":"支持按页查看"}],"fields":[{"name":"owner_id","label":"资产归属人","rule":"必填"}],"rules":[{"rule":"金额>1万需二次审批","type":"business"}],"endpoints":[{"path":"/asset/list","method":"GET","desc":"资产列表"}],"permissions":[{"code":"asset:list","desc":"资产查看权限"}]}
26
+ 若某类提取不到就返回空数组。PRD正文:
27
+ ---
28
+ %s
29
+ ---"""
30
+
31
+
32
+ def _call_qwen(prompt: str, timeout: int = 60) -> str:
33
+ """调 DashScope qwen-plus, 返回文本。失败返回 ''。"""
34
+ key = os.environ.get('DASHSCOPE_API_KEY', '')
35
+ base = os.environ.get('ALI_CHAT_BASE', '').rstrip('/')
36
+ model = os.environ.get('ALI_CHAT_MODEL', 'qwen-plus')
37
+ if not key or not base:
38
+ return ''
39
+ req = urllib.request.Request(
40
+ base + '/chat/completions',
41
+ data=json.dumps({
42
+ 'model': model,
43
+ 'messages': [{'role': 'user', 'content': prompt}],
44
+ 'max_tokens': 2000, 'temperature': 0.1,
45
+ }).encode('utf-8'),
46
+ headers={'Authorization': 'Bearer ' + key, 'Content-Type': 'application/json'},
47
+ )
48
+ try:
49
+ with urllib.request.urlopen(req, timeout=timeout) as resp:
50
+ r = json.loads(resp.read())
51
+ return r['choices'][0]['message']['content']
52
+ except Exception:
53
+ return ''
54
+
55
+
56
+ def _parse_json_lenient(text: str) -> dict:
57
+ """宽松解析 LLM 返回的 JSON (可能有 markdown 包裹或前后缀)。"""
58
+ if not text:
59
+ return {}
60
+ # 剥 markdown 代码块
61
+ text = re.sub(r'^```(?:json)?\s*', '', text.strip())
62
+ text = re.sub(r'\s*```$', '', text.strip())
63
+ # 找第一个 { 到最后一个 }
64
+ s = text.find('{')
65
+ e = text.rfind('}')
66
+ if s >= 0 and e > s:
67
+ text = text[s:e + 1]
68
+ try:
69
+ return json.loads(text)
70
+ except Exception:
71
+ return {}
72
+
73
+
74
+ def extract_prd_knowledge(content_md: str) -> dict:
75
+ """PRD 正文 → LLM 抽取 → 结构化 dict。
76
+
77
+ Returns: {features, fields, rules, endpoints, permissions}
78
+ """
79
+ if not content_md or len(content_md) < 50:
80
+ return {'features': [], 'fields': [], 'rules': [], 'endpoints': [], 'permissions': []}
81
+ # 截断过长正文 (避免超 token)
82
+ content = content_md[:6000]
83
+ raw = _call_qwen(PRD_EXTRACT_PROMPT % content)
84
+ parsed = _parse_json_lenient(raw)
85
+ # 兜底确保字段齐全
86
+ return {
87
+ 'features': parsed.get('features', []),
88
+ 'fields': parsed.get('fields', []),
89
+ 'rules': parsed.get('rules', []),
90
+ 'endpoints': parsed.get('endpoints', []),
91
+ 'permissions': parsed.get('permissions', []),
92
+ }
93
+
94
+
95
+ def to_entities_edges(extracted: dict, prd_id: str, project_id: str, repo_id: str = ''):
96
+ """把 LLM 抽取结果转成 entities + edges。
97
+
98
+ entities: FEATURE / CONSTRAINT / PERMISSION
99
+ edges: 全部锚到 prd_id (强关联, confidence=1.0)
100
+ """
101
+ entities, edges = [], []
102
+ base = 'FEAT:%s' % prd_id[:12] if prd_id else 'FEAT:unknown'
103
+
104
+ # 功能点 → FEATURE
105
+ for f in extracted.get('features', []):
106
+ fname = f.get('name', '').strip()
107
+ if not fname:
108
+ continue
109
+ fid = '%s:%s' % (base, fname[:20])
110
+ entities.append({
111
+ 'id': fid, 'repo_id': repo_id, 'type': 'FEATURE',
112
+ 'canonical': fname, 'cn': f.get('desc', ''),
113
+ 'props': {'source': 'prd_llm', 'prd_id': prd_id},
114
+ })
115
+ edges.append(_anchor_edge(fid, prd_id, repo_id))
116
+
117
+ # 业务规则 → CONSTRAINT
118
+ for r in extracted.get('rules', []):
119
+ rule = r.get('rule', '').strip()
120
+ if not rule:
121
+ continue
122
+ cid = 'CONST:%s:%s' % (prd_id[:8], rule[:20])
123
+ entities.append({
124
+ 'id': cid, 'repo_id': repo_id, 'type': 'CONSTRAINT',
125
+ 'canonical': rule[:50], 'cn': r.get('type', ''),
126
+ 'props': {'source': 'prd_llm', 'prd_id': prd_id, 'rule': rule},
127
+ })
128
+ edges.append(_anchor_edge(cid, prd_id, repo_id))
129
+
130
+ # 权限 → PERMISSION
131
+ for p in extracted.get('permissions', []):
132
+ code = p.get('code', '').strip()
133
+ if not code:
134
+ continue
135
+ pid = 'PERM:PRD:%s:%s' % (prd_id[:8], code[:20])
136
+ entities.append({
137
+ 'id': pid, 'repo_id': repo_id, 'type': 'PERMISSION',
138
+ 'canonical': code, 'cn': p.get('desc', ''),
139
+ 'props': {'source': 'prd_llm', 'prd_id': prd_id},
140
+ })
141
+ edges.append(_anchor_edge(pid, prd_id, repo_id))
142
+
143
+ return entities, edges
144
+
145
+
146
+ def _anchor_edge(from_id: str, prd_id: str, repo_id: str) -> dict:
147
+ """实体 → PRD 的强关联边。"""
148
+ return {
149
+ 'from_id': from_id, 'to_id': 'prd:%s' % prd_id,
150
+ 'edge_type': 'anchors', 'from_repo': repo_id, 'to_repo': repo_id,
151
+ 'confidence': 1.0, 'source': 'explicit',
152
+ 'props': {'via': 'prd_llm'},
153
+ }
@@ -0,0 +1,120 @@
1
+ # -*- coding: utf-8 -*-
2
+ """REQ-ID 强关联: PRD/commit/代码注释 三种锚定来源。
3
+
4
+ 复用 archive_prd.py 的正则 (REQ-2026-005 / REQ-012 两种格式)。
5
+ 锚点写 anchors 表, 关联写 edges (confidence=1.0, source=explicit)。
6
+ """
7
+ from __future__ import annotations
8
+ import os
9
+ import sys
10
+ import re
11
+
12
+ _THIS = os.path.dirname(os.path.abspath(__file__))
13
+ for _i in range(8):
14
+ _p = os.path.dirname(_THIS)
15
+ if os.path.isfile(os.path.join(_p, 'foundation', 'bootstrap.py')):
16
+ sys.path.insert(0, _p); break
17
+ _THIS = _p
18
+
19
+ # 复用 archive_prd.py 的正则 (兼容 REQ-2026-005 标准格式 + REQ-012 老格式)
20
+ _REQ_RE = re.compile(r'REQ-(\d{2,4})(?:-(\d{2,4}))?', re.IGNORECASE)
21
+
22
+
23
+ def format_req_id(match) -> str:
24
+ """格式化 REQ-ID (标准化)。"""
25
+ g1, g2 = match.group(1), match.group(2)
26
+ if g2:
27
+ return 'REQ-%s-%s' % (g1, g2)
28
+ return 'REQ-%s' % g1
29
+
30
+
31
+ def find_req_ids(text: str) -> list:
32
+ """从文本提取所有 REQ-ID (去重保序)。"""
33
+ seen = set()
34
+ out = []
35
+ for m in _REQ_RE.finditer(text or ''):
36
+ rid = format_req_id(m)
37
+ if rid not in seen:
38
+ seen.add(rid)
39
+ out.append(rid)
40
+ return out
41
+
42
+
43
+ # ============ 三种锚定 ============
44
+
45
+ def anchor_prd(prd_id: str, title: str, project_id: str, repo_id: str = ''):
46
+ """时机1: PRD 创建时锚定。
47
+
48
+ PRD 标题/内容含 REQ-ID → 锚到 anchors 表。
49
+ 没有 REQ-ID → 用 PRD id 生成锚点 (REQ-PRD-xxx)。
50
+ Returns: (anchor_entities, anchor_edges)
51
+ """
52
+ entities, edges = [], []
53
+ req_ids = find_req_ids(title)
54
+ if not req_ids:
55
+ # 无 REQ-ID, 用 PRD 生成一个锚点
56
+ req_ids = ['REQ-PRD-%s' % prd_id[:8]]
57
+
58
+ for rid in req_ids:
59
+ # anchors 表实体 (type=ANCHOR)
60
+ entities.append({
61
+ 'id': rid, 'repo_id': repo_id, 'type': 'ANCHOR',
62
+ 'canonical': rid, 'cn': title[:60],
63
+ 'props': {'kind': 'feature', 'prd_id': prd_id, 'project_id': project_id},
64
+ })
65
+ # PRD → 锚点 强关联边
66
+ edges.append({
67
+ 'from_id': 'prd:%s' % prd_id, 'to_id': rid,
68
+ 'edge_type': 'anchors', 'from_repo': repo_id, 'to_repo': repo_id,
69
+ 'confidence': 1.0, 'source': 'explicit',
70
+ 'props': {'via': 'prd_create'},
71
+ })
72
+ return entities, edges
73
+
74
+
75
+ def anchor_commit(commit_msg: str, changed_files: list, repo_id: str = ''):
76
+ """时机2: commit message 带 REQ-ID → 代码文件锚定。
77
+
78
+ feat(REQ-2026-005): 资产列表分页
79
+ → changed_files 里的代码 → 锚到 REQ-2026-005
80
+ Returns: edges (不产生新实体, 只关联已有代码实体到锚点)
81
+ """
82
+ edges = []
83
+ req_ids = find_req_ids(commit_msg)
84
+ if not req_ids:
85
+ return edges
86
+
87
+ for rid in req_ids:
88
+ for filepath in changed_files:
89
+ # 文件级锚定 (文件可能对应多个实体, 这里先建文件→锚点边)
90
+ file_entity_id = 'FILE:%s:%s' % (repo_id, filepath.replace('\\', '/'))
91
+ edges.append({
92
+ 'from_id': file_entity_id, 'to_id': rid,
93
+ 'edge_type': 'implements_req', 'from_repo': repo_id, 'to_repo': repo_id,
94
+ 'confidence': 1.0, 'source': 'commit',
95
+ 'props': {'commit_msg': commit_msg[:100]},
96
+ })
97
+ return edges
98
+
99
+
100
+ def anchor_inline_code(content: str, filepath: str, repo_id: str = ''):
101
+ """时机3: 代码注释里的 REQ-ID 锚定。
102
+
103
+ // REQ-2026-005 资产列表分页
104
+ /* REQ-047 老逻辑 */
105
+ Returns: edges
106
+ """
107
+ edges = []
108
+ req_ids = find_req_ids(content)
109
+ if not req_ids:
110
+ return edges
111
+
112
+ file_entity_id = 'FILE:%s:%s' % (repo_id, filepath.replace('\\', '/'))
113
+ for rid in req_ids:
114
+ edges.append({
115
+ 'from_id': file_entity_id, 'to_id': rid,
116
+ 'edge_type': 'implements_req', 'from_repo': repo_id, 'to_repo': repo_id,
117
+ 'confidence': 1.0, 'source': 'code_comment',
118
+ 'props': {'via': 'inline_comment'},
119
+ })
120
+ return edges
@@ -65,6 +65,117 @@ def _find_calls_in_function_ast(func_node, source_bytes):
65
65
  return calls
66
66
 
67
67
 
68
+ # ── P1: calls / imports 边抽取 ────────────────────────────────────────────
69
+ # 把 tree-sitter 已能识别的 call_expression 和 import 语句导出成图边,
70
+ # 让知识图谱从"按钮→接口→控制器"单条业务链升级为带函数级调用的代码图。
71
+ # 返回结构供 kg_build / kg_incremental 写成 entities(FUNCTION) + edges(calls/imports)。
72
+
73
+ # 函数定义节点类型 (TS/JS): 命名函数 / const fn = () => {} / 类方法
74
+ _FN_NODE_TYPES = (
75
+ 'function_declaration', # function foo() {}
76
+ 'method_definition', # class { foo() {} }
77
+ # lexical_declaration 内的 arrow_function 由下面单独处理 (export const fn = () => {})
78
+ )
79
+
80
+
81
+ def _fn_name_from_node(node, source_bytes):
82
+ """Extract a function's name from a definition node, or None."""
83
+ if node.type == 'function_declaration':
84
+ name_node = node.child_by_field_name('name')
85
+ return _node_text(name_node, source_bytes) if name_node else None
86
+ if node.type == 'method_definition':
87
+ name_node = node.child_by_field_name('name')
88
+ return _node_text(name_node, source_bytes) if name_node else None
89
+ if node.type == 'lexical_declaration':
90
+ # export const fnName = (...) => { ... } / const fnName = function() {}
91
+ for child in node.children:
92
+ if child.type == 'variable_declarator':
93
+ name_node = child.child_by_field_name('name')
94
+ value = child.child_by_field_name('value')
95
+ if name_node and value and value.type in ('arrow_function', 'function_expression'):
96
+ return _node_text(name_node, source_bytes)
97
+ return None
98
+
99
+
100
+ def ts_extract_calls(content):
101
+ """Extract function-level call edges from TS/JS/Vue-script source.
102
+
103
+ Walks every function definition in the AST, then reuses
104
+ _find_calls_in_function_ast to collect each function's callees.
105
+
106
+ Returns:
107
+ [(from_fn, callee), ...] — caller function name and callee text.
108
+ Callee text may be qualified (e.g. 'obj.method', 'useStore()');
109
+ callers choose how to resolve. Tree-sitter unavailable → [] .
110
+ """
111
+ if not _TS_AVAILABLE or not content:
112
+ return []
113
+ try:
114
+ source = content.encode('utf-8')
115
+ tree = _PARSER.parse(source)
116
+ except Exception:
117
+ return []
118
+
119
+ edges = []
120
+ # Collect every function definition (named + arrow + method).
121
+ fns = []
122
+
123
+ def collect_fns(node):
124
+ if node.type in _FN_NODE_TYPES:
125
+ fns.append(node)
126
+ # lexical_declaration may hold `const fn = () => {}` — handled in _fn_name_from_node
127
+ if node.type == 'lexical_declaration':
128
+ fns.append(node)
129
+ for child in node.children:
130
+ collect_fns(child)
131
+
132
+ collect_fns(tree.root_node)
133
+
134
+ for fn_node in fns:
135
+ caller = _fn_name_from_node(fn_node, source)
136
+ if not caller:
137
+ continue
138
+ for callee, _full in _find_calls_in_function_ast(fn_node, source):
139
+ # Skip trivial self-references and empty callees.
140
+ callee = callee.strip()
141
+ if callee and callee != caller:
142
+ edges.append((caller, callee))
143
+ return edges
144
+
145
+
146
+ def ts_extract_imports(content):
147
+ """Extract import edges from TS/JS source (local module imports only).
148
+
149
+ Returns:
150
+ [(imported_name, source_path), ...] for local imports (relative paths
151
+ or @/ #/ alias). Third-party bare specifiers (e.g. 'vue', 'axios') are
152
+ filtered out — they aren't part of the project graph.
153
+ """
154
+ if not content:
155
+ return []
156
+ out = []
157
+ # Generic local import: import { a, b } from './x' | import x from '@/y' | import * as z from '../z'
158
+ for m in re.finditer(
159
+ r"import\s+(?:\{([^}]*)\}|\*\s+as\s+(\w+)|(\w+))\s*(?:,\s*\{([^}]*)\})?\s+from\s+['\"]([^'\"]+)['\"]",
160
+ content,
161
+ ):
162
+ names_group = m.group(1) or m.group(4) # named imports {a, b}
163
+ default_name = m.group(2) or m.group(3) # * as z or default x
164
+ src = (m.group(5) or '').strip()
165
+ # Only local imports (relative or alias); skip bare packages like 'vue'.
166
+ if not src or not (src.startswith('.') or src.startswith('@/') or src.startswith('#/')):
167
+ continue
168
+ names = []
169
+ if names_group:
170
+ names.extend(n.strip().split(' as ')[-1].strip() for n in names_group.split(',') if n.strip())
171
+ if default_name:
172
+ names.append(default_name.strip())
173
+ for name in names:
174
+ if name:
175
+ out.append((name, src))
176
+ return out
177
+
178
+
68
179
  def ts_build_api_fn_table(api_dir):
69
180
  """Tree-sitter version: build {fnName: {url, verb, file}} from api/*.ts.
70
181
 
@@ -179,9 +179,11 @@ def _load_model():
179
179
  # (本进程早先走过 bootstrap), sys.modules 里缓存的是错的, 要一并清掉。
180
180
  saved_paths = [p for p in sys.path]
181
181
  scripts_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
182
+ # Note: sys.path may contain pathlib.Path objects (not just str) in some
183
+ # environments — coerce to str before .lower() to avoid AttributeError.
182
184
  sys.path[:] = [p for p in sys.path
183
- if not (p and p.lower().startswith(scripts_root.lower()))
184
- and os.path.basename(p) not in ('foundation', 'kg', 'mcp', 'report', 'test', 'setup')]
185
+ if not (p and str(p).lower().startswith(scripts_root.lower()))
186
+ and os.path.basename(str(p)) not in ('foundation', 'kg', 'mcp', 'report', 'test', 'setup')]
185
187
  # 清掉被本地模块污染的 sys.modules 缓存 (filelock 等), 让第三方重新 import 干净版
186
188
  _purged = {}
187
189
  for mod_name in list(sys.modules):