@xbzb404/dsh-context-compression-selector-runtime 0.0.0-stage → 0.2.1-rc.3
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/LICENSE +21 -0
- package/README.md +13 -3
- package/README.zh.md +13 -0
- package/THIRD_PARTY_NOTICES.md +36 -0
- package/assets/deepseek-v4/LICENSE.DeepSeek-V4-Pro.txt +21 -0
- package/assets/deepseek-v4/manifest.json +22 -0
- package/assets/deepseek-v4/tokenizer.json +267359 -0
- package/assets/deepseek-v4/tokenizer_config.json +34 -0
- package/assets/deepseek-v4.1-flash/LICENSE.DeepSeek-V4.1-Flash.txt +21 -0
- package/assets/deepseek-v4.1-flash/manifest.json +24 -0
- package/assets/deepseek-v4.1-flash/tokenizer.json +267359 -0
- package/assets/deepseek-v4.1-flash/tokenizer_config.json +34 -0
- package/lib/index.d.ts +581 -0
- package/lib/index.js +3871 -0
- package/lib/invariant.d.ts +10 -0
- package/lib/invariant.js +70 -0
- package/lib/tail-trim.js +142 -0
- package/package.json +67 -4
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
{
|
|
2
|
+
"add_bos_token": false,
|
|
3
|
+
"add_eos_token": false,
|
|
4
|
+
"bos_token": {
|
|
5
|
+
"__type": "AddedToken",
|
|
6
|
+
"content": "<|begin▁of▁sentence|>",
|
|
7
|
+
"lstrip": false,
|
|
8
|
+
"normalized": true,
|
|
9
|
+
"rstrip": false,
|
|
10
|
+
"single_word": false
|
|
11
|
+
},
|
|
12
|
+
"clean_up_tokenization_spaces": false,
|
|
13
|
+
"eos_token": {
|
|
14
|
+
"__type": "AddedToken",
|
|
15
|
+
"content": "<|end▁of▁sentence|>",
|
|
16
|
+
"lstrip": false,
|
|
17
|
+
"normalized": true,
|
|
18
|
+
"rstrip": false,
|
|
19
|
+
"single_word": false
|
|
20
|
+
},
|
|
21
|
+
"legacy": true,
|
|
22
|
+
"model_max_length": 1048576,
|
|
23
|
+
"pad_token": {
|
|
24
|
+
"__type": "AddedToken",
|
|
25
|
+
"content": "<|end▁of▁sentence|>",
|
|
26
|
+
"lstrip": false,
|
|
27
|
+
"normalized": true,
|
|
28
|
+
"rstrip": false,
|
|
29
|
+
"single_word": false
|
|
30
|
+
},
|
|
31
|
+
"sp_model_kwargs": {},
|
|
32
|
+
"unk_token": null,
|
|
33
|
+
"tokenizer_class": "PreTrainedTokenizerFast"
|
|
34
|
+
}
|
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,581 @@
|
|
|
1
|
+
import { Context, Service } from "@deepseek-ai/cordis";
|
|
2
|
+
import z from "@deepseek-ai/schemastery";
|
|
3
|
+
import { ContentBlock, ToolCallId } from "@deepseek-ai/dsh-llm";
|
|
4
|
+
import { Session } from "@deepseek-ai/dsh-session";
|
|
5
|
+
import { TokenMeasurement } from "@deepseek-ai/dsh-token-meter";
|
|
6
|
+
//#region src/types.d.ts
|
|
7
|
+
/** User-facing mixed strategy profile. */
|
|
8
|
+
declare const COMPRESSION_PROFILES: readonly ["off", "native", "balanced", "cache-strict", "savings", "adaptive", "custom"];
|
|
9
|
+
/** Public compression strategy selected for one Session. */
|
|
10
|
+
type CompressionProfile = typeof COMPRESSION_PROFILES[number];
|
|
11
|
+
/** When historical tool results may be aged for one Session policy. */
|
|
12
|
+
type HistoryMode = 'disabled' | 'routine' | 'capacity-pressure' | 'adaptive';
|
|
13
|
+
/** Canonical unit stored by one versioned Custom policy. */
|
|
14
|
+
type CustomCompressionUnit = 'tokens' | 'context-percent';
|
|
15
|
+
/** Whether routine History may rewrite a previously sent Harness prefix. */
|
|
16
|
+
type CustomPrefixPolicy = 'preserve' | 'pressure-break';
|
|
17
|
+
/** One independently selectable Custom Fresh or Aggregate stage. */
|
|
18
|
+
interface CustomCompressionBudget {
|
|
19
|
+
enabled: boolean;
|
|
20
|
+
trigger: number;
|
|
21
|
+
target: number;
|
|
22
|
+
}
|
|
23
|
+
/** Legacy Custom History gate and turn/token working-set protection. */
|
|
24
|
+
interface LegacyCustomHistoryPolicy {
|
|
25
|
+
enabled: boolean;
|
|
26
|
+
trigger: number;
|
|
27
|
+
keepRecentTurns: number;
|
|
28
|
+
keepRecent: number;
|
|
29
|
+
minReclaim: number;
|
|
30
|
+
}
|
|
31
|
+
/** Custom History gate and recent tool-call/token working-set protection. */
|
|
32
|
+
interface CustomHistoryPolicy {
|
|
33
|
+
enabled: boolean;
|
|
34
|
+
trigger: number;
|
|
35
|
+
keepRecentToolCalls: number;
|
|
36
|
+
keepRecentTokens: number;
|
|
37
|
+
minReclaim: number;
|
|
38
|
+
}
|
|
39
|
+
/** Custom-only experimental TailTrim gate. */
|
|
40
|
+
interface CustomTailTrimPolicy {
|
|
41
|
+
enabled: boolean;
|
|
42
|
+
trigger: number;
|
|
43
|
+
}
|
|
44
|
+
/** Common user-authored Custom stages shared by persisted policy versions. */
|
|
45
|
+
interface CustomCompressionPolicyFields<HistoryPolicy> {
|
|
46
|
+
unit: CustomCompressionUnit;
|
|
47
|
+
fresh: CustomCompressionBudget;
|
|
48
|
+
aggregate: CustomCompressionBudget;
|
|
49
|
+
history: HistoryPolicy;
|
|
50
|
+
prefixPolicy: CustomPrefixPolicy;
|
|
51
|
+
}
|
|
52
|
+
/** Legacy R4 Custom policy, accepted without migration. */
|
|
53
|
+
interface CustomCompressionPolicyV1 extends CustomCompressionPolicyFields<LegacyCustomHistoryPolicy> {
|
|
54
|
+
version: 1;
|
|
55
|
+
}
|
|
56
|
+
/** R5 Custom policy with an explicit default-off TailTrim stage. */
|
|
57
|
+
interface CustomCompressionPolicyV2 extends CustomCompressionPolicyFields<LegacyCustomHistoryPolicy> {
|
|
58
|
+
version: 2;
|
|
59
|
+
tailTrim: CustomTailTrimPolicy;
|
|
60
|
+
}
|
|
61
|
+
/** Custom policy with tool-call working-set protection. */
|
|
62
|
+
interface CustomCompressionPolicyV3 extends CustomCompressionPolicyFields<CustomHistoryPolicy> {
|
|
63
|
+
version: 3;
|
|
64
|
+
tailTrim: CustomTailTrimPolicy;
|
|
65
|
+
}
|
|
66
|
+
/** Strict persisted Custom policy union. */
|
|
67
|
+
type CustomCompressionPolicy = CustomCompressionPolicyV1 | CustomCompressionPolicyV2 | CustomCompressionPolicyV3;
|
|
68
|
+
/** User-tunable Auto Compact coordination preferences. */
|
|
69
|
+
interface AutoCompactSettings {
|
|
70
|
+
/** Routed-context percentage that triggers model-driven Auto Compact. */
|
|
71
|
+
thresholdPercent: number;
|
|
72
|
+
}
|
|
73
|
+
/** Durable global preference exposed through `ctx.settings`. */
|
|
74
|
+
interface ContextCompressionSettings {
|
|
75
|
+
/** Default strategy snapped when a Session first reaches the pruner. */
|
|
76
|
+
profile: CompressionProfile;
|
|
77
|
+
/** Versioned Custom policy snapped with `profile` for a newly observed Session. */
|
|
78
|
+
custom: CustomCompressionPolicy;
|
|
79
|
+
/** Auto Compact trigger preference snapped with `profile` for a newly observed Session. */
|
|
80
|
+
autoCompact: AutoCompactSettings;
|
|
81
|
+
}
|
|
82
|
+
/** Token-gated policy with character fields limited to reducer candidate shape. */
|
|
83
|
+
interface ToolResultPruneConfig {
|
|
84
|
+
/** Composition fallback when Host settings are unavailable. Defaults to `balanced`. */
|
|
85
|
+
profile?: CompressionProfile;
|
|
86
|
+
/**
|
|
87
|
+
* Versioned Custom policy written by the settings form and snapped with
|
|
88
|
+
* `profile` for a newly observed Session. Defaults to the Balanced-equivalent
|
|
89
|
+
* Custom document.
|
|
90
|
+
*/
|
|
91
|
+
custom?: CustomCompressionPolicy;
|
|
92
|
+
/** Native fallback leading Unicode code points. Defaults to `4096`. */
|
|
93
|
+
headChars?: number;
|
|
94
|
+
/** Native fallback trailing Unicode code points. Defaults to `1024`. */
|
|
95
|
+
tailChars?: number;
|
|
96
|
+
/** Native original-content token trigger. Profile default when omitted. */
|
|
97
|
+
nativeTriggerTokens?: number;
|
|
98
|
+
/** Native replacement token target. Profile default when omitted. */
|
|
99
|
+
nativeTargetTokens?: number;
|
|
100
|
+
/** Fresh-result exact-token trigger. Profile default when omitted. */
|
|
101
|
+
freshTriggerTokens?: number;
|
|
102
|
+
/** Maximum fresh-result exact-token replacement size. Profile default when omitted. */
|
|
103
|
+
freshTargetTokens?: number;
|
|
104
|
+
/** Combined completed-step token pressure that starts aggregate reduction. */
|
|
105
|
+
aggregateTriggerTokens?: number;
|
|
106
|
+
/** Aggregate token target after completed-step pressure exceeds its trigger. */
|
|
107
|
+
aggregateTargetTokens?: number;
|
|
108
|
+
/** Total live tool-result tokens that permit historical aging. Profile default when omitted. */
|
|
109
|
+
historyTriggerTokens?: number;
|
|
110
|
+
/** Recent completed agent tool calls protected from historical aging. Profile default when omitted. */
|
|
111
|
+
historyKeepRecentToolCalls?: number;
|
|
112
|
+
/** Recent tool-result token tail protected in addition to tool calls. Profile default when omitted. */
|
|
113
|
+
historyKeepRecentTokens?: number;
|
|
114
|
+
/** Minimum reclaim required before historical aging is worth a cache break. Profile default when omitted. */
|
|
115
|
+
historyMinReclaimTokens?: number;
|
|
116
|
+
/**
|
|
117
|
+
* Auto Compact threshold percent frozen into this deployment by the preset
|
|
118
|
+
* overlay generation (50–90 integer). When present it supersedes the live
|
|
119
|
+
* Host setting so one generation never splits Auto Compact and micro
|
|
120
|
+
* compact across two thresholds.
|
|
121
|
+
*/
|
|
122
|
+
autoCompactThresholdPercent?: number;
|
|
123
|
+
}
|
|
124
|
+
/** Resolved per-profile behavior. */
|
|
125
|
+
interface CompressionPolicy {
|
|
126
|
+
readonly profile: CompressionProfile;
|
|
127
|
+
/** Whether this Session may use the selector's native-style head/middle/tail reducer. */
|
|
128
|
+
readonly nativeToolResultEnabled: boolean;
|
|
129
|
+
readonly freshEnabled: boolean;
|
|
130
|
+
readonly aggregateEnabled: boolean;
|
|
131
|
+
readonly historyMode: HistoryMode;
|
|
132
|
+
readonly nativeTriggerTokens: number;
|
|
133
|
+
readonly nativeTargetTokens: number;
|
|
134
|
+
readonly freshTriggerTokens: number;
|
|
135
|
+
readonly freshTargetTokens: number;
|
|
136
|
+
readonly aggregateTriggerTokens: number;
|
|
137
|
+
readonly aggregateTargetTokens: number;
|
|
138
|
+
readonly historyTriggerTokens: number;
|
|
139
|
+
readonly historyKeepRecentToolCalls: number;
|
|
140
|
+
readonly historyKeepRecentTokens: number;
|
|
141
|
+
readonly historyMinReclaimTokens: number;
|
|
142
|
+
/**
|
|
143
|
+
* Auto Compact token watermark `A = floor(C × a)` when the standard-profile
|
|
144
|
+
* History linkage resolved for this Session; absent for Custom, Off, Native,
|
|
145
|
+
* or unresolved routed capacity.
|
|
146
|
+
*/
|
|
147
|
+
readonly autoCompactTokens?: number;
|
|
148
|
+
/**
|
|
149
|
+
* Micro-compact last-chance watermark `D = floor(A × 0.875)`. Absent when
|
|
150
|
+
* {@link autoCompactTokens} is absent; capacity-pressure gates fall back to
|
|
151
|
+
* the fixed 0.7 routed-context ratio in that case.
|
|
152
|
+
*/
|
|
153
|
+
readonly microDeadlineTokens?: number;
|
|
154
|
+
/** Present for Custom v3; standard profiles and legacy Custom policies carry no TailTrim policy. */
|
|
155
|
+
readonly tailTrim?: {
|
|
156
|
+
readonly enabled: boolean;
|
|
157
|
+
readonly triggerTokens: number;
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
/** Validated, detached, deeply immutable configuration. */
|
|
161
|
+
interface ResolvedConfig {
|
|
162
|
+
readonly profile: CompressionProfile;
|
|
163
|
+
/** Versioned Custom policy snapped with `profile` for newly observed Sessions. */
|
|
164
|
+
readonly custom: CustomCompressionPolicy;
|
|
165
|
+
readonly headChars: number;
|
|
166
|
+
readonly tailChars: number;
|
|
167
|
+
readonly nativeTriggerTokens?: number;
|
|
168
|
+
readonly nativeTargetTokens?: number;
|
|
169
|
+
readonly freshTriggerTokens?: number;
|
|
170
|
+
readonly freshTargetTokens?: number;
|
|
171
|
+
readonly aggregateTriggerTokens?: number;
|
|
172
|
+
readonly aggregateTargetTokens?: number;
|
|
173
|
+
readonly historyTriggerTokens?: number;
|
|
174
|
+
readonly historyKeepRecentToolCalls?: number;
|
|
175
|
+
readonly historyKeepRecentTokens?: number;
|
|
176
|
+
readonly historyMinReclaimTokens?: number;
|
|
177
|
+
/**
|
|
178
|
+
* Auto Compact threshold percent frozen into this deployment by the preset
|
|
179
|
+
* overlay generation (50-90 integer). Supersedes the live Host setting.
|
|
180
|
+
*/
|
|
181
|
+
readonly autoCompactThresholdPercent?: number;
|
|
182
|
+
}
|
|
183
|
+
/** Why a pruning pass runs. */
|
|
184
|
+
type PruneStage = 'fresh' | 'pressure';
|
|
185
|
+
/** Optional control over one pruning pass. */
|
|
186
|
+
interface PruneSessionOptions {
|
|
187
|
+
/** `fresh` only reduces never-before-seen oversized results; `pressure` may age older results too. */
|
|
188
|
+
stage?: PruneStage;
|
|
189
|
+
/** Routed model context capacity used only to resolve a context-percent Custom snapshot. */
|
|
190
|
+
contextWindowTokens?: number;
|
|
191
|
+
/** Proposed turn at the pre-step boundary. Used with `freshStep` to freeze keep/reduce decisions. */
|
|
192
|
+
freshTurn?: number;
|
|
193
|
+
/** The immediately preceding completed step whose tool results have not yet entered a model request. */
|
|
194
|
+
freshStep?: number;
|
|
195
|
+
}
|
|
196
|
+
/** Cited source event and size accounting for one landed surface replacement. */
|
|
197
|
+
interface PrunedEntry {
|
|
198
|
+
/** Current surface event shadowed by this replacement. */
|
|
199
|
+
readonly originalSeq: number;
|
|
200
|
+
/** Root full-fidelity source event used in the recovery reference. */
|
|
201
|
+
readonly sourceSeq: number;
|
|
202
|
+
/** Newly appended compressed tool-result event. */
|
|
203
|
+
readonly replacementSeq: number;
|
|
204
|
+
/** Tool call shared by the original and replacement. */
|
|
205
|
+
readonly callId: ToolCallId;
|
|
206
|
+
/** Reducer or aging strategy that produced the replacement. */
|
|
207
|
+
readonly reducer: string;
|
|
208
|
+
/** Pass stage that landed the replacement. */
|
|
209
|
+
readonly stage: PruneStage;
|
|
210
|
+
/** Original deterministic pressure cost. */
|
|
211
|
+
readonly charsBefore: number;
|
|
212
|
+
/** Replacement deterministic pressure cost. */
|
|
213
|
+
readonly charsAfter: number;
|
|
214
|
+
/** Authoritative exact canonical content tokens before replacement. */
|
|
215
|
+
readonly tokensBefore: number;
|
|
216
|
+
/** Authoritative exact canonical content tokens after replacement. */
|
|
217
|
+
readonly tokensAfter: number;
|
|
218
|
+
}
|
|
219
|
+
/** Aggregate outcome of one stable-surface pruning pass. */
|
|
220
|
+
interface PruneResult {
|
|
221
|
+
/** Replacements in landing order. */
|
|
222
|
+
readonly pruned: readonly PrunedEntry[];
|
|
223
|
+
/** Total deterministic pressure cost removed across replacements. */
|
|
224
|
+
readonly charsRemoved: number;
|
|
225
|
+
/** Authoritative exact canonical content tokens removed. */
|
|
226
|
+
readonly tokensRemoved: number;
|
|
227
|
+
}
|
|
228
|
+
//#endregion
|
|
229
|
+
//#region src/custom-policy.d.ts
|
|
230
|
+
/**
|
|
231
|
+
* JSON-survivable union of every Custom document a Host form may carry.
|
|
232
|
+
*
|
|
233
|
+
* A Host Config field that a browser edits must survive the round trip the
|
|
234
|
+
* settings surface performs: the Host serves the field's form schema as
|
|
235
|
+
* `schema.toJSON()` and the client rehydrates it to validate the value it was
|
|
236
|
+
* served. {@link CustomCompressionPolicySchema} cannot survive that trip — a
|
|
237
|
+
* schemastery `transform` serializes its `transform` type and inner schema but
|
|
238
|
+
* never its callback, so the rehydrated node throws `callback is not a function`
|
|
239
|
+
* on the very document it is meant to accept, the client's form never leaves
|
|
240
|
+
* `loading`, and every control rendering from it stays disabled.
|
|
241
|
+
*
|
|
242
|
+
* This union validates the same three input shapes with no callback, so it is
|
|
243
|
+
* the schema a live-editable field may expose. The transform stays the gate for
|
|
244
|
+
* values the runtime resolves itself.
|
|
245
|
+
*/
|
|
246
|
+
declare const CustomCompressionPolicyInputSchema: z<CustomCompressionPolicy>;
|
|
247
|
+
/** Canonical Custom document accepted by Host settings and the runtime resolver. */
|
|
248
|
+
declare const CustomCompressionPolicySchema: z<CustomCompressionPolicy>;
|
|
249
|
+
/** Balanced-equivalent Custom policy stored as one token-canonical document. */
|
|
250
|
+
declare const DEFAULT_CUSTOM_COMPRESSION_POLICY: CustomCompressionPolicyV3;
|
|
251
|
+
/** Routed model facts needed only by context-percent Custom documents. */
|
|
252
|
+
interface CustomPolicyResolutionOptions {
|
|
253
|
+
/** Positive resolved shared model context capacity. */
|
|
254
|
+
readonly contextWindowTokens?: number;
|
|
255
|
+
/**
|
|
256
|
+
* Frozen Auto Compact threshold percent. Standard profiles use it to link
|
|
257
|
+
* History watermarks to the Auto Compact level; Custom resolution ignores it
|
|
258
|
+
* because Custom stays explicit-token manual.
|
|
259
|
+
*/
|
|
260
|
+
readonly autoCompactThresholdPercent?: number;
|
|
261
|
+
}
|
|
262
|
+
/**
|
|
263
|
+
* Resolve one validated Custom document to the same token policy used by public presets.
|
|
264
|
+
* @param value - untrusted or typed Custom settings value.
|
|
265
|
+
* @param options - routed model capacity for context-percent documents.
|
|
266
|
+
* @returns a detached deeply immutable effective token policy.
|
|
267
|
+
*/
|
|
268
|
+
declare function resolveCustomPolicy(value: CustomCompressionPolicy, options?: CustomPolicyResolutionOptions): CompressionPolicy;
|
|
269
|
+
//#endregion
|
|
270
|
+
//#region src/config.d.ts
|
|
271
|
+
/** Settings namespace shared by the Host service and browser selector. */
|
|
272
|
+
declare const CONTEXT_COMPRESSION_SETTINGS_NAMESPACE = "context-compression";
|
|
273
|
+
/** Fixed native fallback marker. */
|
|
274
|
+
declare const PRUNE_MARKER = "\n\n[... tool result middle pruned ...]\n\n";
|
|
275
|
+
/**
|
|
276
|
+
* The one Auto Compact threshold contract shared by the settings UI, the
|
|
277
|
+
* persisted settings schema, and the runtime resolver. Every integer in the
|
|
278
|
+
* range is valid and entered directly in the UI.
|
|
279
|
+
*/
|
|
280
|
+
declare const AUTO_COMPACT_THRESHOLD_LIMITS: {
|
|
281
|
+
readonly min: 50;
|
|
282
|
+
readonly max: 90;
|
|
283
|
+
readonly step: 1;
|
|
284
|
+
readonly default: 80;
|
|
285
|
+
};
|
|
286
|
+
/** Narrow one untrusted value to a valid Auto Compact threshold percent. */
|
|
287
|
+
declare function isValidAutoCompactThresholdPercent(value: unknown): value is number;
|
|
288
|
+
/**
|
|
289
|
+
* Parse one settings document with the persisted-section semantics: `undefined`
|
|
290
|
+
* inherits the defaults (an absent section), while `null` is an explicitly
|
|
291
|
+
* invalid document and must never silently become the default policy.
|
|
292
|
+
*/
|
|
293
|
+
declare function parseContextCompressionSettings(value: unknown): ContextCompressionSettings;
|
|
294
|
+
/** Settings schema used by the user-facing profile selector. */
|
|
295
|
+
declare const ContextCompressionSettingsSchema: z<ContextCompressionSettings>;
|
|
296
|
+
/** Low-friction defaults; token budgets live in resolved profile policy. */
|
|
297
|
+
declare const DEFAULTS: ResolvedConfig;
|
|
298
|
+
/**
|
|
299
|
+
* Count Unicode code points without splitting surrogate pairs.
|
|
300
|
+
* @param text - text whose code points are counted.
|
|
301
|
+
* @returns the number of Unicode code points.
|
|
302
|
+
*/
|
|
303
|
+
declare function codePointLength(text: string): number;
|
|
304
|
+
/**
|
|
305
|
+
* Test whether a settings value names a supported compression profile.
|
|
306
|
+
* @param value - untrusted settings value.
|
|
307
|
+
* @returns whether the value is a supported compression profile.
|
|
308
|
+
*/
|
|
309
|
+
declare function isCompressionProfile(value: unknown): value is CompressionProfile;
|
|
310
|
+
/**
|
|
311
|
+
* Resolve and validate plugin configuration.
|
|
312
|
+
* @param config - optional composition overrides.
|
|
313
|
+
* @returns a detached, deeply immutable configuration snapshot.
|
|
314
|
+
*/
|
|
315
|
+
declare function resolveConfig(config?: ToolResultPruneConfig): ResolvedConfig;
|
|
316
|
+
/**
|
|
317
|
+
* Resolve one public profile into a complete mixed-strategy policy.
|
|
318
|
+
* @param config - validated composition configuration.
|
|
319
|
+
* @param profile - profile frozen for the target Session.
|
|
320
|
+
* @param custom - versioned Custom document used only by the `custom` profile.
|
|
321
|
+
* @param options - routed capacity and the frozen Auto Compact threshold used
|
|
322
|
+
* to resolve context-percent Custom values and standard-profile linkage.
|
|
323
|
+
* @returns the effective deterministic compression policy.
|
|
324
|
+
*/
|
|
325
|
+
declare function resolvePolicy(config: ResolvedConfig, profile: CompressionProfile, custom?: CustomCompressionPolicy, options?: CustomPolicyResolutionOptions): CompressionPolicy;
|
|
326
|
+
//#endregion
|
|
327
|
+
//#region src/reducers.d.ts
|
|
328
|
+
/** Deterministic, evidence-backed reducers for fresh tool results. */
|
|
329
|
+
/** Input shared by every fresh-result reducer. */
|
|
330
|
+
interface ReducerInput {
|
|
331
|
+
readonly toolName: string;
|
|
332
|
+
readonly argumentsText: string;
|
|
333
|
+
readonly text: string;
|
|
334
|
+
readonly budgetChars: number;
|
|
335
|
+
readonly sourceRef: string;
|
|
336
|
+
readonly isError: boolean;
|
|
337
|
+
}
|
|
338
|
+
/** One verified reducer candidate. */
|
|
339
|
+
interface ReducerOutput {
|
|
340
|
+
readonly text: string;
|
|
341
|
+
readonly reducer: string;
|
|
342
|
+
readonly lossy: boolean;
|
|
343
|
+
}
|
|
344
|
+
/**
|
|
345
|
+
* Select a reducer from verified tool, command, and content evidence.
|
|
346
|
+
* @param input - original result text, recovery source, and output budget.
|
|
347
|
+
* @returns a verified candidate, or `null` when every reducer fails open.
|
|
348
|
+
*/
|
|
349
|
+
declare function reduceFreshToolResult(input: ReducerInput): ReducerOutput | null;
|
|
350
|
+
/**
|
|
351
|
+
* Build a recoverable placeholder for an old tool result.
|
|
352
|
+
* @param input - tool identity, source reference, size, status, and retained evidence.
|
|
353
|
+
* @returns a lossy placeholder that cites the immutable source event.
|
|
354
|
+
*/
|
|
355
|
+
declare function historicalPlaceholder(input: {
|
|
356
|
+
readonly toolName: string;
|
|
357
|
+
readonly sourceRef: string;
|
|
358
|
+
readonly charsBefore: number;
|
|
359
|
+
readonly isError: boolean;
|
|
360
|
+
readonly text: string;
|
|
361
|
+
readonly compact?: boolean;
|
|
362
|
+
}): ReducerOutput;
|
|
363
|
+
/**
|
|
364
|
+
* Validate shrinkage, budget, recovery, and error retention.
|
|
365
|
+
* @param input - original reducer input and its safety requirements.
|
|
366
|
+
* @param output - candidate reduced text and reducer metadata.
|
|
367
|
+
* @returns whether the candidate is safe to land.
|
|
368
|
+
*/
|
|
369
|
+
declare function verifyReduction(input: ReducerInput, output: ReducerOutput): boolean;
|
|
370
|
+
/**
|
|
371
|
+
* Strip ANSI, collapse carriage-return progress redraws, and fold exact repeats.
|
|
372
|
+
* @param text - raw terminal output.
|
|
373
|
+
* @returns normalized terminal text.
|
|
374
|
+
*/
|
|
375
|
+
declare function normalizeTerminalText(text: string): string;
|
|
376
|
+
//#endregion
|
|
377
|
+
//#region src/token-count.d.ts
|
|
378
|
+
/** Token counts owned by the standalone compression runtime. */
|
|
379
|
+
/** Exact count of one canonical value under a pinned tokenizer artifact. */
|
|
380
|
+
interface ExactTokenizerTokenCount {
|
|
381
|
+
readonly kind: 'exact-tokenizer';
|
|
382
|
+
readonly tokens: number;
|
|
383
|
+
readonly tokenizerId: string;
|
|
384
|
+
readonly tokenizerRevision: string;
|
|
385
|
+
}
|
|
386
|
+
/** A value that the bundled tokenizer cannot safely count. */
|
|
387
|
+
interface UnavailableTokenCount {
|
|
388
|
+
readonly kind: 'unavailable';
|
|
389
|
+
readonly reason: string;
|
|
390
|
+
}
|
|
391
|
+
/** Best-effort estimate carrying a conservative upper value when available. */
|
|
392
|
+
interface TokenizerEstimateTokenCount {
|
|
393
|
+
readonly kind: 'tokenizer-estimate';
|
|
394
|
+
readonly tokens: number;
|
|
395
|
+
readonly upperBoundTokens: number;
|
|
396
|
+
readonly estimatorId: string;
|
|
397
|
+
readonly estimatorRevision: string;
|
|
398
|
+
readonly calibration?: Readonly<{
|
|
399
|
+
readonly sampleCount: number;
|
|
400
|
+
readonly conservativeMarginTokens: number;
|
|
401
|
+
}>;
|
|
402
|
+
}
|
|
403
|
+
/** Exact, conservative request estimate, or an explicit refusal. */
|
|
404
|
+
type TokenCount = ExactTokenizerTokenCount | TokenizerEstimateTokenCount | UnavailableTokenCount;
|
|
405
|
+
//#endregion
|
|
406
|
+
//#region src/measurement.d.ts
|
|
407
|
+
/** Request identity retained only when every dimension is publicly known. */
|
|
408
|
+
interface ProviderMeasurementKey {
|
|
409
|
+
readonly provider: string;
|
|
410
|
+
readonly baseUrlClass: string;
|
|
411
|
+
readonly apiRoute: string;
|
|
412
|
+
readonly modelId: string;
|
|
413
|
+
readonly requestTemplateRevision: string;
|
|
414
|
+
readonly tokenizerRevision: string;
|
|
415
|
+
readonly modality: string;
|
|
416
|
+
}
|
|
417
|
+
/** Rich request observation used by Adaptive when a future public API supplies it. */
|
|
418
|
+
interface ObservedPromptUsage {
|
|
419
|
+
readonly attemptId: string;
|
|
420
|
+
readonly providerRequestOrdinal: number;
|
|
421
|
+
readonly startedAtMs: number;
|
|
422
|
+
readonly completedAtMs: number;
|
|
423
|
+
readonly measurement: TokenCount;
|
|
424
|
+
readonly observedPromptTokens: number;
|
|
425
|
+
readonly observedOutputTokens?: number;
|
|
426
|
+
readonly responseModelId?: string;
|
|
427
|
+
readonly cacheStatus?: 'complete' | 'unknown';
|
|
428
|
+
readonly cacheReadTokens?: number;
|
|
429
|
+
readonly cacheMissTokens?: number;
|
|
430
|
+
readonly key?: ProviderMeasurementKey;
|
|
431
|
+
}
|
|
432
|
+
/**
|
|
433
|
+
* Intrinsic-grid diagnostic attached to nodes whose count estimates images.
|
|
434
|
+
* It reports ONLY the official V4.1 block arithmetic evaluated on the
|
|
435
|
+
* attachment's intrinsic dimensions. Under V4.1 the block length
|
|
436
|
+
* (`nLlmH * (nLlmW + 1) + 2`) is a single position-independent value, so
|
|
437
|
+
* `paddingMinimumTokens === paddingMaximumTokens`; the two fields are retained
|
|
438
|
+
* for shape stability with earlier revisions. It is
|
|
439
|
+
* NOT a request-token bound: the adapter may still re-project the image
|
|
440
|
+
* (per-route pixel-budget or image-detail overrides, byte-cap reprojection),
|
|
441
|
+
* which can move the real count below this value. It never participates in
|
|
442
|
+
* exact gates, rewrite proofs, or any lossy decision.
|
|
443
|
+
*/
|
|
444
|
+
interface IntrinsicImageBlockDiagnostic {
|
|
445
|
+
readonly paddingMinimumTokens: number;
|
|
446
|
+
readonly paddingMaximumTokens: number;
|
|
447
|
+
}
|
|
448
|
+
/** One same-revision surface node with exact, estimated, or unavailable count. */
|
|
449
|
+
interface MeasuredTokenSurfaceNode {
|
|
450
|
+
readonly seq: number;
|
|
451
|
+
readonly count: TokenCount;
|
|
452
|
+
/** Intrinsic-grid diagnostic when usable image dimensions were available. */
|
|
453
|
+
readonly intrinsicImageBlockEstimate?: IntrinsicImageBlockDiagnostic;
|
|
454
|
+
}
|
|
455
|
+
/** Compression view derived only from published Session and TokenMeter methods. */
|
|
456
|
+
interface CompactionTokenView extends TokenMeasurement {
|
|
457
|
+
readonly providerRoute?: string;
|
|
458
|
+
readonly modelId?: string;
|
|
459
|
+
readonly measuredNodes: readonly MeasuredTokenSurfaceNode[];
|
|
460
|
+
readonly currentSurface: TokenCount;
|
|
461
|
+
/** Sum of per-node intrinsic padding minima; a diagnostic, not a token bound. */
|
|
462
|
+
readonly intrinsicImageBlockEstimateTokens: number;
|
|
463
|
+
readonly latestEnvelopeKey?: ProviderMeasurementKey;
|
|
464
|
+
readonly lastCompletedUsage?: ObservedPromptUsage;
|
|
465
|
+
countCanonicalText(text: string): TokenCount;
|
|
466
|
+
}
|
|
467
|
+
/**
|
|
468
|
+
* Capture one route-bound view without calling patched Harness methods.
|
|
469
|
+
* Official `measure()` remains authoritative for request pressure; the bundled
|
|
470
|
+
* tokenizer supplies exact canonical content counts used by safe rewrites.
|
|
471
|
+
*/
|
|
472
|
+
declare function measureForCompaction(ctx: Context, session: Session): CompactionTokenView;
|
|
473
|
+
//#endregion
|
|
474
|
+
//#region src/index.d.ts
|
|
475
|
+
declare module '@deepseek-ai/cordis' {
|
|
476
|
+
interface Context {
|
|
477
|
+
toolResultPruner: ToolResultPruner;
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
/** Mixed deterministic selector behind the existing `ctx.toolResultPruner` seam. */
|
|
481
|
+
declare class ToolResultPruner extends Service {
|
|
482
|
+
static inject: string[];
|
|
483
|
+
static Config: z<ToolResultPruneConfig>;
|
|
484
|
+
/** Resolved immutable deployment configuration. */
|
|
485
|
+
readonly config: ResolvedConfig;
|
|
486
|
+
/** Complete canonical setting document frozen when each Session first reaches this root service. */
|
|
487
|
+
private readonly sessionSettings;
|
|
488
|
+
/** Original result seqs whose first-exposure KEEP/REDUCE decision has committed. */
|
|
489
|
+
private readonly firstExposure;
|
|
490
|
+
/** Runtime prerequisite warnings deduplicated per Session and failure key. */
|
|
491
|
+
private readonly warnedFailures;
|
|
492
|
+
/** Last Adaptive postflight attempt emitted per Session; keeps diagnostics bounded and independent. */
|
|
493
|
+
private readonly postflightDiagnostics;
|
|
494
|
+
/** Current pre-step chain identity, shared by this producer and downstream compaction-basic. */
|
|
495
|
+
private readonly activeRequestBoundaries;
|
|
496
|
+
/** Boundary identity that already attempted one fully preflighted TailTrim publication. */
|
|
497
|
+
private readonly tailTrimBoundaryAttempts;
|
|
498
|
+
/** Last effective policy audit key emitted for each Session. */
|
|
499
|
+
private readonly policyResolutionAudits;
|
|
500
|
+
constructor(ctx: Context, config?: ToolResultPruneConfig);
|
|
501
|
+
/**
|
|
502
|
+
* Measure text content in Unicode code points; non-text blocks cost zero.
|
|
503
|
+
* @param blocks - tool-result content to measure.
|
|
504
|
+
* @returns total Unicode code points across text blocks.
|
|
505
|
+
*/
|
|
506
|
+
measureContent(blocks: readonly ContentBlock[]): number;
|
|
507
|
+
private pressureCost;
|
|
508
|
+
/**
|
|
509
|
+
* Apply the configured native head/middle/tail transform.
|
|
510
|
+
* @param blocks - original tool-result content.
|
|
511
|
+
* @returns reduced content, or `null` when no reduction is required.
|
|
512
|
+
*/
|
|
513
|
+
pruneContent(blocks: readonly ContentBlock[]): ContentBlock[] | null;
|
|
514
|
+
/**
|
|
515
|
+
* Run one stable-surface pass. `fresh` is invoked before every request and
|
|
516
|
+
* only reduces original oversized results. `pressure` is called by
|
|
517
|
+
* compaction-basic and may additionally age old results at one high-water.
|
|
518
|
+
* @param session - session whose current tool-result surface may be rewritten.
|
|
519
|
+
* @param options - pass stage and optional completed-step coordinates.
|
|
520
|
+
* @returns landed replacements and aggregate Unicode-code-point savings.
|
|
521
|
+
*/
|
|
522
|
+
pruneSession(session: Session, options?: PruneSessionOptions): PruneResult;
|
|
523
|
+
private activeSettings;
|
|
524
|
+
private activePolicy;
|
|
525
|
+
/** Routed provider/model when the durable request header names one route. */
|
|
526
|
+
private routeAuditFact;
|
|
527
|
+
/** Bundled tokenizer identity for one route, when the route is eligible. */
|
|
528
|
+
private tokenizerAuditFact;
|
|
529
|
+
private contextWindowForRequest;
|
|
530
|
+
private runRequestBoundary;
|
|
531
|
+
/** Resolve historical-aging authority without accepting caller-supplied elevation. */
|
|
532
|
+
private historyAllowed;
|
|
533
|
+
/**
|
|
534
|
+
* Match the compaction-basic pressure gate using public durable data. The
|
|
535
|
+
* frozen Auto Compact deadline `D = floor(A x 0.875)` replaces the legacy
|
|
536
|
+
* fixed 0.7 ratio once the standard-profile linkage resolved; without
|
|
537
|
+
* linkage the 0.7 ratio is the documented fallback and reproduces the
|
|
538
|
+
* previous behavior.
|
|
539
|
+
*/
|
|
540
|
+
private capacityPressureActive;
|
|
541
|
+
/** Emit one bounded, independently correlatable postflight cost diagnostic per completed attempt. */
|
|
542
|
+
private logAdaptivePostflight;
|
|
543
|
+
/** Decide one already-planned History batch from adjacent request-level facts only. */
|
|
544
|
+
private adaptiveHistoryAllowed;
|
|
545
|
+
private latestCompletedToolStep;
|
|
546
|
+
private decisions;
|
|
547
|
+
private decideFreshStep;
|
|
548
|
+
private snapshot;
|
|
549
|
+
private planNative;
|
|
550
|
+
private planFresh;
|
|
551
|
+
private planAggregate;
|
|
552
|
+
/** Preserve bounded diagnostic evidence whenever an all-text error is reduced. */
|
|
553
|
+
private planErrorEvidence;
|
|
554
|
+
private isError;
|
|
555
|
+
private historyOutcome;
|
|
556
|
+
private planHistoricalAging;
|
|
557
|
+
private protectedHistoryResultSeqs;
|
|
558
|
+
/** Select the newest completed tool calls and token tail for History-derived stages. */
|
|
559
|
+
private protectedHistoryCandidateSeqs;
|
|
560
|
+
/** Atomically replace at most one oldest safe completed tool-call group. */
|
|
561
|
+
private landOldestTailTrimGroup;
|
|
562
|
+
private reserveTailTrimBoundaryAttempt;
|
|
563
|
+
private uniqueAppendRoot;
|
|
564
|
+
private plan;
|
|
565
|
+
private land;
|
|
566
|
+
private landAll;
|
|
567
|
+
private hasRecoveryTool;
|
|
568
|
+
private auditHistoryEvaluation;
|
|
569
|
+
private auditComponent;
|
|
570
|
+
private auditFailure;
|
|
571
|
+
private auditPublicationFailure;
|
|
572
|
+
private warnExactUnavailable;
|
|
573
|
+
private warnOnce;
|
|
574
|
+
/** Surface replacements are durable turn work; reject before writing the audit half. */
|
|
575
|
+
private hasOpenTurn;
|
|
576
|
+
private rootToolResultSeq;
|
|
577
|
+
private sourceRef;
|
|
578
|
+
private nativePruneContent;
|
|
579
|
+
}
|
|
580
|
+
//#endregion
|
|
581
|
+
export { AUTO_COMPACT_THRESHOLD_LIMITS, type AutoCompactSettings, COMPRESSION_PROFILES, CONTEXT_COMPRESSION_SETTINGS_NAMESPACE, type CompactionTokenView, type CompressionPolicy, type CompressionProfile, type ContextCompressionSettings, ContextCompressionSettingsSchema, type CustomCompressionBudget, type CustomCompressionPolicy, CustomCompressionPolicyInputSchema, CustomCompressionPolicySchema, type CustomCompressionPolicyV1, type CustomCompressionPolicyV2, type CustomCompressionPolicyV3, type CustomCompressionUnit, type CustomHistoryPolicy, type CustomPolicyResolutionOptions, type CustomPrefixPolicy, type CustomTailTrimPolicy, DEFAULTS, DEFAULT_CUSTOM_COMPRESSION_POLICY, type HistoryMode, type MeasuredTokenSurfaceNode, PRUNE_MARKER, type PruneResult, type PruneSessionOptions, type PruneStage, type PrunedEntry, type ResolvedConfig, type ToolResultPruneConfig, ToolResultPruner, ToolResultPruner as default, codePointLength, historicalPlaceholder, isCompressionProfile, isValidAutoCompactThresholdPercent, measureForCompaction, normalizeTerminalText, parseContextCompressionSettings, reduceFreshToolResult, resolveConfig, resolveCustomPolicy, resolvePolicy, verifyReduction };
|