@hupan56/wlkj 2.7.12 → 3.1.2

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 (253) hide show
  1. package/bin/cli.js +1435 -1107
  2. package/package.json +29 -29
  3. package/templates/qoder/agents/insight-planning.md +1 -1
  4. package/templates/qoder/agents/insight-research.md +28 -15
  5. package/templates/qoder/commands/optional/wl-insight.md +161 -161
  6. package/templates/qoder/commands/optional/wl-report.md +20 -39
  7. package/templates/qoder/commands/optional/wl-status.md +4 -4
  8. package/templates/qoder/commands/wl-code.md +2 -2
  9. package/templates/qoder/commands/wl-design.md +6 -6
  10. package/templates/qoder/commands/wl-init.md +3 -3
  11. package/templates/qoder/commands/wl-prd.md +26 -13
  12. package/templates/qoder/commands/wl-req.md +43 -0
  13. package/templates/qoder/commands/wl-search.md +78 -47
  14. package/templates/qoder/commands/wl-task.md +343 -40
  15. package/templates/qoder/commands/wl-test.md +43 -17
  16. package/templates/qoder/config.yaml +52 -14
  17. package/templates/qoder/hooks/post-tool-use.py +181 -0
  18. package/templates/qoder/hooks/session-start.py +94 -23
  19. package/templates/qoder/hooks/stop-eval.py +207 -0
  20. package/templates/qoder/hooks/user-prompt-submit.py +139 -0
  21. package/templates/qoder/rules/wl-pipeline.md +85 -49
  22. package/templates/qoder/scripts/README.md +102 -0
  23. package/templates/qoder/scripts/capability/__init__.py +26 -0
  24. package/templates/qoder/scripts/capability/__main__.py +72 -0
  25. package/templates/qoder/scripts/capability/adapters/__init__.py +29 -0
  26. package/templates/qoder/scripts/capability/adapters/cli.py +316 -0
  27. package/templates/qoder/scripts/capability/adapters/mcp.py +334 -0
  28. package/templates/qoder/scripts/capability/adapters/qw.py +271 -0
  29. package/templates/qoder/scripts/capability/caps/__init__.py +27 -0
  30. package/templates/qoder/scripts/capability/caps/context.py +36 -0
  31. package/templates/qoder/scripts/capability/caps/cron.py +50 -0
  32. package/templates/qoder/scripts/capability/caps/identity.py +43 -0
  33. package/templates/qoder/scripts/capability/caps/memory.py +71 -0
  34. package/templates/qoder/scripts/capability/caps/notify.py +41 -0
  35. package/templates/qoder/scripts/capability/caps/present.py +48 -0
  36. package/templates/qoder/scripts/capability/caps/repo.py +31 -0
  37. package/templates/qoder/scripts/capability/caps/sandbox.py +43 -0
  38. package/templates/qoder/scripts/capability/chain.py +92 -0
  39. package/templates/qoder/scripts/capability/memory_chain.py +41 -0
  40. package/templates/qoder/scripts/capability/registry.py +246 -0
  41. package/templates/qoder/scripts/capability/registry_mcp.py +250 -0
  42. package/templates/qoder/scripts/capability/smoke_test.py +211 -0
  43. package/templates/qoder/scripts/capability/smoke_test_report.json +94 -0
  44. package/templates/qoder/scripts/deployment/setup/__init__.py +11 -0
  45. package/templates/qoder/scripts/deployment/setup/carriers.py +669 -0
  46. package/templates/qoder/scripts/deployment/setup/carriers_verify.py +148 -0
  47. package/templates/qoder/scripts/{init_doctor.py → deployment/setup/init_doctor.py} +100 -39
  48. package/templates/qoder/scripts/{install_qoderwork.py → deployment/setup/install_qoderwork.py} +48 -30
  49. package/templates/qoder/scripts/{platform_doctor.py → deployment/setup/platform_doctor.py} +271 -259
  50. package/templates/qoder/scripts/{repo_root.py → deployment/setup/repo_root.py} +9 -2
  51. package/templates/qoder/scripts/{setup.py → deployment/setup/setup.py} +143 -14
  52. package/templates/qoder/scripts/{setup_lanhu.py → deployment/setup/setup_lanhu.py} +979 -963
  53. package/templates/qoder/scripts/domain/design/__init__.py +0 -0
  54. package/templates/qoder/scripts/{fill_prototype.py → domain/design/fill_prototype.py} +17 -5
  55. package/templates/qoder/scripts/{gen_design_doc.py → domain/design/gen_design_doc.py} +406 -394
  56. package/templates/qoder/scripts/domain/kg/__init__.py +11 -0
  57. package/templates/qoder/scripts/domain/kg/build/__init__.py +0 -0
  58. package/templates/qoder/scripts/domain/kg/build/build_entity_registry.py +201 -0
  59. package/templates/qoder/scripts/domain/kg/build/build_relations.py +132 -0
  60. package/templates/qoder/scripts/{build_style_index.py → domain/kg/build/build_style_index.py} +50 -15
  61. package/templates/qoder/scripts/domain/kg/build/build_workflows.py +149 -0
  62. package/templates/qoder/scripts/{kg_build.py → domain/kg/build/kg_build.py} +685 -612
  63. package/templates/qoder/scripts/{kg_build_db.py → domain/kg/build/kg_build_db.py} +338 -327
  64. package/templates/qoder/scripts/{kg_incremental.py → domain/kg/build/kg_incremental.py} +425 -393
  65. package/templates/qoder/scripts/{learn_aggregate.py → domain/kg/build/learn_aggregate.py} +212 -201
  66. package/templates/qoder/scripts/domain/kg/extract/__init__.py +0 -0
  67. package/templates/qoder/scripts/{common → domain/kg/extract}/extract.py +430 -419
  68. package/templates/qoder/scripts/domain/kg/extract/test_extract.py +115 -0
  69. package/templates/qoder/scripts/{common → domain/kg/extract}/ts_extract.py +614 -536
  70. package/templates/qoder/scripts/domain/kg/graph/__init__.py +0 -0
  71. package/templates/qoder/scripts/{kg_link_db.py → domain/kg/graph/kg_link_db.py} +235 -224
  72. package/templates/qoder/scripts/domain/kg/graph/kg_semantic.py +683 -0
  73. package/templates/qoder/scripts/{kg.py → domain/kg/kg.py} +871 -708
  74. package/templates/qoder/scripts/domain/kg/kg_capabilities.py +182 -0
  75. package/templates/qoder/scripts/domain/kg/search/__init__.py +0 -0
  76. package/templates/qoder/scripts/{context_pack.py → domain/kg/search/context_pack.py} +45 -17
  77. package/templates/qoder/scripts/{enrich_prompt.py → domain/kg/search/enrich_prompt.py} +238 -226
  78. package/templates/qoder/scripts/domain/kg/search/prefetch.py +384 -0
  79. package/templates/qoder/scripts/{common → domain/kg/search}/search_engine.py +207 -205
  80. package/templates/qoder/scripts/{search_index.py → domain/kg/search/search_index.py} +210 -88
  81. package/templates/qoder/scripts/domain/kg/server/__init__.py +0 -0
  82. package/templates/qoder/scripts/domain/kg/server/kgd.py +549 -0
  83. package/templates/qoder/scripts/domain/kg/server/perf_bench.py +197 -0
  84. package/templates/qoder/scripts/domain/kg/storage/__init__.py +0 -0
  85. package/templates/qoder/scripts/{kg_duckdb.py → domain/kg/storage/kg_duckdb.py} +70 -45
  86. package/templates/qoder/scripts/domain/learning/__init__.py +0 -0
  87. package/templates/qoder/scripts/{learn.py → domain/learning/learn.py} +157 -146
  88. package/templates/qoder/scripts/domain/report/__init__.py +11 -0
  89. package/templates/qoder/scripts/{add_session.py → domain/report/add_session.py} +256 -244
  90. package/templates/qoder/scripts/{export.py → domain/report/export.py} +72 -4
  91. package/templates/qoder/scripts/{report.py → domain/report/report.py} +292 -281
  92. package/templates/qoder/scripts/domain/report/report_snapshot.py +261 -0
  93. package/templates/qoder/scripts/domain/report/role.py +39 -0
  94. package/templates/qoder/scripts/{status.py → domain/report/status.py} +737 -628
  95. package/templates/qoder/scripts/domain/report/status_snapshot.py +191 -0
  96. package/templates/qoder/scripts/domain/requirement/__init__.py +0 -0
  97. package/templates/qoder/scripts/{archive_prd.py → domain/requirement/archive_prd.py} +389 -377
  98. package/templates/qoder/scripts/domain/requirement/req.py +228 -0
  99. package/templates/qoder/scripts/domain/task/__init__.py +11 -0
  100. package/templates/qoder/scripts/{git_sync.py → domain/task/git_sync.py} +90 -50
  101. package/templates/qoder/scripts/{syncgate.py → domain/task/syncgate.py} +18 -7
  102. package/templates/qoder/scripts/domain/task/task.py +229 -0
  103. package/templates/qoder/scripts/domain/task/task_lifecycle.py +606 -0
  104. package/templates/qoder/scripts/domain/task/task_query.py +162 -0
  105. package/templates/qoder/scripts/domain/task/task_relations.py +425 -0
  106. package/templates/qoder/scripts/{team_sync.py → domain/task/team_sync.py} +101 -23
  107. package/templates/qoder/scripts/domain/task/zentao_sync.py +370 -0
  108. package/templates/qoder/scripts/foundation/__init__.py +0 -0
  109. package/templates/qoder/scripts/foundation/bootstrap.py +145 -0
  110. package/templates/qoder/scripts/foundation/core/__init__.py +0 -0
  111. package/templates/qoder/scripts/foundation/core/cmd_registry.py +112 -0
  112. package/templates/qoder/scripts/foundation/core/config.py +187 -0
  113. package/templates/qoder/scripts/{common → foundation/core}/paths.py +706 -400
  114. package/templates/qoder/scripts/foundation/data/__init__.py +0 -0
  115. package/templates/qoder/scripts/foundation/data/contract.py +317 -0
  116. package/templates/qoder/scripts/{common → foundation/data}/events.py +2 -2
  117. package/templates/qoder/scripts/foundation/data/result.py +223 -0
  118. package/templates/qoder/scripts/{common → foundation/data}/task_utils.py +427 -392
  119. package/templates/qoder/scripts/foundation/identity/__init__.py +0 -0
  120. package/templates/qoder/scripts/foundation/identity/check_publish.py +103 -0
  121. package/templates/qoder/scripts/{common → foundation/identity}/developer.py +239 -238
  122. package/templates/qoder/scripts/foundation/identity/guard.py +159 -0
  123. package/templates/qoder/scripts/{common → foundation/identity}/identity.py +132 -9
  124. package/templates/qoder/scripts/foundation/identity/roles.py +60 -0
  125. package/templates/qoder/scripts/foundation/integrations/__init__.py +0 -0
  126. package/templates/qoder/scripts/{common → foundation/integrations}/active_task.py +30 -15
  127. package/templates/qoder/scripts/{common → foundation/integrations}/feishu.py +11 -10
  128. package/templates/qoder/scripts/{common → foundation/integrations}/terms.py +3 -3
  129. package/templates/qoder/scripts/foundation/integrations/zentao_client.py +220 -0
  130. package/templates/qoder/scripts/foundation/io/__init__.py +0 -0
  131. package/templates/qoder/scripts/{common → foundation/io}/filelock.py +1 -1
  132. package/templates/qoder/scripts/{common → foundation/io}/reqid.py +1 -1
  133. package/templates/qoder/scripts/foundation/protocol/__init__.py +0 -0
  134. package/templates/qoder/scripts/foundation/protocol/duckdb_conn.py +39 -0
  135. package/templates/qoder/scripts/foundation/protocol/mcp_base.py +268 -0
  136. package/templates/qoder/scripts/foundation/utils/__init__.py +0 -0
  137. package/templates/qoder/scripts/{common → foundation/utils}/eval_api.py +12 -1
  138. package/templates/qoder/scripts/{common → foundation/utils}/pip_install.py +23 -6
  139. package/templates/qoder/scripts/{common → foundation/utils}/platform_guard.py +1 -1
  140. package/templates/qoder/scripts/orchestration/__init__.py +0 -0
  141. package/templates/qoder/scripts/orchestration/wlkj.py +185 -0
  142. package/templates/qoder/scripts/protocol/__init__.py +0 -0
  143. package/templates/qoder/scripts/protocol/browser/README.md +207 -0
  144. package/templates/qoder/scripts/protocol/browser/SKILL.md +265 -0
  145. package/templates/qoder/scripts/protocol/browser/config.env +9 -0
  146. package/templates/qoder/scripts/protocol/browser/references/cdp-api.md +110 -0
  147. package/templates/qoder/scripts/protocol/browser/references/migration-2.5.3.md +72 -0
  148. package/templates/qoder/scripts/protocol/browser/references/site-patterns/.gitkeep +0 -0
  149. package/templates/qoder/scripts/protocol/browser/scripts/browser-discovery.mjs +138 -0
  150. package/templates/qoder/scripts/protocol/browser/scripts/cdp-proxy.mjs +672 -0
  151. package/templates/qoder/scripts/protocol/browser/scripts/check-deps.mjs +206 -0
  152. package/templates/qoder/scripts/protocol/browser/scripts/find-url.mjs +253 -0
  153. package/templates/qoder/scripts/protocol/browser/scripts/match-site.mjs +46 -0
  154. package/templates/qoder/scripts/protocol/browser/templates/config.env.template +9 -0
  155. package/templates/qoder/scripts/protocol/mcp/__init__.py +11 -0
  156. package/templates/qoder/scripts/protocol/mcp/kg_mcp_server.py +478 -0
  157. package/templates/qoder/scripts/{lanhu_stdio_wrapper.py → protocol/mcp/lanhu_stdio_wrapper.py} +131 -119
  158. package/templates/qoder/scripts/protocol/mcp/mcp_doctor.py +548 -0
  159. package/templates/qoder/scripts/protocol/mcp/mcp_launcher.py +600 -0
  160. package/templates/qoder/scripts/protocol/mcp/mysql_mcp_server.py +461 -0
  161. package/templates/qoder/scripts/protocol/mcp/zentao_mcp_server.py +1920 -0
  162. package/templates/qoder/scripts/protocol/transports/__init__.py +56 -0
  163. package/templates/qoder/scripts/protocol/transports/base.py +87 -0
  164. package/templates/qoder/scripts/protocol/transports/cli.py +132 -0
  165. package/templates/qoder/scripts/protocol/transports/http.py +141 -0
  166. package/templates/qoder/scripts/protocol/transports/stdio.py +293 -0
  167. package/templates/qoder/scripts/run_weekly_update.bat +27 -18
  168. package/templates/qoder/scripts/run_weekly_update.sh +22 -13
  169. package/templates/qoder/scripts/validation/__init__.py +0 -0
  170. package/templates/qoder/scripts/validation/eval/__init__.py +0 -0
  171. package/templates/qoder/scripts/validation/eval/perf-report-2026-06-24.md +139 -0
  172. package/templates/qoder/scripts/validation/eval/qwork_harness.py +617 -0
  173. package/templates/qoder/scripts/validation/eval/report-commands.md +220 -0
  174. package/templates/qoder/scripts/validation/eval/transcript_timing.py +386 -0
  175. package/templates/qoder/scripts/validation/metrics/__init__.py +0 -0
  176. package/templates/qoder/scripts/{eval_prd.py → validation/metrics/eval_prd.py} +84 -15
  177. package/templates/qoder/scripts/validation/metrics/usability_score.py +750 -0
  178. package/templates/qoder/scripts/validation/test/__init__.py +11 -0
  179. package/templates/qoder/scripts/validation/test/assertion_gen.py +551 -0
  180. package/templates/qoder/scripts/{autotest.py → validation/test/autotest.py} +1234 -1751
  181. package/templates/qoder/scripts/validation/test/autotest_auth.py +109 -0
  182. package/templates/qoder/scripts/{autotest_batch.py → validation/test/autotest_batch.py} +255 -224
  183. package/templates/qoder/scripts/validation/test/autotest_data.py +680 -0
  184. package/templates/qoder/scripts/validation/test/autotest_opencli.py +646 -0
  185. package/templates/qoder/scripts/{autotest_run.py → validation/test/autotest_run.py} +323 -297
  186. package/templates/qoder/scripts/validation/test/autotest_webaccess.py +571 -0
  187. package/templates/qoder/scripts/{benchmark.py → validation/test/benchmark.py} +17 -5
  188. package/templates/qoder/scripts/{kg_auto_login.py → validation/test/kg_auto_login.py} +208 -196
  189. package/templates/qoder/scripts/{kg_test_runner.py → validation/test/kg_test_runner.py} +13 -2
  190. package/templates/qoder/scripts/{page_probe.py → validation/test/page_probe.py} +470 -459
  191. package/templates/qoder/scripts/validation/test/smoke_all_commands.py +170 -0
  192. package/templates/qoder/settings.json +23 -2
  193. package/templates/qoder/skills/design-import/SKILL.md +235 -226
  194. package/templates/qoder/skills/design-review/SKILL.md +91 -82
  195. package/templates/qoder/skills/prd-generator/SKILL.md +38 -24
  196. package/templates/qoder/skills/prd-review/SKILL.md +8 -6
  197. package/templates/qoder/skills/prototype-generator/SKILL.md +296 -247
  198. package/templates/qoder/skills/spec-coder/SKILL.md +24 -15
  199. package/templates/qoder/skills/spec-generator/SKILL.md +24 -12
  200. package/templates/qoder/skills/test-generator/SKILL.md +9 -7
  201. package/templates/qoder/skills/wl-code/SKILL.md +25 -13
  202. package/templates/qoder/skills/wl-commit/SKILL.md +10 -9
  203. package/templates/qoder/skills/wl-design/SKILL.md +7 -5
  204. package/templates/qoder/skills/wl-init/SKILL.md +8 -8
  205. package/templates/qoder/skills/wl-insight/SKILL.md +25 -12
  206. package/templates/qoder/skills/wl-prd-full/SKILL.md +59 -8
  207. package/templates/qoder/skills/wl-prd-quick/SKILL.md +7 -7
  208. package/templates/qoder/skills/wl-prd-review/SKILL.md +18 -6
  209. package/templates/qoder/skills/wl-report/SKILL.md +27 -25
  210. package/templates/qoder/skills/wl-search/SKILL.md +151 -80
  211. package/templates/qoder/skills/wl-spec/SKILL.md +9 -7
  212. package/templates/qoder/skills/wl-status/SKILL.md +50 -18
  213. package/templates/qoder/skills/wl-task/SKILL.md +94 -12
  214. package/templates/qoder/skills/wl-test/SKILL.md +154 -45
  215. package/templates/qoder/templates/prd-full-template.md +7 -0
  216. package/templates/root/AGENTS.md +275 -237
  217. package/templates/root/requirements.txt +3 -0
  218. package/templates/root//344/275/277/347/224/250/350/257/264/346/230/216.md +3 -3
  219. package/templates/root//346/226/260/346/211/213/346/214/207/345/215/227.md +2 -2
  220. package/templates/qoder/hooks/inject-workflow-state.py +0 -169
  221. package/templates/qoder/scripts/__pycache__/check_mcp_launch.cpython-39.pyc +0 -0
  222. package/templates/qoder/scripts/__pycache__/install_qoderwork.cpython-39.pyc +0 -0
  223. package/templates/qoder/scripts/__pycache__/mcp_launcher.cpython-39.pyc +0 -0
  224. package/templates/qoder/scripts/__pycache__/platform_doctor.cpython-39.pyc +0 -0
  225. package/templates/qoder/scripts/check_carriers.py +0 -238
  226. package/templates/qoder/scripts/check_mcp.py +0 -298
  227. package/templates/qoder/scripts/check_mcp_launch.py +0 -183
  228. package/templates/qoder/scripts/check_qoderwork_consistency.py +0 -166
  229. package/templates/qoder/scripts/collect_prds.py +0 -31
  230. package/templates/qoder/scripts/common/mentions.py +0 -134
  231. package/templates/qoder/scripts/common/utf8.py +0 -38
  232. package/templates/qoder/scripts/extract_api_params.py +0 -246
  233. package/templates/qoder/scripts/extract_routes.py +0 -54
  234. package/templates/qoder/scripts/extract_routes_tree.py +0 -78
  235. package/templates/qoder/scripts/handoff.py +0 -22
  236. package/templates/qoder/scripts/init_developer.py +0 -76
  237. package/templates/qoder/scripts/kg_mcp_server.py +0 -801
  238. package/templates/qoder/scripts/kg_semantic.py +0 -150
  239. package/templates/qoder/scripts/mcp_launcher.py +0 -414
  240. package/templates/qoder/scripts/mysql_mcp_server.py +0 -396
  241. package/templates/qoder/scripts/parse_prds.py +0 -33
  242. package/templates/qoder/scripts/role.py +0 -51
  243. package/templates/qoder/scripts/sync_carriers.py +0 -259
  244. package/templates/qoder/scripts/task.py +0 -1261
  245. package/templates/qoder/scripts/workspace_init.py +0 -102
  246. package/templates/qoder/scripts/zentao_mcp_server.py +0 -424
  247. package/templates/qoder/skills/prompt-enrich/SKILL.md +0 -90
  248. /package/templates/qoder/scripts/{common → deployment}/__init__.py +0 -0
  249. /package/templates/qoder/{skills/prototype-generator/SKILL.md.zcode-79180-2af4721f-f9a6-412c-88db-c0af680d211b.tmp → scripts/domain/__init__.py} +0 -0
  250. /package/templates/qoder/scripts/{common → domain/kg/graph}/graph_traverse.py +0 -0
  251. /package/templates/qoder/scripts/{common → domain/kg/server}/repowiki.py +0 -0
  252. /package/templates/qoder/scripts/{common → foundation/io}/atomicio.py +0 -0
  253. /package/templates/qoder/scripts/{secure-ls.js → validation/test/secure-ls.js} +0 -0
