comfy-ts 2.5.0 → 2.7.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 +15 -2
- 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 +5 -5
- package/dist/cli.js.map +1 -1
- package/dist/index.cjs +7 -2
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +24 -18
- package/dist/index.d.ts +24 -18
- package/dist/index.js +7 -2
- package/dist/index.js.map +1 -1
- package/dist/{run-serve-D47Zas-a.js → run-serve-D1Tsuru9.js} +494 -20
- package/dist/run-serve-D1Tsuru9.js.map +1 -0
- package/dist/{run-tui-t2TRBN0H.js → run-tui-JxlEbb_f.js} +511 -331
- package/dist/run-tui-JxlEbb_f.js.map +1 -0
- package/dist/serve-web.js +273 -0
- package/dist/{state-DY3Mp9Cl.js → state-CaHeDL-0.js} +7 -13
- package/dist/state-CaHeDL-0.js.map +1 -0
- package/dist/web.d.ts +24 -18
- package/dist/web.js +7 -2
- package/dist/web.js.map +1 -1
- package/examples/web/tsconfig.json +4 -1
- package/package.json +7 -5
- package/src/cli/comfy-ts-cli.ts +1 -1
- package/src/cli/serve/ServeApp.ts +383 -14
- package/src/cli/serve/describeVar.ts +6 -1
- package/src/cli/serve/promptEnhancers.ts +69 -0
- package/src/cli/serve/run-serve.ts +41 -7
- package/src/cli/serve/safeName.ts +12 -0
- package/src/cli/serve/web/api.ts +128 -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 +254 -0
- package/src/cli/serve/web/components/Sidebar.tsx +42 -0
- package/src/cli/serve/web/components/VarsForm.tsx +184 -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/llm.ts +254 -0
- package/src/cli/serve/web/main.tsx +18 -0
- package/src/cli/serve/web/state/EnhancerSt.ts +481 -0
- package/src/cli/serve/web/state/FormSt.ts +180 -0
- package/src/cli/serve/web/state/RunSt.ts +136 -0
- package/src/cli/serve/web/state/WebSt.ts +235 -0
- package/src/cli/serve/web/state/payload.ts +110 -0
- package/src/cli/serve/web/styles.ts +261 -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/state.ts +6 -0
- 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-t2TRBN0H.js.map +0 -1
- package/dist/state-DY3Mp9Cl.js.map +0 -1
|
@@ -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,235 @@
|
|
|
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 { deleteDraft, 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
|
+
/** delete the draft FILE, then fall back to another draft (DraftsSt.deleteDraft's rule).
|
|
192
|
+
* ORDER: the form is dropped WITHOUT flushing FIRST — its autosave would otherwise
|
|
193
|
+
* re-create the file the server is about to delete */
|
|
194
|
+
async deleteDraft(p: { module: string; draft: string }): Promise<void> {
|
|
195
|
+
const form = this.form
|
|
196
|
+
if (form != null && form.moduleKey === p.module && form.draft === p.draft) {
|
|
197
|
+
form.dispose({ flush: false })
|
|
198
|
+
runInAction(() => {
|
|
199
|
+
this.form = null
|
|
200
|
+
})
|
|
201
|
+
}
|
|
202
|
+
try {
|
|
203
|
+
const reply = await deleteDraft(p)
|
|
204
|
+
runInAction(() => {
|
|
205
|
+
this.modules = this.modules.map((m) => (m.module === p.module ? { ...m, drafts: reply.drafts } : m))
|
|
206
|
+
})
|
|
207
|
+
const fallback = reply.drafts.includes('default') ? 'default' : (reply.drafts[0] ?? 'default')
|
|
208
|
+
await this.select({ module: p.module, draft: fallback })
|
|
209
|
+
} catch (e) {
|
|
210
|
+
runInAction(() => {
|
|
211
|
+
this.formError = e instanceof Error ? e.message : String(e)
|
|
212
|
+
})
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/** duplicate = save the current values under a new name, then switch to it */
|
|
217
|
+
async duplicateDraft(rawName: string): Promise<void> {
|
|
218
|
+
const form = this.form
|
|
219
|
+
const name = rawName.trim()
|
|
220
|
+
if (form == null || name === '') return
|
|
221
|
+
const existing = this.moduleByKey(form.moduleKey)?.drafts.includes(name) === true
|
|
222
|
+
if (existing && !window.confirm(`draft '${name}' already exists — overwrite it?`)) return
|
|
223
|
+
try {
|
|
224
|
+
const reply = await saveDraft({ module: form.moduleKey, draft: name, values: form.valuesJSON() })
|
|
225
|
+
runInAction(() => {
|
|
226
|
+
this.modules = this.modules.map((m) => (m.module === form.moduleKey ? { ...m, drafts: reply.drafts } : m))
|
|
227
|
+
})
|
|
228
|
+
await this.select({ module: form.moduleKey, draft: name })
|
|
229
|
+
} catch (e) {
|
|
230
|
+
runInAction(() => {
|
|
231
|
+
this.formError = e instanceof Error ? e.message : String(e)
|
|
232
|
+
})
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
}
|
|
@@ -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
|
+
}
|