feat(chat): load model options from backend
Build Push and Deploy / docker-image (push) Successful in 1m6s
Build Push and Deploy / deploy-fallback-log (push) Has been skipped

This commit is contained in:
2026-06-10 19:50:43 +08:00
parent 1e872ca873
commit 7d2ae87e39
7 changed files with 220 additions and 41 deletions
+65
View File
@@ -0,0 +1,65 @@
import { fetchAgentModels } from "./chatModels";
const apiFetch = jest.fn();
jest.mock("@/lib/apiFetch", () => ({
apiFetch: (...args: unknown[]) => apiFetch(...args),
}));
describe("fetchAgentModels", () => {
beforeEach(() => {
apiFetch.mockReset();
});
it("loads model options and backend default model", async () => {
apiFetch.mockResolvedValueOnce({
ok: true,
json: async () => ({
default_model: "deepseek/deepseek-v4-flash",
models: [
{
id: "deepseek/deepseek-v4-flash",
label: "快速",
description: "快速回答和任务执行",
icon: "bolt",
},
],
}),
});
await expect(fetchAgentModels()).resolves.toEqual({
defaultModel: "deepseek/deepseek-v4-flash",
models: [
{
id: "deepseek/deepseek-v4-flash",
label: "快速",
description: "快速回答和任务执行",
icon: "bolt",
},
],
});
expect(apiFetch).toHaveBeenCalledWith(
expect.stringContaining("/api/v1/agent/chat/models"),
expect.objectContaining({
method: "GET",
projectHeaderMode: "include",
userHeaderMode: "include",
skipAuthRedirect: true,
}),
);
});
it("falls back to the first option when default model is omitted", async () => {
apiFetch.mockResolvedValueOnce({
ok: true,
json: async () => ({
models: [{ id: "provider/model", label: "Model" }],
}),
});
await expect(fetchAgentModels()).resolves.toEqual({
defaultModel: "provider/model",
models: [{ id: "provider/model", label: "Model" }],
});
});
});
+74
View File
@@ -0,0 +1,74 @@
import { apiFetch } from "@/lib/apiFetch";
import { config } from "@config/config";
import type { AgentModel } from "./chatStream";
export type AgentModelIcon = "bolt" | "sparkle";
export type AgentModelOption = {
id: AgentModel;
label: string;
description?: string;
icon?: AgentModelIcon;
};
export type AgentModelConfig = {
defaultModel?: AgentModel;
models: AgentModelOption[];
};
const isObjectRecord = (value: unknown): value is Record<string, unknown> =>
typeof value === "object" && value !== null && !Array.isArray(value);
const normalizeModelOption = (value: unknown): AgentModelOption | null => {
if (!isObjectRecord(value) || typeof value.id !== "string") {
return null;
}
const id = value.id.trim();
if (!id) {
return null;
}
const label =
typeof value.label === "string" && value.label.trim()
? value.label.trim()
: id;
const description =
typeof value.description === "string" && value.description.trim()
? value.description.trim()
: undefined;
const icon =
value.icon === "bolt" || value.icon === "sparkle" ? value.icon : undefined;
return {
id,
label,
description,
icon,
};
};
export const fetchAgentModels = async (): Promise<AgentModelConfig> => {
const response = await apiFetch(`${config.AGENT_URL}/api/v1/agent/chat/models`, {
method: "GET",
projectHeaderMode: "include",
userHeaderMode: "include",
skipAuthRedirect: true,
});
if (!response.ok) {
throw new Error(await response.text());
}
const payload = (await response.json()) as {
default_model?: unknown;
models?: unknown[];
};
const models = (payload.models ?? [])
.map(normalizeModelOption)
.filter((model): model is AgentModelOption => Boolean(model));
const defaultModel =
typeof payload.default_model === "string" && payload.default_model.trim()
? payload.default_model.trim()
: models[0]?.id;
return {
defaultModel,
models,
};
};
+2 -2
View File
@@ -60,7 +60,7 @@ describe("streamAgentChat", () => {
await streamAgentChat({
message: "hi",
model: "deepseek/deepseek-v4-flash",
model: "provider/model",
onEvent: (event) => events.push(event),
});
@@ -73,7 +73,7 @@ describe("streamAgentChat", () => {
body: JSON.stringify({
message: "hi",
session_id: undefined,
model: "deepseek/deepseek-v4-flash",
model: "provider/model",
approval_mode: undefined,
}),
}),
+1 -3
View File
@@ -1,9 +1,7 @@
import { apiFetch } from "@/lib/apiFetch";
import { config } from "@config/config";
export type AgentModel =
| "deepseek/deepseek-v4-flash"
| "deepseek/deepseek-v4-pro";
export type AgentModel = string;
export type PermissionReply = "once" | "always" | "reject";
export type AgentApprovalMode = "request" | "always";