Skip to content

Make Codex and Claude Code subscriptions go further: a local model router that gives routine agent work to economical models and keeps your strongest model for the hard parts.

46.2 KB
Raw Blame
#[path = "responses_ingress.rs"]
mod responses;
use responses::{decode_responses_input, decode_responses_tools};

use std::collections::BTreeSet;

use serde_json::{Map, Value};

use crate::content_ref::{ContentRef, JsonValueExt};
use crate::replay::ReplayStore;
use crate::server::core_runtime::model_ir::*;
use crate::server::request_plan::IngressProtocol;

#[path = "responses_options.rs"]
mod responses_options;

mod chat_reasoning;

#[derive(Default)]
struct DecodeContext<'a> {
    native_only: std::cell::Cell<bool>,
    replay: Option<&'a ReplayStore>,
}

pub fn decode_ingress_request(
    protocol: IngressProtocol,
    body: &Value,
) -> Result<ModelRequestIRV1, ModelIrError> {
    decode_ingress_request_with_replay(protocol, body, None)
}

pub(crate) fn decode_ingress_request_with_replay(
    protocol: IngressProtocol,
    body: &Value,
    replay: Option<&ReplayStore>,
) -> Result<ModelRequestIRV1, ModelIrError> {
    let context = &DecodeContext {
        replay,
        ..DecodeContext::default()
    };
    let mut request = match protocol {
        IngressProtocol::Responses => decode_responses(context, body),
        IngressProtocol::ChatCompletions => decode_chat(context, body),
        IngressProtocol::Messages => decode_messages(context, body),
    }?;
    request.native_body = Some(body.clone());
    request.native_only = context.native_only.get();
    Ok(request)
}

fn validate_responses_named_choice(
    tools: &responses::DecodedResponsesTools,
    choice: &ToolChoice,
) -> Result<(), ModelIrError> {
    let mut namespace_names = BTreeSet::new();
    for namespace in &tools.namespaces {
        if !namespace_names.insert(namespace.name.as_str()) {
            return Err(ModelIrError::InvalidField("duplicate namespace name"));
        }
        let mut child_names = BTreeSet::new();
        if namespace
            .tools
            .iter()
            .any(|tool| !child_names.insert(tool.name.as_str()))
        {
            return Err(ModelIrError::InvalidField("duplicate namespace tool name"));
        }
    }
    let ToolChoice::RequiredNamed { tool_kind, name } = choice else {
        return Ok(());
    };
    let flat_matches = tools
        .tools
        .iter()
        .filter(|tool| tool.kind == *tool_kind && tool.name == *name)
        .count();
    let namespace_matches = tools
        .namespaces
        .iter()
        .flat_map(|namespace| namespace.tools.iter())
        .filter(|tool| tool.kind == *tool_kind && tool.name == *name)
        .count();
    if flat_matches.saturating_add(namespace_matches) != 1 {
        return Err(ModelIrError::UnsupportedField(
            "ambiguous responses named tool choice".into(),
        ));
    }
    Ok(())
}

