@chongyan/autospec 1.0.1

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 (243) hide show
  1. package/LICENSE +21 -0
  2. package/README.en.md +472 -0
  3. package/README.md +476 -0
  4. package/bin/autospec.js +3 -0
  5. package/knowledge/README.md +144 -0
  6. package/knowledge/checklists/code.md +182 -0
  7. package/knowledge/checklists/design.md +196 -0
  8. package/knowledge/checklists/release.md +70 -0
  9. package/knowledge/checklists/requirement.md +169 -0
  10. package/knowledge/checklists/test.md +46 -0
  11. package/knowledge/config/README.en.md +44 -0
  12. package/knowledge/config/README.md +44 -0
  13. package/knowledge/config/role-composition.yaml +98 -0
  14. package/knowledge/config/role-extensions.yaml +140 -0
  15. package/knowledge/config/skill-compositions.yaml +142 -0
  16. package/knowledge/config/team-stage.yaml +95 -0
  17. package/knowledge/config/team-tasks.yaml +139 -0
  18. package/knowledge/config/team-triggers.yaml +198 -0
  19. package/knowledge/config/validation-patterns.yaml +137 -0
  20. package/knowledge/domain/README.md +115 -0
  21. package/knowledge/domain/flows/README.md +194 -0
  22. package/knowledge/domain/glossary.md +143 -0
  23. package/knowledge/domain/rules.md +138 -0
  24. package/knowledge/environment/README.en.md +36 -0
  25. package/knowledge/environment/README.md +87 -0
  26. package/knowledge/environment/component-knowledge.md +316 -0
  27. package/knowledge/environment/detection-patterns.yaml +502 -0
  28. package/knowledge/environment/middleware-knowledge.md +237 -0
  29. package/knowledge/environment/template-registry.md +321 -0
  30. package/knowledge/guides/domain-driven-design.md +345 -0
  31. package/knowledge/guides/knowledge-management.md +369 -0
  32. package/knowledge/guides/requirement-engineering.md +329 -0
  33. package/knowledge/guides/stages/ai-effect-evaluator.md +93 -0
  34. package/knowledge/guides/stages/code-implementer.md +205 -0
  35. package/knowledge/guides/stages/code-reviewer.md +111 -0
  36. package/knowledge/guides/stages/consistency-checker.md +177 -0
  37. package/knowledge/guides/stages/design-planner.md +401 -0
  38. package/knowledge/guides/stages/design-reviewer.md +83 -0
  39. package/knowledge/guides/stages/integration-test-runner.md +105 -0
  40. package/knowledge/guides/stages/release-checker.md +205 -0
  41. package/knowledge/guides/stages/requirement-analyzer.md +195 -0
  42. package/knowledge/guides/stages/requirement-reviewer.md +83 -0
  43. package/knowledge/guides/stages/security-reviewer.md +89 -0
  44. package/knowledge/guides/stages/test-context-analyzer.md +250 -0
  45. package/knowledge/guides/stages/test-generator.md +241 -0
  46. package/knowledge/guides/stages/test-planner.md +183 -0
  47. package/knowledge/guides/stages/test-reviewer.md +76 -0
  48. package/knowledge/guides/stages/unit-test-runner.md +83 -0
  49. package/knowledge/guides/support/ai-agent-analyzer.md +362 -0
  50. package/knowledge/guides/support/ai-anomaly-analyzer.md +213 -0
  51. package/knowledge/guides/support/ai-artifact-evaluator.md +192 -0
  52. package/knowledge/guides/support/ai-capability-analyzer.md +193 -0
  53. package/knowledge/guides/support/ai-component-analyzer.md +169 -0
  54. package/knowledge/guides/support/ai-data-validator.md +276 -0
  55. package/knowledge/guides/support/ai-evaluation-planner.md +374 -0
  56. package/knowledge/guides/support/ai-path-evaluator.md +274 -0
  57. package/knowledge/guides/support/ai-pipeline-evaluator.md +219 -0
  58. package/knowledge/guides/support/ai-rag-analyzer.md +339 -0
  59. package/knowledge/guides/support/ai-task-assessor.md +418 -0
  60. package/knowledge/guides/support/ai-test-diagnostics.md +133 -0
  61. package/knowledge/guides/support/complexity-assessor.md +268 -0
  62. package/knowledge/guides/support/component-discovery.md +183 -0
  63. package/knowledge/guides/support/environment-scanner.md +207 -0
  64. package/knowledge/guides/support/environment-validator.md +207 -0
  65. package/knowledge/guides/support/knowledge-generator.md +234 -0
  66. package/knowledge/guides/support/methodology-extractor.md +55 -0
  67. package/knowledge/guides/support/pipeline-protocol.md +438 -0
  68. package/knowledge/guides/support/practice-logger.md +359 -0
  69. package/knowledge/guides/support/scope-inference.md +174 -0
  70. package/knowledge/guides/support/skill-distiller.md +91 -0
  71. package/knowledge/guides/support/skill-updater.md +45 -0
  72. package/knowledge/guides/support/skill-validator.md +72 -0
  73. package/knowledge/guides/support/team-orchestrator.md +323 -0
  74. package/knowledge/guides/support/tech-stack-analyzer.md +139 -0
  75. package/knowledge/guides/support/test-runner.md +254 -0
  76. package/knowledge/guides/system-design.md +352 -0
  77. package/knowledge/organization/ai-native-team.md +318 -0
  78. package/knowledge/organization/team-metrics.md +228 -0
  79. package/knowledge/principles/constitution.md +134 -0
  80. package/knowledge/principles/core-principles.md +368 -0
  81. package/knowledge/principles/design-philosophy.md +877 -0
  82. package/knowledge/principles/evolution.md +553 -0
  83. package/knowledge/process/01-requirement.md +113 -0
  84. package/knowledge/process/02-design.md +123 -0
  85. package/knowledge/process/03-implementation.md +90 -0
  86. package/knowledge/process/04-review.md +80 -0
  87. package/knowledge/process/05-testing.md +90 -0
  88. package/knowledge/process/06-delivery.md +88 -0
  89. package/knowledge/process/README.en.md +38 -0
  90. package/knowledge/process/README.md +48 -0
  91. package/knowledge/process/ai-sdlc.md +475 -0
  92. package/knowledge/process/overview.md +319 -0
  93. package/knowledge/standards/code-review.md +876 -0
  94. package/knowledge/standards/coding-style.md +940 -0
  95. package/knowledge/standards/data-consistency.md +1085 -0
  96. package/knowledge/standards/document-versioning.md +210 -0
  97. package/knowledge/standards/risk-detection.md +186 -0
  98. package/knowledge/templates/ai-evaluation.md +150 -0
  99. package/knowledge/templates/api-design.md +117 -0
  100. package/knowledge/templates/database-design.md +132 -0
  101. package/knowledge/templates/domain-driven-design.md +321 -0
  102. package/knowledge/templates/product-proposal.md +201 -0
  103. package/knowledge/templates/system-design.md +227 -0
  104. package/knowledge/templates/task-breakdown.md +107 -0
  105. package/knowledge/templates/test-case.md +170 -0
  106. package/package.json +53 -0
  107. package/plugins/.claude-plugin/plugin.json +134 -0
  108. package/plugins/agents/roles/ai-engineer.md +129 -0
  109. package/plugins/agents/roles/backend-engineer.md +165 -0
  110. package/plugins/agents/roles/ceo.md +94 -0
  111. package/plugins/agents/roles/data-engineer.md +135 -0
  112. package/plugins/agents/roles/devops-engineer.md +181 -0
  113. package/plugins/agents/roles/frontend-engineer.md +129 -0
  114. package/plugins/agents/roles/product-owner.md +98 -0
  115. package/plugins/agents/roles/quality-engineer.md +129 -0
  116. package/plugins/agents/roles/security-engineer.md +180 -0
  117. package/plugins/agents/roles/tech-lead.md +97 -0
  118. package/plugins/agents/support/blind-comparator.md +88 -0
  119. package/plugins/agents/support/consistency-checker.md +103 -0
  120. package/plugins/agents/support/failure-diagnostician.md +141 -0
  121. package/plugins/agents/support/independent-reviewer.md +80 -0
  122. package/plugins/agents/support/safety-auditor.md +121 -0
  123. package/plugins/agents/support/skill-benchmarker.md +86 -0
  124. package/plugins/agents/support/skill-forger.md +105 -0
  125. package/plugins/agents/support/stage-gate-evaluator.md +121 -0
  126. package/plugins/agents/support/test-coverage-reviewer.md +73 -0
  127. package/plugins/benchmarks/templates/README.md +44 -0
  128. package/plugins/benchmarks/templates/commands/explore-template.yaml +48 -0
  129. package/plugins/benchmarks/templates/pipeline/agile-template.yaml +84 -0
  130. package/plugins/benchmarks/templates/pipeline/waterfall-template.yaml +106 -0
  131. package/plugins/benchmarks/templates/skills/requirement-analyzer-template.yaml +48 -0
  132. package/plugins/commands/README.en.md +96 -0
  133. package/plugins/commands/README.md +96 -0
  134. package/plugins/commands/apply.md +191 -0
  135. package/plugins/commands/archive.md +76 -0
  136. package/plugins/commands/env-export.md +79 -0
  137. package/plugins/commands/env-sync.md +640 -0
  138. package/plugins/commands/env-template.md +223 -0
  139. package/plugins/commands/env-update.md +264 -0
  140. package/plugins/commands/env-validate.md +176 -0
  141. package/plugins/commands/env.md +79 -0
  142. package/plugins/commands/explore.md +76 -0
  143. package/plugins/commands/field-evolve.md +536 -0
  144. package/plugins/commands/memory.md +249 -0
  145. package/plugins/commands/project-evolve.md +821 -0
  146. package/plugins/commands/propose.md +93 -0
  147. package/plugins/commands/review.md +140 -0
  148. package/plugins/commands/run.md +224 -0
  149. package/plugins/commands/status.md +62 -0
  150. package/plugins/commands/validate.md +108 -0
  151. package/plugins/hooks/README.en.md +56 -0
  152. package/plugins/hooks/README.md +56 -0
  153. package/plugins/hooks/ai-project-guard.js +329 -0
  154. package/plugins/hooks/artifact-evaluation-hook.js +237 -0
  155. package/plugins/hooks/constitution-guard.js +211 -0
  156. package/plugins/hooks/environment-autocommit.js +264 -0
  157. package/plugins/hooks/environment-manager.js +778 -0
  158. package/plugins/hooks/execution-tracker.js +354 -0
  159. package/plugins/hooks/frozen-zone-guard.js +140 -0
  160. package/plugins/hooks/layer1-validator.js +423 -0
  161. package/plugins/hooks/lib/artifact-evaluator.js +414 -0
  162. package/plugins/hooks/lib/benchmarks/change-detector.js +390 -0
  163. package/plugins/hooks/lib/benchmarks/evaluator.js +605 -0
  164. package/plugins/hooks/lib/benchmarks/integration-example.js +169 -0
  165. package/plugins/hooks/lib/data-and-ai-detector.js +275 -0
  166. package/plugins/hooks/lib/detection-pattern-loader.js +865 -0
  167. package/plugins/hooks/lib/directory-discovery.js +395 -0
  168. package/plugins/hooks/lib/environment-config-loader.js +341 -0
  169. package/plugins/hooks/lib/environment-detector.js +553 -0
  170. package/plugins/hooks/lib/environment-evolver.js +564 -0
  171. package/plugins/hooks/lib/environment-registry.js +813 -0
  172. package/plugins/hooks/lib/execution-path.js +427 -0
  173. package/plugins/hooks/lib/hook-error-recorder.js +245 -0
  174. package/plugins/hooks/lib/hook-logger.js +538 -0
  175. package/plugins/hooks/lib/hook-runner.js +97 -0
  176. package/plugins/hooks/lib/hook-runner.sh +44 -0
  177. package/plugins/hooks/lib/hook-state-manager.js +480 -0
  178. package/plugins/hooks/lib/memory-extractor.js +377 -0
  179. package/plugins/hooks/lib/memory-manager.js +673 -0
  180. package/plugins/hooks/lib/metrics-analyzer.js +489 -0
  181. package/plugins/hooks/lib/project-evolution/auto-fixer.js +511 -0
  182. package/plugins/hooks/lib/project-evolution/memory-manager.js +346 -0
  183. package/plugins/hooks/lib/project-evolution/pattern-detector.js +476 -0
  184. package/plugins/hooks/lib/project-evolution/semantic-indexer.js +480 -0
  185. package/plugins/hooks/lib/project-structure-detector.js +326 -0
  186. package/plugins/hooks/lib/rollback-tracker.js +346 -0
  187. package/plugins/hooks/lib/source-code-scanner.js +596 -0
  188. package/plugins/hooks/lib/technology-stack-detector.js +374 -0
  189. package/plugins/hooks/lib/test-failure-analyzer.js +375 -0
  190. package/plugins/hooks/lib/test-failure-fixer.js +268 -0
  191. package/plugins/hooks/lib/trace-context.js +277 -0
  192. package/plugins/hooks/lib/validation-patterns.js +415 -0
  193. package/plugins/hooks/memory-sync.js +171 -0
  194. package/plugins/hooks/pipeline-observer.js +413 -0
  195. package/plugins/hooks/scope-sentinel.js +204 -0
  196. package/plugins/hooks/trace-initialization.js +169 -0
  197. package/plugins/memory/templates/code-quality.yaml +149 -0
  198. package/plugins/memory/templates/multi-system.yaml +155 -0
  199. package/plugins/memory/templates/team-habits.yaml +119 -0
  200. package/plugins/memory/templates/testing.yaml +121 -0
  201. package/plugins/skills/README.en.md +47 -0
  202. package/plugins/skills/README.md +104 -0
  203. package/plugins/skills/benchmark-executor/README.md +93 -0
  204. package/plugins/skills/benchmark-executor/SKILL.md +647 -0
  205. package/plugins/skills/benchmark-generator/SKILL.md +349 -0
  206. package/plugins/skills/delivery-stage/SKILL.md +203 -0
  207. package/plugins/skills/design-stage/SKILL.md +216 -0
  208. package/plugins/skills/evolution-process/SKILL.md +291 -0
  209. package/plugins/skills/exploration-phase/SKILL.md +133 -0
  210. package/plugins/skills/implementation-stage/SKILL.md +179 -0
  211. package/plugins/skills/layer1-validation/SKILL.md +79 -0
  212. package/plugins/skills/pending-dashboard/SKILL.md +109 -0
  213. package/plugins/skills/project-evolution/SKILL.md +847 -0
  214. package/plugins/skills/requirement-stage/SKILL.md +183 -0
  215. package/plugins/skills/skill-forge/SKILL.md +223 -0
  216. package/plugins/skills/skill-forge/references/description-guide.md +92 -0
  217. package/plugins/skills/skill-forge/references/quality-rubric.md +104 -0
  218. package/plugins/skills/skill-forge/references/skill-template.md +106 -0
  219. package/plugins/skills/startup-guard/SKILL.md +38 -0
  220. package/plugins/skills/testing-stage/SKILL.md +195 -0
  221. package/scripts/cli/global-init.js +288 -0
  222. package/scripts/cli/global.js +324 -0
  223. package/scripts/cli/index.js +55 -0
  224. package/scripts/cli/init.js +382 -0
  225. package/scripts/cli/list.js +69 -0
  226. package/scripts/cli/org.js +340 -0
  227. package/scripts/cli/update.js +44 -0
  228. package/scripts/config/commands.config.js +145 -0
  229. package/scripts/config/hooks.config.js +197 -0
  230. package/scripts/evolution/evolution-router.js +273 -0
  231. package/scripts/evolution/evolution-signal-collector.js +307 -0
  232. package/scripts/evolution/knowledge-loader.js +346 -0
  233. package/scripts/evolution/marketplace.js +317 -0
  234. package/scripts/evolution/version-manager.js +371 -0
  235. package/scripts/install/agents.js +106 -0
  236. package/scripts/install/commands.js +133 -0
  237. package/scripts/install/constants.js +424 -0
  238. package/scripts/install/hook-logger.js +536 -0
  239. package/scripts/install/hooks.js +110 -0
  240. package/scripts/install/index.js +39 -0
  241. package/scripts/install/skills.js +95 -0
  242. package/scripts/postinstall.js +25 -0
  243. package/scripts/state.js +376 -0
