refactor: add Codex API response types and auto-resolve default model
This commit is contained in:
@@ -454,6 +454,25 @@ type RequestHandler = (params: unknown) => Promise<unknown> | unknown;
|
|||||||
|
|
||||||
type NotificationHandler = (method: string, params: unknown) => void;
|
type NotificationHandler = (method: string, params: unknown) => void;
|
||||||
|
|
||||||
|
// Codex app-server API response types
|
||||||
|
interface CodexModel {
|
||||||
|
id: string;
|
||||||
|
displayName?: string;
|
||||||
|
description?: string;
|
||||||
|
isDefault?: boolean;
|
||||||
|
model?: string;
|
||||||
|
defaultReasoningEffort?: string;
|
||||||
|
supportedReasoningEfforts?: string[];
|
||||||
|
}
|
||||||
|
|
||||||
|
interface CodexModelListResponse {
|
||||||
|
data?: CodexModel[];
|
||||||
|
}
|
||||||
|
|
||||||
|
interface CodexThreadStartResponse {
|
||||||
|
thread?: { id?: string };
|
||||||
|
}
|
||||||
|
|
||||||
class CodexAppServerClient {
|
class CodexAppServerClient {
|
||||||
private readonly rl: readline.Interface;
|
private readonly rl: readline.Interface;
|
||||||
private readonly pending = new Map<number, PendingRequest>();
|
private readonly pending = new Map<number, PendingRequest>();
|
||||||
@@ -1304,6 +1323,19 @@ class CodexAppServerAgentSession implements AgentSession {
|
|||||||
private async ensureThread(): Promise<void> {
|
private async ensureThread(): Promise<void> {
|
||||||
if (!this.client) return;
|
if (!this.client) return;
|
||||||
if (this.currentThreadId) return;
|
if (this.currentThreadId) return;
|
||||||
|
|
||||||
|
// Resolve model - if not specified, query available models and pick default
|
||||||
|
let model = this.config.model;
|
||||||
|
if (!model) {
|
||||||
|
const modelResponse = (await this.client.request("model/list", {})) as CodexModelListResponse;
|
||||||
|
const models = modelResponse?.data ?? [];
|
||||||
|
const defaultModel = models.find((m) => m.isDefault) ?? models[0];
|
||||||
|
if (!defaultModel) {
|
||||||
|
throw new Error("No models available from Codex app-server");
|
||||||
|
}
|
||||||
|
model = defaultModel.id;
|
||||||
|
}
|
||||||
|
|
||||||
const preset = MODE_PRESETS[this.currentMode] ?? MODE_PRESETS[DEFAULT_CODEX_MODE_ID];
|
const preset = MODE_PRESETS[this.currentMode] ?? MODE_PRESETS[DEFAULT_CODEX_MODE_ID];
|
||||||
const approvalPolicy = this.config.approvalPolicy ?? preset.approvalPolicy;
|
const approvalPolicy = this.config.approvalPolicy ?? preset.approvalPolicy;
|
||||||
const sandbox = this.config.sandboxMode ?? preset.sandbox;
|
const sandbox = this.config.sandboxMode ?? preset.sandbox;
|
||||||
@@ -1319,12 +1351,12 @@ class CodexAppServerAgentSession implements AgentSession {
|
|||||||
Object.assign(innerConfig, this.config.extra.codex);
|
Object.assign(innerConfig, this.config.extra.codex);
|
||||||
}
|
}
|
||||||
const response = (await this.client.request("thread/start", {
|
const response = (await this.client.request("thread/start", {
|
||||||
model: this.config.model ?? null,
|
model,
|
||||||
cwd: this.config.cwd ?? null,
|
cwd: this.config.cwd ?? null,
|
||||||
approvalPolicy,
|
approvalPolicy,
|
||||||
sandbox,
|
sandbox,
|
||||||
...(Object.keys(innerConfig).length > 0 ? { config: innerConfig } : {}),
|
...(Object.keys(innerConfig).length > 0 ? { config: innerConfig } : {}),
|
||||||
})) as { thread?: { id?: string } };
|
})) as CodexThreadStartResponse;
|
||||||
const threadId = response?.thread?.id;
|
const threadId = response?.thread?.id;
|
||||||
if (!threadId) {
|
if (!threadId) {
|
||||||
throw new Error("Codex app-server did not return thread id");
|
throw new Error("Codex app-server did not return thread id");
|
||||||
|
|||||||
Reference in New Issue
Block a user