comfy-ts 2.14.0 → 2.16.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 (75) hide show
  1. package/README.md +1 -0
  2. package/dist/{LogsSt-CUeo843w.js → LogsSt-Ct7Le0S_.js} +2 -2
  3. package/dist/{LogsSt-CUeo843w.js.map → LogsSt-Ct7Le0S_.js.map} +1 -1
  4. package/dist/cli.js +4 -4
  5. package/dist/index.cjs +268 -55
  6. package/dist/index.cjs.map +1 -1
  7. package/dist/index.d.cts +176 -26
  8. package/dist/index.d.ts +176 -26
  9. package/dist/index.js +262 -56
  10. package/dist/index.js.map +1 -1
  11. package/dist/{run-serve-DUqcn0h3.js → run-serve-C78hD7t9.js} +119 -40
  12. package/dist/run-serve-C78hD7t9.js.map +1 -0
  13. package/dist/{run-tui-D-LaSqgB.js → run-tui-nPETFRmk.js} +15 -9
  14. package/dist/run-tui-nPETFRmk.js.map +1 -0
  15. package/dist/{schemaDrift-E95iTF7E.js → schemaDrift-wb5XkrHV.js} +218 -27
  16. package/dist/schemaDrift-wb5XkrHV.js.map +1 -0
  17. package/dist/serve-web.js +64 -50
  18. package/dist/{state-BytbdZ8s.js → state-BMW57_M0.js} +45 -35
  19. package/dist/state-BMW57_M0.js.map +1 -0
  20. package/dist/web.d.ts +173 -26
  21. package/dist/web.js +251 -53
  22. package/dist/web.js.map +1 -1
  23. package/examples/README.md +1 -0
  24. package/examples/media/README.md +9 -0
  25. package/examples/media/sine_440hz_3s.flac +0 -0
  26. package/examples/media/testsrc_256x256_3s.mp4 +0 -0
  27. package/examples/media/voice_en_6s.flac +0 -0
  28. package/examples/rvion/04-krea2-turbo-t2i.cflow.ts +9 -1
  29. package/examples/rvion/10-anima-t2i.cflow.ts +8 -1
  30. package/examples/rvion/14-video-soundtrack.cflow.ts +47 -0
  31. package/examples/rvion/15-tts-voice-clone.cflow.ts +111 -0
  32. package/guide-for-agents.md +3 -1
  33. package/package.json +2 -2
  34. package/src/cli/serve/ServeApp.ts +115 -32
  35. package/src/cli/serve/applyVarPayload.ts +5 -3
  36. package/src/cli/serve/describeVar.ts +5 -3
  37. package/src/cli/serve/resultHistory.ts +22 -6
  38. package/src/cli/serve/run-serve.ts +4 -1
  39. package/src/cli/serve/web/api.ts +4 -0
  40. package/src/cli/serve/web/components/DraftTabs.tsx +2 -1
  41. package/src/cli/serve/web/components/Gallery.tsx +86 -5
  42. package/src/cli/serve/web/components/HistoryPicker.tsx +5 -3
  43. package/src/cli/serve/web/components/MenuButton.tsx +2 -4
  44. package/src/cli/serve/web/components/Omnibox.tsx +2 -1
  45. package/src/cli/serve/web/components/PromptEnhancer.tsx +4 -3
  46. package/src/cli/serve/web/components/ShortcutsModal.tsx +2 -1
  47. package/src/cli/serve/web/components/SideMenu.tsx +2 -1
  48. package/src/cli/serve/web/components/VarsForm.tsx +5 -3
  49. package/src/cli/serve/web/components/controls/LorasControl.tsx +4 -3
  50. package/src/cli/serve/web/components/controls/MediaControl.tsx +111 -0
  51. package/src/cli/serve/web/components/controls/PresetPicker.tsx +2 -1
  52. package/src/cli/serve/web/components/controls/PromptControl.tsx +2 -1
  53. package/src/cli/serve/web/components/controls/SizeControl.tsx +2 -1
  54. package/src/cli/serve/web/state/FormSt.ts +70 -2
  55. package/src/cli/serve/web/state/escape.ts +16 -0
  56. package/src/cli/serve/web/state/host.ts +2 -0
  57. package/src/cli/serve/web/state/mediaDrop.ts +76 -0
  58. package/src/cli/serve/web/styles.ts +16 -2
  59. package/src/cli/tui/imagePicker/ImagePickerOverlay.tsx +4 -1
  60. package/src/cli/tui/imagePicker/ImagePickerSt.ts +15 -6
  61. package/src/cli/tui/state/TuiSt.ts +1 -1
  62. package/src/coreExports.ts +20 -1
  63. package/src/exampleAssets.ts +13 -3
  64. package/src/host/ComfyUploader.ts +34 -27
  65. package/src/index.ts +1 -1
  66. package/src/runner/ComfyExecution.ts +38 -20
  67. package/src/runner/ComfyWsApi.ts +3 -0
  68. package/src/runner/mediaKinds.ts +51 -0
  69. package/src/vars/ComfyVars.ts +178 -34
  70. package/src/vars/DefinedWorkflow.ts +35 -2
  71. package/dist/run-serve-DUqcn0h3.js.map +0 -1
  72. package/dist/run-tui-D-LaSqgB.js.map +0 -1
  73. package/dist/schemaDrift-E95iTF7E.js.map +0 -1
  74. package/dist/state-BytbdZ8s.js.map +0 -1
  75. package/src/cli/serve/web/components/controls/ImageControl.tsx +0 -72
