@hupan56/wlkj 3.3.14 → 3.3.15

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 (94) hide show
  1. package/bin/cli.js +16 -1
  2. package/package.json +1 -1
  3. package/templates/qoder/commands/optional/wl-spec.md +4 -2
  4. package/templates/qoder/commands/wl-commit.md +3 -1
  5. package/templates/qoder/commands/wl-init.md +2 -2
  6. package/templates/qoder/commands/wl-prd.md +24 -0
  7. package/templates/qoder/commands/wl-search.md +18 -11
  8. package/templates/qoder/commands/wl-task.md +3 -1
  9. package/templates/qoder/config.yaml +0 -6
  10. package/templates/qoder/contracts/insight.md +55 -0
  11. package/templates/qoder/hooks/pre-tool-use-commit.py +124 -0
  12. package/templates/qoder/hooks/session-start.py +20 -1
  13. package/templates/qoder/hooks/stop-eval.py +129 -0
  14. package/templates/qoder/scripts/capability/adapters/mcp.py +9 -1
  15. package/templates/qoder/scripts/capability/registry.py +0 -3
  16. package/templates/qoder/scripts/capability/registry_mcp.py +10 -4
  17. package/templates/qoder/scripts/deployment/setup/init_doctor.py +6 -2
  18. package/templates/qoder/scripts/deployment/setup/install_qoderwork.py +7 -7
  19. package/templates/qoder/scripts/deployment/setup/setup.py +5 -2
  20. package/templates/qoder/scripts/domain/integration/return_to_platform.py +4 -2
  21. package/templates/qoder/scripts/domain/integration/spec_upload.py +4 -4
  22. package/templates/qoder/scripts/domain/kg/build/build_entity_registry.py +12 -12
  23. package/templates/qoder/scripts/domain/kg/build/build_relations.py +12 -12
  24. package/templates/qoder/scripts/domain/kg/graph/kg_semantic.py +41 -5
  25. package/templates/qoder/scripts/domain/kg/kg.py +14 -0
  26. package/templates/qoder/scripts/domain/kg/switch_project.py +2 -2
  27. package/templates/qoder/scripts/domain/task/wlkj_panel.py +169 -14
  28. package/templates/qoder/scripts/foundation/io/context_cache.py +94 -0
  29. package/templates/qoder/scripts/orchestration/wlkj.py +106 -0
  30. package/templates/qoder/scripts/tool_guide.md +70 -0
  31. package/templates/qoder/scripts/validation/eval/alignment_matrix.py +176 -0
  32. package/templates/qoder/scripts/validation/eval/bf2_content_fidelity.py +110 -0
  33. package/templates/qoder/scripts/validation/eval/bf2_llmjudge.py +104 -0
  34. package/templates/qoder/scripts/validation/eval/bf_score.py +218 -0
  35. package/templates/qoder/scripts/validation/eval/code_flywheel.py +150 -0
  36. package/templates/qoder/scripts/validation/eval/dispatcher_ab.py +156 -0
  37. package/templates/qoder/scripts/validation/eval/dispatcher_ab_2026-07-21.json +23 -0
  38. package/templates/qoder/scripts/validation/eval/feature_fidelity_flywheel.py +143 -0
  39. package/templates/qoder/scripts/validation/eval/gradient_matrix.py +261 -0
  40. package/templates/qoder/scripts/validation/eval/gradient_matrix_baseline_2026-07-21.json +33 -0
  41. package/templates/qoder/scripts/validation/eval/metrics_dashboard.py +105 -0
  42. package/templates/qoder/scripts/validation/eval/multi_turn_flywheel.py +118 -0
  43. package/templates/qoder/scripts/validation/eval/prd_fidelity_flywheel.py +128 -0
  44. package/templates/qoder/scripts/validation/eval/prd_flywheel.py +148 -0
  45. package/templates/qoder/scripts/validation/eval/prototype_fidelity_flywheel.py +166 -0
  46. package/templates/qoder/scripts/validation/eval/recall_flywheel.py +148 -0
  47. package/templates/qoder/scripts/validation/eval/robustness_flywheel.py +139 -0
  48. package/templates/qoder/scripts/validation/eval/speed_accuracy_flywheel.py +188 -0
  49. package/templates/qoder/scripts/validation/eval/task_flywheel.py +124 -0
  50. package/templates/qoder/scripts/validation/eval/token_flywheel.py +88 -0
  51. package/templates/qoder/scripts/validation/metrics/eval_code_ac.py +177 -0
  52. package/templates/qoder/scripts/validation/metrics/lint_cases.py +170 -0
  53. package/templates/qoder/scripts/validation/test/test_context_cache.py +78 -0
  54. package/templates/qoder/scripts/validation/test/test_lint_cases.py +60 -0
  55. package/templates/qoder/scripts/validation/test/test_pre_tool_use_commit.py +70 -0
  56. package/templates/qoder/settings.json +8 -0
  57. package/templates/qoder/skills/wl-spec/SKILL.md +1 -1
  58. package/templates/qoder/agents/design-agent.md +0 -20
  59. package/templates/qoder/agents/spec-generator.md +0 -21
  60. package/templates/qoder/scripts/capability/smoke_test_report.json.new +0 -94
  61. package/templates/qoder/scripts/domain/kg/extract/asset/__init__.py +0 -10
  62. package/templates/qoder/scripts/domain/kg/extract/asset/asset_tree.py +0 -57
  63. package/templates/qoder/scripts/domain/kg/extract/asset/discussion_importer.py +0 -62
  64. package/templates/qoder/scripts/domain/kg/extract/asset/prd_importer.py +0 -146
  65. package/templates/qoder/scripts/domain/kg/extract/asset/prototype_importer.py +0 -64
  66. package/templates/qoder/scripts/domain/kg/extract/asset/returns_importer.py +0 -52
  67. package/templates/qoder/scripts/domain/kg/extract/build_goal3.py +0 -104
  68. package/templates/qoder/scripts/domain/kg/extract/build_goal4.py +0 -55
  69. package/templates/qoder/scripts/domain/kg/extract/build_goal5.py +0 -95
  70. package/templates/qoder/scripts/domain/kg/extract/db/__init__.py +0 -8
  71. package/templates/qoder/scripts/domain/kg/extract/db/data_profile.py +0 -22
  72. package/templates/qoder/scripts/domain/kg/extract/db/fk_extractor.py +0 -55
  73. package/templates/qoder/scripts/domain/kg/extract/db/schema_extractor.py +0 -90
  74. package/templates/qoder/scripts/domain/kg/extract/inference/__init__.py +0 -9
  75. package/templates/qoder/scripts/domain/kg/extract/inference/community_summarizer.py +0 -206
  76. package/templates/qoder/scripts/domain/kg/extract/inference/embed_builder.py +0 -132
  77. package/templates/qoder/scripts/domain/kg/extract/inference/naming_matcher.py +0 -80
  78. package/templates/qoder/scripts/domain/kg/extract/inference/promote.py +0 -59
  79. package/templates/qoder/scripts/domain/kg/extract/inference/recompute.py +0 -93
  80. package/templates/qoder/scripts/domain/kg/extract/inference/weak_link.py +0 -421
  81. package/templates/qoder/scripts/domain/kg/extract/mybatis/__init__.py +0 -9
  82. package/templates/qoder/scripts/domain/kg/extract/mybatis/all.py +0 -79
  83. package/templates/qoder/scripts/domain/kg/extract/mybatis/mapper_parser.py +0 -99
  84. package/templates/qoder/scripts/domain/kg/extract/mybatis/relation_builder.py +0 -69
  85. package/templates/qoder/scripts/domain/kg/extract/mybatis/sql_extractor.py +0 -78
  86. package/templates/qoder/scripts/domain/kg/extract/prd/__init__.py +0 -8
  87. package/templates/qoder/scripts/domain/kg/extract/prd/prd_chunk_embed.py +0 -105
  88. package/templates/qoder/scripts/domain/kg/extract/prd/prd_llm_extract.py +0 -153
  89. package/templates/qoder/scripts/domain/kg/extract/prd/req_anchor.py +0 -120
  90. package/templates/qoder/scripts/domain/kg/graph/kg_link_db.py +0 -235
  91. package/templates/qoder/scripts/domain/kg/search/enrich_prompt.py +0 -238
  92. package/templates/qoder/scripts/domain/kg/server/perf_bench.py +0 -197
  93. package/templates/qoder/scripts/domain/kg/sync_repowiki.py +0 -109
  94. package/templates/qoder/scripts/engine/poller.py +0 -219
