134 lines
3.7 KiB
TypeScript
134 lines
3.7 KiB
TypeScript
const AGENT_AUTO_MODEL_KEY = "agentAutoModel";
|
|
const MULTIMODAL_MODEL_KEY = "multimodalModel";
|
|
const AGENT_CODING_MODEL_ID_KEY = "agentCodingModelId";
|
|
|
|
function toRecord(value: unknown): Record<string, unknown> | null {
|
|
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
|
return null;
|
|
}
|
|
return value as Record<string, unknown>;
|
|
}
|
|
|
|
export function isAiModelAgentAuto(input: {
|
|
defaultParams?: unknown;
|
|
capabilities?: unknown;
|
|
modelId?: string | null;
|
|
displayName?: string | null;
|
|
supportsVision?: boolean | null;
|
|
supportsImageGen?: boolean | null;
|
|
}): boolean {
|
|
const defaultParams = toRecord(input.defaultParams);
|
|
if (typeof defaultParams?.[AGENT_AUTO_MODEL_KEY] === "boolean") {
|
|
return defaultParams[AGENT_AUTO_MODEL_KEY] as boolean;
|
|
}
|
|
|
|
const capabilities = toRecord(input.capabilities);
|
|
if (typeof capabilities?.[AGENT_AUTO_MODEL_KEY] === "boolean") {
|
|
return capabilities[AGENT_AUTO_MODEL_KEY] as boolean;
|
|
}
|
|
|
|
const supportsVision = Boolean(input.supportsVision);
|
|
const supportsImageGen = Boolean(input.supportsImageGen);
|
|
if (!supportsVision || supportsImageGen) {
|
|
return false;
|
|
}
|
|
|
|
const modelText = `${input.modelId || ""} ${input.displayName || ""}`.toLowerCase();
|
|
const isQwenLikeModel =
|
|
modelText.includes("qwen") || modelText.includes("通义");
|
|
const isVisionLikeModel =
|
|
modelText.includes("vl") ||
|
|
modelText.includes("vision") ||
|
|
modelText.includes("多模态");
|
|
|
|
if (isQwenLikeModel && isVisionLikeModel) {
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
export function withAgentAutoModelFlag(
|
|
defaultParams: unknown,
|
|
enabled: boolean
|
|
): Record<string, unknown> | null {
|
|
const next = { ...(toRecord(defaultParams) || {}) };
|
|
|
|
if (enabled) {
|
|
next[AGENT_AUTO_MODEL_KEY] = true;
|
|
} else {
|
|
delete next[AGENT_AUTO_MODEL_KEY];
|
|
}
|
|
|
|
return Object.keys(next).length > 0 ? next : null;
|
|
}
|
|
|
|
export function getAiModelAgentCodingModelId(input: {
|
|
defaultParams?: unknown;
|
|
capabilities?: unknown;
|
|
}): string | null {
|
|
const defaultParams = toRecord(input.defaultParams);
|
|
const defaultValue = defaultParams?.[AGENT_CODING_MODEL_ID_KEY];
|
|
if (typeof defaultValue === "string") {
|
|
const normalized = defaultValue.trim();
|
|
if (normalized) return normalized;
|
|
}
|
|
|
|
const capabilities = toRecord(input.capabilities);
|
|
const capabilityValue = capabilities?.[AGENT_CODING_MODEL_ID_KEY];
|
|
if (typeof capabilityValue === "string") {
|
|
const normalized = capabilityValue.trim();
|
|
if (normalized) return normalized;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
export function withAgentCodingModelId(
|
|
defaultParams: unknown,
|
|
codingModelId?: string | null
|
|
): Record<string, unknown> | null {
|
|
const next = { ...(toRecord(defaultParams) || {}) };
|
|
const normalized = String(codingModelId || "").trim();
|
|
|
|
if (normalized) {
|
|
next[AGENT_CODING_MODEL_ID_KEY] = normalized;
|
|
} else {
|
|
delete next[AGENT_CODING_MODEL_ID_KEY];
|
|
}
|
|
|
|
return Object.keys(next).length > 0 ? next : null;
|
|
}
|
|
|
|
export function isAiModelMultimodal(input: {
|
|
capabilities?: unknown;
|
|
defaultParams?: unknown;
|
|
}): boolean {
|
|
const defaultParams = toRecord(input.defaultParams);
|
|
if (typeof defaultParams?.[MULTIMODAL_MODEL_KEY] === "boolean") {
|
|
return defaultParams[MULTIMODAL_MODEL_KEY] as boolean;
|
|
}
|
|
|
|
const capabilities = toRecord(input.capabilities);
|
|
if (typeof capabilities?.[MULTIMODAL_MODEL_KEY] === "boolean") {
|
|
return capabilities[MULTIMODAL_MODEL_KEY] as boolean;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
export function withMultimodalModelFlag(
|
|
capabilities: unknown,
|
|
enabled: boolean
|
|
): Record<string, unknown> | null {
|
|
const next = { ...(toRecord(capabilities) || {}) };
|
|
|
|
if (enabled) {
|
|
next[MULTIMODAL_MODEL_KEY] = true;
|
|
} else {
|
|
delete next[MULTIMODAL_MODEL_KEY];
|
|
}
|
|
|
|
return Object.keys(next).length > 0 ? next : null;
|
|
}
|