openai/codex-action

Public

mirrored from https://github.com/openai/codex-actionAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
pr25

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

src/writeProxyConfig.ts

55lines · modeblame

1c44f3d7Michael Bolin10 months ago1import * as fs from "node:fs/promises";
2import * as path from "node:path";
685900c1Michael Bolin10 months ago3import * as os from "node:os";
4import { SafetyStrategy } from "./runCodexExec";
5import { checkOutput } from "./checkOutput";
1c44f3d7Michael Bolin10 months ago6
7const MODEL_PROVIDER = "openai-proxy";
8
9export async function writeProxyConfig(
10codexHome: string,
685900c1Michael Bolin10 months ago11port: number,
12safetyStrategy: SafetyStrategy
1c44f3d7Michael Bolin10 months ago13): Promise<void> {
14const configPath = path.join(codexHome, "config.toml");
15
16let existing = "";
17try {
18existing = await fs.readFile(configPath, "utf8");
19} catch {
20existing = "";
21}
22
23const header = `# Added by codex-action.
24model_provider = "${MODEL_PROVIDER}"
25
26
27`;
28const table = `
29
30# Added by codex-action.
31[model_providers.${MODEL_PROVIDER}]
32name = "OpenAI Proxy"
33base_url = "http://127.0.0.1:${port}/v1"
34wire_api = "responses"
35`;
36
37// Prepend model_provider at the very top.
38let output = `${header}${existing}${table}`;
39
685900c1Michael Bolin10 months ago40if (safetyStrategy === "unprivileged-user") {
41// We know we have already created the CODEX_HOME directory, but it is owned
42// by another user, so we need to use sudo to write the file.
43const tempDir = await fs.mkdtemp(path.join(os.tmpdir(), "codex-config"));
44try {
45const tempConfigPath = path.join(tempDir, "config.toml");
46await fs.writeFile(tempConfigPath, output, "utf8");
47await checkOutput(["sudo", "mv", tempConfigPath, configPath]);
48} finally {
49await fs.rm(tempDir, { recursive: true, force: true });
50}
51} else {
52await fs.mkdir(codexHome, { recursive: true });
53await fs.writeFile(configPath, output, "utf8");
54}
1c44f3d7Michael Bolin10 months ago55}