81 lines
3.4 KiB
JavaScript
81 lines
3.4 KiB
JavaScript
import { Type } from '@sinclair/typebox';
|
||
|
||
const outputCapabilities = new Map([
|
||
['detail', { label: '详情页', binding: 'native' }],
|
||
['main_images', { label: '主图', binding: 'native' }],
|
||
['sku_images', { label: 'SKU 图', binding: 'native' }],
|
||
['main_video', {
|
||
label: '主图视频', templateRoles: ['original', 'mapping'],
|
||
skillId: 'main-video-production', adapterId: 'main-video-render',
|
||
}],
|
||
['seeding_images', {
|
||
label: '种草图', templateRoles: ['original', 'mapping'],
|
||
skillId: 'seeding-images-production', adapterId: 'seeding-images-render',
|
||
}],
|
||
['seeding_video', {
|
||
label: '种草视频', templateRoles: ['original', 'mapping'],
|
||
skillId: 'seeding-video-production', adapterId: 'seeding-video-render',
|
||
}],
|
||
]);
|
||
|
||
const issueSchema = Type.Object({
|
||
outputId: Type.String({ minLength: 1 }),
|
||
variantId: Type.Optional(Type.String({ minLength: 1 })),
|
||
code: Type.Union([
|
||
Type.Literal('unsupported_output'),
|
||
Type.Literal('missing_template_resource'),
|
||
Type.Literal('missing_published_skill'),
|
||
Type.Literal('missing_render_adapter'),
|
||
]),
|
||
requirements: Type.Array(Type.String({ minLength: 1 }), { minItems: 1 }),
|
||
message: Type.String({ minLength: 1 }),
|
||
}, { additionalProperties: false });
|
||
|
||
export const outputPreflightSchema = Type.Union([
|
||
Type.Object({
|
||
schemaVersion: Type.Literal(1), status: Type.Literal('ready'), issues: Type.Tuple([]),
|
||
}, { additionalProperties: false }),
|
||
Type.Object({
|
||
schemaVersion: Type.Literal(1), status: Type.Literal('blocked'),
|
||
issues: Type.Array(issueSchema, { minItems: 1 }),
|
||
}, { additionalProperties: false }),
|
||
]);
|
||
|
||
// These bindings come from the host's resolved releases, never from requestedOutputs.
|
||
export function checkOutputCapabilities(requestedOutputs, {
|
||
componentRoles = new Map(), publishedSkills = new Set(), renderAdapters = new Set(),
|
||
} = {}) {
|
||
if (!Array.isArray(requestedOutputs) || !requestedOutputs.length) {
|
||
throw Error('制作目标与当前项目不匹配');
|
||
}
|
||
const issues = [];
|
||
for (const row of requestedOutputs) {
|
||
const { outputId, variantId } = row ?? {};
|
||
if (typeof outputId !== 'string' || !outputId) throw Error('制作目标与当前项目不匹配');
|
||
const capability = outputCapabilities.get(outputId);
|
||
const stage = { outputId, ...(typeof variantId === 'string' && variantId ? { variantId } : {}) };
|
||
const name = (capability?.label ?? outputId) + (stage.variantId ? `(${stage.variantId})` : '');
|
||
const missing = (code, requirements, reason) => {
|
||
issues.push({ ...stage, code, requirements, message: `${name}${reason}` });
|
||
};
|
||
if (!capability) {
|
||
missing('unsupported_output', [outputId], '没有注册输出能力');
|
||
continue;
|
||
}
|
||
if (capability.binding === 'native') continue;
|
||
|
||
const roles = componentRoles.get(outputId);
|
||
const missingRoles = capability.templateRoles.filter(role => !roles?.has(role));
|
||
if (missingRoles.length) {
|
||
missing('missing_template_resource', missingRoles, '缺少模板资源(' + missingRoles.join('、') + ')');
|
||
}
|
||
if (!publishedSkills.has(capability.skillId)) {
|
||
missing('missing_published_skill', [capability.skillId], '缺少已发布的制作 Skill');
|
||
}
|
||
if (!renderAdapters.has(capability.adapterId)) {
|
||
missing('missing_render_adapter', [capability.adapterId], '缺少已发布的渲染适配器');
|
||
}
|
||
}
|
||
return { schemaVersion: 1, status: issues.length ? 'blocked' : 'ready', issues };
|
||
}
|