fn decode_responses(
    context: &DecodeContext,
    body: &Value,
) -> Result<ModelRequestIRV1, ModelIrError> {
    let object = checked_object(
        context,
        body,
        &[
            "model",
            "stream",
            "instructions",
            "input",
            "tools",
            "tool_choice",
            "parallel_tool_calls",
            "reasoning",
            "max_output_tokens",
            "previous_response_id",
            "conversation",
            "store",
            "include",
            "prompt_cache_key",
            "client_metadata",
        ],
        "responses request",
    )?;
    let served_model_id = required_string(object, "model")?;
    let instructions = object
        .get("instructions")
        .map(|value| decode_instruction(context, value, InstructionRole::System))
        .transpose()?
        .into_iter()
        .collect();
    let mut messages = Vec::new();
    let mut additional_tools = Vec::new();
    let mut responses_search_history = std::collections::BTreeMap::new();
    let mut responses_item_statuses = std::collections::BTreeMap::new();
    let mut responses_message_phases = std::collections::BTreeMap::new();
    let mut responses_internal_chat_message_metadata = std::collections::BTreeMap::new();
    let mut responses_reasoning_history = std::collections::BTreeMap::new();
    match object.get("input") {
        Some(Value::String(text)) => messages.push(CanonicalMessage {
            role: MessageRole::User,
            content: vec![ContentPart::Text { text: text.clone() }],
            name: None,
        }),
        Some(Value::Array(items)) => {
            for item in items {
                if item.get("type").and_then(Value::as_str) == Some("additional_tools") {
                    let additional =
                        checked_object(context, item, &["type", "tools"], "additional_tools")?;
                    additional_tools.push(
                        additional
                            .get("tools")
                            .ok_or(ModelIrError::InvalidField("additional_tools.tools"))?,
                    );
                    continue;
                }
                let message_index = messages.len();
                let internal_metadata =
                    responses_options::internal_chat_message_metadata(context, item)?;
                let item_type = item.get("type").and_then(Value::as_str);
                if item_type == Some("web_search_call") {
                    let mut native_item = item.clone();
                    native_item
                        .as_object_mut()
                        .ok_or(ModelIrError::InvalidField("web_search_call"))?
                        .remove("internal_chat_message_metadata_passthrough");
                    let call = WebSearchCallV1::from_wire(&native_item)?;
                    if !matches!(
                        call.status,
                        WebSearchStatus::Completed | WebSearchStatus::Failed
                    ) {
                        return Err(ModelIrError::InvalidField("unfinished search history"));
                    }
                    responses_search_history.insert(messages.len(), call);
                    messages.push(CanonicalMessage {
                        role: MessageRole::Assistant,
                        content: Vec::new(),
                        name: None,
                    });
                } else {
                    let item_status = item
                        .get("status")
                        .filter(|status| item_type != Some("reasoning") || !status.is_null())
                        .map(|status| {
                            status
                                .as_str()
                                .ok_or(ModelIrError::InvalidField("Responses input item status"))
                        })
                        .transpose()?;
                    if let Some(status) = item_status
                        && status != "completed"
                        && !(item_type == Some("function_call_output")
                            && matches!(status, "incomplete" | "in_progress"))
                    {
                        context.native_only.set(true);
                    }
                    decode_responses_input(context, item, &mut messages, item_status)?;
                    if let Some(status) = item_status {
                        responses_item_statuses.insert(message_index, status.into());
                    }
                }
                if let Some(metadata) = internal_metadata {
                    responses_internal_chat_message_metadata.insert(message_index, metadata);
                }
                if matches!(item_type, Some("message") | None)
                    && let Some(phase) = responses_options::message_phase(context, item)?
                {
                    responses_message_phases.insert(message_index, phase);
                }
                if item_type == Some("reasoning") {
                    responses_reasoning_history
                        .insert(message_index, responses_options::reasoning_history(item)?);
                }
            }
        }
        Some(_) => return Err(ModelIrError::InvalidField("input")),
        None => return Err(ModelIrError::InvalidField("input")),
    }
    let provider_state = decode_responses_state(object)?;
    let tools = decode_responses_tools(context, object.get("tools"), &additional_tools)?;
    let tool_choice = decode_tool_choice(
        context,
        IngressProtocol::Responses,
        object.get("tool_choice"),
    )?;
    validate_responses_named_choice(&tools, &tool_choice)?;
    Ok(ModelRequestIRV1 {
        native_body: None,
        native_only: false,
        schema_version: MODEL_REQUEST_IR_SCHEMA.into(),
        ingress_protocol: IngressProtocol::Responses,
        responses_options: responses_options::decode(context, object)?,
        responses_item_ids: responses_options::item_ids(object.get("input"))?,
        served_model_id,
        stream: optional_bool(object, "stream")?.unwrap_or(false),
        instructions,
        messages,
        tools: tools.tools,
        tool_namespaces: tools.namespaces,
        responses_tool_order: tools.order,
        web_search: tools.web_search,
        responses_search_history,
        responses_annotations: responses_options::annotations(object.get("input"))?,
        responses_item_statuses,
        responses_message_phases,
        responses_internal_chat_message_metadata,
        responses_reasoning_history,
        tool_choice,
        parallel_tool_calls: optional_bool(object, "parallel_tool_calls")?.unwrap_or(false),
        requested_reasoning: object
            .get("reasoning")
            .cloned()
            .map(RequestedReasoningControl::overridden)
            .unwrap_or_else(RequestedReasoningControl::absent),
        requested_max_output_tokens: optional_u64(object, "max_output_tokens")?,
        provider_state,
    })
}

fn decode_chat(context: &DecodeContext, body: &Value) -> Result<ModelRequestIRV1, ModelIrError> {
    let object = checked_object(
        context,
        body,
        &[
            "model",
            "stream",
            "messages",
            "tools",
            "tool_choice",
            "parallel_tool_calls",
            "reasoning_effort",
            "max_tokens",
            "max_completion_tokens",
            "stream_options",
        ],
        "chat request",
    )?;
    if let Some(stream_options) = object.get("stream_options") {
        let options = checked_object(
            context,
            stream_options,
            &["include_usage"],
            "stream_options",
        )?;
        if optional_bool(options, "include_usage")? == Some(false) {
            context.native_only.set(true);
        }
    }
    let mut instructions = Vec::new();
    let mut messages = Vec::new();
    for value in required_array(object, "messages")? {
        decode_chat_message(context, value, &mut instructions, &mut messages)?;
    }
    let requested_reasoning = object
        .get("reasoning_effort")
        .cloned()
        .map(RequestedReasoningControl::overridden)
        .unwrap_or_else(RequestedReasoningControl::absent);
    Ok(ModelRequestIRV1 {
        native_body: None,
        native_only: false,
        schema_version: MODEL_REQUEST_IR_SCHEMA.into(),
        ingress_protocol: IngressProtocol::ChatCompletions,
        responses_options: None,
        responses_item_ids: Default::default(),
        responses_item_statuses: Default::default(),
        served_model_id: required_string(object, "model")?,
        stream: optional_bool(object, "stream")?.unwrap_or(false),
        instructions,
        messages,
        tools: decode_chat_tools(context, object.get("tools"))?,
        tool_namespaces: Vec::new(),
        responses_tool_order: Vec::new(),
        web_search: None,
        responses_search_history: Default::default(),
        responses_annotations: Default::default(),
        responses_message_phases: Default::default(),
        responses_internal_chat_message_metadata: Default::default(),
        responses_reasoning_history: Default::default(),
        tool_choice: decode_tool_choice(
            context,
            IngressProtocol::ChatCompletions,
            object.get("tool_choice"),
        )?,
        parallel_tool_calls: optional_bool(object, "parallel_tool_calls")?.unwrap_or(false),
        requested_reasoning,
        requested_max_output_tokens: compatible_max_output(object)?,
        provider_state: Vec::new(),
    })
}

