comfy-ts 2.13.0 → 2.14.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cli.js +3 -3
- package/dist/index.cjs +1 -1
- package/dist/index.js +1 -1
- package/dist/{run-serve-DlRBv_Nk.js → run-serve-DUqcn0h3.js} +211 -59
- package/dist/run-serve-DUqcn0h3.js.map +1 -0
- package/dist/{run-tui-B5uxm2_l.js → run-tui-D-LaSqgB.js} +2 -2
- package/dist/{run-tui-B5uxm2_l.js.map → run-tui-D-LaSqgB.js.map} +1 -1
- package/dist/serve-web.js +146 -71
- package/dist/{state-CUp2J1Zy.js → state-BytbdZ8s.js} +2 -2
- package/dist/{state-CUp2J1Zy.js.map → state-BytbdZ8s.js.map} +1 -1
- package/dist/web.js +1 -1
- package/package.json +1 -1
- package/src/cli/serve/ServeApp.ts +90 -82
- package/src/cli/serve/resultHistory.ts +125 -0
- package/src/cli/serve/runError.ts +23 -0
- package/src/cli/serve/serveSettings.ts +17 -1
- package/src/cli/serve/web/api.ts +29 -2
- package/src/cli/serve/web/components/App.tsx +6 -4
- package/src/cli/serve/web/components/Gallery.tsx +33 -8
- package/src/cli/serve/web/components/Icon.tsx +4 -0
- package/src/cli/serve/web/components/MemoryBudget.tsx +98 -0
- package/src/cli/serve/web/components/RunError.tsx +19 -0
- package/src/cli/serve/web/components/ShortcutsModal.tsx +54 -0
- package/src/cli/serve/web/components/SideMenu.tsx +14 -0
- package/src/cli/serve/web/components/VarsForm.tsx +71 -53
- package/src/cli/serve/web/components/controls/BasicControls.tsx +8 -1
- package/src/cli/serve/web/components/controls/LorasControl.tsx +6 -3
- package/src/cli/serve/web/promptEditor/promptExtensions.ts +3 -1
- package/src/cli/serve/web/state/RunSt.ts +61 -3
- package/src/cli/serve/web/state/WebSt.ts +26 -2
- package/src/cli/serve/web/state/formRows.ts +9 -0
- package/src/cli/serve/web/state/keptResults.ts +26 -0
- package/src/cli/serve/web/state/lightbox.ts +17 -0
- package/src/cli/serve/web/state/shortcutCatalog.ts +100 -0
- package/src/cli/serve/web/state/shortcuts.ts +10 -0
- package/src/cli/serve/web/styles.ts +98 -24
- package/dist/run-serve-DlRBv_Nk.js.map +0 -1
package/dist/web.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "comfy-ts",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.14.0",
|
|
4
4
|
"description": "The complete TypeScript toolkit for ComfyUI and generative AI. Fully type-safe SDK with autocomplete on every node, companion CLI, TUI, agentic docs, and 50+ examples. Design, run and deploy creative pipelines for image, video, audio, 3d and text on any ComfyUI, local, cloud or hosted.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"comfyui",
|
|
@@ -28,7 +28,14 @@ import { type TagNode, workflowTags } from 'src/cli/serve/workflowTags.ts'
|
|
|
28
28
|
import { workspaceLabels } from 'src/cli/serve/workspaceLabels.ts'
|
|
29
29
|
import { driftChanged, summarizeDrift } from 'src/host/schemaDrift.ts'
|
|
30
30
|
import { validSavePrefix, validStoreName } from 'src/utils/safeName.ts'
|
|
31
|
-
import {
|
|
31
|
+
import {
|
|
32
|
+
readServeSettings,
|
|
33
|
+
validMemoryBudgetMb,
|
|
34
|
+
writeServeSettings,
|
|
35
|
+
type ServeSettings,
|
|
36
|
+
} from 'src/cli/serve/serveSettings.ts'
|
|
37
|
+
import { describeRunError } from 'src/cli/serve/runError.ts'
|
|
38
|
+
import { ResultHistory, type KeptBlob, type RunRecord } from 'src/cli/serve/resultHistory.ts'
|
|
32
39
|
import { readTabs, SERVE_TABS_FILE, type DraftTab } from 'src/cli/serve/web/state/draftTabs.ts'
|
|
33
40
|
import { assembleLogChunks } from 'src/cli/tui/state/LogsSt.ts'
|
|
34
41
|
import { draftsDirForFile, listDraftsForFile } from 'src/cli/tui/state/DraftsSt.ts'
|
|
@@ -173,6 +180,7 @@ const INPUT_DOWNLOAD_TIMEOUT_MS = 60_000
|
|
|
173
180
|
/** the danbooru list is ~3.5 MB; ten times that is still a tag list, beyond is a wrong url */
|
|
174
181
|
const TAG_LIST_CAP = 40_000_000
|
|
175
182
|
const TAG_LIST_TIMEOUT_MS = 120_000
|
|
183
|
+
const MB = 1024 * 1024
|
|
176
184
|
|
|
177
185
|
/** read a reply STREAMING, aborting the moment it passes the cap. `arrayBuffer()` buffers the
|
|
178
186
|
* whole body first, so a chunked reply with no content-length could exhaust the process before
|
|
@@ -291,13 +299,14 @@ const USAGE = [
|
|
|
291
299
|
'PUT /prompt-enhancers/<name> with {"text"} — write one · DELETE /prompt-enhancers/<name> — remove it',
|
|
292
300
|
'GET /llm-configs — the enhancer LLM configs (.comfy-ts/llm-configs/*.json)',
|
|
293
301
|
'PUT /llm-configs/<name> with {provider, baseUrl, model, effort, thinkingOnly} · DELETE /llm-configs/<name>',
|
|
294
|
-
'GET /settings — { saveToDisk } · PUT /settings with {"saveToDisk"} — write outputs to disk, or keep them in memory',
|
|
302
|
+
'GET /settings — { saveToDisk, memoryBudgetMb } · PUT /settings with {"saveToDisk"} — write outputs to disk, or keep them in memory; {"memoryBudgetMb"} caps what memory keeps',
|
|
295
303
|
'GET /hosts — every host this process knows · PUT /hosts/<module> with {"host"} — run that workflow elsewhere',
|
|
296
304
|
'POST /hosts/<hostId>/<interrupt|clear-queue|restart|refresh-loras|refresh-schema> — act on a ComfyUI host',
|
|
297
305
|
'GET /hosts/<hostId>/ping — is that host answering right now (restart watch)',
|
|
298
306
|
'GET /hosts/<hostId>/logs — the last lines of that host console',
|
|
299
307
|
'GET /images/<promptId>/<ix> — an in-memory output (saving off), while the process lives',
|
|
300
308
|
'GET /audio/<promptId>/<ix> — the same for an in-memory audio output',
|
|
309
|
+
'GET /results — the last runs this process kept, for a reload · DELETE /results[/<promptId>] — forget them, freeing their memory',
|
|
301
310
|
'GET /outputs/<path> — generated files',
|
|
302
311
|
]
|
|
303
312
|
|
|
@@ -347,9 +356,13 @@ export class ServeApp {
|
|
|
347
356
|
private set settings(next: ServeSettings) {
|
|
348
357
|
this.settingsCache = next
|
|
349
358
|
}
|
|
350
|
-
/**
|
|
351
|
-
*
|
|
352
|
-
private
|
|
359
|
+
/** the last runs, for a reload, and the bytes of their unsaved outputs: the ONLY copy.
|
|
360
|
+
* LAZY for the same reason as settings: its budget is a setting */
|
|
361
|
+
private keptCache: ResultHistory | null = null
|
|
362
|
+
private get kept(): ResultHistory {
|
|
363
|
+
this.keptCache ??= new ResultHistory(this.settings.memoryBudgetMb * MB)
|
|
364
|
+
return this.keptCache
|
|
365
|
+
}
|
|
353
366
|
|
|
354
367
|
constructor(
|
|
355
368
|
public modules: ServeModule[],
|
|
@@ -430,6 +443,8 @@ export class ServeApp {
|
|
|
430
443
|
return this.replyMemoryImage(`${segs[1]}/${segs[2]}`)
|
|
431
444
|
if (segs[0] === 'audio' && segs.length === 3 && segs[1] != null && segs[2] != null)
|
|
432
445
|
return this.replyMemoryImage(`audio/${segs[1]}/${segs[2]}`)
|
|
446
|
+
if (segs[0] === 'results' && segs.length === 1)
|
|
447
|
+
return json(200, { runs: this.kept.list(), memory: this.kept.usage() })
|
|
433
448
|
if (segs[0] === 'outputs') return this.replyOutput(segs.slice(1))
|
|
434
449
|
return json(404, { error: `no route: GET ${path}`, usage: USAGE })
|
|
435
450
|
}
|
|
@@ -446,6 +461,11 @@ export class ServeApp {
|
|
|
446
461
|
if (req.method === 'DELETE' && segs[0] === 'drafts' && segs.length === 3 && segs[1] != null && segs[2] != null)
|
|
447
462
|
return await this.replyDeleteDraft(segs[1], segs[2])
|
|
448
463
|
if (req.method === 'PUT' && segs[0] === 'settings' && segs.length === 1) return this.replySaveSettings(req)
|
|
464
|
+
if (req.method === 'DELETE' && segs[0] === 'results' && segs.length <= 2) {
|
|
465
|
+
if (segs[1] == null) this.kept.clear()
|
|
466
|
+
else this.kept.remove(segs[1])
|
|
467
|
+
return json(200, { memory: this.kept.usage() })
|
|
468
|
+
}
|
|
449
469
|
if (req.method === 'PUT' && segs[0] === 'tabs' && segs.length === 1) return this.replySaveTabs(req)
|
|
450
470
|
if (req.method === 'PUT' && segs[0] === 'hosts' && segs.length === 2 && segs[1] != null)
|
|
451
471
|
return this.replySetHost(segs[1], req)
|
|
@@ -1110,25 +1130,16 @@ export class ServeApp {
|
|
|
1110
1130
|
}
|
|
1111
1131
|
|
|
1112
1132
|
// #region settings + in-memory outputs --------------------------------------
|
|
1113
|
-
|
|
1114
|
-
* saving off these buffers are the only copy and nothing else will free them */
|
|
1115
|
-
private readonly MEMORY_IMAGE_CAP = 60
|
|
1116
|
-
|
|
1117
|
-
private rememberMemoryImage(key: string, bytes: Uint8Array, filename: string): void {
|
|
1133
|
+
private memoryBlob(key: string, bytes: Uint8Array, filename: string): KeptBlob {
|
|
1118
1134
|
const contentType = CONTENT_TYPES[extname(filename).toLowerCase()] ?? sniffImageContentType(bytes)
|
|
1119
|
-
|
|
1120
|
-
while (this.memoryImages.size > this.MEMORY_IMAGE_CAP) {
|
|
1121
|
-
const oldest = this.memoryImages.keys().next()
|
|
1122
|
-
if (oldest.done === true) break
|
|
1123
|
-
this.memoryImages.delete(oldest.value)
|
|
1124
|
-
}
|
|
1135
|
+
return { key, bytes, contentType }
|
|
1125
1136
|
}
|
|
1126
1137
|
|
|
1127
1138
|
private replyMemoryImage(key: string): ServeReply {
|
|
1128
|
-
const hit = this.
|
|
1139
|
+
const hit = this.kept.blob(key)
|
|
1129
1140
|
if (hit == null)
|
|
1130
1141
|
return json(404, {
|
|
1131
|
-
error: `no in-memory output '${key}' — it
|
|
1142
|
+
error: `no in-memory output '${key}' — it was deleted, dropped past the ${this.settings.memoryBudgetMb} MB memory budget, or the server restarted. Turn saving on to keep outputs.`,
|
|
1132
1143
|
})
|
|
1133
1144
|
return { status: 200, contentType: hit.contentType, body: hit.bytes }
|
|
1134
1145
|
}
|
|
@@ -1149,9 +1160,9 @@ export class ServeApp {
|
|
|
1149
1160
|
}
|
|
1150
1161
|
if (parsed == null || typeof parsed !== 'object' || Array.isArray(parsed))
|
|
1151
1162
|
return json(400, {
|
|
1152
|
-
error: 'body must be { "saveToDisk"?: true | false, "savePrefix"?: { "<module>": "path" } }',
|
|
1163
|
+
error: 'body must be { "saveToDisk"?: true | false, "savePrefix"?: { "<module>": "path" }, "memoryBudgetMb"?: number }',
|
|
1153
1164
|
})
|
|
1154
|
-
const wanted = parsed as { saveToDisk?: unknown; savePrefix?: unknown }
|
|
1165
|
+
const wanted = parsed as { saveToDisk?: unknown; savePrefix?: unknown; memoryBudgetMb?: unknown }
|
|
1155
1166
|
let next = this.settings
|
|
1156
1167
|
if (wanted.saveToDisk != null) {
|
|
1157
1168
|
if (typeof wanted.saveToDisk !== 'boolean') return json(400, { error: '"saveToDisk" must be true or false' })
|
|
@@ -1175,8 +1186,14 @@ export class ServeApp {
|
|
|
1175
1186
|
}
|
|
1176
1187
|
next = { ...next, savePrefix: prefixes }
|
|
1177
1188
|
}
|
|
1189
|
+
if (wanted.memoryBudgetMb != null) {
|
|
1190
|
+
const mb = validMemoryBudgetMb(wanted.memoryBudgetMb)
|
|
1191
|
+
if (mb == null) return json(400, { error: '"memoryBudgetMb" must be a number of MB, 1 to 100000' })
|
|
1192
|
+
next = { ...next, memoryBudgetMb: mb }
|
|
1193
|
+
}
|
|
1178
1194
|
const saveToDisk = next.saveToDisk
|
|
1179
1195
|
this.settings = next
|
|
1196
|
+
this.kept.setBudget(next.memoryBudgetMb * MB)
|
|
1180
1197
|
try {
|
|
1181
1198
|
writeServeSettings(this.settings)
|
|
1182
1199
|
} catch (e) {
|
|
@@ -1566,15 +1583,9 @@ export class ServeApp {
|
|
|
1566
1583
|
const durationMs = Date.now() - t0
|
|
1567
1584
|
|
|
1568
1585
|
if (execution.status === 'Failure') {
|
|
1569
|
-
|
|
1570
|
-
|
|
1571
|
-
|
|
1572
|
-
module: mod.key,
|
|
1573
|
-
draft,
|
|
1574
|
-
promptId: execution.data.id,
|
|
1575
|
-
durationMs,
|
|
1576
|
-
error: execution.data.error ?? 'execution failed',
|
|
1577
|
-
})
|
|
1586
|
+
const error = describeRunError(execution.data.error)
|
|
1587
|
+
console.error(`[serve] 🔴 ${mod.key}/${draft} failed after ${durationMs}ms: ${error}`)
|
|
1588
|
+
return json(500, { ok: false, module: mod.key, draft, promptId: execution.data.id, durationMs, error })
|
|
1578
1589
|
}
|
|
1579
1590
|
|
|
1580
1591
|
const produced = [`${execution.images.length} image(s)`]
|
|
@@ -1585,22 +1596,57 @@ export class ServeApp {
|
|
|
1585
1596
|
|
|
1586
1597
|
// saving off → the buffer is the only copy: keep it addressable so the gallery (and
|
|
1587
1598
|
// curl) still get an image instead of a null url
|
|
1588
|
-
const
|
|
1589
|
-
|
|
1590
|
-
if (img.absPath != null || img.buffer == null)
|
|
1599
|
+
const blobs: KeptBlob[] = []
|
|
1600
|
+
const imageKeys = execution.images.map((img, ix) => {
|
|
1601
|
+
if (img.absPath != null || img.buffer == null) return null
|
|
1591
1602
|
const key = `${execution.data.id}/${ix}`
|
|
1592
|
-
this.
|
|
1593
|
-
|
|
1594
|
-
}
|
|
1595
|
-
|
|
1603
|
+
blobs.push(this.memoryBlob(key, img.buffer, img.filename))
|
|
1604
|
+
return key
|
|
1605
|
+
})
|
|
1596
1606
|
// same store as the images, own key space: the audio route adds the `audio/` prefix
|
|
1597
|
-
const audioKeys =
|
|
1598
|
-
|
|
1599
|
-
|
|
1600
|
-
|
|
1601
|
-
|
|
1602
|
-
|
|
1607
|
+
const audioKeys = audios.map((a, ix) => {
|
|
1608
|
+
if (a.absPath != null) return null
|
|
1609
|
+
const key = `audio/${execution.data.id}/${ix}`
|
|
1610
|
+
blobs.push(this.memoryBlob(key, a.bytes, a.filename))
|
|
1611
|
+
return key
|
|
1612
|
+
})
|
|
1613
|
+
// the KEY is raw (`<promptId>/<ix>`); the url encodes each segment, and handle() decodes
|
|
1614
|
+
// per segment, so the lookup gets the raw key back
|
|
1615
|
+
const memoryUrl = (route: string, key: string): string =>
|
|
1616
|
+
`/${route}/${key
|
|
1617
|
+
.split('/')
|
|
1618
|
+
.slice(route === 'audio' ? 1 : 0)
|
|
1619
|
+
.map((seg) => encodeURIComponent(seg))
|
|
1620
|
+
.join('/')}`
|
|
1621
|
+
const record: RunRecord = {
|
|
1622
|
+
ok: true,
|
|
1623
|
+
module: mod.key,
|
|
1624
|
+
draft,
|
|
1625
|
+
promptId: execution.data.id,
|
|
1626
|
+
durationMs,
|
|
1627
|
+
finishedAt: Date.now(),
|
|
1628
|
+
seeds,
|
|
1629
|
+
savedToDisk: this.settings.saveToDisk,
|
|
1630
|
+
texts: (execution.texts ?? []).map((t) => ({ nodeKey: t.nodeKey, text: t.text })),
|
|
1631
|
+
images: execution.images.map((img, ix) => {
|
|
1632
|
+
const key = imageKeys[ix]
|
|
1633
|
+
return {
|
|
1634
|
+
filename: img.filename,
|
|
1635
|
+
url: img.absPath != null ? this.outputUrl(img.absPath) : key != null ? memoryUrl('images', key) : null,
|
|
1636
|
+
absPath: img.absPath,
|
|
1637
|
+
}
|
|
1638
|
+
}),
|
|
1639
|
+
audios: audios.map((a, ix) => {
|
|
1640
|
+
const key = audioKeys[ix]
|
|
1641
|
+
return {
|
|
1642
|
+
filename: a.filename,
|
|
1643
|
+
mime: a.mime,
|
|
1644
|
+
url: a.absPath != null ? this.outputUrl(a.absPath) : key != null ? memoryUrl('audio', key) : null,
|
|
1645
|
+
absPath: a.absPath,
|
|
1646
|
+
}
|
|
1647
|
+
}),
|
|
1603
1648
|
}
|
|
1649
|
+
this.kept.add({ record, imageKeys, audioKeys, blobs })
|
|
1604
1650
|
|
|
1605
1651
|
// Accept: image/* → first image's bytes directly (curl -o, <img src>)
|
|
1606
1652
|
const first = execution.images[0]
|
|
@@ -1609,49 +1655,11 @@ export class ServeApp {
|
|
|
1609
1655
|
const contentType = CONTENT_TYPES[extname(first.absPath).toLowerCase()] ?? 'application/octet-stream'
|
|
1610
1656
|
return { status: 200, contentType, body: new Uint8Array(readFileSync(first.absPath)) }
|
|
1611
1657
|
}
|
|
1612
|
-
const inMemory = this.
|
|
1658
|
+
const inMemory = this.kept.blob(imageKeys[0] ?? '')
|
|
1613
1659
|
if (inMemory != null) return { status: 200, contentType: inMemory.contentType, body: inMemory.bytes }
|
|
1614
1660
|
}
|
|
1615
1661
|
|
|
1616
|
-
return json(200,
|
|
1617
|
-
ok: true,
|
|
1618
|
-
module: mod.key,
|
|
1619
|
-
draft,
|
|
1620
|
-
promptId: execution.data.id,
|
|
1621
|
-
durationMs,
|
|
1622
|
-
seeds,
|
|
1623
|
-
savedToDisk: this.settings.saveToDisk,
|
|
1624
|
-
texts: (execution.texts ?? []).map((t) => ({ nodeKey: t.nodeKey, text: t.text })),
|
|
1625
|
-
images: execution.images.map((img, ix) => ({
|
|
1626
|
-
filename: img.filename,
|
|
1627
|
-
url:
|
|
1628
|
-
img.absPath != null
|
|
1629
|
-
? this.outputUrl(img.absPath)
|
|
1630
|
-
: memoryKeys.has(ix)
|
|
1631
|
-
? // the KEY is raw (`<promptId>/<ix>`); the url encodes each segment, and
|
|
1632
|
-
// handle() decodes per segment, so the lookup gets the raw key back
|
|
1633
|
-
`/images/${(memoryKeys.get(ix) ?? '')
|
|
1634
|
-
.split('/')
|
|
1635
|
-
.map((seg) => encodeURIComponent(seg))
|
|
1636
|
-
.join('/')}`
|
|
1637
|
-
: null,
|
|
1638
|
-
absPath: img.absPath,
|
|
1639
|
-
})),
|
|
1640
|
-
audios: audios.map((a, ix) => ({
|
|
1641
|
-
filename: a.filename,
|
|
1642
|
-
mime: a.mime,
|
|
1643
|
-
url:
|
|
1644
|
-
a.absPath != null
|
|
1645
|
-
? this.outputUrl(a.absPath)
|
|
1646
|
-
: audioKeys.has(ix)
|
|
1647
|
-
? `/audio/${(audioKeys.get(ix) ?? '')
|
|
1648
|
-
.split('/')
|
|
1649
|
-
.map((seg) => encodeURIComponent(seg))
|
|
1650
|
-
.join('/')}`
|
|
1651
|
-
: null,
|
|
1652
|
-
absPath: a.absPath,
|
|
1653
|
-
})),
|
|
1654
|
-
})
|
|
1662
|
+
return json(200, record)
|
|
1655
1663
|
}
|
|
1656
1664
|
|
|
1657
1665
|
/** everything that touches the module's SHARED vars, under the module mutex */
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
// the serve process's memory of its last runs: a page reload (or a second window) shows them
|
|
2
|
+
// again. Unsaved outputs keep their bytes here, the ONLY copy, under one byte budget, FIFO.
|
|
3
|
+
// Nothing is written to disk: a server restart forgets it all. PURE, headless-tested
|
|
4
|
+
|
|
5
|
+
export type RunOutput = { filename: string; url: string | null; absPath: string | null }
|
|
6
|
+
export type RunAudioOutput = RunOutput & { mime: string }
|
|
7
|
+
|
|
8
|
+
/** the run reply `POST /generate` answers, and what `GET /results` lists */
|
|
9
|
+
export type RunRecord = {
|
|
10
|
+
ok: true
|
|
11
|
+
module: string
|
|
12
|
+
draft: string
|
|
13
|
+
promptId: string
|
|
14
|
+
durationMs: number
|
|
15
|
+
/** epoch ms: the panel formats it, the server's clock is the one every window shares */
|
|
16
|
+
finishedAt: number
|
|
17
|
+
seeds: Record<string, number>
|
|
18
|
+
savedToDisk: boolean
|
|
19
|
+
texts: { nodeKey: string | null; text: string }[]
|
|
20
|
+
images: RunOutput[]
|
|
21
|
+
audios: RunAudioOutput[]
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export type MemoryUsage = { usedBytes: number; budgetBytes: number; outputs: number; runs: number }
|
|
25
|
+
|
|
26
|
+
/** the bytes of one unsaved output, under the key its url names */
|
|
27
|
+
export type KeptBlob = { key: string; bytes: Uint8Array; contentType: string }
|
|
28
|
+
|
|
29
|
+
/** how many runs the list keeps, saved or not. The panel shows as many */
|
|
30
|
+
export const KEPT_RUNS = 50
|
|
31
|
+
|
|
32
|
+
export const DEFAULT_MEMORY_BUDGET_MB = 100
|
|
33
|
+
|
|
34
|
+
/** a run and the memory key of each output (null = saved on disk, or never had bytes) */
|
|
35
|
+
type Kept = { record: RunRecord; imageKeys: (string | null)[]; audioKeys: (string | null)[] }
|
|
36
|
+
|
|
37
|
+
export class ResultHistory {
|
|
38
|
+
/** newest first */
|
|
39
|
+
private runs: Kept[] = []
|
|
40
|
+
/** insertion order IS the eviction order */
|
|
41
|
+
private blobs = new Map<string, { bytes: Uint8Array; contentType: string }>()
|
|
42
|
+
private used = 0
|
|
43
|
+
|
|
44
|
+
constructor(private budgetBytes: number) {}
|
|
45
|
+
|
|
46
|
+
add(p: { record: RunRecord; imageKeys: (string | null)[]; audioKeys: (string | null)[]; blobs: KeptBlob[] }): void {
|
|
47
|
+
this.remove(p.record.promptId)
|
|
48
|
+
for (const b of p.blobs) {
|
|
49
|
+
this.blobs.set(b.key, { bytes: b.bytes, contentType: b.contentType })
|
|
50
|
+
this.used += b.bytes.byteLength
|
|
51
|
+
}
|
|
52
|
+
this.runs = [{ record: p.record, imageKeys: p.imageKeys, audioKeys: p.audioKeys }, ...this.runs]
|
|
53
|
+
for (const dropped of this.runs.slice(KEPT_RUNS)) this.freeRun(dropped)
|
|
54
|
+
this.runs = this.runs.slice(0, KEPT_RUNS)
|
|
55
|
+
this.evict()
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
blob(key: string): { bytes: Uint8Array; contentType: string } | null {
|
|
59
|
+
return this.blobs.get(key) ?? null
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** an output whose bytes were evicted lists with url null: the panel says it is gone */
|
|
63
|
+
list(): RunRecord[] {
|
|
64
|
+
return this.runs.map((k) => ({
|
|
65
|
+
...k.record,
|
|
66
|
+
images: k.record.images.map((img, ix) => this.withLiveUrl(img, k.imageKeys[ix])),
|
|
67
|
+
audios: k.record.audios.map((a, ix) => this.withLiveUrl(a, k.audioKeys[ix])),
|
|
68
|
+
}))
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
remove(promptId: string): void {
|
|
72
|
+
const hit = this.runs.find((k) => k.record.promptId === promptId)
|
|
73
|
+
if (hit == null) return
|
|
74
|
+
this.freeRun(hit)
|
|
75
|
+
this.runs = this.runs.filter((k) => k !== hit)
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
clear(): void {
|
|
79
|
+
this.runs = []
|
|
80
|
+
this.blobs.clear()
|
|
81
|
+
this.used = 0
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
setBudget(bytes: number): void {
|
|
85
|
+
this.budgetBytes = bytes
|
|
86
|
+
this.evict()
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
usage(): MemoryUsage {
|
|
90
|
+
return { usedBytes: this.used, budgetBytes: this.budgetBytes, outputs: this.blobs.size, runs: this.runs.length }
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
private withLiveUrl<T extends RunOutput>(out: T, key: string | null | undefined): T {
|
|
94
|
+
if (key == null || this.blobs.has(key)) return out
|
|
95
|
+
return { ...out, url: null }
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
private freeRun(k: Kept): void {
|
|
99
|
+
for (const key of [...k.imageKeys, ...k.audioKeys]) {
|
|
100
|
+
if (key == null) continue
|
|
101
|
+
const b = this.blobs.get(key)
|
|
102
|
+
if (b == null) continue
|
|
103
|
+
this.used -= b.bytes.byteLength
|
|
104
|
+
this.blobs.delete(key)
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** the NEWEST output always stays, even alone over budget: the run that just finished must
|
|
109
|
+
* not reply with a url that already 404s */
|
|
110
|
+
private evict(): void {
|
|
111
|
+
while (this.used > this.budgetBytes && this.blobs.size > 1) {
|
|
112
|
+
const oldest = this.blobs.entries().next()
|
|
113
|
+
if (oldest.done === true) break
|
|
114
|
+
this.used -= oldest.value[1].bytes.byteLength
|
|
115
|
+
this.blobs.delete(oldest.value[0])
|
|
116
|
+
}
|
|
117
|
+
// a run with nothing left to show is noise in the list
|
|
118
|
+
this.runs = this.runs.filter((k) => {
|
|
119
|
+
const live = (keys: (string | null)[]): boolean => keys.some((key) => key == null || this.blobs.has(key))
|
|
120
|
+
const hasText = k.record.texts.length > 0
|
|
121
|
+
const hasNothing = k.record.images.length === 0 && k.record.audios.length === 0
|
|
122
|
+
return hasText || hasNothing || live(k.imageKeys) || live(k.audioKeys)
|
|
123
|
+
})
|
|
124
|
+
}
|
|
125
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
// a failed run's error as ONE readable line. ComfyExecution stores ComfyUI's whole
|
|
2
|
+
// execution_error message; every client reads `error` as text
|
|
3
|
+
export function describeRunError(error: unknown): string {
|
|
4
|
+
if (error == null) return 'execution failed'
|
|
5
|
+
if (typeof error === 'string') return error
|
|
6
|
+
if (error instanceof Error) return error.message
|
|
7
|
+
if (typeof error === 'object') {
|
|
8
|
+
const data = (error as { data?: unknown }).data
|
|
9
|
+
if (typeof data === 'object' && data !== null) {
|
|
10
|
+
const d = data as Record<string, unknown>
|
|
11
|
+
if (typeof d.exception_message === 'string') {
|
|
12
|
+
const where = typeof d.node_type === 'string' ? `${d.node_type} (node ${String(d.node_id)}): ` : ''
|
|
13
|
+
const kind = typeof d.exception_type === 'string' && d.exception_type !== '' ? `${d.exception_type}: ` : ''
|
|
14
|
+
return `${where}${kind}${d.exception_message.trim()}`
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
try {
|
|
19
|
+
return JSON.stringify(error)
|
|
20
|
+
} catch {
|
|
21
|
+
return String(error)
|
|
22
|
+
}
|
|
23
|
+
}
|
|
@@ -7,6 +7,7 @@ import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs'
|
|
|
7
7
|
import { stringMap } from 'src/utils/stringMap.ts'
|
|
8
8
|
import { validSavePrefix } from 'src/utils/safeName.ts'
|
|
9
9
|
import { dirname } from 'pathe'
|
|
10
|
+
import { DEFAULT_MEMORY_BUDGET_MB } from 'src/cli/serve/resultHistory.ts'
|
|
10
11
|
|
|
11
12
|
export type ServeSettings = {
|
|
12
13
|
/** false = generations stay in memory, streamed to the client, nothing written to outputs/ */
|
|
@@ -17,9 +18,23 @@ export type ServeSettings = {
|
|
|
17
18
|
/** module key → subfolder under outputs/ its images land in. Empty/absent = the module key,
|
|
18
19
|
* which is what serve always used before it was choosable */
|
|
19
20
|
savePrefix: Record<string, string>
|
|
21
|
+
/** byte budget of the unsaved outputs the process keeps for a reload, oldest dropped first */
|
|
22
|
+
memoryBudgetMb: number
|
|
20
23
|
}
|
|
21
24
|
|
|
22
|
-
const DEFAULT_SERVE_SETTINGS: ServeSettings = {
|
|
25
|
+
const DEFAULT_SERVE_SETTINGS: ServeSettings = {
|
|
26
|
+
saveToDisk: true,
|
|
27
|
+
hostOverride: {},
|
|
28
|
+
savePrefix: {},
|
|
29
|
+
memoryBudgetMb: DEFAULT_MEMORY_BUDGET_MB,
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** null = not a budget. A hand-written file goes through the same gate as PUT /settings */
|
|
33
|
+
export function validMemoryBudgetMb(raw: unknown): number | null {
|
|
34
|
+
if (typeof raw !== 'number' || !Number.isFinite(raw)) return null
|
|
35
|
+
if (raw < 1 || raw > 100_000) return null
|
|
36
|
+
return Math.round(raw)
|
|
37
|
+
}
|
|
23
38
|
|
|
24
39
|
/** null when no comfyts is registered yet: a ServeApp can be constructed before the global
|
|
25
40
|
* exists (tests do), and reading a setting must degrade to defaults, never throw */
|
|
@@ -69,6 +84,7 @@ export function readServeSettings(): ServeSettings {
|
|
|
69
84
|
// hand-written file did not go through that route, and this value becomes an output
|
|
70
85
|
// directory. One gate on one side is not a gate
|
|
71
86
|
savePrefix: cleanValues(stringMap(o.savePrefix), validSavePrefix),
|
|
87
|
+
memoryBudgetMb: validMemoryBudgetMb(o.memoryBudgetMb) ?? DEFAULT_SERVE_SETTINGS.memoryBudgetMb,
|
|
72
88
|
}
|
|
73
89
|
}
|
|
74
90
|
|
package/src/cli/serve/web/api.ts
CHANGED
|
@@ -39,6 +39,8 @@ export type GenerateOk = {
|
|
|
39
39
|
promptId: string
|
|
40
40
|
durationMs: number
|
|
41
41
|
seeds: Record<string, number>
|
|
42
|
+
/** epoch ms, stamped by the server */
|
|
43
|
+
finishedAt?: number
|
|
42
44
|
images: GeneratedImage[]
|
|
43
45
|
texts?: GeneratedText[]
|
|
44
46
|
audios?: GeneratedAudio[]
|
|
@@ -47,7 +49,13 @@ export type GenerateOk = {
|
|
|
47
49
|
async function jsonFetch<T>(url: string, init?: RequestInit): Promise<T> {
|
|
48
50
|
const res = await fetch(url, init)
|
|
49
51
|
const body = (await res.json().catch(() => null)) as (T & { error?: string }) | null
|
|
50
|
-
if (!res.ok || body == null)
|
|
52
|
+
if (!res.ok || body == null) {
|
|
53
|
+
// a non-string error would print as "[object Object]"
|
|
54
|
+
const error: unknown = body?.error
|
|
55
|
+
throw new Error(
|
|
56
|
+
typeof error === 'string' ? error : error != null ? JSON.stringify(error) : `http ${res.status} on ${url}`,
|
|
57
|
+
)
|
|
58
|
+
}
|
|
51
59
|
return body
|
|
52
60
|
}
|
|
53
61
|
|
|
@@ -71,6 +79,20 @@ export function postGenerate(p: {
|
|
|
71
79
|
})
|
|
72
80
|
}
|
|
73
81
|
|
|
82
|
+
/** the unsaved outputs the serve process holds, against its byte budget */
|
|
83
|
+
export type MemoryUsage = { usedBytes: number; budgetBytes: number; outputs: number; runs: number }
|
|
84
|
+
|
|
85
|
+
/** the last runs the serve process kept: a reload shows them again */
|
|
86
|
+
export function fetchKeptResults(): Promise<{ runs: GenerateOk[]; memory: MemoryUsage }> {
|
|
87
|
+
return jsonFetch('/results')
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** no promptId = every kept run */
|
|
91
|
+
export function deleteKeptResults(p: { promptId?: string } = {}): Promise<{ memory: MemoryUsage }> {
|
|
92
|
+
const url = p.promptId == null ? '/results' : `/results/${encodeURIComponent(p.promptId)}`
|
|
93
|
+
return jsonFetch(url, { method: 'DELETE' })
|
|
94
|
+
}
|
|
95
|
+
|
|
74
96
|
/** the workflow's previews for these values: nothing runs, nothing is written */
|
|
75
97
|
export function fetchPreviews(p: {
|
|
76
98
|
module: string
|
|
@@ -181,6 +203,7 @@ export type ServeSettings = {
|
|
|
181
203
|
saveToDisk: boolean
|
|
182
204
|
hostOverride: Record<string, string>
|
|
183
205
|
savePrefix: Record<string, string>
|
|
206
|
+
memoryBudgetMb: number
|
|
184
207
|
effectivePrefix: Record<string, string>
|
|
185
208
|
}
|
|
186
209
|
|
|
@@ -188,7 +211,11 @@ export function fetchSettings(): Promise<ServeSettings> {
|
|
|
188
211
|
return jsonFetch('/settings')
|
|
189
212
|
}
|
|
190
213
|
|
|
191
|
-
export function saveSettings(p: {
|
|
214
|
+
export function saveSettings(p: {
|
|
215
|
+
saveToDisk?: boolean
|
|
216
|
+
savePrefix?: Record<string, string>
|
|
217
|
+
memoryBudgetMb?: number
|
|
218
|
+
}): Promise<ServeSettings> {
|
|
192
219
|
return jsonFetch('/settings', {
|
|
193
220
|
method: 'PUT',
|
|
194
221
|
headers: { 'content-type': 'application/json' },
|
|
@@ -9,12 +9,15 @@ import { Icon, type IconName } from 'src/cli/serve/web/components/Icon.tsx'
|
|
|
9
9
|
import { LATENT_MODES, type LatentMode } from 'src/cli/serve/web/state/latentMode.ts'
|
|
10
10
|
import { Omnibox, useOmniboxShortcut } from 'src/cli/serve/web/components/Omnibox.tsx'
|
|
11
11
|
import { TooltipLayer } from 'src/cli/serve/web/components/TooltipLayer.tsx'
|
|
12
|
+
import { MemoryBudgetButton } from 'src/cli/serve/web/components/MemoryBudget.tsx'
|
|
13
|
+
import { RunError } from 'src/cli/serve/web/components/RunError.tsx'
|
|
12
14
|
import { MOD_KEY } from 'src/cli/serve/web/components/modKey.ts'
|
|
13
15
|
import { SHORTCUT_KEYS, shortcutOf } from 'src/cli/serve/web/state/shortcuts.ts'
|
|
14
16
|
import { reaction } from 'mobx'
|
|
15
17
|
import { collapsedPreview } from 'src/cli/serve/web/state/stableSlots.ts'
|
|
16
18
|
import { GenerateButton, VarsForm } from 'src/cli/serve/web/components/VarsForm.tsx'
|
|
17
19
|
import { DraftTabs, useTabShortcuts } from 'src/cli/serve/web/components/DraftTabs.tsx'
|
|
20
|
+
import { ShortcutsModal } from 'src/cli/serve/web/components/ShortcutsModal.tsx'
|
|
18
21
|
import type { WebSt } from 'src/cli/serve/web/state/WebSt.ts'
|
|
19
22
|
|
|
20
23
|
/** ⌘A / ctrl+A selects the field you are in. The browser does this on its own until something
|
|
@@ -217,6 +220,7 @@ const ResultsHead = observer(function ResultsHead(p: { st: WebSt }) {
|
|
|
217
220
|
{SHORTCUT_KEYS['toggle-blur']}
|
|
218
221
|
</span>
|
|
219
222
|
</button>
|
|
223
|
+
<MemoryBudgetButton st={p.st} />
|
|
220
224
|
</span>
|
|
221
225
|
</div>
|
|
222
226
|
)
|
|
@@ -452,13 +456,10 @@ export const App = observer(function App(p: { st: WebSt }) {
|
|
|
452
456
|
{p.st.generateInResults ? (
|
|
453
457
|
<div className="results-run">
|
|
454
458
|
<GenerateButton st={p.st} />
|
|
455
|
-
{/* one line, always reserved: an error arriving never pushes the gallery down */}
|
|
456
|
-
<span className="run-error" data-tip={p.st.run.error ?? undefined}>
|
|
457
|
-
{p.st.run.error == null ? '' : `🔴 ${p.st.run.error}`}
|
|
458
|
-
</span>
|
|
459
459
|
</div>
|
|
460
460
|
) : null}
|
|
461
461
|
<LivePreviews st={p.st} />
|
|
462
|
+
<RunError st={p.st} />
|
|
462
463
|
<Gallery st={p.st} compact={layout === 'pinned'} />
|
|
463
464
|
</div>
|
|
464
465
|
)
|
|
@@ -527,6 +528,7 @@ export const App = observer(function App(p: { st: WebSt }) {
|
|
|
527
528
|
</div>
|
|
528
529
|
{narrow ? <MenuDrawer st={p.st} /> : null}
|
|
529
530
|
<Omnibox st={p.st} />
|
|
531
|
+
<ShortcutsModal st={p.st} />
|
|
530
532
|
<TooltipLayer />
|
|
531
533
|
</div>
|
|
532
534
|
)
|