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,367 @@
|
|
|
1
|
+
// lib/token/budget-alert.js - Token Budget Alert System
|
|
2
|
+
// 超预算时的自动预警和处理策略
|
|
3
|
+
|
|
4
|
+
import chalk from 'chalk';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* 预警级别枚举
|
|
8
|
+
*/
|
|
9
|
+
export const ALERT_LEVELS = {
|
|
10
|
+
INFO: 'info',
|
|
11
|
+
WARNING: 'warning',
|
|
12
|
+
CRITICAL: 'critical'
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* 预警处理策略枚举
|
|
17
|
+
*/
|
|
18
|
+
export const ALERT_STRATEGIES = {
|
|
19
|
+
LOG_ONLY: 'log_only', // 仅记录日志
|
|
20
|
+
SOFT_BLOCK: 'soft_block', // 软阻断(警告但允许继续)
|
|
21
|
+
HARD_BLOCK: 'hard_block', // 硬阻断(直接拒绝)
|
|
22
|
+
AUTO_SCALE: 'auto_scale' // 自动降级(降低后续请求的token预算)
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* BudgetAlert - Token预算预警系统
|
|
27
|
+
*
|
|
28
|
+
* 职责:
|
|
29
|
+
* - 监控Token使用率,在达到阈值时触发预警
|
|
30
|
+
* - 提供多级预警处理策略(日志/软阻断/硬阻断/自动降级)
|
|
31
|
+
* - 生成结构化的预警报告
|
|
32
|
+
* - 支持自定义预警回调(如发送通知、写入文件等)
|
|
33
|
+
*
|
|
34
|
+
* 使用示例:
|
|
35
|
+
* ```js
|
|
36
|
+
* const alert = new BudgetAlert({
|
|
37
|
+
* warningThreshold: 0.8,
|
|
38
|
+
* criticalThreshold: 0.95,
|
|
39
|
+
* strategy: ALERT_STRATEGIES.SOFT_BLOCK,
|
|
40
|
+
* onWarning: (data) => sendNotification(data),
|
|
41
|
+
* onCritical: (data) => emergencyStop(data)
|
|
42
|
+
* });
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
45
|
+
export class BudgetAlert {
|
|
46
|
+
/**
|
|
47
|
+
* 创建预算预警实例
|
|
48
|
+
* @param {Object} config - 配置选项
|
|
49
|
+
* @param {number} config.warningThreshold - 预警阈值(默认0.8)
|
|
50
|
+
* @param {number} config.criticalThreshold - 阻断阈值(默认0.95)
|
|
51
|
+
* @param {string} config.strategy - 处理策略(默认SOFT_BLOCK)
|
|
52
|
+
* @param {Function} config.onWarning - 预警回调
|
|
53
|
+
* @param {Function} config.onCritical - 阻断回调
|
|
54
|
+
* @param {Object} config.manager - 关联的BudgetManager实例
|
|
55
|
+
*/
|
|
56
|
+
constructor(config = {}) {
|
|
57
|
+
this.warningThreshold = config.warningThreshold || 0.8;
|
|
58
|
+
this.criticalThreshold = config.criticalThreshold || 0.95;
|
|
59
|
+
this.strategy = config.strategy || ALERT_STRATEGIES.SOFT_BLOCK;
|
|
60
|
+
this.manager = config.manager || null;
|
|
61
|
+
|
|
62
|
+
// 自定义回调
|
|
63
|
+
this.onWarningCallback = config.onWarning || null;
|
|
64
|
+
this.onCriticalCallback = config.onCritical || null;
|
|
65
|
+
|
|
66
|
+
// 预警历史记录
|
|
67
|
+
this.alertHistory = [];
|
|
68
|
+
// 状态追踪
|
|
69
|
+
this.warningTriggered = false;
|
|
70
|
+
this.criticalTriggered = false;
|
|
71
|
+
this.firstWarningTime = null;
|
|
72
|
+
this.firstCriticalTime = null;
|
|
73
|
+
|
|
74
|
+
// 冷却时间(毫秒),防止频繁报警
|
|
75
|
+
this.warningCooldown = config.warningCooldown || 30000; // 30秒
|
|
76
|
+
this.criticalCooldown = config.criticalCooldown || 10000; // 10秒
|
|
77
|
+
this.lastWarningTime = 0;
|
|
78
|
+
this.lastCriticalTime = 0;
|
|
79
|
+
|
|
80
|
+
// 统计信息
|
|
81
|
+
this.stats = {
|
|
82
|
+
totalWarnings: 0,
|
|
83
|
+
totalCriticals: 0,
|
|
84
|
+
totalBlockedOperations: 0,
|
|
85
|
+
autoScaledCount: 0
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* 处理预警级别事件
|
|
91
|
+
* @param {Object} data - 消耗数据
|
|
92
|
+
* @returns {Object} 预警结果
|
|
93
|
+
*/
|
|
94
|
+
handleWarning(data) {
|
|
95
|
+
const now = Date.now();
|
|
96
|
+
|
|
97
|
+
// 检查冷却时间
|
|
98
|
+
if (now - this.lastWarningTime < this.warningCooldown) {
|
|
99
|
+
return { handled: false, reason: 'cooldown' };
|
|
100
|
+
}
|
|
101
|
+
this.lastWarningTime = now;
|
|
102
|
+
|
|
103
|
+
if (!this.warningTriggered) {
|
|
104
|
+
this.firstWarningTime = now;
|
|
105
|
+
this.warningTriggered = true;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const alertRecord = this._createAlertRecord(ALERT_LEVELS.WARNING, data);
|
|
109
|
+
this.alertHistory.push(alertRecord);
|
|
110
|
+
this.stats.totalWarnings++;
|
|
111
|
+
|
|
112
|
+
// 输出控制台预警
|
|
113
|
+
this._printWarningAlert(data);
|
|
114
|
+
|
|
115
|
+
// 执行策略
|
|
116
|
+
let actionTaken = null;
|
|
117
|
+
switch (this.strategy) {
|
|
118
|
+
case ALERT_STRATEGIES.LOG_ONLY:
|
|
119
|
+
actionTaken = 'logged';
|
|
120
|
+
break;
|
|
121
|
+
case ALERT_STRATEGIES.SOFT_BLOCK:
|
|
122
|
+
actionTaken = 'warned';
|
|
123
|
+
break;
|
|
124
|
+
case ALERT_STRATEGIES.AUTO_SCALE:
|
|
125
|
+
actionTaken = this._applyAutoScale(data);
|
|
126
|
+
break;
|
|
127
|
+
default:
|
|
128
|
+
actionTaken = 'warned';
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// 触发自定义回调
|
|
132
|
+
if (typeof this.onWarningCallback === 'function') {
|
|
133
|
+
try {
|
|
134
|
+
this.onWarningCallback({ ...alertRecord, actionTaken });
|
|
135
|
+
} catch (e) {
|
|
136
|
+
console.error(chalk.red('[BudgetAlert] onWarning回调执行失败:', e.message));
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
return {
|
|
141
|
+
handled: true,
|
|
142
|
+
level: ALERT_LEVELS.WARNING,
|
|
143
|
+
record: alertRecord,
|
|
144
|
+
actionTaken,
|
|
145
|
+
shouldContinue: this.strategy !== ALERT_STRATEGIES.HARD_BLOCK
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* 处理阻断级别事件
|
|
151
|
+
* @param {Object} data - 消耗数据
|
|
152
|
+
* @returns {Object} 阻断结果
|
|
153
|
+
*/
|
|
154
|
+
handleCritical(data) {
|
|
155
|
+
const now = Date.now();
|
|
156
|
+
|
|
157
|
+
// 检查冷却时间
|
|
158
|
+
if (now - this.lastCriticalTime < this.criticalCooldown) {
|
|
159
|
+
return { handled: false, reason: 'cooldown' };
|
|
160
|
+
}
|
|
161
|
+
this.lastCriticalTime = now;
|
|
162
|
+
|
|
163
|
+
if (!this.criticalTriggered) {
|
|
164
|
+
this.firstCriticalTime = now;
|
|
165
|
+
this.criticalTriggered = true;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
const alertRecord = this._createAlertRecord(ALERT_LEVELS.CRITICAL, data);
|
|
169
|
+
this.alertHistory.push(alertRecord);
|
|
170
|
+
this.stats.totalCriticals++;
|
|
171
|
+
this.stats.totalBlockedOperations++;
|
|
172
|
+
|
|
173
|
+
// 输出控制台阻断警报
|
|
174
|
+
this._printCriticalAlert(data);
|
|
175
|
+
|
|
176
|
+
// 执行策略
|
|
177
|
+
let actionTaken = 'blocked';
|
|
178
|
+
let shouldContinue = false;
|
|
179
|
+
|
|
180
|
+
switch (this.strategy) {
|
|
181
|
+
case ALERT_STRATEGIES.LOG_ONLY:
|
|
182
|
+
actionTaken = 'logged';
|
|
183
|
+
shouldContinue = true;
|
|
184
|
+
break;
|
|
185
|
+
case ALERT_STRATEGIES.SOFT_BLOCK:
|
|
186
|
+
actionTaken = 'soft_blocked';
|
|
187
|
+
shouldContinue = false;
|
|
188
|
+
break;
|
|
189
|
+
case ALERT_STRATEGIES.HARD_BLOCK:
|
|
190
|
+
actionTaken = 'hard_blocked';
|
|
191
|
+
shouldContinue = false;
|
|
192
|
+
break;
|
|
193
|
+
case ALERT_STRATEGIES.AUTO_SCALE:
|
|
194
|
+
actionTaken = this._applyAutoScale(data);
|
|
195
|
+
shouldContinue = true; // 自动降级后仍可继续,但用更少tokens
|
|
196
|
+
break;
|
|
197
|
+
default:
|
|
198
|
+
shouldContinue = false;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
// 触发自定义回调
|
|
202
|
+
if (typeof this.onCriticalCallback === 'function') {
|
|
203
|
+
try {
|
|
204
|
+
this.onCriticalCallback({ ...alertRecord, actionTaken });
|
|
205
|
+
} catch (e) {
|
|
206
|
+
console.error(chalk.red('[BudgetAlert] onCritical回调执行失败:', e.message));
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
return {
|
|
211
|
+
handled: true,
|
|
212
|
+
level: ALERT_LEVELS.CRITICAL,
|
|
213
|
+
record: alertRecord,
|
|
214
|
+
actionTaken,
|
|
215
|
+
shouldContinue
|
|
216
|
+
};
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* 获取预警历史记录
|
|
221
|
+
* @param {Object} options - 过滤选项
|
|
222
|
+
* @returns {Array}
|
|
223
|
+
*/
|
|
224
|
+
getHistory(options = {}) {
|
|
225
|
+
let filtered = [...this.alertHistory];
|
|
226
|
+
|
|
227
|
+
if (options.level) {
|
|
228
|
+
filtered = filtered.filter(a => a.level === options.level);
|
|
229
|
+
}
|
|
230
|
+
if (options.since) {
|
|
231
|
+
filtered = filtered.filter(a => a.timestamp >= options.since);
|
|
232
|
+
}
|
|
233
|
+
if (options.limit) {
|
|
234
|
+
filtered = filtered.slice(-options.limit);
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
return filtered;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* 获取当前预警状态摘要
|
|
242
|
+
* @returns {Object}
|
|
243
|
+
*/
|
|
244
|
+
getStatus() {
|
|
245
|
+
return {
|
|
246
|
+
warningTriggered: this.warningTriggered,
|
|
247
|
+
criticalTriggered: this.criticalTriggered,
|
|
248
|
+
firstWarningTime: this.firstWarningTime ? new Date(this.firstWarningTime).toISOString() : null,
|
|
249
|
+
firstCriticalTime: this.firstCriticalTime ? new Date(this.firstCriticalTime).toISOString() : null,
|
|
250
|
+
currentStrategy: this.strategy,
|
|
251
|
+
stats: { ...this.stats },
|
|
252
|
+
thresholds: {
|
|
253
|
+
warning: this.warningThreshold,
|
|
254
|
+
critical: this.criticalThreshold
|
|
255
|
+
}
|
|
256
|
+
};
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
/**
|
|
260
|
+
* 重置预警状态
|
|
261
|
+
*/
|
|
262
|
+
reset() {
|
|
263
|
+
this.alertHistory = [];
|
|
264
|
+
this.warningTriggered = false;
|
|
265
|
+
this.criticalTriggered = false;
|
|
266
|
+
this.firstWarningTime = null;
|
|
267
|
+
this.firstCriticalTime = null;
|
|
268
|
+
this.lastWarningTime = 0;
|
|
269
|
+
this.lastCriticalTime = 0;
|
|
270
|
+
this.stats = {
|
|
271
|
+
totalWarnings: 0,
|
|
272
|
+
totalCriticals: 0,
|
|
273
|
+
totalBlockedOperations: 0,
|
|
274
|
+
autoScaledCount: 0
|
|
275
|
+
};
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
/**
|
|
279
|
+
* 更新处理策略
|
|
280
|
+
* @param {string} strategy - 新的策略
|
|
281
|
+
*/
|
|
282
|
+
setStrategy(strategy) {
|
|
283
|
+
if (!Object.values(ALERT_STRATEGIES).includes(strategy)) {
|
|
284
|
+
throw new Error(`无效的预警策略: ${strategy}`);
|
|
285
|
+
}
|
|
286
|
+
this.strategy = strategy;
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
// ==================== 私有方法 ====================
|
|
290
|
+
|
|
291
|
+
/**
|
|
292
|
+
* 创建预警记录
|
|
293
|
+
* @private
|
|
294
|
+
*/
|
|
295
|
+
_createAlertRecord(level, data) {
|
|
296
|
+
return {
|
|
297
|
+
id: this.alertHistory.length + 1,
|
|
298
|
+
level,
|
|
299
|
+
timestamp: Date.now(),
|
|
300
|
+
isoTime: new Date().toISOString(),
|
|
301
|
+
usagePercent: data.usagePercent || (data.cumulativeUsage / (this.manager?.totalBudget || 100000)),
|
|
302
|
+
phase: data.phase,
|
|
303
|
+
tokens: data.tokens,
|
|
304
|
+
detail: data.detail,
|
|
305
|
+
remaining: data.remaining,
|
|
306
|
+
strategy: this.strategy
|
|
307
|
+
};
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
/**
|
|
311
|
+
* 输出预警到控制台
|
|
312
|
+
* @private
|
|
313
|
+
*/
|
|
314
|
+
_printWarningAlert(data) {
|
|
315
|
+
const percent = ((data.usagePercent || 0) * 100).toFixed(1);
|
|
316
|
+
console.log('\n' + chalk.yellow('╔══════════════════════════════════════════════════╗'));
|
|
317
|
+
console.log(chalk.yellow('║') + chalk.bold.yellow(' TOKEN BUDGET WARNING ') + chalk.yellow('║'));
|
|
318
|
+
console.log(chalk.yellow('╠══════════════════════════════════════════════════╣'));
|
|
319
|
+
console.log(chalk.yellow('║') + ` 使用率: ${chalk.bold(percent)}% | 阈值: ${(this.warningThreshold * 100).toFixed(0)}% ` + chalk.yellow('║'));
|
|
320
|
+
console.log(chalk.yellow('║') + ` 剩余: ${chalk.cyan((data.remaining || 0).toLocaleString())} tokens ` + chalk.yellow('║'));
|
|
321
|
+
if (data.phase) {
|
|
322
|
+
console.log(chalk.yellow('║') + ` 阶段: ${chalk.white(data.phase)} | 操作: ${chalk.gray((data.detail || '').substring(0, 30))} ` + chalk.yellow('║'));
|
|
323
|
+
}
|
|
324
|
+
console.log(chalk.yellow('╚══════════════════════════════════════════════════╝\n');
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
/**
|
|
328
|
+
* 输出阻断警报到控制台
|
|
329
|
+
* @private
|
|
330
|
+
*/
|
|
331
|
+
_printCriticalAlert(data) {
|
|
332
|
+
const percent = ((data.usagePercent || 0) * 100).toFixed(1);
|
|
333
|
+
console.log('\n' + chalk.red('╔══════════════════════════════════════════════════╗'));
|
|
334
|
+
console.log(chalk.red('║') + chalk.bold.bgRed(' TOKEN BUDGET CRITICAL - BLOCKED ') + chalk.red('║'));
|
|
335
|
+
console.log(chalk.red('╠══════════════════════════════════════════════════╣'));
|
|
336
|
+
console.log(chalk.red('║') + ` 使用率: ${chalk.bold.white(percent)}% | 阈值: ${(this.criticalThreshold * 100).toFixed(0)}% ` + chalk.red('║'));
|
|
337
|
+
console.log(chalk.red('║') + ` 操作已被${chalk.bold('阻断')}! 剩余: ${(data.remaining || 0).toLocaleString()} tokens ` + chalk.red('║'));
|
|
338
|
+
if (data.phase) {
|
|
339
|
+
console.log(chalk.red('║') + ` 阶段: ${chalk.white(data.phase)} | 请求: ${(data.tokens || 0).toLocaleString()} tokens ` + chalk.red('║'));
|
|
340
|
+
}
|
|
341
|
+
console.log(chalk.red('╚══════════════════════════════════════════════════╝\n'));
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
/**
|
|
345
|
+
* 应用自动降级策略
|
|
346
|
+
* @private
|
|
347
|
+
*/
|
|
348
|
+
_applyAutoScale(data) {
|
|
349
|
+
this.stats.autoScaledCount++;
|
|
350
|
+
|
|
351
|
+
// 计算建议的缩放因子
|
|
352
|
+
const usagePercent = data.usagePercent || 0;
|
|
353
|
+
let scaleFactor = 1.0;
|
|
354
|
+
|
|
355
|
+
if (usagePercent >= this.criticalThreshold) {
|
|
356
|
+
scaleFactor = 0.3; // 降至30%
|
|
357
|
+
} else if (usagePercent >= this.warningThreshold) {
|
|
358
|
+
scaleFactor = 0.6; // 降至60%
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
console.log(chalk.yellow(`[BudgetAlert] 自动降级: 后续请求Token预算缩放至 ${Math.round(scaleFactor * 100)}%`));
|
|
362
|
+
|
|
363
|
+
return `auto_scaled_${Math.round(scaleFactor * 100)}pct`;
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
export default BudgetAlert;
|