|
|
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<T>(endpoint: string, options?: RequestInit): Promise<T> { |
|
|
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(); |
|
|
} |
|
|
|
|
|
|
|
|
async createUser(data: UserCreate): Promise<User> { |
|
|
return this.request<User>('/users', { |
|
|
method: 'POST', |
|
|
body: JSON.stringify(data), |
|
|
}); |
|
|
} |
|
|
|
|
|
async getUser(userId: string): Promise<User> { |
|
|
return this.request<User>(`/users/${userId}`); |
|
|
} |
|
|
|
|
|
async listUsers(): Promise<User[]> { |
|
|
return this.request<User[]>('/users'); |
|
|
} |
|
|
|
|
|
|
|
|
async checkProjectAvailability(projectId: string): Promise<ProjectAvailability> { |
|
|
return this.request<ProjectAvailability>(`/projects/check/${projectId}`); |
|
|
} |
|
|
|
|
|
async listProjects(userId: string): Promise<ProjectsResponse> { |
|
|
return this.request<ProjectsResponse>(`/projects?userId=${userId}`); |
|
|
} |
|
|
|
|
|
async createProject(data: ProjectCreate): Promise<Project> { |
|
|
return this.request<Project>('/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 }), |
|
|
}); |
|
|
} |
|
|
|
|
|
|
|
|
async listTasks(projectId: string, status?: string): Promise<TasksResponse> { |
|
|
const query = status ? `?status=${status}` : ''; |
|
|
return this.request<TasksResponse>(`/projects/${projectId}/tasks${query}`); |
|
|
} |
|
|
|
|
|
async createTask(projectId: string, data: TaskCreate): Promise<Task> { |
|
|
return this.request<Task>(`/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<Task> { |
|
|
return this.request<Task>(`/tasks/${taskId}/status`, { |
|
|
method: 'PATCH', |
|
|
body: JSON.stringify({ status, userId }), |
|
|
}); |
|
|
} |
|
|
|
|
|
|
|
|
async getMembers(projectId: string): Promise<MembersResponse> { |
|
|
return this.request<MembersResponse>(`/projects/${projectId}/members`); |
|
|
} |
|
|
|
|
|
|
|
|
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' }); |
|
|
} |
|
|
|
|
|
|
|
|
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<ActivityResponse> { |
|
|
return this.request<ActivityResponse>(`/projects/${projectId}/activity?limit=${limit}`); |
|
|
} |
|
|
|
|
|
|
|
|
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(), |
|
|
}), |
|
|
}); |
|
|
} |
|
|
|
|
|
|
|
|
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(); |
|
|
|