comfy-ts 2.4.2 → 2.6.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 +12 -1
- package/dist/{DefinedWorkflow-CnzmQrBh.js → DefinedWorkflow-BJgt5t0x.js} +14 -3
- package/dist/DefinedWorkflow-BJgt5t0x.js.map +1 -0
- package/dist/{DraftsSt-C1MbWnEp.js → DraftsSt-dizp3VcC.js} +2 -2
- package/dist/{DraftsSt-C1MbWnEp.js.map → DraftsSt-dizp3VcC.js.map} +1 -1
- package/dist/cli.js +4 -4
- package/dist/index.cjs +3 -2
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +3 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +3 -2
- package/dist/index.js.map +1 -1
- package/dist/{run-serve-D47Zas-a.js → run-serve-CVu39RNd.js} +330 -16
- package/dist/run-serve-CVu39RNd.js.map +1 -0
- package/dist/{run-tui-BqNO9Ck0.js → run-tui-idFhdO4f.js} +707 -527
- package/dist/run-tui-idFhdO4f.js.map +1 -0
- package/dist/serve-web.js +280 -0
- package/dist/{state-D9w1aJow.js → state-C90kdjtB.js} +3 -13
- package/dist/state-C90kdjtB.js.map +1 -0
- package/dist/web.d.ts +3 -0
- package/dist/web.js +3 -2
- package/dist/web.js.map +1 -1
- package/package.json +9 -7
- package/src/cli/serve/ServeApp.ts +314 -13
- package/src/cli/serve/describeVar.ts +6 -1
- package/src/cli/serve/run-serve.ts +4 -2
- package/src/cli/serve/web/api.ts +105 -0
- package/src/cli/serve/web/clipboard.ts +25 -0
- package/src/cli/serve/web/components/App.tsx +53 -0
- package/src/cli/serve/web/components/Gallery.tsx +186 -0
- package/src/cli/serve/web/components/PromptEnhancer.tsx +210 -0
- package/src/cli/serve/web/components/Sidebar.tsx +42 -0
- package/src/cli/serve/web/components/VarsForm.tsx +173 -0
- package/src/cli/serve/web/components/controls/BasicControls.tsx +95 -0
- package/src/cli/serve/web/components/controls/ImageControl.tsx +71 -0
- package/src/cli/serve/web/components/controls/LorasControl.tsx +340 -0
- package/src/cli/serve/web/components/controls/SeedControl.tsx +43 -0
- package/src/cli/serve/web/components/controls/SizeControl.tsx +49 -0
- package/src/cli/serve/web/main.tsx +18 -0
- package/src/cli/serve/web/openrouter.ts +142 -0
- package/src/cli/serve/web/state/EnhancerSt.ts +370 -0
- package/src/cli/serve/web/state/FormSt.ts +177 -0
- package/src/cli/serve/web/state/RunSt.ts +136 -0
- package/src/cli/serve/web/state/WebSt.ts +210 -0
- package/src/cli/serve/web/state/payload.ts +110 -0
- package/src/cli/serve/web/styles.ts +259 -0
- package/src/cli/serve/web/tsconfig.json +15 -0
- package/src/cli/serve/webBundle.ts +66 -0
- package/src/cli/tui/imageEscapes.ts +105 -0
- package/src/cli/tui/imagePicker/ImagePickerSt.ts +21 -8
- package/src/cli/tui/protocolImage.ts +39 -0
- package/src/cli/tui/protocolImagePainter.ts +56 -23
- package/src/cli/tui/run-tui.tsx +3 -2
- package/src/cli/tui/state/LorasSt.ts +22 -9
- package/src/cli/tui/state/PreviewSt.ts +31 -13
- package/src/cli/tui/state/SettingsSt.ts +1 -1
- package/src/cli/tui/state/TuiSt.ts +4 -4
- package/src/vars/ComfyVars.ts +4 -1
- package/dist/DefinedWorkflow-CnzmQrBh.js.map +0 -1
- package/dist/run-serve-D47Zas-a.js.map +0 -1
- package/dist/run-tui-BqNO9Ck0.js.map +0 -1
- package/dist/state-D9w1aJow.js.map +0 -1
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
// form state for ONE module+draft selection — swapped whole on WebSt when the
|
|
2
|
+
// selection changes (observableRef there, dispose() first: the autosave
|
|
3
|
+
// reaction is owned here). DRAFTS ARE LIVE (the TUI model): edits autosave
|
|
4
|
+
// through PUT /drafts, generate posts {} — the draft is the one source of truth
|
|
5
|
+
import { makeAutoObservable, observableRef, reaction, runInAction, type IReactionDisposer } from 'mobx'
|
|
6
|
+
import { saveDraft, type ModuleDescription } from 'src/cli/serve/web/api.ts'
|
|
7
|
+
import type { VarDescriptor } from 'src/cli/serve/describeVar.ts'
|
|
8
|
+
import { normalizeInitial, payloadSnapshot } from 'src/cli/serve/web/state/payload.ts'
|
|
9
|
+
|
|
10
|
+
/** one var row: value in toJSON shape, replaced whole on every edit */
|
|
11
|
+
export class VarSt {
|
|
12
|
+
value: unknown
|
|
13
|
+
/** changed since the draft loaded — drives the revert affordance, not the payload */
|
|
14
|
+
dirty = false
|
|
15
|
+
/** image vars only: a browser-visible url for the current value (upload response or http value) */
|
|
16
|
+
uploadedUrl: string | null = null
|
|
17
|
+
|
|
18
|
+
constructor(
|
|
19
|
+
public readonly name: string,
|
|
20
|
+
public readonly desc: VarDescriptor,
|
|
21
|
+
public readonly initial: unknown,
|
|
22
|
+
) {
|
|
23
|
+
this.value = initial
|
|
24
|
+
makeAutoObservable(this, { value: observableRef, name: false, desc: false, initial: false })
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
set(value: unknown): void {
|
|
28
|
+
this.value = value
|
|
29
|
+
this.dirty = true
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
setUploadedUrl(url: string | null): void {
|
|
33
|
+
this.uploadedUrl = url
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
revert(): void {
|
|
37
|
+
this.value = this.initial
|
|
38
|
+
this.dirty = false
|
|
39
|
+
this.uploadedUrl = null
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
snapshot(): { name: string; value: unknown } {
|
|
43
|
+
return { name: this.name, value: this.value }
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export class FormSt {
|
|
48
|
+
vars: VarSt[]
|
|
49
|
+
/** the module's host id — lora hover data routes are host-scoped */
|
|
50
|
+
readonly host: string
|
|
51
|
+
saveState: 'saved' | 'saving' | 'error' = 'saved'
|
|
52
|
+
saveError: string | null = null
|
|
53
|
+
private disposers: IReactionDisposer[] = []
|
|
54
|
+
/** saves are chained so two PUTs can never land out of order */
|
|
55
|
+
private saveChain: Promise<boolean> = Promise.resolve(true)
|
|
56
|
+
/** the values json the server last confirmed — save() no-ops when nothing changed */
|
|
57
|
+
private lastSaved: string
|
|
58
|
+
|
|
59
|
+
constructor(
|
|
60
|
+
public readonly moduleKey: string,
|
|
61
|
+
public readonly draft: string,
|
|
62
|
+
mod: ModuleDescription,
|
|
63
|
+
values: Record<string, unknown>,
|
|
64
|
+
) {
|
|
65
|
+
this.host = mod.host
|
|
66
|
+
this.vars = Object.entries(mod.vars).map(
|
|
67
|
+
([name, desc]) => new VarSt(name, desc, normalizeInitial(desc, values[name])),
|
|
68
|
+
)
|
|
69
|
+
// seeded from the RAW reply, not the normalized values: when normalizeInitial heals
|
|
70
|
+
// something (a stale lora key), the form is already out of sync with the file and the
|
|
71
|
+
// next save() must actually send — otherwise the server keeps building the stale record
|
|
72
|
+
this.lastSaved = JSON.stringify(Object.fromEntries(this.vars.map((v) => [v.name, values[v.name]])))
|
|
73
|
+
makeAutoObservable<FormSt, 'disposers' | 'saveChain' | 'lastSaved'>(this, {
|
|
74
|
+
vars: false,
|
|
75
|
+
moduleKey: false,
|
|
76
|
+
draft: false,
|
|
77
|
+
host: false,
|
|
78
|
+
disposers: false,
|
|
79
|
+
saveChain: false,
|
|
80
|
+
lastSaved: false,
|
|
81
|
+
})
|
|
82
|
+
// the persistence idiom: the values json is change-detector AND payload
|
|
83
|
+
this.disposers.push(
|
|
84
|
+
reaction(
|
|
85
|
+
() => JSON.stringify(this.valuesJSON()),
|
|
86
|
+
() => void this.save(),
|
|
87
|
+
{ delay: 500 },
|
|
88
|
+
),
|
|
89
|
+
)
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
dispose(): void {
|
|
93
|
+
for (const d of this.disposers) d()
|
|
94
|
+
this.disposers = []
|
|
95
|
+
// a draft switch inside the debounce window must not lose the edit
|
|
96
|
+
if (JSON.stringify(this.valuesJSON()) !== this.lastSaved) void this.save()
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/** tab-close/hide flush. keepalive survives page teardown, so this one does NOT ride
|
|
100
|
+
* saveChain (chaining could delay it past unload). lastSaved is committed only when the
|
|
101
|
+
* server answers: a failed flush must stay dirty so the next save() retries it */
|
|
102
|
+
flushKeepalive(): void {
|
|
103
|
+
const encoded = JSON.stringify(this.valuesJSON())
|
|
104
|
+
if (encoded === this.lastSaved) return
|
|
105
|
+
void saveDraft(
|
|
106
|
+
{ module: this.moduleKey, draft: this.draft, values: JSON.parse(encoded) as Record<string, unknown> },
|
|
107
|
+
{ keepalive: true },
|
|
108
|
+
).then(
|
|
109
|
+
() =>
|
|
110
|
+
runInAction(() => {
|
|
111
|
+
this.lastSaved = encoded
|
|
112
|
+
this.saveState = 'saved'
|
|
113
|
+
}),
|
|
114
|
+
(e: unknown) =>
|
|
115
|
+
runInAction(() => {
|
|
116
|
+
this.saveState = 'error'
|
|
117
|
+
this.saveError = e instanceof Error ? e.message : String(e)
|
|
118
|
+
}),
|
|
119
|
+
)
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
valuesJSON(): Record<string, unknown> {
|
|
123
|
+
return Object.fromEntries(this.vars.map((v) => [v.name, v.value]))
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/** frozen values for a QUEUED run (seeds excluded — payload.ts owns the why) */
|
|
127
|
+
queuePayload(): Record<string, unknown> {
|
|
128
|
+
return payloadSnapshot(this.vars.map((v) => ({ name: v.name, kind: v.desc.kind, value: v.value })))
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
get dirtyCount(): number {
|
|
132
|
+
return this.vars.filter((v) => v.dirty).length
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/** persist the draft now. Resolves FALSE on failure — generate() must not run stale inputs */
|
|
136
|
+
save(): Promise<boolean> {
|
|
137
|
+
const encoded = JSON.stringify(this.valuesJSON())
|
|
138
|
+
if (encoded === this.lastSaved) {
|
|
139
|
+
// nothing to write: the disk already holds these values, whatever an OLDER save did.
|
|
140
|
+
// returning the previous chain here would keep a stale false alive and dead-lock generate
|
|
141
|
+
runInAction(() => {
|
|
142
|
+
this.saveState = 'saved'
|
|
143
|
+
this.saveError = null
|
|
144
|
+
})
|
|
145
|
+
return Promise.resolve(true)
|
|
146
|
+
}
|
|
147
|
+
runInAction(() => {
|
|
148
|
+
this.saveState = 'saving'
|
|
149
|
+
})
|
|
150
|
+
this.saveChain = this.saveChain.then(async () => {
|
|
151
|
+
try {
|
|
152
|
+
await saveDraft({
|
|
153
|
+
module: this.moduleKey,
|
|
154
|
+
draft: this.draft,
|
|
155
|
+
values: JSON.parse(encoded) as Record<string, unknown>,
|
|
156
|
+
})
|
|
157
|
+
runInAction(() => {
|
|
158
|
+
this.lastSaved = encoded
|
|
159
|
+
this.saveState = 'saved'
|
|
160
|
+
this.saveError = null
|
|
161
|
+
})
|
|
162
|
+
return true
|
|
163
|
+
} catch (e) {
|
|
164
|
+
runInAction(() => {
|
|
165
|
+
this.saveState = 'error'
|
|
166
|
+
this.saveError = e instanceof Error ? e.message : String(e)
|
|
167
|
+
})
|
|
168
|
+
return false
|
|
169
|
+
}
|
|
170
|
+
})
|
|
171
|
+
return this.saveChain
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
revertAll(): void {
|
|
175
|
+
for (const v of this.vars) v.revert()
|
|
176
|
+
}
|
|
177
|
+
}
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
// the run QUEUE + results. Clicking generate repeatedly enqueues; the drainer
|
|
2
|
+
// sends ONE prompt at a time so anything still pending can be dropped before it
|
|
3
|
+
// ever reaches the host (his ask: queued prompts must be clearable, singly or
|
|
4
|
+
// all). Progress + latent preview are polled ~700ms while a prompt is in flight
|
|
5
|
+
import { makeAutoObservable, observableShallow, runInAction } from 'mobx'
|
|
6
|
+
import { fetchRunStatus, postGenerate, type GenerateOk } from 'src/cli/serve/web/api.ts'
|
|
7
|
+
|
|
8
|
+
export type RunResult = GenerateOk & { at: string }
|
|
9
|
+
|
|
10
|
+
/** one enqueued prompt. `sent` marks the one on the host — that one is past cancelling */
|
|
11
|
+
export type QueueEntry = {
|
|
12
|
+
id: string
|
|
13
|
+
module: string
|
|
14
|
+
draft: string
|
|
15
|
+
payload: Record<string, unknown>
|
|
16
|
+
sent: boolean
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function sleep(ms: number): Promise<void> {
|
|
20
|
+
return new Promise((resolve) => setTimeout(resolve, ms))
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export class RunSt {
|
|
24
|
+
queue: QueueEntry[] = []
|
|
25
|
+
error: string | null = null
|
|
26
|
+
results: RunResult[] = []
|
|
27
|
+
progressPercent: number | null = null
|
|
28
|
+
hasPreview = false
|
|
29
|
+
/** the server's frame seq — the <img> cache-buster, changes only on a NEW frame */
|
|
30
|
+
previewTick = 0
|
|
31
|
+
private draining = false
|
|
32
|
+
private nextId = 0
|
|
33
|
+
/** staleness guard for the poll loop (mobx-mechanics: counters, not flags) */
|
|
34
|
+
private pollGen = 0
|
|
35
|
+
|
|
36
|
+
constructor() {
|
|
37
|
+
makeAutoObservable(this, { queue: observableShallow, results: observableShallow })
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
get isRunning(): boolean {
|
|
41
|
+
return this.queue.some((e) => e.sent)
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** the module whose prompt is on the host — the running card must not follow the selection */
|
|
45
|
+
get runningModule(): string | null {
|
|
46
|
+
return this.queue.find((e) => e.sent)?.module ?? null
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
get pendingCount(): number {
|
|
50
|
+
return this.queue.filter((e) => !e.sent).length
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
enqueue(p: { module: string; draft: string; payload: Record<string, unknown> }): void {
|
|
54
|
+
this.queue = [...this.queue, { id: `q${this.nextId++}`, ...p, sent: false }]
|
|
55
|
+
this.error = null
|
|
56
|
+
if (!this.draining) void this.drain()
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** drop a prompt that has not been sent yet; the one in flight is past cancelling */
|
|
60
|
+
removeQueued(id: string): void {
|
|
61
|
+
this.queue = this.queue.filter((e) => e.id !== id || e.sent)
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
clearQueue(): void {
|
|
65
|
+
this.queue = this.queue.filter((e) => e.sent)
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
remove(promptId: string): void {
|
|
69
|
+
this.results = this.results.filter((r) => r.promptId !== promptId)
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
clear(): void {
|
|
73
|
+
this.results = []
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** one prompt at a time: a queued entry stays cancellable until its turn comes */
|
|
77
|
+
private async drain(): Promise<void> {
|
|
78
|
+
if (this.draining) return
|
|
79
|
+
this.draining = true
|
|
80
|
+
try {
|
|
81
|
+
while (true) {
|
|
82
|
+
const next = this.queue.find((e) => !e.sent)
|
|
83
|
+
if (next == null) return
|
|
84
|
+
runInAction(() => {
|
|
85
|
+
// replace-by-copy keeps the one array idiom of this store
|
|
86
|
+
this.queue = this.queue.map((e) => (e.id === next.id ? { ...e, sent: true } : e))
|
|
87
|
+
this.progressPercent = null
|
|
88
|
+
this.hasPreview = false
|
|
89
|
+
})
|
|
90
|
+
const gen = ++this.pollGen
|
|
91
|
+
void this.poll(next.module, gen)
|
|
92
|
+
try {
|
|
93
|
+
const result = await postGenerate(next)
|
|
94
|
+
runInAction(() => {
|
|
95
|
+
// capped: full-size <img>s per run would eat the tab
|
|
96
|
+
this.results = [{ ...result, at: new Date().toLocaleTimeString() }, ...this.results].slice(0, 20)
|
|
97
|
+
})
|
|
98
|
+
} catch (e) {
|
|
99
|
+
runInAction(() => {
|
|
100
|
+
this.error = e instanceof Error ? e.message : String(e)
|
|
101
|
+
// a failed prompt must not drag its queue down with it: drop the rest,
|
|
102
|
+
// loud in the run bar, rather than replay N doomed prompts
|
|
103
|
+
this.queue = this.queue.filter((entry) => entry.sent && entry.id !== next.id)
|
|
104
|
+
})
|
|
105
|
+
} finally {
|
|
106
|
+
this.pollGen++
|
|
107
|
+
runInAction(() => {
|
|
108
|
+
this.queue = this.queue.filter((e) => e.id !== next.id)
|
|
109
|
+
this.progressPercent = null
|
|
110
|
+
this.hasPreview = false
|
|
111
|
+
})
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
} finally {
|
|
115
|
+
this.draining = false
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
private async poll(module: string, gen: number): Promise<void> {
|
|
120
|
+
while (gen === this.pollGen) {
|
|
121
|
+
await sleep(700)
|
|
122
|
+
if (gen !== this.pollGen) return
|
|
123
|
+
try {
|
|
124
|
+
const status = await fetchRunStatus({ module })
|
|
125
|
+
runInAction(() => {
|
|
126
|
+
if (gen !== this.pollGen) return
|
|
127
|
+
this.progressPercent = status.percent
|
|
128
|
+
this.hasPreview = status.hasPreview
|
|
129
|
+
this.previewTick = status.previewSeq ?? 0
|
|
130
|
+
})
|
|
131
|
+
} catch {
|
|
132
|
+
// a missed poll is not an incident; the POST's own error is the loud path
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
}
|
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
// ROOT state tree of the serve web ui (app-state-tree doctrine: one root,
|
|
2
|
+
// child stores hang off it, components read and call)
|
|
3
|
+
import { makeAutoObservable, observableRef, observableShallow, runInAction } from 'mobx'
|
|
4
|
+
import { fetchDraftValues, fetchIndex, saveDraft, type ModuleDescription } from 'src/cli/serve/web/api.ts'
|
|
5
|
+
import { EnhancerSt } from 'src/cli/serve/web/state/EnhancerSt.ts'
|
|
6
|
+
import { FormSt } from 'src/cli/serve/web/state/FormSt.ts'
|
|
7
|
+
import { RunSt } from 'src/cli/serve/web/state/RunSt.ts'
|
|
8
|
+
|
|
9
|
+
/** selection + drawer survive a reload (his standing default: hand-tuned state persists and restores) */
|
|
10
|
+
const STORAGE_KEY = 'comfy-ts-serve-ui'
|
|
11
|
+
|
|
12
|
+
type StoredSelection = {
|
|
13
|
+
module?: string
|
|
14
|
+
draft?: string
|
|
15
|
+
sidebar?: boolean
|
|
16
|
+
loraImages?: boolean
|
|
17
|
+
loraTitles?: boolean
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function readStoredSelection(): StoredSelection {
|
|
21
|
+
try {
|
|
22
|
+
return (JSON.parse(localStorage.getItem(STORAGE_KEY) ?? '{}') ?? {}) as StoredSelection
|
|
23
|
+
} catch {
|
|
24
|
+
return {}
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function isNarrowScreen(): boolean {
|
|
29
|
+
return window.matchMedia('(max-width: 800px)').matches
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export class WebSt {
|
|
33
|
+
phase: 'loading' | 'error' | 'ready' = 'loading'
|
|
34
|
+
bootError = ''
|
|
35
|
+
modules: ModuleDescription[] = []
|
|
36
|
+
loadErrors: Record<string, string> = {}
|
|
37
|
+
form: FormSt | null = null
|
|
38
|
+
formLoading = false
|
|
39
|
+
formError: string | null = null
|
|
40
|
+
/** drawer on narrow screens, collapsible column on wide ones — stored toggle wins over the width default */
|
|
41
|
+
sidebarOpen: boolean
|
|
42
|
+
/** lora image/title visibility, EVERY lora surface (row cards + popup) — the hide toggles are NSFW screens */
|
|
43
|
+
showLoraImages: boolean
|
|
44
|
+
showLoraTitles: boolean
|
|
45
|
+
run: RunSt
|
|
46
|
+
/** prompt refiner: own store, own localStorage blob (the openrouter key never leaves the browser) */
|
|
47
|
+
enhancer: EnhancerSt
|
|
48
|
+
|
|
49
|
+
constructor() {
|
|
50
|
+
this.run = new RunSt()
|
|
51
|
+
this.enhancer = new EnhancerSt()
|
|
52
|
+
const stored = readStoredSelection()
|
|
53
|
+
this.sidebarOpen = stored.sidebar ?? !isNarrowScreen()
|
|
54
|
+
this.showLoraImages = stored.loraImages ?? true
|
|
55
|
+
this.showLoraTitles = stored.loraTitles ?? true
|
|
56
|
+
makeAutoObservable(this, { run: false, enhancer: false, form: observableRef, modules: observableShallow })
|
|
57
|
+
// a closing/hidden tab must not lose an edit still inside the autosave debounce
|
|
58
|
+
window.addEventListener('beforeunload', () => this.form?.flushKeepalive())
|
|
59
|
+
document.addEventListener('visibilitychange', () => {
|
|
60
|
+
if (document.visibilityState === 'hidden') this.form?.flushKeepalive()
|
|
61
|
+
})
|
|
62
|
+
void this.boot()
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
toggleSidebar(): void {
|
|
66
|
+
this.sidebarOpen = !this.sidebarOpen
|
|
67
|
+
this.persist()
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
toggleLoraImages(): void {
|
|
71
|
+
this.showLoraImages = !this.showLoraImages
|
|
72
|
+
this.persist()
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
toggleLoraTitles(): void {
|
|
76
|
+
this.showLoraTitles = !this.showLoraTitles
|
|
77
|
+
this.persist()
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
private persist(): void {
|
|
81
|
+
try {
|
|
82
|
+
// merge, never rebuild: toggling the sidebar before a draft loads must not
|
|
83
|
+
// erase the stored selection (this.form is null then)
|
|
84
|
+
const stored = readStoredSelection()
|
|
85
|
+
localStorage.setItem(
|
|
86
|
+
STORAGE_KEY,
|
|
87
|
+
JSON.stringify({
|
|
88
|
+
...stored,
|
|
89
|
+
...(this.form != null ? { module: this.form.moduleKey, draft: this.form.draft } : {}),
|
|
90
|
+
sidebar: this.sidebarOpen,
|
|
91
|
+
loraImages: this.showLoraImages,
|
|
92
|
+
loraTitles: this.showLoraTitles,
|
|
93
|
+
}),
|
|
94
|
+
)
|
|
95
|
+
} catch {
|
|
96
|
+
// storage full/blocked: state just won't survive the reload
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
moduleByKey(key: string): ModuleDescription | null {
|
|
101
|
+
return this.modules.find((m) => m.module === key) ?? null
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
async boot(): Promise<void> {
|
|
105
|
+
try {
|
|
106
|
+
const index = await fetchIndex()
|
|
107
|
+
runInAction(() => {
|
|
108
|
+
this.modules = index.workflows
|
|
109
|
+
this.loadErrors = index.loadErrors ?? {}
|
|
110
|
+
this.phase = 'ready'
|
|
111
|
+
})
|
|
112
|
+
const stored = readStoredSelection()
|
|
113
|
+
const mod = (stored.module != null ? this.moduleByKey(stored.module) : null) ?? this.modules[0]
|
|
114
|
+
if (mod == null) return
|
|
115
|
+
const draft = stored.draft != null && mod.drafts.includes(stored.draft) ? stored.draft : 'default'
|
|
116
|
+
await this.select({ module: mod.module, draft })
|
|
117
|
+
} catch (e) {
|
|
118
|
+
runInAction(() => {
|
|
119
|
+
this.phase = 'error'
|
|
120
|
+
this.bootError = e instanceof Error ? e.message : String(e)
|
|
121
|
+
})
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/** latest-wins guard for rapid draft clicks: only the newest select may write form state */
|
|
126
|
+
private selectToken = 0
|
|
127
|
+
|
|
128
|
+
async select(p: { module: string; draft: string }): Promise<void> {
|
|
129
|
+
const mod = this.moduleByKey(p.module)
|
|
130
|
+
if (mod == null) return
|
|
131
|
+
// re-picking the draft you are IN is a no-op, not a reload: a pending autosave would
|
|
132
|
+
// lose the race against the fetch and the typed edit would vanish from the form
|
|
133
|
+
if (this.form?.moduleKey === p.module && this.form.draft === p.draft) {
|
|
134
|
+
runInAction(() => {
|
|
135
|
+
if (isNarrowScreen()) this.sidebarOpen = false
|
|
136
|
+
})
|
|
137
|
+
return
|
|
138
|
+
}
|
|
139
|
+
const token = ++this.selectToken
|
|
140
|
+
runInAction(() => {
|
|
141
|
+
// the refiner points at a var of the OUTGOING form: applying it after the swap
|
|
142
|
+
// would write into a discarded object, so the modal closes with its form
|
|
143
|
+
this.enhancer.close()
|
|
144
|
+
this.formLoading = true
|
|
145
|
+
this.formError = null
|
|
146
|
+
// picking a draft is the drawer's exit on a phone
|
|
147
|
+
if (isNarrowScreen()) this.sidebarOpen = false
|
|
148
|
+
})
|
|
149
|
+
// flush the outgoing form BEFORE reading the next one, so a switch never races its own save
|
|
150
|
+
const outgoing = this.form
|
|
151
|
+
if (outgoing != null) {
|
|
152
|
+
outgoing.dispose()
|
|
153
|
+
await outgoing.save()
|
|
154
|
+
}
|
|
155
|
+
try {
|
|
156
|
+
const reply = await fetchDraftValues(p)
|
|
157
|
+
runInAction(() => {
|
|
158
|
+
if (token !== this.selectToken) return
|
|
159
|
+
this.form = new FormSt(p.module, p.draft, mod, reply.values ?? {})
|
|
160
|
+
this.persist()
|
|
161
|
+
})
|
|
162
|
+
} catch (e) {
|
|
163
|
+
runInAction(() => {
|
|
164
|
+
if (token !== this.selectToken) return
|
|
165
|
+
this.formError = e instanceof Error ? e.message : String(e)
|
|
166
|
+
})
|
|
167
|
+
} finally {
|
|
168
|
+
runInAction(() => {
|
|
169
|
+
if (token === this.selectToken) this.formLoading = false
|
|
170
|
+
})
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
generate(): void {
|
|
175
|
+
void this.generateNow()
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/** flush the autosave first: the server reads the draft it just wrote — one source of truth.
|
|
179
|
+
* Every click ENQUEUES, so hitting generate n times runs n prompts; the queued payload
|
|
180
|
+
* freezes the values you saw, and seeds stay on the draft's server-side policy */
|
|
181
|
+
private async generateNow(): Promise<void> {
|
|
182
|
+
const form = this.form
|
|
183
|
+
if (form == null) return
|
|
184
|
+
const payload = form.queuePayload()
|
|
185
|
+
const saved = await form.save()
|
|
186
|
+
// the header already shows the loud save error; running the stale draft would lie
|
|
187
|
+
if (!saved) return
|
|
188
|
+
this.run.enqueue({ module: form.moduleKey, draft: form.draft, payload })
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/** duplicate = save the current values under a new name, then switch to it */
|
|
192
|
+
async duplicateDraft(rawName: string): Promise<void> {
|
|
193
|
+
const form = this.form
|
|
194
|
+
const name = rawName.trim()
|
|
195
|
+
if (form == null || name === '') return
|
|
196
|
+
const existing = this.moduleByKey(form.moduleKey)?.drafts.includes(name) === true
|
|
197
|
+
if (existing && !window.confirm(`draft '${name}' already exists — overwrite it?`)) return
|
|
198
|
+
try {
|
|
199
|
+
const reply = await saveDraft({ module: form.moduleKey, draft: name, values: form.valuesJSON() })
|
|
200
|
+
runInAction(() => {
|
|
201
|
+
this.modules = this.modules.map((m) => (m.module === form.moduleKey ? { ...m, drafts: reply.drafts } : m))
|
|
202
|
+
})
|
|
203
|
+
await this.select({ module: form.moduleKey, draft: name })
|
|
204
|
+
} catch (e) {
|
|
205
|
+
runInAction(() => {
|
|
206
|
+
this.formError = e instanceof Error ? e.message : String(e)
|
|
207
|
+
})
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
// PURE value normalization for the web form. DOM-free on purpose:
|
|
2
|
+
// headless-tested by tests/serve-web-payload.test.ts. The form does NOT build
|
|
3
|
+
// override payloads any more: drafts are live (autosave through PUT, generate
|
|
4
|
+
// posts {}) — architecture item 12, web ui
|
|
5
|
+
import type { VarDescriptor } from 'src/cli/serve/describeVar.ts'
|
|
6
|
+
|
|
7
|
+
export type SeedFormValue = { mode: string; value: number }
|
|
8
|
+
export type SizeFormValue = { width: number; height: number }
|
|
9
|
+
|
|
10
|
+
/** what a QUEUED run carries: the values you saw when you clicked, so editing the form
|
|
11
|
+
* afterwards cannot change a prompt already in the queue. SEEDS are deliberately left out —
|
|
12
|
+
* they stay the draft's server-side policy, so a queue of 4 under `?`/`+` advances per run
|
|
13
|
+
* instead of repeating one frozen number (an explicit payload seed is fixed by definition) */
|
|
14
|
+
export function payloadSnapshot(entries: { name: string; kind: string; value: unknown }[]): Record<string, unknown> {
|
|
15
|
+
const out: Record<string, unknown> = {}
|
|
16
|
+
for (const e of entries) if (e.kind !== 'seed') out[e.name] = e.value
|
|
17
|
+
return out
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** draft seed values come as {mode,value} (toJSON) or a legacy plain number */
|
|
21
|
+
export function asSeedForm(raw: unknown): SeedFormValue {
|
|
22
|
+
if (typeof raw === 'number' && Number.isFinite(raw)) return { mode: '=', value: raw }
|
|
23
|
+
if (raw != null && typeof raw === 'object') {
|
|
24
|
+
const o = raw as { mode?: unknown; value?: unknown }
|
|
25
|
+
return {
|
|
26
|
+
mode: typeof o.mode === 'string' ? o.mode : '=',
|
|
27
|
+
value: typeof o.value === 'number' && Number.isFinite(o.value) ? o.value : 0,
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
return { mode: '=', value: 0 }
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function asSizeForm(raw: unknown): SizeFormValue {
|
|
34
|
+
if (raw != null && typeof raw === 'object') {
|
|
35
|
+
const o = raw as { width?: unknown; height?: unknown }
|
|
36
|
+
if (typeof o.width === 'number' && typeof o.height === 'number') return { width: o.width, height: o.height }
|
|
37
|
+
}
|
|
38
|
+
return { width: 1024, height: 1024 }
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** the record the web keeps holds ON loras ONLY, so it always reads as "what is in the
|
|
42
|
+
* palette". Two things are dropped: keys the host no longer offers (dead weight the ui
|
|
43
|
+
* cannot display, and a server-side build failure), and `false` entries — LorasVar writes
|
|
44
|
+
* one for every lora ever unticked, so treating them as palette members put the WHOLE
|
|
45
|
+
* catalog in the row. Off is off: `activeLoras` skips both spellings, so nothing changes
|
|
46
|
+
* in the graph, and with live drafts the next autosave heals the file */
|
|
47
|
+
export function pruneLorasRecord(raw: unknown, options: readonly string[]): Record<string, unknown> {
|
|
48
|
+
if (raw == null || typeof raw !== 'object' || Array.isArray(raw)) return {}
|
|
49
|
+
const known = new Set(options)
|
|
50
|
+
const out: Record<string, unknown> = {}
|
|
51
|
+
for (const [k, st] of Object.entries(raw as Record<string, unknown>)) if (known.has(k) && loraIsOn(st)) out[k] = st
|
|
52
|
+
return out
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** what a fresh VarSt starts from: the draft value when present, else the descriptor default — seed/size/loras normalized so controls never branch on shape */
|
|
56
|
+
export function normalizeInitial(desc: VarDescriptor, raw: unknown): unknown {
|
|
57
|
+
if (desc.kind === 'seed') return asSeedForm(raw ?? desc.default)
|
|
58
|
+
if (desc.kind === 'size') return asSizeForm(raw ?? desc.default)
|
|
59
|
+
if (desc.kind === 'loras') return pruneLorasRecord(raw ?? desc.default, desc.options ?? [])
|
|
60
|
+
return raw ?? desc.default
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export function randomSeed(): number {
|
|
64
|
+
return Math.floor(Math.random() * 2 ** 32)
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// ---- loras record transitions (LorasVar's semantics, mirrored for the web) ----
|
|
68
|
+
// key absent = not selected · false = SELECTED but off (stays in the ui) ·
|
|
69
|
+
// number | [model, clip] | true = on. LorasVar keeps the same `prev` memory so
|
|
70
|
+
// off → on restores the strength instead of snapping to 1
|
|
71
|
+
|
|
72
|
+
export type LoraStrengthPair = { model: number; clip: number }
|
|
73
|
+
|
|
74
|
+
export function loraIsOn(st: unknown): boolean {
|
|
75
|
+
return st != null && st !== false
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** any stored strength shape → the {model, clip} pair the inputs edit */
|
|
79
|
+
export function loraStrengthPair(st: unknown): LoraStrengthPair {
|
|
80
|
+
if (typeof st === 'number' && Number.isFinite(st)) return { model: st, clip: st }
|
|
81
|
+
if (Array.isArray(st) && typeof st[0] === 'number' && typeof st[1] === 'number') return { model: st[0], clip: st[1] }
|
|
82
|
+
return { model: 1, clip: 1 }
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** pause/resume. A PAUSE removes the lora from the record entirely — the palette membership
|
|
86
|
+
* of a paused lora lives in the ui, not in the draft, so a draft never accumulates the
|
|
87
|
+
* off-entries that flooded the palette. Resuming writes the remembered strength back */
|
|
88
|
+
export function setLoraEnabled(
|
|
89
|
+
record: Record<string, unknown>,
|
|
90
|
+
name: string,
|
|
91
|
+
on: boolean,
|
|
92
|
+
prev?: LoraStrengthPair,
|
|
93
|
+
): Record<string, unknown> {
|
|
94
|
+
const next = { ...record }
|
|
95
|
+
if (!on) delete next[name]
|
|
96
|
+
else {
|
|
97
|
+
const pair = prev ?? loraStrengthPair(record[name])
|
|
98
|
+
next[name] = [pair.model, pair.clip]
|
|
99
|
+
}
|
|
100
|
+
return next
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** set both strengths; an OFF lora stays off (its strength is edited on re-enable) */
|
|
104
|
+
export function setLoraStrength(
|
|
105
|
+
record: Record<string, unknown>,
|
|
106
|
+
name: string,
|
|
107
|
+
pair: LoraStrengthPair,
|
|
108
|
+
): Record<string, unknown> {
|
|
109
|
+
return { ...record, [name]: [pair.model, pair.clip] }
|
|
110
|
+
}
|