@@ -19,6 +19,8 @@ export type RunRecord = {
19
19
  texts: { nodeKey: string | null; text: string }[]
20
20
  images: RunOutput[]
21
21
  audios: RunAudioOutput[]
22
+ /** absent on a record from an older server */
23
+ videos?: RunAudioOutput[]
22
24
  }
23
25
 
24
26
  export type MemoryUsage = { usedBytes: number; budgetBytes: number; outputs: number; runs: number }
@@ -32,7 +34,12 @@ export const KEPT_RUNS = 50
32
34
  export const DEFAULT_MEMORY_BUDGET_MB = 100
33
35
 
34
36
  /** 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)[] }
37
+ type Kept = {
38
+ record: RunRecord
39
+ imageKeys: (string | null)[]
40
+ audioKeys: (string | null)[]
41
+ videoKeys: (string | null)[]
42
+ }
36
43
 
37
44
  export class ResultHistory {
38
45
  /** newest first */
@@ -43,13 +50,20 @@ export class ResultHistory {
43
50
 
44
51
  constructor(private budgetBytes: number) {}
45
52
 
46
- add(p: { record: RunRecord; imageKeys: (string | null)[]; audioKeys: (string | null)[]; blobs: KeptBlob[] }): void {
53
+ add(p: {
54
+ record: RunRecord
55
+ imageKeys: (string | null)[]
56
+ audioKeys: (string | null)[]
57
+ videoKeys?: (string | null)[]
58
+ blobs: KeptBlob[]
59
+ }): void {
47
60
  this.remove(p.record.promptId)
48
61
  for (const b of p.blobs) {
49
62
  this.blobs.set(b.key, { bytes: b.bytes, contentType: b.contentType })
50
63
  this.used += b.bytes.byteLength
51
64
  }
52
- this.runs = [{ record: p.record, imageKeys: p.imageKeys, audioKeys: p.audioKeys }, ...this.runs]
65
+ const kept = { record: p.record, imageKeys: p.imageKeys, audioKeys: p.audioKeys, videoKeys: p.videoKeys ?? [] }
66
+ this.runs = [kept, ...this.runs]
53
67
  for (const dropped of this.runs.slice(KEPT_RUNS)) this.freeRun(dropped)
54
68
  this.runs = this.runs.slice(0, KEPT_RUNS)
55
69
  this.evict()
@@ -65,6 +79,7 @@ export class ResultHistory {
65
79
  ...k.record,
66
80
  images: k.record.images.map((img, ix) => this.withLiveUrl(img, k.imageKeys[ix])),
67
81
  audios: k.record.audios.map((a, ix) => this.withLiveUrl(a, k.audioKeys[ix])),
82
+ videos: (k.record.videos ?? []).map((a, ix) => this.withLiveUrl(a, k.videoKeys[ix])),
68
83
  }))
69
84
  }
70
85
 
@@ -96,7 +111,7 @@ export class ResultHistory {
96
111
  }
97
112
 
