comfy-ts 1.0.0 → 1.1.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.
Files changed (124) hide show
  1. package/README.md +42 -6
  2. package/dist/{DefinedWorkflow-O3ynII3Y.js → DefinedWorkflow-BYyu5dK9.js} +76 -5
  3. package/dist/DefinedWorkflow-BYyu5dK9.js.map +1 -0
  4. package/dist/cli.js +421 -308
  5. package/dist/cli.js.map +1 -1
  6. package/dist/index.cjs +394 -162
  7. package/dist/index.cjs.map +1 -1
  8. package/dist/index.d.cts +7426 -353
  9. package/dist/index.d.ts +7408 -335
  10. package/dist/index.js +393 -162
  11. package/dist/index.js.map +1 -1
  12. package/dist/{run-tui-BFhba_oG.js → run-tui-DZ22HDHI.js} +529 -78
  13. package/dist/run-tui-DZ22HDHI.js.map +1 -0
  14. package/examples/02-img2img-upload.cflow.ts +6 -17
  15. package/examples/05-comfy-cloud.cflow.ts +19 -35
  16. package/examples/README.md +30 -0
  17. package/examples/comfy-cloud/ace-step-t2a.cflow.ts +79 -0
  18. package/examples/comfy-cloud/anima-t2i.cflow.ts +57 -0
  19. package/examples/comfy-cloud/boogu-i2i.cflow.ts +76 -0
  20. package/examples/comfy-cloud/capybara-i2i.cflow.ts +85 -0
  21. package/examples/comfy-cloud/capybara-i2v.cflow.ts +90 -0
  22. package/examples/comfy-cloud/capybara-t2i.cflow.ts +67 -0
  23. package/examples/comfy-cloud/chatterbox-t2a.cflow.ts +79 -0
  24. package/examples/comfy-cloud/chroma-t2i.cflow.ts +62 -0
  25. package/examples/comfy-cloud/cloudHost.ts +41 -0
  26. package/examples/comfy-cloud/ernie-t2i.cflow.ts +59 -0
  27. package/examples/comfy-cloud/firered-i2i.cflow.ts +74 -0
  28. package/examples/comfy-cloud/flux1-i2i.cflow.ts +76 -0
  29. package/examples/comfy-cloud/flux1-t2i.cflow.ts +63 -0
  30. package/examples/comfy-cloud/flux2-i2i.cflow.ts +80 -0
  31. package/examples/comfy-cloud/flux2-t2i.cflow.ts +57 -0
  32. package/examples/comfy-cloud/hidream-i2i.cflow.ts +89 -0
  33. package/examples/comfy-cloud/hidream-t2i.cflow.ts +65 -0
  34. package/examples/comfy-cloud/hunyuan-video-i2v.cflow.ts +93 -0
  35. package/examples/comfy-cloud/hunyuan-video-t2v.cflow.ts +74 -0
  36. package/examples/comfy-cloud/ideogram4-t2i.cflow.ts +80 -0
  37. package/examples/comfy-cloud/kandinsky5-i2v.cflow.ts +95 -0
  38. package/examples/comfy-cloud/kandinsky5-t2i.cflow.ts +60 -0
  39. package/examples/comfy-cloud/kandinsky5-t2v.cflow.ts +78 -0
  40. package/examples/comfy-cloud/krea2-t2i.cflow.ts +59 -0
  41. package/examples/comfy-cloud/lens-t2i.cflow.ts +62 -0
  42. package/examples/comfy-cloud/longcat-i2i.cflow.ts +74 -0
  43. package/examples/comfy-cloud/longcat-t2i.cflow.ts +65 -0
  44. package/examples/comfy-cloud/ltxv-i2v.cflow.ts +92 -0
  45. package/examples/comfy-cloud/ltxv-t2v.cflow.ts +78 -0
  46. package/examples/comfy-cloud/newbie-t2i.cflow.ts +67 -0
  47. package/examples/comfy-cloud/omnigen2-i2i.cflow.ts +86 -0
  48. package/examples/comfy-cloud/omnigen2-t2i.cflow.ts +62 -0
  49. package/examples/comfy-cloud/ovis-t2i.cflow.ts +56 -0
  50. package/examples/comfy-cloud/pixeldit-t2i.cflow.ts +63 -0
  51. package/examples/comfy-cloud/qwen-image-i2i.cflow.ts +69 -0
  52. package/examples/comfy-cloud/qwen-image-t2i.cflow.ts +55 -0
  53. package/examples/comfy-cloud/sd35-t2i.cflow.ts +48 -0
  54. package/examples/comfy-cloud/sdk.d.ts +445 -57
  55. package/examples/comfy-cloud/sdxl-t2i.cflow.ts +69 -0
  56. package/examples/comfy-cloud/stable-audio-t2a.cflow.ts +53 -0
  57. package/examples/comfy-cloud/svd-i2v.cflow.ts +75 -0
  58. package/examples/comfy-cloud/wan21-i2v.cflow.ts +83 -0
  59. package/examples/comfy-cloud/wan21-t2v.cflow.ts +64 -0
  60. package/examples/comfy-cloud/wan22-5b-t2v.cflow.ts +69 -0
  61. package/examples/comfy-cloud/wan22-i2v.cflow.ts +107 -0
  62. package/examples/comfy-cloud/wan22-t2v.cflow.ts +98 -0
  63. package/examples/comfy-cloud/z-image-t2i.cflow.ts +62 -0
  64. package/examples/images/README.md +18 -0
  65. package/examples/images/bear_1024x1024.jpg +0 -0
  66. package/examples/images/deer_768x1344.jpg +0 -0
  67. package/examples/images/dog_512x512.jpg +0 -0
  68. package/examples/images/lioness_768x768.jpg +0 -0
  69. package/examples/images/pug_200x300.jpg +0 -0
  70. package/examples/images/walrus_1344x768.jpg +0 -0
  71. package/guide-for-agents.md +7 -1
  72. package/package.json +15 -4
  73. package/src/cli/tui/components/PreviewPanel.tsx +9 -8
  74. package/src/cli/tui/components/StatusBar.tsx +11 -0
  75. package/src/cli/tui/components/TuiApp.tsx +18 -0
  76. package/src/cli/tui/discoverWorkflows.ts +7 -41
  77. package/src/cli/tui/imagePicker/ImagePickerOverlay.tsx +72 -0
  78. package/src/cli/tui/imagePicker/ImagePickerSt.ts +288 -0
  79. package/src/cli/tui/imagePicker/fsListing.ts +46 -0
  80. package/src/cli/tui/imagePicker/pickerPrefs.ts +78 -0
  81. package/src/cli/tui/run-tui.tsx +2 -1
  82. package/src/cli/tui/state/LorasSt.ts +14 -36
  83. package/src/cli/tui/state/PreviewSt.ts +28 -2
  84. package/src/cli/tui/state/TuiSt.ts +6 -0
  85. package/src/exampleAssets.ts +56 -0
  86. package/src/graph/ComfyNode.ts +4 -1
  87. package/src/index.ts +7 -0
  88. package/src/manager/ComfyRegistry.ts +37 -9
  89. package/src/manager/generated/KnownComfyCustomNodeName.ts +23852 -1926
  90. package/src/manager/generated/KnownComfyPluginTitle.ts +6854 -236
  91. package/src/manager/generated/KnownComfyPluginURL.ts +3425 -111
  92. package/src/manager/generated/KnownModel_Base.ts +23 -7
  93. package/src/manager/generated/KnownModel_FileName.ts +421 -0
  94. package/src/manager/generated/KnownModel_Name.ts +421 -0
  95. package/src/manager/generated/KnownModel_SavePath.ts +34 -8
  96. package/src/manager/generated/KnownModel_Type.ts +18 -14
  97. package/src/manager/json/alter-list.json +221 -221
  98. package/src/manager/json/custom-node-list.json +61020 -21946
  99. package/src/manager/json/extension-node-map.json +81077 -31176
  100. package/src/manager/json/github-stats.json +29321 -12601
  101. package/src/manager/json/model-list.json +5935 -4997
  102. package/src/manager/loaders/parseReport.ts +55 -0
  103. package/src/manager/loaders/step1_downloadComfyManagerJSONs.ts +15 -49
  104. package/src/manager/loaders/step2_custom-node-list-loader.ts +38 -83
  105. package/src/manager/loaders/step3_extension-node-map-loader.ts +36 -142
  106. package/src/manager/loaders/step4_model-list-loader.ts +25 -36
  107. package/src/manager/loaders/step5_alter-list-loader.ts +29 -0
  108. package/src/manager/loaders/step6_github-stats-loader.ts +23 -0
  109. package/src/manager/script-update-manager.bun.ts +9 -1
  110. package/src/manager/types/ComfyManagerAlterInfo.ts +13 -0
  111. package/src/manager/types/ComfyManagerFileModelInfo.ts +7 -3
  112. package/src/manager/types/ComfyManagerFilePluginContent.ts +6 -9
  113. package/src/manager/types/ComfyManagerFilePluginList.ts +3 -3
  114. package/src/manager/types/ComfyManagerGithubStats.ts +9 -0
  115. package/src/manager/types/ComfyManagerPluginContentMetadata.ts +2 -3
  116. package/src/manager/types/ComfyManagerPluginInfo.ts +48 -34
  117. package/src/sdk-generator/ComfySchema.ts +14 -1
  118. package/src/sdk-generator/comfyui-sdk-codegen.ts +15 -0
  119. package/src/sdk-generator/inputWidgetKind.ts +17 -0
  120. package/src/vars/ComfyVars.ts +82 -2
  121. package/src/vars/DefinedWorkflow.ts +7 -2
  122. package/dist/DefinedWorkflow-O3ynII3Y.js.map +0 -1
  123. package/dist/run-tui-BFhba_oG.js.map +0 -1
  124. package/src/utils/tryExtracting.ts +0 -41
