comfy-ts 2.6.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 +3 -1
- package/dist/cli.js +4 -4
- package/dist/cli.js.map +1 -1
- package/dist/index.cjs +5 -1
- 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 +5 -1
- package/dist/index.js.map +1 -1
- package/dist/{run-serve-CVu39RNd.js → run-serve-D1Tsuru9.js} +172 -12
- package/dist/run-serve-D1Tsuru9.js.map +1 -0
- package/dist/{run-tui-idFhdO4f.js → run-tui-JxlEbb_f.js} +3 -3
- package/dist/{run-tui-idFhdO4f.js.map → run-tui-JxlEbb_f.js.map} +1 -1
- package/dist/serve-web.js +15 -22
- package/dist/{state-C90kdjtB.js → state-CaHeDL-0.js} +6 -2
- package/dist/{state-C90kdjtB.js.map → state-CaHeDL-0.js.map} +1 -1
- package/dist/web.d.ts +3 -0
- package/dist/web.js +5 -1
- package/dist/web.js.map +1 -1
- package/examples/web/tsconfig.json +4 -1
- package/package.json +1 -1
- package/src/cli/comfy-ts-cli.ts +1 -1
- package/src/cli/serve/ServeApp.ts +76 -8
- package/src/cli/serve/promptEnhancers.ts +69 -0
- package/src/cli/serve/run-serve.ts +38 -6
- package/src/cli/serve/safeName.ts +12 -0
- package/src/cli/serve/web/api.ts +23 -0
- package/src/cli/serve/web/components/PromptEnhancer.tsx +76 -32
- package/src/cli/serve/web/components/VarsForm.tsx +11 -0
- package/src/cli/serve/web/llm.ts +254 -0
- package/src/cli/serve/web/state/EnhancerSt.ts +268 -157
- package/src/cli/serve/web/state/FormSt.ts +4 -1
- package/src/cli/serve/web/state/WebSt.ts +26 -1
- package/src/cli/serve/web/styles.ts +2 -0
- package/src/state.ts +6 -0
- package/dist/run-serve-CVu39RNd.js.map +0 -1
- package/src/cli/serve/web/openrouter.ts +0 -142
|
@@ -1,87 +1,85 @@
|
|
|
1
|
-
// the prompt enhancer: settings +
|
|
2
|
-
//
|
|
3
|
-
//
|
|
4
|
-
//
|
|
5
|
-
|
|
6
|
-
|
|
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'
|
|
7
22
|
import type { VarSt } from 'src/cli/serve/web/state/FormSt.ts'
|
|
8
23
|
|
|
9
24
|
const STORAGE_KEY = 'comfy-ts-serve-enhancer'
|
|
10
25
|
|
|
11
26
|
/** explicit model ids, never an alias: behaviour and cost stay reproducible */
|
|
12
|
-
const DEFAULT_MODEL =
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
const KREA2_MASTER = `You rewrite image prompts for krea2 turbo, a distilled 8 step text to image model whose text encoder reads flowing natural language, not tag soup, and which is steered by adjectives rather than by negatives.
|
|
17
|
-
|
|
18
|
-
Rewrite the user's prompt into ONE dense paragraph someone could picture with their eyes closed:
|
|
19
|
-
- keep every noun and every named subject the user wrote, they are the point
|
|
20
|
-
- add what is missing to make it fun to look at: lighting, palette, material, texture, camera angle, composition, mood
|
|
21
|
-
- prefer concrete visual nouns over praise, never write "masterpiece", "8k", "highly detailed", "award winning"
|
|
22
|
-
- stay under about 80 words, no lists, no headings, no quotes around the result
|
|
23
|
-
- keep any "// " comment line and any "- " negative line from the input verbatim, each on its own line
|
|
24
|
-
|
|
25
|
-
Answer with the rewritten prompt and nothing else: no preamble, no explanation.`
|
|
26
|
-
|
|
27
|
-
/** shipped starting point, not a contract: a refiner is specific to the image model it feeds,
|
|
28
|
-
* so the library is editable and this one is just the first entry */
|
|
29
|
-
export function defaultPresets(): MasterPrompt[] {
|
|
30
|
-
return [{ id: 'refine-krea2-prompt', name: 'refine-krea2-prompt', text: KREA2_MASTER }]
|
|
27
|
+
const DEFAULT_MODEL: Record<ProviderId, string> = {
|
|
28
|
+
openrouter: 'anthropic/claude-sonnet-5',
|
|
29
|
+
openwebui: '',
|
|
31
30
|
}
|
|
32
31
|
|
|
32
|
+
export const PROVIDERS: ProviderId[] = ['openrouter', 'openwebui']
|
|
33
|
+
|
|
33
34
|
export type EnhancerSettings = {
|
|
34
|
-
|
|
35
|
-
model
|
|
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>
|
|
36
40
|
effort: ReasoningEffort
|
|
37
41
|
thinkingOnly: boolean
|
|
38
|
-
|
|
39
|
-
|
|
42
|
+
/** selected master prompt, by FILE NAME */
|
|
43
|
+
presetName: string
|
|
40
44
|
/** last preset used per module: a workflow reopens on its own refiner */
|
|
41
45
|
presetByModule: Record<string, string>
|
|
42
46
|
}
|
|
43
47
|
|
|
44
|
-
function
|
|
45
|
-
return
|
|
48
|
+
function isProvider(raw: unknown): raw is ProviderId {
|
|
49
|
+
return raw === 'openrouter' || raw === 'openwebui'
|
|
46
50
|
}
|
|
47
51
|
|
|
48
52
|
function isEffort(raw: unknown): raw is ReasoningEffort {
|
|
49
53
|
return raw === 'off' || raw === 'low' || raw === 'medium' || raw === 'high'
|
|
50
54
|
}
|
|
51
55
|
|
|
52
|
-
|
|
53
|
-
|
|
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) */
|
|
54
66
|
export function normalizeSettings(raw: unknown): EnhancerSettings {
|
|
55
67
|
const o = typeof raw === 'object' && raw !== null ? (raw as Record<string, unknown>) : {}
|
|
56
|
-
const
|
|
57
|
-
|
|
58
|
-
for (const entry of o.presets) {
|
|
59
|
-
const p = typeof entry === 'object' && entry !== null ? (entry as Record<string, unknown>) : {}
|
|
60
|
-
const id = str(p.id, '')
|
|
61
|
-
if (id === '') continue
|
|
62
|
-
presets.push({ id, name: str(p.name, id), text: typeof p.text === 'string' ? p.text : '' })
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
// an EMPTY library means first run, so seed it; a library he emptied on purpose keeps
|
|
66
|
-
// one entry anyway, because the modal needs something to send
|
|
67
|
-
const library = presets.length > 0 ? presets : defaultPresets()
|
|
68
|
-
const byModule: Record<string, string> = {}
|
|
69
|
-
if (typeof o.presetByModule === 'object' && o.presetByModule !== null) {
|
|
70
|
-
for (const [k, v] of Object.entries(o.presetByModule)) if (typeof v === 'string') byModule[k] = v
|
|
71
|
-
}
|
|
72
|
-
const wantedId = str(o.presetId, '')
|
|
68
|
+
const models = strMap(o.modelByProvider)
|
|
69
|
+
const bases = strMap(o.baseUrlByProvider)
|
|
73
70
|
return {
|
|
74
|
-
|
|
75
|
-
|
|
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 },
|
|
76
75
|
effort: isEffort(o.effort) ? o.effort : 'medium',
|
|
77
76
|
thinkingOnly: o.thinkingOnly !== false,
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
presetByModule: byModule,
|
|
77
|
+
presetName: typeof o.presetName === 'string' ? o.presetName : '',
|
|
78
|
+
presetByModule: strMap(o.presetByModule),
|
|
81
79
|
}
|
|
82
80
|
}
|
|
83
81
|
|
|
84
|
-
/** unique name for a new/duplicated
|
|
82
|
+
/** unique name for a new/duplicated master prompt — the name IS the filename */
|
|
85
83
|
export function nextPresetName(base: string, taken: readonly string[]): string {
|
|
86
84
|
if (!taken.includes(base)) return base
|
|
87
85
|
for (let i = 2; i < 1000; i++) {
|
|
@@ -91,10 +89,6 @@ export function nextPresetName(base: string, taken: readonly string[]): string {
|
|
|
91
89
|
return `${base} ${taken.length + 1}`
|
|
92
90
|
}
|
|
93
91
|
|
|
94
|
-
function newId(): string {
|
|
95
|
-
return `mp-${Math.random().toString(36).slice(2, 10)}`
|
|
96
|
-
}
|
|
97
|
-
|
|
98
92
|
function readStored(): EnhancerSettings {
|
|
99
93
|
try {
|
|
100
94
|
return normalizeSettings(JSON.parse(localStorage.getItem(STORAGE_KEY) ?? '{}'))
|
|
@@ -104,15 +98,23 @@ function readStored(): EnhancerSettings {
|
|
|
104
98
|
}
|
|
105
99
|
|
|
106
100
|
export class EnhancerSt {
|
|
107
|
-
|
|
108
|
-
|
|
101
|
+
provider: ProviderId
|
|
102
|
+
keyByProvider: Record<string, string>
|
|
103
|
+
baseUrlByProvider: Record<string, string>
|
|
104
|
+
modelByProvider: Record<string, string>
|
|
109
105
|
effort: ReasoningEffort
|
|
110
106
|
thinkingOnly: boolean
|
|
111
|
-
|
|
112
|
-
presetId: string
|
|
107
|
+
presetName: string
|
|
113
108
|
presetByModule: Record<string, string>
|
|
114
109
|
|
|
115
|
-
|
|
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[] = []
|
|
116
118
|
modelsState: 'idle' | 'loading' | 'error' = 'idle'
|
|
117
119
|
modelsError = ''
|
|
118
120
|
|
|
@@ -125,58 +127,85 @@ export class EnhancerSt {
|
|
|
125
127
|
phase: 'idle' | 'running' | 'done' | 'error' = 'idle'
|
|
126
128
|
error = ''
|
|
127
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 = ''
|
|
128
134
|
|
|
129
135
|
constructor() {
|
|
130
136
|
const s = readStored()
|
|
131
|
-
this.
|
|
132
|
-
this.
|
|
137
|
+
this.provider = s.provider
|
|
138
|
+
this.keyByProvider = s.keyByProvider
|
|
139
|
+
this.baseUrlByProvider = s.baseUrlByProvider
|
|
140
|
+
this.modelByProvider = s.modelByProvider
|
|
133
141
|
this.effort = s.effort
|
|
134
142
|
this.thinkingOnly = s.thinkingOnly
|
|
135
|
-
this.
|
|
136
|
-
this.presetId = s.presetId
|
|
143
|
+
this.presetName = s.presetName
|
|
137
144
|
this.presetByModule = s.presetByModule
|
|
138
|
-
makeAutoObservable<EnhancerSt, 'abort'>(this, {
|
|
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
|
+
)
|
|
139
163
|
}
|
|
140
164
|
|
|
141
|
-
|
|
142
|
-
|
|
165
|
+
dispose(): void {
|
|
166
|
+
this.disposer()
|
|
143
167
|
}
|
|
144
168
|
|
|
145
|
-
|
|
146
|
-
|
|
169
|
+
// #region settings ---------------------------------------------------------
|
|
170
|
+
get apiKey(): string {
|
|
171
|
+
return this.keyByProvider[this.provider] ?? ''
|
|
147
172
|
}
|
|
148
173
|
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
return this.thinkingOnly ? this.models.filter((m) => m.reasoning) : this.models
|
|
174
|
+
get baseUrl(): string {
|
|
175
|
+
return this.baseUrlByProvider[this.provider] ?? defaultBaseUrl(this.provider)
|
|
152
176
|
}
|
|
153
177
|
|
|
154
|
-
|
|
155
|
-
this.
|
|
156
|
-
this.targetModule = p.module
|
|
157
|
-
this.original = typeof p.v.value === 'string' ? p.v.value : ''
|
|
158
|
-
this.result = ''
|
|
159
|
-
this.thinking = ''
|
|
160
|
-
this.error = ''
|
|
161
|
-
this.phase = 'idle'
|
|
162
|
-
const remembered = this.presetByModule[p.module]
|
|
163
|
-
if (remembered != null && this.presets.some((x) => x.id === remembered)) this.presetId = remembered
|
|
178
|
+
get model(): string {
|
|
179
|
+
return this.modelByProvider[this.provider] ?? ''
|
|
164
180
|
}
|
|
165
181
|
|
|
166
|
-
|
|
167
|
-
this.
|
|
168
|
-
|
|
169
|
-
|
|
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
|
|
170
198
|
}
|
|
171
199
|
|
|
172
200
|
settingsJSON(): EnhancerSettings {
|
|
173
201
|
return {
|
|
174
|
-
|
|
175
|
-
|
|
202
|
+
provider: this.provider,
|
|
203
|
+
keyByProvider: this.keyByProvider,
|
|
204
|
+
baseUrlByProvider: this.baseUrlByProvider,
|
|
205
|
+
modelByProvider: this.modelByProvider,
|
|
176
206
|
effort: this.effort,
|
|
177
207
|
thinkingOnly: this.thinkingOnly,
|
|
178
|
-
|
|
179
|
-
presetId: this.presetId,
|
|
208
|
+
presetName: this.presetName,
|
|
180
209
|
presetByModule: this.presetByModule,
|
|
181
210
|
}
|
|
182
211
|
}
|
|
@@ -189,82 +218,111 @@ export class EnhancerSt {
|
|
|
189
218
|
}
|
|
190
219
|
}
|
|
191
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
|
+
|
|
192
232
|
setApiKey(v: string): void {
|
|
193
|
-
this.
|
|
233
|
+
this.keyByProvider[this.provider] = v.trim()
|
|
234
|
+
this.persist()
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
setBaseUrl(v: string): void {
|
|
238
|
+
this.baseUrlByProvider[this.provider] = v.trim()
|
|
194
239
|
this.persist()
|
|
195
240
|
}
|
|
196
241
|
|
|
197
242
|
setModel(v: string): void {
|
|
198
|
-
this.
|
|
243
|
+
this.modelByProvider[this.provider] = v
|
|
199
244
|
this.persist()
|
|
200
245
|
}
|
|
201
246
|
|
|
202
|
-
/** takes the raw select value: validating here keeps the component cast-free */
|
|
203
247
|
setEffort(v: string): void {
|
|
204
248
|
if (!isEffort(v)) return
|
|
205
249
|
this.effort = v
|
|
206
250
|
this.persist()
|
|
207
251
|
}
|
|
208
252
|
|
|
209
|
-
/** the rewrite is editable before it is applied */
|
|
210
|
-
setResult(v: string): void {
|
|
211
|
-
this.result = v
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
/** what gets SENT is editable too: sharpen the sketch, then refine, without closing the modal.
|
|
215
|
-
* It stays a copy — only apply() writes the var */
|
|
216
|
-
setOriginal(v: string): void {
|
|
217
|
-
this.original = v
|
|
218
|
-
}
|
|
219
|
-
|
|
220
253
|
toggleThinkingOnly(): void {
|
|
221
254
|
this.thinkingOnly = !this.thinkingOnly
|
|
222
255
|
this.persist()
|
|
223
256
|
}
|
|
224
257
|
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
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
|
+
}
|
|
230
278
|
}
|
|
231
279
|
|
|
232
|
-
private
|
|
233
|
-
|
|
234
|
-
|
|
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
|
+
}
|
|
235
298
|
}
|
|
236
299
|
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
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()
|
|
240
306
|
}
|
|
241
307
|
|
|
242
|
-
|
|
308
|
+
setPresetText(text: string): void {
|
|
243
309
|
const p = this.preset
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
this.
|
|
247
|
-
...p,
|
|
248
|
-
name: nextPresetName(
|
|
249
|
-
clean,
|
|
250
|
-
this.presets.filter((x) => x.id !== p.id).map((x) => x.name),
|
|
251
|
-
),
|
|
252
|
-
})
|
|
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))
|
|
253
313
|
}
|
|
254
314
|
|
|
315
|
+
/** create (or duplicate): a new FILE, saved immediately so it exists on disk */
|
|
255
316
|
addPreset(name: string, text = ''): void {
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
}
|
|
266
|
-
this.presets = [...this.presets, entry]
|
|
267
|
-
this.selectPreset(entry.id)
|
|
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 })
|
|
268
326
|
}
|
|
269
327
|
|
|
270
328
|
duplicatePreset(): void {
|
|
@@ -272,23 +330,76 @@ export class EnhancerSt {
|
|
|
272
330
|
if (p != null) this.addPreset(`${p.name} copy`, p.text)
|
|
273
331
|
}
|
|
274
332
|
|
|
275
|
-
|
|
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> {
|
|
276
350
|
const p = this.preset
|
|
277
351
|
if (p == null) return
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
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
|
+
})
|
|
284
359
|
}
|
|
285
360
|
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
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
|
|
292
403
|
}
|
|
293
404
|
|
|
294
405
|
async loadModels(): Promise<void> {
|
|
@@ -297,7 +408,7 @@ export class EnhancerSt {
|
|
|
297
408
|
this.modelsError = ''
|
|
298
409
|
})
|
|
299
410
|
try {
|
|
300
|
-
const models = await fetchModels(
|
|
411
|
+
const models = await fetchModels(this.endpoint)
|
|
301
412
|
runInAction(() => {
|
|
302
413
|
this.models = models
|
|
303
414
|
this.modelsState = 'idle'
|
|
@@ -333,7 +444,7 @@ export class EnhancerSt {
|
|
|
333
444
|
})
|
|
334
445
|
try {
|
|
335
446
|
await streamRefine({
|
|
336
|
-
|
|
447
|
+
endpoint: this.endpoint,
|
|
337
448
|
model: this.model,
|
|
338
449
|
system: preset.text,
|
|
339
450
|
user: this.original,
|
|
@@ -89,9 +89,12 @@ export class FormSt {
|
|
|
89
89
|
)
|
|
90
90
|
}
|
|
91
91
|
|
|
92
|
-
|
|
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 {
|
|
93
95
|
for (const d of this.disposers) d()
|
|
94
96
|
this.disposers = []
|
|
97
|
+
if (p.flush === false) return
|
|
95
98
|
// a draft switch inside the debounce window must not lose the edit
|
|
96
99
|
if (JSON.stringify(this.valuesJSON()) !== this.lastSaved) void this.save()
|
|
97
100
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// ROOT state tree of the serve web ui (app-state-tree doctrine: one root,
|
|
2
2
|
// child stores hang off it, components read and call)
|
|
3
3
|
import { makeAutoObservable, observableRef, observableShallow, runInAction } from 'mobx'
|
|
4
|
-
import { fetchDraftValues, fetchIndex, saveDraft, type ModuleDescription } from 'src/cli/serve/web/api.ts'
|
|
4
|
+
import { deleteDraft, fetchDraftValues, fetchIndex, saveDraft, type ModuleDescription } from 'src/cli/serve/web/api.ts'
|
|
5
5
|
import { EnhancerSt } from 'src/cli/serve/web/state/EnhancerSt.ts'
|
|
6
6
|
import { FormSt } from 'src/cli/serve/web/state/FormSt.ts'
|
|
7
7
|
import { RunSt } from 'src/cli/serve/web/state/RunSt.ts'
|
|
@@ -188,6 +188,31 @@ export class WebSt {
|
|
|
188
188
|
this.run.enqueue({ module: form.moduleKey, draft: form.draft, payload })
|
|
189
189
|
}
|
|
190
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
|
+
|
|
191
216
|
/** duplicate = save the current values under a new name, then switch to it */
|
|
192
217
|
async duplicateDraft(rawName: string): Promise<void> {
|
|
193
218
|
const form = this.form
|
|
@@ -86,6 +86,8 @@ button:hover { border-color: var(--accent); }
|
|
|
86
86
|
button:disabled { opacity: 0.5; cursor: default; }
|
|
87
87
|
button.primary { background: var(--accent); border-color: var(--accent); color: #0d1117; font-weight: 600; padding: 8px 22px; }
|
|
88
88
|
button.link { border: 0; background: none; color: var(--accent); padding: 0; }
|
|
89
|
+
button.link.danger { color: var(--dim); }
|
|
90
|
+
button.link.danger:hover { color: var(--red); }
|
|
89
91
|
button.mode { padding: 4px 8px; font-size: 12px; }
|
|
90
92
|
button.mode.sel { background: var(--accent-dim); border-color: var(--accent); color: #fff; }
|
|
91
93
|
|
package/src/state.ts
CHANGED
|
@@ -95,6 +95,7 @@ export class ComfyTS {
|
|
|
95
95
|
* .comfy-ts/hosts/<id>/{object_info.json, embeddings.json, sdk.d.ts}
|
|
96
96
|
* .comfy-ts/outputs/... generated images & workflows
|
|
97
97
|
* .comfy-ts/drafts/<workflow-id>/<draft>.json TUI var snapshots (local, gitignored)
|
|
98
|
+
* .comfy-ts/prompt-enhancers/<name>.md serve web ui master prompts (local, gitignored)
|
|
98
99
|
*/
|
|
99
100
|
baseFolder: AbsolutePath
|
|
100
101
|
hostsFolder: AbsolutePath
|
|
@@ -104,6 +105,11 @@ export class ComfyTS {
|
|
|
104
105
|
return asAbsolutePath(join(this.baseFolder, 'drafts', relativePath))
|
|
105
106
|
}
|
|
106
107
|
|
|
108
|
+
/** master prompts of the serve web ui's prompt enhancer — hand-editable markdown */
|
|
109
|
+
resolveFromPromptEnhancers(relativePath: string): AbsolutePath {
|
|
110
|
+
return asAbsolutePath(join(this.baseFolder, 'prompt-enhancers', relativePath))
|
|
111
|
+
}
|
|
112
|
+
|
|
107
113
|
resolveFromCache(relativePath: string): AbsolutePath {
|
|
108
114
|
return asAbsolutePath(join(this.baseFolder, 'cache', relativePath))
|
|
109
115
|
}
|