fix(ai): preserve saved and manual models
This commit is contained in:
1 parent
2220eed285
commit
b053adf175
3 files changed
+100
-7
No files matched your search
+41
-1
@@ -1,5 +1,5 @@
|
||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
import { generateId } from "@/lib/ai/aiConfigList";
|
||||
import { addConfiguredAiModel, aiModelOptions, generateId } from "@/lib/ai/aiConfigList";
|
||||
|
||||
describe("generateId", () => {
|
||||
afterEach(() => {
|
||||
@@ -22,3 +22,43 @@ describe("generateId", () => {
|
||||
expect(generateId()).toBe("00000000-0000-4000-8000-000000000000");
|
||||
});
|
||||
});
|
||||
|
||||
describe("AI model options", () => {
|
||||
it("keeps saved models selectable when provider model discovery fails", () => {
|
||||
const options = aiModelOptions(
|
||||
{
|
||||
model: "ark-code-latest",
|
||||
models: [{ name: "doubao-seed-2.0-code", label: "Doubao Code" }],
|
||||
},
|
||||
[],
|
||||
);
|
||||
|
||||
expect(options).toEqual([
|
||||
{ id: "ark-code-latest", displayName: undefined, supportedEffortLevels: undefined, effortCapability: undefined },
|
||||
{ id: "doubao-seed-2.0-code", displayName: "Doubao Code", supportedEffortLevels: undefined, effortCapability: undefined },
|
||||
]);
|
||||
});
|
||||
|
||||
it("persists a manual model only once", () => {
|
||||
expect(addConfiguredAiModel([{ name: "ark-code-latest" }], " doubao-seed-2.0-code ")).toEqual([{ name: "ark-code-latest" }, { name: "doubao-seed-2.0-code" }]);
|
||||
expect(addConfiguredAiModel([{ name: "ark-code-latest" }], "ark-code-latest")).toEqual([{ name: "ark-code-latest" }]);
|
||||
});
|
||||
|
||||
it("merges saved metadata with discovered capabilities", () => {
|
||||
const options = aiModelOptions(
|
||||
{
|
||||
model: "",
|
||||
models: [{ name: "gpt-4o", label: "GPT-4o", supportedEffortLevels: ["high"] }],
|
||||
},
|
||||
[{ id: "gpt-4o", displayName: "Discovered GPT-4o", effortCapability: { kind: "unsupported" } }],
|
||||
);
|
||||
|
||||
expect(options).toEqual([{ id: "gpt-4o", displayName: "GPT-4o", supportedEffortLevels: ["high"], effortCapability: { kind: "unsupported" } }]);
|
||||
});
|
||||
|
||||
it("handles an empty configured model and blank manual ids", () => {
|
||||
expect(aiModelOptions({ model: "", models: [{ name: "a-model" }] }, [])).toEqual([{ id: "a-model", displayName: undefined, supportedEffortLevels: undefined, effortCapability: undefined }]);
|
||||
expect(addConfiguredAiModel(undefined, "new-model")).toEqual([{ name: "new-model" }]);
|
||||
expect(addConfiguredAiModel([{ name: "existing" }], " ")).toEqual([{ name: "existing" }]);
|
||||
});
|
||||
});
|
||||
@@ -1,4 +1,5 @@
|
||||
import type { AiConfig, AiConfigItem } from "@/types/ai";
|
||||
import type { AiConfig, AiConfigItem, AiConfiguredModel } from "@/types/ai";
|
||||
import type { AiModelInfo } from "@/lib/backend/tauri";
|
||||
import { uuid } from "@/lib/common/utils";
|
||||
|
||||
export type { AiConfigItem };
|
||||
@@ -20,6 +21,46 @@ export function aiConfigToItem(config: AiConfig, id: string, name: string): AiCo
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Combines models saved by the user with models returned by a provider's
|
||||
* discovery endpoint. Some OpenAI-compatible providers intentionally do not
|
||||
* expose `/models`, so their saved models must remain selectable on their own.
|
||||
*/
|
||||
export function aiModelOptions(config: Pick<AiConfig, "model" | "models">, discovered: AiModelInfo[]): AiModelInfo[] {
|
||||
const saved: AiModelInfo[] = [
|
||||
config.model.trim() ? { id: config.model.trim() } : null,
|
||||
...(config.models ?? []).map((model) => ({
|
||||
id: model.name.trim(),
|
||||
displayName: model.label?.trim() || undefined,
|
||||
supportedEffortLevels: model.supportedEffortLevels,
|
||||
})),
|
||||
].filter((model): model is AiModelInfo => Boolean(model?.id));
|
||||
|
||||
const options = new Map<string, AiModelInfo>();
|
||||
for (const model of [...saved, ...discovered]) {
|
||||
const id = model.id.trim();
|
||||
if (!id) continue;
|
||||
const existing = options.get(id);
|
||||
options.set(id, {
|
||||
...model,
|
||||
id,
|
||||
displayName: existing?.displayName ?? model.displayName,
|
||||
supportedEffortLevels: existing?.supportedEffortLevels ?? model.supportedEffortLevels,
|
||||
effortCapability: model.effortCapability ?? existing?.effortCapability,
|
||||
});
|
||||
}
|
||||
return [...options.values()];
|
||||
}
|
||||
|
||||
/** Add a manually entered model once while retaining its optional display metadata. */
|
||||
export function addConfiguredAiModel(models: AiConfiguredModel[] | undefined, modelId: string): AiConfiguredModel[] {
|
||||
const id = modelId.trim();
|
||||
if (!id) return models ?? [];
|
||||
const existing = models ?? [];
|
||||
if (existing.some((model) => model.name.trim() === id)) return existing;
|
||||
return [...existing, { name: id }];
|
||||
}
|
||||
|
||||
export type ConfigNameValidationResult = "empty" | "duplicate" | "valid";
|
||||
|
||||
export function validateConfigName(name: string, configs: AiConfigItem[], excludeId?: string): ConfigNameValidationResult {
|
||||
|
||||
Reference in new issue
Block a user