Private
Public Access
0
0

refactor(ai_client): remove ProviderError class; ErrorInfo is the new error type

This commit is contained in:
2026-06-12 19:41:41 -04:00
parent da44e934fc
commit 64b787b881
4 changed files with 29 additions and 54 deletions
-28
View File
@@ -76,31 +76,6 @@ _history_trunc_limit: int = 8000
# Global event emitter for API lifecycle events
events: EventEmitter = EventEmitter()
class ProviderError(Exception):
def __init__(self, kind: str, provider: str, original: Exception) -> None:
"""
[C: src/api_hooks.py:HookServerInstance.__init__, src/mcp_client.py:_DDGParser.__init__, src/mcp_client.py:_TextExtractor.__init__]
"""
self.kind = kind
self.provider = provider
self.original = original
super().__init__(str(original))
def ui_message(self) -> str:
"""
[C: src/app_controller.py:AppController._handle_request_event, src/app_controller.py:_api_generate]
"""
labels = {
"quota": "QUOTA EXHAUSTED",
"rate_limit": "RATE LIMITED",
"auth": "AUTH / API KEY ERROR",
"balance": "BALANCE / BILLING ERROR",
"network": "NETWORK / CONNECTION ERROR",
"unknown": "API ERROR",
}
label = labels.get(self.kind, "API ERROR")
return f"[{self.provider.upper()} {label}]\n\n{self.original}"
#region: Provider Configuration
def set_model_params(temp: float, max_tok: int, trunc_limit: int = 8000, top_p: float = 1.0) -> None:
@@ -1451,9 +1426,6 @@ def _send_anthropic_result(md_content: str, user_message: str, base_dir: str, fi
res = final_text if final_text.strip() else "(No text returned by the model)"
if monitor.enabled: monitor.end_component("ai_client._send_anthropic")
return Result(data=res)
except ProviderError:
if monitor.enabled: monitor.end_component("ai_client._send_anthropic")
raise
except Exception as exc:
if monitor.enabled: monitor.end_component("ai_client._send_anthropic")
return Result(data="", errors=[_classify_anthropic_error(exc, source="ai_client.anthropic")])
+7 -5
View File
@@ -4,7 +4,7 @@ from typing import Any, Callable, Optional
from openai import OpenAIError, RateLimitError, AuthenticationError, PermissionDeniedError, APIConnectionError, APIStatusError, BadRequestError
from src.result_types import ErrorInfo, ErrorKind
from src.result_types import ErrorInfo, ErrorKind, Result
@dataclass(frozen=True)
class NormalizedResponse:
@@ -64,7 +64,7 @@ def send_openai_compatible(
request: OpenAICompatibleRequest,
*,
capabilities: Any,
) -> NormalizedResponse:
) -> Result[str]:
kwargs: dict[str, Any] = {
"model": request.model,
"messages": request.messages,
@@ -80,10 +80,12 @@ def send_openai_compatible(
kwargs["extra_body"] = request.extra_body
try:
if request.stream:
return _send_streaming(client, kwargs, request.stream_callback)
return _send_blocking(client, kwargs)
response = _send_streaming(client, kwargs, request.stream_callback)
else:
response = _send_blocking(client, kwargs)
return Result(data=response.text)
except OpenAIError as exc:
raise _classify_openai_compatible_error(exc) from exc
return Result(data="", errors=[_classify_openai_compatible_error(exc, source="openai_compatible")])
def _send_blocking(client: Any, kwargs: dict[str, Any]) -> NormalizedResponse:
resp = client.chat.completions.create(**kwargs)