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
@@ -2,15 +2,8 @@ import type { ComfyHost } from 'src/host/ComfyHost.ts'
2
2
  import type { ComfyExecution } from 'src/runner/ComfyExecution.ts'
3
3
  import type { ComfyWorkflow, RunSettings } from 'src/runner/ComfyWorkflow.ts'
4
4
  import type { SdkForHost } from 'src/types/comfy-sdk.ts'
5
- import {
6
- type AnyVar,
7
- type LorasVar,
8
- type LoraStrength,
9
- v,
10
- type VarsSpec,
11
- type VarValues,
12
- varValues,
13
- } from 'src/vars/ComfyVars.ts'
5
+ import type { LorasInput } from 'src/vars/lanes.ts'
6
+ import { type AnyVar, type LorasVar, v, type VarsSpec, type VarValues, varValues } from 'src/vars/ComfyVars.ts'
14
7
 
15
8
  /** the host's generated lora-name union (plain string when no sdk is generated) */
16
9
  export type LoraNameOf<ID extends string> = 'E_LoraName' extends keyof SdkForHost<ID>['Union']
@@ -22,16 +15,8 @@ export type LoraNameOf<ID extends string> = 'E_LoraName' extends keyof SdkForHos
22
15
  * `v.loras(regex)` yields the host's lora-name union (resolved via bindHost)
23
16
  */
24
17
  export type BoundVars<ID extends string> = Omit<typeof v, 'loras'> & {
25
- loras(
26
- options: RegExp,
27
- initial?: Partial<Record<LoraNameOf<ID>, LoraStrength>>,
28
- label?: string,
29
- ): LorasVar<LoraNameOf<ID>>
30
- loras<T extends string>(
31
- options: readonly T[],
32
- initial?: Partial<Record<T, LoraStrength>>,
33
- label?: string,
34
- ): LorasVar<T>
18
+ loras(options: RegExp, initial?: LorasInput<LoraNameOf<ID>>, label?: string): LorasVar<LoraNameOf<ID>>
19
+ loras<T extends string>(options: readonly T[], initial?: LorasInput<T>, label?: string): LorasVar<T>
35
20
  }
36
21
 
37
22
  export type DefineWorkflowSpec<ID extends string, V extends VarsSpec> = {
@@ -48,6 +33,13 @@ export type DefineWorkflowSpec<ID extends string, V extends VarsSpec> = {
48
33
  /** builds the graph from the CURRENT var values; re-executed on every run().
49
34
  * may be async (e.g. image uploads through the wf param) */
50
35
  build: (b: SdkForHost<ID>['Builder'], vars: VarValues<V>, wf: ComfyWorkflow<ID>) => void | Promise<void>
36
+ /** named texts computed from the CURRENT var values, the same `vars` build receives, shown
37
+ * live while the vars are edited (the serve panel: a foldable block under generate). Use the
38
+ * function build uses, so a preview is what the run sends, never a paraphrase of it */
39
+ previews?: Record<string, (vars: VarValues<V>) => string>
40
+ /** free search tags for the serve omnibox, added to the ones read from the graph
41
+ * (`image`, `audio`, `video`, `text`, `llm`, `edit`) */
42
+ tags?: string[]
51
43
  }
52
44
 
53
45
  /** type predicate (not a cast): a VarsSpec is a plain record, never callable */
@@ -80,6 +72,38 @@ export class DefinedWorkflow<ID extends string = string, V extends VarsSpec = Va
80
72
  varDef.name = key
81
73
  varDef.bindHost?.(this.host)
82
74
  }
