Restore browser session and normalize research status

This commit is contained in:
faligam
2026-09-06 17:39:22 +02:00
parent 25530b3e75
commit 77b71c6e9d
4 changed files with 52 additions and 9 deletions

View File

@@ -14,6 +14,12 @@ export function logout(): void {
user.set(null); 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 { export function isAuthenticated(): boolean {
return getApiKey() !== null; return getApiKey() !== null;
} }
@@ -28,4 +34,4 @@ export function setApiKey(key: string): void {
export function removeApiKey(): void { export function removeApiKey(): void {
localStorage.removeItem(API_KEY_STORAGE); localStorage.removeItem(API_KEY_STORAGE);
} }

View File

@@ -1,6 +1,35 @@
import { apiGet, apiPost, apiDelete } from '$lib/api'; import { apiGet, apiPost, apiDelete } from '$lib/api';
import type { ResearchStatus, ResearchSummary, SourceInfo, ClaimInfo, EvidenceScore } from '$lib/types'; 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. * Erzeugt eine neue Research-Anfrage.
*/ */
@@ -10,11 +39,12 @@ export async function createResearch(
language: string, language: string,
depth: string depth: string
): Promise<{ id: string }> { ): Promise<{ id: string }> {
return await apiPost<{ id: string }>( const response = await apiPost<{ research_id: string }>(
'/v1/research', '/v1/research',
{ query, language, depth }, { query, language, depth },
{ apiKey } { apiKey }
); );
return { id: response.research_id };
} }
/** /**
@@ -24,7 +54,8 @@ export async function getResearchStatus(
apiKey: string, apiKey: string,
researchId: string researchId: string
): Promise<ResearchStatus> { ): Promise<ResearchStatus> {
return await apiGet<ResearchStatus>(`/v1/research/${researchId}/status`, { apiKey }); const response = await apiGet<ApiResearchStatus>(`/v1/research/${researchId}/status`, { apiKey });
return normalizeStatus(response);
} }
/** /**
@@ -34,7 +65,8 @@ export async function getResearchDetail(
apiKey: string, apiKey: string,
researchId: string researchId: string
): Promise<ResearchDetail> { ): Promise<ResearchDetail> {
return await apiGet<ResearchDetail>(`/v1/research/${researchId}`, { apiKey }); const response = await apiGet<ApiResearchStatus>(`/v1/research/${researchId}`, { apiKey });
return normalizeDetail(response);
} }
/** /**
@@ -44,8 +76,13 @@ export async function listResearch(
apiKey: string, apiKey: string,
limit: number = 50 limit: number = 50
): Promise<ResearchSummary[]> { ): Promise<ResearchSummary[]> {
const list = await apiGet<ResearchSummary[]>('/v1/research', { apiKey }); const response = await apiGet<{ items: ApiResearchStatus[] }>('/v1/research', { apiKey });
return list.slice(0, limit); 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_view_count?: number;
share_max_views?: number; share_max_views?: number;
share_expired?: boolean; share_expired?: boolean;
} }

View File

@@ -1,10 +1,12 @@
<script> <script>
import '../app.css'; import '../app.css';
import { theme, initTheme, applyTheme } from '$lib/theme'; import { theme, initTheme, applyTheme } from '$lib/theme';
import { restoreSession } from '$lib/auth';
import { onMount } from 'svelte'; import { onMount } from 'svelte';
initTheme(); initTheme();
onMount(() => { onMount(() => {
restoreSession();
applyTheme($theme); applyTheme($theme);
}); });
</script> </script>

View File

@@ -73,8 +73,6 @@
// Non-critical // Non-critical
} }
} }
errorBanners.addErrorInfo(null as any);
} catch (e) { } catch (e) {
const msg = e instanceof Error ? e.message : 'Status-Abfrage fehlgeschlagen.'; const msg = e instanceof Error ? e.message : 'Status-Abfrage fehlgeschlagen.';
error = msg; error = msg;