fn decode_chat_message(
    context: &DecodeContext,
    value: &Value,
    instructions: &mut Vec<CanonicalInstruction>,
    messages: &mut Vec<CanonicalMessage>,
) -> Result<(), ModelIrError> {
    let object = checked_object(
        context,
        value,
        &[
            "role",
            "content",
            "name",
            "tool_calls",
            "tool_call_id",
            "reasoning_content",
            "reasoning",
        ],
        "chat message",
    )?;
    let role = required_string(object, "role")?;
    let reasoning = chat_reasoning::canonical_reasoning(context, object, &role);
    if role == "system" || role == "developer" {
        ensure_absent(
            object,
            &["name", "tool_calls", "tool_call_id", "reasoning_content"],
            "chat instruction",
        )?;
        let instruction_role = if role == "system" {
            InstructionRole::System
        } else {
            InstructionRole::Developer
        };
        let content = object
            .get("content")
            .ok_or(ModelIrError::InvalidField("content"))?;
        let instruction = decode_instruction(context, content, instruction_role)?;
        if messages.is_empty() {
            instructions.push(instruction);
        } else {
            messages.push(CanonicalMessage {
                role: if role == "system" {
                    MessageRole::System
                } else {
                    MessageRole::Developer
                },
                content: instruction.content,
                name: None,
            });
        }
        return Ok(());
    }
    if role == "tool" {
        ensure_absent(
            object,
            &["tool_calls", "reasoning_content"],
            "chat Tool message",
        )?;
        messages.push(CanonicalMessage {
            role: MessageRole::User,
            content: vec![ContentPart::ToolResult {
                logical_id: required_string(object, "tool_call_id")?,
                tool_kind: ToolKindV1::Function,
                output: decode_tool_output(
                    object
                        .get("content")
                        .ok_or(ModelIrError::InvalidField("content"))?,
                ),
                status: ToolResultStatusV1::Unknown,
            }],
            name: optional_string(object, "name")?,
        });
        return Ok(());
    }
    let mut content = object
        .get("content")
        .map(|value| decode_chat_content(context, value))
        .transpose()?
        .unwrap_or_default();
    if let Some(reasoning) = reasoning {
        if role != "assistant" || (!reasoning.is_string() && reasoning.content_ref().is_none()) {
            return Err(ModelIrError::InvalidField("reasoning_content"));
        }
        content.push(ContentPart::ProviderState {
            state: Box::new(OpaqueProviderState {
                block_index: None,
                kind: "reasoning_content".into(),
                value: reasoning.clone(),
            }),
        });
    }
    if let Some(tool_calls) = object.get("tool_calls") {
        if role != "assistant" {
            return Err(ModelIrError::InvalidField("tool_calls"));
        }
        for value in tool_calls
            .as_array()
            .ok_or(ModelIrError::InvalidField("tool_calls"))?
        {
            let call = checked_object(
                context,
                value,
                &["id", "type", "function"],
                "chat tool call",
            )?;
            if required_string(call, "type")? != "function" {
                return Err(ModelIrError::UnsupportedValue(
                    "chat non-function Tool".into(),
                ));
            }
            let function = checked_object(
                context,
                call.get("function")
                    .ok_or(ModelIrError::InvalidField("function"))?,
                &["name", "arguments"],
                "chat function",
            )?;
            let (arguments, raw_arguments) =
                parse_tool_arguments(content_string(function, "arguments")?);
            content.push(ContentPart::ToolCall {
                logical_id: required_string(call, "id")?,
                tool_kind: ToolKindV1::Function,
                namespace: None,
                name: required_string(function, "name")?,
                arguments,
                raw_arguments,
            });
        }
    }
    messages.push(CanonicalMessage {
        role: decode_role(&role)?,
        content,
        name: optional_string(object, "name")?,
    });
    Ok(())
}

fn decode_chat_content(
    context: &DecodeContext,
    value: &Value,
) -> Result<Vec<ContentPart>, ModelIrError> {
    match value {
        Value::Null => Ok(Vec::new()),
        Value::String(text) => Ok(vec![ContentPart::Text { text: text.clone() }]),
        Value::Array(values) => values
            .iter()
            .map(|value| {
                let object = checked_object(
                    context,
                    value,
                    &["type", "text", "image_url"],
                    "chat content",
                )?;
                match required_string(object, "type")?.as_str() {
                    "text" => Ok(ContentPart::Text {
                        text: content_string(object, "text")?,
                    }),
                    "image_url" => {
                        let image = checked_object(
                            context,
                            object
                                .get("image_url")
                                .ok_or(ModelIrError::InvalidField("image_url"))?,
                            &["url"],
                            "chat image_url",
                        )?;
                        Ok(ContentPart::Image {
                            source: decode_image_url(context, content_string(image, "url")?)?,
                        })
                    }
                    other => Err(ModelIrError::UnsupportedValue(format!(
                        "chat content {other}"
                    ))),
                }
            })
            .collect(),
        _ => Err(ModelIrError::InvalidField("content")),
    }
}

