@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 03:37 UTC

main
ts 56 lines 2.88 KB
Raw
1 // Eval-only helper: it reads the local Codex config through python3 at startup.
2 // @effect-diagnostics nodeBuiltinImport:off
3 import * as NodeChildProcess from "node:child_process";
4 import * as Schema from "effect/Schema";
5
6 // Read credentials into memory only. Python's TOML parser avoids another runner dependency.
7 const 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 */
17 export const proxyConfig = () => {
18 const config = decodeProxyConfig(
19 JSON.parse(
20 NodeChildProcess.execFileSync(
21 "python3",
22 [
23 "-c",
24 `
25 import json, os, pathlib, sys, tomllib
26 home = pathlib.Path(os.environ.get('CODEX_HOME') or pathlib.Path.home() / '.codex')
27 def 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()
30 path = home / 'config.toml'
31 if not path.is_file(): fail(f'No Codex config at {path}.')
32 config = tomllib.loads(path.read_text())
33 providers = config.get('model_providers', {})
34 override = os.environ.get('T3CODE_EVAL_MODEL_PROVIDER')
35 name = override or (config.get('model_provider') if config.get('model_provider') in providers else 'vibeproxy')
36 if override and name not in providers: fail(f'T3CODE_EVAL_MODEL_PROVIDER names {name!r}, but {path} has no [model_providers.{name}].')
37 if name not in providers: fail(f'{path} has no custom model provider to use (model_provider is {config.get("model_provider", "unset")!r}).')
38 p = providers[name]
39 if not p.get('base_url'): fail(f'[model_providers.{name}] in {path} has no base_url.')
40 h = p.get('http_headers', {}).copy()
41 h.update({k: os.environ[v] for k,v in p.get('env_http_headers', {}).items()})
42 key = os.environ.get(p['env_key']) if p.get('env_key') else p.get('experimental_bearer_token')
43 if 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.')
44 if 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')
45 if 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.')
46 if key: h['Authorization'] = 'Bearer ' + key
47 print(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 };