Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

45.8 KB
Raw Blame
pub mod batches;
pub mod completion;
pub mod responses;

#[cfg(test)]
mod chat_completion_transport_tests;

use anyhow::{Context as _, Result};
use futures::{AsyncBufReadExt, AsyncReadExt, StreamExt, io::BufReader, stream::BoxStream};
use http_client::{
    AsyncBody, CustomHeaders, HttpClient, Method, Request as HttpRequest, RequestBuilderExt,
    StatusCode,
    http::{HeaderMap, HeaderValue},
};
use language_model_core::LanguageModelCompletionError;
pub use language_model_core::ReasoningEffort;
pub use language_model_core::chat_completion::{
    ChoiceDelta, FunctionChunk, PromptTokensDetails, ResponseMessageDelta, ResponseStreamError,
    ResponseStreamEvent, ResponseStreamResult, ToolCallChunk, Usage,
};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use serde_json::value::RawValue;
use std::{convert::TryFrom, future::Future, io};
use strum::EnumIter;
use thiserror::Error;

pub const OPEN_AI_API_URL: &str = "https://api.openai.com/v1";

#[derive(Clone, Copy, Serialize, Deserialize, Debug, Eq, PartialEq)]
#[serde(rename_all = "lowercase")]
pub enum Role {
    User,
    Assistant,
    System,
    Tool,
}

impl TryFrom<String> for Role {
    type Error = anyhow::Error;

    fn try_from(value: String) -> Result<Self> {
        match value.as_str() {
            "user" => Ok(Self::User),
            "assistant" => Ok(Self::Assistant),
            "system" => Ok(Self::System),
            "tool" => Ok(Self::Tool),
            _ => anyhow::bail!("invalid role '{value}'"),
        }
    }
}

impl From<Role> for String {
    fn from(val: Role) -> Self {
        match val {
            Role::User => "user".to_owned(),
            Role::Assistant => "assistant".to_owned(),
            Role::System => "system".to_owned(),
            Role::Tool => "tool".to_owned(),
        }
    }
}

#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq, EnumIter)]
pub enum Model {
    #[serde(rename = "gpt-4")]
    Four,
    #[serde(rename = "gpt-4o-mini")]
    FourOmniMini,
    #[serde(rename = "o3")]
    O3,
    #[serde(rename = "gpt-5")]
    Five,
    #[serde(rename = "gpt-5-mini")]
    FiveMini,
    #[serde(rename = "gpt-5-nano")]
    FiveNano,
    #[serde(rename = "gpt-5.1")]
    FivePointOne,
    #[serde(rename = "gpt-5.2")]
    FivePointTwo,
    #[serde(rename = "gpt-5.3-codex")]
    FivePointThreeCodex,
    #[serde(rename = "gpt-5.4-nano")]
    FivePointFourNano,
    #[serde(rename = "gpt-5.4-mini")]
    FivePointFourMini,
    #[serde(rename = "gpt-5.4")]
    FivePointFour,
    #[serde(rename = "gpt-5.4-pro")]
    FivePointFourPro,
    #[serde(rename = "gpt-5.5")]
    FivePointFive,
    #[serde(rename = "gpt-5.5-pro")]
    FivePointFivePro,
    #[serde(rename = "gpt-5.6-sol")]
    #[default]
    FivePointSixSol,
    #[serde(rename = "gpt-5.6-terra")]
    FivePointSixTerra,
    #[serde(rename = "gpt-5.6-luna")]
    FivePointSixLuna,
    #[serde(rename = "gpt-6-astra")]
    SixAstra,
    #[serde(rename = "gpt-6-sol")]
    SixSol,
    #[serde(rename = "gpt-6-luna")]
    SixLuna,
    #[serde(rename = "gpt-6.1-sol")]
    SixPointOneSol,
    #[serde(rename = "custom")]
    Custom {
        name: String,
        /// The name displayed in the UI, such as in the agent panel model dropdown menu.
        display_name: Option<String>,
        max_tokens: u64,
        max_output_tokens: Option<u64>,
        max_completion_tokens: Option<u64>,
        reasoning_effort: Option<ReasoningEffort>,
        #[serde(default = "default_supports_chat_completions")]
        supports_chat_completions: bool,
        #[serde(default = "default_supports_images")]
        supports_images: bool,
    },
}

const fn default_supports_chat_completions() -> bool {
    true
}

const fn default_supports_images() -> bool {
    true
}

impl Model {
    pub fn default_fast() -> Self {
        Self::FivePointSixLuna
    }

    pub fn from_id(id: &str) -> Result<Self> {
        match id {
            "gpt-4" => Ok(Self::Four),
            "gpt-4o-mini" => Ok(Self::FourOmniMini),
            "o3" => Ok(Self::O3),
            "gpt-5" => Ok(Self::Five),
            "gpt-5-mini" => Ok(Self::FiveMini),
            "gpt-5-nano" => Ok(Self::FiveNano),
            "gpt-5.1" => Ok(Self::FivePointOne),
            "gpt-5.2" => Ok(Self::FivePointTwo),
            "gpt-5.3-codex" => Ok(Self::FivePointThreeCodex),
            "gpt-5.4-nano" => Ok(Self::FivePointFourNano),
            "gpt-5.4-mini" => Ok(Self::FivePointFourMini),
            "gpt-5.4" => Ok(Self::FivePointFour),
            "gpt-5.4-pro" => Ok(Self::FivePointFourPro),
            "gpt-5.5" => Ok(Self::FivePointFive),
            "gpt-5.5-pro" => Ok(Self::FivePointFivePro),
            "gpt-5.6-sol" => Ok(Self::FivePointSixSol),
            "gpt-5.6-terra" => Ok(Self::FivePointSixTerra),
            "gpt-5.6-luna" => Ok(Self::FivePointSixLuna),
            "gpt-6-astra" => Ok(Self::SixAstra),
            "gpt-6-sol" => Ok(Self::SixSol),
            "gpt-6-luna" => Ok(Self::SixLuna),
            "gpt-6.1-sol" => Ok(Self::SixPointOneSol),
            invalid_id => anyhow::bail!("invalid model id '{invalid_id}'"),
        }
    }

