sidekick-shared 0.23.1 → 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.
Files changed (47) hide show
  1. package/README.md +115 -37
  2. package/dist/aggregation/EventAggregator.d.ts +5 -1
  3. package/dist/aggregation/EventAggregator.js +64 -131
  4. package/dist/aggregation/types.d.ts +3 -0
  5. package/dist/browser.d.ts +6 -0
  6. package/dist/browser.js +10 -1
  7. package/dist/context/sessionContext.js +8 -6
  8. package/dist/extractors/tokenUsage.js +1 -1
  9. package/dist/index.d.ts +11 -1
  10. package/dist/index.js +23 -7
  11. package/dist/modelInfo.d.ts +17 -7
  12. package/dist/modelInfo.js +5 -2
  13. package/dist/node.d.ts +4 -0
  14. package/dist/node.js +8 -1
  15. package/dist/observedSessionCollector.d.ts +68 -0
  16. package/dist/observedSessionCollector.js +178 -0
  17. package/dist/parsers/codexParser.js +21 -6
  18. package/dist/parsers/openCodeParser.js +22 -1
  19. package/dist/parsers/subagentTraceParser.js +11 -5
  20. package/dist/providers/claudeCode.js +45 -1
  21. package/dist/providers/codex.js +17 -7
  22. package/dist/report/transcriptParser.d.ts +3 -11
  23. package/dist/report/transcriptParser.js +69 -154
  24. package/dist/schemas/index.d.ts +7 -1
  25. package/dist/schemas/index.js +18 -2
  26. package/dist/schemas/observedSessionV1.d.ts +32 -1
  27. package/dist/schemas/observedSessionV1.js +11 -1
  28. package/dist/schemas/sessionEvent.d.ts +353 -12
  29. package/dist/schemas/sessionEvent.js +76 -14
  30. package/dist/schemas/tokenEstimation.d.ts +14 -0
  31. package/dist/schemas/tokenEstimation.js +10 -0
  32. package/dist/schemas/transcript.d.ts +24 -0
  33. package/dist/schemas/transcript.js +81 -0
  34. package/dist/schemas/usageNormalization.d.ts +55 -0
  35. package/dist/schemas/usageNormalization.js +41 -0
  36. package/dist/sessionTranscripts.d.ts +16 -0
  37. package/dist/sessionTranscripts.js +47 -0
  38. package/dist/tokenEstimation.d.ts +24 -0
  39. package/dist/tokenEstimation.js +62 -0
  40. package/dist/transcript.d.ts +92 -0
  41. package/dist/transcript.js +240 -0
  42. package/dist/types/observedSessionV1.d.ts +3 -1
  43. package/dist/types/observedSessionV1.js +14 -1
  44. package/dist/types/sessionEvent.d.ts +65 -7
  45. package/dist/usageNormalization.d.ts +69 -0
  46. package/dist/usageNormalization.js +100 -0
  47. package/package.json +1 -1
@@ -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 SessionEvent {
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
- /** Tool use details (when type is 'tool_use') */
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
- /** Tool result details (when type is 'tool_result') */
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
  */
@@ -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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sidekick-shared",
3
- "version": "0.23.1",
3
+ "version": "0.24.0",
4
4
  "description": "Shared data access layer for Sidekick — readers, types, providers, credentials, quota",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",