| /* | |
| * Licensed to the Apache Software Foundation (ASF) under one | |
| * or more contributor license agreements. See the NOTICE file | |
| * distributed with this work for additional information | |
| * regarding copyright ownership. The ASF licenses this file | |
| * to you under the Apache License, Version 2.0 (the | |
| * "License"); you may not use this file except in compliance | |
| * with the License. You may obtain a copy of the License at | |
| * | |
| * http://www.apache.org/licenses/LICENSE-2.0 | |
| * | |
| * Unless required by applicable law or agreed to in writing, | |
| * software distributed under the License is distributed on an | |
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
| * KIND, either express or implied. See the License for the | |
| * specific language governing permissions and limitations | |
| * under the License. | |
| */ | |
| //! Compiled API provider policies use explicit definitions and credential contracts. | |
| use crate::facts::{self, AuthKind, ProviderFacts}; | |
| use futures_util::future::BoxFuture; | |
| use maka_agent_facts::configuration::ApiProtocol as Wire; | |
| use maka_agent_facts::configuration::ModelInfo; | |
| use maka_agent_facts::configuration::ModelOverride; | |
| use maka_agent_ports::model::RequestCompression; | |
| use maka_agent_ports::provider::Connection; | |
| use maka_agent_ports::provider::Definition; | |
| use maka_agent_ports::provider::Descriptor; | |
| use maka_agent_ports::provider::Error; | |
| use maka_agent_ports::provider::Model; | |
| use maka_agent_ports::provider::Policy; | |
| use maka_agent_ports::provider::Provider; | |
| use maka_agent_ports::provider::Resolve; | |
| use serde::{Deserialize, Serialize}; | |
| use serde_json::json; | |
| use std::sync::Arc; | |
| mod authentication; | |
| mod discovery; | |
| mod effects; | |
| mod inventory; | |
| mod options; | |
| mod output; | |
| mod route; | |
| mod usage; | |
| pub use output::fixed_thinking_tokens; | |
| pub const ID: &str = "maka.providers"; | |
| /// Catalog metadata and configured execution choices remain distinct: `info` | |
| /// contains model facts; thinking policy includes the user's overrides. | |
| pub struct ModelMetadata { | |
| pub info: ModelInfo, | |
| pub thinking_levels: Vec<maka_agent_facts::execution::ThinkingLevel>, | |
| pub default_thinking_level: Option<maka_agent_facts::execution::ThinkingLevel>, | |
| } | |
| pub struct ApiProviders; | |
| impl ApiProviders { | |
| /// A compiled API implementation for a native application owner. Availability | |
| /// does not activate legacy contributions or grant execution authority. | |
| pub fn definition( | |
| id: &str, | |
| snapshot: Arc<crate::catalog::Snapshot>, | |
| ) -> Result<Definition, String> { | |
| Self::provider(id, snapshot)?.definition() | |
| } | |
| /// Selectable model metadata, checked by the same preparation policy as | |
| /// execution. No credentials or network access are needed. An invalid | |
| /// configured default is an error, never silently replaced by another level. | |
| pub fn model_metadata( | |
| id: &str, | |
| snapshot: Arc<crate::catalog::Snapshot>, | |
| connection: Connection, | |
| model: ModelInfo, | |
| overrides: Option<ModelOverride>, | |
| ) -> Result<ModelMetadata, String> { | |
| let provider = Self::provider(id, snapshot)?; | |
| let mut metadata = provider.model_metadata(model.clone(), overrides.as_ref()); | |
| let mut request = Resolve { | |
| service_tier: None, | |
| connection, | |
| model, | |
| overrides, | |
| thinking_level: None, | |
| }; | |
| let prepared = provider | |
| .prepare(request.clone()) | |
| .map_err(|error| error.to_string())?; | |
| if matches!( | |
| prepared.protocol, | |
| maka_agent_ports::model::ProviderKind::Anthropic | |
| ) { | |
| metadata.info.service_tiers.clear(); | |
| } | |
| metadata.thinking_levels.retain(|level| { | |
| request.thinking_level = Some(*level); | |
| provider.prepare(request.clone()).is_ok() | |
| }); | |
| Ok(metadata) | |
| } | |
| fn provider(id: &str, snapshot: Arc<crate::catalog::Snapshot>) -> Result<ApiProvider, String> { | |
| let (id, facts) = facts::all() | |
| .map_err(|error| error.to_string())? | |
| .iter() | |
| .find(|(name, facts)| { | |
| name.as_str() == id && !facts.retired && facts.auth_kind != AuthKind::OauthToken | |
| }) | |
| .ok_or("API provider is unavailable")?; | |
| Ok(ApiProvider { | |
| id: id.as_str(), | |
| facts, | |
| snapshot, | |
| }) | |
| } | |
| /// Available compiled API policies, without activating legacy contributions. | |
| pub fn names() -> Result<Vec<String>, String> { | |
| Ok(facts::all() | |
| .map_err(|error| error.to_string())? | |
| .iter() | |
| .filter(|(_, facts)| !facts.retired && facts.auth_kind != AuthKind::OauthToken) | |
| .map(|(name, _)| name.clone()) | |
| .collect()) | |
| } | |
| } | |
| #[derive(Clone)] | |
| struct ApiProvider { | |
| id: &'static str, | |
| facts: &'static ProviderFacts, | |
| snapshot: Arc<crate::catalog::Snapshot>, | |
| } | |
| #[derive(Deserialize, Serialize, schemars::JsonSchema)] | |
| #[serde(rename_all = "camelCase", deny_unknown_fields)] | |
| struct Configuration { | |
| #[schemars(length(min = 1))] | |
| base_url: String, | |
| #[serde(default)] | |
| request_compression: RequestCompression, | |
| } | |
| impl Configuration { | |
| fn read(value: &serde_json::Value) -> Result<Self, Error> { | |
| let mut configuration: Self = | |
| serde_json::from_value(value.clone()).map_err(|_| Error::InvalidConfiguration)?; | |
| configuration.base_url = maka_agent_facts::configuration::validation::normalize_base_url( | |
| &configuration.base_url, | |
| ) | |
| .map_err(|_| Error::InvalidConfiguration)?; | |
| Ok(configuration) | |
| } | |
| } | |
| /// Pure policy input, not a Host catalog or a credential handle. | |
| struct Selection<'a> { | |
| provider: &'a str, | |
| connection: &'a Connection, | |
| base_url: &'a str, | |
| model: &'a ModelInfo, | |
| overrides: Option<&'a ModelOverride>, | |
| } | |
| impl ApiProvider { | |
| fn definition(&self) -> Result<Definition, String> { | |
| let policy = self | |
| .facts | |
| .policy | |
| .get_or_init(|| self.compile_policy()) | |
| .clone()?; | |
| Ok(Definition::new(policy, Arc::new(self.clone()))) | |
| } | |
| fn compile_policy(&self) -> Result<Arc<Policy>, String> { | |
| let authentication = | |
| authentication::methods(self.facts.auth_kind).map_err(|error| error.to_string())?; | |
| Policy::new(Descriptor { | |
| label: match self.id { | |
| "openai-compatible" => "Chat Completions".into(), | |
| "openai-responses-compatible" => "Responses".into(), | |
| "anthropic-compatible" => "Anthropic Messages".into(), | |
| _ => self.facts.label.clone(), | |
| }, | |
| custom_endpoint: self.facts.base_url.is_empty(), | |
| configuration_schema: self.configuration_schema()?, | |
| configuration_defaults: if self.facts.base_url.is_empty() { | |
| json!({}) | |
| } else { | |
| json!({"baseUrl":self.facts.base_url}) | |
| }, | |
| authentication, | |
| anonymous: matches!( | |
| self.facts.auth_kind, | |
| AuthKind::None | AuthKind::OptionalApiKey | |
| ), | |
| discovery: true, | |
| usage_authentication: usage::authentication(self.id), | |
| }) | |
| .map(Arc::new) | |
| .map_err(|error| error.to_string()) | |
| } | |
| /// A custom OpenAI-family endpoint advertises a versioned example base; | |
| /// the configured value stays authoritative and is never rewritten. | |
| fn configuration_schema(&self) -> Result<serde_json::Value, String> { | |
| let mut schema = serde_json::to_value(schemars::schema_for!(Configuration)) | |
| .map_err(|error| error.to_string())?; | |
| if self.facts.base_url.is_empty() | |
| && matches!( | |
| self.facts.model_discovery, | |
| facts::discovery::ModelDiscovery::Protocol { .. } | |
| ) | |
| && matches!( | |
| self.facts.runtime_adapter, | |
| facts::adapter::RuntimeAdapter::OpenaiChat {} | |
| | facts::adapter::RuntimeAdapter::OpenaiResponses { .. } | |
| | facts::adapter::RuntimeAdapter::OpenaiCompatible { .. } | |
| ) | |
| { | |
| schema["properties"]["baseUrl"]["examples"] = json!(["https://api.example.com/v1"]); | |
| } | |
| Ok(schema) | |
| } | |
| fn model_metadata(&self, model: ModelInfo, overrides: Option<&ModelOverride>) -> ModelMetadata { | |
| let info = self.snapshot.resolve(self.id, model, overrides); | |
| let thinking_levels = overrides | |
| .and_then(|o| o.thinking_levels.clone()) | |
| .or_else(|| info.thinking_levels.clone()) | |
| .unwrap_or_default(); | |
| let default_thinking_level = overrides.and_then(|o| o.default_thinking_level).or(info | |
| .default_thinking_level | |
| .filter(|level| thinking_levels.contains(level))); | |
| ModelMetadata { | |
| info, | |
| thinking_levels, | |
| default_thinking_level, | |
| } | |
| } | |
| fn prepare(&self, request: Resolve) -> Result<Model, Error> { | |
| let configuration = Configuration::read(&request.connection.configuration)?; | |
| let ModelMetadata { | |
| mut info, | |
| thinking_levels, | |
| default_thinking_level, | |
| } = self.model_metadata(request.model, request.overrides.as_ref()); | |
| // Public route limits describe the provider endpoint, not a model name | |
| // served by an arbitrary user-selected gateway. | |
| let canonical_endpoint = | |
| maka_agent_facts::configuration::validation::normalize_base_url(&self.facts.base_url) | |
| .ok(); | |
| if canonical_endpoint.as_deref() != Some(configuration.base_url.as_str()) { | |
| info.image_input = request | |
| .overrides | |
| .as_ref() | |
| .and_then(|overrides| overrides.image_input.clone()); | |
| } | |
| let selection = Selection { | |
| provider: self.id, | |
| connection: &request.connection, | |
| base_url: &configuration.base_url, | |
| model: &info, | |
| overrides: request.overrides.as_ref(), | |
| }; | |
| let route = route::resolve(&selection, self.facts)?; | |
| route.check_execution()?; | |
| let level = request.thinking_level.or(default_thinking_level); | |
| if level.is_some_and(|level| !thinking_levels.contains(&level)) { | |
| return Err(Error::UnsupportedThinkingLevel); | |
| } | |
| let options = options::resolve( | |
| &selection, | |
| self.facts, | |
| &thinking_levels, | |
| level, | |
| &route, | |
| request.service_tier.as_deref(), | |
| )?; | |
| let output = output::resolve(&selection, route.wire, &options)?; | |
| let adapter = request | |
| .overrides | |
| .as_ref() | |
| .and_then(|o| o.adapter.clone()) | |
| .unwrap_or_else(|| { | |
| match route.wire { | |
| Wire::OpenaiChatCompletions => "chat-completions", | |
| Wire::OpenaiResponses => "responses", | |
| Wire::AnthropicMessages => "anthropic-messages", | |
| } | |
| .into() | |
| }); | |
| if self.id == "deepseek" | |
| && canonical_endpoint.as_deref() == Some(configuration.base_url.as_str()) | |
| && route.wire == Wire::OpenaiResponses | |
| && let Some(web_search) = self | |
| .facts | |
| .models | |
| .get(&info.id) | |
| .and_then(|model| model.metadata.capabilities) | |
| .and_then(|capabilities| capabilities.web_search) | |
| { | |
| info.capabilities.get_or_insert_default().web_search = request | |
| .overrides | |
| .as_ref() | |
| .and_then(|overrides| overrides.capabilities.as_ref()) | |
| .and_then(|capabilities| capabilities.web_search) | |
| .or(Some(web_search)); | |
| } | |
| let model = Model { | |
| adapter, | |
| protocol: route.kind, | |
| base_url: route.base_url, | |
| info, | |
| thinking_levels, | |
| provider_options: options, | |
| main_output_limit: output, | |
| request_compression: configuration.request_compression, | |
| }; | |
| model.validate().map_err(|_| Error::InvalidPreparedModel)?; | |
| Ok(model) | |
| } | |
| } | |
| impl Provider for ApiProvider { | |
| fn validate_configuration(&self, configuration: &serde_json::Value) -> Result<(), Error> { | |
| Configuration::read(configuration).map(|_| ()) | |
| } | |
| fn resolve(&self, request: Resolve) -> BoxFuture<'_, Result<Model, Error>> { | |
| Box::pin(async move { self.prepare(request) }) | |
| } | |
| fn authorize( | |
| &self, | |
| connection: Connection, | |
| credential: Option<maka_agent_facts::provider::Credential>, | |
| _: String, | |
| ) -> BoxFuture<'_, Result<maka_agent_ports::model::Credentials, Error>> { | |
| Box::pin(async move { | |
| Configuration::read(&connection.configuration)?; | |
| authentication::authorize(self.facts.auth_kind, credential) | |
| }) | |
| } | |
| fn authenticate( | |
| &self, | |
| request: maka_agent_ports::provider::authentication::Authenticate, | |
| _: maka_agent_ports::provider::Context, | |
| ) -> BoxFuture<'_, Result<maka_agent_facts::provider::Credential, Error>> { | |
| Box::pin(async move { | |
| Configuration::read(&request.connection.configuration)?; | |
| authentication::authenticate(request) | |
| }) | |
| } | |
| fn discover( | |
| &self, | |
| request: maka_agent_ports::provider::Discovery, | |
| context: maka_agent_ports::provider::Context, | |
| ) -> BoxFuture<'_, Result<Vec<ModelInfo>, Error>> { | |
| Box::pin(async move { effects::discover(self, request, context).await }) | |
| } | |
| fn usage_limits( | |
| &self, | |
| request: maka_agent_ports::provider::UsageLimitsRequest, | |
| context: maka_agent_ports::provider::Context, | |
| ) -> BoxFuture<'_, Result<Vec<maka_agent_ports::provider::UsageLimit>, Error>> { | |
| Box::pin(usage::read(self, request, context)) | |
| } | |
| fn verify( | |
| &self, | |
| request: maka_agent_ports::provider::Verification, | |
| context: maka_agent_ports::provider::Context, | |
| ) -> BoxFuture<'_, Result<(), Error>> { | |
| Box::pin(async move { effects::verify(self, request, context).await }) | |
| } | |
| } | |
| #[cfg(test)] | |
| mod tests { | |
| use super::*; | |
| #[test] | |
| fn catalog_usage_opt_out_survives_chat_preparation() { | |
| let facts = facts::provider_facts("github-copilot").unwrap(); | |
| let provider = ApiProvider { | |
| id: "github-copilot", | |
| facts, | |
| snapshot: Arc::new(crate::catalog::Snapshot::bundled().unwrap()), | |
| }; | |
| let model = provider | |
| .prepare(Resolve { | |
| service_tier: None, | |
| connection: Connection { | |
| id: "copilot".into(), | |
| revision: 1, | |
| configuration: json!({"baseUrl":facts.base_url}), | |
| }, | |
| model: ModelInfo::new("kimi-k3"), | |
| overrides: None, | |
| thinking_level: None, | |
| }) | |
| .unwrap(); | |
| assert_eq!(model.provider_options["maka"]["includeUsage"], false); | |
| } | |
| } |