    pub fn id(&self) -> &str {
        match self {
            Self::Four => "gpt-4",
            Self::FourOmniMini => "gpt-4o-mini",
            Self::O3 => "o3",
            Self::Five => "gpt-5",
            Self::FiveMini => "gpt-5-mini",
            Self::FiveNano => "gpt-5-nano",
            Self::FivePointOne => "gpt-5.1",
            Self::FivePointTwo => "gpt-5.2",
            Self::FivePointThreeCodex => "gpt-5.3-codex",
            Self::FivePointFourNano => "gpt-5.4-nano",
            Self::FivePointFourMini => "gpt-5.4-mini",
            Self::FivePointFour => "gpt-5.4",
            Self::FivePointFourPro => "gpt-5.4-pro",
            Self::FivePointFive => "gpt-5.5",
            Self::FivePointFivePro => "gpt-5.5-pro",
            Self::FivePointSixSol => "gpt-5.6-sol",
            Self::FivePointSixTerra => "gpt-5.6-terra",
            Self::FivePointSixLuna => "gpt-5.6-luna",
            Self::SixAstra => "gpt-6-astra",
            Self::SixSol => "gpt-6-sol",
            Self::SixLuna => "gpt-6-luna",
            Self::SixPointOneSol => "gpt-6.1-sol",
            Self::Custom { name, .. } => name,
        }
    }

    pub fn display_name(&self) -> &str {
        match self {
            Self::Four => "GPT-4",
            Self::FourOmniMini => "GPT-4o Mini",
            Self::O3 => "o3",
            Self::Five => "GPT-5",
            Self::FiveMini => "GPT-5 Mini",
            Self::FiveNano => "GPT-5 Nano",
            Self::FivePointOne => "GPT-5.1",
            Self::FivePointTwo => "GPT-5.2",
            Self::FivePointThreeCodex => "GPT-5.3 Codex",
            Self::FivePointFourNano => "GPT-5.4 Nano",
            Self::FivePointFourMini => "GPT-5.4 Mini",
            Self::FivePointFour => "GPT-5.4",
            Self::FivePointFourPro => "GPT-5.4 Pro",
            Self::FivePointFive => "GPT-5.5",
            Self::FivePointFivePro => "GPT-5.5 Pro",
            Self::FivePointSixSol => "GPT-5.6 Sol",
            Self::FivePointSixTerra => "GPT-5.6 Terra",
            Self::FivePointSixLuna => "GPT-5.6 Luna",
            Self::SixAstra => "GPT-6 Astra",
            Self::SixSol => "GPT-6 Sol",
            Self::SixLuna => "GPT-6 Luna",
            Self::SixPointOneSol => "GPT-6.1 Sol",
            Self::Custom { display_name, .. } => display_name.as_deref().unwrap_or(&self.id()),
        }
    }

    pub fn max_token_count(&self) -> u64 {
        match self {
            Self::Four => 8_192,
            Self::FourOmniMini => 128_000,
            Self::O3 => 200_000,
            Self::Five => 272_000,
            Self::FiveMini => 400_000,
            Self::FiveNano => 400_000,
            Self::FivePointOne => 400_000,
            Self::FivePointTwo => 400_000,
            Self::FivePointThreeCodex => 400_000,
            Self::FivePointFourNano => 400_000,
            Self::FivePointFourMini => 400_000,
            Self::FivePointFour => 1_050_000,
            Self::FivePointFourPro => 1_050_000,
            Self::FivePointFive => 1_050_000,
            Self::FivePointFivePro => 1_050_000,
            Self::FivePointSixSol => 1_050_000,
            Self::FivePointSixTerra => 1_050_000,
            Self::FivePointSixLuna => 1_050_000,
            Self::SixAstra => 1_050_000,
            Self::SixSol => 1_050_000,
            Self::SixLuna => 1_050_000,
            Self::SixPointOneSol => 1_050_000,
            Self::Custom { max_tokens, .. } => *max_tokens,
        }
    }

    pub fn max_output_tokens(&self) -> Option<u64> {
        match self {
            Self::Custom {
                max_output_tokens, ..
            } => *max_output_tokens,
            Self::Four => Some(8_192),
            Self::FourOmniMini => Some(16_384),
            Self::O3 => Some(100_000),
            Self::Five => Some(128_000),
            Self::FiveMini => Some(128_000),
            Self::FiveNano => Some(128_000),
            Self::FivePointOne => Some(128_000),
            Self::FivePointTwo => Some(128_000),
            Self::FivePointThreeCodex => Some(128_000),
            Self::FivePointFourNano => Some(128_000),
            Self::FivePointFourMini => Some(128_000),
            Self::FivePointFour => Some(128_000),
            Self::FivePointFourPro => Some(128_000),
            Self::FivePointFive => Some(128_000),
            Self::FivePointFivePro => Some(128_000),
            Self::FivePointSixSol => Some(128_000),
            Self::FivePointSixTerra => Some(128_000),
            Self::FivePointSixLuna => Some(128_000),
            Self::SixAstra => Some(128_000),
            Self::SixSol => Some(128_000),
            Self::SixLuna => Some(128_000),
            Self::SixPointOneSol => Some(128_000),
        }
    }

