Add Image Iterate: nested multi-prompt still families.
Iterate runs a series of prompts as generate (no still), edit (still A), or compose (A+B), saves each output under one library collection like video extensions, and defaults to iterating from the original still.
This commit is contained in:
@@ -11,6 +11,7 @@ import { comfyJobPrefix } from '~/utils/outputNames'
|
||||
import { restorePromptParts, wrappedPromptForComfy } from '~/utils/promptParts'
|
||||
import { isXaigenStudio } from '~/utils/videoModels'
|
||||
import {
|
||||
imageV2GraphMode,
|
||||
imageV2StackSpecials,
|
||||
mergeLegacyImageV2Loras,
|
||||
parseImageV2Engine,
|
||||
@@ -52,20 +53,24 @@ export type EditV2RunParams = {
|
||||
export async function runEditV2(job: Job, params: EditV2RunParams) {
|
||||
const library = job.library
|
||||
if (!library) throw new Error('Edit job is missing library metadata')
|
||||
if (params.mode === 'refine' && !params.mask) {
|
||||
const graphMode = imageV2GraphMode(params.mode, {
|
||||
hasImage: Boolean(params.image),
|
||||
hasReference: Boolean(params.reference)
|
||||
})
|
||||
if (graphMode === 'refine' && !params.mask) {
|
||||
throw new Error('Refine requires a mask. Refusing to fall back to Edit.')
|
||||
}
|
||||
if (params.mode === 'compose' && !params.reference) {
|
||||
if (graphMode === 'compose' && !params.reference) {
|
||||
throw new Error('Compose requires image B. Refusing to fall back to one-image generation.')
|
||||
}
|
||||
if (params.mode === 'edit' && params.reference) {
|
||||
if (graphMode === 'edit' && params.reference && params.mode !== 'iterate') {
|
||||
throw new Error('Edit mode takes one image. Use Compose for two stills.')
|
||||
}
|
||||
if (params.mode !== 'generate' && !params.image) {
|
||||
if (graphMode !== 'generate' && !params.image) {
|
||||
throw new Error('This v2 mode requires still A.')
|
||||
}
|
||||
|
||||
const canChain = params.mode === 'edit' || params.mode === 'compose'
|
||||
const canChain = params.mode === 'iterate' || params.mode === 'edit' || params.mode === 'compose'
|
||||
const extraPasses = canChain
|
||||
? (params.passes || []).map(item => ({ prompt: String(item.prompt || '').trim() })).filter(item => item.prompt)
|
||||
: []
|
||||
@@ -78,8 +83,9 @@ export async function runEditV2(job: Job, params: EditV2RunParams) {
|
||||
...extraPasses.map(item => item.prompt)
|
||||
]
|
||||
const chainTotal = prompts.length
|
||||
const batch = params.passMode !== 'chain'
|
||||
const unit = batch ? 'Batch' : 'Pass'
|
||||
// Text-only iterate is always independent generations. With a still, honor batch vs chain.
|
||||
const batch = graphMode === 'generate' || params.passMode !== 'chain'
|
||||
const unit = params.mode === 'iterate' ? 'Iteration' : (batch ? 'Batch' : 'Pass')
|
||||
library.chainTotal = chainTotal
|
||||
library.familyId = chainTotal > 1 ? (library.familyId || crypto.randomUUID()) : library.familyId
|
||||
|
||||
@@ -96,7 +102,7 @@ export async function runEditV2(job: Job, params: EditV2RunParams) {
|
||||
job.imageComfyHost = getComfyHost()
|
||||
|
||||
await withImageComfyHost(job.imageComfyHost, async () => {
|
||||
const generate = params.mode === 'generate'
|
||||
const generate = graphMode === 'generate'
|
||||
const engine = parseImageV2Engine(params.engine, 'flux')
|
||||
const engineLabel = engine === 'krea' ? 'Krea' : 'Flux'
|
||||
const { applyImageFill } = await import('~/server/utils/imagePad')
|
||||
@@ -113,7 +119,7 @@ export async function runEditV2(job: Job, params: EditV2RunParams) {
|
||||
const krea = engine === 'krea' ? await resolveKreaGenerateAssets() : null
|
||||
const appliedStack = mergeLegacyImageV2Loras(params.loraStack?.length ? params.loraStack : library.loraStack, {
|
||||
engine,
|
||||
mode: params.mode,
|
||||
mode: graphMode,
|
||||
xaigen: isXaigenStudio(),
|
||||
snofsModel: params.snofsModel,
|
||||
snofsClip: params.snofsClip,
|
||||
@@ -127,7 +133,7 @@ export async function runEditV2(job: Job, params: EditV2RunParams) {
|
||||
const consistencyModel = appliedSpecials.consistency?.strengthModel ?? params.consistencyModel
|
||||
const consistencyClip = appliedSpecials.consistency?.strengthClip ?? params.consistencyClip
|
||||
|
||||
if (!generate && params.mode === 'compose' && params.reference) {
|
||||
if (!generate && graphMode === 'compose' && params.reference) {
|
||||
uploadedRef = await uploadImage({
|
||||
...params.reference,
|
||||
filename: `ref_${params.reference.filename || 'image_b.png'}`
|
||||
@@ -163,17 +169,17 @@ export async function runEditV2(job: Job, params: EditV2RunParams) {
|
||||
emitChainJob(job, {
|
||||
type: 'status',
|
||||
message: generate
|
||||
? `Queueing ${engineLabel} generate on Beast...`
|
||||
: params.mode === 'refine'
|
||||
? `Queueing ${engineLabel} iterate generate on Beast...`
|
||||
: graphMode === 'refine'
|
||||
? 'Uploading canvas and mask to Beast...'
|
||||
: params.mode === 'compose' ? 'Uploading stills A and B to Beast...' : 'Uploading still A to Beast...',
|
||||
: graphMode === 'compose' ? 'Uploading stills A and B to Beast...' : 'Uploading still A to Beast...',
|
||||
progress: generate ? 12 : 8
|
||||
})
|
||||
|
||||
if (index === 0 && !generate && image) {
|
||||
const filled = await applyImageFill({
|
||||
image,
|
||||
mask: params.mode === 'refine' ? mask : null,
|
||||
mask: graphMode === 'refine' ? mask : null,
|
||||
aspect: params.aspect || 'auto'
|
||||
})
|
||||
image = filled.image
|
||||
@@ -199,7 +205,7 @@ export async function runEditV2(job: Job, params: EditV2RunParams) {
|
||||
prompt = composeFillPrompt(prompt, true)
|
||||
}
|
||||
|
||||
if (index === 0 && params.mode === 'refine' && mask && !uploadedMask) {
|
||||
if (index === 0 && graphMode === 'refine' && mask && !uploadedMask) {
|
||||
uploadedMask = await uploadImage({
|
||||
...mask,
|
||||
filename: `mask_${mask.filename || 'refine-mask.png'}`
|
||||
@@ -212,19 +218,21 @@ export async function runEditV2(job: Job, params: EditV2RunParams) {
|
||||
if (!generate) {
|
||||
emitChainJob(job, {
|
||||
type: 'status',
|
||||
message: params.mode === 'refine'
|
||||
? `Queueing ${engineLabel} v2 refine on Beast...`
|
||||
: params.mode === 'compose'
|
||||
? `Queueing ${engineLabel} v2 compose (${params.task}) on Beast...`
|
||||
: `Queueing ${engineLabel} v2 edit on Beast...`,
|
||||
message: params.mode === 'iterate'
|
||||
? `Queueing ${engineLabel} iterate (${graphMode}) on Beast...`
|
||||
: graphMode === 'refine'
|
||||
? `Queueing ${engineLabel} v2 refine on Beast...`
|
||||
: graphMode === 'compose'
|
||||
? `Queueing ${engineLabel} v2 compose (${params.task}) on Beast...`
|
||||
: `Queueing ${engineLabel} v2 edit on Beast...`,
|
||||
progress: 12
|
||||
})
|
||||
}
|
||||
|
||||
const built = buildImageV2Workflow({
|
||||
mode: params.mode,
|
||||
mode: graphMode,
|
||||
engine,
|
||||
task: params.task,
|
||||
task: generate ? 't2i' : params.task,
|
||||
prompt: wrappedPromptForComfy(library, prompt, index === 0 ? params.prompt : prompt),
|
||||
negative: params.negative,
|
||||
imageAName: uploaded?.name,
|
||||
@@ -305,7 +313,7 @@ export async function runEditV2(job: Job, params: EditV2RunParams) {
|
||||
kind: 'imagev2',
|
||||
mode: params.mode,
|
||||
engine,
|
||||
task: params.task,
|
||||
task: generate ? 't2i' : params.task,
|
||||
negative: params.negative,
|
||||
snofsModel,
|
||||
snofsClip,
|
||||
@@ -313,12 +321,12 @@ export async function runEditV2(job: Job, params: EditV2RunParams) {
|
||||
consistencyClip,
|
||||
steps: params.steps,
|
||||
cfg: params.cfg,
|
||||
megapixels: params.mode === 'generate' || !(params.megapixels > 0) ? undefined : params.megapixels,
|
||||
scaleToTotalPixels: params.mode === 'generate' ? undefined : params.megapixels > 0,
|
||||
megapixels: generate || !(params.megapixels > 0) ? undefined : params.megapixels,
|
||||
scaleToTotalPixels: generate ? undefined : params.megapixels > 0,
|
||||
turbo: params.turbo === true,
|
||||
strength: params.mode === 'generate' ? undefined : params.strength,
|
||||
width: params.mode === 'generate' ? params.width : undefined,
|
||||
height: params.mode === 'generate' ? params.height : undefined,
|
||||
strength: generate ? undefined : params.strength,
|
||||
width: generate ? params.width : undefined,
|
||||
height: generate ? params.height : undefined,
|
||||
aspect: params.aspect || 'auto',
|
||||
seed,
|
||||
sourceStillId: params.sourceStillId || library.stillId,
|
||||
@@ -362,7 +370,7 @@ export async function runEditV2(job: Job, params: EditV2RunParams) {
|
||||
emitChainJob(job, {
|
||||
type: 'complete',
|
||||
message: chainTotal > 1
|
||||
? `${engineLabel} v2 ${batch ? 'batch' : 'chain'} finished on Beast`
|
||||
? `${engineLabel} ${params.mode === 'iterate' ? 'iterate' : `v2 ${batch ? 'batch' : 'chain'}`} finished on Beast`
|
||||
: `${engineLabel} v2 finished on Beast`,
|
||||
progress: 100,
|
||||
stillId: still?.id,
|
||||
|
||||
+33
-26
@@ -58,7 +58,7 @@ export interface StudioJobPayload {
|
||||
scaleToTotalPixels?: boolean
|
||||
scaleMegapixels?: number
|
||||
imagePipeline?: 'v1' | 'v2'
|
||||
v2Mode?: 'edit' | 'compose' | 'refine' | 'generate'
|
||||
v2Mode?: 'edit' | 'compose' | 'refine' | 'generate' | 'iterate'
|
||||
v2Task?: 'scene' | 'identity' | 'outfit' | 'face_lock' | 'refine' | 't2i'
|
||||
engine?: 'flux' | 'krea'
|
||||
snofsModel?: number
|
||||
@@ -1017,7 +1017,8 @@ async function startStudioEditJob(item: StudioJob) {
|
||||
const { runEditV2 } = await import('~/server/utils/imageChainV2')
|
||||
|
||||
const payload = item.payload
|
||||
const generate = payload.imagePipeline === 'v2' && payload.v2Mode === 'generate'
|
||||
const iterate = payload.imagePipeline === 'v2' && payload.v2Mode === 'iterate'
|
||||
const generate = payload.imagePipeline === 'v2' && (payload.v2Mode === 'generate' || (iterate && !payload.stillId))
|
||||
if (!generate && (!payload.stillId || !existsSync(stillPath(item.ownerKey, payload.stillId)))) {
|
||||
throw new Error('The input still is missing from the library')
|
||||
}
|
||||
@@ -1038,30 +1039,36 @@ async function startStudioEditJob(item: StudioJob) {
|
||||
: null
|
||||
|
||||
if (payload.imagePipeline === 'v2') {
|
||||
const mode = payload.v2Mode === 'compose'
|
||||
? 'compose'
|
||||
: payload.v2Mode === 'refine'
|
||||
? 'refine'
|
||||
: payload.v2Mode === 'generate'
|
||||
? 'generate'
|
||||
: 'edit'
|
||||
const mode = payload.v2Mode === 'iterate'
|
||||
? 'iterate'
|
||||
: payload.v2Mode === 'compose'
|
||||
? 'compose'
|
||||
: payload.v2Mode === 'refine'
|
||||
? 'refine'
|
||||
: payload.v2Mode === 'generate'
|
||||
? 'generate'
|
||||
: 'edit'
|
||||
const graphMode = mode === 'iterate'
|
||||
? (image ? (reference ? 'compose' : 'edit') : 'generate')
|
||||
: mode
|
||||
const maskId = payload.maskStillId
|
||||
const mask = mode === 'refine' && maskId && existsSync(stillPath(item.ownerKey, maskId))
|
||||
const mask = graphMode === 'refine' && maskId && existsSync(stillPath(item.ownerKey, maskId))
|
||||
? {
|
||||
filename: payload.maskStillFilename || 'refine-mask.png',
|
||||
data: readFileSync(stillPath(item.ownerKey, maskId)),
|
||||
type: 'image/png'
|
||||
}
|
||||
: null
|
||||
if (mode === 'refine' && !mask) {
|
||||
if (graphMode === 'refine' && !mask) {
|
||||
throw new Error('Refine requires a mask. Refusing to fall back to Edit.')
|
||||
}
|
||||
if (mode === 'compose' && !reference) {
|
||||
if (graphMode === 'compose' && !reference) {
|
||||
throw new Error('Compose requires image B. Refusing to fall back to one-image generation.')
|
||||
}
|
||||
if (mode === 'edit' && reference) {
|
||||
throw new Error('Edit mode takes one image. Use Compose for two stills.')
|
||||
}
|
||||
const passCount = (mode === 'edit' || mode === 'compose' || mode === 'iterate') ? (payload.passes?.length || 0) : 0
|
||||
live = createEditLiveJob({
|
||||
steps: payload.steps,
|
||||
hideThumbnail: payload.hideThumbnail,
|
||||
@@ -1088,9 +1095,9 @@ async function startStudioEditJob(item: StudioJob) {
|
||||
familyId: item.familyId,
|
||||
chainIndex: 0,
|
||||
chainStep: 1,
|
||||
chainTotal: 1 + ((mode === 'edit' || mode === 'compose') ? (payload.passes?.length || 0) : 0),
|
||||
chainLabel: (mode === 'edit' || mode === 'compose') && payload.passes?.length
|
||||
? (payload.passMode === 'chain' ? 'Pass 1' : 'Batch 1')
|
||||
chainTotal: 1 + passCount,
|
||||
chainLabel: passCount
|
||||
? (mode === 'iterate' ? 'Iteration 1' : (payload.passMode === 'chain' ? 'Pass 1' : 'Batch 1'))
|
||||
: undefined,
|
||||
passes: payload.passes,
|
||||
...persistLoraFields(payload.loraStack || payload.loraName)
|
||||
@@ -1102,30 +1109,30 @@ async function startStudioEditJob(item: StudioJob) {
|
||||
void runEditV2(live, {
|
||||
mode,
|
||||
engine: v2Engine,
|
||||
task: payload.v2Task || 'scene',
|
||||
image: mode === 'generate' ? null : image,
|
||||
reference: mode === 'refine' || mode === 'generate' ? null : reference,
|
||||
task: payload.v2Task || (graphMode === 'generate' ? 't2i' : 'scene'),
|
||||
image: graphMode === 'generate' ? null : image,
|
||||
reference: graphMode === 'compose' ? reference : null,
|
||||
mask,
|
||||
prompt: payload.prompt,
|
||||
passes: payload.passes || [],
|
||||
passMode: payload.passMode === 'chain' ? 'chain' : 'batch',
|
||||
negative: payload.negative || '',
|
||||
steps: payload.steps,
|
||||
seed: live.library?.seed || payload.seed,
|
||||
seed: payload.seed || Math.floor(Math.random() * 2_147_483_647),
|
||||
cfg: payload.cfg,
|
||||
snofsModel: v2Specials.snofs?.strengthModel ?? payload.snofsModel ?? 0,
|
||||
snofsClip: v2Specials.snofs?.strengthClip ?? payload.snofsClip ?? 0,
|
||||
consistencyModel: v2Specials.consistency?.strengthModel ?? payload.consistencyModel ?? (mode === 'generate' || payload.engine === 'krea' ? 0 : 0.7),
|
||||
consistencyClip: v2Specials.consistency?.strengthClip ?? payload.consistencyClip ?? (mode === 'generate' || payload.engine === 'krea' ? 0 : 0.7),
|
||||
megapixels: payload.scaleToTotalPixels === false ? 0 : (payload.scaleMegapixels ?? 1),
|
||||
consistencyModel: v2Specials.consistency?.strengthModel ?? payload.consistencyModel ?? 0,
|
||||
consistencyClip: v2Specials.consistency?.strengthClip ?? payload.consistencyClip ?? 0,
|
||||
megapixels: payload.scaleMegapixels || 0,
|
||||
strength: payload.refineStrength,
|
||||
aspect: payload.aspect || 'auto',
|
||||
width: payload.width,
|
||||
height: payload.height,
|
||||
turbo: payload.turbo === true,
|
||||
aspect: payload.aspect,
|
||||
sourceStillId: payload.stillId,
|
||||
referenceStillId: payload.referenceStillId,
|
||||
loraStack: payload.loraStack
|
||||
loraStack: payload.loraStack,
|
||||
passes: payload.passes,
|
||||
passMode: payload.passMode === 'chain' ? 'chain' : 'batch',
|
||||
}).catch((error) => {
|
||||
const message = error instanceof Error ? error.message : String(error)
|
||||
if (live && live.status !== 'error' && live.status !== 'cancelled' && live.status !== 'deferred') {
|
||||
|
||||
Reference in New Issue
Block a user