mirror of
https://github.com/wu736139669/hapi.git
synced 2026-11-18 03:27:08 +00:00
feat: add cache-aware token usage dashboard (#1338)
* feat: add cache-aware token usage dashboard Track normalized Claude, Codex, and ACP usage with incremental SQLite backfill. Exclude imported transcript history, rebuild usage after history rewrites, and expose an owner-only dashboard with cache-aware totals and breakdowns. via [HAPI](https://hapi.run) Co-Authored-By: HAPI <noreply@hapi.run> * fix: preserve usage model and local dates via [HAPI](https://hapi.run) Co-Authored-By: HAPI <noreply@hapi.run> * fix: normalize cached usage and timezone buckets via [HAPI](https://hapi.run) Co-Authored-By: HAPI <noreply@hapi.run> --------- Co-authored-by: HAPI <noreply@hapi.run>
This commit is contained in:
@@ -104,16 +104,28 @@ describe('codexSessionScanner', () => {
|
||||
].join('\n') + '\n'
|
||||
);
|
||||
|
||||
const replayFlags: boolean[] = [];
|
||||
scanner = await createCodexSessionScanner({
|
||||
transcriptPath,
|
||||
replayExistingHistory: true,
|
||||
onEvent: (event) => events.push(event)
|
||||
onEvent: (event, context) => {
|
||||
events.push(event);
|
||||
replayFlags.push(context.replayedHistory);
|
||||
}
|
||||
});
|
||||
|
||||
await wait(300);
|
||||
expect(events).toHaveLength(2);
|
||||
expect(events[0]?.type).toBe('session_meta');
|
||||
expect(events[1]?.payload).toEqual({ type: 'agent_message', message: 'old' });
|
||||
expect(replayFlags).toEqual([true, true]);
|
||||
|
||||
await appendFile(
|
||||
transcriptPath,
|
||||
JSON.stringify({ type: 'event_msg', payload: { type: 'agent_message', message: 'new' } }) + '\n'
|
||||
);
|
||||
await scanner.flush();
|
||||
expect(replayFlags).toEqual([true, true, false]);
|
||||
});
|
||||
|
||||
it('reports session id from the transcript metadata', async () => {
|
||||
|
||||
@@ -5,7 +5,7 @@ import type { CodexSessionEvent } from './codexEventConverter';
|
||||
|
||||
interface CodexSessionScannerOptions {
|
||||
transcriptPath: string | null;
|
||||
onEvent: (event: CodexSessionEvent) => void;
|
||||
onEvent: (event: CodexSessionEvent, context: { replayedHistory: boolean }) => void;
|
||||
onSessionId?: (sessionId: string) => void;
|
||||
replayExistingHistory?: boolean;
|
||||
}
|
||||
@@ -35,7 +35,7 @@ export async function createCodexSessionScanner(opts: CodexSessionScannerOptions
|
||||
|
||||
class CodexSessionScannerImpl extends BaseSessionScanner<CodexSessionEvent> {
|
||||
private transcriptPath: string | null;
|
||||
private readonly onEvent: (event: CodexSessionEvent) => void;
|
||||
private readonly onEvent: (event: CodexSessionEvent, context: { replayedHistory: boolean }) => void;
|
||||
private readonly onSessionId?: (sessionId: string) => void;
|
||||
private readonly fileEpochByPath = new Map<string, number>();
|
||||
private readonly fileStateByPath = new Map<string, {
|
||||
@@ -45,6 +45,7 @@ class CodexSessionScannerImpl extends BaseSessionScanner<CodexSessionEvent> {
|
||||
nextLineIndex: number;
|
||||
}>();
|
||||
private replayExistingHistoryOnNextAttach: boolean;
|
||||
private replayingExistingHistory = false;
|
||||
private observedSessionId: string | null = null;
|
||||
|
||||
constructor(opts: CodexSessionScannerOptions) {
|
||||
@@ -92,8 +93,13 @@ class CodexSessionScannerImpl extends BaseSessionScanner<CodexSessionEvent> {
|
||||
}
|
||||
|
||||
protected async handleFileScan(stats: SessionFileScanStats<CodexSessionEvent>): Promise<void> {
|
||||
for (const event of stats.events) {
|
||||
this.onEvent(event);
|
||||
const replayedHistory = this.replayingExistingHistory;
|
||||
try {
|
||||
for (const event of stats.events) {
|
||||
this.onEvent(event, { replayedHistory });
|
||||
}
|
||||
} finally {
|
||||
this.replayingExistingHistory = false;
|
||||
}
|
||||
if (stats.newCount > 0) {
|
||||
logger.debug(`[codex-session-scanner] ${stats.newCount} new events from ${stats.filePath}`);
|
||||
@@ -106,9 +112,11 @@ class CodexSessionScannerImpl extends BaseSessionScanner<CodexSessionEvent> {
|
||||
// 中文注释:导入既有 Codex thread 时,首次挂接 transcript 不能先 prime 到 EOF,
|
||||
// 否则 Hapi 只会看到后续增量,客户端里已经存在的最新消息会被跳过。
|
||||
this.replayExistingHistoryOnNextAttach = false;
|
||||
this.replayingExistingHistory = true;
|
||||
return;
|
||||
}
|
||||
|
||||
this.replayingExistingHistory = false;
|
||||
await this.primeTranscript(filePath);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user