    pub fn reasoning_effort(&self) -> Option<ReasoningEffort> {
        match self {
            Self::Custom {
                reasoning_effort, ..
            } => reasoning_effort.to_owned(),
            Self::FivePointOne
            | Self::FivePointTwo
            | Self::FivePointFour
            | Self::FivePointFourMini
            | Self::FivePointFourNano => Some(ReasoningEffort::None),
            Self::FivePointSixSol => Some(ReasoningEffort::Low),
            Self::O3
            | Self::Five
            | Self::FiveMini
            | Self::FiveNano
            | Self::FivePointThreeCodex
            | Self::FivePointFourPro
            | Self::FivePointFive
            | Self::FivePointFivePro
            | Self::FivePointSixTerra
            | Self::FivePointSixLuna
            | Self::SixAstra
            | Self::SixSol
            | Self::SixLuna
            | Self::SixPointOneSol => Some(ReasoningEffort::Medium),
            _ => None,
        }
    }

    pub fn supported_reasoning_efforts(&self) -> &'static [ReasoningEffort] {
        match self {
            Self::Custom {
                reasoning_effort: Some(effort),
                ..
            } => match effort {
                ReasoningEffort::None => &[ReasoningEffort::None],
                ReasoningEffort::Minimal => &[ReasoningEffort::Minimal],
                ReasoningEffort::Low => &[ReasoningEffort::Low],
                ReasoningEffort::Medium => &[ReasoningEffort::Medium],
                ReasoningEffort::High => &[ReasoningEffort::High],
                ReasoningEffort::XHigh => &[ReasoningEffort::XHigh],
                ReasoningEffort::Max => &[ReasoningEffort::Max],
            },
            Self::O3 => &[
                ReasoningEffort::Low,
                ReasoningEffort::Medium,
                ReasoningEffort::High,
            ],
            Self::FivePointOne => &[
                ReasoningEffort::None,
                ReasoningEffort::Low,
                ReasoningEffort::Medium,
                ReasoningEffort::High,
            ],
            Self::Five | Self::FiveMini | Self::FiveNano => &[
                ReasoningEffort::Minimal,
                ReasoningEffort::Low,
                ReasoningEffort::Medium,
                ReasoningEffort::High,
            ],
            Self::FivePointFourPro | Self::FivePointFivePro => &[
                ReasoningEffort::Medium,
                ReasoningEffort::High,
                ReasoningEffort::XHigh,
            ],
            Self::FivePointThreeCodex => &[
                ReasoningEffort::Low,
                ReasoningEffort::Medium,
                ReasoningEffort::High,
                ReasoningEffort::XHigh,
            ],
            Self::FivePointSixSol
            | Self::FivePointSixTerra
            | Self::FivePointSixLuna
            | Self::SixSol
            | Self::SixLuna => &[
                ReasoningEffort::None,
                ReasoningEffort::Low,
                ReasoningEffort::Medium,
                ReasoningEffort::High,
                ReasoningEffort::XHigh,
                ReasoningEffort::Max,
            ],
            Self::SixAstra | Self::SixPointOneSol => &[
                ReasoningEffort::Low,
                ReasoningEffort::Medium,
                ReasoningEffort::High,
                ReasoningEffort::XHigh,
                ReasoningEffort::Max,
            ],
            Self::FivePointTwo
            | Self::FivePointFour
            | Self::FivePointFive
            | Self::FivePointFourMini
            | Self::FivePointFourNano => &[
                ReasoningEffort::None,
                ReasoningEffort::Low,
                ReasoningEffort::Medium,
                ReasoningEffort::High,
                ReasoningEffort::XHigh,
            ],
            _ => &[],
        }
    }

    pub fn uses_responses_api(&self) -> bool {
        match self {
            Self::Custom {
                supports_chat_completions,
                ..
            } => !*supports_chat_completions,
            _ => true,
        }
    }

    /// Returns whether the given model supports the `parallel_tool_calls` parameter.
    ///
    /// If the model does not support the parameter, do not pass it up, or the API will return an error.
    pub fn supports_parallel_tool_calls(&self) -> bool {
        match self {
            Self::Four
            | Self::FourOmniMini
            | Self::Five
            | Self::FiveMini
            | Self::FivePointOne
            | Self::FivePointTwo
            | Self::FivePointThreeCodex
            | Self::FivePointFour
            | Self::FivePointFourMini
            | Self::FivePointFourNano
            | Self::FivePointFourPro
            | Self::FivePointFive
            | Self::FivePointFivePro
            | Self::FivePointSixSol
            | Self::FivePointSixTerra
            | Self::FivePointSixLuna
            | Self::SixAstra
            | Self::SixSol
            | Self::SixLuna
            | Self::SixPointOneSol
            | Self::FiveNano => true,
            Self::O3 | Model::Custom { .. } => false,
        }
    }

    /// Returns whether the given model supports the `prompt_cache_key` parameter.
    ///
    /// If the model does not support the parameter, do not pass it up.
    pub fn supports_prompt_cache_key(&self) -> bool {
        true
    }

    /// Whether this model supports server-side compaction via the
    /// `context_management` request parameter. OpenAI doesn't publish a
    /// support matrix, but the GPT-5.5 guide notes compaction is a feature
    /// shared with GPT-5.4, and the compaction docs exercise the GPT-5.3
    /// Codex line, so we treat everything from GPT-5.3 onward as supported.
    ///
    /// <https://developers.openai.com/api/docs/guides/compaction>
    pub fn supports_compaction(&self) -> bool {
        match self {
            Self::FivePointThreeCodex
            | Self::FivePointFourNano
            | Self::FivePointFourMini
            | Self::FivePointFour
            | Self::FivePointFourPro
            | Self::FivePointFive
            | Self::FivePointFivePro
            | Self::FivePointSixSol
            | Self::FivePointSixTerra
            | Self::FivePointSixLuna
            | Self::SixAstra
            | Self::SixSol
            | Self::SixLuna
            | Self::SixPointOneSol => true,
            Self::Four
            | Self::FourOmniMini
            | Self::O3
            | Self::Five
            | Self::FiveMini
            | Self::FiveNano
            | Self::FivePointOne
            | Self::FivePointTwo
            | Self::Custom { .. } => false,
        }
    }

    /// Whether OpenAI's Priority processing tier is available for this model.
    /// Sourced from <https://openai.com/api-priority-processing/>. The `*-pro`,
    /// `*-nano`, and legacy `gpt-4` variants are not eligible.
    pub fn supports_priority(&self) -> bool {
        match self {
            Self::FourOmniMini
            | Self::O3
            | Self::Five
            | Self::FiveMini
            | Self::FivePointOne
            | Self::FivePointTwo
            | Self::FivePointThreeCodex
            | Self::FivePointFourMini
            | Self::FivePointFour
            | Self::FivePointFive
            | Self::FivePointSixSol
            | Self::FivePointSixTerra
            | Self::FivePointSixLuna
            | Self::SixAstra
            | Self::SixSol
            | Self::SixLuna
            | Self::SixPointOneSol => true,
            Self::Four
            | Self::FiveNano
            | Self::FivePointFourNano
            | Self::FivePointFourPro
            | Self::FivePointFivePro
            | Self::Custom { .. } => false,
        }
    }
}

