Skip to content

Commit 9a6835b

Browse files
authorization: add support for custom model provider
1 parent 2bca0ca commit 9a6835b

2 files changed

Lines changed: 26 additions & 15 deletions

File tree

src/CodexACPAgent.ts

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import * as acp from "@agentclientprotocol/sdk";
22
import type {MessageConnection} from "vscode-jsonrpc/node";
3-
import type {NewConversationResponse} from "./app-server";
43
import {CodexClient} from "./CodexClient";
54
import {CodexEventHandler} from "./CodexEventHandler";
5+
import type {JsonValue} from "./app-server/serde_json/JsonValue";
66

77
export interface SessionState {
88
sessionId: string,
@@ -11,24 +11,29 @@ export interface SessionState {
1111
}
1212

1313
export class CodexACPAgent implements acp.Agent {
14-
private readonly sessions: Map<string, SessionState>;
1514
private readonly codexClient: CodexClient;
1615
private readonly connection: acp.AgentSideConnection;
16+
private readonly config: JsonObject | null;
17+
private readonly modelProvider: string | null;
18+
19+
private readonly sessions: Map<string, SessionState>;
1720

18-
constructor(connection: acp.AgentSideConnection, codexConnection: MessageConnection) {
21+
constructor(connection: acp.AgentSideConnection, codexConnection: MessageConnection, codexConfig?: JsonObject, modelProvider?: string) {
1922
this.sessions = new Map();
2023
this.codexClient = new CodexClient(codexConnection);
2124
this.connection = connection;
25+
this.config = codexConfig ?? null;
26+
this.modelProvider = modelProvider ?? null;
2227
}
2328

2429
async initialize(
2530
_params: acp.InitializeRequest,
2631
): Promise<acp.InitializeResponse> {
2732
await this.codexClient.initialize({
2833
clientInfo: {
29-
name: "CodexConsoleClient",
30-
version: "0.1.0",
31-
title: "Codex ACP"
34+
name: _params.clientInfo?.name ?? "CodexACPAgent",
35+
version: _params.clientInfo?.version ?? "0.1.0",
36+
title: _params.clientInfo?.title ?? "Codex ACP"
3237
}
3338
});
3439
return {
@@ -42,13 +47,13 @@ export class CodexACPAgent implements acp.Agent {
4247
async newSession(
4348
_params: acp.NewSessionRequest,
4449
): Promise<acp.NewSessionResponse> {
45-
const threadStartResponse = await this.codexClient.threadStart({
50+
const threadStartResponse = await this.codexClient.threadStart({
51+
config: this.config,
52+
modelProvider: this.modelProvider,
4653
model: null,
47-
modelProvider: null,
4854
cwd: _params.cwd,
4955
approvalPolicy: "never",
5056
sandbox: null,
51-
config: null,
5257
baseInstructions: null,
5358
developerInstructions: null,
5459
})
@@ -121,7 +126,7 @@ export class CodexACPAgent implements acp.Agent {
121126

122127
await this.codexClient.turnStart({
123128
threadId: sessionState.sessionId,
124-
input: [ {type: "text", text: prompt} ],
129+
input: [{type: "text", text: prompt}],
125130
approvalPolicy: null,
126131
sandboxPolicy: null,
127132
summary: null,
@@ -138,4 +143,6 @@ export class CodexACPAgent implements acp.Agent {
138143
await this.codexClient.close()
139144
this.sessions.get(params.sessionId)?.pendingPrompt?.abort();
140145
}
141-
}
146+
}
147+
148+
export type JsonObject = { [key in string]?: JsonValue }

src/index.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ const logPath = process.env["APP_SERVER_LOGS"];
1111
const appServerConnection = startCodexConnection(codexPath, logPath)
1212
const acpJsonStream = createJsonStream(process.stdin, process.stdout);
1313

14-
new acp.AgentSideConnection(
15-
(acpConnection) => new CodexACPAgent(acpConnection, appServerConnection),
16-
acpJsonStream
17-
);
14+
function createAgent(connection: acp.AgentSideConnection): CodexACPAgent {
15+
const configString = process.env["CODEX_CONFIG"];
16+
const config = configString ? JSON.parse(configString) : undefined;
17+
const modelProvider = process.env["MODEL_PROVIDER"];
18+
return new CodexACPAgent(connection, appServerConnection, config, modelProvider);
19+
}
20+
21+
new acp.AgentSideConnection(createAgent, acpJsonStream);

0 commit comments

Comments
 (0)