ai-chatbot/lib/ai/detail-agent-model-runtime.ts
Your Name b99eb6cf7a
Some checks failed
Lint / build (20) (push) Has been cancelled
Playwright Tests / test (push) Has been cancelled
fix: enable local moonshot detail agent option
2026-03-21 22:27:00 +08:00

74 lines
1.9 KiB
TypeScript

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";
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,
});
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;
};
};