#[cfg(test)]
mod tests {
    use language_model_core::{
        LanguageModelCompletionError, LanguageModelProviderName, OPEN_AI_PROVIDER_NAME,
        ProviderErrorCategory,
    };

    use super::{Model, ReasoningEffort, RequestError, StatusCode};

    #[test]
    fn gpt_5_1_uses_none_reasoning_by_default() {
        let expected_efforts = [
            ReasoningEffort::None,
            ReasoningEffort::Low,
            ReasoningEffort::Medium,
            ReasoningEffort::High,
        ];

        assert_eq!(
            Model::FivePointOne.reasoning_effort(),
            Some(ReasoningEffort::None)
        );
        assert_eq!(
            Model::FivePointOne.supported_reasoning_efforts(),
            expected_efforts.as_slice()
        );
    }

    #[test]
    fn newer_frontier_models_support_none_reasoning() {
        let expected_efforts = [
            ReasoningEffort::None,
            ReasoningEffort::Low,
            ReasoningEffort::Medium,
            ReasoningEffort::High,
            ReasoningEffort::XHigh,
        ];

        assert_eq!(
            Model::FivePointTwo.reasoning_effort(),
            Some(ReasoningEffort::None)
        );
        assert_eq!(
            Model::FivePointTwo.supported_reasoning_efforts(),
            expected_efforts.as_slice()
        );
        assert_eq!(
            Model::FivePointFour.reasoning_effort(),
            Some(ReasoningEffort::None)
        );
        assert_eq!(
            Model::FivePointFour.supported_reasoning_efforts(),
            expected_efforts.as_slice()
        );
        assert_eq!(
            Model::FivePointFive.reasoning_effort(),
            Some(ReasoningEffort::Medium)
        );
        assert_eq!(
            Model::FivePointFive.supported_reasoning_efforts(),
            expected_efforts.as_slice()
        );
    }

    #[test]
    fn newer_codex_models_support_low_reasoning_effort() {
        let expected_efforts = [
            ReasoningEffort::Low,
            ReasoningEffort::Medium,
            ReasoningEffort::High,
            ReasoningEffort::XHigh,
        ];

        assert_eq!(
            Model::FivePointThreeCodex.supported_reasoning_efforts(),
            expected_efforts.as_slice()
        );
    }

    #[test]
    fn http_credit_balance_exhaustion_is_payment_required() {
        let error = LanguageModelCompletionError::from(RequestError::HttpResponseError {
            provider: OPEN_AI_PROVIDER_NAME.to_string(),
            status_code: StatusCode::TOO_MANY_REQUESTS,
            body: serde_json::json!({
                "error": {
                    "message": "You have no credits remaining.",
                    "type": "insufficient_quota",
                    "param": null,
                    "code": "credit_balance_exhausted"
                }
            })
            .to_string(),
            headers: Box::default(),
        });

        assert!(matches!(
            error,
            LanguageModelCompletionError::ProviderRejection {
                status: Some(StatusCode::TOO_MANY_REQUESTS),
                code: Some(code),
                message,
                category: ProviderErrorCategory::PaymentRequired,
                ..
            } if code == "credit_balance_exhausted"
                && message == "You have no credits remaining."
        ));
    }