fn decode_messages(
    context: &DecodeContext,
    body: &Value,
) -> Result<ModelRequestIRV1, ModelIrError> {
    let object = checked_object(
        context,
        body,
        &[
            "model",
            "stream",
            "max_tokens",
            "system",
            "messages",
            "tools",
            "tool_choice",
            "thinking",
            "output_config",
            "metadata",
            "context_management",
        ],
        "messages request",
    )?;
    validate_messages_metadata(context, object.get("metadata"))?;
    validate_messages_context_management(context, object.get("context_management"))?;
    for (key, fields) in [
        ("thinking", &["type", "budget_tokens", "display"][..]),
        ("output_config", &["effort", "format"][..]),
    ] {
        if let Some(value) = object.get(key) {
            checked_object(context, value, fields, "messages reasoning control")?;
        }
    }
    let omit_thinking =
        body.pointer("/thinking/display").and_then(Value::as_str) == Some("omitted");
    if body.pointer("/thinking/display").is_some() && !omit_thinking {
        // Only omitted has a defined cross-protocol return-content mapping.
        context.native_only.set(true);
    }
    if body.pointer("/output_config/format").is_some()
        && super::structured_output::messages_schema_format(body).is_none()
    {
        context.native_only.set(true);
    }
    let mut instructions = object
        .get("system")
        .map(|value| decode_instruction(context, value, InstructionRole::System))
        .transpose()?
        .into_iter()
        .collect::<Vec<_>>();
    let mut messages = Vec::new();
    for value in required_array(object, "messages")? {
        let message = decode_messages_message(context, value)?;
        match message.role {
            // Claude Code 2.1.231 emits an instruction reminder as a `system` message even though
            // the public Messages wire represents instructions at the top level. Normalize this
            // client extension back into the canonical instruction layer so both a Messages
            // target and a cross-protocol target preserve its authority without inventing an
            // unsupported mid-conversation Messages role.
            MessageRole::System | MessageRole::Developer => {
                instructions.push(CanonicalInstruction {
                    role: if message.role == MessageRole::System {
                        InstructionRole::System
                    } else {
                        InstructionRole::Developer
                    },
                    content: message.content,
                });
            }
            MessageRole::User | MessageRole::Assistant => messages.push(message),
        }
    }
    let mut requested_reasoning = match (object.get("thinking"), object.get("output_config")) {
        (None, None) => RequestedReasoningControl::absent(),
        (thinking, output_config) => RequestedReasoningControl::overridden(serde_json::json!({
            "thinking": thinking.cloned(),
            "output_config": output_config.cloned(),
        })),
    };
    requested_reasoning.messages_omit_thinking = omit_thinking;
    Ok(ModelRequestIRV1 {
        native_body: None,
        native_only: false,
        schema_version: MODEL_REQUEST_IR_SCHEMA.into(),
        ingress_protocol: IngressProtocol::Messages,
        responses_options: None,
        responses_item_ids: Default::default(),
        responses_item_statuses: Default::default(),
        served_model_id: required_string(object, "model")?,
        stream: optional_bool(object, "stream")?.unwrap_or(false),
        instructions,
        messages,
        tools: decode_messages_tools(context, object.get("tools"))?,
        tool_namespaces: Vec::new(),
        responses_tool_order: Vec::new(),
        web_search: None,
        responses_search_history: Default::default(),
        responses_annotations: Default::default(),
        responses_message_phases: Default::default(),
        responses_internal_chat_message_metadata: Default::default(),
        responses_reasoning_history: Default::default(),
        tool_choice: decode_tool_choice(
            context,
            IngressProtocol::Messages,
            object.get("tool_choice"),
        )?,
        parallel_tool_calls: decode_messages_parallel(object.get("tool_choice"))?,
        requested_reasoning,
        requested_max_output_tokens: optional_u64(object, "max_tokens")?,
        provider_state: Vec::new(),
    })
}

fn decode_messages_message(
    context: &DecodeContext,
    value: &Value,
) -> Result<CanonicalMessage, ModelIrError> {
    let object = checked_object(context, value, &["role", "content"], "messages message")?;
    let role = decode_role(required_string(object, "role")?.as_str())?;
    let values = object
        .get("content")
        .ok_or(ModelIrError::InvalidField("content"))?;
    let content = match values {
        Value::String(text) => vec![ContentPart::Text { text: text.clone() }],
        Value::Array(values) => values
            .iter()
            .map(|value| decode_messages_content(context, value))
            .collect::<Result<Vec<_>, _>>()?,
        _ => return Err(ModelIrError::InvalidField("content")),
    };
    Ok(CanonicalMessage {
        role,
        content,
        name: None,
    })
}

