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,87 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.canonicalSessionTranscriptSchema = exports.canonicalTranscriptMessageSchema = exports.canonicalToolCallSchema = exports.canonicalTranscriptBlockSchema = exports.transcriptSourceProvenanceSchema = void 0;
|
|
4
|
+
const zod_1 = require("zod");
|
|
5
|
+
const usageNormalization_1 = require("./usageNormalization");
|
|
6
|
+
exports.transcriptSourceProvenanceSchema = zod_1.z.object({
|
|
7
|
+
provider: zod_1.z.string().optional(),
|
|
8
|
+
sessionId: zod_1.z.string().optional(),
|
|
9
|
+
sourcePath: zod_1.z.string().optional(),
|
|
10
|
+
source: zod_1.z.string().optional(),
|
|
11
|
+
entrypoint: zod_1.z.string().optional(),
|
|
12
|
+
isMeta: zod_1.z.boolean().optional(),
|
|
13
|
+
isSidechain: zod_1.z.boolean().optional(),
|
|
14
|
+
originalRole: zod_1.z.string().optional(),
|
|
15
|
+
eventIndex: zod_1.z.number().int().nonnegative(),
|
|
16
|
+
eventType: zod_1.z.enum(['user', 'assistant', 'tool_use', 'tool_result', 'summary', 'system']),
|
|
17
|
+
eventId: zod_1.z.string().optional(),
|
|
18
|
+
});
|
|
19
|
+
exports.canonicalTranscriptBlockSchema = zod_1.z.object({
|
|
20
|
+
type: zod_1.z.enum(['text', 'thinking', 'tool_use', 'tool_result', 'image', 'unknown']),
|
|
21
|
+
text: zod_1.z.string().optional(),
|
|
22
|
+
toolName: zod_1.z.string().optional(),
|
|
23
|
+
toolUseId: zod_1.z.string().optional(),
|
|
24
|
+
input: zod_1.z.unknown().optional(),
|
|
25
|
+
output: zod_1.z.unknown().optional(),
|
|
26
|
+
isError: zod_1.z.boolean().optional(),
|
|
27
|
+
command: zod_1.z.string().optional(),
|
|
28
|
+
truncated: zod_1.z.boolean().optional(),
|
|
29
|
+
raw: zod_1.z.unknown().optional(),
|
|
30
|
+
});
|
|
31
|
+
exports.canonicalToolCallSchema = zod_1.z.object({
|
|
32
|
+
id: zod_1.z.string().optional(),
|
|
33
|
+
name: zod_1.z.string(),
|
|
34
|
+
timestamp: zod_1.z.string(),
|
|
35
|
+
input: zod_1.z.unknown().optional(),
|
|
36
|
+
output: zod_1.z.unknown().optional(),
|
|
37
|
+
isError: zod_1.z.boolean().optional(),
|
|
38
|
+
command: zod_1.z.string().optional(),
|
|
39
|
+
source: exports.transcriptSourceProvenanceSchema,
|
|
40
|
+
});
|
|
41
|
+
exports.canonicalTranscriptMessageSchema = zod_1.z.object({
|
|
42
|
+
id: zod_1.z.string().optional(),
|
|
43
|
+
role: zod_1.z.enum(['user', 'assistant', 'system', 'summary', 'tool']),
|
|
44
|
+
timestamp: zod_1.z.string(),
|
|
45
|
+
model: zod_1.z.string().optional(),
|
|
46
|
+
sourceLabel: zod_1.z.string().optional(),
|
|
47
|
+
content: zod_1.z.array(exports.canonicalTranscriptBlockSchema),
|
|
48
|
+
text: zod_1.z.string(),
|
|
49
|
+
usage: usageNormalization_1.normalizedUsageSchema.optional(),
|
|
50
|
+
cost: usageNormalization_1.normalizedUsageCostSchema.optional(),
|
|
51
|
+
tools: zod_1.z.array(exports.canonicalToolCallSchema),
|
|
52
|
+
commands: zod_1.z.array(zod_1.z.string()),
|
|
53
|
+
source: exports.transcriptSourceProvenanceSchema,
|
|
54
|
+
});
|
|
55
|
+
exports.canonicalSessionTranscriptSchema = zod_1.z.object({
|
|
56
|
+
provider: zod_1.z.string().optional(),
|
|
57
|
+
sessionId: zod_1.z.string().optional(),
|
|
58
|
+
sourcePath: zod_1.z.string().optional(),
|
|
59
|
+
cwd: zod_1.z.string().optional(),
|
|
60
|
+
gitBranch: zod_1.z.string().optional(),
|
|
61
|
+
startedAt: zod_1.z.string().optional(),
|
|
62
|
+
endedAt: zod_1.z.string().optional(),
|
|
63
|
+
models: zod_1.z.array(zod_1.z.string()),
|
|
64
|
+
messages: zod_1.z.array(exports.canonicalTranscriptMessageSchema),
|
|
65
|
+
tools: zod_1.z.array(exports.canonicalToolCallSchema),
|
|
66
|
+
commands: zod_1.z.array(zod_1.z.string()),
|
|
67
|
+
usage: zod_1.z.object({
|
|
68
|
+
events: zod_1.z.array(usageNormalization_1.normalizedUsageSchema),
|
|
69
|
+
costs: zod_1.z.array(usageNormalization_1.normalizedUsageCostSchema),
|
|
70
|
+
totals: zod_1.z.object({
|
|
71
|
+
uncachedInputTokens: zod_1.z.number().finite().nonnegative(),
|
|
72
|
+
cacheReadTokens: zod_1.z.number().finite().nonnegative(),
|
|
73
|
+
cacheWriteTokens: zod_1.z.number().finite().nonnegative(),
|
|
74
|
+
outputTokens: zod_1.z.number().finite().nonnegative(),
|
|
75
|
+
reasoningTokens: zod_1.z.number().finite().nonnegative(),
|
|
76
|
+
cacheInclusiveInputTokens: zod_1.z.number().finite().nonnegative(),
|
|
77
|
+
billableOutputTokens: zod_1.z.number().finite().nonnegative(),
|
|
78
|
+
totalTokens: zod_1.z.number().finite().nonnegative(),
|
|
79
|
+
}),
|
|
80
|
+
totalCostUsd: zod_1.z.number().finite().nonnegative().nullable(),
|
|
81
|
+
}),
|
|
82
|
+
provenance: zod_1.z.object({
|
|
83
|
+
source: zod_1.z.literal('session-events'),
|
|
84
|
+
fidelity: zod_1.z.enum(['snippet', 'full']),
|
|
85
|
+
eventCount: zod_1.z.number().int().nonnegative(),
|
|
86
|
+
}),
|
|
87
|
+
});
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
export declare const normalizedUsageSchema: z.ZodObject<{
|
|
3
|
+
uncachedInputTokens: z.ZodNumber;
|
|
4
|
+
cacheReadTokens: z.ZodNumber;
|
|
5
|
+
cacheWriteTokens: z.ZodNumber;
|
|
6
|
+
outputTokens: z.ZodNumber;
|
|
7
|
+
reasoningTokens: z.ZodNumber;
|
|
8
|
+
reasoningIncludedInOutput: z.ZodBoolean;
|
|
9
|
+
cacheInclusiveInputTokens: z.ZodNumber;
|
|
10
|
+
billableOutputTokens: z.ZodNumber;
|
|
11
|
+
totalTokens: z.ZodNumber;
|
|
12
|
+
model: z.ZodOptional<z.ZodString>;
|
|
13
|
+
reportedCostUsd: z.ZodOptional<z.ZodNumber>;
|
|
14
|
+
provenance: z.ZodObject<{
|
|
15
|
+
provider: z.ZodOptional<z.ZodString>;
|
|
16
|
+
semantics: z.ZodEnum<{
|
|
17
|
+
"anthropic-additive-cache": "anthropic-additive-cache";
|
|
18
|
+
"openai-inclusive-cache": "openai-inclusive-cache";
|
|
19
|
+
"sidekick-disjoint": "sidekick-disjoint";
|
|
20
|
+
}>;
|
|
21
|
+
source: z.ZodOptional<z.ZodString>;
|
|
22
|
+
}, z.core.$strip>;
|
|
23
|
+
}, z.core.$strip>;
|
|
24
|
+
export declare const normalizedUsageCostSchema: z.ZodObject<{
|
|
25
|
+
costUsd: z.ZodNullable<z.ZodNumber>;
|
|
26
|
+
source: z.ZodEnum<{
|
|
27
|
+
unpriced: "unpriced";
|
|
28
|
+
"provider-reported": "provider-reported";
|
|
29
|
+
"explicit-pricing": "explicit-pricing";
|
|
30
|
+
"model-catalog": "model-catalog";
|
|
31
|
+
}>;
|
|
32
|
+
modelId: z.ZodOptional<z.ZodString>;
|
|
33
|
+
usage: z.ZodObject<{
|
|
34
|
+
uncachedInputTokens: z.ZodNumber;
|
|
35
|
+
cacheReadTokens: z.ZodNumber;
|
|
36
|
+
cacheWriteTokens: z.ZodNumber;
|
|
37
|
+
outputTokens: z.ZodNumber;
|
|
38
|
+
reasoningTokens: z.ZodNumber;
|
|
39
|
+
reasoningIncludedInOutput: z.ZodBoolean;
|
|
40
|
+
cacheInclusiveInputTokens: z.ZodNumber;
|
|
41
|
+
billableOutputTokens: z.ZodNumber;
|
|
42
|
+
totalTokens: z.ZodNumber;
|
|
43
|
+
model: z.ZodOptional<z.ZodString>;
|
|
44
|
+
reportedCostUsd: z.ZodOptional<z.ZodNumber>;
|
|
45
|
+
provenance: z.ZodObject<{
|
|
46
|
+
provider: z.ZodOptional<z.ZodString>;
|
|
47
|
+
semantics: z.ZodEnum<{
|
|
48
|
+
"anthropic-additive-cache": "anthropic-additive-cache";
|
|
49
|
+
"openai-inclusive-cache": "openai-inclusive-cache";
|
|
50
|
+
"sidekick-disjoint": "sidekick-disjoint";
|
|
51
|
+
}>;
|
|
52
|
+
source: z.ZodOptional<z.ZodString>;
|
|
53
|
+
}, z.core.$strip>;
|
|
54
|
+
}, z.core.$strip>;
|
|
55
|
+
}, z.core.$strip>;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.normalizedUsageCostSchema = exports.normalizedUsageSchema = void 0;
|
|
4
|
+
const zod_1 = require("zod");
|
|
5
|
+
const normalizedUsageObjectSchema = zod_1.z.object({
|
|
6
|
+
uncachedInputTokens: zod_1.z.number().finite().nonnegative(),
|
|
7
|
+
cacheReadTokens: zod_1.z.number().finite().nonnegative(),
|
|
8
|
+
cacheWriteTokens: zod_1.z.number().finite().nonnegative(),
|
|
9
|
+
outputTokens: zod_1.z.number().finite().nonnegative(),
|
|
10
|
+
reasoningTokens: zod_1.z.number().finite().nonnegative(),
|
|
11
|
+
reasoningIncludedInOutput: zod_1.z.boolean(),
|
|
12
|
+
cacheInclusiveInputTokens: zod_1.z.number().finite().nonnegative(),
|
|
13
|
+
billableOutputTokens: zod_1.z.number().finite().nonnegative(),
|
|
14
|
+
totalTokens: zod_1.z.number().finite().nonnegative(),
|
|
15
|
+
model: zod_1.z.string().optional(),
|
|
16
|
+
reportedCostUsd: zod_1.z.number().finite().nonnegative().optional(),
|
|
17
|
+
provenance: zod_1.z.object({
|
|
18
|
+
provider: zod_1.z.string().optional(),
|
|
19
|
+
semantics: zod_1.z.enum(['anthropic-additive-cache', 'openai-inclusive-cache', 'sidekick-disjoint']),
|
|
20
|
+
source: zod_1.z.string().optional(),
|
|
21
|
+
}),
|
|
22
|
+
});
|
|
23
|
+
exports.normalizedUsageSchema = normalizedUsageObjectSchema.superRefine((usage, context) => {
|
|
24
|
+
const expectedInput = usage.uncachedInputTokens + usage.cacheReadTokens + usage.cacheWriteTokens;
|
|
25
|
+
const expectedOutput = usage.outputTokens + (usage.reasoningIncludedInOutput ? 0 : usage.reasoningTokens);
|
|
26
|
+
if (usage.cacheInclusiveInputTokens !== expectedInput) {
|
|
27
|
+
context.addIssue({ code: 'custom', message: 'cacheInclusiveInputTokens is inconsistent' });
|
|
28
|
+
}
|
|
29
|
+
if (usage.billableOutputTokens !== expectedOutput) {
|
|
30
|
+
context.addIssue({ code: 'custom', message: 'billableOutputTokens is inconsistent' });
|
|
31
|
+
}
|
|
32
|
+
if (usage.totalTokens !== expectedInput + expectedOutput) {
|
|
33
|
+
context.addIssue({ code: 'custom', message: 'totalTokens is inconsistent' });
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
exports.normalizedUsageCostSchema = zod_1.z.object({
|
|
37
|
+
costUsd: zod_1.z.number().finite().nonnegative().nullable(),
|
|
38
|
+
source: zod_1.z.enum(['provider-reported', 'explicit-pricing', 'model-catalog', 'unpriced']),
|
|
39
|
+
modelId: zod_1.z.string().optional(),
|
|
40
|
+
usage: exports.normalizedUsageSchema,
|
|
41
|
+
});
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { SessionProviderBase, ProviderId } from './providers/types';
|
|
2
|
+
import { type CanonicalSessionTranscript, type ProjectSessionTranscriptOptions } from './transcript';
|
|
3
|
+
export interface ProviderSessionIndex {
|
|
4
|
+
provider: ProviderId;
|
|
5
|
+
sessionId: string;
|
|
6
|
+
sessionPath: string;
|
|
7
|
+
label: string | null;
|
|
8
|
+
modifiedAt: string;
|
|
9
|
+
}
|
|
10
|
+
export interface ListRecentSessionsOptions {
|
|
11
|
+
limit?: number;
|
|
12
|
+
}
|
|
13
|
+
/** List recent sessions using the provider's existing discovery and metadata APIs. */
|
|
14
|
+
export declare function listRecentSessions(provider: SessionProviderBase, workspacePath: string, options?: ListRecentSessionsOptions): ProviderSessionIndex[];
|
|
15
|
+
/** Read a canonical transcript through a provider's existing reader. */
|
|
16
|
+
export declare function readSessionTranscript(provider: SessionProviderBase, sessionPath: string, options?: Omit<ProjectSessionTranscriptOptions, 'provider' | 'sessionId' | 'sourcePath'>): CanonicalSessionTranscript;
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.listRecentSessions = listRecentSessions;
|
|
4
|
+
exports.readSessionTranscript = readSessionTranscript;
|
|
5
|
+
const node_fs_1 = require("node:fs");
|
|
6
|
+
const transcript_1 = require("./transcript");
|
|
7
|
+
/** List recent sessions using the provider's existing discovery and metadata APIs. */
|
|
8
|
+
function listRecentSessions(provider, workspacePath, options = {}) {
|
|
9
|
+
const limit = Math.max(0, options.limit ?? 20);
|
|
10
|
+
return provider
|
|
11
|
+
.findAllSessions(workspacePath)
|
|
12
|
+
.map((sessionPath) => ({
|
|
13
|
+
provider: provider.id,
|
|
14
|
+
sessionId: provider.getSessionId(sessionPath),
|
|
15
|
+
sessionPath,
|
|
16
|
+
label: provider.extractSessionLabel(sessionPath),
|
|
17
|
+
modifiedAt: sessionModifiedAt(provider, sessionPath).toISOString(),
|
|
18
|
+
}))
|
|
19
|
+
.sort((left, right) => right.modifiedAt.localeCompare(left.modifiedAt))
|
|
20
|
+
.slice(0, limit);
|
|
21
|
+
}
|
|
22
|
+
function sessionModifiedAt(provider, sessionPath) {
|
|
23
|
+
const metadata = provider.getSessionMetadata?.(sessionPath);
|
|
24
|
+
if (metadata)
|
|
25
|
+
return metadata.mtime;
|
|
26
|
+
try {
|
|
27
|
+
return (0, node_fs_1.statSync)(sessionPath).mtime;
|
|
28
|
+
}
|
|
29
|
+
catch {
|
|
30
|
+
return new Date(0);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
/** Read a canonical transcript through a provider's existing reader. */
|
|
34
|
+
function readSessionTranscript(provider, sessionPath, options = {}) {
|
|
35
|
+
const reader = provider.createReader(sessionPath);
|
|
36
|
+
try {
|
|
37
|
+
return (0, transcript_1.projectSessionTranscript)(reader.readAll(), {
|
|
38
|
+
...options,
|
|
39
|
+
provider: provider.id,
|
|
40
|
+
sessionId: provider.getSessionId(sessionPath),
|
|
41
|
+
sourcePath: sessionPath,
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
finally {
|
|
45
|
+
reader.flush();
|
|
46
|
+
}
|
|
47
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export type TokenEstimationMethod = 'exact' | 'sidekick-fallback-v1';
|
|
2
|
+
export type TokenEstimationConfidence = 'exact' | 'medium' | 'low';
|
|
3
|
+
export interface ExactTokenCounterContext {
|
|
4
|
+
model?: string;
|
|
5
|
+
}
|
|
6
|
+
export type ExactTokenCounter = (text: string, context: ExactTokenCounterContext) => number | null | undefined;
|
|
7
|
+
export interface TokenEstimationOptions extends ExactTokenCounterContext {
|
|
8
|
+
exactCounter?: ExactTokenCounter;
|
|
9
|
+
}
|
|
10
|
+
export interface TokenEstimate {
|
|
11
|
+
count: number;
|
|
12
|
+
method: TokenEstimationMethod;
|
|
13
|
+
confidence: TokenEstimationConfidence;
|
|
14
|
+
provenance: string;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Estimate tokens without bundling a tokenizer.
|
|
18
|
+
*
|
|
19
|
+
* Fallback v1 counts Latin/source-code runs at 3.5 UTF-16 characters per
|
|
20
|
+
* token, CJK code points at one token, emoji at two, and other non-ASCII code
|
|
21
|
+
* points at one. The result is deliberately conservative for context budgets.
|
|
22
|
+
*/
|
|
23
|
+
export declare function estimateTextTokens(text: string, options?: TokenEstimationOptions): TokenEstimate;
|
|
24
|
+
export declare function estimateSerializedTokens(value: unknown, options?: TokenEstimationOptions): TokenEstimate;
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.estimateTextTokens = estimateTextTokens;
|
|
4
|
+
exports.estimateSerializedTokens = estimateSerializedTokens;
|
|
5
|
+
const CJK = /[\p{Script=Han}\p{Script=Hiragana}\p{Script=Katakana}\p{Script=Hangul}]/u;
|
|
6
|
+
const EMOJI = /\p{Extended_Pictographic}/u;
|
|
7
|
+
/**
|
|
8
|
+
* Estimate tokens without bundling a tokenizer.
|
|
9
|
+
*
|
|
10
|
+
* Fallback v1 counts Latin/source-code runs at 3.5 UTF-16 characters per
|
|
11
|
+
* token, CJK code points at one token, emoji at two, and other non-ASCII code
|
|
12
|
+
* points at one. The result is deliberately conservative for context budgets.
|
|
13
|
+
*/
|
|
14
|
+
function estimateTextTokens(text, options = {}) {
|
|
15
|
+
if (text.length === 0) {
|
|
16
|
+
return { count: 0, method: 'sidekick-fallback-v1', confidence: 'medium', provenance: 'empty' };
|
|
17
|
+
}
|
|
18
|
+
let exact;
|
|
19
|
+
try {
|
|
20
|
+
exact = options.exactCounter?.(text, { model: options.model });
|
|
21
|
+
}
|
|
22
|
+
catch {
|
|
23
|
+
exact = undefined;
|
|
24
|
+
}
|
|
25
|
+
if (typeof exact === 'number' && Number.isFinite(exact) && exact >= 0) {
|
|
26
|
+
return {
|
|
27
|
+
count: Math.ceil(exact),
|
|
28
|
+
method: 'exact',
|
|
29
|
+
confidence: 'exact',
|
|
30
|
+
provenance: options.model ?? 'injected-counter',
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
let asciiChars = 0;
|
|
34
|
+
let conservativeTokens = 0;
|
|
35
|
+
for (const codePoint of text) {
|
|
36
|
+
if (EMOJI.test(codePoint))
|
|
37
|
+
conservativeTokens += 2;
|
|
38
|
+
else if (CJK.test(codePoint))
|
|
39
|
+
conservativeTokens += 1;
|
|
40
|
+
else if ((codePoint.codePointAt(0) ?? 128) <= 127)
|
|
41
|
+
asciiChars += codePoint.length;
|
|
42
|
+
else
|
|
43
|
+
conservativeTokens += 1;
|
|
44
|
+
}
|
|
45
|
+
const count = Math.ceil(asciiChars / 3.5) + conservativeTokens;
|
|
46
|
+
return {
|
|
47
|
+
count: Number.isFinite(count) && count >= 0 ? count : 0,
|
|
48
|
+
method: 'sidekick-fallback-v1',
|
|
49
|
+
confidence: conservativeTokens > 0 ? 'low' : 'medium',
|
|
50
|
+
provenance: 'chars-per-token-3.5+cjk1+emoji2',
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
function estimateSerializedTokens(value, options = {}) {
|
|
54
|
+
let serialized;
|
|
55
|
+
try {
|
|
56
|
+
serialized = JSON.stringify(value) ?? '';
|
|
57
|
+
}
|
|
58
|
+
catch {
|
|
59
|
+
serialized = String(value);
|
|
60
|
+
}
|
|
61
|
+
return estimateTextTokens(serialized, options);
|
|
62
|
+
}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import type { ProviderId } from './providers/types';
|
|
2
|
+
import type { SessionEvent } from './types/sessionEvent';
|
|
3
|
+
import { type NormalizedUsage, type NormalizedUsageCost } from './usageNormalization';
|
|
4
|
+
export type TranscriptFidelity = 'snippet' | 'full';
|
|
5
|
+
export interface TranscriptSourceProvenance {
|
|
6
|
+
provider?: ProviderId | string;
|
|
7
|
+
sessionId?: string;
|
|
8
|
+
sourcePath?: string;
|
|
9
|
+
source?: string;
|
|
10
|
+
entrypoint?: string;
|
|
11
|
+
isMeta?: boolean;
|
|
12
|
+
isSidechain?: boolean;
|
|
13
|
+
originalRole?: string;
|
|
14
|
+
eventIndex: number;
|
|
15
|
+
eventType: SessionEvent['type'];
|
|
16
|
+
eventId?: string;
|
|
17
|
+
}
|
|
18
|
+
export interface CanonicalTranscriptBlock {
|
|
19
|
+
type: 'text' | 'thinking' | 'tool_use' | 'tool_result' | 'image' | 'unknown';
|
|
20
|
+
text?: string;
|
|
21
|
+
toolName?: string;
|
|
22
|
+
toolUseId?: string;
|
|
23
|
+
input?: unknown;
|
|
24
|
+
output?: unknown;
|
|
25
|
+
isError?: boolean;
|
|
26
|
+
command?: string;
|
|
27
|
+
truncated?: boolean;
|
|
28
|
+
raw?: unknown;
|
|
29
|
+
}
|
|
30
|
+
export interface CanonicalTranscriptMessage {
|
|
31
|
+
id?: string;
|
|
32
|
+
role: 'user' | 'assistant' | 'system' | 'summary' | 'tool';
|
|
33
|
+
timestamp: string;
|
|
34
|
+
model?: string;
|
|
35
|
+
sourceLabel?: string;
|
|
36
|
+
content: CanonicalTranscriptBlock[];
|
|
37
|
+
text: string;
|
|
38
|
+
usage?: NormalizedUsage;
|
|
39
|
+
cost?: NormalizedUsageCost;
|
|
40
|
+
tools: CanonicalToolCall[];
|
|
41
|
+
commands: string[];
|
|
42
|
+
source: TranscriptSourceProvenance;
|
|
43
|
+
}
|
|
44
|
+
export interface CanonicalToolCall {
|
|
45
|
+
id?: string;
|
|
46
|
+
name: string;
|
|
47
|
+
timestamp: string;
|
|
48
|
+
input?: unknown;
|
|
49
|
+
output?: unknown;
|
|
50
|
+
isError?: boolean;
|
|
51
|
+
command?: string;
|
|
52
|
+
source: TranscriptSourceProvenance;
|
|
53
|
+
}
|
|
54
|
+
export interface CanonicalTranscriptUsageTotals {
|
|
55
|
+
uncachedInputTokens: number;
|
|
56
|
+
cacheReadTokens: number;
|
|
57
|
+
cacheWriteTokens: number;
|
|
58
|
+
outputTokens: number;
|
|
59
|
+
reasoningTokens: number;
|
|
60
|
+
cacheInclusiveInputTokens: number;
|
|
61
|
+
billableOutputTokens: number;
|
|
62
|
+
totalTokens: number;
|
|
63
|
+
}
|
|
64
|
+
export interface CanonicalSessionTranscript {
|
|
65
|
+
provider?: ProviderId | string;
|
|
66
|
+
sessionId?: string;
|
|
67
|
+
sourcePath?: string;
|
|
68
|
+
cwd?: string;
|
|
69
|
+
gitBranch?: string;
|
|
70
|
+
startedAt?: string;
|
|
71
|
+
endedAt?: string;
|
|
72
|
+
models: string[];
|
|
73
|
+
messages: CanonicalTranscriptMessage[];
|
|
74
|
+
tools: CanonicalToolCall[];
|
|
75
|
+
commands: string[];
|
|
76
|
+
usage: {
|
|
77
|
+
events: NormalizedUsage[];
|
|
78
|
+
costs: NormalizedUsageCost[];
|
|
79
|
+
totals: CanonicalTranscriptUsageTotals;
|
|
80
|
+
totalCostUsd: number | null;
|
|
81
|
+
};
|
|
82
|
+
provenance: {
|
|
83
|
+
source: 'session-events';
|
|
84
|
+
fidelity: TranscriptFidelity;
|
|
85
|
+
eventCount: number;
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
export interface ProjectSessionTranscriptOptions {
|
|
89
|
+
provider?: ProviderId | string;
|
|
90
|
+
sessionId?: string;
|
|
91
|
+
sourcePath?: string;
|
|
92
|
+
fidelity?: TranscriptFidelity;
|
|
93
|
+
/** Maximum characters per text/serialized block in snippet mode. Default 2000. */
|
|
94
|
+
maxContentChars?: number;
|
|
95
|
+
includeUnknownBlocks?: boolean;
|
|
96
|
+
}
|
|
97
|
+
/** Project canonical provider events into a bounded or full-fidelity transcript. */
|
|
98
|
+
export declare function projectSessionTranscript(events: SessionEvent[], options?: ProjectSessionTranscriptOptions): CanonicalSessionTranscript;
|