@@ -0,0 +1,288 @@
1
+ import { existsSync, readFileSync, statSync } from 'node:fs'
2
+ import { homedir } from 'node:os'
3
+ import { makeAutoObservable, reaction } from 'mobx'
4
+ import { basename, dirname, resolve } from 'pathe'
5
+ import { listImageDir, parentDir } from 'src/cli/tui/imagePicker/fsListing.ts'
6
+ import { pickerPrefs } from 'src/cli/tui/imagePicker/pickerPrefs.ts'
7
+ import { extractErrorMessage } from 'src/utils/extractErrorMessage.ts'
8
+ import { fuzzyMatch } from 'src/utils/fuzzyMatch.ts'
9
+ import { imageBufferToAnsi } from 'src/utils/ansiImage.ts'
10
+ import type { ImageVar } from 'src/vars/ComfyVars.ts'
11
+ import type { TuiSt } from 'src/cli/tui/state/TuiSt.ts'
12
+
13
+ export type PickerPane = 'browse' | 'favorites' | 'recents'
14
+ export const PICKER_PANES: readonly PickerPane[] = ['browse', 'favorites', 'recents']
15
+
16
+ export type PickerRow =
17
+ | { kind: 'dir'; name: string; path: string }
18
+ | { kind: 'image'; name: string; path: string; missing: boolean }
19
+
20
+ /** `~`-contract $HOME for display (ImageVar.display's convention) */
21
+ export function contractHome(path: string): string {
22
+ const home = homedir()
23
+ return path.startsWith(`${home}/`) ? `~${path.slice(home.length)}` : path
24
+ }
25
+
26
+ /**
27
+ * image-picker overlay (mode 'overlay-image'): browse the disk for an image
28
+ * var, three panes (browse / favorites / recents), filter-as-you-type, the
29
+ * highlighted image previews into the panel via PreviewSt's overlay slot.
30
+ */
31
+ export class ImagePickerSt {
32
+ constructor(private st: TuiSt) {
33
+ makeAutoObservable<ImagePickerSt, 'st'>(this, { st: false })
34
+ // the highlighted image's preview follows the cursor (debounced so
35
+ // arrowing fast never thrashes sharp; renders into the preview panel)
36
+ this.st.disposers.push(
37
+ reaction(
38
+ () => (this.st.mode === 'overlay-image' ? this.highlightedImage : null),
39
+ () => void this.refreshPreview(),
40
+ { delay: 120 },
41
+ ),
42
+ )
43
+ }
44
+
45
+ pane: PickerPane = 'browse'
46
+ /** current browse folder, absolute */
47
+ folder: string = process.cwd()
48
+ ix: number = 0
49
+ filter: string = ''
50
+
51
+ /** kind-narrowing cast sanctioned (agent/coding.md), `kind` is the tag */
52
+ get selectedVar(): ImageVar | null {
53
+ const sel = this.st.selected?.[1]
54
+ return sel?.kind === 'image' ? (sel as ImageVar) : null
55
+ }
56
+
57
+ get folderDisplay(): string {
58
+ return contractHome(this.folder)
59
+ }
60
+
61
+ /** begin() open order: dirname(value) → persisted lastFolder → opts.folder → cwd */
62
+ private initialFolder(iv: ImageVar): string {
63
+ const candidates: (string | null)[] = [
64
+ iv.isSet() ? dirname(iv.absPath()) : null,
65
+ pickerPrefs().lastFolder,
66
+ iv.opts.folder != null ? resolve(iv.opts.folder) : null,
67
+ ]
68
+ for (const c of candidates) {
69
+ try {
70
+ if (c != null && statSync(c).isDirectory()) return c
71
+ } catch {
72
+ // vanished candidate: fall through to the next one
73
+ }
74
+ }
75
+ return process.cwd()
76
+ }
77
+
78
+ begin(): void {
79
+ const iv = this.selectedVar
80
+ if (iv == null) return
81
+ this.st.mode = 'overlay-image'
82
+ this.pane = 'browse'
83
+ this.filter = ''
84
+ this.ix = 0
85
+ this.folder = this.initialFolder(iv)
86
+ // fresh overlay starts on the placeholder, never a stale slot image
87
+ this.st.preview.clearOverlayImage()
88
+ }
89
+
90
+ cancel(): void {
91
+ this.st.mode = 'nav'
92
+ }
93
+
94
+ // ---- listing ----
95
+ /** every row of the active pane BEFORE the filter, plus the browse-pane
96
+ * read error (unreadable folder: shown IN the overlay, browsing elsewhere
97
+ * stays possible). Computed with NO side effects — mobx forbids them. */
98
+ private get paneListing(): { rows: PickerRow[]; error: string | null } {
99
+ if (this.pane === 'favorites')
100
+ return {
101
+ rows: pickerPrefs().favorites.map((dir) => ({ kind: 'dir', name: contractHome(dir), path: dir })),
102
+ error: null,
103
+ }
104
+ if (this.pane === 'recents')
105
+ return {
106
+ rows: pickerPrefs().recents.map((img) => ({
107
+ kind: 'image',
108
+ name: contractHome(img),
109
+ path: img,
110
+ missing: !existsSync(img),
111
+ })),
112
+ error: null,
113
+ }
114
+ const iv = this.selectedVar
115
+ if (iv == null) return { rows: [], error: null }
116
+ try {
117
+ const listing = listImageDir(this.folder, iv.extensions)
118
+ return {
119
+ rows: [
120
+ ...listing.dirs.map((d): PickerRow => ({ kind: 'dir', name: d, path: resolve(this.folder, d) })),
121
+ ...listing.images.map(
122
+ (f): PickerRow => ({ kind: 'image', name: f, path: resolve(this.folder, f), missing: false }),
123
+ ),
124
+ ],
125
+ error: null,
126
+ }
127
+ } catch (e) {
128
+ return { rows: [], error: extractErrorMessage(e) }
129
+ }
130
+ }
131
+
132
+ get listingError(): string | null {
133
+ return this.paneListing.error
134
+ }
135
+
136
+ /** what the overlay renders: pane rows surviving the filter */
137
+ get rows(): PickerRow[] {
138
+ const all = this.paneListing.rows
139
+ if (this.filter === '') return all
140
+ return all.filter((r) => fuzzyMatch(this.filter, r.name))
141
+ }
142
+
143
+ get current(): PickerRow | null {
144
+ return this.rows[this.ix] ?? null
145
+ }
146
+
147
+ /** feeds the preview reaction: only a real, present image highlights */
148
+ get highlightedImage(): string | null {
149
+ const row = this.current
150
+ return row?.kind === 'image' && !row.missing ? row.path : null
151
+ }
152
+
153
+ // ---- navigation ----
154
+ move(delta: number): void {
155
+ const len = this.rows.length
156
+ if (len === 0) return
157
+ this.ix = (this.ix + delta + len) % len
158
+ }
159
+
160
+ cyclePane(): void {
161
+ const ix = PICKER_PANES.indexOf(this.pane)
162
+ this.pane = PICKER_PANES[(ix + 1) % PICKER_PANES.length] ?? 'browse'
163
+ this.ix = 0
164
+ this.filter = ''
165
+ }
166
+
167
+ private goto(dir: string): void {
168
+ this.pane = 'browse'
169
+ this.folder = dir
170
+ this.ix = 0
171
+ this.filter = ''
172
+ }
173
+
174
+ /** `→`: enter the highlighted dir (browse pane; images select via ⏎ only) */
175
+ enter(): void {
176
+ const row = this.current
177
+ if (this.pane === 'browse' && row?.kind === 'dir') this.goto(row.path)
178
+ }
179
+
180
+ /** `←`: parent dir (browse pane; the fs root is its own parent) */
181
+ goParent(): void {
182
+ if (this.pane !== 'browse') return
183
+ const parent = parentDir(this.folder)
184
+ if (parent !== this.folder) this.goto(parent)
185
+ }
186
+
187
+ /** ⏎: enter a dir (a favorite jumps browse into it), select an image */
188
+ commit(): void {
189
+ const row = this.current
190
+ if (row == null) return
191
+ if (row.kind === 'dir') return this.goto(row.path)
192
+ if (row.missing) return // vanished recent: dim row, never a broken value
193
+ this.select(row.path)
194
+ }
195
+
196
+ /** the pick: absolute path into the var, recents + lastFolder recorded */
197
+ private select(absPath: string): void {
198
+ const iv = this.selectedVar
199
+ if (iv == null) return
200
+ iv.set(absPath)
201
+ pickerPrefs().recordPick(absPath)
202
+ this.st.mode = 'nav'
203
+ }
204
+
205
+ /** ⌃F: favorite FOLDERS only — a dir row favorites it, an image row favorites the current folder */
206
+ toggleFavorite(): void {
207
+ const row = this.current
208
+ const dir =
209
+ row == null
210
+ ? this.pane === 'browse'
211
+ ? this.folder
212
+ : null
213
+ : row.kind === 'dir'
214
+ ? row.path
215
+ : this.pane === 'browse'
216
+ ? this.folder
217
+ : dirname(row.path)
218
+ if (dir == null) return
219
+ pickerPrefs().toggleFavorite(dir)
220
+ // favorites pane: the row may vanish under the cursor — clamp
221
+ this.ix = Math.max(0, Math.min(this.ix, this.rows.length - 1))
222
+ }
223
+
224
+ isFavorite(dir: string): boolean {
225
+ return pickerPrefs().isFavorite(dir)
226
+ }
227
+
228
+ // ---- filter (plain chars ALWAYS filter, LorasSt precedent) ----
229
+ filterInput(chunk: string): void {
230
+ this.filter += chunk
231
+ this.ix = 0
232
+ }
233
+
234
+ filterBackspace(): void {
235
+ this.filter = this.filter.slice(0, -1)
236
+ this.ix = 0
237
+ }
238
+
239
+ /** ⌥⌫ / ⌃W: drop the last word of the filter */
240
+ filterDeleteWord(): void {
241
+ this.filter = this.filter.replace(/\s*\S+\s*$/, '')
242
+ this.ix = 0
243
+ }
244
+
245
+ // ---- preview into the shared overlay slot (debounced reaction above) ----
246
+ private _busy: boolean = false
247
+
248
+ async refreshPreview(): Promise<void> {
249
+ if (this.st.mode !== 'overlay-image') {
250
+ // debounced close: another overlay may ALREADY own the slot — never wipe its image
251
+ if (!this.st.preview.overlayActive) this.st.preview.clearOverlayImage()
252
+ return
253
+ }
254
+ const path = this.highlightedImage
255
+ if (path == null) {
256
+ // dir row / empty pane: clear so a stale image never lingers
257
+ this.st.preview.clearOverlayImage()
258
+ return
259
+ }
260
+ if (this._busy) return
261
+ this._busy = true
262
+ try {
263
+ // local file: bytes read directly, no host round-trip
264
+ const bytes = new Uint8Array(readFileSync(path))
265
+ if (this.st.preview.useNative) {
266
+ this.st.preview.setOverlayImage({ bytes, ansi: null, name: basename(path), note: null })
267
+ } else {
268
+ const ansi = await imageBufferToAnsi(bytes, {
269
+ width: this.st.preview.width,
270
+ height: this.st.preview.height,
271
+ })
272
+ this.st.preview.setOverlayImage({ bytes: null, ansi, name: basename(path), note: null })
273
+ }
274
+ } catch (e) {
275
+ // e.g. a file sharp can't decode — placeholder note, never a crash
276
+ this.st.preview.setOverlayImage({
277
+ bytes: null,
278
+ ansi: null,
279
+ name: basename(path),
280
+ note: extractErrorMessage(e),
281
+ })
282
+ } finally {
283
+ this._busy = false
284
+ // the cursor may have moved while we rendered: catch up once
285
+ if (this.st.mode === 'overlay-image' && this.highlightedImage !== path) void this.refreshPreview()
286
+ }
287
+ }
288
+ }
@@ -0,0 +1,46 @@
1
+ import { readdirSync, statSync } from 'node:fs'
2
+ import { dirname, join } from 'pathe'
3
+
4
+ export type DirListing = { dirs: string[]; images: string[] }
5
+
6
+ /** `name.PNG` matches extensions `['png']` — extensions are lowercase, no dots */
7
+ export function hasImageExtension(name: string, extensions: readonly string[]): boolean {
8
+ const dot = name.lastIndexOf('.')
9
+ if (dot <= 0) return false
10
+ return extensions.includes(name.slice(dot + 1).toLowerCase())
11
+ }
12
+
13
+ /**
14
+ * ONE folder for the picker: dirs first then images, both alphabetical,
15
+ * dotfiles skipped, images filtered by extension. Symlinks are followed
16
+ * (a dangling one is skipped). Pure over fs — an unreadable dir THROWS,
17
+ * the caller surfaces it (never a silent empty listing).
18
+ */
19
+ export function listImageDir(dir: string, extensions: readonly string[]): DirListing {
20
+ const dirs: string[] = []
21
+ const images: string[] = []
22
+ for (const entry of readdirSync(dir, { withFileTypes: true })) {
23
+ if (entry.name.startsWith('.')) continue
24
+ let isDir = entry.isDirectory()
25
+ let isFile = entry.isFile()
26
+ if (entry.isSymbolicLink()) {
27
+ try {
28
+ const target = statSync(join(dir, entry.name))
29
+ isDir = target.isDirectory()
30
+ isFile = target.isFile()
31
+ } catch {
32
+ continue // dangling symlink: neither dir nor image
33
+ }
34
+ }
35
+ if (isDir) dirs.push(entry.name)
36
+ else if (isFile && hasImageExtension(entry.name, extensions)) images.push(entry.name)
37
+ }
38
+ dirs.sort((a, b) => a.localeCompare(b))
39
+ images.sort((a, b) => a.localeCompare(b))
40
+ return { dirs, images }
41
+ }
42
+
43
+ /** parent for `←` navigation — the fs root is its own parent (never ascends past it) */
44
+ export function parentDir(dir: string): string {
45
+ return dirname(dir)
46
+ }
@@ -0,0 +1,78 @@
1
+ import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs'
2
+ import { makeAutoObservable } from 'mobx'
3
+ import { dirname, join } from 'pathe'
4
+ import { logError } from 'src/utils/log.ts'
5
+
6
+ export const RECENTS_CAP = 20
7
+
8
+ /**
9
+ * image-picker favorites + recents + lastFolder — loraKeywords pattern:
10
+ * observable, loaded once, written through on every mutation. Human-editable
11
+ * json at `.comfy-ts/image-picker.json` (hand-tuned data, NOT under cache/ —
12
+ * survives a cache wipe):
13
+ * { "favorites": ["/abs/dir"], "recents": ["/abs/img.png"], "lastFolder": "/abs/dir" }
14
+ * A corrupt file logs loud and starts fresh, never blocks the TUI. The class
15
+ * takes its file path explicitly so tests round-trip it headless; the TUI
16
+ * uses the ONE lazy module singleton below.
17
+ */
18
+ export class PickerPrefs {
19
+ /** favorite FOLDERS, absolute — recents cover files */
20
+ favorites: string[] = []
21
+ /** picked images, absolute, newest first, deduped, capped */
22
+ recents: string[] = []
23
+ lastFolder: string | null = null
24
+
25
+ constructor(public readonly path: string) {
26
+ this.load()
27
+ makeAutoObservable(this)
28
+ }
29
+
30
+ private load(): void {
31
+ if (!existsSync(this.path)) return
32
+ try {
33
+ const raw: unknown = JSON.parse(readFileSync(this.path, 'utf8'))
34
+ if (raw == null || typeof raw !== 'object') throw new Error('not a json object')
35
+ const o = raw as Record<string, unknown>
36
+ if (Array.isArray(o.favorites)) this.favorites = o.favorites.filter((f): f is string => typeof f === 'string')
37
+ if (Array.isArray(o.recents))
38
+ this.recents = o.recents.filter((r): r is string => typeof r === 'string').slice(0, RECENTS_CAP)
39
+ if (typeof o.lastFolder === 'string') this.lastFolder = o.lastFolder
40
+ } catch (e) {
41
+ logError(`[imagePicker] unreadable ${this.path} — starting fresh: ${String(e)}`)
42
+ }
43
+ }
44
+
45
+ private write(): void {
46
+ try {
47
+ mkdirSync(dirname(this.path), { recursive: true })
48
+ const json = { favorites: this.favorites, recents: this.recents, lastFolder: this.lastFolder }
49
+ writeFileSync(this.path, `${JSON.stringify(json, null, 2)}\n`)
50
+ } catch (e) {
51
+ logError(`[imagePicker] write failed ${this.path}: ${String(e)}`)
52
+ }
53
+ }
54
+
55
+ isFavorite(dir: string): boolean {
56
+ return this.favorites.includes(dir)
57
+ }
58
+
59
+ toggleFavorite(dir: string): void {
60
+ this.favorites = this.favorites.includes(dir) ? this.favorites.filter((f) => f !== dir) : [...this.favorites, dir]
61
+ this.write()
62
+ }
63
+
64
+ /** a pick records the image (newest first, deduped, capped) AND remembers its folder */
65
+ recordPick(imagePath: string): void {
66
+ this.recents = [imagePath, ...this.recents.filter((r) => r !== imagePath)].slice(0, RECENTS_CAP)
67
+ this.lastFolder = dirname(imagePath)
68
+ this.write()
69
+ }
70
+ }
71
+
72
+ let _prefs: PickerPrefs | null = null
73
+
74
+ /** the TUI's store, bound lazily to `.comfy-ts/image-picker.json` (gitignored) */
75
+ export function pickerPrefs(): PickerPrefs {
76
+ if (_prefs == null) _prefs = new PickerPrefs(join(comfyts.baseFolder, 'image-picker.json'))
77
+ return _prefs
78
+ }
@@ -3,7 +3,8 @@ import { pathToFileURL } from 'node:url'
3
3
  import { render } from 'ink'
