Stop polling the library catalog and keep the stage on thumbs.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
Towsty
2026-09-29 20:15:03 -05:00
co-authored by Cursor
parent cfabfd37ee
commit a465dd68ec
7 changed files with 294 additions and 182 deletions
+105 -62
View File
@@ -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) {