File size: 4,963 Bytes
35765b5
 
698b2c1
35765b5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
698b2c1
35765b5
 
698b2c1
35765b5
 
 
 
 
 
 
 
698b2c1
 
 
 
 
 
 
 
 
35765b5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
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();
  }

  // User endpoints
  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');
  }

  // Project endpoints
  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 }),
    });
  }

  // Task endpoints
  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 }),
    });
  }

  // Members
  async getMembers(projectId: string): Promise<MembersResponse> {
    return this.request<MembersResponse>(`/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<ActivityResponse> {
    return this.request<ActivityResponse>(`/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();