dsh-context-compression-improved 0.9.7 → 0.9.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (126) hide show
  1. package/.gitattributes +2 -2
  2. package/.githooks/pre-push +37 -37
  3. package/CHANGELOG.md +4 -0
  4. package/CONTRIBUTING.md +22 -22
  5. package/LICENSE +21 -21
  6. package/README.de.md +3 -3
  7. package/README.es.md +3 -3
  8. package/README.fr.md +3 -3
  9. package/README.it.md +3 -3
  10. package/README.ja.md +3 -3
  11. package/README.ko.md +3 -3
  12. package/README.md +3 -3
  13. package/README.ru.md +3 -3
  14. package/README.zh.md +3 -3
  15. package/SECURITY.md +18 -18
  16. package/THIRD_PARTY_NOTICES.md +14 -14
  17. package/docs/compat-0.1.5-report.md +77 -77
  18. package/docs/installation.de.md +2 -2
  19. package/docs/installation.es.md +2 -2
  20. package/docs/installation.fr.md +2 -2
  21. package/docs/installation.it.md +2 -2
  22. package/docs/installation.ja.md +2 -2
  23. package/docs/installation.ko.md +2 -2
  24. package/docs/installation.md +2 -2
  25. package/docs/installation.ru.md +2 -2
  26. package/docs/installation.zh.md +2 -2
  27. package/docs/repair-log.md +657 -657
  28. package/package.json +6 -6
  29. package/packages/selector/LICENSE +21 -21
  30. package/packages/selector/README.md +26 -26
  31. package/packages/selector/README.zh.md +24 -24
  32. package/packages/selector/THIRD_PARTY_NOTICES.md +32 -32
  33. package/packages/selector/cordis.patch.yml +27 -27
  34. package/packages/selector/docs/history-tool-call-working-set-spec.md +112 -112
  35. package/packages/selector/docs/native-tool-result-selector-spec.md +34 -34
  36. package/packages/selector/docs/subagent-cache-reuse-spec.md +46 -46
  37. package/packages/selector/dsh.plugin.json +2 -2
  38. package/packages/selector/lib/style.css +53 -53
  39. package/packages/selector/package.json +21 -21
  40. package/packages/selector/screenshots.json +3 -3
  41. package/packages/selector/src/client/CustomPolicyEditor.tsx +216 -216
  42. package/packages/selector/src/client/EstimatorControls.tsx +277 -277
  43. package/packages/selector/src/client/preset-options.ts +88 -88
  44. package/packages/selector/src/client/scope-face.ts +24 -24
  45. package/packages/selector/src/css-modules.d.ts +6 -6
  46. package/packages/selector/src/estimator-catalog.ts +104 -104
  47. package/packages/selector/src/invariant.ts +113 -113
  48. package/packages/selector/src/pruner/tuning.ts +23 -23
  49. package/packages/selector/src/pruner/types.ts +78 -78
  50. package/packages/selector/src/runtime/adaptive-cost.ts +205 -205
  51. package/packages/selector/src/runtime/audit.ts +303 -303
  52. package/packages/selector/src/runtime/custom-policy.ts +278 -278
  53. package/packages/selector/src/runtime/deepseek-official-pricing.ts +298 -298
  54. package/packages/selector/src/runtime/deepseek-v4-vision-tokens.ts +254 -254
  55. package/packages/selector/src/runtime/session-events.ts +30 -30
  56. package/packages/selector/src/runtime/token-count.ts +72 -72
  57. package/packages/selector/src/runtime/tokenpilot/advisor-prompt.ts +233 -233
  58. package/packages/selector/src/runtime/tokenpilot/advisor-state.ts +204 -204
  59. package/packages/selector/src/runtime/tokenpilot/advisor.ts +419 -419
  60. package/packages/selector/src/runtime/tokenpilot/benefit.ts +200 -200
  61. package/packages/selector/src/runtime/tokenpilot/dedup.ts +81 -81
  62. package/packages/selector/src/runtime/tokenpilot/estimator.ts +151 -151
  63. package/packages/selector/src/runtime/tokenpilot/intent-fold.ts +53 -53
  64. package/packages/selector/src/runtime/tokenpilot/intent-gate.ts +65 -65
  65. package/packages/selector/src/runtime/tokenpilot/intent-input-mask.ts +125 -125
  66. package/packages/selector/src/runtime/tokenpilot/intent-range.ts +37 -37
  67. package/packages/selector/src/runtime/tokenpilot/read-state.ts +98 -98
  68. package/packages/selector/src/runtime/toolclass.ts +103 -103
  69. package/packages/selector/src/runtime/value.ts +48 -48
  70. package/packages/selector/tests/advisor-report.host.spec.ts +223 -223
  71. package/packages/selector/tests/cache-prefix-audit.spec.ts +123 -123
  72. package/packages/selector/tests/estimator-catalog.spec.ts +70 -70
  73. package/packages/selector/tests/helpers/legacy-namespace.ts +35 -35
  74. package/packages/selector/tests/helpers/legacy-settings/index.ts +15 -15
  75. package/packages/selector/tests/helpers/legacy-settings/legacy/provider.d.ts +41 -41
  76. package/packages/selector/tests/helpers/legacy-settings/legacy/provider.js +647 -647
  77. package/packages/selector/tests/helpers/legacy-settings/legacy/redact.js +78 -78
  78. package/packages/selector/tests/helpers/legacy-settings/legacy/util-values.js +234 -234
  79. package/packages/selector/tests/preset-overlay.host.spec.ts +246 -246
  80. package/packages/selector/tests/public/package-contract.client.spec.ts +4 -4
  81. package/packages/selector/tests/runtime/adaptive-cost.spec.ts +167 -167
  82. package/packages/selector/tests/runtime/advisor-invariant.spec.ts +272 -272
  83. package/packages/selector/tests/runtime/advisor.spec.ts +226 -226
  84. package/packages/selector/tests/runtime/char-basis.spec.ts +30 -30
  85. package/packages/selector/tests/runtime/code-skeleton.spec.ts +152 -152
  86. package/packages/selector/tests/runtime/deepseek-official-pricing.spec.ts +186 -186
  87. package/packages/selector/tests/runtime/deepseek-v4-vision-tokens.spec.ts +122 -122
  88. package/packages/selector/tests/runtime/deprecated-preset-options.spec.ts +96 -96
  89. package/packages/selector/tests/runtime/fixtures/profile-baseline.json +273 -273
  90. package/packages/selector/tests/runtime/fixtures/tokenizer-golden.json +106 -106
  91. package/packages/selector/tests/runtime/fixtures/vision-golden.json +459 -459
  92. package/packages/selector/tests/runtime/frequency-longstrings.spec.ts +74 -74
  93. package/packages/selector/tests/runtime/html-reducer.spec.ts +212 -212
  94. package/packages/selector/tests/runtime/intent-gate.spec.ts +62 -62
  95. package/packages/selector/tests/runtime/intent-summary.spec.ts +156 -156
  96. package/packages/selector/tests/runtime/line-mapping.spec.ts +153 -153
  97. package/packages/selector/tests/runtime/prose-reducers.spec.ts +133 -133
  98. package/packages/selector/tests/runtime/read-input-cap.spec.ts +33 -33
  99. package/packages/selector/tests/runtime/search-reducer.spec.ts +110 -110
  100. package/packages/selector/tests/runtime/session-events.spec.ts +27 -27
  101. package/packages/selector/tests/runtime/sidechannel.spec.ts +241 -241
  102. package/packages/selector/tests/runtime/summary-command.spec.ts +154 -154
  103. package/packages/selector/tests/runtime/toc-and-bundled.spec.ts +159 -159
  104. package/packages/selector/tests/runtime/tokenizer-golden.spec.ts +53 -53
  105. package/packages/selector/tests/runtime/tokenpilot/benefit.spec.ts +217 -217
  106. package/packages/selector/tests/runtime/tokenpilot/dedup.spec.ts +52 -52
  107. package/packages/selector/tests/runtime/tokenpilot/estimator.spec.ts +79 -79
  108. package/packages/selector/tests/runtime/tokenpilot/locator.spec.ts +76 -76
  109. package/packages/selector/tests/runtime/tokenpilot/read-state.spec.ts +82 -82
  110. package/packages/selector/tests/runtime/toolclass.spec.ts +156 -156
  111. package/packages/selector/tests/runtime/value.spec.ts +23 -23
  112. package/packages/selector/tests/subagent-cache-reuse.host.spec.ts +269 -269
  113. package/packages/selector/tests/support/cache-prefix-audit.ts +105 -105
  114. package/packages/selector/tests/support/mock-adapter.ts +37 -37
  115. package/packages/selector/tests/support/ui-primitives.tsx +34 -34
  116. package/packages/selector/tsconfig.json +11 -11
  117. package/packages/selector/tsdown.client.config.ts +102 -102
  118. package/packages/selector/tsdown.config.ts +20 -20
  119. package/scripts/capture-profile-baseline.mjs +42 -42
  120. package/scripts/generate-tokenizer-fixtures.py +81 -81
  121. package/scripts/generate-vision-fixtures.py +208 -208
  122. package/scripts/hosts.mjs +2 -1
  123. package/scripts/toolclass-corpus-replay.mjs +281 -281
  124. package/tsconfig.base.json +18 -18
  125. package/tsconfig.json +7 -7
  126. package/vitest.built.config.ts +9 -9
