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,254 @@
|
|
|
1
|
+
// browser → an OpenAI-wire LLM. Two providers: openrouter (cloud) and open webui
|
|
2
|
+
// (a local box). The second http module after api.ts, and the only one that leaves
|
|
3
|
+
// this origin. Keys are parameters, never module state: they live in localStorage
|
|
4
|
+
// (EnhancerSt) and never reach the serve process.
|
|
5
|
+
// Parsing is pure and DOM-free so tests/serve-web-enhancer.test.ts covers it.
|
|
6
|
+
|
|
7
|
+
export type ProviderId = 'openrouter' | 'openwebui'
|
|
8
|
+
|
|
9
|
+
export type Endpoint = { provider: ProviderId; baseUrl: string; key: string }
|
|
10
|
+
|
|
11
|
+
export const OPENROUTER_BASE = 'https://openrouter.ai/api/v1'
|
|
12
|
+
export const OPENWEBUI_BASE = 'http://localhost:3000'
|
|
13
|
+
|
|
14
|
+
export function defaultBaseUrl(provider: ProviderId): string {
|
|
15
|
+
return provider === 'openrouter' ? OPENROUTER_BASE : OPENWEBUI_BASE
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function trimSlash(url: string): string {
|
|
19
|
+
return url.trim().replace(/\/+$/, '')
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** open webui hangs its OpenAI-compatible api under /api, openrouter's base already carries /v1 */
|
|
23
|
+
export function modelsUrl(e: Endpoint): string {
|
|
24
|
+
const base = trimSlash(e.baseUrl) === '' ? defaultBaseUrl(e.provider) : trimSlash(e.baseUrl)
|
|
25
|
+
return e.provider === 'openrouter' ? `${base}/models` : `${base}/api/models`
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function chatUrl(e: Endpoint): string {
|
|
29
|
+
const base = trimSlash(e.baseUrl) === '' ? defaultBaseUrl(e.provider) : trimSlash(e.baseUrl)
|
|
30
|
+
return e.provider === 'openrouter' ? `${base}/chat/completions` : `${base}/api/chat/completions`
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** reasoning: true = says so · false = says it cannot · null = the provider does not tell
|
|
34
|
+
* (open webui reports no capabilities, and hiding every local model would be the wrong answer) */
|
|
35
|
+
export type LlmModel = { id: string; name: string; reasoning: boolean | null }
|
|
36
|
+
|
|
37
|
+
export type ReasoningEffort = 'off' | 'low' | 'medium' | 'high'
|
|
38
|
+
|
|
39
|
+
/** an external api drifts: every field is shape-checked before it is kept (coding.md cast family 10) */
|
|
40
|
+
function obj(raw: unknown): Record<string, unknown> {
|
|
41
|
+
return typeof raw === 'object' && raw !== null && !Array.isArray(raw) ? (raw as Record<string, unknown>) : {}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function str(raw: unknown): string {
|
|
45
|
+
return typeof raw === 'string' ? raw : ''
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function parseModelsReply(raw: unknown): LlmModel[] {
|
|
49
|
+
const data = obj(raw).data
|
|
50
|
+
if (!Array.isArray(data)) return []
|
|
51
|
+
const out: LlmModel[] = []
|
|
52
|
+
for (const entry of data) {
|
|
53
|
+
const m = obj(entry)
|
|
54
|
+
const id = str(m.id)
|
|
55
|
+
if (id === '') continue
|
|
56
|
+
const params = m.supported_parameters
|
|
57
|
+
const reasoning = Array.isArray(params) ? params.includes('reasoning') : null
|
|
58
|
+
out.push({ id, name: str(m.name) === '' ? id : str(m.name), reasoning })
|
|
59
|
+
}
|
|
60
|
+
out.sort((a, b) => a.id.localeCompare(b.id))
|
|
61
|
+
return out
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
async function failure(res: Response, what: string): Promise<Error> {
|
|
65
|
+
// never fail silently, and never let a gateway's html body masquerade as json
|
|
66
|
+
const body = await res.text().catch(() => '')
|
|
67
|
+
return new Error(`llm ${what} failed: http ${res.status} ${body.slice(0, 300)}`)
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** a browser fetch that dies on CORS throws a bare TypeError with no detail — say what it
|
|
71
|
+
* probably is, because "Failed to fetch" against a local box is otherwise unactionable */
|
|
72
|
+
function transportError(e: unknown, url: string): Error {
|
|
73
|
+
if (e instanceof Error && e.name === 'AbortError') return e
|
|
74
|
+
const msg = e instanceof Error ? e.message : String(e)
|
|
75
|
+
return new Error(
|
|
76
|
+
`could not reach ${url} (${msg}) — is it running, and does it allow this origin? open webui needs CORS_ALLOW_ORIGIN`,
|
|
77
|
+
)
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export async function fetchModels(e: Endpoint): Promise<LlmModel[]> {
|
|
81
|
+
const url = modelsUrl(e)
|
|
82
|
+
let res: Response
|
|
83
|
+
try {
|
|
84
|
+
res = await fetch(url, { headers: authHeaders(e) })
|
|
85
|
+
} catch (err) {
|
|
86
|
+
throw transportError(err, url)
|
|
87
|
+
}
|
|
88
|
+
if (!res.ok) throw await failure(res, 'model list')
|
|
89
|
+
return parseModelsReply(await res.json())
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function authHeaders(e: Endpoint): Record<string, string> {
|
|
93
|
+
// open webui on a trusted local box may run without a key: send the header only when there is one
|
|
94
|
+
return e.key.trim() === '' ? {} : { Authorization: `Bearer ${e.key.trim()}` }
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export type RefineRequest = { model: string; system: string; user: string; effort: ReasoningEffort }
|
|
98
|
+
|
|
99
|
+
/** the chat body. `reasoning` rides OPENROUTER only: a local backend rejects the unknown field,
|
|
100
|
+
* and its thinking models think on their own (see ThinkSplitter) */
|
|
101
|
+
export function buildRefineBody(p: RefineRequest & { provider: ProviderId }): Record<string, unknown> {
|
|
102
|
+
const reasoning = p.provider === 'openrouter' && p.effort !== 'off' ? { reasoning: { effort: p.effort } } : {}
|
|
103
|
+
return {
|
|
104
|
+
model: p.model,
|
|
105
|
+
stream: true,
|
|
106
|
+
messages: [
|
|
107
|
+
{ role: 'system', content: p.system },
|
|
108
|
+
{ role: 'user', content: p.user },
|
|
109
|
+
],
|
|
110
|
+
...reasoning,
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** SSE framing: complete lines come out, the trailing partial stays in `rest` for the next chunk */
|
|
115
|
+
export function splitSseBuffer(buffer: string): { events: string[]; rest: string } {
|
|
116
|
+
const lines = buffer.split('\n')
|
|
117
|
+
const rest = lines.pop() ?? ''
|
|
118
|
+
const events: string[] = []
|
|
119
|
+
for (const line of lines) {
|
|
120
|
+
const trimmed = line.trim()
|
|
121
|
+
// ": OPENROUTER PROCESSING" keep-alive comments are not events
|
|
122
|
+
if (trimmed === '' || trimmed.startsWith(':')) continue
|
|
123
|
+
if (trimmed.startsWith('data:')) events.push(trimmed.slice(5).trim())
|
|
124
|
+
}
|
|
125
|
+
return { events, rest }
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
export type StreamDelta = { content: string; reasoning: string }
|
|
129
|
+
|
|
130
|
+
/** one `data:` payload → what it adds. Cloud thinking models put the answer in `content`
|
|
131
|
+
* and their work in `reasoning`; both are streamed, only content is the prompt */
|
|
132
|
+
export function readStreamDelta(payload: string): StreamDelta | null {
|
|
133
|
+
if (payload === '' || payload === '[DONE]') return null
|
|
134
|
+
let parsed: unknown
|
|
135
|
+
try {
|
|
136
|
+
parsed = JSON.parse(payload)
|
|
137
|
+
} catch {
|
|
138
|
+
return null
|
|
139
|
+
}
|
|
140
|
+
const root = obj(parsed)
|
|
141
|
+
const err = obj(root.error)
|
|
142
|
+
if (str(err.message) !== '') throw new Error(`llm stream error: ${str(err.message)}`)
|
|
143
|
+
const choices = root.choices
|
|
144
|
+
if (!Array.isArray(choices)) return null
|
|
145
|
+
const delta = obj(obj(choices[0]).delta)
|
|
146
|
+
const content = str(delta.content)
|
|
147
|
+
// some backends spell it reasoning_content
|
|
148
|
+
const reasoning = str(delta.reasoning) === '' ? str(delta.reasoning_content) : str(delta.reasoning)
|
|
149
|
+
if (content === '' && reasoning === '') return null
|
|
150
|
+
return { content, reasoning }
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* local thinking models (qwen3, deepseek-r1 through ollama) stream their reasoning INLINE,
|
|
155
|
+
* as `<think>…</think>` at the head of content. Left alone it lands in the prompt, so it is
|
|
156
|
+
* routed to the thinking pane instead. Stateful because a tag can straddle two chunks.
|
|
157
|
+
*/
|
|
158
|
+
export class ThinkSplitter {
|
|
159
|
+
private inside = false
|
|
160
|
+
/** a partial tag held back until the next chunk decides what it is */
|
|
161
|
+
private pending = ''
|
|
162
|
+
|
|
163
|
+
feed(chunk: string): StreamDelta {
|
|
164
|
+
let text = this.pending + chunk
|
|
165
|
+
this.pending = ''
|
|
166
|
+
let content = ''
|
|
167
|
+
let reasoning = ''
|
|
168
|
+
for (;;) {
|
|
169
|
+
const tag = this.inside ? '</think>' : '<think>'
|
|
170
|
+
const at = text.indexOf(tag)
|
|
171
|
+
if (at === -1) break
|
|
172
|
+
const before = text.slice(0, at)
|
|
173
|
+
if (this.inside) reasoning += before
|
|
174
|
+
else content += before
|
|
175
|
+
text = text.slice(at + tag.length)
|
|
176
|
+
this.inside = !this.inside
|
|
177
|
+
}
|
|
178
|
+
// a trailing '<' or '</thin' may be the start of a tag: hold it, do not emit it
|
|
179
|
+
const cut = lastOpenTagStart(text)
|
|
180
|
+
if (cut !== -1) {
|
|
181
|
+
this.pending = text.slice(cut)
|
|
182
|
+
text = text.slice(0, cut)
|
|
183
|
+
}
|
|
184
|
+
if (this.inside) reasoning += text
|
|
185
|
+
else content += text
|
|
186
|
+
return { content, reasoning }
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/** end of stream: whatever was held back was never a tag, so it is real text */
|
|
190
|
+
flush(): StreamDelta {
|
|
191
|
+
const rest = this.pending
|
|
192
|
+
this.pending = ''
|
|
193
|
+
return this.inside ? { content: '', reasoning: rest } : { content: rest, reasoning: '' }
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/** index of a trailing fragment that could still become `<think>` or `</think>`, else -1 */
|
|
198
|
+
function lastOpenTagStart(text: string): number {
|
|
199
|
+
const at = text.lastIndexOf('<')
|
|
200
|
+
if (at === -1) return -1
|
|
201
|
+
const tail = text.slice(at)
|
|
202
|
+
return '<think>'.startsWith(tail) || '</think>'.startsWith(tail) ? at : -1
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/** streamed refine. onDelta fires per chunk so a slow thinking model shows work, not a spinner */
|
|
206
|
+
export async function streamRefine(
|
|
207
|
+
p: RefineRequest & { endpoint: Endpoint; signal: AbortSignal; onDelta: (d: StreamDelta) => void },
|
|
208
|
+
): Promise<StreamDelta> {
|
|
209
|
+
if (p.endpoint.provider === 'openrouter' && p.endpoint.key.trim() === '')
|
|
210
|
+
throw new Error('no openrouter api key — paste one in the enhancer settings')
|
|
211
|
+
if (p.model.trim() === '') throw new Error('no model selected')
|
|
212
|
+
const url = chatUrl(p.endpoint)
|
|
213
|
+
let res: Response
|
|
214
|
+
try {
|
|
215
|
+
res = await fetch(url, {
|
|
216
|
+
method: 'POST',
|
|
217
|
+
headers: { ...authHeaders(p.endpoint), 'content-type': 'application/json' },
|
|
218
|
+
body: JSON.stringify(buildRefineBody({ ...p, provider: p.endpoint.provider })),
|
|
219
|
+
signal: p.signal,
|
|
220
|
+
})
|
|
221
|
+
} catch (e) {
|
|
222
|
+
throw transportError(e, url)
|
|
223
|
+
}
|
|
224
|
+
if (!res.ok) throw await failure(res, 'refine')
|
|
225
|
+
const reader = res.body?.getReader()
|
|
226
|
+
if (reader == null) throw new Error('llm refine failed: the response carried no stream')
|
|
227
|
+
const decoder = new TextDecoder()
|
|
228
|
+
const splitter = new ThinkSplitter()
|
|
229
|
+
const total: StreamDelta = { content: '', reasoning: '' }
|
|
230
|
+
const emit = (d: StreamDelta): void => {
|
|
231
|
+
if (d.content === '' && d.reasoning === '') return
|
|
232
|
+
total.content += d.content
|
|
233
|
+
total.reasoning += d.reasoning
|
|
234
|
+
p.onDelta(d)
|
|
235
|
+
}
|
|
236
|
+
let buffer = ''
|
|
237
|
+
for (;;) {
|
|
238
|
+
const chunk = await reader.read()
|
|
239
|
+
if (chunk.done) break
|
|
240
|
+
buffer += decoder.decode(chunk.value, { stream: true })
|
|
241
|
+
const split = splitSseBuffer(buffer)
|
|
242
|
+
buffer = split.rest
|
|
243
|
+
for (const event of split.events) {
|
|
244
|
+
const delta = readStreamDelta(event)
|
|
245
|
+
if (delta == null) continue
|
|
246
|
+
// inline <think> is split out of CONTENT; a provider-reported reasoning field rides along
|
|
247
|
+
const parts = splitter.feed(delta.content)
|
|
248
|
+
emit({ content: parts.content, reasoning: parts.reasoning + delta.reasoning })
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
emit(splitter.flush())
|
|
252
|
+
if (total.content.trim() === '') throw new Error('the model returned no prompt text — try another model')
|
|
253
|
+
return total
|
|
254
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
// browser entry: inject the stylesheet, mount the root store + app
|
|
2
|
+
import { createRoot } from 'react-dom/client'
|
|
3
|
+
import { App } from 'src/cli/serve/web/components/App.tsx'
|
|
4
|
+
import { WebSt } from 'src/cli/serve/web/state/WebSt.ts'
|
|
5
|
+
import { STYLES } from 'src/cli/serve/web/styles.ts'
|
|
6
|
+
|
|
7
|
+
const styleEl = document.createElement('style')
|
|
8
|
+
styleEl.textContent = STYLES
|
|
9
|
+
document.head.appendChild(styleEl)
|
|
10
|
+
|
|
11
|
+
const st = new WebSt()
|
|
12
|
+
// console debugging path (app-state-tree: expose the root) — globalThis probe,
|
|
13
|
+
// agent/coding.md cast whitelist family 3
|
|
14
|
+
;(globalThis as { webst?: WebSt }).webst = st
|
|
15
|
+
|
|
16
|
+
const rootEl = document.getElementById('root')
|
|
17
|
+
if (rootEl == null) throw new Error('[serve web] #root element missing from the shell html')
|
|
18
|
+
createRoot(rootEl).render(<App st={st} />)
|