rig-core 0.47.0

An opinionated library for building LLM powered applications.
Documentation
//! Copilot's dialect of the OpenAI wires. Completion takes the route the
//! model catalog lists for the model, else the Responses route for Codex
//! model identifiers and Chat Completions otherwise; every request carries
//! the editor envelope. Credentials must be
//! exchanged before encoding (see [`auth`](super::auth)).
//!
//! ```no_run
//! use rig_core::providers::copilot::{self, DIALECT};
//! use rig_core::providers::openai::OpenAIConfig;
//!
//! # fn run(http: rig_core::http_client::DynHttpClient) {
//! let github = OpenAIConfig::with_key(&DIALECT, "tid=session-token").connect(http);
//! let model = github.completion(copilot::GPT_4O);
//! # let _ = model;
//! # }
//! ```

use crate::completion::CompletionRequest;
use crate::json_utils::Lenient;
use crate::model::{ModelInfo, ModelList};
use crate::providers::internal::credential_header;
use crate::providers::openai::responses_api::SystemInstructionsPlacement;
use crate::providers::openai::wire::{
    Dialect, DialectHooks, EmbeddingQuirks, OpenAIConfig, Quirks, ResponsesQuirks, Route,
};
use crate::wire::Body;
use serde_json::Value;

use super::PROVIDER_NAME;

/// The reply header carrying Copilot's transport request id. Copilot relays
/// OpenAI's wire on both routes, header included.
pub(super) const REQUEST_ID_HEADER: Option<&str> = Some("x-request-id");

/// Copilot dialect with catalog- and model-based routing and editor-identity
/// headers.
/// Responses requests place system messages in `input`. Verification uses
/// token exchange rather than a dedicated endpoint.
pub const DIALECT: Dialect = Dialect {
    base_url_env: Some("GITHUB_COPILOT_API_BASE"),
    request_id_header: REQUEST_ID_HEADER,
    quirks: Quirks {
        hooks: Some(&HOOKS),
        verify_path: "",
        // Copilot bills a subscription, not the catalog's API rates.
        catalog_priced: false,
        base_url_env_alias: Some("COPILOT_BASE_URL"),
        // Copilot keeps no files, so no file id resolves there.
        accepts_file_ids: false,
        short_lived_key: true,
        // Copilot serves some models only on `/responses`, and the catalog
        // says which.
        routes_by_catalog: true,
        embedding: EmbeddingQuirks {
            requires_usage: false,
            ..EmbeddingQuirks::openai()
        },
        responses: ResponsesQuirks {
            strict_tools_by_default: true,
            system_instructions: SystemInstructionsPlacement::InputSystemMessages,
            ..ResponsesQuirks::openai()
        },
        ..Quirks::openai()
    },
    ..Dialect::gateway(
        PROVIDER_NAME,
        super::GITHUB_COPILOT_API_BASE_URL,
        "GITHUB_COPILOT_API_KEY",
    )
};

static HOOKS: DialectHooks = DialectHooks {
    default_endpoint: Some(super::auth::base_url_from_token),
    model_route: Some(|model| {
        if routes_through_responses(model) {
            Route::Responses
        } else {
            Route::Chat
        }
    }),
    completion_envelope: Some(completion_envelope),
    // Non-conversational modality requests use a user initiator.
    modality_envelope: Some(|provider, request| {
        stamp(request, provider.api_key.expose(), "user", false)
    }),
    model_listing: Some(catalogue),
};

/// The `GET /models` catalogue's `data`, each entry naming its id, its name,
/// its vendor, its modality under `capabilities.type` and its tool-call
/// support under `capabilities.supports.tool_calls`. The whole catalogue
/// comes in one reply.
fn catalogue(event: Value) -> ModelList {
    let text =
        |entry: &Value, pointer: &str| entry.at(pointer).and_then(Value::as_str).map(str::to_owned);
    let models = event
        .arr("data")
        .iter()
        .filter_map(|entry| {
            let mut model = ModelInfo::from_id(entry.str("id")?);
            model.name = text(entry, "/name");
            model.owned_by = text(entry, "/vendor");
            model.r#type = text(entry, "/capabilities/type");
            model.supports_tool_calls = entry
                .at("/capabilities/supports/tool_calls")
                .and_then(Value::as_bool);
            Some(model)
        })
        .collect();
    ModelList::new(models)
}

/// The editor envelope of a completion request: the initiator and vision
/// flag follow the request's history.
fn completion_envelope(
    provider: &OpenAIConfig,
    request: &CompletionRequest,
    mut builder: http::request::Builder,
) -> http::request::Builder {
    for (name, value) in super::default_headers(
        provider.api_key.expose(),
        super::request_initiator(request),
        super::request_has_vision(request),
    ) {
        if let Some(headers) = builder.headers_mut() {
            headers.remove(name);
        }
        builder = if name == http::header::AUTHORIZATION {
            credential_header(builder, name, &value)
        } else {
            builder.header(name, value)
        };
    }
    builder
}

/// Return whether `model` contains `codex`, case-insensitively, selecting
/// `/responses` for a model the catalog does not route.
pub fn routes_through_responses(model: &str) -> bool {
    model.to_ascii_lowercase().contains("codex")
}

/// Stamp Copilot's request envelope onto a built request.
///
/// Used by the modality hook and the catalogue wire. `insert` replaces the
/// shared authentication header rather than appending a second credential.
/// Completion routes use `completion_envelope` during encoding instead.
pub(super) fn stamp(
    request: &mut http::Request<Body>,
    api_key: &str,
    initiator: &'static str,
    has_vision: bool,
) -> Result<(), http::Error> {
    let map = request.headers_mut();
    for (name, value) in super::default_headers(api_key, initiator, has_vision) {
        let name = http::HeaderName::from_bytes(name.as_bytes())?;
        let mut value = http::HeaderValue::from_str(&value)?;
        value.set_sensitive(name == http::header::AUTHORIZATION);
        map.insert(name, value);
    }
    Ok(())
}

#[cfg(test)]
mod tests;