-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathclient.ts
More file actions
57 lines (52 loc) · 1.88 KB
/
client.ts
File metadata and controls
57 lines (52 loc) · 1.88 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
import { z } from 'zod';
import { getLogger } from './logger.js';
const GetModelResponseSchema = z.object({
model: z.unknown(),
});
const SendModelResponseSchema = z.object({
model: z.any(),
corrections: z.array(z.string()),
correctionCount: z.number(),
});
export class AgentClient {
constructor(
private serverUrl: string,
private apiKey: string,
) {}
async getModel(workspaceId: string): Promise<unknown> {
const log = getLogger();
log.info('client', `GET model for workspace ${workspaceId}`);
const response = await fetch(`${this.serverUrl}/api/agent/model/${workspaceId}`, {
headers: { Authorization: `Bearer ${this.apiKey}` },
});
if (!response.ok) {
log.error('client', `GET model failed: ${response.status} ${response.statusText}`);
throw new Error(`Failed to get model: ${response.status} ${response.statusText}`);
}
const data = GetModelResponseSchema.parse(await response.json());
log.info('client', 'GET model success');
return data.model;
}
async sendModel(
workspaceId: string,
model: unknown,
): Promise<z.infer<typeof SendModelResponseSchema>> {
const log = getLogger();
log.info('client', `POST model for workspace ${workspaceId}`);
const response = await fetch(`${this.serverUrl}/api/agent/model/${workspaceId}`, {
method: 'POST',
headers: {
Authorization: `Bearer ${this.apiKey}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({ model }),
});
if (!response.ok) {
log.error('client', `POST model failed: ${response.status} ${response.statusText}`);
throw new Error(`Failed to send model: ${response.status} ${response.statusText}`);
}
const result = SendModelResponseSchema.parse(await response.json());
log.info('client', `POST model success, ${result.correctionCount} corrections`);
return result;
}
}