Restore Klein empty-latent reference sampling
This commit is contained in:
@@ -1,33 +1,37 @@
|
||||
import {sampleKleinSource} from './graphs.mjs'
|
||||
|
||||
export function kleinIdentityPlan(request) {
|
||||
if(request.engine!=='flux' || ['video','extend'].includes(request.mode)) return null
|
||||
const sourceId=request.mode==='generate' ? request.identityStillId || request.imageAId || '' : request.imageAId || request.identityStillId || ''
|
||||
const mode=['generate','iterate'].includes(request.mode) ? (sourceId?'edit':'generate') : request.mode
|
||||
return {sourceId,mode,denoise:request.lockFace!==false?.45:.65}
|
||||
return {sourceId,mode}
|
||||
}
|
||||
|
||||
/** Validate the sampler's actual latent connection, not merely the presence of a reference. */
|
||||
export function assertKleinSample(graph, sourceId) {
|
||||
let link=graph['19']?.inputs?.latent_image
|
||||
const visited=new Set()
|
||||
while(Array.isArray(link) && !visited.has(String(link[0]))) {
|
||||
const id=String(link[0]);visited.add(id)
|
||||
/** Follow the live conditioning chain, including optional hero/compose references. */
|
||||
function hasSourceReference(graph, link, referenceId) {
|
||||
const seen=new Set()
|
||||
while(Array.isArray(link) && !seen.has(String(link[0]))) {
|
||||
const id=String(link[0]);seen.add(id)
|
||||
const node=graph[id]
|
||||
if(node?.class_type==='VAEEncode' && id==='11') return 'source'
|
||||
if(node?.class_type?.startsWith('Empty')) {
|
||||
if(sourceId) throw new Error('Klein identity requires the source image latent; empty-latent sampling was rejected.')
|
||||
return 'empty'
|
||||
}
|
||||
link=node?.class_type==='SetLatentNoiseMask'?node.inputs.samples:null
|
||||
if(id===referenceId) return node?.class_type==='ReferenceLatent' && node.inputs.latent?.[0]==='11' && graph['11']?.class_type==='VAEEncode'
|
||||
link=node?.inputs?.conditioning
|
||||
}
|
||||
throw new Error('Klein sample latent could not be verified against the source still.')
|
||||
return false
|
||||
}
|
||||
|
||||
export function assertKleinSample(graph, sourceId) {
|
||||
const sampler=graph['19']?.inputs
|
||||
const latent=graph[sampler?.latent_image?.[0]]
|
||||
if(latent?.class_type==='EmptyFlux2LatentImage') {
|
||||
if(!sourceId) return 'empty'
|
||||
const guider=graph[sampler.guider?.[0]]?.inputs
|
||||
if(hasSourceReference(graph,guider?.positive,'12') && hasSourceReference(graph,guider?.negative,'13')) return 'empty+reference'
|
||||
throw new Error('Klein source reference conditioning is missing from the sampler.')
|
||||
}
|
||||
// Refine retains its dedicated masked-source workflow.
|
||||
if(latent?.class_type==='SetLatentNoiseMask' && latent.inputs.samples?.[0]==='11' && graph['11']?.class_type==='VAEEncode') return 'masked source'
|
||||
throw new Error('Klein edit must sample empty latent with source references, not the source image latent.')
|
||||
}
|
||||
|
||||
export function applyKleinIdentity(graph, plan) {
|
||||
if(plan.sourceId && plan.mode!=='refine') sampleKleinSource(graph,plan.denoise)
|
||||
// Retain the masked latent and any already lower refine denoise.
|
||||
if(plan.sourceId && plan.mode==='refine') graph['17'].inputs.denoise=Math.min(graph['17'].inputs.denoise,plan.denoise)
|
||||
const sampleLatent=assertKleinSample(graph,plan.sourceId)
|
||||
return {sampleLatent,sampleDenoise:sampleLatent==='source'?(graph.source_sigmas?.inputs.denoise ?? graph['17']?.inputs.denoise):null}
|
||||
return {sampleLatent,sampleDenoise:sampleLatent==='masked source' ? graph['17']?.inputs.denoise ?? null : null}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user