75
+ // a condition naming a var that does not exist would silently never enable its field
76
+ const names = this.entries().map(([key]) => key)
77
+ for (const [key, varDef] of this.entries())
78
+ for (const other of Object.keys(varDef.uiOpts.activeWhen ?? {}))
79
+ if (!names.includes(other))
80
+ throw new Error(
81
+ `workflow '${spec.id ?? '?'}': var '${key}' is activeWhen '${other}', which is not one of its vars (${names.join(', ')})`,
82
+ )
83
+ }
84
+
85
+ /** every preview for the current var values. A preview that throws, or vars that cannot be
86
+ * read yet (an image var still empty), give a line saying so instead of failing the others */
87
+ computePreviews(): Record<string, string> {
88
+ const previews = this.spec.previews ?? {}
89
+ const names = Object.keys(previews)
90
+ if (names.length === 0) return {}
91
+ let values: VarValues<V>
92
+ try {
93
+ values = varValues(this.vars)
94
+ } catch (e) {
95
+ const why = `unavailable: ${e instanceof Error ? e.message : String(e)}`
96
+ return Object.fromEntries(names.map((n) => [n, why]))
97
+ }
98
+ const out: Record<string, string> = {}
99
+ for (const [name, fn] of Object.entries(previews)) {
100
+ try {
101
+ out[name] = fn(values)
102
+ } catch (e) {
103
+ out[name] = `🔴 preview '${name}' failed: ${e instanceof Error ? e.message : String(e)}`
104
+ }
105
+ }
106
+ return out
83
107
  }
84
108
 
85
109
  /** [name, var] pairs, for drivers that enumerate the knobs */
@@ -100,13 +124,16 @@ export class DefinedWorkflow<ID extends string = string, V extends VarsSpec = Va
100
124
  * build() for copy/export never advances). `p.host` substitutes the host
101
125
  * the graph is built against (TUI host override): node/model availability
102
126
  * there surfaces as workflow.problems / server validation, by design.
127
+ * `p.dry` builds a graph only to read it: nothing is uploaded, lastWorkflow is left alone.
103
128
  */