fn decode_messages_content(
    context: &DecodeContext,
    value: &Value,
) -> Result<ContentPart, ModelIrError> {
    let object = value
        .as_object()
        .ok_or(ModelIrError::InvalidField("messages content"))?;
    match required_string(object, "type")?.as_str() {
        "text" => {
            ensure_keys(
                context,
                object,
                &["type", "text", "cache_control"],
                "messages text",
            )?;
            validate_messages_cache_control(context, object.get("cache_control"))?;
            Ok(ContentPart::Text {
                text: content_string(object, "text")?,
            })
        }
        "image" => {
            ensure_keys(context, object, &["type", "source"], "messages image")?;
            let source = checked_object(
                context,
                object
                    .get("source")
                    .ok_or(ModelIrError::InvalidField("source"))?,
                &["type", "media_type", "data", "url"],
                "messages image source",
            )?;
            let source = match required_string(source, "type")?.as_str() {
                "base64" => ImageSource::Base64 {
                    media_type: required_string(source, "media_type")?,
                    data: required_string(source, "data")?,
                },
                "url" => ImageSource::Url {
                    url: decode_http_image_url(context, content_string(source, "url")?)?,
                },
                other => {
                    return Err(ModelIrError::UnsupportedValue(format!(
                        "messages image source {other}"
                    )));
                }
            };
            Ok(ContentPart::Image { source })
        }
        "tool_use" => {
            ensure_keys(
                context,
                object,
                &["type", "id", "name", "input", "cache_control", "caller"],
                "messages tool_use",
            )?;
            if let Some(caller) = object.get("caller") {
                let caller =
                    checked_object(context, caller, &["type"], "messages tool_use caller")?;
                // Direct calls retain ordinary tool semantics. Programmatic callers carry
                // execution authority that cannot be silently dropped during projection.
                if required_string(caller, "type")? != "direct" {
                    return Err(ModelIrError::UnsupportedValue(
                        "messages tool_use caller type".into(),
                    ));
                }
            }
            validate_messages_cache_control(context, object.get("cache_control"))?;
            Ok(ContentPart::ToolCall {
                logical_id: required_string(object, "id")?,
                tool_kind: ToolKindV1::Function,
                namespace: None,
                name: required_string(object, "name")?,
                arguments: object
                    .get("input")
                    .cloned()
                    .ok_or(ModelIrError::InvalidField("input"))?,
                raw_arguments: None,
            })
        }
        "tool_result" => {
            ensure_keys(
                context,
                object,
                &[
                    "type",
                    "tool_use_id",
                    "content",
                    "is_error",
                    "cache_control",
                ],
                "messages tool_result",
            )?;
            validate_messages_cache_control(context, object.get("cache_control"))?;
            Ok(ContentPart::ToolResult {
                logical_id: required_string(object, "tool_use_id")?,
                tool_kind: ToolKindV1::Function,
                output: decode_tool_output(
                    object
                        .get("content")
                        .ok_or(ModelIrError::InvalidField("content"))?,
                ),
                status: if optional_bool(object, "is_error")?.unwrap_or(false) {
                    ToolResultStatusV1::Failed
                } else {
                    ToolResultStatusV1::Completed
                },
            })
        }
        "thinking" | "redacted_thinking" => {
            let kind = required_string(object, "type")?;
            if kind == "thinking" {
                ensure_keys(
                    context,
                    object,
                    &["type", "thinking", "signature"],
                    "messages thinking",
                )?;
                // Provider state is payload, not a HiRoute credential. Plaintext
                // thinking can legitimately arrive without a signature.
                optional_string(object, "signature")?;
                object
                    .get("thinking")
                    .and_then(Value::as_str)
                    .ok_or(ModelIrError::InvalidField("thinking"))?;
            } else {
                ensure_keys(
                    context,
                    object,
                    &["type", "data"],
                    "messages redacted thinking",
                )?;
                content_string(object, "data")?;
            }
            Ok(ContentPart::ProviderState {
                state: Box::new(OpaqueProviderState {
                    block_index: None,
                    kind,
                    value: value.clone(),
                }),
            })
        }
        other => Err(ModelIrError::UnsupportedValue(format!(
            "messages content {other}"
        ))),
    }
}

fn decode_instruction(
    context: &DecodeContext,
    value: &Value,
    role: InstructionRole,
) -> Result<CanonicalInstruction, ModelIrError> {
    let content = match value {
        Value::String(text) => vec![ContentPart::Text { text: text.clone() }],
        Value::Array(values) => values
            .iter()
            .map(|value| {
                let object = checked_object(
                    context,
                    value,
                    &["type", "text", "cache_control"],
                    "instruction content",
                )?;
                if required_string(object, "type")? != "text" {
                    return Err(ModelIrError::UnsupportedValue(
                        "non-text instruction".into(),
                    ));
                }
                validate_messages_cache_control(context, object.get("cache_control"))?;
                Ok(ContentPart::Text {
                    text: content_string(object, "text")?,
                })
            })
            .collect::<Result<Vec<_>, _>>()?,
        _ => return Err(ModelIrError::InvalidField("instructions")),
    };
    Ok(CanonicalInstruction { role, content })
}

