@hupan56/wlkj 2.7.11 → 3.0.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 (152) hide show
  1. package/bin/cli.js +375 -78
  2. package/package.json +29 -29
  3. package/templates/.qoder/.runtime/ctx-cache-5660152f1d6dd819.md +23 -0
  4. package/templates/.qoder/.runtime/ctx-cache-afdce0dac06b25b0.md +23 -0
  5. package/templates/.qoder/.runtime/search-cache-eae7644e7b122f35.txt +1 -0
  6. package/templates/.qoder/learning/eval-history.jsonl +28 -0
  7. package/templates/data/index/wiki-index.json +8 -0
  8. package/templates/qoder/agents/insight-planning.md +1 -1
  9. package/templates/qoder/agents/insight-research.md +28 -15
  10. package/templates/qoder/commands/optional/wl-insight.md +159 -161
  11. package/templates/qoder/commands/optional/wl-report.md +4 -4
  12. package/templates/qoder/commands/optional/wl-status.md +2 -2
  13. package/templates/qoder/commands/wl-code.md +2 -2
  14. package/templates/qoder/commands/wl-design.md +2 -2
  15. package/templates/qoder/commands/wl-init.md +3 -3
  16. package/templates/qoder/commands/wl-prd.md +2 -2
  17. package/templates/qoder/commands/wl-req.md +43 -0
  18. package/templates/qoder/commands/wl-search.md +8 -8
  19. package/templates/qoder/commands/wl-task.md +17 -17
  20. package/templates/qoder/commands/wl-test.md +41 -15
  21. package/templates/qoder/config.yaml +17 -1
  22. package/templates/qoder/hooks/session-start.py +12 -22
  23. package/templates/qoder/nul +4 -0
  24. package/templates/qoder/rules/wl-pipeline.md +22 -48
  25. package/templates/qoder/scripts/README.md +139 -0
  26. package/templates/qoder/scripts/common/autotest_auth.py +109 -0
  27. package/templates/qoder/scripts/common/bootstrap.py +145 -0
  28. package/templates/qoder/scripts/common/check_publish.py +98 -0
  29. package/templates/qoder/scripts/common/cmd_registry.py +112 -0
  30. package/templates/qoder/scripts/common/config.py +187 -0
  31. package/templates/qoder/scripts/common/contract.py +317 -0
  32. package/templates/qoder/scripts/common/developer.py +2 -1
  33. package/templates/qoder/scripts/common/feishu.py +10 -9
  34. package/templates/qoder/scripts/common/guard.py +159 -0
  35. package/templates/qoder/scripts/common/identity.py +121 -2
  36. package/templates/qoder/scripts/common/kg_capabilities.py +182 -0
  37. package/templates/qoder/scripts/common/mcp_base.py +268 -0
  38. package/templates/qoder/scripts/common/paths.py +187 -1
  39. package/templates/qoder/scripts/common/result.py +223 -0
  40. package/templates/qoder/scripts/common/roles.py +60 -0
  41. package/templates/qoder/scripts/common/task_utils.py +21 -9
  42. package/templates/qoder/scripts/common/test_extract.py +115 -0
  43. package/templates/qoder/scripts/kg/__init__.py +11 -0
  44. package/templates/qoder/scripts/kg/build_entity_registry.py +196 -0
  45. package/templates/qoder/scripts/kg/build_relations.py +127 -0
  46. package/templates/qoder/scripts/{build_style_index.py → kg/build_style_index.py} +39 -10
  47. package/templates/qoder/scripts/kg/build_workflows.py +144 -0
  48. package/templates/qoder/scripts/{context_pack.py → kg/context_pack.py} +18 -11
  49. package/templates/qoder/scripts/{enrich_prompt.py → kg/enrich_prompt.py} +232 -226
  50. package/templates/qoder/scripts/{extract_api_params.py → kg/extract.py} +398 -246
  51. package/templates/qoder/scripts/{kg.py → kg/kg.py} +638 -708
  52. package/templates/qoder/scripts/{kg_build.py → kg/kg_build.py} +618 -612
  53. package/templates/qoder/scripts/{kg_build_db.py → kg/kg_build_db.py} +333 -327
  54. package/templates/qoder/scripts/{kg_duckdb.py → kg/kg_duckdb.py} +38 -37
  55. package/templates/qoder/scripts/{kg_incremental.py → kg/kg_incremental.py} +420 -393
  56. package/templates/qoder/scripts/{kg_link_db.py → kg/kg_link_db.py} +230 -224
  57. package/templates/qoder/scripts/{kg_semantic.py → kg/kg_semantic.py} +156 -150
  58. package/templates/qoder/scripts/kg/prefetch.py +359 -0
  59. package/templates/qoder/scripts/{search_index.py → kg/search_index.py} +70 -14
  60. package/templates/qoder/scripts/mcp/__init__.py +11 -0
  61. package/templates/qoder/scripts/{kg_mcp_server.py → mcp/kg_mcp_server.py} +77 -272
  62. package/templates/qoder/scripts/{lanhu_stdio_wrapper.py → mcp/lanhu_stdio_wrapper.py} +125 -119
  63. package/templates/qoder/scripts/{check_mcp.py → mcp/mcp_doctor.py} +515 -298
  64. package/templates/qoder/scripts/{mcp_launcher.py → mcp/mcp_launcher.py} +442 -414
  65. package/templates/qoder/scripts/{mysql_mcp_server.py → mcp/mysql_mcp_server.py} +347 -396
  66. package/templates/qoder/scripts/{zentao_mcp_server.py → mcp/zentao_mcp_server.py} +384 -424
  67. package/templates/qoder/scripts/report/__init__.py +11 -0
  68. package/templates/qoder/scripts/{add_session.py → report/add_session.py} +250 -244
  69. package/templates/qoder/scripts/{archive_prd.py → report/archive_prd.py} +383 -377
  70. package/templates/qoder/scripts/{eval_prd.py → report/eval_prd.py} +73 -11
  71. package/templates/qoder/scripts/{export.py → report/export.py} +63 -0
  72. package/templates/qoder/scripts/{fill_prototype.py → report/fill_prototype.py} +6 -0
  73. package/templates/qoder/scripts/{gen_design_doc.py → report/gen_design_doc.py} +400 -394
  74. package/templates/qoder/scripts/{learn.py → report/learn.py} +152 -146
  75. package/templates/qoder/scripts/{learn_aggregate.py → report/learn_aggregate.py} +207 -201
  76. package/templates/qoder/scripts/{report.py → report/report.py} +287 -281
  77. package/templates/qoder/scripts/report/req.py +222 -0
  78. package/templates/qoder/scripts/report/role.py +33 -0
  79. package/templates/qoder/scripts/{status.py → report/status.py} +634 -628
  80. package/templates/qoder/scripts/setup/__init__.py +11 -0
  81. package/templates/qoder/scripts/setup/carriers.py +662 -0
  82. package/templates/qoder/scripts/{init_doctor.py → setup/init_doctor.py} +63 -26
  83. package/templates/qoder/scripts/{install_qoderwork.py → setup/install_qoderwork.py} +36 -26
  84. package/templates/qoder/scripts/{platform_doctor.py → setup/platform_doctor.py} +265 -259
  85. package/templates/qoder/scripts/{repo_root.py → setup/repo_root.py} +112 -106
  86. package/templates/qoder/scripts/{setup.py → setup/setup.py} +147 -0
  87. package/templates/qoder/scripts/{setup_lanhu.py → setup/setup_lanhu.py} +973 -963
  88. package/templates/qoder/scripts/task/__init__.py +11 -0
  89. package/templates/qoder/scripts/{git_sync.py → task/git_sync.py} +52 -31
  90. package/templates/qoder/scripts/{syncgate.py → task/syncgate.py} +6 -0
  91. package/templates/qoder/scripts/task/task.py +221 -0
  92. package/templates/qoder/scripts/task/task_lifecycle.py +596 -0
  93. package/templates/qoder/scripts/task/task_query.py +161 -0
  94. package/templates/qoder/scripts/task/task_relations.py +424 -0
  95. package/templates/qoder/scripts/{team_sync.py → task/team_sync.py} +93 -20
  96. package/templates/qoder/scripts/test/__init__.py +11 -0
  97. package/templates/qoder/scripts/{autotest.py → test/autotest.py} +1174 -1751
  98. package/templates/qoder/scripts/{autotest_batch.py → test/autotest_batch.py} +242 -224
  99. package/templates/qoder/scripts/test/autotest_data.py +675 -0
  100. package/templates/qoder/scripts/{autotest_run.py → test/autotest_run.py} +309 -297
  101. package/templates/qoder/scripts/{benchmark.py → test/benchmark.py} +6 -0
  102. package/templates/qoder/scripts/{kg_auto_login.py → test/kg_auto_login.py} +202 -196
  103. package/templates/qoder/scripts/{kg_test_runner.py → test/kg_test_runner.py} +7 -1
  104. package/templates/qoder/scripts/{page_probe.py → test/page_probe.py} +465 -459
  105. package/templates/qoder/scripts/wlkj.py +116 -0
  106. package/templates/qoder/settings.json +1 -10
  107. package/templates/qoder/skills/design-import/SKILL.md +226 -226
  108. package/templates/qoder/skills/design-review/SKILL.md +82 -82
  109. package/templates/qoder/skills/prd-generator/SKILL.md +26 -16
  110. package/templates/qoder/skills/prd-review/SKILL.md +5 -5
  111. package/templates/qoder/skills/prototype-generator/SKILL.md +256 -256
  112. package/templates/qoder/skills/spec-coder/SKILL.md +4 -4
  113. package/templates/qoder/skills/spec-generator/SKILL.md +4 -4
  114. package/templates/qoder/skills/test-generator/SKILL.md +5 -5
  115. package/templates/qoder/skills/wl-code/SKILL.md +4 -4
  116. package/templates/qoder/skills/wl-commit/SKILL.md +4 -4
  117. package/templates/qoder/skills/wl-design/SKILL.md +3 -3
  118. package/templates/qoder/skills/wl-init/SKILL.md +8 -8
  119. package/templates/qoder/skills/wl-insight/SKILL.md +5 -5
  120. package/templates/qoder/skills/wl-prd-full/SKILL.md +6 -6
  121. package/templates/qoder/skills/wl-prd-quick/SKILL.md +6 -6
  122. package/templates/qoder/skills/wl-prd-review/SKILL.md +4 -4
  123. package/templates/qoder/skills/wl-report/SKILL.md +7 -7
  124. package/templates/qoder/skills/wl-search/SKILL.md +13 -13
  125. package/templates/qoder/skills/wl-spec/SKILL.md +5 -5
  126. package/templates/qoder/skills/wl-status/SKILL.md +5 -5
  127. package/templates/qoder/skills/wl-task/SKILL.md +6 -6
  128. package/templates/qoder/skills/wl-test/SKILL.md +102 -39
  129. package/templates/root/AGENTS.md +39 -40
  130. package/templates/qoder/hooks/inject-workflow-state.py +0 -169
  131. package/templates/qoder/scripts/__pycache__/check_mcp_launch.cpython-39.pyc +0 -0
  132. package/templates/qoder/scripts/__pycache__/install_qoderwork.cpython-39.pyc +0 -0
  133. package/templates/qoder/scripts/__pycache__/mcp_launcher.cpython-39.pyc +0 -0
  134. package/templates/qoder/scripts/__pycache__/platform_doctor.cpython-39.pyc +0 -0
  135. package/templates/qoder/scripts/check_carriers.py +0 -238
  136. package/templates/qoder/scripts/check_mcp_launch.py +0 -183
  137. package/templates/qoder/scripts/check_qoderwork_consistency.py +0 -166
  138. package/templates/qoder/scripts/collect_prds.py +0 -31
  139. package/templates/qoder/scripts/common/mentions.py +0 -134
  140. package/templates/qoder/scripts/common/utf8.py +0 -38
  141. package/templates/qoder/scripts/extract_routes.py +0 -54
  142. package/templates/qoder/scripts/extract_routes_tree.py +0 -78
  143. package/templates/qoder/scripts/handoff.py +0 -22
  144. package/templates/qoder/scripts/init_developer.py +0 -76
  145. package/templates/qoder/scripts/parse_prds.py +0 -33
  146. package/templates/qoder/scripts/role.py +0 -51
  147. package/templates/qoder/scripts/sync_carriers.py +0 -259
  148. package/templates/qoder/scripts/task.py +0 -1261
  149. package/templates/qoder/scripts/workspace_init.py +0 -102
  150. package/templates/qoder/skills/prompt-enrich/SKILL.md +0 -90
  151. package/templates/qoder/skills/prototype-generator/SKILL.md.zcode-79180-2af4721f-f9a6-412c-88db-c0af680d211b.tmp +0 -0
  152. /package/templates/qoder/scripts/{secure-ls.js → test/secure-ls.js} +0 -0
