-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathCodexAcpServer.ts
More file actions
408 lines (361 loc) · 15.5 KB
/
Copy pathCodexAcpServer.ts
File metadata and controls
408 lines (361 loc) · 15.5 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
396
397
398
399
400
401
402
403
404
405
406
407
408
import * as acp from "@agentclientprotocol/sdk";
import {
RequestError,
type SessionId,
type SessionModelState,
type SessionModeState
} from "@agentclientprotocol/sdk";
import {CodexEventHandler} from "./CodexEventHandler";
import {CodexApprovalHandler} from "./CodexApprovalHandler";
import {CodexAuthMethods, type CodexAuthRequest} from "./CodexAuthMethod";
import {CodexAcpClient, type SessionMetadata} from "./CodexAcpClient";
import type {Account, Model, RateLimitSnapshot} from "./app-server/v2";
import type {ReasoningEffort} from "./app-server";
import {ModelId} from "./ModelId";
import {AgentMode} from "./AgentMode";
import type {TokenCount} from "./TokenCount";
import {CodexCommands} from "./CodexCommands";
import type {QuotaMeta} from "./QuotaMeta";
import {logger} from "./Logger";
export interface SessionState {
sessionId: string,
currentModelId: string,
agentMode: AgentMode,
currentTurnId: string | null;
lastTokenUsage: TokenCount | null;
totalTokenUsage: TokenCount | null;
modelContextWindow: number | null;
rateLimits: RateLimitSnapshot | null;
account: Account | null;
cwd: string;
sessionMcpServers?: Array<string>;
}
export class CodexAcpServer implements acp.Agent {
private readonly codexAcpClient: CodexAcpClient;
private readonly connection: acp.AgentSideConnection;
private readonly defaultAuthRequest: CodexAuthRequest | null;
private readonly getExitCode: () => number | null;
private readonly availableCommands: CodexCommands;
private readonly sessions: Map<string, SessionState>;
constructor(
connection: acp.AgentSideConnection,
codexAcpClient: CodexAcpClient,
defaultAuthRequest?: CodexAuthRequest,
getExitCode?: () => number | null,
) {
this.sessions = new Map();
this.connection = connection;
this.codexAcpClient = codexAcpClient;
this.defaultAuthRequest = defaultAuthRequest ?? null;
this.getExitCode = getExitCode ?? (() => null);
this.availableCommands = new CodexCommands(
connection,
codexAcpClient,
(operation) => this.runWithProcessCheck(operation)
);
}
async initialize(
_params: acp.InitializeRequest,
): Promise<acp.InitializeResponse> {
logger.log("Initialize request received");
await this.runWithProcessCheck(() => this.codexAcpClient.initialize(_params));
return {
protocolVersion: acp.PROTOCOL_VERSION,
agentCapabilities: {
loadSession: false,
promptCapabilities: {
image: true
},
sessionCapabilities: {
resume: { }
},
mcpCapabilities: {
http: true,
sse: false
}
},
authMethods: CodexAuthMethods,
};
}
async checkAuthorization(){
const authNeeded = await this.runWithProcessCheck(() => this.codexAcpClient.authRequired());
logger.log("Auth requirement checked", {authRequired: authNeeded});
if (authNeeded) {
if (this.defaultAuthRequest) {
logger.log("Authenticating with default auth request...", {
authRequest: this.defaultAuthRequest
});
await this.authenticate(this.defaultAuthRequest)
logger.log("Authentication completed");
} else {
logger.log("Authentication required but no default auth request provided, return to IDE");
throw RequestError.authRequired();
}
}
}
async getOrCreateSession(request: acp.NewSessionRequest | acp.ResumeSessionRequest): Promise<[SessionId, SessionModelState, SessionModeState]> {
await this.checkAuthorization();
const pendingMcpServers: Promise<Array<string>> = this.codexAcpClient.awaitMcpServers();
let sessionMetadata: SessionMetadata;
if ("sessionId" in request) {
logger.log(`Resume existing session: ${request.sessionId}...`)
sessionMetadata = await this.runWithProcessCheck(() => this.codexAcpClient.resumeSession(request));
} else {
logger.log(`Create new session...`)
sessionMetadata = await this.runWithProcessCheck(() => this.codexAcpClient.newSession(request));
}
const accountResponse = await this.runWithProcessCheck(() => this.codexAcpClient.getAccount());
const {sessionId, currentModelId, models} = sessionMetadata;
logger.log(`Waiting MCP servers to start...`)
const sessionMcpServers = await pendingMcpServers;
const sessionState: SessionState = {
sessionId: sessionId,
currentModelId: currentModelId,
agentMode: AgentMode.getInitialAgentMode(),
currentTurnId: null,
lastTokenUsage: null,
totalTokenUsage: null,
modelContextWindow: null,
rateLimits: null,
account: accountResponse.account,
cwd: request.cwd,
sessionMcpServers: sessionMcpServers,
}
this.sessions.set(sessionId, sessionState);
this.publishAvailableCommandsAsync(sessionId);
const sessionModelState: SessionModelState = this.createModelState(models, currentModelId);
const sessionModeState: SessionModeState = sessionState.agentMode.toSessionModeState();
return [sessionId, sessionModelState, sessionModeState];
}
async unstable_resumeSession(params: acp.ResumeSessionRequest): Promise<acp.ResumeSessionResponse> {
logger.log("Resuming session...", {sessionId: params.sessionId});
const [sessionId, modelState, modeState] = await this.getOrCreateSession(params);
logger.log("Session resumed", {
sessionId: sessionId,
modelId: modelState.currentModelId,
availableModelCount: modelState.availableModels.length
});
return {
models: modelState,
modes: modeState
};
}
async newSession(
params: acp.NewSessionRequest,
): Promise<acp.NewSessionResponse> {
logger.log("Starting new session...");
const [sessionId, modelState, modeState] = await this.getOrCreateSession(params);
logger.log("New session created", {
sessionId: sessionId,
modelId: modelState.currentModelId,
availableModelCount: modelState.availableModels.length
});
return {
sessionId: sessionId,
models: modelState,
modes: modeState
};
}
async authenticate(
_params: acp.AuthenticateRequest,
): Promise<acp.AuthenticateResponse> {
logger.log("Authenticate request received");
const isAuthenticated = await this.runWithProcessCheck(() => this.codexAcpClient.authenticate(_params));
if (!isAuthenticated) {
logger.log("Authenticate request failed");
throw RequestError.invalidParams();
}
logger.log("Authenticate request completed");
return { };
}
async setSessionMode(
_params: acp.SetSessionModeRequest,
): Promise<acp.SetSessionModeResponse> {
logger.log("Set session mode requested", {
sessionId: _params.sessionId,
modeId: _params.modeId
});
const sessionState = this.sessions.get(_params.sessionId);
if (!sessionState) throw new Error(`Session ${_params.sessionId} not found`);
const newMode = AgentMode.find(_params.modeId);
if (!newMode) {
throw RequestError.invalidParams();
}
sessionState.agentMode = newMode;
return {};
}
async unstable_setSessionModel(params: acp.SetSessionModelRequest): Promise<acp.SetSessionModelResponse | void> {
logger.log("Set session model requested", {
sessionId: params.sessionId,
modelId: params.modelId
});
const sessionState = this.sessions.get(params.sessionId);
if (!sessionState) throw new Error(`Session ${params.sessionId} not found`);
const requestedModelId= ModelId.fromString(params.modelId);
const requestedModelName = requestedModelId.model;
const requestedEffort = requestedModelId.effort;
const models = await this.codexAcpClient.fetchAvailableModels();
const model = models.find(m => m.id === requestedModelName);
if (!model) throw new Error(`Unknown model ${params.modelId}`);
const requestedEffortValue = requestedEffort as ReasoningEffort | undefined;
let reasoningEffort: ReasoningEffort;
if (requestedEffortValue) {
const matchedEffort = model.supportedReasoningEfforts.find(
(option) => option.reasoningEffort === requestedEffortValue
)?.reasoningEffort;
if (!matchedEffort) {
throw new Error(`Unsupported reasoning effort ${requestedEffortValue} for model ${requestedModelName}`);
}
reasoningEffort = matchedEffort;
} else {
reasoningEffort = model.defaultReasoningEffort;
}
sessionState.currentModelId = ModelId.fromComponents(model, reasoningEffort).toString();
return {};
}
private publishAvailableCommandsAsync(sessionId: string) {
void this.availableCommands.publish(sessionId);
}
private createModelState(availableModels: Model[], selectedModelId: string): SessionModelState {
const allowedModels = availableModels
.flatMap((model) =>
model.supportedReasoningEfforts.map((effort) => ({
modelId: ModelId.fromComponents(model, effort.reasoningEffort).toString(),
name: `${model.displayName} (${effort.reasoningEffort})`,
description: `${model.description} ${effort.description}`,
}))
);
return {
availableModels: allowedModels,
currentModelId: selectedModelId,
}
}
getSessionState(sessionId: string): SessionState {
const sessionState = this.sessions.get(sessionId);
if (!sessionState) {
throw new Error(`Session ${sessionId} not found`);
}
return sessionState;
}
async prompt(params: acp.PromptRequest): Promise<acp.PromptResponse> {
logger.log("Prompt received", {
sessionId: params.sessionId,
prompt: params.prompt,
});
const sessionState = this.getSessionState(params.sessionId);
sessionState.currentTurnId = null;
sessionState.lastTokenUsage = null;
try {
const eventHandler = new CodexEventHandler(this.connection, sessionState);
const approvalHandler = new CodexApprovalHandler(this.connection, sessionState);
await this.codexAcpClient.subscribeToSessionEvents(params.sessionId,
(event) => eventHandler.handleNotification(event),
approvalHandler);
if (await this.availableCommands.tryHandle(params.prompt, sessionState)) {
logger.log("Prompt handled by a command");
return {
stopReason: "end_turn",
_meta: this.buildQuotaMeta(sessionState),
};
}
const disableSummary = sessionState.account?.type === "apiKey"
if (disableSummary) {
logger.log("Disable reasoning.summary because API key is used", {sessionId: params.sessionId});
}
const agentMode = sessionState.agentMode;
const modelId = ModelId.fromString(sessionState.currentModelId);
const turnCompleted = await this.runWithProcessCheck(
() => this.codexAcpClient.sendPrompt(params, agentMode, modelId, disableSummary));
// Check if turn was interrupted (cancelled)
if (turnCompleted.turn.status === "interrupted") {
await this.connection.sessionUpdate({
sessionId: params.sessionId,
update: {
sessionUpdate: "agent_message_chunk",
content: {
type: "text",
text: "*Conversation interrupted*"
}
}
});
return {
stopReason: "cancelled",
_meta: this.buildQuotaMeta(sessionState),
};
}
const error = eventHandler.getFailure()
if (error) {
// noinspection ExceptionCaughtLocallyJS
throw error;
}
return {
stopReason: "end_turn",
_meta: this.buildQuotaMeta(sessionState),
};
} catch (err) {
logger.error(`Prompt for session ${params.sessionId} failed`, err);
throw err;
} finally {
logger.log("Prompt completed", {sessionId: params.sessionId});
sessionState.currentTurnId = null;
}
}
private buildQuotaMeta(sessionState: SessionState): { quota: QuotaMeta } {
const lastTokenUsage = sessionState.lastTokenUsage;
// Remove the "[reasoning-level]" suffix from currentModelId if present
const modelName = sessionState.currentModelId.replace(/\[.*?]$/, '');
// FIXME: currently all tokens are reported for the current model
const modelUsage = (lastTokenUsage != null)
? [{ model: modelName, token_count: lastTokenUsage }]
: [];
return {
quota: {
token_count: sessionState.lastTokenUsage,
model_usage: modelUsage
}
};
}
private async runWithProcessCheck<T>(operation: () => Promise<T>): Promise<T> {
try {
return await operation();
} catch (err) {
const exitCode = this.getExitCode();
const requestErrorCode = 1001 // Just some magic number
if (exitCode == 3221225781) {
throw new RequestError(requestErrorCode, `VC++ redistributable should be installed`);
}
if (exitCode !== null) {
throw new RequestError(requestErrorCode, `Codex process has exited with code ${exitCode}`);
}
throw err;
}
}
async cancel(params: acp.CancelNotification): Promise<void> {
const sessionState = this.sessions.get(params.sessionId);
if (!sessionState) {
logger.log("Cancel request rejected: session not found", {sessionId: params.sessionId});
return;
}
if (!sessionState.currentTurnId) {
logger.log("Cancel request rejected: no current turn", {sessionId: params.sessionId});
return;
}
logger.log("Cancel session requested", {
sessionId: params.sessionId,
currentTurnId: sessionState.currentTurnId
});
try {
// After turnInterrupt(), Codex will send turn/completed event, which will naturally complete awaitTurnCompleted()
await this.codexAcpClient.turnInterrupt({
threadId: params.sessionId,
turnId: sessionState.currentTurnId
});
logger.log("Cancel - turnInterrupt succeeded", {
sessionId: params.sessionId,
currentTurnId: sessionState.currentTurnId
});
} catch (err) {
logger.error(`Cancel - turnInterrupt failed`, err);
}
}
}