-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathcodex-app-server.ts
More file actions
395 lines (345 loc) · 10.7 KB
/
Copy pathcodex-app-server.ts
File metadata and controls
395 lines (345 loc) · 10.7 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
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
import { EventEmitter } from "node:events";
import { spawn, type ChildProcessWithoutNullStreams } from "node:child_process";
import type {
CodexChatRequest,
CodexChatResponse,
CodexConnectionStatus,
CodexInterruptTurnRequest,
CodexThread,
CodexThreadListResponse,
CodexThreadReadResponse,
CodexTurn,
JsonValue
} from "../shared/contracts.js";
type JsonObject = {
[key: string]: JsonValue;
};
type AppServerResponse = {
id: number;
result?: JsonValue;
error?: {
code?: number;
message?: string;
data?: JsonValue;
};
};
type PendingRequest = {
resolve: (value: JsonValue) => void;
reject: (error: Error) => void;
};
type CodexAppServerEvents = {
status: [CodexConnectionStatus];
notification: [method: string, params: JsonValue];
};
export declare interface CodexAppServerClient {
on<K extends keyof CodexAppServerEvents>(
eventName: K,
listener: (...args: CodexAppServerEvents[K]) => void
): this;
emit<K extends keyof CodexAppServerEvents>(
eventName: K,
...args: CodexAppServerEvents[K]
): boolean;
}
export class CodexAppServerClient extends EventEmitter {
#process: ChildProcessWithoutNullStreams | null = null;
#nextId = 1;
#stdoutBuffer = "";
#pending = new Map<number, PendingRequest>();
#connectPromise: Promise<CodexConnectionStatus> | null = null;
#thread: CodexThread | null = null;
#status: CodexConnectionStatus = {
state: "disconnected",
transport: "stdio",
detail: "Codex app-server is not connected."
};
getStatus(): CodexConnectionStatus {
return this.#status;
}
async connect(): Promise<CodexConnectionStatus> {
if (this.#status.state === "connected") {
return this.#status;
}
if (this.#connectPromise) {
return this.#connectPromise;
}
this.#connectPromise = this.#connect();
try {
return await this.#connectPromise;
} finally {
this.#connectPromise = null;
}
}
async sendChatMessage(request: CodexChatRequest): Promise<CodexChatResponse> {
await this.connect();
if (request.threadId && request.expectedTurnId) {
const result = await this.#request<JsonObject>("turn/steer", {
threadId: request.threadId,
input: [textInput(request.text)],
expectedTurnId: request.expectedTurnId
});
return {
threadId: request.threadId,
thread: this.#thread?.id === request.threadId ? this.#thread : { id: request.threadId },
turn: {
id: readString(result.turnId, "turnId"),
status: "inProgress"
},
steered: true
};
}
const thread = request.threadId
? await this.#resumeThread(request.threadId)
: await this.#startThread(request.cwd ?? null);
const result = await this.#request<JsonObject>("turn/start", {
threadId: thread.id,
input: [textInput(request.text)],
cwd: request.cwd ?? null
});
const turn = readObject(result.turn, "turn") as CodexTurn;
return {
threadId: thread.id,
thread,
turn
};
}
async listThreads(): Promise<CodexThreadListResponse> {
await this.connect();
const result = await this.#request<JsonObject>("thread/list", {
limit: 40,
sortKey: "updated_at",
sortDirection: "desc",
archived: false
});
return {
threads: Array.isArray(result.data) ? (result.data as CodexThread[]) : [],
nextCursor: typeof result.nextCursor === "string" ? result.nextCursor : null
};
}
async readThread(threadId: string): Promise<CodexThreadReadResponse> {
await this.connect();
const result = await this.#request<JsonObject>("thread/read", {
threadId,
includeTurns: true
});
const thread = readObject(result.thread, "thread") as CodexThread;
this.#thread = thread;
return { thread };
}
async interruptTurn(request: CodexInterruptTurnRequest): Promise<void> {
await this.connect();
await this.#request<JsonObject>("turn/interrupt", {
threadId: request.threadId,
turnId: request.turnId
});
}
dispose(): void {
for (const pending of this.#pending.values()) {
pending.reject(new Error("Codex app-server client disposed."));
}
this.#pending.clear();
if (this.#process && !this.#process.killed) {
this.#process.kill();
}
this.#process = null;
this.#setStatus({
state: "disconnected",
transport: "stdio",
detail: "Codex app-server stopped."
});
}
async #connect(): Promise<CodexConnectionStatus> {
this.#setStatus({
state: "connecting",
transport: "stdio",
detail: "Starting codex app-server."
});
const command = process.env.CODIO_CODEX_COMMAND ?? "codex";
this.#process = spawn(command, ["app-server", "--listen", "stdio://"], {
env: process.env,
stdio: ["pipe", "pipe", "pipe"]
});
this.#process.stdout.setEncoding("utf8");
this.#process.stderr.setEncoding("utf8");
this.#process.stdout.on("data", (chunk: string) => this.#handleStdout(chunk));
this.#process.stderr.on("data", (chunk: string) => this.#handleStderr(chunk));
this.#process.on("error", (error) => this.#handleExit(error));
this.#process.on("exit", (code, signal) => {
this.#handleExit(new Error(`codex app-server exited: code=${code ?? "null"} signal=${signal ?? "null"}`));
});
const initialize = await this.#request<JsonObject>("initialize", {
clientInfo: {
name: "codio",
title: "Codio",
version: "0.1.3"
},
capabilities: {
experimentalApi: true
}
});
this.#notify("initialized", {});
const codexHome = typeof initialize.codexHome === "string" ? initialize.codexHome : null;
const platformFamily =
typeof initialize.platformFamily === "string" ? initialize.platformFamily : null;
const platformOs = typeof initialize.platformOs === "string" ? initialize.platformOs : null;
const platform = [platformFamily, platformOs].filter(Boolean).join("/") || null;
this.#setStatus({
state: "connected",
transport: "stdio",
codexHome,
platform
});
return this.#status;
}
async #ensureThread(cwd: string | null): Promise<CodexThread> {
if (this.#thread) {
return this.#thread;
}
return this.#startThread(cwd);
}
async #startThread(cwd: string | null): Promise<CodexThread> {
const startResult = await this.#request<JsonObject>("thread/start", {
cwd: cwd ?? this.#defaultCwd(),
serviceName: "codio",
personality: "pragmatic",
ephemeral: false,
sessionStartSource: "startup",
experimentalRawEvents: false,
persistExtendedHistory: false
});
const thread = readObject(startResult.thread, "thread") as CodexThread;
this.#thread = thread;
return thread;
}
async #resumeThread(threadId: string): Promise<CodexThread> {
if (this.#thread?.id === threadId) {
return this.#thread;
}
const resumeResult = await this.#request<JsonObject>("thread/resume", {
threadId,
personality: "pragmatic",
persistExtendedHistory: false
});
const thread = readObject(resumeResult.thread, "thread") as CodexThread;
this.#thread = thread;
return thread;
}
#request<T extends JsonValue>(method: string, params?: JsonValue): Promise<T> {
if (!this.#process || !this.#process.stdin.writable) {
return Promise.reject(new Error("Codex app-server is not running."));
}
const id = this.#nextId++;
const payload = params === undefined ? { id, method } : { id, method, params };
return new Promise<T>((resolve, reject) => {
this.#pending.set(id, {
resolve: (value) => resolve(value as T),
reject
});
this.#process?.stdin.write(`${JSON.stringify(payload)}\n`, (error) => {
if (!error) {
return;
}
this.#pending.delete(id);
reject(error);
});
});
}
#notify(method: string, params: JsonObject): void {
this.#process?.stdin.write(`${JSON.stringify({ method, params })}\n`);
}
#handleStdout(chunk: string): void {
this.#stdoutBuffer += chunk;
let newlineIndex = this.#stdoutBuffer.indexOf("\n");
while (newlineIndex >= 0) {
const line = this.#stdoutBuffer.slice(0, newlineIndex).trim();
this.#stdoutBuffer = this.#stdoutBuffer.slice(newlineIndex + 1);
if (line) {
this.#handleMessageLine(line);
}
newlineIndex = this.#stdoutBuffer.indexOf("\n");
}
}
#handleMessageLine(line: string): void {
let parsed: JsonValue;
try {
parsed = JSON.parse(line) as JsonValue;
} catch {
return;
}
if (!isObject(parsed)) {
return;
}
if (typeof parsed.id === "number") {
this.#handleResponse(parsed as AppServerResponse);
return;
}
if (typeof parsed.method === "string") {
this.emit("notification", parsed.method, parsed.params ?? {});
}
}
#handleResponse(response: AppServerResponse): void {
const pending = this.#pending.get(response.id);
if (!pending) {
return;
}
this.#pending.delete(response.id);
if (response.error) {
pending.reject(new Error(response.error.message ?? `Codex app-server error ${response.id}`));
return;
}
pending.resolve(response.result ?? {});
}
#handleStderr(chunk: string): void {
const detail = chunk.trim();
if (!detail || this.#status.state === "connected") {
return;
}
this.#setStatus({
state: "connecting",
transport: "stdio",
detail
});
}
#handleExit(error: Error): void {
for (const pending of this.#pending.values()) {
pending.reject(error);
}
this.#pending.clear();
this.#process = null;
this.#thread = null;
this.#setStatus({
state: "disconnected",
transport: "stdio",
detail: error.message
});
}
#setStatus(status: CodexConnectionStatus): void {
this.#status = status;
this.emit("status", status);
}
#defaultCwd(): string {
return process.env.CODIO_WORKSPACE_CWD ?? process.env.INIT_CWD ?? process.cwd();
}
}
function isObject(value: JsonValue | undefined): value is JsonObject {
return typeof value === "object" && value !== null && !Array.isArray(value);
}
function readObject(value: JsonValue | undefined, label: string): JsonObject {
if (!isObject(value)) {
throw new Error(`Expected ${label} object from codex app-server.`);
}
return value;
}
function readString(value: JsonValue | undefined, label: string): string {
if (typeof value !== "string" || value.length === 0) {
throw new Error(`Expected ${label} string from codex app-server.`);
}
return value;
}
function textInput(text: string): JsonObject {
return {
type: "text",
text,
text_elements: []
};
}