@snylonue/dsh-context-policy 0.3.0 → 0.3.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/dist/types.d.ts CHANGED
@@ -49,15 +49,24 @@ export interface ResolvedPolicy {
49
49
  replyPrimingTokens: number;
50
50
  includeReasoning: boolean;
51
51
  }
52
+ export interface ReplayHeader extends EpochHeader {
53
+ toolsKey?: string;
54
+ toolCounts?: Record<string, number> | null;
55
+ }
52
56
  export interface ReplayNode {
53
57
  seq: SessionSeq;
54
- message: Message | null;
58
+ message: Pick<Message, "content"> | null;
59
+ counts?: {
60
+ text: Record<string, number>;
61
+ reasoning: Record<string, number>;
62
+ envelope: boolean;
63
+ };
55
64
  }
56
65
  export interface ReplayState {
57
66
  nodes: readonly ReplayNode[];
58
- header: EpochHeader | null;
67
+ header: ReplayHeader | null;
59
68
  anchor: {
60
- header: EpochHeader;
69
+ header: ReplayHeader;
61
70
  nodes: readonly ReplayNode[];
62
71
  usage: TokenUsage;
63
72
  } | null;
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACX,WAAW,EACX,UAAU,EACV,gBAAgB,EAChB,YAAY,EACZ,MAAM,0BAA0B,CAAC;AAClC,OAAO,KAAK,EAAE,OAAO,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAC;AAEhE,6EAA6E;AAC7E,MAAM,MAAM,UAAU,GACnB,YAAY,GACZ;IACA,IAAI,EAAE,eAAe,CAAC;IACtB,GAAG,EAAE,UAAU,CAAC;IAChB,IAAI,EAAE;QAAE,OAAO,EAAE;YAAE,GAAG,EAAE,UAAU,CAAC;YAAC,YAAY,EAAE,MAAM,EAAE,CAAA;SAAE,EAAE,CAAA;KAAE,CAAC;CAChE,GACD;IACA,IAAI,EAAE,mBAAmB,CAAC;IAC1B,GAAG,EAAE,UAAU,CAAC;IAChB,IAAI,EAAE;QA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9B"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@snylonue/dsh-context-policy",
3
- "version": "0.3.0",
3
+ "version": "0.3.1",
4
4
  "description": "Percentage-only compaction and model-configurable GPT and DeepSeek token counting for DSH",
5
5
  "type": "module",
6
6
  "license": "MIT",
package/scripts/check.ts CHANGED
@@ -11,6 +11,8 @@ for (const args of [
11
11
  "--test",
12
12
  "tests/current-host.test.ts",
13
13
  "tests/policy.test.ts",
14
+ "tests/compact.test.ts",
15
+ "tests/replay.test.ts",
14
16
  "tests/bundle.test.ts",
15
17
  "tests/tokenizers.test.ts",
16
18
  ],
@@ -20,5 +20,5 @@ export function createTokenizerCounter(
20
20
  return new Tokenizer(data, {});
21
21
  });
22
22
  return (text) =>
23
- tokenizer.value.encode(text, { add_special_tokens: false }).ids.length;
23
+ tokenizer.value.tokenize(text, { add_special_tokens: false }).length;
24
24
  }