    #[test]
    fn http_usage_limit_type_is_payment_required() {
        let error = LanguageModelCompletionError::from(RequestError::HttpResponseError {
            provider: OPEN_AI_PROVIDER_NAME.to_string(),
            status_code: StatusCode::TOO_MANY_REQUESTS,
            body: serde_json::json!({
                "error": {
                    "type": "usage_limit_reached",
                    "message": "The usage limit has been reached",
                    "plan_type": "plus"
                }
            })
            .to_string(),
            headers: Box::default(),
        });

        assert!(!error.is_transient());
        assert!(matches!(
            error,
            LanguageModelCompletionError::ProviderRejection {
                status: Some(StatusCode::TOO_MANY_REQUESTS),
                code: Some(code),
                message,
                category: ProviderErrorCategory::PaymentRequired,
                ..
            } if code == "usage_limit_reached"
                && message == "The usage limit has been reached"
        ));
    }

    #[test]
    fn http_unknown_error_type_is_preserved_for_compatible_provider() {
        let error = LanguageModelCompletionError::from(RequestError::HttpResponseError {
            provider: "Compatible Provider".to_string(),
            status_code: StatusCode::BAD_REQUEST,
            body: serde_json::json!({
                "error": {
                    "type": "provider_specific_error",
                    "message": "Provider-specific rejection"
                }
            })
            .to_string(),
            headers: Box::default(),
        });

        assert!(matches!(
            error,
            LanguageModelCompletionError::ProviderRejection {
                provider,
                code: Some(code),
                message,
                category: ProviderErrorCategory::InvalidRequest,
                ..
            } if provider == LanguageModelProviderName::from("Compatible Provider".to_string())
                && code == "provider_specific_error"
                && message == "Provider-specific rejection"
        ));
    }

    #[test]
    fn http_invalid_prompt_is_content_policy_rejection() {
        let error = LanguageModelCompletionError::from(RequestError::HttpResponseError {
            provider: OPEN_AI_PROVIDER_NAME.to_string(),
            status_code: StatusCode::BAD_REQUEST,
            body: serde_json::json!({
                "error": {
                    "message": "Invalid prompt: your prompt was flagged as potentially violating our usage policy.",
                    "type": "invalid_request_error",
                    "param": null,
                    "code": "invalid_prompt"
                }
            })
            .to_string(),
            headers: Box::default(),
        });

        assert!(matches!(
            error,
            LanguageModelCompletionError::ProviderRejection {
                status: Some(StatusCode::BAD_REQUEST),
                code: Some(code),
                message,
                category: ProviderErrorCategory::ContentPolicy,
                ..
            } if code == "invalid_prompt"
                && message
                    == "Invalid prompt: your prompt was flagged as potentially violating our usage policy."
        ));
    }

    #[test]
    fn stream_usage_with_null_prompt_cache_tokens_is_not_an_error() {
        let chunk = r#"{"choices":[],"usage":{"prompt_tokens":5,"completion_tokens":3,"total_tokens":8,"prompt_tokens_details":{"cached_tokens":0,"cache_write_tokens":null}}}"#;

        let super::ResponseStreamResult::Ok(event) =
            serde_json::from_str::<super::ResponseStreamResult>(chunk).unwrap()
        else {
            panic!("usage-only chunk with null cache token fields must not fail to parse");
        };

        let details = event
            .usage
            .unwrap()
            .prompt_tokens_details
            .expect("prompt_tokens_details should be present");
        assert_eq!(details.cached_tokens, Some(0));
        assert_eq!(details.cache_write_tokens, None);
    }
}

#[derive(Debug, Serialize, Deserialize)]
pub struct StreamOptions {
    pub include_usage: bool,
}

impl Default for StreamOptions {
    fn default() -> Self {
        Self {
            include_usage: true,
        }
    }
}

#[derive(Debug, Serialize, Deserialize)]
pub struct Request {
    pub model: String,
    pub messages: Vec<RequestMessage>,
    pub stream: bool,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub stream_options: Option<StreamOptions>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub max_completion_tokens: Option<u64>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub max_tokens: Option<u64>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub stop: Vec<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub temperature: Option<f32>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub tool_choice: Option<ToolChoice>,
    /// Whether to enable parallel function calling during tool use.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub parallel_tool_calls: Option<bool>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub tools: Vec<ToolDefinition>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub prompt_cache_key: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub reasoning_effort: Option<ReasoningEffort>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub service_tier: Option<ServiceTier>,
}

/// Selects the service tier for OpenAI requests.
///
/// This maps to the top-level `service_tier` field on Responses and Chat
/// Completions. `Priority` serializes as `priority` for compatibility, while
/// deserialization also accepts the `fast` value echoed by some models.
#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum ServiceTier {
    Auto,
    Default,
    Flex,
    Scale,
    #[serde(alias = "fast")]
    Priority,
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum ToolChoice {
    Auto,
    Required,
    None,
    #[serde(untagged)]
    Other(ToolDefinition),
}

#[derive(Clone, Deserialize, Serialize, Debug)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum ToolDefinition {
    #[allow(dead_code)]
    Function { function: FunctionDefinition },
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct FunctionDefinition {
    pub name: String,
    pub description: Option<String>,
    pub parameters: Option<Value>,
}

#[derive(Clone, Serialize, Deserialize, Debug, Eq, PartialEq)]
#[serde(tag = "role", rename_all = "lowercase")]
pub enum RequestMessage {
    Assistant {
        content: Option<MessageContent>,
        #[serde(default, skip_serializing_if = "Vec::is_empty")]
        tool_calls: Vec<ToolCall>,
        #[serde(default, skip_serializing_if = "Option::is_none")]
        reasoning_content: Option<String>,
        /// Provider-defined reasoning metadata required for replay.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        reasoning_details: Option<std::sync::Arc<Value>>,
    },
    User {
        content: MessageContent,
    },
    System {
        content: MessageContent,
    },
    Tool {
        content: MessageContent,
        tool_call_id: String,
    },
}

