@snylonue/dsh-context-policy 0.3.0 → 0.3.2
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/README.md +3 -1
- package/dist/builtin-tokenizers.js +1 -1
- package/dist/builtin-tokenizers.js.map +1 -1
- package/dist/compact.d.ts +56 -0
- package/dist/compact.d.ts.map +1 -0
- package/dist/compact.js +139 -0
- package/dist/compact.js.map +1 -0
- package/dist/core.d.ts +16 -3
- package/dist/core.d.ts.map +1 -1
- package/dist/core.js +34 -13
- package/dist/core.js.map +1 -1
- package/dist/projection.d.ts +266 -33
- package/dist/projection.d.ts.map +1 -1
- package/dist/projection.js +27 -38
- package/dist/projection.js.map +1 -1
- package/dist/token-meter.d.ts +3 -2
- package/dist/token-meter.d.ts.map +1 -1
- package/dist/token-meter.js +5 -4
- package/dist/token-meter.js.map +1 -1
- package/dist/tokenizers.d.ts +2 -0
- package/dist/tokenizers.d.ts.map +1 -1
- package/dist/tokenizers.js +5 -0
- package/dist/tokenizers.js.map +1 -1
- package/dist/types.d.ts +12 -3
- package/dist/types.d.ts.map +1 -1
- package/package.json +17 -17
- package/scripts/check.ts +2 -0
- package/src/builtin-tokenizers.ts +1 -1
- package/src/compact.ts +189 -0
- package/src/core.ts +46 -19
- package/src/projection.ts +41 -45
- package/src/token-meter.ts +13 -6
- package/src/tokenizers.ts +6 -0
- package/src/types.ts +12 -3
- package/tests/compact.test.ts +475 -0
- package/tests/current-host.test.ts +19 -6
- package/tests/policy.test.ts +6 -6
- package/tests/replay.test.ts +255 -0
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:
|
|
67
|
+
header: ReplayHeader | null;
|
|
59
68
|
anchor: {
|
|
60
|
-
header:
|
|
69
|
+
header: ReplayHeader;
|
|
61
70
|
nodes: readonly ReplayNode[];
|
|
62
71
|
usage: TokenUsage;
|
|
63
72
|
} | null;
|
package/dist/types.d.ts.map
CHANGED
|
@@ -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;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,MAAM,CAAA;KAAE,CAAC;CACpC,CAAC;AAEL,MAAM,WAAW,WAAW;IAC3B,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,gBAAgB,CAAC,E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AC1B;AACD,MAAM,WAAW,YAAa,SAAQ,WAAW;IAChD,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,UAAU,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,IAAI,CAAC;CAC3C;AACD,MAAM,WAAW,UAAU;IAC1B,GAAG,EAAE,UAAU,CAAC;IAChB,OAAO,EAAE,IAAI,CAAC,OAAO,EAAE,SAAS,CAAC,GAAG,IAAI,CAAC;IACzC,MAAM,CAAC,EAAE;QACR,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;QAC7B,SAAS,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;QAClC,QAAQ,EAAE,OAAO,CAAC;KAClB,CAAC;CACF;AACD,MAAM,WAAW,WAAW;IAC3B,KAAK,EAAE,SAAS,UAAU,EAAE,CAAC;IAC7B,MAAM,EAAE,YAAY,GAAG,IAAI,CAAC;IAC5B,MAAM,EAAE;QACP,MAAM,EAAE,YAAY,CAAC;QACrB,KAAK,EAAE,SAAS,UAAU,EAAE,CAAC;QAC7B,KAAK,EAAE,UAAU,CAAC;KAClB,GAAG,IAAI,CAAC;IACT,WAAW,EAAE,gBAAgB,CAAC;CAC9B"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@snylonue/dsh-context-policy",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.2",
|
|
4
4
|
"description": "Percentage-only compaction and model-configurable GPT and DeepSeek token counting for DSH",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
},
|
|
15
15
|
"engines": {
|
|
16
16
|
"node": ">=24",
|
|
17
|
-
"dsh": "0.2.1-rc.2"
|
|
17
|
+
"dsh": "0.2.1-rc.2 || 0.2.1-rc.3"
|
|
18
18
|
},
|
|
19
19
|
"exports": {
|
|
20
20
|
".": {
|
|
@@ -68,13 +68,13 @@
|
|
|
68
68
|
},
|
|
69
69
|
"peerDependencies": {
|
|
70
70
|
"@deepseek-ai/cordis": "~4.0.5",
|
|
71
|
-
"@deepseek-ai/dsh-compaction": "0.2.1-rc.2",
|
|
72
|
-
"@deepseek-ai/dsh-compaction-basic": "0.2.1-rc.2",
|
|
73
|
-
"@deepseek-ai/dsh-llm": "0.2.1-rc.2",
|
|
74
|
-
"@deepseek-ai/dsh-session": "0.2.1-rc.2",
|
|
75
|
-
"@deepseek-ai/dsh-session-projection": "0.2.1-rc.2",
|
|
76
|
-
"@deepseek-ai/dsh-token-meter": "0.2.1-rc.2",
|
|
77
|
-
"@deepseek-ai/dsh-util-values": "0.2.1-rc.2",
|
|
71
|
+
"@deepseek-ai/dsh-compaction": "0.2.1-rc.2 || 0.2.1-rc.3",
|
|
72
|
+
"@deepseek-ai/dsh-compaction-basic": "0.2.1-rc.2 || 0.2.1-rc.3",
|
|
73
|
+
"@deepseek-ai/dsh-llm": "0.2.1-rc.2 || 0.2.1-rc.3",
|
|
74
|
+
"@deepseek-ai/dsh-session": "0.2.1-rc.2 || 0.2.1-rc.3",
|
|
75
|
+
"@deepseek-ai/dsh-session-projection": "0.2.1-rc.2 || 0.2.1-rc.3",
|
|
76
|
+
"@deepseek-ai/dsh-token-meter": "0.2.1-rc.2 || 0.2.1-rc.3",
|
|
77
|
+
"@deepseek-ai/dsh-util-values": "0.2.1-rc.2 || 0.2.1-rc.3",
|
|
78
78
|
"@deepseek-ai/schemastery": "~3.18.5",
|
|
79
79
|
"zod": "^4.4.3"
|
|
80
80
|
},
|
|
@@ -87,14 +87,14 @@
|
|
|
87
87
|
"devDependencies": {
|
|
88
88
|
"@biomejs/biome": "2",
|
|
89
89
|
"@deepseek-ai/cordis": "4.0.5",
|
|
90
|
-
"@deepseek-ai/dsh-app-boot": "0.2.1-rc.
|
|
91
|
-
"@deepseek-ai/dsh-compaction": "0.2.1-rc.
|
|
92
|
-
"@deepseek-ai/dsh-compaction-basic": "0.2.1-rc.
|
|
93
|
-
"@deepseek-ai/dsh-llm": "0.2.1-rc.
|
|
94
|
-
"@deepseek-ai/dsh-session": "0.2.1-rc.
|
|
95
|
-
"@deepseek-ai/dsh-session-projection": "0.2.1-rc.
|
|
96
|
-
"@deepseek-ai/dsh-token-meter": "0.2.1-rc.
|
|
97
|
-
"@deepseek-ai/dsh-util-values": "0.2.1-rc.
|
|
90
|
+
"@deepseek-ai/dsh-app-boot": "0.2.1-rc.3",
|
|
91
|
+
"@deepseek-ai/dsh-compaction": "0.2.1-rc.3",
|
|
92
|
+
"@deepseek-ai/dsh-compaction-basic": "0.2.1-rc.3",
|
|
93
|
+
"@deepseek-ai/dsh-llm": "0.2.1-rc.3",
|
|
94
|
+
"@deepseek-ai/dsh-session": "0.2.1-rc.3",
|
|
95
|
+
"@deepseek-ai/dsh-session-projection": "0.2.1-rc.3",
|
|
96
|
+
"@deepseek-ai/dsh-token-meter": "0.2.1-rc.3",
|
|
97
|
+
"@deepseek-ai/dsh-util-values": "0.2.1-rc.3",
|
|
98
98
|
"@deepseek-ai/schemastery": "3.18.5",
|
|
99
99
|
"@types/node": "24.19.0",
|
|
100
100
|
"typescript": "5.9.3",
|
package/scripts/check.ts
CHANGED
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:
|
|
129
|
-
|
|
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:
|
|
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
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
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?.
|
|
220
|
-
?
|
|
221
|
-
|
|
222
|
-
|
|
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 &&
|
|
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 {
|
|
2
|
-
import
|
|
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 {
|
|
12
|
-
|
|
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
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
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
|
-
//
|
|
34
|
-
interface DisplayState extends
|
|
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: (
|
|
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 =
|
|
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
|
-
|
|
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
|
-
...
|
|
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 =
|
|
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"
|
|
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 =
|
|
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 =
|
|
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:
|
|
150
|
+
stateVersion: compactVersion(config),
|
|
155
151
|
stateSchema: displaySchema,
|
|
156
152
|
init: initialDisplayState,
|
|
157
153
|
apply: (state: DisplayState, event: SessionEvent) =>
|
package/src/token-meter.ts
CHANGED
|
@@ -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
|
|
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 {
|
|
11
|
-
import {
|
|
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:
|
|
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(
|
|
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
|
|
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
|
}
|