dsh-context-compression-improved 0.5.6 → 0.5.8

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.
Files changed (183) hide show
  1. package/.gitattributes +2 -2
  2. package/.githooks/pre-push +37 -37
  3. package/.github/workflows/ci.yml +39 -39
  4. package/CHANGELOG.ja.md +169 -169
  5. package/CHANGELOG.ko.md +167 -167
  6. package/CHANGELOG.md +323 -304
  7. package/CHANGELOG.zh.md +152 -152
  8. package/CONTRIBUTING.md +22 -22
  9. package/LICENSE +21 -21
  10. package/README.ja.md +85 -85
  11. package/README.ko.md +85 -85
  12. package/README.md +94 -94
  13. package/README.zh.md +85 -85
  14. package/SECURITY.md +18 -18
  15. package/THIRD_PARTY_NOTICES.md +14 -14
  16. package/docs/compat-0.1.5-report.md +77 -77
  17. package/docs/installation.ja.md +78 -78
  18. package/docs/installation.ko.md +78 -78
  19. package/docs/installation.md +103 -103
  20. package/docs/installation.zh.md +100 -100
  21. package/docs/repair-log.md +657 -657
  22. package/eslint.config.js +30 -30
  23. package/package.json +163 -164
  24. package/packages/selector/LICENSE +21 -21
  25. package/packages/selector/README.md +26 -26
  26. package/packages/selector/README.zh.md +24 -24
  27. package/packages/selector/THIRD_PARTY_NOTICES.md +32 -32
  28. package/packages/selector/cordis.patch.yml +27 -27
  29. package/packages/selector/docs/history-tool-call-working-set-spec.md +112 -112
  30. package/packages/selector/docs/native-tool-result-selector-spec.md +34 -34
  31. package/packages/selector/docs/subagent-cache-reuse-spec.md +46 -46
  32. package/packages/selector/dsh.plugin.json +16 -16
  33. package/packages/selector/lib/client.d.ts +59 -0
  34. package/packages/selector/lib/client.js +1738 -10
  35. package/packages/selector/lib/{advisor-state.js → config.js} +816 -65
  36. package/packages/selector/lib/index.d.ts +13 -1
  37. package/packages/selector/lib/index.js +295 -3
  38. package/packages/selector/lib/pruner.d.ts +65 -0
  39. package/packages/selector/lib/pruner.js +497 -262
  40. package/packages/selector/lib/style.css +88 -46
  41. package/packages/selector/package.json +114 -114
  42. package/packages/selector/screenshots.json +3 -3
  43. package/packages/selector/src/client/CompressionProfileControls.tsx +293 -226
  44. package/packages/selector/src/client/CompressionProfileSelector.module.css +199 -170
  45. package/packages/selector/src/client/CompressionProfileSelector.tsx +85 -83
  46. package/packages/selector/src/client/CustomPolicyEditor.tsx +216 -216
  47. package/packages/selector/src/client/EstimatorControls.tsx +277 -277
  48. package/packages/selector/src/client/decode.ts +57 -49
  49. package/packages/selector/src/client/index.ts +240 -149
  50. package/packages/selector/src/client/locales/de.ts +141 -0
  51. package/packages/selector/src/client/locales/es.ts +141 -0
  52. package/packages/selector/src/client/locales/fr.ts +141 -0
  53. package/packages/selector/src/client/locales/it.ts +141 -0
  54. package/packages/selector/src/client/locales/ja.ts +141 -0
  55. package/packages/selector/src/client/locales/ko.ts +141 -0
  56. package/packages/selector/src/client/locales/ru.ts +141 -0
  57. package/packages/selector/src/client/locales.ts +282 -196
  58. package/packages/selector/src/client/monitor-fab.ts +336 -0
  59. package/packages/selector/src/client/preset-options.ts +88 -88
  60. package/packages/selector/src/client/savings-card.tsx +150 -0
  61. package/packages/selector/src/client/settings-section.tsx +140 -124
  62. package/packages/selector/src/css-modules.d.ts +6 -6
  63. package/packages/selector/src/deepseek-v4-tokenizer.ts +205 -205
  64. package/packages/selector/src/estimator-catalog.ts +104 -104
  65. package/packages/selector/src/index.ts +673 -463
  66. package/packages/selector/src/invariant.ts +113 -113
  67. package/packages/selector/src/preset-overlay.ts +626 -626
  68. package/packages/selector/src/profiles.ts +431 -394
  69. package/packages/selector/src/pruner/content.ts +201 -201
  70. package/packages/selector/src/pruner/session.ts +94 -94
  71. package/packages/selector/src/pruner/state.ts +57 -50
  72. package/packages/selector/src/pruner/tuning.ts +23 -23
  73. package/packages/selector/src/pruner/types.ts +78 -78
  74. package/packages/selector/src/pruner.ts +2736 -2402
  75. package/packages/selector/src/runtime/adaptive-cost.ts +205 -205
  76. package/packages/selector/src/runtime/audit.ts +303 -286
  77. package/packages/selector/src/runtime/config.ts +756 -706
  78. package/packages/selector/src/runtime/custom-policy.ts +278 -278
  79. package/packages/selector/src/runtime/deepseek-official-pricing.ts +298 -298
  80. package/packages/selector/src/runtime/deepseek-v4-vision-tokens.ts +254 -254
  81. package/packages/selector/src/runtime/measurement.ts +436 -436
  82. package/packages/selector/src/runtime/monitor.ts +235 -0
  83. package/packages/selector/src/runtime/reducers.ts +1674 -1674
  84. package/packages/selector/src/runtime/retrieve.ts +479 -457
  85. package/packages/selector/src/runtime/savings.ts +337 -0
  86. package/packages/selector/src/runtime/session-events.ts +30 -30
  87. package/packages/selector/src/runtime/summary-command.ts +127 -0
  88. package/packages/selector/src/runtime/tail-trim.ts +166 -166
  89. package/packages/selector/src/runtime/token-count.ts +72 -72
  90. package/packages/selector/src/runtime/tokenpilot/advisor-prompt.ts +233 -188
  91. package/packages/selector/src/runtime/tokenpilot/advisor-state.ts +204 -149
  92. package/packages/selector/src/runtime/tokenpilot/advisor.ts +419 -419
  93. package/packages/selector/src/runtime/tokenpilot/benefit.ts +200 -200
  94. package/packages/selector/src/runtime/tokenpilot/dedup.ts +81 -81
  95. package/packages/selector/src/runtime/tokenpilot/estimator.ts +151 -151
  96. package/packages/selector/src/runtime/tokenpilot/intent-fold.ts +53 -0
  97. package/packages/selector/src/runtime/tokenpilot/intent-gate.ts +65 -0
  98. package/packages/selector/src/runtime/tokenpilot/intent-input-mask.ts +125 -0
  99. package/packages/selector/src/runtime/tokenpilot/intent-range.ts +37 -0
  100. package/packages/selector/src/runtime/tokenpilot/locator.ts +128 -128
  101. package/packages/selector/src/runtime/tokenpilot/read-state.ts +98 -98
  102. package/packages/selector/src/runtime/tokenpilot/sidechannel.ts +303 -303
  103. package/packages/selector/src/runtime/toolclass.ts +103 -103
  104. package/packages/selector/src/runtime/types.ts +365 -346
  105. package/packages/selector/src/runtime/value.ts +48 -48
  106. package/packages/selector/tests/advisor-report.host.spec.ts +223 -223
  107. package/packages/selector/tests/auto-compact.client.spec.tsx +214 -213
  108. package/packages/selector/tests/built/client-artifact.spec.ts +59 -59
  109. package/packages/selector/tests/cache-prefix-audit.spec.ts +123 -123
  110. package/packages/selector/tests/code-skeleton.client.spec.ts +91 -90
  111. package/packages/selector/tests/custom-contract.client.spec.ts +205 -204
  112. package/packages/selector/tests/estimator-catalog.spec.ts +70 -70
  113. package/packages/selector/tests/estimator-channel.client.spec.tsx +253 -252
  114. package/packages/selector/tests/estimator-route-registration.host.spec.ts +194 -188
  115. package/packages/selector/tests/host-preset-overlay.host.spec.ts +206 -206
  116. package/packages/selector/tests/monitor-fab.client.spec.ts +138 -0
  117. package/packages/selector/tests/preset-options-write.client.spec.ts +207 -206
  118. package/packages/selector/tests/preset-overlay-loader.e2e.host.spec.ts +201 -201
  119. package/packages/selector/tests/preset-overlay.host.spec.ts +246 -246
  120. package/packages/selector/tests/profiles.client.spec.tsx +436 -434
  121. package/packages/selector/tests/public/package-contract.client.spec.ts +78 -78
  122. package/packages/selector/tests/runtime/adaptive-cost.spec.ts +167 -167
  123. package/packages/selector/tests/runtime/advice-never-withholds.host.spec.ts +232 -232
  124. package/packages/selector/tests/runtime/advisor-invariant.spec.ts +272 -272
  125. package/packages/selector/tests/runtime/advisor.spec.ts +226 -226
  126. package/packages/selector/tests/runtime/audit.spec.ts +275 -274
  127. package/packages/selector/tests/runtime/auto-compact-config.spec.ts +523 -523
  128. package/packages/selector/tests/runtime/char-basis.spec.ts +30 -30
  129. package/packages/selector/tests/runtime/code-skeleton.spec.ts +152 -152
  130. package/packages/selector/tests/runtime/deepseek-official-pricing.spec.ts +186 -186
  131. package/packages/selector/tests/runtime/deepseek-v4-tokenizer.spec.ts +122 -122
  132. package/packages/selector/tests/runtime/deepseek-v4-vision-tokens.spec.ts +122 -122
  133. package/packages/selector/tests/runtime/deprecated-preset-options.spec.ts +96 -96
  134. package/packages/selector/tests/runtime/fixtures/profile-baseline.json +273 -273
  135. package/packages/selector/tests/runtime/fixtures/tokenizer-golden.json +106 -106
  136. package/packages/selector/tests/runtime/fixtures/vision-golden.json +459 -459
  137. package/packages/selector/tests/runtime/frequency-longstrings.spec.ts +74 -74
  138. package/packages/selector/tests/runtime/html-reducer.spec.ts +212 -212
  139. package/packages/selector/tests/runtime/intent-gate.spec.ts +62 -0
  140. package/packages/selector/tests/runtime/intent-summary.spec.ts +156 -0
  141. package/packages/selector/tests/runtime/line-mapping.spec.ts +153 -153
  142. package/packages/selector/tests/runtime/monitor.spec.ts +301 -0
  143. package/packages/selector/tests/runtime/prose-reducers.spec.ts +133 -133
  144. package/packages/selector/tests/runtime/public/public-runtime.spec.ts +2724 -2724
  145. package/packages/selector/tests/runtime/read-input-cap.spec.ts +33 -33
  146. package/packages/selector/tests/runtime/savings.spec.ts +120 -0
  147. package/packages/selector/tests/runtime/search-reducer.spec.ts +110 -110
  148. package/packages/selector/tests/runtime/session-events.spec.ts +27 -27
  149. package/packages/selector/tests/runtime/sidechannel.spec.ts +241 -241
  150. package/packages/selector/tests/runtime/summary-command.spec.ts +154 -0
  151. package/packages/selector/tests/runtime/toc-and-bundled.spec.ts +159 -159
  152. package/packages/selector/tests/runtime/tokenizer-golden.spec.ts +53 -53
  153. package/packages/selector/tests/runtime/tokenpilot/benefit.spec.ts +217 -217
  154. package/packages/selector/tests/runtime/tokenpilot/dedup.spec.ts +52 -52
  155. package/packages/selector/tests/runtime/tokenpilot/estimator.spec.ts +79 -79
  156. package/packages/selector/tests/runtime/tokenpilot/locator.spec.ts +76 -76
  157. package/packages/selector/tests/runtime/tokenpilot/profile-baseline.spec.ts +116 -116
  158. package/packages/selector/tests/runtime/tokenpilot/read-state.spec.ts +82 -82
  159. package/packages/selector/tests/runtime/toolclass.spec.ts +156 -156
  160. package/packages/selector/tests/runtime/value.spec.ts +23 -23
  161. package/packages/selector/tests/settings-seat.client.spec.ts +130 -118
  162. package/packages/selector/tests/standing-generation.host.spec.ts +697 -697
  163. package/packages/selector/tests/subagent-cache-reuse.host.spec.ts +253 -253
  164. package/packages/selector/tests/support/cache-prefix-audit.ts +105 -105
  165. package/packages/selector/tests/support/mock-adapter.ts +37 -37
  166. package/packages/selector/tests/support/ui-primitives.tsx +34 -34
  167. package/packages/selector/tsconfig.json +11 -11
  168. package/packages/selector/tsdown.client.config.ts +102 -102
  169. package/packages/selector/tsdown.config.ts +20 -20
  170. package/pnpm-workspace.yaml +66 -66
  171. package/scripts/capture-profile-baseline.mjs +42 -42
  172. package/scripts/generate-tokenizer-fixtures.py +81 -81
  173. package/scripts/generate-vision-fixtures.py +208 -208
  174. package/scripts/packed-components-smoke.mjs +732 -732
  175. package/scripts/packed-install-e2e.mjs +1273 -1273
  176. package/scripts/toolclass-corpus-replay.mjs +281 -281
  177. package/scripts/verify-release.mjs +318 -317
  178. package/tests/TEST_INVENTORY.md +41 -41
  179. package/tsconfig.base.json +18 -18
  180. package/tsconfig.json +7 -7
  181. package/tsconfig.tests.json +15 -15
  182. package/vitest.built.config.ts +9 -9
  183. package/vitest.config.ts +43 -43
