laratext 0.1.0

Laratext for Laravel Rust: translation keys with their source text, and auto-translation with OpenAI, Claude, Google Translate or Translatorio.
Documentation
use crate::config::ModelOverride;
use crate::support::settings;

/// How the answer of a model can be held to JSON.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Json {
    /// The answer can be held to a JSON schema (OpenAI's Structured Outputs).
    Schema,
    /// The answer is guaranteed to be JSON, but not to follow a schema.
    Object,
    /// Nothing can be asked of the format: the prompt is all there is.
    None,
}

impl Json {
    fn parse(value: &str) -> Option<Self> {
        match value {
            "schema" => Some(Self::Schema),
            "object" => Some(Self::Object),
            "none" => Some(Self::None),
            _ => None,
        }
    }
}

/// What a model can answer and which request parameters it takes.
///
/// Translating with an old model is not translating with a new one: one answers 4,096 tokens at
/// most and another 128,000, one takes `temperature` and another refuses the request if it is
/// sent, one can be held to a JSON schema and another only asked nicely. The translators read this
/// to size their batches and shape their requests.
///
/// The table is matched by the longest prefix of the model's name, so a dated snapshot such as
/// `gpt-4o-2024-08-06` finds `gpt-4o`. A model in no row gets the most cautious limits. A project
/// can correct or add a row in `TextsConfig::models` without waiting for a release.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ModelLimits {
    /// The most tokens one answer may hold.
    pub max_output_tokens: u64,
    /// Whether `temperature` may be sent.
    pub temperature: bool,
    /// How the answer can be held to JSON.
    pub json: Json,
    /// Whether the model thinks before answering, spending output tokens.
    pub reasoning: bool,
    /// The effort to ask for, or `None` to send none.
    pub reasoning_effort: Option<String>,
}

type Row = (&'static str, u64, bool, Json, bool, Option<&'static str>);

const fn row(
    prefix: &'static str,
    tokens: u64,
    temperature: bool,
    json: Json,
    reasoning: bool,
    effort: Option<&'static str>,
) -> Row {
    (prefix, tokens, temperature, json, reasoning, effort)
}

/// Read from OpenAI's and Anthropic's model pages on 2026-10-15, as in the PHP package.
///
/// `temperature` is sent only where it is accepted for certain: GPT-5 refuses it, GPT-5.1 and
/// later take it only at no reasoning, and Claude Opus 4.7 and later refuse anything but the
/// default. OpenAI models that can skip reasoning are asked for `none`, the rest for the least they
/// take; Claude models that think by default are marked as reasoning, so room is left for it.
/// Retired models (Claude 3, Claude 4 and 4.1) are left out: no request to them succeeds.
const OPENAI: &[Row] = &[
    row("gpt-3.5-turbo", 4096, true, Json::Object, false, None),
    row("gpt-4", 8192, true, Json::None, false, None),
    row("gpt-4-turbo", 4096, true, Json::Object, false, None),
    row("gpt-4-1106", 4096, true, Json::Object, false, None),
    row("gpt-4-0125", 4096, true, Json::Object, false, None),
    row("gpt-4o", 16384, true, Json::Schema, false, None),
    row("gpt-4o-2024-05-13", 4096, true, Json::Object, false, None),
    row("gpt-4o-mini", 16384, true, Json::Schema, false, None),
    row("gpt-4.1", 32768, true, Json::Schema, false, None),
    row("o1", 100_000, false, Json::Schema, true, None),
    row("o1-mini", 65536, false, Json::None, true, None),
    row("o3", 100_000, false, Json::Schema, true, None),
    row("o4-mini", 100_000, false, Json::Schema, true, None),
    row("gpt-5", 128_000, false, Json::Schema, true, Some("minimal")),
    row("gpt-5.", 128_000, false, Json::Schema, true, None),
    row("gpt-5.1", 128_000, false, Json::Schema, false, Some("none")),
    row("gpt-5.2", 128_000, false, Json::Schema, false, Some("none")),
    row("gpt-5.4", 128_000, false, Json::Schema, false, Some("none")),
    row("gpt-5.5", 128_000, false, Json::Schema, false, Some("none")),
    row("gpt-5.6", 128_000, false, Json::Schema, false, Some("none")),
    row("gpt-6", 128_000, false, Json::Schema, true, Some("low")),
    row("gpt-6-sol", 128_000, false, Json::Schema, false, Some("none")),
    row("gpt-6-luna", 128_000, false, Json::Schema, false, Some("none")),
];

