mirror of
https://github.com/aljazceru/claude-code-viewer.git
synced 2025-12-22 07:44:20 +01:00
perf: added cache for project, session
This commit is contained in:
69
src/server/service/session/SessionRepository.ts
Normal file
69
src/server/service/session/SessionRepository.ts
Normal file
@@ -0,0 +1,69 @@
|
||||
import { readdir, readFile } from "node:fs/promises";
|
||||
import { resolve } from "node:path";
|
||||
import { parseJsonl } from "../parseJsonl";
|
||||
import { decodeProjectId } from "../project/id";
|
||||
import type { Session, SessionDetail } from "../types";
|
||||
import { decodeSessionId, encodeSessionId } from "./id";
|
||||
import { sessionMetaStorage } from "./sessionMetaStorage";
|
||||
|
||||
const getTime = (date: string | null) => {
|
||||
if (date === null) return 0;
|
||||
return new Date(date).getTime();
|
||||
};
|
||||
|
||||
export class SessionRepository {
|
||||
public async getSession(
|
||||
projectId: string,
|
||||
sessionId: string,
|
||||
): Promise<{
|
||||
session: SessionDetail;
|
||||
}> {
|
||||
const sessionPath = decodeSessionId(projectId, sessionId);
|
||||
const content = await readFile(sessionPath, "utf-8");
|
||||
|
||||
const conversations = parseJsonl(content);
|
||||
|
||||
const sessionDetail: SessionDetail = {
|
||||
id: sessionId,
|
||||
jsonlFilePath: sessionPath,
|
||||
meta: await sessionMetaStorage.getSessionMeta(projectId, sessionId),
|
||||
conversations,
|
||||
};
|
||||
|
||||
return {
|
||||
session: sessionDetail,
|
||||
};
|
||||
}
|
||||
|
||||
public async getSessions(
|
||||
projectId: string,
|
||||
): Promise<{ sessions: Session[] }> {
|
||||
try {
|
||||
const claudeProjectPath = decodeProjectId(projectId);
|
||||
const dirents = await readdir(claudeProjectPath, { withFileTypes: true });
|
||||
const sessions = await Promise.all(
|
||||
dirents
|
||||
.filter((d) => d.isFile() && d.name.endsWith(".jsonl"))
|
||||
.map(async (d) => ({
|
||||
id: encodeSessionId(resolve(claudeProjectPath, d.name)),
|
||||
jsonlFilePath: resolve(claudeProjectPath, d.name),
|
||||
meta: await sessionMetaStorage.getSessionMeta(
|
||||
projectId,
|
||||
encodeSessionId(resolve(claudeProjectPath, d.name)),
|
||||
),
|
||||
})),
|
||||
);
|
||||
|
||||
return {
|
||||
sessions: sessions.sort((a, b) => {
|
||||
return (
|
||||
getTime(b.meta.lastModifiedAt) - getTime(a.meta.lastModifiedAt)
|
||||
);
|
||||
}),
|
||||
};
|
||||
} catch (error) {
|
||||
console.warn(`Failed to read sessions for project ${projectId}:`, error);
|
||||
return { sessions: [] };
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user