Skip to content

Commit fcf8ea3

Browse files
update from deprecated api to turn/start
1 parent 26fca07 commit fcf8ea3

4 files changed

Lines changed: 220 additions & 243 deletions

File tree

src/CodexACPAgent.ts

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ export interface SessionState {
1313
export class CodexACPAgent implements acp.Agent {
1414
private readonly sessions: Map<string, SessionState>;
1515
private readonly codexClient: CodexClient;
16-
private readonly messageHandler: CodexEventHandler;
16+
private readonly connection: acp.AgentSideConnection;
1717

1818
constructor(connection: acp.AgentSideConnection, codexConnection: MessageConnection) {
1919
this.sessions = new Map();
2020
this.codexClient = new CodexClient(codexConnection);
21-
this.messageHandler = new CodexEventHandler(connection);
21+
this.connection = connection;
2222
}
2323

2424
async initialize(
@@ -42,21 +42,18 @@ export class CodexACPAgent implements acp.Agent {
4242
async newSession(
4343
_params: acp.NewSessionRequest,
4444
): Promise<acp.NewSessionResponse> {
45-
const newConversationResponse: NewConversationResponse = await this.codexClient.newConversation({
45+
const threadStartResponse = await this.codexClient.threadStart({
4646
model: null,
4747
modelProvider: null,
48-
profile: null,
4948
cwd: _params.cwd,
5049
approvalPolicy: "never",
5150
sandbox: null,
5251
config: null,
5352
baseInstructions: null,
5453
developerInstructions: null,
55-
compactPrompt: null,
56-
includeApplyPatchTool: null,
5754
})
5855

59-
const sessionId = newConversationResponse.conversationId;
56+
const sessionId = threadStartResponse.thread.id;
6057
this.sessions.set(sessionId, {
6158
sessionId: sessionId,
6259
seenReasoningDeltas: false,
@@ -116,23 +113,21 @@ export class CodexACPAgent implements acp.Agent {
116113
sessionState: SessionState,
117114
prompt: string
118115
): Promise<void> {
119-
await this.codexClient.addConversationListener({
120-
conversationId: sessionState.sessionId,
121-
experimentalRawEvents: false,
122-
})
116+
const messageHandler = new CodexEventHandler(this.connection, sessionState);
117+
118+
this.codexClient.onServerNotification(notification => {
119+
messageHandler.handleNotification(notification);
120+
});
123121

124-
this.codexClient.onMessageEvent(event => this.messageHandler.handleEvent(sessionState, event));
125-
126-
await this.codexClient.sendUserMessage({
127-
conversationId: sessionState.sessionId,
128-
items: [
129-
{
130-
type: "text",
131-
data: {
132-
text: prompt
133-
}
134-
}
135-
]
122+
await this.codexClient.turnStart({
123+
threadId: sessionState.sessionId,
124+
input: [ {type: "text", text: prompt} ],
125+
approvalPolicy: null,
126+
sandboxPolicy: null,
127+
summary: null,
128+
cwd: null,
129+
effort: null,
130+
model: null,
136131
})
137132

138133
await this.codexClient.waitForCompletion()

src/CodexClient.ts

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,15 @@ import type {
55
InitializeParams,
66
InitializeResponse,
77
NewConversationParams,
8-
NewConversationResponse, SendUserMessageParams, SendUserMessageResponse, TaskCompleteEvent
8+
NewConversationResponse, SendUserMessageParams, SendUserMessageResponse, ServerNotification, TaskCompleteEvent
99
} from "./app-server";
10+
import type {
11+
ThreadStartParams,
12+
ThreadStartResponse,
13+
TurnCompletedNotification,
14+
TurnStartParams,
15+
TurnStartResponse
16+
} from "./app-server/v2";
1017

1118
export class CodexClient {
1219
readonly connection: MessageConnection;
@@ -15,39 +22,31 @@ export class CodexClient {
1522
this.connection = connection;
1623
}
1724

18-
async initialize(params: InitializeParams) {
19-
const response: InitializeResponse = await this.connection.sendRequest("initialize", params)
20-
return response;
21-
}
22-
23-
async newConversation(params: NewConversationParams){
24-
const response: NewConversationResponse = await this.connection.sendRequest("newConversation", params)
25-
return response;
25+
async initialize(params: InitializeParams): Promise<InitializeResponse> {
26+
return await this.sendRequest({ method: "initialize", params: params });
2627
}
2728

28-
async sendUserMessage(params: SendUserMessageParams) {
29-
const response: SendUserMessageResponse = await this.connection.sendRequest("sendUserMessage", params)
30-
return response;
29+
async turnStart(params: TurnStartParams): Promise<TurnStartResponse> {
30+
return await this.sendRequest({ method: "turn/start", params: params });
3131
}
3232

33-
async addConversationListener(params: AddConversationListenerParams){
34-
const response: AddConversationSubscriptionResponse = await this.connection.sendRequest("addConversationListener", params)
35-
return response;
33+
async threadStart(params: ThreadStartParams): Promise<ThreadStartResponse> {
34+
return await this.sendRequest({ method: "thread/start", params: params });
3635
}
3736

38-
async waitForCompletion(){
39-
await new Promise((resolve) => {
40-
this.connection.onNotification("codex/event/task_complete", (event: TaskCompleteEvent) => {
37+
async waitForCompletion(): Promise<TurnCompletedNotification> {
38+
return await new Promise((resolve) => {
39+
this.connection.onNotification("turn/completed", (event: TurnCompletedNotification) => {
4140
resolve(event);
4241
});
4342
});
4443
}
4544

46-
onMessageEvent(callback: (event: EventMsg) => void){
45+
onServerNotification(callback: (event: ServerNotification) => void){
4746
this.connection.onUnhandledNotification((data) => {
48-
const event = this.getEventMessage(data);
49-
if (event) {
50-
callback(event)
47+
const serverNotification = data as ServerNotification ?? null;
48+
if (serverNotification) {
49+
callback(serverNotification)
5150
}
5251
});
5352
}
@@ -70,4 +69,9 @@ export class CodexClient {
7069
this.connection.end();
7170
}
7271

73-
}
72+
private async sendRequest<R>(request: CodexRequest): Promise<R> {
73+
return await this.connection.sendRequest(request.method, request.params);
74+
}
75+
}
76+
77+
type CodexRequest = Omit<ClientRequest, "id">;

0 commit comments

Comments
 (0)