Files
2026-11-10 19:04:27 +08:00

74 lines
2.8 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""Model failures are independent of the selected EDA engine."""
import json
class ModelError(RuntimeError):
permanent = False
code = None
class ModelUnavailableError(ModelError):
permanent = True
code = 'model_not_found'
class ModelAccessError(ModelError):
permanent = True
code = 'model_access_denied'
class ModelRequestError(ModelError):
permanent = True
code = 'invalid_model_request'
def error_details(value):
if isinstance(value, dict):
child = value.get('error')
nested = error_details(child) if child else error_details(value.get('message')) if isinstance(value.get('message'), str) else {}
return {**{k: value[k] for k in ('code', 'message', 'status') if k in value}, **nested}
if isinstance(value, str):
# Some app-server releases put a JSON HTTP envelope inside error.message.
start = value.find('{')
if start >= 0:
try:
decoded, _ = json.JSONDecoder().raw_decode(value[start:])
return error_details(decoded)
except ValueError:
pass
return {'message': value}
return {}
def provider_failure(detail, *, provider='Codex', model='gpt-6.1-sol'):
parsed = error_details(detail)
message, code = str(parsed.get('message', '请求未完成')), parsed.get('code')
if code in {'model_not_found', 'model_not_supported', 'unsupported_model'} or ('model' in message.lower() and 'not supported' in message.lower()):
return ModelUnavailableError(f'当前 {provider} 账号或调用通道不支持模型 {model}(model_not_found)。需要由服务维护者调整账号或模型配置。')
if parsed.get('status') in {401, 403} or code in {'invalid_api_key', 'authentication_error', 'permission_denied'}:
return ModelAccessError(f'{provider} 认证失败或账号无权访问。需要由服务维护者检查登录和授权配置。')
if parsed.get('status') in {400, 404}:
return ModelRequestError(provider + ' 请求被服务端拒绝:' + message[:500])
return ModelError(provider + ' 请求未完成:' + message[:500])
def known_model_unavailable(text):
"""Read historical provider errors without rewriting native EDA failures."""
if not isinstance(text, str) or 'Codex' not in text:
return None
details = error_details(text)
if details.get('code') in {'model_not_found', 'model_not_supported', 'unsupported_model'}:
return str(provider_failure(details))
if '(model_not_found)' in text or '(model_not_found)' in text:
return text.split('ModelUnavailableError: ', 1)[-1]
return None
def public_model_error(text):
known = known_model_unavailable(text)
if known:
return known
if isinstance(text, str) and text.startswith('KiCadError: Codex'):
return text.removeprefix('KiCadError: ')
return text