comfy-ts 2.11.0 → 2.13.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 (122) hide show
  1. package/README.md +1 -0
  2. package/dist/{LogsSt-qtFzOKpu.js → LogsSt-CUeo843w.js} +51 -7
  3. package/dist/LogsSt-CUeo843w.js.map +1 -0
  4. package/dist/cli.js +4 -4
  5. package/dist/index.cjs +595 -73
  6. package/dist/index.cjs.map +1 -1
  7. package/dist/index.d.cts +286 -46
  8. package/dist/index.d.ts +286 -46
  9. package/dist/index.js +591 -74
  10. package/dist/index.js.map +1 -1
  11. package/dist/{run-serve-BK7BA2dn.js → run-serve-DlRBv_Nk.js} +738 -58
  12. package/dist/run-serve-DlRBv_Nk.js.map +1 -0
  13. package/dist/{run-tui-CQyvMf0N.js → run-tui-B5uxm2_l.js} +43 -18
  14. package/dist/run-tui-B5uxm2_l.js.map +1 -0
  15. package/dist/{schemaDrift-DjjEtwfx.js → schemaDrift-E95iTF7E.js} +473 -60
  16. package/dist/schemaDrift-E95iTF7E.js.map +1 -0
  17. package/dist/serve-web.js +710 -174
  18. package/dist/{state--wrz0BQc.js → state-CUp2J1Zy.js} +126 -14
  19. package/dist/state-CUp2J1Zy.js.map +1 -0
  20. package/dist/web.d.ts +286 -46
  21. package/dist/web.js +591 -74
  22. package/dist/web.js.map +1 -1
  23. package/examples/comfy-cloud/sdk.d.ts +163 -163
  24. package/examples/rvion/06-qwen-image-edit.cflow.ts +3 -2
  25. package/examples/rvion/10-anima-t2i.cflow.ts +252 -0
  26. package/examples/rvion/11-yue2-t2a.cflow.ts +105 -0
  27. package/examples/rvion/12-stable-audio-3-t2a.cflow.ts +78 -0
  28. package/examples/rvion/13-ace-step-15-xl-t2a.cflow.ts +153 -0
  29. package/examples/rvion/stableAudio3Prompts.ts +19 -0
  30. package/extra/README.md +4 -0
  31. package/extra/comfyui-fresh-model-lists/README.md +25 -0
  32. package/extra/comfyui-fresh-model-lists/__init__.py +36 -0
  33. package/extra/comfyui-fresh-model-lists/test_fresh_model_lists.py +97 -0
  34. package/package.json +15 -7
  35. package/src/cli/draftMeta.ts +39 -0
  36. package/src/cli/serve/ServeApp.ts +305 -30
  37. package/src/cli/serve/applyVarPayload.ts +50 -22
  38. package/src/cli/serve/byteRange.ts +48 -0
  39. package/src/cli/serve/describeVar.ts +42 -6
  40. package/src/cli/serve/llmConfigShape.ts +51 -0
  41. package/src/cli/serve/llmConfigs.ts +52 -0
  42. package/src/cli/serve/run-serve.ts +12 -3
  43. package/src/cli/serve/tagList.ts +92 -0
  44. package/src/cli/serve/tagSource.ts +69 -0
  45. package/src/cli/serve/web/api.ts +72 -0
  46. package/src/cli/serve/web/components/App.tsx +485 -44
  47. package/src/cli/serve/web/components/DraftTabs.tsx +167 -0
  48. package/src/cli/serve/web/components/Gallery.tsx +133 -31
  49. package/src/cli/serve/web/components/HistoryPicker.tsx +149 -0
  50. package/src/cli/serve/web/components/Icon.tsx +58 -2
  51. package/src/cli/serve/web/components/MenuButton.tsx +53 -0
  52. package/src/cli/serve/web/components/Omnibox.tsx +135 -0
  53. package/src/cli/serve/web/components/PromptEnhancer.tsx +476 -180
  54. package/src/cli/serve/web/components/SideMenu.tsx +414 -0
  55. package/src/cli/serve/web/components/TooltipLayer.tsx +46 -0
  56. package/src/cli/serve/web/components/VarIcon.tsx +24 -0
  57. package/src/cli/serve/web/components/VarsForm.tsx +223 -351
  58. package/src/cli/serve/web/components/controls/BasicControls.tsx +57 -32
  59. package/src/cli/serve/web/components/controls/LorasControl.tsx +646 -317
  60. package/src/cli/serve/web/components/controls/PresetPicker.tsx +1 -1
  61. package/src/cli/serve/web/components/controls/PromptControl.tsx +279 -0
  62. package/src/cli/serve/web/components/controls/PromptEditor.tsx +113 -0
  63. package/src/cli/serve/web/components/controls/SeedControl.tsx +17 -10
  64. package/src/cli/serve/web/components/controls/SizeControl.tsx +111 -18
  65. package/src/cli/serve/web/components/modKey.ts +2 -0
  66. package/src/cli/serve/web/llm.ts +23 -12
  67. package/src/cli/serve/web/promptEditor/promptExtensions.ts +284 -0
  68. package/src/cli/serve/web/state/EnhancerSt.ts +431 -101
  69. package/src/cli/serve/web/state/FormSt.ts +214 -30
  70. package/src/cli/serve/web/state/OmniboxSt.ts +59 -0
  71. package/src/cli/serve/web/state/WebSt.ts +364 -80
  72. package/src/cli/serve/web/state/choiceButtons.ts +61 -0
  73. package/src/cli/serve/web/state/draftNames.ts +16 -0
  74. package/src/cli/serve/web/state/draftTabs.ts +75 -0
  75. package/src/cli/serve/web/state/generateButton.ts +16 -0
  76. package/src/cli/serve/web/state/history.ts +54 -0
  77. package/src/cli/serve/web/state/host.ts +5 -2
  78. package/src/cli/serve/web/state/keptLines.ts +53 -0
  79. package/src/cli/serve/web/state/latentMode.ts +20 -0
  80. package/src/cli/serve/web/state/loraSort.ts +18 -0
  81. package/src/cli/serve/web/state/moduleTree.ts +2 -2
  82. package/src/cli/serve/web/state/omnibox.ts +133 -0
  83. package/src/cli/serve/web/state/payload.ts +38 -58
  84. package/src/cli/serve/web/state/shortcuts.ts +103 -0
  85. package/src/cli/serve/web/state/sizeStars.ts +31 -0
  86. package/src/cli/serve/web/state/stableSlots.ts +21 -0
  87. package/src/cli/serve/web/state/varGroups.ts +24 -0
  88. package/src/cli/serve/web/styles.ts +653 -154
  89. package/src/cli/serve/webBundle.ts +54 -10
  90. package/src/cli/serve/workflowTags.ts +42 -0
  91. package/src/cli/serve/workspaceLabels.ts +23 -0
  92. package/src/cli/tui/components/PickerOverlays.tsx +9 -2
  93. package/src/cli/tui/imagePicker/ImagePickerSt.ts +1 -1
  94. package/src/cli/tui/state/DraftsSt.ts +28 -8
  95. package/src/cli/tui/state/EditorSt.ts +1 -1
  96. package/src/cli/tui/state/LorasSt.ts +1 -1
  97. package/src/cli/tui/state/PickerSt.ts +25 -7
  98. package/src/cli/tui/state/SettingsSt.ts +1 -1
  99. package/src/cli/tui/state/TuiSt.ts +10 -0
  100. package/src/coreExports.ts +21 -0
  101. package/src/host/ComfyHost.ts +17 -2
  102. package/src/host/ResilientWebsocket.ts +54 -4
  103. package/src/host/loraInfoCache.ts +6 -0
  104. package/src/runner/ComfyExecution.ts +60 -0
  105. package/src/runner/ComfyWorkflow.ts +4 -0
  106. package/src/runner/ComfyWsApi.ts +2 -0
  107. package/src/runner/MediaImage.ts +2 -2
  108. package/src/sdk-generator/ComfyUIObjectInfoTypes.ts +5 -5
  109. package/src/sdk-generator/comfyui-sdk-codegen.ts +1 -1
  110. package/src/state.ts +5 -0
  111. package/src/utils/copyName.ts +12 -0
  112. package/src/vars/ComfyVars.ts +337 -67
  113. package/src/vars/DefinedWorkflow.ts +47 -20
  114. package/src/vars/lanes.ts +238 -0
  115. package/src/vars/loraEntry.ts +101 -0
  116. package/src/vars/promptSyntax.ts +382 -0
  117. package/dist/LogsSt-qtFzOKpu.js.map +0 -1
  118. package/dist/run-serve-BK7BA2dn.js.map +0 -1
  119. package/dist/run-tui-CQyvMf0N.js.map +0 -1
  120. package/dist/schemaDrift-DjjEtwfx.js.map +0 -1
  121. package/dist/state--wrz0BQc.js.map +0 -1
  122. package/src/cli/serve/web/components/Sidebar.tsx +0 -66
