fix: prevent session creation loop when running without backend
Make getOrCreateSession idempotent by returning the existing active session when called with no ID, instead of always creating a new one. Use untrack() in the $effect to sever SvelteMap dependency so it only re-runs on $page changes. Disable SSR on client-only routes, reduce preload aggressiveness, and skip retries when already disconnected. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -5,7 +5,7 @@
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
||||
%sveltekit.head%
|
||||
</head>
|
||||
<body data-sveltekit-preload-data="hover">
|
||||
<body data-sveltekit-preload-data="tap">
|
||||
<div style="display: contents">%sveltekit.body%</div>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
@@ -161,6 +161,11 @@ export async function* processRequest(
|
||||
let lastError: OrchestratorError | null = null;
|
||||
|
||||
for (let attempt = 0; attempt <= MAX_RETRIES; attempt++) {
|
||||
// Skip retries if already known disconnected
|
||||
if (connectionStore.status === 'disconnected' && attempt > 0) {
|
||||
break;
|
||||
}
|
||||
|
||||
if (attempt > 0) {
|
||||
connectionStore.setReconnecting();
|
||||
const delay = backoffDelay(attempt - 1);
|
||||
|
||||
@@ -67,6 +67,10 @@ function createSessionStore() {
|
||||
saveActiveSessionId(id);
|
||||
return sessions.get(id)!;
|
||||
}
|
||||
// No specific ID — prefer existing active session
|
||||
if (!id && activeSessionId && sessions.has(activeSessionId)) {
|
||||
return sessions.get(activeSessionId)!;
|
||||
}
|
||||
return createSession(id);
|
||||
}
|
||||
|
||||
|
||||
2
src/routes/+page.ts
Normal file
2
src/routes/+page.ts
Normal file
@@ -0,0 +1,2 @@
|
||||
export const prerender = false;
|
||||
export const ssr = false;
|
||||
@@ -16,11 +16,13 @@
|
||||
import ConfigSidebar from '$lib/components/ConfigSidebar.svelte';
|
||||
import ThemeToggle from '$lib/components/ThemeToggle.svelte';
|
||||
import ConnectionStatus from '$lib/components/ConnectionStatus.svelte';
|
||||
import { onMount, untrack } from 'svelte';
|
||||
import { sessionStore } from '$lib/stores/sessions.svelte';
|
||||
import { isNonDefaultConfig } from '$lib/utils/sessionConfig';
|
||||
import { createOrchestration } from '$lib/composables/useOrchestration.svelte';
|
||||
|
||||
let messages: ChatMessage[] = $state([]);
|
||||
let initialized = $state(false);
|
||||
let sessionConfig: SessionConfig = $state(
|
||||
create(SessionConfigSchema, { overrideLevel: OverrideLevel.NONE })
|
||||
);
|
||||
@@ -39,13 +41,26 @@
|
||||
goto(url, { replaceState: replace });
|
||||
}
|
||||
|
||||
$effect(() => {
|
||||
onMount(() => {
|
||||
const sessionParam = $page.url.searchParams.get('session');
|
||||
const session = sessionStore.getOrCreateSession(sessionParam ?? undefined);
|
||||
messages = [...session.messages];
|
||||
if (!sessionParam || sessionParam !== session.id) {
|
||||
navigateToSession(session.id, true);
|
||||
}
|
||||
initialized = true;
|
||||
});
|
||||
|
||||
// Sync messages when session changes via sidebar (after initial mount)
|
||||
$effect(() => {
|
||||
if (!initialized) return;
|
||||
const sessionParam = $page.url.searchParams.get('session');
|
||||
if (sessionParam) {
|
||||
const session = untrack(() => sessionStore.activeSession);
|
||||
if (session && session.id === sessionParam) {
|
||||
messages = [...session.messages];
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
function handleNewChat() {
|
||||
|
||||
2
src/routes/chat/+page.ts
Normal file
2
src/routes/chat/+page.ts
Normal file
@@ -0,0 +1,2 @@
|
||||
export const prerender = false;
|
||||
export const ssr = false;
|
||||
Reference in New Issue
Block a user