// Claude Code sends these standard Messages transport hints even for a minimal prompt. They do
// not alter the logical task and are intentionally consumed at ingress: HiRoute owns cache
// affinity across candidates, while forwarding an Anthropic user identifier to another provider
// would be incorrect. Their shapes remain fail-closed so an unknown semantic field cannot be
// silently erased.
fn validate_messages_metadata(
    context: &DecodeContext,
    value: Option<&Value>,
) -> Result<(), ModelIrError> {
    let portable = (|| -> Result<(), ModelIrError> {
        let Some(value) = value else {
            return Ok(());
        };
        let object = checked_object(context, value, &["user_id"], "messages metadata")?;
        if let Some(user_id) = object.get("user_id")
            && !user_id.is_string()
        {
            return Err(ModelIrError::InvalidField("metadata.user_id"));
        }
        Ok(())
    })();
    if portable.is_err() {
        context.native_only.set(true);
    }
    Ok(())
}

fn validate_messages_context_management(
    context: &DecodeContext,
    value: Option<&Value>,
) -> Result<(), ModelIrError> {
    let portable = (|| -> Result<(), ModelIrError> {
        let Some(value) = value else {
            return Ok(());
        };
        let object = checked_object(context, value, &["edits"], "messages context_management")?;
        let edits = object
            .get("edits")
            .and_then(Value::as_array)
            .ok_or(ModelIrError::InvalidField("context_management.edits"))?;
        if edits.len() != 1 {
            return Err(ModelIrError::UnsupportedValue(
                "messages context_management edits".into(),
            ));
        }
        let edit = checked_object(
            context,
            &edits[0],
            &["type", "keep"],
            "messages context_management edit",
        )?;
        if required_string(edit, "type")? != "clear_thinking_20251015"
            || required_string(edit, "keep")? != "all"
        {
            return Err(ModelIrError::UnsupportedValue(
                "messages context_management edit".into(),
            ));
        }

        // Claude Code emits this exact hint even when it retains every thinking block. Because
        // `keep: all` performs no edit, it is safe to consume before a cross-protocol projection.
        // Every behavior-changing context-management shape remains fail-closed above.
        Ok(())
    })();
    if portable.is_err() {
        context.native_only.set(true);
    }
    Ok(())
}

fn validate_messages_cache_control(
    context: &DecodeContext,
    value: Option<&Value>,
) -> Result<(), ModelIrError> {
    let portable = (|| -> Result<(), ModelIrError> {
        let Some(value) = value else {
            return Ok(());
        };
        let object = checked_object(context, value, &["type", "ttl"], "messages cache_control")?;
        if required_string(object, "type")? != "ephemeral" {
            return Err(ModelIrError::UnsupportedValue(
                "messages cache_control type".into(),
            ));
        }
        if let Some(ttl) = object.get("ttl").map(Value::as_str) {
            match ttl {
                Some("5m" | "1h") => {}
                _ => return Err(ModelIrError::InvalidField("cache_control.ttl")),
            }
        }
        Ok(())
    })();
    if portable.is_err() {
        context.native_only.set(true);
    }
    Ok(())
}

fn decode_chat_tools(
    context: &DecodeContext,
    value: Option<&Value>,
) -> Result<Vec<CanonicalTool>, ModelIrError> {
    decode_tools(value, |object| {
        ensure_keys(context, object, &["type", "function"], "chat Tool")?;
        if required_string(object, "type")? != "function" {
            return Err(ModelIrError::UnsupportedValue("chat hosted Tool".into()));
        }
        let function = checked_object(
            context,
            object
                .get("function")
                .ok_or(ModelIrError::InvalidField("function"))?,
            &["name", "description", "parameters", "strict"],
            "chat function Tool",
        )?;
        Ok(CanonicalTool {
            kind: ToolKindV1::Function,
            name: required_string(function, "name")?,
            description: optional_string(function, "description")?,
            input_schema: Some(
                function
                    .get("parameters")
                    .cloned()
                    .ok_or(ModelIrError::InvalidField("parameters"))?,
            ),
            strict: optional_bool(function, "strict")?,
            format: None,
        })
    })
}

fn decode_messages_tools(
    context: &DecodeContext,
    value: Option<&Value>,
) -> Result<Vec<CanonicalTool>, ModelIrError> {
    decode_tools(value, |object| {
        ensure_keys(
            context,
            object,
            &[
                "name",
                "description",
                "input_schema",
                "strict",
                "eager_input_streaming",
                "cache_control",
            ],
            "messages Tool",
        )?;
        // Known delivery/cache hints do not grant another tool or execution target.
        // Native projection preserves them; conversion keeps the canonical function.
        optional_bool(object, "eager_input_streaming")?;
        validate_messages_cache_control(context, object.get("cache_control"))?;
        Ok(CanonicalTool {
            kind: ToolKindV1::Function,
            name: required_string(object, "name")?,
            description: optional_string(object, "description")?,
            input_schema: Some(
                object
                    .get("input_schema")
                    .cloned()
                    .ok_or(ModelIrError::InvalidField("input_schema"))?,
            ),
            strict: optional_bool(object, "strict")?,
            format: None,
        })
    })
}

