dsh-context-compression-improved 0.10.4 → 0.11.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.
Files changed (310) hide show
  1. package/.agents/plans/improve/checklist.md +32 -0
  2. package/.agents/plans/improve/findings.md +102 -0
  3. package/.agents/plans/improve/spec.md +53 -0
  4. package/.agents/plans/improve/tasks.md +70 -0
  5. package/.gitattributes +2 -0
  6. package/.githooks/pre-push +37 -0
  7. package/.github/workflows/ci.yml +47 -0
  8. package/CHANGELOG.de.md +580 -0
  9. package/CHANGELOG.es.md +588 -0
  10. package/CHANGELOG.fr.md +576 -0
  11. package/CHANGELOG.it.md +577 -0
  12. package/CHANGELOG.ja.md +261 -0
  13. package/CHANGELOG.ko.md +254 -0
  14. package/CHANGELOG.md +557 -0
  15. package/CHANGELOG.ru.md +554 -0
  16. package/CHANGELOG.zh.md +247 -0
  17. package/CONTRIBUTING.md +22 -0
  18. package/README.de.md +96 -0
  19. package/README.es.md +96 -0
  20. package/README.fr.md +96 -0
  21. package/README.it.md +96 -0
  22. package/README.ja.md +102 -0
  23. package/README.ko.md +102 -0
  24. package/README.md +110 -15
  25. package/README.ru.md +96 -0
  26. package/README.zh.md +104 -14
  27. package/SECURITY.md +18 -0
  28. package/THIRD_PARTY_NOTICES.md +7 -25
  29. package/assets/board.png +0 -0
  30. package/assets/context.png +0 -0
  31. package/docs/assets/context-compression-selector-profiles.jpg +0 -0
  32. package/docs/assets/context-compression-selector-settings.png +0 -0
  33. package/docs/compat-0.1.5-report.md +77 -0
  34. package/docs/installation.de.md +104 -0
  35. package/docs/installation.es.md +104 -0
  36. package/docs/installation.fr.md +104 -0
  37. package/docs/installation.it.md +104 -0
  38. package/docs/installation.ja.md +78 -0
  39. package/docs/installation.ko.md +78 -0
  40. package/docs/installation.md +103 -0
  41. package/docs/installation.ru.md +104 -0
  42. package/docs/installation.zh.md +100 -0
  43. package/docs/repair-log.md +657 -0
  44. package/docs/shallow-teaching-preset-template.md +46 -0
  45. package/eslint.config.js +30 -0
  46. package/package.json +116 -129
  47. package/packages/selector/LICENSE +21 -0
  48. package/packages/selector/README.md +26 -0
  49. package/packages/selector/README.zh.md +24 -0
  50. package/packages/selector/THIRD_PARTY_NOTICES.md +32 -0
  51. package/packages/selector/docs/history-tool-call-working-set-spec.md +112 -0
  52. package/packages/selector/docs/native-tool-result-selector-spec.md +34 -0
  53. package/packages/selector/docs/subagent-cache-reuse-spec.md +46 -0
  54. package/{dsh.plugin.json → packages/selector/dsh.plugin.json} +1 -1
  55. package/{lib → packages/selector/lib}/client.d.ts +8 -0
  56. package/{lib → packages/selector/lib}/client.js +212 -3
  57. package/{lib → packages/selector/lib}/index.js +1 -1
  58. package/{lib → packages/selector/lib}/profiles.d.ts +34 -0
  59. package/{lib → packages/selector/lib}/pruner.d.ts +55 -0
  60. package/{lib → packages/selector/lib}/pruner.js +1925 -303
  61. package/{lib → packages/selector/lib}/savings-integral.js +126 -3
  62. package/packages/selector/lib/style.css +350 -0
  63. package/{lib → packages/selector/lib}/tool-result.js +9 -1
  64. package/packages/selector/package.json +178 -0
  65. package/packages/selector/screenshots.json +3 -0
  66. package/packages/selector/src/client/CompressionProfileControls.tsx +352 -0
  67. package/packages/selector/src/client/CompressionProfileSelector.module.css +199 -0
  68. package/packages/selector/src/client/CompressionProfileSelector.tsx +89 -0
  69. package/packages/selector/src/client/CustomPolicyEditor.tsx +216 -0
  70. package/packages/selector/src/client/EstimatorControls.tsx +277 -0
  71. package/packages/selector/src/client/bridge-scope.ts +125 -0
  72. package/packages/selector/src/client/decode.ts +68 -0
  73. package/packages/selector/src/client/fab-pos.ts +71 -0
  74. package/packages/selector/src/client/index.ts +438 -0
  75. package/packages/selector/src/client/locales/de.ts +167 -0
  76. package/packages/selector/src/client/locales/es.ts +167 -0
  77. package/packages/selector/src/client/locales/fr.ts +167 -0
  78. package/packages/selector/src/client/locales/it.ts +167 -0
  79. package/packages/selector/src/client/locales/ja.ts +167 -0
  80. package/packages/selector/src/client/locales/ko.ts +167 -0
  81. package/packages/selector/src/client/locales/ru.ts +167 -0
  82. package/packages/selector/src/client/locales.ts +334 -0
  83. package/packages/selector/src/client/monitor-fab.ts +445 -0
  84. package/packages/selector/src/client/preset-options.ts +88 -0
  85. package/packages/selector/src/client/savings-card.tsx +269 -0
  86. package/packages/selector/src/client/scope-face.ts +24 -0
  87. package/packages/selector/src/client/settings-section.tsx +156 -0
  88. package/packages/selector/src/compat/host-generation.ts +96 -0
  89. package/packages/selector/src/compat/llm-messages.ts +50 -0
  90. package/packages/selector/src/compat/settings-lease.ts +108 -0
  91. package/packages/selector/src/compat/tool-result.ts +91 -0
  92. package/packages/selector/src/css-modules.d.ts +6 -0
  93. package/packages/selector/src/deepseek-v4-tokenizer.ts +205 -0
  94. package/packages/selector/src/estimator-catalog.ts +104 -0
  95. package/packages/selector/src/index.ts +890 -0
  96. package/packages/selector/src/invariant.ts +113 -0
  97. package/packages/selector/src/preset-overlay.ts +749 -0
  98. package/packages/selector/src/profiles.ts +554 -0
  99. package/packages/selector/src/pruner/content.ts +199 -0
  100. package/packages/selector/src/pruner/session.ts +97 -0
  101. package/packages/selector/src/pruner/state.ts +61 -0
  102. package/packages/selector/src/pruner/tuning.ts +23 -0
  103. package/packages/selector/src/pruner/types.ts +78 -0
  104. package/packages/selector/src/pruner.ts +3222 -0
  105. package/packages/selector/src/runtime/adaptive-cost.ts +205 -0
  106. package/packages/selector/src/runtime/audit.ts +321 -0
  107. package/packages/selector/src/runtime/config.ts +887 -0
  108. package/packages/selector/src/runtime/custom-policy.ts +300 -0
  109. package/packages/selector/src/runtime/deepseek-official-pricing.ts +298 -0
  110. package/packages/selector/src/runtime/deepseek-v4-vision-tokens.ts +254 -0
  111. package/packages/selector/src/runtime/display-prefs.ts +98 -0
  112. package/packages/selector/src/runtime/fold-router.ts +143 -0
  113. package/packages/selector/src/runtime/measurement.ts +444 -0
  114. package/packages/selector/src/runtime/monitor.ts +341 -0
  115. package/packages/selector/src/runtime/panel-command.ts +213 -0
  116. package/packages/selector/src/runtime/reducers.ts +1755 -0
  117. package/packages/selector/src/runtime/retrieve.ts +505 -0
  118. package/packages/selector/src/runtime/savings-integral.ts +117 -0
  119. package/packages/selector/src/runtime/savings-projection.ts +229 -0
  120. package/packages/selector/src/runtime/savings-verify.ts +446 -0
  121. package/packages/selector/src/runtime/savings.ts +526 -0
  122. package/packages/selector/src/runtime/sediment-card.ts +126 -0
  123. package/packages/selector/src/runtime/sediment-clock.ts +87 -0
  124. package/packages/selector/src/runtime/sediment-guard.ts +136 -0
  125. package/packages/selector/src/runtime/sediment-publish.ts +144 -0
  126. package/packages/selector/src/runtime/sediment-span.ts +170 -0
  127. package/packages/selector/src/runtime/sediment.ts +209 -0
  128. package/packages/selector/src/runtime/session-events.ts +30 -0
  129. package/packages/selector/src/runtime/session-file-decode.ts +523 -0
  130. package/packages/selector/src/runtime/shallow-tools.ts +342 -0
  131. package/packages/selector/src/runtime/stats-backfill.ts +541 -0
  132. package/packages/selector/src/runtime/stats-db.ts +492 -0
  133. package/packages/selector/src/runtime/stats-schema.ts +123 -0
  134. package/packages/selector/src/runtime/stats-sync.ts +307 -0
  135. package/packages/selector/src/runtime/summary-command.ts +127 -0
  136. package/packages/selector/src/runtime/tail-trim.ts +177 -0
  137. package/packages/selector/src/runtime/token-count.ts +72 -0
  138. package/packages/selector/src/runtime/tokenpilot/advisor-prompt.ts +233 -0
  139. package/packages/selector/src/runtime/tokenpilot/advisor-state.ts +204 -0
  140. package/packages/selector/src/runtime/tokenpilot/advisor.ts +419 -0
  141. package/packages/selector/src/runtime/tokenpilot/benefit.ts +200 -0
  142. package/packages/selector/src/runtime/tokenpilot/dedup.ts +82 -0
  143. package/packages/selector/src/runtime/tokenpilot/estimator.ts +151 -0
  144. package/packages/selector/src/runtime/tokenpilot/intent-fold.ts +53 -0
  145. package/packages/selector/src/runtime/tokenpilot/intent-gate.ts +65 -0
  146. package/packages/selector/src/runtime/tokenpilot/intent-input-mask.ts +125 -0
  147. package/packages/selector/src/runtime/tokenpilot/intent-range.ts +37 -0
  148. package/packages/selector/src/runtime/tokenpilot/locator.ts +129 -0
  149. package/packages/selector/src/runtime/tokenpilot/read-state.ts +98 -0
  150. package/packages/selector/src/runtime/tokenpilot/sidechannel.ts +303 -0
  151. package/packages/selector/src/runtime/tool-names.ts +42 -0
  152. package/packages/selector/src/runtime/toolclass.ts +103 -0
  153. package/packages/selector/src/runtime/triage/masks.ts +128 -0
  154. package/packages/selector/src/runtime/triage/shallow.ts +132 -0
  155. package/packages/selector/src/runtime/triage/signals.ts +167 -0
  156. package/packages/selector/src/runtime/triage/types.ts +125 -0
  157. package/packages/selector/src/runtime/types.ts +396 -0
  158. package/packages/selector/src/runtime/value.ts +48 -0
  159. package/packages/selector/src/settings-bridge.ts +175 -0
  160. package/packages/selector/tests/advisor-report.host.spec.ts +223 -0
  161. package/packages/selector/tests/auto-compact.client.spec.tsx +214 -0
  162. package/packages/selector/tests/built/client-artifact.spec.ts +56 -0
  163. package/packages/selector/tests/cache-prefix-audit.spec.ts +123 -0
  164. package/packages/selector/tests/code-skeleton.client.spec.ts +98 -0
  165. package/packages/selector/tests/compat/compat-narrow-waist.spec.ts +150 -0
  166. package/packages/selector/tests/custom-contract.client.spec.ts +226 -0
  167. package/packages/selector/tests/estimator-catalog.spec.ts +70 -0
  168. package/packages/selector/tests/estimator-channel.client.spec.tsx +253 -0
  169. package/packages/selector/tests/estimator-route-registration.host.spec.ts +202 -0
  170. package/packages/selector/tests/fab-pos.client.spec.ts +47 -0
  171. package/packages/selector/tests/fixtures/sediment-pool/52adc205-cloud-5.orig.txt +231 -0
  172. package/packages/selector/tests/fixtures/sediment-pool/95fcba5d-cloud-25.orig.txt +236 -0
  173. package/packages/selector/tests/fixtures/sediment-pool/95fcba5d-cloud-38.orig.txt +220 -0
  174. package/packages/selector/tests/fixtures/sediment-pool/95fcba5d-cloud-42.orig.txt +140 -0
  175. package/packages/selector/tests/fixtures/sediment-pool/95fcba5d-cloud-53.orig.txt +117 -0
  176. package/packages/selector/tests/fixtures/sediment-pool/95fcba5d-local-10.orig.txt +67 -0
  177. package/packages/selector/tests/fixtures/sediment-pool/a7a767c0-cloud-45.orig.txt +119 -0
  178. package/packages/selector/tests/fixtures/sediment-pool/a7a767c0-cloud-49.orig.txt +119 -0
  179. package/packages/selector/tests/fixtures/sediment-pool/a7a767c0-cloud-63.orig.txt +131 -0
  180. package/packages/selector/tests/fixtures/sediment-pool/a7a767c0-cloud-75.orig.txt +196 -0
  181. package/packages/selector/tests/fixtures/sediment-pool/a7a767c0-cloud-76.orig.txt +71 -0
  182. package/packages/selector/tests/fixtures/sediment-pool/a7a767c0-cloud-85.orig.txt +126 -0
  183. package/packages/selector/tests/fixtures/sediment-pool/a7a767c0-cloud-86.orig.txt +86 -0
  184. package/packages/selector/tests/fixtures/sediment-pool/a7a767c0-cloud-87.orig.txt +50 -0
  185. package/packages/selector/tests/fixtures/sediment-pool/a7a767c0-cloud-96.orig.txt +102 -0
  186. package/packages/selector/tests/helpers/legacy-namespace.ts +35 -0
  187. package/packages/selector/tests/helpers/legacy-settings/index.ts +15 -0
  188. package/packages/selector/tests/helpers/legacy-settings/legacy/provider.d.ts +41 -0
  189. package/packages/selector/tests/helpers/legacy-settings/legacy/provider.js +647 -0
  190. package/packages/selector/tests/helpers/legacy-settings/legacy/redact.js +78 -0
  191. package/packages/selector/tests/helpers/legacy-settings/legacy/util-values.js +234 -0
  192. package/packages/selector/tests/host-preset-overlay.host.spec.ts +204 -0
  193. package/packages/selector/tests/monitor-fab.client.spec.ts +240 -0
  194. package/packages/selector/tests/preset-options-write.client.spec.ts +192 -0
  195. package/packages/selector/tests/preset-overlay-loader.e2e.host.spec.ts +200 -0
  196. package/packages/selector/tests/preset-overlay.host.spec.ts +246 -0
  197. package/packages/selector/tests/profiles.client.spec.tsx +436 -0
  198. package/packages/selector/tests/ptc-custom-write.client.spec.ts +86 -0
  199. package/packages/selector/tests/public/package-contract.client.spec.ts +89 -0
  200. package/packages/selector/tests/runtime/adaptive-cost.spec.ts +167 -0
  201. package/packages/selector/tests/runtime/advice-never-withholds.host.spec.ts +230 -0
  202. package/packages/selector/tests/runtime/advisor-invariant.spec.ts +272 -0
  203. package/packages/selector/tests/runtime/advisor.spec.ts +226 -0
  204. package/packages/selector/tests/runtime/audit.spec.ts +275 -0
  205. package/packages/selector/tests/runtime/auto-compact-config.spec.ts +523 -0
  206. package/packages/selector/tests/runtime/char-basis.spec.ts +30 -0
  207. package/packages/selector/tests/runtime/code-skeleton.spec.ts +152 -0
  208. package/packages/selector/tests/runtime/deepseek-official-pricing.spec.ts +186 -0
  209. package/packages/selector/tests/runtime/deepseek-v4-tokenizer.spec.ts +122 -0
  210. package/packages/selector/tests/runtime/deepseek-v4-vision-tokens.spec.ts +122 -0
  211. package/packages/selector/tests/runtime/deprecated-preset-options.spec.ts +96 -0
  212. package/packages/selector/tests/runtime/fixtures/profile-baseline.json +273 -0
  213. package/packages/selector/tests/runtime/fixtures/session-v4-dump.jsonl +46 -0
  214. package/packages/selector/tests/runtime/fixtures/tokenizer-golden.json +106 -0
  215. package/packages/selector/tests/runtime/fixtures/vision-golden.json +459 -0
  216. package/packages/selector/tests/runtime/fold-router.spec.ts +140 -0
  217. package/packages/selector/tests/runtime/frequency-longstrings.spec.ts +74 -0
  218. package/packages/selector/tests/runtime/fresh-gate.spec.ts +43 -0
  219. package/packages/selector/tests/runtime/html-reducer.spec.ts +212 -0
  220. package/packages/selector/tests/runtime/intent-gate.spec.ts +62 -0
  221. package/packages/selector/tests/runtime/intent-summary.spec.ts +156 -0
  222. package/packages/selector/tests/runtime/jsonl-reducer.spec.ts +79 -0
  223. package/packages/selector/tests/runtime/line-mapping.spec.ts +153 -0
  224. package/packages/selector/tests/runtime/monitor.spec.ts +354 -0
  225. package/packages/selector/tests/runtime/panel-command.spec.ts +101 -0
  226. package/packages/selector/tests/runtime/prose-reducers.spec.ts +133 -0
  227. package/packages/selector/tests/runtime/ptc-profile.spec.ts +133 -0
  228. package/packages/selector/tests/runtime/public/public-runtime.spec.ts +2795 -0
  229. package/packages/selector/tests/runtime/read-input-cap.spec.ts +33 -0
  230. package/packages/selector/tests/runtime/savings-accounting.spec.ts +291 -0
  231. package/packages/selector/tests/runtime/savings-integral.spec.ts +169 -0
  232. package/packages/selector/tests/runtime/savings-projection.spec.ts +89 -0
  233. package/packages/selector/tests/runtime/savings-verify.spec.ts +226 -0
  234. package/packages/selector/tests/runtime/savings.spec.ts +139 -0
  235. package/packages/selector/tests/runtime/search-reducer.spec.ts +110 -0
  236. package/packages/selector/tests/runtime/sediment-card.spec.ts +111 -0
  237. package/packages/selector/tests/runtime/sediment-config.spec.ts +56 -0
  238. package/packages/selector/tests/runtime/sediment-guard.spec.ts +71 -0
  239. package/packages/selector/tests/runtime/sediment-masks.spec.ts +95 -0
  240. package/packages/selector/tests/runtime/sediment-orchestrator.spec.ts +166 -0
  241. package/packages/selector/tests/runtime/sediment-pool-share.spec.ts +65 -0
  242. package/packages/selector/tests/runtime/sediment-publish.spec.ts +142 -0
  243. package/packages/selector/tests/runtime/sediment-real-shapes.spec.ts +158 -0
  244. package/packages/selector/tests/runtime/sediment-settings.spec.ts +73 -0
  245. package/packages/selector/tests/runtime/sediment-signals.spec.ts +124 -0
  246. package/packages/selector/tests/runtime/sediment-span.spec.ts +92 -0
  247. package/packages/selector/tests/runtime/session-events.spec.ts +27 -0
  248. package/packages/selector/tests/runtime/session-file-decode.spec.ts +281 -0
  249. package/packages/selector/tests/runtime/settings-unwrap.spec.ts +58 -0
  250. package/packages/selector/tests/runtime/shallow-primitives.spec.ts +135 -0
  251. package/packages/selector/tests/runtime/sidechannel.spec.ts +241 -0
  252. package/packages/selector/tests/runtime/stats-backfill-scheduler.spec.ts +167 -0
  253. package/packages/selector/tests/runtime/stats-backfill.spec.ts +350 -0
  254. package/packages/selector/tests/runtime/stats-db.spec.ts +361 -0
  255. package/packages/selector/tests/runtime/stats-privacy.spec.ts +55 -0
  256. package/packages/selector/tests/runtime/stats-sync.spec.ts +278 -0
  257. package/packages/selector/tests/runtime/summary-command.spec.ts +154 -0
  258. package/packages/selector/tests/runtime/toc-and-bundled.spec.ts +159 -0
  259. package/packages/selector/tests/runtime/tokenizer-golden.spec.ts +53 -0
  260. package/packages/selector/tests/runtime/tokenpilot/benefit.spec.ts +217 -0
  261. package/packages/selector/tests/runtime/tokenpilot/dedup.spec.ts +52 -0
  262. package/packages/selector/tests/runtime/tokenpilot/estimator.spec.ts +79 -0
  263. package/packages/selector/tests/runtime/tokenpilot/locator.spec.ts +76 -0
  264. package/packages/selector/tests/runtime/tokenpilot/profile-baseline.spec.ts +121 -0
  265. package/packages/selector/tests/runtime/tokenpilot/read-state.spec.ts +82 -0
  266. package/packages/selector/tests/runtime/toolclass.spec.ts +156 -0
  267. package/packages/selector/tests/runtime/value.spec.ts +23 -0
  268. package/packages/selector/tests/savings-sessions.client.spec.tsx +140 -0
  269. package/packages/selector/tests/settings-seat.client.spec.ts +215 -0
  270. package/packages/selector/tests/standing-generation.host.spec.ts +724 -0
  271. package/packages/selector/tests/subagent-cache-reuse.host.spec.ts +269 -0
  272. package/packages/selector/tests/support/cache-prefix-audit.ts +105 -0
  273. package/packages/selector/tests/support/mock-adapter.ts +37 -0
  274. package/packages/selector/tests/support/ui-primitives.tsx +34 -0
  275. package/packages/selector/tsconfig.json +11 -0
  276. package/packages/selector/tsdown.client.config.ts +102 -0
  277. package/packages/selector/tsdown.config.ts +20 -0
  278. package/pnpm-workspace.yaml +76 -0
  279. package/screenshots.json +6 -3
  280. package/scripts/capture-profile-baseline.mjs +42 -0
  281. package/scripts/cell-browser-e2e.mjs +354 -0
  282. package/scripts/generate-tokenizer-fixtures.py +81 -0
  283. package/scripts/generate-vision-fixtures.py +208 -0
  284. package/scripts/hosts.d.mts +3 -0
  285. package/scripts/hosts.mjs +79 -0
  286. package/scripts/packed-components-smoke.mjs +763 -0
  287. package/scripts/packed-install-e2e.mjs +1346 -0
  288. package/scripts/sediment-latency-report.mjs +63 -0
  289. package/scripts/sync-hosts.mjs +174 -0
  290. package/scripts/toolclass-corpus-replay.mjs +281 -0
  291. package/scripts/verify-release.mjs +343 -0
  292. package/tests/TEST_INVENTORY.md +41 -0
  293. package/tsconfig.base.json +18 -0
  294. package/tsconfig.json +7 -0
  295. package/tsconfig.tests.json +15 -0
  296. package/vitest.built.config.ts +9 -0
  297. package/vitest.config.ts +54 -0
  298. /package/{assets → packages/selector/assets}/deepseek-v4/LICENSE.DeepSeek-V4-Pro.txt +0 -0
  299. /package/{assets → packages/selector/assets}/deepseek-v4/manifest.json +0 -0
  300. /package/{assets → packages/selector/assets}/deepseek-v4/tokenizer.json +0 -0
  301. /package/{assets → packages/selector/assets}/deepseek-v4/tokenizer_config.json +0 -0
  302. /package/{assets → packages/selector/assets}/deepseek-v4-vision-exp/LICENSE.DeepSeek-V4-Flash-Vision-Exp.txt +0 -0
  303. /package/{assets → packages/selector/assets}/deepseek-v4-vision-exp/manifest.json +0 -0
  304. /package/{assets → packages/selector/assets}/deepseek-v4-vision-exp/tokenizer.json +0 -0
  305. /package/{assets → packages/selector/assets}/deepseek-v4-vision-exp/tokenizer_config.json +0 -0
  306. /package/{cordis.patch.yml → packages/selector/cordis.patch.yml} +0 -0
  307. /package/{lib → packages/selector/lib}/index.d.ts +0 -0
  308. /package/{lib → packages/selector/lib}/invariant.d.ts +0 -0
  309. /package/{lib → packages/selector/lib}/invariant.js +0 -0
  310. /package/{lib → packages/selector/lib}/tail-trim.js +0 -0
