comfy-ts 0.4.0 → 1.0.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.
Files changed (59) hide show
  1. package/README.md +349 -255
  2. package/dist/DefinedWorkflow-O3ynII3Y.js +598 -0
  3. package/dist/DefinedWorkflow-O3ynII3Y.js.map +1 -0
  4. package/dist/cli.js +3969 -128
  5. package/dist/cli.js.map +1 -1
  6. package/dist/index.cjs +1167 -172
  7. package/dist/index.cjs.map +1 -1
  8. package/dist/index.d.cts +401 -115
  9. package/dist/index.d.ts +401 -115
  10. package/dist/index.js +1159 -173
  11. package/dist/index.js.map +1 -1
  12. package/dist/{run-tui-SDSJo1Qf.js → run-tui-BFhba_oG.js} +132 -609
  13. package/dist/run-tui-BFhba_oG.js.map +1 -0
  14. package/examples/01-txt2img.cflow.ts +50 -0
  15. package/examples/02-img2img-upload.cflow.ts +66 -0
  16. package/examples/03-export-workflow-json.cflow.ts +56 -0
  17. package/examples/04-krea2-turbo-t2i.cflow.ts +125 -0
  18. package/examples/05-comfy-cloud.cflow.ts +67 -0
  19. package/examples/comfy-cloud/sdk.d.ts +122674 -0
  20. package/guide-for-agents.md +166 -0
  21. package/package.json +6 -1
  22. package/src/cli/comfy-ts-cli.ts +19 -2
  23. package/src/cli/gen.ts +21 -12
  24. package/src/cli/tui/components/TreePanel.tsx +15 -2
  25. package/src/cli/tui/discoverWorkflows.ts +79 -0
  26. package/src/cli/tui/run-tui.tsx +51 -23
  27. package/src/cli/tui/state/HostSt.ts +2 -3
  28. package/src/cli/tui/state/TreeSt.ts +30 -6
  29. package/src/cli/tui/state/TuiSt.ts +21 -3
  30. package/src/cli/tui/state/WorkflowsSt.ts +15 -1
  31. package/src/host/ComfyHost.ts +203 -77
  32. package/src/host/ComfyManager.ts +10 -9
  33. package/src/host/ComfyUploader.ts +1 -2
  34. package/src/host/ResilientWebsocket.ts +3 -1
  35. package/src/host/hostUrl.ts +59 -0
  36. package/src/host/loraManagerApi.ts +5 -4
  37. package/src/index.ts +20 -1
  38. package/src/litegraph/CanonicalWorkflow.ts +77 -0
  39. package/src/litegraph/LiteGraphGroup.ts +9 -7
  40. package/src/litegraph/LiteGraphJSON.ts +34 -13
  41. package/src/litegraph/LiteGraphLink.ts +32 -2
  42. package/src/litegraph/LiteGraphNode.ts +18 -33
  43. package/src/litegraph/LiteGraphNodeInput.ts +10 -8
  44. package/src/litegraph/LiteGraphNodeOutput.ts +2 -3
  45. package/src/litegraph/LiteGraphSubgraphDef.ts +74 -0
  46. package/src/litegraph/convertFlowToLiteGraphJSON.ts +4 -4
  47. package/src/litegraph/expandSubgraphs.ts +352 -0
  48. package/src/litegraph/normalizeWorkflow.ts +141 -0
  49. package/src/runner/ComfyExecution.ts +4 -5
  50. package/src/runner/ComfyWorkflow.ts +2 -2
  51. package/src/runner/wsBinaryFrame.ts +48 -0
  52. package/src/sdk-generator/Primitives.ts +6 -24
  53. package/src/sdk-generator/comfy-api-json.ts +3 -1
  54. package/src/sdk-generator/inputWidgetKind.ts +179 -0
  55. package/src/sdk-generator/litegraphToApiRequestPayload.ts +372 -96
  56. package/src/state.ts +13 -2
  57. package/dist/log-CdfgILde.js +0 -10
  58. package/dist/log-CdfgILde.js.map +0 -1
  59. package/dist/run-tui-SDSJo1Qf.js.map +0 -1
