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:/model中添加同名model,并切换时,model后面的序号区分
openJiuwen-botc68f2097d51c
diff --git a/jiuwenswarm/channels/tui/frontend/src/core/commands/builtins/model.ts b/jiuwenswarm/channels/tui/frontend/src/core/commands/builtins/model.tsindex cd750c867d..afbbd0f006 100644--- a/jiuwenswarm/channels/tui/frontend/src/core/commands/builtins/model.ts+++ b/jiuwenswarm/channels/tui/frontend/src/core/commands/builtins/model.ts@@ -6,6 +6,8 @@ export interface ModelMeta {model_name?: string;client_provider?: string;api_base?: string;+ api_key_prefix?: string;+ is_current?: boolean;}export interface ModelListPayload {@@ -107,15 +109,17 @@ export function createModelCommand(): SlashCommand {return;}const modelsMeta = payload.models ?? [];- // 与web端一致:同名模型通过编号区分(如 model_name #1, model_name #2)- // 后端 available_models 和 models 位置对齐,用索引匹配而非名称查找+ // 优先用后端 is_current 标记判断当前模型(同名模型仅靠名字无法区分),+ // 回退到 name-matching(兼容不带 is_current 的旧后端)+ const currentIdx = selectable.findIndex((m, i) => {+ const meta = modelsMeta[i];+ return meta?.is_current === true;+ });+ const fallbackCurrentIdx = currentIdx < 0 ? selectable.findIndex((m) => m === current) : currentIdx;const nameOccurrence: Record<string, number> = {};- // current 是defaults首位的display name,对应selectable中第一个未被reserved过滤的条目- // 当同名模型有多条时,只有名字等于current且是selectable中首次出现的条目才是current- const currentIdx = selectable.findIndex((m) => m === current);const items = selectable.map((m, i) => {const meta = modelsMeta[i];- const isCurrent = i === currentIdx;+ const isCurrent = i === fallbackCurrentIdx;// 统计同名出现次序const seq = (nameOccurrence[m] ?? 0) + 1;nameOccurrence[m] = seq;@@ -130,7 +134,15 @@ export function createModelCommand(): SlashCommand {} else {displayName = m;}- const suffix = meta?.api_base && sameNameTotal > 1 ? ` [${meta.api_base}]` : "";+ // 同名模型显示 api_key_prefix + api_base 作为区分标识+ const suffixParts: string[] = [];+ if (sameNameTotal > 1 && meta?.api_key_prefix) {+ suffixParts.push(`key:${meta.api_key_prefix}…`);+ }+ if (sameNameTotal > 1 && meta?.api_base) {+ suffixParts.push(meta.api_base);+ }+ const suffix = suffixParts.length > 0 ? ` [${suffixParts.join(" | ")}]` : "";return {label: String(i + 1),value: `${displayName}${suffix}${isCurrent ? " (current)" : ""}`,diff --git a/jiuwenswarm/channels/tui/frontend/src/ui/app-screen.ts b/jiuwenswarm/channels/tui/frontend/src/ui/app-screen.tsindex 0481fc4cfc..f8ee2a9d83 100644--- a/jiuwenswarm/channels/tui/frontend/src/ui/app-screen.ts+++ b/jiuwenswarm/channels/tui/frontend/src/ui/app-screen.ts@@ -3343,14 +3343,18 @@ export class AppScreen implements Component, Focusable {}const modelsMeta = payload.models ?? [];- // 与web端一致:同名模型通过编号区分(如 model_name #1, model_name #2)- // 后端 available_models 和 models 位置对齐,用索引匹配而非名称查找+ // 优先用后端 is_current 标记判断当前模型(同名模型仅靠名字无法区分),+ // 回退到 name-matching(兼容不带 is_current 的旧后端)+ const currentIdx = selectableWithOrigIdx.findIndex((entry) => {+ const meta = modelsMeta[entry.origIdx];+ return meta?.is_current === true;+ });+ const fallbackCurrentIdx = currentIdx < 0 ? selectable.findIndex((m) => m === current) : currentIdx;const nameOccurrence: Record<string, number> = {};- const currentIdx = selectable.findIndex((m) => m === current);const items = selectableWithOrigIdx.map((entry, i) => {const m = entry.name;const meta = modelsMeta[entry.origIdx];- const isCurrent = i === currentIdx;+ const isCurrent = i === fallbackCurrentIdx;const seq = (nameOccurrence[m] ?? 0) + 1;nameOccurrence[m] = seq;const sameNameTotal = selectable.filter((x) => x === m).length;@@ -3364,7 +3368,15 @@ export class AppScreen implements Component, Focusable {} else {displayName = m;}- const suffix = meta?.api_base && sameNameTotal > 1 ? ` [${meta.api_base}]` : "";+ // 同名模型显示 api_key_prefix + api_base 作为区分标识+ const suffixParts: string[] = [];+ if (sameNameTotal > 1 && meta?.api_key_prefix) {+ suffixParts.push(`key:${meta.api_key_prefix}…`);+ }+ if (sameNameTotal > 1 && meta?.api_base) {+ suffixParts.push(meta.api_base);+ }+ const suffix = suffixParts.length > 0 ? ` [${suffixParts.join(" | ")}]` : "";return {label: `${i + 1}. ${displayName}${suffix}${isCurrent ? " (current)" : ""}`,value: `${m}\x00${entry.origIdx}`,diff --git a/jiuwenswarm/gateway/channel_manager/tui/tui_connect.py b/jiuwenswarm/gateway/channel_manager/tui/tui_connect.pyindex 3e0207d4e5..c9e587ea9d 100644--- a/jiuwenswarm/gateway/channel_manager/tui/tui_connect.py+++ b/jiuwenswarm/gateway/channel_manager/tui/tui_connect.py@@ -2151,8 +2151,18 @@ def register_cli_handlers(bind: CliHandlersBindParams) -> None:resolve_env_vars(str((e.get("model_client_config") or {}).get("model_name", ""))),"model_name": resolve_env_vars(str((e.get("model_client_config") or {}).get("model_name", ""))),"api_base": resolve_env_vars(str((e.get("model_client_config") or {}).get("api_base", ""))),+ "api_key_prefix": (+ resolve_env_vars(+ str((e.get("model_client_config") or {}).get("api_key", ""))+ )[:8]+ if resolve_env_vars(+ str((e.get("model_client_config") or {}).get("api_key", ""))+ )+ else ""+ ),+ "is_current": i == 0,}- for e in _defs if isinstance(e, dict)+ for i, e in enumerate(_defs) if isinstance(e, dict)]else:payload["current"] = os.getenv("MODEL_NAME", "unknown")@@ -2160,7 +2170,7 @@ def register_cli_handlers(bind: CliHandlersBindParams) -> None:returntarget = str(model_name).strip()- logger.info("[cli command.model] 切换模型: target=%s", target)+ logger.info("[cli command.model] 切换模型: target=%s, model_index=%s, params=%s", target, model_index, params)_raw_defs_check = (get_config_raw().get("models") or {}).get("defaults") or []_valid_names: set[str] = set()for _e in _raw_defs_check:@@ -2250,6 +2260,17 @@ def register_cli_handlers(bind: CliHandlersBindParams) -> None:error=f"Model '{target}' missing required config: {', '.join(_missing_fields)}",)return+ # 切换后确保目标条目 is_default=True,清除其他同名模型的 is_default+ # AgentServer 用 is_default=True 确定默认模型,defaults[0] 位置不够——同名模型+ # 需靠 is_default 标记来区分哪个是当前激活的+ _target_model_name_resolved = resolve_env_vars(str(_target_mcc.get("model_name", "")))+ _target_entry["is_default"] = True+ for _e in _other_entries:+ if isinstance(_e, dict):+ _other_mcc = _e.get("model_client_config") or {}+ _other_name = resolve_env_vars(str(_other_mcc.get("model_name", "")))+ if _other_name == _target_model_name_resolved and _e.get("is_default") is True:+ _e["is_default"] = Falseupdate_default_models_in_config([_target_entry] + _other_entries)logger.info("[cli command.model] 切换,已更新 models.defaults 首位: %s", target)_target_model_name = resolve_env_vars(