fn decode_tools(
    value: Option<&Value>,
    decode: impl Fn(&Map<String, Value>) -> Result<CanonicalTool, ModelIrError>,
) -> Result<Vec<CanonicalTool>, ModelIrError> {
    value
        .map(|value| {
            value
                .as_array()
                .ok_or(ModelIrError::InvalidField("tools"))?
                .iter()
                .map(|value| {
                    decode(
                        value
                            .as_object()
                            .ok_or(ModelIrError::InvalidField("tools[]"))?,
                    )
                })
                .collect()
        })
        .transpose()
        .map(Option::unwrap_or_default)
}

fn decode_tool_choice(
    context: &DecodeContext,
    protocol: IngressProtocol,
    value: Option<&Value>,
) -> Result<ToolChoice, ModelIrError> {
    let Some(value) = value else {
        return Ok(ToolChoice::Auto);
    };
    if let Some(value) = value.as_str() {
        return match value {
            "none" => Ok(ToolChoice::None),
            "auto" => Ok(ToolChoice::Auto),
            "required" | "any" => Ok(ToolChoice::RequiredAny),
            other => Err(ModelIrError::UnsupportedValue(format!(
                "Tool choice {other}"
            ))),
        };
    }
    let object = value
        .as_object()
        .ok_or(ModelIrError::InvalidField("tool_choice"))?;
    match protocol {
        IngressProtocol::Messages => {
            ensure_keys(
                context,
                object,
                &["type", "name", "disable_parallel_tool_use"],
                "messages tool_choice",
            )?;
            match required_string(object, "type")?.as_str() {
                "auto" => Ok(ToolChoice::Auto),
                "any" => Ok(ToolChoice::RequiredAny),
                "none" => Ok(ToolChoice::None),
                "tool" => Ok(ToolChoice::RequiredNamed {
                    tool_kind: ToolKindV1::Function,
                    name: required_string(object, "name")?,
                }),
                other => Err(ModelIrError::UnsupportedValue(format!(
                    "messages Tool choice {other}"
                ))),
            }
        }
        IngressProtocol::Responses => {
            ensure_keys(context, object, &["type", "name"], "responses tool_choice")?;
            let kind = match required_string(object, "type")?.as_str() {
                "function" => ToolKindV1::Function,
                "custom" => ToolKindV1::Custom,
                other => {
                    return Err(ModelIrError::UnsupportedValue(format!(
                        "responses Tool choice {other}"
                    )));
                }
            };
            Ok(ToolChoice::RequiredNamed {
                tool_kind: kind,
                name: required_string(object, "name")?,
            })
        }
        IngressProtocol::ChatCompletions => {
            ensure_keys(context, object, &["type", "function"], "chat tool_choice")?;
            if required_string(object, "type")? != "function" {
                return Err(ModelIrError::UnsupportedValue(
                    "chat non-function Tool choice".into(),
                ));
            }
            let function = checked_object(
                context,
                object
                    .get("function")
                    .ok_or(ModelIrError::InvalidField("function"))?,
                &["name"],
                "chat tool_choice function",
            )?;
            Ok(ToolChoice::RequiredNamed {
                tool_kind: ToolKindV1::Function,
                name: required_string(function, "name")?,
            })
        }
    }
}

fn decode_responses_state(
    object: &Map<String, Value>,
) -> Result<Vec<OpaqueProviderState>, ModelIrError> {
    if let Some(value) = object.get("previous_response_id") {
        match value {
            Value::Null => {}
            Value::String(_) | Value::Object(_) => {
                return Err(ModelIrError::ResponsesPreviousResponseIdUnsupported);
            }
            Value::Bool(_) | Value::Number(_) | Value::Array(_) => {
                return Err(ModelIrError::InvalidField("previous_response_id"));
            }
        }
    }
    if let Some(value) = object.get("conversation") {
        if !value.is_string() && !value.is_object() {
            return Err(ModelIrError::InvalidField("conversation"));
        }
        // Reasoning ciphertext provenance does not prove the owner of this
        // separate server-side handle. There is no independent handle binding.
        return Err(ModelIrError::ResponsesConversationUnsupported);
    }
    Ok(Vec::new())
}

fn decode_image_url(context: &DecodeContext, value: String) -> Result<ImageSource, ModelIrError> {
    if let Some(rest) = value.strip_prefix("data:")
        && let Some((media_type, data)) = rest.split_once(";base64,")
    {
        return Ok(ImageSource::Base64 {
            media_type: media_type.into(),
            data: data.into(),
        });
    }
    Ok(ImageSource::Url {
        url: decode_http_image_url(context, value)?,
    })
}

fn decode_http_image_url(context: &DecodeContext, value: String) -> Result<String, ModelIrError> {
    if ContentRef::from_wire_marker(&value).is_some()
        || value.starts_with("https://")
        || value.starts_with("http://")
    {
        return Ok(value);
    }
    // The provider fetches/interprets this reference, not the gateway. Unknown
    // encodings are native-only, so conversion cannot reinterpret their meaning.
    context.native_only.set(true);
    Ok(value)
}

