@snylonue/dsh-context-policy 0.2.1 → 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/README.md +2 -2
- 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 +17 -4
- package/dist/core.d.ts.map +1 -1
- package/dist/core.js +36 -15
- package/dist/core.js.map +1 -1
- package/dist/projection.d.ts +267 -34
- 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 +102 -103
- package/scripts/check.ts +4 -0
- package/src/builtin-tokenizers.ts +1 -1
- package/src/compact.ts +189 -0
- package/src/core.ts +55 -27
- package/src/projection.ts +45 -47
- 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 +15 -0
- package/tests/policy.test.ts +6 -6
- package/tests/replay.test.ts +255 -0
package/src/projection.ts
CHANGED
|
@@ -1,39 +1,42 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import
|
|
3
|
-
import type { Message } from "@deepseek-ai/dsh-llm";
|
|
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
|
-
ContextPressureProjection,
|
|
7
5
|
ContextBreakdownProjection,
|
|
6
|
+
ContextPressureProjection,
|
|
8
7
|
} from "@deepseek-ai/dsh-token-meter";
|
|
9
8
|
import { z } from "zod";
|
|
10
|
-
import { initialState, fold, measureState } from "./core.js";
|
|
11
9
|
import type { MeterServices } from "./core.js";
|
|
12
|
-
import
|
|
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,10 +46,12 @@ const breakdownSchema = z.object({
|
|
|
43
46
|
toolsTokens: count,
|
|
44
47
|
messageTokens: count,
|
|
45
48
|
});
|
|
46
|
-
const displaySchema =
|
|
47
|
-
systems: z.array(
|
|
49
|
+
const displaySchema = compactSchema.extend({
|
|
50
|
+
systems: z.array(
|
|
51
|
+
z.object({ seq: count.transform(SessionSeq), tokens: count }),
|
|
52
|
+
),
|
|
48
53
|
nodes: z.array(
|
|
49
|
-
|
|
54
|
+
compactSchema.shape.nodes.element.extend({ heuristicTokens: count }),
|
|
50
55
|
),
|
|
51
56
|
surfaceTokens: count,
|
|
52
57
|
breakdown: breakdownSchema,
|
|
@@ -54,7 +59,7 @@ const displaySchema = replaySchema.extend({
|
|
|
54
59
|
});
|
|
55
60
|
function initialDisplayState(): DisplayState {
|
|
56
61
|
return {
|
|
57
|
-
...
|
|
62
|
+
...initialCompactState(),
|
|
58
63
|
systems: [],
|
|
59
64
|
nodes: [],
|
|
60
65
|
surfaceTokens: 0,
|
|
@@ -67,13 +72,11 @@ function foldDisplay(
|
|
|
67
72
|
config: MeterConfig,
|
|
68
73
|
services: MeterServices,
|
|
69
74
|
): DisplayState {
|
|
70
|
-
const next =
|
|
71
|
-
const { contextWindow, ...withoutWindow } = {
|
|
72
|
-
...next,
|
|
73
|
-
contextWindow: state.contextWindow,
|
|
74
|
-
};
|
|
75
|
+
const next = foldCompact(state, event, config);
|
|
75
76
|
const window =
|
|
76
|
-
event.type === "request/context"
|
|
77
|
+
event.type === "request/context"
|
|
78
|
+
? event.data.contextWindow
|
|
79
|
+
: state.contextWindow;
|
|
77
80
|
if (
|
|
78
81
|
next.nodes === state.nodes &&
|
|
79
82
|
next.header === state.header &&
|
|
@@ -81,31 +84,26 @@ function foldDisplay(
|
|
|
81
84
|
window === state.contextWindow
|
|
82
85
|
)
|
|
83
86
|
return state;
|
|
84
|
-
const measurement =
|
|
85
|
-
// Empty system events have no derived Message but still occupy a surface position.
|
|
86
|
-
const systemSeqs = new Set(state.systems.map((node) => node.seq));
|
|
87
|
+
const measurement = measureCompact(next, config, services);
|
|
87
88
|
const nodes = next.nodes.map((node, index) => ({
|
|
88
89
|
...node,
|
|
89
|
-
system:
|
|
90
|
-
systemSeqs.has(node.seq) ||
|
|
91
|
-
(event.type === "system/message" && node.seq === event.seq),
|
|
92
|
-
// Native consumers use this field; it carries the model count, not a second estimator.
|
|
93
90
|
heuristicTokens: measurement.nodes[index].tokens,
|
|
94
91
|
}));
|
|
95
|
-
const surfaceTokens = measurement.surfaceTokens;
|
|
96
92
|
const systems = nodes.flatMap((node) =>
|
|
97
93
|
node.system ? [{ seq: node.seq, tokens: node.heuristicTokens }] : [],
|
|
98
94
|
);
|
|
99
95
|
const systemTokens = systems.findLast((node) => node.tokens > 0)?.tokens ?? 0;
|
|
96
|
+
const { contextWindow: _previousWindow, ...withoutWindow } =
|
|
97
|
+
next as CompactState & { contextWindow?: number };
|
|
100
98
|
return {
|
|
101
99
|
...withoutWindow,
|
|
102
100
|
systems,
|
|
103
101
|
nodes,
|
|
104
|
-
surfaceTokens,
|
|
102
|
+
surfaceTokens: measurement.surfaceTokens,
|
|
105
103
|
breakdown: {
|
|
106
104
|
systemTokens,
|
|
107
105
|
toolsTokens: measurement.toolsTokens,
|
|
108
|
-
messageTokens: surfaceTokens - systemTokens,
|
|
106
|
+
messageTokens: measurement.surfaceTokens - systemTokens,
|
|
109
107
|
},
|
|
110
108
|
...(window === undefined ? {} : { contextWindow: window }),
|
|
111
109
|
};
|
|
@@ -125,7 +123,7 @@ export function createDisplayProjections(
|
|
|
125
123
|
const view = (state: DisplayState) => {
|
|
126
124
|
const cached = views.get(state);
|
|
127
125
|
if (cached) return cached;
|
|
128
|
-
const measurement =
|
|
126
|
+
const measurement = measureCompact(state, config, services);
|
|
129
127
|
const systemTokens =
|
|
130
128
|
measurement.nodes.findLast(
|
|
131
129
|
(node, index) => state.nodes[index].system && node.tokens > 0,
|
|
@@ -149,7 +147,7 @@ export function createDisplayProjections(
|
|
|
149
147
|
return value;
|
|
150
148
|
};
|
|
151
149
|
const replay = {
|
|
152
|
-
stateVersion:
|
|
150
|
+
stateVersion: compactVersion(config),
|
|
153
151
|
stateSchema: displaySchema,
|
|
154
152
|
init: initialDisplayState,
|
|
155
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
|
}
|
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:
|
|
69
|
+
header: ReplayHeader | null;
|
|
61
70
|
anchor: {
|
|
62
|
-
header:
|
|
71
|
+
header: ReplayHeader;
|
|
63
72
|
nodes: readonly ReplayNode[];
|
|
64
73
|
usage: TokenUsage;
|
|
65
74
|
} | null;
|