pdd-skills 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.
- package/README.md +1478 -0
- package/bin/pdd.js +354 -0
- package/config/bpmn-rules.yaml +166 -0
- package/config/checkstyle.xml +105 -0
- package/config/eslint.config.js +48 -0
- package/config/pmd.xml +91 -0
- package/config/prd-rules.yaml +113 -0
- package/config/ruff.toml +45 -0
- package/config/sqlfluff.cfg +82 -0
- package/hooks/hook-executor.js +332 -0
- package/index.js +43 -0
- package/lib/api-routes.js +750 -0
- package/lib/api-server.js +408 -0
- package/lib/cache/cache-config.js +209 -0
- package/lib/cache/system-cache.js +852 -0
- package/lib/config-manager.js +373 -0
- package/lib/generate.js +528 -0
- package/lib/grpc/grpc-routes.js +1134 -0
- package/lib/grpc/grpc-server.js +912 -0
- package/lib/grpc/proto-definitions.js +1033 -0
- package/lib/init.js +172 -0
- package/lib/iteration/auto-fixer.js +1025 -0
- package/lib/iteration/auto-reviewer.js +923 -0
- package/lib/iteration/controller.js +577 -0
- package/lib/list.js +130 -0
- package/lib/mcp-server.js +548 -0
- package/lib/openclaw/api-integration.js +535 -0
- package/lib/openclaw/cli-integration.js +567 -0
- package/lib/openclaw/data-sync.js +845 -0
- package/lib/openclaw/openclaw-adapter.js +783 -0
- package/lib/plugin/example-plugins/code-stats/index.js +332 -0
- package/lib/plugin/example-plugins/code-stats/plugin.json +1 -0
- package/lib/plugin/example-plugins/custom-linter/index.js +472 -0
- package/lib/plugin/example-plugins/custom-linter/plugin.json +1 -0
- package/lib/plugin/example-plugins/hello-world/index.js +86 -0
- package/lib/plugin/example-plugins/hello-world/plugin.json +1 -0
- package/lib/plugin/plugin-manager.js +655 -0
- package/lib/plugin/plugin-sdk.js +565 -0
- package/lib/plugin/sandbox.js +627 -0
- package/lib/quality/rules/maintainability.js +418 -0
- package/lib/quality/rules/performance.js +498 -0
- package/lib/quality/rules/readability.js +441 -0
- package/lib/quality/rules/robustness.js +504 -0
- package/lib/quality/rules/security.js +444 -0
- package/lib/quality/scorer.js +576 -0
- package/lib/report.js +669 -0
- package/lib/sdk-base.js +301 -0
- package/lib/sdk-js.js +446 -0
- package/lib/sdk-python/README.md +546 -0
- package/lib/sdk-python/examples/basic_usage.py +450 -0
- package/lib/sdk-python/pdd_sdk/__init__.py +180 -0
- package/lib/sdk-python/pdd_sdk/client.py +1170 -0
- package/lib/sdk-python/pdd_sdk/events.py +423 -0
- package/lib/sdk-python/pdd_sdk/exceptions.py +158 -0
- package/lib/sdk-python/pdd_sdk/models.py +518 -0
- package/lib/sdk-python/pdd_sdk/utils.py +759 -0
- package/lib/token/budget-alert.js +367 -0
- package/lib/token/budget-manager.js +485 -0
- package/lib/update.js +54 -0
- package/lib/utils/logger.js +88 -0
- package/lib/verify.js +741 -0
- package/lib/version.js +52 -0
- package/lib/vm/README.md +102 -0
- package/lib/vm/dashboard/api-routes.js +669 -0
- package/lib/vm/dashboard/server.js +391 -0
- package/lib/vm/dashboard/sse.js +358 -0
- package/lib/vm/dashboard/static/css/dashboard.css +1378 -0
- package/lib/vm/dashboard/static/index.html +118 -0
- package/lib/vm/dashboard/static/js/app.js +949 -0
- package/lib/vm/dashboard/static/js/charts.js +913 -0
- package/lib/vm/dashboard/static/js/kanban-view.js +1053 -0
- package/lib/vm/dashboard/static/js/pipeline-view.js +463 -0
- package/lib/vm/dashboard/static/js/quality-view.js +598 -0
- package/lib/vm/dashboard/static/js/system-view.js +1021 -0
- package/lib/vm/data-provider.js +1191 -0
- package/lib/vm/event-bus.js +402 -0
- package/lib/vm/hooks/extract-hook.js +307 -0
- package/lib/vm/hooks/generate-hook.js +374 -0
- package/lib/vm/hooks/hook-interface.js +458 -0
- package/lib/vm/hooks/report-hook.js +331 -0
- package/lib/vm/hooks/verify-hook.js +454 -0
- package/lib/vm/models.js +1003 -0
- package/lib/vm/reconciler.js +855 -0
- package/lib/vm/scanner.js +988 -0
- package/lib/vm/state-schema.js +955 -0
- package/lib/vm/state-store.js +733 -0
- package/lib/vm/tui/components/card.js +339 -0
- package/lib/vm/tui/components/progress-bar.js +368 -0
- package/lib/vm/tui/components/sparkline.js +327 -0
- package/lib/vm/tui/components/status-light.js +294 -0
- package/lib/vm/tui/components/table.js +370 -0
- package/lib/vm/tui/input.js +335 -0
- package/lib/vm/tui/renderer.js +548 -0
- package/lib/vm/tui/screens/kanban-screen.js +397 -0
- package/lib/vm/tui/screens/overview-screen.js +357 -0
- package/lib/vm/tui/screens/quality-screen.js +336 -0
- package/lib/vm/tui/screens/system-screen.js +379 -0
- package/lib/vm/tui/tui.js +805 -0
- package/package.json +1 -0
- package/scripts/cso-analyzer.js +198 -0
- package/scripts/eval-runner.js +359 -0
- package/scripts/i18n-checker.js +109 -0
- package/scripts/linter/activiti-linter.js +272 -0
- package/scripts/linter/prd-linter.js +162 -0
- package/scripts/linter/report-generator.js +207 -0
- package/scripts/linter/run-linters.js +285 -0
- package/scripts/linter/sql-linter.js +166 -0
- package/scripts/token-analyzer.js +162 -0
- package/scripts/vm-test.js +180 -0
- package/skills/core/official-doc-writer/LICENSE +21 -0
- package/skills/core/official-doc-writer/README.md +232 -0
- package/skills/core/official-doc-writer/SKILL.md +475 -0
- package/skills/core/official-doc-writer/_meta.json +1 -0
- package/skills/core/official-doc-writer/document_generator.py +580 -0
- package/skills/core/official-doc-writer/evals/default-evals.json +1 -0
- package/skills/core/official-doc-writer/examples.md +150 -0
- package/skills/core/official-doc-writer/fonts/FONTS_LIST.md +45 -0
- package/skills/core/official-doc-writer/fonts/README.md +141 -0
- package/skills/core/official-doc-writer/fonts/SIMFANG.TTF +0 -0
- package/skills/core/official-doc-writer/fonts/SIMHEI.TTF +0 -0
- package/skills/core/official-doc-writer/fonts/SIMKAI.TTF +0 -0
- package/skills/core/official-doc-writer/fonts/SIMSUN.TTC +0 -0
- package/skills/core/official-doc-writer/fonts//346/226/271/346/255/243/345/260/217/346/240/207/345/256/213GBK.TTF +0 -0
- package/skills/core/official-doc-writer/references/GBT_9704-2012_/345/205/232/346/224/277/346/234/272/345/205/263/345/205/254/346/226/207/346/240/274/345/274/217.md +422 -0
- package/skills/core/official-doc-writer/scripts/__pycache__/generate_official_doc.cpython-313.pyc +0 -0
- package/skills/core/official-doc-writer/scripts/dialog_manager.py +564 -0
- package/skills/core/official-doc-writer/scripts/generate_official_doc.py +252 -0
- package/skills/core/official-doc-writer/scripts/install_fonts.py +390 -0
- package/skills/core/official-doc-writer/scripts/smart_prompts.py +363 -0
- package/skills/core/pdd-ba/SKILL.md +305 -0
- package/skills/core/pdd-ba/_meta.json +1 -0
- package/skills/core/pdd-ba/evals/default-evals.json +1 -0
- package/skills/core/pdd-code-reviewer/SKILL.md +378 -0
- package/skills/core/pdd-code-reviewer/_meta.json +1 -0
- package/skills/core/pdd-code-reviewer/evals/default-evals.json +1 -0
- package/skills/core/pdd-doc-change/SKILL.md +350 -0
- package/skills/core/pdd-doc-change/_meta.json +1 -0
- package/skills/core/pdd-doc-change/evals/default-evals.json +1 -0
- package/skills/core/pdd-doc-gardener/SKILL.md +248 -0
- package/skills/core/pdd-doc-gardener/_meta.json +1 -0
- package/skills/core/pdd-doc-gardener/evals/default-evals.json +1 -0
- package/skills/core/pdd-entropy-reduction/SKILL.md +360 -0
- package/skills/core/pdd-entropy-reduction/_meta.json +1 -0
- package/skills/core/pdd-entropy-reduction/evals/default-evals.json +1 -0
- package/skills/core/pdd-entropy-reduction/references/entropy-report-template.md +287 -0
- package/skills/core/pdd-entropy-reduction/references/golden-principles.md +573 -0
- package/skills/core/pdd-entropy-reduction/scripts/entropy_scan.py +712 -0
- package/skills/core/pdd-extract-features/SKILL.md +320 -0
- package/skills/core/pdd-extract-features/_meta.json +1 -0
- package/skills/core/pdd-extract-features/evals/default-evals.json +1 -0
- package/skills/core/pdd-generate-spec/SKILL.md +418 -0
- package/skills/core/pdd-generate-spec/_meta.json +1 -0
- package/skills/core/pdd-generate-spec/evals/default-evals.json +1 -0
- package/skills/core/pdd-implement-feature/SKILL.md +332 -0
- package/skills/core/pdd-implement-feature/_meta.json +1 -0
- package/skills/core/pdd-implement-feature/evals/default-evals.json +1 -0
- package/skills/core/pdd-main/SKILL.md +540 -0
- package/skills/core/pdd-main/_meta.json +1 -0
- package/skills/core/pdd-main/evals/default-evals.json +1 -0
- package/skills/core/pdd-main/evals/evals.json +215 -0
- package/skills/core/pdd-verify-feature/SKILL.md +474 -0
- package/skills/core/pdd-verify-feature/_meta.json +1 -0
- package/skills/core/pdd-verify-feature/evals/default-evals.json +1 -0
- package/skills/core/pdd-vm/evals/default-evals.json +1 -0
- package/skills/core/traffic-accident-assessor/LICENSE +29 -0
- package/skills/core/traffic-accident-assessor/SKILL.md +439 -0
- package/skills/core/traffic-accident-assessor/evals/evals.json +1 -0
- package/skills/core/traffic-accident-assessor/references/accident-types.md +369 -0
- package/skills/core/traffic-accident-assessor/references/liability-rules.md +287 -0
- package/skills/core/traffic-accident-assessor/references/traffic-laws.md +226 -0
- package/skills/core/traffic-accident-assessor/references//351/253/230/345/260/224/345/244/253/350/257/264/346/230/216/344/271/246.pdf +32576 -106
- package/skills/core/traffic-accident-assessor/scripts/generate_official_statement.py +588 -0
- package/skills/core/traffic-accident-assessor/scripts/generate_report.py +495 -0
- package/skills/core/traffic-accident-assessor/scripts/generate_statement.py +528 -0
- package/skills/core/traffic-accident-assessor.zip +0 -0
- package/skills/entropy/expert-arch-enforcer/SKILL.md +292 -0
- package/skills/entropy/expert-arch-enforcer/_meta.json +1 -0
- package/skills/entropy/expert-arch-enforcer/evals/default-evals.json +1 -0
- package/skills/entropy/expert-auto-refactor/SKILL.md +327 -0
- package/skills/entropy/expert-auto-refactor/_meta.json +1 -0
- package/skills/entropy/expert-auto-refactor/evals/default-evals.json +1 -0
- package/skills/entropy/expert-code-quality/SKILL.md +468 -0
- package/skills/entropy/expert-code-quality/_meta.json +1 -0
- package/skills/entropy/expert-code-quality/evals/default-evals.json +1 -0
- package/skills/entropy/expert-code-quality/evals/evals.json +109 -0
- package/skills/entropy/expert-code-quality/references/code-smells.md +605 -0
- package/skills/entropy/expert-code-quality/references/design-patterns.md +1111 -0
- package/skills/entropy/expert-code-quality/references/refactoring-catalog.md +1281 -0
- package/skills/entropy/expert-code-quality/references/solid-principles.md +524 -0
- package/skills/entropy/expert-entropy-auditor/SKILL.md +276 -0
- package/skills/entropy/expert-entropy-auditor/_meta.json +1 -0
- package/skills/entropy/expert-entropy-auditor/evals/default-evals.json +1 -0
- package/skills/expert/expert-activiti/SKILL.md +497 -0
- package/skills/expert/expert-activiti/_meta.json +1 -0
- package/skills/expert/expert-mysql/SKILL.md +832 -0
- package/skills/expert/expert-mysql/_meta.json +1 -0
- package/skills/expert/expert-performance/SKILL.md +379 -0
- package/skills/expert/expert-performance/_meta.json +1 -0
- package/skills/expert/expert-performance/evals/default-evals.json +1 -0
- package/skills/expert/expert-ruoyi/SKILL.md +472 -0
- package/skills/expert/expert-ruoyi/_meta.json +1 -0
- package/skills/expert/expert-security/SKILL.md +1341 -0
- package/skills/expert/expert-security/_meta.json +1 -0
- package/skills/expert/expert-security/evals/default-evals.json +1 -0
- package/skills/expert/software-architect/SKILL.md +350 -0
- package/skills/expert/software-architect/_meta.json +1 -0
- package/skills/expert/software-engineer/SKILL.md +437 -0
- package/skills/expert/software-engineer/_meta.json +1 -0
- package/skills/expert/software-engineer/architecture.md +130 -0
- package/skills/expert/software-engineer/patterns.md +151 -0
- package/skills/expert/software-engineer/testing.md +135 -0
- package/skills/expert/system-architect/SKILL.md +628 -0
- package/skills/expert/system-architect/_meta.json +1 -0
- package/skills/expert/system-architect/assets/templates/ARCHITECTURE.md +25 -0
- package/skills/expert/system-architect/assets/templates/README.md +44 -0
- package/skills/expert/system-architect/references/js-ts-standards.md +18 -0
- package/skills/expert/system-architect/references/python-standards.md +19 -0
- package/skills/expert/system-architect/references/scaffolding.md +61 -0
- package/skills/expert/system-architect/references/security-checklist.md +21 -0
- package/skills/openspec/openspec-apply-change/SKILL.md +156 -0
- package/skills/openspec/openspec-apply-change/_meta.json +1 -0
- package/skills/openspec/openspec-archive-change/SKILL.md +114 -0
- package/skills/openspec/openspec-archive-change/_meta.json +1 -0
- package/skills/openspec/openspec-bulk-archive-change/SKILL.md +246 -0
- package/skills/openspec/openspec-bulk-archive-change/_meta.json +1 -0
- package/skills/openspec/openspec-continue-change/SKILL.md +118 -0
- package/skills/openspec/openspec-continue-change/_meta.json +1 -0
- package/skills/openspec/openspec-explore/SKILL.md +288 -0
- package/skills/openspec/openspec-explore/_meta.json +1 -0
- package/skills/openspec/openspec-ff-change/SKILL.md +101 -0
- package/skills/openspec/openspec-ff-change/_meta.json +1 -0
- package/skills/openspec/openspec-new-change/SKILL.md +74 -0
- package/skills/openspec/openspec-new-change/_meta.json +1 -0
- package/skills/openspec/openspec-onboard/SKILL.md +554 -0
- package/skills/openspec/openspec-onboard/_meta.json +1 -0
- package/skills/openspec/openspec-sync-specs/SKILL.md +138 -0
- package/skills/openspec/openspec-sync-specs/_meta.json +1 -0
- package/skills/openspec/openspec-verify-change/SKILL.md +168 -0
- package/skills/openspec/openspec-verify-change/_meta.json +1 -0
- package/skills/pr/pdd-multi-review/SKILL.md +534 -0
- package/skills/pr/pdd-multi-review/_meta.json +1 -0
- package/skills/pr/pdd-pr-batch/SKILL.md +303 -0
- package/skills/pr/pdd-pr-batch/_meta.json +1 -0
- package/skills/pr/pdd-pr-create/SKILL.md +344 -0
- package/skills/pr/pdd-pr-create/_meta.json +1 -0
- package/skills/pr/pdd-pr-merge/SKILL.md +286 -0
- package/skills/pr/pdd-pr-merge/_meta.json +1 -0
- package/skills/pr/pdd-pr-review/SKILL.md +217 -0
- package/skills/pr/pdd-pr-review/_meta.json +1 -0
- package/skills/pr/pdd-task-manager/SKILL.md +636 -0
- package/skills/pr/pdd-task-manager/_meta.json +1 -0
- package/skills/pr/pdd-template-engine/SKILL.md +306 -0
- package/skills/pr/pdd-template-engine/_meta.json +1 -0
- package/templates/behavior-shaping/iron-law-template.md +87 -0
- package/templates/behavior-shaping/rationalization-template.md +62 -0
- package/templates/behavior-shaping/red-flags-template.md +70 -0
- package/templates/bilingual-template.md +139 -0
- package/templates/config/default.yaml +47 -0
- package/templates/project/default/README.md +31 -0
- package/templates/project/frontend/README.md +46 -0
- package/templates/project/java/README.md +48 -0
|
@@ -0,0 +1,374 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module lib/vm/hooks/generate-hook
|
|
3
|
+
* @description Generate 命令 Hook 实现
|
|
4
|
+
* 负责记录代码生成命令的前后状态变化,包括:
|
|
5
|
+
* - 前置:标记功能点为 IMPLEMENTING 阶段,记录开始时间线
|
|
6
|
+
* - 后置:解析生成结果(文件列表、代码行数、Token 消耗),更新 Feature 的 artifacts 和 tokens
|
|
7
|
+
*
|
|
8
|
+
* 这是 PDD VM 系统中最核心的 Hook 之一
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { PDDHook } from './hook-interface.js';
|
|
12
|
+
import { StageEnum, updateStage, addTimelineEvent } from '../models.js';
|
|
13
|
+
import { getStateStore } from '../state-store.js';
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* GenerateHook - 处理 `pdd generate` 命令的生命周期事件
|
|
17
|
+
*
|
|
18
|
+
* @class
|
|
19
|
+
* @extends PDDHook
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* import { GenerateHook } from './lib/vm/hooks/generate-hook.js';
|
|
23
|
+
* const hook = new GenerateHook();
|
|
24
|
+
* hookManager.register(hook);
|
|
25
|
+
*/
|
|
26
|
+
export class GenerateHook extends PDDHook {
|
|
27
|
+
/**
|
|
28
|
+
* 创建 GenerateHook 实例
|
|
29
|
+
*/
|
|
30
|
+
constructor() {
|
|
31
|
+
super({ enabled: true });
|
|
32
|
+
/** @override */
|
|
33
|
+
this.name = 'generate';
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* StateStore 实例缓存(懒加载)
|
|
37
|
+
* @type {import('../state-store.js').StateStore|null}
|
|
38
|
+
* @private
|
|
39
|
+
*/
|
|
40
|
+
this._store = null;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* 获取或初始化 StateStore 实例
|
|
45
|
+
*
|
|
46
|
+
* @async
|
|
47
|
+
* @private
|
|
48
|
+
* @returns {Promise<import('../state-store.js').StateStore>}
|
|
49
|
+
*/
|
|
50
|
+
async _getStore() {
|
|
51
|
+
if (!this._store) {
|
|
52
|
+
this._store = await getStateStore();
|
|
53
|
+
}
|
|
54
|
+
return this._store;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* 命令执行前的钩子
|
|
59
|
+
* 主要职责:
|
|
60
|
+
* 1. 将 feature.stage 设置为 StageEnum.IMPLEMENTING
|
|
61
|
+
* 2. 在 timeline 中添加实现开始记录
|
|
62
|
+
* 3. 更新 state-store 持久化
|
|
63
|
+
*
|
|
64
|
+
* @async
|
|
65
|
+
* @override
|
|
66
|
+
* @param {import('./hook-interface.js').HookContext} ctx - 钩子上下文
|
|
67
|
+
* @returns {Promise<void>}
|
|
68
|
+
*/
|
|
69
|
+
async before(ctx) {
|
|
70
|
+
if (!this._isEnabled()) return;
|
|
71
|
+
|
|
72
|
+
try {
|
|
73
|
+
const store = await this._getStore();
|
|
74
|
+
const featureId = ctx.featureId || ctx.options?.feature;
|
|
75
|
+
|
|
76
|
+
// 如果没有指定 featureId,尝试从 options 中推断
|
|
77
|
+
if (!featureId) {
|
|
78
|
+
this._log('before', ctx);
|
|
79
|
+
console.log(
|
|
80
|
+
'[GenerateHook] 警告: 未提供 featureId,跳过状态记录'
|
|
81
|
+
);
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// 获取或创建 Feature
|
|
86
|
+
let feature = store.getFeature(featureId);
|
|
87
|
+
|
|
88
|
+
if (!feature) {
|
|
89
|
+
// 如果 Feature 不存在,自动创建一个
|
|
90
|
+
console.log(
|
|
91
|
+
`[GenerateHook] 功能点 ${featureId} 不存在,自动创建`
|
|
92
|
+
);
|
|
93
|
+
feature = await store.addFeature({
|
|
94
|
+
id: featureId,
|
|
95
|
+
name: ctx.options?.feature || featureId,
|
|
96
|
+
description: `通过 generate 命令自动创建`
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// 更新阶段为 IMPLEMENTING
|
|
101
|
+
updateStage(feature, StageEnum.IMPLEMENTING, '开始代码生成');
|
|
102
|
+
|
|
103
|
+
// 添加详细的时间线事件
|
|
104
|
+
addTimelineEvent(feature, StageEnum.IMPLEMENTING, '开始代码生成', {
|
|
105
|
+
command: 'generate',
|
|
106
|
+
spec: ctx.options?.spec,
|
|
107
|
+
output: ctx.options?.output,
|
|
108
|
+
dryRun: ctx.options?.dryRun || false
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
// 保存到磁盘
|
|
112
|
+
await store.updateFeature(featureId, {});
|
|
113
|
+
|
|
114
|
+
this._log('before', ctx);
|
|
115
|
+
console.log(
|
|
116
|
+
`[GenerateHook] 已将功能点 ${featureId} 标记为 ${StageEnum.IMPLEMENTING}`
|
|
117
|
+
);
|
|
118
|
+
} catch (error) {
|
|
119
|
+
console.error(`[GenerateHook] before 钩子执行失败: ${error.message}`);
|
|
120
|
+
// 不抛出异常,不影响主流程
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* 命令成功执行后的钩子
|
|
126
|
+
* 主要职责:
|
|
127
|
+
* 1. 解析 result 中的代码生成统计信息:
|
|
128
|
+
* - artifacts.code: 生成的文件路径列表
|
|
129
|
+
* - artifacts.loc: 总代码行数
|
|
130
|
+
* - artifacts.fileCount: 文件数
|
|
131
|
+
* - tokens.used: 本次 Token 消耗
|
|
132
|
+
* 2. 更新 feature.artifacts 和 feature.tokens
|
|
133
|
+
* 3. 在 timeline 中添加完成记录
|
|
134
|
+
* 4. 计算并记录命令耗时
|
|
135
|
+
*
|
|
136
|
+
* @async
|
|
137
|
+
* @override
|
|
138
|
+
* @param {import('./hook-interface.js').HookContext} ctx - 钩子上下文(包含 result)
|
|
139
|
+
* @returns {Promise<void>}
|
|
140
|
+
*/
|
|
141
|
+
async after(ctx) {
|
|
142
|
+
if (!this._isEnabled()) return;
|
|
143
|
+
|
|
144
|
+
try {
|
|
145
|
+
const store = await this._getStore();
|
|
146
|
+
const featureId = ctx.featureId || ctx.options?.feature;
|
|
147
|
+
const result = ctx.result;
|
|
148
|
+
|
|
149
|
+
if (!featureId) {
|
|
150
|
+
this._log('after', ctx);
|
|
151
|
+
console.log('[GenerateHook] 警告: 未提供 featureId,跳过结果记录');
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
const feature = store.getFeature(featureId);
|
|
156
|
+
|
|
157
|
+
if (!feature) {
|
|
158
|
+
console.log(
|
|
159
|
+
`[GenerateHook] 功能点 ${featureId} 不存在,无法记录结果`
|
|
160
|
+
);
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
// 解析代码生成结果
|
|
165
|
+
const generationResult = this._parseGenerationResult(result);
|
|
166
|
+
|
|
167
|
+
// 更新 artifacts
|
|
168
|
+
const artifactsUpdate = {
|
|
169
|
+
code: generationResult.files || [],
|
|
170
|
+
loc: generationResult.totalLoc || 0,
|
|
171
|
+
fileCount: generationResult.fileCount || 0,
|
|
172
|
+
language: generationResult.language || ''
|
|
173
|
+
};
|
|
174
|
+
|
|
175
|
+
// 如果有报告信息也一并更新
|
|
176
|
+
if (generationResult.reportPath) {
|
|
177
|
+
artifactsUpdate.report = {
|
|
178
|
+
path: generationResult.reportPath,
|
|
179
|
+
type: 'md',
|
|
180
|
+
lastModified: new Date()
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
// 更新 tokens 统计
|
|
185
|
+
const tokensUpdate = {};
|
|
186
|
+
|
|
187
|
+
if (generationResult.tokenUsage) {
|
|
188
|
+
tokensUpdate.input =
|
|
189
|
+
(feature.tokens?.input || 0) + (generationResult.tokenUsage.input || 0);
|
|
190
|
+
tokensUpdate.output =
|
|
191
|
+
(feature.tokens?.output || 0) + (generationResult.tokenUsage.output || 0);
|
|
192
|
+
tokensUpdate.total = tokensUpdate.input + tokensUpdate.output;
|
|
193
|
+
|
|
194
|
+
if (generationResult.tokenUsage.cost !== undefined) {
|
|
195
|
+
tokensUpdate.cost =
|
|
196
|
+
(feature.tokens?.cost || 0) + generationResult.tokenUsage.cost;
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
// 批量更新 Feature
|
|
201
|
+
await store.updateFeature(featureId, {
|
|
202
|
+
artifacts: artifactsUpdate,
|
|
203
|
+
tokens: tokensUpdate
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
// 添加完成时间线事件
|
|
207
|
+
addTimelineEvent(feature, StageEnum.IMPLEMENTING, '代码生成完成', {
|
|
208
|
+
filesGenerated: artifactsUpdate.fileCount,
|
|
209
|
+
totalLines: artifactsUpdate.loc,
|
|
210
|
+
tokensUsed: tokensUpdate.total || 0,
|
|
211
|
+
duration: ctx.duration,
|
|
212
|
+
commandOptions: {
|
|
213
|
+
spec: ctx.options?.spec,
|
|
214
|
+
output: ctx.options?.output,
|
|
215
|
+
dryRun: ctx.options?.dryRun
|
|
216
|
+
}
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
// 再次保存时间线更新
|
|
220
|
+
await store.saveState();
|
|
221
|
+
|
|
222
|
+
this._log('after', ctx);
|
|
223
|
+
|
|
224
|
+
// 输出摘要信息
|
|
225
|
+
console.log(
|
|
226
|
+
`[GenerateHook] 代码生成结果已记录:\n` +
|
|
227
|
+
` - 文件数: ${artifactsUpdate.fileCount}\n` +
|
|
228
|
+
` - 总行数: ${artifactsUpdate.loc}\n` +
|
|
229
|
+
` - Token消耗: ${tokensUpdate.total || 0}`
|
|
230
|
+
);
|
|
231
|
+
} catch (error) {
|
|
232
|
+
console.error(`[GenerateHook] after 钩子执行失败: ${error.message}`);
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* 命令执行出错时的钩子
|
|
238
|
+
* 记录错误信息到 timeline,但不改变 stage
|
|
239
|
+
*
|
|
240
|
+
* @async
|
|
241
|
+
* @override
|
|
242
|
+
* @param {import('./hook-interface.js').HookContext} ctx - 钩子上下文(包含 error)
|
|
243
|
+
* @returns {Promise<void>}
|
|
244
|
+
*/
|
|
245
|
+
async error(ctx) {
|
|
246
|
+
if (!this._isEnabled()) return;
|
|
247
|
+
|
|
248
|
+
try {
|
|
249
|
+
const store = await this._getStore();
|
|
250
|
+
const featureId = ctx.featureId || ctx.options?.feature;
|
|
251
|
+
|
|
252
|
+
if (!featureId) return;
|
|
253
|
+
|
|
254
|
+
const feature = store.getFeature(featureId);
|
|
255
|
+
if (!feature) return;
|
|
256
|
+
|
|
257
|
+
// 记录错误到 timeline(不改变 stage)
|
|
258
|
+
addTimelineEvent(feature, feature.stage, '代码生成失败', {
|
|
259
|
+
error: {
|
|
260
|
+
message: ctx.error?.message,
|
|
261
|
+
name: ctx.error?.name,
|
|
262
|
+
stack: ctx.error?.stack
|
|
263
|
+
},
|
|
264
|
+
duration: ctx.duration
|
|
265
|
+
});
|
|
266
|
+
|
|
267
|
+
await store.saveState();
|
|
268
|
+
|
|
269
|
+
this._log('error', ctx);
|
|
270
|
+
console.error(
|
|
271
|
+
`[GenerateHook] 已记录功能点 ${featureId} 的生成失败信息`
|
|
272
|
+
);
|
|
273
|
+
} catch (error) {
|
|
274
|
+
console.error(`[GenerateHook] error 钩子执行失败: ${error.message}`);
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
/**
|
|
279
|
+
* 解析代码生成命令的结果对象
|
|
280
|
+
* 支持多种可能的返回格式,提取标准化数据
|
|
281
|
+
*
|
|
282
|
+
* @private
|
|
283
|
+
* @param {*} result - generateCode 函数的返回值
|
|
284
|
+
* @returns {Object} 标准化的生成结果
|
|
285
|
+
* @returns {string[]} [result.files] - 生成的文件路径列表
|
|
286
|
+
* @returns {number} [result.totalLoc] - 总代码行数
|
|
287
|
+
* @returns {number} [result.fileCount] - 文件数量
|
|
288
|
+
* @returns {string} [result.language] - 主要编程语言
|
|
289
|
+
* @returns {Object} [result.tokenUsage] - Token 使用统计
|
|
290
|
+
* @returns {string} [result.reportPath] - 报告路径
|
|
291
|
+
*/
|
|
292
|
+
_parseGenerationResult(result) {
|
|
293
|
+
if (!result) {
|
|
294
|
+
return {};
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
// 标准化结果格式
|
|
298
|
+
const parsed = {
|
|
299
|
+
files: [],
|
|
300
|
+
totalLoc: 0,
|
|
301
|
+
fileCount: 0,
|
|
302
|
+
language: '',
|
|
303
|
+
tokenUsage: null,
|
|
304
|
+
reportPath: null
|
|
305
|
+
};
|
|
306
|
+
|
|
307
|
+
// 格式1: { files: [...], stats: { loc, fileCount }, tokens: {...} }
|
|
308
|
+
if (Array.isArray(result.files)) {
|
|
309
|
+
parsed.files = result.files;
|
|
310
|
+
parsed.fileCount = result.files.length;
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
// 尝试从不同位置获取 LOC 信息
|
|
314
|
+
if (result.stats?.loc) {
|
|
315
|
+
parsed.totalLoc = result.stats.loc;
|
|
316
|
+
} else if (result.artifacts?.loc) {
|
|
317
|
+
parsed.totalLoc = result.artifacts.loc;
|
|
318
|
+
} else if (result.totalLoc) {
|
|
319
|
+
parsed.totalLoc = result.totalLoc;
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
// 尝试获取文件数
|
|
323
|
+
if (result.stats?.fileCount) {
|
|
324
|
+
parsed.fileCount = result.stats.fileCount;
|
|
325
|
+
} else if (result.artifacts?.fileCount) {
|
|
326
|
+
parsed.fileCount = result.artifacts.fileCount;
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
// 获取编程语言
|
|
330
|
+
parsed.language = result.language || result.artifacts?.language || '';
|
|
331
|
+
|
|
332
|
+
// 解析 Token 使用情况
|
|
333
|
+
if (result.tokens) {
|
|
334
|
+
parsed.tokenUsage = {
|
|
335
|
+
input: result.tokens.input || 0,
|
|
336
|
+
output: result.tokens.output || 0,
|
|
337
|
+
cost: result.tokens.cost
|
|
338
|
+
};
|
|
339
|
+
} else if (result.tokenUsage) {
|
|
340
|
+
parsed.tokenUsage = result.tokenUsage;
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
// 获取报告路径
|
|
344
|
+
if (result.reportPath) {
|
|
345
|
+
parsed.reportPath = result.reportPath;
|
|
346
|
+
} else if (result.artifacts?.report?.path) {
|
|
347
|
+
parsed.reportPath = result.artifacts.report.path;
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
// 格式2: { generatedFiles: [...], codeStats: {...} }
|
|
351
|
+
if (!parsed.files.length && Array.isArray(result.generatedFiles)) {
|
|
352
|
+
parsed.files = result.generatedFiles;
|
|
353
|
+
parsed.fileCount = result.generatedFiles.length;
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
if (!parsed.totalLoc && result.codeStats?.totalLines) {
|
|
357
|
+
parsed.totalLoc = result.codeStats.totalLines;
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
// 格式3: 直接是文件路径数组
|
|
361
|
+
if (
|
|
362
|
+
!parsed.files.length &&
|
|
363
|
+
Array.isArray(result) &&
|
|
364
|
+
typeof result[0] === 'string'
|
|
365
|
+
) {
|
|
366
|
+
parsed.files = result;
|
|
367
|
+
parsed.fileCount = result.length;
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
return parsed;
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
export default GenerateHook;
|