free-claude-code/providers/open_router/request.py
Alishahryar1 f3a7528d49
Some checks are pending
CI / checks (push) Waiting to run
Major refactor: API, providers, messaging, and Anthropic protocol
Consolidates the incremental refactor work into a single change set: modular web tools (api/web_tools), native Anthropic request building and SSE block policy, OpenAI conversion and error handling, provider transports and rate limiting, messaging handler and tree queue, safe logging, smoke tests, and broad test coverage.
2026-04-26 03:01:14 -07:00

42 lines
1.3 KiB
Python

"""Native Anthropic Messages request builder for OpenRouter."""
from __future__ import annotations
from typing import Any
from loguru import logger
from config.constants import (
ANTHROPIC_DEFAULT_MAX_OUTPUT_TOKENS as OPENROUTER_DEFAULT_MAX_TOKENS,
)
from core.anthropic.native_messages_request import (
OpenRouterExtraBodyError,
build_openrouter_native_request_body,
)
from providers.exceptions import InvalidRequestError
def build_request_body(request_data: Any, *, thinking_enabled: bool) -> dict:
"""Build an Anthropic-format request body for OpenRouter's messages API."""
logger.debug(
"OPENROUTER_REQUEST: conversion start model={} msgs={}",
getattr(request_data, "model", "?"),
len(getattr(request_data, "messages", [])),
)
try:
body = build_openrouter_native_request_body(
request_data,
thinking_enabled=thinking_enabled,
default_max_tokens=OPENROUTER_DEFAULT_MAX_TOKENS,
)
except OpenRouterExtraBodyError as exc:
raise InvalidRequestError(str(exc)) from exc
logger.debug(
"OPENROUTER_REQUEST: conversion done model={} msgs={} tools={}",
body.get("model"),
len(body.get("messages", [])),
len(body.get("tools", [])),
)
return body