-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathCodexJsonRpcConnection.ts
More file actions
53 lines (42 loc) · 1.8 KB
/
Copy pathCodexJsonRpcConnection.ts
File metadata and controls
53 lines (42 loc) · 1.8 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 * as rpc from "vscode-jsonrpc/node";
import type {MessageConnection} from "vscode-jsonrpc/node";
import type {ChildProcessWithoutNullStreams} from "node:child_process";
import {spawn} from "node:child_process";
import {createJSONRPCReader, createJSONRPCWriter} from "./StdUtils";
import {logger} from "./Logger";
export interface CodexConnection {
readonly connection: MessageConnection
readonly process: ChildProcessWithoutNullStreams;
}
export function startCodexConnection(codexPath: string, env?: NodeJS.ProcessEnv): CodexConnection {
const spawnEnv = env ?? process.env;
const codex: ChildProcessWithoutNullStreams = process.platform === 'win32'
? spawn(`"${codexPath}" app-server`, { shell: true, env: spawnEnv })
: spawn(codexPath, ['app-server'], { env: spawnEnv });
attachLogs(codex);
const reader = createJSONRPCReader(codex.stdout);
const writer = createJSONRPCWriter(codex.stdin);
let connection = rpc.createMessageConnection(reader, writer);
connection.listen();
// Terminate all current activities on process termination
codex.on("exit", _ => {
connection.dispose();
});
return {connection: connection, process: codex};
}
function attachLogs(proc: ChildProcessWithoutNullStreams) {
const originalWrite = proc.stdin.write.bind(proc.stdin);
proc.stdin.write = (chunk: any, encoding?: any, callback?: any): boolean => {
logger.log(`[IN] ${chunk.toString()}`);
return originalWrite(chunk, encoding, callback);
};
proc.stderr.on("data", (data) => {
logger.log(`[ERR] ${data.toString()}`);
});
proc.stdout.on("data", (data: Buffer) => {
logger.log(`[OUT] ${data.toString()}`);
});
proc.on("exit", (code) => {
logger.log(`[EXIT] code: ${code?.toString()}`);
});
}