Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 52 additions & 17 deletions crates/dim-app-server/src/model_catalog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -586,13 +586,12 @@ impl BundledCatalog {
let strategy = found_lookup.strategy;
let score = found_lookup.score;
entry = merge_entry(entry, &found, &explicit);
entry.capabilities.modalities = effective_modalities(
declared_modalities(input),
&found.capabilities.modalities,
input.get("vision").and_then(Value::as_bool),
);
let declared = input.get("capabilities").and_then(Value::as_object);
if input.get("modalities").is_none()
&& input.get("vision") != Some(&Value::Bool(true))
&& declared.is_none_or(|value| !value.contains_key("modalities"))
{
entry.capabilities.modalities = found.capabilities.modalities.clone();
}
macro_rules! inherit_capability {
($json:literal, $field:ident) => {
if input.get($json).is_none()
Expand Down Expand Up @@ -1062,20 +1061,17 @@ fn descriptor_entry(
if let Some(v) = capabilities {
apply_capabilities(&mut caps, v);
}
if let Some(values) = input.get("modalities").and_then(Value::as_array) {
caps.modalities = strings(values);
}
if let Some(v) = input.get("toolCalling").and_then(Value::as_bool) {
caps.tool_calling = v;
}
if let Some(v) = input.get("vision").and_then(Value::as_bool) {
caps.vision = v;
if v && input.get("modalities").is_none()
&& capabilities.is_none_or(|c| !c.contains_key("modalities"))
{
caps.modalities = vec!["text".into(), "image".into()];
}
}
caps.modalities = effective_modalities(
declared_modalities(input),
&[],
input.get("vision").and_then(Value::as_bool),
);
if let Some(r) = &reasoning {
caps.reasoning = r.supported;
}
Expand Down Expand Up @@ -1311,6 +1307,48 @@ pub(crate) fn declared_limits(input: &Map<String, Value>) -> (Option<u64>, Optio
)
}

/// The input modalities a model descriptor declares, or `None` when it
/// declares none. A listing that writes `modalities: []` has not said the model
/// reads nothing: the empty array is the undeclared value, so the identified
/// row and the descriptor's `vision` flag decide instead (#1980). The
/// descriptor's own array wins over the nested `capabilities` one.
pub fn declared_modalities(input: &Map<String, Value>) -> Option<Vec<String>> {
let declared = input
.get("modalities")
.and_then(Value::as_array)
.or_else(|| {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Problem: an empty top-level modalities array is taken as the declaration before the nested one is considered, so it hides a non-empty capabilities.modalities array. Failure scenario: {"modalities": [], "capabilities": {"modalities": ["text", "image"]}} for a model no row declares and with no top-level vision resolves to ["text"], losing the nested image declaration. Evidence: crates/dim-app-server/src/model_catalog.rs:1315-1327; the contract says an empty array states nothing and the rule covers the nested array (docs/design/appserver/provider-management.md section 3.5). Fix: take the first non-empty array, top-level when non-empty and nested otherwise.

input
.get("capabilities")
.and_then(|capabilities| capabilities.get("modalities"))
.and_then(Value::as_array)
})?;
let modalities = strings(declared);
(!modalities.is_empty()).then_some(modalities)
}

/// The input modalities an entry ends up declaring (#1980). A descriptor's
/// non-empty array is its own statement and stays. Otherwise the identified
/// row's set applies, and only a model no row declares takes the set its
/// `vision` flag implies: `["text", "image"]` when it accepts images and
/// `["text"]` otherwise.
pub fn effective_modalities(
declared: Option<Vec<String>>,
bundled: &[String],
vision: Option<bool>,
) -> Vec<String> {
if let Some(declared) = declared {
return declared;
}
if !bundled.is_empty() {
return bundled.to_vec();
}
if vision == Some(true) {
vec!["text".into(), "image".into()]
} else {
vec!["text".into()]
}
}

/// Marks the capability edits a legacy stored override cannot help but
/// carry (#2189). An override written before capability edits were marked is
/// a clone of the row it edited plus the user's values, with only its limits
Expand Down Expand Up @@ -1777,9 +1815,6 @@ fn pricing(raw: &Map<String, Value>) -> Option<ModelPricing> {
})
}
fn apply_capabilities(c: &mut CatalogCapabilities, m: &Map<String, Value>) {
if let Some(a) = m.get("modalities").and_then(Value::as_array) {
c.modalities = strings(a)
}
macro_rules! b {
($j:literal,$f:ident) => {
if let Some(v) = m.get($j).and_then(Value::as_bool) {
Expand Down
16 changes: 9 additions & 7 deletions crates/dim-app-server/src/provider/remote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,15 +330,17 @@ fn project(
if let Some(value) = output.first() {
descriptor.insert("maxOutputTokens".into(), (*value).clone());
}
if let Some(modalities) = item.get("modalities").and_then(|value| value.get("input")) {
descriptor.insert("modalities".into(), modalities.clone());
// An empty `modalities.input` states nothing about the model's input, the
// same way an empty descriptor `modalities` array does (#1980), so the
// listing writes neither the set nor a `vision` flag it would infer.
if let Some(Value::Array(modalities)) =
item.get("modalities").and_then(|value| value.get("input"))
&& !modalities.is_empty()
{
descriptor.insert("modalities".into(), Value::Array(modalities.clone()));
descriptor.insert(
"vision".into(),
Value::Bool(
modalities
.as_array()
.is_some_and(|values| values.iter().any(|v| v == "image")),
),
Value::Bool(modalities.iter().any(|v| v == "image")),
);
}
bundled
Expand Down
96 changes: 96 additions & 0 deletions crates/dim-app-server/tests/model_catalog_flow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,102 @@ fn media_lookup_and_explicit_capabilities_survive_editing() {
assert_eq!(edited.metadata, model.metadata);
}

/// A listing that writes `modalities: []` states nothing about what the model
/// reads; the empty array is the undeclared value, so the set the identified
/// row declares applies instead of being overwritten by it. A descriptor that
/// states no modalities of its own falls back to its `vision` declaration, and
/// an explicit non-empty array stays authoritative (#1980).
#[test]
fn an_empty_modalities_array_is_the_undeclared_value() {
let declaring = catalog(json!([vendor(
"acme",
json!([with_vision(
row("acme-vision", "Acme Vision", 400_000, 128_000),
&["text", "image", "audio"],
)]),
)]));
let provider = ProviderProjection {
provider_id: "acme".into(),
source: Some(ModelSource::Custom),
..Default::default()
};
let read = |catalog: &BundledCatalog, descriptor: serde_json::Value| {
catalog
.project(&provider, &descriptor)
.unwrap()
.capabilities
.modalities
};
for descriptor in [
json!({ "id": "acme-vision", "modalities": [] }),
json!({ "id": "acme-vision", "vision": true, "modalities": [] }),
json!({ "id": "acme-vision", "vision": true, "capabilities": { "modalities": [] } }),
json!({ "id": "acme-vision", "vision": true }),
] {
assert_eq!(
read(&declaring, descriptor.clone()),
["text", "image", "audio"],
"{descriptor}"
);
}
let declared = declaring
.project(
&provider,
&json!({ "id": "acme-vision", "vision": true, "modalities": ["text"] }),
)
.unwrap();
assert_eq!(declared.capabilities.modalities, ["text"]);
assert!(declared.capabilities.vision);

// No row declares the model, so `vision` is the only statement left.
let undeclared = catalog(json!([]));
for descriptor in [
json!({ "id": "zz-vision", "vision": true, "modalities": [] }),
json!({ "id": "zz-vision", "vision": true }),
json!({ "id": "zz-vision", "vision": true, "capabilities": { "modalities": [] } }),
] {
assert_eq!(
read(&undeclared, descriptor.clone()),
["text", "image"],
"{descriptor}"
);
}
for descriptor in [
json!({ "id": "zz-text", "vision": false, "modalities": [] }),
json!({ "id": "zz-text", "vision": false }),
] {
assert_eq!(
read(&undeclared, descriptor.clone()),
["text"],
"{descriptor}"
);
}
}

/// The shipped `deepseek-v4.1-flash` row declares image input, and the live
/// descriptor that reported the empty array is the account catalog's own
/// listing: the model the catalog snapshot serves keeps the row's set (#1980).
#[test]
fn a_live_empty_modalities_array_keeps_the_shipped_deepseek_declaration() {
let catalog = BundledCatalog::load().unwrap();
let provider = ProviderProjection {
provider_id: "dimcode-api-oauth".into(),
..Default::default()
};
let model = catalog
.project(
&provider,
&json!({ "id": "deepseek-v4.1-flash", "vision": true, "modalities": [] }),
)
.unwrap();
assert!(model.capabilities.vision);
assert_eq!(model.capabilities.modalities, ["text", "image"]);
assert!(
model.capabilities.missing_limits().is_empty(),
"the row the id identifies declares its limits, so the set is the row's"
);
}

#[test]
fn visible_edits_preserve_hidden_fields_and_update_legacy_media_readers() {
let catalog = BundledCatalog::load().unwrap();
Expand Down
30 changes: 30 additions & 0 deletions crates/dim-app-server/tests/provider_flow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1557,6 +1557,36 @@ async fn a_listings_reasoning_levels_become_the_models_efforts() {
listing.close().await;
}

/// A listing that writes an empty `modalities.input` states nothing about the
/// model's input, so the row the id identifies keeps its declaration instead
/// of the empty array overwriting it (#1980).
#[tokio::test]
async fn an_empty_listing_modalities_input_keeps_the_identified_rows_declaration() {
let listing = ServedListing::refreshed(json!([
{"id":"gpt-5.5","modalities":{"input":[]}},
{"id":"zzzz-unlisted","modalities":{"input":[]}},
{"id":"zzzz-listed-text","modalities":{"input":["text"]}}
]))
.await;
let model = |id: &str| {
listing
.catalog
.models
.iter()
.find(|model| model.id == id)
.unwrap()
};
assert!(model("gpt-5.5").capabilities.vision);
assert_eq!(model("gpt-5.5").capabilities.modalities, ["text", "image"]);
// No row declares this one, so it reads as the text-only model it is.
assert_eq!(model("zzzz-unlisted").capabilities.modalities, ["text"]);
assert!(!model("zzzz-unlisted").capabilities.vision);
// An explicit non-empty array stays authoritative.
assert_eq!(model("zzzz-listed-text").capabilities.modalities, ["text"]);
assert!(!model("zzzz-listed-text").capabilities.vision);
listing.close().await;
}

/// A listing's `supported_reasoning_levels` is the model's effort list the
/// same way, from each item's `effort`.
#[tokio::test]
Expand Down
28 changes: 15 additions & 13 deletions crates/dim-oauth/src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ use crate::http::{Body, MAX_SAFE_INTEGER, text};
use dim_agent_capabilities::host::ModelProtocol;
use dim_app_server::{
managed_provider::{ManagedError, ManagedModel},
model_catalog::{BundledCatalog, ProviderProjection, kind_of},
model_catalog::{
BundledCatalog, ProviderProjection, declared_modalities, effective_modalities, kind_of,
},
};
use regex::Regex;
use serde_json::{Map, Value, json};
Expand Down Expand Up @@ -401,6 +403,10 @@ fn project(
.or(safe_positive(limit.get("output")))
.or(safe_positive(declared.get("maxOutputTokens")));
let found_caps = found.as_ref().map(|f| object(f.get("capabilities")));
let bundled_modalities = found_caps
.as_ref()
.map(|caps| strings(caps.get("modalities")))
.unwrap_or_default();
let mut caps = found_caps.clone().unwrap_or_else(|| {
object(Some(
&json!({"modalities":["text"],"toolCalling":true,"streaming":true,
Expand Down Expand Up @@ -431,24 +437,20 @@ fn project(
caps.insert(key.into(), json!(value));
}
}
if declared.contains_key("modalities") {
caps.insert(
"modalities".into(),
json!(strings(declared.get("modalities"))),
);
}
if input.contains_key("modalities") {
caps.insert("modalities".into(), json!(strings(input.get("modalities"))));
}
if let Some(Value::Bool(tool_calling)) = input.get("toolCalling") {
caps.insert("toolCalling".into(), json!(tool_calling));
}
if let Some(Value::Bool(vision)) = input.get("vision") {
caps.insert("vision".into(), json!(vision));
if *vision && !input.contains_key("modalities") && !declared.contains_key("modalities") {
caps.insert("modalities".into(), json!(["text", "image"]));
}
}
caps.insert(
"modalities".into(),
json!(effective_modalities(
declared_modalities(input),
&bundled_modalities,
input.get("vision").and_then(Value::as_bool),
)),
);
if let Some(r) = &r {
caps.insert(
"reasoning".into(),
Expand Down
48 changes: 48 additions & 0 deletions crates/dim-oauth/tests/dim_oauth_flow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1223,6 +1223,54 @@ async fn a_relay_model_without_declared_limits_is_published_as_undeclared() {
std::fs::remove_dir_all(home).unwrap();
}

/// The relay's own descriptors, projected through the account catalog's
/// boundary: an empty `modalities` array states nothing, so the bundled row's
/// set applies and `vision` decides only for a model no row declares (#1980).
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn an_empty_relay_modalities_array_is_the_undeclared_value() {
let fixture = oauth::start().await;
*fixture.service.models.lock().unwrap() = Some(json!({"data":[
{"id":"deepseek-v4.1-flash","dim":{"id":"deepseek-v4.1-flash","endpoint_type":"openai",
"vision":true,"modalities":[],"contextWindow":1048576,"maxOutputTokens":65536}},
{"id":"gemini-2.5-flash","dim":{"id":"gemini-2.5-flash","endpoint_type":"openai",
"vision":true,"modalities":[],"contextWindow":1048576,"maxOutputTokens":65536}},
{"id":"zzzz-relay-vision","dim":{"id":"zzzz-relay-vision","endpoint_type":"openai",
"vision":true,"modalities":[],"contextWindow":64000,"maxOutputTokens":8000}},
{"id":"zzzz-relay-text","dim":{"id":"zzzz-relay-text","endpoint_type":"openai",
"vision":false,"modalities":[],"contextWindow":64000,"maxOutputTokens":8000}},
{"id":"zzzz-relay-declared","dim":{"id":"zzzz-relay-declared","endpoint_type":"openai",
"vision":true,"modalities":["text"],"contextWindow":64000,"maxOutputTokens":8000}}
]}));
let home = home();
let Process { root, providers } = process(&home).await;
login(&providers, &fixture.base).await;
let catalog = providers.catalog(PROVIDER, &cancel()).await.unwrap();
let modalities = |id: &str| {
catalog
.models
.iter()
.find(|model| model.entry.model_id == id)
.unwrap()
.entry
.capabilities
.modalities
.clone()
};
// The relay's empty array does not overwrite the shipped declaration.
assert_eq!(modalities("deepseek-v4.1-flash"), ["text", "image"]);
assert_eq!(
modalities("gemini-2.5-flash"),
["text", "image", "audio", "video"]
);
// No row declares these, so `vision` is the statement that decides.
assert_eq!(modalities("zzzz-relay-vision"), ["text", "image"]);
assert_eq!(modalities("zzzz-relay-text"), ["text"]);
// An explicit non-empty array stays authoritative.
assert_eq!(modalities("zzzz-relay-declared"), ["text"]);
root.shutdown().await.unwrap();
std::fs::remove_dir_all(home).unwrap();
}

/// The distinct model ids the shipped catalog lists, in catalog order. A case
/// picks the id it asserts on from here by property, never by name.
fn listed_ids(catalog: &BundledCatalog) -> Vec<String> {
Expand Down
10 changes: 10 additions & 0 deletions docs/design/appserver/provider-management.md
Original file line number Diff line number Diff line change
Expand Up @@ -994,6 +994,16 @@ declare the limit itself. Probe, refresh and creation store only declared values
and `models.reset` returns a model to its discovery result; when that result
declares nothing, the model is undeclared again.

A model's input modalities are resolved by one rule, whichever source the
descriptor comes from: a listing, a user's descriptor, or the managed account
catalog's own listing. An empty `modalities` array states nothing, so the
identified row's non-empty set applies and the array never narrows a declaration
the row makes. A descriptor that states no modalities and identifies no row takes
`["text", "image"]` when it states `vision: true` and `["text"]` otherwise. An
explicit non-empty array is the descriptor's own statement and stays. The rule
covers the nested `capabilities.modalities` array, and a listing's
`modalities.input` array states no `vision` flag either when it is empty (#1980).

Format v24 stored what a client supplied without a mark, and its probe filled
each missing field with the assumed pair (128000 context, 16384 output). The
format upgrade and both legacy importers therefore decide each field of a user
Expand Down
Loading
Loading