openai/codex-action
Publicmirrored from https://github.com/openai/codex-actionAvailable
src/writeProxyConfig.ts
38lines · modeblame
1c44f3d7Michael Bolin9 months ago | 1 | import * as fs from "node:fs/promises"; |
| 2 | import * as path from "node:path"; | |
| 3 | | |
| 4 | const MODEL_PROVIDER = "openai-proxy"; | |
| 5 | | |
| 6 | export async function writeProxyConfig( | |
| 7 | codexHome: string, | |
| 8 | port: number | |
| 9 | ): Promise<void> { | |
| 10 | const configPath = path.join(codexHome, "config.toml"); | |
| 11 | | |
| 12 | let existing = ""; | |
| 13 | try { | |
| 14 | existing = await fs.readFile(configPath, "utf8"); | |
| 15 | } catch { | |
| 16 | existing = ""; | |
| 17 | } | |
| 18 | | |
| 19 | const header = `# Added by codex-action. | |
| 20 | model_provider = "${MODEL_PROVIDER}" | |
| 21 | | |
| 22 | | |
| 23 | `; | |
| 24 | const table = ` | |
| 25 | | |
| 26 | # Added by codex-action. | |
| 27 | [model_providers.${MODEL_PROVIDER}] | |
| 28 | name = "OpenAI Proxy" | |
| 29 | base_url = "http://127.0.0.1:${port}/v1" | |
| 30 | wire_api = "responses" | |
| 31 | `; | |
| 32 | | |
| 33 | // Prepend model_provider at the very top. | |
| 34 | let output = `${header}${existing}${table}`; | |
| 35 | | |
| 36 | await fs.mkdir(codexHome, { recursive: true }); | |
| 37 | await fs.writeFile(configPath, output, "utf8"); | |
| 38 | } |