@@ -0,0 +1,179 @@
1
+ import type { NodeInputExt } from 'src/sdk-generator/comfyui-types.ts'
2
+ import { ComfyPrimitiveMapping } from 'src/sdk-generator/Primitives.ts'
3
+
4
+ /**
5
+ * Widget-ness of a schema input, decided from its CONFIG — never from a
6
+ * type-name whitelist (2026 object_info spells widget inputs as arbitrary
7
+ * strings backed by a frontend widget registry we cannot mirror; every
8
+ * registry spelling carries a config signal instead). Signal precedence and
9
+ * their upstream anchors: agent/architecture.md "Widget-ness is decided from
10
+ * the input CONFIG".
11
+ */
12
+ export type InputWidgetKind =
13
+ | { kind: 'slot' }
14
+ | { kind: 'widget'; consumes: 1 | 2; strictValues: boolean }
15
+ | { kind: 'dynamic-combo'; options: DynamicComboOption[]; defaultKey: string }
16
+ | { kind: 'dynamic-container'; instanceNames: string[]; requiredCount: number }
17
+
18
+ /** one raw `[type, opts]` input entry as spelled in object_info (dynamic-combo branches arrive in this raw form) */
19
+ export type RawInputSpecEntry = {
20
+ name: string
21
+ type: string | readonly unknown[]
22
+ opts: unknown
23
+ required: boolean
24
+ }
25
+
26
+ export type DynamicComboOption = { key: string; inputs: RawInputSpecEntry[] }
27
+
28
+ export const isRecord = (x: unknown): x is Record<string, unknown> =>
29
+ typeof x === 'object' && x != null && !Array.isArray(x)
30
+
31
+ /**
32
+ * seed phantom: a TRUTHY `control_after_generate` adds one consumed value.
33
+ * The frontend creates the control widget on truthiness, and real 2026 hosts
34
+ * spell it as `true` OR as a string mode like `'fixed'` (PrimitiveInt.value,
35
+ * SeedNode.seed). When the KEY is absent (2024 era) the INT-named-seed
36
+ * heuristic applies.
37
+ */
38
+ const controlConsumes = (o: Record<string, unknown> | null, typeName: string, name: string): 1 | 2 => {
39
+ if (o != null && 'control_after_generate' in o) return o['control_after_generate'] ? 2 : 1
40
+ if (typeName === 'INT' && (name === 'seed' || name === 'noise_seed')) return 2
41
+ return 1
42
+ }
43
+
44
+ const flattenInputSections = (inputs: Record<string, unknown>): RawInputSpecEntry[] => {
45
+ const out: RawInputSpecEntry[] = []
46
+ for (const section of ['required', 'optional'] as const) {
47
+ const sec = inputs[section]
48
+ if (!isRecord(sec)) continue
49
+ for (const [name, spec] of Object.entries(sec)) {
50
+ if (!Array.isArray(spec) || spec.length === 0) continue
51
+ const t: unknown = spec[0]
52
+ if (typeof t !== 'string' && !Array.isArray(t)) continue
53
+ out.push({ name, type: t, opts: spec[1], required: section === 'required' })
54
+ }
55
+ }
56
+ return out
57
+ }
58
+
59
+ /** null when the options array is NOT the dynamic `{key, inputs}` family (then it is a plain combo) */
60
+ const parseDynamicOptions = (options: unknown[]): DynamicComboOption[] | null => {
61
+ if (options.length === 0) return null
62
+ const out: DynamicComboOption[] = []
63
+ for (const entry of options) {
64
+ if (!isRecord(entry)) return null
65
+ const key = entry['key']
66
+ const inputs = entry['inputs']
67
+ if (typeof key !== 'string' || !isRecord(inputs)) return null
68
+ out.push({ key, inputs: flattenInputSections(inputs) })
69
+ }
70
+ return out
71
+ }
72
+
73
+ /**
74
+ * autogrow (`template` WITH a nested `input` section): instances are always
75
+ * link slots named `decl.<name_i>` (the backend force-inputs widget
76
+ * templates), the first `min` of them required WHEN the template input
77
+ * section is `required` — mirrors `_io.py Autogrow._expand_schema_for_dynamic`.
78
+ */
79
+ const parseContainer = (template: Record<string, unknown>): InputWidgetKind => {
80
+ const input = template['input']
81
+ const min = typeof template['min'] === 'number' ? template['min'] : 1
82
+ const prefix = typeof template['prefix'] === 'string' ? template['prefix'] : ''
83
+ const max = typeof template['max'] === 'number' ? template['max'] : 100
84
+ const rawNames = template['names']
85
+ const names = Array.isArray(rawNames)
86
+ ? rawNames.filter((n): n is string => typeof n === 'string')
87
+ : Array.from({ length: max }, (_, i) => `${prefix}${i}`)
88
+ const templateRequired = isRecord(input) && isRecord(input['required']) && Object.keys(input['required']).length > 0
89
+ return {
90
+ kind: 'dynamic-container',
91
+ instanceNames: names,
92
+ requiredCount: templateRequired ? Math.min(min, names.length) : 0,
93
+ }
94
+ }
95
+
96
+ export const classifyWidgetInput = (p: {
97
+ name: string
98
+ type: string | readonly unknown[]
99
+ opts: unknown
100
+ /** true when type is a generated `E_*` union name (parsed 2024-era enum) */
101
+ isEnumUnion?: boolean
102
+ }): InputWidgetKind => {
103
+ const o = isRecord(p.opts) ? p.opts : null
104
+ // 1. forceInput gates widget creation upstream, whatever the type says
105
+ if (o?.['forceInput'] === true) return { kind: 'slot' }
106
+ // 2. enum arrays / parsed E_ unions are combo widgets
107
+ if (Array.isArray(p.type) || p.isEnumUnion === true) {
108
+ const tn = typeof p.type === 'string' ? p.type : 'COMBO'
109
+ return { kind: 'widget', consumes: controlConsumes(o, tn, p.name), strictValues: true }
110
+ }
111
+ if (typeof p.type !== 'string') return { kind: 'slot' }
112
+ // 3. widgetType names the widget for multi-type unions (frontend: widgets.get(widgetType ?? type))
113
+ const widgetTypeName = typeof o?.['widgetType'] === 'string' ? o['widgetType'] : p.type
114
+ // 4. primitives
115
+ if (ComfyPrimitiveMapping[widgetTypeName] != null)
116
+ return { kind: 'widget', consumes: controlConsumes(o, widgetTypeName, p.name), strictValues: true }
117
+ // 5. options array: dynamic {key, inputs} family, else plain combo
118
+ const options = o?.['options']
119
+ if (Array.isArray(options)) {
120
+ const dynOptions = parseDynamicOptions(options)
121
+ if (dynOptions != null) {
122
+ const first = dynOptions[0]
123
+ if (first == null) return { kind: 'slot' } // unreachable: parseDynamicOptions rejects empty
124
+ const defaultKey = typeof o?.['default'] === 'string' ? o['default'] : first.key
125
+ return { kind: 'dynamic-combo', options: dynOptions, defaultKey }
126
+ }
127
+ return { kind: 'widget', consumes: controlConsumes(o, p.type, p.name), strictValues: true }
128
+ }
129
+ // 6. template WITH nested input = autogrow container; WITHOUT = matchtype slot typing
130
+ const template = o?.['template']
131
+ if (isRecord(template)) {
132
+ if (isRecord(template['input'])) return parseContainer(template)
133
+ return { kind: 'slot' }
134
+ }
135
+ // 7. socketless: a widget that never has a socket (IMAGECOMPARE, COLOR); value domain unknown
136
+ if (o?.['socketless'] === true) return { kind: 'widget', consumes: 1, strictValues: false }
137
+ return { kind: 'slot' }
138
+ }
139
+
140
+ export const classifySchemaInput = (input: NodeInputExt): InputWidgetKind =>
141
+ classifyWidgetInput({
142
+ name: input.nameInComfy,
143
+ type: input.typeName,
144
+ opts: input.opts,
145
+ isEnumUnion: input.isEnum,
146
+ })
147
+
148
+ /** how many widgets_values entries this schema input consumes (export path + positional walks) */
149
+ export const howManyWidgetValuesForThisSchemaType = (input: NodeInputExt): number => {
150
+ const kind = classifySchemaInput(input)
151
+ return kind.kind === 'widget' ? kind.consumes : 0
152
+ }
153
+
154
+ /**
155
+ * what ComfyUI's frontend gives a widget created with no serialized value
156
+ * (`getWidgetDefaultValue` semantics), used when a positional array is
157
+ * shorter than the schema's widget list. `null` for custom widgets — the
158
+ * backend accepts an explicit null but errors on an ABSENT required key.
159
+ */
160
+ export const defaultValueForWidget = (p: {
161
+ type: string
162
+ opts: unknown
163
+ /** resolved values when the type is a generated E_ union or an inline enum array */
164
+ enumValues?: readonly (string | boolean | number)[]
165
+ }): unknown => {
166
+ const o = isRecord(p.opts) ? p.opts : null
167
+ if (o != null && o['default'] !== undefined) return o['default']
168
+ const widgetTypeName = typeof o?.['widgetType'] === 'string' ? o['widgetType'] : p.type
169
+ if (widgetTypeName === 'INT' || widgetTypeName === 'FLOAT') return 0
170
+ if (widgetTypeName === 'BOOLEAN') return false
171
+ if (widgetTypeName === 'STRING') return ''
172
+ const options = o?.['options']
173
+ if (Array.isArray(options) && options.length > 0) {
174
+ const first: unknown = options[0]
175
+ if (typeof first === 'string' || typeof first === 'number' || typeof first === 'boolean') return first
176
+ }
177
+ if (p.enumValues != null && p.enumValues.length > 0) return p.enumValues[0]
178
+ return null
179
+ }