mirror of
https://github.com/siteboon/claudecodeui.git
synced 2026-05-08 21:48:20 +00:00
111 lines
3.0 KiB
TypeScript
111 lines
3.0 KiB
TypeScript
import os from 'node:os';
|
|
import path from 'node:path';
|
|
|
|
import { sessionsDb } from '@/modules/database/index.js';
|
|
import {
|
|
buildLookupMap,
|
|
extractFirstValidJsonlData,
|
|
findFilesRecursivelyCreatedAfter,
|
|
normalizeSessionName,
|
|
readFileTimestamps,
|
|
} from '@/shared/utils.js';
|
|
import type { IProviderSessionSynchronizer } from '@/shared/interfaces.js';
|
|
|
|
type ParsedSession = {
|
|
sessionId: string;
|
|
projectPath: string;
|
|
sessionName?: string;
|
|
};
|
|
|
|
/**
|
|
* Session indexer for Claude transcript artifacts.
|
|
*/
|
|
export class ClaudeSessionSynchronizer implements IProviderSessionSynchronizer {
|
|
private readonly provider = 'claude' as const;
|
|
private readonly claudeHome = path.join(os.homedir(), '.claude');
|
|
|
|
/**
|
|
* Scans ~/.claude/projects and upserts discovered sessions into DB.
|
|
*/
|
|
async synchronize(since?: Date): Promise<number> {
|
|
const nameMap = await buildLookupMap(path.join(this.claudeHome, 'history.jsonl'), 'sessionId', 'display');
|
|
const files = await findFilesRecursivelyCreatedAfter(
|
|
path.join(this.claudeHome, 'projects'),
|
|
'.jsonl',
|
|
since ?? null
|
|
);
|
|
|
|
let processed = 0;
|
|
for (const filePath of files) {
|
|
const parsed = await this.processSessionFile(filePath, nameMap);
|
|
if (!parsed) {
|
|
continue;
|
|
}
|
|
|
|
const timestamps = await readFileTimestamps(filePath);
|
|
sessionsDb.createSession(
|
|
parsed.sessionId,
|
|
this.provider,
|
|
parsed.projectPath,
|
|
parsed.sessionName,
|
|
timestamps.createdAt,
|
|
timestamps.updatedAt,
|
|
filePath
|
|
);
|
|
processed += 1;
|
|
}
|
|
|
|
return processed;
|
|
}
|
|
|
|
/**
|
|
* Parses and upserts one Claude session JSONL file.
|
|
*/
|
|
async synchronizeFile(filePath: string): Promise<string | null> {
|
|
if (!filePath.endsWith('.jsonl')) {
|
|
return null;
|
|
}
|
|
|
|
const nameMap = await buildLookupMap(path.join(this.claudeHome, 'history.jsonl'), 'sessionId', 'display');
|
|
const parsed = await this.processSessionFile(filePath, nameMap);
|
|
if (!parsed) {
|
|
return null;
|
|
}
|
|
|
|
const timestamps = await readFileTimestamps(filePath);
|
|
return sessionsDb.createSession(
|
|
parsed.sessionId,
|
|
this.provider,
|
|
parsed.projectPath,
|
|
parsed.sessionName,
|
|
timestamps.createdAt,
|
|
timestamps.updatedAt,
|
|
filePath
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Extracts session metadata from one Claude JSONL session file.
|
|
*/
|
|
private async processSessionFile(
|
|
filePath: string,
|
|
nameMap: Map<string, string>
|
|
): Promise<ParsedSession | null> {
|
|
return extractFirstValidJsonlData(filePath, (rawData) => {
|
|
const data = rawData as Record<string, unknown>;
|
|
const sessionId = typeof data.sessionId === 'string' ? data.sessionId : undefined;
|
|
const projectPath = typeof data.cwd === 'string' ? data.cwd : undefined;
|
|
|
|
if (!sessionId || !projectPath) {
|
|
return null;
|
|
}
|
|
|
|
return {
|
|
sessionId,
|
|
projectPath,
|
|
sessionName: normalizeSessionName(nameMap.get(sessionId), 'Untitled Claude Session'),
|
|
};
|
|
});
|
|
}
|
|
}
|