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,481 @@
|
|
|
1
|
+
// the prompt enhancer: provider settings + the master-prompt library + one refine
|
|
2
|
+
// run (architecture item 12, web ui). Hangs off the WebSt root.
|
|
3
|
+
// SPLIT OF TRUTH: the master prompts are FILES on the server
|
|
4
|
+
// (`.comfy-ts/prompt-enhancers/*.md`, autosaved like a draft), everything else is
|
|
5
|
+
// browser-local in a localStorage blob — keys included, so the serve process never
|
|
6
|
+
// holds a credential. normalizeSettings/nextPresetName are PURE and headless-tested.
|
|
7
|
+
import { makeAutoObservable, reaction, runInAction, type IReactionDisposer } from 'mobx'
|
|
8
|
+
import {
|
|
9
|
+
deletePromptEnhancer,
|
|
10
|
+
fetchPromptEnhancers,
|
|
11
|
+
savePromptEnhancer,
|
|
12
|
+
type PromptEnhancer,
|
|
13
|
+
} from 'src/cli/serve/web/api.ts'
|
|
14
|
+
import {
|
|
15
|
+
defaultBaseUrl,
|
|
16
|
+
fetchModels,
|
|
17
|
+
streamRefine,
|
|
18
|
+
type LlmModel,
|
|
19
|
+
type ProviderId,
|
|
20
|
+
type ReasoningEffort,
|
|
21
|
+
} from 'src/cli/serve/web/llm.ts'
|
|
22
|
+
import type { VarSt } from 'src/cli/serve/web/state/FormSt.ts'
|
|
23
|
+
|
|
24
|
+
const STORAGE_KEY = 'comfy-ts-serve-enhancer'
|
|
25
|
+
|
|
26
|
+
/** explicit model ids, never an alias: behaviour and cost stay reproducible */
|
|
27
|
+
const DEFAULT_MODEL: Record<ProviderId, string> = {
|
|
28
|
+
openrouter: 'anthropic/claude-sonnet-5',
|
|
29
|
+
openwebui: '',
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export const PROVIDERS: ProviderId[] = ['openrouter', 'openwebui']
|
|
33
|
+
|
|
34
|
+
export type EnhancerSettings = {
|
|
35
|
+
provider: ProviderId
|
|
36
|
+
/** per provider, so switching one cannot send an openrouter model id to a local box */
|
|
37
|
+
keyByProvider: Record<string, string>
|
|
38
|
+
baseUrlByProvider: Record<string, string>
|
|
39
|
+
modelByProvider: Record<string, string>
|
|
40
|
+
effort: ReasoningEffort
|
|
41
|
+
thinkingOnly: boolean
|
|
42
|
+
/** selected master prompt, by FILE NAME */
|
|
43
|
+
presetName: string
|
|
44
|
+
/** last preset used per module: a workflow reopens on its own refiner */
|
|
45
|
+
presetByModule: Record<string, string>
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function isProvider(raw: unknown): raw is ProviderId {
|
|
49
|
+
return raw === 'openrouter' || raw === 'openwebui'
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function isEffort(raw: unknown): raw is ReasoningEffort {
|
|
53
|
+
return raw === 'off' || raw === 'low' || raw === 'medium' || raw === 'high'
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function strMap(raw: unknown): Record<string, string> {
|
|
57
|
+
const out: Record<string, string> = {}
|
|
58
|
+
if (typeof raw === 'object' && raw !== null) {
|
|
59
|
+
for (const [k, v] of Object.entries(raw)) if (typeof v === 'string') out[k] = v
|
|
60
|
+
}
|
|
61
|
+
return out
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** stored blob → usable settings. A hand-edited or half-written localStorage entry must
|
|
65
|
+
* degrade to defaults, never break the modal (same wire tolerance as every stored blob) */
|
|
66
|
+
export function normalizeSettings(raw: unknown): EnhancerSettings {
|
|
67
|
+
const o = typeof raw === 'object' && raw !== null ? (raw as Record<string, unknown>) : {}
|
|
68
|
+
const models = strMap(o.modelByProvider)
|
|
69
|
+
const bases = strMap(o.baseUrlByProvider)
|
|
70
|
+
return {
|
|
71
|
+
provider: isProvider(o.provider) ? o.provider : 'openrouter',
|
|
72
|
+
keyByProvider: strMap(o.keyByProvider),
|
|
73
|
+
baseUrlByProvider: { openrouter: defaultBaseUrl('openrouter'), openwebui: defaultBaseUrl('openwebui'), ...bases },
|
|
74
|
+
modelByProvider: { ...DEFAULT_MODEL, ...models },
|
|
75
|
+
effort: isEffort(o.effort) ? o.effort : 'medium',
|
|
76
|
+
thinkingOnly: o.thinkingOnly !== false,
|
|
77
|
+
presetName: typeof o.presetName === 'string' ? o.presetName : '',
|
|
78
|
+
presetByModule: strMap(o.presetByModule),
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** unique name for a new/duplicated master prompt — the name IS the filename */
|
|
83
|
+
export function nextPresetName(base: string, taken: readonly string[]): string {
|
|
84
|
+
if (!taken.includes(base)) return base
|
|
85
|
+
for (let i = 2; i < 1000; i++) {
|
|
86
|
+
const candidate = `${base} ${i}`
|
|
87
|
+
if (!taken.includes(candidate)) return candidate
|
|
88
|
+
}
|
|
89
|
+
return `${base} ${taken.length + 1}`
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function readStored(): EnhancerSettings {
|
|
93
|
+
try {
|
|
94
|
+
return normalizeSettings(JSON.parse(localStorage.getItem(STORAGE_KEY) ?? '{}'))
|
|
95
|
+
} catch {
|
|
96
|
+
return normalizeSettings({})
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export class EnhancerSt {
|
|
101
|
+
provider: ProviderId
|
|
102
|
+
keyByProvider: Record<string, string>
|
|
103
|
+
baseUrlByProvider: Record<string, string>
|
|
104
|
+
modelByProvider: Record<string, string>
|
|
105
|
+
effort: ReasoningEffort
|
|
106
|
+
thinkingOnly: boolean
|
|
107
|
+
presetName: string
|
|
108
|
+
presetByModule: Record<string, string>
|
|
109
|
+
|
|
110
|
+
/** the master prompts, mirrored from the server (files) */
|
|
111
|
+
presets: PromptEnhancer[] = []
|
|
112
|
+
presetsState: 'idle' | 'loading' | 'error' = 'idle'
|
|
113
|
+
presetsError = ''
|
|
114
|
+
saveState: 'saved' | 'saving' | 'error' = 'saved'
|
|
115
|
+
saveError = ''
|
|
116
|
+
|
|
117
|
+
models: LlmModel[] = []
|
|
118
|
+
modelsState: 'idle' | 'loading' | 'error' = 'idle'
|
|
119
|
+
modelsError = ''
|
|
120
|
+
|
|
121
|
+
/** the prompt var being refined — non-null IS the modal being open */
|
|
122
|
+
target: VarSt | null = null
|
|
123
|
+
targetModule = ''
|
|
124
|
+
original = ''
|
|
125
|
+
result = ''
|
|
126
|
+
thinking = ''
|
|
127
|
+
phase: 'idle' | 'running' | 'done' | 'error' = 'idle'
|
|
128
|
+
error = ''
|
|
129
|
+
private abort: AbortController | null = null
|
|
130
|
+
private disposer: IReactionDisposer
|
|
131
|
+
/** the preset json the server last confirmed — the autosave no-ops on it, so loading a
|
|
132
|
+
* library (or switching preset) never writes the files back unchanged */
|
|
133
|
+
private lastSaved = ''
|
|
134
|
+
|
|
135
|
+
constructor() {
|
|
136
|
+
const s = readStored()
|
|
137
|
+
this.provider = s.provider
|
|
138
|
+
this.keyByProvider = s.keyByProvider
|
|
139
|
+
this.baseUrlByProvider = s.baseUrlByProvider
|
|
140
|
+
this.modelByProvider = s.modelByProvider
|
|
141
|
+
this.effort = s.effort
|
|
142
|
+
this.thinkingOnly = s.thinkingOnly
|
|
143
|
+
this.presetName = s.presetName
|
|
144
|
+
this.presetByModule = s.presetByModule
|
|
145
|
+
makeAutoObservable<EnhancerSt, 'abort' | 'disposer' | 'lastSaved'>(this, {
|
|
146
|
+
abort: false,
|
|
147
|
+
disposer: false,
|
|
148
|
+
lastSaved: false,
|
|
149
|
+
})
|
|
150
|
+
// the master prompt autosaves to its file, the live-drafts model (the values json is
|
|
151
|
+
// change-detector AND payload — the house persistence idiom)
|
|
152
|
+
this.disposer = reaction(
|
|
153
|
+
() => {
|
|
154
|
+
const p = this.preset
|
|
155
|
+
return p == null ? null : JSON.stringify(p)
|
|
156
|
+
},
|
|
157
|
+
(encoded) => {
|
|
158
|
+
if (encoded == null || encoded === this.lastSaved) return
|
|
159
|
+
void this.savePresetNow(JSON.parse(encoded) as PromptEnhancer)
|
|
160
|
+
},
|
|
161
|
+
{ delay: 600 },
|
|
162
|
+
)
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
dispose(): void {
|
|
166
|
+
this.disposer()
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// #region settings ---------------------------------------------------------
|
|
170
|
+
get apiKey(): string {
|
|
171
|
+
return this.keyByProvider[this.provider] ?? ''
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
get baseUrl(): string {
|
|
175
|
+
return this.baseUrlByProvider[this.provider] ?? defaultBaseUrl(this.provider)
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
get model(): string {
|
|
179
|
+
return this.modelByProvider[this.provider] ?? ''
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
get endpoint(): { provider: ProviderId; baseUrl: string; key: string } {
|
|
183
|
+
return { provider: this.provider, baseUrl: this.baseUrl, key: this.apiKey }
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/** the list the selector shows. A model whose capability is UNKNOWN (open webui reports
|
|
187
|
+
* none) stays visible: only an explicit "no reasoning" is filtered out */
|
|
188
|
+
get visibleModels(): LlmModel[] {
|
|
189
|
+
return this.thinkingOnly ? this.models.filter((m) => m.reasoning !== false) : this.models
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
get preset(): PromptEnhancer | null {
|
|
193
|
+
return this.presets.find((p) => p.name === this.presetName) ?? this.presets[0] ?? null
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
get isOpen(): boolean {
|
|
197
|
+
return this.target != null
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
settingsJSON(): EnhancerSettings {
|
|
201
|
+
return {
|
|
202
|
+
provider: this.provider,
|
|
203
|
+
keyByProvider: this.keyByProvider,
|
|
204
|
+
baseUrlByProvider: this.baseUrlByProvider,
|
|
205
|
+
modelByProvider: this.modelByProvider,
|
|
206
|
+
effort: this.effort,
|
|
207
|
+
thinkingOnly: this.thinkingOnly,
|
|
208
|
+
presetName: this.presetName,
|
|
209
|
+
presetByModule: this.presetByModule,
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
private persist(): void {
|
|
214
|
+
try {
|
|
215
|
+
localStorage.setItem(STORAGE_KEY, JSON.stringify(this.settingsJSON()))
|
|
216
|
+
} catch {
|
|
217
|
+
// storage full/blocked: settings just won't survive the reload
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
setProvider(v: string): void {
|
|
222
|
+
if (!isProvider(v)) return
|
|
223
|
+
this.provider = v
|
|
224
|
+
// the loaded list belongs to the OTHER provider: showing it would offer models this
|
|
225
|
+
// endpoint has never heard of
|
|
226
|
+
this.models = []
|
|
227
|
+
this.modelsState = 'idle'
|
|
228
|
+
this.modelsError = ''
|
|
229
|
+
this.persist()
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
setApiKey(v: string): void {
|
|
233
|
+
this.keyByProvider[this.provider] = v.trim()
|
|
234
|
+
this.persist()
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
setBaseUrl(v: string): void {
|
|
238
|
+
this.baseUrlByProvider[this.provider] = v.trim()
|
|
239
|
+
this.persist()
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
setModel(v: string): void {
|
|
243
|
+
this.modelByProvider[this.provider] = v
|
|
244
|
+
this.persist()
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
setEffort(v: string): void {
|
|
248
|
+
if (!isEffort(v)) return
|
|
249
|
+
this.effort = v
|
|
250
|
+
this.persist()
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
toggleThinkingOnly(): void {
|
|
254
|
+
this.thinkingOnly = !this.thinkingOnly
|
|
255
|
+
this.persist()
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
// #region master prompts (server files) -------------------------------------
|
|
259
|
+
async loadPresets(): Promise<void> {
|
|
260
|
+
runInAction(() => {
|
|
261
|
+
this.presetsState = 'loading'
|
|
262
|
+
this.presetsError = ''
|
|
263
|
+
})
|
|
264
|
+
try {
|
|
265
|
+
const reply = await fetchPromptEnhancers()
|
|
266
|
+
runInAction(() => {
|
|
267
|
+
this.presets = reply.enhancers
|
|
268
|
+
this.presetsState = 'idle'
|
|
269
|
+
if (!this.presets.some((p) => p.name === this.presetName)) this.presetName = this.presets[0]?.name ?? ''
|
|
270
|
+
this.lastSaved = JSON.stringify(this.preset)
|
|
271
|
+
})
|
|
272
|
+
} catch (e) {
|
|
273
|
+
runInAction(() => {
|
|
274
|
+
this.presetsState = 'error'
|
|
275
|
+
this.presetsError = e instanceof Error ? e.message : String(e)
|
|
276
|
+
})
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
private async savePresetNow(entry: PromptEnhancer): Promise<void> {
|
|
281
|
+
runInAction(() => {
|
|
282
|
+
this.saveState = 'saving'
|
|
283
|
+
this.lastSaved = JSON.stringify(entry)
|
|
284
|
+
})
|
|
285
|
+
try {
|
|
286
|
+
const reply = await savePromptEnhancer(entry)
|
|
287
|
+
runInAction(() => {
|
|
288
|
+
this.presets = reply.enhancers
|
|
289
|
+
this.saveState = 'saved'
|
|
290
|
+
this.saveError = ''
|
|
291
|
+
})
|
|
292
|
+
} catch (e) {
|
|
293
|
+
runInAction(() => {
|
|
294
|
+
this.saveState = 'error'
|
|
295
|
+
this.saveError = e instanceof Error ? e.message : String(e)
|
|
296
|
+
})
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
selectPreset(name: string): void {
|
|
301
|
+
if (!this.presets.some((p) => p.name === name)) return
|
|
302
|
+
this.presetName = name
|
|
303
|
+
this.lastSaved = JSON.stringify(this.preset)
|
|
304
|
+
if (this.targetModule !== '') this.presetByModule[this.targetModule] = name
|
|
305
|
+
this.persist()
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
setPresetText(text: string): void {
|
|
309
|
+
const p = this.preset
|
|
310
|
+
if (p == null) return
|
|
311
|
+
// replace the ENTRY, so the autosave reaction sees a change (the array is observable)
|
|
312
|
+
this.presets = this.presets.map((x) => (x.name === p.name ? { ...x, text } : x))
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
/** create (or duplicate): a new FILE, saved immediately so it exists on disk */
|
|
316
|
+
addPreset(name: string, text = ''): void {
|
|
317
|
+
if (name.trim() === '') return
|
|
318
|
+
const clean = nextPresetName(
|
|
319
|
+
name.trim(),
|
|
320
|
+
this.presets.map((p) => p.name),
|
|
321
|
+
)
|
|
322
|
+
this.presets = [...this.presets, { name: clean, text }]
|
|
323
|
+
this.presetName = clean
|
|
324
|
+
this.persist()
|
|
325
|
+
void this.savePresetNow({ name: clean, text })
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
duplicatePreset(): void {
|
|
329
|
+
const p = this.preset
|
|
330
|
+
if (p != null) this.addPreset(`${p.name} copy`, p.text)
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
/** rename = write the new file, delete the old one (the filename IS the identity) */
|
|
334
|
+
async renamePreset(rawName: string): Promise<void> {
|
|
335
|
+
const p = this.preset
|
|
336
|
+
const clean = nextPresetName(
|
|
337
|
+
rawName.trim(),
|
|
338
|
+
this.presets.filter((x) => x.name !== p?.name).map((x) => x.name),
|
|
339
|
+
)
|
|
340
|
+
if (p == null || clean === '' || clean === p.name) return
|
|
341
|
+
await this.savePresetNow({ name: clean, text: p.text })
|
|
342
|
+
runInAction(() => {
|
|
343
|
+
this.presetName = clean
|
|
344
|
+
this.persist()
|
|
345
|
+
})
|
|
346
|
+
await this.removePresetFile(p.name)
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
async deletePreset(): Promise<void> {
|
|
350
|
+
const p = this.preset
|
|
351
|
+
if (p == null) return
|
|
352
|
+
await this.removePresetFile(p.name)
|
|
353
|
+
runInAction(() => {
|
|
354
|
+
// a deleted preset must not stay pinned to a module
|
|
355
|
+
for (const [k, v] of Object.entries(this.presetByModule)) if (v === p.name) delete this.presetByModule[k]
|
|
356
|
+
if (!this.presets.some((x) => x.name === this.presetName)) this.presetName = this.presets[0]?.name ?? ''
|
|
357
|
+
this.persist()
|
|
358
|
+
})
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
private async removePresetFile(name: string): Promise<void> {
|
|
362
|
+
try {
|
|
363
|
+
const reply = await deletePromptEnhancer({ name })
|
|
364
|
+
runInAction(() => {
|
|
365
|
+
this.presets = reply.enhancers
|
|
366
|
+
})
|
|
367
|
+
} catch (e) {
|
|
368
|
+
runInAction(() => {
|
|
369
|
+
this.saveState = 'error'
|
|
370
|
+
this.saveError = e instanceof Error ? e.message : String(e)
|
|
371
|
+
})
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
// #region the run ----------------------------------------------------------
|
|
376
|
+
openFor(p: { v: VarSt; module: string }): void {
|
|
377
|
+
this.target = p.v
|
|
378
|
+
this.targetModule = p.module
|
|
379
|
+
this.original = typeof p.v.value === 'string' ? p.v.value : ''
|
|
380
|
+
this.result = ''
|
|
381
|
+
this.thinking = ''
|
|
382
|
+
this.error = ''
|
|
383
|
+
this.phase = 'idle'
|
|
384
|
+
const remembered = this.presetByModule[p.module]
|
|
385
|
+
if (remembered != null) this.presetName = remembered
|
|
386
|
+
if (this.presets.length === 0) void this.loadPresets()
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
close(): void {
|
|
390
|
+
this.cancel()
|
|
391
|
+
this.target = null
|
|
392
|
+
this.targetModule = ''
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
setResult(v: string): void {
|
|
396
|
+
this.result = v
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
/** what gets SENT is editable too: sharpen the sketch, then refine, without closing the
|
|
400
|
+
* modal. It stays a copy — only apply() writes the var */
|
|
401
|
+
setOriginal(v: string): void {
|
|
402
|
+
this.original = v
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
async loadModels(): Promise<void> {
|
|
406
|
+
runInAction(() => {
|
|
407
|
+
this.modelsState = 'loading'
|
|
408
|
+
this.modelsError = ''
|
|
409
|
+
})
|
|
410
|
+
try {
|
|
411
|
+
const models = await fetchModels(this.endpoint)
|
|
412
|
+
runInAction(() => {
|
|
413
|
+
this.models = models
|
|
414
|
+
this.modelsState = 'idle'
|
|
415
|
+
})
|
|
416
|
+
} catch (e) {
|
|
417
|
+
runInAction(() => {
|
|
418
|
+
this.modelsState = 'error'
|
|
419
|
+
this.modelsError = e instanceof Error ? e.message : String(e)
|
|
420
|
+
})
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
cancel(): void {
|
|
425
|
+
this.abort?.abort()
|
|
426
|
+
this.abort = null
|
|
427
|
+
if (this.phase === 'running') this.phase = this.result === '' ? 'idle' : 'done'
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
run(): void {
|
|
431
|
+
void this.runNow()
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
private async runNow(): Promise<void> {
|
|
435
|
+
const preset = this.preset
|
|
436
|
+
if (preset == null || this.phase === 'running') return
|
|
437
|
+
const controller = new AbortController()
|
|
438
|
+
this.abort = controller
|
|
439
|
+
runInAction(() => {
|
|
440
|
+
this.phase = 'running'
|
|
441
|
+
this.error = ''
|
|
442
|
+
this.result = ''
|
|
443
|
+
this.thinking = ''
|
|
444
|
+
})
|
|
445
|
+
try {
|
|
446
|
+
await streamRefine({
|
|
447
|
+
endpoint: this.endpoint,
|
|
448
|
+
model: this.model,
|
|
449
|
+
system: preset.text,
|
|
450
|
+
user: this.original,
|
|
451
|
+
effort: this.effort,
|
|
452
|
+
signal: controller.signal,
|
|
453
|
+
onDelta: (d) =>
|
|
454
|
+
runInAction(() => {
|
|
455
|
+
this.result += d.content
|
|
456
|
+
this.thinking += d.reasoning
|
|
457
|
+
}),
|
|
458
|
+
})
|
|
459
|
+
runInAction(() => {
|
|
460
|
+
this.phase = 'done'
|
|
461
|
+
})
|
|
462
|
+
} catch (e) {
|
|
463
|
+
// an abort is a user gesture, not a failure to shout about
|
|
464
|
+
const aborted = controller.signal.aborted
|
|
465
|
+
runInAction(() => {
|
|
466
|
+
this.phase = aborted ? (this.result === '' ? 'idle' : 'done') : 'error'
|
|
467
|
+
if (!aborted) this.error = e instanceof Error ? e.message : String(e)
|
|
468
|
+
})
|
|
469
|
+
} finally {
|
|
470
|
+
if (this.abort === controller) this.abort = null
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
/** the ONLY write into the form: nothing lands on the var until this is clicked */
|
|
475
|
+
apply(): void {
|
|
476
|
+
const text = this.result.trim()
|
|
477
|
+
if (this.target == null || text === '') return
|
|
478
|
+
this.target.set(text)
|
|
479
|
+
this.close()
|
|
480
|
+
}
|
|
481
|
+
}
|
|
@@ -0,0 +1,180 @@
|
|
|
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
|
+
/** stop the autosave. `flush: false` is the DELETE path: flushing there would write the
|
|
93
|
+
* draft file back milliseconds after the server removed it */
|
|
94
|
+
dispose(p: { flush?: boolean } = {}): void {
|
|
95
|
+
for (const d of this.disposers) d()
|
|
96
|
+
this.disposers = []
|
|
97
|
+
if (p.flush === false) return
|
|
98
|
+
// a draft switch inside the debounce window must not lose the edit
|
|
99
|
+
if (JSON.stringify(this.valuesJSON()) !== this.lastSaved) void this.save()
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** tab-close/hide flush. keepalive survives page teardown, so this one does NOT ride
|
|
103
|
+
* saveChain (chaining could delay it past unload). lastSaved is committed only when the
|
|
104
|
+
* server answers: a failed flush must stay dirty so the next save() retries it */
|
|
105
|
+
flushKeepalive(): void {
|
|
106
|
+
const encoded = JSON.stringify(this.valuesJSON())
|
|
107
|
+
if (encoded === this.lastSaved) return
|
|
108
|
+
void saveDraft(
|
|
109
|
+
{ module: this.moduleKey, draft: this.draft, values: JSON.parse(encoded) as Record<string, unknown> },
|
|
110
|
+
{ keepalive: true },
|
|
111
|
+
).then(
|
|
112
|
+
() =>
|
|
113
|
+
runInAction(() => {
|
|
114
|
+
this.lastSaved = encoded
|
|
115
|
+
this.saveState = 'saved'
|
|
116
|
+
}),
|
|
117
|
+
(e: unknown) =>
|
|
118
|
+
runInAction(() => {
|
|
119
|
+
this.saveState = 'error'
|
|
120
|
+
this.saveError = e instanceof Error ? e.message : String(e)
|
|
121
|
+
}),
|
|
122
|
+
)
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
valuesJSON(): Record<string, unknown> {
|
|
126
|
+
return Object.fromEntries(this.vars.map((v) => [v.name, v.value]))
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/** frozen values for a QUEUED run (seeds excluded — payload.ts owns the why) */
|
|
130
|
+
queuePayload(): Record<string, unknown> {
|
|
131
|
+
return payloadSnapshot(this.vars.map((v) => ({ name: v.name, kind: v.desc.kind, value: v.value })))
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
get dirtyCount(): number {
|
|
135
|
+
return this.vars.filter((v) => v.dirty).length
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/** persist the draft now. Resolves FALSE on failure — generate() must not run stale inputs */
|
|
139
|
+
save(): Promise<boolean> {
|
|
140
|
+
const encoded = JSON.stringify(this.valuesJSON())
|
|
141
|
+
if (encoded === this.lastSaved) {
|
|
142
|
+
// nothing to write: the disk already holds these values, whatever an OLDER save did.
|
|
143
|
+
// returning the previous chain here would keep a stale false alive and dead-lock generate
|
|
144
|
+
runInAction(() => {
|
|
145
|
+
this.saveState = 'saved'
|
|
146
|
+
this.saveError = null
|
|
147
|
+
})
|
|
148
|
+
return Promise.resolve(true)
|
|
149
|
+
}
|
|
150
|
+
runInAction(() => {
|
|
151
|
+
this.saveState = 'saving'
|
|
152
|
+
})
|
|
153
|
+
this.saveChain = this.saveChain.then(async () => {
|
|
154
|
+
try {
|
|
155
|
+
await saveDraft({
|
|
156
|
+
module: this.moduleKey,
|
|
157
|
+
draft: this.draft,
|
|
158
|
+
values: JSON.parse(encoded) as Record<string, unknown>,
|
|
159
|
+
})
|
|
160
|
+
runInAction(() => {
|
|
161
|
+
this.lastSaved = encoded
|
|
162
|
+
this.saveState = 'saved'
|
|
163
|
+
this.saveError = null
|
|
164
|
+
})
|
|
165
|
+
return true
|
|
166
|
+
} catch (e) {
|
|
167
|
+
runInAction(() => {
|
|
168
|
+
this.saveState = 'error'
|
|
169
|
+
this.saveError = e instanceof Error ? e.message : String(e)
|
|
170
|
+
})
|
|
171
|
+
return false
|
|
172
|
+
}
|
|
173
|
+
})
|
|
174
|
+
return this.saveChain
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
revertAll(): void {
|
|
178
|
+
for (const v of this.vars) v.revert()
|
|
179
|
+
}
|
|
180
|
+
}
|