rig-core 0.47.0

An opinionated library for building LLM powered applications.
Documentation
use crate::error::EncodeError;
use crate::error::ProviderError;
use crate::json_utils::Lenient;
use crate::{
    model::{ModelInfo, ModelList, listing},
    operation::{ModelListing, ModelPage, Verify, VerifyDecoder},
    providers::internal::{wire::classify_marker_keyed_frame, with_query_pairs},
    wire::{Body, Descriptor, DocumentDecoder, Encoded, Framing, Mode, Wire},
};
use serde::{Deserialize, Serialize};
use serde_json::Value;

const MAX_PAGE_SIZE: usize = 1000;

/// The model a listing entry names: its `baseModelId`, else its `name`
/// without the `models/` prefix, with the limits it reports.
fn model_of(entry: &Value) -> Option<ModelInfo> {
    let id = entry
        .str("baseModelId")
        .map(str::trim)
        .filter(|id| !id.is_empty());
    let name = entry.str("name").map(str::trim).unwrap_or_default();
    let name = name.strip_prefix("models/").unwrap_or(name);
    let id = id.or((!name.is_empty()).then_some(name))?;
    let limit = |key: &str| entry.u64(key).and_then(|limit| u32::try_from(limit).ok());
    let mut model = ModelInfo::from_id(id.to_owned());
    model.name = entry.str("displayName").map(str::to_owned);
    model.description = entry.str("description").map(str::to_owned);
    model.context_length = limit("inputTokenLimit");
    model.max_output_tokens = limit("outputTokenLimit");
    Some(model)
}

fn list_models_path(page_token: Option<&str>) -> String {
    let page_size = MAX_PAGE_SIZE.to_string();
    let mut pairs = vec![("pageSize", page_size.as_str())];
    if let Some(page_token) = page_token {
        pairs.push(("pageToken", page_token));
    }
    with_query_pairs("/v1beta/models", &pairs)
}

/// A decoded model page with an optional nonempty continuation cursor.
fn parse_models_page(body: &[u8], path: &str) -> Result<ModelPage, ProviderError> {
    let error =
        |details: &dyn std::fmt::Display| listing::parse_error("Gemini", path, details, body);
    let page: Value = serde_json::from_slice(body)
        .map_err(|parse| error(&format_args!("parse_error={parse}")))?;
    if page.get("models").is_some_and(|models| !models.is_array()) {
        return Err(error(&"parse_error=`models` is not a list"));
    }
    let models = page
        .arr("models")
        .iter()
        .map(|entry| {
            model_of(entry).ok_or_else(|| {
                error(&"parse_error=model entry missing usable `baseModelId` and `name` values")
            })
        })
        .collect::<Result<Vec<_>, _>>()?;
    // Sending an empty cursor would repeatedly fetch the same page.
    Ok(ModelPage {
        models: ModelList::new(models),
        next: page
            .str("nextPageToken")
            .filter(|token| !token.is_empty())
            .map(str::to_owned),
    })
}

impl super::GeminiConfig {
    /// The GenerateContent model-listing wire.
    pub(crate) fn models(&self) -> Models {
        Models::new(self.clone())
    }

    /// The credential-check wire.
    pub(crate) fn verify(&self) -> VerifyKey {
        VerifyKey::new(self.clone())
    }
}

#[cfg(test)]
mod tests;

/// The top-level keys a genuine `GET /v1beta/models` reply carries.
///
/// A frame naming either must decode fully or it is corrupt; JSON naming
/// neither is some other endpoint's reply rather than a page of this one.
const MODEL_PAGE_MARKERS: &[&str] = &["models", "nextPageToken"];

/// One page's request, after `page_token` when a page named one.
fn list_models_request(
    provider: &super::GeminiConfig,
    page_token: Option<&str>,
) -> Result<http::Request<Body>, EncodeError> {
    provider
        .request(http::Method::GET, &list_models_path(page_token))
        .body(Body::empty())
        .map_err(EncodeError::from)
}

/// The GenerateContent model-listing wire: `GET /v1beta/models`, paged on
/// `nextPageToken`.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct Models {
    /// The provider this wire speaks to.
    pub provider: super::GeminiConfig,
}

impl Models {
    /// Build the wire over `provider`.
    pub fn new(provider: super::GeminiConfig) -> Self {
        Self { provider }
    }
}

impl Wire for Models {
    type Op = ModelListing;
    type Payload = crate::wire::Encoded;
    type Frame = crate::wire::WireFrame;
    type Event = String;
    type Decoder<'id> = DocumentDecoder<ModelListing, String>;
    type Reassembler = crate::wire::document::Unreassembled;

    fn describe(&self) -> Descriptor<'_> {
        Descriptor::new(super::PROVIDER_NAME)
    }

    /// A listing never streams, so both modes send the one request.
    fn encode(&self, cursor: Option<String>, _mode: Mode) -> Result<Encoded, EncodeError> {
        Ok(Encoded::new(
            list_models_request(&self.provider, cursor.as_deref())?,
            Framing::Whole,
        ))
    }

    fn decoder<'id>(&self) -> Self::Decoder<'id> {
        models_decoder()
    }
}

/// Decode model pages and the cursor each names, keeping the page JSON for
/// model-entry error context.
fn models_decoder() -> DocumentDecoder<ModelListing, String> {
    DocumentDecoder::new(
        |body| {
            classify_marker_keyed_frame::<Value>(body, MODEL_PAGE_MARKERS).map(|_| body.to_owned())
        },
        |payload| {
            // Diagnostics identify the endpoint without retaining the previous page's cursor.
            parse_models_page(payload.as_bytes(), &list_models_path(None))
        },
    )
}

/// Check credentials with `GET /v1beta/models`.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct VerifyKey {
    /// The provider this wire speaks to.
    pub provider: super::GeminiConfig,
}

impl VerifyKey {
    /// Build the wire over `provider`.
    pub fn new(provider: super::GeminiConfig) -> Self {
        Self { provider }
    }
}

impl Wire for VerifyKey {
    type Op = Verify;
    type Payload = crate::wire::Encoded;
    type Frame = crate::wire::WireFrame;
    type Event = ();
    type Decoder<'id> = VerifyDecoder;
    type Reassembler = crate::wire::document::Unreassembled;

    fn describe(&self) -> Descriptor<'_> {
        Descriptor::new(super::PROVIDER_NAME)
    }

    fn encode(&self, _request: (), _mode: Mode) -> Result<Encoded, EncodeError> {
        let request = self
            .provider
            .request(http::Method::GET, "/v1beta/models")
            .body(Body::empty())?;
        Ok(Encoded::new(request, Framing::Whole))
    }

    fn decoder<'id>(&self) -> Self::Decoder<'id> {
        VerifyDecoder
    }
}