const CLAUDE: &[Row] = &[
    row("claude-sonnet-4-5", 64_000, true, Json::None, false, None),
    row("claude-haiku-4-5", 64_000, true, Json::None, false, None),
    row("claude-opus-4-5", 64_000, true, Json::None, false, None),
    row("claude-opus-4-6", 128_000, true, Json::None, false, None),
    row("claude-sonnet-4-6", 128_000, true, Json::None, false, None),
    row("claude-opus-4-7", 128_000, false, Json::None, false, None),
    row("claude-opus-4-8", 128_000, false, Json::None, false, None),
    row("claude-opus-5", 128_000, false, Json::None, false, None),
    row("claude-sonnet-5", 128_000, false, Json::None, false, None),
    row("claude-fable-5", 128_000, false, Json::None, false, None),
    row("claude-mythos-5", 128_000, false, Json::None, false, None),
    row("claude-opus-5-5", 128_000, false, Json::None, true, None),
    row("claude-sonnet-5-5", 128_000, false, Json::None, true, None),
    row("claude-haiku-5-5", 128_000, false, Json::None, true, None),
    row("claude-fable-5-1", 128_000, false, Json::None, true, None),
    row("claude-mythos-5-1", 128_000, false, Json::None, true, None),
];

/// What a model in no row gets: a short answer and nothing it might refuse, which is how every
/// model was asked before the table existed, minus `temperature`.
const UNKNOWN: Row = row("", 4096, false, Json::None, false, None);

impl ModelLimits {
    /// The limits of a model, from the table and the project's `TextsConfig::models`.
    ///
    /// `provider` is `openai` or `claude`. The PHP package calls this `ModelLimits::for()`, which
    /// is a keyword here.
    pub fn for_model(provider: &str, model: &str) -> Self {
        let table = if provider == "claude" { CLAUDE } else { OPENAI };

        let mut rows: Vec<(String, Self)> = table
            .iter()
            .map(|(prefix, tokens, temperature, json, reasoning, effort)| {
                (
                    (*prefix).to_owned(),
                    Self {
                        max_output_tokens: *tokens,
                        temperature: *temperature,
                        json: *json,
                        reasoning: *reasoning,
                        reasoning_effort: effort.map(str::to_owned),
                    },
                )
            })
            .collect();

        // A project's own rows, by name or prefix, over the package's. A field the row leaves out
        // comes from the package's row, or from the cautious defaults of a model it does not know.
        settings(|config| {
            for (prefix, row) in &config.models {
                match rows.iter_mut().find(|(known, _)| known == prefix) {
                    Some((_, limits)) => limits.apply(row),
                    None => {
                        let mut limits = Self::unknown();

                        limits.apply(row);
                        rows.push((prefix.clone(), limits));
                    }
                }
            }
        });

        rows.into_iter()
            .filter(|(prefix, _)| model.starts_with(prefix.as_str()))
            .max_by_key(|(prefix, _)| prefix.len())
            .map_or_else(Self::unknown, |(_, limits)| limits)
    }

    fn unknown() -> Self {
        let (_, tokens, temperature, json, reasoning, effort) = UNKNOWN;

        Self {
            max_output_tokens: tokens,
            temperature,
            json,
            reasoning,
            reasoning_effort: effort.map(str::to_owned),
        }
    }

    fn apply(&mut self, row: &ModelOverride) {
        if let Some(tokens) = row.max_output_tokens {
            self.max_output_tokens = tokens.max(1);
        }

        if let Some(temperature) = row.temperature {
            self.temperature = temperature;
        }

        // An unknown JSON mode falls back to `none`.
        if let Some(json) = &row.json {
            self.json = Json::parse(json).unwrap_or(Json::None);
        }

        if let Some(reasoning) = row.reasoning {
            self.reasoning = reasoning;
        }

        if let Some(effort) = &row.reasoning_effort {
            self.reasoning_effort = Some(effort.clone());
        }
    }
}