#[derive(Serialize, Deserialize, Clone, Debug, Eq, PartialEq)]
#[serde(untagged)]
pub enum MessageContent {
    Plain(String),
    Multipart(Vec<MessagePart>),
}

impl MessageContent {
    pub fn empty() -> Self {
        MessageContent::Multipart(vec![])
    }

    pub fn push_part(&mut self, part: MessagePart) {
        match self {
            MessageContent::Plain(text) => {
                *self =
                    MessageContent::Multipart(vec![MessagePart::Text { text: text.clone() }, part]);
            }
            MessageContent::Multipart(parts) if parts.is_empty() => match part {
                MessagePart::Text { text } => *self = MessageContent::Plain(text),
                MessagePart::Image { .. } => *self = MessageContent::Multipart(vec![part]),
            },
            MessageContent::Multipart(parts) => parts.push(part),
        }
    }
}

impl From<Vec<MessagePart>> for MessageContent {
    fn from(mut parts: Vec<MessagePart>) -> Self {
        if let [MessagePart::Text { text }] = parts.as_mut_slice() {
            MessageContent::Plain(std::mem::take(text))
        } else {
            MessageContent::Multipart(parts)
        }
    }
}

#[derive(Serialize, Deserialize, Clone, Debug, Eq, PartialEq)]
#[serde(tag = "type")]
pub enum MessagePart {
    #[serde(rename = "text")]
    Text { text: String },
    #[serde(rename = "image_url")]
    Image { image_url: ImageUrl },
}

#[derive(Serialize, Deserialize, Clone, Debug, Eq, PartialEq)]
pub struct ImageUrl {
    pub url: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub detail: Option<String>,
}

#[derive(Clone, Serialize, Deserialize, Debug, Eq, PartialEq)]
pub struct ToolCall {
    pub id: String,
    #[serde(flatten)]
    pub content: ToolCallContent,
}

#[derive(Clone, Serialize, Deserialize, Debug, Eq, PartialEq)]
#[serde(tag = "type", rename_all = "lowercase")]
pub enum ToolCallContent {
    Function { function: FunctionContent },
}

#[derive(Clone, Serialize, Deserialize, Debug, Eq, PartialEq)]
pub struct FunctionContent {
    pub name: String,
    pub arguments: String,
    /// Provider-defined metadata required to replay a reasoning tool call.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub thought_signature: Option<String>,
}

#[derive(Clone, Serialize, Deserialize, Debug)]
pub struct Response {
    pub id: String,
    pub object: String,
    pub created: u64,
    pub model: String,
    pub choices: Vec<Choice>,
    pub usage: Usage,
}

#[derive(Clone, Serialize, Deserialize, Debug)]
pub struct Choice {
    pub index: u32,
    pub message: RequestMessage,
    pub finish_reason: Option<String>,
}

