dsh-context-compression-improved 0.6.1 → 0.6.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/package.json +162 -162
- package/packages/selector/lib/client.d.ts +29 -19
- package/packages/selector/lib/client.js +28 -14
- package/packages/selector/lib/pruner.d.ts +7 -0
- package/packages/selector/lib/pruner.js +20 -33
- package/packages/selector/lib/tail-trim.js +3 -6
- package/packages/selector/package.json +114 -114
- package/packages/selector/src/client/CompressionProfileControls.tsx +2 -2
- package/packages/selector/src/client/CompressionProfileSelector.tsx +1 -1
- package/packages/selector/src/client/index.ts +225 -212
- package/packages/selector/src/client/scope-face.ts +24 -0
- package/packages/selector/src/pruner/content.ts +0 -3
- package/packages/selector/src/pruner/session.ts +4 -2
- package/packages/selector/src/pruner.ts +26 -15
- package/packages/selector/src/runtime/measurement.ts +3 -8
- package/packages/selector/src/runtime/retrieve.ts +3 -3
- package/packages/selector/src/runtime/tail-trim.ts +4 -5
- package/packages/selector/tests/custom-contract.client.spec.ts +222 -222
- package/packages/selector/tests/public/package-contract.client.spec.ts +78 -78
- package/packages/selector/tests/settings-seat.client.spec.ts +165 -118
- package/packages/selector/tests/support/ui-primitives.tsx +34 -34
- package/pnpm-workspace.yaml +42 -42
|
@@ -1,212 +1,225 @@
|
|
|
1
|
-
import type {} from '@deepseek-ai/dsh-client-locale/client'
|
|
2
|
-
import type { Context as ClientContext } from '@deepseek-ai/cordis'
|
|
3
|
-
import type {} from '@deepseek-ai/dsh-client-ui-session/client'
|
|
4
|
-
import type {} from '@deepseek-ai/dsh-client-ui-settings/client'
|
|
5
|
-
import type {} from '@deepseek-ai/dsh-client-ui-workspace/client'
|
|
6
|
-
import {
|
|
7
|
-
isCustomCompressionPolicy,
|
|
8
|
-
ContextCompressionSettingsSection,
|
|
9
|
-
type CompressionSelectorInjected, type ContextCompressionSettings,
|
|
10
|
-
} from './CompressionProfileSelector.tsx'
|
|
11
|
-
import { DEFAULT_CUSTOM_COMPRESSION_POLICY } from '../profiles.ts'
|
|
12
|
-
import { decodeSettings } from './decode.ts'
|
|
13
|
-
import { en, zh } from './locales.ts'
|
|
14
|
-
import
|
|
15
|
-
import {
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* Harness 0.1.5 mounts the web core's `slots` service on the client context
|
|
19
|
-
* but no longer ships a public type for it; declare the face this plugin
|
|
20
|
-
* consumes (same shape dsh-thinking-levels and dsh-prime-memory rely on).
|
|
21
|
-
* `inject` factories may be sync (return a disposer) or generator (yield the
|
|
22
|
-
* registration); slot options are validated at runtime per slot kind, which
|
|
23
|
-
* lets one entry supply both `id` (Desktop, list) and `key` (CLI, keyed).
|
|
24
|
-
*/
|
|
25
|
-
interface SlotsService {
|
|
26
|
-
inject(slot: string, register: () => (() => void) | Generator<() => void>): () => void
|
|
27
|
-
register(options: Record<string, unknown>, component: unknown): () => void
|
|
28
|
-
}
|
|
29
|
-
declare module '@deepseek-ai/cordis' {
|
|
30
|
-
interface Context {
|
|
31
|
-
slots: SlotsService
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
//
|
|
37
|
-
//
|
|
38
|
-
//
|
|
39
|
-
//
|
|
40
|
-
//
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
)
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
&& left.
|
|
74
|
-
&& left.
|
|
75
|
-
&& left.fresh.
|
|
76
|
-
&& left.fresh.
|
|
77
|
-
&& left.
|
|
78
|
-
&& left.aggregate.
|
|
79
|
-
&& left.aggregate.
|
|
80
|
-
&& left.
|
|
81
|
-
&& left.history.
|
|
82
|
-
&& left.history.
|
|
83
|
-
&& left.history.
|
|
84
|
-
&& left.history.
|
|
85
|
-
&& left.
|
|
86
|
-
&& left.tailTrim.
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
//
|
|
94
|
-
// the
|
|
95
|
-
//
|
|
96
|
-
//
|
|
97
|
-
|
|
98
|
-
const
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
),
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
1
|
+
import type {} from '@deepseek-ai/dsh-client-locale/client'
|
|
2
|
+
import type { Context as ClientContext } from '@deepseek-ai/cordis'
|
|
3
|
+
import type {} from '@deepseek-ai/dsh-client-ui-session/client'
|
|
4
|
+
import type {} from '@deepseek-ai/dsh-client-ui-settings/client'
|
|
5
|
+
import type {} from '@deepseek-ai/dsh-client-ui-workspace/client'
|
|
6
|
+
import {
|
|
7
|
+
isCustomCompressionPolicy,
|
|
8
|
+
ContextCompressionSettingsSection,
|
|
9
|
+
type CompressionSelectorInjected, type ContextCompressionSettings,
|
|
10
|
+
} from './CompressionProfileSelector.tsx'
|
|
11
|
+
import { DEFAULT_CUSTOM_COMPRESSION_POLICY } from '../profiles.ts'
|
|
12
|
+
import { decodeSettings } from './decode.ts'
|
|
13
|
+
import { en, zh } from './locales.ts'
|
|
14
|
+
import { planPresetOptionsOps, presetOptionsOpsAccepted } from './preset-options.ts'
|
|
15
|
+
import type { ScopeSnapshot, SettingsScope } from './scope-face.ts'
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Harness 0.1.5 mounts the web core's `slots` service on the client context
|
|
19
|
+
* but no longer ships a public type for it; declare the face this plugin
|
|
20
|
+
* consumes (same shape dsh-thinking-levels and dsh-prime-memory rely on).
|
|
21
|
+
* `inject` factories may be sync (return a disposer) or generator (yield the
|
|
22
|
+
* registration); slot options are validated at runtime per slot kind, which
|
|
23
|
+
* lets one entry supply both `id` (Desktop, list) and `key` (CLI, keyed).
|
|
24
|
+
*/
|
|
25
|
+
interface SlotsService {
|
|
26
|
+
inject(slot: string, register: () => (() => void) | Generator<() => void>): () => void
|
|
27
|
+
register(options: Record<string, unknown>, component: unknown): () => void
|
|
28
|
+
}
|
|
29
|
+
declare module '@deepseek-ai/cordis' {
|
|
30
|
+
interface Context {
|
|
31
|
+
slots: SlotsService
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// Declare all consumed services (the official client-plugin pattern, and the
|
|
36
|
+
// shape dsh-thinking-levels proves on this same 0.1.5 host line): cordis holds
|
|
37
|
+
// apply until `locale` / `configForms` are provided, so registration can use
|
|
38
|
+
// them directly. Resolving them lazily via ctx.get() instead races the settings
|
|
39
|
+
// client's activation — on a loss the apply early-returned and EVERY settings
|
|
40
|
+
// entry silently vanished.
|
|
41
|
+
export const inject = ['slots', 'locale', 'configForms']
|
|
42
|
+
const NS = 'context-compression'
|
|
43
|
+
|
|
44
|
+
/** 0.1.7: the profile entry whose config carries the compression settings doc. */
|
|
45
|
+
const ENTRY_ID = 'context-compression-improved-bundle'
|
|
46
|
+
|
|
47
|
+
/** The configForms face this client consumes (structural; 0.1.7 ui-settings). */
|
|
48
|
+
interface ConfigFormsFace {
|
|
49
|
+
get<T>(entryId: string): {
|
|
50
|
+
getSnapshot(): {
|
|
51
|
+
status: 'loading' | 'ready' | 'unavailable'
|
|
52
|
+
value: T | undefined
|
|
53
|
+
revision: number | undefined
|
|
54
|
+
writable: boolean
|
|
55
|
+
base: unknown
|
|
56
|
+
user: unknown
|
|
57
|
+
mode: 'host' | 'memory'
|
|
58
|
+
}
|
|
59
|
+
subscribe(listener: () => void): () => void
|
|
60
|
+
set(field: string, value: unknown): Promise<boolean>
|
|
61
|
+
unset(field: string): Promise<boolean>
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
function sameCustomPolicy(
|
|
67
|
+
left: ContextCompressionSettings['custom'],
|
|
68
|
+
right: ContextCompressionSettings['custom'],
|
|
69
|
+
): boolean {
|
|
70
|
+
if (left.version !== 3 || right.version !== 3) return false
|
|
71
|
+
return left.version === right.version
|
|
72
|
+
&& left.unit === right.unit
|
|
73
|
+
&& left.prefixPolicy === right.prefixPolicy
|
|
74
|
+
&& left.fresh.enabled === right.fresh.enabled
|
|
75
|
+
&& left.fresh.trigger === right.fresh.trigger
|
|
76
|
+
&& left.fresh.target === right.fresh.target
|
|
77
|
+
&& left.aggregate.enabled === right.aggregate.enabled
|
|
78
|
+
&& left.aggregate.trigger === right.aggregate.trigger
|
|
79
|
+
&& left.aggregate.target === right.aggregate.target
|
|
80
|
+
&& left.history.enabled === right.history.enabled
|
|
81
|
+
&& left.history.trigger === right.history.trigger
|
|
82
|
+
&& left.history.keepRecentToolCalls === right.history.keepRecentToolCalls
|
|
83
|
+
&& left.history.keepRecentTokens === right.history.keepRecentTokens
|
|
84
|
+
&& left.history.minReclaim === right.history.minReclaim
|
|
85
|
+
&& left.tailTrim.enabled === right.tailTrim.enabled
|
|
86
|
+
&& left.tailTrim.trigger === right.tailTrim.trigger
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export function apply(ctx: ClientContext): void {
|
|
90
|
+
ctx.locale.register(NS, { zh, en })
|
|
91
|
+
const injected = (): CompressionSelectorInjected => {
|
|
92
|
+
// 0.1.7: the compression document rides the selector row's entry config as
|
|
93
|
+
// the volatile `settings` field. The form handle is fetched per factory
|
|
94
|
+
// call on the caller's fiber (same activation rule as the old bind); the
|
|
95
|
+
// wrapper below decodes the stored doc and converts per-field writes into
|
|
96
|
+
// whole-doc commits (a volatile object replaces as one snapshot).
|
|
97
|
+
const form = ctx.configForms.get<Record<string, unknown>>(ENTRY_ID)
|
|
98
|
+
const readDoc = (): ContextCompressionSettings | undefined =>
|
|
99
|
+
decodeSettings(form.getSnapshot().value?.settings)
|
|
100
|
+
// useSyncExternalStore requires a getSnapshot that returns a STABLE
|
|
101
|
+
// reference between changes — a fresh object per call is React error #185
|
|
102
|
+
// (maximum update depth exceeded). Cache the projection keyed on the form
|
|
103
|
+
// snapshot's identity: ConfigFormSnapshot is documented stable until the
|
|
104
|
+
// next accepted change.
|
|
105
|
+
let projectedSource: object | undefined
|
|
106
|
+
let projected: ScopeSnapshot<ContextCompressionSettings> = {
|
|
107
|
+
status: 'loading', value: undefined, revision: undefined,
|
|
108
|
+
writable: false, base: undefined, user: undefined, mode: 'host',
|
|
109
|
+
}
|
|
110
|
+
const scope: SettingsScope<ContextCompressionSettings> = {
|
|
111
|
+
getSnapshot() {
|
|
112
|
+
const snap = form.getSnapshot()
|
|
113
|
+
if (snap !== projectedSource) {
|
|
114
|
+
projectedSource = snap
|
|
115
|
+
projected = {
|
|
116
|
+
status: snap.status,
|
|
117
|
+
value: decodeSettings(snap.value?.settings),
|
|
118
|
+
revision: snap.revision,
|
|
119
|
+
writable: snap.writable,
|
|
120
|
+
base: snap.base,
|
|
121
|
+
user: snap.user,
|
|
122
|
+
mode: snap.mode,
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
return projected
|
|
126
|
+
},
|
|
127
|
+
subscribe: (listener) => form.subscribe(listener),
|
|
128
|
+
set: async (field, value) => {
|
|
129
|
+
const next = { ...(readDoc() ?? {}) } as Record<string, unknown>
|
|
130
|
+
next[field] = value
|
|
131
|
+
return form.set('settings', next)
|
|
132
|
+
},
|
|
133
|
+
unset: async (field) => {
|
|
134
|
+
const next = { ...(readDoc() ?? {}) } as Record<string, unknown>
|
|
135
|
+
delete next[field]
|
|
136
|
+
return form.set('settings', next)
|
|
137
|
+
},
|
|
138
|
+
mutate: async (ops) => {
|
|
139
|
+
const doc = { ...(readDoc() ?? {}) } as Record<string, unknown>
|
|
140
|
+
for (const op of ops) {
|
|
141
|
+
const [head, key] = op.path as [string, string]
|
|
142
|
+
if (head !== 'presetOptions') return false
|
|
143
|
+
const section = { ...((doc.presetOptions ?? {}) as Record<string, unknown>) }
|
|
144
|
+
if (op.op === 'unset') delete section[key]
|
|
145
|
+
else section[key] = (op as { value?: unknown }).value
|
|
146
|
+
doc.presetOptions = section
|
|
147
|
+
}
|
|
148
|
+
return form.set('settings', doc)
|
|
149
|
+
},
|
|
150
|
+
}
|
|
151
|
+
const writeAndConfirm = async (
|
|
152
|
+
write: () => Promise<unknown>,
|
|
153
|
+
accepts: (settings: ContextCompressionSettings) => boolean,
|
|
154
|
+
): Promise<void> => {
|
|
155
|
+
const beforeRevision = scope.getSnapshot().revision
|
|
156
|
+
await write()
|
|
157
|
+
const after = scope.getSnapshot()
|
|
158
|
+
if (
|
|
159
|
+
after.status !== 'ready'
|
|
160
|
+
|| after.value === undefined
|
|
161
|
+
|| after.revision === beforeRevision
|
|
162
|
+
|| !accepts(after.value)
|
|
163
|
+
) {
|
|
164
|
+
throw new Error('Context compression settings were not saved.')
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
return {
|
|
168
|
+
hooks: { compression: scope },
|
|
169
|
+
select: profile => writeAndConfirm(
|
|
170
|
+
() => scope.set('profile', profile),
|
|
171
|
+
settings => settings.profile === profile,
|
|
172
|
+
),
|
|
173
|
+
saveCustom: custom => writeAndConfirm(
|
|
174
|
+
() => scope.set('custom', custom),
|
|
175
|
+
settings => isCustomCompressionPolicy(settings.custom)
|
|
176
|
+
&& sameCustomPolicy(settings.custom, custom),
|
|
177
|
+
),
|
|
178
|
+
resetCustom: () => writeAndConfirm(
|
|
179
|
+
() => scope.set('custom', structuredClone(DEFAULT_CUSTOM_COMPRESSION_POLICY)),
|
|
180
|
+
settings => isCustomCompressionPolicy(settings.custom)
|
|
181
|
+
&& sameCustomPolicy(settings.custom, DEFAULT_CUSTOM_COMPRESSION_POLICY),
|
|
182
|
+
),
|
|
183
|
+
saveAutoCompact: thresholdPercent => writeAndConfirm(
|
|
184
|
+
() => scope.set('autoCompact', { thresholdPercent }),
|
|
185
|
+
settings => settings.autoCompact.thresholdPercent === thresholdPercent,
|
|
186
|
+
),
|
|
187
|
+
saveCodeSkeleton: enabled => writeAndConfirm(
|
|
188
|
+
() => scope.set('codeSkeleton', { enabled }),
|
|
189
|
+
settings => settings.codeSkeleton.enabled === enabled,
|
|
190
|
+
),
|
|
191
|
+
savePresetOptions: options => {
|
|
192
|
+
// Path-addressed so one field write cannot delete its siblings: the
|
|
193
|
+
// previous whole-section set erased estimatorMode (and every other
|
|
194
|
+
// override) whenever the user touched a second field.
|
|
195
|
+
const ops = planPresetOptionsOps(scope.getSnapshot().value?.presetOptions, options)
|
|
196
|
+
if (ops.length === 0) return Promise.resolve()
|
|
197
|
+
return writeAndConfirm(
|
|
198
|
+
() => scope.mutate?.(ops) ?? Promise.resolve(false),
|
|
199
|
+
settings => presetOptionsOpsAccepted(settings.presetOptions, ops),
|
|
200
|
+
)
|
|
201
|
+
},
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
// 设置 → 上下文压缩 直挂分节(0.1.1 契约;0.1.5 官方分节也注册在此,未声明槽
|
|
205
|
+
// 的注册会在激活期抛错,故 try/catch 守卫 —— 同 dsh-prime-memory 的双槽冗余)。
|
|
206
|
+
// 只挂这一处:再注册 `settings.plugins.tab` / `settings.plugin.item` 会在
|
|
207
|
+
// 设置里同时出现独立分节和插件卡片,重复展示同一张面板。
|
|
208
|
+
try {
|
|
209
|
+
ctx.slots.inject('settings.section', () => ctx.slots.register({
|
|
210
|
+
name: 'settings.section',
|
|
211
|
+
id: 'context-compression',
|
|
212
|
+
order: 17,
|
|
213
|
+
label: () => ctx.locale.bind(NS)('nav'),
|
|
214
|
+
locale: NS,
|
|
215
|
+
inject: injected,
|
|
216
|
+
}, ContextCompressionSettingsSection))
|
|
217
|
+
} catch (error) {
|
|
218
|
+
console.warn('[dsh-context-compression-improved] settings.section 注册失败(新宿主已收编):', error)
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
export type {
|
|
223
|
+
CompressionProfile, CompressionProfileSelectorProps, CompressionSelectorInjected,
|
|
224
|
+
ContextCompressionSettings,
|
|
225
|
+
} from './CompressionProfileSelector.tsx'
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 0.1.7-rc.2 stopped exporting `SettingsScope` from
|
|
3
|
+
* `@deepseek-ai/dsh-client-ui-settings/client`; the structural face the
|
|
4
|
+
* selector consumes lives here instead (getSnapshot shape mirrors the 0.1.7
|
|
5
|
+
* `ConfigFormSnapshot` minus the parts the components never read).
|
|
6
|
+
*/
|
|
7
|
+
/** The projected snapshot shape (identity-stable between changes — React #185). */
|
|
8
|
+
export interface ScopeSnapshot<T> {
|
|
9
|
+
status: 'loading' | 'ready' | 'unavailable'
|
|
10
|
+
value: T | undefined
|
|
11
|
+
revision: number | undefined
|
|
12
|
+
writable: boolean
|
|
13
|
+
base: unknown
|
|
14
|
+
user: unknown
|
|
15
|
+
mode: 'host' | 'memory'
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface SettingsScope<T> {
|
|
19
|
+
getSnapshot(): ScopeSnapshot<T>
|
|
20
|
+
subscribe(listener: () => void): () => void
|
|
21
|
+
set(field: string, value: unknown): Promise<boolean>
|
|
22
|
+
unset(field: string): Promise<boolean>
|
|
23
|
+
mutate?(ops: readonly { path: readonly string[]; op: string; value?: unknown }[]): Promise<boolean>
|
|
24
|
+
}
|
|
@@ -105,9 +105,6 @@ function pressureCost(blocks: readonly ContentBlock[]): number {
|
|
|
105
105
|
+ codePointLength(block.name)
|
|
106
106
|
+ codePointLength(block.arguments)
|
|
107
107
|
break
|
|
108
|
-
case 'tool-result':
|
|
109
|
-
cost += RICH_BLOCK_PRESSURE_COST + pressureCost(block.content)
|
|
110
|
-
break
|
|
111
108
|
default: {
|
|
112
109
|
// ContentBlockMap is merge-extensible. Unknown model-visible blocks
|
|
113
110
|
// scale with their durable JSON payload instead of receiving a fixed
|
|
@@ -73,8 +73,10 @@ function tokenizerAuditFact(route: { provider: string, model: string }): { token
|
|
|
73
73
|
|
|
74
74
|
/** Check whether a snapshot candidate represents an error result. */
|
|
75
75
|
function isError(candidate: SnapshotCandidate): boolean {
|
|
76
|
-
|
|
77
|
-
|
|
76
|
+
// 0.1.7-rc.2: tool results are first-class `tool`-role messages — `isError`
|
|
77
|
+
// moved from the result block onto the message itself.
|
|
78
|
+
const message = candidate.event.data.message as { isError?: boolean }
|
|
79
|
+
return message.isError === true || candidate.event.data.error !== undefined
|
|
78
80
|
}
|
|
79
81
|
|
|
80
82
|
/** Wrap a plan list into a HistoryPlanOutcome. */
|
|
@@ -14,6 +14,15 @@ import z from '@deepseek-ai/schemastery'
|
|
|
14
14
|
import { createUserMessage, freezeMessage } from '@deepseek-ai/dsh-llm'
|
|
15
15
|
import type { ContentBlock, UserMessage } from '@deepseek-ai/dsh-llm'
|
|
16
16
|
import { SessionSeq } from '@deepseek-ai/dsh-session'
|
|
17
|
+
|
|
18
|
+
// 0.1.7-rc.2 removed the shared 'plugin' source kind — each producer declares
|
|
19
|
+
// its own kind in its own module (official pattern: compaction's
|
|
20
|
+
// 'compact-checkpoint' in compaction/src/index.ts).
|
|
21
|
+
declare module '@deepseek-ai/dsh-llm' {
|
|
22
|
+
interface MessageSourceMap {
|
|
23
|
+
'dsh-context-compression': { readonly kind: 'dsh-context-compression' }
|
|
24
|
+
}
|
|
25
|
+
}
|
|
17
26
|
import type { Session, SessionEvent, ToolResultMessage } from '@deepseek-ai/dsh-session'
|
|
18
27
|
import type {} from '@deepseek-ai/dsh-agent'
|
|
19
28
|
import type {} from '@deepseek-ai/dsh-compaction'
|
|
@@ -535,7 +544,7 @@ export class ToolResultPruner extends Service {
|
|
|
535
544
|
// carrying the marker would fail the host's closed-transaction validation.
|
|
536
545
|
const replacement = createUserMessage({
|
|
537
546
|
content,
|
|
538
|
-
source: { kind: '
|
|
547
|
+
source: { kind: 'dsh-context-compression' },
|
|
539
548
|
})
|
|
540
549
|
session.append('user/message', replacement, {
|
|
541
550
|
surfaceOp: { op: 'replace', startSeq: SessionSeq(checkpointSeq), endSeq: SessionSeq(checkpointSeq) },
|
|
@@ -1325,11 +1334,11 @@ export class ToolResultPruner extends Service {
|
|
|
1325
1334
|
if (shadowedHeuristicTokenCount === undefined) {
|
|
1326
1335
|
throw new Error(`surface node ${String(seq)} is absent from the atomic legacy projection`)
|
|
1327
1336
|
}
|
|
1328
|
-
const content = event.data.message.content
|
|
1337
|
+
const content = event.data.message.content
|
|
1329
1338
|
candidates.push({
|
|
1330
1339
|
seq,
|
|
1331
1340
|
event,
|
|
1332
|
-
call: calls.get(event.data.message.
|
|
1341
|
+
call: calls.get(event.data.message.toolCallId) ?? { name: 'unknown', arguments: '{}' },
|
|
1333
1342
|
count: onlyTextBlocks(content) === null
|
|
1334
1343
|
? unavailableCount(`surface node ${String(seq)} contains unsupported rich tool-result content`)
|
|
1335
1344
|
: measured.get(seq) ?? unavailableCount(`surface node ${String(seq)} is absent from the atomic token view`),
|
|
@@ -1349,7 +1358,7 @@ export class ToolResultPruner extends Service {
|
|
|
1349
1358
|
): PlannedReplacement | null {
|
|
1350
1359
|
if (this.isRecoveryExempt(session, candidate)) return null
|
|
1351
1360
|
if (candidate.characterPressure <= charsForTokens(policy.nativeTriggerTokens)) return null
|
|
1352
|
-
const result = candidate.event.data.message
|
|
1361
|
+
const result = candidate.event.data.message as ToolResultMessage
|
|
1353
1362
|
if (onlyTextBlocks(result.content) === null) return null
|
|
1354
1363
|
const sourceSeq = rootToolResultSeq(session, candidate.seq)
|
|
1355
1364
|
// R9b site: the marker's retrieve hint starts at the event line right
|
|
@@ -1401,7 +1410,7 @@ export class ToolResultPruner extends Service {
|
|
|
1401
1410
|
view: CompactionTokenView,
|
|
1402
1411
|
): PlannedReplacement | null {
|
|
1403
1412
|
if (typeof candidate.event.surfaceOp === 'object') return null
|
|
1404
|
-
const result = candidate.event.data.message
|
|
1413
|
+
const result = candidate.event.data.message as ToolResultMessage
|
|
1405
1414
|
const text = flattenPlainText(result.content)
|
|
1406
1415
|
if (text === undefined) return null
|
|
1407
1416
|
if (candidate.characterPressure <= charsForTokens(policy.freshTriggerTokens)) return null
|
|
@@ -1449,7 +1458,7 @@ export class ToolResultPruner extends Service {
|
|
|
1449
1458
|
// pre-step coordinate filter prevents previously-kept originals from ever
|
|
1450
1459
|
// being reconsidered after their first request.
|
|
1451
1460
|
if (typeof candidate.event.surfaceOp === 'object') return null
|
|
1452
|
-
const result = candidate.event.data.message
|
|
1461
|
+
const result = candidate.event.data.message as ToolResultMessage
|
|
1453
1462
|
if (candidate.characterPressure <= charsForTokens(policy.freshTriggerTokens)) return null
|
|
1454
1463
|
const sourceSeq = candidate.seq
|
|
1455
1464
|
const sourceRef = sourceRefFn(session, sourceSeq)
|
|
@@ -1527,7 +1536,7 @@ export class ToolResultPruner extends Service {
|
|
|
1527
1536
|
// example one carrying an image) must never reach it. The character basis
|
|
1528
1537
|
// no longer inherits the exact-tokenizer precondition that used to reject
|
|
1529
1538
|
// this path implicitly, so the guard has to be explicit.
|
|
1530
|
-
const redacted = candidate.event.data.message
|
|
1539
|
+
const redacted = candidate.event.data.message as ToolResultMessage
|
|
1531
1540
|
if (onlyTextBlocks(redacted.content) === null) return null
|
|
1532
1541
|
const text = [
|
|
1533
1542
|
'[Tool result reduced to satisfy the completed-step aggregate budget]',
|
|
@@ -1561,7 +1570,7 @@ export class ToolResultPruner extends Service {
|
|
|
1561
1570
|
historyMode?: HistoryMode,
|
|
1562
1571
|
): PlannedReplacement | null {
|
|
1563
1572
|
if (!isError(candidate)) return null
|
|
1564
|
-
const result = candidate.event.data.message
|
|
1573
|
+
const result = candidate.event.data.message as ToolResultMessage
|
|
1565
1574
|
const blocks = onlyTextBlocks(result.content)
|
|
1566
1575
|
if (blocks === null) return null
|
|
1567
1576
|
const text = blocks.map(block => block.text).join('\n')
|
|
@@ -1619,7 +1628,7 @@ export class ToolResultPruner extends Service {
|
|
|
1619
1628
|
const protectedSeqs = this.protectedHistoryCandidateSeqs(candidates, policy)
|
|
1620
1629
|
const isUnsafe = (candidate: SnapshotCandidate): boolean => {
|
|
1621
1630
|
if (this.isRecoveryExempt(session, candidate)) return true
|
|
1622
|
-
const result = candidate.event.data.message
|
|
1631
|
+
const result = candidate.event.data.message as ToolResultMessage
|
|
1623
1632
|
const block = onlyTextBlock(result.content)
|
|
1624
1633
|
return block?.text.includes('[Old tool result content cleared from active context]') === true
|
|
1625
1634
|
}
|
|
@@ -1649,7 +1658,7 @@ export class ToolResultPruner extends Service {
|
|
|
1649
1658
|
? minReclaimChars
|
|
1650
1659
|
: required
|
|
1651
1660
|
for (const candidate of eligible) {
|
|
1652
|
-
const result = candidate.event.data.message
|
|
1661
|
+
const result = candidate.event.data.message as ToolResultMessage
|
|
1653
1662
|
const block = onlyTextBlock(result.content)
|
|
1654
1663
|
// TokenPilot-inspired R2: a read output whose file was later mutated is
|
|
1655
1664
|
// superseded — its text can no longer match the file — so it takes the
|
|
@@ -1858,11 +1867,11 @@ export class ToolResultPruner extends Service {
|
|
|
1858
1867
|
if (event?.type !== 'tool/result'
|
|
1859
1868
|
|| event.data.turn !== assistant.data.turn || event.data.step !== assistant.data.step
|
|
1860
1869
|
|| event.data.error !== undefined) return true
|
|
1861
|
-
const block = event.data.message
|
|
1870
|
+
const block = event.data.message as ToolResultMessage
|
|
1862
1871
|
if (block.isError === true) return true
|
|
1863
1872
|
// Images and other rich inner blocks stay fail-open: a TailTrim stub
|
|
1864
1873
|
// would silently delete them from the active context.
|
|
1865
|
-
return block.content.some(contentBlock => contentBlock.type !== 'text')
|
|
1874
|
+
return block.content.some((contentBlock: ContentBlock) => contentBlock.type !== 'text')
|
|
1866
1875
|
})) continue
|
|
1867
1876
|
const next = events[nodes[index + 1 + calls.length] ?? -1]
|
|
1868
1877
|
if (next?.type === 'tool/result'
|
|
@@ -2049,7 +2058,7 @@ export class ToolResultPruner extends Service {
|
|
|
2049
2058
|
// noise without reclaiming context. Text-level because the placeholder
|
|
2050
2059
|
// guidance lines (source refs, retrieval hints) must pay for themselves.
|
|
2051
2060
|
if (options.noNetSavingsGuard === true) {
|
|
2052
|
-
const originalBlocks = onlyTextBlocks(candidate.event.data.message.content
|
|
2061
|
+
const originalBlocks = onlyTextBlocks(candidate.event.data.message.content)
|
|
2053
2062
|
const replacementBlocks = onlyTextBlocks(content)
|
|
2054
2063
|
if (originalBlocks !== null && replacementBlocks !== null) {
|
|
2055
2064
|
const originalChars = originalBlocks.reduce((sum, block) => sum + codePointLength(block.text), 0)
|
|
@@ -2078,10 +2087,12 @@ export class ToolResultPruner extends Service {
|
|
|
2078
2087
|
|
|
2079
2088
|
private land(session: Session, plan: PlannedReplacement): PrunedEntry | null {
|
|
2080
2089
|
const { candidate } = plan
|
|
2081
|
-
const result = candidate.event.data.message
|
|
2090
|
+
const result = candidate.event.data.message as ToolResultMessage
|
|
2091
|
+
// 0.1.7-rc.2: the tool message's own `content` is the result block set —
|
|
2092
|
+
// no wrapping result block anymore.
|
|
2082
2093
|
const message = freezeMessage<ToolResultMessage>({
|
|
2083
2094
|
...candidate.event.data.message,
|
|
2084
|
-
content:
|
|
2095
|
+
content: plan.content,
|
|
2085
2096
|
})
|
|
2086
2097
|
const manifest = session.append('compaction/prune', {
|
|
2087
2098
|
shadowedRange: { start: SessionSeq(candidate.seq), end: SessionSeq(candidate.seq) },
|