feat: add models management and usage history components
- Introduced RequestContentDialog for displaying request content details. - Added UsageBarChart for visualizing token usage data. - Implemented UsageHistorySection to manage and display usage history with filtering and pagination. - Created provider-types for managing provider-related types. - Developed ModelsPage to encapsulate models configuration, providers, and usage history. - Defined usage-history types and utility functions for managing usage data. - Updated routing to include models page and redirect agents to models. - Refactored chat store to integrate models instead of agents. - Established models store for managing model-related state and data fetching.
This commit is contained in:
@@ -1,21 +1,39 @@
|
||||
import { app } from 'electron';
|
||||
import * as fs from 'fs';
|
||||
import * as path from 'path';
|
||||
import { parseSessionKey } from '@runtime/lib/agents';
|
||||
import { parseSessionKey } from '@runtime/lib/models';
|
||||
|
||||
export function getTranscriptFilePath(sessionKey: string): string {
|
||||
const PRIMARY_TRANSCRIPT_ROOT_DIR = 'models';
|
||||
const LEGACY_TRANSCRIPT_ROOT_DIR = 'agents';
|
||||
|
||||
function buildTranscriptFilePath(sessionKey: string, rootDirName: string): string {
|
||||
const parsed = parseSessionKey(sessionKey);
|
||||
let agentId = parsed.isAgentSession ? parsed.agentId : 'default';
|
||||
const agentId = parsed.isAgentSession ? parsed.agentId : 'default';
|
||||
let sessionId = parsed.isAgentSession ? parsed.sessionId : sessionKey;
|
||||
|
||||
if (!sessionId) {
|
||||
sessionId = 'unknown';
|
||||
}
|
||||
|
||||
const baseDir = path.join(app.getPath('userData'), 'agents', agentId, 'sessions');
|
||||
const baseDir = path.join(app.getPath('userData'), rootDirName, agentId, 'sessions');
|
||||
return path.join(baseDir, `${sessionId}.jsonl`);
|
||||
}
|
||||
|
||||
export function getTranscriptFilePath(sessionKey: string): string {
|
||||
return buildTranscriptFilePath(sessionKey, PRIMARY_TRANSCRIPT_ROOT_DIR);
|
||||
}
|
||||
|
||||
export function getLegacyTranscriptFilePath(sessionKey: string): string {
|
||||
return buildTranscriptFilePath(sessionKey, LEGACY_TRANSCRIPT_ROOT_DIR);
|
||||
}
|
||||
|
||||
export function getTranscriptPathCandidates(sessionKey: string): string[] {
|
||||
return Array.from(new Set([
|
||||
getTranscriptFilePath(sessionKey),
|
||||
getLegacyTranscriptFilePath(sessionKey),
|
||||
]));
|
||||
}
|
||||
|
||||
export function appendTranscriptLine(sessionKey: string, lineObject: any): void {
|
||||
const filePath = getTranscriptFilePath(sessionKey);
|
||||
fs.mkdirSync(path.dirname(filePath), { recursive: true });
|
||||
|
||||
@@ -14,12 +14,14 @@ export {
|
||||
type TokenUsageHistoryEntry,
|
||||
} from './token-usage-core';
|
||||
|
||||
async function listAgentIdsWithSessionDirs(): Promise<string[]> {
|
||||
const agentsDir = join(app.getPath('userData'), 'agents');
|
||||
const TRANSCRIPT_ROOT_DIR_NAMES = ['models', 'agents'] as const;
|
||||
|
||||
async function listAgentIdsWithSessionDirs(rootDirName: string): Promise<string[]> {
|
||||
const rootDir = join(app.getPath('userData'), rootDirName);
|
||||
const agentIds = new Set<string>();
|
||||
|
||||
try {
|
||||
const agentEntries = await readdir(agentsDir, { withFileTypes: true });
|
||||
const agentEntries = await readdir(rootDir, { withFileTypes: true });
|
||||
for (const entry of agentEntries) {
|
||||
if (entry.isDirectory()) {
|
||||
const normalized = entry.name.trim();
|
||||
@@ -36,38 +38,45 @@ async function listAgentIdsWithSessionDirs(): Promise<string[]> {
|
||||
}
|
||||
|
||||
async function listRecentSessionFiles(): Promise<Array<{ filePath: string; sessionId: string; agentId: string; mtimeMs: number }>> {
|
||||
const agentsDir = join(app.getPath('userData'), 'agents');
|
||||
const filesBySession = new Map<string, { filePath: string; sessionId: string; agentId: string; mtimeMs: number }>();
|
||||
|
||||
try {
|
||||
const agentEntries = await listAgentIdsWithSessionDirs();
|
||||
const files: Array<{ filePath: string; sessionId: string; agentId: string; mtimeMs: number }> = [];
|
||||
for (const rootDirName of TRANSCRIPT_ROOT_DIR_NAMES) {
|
||||
const agentEntries = await listAgentIdsWithSessionDirs(rootDirName);
|
||||
|
||||
for (const agentId of agentEntries) {
|
||||
const sessionsDir = join(agentsDir, agentId, 'sessions');
|
||||
try {
|
||||
const sessionEntries = await readdir(sessionsDir);
|
||||
for (const agentId of agentEntries) {
|
||||
const sessionsDir = join(app.getPath('userData'), rootDirName, agentId, 'sessions');
|
||||
try {
|
||||
const sessionEntries = await readdir(sessionsDir);
|
||||
|
||||
for (const fileName of sessionEntries) {
|
||||
const sessionId = extractSessionIdFromTranscriptFileName(fileName);
|
||||
if (!sessionId) continue;
|
||||
const filePath = join(sessionsDir, fileName);
|
||||
try {
|
||||
const fileStat = await stat(filePath);
|
||||
files.push({
|
||||
filePath,
|
||||
sessionId,
|
||||
agentId,
|
||||
mtimeMs: fileStat.mtimeMs,
|
||||
});
|
||||
} catch {
|
||||
continue;
|
||||
for (const fileName of sessionEntries) {
|
||||
const sessionId = extractSessionIdFromTranscriptFileName(fileName);
|
||||
if (!sessionId) continue;
|
||||
const filePath = join(sessionsDir, fileName);
|
||||
try {
|
||||
const fileStat = await stat(filePath);
|
||||
const sessionKey = `${agentId}:${sessionId}`;
|
||||
const existing = filesBySession.get(sessionKey);
|
||||
|
||||
if (!existing || fileStat.mtimeMs > existing.mtimeMs) {
|
||||
filesBySession.set(sessionKey, {
|
||||
filePath,
|
||||
sessionId,
|
||||
agentId,
|
||||
mtimeMs: fileStat.mtimeMs,
|
||||
});
|
||||
}
|
||||
} catch {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
continue;
|
||||
}
|
||||
} catch {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
const files = [...filesBySession.values()];
|
||||
files.sort((a, b) => b.mtimeMs - a.mtimeMs);
|
||||
return files;
|
||||
} catch {
|
||||
|
||||
Reference in New Issue
Block a user