feat: gate model overrides and load full token history (#271)
Co-authored-by: zuolingxuan <zuolingxuan@bytedance.com>
This commit is contained in:
committed by
GitHub
Unverified
parent
3371e4fe74
commit
30b03add1c
@@ -1882,8 +1882,8 @@ function registerSettingsHandlers(gatewayManager: GatewayManager): void {
|
||||
function registerUsageHandlers(): void {
|
||||
ipcMain.handle('usage:recentTokenHistory', async (_, limit?: number) => {
|
||||
const safeLimit = typeof limit === 'number' && Number.isFinite(limit)
|
||||
? Math.min(Math.max(Math.floor(limit), 1), 100)
|
||||
: 20;
|
||||
? Math.max(Math.floor(limit), 1)
|
||||
: undefined;
|
||||
return await getRecentTokenUsageHistory(safeLimit);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -41,12 +41,15 @@ interface TranscriptLineShape {
|
||||
export function parseUsageEntriesFromJsonl(
|
||||
content: string,
|
||||
context: { sessionId: string; agentId: string },
|
||||
limit = 20,
|
||||
limit?: number,
|
||||
): TokenUsageHistoryEntry[] {
|
||||
const entries: TokenUsageHistoryEntry[] = [];
|
||||
const lines = content.split(/\r?\n/).filter(Boolean);
|
||||
const maxEntries = typeof limit === 'number' && Number.isFinite(limit)
|
||||
? Math.max(Math.floor(limit), 0)
|
||||
: Number.POSITIVE_INFINITY;
|
||||
|
||||
for (let i = lines.length - 1; i >= 0 && entries.length < limit; i -= 1) {
|
||||
for (let i = lines.length - 1; i >= 0 && entries.length < maxEntries; i -= 1) {
|
||||
let parsed: TranscriptLineShape;
|
||||
try {
|
||||
parsed = JSON.parse(lines[i]) as TranscriptLineShape;
|
||||
|
||||
@@ -46,18 +46,21 @@ async function listRecentSessionFiles(): Promise<Array<{ filePath: string; sessi
|
||||
}
|
||||
}
|
||||
|
||||
export async function getRecentTokenUsageHistory(limit = 20): Promise<TokenUsageHistoryEntry[]> {
|
||||
export async function getRecentTokenUsageHistory(limit?: number): Promise<TokenUsageHistoryEntry[]> {
|
||||
const files = await listRecentSessionFiles();
|
||||
const results: TokenUsageHistoryEntry[] = [];
|
||||
const maxEntries = typeof limit === 'number' && Number.isFinite(limit)
|
||||
? Math.max(Math.floor(limit), 0)
|
||||
: Number.POSITIVE_INFINITY;
|
||||
|
||||
for (const file of files) {
|
||||
if (results.length >= limit) break;
|
||||
if (results.length >= maxEntries) break;
|
||||
try {
|
||||
const content = await readFile(file.filePath, 'utf8');
|
||||
const entries = parseUsageEntriesFromJsonl(content, {
|
||||
sessionId: file.sessionId,
|
||||
agentId: file.agentId,
|
||||
}, limit - results.length);
|
||||
}, Number.isFinite(maxEntries) ? maxEntries - results.length : undefined);
|
||||
results.push(...entries);
|
||||
} catch (error) {
|
||||
logger.debug(`Failed to read token usage transcript ${file.filePath}:`, error);
|
||||
@@ -65,5 +68,5 @@ export async function getRecentTokenUsageHistory(limit = 20): Promise<TokenUsage
|
||||
}
|
||||
|
||||
results.sort((a, b) => Date.parse(b.timestamp) - Date.parse(a.timestamp));
|
||||
return results.slice(0, limit);
|
||||
return Number.isFinite(maxEntries) ? results.slice(0, maxEntries) : results;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user