blob: 3c4a9c3e981c842512e53fdd86ffc64f2c6d757a [file]
/*
* 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.
*/
use std::collections::HashSet;
use maka_agent_facts::configuration::ApiProtocol;
use maka_agent_facts::configuration::ConnectionEffectFailureClass;
use maka_agent_facts::configuration::ModelCapabilities;
use maka_agent_facts::configuration::ModelInfo;
use maka_agent_facts::configuration::ModelModality;
use serde_json::Value;
use crate::catalog::{modalities, token_limit};
use crate::metadata::thinking_levels;
type Failure = ConnectionEffectFailureClass;
/// Provider-specific JSON ends here; catalog normalization operates on typed rows.
pub(super) fn from_rows<'a>(
provider: &str,
wire: ApiProtocol,
rows: impl IntoIterator<Item = &'a Value>,
) -> Result<Vec<ModelInfo>, Failure> {
let mut models = Vec::new();
for row in rows {
// Magpie includes image generators when asked for per-model search capabilities.
if provider == "magpie" && row["kind"] == "image" {
continue;
}
let Some(mut model) = model_info(provider, row)? else {
continue;
};
if wire != ApiProtocol::AnthropicMessages
&& let Some(tiers) = row.get("service_tiers").filter(|tiers| !tiers.is_null())
{
model.service_tiers =
serde::Deserialize::deserialize(tiers).map_err(|_| Failure::InvalidResponse)?;
}
if provider == "magpie" {
model.capabilities.get_or_insert_default().web_search =
Some(row["web_search"] == "native");
}
models.push(model);
}
normalize(models)
}
pub(super) fn normalize(models: Vec<ModelInfo>) -> Result<Vec<ModelInfo>, Failure> {
let mut seen = HashSet::new();
let mut normalized = Vec::new();
for mut model in models {
let id = model.id.trim_matches(model_whitespace);
if id.is_empty()
|| id.chars().any(|c| c <= '\u{1f}' || c == '\u{7f}')
|| !seen.insert(id.to_owned())
{
continue;
}
model.id = id.to_owned();
model.validate().map_err(|_| Failure::InvalidResponse)?;
normalized.push(model);
}
Ok(normalized)
}
fn optional_array(value: Option<&Value>) -> Result<&[Value], Failure> {
match value {
None => Ok(&[]),
Some(Value::Array(values)) => Ok(values),
_ => Err(Failure::InvalidResponse),
}
}
fn object_array(value: Option<&Value>) -> Result<&[Value], Failure> {
let values = optional_array(value)?;
if values.iter().any(|v| !v.is_object()) {
return Err(Failure::InvalidResponse);
}
Ok(values)
}
fn contains(values: &[Value], expected: &str) -> bool {
values.iter().any(|v| v.as_str() == Some(expected))
}
fn model_info(provider: &str, row: &Value) -> Result<Option<ModelInfo>, Failure> {
let Some(id) = row
.get("id")
.and_then(Value::as_str)
.filter(|id| !id.is_empty())
else {
return Ok(None);
};
if provider == "openrouter" {
return crate::catalog::openrouter::model(row)
.map(Some)
.map_err(|_| Failure::InvalidResponse);
}
let input = row
.pointer("/modalities/input")
.or_else(|| row.get("input_modalities"));
let output = row
.pointer("/modalities/output")
.or_else(|| row.get("output_modalities"));
let input_values = optional_array(input)?;
optional_array(output)?;
let modalities = modalities(input, output).map_err(|_| Failure::InvalidResponse)?;
let tags = optional_array(row.get("tags"))?;
let providers = object_array(row.get("providers"))?;
let parameters = optional_array(row.get("supported_parameters"))?;
let supports_image = row.get("supports_image_in").and_then(Value::as_bool);
let vision = row.pointer("/capabilities/vision").and_then(Value::as_bool);
let mut capabilities = ModelCapabilities {
vision: (supports_image == Some(true) || vision == Some(true) || contains(tags, "vision"))
.then_some(true)
.or(supports_image)
.or(vision)
.or_else(|| {
let has_image = modalities
.as_ref()
.is_some_and(|m| m.input.contains(&ModelModality::Image));
input_values
.iter()
.any(Value::is_string)
.then_some(has_image)
}),
reasoning: row
.get("supports_reasoning")
.and_then(Value::as_bool)
.or_else(|| row.get("reasoning").and_then(Value::as_bool))
.or_else(|| {
row.pointer("/capabilities/reasoning")
.and_then(Value::as_bool)
}),
function_calling: row
.get("tool_call")
.and_then(Value::as_bool)
.or_else(|| {
row.get("providers").map(|_| {
providers.iter().any(|provider| {
provider.get("status").and_then(Value::as_str) == Some("live")
&& provider.get("supports_tools").and_then(Value::as_bool) == Some(true)
})
})
})
.or_else(|| {
(contains(tags, "tool-use") || contains(parameters, "tools")).then_some(true)
}),
..Default::default()
};
if contains(tags, "reasoning") {
capabilities.reasoning = Some(true);
}
if let Some(m) = &modalities
&& !m.output.is_empty()
&& !m.output.contains(&ModelModality::Text)
{
capabilities.chat = Some(false);
capabilities.image_generation = m.output.contains(&ModelModality::Image).then_some(true);
}
let mut model = ModelInfo::new(id);
model.display_name = match row
.get("display_name")
.filter(|value| !value.is_null())
.or(row.get("name"))
{
None | Some(Value::Null) => None,
Some(Value::String(value)) => Some(value.clone()),
_ => return Err(Failure::InvalidResponse),
};
model.context_window =
token_limit(row.get("context_length")).or_else(|| token_limit(row.get("context_window")));
model.input_limit = token_limit(row.get("max_input_tokens"));
model.max_output_tokens =
token_limit(row.get("max_output_tokens")).or_else(|| token_limit(row.get("max_tokens")));
if let Some(levels) = row
.get("reasoning_levels")
.or_else(|| row.get("supported_reasoning_levels"))
{
model.thinking_levels = Some(thinking_levels(
optional_array(Some(levels))?
.iter()
.filter_map(|value| value.as_str().or_else(|| value.get("effort")?.as_str())),
));
}
model.modalities = modalities;
if capabilities != ModelCapabilities::default() {
model.capabilities = Some(capabilities);
}
Ok(Some(model))
}
fn model_whitespace(c: char) -> bool {
matches!(c, '\u{9}'..='\u{d}' | '\u{20}' | '\u{a0}' | '\u{1680}'
| '\u{2000}'..='\u{200a}' | '\u{2028}' | '\u{2029}' | '\u{202f}'
| '\u{205f}' | '\u{3000}' | '\u{feff}')
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
fn decode(body: &[u8]) -> Result<Vec<Value>, Failure> {
let text = String::from_utf8_lossy(body);
let root: Value = serde_json::from_str(text.trim_start_matches('\u{feff}'))
.map_err(|_| Failure::InvalidResponse)?;
if !root.is_object() && !root.is_array() {
return Err(Failure::InvalidResponse);
}
super::from_rows(
"fixture",
ApiProtocol::OpenaiResponses,
object_array(root.get("data"))?,
)
.map(|models| {
models
.into_iter()
.map(|model| serde_json::to_value(model).unwrap())
.collect()
})
}
#[test]
fn metadata_precedence_and_unknown_fields() {
let rows = decode(
br#"{"data":[{"id":" x ","display_name":"","name":"fallback",
"context_length":0,"context_window":4096.0,"max_tokens":1e3,
"input_modalities":["image"],"output_modalities":[null,"Text","image"],
"supports_image_in":false,"capabilities":{"reasoning":true},
"supports_reasoning":false,"tags":["reasoning","tool-use"],"providers":[],
"unknown":true}]}"#,
)
.unwrap();
assert_eq!(
rows,
vec![json!({"id":"x","displayName":"","contextWindow":4096,
"maxOutputTokens":1000,"modalities":{"input":["image"],"output":["image"]},
"capabilities":{"vision":false,"reasoning":true,
"functionCalling":false,"chat":false,"imageGeneration":true}})]
);
}
#[test]
fn generic_inventory_distinguishes_explicit_capabilities_from_inference() {
let rows = decode(
br#"{"data":[
{"id":"text","input_modalities":["text"],"supported_parameters":["tools"],
"supported_reasoning_levels":["max",{"effort":"none"},"off","new-level"]},
{"id":"vision","modalities":{"input":["text"]},"capabilities":{"vision":true},
"tool_call":false,"supported_parameters":["tools"],"reasoning":false},
{"id":"tagged","input_modalities":["text"],"tags":["vision"]},
{"id":"flagged","input_modalities":["text"],"supports_image_in":true},
{"id":"disabled","modalities":{"input":["image"]},"capabilities":{"vision":false}},
{"id":"empty","modalities":{"input":[],"output":["text"]}}
]}"#,
)
.unwrap();
assert_eq!(
rows[0]["capabilities"],
json!({"vision":false,"functionCalling":true})
);
assert_eq!(rows[0]["thinkingLevels"], json!(["off", "max"]));
assert_eq!(
rows[1]["capabilities"],
json!({"vision":true,"functionCalling":false,"reasoning":false})
);
assert_eq!(rows[2]["capabilities"]["vision"], true);
assert_eq!(rows[3]["capabilities"]["vision"], true);
assert_eq!(rows[4]["capabilities"]["vision"], false);
assert_eq!(
rows[5],
json!({"id":"empty","modalities":{"input":[],"output":["text"]}})
);
}
#[test]
fn first_duplicate_wins_and_long_ids_preserve_complete_identity() {
let body = json!({"data":[{"id":"\u{feff}x\u{feff}","name":"first"},
{"id":"x","name":"second"},{"id":"a\u{7f}"},{"id":"😀".repeat(257)},
{"id":"😀".repeat(256)},{"id":"\u{85}y\u{85}"}]})
.to_string();
let rows = decode(body.as_bytes()).unwrap();
assert_eq!(rows.len(), 4);
assert_eq!(rows[0], json!({"id":"x","displayName":"first"}));
assert_eq!(rows[1]["id"], "😀".repeat(257));
assert_eq!(rows[3]["id"], "\u{85}y\u{85}");
}
#[test]
fn envelope_and_metadata_structure_failures() {
for body in [
"null",
"true",
"{",
r#"{"data":null}"#,
r#"{"data":[1]}"#,
r#"{"data":[{"id":"x","providers":[null]}]}"#,
r#"{"data":[{"id":"x","tags":null}]}"#,
r#"{"data":[{"id":"x","name":42}]}"#,
r#"{"data":[{"id":"x"},{"id":"x","input_modalities":null}]}"#,
] {
assert_eq!(
decode(body.as_bytes()),
Err(Failure::InvalidResponse),
"{body}"
);
}
for body in [
"{}",
"[]",
r#"{"data":[{"name":"missing id","tags":null}]}"#,
] {
assert!(decode(body.as_bytes()).unwrap().is_empty());
}
}
#[test]
fn complete_inventory_preserves_additional_models_after_deduplication() {
let mut rows: Vec<Value> = (0..2048).map(|i| json!({"id":i.to_string()})).collect();
rows.push(json!({"id":"0"}));
assert_eq!(
decode(json!({"data":rows}).to_string().as_bytes())
.unwrap()
.len(),
2048
);
rows.push(json!({"id":"extra"}));
let models = decode(json!({"data":rows}).to_string().as_bytes()).unwrap();
assert_eq!(models.len(), 2049);
assert_eq!(models.last().unwrap()["id"], "extra");
}
#[test]
fn response_text_uses_lossy_utf8_and_strips_bom() {
assert_eq!(
decode(b"\xef\xbb\xbf{\"data\":[{\"id\":\"\xff\"}]}").unwrap(),
vec![json!({"id":"\u{fffd}"})]
);
}
}