refactor: remove sessions names db

This commit is contained in:
Haileyesus
2026-03-28 11:06:37 +03:00
parent 6cfe617711
commit ce0dfad638
16 changed files with 196 additions and 201 deletions

View File

@@ -1,22 +1,92 @@
import { workspaceOriginalPathsDb } from '@/shared/database/repositories/workspace-original-paths.db.js';
import { getConnection } from '@/shared/database/connection.js';
import type { SessionWithSummary } from '@/shared/database/types.js';
// ---------------------------------------------------------------------------
// Queries
// ---------------------------------------------------------------------------
type SessionNameLookupRow = {
session_id: string;
custom_name: string;
};
function normalizeTimestamp(value?: string): string | null {
if (!value) return null;
const parsed = new Date(value);
if (Number.isNaN(parsed.getTime())) {
return null;
}
return parsed.toISOString();
}
export const sessionsDb = {
createSession(session_id: string, provider: string, workspacePath: string, customName?: string): void {
createSession(
session_id: string,
provider: string,
workspacePath: string,
customName?: string,
createdAt?: string,
updatedAt?: string,
): void {
const db = getConnection();
const createdAtValue = normalizeTimestamp(createdAt);
const updatedAtValue = normalizeTimestamp(updatedAt);
// First, ensure the workspace path is recorded in the workspace_original_paths table
// since it's a foreign key in the sessions table.
workspaceOriginalPathsDb.createWorkspacePath(workspacePath);
db.prepare(
'INSERT OR IGNORE INTO sessions (session_id, provider, custom_name, workspace_path) VALUES (?, ?, ?, ?)'
).run(session_id, provider, customName, workspacePath);
`INSERT INTO sessions (session_id, provider, custom_name, workspace_path, created_at, updated_at)
VALUES (?, ?, ?, ?, COALESCE(?, CURRENT_TIMESTAMP), COALESCE(?, CURRENT_TIMESTAMP))
ON CONFLICT(session_id) DO UPDATE SET updated_at = excluded.updated_at
WHERE sessions.provider = excluded.provider`
).run(session_id, provider, customName, workspacePath, createdAtValue, updatedAtValue);
},
/** Updates a custom session name for an existing session row. */
createSessionName(sessionId: string, provider: string, customName: string): void {
const db = getConnection();
db.prepare(
`UPDATE sessions
SET custom_name = ?, updated_at = CURRENT_TIMESTAMP
WHERE session_id = ? AND provider = ?`
).run(customName, sessionId, provider);
},
getSessionName(sessionId: string, provider: string): string | null {
const db = getConnection();
const row = db
.prepare(
`SELECT custom_name
FROM sessions
WHERE session_id = ? AND provider = ?`
)
.get(sessionId, provider) as { custom_name: string | null } | undefined;
return row?.custom_name ?? null;
},
getSessionNames(sessionIds: string[], provider: string): Map<string, string> {
if (sessionIds.length === 0) return new Map();
const db = getConnection();
const placeholders = sessionIds.map(() => '?').join(',');
const rows = db
.prepare(
`SELECT session_id, custom_name
FROM sessions
WHERE session_id IN (${placeholders})
AND provider = ?
AND custom_name IS NOT NULL`
)
.all(...sessionIds, provider) as SessionNameLookupRow[];
return new Map(rows.map((row) => [row.session_id, row.custom_name]));
},
deleteSession(session_id: string): void {
@@ -24,59 +94,18 @@ export const sessionsDb = {
db.prepare('DELETE FROM sessions WHERE session_id = ?').run(session_id);
},
applyCustomSessionNames(sessions: SessionWithSummary[] | undefined | null, provider: string): void {
if (!sessions?.length) return;
// /** Inserts or updates a custom session name (upsert on session_id + provider). */
// setName(sessionId: string, provider: string, customName: string): void {
// const db = getConnection();
// db.prepare(
// `INSERT INTO session_names (session_id, provider, custom_name)
// VALUES (?, ?, ?)
// ON CONFLICT(session_id, provider)
// DO UPDATE SET custom_name = excluded.custom_name,
// updated_at = CURRENT_TIMESTAMP`
// ).run(sessionId, provider, customName);
// },
const ids = sessions.map((session) => session.id);
const customNames = sessionsDb.getSessionNames(ids, provider);
/** Returns the custom name for a single session, or null if unset. */
// getName(sessionId: string, provider: string): string | null {
// const db = getConnection();
// const row = db
// .prepare(
// 'SELECT custom_name FROM session_names WHERE session_id = ? AND provider = ?'
// )
// .get(sessionId, provider) as { custom_name: string } | undefined;
// return row?.custom_name ?? null;
// },
/**
* Batch lookup for multiple session IDs.
* Returns a Map<sessionId, customName> for efficient overlay onto session lists.
*/
// getNames(sessionIds: string[], provider: string): Map<string, string> {
// if (sessionIds.length === 0) return new Map();
// const db = getConnection();
// const placeholders = sessionIds.map(() => '?').join(',');
// const rows = db
// .prepare(
// `SELECT session_id, custom_name FROM session_names
// WHERE session_id IN (${placeholders}) AND provider = ?`
// )
// .all(...sessionIds, provider) as SessionNameLookupRow[];
// return new Map(rows.map((r) => [r.session_id, r.custom_name]));
// },
/** Removes a custom session name. Returns true if a row was deleted. */
// deleteName(sessionId: string, provider: string): boolean {
// const db = getConnection();
// return (
// db
// .prepare(
// 'DELETE FROM session_names WHERE session_id = ? AND provider = ?'
// )
// .run(sessionId, provider).changes > 0
// );
// },
for (const session of sessions) {
const customName = customNames.get(session.id);
if (customName) {
session.summary = customName;
}
}
},
};