@@ -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,68 @@
1
+ /** Browser-safe settings decoding shared by the client entry and node tests. */
2
+
3
+ import {
4
+ canonicalizeCustomPolicy,
5
+ COMPRESSION_PROFILES,
6
+ DEFAULT_PTC_COMPRESSION_POLICY,
7
+ decodeAutoCompactSettings,
8
+ decodeCodeSkeletonSettings,
9
+ decodeIntentSummarySettings,
10
+ decodeMonitorPanelSettings,
11
+ decodePresetOptionsSettings,
12
+ decodeSedimentSettings,
13
+ isCustomCompressionPolicy,
14
+ isPlainRecord,
15
+ type CompressionProfile,
16
+ type ContextCompressionSettings,
17
+ } from '../profiles.ts'
18
+
19
+ /**
20
+ * Decode one stored context-compression settings document with exactly the
21
+ * runtime schema's strictness: a plain object with only `profile`, `custom`,
22
+ * `autoCompact`, `codeSkeleton`, and `intentSummary` keys, a supported profile, a valid Custom
23
+ * document canonicalized to v3 exactly as the runtime resolver would, a
24
+ * strictly-shaped autoCompact section (absent inherits the 80% default), and
25
+ * a strictly-shaped codeSkeleton gate (absent inherits off). Anything else
26
+ * decodes to `undefined` so the UI reports the document as unreadable instead
27
+ * of silently disagreeing with the runtime.
28
+ */
29
+ export function decodeSettings(value: unknown): ContextCompressionSettings | undefined {
30
+ if (!isPlainRecord(value)) return undefined
31
+ const keys = Object.keys(value)
32
+ if (keys.some(key => key !== 'profile' && key !== 'custom' && key !== 'ptcCustom' && key !== 'autoCompact' && key !== 'codeSkeleton' && key !== 'intentSummary' && key !== 'monitorPanel' && key !== 'sediment' && key !== 'presetOptions')) {
33
+ return undefined
34
+ }
35
+ const profile = (value as { profile?: unknown }).profile
36
+ const custom = (value as { custom?: unknown }).custom
37
+ const ptcCustomRaw = (value as { ptcCustom?: unknown }).ptcCustom
38
+ const ptcCustom = ptcCustomRaw === undefined
39
+ ? DEFAULT_PTC_COMPRESSION_POLICY
40
+ : isCustomCompressionPolicy(ptcCustomRaw) ? canonicalizeCustomPolicy(ptcCustomRaw) : undefined
41
+ const autoCompact = decodeAutoCompactSettings((value as { autoCompact?: unknown }).autoCompact)
42
+ const codeSkeleton = decodeCodeSkeletonSettings((value as { codeSkeleton?: unknown }).codeSkeleton)
43
+ const intentSummary = decodeIntentSummarySettings((value as { intentSummary?: unknown }).intentSummary)
44
+ const monitorPanel = decodeMonitorPanelSettings((value as { monitorPanel?: unknown }).monitorPanel)
45
+ const sediment = decodeSedimentSettings((value as { sediment?: unknown }).sediment)
46
+ const presetOptions = decodePresetOptionsSettings((value as { presetOptions?: unknown }).presetOptions)
47
+ return typeof profile === 'string'
48
+ && (COMPRESSION_PROFILES as readonly string[]).includes(profile)
49
+ && isCustomCompressionPolicy(custom)
50
+ && ptcCustom !== undefined
51
+ && autoCompact !== undefined
52
+ && codeSkeleton !== undefined
53
+ && intentSummary !== undefined
54
+ && monitorPanel !== undefined
55
+ && sediment !== undefined
56
+ ? {
57
+ profile: profile as CompressionProfile,
58
+ custom: canonicalizeCustomPolicy(custom),
59
+ ptcCustom,
60
+ autoCompact,
61
+ codeSkeleton,
62
+ intentSummary,
63
+ ...monitorPanel === undefined ? {} : { monitorPanel },
64
+ sediment,
65
+ ...presetOptions === undefined ? {} : { presetOptions },
66
+ }
67
+ : undefined
68
+ }
@@ -0,0 +1,71 @@
1
+ /**
2
+ * 视口比例位置持久化布局模块(百分位制,纯函数、node 环境可测、无 DOM 依赖)。
3
+ *
4
+ * 与 dsh-context-compression-improved monitor-fab 布局模型同构,改动须双侧同步
5
+ * (同串纪律:本文件与其 packages/selector/src/client/fab-pos.ts 逐字节一致,
6
+ * 测试用例同集对齐)。
7
+ *
8
+ * 存储模型:RatioPos { fx, fy } —— 悬浮物左上角相对视口的归一化位置(∈[0,1],
9
+ * 4 位小数)。兼容读入旧像素存档 { x, y }(按当前视口换算,形状判别即免迁移)。
10
+ * 换算后越界由接线层既有 clamp 兜底;本模块只做比例语义,不做落位。
11
+ */
12
+
13
+ export interface RatioPos {
14
+ fx: number;
15
+ fy: number;
16
+ }
17
+
18
+ export interface PixelPos {
19
+ x: number;
20
+ y: number;
21
+ }
22
+
23
+ /** vw/vh = 当前视口 CSS 像素;w/h = 悬浮物占位尺寸(本模块不参与运算,供接线层复用形状)。 */
24
+ export interface Viewport {
25
+ vw: number;
26
+ vh: number;
27
+ w: number;
28
+ h: number;
29
+ }
30
+
31
+ const clamp01 = (v: number): number => Math.max(0, Math.min(1, v));
32
+
33
+ const isFin = (v: unknown): v is number => typeof v === 'number' && Number.isFinite(v);
34
+
35
+ /** 除法结果的归一化兜底:±Infinity 夹到 1/0,NaN 视为无效。 */
36
+ const divClamp = (px: number, vpSize: number): number | null => {
37
+ const q = px / vpSize;
38
+ if (q === Infinity) return 1;
39
+ if (q === -Infinity) return 0;
40
+ if (!Number.isFinite(q)) return null;
41
+ return clamp01(q);
42
+ };
43
+
44
+ /**
45
+ * 读入 localStorage 存量形状并归一为比例态:
46
+ * - {fx,fy} 均有限 → 夹回 [0,1] 直用;
47
+ * - 否则 {x,y} 均有限(旧像素格式)→ 按当前视口除法换算(±Infinity 夹 1/0,NaN 拒);
48
+ * - 其余(非对象/数组/字段缺失/脏值)→ null,由调用方走默认位。
49
+ */
50
+ export function normalizeStored(raw: unknown, vp: Viewport): RatioPos | null {
51
+ if (raw === null || typeof raw !== 'object' || Array.isArray(raw)) return null;
52
+ const r = raw as Record<string, unknown>;
53
+ if (isFin(r.fx) && isFin(r.fy)) return { fx: clamp01(r.fx), fy: clamp01(r.fy) };
54
+ if (!isFin(r.x) || !isFin(r.y)) return null;
55
+ const fx = divClamp(r.x, vp.vw);
56
+ const fy = divClamp(r.y, vp.vh);
57
+ if (fx === null || fy === null) return null;
58
+ return { fx, fy };
59
+ }
60
+
61
+ /** 像素 → 比例,4 位小数舍入;像素本身可为 clamp 后越内值(先夹再存)。 */
62
+ export function toRatio(pos: PixelPos, vp: Viewport): RatioPos {
63
+ const fx = divClamp(pos.x, vp.vw);
64
+ const fy = divClamp(pos.y, vp.vh);
65
+ return { fx: fx === null ? 0 : fx, fy: fy === null ? 0 : fy };
66
+ }
67
+
68
+ /** 比例 → 像素(不做 clamp——落位后由接线层既有 clamp 收边)。 */
69
+ export function fromRatio(r: RatioPos, vp: Viewport): PixelPos {
70
+ return { x: Math.round(clamp01(r.fx) * vp.vw), y: Math.round(clamp01(r.fy) * vp.vh) };
71
+ }