refactor(schemas): remove NormalizedResponse backward-compat __init__; use canonical API

This commit is contained in:
ed
2026-06-24 17:12:49 -04:00
parent 03dd44c642
commit 20236546d7
9 changed files with 29 additions and 48 deletions
+4 -3
View File
@@ -2025,6 +2025,7 @@ def _send_gemini_cli(md_content: str, user_message: str, base_dir: str,
stream_callback: Optional[Callable[[str], None]] = None,
patch_callback: Optional[Callable[[str, str], Optional[str]]] = None) -> Result[str]:
from src.openai_compatible import OpenAICompatibleRequest, NormalizedResponse
from src.openai_schemas import UsageStats
"""
[C: src/ai_server.py:_handle_send]
Functional Purpose: Sends requests to Gemini via the headless Gemini CLI subprocess adapter.
@@ -2051,7 +2052,7 @@ def _send_gemini_cli(md_content: str, user_message: str, base_dir: str,
def _send(r_idx: int) -> NormalizedResponse:
if adapter is None:
return NormalizedResponse(text="(adapter unavailable)", tool_calls=[], usage_input_tokens=0, usage_output_tokens=0, usage_cache_read_tokens=0, usage_cache_creation_tokens=0, raw_response=None)
return NormalizedResponse(text="(adapter unavailable)", tool_calls=[], usage=UsageStats(input_tokens=0, output_tokens=0, cache_read_tokens=0, cache_creation_tokens=0), raw_response=None)
send_result = _send_cli_round_result(r_idx, adapter, payload, safety_settings, sys_instr, stream_callback)
if not send_result.ok:
raise cast(Exception, send_result.errors[0].original) from None
@@ -2085,7 +2086,7 @@ def _send_gemini_cli(md_content: str, user_message: str, base_dir: str,
"kind": "history_add",
"payload": {"role": "AI", "content": txt}
})
return NormalizedResponse(text=txt, tool_calls=calls, usage_input_tokens=usage.get("prompt_tokens", 0), usage_output_tokens=usage.get("completion_tokens", 0), usage_cache_read_tokens=0, usage_cache_creation_tokens=0, raw_response=resp_data)
return NormalizedResponse(text=txt, tool_calls=calls, usage=UsageStats(input_tokens=usage.get("prompt_tokens", 0), output_tokens=usage.get("completion_tokens", 0), cache_read_tokens=0, cache_creation_tokens=0), raw_response=resp_data)
def _pre_dispatch(r_idx: int, calls: list[Metadata]) -> list[Metadata]:
nonlocal payload, cumulative_tool_bytes, file_items
@@ -2569,7 +2570,7 @@ def _send_grok(md_content: str, user_message: str, base_dir: str,
Runs synchronously in the caller thread; synchronizes Grok history using _grok_history_lock.
"""
from src.openai_compatible import OpenAICompatibleRequest, _classify_openai_compatible_error
from src.openai_schemas import ChatMessage
from src.openai_schemas import ChatMessage, UsageStats
try:
client = _ensure_grok_client()
tools: list[Metadata] | None = _get_deepseek_tools() or None
+5 -28
View File
@@ -16,7 +16,7 @@ CONVENTION: 1-space indentation. NO COMMENTS.
"""
from __future__ import annotations
from dataclasses import dataclass
from dataclasses import dataclass, field
from typing import Any, Callable, Optional
from src.type_aliases import JsonValue
@@ -72,35 +72,12 @@ class UsageStats:
cache_creation_tokens: int = 0
@dataclass(frozen=True, init=False)
@dataclass(frozen=True)
class NormalizedResponse:
text: str
tool_calls: tuple[ToolCall, ...]
usage: UsageStats
raw_response: Any
def __init__(
self,
text: str,
tool_calls: tuple[ToolCall, ...] = (),
usage: UsageStats | None = None,
raw_response: Any = None,
usage_input_tokens: int | None = None,
usage_output_tokens: int | None = None,
usage_cache_read_tokens: int | None = None,
usage_cache_creation_tokens: int | None = None,
) -> None:
if usage is None:
usage = UsageStats(
input_tokens=usage_input_tokens if usage_input_tokens is not None else 0,
output_tokens=usage_output_tokens if usage_output_tokens is not None else 0,
cache_read_tokens=usage_cache_read_tokens if usage_cache_read_tokens is not None else 0,
cache_creation_tokens=usage_cache_creation_tokens if usage_cache_creation_tokens is not None else 0,
)
object.__setattr__(self, "text", text)
object.__setattr__(self, "tool_calls", tool_calls)
object.__setattr__(self, "usage", usage)
object.__setattr__(self, "raw_response", raw_response)
tool_calls: tuple[ToolCall, ...] = ()
usage: UsageStats = field(default_factory=lambda: UsageStats(input_tokens=0, output_tokens=0))
raw_response: Any = None
def to_legacy_dict(self) -> JsonValue:
return {