@@ -1,224 +1,235 @@
1
- #!/usr/bin/env python
2
- # -*- coding: utf-8 -*-
3
- """kg_link_db.py - 扫代码 @TableName 注解, Entity 类 ↔ 数据库表 的语义桥。
4
-
5
- 解决"数据层孤岛"问题: kg_db.duckdb 有表结构, 但不知道对应代码里的哪个 Entity。
6
- 本脚本扫 Java 源码里的 @TableName("xxx") 注解, 提取:
7
- Entity 类名 → 数据库表名 → (关联到) db_columns 的真实列
8
-
9
- 调研依据 (FalkorDB "结构遍历 > 语义匹配"):
10
- - 不靠表名子串猜 endpoint (验证过 0 命中, 不准)
11
- - 靠代码显式声明的关系 (@TableName 注解) 建边, 这才准
12
-
13
- 产出表 (写进 kg_db.duckdb, 不动 kg.duckdb 写锁):
14
- entity_table_map:
15
- entity_class TEXT -- QualityCaseBo (代码层类名)
16
- table_name TEXT -- quality_case (数据库表名, 关联 db_tables)
17
- source_file TEXT -- 哪个 .java 读到的
18
- annotation TEXT -- @TableName("quality_case") 原文
19
-
20
- 建好后可遍历:
21
- 数据库表 entity_table_map Entity → (kg.duckdb) Controller/接口
22
-
23
- 用法:
24
- python kg_link_db.py # data/code/ 全量
25
- python kg_link_db.py --rebuild # 清旧重建
26
- python kg_link_db.py --stats # 看统计
27
- """
28
- import os
29
- import re
30
- import sys
31
- import time
32
-
33
- SCRIPTS = os.path.dirname(os.path.abspath(__file__))
34
- sys.path.insert(0, SCRIPTS)
35
-
36
- import duckdb
37
- from common.paths import DATA_INDEX_DIR, PROJECT_ROOT
38
-
39
- CODE_DIR = str(PROJECT_ROOT / 'data' / 'code')
40
- DB_PATH = str(DATA_INDEX_DIR / 'kg_db.duckdb')
41
-
42
- # @TableName 的两种格式:
43
- # @TableName("quality_case")
44
- # @TableName(value = "quality_case")
45
- RE_TABLENAME = re.compile(
46
- r'@TableName\s*\(\s*(?:value\s*=\s*)?"([^"]+)"',
47
- re.IGNORECASE
48
- )
49
- # 紧跟注解的 class 声明: class QualityCaseBo
50
- RE_CLASS = re.compile(r'class\s+(\w+)')
51
-
52
-
53
- def _normalize_table_name(raw):
54
- """归一化表名, 提升和数据库真实表的匹配率。
55
- 处理注解写法不规范的情况:
56
- fywl_ics_cloud.sys_dept sys_dept (去库名前缀)
57
- empContractImport → emp_contract_import (camelCase→snake_case)
58
- quality_case → quality_case (已是规范, 不变)
59
- """
60
- t = raw.strip().lower()
61
- # 去库名前缀 (db.table → table)
62
- if '.' in t:
63
- t = t.rsplit('.', 1)[-1]
64
- # 去常见前缀 t_/tb_/t (数据库表常加这些, 注解有时不加)
65
- # 注意: 不在这里去前缀, 因为 db_tables 里有的带 t_ 有的不带, 留原样让 JOIN 双向兜
66
- # camelCase → snake_case (Java 注解里有时写驼峰表名)
67
- if re.search(r'[a-z][A-Z]', t):
68
- t = re.sub(r'([a-z0-9])([A-Z])', r'\1_\2', t).lower()
69
- return t
70
-
71
-
72
- def _scan_java_files():
73
- """扫 data/code/ 下所有 .java, 返回 [(entity_class, table_name, normalized_table, source_file, annotation)]。"""
74
- mappings = []
75
- scanned = 0
76
- for root, dirs, files in os.walk(CODE_DIR):
77
- # 跳过非业务目录
78
- dirs[:] = [d for d in dirs if d not in (
79
- '.git', 'node_modules', '__pycache__', 'target', 'test')]
80
- for fn in files:
81
- if not fn.endswith('.java'):
82
- continue
83
- scanned += 1
84
- fpath = os.path.join(root, fn)
85
- try:
86
- for enc in ('utf-8', 'gbk'):
87
- try:
88
- with open(fpath, encoding=enc) as f:
89
- content = f.read()
90
- break
91
- except (UnicodeDecodeError, OSError):
92
- content = ''
93
- continue
94
- except OSError:
95
- continue
96
- if '@TableName' not in content:
97
- continue
98
- # 找所有 @TableName 注解
99
- for m in RE_TABLENAME.finditer(content):
100
- table_name = m.group(1)
101
- normalized = _normalize_table_name(table_name)
102
- annotation = content[m.start():m.end() + 1]
103
- # 找注解之后最近的 class 声明 (Entity 类名)
104
- after = content[m.end():m.end() + 500]
105
- cm = RE_CLASS.search(after)
106
- entity_class = cm.group(1) if cm else os.path.splitext(fn)[0]
107
- rel = os.path.relpath(fpath, PROJECT_ROOT).replace('\\', '/')
108
- mappings.append((entity_class, table_name, normalized, rel, annotation))
109
- return mappings, scanned
110
-
111
-
112
- def build(rebuild=False):
113
- """扫代码 @TableName entity_table_map。"""
114
- t0 = time.time()
115
- print('扫描 data/code/ 下的 @TableName 注解...')
116
- mappings, scanned = _scan_java_files()
117
- print(' 扫描 %d .java 文件, 找到 %d 个 @TableName 映射 (%.1fs)'
118
- % (scanned, len(mappings), time.time() - t0))
119
-
120
- if not mappings:
121
- print('未找到任何 @TableName 注解。检查 data/code/ 是否有 Java 源码。')
122
- return 0
123
-
124
- con = duckdb.connect(DB_PATH)
125
- try:
126
- con.execute("""CREATE TABLE IF NOT EXISTS entity_table_map (
127
- entity_class TEXT,
128
- table_name TEXT,
129
- normalized_table TEXT,
130
- source_file TEXT,
131
- annotation TEXT
132
- )""")
133
- con.execute("CREATE INDEX IF NOT EXISTS idx_etm_table ON entity_table_map(table_name)")
134
- con.execute("CREATE INDEX IF NOT EXISTS idx_etm_norm ON entity_table_map(normalized_table)")
135
- con.execute("CREATE INDEX IF NOT EXISTS idx_etm_class ON entity_table_map(entity_class)")
136
-
137
- if rebuild:
138
- con.execute("DELETE FROM entity_table_map")
139
-
140
- # 用 JSONL COPY 批量插入 (DuckDB 1.4 executemany 慢)
141
- import json as _json
142
- import tempfile
143
- tmp = tempfile.NamedTemporaryFile(suffix='.jsonl', delete=False,
144
- mode='w', encoding='utf-8')
145
- cols = ['entity_class', 'table_name', 'normalized_table', 'source_file', 'annotation']
146
- for row in mappings:
147
- obj = {cols[i]: (row[i] if row[i] is not None else '')
148
- for i in range(len(cols))}
149
- tmp.write(_json.dumps(obj, ensure_ascii=False) + '\n')
150
- tmp.close()
151
- try:
152
- con.execute("COPY entity_table_map FROM '%s' (FORMAT JSON)"
153
- % tmp.name.replace('\\', '/'))
154
- finally:
155
- os.unlink(tmp.name)
156
-
157
- con.execute("INSERT OR REPLACE INTO build_meta(key, value) VALUES (?,?)",
158
- ['entity_table_map_built', str(time.time())])
159
- print('写入 %d Entity→表 映射到 entity_table_map' % len(mappings))
160
- return len(mappings)
161
- finally:
162
- con.close()
163
-
164
-
165
- def query_impact(table_name):
166
- """查某张表关联的 Entity 类 (给"改表影响分析"用)。"""
167
- con = duckdb.connect(DB_PATH, read_only=True)
168
- try:
169
- rows = con.execute(
170
- "SELECT DISTINCT entity_class, source_file FROM entity_table_map "
171
- "WHERE table_name = ?", [table_name]
172
- ).fetchall()
173
- return rows
174
- finally:
175
- con.close()
176
-
177
-
178
- def stats():
179
- """映射统计 + 和 db_tables/db_columns 的关联率。"""
180
- con = duckdb.connect(DB_PATH, read_only=True)
181
- try:
182
- total = con.execute("SELECT COUNT(*) FROM entity_table_map").fetchone()[0]
183
- classes = con.execute("SELECT COUNT(DISTINCT entity_class) FROM entity_table_map").fetchone()[0]
184
- tables_mapped = con.execute("SELECT COUNT(DISTINCT table_name) FROM entity_table_map").fetchone()[0]
185
- # 关联率: 映射的表名 vs 数据库真实表名 (原始 + 归一化)
186
- try:
187
- raw_matched = con.execute("""
188
- SELECT COUNT(DISTINCT m.table_name) FROM entity_table_map m
189
- JOIN db_tables t ON m.table_name = t.table_name
190
- """).fetchone()[0]
191
- norm_matched = con.execute("""
192
- SELECT COUNT(DISTINCT m.normalized_table) FROM entity_table_map m
193
- JOIN db_tables t ON m.normalized_table = t.table_name
194
- """).fetchone()[0]
195
- db_total = con.execute("SELECT COUNT(DISTINCT table_name) FROM db_tables").fetchone()[0]
196
- except Exception:
197
- raw_matched, norm_matched, db_total = 0, 0, 0
198
- print('entity_table_map 统计:')
199
- print(' 总映射: %d' % total)
200
- print(' Entity 类数: %d' % classes)
201
- print(' 映射的表名数: %d' % tables_mapped)
202
- if db_total:
203
- print(' 原始表名匹配: %d/%d (%.0f%%)'
204
- % (raw_matched, db_total, raw_matched / db_total * 100))
205
- print(' 归一化后匹配: %d/%d (%.0f%%)'
206
- % (norm_matched, db_total, norm_matched / db_total * 100))
207
- # 抽样
208
- sample = con.execute(
209
- "SELECT entity_class, table_name FROM entity_table_map LIMIT 8"
210
- ).fetchall()
211
- print(' 抽样:')
212
- for ec, tn in sample:
213
- print(' %s → %s' % (ec, tn))
214
- finally:
215
- con.close()
216
-
217
-
218
- if __name__ == '__main__':
219
- args = sys.argv[1:]
220
- if '--stats' in args:
221
- stats()
222
- else:
223
- build(rebuild='--rebuild' in args)
224
- stats()
1
+ #!/usr/bin/env python
2
+ # -*- coding: utf-8 -*-
3
+ # v3.0 路径自举: 引导到 common/bootstrap, 统一 sys.path 逻辑
4
+ import os as _o, sys as _s
5
+ _f = _o.path.abspath(__file__)
6
+ for _ in range(10):
7
+ _f = _o.path.dirname(_f)
8
+ _cp = _o.path.join(_f, 'foundation')
9
+ if _o.path.isfile(_o.path.join(_cp, 'bootstrap.py')):
10
+ break
11
+ if _cp not in _s.path: _s.path.insert(0, _cp)
12
+ from bootstrap import setup; setup()
13
+
14
+ """kg_link_db.py - 扫代码 @TableName 注解, 建 Entity 类 ↔ 数据库表 的语义桥。
15
+
16
+ 解决"数据层孤岛"问题: kg_db.duckdb 有表结构, 但不知道对应代码里的哪个 Entity。
17
+ 本脚本扫 Java 源码里的 @TableName("xxx") 注解, 提取:
18
+ Entity 类名 → 数据库表名 → (关联到) db_columns 的真实列
19
+
20
+ 调研依据 (FalkorDB "结构遍历 > 语义匹配"):
21
+ - 不靠表名子串猜 endpoint (验证过 0 命中, 不准)
22
+ - 靠代码显式声明的关系 (@TableName 注解) 建边, 这才准
23
+
24
+ 产出表 (写进 kg_db.duckdb, 不动 kg.duckdb 写锁):
25
+ entity_table_map:
26
+ entity_class TEXT -- QualityCaseBo (代码层类名)
27
+ table_name TEXT -- quality_case (数据库表名, 关联 db_tables)
28
+ source_file TEXT -- 哪个 .java 读到的
29
+ annotation TEXT -- @TableName("quality_case") 原文
30
+
31
+ 建好后可遍历:
32
+ 数据库表 → entity_table_map → Entity 类 → (kg.duckdb) Controller/接口
33
+
34
+ 用法:
35
+ python kg_link_db.py # 扫 data/code/ 全量
36
+ python kg_link_db.py --rebuild # 清旧重建
37
+ python kg_link_db.py --stats # 看统计
38
+ """
39
+ import os
40
+ import re
41
+ import sys
42
+ import time
43
+
44
+ SCRIPTS = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) # 子包→scripts/根
45
+ sys.path.insert(0, SCRIPTS)
46
+
47
+ import duckdb
48
+ from foundation.core.paths import DATA_INDEX_DIR, PROJECT_ROOT
49
+
50
+ CODE_DIR = str(PROJECT_ROOT / 'data' / 'code')
51
+ DB_PATH = str(DATA_INDEX_DIR / 'kg_db.duckdb')
52
+
53
+ # @TableName 的两种格式:
54
+ # @TableName("quality_case")
55
+ # @TableName(value = "quality_case")
56
+ RE_TABLENAME = re.compile(
57
+ r'@TableName\s*\(\s*(?:value\s*=\s*)?"([^"]+)"',
58
+ re.IGNORECASE
59
+ )
60
+ # 紧跟注解的 class 声明: class QualityCaseBo
61
+ RE_CLASS = re.compile(r'class\s+(\w+)')
62
+
63
+
64
+ def _normalize_table_name(raw):
65
+ """归一化表名, 提升和数据库真实表的匹配率。
66
+ 处理注解写法不规范的情况:
67
+ fywl_ics_cloud.sys_dept → sys_dept (去库名前缀)
68
+ empContractImport emp_contract_import (camelCase→snake_case)
69
+ quality_case → quality_case (已是规范, 不变)
70
+ """
71
+ t = raw.strip().lower()
72
+ # 去库名前缀 (db.table → table)
73
+ if '.' in t:
74
+ t = t.rsplit('.', 1)[-1]
75
+ # 去常见前缀 t_/tb_/t (数据库表常加这些, 注解有时不加)
76
+ # 注意: 不在这里去前缀, 因为 db_tables 里有的带 t_ 有的不带, 留原样让 JOIN 双向兜
77
+ # camelCase → snake_case (Java 注解里有时写驼峰表名)
78
+ if re.search(r'[a-z][A-Z]', t):
79
+ t = re.sub(r'([a-z0-9])([A-Z])', r'\1_\2', t).lower()
80
+ return t
81
+
82
+
83
+ def _scan_java_files():
84
+ """扫 data/code/ 下所有 .java, 返回 [(entity_class, table_name, normalized_table, source_file, annotation)]。"""
85
+ mappings = []
86
+ scanned = 0
87
+ for root, dirs, files in os.walk(CODE_DIR):
88
+ # 跳过非业务目录
89
+ dirs[:] = [d for d in dirs if d not in (
90
+ '.git', 'node_modules', '__pycache__', 'target', 'test')]
91
+ for fn in files:
92
+ if not fn.endswith('.java'):
93
+ continue
94
+ scanned += 1
95
+ fpath = os.path.join(root, fn)
96
+ try:
97
+ for enc in ('utf-8', 'gbk'):
98
+ try:
99
+ with open(fpath, encoding=enc) as f:
100
+ content = f.read()
101
+ break
102
+ except (UnicodeDecodeError, OSError):
103
+ content = ''
104
+ continue
105
+ except OSError:
106
+ continue
107
+ if '@TableName' not in content:
108
+ continue
109
+ # 找所有 @TableName 注解
110
+ for m in RE_TABLENAME.finditer(content):
111
+ table_name = m.group(1)
112
+ normalized = _normalize_table_name(table_name)
113
+ annotation = content[m.start():m.end() + 1]
114
+ # 找注解之后最近的 class 声明 (Entity 类名)
115
+ after = content[m.end():m.end() + 500]
116
+ cm = RE_CLASS.search(after)
117
+ entity_class = cm.group(1) if cm else os.path.splitext(fn)[0]
118
+ rel = os.path.relpath(fpath, PROJECT_ROOT).replace('\\', '/')
119
+ mappings.append((entity_class, table_name, normalized, rel, annotation))
120
+ return mappings, scanned
121
+
122
+
123
+ def build(rebuild=False):
124
+ """扫代码 @TableName → 写 entity_table_map。"""
125
+ t0 = time.time()
126
+ print('扫描 data/code/ 下的 @TableName 注解...')
127
+ mappings, scanned = _scan_java_files()
128
+ print(' 扫描 %d 个 .java 文件, 找到 %d 个 @TableName 映射 (%.1fs)'
129
+ % (scanned, len(mappings), time.time() - t0))
130
+
131
+ if not mappings:
132
+ print('未找到任何 @TableName 注解。检查 data/code/ 是否有 Java 源码。')
133
+ return 0
134
+
135
+ con = duckdb.connect(DB_PATH)
136
+ try:
137
+ con.execute("""CREATE TABLE IF NOT EXISTS entity_table_map (
138
+ entity_class TEXT,
139
+ table_name TEXT,
140
+ normalized_table TEXT,
141
+ source_file TEXT,
142
+ annotation TEXT
143
+ )""")
144
+ con.execute("CREATE INDEX IF NOT EXISTS idx_etm_table ON entity_table_map(table_name)")
145
+ con.execute("CREATE INDEX IF NOT EXISTS idx_etm_norm ON entity_table_map(normalized_table)")
146
+ con.execute("CREATE INDEX IF NOT EXISTS idx_etm_class ON entity_table_map(entity_class)")
147
+
148
+ if rebuild:
149
+ con.execute("DELETE FROM entity_table_map")
150
+
151
+ # 用 JSONL COPY 批量插入 (DuckDB 1.4 executemany 慢)
152
+ import json as _json
153
+ import tempfile
154
+ tmp = tempfile.NamedTemporaryFile(suffix='.jsonl', delete=False,
155
+ mode='w', encoding='utf-8')
156
+ cols = ['entity_class', 'table_name', 'normalized_table', 'source_file', 'annotation']
157
+ for row in mappings:
158
+ obj = {cols[i]: (row[i] if row[i] is not None else '')
159
+ for i in range(len(cols))}
160
+ tmp.write(_json.dumps(obj, ensure_ascii=False) + '\n')
161
+ tmp.close()
162
+ try:
163
+ con.execute("COPY entity_table_map FROM '%s' (FORMAT JSON)"
164
+ % tmp.name.replace('\\', '/'))
165
+ finally:
166
+ os.unlink(tmp.name)
167
+
168
+ con.execute("INSERT OR REPLACE INTO build_meta(key, value) VALUES (?,?)",
169
+ ['entity_table_map_built', str(time.time())])
170
+ print('写入 %d Entity→表 映射到 entity_table_map' % len(mappings))
171
+ return len(mappings)
172
+ finally:
173
+ con.close()
174
+
175
+
176
+ def query_impact(table_name):
177
+ """查某张表关联的 Entity 类 (给"改表影响分析"用)。"""
178
+ con = duckdb.connect(DB_PATH, read_only=True)
179
+ try:
180
+ rows = con.execute(
181
+ "SELECT DISTINCT entity_class, source_file FROM entity_table_map "
182
+ "WHERE table_name = ?", [table_name]
183
+ ).fetchall()
184
+ return rows
185
+ finally:
186
+ con.close()
187
+
188
+
189
+ def stats():
190
+ """映射统计 + 和 db_tables/db_columns 的关联率。"""
191
+ con = duckdb.connect(DB_PATH, read_only=True)
192
+ try:
193
+ total = con.execute("SELECT COUNT(*) FROM entity_table_map").fetchone()[0]
194
+ classes = con.execute("SELECT COUNT(DISTINCT entity_class) FROM entity_table_map").fetchone()[0]
195
+ tables_mapped = con.execute("SELECT COUNT(DISTINCT table_name) FROM entity_table_map").fetchone()[0]
196
+ # 关联率: 映射的表名 vs 数据库真实表名 (原始 + 归一化)
197
+ try:
198
+ raw_matched = con.execute("""
199
+ SELECT COUNT(DISTINCT m.table_name) FROM entity_table_map m
200
+ JOIN db_tables t ON m.table_name = t.table_name
201
+ """).fetchone()[0]
202
+ norm_matched = con.execute("""
203
+ SELECT COUNT(DISTINCT m.normalized_table) FROM entity_table_map m
204
+ JOIN db_tables t ON m.normalized_table = t.table_name
205
+ """).fetchone()[0]
206
+ db_total = con.execute("SELECT COUNT(DISTINCT table_name) FROM db_tables").fetchone()[0]
207
+ except Exception:
208
+ raw_matched, norm_matched, db_total = 0, 0, 0
209
+ print('entity_table_map 统计:')
210
+ print(' 总映射: %d' % total)
211
+ print(' Entity 类数: %d' % classes)
212
+ print(' 映射的表名数: %d' % tables_mapped)
213
+ if db_total:
214
+ print(' 原始表名匹配: %d/%d (%.0f%%)'
215
+ % (raw_matched, db_total, raw_matched / db_total * 100))
216
+ print(' 归一化后匹配: %d/%d (%.0f%%)'
217
+ % (norm_matched, db_total, norm_matched / db_total * 100))
218
+ # 抽样
219
+ sample = con.execute(
220
+ "SELECT entity_class, table_name FROM entity_table_map LIMIT 8"
221
+ ).fetchall()
222
+ print(' 抽样:')
223
+ for ec, tn in sample:
224
+ print(' %s → %s' % (ec, tn))
225
+ finally:
226
+ con.close()
227
+
228
+
229
+ if __name__ == '__main__':
230
+ args = sys.argv[1:]
231
+ if '--stats' in args:
232
+ stats()
233
+ else:
234
+ build(rebuild='--rebuild' in args)
235
+ stats()