From 77b71c6e9d782f869b61406132387c1e0376d890 Mon Sep 17 00:00:00 2001 From: faligam Date: Sun, 6 Sep 2026 17:39:22 +0200 Subject: [PATCH] Restore browser session and normalize research status --- src/lib/auth.ts | 8 ++++- src/lib/research-api.ts | 49 +++++++++++++++++++++++---- src/routes/+layout.svelte | 2 ++ src/routes/research/{id}/+page.svelte | 2 -- 4 files changed, 52 insertions(+), 9 deletions(-) diff --git a/src/lib/auth.ts b/src/lib/auth.ts index 68c1f60..86e813c 100644 --- a/src/lib/auth.ts +++ b/src/lib/auth.ts @@ -14,6 +14,12 @@ export function logout(): void { user.set(null); } +/** Restore the in-memory user store from the persistent browser session. */ +export function restoreSession(): void { + const apiKey = getApiKey(); + user.set(apiKey ? { apiKey } : null); +} + export function isAuthenticated(): boolean { return getApiKey() !== null; } @@ -28,4 +34,4 @@ export function setApiKey(key: string): void { export function removeApiKey(): void { localStorage.removeItem(API_KEY_STORAGE); -} \ No newline at end of file +} diff --git a/src/lib/research-api.ts b/src/lib/research-api.ts index 082912c..8b5a5fc 100644 --- a/src/lib/research-api.ts +++ b/src/lib/research-api.ts @@ -1,6 +1,35 @@ import { apiGet, apiPost, apiDelete } from '$lib/api'; import type { ResearchStatus, ResearchSummary, SourceInfo, ClaimInfo, EvidenceScore } from '$lib/types'; +interface ApiResearchStatus { + research_id: string; + query: string; + depth: string; + state: string; + created_at: string; + updated_at?: string; + source_count?: number; + claim_count?: number; +} + +function normalizeStatus(status: ApiResearchStatus): ResearchStatus { + return { + id: status.research_id, + status: status.state.toUpperCase(), + }; +} + +function normalizeDetail(status: ApiResearchStatus): ResearchDetail { + return { + id: status.research_id, + query: status.query, + depth: status.depth, + status: status.state.toUpperCase(), + created_at: status.created_at, + completed_at: status.updated_at, + }; +} + /** * Erzeugt eine neue Research-Anfrage. */ @@ -10,11 +39,12 @@ export async function createResearch( language: string, depth: string ): Promise<{ id: string }> { - return await apiPost<{ id: string }>( + const response = await apiPost<{ research_id: string }>( '/v1/research', { query, language, depth }, { apiKey } ); + return { id: response.research_id }; } /** @@ -24,7 +54,8 @@ export async function getResearchStatus( apiKey: string, researchId: string ): Promise { - return await apiGet(`/v1/research/${researchId}/status`, { apiKey }); + const response = await apiGet(`/v1/research/${researchId}/status`, { apiKey }); + return normalizeStatus(response); } /** @@ -34,7 +65,8 @@ export async function getResearchDetail( apiKey: string, researchId: string ): Promise { - return await apiGet(`/v1/research/${researchId}`, { apiKey }); + const response = await apiGet(`/v1/research/${researchId}`, { apiKey }); + return normalizeDetail(response); } /** @@ -44,8 +76,13 @@ export async function listResearch( apiKey: string, limit: number = 50 ): Promise { - const list = await apiGet('/v1/research', { apiKey }); - return list.slice(0, limit); + const response = await apiGet<{ items: ApiResearchStatus[] }>('/v1/research', { apiKey }); + return response.items.slice(0, limit).map(item => ({ + id: item.research_id, + query: item.query, + status: item.state.toUpperCase(), + created_at: item.created_at, + })); } /** @@ -164,4 +201,4 @@ export interface ShareData { share_view_count?: number; share_max_views?: number; share_expired?: boolean; -} \ No newline at end of file +} diff --git a/src/routes/+layout.svelte b/src/routes/+layout.svelte index 369d7df..4ebc3af 100644 --- a/src/routes/+layout.svelte +++ b/src/routes/+layout.svelte @@ -1,10 +1,12 @@ diff --git a/src/routes/research/{id}/+page.svelte b/src/routes/research/{id}/+page.svelte index 93b8882..0ddbaf7 100644 --- a/src/routes/research/{id}/+page.svelte +++ b/src/routes/research/{id}/+page.svelte @@ -73,8 +73,6 @@ // Non-critical } } - - errorBanners.addErrorInfo(null as any); } catch (e) { const msg = e instanceof Error ? e.message : 'Status-Abfrage fehlgeschlagen.'; error = msg;