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:
34
electron/utils/token-usage-writer.ts
Normal file
34
electron/utils/token-usage-writer.ts
Normal 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');
|
||||
}
|
||||
Reference in New Issue
Block a user