feat: enhance token usage tracking and history management

- Updated HTML assets for improved loading.
- Integrated token usage tracking in chat processing, appending usage details to transcripts.
- Enhanced OpenAIProvider to include usage data in chat completion responses.
- Implemented asynchronous retrieval of recent token usage history.
- Added utility functions for managing transcript files and parsing usage data.
- Updated UI components to reflect changes in usage status handling.
- Ensured consistent usage status definitions across the application.
This commit is contained in:
DEV_DSW
2026-04-15 11:45:33 +08:00
parent 9afb518a19
commit 78d3235ab6
12 changed files with 533 additions and 2577 deletions

View File

@@ -0,0 +1,34 @@
import { app } from 'electron';
import * as fs from 'fs';
import * as path from 'path';
export function getTranscriptFilePath(sessionKey: string): string {
let agentId: string;
let sessionId: string;
if (sessionKey.startsWith('agent:')) {
const parts = sessionKey.split(':');
agentId = parts[1] ?? 'default';
sessionId = parts.slice(2).join(':') || sessionKey;
} else if (sessionKey.startsWith('local:')) {
const parts = sessionKey.split(':');
agentId = parts[1] ?? 'local';
sessionId = parts.slice(2).join(':') || sessionKey;
} else {
agentId = 'default';
sessionId = sessionKey;
}
if (!sessionId) {
sessionId = 'unknown';
}
const baseDir = path.join(app.getPath('userData'), 'agents', agentId, 'sessions');
return path.join(baseDir, `${sessionId}.jsonl`);
}
export function appendTranscriptLine(sessionKey: string, lineObject: any): void {
const filePath = getTranscriptFilePath(sessionKey);
fs.mkdirSync(path.dirname(filePath), { recursive: true });
fs.appendFileSync(filePath, JSON.stringify(lineObject) + '\n', 'utf-8');
}