/// An error produced while sending an OpenAI-compatible request.
///
/// Transport and wire-format failures retain their category so callers can
/// present an appropriate error or translate it into provider-specific errors.
#[derive(Error, Debug)]
pub enum RequestError {
    #[error("HTTP response error from {provider}'s API: status {status_code} - {body:?}")]
    HttpResponseError {
        provider: String,
        status_code: StatusCode,
        body: String,
        headers: Box<HeaderMap<HeaderValue>>,
    },
    #[error("error serializing request to {provider}'s API")]
    SerializeRequest {
        provider: String,
        #[source]
        error: serde_json::Error,
    },
    #[error("error building request body for {provider}'s API")]
    BuildRequestBody {
        provider: String,
        #[source]
        error: http_client::http::Error,
    },
    #[error("error sending HTTP request to {host} for {provider}")]
    HttpSend {
        provider: String,
        host: String,
        #[source]
        error: anyhow::Error,
    },
    #[error("I/O error reading response from {provider}'s API")]
    ReadResponse {
        provider: String,
        #[source]
        error: io::Error,
    },
    #[error("error deserializing {provider}'s API response")]
    DeserializeResponse {
        provider: String,
        #[source]
        error: serde_json::Error,
    },
    #[error(transparent)]
    Other(#[from] anyhow::Error),
}

#[derive(Deserialize)]
struct ResponseErrorEnvelope {
    error: responses::ResponseError,
}

/// A framed Chat Completions server-sent event.
///
/// `Done` is distinct from the underlying response body ending so callers can
/// tell whether the server completed the stream according to the protocol.
#[derive(Debug)]
pub enum ChatCompletionStreamEvent {
    /// A JSON payload from a `data` field, kept as raw text so consumers can
    /// deserialize it into their own wire types in a single pass.
    Data(Box<RawValue>),
    /// The protocol terminator `data: [DONE]`.
    Done,
}

/// Sends a non-streaming compatible Chat Completions request.
///
/// The request may use any serializable envelope. The response body is returned
/// as JSON so the caller can deserialize provider-specific extensions.
///
/// # Errors
///
/// Returns [`RequestError`] when request serialization or construction fails,
/// the HTTP request fails, the server returns a non-success status, the response
/// body cannot be read, or the response is not valid JSON.
pub async fn non_streaming_chat_completion<RequestBody>(
    client: &dyn HttpClient,
    provider_name: &str,
    api_url: &str,
    api_key: &str,
    extra_headers: &CustomHeaders,
    request: &RequestBody,
) -> Result<Value, RequestError>
where
    RequestBody: Serialize + ?Sized,
{
    let request = chat_completion_request(provider_name, api_url, api_key, extra_headers, request)?;
    let host = request.uri().host().unwrap_or(api_url).to_owned();
    let mut response = client
        .send(request)
        .await
        .map_err(|error| RequestError::HttpSend {
            provider: provider_name.to_string(),
            host,
            error,
        })?;
    if !response.status().is_success() {
        return Err(http_response_error(provider_name, &mut response).await);
    }

    let body =
        read_response_body(&mut response)
            .await
            .map_err(|error| RequestError::ReadResponse {
                provider: provider_name.to_string(),
                error,
            })?;
    serde_json::from_str(&body).map_err(|error| RequestError::DeserializeResponse {
        provider: provider_name.to_string(),
        error,
    })
}

pub async fn non_streaming_completion(
    client: &dyn HttpClient,
    api_url: &str,
    api_key: &str,
    request: Request,
) -> Result<Response, RequestError> {
    let response = non_streaming_chat_completion(
        client,
        "openai",
        api_url,
        api_key,
        &CustomHeaders::default(),
        &request,
    )
    .await?;
    serde_json::from_value(response).map_err(|error| RequestError::Other(error.into()))
}

/// Starts a streaming compatible Chat Completions request.
///
/// The returned stream preserves the distinction between a `[DONE]` event and
/// the response body ending without a protocol terminator.
///
/// # Errors
///
/// Returns [`RequestError`] before streaming begins when request serialization
/// or construction fails, the HTTP request fails, or the server returns a
/// non-success status. The returned stream yields [`RequestError`] when the
/// response body cannot be read or a `data` field is not valid JSON.
pub async fn stream_chat_completion<RequestBody>(
    client: &dyn HttpClient,
    provider_name: &str,
    api_url: &str,
    api_key: &str,
    extra_headers: &CustomHeaders,
    request: &RequestBody,
) -> Result<
    BoxStream<'static, std::result::Result<ChatCompletionStreamEvent, RequestError>>,
    RequestError,
>
where
    RequestBody: Serialize + ?Sized,
{
    let request = chat_completion_request(provider_name, api_url, api_key, extra_headers, request)?;
    let host = request.uri().host().unwrap_or(api_url).to_owned();
    let mut response = client
        .send(request)
        .await
        .map_err(|error| RequestError::HttpSend {
            provider: provider_name.to_string(),
            host,
            error,
        })?;
    if !response.status().is_success() {
        return Err(http_response_error(provider_name, &mut response).await);
    }

    let lines = BufReader::new(response.into_body()).lines();
    let provider_name = provider_name.to_string();
    Ok(futures::stream::try_unfold(
        (lines, false, provider_name),
        async move |(mut lines, is_done, provider_name)| {
            if is_done {
                return Ok(None);
            }

            loop {
                let Some(line) = lines.next().await else {
                    return Ok(None);
                };
                let line = line.map_err(|error| RequestError::ReadResponse {
                    provider: provider_name.clone(),
                    error,
                })?;
                let Some(data) = line
                    .strip_prefix("data: ")
                    .or_else(|| line.strip_prefix("data:"))
                else {
                    continue;
                };
                if data == "[DONE]" {
                    return Ok(Some((
                        ChatCompletionStreamEvent::Done,
                        (lines, true, provider_name),
                    )));
                }
                let value = serde_json::from_str(data).map_err(|error| {
                    RequestError::DeserializeResponse {
                        provider: provider_name.clone(),
                        error,
                    }
                })?;
                return Ok(Some((
                    ChatCompletionStreamEvent::Data(value),
                    (lines, false, provider_name),
                )));
            }
        },
    )
    .boxed())
}

pub async fn stream_completion(
    client: &dyn HttpClient,
    provider_name: &str,
    api_url: &str,
    api_key: &str,
    request: Request,
    extra_headers: &CustomHeaders,
) -> Result<
    BoxStream<'static, Result<ResponseStreamEvent, LanguageModelCompletionError>>,
    RequestError,
> {
    let events = stream_chat_completion(
        client,
        provider_name,
        api_url,
        api_key,
        extra_headers,
        &request,
    )
    .await?;
    let provider_name =
        language_model_core::LanguageModelProviderName::from(provider_name.to_string());
    Ok(events
        .filter_map(move |event| {
            let provider_name = provider_name.clone();
            async move {
                let value = match event {
                    Ok(ChatCompletionStreamEvent::Data(value)) => value,
                    Ok(ChatCompletionStreamEvent::Done) => return None,
                    Err(error) => return Some(Err(error.into())),
                };
                match serde_json::from_str::<ResponseStreamResult>(value.get()) {
                    Ok(ResponseStreamResult::Ok(response)) => Some(Ok(response)),
                    Ok(ResponseStreamResult::Err { error }) => {
                        let category = completion::response_error_category(
                            error.code.as_deref(),
                            error.error_type.as_deref(),
                            None,
                            &error.message,
                        );
                        Some(Err(LanguageModelCompletionError::from_provider_response(
                            provider_name,
                            None,
                            error.code.or(error.error_type),
                            error.message,
                            None,
                            category,
                        )))
                    }
                    Err(error) => {
                        log::error!(
                            "Failed to parse OpenAI response into ResponseStreamResult: `{}`\n\
                            Response: `{}`",
                            error,
                            value,
                        );
                        Some(Err(LanguageModelCompletionError::DeserializeResponse {
                            provider: provider_name,
                            error,
                        }))
                    }
                }
            }
        })
        .boxed())
}

fn chat_completion_request<RequestBody>(
    provider_name: &str,
    api_url: &str,
    api_key: &str,
    extra_headers: &CustomHeaders,
    request: &RequestBody,
) -> std::result::Result<HttpRequest<AsyncBody>, RequestError>
where
    RequestBody: Serialize + ?Sized,
{
    let body = serde_json::to_string(request).map_err(|error| RequestError::SerializeRequest {
        provider: provider_name.to_string(),
        error,
    })?;
    HttpRequest::builder()
        .method(Method::POST)
        .uri(format!("{api_url}/chat/completions"))
        .header("Content-Type", "application/json")
        .header("Authorization", format!("Bearer {}", api_key.trim()))
        .extra_headers(extra_headers)
        .body(AsyncBody::from(body))
        .map_err(|error| RequestError::BuildRequestBody {
            provider: provider_name.to_string(),
            error,
        })
}

async fn http_response_error(
    provider_name: &str,
    response: &mut http_client::Response<AsyncBody>,
) -> RequestError {
    let body = match read_response_body(response).await {
        Ok(body) => body,
        Err(error) => {
            return RequestError::ReadResponse {
                provider: provider_name.to_string(),
                error,
            };
        }
    };
    RequestError::HttpResponseError {
        provider: provider_name.to_string(),
        status_code: response.status(),
        body,
        headers: Box::new(response.headers().clone()),
    }
}

async fn read_response_body(response: &mut http_client::Response<AsyncBody>) -> io::Result<String> {
    let mut body = String::new();
    response.body_mut().read_to_string(&mut body).await?;
    Ok(body)
}

#[derive(Copy, Clone, Serialize, Deserialize)]
pub enum OpenAiEmbeddingModel {
    #[serde(rename = "text-embedding-3-small")]
    TextEmbedding3Small,
    #[serde(rename = "text-embedding-3-large")]
    TextEmbedding3Large,
}

#[derive(Serialize)]
struct OpenAiEmbeddingRequest<'a> {
    model: OpenAiEmbeddingModel,
    input: Vec<&'a str>,
}

