Files

189 lines
8.5 KiB
Python

"""JSON-RPC transport; native Codex owns reasoning, tools and conversation history."""
import asyncio
import json
import os
import tomllib
from pathlib import Path
from .config import CODEX_BIN, CODEX_HOME, runtime_for
from .models import catalog, catalog_path
from .storage import write
SECRET_ENV_NAMES = tuple(name for key in ('OPENAI_API_KEY', 'DEEPSEEK_API_KEY', 'ARK_API_KEY', 'DOUBAO_API_KEY', 'CODEX_API_KEY') for name in (key, key+'_FILE'))
def sandbox_settings(production):
return {
'approval_policy': 'never',
'sandbox_mode': 'workspace-write' if production else 'read-only',
'features.shell_tool': production, 'features.multi_agent': False,
'sandbox_workspace_write.network_access': True,
'sandbox_workspace_write.exclude_slash_tmp': True,
'sandbox_workspace_write.exclude_tmpdir_env_var': True,
}
def api_key(runtime=None):
runtime = runtime or runtime_for()
names = (runtime.key_name, *runtime.key_aliases)
key = next((os.environ[name].strip() for name in names if os.environ.get(name, '').strip()), '')
file_name = next((name+'_FILE' for name in names if os.environ.get(name+'_FILE')), None)
if not key and file_name:
try:
with Path(os.environ[file_name]).expanduser().open() as stream:
key = stream.read(16385).strip()
except (OSError, UnicodeError):
raise ValueError('Cannot read '+file_name) from None
if not key:
raise ValueError(runtime.label+' requires '+runtime.key_name+' or '+runtime.key_name+'_FILE')
if len(key) > 16384 or any(c.isspace() for c in key):
raise ValueError(runtime.label+' API key must be a single non-empty value')
return key
def process_config(production, runtime=None, *, read_only_tools=False, allow_web=False):
runtime = runtime or runtime_for()
if runtime.auth_mode not in {'apikey', 'chatgpt'}:
raise ValueError('PCB_CODEX_AUTH must be apikey or chatgpt')
config = {
'model': runtime.model, 'model_provider': runtime.provider, 'model_reasoning_effort': runtime.effort,
**sandbox_settings(production),
'web_search': 'live' if runtime.id == 'openai' else 'disabled',
'shell_environment_policy.ignore_default_excludes': False,
}
env = dict(os.environ, CODEX_HOME=str(CODEX_HOME))
for key in (*SECRET_ENV_NAMES, 'OPENAI_BASE_URL'):
env.pop(key, None)
if runtime.auth_mode == 'apikey':
env[runtime.key_name] = api_key(runtime)
prefix = 'model_providers.' + runtime.provider + '.'
config.update({
prefix + 'name': 'VibeMake '+runtime.label,
prefix + 'base_url': runtime.api_base_url,
prefix + 'wire_api': 'responses',
prefix + 'env_key': runtime.key_name,
prefix + 'requires_openai_auth': False,
})
if runtime.id != 'openai':
config['model_catalog_json'] = str(catalog_path(CODEX_HOME, runtime))
if runtime.id == 'deepseek':
config['web_search'] = 'live'
config['features.standalone_web_search'] = True
if read_only_tools:
config.update({'sandbox_mode':'read-only',
'features.shell_tool':False, 'features.multi_agent':False, 'mcp_servers':{}})
if not allow_web:
config.update({'web_search':'disabled','features.standalone_web_search':False})
# Explicitly disable inherited named MCP servers as well as the root table.
config_file=CODEX_HOME/'config.toml'
if config_file.is_file():
for name in tomllib.loads(config_file.read_text()).get('mcp_servers',{}):
config['mcp_servers.'+name+'.enabled']=False
args = [CODEX_BIN, 'app-server', '--listen', 'stdio://']
for key, value in config.items():
args += ['-c', key + '=' + json.dumps(value)]
return args, env
class AppServer:
def __init__(self, directory, production, handler, runtime=None, *, read_only_tools=False, allow_web=False):
self.directory, self.production, self.handler = directory, production, handler
self.pending, self.requests, self.counter = {}, set(), 0
self.events = asyncio.Queue()
self.proc = None
self.reader = None
self.err = None
self.runtime = runtime or runtime_for()
self.read_only_tools = read_only_tools
self.allow_web = allow_web
async def __aenter__(self):
options={'read_only_tools':True} if self.read_only_tools else {}
if self.allow_web:options['allow_web']=True
args, env = process_config(self.production, self.runtime, **options)
# Production excludes system temp directories from writable roots.
# Keep native shell/tool temporary files in this task's writable root.
temporary = self.directory / 'tmp'
temporary.mkdir(exist_ok=True)
env.update(TMPDIR=str(temporary), TMP=str(temporary), TEMP=str(temporary))
CODEX_HOME.mkdir(parents=True, exist_ok=True, mode=0o700)
if self.runtime.id != 'openai':
write(catalog_path(CODEX_HOME, self.runtime), catalog(self.runtime))
self.err = (self.directory / 'codex-stderr.log').open('ab')
try:
self.proc = await asyncio.create_subprocess_exec(*args, stdin=asyncio.subprocess.PIPE, stdout=asyncio.subprocess.PIPE, stderr=self.err, env=env, cwd=self.directory, limit=64*1024*1024)
self.reader = asyncio.create_task(self._read())
await self.call('initialize', {'clientInfo': {'name':'vibemake_persistent_pcb','title':'VibeMake Persistent PCB','version':'1.0.0'}, 'capabilities':{'experimentalApi':True}})
await self.send({'method':'initialized'})
except BaseException:
await self.__aexit__()
raise
return self
async def send(self, message):
if self.proc.returncode is not None:
raise ConnectionError('Codex process exited')
self.proc.stdin.write((json.dumps(message, ensure_ascii=False)+'\n').encode())
await self.proc.stdin.drain()
async def call(self, method, params, timeout=90):
self.counter += 1
n = self.counter
future = asyncio.get_running_loop().create_future()
self.pending[n] = future
try:
await self.send({'id':n,'method':method,'params':params})
return await asyncio.wait_for(future, timeout)
finally:
self.pending.pop(n, None)
async def answer(self, msg):
try:
result = await self.handler(msg['method'], msg.get('params',{}))
await self.send({'id':msg['id'],'result':result})
except asyncio.CancelledError:
raise
except Exception as e:
await self.send({'id':msg['id'],'error':{'code':-32000,'message':str(e)}})
async def _read(self):
try:
while line := await self.proc.stdout.readline():
msg = json.loads(line)
if 'method' in msg and 'id' in msg:
job = asyncio.create_task(self.answer(msg))
self.requests.add(job)
job.add_done_callback(self.requests.discard)
elif 'method' in msg:
await self.events.put(msg)
elif msg.get('id') in self.pending:
future = self.pending[msg['id']]
if not future.done():
if 'error' in msg:
future.set_exception(RuntimeError(json.dumps(msg['error'],ensure_ascii=False)))
else:
future.set_result(msg.get('result'))
except Exception as e:
await self.events.put({'method':'transport/error','params':{'message':str(e)}})
finally:
for future in list(self.pending.values()):
if not future.done():
future.set_exception(ConnectionError('Codex transport ended'))
await self.events.put({'method':'transport/closed','params':{}})
async def __aexit__(self, *args):
for job in self.requests:
job.cancel()
await asyncio.gather(*self.requests, return_exceptions=True)
if self.proc and self.proc.returncode is None:
self.proc.terminate()
try:
await asyncio.wait_for(self.proc.wait(),10)
except asyncio.TimeoutError:
self.proc.kill()
await self.proc.wait()
if self.reader:
self.reader.cancel()
await asyncio.gather(self.reader,return_exceptions=True)
if self.err:
self.err.close()