Open History to see the last commit
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.Union([
9 Schema.Struct({ error: Schema.String }),
10 Schema.Struct({ url: Schema.String, headers: Schema.Record(Schema.String, Schema.String) }),
11 ]),
12);
13/**
14 * Reads the CLIProxyAPI provider from `$CODEX_HOME/config.toml` (default `~/.codex`): the one
15 * `T3CODE_EVAL_MODEL_PROVIDER` names, else `model_provider` when it is custom, else `vibeproxy`.
16 */
17export const proxyConfig = () => {
18 const config = decodeProxyConfig(
19 JSON.parse(
20 NodeChildProcess.execFileSync(
21 "python3",
22 [
23 "-c",
24 `
25import json, os, pathlib, sys, tomllib
26home = pathlib.Path(os.environ.get('CODEX_HOME') or pathlib.Path.home() / '.codex')
27def fail(message):
28 print(json.dumps({'error': message + ' The GPT evals call models through a CLIProxyAPI server set up as a custom model provider in Codex: set model_provider = "<name>" and add [model_providers.<name>] with base_url and its key.'}))
29 sys.exit()
30path = home / 'config.toml'
31if not path.is_file(): fail(f'No Codex config at {path}.')
32config = tomllib.loads(path.read_text())
33providers = config.get('model_providers', {})
34override = os.environ.get('T3CODE_EVAL_MODEL_PROVIDER')
35name = override or (config.get('model_provider') if config.get('model_provider') in providers else 'vibeproxy')
36if override and name not in providers: fail(f'T3CODE_EVAL_MODEL_PROVIDER names {name!r}, but {path} has no [model_providers.{name}].')
37if name not in providers: fail(f'{path} has no custom model provider to use (model_provider is {config.get("model_provider", "unset")!r}).')
38p = providers[name]
39if not p.get('base_url'): fail(f'[model_providers.{name}] in {path} has no base_url.')
40h = p.get('http_headers', {}).copy()
41h.update({k: os.environ[v] for k,v in p.get('env_http_headers', {}).items()})
42key = os.environ.get(p['env_key']) if p.get('env_key') else p.get('experimental_bearer_token')
43if p.get('env_key') and not key: fail(f'[model_providers.{name}] in {path} reads its key from {p["env_key"]}, which is not set.')
44if not key and p.get('requires_openai_auth') and (home / 'auth.json').is_file(): key = json.loads((home / 'auth.json').read_text()).get('OPENAI_API_KEY')
45if not key and p.get('requires_openai_auth'): fail(f'[model_providers.{name}] in {path} sets requires_openai_auth, but {home / "auth.json"} has no OPENAI_API_KEY.')
46if key: h['Authorization'] = 'Bearer ' + key
47print(json.dumps({'url': p['base_url'].rstrip('/'), 'headers': h}))
48`,
49 ],
50 { encoding: "utf8" },
51 ),
52 ),
53 );
54 if ("error" in config) throw new Error(config.error);
55 return config;
56};