@@ -6,6 +6,21 @@ import { action, makeObservable, observable } from 'mobx'
6
6
  import { basename, isAbsolute, join, resolve } from 'pathe'
7
7
  import { getComfyStorage } from 'src/storage/ComfyStorage.ts'
8
8
  import { getLoraKeyword } from 'src/vars/loraKeywords.ts'
9
+ import {
10
+ flattenLoras,
11
+ isLorasInput,
12
+ isPromptLanes,
13
+ promptLanesFromText,
14
+ promptLanesToText,
15
+ promptText,
16
+ updateLora,
17
+ type LoraRecord,
18
+ type LorasInput,
19
+ type PromptInput,
20
+ } from 'src/vars/lanes.ts'
21
+ import { keptKeyword, loraIsOn, loraMuted, loraStrengths, withLora, type LoraStrength } from 'src/vars/loraEntry.ts'
22
+ import { logError } from 'src/utils/log.ts'
23
+ import { COMMENT_LINE_RE, NEGATIVE_LINE_RE, parsePromptText } from 'src/vars/promptSyntax.ts'
9
24
  import { toPresetList, type VarPreset, type VarPresetSpec } from 'src/vars/presets.ts'
10
25
 
11
26
  /**
@@ -24,10 +39,38 @@ export type VarKind = 'text' | 'prompt' | 'int' | 'float' | 'seed' | 'toggle' |
24
39
  * the one Out-specialized var (string in, PromptValue out); the split keeps
25
40
  * outValue() cast-free.
26
41
  */
