comfy-ts 2.7.0 → 2.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +20 -13
- package/dist/{DefinedWorkflow-BJgt5t0x.js → DefinedWorkflow-BatWaZO1.js} +199 -16
- package/dist/DefinedWorkflow-BatWaZO1.js.map +1 -0
- package/dist/{DraftsSt-dizp3VcC.js → LogsSt-DFxzQfDc.js} +152 -4
- package/dist/LogsSt-DFxzQfDc.js.map +1 -0
- package/dist/cli.js +7 -5
- package/dist/cli.js.map +1 -1
- package/dist/index.cjs +284 -35
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +125 -7
- package/dist/index.d.ts +125 -7
- package/dist/index.js +282 -37
- package/dist/index.js.map +1 -1
- package/dist/run-serve-_nrvJYyt.js +1948 -0
- package/dist/run-serve-_nrvJYyt.js.map +1 -0
- package/dist/{run-tui-JxlEbb_f.js → run-tui-BLEdTsOX.js} +106 -157
- package/dist/run-tui-BLEdTsOX.js.map +1 -0
- package/dist/serve-web.js +441 -72
- package/dist/{state-CaHeDL-0.js → state-BbgJeCe6.js} +180 -45
- package/dist/state-BbgJeCe6.js.map +1 -0
- package/dist/web.d.ts +185 -7
- package/dist/web.js +235 -36
- package/dist/web.js.map +1 -1
- package/examples/README.md +1 -0
- package/examples/comfy-cloud/sdk.d.ts +4066 -321
- package/examples/rvion/04-krea2-turbo-t2i.cflow.ts +18 -2
- package/examples/rvion/07-local-llm-text-gen.cflow.ts +220 -0
- package/examples/web/tsconfig.json +3 -0
- package/extra/README.md +32 -0
- package/extra/comfyui-textgen-stream/README.md +35 -0
- package/extra/comfyui-textgen-stream/__init__.py +166 -0
- package/extra/scripts/fetch_text_encoder.py +137 -0
- package/package.json +6 -3
- package/src/cli/comfy-ts-cli.ts +3 -1
- package/src/cli/serve/ServeApp.ts +754 -40
- package/src/cli/serve/applyVarPayload.ts +10 -3
- package/src/cli/serve/describeVar.ts +47 -7
- package/src/cli/serve/hostProtocol.ts +68 -0
- package/src/cli/serve/managerOnlyLoras.ts +27 -0
- package/src/cli/serve/run-serve.ts +35 -38
- package/src/cli/serve/serveSettings.ts +86 -0
- package/src/cli/serve/startupPrint.ts +111 -0
- package/src/cli/serve/web/api.ts +97 -1
- package/src/cli/serve/web/components/App.tsx +61 -22
- package/src/cli/serve/web/components/Gallery.tsx +306 -41
- package/src/cli/serve/web/components/Icon.tsx +160 -0
- package/src/cli/serve/web/components/PromptEnhancer.tsx +12 -9
- package/src/cli/serve/web/components/Sidebar.tsx +43 -19
- package/src/cli/serve/web/components/VarsForm.tsx +440 -91
- package/src/cli/serve/web/components/controls/BasicControls.tsx +38 -5
- package/src/cli/serve/web/components/controls/ImageControl.tsx +3 -2
- package/src/cli/serve/web/components/controls/LorasControl.tsx +498 -81
- package/src/cli/serve/web/components/controls/PresetPicker.tsx +60 -0
- package/src/cli/serve/web/components/controls/SeedControl.tsx +38 -20
- package/src/cli/serve/web/components/controls/SizeControl.tsx +3 -2
- package/src/cli/serve/web/llm.ts +1 -1
- package/src/cli/serve/web/logWeb.ts +6 -0
- package/src/cli/serve/web/state/EnhancerSt.ts +8 -15
- package/src/cli/serve/web/state/FormSt.ts +74 -18
- package/src/cli/serve/web/state/RunSt.ts +22 -1
- package/src/cli/serve/web/state/WebSt.ts +814 -22
- package/src/cli/serve/web/state/host.ts +206 -0
- package/src/cli/serve/web/state/lightbox.ts +44 -0
- package/src/cli/serve/web/state/moduleTree.ts +52 -0
- package/src/cli/serve/web/state/payload.ts +44 -2
- package/src/cli/serve/web/state/urlSelection.ts +53 -0
- package/src/cli/serve/web/styles.ts +422 -57
- package/src/cli/serve/web/tsconfig.json +4 -0
- package/src/cli/tui/components/PickerOverlays.tsx +37 -0
- package/src/cli/tui/components/StatusBar.tsx +14 -1
- package/src/cli/tui/components/TuiApp.tsx +5 -2
- package/src/cli/tui/state/HostSt.ts +3 -2
- package/src/cli/tui/state/PickerSt.ts +48 -2
- package/src/cli/tui/state/TreeSt.ts +1 -1
- package/src/cli/tui/state/TuiSt.ts +2 -1
- package/src/coreExports.ts +8 -1
- package/src/graph/ComfyNode.ts +58 -1
- package/src/host/ComfyHost.ts +13 -2
- package/src/host/ComfyManager.ts +31 -13
- package/src/host/loraInfoCache.ts +35 -1
- package/src/host/loraManagerApi.ts +94 -0
- package/src/index.ts +2 -0
- package/src/promptEnhancers.ts +102 -0
- package/src/runner/ComfyExecution.ts +61 -6
- package/src/runner/ComfyWorkflow.ts +18 -4
- package/src/runner/ComfyWsApi.ts +3 -0
- package/src/runner/describePromptRejection.ts +34 -0
- package/src/sdk-generator/ComfySchema.ts +17 -4
- package/src/sdk-generator/comfyui-sdk-codegen.ts +56 -2
- package/src/utils/ansi.ts +13 -0
- package/src/utils/matchesRegex.ts +13 -0
- package/src/utils/safeName.ts +29 -0
- package/src/utils/stringMap.ts +9 -0
- package/src/vars/ComfyVars.ts +61 -10
- package/src/vars/presets.ts +17 -0
- package/src/web.ts +12 -0
- package/dist/DefinedWorkflow-BJgt5t0x.js.map +0 -1
- package/dist/DraftsSt-dizp3VcC.js.map +0 -1
- package/dist/run-serve-D1Tsuru9.js +0 -1162
- package/dist/run-serve-D1Tsuru9.js.map +0 -1
- package/dist/run-tui-JxlEbb_f.js.map +0 -1
- package/dist/state-CaHeDL-0.js.map +0 -1
- package/src/cli/serve/promptEnhancers.ts +0 -69
- package/src/cli/serve/safeName.ts +0 -12
|
@@ -1,25 +1,115 @@
|
|
|
1
1
|
// ROOT state tree of the serve web ui (app-state-tree doctrine: one root,
|
|
2
2
|
// child stores hang off it, components read and call)
|
|
3
3
|
import { makeAutoObservable, observableRef, observableShallow, runInAction } from 'mobx'
|
|
4
|
-
import {
|
|
4
|
+
import {
|
|
5
|
+
deleteDraft,
|
|
6
|
+
fetchDraftValues,
|
|
7
|
+
fetchHostLogs,
|
|
8
|
+
fetchHosts,
|
|
9
|
+
fetchIndex,
|
|
10
|
+
fetchSettings,
|
|
11
|
+
pingHost,
|
|
12
|
+
postHostAction,
|
|
13
|
+
saveDraft,
|
|
14
|
+
saveSettings,
|
|
15
|
+
setModuleHost,
|
|
16
|
+
type HostAction,
|
|
17
|
+
type HostsPayload,
|
|
18
|
+
type ModuleDescription,
|
|
19
|
+
type ServeSettings,
|
|
20
|
+
} from 'src/cli/serve/web/api.ts'
|
|
5
21
|
import { EnhancerSt } from 'src/cli/serve/web/state/EnhancerSt.ts'
|
|
6
|
-
import { FormSt } from 'src/cli/serve/web/state/FormSt.ts'
|
|
22
|
+
import { FormSt, type VarSt } from 'src/cli/serve/web/state/FormSt.ts'
|
|
23
|
+
import { logWebError } from 'src/cli/serve/web/logWeb.ts'
|
|
24
|
+
import { asSeedForm } from 'src/cli/serve/web/state/payload.ts'
|
|
25
|
+
import { readUrlSelection, resolveSelection, writeUrlSelection } from 'src/cli/serve/web/state/urlSelection.ts'
|
|
7
26
|
import { RunSt } from 'src/cli/serve/web/state/RunSt.ts'
|
|
27
|
+
import {
|
|
28
|
+
coerceHostValue,
|
|
29
|
+
isEmbedded,
|
|
30
|
+
parseFromHost,
|
|
31
|
+
pickPromptVar,
|
|
32
|
+
postTarget,
|
|
33
|
+
readUrlPrompt,
|
|
34
|
+
resolveHostSelection,
|
|
35
|
+
safeStringify,
|
|
36
|
+
} from 'src/cli/serve/web/state/host.ts'
|
|
37
|
+
import type { HostToPanel, PanelHostAction, PanelRequest, PanelToHost } from 'src/cli/serve/hostProtocol.ts'
|
|
8
38
|
|
|
9
|
-
/** selection + drawer survive a reload
|
|
39
|
+
/** selection + drawer survive a reload: hand-tuned state persists and restores */
|
|
10
40
|
const STORAGE_KEY = 'comfy-ts-serve-ui'
|
|
11
41
|
|
|
42
|
+
/** where the results live. every value is one of the buttons: a width rule would put the panel
|
|
43
|
+
* in a placement no button can show as selected, and a mode you cannot point at is not a mode.
|
|
44
|
+
* 'pinned' floats the newest image over the bottom, so a phone shows the knobs and what they
|
|
45
|
+
* produced without scrolling between them */
|
|
46
|
+
export type ResultsLayout = 'off' | 'bottom' | 'left' | 'side' | 'pinned'
|
|
47
|
+
|
|
48
|
+
/** results on the RIGHT: the wide-screen shape, and the one a first visit opens in */
|
|
49
|
+
export const DEFAULT_LAYOUT: ResultsLayout = 'side'
|
|
50
|
+
|
|
51
|
+
/** icon names live in Icon.tsx; this list stays a plain description of the modes */
|
|
52
|
+
export const LAYOUTS: {
|
|
53
|
+
id: ResultsLayout
|
|
54
|
+
icon: 'panel-off' | 'panel-bottom' | 'panel-left' | 'panel-side' | 'panel-corner'
|
|
55
|
+
title: string
|
|
56
|
+
}[] = [
|
|
57
|
+
{ id: 'off', icon: 'panel-off', title: 'no preview: the form only' },
|
|
58
|
+
{ id: 'bottom', icon: 'panel-bottom', title: 'results below the form' },
|
|
59
|
+
{ id: 'left', icon: 'panel-left', title: 'results left of the form' },
|
|
60
|
+
{ id: 'side', icon: 'panel-side', title: 'results right of the form' },
|
|
61
|
+
{ id: 'pinned', icon: 'panel-corner', title: 'newest image in the bottom right corner, form scrolls under it' },
|
|
62
|
+
]
|
|
63
|
+
|
|
64
|
+
function isLayout(raw: unknown): raw is ResultsLayout {
|
|
65
|
+
return raw === 'off' || raw === 'bottom' || raw === 'left' || raw === 'side' || raw === 'pinned'
|
|
66
|
+
}
|
|
67
|
+
|
|
12
68
|
type StoredSelection = {
|
|
13
69
|
module?: string
|
|
14
70
|
draft?: string
|
|
15
71
|
sidebar?: boolean
|
|
16
72
|
loraImages?: boolean
|
|
17
73
|
loraTitles?: boolean
|
|
74
|
+
/** lora previews fill their card (cover) instead of fitting inside it (contain) */
|
|
75
|
+
loraFill?: boolean
|
|
76
|
+
/** how many lora cards the popup draws before it stops */
|
|
77
|
+
loraCap?: number
|
|
78
|
+
/** the one-shot 60 → 200 default bump already happened for this browser */
|
|
79
|
+
loraCapMigrated?: boolean
|
|
80
|
+
layout?: string
|
|
81
|
+
latent?: boolean
|
|
82
|
+
logs?: boolean
|
|
83
|
+
/** module key → the var names in the order you dragged them into */
|
|
84
|
+
varOrder?: Record<string, string[]>
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** varOrder is the one stored field a reader INDEXES rather than merely reads, so a blob of
|
|
88
|
+
* the wrong shape (hand-edited, or written by an older version) crashed the whole panel to a
|
|
89
|
+
* blank page inside render. Every entry is shape-checked here, once */
|
|
90
|
+
export const DEFAULT_LORA_CAP = 200
|
|
91
|
+
/** the previous default, persisted whether or not anyone chose it: a stored 60 cannot be told
|
|
92
|
+
* apart from a deliberate one, so it is read as unset once (see loraCapMigrated) */
|
|
93
|
+
const LEGACY_LORA_CAP = 60
|
|
94
|
+
/** an upper bound, not a policy: 2000 cards is 2000 image requests, and a number typed by
|
|
95
|
+
* hand (or restored from an older blob) must not be able to hang the page */
|
|
96
|
+
export function clampLoraCap(raw: unknown): number {
|
|
97
|
+
const n = typeof raw === 'number' && Number.isFinite(raw) ? Math.floor(raw) : DEFAULT_LORA_CAP
|
|
98
|
+
return Math.min(2000, Math.max(1, n))
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function readVarOrder(raw: unknown): Record<string, string[]> {
|
|
102
|
+
if (raw == null || typeof raw !== 'object' || Array.isArray(raw)) return {}
|
|
103
|
+
const out: Record<string, string[]> = {}
|
|
104
|
+
for (const [k, v] of Object.entries(raw as Record<string, unknown>))
|
|
105
|
+
if (Array.isArray(v)) out[k] = v.filter((n): n is string => typeof n === 'string')
|
|
106
|
+
return out
|
|
18
107
|
}
|
|
19
108
|
|
|
20
109
|
function readStoredSelection(): StoredSelection {
|
|
21
110
|
try {
|
|
22
|
-
|
|
111
|
+
const raw = (JSON.parse(localStorage.getItem(STORAGE_KEY) ?? '{}') ?? {}) as StoredSelection
|
|
112
|
+
return { ...raw, varOrder: readVarOrder(raw.varOrder) }
|
|
23
113
|
} catch {
|
|
24
114
|
return {}
|
|
25
115
|
}
|
|
@@ -42,24 +132,112 @@ export class WebSt {
|
|
|
42
132
|
/** lora image/title visibility, EVERY lora surface (row cards + popup) — the hide toggles are NSFW screens */
|
|
43
133
|
showLoraImages: boolean
|
|
44
134
|
showLoraTitles: boolean
|
|
135
|
+
/** cover vs contain for every lora preview: art crops well, a character sheet does not */
|
|
136
|
+
loraFill: boolean
|
|
137
|
+
/** how many lora cards the popup draws. A cap exists because each card is an image
|
|
138
|
+
* request; how many is a MACHINE question (your box, your collection), so it is yours */
|
|
139
|
+
loraCap: number
|
|
45
140
|
run: RunSt
|
|
46
141
|
/** prompt refiner: own store, own localStorage blob (the openrouter key never leaves the browser) */
|
|
47
142
|
enhancer: EnhancerSt
|
|
143
|
+
/** SERVER settings, not browser ones: they decide whether a generation writes files at all
|
|
144
|
+
* and where, so they are shared by every client and read back from GET /settings */
|
|
145
|
+
settings: ServeSettings = { saveToDisk: true, hostOverride: {}, savePrefix: {}, effectivePrefix: {} }
|
|
146
|
+
savingError: string | null = null
|
|
147
|
+
/** what you are typing in a prefix field, before the debounced write lands */
|
|
148
|
+
private prefixEdits: Record<string, string> = {}
|
|
149
|
+
private prefixTimers = new Map<string, ReturnType<typeof setTimeout>>()
|
|
150
|
+
|
|
151
|
+
get saveToDisk(): boolean {
|
|
152
|
+
return this.settings.saveToDisk
|
|
153
|
+
}
|
|
154
|
+
/** where the results go — hand-tuned, so it persists and restores */
|
|
155
|
+
layout: ResultsLayout
|
|
156
|
+
/** var row order per module. A UI preference, so it lives in the browser blob: the draft
|
|
157
|
+
* file is the var VALUES contract shared with the TUI, and a layout key there would have to
|
|
158
|
+
* be filtered out by every reader of it */
|
|
159
|
+
varOrder: Record<string, string[]> = {}
|
|
160
|
+
|
|
161
|
+
/** the form's vars in YOUR order, with anything unknown (a new var) kept at the end */
|
|
162
|
+
orderedVars(moduleKey: string, names: readonly string[]): string[] {
|
|
163
|
+
const wanted = this.varOrder[moduleKey]
|
|
164
|
+
if (wanted == null) return [...names]
|
|
165
|
+
const known = new Set(names)
|
|
166
|
+
const ordered = wanted.filter((n) => known.has(n))
|
|
167
|
+
return [...ordered, ...names.filter((n) => !ordered.includes(n))]
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
moveVar(p: { module: string; names: readonly string[]; from: number; to: number }): void {
|
|
171
|
+
const order = this.orderedVars(p.module, p.names)
|
|
172
|
+
const [moved] = order.splice(p.from, 1)
|
|
173
|
+
if (moved == null) return
|
|
174
|
+
order.splice(p.to, 0, moved)
|
|
175
|
+
this.varOrder = { ...this.varOrder, [p.module]: order }
|
|
176
|
+
this.persist()
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/** hosts this process knows + where each module runs (server-owned, like the drafts) */
|
|
180
|
+
hosts: HostsPayload = { hosts: [], defaults: {}, overrides: {} }
|
|
181
|
+
hostError: string | null = null
|
|
182
|
+
/** buttons the EMBEDDING page asked for on every result (host protocol, host.ts). Empty
|
|
183
|
+
* when the panel is a plain tab, so nothing here changes the standalone panel */
|
|
184
|
+
hostActions: PanelHostAction[] = []
|
|
185
|
+
/** the parent window's origin, pinned from its first host message — replies go there only */
|
|
186
|
+
private hostOrigin: string | null = null
|
|
187
|
+
/** module + draft + values of the last `state` posted, so the debounced mirror does not
|
|
188
|
+
* repeat what a reply (or the previous mirror) already told the host */
|
|
189
|
+
private lastPostedState: string | null = null
|
|
190
|
+
/** in-flight selection changes (select, delete, lora refresh): a host request waits for
|
|
191
|
+
* them to settle instead of answering against a form that is null for a moment */
|
|
192
|
+
private switches = new Set<Promise<unknown>>()
|
|
193
|
+
/** host messages run one after another, behind the boot: a message that lands while the
|
|
194
|
+
* index is still loading needs a form, and dropping it was the silent failure */
|
|
195
|
+
private hostChain: Promise<void> = Promise.resolve()
|
|
48
196
|
|
|
49
197
|
constructor() {
|
|
50
198
|
this.run = new RunSt()
|
|
199
|
+
// a finished run reports the seed it used; the form shows it, and the autosave carries it
|
|
200
|
+
// into the draft, which is also what the server continues from, so `+` keeps stepping
|
|
201
|
+
this.run.onSeeds = (p): void => this.applyRunSeeds(p)
|
|
51
202
|
this.enhancer = new EnhancerSt()
|
|
52
203
|
const stored = readStoredSelection()
|
|
53
|
-
|
|
204
|
+
// a stored 'auto' from before the mode was removed resolves to what it MEANT on a
|
|
205
|
+
// wide screen, which is where it spent most of its life
|
|
206
|
+
this.layout = isLayout(stored.layout) ? stored.layout : DEFAULT_LAYOUT
|
|
207
|
+
// CLOSED by default, on every width: the panel is a form, and a workflow tree taking a
|
|
208
|
+
// column of it before you have asked for one is chrome. It reopens from the workflow box
|
|
209
|
+
this.sidebarOpen = stored.sidebar ?? false
|
|
54
210
|
this.showLoraImages = stored.loraImages ?? true
|
|
55
211
|
this.showLoraTitles = stored.loraTitles ?? true
|
|
56
|
-
|
|
212
|
+
this.loraFill = stored.loraFill ?? true
|
|
213
|
+
// marked in the blob, so the bump happens once and a deliberate 60 sticks after it
|
|
214
|
+
this.loraCap = clampLoraCap(
|
|
215
|
+
stored.loraCapMigrated !== true && stored.loraCap === LEGACY_LORA_CAP ? DEFAULT_LORA_CAP : stored.loraCap,
|
|
216
|
+
)
|
|
217
|
+
this.showLatent = stored.latent ?? true
|
|
218
|
+
this.varOrder = stored.varOrder ?? {}
|
|
219
|
+
this.showLogs = stored.logs ?? false
|
|
220
|
+
makeAutoObservable<WebSt, 'hostOrigin' | 'hostChain' | 'lastPostedState' | 'switches'>(this, {
|
|
221
|
+
run: false,
|
|
222
|
+
enhancer: false,
|
|
223
|
+
form: observableRef,
|
|
224
|
+
modules: observableShallow,
|
|
225
|
+
hostOrigin: false,
|
|
226
|
+
hostChain: false,
|
|
227
|
+
lastPostedState: false,
|
|
228
|
+
switches: false,
|
|
229
|
+
})
|
|
230
|
+
// inside a host page → listen for what it asks; a plain tab never sees a message
|
|
231
|
+
if (isEmbedded()) window.addEventListener('message', (e) => this.onHostMessage(e))
|
|
57
232
|
// a closing/hidden tab must not lose an edit still inside the autosave debounce
|
|
58
233
|
window.addEventListener('beforeunload', () => this.form?.flushKeepalive())
|
|
59
234
|
document.addEventListener('visibilitychange', () => {
|
|
60
|
-
|
|
235
|
+
// hiding a tab is not closing it: the ordinary chained save keeps writes in order.
|
|
236
|
+
// keepalive is unchained by necessity and could land before an older one
|
|
237
|
+
if (document.visibilityState === 'hidden') void this.form?.save()
|
|
61
238
|
})
|
|
62
|
-
|
|
239
|
+
// boot catches its own failures; the chain only has to outlive them
|
|
240
|
+
this.hostChain = this.boot().catch((e: unknown) => logWebError('panel boot failed', e))
|
|
63
241
|
}
|
|
64
242
|
|
|
65
243
|
toggleSidebar(): void {
|
|
@@ -77,6 +255,56 @@ export class WebSt {
|
|
|
77
255
|
this.persist()
|
|
78
256
|
}
|
|
79
257
|
|
|
258
|
+
toggleLoraFill(): void {
|
|
259
|
+
this.loraFill = !this.loraFill
|
|
260
|
+
this.persist()
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
setLoraCap(next: number): void {
|
|
264
|
+
this.loraCap = clampLoraCap(next)
|
|
265
|
+
this.persist()
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/** put the seeds a run used back on the form, VALUE only: the mode is the draft's policy and
|
|
269
|
+
* a run never changes it. Writing the USED value (not the next one) is deliberate, the
|
|
270
|
+
* server restarts its continuation from the draft, so writing the next value would skip one */
|
|
271
|
+
private applyRunSeeds(p: { module: string; draft: string; seeds: Record<string, number> }): void {
|
|
272
|
+
const form = this.form
|
|
273
|
+
// the run that finished, not whatever is on screen now: a queued run resolving after you
|
|
274
|
+
// browsed away wrote its seed into ANOTHER draft, and the autosave put it on disk
|
|
275
|
+
if (form == null || form.moduleKey !== p.module || form.draft !== p.draft) return
|
|
276
|
+
runInAction(() => {
|
|
277
|
+
for (const varSt of form.vars) {
|
|
278
|
+
if (varSt.desc.kind !== 'seed') continue
|
|
279
|
+
const used = p.seeds[varSt.name]
|
|
280
|
+
if (typeof used !== 'number' || !Number.isFinite(used)) continue
|
|
281
|
+
const current = asSeedForm(varSt.value)
|
|
282
|
+
if (current.value === used) continue
|
|
283
|
+
varSt.setFromRun({ mode: current.mode, value: used })
|
|
284
|
+
}
|
|
285
|
+
})
|
|
286
|
+
this.mirrorState()
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
/** the address bar follows the selection, so the url on screen is always the one to share.
|
|
290
|
+
* replaceState, not pushState: picking drafts is a browsing gesture here, and one history
|
|
291
|
+
* entry per click would make Back mean "undo the last twelve clicks" */
|
|
292
|
+
private syncUrl(): void {
|
|
293
|
+
const form = this.form
|
|
294
|
+
if (form == null) return
|
|
295
|
+
try {
|
|
296
|
+
const search = writeUrlSelection({
|
|
297
|
+
search: window.location.search,
|
|
298
|
+
module: form.moduleKey,
|
|
299
|
+
draft: form.draft,
|
|
300
|
+
})
|
|
301
|
+
window.history.replaceState(null, '', `${window.location.pathname}${search}${window.location.hash}`)
|
|
302
|
+
} catch (e) {
|
|
303
|
+
// a sandboxed frame can refuse history writes; the panel itself still works
|
|
304
|
+
logWebError('could not put the selection in the url', e)
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
|
|
80
308
|
private persist(): void {
|
|
81
309
|
try {
|
|
82
310
|
// merge, never rebuild: toggling the sidebar before a draft loads must not
|
|
@@ -90,10 +318,17 @@ export class WebSt {
|
|
|
90
318
|
sidebar: this.sidebarOpen,
|
|
91
319
|
loraImages: this.showLoraImages,
|
|
92
320
|
loraTitles: this.showLoraTitles,
|
|
321
|
+
loraFill: this.loraFill,
|
|
322
|
+
loraCap: this.loraCap,
|
|
323
|
+
loraCapMigrated: true,
|
|
324
|
+
layout: this.layout,
|
|
325
|
+
latent: this.showLatent,
|
|
326
|
+
logs: this.showLogs,
|
|
327
|
+
varOrder: this.varOrder,
|
|
93
328
|
}),
|
|
94
329
|
)
|
|
95
|
-
} catch {
|
|
96
|
-
|
|
330
|
+
} catch (e) {
|
|
331
|
+
logWebError('ui state could not be stored (it will not survive a reload)', e)
|
|
97
332
|
}
|
|
98
333
|
}
|
|
99
334
|
|
|
@@ -109,23 +344,67 @@ export class WebSt {
|
|
|
109
344
|
this.loadErrors = index.loadErrors ?? {}
|
|
110
345
|
this.phase = 'ready'
|
|
111
346
|
})
|
|
347
|
+
void this.loadSettings()
|
|
348
|
+
void this.loadHosts()
|
|
112
349
|
const stored = readStoredSelection()
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
const
|
|
116
|
-
|
|
350
|
+
// the URL wins over the stored selection: a link someone sent is an instruction, the
|
|
351
|
+
// stored one is only a memory of this browser's last visit
|
|
352
|
+
const opening = resolveSelection({
|
|
353
|
+
url: readUrlSelection(window.location.search),
|
|
354
|
+
stored: { module: stored.module ?? null, draft: stored.draft ?? null },
|
|
355
|
+
modules: this.modules,
|
|
356
|
+
})
|
|
357
|
+
// `ready` means "booted with a selection": without one the host must hear that the
|
|
358
|
+
// panel is dead, or it cannot tell dead from still loading
|
|
359
|
+
if (opening == null) {
|
|
360
|
+
this.postHost({ comfyTs: 'error', code: 'boot', message: 'no workflows are served' })
|
|
361
|
+
return
|
|
362
|
+
}
|
|
363
|
+
await this.select(opening)
|
|
364
|
+
if (this.form == null) {
|
|
365
|
+
this.postHost({
|
|
366
|
+
comfyTs: 'error',
|
|
367
|
+
code: 'boot',
|
|
368
|
+
message: `the opening draft could not be read: ${this.formError ?? 'unknown error'}`,
|
|
369
|
+
})
|
|
370
|
+
return
|
|
371
|
+
}
|
|
372
|
+
// a host opens the panel with the prompt already typed (`?prompt=`)
|
|
373
|
+
const urlPrompt = readUrlPrompt(window.location.search)
|
|
374
|
+
if (urlPrompt != null) this.setPromptText(urlPrompt)
|
|
375
|
+
// last: the host replies with its buttons + its prompt, and both need a form
|
|
376
|
+
this.postHost({ comfyTs: 'ready' })
|
|
117
377
|
} catch (e) {
|
|
378
|
+
const message = e instanceof Error ? e.message : String(e)
|
|
118
379
|
runInAction(() => {
|
|
119
380
|
this.phase = 'error'
|
|
120
|
-
this.bootError =
|
|
381
|
+
this.bootError = message
|
|
121
382
|
})
|
|
383
|
+
this.postHost({ comfyTs: 'error', code: 'boot', message })
|
|
122
384
|
}
|
|
123
385
|
}
|
|
124
386
|
|
|
125
387
|
/** latest-wins guard for rapid draft clicks: only the newest select may write form state */
|
|
126
388
|
private selectToken = 0
|
|
127
389
|
|
|
128
|
-
|
|
390
|
+
/** `mirror: false` when a host request will answer with its own `state` right after */
|
|
391
|
+
select(p: { module: string; draft: string }, o: { mirror?: boolean } = {}): Promise<void> {
|
|
392
|
+
return this.trackSwitch(this.selectNow(p, o.mirror ?? true))
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
private trackSwitch<T>(op: Promise<T>): Promise<T> {
|
|
396
|
+
this.switches.add(op)
|
|
397
|
+
const done = (): void => void this.switches.delete(op)
|
|
398
|
+
op.then(done, done)
|
|
399
|
+
return op
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
/** resolves once no selection change is in flight (a nested one included) */
|
|
403
|
+
private async switchSettled(): Promise<void> {
|
|
404
|
+
while (this.switches.size > 0) await Promise.allSettled(this.switches)
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
private async selectNow(p: { module: string; draft: string }, mirror: boolean): Promise<void> {
|
|
129
408
|
const mod = this.moduleByKey(p.module)
|
|
130
409
|
if (mod == null) return
|
|
131
410
|
// re-picking the draft you are IN is a no-op, not a reload: a pending autosave would
|
|
@@ -150,18 +429,38 @@ export class WebSt {
|
|
|
150
429
|
const outgoing = this.form
|
|
151
430
|
if (outgoing != null) {
|
|
152
431
|
outgoing.dispose()
|
|
153
|
-
|
|
432
|
+
// OFF SCREEN before the round trip: its autosave reaction is already disposed, so
|
|
433
|
+
// leaving it rendered meant every keystroke during the fetch went nowhere, silently.
|
|
434
|
+
// app shows "loading draft…" while form is null
|
|
435
|
+
runInAction(() => {
|
|
436
|
+
this.form = null
|
|
437
|
+
})
|
|
438
|
+
if (!(await outgoing.save()))
|
|
439
|
+
runInAction(() => {
|
|
440
|
+
this.formError = `the previous draft could not be saved: ${outgoing.saveError ?? 'unknown error'}`
|
|
441
|
+
})
|
|
154
442
|
}
|
|
155
443
|
try {
|
|
156
444
|
const reply = await fetchDraftValues(p)
|
|
445
|
+
// a newer select owns the screen now: telling the host about this one would be a lie
|
|
446
|
+
if (token !== this.selectToken) return
|
|
447
|
+
const form = new FormSt(p.module, p.draft, mod, reply.values ?? {})
|
|
448
|
+
// the host mirrors user edits on the autosave's own debounce (pinned only)
|
|
449
|
+
form.onSettled = (): void => this.mirrorState()
|
|
157
450
|
runInAction(() => {
|
|
158
|
-
|
|
159
|
-
this.form = new FormSt(p.module, p.draft, mod, reply.values ?? {})
|
|
451
|
+
this.form = form
|
|
160
452
|
this.persist()
|
|
453
|
+
this.syncUrl()
|
|
161
454
|
})
|
|
455
|
+
this.postHost({ comfyTs: 'selection', module: form.moduleKey, draft: form.draft })
|
|
456
|
+
if (mirror) this.mirrorState()
|
|
162
457
|
} catch (e) {
|
|
163
458
|
runInAction(() => {
|
|
164
459
|
if (token !== this.selectToken) return
|
|
460
|
+
// the outgoing form was DISPOSED above: keeping it on screen leaves an editable
|
|
461
|
+
// form whose autosave reaction is dead, so edits silently stop persisting while
|
|
462
|
+
// the header still reads 'saved'. No form is honest, the error says what happened
|
|
463
|
+
this.form = null
|
|
165
464
|
this.formError = e instanceof Error ? e.message : String(e)
|
|
166
465
|
})
|
|
167
466
|
} finally {
|
|
@@ -171,12 +470,474 @@ export class WebSt {
|
|
|
171
470
|
}
|
|
172
471
|
}
|
|
173
472
|
|
|
473
|
+
// ── the host protocol (host.ts, types in hostProtocol.ts) ────────────────
|
|
474
|
+
promptVar(): VarSt | null {
|
|
475
|
+
return pickPromptVar(this.form?.vars ?? [])
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
/** `set-prompt` and `?prompt=`: through the same coercion as set-values */
|
|
479
|
+
setPromptText(text: string): void {
|
|
480
|
+
const v = this.promptVar()
|
|
481
|
+
if (v == null) return
|
|
482
|
+
const coerced = coerceHostValue(v.desc, text, v.value)
|
|
483
|
+
if (!coerced.ok) {
|
|
484
|
+
logWebError('host set-prompt', `'${v.name}' (${v.desc.kind}) rejected the text`)
|
|
485
|
+
return
|
|
486
|
+
}
|
|
487
|
+
runInAction(() => v.set(coerced.value))
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
private postHost(msg: PanelToHost): void {
|
|
491
|
+
if (!isEmbedded()) return
|
|
492
|
+
// host.ts owns the rule: before the pin only value-free messages leave, to '*'
|
|
493
|
+
const target = postTarget(msg, this.hostOrigin)
|
|
494
|
+
if (target == null) return
|
|
495
|
+
try {
|
|
496
|
+
window.parent.postMessage(msg, target)
|
|
497
|
+
} catch (e) {
|
|
498
|
+
logWebError('could not post to the host page', e)
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
/** set vars by name, each through coerceHostValue. Returns the names NOT applied (no such
|
|
503
|
+
* var, or a value its control cannot produce); those fields stay as they were */
|
|
504
|
+
setValues(values: Record<string, unknown>): string[] {
|
|
505
|
+
const form = this.form
|
|
506
|
+
if (form == null) return Object.keys(values)
|
|
507
|
+
const rejected: string[] = []
|
|
508
|
+
runInAction(() => {
|
|
509
|
+
for (const [name, raw] of Object.entries(values)) {
|
|
510
|
+
const varSt = form.vars.find((v) => v.name === name)
|
|
511
|
+
if (varSt == null) {
|
|
512
|
+
rejected.push(name)
|
|
513
|
+
logWebError('host set-values', `no var named '${name}' on ${form.moduleKey}`)
|
|
514
|
+
continue
|
|
515
|
+
}
|
|
516
|
+
const coerced = coerceHostValue(varSt.desc, raw, varSt.value)
|
|
517
|
+
if (coerced.ok) varSt.set(coerced.value)
|
|
518
|
+
else {
|
|
519
|
+
rejected.push(name)
|
|
520
|
+
logWebError('host set-values', `'${name}' (${varSt.desc.kind}) rejected ${safeStringify(raw)}`)
|
|
521
|
+
}
|
|
522
|
+
}
|
|
523
|
+
})
|
|
524
|
+
return rejected
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
private stateOf(form: FormSt): { key: string; values: Record<string, unknown> } {
|
|
528
|
+
const values = form.valuesJSON()
|
|
529
|
+
return { key: safeStringify([form.moduleKey, form.draft, values]), values }
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
/** the reply to a request: always posted (the host is pinned, it just spoke) */
|
|
533
|
+
private replyState(form: FormSt, request: PanelRequest, rejected?: string[]): void {
|
|
534
|
+
const { key, values } = this.stateOf(form)
|
|
535
|
+
this.lastPostedState = key
|
|
536
|
+
this.postHost({
|
|
537
|
+
comfyTs: 'state',
|
|
538
|
+
module: form.moduleKey,
|
|
539
|
+
draft: form.draft,
|
|
540
|
+
values,
|
|
541
|
+
request,
|
|
542
|
+
...(rejected != null ? { rejected } : {}),
|
|
543
|
+
})
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
/** volunteered state (a user edit or switch, run seeds): only to a pinned host, and only
|
|
547
|
+
* when it differs from the last state the host was told */
|
|
548
|
+
private mirrorState(): void {
|
|
549
|
+
const form = this.form
|
|
550
|
+
if (form == null || this.hostOrigin == null) return
|
|
551
|
+
const { key, values } = this.stateOf(form)
|
|
552
|
+
if (key === this.lastPostedState) return
|
|
553
|
+
this.lastPostedState = key
|
|
554
|
+
this.postHost({ comfyTs: 'state', module: form.moduleKey, draft: form.draft, values })
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
private replyError(
|
|
558
|
+
request: PanelRequest,
|
|
559
|
+
code: 'unknown-module' | 'select-failed' | 'superseded' | 'no-form',
|
|
560
|
+
message: string,
|
|
561
|
+
): void {
|
|
562
|
+
this.postHost({ comfyTs: 'error', code, message, request })
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
private onHostMessage(e: MessageEvent): void {
|
|
566
|
+
// the host is the parent window, nobody else: a sibling or child frame posting first used
|
|
567
|
+
// to pin ITS origin and lock the real host out
|
|
568
|
+
if (e.source !== window.parent) return
|
|
569
|
+
const msg = parseFromHost(e.data)
|
|
570
|
+
if (msg == null) return
|
|
571
|
+
if (this.hostOrigin == null) this.hostOrigin = e.origin
|
|
572
|
+
else if (e.origin !== this.hostOrigin) return
|
|
573
|
+
this.hostChain = this.hostChain
|
|
574
|
+
.then(() => this.applyHostMessage(msg))
|
|
575
|
+
.catch((err: unknown) => logWebError(`host message '${msg.comfyTs}' failed`, err))
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
/** every request (`set-selection`, `set-values`, `get-state`) answers exactly once, with
|
|
579
|
+
* `state` or `error`; the catch below keeps that true when something throws */
|
|
580
|
+
private async applyHostMessage(msg: HostToPanel): Promise<void> {
|
|
581
|
+
try {
|
|
582
|
+
await this.applyHostMessageNow(msg)
|
|
583
|
+
} catch (e) {
|
|
584
|
+
if (msg.comfyTs === 'set-selection' || msg.comfyTs === 'set-values' || msg.comfyTs === 'get-state')
|
|
585
|
+
this.replyError(msg.comfyTs, 'no-form', e instanceof Error ? e.message : String(e))
|
|
586
|
+
throw e
|
|
587
|
+
}
|
|
588
|
+
}
|
|
589
|
+
|
|
590
|
+
private async applyHostMessageNow(msg: HostToPanel): Promise<void> {
|
|
591
|
+
switch (msg.comfyTs) {
|
|
592
|
+
case 'host-actions':
|
|
593
|
+
runInAction(() => {
|
|
594
|
+
this.hostActions = msg.actions
|
|
595
|
+
})
|
|
596
|
+
return
|
|
597
|
+
case 'set-prompt':
|
|
598
|
+
await this.switchSettled()
|
|
599
|
+
this.setPromptText(msg.text)
|
|
600
|
+
return
|
|
601
|
+
case 'set-selection': {
|
|
602
|
+
await this.switchSettled()
|
|
603
|
+
const target = resolveHostSelection({ want: msg, modules: this.modules })
|
|
604
|
+
if (target == null) {
|
|
605
|
+
this.replyError('set-selection', 'unknown-module', `no workflow named '${msg.module}' is served`)
|
|
606
|
+
return
|
|
607
|
+
}
|
|
608
|
+
const open = this.form
|
|
609
|
+
if (open == null || open.moduleKey !== target.module || open.draft !== target.draft) {
|
|
610
|
+
await this.select(target, { mirror: false })
|
|
611
|
+
await this.switchSettled()
|
|
612
|
+
}
|
|
613
|
+
const form = this.form
|
|
614
|
+
if (form == null) {
|
|
615
|
+
const why = this.formError ?? 'the draft could not be read'
|
|
616
|
+
this.replyError('set-selection', 'select-failed', why)
|
|
617
|
+
} else if (form.moduleKey !== target.module || form.draft !== target.draft)
|
|
618
|
+
this.replyError(
|
|
619
|
+
'set-selection',
|
|
620
|
+
'superseded',
|
|
621
|
+
`the panel switched to ${form.moduleKey}/${form.draft} while ${target.module}/${target.draft} was loading`,
|
|
622
|
+
)
|
|
623
|
+
else this.replyState(form, 'set-selection')
|
|
624
|
+
return
|
|
625
|
+
}
|
|
626
|
+
case 'set-values': {
|
|
627
|
+
await this.switchSettled()
|
|
628
|
+
const form = this.form
|
|
629
|
+
if (form == null) {
|
|
630
|
+
this.replyError('set-values', 'no-form', this.noFormMessage())
|
|
631
|
+
return
|
|
632
|
+
}
|
|
633
|
+
this.replyState(form, 'set-values', this.setValues(msg.values))
|
|
634
|
+
return
|
|
635
|
+
}
|
|
636
|
+
case 'get-state': {
|
|
637
|
+
await this.switchSettled()
|
|
638
|
+
const form = this.form
|
|
639
|
+
if (form == null) this.replyError('get-state', 'no-form', this.noFormMessage())
|
|
640
|
+
else this.replyState(form, 'get-state')
|
|
641
|
+
return
|
|
642
|
+
}
|
|
643
|
+
}
|
|
644
|
+
}
|
|
645
|
+
|
|
646
|
+
private noFormMessage(): string {
|
|
647
|
+
return this.formError != null ? `no draft is open: ${this.formError}` : 'no draft is open'
|
|
648
|
+
}
|
|
649
|
+
|
|
650
|
+
/** a host button on a result: tell the page which image, and the prompt it came from */
|
|
651
|
+
postResultAction(
|
|
652
|
+
actionId: string,
|
|
653
|
+
r: { promptId: string; module: string; draft: string; seeds: Record<string, number> },
|
|
654
|
+
img: { url: string; filename: string },
|
|
655
|
+
ix: number,
|
|
656
|
+
): void {
|
|
657
|
+
const pv = this.promptVar()
|
|
658
|
+
this.postHost({
|
|
659
|
+
comfyTs: 'result-action',
|
|
660
|
+
id: actionId,
|
|
661
|
+
promptId: r.promptId,
|
|
662
|
+
ix,
|
|
663
|
+
url: img.url,
|
|
664
|
+
filename: img.filename,
|
|
665
|
+
module: r.module,
|
|
666
|
+
draft: r.draft,
|
|
667
|
+
seeds: r.seeds,
|
|
668
|
+
prompt: pv != null && typeof pv.value === 'string' ? pv.value : null,
|
|
669
|
+
})
|
|
670
|
+
}
|
|
671
|
+
|
|
672
|
+
/** side and pinned show the results next to (or over) the form, so the run button belongs
|
|
673
|
+
* there: in pinned it stays on screen while the form scrolls */
|
|
674
|
+
get generateInResults(): boolean {
|
|
675
|
+
return this.layout === 'side' || this.layout === 'left' || this.layout === 'pinned'
|
|
676
|
+
}
|
|
677
|
+
|
|
678
|
+
setLayout(next: ResultsLayout): void {
|
|
679
|
+
// a click SETS the mode. cycling back to a width rule lands where no button is showing
|
|
680
|
+
this.layout = next
|
|
681
|
+
this.persist()
|
|
682
|
+
}
|
|
683
|
+
|
|
684
|
+
/** the host a module's runs go to right now (override if any, else the module's own) */
|
|
685
|
+
hostFor(moduleKey: string): string {
|
|
686
|
+
return this.hosts.overrides[moduleKey] ?? this.hosts.defaults[moduleKey] ?? ''
|
|
687
|
+
}
|
|
688
|
+
|
|
689
|
+
/** the host's OWN base url, for opening its pages (the lora manager lives there) */
|
|
690
|
+
hostUrlFor(moduleKey: string): string | null {
|
|
691
|
+
const id = this.hostFor(moduleKey)
|
|
692
|
+
return this.hosts.hosts.find((h) => h.id === id)?.httpUrl ?? null
|
|
693
|
+
}
|
|
694
|
+
|
|
695
|
+
isHostOverridden(moduleKey: string): boolean {
|
|
696
|
+
return this.hosts.overrides[moduleKey] != null
|
|
697
|
+
}
|
|
698
|
+
|
|
699
|
+
private async loadHosts(): Promise<void> {
|
|
700
|
+
try {
|
|
701
|
+
const hosts = await fetchHosts()
|
|
702
|
+
runInAction(() => {
|
|
703
|
+
this.hosts = hosts
|
|
704
|
+
})
|
|
705
|
+
} catch (e) {
|
|
706
|
+
// the process serving this page owns /hosts, so a failure here is a real one (500,
|
|
707
|
+
// transport) rather than an old server: never swallowed, or the host box silently
|
|
708
|
+
// shows the module default and every host action targets the wrong box
|
|
709
|
+
logWebError('could not read the host list', e)
|
|
710
|
+
}
|
|
711
|
+
}
|
|
712
|
+
|
|
713
|
+
/** the ComfyUI console, polled only while the logs panel is open (off by default: most of
|
|
714
|
+
* the time you do not care, and a closed panel must cost nothing) */
|
|
715
|
+
showLogs = false
|
|
716
|
+
logLines: string[] = []
|
|
717
|
+
logsError: string | null = null
|
|
718
|
+
hostNote: string | null = null
|
|
719
|
+
/** the latent frames during a run: on by default, off when you only want the final image */
|
|
720
|
+
showLatent = true
|
|
721
|
+
private logsTimer: ReturnType<typeof setInterval> | null = null
|
|
722
|
+
|
|
723
|
+
toggleLatent(): void {
|
|
724
|
+
this.showLatent = !this.showLatent
|
|
725
|
+
this.persist()
|
|
726
|
+
}
|
|
727
|
+
|
|
728
|
+
toggleLogs(): void {
|
|
729
|
+
this.showLogs = !this.showLogs
|
|
730
|
+
this.persist()
|
|
731
|
+
if (this.logsTimer != null) clearInterval(this.logsTimer)
|
|
732
|
+
this.logsTimer = null
|
|
733
|
+
if (!this.showLogs) return
|
|
734
|
+
void this.pullLogs()
|
|
735
|
+
this.logsTimer = setInterval(() => void this.pullLogs(), 3000)
|
|
736
|
+
}
|
|
737
|
+
|
|
738
|
+
private async pullLogs(): Promise<void> {
|
|
739
|
+
const host = this.form == null ? null : this.hostFor(this.form.moduleKey)
|
|
740
|
+
if (host == null || host === '') return
|
|
741
|
+
try {
|
|
742
|
+
const reply = await fetchHostLogs({ host })
|
|
743
|
+
runInAction(() => {
|
|
744
|
+
// already folded into lines server side, through the TUI's own assembly
|
|
745
|
+
this.logLines = reply.lines
|
|
746
|
+
this.logsError = null
|
|
747
|
+
})
|
|
748
|
+
} catch (e) {
|
|
749
|
+
runInAction(() => {
|
|
750
|
+
this.logsError = e instanceof Error ? e.message : String(e)
|
|
751
|
+
})
|
|
752
|
+
}
|
|
753
|
+
}
|
|
754
|
+
|
|
755
|
+
/** the lora mirror sweep, from the panel: the metadata (names, trigger words, previews) is
|
|
756
|
+
* re-downloaded and every surface re-reads it, which needs the descriptors reloaded too */
|
|
757
|
+
loraSyncing = false
|
|
758
|
+
|
|
759
|
+
async refreshLoras(): Promise<void> {
|
|
760
|
+
if (this.loraSyncing) return
|
|
761
|
+
runInAction(() => {
|
|
762
|
+
this.loraSyncing = true
|
|
763
|
+
})
|
|
764
|
+
await this.hostAction('refresh-loras')
|
|
765
|
+
await this.trackSwitch(this.reloadIndexAndForm())
|
|
766
|
+
}
|
|
767
|
+
|
|
768
|
+
private async reloadIndexAndForm(): Promise<void> {
|
|
769
|
+
// the option lists (and the manager-only union) live in the descriptors: re-read them,
|
|
770
|
+
// then re-select the same draft so the form is rebuilt against the new options
|
|
771
|
+
try {
|
|
772
|
+
const index = await fetchIndex()
|
|
773
|
+
const current = this.form
|
|
774
|
+
runInAction(() => {
|
|
775
|
+
this.modules = index.workflows
|
|
776
|
+
})
|
|
777
|
+
if (current != null) {
|
|
778
|
+
// flush and DISPOSE the outgoing form, in an action: dropping the reference alone
|
|
779
|
+
// leaked its autosave reaction and let it write the file the new form just read
|
|
780
|
+
current.dispose()
|
|
781
|
+
await current.save()
|
|
782
|
+
runInAction(() => {
|
|
783
|
+
this.form = null
|
|
784
|
+
})
|
|
785
|
+
await this.select({ module: current.moduleKey, draft: current.draft })
|
|
786
|
+
}
|
|
787
|
+
} catch (e) {
|
|
788
|
+
runInAction(() => {
|
|
789
|
+
this.hostError = e instanceof Error ? e.message : String(e)
|
|
790
|
+
})
|
|
791
|
+
} finally {
|
|
792
|
+
runInAction(() => {
|
|
793
|
+
this.loraSyncing = false
|
|
794
|
+
})
|
|
795
|
+
}
|
|
796
|
+
}
|
|
797
|
+
|
|
798
|
+
/** a restart takes the host down and back: this WATCHES it instead of leaving you guessing.
|
|
799
|
+
* polls until it answers, or gives up loudly after ~2 minutes */
|
|
800
|
+
hostWatch: 'idle' | 'down' | 'back' = 'idle'
|
|
801
|
+
|
|
802
|
+
private async watchHostComeBack(): Promise<void> {
|
|
803
|
+
const host = this.form == null ? null : this.hostFor(this.form.moduleKey)
|
|
804
|
+
if (host == null || host === '') return
|
|
805
|
+
runInAction(() => {
|
|
806
|
+
this.hostWatch = 'down'
|
|
807
|
+
})
|
|
808
|
+
const deadline = Date.now() + 120_000
|
|
809
|
+
for (;;) {
|
|
810
|
+
await new Promise((r) => setTimeout(r, 2500))
|
|
811
|
+
if (Date.now() > deadline) {
|
|
812
|
+
runInAction(() => {
|
|
813
|
+
this.hostWatch = 'idle'
|
|
814
|
+
this.hostError = `${host} has not answered for 2 minutes — check the console`
|
|
815
|
+
})
|
|
816
|
+
return
|
|
817
|
+
}
|
|
818
|
+
const up = await pingHost({ host })
|
|
819
|
+
.then((r) => r.up)
|
|
820
|
+
.catch(() => false)
|
|
821
|
+
if (!up) continue
|
|
822
|
+
runInAction(() => {
|
|
823
|
+
this.hostWatch = 'back'
|
|
824
|
+
this.hostNote = `${host} is back up`
|
|
825
|
+
})
|
|
826
|
+
setTimeout(() => runInAction(() => (this.hostWatch = 'idle')), 4000)
|
|
827
|
+
return
|
|
828
|
+
}
|
|
829
|
+
}
|
|
830
|
+
|
|
831
|
+
/** interrupt / clear the queue / reboot the box this workflow runs on */
|
|
832
|
+
async hostAction(action: HostAction): Promise<void> {
|
|
833
|
+
const host = this.form == null ? null : this.hostFor(this.form.moduleKey)
|
|
834
|
+
if (host == null || host === '') return
|
|
835
|
+
runInAction(() => {
|
|
836
|
+
this.hostError = null
|
|
837
|
+
this.hostNote = null
|
|
838
|
+
})
|
|
839
|
+
try {
|
|
840
|
+
const reply = await postHostAction({ host, action })
|
|
841
|
+
runInAction(() => {
|
|
842
|
+
this.hostNote = reply.note
|
|
843
|
+
})
|
|
844
|
+
// a reboot is the one action whose result arrives LATER: watch for it
|
|
845
|
+
if (action === 'restart') void this.watchHostComeBack()
|
|
846
|
+
} catch (e) {
|
|
847
|
+
runInAction(() => {
|
|
848
|
+
this.hostError = e instanceof Error ? e.message : String(e)
|
|
849
|
+
})
|
|
850
|
+
}
|
|
851
|
+
}
|
|
852
|
+
|
|
853
|
+
/** run this workflow somewhere else (the TUI's host override). null resets to its own host */
|
|
854
|
+
async setModuleHost(p: { module: string; host: string | null }): Promise<void> {
|
|
855
|
+
runInAction(() => {
|
|
856
|
+
this.hostError = null
|
|
857
|
+
})
|
|
858
|
+
try {
|
|
859
|
+
const reply = await setModuleHost(p)
|
|
860
|
+
runInAction(() => {
|
|
861
|
+
this.hosts = { ...this.hosts, overrides: reply.overrides }
|
|
862
|
+
})
|
|
863
|
+
} catch (e) {
|
|
864
|
+
runInAction(() => {
|
|
865
|
+
this.hostError = e instanceof Error ? e.message : String(e)
|
|
866
|
+
})
|
|
867
|
+
}
|
|
868
|
+
}
|
|
869
|
+
|
|
870
|
+
private async loadSettings(): Promise<void> {
|
|
871
|
+
try {
|
|
872
|
+
const s = await fetchSettings()
|
|
873
|
+
runInAction(() => {
|
|
874
|
+
this.settings = s
|
|
875
|
+
})
|
|
876
|
+
} catch (e) {
|
|
877
|
+
// same as the host list: this process owns /settings, so a failure is a real one and
|
|
878
|
+
// the panel would otherwise show the DEFAULT setting as if it were the server's
|
|
879
|
+
logWebError('could not read the serve settings', e)
|
|
880
|
+
}
|
|
881
|
+
}
|
|
882
|
+
|
|
883
|
+
/** what the prefix input shows: the typed value while editing, else the effective folder */
|
|
884
|
+
savePrefixDraft(moduleKey: string): string {
|
|
885
|
+
return this.prefixEdits[moduleKey] ?? this.settings.savePrefix[moduleKey] ?? ''
|
|
886
|
+
}
|
|
887
|
+
|
|
888
|
+
/** typing is local and instant; the write is debounced, the live-drafts idiom. An invalid
|
|
889
|
+
* folder is rejected by the server and shown, the field keeps what you typed */
|
|
890
|
+
setSavePrefix(moduleKey: string, value: string): void {
|
|
891
|
+
this.prefixEdits[moduleKey] = value
|
|
892
|
+
this.savingError = null
|
|
893
|
+
const timer = this.prefixTimers.get(moduleKey)
|
|
894
|
+
if (timer != null) clearTimeout(timer)
|
|
895
|
+
this.prefixTimers.set(
|
|
896
|
+
moduleKey,
|
|
897
|
+
setTimeout(() => {
|
|
898
|
+
void this.pushSettings({ savePrefix: { [moduleKey]: value } })
|
|
899
|
+
}, 500),
|
|
900
|
+
)
|
|
901
|
+
}
|
|
902
|
+
|
|
903
|
+
private async pushSettings(patch: { saveToDisk?: boolean; savePrefix?: Record<string, string> }): Promise<boolean> {
|
|
904
|
+
try {
|
|
905
|
+
const next = await saveSettings(patch)
|
|
906
|
+
runInAction(() => {
|
|
907
|
+
this.settings = next
|
|
908
|
+
this.savingError = null
|
|
909
|
+
})
|
|
910
|
+
return true
|
|
911
|
+
} catch (e) {
|
|
912
|
+
runInAction(() => {
|
|
913
|
+
this.savingError = e instanceof Error ? e.message : String(e)
|
|
914
|
+
})
|
|
915
|
+
return false
|
|
916
|
+
}
|
|
917
|
+
}
|
|
918
|
+
|
|
919
|
+
/** flip where outputs go. Optimistic, then reconciled with what the server confirms */
|
|
920
|
+
async toggleSaveToDisk(): Promise<void> {
|
|
921
|
+
const next = !this.saveToDisk
|
|
922
|
+
const before = this.settings
|
|
923
|
+
runInAction(() => {
|
|
924
|
+
this.settings = { ...this.settings, saveToDisk: next }
|
|
925
|
+
this.savingError = null
|
|
926
|
+
})
|
|
927
|
+
// pushSettings reports the failure; roll the optimistic flip back so the switch
|
|
928
|
+
// never shows a state the server refused
|
|
929
|
+
if (!(await this.pushSettings({ saveToDisk: next })))
|
|
930
|
+
runInAction(() => {
|
|
931
|
+
this.settings = before
|
|
932
|
+
})
|
|
933
|
+
}
|
|
934
|
+
|
|
174
935
|
generate(): void {
|
|
175
936
|
void this.generateNow()
|
|
176
937
|
}
|
|
177
938
|
|
|
178
939
|
/** flush the autosave first: the server reads the draft it just wrote — one source of truth.
|
|
179
|
-
*
|
|
940
|
+
* every click ENQUEUES, so hitting generate n times runs n prompts; the queued payload
|
|
180
941
|
* freezes the values you saw, and seeds stay on the draft's server-side policy */
|
|
181
942
|
private async generateNow(): Promise<void> {
|
|
182
943
|
const form = this.form
|
|
@@ -188,10 +949,41 @@ export class WebSt {
|
|
|
188
949
|
this.run.enqueue({ module: form.moduleKey, draft: form.draft, payload })
|
|
189
950
|
}
|
|
190
951
|
|
|
952
|
+
/** rename = write the values under the new name, switch to it, then drop the old FILE.
|
|
953
|
+
* same shape as the enhancer presets: the name IS the identity, so there is nothing to
|
|
954
|
+
* "rename" server side. The old file is only removed once the new one is confirmed */
|
|
955
|
+
async renameDraft(rawName: string): Promise<void> {
|
|
956
|
+
const form = this.form
|
|
957
|
+
const name = rawName.trim()
|
|
958
|
+
if (form == null || name === '' || name === form.draft) return
|
|
959
|
+
const from = form.draft
|
|
960
|
+
await this.duplicateDraft(name)
|
|
961
|
+
// duplicateDraft reports its own failure; only drop the old file if the switch happened
|
|
962
|
+
if (this.form?.draft !== name) return
|
|
963
|
+
await deleteDraft({ module: form.moduleKey, draft: from }).then(
|
|
964
|
+
(reply) =>
|
|
965
|
+
runInAction(() => {
|
|
966
|
+
this.modules = this.modules.map((m) =>
|
|
967
|
+
m.module === form.moduleKey ? { ...m, drafts: reply.drafts } : m,
|
|
968
|
+
)
|
|
969
|
+
}),
|
|
970
|
+
(e: unknown) =>
|
|
971
|
+
runInAction(() => {
|
|
972
|
+
this.formError = `renamed to '${name}', but the old draft '${from}' could not be removed: ${
|
|
973
|
+
e instanceof Error ? e.message : String(e)
|
|
974
|
+
}`
|
|
975
|
+
}),
|
|
976
|
+
)
|
|
977
|
+
}
|
|
978
|
+
|
|
191
979
|
/** delete the draft FILE, then fall back to another draft (DraftsSt.deleteDraft's rule).
|
|
192
|
-
* ORDER: the form is dropped WITHOUT flushing FIRST
|
|
980
|
+
* ORDER: the form is dropped WITHOUT flushing FIRST, its autosave would otherwise
|
|
193
981
|
* re-create the file the server is about to delete */
|
|
194
|
-
|
|
982
|
+
deleteDraft(p: { module: string; draft: string }): Promise<void> {
|
|
983
|
+
return this.trackSwitch(this.deleteDraftNow(p))
|
|
984
|
+
}
|
|
985
|
+
|
|
986
|
+
private async deleteDraftNow(p: { module: string; draft: string }): Promise<void> {
|
|
195
987
|
const form = this.form
|
|
196
988
|
if (form != null && form.moduleKey === p.module && form.draft === p.draft) {
|
|
197
989
|
form.dispose({ flush: false })
|