comfy-ts 2.11.0 → 2.13.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 +1 -0
- package/dist/{LogsSt-qtFzOKpu.js → LogsSt-CUeo843w.js} +51 -7
- package/dist/LogsSt-CUeo843w.js.map +1 -0
- package/dist/cli.js +4 -4
- package/dist/index.cjs +595 -73
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +286 -46
- package/dist/index.d.ts +286 -46
- package/dist/index.js +591 -74
- package/dist/index.js.map +1 -1
- package/dist/{run-serve-BK7BA2dn.js → run-serve-DlRBv_Nk.js} +738 -58
- package/dist/run-serve-DlRBv_Nk.js.map +1 -0
- package/dist/{run-tui-CQyvMf0N.js → run-tui-B5uxm2_l.js} +43 -18
- package/dist/run-tui-B5uxm2_l.js.map +1 -0
- package/dist/{schemaDrift-DjjEtwfx.js → schemaDrift-E95iTF7E.js} +473 -60
- package/dist/schemaDrift-E95iTF7E.js.map +1 -0
- package/dist/serve-web.js +710 -174
- package/dist/{state--wrz0BQc.js → state-CUp2J1Zy.js} +126 -14
- package/dist/state-CUp2J1Zy.js.map +1 -0
- package/dist/web.d.ts +286 -46
- package/dist/web.js +591 -74
- package/dist/web.js.map +1 -1
- package/examples/comfy-cloud/sdk.d.ts +163 -163
- package/examples/rvion/06-qwen-image-edit.cflow.ts +3 -2
- package/examples/rvion/10-anima-t2i.cflow.ts +252 -0
- package/examples/rvion/11-yue2-t2a.cflow.ts +105 -0
- package/examples/rvion/12-stable-audio-3-t2a.cflow.ts +78 -0
- package/examples/rvion/13-ace-step-15-xl-t2a.cflow.ts +153 -0
- package/examples/rvion/stableAudio3Prompts.ts +19 -0
- package/extra/README.md +4 -0
- package/extra/comfyui-fresh-model-lists/README.md +25 -0
- package/extra/comfyui-fresh-model-lists/__init__.py +36 -0
- package/extra/comfyui-fresh-model-lists/test_fresh_model_lists.py +97 -0
- package/package.json +15 -7
- package/src/cli/draftMeta.ts +39 -0
- package/src/cli/serve/ServeApp.ts +305 -30
- package/src/cli/serve/applyVarPayload.ts +50 -22
- package/src/cli/serve/byteRange.ts +48 -0
- package/src/cli/serve/describeVar.ts +42 -6
- package/src/cli/serve/llmConfigShape.ts +51 -0
- package/src/cli/serve/llmConfigs.ts +52 -0
- package/src/cli/serve/run-serve.ts +12 -3
- package/src/cli/serve/tagList.ts +92 -0
- package/src/cli/serve/tagSource.ts +69 -0
- package/src/cli/serve/web/api.ts +72 -0
- package/src/cli/serve/web/components/App.tsx +485 -44
- package/src/cli/serve/web/components/DraftTabs.tsx +167 -0
- package/src/cli/serve/web/components/Gallery.tsx +133 -31
- package/src/cli/serve/web/components/HistoryPicker.tsx +149 -0
- package/src/cli/serve/web/components/Icon.tsx +58 -2
- package/src/cli/serve/web/components/MenuButton.tsx +53 -0
- package/src/cli/serve/web/components/Omnibox.tsx +135 -0
- package/src/cli/serve/web/components/PromptEnhancer.tsx +476 -180
- package/src/cli/serve/web/components/SideMenu.tsx +414 -0
- package/src/cli/serve/web/components/TooltipLayer.tsx +46 -0
- package/src/cli/serve/web/components/VarIcon.tsx +24 -0
- package/src/cli/serve/web/components/VarsForm.tsx +223 -351
- package/src/cli/serve/web/components/controls/BasicControls.tsx +57 -32
- package/src/cli/serve/web/components/controls/LorasControl.tsx +646 -317
- package/src/cli/serve/web/components/controls/PresetPicker.tsx +1 -1
- package/src/cli/serve/web/components/controls/PromptControl.tsx +279 -0
- package/src/cli/serve/web/components/controls/PromptEditor.tsx +113 -0
- package/src/cli/serve/web/components/controls/SeedControl.tsx +17 -10
- package/src/cli/serve/web/components/controls/SizeControl.tsx +111 -18
- package/src/cli/serve/web/components/modKey.ts +2 -0
- package/src/cli/serve/web/llm.ts +23 -12
- package/src/cli/serve/web/promptEditor/promptExtensions.ts +284 -0
- package/src/cli/serve/web/state/EnhancerSt.ts +431 -101
- package/src/cli/serve/web/state/FormSt.ts +214 -30
- package/src/cli/serve/web/state/OmniboxSt.ts +59 -0
- package/src/cli/serve/web/state/WebSt.ts +364 -80
- package/src/cli/serve/web/state/choiceButtons.ts +61 -0
- package/src/cli/serve/web/state/draftNames.ts +16 -0
- package/src/cli/serve/web/state/draftTabs.ts +75 -0
- package/src/cli/serve/web/state/generateButton.ts +16 -0
- package/src/cli/serve/web/state/history.ts +54 -0
- package/src/cli/serve/web/state/host.ts +5 -2
- package/src/cli/serve/web/state/keptLines.ts +53 -0
- package/src/cli/serve/web/state/latentMode.ts +20 -0
- package/src/cli/serve/web/state/loraSort.ts +18 -0
- package/src/cli/serve/web/state/moduleTree.ts +2 -2
- package/src/cli/serve/web/state/omnibox.ts +133 -0
- package/src/cli/serve/web/state/payload.ts +38 -58
- package/src/cli/serve/web/state/shortcuts.ts +103 -0
- package/src/cli/serve/web/state/sizeStars.ts +31 -0
- package/src/cli/serve/web/state/stableSlots.ts +21 -0
- package/src/cli/serve/web/state/varGroups.ts +24 -0
- package/src/cli/serve/web/styles.ts +653 -154
- package/src/cli/serve/webBundle.ts +54 -10
- package/src/cli/serve/workflowTags.ts +42 -0
- package/src/cli/serve/workspaceLabels.ts +23 -0
- package/src/cli/tui/components/PickerOverlays.tsx +9 -2
- package/src/cli/tui/imagePicker/ImagePickerSt.ts +1 -1
- package/src/cli/tui/state/DraftsSt.ts +28 -8
- package/src/cli/tui/state/EditorSt.ts +1 -1
- package/src/cli/tui/state/LorasSt.ts +1 -1
- package/src/cli/tui/state/PickerSt.ts +25 -7
- package/src/cli/tui/state/SettingsSt.ts +1 -1
- package/src/cli/tui/state/TuiSt.ts +10 -0
- package/src/coreExports.ts +21 -0
- package/src/host/ComfyHost.ts +17 -2
- package/src/host/ResilientWebsocket.ts +54 -4
- package/src/host/loraInfoCache.ts +6 -0
- package/src/runner/ComfyExecution.ts +60 -0
- package/src/runner/ComfyWorkflow.ts +4 -0
- package/src/runner/ComfyWsApi.ts +2 -0
- package/src/runner/MediaImage.ts +2 -2
- package/src/sdk-generator/ComfyUIObjectInfoTypes.ts +5 -5
- package/src/sdk-generator/comfyui-sdk-codegen.ts +1 -1
- package/src/state.ts +5 -0
- package/src/utils/copyName.ts +12 -0
- package/src/vars/ComfyVars.ts +337 -67
- package/src/vars/DefinedWorkflow.ts +47 -20
- package/src/vars/lanes.ts +238 -0
- package/src/vars/loraEntry.ts +101 -0
- package/src/vars/promptSyntax.ts +382 -0
- package/dist/LogsSt-qtFzOKpu.js.map +0 -1
- package/dist/run-serve-BK7BA2dn.js.map +0 -1
- package/dist/run-tui-CQyvMf0N.js.map +0 -1
- package/dist/schemaDrift-DjjEtwfx.js.map +0 -1
- package/dist/state--wrz0BQc.js.map +0 -1
- package/src/cli/serve/web/components/Sidebar.tsx +0 -66
|
@@ -9,11 +9,10 @@
|
|
|
9
9
|
// below are the sanctioned kind-narrowing family (agent/coding.md cast whitelist 6).
|
|
10
10
|
import type {
|
|
11
11
|
AnyVar,
|
|
12
|
-
|
|
12
|
+
AnyChoiceVar,
|
|
13
13
|
FloatVar,
|
|
14
14
|
ImageVar,
|
|
15
15
|
IntVar,
|
|
16
|
-
LoraStrength,
|
|
17
16
|
LorasVar,
|
|
18
17
|
PromptVar,
|
|
19
18
|
SeedVar,
|
|
@@ -21,17 +20,13 @@ import type {
|
|
|
21
20
|
TextVar,
|
|
22
21
|
ToggleVar,
|
|
23
22
|
} from 'src/vars/ComfyVars.ts'
|
|
23
|
+
import { isLoraLanes, isLorasInput, isPromptInput } from 'src/vars/lanes.ts'
|
|
24
|
+
import { isLoraStrength } from 'src/vars/loraEntry.ts'
|
|
24
25
|
|
|
25
26
|
function isFiniteNumber(x: unknown): x is number {
|
|
26
27
|
return typeof x === 'number' && Number.isFinite(x)
|
|
27
28
|
}
|
|
28
29
|
|
|
29
|
-
function isLoraStrength(x: unknown): x is LoraStrength {
|
|
30
|
-
if (typeof x === 'boolean') return true
|
|
31
|
-
if (isFiniteNumber(x)) return true
|
|
32
|
-
return Array.isArray(x) && x.length === 2 && isFiniteNumber(x[0]) && isFiniteNumber(x[1])
|
|
33
|
-
}
|
|
34
|
-
|
|
35
30
|
/** cap long option lists in error messages */
|
|
36
31
|
function listSome(items: readonly string[], cap: number = 20): string {
|
|
37
32
|
if (items.length <= cap) return items.join(', ')
|
|
@@ -49,10 +44,16 @@ export function applyVarPayload(
|
|
|
49
44
|
const name = varDef.name ?? varDef.label ?? varDef.kind
|
|
50
45
|
|
|
51
46
|
switch (varDef.kind) {
|
|
52
|
-
case 'text':
|
|
53
|
-
case 'prompt': {
|
|
47
|
+
case 'text': {
|
|
54
48
|
if (typeof raw !== 'string') return `var '${name}' expects a string`
|
|
55
|
-
;(varDef as TextVar
|
|
49
|
+
;(varDef as TextVar).set(raw)
|
|
50
|
+
return null
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
case 'prompt': {
|
|
54
|
+
if (!isPromptInput(raw))
|
|
55
|
+
return `var '${name}' expects a string or {"lanes": [{"name": string, "prompt": string, "active": boolean}]}`
|
|
56
|
+
;(varDef as PromptVar).set(raw)
|
|
56
57
|
return null
|
|
57
58
|
}
|
|
58
59
|
|
|
@@ -97,23 +98,50 @@ export function applyVarPayload(
|
|
|
97
98
|
}
|
|
98
99
|
|
|
99
100
|
case 'choice': {
|
|
100
|
-
const v = varDef as
|
|
101
|
-
|
|
101
|
+
const v = varDef as AnyChoiceVar<string>
|
|
102
|
+
const known = (x: unknown): x is string => typeof x === 'string' && v.choices.includes(x)
|
|
103
|
+
if (v.select === 'many') {
|
|
104
|
+
// one value is a list of one: a tab or a draft from when this was a single choice
|
|
105
|
+
if (known(raw)) {
|
|
106
|
+
v.set([raw])
|
|
107
|
+
return null
|
|
108
|
+
}
|
|
109
|
+
if (Array.isArray(raw) && raw.every(known)) {
|
|
110
|
+
v.set(v.choices.filter((c) => raw.includes(c)))
|
|
111
|
+
return null
|
|
112
|
+
}
|
|
113
|
+
return `var '${name}' expects a list of: ${listSome(v.choices)}`
|
|
114
|
+
}
|
|
115
|
+
if (v.select === 'zero-or-one') {
|
|
116
|
+
if (raw === null || known(raw)) {
|
|
117
|
+
v.set(raw)
|
|
118
|
+
return null
|
|
119
|
+
}
|
|
120
|
+
return `var '${name}' expects null or one of: ${listSome(v.choices)}`
|
|
121
|
+
}
|
|
122
|
+
if (known(raw)) {
|
|
123
|
+
v.set(raw)
|
|
124
|
+
return null
|
|
125
|
+
}
|
|
102
126
|
return `var '${name}' expects one of: ${listSome(v.choices)}`
|
|
103
127
|
}
|
|
104
128
|
|
|
105
129
|
case 'loras': {
|
|
106
130
|
const v = varDef as LorasVar<string>
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
const
|
|
131
|
+
const SHAPE = `{"<lora name>": false | true | strength | [model, clip] | {strength, off?, mute?}} or {"lanes": [{"name", "active", "loras": {…}}]}`
|
|
132
|
+
if (raw == null || typeof raw !== 'object' || Array.isArray(raw)) return `var '${name}' expects ${SHAPE}`
|
|
133
|
+
const records = isLoraLanes(raw) ? raw.lanes.map((l) => l.loras) : [raw as Record<string, unknown>]
|
|
110
134
|
const known = new Set<string>([...v.options, ...(opts.extraLoraOptions ?? [])])
|
|
111
|
-
const
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
135
|
+
for (const record of records) {
|
|
136
|
+
const unknownNames = Object.keys(record).filter((k) => !known.has(k))
|
|
137
|
+
if (unknownNames.length > 0)
|
|
138
|
+
return `var '${name}': unknown lora(s) ${unknownNames.join(', ')} — available: ${listSome([...known])}`
|
|
139
|
+
for (const [k, st] of Object.entries(record))
|
|
140
|
+
if (!isLoraStrength(st))
|
|
141
|
+
return `var '${name}': '${k}' must be false | true | number | [model, clip] | { strength: [model, clip], off?, mute? }`
|
|
142
|
+
}
|
|
143
|
+
if (!isLorasInput(raw)) return `var '${name}' expects ${SHAPE}`
|
|
144
|
+
v.set(raw) // every lane and entry validated just above
|
|
117
145
|
return null
|
|
118
146
|
}
|
|
119
147
|
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
// HTTP byte ranges on binary replies. WebKit (Safari, and a WKWebView app window) plays audio and
|
|
2
|
+
// video only from a server that answers `Range` with 206: without it a flac never starts there
|
|
3
|
+
import type { ServeReply } from 'src/cli/serve/ServeApp.ts'
|
|
4
|
+
|
|
5
|
+
/** one range, the only kind media players send: `bytes=a-b`, `bytes=a-`, `bytes=-n` */
|
|
6
|
+
function parseRange(header: string, size: number): { start: number; end: number } | 'unsatisfiable' | null {
|
|
7
|
+
const m = /^bytes=(\d*)-(\d*)$/.exec(header.trim())
|
|
8
|
+
if (m == null) return null
|
|
9
|
+
const a = m[1] ?? ''
|
|
10
|
+
const b = m[2] ?? ''
|
|
11
|
+
if (a === '' && b === '') return null
|
|
12
|
+
if (a === '') {
|
|
13
|
+
const n = Number(b)
|
|
14
|
+
if (n === 0) return 'unsatisfiable'
|
|
15
|
+
return { start: Math.max(0, size - n), end: size - 1 }
|
|
16
|
+
}
|
|
17
|
+
const start = Number(a)
|
|
18
|
+
const end = b === '' ? size - 1 : Math.min(Number(b), size - 1)
|
|
19
|
+
if (start >= size || start > end) return 'unsatisfiable'
|
|
20
|
+
return { start, end }
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** a whole 200 binary reply gets its length and `accept-ranges`; a range request gets 206 with
|
|
24
|
+
* those bytes, or 416 past the end. Text replies and errors pass through untouched */
|
|
25
|
+
export function withByteRange(reply: ServeReply, req: { method: string; range?: string }): ServeReply {
|
|
26
|
+
if (req.method !== 'GET' || reply.status !== 200 || typeof reply.body === 'string') return reply
|
|
27
|
+
const size = reply.body.length
|
|
28
|
+
const base = { ...reply.headers, 'accept-ranges': 'bytes' }
|
|
29
|
+
const range = req.range == null ? null : parseRange(req.range, size)
|
|
30
|
+
if (range == null) return { ...reply, headers: { ...base, 'content-length': String(size) } }
|
|
31
|
+
if (range === 'unsatisfiable')
|
|
32
|
+
return {
|
|
33
|
+
status: 416,
|
|
34
|
+
contentType: reply.contentType,
|
|
35
|
+
body: new Uint8Array(0),
|
|
36
|
+
headers: { ...base, 'content-range': `bytes */${size}` },
|
|
37
|
+
}
|
|
38
|
+
return {
|
|
39
|
+
status: 206,
|
|
40
|
+
contentType: reply.contentType,
|
|
41
|
+
body: reply.body.subarray(range.start, range.end + 1),
|
|
42
|
+
headers: {
|
|
43
|
+
...base,
|
|
44
|
+
'content-range': `bytes ${range.start}-${range.end}/${size}`,
|
|
45
|
+
'content-length': String(range.end - range.start + 1),
|
|
46
|
+
},
|
|
47
|
+
}
|
|
48
|
+
}
|
|
@@ -3,7 +3,8 @@
|
|
|
3
3
|
import { ansi } from 'src/utils/ansi.ts'
|
|
4
4
|
import type {
|
|
5
5
|
AnyVar,
|
|
6
|
-
|
|
6
|
+
AnyChoiceVar,
|
|
7
|
+
ChoiceSelect,
|
|
7
8
|
FloatVar,
|
|
8
9
|
ImageVar,
|
|
9
10
|
IntVar,
|
|
@@ -14,6 +15,7 @@ import type {
|
|
|
14
15
|
SizeVar,
|
|
15
16
|
TextVar,
|
|
16
17
|
VarKind,
|
|
18
|
+
VarUi,
|
|
17
19
|
} from 'src/vars/ComfyVars.ts'
|
|
18
20
|
import type { VarPreset } from 'src/vars/presets.ts'
|
|
19
21
|
import type { LoraTriggers } from 'src/host/loraInfoCache.ts'
|
|
@@ -24,6 +26,10 @@ export type VarDescriptor = {
|
|
|
24
26
|
/** what a POST payload accepts for this var, human-readable */
|
|
25
27
|
payload: string
|
|
26
28
|
default: unknown
|
|
29
|
+
/** choice vars: exactly one, one or none (value may be null), or many (value is a list) */
|
|
30
|
+
select?: ChoiceSelect
|
|
31
|
+
/** how the workflow wants this var to look (VarUi): icon, colors, description */
|
|
32
|
+
ui?: VarUi
|
|
27
33
|
choices?: readonly string[]
|
|
28
34
|
/** loras: the resolved option list */
|
|
29
35
|
options?: readonly string[]
|
|
@@ -48,6 +54,11 @@ export type VarDescriptor = {
|
|
|
48
54
|
/** loras: trigger words per option as the mirror knows them (words, fetched and empty, or
|
|
49
55
|
* civitai never asked). An option ABSENT from this record is not in the mirror at all */
|
|
50
56
|
optionTriggers?: Record<string, LoraTriggers>
|
|
57
|
+
/** prompt: tag completion is on (`v.prompt(…, { tags })`), with the insert rules. The list
|
|
58
|
+
* itself stays on the server: the editor asks `GET /tags/<module>/<var>?q=` */
|
|
59
|
+
tags?: { underscores: boolean; artistPrefix?: string }
|
|
60
|
+
/** prompt: false when the model ignores `(text:1.2)` weights */
|
|
61
|
+
weights?: false
|
|
51
62
|
/** text: this one wants a box, not a line (`v.text(…, { multiline: true })`) */
|
|
52
63
|
multiline?: boolean
|
|
53
64
|
/** text + prompt: named starting texts (`{ presets: { label: text } }`). NOT `presets`, which
|
|
@@ -56,6 +67,8 @@ export type VarDescriptor = {
|
|
|
56
67
|
min?: number
|
|
57
68
|
max?: number
|
|
58
69
|
presets?: SizePreset[]
|
|
70
|
+
/** size: preset labels starred by default (quick buttons before the list) */
|
|
71
|
+
starredPresets?: string[]
|
|
59
72
|
extensions?: readonly string[]
|
|
60
73
|
}
|
|
61
74
|
|
|
@@ -70,7 +83,13 @@ function rangeText(opts: { min?: number; max?: number }): string {
|
|
|
70
83
|
}
|
|
71
84
|
|
|
72
85
|
export function describeVar(varDef: AnyVar): VarDescriptor {
|
|
73
|
-
const base = {
|
|
86
|
+
const base = {
|
|
87
|
+
kind: varDef.kind,
|
|
88
|
+
label: varDef.label,
|
|
89
|
+
default: varDef.defaultValue,
|
|
90
|
+
// absent unless the workflow set something: most vars carry no looks
|
|
91
|
+
...(Object.keys(varDef.uiOpts).length > 0 ? { ui: varDef.uiOpts } : {}),
|
|
92
|
+
}
|
|
74
93
|
// kind, never instanceof: the cli bundle and the consumer's `comfy-ts` import hold
|
|
75
94
|
// different copies of every class (VarKind owns the WHY). Casts are the sanctioned
|
|
76
95
|
// kind-narrowing family (agent/coding.md cast whitelist 6)
|
|
@@ -79,8 +98,13 @@ export function describeVar(varDef: AnyVar): VarDescriptor {
|
|
|
79
98
|
const v = varDef as PromptVar
|
|
80
99
|
return {
|
|
81
100
|
...base,
|
|
82
|
-
payload: 'string ("//"
|
|
101
|
+
payload: 'string ("//" starts a comment, "- " lines = negative)',
|
|
83
102
|
textPresets: presetsOrUndefined(v.presets),
|
|
103
|
+
tags:
|
|
104
|
+
v.tags == null
|
|
105
|
+
? undefined
|
|
106
|
+
: { underscores: v.tags.underscores === true, artistPrefix: v.tags.artistPrefix },
|
|
107
|
+
weights: v.promptOpts.weights === false ? false : undefined,
|
|
84
108
|
}
|
|
85
109
|
}
|
|
86
110
|
case 'text': {
|
|
@@ -109,8 +133,15 @@ export function describeVar(varDef: AnyVar): VarDescriptor {
|
|
|
109
133
|
case 'toggle':
|
|
110
134
|
return { ...base, payload: 'true or false' }
|
|
111
135
|
case 'choice': {
|
|
112
|
-
const v = varDef as
|
|
113
|
-
|
|
136
|
+
const v = varDef as AnyChoiceVar<string>
|
|
137
|
+
const list = v.choices.join(' | ')
|
|
138
|
+
const payload =
|
|
139
|
+
v.select === 'many'
|
|
140
|
+
? `a list of: ${list}`
|
|
141
|
+
: v.select === 'zero-or-one'
|
|
142
|
+
? `null or one of: ${list}`
|
|
143
|
+
: `one of: ${list}`
|
|
144
|
+
return { ...base, payload, choices: v.choices, select: v.select }
|
|
114
145
|
}
|
|
115
146
|
case 'loras': {
|
|
116
147
|
const v = varDef as LorasVar<string>
|
|
@@ -124,7 +155,12 @@ export function describeVar(varDef: AnyVar): VarDescriptor {
|
|
|
124
155
|
}
|
|
125
156
|
case 'size': {
|
|
126
157
|
const v = varDef as SizeVar
|
|
127
|
-
return {
|
|
158
|
+
return {
|
|
159
|
+
...base,
|
|
160
|
+
payload: '{"width":W,"height":H} or "WxH" or a preset label',
|
|
161
|
+
presets: v.presets,
|
|
162
|
+
starredPresets: [...v.starred],
|
|
163
|
+
}
|
|
128
164
|
}
|
|
129
165
|
case 'image': {
|
|
130
166
|
const v = varDef as ImageVar
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
// one LLM config of the prompt enhancer: where the model runs and how it is asked. PURE (the
|
|
2
|
+
// browser bundle and serve both read it), every field runtime-checked, since the file is
|
|
3
|
+
// hand-editable. No api key here: keys stay in the browser (architecture item 13)
|
|
4
|
+
import { defaultBaseUrl, type ProviderId, type ReasoningEffort } from 'src/cli/serve/web/llm.ts'
|
|
5
|
+
|
|
6
|
+
export type LlmConfig = {
|
|
7
|
+
provider: ProviderId
|
|
8
|
+
baseUrl: string
|
|
9
|
+
model: string
|
|
10
|
+
effort: ReasoningEffort
|
|
11
|
+
/** hide models that report no reasoning support */
|
|
12
|
+
thinkingOnly: boolean
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export type LlmConfigEntry = { name: string; config: LlmConfig }
|
|
16
|
+
|
|
17
|
+
export function isProvider(raw: unknown): raw is ProviderId {
|
|
18
|
+
return raw === 'openrouter' || raw === 'openwebui' || raw === 'openai'
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function isEffort(raw: unknown): raw is ReasoningEffort {
|
|
22
|
+
return raw === 'off' || raw === 'low' || raw === 'medium' || raw === 'high'
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** explicit model ids, never an alias: behaviour and cost stay reproducible */
|
|
26
|
+
const DEFAULT_MODEL: Record<ProviderId, string> = {
|
|
27
|
+
openrouter: 'anthropic/claude-sonnet-5',
|
|
28
|
+
openwebui: '',
|
|
29
|
+
openai: '',
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** a stored file → a usable config: a half-written or hand-broken field degrades to its
|
|
33
|
+
* default, never breaks the modal */
|
|
34
|
+
export function normalizeLlmConfig(raw: unknown): LlmConfig {
|
|
35
|
+
const o = typeof raw === 'object' && raw !== null && !Array.isArray(raw) ? (raw as Record<string, unknown>) : {}
|
|
36
|
+
const provider = isProvider(o.provider) ? o.provider : 'openrouter'
|
|
37
|
+
return {
|
|
38
|
+
provider,
|
|
39
|
+
baseUrl: typeof o.baseUrl === 'string' && o.baseUrl.trim() !== '' ? o.baseUrl.trim() : defaultBaseUrl(provider),
|
|
40
|
+
model: typeof o.model === 'string' ? o.model : DEFAULT_MODEL[provider],
|
|
41
|
+
effort: isEffort(o.effort) ? o.effort : 'medium',
|
|
42
|
+
thinkingOnly: o.thinkingOnly !== false,
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** switching provider resets what belongs to the old one: an openrouter model id sent to a
|
|
47
|
+
* local box, or a local base url sent to openrouter, is never what anyone means */
|
|
48
|
+
export function withProvider(c: LlmConfig, provider: ProviderId): LlmConfig {
|
|
49
|
+
if (provider === c.provider) return c
|
|
50
|
+
return { ...c, provider, baseUrl: defaultBaseUrl(provider), model: DEFAULT_MODEL[provider] }
|
|
51
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
// the prompt enhancer's LLM configs as FILES: `.comfy-ts/llm-configs/<name>.json`, the drafts
|
|
2
|
+
// model. The FILENAME is the identity (a rename is a write plus a delete), validStoreName is the
|
|
3
|
+
// same gate the draft routes use. SERVE-ONLY: no workflow reads these
|
|
4
|
+
import { existsSync, mkdirSync, readdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs'
|
|
5
|
+
import { join } from 'pathe'
|
|
6
|
+
import { normalizeLlmConfig, type LlmConfig, type LlmConfigEntry } from 'src/cli/serve/llmConfigShape.ts'
|
|
7
|
+
import { validStoreName } from 'src/utils/safeName.ts'
|
|
8
|
+
|
|
9
|
+
const EXT = '.json'
|
|
10
|
+
|
|
11
|
+
export function llmConfigsDir(): string {
|
|
12
|
+
return comfyts.resolveFromLlmConfigs('')
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function llmConfigPath(name: string): string | null {
|
|
16
|
+
const safe = validStoreName(name)
|
|
17
|
+
return safe == null ? null : join(llmConfigsDir(), `${safe}${EXT}`)
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function listLlmConfigs(): LlmConfigEntry[] {
|
|
21
|
+
const dir = llmConfigsDir()
|
|
22
|
+
if (!existsSync(dir)) return []
|
|
23
|
+
const out: LlmConfigEntry[] = []
|
|
24
|
+
for (const file of readdirSync(dir).sort()) {
|
|
25
|
+
if (!file.endsWith(EXT)) continue
|
|
26
|
+
const name = file.slice(0, -EXT.length)
|
|
27
|
+
if (validStoreName(name) == null) continue
|
|
28
|
+
// one broken file is skipped loudly, the rest still list
|
|
29
|
+
try {
|
|
30
|
+
out.push({ name, config: normalizeLlmConfig(JSON.parse(readFileSync(join(dir, file), 'utf8'))) })
|
|
31
|
+
} catch (e) {
|
|
32
|
+
console.error(`[serve] llm config '${name}' unreadable, skipped:`, e)
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return out
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export function writeLlmConfig(name: string, config: LlmConfig): string | null {
|
|
39
|
+
const path = llmConfigPath(name)
|
|
40
|
+
if (path == null) return null
|
|
41
|
+
mkdirSync(llmConfigsDir(), { recursive: true })
|
|
42
|
+
writeFileSync(path, `${JSON.stringify(normalizeLlmConfig(config), null, 3)}\n`)
|
|
43
|
+
return path
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** a missing file is still a success: the caller asked for it to be gone, and it is */
|
|
47
|
+
export function deleteLlmConfig(name: string): boolean {
|
|
48
|
+
const path = llmConfigPath(name)
|
|
49
|
+
if (path == null) return false
|
|
50
|
+
if (existsSync(path)) rmSync(path)
|
|
51
|
+
return true
|
|
52
|
+
}
|
|
@@ -4,12 +4,13 @@ import { statSync } from 'node:fs'
|
|
|
4
4
|
import { createServer, type IncomingMessage, type ServerResponse } from 'node:http'
|
|
5
5
|
import { networkInterfaces } from 'node:os'
|
|
6
6
|
import { pathToFileURL } from 'node:url'
|
|
7
|
-
import { resolve } from 'pathe'
|
|
7
|
+
import { dirname, resolve } from 'pathe'
|
|
8
8
|
import { scanCflowFiles } from 'src/cli/tui/discoverWorkflows.ts'
|
|
9
9
|
import { findDefinedWorkflow } from 'src/cli/tui/findDefinedWorkflow.ts'
|
|
10
10
|
import { draftKeyForFile } from 'src/cli/tui/state/DraftsSt.ts'
|
|
11
11
|
import { describeVar, renderDescriptorLine } from 'src/cli/serve/describeVar.ts'
|
|
12
12
|
import { ServeApp, type ServeModule } from 'src/cli/serve/ServeApp.ts'
|
|
13
|
+
import { withByteRange } from 'src/cli/serve/byteRange.ts'
|
|
13
14
|
import { renderStartupLines } from 'src/cli/serve/startupPrint.ts'
|
|
14
15
|
import { loadOrBuildWebJs } from 'src/cli/serve/webBundle.ts'
|
|
15
16
|
import { colorsAvailable } from 'src/utils/ansi.ts'
|
|
@@ -82,7 +83,9 @@ export function makeRequestListener(app: ServeApp): (req: IncomingMessage, res:
|
|
|
82
83
|
accept: req.headers.accept,
|
|
83
84
|
body: Buffer.concat(chunks).toString('utf8'),
|
|
84
85
|
})
|
|
85
|
-
.then((
|
|
86
|
+
.then((whole) => {
|
|
87
|
+
const range = req.headers.range
|
|
88
|
+
const reply = withByteRange(whole, { method: req.method ?? 'GET', range })
|
|
86
89
|
res.writeHead(reply.status, { 'content-type': reply.contentType, ...reply.headers, ...cors })
|
|
87
90
|
res.end(reply.body)
|
|
88
91
|
})
|
|
@@ -138,7 +141,13 @@ export async function runServe(args: string[]): Promise<number> {
|
|
|
138
141
|
return 1
|
|
139
142
|
}
|
|
140
143
|
|
|
141
|
-
const app = new ServeApp(modules, {
|
|
144
|
+
const app = new ServeApp(modules, {
|
|
145
|
+
loadErrors,
|
|
146
|
+
webJs: loadOrBuildWebJs,
|
|
147
|
+
// the modules registered comfyts on import: its root holds `.comfy-ts/`
|
|
148
|
+
workspace: typeof comfyts === 'undefined' ? process.cwd() : comfyts.rootPath,
|
|
149
|
+
root: statSync(target).isDirectory() ? target : dirname(target),
|
|
150
|
+
})
|
|
142
151
|
printStartup(app, parsed.bind, parsed.port)
|
|
143
152
|
|
|
144
153
|
const server = createServer(makeRequestListener(app))
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
// a booru tag list in memory: parsed once per source, searched per keystroke. PURE (text in, hits
|
|
2
|
+
// out). Formats: the a1111 tagcomplete csv `name,category,count,"alias,alias"`, or one tag per line
|
|
3
|
+
|
|
4
|
+
export type TagEntry = { name: string; category: number | null; count: number; aliases: string[] }
|
|
5
|
+
export type TagHit = { name: string; category: number | null; count: number; alias?: string }
|
|
6
|
+
|
|
7
|
+
/** one csv line, double-quoted fields with `""` escapes */
|
|
8
|
+
function csvFields(line: string): string[] {
|
|
9
|
+
const out: string[] = []
|
|
10
|
+
let cur = ''
|
|
11
|
+
let quoted = false
|
|
12
|
+
for (let i = 0; i < line.length; i++) {
|
|
13
|
+
const ch = line[i]
|
|
14
|
+
if (quoted) {
|
|
15
|
+
if (ch === '"' && line[i + 1] === '"') {
|
|
16
|
+
cur += '"'
|
|
17
|
+
i++
|
|
18
|
+
} else if (ch === '"') quoted = false
|
|
19
|
+
else cur += ch
|
|
20
|
+
} else if (ch === '"') quoted = true
|
|
21
|
+
else if (ch === ',') {
|
|
22
|
+
out.push(cur)
|
|
23
|
+
cur = ''
|
|
24
|
+
} else cur += ch
|
|
25
|
+
}
|
|
26
|
+
out.push(cur)
|
|
27
|
+
return out
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const intOrNull = (s: string | undefined): number | null => (s != null && /^\d+$/.test(s.trim()) ? Number(s) : null)
|
|
31
|
+
|
|
32
|
+
export function parseTagList(text: string): TagEntry[] {
|
|
33
|
+
const out: TagEntry[] = []
|
|
34
|
+
for (const raw of text.split(/\r?\n/)) {
|
|
35
|
+
const line = raw.trim()
|
|
36
|
+
if (line === '' || line.startsWith('#')) continue
|
|
37
|
+
const f = csvFields(line)
|
|
38
|
+
const name = (f[0] ?? '').trim()
|
|
39
|
+
if (name === '') continue
|
|
40
|
+
// `name,count` (two columns) is a counted plain list; three and more is the tagcomplete shape
|
|
41
|
+
const twoCols = f.length === 2
|
|
42
|
+
out.push({
|
|
43
|
+
name,
|
|
44
|
+
category: twoCols ? null : intOrNull(f[1]),
|
|
45
|
+
count: (twoCols ? intOrNull(f[1]) : intOrNull(f[2])) ?? 0,
|
|
46
|
+
aliases: (f[3] ?? '')
|
|
47
|
+
.split(',')
|
|
48
|
+
.map((a) => a.trim())
|
|
49
|
+
.filter((a) => a !== ''),
|
|
50
|
+
})
|
|
51
|
+
}
|
|
52
|
+
return out
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** the searchable form: lowercase, spaces as underscores (the lists write `long_hair`) */
|
|
56
|
+
const key = (s: string): string => s.trim().toLowerCase().replace(/\s+/g, '_')
|
|
57
|
+
|
|
58
|
+
export class TagList {
|
|
59
|
+
private rows: { e: TagEntry; name: string; aliases: string[] }[]
|
|
60
|
+
constructor(public readonly entries: TagEntry[]) {
|
|
61
|
+
this.rows = entries.map((e) => ({ e, name: key(e.name), aliases: e.aliases.map(key) }))
|
|
62
|
+
this.rows.sort((a, b) => b.e.count - a.e.count)
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
static parse(text: string): TagList {
|
|
66
|
+
return new TagList(parseTagList(text))
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** prefix of the name first, then prefix of an alias (answered with the name it points to),
|
|
70
|
+
* then a word inside the name (`hair` finds `long_hair`), each by post count */
|
|
71
|
+
search(query: string, limit = 20): TagHit[] {
|
|
72
|
+
const q = key(query)
|
|
73
|
+
if (q === '') return []
|
|
74
|
+
const hits: TagHit[] = []
|
|
75
|
+
const seen = new Set<string>()
|
|
76
|
+
const take = (e: TagEntry, alias?: string): boolean => {
|
|
77
|
+
if (seen.has(e.name)) return false
|
|
78
|
+
seen.add(e.name)
|
|
79
|
+
hits.push(alias == null ? { name: e.name, category: e.category, count: e.count } : { ...pick(e), alias })
|
|
80
|
+
return hits.length >= limit
|
|
81
|
+
}
|
|
82
|
+
for (const r of this.rows) if (r.name.startsWith(q) && take(r.e)) return hits
|
|
83
|
+
for (const r of this.rows) {
|
|
84
|
+
const a = r.aliases.find((x) => x.startsWith(q))
|
|
85
|
+
if (a != null && take(r.e, a)) return hits
|
|
86
|
+
}
|
|
87
|
+
for (const r of this.rows) if (r.name.includes(`_${q}`) && take(r.e)) return hits
|
|
88
|
+
return hits
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const pick = (e: TagEntry): TagHit => ({ name: e.name, category: e.category, count: e.count })
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
// where a prompt's tag list comes from: `v.prompt(…, { tags })` names a url, serve reads it once and
|
|
2
|
+
// keeps it in memory. The list never ships with the code: a remote one is fetched on first use and
|
|
3
|
+
// kept under the cache dir, a local one is re-read when its file changes
|
|
4
|
+
import { existsSync, mkdirSync, readFileSync, statSync, writeFileSync } from 'node:fs'
|
|
5
|
+
import { homedir } from 'node:os'
|
|
6
|
+
import { fileURLToPath } from 'node:url'
|
|
7
|
+
import { dirname, isAbsolute, join, resolve } from 'pathe'
|
|
8
|
+
import { TagList } from 'src/cli/serve/tagList.ts'
|
|
9
|
+
import { sha1HexOfString } from 'src/utils/sha1.ts'
|
|
10
|
+
|
|
11
|
+
export type TagSourceRef = { kind: 'file'; path: string } | { kind: 'http'; url: string }
|
|
12
|
+
|
|
13
|
+
/** http(s) stays a url; `file://`, `~/`, absolute and relative (to the workflow file) become a path */
|
|
14
|
+
export function resolveTagSource(url: string, moduleFile: string): TagSourceRef {
|
|
15
|
+
if (/^https?:\/\//i.test(url)) return { kind: 'http', url }
|
|
16
|
+
if (url.startsWith('file://')) return { kind: 'file', path: fileURLToPath(url) }
|
|
17
|
+
if (url === '~' || url.startsWith('~/')) return { kind: 'file', path: join(homedir(), url.slice(1)) }
|
|
18
|
+
if (isAbsolute(url)) return { kind: 'file', path: url }
|
|
19
|
+
return { kind: 'file', path: resolve(dirname(moduleFile), url) }
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export class TagSources {
|
|
23
|
+
private files = new Map<string, { mtimeMs: number; list: TagList }>()
|
|
24
|
+
private remote = new Map<string, Promise<TagList>>()
|
|
25
|
+
|
|
26
|
+
constructor(
|
|
27
|
+
private p: {
|
|
28
|
+
/** where remote lists are kept between runs */
|
|
29
|
+
cacheDir: () => string
|
|
30
|
+
/** a capped download: a tag list url pointing at something huge must not exhaust serve */
|
|
31
|
+
fetchText: (url: string) => Promise<string>
|
|
32
|
+
},
|
|
33
|
+
) {}
|
|
34
|
+
|
|
35
|
+
async load(ref: TagSourceRef): Promise<TagList> {
|
|
36
|
+
if (ref.kind === 'file') return this.loadFile(ref.path)
|
|
37
|
+
const hit = this.remote.get(ref.url)
|
|
38
|
+
if (hit != null) return hit
|
|
39
|
+
const pending = this.loadRemote(ref.url)
|
|
40
|
+
this.remote.set(ref.url, pending)
|
|
41
|
+
// a failure is not remembered: the next keystroke retries (the network may be back)
|
|
42
|
+
pending.catch(() => this.remote.delete(ref.url))
|
|
43
|
+
return pending
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
private loadFile(path: string): TagList {
|
|
47
|
+
if (!existsSync(path)) throw new Error(`tag list not found: ${path}`)
|
|
48
|
+
const mtimeMs = statSync(path).mtimeMs
|
|
49
|
+
const cached = this.files.get(path)
|
|
50
|
+
if (cached != null && cached.mtimeMs === mtimeMs) return cached.list
|
|
51
|
+
const list = TagList.parse(readFileSync(path, 'utf8'))
|
|
52
|
+
if (list.entries.length === 0) throw new Error(`tag list is empty: ${path}`)
|
|
53
|
+
this.files.set(path, { mtimeMs, list })
|
|
54
|
+
return list
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
private async loadRemote(url: string): Promise<TagList> {
|
|
58
|
+
const file = join(this.p.cacheDir(), `${sha1HexOfString(url)}.txt`)
|
|
59
|
+
if (existsSync(file)) return TagList.parse(readFileSync(file, 'utf8'))
|
|
60
|
+
console.log(`[serve] fetching tag list ${url}`)
|
|
61
|
+
const text = await this.p.fetchText(url)
|
|
62
|
+
const list = TagList.parse(text)
|
|
63
|
+
if (list.entries.length === 0) throw new Error(`tag list is empty: ${url}`)
|
|
64
|
+
mkdirSync(dirname(file), { recursive: true })
|
|
65
|
+
writeFileSync(file, text)
|
|
66
|
+
console.log(`[serve] tag list ${url}: ${list.entries.length} tags, kept in ${file}`)
|
|
67
|
+
return list
|
|
68
|
+
}
|
|
69
|
+
}
|