ai-chatbot/lib/ai/model-compat.ts
Your Name 3a087d3392
Some checks failed
Lint / build (20) (push) Has been cancelled
Playwright Tests / test (push) Has been cancelled
fix: stabilize canvas agent mode for kimi
2026-04-05 01:28:28 +08:00

82 lines
1.9 KiB
TypeScript

type ModelCompatDescriptor = {
providerType?: string | null;
providerBaseUrl?: string | null;
modelId?: string | null;
displayName?: string | null;
provider?: {
type?: string | null;
baseUrl?: string | null;
} | null;
model?: {
modelId?: string | null;
displayName?: string | null;
} | null;
};
const KIMI_MODEL_PATTERN = /\bkimi(?:[-_.\s]?[a-z0-9.]+)?\b/i;
function normalizeProviderType(input: ModelCompatDescriptor): string {
return String(input.providerType || input.provider?.type || "")
.trim()
.toLowerCase();
}
function normalizeProviderBaseUrl(input: ModelCompatDescriptor): string {
return String(input.providerBaseUrl || input.provider?.baseUrl || "")
.trim()
.toLowerCase();
}
function normalizeModelText(input: ModelCompatDescriptor): string {
return `${String(input.displayName || input.model?.displayName || "")} ${String(input.modelId || input.model?.modelId || "")}`
.trim()
.toLowerCase();
}
export function isMoonshotKimiModel(
input?: ModelCompatDescriptor | null,
): boolean {
if (!input) {
return false;
}
const providerType = normalizeProviderType(input);
const providerBaseUrl = normalizeProviderBaseUrl(input);
const modelText = normalizeModelText(input);
if (providerType.includes("moonshot")) {
return true;
}
if (providerBaseUrl.includes("moonshot.cn")) {
return true;
}
if (modelText.includes("moonshot")) {
return true;
}
if (KIMI_MODEL_PATTERN.test(modelText)) {
return true;
}
return false;
}
export function getCompatibleTextGenerationTemperature(
input: ModelCompatDescriptor | null | undefined,
preferredTemperature?: number,
): number | undefined {
if (isMoonshotKimiModel(input)) {
return 1;
}
return preferredTemperature;
}
export function shouldForceInlineVisionInputs(
input: ModelCompatDescriptor | null | undefined,
): boolean {
return isMoonshotKimiModel(input);
}