import "server-only"; import { createOpenAI } from "@ai-sdk/openai"; import type { LanguageModel } from "ai"; import { getSystemSetting } from "@/lib/db/queries"; import { DETAIL_AGENT_LOCAL_MOONSHOT_BASE_URL, DETAIL_AGENT_LOCAL_MOONSHOT_OPTION_ID, DETAIL_AGENT_LOCAL_MOONSHOT_SETTING_KEY, isDetailAgentLocalMoonshotOption, normalizeDetailAgentLocalMoonshotConfig, } from "@/lib/ai/detail-agent-model-config"; import { createMoonshotKimiFetch } from "@/lib/ai/detail-agent-kimi-profile"; export async function resolveDetailAgentLocalMoonshotModel(params: { siteSettingModelId?: string | null; }) { if (!isDetailAgentLocalMoonshotOption(params.siteSettingModelId)) { return null; } const setting = await getSystemSetting({ key: DETAIL_AGENT_LOCAL_MOONSHOT_SETTING_KEY, }).catch(() => null); const config = normalizeDetailAgentLocalMoonshotConfig(setting?.value); if (!config.enabled || !config.apiKey.trim()) { return null; } const provider = createOpenAI({ apiKey: config.apiKey, baseURL: config.baseUrl || DETAIL_AGENT_LOCAL_MOONSHOT_BASE_URL, fetch: createMoonshotKimiFetch(), }); const model = provider.chat(config.modelId); return { kind: "local_moonshot" as const, model, modelRef: { id: DETAIL_AGENT_LOCAL_MOONSHOT_OPTION_ID, displayName: config.displayName, modelId: config.modelId, supportsVision: true, capabilities: { multimodalModel: true }, defaultParams: null, }, providerRef: { id: DETAIL_AGENT_LOCAL_MOONSHOT_OPTION_ID, name: config.providerName, type: "openai", baseUrl: config.baseUrl, }, }; } export type DetailAgentResolvedModel = { kind: "local_moonshot" | "db_model"; model: LanguageModel; modelRef: { id: string; displayName: string; modelId: string; supportsVision?: boolean | null; capabilities?: unknown; defaultParams?: unknown; }; providerRef: { id: string; name: string; type: string; baseUrl?: string | null; }; };