import type { Project, Workflow, ExecutionLogEntry, Comment, Timer, KbArticle, KbArticleSummary, PlanStepInfo, LlmCallLogEntry, ChatMessage } from './types' const BASE = `${import.meta.env.BASE_URL.replace(/\/$/, '')}/api` async function request(path: string, options?: RequestInit): Promise { const res = await fetch(`${BASE}${path}`, { headers: { 'Content-Type': 'application/json' }, ...options, }) if (!res.ok) { const text = await res.text() throw new Error(`API error ${res.status}: ${text}`) } return res.json() } export const api = { listProjects: () => request('/projects'), createProject: (name: string, description = '') => request('/projects', { method: 'POST', body: JSON.stringify({ name, description }), }), getProject: (id: string) => request(`/projects/${id}`), updateProject: (id: string, data: { name?: string; description?: string }) => request(`/projects/${id}`, { method: 'PUT', body: JSON.stringify(data), }), deleteProject: (id: string) => request(`/projects/${id}`, { method: 'DELETE' }), listWorkflows: (projectId: string) => request(`/projects/${projectId}/workflows`), createWorkflow: (projectId: string, requirement: string, templateId?: string) => request(`/projects/${projectId}/workflows`, { method: 'POST', body: JSON.stringify({ requirement, template_id: templateId || undefined }), }), listTemplates: () => request<{ id: string; name: string; description: string; examples: { label: string; text: string }[] }[]>('/templates'), listSteps: (workflowId: string) => request(`/workflows/${workflowId}/steps`), listComments: (workflowId: string) => request(`/workflows/${workflowId}/comments`), createComment: (workflowId: string, content: string) => request(`/workflows/${workflowId}/comments`, { method: 'POST', body: JSON.stringify({ content }), }), getReport: (workflowId: string) => request<{ report: string }>(`/workflows/${workflowId}/report`), listPlanSteps: (workflowId: string) => request(`/workflows/${workflowId}/plan`), listLlmCalls: (workflowId: string) => request(`/workflows/${workflowId}/llm-calls`), listTimers: (projectId: string) => request(`/projects/${projectId}/timers`), createTimer: (projectId: string, data: { name: string; interval_secs: number; requirement: string }) => request(`/projects/${projectId}/timers`, { method: 'POST', body: JSON.stringify(data), }), updateTimer: (timerId: string, data: { name?: string; interval_secs?: number; requirement?: string; enabled?: boolean }) => request(`/timers/${timerId}`, { method: 'PUT', body: JSON.stringify(data), }), deleteTimer: (timerId: string) => request(`/timers/${timerId}`, { method: 'DELETE' }), getKb: () => request<{ content: string }>('/kb'), listArticles: () => request('/kb/articles'), createArticle: (title: string) => request('/kb/articles', { method: 'POST', body: JSON.stringify({ title }), }), getArticle: (id: string) => request(`/kb/articles/${id}`), updateArticle: (id: string, data: { title?: string; content?: string }) => request(`/kb/articles/${id}`, { method: 'PUT', body: JSON.stringify(data), }), deleteArticle: (id: string) => request(`/kb/articles/${id}`, { method: 'DELETE' }), chat: (messages: ChatMessage[]) => request<{ reply: string }>('/chat', { method: 'POST', body: JSON.stringify({ messages }), }), getSettings: () => request>('/settings'), putSetting: (key: string, value: string) => request>(`/settings/${key}`, { method: 'PUT', body: JSON.stringify({ value }), }), }