Add asset-pack Image v2 API for Family Board stickers.
POST /api/assets/generate wraps Flux/Krea generate with sticker/panel/icon presets; GET /api/assets/{id} returns status and PNG URLs using the existing poll path.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
/**
|
||||
* Asset-pack status. Same job ids as Image v2.
|
||||
* Poll: GET /api/assets/{id} (or GET /api/generate/{id}).
|
||||
* On complete, includes downloadable still URL(s) under /api/library/stills/{stillId}.
|
||||
*/
|
||||
export default defineEventHandler(async (event) => {
|
||||
const id = String(getRouterParam(event, 'id') || '').trim()
|
||||
if (!id) throw createError({ statusCode: 400, statusMessage: 'Missing job id' })
|
||||
|
||||
const withUrls = (snap: Record<string, unknown>) => {
|
||||
const stillId = typeof snap.stillId === 'string' ? snap.stillId : ''
|
||||
const url = stillId ? `/api/library/stills/${stillId}` : null
|
||||
return {
|
||||
...snap,
|
||||
url,
|
||||
urls: url ? [url] : [],
|
||||
pngUrl: url,
|
||||
thumbUrl: url ? `${url}?thumb=1` : null,
|
||||
complete: snap.status === 'complete'
|
||||
}
|
||||
}
|
||||
|
||||
const live = getJob(id)
|
||||
if (live) return withUrls(jobSnapshot(live) as Record<string, unknown>)
|
||||
|
||||
const pending = readPendingJob(id)
|
||||
if (pending) {
|
||||
const done = await resolvePendingJob(pending).catch(() => null)
|
||||
if (done) return withUrls(done as Record<string, unknown>)
|
||||
const job = ensurePendingWatch(pending)
|
||||
return withUrls(jobSnapshot(job) as Record<string, unknown>)
|
||||
}
|
||||
|
||||
try {
|
||||
const owner = libraryOwnerKey(event)
|
||||
const studio = listStudioJobs(owner).find(item => item.id === id || item.liveJobId === id)
|
||||
if (studio) {
|
||||
const liveId = studio.liveJobId || ''
|
||||
const liveJob = liveId ? getJob(liveId) : undefined
|
||||
if (liveJob) {
|
||||
return {
|
||||
...withUrls(jobSnapshot(liveJob) as Record<string, unknown>),
|
||||
studioJobId: studio.id
|
||||
}
|
||||
}
|
||||
return {
|
||||
jobId: studio.id,
|
||||
studioJobId: studio.id,
|
||||
status: studio.status || 'queued',
|
||||
message: studio.error || 'Waiting in the studio queue',
|
||||
progress: 0,
|
||||
url: null,
|
||||
urls: [],
|
||||
pngUrl: null,
|
||||
thumbUrl: null,
|
||||
complete: studio.status === 'complete',
|
||||
error: studio.error || undefined
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
// fall through
|
||||
}
|
||||
|
||||
throw createError({ statusCode: 404, statusMessage: 'Job not found' })
|
||||
})
|
||||
@@ -0,0 +1,163 @@
|
||||
import { addStudioJob, kickStudioQueue, listStudioJobs, videoJobsBusy } from '~/server/utils/studioQueue'
|
||||
import { comfyConfigured } from '~/server/utils/comfy'
|
||||
import {
|
||||
IMAGE_V2_CFG_DEFAULT,
|
||||
IMAGE_V2_KREA_CFG,
|
||||
IMAGE_V2_KREA_STEPS,
|
||||
IMAGE_V2_STEPS_DEFAULT,
|
||||
IMAGE_V2_TURBO_CFG,
|
||||
IMAGE_V2_TURBO_STEPS,
|
||||
imageV2WorkflowFile
|
||||
} from '~/utils/imageV2'
|
||||
import { clampImageCfg, clampImageSteps } from '~/utils/generationPresets'
|
||||
import { persistLoraFields } from '~/server/utils/loras'
|
||||
import {
|
||||
assetPackSize,
|
||||
buildAssetPackNegative,
|
||||
buildAssetPackPrompt,
|
||||
parseAssetPackEngine,
|
||||
parseAssetPackStyle
|
||||
} from '~/utils/assetPack'
|
||||
|
||||
function parseBool(raw: unknown) {
|
||||
return raw === true || raw === 'true' || raw === '1' || raw === 1
|
||||
}
|
||||
|
||||
/**
|
||||
* Thin Family Board / asset-pack wrapper around Image v2 generate.
|
||||
* POST /api/assets/generate → same studio queue + poll as /api/v2/generate.
|
||||
*/
|
||||
export default defineEventHandler(async (event) => {
|
||||
const body = await readBody<Record<string, unknown>>(event).catch(() => ({}))
|
||||
const promptBody = String(body?.prompt || '').trim()
|
||||
if (!promptBody) {
|
||||
throw createError({ statusCode: 400, statusMessage: 'A prompt is required' })
|
||||
}
|
||||
if (!comfyConfigured()) {
|
||||
throw createError({
|
||||
statusCode: 503,
|
||||
statusMessage: 'Beast ComfyUI is not configured. Set COMFY_HOST.'
|
||||
})
|
||||
}
|
||||
|
||||
const style = parseAssetPackStyle(body?.style, 'sticker')
|
||||
const engine = parseAssetPackEngine(body?.engine ?? body?.Engine, 'flux')
|
||||
const transparentFriendly = body?.transparent_friendly === undefined
|
||||
? style === 'sticker' || style === 'icon'
|
||||
: parseBool(body.transparent_friendly)
|
||||
const size = assetPackSize(style, body?.width, body?.height)
|
||||
const prompt = buildAssetPackPrompt({
|
||||
prompt: promptBody,
|
||||
style,
|
||||
transparentFriendly
|
||||
})
|
||||
const negative = buildAssetPackNegative(
|
||||
String(body?.negative_prompt ?? body?.negative ?? '').trim()
|
||||
)
|
||||
const turbo = parseBool(body?.turbo)
|
||||
const steps = engine === 'krea'
|
||||
? clampImageSteps(body?.steps, IMAGE_V2_KREA_STEPS)
|
||||
: turbo ? IMAGE_V2_TURBO_STEPS : clampImageSteps(body?.steps, IMAGE_V2_STEPS_DEFAULT)
|
||||
const cfg = engine === 'krea'
|
||||
? clampImageCfg(body?.cfg, IMAGE_V2_KREA_CFG)
|
||||
: turbo ? IMAGE_V2_TURBO_CFG : clampImageCfg(body?.cfg, IMAGE_V2_CFG_DEFAULT)
|
||||
const seed = body?.seed != null && String(body.seed) !== '' && String(body.seed) !== 'random'
|
||||
? Number(body.seed)
|
||||
: Math.floor(Math.random() * 2_147_483_647)
|
||||
if (!Number.isFinite(seed)) {
|
||||
throw createError({ statusCode: 400, statusMessage: 'seed must be a number or null' })
|
||||
}
|
||||
|
||||
const ownerKey = libraryOwnerKey(event)
|
||||
const library = publicLibrary(event)
|
||||
const folderId = library.folders.some(folder => folder.id === String(body?.folderId || ''))
|
||||
? String(body.folderId)
|
||||
: library.folders[0]?.id
|
||||
if (!folderId) {
|
||||
throw createError({ statusCode: 400, statusMessage: 'Create a library folder before generating' })
|
||||
}
|
||||
assertFolderExists(event, folderId)
|
||||
const destFolder = library.folders.find(folder => folder.id === folderId)
|
||||
const folderLocked = Boolean(destFolder?.protected && !destFolder.unlocked)
|
||||
const clipName = allocateOutputName(ownerKey, folderId, String(body?.name || `asset-${style}`).trim(), {
|
||||
engine,
|
||||
loraStack: []
|
||||
})
|
||||
|
||||
const studio = await addStudioJob({
|
||||
ownerKey,
|
||||
familyId: crypto.randomUUID(),
|
||||
kind: 'edit',
|
||||
payload: {
|
||||
prompt,
|
||||
promptMid: prompt,
|
||||
name: clipName,
|
||||
folderId,
|
||||
aspect: '1:1',
|
||||
width: size.width,
|
||||
height: size.height,
|
||||
steps,
|
||||
turbo,
|
||||
seed,
|
||||
cfg,
|
||||
fps: 24,
|
||||
samplerName: 'euler',
|
||||
scheduler: 'simple',
|
||||
duration: 0,
|
||||
sound: false,
|
||||
workflow: 'v1',
|
||||
useIdentityRefs: false,
|
||||
hideThumbnail: false,
|
||||
hideInput: false,
|
||||
folderLocked,
|
||||
referenceStillIds: [null, null, null, null],
|
||||
extensions: [],
|
||||
queueAutoRun: false,
|
||||
negative,
|
||||
passes: [],
|
||||
passMode: 'batch',
|
||||
scaleToTotalPixels: false,
|
||||
scaleMegapixels: 0,
|
||||
imagePipeline: 'v2',
|
||||
v2Mode: 'generate',
|
||||
v2Task: 't2i',
|
||||
engine,
|
||||
snofsModel: 0,
|
||||
snofsClip: 0,
|
||||
consistencyModel: 0,
|
||||
consistencyClip: 0,
|
||||
...persistLoraFields([])
|
||||
}
|
||||
})
|
||||
await kickStudioQueue()
|
||||
let latest = listStudioJobs(ownerKey).find(item => item.id === studio.id)
|
||||
if (!latest?.liveJobId && !(await videoJobsBusy())) {
|
||||
await kickStudioQueue()
|
||||
latest = listStudioJobs(ownerKey).find(item => item.id === studio.id)
|
||||
}
|
||||
const liveJobId = latest?.liveJobId || ''
|
||||
const jobId = liveJobId || studio.id
|
||||
|
||||
return {
|
||||
jobId,
|
||||
studioJobId: studio.id,
|
||||
queued: !liveJobId,
|
||||
status: liveJobId ? 'queued' : 'queued',
|
||||
pollUrl: `/api/assets/${jobId}`,
|
||||
generatePollUrl: `/api/generate/${jobId}`,
|
||||
streamUrl: `/api/generate/${jobId}/stream`,
|
||||
seed,
|
||||
steps,
|
||||
cfg,
|
||||
mode: 'generate',
|
||||
task: 't2i',
|
||||
engine,
|
||||
style,
|
||||
transparent_friendly: transparentFriendly,
|
||||
workflow: imageV2WorkflowFile('generate', engine, { hasImage: false, hasReference: false }),
|
||||
width: size.width,
|
||||
height: size.height,
|
||||
prompt,
|
||||
negative
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user