mirror of
https://github.com/HKUDS/DeepTutor.git
synced 2026-12-04 21:59:31 +08:00
123 lines
4.1 KiB
TypeScript
123 lines
4.1 KiB
TypeScript
/**
|
|
* Pure helpers for the fallback-chain editor (#2693 P3).
|
|
*
|
|
* The chain is one ordered list on `services.llm`, pointing at profiles and
|
|
* models that already exist in the catalog — the editor never creates them.
|
|
*/
|
|
|
|
export type FallbackEntry = { profile_id: string; model_id: string };
|
|
|
|
export type FallbackChoice = {
|
|
/** `profile_id` used by the payload. */
|
|
profileId: string;
|
|
/** `models[].id` used by the payload. */
|
|
modelId: string;
|
|
/** Human label shown in the selects. */
|
|
label: string;
|
|
};
|
|
|
|
type ProfileLike = {
|
|
id: string;
|
|
name?: string;
|
|
display_name?: string;
|
|
models?: { id: string; name?: string; model?: string }[];
|
|
};
|
|
|
|
/** Every selectable (profile, model) pair in one flat, ordered list. */
|
|
export function fallbackChoices(profiles: ProfileLike[]): FallbackChoice[] {
|
|
const choices: FallbackChoice[] = [];
|
|
for (const profile of profiles) {
|
|
const profileName = profile.display_name || profile.name || profile.id;
|
|
for (const model of profile.models || []) {
|
|
if (!profile.id || !model.id) continue;
|
|
const modelName = model.name || model.model || model.id;
|
|
choices.push({
|
|
profileId: profile.id,
|
|
modelId: model.id,
|
|
label: `${profileName} / ${modelName}`,
|
|
});
|
|
}
|
|
}
|
|
return choices;
|
|
}
|
|
|
|
export function choiceKey(entry: { profile_id: string; model_id: string }): string {
|
|
return `${entry.profile_id}\u0000${entry.model_id}`;
|
|
}
|
|
|
|
/**
|
|
* Drop entries that no longer resolve, collapse duplicates, cap the length.
|
|
*
|
|
* The backend applies the same rules on save; doing it here too keeps a stale
|
|
* row (a deleted profile) from being submitted and bouncing with a 400.
|
|
*/
|
|
export function sanitizeChain(
|
|
entries: FallbackEntry[],
|
|
choices: FallbackChoice[],
|
|
max = 4,
|
|
): FallbackEntry[] {
|
|
const valid = new Set(choices.map((choice) => `${choice.profileId}\u0000${choice.modelId}`));
|
|
const cleaned: FallbackEntry[] = [];
|
|
const seen = new Set<string>();
|
|
for (const entry of entries) {
|
|
const profileId = String(entry?.profile_id || "").trim();
|
|
const modelId = String(entry?.model_id || "").trim();
|
|
if (!profileId || !modelId) continue;
|
|
const key = `${profileId}\u0000${modelId}`;
|
|
if (!valid.has(key) || seen.has(key)) continue;
|
|
seen.add(key);
|
|
cleaned.push({ profile_id: profileId, model_id: modelId });
|
|
if (cleaned.length >= max) break;
|
|
}
|
|
return cleaned;
|
|
}
|
|
|
|
export function chainsEqual(a: FallbackEntry[], b: FallbackEntry[]): boolean {
|
|
if (a.length !== b.length) return false;
|
|
return a.every(
|
|
(entry, index) =>
|
|
entry.profile_id === b[index].profile_id && entry.model_id === b[index].model_id,
|
|
);
|
|
}
|
|
|
|
/**
|
|
* 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.
|
|
*/
|
|
export function withoutActive(
|
|
entries: FallbackEntry[],
|
|
active: { profile_id?: string | null; model_id?: string | null } | null,
|
|
): FallbackEntry[] {
|
|
if (!active?.profile_id || !active.model_id) return entries;
|
|
const key = `${active.profile_id}\u0000${active.model_id}`;
|
|
return entries.filter((entry) => choiceKey(entry) !== key);
|
|
}
|