diff --git a/lib/agents/psd-template-config.ts b/lib/agents/psd-template-config.ts index 0e4c436b5..b6a63bf14 100644 --- a/lib/agents/psd-template-config.ts +++ b/lib/agents/psd-template-config.ts @@ -2,7 +2,7 @@ import type { AgentPsdTextColorConfig } from "@/types/agent"; import type { AgentPsdTemplateSecondaryCategoryConfig } from "@/lib/agents/psd-template-secondary-category-config"; export const AGENT_PSD_TEMPLATES_KEY = "agent_psd_templates"; -export const AGENT_PSD_TEMPLATE_ASSET_VERSION = 5; +export const AGENT_PSD_TEMPLATE_ASSET_VERSION = 6; export type AgentPsdTemplateStatus = "active" | "disabled"; export type AgentPsdTemplateParseStatus = diff --git a/lib/agents/psd-template-parser.ts b/lib/agents/psd-template-parser.ts index a6403f207..98a82e93d 100644 --- a/lib/agents/psd-template-parser.ts +++ b/lib/agents/psd-template-parser.ts @@ -664,8 +664,7 @@ export async function preparePsdTemplateRenderAssets(params: { if ( !isAgentPsdImageLayerSlot(targetLayer) || - !targetLayer.isEditable || - (!params.forceRefreshLayerAssets && targetLayer.maskUrl) + !targetLayer.isEditable ) { const layerIndex = nextLayers.findIndex((layer) => layer.id === targetLayer.id); if (layerIndex >= 0 && isAgentPsdImageLayerSlot(nextLayers[layerIndex])) { @@ -677,45 +676,19 @@ export async function preparePsdTemplateRenderAssets(params: { continue; } - const pixelCount = entry.layer.width * entry.layer.height; - const maskRgba = new Uint8ClampedArray(pixelCount * 4); - let hasVisiblePixel = false; - - for (let index = 0; index < pixelCount; index += 1) { - const alpha = rgba[index * 4 + 3] || 0; - maskRgba[index * 4] = 255; - maskRgba[index * 4 + 1] = 255; - maskRgba[index * 4 + 2] = 255; - maskRgba[index * 4 + 3] = alpha; - if (alpha > 0) { - hasVisiblePixel = true; - } - } - - if (!hasVisiblePixel) { - continue; - } - - const maskBuffer = await rgbaToPngBuffer(maskRgba, entry.layer.width, entry.layer.height); - const uploaded = await uploadFile( - `agent-psd-mask-${params.templateId}-${sanitizeFileSegment(targetLayer.layerId, targetLayer.id)}.png`, - maskBuffer.buffer.slice( - maskBuffer.byteOffset, - maskBuffer.byteOffset + maskBuffer.byteLength - ) as ArrayBuffer - ); - const layerIndex = nextLayers.findIndex((layer) => layer.id === targetLayer.id); if (layerIndex >= 0 && isAgentPsdImageLayerSlot(nextLayers[layerIndex])) { nextLayers[layerIndex] = { ...nextLayers[layerIndex], contentBounds: visibleContentBounds, - maskUrl: uploaded.url, - maskPath: uploaded.pathname, + // Do not auto-reuse the original PSD image alpha as a hard replacement mask. + // Otherwise newly assigned images get trapped inside the source image silhouette. + maskUrl: undefined, + maskPath: undefined, }; } } catch (error) { - console.warn("[PSD Template] Failed to generate layer mask:", { + console.warn("[PSD Template] Failed to prepare editable image layer assets:", { layerId: targetLayer.layerId, error, }); diff --git a/lib/agents/psd-template-repair.ts b/lib/agents/psd-template-repair.ts index f967a5817..7f0d74797 100644 --- a/lib/agents/psd-template-repair.ts +++ b/lib/agents/psd-template-repair.ts @@ -22,10 +22,7 @@ function hasMissingLayerAssets(template: AgentPsdTemplate) { return template.layers.some( (layer) => !String(layer.previewUrl || "").trim() || - !String(layer.sourceUrl || "").trim() || - (isAgentPsdImageLayerSlot(layer) && - layer.isEditable && - !String(layer.maskUrl || "").trim()) + !String(layer.sourceUrl || "").trim() ); }