import type { User, UserCreate, Project, ProjectCreate, ProjectsResponse, ProjectAvailability, Task, TaskCreate, TasksResponse, MembersResponse, ActivityResponse } from '../types'; const API_BASE = 'http://localhost:8000/api'; class ApiClient { private async request(endpoint: string, options?: RequestInit): Promise { const response = await fetch(`${API_BASE}${endpoint}`, { headers: { 'Content-Type': 'application/json', ...options?.headers, }, ...options, }); if (!response.ok) { const error = await response.json().catch(() => ({ error: 'Request failed' })); throw new Error(error.error || error.detail || 'Request failed'); } return response.json(); } // User endpoints async createUser(data: UserCreate): Promise { return this.request('/users', { method: 'POST', body: JSON.stringify(data), }); } async getUser(userId: string): Promise { return this.request(`/users/${userId}`); } async listUsers(): Promise { return this.request('/users'); } // Project endpoints async checkProjectAvailability(projectId: string): Promise { return this.request(`/projects/check/${projectId}`); } async listProjects(userId: string): Promise { return this.request(`/projects?userId=${userId}`); } async createProject(data: ProjectCreate): Promise { return this.request('/projects', { method: 'POST', body: JSON.stringify(data), }); } async joinProject(projectId: string, userId: string): Promise<{ message: string; project_id: string; role: string }> { return this.request(`/projects/${projectId}/join`, { method: 'POST', body: JSON.stringify({ userId }), }); } // Task endpoints async listTasks(projectId: string, status?: string): Promise { const query = status ? `?status=${status}` : ''; return this.request(`/projects/${projectId}/tasks${query}`); } async createTask(projectId: string, data: TaskCreate): Promise { return this.request(`/projects/${projectId}/tasks`, { method: 'POST', body: JSON.stringify(data), }); } async generateTasks(projectId: string, count = 50): Promise<{ tasks: { title: string; description: string }[] }> { return this.request(`/projects/${projectId}/tasks/generate`, { method: 'POST', body: JSON.stringify({ count: Math.min(count, 50) }), }); } async completeTask(taskId: string, data: { userId: string; whatIDid: string; codeSnippet?: string }) { return this.request(`/tasks/${taskId}/complete`, { method: 'POST', body: JSON.stringify(data), }); } async updateTaskStatus(taskId: string, status: 'todo' | 'in_progress' | 'done', userId?: string): Promise { return this.request(`/tasks/${taskId}/status`, { method: 'PATCH', body: JSON.stringify({ status, userId }), }); } // Members async getMembers(projectId: string): Promise { return this.request(`/projects/${projectId}/members`); } // Agent Control async enableAgent(projectId: string): Promise<{ message: string; project_id: string }> { return this.request(`/projects/${projectId}/agent/enable`, { method: 'POST' }); } async disableAgent(projectId: string): Promise<{ message: string; project_id: string }> { return this.request(`/projects/${projectId}/agent/disable`, { method: 'POST' }); } // Search & Activity async searchProject(projectId: string, query: string, filters?: { userId?: string; dateFrom?: string; dateTo?: string }) { return this.request(`/projects/${projectId}/search`, { method: 'POST', body: JSON.stringify({ query, filters }), }); } async getActivity(projectId: string, limit = 20): Promise { return this.request(`/projects/${projectId}/activity?limit=${limit}`); } // Smart Query async smartQuery(projectId: string, query: string, currentUserId: string) { return this.request(`/projects/${projectId}/smart-query`, { method: 'POST', body: JSON.stringify({ query, currentUserId, currentDatetime: new Date().toISOString(), }), }); } // Task Chat (Agent) async taskChat( projectId: string, taskId: string, userId: string, message: string, history: { role: string; content: string }[] ): Promise<{ message: string; taskCompleted?: boolean; taskStatus?: string }> { return this.request(`/tasks/${taskId}/chat`, { method: 'POST', body: JSON.stringify({ projectId, userId, message, history, currentDatetime: new Date().toISOString(), }), }); } } export const api = new ApiClient();