MCPcopy Create free account
hub / github.com/TraderAlice/OpenAlice / writeAiConfig

Function writeAiConfig

src/workspaces/adapters/codex.ts:135–179  ·  view source on GitHub ↗
(cwd: string, cred: WorkspaceAiCred)

Source from the content-addressed store, hash-verified

133 },
134
135 async writeAiConfig(cwd: string, cred: WorkspaceAiCred): Promise<void> {
136 const hasProvider = !!(cred.baseUrl || cred.model);
137
138 if (!hasProvider) {
139 // Reset: tear down the workspace's entire `.codex/` directory. The
140 // adapter's `composeEnv` won't set `CODEX_HOME` when the directory is
141 // absent, so codex falls back to the user's global `~/.codex/`. We
142 // don't leave empty stubs behind — workspace files exist only when
143 // there's an actual override. Note: `CODEX_HOME` is exclusive (not a
144 // merge layer), so a half-empty `.codex/` would *shadow* the user's
145 // global login and break auth. Full teardown is the only safe reset.
146 const codexDir = join(cwd, '.codex');
147 await rm(codexDir, { recursive: true, force: true });
148 return;
149 }
150
151 // Provider override. config.toml carries only model / model_provider /
152 // [model_providers.*] — the OpenAlice MCP server entries are wired per-spawn
153 // via this adapter's `-c mcp_servers...url=...` flags, so we
154 // don't repeat it here.
155 let toml = '';
156 if (cred.model) toml += `model = ${tomlString(cred.model)}\n`;
157 if (cred.baseUrl) toml += `model_provider = "${CODEX_PROVIDER_NAME}"\n`;
158 if (cred.baseUrl) {
159 toml += '\n';
160 toml += `[model_providers.${CODEX_PROVIDER_NAME}]\n`;
161 toml += `name = "OpenAlice workspace provider"\n`;
162 toml += `base_url = ${tomlString(cred.baseUrl)}\n`;
163 toml += `env_key = "${CODEX_KEY_ENV_NAME}"\n`;
164 // Codex 0.130+ only speaks the OpenAI Responses API — it hard-rejects
165 // wire_api="chat" — so this is always "responses" regardless of the
166 // credential's wireShape. See memory reference_codex_chat_dead.
167 toml += `wire_api = "responses"\n`;
168 }
169 await writeWorkspaceFile(cwd, CODEX_CONFIG_PATH, toml);
170
171 // env.json: holds the per-workspace API key codex picks up via env_key.
172 // composeEnv reads this and exports at spawn.
173 if (cred.apiKey) {
174 const envObj: Record<string, string> = { [CODEX_KEY_ENV_NAME]: cred.apiKey };
175 await writeWorkspaceFile(cwd, CODEX_ENV_PATH, JSON.stringify(envObj, null, 2) + '\n');
176 } else {
177 await writeWorkspaceFile(cwd, CODEX_ENV_PATH, '{}\n');
178 }
179 },
180
181 async readAiConfig(cwd: string): Promise<WorkspaceAiCred | null> {
182 const tomlRaw = await readWorkspaceFile(cwd, CODEX_CONFIG_PATH);

Callers

nothing calls this directly

Calls 2

tomlStringFunction · 0.85
writeWorkspaceFileFunction · 0.85

Tested by

no test coverage detected