mirror of
https://github.com/siteboon/claudecodeui.git
synced 2026-05-04 11:48:43 +00:00
* fix: reset-state-on-new-session-click * fix(chat): preserve continuity while session ids settle New conversations were crossing a short but important consistency gap. The route could already point at a newly created session id while the projects payload had not refreshed yet, and realtime/optimistic messages could still be keyed under a provisional id. In that window the UI could stop reading the active session store, briefly render the conversation as missing, and then repopulate it a moment later. That same gap also made duplication more likely. Optimistic local user messages could survive long enough to appear beside the persisted copy, and finalized assistant streaming rows could sit directly next to the server-backed assistant message with the same content before realtime state was cleared. The result was a chat view that felt unstable exactly when a new session was being created. This commit makes session-id reconciliation a first-class part of the chat flow instead of assuming every layer will agree immediately. The session store now understands canonical session aliases and can migrate one conversation from a provisional id to the real id without dropping its in-memory state. The route navigation path can replace the provisional URL entry instead of stacking it in history, and the project/session selection logic keeps a synthetic selected session alive long enough for the sidebar and project payloads to catch up. The practical goal is to keep one visible conversation throughout the whole creation lifecycle: no dead window between websocket events and project refresh, no stale provisional URL after the real id is known, and no extra optimistic/local bubbles when server history catches up. * fix(cli): resolve executable path for Claude CLI on Windows * fix(session-synchronizer): improve session name extraction for Claude and Codex
180 lines
5.3 KiB
TypeScript
180 lines
5.3 KiB
TypeScript
import os from 'node:os';
|
|
import path from 'node:path';
|
|
import { readFile } from 'node:fs/promises';
|
|
|
|
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 Codex transcript artifacts.
|
|
*/
|
|
export class CodexSessionSynchronizer implements IProviderSessionSynchronizer {
|
|
private readonly provider = 'codex' as const;
|
|
private readonly codexHome = path.join(os.homedir(), '.codex');
|
|
|
|
/**
|
|
* Scans ~/.codex/sessions and upserts discovered sessions into DB.
|
|
*/
|
|
async synchronize(since?: Date): Promise<number> {
|
|
const nameMap = await buildLookupMap(path.join(this.codexHome, 'session_index.jsonl'), 'id', 'thread_name');
|
|
const files = await findFilesRecursivelyCreatedAfter(
|
|
path.join(this.codexHome, 'sessions'),
|
|
'.jsonl',
|
|
since ?? null
|
|
);
|
|
|
|
let processed = 0;
|
|
for (const filePath of files) {
|
|
const parsed = await this.processSessionFile(filePath, nameMap);
|
|
if (!parsed) {
|
|
continue;
|
|
}
|
|
|
|
const existingSession = sessionsDb.getSessionById(parsed.sessionId);
|
|
if (existingSession) {
|
|
// If session name is untitled and we now have a name, update it
|
|
if (existingSession.custom_name === 'Untitled Codex Session' && parsed.sessionName && parsed.sessionName !== 'Untitled Codex Session') {
|
|
sessionsDb.updateSessionCustomName(parsed.sessionId, parsed.sessionName);
|
|
}
|
|
}
|
|
|
|
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 Codex session JSONL file.
|
|
*/
|
|
async synchronizeFile(filePath: string): Promise<string | null> {
|
|
if (!filePath.endsWith('.jsonl')) {
|
|
return null;
|
|
}
|
|
|
|
const nameMap = await buildLookupMap(path.join(this.codexHome, 'session_index.jsonl'), 'id', 'thread_name');
|
|
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 Codex JSONL session file.
|
|
*/
|
|
private async processSessionFile(
|
|
filePath: string,
|
|
nameMap: Map<string, string>
|
|
): Promise<ParsedSession | null> {
|
|
const parsed = await extractFirstValidJsonlData(filePath, (rawData) => {
|
|
const data = rawData as Record<string, unknown>;
|
|
const payload = data.payload as Record<string, unknown> | undefined;
|
|
const sessionId = typeof payload?.id === 'string' ? payload.id : undefined;
|
|
const projectPath = typeof payload?.cwd === 'string' ? payload.cwd : undefined;
|
|
|
|
if (!sessionId || !projectPath) {
|
|
return null;
|
|
}
|
|
|
|
return {
|
|
sessionId,
|
|
projectPath,
|
|
};
|
|
});
|
|
|
|
if (!parsed) {
|
|
return null;
|
|
}
|
|
|
|
const existingSession = sessionsDb.getSessionById(parsed.sessionId);
|
|
const existingSessionName = existingSession?.custom_name;
|
|
if (existingSessionName && existingSessionName !== 'Untitled Codex Session') {
|
|
return {
|
|
...parsed,
|
|
sessionName: normalizeSessionName(existingSessionName, 'Untitled Codex Session'),
|
|
};
|
|
}
|
|
|
|
let sessionName = nameMap.get(parsed.sessionId);
|
|
if (!sessionName) {
|
|
sessionName = await this.extractLastAgentMessageFromEnd(filePath);
|
|
}
|
|
|
|
return {
|
|
...parsed,
|
|
sessionName: normalizeSessionName(sessionName, 'Untitled Codex Session'),
|
|
};
|
|
}
|
|
|
|
private async extractLastAgentMessageFromEnd(filePath: string): Promise<string | undefined> {
|
|
try {
|
|
const content = await readFile(filePath, 'utf8');
|
|
const lines = content.split(/\r?\n/);
|
|
|
|
for (let index = lines.length - 1; index >= 0; index -= 1) {
|
|
const line = lines[index]?.trim();
|
|
if (!line) {
|
|
continue;
|
|
}
|
|
|
|
let parsed: unknown;
|
|
try {
|
|
parsed = JSON.parse(line);
|
|
} catch {
|
|
continue;
|
|
}
|
|
|
|
const data = parsed as Record<string, unknown>;
|
|
const eventType = typeof data.type === 'string' ? data.type : undefined;
|
|
const payload = data.payload as Record<string, unknown> | undefined;
|
|
const payloadType = typeof payload?.type === 'string' ? payload.type : undefined;
|
|
const lastAgentMessage = typeof payload?.last_agent_message === 'string'
|
|
? payload.last_agent_message
|
|
: undefined;
|
|
|
|
if (eventType === 'event_msg' && payloadType === 'task_complete' && lastAgentMessage?.trim()) {
|
|
return lastAgentMessage;
|
|
}
|
|
}
|
|
} catch {
|
|
// Ignore missing/unreadable files so sync can continue.
|
|
}
|
|
|
|
return undefined;
|
|
}
|
|
}
|