4
4
  import { dirname, resolve } from 'pathe'
5
5
  import { COMFY_TS_TUI_ENV } from 'src/cli/tui-env.ts'
6
- import { bundledExamplesDir, mergeWorkflowSources, scanCflowFiles } from 'src/cli/tui/discoverWorkflows.ts'
6
+ import { mergeWorkflowSources, scanCflowFiles } from 'src/cli/tui/discoverWorkflows.ts'
7
+ import { bundledExamplesDir } from 'src/exampleAssets.ts'
7
8
  import { findDefinedWorkflow } from 'src/cli/tui/findDefinedWorkflow.ts'
8
9
  import { installProtocolImagePainter } from 'src/cli/tui/protocolImagePainter.ts'
9
10
  import { TuiApp } from 'src/cli/tui/components/TuiApp.tsx'
@@ -1,4 +1,4 @@
1
- import { makeAutoObservable, reaction, runInAction } from 'mobx'
1
+ import { makeAutoObservable, reaction } from 'mobx'
2
2
  import { fetchLoraPreviewBytes, fetchLoraPreviewMap, loraPreviewKey } from 'src/host/loraManagerApi.ts'
3
3
  import { imageBufferToAnsi } from 'src/utils/ansiImage.ts'
4
4
  import { extractErrorMessage } from 'src/utils/extractErrorMessage.ts'
