from __future__ import annotations import reprlib import uuid from dataclasses import dataclass from datetime import datetime, timezone from typing import Any import orjson from gemini_webapi.constants import Model from loguru import logger from ..models import ( ContentItem, Message, ModelData, ResponseCreateRequest, ResponseCreateResponse, ResponseImageGenerationCall, ResponseInputContent, ResponseInputItem, ResponseOutputContent, ResponseOutputMessage, ResponseReasoning, ResponseSummaryPart, ResponseToolCall, ResponseToolChoice, ResponseUsage, Tool, ToolChoiceFunction, ) from ..utils import g_config from ..utils.helper import ( TOOL_HINT_STRIPPED, TOOL_WRAP_HINT, estimate_tokens, extract_tool_calls, remove_tool_call_blocks, ) @dataclass class StructuredOutputRequirement: """Represents a structured response requirement derived from response_format.""" schema_name: str schema: dict[str, Any] instruction: str raw_format: dict[str, Any] def calculate_usage( messages: list[Message], assistant_text: str | None, tool_calls: list[Any] | None, thoughts: str | None = None, ) -> tuple[int, int, int, int]: """Calculate prompt, completion, total and reasoning tokens consistently.""" prompt_tokens = 0 for msg in messages: if isinstance(msg.content, str): prompt_tokens += estimate_tokens(msg.content) elif isinstance(msg.content, list): text_parts: list[str] = [] for item in msg.content: if isinstance(item, dict): if item.get("type") == "text": text_val = item.get("text") if isinstance(text_val, str) and text_val: text_parts.append(text_val) continue if getattr(item, "type", "") == "text": text_val = getattr(item, "text", None) if isinstance(text_val, str) and text_val: text_parts.append(text_val) prompt_tokens += estimate_tokens("\n".join(text_parts)) tool_args_text = "" if tool_calls: for call in tool_calls: if hasattr(call, "function"): tool_args_text += call.function.arguments or "" elif isinstance(call, dict): tool_args_text += call.get("function", {}).get("arguments", "") completion_basis = assistant_text or "" if tool_args_text: completion_basis = ( f"{completion_basis}\n{tool_args_text}" if completion_basis else tool_args_text ) completion_tokens = estimate_tokens(completion_basis) reasoning_tokens = estimate_tokens(thoughts) if thoughts else 0 total_completion_tokens = completion_tokens + reasoning_tokens return ( prompt_tokens, total_completion_tokens, prompt_tokens + total_completion_tokens, reasoning_tokens, ) def create_responses_standard_payload( response_id: str, created_time: int, model_name: str, detected_tool_calls: list[Any] | None, image_call_items: list[ResponseImageGenerationCall], response_contents: list[ResponseOutputContent], usage: ResponseUsage, request: ResponseCreateRequest, normalized_input: Any, full_thoughts: str | None = None, ) -> ResponseCreateResponse: """Build a standard Responses API payload.""" message_id = f"msg_{uuid.uuid4().hex[:24]}" reason_id = f"rs_{uuid.uuid4().hex[:24]}" output_items: list[Any] = [] if full_thoughts: output_items.append( ResponseReasoning( id=reason_id, type="reasoning", status="completed", summary=[ResponseSummaryPart(type="summary_text", text=full_thoughts)], ) ) output_items.append( ResponseOutputMessage( id=message_id, type="message", status="completed", role="assistant", content=response_contents, ) ) if detected_tool_calls: output_items.extend( [ ResponseToolCall( id=call.id if hasattr(call, "id") else call["id"], type="tool_call", status="completed", function=call.function if hasattr(call, "function") else call["function"], ) for call in detected_tool_calls ] ) output_items.extend(image_call_items) return ResponseCreateResponse( id=response_id, object="response", created_at=created_time, model=model_name, output=output_items, status="completed", usage=usage, input=normalized_input or None, metadata=request.metadata or {}, tools=request.tools or [], tool_choice=request.tool_choice or "auto", ) def create_chat_completion_standard_payload( completion_id: str, created_time: int, model_name: str, visible_output: str | None, tool_calls_payload: list[dict[str, Any]] | None, finish_reason: str, usage: dict[str, Any], reasoning_content: str | None = None, ) -> dict[str, Any]: """Build a standard OpenAI chat completion payload.""" return { "id": completion_id, "object": "chat.completion", "created": created_time, "model": model_name, "choices": [ { "index": 0, "message": { "role": "assistant", "content": visible_output or None, "tool_calls": tool_calls_payload or None, "reasoning_content": reasoning_content or None, }, "finish_reason": finish_reason, } ], "usage": usage, } def process_llm_output( thoughts: str | None, raw_text: str, structured_requirement: StructuredOutputRequirement | None, ) -> tuple[str | None, str, str, list[Any]]: """ Post-process Gemini output to extract tool calls and prepare clean text. Returns: (thoughts, visible_text, storage_output, tool_calls) """ if thoughts: thoughts = thoughts.strip() visible_output, tool_calls = extract_tool_calls(raw_text) if tool_calls: logger.debug(f"Detected {len(tool_calls)} tool call(s) in model output.") visible_output = visible_output.strip() storage_output = remove_tool_call_blocks(raw_text) storage_output = storage_output.strip() if structured_requirement and visible_output: try: structured_payload = orjson.loads(visible_output) canonical_output = orjson.dumps(structured_payload).decode("utf-8") visible_output = canonical_output storage_output = canonical_output logger.debug( f"Structured response fulfilled (schema={structured_requirement.schema_name})." ) except orjson.JSONDecodeError: logger.warning( f"Failed to decode JSON for structured response (schema={structured_requirement.schema_name})." ) return thoughts, visible_output, storage_output, tool_calls def build_structured_requirement( response_format: dict[str, Any] | None, ) -> StructuredOutputRequirement | None: """Translate OpenAI-style response_format into internal instructions.""" if not response_format or not isinstance(response_format, dict): return None if response_format.get("type") != "json_schema": logger.warning( f"Unsupported response_format type requested: {reprlib.repr(response_format)}" ) return None json_schema = response_format.get("json_schema") if not isinstance(json_schema, dict): logger.warning( f"Invalid json_schema payload in response_format: {reprlib.repr(response_format)}" ) return None schema = json_schema.get("schema") if not isinstance(schema, dict): logger.warning( f"Missing `schema` object in response_format payload: {reprlib.repr(response_format)}" ) return None schema_name = json_schema.get("name") or "response" strict = json_schema.get("strict", True) pretty_schema = orjson.dumps(schema, option=orjson.OPT_SORT_KEYS).decode("utf-8") instruction_parts = [ "You must respond with a single valid JSON document that conforms to the schema shown below.", "Do not include explanations, comments, or any text before or after the JSON.", f'Schema name: "{schema_name}"', "JSON Schema:", pretty_schema, ] if not strict: instruction_parts.insert( 1, "The schema allows unspecified fields, but include only what is necessary " "to satisfy the user's request.", ) instruction = "\n\n".join(instruction_parts) return StructuredOutputRequirement( schema_name=schema_name, schema=schema, instruction=instruction, raw_format=response_format, ) def build_tool_prompt( tools: list[Tool], tool_choice: str | ToolChoiceFunction | None, ) -> str: """Generate a system prompt describing available tools.""" if not tools: return "" lines: list[str] = [ "You can invoke the following developer tools. " "Call a tool only when it is required and follow the JSON schema exactly " "when providing arguments." ] for tool in tools: function = tool.function description = function.description or "No description provided." lines.append(f"Tool `{function.name}`: {description}") if function.parameters: schema_text = orjson.dumps( function.parameters, option=orjson.OPT_SORT_KEYS, ).decode("utf-8") lines.append("Arguments JSON schema:") lines.append(schema_text) else: lines.append("Arguments JSON schema: {}") if tool_choice == "none": lines.append( "For this request you must not call any tool. " "Provide the best possible natural language answer." ) elif tool_choice == "required": lines.append( "You must call at least one tool before responding to the user. " "Do not provide a final user-facing answer until a tool call has been issued." ) elif isinstance(tool_choice, ToolChoiceFunction): target = tool_choice.function.name lines.append( f"You are required to call the tool named `{target}`. Do not call any other tool." ) lines.append( "When you decide to call a tool you MUST respond with nothing except a single " "[function_calls] block exactly like the template below." ) lines.append("Do not add text before or after the block.") lines.append("[function_calls]") lines.append('[call:tool_name]{"argument": "value"}[/call]') lines.append("[/function_calls]") lines.append( "Use double quotes for JSON keys and values. CRITICAL: The content inside " "[call:...]...[/call] MUST be a raw JSON object. Do not wrap it in ```json " "blocks or add any conversational text inside the tag." ) lines.append( "To call multiple tools, list each [call:tool_name]...[/call] entry sequentially " "within a single [function_calls] block." ) lines.append( "If no tool call is needed, provide a normal response and DO NOT use the " "[function_calls] tag." ) return "\n".join(lines) def build_image_generation_instruction( tools: list[Any] | None, tool_choice: ResponseToolChoice | None, ) -> str | None: """Construct explicit guidance so Gemini emits images when requested.""" has_forced_choice = tool_choice is not None and tool_choice.type == "image_generation" primary = tools[0] if tools else None if not has_forced_choice and primary is None: return None instructions: list[str] = [ "Image generation is enabled. When the user requests an image, you must return " "an actual generated image, not a text description.", "For new image requests, generate at least one new image matching the description.", "If the user provides an image and asks for edits or variations, return a newly " "generated image with the requested changes.", "Avoid all text replies unless a short caption is explicitly requested. " "Do not explain, apologize, or describe image creation steps.", "Never send placeholder text like 'Here is your image' or any other response " "without an actual image attachment.", ] if primary: primary_model = getattr(primary, "model", None) primary_format = getattr(primary, "output_format", None) if primary_model: instructions.append( f"Where styles differ, favor the `{primary_model}` image model when rendering the scene." ) if primary_format: instructions.append( f"Encode the image using the `{primary_format}` format whenever possible." ) if has_forced_choice: instructions.append( "Image generation was explicitly requested. You must return at least one " "generated image. Any response without an image will be treated as a failure." ) return "\n\n".join(instructions) def append_tool_hint_to_last_user_message(messages: list[Message]) -> None: """Ensure the last user message carries the tool-call formatting hint.""" for msg in reversed(messages): if msg.role != "user" or msg.content is None: continue if isinstance(msg.content, str): if TOOL_HINT_STRIPPED not in msg.content: msg.content = f"{msg.content}\n{TOOL_WRAP_HINT}" return if isinstance(msg.content, list): for part in reversed(msg.content): if isinstance(part, dict): if part.get("type") != "text": continue text_value = part.get("text") or "" if TOOL_HINT_STRIPPED in str(text_value): return part["text"] = f"{text_value}\n{TOOL_WRAP_HINT}" return if getattr(part, "type", None) != "text": continue text_value = getattr(part, "text", "") or "" if TOOL_HINT_STRIPPED in text_value: return setattr(part, "text", f"{text_value}\n{TOOL_WRAP_HINT}") return msg.content.append(ContentItem(type="text", text=TOOL_WRAP_HINT.strip())) # type: ignore[arg-type] return def prepare_messages_for_model( source_messages: list[Message], tools: list[Tool] | None, tool_choice: str | ToolChoiceFunction | None, extra_instructions: list[str] | None = None, inject_system_defaults: bool = True, ) -> list[Message]: """Return a deep-copied message list enriched with internal instructions.""" prepared = [msg.model_copy(deep=True) for msg in source_messages] tool_id_to_name: dict[str, str] = {} for msg in prepared: if msg.role == "assistant" and msg.tool_calls: for tc in msg.tool_calls: tool_id_to_name[tc.id] = tc.function.name for msg in prepared: if msg.role == "tool" and not msg.name and msg.tool_call_id: msg.name = tool_id_to_name.get(msg.tool_call_id) instructions: list[str] = [] if inject_system_defaults: if tools: tool_prompt = build_tool_prompt(tools, tool_choice) if tool_prompt: instructions.append(tool_prompt) if extra_instructions: instructions.extend(instr for instr in extra_instructions if instr) logger.debug( f"Applied {len(extra_instructions)} extra instructions for tool/structured output." ) if not instructions: if tools and tool_choice != "none": append_tool_hint_to_last_user_message(prepared) return prepared combined_instructions = "\n\n".join(instructions) if prepared and prepared[0].role == "system" and isinstance(prepared[0].content, str): existing = prepared[0].content or "" if combined_instructions not in existing: separator = "\n\n" if existing else "" prepared[0].content = f"{existing}{separator}{combined_instructions}" else: prepared.insert(0, Message(role="system", content=combined_instructions)) if tools and tool_choice != "none": append_tool_hint_to_last_user_message(prepared) return prepared def response_items_to_messages( items: str | list[ResponseInputItem], ) -> tuple[list[Message], str | list[ResponseInputItem]]: """Convert Responses API input items into internal Message objects.""" messages: list[Message] = [] if isinstance(items, str): messages.append(Message(role="user", content=items)) logger.debug("Normalized Responses input: single string message.") return messages, items normalized_input: list[ResponseInputItem] = [] for item in items: role = item.role content = item.content normalized_contents: list[ResponseInputContent] = [] if isinstance(content, str): normalized_contents.append(ResponseInputContent(type="input_text", text=content)) messages.append(Message(role=role, content=content)) else: converted: list[ContentItem] = [] for part in content: if part.type == "input_text": text_value = part.text or "" normalized_contents.append( ResponseInputContent(type="input_text", text=text_value) ) if text_value: converted.append(ContentItem(type="text", text=text_value)) elif part.type == "input_image": image_url = part.image_url if image_url: normalized_contents.append( ResponseInputContent( type="input_image", image_url=image_url, detail=part.detail if part.detail else "auto", ) ) converted.append( ContentItem( type="image_url", image_url={ "url": image_url, "detail": part.detail if part.detail else "auto", }, ) ) # type: ignore[arg-type] elif part.type == "input_file": if part.file_url or part.file_data: normalized_contents.append(part) file_info: dict[str, str | None] = {} if part.file_data: file_info["file_data"] = part.file_data file_info["filename"] = part.filename if part.file_url: file_info["url"] = part.file_url converted.append(ContentItem(type="file", file=file_info)) # type: ignore[arg-type] messages.append(Message(role=role, content=converted or None)) # type: ignore[arg-type, assignment] normalized_input.append( ResponseInputItem( type="message", role=item.role, content=normalized_contents or [], ) ) logger.debug(f"Normalized Responses input: {len(normalized_input)} message items.") return messages, normalized_input def instructions_to_messages( instructions: str | list[ResponseInputItem] | None, ) -> list[Message]: """Normalize a Responses API instructions payload into Message objects.""" if not instructions: return [] if isinstance(instructions, str): return [Message(role="system", content=instructions)] instruction_messages: list[Message] = [] for item in instructions: if item.type and item.type != "message": continue role = item.role content = item.content if isinstance(content, str): instruction_messages.append(Message(role=role, content=content)) continue converted: list[ContentItem] = [] for part in content: if part.type == "input_text": text_value = part.text or "" if text_value: converted.append(ContentItem(type="text", text=text_value)) elif part.type == "input_image": image_url = part.image_url if image_url: converted.append( ContentItem( type="image_url", image_url={ "url": image_url, "detail": part.detail if part.detail else "auto", }, ) ) # type: ignore[arg-type] elif part.type == "input_file": file_info: dict[str, str | None] = {} if part.file_data: file_info["file_data"] = part.file_data file_info["filename"] = part.filename if part.file_url: file_info["url"] = part.file_url if file_info: converted.append(ContentItem(type="file", file=file_info)) # type: ignore[arg-type] instruction_messages.append(Message(role=role, content=converted or None)) # type: ignore[arg-type, assignment] return instruction_messages def get_default_model_name() -> str: """Return the configured default model used by the server.""" return g_config.gemini.default_model.strip() or "gemini-3.0-pro" def get_model_by_name(name: str) -> Model: """Resolve a configured or built-in model by name.""" strategy = g_config.gemini.model_strategy custom_models = {m.model_name: m for m in g_config.gemini.models if m.model_name} if name in custom_models: return Model.from_dict(custom_models[name].model_dump()) if strategy == "overwrite": raise ValueError(f"Model '{name}' not found in custom models (strategy='overwrite').") return Model.from_name(name) def get_available_models() -> list[ModelData]: """Return visible models according to the configured model strategy.""" now = int(datetime.now(tz=timezone.utc).timestamp()) strategy = g_config.gemini.model_strategy models_data: list[ModelData] = [] custom_models = [m for m in g_config.gemini.models if m.model_name] for model_cfg in custom_models: models_data.append( ModelData( id=str(model_cfg.model_name), created=now, owned_by="custom", ) ) if strategy == "append": custom_ids = {m.model_name for m in custom_models} for model in Model: model_name = model.model_name if not model_name or model_name == "unspecified": continue if model_name in custom_ids: continue models_data.append( ModelData( id=model_name, created=now, owned_by="gemini-web", ) ) return models_data