Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -336,31 +336,20 @@ fn model_selection_view(
let mut items = Vec::new();
let mut body = String::new();

// Option 1: configured primary model.
let primary_is_current = current_model_id
// An empty or absent selection follows the primary model.
let current = current_model_id
.as_deref()
.map(|m| m.is_empty() || m == "primary")
.unwrap_or(true);
let primary_marker = if primary_is_current {
s.current_marker
} else {
""
};
body.push_str(&format!(
"1. {}{}\n",
s.switch_model_primary, primary_marker
));
items.push(MenuItem::default(s.switch_model_primary, "primary"));

// Remaining options: each enabled model.
for (i, (model_id, model_name)) in options.iter().enumerate() {
let is_current = current_model_id
.as_deref()
.is_some_and(|m| m == model_id.as_str());
let marker = if is_current { s.current_marker } else { "" };
body.push_str(&format!("{}. {}{}\n", i + 2, model_name, marker));
.filter(|m| !m.is_empty())
.unwrap_or("primary");
for (i, (model_id, label)) in options.iter().enumerate() {
let marker = if current == model_id.as_str() {
s.current_marker
} else {
""
};
body.push_str(&format!("{}. {}{}\n", i + 1, label, marker));
items.push(MenuItem::default(
truncate_label(model_name, 24),
truncate_label(label, 24),
model_id.clone(),
));
}
Expand All @@ -372,6 +361,62 @@ fn model_selection_view(
.with_footer(s.footer_reply_model)
}

fn model_display_name(
model: &crate::service::remote_connect::remote_server::RemoteModelConfig,
) -> String {
format!("{} · {}", model.model_name, model.name)
}

/// Labels a `primary`/`fast` alias with the configured model it resolves to,
/// so a chat user sees which model the alias actually runs.
fn model_alias_label(
alias: &str,
target: Option<&crate::service::remote_connect::remote_server::RemoteModelConfig>,
s: &'static BotStrings,
) -> String {
let model = target
.map(model_display_name)
.unwrap_or_else(|| s.switch_model_not_configured.to_string());
s.switch_model_alias_template
.replace("{alias}", alias)
.replace("{model}", &model)
}

/// Menu entries for a catalog: the primary alias, the fast alias when it is
/// configured, then every enabled model.
fn model_selection_options(
catalog: &crate::service::remote_connect::remote_server::RemoteModelCatalog,
s: &'static BotStrings,
) -> Vec<(String, String)> {
let resolve = |id: &Option<String>| {
id.as_deref()
.filter(|id| !id.is_empty())
.and_then(|id| catalog.models.iter().find(|m| m.id == id))
};
let mut options = vec![(
"primary".to_string(),
model_alias_label(
s.switch_model_primary,
resolve(&catalog.default_models.primary),
s,
),
)];
if let Some(fast) = resolve(&catalog.default_models.fast) {
options.push((
"fast".to_string(),
model_alias_label(s.switch_model_fast, Some(fast), s),
));
}
options.extend(
catalog
.models
.iter()
.filter(|m| m.enabled)
.map(|m| (m.id.clone(), model_display_name(m))),
);
options
}

async fn start_switch_model(state: &mut BotChatState, s: &'static BotStrings) -> HandleResult {
use crate::service_agent_runtime::CoreServiceAgentRuntime;
let session_id = match state.current_session_id.clone() {
Expand Down Expand Up @@ -406,24 +451,21 @@ async fn start_switch_model(state: &mut BotChatState, s: &'static BotStrings) ->
}
};

// Collect enabled models as (id, "name · provider") for the selection list.
let options: Vec<(String, String)> = catalog
.models
.iter()
.filter(|m| m.enabled)
.map(|m| (m.id.clone(), format!("{} · {}", m.model_name, m.name)))
.collect();

if options.is_empty() {
if !catalog.models.iter().any(|m| m.enabled) {
return result_from_menu(
state,
MenuView::plain(s.switch_model_no_models)
.with_items(vec![MenuItem::default(s.item_back, "/menu")]),
);
}

let view = model_selection_view(&catalog.session_model_id, &options, s);
state.set_pending(PendingAction::SelectModel { options });
let options = model_selection_options(&catalog, s);
let current_model_id = catalog.session_model_id;
let view = model_selection_view(&current_model_id, &options, s);
state.set_pending(PendingAction::SelectModel {
options,
current_model_id,
});
result_from_menu(state, view)
}

Expand Down Expand Up @@ -2895,24 +2937,26 @@ async fn route_pending(
)
.await
}
PendingAction::SelectModel { options } => {
PendingAction::SelectModel {
options,
current_model_id,
} => {
let parsed: Option<usize> = raw_input.parse().ok();
match parsed {
Some(0) => {
state.clear_pending();
menu_or_welcome(state, s)
}
Some(1) => {
state.clear_pending();
select_model(state, "primary", s.switch_model_primary, s).await
}
Some(n) if n >= 2 && n <= options.len() + 1 => {
Some(n) if n >= 1 && n <= options.len() => {
state.clear_pending();
let (model_id, model_name) = options[n - 2].clone();
let (model_id, model_name) = options[n - 1].clone();
select_model(state, &model_id, &model_name, s).await
}
_ => {
state.set_pending(PendingAction::SelectModel { options });
state.set_pending(PendingAction::SelectModel {
options,
current_model_id,
});
Box::pin(pending_invalid(state, s)).await
}
}
Expand Down Expand Up @@ -3021,7 +3065,10 @@ async fn pending_invalid(state: &mut BotChatState, s: &'static BotStrings) -> Ha
awaiting_custom_text,
..
} => build_question_view(s, questions, *current_index, *awaiting_custom_text),
PendingAction::SelectModel { options } => model_selection_view(&None, options, s),
PendingAction::SelectModel {
options,
current_model_id,
} => model_selection_view(current_model_id, options, s),
PendingAction::ConfirmModeSwitch { target_mode, .. } => {
confirm_mode_switch_view(*target_mode, s)
}
Expand Down Expand Up @@ -5541,12 +5588,19 @@ mod remote_device_flow_tests {
let text = result.menu.render_plain_text(BotLanguage::EnUS);
assert!(text.contains("alpha · Provider A"), "{text}");
assert!(!text.contains("off · Provider B"), "{text}");
let Some(PendingAction::SelectModel { options }) = state.pending_action.clone() else {
assert!(
text.contains("1. Primary model (alpha · Provider A)"),
"the primary alias names the model it runs: {text}"
);
assert!(
!text.contains("Fast model"),
"unconfigured fast is hidden: {text}"
);
let Some(pending @ PendingAction::SelectModel { .. }) = state.pending_action.clone() else {
panic!("expected a model selection");
};

let result =
route_pending(&mut state, PendingAction::SelectModel { options }, "2", s).await;
let result = route_pending(&mut state, pending, "2", s).await;
assert!(
result.reply.contains(s.switch_model_applied_prefix),
"{}",
Expand All @@ -5563,6 +5617,62 @@ mod remote_device_flow_tests {
assert!(update.get("reasoning_preset").is_none(), "{update}");
}

fn catalog_with_defaults(
primary: Option<&str>,
fast: Option<&str>,
session_model_id: Option<&str>,
) -> crate::service::remote_connect::remote_server::RemoteModelCatalog {
serde_json::from_value(serde_json::json!({
"version":1,
"models":[
{"id":"m-a","name":"Provider A","provider":"openai","base_url":"https://a","model_name":"alpha","enabled":true,"capabilities":["text_chat"]},
{"id":"m-b","name":"Provider B","provider":"openai","base_url":"https://b","model_name":"beta","enabled":true,"capabilities":["text_chat"]}
],
"default_models":{"primary":primary,"fast":fast,"search":null,"image_understanding":null,"image_generation":null,"speech_recognition":null},
"session_model_id":session_model_id
}))
.unwrap()
}

#[test]
fn model_menu_names_the_models_behind_primary_and_fast() {
let s = strings_for(BotLanguage::ZhCN);
let catalog = catalog_with_defaults(Some("m-a"), Some("m-b"), Some("fast"));
let options = model_selection_options(&catalog, s);
let view = model_selection_view(&catalog.session_model_id, &options, s);
let text = view.render_plain_text(BotLanguage::ZhCN);
assert!(
text.contains("1. 主力模型(alpha · Provider A)\n"),
"{text}"
);
assert!(
text.contains(&format!(
"2. 快速模型(beta · Provider B){}",
s.current_marker
)),
"{text}"
);
assert!(text.contains("3. alpha · Provider A\n"), "{text}");
assert_eq!(options[1].0, "fast");
}

#[test]
fn model_menu_flags_an_unconfigured_primary_model() {
let s = strings_for(BotLanguage::EnUS);
let catalog = catalog_with_defaults(None, None, None);
let options = model_selection_options(&catalog, s);
let text = model_selection_view(&catalog.session_model_id, &options, s)
.render_plain_text(BotLanguage::EnUS);
assert!(
text.contains(&format!(
"1. Primary model (not configured){}",
s.current_marker
)),
"{text}"
);
assert_eq!(options.len(), 3, "no fast entry without a fast model");
}

#[tokio::test]
async fn remote_assistant_mode_selects_resumes_and_creates_in_device_assistants() {
let (relay_url, secret, calls) = spawn_device_fixture(Arc::new(device_response)).await;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,11 @@ pub struct BotStrings {
pub switch_model_pick: &'static str,
pub switch_model_no_models: &'static str,
pub switch_model_primary: &'static str,
pub switch_model_fast: &'static str,
/// Shown in place of a model name when an alias points at no configured model.
pub switch_model_not_configured: &'static str,
/// Renders an alias with the model it resolves to; `{alias}` and `{model}` are replaced.
pub switch_model_alias_template: &'static str,
pub switch_model_applied_prefix: &'static str,
pub switch_model_failed_prefix: &'static str,
pub switch_model_no_session: &'static str,
Expand Down Expand Up @@ -291,6 +296,9 @@ const STRINGS_ZH: BotStrings = BotStrings {
switch_model_pick: "请选择要使用的模型:",
switch_model_no_models: "没有可用的模型,请先在 OpenBitFun 桌面端配置 AI 模型。",
switch_model_primary: "主力模型",
switch_model_fast: "快速模型",
switch_model_not_configured: "未配置",
switch_model_alias_template: "{alias}({model})",
switch_model_applied_prefix: "已切换模型:",
switch_model_failed_prefix: "切换模型失败:",
switch_model_no_session: "当前没有活跃会话,请先新建或恢复会话。",
Expand Down Expand Up @@ -459,6 +467,9 @@ const STRINGS_ZH_TW: BotStrings = BotStrings {
switch_model_pick: "請選擇要使用的模型:",
switch_model_no_models: "沒有可用的模型,請先在 OpenBitFun 桌面端配置 AI 模型。",
switch_model_primary: "主力模型",
switch_model_fast: "快速模型",
switch_model_not_configured: "未設定",
switch_model_alias_template: "{alias}({model})",
switch_model_applied_prefix: "已切換模型:",
switch_model_failed_prefix: "切換模型失敗:",
switch_model_no_session: "當前沒有活躍會話,請先新建或恢復會話。",
Expand Down Expand Up @@ -628,6 +639,9 @@ Open Remote Connect in OpenBitFun Desktop and send the 6-digit pairing code here
switch_model_pick: "Pick a model to use:",
switch_model_no_models: "No models available. Please configure AI models in OpenBitFun Desktop first.",
switch_model_primary: "Primary model",
switch_model_fast: "Fast model",
switch_model_not_configured: "not configured",
switch_model_alias_template: "{alias} ({model})",
switch_model_applied_prefix: "Switched model: ",
switch_model_failed_prefix: "Failed to switch model: ",
switch_model_no_session: "No active session. Create or resume a session first.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,11 @@ pub enum PendingAction {
has_more: bool,
},
SelectModel {
/// `(model selection, display label)` in menu order: the `primary`
/// alias first, then `fast` when configured, then concrete models.
options: Vec<(String, String)>,
/// The session's selection when the menu opened, for re-rendering.
current_model_id: Option<String>,
},
AskUserQuestion {
tool_id: String,
Expand Down
Loading