refactor: 简化模型管理,移除启用/禁用,优化测试和布局

- 移除供应商/模型启用禁用能力,清理DB schema/migration/API/前端
- 供应商测试改为Base URL连通性+/models探测
- 新增POST /api/models/test模型连接测试
- 新增GET /api/providers/options专用供应商选项接口
- 统一工具栏为ModelsToolbar,参考项目管理布局
- 模型弹窗优化:默认能力、响应式3列标签、并排数值
- 前后端正整数校验、供应商下拉loading/error/empty状态
- 表格列宽统一,操作列/名称列固定宽度
This commit is contained in:
2026-05-29 18:03:33 +08:00
parent 9241c782e6
commit 34e915ccf4
39 changed files with 895 additions and 961 deletions

View File

@@ -1,6 +1,6 @@
import type Database from "bun:sqlite";
import { describe, expect, test } from "bun:test";
import { describe, expect, mock, test } from "bun:test";
import type { Model, RuntimeMode } from "../../../src/shared/api";
@@ -30,16 +30,6 @@ async function deleteModelViaHandler(req: Request, db: Database): Promise<Respon
return h(req, db, MODE);
}
async function disableModelViaHandler(req: Request, db: Database): Promise<Response> {
const { handleDisableModel: h } = await import("../../../src/server/routes/models/disable");
return h(req, db, MODE);
}
async function enableModelViaHandler(req: Request, db: Database): Promise<Response> {
const { handleEnableModel: h } = await import("../../../src/server/routes/models/enable");
return h(req, db, MODE);
}
async function getModelViaHandler(req: Request, db: Database): Promise<Response> {
const { handleGetModel: h } = await import("../../../src/server/routes/models/get");
return h(req, db, MODE);
@@ -53,6 +43,13 @@ async function listModelsViaHandler(req: Request, db: Database): Promise<Respons
import { createModel } from "../../../src/server/db/models";
import { createProvider } from "../../../src/server/db/providers";
void mock.module("ai", () => ({
createProviderRegistry: () => ({
languageModel: () => ({}),
}),
generateText: () => Promise.resolve({ text: "Hi" }),
}));
function seedProvider(db: Database, name?: string): string {
const result = createProvider(db, {
apiKey: "sk-test",
@@ -64,6 +61,11 @@ function seedProvider(db: Database, name?: string): string {
return result.provider.id;
}
async function testModelViaHandler(req: Request, db: Database): Promise<Response> {
const { handleTestModelConfig: h } = await import("../../../src/server/routes/models/test");
return h(req, db, MODE);
}
async function updateModelViaHandler(req: Request, db: Database): Promise<Response> {
const { handleUpdateModel: h } = await import("../../../src/server/routes/models/update");
return h(req, db, MODE);
@@ -163,34 +165,6 @@ describe("models API routes", () => {
});
});
test("POST /api/models/:id/enable", async () => {
await withRouteDb(async (db) => {
const model = createTestModel(db, "EnableTest");
await disableModelViaHandler(
new Request("http://localhost/api/models/" + model.id + "/disable", { method: "POST" }),
db,
);
const req = new Request("http://localhost/api/models/" + model.id + "/enable", { method: "POST" });
const res = await enableModelViaHandler(req, db);
expect(res.status).toBe(200);
const body = (await res.json()) as { model: Model };
expect(body.model.enabled).toBe(true);
});
});
test("POST /api/models/:id/disable", async () => {
await withRouteDb(async (db) => {
const model = createTestModel(db, "DisableTest");
const req = new Request("http://localhost/api/models/" + model.id + "/disable", { method: "POST" });
const res = await disableModelViaHandler(req, db);
expect(res.status).toBe(200);
const body = (await res.json()) as { model: Model };
expect(body.model.enabled).toBe(false);
});
});
test("DELETE /api/models/:id", async () => {
await withRouteDb(async (db) => {
const model = createTestModel(db, "DeleteTest");
@@ -219,4 +193,74 @@ describe("models API routes", () => {
expect(res.status).toBe(400);
});
});
test("invalid numeric fields return 400", async () => {
await withRouteDb(async (db) => {
const providerId = seedProvider(db);
const createReq = new Request("http://localhost/api/models", {
body: JSON.stringify({
capabilities: ["text"],
contextLength: 0,
modelId: "test",
name: "Test",
providerId,
}),
headers: { "Content-Type": "application/json" },
method: "POST",
});
const createRes = await createModelViaHandler(createReq, db);
expect(createRes.status).toBe(400);
const model = createTestModel(db, "NumericTest", providerId);
const updateReq = new Request("http://localhost/api/models/" + model.id, {
body: JSON.stringify({ maxOutputTokens: 1.5 }),
headers: { "Content-Type": "application/json" },
method: "PATCH",
});
const updateRes = await updateModelViaHandler(updateReq, db);
expect(updateRes.status).toBe(400);
});
});
test("POST /api/models/test 成功测试模型连接", async () => {
await withRouteDb(async (db) => {
const providerId = seedProvider(db);
const req = new Request("http://localhost/api/models/test", {
body: JSON.stringify({ modelId: "gpt-4o", providerId }),
headers: { "Content-Type": "application/json" },
method: "POST",
});
const res = await testModelViaHandler(req, db);
expect(res.status).toBe(200);
const body = (await res.json()) as { modelTestResponse: { message: string; ok: boolean } };
expect(body.modelTestResponse.ok).toBe(true);
expect(body.modelTestResponse.message).toContain("模型连接成功");
});
});
test("POST /api/models/test 缺少 providerId 返回 400", async () => {
await withRouteDb(async (db) => {
const req = new Request("http://localhost/api/models/test", {
body: JSON.stringify({ modelId: "gpt-4o" }),
headers: { "Content-Type": "application/json" },
method: "POST",
});
const res = await testModelViaHandler(req, db);
expect(res.status).toBe(400);
});
});
test("POST /api/models/test 不存在的供应商返回 404", async () => {
await withRouteDb(async (db) => {
const req = new Request("http://localhost/api/models/test", {
body: JSON.stringify({ modelId: "gpt-4o", providerId: "nonexistent" }),
headers: { "Content-Type": "application/json" },
method: "POST",
});
const res = await testModelViaHandler(req, db);
expect(res.status).toBe(404);
});
});
});