fn decode_tool_output(value: &Value) -> ToolOutput {
    match value {
        Value::String(value) => ToolOutput::Text(value.clone()),
        value => ToolOutput::Json(value.clone()),
    }
}

fn parse_tool_arguments(value: String) -> (Value, Option<String>) {
    if let Some(reference) = ContentRef::from_wire_marker(&value) {
        return (reference.json_marker(), None);
    }
    match serde_json::from_str(&value) {
        Ok(parsed) => (parsed, None),
        Err(_) => (Value::Null, Some(value)),
    }
}

fn decode_role(value: &str) -> Result<MessageRole, ModelIrError> {
    match value {
        "user" => Ok(MessageRole::User),
        "assistant" => Ok(MessageRole::Assistant),
        "system" => Ok(MessageRole::System),
        "developer" => Ok(MessageRole::Developer),
        other => Err(ModelIrError::UnsupportedValue(format!("role {other}"))),
    }
}

fn checked_object<'a>(
    context: &DecodeContext,
    value: &'a Value,
    keys: &[&str],
    label: &str,
) -> Result<&'a Map<String, Value>, ModelIrError> {
    let object = value.as_object().ok_or(ModelIrError::ExpectedObject)?;
    ensure_keys(context, object, keys, label)?;
    Ok(object)
}

fn ensure_keys(
    context: &DecodeContext,
    object: &Map<String, Value>,
    keys: &[&str],
    label: &str,
) -> Result<(), ModelIrError> {
    let allowed = keys.iter().copied().collect::<BTreeSet<_>>();
    for key in object.keys().filter(|key| !allowed.contains(key.as_str())) {
        // Execution-bearing tool declarations and gateway authority are not
        // opaque payload extensions. They must remain visible to authorization.
        if label.to_ascii_lowercase().contains("tool")
            || matches!(
                key.as_str(),
                "authorization"
                    | "api_key"
                    | "base_url"
                    | "endpoint"
                    | "headers"
                    | "credentials"
                    | "mcp_servers"
                    | "container"
                    | "computer"
                    | "web_search_options"
                    | "conversation"
                    | "previous_response_id"
            )
        {
            return Err(ModelIrError::UnsupportedField(format!("{label}.{key}")));
        }
        context.native_only.set(true);
    }
    Ok(())
}

fn ensure_absent(
    object: &Map<String, Value>,
    fields: &[&str],
    label: &str,
) -> Result<(), ModelIrError> {
    if let Some(field) = fields.iter().find(|field| object.contains_key(**field)) {
        return Err(ModelIrError::UnsupportedField(format!("{label}.{field}")));
    }
    Ok(())
}

fn compatible_max_output(object: &Map<String, Value>) -> Result<Option<u64>, ModelIrError> {
    let completion = optional_u64(object, "max_completion_tokens")?;
    let legacy = optional_u64(object, "max_tokens")?;
    if completion.is_some() && legacy.is_some() && completion != legacy {
        return Err(ModelIrError::UnsupportedValue(
            "conflicting Chat max output fields".into(),
        ));
    }
    Ok(completion.or(legacy))
}

fn decode_messages_parallel(value: Option<&Value>) -> Result<bool, ModelIrError> {
    let Some(object) = value.and_then(Value::as_object) else {
        return Ok(false);
    };
    Ok(!optional_bool(object, "disable_parallel_tool_use")?.unwrap_or(true))
}

fn content_string(
    object: &Map<String, Value>,
    field: &'static str,
) -> Result<String, ModelIrError> {
    object
        .get(field)
        .and_then(Value::as_str)
        .map(str::to_owned)
        .ok_or(ModelIrError::InvalidField(field))
}

fn required_string(
    object: &Map<String, Value>,
    field: &'static str,
) -> Result<String, ModelIrError> {
    object
        .get(field)
        .and_then(Value::as_str)
        .filter(|value| !value.is_empty())
        .map(str::to_owned)
        .ok_or(ModelIrError::InvalidField(field))
}

fn optional_string(
    object: &Map<String, Value>,
    field: &'static str,
) -> Result<Option<String>, ModelIrError> {
    object
        .get(field)
        .map(|value| {
            value
                .as_str()
                .map(str::to_owned)
                .ok_or(ModelIrError::InvalidField(field))
        })
        .transpose()
}

fn optional_bool(
    object: &Map<String, Value>,
    field: &'static str,
) -> Result<Option<bool>, ModelIrError> {
    object
        .get(field)
        .map(|value| value.as_bool().ok_or(ModelIrError::InvalidField(field)))
        .transpose()
}

fn optional_u64(
    object: &Map<String, Value>,
    field: &'static str,
) -> Result<Option<u64>, ModelIrError> {
    object
        .get(field)
        .map(|value| value.as_u64().ok_or(ModelIrError::InvalidField(field)))
        .transpose()
}

fn required_array<'a>(
    object: &'a Map<String, Value>,
    field: &'static str,
) -> Result<&'a Vec<Value>, ModelIrError> {
    object
        .get(field)
        .and_then(Value::as_array)
        .ok_or(ModelIrError::InvalidField(field))
}