@@ -1,49 +1,57 @@
1
- /** Browser-safe settings decoding shared by the client entry and node tests. */
2
-
3
- import {
4
- canonicalizeCustomPolicy,
5
- COMPRESSION_PROFILES,
6
- decodeAutoCompactSettings,
7
- decodeCodeSkeletonSettings,
8
- decodePresetOptionsSettings,
9
- isCustomCompressionPolicy,
10
- isPlainRecord,
11
- type CompressionProfile,
12
- type ContextCompressionSettings,
13
- } from '../profiles.ts'
14
-
15
- /**
16
- * Decode one stored context-compression settings document with exactly the
17
- * runtime schema's strictness: a plain object with only `profile`, `custom`,
18
- * `autoCompact`, and `codeSkeleton` keys, a supported profile, a valid Custom
19
- * document canonicalized to v3 exactly as the runtime resolver would, a
20
- * strictly-shaped autoCompact section (absent inherits the 80% default), and
21
- * a strictly-shaped codeSkeleton gate (absent inherits off). Anything else
22
- * decodes to `undefined` so the UI reports the document as unreadable instead
23
- * of silently disagreeing with the runtime.
24
- */
25
- export function decodeSettings(value: unknown): ContextCompressionSettings | undefined {
26
- if (!isPlainRecord(value)) return undefined
27
- const keys = Object.keys(value)
28
- if (keys.some(key => key !== 'profile' && key !== 'custom' && key !== 'autoCompact' && key !== 'codeSkeleton' && key !== 'presetOptions')) {
29
- return undefined
30
- }
31
- const profile = (value as { profile?: unknown }).profile
32
- const custom = (value as { custom?: unknown }).custom
33
- const autoCompact = decodeAutoCompactSettings((value as { autoCompact?: unknown }).autoCompact)
34
- const codeSkeleton = decodeCodeSkeletonSettings((value as { codeSkeleton?: unknown }).codeSkeleton)
35
- const presetOptions = decodePresetOptionsSettings((value as { presetOptions?: unknown }).presetOptions)
36
- return typeof profile === 'string'
37
- && (COMPRESSION_PROFILES as readonly string[]).includes(profile)
38
- && isCustomCompressionPolicy(custom)
39
- && autoCompact !== undefined
40
- && codeSkeleton !== undefined
41
- ? {
42
- profile: profile as CompressionProfile,
43
- custom: canonicalizeCustomPolicy(custom),
44
- autoCompact,
45
- codeSkeleton,
46
- ...presetOptions === undefined ? {} : { presetOptions },
47
- }
48
- : undefined
49
- }
1
+ /** Browser-safe settings decoding shared by the client entry and node tests. */
2
+
3
+ import {
4
+ canonicalizeCustomPolicy,
5
+ COMPRESSION_PROFILES,
6
+ decodeAutoCompactSettings,
7
+ decodeCodeSkeletonSettings,
8
+ decodeIntentSummarySettings,
9
+ decodeMonitorPanelSettings,
10
+ decodePresetOptionsSettings,
11
+ isCustomCompressionPolicy,
12
+ isPlainRecord,
13
+ type CompressionProfile,
14
+ type ContextCompressionSettings,
15
+ } from '../profiles.ts'
16
+
17
+ /**
18
+ * Decode one stored context-compression settings document with exactly the
19
+ * runtime schema's strictness: a plain object with only `profile`, `custom`,
20
+ * `autoCompact`, `codeSkeleton`, and `intentSummary` keys, a supported profile, a valid Custom
21
+ * document canonicalized to v3 exactly as the runtime resolver would, a
22
+ * strictly-shaped autoCompact section (absent inherits the 80% default), and
23
+ * a strictly-shaped codeSkeleton gate (absent inherits off). Anything else
24
+ * decodes to `undefined` so the UI reports the document as unreadable instead
25
+ * of silently disagreeing with the runtime.
26
+ */
27
+ export function decodeSettings(value: unknown): ContextCompressionSettings | undefined {
28
+ if (!isPlainRecord(value)) return undefined
29
+ const keys = Object.keys(value)
30
+ if (keys.some(key => key !== 'profile' && key !== 'custom' && key !== 'autoCompact' && key !== 'codeSkeleton' && key !== 'intentSummary' && key !== 'monitorPanel' && key !== 'presetOptions')) {
31
+ return undefined
32
+ }
33
+ const profile = (value as { profile?: unknown }).profile
34
+ const custom = (value as { custom?: unknown }).custom
35
+ const autoCompact = decodeAutoCompactSettings((value as { autoCompact?: unknown }).autoCompact)
36
+ const codeSkeleton = decodeCodeSkeletonSettings((value as { codeSkeleton?: unknown }).codeSkeleton)
37
+ const intentSummary = decodeIntentSummarySettings((value as { intentSummary?: unknown }).intentSummary)
38
+ const monitorPanel = decodeMonitorPanelSettings((value as { monitorPanel?: unknown }).monitorPanel)
39
+ const presetOptions = decodePresetOptionsSettings((value as { presetOptions?: unknown }).presetOptions)
40
+ return typeof profile === 'string'
41
+ && (COMPRESSION_PROFILES as readonly string[]).includes(profile)
42
+ && isCustomCompressionPolicy(custom)
43
+ && autoCompact !== undefined
44
+ && codeSkeleton !== undefined
45
+ && intentSummary !== undefined
46
+ && monitorPanel !== undefined
47
+ ? {
48
+ profile: profile as CompressionProfile,
49
+ custom: canonicalizeCustomPolicy(custom),
50
+ autoCompact,
51
+ codeSkeleton,
52
+ intentSummary,
53
+ ...monitorPanel === undefined ? {} : { monitorPanel },
54
+ ...presetOptions === undefined ? {} : { presetOptions },
55
+ }
56
+ : undefined
57
+ }
@@ -1,149 +1,240 @@
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
-
16
- /**
17
- * Harness 0.1.5 mounts the web core's `slots` service on the client context
18
- * but no longer ships a public type for it; declare the face this plugin
19
- * consumes (same shape dsh-thinking-levels and dsh-prime-memory rely on).
20
- * `inject` factories may be sync (return a disposer) or generator (yield the
21
- * registration); slot options are validated at runtime per slot kind, which
22
- * lets one entry supply both `id` (Desktop, list) and `key` (CLI, keyed).
23
- */
24
- interface SlotsService {
25
- inject(slot: string, register: () => (() => void) | Generator<() => void>): () => void
26
- register(options: Record<string, unknown>, component: unknown): () => void
27
- }
28
- declare module '@deepseek-ai/cordis' {
29
- interface Context {
30
- slots: SlotsService
31
- }
32
- }
33
-
34
- // Declare all consumed services (the official client-plugin pattern, and the
35
- // shape dsh-thinking-levels proves on this same 0.1.5 host line): cordis holds
36
- // apply until `locale` / `settingsScope` are provided, so registration can use
37
- // them directly. Resolving them lazily via ctx.get() instead races the settings
38
- // client's activation — on a loss the apply early-returned and EVERY settings
39
- // entry (the standalone section, the plugins-tab card, the item card) silently
40
- // vanished.
41
- export const inject = ['slots', 'locale', 'settingsScope']
42
- const NS = 'context-compression'
43
-
44
-
45
- function sameCustomPolicy(
46
- left: ContextCompressionSettings['custom'],
47
- right: ContextCompressionSettings['custom'],
48
- ): boolean {
49
- if (left.version !== 3 || right.version !== 3) return false
50
- return left.version === right.version
51
- && left.unit === right.unit
52
- && left.prefixPolicy === right.prefixPolicy
53
- && left.fresh.enabled === right.fresh.enabled
54
- && left.fresh.trigger === right.fresh.trigger
55
- && left.fresh.target === right.fresh.target
56
- && left.aggregate.enabled === right.aggregate.enabled
57
- && left.aggregate.trigger === right.aggregate.trigger
58
- && left.aggregate.target === right.aggregate.target
59
- && left.history.enabled === right.history.enabled
60
- && left.history.trigger === right.history.trigger
61
- && left.history.keepRecentToolCalls === right.history.keepRecentToolCalls
62
- && left.history.keepRecentTokens === right.history.keepRecentTokens
63
- && left.history.minReclaim === right.history.minReclaim
64
- && left.tailTrim.enabled === right.tailTrim.enabled
65
- && left.tailTrim.trigger === right.tailTrim.trigger
66
- }
67
-
68
- export function apply(ctx: ClientContext): void {
69
- ctx.locale.register(NS, { zh, en })
70
- const injected = (): CompressionSelectorInjected => {
71
- // Bind per factory call on the caller's fiber (0.1.5: activation must
72
- // never block on the settings transport; the scope disposer belongs to
73
- // the calling registration's lifecycle).
74
- const scope = ctx.settingsScope.bind<ContextCompressionSettings>({ namespace: NS, decode: decodeSettings })
75
- const writeAndConfirm = async (
76
- write: () => Promise<void>,
77
- accepts: (settings: ContextCompressionSettings) => boolean,
78
- ): Promise<void> => {
79
- const beforeRevision = scope.getSnapshot().revision
80
- await write()
81
- const after = scope.getSnapshot()
82
- if (
83
- after.status !== 'ready'
84
- || after.value === undefined
85
- || after.revision === beforeRevision
86
- || !accepts(after.value)
87
- ) {
88
- throw new Error('Context compression settings were not saved.')
89
- }
90
- }
91
- return {
92
- hooks: { compression: scope },
93
- select: profile => writeAndConfirm(
94
- () => scope.set('profile', profile),
95
- settings => settings.profile === profile,
96
- ),
97
- saveCustom: custom => writeAndConfirm(
98
- () => scope.set('custom', custom),
99
- settings => isCustomCompressionPolicy(settings.custom)
100
- && sameCustomPolicy(settings.custom, custom),
101
- ),
102
- resetCustom: () => writeAndConfirm(
103
- () => scope.set('custom', structuredClone(DEFAULT_CUSTOM_COMPRESSION_POLICY)),
104
- settings => isCustomCompressionPolicy(settings.custom)
105
- && sameCustomPolicy(settings.custom, DEFAULT_CUSTOM_COMPRESSION_POLICY),
106
- ),
107
- saveAutoCompact: thresholdPercent => writeAndConfirm(
108
- () => scope.set('autoCompact', { thresholdPercent }),
109
- settings => settings.autoCompact.thresholdPercent === thresholdPercent,
110
- ),
111
- saveCodeSkeleton: enabled => writeAndConfirm(
112
- () => scope.set('codeSkeleton', { enabled }),
113
- settings => settings.codeSkeleton.enabled === enabled,
114
- ),
115
- savePresetOptions: options => {
116
- // Path-addressed so one field write cannot delete its siblings: the
117
- // previous whole-section set erased estimatorMode (and every other
118
- // override) whenever the user touched a second field.
119
- const ops = planPresetOptionsOps(scope.getSnapshot().value?.presetOptions, options)
120
- if (ops.length === 0) return Promise.resolve()
121
- return writeAndConfirm(
122
- () => scope.mutate(ops),
123
- settings => presetOptionsOpsAccepted(settings.presetOptions, ops),
124
- )
125
- },
126
- }
127
- }
128
- // 设置 → 上下文压缩 直挂分节(0.1.1 契约;0.1.5 官方分节也注册在此,未声明槽
129
- // 的注册会在激活期抛错,故 try/catch 守卫 —— 同 dsh-prime-memory 的双槽冗余)。
130
- // 只挂这一处:再注册 `settings.plugins.tab` / `settings.plugin.item` 会在
131
- // 设置里同时出现独立分节和插件卡片,重复展示同一张面板。
132
- try {
133
- ctx.slots.inject('settings.section', () => ctx.slots.register({
134
- name: 'settings.section',
135
- id: 'context-compression',
136
- order: 17,
137
- label: () => ctx.locale.bind(NS)('nav'),
138
- locale: NS,
139
- inject: injected,
140
- }, ContextCompressionSettingsSection))
141
- } catch (error) {
142
- console.warn('[dsh-context-compression-improved] settings.section 注册失败(新宿主已收编):', error)
143
- }
144
- }
145
-
146
- export type {
147
- CompressionProfile, CompressionProfileSelectorProps, CompressionSelectorInjected,
148
- ContextCompressionSettings,
149
- } from './CompressionProfileSelector.tsx'
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 { de } from './locales/de.ts'
15
+ import { es } from './locales/es.ts'
16
+ import { fr } from './locales/fr.ts'
17
+ import { it } from './locales/it.ts'
18
+ import { ja } from './locales/ja.ts'
19
+ import { ko } from './locales/ko.ts'
20
+ import { ru } from './locales/ru.ts'
21
+ import { planPresetOptionsOps, presetOptionsOpsAccepted } from './preset-options.ts'
22
+ import { initMonitorFab } from './monitor-fab.ts'
23
+
24
+ /**
25
+ * Harness 0.1.5 mounts the web core's `slots` service on the client context
26
+ * but no longer ships a public type for it; declare the face this plugin
27
+ * consumes (same shape dsh-thinking-levels and dsh-prime-memory rely on).
28
+ * `inject` factories may be sync (return a disposer) or generator (yield the
29
+ * registration); slot options are validated at runtime per slot kind, which
30
+ * lets one entry supply both `id` (Desktop, list) and `key` (CLI, keyed).
31
+ */
32
+ interface SlotsService {
33
+ inject(slot: string, register: () => (() => void) | Generator<() => void>): () => void
34
+ register(options: Record<string, unknown>, component: unknown): () => void
35
+ }
36
+ declare module '@deepseek-ai/cordis' {
37
+ interface Context {
38
+ slots: SlotsService
39
+ }
40
+ }
41
+
42
+ // Declare all consumed services (the official client-plugin pattern, and the
43
+ // shape dsh-thinking-levels proves on this same 0.1.5 host line): cordis holds
44
+ // apply until `locale` / `settingsScope` are provided, so registration can use
45
+ // them directly. Resolving them lazily via ctx.get() instead races the settings
46
+ // client's activation — on a loss the apply early-returned and EVERY settings
47
+ // entry (the standalone section, the plugins-tab card, the item card) silently
48
+ // vanished.
49
+ export const inject = ['slots', 'locale', 'settingsScope']
50
+ const NS = 'context-compression'
51
+
52
+
53
+ function sameCustomPolicy(
54
+ left: ContextCompressionSettings['custom'],
55
+ right: ContextCompressionSettings['custom'],
56
+ ): boolean {
57
+ if (left.version !== 3 || right.version !== 3) return false
58
+ return left.version === right.version
59
+ && left.unit === right.unit
60
+ && left.prefixPolicy === right.prefixPolicy
61
+ && left.fresh.enabled === right.fresh.enabled
62
+ && left.fresh.trigger === right.fresh.trigger
63
+ && left.fresh.target === right.fresh.target
64
+ && left.aggregate.enabled === right.aggregate.enabled
65
+ && left.aggregate.trigger === right.aggregate.trigger
66
+ && left.aggregate.target === right.aggregate.target
67
+ && left.history.enabled === right.history.enabled
68
+ && left.history.trigger === right.history.trigger
69
+ && left.history.keepRecentToolCalls === right.history.keepRecentToolCalls
70
+ && left.history.keepRecentTokens === right.history.keepRecentTokens
71
+ && left.history.minReclaim === right.history.minReclaim
72
+ && left.tailTrim.enabled === right.tailTrim.enabled
73
+ && left.tailTrim.trigger === right.tailTrim.trigger
74
+ }
75
+
76
+ export function apply(ctx: ClientContext): void {
77
+ ctx.locale.register(NS, { zh, en })
78
+ // de/es/fr/it/ja/ko/ru are not host built-ins (`BuiltInLocaleId` is zh|en), so
79
+ // their dictionaries ride the single-locale untyped register overload, and each
80
+ // needs an `addLanguage` definition to become selectable. Labels are
81
+ // self-described in their own language; the fallback chain ends at English.
82
+ // One occupied locale must not sink activation, hence the per-language guard.
83
+ const languages = [
84
+ { id: 'de', label: 'Deutsch', dict: de },
85
+ { id: 'es', label: 'Español', dict: es },
86
+ { id: 'fr', label: 'Français', dict: fr },
87
+ { id: 'it', label: 'Italiano', dict: it },
88
+ { id: 'ja', label: '日本語', dict: ja },
89
+ { id: 'ko', label: '한국어', dict: ko },
90
+ { id: 'ru', label: 'Русский', dict: ru },
91
+ ] as const
92
+ for (const language of languages) {
93
+ try {
94
+ ctx.locale.register(NS, language.id, language.dict)
95
+ ctx.locale.addLanguage({ id: language.id, label: language.label, fallback: 'en' })
96
+ } catch (error) {
97
+ console.warn(`[dsh-context-compression-improved] locale "${language.id}" registration failed:`, error)
98
+ }
99
+ }
100
+ // Section label translator, captured EAGERLY: the settings shell evaluates
101
+ // `label()` during its own render; a lazy ctx.locale accessor inside the
102
+ // thunk would throw there if `locale` ever left this module's inject list.
103
+ const localeSvc = ctx.locale as { bind?: (n: string) => (key: string) => string } | undefined
104
+ const tNav = localeSvc?.bind?.(NS) ?? ((key: string) => key)
105
+
106
+ // 浮动监控面板(FAB): body 级幂等单例。enabled 读取走 0.1.5 的 settingsScope
107
+ // 文档——注入工厂内再刷新读取闭包(bind 发生在 settings.section 工厂里)。
108
+ let readMonitorPanelEnabled = (): boolean => false
109
+ // 浮动面板会话绑定:跟随输入栏座位的 inject 回调(裸 sessionId,pm longtask 同款)。
110
+ let latestSessionId: string | undefined
111
+ try {
112
+ initMonitorFab({
113
+ t: tNav,
114
+ sessionIdOf: () => latestSessionId,
115
+ enabledOf: () => readMonitorPanelEnabled(),
116
+ fetchSnapshot: async (sessionId) => {
117
+ const query = sessionId === undefined ? '' : `?sessionId=${encodeURIComponent(sessionId)}`
118
+ const response = await fetch(`/api/dsh-context-compression-improved/monitor${query}`, { headers: { 'cache-control': 'no-cache' } })
119
+ if (!response.ok) throw new Error(`monitor snapshot ${response.status}`)
120
+ return await response.json() as Parameters<typeof initMonitorFab>[0]['fetchSnapshot'] extends (...args: never[]) => Promise<infer T> ? T : never
121
+ },
122
+ applyOverride: async (sessionId, action) => {
123
+ const query = sessionId === undefined ? '' : `?sessionId=${encodeURIComponent(sessionId)}`
124
+ await fetch(`/api/dsh-context-compression-improved/monitor${query}`, {
125
+ method: 'POST',
126
+ headers: { 'content-type': 'application/json' },
127
+ body: JSON.stringify({ action }),
128
+ })
129
+ },
130
+ })
131
+ } catch (error) {
132
+ console.warn('[dsh-context-compression-improved] 监控浮动球挂载失败(不影响设置分节):', error)
133
+ }
134
+ const injected = (): CompressionSelectorInjected => {
135
+ // Bind per factory call on the caller's fiber (0.1.5: activation must
136
+ // never block on the settings transport; the scope disposer belongs to
137
+ // the calling registration's lifecycle).
138
+ const scope = ctx.settingsScope.bind<ContextCompressionSettings>({ namespace: NS, decode: decodeSettings })
139
+ readMonitorPanelEnabled = (): boolean =>
140
+ scope.getSnapshot().value?.monitorPanel?.enabled ?? false
141
+ const writeAndConfirm = async (
142
+ write: () => Promise<void>,
143
+ accepts: (settings: ContextCompressionSettings) => boolean,
144
+ ): Promise<void> => {
145
+ const beforeRevision = scope.getSnapshot().revision
146
+ await write()
147
+ const after = scope.getSnapshot()
148
+ if (
149
+ after.status !== 'ready'
150
+ || after.value === undefined
151
+ || after.revision === beforeRevision
152
+ || !accepts(after.value)
153
+ ) {
154
+ throw new Error('Context compression settings were not saved.')
155
+ }
156
+ }
157
+ return {
158
+ hooks: { compression: scope },
159
+ select: profile => writeAndConfirm(
160
+ () => scope.set('profile', profile),
161
+ settings => settings.profile === profile,
162
+ ),
163
+ saveCustom: custom => writeAndConfirm(
164
+ () => scope.set('custom', custom),
165
+ settings => isCustomCompressionPolicy(settings.custom)
166
+ && sameCustomPolicy(settings.custom, custom),
167
+ ),
168
+ resetCustom: () => writeAndConfirm(
169
+ () => scope.set('custom', structuredClone(DEFAULT_CUSTOM_COMPRESSION_POLICY)),
170
+ settings => isCustomCompressionPolicy(settings.custom)
171
+ && sameCustomPolicy(settings.custom, DEFAULT_CUSTOM_COMPRESSION_POLICY),
172
+ ),
173
+ saveAutoCompact: thresholdPercent => writeAndConfirm(
174
+ () => scope.set('autoCompact', { thresholdPercent }),
175
+ settings => settings.autoCompact.thresholdPercent === thresholdPercent,
176
+ ),
177
+ saveCodeSkeleton: enabled => writeAndConfirm(
178
+ () => scope.set('codeSkeleton', { enabled }),
179
+ settings => settings.codeSkeleton.enabled === enabled,
180
+ ),
181
+ saveIntentSummary: enabled => writeAndConfirm(
182
+ () => scope.set('intentSummary', { enabled }),
183
+ settings => settings.intentSummary.enabled === enabled,
184
+ ),
185
+ saveMonitorPanel: enabled => writeAndConfirm(
186
+ () => scope.set('monitorPanel', { enabled }),
187
+ settings => settings.monitorPanel?.enabled === enabled,
188
+ ),
189
+ savePresetOptions: options => {
190
+ // Path-addressed so one field write cannot delete its siblings: the
191
+ // previous whole-section set erased estimatorMode (and every other
192
+ // override) whenever the user touched a second field.
193
+ const ops = planPresetOptionsOps(scope.getSnapshot().value?.presetOptions, options)
194
+ if (ops.length === 0) return Promise.resolve()
195
+ return writeAndConfirm(
196
+ () => scope.mutate(ops),
197
+ settings => presetOptionsOpsAccepted(settings.presetOptions, ops),
198
+ )
199
+ },
200
+ }
201
+ }
202
+ // 设置 → 上下文压缩 直挂分节(0.1.1 契约;0.1.5 官方分节也注册在此,未声明槽
203
+ // 的注册会在激活期抛错,故 try/catch 守卫 —— 同 dsh-prime-memory 的双槽冗余)。
204
+ // 只挂这一处:再注册 `settings.plugins.tab` / `settings.plugin.item` 会在
205
+ // 设置里同时出现独立分节和插件卡片,重复展示同一张面板。
206
+ try {
207
+ ctx.slots.inject('settings.section', () => ctx.slots.register({
208
+ name: 'settings.section',
209
+ id: 'context-compression',
210
+ order: 17,
211
+ label: () => ctx.locale.bind(NS)('nav'),
212
+ locale: NS,
213
+ inject: injected,
214
+ }, ContextCompressionSettingsSection))
215
+ } catch (error) {
216
+ console.warn('[dsh-context-compression-improved] settings.section 注册失败(新宿主已收编):', error)
217
+ }
218
+
219
+ // 输入栏左座(0.1.5+ 宿主):组件刻意空渲染——注册此座位只为接收宿主的
220
+ // sessionId 注入回调,供浮动监控面板绑定会话(pm longtask 同款)。旧宿主
221
+ // 未声明该槽时激活期会抛错,try/catch 守卫(同上双槽冗余先例)。
222
+ try {
223
+ ctx.slots.inject('conversation.input.left', () => ctx.slots.register({
224
+ name: 'conversation.input.left',
225
+ id: 'context-compression-session-bind',
226
+ order: 170,
227
+ inject: (sessionId: string) => {
228
+ latestSessionId = sessionId
229
+ return {}
230
+ },
231
+ }, () => null))
232
+ } catch (error) {
233
+ console.warn('[dsh-context-compression-improved] conversation.input.left 注册失败(旧宿主无该槽,浮动面板保持聚合口径):', error)
234
+ }
235
+ }
236
+
237
+ export type {
238
+ CompressionProfile, CompressionProfileSelectorProps, CompressionSelectorInjected,
239
+ ContextCompressionSettings,
240
+ } from './CompressionProfileSelector.tsx'