Files
vibemake-pcb-studio/backend/providerctl.py
T
2026-11-10 19:04:27 +08:00

60 lines
2.8 KiB
Python

"""Server operator CLI. No public endpoint or browser model override."""
import argparse
import asyncio
import json
from pathlib import Path
import sys
from launch import load_env
def ready_to_select(result):
if not result.get('transport_ready'):
return False
if result.get('auth_mode') == 'chatgpt':
return bool(result.get('uses_personal_login') and result.get('catalog_match'))
return bool(result.get('auth_mode') == 'apikey' and result.get('api_key_configured'))
def main():
load_env(Path(__file__).resolve().parent / '.env.local')
parser = argparse.ArgumentParser(description=__doc__)
sub = parser.add_subparsers(dest='command', required=True)
sub.add_parser('status')
use = sub.add_parser('use', help='Change the provider for NEW tasks only; no API restart needed')
use.add_argument('provider', choices=['deepseek', 'codex'])
doctor = sub.add_parser('doctor', help='Configuration and protocol checks only; never starts inference')
doctor.add_argument('provider', choices=['deepseek', 'codex'])
doctor.add_argument('--protocol-only', action='store_true', help='Check Codex protocol without requiring an API key')
args = parser.parse_args()
from providers.config import binding, current_binding, config_path, select
from providers.factory import check_configuration
if args.command == 'status':
result = {'active': current_binding(), 'config_path': str(config_path()),
'profiles': [binding('deepseek'), binding('codex')], 'scope': 'new-tasks-only'}
else:
profile = binding(args.provider)
if args.command == 'doctor' and args.protocol_only and args.provider == 'codex':
from providers.codex import probe
result = asyncio.run(probe(require_key=False))
else:
check_configuration(profile)
if args.provider == 'codex':
from providers.codex import probe
result = asyncio.run(probe())
result['configuration_ready'] = ready_to_select(result)
if args.command == 'use' and not result['configuration_ready']:
raise RuntimeError('当前登录或客户端模型目录未确认支持 gpt-6.1-sol;请更新客户端并刷新官方目录。仅会话参数回读成功不能作为启用依据。')
else:
result = {'configuration_ready': True, 'inference_verified': False, **profile}
if args.command == 'use':
result = {**result, 'active': select(args.provider), 'scope': 'new-tasks-only'}
print(json.dumps(result, ensure_ascii=False, indent=2))
if __name__ == '__main__':
try:
main()
except Exception as exc:
print(json.dumps({'error': type(exc).__name__, 'message': str(exc)}, ensure_ascii=False), file=sys.stderr)
raise SystemExit(1)