import { writable } from 'svelte/store'; import { apiGet, apiDelete, apiPost } from '$lib/api'; import type { ResearchSummary } from '$lib/types'; // Sidebar visibility (mobile toggle) export const sidebarOpen = writable(false); // Currently selected research session ID export const selectedResearchId = writable(null); export interface ResearchListItem extends ResearchSummary { truncatedQuery?: string; truncatedId?: string; } const HISTORY_LIMIT = 50; interface ResearchListResponse { items: Array<{ research_id: string; query: string; state: string; created_at: string; }>; } export async function loadResearchHistory(apiKey: string): Promise { const response = await apiGet('/v1/research', { apiKey }); const list = response.items.map(item => ({ id: item.research_id, query: item.query, status: item.state, created_at: item.created_at, })); // Sort descending by created_at (newest first), limit to HISTORY_LIMIT const sorted = list .slice() .sort((a, b) => new Date(b.created_at).getTime() - new Date(a.created_at).getTime()) .slice(0, HISTORY_LIMIT); return sorted; } export async function deleteResearch(id: string, apiKey: string): Promise { await apiDelete(`/v1/research/${id}`, { apiKey }); } export async function createResearch( apiKey: string, query: string, language: string, depth: string ): Promise<{ research_id: string }> { return await apiPost<{ research_id: string }>( '/v1/research', { query, language, depth }, { apiKey } ); }