askrobots / dbbasic-object-server
Public
Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.
danq9
signup: the person chooses the password and name on the confirm page
83c7aa6
2 weeks, 5 days ago
History
object_ai.py · 21.4 KB · 543 lines
| 1 | """Provider-neutral AI chat with MCP tool calling. |
| 2 | |
| 3 | This is the shell's brain: one conversation turn goes to an AI provider |
| 4 | using the caller's own stored service key, the model may call a small, |
| 5 | caller-chosen subset of the server's MCP tools, and every tool call is |
| 6 | dispatched back through the server's own routing so permission checks |
| 7 | and audit apply exactly as if the caller made the request directly. |
| 8 | |
| 9 | Models are named ``service:model`` (``anthropic:claude-haiku-4-5``, |
| 10 | ``openai:gpt-5-mini``). Handing the model only a few named tools keeps |
| 11 | the context small enough for fast, inexpensive models — the tool subset |
| 12 | is configuration, not code. |
| 13 | |
| 14 | The module is pure: HTTP is injected by the caller, so tests run without |
| 15 | a network and the server owns timeouts and threading. |
| 16 | """ |
| 17 | |
| 18 | from __future__ import annotations |
| 19 | |
| 20 | import json |
| 21 | from typing import Any, Callable, Mapping |
| 22 | |
| 23 | DEFAULT_MAX_ROUNDS = 6 |
| 24 | MAX_ROUNDS_LIMIT = 12 |
| 25 | DEFAULT_MAX_TOKENS = 1024 |
| 26 | |
| 27 | ANTHROPIC_URL = "https://api.anthropic.com/v1/messages" |
| 28 | ANTHROPIC_VERSION = "2023-06-01" |
| 29 | OPENAI_URL = "https://api.openai.com/v1/chat/completions" |
| 30 | |
| 31 | SUPPORTED_SERVICES = ("anthropic", "openai") |
| 32 | |
| 33 | # The gpt-5.6 family defaults to a non-"none" reasoning_effort that OpenAI's |
| 34 | # /v1/chat/completions rejects outright when tools are also present ("Function |
| 35 | # tools with reasoning_effort are not supported ... set reasoning_effort to |
| 36 | # 'none'" -- their error, not ours). Older models (gpt-5, gpt-5-mini, ...) |
| 37 | # already work fine with tools under their own default, so this is scoped to |
| 38 | # the specific family that needs it rather than applied to every OpenAI call. |
| 39 | _REASONING_EFFORT_NONE_MODELS = ("gpt-5.6-luna", "gpt-5.6-terra", "gpt-5.6-sol") |
| 40 | |
| 41 | # send_http(url, headers, body_bytes) -> (status, response_bytes) |
| 42 | SendHttp = Callable[[str, Mapping[str, str], bytes], tuple[int, bytes]] |
| 43 | # dispatch_tool(name, arguments) -> {"http_status": int, "response": Any} |
| 44 | DispatchTool = Callable[[str, dict[str, Any]], dict[str, Any]] |
| 45 | |
| 46 | |
| 47 | class AIProviderError(RuntimeError): |
| 48 | """Raised when the provider returns an unusable response.""" |
| 49 | |
| 50 | |
| 51 | class InvalidChatRequestError(ValueError): |
| 52 | """Raised when a chat request payload is not usable.""" |
| 53 | |
| 54 | |
| 55 | def split_model(model: str) -> tuple[str, str]: |
| 56 | """Split 'service:model' and validate the service.""" |
| 57 | if not isinstance(model, str) or ":" not in model: |
| 58 | raise InvalidChatRequestError( |
| 59 | "model must be 'service:model', like anthropic:claude-haiku-4-5" |
| 60 | ) |
| 61 | service, _, name = model.partition(":") |
| 62 | if service not in SUPPORTED_SERVICES or not name: |
| 63 | supported = ", ".join(SUPPORTED_SERVICES) |
| 64 | raise InvalidChatRequestError(f"model service must be one of: {supported}") |
| 65 | return service, name |
| 66 | |
| 67 | |
| 68 | def mcp_tools_as_provider_tools( |
| 69 | tool_names: list[str], |
| 70 | catalog: list[dict[str, Any]], |
| 71 | *, |
| 72 | service: str, |
| 73 | ) -> list[dict[str, Any]]: |
| 74 | """Convert named MCP catalog entries into the provider's tool format.""" |
| 75 | by_name = {tool["name"]: tool for tool in catalog} |
| 76 | unknown = [name for name in tool_names if name not in by_name] |
| 77 | if unknown: |
| 78 | raise InvalidChatRequestError(f"Unknown tools: {', '.join(sorted(unknown))}") |
| 79 | |
| 80 | tools = [] |
| 81 | for name in tool_names: |
| 82 | tool = by_name[name] |
| 83 | if service == "anthropic": |
| 84 | tools.append( |
| 85 | { |
| 86 | "name": tool["name"], |
| 87 | "description": tool.get("description", ""), |
| 88 | "input_schema": tool.get("inputSchema", {"type": "object"}), |
| 89 | } |
| 90 | ) |
| 91 | else: |
| 92 | tools.append( |
| 93 | { |
| 94 | "type": "function", |
| 95 | "function": { |
| 96 | "name": tool["name"], |
| 97 | "description": tool.get("description", ""), |
| 98 | "parameters": tool.get("inputSchema", {"type": "object"}), |
| 99 | }, |
| 100 | } |
| 101 | ) |
| 102 | return tools |
| 103 | |
| 104 | |
| 105 | # read_page's real links (with real hrefs) are exactly the kind of precise |
| 106 | # fact a model's own restated prose loses -- conversation history only ever |
| 107 | # carries the model's text, never a tool's actual result, so "go to that |
| 108 | # story" on a LATER turn had nothing but the model's own possibly-vague |
| 109 | # summary to resolve against. Caught live: a real Talk session fetched |
| 110 | # Techmeme, summarized it in prose with no link numbers at all (voice |
| 111 | # mode's "keep it short" instruction won against "cite link numbers" on a |
| 112 | # link-heavy page), and the very next turn had no way back to the actual |
| 113 | # article. Echoing the real link list back to the caller here -- capped, |
| 114 | # so a link-heavy page can't balloon every subsequent turn's context size |
| 115 | # once a caller starts re-injecting it -- lets a later turn ground a |
| 116 | # reference in real hrefs instead of the model's own memory of what it |
| 117 | # said. Scoped to read_page only: most tools' results are either already |
| 118 | # small (a single record) or not the kind of thing a later turn needs to |
| 119 | # resolve a vague reference against. |
| 120 | READER_LINKS_ECHO_CAP = 40 |
| 121 | |
| 122 | |
| 123 | def _tool_result_summary(name: str, response: Any) -> dict[str, Any] | None: |
| 124 | """A bounded, caller-facing echo of read_page's real result, or None |
| 125 | for every other tool (including a read_page call that returned no |
| 126 | usable links). Not persisted server-side -- the caller (Talk) owns |
| 127 | carrying this forward on later turns, the same way it already owns |
| 128 | aiHistory; the server stays stateless about chats either way. |
| 129 | """ |
| 130 | if name != "read_page" or not isinstance(response, dict): |
| 131 | return None |
| 132 | links = response.get("links") |
| 133 | if not isinstance(links, list) or not links: |
| 134 | return None |
| 135 | return { |
| 136 | "final_url": response.get("final_url"), |
| 137 | "title": response.get("title"), |
| 138 | "links": links[:READER_LINKS_ECHO_CAP], |
| 139 | } |
| 140 | |
| 141 | |
| 142 | def run_chat( |
| 143 | *, |
| 144 | send_http: SendHttp, |
| 145 | dispatch_tool: DispatchTool, |
| 146 | service: str, |
| 147 | model: str, |
| 148 | key: str, |
| 149 | message: str, |
| 150 | system: str | None = None, |
| 151 | tools: list[dict[str, Any]] | None = None, |
| 152 | history: list[dict[str, str]] | None = None, |
| 153 | attachments: list[dict[str, Any]] | None = None, |
| 154 | max_rounds: int = DEFAULT_MAX_ROUNDS, |
| 155 | max_tokens: int = DEFAULT_MAX_TOKENS, |
| 156 | ) -> dict[str, Any]: |
| 157 | """Run one chat turn, looping through tool calls until a final reply. |
| 158 | |
| 159 | ``history`` is prior conversation turns ({"role": "user"|"assistant", |
| 160 | "content": ...}) so a caller can resume a conversation it logged — |
| 161 | the server itself stays stateless about chats. |
| 162 | """ |
| 163 | if not isinstance(message, str) or not message.strip(): |
| 164 | raise InvalidChatRequestError("message is required") |
| 165 | if attachments and len(attachments) > MAX_ATTACHMENTS_PER_TURN: |
| 166 | raise InvalidChatRequestError( |
| 167 | f"{len(attachments)} attachments; the limit is " |
| 168 | f"{MAX_ATTACHMENTS_PER_TURN} per message." |
| 169 | ) |
| 170 | rounds = max(1, min(int(max_rounds), MAX_ROUNDS_LIMIT)) |
| 171 | |
| 172 | if service == "anthropic": |
| 173 | provider = _AnthropicProvider(key, model, system, tools or [], max_tokens) |
| 174 | else: |
| 175 | provider = _OpenAIProvider(key, model, system, tools or [], max_tokens) |
| 176 | |
| 177 | for turn in normalize_history(history): |
| 178 | provider.messages.append(turn) |
| 179 | provider.start(message.strip(), attachments or None) |
| 180 | tool_log: list[dict[str, Any]] = [] |
| 181 | usage = {"input_tokens": 0, "output_tokens": 0} |
| 182 | |
| 183 | for round_index in range(rounds): |
| 184 | status, body = send_http(*provider.request()) |
| 185 | parsed = provider.parse(status, body) |
| 186 | usage["input_tokens"] += parsed["usage"].get("input_tokens", 0) |
| 187 | usage["output_tokens"] += parsed["usage"].get("output_tokens", 0) |
| 188 | |
| 189 | if not parsed["tool_calls"]: |
| 190 | return { |
| 191 | "reply": parsed["text"], |
| 192 | "rounds": round_index + 1, |
| 193 | "tool_calls": tool_log, |
| 194 | "usage": usage, |
| 195 | } |
| 196 | |
| 197 | results = [] |
| 198 | for call in parsed["tool_calls"]: |
| 199 | result = dispatch_tool(call["name"], call["arguments"]) |
| 200 | entry = { |
| 201 | "name": call["name"], |
| 202 | "arguments": call["arguments"], |
| 203 | "http_status": result.get("http_status"), |
| 204 | } |
| 205 | summary = _tool_result_summary(call["name"], result.get("response")) |
| 206 | if summary is not None: |
| 207 | entry["result"] = summary |
| 208 | tool_log.append(entry) |
| 209 | results.append((call, result)) |
| 210 | provider.add_tool_results(parsed, results) |
| 211 | |
| 212 | return { |
| 213 | "reply": parsed["text"] or "(stopped after reaching the tool-call round limit)", |
| 214 | "rounds": rounds, |
| 215 | "tool_calls": tool_log, |
| 216 | "usage": usage, |
| 217 | "truncated": True, |
| 218 | } |
| 219 | |
| 220 | |
| 221 | MAX_HISTORY_TURNS = 40 |
| 222 | |
| 223 | |
| 224 | def normalize_history(history: list[dict[str, str]] | None) -> list[dict[str, str]]: |
| 225 | """Validate and trim prior turns to plain user/assistant text messages.""" |
| 226 | if history is None: |
| 227 | return [] |
| 228 | if not isinstance(history, list): |
| 229 | raise InvalidChatRequestError("history must be a list of {role, content} turns") |
| 230 | turns = [] |
| 231 | for item in history: |
| 232 | if not isinstance(item, dict): |
| 233 | raise InvalidChatRequestError("history turns must be objects") |
| 234 | role = item.get("role") |
| 235 | content = item.get("content") |
| 236 | if role not in ("user", "assistant") or not isinstance(content, str) or not content.strip(): |
| 237 | raise InvalidChatRequestError( |
| 238 | "history turns need role user|assistant and non-empty content" |
| 239 | ) |
| 240 | turns.append({"role": role, "content": content.strip()}) |
| 241 | return turns[-MAX_HISTORY_TURNS:] |
| 242 | |
| 243 | |
| 244 | # === chat attachments ======================================================== |
| 245 | # |
| 246 | # A dropped file becomes a provider-neutral PART here, and the split of |
| 247 | # labour is the point: the SERVER resolves file ids to bytes (permissions, |
| 248 | # ownership, reading the blob -- all I/O), while this module classifies and |
| 249 | # formats -- pure, testable without a data directory, like everything else |
| 250 | # in it. run_chat then renders parts per provider, because Anthropic wants |
| 251 | # a content array with base64 image blocks and OpenAI wants data: URLs, |
| 252 | # and neither difference belongs anywhere near the caller. |
| 253 | |
| 254 | MAX_ATTACHMENTS_PER_TURN = 8 |
| 255 | MAX_IMAGE_BYTES = 5 * 1024 * 1024 # both providers cap around here |
| 256 | MAX_TEXT_BYTES = 200 * 1024 # ~50k tokens of context is plenty |
| 257 | |
| 258 | IMAGE_MEDIA_TYPES = ("image/png", "image/jpeg", "image/gif", "image/webp") |
| 259 | TEXTUAL_CONTENT_TYPES = ( |
| 260 | "application/json", "application/xml", "application/x-yaml", |
| 261 | "application/csv", "application/javascript", "application/sql", |
| 262 | ) |
| 263 | |
| 264 | |
| 265 | def attachment_part(filename: str, content_type: str, data: bytes) -> dict[str, Any]: |
| 266 | """Classify one file's bytes into a chat part, or refuse by name. |
| 267 | |
| 268 | Images pass through as base64 for the provider's vision input; anything |
| 269 | that reads as text is inlined as text. Everything else is refused with |
| 270 | a message that says what IS supported -- a binary the model cannot see |
| 271 | must fail here, loudly, rather than arrive as mojibake the model |
| 272 | hallucinates an interpretation of. That silent path is the whole |
| 273 | reason this function exists as a gate rather than a best-effort |
| 274 | decoder. |
| 275 | """ |
| 276 | import base64 |
| 277 | |
| 278 | name = str(filename or "file") |
| 279 | ctype = str(content_type or "").split(";")[0].strip().lower() |
| 280 | |
| 281 | if ctype in IMAGE_MEDIA_TYPES: |
| 282 | if len(data) > MAX_IMAGE_BYTES: |
| 283 | raise InvalidChatRequestError( |
| 284 | f"{name}: image is {len(data) // 1024}KB; the limit is " |
| 285 | f"{MAX_IMAGE_BYTES // 1024}KB. Resize it before attaching." |
| 286 | ) |
| 287 | return {"kind": "image", "name": name, "media_type": ctype, |
| 288 | "data_b64": base64.b64encode(data).decode("ascii")} |
| 289 | |
| 290 | looks_textual = ( |
| 291 | ctype.startswith("text/") |
| 292 | or ctype in TEXTUAL_CONTENT_TYPES |
| 293 | or ctype.endswith("+json") or ctype.endswith("+xml") |
| 294 | ) |
| 295 | if not looks_textual: |
| 296 | try: |
| 297 | data.decode("utf-8") |
| 298 | looks_textual = True # honest text under a lazy MIME type |
| 299 | except UnicodeDecodeError: |
| 300 | looks_textual = False |
| 301 | if looks_textual: |
| 302 | if len(data) > MAX_TEXT_BYTES: |
| 303 | raise InvalidChatRequestError( |
| 304 | f"{name}: text file is {len(data) // 1024}KB; the limit is " |
| 305 | f"{MAX_TEXT_BYTES // 1024}KB. Attach an excerpt instead." |
| 306 | ) |
| 307 | try: |
| 308 | text = data.decode("utf-8") |
| 309 | except UnicodeDecodeError as exc: |
| 310 | raise InvalidChatRequestError( |
| 311 | f"{name}: declared {ctype or 'text'} but is not valid UTF-8." |
| 312 | ) from exc |
| 313 | return {"kind": "text", "name": name, "text": text} |
| 314 | |
| 315 | raise InvalidChatRequestError( |
| 316 | f"{name}: cannot attach {ctype or 'unknown'} to a chat. Supported: " |
| 317 | f"images ({', '.join(IMAGE_MEDIA_TYPES)}) and text-like files " |
| 318 | f"(anything UTF-8). PDFs and other binaries need their text " |
| 319 | f"extracted first." |
| 320 | ) |
| 321 | |
| 322 | |
| 323 | def _content_with_parts_anthropic(message: str, parts: list[dict[str, Any]]): |
| 324 | content: list[dict[str, Any]] = [] |
| 325 | for part in parts: |
| 326 | if part["kind"] == "image": |
| 327 | content.append({"type": "image", "source": { |
| 328 | "type": "base64", "media_type": part["media_type"], |
| 329 | "data": part["data_b64"]}}) |
| 330 | else: |
| 331 | content.append({"type": "text", |
| 332 | "text": f"[file: {part['name']}]\n{part['text']}"}) |
| 333 | content.append({"type": "text", "text": message}) |
| 334 | return content |
| 335 | |
| 336 | |
| 337 | def _content_with_parts_openai(message: str, parts: list[dict[str, Any]]): |
| 338 | content: list[dict[str, Any]] = [] |
| 339 | for part in parts: |
| 340 | if part["kind"] == "image": |
| 341 | content.append({"type": "image_url", "image_url": { |
| 342 | "url": f"data:{part['media_type']};base64,{part['data_b64']}"}}) |
| 343 | else: |
| 344 | content.append({"type": "text", |
| 345 | "text": f"[file: {part['name']}]\n{part['text']}"}) |
| 346 | content.append({"type": "text", "text": message}) |
| 347 | return content |
| 348 | |
| 349 | |
| 350 | class _AnthropicProvider: |
| 351 | def __init__(self, key, model, system, tools, max_tokens): |
| 352 | self.key = key |
| 353 | self.model = model |
| 354 | self.system = system |
| 355 | self.tools = tools |
| 356 | self.max_tokens = max_tokens |
| 357 | self.messages: list[dict[str, Any]] = [] |
| 358 | |
| 359 | def start(self, message: str, parts: list[dict[str, Any]] | None = None) -> None: |
| 360 | # A plain string when there are no parts, so a text-only chat's |
| 361 | # outbound payload is byte-identical to what it always was. |
| 362 | content = (_content_with_parts_anthropic(message, parts) |
| 363 | if parts else message) |
| 364 | self.messages.append({"role": "user", "content": content}) |
| 365 | |
| 366 | def request(self) -> tuple[str, dict[str, str], bytes]: |
| 367 | payload: dict[str, Any] = { |
| 368 | "model": self.model, |
| 369 | "max_tokens": self.max_tokens, |
| 370 | "messages": self.messages, |
| 371 | } |
| 372 | if self.system: |
| 373 | payload["system"] = self.system |
| 374 | if self.tools: |
| 375 | payload["tools"] = self.tools |
| 376 | headers = { |
| 377 | "x-api-key": self.key, |
| 378 | "anthropic-version": ANTHROPIC_VERSION, |
| 379 | "content-type": "application/json", |
| 380 | } |
| 381 | return ANTHROPIC_URL, headers, json.dumps(payload).encode("utf-8") |
| 382 | |
| 383 | def parse(self, status: int, body: bytes) -> dict[str, Any]: |
| 384 | payload = _json_or_error(status, body) |
| 385 | content = payload.get("content") or [] |
| 386 | text_parts = [item.get("text", "") for item in content if item.get("type") == "text"] |
| 387 | tool_calls = [ |
| 388 | { |
| 389 | "id": item.get("id"), |
| 390 | "name": item.get("name"), |
| 391 | "arguments": item.get("input") or {}, |
| 392 | } |
| 393 | for item in content |
| 394 | if item.get("type") == "tool_use" |
| 395 | ] |
| 396 | raw_usage = payload.get("usage") or {} |
| 397 | self._last_content = content |
| 398 | return { |
| 399 | "text": "\n".join(part for part in text_parts if part), |
| 400 | "tool_calls": tool_calls, |
| 401 | "usage": { |
| 402 | "input_tokens": raw_usage.get("input_tokens", 0), |
| 403 | "output_tokens": raw_usage.get("output_tokens", 0), |
| 404 | }, |
| 405 | } |
| 406 | |
| 407 | def add_tool_results(self, parsed, results) -> None: |
| 408 | self.messages.append({"role": "assistant", "content": self._last_content}) |
| 409 | result_blocks = [] |
| 410 | for call, result in results: |
| 411 | result_blocks.append( |
| 412 | { |
| 413 | "type": "tool_result", |
| 414 | "tool_use_id": call["id"], |
| 415 | "content": json.dumps(result.get("response"), default=str)[:8000], |
| 416 | } |
| 417 | ) |
| 418 | self.messages.append({"role": "user", "content": result_blocks}) |
| 419 | |
| 420 | |
| 421 | class _OpenAIProvider: |
| 422 | def __init__(self, key, model, system, tools, max_tokens): |
| 423 | self.key = key |
| 424 | self.model = model |
| 425 | self.tools = tools |
| 426 | self.max_tokens = max_tokens |
| 427 | self.messages: list[dict[str, Any]] = [] |
| 428 | if system: |
| 429 | self.messages.append({"role": "system", "content": system}) |
| 430 | |
| 431 | def start(self, message: str, parts: list[dict[str, Any]] | None = None) -> None: |
| 432 | content = (_content_with_parts_openai(message, parts) |
| 433 | if parts else message) |
| 434 | self.messages.append({"role": "user", "content": content}) |
| 435 | |
| 436 | def request(self) -> tuple[str, dict[str, str], bytes]: |
| 437 | payload: dict[str, Any] = { |
| 438 | "model": self.model, |
| 439 | "messages": self.messages, |
| 440 | "max_completion_tokens": self.max_tokens, |
| 441 | } |
| 442 | if self.tools: |
| 443 | payload["tools"] = self.tools |
| 444 | if self.model in _REASONING_EFFORT_NONE_MODELS: |
| 445 | payload["reasoning_effort"] = "none" |
| 446 | headers = { |
| 447 | "authorization": f"Bearer {self.key}", |
| 448 | "content-type": "application/json", |
| 449 | } |
| 450 | return OPENAI_URL, headers, json.dumps(payload).encode("utf-8") |
| 451 | |
| 452 | def parse(self, status: int, body: bytes) -> dict[str, Any]: |
| 453 | payload = _json_or_error(status, body) |
| 454 | choices = payload.get("choices") or [] |
| 455 | if not choices: |
| 456 | raise AIProviderError("Provider returned no choices") |
| 457 | message = choices[0].get("message") or {} |
| 458 | raw_calls = message.get("tool_calls") or [] |
| 459 | tool_calls = [] |
| 460 | for item in raw_calls: |
| 461 | function = item.get("function") or {} |
| 462 | try: |
| 463 | arguments = json.loads(function.get("arguments") or "{}") |
| 464 | except json.JSONDecodeError: |
| 465 | arguments = {} |
| 466 | tool_calls.append( |
| 467 | {"id": item.get("id"), "name": function.get("name"), "arguments": arguments} |
| 468 | ) |
| 469 | raw_usage = payload.get("usage") or {} |
| 470 | self._last_message = message |
| 471 | return { |
| 472 | "text": message.get("content") or "", |
| 473 | "tool_calls": tool_calls, |
| 474 | "usage": { |
| 475 | "input_tokens": raw_usage.get("prompt_tokens", 0), |
| 476 | "output_tokens": raw_usage.get("completion_tokens", 0), |
| 477 | }, |
| 478 | } |
| 479 | |
| 480 | def add_tool_results(self, parsed, results) -> None: |
| 481 | self.messages.append(self._last_message) |
| 482 | for call, result in results: |
| 483 | self.messages.append( |
| 484 | { |
| 485 | "role": "tool", |
| 486 | "tool_call_id": call["id"], |
| 487 | "content": json.dumps(result.get("response"), default=str)[:8000], |
| 488 | } |
| 489 | ) |
| 490 | |
| 491 | |
| 492 | def select_price_row( |
| 493 | rows: list[dict[str, Any]], *, provider: str, model: str |
| 494 | ) -> dict[str, Any] | None: |
| 495 | """Pick the most recent ai_prices row for (provider, model), or None. |
| 496 | |
| 497 | "Most recent" is the matching row with the greatest ``effective_date`` |
| 498 | (ISO 8601 strings sort lexicographically). Prices are ordinary records — |
| 499 | editable live, never hardcoded — so a missing row is expected, not an |
| 500 | error; the caller decides what that means (it records tokens with a |
| 501 | null cost rather than failing the chat). |
| 502 | """ |
| 503 | candidates = [ |
| 504 | row for row in rows if row.get("provider") == provider and row.get("model") == model |
| 505 | ] |
| 506 | if not candidates: |
| 507 | return None |
| 508 | return max(candidates, key=lambda row: row.get("effective_date") or "") |
| 509 | |
| 510 | |
| 511 | def compute_cost_cents( |
| 512 | tokens_in: int, tokens_out: int, price_row: dict[str, Any] | None |
| 513 | ) -> int | None: |
| 514 | """Integer-cent cost for a turn, or None when there is no price row. |
| 515 | |
| 516 | Money math stays in integer cents throughout: sum the raw |
| 517 | tokens*per_million_cents products for both sides first, then floor |
| 518 | once -- never a float division on money. Flooring each side |
| 519 | separately before summing (the previous approach) silently |
| 520 | undercounts cheap models: e.g. gpt-5-mini at 25c/M input, 200c/M |
| 521 | output floors a 36,428-token call to 0+0=0c even though the combined |
| 522 | cost is a real ~1c. |
| 523 | """ |
| 524 | if price_row is None: |
| 525 | return None |
| 526 | input_rate = int(price_row.get("input_per_million_cents") or 0) |
| 527 | output_rate = int(price_row.get("output_per_million_cents") or 0) |
| 528 | return (tokens_in * input_rate + tokens_out * output_rate) // 1_000_000 |
| 529 | |
| 530 | |
| 531 | def _json_or_error(status: int, body: bytes) -> dict[str, Any]: |
| 532 | try: |
| 533 | payload = json.loads(body.decode("utf-8")) |
| 534 | except (UnicodeDecodeError, json.JSONDecodeError) as exc: |
| 535 | raise AIProviderError(f"Provider returned unreadable response (HTTP {status})") from exc |
| 536 | if status >= 400: |
| 537 | detail = payload.get("error") |
| 538 | if isinstance(detail, dict): |
| 539 | detail = detail.get("message") or json.dumps(detail) |
| 540 | raise AIProviderError(f"Provider error (HTTP {status}): {str(detail)[:300]}") |
| 541 | if not isinstance(payload, dict): |
| 542 | raise AIProviderError("Provider returned a non-object response") |
| 543 | return payload |