42
+ /** how a var LOOKS in the panel, set by the workflow: `v.int(8).ui({ icon, color, description })`.
43
+ * Every slot is optional, a color is any css color */
44
+ export type VarUi = {
45
+ /** shown as a (?) after the label, the text is its tooltip */
46
+ description?: string
47
+ /** a 24×24 svg path drawn in the icon color, or a whole `<svg …>` string */
48
+ icon?: string
49
+ /** the icon's color */
50
+ color?: string
51
+ labelColor?: string
52
+ /** the whole row, drawn without moving anything in it */
53
+ background?: string
54
+ /** vars that go together: consecutive vars with the same group share one tinted block */
55
+ group?: string
56
+ /** the block's tint, read from the first var of the group that sets it */
57
+ groupColor?: string
58
+ border?: string
59
+ /** choice vars: a look per option, keyed by the option value. The lit option is filled
60
+ * with its own color */
61
+ options?: Record<string, { icon?: string; color?: string }>
62
+ /** the field is shown but disabled unless every named var holds one of the listed values:
63
+ * `{ model: ['aesthetic', 'base'] }`. Checked against the var names when the workflow loads.
64
+ * Display only: a disabled var's value still reaches the build */
65
+ activeWhen?: Record<string, readonly (string | number | boolean)[]>
66
+ }
67
+
27
68
  export abstract class ComfyVarBase<T, Out> {
28
69
  abstract readonly kind: VarKind
29
70
  /** the vars-spec key, stamped by DefinedWorkflow at define time — error messages name the var with it */
30
71
  name?: string
72
+ /** panel looks, see VarUi. Never part of the value, never in a draft */
73
+ uiOpts: VarUi = {}
31
74
  value: T
32
75
  constructor(
33
76
  public readonly defaultValue: T,
@@ -44,6 +87,11 @@ export abstract class ComfyVarBase<T, Out> {
44
87
  this.value = value
45
88
  return this
46
89
  }
90
+ /** chainable: `v.choice([...], 'a').ui({ icon: 'M4 12h16', color: '#e0af68' })` */
91
+ ui(p: VarUi): this {
92
+ this.uiOpts = { ...this.uiOpts, ...p }
93
+ return this
94
+ }
47
95
  /** what drafts persist for this var (subclasses may add fields, e.g. seed mode) */
48
96
  toJSON(): unknown {
49
97
  return this.value
@@ -111,14 +159,36 @@ export class TextVar extends ComfyVar<string> {
111
159
  }
112
160
  }
113
161
 
114
- const commentLineRe = /^\s*\/\//
115
- const negativeLineRe = /^\s*- (.*)$/
116
-
117
162
  /** what PromptVar needs from a loras var: covariant face, so LorasVar<'a'|'b'> fits.
118
163
  * `hostId` scopes the keyword lookup: the same file name on two hosts is often a
119
164
  * different model, and injecting the other host's trigger words is a silently
120
165
  * wrong generation. */
121
- export type ActiveLoraSource = { activeNames(): string[]; hostId?: string }
166
+ export type ActiveLoraSource = {
167
+ activeNames(): string[]
168
+ hostId?: string
169
+ /** the parts of a lora's keyword left out of the prompt (LorasVar: the `mute` list) */
170
+ mutedWords?(name: string): string[]
171
+ }
172
+
173
+ /** where a prompt's tag completion comes from (the serve web editor). The list is never shipped:
174
+ * `url` is a local path (relative to the workflow file, `~/`, `file://`) or an http(s) url */
175
+ export type PromptTagsOpts = {
176
+ url: string
177
+ /** insert `long_hair` as written in the list; default inserts `long hair` */
178
+ underscores?: boolean
179
+ /** put in front of an artist tag on insert (Anima: `@`) */
180
+ artistPrefix?: string
181
+ }
182
+
183
+ export type PromptVarOpts = {
184
+ label?: string
185
+ loraKeywordsFrom?: ActiveLoraSource
186
+ presets?: VarPresetSpec
187
+ /** booru tag completion for this prompt: a tag list url, or the url with its insert rules */
188
+ tags?: string | PromptTagsOpts
189
+ /** false when the model's encoder ignores `(text:1.2)`: the editor then flags every weight */
190
+ weights?: boolean
191
+ }
122
192
 
123
193
  /** what a prompt CONTRIBUTES to build: `vars.prompt.positive` / `.negative` */
124
194
  export type PromptValue = { positive: string; negative: string }
@@ -126,37 +196,52 @@ export type PromptValue = { positive: string; negative: string }
126
196
  /**
127
197
  * prompt text with editor comforts, all resolved at BUILD time (the editor and
128
198
  * drafts keep the raw text):
129
- * - `//` lines are COMMENTS — stripped
199
+ * - `//` starts a COMMENT to the end of the line (src/vars/promptSyntax.ts) — stripped
130
200
  * - `- ` lines are NEGATIVE prompt lines — removed from `.positive`,
131
201
  * comma-joined into `.negative`
132
202
  * - with `loraKeywordsFrom`, the ACTIVE loras' hand-assigned keywords
133
203
  * (src/vars/loraKeywords.ts, ⌃K in the TUI) prefix `.positive` —
134
204
  * `injectedKeywords()` is public so the TUI can PREVIEW the injection.
135
205
  */
136
- export class PromptVar extends ComfyVarBase<string, PromptValue> {
206
+ export class PromptVar extends ComfyVarBase<PromptInput, PromptValue> {
137
207
  readonly kind = 'prompt' as const
138
208
  /** normalized `promptOpts.presets`, in authored order (TextVarOpts.presets owns the WHY) */
139
209
  readonly presets: VarPreset[]
140
210
  constructor(
141
- defaultValue: string,
142
- public promptOpts: {
143
- label?: string
144
- loraKeywordsFrom?: ActiveLoraSource
145
- presets?: VarPresetSpec
146
- } = {},
211
+ defaultValue: PromptInput,
212
+ public promptOpts: PromptVarOpts = {},
147
213
  ) {
148
214
  super(defaultValue, promptOpts.label)
149
215
  this.presets = toPresetList(promptOpts.presets)
150
216
  }
217
+ /** in lanes mode the text carries `# name` headers, so a lane survives a round trip */
151
218
  parse(raw: string): boolean {
152
- this.set(raw)
219
+ this.set(isPromptLanes(this.value) ? promptLanesFromText(raw) : raw)
153
220
  return true
154
221
  }
222
+ override toEditBuffer(): string {
223
+ return isPromptLanes(this.value) ? promptLanesToText(this.value) : this.value
224
+ }
225
+ /** a var list shows the lanes by name, the inactive ones in parentheses */
226
+ override display(): string {
227
+ if (!isPromptLanes(this.value)) return super.display()
228
+ return `${this.value.lanes.length} lanes: ${this.value.lanes.map((l) => (l.active ? l.name : `(${l.name})`)).join(', ')}`
229
+ }
230
+ /** the text the build reads: the plain string, or the active lanes joined in order */
231
+ get text(): string {
232
+ return promptText(this.value)
233
+ }
234
+ /** the tag list options in their one object form, null when this prompt has no completion */
235
+ get tags(): PromptTagsOpts | null {
236
+ const t = this.promptOpts.tags
237
+ if (t == null) return null
238
+ return typeof t === 'string' ? { url: t } : t
239
+ }
155
240
  static isCommentLine(line: string): boolean {
156
- return commentLineRe.test(line)
241
+ return COMMENT_LINE_RE.test(line)
157
242
  }
158
243
  static isNegativeLine(line: string): boolean {
159
- return negativeLineRe.test(line)
244
+ return NEGATIVE_LINE_RE.test(line)
160
245
  }
161
246
  /** the keyword prefix the flag will inject (deduped, lora-list order) — the TUI previews this */
162
247
  injectedKeywords(): string[] {
@@ -166,27 +251,18 @@ export class PromptVar extends ComfyVarBase<string, PromptValue> {
166
251
  ...new Set(
167
252
  src
168
253
  .activeNames()
169
- .map((name) => getLoraKeyword(name, src.hostId))
254
+ .map((name) => keptKeyword(getLoraKeyword(name, src.hostId), src.mutedWords?.(name) ?? []))
170
255
  .filter((k) => k !== ''),
171
256
  ),
172
257
  ]
173
258
  }
174
259
  outValue(): PromptValue {
175
- const pos: string[] = []
176
- const neg: string[] = []
177
- for (const line of this.value.split('\n')) {
178
- if (commentLineRe.test(line)) continue
179
- const m = negativeLineRe.exec(line)
180
- if (m != null) {
181
- const text = (m[1] ?? '').trim()
182
- if (text !== '') neg.push(text)
183
- } else pos.push(line)
184
- }
185
- const body = pos.join('\n').trim()
260
+ const parsed = parsePromptText(this.text)
261
+ const body = parsed.positive
186
262
  const keywords = this.injectedKeywords()
187
263
  const positive =
188
264
  keywords.length === 0 ? body : body === '' ? keywords.join(', ') : `${keywords.join(', ')}, ${body}`
189
- return { positive, negative: neg.join(', ') }
265
+ return { positive, negative: parsed.negative }
190
266
  }
191
267
  }
192
268
 
@@ -327,8 +403,12 @@ export class ToggleVar extends ComfyVar<boolean> {
327
403
  }
328
404
  }
329
405
 
406
+ /** how many options a choice holds: exactly one (the default), one or none, or any number */
407
+ export type ChoiceSelect = 'one' | 'zero-or-one' | 'many'
408
+
330
409
  export class ChoiceVar<T extends string> extends ComfyVar<T> {
331
410
  readonly kind = 'choice' as const
411
+ readonly select = 'one' as const
332
412
  constructor(
333
413
  public readonly choices: readonly T[],
334
414
  defaultValue: T,
@@ -349,20 +429,140 @@ export class ChoiceVar<T extends string> extends ComfyVar<T> {
349
429
  }
350
430
  }
351
431
 
352
- /**
353
- * per-lora setting: false = off, true = 1/1, number = both strengths,
354
- * [strength_model, strength_clip] = each explicitly
355
- */
356
- export type LoraStrength = boolean | number | [strengthModel: number, strengthClip: number]
432
+ /** one option or none: `null` is "nothing picked", clicking the lit option clears it */
433
+ export class OptionalChoiceVar<T extends string> extends ComfyVar<T | null> {
434
+ readonly kind = 'choice' as const
435
+ readonly select = 'zero-or-one' as const
436
+ constructor(
437
+ public readonly choices: readonly T[],
438
+ defaultValue: T | null,
439
+ label?: string,
440
+ ) {
441
+ super(defaultValue, label)
442
+ }
443
+ /** pick `c`, or clear it when it is already the one picked */
444
+ toggle(c: T): this {
445
+ return this.set(this.value === c ? null : c)
446
+ }
447
+ /** the TUI text form: a choice, or an empty line for none */
448
+ parse(raw: string): boolean {
449
+ if (raw.trim() === '') {
450
+ this.set(null)
451
+ return true
452
+ }
453
+ const hit = this.choices.find((c) => c === raw.trim())
454
+ if (hit == null) return false
455
+ this.set(hit)
456
+ return true
457
+ }
458
+ override toEditBuffer(): string {
459
+ return this.value ?? ''
460
+ }
461
+ /** a draft from when this was a many choice holds a list: its first known option */
462
+ override loadJSON(value: unknown): this {
463
+ const one = Array.isArray(value) ? value[0] : value
464
+ return this.set(this.choices.find((c) => c === one) ?? null)
465
+ }
466
+ override display(): string {
467
+ return this.value ?? 'none'
468
+ }
469
+ }
470
+
471
+ /** any number of options, kept in the order the choices list them */
472
+ export class MultiChoiceVar<T extends string> extends ComfyVar<T[]> {
473
+ readonly kind = 'choice' as const
474
+ readonly select = 'many' as const
475
+ constructor(
476
+ public readonly choices: readonly T[],
477
+ defaultValue: readonly T[],
478
+ label?: string,
479
+ ) {
480
+ super([...defaultValue], label)
481
+ }
482
+ toggle(c: T): this {
483
+ const on = this.value.includes(c)
484
+ return this.set(this.choices.filter((x) => (x === c ? !on : this.value.includes(x))))
485
+ }
486
+ /** the TUI text form: `a, b` */
487
+ parse(raw: string): boolean {
488
+ const wanted = raw
489
+ .split(',')
490
+ .map((w) => w.trim())
491
+ .filter((w) => w !== '')
492
+ const picked = this.choices.filter((c) => wanted.includes(c))
493
+ if (picked.length !== new Set(wanted).size) return false
494
+ this.set(picked)
495
+ return true
496
+ }
497
+ override toEditBuffer(): string {
498
+ return this.value.join(', ')
499
+ }
500
+ /** a draft from when this was a single choice holds one value: a list of one. Unknown
501
+ * options are dropped, the list comes back in the choices order */
502
+ override loadJSON(value: unknown): this {
503
+ const listed: unknown[] = Array.isArray(value) ? value : value == null ? [] : [value]
504
+ return this.set(this.choices.filter((c) => listed.includes(c)))
505
+ }
506
+ override display(): string {
507
+ return this.value.length === 0 ? 'none' : this.value.join(', ')
508
+ }
509
+ }
510
+
511
+ /** the three choice shapes, told apart by `select` */
512
+ export type AnyChoiceVar<T extends string = string> = ChoiceVar<T> | OptionalChoiceVar<T> | MultiChoiceVar<T>
513
+
514
+ type ChoiceOpts<S extends ChoiceSelect> = { label?: string; select: S }
515
+
516
+ /** Array.isArray does not narrow a READONLY array out of a union, this does */
517
+ function isChoiceList<T extends string>(v: T | null | readonly T[]): v is readonly T[] {
518
+ return Array.isArray(v)
519
+ }
520
+
521
+ function choice<const T extends string>(
522
+ choices: readonly T[],
523
+ defaultValue: T,
524
+ label?: string | { label?: string; select?: 'one' },
525
+ ): ChoiceVar<T>
526
+ function choice<const T extends string>(
527
+ choices: readonly T[],
528
+ defaultValue: T | null,
529
+ opts: ChoiceOpts<'zero-or-one'>,
530
+ ): OptionalChoiceVar<T>
531
+ function choice<const T extends string>(
532
+ choices: readonly T[],
533
+ defaultValue: readonly T[],
534
+ opts: ChoiceOpts<'many'>,
535
+ ): MultiChoiceVar<T>
536
+ function choice<const T extends string>(
537
+ choices: readonly T[],
538
+ defaultValue: T | null | readonly T[],
539
+ opts?: string | { label?: string; select?: ChoiceSelect },
540
+ ): AnyChoiceVar<T> {
541
+ const o = typeof opts === 'string' ? { label: opts } : (opts ?? {})
542
+ const select = o.select ?? 'one'
543
+ if (select === 'many') {
544
+ if (!isChoiceList(defaultValue))
545
+ throw new Error(`a 'many' choice takes a list as its default, got ${String(defaultValue)}`)
546
+ return new MultiChoiceVar(choices, defaultValue, o.label)
547
+ }
548
+ if (isChoiceList(defaultValue)) throw new Error(`a '${select}' choice takes one value as its default, not a list`)
549
+ if (select === 'zero-or-one') return new OptionalChoiceVar(choices, defaultValue, o.label)
550
+ if (defaultValue == null) throw new Error("a 'one' choice needs a default: use { select: 'zero-or-one' } for none")
551
+ return new ChoiceVar(choices, defaultValue, o.label)
552
+ }
553
+
554
+ /** per-lora setting, every spelling documented in src/vars/loraEntry.ts */
555
+ export type { LoraStrength } from 'src/vars/loraEntry.ts'
357
556
 
358
557
  export type ActiveLora<T extends string> = { lora_name: T; strength_model: number; strength_clip: number }
359
558
 
360
559
  /** normalize a loras record into what a standard LoraLoader chain consumes */
361
- export function activeLoras<T extends string>(loras: Partial<Record<T, LoraStrength>>): ActiveLora<T>[] {
560
+ export function activeLoras<T extends string>(loras: LorasInput<T>): ActiveLora<T>[] {
362
561
  const out: ActiveLora<T>[] = []
363
- for (const [lora_name, s] of Object.entries(loras) as [T, LoraStrength | undefined][]) {
364
- if (s == null || s === false) continue
365
- const [strength_model, strength_clip] = Array.isArray(s) ? s : s === true ? [1, 1] : [s, s]
562
+ for (const [lora_name, s] of Object.entries(flattenLoras(loras).record) as [T, LoraStrength | undefined][]) {
563
+ // a paused lora ({ off: true }) is in the palette, never in the graph
564
+ if (!loraIsOn(s)) continue
565
+ const [strength_model, strength_clip] = loraStrengths(s)
366
566
  out.push({ lora_name, strength_model, strength_clip })
367
567
  }
368
568
  return out
@@ -377,7 +577,12 @@ export type LorasHost = { data?: { id?: string }; schema: { getLoras(filter?: Re
377
577
  * or feed it host discovery (`host.schema.getLoras(regex?)`) directly; never
378
578
  * a hardcoded inventory. Selection is empty by default.
379
579
  */
380
- export class LorasVar<T extends string> extends ComfyVar<Partial<Record<T, LoraStrength>>> {
580
+ /**
581
+ * the value is EITHER a plain record OR `{ lanes: [...] }` (src/vars/lanes.ts): named groups the
582
+ * build merges in listed order, skipping inactive ones. Every method reads the merged record and
583
+ * writes into the lane that holds the lora, so the TUI never needs to know lanes exist
584
+ */
585
+ export class LorasVar<T extends string> extends ComfyVarBase<LorasInput<T>, LoraRecord<T>> {
381
586
  readonly kind = 'loras' as const
382
587
  /** last non-false strength per lora, so untick → re-tick restores it */
383
588
  private prev: Partial<Record<T, LoraStrength>> = {}
@@ -388,7 +593,7 @@ export class LorasVar<T extends string> extends ComfyVar<Partial<Record<T, LoraS
388
593
 
389
594
  constructor(
390
595
  private readonly optionsSource: readonly T[] | RegExp,
391
- initial: Partial<Record<T, LoraStrength>> = {},
596
+ initial: LorasInput<T> = {},
392
597
  label?: string,
393
598
  ) {
394
599
  super(initial, label)
@@ -423,46 +628,78 @@ export class LorasVar<T extends string> extends ComfyVar<Partial<Record<T, LoraS
423
628
  return [...this.options]
424
629
  }
425
630
 
631
+ /** the loras the build reads: the plain record, or the active lanes merged in order */
632
+ get record(): LoraRecord<T> {
633
+ return flattenLoras(this.value).record
634
+ }
635
+
636
+ /** what the graph consumes. A lora in two active lanes runs once, with its FIRST setting */
637
+ outValue(): LoraRecord<T> {
638
+ const flat = flattenLoras(this.value)
639
+ if (flat.duplicates.length > 0)
640
+ logError(
641
+ `loras var '${this.name ?? this.label ?? '?'}': ${flat.duplicates.join(', ')} sit in two active lanes, the first lane's setting is used`,
642
+ )
643
+ return flat.record
644
+ }
645
+
646
+ /** rewrite one lora wherever it lives: the plain record, or the lane that holds it */
647
+ private write(name: T, next: LoraStrength | null): this {
648
+ return this.set(updateLora(this.value, name, next))
649
+ }
650
+
651
+ /** the parts of this lora's prompt keyword left out of the prompt */
652
+ mutedWords(name: string): string[] {
653
+ return loraMuted(this.record[name as T])
654
+ }
655
+
426
656
  /** loras currently ON (PromptVar keyword prefixing consumes this) */
427
657
  activeNames(): T[] {
428
658
  return this.names.filter((n) => this.isOn(n))
429
659
  }
430
660
 
431
661
  isOn(name: T): boolean {
432
- const s = this.value[name]
433
- return s != null && s !== false
662
+ return loraIsOn(this.record[name])
434
663
  }
435
664
 
436
665
  toggleItem(name: T): this {
437
- const cur = this.value[name]
438
- if (cur == null || cur === false) return this.set({ ...this.value, [name]: this.prev[name] ?? true })
666
+ const cur = this.record[name]
667
+ if (cur == null || cur === false) return this.write(name, this.prev[name] ?? true)
668
+ // a paused palette entry resumes in place, strengths kept
669
+ if (!loraIsOn(cur)) return this.write(name, withLora(cur, { on: true }))
439
670
  this.prev[name] = cur
440
- return this.set({ ...this.value, [name]: false })
671
+ return this.write(name, false)
441
672
  }
442
673
 
443
674
  /** step both strengths by delta (true becomes 1 first); no-op when off */
444
675
  adjustItem(name: T, delta: number): this {
445
- const cur = this.value[name]
446
- if (cur == null || cur === false) return this
676
+ const cur = this.record[name]
677
+ if (!loraIsOn(cur)) return this
447
678
  const step = (n: number): number => Math.max(0, Math.round((n + delta) * 100) / 100)
448
- const next: LoraStrength = Array.isArray(cur) ? [step(cur[0]), step(cur[1])] : step(cur === true ? 1 : cur)
449
- return this.set({ ...this.value, [name]: next })
679
+ // the short spellings stay short: only a palette entry needs the object form
680
+ const next: LoraStrength =
681
+ typeof cur === 'number' || cur === true
682
+ ? step(cur === true ? 1 : cur)
683
+ : Array.isArray(cur)
684
+ ? [step(cur[0]), step(cur[1])]
685
+ : withLora(cur, { strength: [step(loraStrengths(cur)[0]), step(loraStrengths(cur)[1])] })
686
+ return this.write(name, next)
450
687
  }
451
688
 
452
689
  /** compact per-item strength label for lists */
453
690
  strengthLabel(name: T): string {
454
- const s = this.value[name]
455
- if (s == null || s === false) return ''
456
- if (s === true) return '1.00'
457
- if (Array.isArray(s)) return `m:${s[0].toFixed(2)} c:${s[1].toFixed(2)}`
458
- return s.toFixed(2)
691
+ const s = this.record[name]
692
+ if (!loraIsOn(s)) return ''
693
+ const [m, c] = loraStrengths(s)
694
+ return m === c ? m.toFixed(2) : `m:${m.toFixed(2)} c:${c.toFixed(2)}`
459
695
  }
460
696
 
461
697
  parse(raw: string): boolean {
462
698
  try {
463
699
  const parsed: unknown = JSON.parse(raw)
464
- if (typeof parsed !== 'object' || parsed == null || Array.isArray(parsed)) return false
465
- this.set(parsed as Partial<Record<T, LoraStrength>>)
700
+ if (!isLorasInput(parsed)) return false
701
+ // cast: whitelist family 1, the names are this host's lora enum, checked by the build
702
+ this.set(parsed as LorasInput<T>)
466
703
  return true
467
704
  } catch {
468
705
  return false
@@ -476,14 +713,17 @@ export class LorasVar<T extends string> extends ComfyVar<Partial<Record<T, LoraS
476
713
 
477
714
  /** tick (true) or untick (false) every lora in `names` (default: all options) */
478
715
  setAll(on: boolean, names: readonly T[] = this.options): this {
479
- const next = { ...this.value }
716
+ const record = this.record
717
+ let next = this.value
480
718
  for (const name of names) {
481
- const cur = next[name]
719
+ const cur = record[name]
482
720
  if (on) {
483
- if (cur == null || cur === false) next[name] = this.prev[name] ?? true
721
+ if (cur == null || cur === false) next = updateLora(next, name, this.prev[name] ?? true)
722
+ // a paused palette entry resumes in place, strengths kept
723
+ else if (!loraIsOn(cur)) next = updateLora(next, name, withLora(cur, { on: true }))
484
724
  } else {
485
- if (cur != null && cur !== false) this.prev[name] = cur
486
- next[name] = false
725
+ if (loraIsOn(cur)) this.prev[name] = cur
726
+ next = updateLora(next, name, false)
487
727
  }
488
728
  }
489
729
  return this.set(next)
@@ -510,6 +750,27 @@ export const DEFAULT_SIZE_PRESETS: SizePreset[] = [
510
750
  { label: '9:16 tall', width: 768, height: 1344 },
511
751
  ]
512
752
 
753
+ /** the same aspect ratios around 512, for SD1.5-era models trained at ~0.25MP */
754
+ export const SD15_SIZE_PRESETS: SizePreset[] = [
755
+ { label: '1:1 square', width: 512, height: 512 },
756
+ { label: '4:3 landscape', width: 576, height: 448 },
757
+ { label: '3:4 portrait', width: 448, height: 576 },
758
+ { label: '3:2 landscape', width: 576, height: 384 },
759
+ { label: '2:3 portrait', width: 384, height: 576 },
760
+ { label: '16:9 widescreen', width: 704, height: 384 },
761
+ { label: '9:16 tall', width: 384, height: 704 },
762
+ ]
763
+
764
+ /** starred presets are one click away in the panel, before the full list */
765
+ export const DEFAULT_STARRED_SIZES = ['1:1 square', '3:4 portrait', '4:3 landscape']
766
+
767
+ /** the family that matches a workflow's own default: a 512×512 workflow is an SD1.5-era one,
768
+ * and offering it 1024 buckets would be offering sizes it was never trained at */
769
+ export function sizePresetsFor(defaultValue: SizeValue | undefined): SizePreset[] {
770
+ if (defaultValue == null) return DEFAULT_SIZE_PRESETS
771
+ return defaultValue.width * defaultValue.height <= 600 * 600 ? SD15_SIZE_PRESETS : DEFAULT_SIZE_PRESETS
772
+ }
773
+
513
774
  /** width/height picker: presets + free `WxH` entry + optionally the LIVE
514
775
  * size of a linked image var (the widget shows
515
776
  * `WxH size of image '<name>'` as a pickable row) */
@@ -520,6 +781,8 @@ export class SizeVar extends ComfyVar<SizeValue> {
520
781
  public readonly presets: SizePreset[] = DEFAULT_SIZE_PRESETS,
521
782
  label?: string,
522
783
  public readonly imageVar?: ImageVar,
784
+ /** preset LABELS starred by default; a user's own stars live in the panel */
785
+ public readonly starred: readonly string[] = DEFAULT_STARRED_SIZES,
523
786
  ) {
524
787
  super(defaultValue, label)
525
788
  }
@@ -646,8 +909,9 @@ export const v = {
646
909
  text: (defaultValue: string, opts: TextVarOpts | string = {}): TextVar =>
647
910
  new TextVar(defaultValue, typeof opts === 'string' ? { label: opts } : opts),
648
911
  prompt: (
649
- defaultValue: string,
650
- opts: { label?: string; loraKeywordsFrom?: ActiveLoraSource; presets?: VarPresetSpec } = {},
912
+ /** a string, or `{ lanes: [{ name, prompt, active }] }` merged in listed order */
913
+ defaultValue: PromptInput,
914
+ opts: PromptVarOpts = {},
651
915
  ): PromptVar => new PromptVar(defaultValue, opts),
652
916
  int: (defaultValue: number, opts: { min?: number; max?: number; label?: string } = {}): IntVar =>
653
917
  new IntVar(defaultValue, opts),
@@ -658,15 +922,20 @@ export const v = {
658
922
  seed: (defaultValue: number = 0, opts: string | { label?: string; mode?: SeedMode } = {}): SeedVar =>
659
923
  new SeedVar(defaultValue, opts),
660
924
  toggle: (defaultValue: boolean, label?: string): ToggleVar => new ToggleVar(defaultValue, label),
661
- choice: <const T extends string>(choices: readonly T[], defaultValue: T, label?: string): ChoiceVar<T> =>
662
- new ChoiceVar(choices, defaultValue, label),
925
+ /** exactly one option (the default), `{ select: 'zero-or-one' }` for one or none (value
926
+ * `T | null`), `{ select: 'many' }` for any number (value `T[]`) */
927
+ choice,
663
928
  loras: <T extends string = string>(
664
929
  options: readonly T[] | RegExp,
665
- initial: Partial<Record<T, LoraStrength>> = {},
930
+ /** a record, or `{ lanes: [{ name, active, loras }] }` merged in listed order */
931
+ initial: LorasInput<T> = {},
666
932
  label?: string,
667
933
  ): LorasVar<T> => new LorasVar(options, initial, label),
668
- size: (defaultValue?: SizeValue, opts: { presets?: SizePreset[]; label?: string; image?: ImageVar } = {}): SizeVar =>
669
- new SizeVar(defaultValue, opts.presets, opts.label, opts.image),
934
+ size: (
935
+ defaultValue?: SizeValue,
936
+ opts: { presets?: SizePreset[]; starred?: readonly string[]; label?: string; image?: ImageVar } = {},
937
+ ): SizeVar =>
938
+ new SizeVar(defaultValue, opts.presets ?? sizePresetsFor(defaultValue), opts.label, opts.image, opts.starred),
670
939
  image: (defaultValue: string, opts: ImageVarOpts = {}): ImageVar => new ImageVar(defaultValue, opts),
671
940
  }
672
941
 
@@ -682,6 +951,7 @@ export type AnyVar = {
682
951
  readonly label?: string
683
952
  /** the vars-spec key — DefinedWorkflow writes it at define time */
684
953
  name?: string
954
+ readonly uiOpts: VarUi
685
955
  parse(raw: string): boolean
686
956
  loadJSON(value: unknown): unknown
687
957
  reset(): unknown