feat: add session creation and ID management with localStorage

- Session store with UUID v4 generation and localStorage persistence
- Session ID in URL params (?session=<id>) for deep linking
- "New Chat" button for creating fresh sessions
- Message history persisted per session
- Session title auto-generated from first user message

Closes #11

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
shahondin1624
2026-03-12 11:52:19 +01:00
parent 9613a5ad5b
commit 247abfe32c
4 changed files with 165 additions and 2 deletions

View File

@@ -0,0 +1,109 @@
import type { ChatMessage } from '$lib/types';
export interface Session {
id: string;
title: string;
messages: ChatMessage[];
createdAt: Date;
}
const STORAGE_KEY = 'llm-multiverse-sessions';
const ACTIVE_SESSION_KEY = 'llm-multiverse-active-session';
function loadSessions(): Map<string, Session> {
if (typeof localStorage === 'undefined') return new Map();
try {
const raw = localStorage.getItem(STORAGE_KEY);
if (!raw) return new Map();
const arr: [string, Session][] = JSON.parse(raw);
return new Map(
arr.map(([id, s]) => [
id,
{ ...s, createdAt: new Date(s.createdAt), messages: s.messages.map(m => ({ ...m, timestamp: new Date(m.timestamp) })) }
])
);
} catch {
return new Map();
}
}
function saveSessions(sessions: Map<string, Session>) {
if (typeof localStorage === 'undefined') return;
localStorage.setItem(STORAGE_KEY, JSON.stringify([...sessions.entries()]));
}
function loadActiveSessionId(): string | null {
if (typeof localStorage === 'undefined') return null;
return localStorage.getItem(ACTIVE_SESSION_KEY);
}
function saveActiveSessionId(id: string) {
if (typeof localStorage === 'undefined') return;
localStorage.setItem(ACTIVE_SESSION_KEY, id);
}
function createSessionStore() {
const sessions = $state<Map<string, Session>>(loadSessions());
let activeSessionId = $state<string | null>(loadActiveSessionId());
function createSession(id?: string): Session {
const session: Session = {
id: id ?? crypto.randomUUID(),
title: 'New Chat',
messages: [],
createdAt: new Date()
};
sessions.set(session.id, session);
activeSessionId = session.id;
saveSessions(sessions);
saveActiveSessionId(session.id);
return session;
}
function getOrCreateSession(id?: string): Session {
if (id && sessions.has(id)) {
activeSessionId = id;
saveActiveSessionId(id);
return sessions.get(id)!;
}
return createSession(id);
}
function updateMessages(sessionId: string, messages: ChatMessage[]) {
const session = sessions.get(sessionId);
if (!session) return;
session.messages = messages;
// Update title from first user message if still default
if (session.title === 'New Chat') {
const firstUser = messages.find(m => m.role === 'user');
if (firstUser) {
session.title = firstUser.content.slice(0, 50) + (firstUser.content.length > 50 ? '...' : '');
}
}
sessions.set(sessionId, session);
saveSessions(sessions);
}
function switchSession(id: string) {
if (sessions.has(id)) {
activeSessionId = id;
saveActiveSessionId(id);
}
}
function getAllSessions(): Session[] {
return [...sessions.values()].sort((a, b) => b.createdAt.getTime() - a.createdAt.getTime());
}
return {
get activeSessionId() { return activeSessionId; },
get activeSession() { return activeSessionId ? sessions.get(activeSessionId) ?? null : null; },
createSession,
getOrCreateSession,
updateMessages,
switchSession,
getAllSessions
};
}
export const sessionStore = createSessionStore();