169 lines
5.6 KiB
TypeScript
169 lines
5.6 KiB
TypeScript
import {
|
|
invocationSupportsVip98kImages,
|
|
isMultipartVip98kInvocation,
|
|
normalizeVip98kInvocationHeaders,
|
|
selectVip98kTaskInvocation,
|
|
type Vip98kTaskInvocation,
|
|
} from "./vip98k-task-invocation";
|
|
|
|
type ModelDescriptor = {
|
|
displayName?: unknown;
|
|
modelId?: unknown;
|
|
supportsVision?: boolean | null;
|
|
capabilities?: unknown;
|
|
imageGenConfig?: unknown;
|
|
defaultParams?: unknown;
|
|
};
|
|
|
|
type ProviderDescriptor = {
|
|
type?: unknown;
|
|
};
|
|
|
|
const BFL_REFERENCE_IMAGE_MODEL_REGEX =
|
|
/^(?:bfl\/)?(?:flux-2-(?:max|flex|pro(?:-preview)?|klein-(?:4b|9b)(?:-preview)?)|flux-kontext-(?:pro|max))$/i;
|
|
const REFERENCE_IMAGE_MODE_REGEX =
|
|
/^(?:image-edit|image-to-image|img2img|reference-images?|image-reference|reference-image)$/i;
|
|
|
|
function readStringField(source: unknown, key: string) {
|
|
if (!source || typeof source !== "object" || !(key in source)) return "";
|
|
return String((source as Record<string, unknown>)[key] || "").trim();
|
|
}
|
|
|
|
function normalizeBflModelId(value: unknown) {
|
|
const trimmed = String(value || "").trim();
|
|
if (!trimmed) return "";
|
|
|
|
return trimmed
|
|
.replace(/^https?:\/\/[^/]+/i, "")
|
|
.replace(/^\/?(?:bfl\/)?v1\//i, "")
|
|
.replace(/^\/+/, "")
|
|
.replace(/[?#].*$/, "");
|
|
}
|
|
|
|
function readStringArrayField(source: unknown, key: string) {
|
|
if (!source || typeof source !== "object" || !(key in source)) return [];
|
|
const value = (source as Record<string, unknown>)[key];
|
|
if (!Array.isArray(value)) return [];
|
|
return value.map((item) => String(item || "").trim()).filter(Boolean);
|
|
}
|
|
|
|
function hasReferenceImageMode(value: unknown) {
|
|
if (!value || typeof value !== "object") return false;
|
|
const supportedModes = [
|
|
...readStringArrayField(value, "supportedModes"),
|
|
...readStringArrayField(value, "modes"),
|
|
];
|
|
return supportedModes.some((mode) => REFERENCE_IMAGE_MODE_REGEX.test(mode));
|
|
}
|
|
|
|
export function isKnownReferenceImageModel(
|
|
model: ModelDescriptor | null | undefined,
|
|
provider?: ProviderDescriptor | null
|
|
) {
|
|
const providerType = String(provider?.type || "").trim();
|
|
const candidates = [
|
|
model?.displayName,
|
|
model?.modelId,
|
|
readStringField(model?.defaultParams, "endpoint"),
|
|
readStringField(model?.defaultParams, "submitEndpoint"),
|
|
readStringField(model?.capabilities, "endpoint"),
|
|
];
|
|
|
|
const capabilitiesEndpoints =
|
|
model?.capabilities &&
|
|
typeof model.capabilities === "object" &&
|
|
"endpoints" in model.capabilities
|
|
? (model.capabilities as { endpoints?: unknown }).endpoints
|
|
: undefined;
|
|
|
|
candidates.push(readStringField(capabilitiesEndpoints, "submit"));
|
|
|
|
const matchesKnownBflModel = candidates
|
|
.map(normalizeBflModelId)
|
|
.some((modelId) => BFL_REFERENCE_IMAGE_MODEL_REGEX.test(modelId));
|
|
if (matchesKnownBflModel) return true;
|
|
|
|
if (providerType === "bfl-image") {
|
|
return hasReferenceImageMode(model?.capabilities) || hasReferenceImageMode(model?.defaultParams);
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
function toRecord(value: unknown): Record<string, unknown> | null {
|
|
if (!value || typeof value !== "object" || Array.isArray(value)) return null;
|
|
return value as Record<string, unknown>;
|
|
}
|
|
|
|
function parseVip98kInvocationCandidate(value: unknown): Vip98kTaskInvocation | null {
|
|
const invocation = toRecord(value);
|
|
if (!invocation) return null;
|
|
const path = String(invocation.path || "").trim();
|
|
if (!path.startsWith("/")) return null;
|
|
return {
|
|
kind: String(invocation.kind || "task").trim().toLowerCase(),
|
|
method: String(invocation.method || "POST").trim().toUpperCase(),
|
|
path,
|
|
contentType: String(
|
|
invocation.contentType || invocation.content_type || "application/json",
|
|
).trim(),
|
|
headers: normalizeVip98kInvocationHeaders(invocation.headers),
|
|
requestExample: {
|
|
...(toRecord(invocation.requestExample) ||
|
|
toRecord(invocation.request_example) ||
|
|
{}),
|
|
},
|
|
inputSchema:
|
|
toRecord(invocation.inputSchema) || toRecord(invocation.input_schema),
|
|
resultMethod: String(
|
|
invocation.resultMethod || invocation.result_method || "GET",
|
|
)
|
|
.trim()
|
|
.toUpperCase(),
|
|
resultPath: String(invocation.resultPath || invocation.result_path || "").trim(),
|
|
};
|
|
}
|
|
|
|
function vip98kLacksUrlReferenceInvocation(model: ModelDescriptor | null | undefined) {
|
|
const capabilitiesVip98k = toRecord(toRecord(model?.capabilities)?.vip98k);
|
|
const imageConfigVip98k = toRecord(toRecord(model?.imageGenConfig)?.vip98k);
|
|
const vip98k = capabilitiesVip98k || imageConfigVip98k;
|
|
if (!vip98k) return false;
|
|
|
|
const rawCandidates = [
|
|
vip98k.invocation,
|
|
...(Array.isArray(vip98k.invocations) ? vip98k.invocations : []),
|
|
];
|
|
const candidates = rawCandidates
|
|
.map(parseVip98kInvocationCandidate)
|
|
.filter((item): item is Vip98kTaskInvocation => Boolean(item));
|
|
if (candidates.length === 0) return false;
|
|
|
|
const selected = selectVip98kTaskInvocation({
|
|
modelId: String(model?.modelId || ""),
|
|
kind: "image",
|
|
configuredInvocation: candidates[0] || null,
|
|
configuredInvocations: candidates,
|
|
imageCount: 1,
|
|
});
|
|
return !selected || isMultipartVip98kInvocation(selected) || !invocationSupportsVip98kImages(selected);
|
|
}
|
|
|
|
export function modelLacksReferenceImageInputSupport(
|
|
model: ModelDescriptor | null | undefined,
|
|
provider?: ProviderDescriptor | null
|
|
) {
|
|
if (isKnownReferenceImageModel(model, provider)) return false;
|
|
if (String(provider?.type || "").trim() === "vip98k" && vip98kLacksUrlReferenceInvocation(model)) {
|
|
return true;
|
|
}
|
|
return model?.supportsVision === false;
|
|
}
|
|
|
|
export function modelSupportsReferenceImageInput(
|
|
model: ModelDescriptor | null | undefined,
|
|
provider?: ProviderDescriptor | null
|
|
) {
|
|
return !modelLacksReferenceImageInputSupport(model, provider);
|
|
}
|