fix(settings): stop the chain editor from offering an already chosen model

This commit is contained in:
yaqi
2026-11-25 09:53:41 +08:00
parent 874ea33d76
commit dbebb33608
3 changed files with 60 additions and 2 deletions

View File

@@ -9,6 +9,7 @@ import { useSettings } from "@/features/settings/store/SettingsStore";
import { apiFetch, apiUrl } from "@/lib/api";
import {
availableChoices,
fallbackChoices,
sanitizeChain,
withoutActive,
@@ -118,7 +119,7 @@ export function FallbackChainEditor() {
setRows((current) => [...current, emptyRow()]);
setSaved(null);
}}
disabled={rows.length >= MAX_FALLBACKS}
disabled={rows.length >= MAX_FALLBACKS || choices.length <= rows.length}
>
<Plus className="mr-1 inline h-3.5 w-3.5" />
{t("Add fallback")}
@@ -148,7 +149,7 @@ export function FallbackChainEditor() {
}}
>
<option value="">{t("Choose a configured model")}</option>
{choices.map((choice) => (
{availableChoices(choices, rows, index).map((choice) => (
<option
key={`${choice.profileId}:${choice.modelId}`}
value={`${choice.profileId}:${choice.modelId}`}

View File

@@ -80,6 +80,34 @@ export function chainsEqual(a: FallbackEntry[], b: FallbackEntry[]): boolean {
);
}
/**
* Choices a single row may offer: everything except the pairs already taken by
* the *other* rows, so the same model cannot be picked twice.
*
* The row's own current value stays in the list even when it is duplicated
* elsewhere (a duplicate can only arrive from an older payload), otherwise the
* select would show an empty value the user cannot recognise.
*/
export function availableChoices(
choices: FallbackChoice[],
rows: FallbackEntry[],
rowIndex: number,
): FallbackChoice[] {
const own = rows[rowIndex];
const ownKey = own ? `${own.profile_id}\u0000${own.model_id}` : "";
const taken = new Set(
rows
.filter((_, index) => index !== rowIndex)
.map((row) => choiceKey(row)),
);
return choices.filter((choice) => {
const key = `${choice.profileId}\u0000${choice.modelId}`;
// The row's own value stays selectable even when another row duplicates it,
// so an older payload renders as a chosen option instead of a blank select.
return key === ownKey || !taken.has(key);
});
}
/**
* The chain with the active model removed: retrying the model that just failed
* as its own fallback would only waste the cooldown.

View File

@@ -1,6 +1,7 @@
import { describe, expect, it } from "vitest";
import {
availableChoices,
fallbackChoices,
chainsEqual,
sanitizeChain,
@@ -30,6 +31,8 @@ const entry = (profile: string, model: string): FallbackEntry => ({
model_id: model,
});
const emptyRow = (): FallbackEntry => ({ profile_id: "", model_id: "" });
describe("fallbackChoices", () => {
it("offers every configured model with a readable label", () => {
expect(fallbackChoices(PROFILES)).toEqual([
@@ -80,6 +83,32 @@ describe("sanitizeChain", () => {
});
});
describe("availableChoices", () => {
const choices = fallbackChoices(PROFILES);
it("hides pairs already chosen by the other rows", () => {
const rows = [entry("deepseek", "ds-chat"), emptyRow()];
expect(availableChoices(choices, rows, 1)).toEqual([
{
profileId: "deepseek",
modelId: "ds-reasoner",
label: "DeepSeek / deepseek-reasoner",
},
{ profileId: "local", modelId: "local-qwen", label: "Local vLLM / qwen3-32b" },
]);
});
it("keeps the row's own value even if another row duplicates it", () => {
// Only an older payload can hold a duplicate; the select must still show it.
const rows = [entry("deepseek", "ds-chat"), entry("deepseek", "ds-chat")];
expect(availableChoices(choices, rows, 1).map((choice) => choice.modelId)).toContain(
"ds-chat",
);
});
});
describe("withoutActive", () => {
it("removes the active model so it is not its own fallback", () => {
expect(