refactor(usage): transcript readers come from their drivers (#17576)
Co-authored-by: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
507361bc73
commit
e5dca2332f
34 files changed
+1731
-1411
No files matched your search
@@ -19,23 +19,16 @@ import * as NodeFSP from "node:fs/promises";
|
||||
import * as NodePath from "node:path";
|
||||
import * as NodeStringDecoder from "node:string_decoder";
|
||||
|
||||
import type { UsageProviderKind } from "@t3tools/contracts";
|
||||
import type {
|
||||
SelectedFields,
|
||||
TranscriptUsageFormat,
|
||||
UsageRecord,
|
||||
} from "@t3tools/provider-core/server/usage";
|
||||
import * as Option from "effect/Option";
|
||||
import * as Schema from "effect/Schema";
|
||||
|
||||
import { createTranscriptJsonReader } from "../project/AgentSessionJson.ts";
|
||||
|
||||
import {
|
||||
initialCodexScanState,
|
||||
mightCarryUsage,
|
||||
parseClaudeLine,
|
||||
parseClaudeRecord,
|
||||
parseCodexLine,
|
||||
parseCodexRecord,
|
||||
parseGrokLine,
|
||||
parseGrokRecord,
|
||||
type CodexScanState,
|
||||
type UsageRecord,
|
||||
} from "./usageTranscripts.ts";
|
||||
|
||||
export interface TranscriptFile {
|
||||
readonly path: string;
|
||||
readonly size: number;
|
||||
@@ -60,8 +53,11 @@ export interface TranscriptParsePosition {
|
||||
readonly guardLength: number;
|
||||
/** FNV-1a hash of that window. */
|
||||
readonly guardHash: number;
|
||||
/** Codex reducer state as of `resumeOffset`; `null` for stateless providers. */
|
||||
readonly codexState: CodexScanState | null;
|
||||
/**
|
||||
* The format's reducer state as of `resumeOffset`, encoded by its schema;
|
||||
* `null` for stateless formats.
|
||||
*/
|
||||
readonly state: unknown;
|
||||
}
|
||||
|
||||
export interface TranscriptParseResult {
|
||||
@@ -89,45 +85,7 @@ const NEWLINE = 0x0a;
|
||||
const STAT_CONCURRENCY = 32;
|
||||
const CARRIAGE_RETURN = 0x0d;
|
||||
|
||||
type SelectedFields = { readonly [key: string]: true | SelectedFields };
|
||||
|
||||
// Keep the fields consumed by usageTranscripts, including reducer state and
|
||||
// dedupe/cost metadata. A selected subtree (usage) keeps future token fields.
|
||||
const USAGE_FIELDS: Record<"claude" | "codex" | "grok", SelectedFields> = {
|
||||
claude: {
|
||||
type: true,
|
||||
timestamp: true,
|
||||
requestId: true,
|
||||
sessionId: true,
|
||||
costUSD: true,
|
||||
message: { id: true, model: true, usage: true },
|
||||
},
|
||||
codex: {
|
||||
type: true,
|
||||
timestamp: true,
|
||||
payload: {
|
||||
type: true,
|
||||
id: true,
|
||||
session_id: true,
|
||||
model: true,
|
||||
thread_settings: { service_tier: true },
|
||||
forked_from_id: true,
|
||||
source: { subagent: { thread_spawn: { parent_thread_id: true } } },
|
||||
info: { last_token_usage: true },
|
||||
},
|
||||
},
|
||||
grok: {
|
||||
timestamp: true,
|
||||
params: {
|
||||
sessionId: true,
|
||||
_meta: { agentTimestampMs: true },
|
||||
update: { sessionUpdate: true, prompt_id: true, usage: true },
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
function selectUsageFields(provider: UsageProviderKind) {
|
||||
const fields = USAGE_FIELDS[provider === "codex" || provider === "grok" ? provider : "claude"];
|
||||
function selectUsageFields(fields: SelectedFields) {
|
||||
return (path: ReadonlyArray<string | number | null>): boolean => {
|
||||
let selected: true | SelectedFields = fields;
|
||||
for (const key of path) {
|
||||
@@ -257,14 +215,12 @@ async function guardMatches(
|
||||
* still match, so only appended lines are read; otherwise the whole file is
|
||||
* re-parsed from the start and `resumed` reports `false`.
|
||||
*
|
||||
* Codex carries the active model on `turn_context` lines and the service tier
|
||||
* on `thread_settings_applied` lines. Neither holds usage of its own, but both
|
||||
* still have to pass through the reducer to keep attribution and pricing
|
||||
* correct.
|
||||
* A stateful format resumes with the state saved at that position; a saved
|
||||
* state its schema rejects re-parses the file whole.
|
||||
*/
|
||||
export async function readTranscriptRecords(
|
||||
export async function readTranscriptRecords<State>(
|
||||
filePath: string,
|
||||
provider: UsageProviderKind,
|
||||
format: TranscriptUsageFormat<State>,
|
||||
resumeFrom?: TranscriptParsePosition,
|
||||
options?: { readonly streamingThresholdBytes?: number },
|
||||
): Promise<TranscriptParseResult | null> {
|
||||
@@ -277,41 +233,31 @@ export async function readTranscriptRecords(
|
||||
}
|
||||
|
||||
try {
|
||||
let codexState = initialCodexScanState();
|
||||
const codec = format.state;
|
||||
const decodeState = codec === undefined ? undefined : Schema.decodeUnknownOption(codec.schema);
|
||||
const encodeState = codec === undefined ? undefined : Schema.encodeSync(codec.schema);
|
||||
// Stateless formats never read their state argument.
|
||||
let state = codec === undefined ? (undefined as State) : codec.initial();
|
||||
let resumed = false;
|
||||
let start = 0;
|
||||
const savedState =
|
||||
resumeFrom === undefined || decodeState === undefined || resumeFrom.state === null
|
||||
? Option.none()
|
||||
: decodeState(resumeFrom.state);
|
||||
if (
|
||||
resumeFrom !== undefined &&
|
||||
resumeFrom.resumeOffset > 0 &&
|
||||
(provider !== "codex" || resumeFrom.codexState !== null) &&
|
||||
(codec === undefined || Option.isSome(savedState)) &&
|
||||
(await guardMatches(handle, resumeFrom))
|
||||
) {
|
||||
if (resumeFrom.codexState !== null) codexState = { ...resumeFrom.codexState };
|
||||
if (Option.isSome(savedState)) state = savedState.value;
|
||||
start = resumeFrom.resumeOffset;
|
||||
resumed = true;
|
||||
}
|
||||
|
||||
const parseLine = (line: string, state: CodexScanState, out: UsageRecord[]): void => {
|
||||
if (provider === "codex") {
|
||||
if (
|
||||
!mightCarryUsage(line, provider) &&
|
||||
!line.includes('"turn_context"') &&
|
||||
!line.includes('"thread_settings_applied"') &&
|
||||
!line.includes('"session_meta"')
|
||||
) {
|
||||
return;
|
||||
}
|
||||
const record = parseCodexLine(line, state);
|
||||
if (record !== null) out.push(record);
|
||||
return;
|
||||
}
|
||||
if (!mightCarryUsage(line, provider)) return;
|
||||
if (provider === "grok") {
|
||||
for (const grokRecord of parseGrokLine(line)) out.push(grokRecord);
|
||||
return;
|
||||
}
|
||||
const record = parseClaudeLine(line);
|
||||
if (record !== null) out.push(record);
|
||||
const parseLine = (line: string, lineState: State, out: UsageRecord[]): void => {
|
||||
if (!format.mightCarryUsage(line)) return;
|
||||
for (const record of format.parseLine(line, lineState)) out.push(record);
|
||||
};
|
||||
|
||||
const toLineString = (lineBuffer: Buffer): string => {
|
||||
@@ -331,7 +277,7 @@ export async function readTranscriptRecords(
|
||||
let pendingBytes = 0;
|
||||
let streaming: ReturnType<typeof createTranscriptJsonReader> | undefined;
|
||||
let decoder: NodeStringDecoder.StringDecoder | undefined;
|
||||
const selectPath = selectUsageFields(provider);
|
||||
const selectPath = selectUsageFields(format.selectFields);
|
||||
|
||||
const append = (segment: Buffer) => {
|
||||
if (!streaming && pendingBytes + segment.length <= streamingThresholdBytes) {
|
||||
@@ -350,25 +296,17 @@ export async function readTranscriptRecords(
|
||||
}
|
||||
streaming.write(decoder!.write(segment));
|
||||
};
|
||||
const finish = (state: CodexScanState, out: UsageRecord[]) => {
|
||||
const finish = (lineState: State, out: UsageRecord[]) => {
|
||||
if (streaming) {
|
||||
streaming.write(decoder!.end());
|
||||
const projected = streaming.finish();
|
||||
if (provider === "grok") {
|
||||
out.push(...parseGrokRecord(projected));
|
||||
} else {
|
||||
const record =
|
||||
provider === "codex"
|
||||
? parseCodexRecord(projected, state)
|
||||
: parseClaudeRecord(projected);
|
||||
if (record !== null) out.push(record);
|
||||
}
|
||||
for (const record of format.parseProjected(projected, lineState)) out.push(record);
|
||||
} else if (pendingBytes > 0) {
|
||||
const line =
|
||||
pendingChunks.length === 1
|
||||
? pendingChunks[0]!
|
||||
: Buffer.concat(pendingChunks, pendingBytes);
|
||||
parseLine(toLineString(line), state, out);
|
||||
parseLine(toLineString(line), lineState, out);
|
||||
}
|
||||
pendingChunks = [];
|
||||
pendingBytes = 0;
|
||||
@@ -391,10 +329,10 @@ export async function readTranscriptRecords(
|
||||
// Most lines fit in the current chunk. Avoid buffering/streaming
|
||||
// machinery on this hot path.
|
||||
if (!streaming && pendingBytes === 0) {
|
||||
parseLine(toLineString(chunk.subarray(lineStart, newlineIndex)), codexState, records);
|
||||
parseLine(toLineString(chunk.subarray(lineStart, newlineIndex)), state, records);
|
||||
} else {
|
||||
append(chunk.subarray(lineStart, newlineIndex));
|
||||
finish(codexState, records);
|
||||
finish(state, records);
|
||||
}
|
||||
lineStart = newlineIndex + 1;
|
||||
resumeOffset = scanOffset + lineStart;
|
||||
@@ -402,8 +340,14 @@ export async function readTranscriptRecords(
|
||||
scanOffset += chunk.length;
|
||||
}
|
||||
|
||||
// The unfinished tail parses against a copy, so the saved state stays at
|
||||
// `resumeOffset` and the next scan replays the tail from it.
|
||||
const encodedState = encodeState === undefined ? null : encodeState(state);
|
||||
const tailRecords: UsageRecord[] = [];
|
||||
finish({ ...codexState }, tailRecords);
|
||||
finish(
|
||||
decodeState === undefined ? state : Option.getOrThrow(decodeState(encodedState)),
|
||||
tailRecords,
|
||||
);
|
||||
|
||||
const guardLength = Math.min(GUARD_LENGTH, resumeOffset);
|
||||
let guardHash = 0;
|
||||
@@ -420,7 +364,7 @@ export async function readTranscriptRecords(
|
||||
resumeOffset,
|
||||
guardLength,
|
||||
guardHash,
|
||||
codexState: provider === "codex" ? codexState : null,
|
||||
state: encodedState,
|
||||
},
|
||||
resumed,
|
||||
};
|
||||
|
||||
Reference in new issue
Block a user