mirror of
https://github.com/Alishahryar1/free-claude-code.git
synced 2026-04-28 03:20:01 +00:00
43 lines
1.3 KiB
Python
43 lines
1.3 KiB
Python
"""OpenAI chat-completions request builder for OpenRouter rollback mode."""
|
|
|
|
from typing import Any
|
|
|
|
from loguru import logger
|
|
|
|
from core.anthropic import build_base_request_body
|
|
|
|
OPENROUTER_DEFAULT_MAX_TOKENS = 81920
|
|
|
|
|
|
def build_chat_request_body(request_data: Any, *, thinking_enabled: bool) -> dict:
|
|
"""Build OpenAI-format request body from Anthropic request for OpenRouter."""
|
|
logger.debug(
|
|
"OPENROUTER_CHAT_REQUEST: conversion start model={} msgs={}",
|
|
getattr(request_data, "model", "?"),
|
|
len(getattr(request_data, "messages", [])),
|
|
)
|
|
body = build_base_request_body(
|
|
request_data,
|
|
include_thinking=thinking_enabled,
|
|
default_max_tokens=OPENROUTER_DEFAULT_MAX_TOKENS,
|
|
include_reasoning_for_openrouter=thinking_enabled,
|
|
)
|
|
|
|
extra_body: dict[str, Any] = {}
|
|
request_extra = getattr(request_data, "extra_body", None)
|
|
if request_extra:
|
|
extra_body.update(request_extra)
|
|
|
|
if thinking_enabled:
|
|
extra_body.setdefault("reasoning", {"enabled": True})
|
|
|
|
if extra_body:
|
|
body["extra_body"] = extra_body
|
|
|
|
logger.debug(
|
|
"OPENROUTER_CHAT_REQUEST: conversion done model={} msgs={} tools={}",
|
|
body.get("model"),
|
|
len(body.get("messages", [])),
|
|
len(body.get("tools", [])),
|
|
)
|
|
return body
|