#[derive(Deserialize)]
pub struct OpenAiEmbeddingResponse {
    pub data: Vec<OpenAiEmbedding>,
}

#[derive(Deserialize)]
pub struct OpenAiEmbedding {
    pub embedding: Vec<f32>,
}

pub fn embed<'a>(
    client: &dyn HttpClient,
    api_url: &str,
    api_key: &str,
    model: OpenAiEmbeddingModel,
    texts: impl IntoIterator<Item = &'a str>,
) -> impl 'static + Future<Output = Result<OpenAiEmbeddingResponse>> {
    let uri = format!("{api_url}/embeddings");

    let request = OpenAiEmbeddingRequest {
        model,
        input: texts.into_iter().collect(),
    };
    let body = AsyncBody::from(serde_json::to_string(&request).unwrap());
    let request = HttpRequest::builder()
        .method(Method::POST)
        .uri(uri)
        .header("Content-Type", "application/json")
        .header("Authorization", format!("Bearer {}", api_key.trim()))
        .body(body)
        .map(|request| client.send(request));

    async move {
        let mut response = request?.await?;
        let mut body = String::new();
        response.body_mut().read_to_string(&mut body).await?;

        anyhow::ensure!(
            response.status().is_success(),
            "error during embedding, status: {:?}, body: {:?}",
            response.status(),
            body
        );
        let response: OpenAiEmbeddingResponse =
            serde_json::from_str(&body).context("failed to parse OpenAI embedding response")?;
        Ok(response)
    }
}

// -- Conversions to `language_model_core` types --

impl From<RequestError> for language_model_core::LanguageModelCompletionError {
    fn from(error: RequestError) -> Self {
        match error {
            RequestError::HttpResponseError {
                provider,
                status_code,
                body,
                headers,
            } => {
                let retry_after = headers
                    .get(http_client::http::header::RETRY_AFTER)
                    .and_then(|val| val.to_str().ok()?.parse::<u64>().ok())
                    .map(std::time::Duration::from_secs);

                if let Ok(error_response) = serde_json::from_str::<ResponseErrorEnvelope>(&body) {
                    let error = error_response.error;
                    let category = completion::response_error_category(
                        error.code.as_deref(),
                        error.error_type.as_deref(),
                        Some(status_code),
                        &error.message,
                    );
                    Self::from_provider_response(
                        provider.into(),
                        Some(status_code),
                        error.code.or(error.error_type),
                        error.message,
                        retry_after,
                        category,
                    )
                } else {
                    Self::from_http_status(provider.into(), status_code, body, retry_after)
                }
            }
            RequestError::SerializeRequest { provider, error } => Self::SerializeRequest {
                provider: provider.into(),
                error,
            },
            RequestError::BuildRequestBody { provider, error } => Self::BuildRequestBody {
                provider: provider.into(),
                error,
            },
            RequestError::HttpSend {
                provider,
                host,
                error,
            } => Self::HttpSend {
                provider: provider.into(),
                host,
                error,
            },
            RequestError::ReadResponse { provider, error } => Self::ApiReadResponseError {
                provider: provider.into(),
                error,
            },
            RequestError::DeserializeResponse { provider, error } => Self::DeserializeResponse {
                provider: provider.into(),
                error,
            },
            RequestError::Other(e) => Self::Other(e),
        }
    }
}