-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathCapiProxy.ts
More file actions
53 lines (45 loc) · 1.82 KB
/
CapiProxy.ts
File metadata and controls
53 lines (45 loc) · 1.82 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
import { spawn } from "child_process";
import { resolve } from "path";
import { expect } from "vitest";
import { ParsedHttpExchange } from "../../../../test/harness/replayingCapiProxy";
const HARNESS_SERVER_PATH = resolve(__dirname, "../../../../test/harness/server.ts");
// Manages a child process that acts as a replaying proxy to the underlying AI endpoints
export class CapiProxy {
private proxyUrl: string | undefined;
async start(): Promise<string> {
const serverProcess = spawn("npx", ["tsx", HARNESS_SERVER_PATH], {
stdio: ["ignore", "pipe", "inherit"],
shell: true,
});
this.proxyUrl = await new Promise<string>((resolve) => {
serverProcess.stdout!.once("data", (chunk: Buffer) => {
const match = chunk.toString().match(/Listening: (http:\/\/[^\s]+)/);
resolve(match![1]);
});
});
return this.proxyUrl;
}
async updateConfig(config: {
filePath: string;
workDir: string;
testInfo?: { file: string; line?: number };
}): Promise<void> {
const response = await fetch(`${this.proxyUrl}/config`, {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify(config),
});
expect(response.ok).toBe(true);
}
async getExchanges(): Promise<ParsedHttpExchange[]> {
const response = await fetch(`${this.proxyUrl}/exchanges`, { method: "GET" });
return await response.json();
}
async stop(skipWritingCache?: boolean): Promise<void> {
const url = skipWritingCache
? `${this.proxyUrl}/stop?skipWritingCache=true`
: `${this.proxyUrl}/stop`;
const response = await fetch(url, { method: "POST" });
expect(response.ok).toBe(true);
}
}