Skip to content
Source
Sign inSign up
Open History to see the last commit

proxy.ts

33 lines1.3 KB

1// Eval-only helper: it reads the local Codex config through python3 at startup.
2// @effect-diagnostics nodeBuiltinImport:off
3import * as NodeChildProcess from "node:child_process";
4import * as Schema from "effect/Schema";
5​
6// Read credentials into memory only. Python's TOML parser avoids another runner dependency.
7const decodeProxyConfig = Schema.decodeUnknownSync(
8 Schema.Struct({ url: Schema.String, headers: Schema.Record(Schema.String, Schema.String) }),
9);
10/** Reads the `vibeproxy` CLIProxyAPI provider from `~/.codex/config.toml`. */
11export const proxyConfig = () =>
12 decodeProxyConfig(
13 JSON.parse(
14 NodeChildProcess.execFileSync(
15 "python3",
16 [
17 "-c",
18 `
19import json, os, pathlib, tomllib
20home = pathlib.Path.home() / '.codex'
21p = tomllib.loads((home / 'config.toml').read_text())['model_providers']['vibeproxy']
22h = p.get('http_headers', {}).copy()
23h.update({k: os.environ[v] for k,v in p.get('env_http_headers', {}).items()})
24key = os.environ[p['env_key']] if p.get('env_key') else p.get('experimental_bearer_token')
25if not key and p.get('requires_openai_auth'): key = json.loads((home / 'auth.json').read_text()).get('OPENAI_API_KEY')
26if key: h['Authorization'] = 'Bearer ' + key
27print(json.dumps({'url': p['base_url'].rstrip('/'), 'headers': h}))
28`,
29 ],
30 { encoding: "utf8" },
31 ),
32 ),
33 );