Skip to content

JiuwenSwarm is an intelligent AI Agent built on openJiuwen. It extends the powerful capabilities of large language models directly to your fingertips through various communication apps you use daily.

Python

fix: support switch same name model

huanggawae95a6db93ae7
diff --git a/jiuwenswarm/channels/tui/frontend/src/ui/app-screen.ts b/jiuwenswarm/channels/tui/frontend/src/ui/app-screen.ts
index 69bef31b77..c2138dac36 100644
--- a/jiuwenswarm/channels/tui/frontend/src/ui/app-screen.ts
+++ b/jiuwenswarm/channels/tui/frontend/src/ui/app-screen.ts
@@ -2130,7 +2130,11 @@ export class AppScreen implements Component, Focusable {
}
const skipped = models.filter((m) => isReservedMultimodalModelKey(m));
- const selectable = models.filter((m) => !isReservedMultimodalModelKey(m));
+ // 构建 selectable 时保留在完整 models 列表中的原始索引,避免 reserved 模型过滤后索引错位
+ const selectableWithOrigIdx = models
+ .map((m, i) => ({ name: m, origIdx: i }))
+ .filter((entry) => !isReservedMultimodalModelKey(entry.name));
+ const selectable = selectableWithOrigIdx.map((entry) => entry.name);
if (skipped.length > 0) {
this.state.addItem(
addInfo(
@@ -2151,8 +2155,9 @@ export class AppScreen implements Component, Focusable {
// 后端 available_models 和 models 位置对齐,用索引匹配而非名称查找
const nameOccurrence: Record<string, number> = {};
const currentIdx = selectable.findIndex((m) => m === current);
- const items = selectable.map((m, i) => {
- const meta = modelsMeta[i];
+ const items = selectableWithOrigIdx.map((entry, i) => {
+ const m = entry.name;
+ const meta = modelsMeta[entry.origIdx];
const isCurrent = i === currentIdx;
const seq = (nameOccurrence[m] ?? 0) + 1;
nameOccurrence[m] = seq;
@@ -2170,7 +2175,7 @@ export class AppScreen implements Component, Focusable {
const suffix = meta?.api_base && sameNameTotal > 1 ? ` [${meta.api_base}]` : "";
return {
label: `${i + 1}. ${displayName}${suffix}${isCurrent ? " (current)" : ""}`,
- value: m,
+ value: `${m}\x00${entry.origIdx}`,
};
});
const list = new SelectList(items, Math.min(Math.max(items.length, 1), 8), selectListTheme, {
@@ -2272,10 +2277,15 @@ export class AppScreen implements Component, Focusable {
}
}
- private async handleModelSelection(modelName: string): Promise<void> {
- if (!modelName) {
+ private async handleModelSelection(modelValue: string): Promise<void> {
+ if (!modelValue) {
return;
}
+ // Parse "modelName\x00origIdx" format from SelectList (null separator avoids collision with model names)
+ const sepIdx = modelValue.indexOf("\x00");
+ const modelName = sepIdx >= 0 ? modelValue.substring(0, sepIdx) : modelValue;
+ const modelIndex = sepIdx >= 0 ? parseInt(modelValue.substring(sepIdx + 1), 10) : undefined;
+
if (isReservedMultimodalModelKey(modelName)) {
this.modelList = null;
this.state.addItem(
@@ -2289,11 +2299,15 @@ export class AppScreen implements Component, Focusable {
}
this.modelList = null;
try {
+ const reqParams: Record<string, unknown> = { model: modelName };
+ if (modelIndex !== undefined && !isNaN(modelIndex)) {
+ reqParams.index = modelIndex;
+ }
const payload = await this.state.request<{
current?: string;
requested?: string;
applied?: boolean;
- }>("command.model", { model: modelName });
+ }>("command.model", reqParams);
const nextModel = payload.current ?? modelName;
this.state.setModel(nextModel);
this.state.addItem(
diff --git a/jiuwenswarm/gateway/channel_manager/tui/tui_connect.py b/jiuwenswarm/gateway/channel_manager/tui/tui_connect.py
index 27c4487a70..96085e3970 100644
--- a/jiuwenswarm/gateway/channel_manager/tui/tui_connect.py
+++ b/jiuwenswarm/gateway/channel_manager/tui/tui_connect.py
@@ -1671,6 +1671,7 @@ def register_cli_handlers(bind: CliHandlersBindParams) -> None:
params = {}
action = params.get("action")
model_name = params.get("model")
+ model_index = params.get("index")
real_client = (
agent_client.get("value")
@@ -1881,7 +1882,9 @@ def register_cli_handlers(bind: CliHandlersBindParams) -> None:
_valid_names.add(_al)
if not _valid_names:
_valid_names = set(get_model_names())
- if target not in _valid_names:
+ # 当有 model_index 时跳过名称验证(前端已通过列表选择,索引即可信)
+ _skip_name_check = model_index is not None
+ if not _skip_name_check and target not in _valid_names:
logger.warning(
"[cli command.model] 模型不存在: %s, 可用: %s",
target,
@@ -1912,15 +1915,28 @@ def register_cli_handlers(bind: CliHandlersBindParams) -> None:
_raw_defaults = ensure_defaults_list_in_config()
_target_entry = None
_target_idx = None
- for _i, _e in enumerate(_raw_defaults):
- if not isinstance(_e, dict):
- continue
- _ename = resolve_env_vars(str((_e.get("model_client_config") or {}).get("model_name", "")))
- _ealias = resolve_env_vars(str(_e.get("alias", ""))) if _e.get("alias") else ""
- if _ename == target or _ealias == target:
- _target_entry = _e
- _target_idx = _i
- break
+
+ # 如果前端传了 index,直接按索引定位(支持同名模型区分)
+ if model_index is not None:
+ try:
+ _idx = int(model_index)
+ if 0 <= _idx < len(_raw_defaults) and isinstance(_raw_defaults[_idx], dict):
+ _target_entry = _raw_defaults[_idx]
+ _target_idx = _idx
+ except (ValueError, TypeError):
+ pass
+
+ # 回退到按名称/alias查找
+ if _target_entry is None:
+ for _i, _e in enumerate(_raw_defaults):
+ if not isinstance(_e, dict):
+ continue
+ _ename = resolve_env_vars(str((_e.get("model_client_config") or {}).get("model_name", "")))
+ _ealias = resolve_env_vars(str(_e.get("alias", ""))) if _e.get("alias") else ""
+ if _ename == target or _ealias == target:
+ _target_entry = _e
+ _target_idx = _i
+ break
_other_entries = [_e for _i, _e in enumerate(_raw_defaults) if _i != _target_idx]
if _target_entry is None:
await channel.send_response(ws, req_id, ok=False, error=f"Model '{target}' config not found")