dsh-context-compression-improved 0.10.1 → 0.10.2
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 +15 -110
- package/README.zh.md +14 -104
- package/THIRD_PARTY_NOTICES.md +25 -7
- package/{packages/selector/dsh.plugin.json → dsh.plugin.json} +1 -1
- package/{packages/selector/lib → lib}/client.js +73 -12
- package/package.json +125 -112
- package/screenshots.json +3 -6
- package/.agents/plans/improve/checklist.md +0 -32
- package/.agents/plans/improve/findings.md +0 -102
- package/.agents/plans/improve/spec.md +0 -53
- package/.agents/plans/improve/tasks.md +0 -70
- package/.gitattributes +0 -2
- package/.githooks/pre-push +0 -37
- package/.github/workflows/ci.yml +0 -39
- package/CHANGELOG.de.md +0 -580
- package/CHANGELOG.es.md +0 -588
- package/CHANGELOG.fr.md +0 -576
- package/CHANGELOG.it.md +0 -577
- package/CHANGELOG.ja.md +0 -261
- package/CHANGELOG.ko.md +0 -254
- package/CHANGELOG.md +0 -544
- package/CHANGELOG.ru.md +0 -554
- package/CHANGELOG.zh.md +0 -238
- package/CONTRIBUTING.md +0 -22
- package/README.de.md +0 -96
- package/README.es.md +0 -96
- package/README.fr.md +0 -96
- package/README.it.md +0 -96
- package/README.ja.md +0 -102
- package/README.ko.md +0 -102
- package/README.ru.md +0 -96
- package/SECURITY.md +0 -18
- package/assets/board.png +0 -0
- package/assets/context.png +0 -0
- package/docs/assets/context-compression-selector-profiles.jpg +0 -0
- package/docs/assets/context-compression-selector-settings.png +0 -0
- package/docs/compat-0.1.5-report.md +0 -77
- package/docs/installation.de.md +0 -104
- package/docs/installation.es.md +0 -104
- package/docs/installation.fr.md +0 -104
- package/docs/installation.it.md +0 -104
- package/docs/installation.ja.md +0 -78
- package/docs/installation.ko.md +0 -78
- package/docs/installation.md +0 -103
- package/docs/installation.ru.md +0 -104
- package/docs/installation.zh.md +0 -100
- package/docs/repair-log.md +0 -657
- package/eslint.config.js +0 -30
- package/packages/selector/LICENSE +0 -21
- package/packages/selector/README.md +0 -26
- package/packages/selector/README.zh.md +0 -24
- package/packages/selector/THIRD_PARTY_NOTICES.md +0 -32
- package/packages/selector/docs/history-tool-call-working-set-spec.md +0 -112
- package/packages/selector/docs/native-tool-result-selector-spec.md +0 -34
- package/packages/selector/docs/subagent-cache-reuse-spec.md +0 -46
- package/packages/selector/lib/style.css +0 -350
- package/packages/selector/package.json +0 -178
- package/packages/selector/screenshots.json +0 -3
- package/packages/selector/src/client/CompressionProfileControls.tsx +0 -293
- package/packages/selector/src/client/CompressionProfileSelector.module.css +0 -199
- package/packages/selector/src/client/CompressionProfileSelector.tsx +0 -85
- package/packages/selector/src/client/CustomPolicyEditor.tsx +0 -216
- package/packages/selector/src/client/EstimatorControls.tsx +0 -277
- package/packages/selector/src/client/bridge-scope.ts +0 -125
- package/packages/selector/src/client/decode.ts +0 -57
- package/packages/selector/src/client/index.ts +0 -424
- package/packages/selector/src/client/locales/de.ts +0 -158
- package/packages/selector/src/client/locales/es.ts +0 -158
- package/packages/selector/src/client/locales/fr.ts +0 -158
- package/packages/selector/src/client/locales/it.ts +0 -158
- package/packages/selector/src/client/locales/ja.ts +0 -158
- package/packages/selector/src/client/locales/ko.ts +0 -158
- package/packages/selector/src/client/locales/ru.ts +0 -158
- package/packages/selector/src/client/locales.ts +0 -316
- package/packages/selector/src/client/monitor-fab.ts +0 -419
- package/packages/selector/src/client/preset-options.ts +0 -88
- package/packages/selector/src/client/savings-card.tsx +0 -269
- package/packages/selector/src/client/scope-face.ts +0 -24
- package/packages/selector/src/client/settings-section.tsx +0 -140
- package/packages/selector/src/compat/host-generation.ts +0 -96
- package/packages/selector/src/compat/llm-messages.ts +0 -50
- package/packages/selector/src/compat/settings-lease.ts +0 -108
- package/packages/selector/src/compat/tool-result.ts +0 -91
- package/packages/selector/src/css-modules.d.ts +0 -6
- package/packages/selector/src/deepseek-v4-tokenizer.ts +0 -205
- package/packages/selector/src/estimator-catalog.ts +0 -104
- package/packages/selector/src/index.ts +0 -890
- package/packages/selector/src/invariant.ts +0 -113
- package/packages/selector/src/preset-overlay.ts +0 -749
- package/packages/selector/src/profiles.ts +0 -431
- package/packages/selector/src/pruner/content.ts +0 -198
- package/packages/selector/src/pruner/session.ts +0 -97
- package/packages/selector/src/pruner/state.ts +0 -61
- package/packages/selector/src/pruner/tuning.ts +0 -23
- package/packages/selector/src/pruner/types.ts +0 -78
- package/packages/selector/src/pruner.ts +0 -2914
- package/packages/selector/src/runtime/adaptive-cost.ts +0 -205
- package/packages/selector/src/runtime/audit.ts +0 -320
- package/packages/selector/src/runtime/config.ts +0 -756
- package/packages/selector/src/runtime/custom-policy.ts +0 -278
- package/packages/selector/src/runtime/deepseek-official-pricing.ts +0 -298
- package/packages/selector/src/runtime/deepseek-v4-vision-tokens.ts +0 -254
- package/packages/selector/src/runtime/display-prefs.ts +0 -98
- package/packages/selector/src/runtime/measurement.ts +0 -444
- package/packages/selector/src/runtime/monitor.ts +0 -310
- package/packages/selector/src/runtime/panel-command.ts +0 -213
- package/packages/selector/src/runtime/reducers.ts +0 -1754
- package/packages/selector/src/runtime/retrieve.ts +0 -503
- package/packages/selector/src/runtime/savings-integral.ts +0 -117
- package/packages/selector/src/runtime/savings-projection.ts +0 -229
- package/packages/selector/src/runtime/savings-verify.ts +0 -446
- package/packages/selector/src/runtime/savings.ts +0 -526
- package/packages/selector/src/runtime/session-events.ts +0 -30
- package/packages/selector/src/runtime/session-file-decode.ts +0 -523
- package/packages/selector/src/runtime/stats-backfill.ts +0 -541
- package/packages/selector/src/runtime/stats-db.ts +0 -492
- package/packages/selector/src/runtime/stats-schema.ts +0 -123
- package/packages/selector/src/runtime/stats-sync.ts +0 -307
- package/packages/selector/src/runtime/summary-command.ts +0 -127
- package/packages/selector/src/runtime/tail-trim.ts +0 -176
- package/packages/selector/src/runtime/token-count.ts +0 -72
- package/packages/selector/src/runtime/tokenpilot/advisor-prompt.ts +0 -233
- package/packages/selector/src/runtime/tokenpilot/advisor-state.ts +0 -204
- package/packages/selector/src/runtime/tokenpilot/advisor.ts +0 -419
- package/packages/selector/src/runtime/tokenpilot/benefit.ts +0 -200
- package/packages/selector/src/runtime/tokenpilot/dedup.ts +0 -81
- package/packages/selector/src/runtime/tokenpilot/estimator.ts +0 -151
- package/packages/selector/src/runtime/tokenpilot/intent-fold.ts +0 -53
- package/packages/selector/src/runtime/tokenpilot/intent-gate.ts +0 -65
- package/packages/selector/src/runtime/tokenpilot/intent-input-mask.ts +0 -125
- package/packages/selector/src/runtime/tokenpilot/intent-range.ts +0 -37
- package/packages/selector/src/runtime/tokenpilot/locator.ts +0 -128
- package/packages/selector/src/runtime/tokenpilot/read-state.ts +0 -98
- package/packages/selector/src/runtime/tokenpilot/sidechannel.ts +0 -303
- package/packages/selector/src/runtime/toolclass.ts +0 -103
- package/packages/selector/src/runtime/types.ts +0 -365
- package/packages/selector/src/runtime/value.ts +0 -48
- package/packages/selector/src/settings-bridge.ts +0 -175
- package/packages/selector/tests/advisor-report.host.spec.ts +0 -223
- package/packages/selector/tests/auto-compact.client.spec.tsx +0 -214
- package/packages/selector/tests/built/client-artifact.spec.ts +0 -56
- package/packages/selector/tests/cache-prefix-audit.spec.ts +0 -123
- package/packages/selector/tests/code-skeleton.client.spec.ts +0 -98
- package/packages/selector/tests/compat/compat-narrow-waist.spec.ts +0 -150
- package/packages/selector/tests/custom-contract.client.spec.ts +0 -226
- package/packages/selector/tests/estimator-catalog.spec.ts +0 -70
- package/packages/selector/tests/estimator-channel.client.spec.tsx +0 -253
- package/packages/selector/tests/estimator-route-registration.host.spec.ts +0 -202
- package/packages/selector/tests/helpers/legacy-namespace.ts +0 -35
- package/packages/selector/tests/helpers/legacy-settings/index.ts +0 -15
- package/packages/selector/tests/helpers/legacy-settings/legacy/provider.d.ts +0 -41
- package/packages/selector/tests/helpers/legacy-settings/legacy/provider.js +0 -647
- package/packages/selector/tests/helpers/legacy-settings/legacy/redact.js +0 -78
- package/packages/selector/tests/helpers/legacy-settings/legacy/util-values.js +0 -234
- package/packages/selector/tests/host-preset-overlay.host.spec.ts +0 -204
- package/packages/selector/tests/monitor-fab.client.spec.ts +0 -220
- package/packages/selector/tests/preset-options-write.client.spec.ts +0 -192
- package/packages/selector/tests/preset-overlay-loader.e2e.host.spec.ts +0 -200
- package/packages/selector/tests/preset-overlay.host.spec.ts +0 -246
- package/packages/selector/tests/profiles.client.spec.tsx +0 -436
- package/packages/selector/tests/public/package-contract.client.spec.ts +0 -86
- package/packages/selector/tests/runtime/adaptive-cost.spec.ts +0 -167
- package/packages/selector/tests/runtime/advice-never-withholds.host.spec.ts +0 -230
- package/packages/selector/tests/runtime/advisor-invariant.spec.ts +0 -272
- package/packages/selector/tests/runtime/advisor.spec.ts +0 -226
- package/packages/selector/tests/runtime/audit.spec.ts +0 -275
- package/packages/selector/tests/runtime/auto-compact-config.spec.ts +0 -523
- package/packages/selector/tests/runtime/char-basis.spec.ts +0 -30
- package/packages/selector/tests/runtime/code-skeleton.spec.ts +0 -152
- package/packages/selector/tests/runtime/deepseek-official-pricing.spec.ts +0 -186
- package/packages/selector/tests/runtime/deepseek-v4-tokenizer.spec.ts +0 -122
- package/packages/selector/tests/runtime/deepseek-v4-vision-tokens.spec.ts +0 -122
- package/packages/selector/tests/runtime/deprecated-preset-options.spec.ts +0 -96
- package/packages/selector/tests/runtime/fixtures/profile-baseline.json +0 -273
- package/packages/selector/tests/runtime/fixtures/session-v4-dump.jsonl +0 -46
- package/packages/selector/tests/runtime/fixtures/tokenizer-golden.json +0 -106
- package/packages/selector/tests/runtime/fixtures/vision-golden.json +0 -459
- package/packages/selector/tests/runtime/frequency-longstrings.spec.ts +0 -74
- package/packages/selector/tests/runtime/fresh-gate.spec.ts +0 -43
- package/packages/selector/tests/runtime/html-reducer.spec.ts +0 -212
- package/packages/selector/tests/runtime/intent-gate.spec.ts +0 -62
- package/packages/selector/tests/runtime/intent-summary.spec.ts +0 -156
- package/packages/selector/tests/runtime/jsonl-reducer.spec.ts +0 -79
- package/packages/selector/tests/runtime/line-mapping.spec.ts +0 -153
- package/packages/selector/tests/runtime/monitor.spec.ts +0 -354
- package/packages/selector/tests/runtime/panel-command.spec.ts +0 -101
- package/packages/selector/tests/runtime/prose-reducers.spec.ts +0 -133
- package/packages/selector/tests/runtime/public/public-runtime.spec.ts +0 -2781
- package/packages/selector/tests/runtime/read-input-cap.spec.ts +0 -33
- package/packages/selector/tests/runtime/savings-accounting.spec.ts +0 -291
- package/packages/selector/tests/runtime/savings-integral.spec.ts +0 -169
- package/packages/selector/tests/runtime/savings-projection.spec.ts +0 -89
- package/packages/selector/tests/runtime/savings-verify.spec.ts +0 -226
- package/packages/selector/tests/runtime/savings.spec.ts +0 -139
- package/packages/selector/tests/runtime/search-reducer.spec.ts +0 -110
- package/packages/selector/tests/runtime/session-events.spec.ts +0 -27
- package/packages/selector/tests/runtime/session-file-decode.spec.ts +0 -281
- package/packages/selector/tests/runtime/settings-unwrap.spec.ts +0 -58
- package/packages/selector/tests/runtime/sidechannel.spec.ts +0 -241
- package/packages/selector/tests/runtime/stats-backfill-scheduler.spec.ts +0 -167
- package/packages/selector/tests/runtime/stats-backfill.spec.ts +0 -348
- package/packages/selector/tests/runtime/stats-db.spec.ts +0 -361
- package/packages/selector/tests/runtime/stats-privacy.spec.ts +0 -55
- package/packages/selector/tests/runtime/stats-sync.spec.ts +0 -278
- package/packages/selector/tests/runtime/summary-command.spec.ts +0 -154
- package/packages/selector/tests/runtime/toc-and-bundled.spec.ts +0 -159
- package/packages/selector/tests/runtime/tokenizer-golden.spec.ts +0 -53
- package/packages/selector/tests/runtime/tokenpilot/benefit.spec.ts +0 -217
- package/packages/selector/tests/runtime/tokenpilot/dedup.spec.ts +0 -52
- package/packages/selector/tests/runtime/tokenpilot/estimator.spec.ts +0 -79
- package/packages/selector/tests/runtime/tokenpilot/locator.spec.ts +0 -76
- package/packages/selector/tests/runtime/tokenpilot/profile-baseline.spec.ts +0 -119
- package/packages/selector/tests/runtime/tokenpilot/read-state.spec.ts +0 -82
- package/packages/selector/tests/runtime/toolclass.spec.ts +0 -156
- package/packages/selector/tests/runtime/value.spec.ts +0 -23
- package/packages/selector/tests/savings-sessions.client.spec.tsx +0 -140
- package/packages/selector/tests/settings-seat.client.spec.ts +0 -215
- package/packages/selector/tests/standing-generation.host.spec.ts +0 -724
- package/packages/selector/tests/subagent-cache-reuse.host.spec.ts +0 -269
- package/packages/selector/tests/support/cache-prefix-audit.ts +0 -105
- package/packages/selector/tests/support/mock-adapter.ts +0 -37
- package/packages/selector/tests/support/ui-primitives.tsx +0 -34
- package/packages/selector/tsconfig.json +0 -11
- package/packages/selector/tsdown.client.config.ts +0 -102
- package/packages/selector/tsdown.config.ts +0 -20
- package/pnpm-workspace.yaml +0 -76
- package/scripts/capture-profile-baseline.mjs +0 -42
- package/scripts/cell-browser-e2e.mjs +0 -354
- package/scripts/generate-tokenizer-fixtures.py +0 -81
- package/scripts/generate-vision-fixtures.py +0 -208
- package/scripts/hosts.mjs +0 -78
- package/scripts/packed-components-smoke.mjs +0 -763
- package/scripts/packed-install-e2e.mjs +0 -1346
- package/scripts/sync-hosts.mjs +0 -174
- package/scripts/toolclass-corpus-replay.mjs +0 -281
- package/scripts/verify-release.mjs +0 -338
- package/tests/TEST_INVENTORY.md +0 -41
- package/tsconfig.base.json +0 -18
- package/tsconfig.json +0 -7
- package/tsconfig.tests.json +0 -15
- package/vitest.built.config.ts +0 -9
- package/vitest.config.ts +0 -54
- /package/{packages/selector/assets → assets}/deepseek-v4/LICENSE.DeepSeek-V4-Pro.txt +0 -0
- /package/{packages/selector/assets → assets}/deepseek-v4/manifest.json +0 -0
- /package/{packages/selector/assets → assets}/deepseek-v4/tokenizer.json +0 -0
- /package/{packages/selector/assets → assets}/deepseek-v4/tokenizer_config.json +0 -0
- /package/{packages/selector/assets → assets}/deepseek-v4-vision-exp/LICENSE.DeepSeek-V4-Flash-Vision-Exp.txt +0 -0
- /package/{packages/selector/assets → assets}/deepseek-v4-vision-exp/manifest.json +0 -0
- /package/{packages/selector/assets → assets}/deepseek-v4-vision-exp/tokenizer.json +0 -0
- /package/{packages/selector/assets → assets}/deepseek-v4-vision-exp/tokenizer_config.json +0 -0
- /package/{packages/selector/cordis.patch.yml → cordis.patch.yml} +0 -0
- /package/{packages/selector/lib → lib}/client.d.ts +0 -0
- /package/{packages/selector/lib → lib}/index.d.ts +0 -0
- /package/{packages/selector/lib → lib}/index.js +0 -0
- /package/{packages/selector/lib → lib}/invariant.d.ts +0 -0
- /package/{packages/selector/lib → lib}/invariant.js +0 -0
- /package/{packages/selector/lib → lib}/profiles.d.ts +0 -0
- /package/{packages/selector/lib → lib}/pruner.d.ts +0 -0
- /package/{packages/selector/lib → lib}/pruner.js +0 -0
- /package/{packages/selector/lib → lib}/savings-integral.js +0 -0
- /package/{packages/selector/lib → lib}/tail-trim.js +0 -0
- /package/{packages/selector/lib → lib}/tool-result.js +0 -0
|
@@ -1,431 +0,0 @@
|
|
|
1
|
-
/** Public context-compression choices shared by the Host schema and browser selector. */
|
|
2
|
-
export const COMPRESSION_PROFILES = [
|
|
3
|
-
'off',
|
|
4
|
-
'native',
|
|
5
|
-
'balanced',
|
|
6
|
-
'cache-strict',
|
|
7
|
-
'savings',
|
|
8
|
-
'adaptive',
|
|
9
|
-
'tokenpilot-inspired',
|
|
10
|
-
'custom',
|
|
11
|
-
] as const
|
|
12
|
-
|
|
13
|
-
/** One supported context-compression profile. */
|
|
14
|
-
export type CompressionProfile = typeof COMPRESSION_PROFILES[number]
|
|
15
|
-
|
|
16
|
-
/** Single canonical unit stored by a version-1 browser Custom document. */
|
|
17
|
-
export type CustomCompressionUnit = 'tokens' | 'context-percent'
|
|
18
|
-
|
|
19
|
-
/** Whether Custom History may routinely rewrite an already-sent prefix. */
|
|
20
|
-
export type CustomPrefixPolicy = 'preserve' | 'pressure-break'
|
|
21
|
-
|
|
22
|
-
/** Browser representation of one independently enabled Fresh or Aggregate budget. */
|
|
23
|
-
export interface CustomCompressionBudget {
|
|
24
|
-
enabled: boolean
|
|
25
|
-
trigger: number
|
|
26
|
-
target: number
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
/** Legacy browser representation of the Custom History working set. */
|
|
30
|
-
export interface LegacyCustomHistoryPolicy {
|
|
31
|
-
enabled: boolean
|
|
32
|
-
trigger: number
|
|
33
|
-
keepRecentTurns: number
|
|
34
|
-
keepRecent: number
|
|
35
|
-
minReclaim: number
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
/** Browser representation of Custom History tool-call and token-tail protection. */
|
|
39
|
-
export interface CustomHistoryPolicy {
|
|
40
|
-
enabled: boolean
|
|
41
|
-
trigger: number
|
|
42
|
-
keepRecentToolCalls: number
|
|
43
|
-
keepRecentTokens: number
|
|
44
|
-
minReclaim: number
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
/** Browser representation of the Custom-only Experimental TailTrim gate. */
|
|
48
|
-
export interface CustomTailTrimPolicy {
|
|
49
|
-
enabled: boolean
|
|
50
|
-
trigger: number
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
interface CustomCompressionPolicyCommon<HistoryPolicy> {
|
|
54
|
-
unit: CustomCompressionUnit
|
|
55
|
-
fresh: CustomCompressionBudget
|
|
56
|
-
aggregate: CustomCompressionBudget
|
|
57
|
-
history: HistoryPolicy
|
|
58
|
-
prefixPolicy: CustomPrefixPolicy
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
/** Legacy public Custom document; accepted and normalized before editing. */
|
|
62
|
-
export interface CustomCompressionPolicyV1 extends CustomCompressionPolicyCommon<LegacyCustomHistoryPolicy> {
|
|
63
|
-
version: 1
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
/** Legacy Custom document with a default-disabled TailTrim stage. */
|
|
67
|
-
export interface CustomCompressionPolicyV2 extends CustomCompressionPolicyCommon<LegacyCustomHistoryPolicy> {
|
|
68
|
-
version: 2
|
|
69
|
-
tailTrim: CustomTailTrimPolicy
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
/** Public Custom document with tool-call working-set protection. */
|
|
73
|
-
export interface CustomCompressionPolicyV3 extends CustomCompressionPolicyCommon<CustomHistoryPolicy> {
|
|
74
|
-
version: 3
|
|
75
|
-
tailTrim: CustomTailTrimPolicy
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
/** Exact public Custom document accepted by the Host and browser boundary. */
|
|
79
|
-
export type CustomCompressionPolicy = CustomCompressionPolicyV1 | CustomCompressionPolicyV2 | CustomCompressionPolicyV3
|
|
80
|
-
|
|
81
|
-
/** Browser-safe mirror of the Host's Balanced-equivalent Custom default. */
|
|
82
|
-
export const DEFAULT_CUSTOM_COMPRESSION_POLICY: CustomCompressionPolicyV3 = {
|
|
83
|
-
version: 3,
|
|
84
|
-
unit: 'tokens',
|
|
85
|
-
fresh: { enabled: true, trigger: 8_192, target: 3_072 },
|
|
86
|
-
aggregate: { enabled: true, trigger: 32_768, target: 12_288 },
|
|
87
|
-
history: {
|
|
88
|
-
enabled: true,
|
|
89
|
-
trigger: 500_000,
|
|
90
|
-
keepRecentToolCalls: 10,
|
|
91
|
-
keepRecentTokens: 64_000,
|
|
92
|
-
minReclaim: 96_000,
|
|
93
|
-
},
|
|
94
|
-
prefixPolicy: 'pressure-break',
|
|
95
|
-
tailTrim: { enabled: false, trigger: 700_000 },
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
/** User-tunable Auto Compact coordination preferences. */
|
|
99
|
-
export interface AutoCompactSettings {
|
|
100
|
-
thresholdPercent: number
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
/**
|
|
104
|
-
* Orthogonal code-skeleton reducer gate, mirrored browser-safe from the
|
|
105
|
-
* runtime: independent of every profile, default off.
|
|
106
|
-
*/
|
|
107
|
-
export interface CodeSkeletonSettings {
|
|
108
|
-
enabled: boolean
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
/**
|
|
112
|
-
* Orthogonal turn-tail intent-summary gate, mirrored browser-safe from the
|
|
113
|
-
* runtime: independent of every profile, default off.
|
|
114
|
-
*/
|
|
115
|
-
export interface IntentSummarySettings {
|
|
116
|
-
enabled: boolean
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
/** Floating compression-monitor panel (FAB); visibility switch. */
|
|
120
|
-
export interface MonitorPanelSettings {
|
|
121
|
-
enabled: boolean
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
/**
|
|
125
|
-
* Decode the persisted codeSkeleton section with exactly the runtime schema's
|
|
126
|
-
* strictness: absent means the lossless off default; present values must be a
|
|
127
|
-
* plain object carrying only a boolean `enabled`. Anything else is invalid,
|
|
128
|
-
* never silently coerced.
|
|
129
|
-
*/
|
|
130
|
-
export function decodeCodeSkeletonSettings(value: unknown): CodeSkeletonSettings | undefined {
|
|
131
|
-
if (value === undefined) return { enabled: false }
|
|
132
|
-
if (!isPlainRecord(value)) return undefined
|
|
133
|
-
const keys = Object.keys(value)
|
|
134
|
-
if (keys.length !== 1 || keys[0] !== 'enabled') return undefined
|
|
135
|
-
const enabled = (value as Record<string, unknown>).enabled
|
|
136
|
-
return typeof enabled === 'boolean' ? { enabled } : undefined
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
/** Browser-safe mirror of the runtime intentSummary section (absent inherits off). */
|
|
140
|
-
export function decodeIntentSummarySettings(value: unknown): IntentSummarySettings | undefined {
|
|
141
|
-
if (value === undefined) return { enabled: false }
|
|
142
|
-
if (!isPlainRecord(value)) return undefined
|
|
143
|
-
const keys = Object.keys(value)
|
|
144
|
-
if (keys.length !== 1 || keys[0] !== 'enabled') return undefined
|
|
145
|
-
const enabled = (value as Record<string, unknown>).enabled
|
|
146
|
-
return typeof enabled === 'boolean' ? { enabled } : undefined
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
/** Browser-safe mirror of the runtime monitorPanel section (absent inherits hidden). */
|
|
150
|
-
export function decodeMonitorPanelSettings(value: unknown): MonitorPanelSettings | undefined {
|
|
151
|
-
if (value === undefined) return { enabled: false }
|
|
152
|
-
if (!isPlainRecord(value)) return undefined
|
|
153
|
-
const keys = Object.keys(value)
|
|
154
|
-
if (keys.length !== 1 || keys[0] !== 'enabled') return undefined
|
|
155
|
-
const enabled = (value as Record<string, unknown>).enabled
|
|
156
|
-
return typeof enabled === 'boolean' ? { enabled } : undefined
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
/** Browser-safe mirror of the runtime presetOptions section. */
|
|
160
|
-
export interface PresetOptionsSettings {
|
|
161
|
-
readonly dedupeToolResults?: boolean
|
|
162
|
-
readonly summaryLocator?: boolean
|
|
163
|
-
readonly prefixStabilizer?: boolean
|
|
164
|
-
readonly readState?: boolean
|
|
165
|
-
readonly estimatorMode?: '' | 'host' | 'direct'
|
|
166
|
-
readonly estimatorProvider?: string
|
|
167
|
-
readonly estimatorModel?: string
|
|
168
|
-
readonly estimatorBaseUrl?: string
|
|
169
|
-
readonly estimatorApiKey?: string
|
|
170
|
-
readonly estimatorTimeoutMs?: number
|
|
171
|
-
/** Advisory advisor channel; `''` (the default) keeps the advisor off. */
|
|
172
|
-
readonly advisorMode?: '' | 'host' | 'direct'
|
|
173
|
-
readonly advisorTimeoutMs?: number
|
|
174
|
-
readonly advisorRefreshTurns?: number
|
|
175
|
-
readonly advisorScoreThreshold?: number
|
|
176
|
-
readonly advisorSampleLimit?: number
|
|
177
|
-
readonly advisorMinTokens?: number
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
/**
|
|
181
|
-
* Browser mirror of the runtime presetOptions section: absent inherits the
|
|
182
|
-
* preset defaults (decodes to `undefined`); present values must be a plain
|
|
183
|
-
* object carrying only the known keys with valid types.
|
|
184
|
-
*/
|
|
185
|
-
export function decodePresetOptionsSettings(value: unknown): PresetOptionsSettings | undefined {
|
|
186
|
-
if (value === undefined) return undefined
|
|
187
|
-
if (!isPlainRecord(value)) return undefined
|
|
188
|
-
const allowed = new Set([
|
|
189
|
-
'dedupeToolResults', 'summaryLocator', 'prefixStabilizer', 'readState', 'estimatorMode',
|
|
190
|
-
'estimatorProvider', 'estimatorModel', 'estimatorBaseUrl', 'estimatorApiKey', 'estimatorTimeoutMs',
|
|
191
|
-
// Legacy keys of the retired review gate: still ACCEPTED so an existing
|
|
192
|
-
// settings document keeps decoding (rejecting them would report the whole
|
|
193
|
-
// card unreadable), and ignored — nothing reads them any more.
|
|
194
|
-
'reviewMode', 'reviewTimeoutTurns', 'cacheHitDiscountAlpha', 'reviewHighImpactTokens',
|
|
195
|
-
'advisorMode', 'advisorTimeoutMs', 'advisorRefreshTurns', 'advisorScoreThreshold', 'advisorSampleLimit',
|
|
196
|
-
'advisorMinTokens',
|
|
197
|
-
])
|
|
198
|
-
if (Object.keys(value).some(key => !allowed.has(key))) return undefined
|
|
199
|
-
for (const key of ['dedupeToolResults', 'summaryLocator', 'prefixStabilizer', 'readState'] as const) {
|
|
200
|
-
const entry = value[key]
|
|
201
|
-
if (entry !== undefined && typeof entry !== 'boolean') return undefined
|
|
202
|
-
}
|
|
203
|
-
const estimatorMode = value.estimatorMode
|
|
204
|
-
if (estimatorMode !== undefined && estimatorMode !== '' && estimatorMode !== 'host' && estimatorMode !== 'direct') {
|
|
205
|
-
return undefined
|
|
206
|
-
}
|
|
207
|
-
const advisorMode = value.advisorMode
|
|
208
|
-
if (advisorMode !== undefined && advisorMode !== '' && advisorMode !== 'host' && advisorMode !== 'direct') {
|
|
209
|
-
return undefined
|
|
210
|
-
}
|
|
211
|
-
for (const key of ['estimatorProvider', 'estimatorModel', 'estimatorBaseUrl', 'estimatorApiKey'] as const) {
|
|
212
|
-
const entry = value[key]
|
|
213
|
-
if (entry !== undefined && typeof entry !== 'string') return undefined
|
|
214
|
-
}
|
|
215
|
-
const estimatorTimeoutMs = value.estimatorTimeoutMs
|
|
216
|
-
if (estimatorTimeoutMs !== undefined
|
|
217
|
-
&& (typeof estimatorTimeoutMs !== 'number' || !Number.isSafeInteger(estimatorTimeoutMs)
|
|
218
|
-
|| estimatorTimeoutMs < 100 || estimatorTimeoutMs > 60_000)) {
|
|
219
|
-
return undefined
|
|
220
|
-
}
|
|
221
|
-
const advisorTimeoutMs = value.advisorTimeoutMs
|
|
222
|
-
if (advisorTimeoutMs !== undefined
|
|
223
|
-
&& (typeof advisorTimeoutMs !== 'number' || !Number.isSafeInteger(advisorTimeoutMs)
|
|
224
|
-
|| advisorTimeoutMs < 100 || advisorTimeoutMs > 60_000)) {
|
|
225
|
-
return undefined
|
|
226
|
-
}
|
|
227
|
-
const advisorRefreshTurns = value.advisorRefreshTurns
|
|
228
|
-
if (advisorRefreshTurns !== undefined
|
|
229
|
-
&& (typeof advisorRefreshTurns !== 'number' || !Number.isSafeInteger(advisorRefreshTurns)
|
|
230
|
-
|| advisorRefreshTurns < 1)) {
|
|
231
|
-
return undefined
|
|
232
|
-
}
|
|
233
|
-
const advisorScoreThreshold = value.advisorScoreThreshold
|
|
234
|
-
if (advisorScoreThreshold !== undefined
|
|
235
|
-
&& (typeof advisorScoreThreshold !== 'number' || !Number.isFinite(advisorScoreThreshold)
|
|
236
|
-
|| advisorScoreThreshold <= 0 || advisorScoreThreshold >= 1)) {
|
|
237
|
-
return undefined
|
|
238
|
-
}
|
|
239
|
-
const advisorSampleLimit = value.advisorSampleLimit
|
|
240
|
-
if (advisorSampleLimit !== undefined
|
|
241
|
-
&& (typeof advisorSampleLimit !== 'number' || !Number.isSafeInteger(advisorSampleLimit)
|
|
242
|
-
|| advisorSampleLimit < 1 || advisorSampleLimit > 64)) {
|
|
243
|
-
return undefined
|
|
244
|
-
}
|
|
245
|
-
const advisorMinTokens = value.advisorMinTokens
|
|
246
|
-
if (advisorMinTokens !== undefined
|
|
247
|
-
&& (typeof advisorMinTokens !== 'number' || !Number.isSafeInteger(advisorMinTokens) || advisorMinTokens < 1)) {
|
|
248
|
-
return undefined
|
|
249
|
-
}
|
|
250
|
-
const decoded: {
|
|
251
|
-
-readonly [K in keyof PresetOptionsSettings]: PresetOptionsSettings[K]
|
|
252
|
-
} = {}
|
|
253
|
-
if (value.dedupeToolResults !== undefined) decoded.dedupeToolResults = value.dedupeToolResults as boolean
|
|
254
|
-
if (value.summaryLocator !== undefined) decoded.summaryLocator = value.summaryLocator as boolean
|
|
255
|
-
if (value.prefixStabilizer !== undefined) decoded.prefixStabilizer = value.prefixStabilizer as boolean
|
|
256
|
-
if (value.readState !== undefined) decoded.readState = value.readState as boolean
|
|
257
|
-
if (estimatorMode !== undefined) decoded.estimatorMode = estimatorMode as '' | 'host' | 'direct'
|
|
258
|
-
if (value.estimatorProvider !== undefined) decoded.estimatorProvider = value.estimatorProvider as string
|
|
259
|
-
if (value.estimatorModel !== undefined) decoded.estimatorModel = value.estimatorModel as string
|
|
260
|
-
if (value.estimatorBaseUrl !== undefined) decoded.estimatorBaseUrl = value.estimatorBaseUrl as string
|
|
261
|
-
if (value.estimatorApiKey !== undefined) decoded.estimatorApiKey = value.estimatorApiKey as string
|
|
262
|
-
if (estimatorTimeoutMs !== undefined) decoded.estimatorTimeoutMs = estimatorTimeoutMs as number
|
|
263
|
-
if (advisorMode !== undefined) decoded.advisorMode = advisorMode as '' | 'host' | 'direct'
|
|
264
|
-
if (advisorTimeoutMs !== undefined) decoded.advisorTimeoutMs = advisorTimeoutMs as number
|
|
265
|
-
if (advisorRefreshTurns !== undefined) decoded.advisorRefreshTurns = advisorRefreshTurns as number
|
|
266
|
-
if (advisorScoreThreshold !== undefined) decoded.advisorScoreThreshold = advisorScoreThreshold as number
|
|
267
|
-
if (advisorSampleLimit !== undefined) decoded.advisorSampleLimit = advisorSampleLimit as number
|
|
268
|
-
if (advisorMinTokens !== undefined) decoded.advisorMinTokens = advisorMinTokens as number
|
|
269
|
-
return decoded
|
|
270
|
-
}
|
|
271
|
-
|
|
272
|
-
/**
|
|
273
|
-
* The one threshold contract shared by the UI, the persisted settings, and the
|
|
274
|
-
* runtime resolver; mirrored browser-safe from the runtime package.
|
|
275
|
-
*/
|
|
276
|
-
export const AUTO_COMPACT_THRESHOLD_LIMITS = Object.freeze({
|
|
277
|
-
min: 50,
|
|
278
|
-
max: 90,
|
|
279
|
-
step: 1,
|
|
280
|
-
default: 80,
|
|
281
|
-
} as const)
|
|
282
|
-
|
|
283
|
-
/** Narrow one unknown value to a valid Auto Compact threshold percent. */
|
|
284
|
-
export function isValidAutoCompactThresholdPercent(value: unknown): value is number {
|
|
285
|
-
return typeof value === 'number'
|
|
286
|
-
&& Number.isSafeInteger(value)
|
|
287
|
-
&& value >= AUTO_COMPACT_THRESHOLD_LIMITS.min
|
|
288
|
-
&& value <= AUTO_COMPACT_THRESHOLD_LIMITS.max
|
|
289
|
-
}
|
|
290
|
-
|
|
291
|
-
/** Accept JSON-object records while rejecting class instances and exotic prototypes. */
|
|
292
|
-
export function isPlainRecord(value: unknown): value is Record<string, unknown> {
|
|
293
|
-
if (value === null || typeof value !== 'object' || Array.isArray(value)) return false
|
|
294
|
-
const prototype = Object.getPrototypeOf(value) as object | null
|
|
295
|
-
return prototype === Object.prototype || prototype === null
|
|
296
|
-
}
|
|
297
|
-
|
|
298
|
-
/**
|
|
299
|
-
* Decode the persisted autoCompact section with exactly the runtime schema's
|
|
300
|
-
* strictness: absent means the 80% default; present values must be a plain
|
|
301
|
-
* object carrying only a valid `thresholdPercent`. Anything else is invalid,
|
|
302
|
-
* never silently coerced.
|
|
303
|
-
*/
|
|
304
|
-
export function decodeAutoCompactSettings(value: unknown): AutoCompactSettings | undefined {
|
|
305
|
-
if (value === undefined) return { thresholdPercent: AUTO_COMPACT_THRESHOLD_LIMITS.default }
|
|
306
|
-
if (!isPlainRecord(value)) return undefined
|
|
307
|
-
const keys = Object.keys(value)
|
|
308
|
-
if (keys.length !== 1 || keys[0] !== 'thresholdPercent') return undefined
|
|
309
|
-
const thresholdPercent = (value as Record<string, unknown>).thresholdPercent
|
|
310
|
-
return isValidAutoCompactThresholdPercent(thresholdPercent)
|
|
311
|
-
? { thresholdPercent }
|
|
312
|
-
: undefined
|
|
313
|
-
}
|
|
314
|
-
|
|
315
|
-
/** Durable settings section owned by this package. */
|
|
316
|
-
export interface ContextCompressionSettings {
|
|
317
|
-
/** Default profile captured when each Session first reaches the pruner. */
|
|
318
|
-
profile: CompressionProfile
|
|
319
|
-
/** Complete canonical policy captured with `profile` when the runtime first observes a Session. */
|
|
320
|
-
custom: CustomCompressionPolicy
|
|
321
|
-
/** Auto Compact trigger captured with `profile` when the runtime first observes a Session. */
|
|
322
|
-
autoCompact: AutoCompactSettings
|
|
323
|
-
/** Code-skeleton reducer gate captured independently of `profile`. */
|
|
324
|
-
codeSkeleton: CodeSkeletonSettings
|
|
325
|
-
/** Turn-tail intent-summary gate captured independently of `profile`. */
|
|
326
|
-
intentSummary: IntentSummarySettings
|
|
327
|
-
/** Floating compression-monitor panel (FAB); absent inherits hidden. */
|
|
328
|
-
monitorPanel?: MonitorPanelSettings
|
|
329
|
-
/** Optional tokenpilot-inspired sub-capability overrides (presence-validated only). */
|
|
330
|
-
presetOptions?: PresetOptionsSettings
|
|
331
|
-
}
|
|
332
|
-
|
|
333
|
-
/**
|
|
334
|
-
* Narrow an unknown settings value to a complete supported Custom policy.
|
|
335
|
-
* @param value - Candidate settings value received from the Host or edited locally.
|
|
336
|
-
* @returns Whether the value is a relation-valid Custom policy.
|
|
337
|
-
*/
|
|
338
|
-
export function isCustomCompressionPolicy(value: unknown): value is CustomCompressionPolicy {
|
|
339
|
-
if (!hasExactKeys(
|
|
340
|
-
value,
|
|
341
|
-
value !== null && typeof value === 'object' && 'version' in value && value.version === 1
|
|
342
|
-
? ['version', 'unit', 'fresh', 'aggregate', 'history', 'prefixPolicy']
|
|
343
|
-
: ['version', 'unit', 'fresh', 'aggregate', 'history', 'prefixPolicy', 'tailTrim'],
|
|
344
|
-
)) return false
|
|
345
|
-
if ((value.version !== 1 && value.version !== 2 && value.version !== 3)
|
|
346
|
-
|| (value.unit !== 'tokens' && value.unit !== 'context-percent')) return false
|
|
347
|
-
if (value.prefixPolicy !== 'preserve' && value.prefixPolicy !== 'pressure-break') return false
|
|
348
|
-
if (!isBudget(value.fresh) || !isBudget(value.aggregate)) return false
|
|
349
|
-
const modernHistory = value.version === 3
|
|
350
|
-
if (!hasExactKeys(
|
|
351
|
-
value.history,
|
|
352
|
-
modernHistory
|
|
353
|
-
? ['enabled', 'trigger', 'keepRecentToolCalls', 'keepRecentTokens', 'minReclaim']
|
|
354
|
-
: ['enabled', 'trigger', 'keepRecentTurns', 'keepRecent', 'minReclaim'],
|
|
355
|
-
)) return false
|
|
356
|
-
if (typeof value.history.enabled !== 'boolean'
|
|
357
|
-
|| typeof value.history.trigger !== 'number'
|
|
358
|
-
|| typeof value.history.minReclaim !== 'number') return false
|
|
359
|
-
const recent = modernHistory
|
|
360
|
-
? value.history.keepRecentTokens
|
|
361
|
-
: value.history.keepRecent
|
|
362
|
-
const calls = modernHistory ? value.history.keepRecentToolCalls : value.history.keepRecentTurns
|
|
363
|
-
if (typeof recent !== 'number'
|
|
364
|
-
|| typeof calls !== 'number'
|
|
365
|
-
|| !Number.isSafeInteger(calls)
|
|
366
|
-
|| calls < 0) return false
|
|
367
|
-
let tailTrimTrigger: number | undefined
|
|
368
|
-
if (value.version !== 1) {
|
|
369
|
-
const tailTrim = value.tailTrim
|
|
370
|
-
if (!hasExactKeys(tailTrim, ['enabled', 'trigger'])
|
|
371
|
-
|| typeof tailTrim.enabled !== 'boolean'
|
|
372
|
-
|| typeof tailTrim.trigger !== 'number') return false
|
|
373
|
-
tailTrimTrigger = tailTrim.trigger
|
|
374
|
-
}
|
|
375
|
-
const measured = [
|
|
376
|
-
value.fresh.trigger, value.fresh.target,
|
|
377
|
-
value.aggregate.trigger, value.aggregate.target,
|
|
378
|
-
value.history.trigger, recent, value.history.minReclaim,
|
|
379
|
-
...tailTrimTrigger === undefined ? [] : [tailTrimTrigger],
|
|
380
|
-
]
|
|
381
|
-
if (!measured.every(entry => typeof entry === 'number' && Number.isFinite(entry))) return false
|
|
382
|
-
if (value.fresh.trigger <= 0 || value.fresh.target <= 0
|
|
383
|
-
|| value.aggregate.trigger <= 0 || value.aggregate.target <= 0
|
|
384
|
-
|| value.history.trigger <= 0 || recent < 0
|
|
385
|
-
|| value.history.minReclaim <= 0
|
|
386
|
-
|| (tailTrimTrigger !== undefined && tailTrimTrigger <= 0)) return false
|
|
387
|
-
if (value.unit === 'tokens' && !measured.every(Number.isSafeInteger)) return false
|
|
388
|
-
if (value.unit === 'context-percent' && !measured.every(entry => entry <= 100)) return false
|
|
389
|
-
return value.fresh.target < value.fresh.trigger
|
|
390
|
-
&& value.aggregate.target < value.aggregate.trigger
|
|
391
|
-
&& value.history.minReclaim <= value.history.trigger
|
|
392
|
-
}
|
|
393
|
-
|
|
394
|
-
function isBudget(value: unknown): value is CustomCompressionBudget {
|
|
395
|
-
return hasExactKeys(value, ['enabled', 'trigger', 'target'])
|
|
396
|
-
&& typeof value.enabled === 'boolean'
|
|
397
|
-
&& typeof value.trigger === 'number'
|
|
398
|
-
&& typeof value.target === 'number'
|
|
399
|
-
}
|
|
400
|
-
|
|
401
|
-
/**
|
|
402
|
-
* Canonicalize one validated Custom document to version 3, mirroring the
|
|
403
|
-
* runtime's `canonicalizeCustomPolicy` exactly so the browser and runtime
|
|
404
|
-
* boundaries hand the SAME complete document to the UI and the policy
|
|
405
|
-
* resolver: legacy v1/v2 History working sets upgrade to the 10-call default,
|
|
406
|
-
* and a v1 document gains the default-disabled TailTrim stage.
|
|
407
|
-
*/
|
|
408
|
-
export function canonicalizeCustomPolicy(policy: CustomCompressionPolicy): CustomCompressionPolicyV3 {
|
|
409
|
-
if (policy.version === 3) return structuredClone(policy)
|
|
410
|
-
return {
|
|
411
|
-
version: 3,
|
|
412
|
-
unit: policy.unit,
|
|
413
|
-
fresh: structuredClone(policy.fresh),
|
|
414
|
-
aggregate: structuredClone(policy.aggregate),
|
|
415
|
-
history: {
|
|
416
|
-
enabled: policy.history.enabled,
|
|
417
|
-
trigger: policy.history.trigger,
|
|
418
|
-
keepRecentToolCalls: 10,
|
|
419
|
-
keepRecentTokens: policy.history.keepRecent,
|
|
420
|
-
minReclaim: policy.history.minReclaim,
|
|
421
|
-
},
|
|
422
|
-
prefixPolicy: policy.prefixPolicy,
|
|
423
|
-
tailTrim: policy.version === 1 ? { enabled: false, trigger: 700_000 } : structuredClone(policy.tailTrim),
|
|
424
|
-
}
|
|
425
|
-
}
|
|
426
|
-
|
|
427
|
-
function hasExactKeys(value: unknown, expected: readonly string[]): value is Record<string, unknown> {
|
|
428
|
-
if (!isPlainRecord(value)) return false
|
|
429
|
-
const keys = Object.keys(value)
|
|
430
|
-
return keys.length === expected.length && keys.every(key => expected.includes(key))
|
|
431
|
-
}
|
|
@@ -1,198 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Pure content-analysis functions extracted from ToolResultPruner.
|
|
3
|
-
*
|
|
4
|
-
* Every function here has ZERO `this` dependency — they receive all inputs
|
|
5
|
-
* as explicit arguments. Grouped by natural cohesion (content blocks →
|
|
6
|
-
* token counting → recovery helpers).
|
|
7
|
-
*
|
|
8
|
-
* @module dsh-context-compression-improved/pruner/content
|
|
9
|
-
*/
|
|
10
|
-
|
|
11
|
-
import type { ContentBlock } from '@deepseek-ai/dsh-llm'
|
|
12
|
-
import type {
|
|
13
|
-
CompactionTokenView,
|
|
14
|
-
ProviderMeasurementKey,
|
|
15
|
-
TokenCount,
|
|
16
|
-
} from '../runtime/measurement.ts'
|
|
17
|
-
import { countExactCanonicalTextFields } from '../runtime/token-count.ts'
|
|
18
|
-
import { codePointLength, PRUNE_MARKER } from '../runtime/config.ts'
|
|
19
|
-
import type { PrunedEntry, PruneResult } from '../runtime/types.ts'
|
|
20
|
-
import { RICH_BLOCK_PRESSURE_COST } from './tuning.ts'
|
|
21
|
-
|
|
22
|
-
// ── Content-block predicates ────────────────────────────────────────────
|
|
23
|
-
|
|
24
|
-
function onlyTextBlock(blocks: readonly ContentBlock[]): Extract<ContentBlock, { type: 'text' }> | null {
|
|
25
|
-
return blocks.length === 1 && blocks[0]?.type === 'text' ? blocks[0] : null
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
function onlyTextBlocks(blocks: readonly ContentBlock[]): readonly Extract<ContentBlock, { type: 'text' }>[] | null {
|
|
29
|
-
return blocks.every((block): block is Extract<ContentBlock, { type: 'text' }> => block.type === 'text')
|
|
30
|
-
? blocks
|
|
31
|
-
: null
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
// ── Token counting ──────────────────────────────────────────────────────
|
|
35
|
-
|
|
36
|
-
function countToolContent(blocks: readonly ContentBlock[], view: CompactionTokenView): TokenCount {
|
|
37
|
-
const text = onlyTextBlocks(blocks)
|
|
38
|
-
if (text === null) return unavailableCount('tool result contains unsupported rich content')
|
|
39
|
-
return countExactCanonicalTextFields(
|
|
40
|
-
text.map(block => block.text),
|
|
41
|
-
candidate => view.countCanonicalText(candidate),
|
|
42
|
-
'tool result replacement',
|
|
43
|
-
)
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
function exactTokens(count: TokenCount): number | undefined {
|
|
47
|
-
return count.kind === 'exact-tokenizer' ? count.tokens : undefined
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
// ── Provider measurement key comparison ─────────────────────────────────
|
|
51
|
-
|
|
52
|
-
function sameProviderMeasurementKey(
|
|
53
|
-
left: Readonly<ProviderMeasurementKey>,
|
|
54
|
-
right: Readonly<ProviderMeasurementKey>,
|
|
55
|
-
): boolean {
|
|
56
|
-
return left.provider === right.provider
|
|
57
|
-
&& left.baseUrlClass === right.baseUrlClass
|
|
58
|
-
&& left.apiRoute === right.apiRoute
|
|
59
|
-
&& left.modelId === right.modelId
|
|
60
|
-
&& left.requestTemplateRevision === right.requestTemplateRevision
|
|
61
|
-
&& left.tokenizerRevision === right.tokenizerRevision
|
|
62
|
-
&& left.modality === right.modality
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
// ── Token helpers ───────────────────────────────────────────────────────
|
|
66
|
-
|
|
67
|
-
function unavailableCount(reason: string): TokenCount {
|
|
68
|
-
return Object.freeze({ kind: 'unavailable', reason })
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
// ── Recovery markers ────────────────────────────────────────────────────
|
|
72
|
-
|
|
73
|
-
function recoveryMarker(sourceRef: string, label: string, startLine?: number): string {
|
|
74
|
-
const hint = startLine === undefined
|
|
75
|
-
? 'use context_compression_retrieve with a query or narrow start_line/max_lines if needed (avoid re-reading the whole result)'
|
|
76
|
-
: `retrieve with context_compression_retrieve({"ref":"${sourceRef}","start_line":${String(startLine)},"max_lines":80})`
|
|
77
|
-
return `\n\n[... ${label}; source=${sourceRef}; ${hint} ...]\n\n`
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
// ── Content measurement ─────────────────────────────────────────────────
|
|
81
|
-
|
|
82
|
-
/**
|
|
83
|
-
* Measure text content in Unicode code points; non-text blocks cost zero.
|
|
84
|
-
* @param blocks - tool-result content to measure.
|
|
85
|
-
* @returns total Unicode code points across text blocks.
|
|
86
|
-
*/
|
|
87
|
-
function measureContent(blocks: readonly ContentBlock[]): number {
|
|
88
|
-
let chars = 0
|
|
89
|
-
for (const block of blocks) {
|
|
90
|
-
if (block.type === 'text') chars += codePointLength(block.text)
|
|
91
|
-
}
|
|
92
|
-
return chars
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
function pressureCost(blocks: readonly ContentBlock[]): number {
|
|
96
|
-
let cost = 0
|
|
97
|
-
for (const block of blocks) {
|
|
98
|
-
switch (block.type) {
|
|
99
|
-
case 'text':
|
|
100
|
-
case 'reasoning':
|
|
101
|
-
cost += codePointLength(block.text)
|
|
102
|
-
break
|
|
103
|
-
case 'tool-call':
|
|
104
|
-
cost += RICH_BLOCK_PRESSURE_COST
|
|
105
|
-
+ codePointLength(block.name)
|
|
106
|
-
+ codePointLength(block.arguments)
|
|
107
|
-
break
|
|
108
|
-
default: {
|
|
109
|
-
// ContentBlockMap is merge-extensible. Unknown model-visible blocks
|
|
110
|
-
// scale with their durable JSON payload instead of receiving a fixed
|
|
111
|
-
// token that a large provider block could bypass.
|
|
112
|
-
const serialized = JSON.stringify(block)
|
|
113
|
-
cost += Math.max(RICH_BLOCK_PRESSURE_COST, codePointLength(serialized))
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
return cost
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
// ── Native content pruning ──────────────────────────────────────────────
|
|
121
|
-
|
|
122
|
-
function nativePruneContent(
|
|
123
|
-
blocks: readonly ContentBlock[],
|
|
124
|
-
thresholdChars: number,
|
|
125
|
-
headChars: number,
|
|
126
|
-
tailChars: number,
|
|
127
|
-
marker: string | ((startLine: number) => string) = PRUNE_MARKER,
|
|
128
|
-
): ContentBlock[] | null {
|
|
129
|
-
const totalChars = measureContent(blocks)
|
|
130
|
-
if (totalChars <= thresholdChars) return null
|
|
131
|
-
const markerChars = codePointLength(typeof marker === 'function' ? marker(1) : marker)
|
|
132
|
-
const safeHead = Math.max(0, Math.min(headChars, thresholdChars - markerChars))
|
|
133
|
-
const safeTail = Math.max(0, Math.min(tailChars, thresholdChars - markerChars - safeHead))
|
|
134
|
-
const removedStart = safeHead
|
|
135
|
-
const removedEnd = totalChars - safeTail
|
|
136
|
-
const pruned: ContentBlock[] = []
|
|
137
|
-
let consumed = 0
|
|
138
|
-
let markerInserted = false
|
|
139
|
-
// '\n' count in the RAW event text before the current block (retrieve scans
|
|
140
|
-
// the original event; blocks are joined with '\n'). Line numbers therefore
|
|
141
|
-
// never depend on what the pruning removed.
|
|
142
|
-
let newlinesBefore = 0
|
|
143
|
-
for (const block of blocks) {
|
|
144
|
-
if (block.type !== 'text') {
|
|
145
|
-
pruned.push(block)
|
|
146
|
-
newlinesBefore += 1
|
|
147
|
-
continue
|
|
148
|
-
}
|
|
149
|
-
const points = Array.from(block.text)
|
|
150
|
-
const blockStart = consumed
|
|
151
|
-
const blockEnd = blockStart + points.length
|
|
152
|
-
const headEnd = Math.min(points.length, Math.max(0, removedStart - blockStart))
|
|
153
|
-
const tailStart = Math.min(points.length, Math.max(0, removedEnd - blockStart))
|
|
154
|
-
const intersectsRemoved = blockStart < removedEnd && blockEnd > removedStart
|
|
155
|
-
// The elided region starts on the event line after the retained head.
|
|
156
|
-
const headText = points.slice(0, headEnd).join('')
|
|
157
|
-
const insertion = intersectsRemoved && !markerInserted && typeof marker === 'function'
|
|
158
|
-
? marker(1 + newlinesBefore + headText.split('\n').length - 1)
|
|
159
|
-
: intersectsRemoved && !markerInserted ? marker : ''
|
|
160
|
-
if (insertion !== '') markerInserted = true
|
|
161
|
-
const text = points.slice(0, headEnd).join('') + insertion + points.slice(tailStart).join('')
|
|
162
|
-
if (text !== '') pruned.push({ ...block, text })
|
|
163
|
-
newlinesBefore += block.text.split('\n').length - 1 + 1
|
|
164
|
-
consumed = blockEnd
|
|
165
|
-
}
|
|
166
|
-
if (!markerInserted) return null
|
|
167
|
-
const charsAfter = measureContent(pruned)
|
|
168
|
-
return charsAfter <= thresholdChars && charsAfter < totalChars ? pruned : null
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
// ── Result aggregation ──────────────────────────────────────────────────
|
|
172
|
-
|
|
173
|
-
function summarize(entries: readonly PrunedEntry[]): PruneResult {
|
|
174
|
-
return {
|
|
175
|
-
pruned: entries,
|
|
176
|
-
charsRemoved: entries.reduce((sum, entry) => sum + entry.charsBefore - entry.charsAfter, 0),
|
|
177
|
-
tokensRemoved: entries.reduce((sum, entry) => sum + entry.tokensBefore - entry.tokensAfter, 0),
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
function emptyResult(): PruneResult {
|
|
182
|
-
return { pruned: [], charsRemoved: 0, tokensRemoved: 0 }
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
export {
|
|
186
|
-
onlyTextBlock,
|
|
187
|
-
onlyTextBlocks,
|
|
188
|
-
countToolContent,
|
|
189
|
-
exactTokens,
|
|
190
|
-
sameProviderMeasurementKey,
|
|
191
|
-
unavailableCount,
|
|
192
|
-
recoveryMarker,
|
|
193
|
-
summarize,
|
|
194
|
-
emptyResult,
|
|
195
|
-
measureContent,
|
|
196
|
-
pressureCost,
|
|
197
|
-
nativePruneContent,
|
|
198
|
-
}
|