fix(canvas): 短剧分镜 - 支持多镜头并行生成并正确引用角色图声资产
This commit is contained in:
1 parent
f62ea62355
commit
4787c130d0
13 files changed
+393
-55
No files matched your search
@@ -3,10 +3,23 @@ import { resourceFileUrl, resourceIdFromStorageKey, resourceStorageKey } from "@
|
||||
import type { ProjectAsset, ProjectDetail } from "@/services/api/projects";
|
||||
import type { AssetCategory } from "@/stores/use-asset-store";
|
||||
import type { ReferenceImage } from "@/types/image";
|
||||
import type { ReferenceAudio } from "@/types/media";
|
||||
|
||||
export type ShotAssetReferenceContext = {
|
||||
mentionReferences: CanvasResourceReference[];
|
||||
referenceImages: ReferenceImage[];
|
||||
referenceAudios: ReferenceAudio[];
|
||||
assetReferences: ShotPromptAssetReference[];
|
||||
resolvedCharacterVersions: Array<{ assetId: string; versionId: string }>;
|
||||
};
|
||||
|
||||
export type ShotPromptAssetReference = {
|
||||
assetId: string;
|
||||
title: string;
|
||||
category: string;
|
||||
imageIndex: number;
|
||||
audioIndex?: number;
|
||||
voiceDescription?: string;
|
||||
};
|
||||
|
||||
const assetCategories = new Set<AssetCategory>(["character", "environment", "wardrobe", "prop", "weapon", "style", "other"]);
|
||||
@@ -24,6 +37,35 @@ export function buildShotAssetReferenceContext(detail: ProjectDetail, shotId: st
|
||||
return [{ asset, image }];
|
||||
});
|
||||
|
||||
const referenceAudios: ReferenceAudio[] = [];
|
||||
const audioIndexByResourceId = new Map<string, number>();
|
||||
const assetReferences = entries.map(({ asset }, index) => {
|
||||
const sampleResourceId = stringValue(asset.character?.voice?.profile.sampleResourceId);
|
||||
let audioIndex: number | undefined;
|
||||
if (sampleResourceId) {
|
||||
audioIndex = audioIndexByResourceId.get(sampleResourceId);
|
||||
if (!audioIndex) {
|
||||
audioIndex = referenceAudios.length + 1;
|
||||
audioIndexByResourceId.set(sampleResourceId, audioIndex);
|
||||
referenceAudios.push({
|
||||
id: `project-character-voice:${asset.id}`,
|
||||
name: `${asset.title}-声音样本`,
|
||||
type: "audio/*",
|
||||
url: resourceFileUrl(sampleResourceId),
|
||||
storageKey: resourceStorageKey(sampleResourceId),
|
||||
});
|
||||
}
|
||||
}
|
||||
return {
|
||||
assetId: asset.id,
|
||||
title: asset.title,
|
||||
category: asset.category,
|
||||
imageIndex: index + 1,
|
||||
...(audioIndex ? { audioIndex } : {}),
|
||||
...(asset.character ? { voiceDescription: characterVoiceDescription(asset) } : {}),
|
||||
} satisfies ShotPromptAssetReference;
|
||||
});
|
||||
|
||||
return {
|
||||
mentionReferences: entries.map(({ asset, image }) => ({
|
||||
id: `project-asset:${asset.id}`,
|
||||
@@ -38,6 +80,9 @@ export function buildShotAssetReferenceContext(detail: ProjectDetail, shotId: st
|
||||
category: projectAssetCategory(asset.category),
|
||||
})),
|
||||
referenceImages: entries.map(({ image }) => image),
|
||||
referenceAudios,
|
||||
assetReferences,
|
||||
resolvedCharacterVersions: entries.flatMap(({ asset }) => asset.character?.versionId ? [{ assetId: asset.id, versionId: asset.character.versionId }] : []),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -53,7 +98,21 @@ export function resolveShotAssetMentionPrompt(prompt: string, context: ShotAsset
|
||||
return label;
|
||||
});
|
||||
if (unresolved.size) throw new Error(`提示词中的 ${Array.from(unresolved).join("、")} 未绑定到当前镜头,请重新选择资产或删除引用`);
|
||||
return resolved;
|
||||
const assetBlock = compileShotAssetReferencePrompt(context.assetReferences);
|
||||
return [resolved.trim(), assetBlock].filter(Boolean).join("\n\n");
|
||||
}
|
||||
|
||||
export function compileShotAssetReferencePrompt(references: ShotPromptAssetReference[]) {
|
||||
if (!references.length) return "";
|
||||
const lines = references.flatMap((reference) => {
|
||||
const visualLabel = reference.category === "character" ? "人物参考" : `${projectAssetCategoryLabel(reference.category)}参考`;
|
||||
const media = [`${visualLabel}:图片${reference.imageIndex}`, reference.audioIndex ? `声音参考:音频${reference.audioIndex}` : ""].filter(Boolean).join(";");
|
||||
return [
|
||||
`- ${reference.title}:${media}`,
|
||||
reference.voiceDescription ? ` 声音说明:${reference.voiceDescription}` : "",
|
||||
].filter(Boolean);
|
||||
});
|
||||
return ["【资产参考】", "", ...lines].join("\n");
|
||||
}
|
||||
|
||||
function projectAssetReferenceImage(asset: ProjectAsset): ReferenceImage | undefined {
|
||||
@@ -87,3 +146,22 @@ function projectAssetReferenceImage(asset: ProjectAsset): ReferenceImage | undef
|
||||
function projectAssetCategory(value: string): AssetCategory {
|
||||
return assetCategories.has(value as AssetCategory) ? value as AssetCategory : "other";
|
||||
}
|
||||
|
||||
function projectAssetCategoryLabel(value: string) {
|
||||
return ({ environment: "场景", wardrobe: "服装", prop: "道具", weapon: "武器", style: "风格", other: "资产" } as Record<string, string>)[value] || "资产";
|
||||
}
|
||||
|
||||
function characterVoiceDescription(asset: ProjectAsset) {
|
||||
const definition = asset.character?.definition || {};
|
||||
const voice = asset.character?.voice;
|
||||
return [
|
||||
stringValue(definition.voiceLanguage) || stringValue(voice?.profile.language),
|
||||
stringValue(definition.voiceAge),
|
||||
stringValue(definition.voiceTimbre) || stringValue(voice?.profile.timbre),
|
||||
stringValue(voice?.instructions),
|
||||
].filter(Boolean).join(";");
|
||||
}
|
||||
|
||||
function stringValue(value: unknown) {
|
||||
return typeof value === "string" ? value.trim() : "";
|
||||
}
|
||||
Reference in new issue
Block a user