@@ -43,6 +43,8 @@ export class LorasSt {
43
43
  this.st.mode = 'overlay-loras'
44
44
  this.ix = 0
45
45
  this.filter = ''
46
+ // fresh overlay starts on the placeholder, never a stale slot image
47
+ this.st.preview.clearOverlayImage()
46
48
  }
47
49
 
48
50
  move(delta: number): void {
@@ -106,26 +108,16 @@ export class LorasSt {
106
108
  return this.filteredNames[this.ix] ?? null
107
109
  }
108
110
 
109
- previewAnsi: string | null = null
110
- /** protocol mode: raw preview bytes for the overlay painter */
111
- previewBytes: Uint8Array | null = null
112
- previewName: string | null = null
113
- /** placeholder line when no image can be shown (missing extension, no preview, video preview…) */
114
- previewNote: string | null = null
115
-
116
111
  /** undefined = not fetched yet · null = extension absent */
117
112
  private previewMap: Map<string, string> | null | undefined = undefined
118
113
  private previewMapHostId: string | null = null
119
114
  private _busy: boolean = false
120
115
 
116
+ /** preview goes through PreviewSt's shared overlay slot (one code path with the image picker) */
121
117
  async refreshPreview(): Promise<void> {
122
118
  if (this.st.mode !== 'overlay-loras') {
123
- runInAction(() => {
124
- this.previewAnsi = null
125
- this.previewBytes = null
126
- this.previewName = null
127
- this.previewNote = null
128
- })
119
+ // debounced close: another overlay may ALREADY own the slot — never wipe its image
120
+ if (!this.st.preview.overlayActive) this.st.preview.clearOverlayImage()
129
121
  return
130
122
  }
131
123
  const name = this.selectedName
@@ -141,12 +133,7 @@ export class LorasSt {
141
133
  const url = map?.get(loraPreviewKey(name))
142
134
  const note = map == null ? 'lora-manager extension not detected' : url == null ? 'no preview available' : null
143
135
  if (url == null) {
144
- runInAction(() => {
145
- this.previewAnsi = null
146
- this.previewBytes = null
147
- this.previewName = name
148
- this.previewNote = note
149
- })
136
+ this.st.preview.setOverlayImage({ bytes: null, ansi: null, name: `lora ${name}`, note })
150
137
  return
151
138
  }
152
139
  const bytes = await fetchLoraPreviewBytes(host, url)
@@ -154,27 +141,18 @@ export class LorasSt {
154
141
  if (bytes == null) throw new Error('no image preview on the server')
155
142
  if (this.st.preview.useNative) {
156
143
  // the overlay painter shows the REAL image — no ansi rendering needed
157
- runInAction(() => {
158
- this.previewBytes = bytes
159
- this.previewAnsi = null
160
- this.previewName = name
161
- this.previewNote = null
162
- })
144
+ this.st.preview.setOverlayImage({ bytes, ansi: null, name: `lora ${name}`, note: null })
163
145
  return
164
146
  }
165
147
  const ansi = await imageBufferToAnsi(bytes, { width: this.st.preview.width, height: this.st.preview.height })
166
- runInAction(() => {
167
- this.previewAnsi = ansi
168
- this.previewName = name
169
- this.previewNote = null
170
- })
148
+ this.st.preview.setOverlayImage({ bytes: null, ansi, name: `lora ${name}`, note: null })
171
149
  } catch (e) {
172
150
  // e.g. video previews sharp can't decode — placeholder, not an incident
173
- runInAction(() => {
174
- this.previewAnsi = null
175
- this.previewBytes = null
176
- this.previewName = name
177
- this.previewNote = extractErrorMessage(e)
151
+ this.st.preview.setOverlayImage({
152
+ bytes: null,
153
+ ansi: null,
154
+ name: `lora ${name}`,
155
+ note: extractErrorMessage(e),
178
156
  })
179
157
  } finally {
180
158
  this._busy = false
@@ -15,6 +15,15 @@ export function protocolCapable(): boolean {
15
15
 
16
16
  type MenuRow = { key: 'panel' | 'renderer' | 'during-run'; label: string; value: string }
17
17
 
18
+ /** what an overlay (loras / image picker) wants the panel to show: real bytes
19
+ * (native renderer), an ansi render (pixel), or a placeholder note */
20
+ export type OverlayImage = {
21
+ bytes: Uint8Array | null
22
+ ansi: string | null
23
+ name: string | null
24
+ note: string | null
25
+ }
26
+
18
27
  /** small-latent corner: fraction of the panel each dimension takes */
19
28
  const CORNER_FRACTION = 0.38
20
29
  /** pixel 'latent small': the sharp-composited thumb takes this fraction of the panel */
@@ -61,6 +70,23 @@ export class PreviewSt {
61
70
  get menuOpen(): boolean {
62
71
  return this.st.mode === 'preview'
63
72
  }
73
+
74
+ // ---- overlay slot: ONE code path for every overlay that previews into the
75
+ // panel (loras + image picker write it; the panel and the protocol painter
76
+ // read it whenever an overlay mode owns the vars area) ----
77
+ overlay: OverlayImage | null = null
78
+
79
+ get overlayActive(): boolean {
80
+ return this.st.mode === 'overlay-loras' || this.st.mode === 'overlay-image'
81
+ }
82
+
83
+ setOverlayImage(p: OverlayImage): void {
84
+ this.overlay = p
85
+ }
86
+
87
+ clearOverlayImage(): void {
88
+ this.overlay = null
89
+ }
64
90
  /** running + latents wanted + sampling well underway + none arrived = server
65
91
  * sends none. The ≥20% gate keeps healthy runs from red-flashing: the first
66
92
  * progress tick lands a beat before the first preview frame */
@@ -157,7 +183,7 @@ export class PreviewSt {
157
183
  /** what the painter should show RIGHT NOW (native renderer only; mirrors the panel's source) */
158
184
  get protocolImage(): Uint8Array | null {
159
185
  if (!this.useNative || !this.show || this.menuOpen) return null
160
- if (this.st.mode === 'overlay-loras') return this.st.loras.previewBytes
186
+ if (this.overlayActive) return this.overlay?.bytes ?? null
161
187
  if (this.st.exec.running) {
162
188
  if (this.duringRun === 'latent') return this.latentBytes ?? this.outputBytes
163
189
  // latent-small keeps the OUTPUT big (corner carries the latent) — last-output too
@@ -169,7 +195,7 @@ export class PreviewSt {
169
195
  /** small latent painted over the panel's top-right corner (native + 'latent small' only) */
170
196
  get protocolImageCorner(): Uint8Array | null {
171
197
  if (!this.useNative || !this.show || this.menuOpen) return null
172
- if (this.st.mode === 'overlay-loras') return null
198
+ if (this.overlayActive) return null
173
199
  if (this.st.exec.running && this.duringRun === 'latent-small') return this.latentBytes
174
200
  return null
175
201
  }
@@ -1,5 +1,6 @@
1
1
  import { makeAutoObservable, observable, runInAction } from 'mobx'
2
2
  import type { SeedVar, ToggleVar, AnyVar } from 'src/vars/ComfyVars.ts'
3
+ import { ImagePickerSt } from 'src/cli/tui/imagePicker/ImagePickerSt.ts'
3
4
  import { DraftsSt } from 'src/cli/tui/state/DraftsSt.ts'
4
5
  import { EditorSt } from 'src/cli/tui/state/EditorSt.ts'
5
6
  import { ExecSt } from 'src/cli/tui/state/ExecSt.ts'
@@ -27,6 +28,7 @@ export type TuiMode =
27
28
  | 'overlay-choice'
28
29
  | 'overlay-size'
29
30
  | 'overlay-loras'
31
+ | 'overlay-image'
30
32
  | 'overlay-drafts'
31
33
 
32
34
  /**
@@ -41,6 +43,7 @@ export class TuiSt {
41
43
  editor: EditorSt
42
44
  picker: PickerSt
43
45
  loras: LorasSt
46
+ imagePicker: ImagePickerSt
44
47
  workflows: WorkflowsSt
45
48
  tree: TreeSt
46
49
  drafts: DraftsSt
@@ -75,6 +78,7 @@ export class TuiSt {
75
78
  editor: false,
76
79
  picker: false,
77
80
  loras: false,
81
+ imagePicker: false,
78
82
  workflows: false,
79
83
  tree: false,
80
84
  drafts: false,
@@ -89,6 +93,7 @@ export class TuiSt {
89
93
  this.editor = new EditorSt(this)
90
94
  this.picker = new PickerSt(this)
91
95
  this.loras = new LorasSt(this)
96
+ this.imagePicker = new ImagePickerSt(this)
92
97
  this.workflows = new WorkflowsSt(
93
98
  this,
94
99
  opts.workflowFiles ?? [],
@@ -172,6 +177,7 @@ export class TuiSt {
172
177
  else if (sel.kind === 'choice') this.picker.beginChoice()
173
178
  else if (sel.kind === 'size') this.picker.beginSize()
174
179
  else if (sel.kind === 'loras') this.loras.begin()
180
+ else if (sel.kind === 'image') this.imagePicker.begin()
175
181
  else this.editor.beginInline() // int / float / seed: type the number
176
182
  }
177
183
 
@@ -0,0 +1,56 @@
1
+ import { existsSync, readFileSync } from 'node:fs'
2
+ import { fileURLToPath } from 'node:url'
3
+ import { dirname, join } from 'pathe'
4
+
5
+ /**
6
+ * the examples/ folder shipped in the npm tarball, resolved from THIS module's
7
+ * own location (NEVER cwd): walking up to the nearest NAMED package.json finds
8
+ * <pkg>/examples both from a consumer's node_modules/comfy-ts/dist/ and from
9
+ * this repo's src/. Returns null when the examples are absent (a consumer may
10
+ * prune the folder) or when the nearest named package is not comfy-ts (the lib
11
+ * was inlined into someone else's bundle — its examples are not on disk); no
12
+ * package.json all the way up is a broken install and throws.
13
+ */
14
+ export function bundledExamplesDir(moduleUrl: string = import.meta.url): string | null {
15
+ let dir = dirname(fileURLToPath(moduleUrl))
16
+ let prev = ''
17
+ while (dir !== prev) {
18
+ const pkgPath = join(dir, 'package.json')
19
+ if (existsSync(pkgPath)) {
20
+ let pkg: { name?: string } = {}
21
+ try {
22
+ pkg = JSON.parse(readFileSync(pkgPath, 'utf8'))
23
+ } catch {
24
+ // a malformed package.json on the walk (someone's broken fixture
25
+ // dir) must not take the whole TUI down; treat as unnamed
26
+ }
27
+ if (pkg.name === 'comfy-ts') {
28
+ const examples = join(dir, 'examples')
29
+ return existsSync(examples) ? examples : null
30
+ }
31
+ if (pkg.name != null) return null
32
+ }
33
+ prev = dir
34
+ dir = dirname(dir)
35
+ }
36
+ throw new Error(`[comfy-ts] no package.json found walking up from ${fileURLToPath(moduleUrl)}`)
37
+ }
38
+
39
+ /**
40
+ * absolute path of a bundled `examples/images/` input image, e.g.
41
+ * `exampleImagePath('bear_1024x1024.jpg')` — the out-of-the-box default for
42
+ * i2i/i2v example image vars, valid from this repo AND from a consumer's
43
+ * node_modules/comfy-ts/. Loud throw naming the path when the file is missing
44
+ * or the package's examples/ folder was pruned.
45
+ */
46
+ export function exampleImagePath(name: string): string {
47
+ const examples = bundledExamplesDir()
48
+ if (examples == null) {
49
+ throw new Error(
50
+ `[comfy-ts] cannot resolve bundled example image '${name}': the package's examples/ folder is not on disk`,
51
+ )
52
+ }
53
+ const abs = join(examples, 'images', name)
54
+ if (!existsSync(abs)) throw new Error(`[comfy-ts] bundled example image not found: ${abs}`)
55
+ return abs
56
+ }