comfy-ts 2.8.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/dist/cli.js +2 -2
- package/dist/index.cjs +1 -1
- package/dist/index.js +1 -1
- package/dist/{run-tui-BPw-SZYW.js → run-tui-BLEdTsOX.js} +2 -2
- package/dist/{run-tui-BPw-SZYW.js.map → run-tui-BLEdTsOX.js.map} +1 -1
- package/dist/serve-web.js +26 -17
- package/dist/{state-B-boDz-Z.js → state-BbgJeCe6.js} +2 -2
- package/dist/{state-B-boDz-Z.js.map → state-BbgJeCe6.js.map} +1 -1
- package/dist/web.d.ts +77 -1
- package/dist/web.js +1 -1
- package/dist/web.js.map +1 -1
- package/package.json +1 -1
- package/src/cli/serve/hostProtocol.ts +68 -0
- package/src/cli/serve/web/components/Gallery.tsx +70 -18
- package/src/cli/serve/web/state/FormSt.ts +8 -1
- package/src/cli/serve/web/state/WebSt.ts +297 -9
- package/src/cli/serve/web/state/host.ts +206 -0
- package/src/cli/serve/web/styles.ts +9 -0
- package/src/web.ts +12 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "comfy-ts",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.9.0",
|
|
4
4
|
"description": "The complete TypeScript toolkit for ComfyUI and generative AI. Fully type-safe SDK with autocomplete on every node, companion CLI, TUI, agentic docs, and 50+ examples. Design, run and deploy creative pipelines for image, video, audio, 3d and text on any ComfyUI, local, cloud or hosted.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"comfyui",
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
// the serve web panel's HOST PROTOCOL, as types only: what a page embedding the panel in an
|
|
2
|
+
// <iframe> sends and receives over postMessage. No runtime, no DOM, no imports: `comfy-ts/web`
|
|
3
|
+
// re-exports these as types, and the panel's parsers live in src/cli/serve/web/state/host.ts.
|
|
4
|
+
// The contract, message by message, is agent/architecture.md → HOST PROTOCOL.
|
|
5
|
+
|
|
6
|
+
/** a button the host asks the panel to draw on every result */
|
|
7
|
+
export type PanelHostAction = { id: string; label: string; title?: string }
|
|
8
|
+
|
|
9
|
+
/** the host requests that always get exactly one reply: `state` or `error` */
|
|
10
|
+
export type PanelRequest = 'set-selection' | 'set-values' | 'get-state'
|
|
11
|
+
|
|
12
|
+
/** the form as it is now: var name → value in the draft file's shape (seed `{mode,value}`,
|
|
13
|
+
* size `{width,height}`, loras `{name: strength}`). `request` names the host request this
|
|
14
|
+
* answers; absent when the panel volunteers it (a user edit, a user switch, run seeds).
|
|
15
|
+
* `rejected` is on a `set-values` reply only: every name that was NOT applied (no such var,
|
|
16
|
+
* or a value its control cannot produce). Never posted before the host origin is pinned */
|
|
17
|
+
export type PanelState = {
|
|
18
|
+
comfyTs: 'state'
|
|
19
|
+
module: string
|
|
20
|
+
draft: string
|
|
21
|
+
values: Record<string, unknown>
|
|
22
|
+
request?: PanelRequest
|
|
23
|
+
rejected?: string[]
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** `code: 'boot'` (no `request`): the panel could not boot (index failed, zero workflows, the
|
|
27
|
+
* opening draft unreadable); may go out before the pin. Every other code answers one request:
|
|
28
|
+
* `unknown-module` · `select-failed` · `superseded` · `no-form` */
|
|
29
|
+
export type PanelError =
|
|
30
|
+
| { comfyTs: 'error'; code: 'boot'; message: string }
|
|
31
|
+
| {
|
|
32
|
+
comfyTs: 'error'
|
|
33
|
+
code: 'unknown-module' | 'select-failed' | 'superseded' | 'no-form'
|
|
34
|
+
message: string
|
|
35
|
+
request: PanelRequest
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export type PanelResultAction = {
|
|
39
|
+
comfyTs: 'result-action'
|
|
40
|
+
id: string
|
|
41
|
+
promptId: string
|
|
42
|
+
ix: number
|
|
43
|
+
/** the image url, relative to the panel's own origin (`/images/<promptId>/<ix>` or `/outputs/…`) */
|
|
44
|
+
url: string
|
|
45
|
+
filename: string
|
|
46
|
+
module: string
|
|
47
|
+
draft: string
|
|
48
|
+
seeds: Record<string, number>
|
|
49
|
+
/** the prompt var's text at click time, when the form has one */
|
|
50
|
+
prompt: string | null
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** panel → host. `selection` is the light subset of `state` (no values), so it may go out
|
|
54
|
+
* before the pin, like `ready` and the boot error */
|
|
55
|
+
export type PanelToHost =
|
|
56
|
+
| { comfyTs: 'ready' }
|
|
57
|
+
| { comfyTs: 'selection'; module: string; draft: string }
|
|
58
|
+
| PanelState
|
|
59
|
+
| PanelError
|
|
60
|
+
| PanelResultAction
|
|
61
|
+
|
|
62
|
+
/** host → panel */
|
|
63
|
+
export type HostToPanel =
|
|
64
|
+
| { comfyTs: 'host-actions'; actions: PanelHostAction[] }
|
|
65
|
+
| { comfyTs: 'set-prompt'; text: string }
|
|
66
|
+
| { comfyTs: 'set-selection'; module: string; draft: string }
|
|
67
|
+
| { comfyTs: 'set-values'; values: Record<string, unknown> }
|
|
68
|
+
| { comfyTs: 'get-state' }
|
|
@@ -166,6 +166,24 @@ const Lightbox = observer(function Lightbox(p: { st: WebSt; local: GalleryLocal
|
|
|
166
166
|
<a href={box.url} target="_blank" rel="noreferrer">
|
|
167
167
|
open ↗
|
|
168
168
|
</a>
|
|
169
|
+
{box.promptId != null
|
|
170
|
+
? p.st.hostActions.map((a) => {
|
|
171
|
+
const run = p.st.run.results.find((r) => r.promptId === box.promptId)
|
|
172
|
+
const ix = run?.images.findIndex((i) => i.url === box.url) ?? -1
|
|
173
|
+
if (run == null || ix < 0) return null
|
|
174
|
+
return (
|
|
175
|
+
<button
|
|
176
|
+
key={a.id}
|
|
177
|
+
type="button"
|
|
178
|
+
className="host-action"
|
|
179
|
+
data-tip={a.title}
|
|
180
|
+
onClick={() => p.st.postResultAction(a.id, run, { url: box.url, filename: box.title }, ix)}
|
|
181
|
+
>
|
|
182
|
+
{a.label}
|
|
183
|
+
</button>
|
|
184
|
+
)
|
|
185
|
+
})
|
|
186
|
+
: null}
|
|
169
187
|
{box.promptId != null ? (
|
|
170
188
|
<button
|
|
171
189
|
type="button"
|
|
@@ -359,25 +377,59 @@ export const Gallery = observer(function Gallery(p: { st: WebSt; compact?: boole
|
|
|
359
377
|
</button>
|
|
360
378
|
</div>
|
|
361
379
|
<div className="imgs">
|
|
362
|
-
{r.images.map((img) =>
|
|
380
|
+
{r.images.map((img, ix) =>
|
|
363
381
|
img.url != null ? (
|
|
364
|
-
<
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
382
|
+
<div key={img.filename} className="img-cell">
|
|
383
|
+
<button
|
|
384
|
+
type="button"
|
|
385
|
+
className="img-button"
|
|
386
|
+
data-tip={img.filename}
|
|
387
|
+
onClick={() => {
|
|
388
|
+
if (img.url != null)
|
|
389
|
+
local.openLightbox({
|
|
390
|
+
kind: 'image',
|
|
391
|
+
url: img.url,
|
|
392
|
+
title: img.filename,
|
|
393
|
+
promptId: r.promptId,
|
|
394
|
+
})
|
|
395
|
+
}}
|
|
396
|
+
>
|
|
397
|
+
<img src={img.url} alt={img.filename} />
|
|
398
|
+
</button>
|
|
399
|
+
{/* the EMBEDDING page's buttons (host protocol): what it does with the
|
|
400
|
+
image is its business — send it somewhere, keep it as something */}
|
|
401
|
+
{p.st.hostActions.length > 0 ? (
|
|
402
|
+
<div className="host-actions">
|
|
403
|
+
{p.st.hostActions.map((a) => (
|
|
404
|
+
<button
|
|
405
|
+
key={a.id}
|
|
406
|
+
type="button"
|
|
407
|
+
className="host-action"
|
|
408
|
+
data-tip={a.title}
|
|
409
|
+
onClick={() => {
|
|
410
|
+
if (img.url != null)
|
|
411
|
+
p.st.postResultAction(
|
|
412
|
+
a.id,
|
|
413
|
+
r,
|
|
414
|
+
{ url: img.url, filename: img.filename },
|
|
415
|
+
ix,
|
|
416
|
+
)
|
|
417
|
+
}}
|
|
418
|
+
>
|
|
419
|
+
{a.label}
|
|
420
|
+
</button>
|
|
421
|
+
))}
|
|
422
|
+
<button
|
|
423
|
+
type="button"
|
|
424
|
+
className="host-action danger"
|
|
425
|
+
data-tip="remove this result"
|
|
426
|
+
onClick={() => p.st.run.remove(r.promptId)}
|
|
427
|
+
>
|
|
428
|
+
✕
|
|
429
|
+
</button>
|
|
430
|
+
</div>
|
|
431
|
+
) : null}
|
|
432
|
+
</div>
|
|
381
433
|
) : (
|
|
382
434
|
<div key={img.filename} className="noimg">
|
|
383
435
|
{img.filename} (not saved locally — no preview)
|
|
@@ -69,6 +69,9 @@ export class FormSt {
|
|
|
69
69
|
private lastQueued: string
|
|
70
70
|
/** identity of the newest queued write, so a rollback cannot claim someone else's */
|
|
71
71
|
private queueSeq = 0
|
|
72
|
+
/** called on the autosave debounce, right after the save is queued: the embedding host
|
|
73
|
+
* mirrors the form from it (WebSt), one call per settled burst of edits, never per key */
|
|
74
|
+
onSettled: (() => void) | null = null
|
|
72
75
|
|
|
73
76
|
constructor(
|
|
74
77
|
public readonly moduleKey: string,
|
|
@@ -95,12 +98,16 @@ export class FormSt {
|
|
|
95
98
|
lastSaved: false,
|
|
96
99
|
lastQueued: false,
|
|
97
100
|
queueSeq: false,
|
|
101
|
+
onSettled: false,
|
|
98
102
|
})
|
|
99
103
|
// the persistence idiom: the values json is change-detector AND payload
|
|
100
104
|
this.disposers.push(
|
|
101
105
|
reaction(
|
|
102
106
|
() => JSON.stringify(this.valuesJSON()),
|
|
103
|
-
() =>
|
|
107
|
+
() => {
|
|
108
|
+
void this.save()
|
|
109
|
+
this.onSettled?.()
|
|
110
|
+
},
|
|
104
111
|
{ delay: 500 },
|
|
105
112
|
),
|
|
106
113
|
)
|
|
@@ -19,11 +19,22 @@ import {
|
|
|
19
19
|
type ServeSettings,
|
|
20
20
|
} from 'src/cli/serve/web/api.ts'
|
|
21
21
|
import { EnhancerSt } from 'src/cli/serve/web/state/EnhancerSt.ts'
|
|
22
|
-
import { FormSt } from 'src/cli/serve/web/state/FormSt.ts'
|
|
22
|
+
import { FormSt, type VarSt } from 'src/cli/serve/web/state/FormSt.ts'
|
|
23
23
|
import { logWebError } from 'src/cli/serve/web/logWeb.ts'
|
|
24
24
|
import { asSeedForm } from 'src/cli/serve/web/state/payload.ts'
|
|
25
25
|
import { readUrlSelection, resolveSelection, writeUrlSelection } from 'src/cli/serve/web/state/urlSelection.ts'
|
|
26
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'
|
|
27
38
|
|
|
28
39
|
/** selection + drawer survive a reload: hand-tuned state persists and restores */
|
|
29
40
|
const STORAGE_KEY = 'comfy-ts-serve-ui'
|
|
@@ -168,6 +179,20 @@ export class WebSt {
|
|
|
168
179
|
/** hosts this process knows + where each module runs (server-owned, like the drafts) */
|
|
169
180
|
hosts: HostsPayload = { hosts: [], defaults: {}, overrides: {} }
|
|
170
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()
|
|
171
196
|
|
|
172
197
|
constructor() {
|
|
173
198
|
this.run = new RunSt()
|
|
@@ -192,7 +217,18 @@ export class WebSt {
|
|
|
192
217
|
this.showLatent = stored.latent ?? true
|
|
193
218
|
this.varOrder = stored.varOrder ?? {}
|
|
194
219
|
this.showLogs = stored.logs ?? false
|
|
195
|
-
makeAutoObservable
|
|
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))
|
|
196
232
|
// a closing/hidden tab must not lose an edit still inside the autosave debounce
|
|
197
233
|
window.addEventListener('beforeunload', () => this.form?.flushKeepalive())
|
|
198
234
|
document.addEventListener('visibilitychange', () => {
|
|
@@ -200,7 +236,8 @@ export class WebSt {
|
|
|
200
236
|
// keepalive is unchained by necessity and could land before an older one
|
|
201
237
|
if (document.visibilityState === 'hidden') void this.form?.save()
|
|
202
238
|
})
|
|
203
|
-
|
|
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))
|
|
204
241
|
}
|
|
205
242
|
|
|
206
243
|
toggleSidebar(): void {
|
|
@@ -246,6 +283,7 @@ export class WebSt {
|
|
|
246
283
|
varSt.setFromRun({ mode: current.mode, value: used })
|
|
247
284
|
}
|
|
248
285
|
})
|
|
286
|
+
this.mirrorState()
|
|
249
287
|
}
|
|
250
288
|
|
|
251
289
|
/** the address bar follows the selection, so the url on screen is always the one to share.
|
|
@@ -316,20 +354,57 @@ export class WebSt {
|
|
|
316
354
|
stored: { module: stored.module ?? null, draft: stored.draft ?? null },
|
|
317
355
|
modules: this.modules,
|
|
318
356
|
})
|
|
319
|
-
|
|
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
|
+
}
|
|
320
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' })
|
|
321
377
|
} catch (e) {
|
|
378
|
+
const message = e instanceof Error ? e.message : String(e)
|
|
322
379
|
runInAction(() => {
|
|
323
380
|
this.phase = 'error'
|
|
324
|
-
this.bootError =
|
|
381
|
+
this.bootError = message
|
|
325
382
|
})
|
|
383
|
+
this.postHost({ comfyTs: 'error', code: 'boot', message })
|
|
326
384
|
}
|
|
327
385
|
}
|
|
328
386
|
|
|
329
387
|
/** latest-wins guard for rapid draft clicks: only the newest select may write form state */
|
|
330
388
|
private selectToken = 0
|
|
331
389
|
|
|
332
|
-
|
|
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> {
|
|
333
408
|
const mod = this.moduleByKey(p.module)
|
|
334
409
|
if (mod == null) return
|
|
335
410
|
// re-picking the draft you are IN is a no-op, not a reload: a pending autosave would
|
|
@@ -367,12 +442,18 @@ export class WebSt {
|
|
|
367
442
|
}
|
|
368
443
|
try {
|
|
369
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()
|
|
370
450
|
runInAction(() => {
|
|
371
|
-
|
|
372
|
-
this.form = new FormSt(p.module, p.draft, mod, reply.values ?? {})
|
|
451
|
+
this.form = form
|
|
373
452
|
this.persist()
|
|
374
453
|
this.syncUrl()
|
|
375
454
|
})
|
|
455
|
+
this.postHost({ comfyTs: 'selection', module: form.moduleKey, draft: form.draft })
|
|
456
|
+
if (mirror) this.mirrorState()
|
|
376
457
|
} catch (e) {
|
|
377
458
|
runInAction(() => {
|
|
378
459
|
if (token !== this.selectToken) return
|
|
@@ -389,6 +470,205 @@ export class WebSt {
|
|
|
389
470
|
}
|
|
390
471
|
}
|
|
391
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
|
+
|
|
392
672
|
/** side and pinned show the results next to (or over) the form, so the run button belongs
|
|
393
673
|
* there: in pinned it stays on screen while the form scrolls */
|
|
394
674
|
get generateInResults(): boolean {
|
|
@@ -482,6 +762,10 @@ export class WebSt {
|
|
|
482
762
|
this.loraSyncing = true
|
|
483
763
|
})
|
|
484
764
|
await this.hostAction('refresh-loras')
|
|
765
|
+
await this.trackSwitch(this.reloadIndexAndForm())
|
|
766
|
+
}
|
|
767
|
+
|
|
768
|
+
private async reloadIndexAndForm(): Promise<void> {
|
|
485
769
|
// the option lists (and the manager-only union) live in the descriptors: re-read them,
|
|
486
770
|
// then re-select the same draft so the form is rebuilt against the new options
|
|
487
771
|
try {
|
|
@@ -695,7 +979,11 @@ export class WebSt {
|
|
|
695
979
|
/** delete the draft FILE, then fall back to another draft (DraftsSt.deleteDraft's rule).
|
|
696
980
|
* ORDER: the form is dropped WITHOUT flushing FIRST, its autosave would otherwise
|
|
697
981
|
* re-create the file the server is about to delete */
|
|
698
|
-
|
|
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> {
|
|
699
987
|
const form = this.form
|
|
700
988
|
if (form != null && form.moduleKey === p.module && form.draft === p.draft) {
|
|
701
989
|
form.dispose({ flush: false })
|