@@ -1,186 +1,186 @@
1
- import { describe, expect, it } from 'vitest'
2
- import {
3
- DEEPSEEK_OFFICIAL_PRICE_CATALOG_VERSION,
4
- priceBandAt,
5
- priceOfficialDeepSeekUsage,
6
- resolveOfficialDeepSeekPrice,
7
- } from '../../src/runtime/deepseek-official-pricing.ts'
8
-
9
- describe('DeepSeek checked-in official price catalog', () => {
10
- it.each([
11
- ['deepseek-v4-flash', 'USD', 'off-peak', '0.007', '0.22', '0.66'],
12
- ['deepseek-v4-flash', 'USD', 'peak', '0.014', '0.44', '1.32'],
13
- ['deepseek-v4-flash', 'CNY', 'off-peak', '0.05', '1.5', '4.5'],
14
- ['deepseek-v4-flash', 'CNY', 'peak', '0.10', '3.0', '9.0'],
15
- ['deepseek-v4-pro', 'USD', 'off-peak', '0.022', '0.66', '1.98'],
16
- ['deepseek-v4-pro', 'USD', 'peak', '0.044', '1.32', '3.96'],
17
- ['deepseek-v4-pro', 'CNY', 'off-peak', '0.15', '4.5', '13.5'],
18
- ['deepseek-v4-pro', 'CNY', 'peak', '0.30', '9.0', '27.0'],
19
- ['deepseek-v4-flash-vision-exp', 'USD', 'off-peak', '0.007', '0.22', '0.66'],
20
- ['deepseek-v4-flash-vision-exp', 'USD', 'peak', '0.014', '0.44', '1.32'],
21
- ['deepseek-v4-flash-vision-exp', 'CNY', 'off-peak', '0.05', '1.5', '4.5'],
22
- ['deepseek-v4-flash-vision-exp', 'CNY', 'peak', '0.10', '3.0', '9.0'],
23
- ] as const)(
24
- 'resolves %s %s %s from the versioned catalog',
25
- (modelId, currency, band, hit, miss, output) => {
26
- const resolved = resolveOfficialDeepSeekPrice({
27
- provider: 'deepseek-official',
28
- baseUrlClass: 'official-public',
29
- apiRoute: 'chat-completions',
30
- modelId,
31
- currency,
32
- at: band === 'peak'
33
- ? new Date('2026-08-25T01:00:00.000Z')
34
- : new Date('2026-08-23T01:00:00.000Z'),
35
- })
36
- expect(resolved).toMatchObject({
37
- kind: 'priced',
38
- record: {
39
- catalogVersion: DEEPSEEK_OFFICIAL_PRICE_CATALOG_VERSION,
40
- modelId, currency, band,
41
- inputCacheHit: hit,
42
- inputCacheMiss: miss,
43
- output,
44
- },
45
- })
46
- },
47
- )
48
-
49
- it.each(['chat-completions', 'responses'] as const)(
50
- 'prices the documented %s route without changing the catalog tuple',
51
- (apiRoute) => {
52
- expect(resolveOfficialDeepSeekPrice({
53
- provider: 'deepseek-official',
54
- baseUrlClass: 'official-public',
55
- apiRoute,
56
- modelId: 'deepseek-v4-pro',
57
- currency: 'USD',
58
- at: new Date('2026-08-25T01:00:00.000Z'),
59
- })).toMatchObject({
60
- kind: 'priced',
61
- record: { apiRoute, modelId: 'deepseek-v4-pro', band: 'peak' },
62
- })
63
- },
64
- )
65
-
66
- it.each([
67
- ['2026-08-24T00:59:59.000Z', 'off-peak'],
68
- ['2026-08-24T01:00:00.000Z', 'peak'],
69
- ['2026-08-24T03:59:59.000Z', 'peak'],
70
- ['2026-08-24T04:00:00.000Z', 'off-peak'],
71
- ['2026-08-24T05:59:59.000Z', 'off-peak'],
72
- ['2026-08-24T06:00:00.000Z', 'peak'],
73
- ['2026-08-24T09:59:59.000Z', 'peak'],
74
- ['2026-08-24T10:00:00.000Z', 'off-peak'],
75
- ['2026-08-29T01:00:00.000Z', 'off-peak'],
76
- ['2026-08-30T06:00:00.000Z', 'off-peak'],
77
- ] as const)('classifies exact UTC schedule boundary %s as %s', (iso, expected) => {
78
- expect(priceBandAt(new Date(iso))).toBe(expected)
79
- })
80
-
81
- it.each([
82
- [{ provider: 'gateway', baseUrlClass: 'official-public' }, 'provider'],
83
- [{ provider: 'deepseek-official', baseUrlClass: 'compatible-hmac:v1:test' }, 'base-url'],
84
- [{ provider: 'deepseek-official', baseUrlClass: 'official-public', modelId: 'deepseek-chat' }, 'model'],
85
- [{ provider: 'deepseek-official', baseUrlClass: 'official-public', apiRoute: 'other' }, 'route'],
86
- ] as const)('fails closed instead of aliasing an unknown applicability: %s', (patch, reason) => {
87
- const resolved = resolveOfficialDeepSeekPrice({
88
- apiRoute: 'chat-completions',
89
- modelId: 'deepseek-v4-flash',
90
- currency: 'USD',
91
- at: new Date('2026-08-25T01:00:00.000Z'),
92
- ...patch,
93
- })
94
- expect(resolved.kind).toBe('unpriced')
95
- if (resolved.kind !== 'unpriced')
96
- throw new Error(`Expected an unpriced result, received ${resolved.kind}`)
97
- expect(resolved.reason).toContain(reason)
98
- })
99
-
100
- it('computes provider-usage cost with fixed-point arithmetic, never JS float money', () => {
101
- const cost = priceOfficialDeepSeekUsage({
102
- provider: 'deepseek-official',
103
- baseUrlClass: 'official-public',
104
- apiRoute: 'chat-completions',
105
- modelId: 'deepseek-v4-flash',
106
- currency: 'USD',
107
- startedAt: new Date('2026-08-25T01:00:00.000Z'),
108
- completedAt: new Date('2026-08-25T01:01:00.000Z'),
109
- usage: { cacheReadTokens: 1, cacheMissTokens: 2, outputTokens: 3 },
110
- })
111
- expect(cost).toEqual({
112
- kind: 'exact',
113
- currency: 'USD',
114
- band: 'peak',
115
- femtoUnits: '4854000000',
116
- decimal: '0.000004854',
117
- })
118
- })
119
-
120
- it('returns a range when one request spans a published price-band boundary', () => {
121
- const cost = priceOfficialDeepSeekUsage({
122
- provider: 'deepseek-official',
123
- baseUrlClass: 'official-public',
124
- apiRoute: 'chat-completions',
125
- modelId: 'deepseek-v4-pro',
126
- currency: 'CNY',
127
- startedAt: new Date('2026-08-25T03:59:59.000Z'),
128
- completedAt: new Date('2026-08-25T04:00:01.000Z'),
129
- usage: { cacheReadTokens: 10, cacheMissTokens: 20, outputTokens: 3 },
130
- })
131
- expect(cost).toMatchObject({
132
- kind: 'range', currency: 'CNY', bands: ['peak', 'off-peak'],
133
- })
134
- })
135
-
136
- it('returns a range when both endpoints share a band but the interval crosses peak', () => {
137
- const cost = priceOfficialDeepSeekUsage({
138
- provider: 'deepseek-official',
139
- baseUrlClass: 'official-public',
140
- apiRoute: 'chat-completions',
141
- modelId: 'deepseek-v4-flash',
142
- currency: 'USD',
143
- startedAt: new Date('2026-08-25T00:59:59.000Z'),
144
- completedAt: new Date('2026-08-25T04:00:01.000Z'),
145
- usage: { cacheReadTokens: 10, cacheMissTokens: 20, outputTokens: 3 },
146
- })
147
- expect(cost).toMatchObject({
148
- kind: 'range', currency: 'USD', bands: ['off-peak', 'peak'],
149
- })
150
- })
151
-
152
- it.each([
153
- ['Friday through Monday', '2026-08-28T10:00:01.000Z', '2026-08-31T01:00:01.000Z'],
154
- ['seven full days', '2026-08-25T00:00:00.000Z', '2026-09-01T00:00:00.000Z'],
155
- ])('returns a range across %s', (_label, startedAt, completedAt) => {
156
- expect(priceOfficialDeepSeekUsage({
157
- provider: 'deepseek-official',
158
- baseUrlClass: 'official-public',
159
- apiRoute: 'responses',
160
- modelId: 'deepseek-v4-flash',
161
- currency: 'USD',
162
- startedAt: new Date(startedAt),
163
- completedAt: new Date(completedAt),
164
- usage: { cacheReadTokens: 10, cacheMissTokens: 20, outputTokens: 3 },
165
- })).toMatchObject({ kind: 'range', currency: 'USD' })
166
- })
167
-
168
- it.each([
169
- ['invalid start timestamp', new Date('invalid'), new Date('2026-08-25T01:00:00.000Z'), 'timestamp'],
170
- ['completion before start', new Date('2026-08-25T01:00:01.000Z'), new Date('2026-08-25T01:00:00.000Z'), 'precedes'],
171
- ] as const)('fails closed for %s', (_label, startedAt, completedAt, reason) => {
172
- const cost = priceOfficialDeepSeekUsage({
173
- provider: 'deepseek-official',
174
- baseUrlClass: 'official-public',
175
- apiRoute: 'chat-completions',
176
- modelId: 'deepseek-v4-flash',
177
- currency: 'USD',
178
- startedAt,
179
- completedAt,
180
- usage: { cacheReadTokens: 1, cacheMissTokens: 2, outputTokens: 3 },
181
- })
182
- expect(cost.kind).toBe('unpriced')
183
- if (cost.kind !== 'unpriced') throw new Error('expected unpriced interval')
184
- expect(cost.reason).toContain(reason)
185
- })
186
- })
1
+ import { describe, expect, it } from 'vitest'
2
+ import {
3
+ DEEPSEEK_OFFICIAL_PRICE_CATALOG_VERSION,
4
+ priceBandAt,
5
+ priceOfficialDeepSeekUsage,
6
+ resolveOfficialDeepSeekPrice,
7
+ } from '../../src/runtime/deepseek-official-pricing.ts'
8
+
9
+ describe('DeepSeek checked-in official price catalog', () => {
10
+ it.each([
11
+ ['deepseek-v4-flash', 'USD', 'off-peak', '0.007', '0.22', '0.66'],
12
+ ['deepseek-v4-flash', 'USD', 'peak', '0.014', '0.44', '1.32'],
13
+ ['deepseek-v4-flash', 'CNY', 'off-peak', '0.05', '1.5', '4.5'],
14
+ ['deepseek-v4-flash', 'CNY', 'peak', '0.10', '3.0', '9.0'],
15
+ ['deepseek-v4-pro', 'USD', 'off-peak', '0.022', '0.66', '1.98'],
16
+ ['deepseek-v4-pro', 'USD', 'peak', '0.044', '1.32', '3.96'],
17
+ ['deepseek-v4-pro', 'CNY', 'off-peak', '0.15', '4.5', '13.5'],
18
+ ['deepseek-v4-pro', 'CNY', 'peak', '0.30', '9.0', '27.0'],
19
+ ['deepseek-v4-flash-vision-exp', 'USD', 'off-peak', '0.007', '0.22', '0.66'],
20
+ ['deepseek-v4-flash-vision-exp', 'USD', 'peak', '0.014', '0.44', '1.32'],
21
+ ['deepseek-v4-flash-vision-exp', 'CNY', 'off-peak', '0.05', '1.5', '4.5'],
22
+ ['deepseek-v4-flash-vision-exp', 'CNY', 'peak', '0.10', '3.0', '9.0'],
23
+ ] as const)(
24
+ 'resolves %s %s %s from the versioned catalog',
25
+ (modelId, currency, band, hit, miss, output) => {
26
+ const resolved = resolveOfficialDeepSeekPrice({
27
+ provider: 'deepseek-official',
28
+ baseUrlClass: 'official-public',
29
+ apiRoute: 'chat-completions',
30
+ modelId,
31
+ currency,
32
+ at: band === 'peak'
33
+ ? new Date('2026-08-25T01:00:00.000Z')
34
+ : new Date('2026-08-23T01:00:00.000Z'),
35
+ })
36
+ expect(resolved).toMatchObject({
37
+ kind: 'priced',
38
+ record: {
39
+ catalogVersion: DEEPSEEK_OFFICIAL_PRICE_CATALOG_VERSION,
40
+ modelId, currency, band,
41
+ inputCacheHit: hit,
42
+ inputCacheMiss: miss,
43
+ output,
44
+ },
45
+ })
46
+ },
47
+ )
48
+
49
+ it.each(['chat-completions', 'responses'] as const)(
50
+ 'prices the documented %s route without changing the catalog tuple',
51
+ (apiRoute) => {
52
+ expect(resolveOfficialDeepSeekPrice({
53
+ provider: 'deepseek-official',
54
+ baseUrlClass: 'official-public',
55
+ apiRoute,
56
+ modelId: 'deepseek-v4-pro',
57
+ currency: 'USD',
58
+ at: new Date('2026-08-25T01:00:00.000Z'),
59
+ })).toMatchObject({
60
+ kind: 'priced',
61
+ record: { apiRoute, modelId: 'deepseek-v4-pro', band: 'peak' },
62
+ })
63
+ },
64
+ )
65
+
66
+ it.each([
67
+ ['2026-08-24T00:59:59.000Z', 'off-peak'],
68
+ ['2026-08-24T01:00:00.000Z', 'peak'],
69
+ ['2026-08-24T03:59:59.000Z', 'peak'],
70
+ ['2026-08-24T04:00:00.000Z', 'off-peak'],
71
+ ['2026-08-24T05:59:59.000Z', 'off-peak'],
72
+ ['2026-08-24T06:00:00.000Z', 'peak'],
73
+ ['2026-08-24T09:59:59.000Z', 'peak'],
74
+ ['2026-08-24T10:00:00.000Z', 'off-peak'],
75
+ ['2026-08-29T01:00:00.000Z', 'off-peak'],
76
+ ['2026-08-30T06:00:00.000Z', 'off-peak'],
77
+ ] as const)('classifies exact UTC schedule boundary %s as %s', (iso, expected) => {
78
+ expect(priceBandAt(new Date(iso))).toBe(expected)
79
+ })
80
+
81
+ it.each([
82
+ [{ provider: 'gateway', baseUrlClass: 'official-public' }, 'provider'],
83
+ [{ provider: 'deepseek-official', baseUrlClass: 'compatible-hmac:v1:test' }, 'base-url'],
84
+ [{ provider: 'deepseek-official', baseUrlClass: 'official-public', modelId: 'deepseek-chat' }, 'model'],
85
+ [{ provider: 'deepseek-official', baseUrlClass: 'official-public', apiRoute: 'other' }, 'route'],
86
+ ] as const)('fails closed instead of aliasing an unknown applicability: %s', (patch, reason) => {
87
+ const resolved = resolveOfficialDeepSeekPrice({
88
+ apiRoute: 'chat-completions',
89
+ modelId: 'deepseek-v4-flash',
90
+ currency: 'USD',
91
+ at: new Date('2026-08-25T01:00:00.000Z'),
92
+ ...patch,
93
+ })
94
+ expect(resolved.kind).toBe('unpriced')
95
+ if (resolved.kind !== 'unpriced')
96
+ throw new Error(`Expected an unpriced result, received ${resolved.kind}`)
97
+ expect(resolved.reason).toContain(reason)
98
+ })
99
+
100
+ it('computes provider-usage cost with fixed-point arithmetic, never JS float money', () => {
101
+ const cost = priceOfficialDeepSeekUsage({
102
+ provider: 'deepseek-official',
103
+ baseUrlClass: 'official-public',
104
+ apiRoute: 'chat-completions',
105
+ modelId: 'deepseek-v4-flash',
106
+ currency: 'USD',
107
+ startedAt: new Date('2026-08-25T01:00:00.000Z'),
108
+ completedAt: new Date('2026-08-25T01:01:00.000Z'),
109
+ usage: { cacheReadTokens: 1, cacheMissTokens: 2, outputTokens: 3 },
110
+ })
111
+ expect(cost).toEqual({
112
+ kind: 'exact',
113
+ currency: 'USD',
114
+ band: 'peak',
115
+ femtoUnits: '4854000000',
116
+ decimal: '0.000004854',
117
+ })
118
+ })
119
+
120
+ it('returns a range when one request spans a published price-band boundary', () => {
121
+ const cost = priceOfficialDeepSeekUsage({
122
+ provider: 'deepseek-official',
123
+ baseUrlClass: 'official-public',
124
+ apiRoute: 'chat-completions',
125
+ modelId: 'deepseek-v4-pro',
126
+ currency: 'CNY',
127
+ startedAt: new Date('2026-08-25T03:59:59.000Z'),
128
+ completedAt: new Date('2026-08-25T04:00:01.000Z'),
129
+ usage: { cacheReadTokens: 10, cacheMissTokens: 20, outputTokens: 3 },
130
+ })
131
+ expect(cost).toMatchObject({
132
+ kind: 'range', currency: 'CNY', bands: ['peak', 'off-peak'],
133
+ })
134
+ })
135
+
136
+ it('returns a range when both endpoints share a band but the interval crosses peak', () => {
137
+ const cost = priceOfficialDeepSeekUsage({
138
+ provider: 'deepseek-official',
139
+ baseUrlClass: 'official-public',
140
+ apiRoute: 'chat-completions',
141
+ modelId: 'deepseek-v4-flash',
142
+ currency: 'USD',
143
+ startedAt: new Date('2026-08-25T00:59:59.000Z'),
144
+ completedAt: new Date('2026-08-25T04:00:01.000Z'),
145
+ usage: { cacheReadTokens: 10, cacheMissTokens: 20, outputTokens: 3 },
146
+ })
147
+ expect(cost).toMatchObject({
148
+ kind: 'range', currency: 'USD', bands: ['off-peak', 'peak'],
149
+ })
150
+ })
151
+
152
+ it.each([
153
+ ['Friday through Monday', '2026-08-28T10:00:01.000Z', '2026-08-31T01:00:01.000Z'],
154
+ ['seven full days', '2026-08-25T00:00:00.000Z', '2026-09-01T00:00:00.000Z'],
155
+ ])('returns a range across %s', (_label, startedAt, completedAt) => {
156
+ expect(priceOfficialDeepSeekUsage({
157
+ provider: 'deepseek-official',
158
+ baseUrlClass: 'official-public',
159
+ apiRoute: 'responses',
160
+ modelId: 'deepseek-v4-flash',
161
+ currency: 'USD',
162
+ startedAt: new Date(startedAt),
163
+ completedAt: new Date(completedAt),
164
+ usage: { cacheReadTokens: 10, cacheMissTokens: 20, outputTokens: 3 },
165
+ })).toMatchObject({ kind: 'range', currency: 'USD' })
166
+ })
167
+
168
+ it.each([
169
+ ['invalid start timestamp', new Date('invalid'), new Date('2026-08-25T01:00:00.000Z'), 'timestamp'],
170
+ ['completion before start', new Date('2026-08-25T01:00:01.000Z'), new Date('2026-08-25T01:00:00.000Z'), 'precedes'],
171
+ ] as const)('fails closed for %s', (_label, startedAt, completedAt, reason) => {
172
+ const cost = priceOfficialDeepSeekUsage({
173
+ provider: 'deepseek-official',
174
+ baseUrlClass: 'official-public',
175
+ apiRoute: 'chat-completions',
176
+ modelId: 'deepseek-v4-flash',
177
+ currency: 'USD',
178
+ startedAt,
179
+ completedAt,
180
+ usage: { cacheReadTokens: 1, cacheMissTokens: 2, outputTokens: 3 },
181
+ })
182
+ expect(cost.kind).toBe('unpriced')
183
+ if (cost.kind !== 'unpriced') throw new Error('expected unpriced interval')
184
+ expect(cost.reason).toContain(reason)
185
+ })
186
+ })
@@ -1,122 +1,122 @@
1
- /** Node vision-token arithmetic against the official DeepSeek reference fixtures. */
2
-
3
- import { readFileSync } from 'node:fs'
4
- import { fileURLToPath } from 'node:url'
5
- import { describe, expect, it } from 'vitest'
6
- import {
7
- DEEPSEEK_VISION_DEFAULT_IMAGE_TOKENS,
8
- DEEPSEEK_VISION_IMAGE_ESTIMATOR,
9
- DEEPSEEK_VISION_PROJECTION,
10
- deepSeekVisionImageGrid,
11
- deepSeekVisionImageTokens,
12
- estimateDeepSeekVisionImageTokens,
13
- isWithinDeepSeekRequestPixelBudget,
14
- } from '../../src/runtime/deepseek-v4-vision-tokens.ts'
15
-
16
- interface GoldenEntry {
17
- readonly width: number
18
- readonly height: number
19
- readonly startTokenPos: number
20
- readonly nLlmH: number
21
- readonly nLlmW: number
22
- readonly tokens: number
23
- }
24
-
25
- interface GoldenFixture {
26
- readonly source: { readonly repository: string, readonly revision: string }
27
- readonly parameters: Readonly<Record<string, number>>
28
- readonly singleImages: readonly { width: number, height: number, nLlmH: number, nLlmW: number, tokensAtStart0: number }[]
29
- readonly startPositions: readonly GoldenEntry[]
30
- readonly sequences: readonly { entries: readonly GoldenEntry[] }[]
31
- }
32
-
33
- const fixture = JSON.parse(readFileSync(
34
- fileURLToPath(new URL('fixtures/vision-golden.json', import.meta.url)),
35
- 'utf8',
36
- )) as GoldenFixture
37
-
38
- describe('DeepSeek vision token arithmetic matches the official reference', () => {
39
- it('pins the official projection parameters and revision', () => {
40
- expect(DEEPSEEK_VISION_PROJECTION.sourceRepository).toBe(fixture.source.repository)
41
- expect(DEEPSEEK_VISION_PROJECTION.sourceRevision).toBe(fixture.source.revision)
42
- expect(DEEPSEEK_VISION_PROJECTION.visionPatchSize).toBe(fixture.parameters.visionPatchSize)
43
- expect(DEEPSEEK_VISION_PROJECTION.visionDownsampleRatio).toBe(fixture.parameters.visionDownsampleRatio)
44
- expect(DEEPSEEK_VISION_PROJECTION.visionMaxNTokens).toBe(fixture.parameters.visionMaxNTokens)
45
- expect(DEEPSEEK_VISION_PROJECTION.visionMinPixels).toBe(fixture.parameters.visionMinPixels)
46
- expect(DEEPSEEK_VISION_PROJECTION.visionMaxWhRatio).toBe(fixture.parameters.visionMaxWhRatio)
47
- })
48
-
49
- it.each(fixture.singleImages)('expands $width×$height to the official grid', (entry) => {
50
- const grid = deepSeekVisionImageGrid(entry.width, entry.height)
51
- expect(grid).toMatchObject({ nLlmH: entry.nLlmH, nLlmW: entry.nLlmW })
52
- expect(deepSeekVisionImageTokens(entry.width, entry.height, 0)).toBe(entry.tokensAtStart0)
53
- })
54
-
55
- it.each(fixture.startPositions)('aligns 640×480 at start $startTokenPos to $tokens tokens', (entry) => {
56
- const grid = deepSeekVisionImageGrid(entry.width, entry.height)
57
- expect(grid.nLlmH).toBe(entry.nLlmH)
58
- expect(grid.nLlmW).toBe(entry.nLlmW)
59
- expect(deepSeekVisionImageTokens(entry.width, entry.height, entry.startTokenPos)).toBe(entry.tokens)
60
- })
61
-
62
- it('reproduces multi-image sequences with accumulated start positions', () => {
63
- for (const sequence of fixture.sequences) {
64
- let position = sequence.entries[0]?.startTokenPos ?? 0
65
- for (const entry of sequence.entries) {
66
- expect(position).toBe(entry.startTokenPos)
67
- const grid = deepSeekVisionImageGrid(entry.width, entry.height)
68
- expect(grid.nLlmH).toBe(entry.nLlmH)
69
- expect(grid.nLlmW).toBe(entry.nLlmW)
70
- const tokens = deepSeekVisionImageTokens(entry.width, entry.height, position)
71
- expect(tokens).toBe(entry.tokens)
72
- position += tokens
73
- }
74
- }
75
- })
76
-
77
- it('reports the adapter default request pixel-budget boundary', () => {
78
- expect(isWithinDeepSeekRequestPixelBudget(640, 480)).toBe(true)
79
- expect(isWithinDeepSeekRequestPixelBudget(800, 800)).toBe(true) // 640000 exactly
80
- expect(isWithinDeepSeekRequestPixelBudget(801, 800)).toBe(false) // one pixel over
81
- expect(isWithinDeepSeekRequestPixelBudget(4096, 4096)).toBe(false)
82
- })
83
-
84
- it('estimates valid images at the midpoint of all alignment residues', () => {
85
- const estimate = estimateDeepSeekVisionImageTokens(512, 512)
86
- const counts = [0, 1, 2, 3].map(position => deepSeekVisionImageTokens(512, 512, position))
87
- expect(estimate).toEqual({
88
- tokens: Math.round((Math.min(...counts) + Math.max(...counts)) / 2),
89
- upperBoundTokens: DEEPSEEK_VISION_PROJECTION.visionMaxNTokens,
90
- source: 'intrinsic-grid',
91
- paddingMinimumTokens: Math.min(...counts),
92
- paddingMaximumTokens: Math.max(...counts),
93
- })
94
- expect(Math.max(...counts) - Math.min(...counts)).toBeLessThanOrEqual(3)
95
- expect(DEEPSEEK_VISION_IMAGE_ESTIMATOR.revision).toContain(fixture.source.revision)
96
- })
97
-
98
- it('keeps alignment-only estimation error below ten percent across the official fixture sizes', () => {
99
- for (const entry of fixture.singleImages) {
100
- const estimate = estimateDeepSeekVisionImageTokens(entry.width, entry.height)
101
- expect(estimate.source).toBe('intrinsic-grid')
102
- for (let position = 0; position < 4; position += 1) {
103
- const actual = deepSeekVisionImageTokens(entry.width, entry.height, position)
104
- expect(Math.abs(estimate.tokens - actual) / actual).toBeLessThan(0.1)
105
- }
106
- }
107
- })
108
-
109
- it.each([
110
- [0, 480],
111
- [640.5, 480],
112
- [Number.NaN, 480],
113
- [Number.POSITIVE_INFINITY, 480],
114
- [Number.MAX_SAFE_INTEGER, Number.MAX_SAFE_INTEGER],
115
- ])('uses the fixed fallback for unusable metadata %s×%s', (width, height) => {
116
- expect(estimateDeepSeekVisionImageTokens(width, height)).toEqual({
117
- tokens: DEEPSEEK_VISION_DEFAULT_IMAGE_TOKENS,
118
- upperBoundTokens: DEEPSEEK_VISION_PROJECTION.visionMaxNTokens,
119
- source: 'default',
120
- })
121
- })
122
- })
1
+ /** Node vision-token arithmetic against the official DeepSeek reference fixtures. */
2
+
3
+ import { readFileSync } from 'node:fs'
4
+ import { fileURLToPath } from 'node:url'
5
+ import { describe, expect, it } from 'vitest'
6
+ import {
7
+ DEEPSEEK_VISION_DEFAULT_IMAGE_TOKENS,
8
+ DEEPSEEK_VISION_IMAGE_ESTIMATOR,
9
+ DEEPSEEK_VISION_PROJECTION,
10
+ deepSeekVisionImageGrid,
11
+ deepSeekVisionImageTokens,
12
+ estimateDeepSeekVisionImageTokens,
13
+ isWithinDeepSeekRequestPixelBudget,
14
+ } from '../../src/runtime/deepseek-v4-vision-tokens.ts'
15
+
16
+ interface GoldenEntry {
17
+ readonly width: number
18
+ readonly height: number
19
+ readonly startTokenPos: number
20
+ readonly nLlmH: number
21
+ readonly nLlmW: number
22
+ readonly tokens: number
23
+ }
24
+
25
+ interface GoldenFixture {
26
+ readonly source: { readonly repository: string, readonly revision: string }
27
+ readonly parameters: Readonly<Record<string, number>>
28
+ readonly singleImages: readonly { width: number, height: number, nLlmH: number, nLlmW: number, tokensAtStart0: number }[]
29
+ readonly startPositions: readonly GoldenEntry[]
30
+ readonly sequences: readonly { entries: readonly GoldenEntry[] }[]
31
+ }
32
+
33
+ const fixture = JSON.parse(readFileSync(
34
+ fileURLToPath(new URL('fixtures/vision-golden.json', import.meta.url)),
35
+ 'utf8',
36
+ )) as GoldenFixture
37
+
38
+ describe('DeepSeek vision token arithmetic matches the official reference', () => {
39
+ it('pins the official projection parameters and revision', () => {
40
+ expect(DEEPSEEK_VISION_PROJECTION.sourceRepository).toBe(fixture.source.repository)
41
+ expect(DEEPSEEK_VISION_PROJECTION.sourceRevision).toBe(fixture.source.revision)
42
+ expect(DEEPSEEK_VISION_PROJECTION.visionPatchSize).toBe(fixture.parameters.visionPatchSize)
43
+ expect(DEEPSEEK_VISION_PROJECTION.visionDownsampleRatio).toBe(fixture.parameters.visionDownsampleRatio)
44
+ expect(DEEPSEEK_VISION_PROJECTION.visionMaxNTokens).toBe(fixture.parameters.visionMaxNTokens)
45
+ expect(DEEPSEEK_VISION_PROJECTION.visionMinPixels).toBe(fixture.parameters.visionMinPixels)
46
+ expect(DEEPSEEK_VISION_PROJECTION.visionMaxWhRatio).toBe(fixture.parameters.visionMaxWhRatio)
47
+ })
48
+
49
+ it.each(fixture.singleImages)('expands $width×$height to the official grid', (entry) => {
50
+ const grid = deepSeekVisionImageGrid(entry.width, entry.height)
51
+ expect(grid).toMatchObject({ nLlmH: entry.nLlmH, nLlmW: entry.nLlmW })
52
+ expect(deepSeekVisionImageTokens(entry.width, entry.height, 0)).toBe(entry.tokensAtStart0)
53
+ })
54
+
55
+ it.each(fixture.startPositions)('aligns 640×480 at start $startTokenPos to $tokens tokens', (entry) => {
56
+ const grid = deepSeekVisionImageGrid(entry.width, entry.height)
57
+ expect(grid.nLlmH).toBe(entry.nLlmH)
58
+ expect(grid.nLlmW).toBe(entry.nLlmW)
59
+ expect(deepSeekVisionImageTokens(entry.width, entry.height, entry.startTokenPos)).toBe(entry.tokens)
60
+ })
61
+
62
+ it('reproduces multi-image sequences with accumulated start positions', () => {
63
+ for (const sequence of fixture.sequences) {
64
+ let position = sequence.entries[0]?.startTokenPos ?? 0
65
+ for (const entry of sequence.entries) {
66
+ expect(position).toBe(entry.startTokenPos)
67
+ const grid = deepSeekVisionImageGrid(entry.width, entry.height)
68
+ expect(grid.nLlmH).toBe(entry.nLlmH)
69
+ expect(grid.nLlmW).toBe(entry.nLlmW)
70
+ const tokens = deepSeekVisionImageTokens(entry.width, entry.height, position)
71
+ expect(tokens).toBe(entry.tokens)
72
+ position += tokens
73
+ }
74
+ }
75
+ })
76
+
77
+ it('reports the adapter default request pixel-budget boundary', () => {
78
+ expect(isWithinDeepSeekRequestPixelBudget(640, 480)).toBe(true)
79
+ expect(isWithinDeepSeekRequestPixelBudget(800, 800)).toBe(true) // 640000 exactly
80
+ expect(isWithinDeepSeekRequestPixelBudget(801, 800)).toBe(false) // one pixel over
81
+ expect(isWithinDeepSeekRequestPixelBudget(4096, 4096)).toBe(false)
82
+ })
83
+
84
+ it('estimates valid images at the midpoint of all alignment residues', () => {
85
+ const estimate = estimateDeepSeekVisionImageTokens(512, 512)
86
+ const counts = [0, 1, 2, 3].map(position => deepSeekVisionImageTokens(512, 512, position))
87
+ expect(estimate).toEqual({
88
+ tokens: Math.round((Math.min(...counts) + Math.max(...counts)) / 2),
89
+ upperBoundTokens: DEEPSEEK_VISION_PROJECTION.visionMaxNTokens,
90
+ source: 'intrinsic-grid',
91
+ paddingMinimumTokens: Math.min(...counts),
92
+ paddingMaximumTokens: Math.max(...counts),
93
+ })
94
+ expect(Math.max(...counts) - Math.min(...counts)).toBeLessThanOrEqual(3)
95
+ expect(DEEPSEEK_VISION_IMAGE_ESTIMATOR.revision).toContain(fixture.source.revision)
96
+ })
97
+
98
+ it('keeps alignment-only estimation error below ten percent across the official fixture sizes', () => {
99
+ for (const entry of fixture.singleImages) {
100
+ const estimate = estimateDeepSeekVisionImageTokens(entry.width, entry.height)
101
+ expect(estimate.source).toBe('intrinsic-grid')
102
+ for (let position = 0; position < 4; position += 1) {
103
+ const actual = deepSeekVisionImageTokens(entry.width, entry.height, position)
104
+ expect(Math.abs(estimate.tokens - actual) / actual).toBeLessThan(0.1)
105
+ }
106
+ }
107
+ })
108
+
109
+ it.each([
110
+ [0, 480],
111
+ [640.5, 480],
112
+ [Number.NaN, 480],
113
+ [Number.POSITIVE_INFINITY, 480],
114
+ [Number.MAX_SAFE_INTEGER, Number.MAX_SAFE_INTEGER],
115
+ ])('uses the fixed fallback for unusable metadata %s×%s', (width, height) => {
116
+ expect(estimateDeepSeekVisionImageTokens(width, height)).toEqual({
117
+ tokens: DEEPSEEK_VISION_DEFAULT_IMAGE_TOKENS,
118
+ upperBoundTokens: DEEPSEEK_VISION_PROJECTION.visionMaxNTokens,
119
+ source: 'default',
120
+ })
121
+ })
122
+ })