Files
OCS_service/server/plugins/initConfig.ts
T
2026-05-23 22:23:29 +08:00

62 lines
1.9 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// server/plugins/initConfig.ts - 服务启动时检查 system_config 表,补全缺失的默认配置
// 只在首次部署或新增配置项时写入,不覆盖已有值;不读取环境变量
import { prisma } from "~~/server/utils/db";
// 所有系统配置的默认值,key 对应 system_config.key
// 管理员后续通过后台修改 valueopenai_api_key 默认留空,需手动填入
const DEFAULT_CONFIGS = [
{
key: "openai_api_key",
value: "",
description: "OpenAI 或兼容 API Key,仅服务端使用,不能返回给前端"
},
{
key: "openai_api_base",
value: "https://api.openai.com/v1",
description: "OpenAI 兼容 API base URL,例如 https://api.openai.com/v1"
},
{
key: "openai_model",
value: "gpt-5.2",
description: "Chat Completions 使用的模型名"
},
{
key: "max_tokens",
value: "500",
description: "单次回答最大 token 数"
},
{
key: "temperature",
value: "0.7",
description: "模型采样温度,越低越稳定,范围 0~2"
},
{
key: "openai_stream_max_sse_line_bytes",
value: "1048576",
description: "单行 SSE data 最大字节数,防止异常流无限堆内存"
}
];
export default defineNitroPlugin(async () => {
try {
const existingRows = await prisma.systemConfig.findMany({
select: { key: true }
});
const existingKeys = new Set(existingRows.map((r) => r.key));
const missing = DEFAULT_CONFIGS.filter((c) => !existingKeys.has(c.key));
if (missing.length === 0) return;
await prisma.systemConfig.createMany({
data: missing,
skipDuplicates: true
});
console.log(
`[initConfig] 已写入 ${missing.length} 项默认系统配置:${missing.map((c) => c.key).join(", ")}`
);
} catch (err) {
// 初始化失败不阻断进程启动,记录警告供排查
console.error("[initConfig] 系统配置初始化失败:", String(err));
}
});