@@ -1,120 +0,0 @@
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
@@ -1,235 +0,0 @@
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()
@@ -1,238 +0,0 @@
1
- #!/usr/bin/env python3
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
- from foundation.core.paths import get_repo_root
14
-
15
- """enrich_prompt.py - 极速提示词润色 (用户提问后第 0 步)
16
-
17
- 目标: 在用户提问后, <10ms 内判断这句话命中哪些业务关键词,
18
- 输出一个「润色后的提示词」给用户确认, 再走原命令。
19
-
20
- 设计:
21
- - 纯内存查表 (不跑重检索 context_pack, 那个留给确认后)
22
- - 中文分词复用 common/terms.py 的 expand_chinese_query
23
- - 命中 keyword-index / api-index / prd-index 的键
24
- - 命中 0 个 = 非业务消息 (谢谢/继续), 直接放行不润色
25
-
26
- 用法:
27
- python .qoder/scripts/enrich_prompt.py "保险批改那个异常统计页加个审批按钮"
28
- python .qoder/scripts/enrich_prompt.py "保险批改..." --json # 机器可读
29
-
30
- 输出 (默认人类可读):
31
- ── 极速润色 ──
32
- 业务词: 保险→insurance, 批改→amend, 异常→abnormal...
33
- 相关代码符号: 108 处 (abnormalovertime, approval-cell...)
34
- 相关API: 5 个
35
- 历史 PRD: 2 篇
36
- ──────────────
37
- 将以上下文继续。回复"改:xxx"调整, 任意其它内容=接受。
38
- """
39
- import os, sys, json, time
40
-
41
- if sys.platform == 'win32':
42
- try:
43
- sys.stdout.reconfigure(encoding='utf-8')
44
- except Exception:
45
- pass
46
-
47
- BASE = get_repo_root()
48
- sys.path.insert(0, os.path.join(BASE, '.qoder', 'scripts'))
49
-
50
- IDX_DIR = os.path.join(BASE, 'data', 'index')
51
- # 缓存: 同一进程内只加载一次索引
52
- _CACHE = {}
53
-
54
-
55
- def _load(name):
56
- """加载索引 (优先 DuckDB, 回退 JSON)。复用 search_index.load_index 的 fallback 逻辑。"""
57
- if name in _CACHE:
58
- return _CACHE[name]
59
- try:
60
- from domain.kg.search.search_index import load_index
61
- d = load_index(name) or {}
62
- except ImportError:
63
- # search_index 不可用时, 直接读 JSON (老逻辑)
64
- d = {}
65
- path = os.path.join(IDX_DIR, name)
66
- if os.path.isfile(path):
67
- try:
68
- for enc in ('utf-8', 'gbk'):
69
- try:
70
- with open(path, encoding=enc) as f:
71
- d = json.load(f)
72
- break
73
- except (OSError, UnicodeDecodeError):
74
- continue
75
- except Exception:
76
- pass
77
- _CACHE[name] = d
78
- return d
79
-
80
-
81
- # 短而无意义的英文 token 黑名单 (避免子串误命中 'cs'/'fl'/'in' 这类噪声)
82
- # 提到模块级: _match_keys 和 code_total 共用一份
83
- NOISE_KEYS = {
84
- 'app', 'api', 'all', 'add', 'age', 'and', 'are', 'ars', 'ase', 'ask',
85
- 'ate', 'aud', 'bar', 'big', 'btn', 'can', 'car', 'cas', 'cat', 'code',
86
- 'con', 'cor', 'cs', 'cur', 'dae', 'dat', 'day', 'del', 'dep', 'des',
87
- 'det', 'dim', 'div', 'doc', 'dos', 'ear', 'ecs', 'ema', 'end', 'ent',
88
- 'err', 'eva', 'exe', 'exp', 'ext', 'fil', 'fix', 'fl', 'for', 'ge',
89
- 'gen', 'get', 'has', 'her', 'his', 'ics', 'idx', 'imp', 'int', 'inv',
90
- 'io', 'is', 'iso', 'ite', 'its', 'key', 'lab', 'lan', 'lat', 'let',
91
- 'lin', 'lis', 'log', 'ma', 'map', 'max', 'min', 'mod', 'msg', 'new',
92
- 'nil', 'not', 'now', 'num', 'obj', 'off', 'one', 'opt', 'ord', 'out',
93
- 'ove', 'own', 'pag', 'par', 'pas', 'pay', 'pen', 'per', 'pos', 'pre',
94
- 'pro', 'put', 'que', 'rec', 'red', 'ref', 'reg', 'rep', 'res', 'ret',
95
- 'rev', 'row', 'run', 'sa', 'sel', 'set', 'sev', 'sie', 'sit', 'sor',
96
- 'src', 'sta', 'sub', 'sum', 'sys', 'tab', 'tar', 'tes', 'tex', 'tim',
97
- 'tmp', 'tok', 'tom', 'top', 'tot', 'tru', 'try', 'typ', 'udp', 'uni',
98
- 'upd', 'url', 'use', 'usr', 'val', 'ver', 'vid', 'vio', 'vis', 'voi',
99
- 'was', 'way', 'who', 'why', 'win', 'wri', 'xml', 'you', 'your', 'zone',
100
- }
101
-
102
-
103
- def _match_keys(query_lower, keys, max_hits=15, min_len=3):
104
- """在 keys 里找被 query 包含的 (子串匹配), 返回命中列表 + 总数。
105
- min_len: 短于这个长度的键忽略 (避免 'cs'/'fl'/'in' 这类噪声子串)。"""
106
- hits = []
107
- for k in keys:
108
- kl = k.lower()
109
- if len(kl) < min_len:
110
- continue
111
- if kl in NOISE_KEYS:
112
- continue
113
- if kl in query_lower:
114
- hits.append(k)
115
- if len(hits) >= max_hits:
116
- break
117
- return hits
118
- for k in keys:
119
- kl = k.lower()
120
- if len(kl) < min_len:
121
- continue
122
- if kl in NOISE:
123
- continue
124
- if kl in query_lower:
125
- hits.append(k)
126
- if len(hits) >= max_hits:
127
- break
128
- return hits
129
-
130
-
131
- def enrich(query):
132
- """润色主函数。返回 dict:
133
- {
134
- 'is_business': bool, # 是否业务消息 (有命中才算)
135
- 'cn_words': [...], # 中文分词出的英文搜索词
136
- 'code_hits': [...], # 命中的代码符号 (样例)
137
- 'code_total': int, # 代码命中总数
138
- 'api_hits': [...], # 命中的 API
139
- 'prd_hits': [...], # 命中的历史 PRD
140
- 'elapsed_ms': float, # 耗时
141
- 'polished': str, # 润色后的提示词 (人类可读)
142
- }
143
- """
144
- t0 = time.perf_counter()
145
- from foundation.integrations.terms import expand_chinese_query
146
-
147
- # 1. 中文分词 → 英文搜索词
148
- cn_words = list(dict.fromkeys(expand_chinese_query(query))) # 去重保序
149
-
150
- # 2. 构造匹配串: 原句 + 扩展出的英文词, 一起拿去命中索引
151
- match_str = query.lower()
152
- for w in cn_words:
153
- match_str += ' ' + w.lower()
154
-
155
- # 3. 命中三类索引
156
- code_idx = _load('code-keyword.json')
157
- code_keys = list(code_idx.keys()) if isinstance(code_idx, dict) else []
158
- code_hits = _match_keys(match_str, code_keys)
159
-
160
- api_idx = _load('code-api.json')
161
- api_keys = []
162
- if isinstance(api_idx, dict):
163
- api_keys = list(api_idx.keys())
164
- api_hits = _match_keys(match_str, api_keys)
165
-
166
- prd_idx = _load('prd-index.json')
167
- prd_keys = []
168
- if isinstance(prd_idx, dict):
169
- prd_keys = list(prd_idx.keys())
170
- prd_hits = _match_keys(match_str, prd_keys, max_hits=5)
171
-
172
- # 4. 代码命中总数 (不限 max, 用于展示规模; 用与 _match_keys 一致的过滤)
173
- code_total = 0
174
- if code_keys:
175
- code_total = sum(1 for k in code_keys
176
- if len(k) >= 3 and k.lower() not in NOISE_KEYS
177
- and k.lower() in match_str)
178
-
179
- elapsed_ms = (time.perf_counter() - t0) * 1000
180
- is_business = bool(code_hits or api_hits or prd_hits or cn_words)
181
-
182
- # 5. 组装润色后提示词 (人类可读)
183
- lines = []
184
- if not is_business:
185
- lines.append('(非业务消息,无需润色,直接放行)')
186
- else:
187
- if cn_words:
188
- shown = ', '.join(cn_words[:8])
189
- lines.append('业务词: ' + shown)
190
- if code_total:
191
- sample = ', '.join(code_hits[:8])
192
- more = '' if code_total <= len(code_hits) else ' ...'
193
- lines.append('相关代码符号: %d 处 (%s%s)' % (code_total, sample, more))
194
- if api_hits:
195
- lines.append('相关 API: %d 个 (%s)' % (len(api_hits), ', '.join(api_hits[:5])))
196
- if prd_hits:
197
- lines.append('历史 PRD: %s' % ', '.join(prd_hits[:5]))
198
-
199
- polished = '\n'.join(lines)
200
-
201
- return {
202
- 'is_business': is_business,
203
- 'cn_words': cn_words,
204
- 'code_hits': code_hits,
205
- 'code_total': code_total,
206
- 'api_hits': api_hits,
207
- 'prd_hits': prd_hits,
208
- 'elapsed_ms': round(elapsed_ms, 2),
209
- 'polished': polished,
210
- }
211
-
212
-
213
- def main():
214
- args = sys.argv[1:]
215
- if not args:
216
- print(__doc__)
217
- print('错误: 缺少查询参数。用法: enrich_prompt.py "你的问题"')
218
- sys.exit(2)
219
- query = ' '.join(args[:1]) if args[0] in ('--json',) else ' '.join(args)
220
- # 处理 --json 标志
221
- as_json = '--json' in args
222
- query = query.replace('--json', '').strip()
223
-
224
- result = enrich(query)
225
- if as_json:
226
- print(json.dumps(result, ensure_ascii=False))
227
- return
228
-
229
- # 人类可读输出
230
- print('── 极速润色 (%.1fms) ──' % result['elapsed_ms'])
231
- print(result['polished'])
232
- if result['is_business']:
233
- print('──────────────')
234
- print('将以上下文继续。回复"改:xxx"调整,任意其它内容=接受。')
235
-
236
-
237
- if __name__ == '__main__':
238
- main()