sidekick-shared 0.23.1 → 0.24.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 +134 -37
- package/dist/aggregation/EventAggregator.d.ts +5 -1
- package/dist/aggregation/EventAggregator.js +64 -131
- package/dist/aggregation/types.d.ts +3 -0
- package/dist/browser.d.ts +6 -0
- package/dist/browser.js +10 -1
- package/dist/context/sessionContext.js +8 -6
- package/dist/extractors/tokenUsage.js +1 -1
- package/dist/index.d.ts +11 -1
- package/dist/index.js +23 -7
- package/dist/modelInfo.d.ts +17 -7
- package/dist/modelInfo.js +5 -2
- package/dist/node.d.ts +4 -0
- package/dist/node.js +8 -1
- package/dist/observedSessionCollector.d.ts +71 -0
- package/dist/observedSessionCollector.js +179 -0
- package/dist/parsers/codexParser.js +32 -14
- package/dist/parsers/openCodeParser.js +22 -1
- package/dist/parsers/subagentTraceParser.js +11 -5
- package/dist/providers/claudeCode.js +45 -1
- package/dist/providers/codex.js +17 -7
- package/dist/report/transcriptParser.d.ts +3 -11
- package/dist/report/transcriptParser.js +69 -154
- package/dist/schemas/index.d.ts +7 -1
- package/dist/schemas/index.js +19 -2
- package/dist/schemas/observedSessionV1.d.ts +32 -1
- package/dist/schemas/observedSessionV1.js +11 -1
- package/dist/schemas/sessionEvent.d.ts +373 -12
- package/dist/schemas/sessionEvent.js +101 -16
- package/dist/schemas/tokenEstimation.d.ts +14 -0
- package/dist/schemas/tokenEstimation.js +10 -0
- package/dist/schemas/transcript.d.ts +25 -0
- package/dist/schemas/transcript.js +87 -0
- package/dist/schemas/usageNormalization.d.ts +55 -0
- package/dist/schemas/usageNormalization.js +41 -0
- package/dist/sessionTranscripts.d.ts +16 -0
- package/dist/sessionTranscripts.js +47 -0
- package/dist/tokenEstimation.d.ts +24 -0
- package/dist/tokenEstimation.js +62 -0
- package/dist/transcript.d.ts +98 -0
- package/dist/transcript.js +255 -0
- package/dist/types/observedSessionV1.d.ts +3 -1
- package/dist/types/observedSessionV1.js +14 -1
- package/dist/types/sessionEvent.d.ts +73 -7
- package/dist/usageNormalization.d.ts +69 -0
- package/dist/usageNormalization.js +100 -0
- package/package.json +1 -1
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.projectSessionTranscript = projectSessionTranscript;
|
|
4
|
+
const usageNormalization_1 = require("./usageNormalization");
|
|
5
|
+
const EMPTY_TOTALS = {
|
|
6
|
+
uncachedInputTokens: 0,
|
|
7
|
+
cacheReadTokens: 0,
|
|
8
|
+
cacheWriteTokens: 0,
|
|
9
|
+
outputTokens: 0,
|
|
10
|
+
reasoningTokens: 0,
|
|
11
|
+
cacheInclusiveInputTokens: 0,
|
|
12
|
+
billableOutputTokens: 0,
|
|
13
|
+
totalTokens: 0,
|
|
14
|
+
};
|
|
15
|
+
/** Project canonical provider events into a bounded or full-fidelity transcript. */
|
|
16
|
+
function projectSessionTranscript(events, options = {}) {
|
|
17
|
+
const fidelity = options.fidelity ?? 'snippet';
|
|
18
|
+
const requestedMax = options.maxContentChars ?? 2_000;
|
|
19
|
+
const maxContentChars = Number.isFinite(requestedMax) ? Math.max(0, requestedMax) : 2_000;
|
|
20
|
+
const messages = [];
|
|
21
|
+
const tools = [];
|
|
22
|
+
const commands = [];
|
|
23
|
+
const models = new Set();
|
|
24
|
+
const usageEvents = [];
|
|
25
|
+
const usageCosts = [];
|
|
26
|
+
const totals = { ...EMPTY_TOTALS };
|
|
27
|
+
const toolsById = new Map();
|
|
28
|
+
let cwd;
|
|
29
|
+
let gitBranch;
|
|
30
|
+
events.forEach((event, eventIndex) => {
|
|
31
|
+
if (nonEmpty(event.cwd))
|
|
32
|
+
cwd = event.cwd;
|
|
33
|
+
if (nonEmpty(event.gitBranch))
|
|
34
|
+
gitBranch = event.gitBranch;
|
|
35
|
+
const message = event.message;
|
|
36
|
+
const source = {
|
|
37
|
+
provider: options.provider ?? event.providerMetadata?.providerId,
|
|
38
|
+
sessionId: options.sessionId,
|
|
39
|
+
sourcePath: options.sourcePath,
|
|
40
|
+
source: event.providerMetadata?.source,
|
|
41
|
+
entrypoint: event.entrypoint,
|
|
42
|
+
isMeta: event.isMeta,
|
|
43
|
+
isSidechain: event.isSidechain,
|
|
44
|
+
originalRole: message?.role,
|
|
45
|
+
eventIndex,
|
|
46
|
+
eventType: event.type,
|
|
47
|
+
eventId: message?.id,
|
|
48
|
+
};
|
|
49
|
+
const usage = (0, usageNormalization_1.extractNormalizedUsage)(event) ?? undefined;
|
|
50
|
+
if (usage) {
|
|
51
|
+
usageEvents.push(usage);
|
|
52
|
+
addUsage(totals, usage);
|
|
53
|
+
}
|
|
54
|
+
const cost = usage
|
|
55
|
+
? (0, usageNormalization_1.calculateNormalizedUsageCost)({ usage, modelId: message?.model })
|
|
56
|
+
: undefined;
|
|
57
|
+
if (cost)
|
|
58
|
+
usageCosts.push(cost);
|
|
59
|
+
if (message?.model)
|
|
60
|
+
models.add(message.model);
|
|
61
|
+
const blocks = contentBlocks(message?.content, fidelity, maxContentChars, options.includeUnknownBlocks);
|
|
62
|
+
if (event.type === 'summary' && event.summary && blocks.length === 0) {
|
|
63
|
+
blocks.push({ type: 'text', ...boundedText(event.summary, fidelity, maxContentChars) });
|
|
64
|
+
}
|
|
65
|
+
if (event.type === 'tool_use') {
|
|
66
|
+
blocks.push({
|
|
67
|
+
type: 'tool_use',
|
|
68
|
+
toolName: event.tool.name,
|
|
69
|
+
input: boundedValue(event.tool.input, fidelity, maxContentChars),
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
if (event.type === 'tool_result') {
|
|
73
|
+
blocks.push({
|
|
74
|
+
type: 'tool_result',
|
|
75
|
+
toolUseId: event.result.tool_use_id,
|
|
76
|
+
output: boundedValue(event.result.output, fidelity, maxContentChars),
|
|
77
|
+
isError: event.result.is_error,
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
const messageTools = [];
|
|
81
|
+
for (const block of blocks) {
|
|
82
|
+
if (block.type === 'tool_use' && block.toolName) {
|
|
83
|
+
const command = block.command ?? commandFromTool(block.toolName, block.input);
|
|
84
|
+
const call = {
|
|
85
|
+
id: block.toolUseId,
|
|
86
|
+
name: block.toolName,
|
|
87
|
+
timestamp: event.timestamp,
|
|
88
|
+
input: block.input,
|
|
89
|
+
command,
|
|
90
|
+
source,
|
|
91
|
+
};
|
|
92
|
+
tools.push(call);
|
|
93
|
+
messageTools.push(call);
|
|
94
|
+
if (call.id)
|
|
95
|
+
toolsById.set(call.id, call);
|
|
96
|
+
if (command)
|
|
97
|
+
commands.push(command);
|
|
98
|
+
}
|
|
99
|
+
else if (block.type === 'tool_result' && block.toolUseId) {
|
|
100
|
+
const call = toolsById.get(block.toolUseId);
|
|
101
|
+
if (call) {
|
|
102
|
+
call.output = block.output;
|
|
103
|
+
call.isError = block.isError;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
if (blocks.length === 0 && !usage && event.type !== 'summary')
|
|
108
|
+
return;
|
|
109
|
+
messages.push({
|
|
110
|
+
id: message?.id,
|
|
111
|
+
role: roleForEvent(event),
|
|
112
|
+
timestamp: event.timestamp,
|
|
113
|
+
model: message?.model,
|
|
114
|
+
sourceLabel: message?.sourceLabel,
|
|
115
|
+
content: blocks,
|
|
116
|
+
text: blocks
|
|
117
|
+
.filter((block) => block.type === 'text' || block.type === 'thinking')
|
|
118
|
+
.map((block) => block.text ?? '')
|
|
119
|
+
.filter(Boolean)
|
|
120
|
+
.join('\n'),
|
|
121
|
+
usage,
|
|
122
|
+
cost,
|
|
123
|
+
tools: messageTools,
|
|
124
|
+
commands: messageTools.flatMap((tool) => (tool.command ? [tool.command] : [])),
|
|
125
|
+
source,
|
|
126
|
+
});
|
|
127
|
+
});
|
|
128
|
+
return {
|
|
129
|
+
provider: options.provider,
|
|
130
|
+
sessionId: options.sessionId,
|
|
131
|
+
sourcePath: options.sourcePath,
|
|
132
|
+
cwd,
|
|
133
|
+
gitBranch,
|
|
134
|
+
startedAt: events[0]?.timestamp,
|
|
135
|
+
endedAt: events.at(-1)?.timestamp,
|
|
136
|
+
models: [...models],
|
|
137
|
+
messages,
|
|
138
|
+
tools,
|
|
139
|
+
commands: [...new Set(commands)],
|
|
140
|
+
usage: {
|
|
141
|
+
events: usageEvents,
|
|
142
|
+
costs: usageCosts,
|
|
143
|
+
totals,
|
|
144
|
+
totalCostUsd: usageCosts.length > 0 && usageCosts.every((cost) => cost.costUsd !== null)
|
|
145
|
+
? usageCosts.reduce((sum, cost) => sum + (cost.costUsd ?? 0), 0)
|
|
146
|
+
: null,
|
|
147
|
+
},
|
|
148
|
+
provenance: { source: 'session-events', fidelity, eventCount: events.length },
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
function nonEmpty(value) {
|
|
152
|
+
return typeof value === 'string' && value.trim().length > 0;
|
|
153
|
+
}
|
|
154
|
+
function roleForEvent(event) {
|
|
155
|
+
if (event.type === 'summary')
|
|
156
|
+
return 'summary';
|
|
157
|
+
if (event.type === 'system')
|
|
158
|
+
return 'system';
|
|
159
|
+
if (event.type === 'tool_use' || event.type === 'tool_result')
|
|
160
|
+
return 'tool';
|
|
161
|
+
return event.type;
|
|
162
|
+
}
|
|
163
|
+
function contentBlocks(content, fidelity, maxChars, includeUnknown = false) {
|
|
164
|
+
if (typeof content === 'string')
|
|
165
|
+
return [{ type: 'text', ...boundedText(content, fidelity, maxChars) }];
|
|
166
|
+
if (!Array.isArray(content))
|
|
167
|
+
return [];
|
|
168
|
+
const result = [];
|
|
169
|
+
for (const raw of content) {
|
|
170
|
+
if (!raw || typeof raw !== 'object')
|
|
171
|
+
continue;
|
|
172
|
+
const block = raw;
|
|
173
|
+
if (block.type === 'text' && typeof block.text === 'string') {
|
|
174
|
+
result.push({ type: 'text', ...boundedText(block.text, fidelity, maxChars) });
|
|
175
|
+
}
|
|
176
|
+
else if ((block.type === 'thinking' || block.type === 'reasoning') &&
|
|
177
|
+
(typeof block.thinking === 'string' || typeof block.text === 'string')) {
|
|
178
|
+
result.push({
|
|
179
|
+
type: 'thinking',
|
|
180
|
+
...boundedText(String(block.thinking ?? block.text), fidelity, maxChars),
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
else if (block.type === 'tool_use' && typeof block.name === 'string') {
|
|
184
|
+
result.push({
|
|
185
|
+
type: 'tool_use',
|
|
186
|
+
toolName: block.name,
|
|
187
|
+
toolUseId: typeof block.id === 'string' ? block.id : undefined,
|
|
188
|
+
input: boundedValue(block.input, fidelity, maxChars),
|
|
189
|
+
command: commandFromTool(block.name, block.input),
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
else if (block.type === 'tool_result') {
|
|
193
|
+
result.push({
|
|
194
|
+
type: 'tool_result',
|
|
195
|
+
toolUseId: typeof block.tool_use_id === 'string' ? block.tool_use_id : undefined,
|
|
196
|
+
output: boundedValue(block.content, fidelity, maxChars),
|
|
197
|
+
isError: block.is_error === true,
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
else if (block.type === 'image') {
|
|
201
|
+
result.push({
|
|
202
|
+
type: 'image',
|
|
203
|
+
text: '[Image content]',
|
|
204
|
+
...(fidelity === 'full' ? { raw } : {}),
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
else if (includeUnknown) {
|
|
208
|
+
result.push({ type: 'unknown', raw: boundedValue(raw, fidelity, maxChars) });
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
return result;
|
|
212
|
+
}
|
|
213
|
+
function boundedText(text, fidelity, maxChars) {
|
|
214
|
+
if (fidelity === 'full' || text.length <= maxChars)
|
|
215
|
+
return { text, truncated: false };
|
|
216
|
+
return { text: text.slice(0, maxChars), truncated: true };
|
|
217
|
+
}
|
|
218
|
+
function boundedValue(value, fidelity, maxChars) {
|
|
219
|
+
if (fidelity === 'full' || value === undefined)
|
|
220
|
+
return value;
|
|
221
|
+
if (typeof value === 'string')
|
|
222
|
+
return boundedText(value, fidelity, maxChars).text;
|
|
223
|
+
try {
|
|
224
|
+
const json = JSON.stringify(value);
|
|
225
|
+
return json.length <= maxChars ? value : json.slice(0, maxChars);
|
|
226
|
+
}
|
|
227
|
+
catch {
|
|
228
|
+
return String(value).slice(0, maxChars);
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
function commandFromTool(name, input) {
|
|
232
|
+
if (!input || typeof input !== 'object')
|
|
233
|
+
return undefined;
|
|
234
|
+
const value = input;
|
|
235
|
+
if (!['bash', 'shell', 'execcommand', 'exec_command', 'local_shell'].includes(name.toLowerCase())) {
|
|
236
|
+
return undefined;
|
|
237
|
+
}
|
|
238
|
+
if (typeof value.command === 'string')
|
|
239
|
+
return value.command;
|
|
240
|
+
if (Array.isArray(value.command))
|
|
241
|
+
return value.command.map(String).join(' ');
|
|
242
|
+
if (typeof value.cmd === 'string')
|
|
243
|
+
return value.cmd;
|
|
244
|
+
return undefined;
|
|
245
|
+
}
|
|
246
|
+
function addUsage(total, usage) {
|
|
247
|
+
total.uncachedInputTokens += usage.uncachedInputTokens;
|
|
248
|
+
total.cacheReadTokens += usage.cacheReadTokens;
|
|
249
|
+
total.cacheWriteTokens += usage.cacheWriteTokens;
|
|
250
|
+
total.outputTokens += usage.outputTokens;
|
|
251
|
+
total.reasoningTokens += usage.reasoningTokens;
|
|
252
|
+
total.cacheInclusiveInputTokens += usage.cacheInclusiveInputTokens;
|
|
253
|
+
total.billableOutputTokens += usage.billableOutputTokens;
|
|
254
|
+
total.totalTokens += usage.totalTokens;
|
|
255
|
+
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { SessionEvent } from './sessionEvent';
|
|
2
2
|
import type { ProviderId, SessionProviderBase } from '../providers/types';
|
|
3
|
+
import type { CanonicalTranscriptUsageTotals } from '../transcript';
|
|
3
4
|
export type ObservationProvenanceV1 = 'reported' | 'estimated' | 'inferred';
|
|
4
5
|
export interface SessionEvidenceRefV1 {
|
|
5
6
|
schemaVersion: 1;
|
|
@@ -40,7 +41,8 @@ export interface ObservedAgentSessionV1 {
|
|
|
40
41
|
outputTokens: ObservedValueV1<number>;
|
|
41
42
|
cacheReadTokens: ObservedValueV1<number>;
|
|
42
43
|
cacheWriteTokens: ObservedValueV1<number>;
|
|
43
|
-
|
|
44
|
+
normalized?: ObservedValueV1<CanonicalTranscriptUsageTotals>;
|
|
45
|
+
costUsd: ObservedValueV1<number | null>;
|
|
44
46
|
};
|
|
45
47
|
pendingUserRequest: PendingUserRequestV1 | null;
|
|
46
48
|
observedAt: string;
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.derivePendingUserRequestV1 = derivePendingUserRequestV1;
|
|
4
4
|
exports.createProviderSessionAdapterV1 = createProviderSessionAdapterV1;
|
|
5
|
+
const transcript_1 = require("../transcript");
|
|
5
6
|
function evidence(provider, sessionId, sourcePath, event, eventIndex) {
|
|
6
7
|
return [
|
|
7
8
|
{
|
|
@@ -121,11 +122,18 @@ function createProviderSessionAdapterV1(sessionProvider, options = {}) {
|
|
|
121
122
|
async read(sessionPath, cwd = '') {
|
|
122
123
|
const stats = sessionProvider.readSessionStats(sessionPath);
|
|
123
124
|
const events = sessionProvider.createReader(sessionPath).readAll();
|
|
125
|
+
const transcript = (0, transcript_1.projectSessionTranscript)(events, {
|
|
126
|
+
provider: sessionProvider.id,
|
|
127
|
+
sessionId: stats.sessionId,
|
|
128
|
+
sourcePath: sessionPath,
|
|
129
|
+
});
|
|
124
130
|
const refs = evidence(sessionProvider.id, stats.sessionId, sessionPath);
|
|
125
131
|
const model = Object.entries(stats.modelUsage).sort((left, right) => right[1].calls - left[1].calls)[0]?.[0] ?? null;
|
|
126
132
|
const metadata = sessionProvider.getSessionMetadata?.(sessionPath);
|
|
127
133
|
const modifiedAt = metadata?.mtime.getTime() ?? Date.parse(stats.endTime);
|
|
128
134
|
const recent = Number.isFinite(modifiedAt) && Date.now() - modifiedAt < 2 * 60_000;
|
|
135
|
+
const allCostsReported = transcript.usage.costs.length > 0 &&
|
|
136
|
+
transcript.usage.costs.every((cost) => cost.source === 'provider-reported');
|
|
129
137
|
return {
|
|
130
138
|
schemaVersion: 1,
|
|
131
139
|
identity: {
|
|
@@ -141,7 +149,12 @@ function createProviderSessionAdapterV1(sessionProvider, options = {}) {
|
|
|
141
149
|
outputTokens: observed(stats.tokens.output, 'reported', 1, refs),
|
|
142
150
|
cacheReadTokens: observed(stats.tokens.cacheRead, 'reported', 1, refs),
|
|
143
151
|
cacheWriteTokens: observed(stats.tokens.cacheWrite, 'reported', 1, refs),
|
|
144
|
-
|
|
152
|
+
normalized: observed(transcript.usage.totals, 'reported', 1, refs),
|
|
153
|
+
costUsd: observed(transcript.usage.totalCostUsd, allCostsReported
|
|
154
|
+
? 'reported'
|
|
155
|
+
: transcript.usage.totalCostUsd === null
|
|
156
|
+
? 'inferred'
|
|
157
|
+
: 'estimated', allCostsReported ? 1 : transcript.usage.totalCostUsd === null ? 0.5 : 0.9, refs),
|
|
145
158
|
},
|
|
146
159
|
pendingUserRequest: derivePendingUserRequestV1(sessionProvider.id, stats.sessionId, sessionPath, events),
|
|
147
160
|
observedAt: new Date().toISOString(),
|
|
@@ -43,6 +43,8 @@ export interface SessionMessage {
|
|
|
43
43
|
model?: string;
|
|
44
44
|
/** Token usage statistics (only present in assistant messages) */
|
|
45
45
|
usage?: MessageUsage;
|
|
46
|
+
/** Authoritative disjoint usage projection, when normalized by a provider parser. */
|
|
47
|
+
normalizedUsage?: import('../usageNormalization').NormalizedUsage;
|
|
46
48
|
/** Message content (may be string or structured content) */
|
|
47
49
|
content?: unknown;
|
|
48
50
|
}
|
|
@@ -52,15 +54,19 @@ export interface SessionMessage {
|
|
|
52
54
|
* Events are normalized from provider-specific formats into this common shape.
|
|
53
55
|
* Each event represents a single interaction in a CLI agent session.
|
|
54
56
|
*/
|
|
55
|
-
export interface
|
|
56
|
-
/** Event type discriminator */
|
|
57
|
-
type: 'user' | 'assistant' | 'tool_use' | 'tool_result' | 'summary' | 'system';
|
|
58
|
-
/** Message data containing role, model, usage */
|
|
59
|
-
message: SessionMessage;
|
|
57
|
+
export interface SessionEventBase {
|
|
60
58
|
/** ISO 8601 timestamp of event */
|
|
61
59
|
timestamp: string;
|
|
60
|
+
/** Provider-reported entrypoint that produced this event. */
|
|
61
|
+
entrypoint?: string;
|
|
62
|
+
/** Whether the provider identifies this as orchestration or metadata. */
|
|
63
|
+
isMeta?: boolean;
|
|
62
64
|
/** Whether this is a subagent/sidechain event */
|
|
63
65
|
isSidechain?: boolean;
|
|
66
|
+
/** Provider-reported working directory for this event/session. */
|
|
67
|
+
cwd?: string;
|
|
68
|
+
/** Provider-reported git branch for this event/session. */
|
|
69
|
+
gitBranch?: string;
|
|
64
70
|
/** Permission mode active when this event occurred */
|
|
65
71
|
permissionMode?: 'default' | 'acceptEdits' | 'bypassPermissions' | 'plan';
|
|
66
72
|
/** Subscription rate limits, when provided by the session source. */
|
|
@@ -83,6 +89,8 @@ export interface SessionEvent {
|
|
|
83
89
|
};
|
|
84
90
|
/** Provider-specific signals normalized for shared analytics. */
|
|
85
91
|
providerMetadata?: {
|
|
92
|
+
providerId?: string;
|
|
93
|
+
source?: string;
|
|
86
94
|
retryAttempts?: number[];
|
|
87
95
|
finishReasons?: string[];
|
|
88
96
|
providerError?: {
|
|
@@ -91,18 +99,76 @@ export interface SessionEvent {
|
|
|
91
99
|
code?: string | number;
|
|
92
100
|
};
|
|
93
101
|
};
|
|
94
|
-
/**
|
|
102
|
+
/** Optional on the base so consumers can inspect legacy enriched events. */
|
|
95
103
|
tool?: {
|
|
96
104
|
name: string;
|
|
97
105
|
input: Record<string, unknown>;
|
|
98
106
|
};
|
|
99
|
-
/**
|
|
107
|
+
/** Optional on the base so consumers can inspect legacy enriched events. */
|
|
100
108
|
result?: {
|
|
101
109
|
tool_use_id: string;
|
|
102
110
|
output?: unknown;
|
|
103
111
|
is_error?: boolean;
|
|
104
112
|
};
|
|
105
113
|
}
|
|
114
|
+
export interface UserSessionEvent extends SessionEventBase {
|
|
115
|
+
type: 'user';
|
|
116
|
+
message: SessionMessage;
|
|
117
|
+
}
|
|
118
|
+
export interface AssistantSessionEvent extends SessionEventBase {
|
|
119
|
+
type: 'assistant';
|
|
120
|
+
message: SessionMessage;
|
|
121
|
+
}
|
|
122
|
+
export type MessageSessionEvent = UserSessionEvent | AssistantSessionEvent;
|
|
123
|
+
/** Providers must emit textual summary data, exact compaction counts, or both. */
|
|
124
|
+
export type SummarySessionEvent = SessionEventBase & {
|
|
125
|
+
type: 'summary';
|
|
126
|
+
} & ({
|
|
127
|
+
message: SessionMessage;
|
|
128
|
+
summary?: string;
|
|
129
|
+
compaction?: {
|
|
130
|
+
tokensBefore: number;
|
|
131
|
+
tokensAfter: number;
|
|
132
|
+
};
|
|
133
|
+
} | {
|
|
134
|
+
message?: SessionMessage;
|
|
135
|
+
summary: string;
|
|
136
|
+
compaction?: {
|
|
137
|
+
tokensBefore: number;
|
|
138
|
+
tokensAfter: number;
|
|
139
|
+
};
|
|
140
|
+
} | {
|
|
141
|
+
message?: SessionMessage;
|
|
142
|
+
summary?: string;
|
|
143
|
+
compaction: {
|
|
144
|
+
tokensBefore: number;
|
|
145
|
+
tokensAfter: number;
|
|
146
|
+
};
|
|
147
|
+
});
|
|
148
|
+
export interface SystemSessionEvent extends SessionEventBase {
|
|
149
|
+
type: 'system';
|
|
150
|
+
/** Bookkeeping-only system rows legitimately have no message. */
|
|
151
|
+
message?: SessionMessage;
|
|
152
|
+
}
|
|
153
|
+
export interface ToolUseSessionEvent extends SessionEventBase {
|
|
154
|
+
type: 'tool_use';
|
|
155
|
+
message?: SessionMessage;
|
|
156
|
+
tool: {
|
|
157
|
+
name: string;
|
|
158
|
+
input: Record<string, unknown>;
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
export interface ToolResultSessionEvent extends SessionEventBase {
|
|
162
|
+
type: 'tool_result';
|
|
163
|
+
message?: SessionMessage;
|
|
164
|
+
result: {
|
|
165
|
+
tool_use_id: string;
|
|
166
|
+
output?: unknown;
|
|
167
|
+
is_error?: boolean;
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
/** Canonical provider-neutral event contract. */
|
|
171
|
+
export type SessionEvent = UserSessionEvent | AssistantSessionEvent | SummarySessionEvent | SystemSessionEvent | ToolUseSessionEvent | ToolResultSessionEvent;
|
|
106
172
|
/**
|
|
107
173
|
* Permission mode values from Claude Code JSONL.
|
|
108
174
|
*/
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { type ModelPricing } from './modelInfo';
|
|
2
|
+
import type { SessionEvent } from './types/sessionEvent';
|
|
3
|
+
export type UsageProvider = 'anthropic' | 'openai' | 'sidekick' | (string & {});
|
|
4
|
+
export type UsageSemantics = 'anthropic-additive-cache' | 'openai-inclusive-cache' | 'sidekick-disjoint';
|
|
5
|
+
export interface UsageNormalizationProvenance {
|
|
6
|
+
provider?: UsageProvider;
|
|
7
|
+
semantics: UsageSemantics;
|
|
8
|
+
source?: string;
|
|
9
|
+
}
|
|
10
|
+
interface ProviderUsageBase {
|
|
11
|
+
provider?: UsageProvider;
|
|
12
|
+
source?: string;
|
|
13
|
+
model?: string;
|
|
14
|
+
reportedCostUsd?: number | null;
|
|
15
|
+
inputTokens?: number;
|
|
16
|
+
outputTokens?: number;
|
|
17
|
+
cacheReadTokens?: number;
|
|
18
|
+
cacheWriteTokens?: number;
|
|
19
|
+
reasoningTokens?: number;
|
|
20
|
+
}
|
|
21
|
+
/** Anthropic input and cache categories are additive. */
|
|
22
|
+
export interface AnthropicUsageInput extends ProviderUsageBase {
|
|
23
|
+
semantics: 'anthropic';
|
|
24
|
+
reasoningIncludedInOutput?: boolean;
|
|
25
|
+
}
|
|
26
|
+
/** OpenAI cached input is a subset of input and reasoning is normally a subset of output. */
|
|
27
|
+
export interface OpenAIUsageInput extends ProviderUsageBase {
|
|
28
|
+
semantics: 'openai';
|
|
29
|
+
reasoningIncludedInOutput?: boolean;
|
|
30
|
+
}
|
|
31
|
+
/** Already-disjoint usage. Reasoning inclusion must be stated explicitly. */
|
|
32
|
+
export interface SidekickUsageInput extends ProviderUsageBase {
|
|
33
|
+
semantics: 'sidekick';
|
|
34
|
+
reasoningIncludedInOutput: boolean;
|
|
35
|
+
}
|
|
36
|
+
export type ProviderUsageInput = AnthropicUsageInput | OpenAIUsageInput | SidekickUsageInput;
|
|
37
|
+
export interface NormalizedUsage {
|
|
38
|
+
uncachedInputTokens: number;
|
|
39
|
+
cacheReadTokens: number;
|
|
40
|
+
cacheWriteTokens: number;
|
|
41
|
+
outputTokens: number;
|
|
42
|
+
reasoningTokens: number;
|
|
43
|
+
reasoningIncludedInOutput: boolean;
|
|
44
|
+
cacheInclusiveInputTokens: number;
|
|
45
|
+
billableOutputTokens: number;
|
|
46
|
+
totalTokens: number;
|
|
47
|
+
model?: string;
|
|
48
|
+
reportedCostUsd?: number;
|
|
49
|
+
provenance: UsageNormalizationProvenance;
|
|
50
|
+
}
|
|
51
|
+
export type PricingProvenance = 'provider-reported' | 'explicit-pricing' | 'model-catalog' | 'unpriced';
|
|
52
|
+
export interface NormalizedUsageCostInput {
|
|
53
|
+
usage: NormalizedUsage;
|
|
54
|
+
modelId?: string;
|
|
55
|
+
pricing?: ModelPricing | null;
|
|
56
|
+
}
|
|
57
|
+
export interface NormalizedUsageCost {
|
|
58
|
+
costUsd: number | null;
|
|
59
|
+
source: PricingProvenance;
|
|
60
|
+
modelId?: string;
|
|
61
|
+
usage: NormalizedUsage;
|
|
62
|
+
}
|
|
63
|
+
/** Convert provider-specific counters into finite, nonnegative, disjoint categories. */
|
|
64
|
+
export declare function normalizeProviderUsage(input: ProviderUsageInput): NormalizedUsage;
|
|
65
|
+
/** Extract normalized usage from an event, preserving parser-owned semantics when present. */
|
|
66
|
+
export declare function extractNormalizedUsage(event: SessionEvent): NormalizedUsage | null;
|
|
67
|
+
/** Price normalized, disjoint usage without double-counting reasoning tokens. */
|
|
68
|
+
export declare function calculateNormalizedUsageCost(input: NormalizedUsageCostInput): NormalizedUsageCost;
|
|
69
|
+
export {};
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.normalizeProviderUsage = normalizeProviderUsage;
|
|
4
|
+
exports.extractNormalizedUsage = extractNormalizedUsage;
|
|
5
|
+
exports.calculateNormalizedUsageCost = calculateNormalizedUsageCost;
|
|
6
|
+
const modelInfo_1 = require("./modelInfo");
|
|
7
|
+
function tokenCount(value) {
|
|
8
|
+
return typeof value === 'number' && Number.isFinite(value) && value > 0
|
|
9
|
+
? Math.min(Number.MAX_SAFE_INTEGER, Math.floor(value))
|
|
10
|
+
: 0;
|
|
11
|
+
}
|
|
12
|
+
function reportedCost(value) {
|
|
13
|
+
return typeof value === 'number' && Number.isFinite(value) && value >= 0 ? value : undefined;
|
|
14
|
+
}
|
|
15
|
+
/** Convert provider-specific counters into finite, nonnegative, disjoint categories. */
|
|
16
|
+
function normalizeProviderUsage(input) {
|
|
17
|
+
const rawInput = tokenCount(input.inputTokens);
|
|
18
|
+
const cacheReadTokens = tokenCount(input.cacheReadTokens);
|
|
19
|
+
const cacheWriteTokens = tokenCount(input.cacheWriteTokens);
|
|
20
|
+
const outputTokens = tokenCount(input.outputTokens);
|
|
21
|
+
const reasoningTokens = tokenCount(input.reasoningTokens);
|
|
22
|
+
const isOpenAI = input.semantics === 'openai';
|
|
23
|
+
const uncachedInputTokens = isOpenAI ? Math.max(0, rawInput - cacheReadTokens) : rawInput;
|
|
24
|
+
const reasoningIncludedInOutput = input.reasoningIncludedInOutput ?? input.semantics === 'openai';
|
|
25
|
+
const cacheInclusiveInputTokens = uncachedInputTokens + cacheReadTokens + cacheWriteTokens;
|
|
26
|
+
const billableOutputTokens = outputTokens + (reasoningIncludedInOutput ? 0 : reasoningTokens);
|
|
27
|
+
const totalTokens = cacheInclusiveInputTokens + billableOutputTokens;
|
|
28
|
+
const semantics = isOpenAI
|
|
29
|
+
? 'openai-inclusive-cache'
|
|
30
|
+
: input.semantics === 'anthropic'
|
|
31
|
+
? 'anthropic-additive-cache'
|
|
32
|
+
: 'sidekick-disjoint';
|
|
33
|
+
return {
|
|
34
|
+
uncachedInputTokens,
|
|
35
|
+
cacheReadTokens,
|
|
36
|
+
cacheWriteTokens,
|
|
37
|
+
outputTokens,
|
|
38
|
+
reasoningTokens,
|
|
39
|
+
reasoningIncludedInOutput,
|
|
40
|
+
cacheInclusiveInputTokens,
|
|
41
|
+
billableOutputTokens,
|
|
42
|
+
totalTokens,
|
|
43
|
+
...(input.model ? { model: input.model } : {}),
|
|
44
|
+
...(reportedCost(input.reportedCostUsd) !== undefined
|
|
45
|
+
? { reportedCostUsd: reportedCost(input.reportedCostUsd) }
|
|
46
|
+
: {}),
|
|
47
|
+
provenance: {
|
|
48
|
+
semantics,
|
|
49
|
+
...(input.provider ? { provider: input.provider } : {}),
|
|
50
|
+
...(input.source ? { source: input.source } : {}),
|
|
51
|
+
},
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
/** Extract normalized usage from an event, preserving parser-owned semantics when present. */
|
|
55
|
+
function extractNormalizedUsage(event) {
|
|
56
|
+
if (event.message?.normalizedUsage)
|
|
57
|
+
return event.message.normalizedUsage;
|
|
58
|
+
const usage = event.message?.usage;
|
|
59
|
+
if (!usage)
|
|
60
|
+
return null;
|
|
61
|
+
const providerId = event.providerMetadata?.providerId;
|
|
62
|
+
return normalizeProviderUsage({
|
|
63
|
+
semantics: 'sidekick',
|
|
64
|
+
provider: providerId,
|
|
65
|
+
source: event.providerMetadata?.source ?? 'legacy-message-usage',
|
|
66
|
+
model: event.message?.model,
|
|
67
|
+
inputTokens: usage.input_tokens,
|
|
68
|
+
outputTokens: usage.output_tokens,
|
|
69
|
+
cacheReadTokens: usage.cache_read_input_tokens,
|
|
70
|
+
cacheWriteTokens: usage.cache_creation_input_tokens,
|
|
71
|
+
reasoningTokens: usage.reasoning_tokens,
|
|
72
|
+
reasoningIncludedInOutput: providerId === 'codex' || providerId === 'openai',
|
|
73
|
+
reportedCostUsd: usage.reported_cost,
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
/** Price normalized, disjoint usage without double-counting reasoning tokens. */
|
|
77
|
+
function calculateNormalizedUsageCost(input) {
|
|
78
|
+
const { usage } = input;
|
|
79
|
+
const modelId = input.modelId ?? usage.model;
|
|
80
|
+
if (usage.reportedCostUsd !== undefined) {
|
|
81
|
+
return { costUsd: usage.reportedCostUsd, source: 'provider-reported', modelId, usage };
|
|
82
|
+
}
|
|
83
|
+
const explicit = input.pricing !== undefined;
|
|
84
|
+
const pricing = explicit ? input.pricing : modelId ? (0, modelInfo_1.getModelPricing)(modelId) : null;
|
|
85
|
+
if (!pricing)
|
|
86
|
+
return { costUsd: null, source: 'unpriced', modelId, usage };
|
|
87
|
+
const costUsd = (usage.uncachedInputTokens / 1_000_000) * pricing.inputCostPerMillion +
|
|
88
|
+
(usage.cacheReadTokens / 1_000_000) * pricing.cacheReadCostPerMillion +
|
|
89
|
+
(usage.cacheWriteTokens / 1_000_000) * pricing.cacheWriteCostPerMillion +
|
|
90
|
+
(usage.billableOutputTokens / 1_000_000) * pricing.outputCostPerMillion;
|
|
91
|
+
if (!Number.isFinite(costUsd) || costUsd < 0) {
|
|
92
|
+
return { costUsd: null, source: 'unpriced', modelId, usage };
|
|
93
|
+
}
|
|
94
|
+
return {
|
|
95
|
+
costUsd,
|
|
96
|
+
source: explicit ? 'explicit-pricing' : 'model-catalog',
|
|
97
|
+
modelId,
|
|
98
|
+
usage,
|
|
99
|
+
};
|
|
100
|
+
}
|
package/package.json
CHANGED