-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.ts
More file actions
77 lines (63 loc) · 2.11 KB
/
api.ts
File metadata and controls
77 lines (63 loc) · 2.11 KB
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
// Add this import at the top of the file
import fetch from 'node-fetch'; // For Node < 18
// OR
// import { fetch } from 'undici'; // For Node ≥ 18
// src/api.ts
const API_BASE = 'http://localhost:5001';
import { Context, Session, SessionEndResponse, SessionEvent, SessionSummary } from './types';
class ApiClient {
private async fetchApi<T>(endpoint: string, options: {
method?: string;
headers?: Record<string, string>;
body?: string;
} = {}): Promise<T> {
const response = await fetch(`${API_BASE}${endpoint}`, {
...options,
headers: {
'Content-Type': 'application/json',
...options.headers,
},
});
if (!response.ok) {
throw new Error(`API Error: ${response.statusText}`);
}
return response.json() as Promise<T>;
}
async createContext(name: string, description?: string): Promise<Context> {
return this.fetchApi('/context', {
method: 'POST',
body: JSON.stringify({ name, description }),
});
}
async startSession(contextId: string): Promise<Session> {
return this.fetchApi('/session', {
method: 'POST',
body: JSON.stringify({ context_id: contextId }),
});
}
async endSession(sessionId: number): Promise<SessionEndResponse> {
return this.fetchApi(`/session/${sessionId}/end`, {
method: 'POST',
});
}
async getSessionEvents(sessionId: number): Promise<SessionEvent[]> {
return this.fetchApi(`/session/${sessionId}/events`);
}
async generateSummary(sessionId: number): Promise<SessionSummary> {
return this.fetchApi(`/session/${sessionId}/summary`);
}
async getActiveSessions(): Promise<{ active_sessions: Session[]; count: number }> {
return this.fetchApi('/sessions/active');
}
async getContexts(): Promise<Context[]> {
return this.fetchApi('/context/list');
}
async saveSession(sessionId: number,instructions: string): Promise<{ path: string }> {
return this.fetchApi(`/session/${sessionId}/save`, {
method: 'POST',
body: JSON.stringify({ instructions }),
});
}
}
// Export singleton instance
export const api = new ApiClient();