Skip to content

Commit bc8bc89

Browse files
authored
fix(cli-rs): keep the server's model order in the mini picker and ACP (#68247)
1 parent 3d60ba6 commit bc8bc89

7 files changed

Lines changed: 70 additions & 16 deletions

File tree

‎packages/cli-rs/crates/acp/src/config_option.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use opencode_api::generated::{ModelInfo, ModelRef, ProviderID};
2-
use opencode_api::js::locale_compare;
32
use opencode_util::json_object;
43
use serde_json::{Value, json};
54

@@ -38,8 +37,9 @@ pub fn config_options(catalog: &Catalog, selection: &Selection) -> Vec<Value> {
3837
let variants: Vec<String> = find_model(&catalog.models, &model)
3938
.map(|item| item.variants.iter().map(|variant| variant.id.clone()).collect())
4039
.unwrap_or_default();
40+
// Grouped by provider; inside a provider the server's order, newest first (deviation C11-acp-model-order).
4141
let mut models: Vec<&ModelInfo> = catalog.models.iter().collect();
42-
models.sort_by(|a, b| a.provider_id.0.cmp(&b.provider_id.0).then_with(|| locale_compare(&a.name, &b.name)));
42+
models.sort_by(|a, b| a.provider_id.0.cmp(&b.provider_id.0));
4343
let mut options = vec![json!({
4444
"id": "model",
4545
"name": "Model",

‎packages/cli-rs/crates/acp/tests/acp/catalog.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,8 @@ async fn answers_the_first_session_after_a_cold_location_activates_its_plugins()
167167
assert_eq!(
168168
model["options"],
169169
json!([
170-
{ "value": "test/second-model", "name": "Test Provider / Second Model" },
171170
{ "value": "test/test-model", "name": "Test Provider / Test Model" },
171+
{ "value": "test/second-model", "name": "Test Provider / Second Model" },
172172
])
173173
);
174174
assert_eq!(current_value(&selected, "model"), "test/second-model");

‎packages/cli-rs/crates/acp/tests/acp/config_option.rs‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ fn catalog() -> Catalog {
1919
Catalog {
2020
models: vec![
2121
model("anthropic", "claude/sonnet-4", "Claude Sonnet 4", &["default", "high", "very-high"]),
22-
model("anthropic", "claude-haiku", "Claude Haiku", &[]),
2322
model("openai", "gpt-5", "GPT-5", &["minimal", "low"]),
23+
model("anthropic", "claude-opus", "Claude Opus", &[]),
24+
model("anthropic", "claude-haiku", "Claude Haiku", &[]),
2425
model("test", "effort", "Effort", &["low", "default", "high"]),
2526
],
2627
default_model: model_ref("openai", "gpt-5", None),
@@ -32,15 +33,17 @@ fn catalog() -> Catalog {
3233
}
3334
}
3435

36+
// Grouped by provider ID; inside a provider, the server's order (newest first), not the names' order.
3537
#[test]
3638
fn labels_models_with_their_provider_display_name_or_id() {
3739
let model =
3840
config_options(&catalog(), &Selection::default()).into_iter().find(|option| option["id"] == "model").unwrap();
3941
assert_eq!(
4042
model["options"],
4143
json!([
42-
{ "value": "anthropic/claude-haiku", "name": "Anthropic / Claude Haiku" },
4344
{ "value": "anthropic/claude/sonnet-4", "name": "Anthropic / Claude Sonnet 4" },
45+
{ "value": "anthropic/claude-opus", "name": "Anthropic / Claude Opus" },
46+
{ "value": "anthropic/claude-haiku", "name": "Anthropic / Claude Haiku" },
4447
{ "value": "openai/gpt-5", "name": "openai / GPT-5" },
4548
{ "value": "test/effort", "name": "test / Effort" },
4649
])

‎packages/cli-rs/crates/mini/src/footers/mod.rs‎

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2019,8 +2019,9 @@ async fn lineage(client: &Client, session: &str, family: &HashSet<String>) -> Di
20192019
Discovery::Unrelated
20202020
}
20212021

2022-
/// `RunModelSelectBody`'s entries: every model but the deprecated, opencode first, then by provider and model name.
2023-
/// A query shows each model's provider in the footer.
2022+
/// `RunModelSelectBody`'s entries: every model but the deprecated, opencode first, then by provider name, and inside a
2023+
/// provider in the server's order, newest first (deviation M04-model-order). A query shows each model's provider in the
2024+
/// footer.
20242025
fn model_entries(models: &Models, query: String) -> Vec<Entry<Pick>> {
20252026
let current = models.current.as_ref();
20262027
let searching = !query.trim().is_empty();
@@ -2064,15 +2065,12 @@ fn model_entries(models: &Models, query: String) -> Vec<Entry<Pick>> {
20642065
})
20652066
.collect();
20662067
entries.sort_by(|(a_id, a), (b_id, b)| {
2067-
(a_id != "opencode")
2068-
.cmp(&(b_id != "opencode"))
2069-
.then_with(|| {
2070-
opencode_api::js::locale_compare(
2071-
a.item.category.as_deref().unwrap_or_default(),
2072-
b.item.category.as_deref().unwrap_or_default(),
2073-
)
2074-
})
2075-
.then_with(|| opencode_api::js::locale_compare(&a.item.display, &b.item.display))
2068+
(a_id != "opencode").cmp(&(b_id != "opencode")).then_with(|| {
2069+
opencode_api::js::locale_compare(
2070+
a.item.category.as_deref().unwrap_or_default(),
2071+
b.item.category.as_deref().unwrap_or_default(),
2072+
)
2073+
})
20762074
});
20772075
entries.into_iter().map(|(_, entry)| entry).collect()
20782076
}
@@ -2377,6 +2375,20 @@ mod tests {
23772375
assert_eq!(footers.blocker_status(), "");
23782376
}
23792377

2378+
/// Inside a provider the list keeps the server's order, whatever the titles.
2379+
#[test]
2380+
fn the_model_list_keeps_the_server_order_inside_a_provider() {
2381+
for order in [["GPT-6.2", "GPT-6", "GPT-5.6"], ["GPT-6", "GPT-5.6", "GPT-6.2"], ["GPT-5.6", "GPT-6.2", "GPT-6"]]
2382+
{
2383+
let models = order
2384+
.map(|name| (name.to_owned(), crate::variants::Model { name: name.to_owned(), ..Default::default() }));
2385+
let provider = Provider { id: "openai".to_owned(), name: "OpenAI".to_owned(), models: models.into() };
2386+
let entries =
2387+
model_entries(&Models { providers: Some(vec![provider]), ..Models::default() }, String::new());
2388+
assert_eq!(entries.iter().map(|entry| entry.item.display.as_str()).collect::<Vec<_>>(), order);
2389+
}
2390+
}
2391+
23802392
/// A read for the last session does not reach the session opened after it.
23812393
#[test]
23822394
fn a_read_of_the_last_session_is_dropped() {

‎packages/cli-rs/parity/acp/run.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ function messages(text: string) {
118118
return text
119119
.split("\n")
120120
.filter((line) => line.trim())
121+
.map(modelOrder)
121122
.map((line) =>
122123
// A background shell's ID, process and output directory are the server's own for each run.
123124
[
@@ -132,6 +133,20 @@ function messages(text: string) {
132133
)
133134
}
134135

136+
// TypeScript lists the `model` config option's models by name, Rust in the server's order inside each provider
137+
// (parity/deviations/C11-acp-model-order.md), so both sides compare that list sorted by value.
138+
function modelOrder(line: string) {
139+
if (!line.includes('"configOptions"')) return line
140+
return JSON.stringify(JSON.parse(line), (_, value) =>
141+
value?.id === "model" && Array.isArray(value.options)
142+
? {
143+
...value,
144+
options: value.options.toSorted((a: { value: string }, b: { value: string }) => (a.value < b.value ? -1 : 1)),
145+
}
146+
: value,
147+
)
148+
}
149+
135150
function compare(ts: Process[], rs: Process[]) {
136151
const count = ts.length === rs.length ? [] : [` process count differs: ts ${ts.length}, rs ${rs.length}`]
137152
return [
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# C11: the ACP model list keeps the server's order inside a provider
2+
3+
**TypeScript.** The `model` config option (`packages/cli/src/acp/config-option.ts`) lists the models by provider ID,
4+
and inside a provider by name with `localeCompare`. Names with version numbers sort by character, so GPT-5.6 comes
5+
before GPT-6 and GPT-6.2.
6+
7+
**Rust.** The models are grouped by provider ID the same way. Inside a provider they keep the order the server sends,
8+
newest release first (`GET /api/model` sorts by `time.released`, `packages/core/src/model.ts`).
9+
10+
**Reason.** The name sort puts older models above newer ones ("5.6 shouldn't appear before newer models"). The
11+
server already orders by release date, and the protocol promises it. P-WIRE (`parity/acp/run.ts`) compares the
12+
`model` option's list sorted by value on both sides, so the suites' transcripts still match.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# M04: mini's model list keeps the server's order inside a provider
2+
3+
**TypeScript.** `RunModelSelectBody` (`packages/tui/src/mini/footer.command.tsx`) lists opencode first, then the
4+
providers by name, and inside a provider the models by title with `localeCompare`. Titles with version numbers sort
5+
by character, so GPT-5.6 comes before GPT-6 and GPT-6.2.
6+
7+
**Rust.** Providers are grouped the same way. Inside a provider the models keep the order the server sends, newest
8+
release first (`GET /api/model` sorts by `time.released`, `packages/core/src/model.ts`).
9+
10+
**Reason.** The name sort puts older models above newer ones ("5.6 shouldn't appear before newer models"). The
11+
server already orders by release date, and the protocol promises it. The `M.4/models` scenario lists models whose
12+
server order and title order agree, so its screens match.

0 commit comments

Comments
 (0)