104
- async build(p: { advance?: boolean; host?: ComfyHost<ID> } = {}): Promise<ComfyWorkflow<ID>> {
129
+ async build(p: { advance?: boolean; host?: ComfyHost<ID>; dry?: boolean } = {}): Promise<ComfyWorkflow<ID>> {
105
130
  const host = p.host ?? this.host
106
131
  const wf = host.workflow({ id: this.spec.id })
132
+ wf.dry = p.dry === true
107
133
  const values = varValues(this.vars)
108
134
  if (p.advance) for (const varDef of Object.values(this.vars)) varDef.afterRun()
109
135
  await this.spec.build(wf.builder, values, wf)
136
+ if (wf.dry) return wf
110
137
  this.lastWorkflow = wf
111
138
  return wf
112
139
  }
@@ -0,0 +1,238 @@
1
+ // lanes: the second value shape of a prompt or loras var. A var holds EITHER its plain value OR
2
+ // `{ lanes: [...] }`; the build merges the active lanes in listed order. PURE and dependency-free
3
+ // (the browser bundle imports it), every shape runtime-checked like loraEntry.ts.
4
+ import { isLoraStrength, loraIsOn, type LoraStrength } from 'src/vars/loraEntry.ts'
5
+
6
+ export type PromptLane = { name: string; prompt: string; active: boolean }
7
+ export type PromptLanes = { lanes: PromptLane[] }
8
+ export type PromptInput = string | PromptLanes
9
+
10
+ export type LoraRecord<T extends string = string> = Partial<Record<T, LoraStrength>>
11
+ export type LoraLane<T extends string = string> = { name: string; active: boolean; loras: LoraRecord<T> }
12
+ export type LoraLanes<T extends string = string> = { lanes: LoraLane<T>[] }
13
+ /** a lora file is never named `lanes` (options end in .safetensors), so the plain record and the
14
+ * lanes object can never be mistaken for each other */
15
+ export type LorasInput<T extends string = string> = LoraRecord<T> | LoraLanes<T>
16
+
17
+ export const FIRST_LANE = 'main'
18
+
19
+ function isObject(v: unknown): v is Record<string, unknown> {
20
+ return typeof v === 'object' && v != null && !Array.isArray(v)
21
+ }
22
+
23
+ // ---- prompt ----
24
+
25
+ export function isPromptLane(v: unknown): v is PromptLane {
26
+ return isObject(v) && typeof v.name === 'string' && typeof v.prompt === 'string' && typeof v.active === 'boolean'
27
+ }
28
+
29
+ export function isPromptLanes(v: unknown): v is PromptLanes {
30
+ return isObject(v) && Array.isArray(v.lanes) && v.lanes.every(isPromptLane)
31
+ }
32
+
33
+ export function isPromptInput(v: unknown): v is PromptInput {
34
+ return typeof v === 'string' || isPromptLanes(v)
35
+ }
36
+
37
+ /** the text the build reads: the active lanes joined in order, one line break between them, so
38
+ * `//` and `- ` lines keep working inside every lane */
39
+ export function promptText(v: PromptInput): string {
40
+ if (typeof v === 'string') return v
41
+ return v.lanes
42
+ .filter((l) => l.active)
43
+ .map((l) => l.prompt)
44
+ .join('\n')
45
+ }
46
+
47
+ export function toPromptLanes(v: PromptInput): PromptLanes {
48
+ return typeof v === 'string' ? { lanes: [{ name: FIRST_LANE, prompt: v, active: true }] } : v
49
+ }
50
+
51
+ /** back to one string: only when it loses nothing (a single active lane) */
52
+ export function promptFromLanes(v: PromptLanes): string | null {
53
+ const only = v.lanes[0]
54
+ return v.lanes.length === 1 && only != null && only.active ? only.prompt : null
55
+ }
56
+
57
+ // the TUI edits a prompt as text: lanes round-trip through header lines, so it never flattens
58
+ // them. `# name` starts a lane, `# name (off)` an inactive one
59
+ const HEADER = /^# (.+?)( \(off\))?$/
60
+
61
+ export function promptLanesToText(v: PromptLanes): string {
62
+ return v.lanes.map((l) => `# ${l.name}${l.active ? '' : ' (off)'}\n${l.prompt}`).join('\n')
63
+ }
64
+
65
+ /** text before the first header lands in a lane named FIRST_LANE, so no typed line is lost */
66
+ export function promptLanesFromText(text: string): PromptLanes {
67
+ const lanes: PromptLane[] = []
68
+ let current: PromptLane | null = null
69
+ const body: string[] = []
70
+ const close = (): void => {
71
+ if (current != null) lanes.push({ ...current, prompt: body.join('\n') })
72
+ else if (body.some((l) => l.trim() !== ''))
73
+ lanes.push({ name: FIRST_LANE, prompt: body.join('\n'), active: true })
74
+ body.length = 0
75
+ }
76
+ for (const line of text.split('\n')) {
77
+ const m = HEADER.exec(line)
78
+ if (m == null) {
79
+ body.push(line)
80
+ continue
81
+ }
82
+ close()
83
+ current = { name: (m[1] ?? '').trim(), prompt: '', active: m[2] == null }
84
+ }
85
+ close()
86
+ return { lanes: lanes.length > 0 ? lanes : [{ name: FIRST_LANE, prompt: '', active: true }] }
87
+ }
88
+
89
+ // ---- loras ----
90
+
91
+ function isLoraRecord(v: unknown): v is LoraRecord {
92
+ return isObject(v) && !('lanes' in v) && Object.values(v).every((s) => s == null || isLoraStrength(s))
93
+ }
94
+
95
+ export function isLoraLane(v: unknown): v is LoraLane {
96
+ return isObject(v) && typeof v.name === 'string' && typeof v.active === 'boolean' && isLoraRecord(v.loras)
97
+ }
98
+
99
+ export function isLoraLanes<T extends string>(v: LorasInput<T>): v is LoraLanes<T>
100
+ export function isLoraLanes(v: unknown): v is LoraLanes
101
+ export function isLoraLanes(v: unknown): v is LoraLanes {
102
+ return isObject(v) && Array.isArray(v.lanes) && v.lanes.every(isLoraLane)
103
+ }
104
+
105
+ export function isLorasInput(v: unknown): v is LorasInput {
106
+ return isLoraRecord(v) || isLoraLanes(v)
107
+ }
108
+
109
+ /** one record for the build: the active lanes in order. A lora in two active lanes keeps its
110
+ * FIRST setting, and `duplicates` names it so the caller can say so */
111
+ export function flattenLoras<T extends string>(v: LorasInput<T>): { record: LoraRecord<T>; duplicates: T[] } {
112
+ if (!isLoraLanes(v)) return { record: v, duplicates: [] }
113
+ const record: LoraRecord<T> = {}
114
+ const duplicates: T[] = []
115
+ for (const lane of v.lanes) {
116
+ if (!lane.active) continue
117
+ for (const [name, st] of Object.entries(lane.loras) as [T, LoraStrength | undefined][]) {
118
+ if (st == null) continue
119
+ if (name in record) duplicates.push(name)
120
+ else record[name] = st
121
+ }
122
+ }
123
+ return { record, duplicates }
124
+ }
125
+
126
+ export function toLoraLanes<T extends string>(v: LorasInput<T>): LoraLanes<T> {
127
+ return isLoraLanes(v) ? v : { lanes: [{ name: FIRST_LANE, active: true, loras: v }] }
128
+ }
129
+
130
+ export function lorasFromLanes<T extends string>(v: LoraLanes<T>): LoraRecord<T> | null {
131
+ const only = v.lanes[0]
132
+ return v.lanes.length === 1 && only != null && only.active ? only.loras : null
133
+ }
134
+
135
+ /** where a lora lives, so an edit lands in the lane that holds it: the first ACTIVE lane that
136
+ * has it (the one the build reads), else any lane, else -1 */
137
+ export function laneIndexOf<T extends string>(v: LoraLanes<T>, name: T): number {
138
+ const active = v.lanes.findIndex((l) => l.active && l.loras[name] != null)
139
+ return active !== -1 ? active : v.lanes.findIndex((l) => l.loras[name] != null)
140
+ }
141
+
142
+ /** rewrite one lora's setting wherever it lives (plain record, or its lane). `null` removes it.
143
+ * A lora in no lane yet lands in the first lane */
144
+ export function updateLora<T extends string>(v: LorasInput<T>, name: T, next: LoraStrength | null): LorasInput<T> {
145
+ const write = (rec: LoraRecord<T>): LoraRecord<T> => {
146
+ const out = { ...rec }
147
+ if (next == null) delete out[name]
148
+ else out[name] = next
149
+ return out
150
+ }
151
+ if (!isLoraLanes(v)) return write(v)
152
+ if (v.lanes.length === 0) return { lanes: [{ name: FIRST_LANE, active: true, loras: write({}) }] }
153
+ const found = laneIndexOf(v, name)
154
+ const ix = found === -1 ? 0 : found
155
+ return { lanes: v.lanes.map((l, i) => (i === ix ? { ...l, loras: write(l.loras) } : l)) }
156
+ }
157
+
158
+ // ---- lane list edits, the same for prompt and lora lanes ----
159
+
160
+ type NamedLane = { name: string; active: boolean }
161
+
162
+ /** a name no other lane has: `lane 2`, `lane 3`… */
163
+ export function newLaneName(lanes: readonly NamedLane[]): string {
164
+ const taken = new Set(lanes.map((l) => l.name))
165
+ let n = lanes.length + 1
166
+ while (taken.has(`lane ${n}`)) n++
167
+ return `lane ${n}`
168
+ }
169
+
170
+ export function patchLane<L extends NamedLane>(lanes: readonly L[], ix: number, patch: Partial<L>): L[] {
171
+ return lanes.map((l, i) => (i === ix ? { ...l, ...patch } : l))
172
+ }
173
+
174
+ /** move a lane up (-1) or down (+1); out of range changes nothing */
175
+ export function moveLane<L>(lanes: readonly L[], ix: number, delta: number): L[] {
176
+ const to = ix + delta
177
+ if (ix < 0 || ix >= lanes.length || to < 0 || to >= lanes.length) return [...lanes]
178
+ const out = [...lanes]
179
+ const [moved] = out.splice(ix, 1)
180
+ if (moved != null) out.splice(to, 0, moved)
181
+ return out
182
+ }
183
+
184
+ export function removeLane<L>(lanes: readonly L[], ix: number): L[] {
185
+ return lanes.filter((_, i) => i !== ix)
186
+ }
187
+
188
+ /** move one lora to another lane (or another slot of its own), before `beforeName` or last */
189
+ export function moveLoraToLane<T extends string>(
190
+ v: LoraLanes<T>,
191
+ p: { from: number; name: T; to: number; beforeName?: T },
192
+ ): LoraLanes<T> {
193
+ const src = v.lanes[p.from]
194
+ const setting = src?.loras[p.name]
195
+ if (src == null || setting == null || v.lanes[p.to] == null) return v
196
+ const lanes = v.lanes.map((l) => ({ ...l, loras: { ...l.loras } }))
197
+ const from = lanes[p.from]
198
+ const to = lanes[p.to]
199
+ if (from == null || to == null) return v
200
+ delete from.loras[p.name]
201
+ const out: LoraRecord<T> = {}
202
+ let placed = false
203
+ for (const [k, st] of Object.entries(to.loras) as [T, LoraStrength | undefined][]) {
204
+ if (k === p.beforeName && !placed) {
205
+ out[p.name] = setting
206
+ placed = true
207
+ }
208
+ out[k] = st
209
+ }
210
+ if (!placed) out[p.name] = setting
211
+ to.loras = out
212
+ return { lanes }
213
+ }
214
+
215
+ /** one lora's stored setting wherever it lives, inactive lanes included (the lane an edit
216
+ * would write to, `updateLora`), or undefined */
217
+ export function loraSetting<T extends string>(v: LorasInput<T>, name: T): LoraStrength | undefined {
218
+ if (!isLoraLanes(v)) return v[name]
219
+ return v.lanes[laneIndexOf(v, name)]?.loras[name]
220
+ }
221
+
222
+ /** every lora in the palette of either shape, each once, with whether the build runs it: a lora
223
+ * in an inactive lane, or paused, is there but not running */
224
+ export function paletteLoras<T extends string>(
225
+ v: LorasInput<T>,
226
+ ): { name: T; setting: LoraStrength; running: boolean }[] {
227
+ const out = new Map<T, { name: T; setting: LoraStrength; running: boolean }>()
228
+ const lanes = isLoraLanes(v) ? v.lanes : [{ name: '', active: true, loras: v }]
229
+ for (const lane of lanes)
230
+ for (const [name, setting] of Object.entries(lane.loras) as [T, LoraStrength | undefined][]) {
231
+ if (setting == null || setting === false) continue
232
+ const running = lane.active && loraIsOn(setting)
233
+ const seen = out.get(name)
234
+ // a running copy wins over a stopped one, as it does in the build
235
+ if (seen == null || (!seen.running && running)) out.set(name, { name, setting, running })
236
+ }
237
+ return [...out.values()]
238
+ }
@@ -0,0 +1,101 @@
1
+ // one lora's stored setting, read the same way by LorasVar, serve and the web panel. PURE and
2
+ // dependency-free, so the browser bundle imports it without pulling ComfyVars in.
3
+ // absent not in the palette
4
+ // false off, not in the palette (what an untick in the TUI writes)
5
+ // true | n on, both strengths 1 | n
6
+ // [m, c] on, each strength
7
+ // { strength: [m, c], off?: true, mute?: string[] }
8
+ // the palette form: `off` pauses it without forgetting it or its strengths,
9
+ // `mute` the parts of its prompt keyword (split at commas) left out of the
10
+ // prompt. Grouping loras into lanes is the VAR's shape, src/vars/lanes.ts
11
+
12
+ export type LoraEntry = {
13
+ strength: [strengthModel: number, strengthClip: number]
14
+ off?: boolean
15
+ mute?: string[]
16
+ }
17
+
18
+ export type LoraStrength = boolean | number | [strengthModel: number, strengthClip: number] | LoraEntry
19
+
20
+ export function isLoraEntry(s: unknown): s is LoraEntry {
21
+ if (typeof s !== 'object' || s == null || Array.isArray(s)) return false
22
+ const o = s as Record<string, unknown>
23
+ const st = o.strength
24
+ return (
25
+ Array.isArray(st) &&
26
+ st.length === 2 &&
27
+ typeof st[0] === 'number' &&
28
+ Number.isFinite(st[0]) &&
29
+ typeof st[1] === 'number' &&
30
+ Number.isFinite(st[1]) &&
31
+ (o.off == null || typeof o.off === 'boolean') &&
32
+ (o.mute == null || (Array.isArray(o.mute) && o.mute.every((w) => typeof w === 'string')))
33
+ )
34
+ }
35
+
36
+ export function isLoraStrength(s: unknown): s is LoraStrength {
37
+ if (typeof s === 'boolean') return true
38
+ if (typeof s === 'number') return Number.isFinite(s)
39
+ if (Array.isArray(s)) return s.length === 2 && s.every((n) => typeof n === 'number' && Number.isFinite(n))
40
+ return isLoraEntry(s)
41
+ }
42
+
43
+ /** on = it runs */
44
+ export function loraIsOn(s: unknown): boolean {
45
+ if (s == null || s === false) return false
46
+ if (isLoraEntry(s)) return s.off !== true
47
+ return true
48
+ }
49
+
50
+ /** in the palette = on, or paused in the palette form */
51
+ export function loraInPalette(s: unknown): boolean {
52
+ return loraIsOn(s) || (isLoraEntry(s) && s.off === true)
53
+ }
54
+
55
+ /** the strengths, whatever the spelling (1/1 when there is nothing to read) */
56
+ export function loraStrengths(s: unknown): [number, number] {
57
+ if (typeof s === 'number' && Number.isFinite(s)) return [s, s]
58
+ if (Array.isArray(s) && typeof s[0] === 'number' && typeof s[1] === 'number') return [s[0], s[1]]
59
+ if (isLoraEntry(s)) return [s.strength[0], s.strength[1]]
60
+ return [1, 1]
61
+ }
62
+
63
+ export function loraMuted(s: unknown): string[] {
64
+ return isLoraEntry(s) && s.mute != null ? s.mute : []
65
+ }
66
+
67
+ /** a prompt keyword's parts, split at commas: each one can be left out on its own */
68
+ export function keywordParts(keyword: string): string[] {
69
+ return [
70
+ ...new Set(
71
+ keyword
72
+ .split(',')
73
+ .map((w) => w.trim())
74
+ .filter((w) => w !== ''),
75
+ ),
76
+ ]
77
+ }
78
+
79
+ /** the keyword a lora adds to the prompt once its muted parts are left out ('' = nothing) */
80
+ export function keptKeyword(keyword: string, muted: readonly string[]): string {
81
+ return keywordParts(keyword)
82
+ .filter((w) => !muted.includes(w))
83
+ .join(', ')
84
+ }
85
+
86
+ /** rewrite one setting, keeping what the patch does not name. The result stays in the short
87
+ * spellings while nothing needs the palette form, so a plain draft reads as it always did */
88
+ export function withLora(
89
+ s: unknown,
90
+ patch: { on?: boolean; strength?: [number, number]; mute?: readonly string[] },
91
+ ): LoraStrength {
92
+ const strength = patch.strength ?? loraStrengths(s)
93
+ const off = patch.on == null ? !loraIsOn(s) : !patch.on
94
+ const mute = [...(patch.mute ?? loraMuted(s))]
95
+ if (!off && mute.length === 0) return [strength[0], strength[1]]
96
+ return {
97
+ strength: [strength[0], strength[1]],
98
+ ...(off ? { off: true } : {}),
99
+ ...(mute.length === 0 ? {} : { mute }),
100
+ }
101
+ }