sidekick-shared 0.23.0 → 0.24.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/README.md +139 -57
- package/dist/accountManager.js +10 -0
- package/dist/aggregation/EventAggregator.d.ts +8 -1
- package/dist/aggregation/EventAggregator.js +82 -136
- package/dist/aggregation/snapshot.d.ts +1 -1
- package/dist/aggregation/snapshot.js +2 -1
- package/dist/aggregation/types.d.ts +5 -0
- package/dist/autoSwitch.d.ts +1 -1
- package/dist/autoSwitch.js +14 -3
- package/dist/browser.d.ts +6 -0
- package/dist/browser.js +10 -1
- package/dist/codexQuota.d.ts +2 -0
- package/dist/codexQuota.js +27 -0
- package/dist/codexQuotaWatcher.d.ts +7 -0
- package/dist/codexQuotaWatcher.js +37 -19
- package/dist/context/sessionContext.js +43 -15
- package/dist/credentialIO.js +19 -5
- package/dist/errorHistory.d.ts +1 -0
- package/dist/errorHistory.js +3 -2
- package/dist/extractors/tokenUsage.js +1 -1
- package/dist/extractors/toolCall.js +2 -2
- package/dist/index.d.ts +14 -4
- package/dist/index.js +25 -7
- package/dist/modelInfo.d.ts +17 -7
- package/dist/modelInfo.js +11 -2
- package/dist/multiProviderQuotaService.js +13 -2
- package/dist/node.d.ts +4 -0
- package/dist/node.js +8 -1
- package/dist/observedSessionCollector.d.ts +68 -0
- package/dist/observedSessionCollector.js +178 -0
- package/dist/parsers/codexParser.js +24 -6
- package/dist/parsers/openCodeParser.js +22 -1
- package/dist/parsers/sessionActivityDetector.js +2 -0
- package/dist/parsers/sessionPathResolver.js +19 -16
- package/dist/parsers/subagentScanner.d.ts +1 -1
- package/dist/parsers/subagentScanner.js +4 -5
- package/dist/parsers/subagentTraceParser.js +15 -12
- package/dist/providerStatus.js +13 -2
- package/dist/providers/claudeCode.js +63 -5
- package/dist/providers/codex.js +56 -17
- package/dist/providers/detect.js +32 -3
- package/dist/providers/openCodeDatabase.js +12 -3
- package/dist/quota.js +8 -0
- package/dist/quotaPoller.js +17 -6
- package/dist/quotaSnapshots.js +80 -26
- package/dist/readers/decisions.js +3 -1
- package/dist/readers/handoff.js +1 -1
- package/dist/readers/notes.js +9 -4
- package/dist/readers/plans.js +2 -4
- package/dist/readers/tasks.js +3 -1
- package/dist/report/htmlHelpers.js +6 -1
- package/dist/report/openBrowser.js +3 -3
- 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 +18 -2
- package/dist/schemas/observedSessionV1.d.ts +32 -1
- package/dist/schemas/observedSessionV1.js +11 -1
- package/dist/schemas/sessionEvent.d.ts +353 -12
- package/dist/schemas/sessionEvent.js +76 -14
- package/dist/schemas/tokenEstimation.d.ts +14 -0
- package/dist/schemas/tokenEstimation.js +10 -0
- package/dist/schemas/transcript.d.ts +24 -0
- package/dist/schemas/transcript.js +81 -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/statusline/formatter.js +8 -1
- package/dist/terminalSync.js +16 -7
- package/dist/tokenEstimation.d.ts +24 -0
- package/dist/tokenEstimation.js +62 -0
- package/dist/transcript.d.ts +92 -0
- package/dist/transcript.js +240 -0
- package/dist/types/historicalData.d.ts +14 -0
- package/dist/types/historicalData.js +22 -0
- package/dist/types/observedSessionV1.d.ts +3 -1
- package/dist/types/observedSessionV1.js +20 -6
- package/dist/types/sessionEvent.d.ts +66 -7
- package/dist/usageNormalization.d.ts +69 -0
- package/dist/usageNormalization.js +100 -0
- package/dist/watchers/factory.js +14 -3
- package/dist/watchers/jsonlTail.js +34 -14
- package/dist/watchers/jsonlWatcher.js +5 -3
- package/dist/watchers/providerReaderWatcher.js +4 -2
- package/dist/watchers/sqliteWatcher.d.ts +2 -0
- package/dist/watchers/sqliteWatcher.js +27 -14
- package/dist/writers/atomic.js +100 -13
- package/dist/zaiQuotaWatcher.js +6 -0
- package/package.json +1 -1
|
@@ -51,6 +51,13 @@ export interface DailyData {
|
|
|
51
51
|
toolUsage: ToolUsageRecord[];
|
|
52
52
|
updatedAt: string;
|
|
53
53
|
}
|
|
54
|
+
export interface HourlyData {
|
|
55
|
+
hour: number;
|
|
56
|
+
tokens: TokenTotals;
|
|
57
|
+
totalCost: number;
|
|
58
|
+
messageCount: number;
|
|
59
|
+
sessionCount: number;
|
|
60
|
+
}
|
|
54
61
|
export interface MonthlyData {
|
|
55
62
|
month: string;
|
|
56
63
|
tokens: TokenTotals;
|
|
@@ -75,6 +82,8 @@ export interface AllTimeStats {
|
|
|
75
82
|
export interface HistoricalDataStore {
|
|
76
83
|
schemaVersion: number;
|
|
77
84
|
daily: Record<string, DailyData>;
|
|
85
|
+
/** Hourly buckets keyed by local YYYY-MM-DD. */
|
|
86
|
+
hourly?: Record<string, HourlyData[]>;
|
|
78
87
|
monthly: Record<string, MonthlyData>;
|
|
79
88
|
allTime: AllTimeStats;
|
|
80
89
|
lastSaved: string;
|
|
@@ -92,6 +101,10 @@ export interface SessionHistoryRecord {
|
|
|
92
101
|
tokens: TokenTotals;
|
|
93
102
|
totalCost: number;
|
|
94
103
|
messageCount: number;
|
|
104
|
+
/** Schema v3 additive fields used to make aggregate replacement idempotent. */
|
|
105
|
+
modelUsage?: ModelUsageRecord[];
|
|
106
|
+
toolUsage?: ToolUsageRecord[];
|
|
107
|
+
unpricedModelIds?: string[];
|
|
95
108
|
qualityScore: number;
|
|
96
109
|
qualityFactors: Array<{
|
|
97
110
|
id: string;
|
|
@@ -132,3 +145,4 @@ export interface SessionSummary {
|
|
|
132
145
|
costPerChangedLine?: number | null;
|
|
133
146
|
}
|
|
134
147
|
export declare function createEmptyTokenTotals(): TokenTotals;
|
|
148
|
+
export declare function createEmptyDataStore(): HistoricalDataStore;
|
|
@@ -18,6 +18,7 @@
|
|
|
18
18
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
19
19
|
exports.HISTORICAL_SESSION_RETENTION_LIMIT = exports.HISTORICAL_DATA_SCHEMA_VERSION = void 0;
|
|
20
20
|
exports.createEmptyTokenTotals = createEmptyTokenTotals;
|
|
21
|
+
exports.createEmptyDataStore = createEmptyDataStore;
|
|
21
22
|
exports.HISTORICAL_DATA_SCHEMA_VERSION = 3;
|
|
22
23
|
exports.HISTORICAL_SESSION_RETENTION_LIMIT = 500;
|
|
23
24
|
function createEmptyTokenTotals() {
|
|
@@ -28,3 +29,24 @@ function createEmptyTokenTotals() {
|
|
|
28
29
|
cacheReadTokens: 0,
|
|
29
30
|
};
|
|
30
31
|
}
|
|
32
|
+
function createEmptyDataStore() {
|
|
33
|
+
const now = new Date().toISOString();
|
|
34
|
+
return {
|
|
35
|
+
schemaVersion: exports.HISTORICAL_DATA_SCHEMA_VERSION,
|
|
36
|
+
daily: {},
|
|
37
|
+
monthly: {},
|
|
38
|
+
sessions: [],
|
|
39
|
+
allTime: {
|
|
40
|
+
tokens: createEmptyTokenTotals(),
|
|
41
|
+
totalCost: 0,
|
|
42
|
+
messageCount: 0,
|
|
43
|
+
sessionCount: 0,
|
|
44
|
+
firstDate: '',
|
|
45
|
+
lastDate: '',
|
|
46
|
+
modelUsage: [],
|
|
47
|
+
toolUsage: [],
|
|
48
|
+
updatedAt: now,
|
|
49
|
+
},
|
|
50
|
+
lastSaved: now,
|
|
51
|
+
};
|
|
52
|
+
}
|
|
@@ -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
|
{
|
|
@@ -10,7 +11,7 @@ function evidence(provider, sessionId, sourcePath, event, eventIndex) {
|
|
|
10
11
|
sessionId,
|
|
11
12
|
sourcePath,
|
|
12
13
|
eventIndex,
|
|
13
|
-
eventId: event?.message
|
|
14
|
+
eventId: event?.message?.id,
|
|
14
15
|
timestamp: event?.timestamp,
|
|
15
16
|
},
|
|
16
17
|
];
|
|
@@ -29,7 +30,7 @@ function derivePendingUserRequestV1(provider, sessionId, sourcePath, events) {
|
|
|
29
30
|
const refs = evidence(provider, sessionId, sourcePath, event, index);
|
|
30
31
|
return {
|
|
31
32
|
schemaVersion: 1,
|
|
32
|
-
id: `${provider}:${sessionId}:${request.id ?? event.message
|
|
33
|
+
id: `${provider}:${sessionId}:${request.id ?? event.message?.id ?? index}`,
|
|
33
34
|
provider,
|
|
34
35
|
sessionId,
|
|
35
36
|
kind: observed(request.kind, 'inferred', request.kind === 'question' ? 0.95 : 0.9, refs),
|
|
@@ -45,11 +46,12 @@ function derivePendingUserRequestV1(provider, sessionId, sourcePath, events) {
|
|
|
45
46
|
}
|
|
46
47
|
function waitingToolRequest(event) {
|
|
47
48
|
const calls = [];
|
|
49
|
+
const message = event.message;
|
|
48
50
|
if (event.type === 'tool_use') {
|
|
49
|
-
calls.push({ id:
|
|
51
|
+
calls.push({ id: message?.id, name: event.tool?.name, input: event.tool?.input });
|
|
50
52
|
}
|
|
51
|
-
if (Array.isArray(
|
|
52
|
-
for (const block of
|
|
53
|
+
if (Array.isArray(message?.content)) {
|
|
54
|
+
for (const block of message.content) {
|
|
53
55
|
if (!block || typeof block !== 'object')
|
|
54
56
|
continue;
|
|
55
57
|
const value = block;
|
|
@@ -120,11 +122,18 @@ function createProviderSessionAdapterV1(sessionProvider, options = {}) {
|
|
|
120
122
|
async read(sessionPath, cwd = '') {
|
|
121
123
|
const stats = sessionProvider.readSessionStats(sessionPath);
|
|
122
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
|
+
});
|
|
123
130
|
const refs = evidence(sessionProvider.id, stats.sessionId, sessionPath);
|
|
124
131
|
const model = Object.entries(stats.modelUsage).sort((left, right) => right[1].calls - left[1].calls)[0]?.[0] ?? null;
|
|
125
132
|
const metadata = sessionProvider.getSessionMetadata?.(sessionPath);
|
|
126
133
|
const modifiedAt = metadata?.mtime.getTime() ?? Date.parse(stats.endTime);
|
|
127
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');
|
|
128
137
|
return {
|
|
129
138
|
schemaVersion: 1,
|
|
130
139
|
identity: {
|
|
@@ -140,7 +149,12 @@ function createProviderSessionAdapterV1(sessionProvider, options = {}) {
|
|
|
140
149
|
outputTokens: observed(stats.tokens.output, 'reported', 1, refs),
|
|
141
150
|
cacheReadTokens: observed(stats.tokens.cacheRead, 'reported', 1, refs),
|
|
142
151
|
cacheWriteTokens: observed(stats.tokens.cacheWrite, 'reported', 1, refs),
|
|
143
|
-
|
|
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),
|
|
144
158
|
},
|
|
145
159
|
pendingUserRequest: derivePendingUserRequestV1(sessionProvider.id, stats.sessionId, sessionPath, events),
|
|
146
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,11 +54,7 @@ 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;
|
|
62
60
|
/** Whether this is a subagent/sidechain event */
|
|
@@ -83,6 +81,8 @@ export interface SessionEvent {
|
|
|
83
81
|
};
|
|
84
82
|
/** Provider-specific signals normalized for shared analytics. */
|
|
85
83
|
providerMetadata?: {
|
|
84
|
+
providerId?: string;
|
|
85
|
+
source?: string;
|
|
86
86
|
retryAttempts?: number[];
|
|
87
87
|
finishReasons?: string[];
|
|
88
88
|
providerError?: {
|
|
@@ -91,18 +91,76 @@ export interface SessionEvent {
|
|
|
91
91
|
code?: string | number;
|
|
92
92
|
};
|
|
93
93
|
};
|
|
94
|
-
/**
|
|
94
|
+
/** Optional on the base so consumers can inspect legacy enriched events. */
|
|
95
95
|
tool?: {
|
|
96
96
|
name: string;
|
|
97
97
|
input: Record<string, unknown>;
|
|
98
98
|
};
|
|
99
|
-
/**
|
|
99
|
+
/** Optional on the base so consumers can inspect legacy enriched events. */
|
|
100
100
|
result?: {
|
|
101
101
|
tool_use_id: string;
|
|
102
102
|
output?: unknown;
|
|
103
103
|
is_error?: boolean;
|
|
104
104
|
};
|
|
105
105
|
}
|
|
106
|
+
export interface UserSessionEvent extends SessionEventBase {
|
|
107
|
+
type: 'user';
|
|
108
|
+
message: SessionMessage;
|
|
109
|
+
}
|
|
110
|
+
export interface AssistantSessionEvent extends SessionEventBase {
|
|
111
|
+
type: 'assistant';
|
|
112
|
+
message: SessionMessage;
|
|
113
|
+
}
|
|
114
|
+
export type MessageSessionEvent = UserSessionEvent | AssistantSessionEvent;
|
|
115
|
+
/** Providers must emit textual summary data, exact compaction counts, or both. */
|
|
116
|
+
export type SummarySessionEvent = SessionEventBase & {
|
|
117
|
+
type: 'summary';
|
|
118
|
+
} & ({
|
|
119
|
+
message: SessionMessage;
|
|
120
|
+
summary?: string;
|
|
121
|
+
compaction?: {
|
|
122
|
+
tokensBefore: number;
|
|
123
|
+
tokensAfter: number;
|
|
124
|
+
};
|
|
125
|
+
} | {
|
|
126
|
+
message?: SessionMessage;
|
|
127
|
+
summary: string;
|
|
128
|
+
compaction?: {
|
|
129
|
+
tokensBefore: number;
|
|
130
|
+
tokensAfter: number;
|
|
131
|
+
};
|
|
132
|
+
} | {
|
|
133
|
+
message?: SessionMessage;
|
|
134
|
+
summary?: string;
|
|
135
|
+
compaction: {
|
|
136
|
+
tokensBefore: number;
|
|
137
|
+
tokensAfter: number;
|
|
138
|
+
};
|
|
139
|
+
});
|
|
140
|
+
export interface SystemSessionEvent extends SessionEventBase {
|
|
141
|
+
type: 'system';
|
|
142
|
+
/** Bookkeeping-only system rows legitimately have no message. */
|
|
143
|
+
message?: SessionMessage;
|
|
144
|
+
}
|
|
145
|
+
export interface ToolUseSessionEvent extends SessionEventBase {
|
|
146
|
+
type: 'tool_use';
|
|
147
|
+
message?: SessionMessage;
|
|
148
|
+
tool: {
|
|
149
|
+
name: string;
|
|
150
|
+
input: Record<string, unknown>;
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
export interface ToolResultSessionEvent extends SessionEventBase {
|
|
154
|
+
type: 'tool_result';
|
|
155
|
+
message?: SessionMessage;
|
|
156
|
+
result: {
|
|
157
|
+
tool_use_id: string;
|
|
158
|
+
output?: unknown;
|
|
159
|
+
is_error?: boolean;
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
/** Canonical provider-neutral event contract. */
|
|
163
|
+
export type SessionEvent = UserSessionEvent | AssistantSessionEvent | SummarySessionEvent | SystemSessionEvent | ToolUseSessionEvent | ToolResultSessionEvent;
|
|
106
164
|
/**
|
|
107
165
|
* Permission mode values from Claude Code JSONL.
|
|
108
166
|
*/
|
|
@@ -221,6 +279,7 @@ export interface TimelineEvent {
|
|
|
221
279
|
metadata?: {
|
|
222
280
|
model?: string;
|
|
223
281
|
toolName?: string;
|
|
282
|
+
toolUseId?: string;
|
|
224
283
|
isError?: boolean;
|
|
225
284
|
tokenCount?: number;
|
|
226
285
|
fullText?: string;
|
|
@@ -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/dist/watchers/factory.js
CHANGED
|
@@ -56,14 +56,25 @@ function createWatcher(options) {
|
|
|
56
56
|
}
|
|
57
57
|
let sessionPath;
|
|
58
58
|
if (sessionId) {
|
|
59
|
-
const
|
|
60
|
-
|
|
59
|
+
const matches = sessions.map((sessionPath) => ({
|
|
60
|
+
sessionPath,
|
|
61
|
+
id: provider.getSessionId(sessionPath),
|
|
62
|
+
}));
|
|
63
|
+
const exact = matches.filter((candidate) => candidate.id === sessionId);
|
|
64
|
+
const candidates = exact.length > 0 ? exact : matches.filter((candidate) => candidate.id.startsWith(sessionId));
|
|
65
|
+
if (candidates.length === 0) {
|
|
61
66
|
throw new Error(`Session ${sessionId} not found. Available: ${sessions
|
|
62
67
|
.slice(0, 5)
|
|
63
68
|
.map((s) => path.basename(s))
|
|
64
69
|
.join(', ')}`);
|
|
65
70
|
}
|
|
66
|
-
|
|
71
|
+
if (candidates.length > 1) {
|
|
72
|
+
throw new Error(`Session ${sessionId} is ambiguous. Matches: ${candidates
|
|
73
|
+
.slice(0, 5)
|
|
74
|
+
.map((candidate) => candidate.id)
|
|
75
|
+
.join(', ')}`);
|
|
76
|
+
}
|
|
77
|
+
sessionPath = candidates[0].sessionPath;
|
|
67
78
|
}
|
|
68
79
|
else {
|
|
69
80
|
sessionPath = sessions[0]; // most recent
|
|
@@ -48,7 +48,11 @@ function createJsonlTail(options) {
|
|
|
48
48
|
class JsonlTailReader {
|
|
49
49
|
options;
|
|
50
50
|
active = false;
|
|
51
|
-
|
|
51
|
+
/** Physical byte position already read from the file. */
|
|
52
|
+
readOffset;
|
|
53
|
+
/** Resume-safe byte position ending immediately after a newline. */
|
|
54
|
+
committedOffset;
|
|
55
|
+
pendingBytes = Buffer.alloc(0);
|
|
52
56
|
fsWatcher = null;
|
|
53
57
|
debounceTimer = null;
|
|
54
58
|
catchupTimer = null;
|
|
@@ -56,7 +60,8 @@ class JsonlTailReader {
|
|
|
56
60
|
parser;
|
|
57
61
|
constructor(options) {
|
|
58
62
|
this.options = options;
|
|
59
|
-
this.
|
|
63
|
+
this.readOffset = options.startOffset ?? 0;
|
|
64
|
+
this.committedOffset = this.readOffset;
|
|
60
65
|
this.parser = new jsonl_1.JsonlParser({
|
|
61
66
|
onEvent: (event) => {
|
|
62
67
|
this.eventsInCurrentBatch += 1;
|
|
@@ -74,11 +79,12 @@ class JsonlTailReader {
|
|
|
74
79
|
this.active = true;
|
|
75
80
|
if (this.options.startAtEnd && this.options.startOffset === undefined) {
|
|
76
81
|
try {
|
|
77
|
-
this.
|
|
82
|
+
this.readOffset = fs.statSync(this.options.path).size;
|
|
78
83
|
}
|
|
79
84
|
catch {
|
|
80
|
-
this.
|
|
85
|
+
this.readOffset = 0;
|
|
81
86
|
}
|
|
87
|
+
this.committedOffset = this.readOffset;
|
|
82
88
|
}
|
|
83
89
|
this.readNow();
|
|
84
90
|
this.watchFile();
|
|
@@ -112,27 +118,39 @@ class JsonlTailReader {
|
|
|
112
118
|
let fd = null;
|
|
113
119
|
try {
|
|
114
120
|
const stat = fs.statSync(this.options.path);
|
|
115
|
-
if (stat.size < this.
|
|
116
|
-
this.
|
|
121
|
+
if (stat.size < this.readOffset) {
|
|
122
|
+
this.readOffset = 0;
|
|
123
|
+
this.committedOffset = 0;
|
|
124
|
+
this.pendingBytes = Buffer.alloc(0);
|
|
117
125
|
this.parser.reset();
|
|
118
126
|
}
|
|
119
|
-
if (stat.size <= this.
|
|
127
|
+
if (stat.size <= this.readOffset)
|
|
120
128
|
return;
|
|
121
|
-
const bytesToRead = stat.size - this.
|
|
129
|
+
const bytesToRead = stat.size - this.readOffset;
|
|
122
130
|
const buffer = Buffer.alloc(bytesToRead);
|
|
123
131
|
fd = fs.openSync(this.options.path, 'r');
|
|
124
|
-
const bytesRead = fs.readSync(fd, buffer, 0, bytesToRead, this.
|
|
132
|
+
const bytesRead = fs.readSync(fd, buffer, 0, bytesToRead, this.readOffset);
|
|
125
133
|
fs.closeSync(fd);
|
|
126
134
|
fd = null;
|
|
127
135
|
if (bytesRead <= 0)
|
|
128
136
|
return;
|
|
129
|
-
this.
|
|
137
|
+
this.readOffset += bytesRead;
|
|
130
138
|
this.eventsInCurrentBatch = 0;
|
|
131
|
-
this.
|
|
139
|
+
const combined = Buffer.concat([this.pendingBytes, buffer.subarray(0, bytesRead)]);
|
|
140
|
+
const lastNewline = combined.lastIndexOf(0x0a);
|
|
141
|
+
if (lastNewline >= 0) {
|
|
142
|
+
const completeLines = combined.subarray(0, lastNewline + 1);
|
|
143
|
+
this.pendingBytes = Buffer.from(combined.subarray(lastNewline + 1));
|
|
144
|
+
this.parser.processChunk(completeLines.toString('utf8'));
|
|
145
|
+
}
|
|
146
|
+
else {
|
|
147
|
+
this.pendingBytes = combined;
|
|
148
|
+
}
|
|
149
|
+
this.committedOffset = this.readOffset - this.pendingBytes.length;
|
|
132
150
|
this.options.onBatchComplete?.({
|
|
133
151
|
bytesRead,
|
|
134
152
|
eventsRead: this.eventsInCurrentBatch,
|
|
135
|
-
offset: this.
|
|
153
|
+
offset: this.committedOffset,
|
|
136
154
|
});
|
|
137
155
|
}
|
|
138
156
|
catch (error) {
|
|
@@ -148,10 +166,12 @@ class JsonlTailReader {
|
|
|
148
166
|
}
|
|
149
167
|
}
|
|
150
168
|
getOffset() {
|
|
151
|
-
return this.
|
|
169
|
+
return this.committedOffset;
|
|
152
170
|
}
|
|
153
171
|
seekTo(offset) {
|
|
154
|
-
this.
|
|
172
|
+
this.readOffset = Math.max(0, offset);
|
|
173
|
+
this.committedOffset = this.readOffset;
|
|
174
|
+
this.pendingBytes = Buffer.alloc(0);
|
|
155
175
|
this.parser.reset();
|
|
156
176
|
}
|
|
157
177
|
watchFile() {
|
|
@@ -274,9 +274,11 @@ class JsonlSessionWatcher {
|
|
|
274
274
|
this.fsWatcher = fs.watch(this.sessionPath, { persistent: false }, () => {
|
|
275
275
|
this.debouncedRead();
|
|
276
276
|
});
|
|
277
|
-
this.fsWatcher.on('error', () => {
|
|
278
|
-
//
|
|
279
|
-
this.
|
|
277
|
+
this.fsWatcher.on('error', (error) => {
|
|
278
|
+
// Degrade to catch-up polling without killing the live session.
|
|
279
|
+
this.fsWatcher?.close();
|
|
280
|
+
this.fsWatcher = null;
|
|
281
|
+
this.callbacks.onError?.(error);
|
|
280
282
|
});
|
|
281
283
|
}
|
|
282
284
|
catch {
|
|
@@ -82,8 +82,10 @@ class ProviderReaderSessionWatcher {
|
|
|
82
82
|
this.fsWatcher = fs.watch(this.sessionPath, { persistent: false }, () => {
|
|
83
83
|
this.debouncedRead();
|
|
84
84
|
});
|
|
85
|
-
this.fsWatcher.on('error', () => {
|
|
86
|
-
this.
|
|
85
|
+
this.fsWatcher.on('error', (error) => {
|
|
86
|
+
this.fsWatcher?.close();
|
|
87
|
+
this.fsWatcher = null;
|
|
88
|
+
this.callbacks.onError?.(error);
|
|
87
89
|
});
|
|
88
90
|
}
|
|
89
91
|
catch {
|
|
@@ -7,6 +7,8 @@ export declare class SqliteSessionWatcher implements SessionWatcher {
|
|
|
7
7
|
private _isActive;
|
|
8
8
|
private lastMessageTime;
|
|
9
9
|
private lastPartTime;
|
|
10
|
+
private seenMessageVersions;
|
|
11
|
+
private seenPartVersions;
|
|
10
12
|
private fsWatcher;
|
|
11
13
|
private walWatcher;
|
|
12
14
|
private pollTimer;
|