package/src/compact.ts ADDED
@@ -0,0 +1,189 @@
1
+ import { createHash } from "node:crypto";
2
+ import type { EpochHeader } from "@deepseek-ai/dsh-session";
3
+ import { SessionLogOffset, SessionSeq } from "@deepseek-ai/dsh-session";
4
+ import type { FileBlock, ImageBlock, TokenUsage } from "@deepseek-ai/dsh-llm";
5
+ import { z } from "zod";
6
+ import { contentTokens, fold, headerToolsKey, initialState } from "./core.js";
7
+ import { deepFreeze } from "@deepseek-ai/dsh-util-values";
8
+ import { countText, tokenizerGeneration } from "./tokenizers.js";
9
+ import type {
10
+ MeterConfig,
11
+ MeterEvent,
12
+ ReplayHeader,
13
+ ReplayNode,
14
+ } from "./types.js";
15
+ export { measureState as measureCompact } from "./core.js";
16
+
17
+ const count = z.number().int().nonnegative();
18
+ const counts = z.record(z.string(), count);
19
+ const nodeSchema = z.object({
20
+ seq: count.transform(SessionSeq),
21
+ system: z.boolean(),
22
+ counts: z.object({ text: counts, reasoning: counts, envelope: z.boolean() }),
23
+ // Only durable attachment references survive; never text or tool-call bodies.
24
+ message: z
25
+ .object({ content: z.array(z.custom<ImageBlock | FileBlock>()) })
26
+ .nullable(),
27
+ heuristicTokens: count.optional(),
28
+ });
29
+ const headerSchema = z.object({
30
+ config: z.custom<EpochHeader["config"]>(),
31
+ adapterDefaults: z.custom<EpochHeader["adapterDefaults"]>().optional(),
32
+ toolsKey: z.string(),
33
+ toolCounts: counts.nullable(),
34
+ });
35
+ export const compactSchema = z.object({
36
+ nodes: z.array(nodeSchema),
37
+ header: headerSchema.nullable(),
38
+ anchor: z
39
+ .object({
40
+ header: headerSchema,
41
+ nodes: z.array(nodeSchema),
42
+ usage: z.custom<TokenUsage>(),
43
+ })
44
+ .nullable(),
45
+ logRevision: count.transform(SessionLogOffset),
46
+ });
47
+ export type CompactState = z.infer<typeof compactSchema>;
48
+ export const initialCompactState = () => initialState() as CompactState;
49
+
50
+ /** Counts cannot be reused across schema or policy changes. */
51
+ export function compactVersion(config: MeterConfig) {
52
+ const hash = createHash("sha256")
53
+ .update(JSON.stringify([2, config]))
54
+ .digest("hex");
55
+ return 0x100000000 + Number.parseInt(hash.slice(0, 8), 16);
56
+ }
57
+ function tokenizerNames(config: MeterConfig) {
58
+ return [
59
+ ...new Set([
60
+ config.defaultTokenizer,
61
+ ...[...(config.providerPolicies ?? []), ...config.modelPolicies].flatMap(
62
+ (p) => (p.tokenizer === undefined ? [] : [p.tokenizer]),
63
+ ),
64
+ ]),
65
+ ];
66
+ }
67
+ interface PayloadCounts {
68
+ header?: Pick<NonNullable<CompactState["header"]>, "toolsKey" | "toolCounts">;
69
+ node?: CompactState["nodes"][number]["counts"];
70
+ }
71
+ // The host cold-replays one whole projection at a time. A bounded text cache
72
+ // cannot bridge those passes. Share only immutable numeric payload summaries:
73
+ // never a fold state, body, tool schema, attachment, or strong event reference.
74
+ let payloads = new WeakMap<MeterEvent, Map<string, PayloadCounts>>();
75
+ let payloadGeneration = -1;
76
+ function payloadCounts(event: MeterEvent, config: MeterConfig): PayloadCounts {
77
+ const generation = tokenizerGeneration();
78
+ if (generation !== payloadGeneration) {
79
+ payloads = new WeakMap();
80
+ payloadGeneration = generation;
81
+ }
82
+ let policies = payloads.get(event);
83
+ if (!policies) {
84
+ policies = new Map();
85
+ payloads.set(event, policies);
86
+ }
87
+ // Overheads and route/reasoning selection do not affect these base counts.
88
+ const key = JSON.stringify(tokenizerNames(config));
89
+ let summary = policies.get(key);
90
+ if (!summary) {
91
+ summary = {};
92
+ policies.set(key, summary);
93
+ }
94
+ return summary;
95
+ }
96
+ function compactHeader(
97
+ header: ReplayHeader,
98
+ config: MeterConfig,
99
+ memo: PayloadCounts,
100
+ ) {
101
+ if (!memo.header) {
102
+ const tools = header.tools?.length ? JSON.stringify(header.tools) : null;
103
+ memo.header = deepFreeze({
104
+ toolsKey: headerToolsKey(header),
105
+ toolCounts:
106
+ tools === null
107
+ ? null
108
+ : Object.fromEntries(
109
+ tokenizerNames(config).map((name) => [
110
+ name,
111
+ countText(tools, name),
112
+ ]),
113
+ ),
114
+ });
115
+ }
116
+ return {
117
+ config: header.config,
118
+ ...(header.adapterDefaults
119
+ ? { adapterDefaults: header.adapterDefaults }
120
+ : {}),
121
+ ...memo.header,
122
+ };
123
+ }
124
+ function compactNode(
125
+ node: ReplayNode,
126
+ system: boolean,
127
+ config: MeterConfig,
128
+ memo: PayloadCounts,
129
+ ): CompactState["nodes"][number] {
130
+ const content = node.message?.content ?? [];
131
+ const attachments = content.filter(
132
+ (b): b is ImageBlock | FileBlock => b.type === "image" || b.type === "file",
133
+ );
134
+ if (!memo.node) {
135
+ const text = content.filter((b) => b.type !== "image" && b.type !== "file");
136
+ const names = tokenizerNames(config);
137
+ const prices = (blocks: typeof content, includeReasoning: boolean) =>
138
+ Object.fromEntries(
139
+ names.map((tokenizer) => [
140
+ tokenizer,
141
+ contentTokens(blocks, { tokenizer, includeReasoning }),
142
+ ]),
143
+ );
144
+ memo.node = deepFreeze({
145
+ text: prices(text, false),
146
+ reasoning: prices(
147
+ text.filter((b) => b.type === "reasoning"),
148
+ true,
149
+ ),
150
+ envelope: text.some((b) => b.type === "text" || b.type === "tool-call"),
151
+ });
152
+ }
153
+ return {
154
+ seq: node.seq,
155
+ system,
156
+ counts: memo.node,
157
+ message: attachments.length ? { content: attachments } : null,
158
+ };
159
+ }
160
+
161
+ /** The existing fold owns usage, offload and replacement semantics; only its new payloads are summarized. */
162
+ export function foldCompact(
163
+ state: CompactState,
164
+ event: MeterEvent,
165
+ config: MeterConfig,
166
+ ): CompactState {
167
+ const next = fold(state, event);
168
+ return {
169
+ ...next,
170
+ header:
171
+ next.header === state.header
172
+ ? state.header
173
+ : next.header &&
174
+ compactHeader(next.header, config, payloadCounts(event, config)),
175
+ nodes:
176
+ next.nodes === state.nodes
177
+ ? state.nodes
178
+ : next.nodes.map((node) =>
179
+ node.counts
180
+ ? node
181
+ : compactNode(
182
+ node,
183
+ event.type === "system/message",
184
+ config,
185
+ payloadCounts(event, config),
186
+ ),
187
+ ),
188
+ } as CompactState;
189
+ }
package/src/core.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import { createHash } from "node:crypto";
1
2
  import type {
2
3
  LlmImageRequestPrice,
3
4
  LlmImageRequestPricing,
@@ -11,6 +12,7 @@ import type { TokenMeasurementBaseline } from "@deepseek-ai/dsh-token-meter";
11
12
  import type {
12
13
  MeterConfig,
13
14
  MeterEvent,
15
+ ReplayHeader,
14
16
  ReplayNode,
15
17
  ReplayState,
16
18
  } from "./types.js";
@@ -36,6 +38,19 @@ import {
36
38
  import { deepFreeze } from "@deepseek-ai/dsh-util-values";
37
39
  import { countText, resolvePolicy } from "./tokenizers.js";
38
40
 
41
+ /** Tool schemas compare in order, identically for raw and summarized headers. */
42
+ export const headerToolsKey = (header: ReplayHeader) =>
43
+ header.toolsKey ??
44
+ createHash("sha256")
45
+ .update(JSON.stringify(header.tools ?? []))
46
+ .digest("hex");
47
+ function meterHeaderEquals(a: ReplayHeader, b: ReplayHeader) {
48
+ return a.toolsKey || b.toolsKey
49
+ ? headerEquals({ ...a, tools: undefined }, { ...b, tools: undefined }) &&
50
+ headerToolsKey(a) === headerToolsKey(b)
51
+ : headerEquals(a, b);
52
+ }
53
+
39
54
  export const initialState = (): ReplayState => ({
40
55
  nodes: [],
41
56
  header: null,
@@ -123,10 +138,18 @@ export function fold(state: ReplayState, event: MeterEvent): ReplayState {
123
138
  return next;
124
139
  }
125
140
 
126
- function contentTokens(
141
+ export function contentTokens(
127
142
  blocks: Message["content"],
128
- policy: ReturnType<typeof resolvePolicy>,
129
- attachments: Attachments,
143
+ policy: Pick<
144
+ ReturnType<typeof resolvePolicy>,
145
+ "tokenizer" | "includeReasoning"
146
+ >,
147
+ attachments: Attachments = {
148
+ imagePrices: [],
149
+ imageCursor: 0,
150
+ unpricedImages: false,
151
+ unpricedFiles: false,
152
+ },
130
153
  ) {
131
154
  const count = (text: string) => countText(text, policy.tokenizer);
132
155
  let tokens = 0;
@@ -170,7 +193,7 @@ function contentTokens(
170
193
 
171
194
  export function priceSurface(
172
195
  nodes: readonly ReplayNode[],
173
- header: EpochHeader | null,
196
+ header: ReplayHeader | null,
174
197
  config: MeterConfig,
175
198
  services: MeterServices,
176
199
  ) {
@@ -203,23 +226,27 @@ export function priceSurface(
203
226
  throw new Error("Invalid route image pricing");
204
227
  const priced = nodes.map((node) => {
205
228
  const message = node.message;
206
- let tokens = 0;
207
- if (message) {
208
- const body = contentTokens(message.content, policy, attachments);
209
- if (
210
- body > 0 ||
211
- message.content.some((b) => b.type === "text" || b.type === "tool-call")
212
- )
213
- tokens = body + policy.tokensPerMessage;
214
- }
229
+ const counts = node.counts;
230
+ const body =
231
+ contentTokens(message?.content ?? [], policy, attachments) +
232
+ (counts?.text[policy.tokenizer] ?? 0) +
233
+ (policy.includeReasoning
234
+ ? (counts?.reasoning[policy.tokenizer] ?? 0)
235
+ : 0);
236
+ const envelope =
237
+ counts?.envelope ||
238
+ message?.content.some((b) => b.type === "text" || b.type === "tool-call");
239
+ const tokens = body > 0 || envelope ? body + policy.tokensPerMessage : 0;
215
240
  // DSH 0.2.1-rc.2 compaction requires this field for shadowedTokenCount.
216
241
  return { seq: node.seq, tokens, heuristicTokens: tokens };
217
242
  });
218
243
  const surfaceTokens = priced.reduce((sum, node) => sum + node.tokens, 0);
219
- const toolsTokens = header?.tools?.length
220
- ? countText(JSON.stringify(header.tools), policy.tokenizer) +
221
- policy.toolsOverhead
222
- : 0;
244
+ const toolsTokens = header?.toolCounts
245
+ ? header.toolCounts[policy.tokenizer] + policy.toolsOverhead
246
+ : header?.tools?.length
247
+ ? countText(JSON.stringify(header.tools), policy.tokenizer) +
248
+ policy.toolsOverhead
249
+ : 0;
223
250
  const primingTokens = header || nodes.length ? policy.replyPrimingTokens : 0;
224
251
  return {
225
252
  nodes: priced,
@@ -242,11 +269,11 @@ export function measureState(
242
269
  services: MeterServices = {},
243
270
  requestHeader?: EpochHeader,
244
271
  ) {
245
- const header =
272
+ const header: ReplayHeader | null =
246
273
  requestHeader === undefined ? state.header : canonicalHeader(requestHeader);
247
274
  const current = priceSurface(state.nodes, header, config, services);
248
275
  const anchor = state.anchor;
249
- const reusable = anchor && header && headerEquals(anchor.header, header);
276
+ const reusable = anchor && header && meterHeaderEquals(anchor.header, header);
250
277
  let baseline: TokenMeasurementBaseline;
251
278
  let surfaceDeltaTokens = 0;
252
279
  if (reusable) {
package/src/projection.ts CHANGED
@@ -1,6 +1,5 @@
1
- import type { Message } from "@deepseek-ai/dsh-llm";
2
- import type { EpochHeader, SessionEvent } from "@deepseek-ai/dsh-session";
3
- import { SessionLogOffset, SessionSeq } from "@deepseek-ai/dsh-session";
1
+ import type { SessionEvent } from "@deepseek-ai/dsh-session";
2
+ import { SessionSeq } from "@deepseek-ai/dsh-session";
4
3
  import type { ProjectionDefinition } from "@deepseek-ai/dsh-session-projection";
5
4
  import type {
6
5
  ContextBreakdownProjection,
@@ -8,32 +7,36 @@ import type {
8
7
  } from "@deepseek-ai/dsh-token-meter";
9
8
  import { z } from "zod";
10
9
  import type { MeterServices } from "./core.js";
11
- import { fold, initialState, measureState } from "./core.js";
12
- import type { MeterConfig, ReplayNode, ReplayState } from "./types.js";
10
+ import {
11
+ compactSchema,
12
+ compactVersion,
13
+ foldCompact,
14
+ initialCompactState,
15
+ measureCompact,
16
+ } from "./compact.js";
17
+ import type { CompactState } from "./compact.js";
18
+ import type { MeterConfig } from "./types.js";
13
19
 
14
20
  const count = z.number().int().nonnegative();
15
- const nodeSchema = z.object({
16
- seq: count.transform(SessionSeq),
17
- message: z.custom<Message>().nullable(),
18
- });
19
- const replaySchema = z.object({
20
- nodes: z.array(nodeSchema),
21
- header: z.custom<EpochHeader>().nullable(),
22
- anchor: z.custom<NonNullable<ReplayState["anchor"]>>().nullable(),
23
- logRevision: count.transform(SessionLogOffset),
21
+ export function createReplayProjection(config: MeterConfig) {
22
+ return {
23
+ key: "contextPolicyMeter",
24
+ stateVersion: compactVersion(config),
25
+ stateSchema: compactSchema,
26
+ init: initialCompactState,
27
+ apply: (state: CompactState, event: SessionEvent) =>
28
+ foldCompact(state, event, config),
29
+ } satisfies ProjectionDefinition<"contextPolicyMeter">;
30
+ }
31
+ export const replayProjection = createReplayProjection({
32
+ defaultTokenizer: "deepseek_v4",
33
+ modelPolicies: [],
24
34
  });
25
- export const replayProjection: ProjectionDefinition<"contextPolicyMeter"> = {
26
- key: "contextPolicyMeter",
27
- stateVersion: 2,
28
- stateSchema: replaySchema,
29
- init: initialState,
30
- apply: fold,
31
- };
32
35
 
33
- // These native projection keys must satisfy the DSH 0.2.1-rc.2 state contract.
34
- interface DisplayState extends ReplayState {
36
+ // Keep the native state contract without retaining message bodies or tool schemas.
37
+ interface DisplayState extends CompactState {
35
38
  systems: { seq: SessionSeq; tokens: number }[];
36
- nodes: (ReplayNode & { system: boolean; heuristicTokens: number })[];
39
+ nodes: (CompactState["nodes"][number] & { heuristicTokens: number })[];
37
40
  surfaceTokens: number;
38
41
  breakdown: ContextBreakdownProjection;
39
42
  contextWindow?: number;
@@ -43,12 +46,12 @@ const breakdownSchema = z.object({
43
46
  toolsTokens: count,
44
47
  messageTokens: count,
45
48
  });
46
- const displaySchema = replaySchema.extend({
49
+ const displaySchema = compactSchema.extend({
47
50
  systems: z.array(
48
51
  z.object({ seq: count.transform(SessionSeq), tokens: count }),
49
52
  ),
50
53
  nodes: z.array(
51
- nodeSchema.extend({ system: z.boolean(), heuristicTokens: count }),
54
+ compactSchema.shape.nodes.element.extend({ heuristicTokens: count }),
52
55
  ),
53
56
  surfaceTokens: count,
54
57
  breakdown: breakdownSchema,
@@ -56,7 +59,7 @@ const displaySchema = replaySchema.extend({
56
59
  });
57
60
  function initialDisplayState(): DisplayState {
58
61
  return {
59
- ...initialState(),
62
+ ...initialCompactState(),
60
63
  systems: [],
61
64
  nodes: [],
62
65
  surfaceTokens: 0,
@@ -69,13 +72,11 @@ function foldDisplay(
69
72
  config: MeterConfig,
70
73
  services: MeterServices,
71
74
  ): DisplayState {
72
- const next = fold(state, event);
73
- const { contextWindow, ...withoutWindow } = {
74
- ...next,
75
- contextWindow: state.contextWindow,
76
- };
75
+ const next = foldCompact(state, event, config);
77
76
  const window =
78
- event.type === "request/context" ? event.data.contextWindow : contextWindow;
77
+ event.type === "request/context"
78
+ ? event.data.contextWindow
79
+ : state.contextWindow;
79
80
  if (
80
81
  next.nodes === state.nodes &&
81
82
  next.header === state.header &&
@@ -83,31 +84,26 @@ function foldDisplay(
83
84
  window === state.contextWindow
84
85
  )
85
86
  return state;
86
- const measurement = measureState(next, config, services);
87
- // Empty system events have no derived Message but still occupy a surface position.
88
- const systemSeqs = new Set(state.systems.map((node) => node.seq));
87
+ const measurement = measureCompact(next, config, services);
89
88
  const nodes = next.nodes.map((node, index) => ({
90
89
  ...node,
91
- system:
92
- systemSeqs.has(node.seq) ||
93
- (event.type === "system/message" && node.seq === event.seq),
94
- // Native consumers use this field; it carries the model count, not a second estimator.
95
90
  heuristicTokens: measurement.nodes[index].tokens,
96
91
  }));
97
- const surfaceTokens = measurement.surfaceTokens;
98
92
  const systems = nodes.flatMap((node) =>
99
93
  node.system ? [{ seq: node.seq, tokens: node.heuristicTokens }] : [],
100
94
  );
101
95
  const systemTokens = systems.findLast((node) => node.tokens > 0)?.tokens ?? 0;
96
+ const { contextWindow: _previousWindow, ...withoutWindow } =
97
+ next as CompactState & { contextWindow?: number };
102
98
  return {
103
99
  ...withoutWindow,
104
100
  systems,
105
101
  nodes,
106
- surfaceTokens,
102
+ surfaceTokens: measurement.surfaceTokens,
107
103
  breakdown: {
108
104
  systemTokens,
109
105
  toolsTokens: measurement.toolsTokens,
110
- messageTokens: surfaceTokens - systemTokens,
106
+ messageTokens: measurement.surfaceTokens - systemTokens,
111
107
  },
112
108
  ...(window === undefined ? {} : { contextWindow: window }),
113
109
  };
@@ -127,7 +123,7 @@ export function createDisplayProjections(
127
123
  const view = (state: DisplayState) => {
128
124
  const cached = views.get(state);
129
125
  if (cached) return cached;
130
- const measurement = measureState(state, config, services);
126
+ const measurement = measureCompact(state, config, services);
131
127
  const systemTokens =
132
128
  measurement.nodes.findLast(
133
129
  (node, index) => state.nodes[index].system && node.tokens > 0,
@@ -151,7 +147,7 @@ export function createDisplayProjections(
151
147
  return value;
152
148
  };
153
149
  const replay = {
154
- stateVersion: 9,
150
+ stateVersion: compactVersion(config),
155
151
  stateSchema: displaySchema,
156
152
  init: initialDisplayState,
157
153
  apply: (state: DisplayState, event: SessionEvent) =>
@@ -3,17 +3,22 @@ import type { Context } from "@deepseek-ai/cordis";
3
3
  import type { EpochHeader, Session } from "@deepseek-ai/dsh-session";
4
4
  import { SessionSeq } from "@deepseek-ai/dsh-session";
5
5
  import type { Message } from "@deepseek-ai/dsh-llm";
6
- import type { MeterConfig, MeterConfigInput, ReplayState } from "./types.js";
6
+ import type { MeterConfig, MeterConfigInput } from "./types.js";
7
+ import type { CompactState } from "./compact.js";
8
+ import { measureCompact } from "./compact.js";
7
9
  import type { MeterServices } from "./core.js";
8
10
  import "@deepseek-ai/dsh-session-projection";
9
11
  import settings from "@deepseek-ai/schemastery";
10
- import { measureState, priceSurface } from "./core.js";
11
- import { createDisplayProjections, replayProjection } from "./projection.js";
12
+ import { priceSurface } from "./core.js";
13
+ import {
14
+ createDisplayProjections,
15
+ createReplayProjection,
16
+ } from "./projection.js";
12
17
  import { usageProjection } from "./usage.js";
13
18
 
14
19
  declare module "@deepseek-ai/dsh-session-projection" {
15
20
  interface SessionProjectionStateMap {
16
- contextPolicyMeter: ReplayState;
21
+ contextPolicyMeter: CompactState;
17
22
  }
18
23
  }
19
24
 
@@ -60,7 +65,9 @@ export class ModelTokenMeter extends Service {
60
65
  ctx.sessionProjections.register(usageProjection);
61
66
  ctx.sessionProjections.register(display.pressure);
62
67
  ctx.sessionProjections.register(display.breakdown);
63
- ctx.sessionProjections.register(replayProjection);
68
+ ctx.sessionProjections.register(
69
+ createReplayProjection(this.config),
70
+ );
64
71
  }
65
72
  config: MeterConfig;
66
73
  services: MeterServices;
@@ -73,7 +80,7 @@ export class ModelTokenMeter extends Service {
73
80
  throw new Error(
74
81
  "ModelTokenMeter contextPolicyMeter projection is unavailable",
75
82
  );
76
- return measureState(state, this.config, this.services, requestHeader);
83
+ return measureCompact(state, this.config, this.services, requestHeader);
77
84
  }
78
85
  estimateMessage(message: Message) {
79
86
  return priceSurface(
package/src/tokenizers.ts CHANGED
@@ -13,6 +13,10 @@ const tokenizers = new Map<string, (text: string) => number>([
13
13
  ["o200k_base", createTokenizerCounter("o200k-base")],
14
14
  ]);
15
15
 
16
+ let generation = 0;
17
+ /** Invalidates derived numeric summaries when custom counters are replaced. */
18
+ export const tokenizerGeneration = () => generation;
19
+
16
20
  export function registerTokenizer(
17
21
  name: string,
18
22
  count: (text: string) => number,
@@ -20,10 +24,12 @@ export function registerTokenizer(
20
24
  if (tokenizers.has(name))
21
25
  throw new Error(`Tokenizer already registered: ${name}`);
22
26
  tokenizers.set(name, count);
27
+ generation++;
23
28
  return () => {
24
29
  if (tokenizers.get(name) === count) {
25
30
  tokenizers.delete(name);
26
31
  caches.delete(count);
32
+ generation++;
27
33
  }
28
34
  };
29
35
  }
package/src/types.ts CHANGED
@@ -51,15 +51,24 @@ export interface ResolvedPolicy {
51
51
  replyPrimingTokens: number;
52
52
  includeReasoning: boolean;
53
53
  }
54
+ export interface ReplayHeader extends EpochHeader {
55
+ toolsKey?: string;
56
+ toolCounts?: Record<string, number> | null;
57
+ }
54
58
  export interface ReplayNode {
55
59
  seq: SessionSeq;
56
- message: Message | null;
60
+ message: Pick<Message, "content"> | null;
61
+ counts?: {
62
+ text: Record<string, number>;
63
+ reasoning: Record<string, number>;
64
+ envelope: boolean;
65
+ };
57
66
  }
58
67
  export interface ReplayState {
59
68
  nodes: readonly ReplayNode[];
60
- header: EpochHeader | null;
69
+ header: ReplayHeader | null;
61
70
  anchor: {
62
- header: EpochHeader;
71
+ header: ReplayHeader;
63
72
  nodes: readonly ReplayNode[];
64
73
  usage: TokenUsage;
65
74
  } | null;