Restore browser session and normalize research status
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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<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,
|
||||
researchId: string
|
||||
): 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,
|
||||
limit: number = 50
|
||||
): Promise<ResearchSummary[]> {
|
||||
const list = await apiGet<ResearchSummary[]>('/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,
|
||||
}));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
<script>
|
||||
import '../app.css';
|
||||
import { theme, initTheme, applyTheme } from '$lib/theme';
|
||||
import { restoreSession } from '$lib/auth';
|
||||
import { onMount } from 'svelte';
|
||||
|
||||
initTheme();
|
||||
onMount(() => {
|
||||
restoreSession();
|
||||
applyTheme($theme);
|
||||
});
|
||||
</script>
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user