Stop polling the library catalog and keep the stage on thumbs.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -3,29 +3,21 @@ import { publicLibrary, publicLibraryPage } from '~/server/utils/library'
|
||||
export default defineEventHandler((event) => {
|
||||
const query = getQuery(event)
|
||||
const light = String(query.light || '') === '1'
|
||||
const full = String(query.full || '') === '1'
|
||||
const limitRaw = query.limit
|
||||
const wantsPage = limitRaw != null && String(limitRaw) !== ''
|
||||
const hasLimit = limitRaw != null && String(limitRaw) !== ''
|
||||
// Default GET /api/library → page helper (limit 50). Opt into full catalog with full=1 or legacy light=1.
|
||||
const wantsFull = full || (light && !hasLimit)
|
||||
|
||||
if (wantsPage) {
|
||||
const page = publicLibraryPage(event, {
|
||||
limit: Number(limitRaw) || 50,
|
||||
if (!wantsFull) {
|
||||
return publicLibraryPage(event, {
|
||||
limit: hasLimit ? Number(limitRaw) || 50 : 50,
|
||||
cursor: String(query.cursor || ''),
|
||||
folderId: String(query.folderId || query.folder || ''),
|
||||
type: String(query.type || query.filter || 'all')
|
||||
})
|
||||
try {
|
||||
const { owner } = assertLibraryOwner(event)
|
||||
const folderId = page.folders.find(folder => folder.unlocked)?.id
|
||||
void maintainLibrary(owner, folderId).catch(() => null)
|
||||
} catch { /* locked or anonymous */ }
|
||||
return page
|
||||
}
|
||||
|
||||
const library = publicLibrary(event, { light })
|
||||
try {
|
||||
const { owner } = assertLibraryOwner(event)
|
||||
const folderId = library.folders.find(folder => folder.unlocked)?.id
|
||||
void maintainLibrary(owner, folderId).catch(() => null)
|
||||
} catch { /* locked or anonymous — still return what we can */ }
|
||||
return library
|
||||
// Full catalog path — no thumb prewarm on the request (thumbs build on /thumb hit)
|
||||
return publicLibrary(event, { light: true })
|
||||
})
|
||||
|
||||
@@ -25,11 +25,18 @@ export default defineEventHandler(async (event) => {
|
||||
assertFolderAccess(event, getStill(owner, id).folderId);
|
||||
if (request.startClipId)
|
||||
assertFolderAccess(event, getClip(owner, request.startClipId).folderId);
|
||||
// Header-only size probe — never decode the archive PNG on Generate click
|
||||
await resolveRequestSize(owner,request);
|
||||
const id = crypto.randomUUID(), record = { id, owner, request, state: 'waiting', queuedAt: Date.now(), familyId: crypto.randomUUID(), outputs: [], purgeResult: 'Not yet saved' };
|
||||
saveRecord(record);
|
||||
const row = await addStudioJob({ ownerKey: owner, kind: ['video', 'extend'].includes(request.mode) ? 'video' : 'edit', familyId: record.familyId, payload: { ...request.settings, studio2Id: id, studio2Request: structuredClone(request), prompt: request.compiledPrompt, folderId: request.folderId, extensions: [], referenceStillIds: [], useIdentityRefs: false, queueAutoRun: true } as StudioJobPayload });
|
||||
saveRecord({ ...readRecord(owner, id), queueId: row.id });
|
||||
kickStudioQueue();
|
||||
return { id, seed:request.settings.seed, width:request.settings.width, height:request.settings.height };
|
||||
const ack = { id, seed:request.settings.seed, width:request.settings.width, height:request.settings.height };
|
||||
// Enqueue + kick after the client has the id — do not block the response on the worker wake path
|
||||
void (async () => {
|
||||
try {
|
||||
const row = await addStudioJob({ ownerKey: owner, kind: ['video', 'extend'].includes(request.mode) ? 'video' : 'edit', familyId: record.familyId, payload: { ...request.settings, studio2Id: id, studio2Request: structuredClone(request), prompt: request.compiledPrompt, folderId: request.folderId, extensions: [], referenceStillIds: [], useIdentityRefs: false, queueAutoRun: true } as StudioJobPayload });
|
||||
saveRecord({ ...readRecord(owner, id), queueId: row.id });
|
||||
} catch { /* poll / resume will pick up waiting studio2 records */ }
|
||||
kickStudioQueue();
|
||||
})();
|
||||
return ack;
|
||||
});
|
||||
|
||||
+105
-62
@@ -615,7 +615,7 @@ export function publicLibrary(event: H3Event, opts?: { light?: boolean }) {
|
||||
}
|
||||
}
|
||||
|
||||
/** Paginated metadata page — no file bytes, no existsSync join of the volume. */
|
||||
/** Paginated metadata page — reads catalog rows only; no existsSync / full publicLibrary map. */
|
||||
export function publicLibraryPage(
|
||||
event: H3Event,
|
||||
opts: { limit?: number; cursor?: string; folderId?: string; type?: string } = {}
|
||||
@@ -624,7 +624,30 @@ export function publicLibraryPage(
|
||||
const cursor = String(opts.cursor || '')
|
||||
const folderId = String(opts.folderId || '')
|
||||
const typeFilter = String(opts.type || 'all')
|
||||
const full = publicLibrary(event, { light: true })
|
||||
const owner = libraryOwnerKey(event)
|
||||
const catalog = readCatalog(owner)
|
||||
const clipIds = new Set(catalog.clips.map(clip => clip.id.toLowerCase()))
|
||||
const clipCounts = new Map<string, number>()
|
||||
const imageCounts = new Map<string, number>()
|
||||
const trackCounts = new Map<string, number>()
|
||||
for (const clip of catalog.clips) {
|
||||
clipCounts.set(clip.folderId, (clipCounts.get(clip.folderId) || 0) + 1)
|
||||
}
|
||||
for (const still of catalog.stills) {
|
||||
if (isPipelineStill(catalog, still, clipIds) || stillRole(still) !== 'output') continue
|
||||
imageCounts.set(still.folderId, (imageCounts.get(still.folderId) || 0) + 1)
|
||||
}
|
||||
for (const track of catalog.tracks || []) {
|
||||
trackCounts.set(track.folderId, (trackCounts.get(track.folderId) || 0) + 1)
|
||||
}
|
||||
const folders = catalog.folders.map(folder => publicFolder(
|
||||
event,
|
||||
folder,
|
||||
clipCounts.get(folder.id) || 0,
|
||||
imageCounts.get(folder.id) || 0,
|
||||
trackCounts.get(folder.id) || 0
|
||||
))
|
||||
const unlockedIds = new Set(folders.filter(folder => folder.unlocked).map(folder => folder.id))
|
||||
|
||||
type Row = {
|
||||
id: string
|
||||
@@ -646,57 +669,64 @@ export function publicLibraryPage(
|
||||
}
|
||||
|
||||
const rows: Row[] = []
|
||||
for (const still of full.stills) {
|
||||
if (folderId && folderId !== 'all' && still.folderId !== folderId) continue
|
||||
if (typeFilter === 'videos' || typeFilter === 'music') continue
|
||||
rows.push({
|
||||
id: still.id,
|
||||
name: still.name || still.filename || still.id,
|
||||
type: 'still',
|
||||
createdAt: still.createdAt || 0,
|
||||
thumbUrl: `/api/library/stills/${still.id}/thumb`,
|
||||
folderId: still.folderId,
|
||||
prompt: still.prompt,
|
||||
promptPre: still.promptPre,
|
||||
promptPost: still.promptPost,
|
||||
width: still.width,
|
||||
height: still.height,
|
||||
role: still.role,
|
||||
hideInput: still.hideInput,
|
||||
filename: still.filename
|
||||
})
|
||||
if (typeFilter !== 'videos' && typeFilter !== 'music') {
|
||||
for (const still of catalog.stills) {
|
||||
if (!unlockedIds.has(still.folderId)) continue
|
||||
if (folderId && folderId !== 'all' && still.folderId !== folderId) continue
|
||||
if (isPipelineStill(catalog, still, clipIds)) continue
|
||||
rows.push({
|
||||
id: still.id,
|
||||
name: still.name || still.filename || still.id,
|
||||
type: 'still',
|
||||
createdAt: still.createdAt || 0,
|
||||
thumbUrl: `/api/library/stills/${still.id}/thumb`,
|
||||
folderId: still.folderId,
|
||||
prompt: still.prompt,
|
||||
promptPre: still.promptPre,
|
||||
promptPost: still.promptPost,
|
||||
width: still.width,
|
||||
height: still.height,
|
||||
role: stillRole(still),
|
||||
hideInput: still.hideInput,
|
||||
filename: still.filename
|
||||
})
|
||||
}
|
||||
}
|
||||
for (const clip of full.clips) {
|
||||
if (folderId && folderId !== 'all' && clip.folderId !== folderId) continue
|
||||
if (typeFilter === 'images' || typeFilter === 'music') continue
|
||||
rows.push({
|
||||
id: clip.id,
|
||||
name: clip.name || clip.id,
|
||||
type: 'clip',
|
||||
createdAt: clip.createdAt || 0,
|
||||
thumbUrl: `/api/library/clips/${clip.id}/thumb`,
|
||||
folderId: clip.folderId,
|
||||
prompt: clip.prompt,
|
||||
promptPre: clip.promptPre,
|
||||
promptPost: clip.promptPost,
|
||||
width: clip.width,
|
||||
height: clip.height,
|
||||
duration: clip.duration
|
||||
})
|
||||
if (typeFilter !== 'images' && typeFilter !== 'music') {
|
||||
for (const clip of catalog.clips) {
|
||||
if (!unlockedIds.has(clip.folderId)) continue
|
||||
if (folderId && folderId !== 'all' && clip.folderId !== folderId) continue
|
||||
rows.push({
|
||||
id: clip.id,
|
||||
name: clip.name || clip.id,
|
||||
type: 'clip',
|
||||
createdAt: clip.createdAt || 0,
|
||||
thumbUrl: `/api/library/clips/${clip.id}/thumb`,
|
||||
folderId: clip.folderId,
|
||||
prompt: clip.prompt,
|
||||
promptPre: clip.promptPre,
|
||||
promptPost: clip.promptPost,
|
||||
width: clip.width,
|
||||
height: clip.height,
|
||||
duration: clip.duration
|
||||
})
|
||||
}
|
||||
}
|
||||
for (const track of full.tracks || []) {
|
||||
if (folderId && folderId !== 'all' && track.folderId !== folderId) continue
|
||||
if (typeFilter === 'images' || typeFilter === 'videos') continue
|
||||
rows.push({
|
||||
id: track.id,
|
||||
name: track.name || track.id,
|
||||
type: 'track',
|
||||
createdAt: track.createdAt || 0,
|
||||
thumbUrl: '',
|
||||
folderId: track.folderId,
|
||||
duration: track.duration,
|
||||
ext: track.ext
|
||||
})
|
||||
if (typeFilter !== 'images' && typeFilter !== 'videos') {
|
||||
for (const track of catalog.tracks || []) {
|
||||
if (!unlockedIds.has(track.folderId)) continue
|
||||
if (folderId && folderId !== 'all' && track.folderId !== folderId) continue
|
||||
rows.push({
|
||||
id: track.id,
|
||||
name: track.name || track.id,
|
||||
type: 'track',
|
||||
createdAt: track.createdAt || 0,
|
||||
thumbUrl: '',
|
||||
folderId: track.folderId,
|
||||
duration: track.duration,
|
||||
ext: track.ext
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
rows.sort((a, b) => (b.createdAt || 0) - (a.createdAt || 0) || String(b.id).localeCompare(String(a.id)))
|
||||
@@ -711,21 +741,27 @@ export function publicLibraryPage(
|
||||
const nextCursor = page.length === limit && last ? `${last.type}:${last.id}` : null
|
||||
|
||||
const stillIds = new Set(page.filter(r => r.type === 'still').map(r => r.id))
|
||||
const clipIds = new Set(page.filter(r => r.type === 'clip').map(r => r.id))
|
||||
const clipIdPage = new Set(page.filter(r => r.type === 'clip').map(r => r.id))
|
||||
const trackIds = new Set(page.filter(r => r.type === 'track').map(r => r.id))
|
||||
|
||||
return {
|
||||
folders: full.folders,
|
||||
folders,
|
||||
items: page,
|
||||
nextCursor,
|
||||
limit,
|
||||
stills: full.stills.filter(s => stillIds.has(s.id)),
|
||||
clips: full.clips.filter(c => clipIds.has(c.id)),
|
||||
tracks: (full.tracks || []).filter(t => trackIds.has(t.id)),
|
||||
drafts: full.drafts,
|
||||
loraAliases: full.loraAliases,
|
||||
loraTriggers: full.loraTriggers,
|
||||
keepPrompts: full.keepPrompts,
|
||||
stills: catalog.stills
|
||||
.filter(s => stillIds.has(s.id))
|
||||
.map(still => ({ ...still, role: stillRole(still) })),
|
||||
clips: catalog.clips
|
||||
.filter(c => clipIdPage.has(c.id))
|
||||
.map(clip => ({ ...clip, name: clip.name || clip.id, hasThumbnail: !clip.hideThumbnail, hasVideo: true })),
|
||||
tracks: (catalog.tracks || [])
|
||||
.filter(t => trackIds.has(t.id))
|
||||
.map(track => ({ ...track, hasAudio: true })),
|
||||
drafts: catalog.drafts,
|
||||
loraAliases: catalog.loraAliases || {},
|
||||
loraTriggers: catalog.loraTriggers || {},
|
||||
keepPrompts: catalog.keepPrompts || emptyKeepPrompts(),
|
||||
total: rows.length
|
||||
}
|
||||
}
|
||||
@@ -1288,11 +1324,17 @@ export async function stillThumbPath(owner: string, id: string) {
|
||||
return build
|
||||
}
|
||||
|
||||
export async function ensureStillThumbnails(owner: string) {
|
||||
export async function ensureStillThumbnails(owner: string, opts?: { limit?: number }) {
|
||||
const catalog = readCatalog(owner)
|
||||
const cap = Math.max(0, Math.min(Number(opts?.limit) || 2, 8))
|
||||
let built = 0
|
||||
for (const still of catalog.stills) {
|
||||
if (built >= cap) break
|
||||
if (isPipelineStill(catalog, still)) continue
|
||||
const dest = stillThumbFile(owner, still.id)
|
||||
if (existsSync(dest)) continue
|
||||
await stillThumbPath(owner, still.id)
|
||||
built++
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1302,7 +1344,8 @@ export async function maintainLibrary(owner: string, folderId?: string) {
|
||||
const prev = lastMaintain.get(owner) || 0
|
||||
if (Date.now() - prev < 20000) return
|
||||
lastMaintain.set(owner, Date.now())
|
||||
void ensureStillThumbnails(owner).catch(() => null)
|
||||
// At most 2 missing thumbs per tick — never walk the whole catalog on list
|
||||
void ensureStillThumbnails(owner, { limit: 2 }).catch(() => null)
|
||||
const live = await fetchLiveQueue().catch(() => null)
|
||||
const busy = Boolean(live && live.running > 0)
|
||||
if (!busy) {
|
||||
|
||||
Reference in New Issue
Block a user