@dommaker/harness 0.8.3 → 0.9.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 +127 -187
- package/bin/harness.js +15 -65
- package/dist/agents/index.d.ts +3 -0
- package/dist/agents/index.d.ts.map +1 -0
- package/dist/agents/index.js +19 -0
- package/dist/agents/index.js.map +1 -0
- package/dist/agents/lifecycle.d.ts +68 -0
- package/dist/agents/lifecycle.d.ts.map +1 -0
- package/dist/agents/lifecycle.js +181 -0
- package/dist/agents/lifecycle.js.map +1 -0
- package/dist/agents/types.d.ts +42 -0
- package/dist/agents/types.d.ts.map +1 -0
- package/dist/agents/types.js +6 -0
- package/dist/agents/types.js.map +1 -0
- package/dist/architecture/cross-project-checker.d.ts +19 -1
- package/dist/architecture/cross-project-checker.d.ts.map +1 -1
- package/dist/architecture/cross-project-checker.js +72 -60
- package/dist/architecture/cross-project-checker.js.map +1 -1
- package/dist/cli/commands/acceptance.d.ts.map +1 -1
- package/dist/cli/commands/acceptance.js +1 -0
- package/dist/cli/commands/acceptance.js.map +1 -1
- package/dist/cli/commands/check.d.ts.map +1 -1
- package/dist/cli/commands/check.js +2 -4
- package/dist/cli/commands/check.js.map +1 -1
- package/dist/cli/commands/command.d.ts +10 -3
- package/dist/cli/commands/command.d.ts.map +1 -1
- package/dist/cli/commands/command.js +7 -71
- package/dist/cli/commands/command.js.map +1 -1
- package/dist/cli/commands/index.d.ts +1 -1
- package/dist/cli/commands/index.d.ts.map +1 -1
- package/dist/cli/commands/index.js +2 -3
- package/dist/cli/commands/index.js.map +1 -1
- package/dist/cli/commands/passes-gate.d.ts.map +1 -1
- package/dist/cli/commands/passes-gate.js +2 -4
- package/dist/cli/commands/passes-gate.js.map +1 -1
- package/dist/cli/commands/review.d.ts.map +1 -1
- package/dist/cli/commands/review.js +27 -63
- package/dist/cli/commands/review.js.map +1 -1
- package/dist/cli/commands/security.d.ts +1 -1
- package/dist/cli/commands/security.d.ts.map +1 -1
- package/dist/cli/commands/security.js +40 -94
- package/dist/cli/commands/security.js.map +1 -1
- package/dist/cli/commands/status.d.ts.map +1 -1
- package/dist/cli/commands/status.js +2 -2
- package/dist/cli/commands/status.js.map +1 -1
- package/dist/constraints/definitions.d.ts +39 -0
- package/dist/constraints/definitions.d.ts.map +1 -0
- package/dist/constraints/definitions.js +71 -0
- package/dist/constraints/definitions.js.map +1 -0
- package/dist/constraints/index.d.ts +5 -0
- package/dist/constraints/index.d.ts.map +1 -0
- package/dist/constraints/index.js +21 -0
- package/dist/constraints/index.js.map +1 -0
- package/dist/constraints/quality.d.ts +11 -0
- package/dist/constraints/quality.d.ts.map +1 -0
- package/dist/constraints/quality.js +257 -0
- package/dist/constraints/quality.js.map +1 -0
- package/dist/constraints/registry.d.ts +64 -0
- package/dist/constraints/registry.d.ts.map +1 -0
- package/dist/constraints/registry.js +167 -0
- package/dist/constraints/registry.js.map +1 -0
- package/dist/constraints/safety.d.ts +13 -0
- package/dist/constraints/safety.d.ts.map +1 -0
- package/dist/constraints/safety.js +103 -0
- package/dist/constraints/safety.js.map +1 -0
- package/dist/constraints/types.d.ts +53 -0
- package/dist/constraints/types.d.ts.map +1 -0
- package/dist/constraints/types.js +8 -0
- package/dist/constraints/types.js.map +1 -0
- package/dist/context/compaction.d.ts +57 -0
- package/dist/context/compaction.d.ts.map +1 -0
- package/dist/context/compaction.js +166 -0
- package/dist/context/compaction.js.map +1 -0
- package/dist/context/file-budget.d.ts +40 -0
- package/dist/context/file-budget.d.ts.map +1 -0
- package/dist/context/file-budget.js +147 -0
- package/dist/context/file-budget.js.map +1 -0
- package/dist/context/index.d.ts +15 -0
- package/dist/context/index.d.ts.map +1 -0
- package/dist/context/index.js +31 -0
- package/dist/context/index.js.map +1 -0
- package/dist/context/knowledge-injector.d.ts +49 -0
- package/dist/context/knowledge-injector.d.ts.map +1 -0
- package/dist/context/knowledge-injector.js +122 -0
- package/dist/context/knowledge-injector.js.map +1 -0
- package/dist/context/progressive-loader.d.ts +0 -1
- package/dist/context/progressive-loader.d.ts.map +1 -1
- package/dist/context/progressive-loader.js +15 -32
- package/dist/context/progressive-loader.js.map +1 -1
- package/dist/context/session-manager.d.ts +67 -0
- package/dist/context/session-manager.d.ts.map +1 -0
- package/dist/context/session-manager.js +275 -0
- package/dist/context/session-manager.js.map +1 -0
- package/dist/context/token-pipeline.d.ts +55 -0
- package/dist/context/token-pipeline.d.ts.map +1 -0
- package/dist/context/token-pipeline.js +220 -0
- package/dist/context/token-pipeline.js.map +1 -0
- package/dist/context/tool-output-budget.d.ts +59 -0
- package/dist/context/tool-output-budget.d.ts.map +1 -0
- package/dist/context/tool-output-budget.js +177 -0
- package/dist/context/tool-output-budget.js.map +1 -0
- package/dist/context/types.d.ts +113 -0
- package/dist/context/types.d.ts.map +1 -0
- package/dist/context/types.js +30 -0
- package/dist/context/types.js.map +1 -0
- package/dist/core/constraints/checker.d.ts +9 -1
- package/dist/core/constraints/checker.d.ts.map +1 -1
- package/dist/core/constraints/checker.js +72 -155
- package/dist/core/constraints/checker.js.map +1 -1
- package/dist/core/constraints/definitions.d.ts.map +1 -1
- package/dist/core/constraints/definitions.js +8 -10
- package/dist/core/constraints/definitions.js.map +1 -1
- package/dist/core/constraints/interceptor.d.ts.map +1 -1
- package/dist/core/constraints/interceptor.js +2 -6
- package/dist/core/constraints/interceptor.js.map +1 -1
- package/dist/core/project-config-loader.d.ts.map +1 -1
- package/dist/core/project-config-loader.js +12 -30
- package/dist/core/project-config-loader.js.map +1 -1
- package/dist/core/session/clean-state.d.ts.map +1 -1
- package/dist/core/session/clean-state.js +6 -8
- package/dist/core/session/clean-state.js.map +1 -1
- package/dist/core/session/startup.d.ts.map +1 -1
- package/dist/core/session/startup.js +3 -5
- package/dist/core/session/startup.js.map +1 -1
- package/dist/core/spec/validator.d.ts.map +1 -1
- package/dist/core/spec/validator.js +2 -4
- package/dist/core/spec/validator.js.map +1 -1
- package/dist/core/validators/checkpoint.d.ts.map +1 -1
- package/dist/core/validators/checkpoint.js +3 -5
- package/dist/core/validators/checkpoint.js.map +1 -1
- package/dist/core/validators/passes-gate.d.ts +0 -4
- package/dist/core/validators/passes-gate.d.ts.map +1 -1
- package/dist/core/validators/passes-gate.js +5 -13
- package/dist/core/validators/passes-gate.js.map +1 -1
- package/dist/dashboard/data.d.ts +39 -0
- package/dist/dashboard/data.d.ts.map +1 -0
- package/dist/dashboard/data.js +89 -0
- package/dist/dashboard/data.js.map +1 -0
- package/dist/dashboard/index.d.ts +4 -0
- package/dist/dashboard/index.d.ts.map +1 -0
- package/dist/dashboard/index.js +20 -0
- package/dist/dashboard/index.js.map +1 -0
- package/dist/dashboard/stats.d.ts +20 -0
- package/dist/dashboard/stats.d.ts.map +1 -0
- package/dist/dashboard/stats.js +115 -0
- package/dist/dashboard/stats.js.map +1 -0
- package/dist/dashboard/types.d.ts +98 -0
- package/dist/dashboard/types.d.ts.map +1 -0
- package/dist/dashboard/types.js +8 -0
- package/dist/dashboard/types.js.map +1 -0
- package/dist/failure/recorder.d.ts +4 -0
- package/dist/failure/recorder.d.ts.map +1 -1
- package/dist/failure/recorder.js +9 -11
- package/dist/failure/recorder.js.map +1 -1
- package/dist/gates/acceptance.d.ts.map +1 -1
- package/dist/gates/acceptance.js +2 -4
- package/dist/gates/acceptance.js.map +1 -1
- package/dist/gates/contract.d.ts.map +1 -1
- package/dist/gates/contract.js +0 -3
- package/dist/gates/contract.js.map +1 -1
- package/dist/gates/performance.d.ts.map +1 -1
- package/dist/gates/performance.js +3 -5
- package/dist/gates/performance.js.map +1 -1
- package/dist/gates/review.d.ts.map +1 -1
- package/dist/gates/review.js +4 -6
- package/dist/gates/review.js.map +1 -1
- package/dist/gates/security.d.ts.map +1 -1
- package/dist/gates/security.js +2 -4
- package/dist/gates/security.js.map +1 -1
- package/dist/index.d.ts +9 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +34 -3
- package/dist/index.js.map +1 -1
- package/dist/knowledge/import.d.ts +95 -0
- package/dist/knowledge/import.d.ts.map +1 -0
- package/dist/knowledge/import.js +429 -0
- package/dist/knowledge/import.js.map +1 -0
- package/dist/knowledge/index.d.ts +15 -0
- package/dist/knowledge/index.d.ts.map +1 -0
- package/dist/knowledge/index.js +40 -0
- package/dist/knowledge/index.js.map +1 -0
- package/dist/knowledge/ingest.d.ts +30 -0
- package/dist/knowledge/ingest.d.ts.map +1 -0
- package/dist/knowledge/ingest.js +94 -0
- package/dist/knowledge/ingest.js.map +1 -0
- package/dist/knowledge/lifecycle-hooks.d.ts +62 -0
- package/dist/knowledge/lifecycle-hooks.d.ts.map +1 -0
- package/dist/knowledge/lifecycle-hooks.js +140 -0
- package/dist/knowledge/lifecycle-hooks.js.map +1 -0
- package/dist/knowledge/lifecycle.d.ts +43 -0
- package/dist/knowledge/lifecycle.d.ts.map +1 -0
- package/dist/knowledge/lifecycle.js +141 -0
- package/dist/knowledge/lifecycle.js.map +1 -0
- package/dist/knowledge/lint.d.ts +57 -0
- package/dist/knowledge/lint.d.ts.map +1 -0
- package/dist/knowledge/lint.js +235 -0
- package/dist/knowledge/lint.js.map +1 -0
- package/dist/knowledge/query.d.ts +33 -0
- package/dist/knowledge/query.d.ts.map +1 -0
- package/dist/knowledge/query.js +107 -0
- package/dist/knowledge/query.js.map +1 -0
- package/dist/knowledge/reference-tracker.d.ts +31 -0
- package/dist/knowledge/reference-tracker.d.ts.map +1 -0
- package/dist/knowledge/reference-tracker.js +124 -0
- package/dist/knowledge/reference-tracker.js.map +1 -0
- package/dist/knowledge/store.d.ts +42 -0
- package/dist/knowledge/store.d.ts.map +1 -0
- package/dist/knowledge/store.js +278 -0
- package/dist/knowledge/store.js.map +1 -0
- package/dist/knowledge/types.d.ts +103 -0
- package/dist/knowledge/types.d.ts.map +1 -0
- package/dist/knowledge/types.js +17 -0
- package/dist/knowledge/types.js.map +1 -0
- package/dist/llm/adapter.d.ts +36 -0
- package/dist/llm/adapter.d.ts.map +1 -0
- package/dist/llm/adapter.js +118 -0
- package/dist/llm/adapter.js.map +1 -0
- package/dist/llm/index.d.ts +3 -0
- package/dist/llm/index.d.ts.map +1 -0
- package/dist/llm/index.js +19 -0
- package/dist/llm/index.js.map +1 -0
- package/dist/llm/types.d.ts +62 -0
- package/dist/llm/types.d.ts.map +1 -0
- package/dist/llm/types.js +8 -0
- package/dist/llm/types.js.map +1 -0
- package/dist/monitoring/context-tracker.d.ts +38 -0
- package/dist/monitoring/context-tracker.d.ts.map +1 -0
- package/dist/monitoring/context-tracker.js +162 -0
- package/dist/monitoring/context-tracker.js.map +1 -0
- package/dist/monitoring/index.d.ts +3 -0
- package/dist/monitoring/index.d.ts.map +1 -1
- package/dist/monitoring/index.js +6 -0
- package/dist/monitoring/index.js.map +1 -1
- package/dist/monitoring/knowledge-doctor.d.ts +78 -0
- package/dist/monitoring/knowledge-doctor.d.ts.map +1 -0
- package/dist/monitoring/knowledge-doctor.js +266 -0
- package/dist/monitoring/knowledge-doctor.js.map +1 -0
- package/dist/monitoring/knowledge-evolver.d.ts +72 -0
- package/dist/monitoring/knowledge-evolver.d.ts.map +1 -0
- package/dist/monitoring/knowledge-evolver.js +205 -0
- package/dist/monitoring/knowledge-evolver.js.map +1 -0
- package/dist/monitoring/performance-analyzer.d.ts.map +1 -1
- package/dist/monitoring/performance-analyzer.js +10 -4
- package/dist/monitoring/performance-analyzer.js.map +1 -1
- package/dist/monitoring/trace-analyzer.d.ts.map +1 -1
- package/dist/monitoring/trace-analyzer.js +12 -5
- package/dist/monitoring/trace-analyzer.js.map +1 -1
- package/dist/safety/index.d.ts +6 -0
- package/dist/safety/index.d.ts.map +1 -0
- package/dist/safety/index.js +22 -0
- package/dist/safety/index.js.map +1 -0
- package/dist/safety/input-guardrail.d.ts +36 -0
- package/dist/safety/input-guardrail.d.ts.map +1 -0
- package/dist/safety/input-guardrail.js +118 -0
- package/dist/safety/input-guardrail.js.map +1 -0
- package/dist/safety/output-guardrail.d.ts +40 -0
- package/dist/safety/output-guardrail.d.ts.map +1 -0
- package/dist/safety/output-guardrail.js +150 -0
- package/dist/safety/output-guardrail.js.map +1 -0
- package/dist/safety/sandbox.d.ts +45 -0
- package/dist/safety/sandbox.d.ts.map +1 -0
- package/dist/safety/sandbox.js +134 -0
- package/dist/safety/sandbox.js.map +1 -0
- package/dist/safety/tool-guardrail.d.ts +51 -0
- package/dist/safety/tool-guardrail.d.ts.map +1 -0
- package/dist/safety/tool-guardrail.js +147 -0
- package/dist/safety/tool-guardrail.js.map +1 -0
- package/dist/safety/types.d.ts +104 -0
- package/dist/safety/types.d.ts.map +1 -0
- package/dist/safety/types.js +6 -0
- package/dist/safety/types.js.map +1 -0
- package/dist/tools/core/index.d.ts +13 -0
- package/dist/tools/core/index.d.ts.map +1 -0
- package/dist/tools/core/index.js +198 -0
- package/dist/tools/core/index.js.map +1 -0
- package/dist/tools/index.d.ts +4 -0
- package/dist/tools/index.d.ts.map +1 -0
- package/dist/tools/index.js +20 -0
- package/dist/tools/index.js.map +1 -0
- package/dist/tools/registry.d.ts +59 -0
- package/dist/tools/registry.d.ts.map +1 -0
- package/dist/tools/registry.js +115 -0
- package/dist/tools/registry.js.map +1 -0
- package/dist/tools/types.d.ts +38 -0
- package/dist/tools/types.d.ts.map +1 -0
- package/dist/tools/types.js +6 -0
- package/dist/tools/types.js.map +1 -0
- package/dist/types/constraint.d.ts +4 -0
- package/dist/types/constraint.d.ts.map +1 -1
- package/dist/types/constraint.js.map +1 -1
- package/dist/utils/exec.d.ts +11 -1
- package/dist/utils/exec.d.ts.map +1 -1
- package/dist/utils/exec.js +19 -1
- package/dist/utils/exec.js.map +1 -1
- package/dist/verification/index.d.ts +4 -0
- package/dist/verification/index.d.ts.map +1 -0
- package/dist/verification/index.js +20 -0
- package/dist/verification/index.js.map +1 -0
- package/dist/verification/loop.d.ts +30 -0
- package/dist/verification/loop.d.ts.map +1 -0
- package/dist/verification/loop.js +114 -0
- package/dist/verification/loop.js.map +1 -0
- package/dist/verification/rules-based.d.ts +40 -0
- package/dist/verification/rules-based.d.ts.map +1 -0
- package/dist/verification/rules-based.js +130 -0
- package/dist/verification/rules-based.js.map +1 -0
- package/dist/verification/types.d.ts +69 -0
- package/dist/verification/types.d.ts.map +1 -0
- package/dist/verification/types.js +6 -0
- package/dist/verification/types.js.map +1 -0
- package/package.json +1 -1
- package/dist/cli/commands/diagnose.d.ts +0 -16
- package/dist/cli/commands/diagnose.d.ts.map +0 -1
- package/dist/cli/commands/diagnose.js +0 -195
- package/dist/cli/commands/diagnose.js.map +0 -1
- package/dist/cli/commands/propose.d.ts +0 -18
- package/dist/cli/commands/propose.d.ts.map +0 -1
- package/dist/cli/commands/propose.js +0 -331
- package/dist/cli/commands/propose.js.map +0 -1
- package/dist/cli/commands/traces.d.ts +0 -15
- package/dist/cli/commands/traces.d.ts.map +0 -1
- package/dist/cli/commands/traces.js +0 -167
- package/dist/cli/commands/traces.js.map +0 -1
- package/dist/extensions/long-running/constraints.d.ts +0 -38
- package/dist/extensions/long-running/constraints.d.ts.map +0 -1
- package/dist/extensions/long-running/constraints.js +0 -153
- package/dist/extensions/long-running/constraints.js.map +0 -1
- package/dist/extensions/long-running/index.d.ts +0 -30
- package/dist/extensions/long-running/index.d.ts.map +0 -1
- package/dist/extensions/long-running/index.js +0 -50
- package/dist/extensions/long-running/index.js.map +0 -1
- package/dist/extensions/long-running/types.d.ts +0 -154
- package/dist/extensions/long-running/types.d.ts.map +0 -1
- package/dist/extensions/long-running/types.js +0 -9
- package/dist/extensions/long-running/types.js.map +0 -1
- package/dist/presets/long-running.d.ts +0 -33
- package/dist/presets/long-running.d.ts.map +0 -1
- package/dist/presets/long-running.js +0 -52
- package/dist/presets/long-running.js.map +0 -1
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Token 流水线
|
|
3
|
+
*
|
|
4
|
+
* 五步:收集 → 排序 → 压缩 → 预算 → 组装
|
|
5
|
+
* 每次 LLM 调用前运行
|
|
6
|
+
*/
|
|
7
|
+
import type { ContextSource, TokenBudgetAllocation, PipelineInput, PipelineOutput } from './types';
|
|
8
|
+
interface CollectedGroup {
|
|
9
|
+
priority: number;
|
|
10
|
+
sources: ContextSource[];
|
|
11
|
+
}
|
|
12
|
+
export declare class TokenPipeline {
|
|
13
|
+
/**
|
|
14
|
+
* 完整流水线:收集 → 排序 → 压缩 → 预算 → 组装
|
|
15
|
+
*/
|
|
16
|
+
run(input: PipelineInput): PipelineOutput;
|
|
17
|
+
/**
|
|
18
|
+
* Step 1: 收集 — 从 sources 按 priority 分组
|
|
19
|
+
*/
|
|
20
|
+
collect(sources: ContextSource[]): CollectedGroup[];
|
|
21
|
+
/**
|
|
22
|
+
* Step 2: 排序 — 按 P1→P6,组内保持原序
|
|
23
|
+
*/
|
|
24
|
+
sort(groups: CollectedGroup[]): ContextSource[];
|
|
25
|
+
/**
|
|
26
|
+
* Step 3: 压缩 — 对 P3-P5 应用 token 预算裁剪
|
|
27
|
+
*
|
|
28
|
+
* P1(系统提示词)和 P6(用户消息)不压缩
|
|
29
|
+
* P2(工具定义)按需懒加载,这里不处理
|
|
30
|
+
*/
|
|
31
|
+
compress(sorted: ContextSource[], budget: TokenBudgetAllocation): {
|
|
32
|
+
compressed: ContextSource[];
|
|
33
|
+
dropped: Array<{
|
|
34
|
+
type: string;
|
|
35
|
+
id: string;
|
|
36
|
+
reason: string;
|
|
37
|
+
}>;
|
|
38
|
+
};
|
|
39
|
+
/**
|
|
40
|
+
* Step 4: 预算 — 按 TokenBudgetAllocation 分配各段
|
|
41
|
+
*/
|
|
42
|
+
applyBudget(compressed: ContextSource[], budget: TokenBudgetAllocation): ContextSource[];
|
|
43
|
+
/**
|
|
44
|
+
* Step 5: 组装 — 拼装最终 prompt
|
|
45
|
+
*
|
|
46
|
+
* P1 开头 + P6 结尾,中间 P2-P5
|
|
47
|
+
*/
|
|
48
|
+
assemble(budgeted: ContextSource[]): string;
|
|
49
|
+
/**
|
|
50
|
+
* 构建上下文使用快照
|
|
51
|
+
*/
|
|
52
|
+
private buildSnapshot;
|
|
53
|
+
}
|
|
54
|
+
export {};
|
|
55
|
+
//# sourceMappingURL=token-pipeline.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"token-pipeline.d.ts","sourceRoot":"","sources":["../../src/context/token-pipeline.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAGH,OAAO,KAAK,EACV,aAAa,EACb,qBAAqB,EACrB,aAAa,EACb,cAAc,EAEf,MAAM,SAAS,CAAC;AAEjB,UAAU,cAAc;IACtB,QAAQ,EAAE,MAAM,CAAC;IACjB,OAAO,EAAE,aAAa,EAAE,CAAC;CAC1B;AAED,qBAAa,aAAa;IACxB;;OAEG;IACH,GAAG,CAAC,KAAK,EAAE,aAAa,GAAG,cAAc;IAwBzC;;OAEG;IACH,OAAO,CAAC,OAAO,EAAE,aAAa,EAAE,GAAG,cAAc,EAAE;IAcnD;;OAEG;IACH,IAAI,CAAC,MAAM,EAAE,cAAc,EAAE,GAAG,aAAa,EAAE;IAU/C;;;;;OAKG;IACH,QAAQ,CAAC,MAAM,EAAE,aAAa,EAAE,EAAE,MAAM,EAAE,qBAAqB,GAAG;QAChE,UAAU,EAAE,aAAa,EAAE,CAAC;QAC5B,OAAO,EAAE,KAAK,CAAC;YAAE,IAAI,EAAE,MAAM,CAAC;YAAC,EAAE,EAAE,MAAM,CAAC;YAAC,MAAM,EAAE,MAAM,CAAA;SAAE,CAAC,CAAC;KAC9D;IAkED;;OAEG;IACH,WAAW,CAAC,UAAU,EAAE,aAAa,EAAE,EAAE,MAAM,EAAE,qBAAqB,GAAG,aAAa,EAAE;IAqBxF;;;;OAIG;IACH,QAAQ,CAAC,QAAQ,EAAE,aAAa,EAAE,GAAG,MAAM;IAgC3C;;OAEG;IACH,OAAO,CAAC,aAAa;CA8CtB"}
|
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Token 流水线
|
|
4
|
+
*
|
|
5
|
+
* 五步:收集 → 排序 → 压缩 → 预算 → 组装
|
|
6
|
+
* 每次 LLM 调用前运行
|
|
7
|
+
*/
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.TokenPipeline = void 0;
|
|
10
|
+
const token_budget_1 = require("./token-budget");
|
|
11
|
+
class TokenPipeline {
|
|
12
|
+
/**
|
|
13
|
+
* 完整流水线:收集 → 排序 → 压缩 → 预算 → 组装
|
|
14
|
+
*/
|
|
15
|
+
run(input) {
|
|
16
|
+
const { sources, budget } = input;
|
|
17
|
+
// Step 1: 收集 — 按 priority 分组
|
|
18
|
+
const collected = this.collect(sources);
|
|
19
|
+
// Step 2: 排序 — 按 P1→P6 排序
|
|
20
|
+
const sorted = this.sort(collected);
|
|
21
|
+
// Step 3: 压缩 — 对 P3-P5 应用预算裁剪
|
|
22
|
+
const { compressed, dropped } = this.compress(sorted, budget);
|
|
23
|
+
// Step 4: 预算 — 按分配裁剪
|
|
24
|
+
const budgeted = this.applyBudget(compressed, budget);
|
|
25
|
+
// Step 5: 组装 — 拼装最终 prompt
|
|
26
|
+
const prompt = this.assemble(budgeted);
|
|
27
|
+
// 生成快照
|
|
28
|
+
const snapshot = this.buildSnapshot(budgeted, dropped);
|
|
29
|
+
return { prompt, snapshot, dropped };
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Step 1: 收集 — 从 sources 按 priority 分组
|
|
33
|
+
*/
|
|
34
|
+
collect(sources) {
|
|
35
|
+
const groups = new Map();
|
|
36
|
+
for (const source of sources) {
|
|
37
|
+
const existing = groups.get(source.priority) || [];
|
|
38
|
+
existing.push(source);
|
|
39
|
+
groups.set(source.priority, existing);
|
|
40
|
+
}
|
|
41
|
+
return Array.from(groups.entries())
|
|
42
|
+
.map(([priority, sources]) => ({ priority, sources }))
|
|
43
|
+
.sort((a, b) => a.priority - b.priority);
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Step 2: 排序 — 按 P1→P6,组内保持原序
|
|
47
|
+
*/
|
|
48
|
+
sort(groups) {
|
|
49
|
+
const sorted = [];
|
|
50
|
+
for (const group of groups) {
|
|
51
|
+
sorted.push(...group.sources);
|
|
52
|
+
}
|
|
53
|
+
return sorted;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Step 3: 压缩 — 对 P3-P5 应用 token 预算裁剪
|
|
57
|
+
*
|
|
58
|
+
* P1(系统提示词)和 P6(用户消息)不压缩
|
|
59
|
+
* P2(工具定义)按需懒加载,这里不处理
|
|
60
|
+
*/
|
|
61
|
+
compress(sorted, budget) {
|
|
62
|
+
const compressed = [];
|
|
63
|
+
const dropped = [];
|
|
64
|
+
// 各段预算
|
|
65
|
+
const budgetMap = {
|
|
66
|
+
1: budget.systemPrompt,
|
|
67
|
+
2: budget.toolDefinitions,
|
|
68
|
+
3: budget.knowledge,
|
|
69
|
+
4: budget.notes,
|
|
70
|
+
5: budget.history,
|
|
71
|
+
6: Infinity, // 用户消息不限
|
|
72
|
+
};
|
|
73
|
+
// 按优先级分组处理
|
|
74
|
+
const byPriority = new Map();
|
|
75
|
+
for (const item of sorted) {
|
|
76
|
+
const existing = byPriority.get(item.priority) || [];
|
|
77
|
+
existing.push(item);
|
|
78
|
+
byPriority.set(item.priority, existing);
|
|
79
|
+
}
|
|
80
|
+
for (const [priority, items] of byPriority) {
|
|
81
|
+
const sectionBudget = budgetMap[priority] || budget.history;
|
|
82
|
+
if (priority === 1 || priority === 6) {
|
|
83
|
+
// P1/P6 不压缩
|
|
84
|
+
compressed.push(...items);
|
|
85
|
+
continue;
|
|
86
|
+
}
|
|
87
|
+
// 按 token 预算裁剪
|
|
88
|
+
let usedTokens = 0;
|
|
89
|
+
for (const item of items) {
|
|
90
|
+
const tokens = token_budget_1.TokenEstimator.estimateText(item.content);
|
|
91
|
+
if (usedTokens + tokens <= sectionBudget) {
|
|
92
|
+
compressed.push(item);
|
|
93
|
+
usedTokens += tokens;
|
|
94
|
+
}
|
|
95
|
+
else {
|
|
96
|
+
// 尝试截断
|
|
97
|
+
const remaining = sectionBudget - usedTokens;
|
|
98
|
+
if (remaining > 50) {
|
|
99
|
+
const ratio = remaining / tokens;
|
|
100
|
+
const truncatedContent = item.content.slice(0, Math.floor(item.content.length * ratio));
|
|
101
|
+
compressed.push({ ...item, content: truncatedContent });
|
|
102
|
+
dropped.push({
|
|
103
|
+
type: item.type,
|
|
104
|
+
id: item.id,
|
|
105
|
+
reason: `截断: ${tokens} → ${Math.floor(tokens * ratio)} tokens`,
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
else {
|
|
109
|
+
dropped.push({
|
|
110
|
+
type: item.type,
|
|
111
|
+
id: item.id,
|
|
112
|
+
reason: `超出预算: 需要 ${tokens} tokens,剩余 ${remaining}`,
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
usedTokens += tokens; // 仍然计入已用
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
return { compressed, dropped };
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Step 4: 预算 — 按 TokenBudgetAllocation 分配各段
|
|
123
|
+
*/
|
|
124
|
+
applyBudget(compressed, budget) {
|
|
125
|
+
const tokenBudget = new token_budget_1.TokenBudget(budget.total);
|
|
126
|
+
const result = [];
|
|
127
|
+
for (const item of compressed) {
|
|
128
|
+
const tokens = token_budget_1.TokenEstimator.estimateText(item.content);
|
|
129
|
+
if (tokenBudget.canAfford(tokens)) {
|
|
130
|
+
tokenBudget.consume(tokens);
|
|
131
|
+
result.push(item);
|
|
132
|
+
}
|
|
133
|
+
else if (item.priority === 1 || item.priority === 6) {
|
|
134
|
+
// P1/P6 强制保留
|
|
135
|
+
tokenBudget.forceConsume(tokens);
|
|
136
|
+
result.push(item);
|
|
137
|
+
}
|
|
138
|
+
// 其他优先级的超预算项被丢弃
|
|
139
|
+
}
|
|
140
|
+
return result;
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Step 5: 组装 — 拼装最终 prompt
|
|
144
|
+
*
|
|
145
|
+
* P1 开头 + P6 结尾,中间 P2-P5
|
|
146
|
+
*/
|
|
147
|
+
assemble(budgeted) {
|
|
148
|
+
const sections = {
|
|
149
|
+
1: [], 2: [], 3: [], 4: [], 5: [], 6: [],
|
|
150
|
+
};
|
|
151
|
+
for (const item of budgeted) {
|
|
152
|
+
sections[item.priority] = sections[item.priority] || [];
|
|
153
|
+
sections[item.priority].push(item.content);
|
|
154
|
+
}
|
|
155
|
+
const parts = [];
|
|
156
|
+
// P1: 系统提示词(开头)
|
|
157
|
+
if (sections[1].length > 0) {
|
|
158
|
+
parts.push(sections[1].join('\n'));
|
|
159
|
+
}
|
|
160
|
+
// P2-P5: 中间部分
|
|
161
|
+
for (const priority of [2, 3, 4, 5]) {
|
|
162
|
+
if (sections[priority].length > 0) {
|
|
163
|
+
parts.push(sections[priority].join('\n'));
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
// P6: 用户消息(结尾)
|
|
167
|
+
if (sections[6].length > 0) {
|
|
168
|
+
parts.push(sections[6].join('\n'));
|
|
169
|
+
}
|
|
170
|
+
return parts.join('\n\n');
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* 构建上下文使用快照
|
|
174
|
+
*/
|
|
175
|
+
buildSnapshot(budgeted, dropped) {
|
|
176
|
+
const breakdown = {
|
|
177
|
+
systemPrompt: 0,
|
|
178
|
+
messages: 0,
|
|
179
|
+
toolOutputs: 0,
|
|
180
|
+
knowledge: 0,
|
|
181
|
+
other: 0,
|
|
182
|
+
};
|
|
183
|
+
for (const item of budgeted) {
|
|
184
|
+
const tokens = token_budget_1.TokenEstimator.estimateText(item.content);
|
|
185
|
+
switch (item.type) {
|
|
186
|
+
case 'system_prompt':
|
|
187
|
+
breakdown.systemPrompt += tokens;
|
|
188
|
+
break;
|
|
189
|
+
case 'user_message':
|
|
190
|
+
case 'session_event':
|
|
191
|
+
breakdown.messages += tokens;
|
|
192
|
+
break;
|
|
193
|
+
case 'tool_output':
|
|
194
|
+
case 'tool_definition':
|
|
195
|
+
breakdown.toolOutputs += tokens;
|
|
196
|
+
break;
|
|
197
|
+
case 'knowledge':
|
|
198
|
+
breakdown.knowledge += tokens;
|
|
199
|
+
break;
|
|
200
|
+
default:
|
|
201
|
+
breakdown.other += tokens;
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
return {
|
|
205
|
+
timestamp: new Date().toISOString(),
|
|
206
|
+
totalTokens: breakdown.systemPrompt + breakdown.messages + breakdown.toolOutputs + breakdown.knowledge + breakdown.other,
|
|
207
|
+
breakdown,
|
|
208
|
+
truncatedItems: dropped.filter(d => d.reason.startsWith('截断')).map(d => ({
|
|
209
|
+
type: d.type,
|
|
210
|
+
id: d.id,
|
|
211
|
+
originalTokens: 0,
|
|
212
|
+
keptTokens: 0,
|
|
213
|
+
})),
|
|
214
|
+
offloadedItems: [],
|
|
215
|
+
compactionTriggered: false,
|
|
216
|
+
};
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
exports.TokenPipeline = TokenPipeline;
|
|
220
|
+
//# sourceMappingURL=token-pipeline.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"token-pipeline.js","sourceRoot":"","sources":["../../src/context/token-pipeline.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;AAEH,iDAA6D;AAc7D,MAAa,aAAa;IACxB;;OAEG;IACH,GAAG,CAAC,KAAoB;QACtB,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,GAAG,KAAK,CAAC;QAElC,6BAA6B;QAC7B,MAAM,SAAS,GAAG,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;QAExC,0BAA0B;QAC1B,MAAM,MAAM,GAAG,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;QAEpC,8BAA8B;QAC9B,MAAM,EAAE,UAAU,EAAE,OAAO,EAAE,GAAG,IAAI,CAAC,QAAQ,CAAC,MAAM,EAAE,MAAM,CA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,SAAS,CAAC,QAAQ,IAAI,MAAM,CAAC;oBAC7B,MAAM;gBACR,KAAK,aAAa,CAAC;gBACnB,KAAK,iBAAiB;oBACpB,SAAS,CAAC,WAAW,IAAI,MAAM,CAAC;oBAChC,MAAM;gBACR,KAAK,WAAW;oBACd,SAAS,CAAC,SAAS,IAAI,MAAM,CAAC;oBAC9B,MAAM;gBACR;oBACE,SAAS,CAAC,KAAK,IAAI,MAAM,CAAC;YAC9B,CAAC;QACH,CAAC;QAED,OAAO;YACL,SAAS,EAAE,IAAI,IAAI,EAAE,CAAC,WAAW,EAAE;YACnC,WAAW,EAAE,SAAS,CAAC,YAAY,GAAG,SAAS,CAAC,QAAQ,GAAG,SAAS,CAAC,WAAW,GAAG,SAAS,CAAC,SAAS,GAAG,SAAS,CAAC,KAAK;YACxH,SAAS;YACT,cAAc,EAAE,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,MAAM,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC;gBACvE,IAAI,EAAE,CAAC,CAAC,IAAI;gBACZ,EAAE,EAAE,CAAC,CAAC,EAAE;gBACR,cAAc,EAAE,CAAC;gBACjB,UAAU,EAAE,CAAC;aACd,CAAC,CAAC;YACH,cAAc,EAAE,EAAE;YAClB,mBAAmB,EAAE,KAAK;SAC3B,CAAC;IACJ,CAAC;CACF;AAnPD,sCAmPC"}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 工具输出预算
|
|
3
|
+
*
|
|
4
|
+
* 控制工具输出对上下文的占用
|
|
5
|
+
* 策略:full / preview / overflow / dedup
|
|
6
|
+
*/
|
|
7
|
+
import type { ToolOutputBudgetConfig } from './types';
|
|
8
|
+
export type OutputStrategy = 'full' | 'preview' | 'overflow' | 'dedup';
|
|
9
|
+
export interface OutputCheckResult {
|
|
10
|
+
allowed: boolean;
|
|
11
|
+
strategy: OutputStrategy;
|
|
12
|
+
reason?: string;
|
|
13
|
+
}
|
|
14
|
+
export interface OutputApplyResult {
|
|
15
|
+
content: string;
|
|
16
|
+
offloaded?: {
|
|
17
|
+
path: string;
|
|
18
|
+
};
|
|
19
|
+
originalTokens: number;
|
|
20
|
+
keptTokens: number;
|
|
21
|
+
}
|
|
22
|
+
export declare class ToolOutputBudget {
|
|
23
|
+
private config;
|
|
24
|
+
private seenHashes;
|
|
25
|
+
constructor(config?: Partial<ToolOutputBudgetConfig>);
|
|
26
|
+
/**
|
|
27
|
+
* 检查工具输出应该使用什么策略
|
|
28
|
+
*/
|
|
29
|
+
checkOutput(output: string, totalBudget?: number): OutputCheckResult;
|
|
30
|
+
/**
|
|
31
|
+
* 应用预算策略
|
|
32
|
+
*/
|
|
33
|
+
applyBudget(output: string, strategy: OutputStrategy, totalBudget?: number): OutputApplyResult;
|
|
34
|
+
/**
|
|
35
|
+
* 生成预览内容
|
|
36
|
+
*/
|
|
37
|
+
private generatePreview;
|
|
38
|
+
/**
|
|
39
|
+
* 写入溢出文件
|
|
40
|
+
*/
|
|
41
|
+
private writeToOverflow;
|
|
42
|
+
/**
|
|
43
|
+
* 计算内容 hash
|
|
44
|
+
*/
|
|
45
|
+
private computeHash;
|
|
46
|
+
/**
|
|
47
|
+
* 记录已见 hash
|
|
48
|
+
*/
|
|
49
|
+
private recordHash;
|
|
50
|
+
/**
|
|
51
|
+
* 清除去重缓存
|
|
52
|
+
*/
|
|
53
|
+
clearDedupCache(): void;
|
|
54
|
+
/**
|
|
55
|
+
* 获取配置
|
|
56
|
+
*/
|
|
57
|
+
getConfig(): ToolOutputBudgetConfig;
|
|
58
|
+
}
|
|
59
|
+
//# sourceMappingURL=tool-output-budget.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tool-output-budget.d.ts","sourceRoot":"","sources":["../../src/context/tool-output-budget.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAIH,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,SAAS,CAAC;AAGtD,MAAM,MAAM,cAAc,GAAG,MAAM,GAAG,SAAS,GAAG,UAAU,GAAG,OAAO,CAAC;AAEvE,MAAM,WAAW,iBAAiB;IAChC,OAAO,EAAE,OAAO,CAAC;IACjB,QAAQ,EAAE,cAAc,CAAC;IACzB,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB;AAED,MAAM,WAAW,iBAAiB;IAChC,OAAO,EAAE,MAAM,CAAC;IAChB,SAAS,CAAC,EAAE;QAAE,IAAI,EAAE,MAAM,CAAA;KAAE,CAAC;IAC7B,cAAc,EAAE,MAAM,CAAC;IACvB,UAAU,EAAE,MAAM,CAAC;CACpB;AAED,qBAAa,gBAAgB;IAC3B,OAAO,CAAC,MAAM,CAAyB;IACvC,OAAO,CAAC,UAAU,CAAmC;gBAEzC,MAAM,CAAC,EAAE,OAAO,CAAC,sBAAsB,CAAC;IAIpD;;OAEG;IACH,WAAW,CAAC,MAAM,EAAE,MAAM,EAAE,WAAW,CAAC,EAAE,MAAM,GAAG,iBAAiB;IAsCpE;;OAEG;IACH,WAAW,CAAC,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,cAAc,EAAE,WAAW,CAAC,EAAE,MAAM,GAAG,iBAAiB;IAkC9F;;OAEG;IACH,OAAO,CAAC,eAAe;IAavB;;OAEG;IACH,OAAO,CAAC,eAAe;IAiBvB;;OAEG;IACH,OAAO,CAAC,WAAW;IAInB;;OAEG;IACH,OAAO,CAAC,UAAU;IAKlB;;OAEG;IACH,eAAe,IAAI,IAAI;IAIvB;;OAEG;IACH,SAAS,IAAI,sBAAsB;CAGpC"}
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* 工具输出预算
|
|
4
|
+
*
|
|
5
|
+
* 控制工具输出对上下文的占用
|
|
6
|
+
* 策略:full / preview / overflow / dedup
|
|
7
|
+
*/
|
|
8
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
9
|
+
if (k2 === undefined) k2 = k;
|
|
10
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
11
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
12
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
13
|
+
}
|
|
14
|
+
Object.defineProperty(o, k2, desc);
|
|
15
|
+
}) : (function(o, m, k, k2) {
|
|
16
|
+
if (k2 === undefined) k2 = k;
|
|
17
|
+
o[k2] = m[k];
|
|
18
|
+
}));
|
|
19
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
20
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
21
|
+
}) : function(o, v) {
|
|
22
|
+
o["default"] = v;
|
|
23
|
+
});
|
|
24
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
25
|
+
var ownKeys = function(o) {
|
|
26
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
27
|
+
var ar = [];
|
|
28
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
29
|
+
return ar;
|
|
30
|
+
};
|
|
31
|
+
return ownKeys(o);
|
|
32
|
+
};
|
|
33
|
+
return function (mod) {
|
|
34
|
+
if (mod && mod.__esModule) return mod;
|
|
35
|
+
var result = {};
|
|
36
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
37
|
+
__setModuleDefault(result, mod);
|
|
38
|
+
return result;
|
|
39
|
+
};
|
|
40
|
+
})();
|
|
41
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
42
|
+
exports.ToolOutputBudget = void 0;
|
|
43
|
+
const crypto = __importStar(require("crypto"));
|
|
44
|
+
const token_budget_1 = require("./token-budget");
|
|
45
|
+
const types_1 = require("./types");
|
|
46
|
+
class ToolOutputBudget {
|
|
47
|
+
config;
|
|
48
|
+
seenHashes = new Map();
|
|
49
|
+
constructor(config) {
|
|
50
|
+
this.config = { ...types_1.DEFAULT_TOOL_OUTPUT_BUDGET, ...config };
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* 检查工具输出应该使用什么策略
|
|
54
|
+
*/
|
|
55
|
+
checkOutput(output, totalBudget) {
|
|
56
|
+
const charCount = output.length;
|
|
57
|
+
const tokens = token_budget_1.TokenEstimator.estimateText(output);
|
|
58
|
+
// 去重检查
|
|
59
|
+
if (this.config.dedup) {
|
|
60
|
+
const hash = this.computeHash(output);
|
|
61
|
+
if (this.seenHashes.has(hash)) {
|
|
62
|
+
return { allowed: true, strategy: 'dedup', reason: '重复内容,将被去重' };
|
|
63
|
+
}
|
|
64
|
+
// 记录 hash(无论最终策略是什么)
|
|
65
|
+
this.seenHashes.set(hash, true);
|
|
66
|
+
}
|
|
67
|
+
// 硬上限检查
|
|
68
|
+
if (charCount > this.config.maxChars) {
|
|
69
|
+
return {
|
|
70
|
+
allowed: true,
|
|
71
|
+
strategy: this.config.overflowToDisk ? 'overflow' : 'preview',
|
|
72
|
+
reason: `输出 ${charCount} 字符超过限制 ${this.config.maxChars}`,
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
// Token 比例检查
|
|
76
|
+
if (totalBudget) {
|
|
77
|
+
const maxTokens = totalBudget * this.config.maxTokenRatio;
|
|
78
|
+
if (tokens > maxTokens) {
|
|
79
|
+
return {
|
|
80
|
+
allowed: true,
|
|
81
|
+
strategy: 'preview',
|
|
82
|
+
reason: `输出 ${tokens} tokens 超过预算比例 ${(this.config.maxTokenRatio * 100).toFixed(0)}% (${Math.floor(maxTokens)} tokens)`,
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
return { allowed: true, strategy: 'full' };
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* 应用预算策略
|
|
90
|
+
*/
|
|
91
|
+
applyBudget(output, strategy, totalBudget) {
|
|
92
|
+
const originalTokens = token_budget_1.TokenEstimator.estimateText(output);
|
|
93
|
+
switch (strategy) {
|
|
94
|
+
case 'full': {
|
|
95
|
+
if (this.config.dedup) {
|
|
96
|
+
this.recordHash(output);
|
|
97
|
+
}
|
|
98
|
+
return { content: output, originalTokens, keptTokens: originalTokens };
|
|
99
|
+
}
|
|
100
|
+
case 'preview': {
|
|
101
|
+
const preview = this.generatePreview(output);
|
|
102
|
+
const keptTokens = token_budget_1.TokenEstimator.estimateText(preview);
|
|
103
|
+
return { content: preview, originalTokens, keptTokens };
|
|
104
|
+
}
|
|
105
|
+
case 'overflow': {
|
|
106
|
+
const path = this.writeToOverflow(output);
|
|
107
|
+
const summary = `[输出已写入磁盘: ${path}]\n前 ${this.config.previewLines} 行预览:\n${output.split('\n').slice(0, this.config.previewLines).join('\n')}`;
|
|
108
|
+
const keptTokens = token_budget_1.TokenEstimator.estimateText(summary);
|
|
109
|
+
return { content: summary, offloaded: { path }, originalTokens, keptTokens };
|
|
110
|
+
}
|
|
111
|
+
case 'dedup': {
|
|
112
|
+
const dedupContent = `[重复内容,已省略。原始大小: ${output.length} 字符]`;
|
|
113
|
+
return { content: dedupContent, originalTokens, keptTokens: token_budget_1.TokenEstimator.estimateText(dedupContent) };
|
|
114
|
+
}
|
|
115
|
+
default:
|
|
116
|
+
return { content: output, originalTokens, keptTokens: originalTokens };
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* 生成预览内容
|
|
121
|
+
*/
|
|
122
|
+
generatePreview(output) {
|
|
123
|
+
const lines = output.split('\n');
|
|
124
|
+
const previewLines = this.config.previewLines;
|
|
125
|
+
if (lines.length <= previewLines) {
|
|
126
|
+
return output;
|
|
127
|
+
}
|
|
128
|
+
const head = lines.slice(0, previewLines).join('\n');
|
|
129
|
+
const omitted = lines.length - previewLines;
|
|
130
|
+
return `${head}\n\n... [省略 ${omitted} 行] ...\n\n${lines.slice(-3).join('\n')}`;
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* 写入溢出文件
|
|
134
|
+
*/
|
|
135
|
+
writeToOverflow(output) {
|
|
136
|
+
const hash = this.computeHash(output).slice(0, 8);
|
|
137
|
+
const path = `.harness/overflow/tool-output-${hash}.txt`;
|
|
138
|
+
try {
|
|
139
|
+
const fs = require('fs');
|
|
140
|
+
const dir = require('path').dirname(path);
|
|
141
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
142
|
+
fs.writeFileSync(path, output, 'utf-8');
|
|
143
|
+
}
|
|
144
|
+
catch {
|
|
145
|
+
// 写入失败时回退到 preview
|
|
146
|
+
return '';
|
|
147
|
+
}
|
|
148
|
+
return path;
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* 计算内容 hash
|
|
152
|
+
*/
|
|
153
|
+
computeHash(content) {
|
|
154
|
+
return crypto.createHash('md5').update(content).digest('hex');
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* 记录已见 hash
|
|
158
|
+
*/
|
|
159
|
+
recordHash(content) {
|
|
160
|
+
const hash = this.computeHash(content);
|
|
161
|
+
this.seenHashes.set(hash, true);
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* 清除去重缓存
|
|
165
|
+
*/
|
|
166
|
+
clearDedupCache() {
|
|
167
|
+
this.seenHashes.clear();
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* 获取配置
|
|
171
|
+
*/
|
|
172
|
+
getConfig() {
|
|
173
|
+
return { ...this.config };
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
exports.ToolOutputBudget = ToolOutputBudget;
|
|
177
|
+
//# sourceMappingURL=tool-output-budget.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tool-output-budget.js","sourceRoot":"","sources":["../../src/context/tool-output-budget.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;A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|
|
1
|
+
/**
|
|
2
|
+
* 上下文管理类型定义
|
|
3
|
+
*
|
|
4
|
+
* Phase 2: Token 流水线 + 预算 + 压缩
|
|
5
|
+
*/
|
|
6
|
+
import type { QueryResult } from '../knowledge/types';
|
|
7
|
+
export interface FileBudgetConfig {
|
|
8
|
+
maxLines: number;
|
|
9
|
+
maxBytes: number;
|
|
10
|
+
maxTokenEstimate: number;
|
|
11
|
+
continuationHint: boolean;
|
|
12
|
+
}
|
|
13
|
+
export declare const DEFAULT_FILE_BUDGET: FileBudgetConfig;
|
|
14
|
+
export interface ToolOutputBudgetConfig {
|
|
15
|
+
maxChars: number;
|
|
16
|
+
maxTokenRatio: number;
|
|
17
|
+
previewLines: number;
|
|
18
|
+
overflowToDisk: boolean;
|
|
19
|
+
dedup: boolean;
|
|
20
|
+
}
|
|
21
|
+
export declare const DEFAULT_TOOL_OUTPUT_BUDGET: ToolOutputBudgetConfig;
|
|
22
|
+
export type CompactionLevel = 'eviction' | 'summary' | 'checkpoint';
|
|
23
|
+
export interface CompactionConfig {
|
|
24
|
+
triggerRatio: number;
|
|
25
|
+
level: CompactionLevel;
|
|
26
|
+
preserveToolCallPairs: boolean;
|
|
27
|
+
structuredSummary: boolean;
|
|
28
|
+
maxSummaryTokens: number;
|
|
29
|
+
fallbackStrategy: 'truncate-middle' | 'head-drop' | 'retry-with-clamp';
|
|
30
|
+
}
|
|
31
|
+
export declare const DEFAULT_COMPACTION_CONFIG: CompactionConfig;
|
|
32
|
+
export interface SessionMessage {
|
|
33
|
+
role: 'user' | 'assistant' | 'tool';
|
|
34
|
+
content: string;
|
|
35
|
+
toolCallId?: string;
|
|
36
|
+
timestamp: string;
|
|
37
|
+
}
|
|
38
|
+
export type ContextSourceType = 'session_event' | 'tool_output' | 'knowledge' | 'user_message' | 'system_prompt' | 'tool_definition';
|
|
39
|
+
export interface ContextSource {
|
|
40
|
+
type: ContextSourceType;
|
|
41
|
+
id: string;
|
|
42
|
+
content: string;
|
|
43
|
+
priority: number;
|
|
44
|
+
metadata?: Record<string, any>;
|
|
45
|
+
}
|
|
46
|
+
export interface TokenBudgetAllocation {
|
|
47
|
+
total: number;
|
|
48
|
+
systemPrompt: number;
|
|
49
|
+
toolDefinitions: number;
|
|
50
|
+
knowledge: number;
|
|
51
|
+
notes: number;
|
|
52
|
+
history: number;
|
|
53
|
+
}
|
|
54
|
+
export interface PipelineInput {
|
|
55
|
+
sources: ContextSource[];
|
|
56
|
+
budget: TokenBudgetAllocation;
|
|
57
|
+
knowledge?: QueryResult;
|
|
58
|
+
}
|
|
59
|
+
export interface PipelineOutput {
|
|
60
|
+
prompt: string;
|
|
61
|
+
snapshot: ContextUsageSnapshot;
|
|
62
|
+
dropped: Array<{
|
|
63
|
+
type: string;
|
|
64
|
+
id: string;
|
|
65
|
+
reason: string;
|
|
66
|
+
}>;
|
|
67
|
+
}
|
|
68
|
+
export interface ContextUsageSnapshot {
|
|
69
|
+
timestamp: string;
|
|
70
|
+
totalTokens: number;
|
|
71
|
+
breakdown: {
|
|
72
|
+
systemPrompt: number;
|
|
73
|
+
messages: number;
|
|
74
|
+
toolOutputs: number;
|
|
75
|
+
knowledge: number;
|
|
76
|
+
other: number;
|
|
77
|
+
};
|
|
78
|
+
truncatedItems: Array<{
|
|
79
|
+
type: string;
|
|
80
|
+
id: string;
|
|
81
|
+
originalTokens: number;
|
|
82
|
+
keptTokens: number;
|
|
83
|
+
}>;
|
|
84
|
+
offloadedItems: Array<{
|
|
85
|
+
type: string;
|
|
86
|
+
id: string;
|
|
87
|
+
target: 'disk' | 'summary' | 'dropped';
|
|
88
|
+
}>;
|
|
89
|
+
compactionTriggered: boolean;
|
|
90
|
+
compactionLevel?: CompactionLevel;
|
|
91
|
+
}
|
|
92
|
+
export type SessionEventType = 'user_message' | 'assistant_message' | 'tool_call' | 'tool_result' | 'checkpoint' | 'system';
|
|
93
|
+
export interface SessionEvent {
|
|
94
|
+
type: SessionEventType;
|
|
95
|
+
id: string;
|
|
96
|
+
content: string;
|
|
97
|
+
timestamp: string;
|
|
98
|
+
metadata?: Record<string, any>;
|
|
99
|
+
}
|
|
100
|
+
export interface SessionHandle {
|
|
101
|
+
id: string;
|
|
102
|
+
events: SessionEvent[];
|
|
103
|
+
createdAt: string;
|
|
104
|
+
lastActiveAt: string;
|
|
105
|
+
}
|
|
106
|
+
export interface SessionCheckpoint {
|
|
107
|
+
id: string;
|
|
108
|
+
sessionId: string;
|
|
109
|
+
timestamp: string;
|
|
110
|
+
eventCount: number;
|
|
111
|
+
summary: string;
|
|
112
|
+
}
|
|
113
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/context/types.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAC;AAMtD,MAAM,WAAW,gBAAgB;IAC/B,QAAQ,EAAE,MAAM,CAAC;IACjB,QAAQ,EAAE,MAAM,CAAC;IACjB,gBAAgB,EAAE,MAAM,CAAC;IACzB,gBAAgB,EAAE,OAAO,CAAC;CAC3B;AAED,eAAO,MAAM,mBAAmB,EAAE,gBAKjC,CAAC;AAMF,MAAM,WAAW,sBAAsB;IACrC,QAAQ,EAAE,MAAM,CAAC;IACjB,aAAa,EAAE,MAAM,CAAC;IACtB,YAAY,EAAE,MAAM,CAAC;IACrB,cAAc,EAAE,OAAO,CAAC;IACxB,KAAK,EAAE,OAAO,CAAC;CAChB;AAED,eAAO,MAAM,0BAA0B,EAAE,sBAMxC,CAAC;AAMF,MAAM,MAAM,eAAe,GAAG,UAAU,GAAG,SAAS,GAAG,YAAY,CAAC;AAEpE,MAAM,WAAW,gBAAgB;IAC/B,YAAY,EAAE,MAAM,CAAC;IACrB,KAAK,EAAE,eAAe,CAAC;IACvB,qBAAqB,EAAE,OAAO,CAAC;IAC/B,iBAAiB,EAAE,OAAO,CAAC;IAC3B,gBAAgB,EAAE,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