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

@@ -1,4 +1,7 @@
<script lang="ts">
import { page } from '$app/stores';
import { goto } from '$app/navigation';
import { resolveRoute } from '$app/paths';
import type { ChatMessage } from '$lib/types';
import type { SubagentResult } from '$lib/proto/llm_multiverse/v1/common_pb';
import MessageList from '$lib/components/MessageList.svelte';
@@ -8,21 +11,45 @@
import FinalResult from '$lib/components/FinalResult.svelte';
import { processRequest, OrchestratorError } from '$lib/services/orchestrator';
import { OrchestrationState } from '$lib/proto/llm_multiverse/v1/orchestrator_pb';
import { sessionStore } from '$lib/stores/sessions.svelte';
let messages: ChatMessage[] = $state([]);
let isStreaming = $state(false);
let error: string | null = $state(null);
let sessionId = $state(crypto.randomUUID());
let orchestrationState: OrchestrationState = $state(OrchestrationState.UNSPECIFIED);
let intermediateResult: string = $state('');
let finalResult: SubagentResult | null = $state(null);
// Initialize session from URL or create new one
$effect(() => {
const sessionParam = $page.url.searchParams.get('session');
const session = sessionStore.getOrCreateSession(sessionParam ?? undefined);
messages = [...session.messages];
if (!sessionParam || sessionParam !== session.id) {
const url = `${resolveRoute('/chat')}?session=${session.id}`;
// eslint-disable-next-line svelte/no-navigation-without-resolve
goto(url, { replaceState: true });
}
});
function handleNewChat() {
const session = sessionStore.createSession();
messages = [];
error = null;
finalResult = null;
const url = `${resolveRoute('/chat')}?session=${session.id}`;
// eslint-disable-next-line svelte/no-navigation-without-resolve
goto(url);
}
async function handleSend(content: string) {
error = null;
orchestrationState = OrchestrationState.UNSPECIFIED;
intermediateResult = '';
finalResult = null;
const sessionId = sessionStore.activeSessionId!;
const userMessage: ChatMessage = {
id: crypto.randomUUID(),
role: 'user',
@@ -39,6 +66,7 @@
};
messages.push(assistantMessage);
sessionStore.updateMessages(sessionId, messages);
isStreaming = true;
try {
@@ -69,13 +97,21 @@
};
} finally {
isStreaming = false;
sessionStore.updateMessages(sessionId, messages);
}
}
</script>
<div class="flex h-screen flex-col">
<header class="border-b border-gray-200 bg-white px-4 py-3">
<header class="flex items-center justify-between border-b border-gray-200 bg-white px-4 py-3">
<h1 class="text-lg font-semibold text-gray-900">Chat</h1>
<button
type="button"
onclick={handleNewChat}
class="rounded-lg bg-gray-100 px-3 py-1.5 text-sm font-medium text-gray-700 hover:bg-gray-200"
>
New Chat
</button>
</header>
<MessageList {messages} />