@open33/harness-token-meter 0.0.0-stage → 0.1.0
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/LICENSE +22 -0
- package/README.i18n.yaml +64 -0
- package/README.md +162 -2
- package/README.zh.md +163 -0
- package/lib/index.js +981 -0
- package/lib/types/breakdown-projection.d.ts +128 -0
- package/lib/types/breakdown-projection.d.ts.map +1 -0
- package/lib/types/breakdown-projection.js +153 -0
- package/lib/types/breakdown-projection.js.map +1 -0
- package/lib/types/client.d.ts +9 -0
- package/lib/types/client.d.ts.map +1 -0
- package/lib/types/client.js +7 -0
- package/lib/types/client.js.map +1 -0
- package/lib/types/estimate.d.ts +50 -0
- package/lib/types/estimate.d.ts.map +1 -0
- package/lib/types/estimate.js +95 -0
- package/lib/types/estimate.js.map +1 -0
- package/lib/types/index.d.ts +73 -0
- package/lib/types/index.d.ts.map +1 -0
- package/lib/types/index.js +239 -0
- package/lib/types/index.js.map +1 -0
- package/lib/types/measurement-projection.d.ts +74 -0
- package/lib/types/measurement-projection.d.ts.map +1 -0
- package/lib/types/measurement-projection.js +94 -0
- package/lib/types/measurement-projection.js.map +1 -0
- package/lib/types/projection.d.ts +74 -0
- package/lib/types/projection.d.ts.map +1 -0
- package/lib/types/projection.js +7 -0
- package/lib/types/projection.js.map +1 -0
- package/lib/types/route-pricing.d.ts +31 -0
- package/lib/types/route-pricing.d.ts.map +1 -0
- package/lib/types/route-pricing.js +56 -0
- package/lib/types/route-pricing.js.map +1 -0
- package/lib/types/surface-fold.d.ts +68 -0
- package/lib/types/surface-fold.d.ts.map +1 -0
- package/lib/types/surface-fold.js +98 -0
- package/lib/types/surface-fold.js.map +1 -0
- package/lib/types/surface-projection.d.ts +58 -0
- package/lib/types/surface-projection.d.ts.map +1 -0
- package/lib/types/surface-projection.js +68 -0
- package/lib/types/surface-projection.js.map +1 -0
- package/lib/types/turn-usage.d.ts +33 -0
- package/lib/types/turn-usage.d.ts.map +1 -0
- package/lib/types/turn-usage.js +229 -0
- package/lib/types/turn-usage.js.map +1 -0
- package/lib/types/types.d.ts +56 -0
- package/lib/types/types.d.ts.map +1 -0
- package/lib/types/types.js +7 -0
- package/lib/types/types.js.map +1 -0
- package/lib/types/usage-projection.d.ts +196 -0
- package/lib/types/usage-projection.d.ts.map +1 -0
- package/lib/types/usage-projection.js +190 -0
- package/lib/types/usage-projection.js.map +1 -0
- package/locale/de.json +6 -0
- package/locale/en.json +5 -0
- package/locale/es.json +6 -0
- package/locale/fr.json +6 -0
- package/locale/ja.json +6 -0
- package/locale/pt.json +6 -0
- package/locale/ru.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +64 -3
- package/src/breakdown-projection.ts +184 -0
- package/src/client.ts +9 -0
- package/src/estimate.ts +102 -0
- package/src/index.ts +296 -0
- package/src/measurement-projection.ts +100 -0
- package/src/projection.ts +79 -0
- package/src/route-pricing.ts +76 -0
- package/src/surface-fold.ts +142 -0
- package/src/surface-projection.ts +96 -0
- package/src/turn-usage.ts +274 -0
- package/src/types.ts +54 -0
- package/src/usage-projection.ts +213 -0
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Heuristic composition of the current retained surface, independent of route
|
|
3
|
+
* image pricing and provider usage. The state keeps the surviving system
|
|
4
|
+
* prompts in surface order and one running surface total; every other node is
|
|
5
|
+
* priced only as a delta, so state size follows the system prompts, not the
|
|
6
|
+
* message count.
|
|
7
|
+
*/
|
|
8
|
+
import { z } from 'zod';
|
|
9
|
+
import { SessionSeq } from '@open33/harness-session';
|
|
10
|
+
/** Plain-JSON checkpoint: surviving system prompts in surface order, the surface total, and an armed claim. */
|
|
11
|
+
declare const contextBreakdownStateSchema: z.ZodObject<{
|
|
12
|
+
systems: z.ZodArray<z.ZodObject<{
|
|
13
|
+
seq: z.ZodPipe<z.ZodNumber, z.ZodTransform<SessionSeq, number>>;
|
|
14
|
+
tokens: z.ZodNumber;
|
|
15
|
+
}, z.core.$strict>>;
|
|
16
|
+
surfaceTokens: z.ZodNumber;
|
|
17
|
+
claim: z.ZodOptional<z.ZodObject<{
|
|
18
|
+
start: z.ZodPipe<z.ZodNumber, z.ZodTransform<SessionSeq, number>>;
|
|
19
|
+
end: z.ZodPipe<z.ZodNumber, z.ZodTransform<SessionSeq, number>>;
|
|
20
|
+
tokens: z.ZodNumber;
|
|
21
|
+
systemSeqs: z.ZodArray<z.ZodPipe<z.ZodNumber, z.ZodTransform<SessionSeq, number>>>;
|
|
22
|
+
}, z.core.$strict>>;
|
|
23
|
+
breakdown: z.ZodObject<{
|
|
24
|
+
systemTokens: z.ZodNumber;
|
|
25
|
+
toolsTokens: z.ZodNumber;
|
|
26
|
+
messageTokens: z.ZodNumber;
|
|
27
|
+
}, z.core.$strict>;
|
|
28
|
+
}, z.core.$strict>;
|
|
29
|
+
type ContextBreakdownState = z.infer<typeof contextBreakdownStateSchema>;
|
|
30
|
+
/**
|
|
31
|
+
* Context composition with the last nonempty surviving system in surface
|
|
32
|
+
* order classified as system tokens; all other visible prices are messages.
|
|
33
|
+
* Appends add their price; a replacement subtracts the shadow price of the
|
|
34
|
+
* adjacent `compaction/summary` or `compaction/prune` event, whose
|
|
35
|
+
* `shadowedSeqs` also name the system prompts it removes, and a system-prompt
|
|
36
|
+
* rewrite subtracts the retained price of the system node it replaces. Tools
|
|
37
|
+
* are priced from the latest request header. No route pricing applies.
|
|
38
|
+
*/
|
|
39
|
+
export declare const contextBreakdownProjectionDefinition: {
|
|
40
|
+
key: "contextBreakdown";
|
|
41
|
+
stateVersion: number;
|
|
42
|
+
stateSchema: z.ZodObject<{
|
|
43
|
+
systems: z.ZodArray<z.ZodObject<{
|
|
44
|
+
seq: z.ZodPipe<z.ZodNumber, z.ZodTransform<SessionSeq, number>>;
|
|
45
|
+
tokens: z.ZodNumber;
|
|
46
|
+
}, z.core.$strict>>;
|
|
47
|
+
surfaceTokens: z.ZodNumber;
|
|
48
|
+
claim: z.ZodOptional<z.ZodObject<{
|
|
49
|
+
start: z.ZodPipe<z.ZodNumber, z.ZodTransform<SessionSeq, number>>;
|
|
50
|
+
end: z.ZodPipe<z.ZodNumber, z.ZodTransform<SessionSeq, number>>;
|
|
51
|
+
tokens: z.ZodNumber;
|
|
52
|
+
systemSeqs: z.ZodArray<z.ZodPipe<z.ZodNumber, z.ZodTransform<SessionSeq, number>>>;
|
|
53
|
+
}, z.core.$strict>>;
|
|
54
|
+
breakdown: z.ZodObject<{
|
|
55
|
+
systemTokens: z.ZodNumber;
|
|
56
|
+
toolsTokens: z.ZodNumber;
|
|
57
|
+
messageTokens: z.ZodNumber;
|
|
58
|
+
}, z.core.$strict>;
|
|
59
|
+
}, z.core.$strict>;
|
|
60
|
+
init: () => ContextBreakdownState;
|
|
61
|
+
apply: (state: NoInfer<{
|
|
62
|
+
systems: {
|
|
63
|
+
seq: SessionSeq;
|
|
64
|
+
tokens: number;
|
|
65
|
+
}[];
|
|
66
|
+
surfaceTokens: number;
|
|
67
|
+
breakdown: {
|
|
68
|
+
systemTokens: number;
|
|
69
|
+
toolsTokens: number;
|
|
70
|
+
messageTokens: number;
|
|
71
|
+
};
|
|
72
|
+
claim?: {
|
|
73
|
+
start: SessionSeq;
|
|
74
|
+
end: SessionSeq;
|
|
75
|
+
tokens: number;
|
|
76
|
+
systemSeqs: SessionSeq[];
|
|
77
|
+
} | undefined;
|
|
78
|
+
}>, event: import("@open33/harness-session").SessionEvent) => {
|
|
79
|
+
systems: {
|
|
80
|
+
seq: SessionSeq;
|
|
81
|
+
tokens: number;
|
|
82
|
+
}[];
|
|
83
|
+
surfaceTokens: number;
|
|
84
|
+
breakdown: {
|
|
85
|
+
systemTokens: number;
|
|
86
|
+
toolsTokens: number;
|
|
87
|
+
messageTokens: number;
|
|
88
|
+
};
|
|
89
|
+
claim?: {
|
|
90
|
+
start: SessionSeq;
|
|
91
|
+
end: SessionSeq;
|
|
92
|
+
tokens: number;
|
|
93
|
+
systemSeqs: SessionSeq[];
|
|
94
|
+
} | undefined;
|
|
95
|
+
};
|
|
96
|
+
wire: {
|
|
97
|
+
viewSchema: z.ZodObject<{
|
|
98
|
+
systemTokens: z.ZodNumber;
|
|
99
|
+
toolsTokens: z.ZodNumber;
|
|
100
|
+
messageTokens: z.ZodNumber;
|
|
101
|
+
}, z.core.$strict>;
|
|
102
|
+
view: (state: NoInfer<{
|
|
103
|
+
systems: {
|
|
104
|
+
seq: SessionSeq;
|
|
105
|
+
tokens: number;
|
|
106
|
+
}[];
|
|
107
|
+
surfaceTokens: number;
|
|
108
|
+
breakdown: {
|
|
109
|
+
systemTokens: number;
|
|
110
|
+
toolsTokens: number;
|
|
111
|
+
messageTokens: number;
|
|
112
|
+
};
|
|
113
|
+
claim?: {
|
|
114
|
+
start: SessionSeq;
|
|
115
|
+
end: SessionSeq;
|
|
116
|
+
tokens: number;
|
|
117
|
+
systemSeqs: SessionSeq[];
|
|
118
|
+
} | undefined;
|
|
119
|
+
}>) => {
|
|
120
|
+
systemTokens: number;
|
|
121
|
+
toolsTokens: number;
|
|
122
|
+
messageTokens: number;
|
|
123
|
+
};
|
|
124
|
+
summary: false;
|
|
125
|
+
};
|
|
126
|
+
};
|
|
127
|
+
export {};
|
|
128
|
+
//# sourceMappingURL=breakdown-projection.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"breakdown-projection.d.ts","sourceRoot":"","sources":["../../src/breakdown-projection.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAA;AACvB,OAAO,EAAuD,UAAU,EAAE,MAAM,yBAAyB,CAAA;AAoCzG,+GAA+G;AAC/G,QAAA,MAAM,2BAA2B;;;;;;;;;;;;;;;;;kBAKtB,CAAA;AACX,KAAK,qBAAqB,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,2BAA2B,CAAC,CAAA;AAkFxE;;;;;;;;GAQG;AACH,eAAO,MAAM,oCAAoC;;;;;;;;;;;;;;;;;;;;;gBAIrC,qBAAqB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAoC0C,CAAA"}
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Heuristic composition of the current retained surface, independent of route
|
|
3
|
+
* image pricing and provider usage. The state keeps the surviving system
|
|
4
|
+
* prompts in surface order and one running surface total; every other node is
|
|
5
|
+
* priced only as a delta, so state size follows the system prompts, not the
|
|
6
|
+
* message count.
|
|
7
|
+
*/
|
|
8
|
+
import { z } from 'zod';
|
|
9
|
+
import { canonicalHeader, deriveEventMessage, isSurfaceEvent, SessionSeq } from '@open33/harness-session';
|
|
10
|
+
import { estimateMessage, estimateToolsTokens } from "./estimate.js";
|
|
11
|
+
const tokenCount = z.number().int().nonnegative();
|
|
12
|
+
const sessionSeq = z.number().int().nonnegative().max(Number.MAX_SAFE_INTEGER).transform(SessionSeq);
|
|
13
|
+
const breakdownSchema = z.object({
|
|
14
|
+
systemTokens: tokenCount,
|
|
15
|
+
toolsTokens: tokenCount,
|
|
16
|
+
messageTokens: tokenCount,
|
|
17
|
+
}).strict();
|
|
18
|
+
/** One surviving `system/message` node and its heuristic price. */
|
|
19
|
+
const systemNodeSchema = z.object({
|
|
20
|
+
seq: sessionSeq,
|
|
21
|
+
tokens: tokenCount,
|
|
22
|
+
}).strict();
|
|
23
|
+
/**
|
|
24
|
+
* Shadow price armed by the `compaction/summary` or `compaction/prune` event
|
|
25
|
+
* immediately before a replacement: the replaced range, its heuristic price,
|
|
26
|
+
* and the surviving system prompts inside it.
|
|
27
|
+
*/
|
|
28
|
+
const claimSchema = z.object({
|
|
29
|
+
start: sessionSeq,
|
|
30
|
+
end: sessionSeq,
|
|
31
|
+
tokens: tokenCount,
|
|
32
|
+
systemSeqs: z.array(sessionSeq),
|
|
33
|
+
}).strict();
|
|
34
|
+
/** Plain-JSON checkpoint: surviving system prompts in surface order, the surface total, and an armed claim. */
|
|
35
|
+
const contextBreakdownStateSchema = z.object({
|
|
36
|
+
systems: z.array(systemNodeSchema),
|
|
37
|
+
surfaceTokens: tokenCount,
|
|
38
|
+
claim: claimSchema.optional(),
|
|
39
|
+
breakdown: breakdownSchema,
|
|
40
|
+
}).strict();
|
|
41
|
+
/**
|
|
42
|
+
* Place one surface event's priced node.
|
|
43
|
+
* @param state - state before the event, without its armed claim.
|
|
44
|
+
* @param claim - shadow price armed by the immediately preceding event, if any.
|
|
45
|
+
* @param event - the surface event.
|
|
46
|
+
* @param tokens - the event message's heuristic price.
|
|
47
|
+
* @returns the system prompts and surface total after the event.
|
|
48
|
+
* @throws when a metered replacement names another range than its claim, or
|
|
49
|
+
* puts a system message in place of a metered range.
|
|
50
|
+
*/
|
|
51
|
+
function placeSurfaceEvent(state, claim, event, tokens) {
|
|
52
|
+
const op = event.surfaceOp;
|
|
53
|
+
const node = { seq: event.seq, tokens };
|
|
54
|
+
const isSystem = event.type === 'system/message';
|
|
55
|
+
if (op === 'append') {
|
|
56
|
+
return { systems: isSystem ? [...state.systems, node] : state.systems, surfaceTokens: state.surfaceTokens + tokens };
|
|
57
|
+
}
|
|
58
|
+
if (claim !== undefined) {
|
|
59
|
+
if (claim.start !== op.startSeq || claim.end !== op.endSeq) {
|
|
60
|
+
throw new Error(`context breakdown: replace at seq ${event.seq} over range ${op.startSeq}-${op.endSeq} has no adjacent shadow price`
|
|
61
|
+
+ ` (armed claim covers ${claim.start}-${claim.end})`);
|
|
62
|
+
}
|
|
63
|
+
if (isSystem) {
|
|
64
|
+
throw new Error(`context breakdown: replace at seq ${event.seq} puts a system message in place of a metered range;`
|
|
65
|
+
+ ' its order among the surviving system prompts is unknown');
|
|
66
|
+
}
|
|
67
|
+
const shadowed = new Set(claim.systemSeqs);
|
|
68
|
+
return {
|
|
69
|
+
systems: state.systems.filter(system => !shadowed.has(system.seq)),
|
|
70
|
+
surfaceTokens: state.surfaceTokens + tokens - claim.tokens,
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
const index = op.startSeq === op.endSeq ? state.systems.findIndex(system => system.seq === op.startSeq) : -1;
|
|
74
|
+
const replaced = state.systems[index];
|
|
75
|
+
if (replaced === undefined) {
|
|
76
|
+
// Bounded state cannot price an unmetered non-system range, so it folds at
|
|
77
|
+
// zero delta; current producers meter every such replacement.
|
|
78
|
+
return { systems: state.systems, surfaceTokens: state.surfaceTokens };
|
|
79
|
+
}
|
|
80
|
+
return {
|
|
81
|
+
systems: isSystem ? state.systems.with(index, node) : state.systems.toSpliced(index, 1),
|
|
82
|
+
surfaceTokens: state.surfaceTokens + tokens - replaced.tokens,
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
/** Publish totals, keeping the previous breakdown object when its figures are unchanged. */
|
|
86
|
+
function withTotals(state, totals) {
|
|
87
|
+
const surfaceTokens = Math.max(0, totals.surfaceTokens);
|
|
88
|
+
const systemTokens = totals.systems.findLast(system => system.tokens > 0)?.tokens ?? 0;
|
|
89
|
+
const messageTokens = Math.max(0, surfaceTokens - systemTokens);
|
|
90
|
+
const breakdown = systemTokens === state.breakdown.systemTokens && messageTokens === state.breakdown.messageTokens
|
|
91
|
+
? state.breakdown
|
|
92
|
+
: { systemTokens, toolsTokens: state.breakdown.toolsTokens, messageTokens };
|
|
93
|
+
return { ...state, systems: totals.systems, surfaceTokens, breakdown };
|
|
94
|
+
}
|
|
95
|
+
/** Drop an armed claim; the state is returned unchanged when none is armed. */
|
|
96
|
+
function withoutClaim(state) {
|
|
97
|
+
if (state.claim === undefined)
|
|
98
|
+
return state;
|
|
99
|
+
const { claim: _expired, ...unclaimed } = state;
|
|
100
|
+
return unclaimed;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Context composition with the last nonempty surviving system in surface
|
|
104
|
+
* order classified as system tokens; all other visible prices are messages.
|
|
105
|
+
* Appends add their price; a replacement subtracts the shadow price of the
|
|
106
|
+
* adjacent `compaction/summary` or `compaction/prune` event, whose
|
|
107
|
+
* `shadowedSeqs` also name the system prompts it removes, and a system-prompt
|
|
108
|
+
* rewrite subtracts the retained price of the system node it replaces. Tools
|
|
109
|
+
* are priced from the latest request header. No route pricing applies.
|
|
110
|
+
*/
|
|
111
|
+
export const contextBreakdownProjectionDefinition = {
|
|
112
|
+
key: 'contextBreakdown',
|
|
113
|
+
stateVersion: 6,
|
|
114
|
+
stateSchema: contextBreakdownStateSchema,
|
|
115
|
+
init: () => ({
|
|
116
|
+
systems: [],
|
|
117
|
+
surfaceTokens: 0,
|
|
118
|
+
breakdown: { systemTokens: 0, toolsTokens: 0, messageTokens: 0 },
|
|
119
|
+
}),
|
|
120
|
+
apply: (state, event) => {
|
|
121
|
+
if (event.type === 'compaction/summary' || event.type === 'compaction/prune') {
|
|
122
|
+
const shadowed = new Set(event.data.shadowedSeqs);
|
|
123
|
+
return {
|
|
124
|
+
...state,
|
|
125
|
+
claim: {
|
|
126
|
+
start: SessionSeq(event.data.shadowedRange.start),
|
|
127
|
+
end: SessionSeq(event.data.shadowedRange.end),
|
|
128
|
+
tokens: event.data.shadowedTokenCount,
|
|
129
|
+
systemSeqs: state.systems.filter(system => shadowed.has(system.seq)).map(system => system.seq),
|
|
130
|
+
},
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
// A claim prices only the event immediately after its metering event.
|
|
134
|
+
const unclaimed = withoutClaim(state);
|
|
135
|
+
if (event.type === 'request/header') {
|
|
136
|
+
const toolsTokens = estimateToolsTokens(canonicalHeader(event.data.header));
|
|
137
|
+
return toolsTokens === unclaimed.breakdown.toolsTokens
|
|
138
|
+
? unclaimed
|
|
139
|
+
: { ...unclaimed, breakdown: { ...unclaimed.breakdown, toolsTokens } };
|
|
140
|
+
}
|
|
141
|
+
if (!isSurfaceEvent(event))
|
|
142
|
+
return unclaimed;
|
|
143
|
+
const message = deriveEventMessage(event);
|
|
144
|
+
const tokens = message === null ? 0 : estimateMessage(message);
|
|
145
|
+
return withTotals(unclaimed, placeSurfaceEvent(unclaimed, state.claim, event, tokens));
|
|
146
|
+
},
|
|
147
|
+
wire: {
|
|
148
|
+
viewSchema: breakdownSchema,
|
|
149
|
+
view: state => state.breakdown,
|
|
150
|
+
summary: false,
|
|
151
|
+
},
|
|
152
|
+
};
|
|
153
|
+
//# sourceMappingURL=breakdown-projection.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"breakdown-projection.js","sourceRoot":"","sources":["../../src/breakdown-projection.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAA;AACvB,OAAO,EAAE,eAAe,EAAE,kBAAkB,EAAE,cAAc,EAAE,UAAU,EAAE,MAAM,yBAAyB,CAAA;AAKzG,OAAO,EAAE,eAAe,EAAE,mBAAmB,EAAE,MAAM,eAAe,CAAA;AAIpE,MAAM,UAAU,GAAG,CAAC,CAAC,MAAM,EAAE,CAAC,GAAG,EAAE,CAAC,WAAW,EAAE,CAAA;AACjD,MAAM,UAAU,GAAG,CAAC,CAAC,MAAM,EAAE,CAAC,GAAG,EAAE,CAAC,WAAW,EAAE,CAAC,GAAG,CAAC,MAAM,CAAC,gBAAgB,CAAC,CAAC,SAAS,CAAC,UAAU,CAAC,CAAA;AAEpG,MAAM,eAAe,GAAG,CAAC,CAAC,MAAM,CAAC;IAC/B,YAAY,EAAE,UAAU;IACxB,WAAW,EAAE,UAAU;IACvB,aAAa,EAAE,UAAU;CAC1B,CAAC,CAAC,MAAM,EAAE,CAAA;AAEX,mEAAmE;AACnE,MAAM,gBAAgB,GAAG,CAAC,CAAC,MAAM,CAAC;IAChC,GAAG,EAAE,UAAU;IACf,MAAM,EAAE,UAAU;CACnB,CAAC,CAAC,MAAM,EAAE,CAAA;AAEX;;;;GAIG;AACH,MAAM,WAAW,GAAG,CAAC,CAAC,MAAM,CAAC;IAC3B,KAAK,EAAE,UAAU;IACjB,GAAG,EAAE,UAAU;IACf,MAAM,EAAE,UAAU;IAClB,UAAU,EAAE,CAAC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AAI,CAAC,cAAc,CAAC,KAAK,CAAC;YAAE,OAAO,SAAS,CAAA;QAC5C,MAAM,OAAO,GAAG,kBAAkB,CAAC,KAAK,CAAC,CAAA;QACzC,MAAM,MAAM,GAAG,OAAO,KAAK,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,eAAe,CAAC,OAAO,CAAC,CAAA;QAC9D,OAAO,UAAU,CAAC,SAAS,EAAE,iBAAiB,CAAC,SAAS,EAAE,KAAK,CAAC,KAAK,EAAE,KAAK,EAAE,MAAM,CAAC,CAAC,CAAA;IACxF,CAAC;IACD,IAAI,EAAE;QACJ,UAAU,EAAE,eAAe;QAC3B,IAAI,EAAE,KAAK,CAAC,EAAE,CAAC,KAAK,CAAC,SAAS;QAC9B,OAAO,EAAE,KAAK;KACf;CACwE,CAAA"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Client-namespace projection of token-meter's browser-safe contracts and folds.
|
|
3
|
+
*
|
|
4
|
+
* @module @open33/harness-token-meter/client
|
|
5
|
+
*/
|
|
6
|
+
export type * from './projection.ts';
|
|
7
|
+
export { deriveTurnTokenUsage } from './turn-usage.ts';
|
|
8
|
+
export type { TurnTokenUsage, TurnTokenUsageRoute } from './turn-usage.ts';
|
|
9
|
+
//# sourceMappingURL=client.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../../src/client.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,mBAAmB,iBAAiB,CAAA;AACpC,OAAO,EAAE,oBAAoB,EAAE,MAAM,iBAAiB,CAAA;AACtD,YAAY,EAAE,cAAc,EAAE,mBAAmB,EAAE,MAAM,iBAAiB,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.js","sourceRoot":"","sources":["../../src/client.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAGH,OAAO,EAAE,oBAAoB,EAAE,MAAM,iBAAiB,CAAA"}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Fixed-density heuristic token pricing shared by the meter service and the
|
|
3
|
+
* pure context-breakdown projection, so both surfaces price identical content
|
|
4
|
+
* to identical numbers.
|
|
5
|
+
*
|
|
6
|
+
* @module @open33/harness-token-meter/estimate
|
|
7
|
+
*/
|
|
8
|
+
import type { ContentBlock, Message } from '@open33/harness-llm';
|
|
9
|
+
import type { EpochHeader } from '@open33/harness-session';
|
|
10
|
+
/** Role-field framing overhead added to every priced message. */
|
|
11
|
+
export declare const ROLE_OVERHEAD = 4;
|
|
12
|
+
/**
|
|
13
|
+
* Structural JSON price of one block outside the typed pricing arms: the
|
|
14
|
+
* fixed heuristic for merge-extended blocks and for image references, whose
|
|
15
|
+
* request price is route-owned rather than fixed. Image offload marks do not
|
|
16
|
+
* change this reference-only heuristic; route pricing owns their placeholders.
|
|
17
|
+
* @param block - block to price without mutation.
|
|
18
|
+
* @returns heuristic tokens for the block's JSON structure.
|
|
19
|
+
*/
|
|
20
|
+
export declare function estimateStructuralBlock(block: ContentBlock): number;
|
|
21
|
+
/**
|
|
22
|
+
* Price content blocks recursively under the fixed density heuristic.
|
|
23
|
+
* @param blocks - content blocks to price without mutation.
|
|
24
|
+
* @returns heuristic tokens including per-block structural overhead.
|
|
25
|
+
*/
|
|
26
|
+
export declare function estimateContent(blocks: readonly ContentBlock[]): number;
|
|
27
|
+
/**
|
|
28
|
+
* Price the rendered system prompt: the `system/message` surface node's text.
|
|
29
|
+
* Adapters serialize the prompt as a plain string — a system-role message or
|
|
30
|
+
* the request's system field — not as a typed content block, so the price is
|
|
31
|
+
* text density plus role framing with no per-block overhead.
|
|
32
|
+
* @param message - system-role message to price without mutation.
|
|
33
|
+
* @returns heuristic system-prompt tokens; 0 for empty content ("no system prompt").
|
|
34
|
+
*/
|
|
35
|
+
export declare function estimateSystemMessage(message: Message): number;
|
|
36
|
+
/**
|
|
37
|
+
* Heuristically price one model-visible message.
|
|
38
|
+
* @param message - message to price without mutation.
|
|
39
|
+
* @returns content and role-framing tokens under the fixed heuristic; a
|
|
40
|
+
* system-role message prices as {@link estimateSystemMessage}.
|
|
41
|
+
*/
|
|
42
|
+
export declare function estimateMessage(message: Message): number;
|
|
43
|
+
/**
|
|
44
|
+
* Price the tool-schema part of a canonical request envelope — the envelope's
|
|
45
|
+
* only priced field, since the system prompt is a surface node.
|
|
46
|
+
* @param header - canonical envelope, or undefined before any request.
|
|
47
|
+
* @returns heuristic tool-schema tokens; 0 when absent or empty.
|
|
48
|
+
*/
|
|
49
|
+
export declare function estimateToolsTokens(header: EpochHeader | undefined): number;
|
|
50
|
+
//# sourceMappingURL=estimate.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"estimate.d.ts","sourceRoot":"","sources":["../../src/estimate.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,KAAK,EAAE,YAAY,EAAE,OAAO,EAAE,MAAM,qBAAqB,CAAA;AAChE,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,yBAAyB,CAAA;AAQ1D,iEAAiE;AACjE,eAAO,MAAM,aAAa,IAAI,CAAA;AAE9B;;;;;;;GAOG;AACH,wBAAgB,uBAAuB,CAAC,KAAK,EAAE,YAAY,GAAG,MAAM,CAMnE;AAED;;;;GAIG;AACH,wBAAgB,eAAe,CAAC,MAAM,EAAE,SAAS,YAAY,EAAE,GAAG,MAAM,CAqBvE;AAED;;;;;;;GAOG;AACH,wBAAgB,qBAAqB,CAAC,OAAO,EAAE,OAAO,GAAG,MAAM,CAO9D;AAED;;;;;GAKG;AACH,wBAAgB,eAAe,CAAC,OAAO,EAAE,OAAO,GAAG,MAAM,CAGxD;AAED;;;;;GAKG;AACH,wBAAgB,mBAAmB,CAAC,MAAM,EAAE,WAAW,GAAG,SAAS,GAAG,MAAM,CAG3E"}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Fixed-density heuristic token pricing shared by the meter service and the
|
|
3
|
+
* pure context-breakdown projection, so both surfaces price identical content
|
|
4
|
+
* to identical numbers.
|
|
5
|
+
*
|
|
6
|
+
* @module @open33/harness-token-meter/estimate
|
|
7
|
+
*/
|
|
8
|
+
/** Fixed text-density estimate used until exact tokenization is needed. */
|
|
9
|
+
const CHARS_PER_TOKEN = 4;
|
|
10
|
+
/** Per-block structural overhead for JSON framing and type tags. */
|
|
11
|
+
const BLOCK_OVERHEAD = 4;
|
|
12
|
+
/** Role-field framing overhead added to every priced message. */
|
|
13
|
+
export const ROLE_OVERHEAD = 4;
|
|
14
|
+
/**
|
|
15
|
+
* Structural JSON price of one block outside the typed pricing arms: the
|
|
16
|
+
* fixed heuristic for merge-extended blocks and for image references, whose
|
|
17
|
+
* request price is route-owned rather than fixed. Image offload marks do not
|
|
18
|
+
* change this reference-only heuristic; route pricing owns their placeholders.
|
|
19
|
+
* @param block - block to price without mutation.
|
|
20
|
+
* @returns heuristic tokens for the block's JSON structure.
|
|
21
|
+
*/
|
|
22
|
+
export function estimateStructuralBlock(block) {
|
|
23
|
+
if (block.type === 'image') {
|
|
24
|
+
const { offloaded: _offloaded, ...reference } = block;
|
|
25
|
+
return BLOCK_OVERHEAD + Math.ceil(JSON.stringify(reference).length / CHARS_PER_TOKEN);
|
|
26
|
+
}
|
|
27
|
+
return BLOCK_OVERHEAD + Math.ceil(JSON.stringify(block).length / CHARS_PER_TOKEN);
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Price content blocks recursively under the fixed density heuristic.
|
|
31
|
+
* @param blocks - content blocks to price without mutation.
|
|
32
|
+
* @returns heuristic tokens including per-block structural overhead.
|
|
33
|
+
*/
|
|
34
|
+
export function estimateContent(blocks) {
|
|
35
|
+
let tokens = 0;
|
|
36
|
+
for (const block of blocks) {
|
|
37
|
+
switch (block.type) {
|
|
38
|
+
case 'text':
|
|
39
|
+
case 'reasoning':
|
|
40
|
+
tokens += Math.ceil(block.text.length / CHARS_PER_TOKEN) + BLOCK_OVERHEAD;
|
|
41
|
+
break;
|
|
42
|
+
case 'tool-call':
|
|
43
|
+
tokens += Math.ceil(block.name.length / CHARS_PER_TOKEN)
|
|
44
|
+
+ Math.ceil(block.arguments.length / CHARS_PER_TOKEN)
|
|
45
|
+
+ BLOCK_OVERHEAD;
|
|
46
|
+
break;
|
|
47
|
+
default:
|
|
48
|
+
// ContentBlockMap is merge-extensible; unknown blocks (and image
|
|
49
|
+
// references, whose request price is route-owned) retain a
|
|
50
|
+
// conservative structural JSON price under the fixed heuristic.
|
|
51
|
+
tokens += estimateStructuralBlock(block);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
return tokens;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Price the rendered system prompt: the `system/message` surface node's text.
|
|
58
|
+
* Adapters serialize the prompt as a plain string — a system-role message or
|
|
59
|
+
* the request's system field — not as a typed content block, so the price is
|
|
60
|
+
* text density plus role framing with no per-block overhead.
|
|
61
|
+
* @param message - system-role message to price without mutation.
|
|
62
|
+
* @returns heuristic system-prompt tokens; 0 for empty content ("no system prompt").
|
|
63
|
+
*/
|
|
64
|
+
export function estimateSystemMessage(message) {
|
|
65
|
+
if (message.content.length === 0)
|
|
66
|
+
return 0;
|
|
67
|
+
let characters = 0;
|
|
68
|
+
for (const block of message.content) {
|
|
69
|
+
characters += block.type === 'text' ? block.text.length : JSON.stringify(block).length;
|
|
70
|
+
}
|
|
71
|
+
return Math.ceil(characters / CHARS_PER_TOKEN) + ROLE_OVERHEAD;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Heuristically price one model-visible message.
|
|
75
|
+
* @param message - message to price without mutation.
|
|
76
|
+
* @returns content and role-framing tokens under the fixed heuristic; a
|
|
77
|
+
* system-role message prices as {@link estimateSystemMessage}.
|
|
78
|
+
*/
|
|
79
|
+
export function estimateMessage(message) {
|
|
80
|
+
if (message.role === 'system')
|
|
81
|
+
return estimateSystemMessage(message);
|
|
82
|
+
return estimateContent(message.content) + ROLE_OVERHEAD;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Price the tool-schema part of a canonical request envelope — the envelope's
|
|
86
|
+
* only priced field, since the system prompt is a surface node.
|
|
87
|
+
* @param header - canonical envelope, or undefined before any request.
|
|
88
|
+
* @returns heuristic tool-schema tokens; 0 when absent or empty.
|
|
89
|
+
*/
|
|
90
|
+
export function estimateToolsTokens(header) {
|
|
91
|
+
if (header?.tools === undefined || header.tools.length === 0)
|
|
92
|
+
return 0;
|
|
93
|
+
return Math.ceil(JSON.stringify(header.tools).length / CHARS_PER_TOKEN) + BLOCK_OVERHEAD;
|
|
94
|
+
}
|
|
95
|
+
//# sourceMappingURL=estimate.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"estimate.js","sourceRoot":"","sources":["../../src/estimate.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAKH,2EAA2E;AAC3E,MAAM,eAAe,GAAG,CAAC,CAAA;AAEzB,oEAAoE;AACpE,MAAM,cAAc,GAAG,CAAC,CAAA;AAExB,iEAAiE;AACjE,MAAM,CAAC,MAAM,aAAa,GAAG,CAAC,CAAA;AAE9B;;;;;;;GAOG;AACH,MAAM,UAAU,uBAAuB,CAAC,KAAmB;IACzD,IAAI,KAAK,CAAC,IAAI,KAAK,OAAO,EAAE,CAAC;QAC3B,MAAM,EAAE,SAAS,EAAE,UAAU,EAAE,GAAG,SAAS,EAAE,GAAG,KAAK,CAAA;QACrD,OAAO,cAAc,GAAG,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,SAAS,CAAC,CAAC,MAAM,GAAG,eAAe,CAAC,CAAA;IACvF,CAAC;IACD,OAAO,cAAc,GAAG,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC,MAAM,GAAG,eAAe,CAAC,CAAA;AACnF,CAAC;AAED;;;;GAIG;AACH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,UAAU,eAAe,CAAC,OAAgB;IAC9C,IAAI,OAAO,CAAC,IAAI,KAAK,QAAQ;QAAE,OAAO,qBAAqB,CAAC,OAAO,CAAC,CAAA;IACpE,OAAO,eAAe,CAAC,OAAO,CAAC,OAAO,CAAC,GAAG,aAAa,CAAA;AACzD,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,mBAAmB,CAAC,MAA+B;IACjE,IAAI,MAAM,EAAE,KAAK,KAAK,SAAS,IAAI,MAAM,CAAC,KAAK,CAAC,MAAM,KAAK,CAAC;QAAE,OAAO,CAAC,CAAA;IACtE,OAAO,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,MAAM,GAAG,eAAe,CAAC,GAAG,cAAc,CAAA;AAC1F,CAAC"}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Single replay-aware token-meter service for request and surface pressure.
|
|
3
|
+
*
|
|
4
|
+
* @module @open33/harness-token-meter
|
|
5
|
+
*/
|
|
6
|
+
import { Context, Service } from '@open33/cordis';
|
|
7
|
+
import z from '@open33/schemastery';
|
|
8
|
+
import type { Message } from '@open33/harness-llm';
|
|
9
|
+
import type { EpochHeader, Session } from '@open33/harness-session';
|
|
10
|
+
import type { ContextPressureProjection, TokenMeasurement, TokenMeterConfig } from './types.ts';
|
|
11
|
+
export type * from './types.ts';
|
|
12
|
+
declare module '@open33/cordis' {
|
|
13
|
+
interface Context {
|
|
14
|
+
tokenMeter: TokenMeter;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
/** Replay owner for one service-wide estimator and isolated per-session folds. */
|
|
18
|
+
export declare class TokenMeter extends Service {
|
|
19
|
+
static Config: z<TokenMeterConfig>;
|
|
20
|
+
static inject: string[];
|
|
21
|
+
private readonly states;
|
|
22
|
+
private measurementProjectionHandle;
|
|
23
|
+
private contextPressureProjectionHandle;
|
|
24
|
+
constructor(ctx: Context, config?: TokenMeterConfig);
|
|
25
|
+
protected [Service.init](): Promise<void>;
|
|
26
|
+
/**
|
|
27
|
+
* Read the latest logged provider prompt size and route capacity.
|
|
28
|
+
* @param session - Session whose committed usage and request capacity are read.
|
|
29
|
+
* @returns context pressure; fields remain absent until their source events are recorded.
|
|
30
|
+
*/
|
|
31
|
+
contextPressure(session: Session): Pick<ContextPressureProjection, 'pressureTokens' | 'contextWindow'>;
|
|
32
|
+
/**
|
|
33
|
+
* Measure current request pressure and surface through the durable tail.
|
|
34
|
+
*
|
|
35
|
+
* The effective envelope's routed provider/model selects the request-image
|
|
36
|
+
* pricing every node is priced under: a route whose adapter declares image
|
|
37
|
+
* pricing charges each retained image its visual tokens plus its
|
|
38
|
+
* model-visible text, while other routes keep the fixed heuristic. Provider
|
|
39
|
+
* usage is reused only when the latest successful call's canonical request
|
|
40
|
+
* envelope matches `requestHeader` and its total is no lower than that
|
|
41
|
+
* call's full route-priced anchor; otherwise the complete envelope and
|
|
42
|
+
* surface are repriced. The anchor includes all surface nodes immediately
|
|
43
|
+
* before the assistant message, including inputs admitted after step/start.
|
|
44
|
+
*
|
|
45
|
+
* `requestHeader` replaces the latest logged envelope for pressure and node
|
|
46
|
+
* pricing; the node set always describes the current session surface. Every
|
|
47
|
+
* call clones those positional nodes, so measurement is O(surface).
|
|
48
|
+
*
|
|
49
|
+
* @param session - session to replay through its current durable tail.
|
|
50
|
+
* @param requestHeader - optional effective request envelope replacing the latest logged header.
|
|
51
|
+
* @returns a detached deeply immutable pressure and surface measurement.
|
|
52
|
+
*/
|
|
53
|
+
measure(session: Session, requestHeader?: EpochHeader): TokenMeasurement;
|
|
54
|
+
/** Resolve the routed model's image pricing, when the llm service and route declare one. */
|
|
55
|
+
private _routeImagePricing;
|
|
56
|
+
/** Resolve request-time file projection when an LLM service is mounted. */
|
|
57
|
+
private _fileRequestText;
|
|
58
|
+
/**
|
|
59
|
+
* Heuristically price one model-visible message (instance face of the pure
|
|
60
|
+
* `estimateMessage` export from `estimate.ts`).
|
|
61
|
+
* @param message - message to price without mutation.
|
|
62
|
+
* @returns content and role-framing tokens under the fixed service heuristic.
|
|
63
|
+
*/
|
|
64
|
+
estimateMessage(message: Message): number;
|
|
65
|
+
/** Resolve one of the two header payloads retained by the rebuildable price cache. */
|
|
66
|
+
private _cachedHeader;
|
|
67
|
+
/** Catch rebuildable prices up to the current durable tail. */
|
|
68
|
+
private _sync;
|
|
69
|
+
/** Update only discardable positional prices and the pre-assistant view. */
|
|
70
|
+
private _foldEvent;
|
|
71
|
+
}
|
|
72
|
+
export default TokenMeter;
|
|
73
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,gBAAgB,CAAA;AAEjD,OAAO,CAAC,MAAM,qBAAqB,CAAA;AACnC,OAAO,KAAK,EAAsC,OAAO,EAAc,MAAM,qBAAqB,CAAA;AAElG,OAAO,KAAK,EACV,WAAW,EACX,OAAO,EAIR,MAAM,yBAAyB,CAAA;AAShC,OAAO,KAAK,EACV,yBAAyB,EACzB,gBAAgB,EAEhB,gBAAgB,EACjB,MAAM,YAAY,CAAA;AAWnB,mBAAmB,YAAY,CAAA;AAqC/B,OAAO,QAAQ,gBAAgB,CAAC;IAC9B,UAAU,OAAO;QACf,UAAU,EAAE,UAAU,CAAA;KACvB;CACF;AAED,kFAAkF;AAClF,qBAAa,UAAW,SAAQ,OAAO;IAGrC,MAAM,CAAC,MAAM,EAAE,CAAC,CAAC,gBAAgB,CAAC,CAAiD;IAEnF,MAAM,CAAC,MAAM,WAAyB;IAEtC,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAsC;IAC7D,OAAO,CAAC,2BAA2B,CAAgD;IACnF,OAAO,CAAC,+BAA+B,CAAyC;gBAEpE,GAAG,EAAE,OAAO,EAAE,MAAM,G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