Files
openmotion/shared/model-capabilities.ts
Haoran Zhou b87b33f84c feat(studio): match image and video features by model capabilities instead of ID allowlists (#796)
* feat(studio): match image and video features by model capabilities instead of ID allowlists

* test: fold model-capabilities tests into scheduled generation-options suite

---------

Co-authored-by: Awek-015 <zhouhaoran@refly.ai>
2026-11-21 18:35:23 +08:00

86 lines
3.0 KiB
TypeScript

import { modelReferenceLimit, type Model } from './gateway.ts';
import { generationOptions } from './generation-options.ts';
/** Normalized media capabilities derived from adapter limits, live `nativeOptions`, and `generationOptions`. */
export type MediaCapabilities = {
image: {
edit: boolean;
maxReferences: number;
transparentBackground: boolean;
};
video: {
textToVideo: boolean;
firstFrame: boolean;
};
};
/** OpenDesign Cloud (API key) and OpenDesign signed-in account share the same managed media API. */
export function isOpenDesignProvider(providerId: string): boolean {
return providerId === 'opendesign' || providerId === 'account';
}
const isRecord = (value: unknown): value is Record<string, unknown> =>
Boolean(value) && typeof value === 'object' && !Array.isArray(value);
function stringList(raw: unknown): string[] {
const values = Array.isArray(raw) ? raw : isRecord(raw) && Array.isArray(raw.values) ? raw.values : undefined;
if (!values) return [];
return values
.filter((item): item is string => typeof item === 'string')
.map(item => item.trim().toLowerCase())
.filter(Boolean);
}
function hasPublishedTransparentBackground(model: Model): boolean {
const native = model.nativeOptions;
if (native) {
const values = [
...stringList(native.background),
...stringList(native.backgrounds),
];
if (values.includes('transparent')) return true;
if (native.supports_transparent_background === true || native.transparent_background === true) return true;
}
const options = model.generationOptions ?? generationOptions(model);
return Boolean(
options?.fields.some(
field => field.key === 'background' && field.choices?.some(choice => String(choice.value).toLowerCase() === 'transparent'),
),
);
}
/**
* Single capability reader for Studio feature matching.
* - Image `maxReferences` / `edit` come from `modelReferenceLimit(model)`.
* - Image `transparentBackground` reads `nativeOptions.background(s)` first, then `generationOptions(model)`.
* - Video `textToVideo` / `firstFrame` derive from `model.mode` and `modelReferenceLimit(model)`.
*/
export function modelCapabilities(model: Model): MediaCapabilities {
if (model.modality === 'image') {
const maxReferences = modelReferenceLimit(model);
return {
image: {
edit: maxReferences > 0,
maxReferences,
transparentBackground: hasPublishedTransparentBackground(model),
},
video: { textToVideo: false, firstFrame: false },
};
}
if (model.modality === 'video') {
return {
image: { edit: false, maxReferences: 0, transparentBackground: false },
video: {
textToVideo: !/^(image|video)-to-/.test(model.mode),
firstFrame: modelReferenceLimit(model) > 0 || model.mode === 'image-to-video',
},
};
}
return {
image: { edit: false, maxReferences: 0, transparentBackground: false },
video: { textToVideo: false, firstFrame: false },
};
}