@@ -0,0 +1,813 @@
1
+ /**
2
+ * Environment Knowledge Registry
3
+ *
4
+ * 管理环境知识注册表,合并了原 EnvironmentLoader 的功能
5
+ * 提供知识注册、加载、缓存等完整能力
6
+ */
7
+
8
+ import fs from 'fs';
9
+ import path from 'path';
10
+ import yaml from 'js-yaml';
11
+ import { ENV_DIRS, ENV_FILES, DEFAULT_ENV_REGISTRY, KNOWLEDGE_TYPES } from '../../constants.js';
12
+
13
+ /**
14
+ * 知识注册表管理器
15
+ *
16
+ * 整合功能:
17
+ * - 注册表管理(register, unregister, activate)
18
+ * - 知识加载(loadMiddlewareKnowledge, loadComponentKnowledge)
19
+ * - 缓存机制
20
+ */
21
+ export class EnvironmentRegistry {
22
+ constructor(autospecDir) {
23
+ this.autospecDir = autospecDir;
24
+ this.envDir = path.join(autospecDir, ENV_DIRS.ROOT);
25
+ this.registryPath = path.join(this.envDir, ENV_FILES.REGISTRY);
26
+ this.registry = null;
27
+ this.cache = new Map(); // 知识缓存
28
+ }
29
+
30
+ // ============================================================
31
+ // 初始化与基础操作
32
+ // ============================================================
33
+
34
+ /**
35
+ * 初始化(兼容 EnvironmentLoader)
36
+ */
37
+ async initialize() {
38
+ this.load();
39
+ return this;
40
+ }
41
+
42
+ /**
43
+ * 检查环境衔接层是否存在
44
+ */
45
+ exists() {
46
+ return fs.existsSync(this.envDir);
47
+ }
48
+
49
+ /**
50
+ * 加载注册表
51
+ */
52
+ load() {
53
+ if (!fs.existsSync(this.registryPath)) {
54
+ this.registry = { ...DEFAULT_ENV_REGISTRY };
55
+ return this.registry;
56
+ }
57
+
58
+ try {
59
+ this.registry = JSON.parse(fs.readFileSync(this.registryPath, 'utf-8'));
60
+ return this.registry;
61
+ } catch (e) {
62
+ console.warn('Failed to load registry:', e.message);
63
+ this.registry = { ...DEFAULT_ENV_REGISTRY };
64
+ return this.registry;
65
+ }
66
+ }
67
+
68
+ /**
69
+ * 获取默认注册表(兼容 EnvironmentLoader)
70
+ */
71
+ getDefaultRegistry() {
72
+ return { ...DEFAULT_ENV_REGISTRY };
73
+ }
74
+
75
+ /**
76
+ * 保存注册表
77
+ */
78
+ save() {
79
+ if (!fs.existsSync(this.envDir)) {
80
+ fs.mkdirSync(this.envDir, { recursive: true });
81
+ }
82
+
83
+ this.registry.lastUpdated = new Date().toISOString().split('T')[0];
84
+ fs.writeFileSync(this.registryPath, JSON.stringify(this.registry, null, 2));
85
+ }
86
+
87
+ /**
88
+ * 获取注册表
89
+ */
90
+ get() {
91
+ if (!this.registry) {
92
+ this.load();
93
+ }
94
+ return this.registry;
95
+ }
96
+
97
+ /**
98
+ * 清除缓存
99
+ */
100
+ clearCache() {
101
+ this.cache.clear();
102
+ }
103
+
104
+ // ============================================================
105
+ // 知识加载(原 EnvironmentLoader 功能)
106
+ // ============================================================
107
+
108
+ /**
109
+ * 加载中间件知识
110
+ * @param {string} name - 中间件名称
111
+ * @returns {Object|null} 中间件知识对象
112
+ */
113
+ loadMiddlewareKnowledge(name) {
114
+ const cacheKey = `middleware:${name}`;
115
+
116
+ if (this.cache.has(cacheKey)) {
117
+ return this.cache.get(cacheKey);
118
+ }
119
+
120
+ const entry = this.find('middleware', name);
121
+ if (!entry) {
122
+ return null;
123
+ }
124
+
125
+ const knowledgePath = path.join(this.envDir, entry.path);
126
+ if (!fs.existsSync(knowledgePath)) {
127
+ return null;
128
+ }
129
+
130
+ try {
131
+ const knowledge = yaml.load(fs.readFileSync(knowledgePath, 'utf-8'));
132
+ this.cache.set(cacheKey, knowledge);
133
+ return knowledge;
134
+ } catch (e) {
135
+ console.warn(`Failed to load middleware knowledge ${name}:`, e.message);
136
+ return null;
137
+ }
138
+ }
139
+
140
+ /**
141
+ * 加载组件知识
142
+ * @param {string} name - 组件名称
143
+ * @returns {Object|null} 组件知识对象
144
+ */
145
+ loadComponentKnowledge(name) {
146
+ const cacheKey = `component:${name}`;
147
+
148
+ if (this.cache.has(cacheKey)) {
149
+ return this.cache.get(cacheKey);
150
+ }
151
+
152
+ const entry = this.find('components', name);
153
+ if (!entry) {
154
+ return null;
155
+ }
156
+
157
+ const knowledgePath = path.join(this.envDir, entry.path);
158
+ if (!fs.existsSync(knowledgePath)) {
159
+ return null;
160
+ }
161
+
162
+ try {
163
+ const knowledge = yaml.load(fs.readFileSync(knowledgePath, 'utf-8'));
164
+ this.cache.set(cacheKey, knowledge);
165
+ return knowledge;
166
+ } catch (e) {
167
+ console.warn(`Failed to load component knowledge ${name}:`, e.message);
168
+ return null;
169
+ }
170
+ }
171
+
172
+ /**
173
+ * 加载集成知识
174
+ * @param {string} type - 集成类型 (ci_cd, testing, deployment)
175
+ * @param {string} name - 集成名称
176
+ * @returns {Object|null} 集成知识对象
177
+ */
178
+ loadIntegrationKnowledge(type, name) {
179
+ const cacheKey = `integration:${type}:${name}`;
180
+
181
+ if (this.cache.has(cacheKey)) {
182
+ return this.cache.get(cacheKey);
183
+ }
184
+
185
+ const typeMap = {
186
+ ci_cd: 'ci_cd',
187
+ testing: 'testing',
188
+ deployment: 'deployment',
189
+ };
190
+
191
+ const list = this.registry.integrationKnowledge?.[typeMap[type]] || [];
192
+ const entry = list.find((i) => i.name === name);
193
+
194
+ if (!entry) {
195
+ return null;
196
+ }
197
+
198
+ const knowledgePath = path.join(this.envDir, entry.path);
199
+ if (!fs.existsSync(knowledgePath)) {
200
+ return null;
201
+ }
202
+
203
+ try {
204
+ const knowledge = yaml.load(fs.readFileSync(knowledgePath, 'utf-8'));
205
+ this.cache.set(cacheKey, knowledge);
206
+ return knowledge;
207
+ } catch (e) {
208
+ console.warn(`Failed to load integration knowledge ${type}/${name}:`, e.message);
209
+ return null;
210
+ }
211
+ }
212
+
213
+ /**
214
+ * 获取激活的环境知识
215
+ * @returns {Object} 包含所有激活知识的对象
216
+ */
217
+ getActiveEnvironmentKnowledge() {
218
+ if (!this.registry) {
219
+ this.load();
220
+ }
221
+
222
+ const activeEnv = this.registry.activeProfiles?.environment || 'development';
223
+ const activeComponents = this.registry.activeProfiles?.components || [];
224
+ const activeCiCd = this.registry.activeProfiles?.ci_cd;
225
+
226
+ return {
227
+ environment: activeEnv,
228
+ middleware: this.list('middleware')
229
+ .map((m) => this.loadMiddlewareKnowledge(m.name))
230
+ .filter(Boolean),
231
+ components: activeComponents
232
+ .map((c) => this.loadComponentKnowledge(c))
233
+ .filter(Boolean),
234
+ ci_cd: activeCiCd ? this.loadIntegrationKnowledge('ci_cd', activeCiCd) : null,
235
+ };
236
+ }
237
+
238
+ /**
239
+ * 获取注入到 skill 的上下文
240
+ * @returns {Object} 可直接注入到 skill 执行上下文的知识
241
+ */
242
+ getInjectionContext() {
243
+ const knowledge = this.getActiveEnvironmentKnowledge();
244
+
245
+ return {
246
+ // 当前环境
247
+ activeEnvironment: knowledge.environment,
248
+
249
+ // 中间件连接信息模板
250
+ middlewareTemplates: knowledge.middleware.map((m) => ({
251
+ name: m.metadata?.name || m.meta?.name,
252
+ type: m.spec?.type,
253
+ connectionTemplate: m.spec?.connection?.template,
254
+ requiredEnvVars: m.spec?.connection?.requiredEnvVars || [],
255
+ optionalEnvVars: m.spec?.connection?.optionalEnvVars || [],
256
+ bestPractices: m.spec?.bestPractices || [],
257
+ antiPatterns: m.spec?.antiPatterns || [],
258
+ })),
259
+
260
+ // 组件使用信息
261
+ componentUsage: knowledge.components.map((c) => ({
262
+ name: c.metadata?.name || c.meta?.name,
263
+ type: c.spec?.type,
264
+ description: c.spec?.description,
265
+ usage: c.spec?.usage || {},
266
+ configuration: c.spec?.configuration || {},
267
+ checklist: c.spec?.checklist || {},
268
+ interfaces: c.spec?.interfaces || [],
269
+ })),
270
+
271
+ // CI/CD 集成点
272
+ integrationHooks: knowledge.ci_cd?.spec?.autospecIntegration || null,
273
+
274
+ // 部署门禁
275
+ deploymentGates: knowledge.ci_cd?.spec?.deploymentGates || [],
276
+ };
277
+ }
278
+
279
+ // ============================================================
280
+ // 注册表管理
281
+ // ============================================================
282
+
283
+ /**
284
+ * 注册知识条目
285
+ * @param {string} category - 知识类别 (middleware, components, ci_cd, etc.)
286
+ * @param {Object} entry - 条目信息
287
+ */
288
+ register(category, entry) {
289
+ if (!this.registry) {
290
+ this.load();
291
+ }
292
+
293
+ const categoryMap = {
294
+ middleware: 'environmentKnowledge.middleware',
295
+ infrastructure: 'environmentKnowledge.infrastructure',
296
+ components: 'componentKnowledge.components',
297
+ bestPractices: 'componentKnowledge.bestPractices',
298
+ domainModels: 'componentKnowledge.domainModels',
299
+ ci_cd: 'integrationKnowledge.ci_cd',
300
+ testing: 'integrationKnowledge.testing',
301
+ deployment: 'integrationKnowledge.deployment',
302
+ };
303
+
304
+ const path = categoryMap[category];
305
+ if (!path) {
306
+ throw new Error(`Unknown category: ${category}`);
307
+ }
308
+
309
+ const list = this.getNestedProperty(this.registry, path);
310
+ if (!list) {
311
+ this.setNestedProperty(this.registry, path, []);
312
+ }
313
+
314
+ const targetList = this.getNestedProperty(this.registry, path);
315
+ const existingIndex = targetList.findIndex((item) => item.name === entry.name);
316
+
317
+ if (existingIndex >= 0) {
318
+ // 更新现有条目
319
+ targetList[existingIndex] = { ...targetList[existingIndex], ...entry };
320
+ } else {
321
+ // 添加新条目
322
+ targetList.push(entry);
323
+ }
324
+
325
+ this.save();
326
+ }
327
+
328
+ /**
329
+ * 注销知识条目
330
+ * @param {string} category - 知识类别
331
+ * @param {string} name - 条目名称
332
+ */
333
+ unregister(category, name) {
334
+ if (!this.registry) {
335
+ this.load();
336
+ }
337
+
338
+ const categoryMap = {
339
+ middleware: 'environmentKnowledge.middleware',
340
+ infrastructure: 'environmentKnowledge.infrastructure',
341
+ components: 'componentKnowledge.components',
342
+ bestPractices: 'componentKnowledge.bestPractices',
343
+ domainModels: 'componentKnowledge.domainModels',
344
+ ci_cd: 'integrationKnowledge.ci_cd',
345
+ testing: 'integrationKnowledge.testing',
346
+ deployment: 'integrationKnowledge.deployment',
347
+ };
348
+
349
+ const path = categoryMap[category];
350
+ if (!path) {
351
+ throw new Error(`Unknown category: ${category}`);
352
+ }
353
+
354
+ const list = this.getNestedProperty(this.registry, path);
355
+ if (list) {
356
+ const index = list.findIndex((item) => item.name === name);
357
+ if (index >= 0) {
358
+ list.splice(index, 1);
359
+ this.save();
360
+ }
361
+ }
362
+ }
363
+
364
+ /**
365
+ * 查找知识条目
366
+ * @param {string} category - 知识类别
367
+ * @param {string} name - 条目名称
368
+ */
369
+ find(category, name) {
370
+ if (!this.registry) {
371
+ this.load();
372
+ }
373
+
374
+ const categoryMap = {
375
+ middleware: 'environmentKnowledge.middleware',
376
+ infrastructure: 'environmentKnowledge.infrastructure',
377
+ components: 'componentKnowledge.components',
378
+ bestPractices: 'componentKnowledge.bestPractices',
379
+ domainModels: 'componentKnowledge.domainModels',
380
+ ci_cd: 'integrationKnowledge.ci_cd',
381
+ testing: 'integrationKnowledge.testing',
382
+ deployment: 'integrationKnowledge.deployment',
383
+ };
384
+
385
+ const path = categoryMap[category];
386
+ if (!path) {
387
+ return null;
388
+ }
389
+
390
+ const list = this.getNestedProperty(this.registry, path);
391
+ return list ? list.find((item) => item.name === name) : null;
392
+ }
393
+
394
+ /**
395
+ * 列出某类别所有条目
396
+ * @param {string} category - 知识类别
397
+ */
398
+ list(category) {
399
+ if (!this.registry) {
400
+ this.load();
401
+ }
402
+
403
+ const categoryMap = {
404
+ middleware: 'environmentKnowledge.middleware',
405
+ infrastructure: 'environmentKnowledge.infrastructure',
406
+ components: 'componentKnowledge.components',
407
+ bestPractices: 'componentKnowledge.bestPractices',
408
+ domainModels: 'componentKnowledge.domainModels',
409
+ ci_cd: 'integrationKnowledge.ci_cd',
410
+ testing: 'integrationKnowledge.testing',
411
+ deployment: 'integrationKnowledge.deployment',
412
+ };
413
+
414
+ const path = categoryMap[category];
415
+ if (!path) {
416
+ return [];
417
+ }
418
+
419
+ return this.getNestedProperty(this.registry, path) || [];
420
+ }
421
+
422
+ /**
423
+ * 列出所有已注册的中间件(兼容 EnvironmentLoader)
424
+ */
425
+ listMiddleware() {
426
+ return this.list('middleware');
427
+ }
428
+
429
+ /**
430
+ * 列出所有已注册的组件(兼容 EnvironmentLoader)
431
+ */
432
+ listComponents() {
433
+ return this.list('components');
434
+ }
435
+
436
+ /**
437
+ * 列出所有已注册的集成配置(兼容 EnvironmentLoader)
438
+ */
439
+ listIntegrations(type) {
440
+ if (type) {
441
+ const typeMap = { ci_cd: 'ci_cd', testing: 'testing', deployment: 'deployment' };
442
+ return this.list(typeMap[type] || type);
443
+ }
444
+ return this.registry?.integrationKnowledge || {};
445
+ }
446
+
447
+ // ============================================================
448
+ // 激活状态管理
449
+ // ============================================================
450
+
451
+ /**
452
+ * 设置活跃配置
453
+ * @param {Object} profiles - 活跃配置 { environment, components, ci_cd }
454
+ */
455
+ setActiveProfiles(profiles) {
456
+ if (!this.registry) {
457
+ this.load();
458
+ }
459
+
460
+ if (!this.registry.activeProfiles) {
461
+ this.registry.activeProfiles = {};
462
+ }
463
+
464
+ if (profiles.environment !== undefined) {
465
+ this.registry.activeProfiles.environment = profiles.environment;
466
+ }
467
+ if (profiles.components !== undefined) {
468
+ this.registry.activeProfiles.components = profiles.components;
469
+ }
470
+ if (profiles.ci_cd !== undefined) {
471
+ this.registry.activeProfiles.ci_cd = profiles.ci_cd;
472
+ }
473
+
474
+ this.save();
475
+ }
476
+
477
+ /**
478
+ * 获取活跃配置
479
+ */
480
+ getActiveProfiles() {
481
+ if (!this.registry) {
482
+ this.load();
483
+ }
484
+
485
+ return this.registry.activeProfiles || {
486
+ environment: 'development',
487
+ components: [],
488
+ ci_cd: null,
489
+ middleware: [],
490
+ };
491
+ }
492
+
493
+ /**
494
+ * 设置激活的环境(兼容 EnvironmentLoader)
495
+ */
496
+ setActiveEnvironment(env) {
497
+ if (!this.registry) {
498
+ this.load();
499
+ }
500
+
501
+ if (!this.registry.activeProfiles) {
502
+ this.registry.activeProfiles = {};
503
+ }
504
+
505
+ this.registry.activeProfiles.environment = env;
506
+ this.save();
507
+ }
508
+
509
+ /**
510
+ * 设置激活的组件(兼容 EnvironmentLoader)
511
+ */
512
+ setActiveComponents(components) {
513
+ if (!this.registry) {
514
+ this.load();
515
+ }
516
+
517
+ if (!this.registry.activeProfiles) {
518
+ this.registry.activeProfiles = {};
519
+ }
520
+
521
+ this.registry.activeProfiles.components = components;
522
+ this.save();
523
+ }
524
+
525
+ /**
526
+ * 设置激活的 CI/CD 系统(兼容 EnvironmentLoader)
527
+ */
528
+ setActiveCiCd(ciSystem) {
529
+ if (!this.registry) {
530
+ this.load();
531
+ }
532
+
533
+ if (!this.registry.activeProfiles) {
534
+ this.registry.activeProfiles = {};
535
+ }
536
+
537
+ this.registry.activeProfiles.ci_cd = ciSystem;
538
+ this.save();
539
+ }
540
+
541
+ /**
542
+ * 激活知识条目
543
+ * @param {string} category - 知识类别 (middleware, components, ci_cd)
544
+ * @param {string} name - 条目名称
545
+ */
546
+ activate(category, name) {
547
+ if (!this.registry) {
548
+ this.load();
549
+ }
550
+
551
+ if (!this.registry.activeProfiles) {
552
+ this.registry.activeProfiles = {
553
+ environment: 'development',
554
+ components: [],
555
+ ci_cd: null,
556
+ middleware: [],
557
+ };
558
+ }
559
+
560
+ // 确保 middleware 数组存在
561
+ if (!this.registry.activeProfiles.middleware) {
562
+ this.registry.activeProfiles.middleware = [];
563
+ }
564
+
565
+ switch (category) {
566
+ case 'middleware':
567
+ if (!this.registry.activeProfiles.middleware.includes(name)) {
568
+ this.registry.activeProfiles.middleware.push(name);
569
+ }
570
+ break;
571
+ case 'components':
572
+ if (!this.registry.activeProfiles.components.includes(name)) {
573
+ this.registry.activeProfiles.components.push(name);
574
+ }
575
+ break;
576
+ case 'ci_cd':
577
+ this.registry.activeProfiles.ci_cd = name;
578
+ break;
579
+ default:
580
+ console.warn(`Unknown category for activation: ${category}`);
581
+ }
582
+
583
+ this.save();
584
+ }
585
+
586
+ /**
587
+ * 停用知识条目
588
+ * @param {string} category - 知识类别
589
+ * @param {string} name - 条目名称
590
+ */
591
+ deactivate(category, name) {
592
+ if (!this.registry) {
593
+ this.load();
594
+ }
595
+
596
+ if (!this.registry.activeProfiles) {
597
+ return;
598
+ }
599
+
600
+ switch (category) {
601
+ case 'middleware':
602
+ this.registry.activeProfiles.middleware =
603
+ (this.registry.activeProfiles.middleware || []).filter(n => n !== name);
604
+ break;
605
+ case 'components':
606
+ this.registry.activeProfiles.components =
607
+ (this.registry.activeProfiles.components || []).filter(n => n !== name);
608
+ break;
609
+ case 'ci_cd':
610
+ if (this.registry.activeProfiles.ci_cd === name) {
611
+ this.registry.activeProfiles.ci_cd = null;
612
+ }
613
+ break;
614
+ }
615
+
616
+ this.save();
617
+ }
618
+
619
+ /**
620
+ * 检查知识条目是否激活
621
+ * @param {string} category - 知识类别
622
+ * @param {string} name - 条目名称
623
+ */
624
+ isActive(category, name) {
625
+ if (!this.registry) {
626
+ this.load();
627
+ }
628
+
629
+ if (!this.registry.activeProfiles) {
630
+ return false;
631
+ }
632
+
633
+ switch (category) {
634
+ case 'middleware':
635
+ return (this.registry.activeProfiles.middleware || []).includes(name);
636
+ case 'components':
637
+ return (this.registry.activeProfiles.components || []).includes(name);
638
+ case 'ci_cd':
639
+ return this.registry.activeProfiles.ci_cd === name;
640
+ default:
641
+ return false;
642
+ }
643
+ }
644
+
645
+ /**
646
+ * 获取所有激活的中间件
647
+ */
648
+ getActiveMiddleware() {
649
+ if (!this.registry) {
650
+ this.load();
651
+ }
652
+ return this.registry.activeProfiles?.middleware || [];
653
+ }
654
+
655
+ /**
656
+ * 获取所有激活的组件
657
+ */
658
+ getActiveComponents() {
659
+ if (!this.registry) {
660
+ this.load();
661
+ }
662
+ return this.registry.activeProfiles?.components || [];
663
+ }
664
+
665
+ // ============================================================
666
+ // 文件注册
667
+ // ============================================================
668
+
669
+ /**
670
+ * 从知识文件自动注册
671
+ * @param {string} filePath - 知识文件路径
672
+ */
673
+ registerFromFile(filePath) {
674
+ if (!fs.existsSync(filePath)) {
675
+ throw new Error(`File not found: ${filePath}`);
676
+ }
677
+
678
+ const content = fs.readFileSync(filePath, 'utf-8');
679
+ const knowledge = yaml.load(content);
680
+
681
+ const kind = knowledge.kind;
682
+ const meta = knowledge.metadata || knowledge.meta;
683
+
684
+ if (!meta || !meta.name) {
685
+ throw new Error('Knowledge file missing metadata.name');
686
+ }
687
+
688
+ const relativePath = path.relative(this.envDir, filePath);
689
+
690
+ const entry = {
691
+ name: meta.name,
692
+ path: relativePath,
693
+ version: meta.version || '1.0',
694
+ tags: meta.tags || [],
695
+ };
696
+
697
+ // 根据 kind 确定类别
698
+ const kindToCategory = {
699
+ [KNOWLEDGE_TYPES.MIDDLEWARE]: 'middleware',
700
+ [KNOWLEDGE_TYPES.INFRASTRUCTURE]: 'infrastructure',
701
+ [KNOWLEDGE_TYPES.COMPONENT]: 'components',
702
+ [KNOWLEDGE_TYPES.INTEGRATION]: 'ci_cd', // 需要根据 spec.type 进一步区分
703
+ };
704
+
705
+ let category = kindToCategory[kind];
706
+
707
+ // 对于 Integration 类型,根据 spec.type 细分
708
+ if (kind === KNOWLEDGE_TYPES.INTEGRATION && knowledge.spec?.type) {
709
+ const integrationTypeMap = {
710
+ 'ci-cd': 'ci_cd',
711
+ 'ci_cd': 'ci_cd',
712
+ 'testing': 'testing',
713
+ 'deployment': 'deployment',
714
+ };
715
+ category = integrationTypeMap[knowledge.spec.type] || 'ci_cd';
716
+ }
717
+
718
+ if (!category) {
719
+ throw new Error(`Unknown knowledge kind: ${kind}`);
720
+ }
721
+
722
+ this.register(category, entry);
723
+ return entry;
724
+ }
725
+
726
+ /**
727
+ * 同步目录中的所有知识文件
728
+ * @param {string} subDir - 子目录名
729
+ */
730
+ syncFromDirectory(subDir) {
731
+ const dirPath = path.join(this.envDir, subDir);
732
+ if (!fs.existsSync(dirPath)) {
733
+ return [];
734
+ }
735
+
736
+ const registered = [];
737
+ const files = fs.readdirSync(dirPath, { withFileTypes: true });
738
+
739
+ for (const file of files) {
740
+ if (file.isFile() && (file.name.endsWith('.yaml') || file.name.endsWith('.yml'))) {
741
+ try {
742
+ const entry = this.registerFromFile(path.join(dirPath, file.name));
743
+ registered.push(entry);
744
+ } catch (e) {
745
+ console.warn(`Failed to register ${file.name}:`, e.message);
746
+ }
747
+ } else if (file.isDirectory()) {
748
+ // 处理组件目录(如 components/auth/component.yaml)
749
+ const subDirPath = path.join(dirPath, file.name);
750
+ const componentFile = path.join(subDirPath, 'component.yaml');
751
+ if (fs.existsSync(componentFile)) {
752
+ try {
753
+ const entry = this.registerFromFile(componentFile);
754
+ registered.push(entry);
755
+ } catch (e) {
756
+ console.warn(`Failed to register ${file.name}:`, e.message);
757
+ }
758
+ }
759
+ }
760
+ }
761
+
762
+ return registered;
763
+ }
764
+
765
+ /**
766
+ * 同步所有知识文件
767
+ */
768
+ syncAll() {
769
+ const results = {
770
+ middleware: this.syncFromDirectory('env-knowledge/middleware'),
771
+ infrastructure: this.syncFromDirectory('env-knowledge/infrastructure'),
772
+ components: this.syncFromDirectory('component-lib/components'),
773
+ ci_cd: this.syncFromDirectory('integration/ci-cd'),
774
+ testing: this.syncFromDirectory('integration/testing'),
775
+ deployment: this.syncFromDirectory('integration/deployment'),
776
+ };
777
+
778
+ return results;
779
+ }
780
+
781
+ // ============================================================
782
+ // 工具方法
783
+ // ============================================================
784
+
785
+ /**
786
+ * 获取嵌套属性
787
+ */
788
+ getNestedProperty(obj, path) {
789
+ return path.split('.').reduce((current, key) => {
790
+ return current && current[key] !== undefined ? current[key] : null;
791
+ }, obj);
792
+ }
793
+
794
+ /**
795
+ * 设置嵌套属性
796
+ */
797
+ setNestedProperty(obj, path, value) {
798
+ const keys = path.split('.');
799
+ const lastKey = keys.pop();
800
+ const target = keys.reduce((current, key) => {
801
+ if (!current[key]) {
802
+ current[key] = {};
803
+ }
804
+ return current[key];
805
+ }, obj);
806
+ target[lastKey] = value;
807
+ }
808
+ }
809
+
810
+ // 向后兼容:EnvironmentLoader 是 EnvironmentRegistry 的别名
811
+ export { EnvironmentRegistry as EnvironmentLoader };
812
+
813
+ export default EnvironmentRegistry;