dsh-context-compression-improved 0.12.0 → 0.13.1
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 +47 -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 +582 -0
- package/CHANGELOG.pt.md +167 -0
- package/CHANGELOG.ru.md +554 -0
- package/CHANGELOG.zh.md +247 -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 +128 -22
- package/README.pt.md +96 -0
- package/README.ru.md +96 -0
- package/README.zh.md +114 -24
- 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/assets/dshfind-card.svg +33 -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/docs/shallow-teaching-preset-template.md +46 -0
- package/eslint.config.js +30 -0
- package/package.json +112 -127
- 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} +1 -1
- package/{lib → packages/selector/lib}/client.js +18 -10
- package/{lib → packages/selector/lib}/profiles.d.ts +6 -0
- package/packages/selector/lib/style.css +350 -0
- package/packages/selector/package.json +180 -0
- package/packages/selector/screenshots.json +3 -0
- package/packages/selector/src/client/CompressionProfileControls.tsx +352 -0
- package/packages/selector/src/client/CompressionProfileSelector.module.css +199 -0
- package/packages/selector/src/client/CompressionProfileSelector.tsx +89 -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 +68 -0
- package/packages/selector/src/client/fab-pos.ts +71 -0
- package/packages/selector/src/client/guide-steps.ts +50 -0
- package/packages/selector/src/client/guide.tsx +368 -0
- package/packages/selector/src/client/index.ts +456 -0
- package/packages/selector/src/client/locales/de.ts +167 -0
- package/packages/selector/src/client/locales/es.ts +167 -0
- package/packages/selector/src/client/locales/fr.ts +167 -0
- package/packages/selector/src/client/locales/it.ts +167 -0
- package/packages/selector/src/client/locales/ja.ts +167 -0
- package/packages/selector/src/client/locales/ko.ts +167 -0
- package/packages/selector/src/client/locales/pt.ts +167 -0
- package/packages/selector/src/client/locales/ru.ts +167 -0
- package/packages/selector/src/client/locales.ts +334 -0
- package/packages/selector/src/client/monitor-fab.ts +445 -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 +171 -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 +909 -0
- package/packages/selector/src/invariant.ts +113 -0
- package/packages/selector/src/preset-overlay.ts +749 -0
- package/packages/selector/src/profiles.ts +560 -0
- package/packages/selector/src/pruner/content.ts +199 -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 +3222 -0
- package/packages/selector/src/runtime/adaptive-cost.ts +205 -0
- package/packages/selector/src/runtime/audit.ts +321 -0
- package/packages/selector/src/runtime/config.ts +887 -0
- package/packages/selector/src/runtime/custom-policy.ts +300 -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/fold-router.ts +143 -0
- package/packages/selector/src/runtime/injected-kinds.ts +62 -0
- package/packages/selector/src/runtime/measurement.ts +444 -0
- package/packages/selector/src/runtime/monitor.ts +341 -0
- package/packages/selector/src/runtime/netgain.ts +48 -0
- package/packages/selector/src/runtime/panel-command.ts +213 -0
- package/packages/selector/src/runtime/reducers.ts +1755 -0
- package/packages/selector/src/runtime/retrieve.ts +505 -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/sediment-card.ts +126 -0
- package/packages/selector/src/runtime/sediment-clock.ts +87 -0
- package/packages/selector/src/runtime/sediment-guard.ts +136 -0
- package/packages/selector/src/runtime/sediment-publish.ts +144 -0
- package/packages/selector/src/runtime/sediment-span.ts +170 -0
- package/packages/selector/src/runtime/sediment.ts +209 -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/shallow-tools.ts +342 -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 +177 -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 +82 -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 +129 -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/tool-names.ts +42 -0
- package/packages/selector/src/runtime/toolclass.ts +103 -0
- package/packages/selector/src/runtime/triage/masks.ts +128 -0
- package/packages/selector/src/runtime/triage/shallow.ts +132 -0
- package/packages/selector/src/runtime/triage/signals.ts +167 -0
- package/packages/selector/src/runtime/triage/types.ts +125 -0
- package/packages/selector/src/runtime/types.ts +396 -0
- package/packages/selector/src/runtime/value.ts +48 -0
- package/packages/selector/src/settings-bridge.ts +175 -0
- package/packages/selector/src/tui-command.ts +99 -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/fab-pos.client.spec.ts +47 -0
- package/packages/selector/tests/fixtures/sediment-pool/52adc205-cloud-5.orig.txt +231 -0
- package/packages/selector/tests/fixtures/sediment-pool/95fcba5d-cloud-25.orig.txt +236 -0
- package/packages/selector/tests/fixtures/sediment-pool/95fcba5d-cloud-38.orig.txt +220 -0
- package/packages/selector/tests/fixtures/sediment-pool/95fcba5d-cloud-42.orig.txt +140 -0
- package/packages/selector/tests/fixtures/sediment-pool/95fcba5d-cloud-53.orig.txt +117 -0
- package/packages/selector/tests/fixtures/sediment-pool/95fcba5d-local-10.orig.txt +67 -0
- package/packages/selector/tests/fixtures/sediment-pool/a7a767c0-cloud-45.orig.txt +119 -0
- package/packages/selector/tests/fixtures/sediment-pool/a7a767c0-cloud-49.orig.txt +119 -0
- package/packages/selector/tests/fixtures/sediment-pool/a7a767c0-cloud-63.orig.txt +131 -0
- package/packages/selector/tests/fixtures/sediment-pool/a7a767c0-cloud-75.orig.txt +196 -0
- package/packages/selector/tests/fixtures/sediment-pool/a7a767c0-cloud-76.orig.txt +71 -0
- package/packages/selector/tests/fixtures/sediment-pool/a7a767c0-cloud-85.orig.txt +126 -0
- package/packages/selector/tests/fixtures/sediment-pool/a7a767c0-cloud-86.orig.txt +86 -0
- package/packages/selector/tests/fixtures/sediment-pool/a7a767c0-cloud-87.orig.txt +50 -0
- package/packages/selector/tests/fixtures/sediment-pool/a7a767c0-cloud-96.orig.txt +102 -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 +240 -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/ptc-custom-write.client.spec.ts +86 -0
- package/packages/selector/tests/public/package-contract.client.spec.ts +91 -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/behavior-lock-profiles.spec.ts +128 -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/fallback-diversity.spec.ts +75 -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/fold-router.spec.ts +140 -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/injected-kinds.spec.ts +86 -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/netgain.spec.ts +61 -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/ptc-profile.spec.ts +133 -0
- package/packages/selector/tests/runtime/public/public-runtime.spec.ts +2795 -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/sediment-card.spec.ts +111 -0
- package/packages/selector/tests/runtime/sediment-config.spec.ts +56 -0
- package/packages/selector/tests/runtime/sediment-guard.spec.ts +71 -0
- package/packages/selector/tests/runtime/sediment-masks.spec.ts +95 -0
- package/packages/selector/tests/runtime/sediment-orchestrator.spec.ts +166 -0
- package/packages/selector/tests/runtime/sediment-pool-share.spec.ts +65 -0
- package/packages/selector/tests/runtime/sediment-publish.spec.ts +142 -0
- package/packages/selector/tests/runtime/sediment-real-shapes.spec.ts +158 -0
- package/packages/selector/tests/runtime/sediment-settings.spec.ts +73 -0
- package/packages/selector/tests/runtime/sediment-signals.spec.ts +124 -0
- package/packages/selector/tests/runtime/sediment-span.spec.ts +92 -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/shallow-primitives.spec.ts +135 -0
- package/packages/selector/tests/runtime/sidechannel.spec.ts +241 -0
- package/packages/selector/tests/runtime/source-kind-audit.spec.ts +79 -0
- package/packages/selector/tests/runtime/stats-backfill-scheduler.spec.ts +167 -0
- package/packages/selector/tests/runtime/stats-backfill.spec.ts +350 -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 +121 -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 +729 -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.d.mts +3 -0
- package/scripts/hosts.mjs +81 -0
- package/scripts/packed-components-smoke.mjs +763 -0
- package/scripts/packed-install-e2e.mjs +1346 -0
- package/scripts/sediment-latency-report.mjs +63 -0
- package/scripts/sync-hosts.mjs +176 -0
- package/scripts/toolclass-corpus-replay.mjs +281 -0
- package/scripts/verify-release.mjs +342 -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}/pruner.d.ts +0 -0
- /package/{lib → packages/selector/lib}/pruner.js +0 -0
- /package/{lib → packages/selector/lib}/savings-integral.js +0 -0
- /package/{lib → packages/selector/lib}/tail-trim.js +0 -0
- /package/{lib → packages/selector/lib}/tool-result.js +0 -0
- /package/{locale → packages/selector/locale}/de.json +0 -0
- /package/{locale → packages/selector/locale}/en.json +0 -0
- /package/{locale → packages/selector/locale}/es.json +0 -0
- /package/{locale → packages/selector/locale}/fr.json +0 -0
- /package/{locale → packages/selector/locale}/it.json +0 -0
- /package/{locale → packages/selector/locale}/ja.json +0 -0
- /package/{locale → packages/selector/locale}/ko.json +0 -0
- /package/{locale → packages/selector/locale}/pt.json +0 -0
- /package/{locale → packages/selector/locale}/ru.json +0 -0
- /package/{locale → packages/selector/locale}/zh.json +0 -0
|
@@ -0,0 +1,444 @@
|
|
|
1
|
+
/** Public-API-only measurement adapter for the standalone runtime. */
|
|
2
|
+
|
|
3
|
+
import type { Context } from '@deepseek-ai/cordis'
|
|
4
|
+
import type { ContentBlock, TokenUsage } from '@deepseek-ai/dsh-llm'
|
|
5
|
+
// ≤0.1.1 宿主 dsh-session 无 deriveEventMessage 运行时导出(SessionSeq 同因,
|
|
6
|
+
// 见 pruner.ts 顶部注):namespace 软取,缺席落恒 null——调用方 L152 已有
|
|
7
|
+
// message===null → unavailableTokenCount 的降级路径,测量退化为 unavailable
|
|
8
|
+
// 而不是炸 loader entry。
|
|
9
|
+
import * as dshSessionNamespace from '@deepseek-ai/dsh-session'
|
|
10
|
+
const dshSessionSoft = dshSessionNamespace as unknown as {
|
|
11
|
+
deriveEventMessage?: (event: never) => unknown
|
|
12
|
+
default?: { deriveEventMessage?: (event: never) => unknown }
|
|
13
|
+
}
|
|
14
|
+
const deriveEventMessageImpl =
|
|
15
|
+
dshSessionSoft.deriveEventMessage ??
|
|
16
|
+
dshSessionSoft.default?.deriveEventMessage ??
|
|
17
|
+
(() => null)
|
|
18
|
+
const deriveEventMessage = deriveEventMessageImpl as typeof import('@deepseek-ai/dsh-session').deriveEventMessage
|
|
19
|
+
import { sessionEvents } from './session-events.ts'
|
|
20
|
+
import type { Session } from '@deepseek-ai/dsh-session'
|
|
21
|
+
import type {} from '@deepseek-ai/dsh-token-meter'
|
|
22
|
+
import type { TokenMeasurement } from '@deepseek-ai/dsh-token-meter'
|
|
23
|
+
import {
|
|
24
|
+
DEEPSEEK_VISION_TOKENIZER_ARTIFACT,
|
|
25
|
+
deepSeekV4TokenizerForModel,
|
|
26
|
+
} from '../deepseek-v4-tokenizer.ts'
|
|
27
|
+
import {
|
|
28
|
+
DEEPSEEK_VISION_IMAGE_ESTIMATOR,
|
|
29
|
+
estimateDeepSeekVisionImageTokens,
|
|
30
|
+
} from './deepseek-v4-vision-tokens.ts'
|
|
31
|
+
import { unavailableTokenCount } from './token-count.ts'
|
|
32
|
+
import { pressureCost } from '../pruner/content.ts'
|
|
33
|
+
import type {
|
|
34
|
+
CanonicalTextTokenCounter,
|
|
35
|
+
ExactTokenizerTokenCount,
|
|
36
|
+
TokenCount,
|
|
37
|
+
TokenizerEstimateTokenCount,
|
|
38
|
+
} from './token-count.ts'
|
|
39
|
+
|
|
40
|
+
export type { TokenCount } from './token-count.ts'
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Character pressure of one model-visible content array, measured on the same
|
|
44
|
+
* level `SnapshotCandidate.characterPressure` uses: a tool/result message is
|
|
45
|
+
* measured by its inner tool-result content, never by the wrapper block, so the
|
|
46
|
+
* node-level and candidate-level figures stay directly comparable.
|
|
47
|
+
* @param content - the node's model-visible content blocks.
|
|
48
|
+
* @returns character pressure in Unicode code points, plus rich-block costs.
|
|
49
|
+
*/
|
|
50
|
+
function nodeCharacterPressure(content: readonly ContentBlock[]): number {
|
|
51
|
+
// 0.1.7-rc.2: tool results are tool-role messages, so a node's content
|
|
52
|
+
// array can no longer wrap them — the blocks are measured as-is.
|
|
53
|
+
return pressureCost(content)
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** Request identity retained only when every dimension is publicly known. */
|
|
57
|
+
export interface ProviderMeasurementKey {
|
|
58
|
+
readonly provider: string
|
|
59
|
+
readonly baseUrlClass: string
|
|
60
|
+
readonly apiRoute: string
|
|
61
|
+
readonly modelId: string
|
|
62
|
+
readonly requestTemplateRevision: string
|
|
63
|
+
readonly tokenizerRevision: string
|
|
64
|
+
readonly modality: string
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** Rich request observation used by Adaptive when a future public API supplies it. */
|
|
68
|
+
export interface ObservedPromptUsage {
|
|
69
|
+
readonly attemptId: string
|
|
70
|
+
readonly providerRequestOrdinal: number
|
|
71
|
+
readonly startedAtMs: number
|
|
72
|
+
readonly completedAtMs: number
|
|
73
|
+
readonly measurement: TokenCount
|
|
74
|
+
readonly observedPromptTokens: number
|
|
75
|
+
readonly observedOutputTokens?: number
|
|
76
|
+
readonly responseModelId?: string
|
|
77
|
+
readonly cacheStatus?: 'complete' | 'unknown'
|
|
78
|
+
readonly cacheReadTokens?: number
|
|
79
|
+
readonly cacheMissTokens?: number
|
|
80
|
+
readonly key?: ProviderMeasurementKey
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** Metadata-only image dimensions; the pixel payload is never read or logged. */
|
|
84
|
+
export interface CanonicalImageAttachment {
|
|
85
|
+
readonly width: number
|
|
86
|
+
readonly height: number
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Intrinsic-grid diagnostic attached to nodes whose count estimates images.
|
|
91
|
+
* It reports ONLY the official block
|
|
92
|
+
* arithmetic evaluated on the attachment's intrinsic dimensions at the two
|
|
93
|
+
* alignment-padding extremes (compress-pad 0 and 3). It is NOT a request-token
|
|
94
|
+
* bound: the adapter may still re-project the image (per-route pixel-budget
|
|
95
|
+
* or image-detail overrides, byte-cap reprojection), which can move the real
|
|
96
|
+
* count below the diagnostic minimum. It never participates in exact gates,
|
|
97
|
+
* rewrite proofs, or any lossy decision.
|
|
98
|
+
*/
|
|
99
|
+
export interface IntrinsicImageBlockDiagnostic {
|
|
100
|
+
readonly paddingMinimumTokens: number
|
|
101
|
+
readonly paddingMaximumTokens: number
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** One same-revision surface node with exact, estimated, or unavailable count. */
|
|
105
|
+
export interface MeasuredTokenSurfaceNode {
|
|
106
|
+
readonly seq: number
|
|
107
|
+
readonly count: TokenCount
|
|
108
|
+
/**
|
|
109
|
+
* Authoritative decision metric for this node: character pressure in
|
|
110
|
+
* Unicode code points (same convention as SnapshotCandidate.characterPressure).
|
|
111
|
+
*/
|
|
112
|
+
readonly characterPressure: number
|
|
113
|
+
/** Intrinsic-grid diagnostic when usable image dimensions were available. */
|
|
114
|
+
readonly intrinsicImageBlockEstimate?: IntrinsicImageBlockDiagnostic
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/** Compression view derived only from published Session and TokenMeter methods. */
|
|
118
|
+
export interface CompactionTokenView extends TokenMeasurement {
|
|
119
|
+
readonly providerRoute?: string
|
|
120
|
+
readonly modelId?: string
|
|
121
|
+
readonly measuredNodes: readonly MeasuredTokenSurfaceNode[]
|
|
122
|
+
readonly currentSurface: TokenCount
|
|
123
|
+
/**
|
|
124
|
+
* Exact character analogue of `currentSurface`: sum of per-node
|
|
125
|
+
* `characterPressure` over the same nodes `currentSurface` covers.
|
|
126
|
+
*/
|
|
127
|
+
readonly currentSurfaceChars: number
|
|
128
|
+
/** Sum of per-node intrinsic padding minima; a diagnostic, not a token bound. */
|
|
129
|
+
readonly intrinsicImageBlockEstimateTokens: number
|
|
130
|
+
readonly latestEnvelopeKey?: ProviderMeasurementKey
|
|
131
|
+
readonly lastCompletedUsage?: ObservedPromptUsage
|
|
132
|
+
countCanonicalText(text: string): TokenCount
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/** Text plus image gating for one durable request target. */
|
|
136
|
+
interface CanonicalCounter {
|
|
137
|
+
readonly countText: CanonicalTextTokenCounter
|
|
138
|
+
readonly countImage: (attachment: CanonicalImageAttachment) => TokenCount
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
const VISION_MODEL_ID = DEEPSEEK_VISION_TOKENIZER_ARTIFACT.modelIds[0] as string
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Capture one route-bound view without calling patched Harness methods.
|
|
145
|
+
* Official `measure()` remains authoritative for request pressure; the bundled
|
|
146
|
+
* tokenizer supplies exact canonical content counts used by safe rewrites.
|
|
147
|
+
*/
|
|
148
|
+
export function measureForCompaction(ctx: Context, session: Session): CompactionTokenView {
|
|
149
|
+
const header = session.requestHeader()
|
|
150
|
+
const measurement = ctx.tokenMeter.measure(session, header)
|
|
151
|
+
const target = header?.config
|
|
152
|
+
const counter = bindCounter(target?.provider, target?.model)
|
|
153
|
+
const events = sessionEvents(session)
|
|
154
|
+
// 0.1.5 surface nodes are positional and may replace earlier ranges, so
|
|
155
|
+
// their seq values are not guaranteed to equal the snapshot array index:
|
|
156
|
+
// resolve events by seq, never by position.
|
|
157
|
+
const eventsBySeq = new Map(events.map(event => [Number(event.seq), event]))
|
|
158
|
+
const measuredNodes = measurement.nodes.map((node): MeasuredTokenSurfaceNode => {
|
|
159
|
+
const event = eventsBySeq.get(Number(node.seq))
|
|
160
|
+
if (event === undefined) {
|
|
161
|
+
// The node's content is not model-visible, so it carries no character
|
|
162
|
+
// pressure on the decision surface.
|
|
163
|
+
return { seq: node.seq, count: unavailableTokenCount(`surface node ${String(node.seq)} is missing`), characterPressure: 0 }
|
|
164
|
+
}
|
|
165
|
+
const message = deriveEventMessage(event)
|
|
166
|
+
if (message === null) {
|
|
167
|
+
return { seq: node.seq, count: unavailableTokenCount(`surface node ${String(node.seq)} is not model-visible`), characterPressure: 0 }
|
|
168
|
+
}
|
|
169
|
+
const count = countCanonicalContent(message.content, counter, `surface node ${String(node.seq)}`)
|
|
170
|
+
const intrinsicImageBlockEstimate = count.kind === 'tokenizer-estimate'
|
|
171
|
+
? intrinsicImageDiagnostic(message.content, target)
|
|
172
|
+
: undefined
|
|
173
|
+
return {
|
|
174
|
+
seq: node.seq,
|
|
175
|
+
count,
|
|
176
|
+
characterPressure: nodeCharacterPressure(message.content),
|
|
177
|
+
...intrinsicImageBlockEstimate === undefined ? {} : { intrinsicImageBlockEstimate },
|
|
178
|
+
}
|
|
179
|
+
})
|
|
180
|
+
const currentSurface = countSurfaceCounts(
|
|
181
|
+
measuredNodes.map(node => node.count),
|
|
182
|
+
'current surface',
|
|
183
|
+
)
|
|
184
|
+
const currentSurfaceChars = measuredNodes.reduce((sum, node) => sum + node.characterPressure, 0)
|
|
185
|
+
const intrinsicImageBlockEstimateTokens = measuredNodes.reduce(
|
|
186
|
+
(sum, node) => sum + (node.intrinsicImageBlockEstimate?.paddingMinimumTokens ?? 0),
|
|
187
|
+
0,
|
|
188
|
+
)
|
|
189
|
+
return Object.freeze({
|
|
190
|
+
...measurement,
|
|
191
|
+
...(target === undefined ? {} : { providerRoute: target.provider, modelId: target.model }),
|
|
192
|
+
measuredNodes: Object.freeze(measuredNodes),
|
|
193
|
+
currentSurface,
|
|
194
|
+
currentSurfaceChars,
|
|
195
|
+
intrinsicImageBlockEstimateTokens,
|
|
196
|
+
countCanonicalText: counter.countText,
|
|
197
|
+
})
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/** Request-level usage exposed by official TokenMeter, without invented route attribution. */
|
|
201
|
+
export function officialRequestUsage(view: CompactionTokenView): Readonly<TokenUsage> | undefined {
|
|
202
|
+
return view.baseline.kind === 'usage' ? view.baseline.usage : undefined
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* Count one canonical content walk in canonical field order.
|
|
207
|
+
*
|
|
208
|
+
* Text, reasoning, tool-call names/arguments, and nested text tool results are
|
|
209
|
+
* counted exactly with one tokenizer identity. Image blocks produce a bounded
|
|
210
|
+
* estimate because the absolute prompt position and the adapter's final
|
|
211
|
+
* projection are not publicly observable. A mixed text/image node is therefore
|
|
212
|
+
* an estimate and never qualifies for an exact rewrite proof.
|
|
213
|
+
*/
|
|
214
|
+
function countCanonicalContent(
|
|
215
|
+
blocks: readonly ContentBlock[],
|
|
216
|
+
counter: CanonicalCounter,
|
|
217
|
+
subject: string,
|
|
218
|
+
): TokenCount {
|
|
219
|
+
let identity: ExactTokenizerTokenCount | undefined
|
|
220
|
+
let estimateIdentity: Pick<TokenizerEstimateTokenCount, 'estimatorId' | 'estimatorRevision'> | undefined
|
|
221
|
+
let tokens = 0
|
|
222
|
+
let upperBoundTokens = 0
|
|
223
|
+
let firstRefusal: TokenCount | undefined
|
|
224
|
+
const absorb = (count: TokenCount): boolean => {
|
|
225
|
+
if (count.kind === 'unavailable') {
|
|
226
|
+
firstRefusal ??= count
|
|
227
|
+
return false
|
|
228
|
+
}
|
|
229
|
+
if (count.kind === 'exact-tokenizer') {
|
|
230
|
+
if (identity !== undefined
|
|
231
|
+
&& (identity.tokenizerId !== count.tokenizerId
|
|
232
|
+
|| identity.tokenizerRevision !== count.tokenizerRevision)) {
|
|
233
|
+
firstRefusal ??= unavailableTokenCount(`${subject}: tokenizer identity changed within one measurement`)
|
|
234
|
+
return false
|
|
235
|
+
}
|
|
236
|
+
identity ??= count
|
|
237
|
+
tokens += count.tokens
|
|
238
|
+
upperBoundTokens += count.tokens
|
|
239
|
+
} else {
|
|
240
|
+
if (estimateIdentity !== undefined
|
|
241
|
+
&& (estimateIdentity.estimatorId !== count.estimatorId
|
|
242
|
+
|| estimateIdentity.estimatorRevision !== count.estimatorRevision)) {
|
|
243
|
+
firstRefusal ??= unavailableTokenCount(`${subject}: image estimator identity changed within one measurement`)
|
|
244
|
+
return false
|
|
245
|
+
}
|
|
246
|
+
estimateIdentity ??= {
|
|
247
|
+
estimatorId: count.estimatorId,
|
|
248
|
+
estimatorRevision: count.estimatorRevision,
|
|
249
|
+
}
|
|
250
|
+
tokens += count.tokens
|
|
251
|
+
upperBoundTokens += count.upperBoundTokens
|
|
252
|
+
}
|
|
253
|
+
return Number.isSafeInteger(tokens) && tokens >= 0
|
|
254
|
+
&& Number.isSafeInteger(upperBoundTokens) && upperBoundTokens >= tokens
|
|
255
|
+
}
|
|
256
|
+
const walk = (content: readonly ContentBlock[]): boolean => {
|
|
257
|
+
for (const block of content) {
|
|
258
|
+
switch (block.type) {
|
|
259
|
+
case 'text':
|
|
260
|
+
case 'reasoning': {
|
|
261
|
+
if (!absorb(counter.countText(block.text))) return false
|
|
262
|
+
break
|
|
263
|
+
}
|
|
264
|
+
case 'tool-call': {
|
|
265
|
+
if (!absorb(counter.countText(block.name))) return false
|
|
266
|
+
if (!absorb(counter.countText(block.arguments))) return false
|
|
267
|
+
break
|
|
268
|
+
}
|
|
269
|
+
case 'image': {
|
|
270
|
+
if (!absorb(counter.countImage(block.attachment))) return false
|
|
271
|
+
break
|
|
272
|
+
}
|
|
273
|
+
default: {
|
|
274
|
+
firstRefusal ??= unavailableTokenCount(`${subject}: contains an unsupported content block`)
|
|
275
|
+
return false
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
return true
|
|
280
|
+
}
|
|
281
|
+
if (!walk(blocks)) {
|
|
282
|
+
if (firstRefusal?.kind === 'unavailable') {
|
|
283
|
+
return unavailableTokenCount(`${subject}: ${firstRefusal.reason}`)
|
|
284
|
+
}
|
|
285
|
+
return unavailableTokenCount(`${subject}: contains content the canonical counter cannot count exactly`)
|
|
286
|
+
}
|
|
287
|
+
if (identity === undefined && estimateIdentity === undefined) {
|
|
288
|
+
// Empty content is a legal durable shape; it keeps the exact-0 count with
|
|
289
|
+
// the bound tokenizer identity instead of fail-opening the whole surface.
|
|
290
|
+
const empty = counter.countText('')
|
|
291
|
+
if (empty.kind !== 'exact-tokenizer') return empty
|
|
292
|
+
return empty
|
|
293
|
+
}
|
|
294
|
+
if (!Number.isSafeInteger(tokens) || tokens < 0) {
|
|
295
|
+
return unavailableTokenCount(`${subject}: invalid token sum`)
|
|
296
|
+
}
|
|
297
|
+
if (estimateIdentity !== undefined) {
|
|
298
|
+
return Object.freeze({
|
|
299
|
+
kind: 'tokenizer-estimate',
|
|
300
|
+
...estimateIdentity,
|
|
301
|
+
tokens,
|
|
302
|
+
upperBoundTokens,
|
|
303
|
+
})
|
|
304
|
+
}
|
|
305
|
+
if (identity === undefined) return unavailableTokenCount(`${subject}: no tokenizer identity`)
|
|
306
|
+
return Object.freeze({ ...identity, tokens })
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
function bindCounter(provider: string | undefined, model: string | undefined): CanonicalCounter {
|
|
310
|
+
if (provider === undefined || model === undefined) {
|
|
311
|
+
const unavailable = () => unavailableTokenCount('canonical text: no durable provider/model request header')
|
|
312
|
+
return { countText: unavailable, countImage: () => unavailableTokenCount('canonical image: no durable provider/model request header') }
|
|
313
|
+
}
|
|
314
|
+
if (provider !== 'deepseek' && provider !== 'deepseek-official') {
|
|
315
|
+
const reason = `canonical text: provider "${provider}" is not the supported DeepSeek route`
|
|
316
|
+
return { countText: () => unavailableTokenCount(reason), countImage: () => unavailableTokenCount(`canonical image: provider "${provider}" is not the supported DeepSeek route`) }
|
|
317
|
+
}
|
|
318
|
+
const tokenizer = deepSeekV4TokenizerForModel(model)
|
|
319
|
+
if (tokenizer === undefined) {
|
|
320
|
+
const reason = `canonical text: no verified bundled tokenizer for model "${model}"`
|
|
321
|
+
return {
|
|
322
|
+
countText: () => unavailableTokenCount(reason),
|
|
323
|
+
// Image arithmetic is independently pinned and does not need the text
|
|
324
|
+
// tokenizer asset. A pure-image node can therefore retain its estimate
|
|
325
|
+
// even when text measurement fails closed.
|
|
326
|
+
countImage: attachment => countCanonicalImage(model, attachment),
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
return {
|
|
330
|
+
countText: (text: string) => tokenizer.countText(text),
|
|
331
|
+
countImage: attachment => countCanonicalImage(model, attachment),
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
/**
|
|
336
|
+
* Images never claim an exact count. The official expansion depends on the
|
|
337
|
+
* absolute prompt position (system prompt, chat-template framing, adapter
|
|
338
|
+
* image handles) and on the adapter's final request-image projection, neither
|
|
339
|
+
* of which is exposed through a public API; a route may even override the
|
|
340
|
+
* pixel budget or re-project under the byte cap. Valid dimensions therefore
|
|
341
|
+
* use the midpoint of the four alignment residues as a bounded estimate;
|
|
342
|
+
* malformed dimensions use a fixed default. Estimate-bearing nodes remain
|
|
343
|
+
* ineligible for exact rewrite proofs.
|
|
344
|
+
*/
|
|
345
|
+
function countCanonicalImage(
|
|
346
|
+
model: string,
|
|
347
|
+
attachment: CanonicalImageAttachment,
|
|
348
|
+
): TokenCount {
|
|
349
|
+
if (model !== VISION_MODEL_ID) {
|
|
350
|
+
return unavailableTokenCount(`canonical image: model "${model}" has no vision image counter`)
|
|
351
|
+
}
|
|
352
|
+
const estimate = estimateDeepSeekVisionImageTokens(attachment.width, attachment.height)
|
|
353
|
+
return Object.freeze({
|
|
354
|
+
kind: 'tokenizer-estimate',
|
|
355
|
+
tokens: estimate.tokens,
|
|
356
|
+
upperBoundTokens: estimate.upperBoundTokens,
|
|
357
|
+
estimatorId: DEEPSEEK_VISION_IMAGE_ESTIMATOR.id,
|
|
358
|
+
estimatorRevision: DEEPSEEK_VISION_IMAGE_ESTIMATOR.revision,
|
|
359
|
+
})
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
/**
|
|
363
|
+
* Intrinsic-grid diagnostic for one content walk: the official block
|
|
364
|
+
* arithmetic on intrinsic dimensions at both alignment extremes. Only images
|
|
365
|
+
* on the pinned DeepSeek vision route with usable metadata contribute.
|
|
366
|
+
*/
|
|
367
|
+
function intrinsicImageDiagnostic(
|
|
368
|
+
blocks: readonly ContentBlock[],
|
|
369
|
+
target: { readonly provider: string, readonly model: string } | undefined,
|
|
370
|
+
): IntrinsicImageBlockDiagnostic | undefined {
|
|
371
|
+
// Mirror the gating route: only the DeepSeek vision route carries the
|
|
372
|
+
// official-arithmetic bounds.
|
|
373
|
+
if (target === undefined
|
|
374
|
+
|| (target.provider !== 'deepseek' && target.provider !== 'deepseek-official')
|
|
375
|
+
|| target.model !== VISION_MODEL_ID) return undefined
|
|
376
|
+
let paddingMinimumTokens = 0
|
|
377
|
+
let paddingMaximumTokens = 0
|
|
378
|
+
let seen = false
|
|
379
|
+
const walk = (content: readonly ContentBlock[]): void => {
|
|
380
|
+
for (const block of content) {
|
|
381
|
+
if (block.type === 'image') {
|
|
382
|
+
const { width, height } = block.attachment
|
|
383
|
+
const estimate = estimateDeepSeekVisionImageTokens(width, height)
|
|
384
|
+
if (estimate.source !== 'intrinsic-grid'
|
|
385
|
+
|| estimate.paddingMinimumTokens === undefined
|
|
386
|
+
|| estimate.paddingMaximumTokens === undefined) continue
|
|
387
|
+
paddingMinimumTokens += estimate.paddingMinimumTokens
|
|
388
|
+
paddingMaximumTokens += estimate.paddingMaximumTokens
|
|
389
|
+
seen = true
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
walk(blocks)
|
|
394
|
+
return seen ? Object.freeze({ paddingMinimumTokens, paddingMaximumTokens }) : undefined
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
function countSurfaceCounts(counts: readonly TokenCount[], subject: string): TokenCount {
|
|
398
|
+
if (counts.length === 0) return unavailableTokenCount(`${subject}: no surface nodes`)
|
|
399
|
+
let identity: Extract<TokenCount, { kind: 'exact-tokenizer' }> | undefined
|
|
400
|
+
let estimateIdentity: Pick<TokenizerEstimateTokenCount, 'estimatorId' | 'estimatorRevision'> | undefined
|
|
401
|
+
let tokens = 0
|
|
402
|
+
let upperBoundTokens = 0
|
|
403
|
+
for (const count of counts) {
|
|
404
|
+
if (count.kind === 'unavailable') {
|
|
405
|
+
return unavailableTokenCount(`${subject}: ${count.reason}`)
|
|
406
|
+
}
|
|
407
|
+
if (count.kind === 'exact-tokenizer') {
|
|
408
|
+
if (identity !== undefined
|
|
409
|
+
&& (identity.tokenizerId !== count.tokenizerId
|
|
410
|
+
|| identity.tokenizerRevision !== count.tokenizerRevision)) {
|
|
411
|
+
return unavailableTokenCount(`${subject}: tokenizer identity changed within one measurement`)
|
|
412
|
+
}
|
|
413
|
+
identity ??= count
|
|
414
|
+
tokens += count.tokens
|
|
415
|
+
upperBoundTokens += count.tokens
|
|
416
|
+
} else {
|
|
417
|
+
if (estimateIdentity !== undefined
|
|
418
|
+
&& (estimateIdentity.estimatorId !== count.estimatorId
|
|
419
|
+
|| estimateIdentity.estimatorRevision !== count.estimatorRevision)) {
|
|
420
|
+
return unavailableTokenCount(`${subject}: image estimator identity changed within one measurement`)
|
|
421
|
+
}
|
|
422
|
+
estimateIdentity ??= {
|
|
423
|
+
estimatorId: count.estimatorId,
|
|
424
|
+
estimatorRevision: count.estimatorRevision,
|
|
425
|
+
}
|
|
426
|
+
tokens += count.tokens
|
|
427
|
+
upperBoundTokens += count.upperBoundTokens
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
if (!Number.isSafeInteger(tokens) || tokens < 0
|
|
431
|
+
|| !Number.isSafeInteger(upperBoundTokens) || upperBoundTokens < tokens) {
|
|
432
|
+
return unavailableTokenCount(`${subject}: invalid token sum`)
|
|
433
|
+
}
|
|
434
|
+
if (estimateIdentity !== undefined) {
|
|
435
|
+
return Object.freeze({
|
|
436
|
+
kind: 'tokenizer-estimate',
|
|
437
|
+
...estimateIdentity,
|
|
438
|
+
tokens,
|
|
439
|
+
upperBoundTokens,
|
|
440
|
+
})
|
|
441
|
+
}
|
|
442
|
+
if (identity === undefined) return unavailableTokenCount(`${subject}: no tokenizer identity`)
|
|
443
|
+
return Object.freeze({ ...identity, tokens })
|
|
444
|
+
}
|