fix: persist moonshot detail config to system settings
This commit is contained in:
parent
a2de2c6d6d
commit
8d602bf543
@ -1,16 +1,9 @@
|
||||
import { z } from "zod";
|
||||
import { requireAdmin } from "@/lib/middleware/admin";
|
||||
import {
|
||||
createAiModel,
|
||||
createAiProvider,
|
||||
getAiModelById,
|
||||
getAiProviderById,
|
||||
getAllAiModels,
|
||||
getAllSystemSettings,
|
||||
getSystemSetting,
|
||||
upsertSystemSetting,
|
||||
updateAiModel,
|
||||
updateAiProvider,
|
||||
} from "@/lib/db/queries";
|
||||
import { ChatSDKError } from "@/lib/errors";
|
||||
import { probeBflModelSupport } from "@/lib/realtime/bfl-capability";
|
||||
@ -20,9 +13,6 @@ import {
|
||||
resolveRealtimeProviders,
|
||||
} from "@/lib/realtime/config";
|
||||
import { DETAIL_AGENT_LOCAL_MOONSHOT_SETTING_KEY } from "@/lib/ai/detail-agent-model-config";
|
||||
import { db } from "@/lib/db";
|
||||
import { siteSettings } from "@/lib/db/schema";
|
||||
import { eq } from "drizzle-orm";
|
||||
|
||||
const settingSchema = z.object({
|
||||
key: z.string(),
|
||||
@ -30,8 +20,6 @@ const settingSchema = z.object({
|
||||
});
|
||||
|
||||
const moonshotDetailAgentConfigSchema = z.object({
|
||||
providerId: z.string().uuid().optional(),
|
||||
modelEntryId: z.string().uuid().optional(),
|
||||
providerName: z.string().trim().min(1).max(128).optional(),
|
||||
baseUrl: z.string().trim().min(1),
|
||||
apiKey: z.string().optional(),
|
||||
@ -88,13 +76,22 @@ export async function POST(request: Request) {
|
||||
let nextValue = value;
|
||||
|
||||
if (key === DETAIL_AGENT_LOCAL_MOONSHOT_SETTING_KEY) {
|
||||
const existingSetting = await getSystemSetting({
|
||||
key: DETAIL_AGENT_LOCAL_MOONSHOT_SETTING_KEY,
|
||||
}).catch(() => null);
|
||||
const existingValue =
|
||||
existingSetting?.value && typeof existingSetting.value === "object"
|
||||
? (existingSetting.value as Record<string, unknown>)
|
||||
: {};
|
||||
const config = moonshotDetailAgentConfigSchema.parse(value || {});
|
||||
const normalizedBaseUrl = String(config.baseUrl || "").trim() || MOONSHOT_OFFICIAL_BASE_URL;
|
||||
const normalizedModelId = String(config.modelId || "").trim() || MOONSHOT_KIMI_K25_MODEL_ID;
|
||||
const normalizedDisplayName = String(config.displayName || "").trim() || "官方 Kimi K2.5";
|
||||
const normalizedProviderName =
|
||||
String(config.providerName || "").trim() || "Moonshot AI (Official)";
|
||||
const normalizedApiKey = String(config.apiKey || "").trim();
|
||||
const normalizedApiKey =
|
||||
String(config.apiKey || "").trim() ||
|
||||
String(existingValue.apiKey || "").trim();
|
||||
const enabled = config.isEnabled !== false;
|
||||
|
||||
if (!isMoonshotOfficialBaseUrl(normalizedBaseUrl)) {
|
||||
@ -104,112 +101,25 @@ export async function POST(request: Request) {
|
||||
).toResponse();
|
||||
}
|
||||
|
||||
if (!config.providerId && !normalizedApiKey) {
|
||||
if (!normalizedApiKey) {
|
||||
return new ChatSDKError("bad_request:api", "缺少 Moonshot 官方 API Key").toResponse();
|
||||
}
|
||||
|
||||
const [existingSiteSettings] = await db.select().from(siteSettings).limit(1);
|
||||
|
||||
const providerRows = config.providerId
|
||||
? await updateAiProvider({
|
||||
id: config.providerId,
|
||||
name: normalizedProviderName,
|
||||
type: "openai",
|
||||
baseUrl: normalizedBaseUrl,
|
||||
apiKey: normalizedApiKey || undefined,
|
||||
isEnabled: enabled,
|
||||
updatedBy: session.user.id,
|
||||
})
|
||||
: await createAiProvider({
|
||||
name: normalizedProviderName,
|
||||
type: "openai",
|
||||
baseUrl: normalizedBaseUrl,
|
||||
apiKey: normalizedApiKey,
|
||||
isEnabled: enabled,
|
||||
updatedBy: session.user.id,
|
||||
});
|
||||
|
||||
const provider = providerRows[0];
|
||||
if (!provider) {
|
||||
return new ChatSDKError("bad_request:database", "保存 Moonshot 提供商失败").toResponse();
|
||||
}
|
||||
|
||||
let existingModel =
|
||||
config.modelEntryId ? await getAiModelById({ id: config.modelEntryId }) : undefined;
|
||||
|
||||
if (!existingModel) {
|
||||
const allModels = await getAllAiModels();
|
||||
existingModel =
|
||||
allModels.find(
|
||||
(model) => model.providerId === provider.id && model.modelId === normalizedModelId,
|
||||
) || undefined;
|
||||
}
|
||||
|
||||
const modelRows = existingModel
|
||||
? await updateAiModel({
|
||||
id: existingModel.id,
|
||||
providerId: provider.id,
|
||||
modelId: normalizedModelId,
|
||||
displayName: normalizedDisplayName,
|
||||
description: "Moonshot 官方 Kimi K2.5,专用于 AI详情生成 的规划与排版复审",
|
||||
supportsVision: true,
|
||||
supportsImageGen: false,
|
||||
isEnabled: enabled,
|
||||
isHidden: false,
|
||||
isDefault: false,
|
||||
isAgentModel: false,
|
||||
isMultimodal: true,
|
||||
})
|
||||
: await createAiModel({
|
||||
providerId: provider.id,
|
||||
modelId: normalizedModelId,
|
||||
displayName: normalizedDisplayName,
|
||||
description: "Moonshot 官方 Kimi K2.5,专用于 AI详情生成 的规划与排版复审",
|
||||
supportsVision: true,
|
||||
supportsImageGen: false,
|
||||
isEnabled: enabled,
|
||||
isHidden: false,
|
||||
isDefault: false,
|
||||
isAgentModel: false,
|
||||
isMultimodal: true,
|
||||
});
|
||||
|
||||
const model = modelRows[0];
|
||||
if (!model) {
|
||||
return new ChatSDKError("bad_request:database", "保存 Kimi K2.5 模型失败").toResponse();
|
||||
}
|
||||
|
||||
if (existingSiteSettings) {
|
||||
await db
|
||||
.update(siteSettings)
|
||||
.set({
|
||||
canvasDetailAgentModelId: model.id,
|
||||
updatedAt: new Date(),
|
||||
updatedBy: session.user.id,
|
||||
})
|
||||
.where(eq(siteSettings.id, existingSiteSettings.id));
|
||||
} else {
|
||||
await db.insert(siteSettings).values({
|
||||
siteName: "AI Chatbot",
|
||||
canvasDetailAgentModelId: model.id,
|
||||
updatedBy: session.user.id,
|
||||
});
|
||||
}
|
||||
const saved = await upsertSystemSetting({
|
||||
key: DETAIL_AGENT_LOCAL_MOONSHOT_SETTING_KEY,
|
||||
value: {
|
||||
enabled,
|
||||
providerName: normalizedProviderName,
|
||||
baseUrl: normalizedBaseUrl,
|
||||
apiKey: normalizedApiKey,
|
||||
modelId: normalizedModelId,
|
||||
displayName: normalizedDisplayName,
|
||||
},
|
||||
updatedBy: session.user.id,
|
||||
});
|
||||
|
||||
return Response.json({
|
||||
setting: {
|
||||
key: DETAIL_AGENT_LOCAL_MOONSHOT_SETTING_KEY,
|
||||
value: {
|
||||
providerId: provider.id,
|
||||
modelEntryId: model.id,
|
||||
providerName: provider.name,
|
||||
baseUrl: provider.baseUrl,
|
||||
modelId: model.modelId,
|
||||
displayName: model.displayName,
|
||||
isEnabled: model.isEnabled,
|
||||
canvasDetailAgentModelId: model.id,
|
||||
},
|
||||
},
|
||||
setting: saved[0],
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user