98
113
  private freeRun(k: Kept): void {
99
- for (const key of [...k.imageKeys, ...k.audioKeys]) {
114
+ for (const key of [...k.imageKeys, ...k.audioKeys, ...k.videoKeys]) {
100
115
  if (key == null) continue
101
116
  const b = this.blobs.get(key)
102
117
  if (b == null) continue
@@ -118,8 +133,9 @@ export class ResultHistory {
118
133
  this.runs = this.runs.filter((k) => {
119
134
  const live = (keys: (string | null)[]): boolean => keys.some((key) => key == null || this.blobs.has(key))
120
135
  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)
136
+ const hasNothing =
137
+ k.record.images.length === 0 && k.record.audios.length === 0 && (k.record.videos ?? []).length === 0
138
+ return hasText || hasNothing || live(k.imageKeys) || live(k.audioKeys) || live(k.videoKeys)
123
139
  })
124
140
  }
125
141
  }
@@ -18,6 +18,8 @@ import { extractErrorMessage } from 'src/utils/extractErrorMessage.ts'
18
18
 
19
19
  const DEFAULT_PORT = 8288
20
20
  const BODY_CAP = 10_000_000
21
+ /** a video dropped on a media var rides POST /upload as base64 json: ~200 MB of file */
22
+ export const UPLOAD_BODY_CAP = 280_000_000
21
23
 
22
24
  export function parseArgs(args: string[]): { target?: string; port: number; bind: string } | { error: string } {
23
25
  let target: string | undefined
@@ -56,6 +58,7 @@ export function makeRequestListener(app: ServeApp): (req: IncomingMessage, res:
56
58
  'access-control-allow-headers': 'content-type, accept',
57
59
  }
58
60
  return (req, res) => {
61
+ const cap = req.method === 'POST' && (req.url ?? '').split('?')[0] === '/upload' ? UPLOAD_BODY_CAP : BODY_CAP
59
62
  const chunks: Buffer[] = []
60
63
  let size = 0
61
64
  let overflow = false
@@ -63,7 +66,7 @@ export function makeRequestListener(app: ServeApp): (req: IncomingMessage, res:
63
66
  req.on('data', (c: Buffer) => {
64
67
  if (overflow) return
65
68
  size += c.length
66
- if (size > BODY_CAP) {
69
+ if (size > cap) {
67
70
  // answer NOW: a destroyed request never emits 'end'
68
71
  overflow = true
69
72
  res.writeHead(413, { 'content-type': 'application/json', ...cors })
@@ -32,6 +32,9 @@ export type GeneratedText = { nodeKey: string | null; text: string }
32
32
  /** an audio file (SaveAudio*, PreviewAudio): the panel plays it in an <audio> player */
33
33
  export type GeneratedAudio = { filename: string; mime: string; url: string | null; absPath: string | null }
34
34
 
35
+ /** a video file (SaveVideo, SaveWEBM, VHS_VideoCombine): the panel plays it in a <video> player */
36
+ export type GeneratedVideo = GeneratedAudio
37
+
35
38
  export type GenerateOk = {
36
39
  ok: true
37
40
  module: string
@@ -44,6 +47,7 @@ export type GenerateOk = {
44
47
  images: GeneratedImage[]
45
48
  texts?: GeneratedText[]
46
49
  audios?: GeneratedAudio[]
50
+ videos?: GeneratedVideo[]
47
51
  }
48
52
 
49
53
  async function jsonFetch<T>(url: string, init?: RequestInit): Promise<T> {
@@ -18,6 +18,7 @@ import { MOD_KEY } from 'src/cli/serve/web/components/modKey.ts'
18
18
  import { tabForKey, tabStepForKey, type DraftTab } from 'src/cli/serve/web/state/draftTabs.ts'
19
19
  import { shortcutLabel } from 'src/cli/serve/web/state/shortcuts.ts'
20
20
  import type { WebSt } from 'src/cli/serve/web/state/WebSt.ts'
21
+ import { onEscape } from 'src/cli/serve/web/state/escape.ts'
21
22
 
22
23
  export function useTabShortcuts(st: WebSt): void {
23
24
  useEffect(() => {
@@ -128,7 +129,7 @@ const TabMenu = observer(function TabMenu(p: { st: WebSt; tab: DraftTab; anchor:
128
129
  onChange={(e) => setName(e.target.value)}
129
130
  onKeyDown={(e) => {
130
131
  if (e.key === 'Enter') rename()
131
- if (e.key === 'Escape') p.onClose()
132
+ onEscape(e, () => p.onClose())
132
133
  }}
133
134
  />
134
135
  <button
@@ -4,7 +4,8 @@
4
4
  // the button hides where the clipboard api is absent
5
5
  import { Icon } from 'src/cli/serve/web/components/Icon.tsx'
6
6
  import { observer, useLocalObservable } from 'mobx-react-lite'
7
- import { useEffect, useRef, type ReactNode } from 'react'
7
+ import { useEffect, useRef, type DragEvent, type ReactNode } from 'react'
8
+ import { encodeDraggedOutput, OUTPUT_DRAG_MIME, type DraggedOutput } from 'src/cli/serve/web/state/mediaDrop.ts'
8
9
  import { isEphemeral } from 'src/cli/serve/web/state/keptResults.ts'
9
10
  import { runPreviewSrc } from 'src/cli/serve/web/api.ts'
10
11
  import { asSizeForm } from 'src/cli/serve/web/state/payload.ts'
@@ -13,6 +14,7 @@ import { copyImageToClipboard } from 'src/cli/serve/web/clipboard.ts'
13
14
  import type { WebSt } from 'src/cli/serve/web/state/WebSt.ts'
14
15
  import { MOD_KEY } from 'src/cli/serve/web/components/modKey.ts'
15
16
  import { SHORTCUT_KEYS } from 'src/cli/serve/web/state/shortcuts.ts'
17
+ import { onEscape } from 'src/cli/serve/web/state/escape.ts'
16
18
 
17
19
  /** on an output only the serve process holds: a restart, or the memory budget, loses it */
18
20
  function EphemeralPill(p: { out: { url: string | null; absPath: string | null } }): ReactNode {
@@ -38,6 +40,38 @@ const BlurHint = observer(function BlurHint(p: { st: WebSt }) {
38
40
  )
39
41
  })
40
42
 
43
+ /** one output's drag + its `→ var` buttons: every media var of the SAME kind in the open form */
44
+ function outputDrag(o: DraggedOutput): { draggable: true; onDragStart: (e: DragEvent) => void } {
45
+ return {
46
+ draggable: true,
47
+ onDragStart: (e) => {
48
+ e.dataTransfer.effectAllowed = 'copy'
49
+ e.dataTransfer.setData(OUTPUT_DRAG_MIME, encodeDraggedOutput(o))
50
+ },
51
+ }
52
+ }
53
+
54
+ const UseAs = observer(function UseAs(p: { st: WebSt; out: DraggedOutput }) {
55
+ const targets = (p.st.form?.vars ?? []).filter((v) => v.mediaKind === p.out.kind)
56
+ if (targets.length === 0 || (p.out.url == null && p.out.absPath == null)) return null
57
+ return (
58
+ <div className="use-as">
59
+ {targets.map((v) => (
60
+ <button
61
+ key={v.name}
62
+ type="button"
63
+ className="host-action"
64
+ disabled={v.mediaBusy}
65
+ data-tip={`use this ${p.out.kind} as the '${v.name}' input of the open workflow`}
66
+ onClick={() => void v.takeOutput(p.out)}
67
+ >
68
+ → {v.name}
69
+ </button>
70
+ ))}
71
+ </div>
72
+ )
73
+ })
74
+
41
75
  const canCopy = typeof navigator !== 'undefined' && navigator.clipboard != null
42
76
 
43
77
  type GalleryLocal = {
@@ -116,7 +150,7 @@ const Lightbox = observer(function Lightbox(p: { st: WebSt; local: GalleryLocal
116
150
  useEffect(() => {
117
151
  if (target == null) return
118
152
  const onKey = (e: KeyboardEvent): void => {
119
- if (e.key === 'Escape') return p.local.closeLightbox()
153
+ if (onEscape(e, () => p.local.closeLightbox())) return
120
154
  const dir =
121
155
  e.key === 'ArrowDown' || e.key === 'ArrowRight' ? 1 : e.key === 'ArrowUp' || e.key === 'ArrowLeft' ? -1 : 0
122
156
  if (dir === 0) return
@@ -489,7 +523,16 @@ export const Gallery = observer(function Gallery(p: { st: WebSt; compact?: boole
489
523
  <div className="imgs">
490
524
  {r.images.map((img, ix) =>
491
525
  img.url != null ? (
492
- <div key={img.filename} className="img-cell">
526
+ <div
527
+ key={img.filename}
528
+ className="img-cell"
529
+ {...outputDrag({
530
+ kind: 'image',
531
+ url: img.url,
532
+ absPath: img.absPath,
533
+ filename: img.filename,
534
+ })}
535
+ >
493
536
  <button
494
537
  type="button"
495
538
  className="img-button"
@@ -508,6 +551,10 @@ export const Gallery = observer(function Gallery(p: { st: WebSt; compact?: boole
508
551
  <BlurHint st={p.st} />
509
552
  <EphemeralPill out={img} />
510
553
  </button>
554
+ <UseAs
555
+ st={p.st}
556
+ out={{ kind: 'image', url: img.url, absPath: img.absPath, filename: img.filename }}
557
+ />
511
558
  {/* the EMBEDDING page's buttons (host protocol): what it does with the
512
559
  image is its business — send it somewhere, keep it as something */}
513
560
  {p.st.hostActions.length > 0 ? (
@@ -550,12 +597,20 @@ export const Gallery = observer(function Gallery(p: { st: WebSt; compact?: boole
550
597
  )}
551
598
  {(r.audios ?? []).map((a, ix) =>
552
599
  a.url != null ? (
553
- <div key={`${r.promptId}-audio-${ix}`} className="audio-cell">
600
+ <div
601
+ key={`${r.promptId}-audio-${ix}`}
602
+ className="audio-cell"
603
+ {...outputDrag({ kind: 'audio', url: a.url, absPath: a.absPath, filename: a.filename })}
604
+ >
554
605
  <audio controls preload="metadata" src={a.url} />
555
606
  <a className="hint" href={a.url} download={a.filename}>
556
607
  {a.filename}
557
608
  </a>
558
609
  <EphemeralPill out={a} />
610
+ <UseAs
611
+ st={p.st}
612
+ out={{ kind: 'audio', url: a.url, absPath: a.absPath, filename: a.filename }}
613
+ />
559
614
  </div>
560
615
  ) : (
561
616
  <div key={`${r.promptId}-audio-${ix}`} className="noimg">
@@ -563,7 +618,33 @@ export const Gallery = observer(function Gallery(p: { st: WebSt; compact?: boole
563
618
  </div>
564
619
  ),
565
620
  )}
566
- {r.images.length === 0 && (r.texts ?? []).length === 0 && (r.audios ?? []).length === 0 ? (
621
+ {(r.videos ?? []).map((a, ix) =>
622
+ a.url != null ? (
623
+ <div
624
+ key={`${r.promptId}-video-${ix}`}
625
+ className="video-cell"
626
+ {...outputDrag({ kind: 'video', url: a.url, absPath: a.absPath, filename: a.filename })}
627
+ >
628
+ <video controls muted loop preload="metadata" src={a.url} />
629
+ <a className="hint" href={a.url} download={a.filename}>
630
+ {a.filename}
631
+ </a>
632
+ <EphemeralPill out={a} />
633
+ <UseAs
634
+ st={p.st}
635
+ out={{ kind: 'video', url: a.url, absPath: a.absPath, filename: a.filename }}
636
+ />
637
+ </div>
638
+ ) : (
639
+ <div key={`${r.promptId}-video-${ix}`} className="noimg">
640
+ {a.filename} (not saved, and no longer in memory)
641
+ </div>
642
+ ),
643
+ )}
644
+ {r.images.length === 0 &&
645
+ (r.texts ?? []).length === 0 &&
646
+ (r.audios ?? []).length === 0 &&
647
+ (r.videos ?? []).length === 0 ? (
567
648
  <div className="noimg">no outputs</div>
568
649
  ) : null}
569
650
  </div>
@@ -4,6 +4,7 @@
4
4
  import { useEffect, useRef, useState, type ReactNode } from 'react'
5
5
  import { Icon } from 'src/cli/serve/web/components/Icon.tsx'
6
6
  import { searchHistory, timeAgo, type HistoryEntry } from 'src/cli/serve/web/state/history.ts'
7
+ import { onEscape } from 'src/cli/serve/web/state/escape.ts'
7
8
 
8
9
  /** the button that opens it: always there, disabled while nothing was submitted yet. Plain (not
9
10
  * an observer) to stay generic: the observer parent reads the list and passes it down */
@@ -67,9 +68,10 @@ function HistoryPicker<V>(p: {
67
68
  // listener runs first and stops the event there
68
69
  useEffect(() => {
69
70
  const onKey = (e: KeyboardEvent): void => {
70
- if (e.key !== 'Escape') return
71
- e.stopImmediatePropagation()
72
- p.onClose()
71
+ onEscape(e, () => {
72
+ e.stopImmediatePropagation()
73
+ p.onClose()
74
+ })
73
75
  }
74
76
  window.addEventListener('keydown', onKey, true)
75
77
  return () => window.removeEventListener('keydown', onKey, true)
@@ -3,6 +3,7 @@
3
3
  // click, no document listener to leak
4
4
  import { useState, type ReactNode } from 'react'
5
5
  import { Icon } from 'src/cli/serve/web/components/Icon.tsx'
6
+ import { onEscape } from 'src/cli/serve/web/state/escape.ts'
6
7
 
7
8
  export function MenuButton(p: {
8
9
  tip: string
@@ -13,10 +14,7 @@ export function MenuButton(p: {
13
14
  const [open, setOpen] = useState(false)
14
15
  const close = (): void => setOpen(false)
15
16
  return (
16
- <span
17
- className={`preset-box menu-box ${p.className ?? ''}`}
18
- onKeyDown={(e) => (e.key === 'Escape' ? close() : undefined)}
19
- >
17
+ <span className={`preset-box menu-box ${p.className ?? ''}`} onKeyDown={(e) => onEscape(e, close)}>
20
18
  <button
21
19
  type="button"
22
20
  className="menu-btn"
@@ -7,6 +7,7 @@ import { Icon, type IconName } from 'src/cli/serve/web/components/Icon.tsx'
7
7
  import { entryKey, type OmniboxEntry } from 'src/cli/serve/web/state/omnibox.ts'
8
8
  import { isOmniboxKey } from 'src/cli/serve/web/state/shortcuts.ts'
9
9
  import type { WebSt } from 'src/cli/serve/web/state/WebSt.ts'
10
+ import { onEscape } from 'src/cli/serve/web/state/escape.ts'
10
11
 
11
12
  /** the first media tag picks the icon: what the workflow makes */
12
13
  const ICON_BY_TAG: Record<string, IconName> = { image: 'image', audio: 'audio', video: 'video', text: 'text' }
@@ -94,10 +95,10 @@ export const Omnibox = observer(function Omnibox(p: { st: WebSt }) {
94
95
  value={o.query}
95
96
  onChange={(e) => o.setQuery(e.target.value)}
96
97
  onKeyDown={(e) => {
98
+ if (onEscape(e, () => o.close())) return
97
99
  if (e.key === 'ArrowDown') o.move(1)
98
100
  else if (e.key === 'ArrowUp') o.move(-1)
99
101
  else if (e.key === 'Enter') o.pick()
100
- else if (e.key === 'Escape') o.close()
101
102
  else return
102
103
  e.preventDefault()
103
104
  }}
@@ -16,6 +16,7 @@ import {
16
16
  SHORTCUT_KEYS,
17
17
  type EnhancerShortcut,
18
18
  } from 'src/cli/serve/web/state/shortcuts.ts'
19
+ import { onEscape } from 'src/cli/serve/web/state/escape.ts'
19
20
 
20
21
  const EFFORTS: ReasoningEffort[] = ['off', 'low', 'medium', 'high']
21
22
 
@@ -473,11 +474,11 @@ const Modal = observer(function Modal(p: { e: EnhancerSt; st: WebSt }) {
473
474
  useEffect(() => {
474
475
  const onKey = (ev: KeyboardEvent): void => {
475
476
  // esc closes the editor first, the modal only when nothing is open over the job
476
- if (ev.key === 'Escape') {
477
+ const closed = onEscape(ev, () => {
477
478
  if (e.editing != null) e.setEditing(null)
478
479
  else e.close()
479
- return
480
- }
480
+ })
481
+ if (closed) return
481
482
  const s = enhancerShortcutOf(ev)
482
483
  if (s == null) return
483
484
  ev.preventDefault()
@@ -4,13 +4,14 @@ import { useEffect } from 'react'
4
4
  import { Icon } from 'src/cli/serve/web/components/Icon.tsx'
5
5
  import { shortcutCatalog } from 'src/cli/serve/web/state/shortcutCatalog.ts'
6
6
  import type { WebSt } from 'src/cli/serve/web/state/WebSt.ts'
7
+ import { onEscape } from 'src/cli/serve/web/state/escape.ts'
7
8
 
8
9
  export const ShortcutsModal = observer(function ShortcutsModal(p: { st: WebSt }) {
9
10
  const open = p.st.showShortcuts
10
11
  useEffect(() => {
11
12
  if (!open) return
12
13
  const onKey = (e: KeyboardEvent): void => {
13
- if (e.key === 'Escape') p.st.setShowShortcuts(false)
14
+ onEscape(e, () => p.st.setShowShortcuts(false))
14
15
  }
15
16
  window.addEventListener('keydown', onKey)
16
17
  return () => window.removeEventListener('keydown', onKey)
@@ -8,6 +8,7 @@ import { SHORTCUT_KEYS, shortcutLabel } from 'src/cli/serve/web/state/shortcuts.
8
8
  import type { PathLabel } from 'src/cli/serve/web/api.ts'
9
9
  import type { FormSt } from 'src/cli/serve/web/state/FormSt.ts'
10
10
  import { LAYOUTS, type WebSt } from 'src/cli/serve/web/state/WebSt.ts'
11
+ import { onEscape } from 'src/cli/serve/web/state/escape.ts'
11
12
 
12
13
  /** a titled block of rows; `actions` sit at the right end of the title */
13
14
  function Section(p: { title: string; actions?: ReactNode; children: ReactNode }): ReactNode {
@@ -67,7 +68,7 @@ const DraftSection = observer(function DraftSection(p: { st: WebSt; form: FormSt
67
68
  onChange={(e) => p.st.setRenaming(e.target.value)}
68
69
  onKeyDown={(e) => {
69
70
  if (e.key === 'Enter') confirm()
70
- if (e.key === 'Escape') p.st.stopRename()
71
+ onEscape(e, () => p.st.stopRename())
71
72
  }}
72
73
  // blur CANCELS, it does not commit: picking another draft blurs this input, and a
73
74
  // rename committed on blur raced that selection, renameDraft bails when the form
@@ -19,7 +19,7 @@ import { Icon } from 'src/cli/serve/web/components/Icon.tsx'
19
19
  import { VarIcon } from 'src/cli/serve/web/components/VarIcon.tsx'
20
20
  import { generateButtonLook } from 'src/cli/serve/web/state/generateButton.ts'
21
21
  import { groupPlaces, type GroupPlace } from 'src/cli/serve/web/state/varGroups.ts'
22
- import { ImageControl } from 'src/cli/serve/web/components/controls/ImageControl.tsx'
22
+ import { MediaControl } from 'src/cli/serve/web/components/controls/MediaControl.tsx'
23
23
  import { LorasControl } from 'src/cli/serve/web/components/controls/LorasControl.tsx'
24
24
  import { SeedControl } from 'src/cli/serve/web/components/controls/SeedControl.tsx'
25
25
  import { SizeControl } from 'src/cli/serve/web/components/controls/SizeControl.tsx'
@@ -63,7 +63,9 @@ const VarControl = observer(function VarControl(p: {
63
63
  case 'size':
64
64
  return <SizeControl v={p.v} st={p.st} module={p.module} />
65
65
  case 'image':
66
- return <ImageControl v={p.v} />
66
+ case 'audio':
67
+ case 'video':
68
+ return <MediaControl v={p.v} kind={p.v.desc.kind} />
67
69
  default:
68
70
  // a newer server may describe a kind this bundle predates: stay usable
69
71
  return <div className="hint">unsupported var kind '{p.v.desc.kind}' — use the json api</div>
@@ -78,7 +80,7 @@ function rowDrag(p: { st: WebSt; module: string; names: readonly string[]; index
78
80
  return {
79
81
  row: {
80
82
  onDragOver: (e) => {
81
- // only a row drag: a file dropped on an image var must still reach its own handler
83
+ // only a row drag: a file or an output dropped on a media var reaches its own handler
82
84
  if (e.dataTransfer.types.includes('application/x-comfy-var')) e.preventDefault()
83
85
  },
84
86
  onDrop: (e) => {
@@ -46,6 +46,7 @@ import {
46
46
  type LoraRecord,
47
47
  type LorasInput,
48
48
  } from 'src/vars/lanes.ts'
49
+ import { onEscape } from 'src/cli/serve/web/state/escape.ts'
49
50
 
50
51
  /** one editable list of loras: the whole var, or one of its lanes (ix -1 = no lanes) */
51
52
  type Section = { ix: number; record: LoraRecord; lane: LoraLane | null }
@@ -125,7 +126,7 @@ const LoraLaneBar = observer(function LoraLaneBar(p: {
125
126
  write(patchLane(p.lanes, p.ix, { name: e.currentTarget.value.trim() || lane.name }))
126
127
  setRenaming(false)
127
128
  }
128
- if (e.key === 'Escape') setRenaming(false)
129
+ onEscape(e, () => setRenaming(false))
129
130
  }}
130
131
  onBlur={() => setRenaming(false)}
131
132
  />
@@ -404,7 +405,7 @@ export const LorasControl = observer(function LorasControl(p: {
404
405
  useEffect(() => {
405
406
  if (!open) return
406
407
  const onKey = (e: KeyboardEvent): void => {
407
- if (e.key === 'Escape') local.setOpen(false)
408
+ onEscape(e, () => local.setOpen(false))
408
409
  }
409
410
  window.addEventListener('keydown', onKey)
410
411
  return () => window.removeEventListener('keydown', onKey)
@@ -1095,7 +1096,7 @@ const LoraDetails = observer(function LoraDetails(p: {
1095
1096
  }, [host, name, local])
1096
1097
  useEffect(() => {
1097
1098
  const onKey = (e: KeyboardEvent): void => {
1098
- if (e.key === 'Escape') p.onClose()
1099
+ onEscape(e, () => p.onClose())
1099
1100
  }
1100
1101
  window.addEventListener('keydown', onKey)
1101
1102
  return () => window.removeEventListener('keydown', onKey)
@@ -0,0 +1,111 @@
1
+ // image, audio, video: a path/url input the api already accepts, a browser upload (POST /upload
2
+ // → local file under outputs/serve-inputs/), and a DROP ZONE for desktop files and gallery
3
+ // outputs. The preview plays the var's own medium when the value has a browser url
4
+ import { Icon } from 'src/cli/serve/web/components/Icon.tsx'
5
+ import { observer } from 'mobx-react-lite'
6
+ import { useRef, useState, type DragEvent, type ReactNode } from 'react'
7
+ import { decodeDraggedOutput, OUTPUT_DRAG_MIME } from 'src/cli/serve/web/state/mediaDrop.ts'
8
+ import type { VarSt } from 'src/cli/serve/web/state/FormSt.ts'
9
+ import type { MediaKind } from 'src/runner/mediaKinds.ts'
10
+
11
+ /** a drag we can take: one of our outputs, or files from the desktop. A row reorder is not one */
12
+ function isMediaDrag(e: DragEvent): boolean {
13
+ const types = e.dataTransfer.types
14
+ return types.includes(OUTPUT_DRAG_MIME) || types.includes('Files')
15
+ }
16
+
17
+ function Preview(p: { kind: MediaKind; url: string; alt: string }): ReactNode {
18
+ if (p.kind === 'audio') return <audio controls preload="metadata" src={p.url} />
19
+ if (p.kind === 'video') return <video controls muted preload="metadata" src={p.url} />
20
+ return <img src={p.url} alt={p.alt} />
21
+ }
22
+
23
+ export const MediaControl = observer(function MediaControl(p: { v: VarSt; kind: MediaKind }) {
24
+ const fileInput = useRef<HTMLInputElement>(null)
25
+ const [over, setOver] = useState(false)
26
+ const value = typeof p.v.value === 'string' ? p.v.value : ''
27
+ const previewUrl = /^https?:\/\//.test(value) ? value : p.v.uploadedUrl
28
+ const extensions = p.v.desc.extensions ?? []
29
+ const onDrop = (e: DragEvent): void => {
30
+ if (!isMediaDrag(e)) return
31
+ e.preventDefault()
32
+ e.stopPropagation()
33
+ setOver(false)
34
+ const raw = e.dataTransfer.getData(OUTPUT_DRAG_MIME)
35
+ if (raw !== '') {
36
+ const out = decodeDraggedOutput(raw)
37
+ if (out == null) return p.v.setMediaError('that drop is not a gallery output this panel knows')
38
+ void p.v.takeOutput(out)
39
+ return
40
+ }
41
+ const file = e.dataTransfer.files[0]
42
+ if (file != null) void p.v.takeFile(file)
43
+ }
44
+ return (
45
+ <div
46
+ className={`media-drop${over ? ' over' : ''}`}
47
+ onDragOver={(e) => {
48
+ if (!isMediaDrag(e)) return
49
+ e.preventDefault()
50
+ e.dataTransfer.dropEffect = 'copy'
51
+ setOver(true)
52
+ }}
53
+ onDragLeave={(e) => {
54
+ // leaving for a child is not leaving the zone
55
+ if (e.currentTarget.contains(e.relatedTarget as Node | null)) return
56
+ setOver(false)
57
+ }}
58
+ onDrop={onDrop}
59
+ >
60
+ <div className="row-inline">
61
+ <input
62
+ type="text"
63
+ style={{ flex: 1 }}
64
+ placeholder={`local ${p.kind} path or http(s) url`}
65
+ value={value}
66
+ onChange={(e) => {
67
+ p.v.set(e.target.value)
68
+ p.v.setUploadedUrl(null)
69
+ p.v.setMediaError(null)
70
+ }}
71
+ />
72
+ <button type="button" disabled={p.v.mediaBusy} onClick={() => fileInput.current?.click()}>
73
+ {p.v.mediaBusy ? 'uploading…' : 'upload…'}
74
+ </button>
75
+ {value !== '' ? (
76
+ <button
77
+ type="button"
78
+ data-tip={`clear the ${p.kind}`}
79
+ onClick={() => {
80
+ p.v.set('')
81
+ p.v.setUploadedUrl(null)
82
+ p.v.setMediaError(null)
83
+ }}
84
+ >
85
+ <Icon name="close" />
86
+ </button>
87
+ ) : null}
88
+ <input
89
+ ref={fileInput}
90
+ type="file"
91
+ accept={extensions.map((ext) => `.${ext}`).join(',')}
92
+ style={{ display: 'none' }}
93
+ onChange={(e) => {
94
+ const file = e.target.files?.[0]
95
+ if (file != null) void p.v.takeFile(file)
96
+ e.target.value = ''
97
+ }}
98
+ />
99
+ </div>
100
+ <div className="hint media-drop-hint">
101
+ drop a {p.kind} file here, or drag one from the results (or use its <b>→ {p.v.name}</b> button)
102
+ </div>
103
+ {p.v.mediaError != null ? <div className="error">{p.v.mediaError}</div> : null}
104
+ {previewUrl != null && previewUrl !== '' ? (
105
+ <div className={`img-preview media-preview-${p.kind}`}>
106
+ <Preview kind={p.kind} url={previewUrl} alt={value} />
107
+ </div>
108
+ ) : null}
109
+ </div>
110
+ )
111
+ })
@@ -5,6 +5,7 @@ import { useState } from 'react'
5
5
  import { Icon } from 'src/cli/serve/web/components/Icon.tsx'
6
6
  import type { VarSt } from 'src/cli/serve/web/state/FormSt.ts'
7
7
  import { activePresetLabel } from 'src/vars/presets.ts'
8
+ import { onEscape } from 'src/cli/serve/web/state/escape.ts'
8
9
 
9
10
  /** one line of a preset's text, enough to tell two of them apart in the menu */
10
11
  function firstLine(text: string): string {
@@ -19,7 +20,7 @@ export const PresetPicker = observer(function PresetPicker(p: { v: VarSt }) {
19
20
  const value = typeof p.v.value === 'string' ? p.v.value : ''
20
21
  const active = activePresetLabel(presets, value)
21
22
  return (
22
- <div className="preset-box" onKeyDown={(e) => (e.key === 'Escape' ? setOpen(false) : undefined)}>
23
+ <div className="preset-box" onKeyDown={(e) => onEscape(e, () => setOpen(false))}>
23
24
  <button
24
25
  type="button"
25
26
  className="preset-btn mini"
@@ -19,6 +19,7 @@ import {
19
19
  toPromptLanes,
20
20
  type PromptLane,
21
21
  } from 'src/vars/lanes.ts'
22
+ import { onEscape } from 'src/cli/serve/web/state/escape.ts'
22
23
 
23
24
  /** the words each active lora adds in front of the prompt. One line per lora, cut with an
24
25
  * ellipsis; clicking opens the full list with a checkbox per word and all / none */
@@ -143,7 +144,7 @@ const LaneBox = observer(function LaneBox(p: {
143
144
  write(patchLane(p.lanes, p.ix, { name: e.currentTarget.value.trim() || lane.name }))
144
145
  setRenaming(false)
145
146
  }
146
- if (e.key === 'Escape') setRenaming(false)
147
+ onEscape(e, () => setRenaming(false))
147
148
  }}
148
149
  onBlur={() => setRenaming(false)}
149
150
  />
@@ -7,6 +7,7 @@ import type { VarSt } from 'src/cli/serve/web/state/FormSt.ts'
7
7
  import { asSizeForm } from 'src/cli/serve/web/state/payload.ts'
8
8
  import { aspectBox, starredPresets, toggleStar } from 'src/cli/serve/web/state/sizeStars.ts'
9
9
  import type { WebSt } from 'src/cli/serve/web/state/WebSt.ts'
10
+ import { onEscape } from 'src/cli/serve/web/state/escape.ts'
10
11
 
11
12
  /** a tiny rectangle in the preset's own ratio, centred in a fixed square so a row lines up */
12
13
  function AspectIcon(p: { width: number; height: number; box?: number }): ReactNode {
@@ -66,7 +67,7 @@ export const SizeControl = observer(function SizeControl(p: { v: VarSt; st: WebS
66
67
  ))}
67
68
  </span>
68
69
  ) : null}
69
- <div className="preset-box" onKeyDown={(e) => (e.key === 'Escape' ? setOpen(false) : undefined)}>
70
+ <div className="preset-box" onKeyDown={(e) => onEscape(e, () => setOpen(false))}>
70
71
  <button type="button" className="preset-btn size-pick" aria-expanded={open} onClick={() => setOpen(!open)}>
71
72
  <AspectIcon width={size.width} height={size.height} /> {current?.label ?? 'custom'} ▾
72
73
  </button>