@@ -1,393 +1,420 @@
1
- # -*- coding: utf-8 -*-
2
- """
3
- Incremental UPSERT builder for the DuckDB knowledge graph.
4
-
5
- Instead of full rebuild (build_style_index.py → kg_duckdb.py bulk_import),
6
- this takes a list of changed files (from git diff) and only updates the
7
- entities + edges that belong to those files.
8
-
9
- Performance: ~0.2s per file vs 60s full rebuild.
10
- For 500 changed files: ~2 minutes incremental vs 5+ minutes full rebuild.
11
-
12
- Usage:
13
- python kg_incremental.py # auto-detect changes via git diff
14
- python kg_incremental.py file1.vue file2.ts # explicit file list
15
- """
16
- import json
17
- import os
18
- import re
19
- import sys
20
-
21
- SCRIPTS = os.path.join(os.path.dirname(os.path.abspath(__file__)))
22
- sys.path.insert(0, SCRIPTS)
23
-
24
- from common.paths import DATA_INDEX_DIR, PROJECT_ROOT
25
- from kg_duckdb import get_db, init_schema, upsert_entity, upsert_alias, upsert_edge, delete_edges_from
26
- from common.ts_extract import ts_build_api_fn_table, ts_trace_clicks
27
- from common.extract import build_api_fn_table
28
- from common.test_extract import extract_test_assertions
29
-
30
- CODE_DIR = str(PROJECT_ROOT / 'data' / 'code')
31
- INDEX_DIR = str(DATA_INDEX_DIR)
32
-
33
-
34
- def get_changed_files():
35
- """Auto-detect ALL changed files since last kg_build via stored commit hash.
36
-
37
- Records the last-built commit per project in build_meta table.
38
- Next run diffs from that commit to HEAD, capturing ALL changes (not just last 2 commits).
39
- """
40
- changed = []
41
- if not os.path.isdir(CODE_DIR):
42
- return changed
43
- import subprocess
44
-
45
- # Read last-built commits from DuckDB build_meta
46
- last_commits = {}
47
- try:
48
- con = get_db()
49
- rows = con.execute("SELECT key, value FROM build_meta WHERE key LIKE 'last_commit_%'").fetchall()
50
- con.close()
51
- for k, v in rows:
52
- last_commits[k.replace('last_commit_', '')] = v
53
- except Exception:
54
- pass
55
-
56
- current_commits = {}
57
- for proj in os.listdir(CODE_DIR):
58
- proj_path = os.path.join(CODE_DIR, proj)
59
- if not os.path.isdir(os.path.join(proj_path, '.git')):
60
- continue
61
- try:
62
- # Get current HEAD
63
- r = subprocess.run(['git', 'rev-parse', 'HEAD'],
64
- cwd=proj_path, capture_output=True, text=True, encoding='utf-8')
65
- if r.returncode != 0:
66
- continue
67
- current_head = r.stdout.strip()
68
- current_commits[proj] = current_head
69
-
70
- # Determine diff range: from last-built commit to HEAD
71
- last_built = last_commits.get(proj)
72
- if last_built and last_built != current_head:
73
- # Diff from last build to now — captures ALL changes since last build
74
- diff_args = ['git', 'diff', '--name-only', last_built, current_head]
75
- elif not last_built:
76
- # First run get last 7 days of changes (or all if repo is new)
77
- r3 = subprocess.run(['git', 'log', '--since=7 days ago', '--oneline'],
78
- cwd=proj_path, capture_output=True, text=True, encoding='utf-8')
79
- log_lines = [l for l in r3.stdout.strip().split('\n') if l.strip()]
80
- if len(log_lines) >= 2:
81
- old_commit = log_lines[-1].split()[0]
82
- diff_args = ['git', 'diff', '--name-only', old_commit, current_head]
83
- else:
84
- continue # No changes or single commit
85
- else:
86
- continue # Already up to date, no diff needed
87
-
88
- r2 = subprocess.run(diff_args,
89
- cwd=proj_path, capture_output=True, text=True, encoding='utf-8')
90
- for f in r2.stdout.strip().split('\n'):
91
- f = f.strip()
92
- if f:
93
- changed.append(os.path.join(proj_path, f.replace('/', os.sep)))
94
- except Exception:
95
- pass
96
-
97
- # Save current commits as last-built
98
- try:
99
- con = get_db()
100
- for proj, commit in current_commits.items():
101
- con.execute(
102
- "INSERT OR REPLACE INTO build_meta VALUES (?, ?)",
103
- ['last_commit_%s' % proj, commit]
104
- )
105
- con.close()
106
- except Exception:
107
- pass
108
-
109
- return changed
110
-
111
-
112
- def incremental_update(changed_files):
113
- """Update DuckDB incrementally for a list of changed files.
114
-
115
- For each file:
116
- - .vue: re-extract trace_clicks, UPSERT button entities + click edges
117
- - .ts (api): re-extract api functions, UPSERT endpoint entities
118
- - .java (test): re-extract test assertions, UPSERT test entities
119
- """
120
- con = get_db()
121
- init_schema(con)
122
-
123
- stats = {'vue': 0, 'ts': 0, 'java_test': 0, 'edges_updated': 0, 'entities_upserted': 0}
124
-
125
- # 判断是否有 api 文件变更 — 决定是否需要重建 api_table
126
- has_api_changes = any(
127
- f.replace('\\', '/').endswith('.ts') and '/api/' in f.replace('\\', '/')
128
- for f in changed_files if os.path.isfile(f)
129
- )
130
-
131
- api_dir = _find_api_dir()
132
-
133
- if has_api_changes:
134
- # api 文件变了 — 必须重建完整 api_table
135
- api_table = ts_build_api_fn_table(api_dir) if api_dir else {}
136
- # 缓存到文件, 供下次增量复用
137
- if api_table and api_dir:
138
- _save_api_cache(api_table, api_dir)
139
- else:
140
- # api 文件没变 从缓存加载 (毫秒级, 不扫文件系统)
141
- api_table = _load_api_cache(api_dir)
142
- if not api_table:
143
- # 缓存不存在 (首次运行) 重建并缓存
144
- api_table = ts_build_api_fn_table(api_dir) if api_dir else {}
145
- if api_table and api_dir:
146
- _save_api_cache(api_table, api_dir)
147
-
148
- for filepath in changed_files:
149
- if not os.path.isfile(filepath):
150
- # File was deleted clean up its entities
151
- _cleanup_deleted_file(con, filepath)
152
- continue
153
-
154
- rel_path = filepath.replace('\\', '/')
155
-
156
- if filepath.endswith('.vue'):
157
- stats['vue'] += 1
158
- _update_vue_file(con, filepath, api_table, stats)
159
-
160
- elif filepath.endswith('.ts') and '/api/' in rel_path:
161
- stats['ts'] += 1
162
- _update_api_file(con, filepath, stats)
163
-
164
- elif filepath.endswith('Test.java'):
165
- stats['java_test'] += 1
166
- _update_test_file(con, filepath, stats)
167
-
168
- # After file updates, refresh PRD + Wiki from git_sync's JSON output
169
- _refresh_prds_and_wiki(con, stats)
170
-
171
- con.close()
172
- return stats
173
-
174
-
175
- def _refresh_prds_and_wiki(con, stats):
176
- """Import PRD index + Wiki from git_sync's JSON output into DuckDB.
177
-
178
- git_sync.py writes these as JSON; kg_incremental reads them and UPSERTs
179
- into DuckDB so MCP tools can query them.
180
- """
181
- INDEX = str(DATA_INDEX_DIR)
182
- import json as _json
183
-
184
- # PRD
185
- prd_path = os.path.join(INDEX, '_build_cache', 'prd-index.json')
186
- if not os.path.isfile(prd_path):
187
- prd_path = os.path.join(INDEX, 'prd-index.json')
188
- if os.path.isfile(prd_path):
189
- try:
190
- prds = _json.load(open(prd_path, encoding='utf-8'))
191
- con.execute("DELETE FROM prds")
192
- rows = [(f, info.get('title', ''),
193
- _json.dumps(info.get('keywords', []), ensure_ascii=False),
194
- _json.dumps(info.get('features', []), ensure_ascii=False),
195
- _json.dumps(info.get('cn_terms', []), ensure_ascii=False))
196
- for f, info in prds.items()]
197
- if rows:
198
- _batch_insert(con, 'prds', rows, ignore=True)
199
- stats['prds_refreshed'] = len(rows)
200
- except Exception:
201
- pass
202
-
203
- # Wiki
204
- wiki_path = os.path.join(INDEX, 'wiki-index.json')
205
- if os.path.isfile(wiki_path):
206
- try:
207
- wiki = _json.load(open(wiki_path, encoding='utf-8'))
208
- con.execute("DELETE FROM wiki")
209
- wiki_rows = []
210
- for kw, entries in (wiki.get('index', {}) if isinstance(wiki, dict) else {}).items():
211
- for e in (entries if isinstance(entries, list) else []):
212
- wiki_rows.append((kw, e.get('project', ''), e.get('title', ''),
213
- e.get('md_path', ''), e.get('description', '')))
214
- if wiki_rows:
215
- _batch_insert(con, 'wiki', wiki_rows)
216
- stats['wiki_refreshed'] = len(wiki_rows)
217
- except Exception:
218
- pass
219
-
220
-
221
- def _find_api_dir():
222
- """Find the fywl-ui api directory."""
223
- apps = os.path.join(CODE_DIR, 'fywl-ui', 'apps')
224
- if os.path.isdir(apps):
225
- for app in os.listdir(apps):
226
- candidate = os.path.join(apps, app, 'src', 'api')
227
- if os.path.isdir(candidate):
228
- return candidate
229
- return None
230
-
231
-
232
- def _load_api_table_from_duckdb(con):
233
- """从 DuckDB 加载 api_table (api 函数名→端点), 避免重建。"""
234
- table = {}
235
- try:
236
- rows = con.execute("SELECT id, props FROM entities WHERE type = 'ENDPOINT'").fetchall()
237
- for eid, props_json in rows:
238
- url = eid.replace('E:', '')
239
- props = json.loads(props_json) if props_json else {}
240
- fn_name = props.get('api_fn', '')
241
- if fn_name:
242
- table[fn_name] = {'url': url, 'verb': props.get('verb', '?'),
243
- 'file': props.get('source_file', '')}
244
- except Exception:
245
- pass
246
- return table
247
-
248
-
249
- _API_CACHE_PATH = os.path.join(INDEX_DIR, '.api-table-cache.json')
250
-
251
-
252
- def _save_api_cache(api_table, api_dir):
253
- """保存 api_table 到缓存文件 ( api_dir mtime 作为失效依据)。"""
254
- import time
255
- cache = {
256
- 'api_dir_mtime': os.path.getmtime(api_dir) if os.path.isdir(api_dir) else 0,
257
- 'saved_at': time.time(),
258
- 'table': api_table,
259
- }
260
- with open(_API_CACHE_PATH, 'w', encoding='utf-8') as f:
261
- json.dump(cache, f, ensure_ascii=False)
262
-
263
-
264
- def _load_api_cache(api_dir):
265
- """从缓存文件加载 api_table。如果缓存不存在或过期返回 {}。"""
266
- if not os.path.isfile(_API_CACHE_PATH):
267
- return {}
268
- try:
269
- cache = json.load(open(_API_CACHE_PATH, encoding='utf-8'))
270
- # 检查 api_dir 的 mtime 是否变了
271
- current_mtime = os.path.getmtime(api_dir) if api_dir and os.path.isdir(api_dir) else 0
272
- if cache.get('api_dir_mtime') == current_mtime:
273
- return cache.get('table', {})
274
- except Exception:
275
- pass
276
- return {}
277
-
278
-
279
- def _update_vue_file(con, filepath, api_table, stats):
280
- """Re-extract traces from one Vue file and UPSERT its button entities + edges."""
281
- try:
282
- content = open(filepath, 'rb').read().decode('utf-8', errors='replace')
283
- except Exception:
284
- return
285
- if '@click' not in content:
286
- return
287
-
288
- traces = ts_trace_clicks(filepath, content, api_table)
289
- vue_norm = filepath.replace('\\', '/')
290
- # Normalize vue_short — MUST match kg_build.py's format exactly
291
- if '/views/' in vue_norm:
292
- vue_short = vue_norm.split('/views/')[-1]
293
- elif '/pages/' in vue_norm:
294
- vue_short = vue_norm.split('/pages/')[-1]
295
- else:
296
- vue_short = os.path.basename(filepath)
297
-
298
- for t in traces:
299
- handler = t.get('handler', '')
300
- eid = 'B:%s:%s' % (vue_short, handler)
301
- # UPSERT entity (INSERT OR REPLACE, no duplicates)
302
- upsert_entity(con, eid, 'BUTTON', handler, '',
303
- {'button_text': t.get('button', ''),
304
- 'vue_file': vue_norm,
305
- 'confidence': t.get('confidence', '')})
306
- upsert_alias(con, handler.lower(), eid)
307
- stats['entities_upserted'] += 1
308
-
309
- # Delete ALL old edges from this button first, then insert fresh
310
- delete_edges_from(con, eid)
311
- ep = t.get('endpoint')
312
- if ep and ep != 'None':
313
- to_eid = 'E:%s' % ep
314
- upsert_edge(con, eid, to_eid, 'clicks', props={
315
- 'button': t.get('button', ''),
316
- 'confidence': t.get('confidence', ''),
317
- })
318
- stats['edges_updated'] += 1
319
-
320
-
321
- def _update_api_file(con, filepath, stats):
322
- """Re-extract API functions from one .ts file and UPSERT endpoint entities."""
323
- # Re-use the bulk api_table extraction on this single file's directory
324
- api_dir = os.path.dirname(filepath)
325
- file_table = build_api_fn_table(api_dir)
326
- for fn_name, info in file_table.items():
327
- # Only update functions defined in THIS file
328
- if info.get('file', '').replace('\\', '/') != filepath.replace('\\', '/'):
329
- continue
330
- eid = 'E:%s' % info['url']
331
- upsert_entity(con, eid, 'ENDPOINT', info['url'], '',
332
- {'verb': info.get('verb', '?'),
333
- 'api_fn': fn_name,
334
- 'source_file': filepath.replace('\\', '/')})
335
- upsert_alias(con, fn_name.lower(), eid)
336
- stats['entities_upserted'] += 1
337
-
338
-
339
- def _update_test_file(con, filepath, stats):
340
- """Re-extract test assertions from one Test.java and UPSERT test entities."""
341
- tests = extract_test_assertions(os.path.dirname(filepath))
342
- # Filter to only this file
343
- file_norm = filepath.replace('\\', '/')
344
- for t in tests:
345
- if t.get('file', '').replace('\\', '/') != file_norm:
346
- continue
347
- name = '%s.%s' % (t['class'], t['method'])
348
- eid = 'T:%s' % name
349
- upsert_entity(con, eid, 'TEST', name, '',
350
- {'display': t.get('display_name', ''),
351
- 'deps': t.get('injected_deps', []),
352
- 'assertions': t.get('assertions', []),
353
- 'package': t.get('package', '')})
354
- upsert_alias(con, name.lower(), eid)
355
- stats['entities_upserted'] += 1
356
-
357
-
358
- def _cleanup_deleted_file(con, filepath):
359
- """Remove entities belonging to a deleted file."""
360
- rel = filepath.replace('\\', '/')
361
- # Delete buttons from this vue file
362
- if '.vue' in rel:
363
- vue_short = rel.split('/views/')[-1] if '/views/' in rel else os.path.basename(rel)
364
- con.execute("DELETE FROM entities WHERE id LIKE 'B:%s:%%'" % vue_short.replace("'", "''"))
365
- con.execute("DELETE FROM edges WHERE from_id LIKE 'B:%s:%%'" % vue_short.replace("'", "''"))
366
-
367
-
368
- if __name__ == '__main__':
369
- if len(sys.argv) > 1:
370
- changed = sys.argv[1:]
371
- else:
372
- print('Auto-detecting changes via git diff...')
373
- changed = get_changed_files()
374
-
375
- if not changed:
376
- print('No changes detected.')
377
- sys.exit(0)
378
-
379
- # Filter to relevant file types
380
- relevant = [f for f in changed if f.endswith(('.vue', '.ts', '.java'))]
381
- print('Changed files: %d total, %d relevant' % (len(changed), len(relevant)))
382
-
383
- if relevant:
384
- import time
385
- t0 = time.time()
386
- stats = incremental_update(relevant)
387
- elapsed = time.time() - t0
388
- print('Incremental update done in %.1fs' % elapsed)
389
- print(' Vue: %d, TS: %d, Java: %d' % (stats['vue'], stats['ts'], stats['java_test']))
390
- print(' Entities UPSERTed: %d' % stats['entities_upserted'])
391
- print(' Edges updated: %d' % stats['edges_updated'])
392
- else:
393
- print('No relevant files to update.')
1
+ # -*- coding: utf-8 -*-
2
+ # v3.0 路径自举: 引导到 common/bootstrap, 统一 sys.path 逻辑
3
+ import os as _o, sys as _s
4
+ _cp = _o.path.join(_o.path.dirname(_o.path.dirname(_o.path.abspath(__file__))), 'common')
5
+ if _cp not in _s.path: _s.path.insert(0, _cp)
6
+ from bootstrap import setup; setup()
7
+
8
+ """
9
+ Incremental UPSERT builder for the DuckDB knowledge graph.
10
+
11
+ Instead of full rebuild (build_style_index.py → kg_duckdb.py bulk_import),
12
+ this takes a list of changed files (from git diff) and only updates the
13
+ entities + edges that belong to those files.
14
+
15
+ Performance: ~0.2s per file vs 60s full rebuild.
16
+ For 500 changed files: ~2 minutes incremental vs 5+ minutes full rebuild.
17
+
18
+ Usage:
19
+ python kg_incremental.py # auto-detect changes via git diff
20
+ python kg_incremental.py file1.vue file2.ts # explicit file list
21
+ """
22
+ import json
23
+ import os
24
+ import re
25
+ import sys
26
+
27
+ SCRIPTS = os.path.join(os.path.dirname(os.path.abspath(__file__)))
28
+ sys.path.insert(0, SCRIPTS)
29
+
30
+ from common.paths import DATA_INDEX_DIR, PROJECT_ROOT
31
+ from kg_duckdb import get_db, init_schema, upsert_entity, upsert_alias, upsert_edge, delete_edges_from
32
+ from common.ts_extract import ts_build_api_fn_table, ts_trace_clicks
33
+ from common.extract import build_api_fn_table
34
+ from common.test_extract import extract_test_assertions
35
+
36
+ CODE_DIR = str(PROJECT_ROOT / 'data' / 'code')
37
+ INDEX_DIR = str(DATA_INDEX_DIR)
38
+
39
+
40
+ def _git(*args, cwd=None, timeout=None):
41
+ """git 命令包装(与 git_sync._git 一致)。网络操作必传 timeout; 本地操作可不传。
42
+
43
+ 返回 rc=124 表示超时, rc=127 表示 git 未安装, 不让进程崩溃或挂死。
44
+ """
45
+ import subprocess
46
+ try:
47
+ kw = dict(cwd=cwd, capture_output=True, text=True, encoding='utf-8', errors='replace')
48
+ if timeout:
49
+ kw['timeout'] = timeout
50
+ return subprocess.run(['git'] + list(args), **kw)
51
+ except FileNotFoundError:
52
+ import types
53
+ return types.SimpleNamespace(returncode=127, stdout='', stderr='git not installed')
54
+ except subprocess.TimeoutExpired:
55
+ import types
56
+ return types.SimpleNamespace(returncode=124, stdout='', stderr='git {} timed out'.format(' '.join(args[:3])))
57
+
58
+
59
+ def get_changed_files():
60
+ """Auto-detect ALL changed files since last kg_build via stored commit hash.
61
+
62
+ Records the last-built commit per project in build_meta table.
63
+ Next run diffs from that commit to HEAD, capturing ALL changes (not just last 2 commits).
64
+ """
65
+ changed = []
66
+ if not os.path.isdir(CODE_DIR):
67
+ return changed
68
+ import subprocess
69
+
70
+ # Read last-built commits from DuckDB build_meta
71
+ last_commits = {}
72
+ try:
73
+ con = get_db()
74
+ rows = con.execute("SELECT key, value FROM build_meta WHERE key LIKE 'last_commit_%'").fetchall()
75
+ con.close()
76
+ for k, v in rows:
77
+ last_commits[k.replace('last_commit_', '')] = v
78
+ except Exception:
79
+ pass
80
+
81
+ current_commits = {}
82
+ for proj in os.listdir(CODE_DIR):
83
+ proj_path = os.path.join(CODE_DIR, proj)
84
+ if not os.path.isdir(os.path.join(proj_path, '.git')):
85
+ continue
86
+ try:
87
+ # Get current HEAD (本地操作, 快速)
88
+ r = _git('rev-parse', 'HEAD', cwd=proj_path)
89
+ if r.returncode != 0:
90
+ continue
91
+ current_head = r.stdout.strip()
92
+ current_commits[proj] = current_head
93
+
94
+ # Determine diff range: from last-built commit to HEAD
95
+ last_built = last_commits.get(proj)
96
+ if last_built and last_built != current_head:
97
+ # Diff from last build to now — captures ALL changes since last build
98
+ diff_rev_range = ['diff', '--name-only', last_built, current_head]
99
+ elif not last_built:
100
+ # First run get last 7 days of changes (or all if repo is new)
101
+ r3 = _git('log', '--since=7 days ago', '--oneline', cwd=proj_path)
102
+ log_lines = [l for l in r3.stdout.strip().split('\n') if l.strip()]
103
+ if len(log_lines) >= 2:
104
+ old_commit = log_lines[-1].split()[0]
105
+ diff_rev_range = ['diff', '--name-only', old_commit, current_head]
106
+ else:
107
+ continue # No changes or single commit
108
+ else:
109
+ continue # Already up to date, no diff needed
110
+
111
+ r2 = _git(*diff_rev_range, cwd=proj_path)
112
+ # P2-11: 必须判 returncode。若 diff 失败 (如 last_built commit 被 git gc
113
+ # 掉、对象库损坏), r2.stdout 可能是错误信息, 把它当文件名加入 changed
114
+ # 会让后续 _cleanup_deleted_file 误删真实文件。
115
+ if r2.returncode != 0:
116
+ continue
117
+ for f in r2.stdout.strip().split('\n'):
118
+ f = f.strip()
119
+ if f:
120
+ changed.append(os.path.join(proj_path, f.replace('/', os.sep)))
121
+ except Exception:
122
+ pass
123
+
124
+ # Save current commits as last-built
125
+ try:
126
+ con = get_db()
127
+ for proj, commit in current_commits.items():
128
+ con.execute(
129
+ "INSERT OR REPLACE INTO build_meta VALUES (?, ?)",
130
+ ['last_commit_%s' % proj, commit]
131
+ )
132
+ con.close()
133
+ except Exception:
134
+ pass
135
+
136
+ return changed
137
+
138
+
139
+ def incremental_update(changed_files):
140
+ """Update DuckDB incrementally for a list of changed files.
141
+
142
+ For each file:
143
+ - .vue: re-extract trace_clicks, UPSERT button entities + click edges
144
+ - .ts (api): re-extract api functions, UPSERT endpoint entities
145
+ - .java (test): re-extract test assertions, UPSERT test entities
146
+ """
147
+ con = get_db()
148
+ init_schema(con)
149
+
150
+ stats = {'vue': 0, 'ts': 0, 'java_test': 0, 'edges_updated': 0, 'entities_upserted': 0}
151
+
152
+ # 判断是否有 api 文件变更 — 决定是否需要重建 api_table
153
+ has_api_changes = any(
154
+ f.replace('\\', '/').endswith('.ts') and '/api/' in f.replace('\\', '/')
155
+ for f in changed_files if os.path.isfile(f)
156
+ )
157
+
158
+ api_dir = _find_api_dir()
159
+
160
+ if has_api_changes:
161
+ # api 文件变了 — 必须重建完整 api_table
162
+ api_table = ts_build_api_fn_table(api_dir) if api_dir else {}
163
+ # 缓存到文件, 供下次增量复用
164
+ if api_table and api_dir:
165
+ _save_api_cache(api_table, api_dir)
166
+ else:
167
+ # api 文件没变 — 从缓存加载 (毫秒级, 不扫文件系统)
168
+ api_table = _load_api_cache(api_dir)
169
+ if not api_table:
170
+ # 缓存不存在 (首次运行) — 重建并缓存
171
+ api_table = ts_build_api_fn_table(api_dir) if api_dir else {}
172
+ if api_table and api_dir:
173
+ _save_api_cache(api_table, api_dir)
174
+
175
+ for filepath in changed_files:
176
+ if not os.path.isfile(filepath):
177
+ # File was deleted — clean up its entities
178
+ _cleanup_deleted_file(con, filepath)
179
+ continue
180
+
181
+ rel_path = filepath.replace('\\', '/')
182
+
183
+ if filepath.endswith('.vue'):
184
+ stats['vue'] += 1
185
+ _update_vue_file(con, filepath, api_table, stats)
186
+
187
+ elif filepath.endswith('.ts') and '/api/' in rel_path:
188
+ stats['ts'] += 1
189
+ _update_api_file(con, filepath, stats)
190
+
191
+ elif filepath.endswith('Test.java'):
192
+ stats['java_test'] += 1
193
+ _update_test_file(con, filepath, stats)
194
+
195
+ # After file updates, refresh PRD + Wiki from git_sync's JSON output
196
+ _refresh_prds_and_wiki(con, stats)
197
+
198
+ con.close()
199
+ return stats
200
+
201
+
202
+ def _refresh_prds_and_wiki(con, stats):
203
+ """Import PRD index + Wiki from git_sync's JSON output into DuckDB.
204
+
205
+ git_sync.py writes these as JSON; kg_incremental reads them and UPSERTs
206
+ into DuckDB so MCP tools can query them.
207
+ """
208
+ INDEX = str(DATA_INDEX_DIR)
209
+ import json as _json
210
+
211
+ # PRD
212
+ prd_path = os.path.join(INDEX, '_build_cache', 'prd-index.json')
213
+ if not os.path.isfile(prd_path):
214
+ prd_path = os.path.join(INDEX, 'prd-index.json')
215
+ if os.path.isfile(prd_path):
216
+ try:
217
+ prds = _json.load(open(prd_path, encoding='utf-8'))
218
+ con.execute("DELETE FROM prds")
219
+ rows = [(f, info.get('title', ''),
220
+ _json.dumps(info.get('keywords', []), ensure_ascii=False),
221
+ _json.dumps(info.get('features', []), ensure_ascii=False),
222
+ _json.dumps(info.get('cn_terms', []), ensure_ascii=False))
223
+ for f, info in prds.items()]
224
+ if rows:
225
+ _batch_insert(con, 'prds', rows, ignore=True)
226
+ stats['prds_refreshed'] = len(rows)
227
+ except Exception:
228
+ pass
229
+
230
+ # Wiki
231
+ wiki_path = os.path.join(INDEX, 'wiki-index.json')
232
+ if os.path.isfile(wiki_path):
233
+ try:
234
+ wiki = _json.load(open(wiki_path, encoding='utf-8'))
235
+ con.execute("DELETE FROM wiki")
236
+ wiki_rows = []
237
+ for kw, entries in (wiki.get('index', {}) if isinstance(wiki, dict) else {}).items():
238
+ for e in (entries if isinstance(entries, list) else []):
239
+ wiki_rows.append((kw, e.get('project', ''), e.get('title', ''),
240
+ e.get('md_path', ''), e.get('description', '')))
241
+ if wiki_rows:
242
+ _batch_insert(con, 'wiki', wiki_rows)
243
+ stats['wiki_refreshed'] = len(wiki_rows)
244
+ except Exception:
245
+ pass
246
+
247
+
248
+ def _find_api_dir():
249
+ """Find the fywl-ui api directory."""
250
+ apps = os.path.join(CODE_DIR, 'fywl-ui', 'apps')
251
+ if os.path.isdir(apps):
252
+ for app in os.listdir(apps):
253
+ candidate = os.path.join(apps, app, 'src', 'api')
254
+ if os.path.isdir(candidate):
255
+ return candidate
256
+ return None
257
+
258
+
259
+ def _load_api_table_from_duckdb(con):
260
+ """从 DuckDB 加载 api_table (api 函数名→端点), 避免重建。"""
261
+ table = {}
262
+ try:
263
+ rows = con.execute("SELECT id, props FROM entities WHERE type = 'ENDPOINT'").fetchall()
264
+ for eid, props_json in rows:
265
+ url = eid.replace('E:', '')
266
+ props = json.loads(props_json) if props_json else {}
267
+ fn_name = props.get('api_fn', '')
268
+ if fn_name:
269
+ table[fn_name] = {'url': url, 'verb': props.get('verb', '?'),
270
+ 'file': props.get('source_file', '')}
271
+ except Exception:
272
+ pass
273
+ return table
274
+
275
+
276
+ _API_CACHE_PATH = os.path.join(INDEX_DIR, '.api-table-cache.json')
277
+
278
+
279
+ def _save_api_cache(api_table, api_dir):
280
+ """保存 api_table 到缓存文件 (含 api_dir mtime 作为失效依据)。"""
281
+ import time
282
+ cache = {
283
+ 'api_dir_mtime': os.path.getmtime(api_dir) if os.path.isdir(api_dir) else 0,
284
+ 'saved_at': time.time(),
285
+ 'table': api_table,
286
+ }
287
+ with open(_API_CACHE_PATH, 'w', encoding='utf-8') as f:
288
+ json.dump(cache, f, ensure_ascii=False)
289
+
290
+
291
+ def _load_api_cache(api_dir):
292
+ """从缓存文件加载 api_table。如果缓存不存在或过期返回 {}。"""
293
+ if not os.path.isfile(_API_CACHE_PATH):
294
+ return {}
295
+ try:
296
+ cache = json.load(open(_API_CACHE_PATH, encoding='utf-8'))
297
+ # 检查 api_dir 的 mtime 是否变了
298
+ current_mtime = os.path.getmtime(api_dir) if api_dir and os.path.isdir(api_dir) else 0
299
+ if cache.get('api_dir_mtime') == current_mtime:
300
+ return cache.get('table', {})
301
+ except Exception:
302
+ pass
303
+ return {}
304
+
305
+
306
+ def _update_vue_file(con, filepath, api_table, stats):
307
+ """Re-extract traces from one Vue file and UPSERT its button entities + edges."""
308
+ try:
309
+ content = open(filepath, 'rb').read().decode('utf-8', errors='replace')
310
+ except Exception:
311
+ return
312
+ if '@click' not in content:
313
+ return
314
+
315
+ traces = ts_trace_clicks(filepath, content, api_table)
316
+ vue_norm = filepath.replace('\\', '/')
317
+ # Normalize vue_short — MUST match kg_build.py's format exactly
318
+ if '/views/' in vue_norm:
319
+ vue_short = vue_norm.split('/views/')[-1]
320
+ elif '/pages/' in vue_norm:
321
+ vue_short = vue_norm.split('/pages/')[-1]
322
+ else:
323
+ vue_short = os.path.basename(filepath)
324
+
325
+ for t in traces:
326
+ handler = t.get('handler', '')
327
+ eid = 'B:%s:%s' % (vue_short, handler)
328
+ # UPSERT entity (INSERT OR REPLACE, no duplicates)
329
+ upsert_entity(con, eid, 'BUTTON', handler, '',
330
+ {'button_text': t.get('button', ''),
331
+ 'vue_file': vue_norm,
332
+ 'confidence': t.get('confidence', '')})
333
+ upsert_alias(con, handler.lower(), eid)
334
+ stats['entities_upserted'] += 1
335
+
336
+ # Delete ALL old edges from this button first, then insert fresh
337
+ delete_edges_from(con, eid)
338
+ ep = t.get('endpoint')
339
+ if ep and ep != 'None':
340
+ to_eid = 'E:%s' % ep
341
+ upsert_edge(con, eid, to_eid, 'clicks', props={
342
+ 'button': t.get('button', ''),
343
+ 'confidence': t.get('confidence', ''),
344
+ })
345
+ stats['edges_updated'] += 1
346
+
347
+
348
+ def _update_api_file(con, filepath, stats):
349
+ """Re-extract API functions from one .ts file and UPSERT endpoint entities."""
350
+ # Re-use the bulk api_table extraction on this single file's directory
351
+ api_dir = os.path.dirname(filepath)
352
+ file_table = build_api_fn_table(api_dir)
353
+ for fn_name, info in file_table.items():
354
+ # Only update functions defined in THIS file
355
+ if info.get('file', '').replace('\\', '/') != filepath.replace('\\', '/'):
356
+ continue
357
+ eid = 'E:%s' % info['url']
358
+ upsert_entity(con, eid, 'ENDPOINT', info['url'], '',
359
+ {'verb': info.get('verb', '?'),
360
+ 'api_fn': fn_name,
361
+ 'source_file': filepath.replace('\\', '/')})
362
+ upsert_alias(con, fn_name.lower(), eid)
363
+ stats['entities_upserted'] += 1
364
+
365
+
366
+ def _update_test_file(con, filepath, stats):
367
+ """Re-extract test assertions from one Test.java and UPSERT test entities."""
368
+ tests = extract_test_assertions(os.path.dirname(filepath))
369
+ # Filter to only this file
370
+ file_norm = filepath.replace('\\', '/')
371
+ for t in tests:
372
+ if t.get('file', '').replace('\\', '/') != file_norm:
373
+ continue
374
+ name = '%s.%s' % (t['class'], t['method'])
375
+ eid = 'T:%s' % name
376
+ upsert_entity(con, eid, 'TEST', name, '',
377
+ {'display': t.get('display_name', ''),
378
+ 'deps': t.get('injected_deps', []),
379
+ 'assertions': t.get('assertions', []),
380
+ 'package': t.get('package', '')})
381
+ upsert_alias(con, name.lower(), eid)
382
+ stats['entities_upserted'] += 1
383
+
384
+
385
+ def _cleanup_deleted_file(con, filepath):
386
+ """Remove entities belonging to a deleted file."""
387
+ rel = filepath.replace('\\', '/')
388
+ # Delete buttons from this vue file
389
+ if '.vue' in rel:
390
+ vue_short = rel.split('/views/')[-1] if '/views/' in rel else os.path.basename(rel)
391
+ con.execute("DELETE FROM entities WHERE id LIKE 'B:%s:%%'" % vue_short.replace("'", "''"))
392
+ con.execute("DELETE FROM edges WHERE from_id LIKE 'B:%s:%%'" % vue_short.replace("'", "''"))
393
+
394
+
395
+ if __name__ == '__main__':
396
+ if len(sys.argv) > 1:
397
+ changed = sys.argv[1:]
398
+ else:
399
+ print('Auto-detecting changes via git diff...')
400
+ changed = get_changed_files()
401
+
402
+ if not changed:
403
+ print('No changes detected.')
404
+ sys.exit(0)
405
+
406
+ # Filter to relevant file types
407
+ relevant = [f for f in changed if f.endswith(('.vue', '.ts', '.java'))]
408
+ print('Changed files: %d total, %d relevant' % (len(changed), len(relevant)))
409
+
410
+ if relevant:
411
+ import time
412
+ t0 = time.time()
413
+ stats = incremental_update(relevant)
414
+ elapsed = time.time() - t0
415
+ print('Incremental update done in %.1fs' % elapsed)
416
+ print(' Vue: %d, TS: %d, Java: %d' % (stats['vue'], stats['ts'], stats['java_test']))
417
+ print(' Entities UPSERTed: %d' % stats['entities_upserted'])
418
+ print(' Edges updated: %d' % stats['edges_updated'])
419
+ else:
420
+ print('No relevant files to update.')