Skip to content

Commit 8de2941

Browse files
committed
fix(codex): preserve external model providers
1 parent 92d9a9e commit 8de2941

4 files changed

Lines changed: 53 additions & 2 deletions

File tree

‎docs-site/src/content/docs/guides/codex-integration.md‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,18 @@ reapplies the configured name. Genuine upstream native names (e.g. `gpt-5.6-sol`
153153
"GPT-5.6-Sol") come from the pinned upstream snapshot and are never overridden by a custom display
154154
name.
155155

156+
### External provider managers
157+
158+
If `config.toml` already selects a provider other than `openai` or `opencodex`, OpenCodex leaves the
159+
file unchanged and skips Codex history migration. Tools that manage a custom provider often tag
160+
existing sessions with that provider id; replacing the active id can make those intact sessions
161+
disappear from Codex's history view.
162+
163+
Keep one tool as the owner of Codex provider configuration. To use OpenCodex behind an existing
164+
provider manager, point that manager at the local OpenAI-compatible endpoint
165+
`http://127.0.0.1:10100/v1`. To let OpenCodex inject routing directly, first switch Codex back to its
166+
built-in `openai` provider, then rerun `ocx start`.
167+
156168
### Catalog troubleshooting
157169

158170
If a model is missing from Codex, or the catalog order/visibility looks wrong, check in order:

‎src/codex/inject.ts‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,8 +365,18 @@ export async function injectCodexConfig(port: number, config?: OcxConfig, option
365365
return { success: false, message: `Codex config not found at ${CODEX_CONFIG_PATH}. Is Codex installed?` };
366366
}
367367

368-
writeJournal();
369368
const rawContent = readFileSync(CODEX_CONFIG_PATH, "utf-8");
369+
const activeProvider = readRootTomlString(rawContent, "model_provider");
370+
if (activeProvider && activeProvider !== "openai" && activeProvider !== "opencodex") {
371+
return {
372+
success: true,
373+
message: `⚠️ Codex routing NOT injected: config.toml selects the external model_provider ${tomlString(activeProvider)}.\n` +
374+
` OpenCodex preserves external provider configuration so existing ${tomlString(activeProvider)} session history stays visible.\n` +
375+
` Configure your provider manager to use http://${providerBaseHost(config?.hostname)}:${port}/v1, or switch Codex to the built-in openai provider and rerun 'ocx start'.`,
376+
};
377+
}
378+
379+
writeJournal();
370380
// EOL boundary: transforms below are LF-pure; preserve the file's dominant ending on write.
371381
const eol = dominantEol(rawContent);
372382
let content = applyEol(rawContent, "\n");

‎structure/02_config-and-codex-home.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ requires_openai_auth = true
3737
Root TOML keys must be written before the first `[table]`. Re-injection strips stale opencodex
3838
blocks, stale root context-window overrides, and stale opencodex catalog paths before rewriting.
3939

40+
If the root config selects a provider other than `openai` or `opencodex`, injection must leave the
41+
config byte-for-byte unchanged and skip history migration. External provider managers own that
42+
routing configuration, and replacing their provider id can hide otherwise intact Codex sessions.
43+
4044
`supports_websockets = true` is appended only when `websocketsEnabled(config)` returns true.
4145

4246
## Profile and fast tier

‎tests/codex-inject-integration.test.ts‎

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { describe, expect, test, beforeEach, afterEach } from "bun:test";
2-
import { mkdtempSync, rmSync, writeFileSync, readFileSync } from "node:fs";
2+
import { existsSync, mkdtempSync, rmSync, writeFileSync, readFileSync } from "node:fs";
33
import { spawnSync } from "node:child_process";
44
import { tmpdir } from "node:os";
55
import { dirname, join } from "node:path";
@@ -101,6 +101,31 @@ describe("injectCodexConfig integration (Design B)", () => {
101101
expect(config).not.toContain("# Auto-injected by opencodex\nopenai_base_url");
102102
});
103103

104+
test("external model provider stays byte-for-byte unchanged so its session history remains visible", () => {
105+
const original = [
106+
'model_provider = "custom"',
107+
'model = "third-party-model"',
108+
"",
109+
"[model_providers.custom]",
110+
'name = "Provider Manager"',
111+
'base_url = "https://gateway.example/v1"',
112+
'wire_api = "responses"',
113+
"requires_openai_auth = true",
114+
"",
115+
].join("\n");
116+
writeFileSync(join(codexHome, "config.toml"), original, "utf8");
117+
118+
const r = runInject(codexHome, ocxHome);
119+
expect(r.status).toBe(0);
120+
const result = JSON.parse(r.stdout);
121+
expect(result.success).toBe(true);
122+
expect(result.message).toContain("routing NOT injected");
123+
expect(result.message).toContain('external model_provider "custom"');
124+
125+
expect(readFileSync(join(codexHome, "config.toml"), "utf8")).toBe(original);
126+
expect(existsSync(join(codexHome, "opencodex.config.toml"))).toBe(false);
127+
});
128+
104129
test("non-loopback hostname still uses the legacy provider-table injection", () => {
105130
writeFileSync(join(codexHome, "config.toml"), 'model = "gpt-5.5"\n', "utf8");
106131

0 commit comments

Comments
 (0)