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 { z } from "zod";
|
||||||
import { requireAdmin } from "@/lib/middleware/admin";
|
import { requireAdmin } from "@/lib/middleware/admin";
|
||||||
import {
|
import {
|
||||||
createAiModel,
|
|
||||||
createAiProvider,
|
|
||||||
getAiModelById,
|
|
||||||
getAiProviderById,
|
|
||||||
getAllAiModels,
|
|
||||||
getAllSystemSettings,
|
getAllSystemSettings,
|
||||||
getSystemSetting,
|
getSystemSetting,
|
||||||
upsertSystemSetting,
|
upsertSystemSetting,
|
||||||
updateAiModel,
|
|
||||||
updateAiProvider,
|
|
||||||
} from "@/lib/db/queries";
|
} from "@/lib/db/queries";
|
||||||
import { ChatSDKError } from "@/lib/errors";
|
import { ChatSDKError } from "@/lib/errors";
|
||||||
import { probeBflModelSupport } from "@/lib/realtime/bfl-capability";
|
import { probeBflModelSupport } from "@/lib/realtime/bfl-capability";
|
||||||
@ -20,9 +13,6 @@ import {
|
|||||||
resolveRealtimeProviders,
|
resolveRealtimeProviders,
|
||||||
} from "@/lib/realtime/config";
|
} from "@/lib/realtime/config";
|
||||||
import { DETAIL_AGENT_LOCAL_MOONSHOT_SETTING_KEY } from "@/lib/ai/detail-agent-model-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({
|
const settingSchema = z.object({
|
||||||
key: z.string(),
|
key: z.string(),
|
||||||
@ -30,8 +20,6 @@ const settingSchema = z.object({
|
|||||||
});
|
});
|
||||||
|
|
||||||
const moonshotDetailAgentConfigSchema = 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(),
|
providerName: z.string().trim().min(1).max(128).optional(),
|
||||||
baseUrl: z.string().trim().min(1),
|
baseUrl: z.string().trim().min(1),
|
||||||
apiKey: z.string().optional(),
|
apiKey: z.string().optional(),
|
||||||
@ -88,13 +76,22 @@ export async function POST(request: Request) {
|
|||||||
let nextValue = value;
|
let nextValue = value;
|
||||||
|
|
||||||
if (key === DETAIL_AGENT_LOCAL_MOONSHOT_SETTING_KEY) {
|
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 config = moonshotDetailAgentConfigSchema.parse(value || {});
|
||||||
const normalizedBaseUrl = String(config.baseUrl || "").trim() || MOONSHOT_OFFICIAL_BASE_URL;
|
const normalizedBaseUrl = String(config.baseUrl || "").trim() || MOONSHOT_OFFICIAL_BASE_URL;
|
||||||
const normalizedModelId = String(config.modelId || "").trim() || MOONSHOT_KIMI_K25_MODEL_ID;
|
const normalizedModelId = String(config.modelId || "").trim() || MOONSHOT_KIMI_K25_MODEL_ID;
|
||||||
const normalizedDisplayName = String(config.displayName || "").trim() || "官方 Kimi K2.5";
|
const normalizedDisplayName = String(config.displayName || "").trim() || "官方 Kimi K2.5";
|
||||||
const normalizedProviderName =
|
const normalizedProviderName =
|
||||||
String(config.providerName || "").trim() || "Moonshot AI (Official)";
|
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;
|
const enabled = config.isEnabled !== false;
|
||||||
|
|
||||||
if (!isMoonshotOfficialBaseUrl(normalizedBaseUrl)) {
|
if (!isMoonshotOfficialBaseUrl(normalizedBaseUrl)) {
|
||||||
@ -104,112 +101,25 @@ export async function POST(request: Request) {
|
|||||||
).toResponse();
|
).toResponse();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!config.providerId && !normalizedApiKey) {
|
if (!normalizedApiKey) {
|
||||||
return new ChatSDKError("bad_request:api", "缺少 Moonshot 官方 API Key").toResponse();
|
return new ChatSDKError("bad_request:api", "缺少 Moonshot 官方 API Key").toResponse();
|
||||||
}
|
}
|
||||||
|
|
||||||
const [existingSiteSettings] = await db.select().from(siteSettings).limit(1);
|
const saved = await upsertSystemSetting({
|
||||||
|
key: DETAIL_AGENT_LOCAL_MOONSHOT_SETTING_KEY,
|
||||||
const providerRows = config.providerId
|
value: {
|
||||||
? await updateAiProvider({
|
enabled,
|
||||||
id: config.providerId,
|
providerName: normalizedProviderName,
|
||||||
name: normalizedProviderName,
|
baseUrl: normalizedBaseUrl,
|
||||||
type: "openai",
|
apiKey: normalizedApiKey,
|
||||||
baseUrl: normalizedBaseUrl,
|
modelId: normalizedModelId,
|
||||||
apiKey: normalizedApiKey || undefined,
|
displayName: normalizedDisplayName,
|
||||||
isEnabled: enabled,
|
},
|
||||||
updatedBy: session.user.id,
|
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,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
return Response.json({
|
return Response.json({
|
||||||
setting: {
|
setting: saved[0],
|
||||||
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,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user