dsh-context-compression-improved 0.9.9-beta.0 → 0.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.agents/plans/improve/checklist.md +32 -0
- package/.agents/plans/improve/findings.md +102 -0
- package/.agents/plans/improve/spec.md +53 -0
- package/.agents/plans/improve/tasks.md +70 -0
- package/.gitattributes +2 -0
- package/.githooks/pre-push +37 -0
- package/.github/workflows/ci.yml +39 -0
- package/CHANGELOG.de.md +580 -0
- package/CHANGELOG.es.md +588 -0
- package/CHANGELOG.fr.md +576 -0
- package/CHANGELOG.it.md +577 -0
- package/CHANGELOG.ja.md +261 -0
- package/CHANGELOG.ko.md +254 -0
- package/CHANGELOG.md +544 -0
- package/CHANGELOG.ru.md +554 -0
- package/CHANGELOG.zh.md +238 -0
- package/CONTRIBUTING.md +22 -0
- package/README.de.md +96 -0
- package/README.es.md +96 -0
- package/README.fr.md +96 -0
- package/README.it.md +96 -0
- package/README.ja.md +102 -0
- package/README.ko.md +102 -0
- package/README.md +110 -15
- package/README.ru.md +96 -0
- package/README.zh.md +104 -14
- package/SECURITY.md +18 -0
- package/THIRD_PARTY_NOTICES.md +7 -25
- package/assets/board.png +0 -0
- package/assets/context.png +0 -0
- package/docs/assets/context-compression-selector-profiles.jpg +0 -0
- package/docs/assets/context-compression-selector-settings.png +0 -0
- package/docs/compat-0.1.5-report.md +77 -0
- package/docs/installation.de.md +104 -0
- package/docs/installation.es.md +104 -0
- package/docs/installation.fr.md +104 -0
- package/docs/installation.it.md +104 -0
- package/docs/installation.ja.md +78 -0
- package/docs/installation.ko.md +78 -0
- package/docs/installation.md +103 -0
- package/docs/installation.ru.md +104 -0
- package/docs/installation.zh.md +100 -0
- package/docs/repair-log.md +657 -0
- package/eslint.config.js +30 -0
- package/package.json +116 -129
- package/packages/selector/LICENSE +21 -0
- package/packages/selector/README.md +26 -0
- package/packages/selector/README.zh.md +24 -0
- package/packages/selector/THIRD_PARTY_NOTICES.md +32 -0
- package/packages/selector/docs/history-tool-call-working-set-spec.md +112 -0
- package/packages/selector/docs/native-tool-result-selector-spec.md +34 -0
- package/packages/selector/docs/subagent-cache-reuse-spec.md +46 -0
- package/{dsh.plugin.json → packages/selector/dsh.plugin.json} +2 -2
- package/{lib → packages/selector/lib}/client.js +3 -1
- package/{lib → packages/selector/lib}/savings-integral.js +2 -0
- package/packages/selector/lib/style.css +350 -0
- package/packages/selector/package.json +178 -0
- package/packages/selector/screenshots.json +3 -0
- package/packages/selector/src/client/CompressionProfileControls.tsx +293 -0
- package/packages/selector/src/client/CompressionProfileSelector.module.css +199 -0
- package/packages/selector/src/client/CompressionProfileSelector.tsx +85 -0
- package/packages/selector/src/client/CustomPolicyEditor.tsx +216 -0
- package/packages/selector/src/client/EstimatorControls.tsx +277 -0
- package/packages/selector/src/client/bridge-scope.ts +125 -0
- package/packages/selector/src/client/decode.ts +57 -0
- package/packages/selector/src/client/index.ts +424 -0
- package/packages/selector/src/client/locales/de.ts +158 -0
- package/packages/selector/src/client/locales/es.ts +158 -0
- package/packages/selector/src/client/locales/fr.ts +158 -0
- package/packages/selector/src/client/locales/it.ts +158 -0
- package/packages/selector/src/client/locales/ja.ts +158 -0
- package/packages/selector/src/client/locales/ko.ts +158 -0
- package/packages/selector/src/client/locales/ru.ts +158 -0
- package/packages/selector/src/client/locales.ts +316 -0
- package/packages/selector/src/client/monitor-fab.ts +419 -0
- package/packages/selector/src/client/preset-options.ts +88 -0
- package/packages/selector/src/client/savings-card.tsx +269 -0
- package/packages/selector/src/client/scope-face.ts +24 -0
- package/packages/selector/src/client/settings-section.tsx +140 -0
- package/packages/selector/src/compat/host-generation.ts +96 -0
- package/packages/selector/src/compat/llm-messages.ts +50 -0
- package/packages/selector/src/compat/settings-lease.ts +108 -0
- package/packages/selector/src/compat/tool-result.ts +91 -0
- package/packages/selector/src/css-modules.d.ts +6 -0
- package/packages/selector/src/deepseek-v4-tokenizer.ts +205 -0
- package/packages/selector/src/estimator-catalog.ts +104 -0
- package/packages/selector/src/index.ts +890 -0
- package/packages/selector/src/invariant.ts +113 -0
- package/packages/selector/src/preset-overlay.ts +749 -0
- package/packages/selector/src/profiles.ts +431 -0
- package/packages/selector/src/pruner/content.ts +198 -0
- package/packages/selector/src/pruner/session.ts +97 -0
- package/packages/selector/src/pruner/state.ts +61 -0
- package/packages/selector/src/pruner/tuning.ts +23 -0
- package/packages/selector/src/pruner/types.ts +78 -0
- package/packages/selector/src/pruner.ts +2914 -0
- package/packages/selector/src/runtime/adaptive-cost.ts +205 -0
- package/packages/selector/src/runtime/audit.ts +320 -0
- package/packages/selector/src/runtime/config.ts +756 -0
- package/packages/selector/src/runtime/custom-policy.ts +278 -0
- package/packages/selector/src/runtime/deepseek-official-pricing.ts +298 -0
- package/packages/selector/src/runtime/deepseek-v4-vision-tokens.ts +254 -0
- package/packages/selector/src/runtime/display-prefs.ts +98 -0
- package/packages/selector/src/runtime/measurement.ts +444 -0
- package/packages/selector/src/runtime/monitor.ts +310 -0
- package/packages/selector/src/runtime/panel-command.ts +213 -0
- package/packages/selector/src/runtime/reducers.ts +1754 -0
- package/packages/selector/src/runtime/retrieve.ts +503 -0
- package/packages/selector/src/runtime/savings-integral.ts +117 -0
- package/packages/selector/src/runtime/savings-projection.ts +229 -0
- package/packages/selector/src/runtime/savings-verify.ts +446 -0
- package/packages/selector/src/runtime/savings.ts +526 -0
- package/packages/selector/src/runtime/session-events.ts +30 -0
- package/packages/selector/src/runtime/session-file-decode.ts +523 -0
- package/packages/selector/src/runtime/stats-backfill.ts +541 -0
- package/packages/selector/src/runtime/stats-db.ts +492 -0
- package/packages/selector/src/runtime/stats-schema.ts +123 -0
- package/packages/selector/src/runtime/stats-sync.ts +307 -0
- package/packages/selector/src/runtime/summary-command.ts +127 -0
- package/packages/selector/src/runtime/tail-trim.ts +176 -0
- package/packages/selector/src/runtime/token-count.ts +72 -0
- package/packages/selector/src/runtime/tokenpilot/advisor-prompt.ts +233 -0
- package/packages/selector/src/runtime/tokenpilot/advisor-state.ts +204 -0
- package/packages/selector/src/runtime/tokenpilot/advisor.ts +419 -0
- package/packages/selector/src/runtime/tokenpilot/benefit.ts +200 -0
- package/packages/selector/src/runtime/tokenpilot/dedup.ts +81 -0
- package/packages/selector/src/runtime/tokenpilot/estimator.ts +151 -0
- package/packages/selector/src/runtime/tokenpilot/intent-fold.ts +53 -0
- package/packages/selector/src/runtime/tokenpilot/intent-gate.ts +65 -0
- package/packages/selector/src/runtime/tokenpilot/intent-input-mask.ts +125 -0
- package/packages/selector/src/runtime/tokenpilot/intent-range.ts +37 -0
- package/packages/selector/src/runtime/tokenpilot/locator.ts +128 -0
- package/packages/selector/src/runtime/tokenpilot/read-state.ts +98 -0
- package/packages/selector/src/runtime/tokenpilot/sidechannel.ts +303 -0
- package/packages/selector/src/runtime/toolclass.ts +103 -0
- package/packages/selector/src/runtime/types.ts +365 -0
- package/packages/selector/src/runtime/value.ts +48 -0
- package/packages/selector/src/settings-bridge.ts +175 -0
- package/packages/selector/tests/advisor-report.host.spec.ts +223 -0
- package/packages/selector/tests/auto-compact.client.spec.tsx +214 -0
- package/packages/selector/tests/built/client-artifact.spec.ts +56 -0
- package/packages/selector/tests/cache-prefix-audit.spec.ts +123 -0
- package/packages/selector/tests/code-skeleton.client.spec.ts +98 -0
- package/packages/selector/tests/compat/compat-narrow-waist.spec.ts +150 -0
- package/packages/selector/tests/custom-contract.client.spec.ts +226 -0
- package/packages/selector/tests/estimator-catalog.spec.ts +70 -0
- package/packages/selector/tests/estimator-channel.client.spec.tsx +253 -0
- package/packages/selector/tests/estimator-route-registration.host.spec.ts +202 -0
- package/packages/selector/tests/helpers/legacy-namespace.ts +35 -0
- package/packages/selector/tests/helpers/legacy-settings/index.ts +15 -0
- package/packages/selector/tests/helpers/legacy-settings/legacy/provider.d.ts +41 -0
- package/packages/selector/tests/helpers/legacy-settings/legacy/provider.js +647 -0
- package/packages/selector/tests/helpers/legacy-settings/legacy/redact.js +78 -0
- package/packages/selector/tests/helpers/legacy-settings/legacy/util-values.js +234 -0
- package/packages/selector/tests/host-preset-overlay.host.spec.ts +204 -0
- package/packages/selector/tests/monitor-fab.client.spec.ts +220 -0
- package/packages/selector/tests/preset-options-write.client.spec.ts +192 -0
- package/packages/selector/tests/preset-overlay-loader.e2e.host.spec.ts +200 -0
- package/packages/selector/tests/preset-overlay.host.spec.ts +246 -0
- package/packages/selector/tests/profiles.client.spec.tsx +436 -0
- package/packages/selector/tests/public/package-contract.client.spec.ts +86 -0
- package/packages/selector/tests/runtime/adaptive-cost.spec.ts +167 -0
- package/packages/selector/tests/runtime/advice-never-withholds.host.spec.ts +230 -0
- package/packages/selector/tests/runtime/advisor-invariant.spec.ts +272 -0
- package/packages/selector/tests/runtime/advisor.spec.ts +226 -0
- package/packages/selector/tests/runtime/audit.spec.ts +275 -0
- package/packages/selector/tests/runtime/auto-compact-config.spec.ts +523 -0
- package/packages/selector/tests/runtime/char-basis.spec.ts +30 -0
- package/packages/selector/tests/runtime/code-skeleton.spec.ts +152 -0
- package/packages/selector/tests/runtime/deepseek-official-pricing.spec.ts +186 -0
- package/packages/selector/tests/runtime/deepseek-v4-tokenizer.spec.ts +122 -0
- package/packages/selector/tests/runtime/deepseek-v4-vision-tokens.spec.ts +122 -0
- package/packages/selector/tests/runtime/deprecated-preset-options.spec.ts +96 -0
- package/packages/selector/tests/runtime/fixtures/profile-baseline.json +273 -0
- package/packages/selector/tests/runtime/fixtures/session-v4-dump.jsonl +46 -0
- package/packages/selector/tests/runtime/fixtures/tokenizer-golden.json +106 -0
- package/packages/selector/tests/runtime/fixtures/vision-golden.json +459 -0
- package/packages/selector/tests/runtime/frequency-longstrings.spec.ts +74 -0
- package/packages/selector/tests/runtime/fresh-gate.spec.ts +43 -0
- package/packages/selector/tests/runtime/html-reducer.spec.ts +212 -0
- package/packages/selector/tests/runtime/intent-gate.spec.ts +62 -0
- package/packages/selector/tests/runtime/intent-summary.spec.ts +156 -0
- package/packages/selector/tests/runtime/jsonl-reducer.spec.ts +79 -0
- package/packages/selector/tests/runtime/line-mapping.spec.ts +153 -0
- package/packages/selector/tests/runtime/monitor.spec.ts +354 -0
- package/packages/selector/tests/runtime/panel-command.spec.ts +101 -0
- package/packages/selector/tests/runtime/prose-reducers.spec.ts +133 -0
- package/packages/selector/tests/runtime/public/public-runtime.spec.ts +2781 -0
- package/packages/selector/tests/runtime/read-input-cap.spec.ts +33 -0
- package/packages/selector/tests/runtime/savings-accounting.spec.ts +291 -0
- package/packages/selector/tests/runtime/savings-integral.spec.ts +169 -0
- package/packages/selector/tests/runtime/savings-projection.spec.ts +89 -0
- package/packages/selector/tests/runtime/savings-verify.spec.ts +226 -0
- package/packages/selector/tests/runtime/savings.spec.ts +139 -0
- package/packages/selector/tests/runtime/search-reducer.spec.ts +110 -0
- package/packages/selector/tests/runtime/session-events.spec.ts +27 -0
- package/packages/selector/tests/runtime/session-file-decode.spec.ts +281 -0
- package/packages/selector/tests/runtime/settings-unwrap.spec.ts +58 -0
- package/packages/selector/tests/runtime/sidechannel.spec.ts +241 -0
- package/packages/selector/tests/runtime/stats-backfill-scheduler.spec.ts +167 -0
- package/packages/selector/tests/runtime/stats-backfill.spec.ts +348 -0
- package/packages/selector/tests/runtime/stats-db.spec.ts +361 -0
- package/packages/selector/tests/runtime/stats-privacy.spec.ts +55 -0
- package/packages/selector/tests/runtime/stats-sync.spec.ts +278 -0
- package/packages/selector/tests/runtime/summary-command.spec.ts +154 -0
- package/packages/selector/tests/runtime/toc-and-bundled.spec.ts +159 -0
- package/packages/selector/tests/runtime/tokenizer-golden.spec.ts +53 -0
- package/packages/selector/tests/runtime/tokenpilot/benefit.spec.ts +217 -0
- package/packages/selector/tests/runtime/tokenpilot/dedup.spec.ts +52 -0
- package/packages/selector/tests/runtime/tokenpilot/estimator.spec.ts +79 -0
- package/packages/selector/tests/runtime/tokenpilot/locator.spec.ts +76 -0
- package/packages/selector/tests/runtime/tokenpilot/profile-baseline.spec.ts +119 -0
- package/packages/selector/tests/runtime/tokenpilot/read-state.spec.ts +82 -0
- package/packages/selector/tests/runtime/toolclass.spec.ts +156 -0
- package/packages/selector/tests/runtime/value.spec.ts +23 -0
- package/packages/selector/tests/savings-sessions.client.spec.tsx +140 -0
- package/packages/selector/tests/settings-seat.client.spec.ts +215 -0
- package/packages/selector/tests/standing-generation.host.spec.ts +724 -0
- package/packages/selector/tests/subagent-cache-reuse.host.spec.ts +269 -0
- package/packages/selector/tests/support/cache-prefix-audit.ts +105 -0
- package/packages/selector/tests/support/mock-adapter.ts +37 -0
- package/packages/selector/tests/support/ui-primitives.tsx +34 -0
- package/packages/selector/tsconfig.json +11 -0
- package/packages/selector/tsdown.client.config.ts +102 -0
- package/packages/selector/tsdown.config.ts +20 -0
- package/pnpm-workspace.yaml +76 -0
- package/screenshots.json +6 -3
- package/scripts/capture-profile-baseline.mjs +42 -0
- package/scripts/cell-browser-e2e.mjs +354 -0
- package/scripts/generate-tokenizer-fixtures.py +81 -0
- package/scripts/generate-vision-fixtures.py +208 -0
- package/scripts/hosts.mjs +78 -0
- package/scripts/packed-components-smoke.mjs +763 -0
- package/scripts/packed-install-e2e.mjs +1346 -0
- package/scripts/sync-hosts.mjs +174 -0
- package/scripts/toolclass-corpus-replay.mjs +281 -0
- package/scripts/verify-release.mjs +338 -0
- package/tests/TEST_INVENTORY.md +41 -0
- package/tsconfig.base.json +18 -0
- package/tsconfig.json +7 -0
- package/tsconfig.tests.json +15 -0
- package/vitest.built.config.ts +9 -0
- package/vitest.config.ts +54 -0
- /package/{assets → packages/selector/assets}/deepseek-v4/LICENSE.DeepSeek-V4-Pro.txt +0 -0
- /package/{assets → packages/selector/assets}/deepseek-v4/manifest.json +0 -0
- /package/{assets → packages/selector/assets}/deepseek-v4/tokenizer.json +0 -0
- /package/{assets → packages/selector/assets}/deepseek-v4/tokenizer_config.json +0 -0
- /package/{assets → packages/selector/assets}/deepseek-v4-vision-exp/LICENSE.DeepSeek-V4-Flash-Vision-Exp.txt +0 -0
- /package/{assets → packages/selector/assets}/deepseek-v4-vision-exp/manifest.json +0 -0
- /package/{assets → packages/selector/assets}/deepseek-v4-vision-exp/tokenizer.json +0 -0
- /package/{assets → packages/selector/assets}/deepseek-v4-vision-exp/tokenizer_config.json +0 -0
- /package/{cordis.patch.yml → packages/selector/cordis.patch.yml} +0 -0
- /package/{lib → packages/selector/lib}/client.d.ts +0 -0
- /package/{lib → packages/selector/lib}/index.d.ts +0 -0
- /package/{lib → packages/selector/lib}/index.js +0 -0
- /package/{lib → packages/selector/lib}/invariant.d.ts +0 -0
- /package/{lib → packages/selector/lib}/invariant.js +0 -0
- /package/{lib → packages/selector/lib}/profiles.d.ts +0 -0
- /package/{lib → packages/selector/lib}/pruner.d.ts +0 -0
- /package/{lib → packages/selector/lib}/pruner.js +0 -0
- /package/{lib → packages/selector/lib}/tail-trim.js +0 -0
- /package/{lib → packages/selector/lib}/tool-result.js +0 -0
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* CustomPolicyEditor: full custom compression policy editor with StageFields.
|
|
3
|
+
*
|
|
4
|
+
* Extracted from CompressionProfileSelector.tsx to reduce god-module size.
|
|
5
|
+
*
|
|
6
|
+
* @module dsh-context-compression-improved/client/CustomPolicyEditor
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import type { ContextCompressionLocaleKey } from './locales.ts'
|
|
10
|
+
import css from './CompressionProfileSelector.module.css'
|
|
11
|
+
import {
|
|
12
|
+
isCustomCompressionPolicy,
|
|
13
|
+
type CustomCompressionBudget,
|
|
14
|
+
type CustomCompressionPolicy,
|
|
15
|
+
type CustomCompressionPolicyV3,
|
|
16
|
+
type CustomHistoryPolicy,
|
|
17
|
+
} from '../profiles.ts'
|
|
18
|
+
|
|
19
|
+
interface CustomPolicyEditorProps {
|
|
20
|
+
value: CustomCompressionPolicyV3
|
|
21
|
+
disabled: boolean
|
|
22
|
+
setValue: (value: CustomCompressionPolicyV3) => void
|
|
23
|
+
save: () => Promise<void>
|
|
24
|
+
reset: () => Promise<void>
|
|
25
|
+
settle: (operation: () => Promise<void>) => void
|
|
26
|
+
t: (key: ContextCompressionLocaleKey) => string
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function CustomPolicyEditor({ value, disabled, setValue, save, reset, settle, t }: CustomPolicyEditorProps) {
|
|
30
|
+
const valid = isCustomCompressionPolicy(value)
|
|
31
|
+
const unitStep = value.unit === 'tokens' ? 1 : 0.01
|
|
32
|
+
const unitMax = value.unit === 'tokens' ? undefined : 100
|
|
33
|
+
const unitBounds = unitMax === undefined ? {} : { max: unitMax }
|
|
34
|
+
const setBudget = (
|
|
35
|
+
stage: 'fresh' | 'aggregate',
|
|
36
|
+
patch: Partial<CustomCompressionBudget>,
|
|
37
|
+
): void => {
|
|
38
|
+
setValue({ ...value, [stage]: { ...value[stage], ...patch } })
|
|
39
|
+
}
|
|
40
|
+
const setHistory = (patch: Partial<CustomHistoryPolicy>): void => {
|
|
41
|
+
setValue({ ...value, history: { ...value.history, ...patch } })
|
|
42
|
+
}
|
|
43
|
+
return (
|
|
44
|
+
<section className={css.custom} aria-labelledby="context-compression-custom-title">
|
|
45
|
+
<h3 id="context-compression-custom-title" className={css.customTitle}>{t('custom.title')}</h3>
|
|
46
|
+
<p className={css.customNote}>{t('custom.sessionScope')}</p>
|
|
47
|
+
<p className={css.customNote}>{t('custom.measurement')}</p>
|
|
48
|
+
<label className={css.field}>
|
|
49
|
+
<span>{t('custom.unit')}</span>
|
|
50
|
+
<select
|
|
51
|
+
value={value.unit}
|
|
52
|
+
disabled={disabled}
|
|
53
|
+
onChange={(event) => {
|
|
54
|
+
setValue({
|
|
55
|
+
...value,
|
|
56
|
+
unit: event.currentTarget.value as CustomCompressionPolicy['unit'],
|
|
57
|
+
})
|
|
58
|
+
}}
|
|
59
|
+
>
|
|
60
|
+
<option value="tokens">{t('custom.unit.tokens')}</option>
|
|
61
|
+
<option value="context-percent">{t('custom.unit.contextPercent')}</option>
|
|
62
|
+
</select>
|
|
63
|
+
</label>
|
|
64
|
+
<StageFields
|
|
65
|
+
t={t}
|
|
66
|
+
title={t('custom.fresh.enabled')}
|
|
67
|
+
enabled={value.fresh.enabled}
|
|
68
|
+
disabled={disabled}
|
|
69
|
+
onEnabled={(enabled) => { setBudget('fresh', { enabled }) }}
|
|
70
|
+
fields={[
|
|
71
|
+
{ label: t('custom.fresh.trigger'), value: value.fresh.trigger, set: (trigger) => { setBudget('fresh', { trigger }) }, ...unitBounds },
|
|
72
|
+
{ label: t('custom.fresh.target'), value: value.fresh.target, set: (target) => { setBudget('fresh', { target }) }, ...unitBounds },
|
|
73
|
+
]}
|
|
74
|
+
step={unitStep}
|
|
75
|
+
/>
|
|
76
|
+
<StageFields
|
|
77
|
+
t={t}
|
|
78
|
+
title={t('custom.aggregate.enabled')}
|
|
79
|
+
enabled={value.aggregate.enabled}
|
|
80
|
+
disabled={disabled}
|
|
81
|
+
onEnabled={(enabled) => { setBudget('aggregate', { enabled }) }}
|
|
82
|
+
fields={[
|
|
83
|
+
{ label: t('custom.aggregate.trigger'), value: value.aggregate.trigger, set: (trigger) => { setBudget('aggregate', { trigger }) }, ...unitBounds },
|
|
84
|
+
{ label: t('custom.aggregate.target'), value: value.aggregate.target, set: (target) => { setBudget('aggregate', { target }) }, ...unitBounds },
|
|
85
|
+
]}
|
|
86
|
+
step={unitStep}
|
|
87
|
+
/>
|
|
88
|
+
<StageFields
|
|
89
|
+
t={t}
|
|
90
|
+
title={t('custom.history.enabled')}
|
|
91
|
+
enabled={value.history.enabled}
|
|
92
|
+
disabled={disabled}
|
|
93
|
+
onEnabled={(enabled) => { setHistory({ enabled }) }}
|
|
94
|
+
fields={[
|
|
95
|
+
{ label: t('custom.history.trigger'), value: value.history.trigger, set: (trigger) => { setHistory({ trigger }) }, ...unitBounds },
|
|
96
|
+
{ label: t('custom.history.keepRecentToolCalls'), value: value.history.keepRecentToolCalls, set: (keepRecentToolCalls) => { setHistory({ keepRecentToolCalls }) }, integer: true, allowZero: true },
|
|
97
|
+
{ label: t('custom.history.keepRecentTokens'), value: value.history.keepRecentTokens, set: (keepRecentTokens) => { setHistory({ keepRecentTokens }) }, allowZero: true, ...unitBounds },
|
|
98
|
+
{ label: t('custom.history.minReclaim'), value: value.history.minReclaim, set: (minReclaim) => { setHistory({ minReclaim }) }, ...unitBounds },
|
|
99
|
+
]}
|
|
100
|
+
step={unitStep}
|
|
101
|
+
fieldsEnabled={value.history.enabled || value.tailTrim.enabled}
|
|
102
|
+
/>
|
|
103
|
+
<label className={css.field}>
|
|
104
|
+
<span>{t('custom.prefixPolicy')}</span>
|
|
105
|
+
<select
|
|
106
|
+
value={value.prefixPolicy}
|
|
107
|
+
disabled={disabled || !value.history.enabled}
|
|
108
|
+
onChange={(event) => {
|
|
109
|
+
setValue({
|
|
110
|
+
...value,
|
|
111
|
+
prefixPolicy: event.currentTarget.value as CustomCompressionPolicy['prefixPolicy'],
|
|
112
|
+
})
|
|
113
|
+
}}
|
|
114
|
+
>
|
|
115
|
+
<option value="preserve">{t('custom.prefixPolicy.preserve')}</option>
|
|
116
|
+
<option value="pressure-break">{t('custom.prefixPolicy.pressureBreak')}</option>
|
|
117
|
+
</select>
|
|
118
|
+
</label>
|
|
119
|
+
<div className={css.customNote}>{t('custom.experimental')}</div>
|
|
120
|
+
<StageFields
|
|
121
|
+
t={t}
|
|
122
|
+
title={t('custom.tailTrim.enabled')}
|
|
123
|
+
enabled={value.tailTrim.enabled}
|
|
124
|
+
disabled={disabled}
|
|
125
|
+
onEnabled={(enabled) => {
|
|
126
|
+
setValue({ ...value, tailTrim: { ...value.tailTrim, enabled } })
|
|
127
|
+
}}
|
|
128
|
+
fields={[{
|
|
129
|
+
label: t('custom.tailTrim.trigger'),
|
|
130
|
+
value: value.tailTrim.trigger,
|
|
131
|
+
set: (trigger) => {
|
|
132
|
+
setValue({ ...value, tailTrim: { ...value.tailTrim, trigger } })
|
|
133
|
+
},
|
|
134
|
+
...unitBounds,
|
|
135
|
+
}]}
|
|
136
|
+
step={unitStep}
|
|
137
|
+
/>
|
|
138
|
+
<p className={css.customNote}>{t('custom.tailTrim.warning')}</p>
|
|
139
|
+
{valid ? null : <div className={css.error} role="alert">{t('custom.invalid')}</div>}
|
|
140
|
+
<div className={css.actions}>
|
|
141
|
+
<button type="button" disabled={disabled || !valid} onClick={() => { settle(save) }}>{t('custom.save')}</button>
|
|
142
|
+
<button type="button" disabled={disabled} onClick={() => { settle(reset) }}>{t('custom.reset')}</button>
|
|
143
|
+
</div>
|
|
144
|
+
</section>
|
|
145
|
+
)
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
interface StageNumberField {
|
|
149
|
+
label: string
|
|
150
|
+
value: number
|
|
151
|
+
set: (value: number) => void
|
|
152
|
+
integer?: boolean
|
|
153
|
+
allowZero?: boolean
|
|
154
|
+
max?: number
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
export function StageFields({ title, enabled, disabled, onEnabled, fields, step, fieldsEnabled = enabled, t }: {
|
|
158
|
+
title: string
|
|
159
|
+
enabled: boolean
|
|
160
|
+
disabled: boolean
|
|
161
|
+
onEnabled: (enabled: boolean) => void
|
|
162
|
+
fields: readonly StageNumberField[]
|
|
163
|
+
step: number
|
|
164
|
+
fieldsEnabled?: boolean
|
|
165
|
+
t: (key: ContextCompressionLocaleKey) => string
|
|
166
|
+
}) {
|
|
167
|
+
return (
|
|
168
|
+
<fieldset className={css.stage} disabled={disabled}>
|
|
169
|
+
<legend>{title}</legend>
|
|
170
|
+
<label className={css.field}>
|
|
171
|
+
<span>{t('custom.enabled')}</span>
|
|
172
|
+
<select aria-label={title} value={enabled ? 'on' : 'off'}
|
|
173
|
+
onChange={(event) => { onEnabled(event.currentTarget.value === 'on') }}>
|
|
174
|
+
<option value="on">{t('custom.enabled.on')}</option>
|
|
175
|
+
<option value="off">{t('custom.enabled.off')}</option>
|
|
176
|
+
</select>
|
|
177
|
+
</label>
|
|
178
|
+
<div className={css.fieldGrid}>
|
|
179
|
+
{fields.map(field => (
|
|
180
|
+
<label className={css.field} key={field.label}>
|
|
181
|
+
<span>{field.label}</span>
|
|
182
|
+
<input
|
|
183
|
+
type="number"
|
|
184
|
+
value={field.value}
|
|
185
|
+
min={field.allowZero === true ? 0 : field.integer === true ? 1 : step}
|
|
186
|
+
max={field.max}
|
|
187
|
+
step={field.integer === true ? 1 : step}
|
|
188
|
+
disabled={!fieldsEnabled || disabled}
|
|
189
|
+
onChange={(event) => { field.set(Number(event.currentTarget.value)) }}
|
|
190
|
+
/>
|
|
191
|
+
</label>
|
|
192
|
+
))}
|
|
193
|
+
</div>
|
|
194
|
+
</fieldset>
|
|
195
|
+
)
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/** Convert a legacy custom policy to V3 format. */
|
|
199
|
+
export function editableCustom(value: CustomCompressionPolicy): CustomCompressionPolicyV3 {
|
|
200
|
+
if (value.version === 3) return structuredClone(value)
|
|
201
|
+
return {
|
|
202
|
+
version: 3,
|
|
203
|
+
unit: value.unit,
|
|
204
|
+
fresh: structuredClone(value.fresh),
|
|
205
|
+
aggregate: structuredClone(value.aggregate),
|
|
206
|
+
history: {
|
|
207
|
+
enabled: value.history.enabled,
|
|
208
|
+
trigger: value.history.trigger,
|
|
209
|
+
keepRecentToolCalls: 10,
|
|
210
|
+
keepRecentTokens: value.history.keepRecent,
|
|
211
|
+
minReclaim: value.history.minReclaim,
|
|
212
|
+
},
|
|
213
|
+
prefixPolicy: value.prefixPolicy,
|
|
214
|
+
tailTrim: value.version === 1 ? { enabled: false, trigger: 700_000 } : structuredClone(value.tailTrim),
|
|
215
|
+
}
|
|
216
|
+
}
|
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* TokenPilot-inspired estimator channel card components.
|
|
3
|
+
*
|
|
4
|
+
* Extracted from CompressionProfileSelector.tsx to reduce god-module size.
|
|
5
|
+
*
|
|
6
|
+
* @module dsh-context-compression-improved/client/EstimatorControls
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { useEffect, useState } from 'react'
|
|
10
|
+
import type { ContextCompressionLocaleKey } from './locales.ts'
|
|
11
|
+
import type { PresetOptionsPatch } from './preset-options.ts'
|
|
12
|
+
import type { PresetOptionsSettings } from '../profiles.ts'
|
|
13
|
+
import css from './CompressionProfileSelector.module.css'
|
|
14
|
+
|
|
15
|
+
interface EstimatorControlsProps {
|
|
16
|
+
options: PresetOptionsSettings
|
|
17
|
+
disabled: boolean
|
|
18
|
+
save: (options: PresetOptionsPatch) => Promise<void>
|
|
19
|
+
settle: (operation: () => Promise<void>) => void
|
|
20
|
+
t: (key: ContextCompressionLocaleKey) => string
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
interface EstimatorInactiveNoticeProps {
|
|
24
|
+
/** Already-localized label of the profile that currently owns the selector. */
|
|
25
|
+
profile: string
|
|
26
|
+
t: (key: ContextCompressionLocaleKey) => string
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* The estimator card is gated on the tokenpilot-inspired profile, because
|
|
31
|
+
* `presetOptions` is merged into that profile alone. A card that merely
|
|
32
|
+
* disappears reads as a missing feature — the first real-machine report was
|
|
33
|
+
* exactly that — so keep the heading and its anchor id in place and spend them
|
|
34
|
+
* on the reason plus the profile that unlocks the card. The gate itself is
|
|
35
|
+
* unchanged: no estimator control exists outside tokenpilot-inspired.
|
|
36
|
+
*
|
|
37
|
+
* Exported from the estimator card's own module so the gated card and its
|
|
38
|
+
* explanation cannot drift apart.
|
|
39
|
+
*/
|
|
40
|
+
export function EstimatorInactiveNotice({ profile, t }: EstimatorInactiveNoticeProps) {
|
|
41
|
+
return (
|
|
42
|
+
<section className={css.autoCompact} aria-labelledby="context-compression-estimator-title">
|
|
43
|
+
<h3 id="context-compression-estimator-title" className={css.autoCompactTitle}>{t('estimator.title')}</h3>
|
|
44
|
+
<p className={css.customNote}>{t('estimator.inactive').replace('{profile}', profile)}</p>
|
|
45
|
+
</section>
|
|
46
|
+
)
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
interface CatalogModelEntry { readonly id: string, readonly name: string }
|
|
50
|
+
interface CatalogProviderEntry { readonly id: string, readonly name: string, readonly models: readonly CatalogModelEntry[], readonly error?: string }
|
|
51
|
+
interface EstimatorCatalogBody {
|
|
52
|
+
readonly ok?: boolean
|
|
53
|
+
readonly providers?: readonly CatalogProviderEntry[]
|
|
54
|
+
readonly selection?: { readonly provider: string, readonly model: string }
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const ESTIMATOR_CATALOG_ROUTE = '/api/dsh-context-compression-improved/estimator-catalog'
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* TokenPilot-inspired estimator channel card. Shown only while the
|
|
61
|
+
* tokenpilot-inspired profile is selected, and split by channel:
|
|
62
|
+
*
|
|
63
|
+
* - `host` reuses the providers and credentials the user already configured in
|
|
64
|
+
* DSH through the Harness `llm` service, so this card names a provider and a
|
|
65
|
+
* model and accepts NO API key — the key field belongs to the direct channel
|
|
66
|
+
* alone.
|
|
67
|
+
* - `direct` talks to a native OpenAI-compatible endpoint, which is the only
|
|
68
|
+
* channel that carries its own base URL and write-only key.
|
|
69
|
+
*
|
|
70
|
+
* The whole card is advisory: an unconfigured or failing endpoint keeps every
|
|
71
|
+
* consumer on its rule-only fallback.
|
|
72
|
+
*/
|
|
73
|
+
export function EstimatorControls({ options, disabled, save, settle, t }: EstimatorControlsProps) {
|
|
74
|
+
const [keyDraft, setKeyDraft] = useState('')
|
|
75
|
+
const [baseUrl, setBaseUrl] = useState(options.estimatorBaseUrl ?? '')
|
|
76
|
+
const [model, setModel] = useState(options.estimatorModel ?? '')
|
|
77
|
+
const [provider, setProvider] = useState(options.estimatorProvider ?? '')
|
|
78
|
+
const mode = options.estimatorMode ?? ''
|
|
79
|
+
// Host-mode dropdown source: the live provider/model-group catalog served by
|
|
80
|
+
// the Bundle entry's estimator-catalog route (dsh-perm-gate receiver
|
|
81
|
+
// pattern). The provider/model fields are always comboboxes: an empty
|
|
82
|
+
// catalog (route missing / llm service absent) simply offers no suggestions
|
|
83
|
+
// while custom typing keeps working, so nothing breaks on hosts without the
|
|
84
|
+
// webServer.
|
|
85
|
+
const [catalog, setCatalog] = useState<EstimatorCatalogBody | undefined>()
|
|
86
|
+
useEffect(() => {
|
|
87
|
+
if (mode !== 'host') return
|
|
88
|
+
let alive = true
|
|
89
|
+
// The catalog route answers once the host webServer/llm services are up;
|
|
90
|
+
// poll briefly so the dropdowns fill without reopening the panel.
|
|
91
|
+
let attempts = 0
|
|
92
|
+
const load = async (): Promise<EstimatorCatalogBody | undefined> => {
|
|
93
|
+
try {
|
|
94
|
+
const response = await fetch(ESTIMATOR_CATALOG_ROUTE, { headers: { 'cache-control': 'no-cache' } })
|
|
95
|
+
return response.ok ? (await response.json()) as EstimatorCatalogBody : undefined
|
|
96
|
+
} catch {
|
|
97
|
+
return undefined
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
const tick = (): void => {
|
|
101
|
+
attempts += 1
|
|
102
|
+
void load().then((body) => {
|
|
103
|
+
if (!alive) return
|
|
104
|
+
if (body !== undefined && (body.providers?.length ?? 0) > 0) {
|
|
105
|
+
setCatalog(body)
|
|
106
|
+
return
|
|
107
|
+
}
|
|
108
|
+
if (attempts < 10) setTimeout(tick, 3_000)
|
|
109
|
+
})
|
|
110
|
+
}
|
|
111
|
+
tick()
|
|
112
|
+
return () => { alive = false }
|
|
113
|
+
}, [mode])
|
|
114
|
+
const hostProviders = catalog?.providers ?? []
|
|
115
|
+
// Model suggestions follow the provider draft exactly like dsh-perm-gate's
|
|
116
|
+
// receiver card: an empty provider lists every group's models, a chosen
|
|
117
|
+
// provider narrows to its own. The provider error (if any) rides along in
|
|
118
|
+
// the option label so a broken group is visible without blocking typing.
|
|
119
|
+
const providerDraft = provider
|
|
120
|
+
const hostModels = hostProviders
|
|
121
|
+
.filter(entry => providerDraft === '' || entry.id === providerDraft)
|
|
122
|
+
.flatMap(entry => entry.models.map(model => ({ ...model, provider: entry.id })))
|
|
123
|
+
const hostProvider = hostProviders.find(entry => entry.id === providerDraft)
|
|
124
|
+
?? hostProviders.find(entry => entry.id === (options.estimatorProvider ?? ''))
|
|
125
|
+
const hasKey = (options.estimatorApiKey ?? '') !== ''
|
|
126
|
+
const commit = (patch: PresetOptionsPatch) => {
|
|
127
|
+
settle(() => save(patch))
|
|
128
|
+
}
|
|
129
|
+
// What the host channel would actually call right now: an explicit override
|
|
130
|
+
// wins, otherwise the session default the catalog reports. Naming the
|
|
131
|
+
// resolved route is the whole point of this channel — the user re-enters
|
|
132
|
+
// nothing the Harness already knows.
|
|
133
|
+
const overrideProvider = options.estimatorProvider ?? ''
|
|
134
|
+
const overrideModel = options.estimatorModel ?? ''
|
|
135
|
+
const effectiveProvider = overrideProvider !== '' ? overrideProvider : catalog?.selection?.provider ?? ''
|
|
136
|
+
const effectiveModel = overrideModel !== '' ? overrideModel : catalog?.selection?.model ?? ''
|
|
137
|
+
const effectiveRoute = effectiveProvider !== '' && effectiveModel !== ''
|
|
138
|
+
? `${effectiveProvider} / ${effectiveModel}`
|
|
139
|
+
: t('estimator.hostUnresolved')
|
|
140
|
+
return (
|
|
141
|
+
<section className={css.autoCompact} aria-labelledby="context-compression-estimator-title">
|
|
142
|
+
<h3 id="context-compression-estimator-title" className={css.autoCompactTitle}>{t('estimator.title')}</h3>
|
|
143
|
+
<p className={css.customNote}>{t('estimator.description')}</p>
|
|
144
|
+
<label className={css.field}>
|
|
145
|
+
<span>{t('estimator.mode')}</span>
|
|
146
|
+
<select
|
|
147
|
+
value={mode}
|
|
148
|
+
disabled={disabled}
|
|
149
|
+
onChange={(event) => { settle(() => save({ estimatorMode: event.currentTarget.value as '' | 'host' | 'direct' })) }}
|
|
150
|
+
>
|
|
151
|
+
<option value="">{t('estimator.mode.off')}</option>
|
|
152
|
+
<option value="host">{t('estimator.mode.host')}</option>
|
|
153
|
+
<option value="direct">{t('estimator.mode.direct')}</option>
|
|
154
|
+
</select>
|
|
155
|
+
</label>
|
|
156
|
+
{mode === '' ? null : mode === 'host' ? (
|
|
157
|
+
<>
|
|
158
|
+
<label className={css.field}>
|
|
159
|
+
<span>{t('estimator.provider')}</span>
|
|
160
|
+
<input
|
|
161
|
+
type="text"
|
|
162
|
+
list="estimator-provider-options"
|
|
163
|
+
value={provider}
|
|
164
|
+
disabled={disabled}
|
|
165
|
+
placeholder={t('estimator.provider.placeholder')}
|
|
166
|
+
onChange={(event) => {
|
|
167
|
+
const next = event.currentTarget.value
|
|
168
|
+
setProvider(next)
|
|
169
|
+
// Picking from the datalist fires change without blur: commit
|
|
170
|
+
// an exact catalog hit immediately; free typing waits for blur
|
|
171
|
+
// so mid-edit keystrokes never disable the panel.
|
|
172
|
+
if (next !== '' && hostProviders.some(entry => entry.id === next)) {
|
|
173
|
+
commit({ estimatorProvider: next })
|
|
174
|
+
}
|
|
175
|
+
}}
|
|
176
|
+
onBlur={() => { if (provider !== (options.estimatorProvider ?? '')) commit({ estimatorProvider: provider }) }}
|
|
177
|
+
/>
|
|
178
|
+
</label>
|
|
179
|
+
{/* Datalists ride outside the labels on purpose: option text inside a
|
|
180
|
+
label would leak into its accessible name. */}
|
|
181
|
+
<datalist id="estimator-provider-options">
|
|
182
|
+
{hostProviders.map(entry => (
|
|
183
|
+
<option key={entry.id} value={entry.id}>
|
|
184
|
+
{entry.name === '' ? entry.id : entry.name}{entry.error === undefined ? '' : ` (${entry.error})`}
|
|
185
|
+
</option>
|
|
186
|
+
))}
|
|
187
|
+
</datalist>
|
|
188
|
+
<label className={css.field}>
|
|
189
|
+
<span>{t('estimator.model')}</span>
|
|
190
|
+
<input
|
|
191
|
+
type="text"
|
|
192
|
+
list="estimator-model-options"
|
|
193
|
+
value={model}
|
|
194
|
+
disabled={disabled}
|
|
195
|
+
placeholder={t('estimator.model.placeholder')}
|
|
196
|
+
onChange={(event) => {
|
|
197
|
+
const next = event.currentTarget.value
|
|
198
|
+
setModel(next)
|
|
199
|
+
if (next !== '' && hostModels.some(entry => entry.id === next)) {
|
|
200
|
+
commit({ estimatorModel: next })
|
|
201
|
+
}
|
|
202
|
+
}}
|
|
203
|
+
onBlur={() => { if (model !== (options.estimatorModel ?? '')) commit({ estimatorModel: model }) }}
|
|
204
|
+
/>
|
|
205
|
+
</label>
|
|
206
|
+
<datalist id="estimator-model-options">
|
|
207
|
+
{hostModels.map(entry => (
|
|
208
|
+
<option key={`${entry.provider}\0${entry.id}`} value={entry.id}>{entry.name}</option>
|
|
209
|
+
))}
|
|
210
|
+
</datalist>
|
|
211
|
+
{hostProvider?.error === undefined ? null : (
|
|
212
|
+
<p className={css.customNote}>{String(hostProvider.error)}</p>
|
|
213
|
+
)}
|
|
214
|
+
<p className={css.customNote}>{t('estimator.hostReuse')}</p>
|
|
215
|
+
<p className={css.customNote}>{t('estimator.hostRoute').replace('{route}', effectiveRoute)}</p>
|
|
216
|
+
</>
|
|
217
|
+
) : (
|
|
218
|
+
<>
|
|
219
|
+
<label className={css.field}>
|
|
220
|
+
<span>{t('estimator.baseUrl')}</span>
|
|
221
|
+
<input
|
|
222
|
+
type="text"
|
|
223
|
+
value={baseUrl}
|
|
224
|
+
disabled={disabled}
|
|
225
|
+
placeholder="https://127.0.0.1:8000/v1"
|
|
226
|
+
onChange={(event) => { setBaseUrl(event.currentTarget.value) }}
|
|
227
|
+
onBlur={() => { if (baseUrl !== (options.estimatorBaseUrl ?? '')) commit({ estimatorBaseUrl: baseUrl }) }}
|
|
228
|
+
/>
|
|
229
|
+
</label>
|
|
230
|
+
<label className={css.field}>
|
|
231
|
+
<span>{t('estimator.model')}</span>
|
|
232
|
+
<input
|
|
233
|
+
type="text"
|
|
234
|
+
value={model}
|
|
235
|
+
disabled={disabled}
|
|
236
|
+
onChange={(event) => { setModel(event.currentTarget.value) }}
|
|
237
|
+
onBlur={() => { if (model !== (options.estimatorModel ?? '')) commit({ estimatorModel: model }) }}
|
|
238
|
+
/>
|
|
239
|
+
</label>
|
|
240
|
+
<label className={css.field}>
|
|
241
|
+
<span>{t('estimator.apiKey')}</span>
|
|
242
|
+
<div style={{ display: 'flex', gap: '6px' }}>
|
|
243
|
+
<input
|
|
244
|
+
type="password"
|
|
245
|
+
autoComplete="off"
|
|
246
|
+
spellCheck={false}
|
|
247
|
+
value={keyDraft}
|
|
248
|
+
disabled={disabled}
|
|
249
|
+
placeholder={hasKey ? t('estimator.apiKey.set') : t('estimator.apiKey.placeholder')}
|
|
250
|
+
onChange={(event) => { setKeyDraft(event.currentTarget.value) }}
|
|
251
|
+
onBlur={() => {
|
|
252
|
+
const next = keyDraft.trim()
|
|
253
|
+
if (next === '') return
|
|
254
|
+
settle(() => save({ estimatorApiKey: next }))
|
|
255
|
+
setKeyDraft('')
|
|
256
|
+
}}
|
|
257
|
+
onKeyDown={(event) => {
|
|
258
|
+
if (event.key === 'Enter') event.currentTarget.blur()
|
|
259
|
+
}}
|
|
260
|
+
/>
|
|
261
|
+
{hasKey ? (
|
|
262
|
+
<button
|
|
263
|
+
type="button"
|
|
264
|
+
disabled={disabled}
|
|
265
|
+
onClick={() => { settle(() => save({ estimatorApiKey: undefined })) }}
|
|
266
|
+
>
|
|
267
|
+
{t('estimator.apiKey.clear')}
|
|
268
|
+
</button>
|
|
269
|
+
) : null}
|
|
270
|
+
</div>
|
|
271
|
+
{hasKey ? <span className={css.customNote}>{t('estimator.apiKey.overwrite')}</span> : null}
|
|
272
|
+
</label>
|
|
273
|
+
</>
|
|
274
|
+
)}
|
|
275
|
+
</section>
|
|
276
|
+
)
|
|
277
|
+
}
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Bridge-backed settings scope — the default data plane on legacy hosts
|
|
3
|
+
* (0.1.0–0.1.5), where the client `settingsScope.bind` face never becomes
|
|
4
|
+
* ready (Gate 0: status "unavailable", writes rejected) and configForms does
|
|
5
|
+
* not exist. Speaks HTTP to the settings bridge route (settings-bridge.ts)
|
|
6
|
+
* and implements the same `SettingsScope` face the configForms scope builds,
|
|
7
|
+
* so the selector components stay generation-agnostic.
|
|
8
|
+
*
|
|
9
|
+
* Detection-free by design (audit P1-9): no property probing, no timeouts —
|
|
10
|
+
* the bridge works on every line, and on 0.1.7+ the configForms scoped
|
|
11
|
+
* sub-inject resolves and replaces this scope with the native one.
|
|
12
|
+
*/
|
|
13
|
+
import type { ScopeSnapshot, SettingsScope } from './scope-face.ts'
|
|
14
|
+
import { decodeSettings } from './decode.ts'
|
|
15
|
+
import type { ContextCompressionSettings } from './CompressionProfileSelector.tsx'
|
|
16
|
+
|
|
17
|
+
const SETTINGS_ROUTE = '/api/dsh-context-compression-improved/settings'
|
|
18
|
+
const POLL_MS = 4000
|
|
19
|
+
|
|
20
|
+
export interface BridgeScope extends SettingsScope<ContextCompressionSettings> {
|
|
21
|
+
/** Kick an immediate refresh (called once at apply). */
|
|
22
|
+
refresh(): void
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function createBridgeScope(): BridgeScope {
|
|
26
|
+
let snapshot: ScopeSnapshot<ContextCompressionSettings> = {
|
|
27
|
+
status: 'loading', value: undefined, revision: undefined,
|
|
28
|
+
writable: true, base: undefined, user: undefined, mode: 'host',
|
|
29
|
+
}
|
|
30
|
+
const listeners = new Set<() => void>()
|
|
31
|
+
let pollTimer: ReturnType<typeof setInterval> | undefined
|
|
32
|
+
|
|
33
|
+
const emit = () => { for (const listener of listeners) listener() }
|
|
34
|
+
|
|
35
|
+
const applyDoc = (doc: unknown, revision: number | undefined): void => {
|
|
36
|
+
const value = decodeSettings(doc)
|
|
37
|
+
const next: ScopeSnapshot<ContextCompressionSettings> = {
|
|
38
|
+
status: value === undefined ? 'unavailable' : 'ready',
|
|
39
|
+
value,
|
|
40
|
+
revision,
|
|
41
|
+
writable: true,
|
|
42
|
+
base: undefined,
|
|
43
|
+
user: undefined,
|
|
44
|
+
mode: 'host',
|
|
45
|
+
}
|
|
46
|
+
if (JSON.stringify(next) !== JSON.stringify(snapshot)) {
|
|
47
|
+
snapshot = next
|
|
48
|
+
emit()
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
async function refresh(): Promise<void> {
|
|
53
|
+
try {
|
|
54
|
+
const response = await fetch(SETTINGS_ROUTE, { headers: { 'cache-control': 'no-cache' } })
|
|
55
|
+
if (!response.ok) throw new Error(`settings bridge ${response.status}`)
|
|
56
|
+
const body = await response.json() as { doc?: unknown; revision?: number | null }
|
|
57
|
+
applyDoc(body.doc, body.revision ?? undefined)
|
|
58
|
+
} catch {
|
|
59
|
+
if (snapshot.status !== 'unavailable') {
|
|
60
|
+
snapshot = { ...snapshot, status: 'unavailable' }
|
|
61
|
+
emit()
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
async function writeDoc(next: ContextCompressionSettings): Promise<boolean> {
|
|
67
|
+
const response = await fetch(SETTINGS_ROUTE, {
|
|
68
|
+
method: 'POST',
|
|
69
|
+
headers: { 'content-type': 'application/json' },
|
|
70
|
+
body: JSON.stringify({ doc: next }),
|
|
71
|
+
})
|
|
72
|
+
if (!response.ok) return false
|
|
73
|
+
const body = await response.json() as { ok?: boolean; doc?: unknown }
|
|
74
|
+
if (body.ok !== true) return false
|
|
75
|
+
applyDoc(body.doc ?? next, (snapshot.revision ?? 0) + 1)
|
|
76
|
+
return true
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const cloneDoc = (): ContextCompressionSettings => {
|
|
80
|
+
const value = snapshot.value
|
|
81
|
+
return (value === undefined ? {} : structuredClone(value)) as ContextCompressionSettings
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const scope: BridgeScope = {
|
|
85
|
+
getSnapshot: () => snapshot,
|
|
86
|
+
subscribe: (listener) => {
|
|
87
|
+
listeners.add(listener)
|
|
88
|
+
if (listeners.size === 1) {
|
|
89
|
+
void refresh()
|
|
90
|
+
pollTimer = setInterval(() => { void refresh() }, POLL_MS)
|
|
91
|
+
}
|
|
92
|
+
return () => {
|
|
93
|
+
listeners.delete(listener)
|
|
94
|
+
if (listeners.size === 0 && pollTimer !== undefined) {
|
|
95
|
+
clearInterval(pollTimer)
|
|
96
|
+
pollTimer = undefined
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
},
|
|
100
|
+
set: async (field, value) => {
|
|
101
|
+
const next = cloneDoc() as unknown as Record<string, unknown>
|
|
102
|
+
next[field] = value
|
|
103
|
+
return writeDoc(next as unknown as ContextCompressionSettings)
|
|
104
|
+
},
|
|
105
|
+
unset: async (field) => {
|
|
106
|
+
const next = cloneDoc() as unknown as Record<string, unknown>
|
|
107
|
+
delete next[field]
|
|
108
|
+
return writeDoc(next as unknown as ContextCompressionSettings)
|
|
109
|
+
},
|
|
110
|
+
mutate: async (ops) => {
|
|
111
|
+
const doc = cloneDoc() as unknown as Record<string, unknown>
|
|
112
|
+
for (const op of ops) {
|
|
113
|
+
const [head, key] = op.path as [string, string]
|
|
114
|
+
if (head !== 'presetOptions') return false
|
|
115
|
+
const section = { ...((doc.presetOptions ?? {}) as Record<string, unknown>) }
|
|
116
|
+
if (op.op === 'unset') delete section[key]
|
|
117
|
+
else section[key] = (op as { value?: unknown }).value
|
|
118
|
+
doc.presetOptions = section
|
|
119
|
+
}
|
|
120
|
+
return writeDoc(doc as unknown as ContextCompressionSettings)
|
|
121
|
+
},
|
|
122
|
+
refresh: () => { void refresh() },
|
|
123
|
+
}
|
|
124
|
+
return scope
|
|
125
|
+
}
|
|
@@ -0,0 +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
|
+
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
|
+
}
|