HEL-892: pin structural reference sources and record bounded comparison evidence
Co-authored-by: multica-agent <github@multica.ai>
This commit is contained in:
@@ -0,0 +1,92 @@
|
||||
# HEL-892 Primitive 与标准子图结构参考转换
|
||||
|
||||
本轮代码基于已审 `97f3a76f0ab5da110f4c3a7e79576627d7dedaf9`。受测代码提交 `1f779d53c4312068513c2bd6d513f01a3c9f3caf`;其后的交付提交只增加本报告、固定来源和检查证据。继续原 PR #25,分支 `agent/agent/a64a969a6a44`,base `main`。原 PR 开工时为 open、head 97f3a76;Multica 链接表为空,未重复建 PR。
|
||||
|
||||
这是参考算法增量,**不是整图可靠转换或执行验收**。生产 `convert`、库导入、registry 和 object_info 资格未放宽。本轮没有修改界面、worker、下载链或在线配置。
|
||||
|
||||
## 固定来源与适用条件
|
||||
|
||||
49 份原始源码/许可证保存在 `structure-reference/`;`sources.json` 记录每份原始 URL、完整提交、SHA256、获取时间、完整行数及重点 `rule_locations`。`source-verification.json` 是本轮按固定 raw URL 重新取回 49 份文件的哈希核验;没有安装、导入或执行上游代码。许可证原件分别随 frontend、ComfyUI、QwenASR 保存,均为 GPL v3 文本;规则在本仓库独立实现。
|
||||
|
||||
| 源图声明 | 官方 tag | API ref 返回的完整提交 |
|
||||
| --- | --- | --- |
|
||||
| case02 1.37.11 | v1.37.11 | 4bf6a1960028cc5cda57d48f9fb01a74dfaade43 |
|
||||
| case03 1.48.7 | v1.48.7 | 6d6af63c00f132cd25dc29307fc56bd2c094fa22 |
|
||||
| case04 1.53.6 | v1.53.6 | 7d533a4cd4c4904ffdb0537f6b80ae9793dd62f1 |
|
||||
| case07 1.48.6 | v1.48.6 | 7b4172a243e0d43f1679c0be1c307560cd83c00d |
|
||||
|
||||
四份 `tag-*.json` 保留官方 ref 原始响应,`versions.json` 可逐项核对;`discovery.json` 保留递归源码树检索的 URL、响应摘要和 SHA。不是用当前 main 推定历史实现。源图的版本声明不证明目标安装或收件 API 的版本。
|
||||
|
||||
关键调用链(均在上表固定提交中):
|
||||
|
||||
- 1.37.11 `src/extensions/core/widgetInputs.ts:43-86` 按目标输入 `widget.name` 找控件并复制 Primitive 的第一个值;`:148-214` 绑定首个目标配置,`:216-286` 构造控件及数值/combo 附加控制,`:315-365` 合并配置。`src/utils/nodeDefUtil.ts:22-31,90-143` 证明 STRING 类型合并与忽略 multiline/default/tooltip 等元数据的边界。
|
||||
- 1.37.11 `src/utils/executionUtil.ts:26-140`:先调用虚拟节点 applyToGraph,再遍历控件;检查的是 `widget.options.serialize`,不是 `widget.serialize`,随后解析实际连线。
|
||||
- 各版本 `ExecutableNodeDTO.ts` 构造器把实例路径和内部节点 ID 用 `:` 连接;1.48.7/1.53.6 的 `:106-125` 可直接核对。`resolveInput` 向外走输入边界,`_resolveSubgraphOutput` 向内走输出边界。`SubgraphNode.ts` 的 getInnerNodes 为同一定义的不同实例建立独立路径;准确位置见各文件 rule_locations。
|
||||
- 1.48.7 `LLink.ts:183-207,363-381` 明确接收列表式/对象式连线;另外两版也保存对应原件。新算法只接收这两种明确结构、标准 -10/-20 边界和非负整数局部 ID;其它形式不猜。
|
||||
- LoadAudio 使用 ComfyUI **v0.11.0** 精确 tag 的 `comfy_extras/nodes_audio.py:261-275`,frontend 1.37.11 `uploadAudio.ts:74-105,162-241` 和 `domWidget.ts:142-149,331-333,360-394`。普通文件输入、音频 DOM 与上传按钮分别处理,录音不支持。
|
||||
- QwenASR 的先前协议参考提交 3f9a652a 含新 `normalize_text`,不能套在源图的14控件上。本轮检索原仓库历史,固定更早 `b037d7be121111a1ba6f40a4d99f6b43ac6411b6` 的 `AILab_QwenASR.py:582-609,715-722` 与注册入口 `__init__.py:24-40`,覆盖14个旧控件及四个 STRING 出口。两份历史源码和检索响应均保留;不声称源图插件就是这个提交,目标/导出版本仍 unknown。
|
||||
|
||||
新 profile 显式假定这些固定版本的未修改控件构造。UE 扩展重写、动态 combo 选项是否存在、模型/媒体文件是否可用、目标前端和节点安装版本均未知。默认 core/AUX profile 没有新增 Primitive 或子图权限。
|
||||
|
||||
## 算法与失败关闭
|
||||
|
||||
`serialization_reference.py` 新增 `PRIMITIVE_PROFILE`(`comfy-frontend-1.37.11-qwen-b037d7be-reference`)。先深拷贝图并核对所有连线的双向记录;仅支持已经证明的 Qwen `hints` / `output_path` STRING 控件,按名字找固定布局位置。全部扇出目标都通过提供方/版本条件、布局、类型和输入/输出控件绑定核对后才复制值、移除虚拟节点及对应连线。数值/combo、额外 seed 控制、替换表达式、未知目标回调、经 Reroute 的 Primitive 绑定、孤儿、坏端口、冲突目标和错误模式均整图拒绝。没有从 API 反推值或字段,未增加 seed/动态执行语义。
|
||||
|
||||
LoadAudio 的参考输出保留 `audioUI: ""`:固定 frontend 的音频 DOM 只设置 `widget.serialize=false`,而 graphToPrompt 检查 `options.serialize`;DOM 无 getValue 时返回空字符串。已收 API 没有这个字段,**如实输出差异**。上传按钮则有 `options.serialize=false`,不输出。不能为了对齐收件而把这两个标记混为一谈。
|
||||
|
||||
`structure_reference.py` 是现有转换器的结构辅助模块,不是另一个执行器。显式的 1.48.7 / 1.53.6 / 1.48.6 structure profile:
|
||||
|
||||
1. 验证根图和全部定义的节点/连线 ID、实例/定义槽、输入输出互指及 -10/-20 边界;缺定义、重复节点/定义/连线、递归、歧义出口、悬空和局部循环都拒绝,未使用分支也校验。
|
||||
2. 以实例路径递归展开,保留内部节点类型和原控件,不消费子图实例的位置数组;通过边界分别向外/向内追踪最终端点,保留非零槽和同一定义多实例的隔离。
|
||||
3. 边界没有外部连线时,记录具体实例/边界槽/内部节点字段到 `unresolved_boundaries`,不按 widgets_values 的位置猜 promoted widget 值。结构结果只放在 `diagnostic_graph`;未知内部节点仍逐个报错,静音/旁路有具体错误,不用“存在子图”掩盖后续缺口。
|
||||
4. 再检查展开图循环。**结构 profile 始终 prompt=None**;它没有新版内部节点序列化契约。不能把图诊断当作部分生产 prompt。
|
||||
|
||||
## 真实六份对照
|
||||
|
||||
`audit_reference_conversion.py --profile structure-batch-reference` 复用原差分函数,按声明版本选择显式 profile。报告保留 class_type、字段值与类型、端点/槽、标题差异、API 额外字段、未支持节点及未解析边界;未产生整图候选时 whole_prompt_differences 是 null,不是“整图零差异”。所有 scalar 数据仍区分源值与 API 值。
|
||||
|
||||
| case | 新参考结果 | 实际比较范围 | 仍未解决 |
|
||||
| --- | --- | --- | --- |
|
||||
| 01 | 原 AUX 候选保留,22节点 | 34标量、23端点/槽相同;完整JSON仍有13标题差异 | 原有版本/环境/执行未知全部保留 |
|
||||
| 02 | Primitive 7 折叠到 9.hints,诊断图6节点,prompt=None | 6个 class_type相同;15标量与5连线相同;额外 audioUI空字符串差异;6标题差异 | 四个 PreviewAny;API额外 normalize_text=true;源插件/UE及目标未知 |
|
||||
| 03 | 根37展开为37:*;含2注释共14节点,prompt=None | 12个非注释 class_type、16条连线一致,含35.audio→37:43[0];12标题差异 | 8个无外部线的边界控件、12节点的当前版本序列化;尤其 SeedNode/KSampler附加控制和ComfySwitchNode语义 |
|
||||
| 04 | 根459展开为459:*;含2注释共18节点,prompt=None | 16个非注释 class_type、18条连线一致,含图像保存→459:457[0]及外部宽高/提示词进入内部;14标题差异 | 15个边界控件、16节点当前版本序列化;TextGenerate/缓存/开关和seed等不推定 |
|
||||
| 05 | 原有失败关闭 | 仅原 root 范围18端点核对,未增加能力 | 原版本/控件缺口保留 |
|
||||
| 07 | 结构校验拒绝,prompt=None | 不能声称标准结构展开成功;原根图对照仍保留39项中的33项差异/不支持 | 定义中节点241 outputs[0]引用655未互指;另有Set/Get、forLoop、模式等不在本轮规则内 |
|
||||
|
||||
case02 PreviewAny 的具体验证缺口:1.37.11 `previewAny.ts:20-63` 创建两个名为 preview 的展示控件和 previewMode;源图三项为 null,而收件 API 只有 source。不能从 API 没有字段反推它们的 serializer。这里没有擅自丢控件或添加默认值;逐节点仍报 reference_node_unsupported。STRING 正文、SRT 内容或输出路径均没有被改成可下载文件。
|
||||
|
||||
03/04 上表只证明声明范围内的结构和连线;没有比较未知位置数组对应的参数值,不证明字段值/完整 JSON/动态分支等价。既有源/API 值和版本差异仍在配套说明和旧证据,本轮没有清除。
|
||||
|
||||
## 验证与复现
|
||||
|
||||
在本 checkout 设置 `PYTHONPATH=calliope-backend/src`,使用已存在的 Python 环境,无新增项目依赖:
|
||||
|
||||
```bash
|
||||
python calliope-backend/scripts/verify_frontend_sources.py --source-dir docs/hel-892/structure-reference --fetch --output source-verification.json
|
||||
python calliope-backend/scripts/audit_reference_conversion.py --repo . --profile structure-batch-reference --output six-case-comparison.json
|
||||
python -m pytest calliope-backend/tests/test_structure_reference.py calliope-backend/tests/test_controlnet_reference.py calliope-backend/tests/test_reference_serialization.py calliope-backend/tests/test_ui2api_differential.py calliope-backend/tests/test_workflow_library.py calliope-backend/tests/test_supplied_companions.py calliope-backend/tests/test_delivery_scope.py calliope-backend/tests/test_output_protocol_references.py -q
|
||||
```
|
||||
|
||||
- 本次最终受测代码 `1f779d5`:**178 passed,1项第三方 Starlette 弃用警告**。含32项本轮专项、146项受影响既有回归。合成减图/嵌套图只验证算法行为,文件和测试明确标明;真实源文件与真实导出对照单列。
|
||||
- 专项覆盖字符串变化传播、名字绑定而非位置推断、扇出/类型冲突/坏绑定/额外控制/Reroute拒绝、嵌套和多实例隔离、非零槽、缺定义/重复ID/递归/歧义/悬空/局部循环拒绝、无外部线的未知边界值、mode与未知第三方继续阻断。
|
||||
- 五个改动 Python 文件 Ruff check 和 format check 通过。没有机械重跑全后台、前端或 worker;142及865/5/6仅历史记录,未称全仓全绿,未修无关基线失败。
|
||||
- `uv build --offline` 生成 wheel/sdist;包内三个转换模块与受测源码逐字节一致。最初旧环境缺 build/hatchling,改用已有 uv 的离线构建缓存完成,没有新增依赖或改锁文件。
|
||||
- 显式 AUX profile 的六份对照与上批在候选、原始差异、问题、字段、端点、未支持节点和生产状态上逐项相等,见 previous-profile-protection.json。
|
||||
- 隔离导入155/排除1、16 pending_check/139 needs_conversion;重导155 skipped。155条全停用,说明完成/执行验证时间戳全空;1217源具名值/3660位置控件/294配套值保留。源156份及原始7份API均逐字节等于已审基点,用户整行保护回归通过。记录在 import/reimport、document/companion审计摘要及protection.json。
|
||||
|
||||
## 逐文件改动与下一段
|
||||
|
||||
| 文件 | 改动 |
|
||||
| --- | --- |
|
||||
| `serialization_reference.py` | 显式旧Qwen/LoadAudio布局、Primitive具名字符串折叠;保留audioUI与API差异 |
|
||||
| `structure_reference.py` | 标准子图完整结构验证、实例作用域、双向边界解析和非执行诊断 |
|
||||
| `ui2api.py` | 接入显式profile;Conversion增加诊断图和未解边界;默认生产路径不使用新规则 |
|
||||
| `audit_reference_conversion.py` | 复用差分,新增本批profile选择及结构/标题/API额外字段报告 |
|
||||
| `test_structure_reference.py` | 32项有意义变形/真实文件对照/拒绝验证 |
|
||||
| `structure-reference/*` | 固定官方源码、tag响应、两版Qwen历史与许可证/来源清单 |
|
||||
| `structure-evidence/*`、本报告、README | 本次测试/来源/比较/构建/生产隔离证据 |
|
||||
|
||||
最小后续工作是补齐明确版本的 PreviewAny 控件序列化差异、子图 promoted widget 值解析与对应内部节点契约;case07先解释655的原图记录,再逐个处理Set/Get/循环/模式,不能丢分支后声称成功。本轮未要求用户补资料/节点快照/模型或开放机器,也未连接目标机、停TTS、启动ComfyUI、提交prompt、调用付费服务、写在线库、合并或部署。
|
||||
|
||||
全量145份UI可靠转换、155份完整说明与必要实跑仍未完成。请总管把本次实际代码增量交HEL-893审核,保持全量目标和运行资格限制。
|
||||
@@ -87,3 +87,4 @@ uv run --project calliope-backend --frozen python -m calliope.comfyui.library_cl
|
||||
- 新模块和新增测试 Ruff 通过;变更文件对照基线无新增 lint 问题,现有15项问题仍在(基线17项,见 lint-comparison.json),不能称全仓 lint 全绿。
|
||||
- 前端 `npm run check` 未通过:原有 `PromptPreviewModal.svelte:125` 调用 mutate 缺参数,另有2项警告。本轮没有前端源文件或依赖改动,未扩展范围去修此前问题。详细日志随交付附上。
|
||||
- 真实目标机只做一次 GET /object_info,连接拒绝;真实节点/模型核对、7类黄金文件差分、音频编码/SRT格式、真实生成和新界面均未验证。夹具回传的占位字节只证明传输落盘,不证明媒体编码正确。
|
||||
> 当前 Primitive/标准子图增量:见 `Primitive与标准子图结构参考转换.md`、`structure-reference/`、`structure-evidence/`。case02具名字符串已折叠;case03/04双向边界及实例复合ID已结构展开。PreviewAny、边界控件/内部序列化、case07坏连线等具体缺口保留,均不构成整图可靠转换或生产执行资格。本轮178项受影响回归通过,全量目标仍未完成。
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
Building source distribution...
|
||||
Building wheel from source distribution...
|
||||
Successfully built ../.local-evidence/structure-build/calliope-1.5.8.tar.gz
|
||||
Successfully built ../.local-evidence/structure-build/calliope-1.5.8-py3-none-any.whl
|
||||
@@ -0,0 +1,936 @@
|
||||
{
|
||||
"cases": [
|
||||
{
|
||||
"id": "01",
|
||||
"source_path": "example_ComfyUI_workflows/0-基础处理:图像合并-音频分离-视频截取合并-图像视频放大/ControlNet_aux_常用样例.json",
|
||||
"state": "pending_check",
|
||||
"adopted": true,
|
||||
"api_nodes": 22,
|
||||
"api_inputs": 57,
|
||||
"new_api_literal_params": 34,
|
||||
"field_comparison": {
|
||||
"unnamed_or_virtual_boundary_unverified": 34,
|
||||
"link_difference": 9,
|
||||
"link_equal": 14
|
||||
},
|
||||
"source_only_ids": [
|
||||
"31",
|
||||
"32",
|
||||
"24",
|
||||
"35"
|
||||
],
|
||||
"api_only_ids": [],
|
||||
"named_evidence_differences": [],
|
||||
"output_map": [
|
||||
{
|
||||
"class_type": "PreviewImage",
|
||||
"kind": "image",
|
||||
"label": "预览图像",
|
||||
"nodeId": "3"
|
||||
},
|
||||
{
|
||||
"class_type": "PreviewImage",
|
||||
"kind": "image",
|
||||
"label": "预览图像",
|
||||
"nodeId": "5"
|
||||
},
|
||||
{
|
||||
"class_type": "PreviewImage",
|
||||
"kind": "image",
|
||||
"label": "预览图像",
|
||||
"nodeId": "11"
|
||||
},
|
||||
{
|
||||
"class_type": "PreviewImage",
|
||||
"kind": "image",
|
||||
"label": "预览图像",
|
||||
"nodeId": "13"
|
||||
},
|
||||
{
|
||||
"class_type": "PreviewImage",
|
||||
"kind": "image",
|
||||
"label": "预览图像",
|
||||
"nodeId": "22"
|
||||
},
|
||||
{
|
||||
"class_type": "PreviewImage",
|
||||
"kind": "image",
|
||||
"label": "预览图像",
|
||||
"nodeId": "36"
|
||||
},
|
||||
{
|
||||
"class_type": "PreviewImage",
|
||||
"kind": "image",
|
||||
"label": "预览图像",
|
||||
"nodeId": "37"
|
||||
}
|
||||
],
|
||||
"kind": "image",
|
||||
"issues": [
|
||||
{
|
||||
"code": "companion_unverified",
|
||||
"message": "采用提供的配套API;源图差异、导出版本与真实执行尚未核验,保持停用"
|
||||
}
|
||||
],
|
||||
"general_conversion_verified": false,
|
||||
"execution_verified": false,
|
||||
"evidence_file": "01.json"
|
||||
},
|
||||
{
|
||||
"id": "02",
|
||||
"source_path": "example_ComfyUI_workflows/1-语音生成/Qwen3-ASR-Subtitle语音转字幕.json",
|
||||
"state": "pending_check",
|
||||
"adopted": true,
|
||||
"api_nodes": 6,
|
||||
"api_inputs": 21,
|
||||
"new_api_literal_params": 16,
|
||||
"field_comparison": {
|
||||
"link_equal": 5,
|
||||
"unnamed_or_virtual_boundary_unverified": 15,
|
||||
"link_difference": 1
|
||||
},
|
||||
"source_only_ids": [
|
||||
"7"
|
||||
],
|
||||
"api_only_ids": [],
|
||||
"named_evidence_differences": [],
|
||||
"output_map": [],
|
||||
"kind": "unknown",
|
||||
"issues": [
|
||||
{
|
||||
"code": "companion_unverified",
|
||||
"message": "采用提供的配套API;源图差异、导出版本与真实执行尚未核验,保持停用"
|
||||
}
|
||||
],
|
||||
"general_conversion_verified": false,
|
||||
"execution_verified": false,
|
||||
"evidence_file": "02.json"
|
||||
},
|
||||
{
|
||||
"id": "03",
|
||||
"source_path": "example_ComfyUI_workflows/2-音乐生成/minimax_music_文生音乐.json",
|
||||
"state": "pending_check",
|
||||
"adopted": true,
|
||||
"api_nodes": 12,
|
||||
"api_inputs": 40,
|
||||
"new_api_literal_params": 24,
|
||||
"field_comparison": {
|
||||
"named_equal": 16,
|
||||
"link_difference": 9,
|
||||
"link_equal": 15
|
||||
},
|
||||
"source_only_ids": [
|
||||
"39",
|
||||
"40",
|
||||
"37"
|
||||
],
|
||||
"api_only_ids": [],
|
||||
"named_evidence_differences": [
|
||||
{
|
||||
"node_id": "37:6",
|
||||
"field": "unet_name",
|
||||
"api_value": "MiniMax/minimax_music3_dit_fp16.safetensors",
|
||||
"api_type": "str",
|
||||
"named_evidence": {
|
||||
"source_value": "minimax_music3_dit_fp16.safetensors",
|
||||
"source_type": "str",
|
||||
"status": "different"
|
||||
},
|
||||
"source_link": [
|
||||
55,
|
||||
-10,
|
||||
4,
|
||||
6,
|
||||
0,
|
||||
"COMBO"
|
||||
],
|
||||
"source_expected": [
|
||||
"37:-10",
|
||||
4
|
||||
],
|
||||
"status": "link_difference"
|
||||
},
|
||||
{
|
||||
"node_id": "37:13",
|
||||
"field": "caption",
|
||||
"api_value": "Global Metadata: Lo-fi hip-hop, chillhop. 78 BPM, D flat major, major scale with jazzy extensions. Laid-back and dreamy throughout, a gentle warm drift with a subtle late-night glow that deepens in the middle and dissolves softly at the end. Studying, raining-outside, headphones-on late-night listening. Bedroom production: muddy warm texture, heavy vinyl crackle, tape hiss and wow-flutter pitch wobble, low-passed dusty mix, soft-clipped drums, everything slightly detuned and cozy.\n\nVocal Details: Soft androgynous vocal, hushed half-sung half-spoken delivery, sitting low in the mix like another instrument, lazy behind-the-beat phrasing, gentle breathy timbre. Sparse murmured double-tracked harmonies, occasional wordless \"mmm\" and \"ooh\" hums drenched in tape delay and warm spring reverb. Long stretches with no vocals at all.\n\nArrangement: Dusty boom-bap drums with a soft thumping kick, cracked snare with lazy swing, brushed hi-hats, low round sub bass. Warm Rhodes piano chords with slow chorus wobble as the harmonic bed, mellow jazzy guitar licks answering the vocal lines, constant vinyl crackle as texture. Intro: rain and vinyl noise, solo Rhodes chords fading in, drums slipping in halfway. Verses: minimal — drums, bass, Rhodes, soft guitar fills between lines. Instrumental sections: guitar and Rhodes trade relaxed jazzy phrases over the beat, occasional muted trumpet ghost notes far in the background. Bridge: drums drop away to rain, crackle, and floating detuned Rhodes, then the beat eases back in. Outro: elements fade one by one until only vinyl crackle and a last unresolved Rhodes chord remain.",
|
||||
"api_type": "str",
|
||||
"named_evidence": {
|
||||
"source_value": "",
|
||||
"source_type": "str",
|
||||
"status": "different"
|
||||
},
|
||||
"source_link": [
|
||||
45,
|
||||
-10,
|
||||
0,
|
||||
13,
|
||||
1,
|
||||
"STRING"
|
||||
],
|
||||
"source_expected": [
|
||||
"37:-10",
|
||||
0
|
||||
],
|
||||
"status": "link_difference"
|
||||
},
|
||||
{
|
||||
"node_id": "37:13",
|
||||
"field": "lyrics",
|
||||
"api_value": "[Intro]\nMmm...\n(rain on the window)\nOoh...\n\n[Verse]\nMidnight and the canvas glows\nDragging little wires where the current flows\nType a quiet dream, let the sampler drift\nNoise into a picture, like the fog just lifts\nTwenty slow steps, I'm in no hurry now\nLatents turning colors and I don't know how\nEvery render's like a polaroid I found\nSoft focus memories, no sound\n\n[Instrumental]\n\n[Verse]\nQueue another frame, let the motion breathe\nPictures start to move like the falling leaves\nVideo drifting by at twenty-four\nLittle animations on my bedroom floor\nSeed after seed like the rain outside\nSome of them are keepers, some I let slide\nSave the ones that feel like a Sunday slow\nNode to node to node... and off we go\n\n[Chorus]\nMmm... let it render on\n(take your time, take your time)\nOoh... by the morning it'll all be done\n(one more queue, one more try)\n\n[Instrumental]\n\n[Bridge]\nRain keeps drawing pictures on the glass...\nMy machine keeps dreaming...\nNeither of us fast...\n\n[Chorus]\nMmm... let it render on\n(take your time, take your time)\nOoh... by the morning it'll all be done\n(one more queue, one more try)\n\n[Instrumental]\n\n[Outro]\nMmm...\n(node to node)\nOoh... goodnight",
|
||||
"api_type": "str",
|
||||
"named_evidence": {
|
||||
"source_value": "",
|
||||
"source_type": "str",
|
||||
"status": "different"
|
||||
},
|
||||
"source_link": [
|
||||
46,
|
||||
-10,
|
||||
1,
|
||||
13,
|
||||
2,
|
||||
"STRING"
|
||||
],
|
||||
"source_expected": [
|
||||
"37:-10",
|
||||
1
|
||||
],
|
||||
"status": "link_difference"
|
||||
},
|
||||
{
|
||||
"node_id": "37:13",
|
||||
"field": "seed",
|
||||
"api_value": [
|
||||
"37:38",
|
||||
0
|
||||
],
|
||||
"api_type": "list",
|
||||
"named_evidence": {
|
||||
"source_value": 222,
|
||||
"source_type": "int",
|
||||
"status": "different"
|
||||
},
|
||||
"source_link": [
|
||||
52,
|
||||
38,
|
||||
0,
|
||||
13,
|
||||
3,
|
||||
"INT"
|
||||
],
|
||||
"source_expected": [
|
||||
"37:38",
|
||||
0
|
||||
],
|
||||
"status": "link_equal"
|
||||
},
|
||||
{
|
||||
"node_id": "37:3",
|
||||
"field": "clip_name",
|
||||
"api_value": "MiniMax/minimax_music3_text_encoder_pruned_int8_convrot.safetensors",
|
||||
"api_type": "str",
|
||||
"named_evidence": {
|
||||
"source_value": "minimax_music3_text_encoder_pruned_int8_convrot.safetensors",
|
||||
"source_type": "str",
|
||||
"status": "different"
|
||||
},
|
||||
"source_link": [
|
||||
56,
|
||||
-10,
|
||||
5,
|
||||
3,
|
||||
0,
|
||||
"COMBO"
|
||||
],
|
||||
"source_expected": [
|
||||
"37:-10",
|
||||
5
|
||||
],
|
||||
"status": "link_difference"
|
||||
},
|
||||
{
|
||||
"node_id": "37:7",
|
||||
"field": "vae_name",
|
||||
"api_value": "MiniMax/minimax_music3_dav.safetensors",
|
||||
"api_type": "str",
|
||||
"named_evidence": {
|
||||
"source_value": "minimax_music3_dav.safetensors",
|
||||
"source_type": "str",
|
||||
"status": "different"
|
||||
},
|
||||
"source_link": [
|
||||
57,
|
||||
-10,
|
||||
6,
|
||||
7,
|
||||
0,
|
||||
"COMBO"
|
||||
],
|
||||
"source_expected": [
|
||||
"37:-10",
|
||||
6
|
||||
],
|
||||
"status": "link_difference"
|
||||
},
|
||||
{
|
||||
"node_id": "37:15",
|
||||
"field": "seconds",
|
||||
"api_value": [
|
||||
"37:13",
|
||||
1
|
||||
],
|
||||
"api_type": "list",
|
||||
"named_evidence": {
|
||||
"source_value": 120,
|
||||
"source_type": "int",
|
||||
"status": "different"
|
||||
},
|
||||
"source_link": [
|
||||
20,
|
||||
13,
|
||||
1,
|
||||
15,
|
||||
0,
|
||||
"FLOAT"
|
||||
],
|
||||
"source_expected": [
|
||||
"37:13",
|
||||
1
|
||||
],
|
||||
"status": "link_equal"
|
||||
},
|
||||
{
|
||||
"node_id": "37:9",
|
||||
"field": "seed",
|
||||
"api_value": [
|
||||
"37:38",
|
||||
0
|
||||
],
|
||||
"api_type": "list",
|
||||
"named_evidence": {
|
||||
"source_value": 1111111112,
|
||||
"source_type": "int",
|
||||
"status": "different"
|
||||
},
|
||||
"source_link": [
|
||||
53,
|
||||
38,
|
||||
0,
|
||||
9,
|
||||
4,
|
||||
"INT"
|
||||
],
|
||||
"source_expected": [
|
||||
"37:38",
|
||||
0
|
||||
],
|
||||
"status": "link_equal"
|
||||
},
|
||||
{
|
||||
"node_id": "37:38",
|
||||
"field": "seed",
|
||||
"api_value": 617970236276137,
|
||||
"api_type": "int",
|
||||
"named_evidence": {
|
||||
"source_value": 0,
|
||||
"source_type": "int",
|
||||
"status": "different"
|
||||
},
|
||||
"source_link": [
|
||||
54,
|
||||
-10,
|
||||
3,
|
||||
38,
|
||||
0,
|
||||
"INT"
|
||||
],
|
||||
"source_expected": [
|
||||
"37:-10",
|
||||
3
|
||||
],
|
||||
"status": "link_difference"
|
||||
},
|
||||
{
|
||||
"node_id": "37:43",
|
||||
"field": "switch",
|
||||
"api_value": true,
|
||||
"api_type": "bool",
|
||||
"named_evidence": {
|
||||
"source_value": false,
|
||||
"source_type": "bool",
|
||||
"status": "different"
|
||||
},
|
||||
"source_link": [
|
||||
63,
|
||||
-10,
|
||||
7,
|
||||
43,
|
||||
2,
|
||||
"BOOLEAN"
|
||||
],
|
||||
"source_expected": [
|
||||
"37:-10",
|
||||
7
|
||||
],
|
||||
"status": "link_difference"
|
||||
}
|
||||
],
|
||||
"output_map": [],
|
||||
"kind": "unknown",
|
||||
"issues": [
|
||||
{
|
||||
"code": "companion_unverified",
|
||||
"message": "采用提供的配套API;源图差异、导出版本与真实执行尚未核验,保持停用"
|
||||
}
|
||||
],
|
||||
"general_conversion_verified": false,
|
||||
"execution_verified": false,
|
||||
"evidence_file": "03.json"
|
||||
},
|
||||
{
|
||||
"id": "04",
|
||||
"source_path": "example_ComfyUI_workflows/3-图像生成/Qwen-Image-2.1/Qwen_image2.1_文生图_提示词增强.json",
|
||||
"state": "pending_check",
|
||||
"adopted": true,
|
||||
"api_nodes": 16,
|
||||
"api_inputs": 60,
|
||||
"new_api_literal_params": 42,
|
||||
"field_comparison": {
|
||||
"named_equal": 27,
|
||||
"link_difference": 20,
|
||||
"link_equal": 13
|
||||
},
|
||||
"source_only_ids": [
|
||||
"459",
|
||||
"463",
|
||||
"468"
|
||||
],
|
||||
"api_only_ids": [],
|
||||
"named_evidence_differences": [
|
||||
{
|
||||
"node_id": "459:451",
|
||||
"field": "unet_name",
|
||||
"api_value": "Qwen-Image/qwen_image_2.1_bf16.safetensors",
|
||||
"api_type": "str",
|
||||
"named_evidence": {
|
||||
"source_value": "qwen_image_2.1_int8_convrot.safetensors",
|
||||
"source_type": "str",
|
||||
"status": "different"
|
||||
},
|
||||
"source_link": [
|
||||
663,
|
||||
-10,
|
||||
11,
|
||||
451,
|
||||
0,
|
||||
"COMBO"
|
||||
],
|
||||
"source_expected": [
|
||||
"459:-10",
|
||||
11
|
||||
],
|
||||
"status": "link_difference"
|
||||
},
|
||||
{
|
||||
"node_id": "459:452",
|
||||
"field": "prompt",
|
||||
"api_value": [
|
||||
"459:472",
|
||||
0
|
||||
],
|
||||
"api_type": "list",
|
||||
"named_evidence": {
|
||||
"source_value": "",
|
||||
"source_type": "str",
|
||||
"status": "different"
|
||||
},
|
||||
"source_link": [
|
||||
687,
|
||||
472,
|
||||
0,
|
||||
452,
|
||||
3,
|
||||
"STRING"
|
||||
],
|
||||
"source_expected": [
|
||||
"459:472",
|
||||
0
|
||||
],
|
||||
"status": "link_equal"
|
||||
},
|
||||
{
|
||||
"node_id": "459:456",
|
||||
"field": "width",
|
||||
"api_value": [
|
||||
"13",
|
||||
0
|
||||
],
|
||||
"api_type": "list",
|
||||
"named_evidence": {
|
||||
"source_value": 1024,
|
||||
"source_type": "int",
|
||||
"status": "different"
|
||||
},
|
||||
"source_link": [
|
||||
660,
|
||||
-10,
|
||||
6,
|
||||
456,
|
||||
0,
|
||||
"INT"
|
||||
],
|
||||
"source_expected": [
|
||||
"459:-10",
|
||||
6
|
||||
],
|
||||
"status": "link_difference"
|
||||
},
|
||||
{
|
||||
"node_id": "459:456",
|
||||
"field": "height",
|
||||
"api_value": [
|
||||
"13",
|
||||
1
|
||||
],
|
||||
"api_type": "list",
|
||||
"named_evidence": {
|
||||
"source_value": 1024,
|
||||
"source_type": "int",
|
||||
"status": "different"
|
||||
},
|
||||
"source_link": [
|
||||
661,
|
||||
-10,
|
||||
7,
|
||||
456,
|
||||
1,
|
||||
"INT"
|
||||
],
|
||||
"source_expected": [
|
||||
"459:-10",
|
||||
7
|
||||
],
|
||||
"status": "link_difference"
|
||||
},
|
||||
{
|
||||
"node_id": "459:458",
|
||||
"field": "seed",
|
||||
"api_value": 447606998181263,
|
||||
"api_type": "int",
|
||||
"named_evidence": {
|
||||
"source_value": 0,
|
||||
"source_type": "int",
|
||||
"status": "different"
|
||||
},
|
||||
"source_link": [
|
||||
667,
|
||||
-10,
|
||||
10,
|
||||
458,
|
||||
4,
|
||||
"INT"
|
||||
],
|
||||
"source_expected": [
|
||||
"459:-10",
|
||||
10
|
||||
],
|
||||
"status": "link_difference"
|
||||
},
|
||||
{
|
||||
"node_id": "459:471",
|
||||
"field": "prompt",
|
||||
"api_value": [
|
||||
"483",
|
||||
0
|
||||
],
|
||||
"api_type": "list",
|
||||
"named_evidence": {
|
||||
"source_value": "",
|
||||
"source_type": "str",
|
||||
"status": "different"
|
||||
},
|
||||
"source_link": [
|
||||
711,
|
||||
-10,
|
||||
0,
|
||||
471,
|
||||
5,
|
||||
"STRING"
|
||||
],
|
||||
"source_expected": [
|
||||
"459:-10",
|
||||
0
|
||||
],
|
||||
"status": "link_difference"
|
||||
},
|
||||
{
|
||||
"node_id": "459:471",
|
||||
"field": "sampling_mode.seed",
|
||||
"api_value": 447606998181263,
|
||||
"api_type": "int",
|
||||
"named_evidence": {
|
||||
"source_value": 0,
|
||||
"source_type": "int",
|
||||
"status": "different"
|
||||
},
|
||||
"source_link": [
|
||||
697,
|
||||
-10,
|
||||
10,
|
||||
471,
|
||||
13,
|
||||
"INT"
|
||||
],
|
||||
"source_expected": [
|
||||
"459:-10",
|
||||
10
|
||||
],
|
||||
"status": "link_difference"
|
||||
},
|
||||
{
|
||||
"node_id": "459:473",
|
||||
"field": "clip_name",
|
||||
"api_value": "Qwen-Image/qwen3.5_qwen_image_2.1_pe_t2i.int8_convrot.safetensors",
|
||||
"api_type": "str",
|
||||
"named_evidence": {
|
||||
"source_value": "qwen3.5_9b_qwen_image_2.1_pe_t2i.int8_convrot.safetensors",
|
||||
"source_type": "str",
|
||||
"status": "different"
|
||||
},
|
||||
"source_link": [
|
||||
696,
|
||||
-10,
|
||||
14,
|
||||
473,
|
||||
0,
|
||||
"COMBO"
|
||||
],
|
||||
"source_expected": [
|
||||
"459:-10",
|
||||
14
|
||||
],
|
||||
"status": "link_difference"
|
||||
},
|
||||
{
|
||||
"node_id": "459:474",
|
||||
"field": "switch",
|
||||
"api_value": true,
|
||||
"api_type": "bool",
|
||||
"named_evidence": {
|
||||
"source_value": false,
|
||||
"source_type": "bool",
|
||||
"status": "different"
|
||||
},
|
||||
"source_link": [
|
||||
688,
|
||||
-10,
|
||||
1,
|
||||
474,
|
||||
2,
|
||||
"BOOLEAN"
|
||||
],
|
||||
"source_expected": [
|
||||
"459:-10",
|
||||
1
|
||||
],
|
||||
"status": "link_difference"
|
||||
}
|
||||
],
|
||||
"output_map": [
|
||||
{
|
||||
"class_type": "SaveImageAdvanced",
|
||||
"kind": "image",
|
||||
"label": "保存图像(高级)",
|
||||
"nodeId": "461"
|
||||
}
|
||||
],
|
||||
"kind": "image",
|
||||
"issues": [
|
||||
{
|
||||
"code": "companion_unverified",
|
||||
"message": "采用提供的配套API;源图差异、导出版本与真实执行尚未核验,保持停用"
|
||||
}
|
||||
],
|
||||
"general_conversion_verified": false,
|
||||
"execution_verified": false,
|
||||
"evidence_file": "04.json"
|
||||
},
|
||||
{
|
||||
"id": "05",
|
||||
"source_path": "example_ComfyUI_workflows/4-视频生成/Minimax-h3/MiniMax_H3_导演台时间线素材_双采样潜空间放大_多分段拼接.json",
|
||||
"state": "pending_check",
|
||||
"adopted": true,
|
||||
"api_nodes": 16,
|
||||
"api_inputs": 66,
|
||||
"new_api_literal_params": 48,
|
||||
"field_comparison": {
|
||||
"named_difference": 5,
|
||||
"named_equal": 43,
|
||||
"link_equal": 18
|
||||
},
|
||||
"source_only_ids": [
|
||||
"25"
|
||||
],
|
||||
"api_only_ids": [],
|
||||
"named_evidence_differences": [
|
||||
{
|
||||
"node_id": "1",
|
||||
"field": "unet_name",
|
||||
"api_value": "MiniMax/minimax_h3_ref2va_pruned_int8_convrot.safetensors",
|
||||
"api_type": "str",
|
||||
"named_evidence": {
|
||||
"source_value": "MiniMax\\minimax_h3_ref2va_pruned_int8_convrot.safetensors",
|
||||
"source_type": "str",
|
||||
"status": "different"
|
||||
},
|
||||
"source_value": "MiniMax\\minimax_h3_ref2va_pruned_int8_convrot.safetensors",
|
||||
"status": "named_difference"
|
||||
},
|
||||
{
|
||||
"node_id": "2",
|
||||
"field": "clip_name",
|
||||
"api_value": "MiniMax/qwen3vl_32b_minimax_h3_nvfp4_awq.safetensors",
|
||||
"api_type": "str",
|
||||
"named_evidence": {
|
||||
"source_value": "MiniMax\\qwen3vl_32b_minimax_h3_nvfp4_awq.safetensors",
|
||||
"source_type": "str",
|
||||
"status": "different"
|
||||
},
|
||||
"source_value": "MiniMax\\qwen3vl_32b_minimax_h3_nvfp4_awq.safetensors",
|
||||
"status": "named_difference"
|
||||
},
|
||||
{
|
||||
"node_id": "3",
|
||||
"field": "vae_name",
|
||||
"api_value": "MiniMax/minimax_h3_video_vae_fp16.safetensors",
|
||||
"api_type": "str",
|
||||
"named_evidence": {
|
||||
"source_value": "MiniMax\\minimax_h3_video_vae_fp16.safetensors",
|
||||
"source_type": "str",
|
||||
"status": "different"
|
||||
},
|
||||
"source_value": "MiniMax\\minimax_h3_video_vae_fp16.safetensors",
|
||||
"status": "named_difference"
|
||||
},
|
||||
{
|
||||
"node_id": "4",
|
||||
"field": "vae_name",
|
||||
"api_value": "MiniMax/minimax_h3_audio_vae_fp32.safetensors",
|
||||
"api_type": "str",
|
||||
"named_evidence": {
|
||||
"source_value": "MiniMax\\minimax_h3_audio_vae_fp32.safetensors",
|
||||
"source_type": "str",
|
||||
"status": "different"
|
||||
},
|
||||
"source_value": "MiniMax\\minimax_h3_audio_vae_fp32.safetensors",
|
||||
"status": "named_difference"
|
||||
},
|
||||
{
|
||||
"node_id": "7",
|
||||
"field": "width",
|
||||
"api_value": [
|
||||
"24",
|
||||
0
|
||||
],
|
||||
"api_type": "list",
|
||||
"named_evidence": {
|
||||
"source_value": 1536,
|
||||
"source_type": "int",
|
||||
"status": "different"
|
||||
},
|
||||
"source_link": [
|
||||
44,
|
||||
24,
|
||||
0,
|
||||
7,
|
||||
2,
|
||||
"INT"
|
||||
],
|
||||
"source_expected": [
|
||||
"24",
|
||||
0
|
||||
],
|
||||
"status": "link_equal"
|
||||
},
|
||||
{
|
||||
"node_id": "7",
|
||||
"field": "height",
|
||||
"api_value": [
|
||||
"24",
|
||||
1
|
||||
],
|
||||
"api_type": "list",
|
||||
"named_evidence": {
|
||||
"source_value": 960,
|
||||
"source_type": "int",
|
||||
"status": "different"
|
||||
},
|
||||
"source_link": [
|
||||
45,
|
||||
24,
|
||||
1,
|
||||
7,
|
||||
3,
|
||||
"INT"
|
||||
],
|
||||
"source_expected": [
|
||||
"24",
|
||||
1
|
||||
],
|
||||
"status": "link_equal"
|
||||
},
|
||||
{
|
||||
"node_id": "21",
|
||||
"field": "lora_name",
|
||||
"api_value": "MiniMax/minimax_h3_ref2v_turbo_8step_v1.0_768p_comfyui_bf16.safetensors",
|
||||
"api_type": "str",
|
||||
"named_evidence": {
|
||||
"source_value": "MiniMax\\minimax_h3_ref2v_turbo_8step_v1.0_768p_comfyui_bf16.safetensors",
|
||||
"source_type": "str",
|
||||
"status": "different"
|
||||
},
|
||||
"source_value": "MiniMax\\minimax_h3_ref2v_turbo_8step_v1.0_768p_comfyui_bf16.safetensors",
|
||||
"status": "named_difference"
|
||||
}
|
||||
],
|
||||
"output_map": [
|
||||
{
|
||||
"class_type": "SaveVideo",
|
||||
"kind": "video",
|
||||
"label": "保存视频",
|
||||
"nodeId": "10"
|
||||
}
|
||||
],
|
||||
"kind": "video",
|
||||
"issues": [
|
||||
{
|
||||
"code": "companion_unverified",
|
||||
"message": "采用提供的配套API;源图差异、导出版本与真实执行尚未核验,保持停用"
|
||||
}
|
||||
],
|
||||
"general_conversion_verified": false,
|
||||
"execution_verified": false,
|
||||
"evidence_file": "05.json"
|
||||
},
|
||||
{
|
||||
"id": "06",
|
||||
"source_path": "example_ComfyUI_workflows/5-数字人/Wan2.1_I2V_InfiniteTalk_rCM_数字人对口型多动态(KJ版).json",
|
||||
"state": "user_excluded",
|
||||
"adopted": false,
|
||||
"new_api_literal_params": 0,
|
||||
"exclusion": {
|
||||
"status": "user_excluded",
|
||||
"case_id": "06",
|
||||
"reason": "用户明确排除这一份,不补导、不修补,不作为剩余阻塞",
|
||||
"decision": {
|
||||
"issue_id": "01a17910-fd9c-72aa-9e76-331f0cfdf896",
|
||||
"comment_id": "01a187e6-5df9-7c98-8afe-05fc9baa5b55",
|
||||
"issue_link": "mention://issue/01a17910-fd9c-72aa-9e76-331f0cfdf896",
|
||||
"quote": "缺的那份不需要了"
|
||||
}
|
||||
},
|
||||
"historical_row_preserved": false
|
||||
},
|
||||
{
|
||||
"id": "07",
|
||||
"source_path": "example_ComfyUI_workflows/6-动作迁移/Wan_Animate2_动作迁移_for循环长视频.json",
|
||||
"state": "pending_check",
|
||||
"adopted": true,
|
||||
"api_nodes": 72,
|
||||
"api_inputs": 251,
|
||||
"new_api_literal_params": 130,
|
||||
"field_comparison": {
|
||||
"unnamed_or_virtual_boundary_unverified": 92,
|
||||
"link_difference": 57,
|
||||
"link_equal": 93,
|
||||
"named_difference": 1,
|
||||
"named_equal": 7,
|
||||
"source_node_unresolved": 1
|
||||
},
|
||||
"source_only_ids": [
|
||||
"574",
|
||||
"575",
|
||||
"292",
|
||||
"582",
|
||||
"291",
|
||||
"291:78",
|
||||
"291:77",
|
||||
"291:90",
|
||||
"291:80",
|
||||
"291:290",
|
||||
"291:96",
|
||||
"291:79",
|
||||
"291:97",
|
||||
"291:416",
|
||||
"291:417",
|
||||
"291:571",
|
||||
"291:572",
|
||||
"291:573",
|
||||
"581",
|
||||
"539",
|
||||
"600",
|
||||
"603",
|
||||
"602",
|
||||
"604",
|
||||
"605",
|
||||
"587",
|
||||
"597",
|
||||
"477",
|
||||
"477:466",
|
||||
"598",
|
||||
"261",
|
||||
"261:241",
|
||||
"608",
|
||||
"609",
|
||||
"540",
|
||||
"601",
|
||||
"610",
|
||||
"606"
|
||||
],
|
||||
"api_only_ids": [
|
||||
"612"
|
||||
],
|
||||
"named_evidence_differences": [
|
||||
{
|
||||
"node_id": "585",
|
||||
"field": "video",
|
||||
"api_value": "抖音20261025-285806.mp4",
|
||||
"api_type": "str",
|
||||
"named_evidence": {
|
||||
"source_value": "1daafaf7f9a12f651c41e56d38bc518f.mp4",
|
||||
"source_type": "str",
|
||||
"status": "different"
|
||||
},
|
||||
"source_value": "1daafaf7f9a12f651c41e56d38bc518f.mp4",
|
||||
"status": "named_difference"
|
||||
}
|
||||
],
|
||||
"output_map": [
|
||||
{
|
||||
"class_type": "SaveVideo",
|
||||
"kind": "video",
|
||||
"label": "保存视频",
|
||||
"nodeId": "246"
|
||||
}
|
||||
],
|
||||
"kind": "video",
|
||||
"issues": [
|
||||
{
|
||||
"code": "companion_unverified",
|
||||
"message": "采用提供的配套API;源图差异、导出版本与真实执行尚未核验,保持停用"
|
||||
}
|
||||
],
|
||||
"general_conversion_verified": false,
|
||||
"execution_verified": false,
|
||||
"evidence_file": "07.json"
|
||||
}
|
||||
],
|
||||
"adopted": 6,
|
||||
"user_excluded": 1,
|
||||
"source_files": 156,
|
||||
"target_files": 155,
|
||||
"general_conversion_verified": 0,
|
||||
"execution_verified": 0,
|
||||
"new_api_literal_params": 294
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
{
|
||||
"total": 155,
|
||||
"audit_scope": "current_delivery",
|
||||
"source_files": 156,
|
||||
"target_files": 155,
|
||||
"user_excluded": 1,
|
||||
"source_structure_purpose_partially_reviewed": 155,
|
||||
"full_documents_completed": 0,
|
||||
"files_with_selected_named_fields_reviewed": 41,
|
||||
"selected_named_fields_reviewed": 1217,
|
||||
"real_executions": 0,
|
||||
"states": {
|
||||
"pending_check": 16,
|
||||
"needs_conversion": 139
|
||||
},
|
||||
"named_values_preserved": 1217,
|
||||
"unnamed_widget_nodes": 3660,
|
||||
"semantic_batch_files": 139,
|
||||
"semantic_batch_without_named_fields": 114,
|
||||
"semantic_batch_named_fields": 490,
|
||||
"named_values_without_manual_field_review": 0,
|
||||
"named_values_without_verified_units_ranges_effects": 1217,
|
||||
"scope_limit": "人工结论与机械原值清单分开;源语义局部审校非完整说明定稿"
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
{"batch_id": "ef77ea9c-f798-4db6-88b8-b6a5cfc3d26e", "source_commit": "97f3a76f0ab5da110f4c3a7e79576627d7dedaf9", "counts": {"imported": 155, "user_excluded": 1}, "states": {"pending_check": 16, "needs_conversion": 139}, "source_total": 156, "excluded_total": 1, "target_total": 155, "total": 156, "doc_completed": 0, "exec_verified": 0}
|
||||
@@ -0,0 +1,2 @@
|
||||
All checks passed!
|
||||
5 files already formatted
|
||||
@@ -0,0 +1,17 @@
|
||||
{
|
||||
"tested_code_commit": "1f779d53c4312068513c2bd6d513f01a3c9f3caf",
|
||||
"wheel_matches_code": [
|
||||
{
|
||||
"file": "ui2api.py",
|
||||
"sha256": "793a74cff1923e80883d7b1e45ce3e0d08c88a9b194f0702f1cdf0d28e8c2589"
|
||||
},
|
||||
{
|
||||
"file": "serialization_reference.py",
|
||||
"sha256": "1d36316c5619471a13964f13a883a87dc01e445e028d1f653db6b5cd9086a509"
|
||||
},
|
||||
{
|
||||
"file": "structure_reference.py",
|
||||
"sha256": "5bf992902afedd52c91fca31d6d29e29c112e973b112f6754ddb39da9684c7fd"
|
||||
}
|
||||
]
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,80 @@
|
||||
[
|
||||
{
|
||||
"id": "01",
|
||||
"unchanged_fields": [
|
||||
"whole_prompt_available",
|
||||
"whole_prompt_differences",
|
||||
"whole_prompt_issues",
|
||||
"serialized_fields",
|
||||
"topology_edges",
|
||||
"unsupported_widget_nodes",
|
||||
"production_prompt_available"
|
||||
],
|
||||
"all_equal": true
|
||||
},
|
||||
{
|
||||
"id": "02",
|
||||
"unchanged_fields": [
|
||||
"whole_prompt_available",
|
||||
"whole_prompt_differences",
|
||||
"whole_prompt_issues",
|
||||
"serialized_fields",
|
||||
"topology_edges",
|
||||
"unsupported_widget_nodes",
|
||||
"production_prompt_available"
|
||||
],
|
||||
"all_equal": true
|
||||
},
|
||||
{
|
||||
"id": "03",
|
||||
"unchanged_fields": [
|
||||
"whole_prompt_available",
|
||||
"whole_prompt_differences",
|
||||
"whole_prompt_issues",
|
||||
"serialized_fields",
|
||||
"topology_edges",
|
||||
"unsupported_widget_nodes",
|
||||
"production_prompt_available"
|
||||
],
|
||||
"all_equal": true
|
||||
},
|
||||
{
|
||||
"id": "04",
|
||||
"unchanged_fields": [
|
||||
"whole_prompt_available",
|
||||
"whole_prompt_differences",
|
||||
"whole_prompt_issues",
|
||||
"serialized_fields",
|
||||
"topology_edges",
|
||||
"unsupported_widget_nodes",
|
||||
"production_prompt_available"
|
||||
],
|
||||
"all_equal": true
|
||||
},
|
||||
{
|
||||
"id": "05",
|
||||
"unchanged_fields": [
|
||||
"whole_prompt_available",
|
||||
"whole_prompt_differences",
|
||||
"whole_prompt_issues",
|
||||
"serialized_fields",
|
||||
"topology_edges",
|
||||
"unsupported_widget_nodes",
|
||||
"production_prompt_available"
|
||||
],
|
||||
"all_equal": true
|
||||
},
|
||||
{
|
||||
"id": "07",
|
||||
"unchanged_fields": [
|
||||
"whole_prompt_available",
|
||||
"whole_prompt_differences",
|
||||
"whole_prompt_issues",
|
||||
"serialized_fields",
|
||||
"topology_edges",
|
||||
"unsupported_widget_nodes",
|
||||
"production_prompt_available"
|
||||
],
|
||||
"all_equal": true
|
||||
}
|
||||
]
|
||||
@@ -0,0 +1,22 @@
|
||||
{
|
||||
"tested_code_commit": "1f779d53c4312068513c2bd6d513f01a3c9f3caf",
|
||||
"source_baseline_commit": "97f3a76f0ab5da110f4c3a7e79576627d7dedaf9",
|
||||
"original_bytes": {
|
||||
"source": {
|
||||
"files": 156,
|
||||
"all_equal_to_base": true,
|
||||
"set_sha256": "703c2841e752d62b9f4b04f2358053beb3e071ac7a3f717e59adf0d29606afab"
|
||||
},
|
||||
"received_api": {
|
||||
"files": 7,
|
||||
"all_equal_to_base": true,
|
||||
"set_sha256": "416f67420e4b633956136c61381a0e957e50cf59b21ec38ed51d8f5b035ceab3"
|
||||
}
|
||||
},
|
||||
"isolated_rows": 155,
|
||||
"disabled_rows": 155,
|
||||
"null_completion_and_execution_timestamps": true,
|
||||
"production_registry_and_import_unchanged": true,
|
||||
"user_whole_row_protection": "test_workflow_library.py and test_delivery_scope.py regression passed",
|
||||
"new_reference_not_used_by_production_import": true
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
{"batch_id": "233df731-fb45-4949-a6de-0f15eb1d9699", "source_commit": "97f3a76f0ab5da110f4c3a7e79576627d7dedaf9", "counts": {"skipped": 155, "user_excluded": 1}, "states": {"pending_check": 16, "needs_conversion": 139}, "source_total": 156, "excluded_total": 1, "target_total": 155, "total": 156, "doc_completed": 0, "exec_verified": 0}
|
||||
File diff suppressed because one or more lines are too long
@@ -0,0 +1,203 @@
|
||||
{
|
||||
"authority": "public_reference_only",
|
||||
"sources_verified": [
|
||||
{
|
||||
"id": "1.37.11:LICENSE",
|
||||
"sha256": "3972dc9744f6499f0f9b2dbf76696f2ae7ad8af9b23dde66d6af86c9dfb36986"
|
||||
},
|
||||
{
|
||||
"id": "1.37.11:src/extensions/core/widgetInputs.ts",
|
||||
"sha256": "488d38eb209816778a6da8cb8f734f539e3fbd7f77aae41ec649a52b8764b613"
|
||||
},
|
||||
{
|
||||
"id": "1.37.11:src/utils/executionUtil.ts",
|
||||
"sha256": "4e51bc54e35cb7ac9bb7cba418a74ccdd5974f640b9b088aa4ae7668f746ab36"
|
||||
},
|
||||
{
|
||||
"id": "1.37.11:src/lib/litegraph/src/subgraph/ExecutableNodeDTO.ts",
|
||||
"sha256": "fd2e19a6b8f4369e6e97d79f407cc1bec130feec9d6d3c979cc03b0a556f249f"
|
||||
},
|
||||
{
|
||||
"id": "1.37.11:src/lib/litegraph/src/subgraph/Subgraph.ts",
|
||||
"sha256": "321403ca3ba2faa0b448dec6e8bf379e49ddea4bfbe8619547d29926e30d62d2"
|
||||
},
|
||||
{
|
||||
"id": "1.37.11:src/lib/litegraph/src/subgraph/SubgraphNode.ts",
|
||||
"sha256": "d4f1017495158a6133ff770441a4d517dfbf2a328ba42b0405f8729853fe8209"
|
||||
},
|
||||
{
|
||||
"id": "1.37.11:src/lib/litegraph/src/LLink.ts",
|
||||
"sha256": "1d58845322b30f122aeafb23ca286ca7349ca23215469f96afb7a98fd6596af4"
|
||||
},
|
||||
{
|
||||
"id": "1.48.7:LICENSE",
|
||||
"sha256": "3972dc9744f6499f0f9b2dbf76696f2ae7ad8af9b23dde66d6af86c9dfb36986"
|
||||
},
|
||||
{
|
||||
"id": "1.48.7:src/extensions/core/widgetInputs.ts",
|
||||
"sha256": "fdceceedc727e8a696f273a810fc39a075004a09ea793e0df51418f96ac52a61"
|
||||
},
|
||||
{
|
||||
"id": "1.48.7:src/utils/executionUtil.ts",
|
||||
"sha256": "264e11395f28880603e7f9fe36d2e61c74cd59d6d88be990225cce8510e3f816"
|
||||
},
|
||||
{
|
||||
"id": "1.48.7:src/lib/litegraph/src/subgraph/ExecutableNodeDTO.ts",
|
||||
"sha256": "64baa71b46bd67440b032e18e4d9aebb67a6cc135fe7c4509ae1b5bc25acc2a3"
|
||||
},
|
||||
{
|
||||
"id": "1.48.7:src/lib/litegraph/src/subgraph/Subgraph.ts",
|
||||
"sha256": "321403ca3ba2faa0b448dec6e8bf379e49ddea4bfbe8619547d29926e30d62d2"
|
||||
},
|
||||
{
|
||||
"id": "1.48.7:src/lib/litegraph/src/subgraph/SubgraphNode.ts",
|
||||
"sha256": "822b0cdde3fe6a096f82310075a6285ae967dc58d760f19d743442ea20f4f15f"
|
||||
},
|
||||
{
|
||||
"id": "1.48.7:src/lib/litegraph/src/LLink.ts",
|
||||
"sha256": "9e9c92acacd5edc58a9c1ad2b3ba66679f27ebda28da74765947d88ce11df656"
|
||||
},
|
||||
{
|
||||
"id": "1.53.6:LICENSE",
|
||||
"sha256": "3972dc9744f6499f0f9b2dbf76696f2ae7ad8af9b23dde66d6af86c9dfb36986"
|
||||
},
|
||||
{
|
||||
"id": "1.53.6:src/extensions/core/widgetInputs.ts",
|
||||
"sha256": "f6b3d33b9ffdf4f68136ee0f72d03dabf4affa3292721d1f221b47de792e0daf"
|
||||
},
|
||||
{
|
||||
"id": "1.53.6:src/utils/executionUtil.ts",
|
||||
"sha256": "b29fd312e66c7415fa4c720654ac121dc67600b0c1fedf079b6575a174960698"
|
||||
},
|
||||
{
|
||||
"id": "1.53.6:src/lib/litegraph/src/subgraph/ExecutableNodeDTO.ts",
|
||||
"sha256": "0c0ca487822a9f86324925c2fb4ea59c6c032ad5d042a83ac367cb15196454fe"
|
||||
},
|
||||
{
|
||||
"id": "1.53.6:src/lib/litegraph/src/subgraph/Subgraph.ts",
|
||||
"sha256": "321403ca3ba2faa0b448dec6e8bf379e49ddea4bfbe8619547d29926e30d62d2"
|
||||
},
|
||||
{
|
||||
"id": "1.53.6:src/lib/litegraph/src/subgraph/SubgraphNode.ts",
|
||||
"sha256": "29f2da27396bcc426180d819549f654278d65de973ade651d57c4905e319fb9c"
|
||||
},
|
||||
{
|
||||
"id": "1.53.6:src/lib/litegraph/src/LLink.ts",
|
||||
"sha256": "562694d5bccb5682222c500a7e7069501eeb94e69eeb1b9a534d1e511902ef57"
|
||||
},
|
||||
{
|
||||
"id": "1.48.6:LICENSE",
|
||||
"sha256": "3972dc9744f6499f0f9b2dbf76696f2ae7ad8af9b23dde66d6af86c9dfb36986"
|
||||
},
|
||||
{
|
||||
"id": "1.48.6:src/extensions/core/widgetInputs.ts",
|
||||
"sha256": "fdceceedc727e8a696f273a810fc39a075004a09ea793e0df51418f96ac52a61"
|
||||
},
|
||||
{
|
||||
"id": "1.48.6:src/utils/executionUtil.ts",
|
||||
"sha256": "264e11395f28880603e7f9fe36d2e61c74cd59d6d88be990225cce8510e3f816"
|
||||
},
|
||||
{
|
||||
"id": "1.48.6:src/lib/litegraph/src/subgraph/ExecutableNodeDTO.ts",
|
||||
"sha256": "64baa71b46bd67440b032e18e4d9aebb67a6cc135fe7c4509ae1b5bc25acc2a3"
|
||||
},
|
||||
{
|
||||
"id": "1.48.6:src/lib/litegraph/src/subgraph/Subgraph.ts",
|
||||
"sha256": "321403ca3ba2faa0b448dec6e8bf379e49ddea4bfbe8619547d29926e30d62d2"
|
||||
},
|
||||
{
|
||||
"id": "1.48.6:src/lib/litegraph/src/subgraph/SubgraphNode.ts",
|
||||
"sha256": "822b0cdde3fe6a096f82310075a6285ae967dc58d760f19d743442ea20f4f15f"
|
||||
},
|
||||
{
|
||||
"id": "1.48.6:src/lib/litegraph/src/LLink.ts",
|
||||
"sha256": "9e9c92acacd5edc58a9c1ad2b3ba66679f27ebda28da74765947d88ce11df656"
|
||||
},
|
||||
{
|
||||
"id": "Comfy-Org/ComfyUI_frontend:src/extensions/core/previewAny.ts",
|
||||
"sha256": "2b19989ac8a3c26e198c7db7f7e2210936b0a4f4d3b26183917baafb841c1874"
|
||||
},
|
||||
{
|
||||
"id": "Comfy-Org/ComfyUI_frontend:src/extensions/core/uploadAudio.ts",
|
||||
"sha256": "0a11f1ad5651d238130bc06aca2a4b31a41f8bec98d0f8512e02ffb42b262e66"
|
||||
},
|
||||
{
|
||||
"id": "Comfy-Org/ComfyUI_frontend:src/scripts/widgets.ts",
|
||||
"sha256": "e8bb428cbf69635015de43a4ebc89817b3443f876cbed52eeafe67d26f2398d8"
|
||||
},
|
||||
{
|
||||
"id": "Comfy-Org/ComfyUI_frontend:src/scripts/app.ts",
|
||||
"sha256": "e57985f7d365abb035bc2fc755a8f5b10efbca97a7dcb5b2a4a05bcbc78b9d5c"
|
||||
},
|
||||
{
|
||||
"id": "1038lab/ComfyUI-QwenASR:AILab_QwenASR.py",
|
||||
"sha256": "f8cda2d0d39517f93ace1a73263146a4bea59a02604e58919a05393c45ade6b7"
|
||||
},
|
||||
{
|
||||
"id": "1038lab/ComfyUI-QwenASR:__init__.py",
|
||||
"sha256": "0919008142c12f8fe9a12671a1ca1de548ded128dd7287a13f6230d9729b47c2"
|
||||
},
|
||||
{
|
||||
"id": "Comfy-Org/ComfyUI:LICENSE",
|
||||
"sha256": "3972dc9744f6499f0f9b2dbf76696f2ae7ad8af9b23dde66d6af86c9dfb36986"
|
||||
},
|
||||
{
|
||||
"id": "Comfy-Org/ComfyUI:comfy_extras/nodes_audio.py",
|
||||
"sha256": "d47eefe226c273f20561545f6a664bfdfa5c193cec7178c925b5dc5ccb642a9c"
|
||||
},
|
||||
{
|
||||
"id": "Comfy-Org/ComfyUI:comfy_extras/nodes_preview_any.py",
|
||||
"sha256": "f8c526dbc4ec512f88a067524865f59eb24db636858f37d601e848885b2d0751"
|
||||
},
|
||||
{
|
||||
"id": "qwen-history:aaf40688c93c8e083f8a47d9107e6f95405ea7c9",
|
||||
"sha256": "f8cda2d0d39517f93ace1a73263146a4bea59a02604e58919a05393c45ade6b7"
|
||||
},
|
||||
{
|
||||
"id": "qwen-history:b037d7be121111a1ba6f40a4d99f6b43ac6411b6",
|
||||
"sha256": "af7721c1015fef8cb40a724226fb0f720397d70fb1ce367f9145a3b653dd0cd9"
|
||||
},
|
||||
{
|
||||
"id": "Comfy-Org/ComfyUI_frontend:src/utils/nodeDefUtil.ts",
|
||||
"sha256": "9c62901804e584a2e9d7e17e7914475113e41993c44df3d063f0027e95735dc1"
|
||||
},
|
||||
{
|
||||
"id": "Comfy-Org/ComfyUI_frontend:src/renderer/extensions/vueNodes/widgets/composables/useStringWidget.ts",
|
||||
"sha256": "b734a2584d558de501150e8011e1c16031f33778c2cd3b4c712f296ce4f05396"
|
||||
},
|
||||
{
|
||||
"id": "Comfy-Org/ComfyUI_frontend:packages/shared-frontend-utils/src/formatUtil.ts",
|
||||
"sha256": "7313418fe3a92aae99f2ef91f081257c2b2162b1578155d7346f2f03ab440039"
|
||||
},
|
||||
{
|
||||
"id": "Comfy-Org/ComfyUI_frontend:src/scripts/domWidget.ts",
|
||||
"sha256": "34ec3c58d6b236f4e99bfa0c82dcab8e15c6a6b51c6dcb9ace30e37d1a395800"
|
||||
},
|
||||
{
|
||||
"id": "Comfy-Org/ComfyUI_frontend:src/lib/litegraph/src/LGraphNode.ts",
|
||||
"sha256": "706225a8a06cd7929f9e04fe2388bcdb8f158cdac756c65c45657c020ab7b319"
|
||||
},
|
||||
{
|
||||
"id": "1038lab/ComfyUI-QwenASR:LICENSE",
|
||||
"sha256": "3972dc9744f6499f0f9b2dbf76696f2ae7ad8af9b23dde66d6af86c9dfb36986"
|
||||
},
|
||||
{
|
||||
"id": "1038lab/ComfyUI-QwenASR:__init__.py",
|
||||
"sha256": "e35f18c3986ee7b00a918ed644e375bc7cee1b88f740cc52fd93b45ace621a41"
|
||||
},
|
||||
{
|
||||
"id": "ComfyUI_frontend:src/lib/litegraph/src/widgets/BaseWidget.ts",
|
||||
"sha256": "6a77b402bbf8c67b9ea2ce58de28c61a1b17da82bb437c29565fb260b721b8c7"
|
||||
},
|
||||
{
|
||||
"id": "ComfyUI_frontend:src/lib/litegraph/src/widgets/LegacyWidget.ts",
|
||||
"sha256": "05d098254988191736f1c72f7efc57e3f350d97c82267a97f0b8c193d65daa96"
|
||||
},
|
||||
{
|
||||
"id": "ComfyUI_frontend:src/renderer/extensions/vueNodes/widgets/composables/useBooleanWidget.ts",
|
||||
"sha256": "36f477f75561e0c7bfc2c6757906724f8ff2c8660ace26759f3f07493877fece"
|
||||
}
|
||||
],
|
||||
"upstream_code_executed": false,
|
||||
"target_version_match": "unknown"
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
{
|
||||
"tested_code_commit": "1f779d53c4312068513c2bd6d513f01a3c9f3caf",
|
||||
"increment_base": "97f3a76f0ab5da110f4c3a7e79576627d7dedaf9",
|
||||
"final_delivery_commit": "see subsequent evidence-only commit containing this file; code hashes below are unchanged",
|
||||
"code_sha256": {
|
||||
"calliope-backend/scripts/audit_reference_conversion.py": "af3bc0b790f459a72b32517580db7fb7183a5c9d17d563a527ec8c58ff0576f0",
|
||||
"calliope-backend/src/calliope/comfyui/serialization_reference.py": "1d36316c5619471a13964f13a883a87dc01e445e028d1f653db6b5cd9086a509",
|
||||
"calliope-backend/src/calliope/comfyui/structure_reference.py": "5bf992902afedd52c91fca31d6d29e29c112e973b112f6754ddb39da9684c7fd",
|
||||
"calliope-backend/src/calliope/comfyui/ui2api.py": "793a74cff1923e80883d7b1e45ce3e0d08c88a9b194f0702f1cdf0d28e8c2589",
|
||||
"calliope-backend/tests/test_structure_reference.py": "ab532c7aeb592cff1935dbd4a8657e1c318b3d5a7d8f2da1b5886c20775fcdc5"
|
||||
},
|
||||
"tests": {
|
||||
"passed": 178,
|
||||
"failed": 0,
|
||||
"warning": 1,
|
||||
"new_specialized": 32,
|
||||
"existing_affected": 146
|
||||
},
|
||||
"sources_refetched_and_verified": 49,
|
||||
"full_backend_rerun": false,
|
||||
"historical_only": "142 and 865/5/6",
|
||||
"production_eligibility_changed": false,
|
||||
"cases": [
|
||||
{
|
||||
"id": "01",
|
||||
"profile": "comfy-frontend-1.37.11-controlnet-aux-e8b689a-reference",
|
||||
"candidate": true,
|
||||
"class_types_compared": 22,
|
||||
"field_items": 34,
|
||||
"field_differences": 0,
|
||||
"edges_compared": 23,
|
||||
"edges_equal": 23,
|
||||
"unresolved_boundaries": 0,
|
||||
"metadata_differences": 13,
|
||||
"whole_prompt_issue_count": 0
|
||||
},
|
||||
{
|
||||
"id": "02",
|
||||
"profile": "comfy-frontend-1.37.11-qwen-b037d7be-reference",
|
||||
"candidate": false,
|
||||
"class_types_compared": 6,
|
||||
"field_items": 16,
|
||||
"field_differences": 1,
|
||||
"edges_compared": 5,
|
||||
"edges_equal": 5,
|
||||
"unresolved_boundaries": 0,
|
||||
"metadata_differences": 6,
|
||||
"whole_prompt_issue_count": 4
|
||||
},
|
||||
{
|
||||
"id": "03",
|
||||
"profile": "comfy-frontend-1.48.7-structure-reference",
|
||||
"candidate": false,
|
||||
"class_types_compared": 12,
|
||||
"field_items": 0,
|
||||
"field_differences": 0,
|
||||
"edges_compared": 16,
|
||||
"edges_equal": 16,
|
||||
"unresolved_boundaries": 8,
|
||||
"metadata_differences": 12,
|
||||
"whole_prompt_issue_count": 21
|
||||
},
|
||||
{
|
||||
"id": "04",
|
||||
"profile": "comfy-frontend-1.53.6-structure-reference",
|
||||
"candidate": false,
|
||||
"class_types_compared": 16,
|
||||
"field_items": 0,
|
||||
"field_differences": 0,
|
||||
"edges_compared": 18,
|
||||
"edges_equal": 18,
|
||||
"unresolved_boundaries": 15,
|
||||
"metadata_differences": 14,
|
||||
"whole_prompt_issue_count": 32
|
||||
},
|
||||
{
|
||||
"id": "05",
|
||||
"profile": "comfy-frontend-1.37.11-controlnet-aux-e8b689a-reference",
|
||||
"candidate": false,
|
||||
"class_types_compared": 16,
|
||||
"field_items": 0,
|
||||
"field_differences": 0,
|
||||
"edges_compared": 18,
|
||||
"edges_equal": 18,
|
||||
"unresolved_boundaries": 0,
|
||||
"metadata_differences": 16,
|
||||
"whole_prompt_issue_count": 18
|
||||
},
|
||||
{
|
||||
"id": "07",
|
||||
"profile": "comfy-frontend-1.48.6-structure-reference",
|
||||
"candidate": false,
|
||||
"class_types_compared": 28,
|
||||
"field_items": 0,
|
||||
"field_differences": 0,
|
||||
"edges_compared": 39,
|
||||
"edges_equal": 4,
|
||||
"unresolved_boundaries": 0,
|
||||
"metadata_differences": 21,
|
||||
"whole_prompt_issue_count": 1
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
........................................................................ [ 40%]
|
||||
........................................................................ [ 80%]
|
||||
.................................. [100%]
|
||||
=============================== warnings summary ===============================
|
||||
../../../hel-892-a64a969a6a44/workdir/aigc-home/calliope-backend/.venv/lib/python3.11/site-packages/starlette/testclient.py:53
|
||||
/home/xiaomo/multica_workspaces/hello-af23464d222e/hel-892-a64a969a6a44/workdir/aigc-home/calliope-backend/.venv/lib/python3.11/site-packages/starlette/testclient.py:53: DeprecationWarning: The anyio.abc.BlockingPortal alias is deprecated, use anyio.from_thread.BlockingPortal instead.
|
||||
_PortalFactoryType = Callable[[], AbstractContextManager[anyio.abc.BlockingPortal]]
|
||||
|
||||
-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
|
||||
178 passed, 1 warning in 8.66s
|
||||
@@ -0,0 +1,674 @@
|
||||
GNU GENERAL PUBLIC LICENSE
|
||||
Version 3, 29 June 2007
|
||||
|
||||
Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
|
||||
Everyone is permitted to copy and distribute verbatim copies
|
||||
of this license document, but changing it is not allowed.
|
||||
|
||||
Preamble
|
||||
|
||||
The GNU General Public License is a free, copyleft license for
|
||||
software and other kinds of works.
|
||||
|
||||
The licenses for most software and other practical works are designed
|
||||
to take away your freedom to share and change the works. By contrast,
|
||||
the GNU General Public License is intended to guarantee your freedom to
|
||||
share and change all versions of a program--to make sure it remains free
|
||||
software for all its users. We, the Free Software Foundation, use the
|
||||
GNU General Public License for most of our software; it applies also to
|
||||
any other work released this way by its authors. You can apply it to
|
||||
your programs, too.
|
||||
|
||||
When we speak of free software, we are referring to freedom, not
|
||||
price. Our General Public Licenses are designed to make sure that you
|
||||
have the freedom to distribute copies of free software (and charge for
|
||||
them if you wish), that you receive source code or can get it if you
|
||||
want it, that you can change the software or use pieces of it in new
|
||||
free programs, and that you know you can do these things.
|
||||
|
||||
To protect your rights, we need to prevent others from denying you
|
||||
these rights or asking you to surrender the rights. Therefore, you have
|
||||
certain responsibilities if you distribute copies of the software, or if
|
||||
you modify it: responsibilities to respect the freedom of others.
|
||||
|
||||
For example, if you distribute copies of such a program, whether
|
||||
gratis or for a fee, you must pass on to the recipients the same
|
||||
freedoms that you received. You must make sure that they, too, receive
|
||||
or can get the source code. And you must show them these terms so they
|
||||
know their rights.
|
||||
|
||||
Developers that use the GNU GPL protect your rights with two steps:
|
||||
(1) assert copyright on the software, and (2) offer you this License
|
||||
giving you legal permission to copy, distribute and/or modify it.
|
||||
|
||||
For the developers' and authors' protection, the GPL clearly explains
|
||||
that there is no warranty for this free software. For both users' and
|
||||
authors' sake, the GPL requires that modified versions be marked as
|
||||
changed, so that their problems will not be attributed erroneously to
|
||||
authors of previous versions.
|
||||
|
||||
Some devices are designed to deny users access to install or run
|
||||
modified versions of the software inside them, although the manufacturer
|
||||
can do so. This is fundamentally incompatible with the aim of
|
||||
protecting users' freedom to change the software. The systematic
|
||||
pattern of such abuse occurs in the area of products for individuals to
|
||||
use, which is precisely where it is most unacceptable. Therefore, we
|
||||
have designed this version of the GPL to prohibit the practice for those
|
||||
products. If such problems arise substantially in other domains, we
|
||||
stand ready to extend this provision to those domains in future versions
|
||||
of the GPL, as needed to protect the freedom of users.
|
||||
|
||||
Finally, every program is threatened constantly by software patents.
|
||||
States should not allow patents to restrict development and use of
|
||||
software on general-purpose computers, but in those that do, we wish to
|
||||
avoid the special danger that patents applied to a free program could
|
||||
make it effectively proprietary. To prevent this, the GPL assures that
|
||||
patents cannot be used to render the program non-free.
|
||||
|
||||
The precise terms and conditions for copying, distribution and
|
||||
modification follow.
|
||||
|
||||
TERMS AND CONDITIONS
|
||||
|
||||
0. Definitions.
|
||||
|
||||
"This License" refers to version 3 of the GNU General Public License.
|
||||
|
||||
"Copyright" also means copyright-like laws that apply to other kinds of
|
||||
works, such as semiconductor masks.
|
||||
|
||||
"The Program" refers to any copyrightable work licensed under this
|
||||
License. Each licensee is addressed as "you". "Licensees" and
|
||||
"recipients" may be individuals or organizations.
|
||||
|
||||
To "modify" a work means to copy from or adapt all or part of the work
|
||||
in a fashion requiring copyright permission, other than the making of an
|
||||
exact copy. The resulting work is called a "modified version" of the
|
||||
earlier work or a work "based on" the earlier work.
|
||||
|
||||
A "covered work" means either the unmodified Program or a work based
|
||||
on the Program.
|
||||
|
||||
To "propagate" a work means to do anything with it that, without
|
||||
permission, would make you directly or secondarily liable for
|
||||
infringement under applicable copyright law, except executing it on a
|
||||
computer or modifying a private copy. Propagation includes copying,
|
||||
distribution (with or without modification), making available to the
|
||||
public, and in some countries other activities as well.
|
||||
|
||||
To "convey" a work means any kind of propagation that enables other
|
||||
parties to make or receive copies. Mere interaction with a user through
|
||||
a computer network, with no transfer of a copy, is not conveying.
|
||||
|
||||
An interactive user interface displays "Appropriate Legal Notices"
|
||||
to the extent that it includes a convenient and prominently visible
|
||||
feature that (1) displays an appropriate copyright notice, and (2)
|
||||
tells the user that there is no warranty for the work (except to the
|
||||
extent that warranties are provided), that licensees may convey the
|
||||
work under this License, and how to view a copy of this License. If
|
||||
the interface presents a list of user commands or options, such as a
|
||||
menu, a prominent item in the list meets this criterion.
|
||||
|
||||
1. Source Code.
|
||||
|
||||
The "source code" for a work means the preferred form of the work
|
||||
for making modifications to it. "Object code" means any non-source
|
||||
form of a work.
|
||||
|
||||
A "Standard Interface" means an interface that either is an official
|
||||
standard defined by a recognized standards body, or, in the case of
|
||||
interfaces specified for a particular programming language, one that
|
||||
is widely used among developers working in that language.
|
||||
|
||||
The "System Libraries" of an executable work include anything, other
|
||||
than the work as a whole, that (a) is included in the normal form of
|
||||
packaging a Major Component, but which is not part of that Major
|
||||
Component, and (b) serves only to enable use of the work with that
|
||||
Major Component, or to implement a Standard Interface for which an
|
||||
implementation is available to the public in source code form. A
|
||||
"Major Component", in this context, means a major essential component
|
||||
(kernel, window system, and so on) of the specific operating system
|
||||
(if any) on which the executable work runs, or a compiler used to
|
||||
produce the work, or an object code interpreter used to run it.
|
||||
|
||||
The "Corresponding Source" for a work in object code form means all
|
||||
the source code needed to generate, install, and (for an executable
|
||||
work) run the object code and to modify the work, including scripts to
|
||||
control those activities. However, it does not include the work's
|
||||
System Libraries, or general-purpose tools or generally available free
|
||||
programs which are used unmodified in performing those activities but
|
||||
which are not part of the work. For example, Corresponding Source
|
||||
includes interface definition files associated with source files for
|
||||
the work, and the source code for shared libraries and dynamically
|
||||
linked subprograms that the work is specifically designed to require,
|
||||
such as by intimate data communication or control flow between those
|
||||
subprograms and other parts of the work.
|
||||
|
||||
The Corresponding Source need not include anything that users
|
||||
can regenerate automatically from other parts of the Corresponding
|
||||
Source.
|
||||
|
||||
The Corresponding Source for a work in source code form is that
|
||||
same work.
|
||||
|
||||
2. Basic Permissions.
|
||||
|
||||
All rights granted under this License are granted for the term of
|
||||
copyright on the Program, and are irrevocable provided the stated
|
||||
conditions are met. This License explicitly affirms your unlimited
|
||||
permission to run the unmodified Program. The output from running a
|
||||
covered work is covered by this License only if the output, given its
|
||||
content, constitutes a covered work. This License acknowledges your
|
||||
rights of fair use or other equivalent, as provided by copyright law.
|
||||
|
||||
You may make, run and propagate covered works that you do not
|
||||
convey, without conditions so long as your license otherwise remains
|
||||
in force. You may convey covered works to others for the sole purpose
|
||||
of having them make modifications exclusively for you, or provide you
|
||||
with facilities for running those works, provided that you comply with
|
||||
the terms of this License in conveying all material for which you do
|
||||
not control copyright. Those thus making or running the covered works
|
||||
for you must do so exclusively on your behalf, under your direction
|
||||
and control, on terms that prohibit them from making any copies of
|
||||
your copyrighted material outside their relationship with you.
|
||||
|
||||
Conveying under any other circumstances is permitted solely under
|
||||
the conditions stated below. Sublicensing is not allowed; section 10
|
||||
makes it unnecessary.
|
||||
|
||||
3. Protecting Users' Legal Rights From Anti-Circumvention Law.
|
||||
|
||||
No covered work shall be deemed part of an effective technological
|
||||
measure under any applicable law fulfilling obligations under article
|
||||
11 of the WIPO copyright treaty adopted on 20 December 1996, or
|
||||
similar laws prohibiting or restricting circumvention of such
|
||||
measures.
|
||||
|
||||
When you convey a covered work, you waive any legal power to forbid
|
||||
circumvention of technological measures to the extent such circumvention
|
||||
is effected by exercising rights under this License with respect to
|
||||
the covered work, and you disclaim any intention to limit operation or
|
||||
modification of the work as a means of enforcing, against the work's
|
||||
users, your or third parties' legal rights to forbid circumvention of
|
||||
technological measures.
|
||||
|
||||
4. Conveying Verbatim Copies.
|
||||
|
||||
You may convey verbatim copies of the Program's source code as you
|
||||
receive it, in any medium, provided that you conspicuously and
|
||||
appropriately publish on each copy an appropriate copyright notice;
|
||||
keep intact all notices stating that this License and any
|
||||
non-permissive terms added in accord with section 7 apply to the code;
|
||||
keep intact all notices of the absence of any warranty; and give all
|
||||
recipients a copy of this License along with the Program.
|
||||
|
||||
You may charge any price or no price for each copy that you convey,
|
||||
and you may offer support or warranty protection for a fee.
|
||||
|
||||
5. Conveying Modified Source Versions.
|
||||
|
||||
You may convey a work based on the Program, or the modifications to
|
||||
produce it from the Program, in the form of source code under the
|
||||
terms of section 4, provided that you also meet all of these conditions:
|
||||
|
||||
a) The work must carry prominent notices stating that you modified
|
||||
it, and giving a relevant date.
|
||||
|
||||
b) The work must carry prominent notices stating that it is
|
||||
released under this License and any conditions added under section
|
||||
7. This requirement modifies the requirement in section 4 to
|
||||
"keep intact all notices".
|
||||
|
||||
c) You must license the entire work, as a whole, under this
|
||||
License to anyone who comes into possession of a copy. This
|
||||
License will therefore apply, along with any applicable section 7
|
||||
additional terms, to the whole of the work, and all its parts,
|
||||
regardless of how they are packaged. This License gives no
|
||||
permission to license the work in any other way, but it does not
|
||||
invalidate such permission if you have separately received it.
|
||||
|
||||
d) If the work has interactive user interfaces, each must display
|
||||
Appropriate Legal Notices; however, if the Program has interactive
|
||||
interfaces that do not display Appropriate Legal Notices, your
|
||||
work need not make them do so.
|
||||
|
||||
A compilation of a covered work with other separate and independent
|
||||
works, which are not by their nature extensions of the covered work,
|
||||
and which are not combined with it such as to form a larger program,
|
||||
in or on a volume of a storage or distribution medium, is called an
|
||||
"aggregate" if the compilation and its resulting copyright are not
|
||||
used to limit the access or legal rights of the compilation's users
|
||||
beyond what the individual works permit. Inclusion of a covered work
|
||||
in an aggregate does not cause this License to apply to the other
|
||||
parts of the aggregate.
|
||||
|
||||
6. Conveying Non-Source Forms.
|
||||
|
||||
You may convey a covered work in object code form under the terms
|
||||
of sections 4 and 5, provided that you also convey the
|
||||
machine-readable Corresponding Source under the terms of this License,
|
||||
in one of these ways:
|
||||
|
||||
a) Convey the object code in, or embodied in, a physical product
|
||||
(including a physical distribution medium), accompanied by the
|
||||
Corresponding Source fixed on a durable physical medium
|
||||
customarily used for software interchange.
|
||||
|
||||
b) Convey the object code in, or embodied in, a physical product
|
||||
(including a physical distribution medium), accompanied by a
|
||||
written offer, valid for at least three years and valid for as
|
||||
long as you offer spare parts or customer support for that product
|
||||
model, to give anyone who possesses the object code either (1) a
|
||||
copy of the Corresponding Source for all the software in the
|
||||
product that is covered by this License, on a durable physical
|
||||
medium customarily used for software interchange, for a price no
|
||||
more than your reasonable cost of physically performing this
|
||||
conveying of source, or (2) access to copy the
|
||||
Corresponding Source from a network server at no charge.
|
||||
|
||||
c) Convey individual copies of the object code with a copy of the
|
||||
written offer to provide the Corresponding Source. This
|
||||
alternative is allowed only occasionally and noncommercially, and
|
||||
only if you received the object code with such an offer, in accord
|
||||
with subsection 6b.
|
||||
|
||||
d) Convey the object code by offering access from a designated
|
||||
place (gratis or for a charge), and offer equivalent access to the
|
||||
Corresponding Source in the same way through the same place at no
|
||||
further charge. You need not require recipients to copy the
|
||||
Corresponding Source along with the object code. If the place to
|
||||
copy the object code is a network server, the Corresponding Source
|
||||
may be on a different server (operated by you or a third party)
|
||||
that supports equivalent copying facilities, provided you maintain
|
||||
clear directions next to the object code saying where to find the
|
||||
Corresponding Source. Regardless of what server hosts the
|
||||
Corresponding Source, you remain obligated to ensure that it is
|
||||
available for as long as needed to satisfy these requirements.
|
||||
|
||||
e) Convey the object code using peer-to-peer transmission, provided
|
||||
you inform other peers where the object code and Corresponding
|
||||
Source of the work are being offered to the general public at no
|
||||
charge under subsection 6d.
|
||||
|
||||
A separable portion of the object code, whose source code is excluded
|
||||
from the Corresponding Source as a System Library, need not be
|
||||
included in conveying the object code work.
|
||||
|
||||
A "User Product" is either (1) a "consumer product", which means any
|
||||
tangible personal property which is normally used for personal, family,
|
||||
or household purposes, or (2) anything designed or sold for incorporation
|
||||
into a dwelling. In determining whether a product is a consumer product,
|
||||
doubtful cases shall be resolved in favor of coverage. For a particular
|
||||
product received by a particular user, "normally used" refers to a
|
||||
typical or common use of that class of product, regardless of the status
|
||||
of the particular user or of the way in which the particular user
|
||||
actually uses, or expects or is expected to use, the product. A product
|
||||
is a consumer product regardless of whether the product has substantial
|
||||
commercial, industrial or non-consumer uses, unless such uses represent
|
||||
the only significant mode of use of the product.
|
||||
|
||||
"Installation Information" for a User Product means any methods,
|
||||
procedures, authorization keys, or other information required to install
|
||||
and execute modified versions of a covered work in that User Product from
|
||||
a modified version of its Corresponding Source. The information must
|
||||
suffice to ensure that the continued functioning of the modified object
|
||||
code is in no case prevented or interfered with solely because
|
||||
modification has been made.
|
||||
|
||||
If you convey an object code work under this section in, or with, or
|
||||
specifically for use in, a User Product, and the conveying occurs as
|
||||
part of a transaction in which the right of possession and use of the
|
||||
User Product is transferred to the recipient in perpetuity or for a
|
||||
fixed term (regardless of how the transaction is characterized), the
|
||||
Corresponding Source conveyed under this section must be accompanied
|
||||
by the Installation Information. But this requirement does not apply
|
||||
if neither you nor any third party retains the ability to install
|
||||
modified object code on the User Product (for example, the work has
|
||||
been installed in ROM).
|
||||
|
||||
The requirement to provide Installation Information does not include a
|
||||
requirement to continue to provide support service, warranty, or updates
|
||||
for a work that has been modified or installed by the recipient, or for
|
||||
the User Product in which it has been modified or installed. Access to a
|
||||
network may be denied when the modification itself materially and
|
||||
adversely affects the operation of the network or violates the rules and
|
||||
protocols for communication across the network.
|
||||
|
||||
Corresponding Source conveyed, and Installation Information provided,
|
||||
in accord with this section must be in a format that is publicly
|
||||
documented (and with an implementation available to the public in
|
||||
source code form), and must require no special password or key for
|
||||
unpacking, reading or copying.
|
||||
|
||||
7. Additional Terms.
|
||||
|
||||
"Additional permissions" are terms that supplement the terms of this
|
||||
License by making exceptions from one or more of its conditions.
|
||||
Additional permissions that are applicable to the entire Program shall
|
||||
be treated as though they were included in this License, to the extent
|
||||
that they are valid under applicable law. If additional permissions
|
||||
apply only to part of the Program, that part may be used separately
|
||||
under those permissions, but the entire Program remains governed by
|
||||
this License without regard to the additional permissions.
|
||||
|
||||
When you convey a copy of a covered work, you may at your option
|
||||
remove any additional permissions from that copy, or from any part of
|
||||
it. (Additional permissions may be written to require their own
|
||||
removal in certain cases when you modify the work.) You may place
|
||||
additional permissions on material, added by you to a covered work,
|
||||
for which you have or can give appropriate copyright permission.
|
||||
|
||||
Notwithstanding any other provision of this License, for material you
|
||||
add to a covered work, you may (if authorized by the copyright holders of
|
||||
that material) supplement the terms of this License with terms:
|
||||
|
||||
a) Disclaiming warranty or limiting liability differently from the
|
||||
terms of sections 15 and 16 of this License; or
|
||||
|
||||
b) Requiring preservation of specified reasonable legal notices or
|
||||
author attributions in that material or in the Appropriate Legal
|
||||
Notices displayed by works containing it; or
|
||||
|
||||
c) Prohibiting misrepresentation of the origin of that material, or
|
||||
requiring that modified versions of such material be marked in
|
||||
reasonable ways as different from the original version; or
|
||||
|
||||
d) Limiting the use for publicity purposes of names of licensors or
|
||||
authors of the material; or
|
||||
|
||||
e) Declining to grant rights under trademark law for use of some
|
||||
trade names, trademarks, or service marks; or
|
||||
|
||||
f) Requiring indemnification of licensors and authors of that
|
||||
material by anyone who conveys the material (or modified versions of
|
||||
it) with contractual assumptions of liability to the recipient, for
|
||||
any liability that these contractual assumptions directly impose on
|
||||
those licensors and authors.
|
||||
|
||||
All other non-permissive additional terms are considered "further
|
||||
restrictions" within the meaning of section 10. If the Program as you
|
||||
received it, or any part of it, contains a notice stating that it is
|
||||
governed by this License along with a term that is a further
|
||||
restriction, you may remove that term. If a license document contains
|
||||
a further restriction but permits relicensing or conveying under this
|
||||
License, you may add to a covered work material governed by the terms
|
||||
of that license document, provided that the further restriction does
|
||||
not survive such relicensing or conveying.
|
||||
|
||||
If you add terms to a covered work in accord with this section, you
|
||||
must place, in the relevant source files, a statement of the
|
||||
additional terms that apply to those files, or a notice indicating
|
||||
where to find the applicable terms.
|
||||
|
||||
Additional terms, permissive or non-permissive, may be stated in the
|
||||
form of a separately written license, or stated as exceptions;
|
||||
the above requirements apply either way.
|
||||
|
||||
8. Termination.
|
||||
|
||||
You may not propagate or modify a covered work except as expressly
|
||||
provided under this License. Any attempt otherwise to propagate or
|
||||
modify it is void, and will automatically terminate your rights under
|
||||
this License (including any patent licenses granted under the third
|
||||
paragraph of section 11).
|
||||
|
||||
However, if you cease all violation of this License, then your
|
||||
license from a particular copyright holder is reinstated (a)
|
||||
provisionally, unless and until the copyright holder explicitly and
|
||||
finally terminates your license, and (b) permanently, if the copyright
|
||||
holder fails to notify you of the violation by some reasonable means
|
||||
prior to 60 days after the cessation.
|
||||
|
||||
Moreover, your license from a particular copyright holder is
|
||||
reinstated permanently if the copyright holder notifies you of the
|
||||
violation by some reasonable means, this is the first time you have
|
||||
received notice of violation of this License (for any work) from that
|
||||
copyright holder, and you cure the violation prior to 30 days after
|
||||
your receipt of the notice.
|
||||
|
||||
Termination of your rights under this section does not terminate the
|
||||
licenses of parties who have received copies or rights from you under
|
||||
this License. If your rights have been terminated and not permanently
|
||||
reinstated, you do not qualify to receive new licenses for the same
|
||||
material under section 10.
|
||||
|
||||
9. Acceptance Not Required for Having Copies.
|
||||
|
||||
You are not required to accept this License in order to receive or
|
||||
run a copy of the Program. Ancillary propagation of a covered work
|
||||
occurring solely as a consequence of using peer-to-peer transmission
|
||||
to receive a copy likewise does not require acceptance. However,
|
||||
nothing other than this License grants you permission to propagate or
|
||||
modify any covered work. These actions infringe copyright if you do
|
||||
not accept this License. Therefore, by modifying or propagating a
|
||||
covered work, you indicate your acceptance of this License to do so.
|
||||
|
||||
10. Automatic Licensing of Downstream Recipients.
|
||||
|
||||
Each time you convey a covered work, the recipient automatically
|
||||
receives a license from the original licensors, to run, modify and
|
||||
propagate that work, subject to this License. You are not responsible
|
||||
for enforcing compliance by third parties with this License.
|
||||
|
||||
An "entity transaction" is a transaction transferring control of an
|
||||
organization, or substantially all assets of one, or subdividing an
|
||||
organization, or merging organizations. If propagation of a covered
|
||||
work results from an entity transaction, each party to that
|
||||
transaction who receives a copy of the work also receives whatever
|
||||
licenses to the work the party's predecessor in interest had or could
|
||||
give under the previous paragraph, plus a right to possession of the
|
||||
Corresponding Source of the work from the predecessor in interest, if
|
||||
the predecessor has it or can get it with reasonable efforts.
|
||||
|
||||
You may not impose any further restrictions on the exercise of the
|
||||
rights granted or affirmed under this License. For example, you may
|
||||
not impose a license fee, royalty, or other charge for exercise of
|
||||
rights granted under this License, and you may not initiate litigation
|
||||
(including a cross-claim or counterclaim in a lawsuit) alleging that
|
||||
any patent claim is infringed by making, using, selling, offering for
|
||||
sale, or importing the Program or any portion of it.
|
||||
|
||||
11. Patents.
|
||||
|
||||
A "contributor" is a copyright holder who authorizes use under this
|
||||
License of the Program or a work on which the Program is based. The
|
||||
work thus licensed is called the contributor's "contributor version".
|
||||
|
||||
A contributor's "essential patent claims" are all patent claims
|
||||
owned or controlled by the contributor, whether already acquired or
|
||||
hereafter acquired, that would be infringed by some manner, permitted
|
||||
by this License, of making, using, or selling its contributor version,
|
||||
but do not include claims that would be infringed only as a
|
||||
consequence of further modification of the contributor version. For
|
||||
purposes of this definition, "control" includes the right to grant
|
||||
patent sublicenses in a manner consistent with the requirements of
|
||||
this License.
|
||||
|
||||
Each contributor grants you a non-exclusive, worldwide, royalty-free
|
||||
patent license under the contributor's essential patent claims, to
|
||||
make, use, sell, offer for sale, import and otherwise run, modify and
|
||||
propagate the contents of its contributor version.
|
||||
|
||||
In the following three paragraphs, a "patent license" is any express
|
||||
agreement or commitment, however denominated, not to enforce a patent
|
||||
(such as an express permission to practice a patent or covenant not to
|
||||
sue for patent infringement). To "grant" such a patent license to a
|
||||
party means to make such an agreement or commitment not to enforce a
|
||||
patent against the party.
|
||||
|
||||
If you convey a covered work, knowingly relying on a patent license,
|
||||
and the Corresponding Source of the work is not available for anyone
|
||||
to copy, free of charge and under the terms of this License, through a
|
||||
publicly available network server or other readily accessible means,
|
||||
then you must either (1) cause the Corresponding Source to be so
|
||||
available, or (2) arrange to deprive yourself of the benefit of the
|
||||
patent license for this particular work, or (3) arrange, in a manner
|
||||
consistent with the requirements of this License, to extend the patent
|
||||
license to downstream recipients. "Knowingly relying" means you have
|
||||
actual knowledge that, but for the patent license, your conveying the
|
||||
covered work in a country, or your recipient's use of the covered work
|
||||
in a country, would infringe one or more identifiable patents in that
|
||||
country that you have reason to believe are valid.
|
||||
|
||||
If, pursuant to or in connection with a single transaction or
|
||||
arrangement, you convey, or propagate by procuring conveyance of, a
|
||||
covered work, and grant a patent license to some of the parties
|
||||
receiving the covered work authorizing them to use, propagate, modify
|
||||
or convey a specific copy of the covered work, then the patent license
|
||||
you grant is automatically extended to all recipients of the covered
|
||||
work and works based on it.
|
||||
|
||||
A patent license is "discriminatory" if it does not include within
|
||||
the scope of its coverage, prohibits the exercise of, or is
|
||||
conditioned on the non-exercise of one or more of the rights that are
|
||||
specifically granted under this License. You may not convey a covered
|
||||
work if you are a party to an arrangement with a third party that is
|
||||
in the business of distributing software, under which you make payment
|
||||
to the third party based on the extent of your activity of conveying
|
||||
the work, and under which the third party grants, to any of the
|
||||
parties who would receive the covered work from you, a discriminatory
|
||||
patent license (a) in connection with copies of the covered work
|
||||
conveyed by you (or copies made from those copies), or (b) primarily
|
||||
for and in connection with specific products or compilations that
|
||||
contain the covered work, unless you entered into that arrangement,
|
||||
or that patent license was granted, prior to 28 March 2007.
|
||||
|
||||
Nothing in this License shall be construed as excluding or limiting
|
||||
any implied license or other defenses to infringement that may
|
||||
otherwise be available to you under applicable patent law.
|
||||
|
||||
12. No Surrender of Others' Freedom.
|
||||
|
||||
If conditions are imposed on you (whether by court order, agreement or
|
||||
otherwise) that contradict the conditions of this License, they do not
|
||||
excuse you from the conditions of this License. If you cannot convey a
|
||||
covered work so as to satisfy simultaneously your obligations under this
|
||||
License and any other pertinent obligations, then as a consequence you may
|
||||
not convey it at all. For example, if you agree to terms that obligate you
|
||||
to collect a royalty for further conveying from those to whom you convey
|
||||
the Program, the only way you could satisfy both those terms and this
|
||||
License would be to refrain entirely from conveying the Program.
|
||||
|
||||
13. Use with the GNU Affero General Public License.
|
||||
|
||||
Notwithstanding any other provision of this License, you have
|
||||
permission to link or combine any covered work with a work licensed
|
||||
under version 3 of the GNU Affero General Public License into a single
|
||||
combined work, and to convey the resulting work. The terms of this
|
||||
License will continue to apply to the part which is the covered work,
|
||||
but the special requirements of the GNU Affero General Public License,
|
||||
section 13, concerning interaction through a network will apply to the
|
||||
combination as such.
|
||||
|
||||
14. Revised Versions of this License.
|
||||
|
||||
The Free Software Foundation may publish revised and/or new versions of
|
||||
the GNU General Public License from time to time. Such new versions will
|
||||
be similar in spirit to the present version, but may differ in detail to
|
||||
address new problems or concerns.
|
||||
|
||||
Each version is given a distinguishing version number. If the
|
||||
Program specifies that a certain numbered version of the GNU General
|
||||
Public License "or any later version" applies to it, you have the
|
||||
option of following the terms and conditions either of that numbered
|
||||
version or of any later version published by the Free Software
|
||||
Foundation. If the Program does not specify a version number of the
|
||||
GNU General Public License, you may choose any version ever published
|
||||
by the Free Software Foundation.
|
||||
|
||||
If the Program specifies that a proxy can decide which future
|
||||
versions of the GNU General Public License can be used, that proxy's
|
||||
public statement of acceptance of a version permanently authorizes you
|
||||
to choose that version for the Program.
|
||||
|
||||
Later license versions may give you additional or different
|
||||
permissions. However, no additional obligations are imposed on any
|
||||
author or copyright holder as a result of your choosing to follow a
|
||||
later version.
|
||||
|
||||
15. Disclaimer of Warranty.
|
||||
|
||||
THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
|
||||
APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
|
||||
HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
|
||||
OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
|
||||
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
|
||||
IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
|
||||
ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
|
||||
|
||||
16. Limitation of Liability.
|
||||
|
||||
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
|
||||
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
|
||||
THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
|
||||
GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
|
||||
USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
|
||||
DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
|
||||
PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
|
||||
EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
|
||||
SUCH DAMAGES.
|
||||
|
||||
17. Interpretation of Sections 15 and 16.
|
||||
|
||||
If the disclaimer of warranty and limitation of liability provided
|
||||
above cannot be given local legal effect according to their terms,
|
||||
reviewing courts shall apply local law that most closely approximates
|
||||
an absolute waiver of all civil liability in connection with the
|
||||
Program, unless a warranty or assumption of liability accompanies a
|
||||
copy of the Program in return for a fee.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
How to Apply These Terms to Your New Programs
|
||||
|
||||
If you develop a new program, and you want it to be of the greatest
|
||||
possible use to the public, the best way to achieve this is to make it
|
||||
free software which everyone can redistribute and change under these terms.
|
||||
|
||||
To do so, attach the following notices to the program. It is safest
|
||||
to attach them to the start of each source file to most effectively
|
||||
state the exclusion of warranty; and each file should have at least
|
||||
the "copyright" line and a pointer to where the full notice is found.
|
||||
|
||||
<one line to give the program's name and a brief idea of what it does.>
|
||||
Copyright (C) <year> <name of author>
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
Also add information on how to contact you by electronic and paper mail.
|
||||
|
||||
If the program does terminal interaction, make it output a short
|
||||
notice like this when it starts in an interactive mode:
|
||||
|
||||
<program> Copyright (C) <year> <name of author>
|
||||
This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
|
||||
This is free software, and you are welcome to redistribute it
|
||||
under certain conditions; type `show c' for details.
|
||||
|
||||
The hypothetical commands `show w' and `show c' should show the appropriate
|
||||
parts of the General Public License. Of course, your program's commands
|
||||
might be different; for a GUI interface, you would use an "about box".
|
||||
|
||||
You should also get your employer (if you work as a programmer) or school,
|
||||
if any, to sign a "copyright disclaimer" for the program, if necessary.
|
||||
For more information on this, and how to apply and follow the GNU GPL, see
|
||||
<https://www.gnu.org/licenses/>.
|
||||
|
||||
The GNU General Public License does not permit incorporating your program
|
||||
into proprietary programs. If your program is a subroutine library, you
|
||||
may consider it more useful to permit linking proprietary applications with
|
||||
the library. If this is what you want to do, use the GNU Lesser General
|
||||
Public License instead of this License. But first, please read
|
||||
<https://www.gnu.org/licenses/why-not-lgpl.html>.
|
||||
@@ -0,0 +1,614 @@
|
||||
import {
|
||||
type CallbackParams,
|
||||
useChainCallback
|
||||
} from '@/composables/functional/useChainCallback'
|
||||
import { LGraphNode, LiteGraph } from '@/lib/litegraph/src/litegraph'
|
||||
import type {
|
||||
INodeInputSlot,
|
||||
INodeOutputSlot,
|
||||
ISlotType,
|
||||
LLink
|
||||
} from '@/lib/litegraph/src/litegraph'
|
||||
import { NodeSlot } from '@/lib/litegraph/src/node/NodeSlot'
|
||||
import type { CanvasPointerEvent } from '@/lib/litegraph/src/types/events'
|
||||
import type { IBaseWidget } from '@/lib/litegraph/src/types/widgets'
|
||||
import type { InputSpec } from '@/schemas/nodeDefSchema'
|
||||
import { app } from '@/scripts/app'
|
||||
import {
|
||||
ComfyWidgets,
|
||||
addValueControlWidgets,
|
||||
isValidWidgetType
|
||||
} from '@/scripts/widgets'
|
||||
import { CONFIG, GET_CONFIG } from '@/services/litegraphService'
|
||||
import { mergeInputSpec } from '@/utils/nodeDefUtil'
|
||||
import { applyTextReplacements } from '@/utils/searchAndReplace'
|
||||
import { isPrimitiveNode } from '@/renderer/utils/nodeTypeGuards'
|
||||
|
||||
const replacePropertyName = 'Run widget replace on values'
|
||||
export class PrimitiveNode extends LGraphNode {
|
||||
controlValues?: any[]
|
||||
lastType?: string
|
||||
static override category: string
|
||||
constructor(title: string) {
|
||||
super(title)
|
||||
this.addOutput('connect to widget input', '*')
|
||||
this.serialize_widgets = true
|
||||
this.isVirtualNode = true
|
||||
|
||||
if (!this.properties || !(replacePropertyName in this.properties)) {
|
||||
this.addProperty(replacePropertyName, false, 'boolean')
|
||||
}
|
||||
}
|
||||
|
||||
override applyToGraph(extraLinks: LLink[] = []) {
|
||||
if (!this.outputs[0].links?.length || !this.graph) return
|
||||
|
||||
const links = [
|
||||
...this.outputs[0].links.map((l) => this.graph!.links[l]),
|
||||
...extraLinks
|
||||
]
|
||||
let v = this.widgets?.[0].value
|
||||
if (v && this.properties[replacePropertyName]) {
|
||||
v = applyTextReplacements(this.graph, v as string)
|
||||
}
|
||||
|
||||
// For each output link copy our value over the original widget value
|
||||
for (const linkInfo of links) {
|
||||
const node = this.graph?.getNodeById(linkInfo.target_id)
|
||||
const input = node?.inputs[linkInfo.target_slot]
|
||||
if (!input) {
|
||||
console.warn('Unable to resolve node or input for link', linkInfo)
|
||||
continue
|
||||
}
|
||||
|
||||
const widgetName = input.widget?.name
|
||||
if (!widgetName) {
|
||||
console.warn('Invalid widget or widget name', input.widget)
|
||||
continue
|
||||
}
|
||||
|
||||
const widget = node.widgets?.find((w) => w.name === widgetName)
|
||||
if (!widget) {
|
||||
console.warn(
|
||||
`Unable to find widget "${widgetName}" on node [${node.id}]`
|
||||
)
|
||||
continue
|
||||
}
|
||||
|
||||
widget.value = v
|
||||
widget.callback?.(
|
||||
widget.value,
|
||||
app.canvas,
|
||||
node,
|
||||
app.canvas.graph_mouse,
|
||||
{} as CanvasPointerEvent
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
override refreshComboInNode() {
|
||||
const widget = this.widgets?.[0]
|
||||
if (widget?.type === 'combo') {
|
||||
// @ts-expect-error fixme ts strict error
|
||||
widget.options.values = this.outputs[0].widget[GET_CONFIG]()[0]
|
||||
|
||||
// @ts-expect-error fixme ts strict error
|
||||
if (!widget.options.values.includes(widget.value as string)) {
|
||||
// @ts-expect-error fixme ts strict error
|
||||
widget.value = widget.options.values[0]
|
||||
;(widget.callback as Function)(widget.value)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override onAfterGraphConfigured() {
|
||||
if (this.outputs[0].links?.length && !this.widgets?.length) {
|
||||
this.#onFirstConnection()
|
||||
|
||||
// Populate widget values from config data
|
||||
if (this.widgets && this.widgets_values) {
|
||||
for (let i = 0; i < this.widgets_values.length; i++) {
|
||||
const w = this.widgets[i]
|
||||
if (w) {
|
||||
w.value = this.widgets_values[i] as any
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Merge values if required
|
||||
this.#mergeWidgetConfig()
|
||||
}
|
||||
}
|
||||
|
||||
override onConnectionsChange(
|
||||
_type: ISlotType,
|
||||
_index: number,
|
||||
connected: boolean
|
||||
) {
|
||||
if (app.configuringGraph) {
|
||||
// Dont run while the graph is still setting up
|
||||
return
|
||||
}
|
||||
|
||||
const links = this.outputs[0].links
|
||||
if (connected) {
|
||||
if (links?.length && !this.widgets?.length) {
|
||||
this.#onFirstConnection()
|
||||
}
|
||||
} else {
|
||||
// We may have removed a link that caused the constraints to change
|
||||
this.#mergeWidgetConfig()
|
||||
|
||||
if (!links?.length) {
|
||||
this.onLastDisconnect()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override onConnectOutput(
|
||||
slot: number,
|
||||
_type: string,
|
||||
input: INodeInputSlot,
|
||||
target_node: LGraphNode,
|
||||
target_slot: number
|
||||
) {
|
||||
// Fires before the link is made allowing us to reject it if it isn't valid
|
||||
// No widget, we can't connect
|
||||
if (!input.widget && !(input.type in ComfyWidgets)) {
|
||||
return false
|
||||
}
|
||||
|
||||
if (this.outputs[slot].links?.length) {
|
||||
const valid = this.#isValidConnection(input)
|
||||
if (valid) {
|
||||
// On connect of additional outputs, copy our value to their widget
|
||||
this.applyToGraph([{ target_id: target_node.id, target_slot } as LLink])
|
||||
}
|
||||
return valid
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
#onFirstConnection(recreating?: boolean) {
|
||||
// First connection can fire before the graph is ready on initial load so random things can be missing
|
||||
if (!this.outputs[0].links || !this.graph) {
|
||||
this.onLastDisconnect()
|
||||
return
|
||||
}
|
||||
const linkId = this.outputs[0].links[0]
|
||||
const link = this.graph.links[linkId]
|
||||
if (!link) return
|
||||
|
||||
const theirNode = this.graph.getNodeById(link.target_id)
|
||||
if (!theirNode || !theirNode.inputs) return
|
||||
|
||||
const input = theirNode.inputs[link.target_slot]
|
||||
if (!input) return
|
||||
|
||||
let widget
|
||||
if (!input.widget) {
|
||||
if (!(input.type in ComfyWidgets)) return
|
||||
widget = { name: input.name, [GET_CONFIG]: () => [input.type, {}] } //fake widget
|
||||
} else {
|
||||
widget = input.widget
|
||||
}
|
||||
|
||||
// @ts-expect-error fixme ts strict error
|
||||
const config = widget[GET_CONFIG]?.()
|
||||
if (!config) return
|
||||
|
||||
const { type } = getWidgetType(config)
|
||||
// Update our output to restrict to the widget type
|
||||
this.outputs[0].type = type
|
||||
this.outputs[0].name = type
|
||||
this.outputs[0].widget = widget
|
||||
|
||||
this.#createWidget(
|
||||
widget[CONFIG] ?? config,
|
||||
theirNode,
|
||||
widget.name,
|
||||
// @ts-expect-error fixme ts strict error
|
||||
recreating
|
||||
)
|
||||
}
|
||||
|
||||
#createWidget(
|
||||
inputData: InputSpec,
|
||||
node: LGraphNode,
|
||||
widgetName: string,
|
||||
recreating: boolean
|
||||
) {
|
||||
let type = inputData[0]
|
||||
|
||||
if (type instanceof Array) {
|
||||
type = 'COMBO'
|
||||
}
|
||||
|
||||
// Store current size as addWidget resizes the node
|
||||
const [oldWidth, oldHeight] = this.size
|
||||
let widget: IBaseWidget
|
||||
if (isValidWidgetType(type)) {
|
||||
widget = (ComfyWidgets[type](this, 'value', inputData, app) || {}).widget
|
||||
} else {
|
||||
// @ts-expect-error InputSpec is not typed correctly
|
||||
widget = this.addWidget(type, 'value', null, () => {}, {})
|
||||
}
|
||||
|
||||
if (node?.widgets && widget) {
|
||||
const theirWidget = node.widgets.find((w) => w.name === widgetName)
|
||||
if (theirWidget) {
|
||||
widget.value = theirWidget.value
|
||||
}
|
||||
}
|
||||
|
||||
if (
|
||||
!inputData?.[1]?.control_after_generate &&
|
||||
(widget.type === 'number' || widget.type === 'combo')
|
||||
) {
|
||||
let control_value = this.widgets_values?.[1]
|
||||
if (!control_value) {
|
||||
control_value = 'fixed'
|
||||
}
|
||||
addValueControlWidgets(
|
||||
this,
|
||||
widget,
|
||||
control_value as string,
|
||||
undefined,
|
||||
inputData
|
||||
)
|
||||
if (this.widgets?.[1]) widget.linkedWidgets = [this.widgets[1]]
|
||||
|
||||
let filter = this.widgets_values?.[2]
|
||||
if (filter && this.widgets && this.widgets.length === 3) {
|
||||
this.widgets[2].value = filter
|
||||
}
|
||||
}
|
||||
|
||||
// Restore any saved control values
|
||||
const controlValues = this.controlValues
|
||||
if (
|
||||
this.widgets &&
|
||||
this.lastType === this.widgets[0]?.type &&
|
||||
controlValues?.length === this.widgets.length - 1
|
||||
) {
|
||||
for (let i = 0; i < controlValues.length; i++) {
|
||||
this.widgets[i + 1].value = controlValues[i]
|
||||
}
|
||||
}
|
||||
|
||||
// When our value changes, update other widgets to reflect our changes
|
||||
// e.g. so LoadImage shows correct image
|
||||
widget.callback = useChainCallback(widget.callback, () => {
|
||||
this.applyToGraph()
|
||||
})
|
||||
|
||||
// Use the biggest dimensions in case the widgets caused the node to grow
|
||||
this.setSize([
|
||||
Math.max(this.size[0], oldWidth),
|
||||
Math.max(this.size[1], oldHeight)
|
||||
])
|
||||
|
||||
if (!recreating) {
|
||||
// Grow our node more if required
|
||||
const sz = this.computeSize()
|
||||
if (this.size[0] < sz[0]) {
|
||||
this.size[0] = sz[0]
|
||||
}
|
||||
if (this.size[1] < sz[1]) {
|
||||
this.size[1] = sz[1]
|
||||
}
|
||||
|
||||
requestAnimationFrame(() => {
|
||||
this.onResize?.(this.size)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
recreateWidget() {
|
||||
const values = this.widgets?.map((w) => w.value)
|
||||
this.#removeWidgets()
|
||||
this.#onFirstConnection(true)
|
||||
if (values?.length && this.widgets) {
|
||||
for (let i = 0; i < this.widgets.length; i++)
|
||||
this.widgets[i].value = values[i]
|
||||
}
|
||||
return this.widgets?.[0]
|
||||
}
|
||||
|
||||
#mergeWidgetConfig() {
|
||||
// Merge widget configs if the node has multiple outputs
|
||||
const output = this.outputs[0]
|
||||
const links = output.links ?? []
|
||||
|
||||
const hasConfig = !!output.widget?.[CONFIG]
|
||||
if (hasConfig) {
|
||||
delete output.widget?.[CONFIG]
|
||||
}
|
||||
|
||||
if (links?.length < 2 && hasConfig) {
|
||||
// Copy the widget options from the source
|
||||
if (links.length) {
|
||||
this.recreateWidget()
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
const config1 = (output.widget?.[GET_CONFIG] as () => InputSpec)?.()
|
||||
if (!config1) return
|
||||
const isNumber = config1[0] === 'INT' || config1[0] === 'FLOAT'
|
||||
if (!isNumber || !this.graph) return
|
||||
|
||||
for (const linkId of links) {
|
||||
const link = this.graph.links[linkId]
|
||||
if (!link) continue // Can be null when removing a node
|
||||
|
||||
const theirNode = this.graph.getNodeById(link.target_id)
|
||||
if (!theirNode) continue
|
||||
const theirInput = theirNode.inputs[link.target_slot]
|
||||
|
||||
// Call is valid connection so it can merge the configs when validating
|
||||
this.#isValidConnection(theirInput, hasConfig)
|
||||
}
|
||||
}
|
||||
|
||||
#isValidConnection(input: INodeInputSlot, forceUpdate?: boolean) {
|
||||
// Only allow connections where the configs match
|
||||
const output = this.outputs?.[0]
|
||||
const config2 = (input.widget?.[GET_CONFIG] as () => InputSpec)?.()
|
||||
if (!config2) return false
|
||||
|
||||
return !!mergeIfValid.call(
|
||||
this,
|
||||
output,
|
||||
config2,
|
||||
forceUpdate,
|
||||
this.recreateWidget
|
||||
)
|
||||
}
|
||||
|
||||
#removeWidgets() {
|
||||
if (this.widgets) {
|
||||
// Allow widgets to cleanup
|
||||
for (const w of this.widgets) {
|
||||
if (w.onRemove) {
|
||||
w.onRemove()
|
||||
}
|
||||
}
|
||||
|
||||
// Temporarily store the current values in case the node is being recreated
|
||||
// e.g. by group node conversion
|
||||
this.controlValues = []
|
||||
this.lastType = this.widgets[0]?.type
|
||||
for (let i = 1; i < this.widgets.length; i++) {
|
||||
this.controlValues.push(this.widgets[i].value)
|
||||
}
|
||||
setTimeout(() => {
|
||||
delete this.lastType
|
||||
delete this.controlValues
|
||||
}, 15)
|
||||
this.widgets.length = 0
|
||||
}
|
||||
}
|
||||
|
||||
onLastDisconnect() {
|
||||
// We can't remove + re-add the output here as if you drag a link over the same link
|
||||
// it removes, then re-adds, causing it to break
|
||||
this.outputs[0].type = '*'
|
||||
this.outputs[0].name = 'connect to widget input'
|
||||
delete this.outputs[0].widget
|
||||
|
||||
this.#removeWidgets()
|
||||
}
|
||||
}
|
||||
|
||||
export function getWidgetConfig(
|
||||
slot: INodeInputSlot | INodeOutputSlot
|
||||
): InputSpec {
|
||||
return (slot.widget?.[CONFIG] ??
|
||||
(slot.widget?.[GET_CONFIG] as () => InputSpec)?.() ?? [
|
||||
'*',
|
||||
{}
|
||||
]) as InputSpec
|
||||
}
|
||||
|
||||
function getConfig(this: LGraphNode, widgetName: string) {
|
||||
const { nodeData } = this.constructor
|
||||
return (
|
||||
nodeData?.input?.required?.[widgetName] ??
|
||||
nodeData?.input?.optional?.[widgetName]
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a widget to an input slot.
|
||||
* @deprecated Widget to socket conversion is no longer necessary, as they co-exist now.
|
||||
* @param node The node to convert the widget to an input slot for.
|
||||
* @param widget The widget to convert to an input slot.
|
||||
* @returns The input slot that was converted from the widget or undefined if the widget is not found.
|
||||
* @knipIgnoreUnusedButUsedByCustomNodes
|
||||
*/
|
||||
export function convertToInput(
|
||||
node: LGraphNode,
|
||||
widget: IBaseWidget
|
||||
): INodeInputSlot | undefined {
|
||||
console.warn(
|
||||
'Please remove call to convertToInput. Widget to socket conversion is no longer necessary, as they co-exist now.'
|
||||
)
|
||||
return node.inputs.find((slot) => slot.widget?.name === widget.name)
|
||||
}
|
||||
|
||||
function getWidgetType(config: InputSpec) {
|
||||
// Special handling for COMBO so we restrict links based on the entries
|
||||
let type = config[0]
|
||||
if (type instanceof Array) {
|
||||
type = 'COMBO'
|
||||
}
|
||||
return { type }
|
||||
}
|
||||
|
||||
export function setWidgetConfig(slot: INodeInputSlot, config?: InputSpec) {
|
||||
if (!slot.widget) return
|
||||
if (config) {
|
||||
slot.widget[GET_CONFIG] = () => config
|
||||
} else {
|
||||
delete slot.widget
|
||||
}
|
||||
|
||||
if (!(slot instanceof NodeSlot)) return
|
||||
const graph = slot.node.graph
|
||||
if (!graph) return
|
||||
const link = graph.links[slot.link ?? -1]
|
||||
if (!link) return
|
||||
const originNode = graph.getNodeById(link.origin_id)
|
||||
if (!originNode || !isPrimitiveNode(originNode)) return
|
||||
if (config) {
|
||||
originNode.recreateWidget()
|
||||
} else if (!app.configuringGraph) {
|
||||
originNode.disconnectOutput(0)
|
||||
originNode.onLastDisconnect()
|
||||
}
|
||||
}
|
||||
|
||||
export function mergeIfValid(
|
||||
output: INodeOutputSlot | INodeInputSlot,
|
||||
config2: InputSpec,
|
||||
forceUpdate?: boolean,
|
||||
recreateWidget?: () => void,
|
||||
config1?: InputSpec
|
||||
): { customConfig: InputSpec[1] } {
|
||||
if (!config1) {
|
||||
config1 = getWidgetConfig(output)
|
||||
}
|
||||
|
||||
const customSpec = mergeInputSpec(config1, config2)
|
||||
|
||||
if (customSpec || forceUpdate) {
|
||||
if (customSpec) {
|
||||
// @ts-expect-error fixme ts strict error
|
||||
output.widget[CONFIG] = customSpec
|
||||
}
|
||||
|
||||
// @ts-expect-error fixme ts strict error
|
||||
const widget = recreateWidget?.call(this)
|
||||
// When deleting a node this can be null
|
||||
if (widget) {
|
||||
// @ts-expect-error fixme ts strict error
|
||||
const min = widget.options.min
|
||||
// @ts-expect-error fixme ts strict error
|
||||
const max = widget.options.max
|
||||
// @ts-expect-error fixme ts strict error
|
||||
if (min != null && widget.value < min) widget.value = min
|
||||
// @ts-expect-error fixme ts strict error
|
||||
if (max != null && widget.value > max) widget.value = max
|
||||
// @ts-expect-error fixme ts strict error
|
||||
widget.callback(widget.value)
|
||||
}
|
||||
}
|
||||
|
||||
return { customConfig: customSpec?.[1] ?? {} }
|
||||
}
|
||||
|
||||
app.registerExtension({
|
||||
name: 'Comfy.WidgetInputs',
|
||||
async beforeRegisterNodeDef(nodeType, _nodeData, app) {
|
||||
// @ts-expect-error adding extra property
|
||||
nodeType.prototype.convertWidgetToInput = function (this: LGraphNode) {
|
||||
console.warn(
|
||||
'Please remove call to convertWidgetToInput. Widget to socket conversion is no longer necessary, as they co-exist now.'
|
||||
)
|
||||
return false
|
||||
}
|
||||
|
||||
nodeType.prototype.onGraphConfigured = useChainCallback(
|
||||
nodeType.prototype.onGraphConfigured,
|
||||
function (this: LGraphNode) {
|
||||
if (!this.inputs) return
|
||||
this.widgets ??= []
|
||||
|
||||
for (const input of this.inputs) {
|
||||
if (input.widget) {
|
||||
const name = input.widget.name
|
||||
if (!input.widget[GET_CONFIG]) {
|
||||
input.widget[GET_CONFIG] = () => getConfig.call(this, name)
|
||||
}
|
||||
|
||||
const w = this.widgets?.find((w) => w.name === name)
|
||||
if (!w) {
|
||||
this.removeInput(this.inputs.findIndex((i) => i === input))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
nodeType.prototype.onConfigure = useChainCallback(
|
||||
nodeType.prototype.onConfigure,
|
||||
function (this: LGraphNode) {
|
||||
if (!app.configuringGraph && this.inputs) {
|
||||
// On copy + paste of nodes, ensure that widget configs are set up
|
||||
for (const input of this.inputs) {
|
||||
if (input.widget && !input.widget[GET_CONFIG]) {
|
||||
const name = input.widget.name
|
||||
input.widget[GET_CONFIG] = () => getConfig.call(this, name)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
// Double click a widget input to automatically attach a primitive
|
||||
const origOnInputDblClick = nodeType.prototype.onInputDblClick
|
||||
nodeType.prototype.onInputDblClick = function (
|
||||
this: LGraphNode,
|
||||
...[slot, ...args]: CallbackParams<typeof origOnInputDblClick>
|
||||
) {
|
||||
const r = origOnInputDblClick?.apply(this, [slot, ...args])
|
||||
|
||||
const input = this.inputs[slot]
|
||||
if (!input.widget) {
|
||||
// Not a widget input or already handled input
|
||||
if (
|
||||
!(input.type in ComfyWidgets) &&
|
||||
!(
|
||||
(
|
||||
input.widget?.[GET_CONFIG] as (() => InputSpec) | undefined
|
||||
)?.()?.[0] instanceof Array
|
||||
)
|
||||
) {
|
||||
return r //also Not a ComfyWidgets input or combo (do nothing)
|
||||
}
|
||||
}
|
||||
|
||||
// Create a primitive node
|
||||
const node = LiteGraph.createNode('PrimitiveNode')
|
||||
const graph = app.canvas.graph
|
||||
if (!node || !graph) return r
|
||||
|
||||
graph?.add(node)
|
||||
|
||||
// Calculate a position that won't directly overlap another node
|
||||
const pos: [number, number] = [
|
||||
this.pos[0] - node.size[0] - 30,
|
||||
this.pos[1]
|
||||
]
|
||||
while (graph.getNodeOnPos(pos[0], pos[1], graph.nodes))
|
||||
pos[1] += LiteGraph.NODE_TITLE_HEIGHT
|
||||
|
||||
node.pos = pos
|
||||
node.connect(0, this, slot)
|
||||
node.title = input.name
|
||||
|
||||
return r
|
||||
}
|
||||
},
|
||||
registerCustomNodes() {
|
||||
LiteGraph.registerNodeType(
|
||||
'PrimitiveNode',
|
||||
Object.assign(PrimitiveNode, {
|
||||
title: 'Primitive'
|
||||
})
|
||||
)
|
||||
PrimitiveNode.category = 'utils'
|
||||
}
|
||||
})
|
||||
@@ -0,0 +1,491 @@
|
||||
import {
|
||||
SUBGRAPH_INPUT_ID,
|
||||
SUBGRAPH_OUTPUT_ID
|
||||
} from '@/lib/litegraph/src/constants'
|
||||
import type { SubgraphInput } from '@/lib/litegraph/src/subgraph/SubgraphInput'
|
||||
import type { SubgraphOutput } from '@/lib/litegraph/src/subgraph/SubgraphOutput'
|
||||
import { useLayoutMutations } from '@/renderer/core/layout/operations/layoutMutations'
|
||||
import { LayoutSource } from '@/renderer/core/layout/types'
|
||||
|
||||
import type { LGraphNode, NodeId } from './LGraphNode'
|
||||
import type { Reroute, RerouteId } from './Reroute'
|
||||
import type {
|
||||
CanvasColour,
|
||||
INodeInputSlot,
|
||||
INodeOutputSlot,
|
||||
ISlotType,
|
||||
LinkNetwork,
|
||||
LinkSegment,
|
||||
Point,
|
||||
ReadonlyLinkNetwork
|
||||
} from './interfaces'
|
||||
import type { Serialisable, SerialisableLLink } from './types/serialisation'
|
||||
|
||||
const layoutMutations = useLayoutMutations()
|
||||
|
||||
export type LinkId = number
|
||||
|
||||
export type SerialisedLLinkArray = [
|
||||
id: LinkId,
|
||||
origin_id: NodeId,
|
||||
origin_slot: number,
|
||||
target_id: NodeId,
|
||||
target_slot: number,
|
||||
type: ISlotType
|
||||
]
|
||||
|
||||
// Resolved connection union; eliminates subgraph in/out as a possibility
|
||||
export type ResolvedConnection = BaseResolvedConnection &
|
||||
(
|
||||
| (ResolvedSubgraphInput & ResolvedNormalOutput)
|
||||
| (ResolvedNormalInput & ResolvedSubgraphOutput)
|
||||
| (ResolvedNormalInput & ResolvedNormalOutput)
|
||||
)
|
||||
|
||||
interface BaseResolvedConnection {
|
||||
link: LLink
|
||||
/** The node on the input side of the link (owns {@link input}) */
|
||||
inputNode?: LGraphNode
|
||||
/** The input the link is connected to (mutually exclusive with {@link subgraphOutput}) */
|
||||
input?: INodeInputSlot
|
||||
/** The node on the output side of the link (owns {@link output}) */
|
||||
outputNode?: LGraphNode
|
||||
/** The output the link is connected to (mutually exclusive with {@link subgraphInput}) */
|
||||
output?: INodeOutputSlot
|
||||
/** The subgraph output the link is connected to (mutually exclusive with {@link input}) */
|
||||
subgraphOutput?: SubgraphOutput
|
||||
/** The subgraph input the link is connected to (mutually exclusive with {@link output}) */
|
||||
subgraphInput?: SubgraphInput
|
||||
}
|
||||
|
||||
interface ResolvedNormalInput {
|
||||
inputNode: LGraphNode | undefined
|
||||
input: INodeInputSlot | undefined
|
||||
subgraphOutput?: undefined
|
||||
}
|
||||
|
||||
interface ResolvedNormalOutput {
|
||||
outputNode: LGraphNode | undefined
|
||||
output: INodeOutputSlot | undefined
|
||||
subgraphInput?: undefined
|
||||
}
|
||||
|
||||
interface ResolvedSubgraphInput {
|
||||
inputNode?: undefined
|
||||
/** The actual input slot the link is connected to (mutually exclusive with {@link subgraphOutput}) */
|
||||
input?: undefined
|
||||
subgraphOutput: SubgraphOutput
|
||||
}
|
||||
|
||||
interface ResolvedSubgraphOutput {
|
||||
outputNode?: undefined
|
||||
output?: undefined
|
||||
subgraphInput: SubgraphInput
|
||||
}
|
||||
|
||||
type BasicReadonlyNetwork = Pick<
|
||||
ReadonlyLinkNetwork,
|
||||
'getNodeById' | 'links' | 'getLink' | 'inputNode' | 'outputNode'
|
||||
>
|
||||
|
||||
// this is the class in charge of storing link information
|
||||
export class LLink implements LinkSegment, Serialisable<SerialisableLLink> {
|
||||
static _drawDebug = false
|
||||
|
||||
/** Link ID */
|
||||
id: LinkId
|
||||
parentId?: RerouteId
|
||||
type: ISlotType
|
||||
/** Output node ID */
|
||||
origin_id: NodeId
|
||||
/** Output slot index */
|
||||
origin_slot: number
|
||||
/** Input node ID */
|
||||
target_id: NodeId
|
||||
/** Input slot index */
|
||||
target_slot: number
|
||||
|
||||
data?: number | string | boolean | { toToolTip?(): string }
|
||||
_data?: unknown
|
||||
/** Centre point of the link, calculated during render only - can be inaccurate */
|
||||
_pos: Point
|
||||
/** @todo Clean up - never implemented in comfy. */
|
||||
_last_time?: number
|
||||
/** The last canvas 2D path that was used to render this link */
|
||||
path?: Path2D
|
||||
/** @inheritdoc */
|
||||
_centreAngle?: number
|
||||
|
||||
/** @inheritdoc */
|
||||
_dragging?: boolean
|
||||
|
||||
#color?: CanvasColour | null
|
||||
/** Custom colour for this link only */
|
||||
public get color(): CanvasColour | null | undefined {
|
||||
return this.#color
|
||||
}
|
||||
|
||||
public set color(value: CanvasColour) {
|
||||
this.#color = value === '' ? null : value
|
||||
}
|
||||
|
||||
public get isFloatingOutput(): boolean {
|
||||
return this.origin_id === -1 && this.origin_slot === -1
|
||||
}
|
||||
|
||||
public get isFloatingInput(): boolean {
|
||||
return this.target_id === -1 && this.target_slot === -1
|
||||
}
|
||||
|
||||
public get isFloating(): boolean {
|
||||
return this.isFloatingOutput || this.isFloatingInput
|
||||
}
|
||||
|
||||
/** `true` if this link is connected to a subgraph input node (the actual origin is in a different graph). */
|
||||
get originIsIoNode(): boolean {
|
||||
return this.origin_id === SUBGRAPH_INPUT_ID
|
||||
}
|
||||
|
||||
/** `true` if this link is connected to a subgraph output node (the actual target is in a different graph). */
|
||||
get targetIsIoNode(): boolean {
|
||||
return this.target_id === SUBGRAPH_OUTPUT_ID
|
||||
}
|
||||
|
||||
constructor(
|
||||
id: LinkId,
|
||||
type: ISlotType,
|
||||
origin_id: NodeId,
|
||||
origin_slot: number,
|
||||
target_id: NodeId,
|
||||
target_slot: number,
|
||||
parentId?: RerouteId
|
||||
) {
|
||||
this.id = id
|
||||
this.type = type
|
||||
this.origin_id = origin_id
|
||||
this.origin_slot = origin_slot
|
||||
this.target_id = target_id
|
||||
this.target_slot = target_slot
|
||||
this.parentId = parentId
|
||||
|
||||
this._data = null
|
||||
// center
|
||||
this._pos = [0, 0]
|
||||
}
|
||||
|
||||
/** @deprecated Use {@link LLink.create} */
|
||||
static createFromArray(data: SerialisedLLinkArray): LLink {
|
||||
return new LLink(data[0], data[5], data[1], data[2], data[3], data[4])
|
||||
}
|
||||
|
||||
/**
|
||||
* LLink static factory: creates a new LLink from the provided data.
|
||||
* @param data Serialised LLink data to create the link from
|
||||
* @returns A new LLink
|
||||
*/
|
||||
static create(data: SerialisableLLink): LLink {
|
||||
return new LLink(
|
||||
data.id,
|
||||
data.type,
|
||||
data.origin_id,
|
||||
data.origin_slot,
|
||||
data.target_id,
|
||||
data.target_slot,
|
||||
data.parentId
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets all reroutes from the output slot to this segment. If this segment is a reroute, it will not be included.
|
||||
* @returns An ordered array of all reroutes from the node output to
|
||||
* this reroute or the reroute before it. Otherwise, an empty array.
|
||||
*/
|
||||
static getReroutes(
|
||||
network: Pick<ReadonlyLinkNetwork, 'reroutes'>,
|
||||
linkSegment: LinkSegment
|
||||
): Reroute[] {
|
||||
if (linkSegment.parentId === undefined) return []
|
||||
return network.reroutes.get(linkSegment.parentId)?.getReroutes() ?? []
|
||||
}
|
||||
|
||||
static getFirstReroute(
|
||||
network: Pick<ReadonlyLinkNetwork, 'reroutes'>,
|
||||
linkSegment: LinkSegment
|
||||
): Reroute | undefined {
|
||||
return LLink.getReroutes(network, linkSegment).at(0)
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds the reroute in the chain after the provided reroute ID.
|
||||
* @param network The network this link belongs to
|
||||
* @param linkSegment The starting point of the search (input side).
|
||||
* Typically the LLink object itself, but can be any link segment.
|
||||
* @param rerouteId The matching reroute will have this set as its {@link parentId}.
|
||||
* @returns The reroute that was found, `undefined` if no reroute was found, or `null` if an infinite loop was detected.
|
||||
*/
|
||||
static findNextReroute(
|
||||
network: Pick<ReadonlyLinkNetwork, 'reroutes'>,
|
||||
linkSegment: LinkSegment,
|
||||
rerouteId: RerouteId
|
||||
): Reroute | null | undefined {
|
||||
if (linkSegment.parentId === undefined) return
|
||||
return network.reroutes
|
||||
.get(linkSegment.parentId)
|
||||
?.findNextReroute(rerouteId)
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the origin node of a link.
|
||||
* @param network The network to search
|
||||
* @param linkId The ID of the link to get the origin node of
|
||||
* @returns The origin node of the link, or `undefined` if the link is not found or the origin node is not found
|
||||
*/
|
||||
static getOriginNode(
|
||||
network: BasicReadonlyNetwork,
|
||||
linkId: LinkId
|
||||
): LGraphNode | undefined {
|
||||
const id = network.links.get(linkId)?.origin_id
|
||||
return network.getNodeById(id) ?? undefined
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the target node of a link.
|
||||
* @param network The network to search
|
||||
* @param linkId The ID of the link to get the target node of
|
||||
* @returns The target node of the link, or `undefined` if the link is not found or the target node is not found
|
||||
*/
|
||||
static getTargetNode(
|
||||
network: BasicReadonlyNetwork,
|
||||
linkId: LinkId
|
||||
): LGraphNode | undefined {
|
||||
const id = network.links.get(linkId)?.target_id
|
||||
return network.getNodeById(id) ?? undefined
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolves a link ID to the link, node, and slot objects.
|
||||
* @param linkId The {@link id} of the link to resolve
|
||||
* @param network The link network to search
|
||||
* @returns An object containing the input and output nodes, as well as the input and output slots.
|
||||
* @remarks This method is heavier than others; it will always resolve all objects.
|
||||
* Whilst the performance difference should in most cases be negligible,
|
||||
* it is recommended to use simpler methods where appropriate.
|
||||
*/
|
||||
static resolve(
|
||||
linkId: LinkId | null | undefined,
|
||||
network: BasicReadonlyNetwork
|
||||
): ResolvedConnection | undefined {
|
||||
return network.getLink(linkId)?.resolve(network)
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolves a list of link IDs to the link, node, and slot objects.
|
||||
* Discards invalid link IDs.
|
||||
* @param linkIds An iterable of link {@link id}s to resolve
|
||||
* @param network The link network to search
|
||||
* @returns An array of resolved connections. If a link is not found, it is not included in the array.
|
||||
* @see {@link LLink.resolve}
|
||||
*/
|
||||
static resolveMany(
|
||||
linkIds: Iterable<LinkId>,
|
||||
network: BasicReadonlyNetwork
|
||||
): ResolvedConnection[] {
|
||||
const resolved: ResolvedConnection[] = []
|
||||
for (const id of linkIds) {
|
||||
const r = network.getLink(id)?.resolve(network)
|
||||
if (r) resolved.push(r)
|
||||
}
|
||||
return resolved
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolves the primitive ID values stored in the link to the referenced objects.
|
||||
* @param network The link network to search
|
||||
* @returns An object containing the input and output nodes, as well as the input and output slots.
|
||||
* @remarks This method is heavier than others; it will always resolve all objects.
|
||||
* Whilst the performance difference should in most cases be negligible,
|
||||
* it is recommended to use simpler methods where appropriate.
|
||||
*/
|
||||
resolve(network: BasicReadonlyNetwork): ResolvedConnection {
|
||||
const inputNode =
|
||||
this.target_id === -1
|
||||
? undefined
|
||||
: (network.getNodeById(this.target_id) ?? undefined)
|
||||
const input = inputNode?.inputs[this.target_slot]
|
||||
const subgraphInput = this.originIsIoNode
|
||||
? network.inputNode?.slots[this.origin_slot]
|
||||
: undefined
|
||||
if (subgraphInput) {
|
||||
return { inputNode, input, subgraphInput, link: this }
|
||||
}
|
||||
|
||||
const outputNode =
|
||||
this.origin_id === -1
|
||||
? undefined
|
||||
: (network.getNodeById(this.origin_id) ?? undefined)
|
||||
const output = outputNode?.outputs[this.origin_slot]
|
||||
const subgraphOutput = this.targetIsIoNode
|
||||
? network.outputNode?.slots[this.target_slot]
|
||||
: undefined
|
||||
if (subgraphOutput) {
|
||||
return {
|
||||
outputNode,
|
||||
output,
|
||||
subgraphInput: undefined,
|
||||
subgraphOutput,
|
||||
link: this
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
inputNode,
|
||||
outputNode,
|
||||
input,
|
||||
output,
|
||||
subgraphInput,
|
||||
subgraphOutput,
|
||||
link: this
|
||||
}
|
||||
}
|
||||
|
||||
configure(o: LLink | SerialisedLLinkArray) {
|
||||
if (Array.isArray(o)) {
|
||||
this.id = o[0]
|
||||
this.origin_id = o[1]
|
||||
this.origin_slot = o[2]
|
||||
this.target_id = o[3]
|
||||
this.target_slot = o[4]
|
||||
this.type = o[5]
|
||||
} else {
|
||||
this.id = o.id
|
||||
this.type = o.type
|
||||
this.origin_id = o.origin_id
|
||||
this.origin_slot = o.origin_slot
|
||||
this.target_id = o.target_id
|
||||
this.target_slot = o.target_slot
|
||||
this.parentId = o.parentId
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the specified node id and output index are this link's origin (output side).
|
||||
* @param nodeId ID of the node to check
|
||||
* @param outputIndex The array index of the node output
|
||||
* @returns `true` if the origin matches, otherwise `false`.
|
||||
*/
|
||||
hasOrigin(nodeId: NodeId, outputIndex: number): boolean {
|
||||
return this.origin_id === nodeId && this.origin_slot === outputIndex
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the specified node id and input index are this link's target (input side).
|
||||
* @param nodeId ID of the node to check
|
||||
* @param inputIndex The array index of the node input
|
||||
* @returns `true` if the target matches, otherwise `false`.
|
||||
*/
|
||||
hasTarget(nodeId: NodeId, inputIndex: number): boolean {
|
||||
return this.target_id === nodeId && this.target_slot === inputIndex
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a floating link from this link.
|
||||
* @param slotType The side of the link that is still connected
|
||||
* @param parentId The parent reroute ID of the link
|
||||
* @returns A new LLink that is floating
|
||||
*/
|
||||
toFloating(slotType: 'input' | 'output', parentId: RerouteId): LLink {
|
||||
const exported = this.asSerialisable()
|
||||
exported.id = -1
|
||||
exported.parentId = parentId
|
||||
|
||||
if (slotType === 'input') {
|
||||
exported.origin_id = -1
|
||||
exported.origin_slot = -1
|
||||
} else {
|
||||
exported.target_id = -1
|
||||
exported.target_slot = -1
|
||||
}
|
||||
|
||||
return LLink.create(exported)
|
||||
}
|
||||
|
||||
/**
|
||||
* Disconnects a link and removes it from the graph, cleaning up any reroutes that are no longer used
|
||||
* @param network The container (LGraph) where reroutes should be updated
|
||||
* @param keepReroutes If `undefined`, reroutes will be automatically removed if no links remain.
|
||||
* If `input` or `output`, reroutes will not be automatically removed, and retain a connection to the input or output, respectively.
|
||||
*/
|
||||
disconnect(network: LinkNetwork, keepReroutes?: 'input' | 'output'): void {
|
||||
const reroutes = LLink.getReroutes(network, this)
|
||||
|
||||
const lastReroute = reroutes.at(-1)
|
||||
|
||||
// When floating from output, 1-to-1 ratio of floating link to final reroute (tree-like)
|
||||
const outputFloating =
|
||||
keepReroutes === 'output' &&
|
||||
lastReroute?.linkIds.size === 1 &&
|
||||
lastReroute.floatingLinkIds.size === 0
|
||||
|
||||
// When floating from inputs, the final (input side) reroute may have many floating links
|
||||
if (outputFloating || (keepReroutes === 'input' && lastReroute)) {
|
||||
const newLink = LLink.create(this)
|
||||
newLink.id = -1
|
||||
|
||||
if (keepReroutes === 'input') {
|
||||
newLink.origin_id = -1
|
||||
newLink.origin_slot = -1
|
||||
|
||||
lastReroute.floating = { slotType: 'input' }
|
||||
} else {
|
||||
newLink.target_id = -1
|
||||
newLink.target_slot = -1
|
||||
|
||||
lastReroute.floating = { slotType: 'output' }
|
||||
}
|
||||
|
||||
network.addFloatingLink(newLink)
|
||||
}
|
||||
|
||||
for (const reroute of reroutes) {
|
||||
reroute.linkIds.delete(this.id)
|
||||
if (!keepReroutes && !reroute.totalLinks) {
|
||||
network.reroutes.delete(reroute.id)
|
||||
// Delete reroute from Layout Store
|
||||
layoutMutations.setSource(LayoutSource.Canvas)
|
||||
layoutMutations.deleteReroute(reroute.id)
|
||||
}
|
||||
}
|
||||
network.links.delete(this.id)
|
||||
// Delete link from Layout Store
|
||||
layoutMutations.setSource(LayoutSource.Canvas)
|
||||
layoutMutations.deleteLink(this.id)
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated Prefer {@link LLink.asSerialisable} (returns an object, not an array)
|
||||
* @returns An array representing this LLink
|
||||
*/
|
||||
serialize(): SerialisedLLinkArray {
|
||||
return [
|
||||
this.id,
|
||||
this.origin_id,
|
||||
this.origin_slot,
|
||||
this.target_id,
|
||||
this.target_slot,
|
||||
this.type
|
||||
]
|
||||
}
|
||||
|
||||
asSerialisable(): SerialisableLLink {
|
||||
const copy: SerialisableLLink = {
|
||||
id: this.id,
|
||||
origin_id: this.origin_id,
|
||||
origin_slot: this.origin_slot,
|
||||
target_id: this.target_id,
|
||||
target_slot: this.target_slot,
|
||||
type: this.type
|
||||
}
|
||||
if (this.parentId !== undefined) copy.parentId = this.parentId
|
||||
return copy
|
||||
}
|
||||
}
|
||||
+408
@@ -0,0 +1,408 @@
|
||||
import type { LGraph } from '@/lib/litegraph/src/LGraph'
|
||||
import type { LGraphNode, NodeId } from '@/lib/litegraph/src/LGraphNode'
|
||||
import { InvalidLinkError } from '@/lib/litegraph/src/infrastructure/InvalidLinkError'
|
||||
import { NullGraphError } from '@/lib/litegraph/src/infrastructure/NullGraphError'
|
||||
import { RecursionError } from '@/lib/litegraph/src/infrastructure/RecursionError'
|
||||
import { SlotIndexError } from '@/lib/litegraph/src/infrastructure/SlotIndexError'
|
||||
import type {
|
||||
CallbackParams,
|
||||
CallbackReturn,
|
||||
ISlotType
|
||||
} from '@/lib/litegraph/src/interfaces'
|
||||
import { LGraphEventMode, LiteGraph } from '@/lib/litegraph/src/litegraph'
|
||||
|
||||
import type { Subgraph } from './Subgraph'
|
||||
import type { SubgraphNode } from './SubgraphNode'
|
||||
|
||||
export type ExecutionId = string
|
||||
|
||||
/**
|
||||
* Interface describing the data transfer objects used when compiling a graph for execution.
|
||||
*/
|
||||
export type ExecutableLGraphNode = Omit<
|
||||
ExecutableNodeDTO,
|
||||
'graph' | 'node' | 'subgraphNode'
|
||||
>
|
||||
|
||||
/**
|
||||
* The end result of resolving a DTO input.
|
||||
* When a widget value is returned, {@link widgetInfo} is present and {@link origin_slot} is `-1`.
|
||||
*/
|
||||
type ResolvedInput = {
|
||||
/** DTO for the node that the link originates from. */
|
||||
node: ExecutableLGraphNode
|
||||
/** Full unique execution ID of the node that the link originates from. In the case of a widget value, this is the ID of the subgraph node. */
|
||||
origin_id: ExecutionId
|
||||
/** The slot index of the output on the node that the link originates from. `-1` when widget value is set. */
|
||||
origin_slot: number
|
||||
/** Boxed widget value (e.g. for widgets). If this box is `undefined`, then an input link is connected, and widget values from the subgraph node are ignored. */
|
||||
widgetInfo?: { value: unknown }
|
||||
}
|
||||
|
||||
/**
|
||||
* Concrete implementation of {@link ExecutableLGraphNode}.
|
||||
* @remarks This is the class that is used to create the data transfer objects for executable nodes.
|
||||
*/
|
||||
export class ExecutableNodeDTO implements ExecutableLGraphNode {
|
||||
applyToGraph?(
|
||||
...args: CallbackParams<typeof this.node.applyToGraph>
|
||||
): CallbackReturn<typeof this.node.applyToGraph>
|
||||
|
||||
/** The graph that this node is a part of. */
|
||||
readonly graph: LGraph | Subgraph
|
||||
|
||||
inputs: { linkId: number | null; name: string; type: ISlotType }[]
|
||||
|
||||
/** Backing field for {@link id}. */
|
||||
#id: ExecutionId
|
||||
|
||||
/**
|
||||
* The path to the actual node through subgraph instances, represented as a list of all subgraph node IDs (instances),
|
||||
* followed by the actual original node ID within the subgraph. Each segment is separated by `:`.
|
||||
*
|
||||
* e.g. `1:2:3`:
|
||||
* - `1` is the node ID of the first subgraph node in the parent workflow
|
||||
* - `2` is the node ID of the second subgraph node in the first subgraph
|
||||
* - `3` is the node ID of the actual node in the subgraph definition
|
||||
*/
|
||||
get id() {
|
||||
return this.#id
|
||||
}
|
||||
|
||||
get type() {
|
||||
return this.node.type
|
||||
}
|
||||
|
||||
get title() {
|
||||
return this.node.title
|
||||
}
|
||||
|
||||
get mode() {
|
||||
return this.node.mode
|
||||
}
|
||||
|
||||
get comfyClass() {
|
||||
return this.node.comfyClass
|
||||
}
|
||||
|
||||
get isVirtualNode() {
|
||||
return this.node.isVirtualNode
|
||||
}
|
||||
|
||||
get widgets() {
|
||||
return this.node.widgets
|
||||
}
|
||||
|
||||
get subgraphId() {
|
||||
return this.subgraphNode?.subgraph.id
|
||||
}
|
||||
|
||||
constructor(
|
||||
/** The actual node that this DTO wraps. */
|
||||
readonly node: LGraphNode | SubgraphNode,
|
||||
/** A list of subgraph instance node IDs from the root graph to the containing instance. @see {@link id} */
|
||||
readonly subgraphNodePath: readonly NodeId[],
|
||||
/** A flattened map of all DTOs in this node network. Subgraph instances have been expanded into their inner nodes. */
|
||||
readonly nodesByExecutionId: Map<ExecutionId, ExecutableLGraphNode>,
|
||||
/** The actual subgraph instance that contains this node, otherwise undefined. */
|
||||
readonly subgraphNode?: SubgraphNode
|
||||
) {
|
||||
if (!node.graph) throw new NullGraphError()
|
||||
|
||||
// Set the internal ID of the DTO
|
||||
this.#id = [...this.subgraphNodePath, this.node.id].join(':')
|
||||
this.graph = node.graph
|
||||
this.inputs = this.node.inputs.map((x) => ({
|
||||
linkId: x.link,
|
||||
name: x.name,
|
||||
type: x.type
|
||||
}))
|
||||
|
||||
// Only create a wrapper if the node has an applyToGraph method
|
||||
if (this.node.applyToGraph) {
|
||||
this.applyToGraph = (...args) => this.node.applyToGraph?.(...args)
|
||||
}
|
||||
}
|
||||
|
||||
/** Returns either the DTO itself, or the DTOs of the inner nodes of the subgraph. */
|
||||
getInnerNodes(): ExecutableLGraphNode[] {
|
||||
return this.node.isSubgraphNode()
|
||||
? this.node.getInnerNodes(this.nodesByExecutionId, this.subgraphNodePath)
|
||||
: [this]
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolves the executable node & link IDs for a given input slot.
|
||||
* @param slot The slot index of the input.
|
||||
* @param visited Leave empty unless overriding this method.
|
||||
* A set of unique IDs, used to guard against infinite recursion.
|
||||
* If overriding, ensure that the set is passed on all recursive calls.
|
||||
* @returns The node and the origin ID / slot index of the output.
|
||||
*/
|
||||
resolveInput(
|
||||
slot: number,
|
||||
visited = new Set<string>(),
|
||||
type?: ISlotType
|
||||
): ResolvedInput | undefined {
|
||||
const uniqueId = `${this.subgraphNode?.subgraph.id}:${this.node.id}[I]${slot}`
|
||||
if (visited.has(uniqueId)) {
|
||||
const nodeInfo = `${this.node.id}${this.node.title ? ` (${this.node.title})` : ''}`
|
||||
const pathInfo =
|
||||
this.subgraphNodePath.length > 0
|
||||
? ` at path ${this.subgraphNodePath.join(':')}`
|
||||
: ''
|
||||
throw new RecursionError(
|
||||
`Circular reference detected while resolving input ${slot} of node ${nodeInfo}${pathInfo}. ` +
|
||||
`This creates an infinite loop in link resolution. UniqueID: [${uniqueId}]`
|
||||
)
|
||||
}
|
||||
visited.add(uniqueId)
|
||||
|
||||
const input = this.inputs.at(slot)
|
||||
if (!input)
|
||||
throw new SlotIndexError(
|
||||
`No input found for flattened id [${this.id}] slot [${slot}]`
|
||||
)
|
||||
|
||||
// Nothing connected
|
||||
if (input.linkId == null) return
|
||||
|
||||
const link = this.graph.getLink(input.linkId)
|
||||
if (!link)
|
||||
throw new InvalidLinkError(
|
||||
`No link found in parent graph for id [${this.id}] slot [${slot}] ${input.name}`
|
||||
)
|
||||
|
||||
const { subgraphNode } = this
|
||||
|
||||
// Link goes up and out of this subgraph
|
||||
if (subgraphNode && link.originIsIoNode) {
|
||||
const subgraphNodeInput = subgraphNode.inputs.at(link.origin_slot)
|
||||
if (!subgraphNodeInput)
|
||||
throw new SlotIndexError(
|
||||
`No input found for slot [${link.origin_slot}] ${input.name}`
|
||||
)
|
||||
|
||||
// Nothing connected
|
||||
const linkId = subgraphNodeInput.link
|
||||
if (linkId == null) {
|
||||
const widget = subgraphNode.getWidgetFromSlot(subgraphNodeInput)
|
||||
if (!widget) return
|
||||
|
||||
// Special case: SubgraphNode widget.
|
||||
return {
|
||||
node: this,
|
||||
origin_id: this.id,
|
||||
origin_slot: -1,
|
||||
widgetInfo: { value: widget.value }
|
||||
}
|
||||
}
|
||||
|
||||
const outerLink = subgraphNode.graph.getLink(linkId)
|
||||
if (!outerLink)
|
||||
throw new InvalidLinkError(
|
||||
`No outer link found for slot [${link.origin_slot}] ${input.name}`
|
||||
)
|
||||
|
||||
const subgraphNodeExecutionId = this.subgraphNodePath.join(':')
|
||||
const subgraphNodeDto = this.nodesByExecutionId.get(
|
||||
subgraphNodeExecutionId
|
||||
)
|
||||
if (!subgraphNodeDto)
|
||||
throw new Error(
|
||||
`No subgraph node DTO found for id [${subgraphNodeExecutionId}]`
|
||||
)
|
||||
|
||||
return subgraphNodeDto.resolveInput(outerLink.target_slot, visited)
|
||||
}
|
||||
|
||||
// Not part of a subgraph; use the original link
|
||||
const outputNode = this.graph.getNodeById(link.origin_id)
|
||||
if (!outputNode)
|
||||
throw new InvalidLinkError(
|
||||
`No input node found for id [${this.id}] slot [${slot}] ${input.name}`
|
||||
)
|
||||
|
||||
const outputNodeExecutionId = [
|
||||
...this.subgraphNodePath,
|
||||
outputNode.id
|
||||
].join(':')
|
||||
const outputNodeDto = this.nodesByExecutionId.get(outputNodeExecutionId)
|
||||
if (!outputNodeDto)
|
||||
throw new Error(
|
||||
`No output node DTO found for id [${outputNodeExecutionId}]`
|
||||
)
|
||||
|
||||
return outputNodeDto.resolveOutput(
|
||||
link.origin_slot,
|
||||
type ?? input.type,
|
||||
visited
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines whether this output is a valid endpoint for a link (non-virtual, non-bypass).
|
||||
* @param slot The slot index of the output.
|
||||
* @param type The type of the input
|
||||
* @param visited A set of unique IDs to guard against infinite recursion. See {@link resolveInput}.
|
||||
* @returns The node and the origin ID / slot index of the output.
|
||||
*/
|
||||
resolveOutput(
|
||||
slot: number,
|
||||
type: ISlotType,
|
||||
visited: Set<string>
|
||||
): ResolvedInput | undefined {
|
||||
const uniqueId = `${this.subgraphNode?.subgraph.id}:${this.node.id}[O]${slot}`
|
||||
if (visited.has(uniqueId)) {
|
||||
const nodeInfo = `${this.node.id}${this.node.title ? ` (${this.node.title})` : ''}`
|
||||
const pathInfo =
|
||||
this.subgraphNodePath.length > 0
|
||||
? ` at path ${this.subgraphNodePath.join(':')}`
|
||||
: ''
|
||||
throw new RecursionError(
|
||||
`Circular reference detected while resolving output ${slot} of node ${nodeInfo}${pathInfo}. ` +
|
||||
`This creates an infinite loop in link resolution. UniqueID: [${uniqueId}]`
|
||||
)
|
||||
}
|
||||
visited.add(uniqueId)
|
||||
|
||||
// Upstreamed: Bypass nodes are bypassed using the first input with matching type
|
||||
if (this.mode === LGraphEventMode.BYPASS) {
|
||||
// Bypass nodes by finding first input with matching type
|
||||
const matchingIndex = this.#getBypassSlotIndex(slot, type)
|
||||
|
||||
// No input types match - bypass not possible
|
||||
if (matchingIndex === -1) {
|
||||
console.warn(
|
||||
`[ExecutableNodeDTO.resolveOutput] No input types match type [${type}] for id [${this.id}] slot [${slot}]`,
|
||||
this
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
return this.resolveInput(matchingIndex, visited)
|
||||
}
|
||||
|
||||
const { node } = this
|
||||
if (node.isSubgraphNode())
|
||||
return this.#resolveSubgraphOutput(slot, type, visited)
|
||||
|
||||
if (node.isVirtualNode) {
|
||||
const virtualLink = this.node.getInputLink(slot)
|
||||
if (virtualLink) {
|
||||
const { inputNode } = virtualLink.resolve(this.graph)
|
||||
if (!inputNode)
|
||||
throw new InvalidLinkError(
|
||||
`Virtual node failed to resolve parent [${this.id}] slot [${slot}]`
|
||||
)
|
||||
|
||||
const inputNodeExecutionId = [
|
||||
...this.subgraphNodePath,
|
||||
inputNode.id
|
||||
].join(':')
|
||||
const inputNodeDto = this.nodesByExecutionId.get(inputNodeExecutionId)
|
||||
if (!inputNodeDto)
|
||||
throw new Error(`No input node DTO found for id [${inputNode.id}]`)
|
||||
|
||||
return inputNodeDto.resolveInput(virtualLink.target_slot, visited, type)
|
||||
}
|
||||
|
||||
// Virtual nodes without a matching input should be discarded.
|
||||
return
|
||||
}
|
||||
|
||||
return {
|
||||
node: this,
|
||||
origin_id: this.id,
|
||||
origin_slot: slot
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds the index of the input slot on this node that matches the given output {@link slot} index.
|
||||
* Used when bypassing nodes.
|
||||
* @param slot The output slot index on this node
|
||||
* @param type The type of the final target input (so type list matches are accurate)
|
||||
* @returns The index of the input slot on this node, otherwise `-1`.
|
||||
*/
|
||||
#getBypassSlotIndex(slot: number, type: ISlotType) {
|
||||
const { inputs } = this
|
||||
const oppositeInput = inputs[slot]
|
||||
const outputType = this.node.outputs[slot].type
|
||||
|
||||
// Any type short circuit - match slot ID, fallback to first slot
|
||||
if (type === '*' || type === '') {
|
||||
return inputs.length > slot ? slot : 0
|
||||
}
|
||||
|
||||
// Prefer input with the same slot ID
|
||||
if (
|
||||
oppositeInput &&
|
||||
LiteGraph.isValidConnection(oppositeInput.type, outputType) &&
|
||||
LiteGraph.isValidConnection(oppositeInput.type, type)
|
||||
) {
|
||||
return slot
|
||||
}
|
||||
|
||||
// Preserve legacy behaviour; use exact match first.
|
||||
const exactMatch = inputs.findIndex((input) => input.type === type)
|
||||
if (exactMatch !== -1) return exactMatch
|
||||
|
||||
// Find first matching slot - prefer exact type
|
||||
return inputs.findIndex(
|
||||
(input) =>
|
||||
LiteGraph.isValidConnection(input.type, outputType) &&
|
||||
LiteGraph.isValidConnection(input.type, type)
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolves the link inside a subgraph node, from the subgraph IO node to the node inside the subgraph.
|
||||
* @param slot The slot index of the output on the subgraph node.
|
||||
* @param visited A set of unique IDs to guard against infinite recursion. See {@link resolveInput}.
|
||||
* @returns A DTO for the node, and the origin ID / slot index of the output.
|
||||
*/
|
||||
#resolveSubgraphOutput(
|
||||
slot: number,
|
||||
type: ISlotType,
|
||||
visited: Set<string>
|
||||
): ResolvedInput | undefined {
|
||||
const { node } = this
|
||||
const output = node.outputs.at(slot)
|
||||
|
||||
if (!output)
|
||||
throw new SlotIndexError(
|
||||
`No output found for flattened id [${this.id}] slot [${slot}]`
|
||||
)
|
||||
if (!node.isSubgraphNode())
|
||||
throw new TypeError(`Node is not a subgraph node: ${node.id}`)
|
||||
|
||||
// Link inside the subgraph
|
||||
const innerResolved = node.resolveSubgraphOutputLink(slot)
|
||||
if (!innerResolved) return
|
||||
|
||||
const innerNode = innerResolved.outputNode
|
||||
if (!innerNode)
|
||||
throw new Error(
|
||||
`No output node found for id [${this.id}] slot [${slot}] ${output.name}`
|
||||
)
|
||||
|
||||
// Recurse into the subgraph
|
||||
const innerNodeExecutionId = [
|
||||
...this.subgraphNodePath,
|
||||
node.id,
|
||||
innerNode.id
|
||||
].join(':')
|
||||
const innerNodeDto = this.nodesByExecutionId.get(innerNodeExecutionId)
|
||||
if (!innerNodeDto)
|
||||
throw new Error(
|
||||
`No inner node DTO found for id [${innerNodeExecutionId}]`
|
||||
)
|
||||
|
||||
return innerNodeDto.resolveOutput(
|
||||
innerResolved.link.origin_slot,
|
||||
type,
|
||||
visited
|
||||
)
|
||||
}
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
// Re-export Subgraph and GraphOrSubgraph from LGraph.ts to maintain compatibility
|
||||
// This is a temporary fix to resolve circular dependency issues
|
||||
export { Subgraph, type GraphOrSubgraph } from '@/lib/litegraph/src/LGraph'
|
||||
+645
@@ -0,0 +1,645 @@
|
||||
import type { BaseLGraph, LGraph } from '@/lib/litegraph/src/LGraph'
|
||||
import type { LGraphButton } from '@/lib/litegraph/src/LGraphButton'
|
||||
import type { LGraphCanvas } from '@/lib/litegraph/src/LGraphCanvas'
|
||||
import { LGraphNode } from '@/lib/litegraph/src/LGraphNode'
|
||||
import type { DrawTitleBoxOptions } from '@/lib/litegraph/src/LGraphNode'
|
||||
import { LLink } from '@/lib/litegraph/src/LLink'
|
||||
import type { ResolvedConnection } from '@/lib/litegraph/src/LLink'
|
||||
import { RecursionError } from '@/lib/litegraph/src/infrastructure/RecursionError'
|
||||
import type {
|
||||
ISubgraphInput,
|
||||
IWidgetLocator
|
||||
} from '@/lib/litegraph/src/interfaces'
|
||||
import { LiteGraph } from '@/lib/litegraph/src/litegraph'
|
||||
import type {
|
||||
INodeInputSlot,
|
||||
ISlotType,
|
||||
NodeId
|
||||
} from '@/lib/litegraph/src/litegraph'
|
||||
import { NodeInputSlot } from '@/lib/litegraph/src/node/NodeInputSlot'
|
||||
import { NodeOutputSlot } from '@/lib/litegraph/src/node/NodeOutputSlot'
|
||||
import type {
|
||||
GraphOrSubgraph,
|
||||
Subgraph
|
||||
} from '@/lib/litegraph/src/subgraph/Subgraph'
|
||||
import type {
|
||||
ExportedSubgraphInstance,
|
||||
ISerialisedNode
|
||||
} from '@/lib/litegraph/src/types/serialisation'
|
||||
import type { IBaseWidget } from '@/lib/litegraph/src/types/widgets'
|
||||
import type { UUID } from '@/lib/litegraph/src/utils/uuid'
|
||||
import { AssetWidget } from '@/lib/litegraph/src/widgets/AssetWidget'
|
||||
import { toConcreteWidget } from '@/lib/litegraph/src/widgets/widgetMap'
|
||||
|
||||
import { ExecutableNodeDTO } from './ExecutableNodeDTO'
|
||||
import type { ExecutableLGraphNode, ExecutionId } from './ExecutableNodeDTO'
|
||||
import type { SubgraphInput } from './SubgraphInput'
|
||||
|
||||
const workflowSvg = new Image()
|
||||
workflowSvg.src =
|
||||
"data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16' width='16' height='16'%3E%3Csvg xmlns='http://www.w3.org/2000/svg' fill='none' viewBox='0 0 16 16'%3E%3Cpath stroke='white' stroke-linecap='round' stroke-width='1.3' d='M9.18613 3.09999H6.81377M9.18613 12.9H7.55288c-3.08678 0-5.35171-2.99581-4.60305-6.08843l.3054-1.26158M14.7486 2.1721l-.5931 2.45c-.132.54533-.6065.92789-1.1508.92789h-2.2993c-.77173 0-1.33797-.74895-1.1508-1.5221l.5931-2.45c.132-.54533.6065-.9279 1.1508-.9279h2.2993c.7717 0 1.3379.74896 1.1508 1.52211Zm-8.3033 0-.59309 2.45c-.13201.54533-.60646.92789-1.15076.92789H2.4021c-.7717 0-1.33793-.74895-1.15077-1.5221l.59309-2.45c.13201-.54533.60647-.9279 1.15077-.9279h2.29935c.77169 0 1.33792.74896 1.15076 1.52211Zm8.3033 9.8-.5931 2.45c-.132.5453-.6065.9279-1.1508.9279h-2.2993c-.77173 0-1.33797-.749-1.1508-1.5221l.5931-2.45c.132-.5453.6065-.9279 1.1508-.9279h2.2993c.7717 0 1.3379.7489 1.1508 1.5221Z'/%3E%3C/svg%3E %3C/svg%3E"
|
||||
|
||||
/**
|
||||
* An instance of a {@link Subgraph}, displayed as a node on the containing (parent) graph.
|
||||
*/
|
||||
export class SubgraphNode extends LGraphNode implements BaseLGraph {
|
||||
declare inputs: (INodeInputSlot & Partial<ISubgraphInput>)[]
|
||||
|
||||
override readonly type: UUID
|
||||
override readonly isVirtualNode = true as const
|
||||
|
||||
get rootGraph(): LGraph {
|
||||
return this.graph.rootGraph
|
||||
}
|
||||
|
||||
override get displayType(): string {
|
||||
return 'Subgraph node'
|
||||
}
|
||||
|
||||
override isSubgraphNode(): this is SubgraphNode {
|
||||
return true
|
||||
}
|
||||
|
||||
override widgets: IBaseWidget[] = []
|
||||
|
||||
/** Manages lifecycle of all subgraph event listeners */
|
||||
#eventAbortController = new AbortController()
|
||||
|
||||
constructor(
|
||||
/** The (sub)graph that contains this subgraph instance. */
|
||||
override readonly graph: GraphOrSubgraph,
|
||||
/** The definition of this subgraph; how its nodes are configured, etc. */
|
||||
readonly subgraph: Subgraph,
|
||||
instanceData: ExportedSubgraphInstance
|
||||
) {
|
||||
super(subgraph.name, subgraph.id)
|
||||
|
||||
// Update this node when the subgraph input / output slots are changed
|
||||
const subgraphEvents = this.subgraph.events
|
||||
const { signal } = this.#eventAbortController
|
||||
|
||||
subgraphEvents.addEventListener(
|
||||
'input-added',
|
||||
(e) => {
|
||||
const subgraphInput = e.detail.input
|
||||
const { name, type } = subgraphInput
|
||||
const existingInput = this.inputs.find((i) => i.name == name)
|
||||
if (existingInput) {
|
||||
const linkId = subgraphInput.linkIds[0]
|
||||
const { inputNode, input } = subgraph.links[linkId].resolve(subgraph)
|
||||
const widget = inputNode?.widgets?.find?.((w) => w.name == name)
|
||||
if (widget)
|
||||
this.#setWidget(subgraphInput, existingInput, widget, input?.widget)
|
||||
return
|
||||
}
|
||||
const input = this.addInput(name, type)
|
||||
|
||||
this.#addSubgraphInputListeners(subgraphInput, input)
|
||||
},
|
||||
{ signal }
|
||||
)
|
||||
|
||||
subgraphEvents.addEventListener(
|
||||
'removing-input',
|
||||
(e) => {
|
||||
const widget = e.detail.input._widget
|
||||
if (widget) this.ensureWidgetRemoved(widget)
|
||||
|
||||
this.removeInput(e.detail.index)
|
||||
this.setDirtyCanvas(true, true)
|
||||
},
|
||||
{ signal }
|
||||
)
|
||||
|
||||
subgraphEvents.addEventListener(
|
||||
'output-added',
|
||||
(e) => {
|
||||
const { name, type } = e.detail.output
|
||||
this.addOutput(name, type)
|
||||
},
|
||||
{ signal }
|
||||
)
|
||||
|
||||
subgraphEvents.addEventListener(
|
||||
'removing-output',
|
||||
(e) => {
|
||||
this.removeOutput(e.detail.index)
|
||||
this.setDirtyCanvas(true, true)
|
||||
},
|
||||
{ signal }
|
||||
)
|
||||
|
||||
subgraphEvents.addEventListener(
|
||||
'renaming-input',
|
||||
(e) => {
|
||||
const { index, newName } = e.detail
|
||||
const input = this.inputs.at(index)
|
||||
if (!input) throw new Error('Subgraph input not found')
|
||||
|
||||
input.label = newName
|
||||
if (input._widget) {
|
||||
input._widget.label = newName
|
||||
}
|
||||
},
|
||||
{ signal }
|
||||
)
|
||||
|
||||
subgraphEvents.addEventListener(
|
||||
'renaming-output',
|
||||
(e) => {
|
||||
const { index, newName } = e.detail
|
||||
const output = this.outputs.at(index)
|
||||
if (!output) throw new Error('Subgraph output not found')
|
||||
|
||||
output.label = newName
|
||||
},
|
||||
{ signal }
|
||||
)
|
||||
|
||||
this.type = subgraph.id
|
||||
this.configure(instanceData)
|
||||
|
||||
this.addTitleButton({
|
||||
name: 'enter_subgraph',
|
||||
text: '\uE93B', // Unicode for pi-window-maximize
|
||||
yOffset: 0, // No vertical offset needed, button is centered
|
||||
xOffset: -10,
|
||||
fontSize: 16
|
||||
})
|
||||
}
|
||||
|
||||
override onTitleButtonClick(
|
||||
button: LGraphButton,
|
||||
canvas: LGraphCanvas
|
||||
): void {
|
||||
if (button.name === 'enter_subgraph') {
|
||||
canvas.openSubgraph(this.subgraph, this)
|
||||
} else {
|
||||
super.onTitleButtonClick(button, canvas)
|
||||
}
|
||||
}
|
||||
|
||||
#addSubgraphInputListeners(
|
||||
subgraphInput: SubgraphInput,
|
||||
input: INodeInputSlot & Partial<ISubgraphInput>
|
||||
) {
|
||||
if (
|
||||
input._listenerController &&
|
||||
typeof input._listenerController.abort === 'function'
|
||||
) {
|
||||
input._listenerController.abort()
|
||||
}
|
||||
input._listenerController = new AbortController()
|
||||
const { signal } = input._listenerController
|
||||
|
||||
subgraphInput.events.addEventListener(
|
||||
'input-connected',
|
||||
(e) => {
|
||||
if (input._widget) return
|
||||
|
||||
const widget = subgraphInput._widget
|
||||
if (!widget) return
|
||||
|
||||
const widgetLocator = e.detail.input.widget
|
||||
this.#setWidget(subgraphInput, input, widget, widgetLocator)
|
||||
},
|
||||
{ signal }
|
||||
)
|
||||
|
||||
subgraphInput.events.addEventListener(
|
||||
'input-disconnected',
|
||||
() => {
|
||||
// If the input is connected to more than one widget, don't remove the widget
|
||||
const connectedWidgets = subgraphInput.getConnectedWidgets()
|
||||
if (connectedWidgets.length > 0) return
|
||||
|
||||
this.removeWidgetByName(input.name)
|
||||
|
||||
delete input.pos
|
||||
delete input.widget
|
||||
input._widget = undefined
|
||||
},
|
||||
{ signal }
|
||||
)
|
||||
}
|
||||
|
||||
override configure(info: ExportedSubgraphInstance): void {
|
||||
for (const input of this.inputs) {
|
||||
if (
|
||||
input._listenerController &&
|
||||
typeof input._listenerController.abort === 'function'
|
||||
) {
|
||||
input._listenerController.abort()
|
||||
}
|
||||
}
|
||||
|
||||
this.inputs.length = 0
|
||||
this.inputs.push(
|
||||
...this.subgraph.inputNode.slots.map(
|
||||
(slot) =>
|
||||
new NodeInputSlot(
|
||||
{
|
||||
name: slot.name,
|
||||
localized_name: slot.localized_name,
|
||||
label: slot.label,
|
||||
type: slot.type,
|
||||
link: null
|
||||
},
|
||||
this
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
this.outputs.length = 0
|
||||
this.outputs.push(
|
||||
...this.subgraph.outputNode.slots.map(
|
||||
(slot) =>
|
||||
new NodeOutputSlot(
|
||||
{
|
||||
name: slot.name,
|
||||
localized_name: slot.localized_name,
|
||||
label: slot.label,
|
||||
type: slot.type,
|
||||
links: null
|
||||
},
|
||||
this
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
super.configure(info)
|
||||
}
|
||||
|
||||
override _internalConfigureAfterSlots() {
|
||||
// Reset widgets
|
||||
this.widgets.length = 0
|
||||
|
||||
// Check all inputs for connected widgets
|
||||
for (const input of this.inputs) {
|
||||
const subgraphInput = this.subgraph.inputNode.slots.find(
|
||||
(slot) => slot.name === input.name
|
||||
)
|
||||
if (!subgraphInput) {
|
||||
// Skip inputs that don't exist in the subgraph definition
|
||||
// This can happen when loading workflows with dynamically added inputs
|
||||
console.warn(
|
||||
`[SubgraphNode.configure] No subgraph input found for input ${input.name}, skipping`
|
||||
)
|
||||
continue
|
||||
}
|
||||
|
||||
this.#addSubgraphInputListeners(subgraphInput, input)
|
||||
|
||||
// Find the first widget that this slot is connected to
|
||||
for (const linkId of subgraphInput.linkIds) {
|
||||
const link = this.subgraph.getLink(linkId)
|
||||
if (!link) {
|
||||
console.warn(
|
||||
`[SubgraphNode.configure] No link found for link ID ${linkId}`,
|
||||
this
|
||||
)
|
||||
continue
|
||||
}
|
||||
|
||||
const { inputNode } = link.resolve(this.subgraph)
|
||||
if (!inputNode) {
|
||||
console.warn('Failed to resolve inputNode', link, this)
|
||||
continue
|
||||
}
|
||||
|
||||
//Manually find input since target_slot can't be trusted
|
||||
const targetInput = inputNode.inputs.find((inp) => inp.link === linkId)
|
||||
if (!targetInput) {
|
||||
console.warn('Failed to find corresponding input', link, inputNode)
|
||||
continue
|
||||
}
|
||||
|
||||
// No widget - ignore this link
|
||||
const widget = inputNode.getWidgetFromSlot(targetInput)
|
||||
if (!widget) continue
|
||||
|
||||
this.#setWidget(subgraphInput, input, widget, targetInput.widget)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#setWidget(
|
||||
subgraphInput: Readonly<SubgraphInput>,
|
||||
input: INodeInputSlot,
|
||||
widget: Readonly<IBaseWidget>,
|
||||
inputWidget: IWidgetLocator | undefined
|
||||
) {
|
||||
// Use the first matching widget
|
||||
const promotedWidget = toConcreteWidget(widget, this).createCopyForNode(
|
||||
this
|
||||
)
|
||||
if (widget instanceof AssetWidget)
|
||||
promotedWidget.options.nodeType ??= widget.node.type
|
||||
|
||||
Object.assign(promotedWidget, {
|
||||
get name() {
|
||||
return subgraphInput.name
|
||||
},
|
||||
set name(value) {
|
||||
console.warn(
|
||||
'Promoted widget: setting name is not allowed',
|
||||
this,
|
||||
value
|
||||
)
|
||||
},
|
||||
get localized_name() {
|
||||
return subgraphInput.localized_name
|
||||
},
|
||||
set localized_name(value) {
|
||||
console.warn(
|
||||
'Promoted widget: setting localized_name is not allowed',
|
||||
this,
|
||||
value
|
||||
)
|
||||
},
|
||||
get label() {
|
||||
return subgraphInput.label
|
||||
},
|
||||
set label(value) {
|
||||
console.warn(
|
||||
'Promoted widget: setting label is not allowed',
|
||||
this,
|
||||
value
|
||||
)
|
||||
},
|
||||
get tooltip() {
|
||||
// Preserve the original widget's tooltip for promoted widgets
|
||||
return widget.tooltip
|
||||
},
|
||||
set tooltip(value) {
|
||||
console.warn(
|
||||
'Promoted widget: setting tooltip is not allowed',
|
||||
this,
|
||||
value
|
||||
)
|
||||
}
|
||||
})
|
||||
|
||||
const widgetCount = this.inputs.filter((i) => i.widget).length
|
||||
this.widgets.splice(widgetCount, 0, promotedWidget)
|
||||
|
||||
// Dispatch widget-promoted event
|
||||
this.subgraph.events.dispatch('widget-promoted', {
|
||||
widget: promotedWidget,
|
||||
subgraphNode: this
|
||||
})
|
||||
|
||||
// NOTE: This code creates linked chains of prototypes for passing across
|
||||
// multiple levels of subgraphs. As part of this, it intentionally avoids
|
||||
// creating new objects. Have care when making changes.
|
||||
input.widget ??= { name: subgraphInput.name }
|
||||
input.widget.name = subgraphInput.name
|
||||
if (inputWidget) Object.setPrototypeOf(input.widget, inputWidget)
|
||||
|
||||
input._widget = promotedWidget
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensures the subgraph slot is in the params before adding the input as normal.
|
||||
* @param name The name of the input slot.
|
||||
* @param type The type of the input slot.
|
||||
* @param inputProperties Properties that are directly assigned to the created input. Default: a new, empty object.
|
||||
* @returns The new input slot.
|
||||
* @remarks Assertion is required to instantiate empty generic POJO.
|
||||
*/
|
||||
override addInput<TInput extends Partial<ISubgraphInput>>(
|
||||
name: string,
|
||||
type: ISlotType,
|
||||
inputProperties: TInput = {} as TInput
|
||||
): INodeInputSlot & TInput {
|
||||
// Bypasses type narrowing on this.inputs
|
||||
return super.addInput(name, type, inputProperties)
|
||||
}
|
||||
|
||||
override getInputLink(slot: number): LLink | null {
|
||||
// Output side: the link from inside the subgraph
|
||||
const innerLink = this.subgraph.outputNode.slots[slot].getLinks().at(0)
|
||||
if (!innerLink) {
|
||||
console.warn(
|
||||
`SubgraphNode.getInputLink: no inner link found for slot ${slot}`
|
||||
)
|
||||
return null
|
||||
}
|
||||
|
||||
const newLink = LLink.create(innerLink)
|
||||
newLink.origin_id = `${this.id}:${innerLink.origin_id}`
|
||||
newLink.origin_slot = innerLink.origin_slot
|
||||
|
||||
return newLink
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds the internal links connected to the given input slot inside the subgraph, and resolves the nodes / slots.
|
||||
* @param slot The slot index
|
||||
* @returns The resolved connections, or undefined if no input node is found.
|
||||
* @remarks This is used to resolve the input links when dragging a link from a subgraph input slot.
|
||||
*/
|
||||
resolveSubgraphInputLinks(slot: number): ResolvedConnection[] {
|
||||
const inputSlot = this.subgraph.inputNode.slots[slot]
|
||||
const innerLinks = inputSlot.getLinks()
|
||||
if (innerLinks.length === 0) {
|
||||
console.warn(
|
||||
`[SubgraphNode.resolveSubgraphInputLinks] No inner links found for input slot [${slot}] ${inputSlot.name}`,
|
||||
this
|
||||
)
|
||||
return []
|
||||
}
|
||||
return innerLinks.map((link) => link.resolve(this.subgraph))
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds the internal link connected to the given output slot inside the subgraph, and resolves the nodes / slots.
|
||||
* @param slot The slot index
|
||||
* @returns The output node if found, otherwise undefined.
|
||||
*/
|
||||
resolveSubgraphOutputLink(slot: number): ResolvedConnection | undefined {
|
||||
const outputSlot = this.subgraph.outputNode.slots[slot]
|
||||
const innerLink = outputSlot.getLinks().at(0)
|
||||
if (innerLink) {
|
||||
return innerLink.resolve(this.subgraph)
|
||||
}
|
||||
console.warn(
|
||||
`[SubgraphNode.resolveSubgraphOutputLink] No inner link found for output slot [${slot}] ${outputSlot.name}`,
|
||||
this
|
||||
)
|
||||
}
|
||||
|
||||
/** @internal Used to flatten the subgraph before execution. */
|
||||
override getInnerNodes(
|
||||
/** The set of computed node DTOs for this execution. */
|
||||
executableNodes: Map<ExecutionId, ExecutableLGraphNode>,
|
||||
/** The path of subgraph node IDs. */
|
||||
subgraphNodePath: readonly NodeId[] = [],
|
||||
/** Internal recursion param. The list of nodes to add to. */
|
||||
nodes: ExecutableLGraphNode[] = [],
|
||||
/** Internal recursion param. The set of visited nodes. */
|
||||
visited = new Set<SubgraphNode>()
|
||||
): ExecutableLGraphNode[] {
|
||||
if (visited.has(this)) {
|
||||
const nodeInfo = `${this.id}${this.title ? ` (${this.title})` : ''}`
|
||||
const subgraphInfo = `'${this.subgraph.name || 'Unnamed Subgraph'}'`
|
||||
const depth = subgraphNodePath.length
|
||||
throw new RecursionError(
|
||||
`Circular reference detected at depth ${depth} in node ${nodeInfo} of subgraph ${subgraphInfo}. ` +
|
||||
`This creates an infinite loop in the subgraph hierarchy.`
|
||||
)
|
||||
}
|
||||
visited.add(this)
|
||||
|
||||
const subgraphInstanceIdPath = [...subgraphNodePath, this.id]
|
||||
|
||||
// Store the subgraph node DTO
|
||||
const parentSubgraphNode = this.graph.rootGraph
|
||||
.resolveSubgraphIdPath(subgraphNodePath)
|
||||
.at(-1)
|
||||
const subgraphNodeDto = new ExecutableNodeDTO(
|
||||
this,
|
||||
subgraphNodePath,
|
||||
executableNodes,
|
||||
parentSubgraphNode
|
||||
)
|
||||
executableNodes.set(subgraphNodeDto.id, subgraphNodeDto)
|
||||
|
||||
for (const node of this.subgraph.nodes) {
|
||||
if ('getInnerNodes' in node && node.getInnerNodes) {
|
||||
node.getInnerNodes(
|
||||
executableNodes,
|
||||
subgraphInstanceIdPath,
|
||||
nodes,
|
||||
new Set(visited)
|
||||
)
|
||||
} else {
|
||||
// Create minimal DTOs rather than cloning the node
|
||||
const aVeryRealNode = new ExecutableNodeDTO(
|
||||
node,
|
||||
subgraphInstanceIdPath,
|
||||
executableNodes,
|
||||
this
|
||||
)
|
||||
executableNodes.set(aVeryRealNode.id, aVeryRealNode)
|
||||
nodes.push(aVeryRealNode)
|
||||
}
|
||||
}
|
||||
return nodes
|
||||
}
|
||||
|
||||
override removeWidgetByName(name: string): void {
|
||||
const widget = this.widgets.find((w) => w.name === name)
|
||||
if (widget) {
|
||||
this.subgraph.events.dispatch('widget-demoted', {
|
||||
widget,
|
||||
subgraphNode: this
|
||||
})
|
||||
}
|
||||
super.removeWidgetByName(name)
|
||||
}
|
||||
|
||||
override ensureWidgetRemoved(widget: IBaseWidget): void {
|
||||
if (this.widgets.includes(widget)) {
|
||||
this.subgraph.events.dispatch('widget-demoted', {
|
||||
widget,
|
||||
subgraphNode: this
|
||||
})
|
||||
}
|
||||
super.ensureWidgetRemoved(widget)
|
||||
}
|
||||
|
||||
override onRemoved(): void {
|
||||
// Clean up all subgraph event listeners
|
||||
this.#eventAbortController.abort()
|
||||
|
||||
// Clean up all promoted widgets
|
||||
for (const widget of this.widgets) {
|
||||
if ('isProxyWidget' in widget && widget.isProxyWidget) continue
|
||||
this.subgraph.events.dispatch('widget-demoted', {
|
||||
widget,
|
||||
subgraphNode: this
|
||||
})
|
||||
}
|
||||
|
||||
for (const input of this.inputs) {
|
||||
if (
|
||||
input._listenerController &&
|
||||
typeof input._listenerController.abort === 'function'
|
||||
) {
|
||||
input._listenerController.abort()
|
||||
}
|
||||
}
|
||||
}
|
||||
override drawTitleBox(
|
||||
ctx: CanvasRenderingContext2D,
|
||||
{
|
||||
scale,
|
||||
low_quality = false,
|
||||
title_height = LiteGraph.NODE_TITLE_HEIGHT,
|
||||
box_size = 10
|
||||
}: DrawTitleBoxOptions
|
||||
): void {
|
||||
if (this.onDrawTitleBox) {
|
||||
this.onDrawTitleBox(ctx, title_height, this.renderingSize, scale)
|
||||
return
|
||||
}
|
||||
ctx.save()
|
||||
ctx.fillStyle = '#3b82f6'
|
||||
ctx.beginPath()
|
||||
ctx.roundRect(6, -24.5, 22, 20, 5)
|
||||
ctx.fill()
|
||||
if (!low_quality) {
|
||||
ctx.translate(25, 23)
|
||||
ctx.scale(-1.5, 1.5)
|
||||
ctx.drawImage(workflowSvg, 0, -title_height, box_size, box_size)
|
||||
}
|
||||
ctx.restore()
|
||||
}
|
||||
|
||||
/**
|
||||
* Synchronizes widget values from this SubgraphNode instance to the
|
||||
* corresponding widgets in the subgraph definition before serialization.
|
||||
* This ensures nested subgraph widget values are preserved when saving.
|
||||
*/
|
||||
override serialize(): ISerialisedNode {
|
||||
// Sync widget values to subgraph definition before serialization
|
||||
for (let i = 0; i < this.widgets.length; i++) {
|
||||
const widget = this.widgets[i]
|
||||
const input = this.inputs.find((inp) => inp.name === widget.name)
|
||||
|
||||
if (input) {
|
||||
const subgraphInput = this.subgraph.inputNode.slots.find(
|
||||
(slot) => slot.name === input.name
|
||||
)
|
||||
|
||||
if (subgraphInput) {
|
||||
// Find all widgets connected to this subgraph input
|
||||
const connectedWidgets = subgraphInput.getConnectedWidgets()
|
||||
|
||||
// Update the value of all connected widgets
|
||||
for (const connectedWidget of connectedWidgets) {
|
||||
connectedWidget.value = widget.value
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Call parent serialize method
|
||||
return super.serialize()
|
||||
}
|
||||
override clone() {
|
||||
const clone = super.clone()
|
||||
// force reasign so domWidgets reset ownership
|
||||
|
||||
this.properties.proxyWidgets = this.properties.proxyWidgets
|
||||
|
||||
//TODO: Consider deep cloning subgraphs here.
|
||||
//It's the safest place to prevent creation of linked subgraphs
|
||||
//But the frequency of clone().serialize() calls is likely to result in
|
||||
//pollution of rootGraph.subgraphs
|
||||
|
||||
return clone
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,150 @@
|
||||
import type {
|
||||
ExecutableLGraphNode,
|
||||
ExecutionId,
|
||||
LGraph
|
||||
} from '@/lib/litegraph/src/litegraph'
|
||||
import {
|
||||
ExecutableNodeDTO,
|
||||
LGraphEventMode
|
||||
} from '@/lib/litegraph/src/litegraph'
|
||||
import type {
|
||||
ComfyApiWorkflow,
|
||||
ComfyWorkflowJSON
|
||||
} from '@/platform/workflow/validation/schemas/workflowSchema'
|
||||
|
||||
import { ExecutableGroupNodeDTO, isGroupNode } from './executableGroupNodeDto'
|
||||
import { compressWidgetInputSlots } from './litegraphUtil'
|
||||
|
||||
/**
|
||||
* Converts the current graph workflow for sending to the API.
|
||||
* @note Node widgets are updated before serialization to prepare queueing.
|
||||
*
|
||||
* @param graph The graph to convert.
|
||||
* @param options The options for the conversion.
|
||||
* - `sortNodes`: Whether to sort the nodes by execution order.
|
||||
* @returns The workflow and node links
|
||||
*/
|
||||
export const graphToPrompt = async (
|
||||
graph: LGraph,
|
||||
options: { sortNodes?: boolean } = {}
|
||||
): Promise<{ workflow: ComfyWorkflowJSON; output: ComfyApiWorkflow }> => {
|
||||
const { sortNodes = false } = options
|
||||
|
||||
for (const node of graph.computeExecutionOrder(false)) {
|
||||
const innerNodes = node.getInnerNodes
|
||||
? node.getInnerNodes(new Map())
|
||||
: [node]
|
||||
for (const innerNode of innerNodes) {
|
||||
if (innerNode.isVirtualNode) {
|
||||
innerNode.applyToGraph?.()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const workflow = graph.serialize({ sortNodes })
|
||||
|
||||
// Remove localized_name from the workflow
|
||||
for (const node of workflow.nodes) {
|
||||
for (const slot of node.inputs ?? []) {
|
||||
delete slot.localized_name
|
||||
}
|
||||
for (const slot of node.outputs ?? []) {
|
||||
delete slot.localized_name
|
||||
}
|
||||
}
|
||||
|
||||
compressWidgetInputSlots(workflow)
|
||||
workflow.extra ??= {}
|
||||
workflow.extra.frontendVersion = __COMFYUI_FRONTEND_VERSION__
|
||||
|
||||
const nodeDtoMap = new Map<ExecutionId, ExecutableLGraphNode>()
|
||||
for (const node of graph.computeExecutionOrder(false)) {
|
||||
const dto: ExecutableLGraphNode = isGroupNode(node)
|
||||
? new ExecutableGroupNodeDTO(node, [], nodeDtoMap)
|
||||
: new ExecutableNodeDTO(node, [], nodeDtoMap)
|
||||
|
||||
for (const innerNode of dto.getInnerNodes()) {
|
||||
nodeDtoMap.set(innerNode.id, innerNode)
|
||||
}
|
||||
|
||||
nodeDtoMap.set(dto.id, dto)
|
||||
}
|
||||
|
||||
const output: ComfyApiWorkflow = {}
|
||||
// Process nodes in order of execution
|
||||
for (const node of nodeDtoMap.values()) {
|
||||
// Don't serialize muted nodes
|
||||
if (
|
||||
node.isVirtualNode ||
|
||||
node.mode === LGraphEventMode.NEVER ||
|
||||
node.mode === LGraphEventMode.BYPASS
|
||||
) {
|
||||
continue
|
||||
}
|
||||
|
||||
const inputs: ComfyApiWorkflow[string]['inputs'] = {}
|
||||
const { widgets } = node
|
||||
|
||||
// Store all widget values
|
||||
if (widgets) {
|
||||
for (const [i, widget] of widgets.entries()) {
|
||||
if (!widget.name || widget.options?.serialize === false) continue
|
||||
|
||||
const widgetValue = widget.serializeValue
|
||||
? await widget.serializeValue(node, i)
|
||||
: widget.value
|
||||
// By default, Array values are reserved to represent node connections.
|
||||
// We need to wrap the array as an object to avoid the misinterpretation
|
||||
// of the array as a node connection.
|
||||
// The backend automatically unwraps the object to an array during
|
||||
// execution.
|
||||
inputs[widget.name] = Array.isArray(widgetValue)
|
||||
? {
|
||||
__value__: widgetValue
|
||||
}
|
||||
: widgetValue
|
||||
}
|
||||
}
|
||||
|
||||
// Store all node links
|
||||
for (const [i, input] of node.inputs.entries()) {
|
||||
const resolvedInput = node.resolveInput(i)
|
||||
if (!resolvedInput) continue
|
||||
|
||||
// Resolved to an actual widget value rather than a node connection
|
||||
if (resolvedInput.widgetInfo) {
|
||||
const { value } = resolvedInput.widgetInfo
|
||||
inputs[input.name] = Array.isArray(value) ? { __value__: value } : value
|
||||
continue
|
||||
}
|
||||
|
||||
inputs[input.name] = [
|
||||
String(resolvedInput.origin_id),
|
||||
// @ts-expect-error link.origin_slot is already number.
|
||||
parseInt(resolvedInput.origin_slot)
|
||||
]
|
||||
}
|
||||
|
||||
output[String(node.id)] = {
|
||||
inputs,
|
||||
// TODO(huchenlei): Filter out all nodes that cannot be mapped to a
|
||||
// comfyClass.
|
||||
class_type: node.comfyClass!,
|
||||
// Ignored by the backend.
|
||||
_meta: {
|
||||
title: node.title
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Remove inputs connected to removed nodes
|
||||
for (const { inputs } of Object.values(output)) {
|
||||
for (const [i, input] of Object.entries(inputs)) {
|
||||
if (Array.isArray(input) && input.length === 2 && !output[input[0]]) {
|
||||
delete inputs[i]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return { workflow: workflow as ComfyWorkflowJSON, output }
|
||||
}
|
||||
@@ -0,0 +1,674 @@
|
||||
GNU GENERAL PUBLIC LICENSE
|
||||
Version 3, 29 June 2007
|
||||
|
||||
Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
|
||||
Everyone is permitted to copy and distribute verbatim copies
|
||||
of this license document, but changing it is not allowed.
|
||||
|
||||
Preamble
|
||||
|
||||
The GNU General Public License is a free, copyleft license for
|
||||
software and other kinds of works.
|
||||
|
||||
The licenses for most software and other practical works are designed
|
||||
to take away your freedom to share and change the works. By contrast,
|
||||
the GNU General Public License is intended to guarantee your freedom to
|
||||
share and change all versions of a program--to make sure it remains free
|
||||
software for all its users. We, the Free Software Foundation, use the
|
||||
GNU General Public License for most of our software; it applies also to
|
||||
any other work released this way by its authors. You can apply it to
|
||||
your programs, too.
|
||||
|
||||
When we speak of free software, we are referring to freedom, not
|
||||
price. Our General Public Licenses are designed to make sure that you
|
||||
have the freedom to distribute copies of free software (and charge for
|
||||
them if you wish), that you receive source code or can get it if you
|
||||
want it, that you can change the software or use pieces of it in new
|
||||
free programs, and that you know you can do these things.
|
||||
|
||||
To protect your rights, we need to prevent others from denying you
|
||||
these rights or asking you to surrender the rights. Therefore, you have
|
||||
certain responsibilities if you distribute copies of the software, or if
|
||||
you modify it: responsibilities to respect the freedom of others.
|
||||
|
||||
For example, if you distribute copies of such a program, whether
|
||||
gratis or for a fee, you must pass on to the recipients the same
|
||||
freedoms that you received. You must make sure that they, too, receive
|
||||
or can get the source code. And you must show them these terms so they
|
||||
know their rights.
|
||||
|
||||
Developers that use the GNU GPL protect your rights with two steps:
|
||||
(1) assert copyright on the software, and (2) offer you this License
|
||||
giving you legal permission to copy, distribute and/or modify it.
|
||||
|
||||
For the developers' and authors' protection, the GPL clearly explains
|
||||
that there is no warranty for this free software. For both users' and
|
||||
authors' sake, the GPL requires that modified versions be marked as
|
||||
changed, so that their problems will not be attributed erroneously to
|
||||
authors of previous versions.
|
||||
|
||||
Some devices are designed to deny users access to install or run
|
||||
modified versions of the software inside them, although the manufacturer
|
||||
can do so. This is fundamentally incompatible with the aim of
|
||||
protecting users' freedom to change the software. The systematic
|
||||
pattern of such abuse occurs in the area of products for individuals to
|
||||
use, which is precisely where it is most unacceptable. Therefore, we
|
||||
have designed this version of the GPL to prohibit the practice for those
|
||||
products. If such problems arise substantially in other domains, we
|
||||
stand ready to extend this provision to those domains in future versions
|
||||
of the GPL, as needed to protect the freedom of users.
|
||||
|
||||
Finally, every program is threatened constantly by software patents.
|
||||
States should not allow patents to restrict development and use of
|
||||
software on general-purpose computers, but in those that do, we wish to
|
||||
avoid the special danger that patents applied to a free program could
|
||||
make it effectively proprietary. To prevent this, the GPL assures that
|
||||
patents cannot be used to render the program non-free.
|
||||
|
||||
The precise terms and conditions for copying, distribution and
|
||||
modification follow.
|
||||
|
||||
TERMS AND CONDITIONS
|
||||
|
||||
0. Definitions.
|
||||
|
||||
"This License" refers to version 3 of the GNU General Public License.
|
||||
|
||||
"Copyright" also means copyright-like laws that apply to other kinds of
|
||||
works, such as semiconductor masks.
|
||||
|
||||
"The Program" refers to any copyrightable work licensed under this
|
||||
License. Each licensee is addressed as "you". "Licensees" and
|
||||
"recipients" may be individuals or organizations.
|
||||
|
||||
To "modify" a work means to copy from or adapt all or part of the work
|
||||
in a fashion requiring copyright permission, other than the making of an
|
||||
exact copy. The resulting work is called a "modified version" of the
|
||||
earlier work or a work "based on" the earlier work.
|
||||
|
||||
A "covered work" means either the unmodified Program or a work based
|
||||
on the Program.
|
||||
|
||||
To "propagate" a work means to do anything with it that, without
|
||||
permission, would make you directly or secondarily liable for
|
||||
infringement under applicable copyright law, except executing it on a
|
||||
computer or modifying a private copy. Propagation includes copying,
|
||||
distribution (with or without modification), making available to the
|
||||
public, and in some countries other activities as well.
|
||||
|
||||
To "convey" a work means any kind of propagation that enables other
|
||||
parties to make or receive copies. Mere interaction with a user through
|
||||
a computer network, with no transfer of a copy, is not conveying.
|
||||
|
||||
An interactive user interface displays "Appropriate Legal Notices"
|
||||
to the extent that it includes a convenient and prominently visible
|
||||
feature that (1) displays an appropriate copyright notice, and (2)
|
||||
tells the user that there is no warranty for the work (except to the
|
||||
extent that warranties are provided), that licensees may convey the
|
||||
work under this License, and how to view a copy of this License. If
|
||||
the interface presents a list of user commands or options, such as a
|
||||
menu, a prominent item in the list meets this criterion.
|
||||
|
||||
1. Source Code.
|
||||
|
||||
The "source code" for a work means the preferred form of the work
|
||||
for making modifications to it. "Object code" means any non-source
|
||||
form of a work.
|
||||
|
||||
A "Standard Interface" means an interface that either is an official
|
||||
standard defined by a recognized standards body, or, in the case of
|
||||
interfaces specified for a particular programming language, one that
|
||||
is widely used among developers working in that language.
|
||||
|
||||
The "System Libraries" of an executable work include anything, other
|
||||
than the work as a whole, that (a) is included in the normal form of
|
||||
packaging a Major Component, but which is not part of that Major
|
||||
Component, and (b) serves only to enable use of the work with that
|
||||
Major Component, or to implement a Standard Interface for which an
|
||||
implementation is available to the public in source code form. A
|
||||
"Major Component", in this context, means a major essential component
|
||||
(kernel, window system, and so on) of the specific operating system
|
||||
(if any) on which the executable work runs, or a compiler used to
|
||||
produce the work, or an object code interpreter used to run it.
|
||||
|
||||
The "Corresponding Source" for a work in object code form means all
|
||||
the source code needed to generate, install, and (for an executable
|
||||
work) run the object code and to modify the work, including scripts to
|
||||
control those activities. However, it does not include the work's
|
||||
System Libraries, or general-purpose tools or generally available free
|
||||
programs which are used unmodified in performing those activities but
|
||||
which are not part of the work. For example, Corresponding Source
|
||||
includes interface definition files associated with source files for
|
||||
the work, and the source code for shared libraries and dynamically
|
||||
linked subprograms that the work is specifically designed to require,
|
||||
such as by intimate data communication or control flow between those
|
||||
subprograms and other parts of the work.
|
||||
|
||||
The Corresponding Source need not include anything that users
|
||||
can regenerate automatically from other parts of the Corresponding
|
||||
Source.
|
||||
|
||||
The Corresponding Source for a work in source code form is that
|
||||
same work.
|
||||
|
||||
2. Basic Permissions.
|
||||
|
||||
All rights granted under this License are granted for the term of
|
||||
copyright on the Program, and are irrevocable provided the stated
|
||||
conditions are met. This License explicitly affirms your unlimited
|
||||
permission to run the unmodified Program. The output from running a
|
||||
covered work is covered by this License only if the output, given its
|
||||
content, constitutes a covered work. This License acknowledges your
|
||||
rights of fair use or other equivalent, as provided by copyright law.
|
||||
|
||||
You may make, run and propagate covered works that you do not
|
||||
convey, without conditions so long as your license otherwise remains
|
||||
in force. You may convey covered works to others for the sole purpose
|
||||
of having them make modifications exclusively for you, or provide you
|
||||
with facilities for running those works, provided that you comply with
|
||||
the terms of this License in conveying all material for which you do
|
||||
not control copyright. Those thus making or running the covered works
|
||||
for you must do so exclusively on your behalf, under your direction
|
||||
and control, on terms that prohibit them from making any copies of
|
||||
your copyrighted material outside their relationship with you.
|
||||
|
||||
Conveying under any other circumstances is permitted solely under
|
||||
the conditions stated below. Sublicensing is not allowed; section 10
|
||||
makes it unnecessary.
|
||||
|
||||
3. Protecting Users' Legal Rights From Anti-Circumvention Law.
|
||||
|
||||
No covered work shall be deemed part of an effective technological
|
||||
measure under any applicable law fulfilling obligations under article
|
||||
11 of the WIPO copyright treaty adopted on 20 December 1996, or
|
||||
similar laws prohibiting or restricting circumvention of such
|
||||
measures.
|
||||
|
||||
When you convey a covered work, you waive any legal power to forbid
|
||||
circumvention of technological measures to the extent such circumvention
|
||||
is effected by exercising rights under this License with respect to
|
||||
the covered work, and you disclaim any intention to limit operation or
|
||||
modification of the work as a means of enforcing, against the work's
|
||||
users, your or third parties' legal rights to forbid circumvention of
|
||||
technological measures.
|
||||
|
||||
4. Conveying Verbatim Copies.
|
||||
|
||||
You may convey verbatim copies of the Program's source code as you
|
||||
receive it, in any medium, provided that you conspicuously and
|
||||
appropriately publish on each copy an appropriate copyright notice;
|
||||
keep intact all notices stating that this License and any
|
||||
non-permissive terms added in accord with section 7 apply to the code;
|
||||
keep intact all notices of the absence of any warranty; and give all
|
||||
recipients a copy of this License along with the Program.
|
||||
|
||||
You may charge any price or no price for each copy that you convey,
|
||||
and you may offer support or warranty protection for a fee.
|
||||
|
||||
5. Conveying Modified Source Versions.
|
||||
|
||||
You may convey a work based on the Program, or the modifications to
|
||||
produce it from the Program, in the form of source code under the
|
||||
terms of section 4, provided that you also meet all of these conditions:
|
||||
|
||||
a) The work must carry prominent notices stating that you modified
|
||||
it, and giving a relevant date.
|
||||
|
||||
b) The work must carry prominent notices stating that it is
|
||||
released under this License and any conditions added under section
|
||||
7. This requirement modifies the requirement in section 4 to
|
||||
"keep intact all notices".
|
||||
|
||||
c) You must license the entire work, as a whole, under this
|
||||
License to anyone who comes into possession of a copy. This
|
||||
License will therefore apply, along with any applicable section 7
|
||||
additional terms, to the whole of the work, and all its parts,
|
||||
regardless of how they are packaged. This License gives no
|
||||
permission to license the work in any other way, but it does not
|
||||
invalidate such permission if you have separately received it.
|
||||
|
||||
d) If the work has interactive user interfaces, each must display
|
||||
Appropriate Legal Notices; however, if the Program has interactive
|
||||
interfaces that do not display Appropriate Legal Notices, your
|
||||
work need not make them do so.
|
||||
|
||||
A compilation of a covered work with other separate and independent
|
||||
works, which are not by their nature extensions of the covered work,
|
||||
and which are not combined with it such as to form a larger program,
|
||||
in or on a volume of a storage or distribution medium, is called an
|
||||
"aggregate" if the compilation and its resulting copyright are not
|
||||
used to limit the access or legal rights of the compilation's users
|
||||
beyond what the individual works permit. Inclusion of a covered work
|
||||
in an aggregate does not cause this License to apply to the other
|
||||
parts of the aggregate.
|
||||
|
||||
6. Conveying Non-Source Forms.
|
||||
|
||||
You may convey a covered work in object code form under the terms
|
||||
of sections 4 and 5, provided that you also convey the
|
||||
machine-readable Corresponding Source under the terms of this License,
|
||||
in one of these ways:
|
||||
|
||||
a) Convey the object code in, or embodied in, a physical product
|
||||
(including a physical distribution medium), accompanied by the
|
||||
Corresponding Source fixed on a durable physical medium
|
||||
customarily used for software interchange.
|
||||
|
||||
b) Convey the object code in, or embodied in, a physical product
|
||||
(including a physical distribution medium), accompanied by a
|
||||
written offer, valid for at least three years and valid for as
|
||||
long as you offer spare parts or customer support for that product
|
||||
model, to give anyone who possesses the object code either (1) a
|
||||
copy of the Corresponding Source for all the software in the
|
||||
product that is covered by this License, on a durable physical
|
||||
medium customarily used for software interchange, for a price no
|
||||
more than your reasonable cost of physically performing this
|
||||
conveying of source, or (2) access to copy the
|
||||
Corresponding Source from a network server at no charge.
|
||||
|
||||
c) Convey individual copies of the object code with a copy of the
|
||||
written offer to provide the Corresponding Source. This
|
||||
alternative is allowed only occasionally and noncommercially, and
|
||||
only if you received the object code with such an offer, in accord
|
||||
with subsection 6b.
|
||||
|
||||
d) Convey the object code by offering access from a designated
|
||||
place (gratis or for a charge), and offer equivalent access to the
|
||||
Corresponding Source in the same way through the same place at no
|
||||
further charge. You need not require recipients to copy the
|
||||
Corresponding Source along with the object code. If the place to
|
||||
copy the object code is a network server, the Corresponding Source
|
||||
may be on a different server (operated by you or a third party)
|
||||
that supports equivalent copying facilities, provided you maintain
|
||||
clear directions next to the object code saying where to find the
|
||||
Corresponding Source. Regardless of what server hosts the
|
||||
Corresponding Source, you remain obligated to ensure that it is
|
||||
available for as long as needed to satisfy these requirements.
|
||||
|
||||
e) Convey the object code using peer-to-peer transmission, provided
|
||||
you inform other peers where the object code and Corresponding
|
||||
Source of the work are being offered to the general public at no
|
||||
charge under subsection 6d.
|
||||
|
||||
A separable portion of the object code, whose source code is excluded
|
||||
from the Corresponding Source as a System Library, need not be
|
||||
included in conveying the object code work.
|
||||
|
||||
A "User Product" is either (1) a "consumer product", which means any
|
||||
tangible personal property which is normally used for personal, family,
|
||||
or household purposes, or (2) anything designed or sold for incorporation
|
||||
into a dwelling. In determining whether a product is a consumer product,
|
||||
doubtful cases shall be resolved in favor of coverage. For a particular
|
||||
product received by a particular user, "normally used" refers to a
|
||||
typical or common use of that class of product, regardless of the status
|
||||
of the particular user or of the way in which the particular user
|
||||
actually uses, or expects or is expected to use, the product. A product
|
||||
is a consumer product regardless of whether the product has substantial
|
||||
commercial, industrial or non-consumer uses, unless such uses represent
|
||||
the only significant mode of use of the product.
|
||||
|
||||
"Installation Information" for a User Product means any methods,
|
||||
procedures, authorization keys, or other information required to install
|
||||
and execute modified versions of a covered work in that User Product from
|
||||
a modified version of its Corresponding Source. The information must
|
||||
suffice to ensure that the continued functioning of the modified object
|
||||
code is in no case prevented or interfered with solely because
|
||||
modification has been made.
|
||||
|
||||
If you convey an object code work under this section in, or with, or
|
||||
specifically for use in, a User Product, and the conveying occurs as
|
||||
part of a transaction in which the right of possession and use of the
|
||||
User Product is transferred to the recipient in perpetuity or for a
|
||||
fixed term (regardless of how the transaction is characterized), the
|
||||
Corresponding Source conveyed under this section must be accompanied
|
||||
by the Installation Information. But this requirement does not apply
|
||||
if neither you nor any third party retains the ability to install
|
||||
modified object code on the User Product (for example, the work has
|
||||
been installed in ROM).
|
||||
|
||||
The requirement to provide Installation Information does not include a
|
||||
requirement to continue to provide support service, warranty, or updates
|
||||
for a work that has been modified or installed by the recipient, or for
|
||||
the User Product in which it has been modified or installed. Access to a
|
||||
network may be denied when the modification itself materially and
|
||||
adversely affects the operation of the network or violates the rules and
|
||||
protocols for communication across the network.
|
||||
|
||||
Corresponding Source conveyed, and Installation Information provided,
|
||||
in accord with this section must be in a format that is publicly
|
||||
documented (and with an implementation available to the public in
|
||||
source code form), and must require no special password or key for
|
||||
unpacking, reading or copying.
|
||||
|
||||
7. Additional Terms.
|
||||
|
||||
"Additional permissions" are terms that supplement the terms of this
|
||||
License by making exceptions from one or more of its conditions.
|
||||
Additional permissions that are applicable to the entire Program shall
|
||||
be treated as though they were included in this License, to the extent
|
||||
that they are valid under applicable law. If additional permissions
|
||||
apply only to part of the Program, that part may be used separately
|
||||
under those permissions, but the entire Program remains governed by
|
||||
this License without regard to the additional permissions.
|
||||
|
||||
When you convey a copy of a covered work, you may at your option
|
||||
remove any additional permissions from that copy, or from any part of
|
||||
it. (Additional permissions may be written to require their own
|
||||
removal in certain cases when you modify the work.) You may place
|
||||
additional permissions on material, added by you to a covered work,
|
||||
for which you have or can give appropriate copyright permission.
|
||||
|
||||
Notwithstanding any other provision of this License, for material you
|
||||
add to a covered work, you may (if authorized by the copyright holders of
|
||||
that material) supplement the terms of this License with terms:
|
||||
|
||||
a) Disclaiming warranty or limiting liability differently from the
|
||||
terms of sections 15 and 16 of this License; or
|
||||
|
||||
b) Requiring preservation of specified reasonable legal notices or
|
||||
author attributions in that material or in the Appropriate Legal
|
||||
Notices displayed by works containing it; or
|
||||
|
||||
c) Prohibiting misrepresentation of the origin of that material, or
|
||||
requiring that modified versions of such material be marked in
|
||||
reasonable ways as different from the original version; or
|
||||
|
||||
d) Limiting the use for publicity purposes of names of licensors or
|
||||
authors of the material; or
|
||||
|
||||
e) Declining to grant rights under trademark law for use of some
|
||||
trade names, trademarks, or service marks; or
|
||||
|
||||
f) Requiring indemnification of licensors and authors of that
|
||||
material by anyone who conveys the material (or modified versions of
|
||||
it) with contractual assumptions of liability to the recipient, for
|
||||
any liability that these contractual assumptions directly impose on
|
||||
those licensors and authors.
|
||||
|
||||
All other non-permissive additional terms are considered "further
|
||||
restrictions" within the meaning of section 10. If the Program as you
|
||||
received it, or any part of it, contains a notice stating that it is
|
||||
governed by this License along with a term that is a further
|
||||
restriction, you may remove that term. If a license document contains
|
||||
a further restriction but permits relicensing or conveying under this
|
||||
License, you may add to a covered work material governed by the terms
|
||||
of that license document, provided that the further restriction does
|
||||
not survive such relicensing or conveying.
|
||||
|
||||
If you add terms to a covered work in accord with this section, you
|
||||
must place, in the relevant source files, a statement of the
|
||||
additional terms that apply to those files, or a notice indicating
|
||||
where to find the applicable terms.
|
||||
|
||||
Additional terms, permissive or non-permissive, may be stated in the
|
||||
form of a separately written license, or stated as exceptions;
|
||||
the above requirements apply either way.
|
||||
|
||||
8. Termination.
|
||||
|
||||
You may not propagate or modify a covered work except as expressly
|
||||
provided under this License. Any attempt otherwise to propagate or
|
||||
modify it is void, and will automatically terminate your rights under
|
||||
this License (including any patent licenses granted under the third
|
||||
paragraph of section 11).
|
||||
|
||||
However, if you cease all violation of this License, then your
|
||||
license from a particular copyright holder is reinstated (a)
|
||||
provisionally, unless and until the copyright holder explicitly and
|
||||
finally terminates your license, and (b) permanently, if the copyright
|
||||
holder fails to notify you of the violation by some reasonable means
|
||||
prior to 60 days after the cessation.
|
||||
|
||||
Moreover, your license from a particular copyright holder is
|
||||
reinstated permanently if the copyright holder notifies you of the
|
||||
violation by some reasonable means, this is the first time you have
|
||||
received notice of violation of this License (for any work) from that
|
||||
copyright holder, and you cure the violation prior to 30 days after
|
||||
your receipt of the notice.
|
||||
|
||||
Termination of your rights under this section does not terminate the
|
||||
licenses of parties who have received copies or rights from you under
|
||||
this License. If your rights have been terminated and not permanently
|
||||
reinstated, you do not qualify to receive new licenses for the same
|
||||
material under section 10.
|
||||
|
||||
9. Acceptance Not Required for Having Copies.
|
||||
|
||||
You are not required to accept this License in order to receive or
|
||||
run a copy of the Program. Ancillary propagation of a covered work
|
||||
occurring solely as a consequence of using peer-to-peer transmission
|
||||
to receive a copy likewise does not require acceptance. However,
|
||||
nothing other than this License grants you permission to propagate or
|
||||
modify any covered work. These actions infringe copyright if you do
|
||||
not accept this License. Therefore, by modifying or propagating a
|
||||
covered work, you indicate your acceptance of this License to do so.
|
||||
|
||||
10. Automatic Licensing of Downstream Recipients.
|
||||
|
||||
Each time you convey a covered work, the recipient automatically
|
||||
receives a license from the original licensors, to run, modify and
|
||||
propagate that work, subject to this License. You are not responsible
|
||||
for enforcing compliance by third parties with this License.
|
||||
|
||||
An "entity transaction" is a transaction transferring control of an
|
||||
organization, or substantially all assets of one, or subdividing an
|
||||
organization, or merging organizations. If propagation of a covered
|
||||
work results from an entity transaction, each party to that
|
||||
transaction who receives a copy of the work also receives whatever
|
||||
licenses to the work the party's predecessor in interest had or could
|
||||
give under the previous paragraph, plus a right to possession of the
|
||||
Corresponding Source of the work from the predecessor in interest, if
|
||||
the predecessor has it or can get it with reasonable efforts.
|
||||
|
||||
You may not impose any further restrictions on the exercise of the
|
||||
rights granted or affirmed under this License. For example, you may
|
||||
not impose a license fee, royalty, or other charge for exercise of
|
||||
rights granted under this License, and you may not initiate litigation
|
||||
(including a cross-claim or counterclaim in a lawsuit) alleging that
|
||||
any patent claim is infringed by making, using, selling, offering for
|
||||
sale, or importing the Program or any portion of it.
|
||||
|
||||
11. Patents.
|
||||
|
||||
A "contributor" is a copyright holder who authorizes use under this
|
||||
License of the Program or a work on which the Program is based. The
|
||||
work thus licensed is called the contributor's "contributor version".
|
||||
|
||||
A contributor's "essential patent claims" are all patent claims
|
||||
owned or controlled by the contributor, whether already acquired or
|
||||
hereafter acquired, that would be infringed by some manner, permitted
|
||||
by this License, of making, using, or selling its contributor version,
|
||||
but do not include claims that would be infringed only as a
|
||||
consequence of further modification of the contributor version. For
|
||||
purposes of this definition, "control" includes the right to grant
|
||||
patent sublicenses in a manner consistent with the requirements of
|
||||
this License.
|
||||
|
||||
Each contributor grants you a non-exclusive, worldwide, royalty-free
|
||||
patent license under the contributor's essential patent claims, to
|
||||
make, use, sell, offer for sale, import and otherwise run, modify and
|
||||
propagate the contents of its contributor version.
|
||||
|
||||
In the following three paragraphs, a "patent license" is any express
|
||||
agreement or commitment, however denominated, not to enforce a patent
|
||||
(such as an express permission to practice a patent or covenant not to
|
||||
sue for patent infringement). To "grant" such a patent license to a
|
||||
party means to make such an agreement or commitment not to enforce a
|
||||
patent against the party.
|
||||
|
||||
If you convey a covered work, knowingly relying on a patent license,
|
||||
and the Corresponding Source of the work is not available for anyone
|
||||
to copy, free of charge and under the terms of this License, through a
|
||||
publicly available network server or other readily accessible means,
|
||||
then you must either (1) cause the Corresponding Source to be so
|
||||
available, or (2) arrange to deprive yourself of the benefit of the
|
||||
patent license for this particular work, or (3) arrange, in a manner
|
||||
consistent with the requirements of this License, to extend the patent
|
||||
license to downstream recipients. "Knowingly relying" means you have
|
||||
actual knowledge that, but for the patent license, your conveying the
|
||||
covered work in a country, or your recipient's use of the covered work
|
||||
in a country, would infringe one or more identifiable patents in that
|
||||
country that you have reason to believe are valid.
|
||||
|
||||
If, pursuant to or in connection with a single transaction or
|
||||
arrangement, you convey, or propagate by procuring conveyance of, a
|
||||
covered work, and grant a patent license to some of the parties
|
||||
receiving the covered work authorizing them to use, propagate, modify
|
||||
or convey a specific copy of the covered work, then the patent license
|
||||
you grant is automatically extended to all recipients of the covered
|
||||
work and works based on it.
|
||||
|
||||
A patent license is "discriminatory" if it does not include within
|
||||
the scope of its coverage, prohibits the exercise of, or is
|
||||
conditioned on the non-exercise of one or more of the rights that are
|
||||
specifically granted under this License. You may not convey a covered
|
||||
work if you are a party to an arrangement with a third party that is
|
||||
in the business of distributing software, under which you make payment
|
||||
to the third party based on the extent of your activity of conveying
|
||||
the work, and under which the third party grants, to any of the
|
||||
parties who would receive the covered work from you, a discriminatory
|
||||
patent license (a) in connection with copies of the covered work
|
||||
conveyed by you (or copies made from those copies), or (b) primarily
|
||||
for and in connection with specific products or compilations that
|
||||
contain the covered work, unless you entered into that arrangement,
|
||||
or that patent license was granted, prior to 28 March 2007.
|
||||
|
||||
Nothing in this License shall be construed as excluding or limiting
|
||||
any implied license or other defenses to infringement that may
|
||||
otherwise be available to you under applicable patent law.
|
||||
|
||||
12. No Surrender of Others' Freedom.
|
||||
|
||||
If conditions are imposed on you (whether by court order, agreement or
|
||||
otherwise) that contradict the conditions of this License, they do not
|
||||
excuse you from the conditions of this License. If you cannot convey a
|
||||
covered work so as to satisfy simultaneously your obligations under this
|
||||
License and any other pertinent obligations, then as a consequence you may
|
||||
not convey it at all. For example, if you agree to terms that obligate you
|
||||
to collect a royalty for further conveying from those to whom you convey
|
||||
the Program, the only way you could satisfy both those terms and this
|
||||
License would be to refrain entirely from conveying the Program.
|
||||
|
||||
13. Use with the GNU Affero General Public License.
|
||||
|
||||
Notwithstanding any other provision of this License, you have
|
||||
permission to link or combine any covered work with a work licensed
|
||||
under version 3 of the GNU Affero General Public License into a single
|
||||
combined work, and to convey the resulting work. The terms of this
|
||||
License will continue to apply to the part which is the covered work,
|
||||
but the special requirements of the GNU Affero General Public License,
|
||||
section 13, concerning interaction through a network will apply to the
|
||||
combination as such.
|
||||
|
||||
14. Revised Versions of this License.
|
||||
|
||||
The Free Software Foundation may publish revised and/or new versions of
|
||||
the GNU General Public License from time to time. Such new versions will
|
||||
be similar in spirit to the present version, but may differ in detail to
|
||||
address new problems or concerns.
|
||||
|
||||
Each version is given a distinguishing version number. If the
|
||||
Program specifies that a certain numbered version of the GNU General
|
||||
Public License "or any later version" applies to it, you have the
|
||||
option of following the terms and conditions either of that numbered
|
||||
version or of any later version published by the Free Software
|
||||
Foundation. If the Program does not specify a version number of the
|
||||
GNU General Public License, you may choose any version ever published
|
||||
by the Free Software Foundation.
|
||||
|
||||
If the Program specifies that a proxy can decide which future
|
||||
versions of the GNU General Public License can be used, that proxy's
|
||||
public statement of acceptance of a version permanently authorizes you
|
||||
to choose that version for the Program.
|
||||
|
||||
Later license versions may give you additional or different
|
||||
permissions. However, no additional obligations are imposed on any
|
||||
author or copyright holder as a result of your choosing to follow a
|
||||
later version.
|
||||
|
||||
15. Disclaimer of Warranty.
|
||||
|
||||
THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
|
||||
APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
|
||||
HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
|
||||
OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
|
||||
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
|
||||
IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
|
||||
ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
|
||||
|
||||
16. Limitation of Liability.
|
||||
|
||||
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
|
||||
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
|
||||
THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
|
||||
GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
|
||||
USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
|
||||
DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
|
||||
PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
|
||||
EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
|
||||
SUCH DAMAGES.
|
||||
|
||||
17. Interpretation of Sections 15 and 16.
|
||||
|
||||
If the disclaimer of warranty and limitation of liability provided
|
||||
above cannot be given local legal effect according to their terms,
|
||||
reviewing courts shall apply local law that most closely approximates
|
||||
an absolute waiver of all civil liability in connection with the
|
||||
Program, unless a warranty or assumption of liability accompanies a
|
||||
copy of the Program in return for a fee.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
How to Apply These Terms to Your New Programs
|
||||
|
||||
If you develop a new program, and you want it to be of the greatest
|
||||
possible use to the public, the best way to achieve this is to make it
|
||||
free software which everyone can redistribute and change under these terms.
|
||||
|
||||
To do so, attach the following notices to the program. It is safest
|
||||
to attach them to the start of each source file to most effectively
|
||||
state the exclusion of warranty; and each file should have at least
|
||||
the "copyright" line and a pointer to where the full notice is found.
|
||||
|
||||
<one line to give the program's name and a brief idea of what it does.>
|
||||
Copyright (C) <year> <name of author>
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
Also add information on how to contact you by electronic and paper mail.
|
||||
|
||||
If the program does terminal interaction, make it output a short
|
||||
notice like this when it starts in an interactive mode:
|
||||
|
||||
<program> Copyright (C) <year> <name of author>
|
||||
This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
|
||||
This is free software, and you are welcome to redistribute it
|
||||
under certain conditions; type `show c' for details.
|
||||
|
||||
The hypothetical commands `show w' and `show c' should show the appropriate
|
||||
parts of the General Public License. Of course, your program's commands
|
||||
might be different; for a GUI interface, you would use an "about box".
|
||||
|
||||
You should also get your employer (if you work as a programmer) or school,
|
||||
if any, to sign a "copyright disclaimer" for the program, if necessary.
|
||||
For more information on this, and how to apply and follow the GNU GPL, see
|
||||
<https://www.gnu.org/licenses/>.
|
||||
|
||||
The GNU General Public License does not permit incorporating your program
|
||||
into proprietary programs. If your program is a subroutine library, you
|
||||
may consider it more useful to permit linking proprietary applications with
|
||||
the library. If this is what you want to do, use the GNU Lesser General
|
||||
Public License instead of this License. But first, please read
|
||||
<https://www.gnu.org/licenses/why-not-lgpl.html>.
|
||||
@@ -0,0 +1,617 @@
|
||||
import { useChainCallback } from '@/composables/functional/useChainCallback'
|
||||
import { LGraphNode, LiteGraph } from '@/lib/litegraph/src/litegraph'
|
||||
import type {
|
||||
INodeInputSlot,
|
||||
INodeOutputSlot,
|
||||
ISlotType,
|
||||
LLink
|
||||
} from '@/lib/litegraph/src/litegraph'
|
||||
import { NodeSlot } from '@/lib/litegraph/src/node/NodeSlot'
|
||||
import type {
|
||||
IBaseWidget,
|
||||
TWidgetValue
|
||||
} from '@/lib/litegraph/src/types/widgets'
|
||||
import { assetService } from '@/platform/assets/services/assetService'
|
||||
import { createAssetWidget } from '@/platform/assets/utils/createAssetWidget'
|
||||
import type { ComfyNodeDef, InputSpec } from '@/schemas/nodeDefSchema'
|
||||
import { app } from '@/scripts/app'
|
||||
import {
|
||||
ComfyWidgets,
|
||||
addValueControlWidgets,
|
||||
isValidWidgetType
|
||||
} from '@/scripts/widgets'
|
||||
import { isPrimitiveNode } from '@/renderer/utils/nodeTypeGuards'
|
||||
import { CONFIG, GET_CONFIG } from '@/services/litegraphService'
|
||||
import { mergeInputSpec } from '@/utils/nodeDefUtil'
|
||||
import { applyTextReplacements } from '@/utils/searchAndReplace'
|
||||
|
||||
import { applyFirstWidgetValueToGraph } from './widgetValuePropagation'
|
||||
|
||||
const replacePropertyName = 'Run widget replace on values'
|
||||
export class PrimitiveNode extends LGraphNode {
|
||||
controlValues?: TWidgetValue[]
|
||||
lastType?: string
|
||||
static override category: string
|
||||
constructor(title: string) {
|
||||
super(title)
|
||||
this.addOutput('connect to widget input', '*')
|
||||
this.serialize_widgets = true
|
||||
this.isVirtualNode = true
|
||||
|
||||
if (!this.properties || !(replacePropertyName in this.properties)) {
|
||||
this.addProperty(replacePropertyName, false, 'boolean')
|
||||
}
|
||||
}
|
||||
|
||||
override applyToGraph(extraLinks: LLink[] = []) {
|
||||
const sourceWidget = this.widgets?.[0]
|
||||
const graph = this.graph
|
||||
if (!sourceWidget || !graph) return
|
||||
|
||||
let v = sourceWidget.value
|
||||
if (v && this.properties[replacePropertyName]) {
|
||||
v = applyTextReplacements(graph, v as string)
|
||||
}
|
||||
applyFirstWidgetValueToGraph(this, extraLinks, () => v)
|
||||
}
|
||||
|
||||
override refreshComboInNode() {
|
||||
const widget = this.widgets?.[0]
|
||||
if (widget?.type === 'combo') {
|
||||
// @ts-expect-error fixme ts strict error
|
||||
widget.options.values = this.outputs[0].widget[GET_CONFIG]()[0]
|
||||
|
||||
// @ts-expect-error fixme ts strict error
|
||||
if (!widget.options.values.includes(widget.value as string)) {
|
||||
// @ts-expect-error fixme ts strict error
|
||||
widget.value = widget.options.values[0]
|
||||
widget.callback?.(widget.value)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override onAfterGraphConfigured() {
|
||||
if (this.outputs[0].links?.length && !this.widgets?.length) {
|
||||
this._onFirstConnection()
|
||||
|
||||
// Populate widget values from config data
|
||||
if (this.widgets && this.widgets_values) {
|
||||
for (let i = 0; i < this.widgets_values.length; i++) {
|
||||
const w = this.widgets[i]
|
||||
if (w) {
|
||||
w.value = this.widgets_values[i]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Merge values if required
|
||||
this._mergeWidgetConfig()
|
||||
}
|
||||
}
|
||||
|
||||
override onConnectionsChange(
|
||||
_type: ISlotType,
|
||||
_index: number,
|
||||
connected: boolean
|
||||
) {
|
||||
if (app.configuringGraph) {
|
||||
// Dont run while the graph is still setting up
|
||||
return
|
||||
}
|
||||
|
||||
const links = this.outputs[0].links
|
||||
if (connected) {
|
||||
if (links?.length && !this.widgets?.length) {
|
||||
this._onFirstConnection()
|
||||
}
|
||||
} else {
|
||||
// We may have removed a link that caused the constraints to change
|
||||
this._mergeWidgetConfig()
|
||||
|
||||
if (!links?.length) {
|
||||
this.onLastDisconnect()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override onConnectOutput(
|
||||
slot: number,
|
||||
_type: string,
|
||||
input: INodeInputSlot,
|
||||
target_node: LGraphNode,
|
||||
target_slot: number
|
||||
) {
|
||||
// Fires before the link is made allowing us to reject it if it isn't valid
|
||||
// No widget, we can't connect
|
||||
if (!input.widget && !(input.type in ComfyWidgets)) {
|
||||
return false
|
||||
}
|
||||
|
||||
if (this.outputs[slot].links?.length) {
|
||||
const valid = this._isValidConnection(input)
|
||||
if (valid) {
|
||||
// On connect of additional outputs, copy our value to their widget
|
||||
this.applyToGraph([{ target_id: target_node.id, target_slot } as LLink])
|
||||
}
|
||||
return valid
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
private _onFirstConnection(recreating?: boolean) {
|
||||
// First connection can fire before the graph is ready on initial load so random things can be missing
|
||||
if (!this.outputs[0].links || !this.graph) {
|
||||
this.onLastDisconnect()
|
||||
return
|
||||
}
|
||||
const linkId = this.outputs[0].links[0]
|
||||
const link = this.graph.links[linkId]
|
||||
if (!link) return
|
||||
|
||||
const theirNode = this.graph.getNodeById(link.target_id)
|
||||
if (!theirNode || !theirNode.inputs) return
|
||||
|
||||
const input = theirNode.inputs[link.target_slot]
|
||||
if (!input) return
|
||||
|
||||
let widget
|
||||
if (!input.widget) {
|
||||
if (!(input.type in ComfyWidgets)) return
|
||||
widget = { name: input.name, [GET_CONFIG]: () => [input.type, {}] } //fake widget
|
||||
} else {
|
||||
widget = input.widget
|
||||
}
|
||||
|
||||
// @ts-expect-error fixme ts strict error
|
||||
const config = widget[GET_CONFIG]?.()
|
||||
if (!config) return
|
||||
|
||||
const { type } = getWidgetType(config)
|
||||
// Update our output to restrict to the widget type
|
||||
this.outputs[0].type = type
|
||||
this.outputs[0].name = type
|
||||
this.outputs[0].widget = widget
|
||||
|
||||
this._createWidget(
|
||||
widget[CONFIG] ?? config,
|
||||
theirNode,
|
||||
widget.name,
|
||||
// @ts-expect-error fixme ts strict error
|
||||
recreating
|
||||
)
|
||||
}
|
||||
|
||||
private _createWidget(
|
||||
inputData: InputSpec,
|
||||
node: LGraphNode,
|
||||
widgetName: string,
|
||||
recreating: boolean
|
||||
) {
|
||||
let type = inputData[0]
|
||||
|
||||
if (type instanceof Array) {
|
||||
type = 'COMBO'
|
||||
}
|
||||
|
||||
// Store current size as addWidget resizes the node
|
||||
const [oldWidth, oldHeight] = this.size
|
||||
let widget: IBaseWidget
|
||||
|
||||
if (
|
||||
type === 'COMBO' &&
|
||||
assetService.shouldUseAssetBrowser(node.comfyClass, widgetName)
|
||||
) {
|
||||
widget = this._createAssetWidget(node, widgetName, inputData)
|
||||
const theirWidget = node.widgets?.find((w) => w.name === widgetName)
|
||||
if (theirWidget) widget.value = theirWidget.value
|
||||
this._finalizeWidget(widget, oldWidth, oldHeight, recreating)
|
||||
return
|
||||
}
|
||||
|
||||
if (isValidWidgetType(type)) {
|
||||
widget = (ComfyWidgets[type](this, 'value', inputData, app) || {}).widget
|
||||
} else {
|
||||
// @ts-expect-error InputSpec is not typed correctly
|
||||
widget = this.addWidget(type, 'value', null, () => {}, {})
|
||||
}
|
||||
|
||||
if (node?.widgets && widget) {
|
||||
const theirWidget = node.widgets.find((w) => w.name === widgetName)
|
||||
if (theirWidget) {
|
||||
widget.value = theirWidget.value
|
||||
}
|
||||
}
|
||||
|
||||
if (
|
||||
!inputData?.[1]?.control_after_generate &&
|
||||
(widget.type === 'number' || widget.type === 'combo')
|
||||
) {
|
||||
let control_value = this.widgets_values?.[1]
|
||||
if (!control_value) {
|
||||
control_value = 'fixed'
|
||||
}
|
||||
addValueControlWidgets(
|
||||
this,
|
||||
widget,
|
||||
control_value as string,
|
||||
undefined,
|
||||
inputData
|
||||
)
|
||||
if (this.widgets?.[1]) widget.linkedWidgets = [this.widgets[1]]
|
||||
|
||||
const filter = this.widgets_values?.[2]
|
||||
if (filter && this.widgets && this.widgets.length === 3) {
|
||||
this.widgets[2].value = filter
|
||||
}
|
||||
}
|
||||
|
||||
// Restore any saved control values
|
||||
const controlValues = this.controlValues
|
||||
if (
|
||||
this.widgets &&
|
||||
this.lastType === this.widgets[0]?.type &&
|
||||
controlValues?.length === this.widgets.length - 1
|
||||
) {
|
||||
for (let i = 0; i < controlValues.length; i++) {
|
||||
this.widgets[i + 1].value = controlValues[i]
|
||||
}
|
||||
}
|
||||
|
||||
this._finalizeWidget(widget, oldWidth, oldHeight, recreating)
|
||||
}
|
||||
|
||||
private _createAssetWidget(
|
||||
targetNode: LGraphNode,
|
||||
targetInputName: string,
|
||||
inputData: InputSpec
|
||||
): IBaseWidget {
|
||||
const defaultValue = inputData[1]?.default as string | undefined
|
||||
return createAssetWidget({
|
||||
node: this,
|
||||
widgetName: 'value',
|
||||
nodeTypeForBrowser: targetNode.comfyClass ?? '',
|
||||
inputNameForBrowser: targetInputName,
|
||||
defaultValue
|
||||
})
|
||||
}
|
||||
|
||||
private _finalizeWidget(
|
||||
widget: IBaseWidget,
|
||||
oldWidth: number,
|
||||
oldHeight: number,
|
||||
recreating: boolean
|
||||
) {
|
||||
widget.callback = useChainCallback(widget.callback, () => {
|
||||
this.applyToGraph()
|
||||
})
|
||||
|
||||
this.setSize([
|
||||
Math.max(this.size[0], oldWidth),
|
||||
Math.max(this.size[1], oldHeight)
|
||||
])
|
||||
|
||||
if (!recreating) {
|
||||
const sz = this.computeSize()
|
||||
if (this.size[0] < sz[0]) {
|
||||
this.size[0] = sz[0]
|
||||
}
|
||||
if (this.size[1] < sz[1]) {
|
||||
this.size[1] = sz[1]
|
||||
}
|
||||
|
||||
requestAnimationFrame(() => {
|
||||
this.onResize?.(this.size)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
recreateWidget() {
|
||||
const values = this.widgets?.map((w) => w.value)
|
||||
this._removeWidgets()
|
||||
this._onFirstConnection(true)
|
||||
if (values?.length && this.widgets) {
|
||||
for (let i = 0; i < this.widgets.length; i++)
|
||||
this.widgets[i].value = values[i]
|
||||
}
|
||||
return this.widgets?.[0]
|
||||
}
|
||||
|
||||
private _mergeWidgetConfig() {
|
||||
// Merge widget configs if the node has multiple outputs
|
||||
const output = this.outputs[0]
|
||||
const links = output.links ?? []
|
||||
|
||||
const hasConfig = !!output.widget?.[CONFIG]
|
||||
if (hasConfig) {
|
||||
delete output.widget?.[CONFIG]
|
||||
}
|
||||
|
||||
if (links?.length < 2 && hasConfig) {
|
||||
// Copy the widget options from the source
|
||||
if (links.length) {
|
||||
this.recreateWidget()
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
const config1 = (output.widget?.[GET_CONFIG] as () => InputSpec)?.()
|
||||
if (!config1) return
|
||||
const isNumber = config1[0] === 'INT' || config1[0] === 'FLOAT'
|
||||
if (!isNumber || !this.graph) return
|
||||
|
||||
for (const linkId of links) {
|
||||
const link = this.graph.links[linkId]
|
||||
if (!link) continue // Can be null when removing a node
|
||||
|
||||
const theirNode = this.graph.getNodeById(link.target_id)
|
||||
if (!theirNode) continue
|
||||
const theirInput = theirNode.inputs[link.target_slot]
|
||||
|
||||
// Call is valid connection so it can merge the configs when validating
|
||||
this._isValidConnection(theirInput, hasConfig)
|
||||
}
|
||||
}
|
||||
|
||||
private _isValidConnection(input: INodeInputSlot, forceUpdate?: boolean) {
|
||||
// Only allow connections where the configs match
|
||||
const output = this.outputs?.[0]
|
||||
const config2 = (input.widget?.[GET_CONFIG] as () => InputSpec)?.()
|
||||
if (!config2) return false
|
||||
|
||||
return !!mergeIfValid.call(
|
||||
this,
|
||||
output,
|
||||
config2,
|
||||
forceUpdate,
|
||||
this.recreateWidget
|
||||
)
|
||||
}
|
||||
|
||||
private _removeWidgets() {
|
||||
if (this.widgets) {
|
||||
// Allow widgets to cleanup
|
||||
for (const w of this.widgets) {
|
||||
if (w.onRemove) {
|
||||
w.onRemove()
|
||||
}
|
||||
}
|
||||
|
||||
// Temporarily store the current values in case the node is being recreated
|
||||
this.controlValues = []
|
||||
this.lastType = this.widgets[0]?.type
|
||||
for (let i = 1; i < this.widgets.length; i++) {
|
||||
this.controlValues.push(this.widgets[i].value)
|
||||
}
|
||||
setTimeout(() => {
|
||||
delete this.lastType
|
||||
delete this.controlValues
|
||||
}, 15)
|
||||
this.widgets.length = 0
|
||||
}
|
||||
}
|
||||
|
||||
onLastDisconnect() {
|
||||
// We can't remove + re-add the output here as if you drag a link over the same link
|
||||
// it removes, then re-adds, causing it to break
|
||||
this.outputs[0].type = '*'
|
||||
this.outputs[0].name = 'connect to widget input'
|
||||
delete this.outputs[0].widget
|
||||
|
||||
this._removeWidgets()
|
||||
}
|
||||
}
|
||||
|
||||
export function getWidgetConfig(
|
||||
slot: INodeInputSlot | INodeOutputSlot
|
||||
): InputSpec {
|
||||
return (slot.widget?.[CONFIG] ??
|
||||
(slot.widget?.[GET_CONFIG] as () => InputSpec)?.() ?? [
|
||||
'*',
|
||||
{}
|
||||
]) as InputSpec
|
||||
}
|
||||
|
||||
function getConfig(this: LGraphNode, widgetName: string) {
|
||||
const { nodeData } = this.constructor
|
||||
return (
|
||||
nodeData?.input?.required?.[widgetName] ??
|
||||
nodeData?.input?.optional?.[widgetName]
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a widget to an input slot.
|
||||
* @deprecated Widget to socket conversion is no longer necessary, as they co-exist now.
|
||||
* @param node The node to convert the widget to an input slot for.
|
||||
* @param widget The widget to convert to an input slot.
|
||||
* @returns The input slot that was converted from the widget or undefined if the widget is not found.
|
||||
* @knipIgnoreUnusedButUsedByCustomNodes
|
||||
*/
|
||||
export function convertToInput(
|
||||
node: LGraphNode,
|
||||
widget: IBaseWidget
|
||||
): INodeInputSlot | undefined {
|
||||
console.warn(
|
||||
'Please remove call to convertToInput. Widget to socket conversion is no longer necessary, as they co-exist now.'
|
||||
)
|
||||
return node.inputs.find((slot) => slot.widget?.name === widget.name)
|
||||
}
|
||||
|
||||
function getWidgetType(config: InputSpec) {
|
||||
// Special handling for COMBO so we restrict links based on the entries
|
||||
let type = config[0]
|
||||
if (type instanceof Array) {
|
||||
type = 'COMBO'
|
||||
}
|
||||
return { type }
|
||||
}
|
||||
|
||||
export function setWidgetConfig(slot: INodeInputSlot, config?: InputSpec) {
|
||||
if (!slot.widget) return
|
||||
if (config) {
|
||||
slot.widget[GET_CONFIG] = () => config
|
||||
} else {
|
||||
delete slot.widget
|
||||
}
|
||||
|
||||
if (!(slot instanceof NodeSlot)) return
|
||||
const graph = slot.node.graph
|
||||
if (!graph) return
|
||||
const link = graph.getLink(slot.link)
|
||||
if (!link) return
|
||||
const originNode = graph.getNodeById(link.origin_id)
|
||||
if (!originNode || !isPrimitiveNode(originNode)) return
|
||||
if (config) {
|
||||
originNode.recreateWidget()
|
||||
} else if (!app.configuringGraph) {
|
||||
originNode.disconnectOutput(0)
|
||||
originNode.onLastDisconnect()
|
||||
}
|
||||
}
|
||||
|
||||
export function mergeIfValid(
|
||||
output: INodeOutputSlot | INodeInputSlot,
|
||||
config2: InputSpec,
|
||||
forceUpdate?: boolean,
|
||||
recreateWidget?: () => void,
|
||||
config1?: InputSpec
|
||||
): { customConfig: InputSpec[1] } {
|
||||
if (!config1) {
|
||||
config1 = getWidgetConfig(output)
|
||||
}
|
||||
|
||||
const customSpec = mergeInputSpec(config1, config2)
|
||||
|
||||
if (customSpec || forceUpdate) {
|
||||
if (customSpec) {
|
||||
// @ts-expect-error fixme ts strict error
|
||||
output.widget[CONFIG] = customSpec
|
||||
}
|
||||
|
||||
// @ts-expect-error fixme ts strict error
|
||||
const widget = recreateWidget?.call(this)
|
||||
// When deleting a node this can be null
|
||||
if (widget) {
|
||||
// @ts-expect-error fixme ts strict error
|
||||
const min = widget.options.min
|
||||
// @ts-expect-error fixme ts strict error
|
||||
const max = widget.options.max
|
||||
// @ts-expect-error fixme ts strict error
|
||||
if (min != null && widget.value < min) widget.value = min
|
||||
// @ts-expect-error fixme ts strict error
|
||||
if (max != null && widget.value > max) widget.value = max
|
||||
// @ts-expect-error fixme ts strict error
|
||||
widget.callback(widget.value)
|
||||
}
|
||||
}
|
||||
|
||||
return { customConfig: customSpec?.[1] ?? {} }
|
||||
}
|
||||
|
||||
app.registerExtension({
|
||||
name: 'Comfy.WidgetInputs',
|
||||
async beforeRegisterNodeDef(
|
||||
nodeType: typeof LGraphNode,
|
||||
_nodeData: ComfyNodeDef
|
||||
) {
|
||||
// @ts-expect-error adding extra property
|
||||
nodeType.prototype.convertWidgetToInput = function (this: LGraphNode) {
|
||||
console.warn(
|
||||
'Please remove call to convertWidgetToInput. Widget to socket conversion is no longer necessary, as they co-exist now.'
|
||||
)
|
||||
return false
|
||||
}
|
||||
|
||||
nodeType.prototype.onGraphConfigured = useChainCallback(
|
||||
nodeType.prototype.onGraphConfigured,
|
||||
function (this: LGraphNode) {
|
||||
if (!this.inputs) return
|
||||
this.widgets ??= []
|
||||
|
||||
for (const input of this.inputs) {
|
||||
if (input.widget) {
|
||||
const name = input.widget.name
|
||||
if (!input.widget[GET_CONFIG]) {
|
||||
input.widget[GET_CONFIG] = () => getConfig.call(this, name)
|
||||
}
|
||||
|
||||
const w = this.widgets?.find((w) => w.name === name)
|
||||
if (!w) {
|
||||
this.removeInput(this.inputs.findIndex((i) => i === input))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
nodeType.prototype.onConfigure = useChainCallback(
|
||||
nodeType.prototype.onConfigure,
|
||||
function (this: LGraphNode) {
|
||||
if (!app.configuringGraph && this.inputs) {
|
||||
// On copy + paste of nodes, ensure that widget configs are set up
|
||||
for (const input of this.inputs) {
|
||||
if (input.widget && !input.widget[GET_CONFIG]) {
|
||||
const name = input.widget.name
|
||||
input.widget[GET_CONFIG] = () => getConfig.call(this, name)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
// Double click a widget input to automatically attach a primitive
|
||||
const origOnInputDblClick = nodeType.prototype.onInputDblClick
|
||||
nodeType.prototype.onInputDblClick = function (
|
||||
this: LGraphNode,
|
||||
...[slot, ...args]: Parameters<NonNullable<typeof origOnInputDblClick>>
|
||||
) {
|
||||
const r = origOnInputDblClick?.apply(this, [slot, ...args])
|
||||
|
||||
const input = this.inputs[slot]
|
||||
if (!input.widget) {
|
||||
// Not a widget input or already handled input
|
||||
if (
|
||||
!(input.type in ComfyWidgets) &&
|
||||
!(
|
||||
(
|
||||
input.widget?.[GET_CONFIG] as (() => InputSpec) | undefined
|
||||
)?.()?.[0] instanceof Array
|
||||
)
|
||||
) {
|
||||
return r //also Not a ComfyWidgets input or combo (do nothing)
|
||||
}
|
||||
}
|
||||
|
||||
// Create a primitive node
|
||||
const node = LiteGraph.createNode('PrimitiveNode')
|
||||
const graph = app.canvas.graph
|
||||
if (!node || !graph) return r
|
||||
|
||||
graph?.add(node)
|
||||
|
||||
// Calculate a position that won't directly overlap another node
|
||||
const pos: [number, number] = [
|
||||
this.pos[0] - node.size[0] - 30,
|
||||
this.pos[1]
|
||||
]
|
||||
while (graph.getNodeOnPos(pos[0], pos[1], graph.nodes))
|
||||
pos[1] += LiteGraph.NODE_TITLE_HEIGHT
|
||||
|
||||
node.pos = pos
|
||||
node.connect(0, this, slot)
|
||||
node.title = input.name
|
||||
|
||||
return r
|
||||
}
|
||||
},
|
||||
registerCustomNodes() {
|
||||
LiteGraph.registerNodeType(
|
||||
'PrimitiveNode',
|
||||
Object.assign(PrimitiveNode, {
|
||||
title: 'Primitive'
|
||||
})
|
||||
)
|
||||
PrimitiveNode.category = 'utilities/primitive'
|
||||
}
|
||||
})
|
||||
@@ -0,0 +1,507 @@
|
||||
import {
|
||||
SUBGRAPH_INPUT_ID,
|
||||
SUBGRAPH_OUTPUT_ID
|
||||
} from '@/lib/litegraph/src/constants'
|
||||
import type { SubgraphInput } from '@/lib/litegraph/src/subgraph/SubgraphInput'
|
||||
import type { SubgraphOutput } from '@/lib/litegraph/src/subgraph/SubgraphOutput'
|
||||
import { useLayoutMutations } from '@/renderer/core/layout/operations/layoutMutations'
|
||||
import { LayoutSource } from '@/renderer/core/layout/types'
|
||||
import { toLinkId } from '@/types/linkId'
|
||||
import { UNASSIGNED_NODE_ID, toNodeId, serializeNodeId } from '@/types/nodeId'
|
||||
import { toRerouteId } from '@/types/rerouteId'
|
||||
|
||||
import type { LinkId } from '@/types/linkId'
|
||||
import type { RerouteId } from '@/types/rerouteId'
|
||||
import type { LGraphNode } from './LGraphNode'
|
||||
import type { NodeId, SerializedNodeId } from '@/types/nodeId'
|
||||
import type { Reroute } from './Reroute'
|
||||
import type {
|
||||
CanvasColour,
|
||||
INodeInputSlot,
|
||||
INodeOutputSlot,
|
||||
ISlotType,
|
||||
LinkNetwork,
|
||||
LinkSegment,
|
||||
Point,
|
||||
ReadonlyLinkNetwork
|
||||
} from './interfaces'
|
||||
import type { Serialisable, SerialisableLLink } from './types/serialisation'
|
||||
|
||||
const layoutMutations = useLayoutMutations()
|
||||
|
||||
export type { LinkId } from '@/types/linkId'
|
||||
export type SerialisedLLinkArray = [
|
||||
id: number,
|
||||
origin_id: SerializedNodeId,
|
||||
origin_slot: number,
|
||||
target_id: SerializedNodeId,
|
||||
target_slot: number,
|
||||
type: ISlotType
|
||||
]
|
||||
|
||||
// Resolved connection union; eliminates subgraph in/out as a possibility
|
||||
export type ResolvedConnection = BaseResolvedConnection &
|
||||
(
|
||||
| (ResolvedSubgraphInput & ResolvedNormalOutput)
|
||||
| (ResolvedNormalInput & ResolvedSubgraphOutput)
|
||||
| (ResolvedNormalInput & ResolvedNormalOutput)
|
||||
)
|
||||
|
||||
interface BaseResolvedConnection {
|
||||
link: LLink
|
||||
/** The node on the input side of the link (owns {@link input}) */
|
||||
inputNode?: LGraphNode
|
||||
/** The input the link is connected to (mutually exclusive with {@link subgraphOutput}) */
|
||||
input?: INodeInputSlot
|
||||
/** The node on the output side of the link (owns {@link output}) */
|
||||
outputNode?: LGraphNode
|
||||
/** The output the link is connected to (mutually exclusive with {@link subgraphInput}) */
|
||||
output?: INodeOutputSlot
|
||||
/** The subgraph output the link is connected to (mutually exclusive with {@link input}) */
|
||||
subgraphOutput?: SubgraphOutput
|
||||
/** The subgraph input the link is connected to (mutually exclusive with {@link output}) */
|
||||
subgraphInput?: SubgraphInput
|
||||
}
|
||||
|
||||
interface ResolvedNormalInput {
|
||||
inputNode: LGraphNode | undefined
|
||||
input: INodeInputSlot | undefined
|
||||
subgraphOutput?: undefined
|
||||
}
|
||||
|
||||
interface ResolvedNormalOutput {
|
||||
outputNode: LGraphNode | undefined
|
||||
output: INodeOutputSlot | undefined
|
||||
subgraphInput?: undefined
|
||||
}
|
||||
|
||||
interface ResolvedSubgraphInput {
|
||||
inputNode?: undefined
|
||||
/** The actual input slot the link is connected to (mutually exclusive with {@link subgraphOutput}) */
|
||||
input?: undefined
|
||||
subgraphOutput: SubgraphOutput
|
||||
}
|
||||
|
||||
interface ResolvedSubgraphOutput {
|
||||
outputNode?: undefined
|
||||
output?: undefined
|
||||
subgraphInput: SubgraphInput
|
||||
}
|
||||
|
||||
type BasicReadonlyNetwork = Pick<
|
||||
ReadonlyLinkNetwork,
|
||||
'getNodeById' | 'links' | 'getLink' | 'inputNode' | 'outputNode'
|
||||
>
|
||||
|
||||
// this is the class in charge of storing link information
|
||||
export class LLink implements LinkSegment, Serialisable<SerialisableLLink> {
|
||||
static _drawDebug = false
|
||||
|
||||
/** Link ID */
|
||||
id: LinkId
|
||||
parentId?: RerouteId
|
||||
type: ISlotType
|
||||
/** Output node ID */
|
||||
origin_id: NodeId
|
||||
/** Output slot index */
|
||||
origin_slot: number
|
||||
/** Input node ID */
|
||||
target_id: NodeId
|
||||
/** Input slot index */
|
||||
target_slot: number
|
||||
|
||||
data?: number | string | boolean | { toToolTip?(): string }
|
||||
_data?: unknown
|
||||
/** Centre point of the link, calculated during render only - can be inaccurate */
|
||||
_pos: Point
|
||||
/** @todo Clean up - never implemented in comfy. */
|
||||
_last_time?: number
|
||||
/** The last canvas 2D path that was used to render this link */
|
||||
path?: Path2D
|
||||
/** @inheritdoc */
|
||||
_centreAngle?: number
|
||||
|
||||
/** @inheritdoc */
|
||||
_dragging?: boolean
|
||||
|
||||
private _color?: CanvasColour | null
|
||||
/** Custom colour for this link only */
|
||||
public get color(): CanvasColour | null | undefined {
|
||||
return this._color
|
||||
}
|
||||
|
||||
public set color(value: CanvasColour) {
|
||||
this._color = value === '' ? null : value
|
||||
}
|
||||
|
||||
public get isFloatingOutput(): boolean {
|
||||
return this.origin_id === UNASSIGNED_NODE_ID && this.origin_slot === -1
|
||||
}
|
||||
|
||||
public get isFloatingInput(): boolean {
|
||||
return this.target_id === UNASSIGNED_NODE_ID && this.target_slot === -1
|
||||
}
|
||||
|
||||
public get isFloating(): boolean {
|
||||
return this.isFloatingOutput || this.isFloatingInput
|
||||
}
|
||||
|
||||
/** `true` if this link is connected to a subgraph input node (the actual origin is in a different graph). */
|
||||
get originIsIoNode(): boolean {
|
||||
return this.origin_id === SUBGRAPH_INPUT_ID
|
||||
}
|
||||
|
||||
/** `true` if this link is connected to a subgraph output node (the actual target is in a different graph). */
|
||||
get targetIsIoNode(): boolean {
|
||||
return this.target_id === SUBGRAPH_OUTPUT_ID
|
||||
}
|
||||
|
||||
constructor(
|
||||
id: LinkId,
|
||||
type: ISlotType,
|
||||
origin_id: SerializedNodeId,
|
||||
origin_slot: number,
|
||||
target_id: SerializedNodeId,
|
||||
target_slot: number,
|
||||
parentId?: RerouteId
|
||||
) {
|
||||
this.id = id
|
||||
this.type = type
|
||||
this.origin_id = toNodeId(origin_id)
|
||||
this.origin_slot = origin_slot
|
||||
this.target_id = toNodeId(target_id)
|
||||
this.target_slot = target_slot
|
||||
this.parentId = parentId
|
||||
|
||||
this._data = null
|
||||
// center
|
||||
this._pos = [0, 0]
|
||||
}
|
||||
|
||||
/** @deprecated Use {@link LLink.create} */
|
||||
static createFromArray(data: SerialisedLLinkArray): LLink {
|
||||
return new LLink(
|
||||
toLinkId(data[0]),
|
||||
data[5],
|
||||
data[1],
|
||||
data[2],
|
||||
data[3],
|
||||
data[4]
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* LLink static factory: creates a new LLink from the provided data.
|
||||
* @param data Serialised LLink data to create the link from
|
||||
* @returns A new LLink
|
||||
*/
|
||||
static create(data: SerialisableLLink): LLink {
|
||||
return new LLink(
|
||||
toLinkId(data.id),
|
||||
data.type,
|
||||
data.origin_id,
|
||||
data.origin_slot,
|
||||
data.target_id,
|
||||
data.target_slot,
|
||||
data.parentId === undefined ? undefined : toRerouteId(data.parentId)
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets all reroutes from the output slot to this segment. If this segment is a reroute, it will not be included.
|
||||
* @returns An ordered array of all reroutes from the node output to
|
||||
* this reroute or the reroute before it. Otherwise, an empty array.
|
||||
*/
|
||||
static getReroutes(
|
||||
network: Pick<ReadonlyLinkNetwork, 'reroutes'>,
|
||||
linkSegment: LinkSegment
|
||||
): Reroute[] {
|
||||
if (linkSegment.parentId === undefined) return []
|
||||
return network.reroutes.get(linkSegment.parentId)?.getReroutes() ?? []
|
||||
}
|
||||
|
||||
static getFirstReroute(
|
||||
network: Pick<ReadonlyLinkNetwork, 'reroutes'>,
|
||||
linkSegment: LinkSegment
|
||||
): Reroute | undefined {
|
||||
return LLink.getReroutes(network, linkSegment).at(0)
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds the reroute in the chain after the provided reroute ID.
|
||||
* @param network The network this link belongs to
|
||||
* @param linkSegment The starting point of the search (input side).
|
||||
* Typically the LLink object itself, but can be any link segment.
|
||||
* @param rerouteId The matching reroute will have this set as its {@link parentId}.
|
||||
* @returns The reroute that was found, `undefined` if no reroute was found, or `null` if an infinite loop was detected.
|
||||
*/
|
||||
static findNextReroute(
|
||||
network: Pick<ReadonlyLinkNetwork, 'reroutes'>,
|
||||
linkSegment: LinkSegment,
|
||||
rerouteId: RerouteId
|
||||
): Reroute | null | undefined {
|
||||
if (linkSegment.parentId === undefined) return
|
||||
return network.reroutes
|
||||
.get(linkSegment.parentId)
|
||||
?.findNextReroute(rerouteId)
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the origin node of a link.
|
||||
* @param network The network to search
|
||||
* @param linkId The ID of the link to get the origin node of
|
||||
* @returns The origin node of the link, or `undefined` if the link is not found or the origin node is not found
|
||||
*/
|
||||
static getOriginNode(
|
||||
network: BasicReadonlyNetwork,
|
||||
linkId: LinkId
|
||||
): LGraphNode | undefined {
|
||||
const id = network.links.get(linkId)?.origin_id
|
||||
return network.getNodeById(id) ?? undefined
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the target node of a link.
|
||||
* @param network The network to search
|
||||
* @param linkId The ID of the link to get the target node of
|
||||
* @returns The target node of the link, or `undefined` if the link is not found or the target node is not found
|
||||
*/
|
||||
static getTargetNode(
|
||||
network: BasicReadonlyNetwork,
|
||||
linkId: LinkId
|
||||
): LGraphNode | undefined {
|
||||
const id = network.links.get(linkId)?.target_id
|
||||
return network.getNodeById(id) ?? undefined
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolves a link ID to the link, node, and slot objects.
|
||||
* @param linkId The {@link id} of the link to resolve
|
||||
* @param network The link network to search
|
||||
* @returns An object containing the input and output nodes, as well as the input and output slots.
|
||||
* @remarks This method is heavier than others; it will always resolve all objects.
|
||||
* Whilst the performance difference should in most cases be negligible,
|
||||
* it is recommended to use simpler methods where appropriate.
|
||||
*/
|
||||
static resolve(
|
||||
linkId: LinkId | null | undefined,
|
||||
network: BasicReadonlyNetwork
|
||||
): ResolvedConnection | undefined {
|
||||
return network.getLink(linkId)?.resolve(network)
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolves a list of link IDs to the link, node, and slot objects.
|
||||
* Discards invalid link IDs.
|
||||
* @param linkIds An iterable of link {@link id}s to resolve
|
||||
* @param network The link network to search
|
||||
* @returns An array of resolved connections. If a link is not found, it is not included in the array.
|
||||
* @see {@link LLink.resolve}
|
||||
*/
|
||||
static resolveMany(
|
||||
linkIds: Iterable<LinkId>,
|
||||
network: BasicReadonlyNetwork
|
||||
): ResolvedConnection[] {
|
||||
const resolved: ResolvedConnection[] = []
|
||||
for (const id of linkIds) {
|
||||
const r = network.getLink(id)?.resolve(network)
|
||||
if (r) resolved.push(r)
|
||||
}
|
||||
return resolved
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolves the primitive ID values stored in the link to the referenced objects.
|
||||
* @param network The link network to search
|
||||
* @returns An object containing the input and output nodes, as well as the input and output slots.
|
||||
* @remarks This method is heavier than others; it will always resolve all objects.
|
||||
* Whilst the performance difference should in most cases be negligible,
|
||||
* it is recommended to use simpler methods where appropriate.
|
||||
*/
|
||||
resolve(network: BasicReadonlyNetwork): ResolvedConnection {
|
||||
const inputNode =
|
||||
this.target_id === UNASSIGNED_NODE_ID
|
||||
? undefined
|
||||
: (network.getNodeById(this.target_id) ?? undefined)
|
||||
const input = inputNode?.inputs[this.target_slot]
|
||||
const subgraphInput = this.originIsIoNode
|
||||
? network.inputNode?.slots[this.origin_slot]
|
||||
: undefined
|
||||
if (subgraphInput) {
|
||||
return { inputNode, input, subgraphInput, link: this }
|
||||
}
|
||||
|
||||
const outputNode =
|
||||
this.origin_id === UNASSIGNED_NODE_ID
|
||||
? undefined
|
||||
: (network.getNodeById(this.origin_id) ?? undefined)
|
||||
const output = outputNode?.outputs[this.origin_slot]
|
||||
const subgraphOutput = this.targetIsIoNode
|
||||
? network.outputNode?.slots[this.target_slot]
|
||||
: undefined
|
||||
if (subgraphOutput) {
|
||||
return {
|
||||
outputNode,
|
||||
output,
|
||||
subgraphInput: undefined,
|
||||
subgraphOutput,
|
||||
link: this
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
inputNode,
|
||||
outputNode,
|
||||
input,
|
||||
output,
|
||||
subgraphInput,
|
||||
subgraphOutput,
|
||||
link: this
|
||||
}
|
||||
}
|
||||
|
||||
configure(o: LLink | SerialisedLLinkArray) {
|
||||
if (Array.isArray(o)) {
|
||||
this.id = toLinkId(o[0])
|
||||
this.origin_id = toNodeId(o[1])
|
||||
this.origin_slot = o[2]
|
||||
this.target_id = toNodeId(o[3])
|
||||
this.target_slot = o[4]
|
||||
this.type = o[5]
|
||||
} else {
|
||||
this.id = o.id
|
||||
this.type = o.type
|
||||
this.origin_id = o.origin_id
|
||||
this.origin_slot = o.origin_slot
|
||||
this.target_id = o.target_id
|
||||
this.target_slot = o.target_slot
|
||||
this.parentId = o.parentId
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the specified node id and output index are this link's origin (output side).
|
||||
* @param nodeId ID of the node to check
|
||||
* @param outputIndex The array index of the node output
|
||||
* @returns `true` if the origin matches, otherwise `false`.
|
||||
*/
|
||||
hasOrigin(nodeId: SerializedNodeId, outputIndex: number): boolean {
|
||||
return (
|
||||
this.origin_id === toNodeId(nodeId) && this.origin_slot === outputIndex
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the specified node id and input index are this link's target (input side).
|
||||
* @param nodeId ID of the node to check
|
||||
* @param inputIndex The array index of the node input
|
||||
* @returns `true` if the target matches, otherwise `false`.
|
||||
*/
|
||||
hasTarget(nodeId: SerializedNodeId, inputIndex: number): boolean {
|
||||
return (
|
||||
this.target_id === toNodeId(nodeId) && this.target_slot === inputIndex
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a floating link from this link.
|
||||
* @param slotType The side of the link that is still connected
|
||||
* @param parentId The parent reroute ID of the link
|
||||
* @returns A new LLink that is floating
|
||||
*/
|
||||
toFloating(slotType: 'input' | 'output', parentId: RerouteId): LLink {
|
||||
const exported = this.asSerialisable()
|
||||
exported.id = toLinkId(-1)
|
||||
exported.parentId = parentId
|
||||
|
||||
if (slotType === 'input') {
|
||||
exported.origin_id = UNASSIGNED_NODE_ID
|
||||
exported.origin_slot = -1
|
||||
} else {
|
||||
exported.target_id = UNASSIGNED_NODE_ID
|
||||
exported.target_slot = -1
|
||||
}
|
||||
|
||||
return LLink.create(exported)
|
||||
}
|
||||
|
||||
/**
|
||||
* Disconnects a link and removes it from the graph, cleaning up any reroutes that are no longer used
|
||||
* @param network The container (LGraph) where reroutes should be updated
|
||||
* @param keepReroutes If `undefined`, reroutes will be automatically removed if no links remain.
|
||||
* If `input` or `output`, reroutes will not be automatically removed, and retain a connection to the input or output, respectively.
|
||||
*/
|
||||
disconnect(network: LinkNetwork, keepReroutes?: 'input' | 'output'): void {
|
||||
const reroutes = LLink.getReroutes(network, this)
|
||||
|
||||
const lastReroute = reroutes.at(-1)
|
||||
|
||||
// When floating from output, 1-to-1 ratio of floating link to final reroute (tree-like)
|
||||
const outputFloating =
|
||||
keepReroutes === 'output' &&
|
||||
lastReroute?.linkIds.size === 1 &&
|
||||
lastReroute.floatingLinkIds.size === 0
|
||||
|
||||
// When floating from inputs, the final (input side) reroute may have many floating links
|
||||
if (outputFloating || (keepReroutes === 'input' && lastReroute)) {
|
||||
const newLink = LLink.create(this)
|
||||
newLink.id = toLinkId(-1)
|
||||
|
||||
if (keepReroutes === 'input') {
|
||||
newLink.origin_id = UNASSIGNED_NODE_ID
|
||||
newLink.origin_slot = -1
|
||||
|
||||
lastReroute.floating = { slotType: 'input' }
|
||||
} else {
|
||||
newLink.target_id = UNASSIGNED_NODE_ID
|
||||
newLink.target_slot = -1
|
||||
|
||||
lastReroute.floating = { slotType: 'output' }
|
||||
}
|
||||
|
||||
network.addFloatingLink(newLink)
|
||||
}
|
||||
|
||||
for (const reroute of reroutes) {
|
||||
reroute.linkIds.delete(this.id)
|
||||
if (!keepReroutes && !reroute.totalLinks) {
|
||||
network.reroutes.delete(reroute.id)
|
||||
// Delete reroute from Layout Store
|
||||
layoutMutations.setSource(LayoutSource.Canvas)
|
||||
layoutMutations.deleteReroute(reroute.id)
|
||||
}
|
||||
}
|
||||
network.links.delete(this.id)
|
||||
// Delete link from Layout Store
|
||||
layoutMutations.setSource(LayoutSource.Canvas)
|
||||
layoutMutations.deleteLink(this.id)
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated Prefer {@link LLink.asSerialisable} (returns an object, not an array)
|
||||
* @returns An array representing this LLink
|
||||
*/
|
||||
serialize(): SerialisedLLinkArray {
|
||||
return [
|
||||
this.id,
|
||||
serializeNodeId(this.origin_id),
|
||||
this.origin_slot,
|
||||
serializeNodeId(this.target_id),
|
||||
this.target_slot,
|
||||
this.type
|
||||
]
|
||||
}
|
||||
|
||||
asSerialisable(): SerialisableLLink {
|
||||
const copy: SerialisableLLink = {
|
||||
id: this.id,
|
||||
origin_id: serializeNodeId(this.origin_id),
|
||||
origin_slot: this.origin_slot,
|
||||
target_id: serializeNodeId(this.target_id),
|
||||
target_slot: this.target_slot,
|
||||
type: this.type
|
||||
}
|
||||
if (this.parentId !== undefined) copy.parentId = this.parentId
|
||||
return copy
|
||||
}
|
||||
}
|
||||
+429
@@ -0,0 +1,429 @@
|
||||
import { toLinkId } from '@/types/linkId'
|
||||
import type { LGraph } from '@/lib/litegraph/src/LGraph'
|
||||
import type { LGraphNode } from '@/lib/litegraph/src/LGraphNode'
|
||||
import type { SerializedNodeId } from '@/types/nodeId'
|
||||
import type { LinkId } from '@/lib/litegraph/src/LLink'
|
||||
import { InvalidLinkError } from '@/lib/litegraph/src/infrastructure/InvalidLinkError'
|
||||
import { useWidgetValueStore } from '@/stores/widgetValueStore'
|
||||
import { NullGraphError } from '@/lib/litegraph/src/infrastructure/NullGraphError'
|
||||
import { RecursionError } from '@/lib/litegraph/src/infrastructure/RecursionError'
|
||||
import { SlotIndexError } from '@/lib/litegraph/src/infrastructure/SlotIndexError'
|
||||
import type { ISlotType } from '@/lib/litegraph/src/interfaces'
|
||||
import { LGraphEventMode, LiteGraph } from '@/lib/litegraph/src/litegraph'
|
||||
|
||||
import type { Subgraph } from './Subgraph'
|
||||
import type { SubgraphNode } from './SubgraphNode'
|
||||
|
||||
export type ExecutionId = string
|
||||
|
||||
/**
|
||||
* Interface describing the data transfer objects used when compiling a graph for execution.
|
||||
*/
|
||||
export type ExecutableLGraphNode = Omit<
|
||||
ExecutableNodeDTO,
|
||||
'graph' | 'node' | 'subgraphNode'
|
||||
>
|
||||
|
||||
/**
|
||||
* The end result of resolving a DTO input.
|
||||
* When a widget value is returned, {@link widgetInfo} is present and {@link origin_slot} is `-1`.
|
||||
*/
|
||||
type ResolvedInput = {
|
||||
/** DTO for the node that the link originates from. */
|
||||
node: ExecutableLGraphNode
|
||||
/** Full unique execution ID of the node that the link originates from. In the case of a widget value, this is the ID of the subgraph node. */
|
||||
origin_id: ExecutionId
|
||||
/** The slot index of the output on the node that the link originates from. `-1` when widget value is set. */
|
||||
origin_slot: number
|
||||
/** Boxed widget value (e.g. for widgets). If this box is `undefined`, then an input link is connected, and widget values from the subgraph node are ignored. */
|
||||
widgetInfo?: { value: unknown }
|
||||
}
|
||||
|
||||
/**
|
||||
* Concrete implementation of {@link ExecutableLGraphNode}.
|
||||
* @remarks This is the class that is used to create the data transfer objects for executable nodes.
|
||||
*/
|
||||
export class ExecutableNodeDTO implements ExecutableLGraphNode {
|
||||
applyToGraph?(
|
||||
...args: Parameters<NonNullable<typeof this.node.applyToGraph>>
|
||||
): ReturnType<NonNullable<typeof this.node.applyToGraph>>
|
||||
|
||||
/** The graph that this node is a part of. */
|
||||
readonly graph: LGraph | Subgraph
|
||||
|
||||
inputs: { linkId: LinkId | null; name: string; type: ISlotType }[]
|
||||
|
||||
/** Backing field for {@link id}. */
|
||||
private _id: ExecutionId
|
||||
|
||||
/**
|
||||
* The path to the actual node through subgraph instances, represented as a list of all subgraph node IDs (instances),
|
||||
* followed by the actual original node ID within the subgraph. Each segment is separated by `:`.
|
||||
*
|
||||
* e.g. `1:2:3`:
|
||||
* - `1` is the node ID of the first subgraph node in the parent workflow
|
||||
* - `2` is the node ID of the second subgraph node in the first subgraph
|
||||
* - `3` is the node ID of the actual node in the subgraph definition
|
||||
*/
|
||||
get id() {
|
||||
return this._id
|
||||
}
|
||||
|
||||
get type() {
|
||||
return this.node.type
|
||||
}
|
||||
|
||||
get title() {
|
||||
return this.node.title
|
||||
}
|
||||
|
||||
get mode() {
|
||||
return this.node.mode
|
||||
}
|
||||
|
||||
get comfyClass() {
|
||||
return this.node.comfyClass
|
||||
}
|
||||
|
||||
get isVirtualNode() {
|
||||
return this.node.isVirtualNode
|
||||
}
|
||||
|
||||
get widgets() {
|
||||
return this.node.widgets
|
||||
}
|
||||
|
||||
get subgraphId() {
|
||||
return this.subgraphNode?.subgraph.id
|
||||
}
|
||||
|
||||
constructor(
|
||||
/** The actual node that this DTO wraps. */
|
||||
readonly node: LGraphNode | SubgraphNode,
|
||||
/** A list of subgraph instance node IDs from the root graph to the containing instance. @see {@link id} */
|
||||
readonly subgraphNodePath: readonly SerializedNodeId[],
|
||||
/** A flattened map of all DTOs in this node network. Subgraph instances have been expanded into their inner nodes. */
|
||||
readonly nodesByExecutionId: Map<ExecutionId, ExecutableLGraphNode>,
|
||||
/** The actual subgraph instance that contains this node, otherwise undefined. */
|
||||
readonly subgraphNode?: SubgraphNode
|
||||
) {
|
||||
if (!node.graph) throw new NullGraphError()
|
||||
|
||||
// Set the internal ID of the DTO
|
||||
this._id = [...this.subgraphNodePath, this.node.id].join(':')
|
||||
this.graph = node.graph
|
||||
this.inputs = this.node.inputs.map((x) => ({
|
||||
linkId: x.link == null ? null : toLinkId(x.link),
|
||||
name: x.name,
|
||||
type: x.type
|
||||
}))
|
||||
|
||||
// Only create a wrapper if the node has an applyToGraph method
|
||||
if (this.node.applyToGraph) {
|
||||
this.applyToGraph = (...args) => this.node.applyToGraph?.(...args)
|
||||
}
|
||||
}
|
||||
|
||||
/** Returns either the DTO itself, or the DTOs of the inner nodes of the subgraph. */
|
||||
getInnerNodes(): ExecutableLGraphNode[] {
|
||||
return this.node.isSubgraphNode()
|
||||
? this.node.getInnerNodes(this.nodesByExecutionId, this.subgraphNodePath)
|
||||
: [this]
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolves the executable node & link IDs for a given input slot.
|
||||
* @param slot The slot index of the input.
|
||||
* @param visited Leave empty unless overriding this method.
|
||||
* A set of unique IDs, used to guard against infinite recursion.
|
||||
* If overriding, ensure that the set is passed on all recursive calls.
|
||||
* @returns The node and the origin ID / slot index of the output.
|
||||
*/
|
||||
resolveInput(
|
||||
slot: number,
|
||||
visited = new Set<string>(),
|
||||
type?: ISlotType
|
||||
): ResolvedInput | undefined {
|
||||
const uniqueId = `${this.subgraphNode?.subgraph.id}:${this.node.id}[I]${slot}`
|
||||
if (visited.has(uniqueId)) {
|
||||
const nodeInfo = `${this.node.id}${this.node.title ? ` (${this.node.title})` : ''}`
|
||||
const pathInfo =
|
||||
this.subgraphNodePath.length > 0
|
||||
? ` at path ${this.subgraphNodePath.join(':')}`
|
||||
: ''
|
||||
throw new RecursionError(
|
||||
`Circular reference detected while resolving input ${slot} of node ${nodeInfo}${pathInfo}. ` +
|
||||
`This creates an infinite loop in link resolution. UniqueID: [${uniqueId}]`
|
||||
)
|
||||
}
|
||||
visited.add(uniqueId)
|
||||
|
||||
const input = this.inputs.at(slot)
|
||||
if (!input)
|
||||
throw new SlotIndexError(
|
||||
`No input found for flattened id [${this.id}] slot [${slot}]`
|
||||
)
|
||||
|
||||
// Nothing connected
|
||||
if (input.linkId == null) return
|
||||
|
||||
const link = this.graph.getLink(input.linkId)
|
||||
if (!link)
|
||||
throw new InvalidLinkError(
|
||||
`No link found in parent graph for id [${this.id}] slot [${slot}] ${input.name}`
|
||||
)
|
||||
|
||||
const { subgraphNode } = this
|
||||
|
||||
// Link goes up and out of this subgraph
|
||||
if (subgraphNode && link.originIsIoNode) {
|
||||
const subgraphNodeInput = subgraphNode.inputs.at(link.origin_slot)
|
||||
if (!subgraphNodeInput)
|
||||
throw new SlotIndexError(
|
||||
`No input found for slot [${link.origin_slot}] ${input.name}`
|
||||
)
|
||||
|
||||
// Nothing connected
|
||||
const linkId = subgraphNodeInput.link
|
||||
if (linkId == null) {
|
||||
const id = subgraphNodeInput.widgetId
|
||||
if (!id) return
|
||||
|
||||
return {
|
||||
node: this,
|
||||
origin_id: this.id,
|
||||
origin_slot: -1,
|
||||
widgetInfo: { value: useWidgetValueStore().getWidget(id)?.value }
|
||||
}
|
||||
}
|
||||
|
||||
if (!subgraphNode.graph)
|
||||
throw new NullGraphError(
|
||||
`SubgraphNode ${subgraphNode.id} has no graph during input resolution`
|
||||
)
|
||||
const outerLink = subgraphNode.graph.getLink(linkId)
|
||||
if (!outerLink)
|
||||
throw new InvalidLinkError(
|
||||
`No outer link found for slot [${link.origin_slot}] ${input.name}`
|
||||
)
|
||||
|
||||
const subgraphNodeExecutionId: ExecutionId =
|
||||
this.subgraphNodePath.join(':')
|
||||
const subgraphNodeDto = this.nodesByExecutionId.get(
|
||||
subgraphNodeExecutionId
|
||||
)
|
||||
if (!subgraphNodeDto)
|
||||
throw new Error(
|
||||
`No subgraph node DTO found for id [${subgraphNodeExecutionId}]`
|
||||
)
|
||||
|
||||
return subgraphNodeDto.resolveInput(outerLink.target_slot, visited)
|
||||
}
|
||||
|
||||
// Not part of a subgraph; use the original link
|
||||
const outputNode = this.graph.getNodeById(link.origin_id)
|
||||
if (!outputNode)
|
||||
throw new InvalidLinkError(
|
||||
`No input node found for id [${this.id}] slot [${slot}] ${input.name}`
|
||||
)
|
||||
|
||||
const outputNodeExecutionId: ExecutionId = [
|
||||
...this.subgraphNodePath,
|
||||
outputNode.id
|
||||
].join(':')
|
||||
const outputNodeDto = this.nodesByExecutionId.get(outputNodeExecutionId)
|
||||
if (!outputNodeDto)
|
||||
throw new Error(
|
||||
`No output node DTO found for id [${outputNodeExecutionId}]`
|
||||
)
|
||||
|
||||
return outputNodeDto.resolveOutput(
|
||||
link.origin_slot,
|
||||
type ?? input.type,
|
||||
visited
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines whether this output is a valid endpoint for a link (non-virtual, non-bypass).
|
||||
* @param slot The slot index of the output.
|
||||
* @param type The type of the input
|
||||
* @param visited A set of unique IDs to guard against infinite recursion. See {@link resolveInput}.
|
||||
* @returns The node and the origin ID / slot index of the output.
|
||||
*/
|
||||
resolveOutput(
|
||||
slot: number,
|
||||
type: ISlotType,
|
||||
visited: Set<string>
|
||||
): ResolvedInput | undefined {
|
||||
const uniqueId = `${this.subgraphNode?.subgraph.id}:${this.node.id}[O]${slot}`
|
||||
if (visited.has(uniqueId)) {
|
||||
const nodeInfo = `${this.node.id}${this.node.title ? ` (${this.node.title})` : ''}`
|
||||
const pathInfo =
|
||||
this.subgraphNodePath.length > 0
|
||||
? ` at path ${this.subgraphNodePath.join(':')}`
|
||||
: ''
|
||||
throw new RecursionError(
|
||||
`Circular reference detected while resolving output ${slot} of node ${nodeInfo}${pathInfo}. ` +
|
||||
`This creates an infinite loop in link resolution. UniqueID: [${uniqueId}]`
|
||||
)
|
||||
}
|
||||
visited.add(uniqueId)
|
||||
|
||||
// Muted nodes produce no output
|
||||
if (this.mode === LGraphEventMode.NEVER) return
|
||||
|
||||
// Upstreamed: Bypass nodes are bypassed using the first input with matching type
|
||||
if (this.mode === LGraphEventMode.BYPASS) {
|
||||
// Bypass nodes by finding first input with matching type
|
||||
const matchingIndex = this._getBypassSlotIndex(slot, type)
|
||||
|
||||
// No input types match - bypass not possible
|
||||
if (matchingIndex === -1) {
|
||||
console.warn(
|
||||
`[ExecutableNodeDTO.resolveOutput] No input types match type [${type}] for id [${this.id}] slot [${slot}]`,
|
||||
this
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
return this.resolveInput(matchingIndex, visited)
|
||||
}
|
||||
|
||||
const { node } = this
|
||||
if (node.isSubgraphNode())
|
||||
return this._resolveSubgraphOutput(slot, type, visited)
|
||||
|
||||
if (node.isVirtualNode) {
|
||||
// Cross-graph virtual nodes (e.g. Set/Get) resolve their source directly.
|
||||
const virtualSource = this.node.resolveVirtualOutput?.(slot)
|
||||
if (virtualSource) {
|
||||
const inputNodeDto = [...this.nodesByExecutionId.values()].find(
|
||||
(dto) =>
|
||||
dto instanceof ExecutableNodeDTO && dto.node === virtualSource.node
|
||||
)
|
||||
if (!inputNodeDto)
|
||||
throw new Error(
|
||||
`No DTO found for virtual source node [${virtualSource.node.id}]`
|
||||
)
|
||||
|
||||
return inputNodeDto.resolveOutput(virtualSource.slot, type, visited)
|
||||
}
|
||||
const virtualLink = this.node.getInputLink(slot)
|
||||
if (virtualLink) {
|
||||
const { inputNode } = virtualLink.resolve(this.graph)
|
||||
if (!inputNode)
|
||||
throw new InvalidLinkError(
|
||||
`Virtual node failed to resolve parent [${this.id}] slot [${slot}]`
|
||||
)
|
||||
|
||||
const inputNodeExecutionId: ExecutionId = [
|
||||
...this.subgraphNodePath,
|
||||
inputNode.id
|
||||
].join(':')
|
||||
const inputNodeDto = this.nodesByExecutionId.get(inputNodeExecutionId)
|
||||
if (!inputNodeDto)
|
||||
throw new Error(`No input node DTO found for id [${inputNode.id}]`)
|
||||
|
||||
return inputNodeDto.resolveInput(virtualLink.target_slot, visited, type)
|
||||
}
|
||||
|
||||
// Virtual nodes without a matching input should be discarded.
|
||||
return
|
||||
}
|
||||
|
||||
return {
|
||||
node: this,
|
||||
origin_id: this.id,
|
||||
origin_slot: slot
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds the index of the input slot on this node that matches the given output {@link slot} index.
|
||||
* Used when bypassing nodes.
|
||||
* @param slot The output slot index on this node
|
||||
* @param type The type of the final target input (so type list matches are accurate)
|
||||
* @returns The index of the input slot on this node, otherwise `-1`.
|
||||
*/
|
||||
private _getBypassSlotIndex(slot: number, type: ISlotType) {
|
||||
const { inputs } = this
|
||||
const oppositeInput = inputs[slot]
|
||||
const outputType = this.node.outputs[slot].type
|
||||
|
||||
// Any type short circuit - match slot ID, fallback to first slot
|
||||
if (type === '*' || type === '') {
|
||||
return inputs.length > slot ? slot : 0
|
||||
}
|
||||
|
||||
// Prefer input with the same slot ID
|
||||
if (
|
||||
oppositeInput &&
|
||||
LiteGraph.isValidConnection(oppositeInput.type, outputType) &&
|
||||
LiteGraph.isValidConnection(oppositeInput.type, type)
|
||||
) {
|
||||
return slot
|
||||
}
|
||||
|
||||
// Preserve legacy behaviour; use exact match first.
|
||||
const exactMatch = inputs.findIndex((input) => input.type === type)
|
||||
if (exactMatch !== -1) return exactMatch
|
||||
|
||||
// Find first matching slot - prefer exact type
|
||||
return inputs.findIndex(
|
||||
(input) =>
|
||||
LiteGraph.isValidConnection(input.type, outputType) &&
|
||||
LiteGraph.isValidConnection(input.type, type)
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolves the link inside a subgraph node, from the subgraph IO node to the node inside the subgraph.
|
||||
* @param slot The slot index of the output on the subgraph node.
|
||||
* @param visited A set of unique IDs to guard against infinite recursion. See {@link resolveInput}.
|
||||
* @returns A DTO for the node, and the origin ID / slot index of the output.
|
||||
*/
|
||||
private _resolveSubgraphOutput(
|
||||
slot: number,
|
||||
type: ISlotType,
|
||||
visited: Set<string>
|
||||
): ResolvedInput | undefined {
|
||||
const { node } = this
|
||||
const output = node.outputs.at(slot)
|
||||
|
||||
if (!output)
|
||||
throw new SlotIndexError(
|
||||
`No output found for flattened id [${this.id}] slot [${slot}]`
|
||||
)
|
||||
if (!node.isSubgraphNode())
|
||||
throw new TypeError(`Node is not a subgraph node: ${node.id}`)
|
||||
|
||||
// Link inside the subgraph
|
||||
const innerResolved = node.resolveSubgraphOutputLink(slot)
|
||||
if (!innerResolved) return
|
||||
|
||||
const innerNode = innerResolved.outputNode
|
||||
if (!innerNode)
|
||||
throw new Error(
|
||||
`No output node found for id [${this.id}] slot [${slot}] ${output.name}`
|
||||
)
|
||||
|
||||
// Recurse into the subgraph
|
||||
const innerNodeExecutionId: ExecutionId = [
|
||||
...this.subgraphNodePath,
|
||||
node.id,
|
||||
innerNode.id
|
||||
].join(':')
|
||||
const innerNodeDto = this.nodesByExecutionId.get(innerNodeExecutionId)
|
||||
if (!innerNodeDto)
|
||||
throw new Error(
|
||||
`No inner node DTO found for id [${innerNodeExecutionId}]`
|
||||
)
|
||||
|
||||
return innerNodeDto.resolveOutput(
|
||||
innerResolved.link.origin_slot,
|
||||
type,
|
||||
visited
|
||||
)
|
||||
}
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
// Re-export Subgraph and GraphOrSubgraph from LGraph.ts to maintain compatibility
|
||||
// This is a temporary fix to resolve circular dependency issues
|
||||
export { Subgraph, type GraphOrSubgraph } from '@/lib/litegraph/src/LGraph'
|
||||
+948
@@ -0,0 +1,948 @@
|
||||
import cloneDeep from 'es-toolkit/compat/cloneDeep'
|
||||
import type { BaseLGraph, LGraph, SubgraphId } from '@/lib/litegraph/src/LGraph'
|
||||
import type { LGraphButton } from '@/lib/litegraph/src/LGraphButton'
|
||||
import type { LGraphCanvas } from '@/lib/litegraph/src/LGraphCanvas'
|
||||
import { LGraphNode } from '@/lib/litegraph/src/LGraphNode'
|
||||
import type { DrawTitleBoxOptions } from '@/lib/litegraph/src/LGraphNode'
|
||||
import { LLink } from '@/lib/litegraph/src/LLink'
|
||||
import type { ResolvedConnection } from '@/lib/litegraph/src/LLink'
|
||||
import { NullGraphError } from '@/lib/litegraph/src/infrastructure/NullGraphError'
|
||||
import { RecursionError } from '@/lib/litegraph/src/infrastructure/RecursionError'
|
||||
import type {
|
||||
ISubgraphInput,
|
||||
IWidgetLocator
|
||||
} from '@/lib/litegraph/src/interfaces'
|
||||
import { LiteGraph } from '@/lib/litegraph/src/litegraph'
|
||||
import type { INodeInputSlot, ISlotType } from '@/lib/litegraph/src/litegraph'
|
||||
import { NodeInputSlot } from '@/lib/litegraph/src/node/NodeInputSlot'
|
||||
import { NodeOutputSlot } from '@/lib/litegraph/src/node/NodeOutputSlot'
|
||||
import { toNodeId } from '@/types/nodeId'
|
||||
import type { SerializedNodeId } from '@/types/nodeId'
|
||||
import type {
|
||||
GraphOrSubgraph,
|
||||
Subgraph
|
||||
} from '@/lib/litegraph/src/subgraph/Subgraph'
|
||||
import type {
|
||||
ExportedSubgraphInstance,
|
||||
ISerialisedNode
|
||||
} from '@/lib/litegraph/src/types/serialisation'
|
||||
import { NodeSlotType } from '@/lib/litegraph/src/types/globalEnums'
|
||||
import type {
|
||||
IBaseWidget,
|
||||
TWidgetValue
|
||||
} from '@/lib/litegraph/src/types/widgets'
|
||||
import { isWidgetValue } from '@/lib/litegraph/src/types/widgets'
|
||||
import { resolveConcretePromotedWidget } from '@/core/graph/subgraph/resolveConcretePromotedWidget'
|
||||
import { resolveSubgraphInputTarget } from '@/core/graph/subgraph/resolveSubgraphInputTarget'
|
||||
import { parsePreviewExposures } from '@/core/schemas/previewExposureSchema'
|
||||
import { parseProxyWidgetErrorQuarantine } from '@/core/schemas/proxyWidgetQuarantineSchema'
|
||||
import { usePreviewExposureStore } from '@/stores/previewExposureStore'
|
||||
import { useWidgetValueStore } from '@/stores/widgetValueStore'
|
||||
import { createNodeLocatorId } from '@/types/nodeIdentification'
|
||||
import type { WidgetId } from '@/types/widgetId'
|
||||
import { widgetId } from '@/types/widgetId'
|
||||
|
||||
import { ExecutableNodeDTO } from './ExecutableNodeDTO'
|
||||
import type { ExecutableLGraphNode, ExecutionId } from './ExecutableNodeDTO'
|
||||
import type { SubgraphInput } from './SubgraphInput'
|
||||
import { createBitmapCache } from './svgBitmapCache'
|
||||
|
||||
const workflowSvg = new Image()
|
||||
workflowSvg.src =
|
||||
"data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16' width='16' height='16'%3E%3Csvg xmlns='http://www.w3.org/2000/svg' fill='none' viewBox='0 0 16 16'%3E%3Cpath stroke='white' stroke-linecap='round' stroke-width='1.3' d='M9.18613 3.09999H6.81377M9.18613 12.9H7.55288c-3.08678 0-5.35171-2.99581-4.60305-6.08843l.3054-1.26158M14.7486 2.1721l-.5931 2.45c-.132.54533-.6065.92789-1.1508.92789h-2.2993c-.77173 0-1.33797-.74895-1.1508-1.5221l.5931-2.45c.132-.54533.6065-.9279 1.1508-.9279h2.2993c.7717 0 1.3379.74896 1.1508 1.52211Zm-8.3033 0-.59309 2.45c-.13201.54533-.60646.92789-1.15076.92789H2.4021c-.7717 0-1.33793-.74895-1.15077-1.5221l.59309-2.45c.13201-.54533.60647-.9279 1.15077-.9279h2.29935c.77169 0 1.33792.74896 1.15076 1.52211Zm8.3033 9.8-.5931 2.45c-.132.5453-.6065.9279-1.1508.9279h-2.2993c-.77173 0-1.33797-.749-1.1508-1.5221l.5931-2.45c.132-.5453.6065-.9279 1.1508-.9279h2.2993c.7717 0 1.3379.7489 1.1508 1.5221Z'/%3E%3C/svg%3E %3C/svg%3E"
|
||||
|
||||
const workflowBitmapCache = createBitmapCache(workflowSvg, 32)
|
||||
|
||||
export class SubgraphNode extends LGraphNode implements BaseLGraph {
|
||||
declare inputs: (INodeInputSlot & Partial<ISubgraphInput>)[]
|
||||
|
||||
override readonly type: SubgraphId
|
||||
override readonly isVirtualNode = true as const
|
||||
override graph: GraphOrSubgraph | null
|
||||
|
||||
get rootGraph(): LGraph {
|
||||
if (!this.graph)
|
||||
throw new NullGraphError(`SubgraphNode ${this.id} has no graph`)
|
||||
return this.graph.rootGraph
|
||||
}
|
||||
|
||||
get isDetached(): boolean {
|
||||
return !this.graph
|
||||
}
|
||||
|
||||
override get displayType(): string {
|
||||
return 'Subgraph node'
|
||||
}
|
||||
|
||||
override isSubgraphNode(): this is SubgraphNode {
|
||||
return true
|
||||
}
|
||||
|
||||
declare widgets: IBaseWidget[]
|
||||
|
||||
/**
|
||||
* Retained as a no-op for extension compatibility: promoted host widgets are
|
||||
* now store-backed and addressed by widgetId, so there is no view cache to
|
||||
* invalidate.
|
||||
*/
|
||||
invalidatePromotedViews(): void {}
|
||||
|
||||
private _eventAbortController = new AbortController()
|
||||
|
||||
constructor(
|
||||
graph: GraphOrSubgraph,
|
||||
readonly subgraph: Subgraph,
|
||||
instanceData: ExportedSubgraphInstance
|
||||
) {
|
||||
super(subgraph.name, subgraph.id)
|
||||
this.graph = graph
|
||||
|
||||
Object.defineProperty(this, 'widgets', {
|
||||
get: () =>
|
||||
this.inputs.flatMap((input) => {
|
||||
const widget = this._projectPromotedWidget(input)
|
||||
return widget ? [widget] : []
|
||||
}),
|
||||
set: () => {
|
||||
if (import.meta.env.DEV)
|
||||
console.warn(
|
||||
'Cannot manually set widgets on SubgraphNode; use the promotion system.'
|
||||
)
|
||||
},
|
||||
configurable: true,
|
||||
enumerable: true
|
||||
})
|
||||
|
||||
const subgraphEvents = this.subgraph.events
|
||||
const { signal } = this._eventAbortController
|
||||
|
||||
subgraphEvents.addEventListener(
|
||||
'input-added',
|
||||
(e) => {
|
||||
const subgraphInput = e.detail.input
|
||||
const { name, type } = subgraphInput
|
||||
const existingInput = this.inputs.find(
|
||||
(input) =>
|
||||
input._subgraphSlot === subgraphInput ||
|
||||
(input._subgraphSlot && input._subgraphSlot.id === subgraphInput.id)
|
||||
)
|
||||
if (existingInput) {
|
||||
this._addSubgraphInputListeners(subgraphInput, existingInput)
|
||||
const linkId = subgraphInput.linkIds[0]
|
||||
if (linkId === undefined) return
|
||||
|
||||
const link = this.subgraph.getLink(linkId)
|
||||
if (!link) return
|
||||
|
||||
const { inputNode, input } = link.resolve(subgraph)
|
||||
if (!inputNode || !input) return
|
||||
|
||||
const widget = inputNode.getWidgetFromSlot(input)
|
||||
if (widget)
|
||||
this._setWidget(subgraphInput, existingInput, widget, input.widget)
|
||||
return
|
||||
}
|
||||
const input = this.addInput(name, type, {
|
||||
_subgraphSlot: subgraphInput
|
||||
})
|
||||
this.invalidatePromotedViews()
|
||||
|
||||
this._addSubgraphInputListeners(subgraphInput, input)
|
||||
},
|
||||
{ signal }
|
||||
)
|
||||
|
||||
subgraphEvents.addEventListener(
|
||||
'removing-input',
|
||||
(e) => {
|
||||
const widget = e.detail.input._widget
|
||||
if (widget) this.ensureWidgetRemoved(widget)
|
||||
|
||||
this.removeInput(e.detail.index)
|
||||
this.invalidatePromotedViews()
|
||||
this.setDirtyCanvas(true, true)
|
||||
},
|
||||
{ signal }
|
||||
)
|
||||
|
||||
subgraphEvents.addEventListener(
|
||||
'output-added',
|
||||
(e) => {
|
||||
const { name, type } = e.detail.output
|
||||
this.addOutput(name, type)
|
||||
},
|
||||
{ signal }
|
||||
)
|
||||
|
||||
subgraphEvents.addEventListener(
|
||||
'removing-output',
|
||||
(e) => {
|
||||
this.removeOutput(e.detail.index)
|
||||
this.setDirtyCanvas(true, true)
|
||||
},
|
||||
{ signal }
|
||||
)
|
||||
|
||||
subgraphEvents.addEventListener(
|
||||
'renaming-input',
|
||||
(e) => {
|
||||
const { index, newName } = e.detail
|
||||
const input = this.inputs.at(index)
|
||||
if (!input) throw new Error('Subgraph input not found')
|
||||
|
||||
input.label = newName
|
||||
// Do NOT change input.widget.name — it is the stable internal
|
||||
// identifier used by onGraphConfigured (widgetInputs.ts) to match
|
||||
// inputs to widgets. Changing it to the display label would cause
|
||||
// collisions when two promoted inputs share the same label.
|
||||
if (input._widget) input._widget.label = newName
|
||||
if (input.widgetId) {
|
||||
const state = useWidgetValueStore().getWidget(input.widgetId)
|
||||
if (state) state.label = newName
|
||||
}
|
||||
this.invalidatePromotedViews()
|
||||
this.graph?.trigger('node:slot-label:changed', {
|
||||
nodeId: this.id,
|
||||
slotType: NodeSlotType.INPUT
|
||||
})
|
||||
},
|
||||
{ signal }
|
||||
)
|
||||
|
||||
subgraphEvents.addEventListener(
|
||||
'renaming-output',
|
||||
(e) => {
|
||||
const { index, newName } = e.detail
|
||||
const output = this.outputs.at(index)
|
||||
if (!output) throw new Error('Subgraph output not found')
|
||||
|
||||
output.label = newName
|
||||
this.graph?.trigger('node:slot-label:changed', {
|
||||
nodeId: this.id,
|
||||
slotType: NodeSlotType.OUTPUT
|
||||
})
|
||||
},
|
||||
{ signal }
|
||||
)
|
||||
|
||||
this.type = subgraph.id
|
||||
this.configure(instanceData)
|
||||
|
||||
this.addTitleButton({
|
||||
name: 'enter_subgraph',
|
||||
text: '\uE93B', // Unicode for pi-window-maximize
|
||||
yOffset: 0, // No vertical offset needed, button is centered
|
||||
xOffset: -10,
|
||||
fontSize: 16
|
||||
})
|
||||
}
|
||||
|
||||
override onTitleButtonClick(
|
||||
button: LGraphButton,
|
||||
canvas: LGraphCanvas
|
||||
): void {
|
||||
if (button.name === 'enter_subgraph') {
|
||||
canvas.openSubgraph(this.subgraph, this)
|
||||
} else {
|
||||
super.onTitleButtonClick(button, canvas)
|
||||
}
|
||||
}
|
||||
|
||||
private _projectPromotedWidget(
|
||||
input: INodeInputSlot
|
||||
): IBaseWidget | undefined {
|
||||
if (input._widget) return input._widget
|
||||
|
||||
const id = input.widgetId
|
||||
if (!id) return
|
||||
|
||||
const store = useWidgetValueStore()
|
||||
const widget: IBaseWidget = {
|
||||
get name() {
|
||||
return store.getWidget(id)?.name ?? input.name
|
||||
},
|
||||
get label() {
|
||||
return store.getWidget(id)?.label ?? input.label ?? input.name
|
||||
},
|
||||
set label(next) {
|
||||
const state = store.getWidget(id)
|
||||
if (state) state.label = next
|
||||
},
|
||||
get y() {
|
||||
return store.getWidget(id)?.y ?? 0
|
||||
},
|
||||
set y(next) {
|
||||
const state = store.getWidget(id)
|
||||
if (state) state.y = next
|
||||
},
|
||||
get type() {
|
||||
return store.getWidget(id)?.type ?? 'text'
|
||||
},
|
||||
get options() {
|
||||
return store.getWidget(id)?.options ?? {}
|
||||
},
|
||||
get value() {
|
||||
const value = store.getWidget(id)?.value
|
||||
return isWidgetValue(value) ? value : undefined
|
||||
},
|
||||
set value(next) {
|
||||
store.setValue(id, next)
|
||||
},
|
||||
// Canvas edits operate on a transient concrete widget (toConcreteWidget),
|
||||
// so the value setter above is never invoked; BaseWidget.setValue writes
|
||||
// its own local state and then calls this callback, which is the only
|
||||
// bridge back to the store.
|
||||
callback(next) {
|
||||
store.setValue(id, next)
|
||||
}
|
||||
}
|
||||
Object.defineProperty(widget, 'widgetId', {
|
||||
value: id,
|
||||
enumerable: false,
|
||||
configurable: true
|
||||
})
|
||||
input._widget = widget
|
||||
return input._widget
|
||||
}
|
||||
|
||||
private _addSubgraphInputListeners(
|
||||
subgraphInput: SubgraphInput,
|
||||
input: INodeInputSlot & Partial<ISubgraphInput>
|
||||
) {
|
||||
input._subgraphSlot = subgraphInput
|
||||
|
||||
if (
|
||||
input._listenerController &&
|
||||
typeof input._listenerController.abort === 'function'
|
||||
) {
|
||||
input._listenerController.abort()
|
||||
}
|
||||
input._listenerController = new AbortController()
|
||||
const { signal } = input._listenerController
|
||||
|
||||
subgraphInput.events.addEventListener(
|
||||
'input-connected',
|
||||
(e) => {
|
||||
input.shape = this.getSlotShape(subgraphInput, e.detail.input)
|
||||
if (!e.detail.widget || !e.detail.node) return
|
||||
|
||||
this._setWidget(
|
||||
subgraphInput,
|
||||
input,
|
||||
e.detail.widget,
|
||||
e.detail.input.widget
|
||||
)
|
||||
},
|
||||
{ signal }
|
||||
)
|
||||
|
||||
subgraphInput.events.addEventListener(
|
||||
'input-disconnected',
|
||||
() => {
|
||||
this.invalidatePromotedViews()
|
||||
input.shape = this.getSlotShape(subgraphInput)
|
||||
|
||||
const connectedWidgets = subgraphInput.getConnectedWidgets()
|
||||
if (connectedWidgets.length > 0) {
|
||||
this._resolveInputWidget(subgraphInput, input)
|
||||
this.invalidatePromotedViews()
|
||||
return
|
||||
}
|
||||
|
||||
if (input._widget) this.ensureWidgetRemoved(input._widget)
|
||||
if (input.widgetId) {
|
||||
const id = input.widgetId
|
||||
queueMicrotask(() => {
|
||||
if (this.inputs.some((input) => input.widgetId === id)) return
|
||||
useWidgetValueStore().deleteWidget(id)
|
||||
})
|
||||
}
|
||||
|
||||
delete input.pos
|
||||
delete input.widget
|
||||
delete input.widgetId
|
||||
input._widget = undefined
|
||||
this.invalidatePromotedViews()
|
||||
},
|
||||
{ signal }
|
||||
)
|
||||
}
|
||||
|
||||
private _rebindInputSubgraphSlots(): void {
|
||||
this.invalidatePromotedViews()
|
||||
|
||||
const subgraphSlots = [...this.subgraph.inputNode.slots]
|
||||
const slotsBySignature = new Map<string, SubgraphInput[]>()
|
||||
const slotsByName = new Map<string, SubgraphInput[]>()
|
||||
|
||||
for (const slot of subgraphSlots) {
|
||||
const signature = `${slot.name}:${String(slot.type)}`
|
||||
const signatureSlots = slotsBySignature.get(signature)
|
||||
if (signatureSlots) {
|
||||
signatureSlots.push(slot)
|
||||
} else {
|
||||
slotsBySignature.set(signature, [slot])
|
||||
}
|
||||
|
||||
const nameSlots = slotsByName.get(slot.name)
|
||||
if (nameSlots) {
|
||||
nameSlots.push(slot)
|
||||
} else {
|
||||
slotsByName.set(slot.name, [slot])
|
||||
}
|
||||
}
|
||||
|
||||
const assignedSlotIds = new Set<string>()
|
||||
const takeUnassignedSlot = (
|
||||
slots: SubgraphInput[] | undefined
|
||||
): SubgraphInput | undefined => {
|
||||
if (!slots) return undefined
|
||||
return slots.find((slot) => !assignedSlotIds.has(String(slot.id)))
|
||||
}
|
||||
|
||||
for (const input of this.inputs) {
|
||||
const existingSlot = input._subgraphSlot
|
||||
if (
|
||||
existingSlot &&
|
||||
this.subgraph.inputNode.slots.some((slot) => slot === existingSlot)
|
||||
) {
|
||||
assignedSlotIds.add(String(existingSlot.id))
|
||||
continue
|
||||
}
|
||||
|
||||
const signature = `${input.name}:${String(input.type)}`
|
||||
const matchedSlot =
|
||||
takeUnassignedSlot(slotsBySignature.get(signature)) ??
|
||||
takeUnassignedSlot(slotsByName.get(input.name))
|
||||
|
||||
if (matchedSlot) {
|
||||
input._subgraphSlot = matchedSlot
|
||||
assignedSlotIds.add(String(matchedSlot.id))
|
||||
} else {
|
||||
delete input._subgraphSlot
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override configure(info: ExportedSubgraphInstance): void {
|
||||
for (const input of this.inputs) {
|
||||
if (
|
||||
input._listenerController &&
|
||||
typeof input._listenerController.abort === 'function'
|
||||
) {
|
||||
input._listenerController.abort()
|
||||
}
|
||||
}
|
||||
|
||||
this.inputs.length = 0
|
||||
this.inputs.push(
|
||||
...this.subgraph.inputNode.slots.map((slot) =>
|
||||
Object.assign(
|
||||
new NodeInputSlot(
|
||||
{
|
||||
name: slot.name,
|
||||
localized_name: slot.localized_name,
|
||||
label: slot.label,
|
||||
shape: this.getSlotShape(slot),
|
||||
type: slot.type,
|
||||
link: null
|
||||
},
|
||||
this
|
||||
),
|
||||
{
|
||||
_subgraphSlot: slot
|
||||
}
|
||||
)
|
||||
)
|
||||
)
|
||||
this.invalidatePromotedViews()
|
||||
|
||||
this.outputs.length = 0
|
||||
this.outputs.push(
|
||||
...this.subgraph.outputNode.slots.map(
|
||||
(slot) =>
|
||||
new NodeOutputSlot(
|
||||
{
|
||||
name: slot.name,
|
||||
localized_name: slot.localized_name,
|
||||
label: slot.label,
|
||||
type: slot.type,
|
||||
links: null
|
||||
},
|
||||
this
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
super.configure(info)
|
||||
this._applyPromotedWidgetValues(info.widgets_values)
|
||||
}
|
||||
|
||||
private _applyPromotedWidgetValues(
|
||||
widgetValues: ExportedSubgraphInstance['widgets_values']
|
||||
): void {
|
||||
const quarantineValuesByInputName = this._readQuarantineHostValuesByName()
|
||||
|
||||
let valueIndex = 0
|
||||
for (const input of this.inputs) {
|
||||
if (!input.widgetId) continue
|
||||
const value =
|
||||
quarantineValuesByInputName.get(input.name) ??
|
||||
widgetValues?.[valueIndex]
|
||||
if (value !== undefined) {
|
||||
useWidgetValueStore().setValue(input.widgetId, value)
|
||||
}
|
||||
valueIndex += 1
|
||||
}
|
||||
}
|
||||
|
||||
private _readQuarantineHostValuesByName(): Map<string, TWidgetValue> {
|
||||
return new Map(
|
||||
parseProxyWidgetErrorQuarantine(
|
||||
this.properties.proxyWidgetErrorQuarantine
|
||||
)
|
||||
.toReversed()
|
||||
.flatMap(({ originalEntry: [sourceNodeId, name], hostValue }) =>
|
||||
sourceNodeId === '-1' &&
|
||||
hostValue !== undefined &&
|
||||
isWidgetValue(hostValue)
|
||||
? [[name, hostValue] as const]
|
||||
: []
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
override _internalConfigureAfterSlots() {
|
||||
this._rebindInputSubgraphSlots()
|
||||
|
||||
this.inputs = this.inputs.filter((input) => input._subgraphSlot)
|
||||
this.invalidatePromotedViews()
|
||||
|
||||
this._hydratePreviewExposures()
|
||||
|
||||
for (const input of this.inputs) {
|
||||
const subgraphInput = input._subgraphSlot
|
||||
if (!subgraphInput) {
|
||||
console.warn(
|
||||
`[SubgraphNode.configure] No subgraph input found for input ${input.name}, skipping`
|
||||
)
|
||||
continue
|
||||
}
|
||||
|
||||
this._addSubgraphInputListeners(subgraphInput, input)
|
||||
this._resolveInputWidget(subgraphInput, input)
|
||||
}
|
||||
}
|
||||
|
||||
private _hydratePreviewExposures() {
|
||||
const store = usePreviewExposureStore()
|
||||
const rootGraphId = this.rootGraph.id
|
||||
const hostLocator = String(this.id)
|
||||
const rawProperty = this.properties.previewExposures
|
||||
const hasExplicitProperty = Array.isArray(rawProperty)
|
||||
const fromProperty = parsePreviewExposures(rawProperty)
|
||||
if (fromProperty.length) {
|
||||
store.setExposures(rootGraphId, hostLocator, fromProperty)
|
||||
return
|
||||
}
|
||||
if (hasExplicitProperty) {
|
||||
store.setExposures(rootGraphId, hostLocator, [])
|
||||
return
|
||||
}
|
||||
const legacyKey = createNodeLocatorId(null, this.id)
|
||||
const legacy = store.getExposures(rootGraphId, legacyKey)
|
||||
if (legacy.length) {
|
||||
store.setExposures(rootGraphId, hostLocator, [...legacy])
|
||||
return
|
||||
}
|
||||
store.setExposures(rootGraphId, hostLocator, [])
|
||||
}
|
||||
|
||||
rebuildInputWidgetBindings(): void {
|
||||
this.invalidatePromotedViews()
|
||||
|
||||
for (const input of this.inputs) {
|
||||
delete input.widget
|
||||
delete input.pos
|
||||
delete input.widgetId
|
||||
this._clearPromotedWidget(input)
|
||||
const subgraphInput = input._subgraphSlot
|
||||
if (!subgraphInput) continue
|
||||
this._resolveInputWidget(subgraphInput, input)
|
||||
}
|
||||
|
||||
this.invalidatePromotedViews()
|
||||
}
|
||||
|
||||
private _resolveInputWidget(
|
||||
subgraphInput: SubgraphInput,
|
||||
input: INodeInputSlot
|
||||
) {
|
||||
for (const linkId of subgraphInput.linkIds) {
|
||||
const link = this.subgraph.getLink(linkId)
|
||||
if (!link) {
|
||||
console.warn(
|
||||
`[SubgraphNode.configure] No link found for link ID ${linkId}`,
|
||||
this
|
||||
)
|
||||
continue
|
||||
}
|
||||
|
||||
const { inputNode } = link.resolve(this.subgraph)
|
||||
if (!inputNode) {
|
||||
console.warn('Failed to resolve inputNode', link, this)
|
||||
continue
|
||||
}
|
||||
|
||||
const targetInput = inputNode.inputs.find((inp) => inp.link === linkId)
|
||||
if (!targetInput) {
|
||||
console.warn('Failed to find corresponding input', link, inputNode)
|
||||
continue
|
||||
}
|
||||
|
||||
const widget = inputNode.getWidgetFromSlot(targetInput)
|
||||
if (widget) {
|
||||
this._setWidget(subgraphInput, input, widget, targetInput.widget)
|
||||
break
|
||||
}
|
||||
|
||||
// Nested promotion: the source is itself a promoted subgraph input with
|
||||
// no concrete widget. Resolve the deepest interior widget so this input
|
||||
// still receives widget state and a widgetId.
|
||||
const nested = this._resolveNestedPromotedSource(inputNode, targetInput)
|
||||
if (nested) {
|
||||
this._setWidget(subgraphInput, input, nested.widget, targetInput.widget)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private _resolveNestedPromotedSource(
|
||||
inputNode: LGraphNode,
|
||||
targetInput: INodeInputSlot
|
||||
): { node: LGraphNode; widget: IBaseWidget } | undefined {
|
||||
if (!inputNode.isSubgraphNode()) return undefined
|
||||
|
||||
const target = resolveSubgraphInputTarget(inputNode, targetInput.name)
|
||||
if (!target) return undefined
|
||||
|
||||
const resolved = resolveConcretePromotedWidget(
|
||||
inputNode,
|
||||
target.nodeId,
|
||||
target.widgetName
|
||||
)
|
||||
return resolved.status === 'resolved' ? resolved.resolved : undefined
|
||||
}
|
||||
|
||||
private _setWidget(
|
||||
subgraphInput: Readonly<SubgraphInput>,
|
||||
input: INodeInputSlot,
|
||||
interiorWidget: Readonly<IBaseWidget>,
|
||||
inputWidget: IWidgetLocator | undefined
|
||||
) {
|
||||
this.invalidatePromotedViews()
|
||||
|
||||
this._clearPromotedWidget(input)
|
||||
|
||||
input.widget ??= { name: subgraphInput.name }
|
||||
input.widget.name = subgraphInput.name
|
||||
if (inputWidget) Object.setPrototypeOf(input.widget, inputWidget)
|
||||
|
||||
const id = widgetId(this.rootGraph.id, this.id, subgraphInput.name)
|
||||
input.widgetId = id
|
||||
useWidgetValueStore().registerWidget(id, {
|
||||
type: interiorWidget.type,
|
||||
value: interiorWidget.value,
|
||||
options: cloneDeep(interiorWidget.options ?? {}),
|
||||
label: input.label ?? subgraphInput.name,
|
||||
serialize: interiorWidget.serialize,
|
||||
disabled: interiorWidget.disabled,
|
||||
isDOMWidget:
|
||||
'isDOMWidget' in interiorWidget &&
|
||||
typeof interiorWidget.isDOMWidget === 'boolean'
|
||||
? interiorWidget.isDOMWidget
|
||||
: undefined
|
||||
})
|
||||
input._widget =
|
||||
this.createPromotedHostWidget(input, id, interiorWidget) ??
|
||||
this._projectPromotedWidget(input)
|
||||
this._setConcreteSlots()
|
||||
|
||||
this.subgraph.events.dispatch('widget-promoted', {
|
||||
widget: interiorWidget,
|
||||
subgraphNode: this
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* App-layer hook to build a promoted DOM widget; the default falls back to the
|
||||
* store-backed projection. Litegraph core stays free of Vue/Pinia/DOM.
|
||||
*/
|
||||
protected createPromotedHostWidget(
|
||||
_input: INodeInputSlot,
|
||||
_id: WidgetId,
|
||||
_sourceWidget: Readonly<IBaseWidget>
|
||||
): IBaseWidget | undefined {
|
||||
return undefined
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs the host widget's `onRemove` (unregistering DOM widgets) and clears it.
|
||||
* Unlike {@link ensureWidgetRemoved}, dispatches no demotion event, so it is
|
||||
* safe on re-resolution.
|
||||
*/
|
||||
private _clearPromotedWidget(input: INodeInputSlot): void {
|
||||
input._widget?.onRemove?.()
|
||||
input._widget = undefined
|
||||
}
|
||||
|
||||
override onAdded(_graph: LGraph): void {
|
||||
this.invalidatePromotedViews()
|
||||
}
|
||||
|
||||
/**
|
||||
* @param name The name of the input slot.
|
||||
* @param type The type of the input slot.
|
||||
* @param inputProperties Properties that are directly assigned to the created input. Default: a new, empty object.
|
||||
* @returns The new input slot.
|
||||
*/
|
||||
override addInput<TInput extends Partial<ISubgraphInput>>(
|
||||
name: string,
|
||||
type: ISlotType,
|
||||
inputProperties: TInput = {} as TInput
|
||||
): INodeInputSlot & TInput {
|
||||
return super.addInput(name, type, inputProperties)
|
||||
}
|
||||
|
||||
override getSlotFromWidget(
|
||||
widget: IBaseWidget | undefined
|
||||
): INodeInputSlot | undefined {
|
||||
if (widget?.widgetId) {
|
||||
const promotedInput = this.inputs.find(
|
||||
(input) => input.widgetId === widget.widgetId
|
||||
)
|
||||
if (promotedInput) return promotedInput
|
||||
}
|
||||
return super.getSlotFromWidget(widget)
|
||||
}
|
||||
|
||||
override getWidgetFromSlot(slot: INodeInputSlot): IBaseWidget | undefined {
|
||||
if (slot._widget) return slot._widget
|
||||
return super.getWidgetFromSlot(slot)
|
||||
}
|
||||
|
||||
override getInputLink(slot: number): LLink | null {
|
||||
const innerLink = this.subgraph.outputNode.slots[slot].getLinks().at(0)
|
||||
if (!innerLink) {
|
||||
console.warn(
|
||||
`SubgraphNode.getInputLink: no inner link found for slot ${slot}`
|
||||
)
|
||||
return null
|
||||
}
|
||||
|
||||
const newLink = LLink.create(innerLink)
|
||||
newLink.origin_id = toNodeId(`${this.id}:${innerLink.origin_id}`)
|
||||
newLink.origin_slot = innerLink.origin_slot
|
||||
|
||||
return newLink
|
||||
}
|
||||
|
||||
/**
|
||||
* @param slot The slot index
|
||||
* @returns The resolved connections, or undefined if no input node is found.
|
||||
*/
|
||||
resolveSubgraphInputLinks(slot: number): ResolvedConnection[] {
|
||||
const inputSlot = this.subgraph.inputNode.slots[slot]
|
||||
const innerLinks = inputSlot.getLinks()
|
||||
if (innerLinks.length === 0) {
|
||||
console.warn(
|
||||
`[SubgraphNode.resolveSubgraphInputLinks] No inner links found for input slot [${slot}] ${inputSlot.name}`,
|
||||
this
|
||||
)
|
||||
return []
|
||||
}
|
||||
return innerLinks.map((link) => link.resolve(this.subgraph))
|
||||
}
|
||||
|
||||
/**
|
||||
* @param slot The slot index
|
||||
* @returns The output node if found, otherwise undefined.
|
||||
*/
|
||||
resolveSubgraphOutputLink(slot: number): ResolvedConnection | undefined {
|
||||
const outputSlot = this.subgraph.outputNode.slots[slot]
|
||||
const innerLink = outputSlot.getLinks().at(0)
|
||||
if (innerLink) {
|
||||
return innerLink.resolve(this.subgraph)
|
||||
}
|
||||
console.warn(
|
||||
`[SubgraphNode.resolveSubgraphOutputLink] No inner link found for output slot [${slot}] ${outputSlot.name}`,
|
||||
this
|
||||
)
|
||||
}
|
||||
|
||||
override getInnerNodes(
|
||||
executableNodes: Map<ExecutionId, ExecutableLGraphNode>,
|
||||
subgraphNodePath: readonly SerializedNodeId[] = [],
|
||||
nodes: ExecutableLGraphNode[] = [],
|
||||
visited = new Set<SubgraphNode>()
|
||||
): ExecutableLGraphNode[] {
|
||||
if (visited.has(this)) {
|
||||
const nodeInfo = `${this.id}${this.title ? ` (${this.title})` : ''}`
|
||||
const subgraphInfo = `'${this.subgraph.name || 'Unnamed Subgraph'}'`
|
||||
const depth = subgraphNodePath.length
|
||||
throw new RecursionError(
|
||||
`Circular reference detected at depth ${depth} in node ${nodeInfo} of subgraph ${subgraphInfo}. ` +
|
||||
`This creates an infinite loop in the subgraph hierarchy.`
|
||||
)
|
||||
}
|
||||
visited.add(this)
|
||||
|
||||
const subgraphInstanceIdPath = [...subgraphNodePath, this.id]
|
||||
|
||||
const parentSubgraphNode = this.rootGraph
|
||||
.resolveSubgraphIdPath(subgraphNodePath.map(toNodeId))
|
||||
.at(-1)
|
||||
const subgraphNodeDto = new ExecutableNodeDTO(
|
||||
this,
|
||||
subgraphNodePath,
|
||||
executableNodes,
|
||||
parentSubgraphNode
|
||||
)
|
||||
executableNodes.set(subgraphNodeDto.id, subgraphNodeDto)
|
||||
|
||||
for (const node of this.subgraph.nodes) {
|
||||
if ('getInnerNodes' in node && node.getInnerNodes) {
|
||||
node.getInnerNodes(
|
||||
executableNodes,
|
||||
subgraphInstanceIdPath,
|
||||
nodes,
|
||||
new Set(visited)
|
||||
)
|
||||
} else {
|
||||
const aVeryRealNode = new ExecutableNodeDTO(
|
||||
node,
|
||||
subgraphInstanceIdPath,
|
||||
executableNodes,
|
||||
this
|
||||
)
|
||||
executableNodes.set(aVeryRealNode.id, aVeryRealNode)
|
||||
nodes.push(aVeryRealNode)
|
||||
}
|
||||
}
|
||||
return nodes
|
||||
}
|
||||
|
||||
override removeWidget(widget: IBaseWidget): void {
|
||||
this.ensureWidgetRemoved(widget)
|
||||
}
|
||||
|
||||
override ensureWidgetRemoved(widget: IBaseWidget): void {
|
||||
widget.onRemove?.()
|
||||
this.subgraph.events.dispatch('widget-demoted', {
|
||||
widget,
|
||||
subgraphNode: this
|
||||
})
|
||||
}
|
||||
|
||||
override onRemoved(): void {
|
||||
this._eventAbortController.abort()
|
||||
|
||||
for (const input of this.inputs) {
|
||||
if (
|
||||
input._listenerController &&
|
||||
typeof input._listenerController.abort === 'function'
|
||||
) {
|
||||
input._listenerController.abort()
|
||||
}
|
||||
this._clearPromotedWidget(input)
|
||||
}
|
||||
}
|
||||
override drawTitleBox(
|
||||
ctx: CanvasRenderingContext2D,
|
||||
{
|
||||
scale,
|
||||
low_quality = false,
|
||||
title_height = LiteGraph.NODE_TITLE_HEIGHT,
|
||||
box_size = 10
|
||||
}: DrawTitleBoxOptions
|
||||
): void {
|
||||
if (this.onDrawTitleBox) {
|
||||
this.onDrawTitleBox(ctx, title_height, this.renderingSize, scale)
|
||||
return
|
||||
}
|
||||
ctx.save()
|
||||
ctx.fillStyle = '#3b82f6'
|
||||
ctx.beginPath()
|
||||
ctx.roundRect(6, -24.5, 22, 20, 5)
|
||||
ctx.fill()
|
||||
if (!low_quality) {
|
||||
ctx.translate(25, 23)
|
||||
ctx.scale(-1.5, 1.5)
|
||||
ctx.drawImage(
|
||||
workflowBitmapCache.get(),
|
||||
0,
|
||||
-title_height,
|
||||
box_size,
|
||||
box_size
|
||||
)
|
||||
}
|
||||
ctx.restore()
|
||||
}
|
||||
|
||||
override serialize(): ISerialisedNode {
|
||||
const serialized = super.serialize()
|
||||
const serializedProperties = { ...(serialized.properties ?? {}) }
|
||||
const rootGraphId = this.rootGraph.id
|
||||
const hostLocator = String(this.id)
|
||||
|
||||
const previewExposures = usePreviewExposureStore().getExposures(
|
||||
rootGraphId,
|
||||
hostLocator
|
||||
)
|
||||
serializedProperties.previewExposures = previewExposures.map((entry) => ({
|
||||
...entry
|
||||
}))
|
||||
|
||||
const quarantine = parseProxyWidgetErrorQuarantine(
|
||||
this.properties.proxyWidgetErrorQuarantine
|
||||
)
|
||||
if (quarantine.length === 0) {
|
||||
delete serializedProperties.proxyWidgetErrorQuarantine
|
||||
} else {
|
||||
serializedProperties.proxyWidgetErrorQuarantine = quarantine.map(
|
||||
(entry) => ({ ...entry })
|
||||
)
|
||||
}
|
||||
|
||||
serialized.properties = serializedProperties
|
||||
|
||||
const widgetValues = this.inputs.flatMap((input) => {
|
||||
if (!input.widgetId) return []
|
||||
const value = useWidgetValueStore().getWidget(input.widgetId)?.value
|
||||
return [isWidgetValue(value) ? value : undefined]
|
||||
})
|
||||
|
||||
if (widgetValues.some((value) => value !== undefined)) {
|
||||
serialized.widgets_values = widgetValues
|
||||
} else {
|
||||
delete serialized.widgets_values
|
||||
}
|
||||
|
||||
return serialized
|
||||
}
|
||||
|
||||
override clone() {
|
||||
const clone = super.clone()
|
||||
|
||||
// TODO: Consider deep cloning subgraphs here.
|
||||
|
||||
return clone
|
||||
}
|
||||
getSlotShape(slot: SubgraphInput, extraInput?: INodeInputSlot) {
|
||||
const shapes = slot.linkIds.map(
|
||||
(id) => this.subgraph.links[id]?.resolve(this.subgraph)?.input?.shape
|
||||
)
|
||||
if (extraInput) shapes.push(extraInput.shape)
|
||||
return shapes.every((shape) => shape === shapes[0]) ? shapes[0] : undefined
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,160 @@
|
||||
import type {
|
||||
ExecutableLGraphNode,
|
||||
ExecutionId,
|
||||
LGraph
|
||||
} from '@/lib/litegraph/src/litegraph'
|
||||
import {
|
||||
ExecutableNodeDTO,
|
||||
LGraphEventMode
|
||||
} from '@/lib/litegraph/src/litegraph'
|
||||
import type {
|
||||
ComfyApiWorkflow,
|
||||
ComfyWorkflowJSON
|
||||
} from '@/platform/workflow/validation/schemas/workflowSchema'
|
||||
|
||||
import { compressWidgetInputSlots } from './litegraphUtil'
|
||||
|
||||
/**
|
||||
* Converts the current graph workflow for sending to the API.
|
||||
* @note Node widgets are updated before serialization to prepare queueing.
|
||||
*
|
||||
* @param graph The graph to convert.
|
||||
* @param options The options for the conversion.
|
||||
* - `sortNodes`: Whether to sort the nodes by execution order.
|
||||
* @returns The workflow and node links
|
||||
*/
|
||||
export const graphToPrompt = async (
|
||||
graph: LGraph,
|
||||
options: { sortNodes?: boolean } = {}
|
||||
): Promise<{ workflow: ComfyWorkflowJSON; output: ComfyApiWorkflow }> => {
|
||||
const { sortNodes = false } = options
|
||||
|
||||
for (const node of graph.computeExecutionOrder(false)) {
|
||||
const innerNodes = node.getInnerNodes
|
||||
? node.getInnerNodes(new Map())
|
||||
: [node]
|
||||
for (const innerNode of innerNodes) {
|
||||
if (innerNode.isVirtualNode) {
|
||||
innerNode.applyToGraph?.()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const workflow = graph.serialize({ sortNodes })
|
||||
|
||||
// Remove localized_name from the workflow
|
||||
for (const node of workflow.nodes) {
|
||||
for (const slot of node.inputs ?? []) {
|
||||
delete slot.localized_name
|
||||
}
|
||||
for (const slot of node.outputs ?? []) {
|
||||
delete slot.localized_name
|
||||
}
|
||||
}
|
||||
|
||||
compressWidgetInputSlots(workflow)
|
||||
workflow.extra ??= {}
|
||||
workflow.extra.frontendVersion = __COMFYUI_FRONTEND_VERSION__
|
||||
|
||||
const nodeDtoMap = new Map<ExecutionId, ExecutableLGraphNode>()
|
||||
for (const node of graph.computeExecutionOrder(false)) {
|
||||
const dto: ExecutableLGraphNode = new ExecutableNodeDTO(
|
||||
node,
|
||||
[],
|
||||
nodeDtoMap
|
||||
)
|
||||
|
||||
nodeDtoMap.set(dto.id, dto)
|
||||
|
||||
if (
|
||||
node.mode === LGraphEventMode.NEVER ||
|
||||
node.mode === LGraphEventMode.BYPASS
|
||||
) {
|
||||
continue
|
||||
}
|
||||
|
||||
for (const innerNode of dto.getInnerNodes()) {
|
||||
nodeDtoMap.set(innerNode.id, innerNode)
|
||||
}
|
||||
}
|
||||
|
||||
const output: ComfyApiWorkflow = {}
|
||||
// Process nodes in order of execution
|
||||
for (const node of nodeDtoMap.values()) {
|
||||
// Don't serialize muted nodes
|
||||
if (
|
||||
node.isVirtualNode ||
|
||||
node.mode === LGraphEventMode.NEVER ||
|
||||
node.mode === LGraphEventMode.BYPASS
|
||||
) {
|
||||
continue
|
||||
}
|
||||
|
||||
const inputs: ComfyApiWorkflow[string]['inputs'] = {}
|
||||
const { widgets } = node
|
||||
|
||||
// Store all widget values in the API prompt.
|
||||
// Note: widget.options.serialize controls prompt inclusion (checked here).
|
||||
// widget.serialize controls workflow persistence (checked by LGraphNode).
|
||||
if (widgets) {
|
||||
for (const [i, widget] of widgets.entries()) {
|
||||
if (!widget.name || widget.options?.serialize === false) continue
|
||||
|
||||
const widgetValue = widget.serializeValue
|
||||
? await widget.serializeValue(node, i)
|
||||
: widget.value
|
||||
// By default, Array values are reserved to represent node connections.
|
||||
// We need to wrap the array as an object to avoid the misinterpretation
|
||||
// of the array as a node connection.
|
||||
// The backend automatically unwraps the object to an array during
|
||||
// execution.
|
||||
inputs[widget.name] = Array.isArray(widgetValue)
|
||||
? widget.type === 'curve'
|
||||
? { __type__: 'CURVE', __value__: widgetValue }
|
||||
: { __value__: widgetValue }
|
||||
: widgetValue
|
||||
}
|
||||
}
|
||||
|
||||
// Store all node links
|
||||
for (const [i, input] of node.inputs.entries()) {
|
||||
const resolvedInput = node.resolveInput(i)
|
||||
if (!resolvedInput) continue
|
||||
|
||||
// Resolved to an actual widget value rather than a node connection
|
||||
if (resolvedInput.widgetInfo) {
|
||||
const { value } = resolvedInput.widgetInfo
|
||||
inputs[input.name] = Array.isArray(value) ? { __value__: value } : value
|
||||
continue
|
||||
}
|
||||
|
||||
inputs[input.name] = [
|
||||
String(resolvedInput.origin_id),
|
||||
// @ts-expect-error link.origin_slot is already number.
|
||||
parseInt(resolvedInput.origin_slot)
|
||||
]
|
||||
}
|
||||
|
||||
output[String(node.id)] = {
|
||||
inputs,
|
||||
// TODO(huchenlei): Filter out all nodes that cannot be mapped to a
|
||||
// comfyClass.
|
||||
class_type: node.comfyClass!,
|
||||
// Ignored by the backend.
|
||||
_meta: {
|
||||
title: node.title
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Remove inputs connected to removed nodes
|
||||
for (const { inputs } of Object.values(output)) {
|
||||
for (const [i, input] of Object.entries(inputs)) {
|
||||
if (Array.isArray(input) && input.length === 2 && !output[input[0]]) {
|
||||
delete inputs[i]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return { workflow: workflow as ComfyWorkflowJSON, output }
|
||||
}
|
||||
@@ -0,0 +1,674 @@
|
||||
GNU GENERAL PUBLIC LICENSE
|
||||
Version 3, 29 June 2007
|
||||
|
||||
Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
|
||||
Everyone is permitted to copy and distribute verbatim copies
|
||||
of this license document, but changing it is not allowed.
|
||||
|
||||
Preamble
|
||||
|
||||
The GNU General Public License is a free, copyleft license for
|
||||
software and other kinds of works.
|
||||
|
||||
The licenses for most software and other practical works are designed
|
||||
to take away your freedom to share and change the works. By contrast,
|
||||
the GNU General Public License is intended to guarantee your freedom to
|
||||
share and change all versions of a program--to make sure it remains free
|
||||
software for all its users. We, the Free Software Foundation, use the
|
||||
GNU General Public License for most of our software; it applies also to
|
||||
any other work released this way by its authors. You can apply it to
|
||||
your programs, too.
|
||||
|
||||
When we speak of free software, we are referring to freedom, not
|
||||
price. Our General Public Licenses are designed to make sure that you
|
||||
have the freedom to distribute copies of free software (and charge for
|
||||
them if you wish), that you receive source code or can get it if you
|
||||
want it, that you can change the software or use pieces of it in new
|
||||
free programs, and that you know you can do these things.
|
||||
|
||||
To protect your rights, we need to prevent others from denying you
|
||||
these rights or asking you to surrender the rights. Therefore, you have
|
||||
certain responsibilities if you distribute copies of the software, or if
|
||||
you modify it: responsibilities to respect the freedom of others.
|
||||
|
||||
For example, if you distribute copies of such a program, whether
|
||||
gratis or for a fee, you must pass on to the recipients the same
|
||||
freedoms that you received. You must make sure that they, too, receive
|
||||
or can get the source code. And you must show them these terms so they
|
||||
know their rights.
|
||||
|
||||
Developers that use the GNU GPL protect your rights with two steps:
|
||||
(1) assert copyright on the software, and (2) offer you this License
|
||||
giving you legal permission to copy, distribute and/or modify it.
|
||||
|
||||
For the developers' and authors' protection, the GPL clearly explains
|
||||
that there is no warranty for this free software. For both users' and
|
||||
authors' sake, the GPL requires that modified versions be marked as
|
||||
changed, so that their problems will not be attributed erroneously to
|
||||
authors of previous versions.
|
||||
|
||||
Some devices are designed to deny users access to install or run
|
||||
modified versions of the software inside them, although the manufacturer
|
||||
can do so. This is fundamentally incompatible with the aim of
|
||||
protecting users' freedom to change the software. The systematic
|
||||
pattern of such abuse occurs in the area of products for individuals to
|
||||
use, which is precisely where it is most unacceptable. Therefore, we
|
||||
have designed this version of the GPL to prohibit the practice for those
|
||||
products. If such problems arise substantially in other domains, we
|
||||
stand ready to extend this provision to those domains in future versions
|
||||
of the GPL, as needed to protect the freedom of users.
|
||||
|
||||
Finally, every program is threatened constantly by software patents.
|
||||
States should not allow patents to restrict development and use of
|
||||
software on general-purpose computers, but in those that do, we wish to
|
||||
avoid the special danger that patents applied to a free program could
|
||||
make it effectively proprietary. To prevent this, the GPL assures that
|
||||
patents cannot be used to render the program non-free.
|
||||
|
||||
The precise terms and conditions for copying, distribution and
|
||||
modification follow.
|
||||
|
||||
TERMS AND CONDITIONS
|
||||
|
||||
0. Definitions.
|
||||
|
||||
"This License" refers to version 3 of the GNU General Public License.
|
||||
|
||||
"Copyright" also means copyright-like laws that apply to other kinds of
|
||||
works, such as semiconductor masks.
|
||||
|
||||
"The Program" refers to any copyrightable work licensed under this
|
||||
License. Each licensee is addressed as "you". "Licensees" and
|
||||
"recipients" may be individuals or organizations.
|
||||
|
||||
To "modify" a work means to copy from or adapt all or part of the work
|
||||
in a fashion requiring copyright permission, other than the making of an
|
||||
exact copy. The resulting work is called a "modified version" of the
|
||||
earlier work or a work "based on" the earlier work.
|
||||
|
||||
A "covered work" means either the unmodified Program or a work based
|
||||
on the Program.
|
||||
|
||||
To "propagate" a work means to do anything with it that, without
|
||||
permission, would make you directly or secondarily liable for
|
||||
infringement under applicable copyright law, except executing it on a
|
||||
computer or modifying a private copy. Propagation includes copying,
|
||||
distribution (with or without modification), making available to the
|
||||
public, and in some countries other activities as well.
|
||||
|
||||
To "convey" a work means any kind of propagation that enables other
|
||||
parties to make or receive copies. Mere interaction with a user through
|
||||
a computer network, with no transfer of a copy, is not conveying.
|
||||
|
||||
An interactive user interface displays "Appropriate Legal Notices"
|
||||
to the extent that it includes a convenient and prominently visible
|
||||
feature that (1) displays an appropriate copyright notice, and (2)
|
||||
tells the user that there is no warranty for the work (except to the
|
||||
extent that warranties are provided), that licensees may convey the
|
||||
work under this License, and how to view a copy of this License. If
|
||||
the interface presents a list of user commands or options, such as a
|
||||
menu, a prominent item in the list meets this criterion.
|
||||
|
||||
1. Source Code.
|
||||
|
||||
The "source code" for a work means the preferred form of the work
|
||||
for making modifications to it. "Object code" means any non-source
|
||||
form of a work.
|
||||
|
||||
A "Standard Interface" means an interface that either is an official
|
||||
standard defined by a recognized standards body, or, in the case of
|
||||
interfaces specified for a particular programming language, one that
|
||||
is widely used among developers working in that language.
|
||||
|
||||
The "System Libraries" of an executable work include anything, other
|
||||
than the work as a whole, that (a) is included in the normal form of
|
||||
packaging a Major Component, but which is not part of that Major
|
||||
Component, and (b) serves only to enable use of the work with that
|
||||
Major Component, or to implement a Standard Interface for which an
|
||||
implementation is available to the public in source code form. A
|
||||
"Major Component", in this context, means a major essential component
|
||||
(kernel, window system, and so on) of the specific operating system
|
||||
(if any) on which the executable work runs, or a compiler used to
|
||||
produce the work, or an object code interpreter used to run it.
|
||||
|
||||
The "Corresponding Source" for a work in object code form means all
|
||||
the source code needed to generate, install, and (for an executable
|
||||
work) run the object code and to modify the work, including scripts to
|
||||
control those activities. However, it does not include the work's
|
||||
System Libraries, or general-purpose tools or generally available free
|
||||
programs which are used unmodified in performing those activities but
|
||||
which are not part of the work. For example, Corresponding Source
|
||||
includes interface definition files associated with source files for
|
||||
the work, and the source code for shared libraries and dynamically
|
||||
linked subprograms that the work is specifically designed to require,
|
||||
such as by intimate data communication or control flow between those
|
||||
subprograms and other parts of the work.
|
||||
|
||||
The Corresponding Source need not include anything that users
|
||||
can regenerate automatically from other parts of the Corresponding
|
||||
Source.
|
||||
|
||||
The Corresponding Source for a work in source code form is that
|
||||
same work.
|
||||
|
||||
2. Basic Permissions.
|
||||
|
||||
All rights granted under this License are granted for the term of
|
||||
copyright on the Program, and are irrevocable provided the stated
|
||||
conditions are met. This License explicitly affirms your unlimited
|
||||
permission to run the unmodified Program. The output from running a
|
||||
covered work is covered by this License only if the output, given its
|
||||
content, constitutes a covered work. This License acknowledges your
|
||||
rights of fair use or other equivalent, as provided by copyright law.
|
||||
|
||||
You may make, run and propagate covered works that you do not
|
||||
convey, without conditions so long as your license otherwise remains
|
||||
in force. You may convey covered works to others for the sole purpose
|
||||
of having them make modifications exclusively for you, or provide you
|
||||
with facilities for running those works, provided that you comply with
|
||||
the terms of this License in conveying all material for which you do
|
||||
not control copyright. Those thus making or running the covered works
|
||||
for you must do so exclusively on your behalf, under your direction
|
||||
and control, on terms that prohibit them from making any copies of
|
||||
your copyrighted material outside their relationship with you.
|
||||
|
||||
Conveying under any other circumstances is permitted solely under
|
||||
the conditions stated below. Sublicensing is not allowed; section 10
|
||||
makes it unnecessary.
|
||||
|
||||
3. Protecting Users' Legal Rights From Anti-Circumvention Law.
|
||||
|
||||
No covered work shall be deemed part of an effective technological
|
||||
measure under any applicable law fulfilling obligations under article
|
||||
11 of the WIPO copyright treaty adopted on 20 December 1996, or
|
||||
similar laws prohibiting or restricting circumvention of such
|
||||
measures.
|
||||
|
||||
When you convey a covered work, you waive any legal power to forbid
|
||||
circumvention of technological measures to the extent such circumvention
|
||||
is effected by exercising rights under this License with respect to
|
||||
the covered work, and you disclaim any intention to limit operation or
|
||||
modification of the work as a means of enforcing, against the work's
|
||||
users, your or third parties' legal rights to forbid circumvention of
|
||||
technological measures.
|
||||
|
||||
4. Conveying Verbatim Copies.
|
||||
|
||||
You may convey verbatim copies of the Program's source code as you
|
||||
receive it, in any medium, provided that you conspicuously and
|
||||
appropriately publish on each copy an appropriate copyright notice;
|
||||
keep intact all notices stating that this License and any
|
||||
non-permissive terms added in accord with section 7 apply to the code;
|
||||
keep intact all notices of the absence of any warranty; and give all
|
||||
recipients a copy of this License along with the Program.
|
||||
|
||||
You may charge any price or no price for each copy that you convey,
|
||||
and you may offer support or warranty protection for a fee.
|
||||
|
||||
5. Conveying Modified Source Versions.
|
||||
|
||||
You may convey a work based on the Program, or the modifications to
|
||||
produce it from the Program, in the form of source code under the
|
||||
terms of section 4, provided that you also meet all of these conditions:
|
||||
|
||||
a) The work must carry prominent notices stating that you modified
|
||||
it, and giving a relevant date.
|
||||
|
||||
b) The work must carry prominent notices stating that it is
|
||||
released under this License and any conditions added under section
|
||||
7. This requirement modifies the requirement in section 4 to
|
||||
"keep intact all notices".
|
||||
|
||||
c) You must license the entire work, as a whole, under this
|
||||
License to anyone who comes into possession of a copy. This
|
||||
License will therefore apply, along with any applicable section 7
|
||||
additional terms, to the whole of the work, and all its parts,
|
||||
regardless of how they are packaged. This License gives no
|
||||
permission to license the work in any other way, but it does not
|
||||
invalidate such permission if you have separately received it.
|
||||
|
||||
d) If the work has interactive user interfaces, each must display
|
||||
Appropriate Legal Notices; however, if the Program has interactive
|
||||
interfaces that do not display Appropriate Legal Notices, your
|
||||
work need not make them do so.
|
||||
|
||||
A compilation of a covered work with other separate and independent
|
||||
works, which are not by their nature extensions of the covered work,
|
||||
and which are not combined with it such as to form a larger program,
|
||||
in or on a volume of a storage or distribution medium, is called an
|
||||
"aggregate" if the compilation and its resulting copyright are not
|
||||
used to limit the access or legal rights of the compilation's users
|
||||
beyond what the individual works permit. Inclusion of a covered work
|
||||
in an aggregate does not cause this License to apply to the other
|
||||
parts of the aggregate.
|
||||
|
||||
6. Conveying Non-Source Forms.
|
||||
|
||||
You may convey a covered work in object code form under the terms
|
||||
of sections 4 and 5, provided that you also convey the
|
||||
machine-readable Corresponding Source under the terms of this License,
|
||||
in one of these ways:
|
||||
|
||||
a) Convey the object code in, or embodied in, a physical product
|
||||
(including a physical distribution medium), accompanied by the
|
||||
Corresponding Source fixed on a durable physical medium
|
||||
customarily used for software interchange.
|
||||
|
||||
b) Convey the object code in, or embodied in, a physical product
|
||||
(including a physical distribution medium), accompanied by a
|
||||
written offer, valid for at least three years and valid for as
|
||||
long as you offer spare parts or customer support for that product
|
||||
model, to give anyone who possesses the object code either (1) a
|
||||
copy of the Corresponding Source for all the software in the
|
||||
product that is covered by this License, on a durable physical
|
||||
medium customarily used for software interchange, for a price no
|
||||
more than your reasonable cost of physically performing this
|
||||
conveying of source, or (2) access to copy the
|
||||
Corresponding Source from a network server at no charge.
|
||||
|
||||
c) Convey individual copies of the object code with a copy of the
|
||||
written offer to provide the Corresponding Source. This
|
||||
alternative is allowed only occasionally and noncommercially, and
|
||||
only if you received the object code with such an offer, in accord
|
||||
with subsection 6b.
|
||||
|
||||
d) Convey the object code by offering access from a designated
|
||||
place (gratis or for a charge), and offer equivalent access to the
|
||||
Corresponding Source in the same way through the same place at no
|
||||
further charge. You need not require recipients to copy the
|
||||
Corresponding Source along with the object code. If the place to
|
||||
copy the object code is a network server, the Corresponding Source
|
||||
may be on a different server (operated by you or a third party)
|
||||
that supports equivalent copying facilities, provided you maintain
|
||||
clear directions next to the object code saying where to find the
|
||||
Corresponding Source. Regardless of what server hosts the
|
||||
Corresponding Source, you remain obligated to ensure that it is
|
||||
available for as long as needed to satisfy these requirements.
|
||||
|
||||
e) Convey the object code using peer-to-peer transmission, provided
|
||||
you inform other peers where the object code and Corresponding
|
||||
Source of the work are being offered to the general public at no
|
||||
charge under subsection 6d.
|
||||
|
||||
A separable portion of the object code, whose source code is excluded
|
||||
from the Corresponding Source as a System Library, need not be
|
||||
included in conveying the object code work.
|
||||
|
||||
A "User Product" is either (1) a "consumer product", which means any
|
||||
tangible personal property which is normally used for personal, family,
|
||||
or household purposes, or (2) anything designed or sold for incorporation
|
||||
into a dwelling. In determining whether a product is a consumer product,
|
||||
doubtful cases shall be resolved in favor of coverage. For a particular
|
||||
product received by a particular user, "normally used" refers to a
|
||||
typical or common use of that class of product, regardless of the status
|
||||
of the particular user or of the way in which the particular user
|
||||
actually uses, or expects or is expected to use, the product. A product
|
||||
is a consumer product regardless of whether the product has substantial
|
||||
commercial, industrial or non-consumer uses, unless such uses represent
|
||||
the only significant mode of use of the product.
|
||||
|
||||
"Installation Information" for a User Product means any methods,
|
||||
procedures, authorization keys, or other information required to install
|
||||
and execute modified versions of a covered work in that User Product from
|
||||
a modified version of its Corresponding Source. The information must
|
||||
suffice to ensure that the continued functioning of the modified object
|
||||
code is in no case prevented or interfered with solely because
|
||||
modification has been made.
|
||||
|
||||
If you convey an object code work under this section in, or with, or
|
||||
specifically for use in, a User Product, and the conveying occurs as
|
||||
part of a transaction in which the right of possession and use of the
|
||||
User Product is transferred to the recipient in perpetuity or for a
|
||||
fixed term (regardless of how the transaction is characterized), the
|
||||
Corresponding Source conveyed under this section must be accompanied
|
||||
by the Installation Information. But this requirement does not apply
|
||||
if neither you nor any third party retains the ability to install
|
||||
modified object code on the User Product (for example, the work has
|
||||
been installed in ROM).
|
||||
|
||||
The requirement to provide Installation Information does not include a
|
||||
requirement to continue to provide support service, warranty, or updates
|
||||
for a work that has been modified or installed by the recipient, or for
|
||||
the User Product in which it has been modified or installed. Access to a
|
||||
network may be denied when the modification itself materially and
|
||||
adversely affects the operation of the network or violates the rules and
|
||||
protocols for communication across the network.
|
||||
|
||||
Corresponding Source conveyed, and Installation Information provided,
|
||||
in accord with this section must be in a format that is publicly
|
||||
documented (and with an implementation available to the public in
|
||||
source code form), and must require no special password or key for
|
||||
unpacking, reading or copying.
|
||||
|
||||
7. Additional Terms.
|
||||
|
||||
"Additional permissions" are terms that supplement the terms of this
|
||||
License by making exceptions from one or more of its conditions.
|
||||
Additional permissions that are applicable to the entire Program shall
|
||||
be treated as though they were included in this License, to the extent
|
||||
that they are valid under applicable law. If additional permissions
|
||||
apply only to part of the Program, that part may be used separately
|
||||
under those permissions, but the entire Program remains governed by
|
||||
this License without regard to the additional permissions.
|
||||
|
||||
When you convey a copy of a covered work, you may at your option
|
||||
remove any additional permissions from that copy, or from any part of
|
||||
it. (Additional permissions may be written to require their own
|
||||
removal in certain cases when you modify the work.) You may place
|
||||
additional permissions on material, added by you to a covered work,
|
||||
for which you have or can give appropriate copyright permission.
|
||||
|
||||
Notwithstanding any other provision of this License, for material you
|
||||
add to a covered work, you may (if authorized by the copyright holders of
|
||||
that material) supplement the terms of this License with terms:
|
||||
|
||||
a) Disclaiming warranty or limiting liability differently from the
|
||||
terms of sections 15 and 16 of this License; or
|
||||
|
||||
b) Requiring preservation of specified reasonable legal notices or
|
||||
author attributions in that material or in the Appropriate Legal
|
||||
Notices displayed by works containing it; or
|
||||
|
||||
c) Prohibiting misrepresentation of the origin of that material, or
|
||||
requiring that modified versions of such material be marked in
|
||||
reasonable ways as different from the original version; or
|
||||
|
||||
d) Limiting the use for publicity purposes of names of licensors or
|
||||
authors of the material; or
|
||||
|
||||
e) Declining to grant rights under trademark law for use of some
|
||||
trade names, trademarks, or service marks; or
|
||||
|
||||
f) Requiring indemnification of licensors and authors of that
|
||||
material by anyone who conveys the material (or modified versions of
|
||||
it) with contractual assumptions of liability to the recipient, for
|
||||
any liability that these contractual assumptions directly impose on
|
||||
those licensors and authors.
|
||||
|
||||
All other non-permissive additional terms are considered "further
|
||||
restrictions" within the meaning of section 10. If the Program as you
|
||||
received it, or any part of it, contains a notice stating that it is
|
||||
governed by this License along with a term that is a further
|
||||
restriction, you may remove that term. If a license document contains
|
||||
a further restriction but permits relicensing or conveying under this
|
||||
License, you may add to a covered work material governed by the terms
|
||||
of that license document, provided that the further restriction does
|
||||
not survive such relicensing or conveying.
|
||||
|
||||
If you add terms to a covered work in accord with this section, you
|
||||
must place, in the relevant source files, a statement of the
|
||||
additional terms that apply to those files, or a notice indicating
|
||||
where to find the applicable terms.
|
||||
|
||||
Additional terms, permissive or non-permissive, may be stated in the
|
||||
form of a separately written license, or stated as exceptions;
|
||||
the above requirements apply either way.
|
||||
|
||||
8. Termination.
|
||||
|
||||
You may not propagate or modify a covered work except as expressly
|
||||
provided under this License. Any attempt otherwise to propagate or
|
||||
modify it is void, and will automatically terminate your rights under
|
||||
this License (including any patent licenses granted under the third
|
||||
paragraph of section 11).
|
||||
|
||||
However, if you cease all violation of this License, then your
|
||||
license from a particular copyright holder is reinstated (a)
|
||||
provisionally, unless and until the copyright holder explicitly and
|
||||
finally terminates your license, and (b) permanently, if the copyright
|
||||
holder fails to notify you of the violation by some reasonable means
|
||||
prior to 60 days after the cessation.
|
||||
|
||||
Moreover, your license from a particular copyright holder is
|
||||
reinstated permanently if the copyright holder notifies you of the
|
||||
violation by some reasonable means, this is the first time you have
|
||||
received notice of violation of this License (for any work) from that
|
||||
copyright holder, and you cure the violation prior to 30 days after
|
||||
your receipt of the notice.
|
||||
|
||||
Termination of your rights under this section does not terminate the
|
||||
licenses of parties who have received copies or rights from you under
|
||||
this License. If your rights have been terminated and not permanently
|
||||
reinstated, you do not qualify to receive new licenses for the same
|
||||
material under section 10.
|
||||
|
||||
9. Acceptance Not Required for Having Copies.
|
||||
|
||||
You are not required to accept this License in order to receive or
|
||||
run a copy of the Program. Ancillary propagation of a covered work
|
||||
occurring solely as a consequence of using peer-to-peer transmission
|
||||
to receive a copy likewise does not require acceptance. However,
|
||||
nothing other than this License grants you permission to propagate or
|
||||
modify any covered work. These actions infringe copyright if you do
|
||||
not accept this License. Therefore, by modifying or propagating a
|
||||
covered work, you indicate your acceptance of this License to do so.
|
||||
|
||||
10. Automatic Licensing of Downstream Recipients.
|
||||
|
||||
Each time you convey a covered work, the recipient automatically
|
||||
receives a license from the original licensors, to run, modify and
|
||||
propagate that work, subject to this License. You are not responsible
|
||||
for enforcing compliance by third parties with this License.
|
||||
|
||||
An "entity transaction" is a transaction transferring control of an
|
||||
organization, or substantially all assets of one, or subdividing an
|
||||
organization, or merging organizations. If propagation of a covered
|
||||
work results from an entity transaction, each party to that
|
||||
transaction who receives a copy of the work also receives whatever
|
||||
licenses to the work the party's predecessor in interest had or could
|
||||
give under the previous paragraph, plus a right to possession of the
|
||||
Corresponding Source of the work from the predecessor in interest, if
|
||||
the predecessor has it or can get it with reasonable efforts.
|
||||
|
||||
You may not impose any further restrictions on the exercise of the
|
||||
rights granted or affirmed under this License. For example, you may
|
||||
not impose a license fee, royalty, or other charge for exercise of
|
||||
rights granted under this License, and you may not initiate litigation
|
||||
(including a cross-claim or counterclaim in a lawsuit) alleging that
|
||||
any patent claim is infringed by making, using, selling, offering for
|
||||
sale, or importing the Program or any portion of it.
|
||||
|
||||
11. Patents.
|
||||
|
||||
A "contributor" is a copyright holder who authorizes use under this
|
||||
License of the Program or a work on which the Program is based. The
|
||||
work thus licensed is called the contributor's "contributor version".
|
||||
|
||||
A contributor's "essential patent claims" are all patent claims
|
||||
owned or controlled by the contributor, whether already acquired or
|
||||
hereafter acquired, that would be infringed by some manner, permitted
|
||||
by this License, of making, using, or selling its contributor version,
|
||||
but do not include claims that would be infringed only as a
|
||||
consequence of further modification of the contributor version. For
|
||||
purposes of this definition, "control" includes the right to grant
|
||||
patent sublicenses in a manner consistent with the requirements of
|
||||
this License.
|
||||
|
||||
Each contributor grants you a non-exclusive, worldwide, royalty-free
|
||||
patent license under the contributor's essential patent claims, to
|
||||
make, use, sell, offer for sale, import and otherwise run, modify and
|
||||
propagate the contents of its contributor version.
|
||||
|
||||
In the following three paragraphs, a "patent license" is any express
|
||||
agreement or commitment, however denominated, not to enforce a patent
|
||||
(such as an express permission to practice a patent or covenant not to
|
||||
sue for patent infringement). To "grant" such a patent license to a
|
||||
party means to make such an agreement or commitment not to enforce a
|
||||
patent against the party.
|
||||
|
||||
If you convey a covered work, knowingly relying on a patent license,
|
||||
and the Corresponding Source of the work is not available for anyone
|
||||
to copy, free of charge and under the terms of this License, through a
|
||||
publicly available network server or other readily accessible means,
|
||||
then you must either (1) cause the Corresponding Source to be so
|
||||
available, or (2) arrange to deprive yourself of the benefit of the
|
||||
patent license for this particular work, or (3) arrange, in a manner
|
||||
consistent with the requirements of this License, to extend the patent
|
||||
license to downstream recipients. "Knowingly relying" means you have
|
||||
actual knowledge that, but for the patent license, your conveying the
|
||||
covered work in a country, or your recipient's use of the covered work
|
||||
in a country, would infringe one or more identifiable patents in that
|
||||
country that you have reason to believe are valid.
|
||||
|
||||
If, pursuant to or in connection with a single transaction or
|
||||
arrangement, you convey, or propagate by procuring conveyance of, a
|
||||
covered work, and grant a patent license to some of the parties
|
||||
receiving the covered work authorizing them to use, propagate, modify
|
||||
or convey a specific copy of the covered work, then the patent license
|
||||
you grant is automatically extended to all recipients of the covered
|
||||
work and works based on it.
|
||||
|
||||
A patent license is "discriminatory" if it does not include within
|
||||
the scope of its coverage, prohibits the exercise of, or is
|
||||
conditioned on the non-exercise of one or more of the rights that are
|
||||
specifically granted under this License. You may not convey a covered
|
||||
work if you are a party to an arrangement with a third party that is
|
||||
in the business of distributing software, under which you make payment
|
||||
to the third party based on the extent of your activity of conveying
|
||||
the work, and under which the third party grants, to any of the
|
||||
parties who would receive the covered work from you, a discriminatory
|
||||
patent license (a) in connection with copies of the covered work
|
||||
conveyed by you (or copies made from those copies), or (b) primarily
|
||||
for and in connection with specific products or compilations that
|
||||
contain the covered work, unless you entered into that arrangement,
|
||||
or that patent license was granted, prior to 28 March 2007.
|
||||
|
||||
Nothing in this License shall be construed as excluding or limiting
|
||||
any implied license or other defenses to infringement that may
|
||||
otherwise be available to you under applicable patent law.
|
||||
|
||||
12. No Surrender of Others' Freedom.
|
||||
|
||||
If conditions are imposed on you (whether by court order, agreement or
|
||||
otherwise) that contradict the conditions of this License, they do not
|
||||
excuse you from the conditions of this License. If you cannot convey a
|
||||
covered work so as to satisfy simultaneously your obligations under this
|
||||
License and any other pertinent obligations, then as a consequence you may
|
||||
not convey it at all. For example, if you agree to terms that obligate you
|
||||
to collect a royalty for further conveying from those to whom you convey
|
||||
the Program, the only way you could satisfy both those terms and this
|
||||
License would be to refrain entirely from conveying the Program.
|
||||
|
||||
13. Use with the GNU Affero General Public License.
|
||||
|
||||
Notwithstanding any other provision of this License, you have
|
||||
permission to link or combine any covered work with a work licensed
|
||||
under version 3 of the GNU Affero General Public License into a single
|
||||
combined work, and to convey the resulting work. The terms of this
|
||||
License will continue to apply to the part which is the covered work,
|
||||
but the special requirements of the GNU Affero General Public License,
|
||||
section 13, concerning interaction through a network will apply to the
|
||||
combination as such.
|
||||
|
||||
14. Revised Versions of this License.
|
||||
|
||||
The Free Software Foundation may publish revised and/or new versions of
|
||||
the GNU General Public License from time to time. Such new versions will
|
||||
be similar in spirit to the present version, but may differ in detail to
|
||||
address new problems or concerns.
|
||||
|
||||
Each version is given a distinguishing version number. If the
|
||||
Program specifies that a certain numbered version of the GNU General
|
||||
Public License "or any later version" applies to it, you have the
|
||||
option of following the terms and conditions either of that numbered
|
||||
version or of any later version published by the Free Software
|
||||
Foundation. If the Program does not specify a version number of the
|
||||
GNU General Public License, you may choose any version ever published
|
||||
by the Free Software Foundation.
|
||||
|
||||
If the Program specifies that a proxy can decide which future
|
||||
versions of the GNU General Public License can be used, that proxy's
|
||||
public statement of acceptance of a version permanently authorizes you
|
||||
to choose that version for the Program.
|
||||
|
||||
Later license versions may give you additional or different
|
||||
permissions. However, no additional obligations are imposed on any
|
||||
author or copyright holder as a result of your choosing to follow a
|
||||
later version.
|
||||
|
||||
15. Disclaimer of Warranty.
|
||||
|
||||
THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
|
||||
APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
|
||||
HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
|
||||
OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
|
||||
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
|
||||
IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
|
||||
ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
|
||||
|
||||
16. Limitation of Liability.
|
||||
|
||||
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
|
||||
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
|
||||
THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
|
||||
GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
|
||||
USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
|
||||
DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
|
||||
PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
|
||||
EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
|
||||
SUCH DAMAGES.
|
||||
|
||||
17. Interpretation of Sections 15 and 16.
|
||||
|
||||
If the disclaimer of warranty and limitation of liability provided
|
||||
above cannot be given local legal effect according to their terms,
|
||||
reviewing courts shall apply local law that most closely approximates
|
||||
an absolute waiver of all civil liability in connection with the
|
||||
Program, unless a warranty or assumption of liability accompanies a
|
||||
copy of the Program in return for a fee.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
How to Apply These Terms to Your New Programs
|
||||
|
||||
If you develop a new program, and you want it to be of the greatest
|
||||
possible use to the public, the best way to achieve this is to make it
|
||||
free software which everyone can redistribute and change under these terms.
|
||||
|
||||
To do so, attach the following notices to the program. It is safest
|
||||
to attach them to the start of each source file to most effectively
|
||||
state the exclusion of warranty; and each file should have at least
|
||||
the "copyright" line and a pointer to where the full notice is found.
|
||||
|
||||
<one line to give the program's name and a brief idea of what it does.>
|
||||
Copyright (C) <year> <name of author>
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
Also add information on how to contact you by electronic and paper mail.
|
||||
|
||||
If the program does terminal interaction, make it output a short
|
||||
notice like this when it starts in an interactive mode:
|
||||
|
||||
<program> Copyright (C) <year> <name of author>
|
||||
This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
|
||||
This is free software, and you are welcome to redistribute it
|
||||
under certain conditions; type `show c' for details.
|
||||
|
||||
The hypothetical commands `show w' and `show c' should show the appropriate
|
||||
parts of the General Public License. Of course, your program's commands
|
||||
might be different; for a GUI interface, you would use an "about box".
|
||||
|
||||
You should also get your employer (if you work as a programmer) or school,
|
||||
if any, to sign a "copyright disclaimer" for the program, if necessary.
|
||||
For more information on this, and how to apply and follow the GNU GPL, see
|
||||
<https://www.gnu.org/licenses/>.
|
||||
|
||||
The GNU General Public License does not permit incorporating your program
|
||||
into proprietary programs. If your program is a subroutine library, you
|
||||
may consider it more useful to permit linking proprietary applications with
|
||||
the library. If this is what you want to do, use the GNU Lesser General
|
||||
Public License instead of this License. But first, please read
|
||||
<https://www.gnu.org/licenses/why-not-lgpl.html>.
|
||||
@@ -0,0 +1,617 @@
|
||||
import { useChainCallback } from '@/composables/functional/useChainCallback'
|
||||
import { LGraphNode, LiteGraph } from '@/lib/litegraph/src/litegraph'
|
||||
import type {
|
||||
INodeInputSlot,
|
||||
INodeOutputSlot,
|
||||
ISlotType,
|
||||
LLink
|
||||
} from '@/lib/litegraph/src/litegraph'
|
||||
import { NodeSlot } from '@/lib/litegraph/src/node/NodeSlot'
|
||||
import type {
|
||||
IBaseWidget,
|
||||
TWidgetValue
|
||||
} from '@/lib/litegraph/src/types/widgets'
|
||||
import { assetService } from '@/platform/assets/services/assetService'
|
||||
import { createAssetWidget } from '@/platform/assets/utils/createAssetWidget'
|
||||
import type { ComfyNodeDef, InputSpec } from '@/schemas/nodeDefSchema'
|
||||
import { app } from '@/scripts/app'
|
||||
import {
|
||||
ComfyWidgets,
|
||||
addValueControlWidgets,
|
||||
isValidWidgetType
|
||||
} from '@/scripts/widgets'
|
||||
import { isPrimitiveNode } from '@/renderer/utils/nodeTypeGuards'
|
||||
import { CONFIG, GET_CONFIG } from '@/services/litegraphService'
|
||||
import { mergeInputSpec } from '@/utils/nodeDefUtil'
|
||||
import { applyTextReplacements } from '@/utils/searchAndReplace'
|
||||
|
||||
import { applyFirstWidgetValueToGraph } from './widgetValuePropagation'
|
||||
|
||||
const replacePropertyName = 'Run widget replace on values'
|
||||
export class PrimitiveNode extends LGraphNode {
|
||||
controlValues?: TWidgetValue[]
|
||||
lastType?: string
|
||||
static override category: string
|
||||
constructor(title: string) {
|
||||
super(title)
|
||||
this.addOutput('connect to widget input', '*')
|
||||
this.serialize_widgets = true
|
||||
this.isVirtualNode = true
|
||||
|
||||
if (!this.properties || !(replacePropertyName in this.properties)) {
|
||||
this.addProperty(replacePropertyName, false, 'boolean')
|
||||
}
|
||||
}
|
||||
|
||||
override applyToGraph(extraLinks: LLink[] = []) {
|
||||
const sourceWidget = this.widgets?.[0]
|
||||
const graph = this.graph
|
||||
if (!sourceWidget || !graph) return
|
||||
|
||||
let v = sourceWidget.value
|
||||
if (v && this.properties[replacePropertyName]) {
|
||||
v = applyTextReplacements(graph, v as string)
|
||||
}
|
||||
applyFirstWidgetValueToGraph(this, extraLinks, () => v)
|
||||
}
|
||||
|
||||
override refreshComboInNode() {
|
||||
const widget = this.widgets?.[0]
|
||||
if (widget?.type === 'combo') {
|
||||
// @ts-expect-error fixme ts strict error
|
||||
widget.options.values = this.outputs[0].widget[GET_CONFIG]()[0]
|
||||
|
||||
// @ts-expect-error fixme ts strict error
|
||||
if (!widget.options.values.includes(widget.value as string)) {
|
||||
// @ts-expect-error fixme ts strict error
|
||||
widget.value = widget.options.values[0]
|
||||
widget.callback?.(widget.value)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override onAfterGraphConfigured() {
|
||||
if (this.outputs[0].links?.length && !this.widgets?.length) {
|
||||
this._onFirstConnection()
|
||||
|
||||
// Populate widget values from config data
|
||||
if (this.widgets && this.widgets_values) {
|
||||
for (let i = 0; i < this.widgets_values.length; i++) {
|
||||
const w = this.widgets[i]
|
||||
if (w) {
|
||||
w.value = this.widgets_values[i]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Merge values if required
|
||||
this._mergeWidgetConfig()
|
||||
}
|
||||
}
|
||||
|
||||
override onConnectionsChange(
|
||||
_type: ISlotType,
|
||||
_index: number,
|
||||
connected: boolean
|
||||
) {
|
||||
if (app.configuringGraph) {
|
||||
// Dont run while the graph is still setting up
|
||||
return
|
||||
}
|
||||
|
||||
const links = this.outputs[0].links
|
||||
if (connected) {
|
||||
if (links?.length && !this.widgets?.length) {
|
||||
this._onFirstConnection()
|
||||
}
|
||||
} else {
|
||||
// We may have removed a link that caused the constraints to change
|
||||
this._mergeWidgetConfig()
|
||||
|
||||
if (!links?.length) {
|
||||
this.onLastDisconnect()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override onConnectOutput(
|
||||
slot: number,
|
||||
_type: string,
|
||||
input: INodeInputSlot,
|
||||
target_node: LGraphNode,
|
||||
target_slot: number
|
||||
) {
|
||||
// Fires before the link is made allowing us to reject it if it isn't valid
|
||||
// No widget, we can't connect
|
||||
if (!input.widget && !(input.type in ComfyWidgets)) {
|
||||
return false
|
||||
}
|
||||
|
||||
if (this.outputs[slot].links?.length) {
|
||||
const valid = this._isValidConnection(input)
|
||||
if (valid) {
|
||||
// On connect of additional outputs, copy our value to their widget
|
||||
this.applyToGraph([{ target_id: target_node.id, target_slot } as LLink])
|
||||
}
|
||||
return valid
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
private _onFirstConnection(recreating?: boolean) {
|
||||
// First connection can fire before the graph is ready on initial load so random things can be missing
|
||||
if (!this.outputs[0].links || !this.graph) {
|
||||
this.onLastDisconnect()
|
||||
return
|
||||
}
|
||||
const linkId = this.outputs[0].links[0]
|
||||
const link = this.graph.links[linkId]
|
||||
if (!link) return
|
||||
|
||||
const theirNode = this.graph.getNodeById(link.target_id)
|
||||
if (!theirNode || !theirNode.inputs) return
|
||||
|
||||
const input = theirNode.inputs[link.target_slot]
|
||||
if (!input) return
|
||||
|
||||
let widget
|
||||
if (!input.widget) {
|
||||
if (!(input.type in ComfyWidgets)) return
|
||||
widget = { name: input.name, [GET_CONFIG]: () => [input.type, {}] } //fake widget
|
||||
} else {
|
||||
widget = input.widget
|
||||
}
|
||||
|
||||
// @ts-expect-error fixme ts strict error
|
||||
const config = widget[GET_CONFIG]?.()
|
||||
if (!config) return
|
||||
|
||||
const { type } = getWidgetType(config)
|
||||
// Update our output to restrict to the widget type
|
||||
this.outputs[0].type = type
|
||||
this.outputs[0].name = type
|
||||
this.outputs[0].widget = widget
|
||||
|
||||
this._createWidget(
|
||||
widget[CONFIG] ?? config,
|
||||
theirNode,
|
||||
widget.name,
|
||||
// @ts-expect-error fixme ts strict error
|
||||
recreating
|
||||
)
|
||||
}
|
||||
|
||||
private _createWidget(
|
||||
inputData: InputSpec,
|
||||
node: LGraphNode,
|
||||
widgetName: string,
|
||||
recreating: boolean
|
||||
) {
|
||||
let type = inputData[0]
|
||||
|
||||
if (type instanceof Array) {
|
||||
type = 'COMBO'
|
||||
}
|
||||
|
||||
// Store current size as addWidget resizes the node
|
||||
const [oldWidth, oldHeight] = this.size
|
||||
let widget: IBaseWidget
|
||||
|
||||
if (
|
||||
type === 'COMBO' &&
|
||||
assetService.shouldUseAssetBrowser(node.comfyClass, widgetName)
|
||||
) {
|
||||
widget = this._createAssetWidget(node, widgetName, inputData)
|
||||
const theirWidget = node.widgets?.find((w) => w.name === widgetName)
|
||||
if (theirWidget) widget.value = theirWidget.value
|
||||
this._finalizeWidget(widget, oldWidth, oldHeight, recreating)
|
||||
return
|
||||
}
|
||||
|
||||
if (isValidWidgetType(type)) {
|
||||
widget = (ComfyWidgets[type](this, 'value', inputData, app) || {}).widget
|
||||
} else {
|
||||
// @ts-expect-error InputSpec is not typed correctly
|
||||
widget = this.addWidget(type, 'value', null, () => {}, {})
|
||||
}
|
||||
|
||||
if (node?.widgets && widget) {
|
||||
const theirWidget = node.widgets.find((w) => w.name === widgetName)
|
||||
if (theirWidget) {
|
||||
widget.value = theirWidget.value
|
||||
}
|
||||
}
|
||||
|
||||
if (
|
||||
!inputData?.[1]?.control_after_generate &&
|
||||
(widget.type === 'number' || widget.type === 'combo')
|
||||
) {
|
||||
let control_value = this.widgets_values?.[1]
|
||||
if (!control_value) {
|
||||
control_value = 'fixed'
|
||||
}
|
||||
addValueControlWidgets(
|
||||
this,
|
||||
widget,
|
||||
control_value as string,
|
||||
undefined,
|
||||
inputData
|
||||
)
|
||||
if (this.widgets?.[1]) widget.linkedWidgets = [this.widgets[1]]
|
||||
|
||||
const filter = this.widgets_values?.[2]
|
||||
if (filter && this.widgets && this.widgets.length === 3) {
|
||||
this.widgets[2].value = filter
|
||||
}
|
||||
}
|
||||
|
||||
// Restore any saved control values
|
||||
const controlValues = this.controlValues
|
||||
if (
|
||||
this.widgets &&
|
||||
this.lastType === this.widgets[0]?.type &&
|
||||
controlValues?.length === this.widgets.length - 1
|
||||
) {
|
||||
for (let i = 0; i < controlValues.length; i++) {
|
||||
this.widgets[i + 1].value = controlValues[i]
|
||||
}
|
||||
}
|
||||
|
||||
this._finalizeWidget(widget, oldWidth, oldHeight, recreating)
|
||||
}
|
||||
|
||||
private _createAssetWidget(
|
||||
targetNode: LGraphNode,
|
||||
targetInputName: string,
|
||||
inputData: InputSpec
|
||||
): IBaseWidget {
|
||||
const defaultValue = inputData[1]?.default as string | undefined
|
||||
return createAssetWidget({
|
||||
node: this,
|
||||
widgetName: 'value',
|
||||
nodeTypeForBrowser: targetNode.comfyClass ?? '',
|
||||
inputNameForBrowser: targetInputName,
|
||||
defaultValue
|
||||
})
|
||||
}
|
||||
|
||||
private _finalizeWidget(
|
||||
widget: IBaseWidget,
|
||||
oldWidth: number,
|
||||
oldHeight: number,
|
||||
recreating: boolean
|
||||
) {
|
||||
widget.callback = useChainCallback(widget.callback, () => {
|
||||
this.applyToGraph()
|
||||
})
|
||||
|
||||
this.setSize([
|
||||
Math.max(this.size[0], oldWidth),
|
||||
Math.max(this.size[1], oldHeight)
|
||||
])
|
||||
|
||||
if (!recreating) {
|
||||
const sz = this.computeSize()
|
||||
if (this.size[0] < sz[0]) {
|
||||
this.size[0] = sz[0]
|
||||
}
|
||||
if (this.size[1] < sz[1]) {
|
||||
this.size[1] = sz[1]
|
||||
}
|
||||
|
||||
requestAnimationFrame(() => {
|
||||
this.onResize?.(this.size)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
recreateWidget() {
|
||||
const values = this.widgets?.map((w) => w.value)
|
||||
this._removeWidgets()
|
||||
this._onFirstConnection(true)
|
||||
if (values?.length && this.widgets) {
|
||||
for (let i = 0; i < this.widgets.length; i++)
|
||||
this.widgets[i].value = values[i]
|
||||
}
|
||||
return this.widgets?.[0]
|
||||
}
|
||||
|
||||
private _mergeWidgetConfig() {
|
||||
// Merge widget configs if the node has multiple outputs
|
||||
const output = this.outputs[0]
|
||||
const links = output.links ?? []
|
||||
|
||||
const hasConfig = !!output.widget?.[CONFIG]
|
||||
if (hasConfig) {
|
||||
delete output.widget?.[CONFIG]
|
||||
}
|
||||
|
||||
if (links?.length < 2 && hasConfig) {
|
||||
// Copy the widget options from the source
|
||||
if (links.length) {
|
||||
this.recreateWidget()
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
const config1 = (output.widget?.[GET_CONFIG] as () => InputSpec)?.()
|
||||
if (!config1) return
|
||||
const isNumber = config1[0] === 'INT' || config1[0] === 'FLOAT'
|
||||
if (!isNumber || !this.graph) return
|
||||
|
||||
for (const linkId of links) {
|
||||
const link = this.graph.links[linkId]
|
||||
if (!link) continue // Can be null when removing a node
|
||||
|
||||
const theirNode = this.graph.getNodeById(link.target_id)
|
||||
if (!theirNode) continue
|
||||
const theirInput = theirNode.inputs[link.target_slot]
|
||||
|
||||
// Call is valid connection so it can merge the configs when validating
|
||||
this._isValidConnection(theirInput, hasConfig)
|
||||
}
|
||||
}
|
||||
|
||||
private _isValidConnection(input: INodeInputSlot, forceUpdate?: boolean) {
|
||||
// Only allow connections where the configs match
|
||||
const output = this.outputs?.[0]
|
||||
const config2 = (input.widget?.[GET_CONFIG] as () => InputSpec)?.()
|
||||
if (!config2) return false
|
||||
|
||||
return !!mergeIfValid.call(
|
||||
this,
|
||||
output,
|
||||
config2,
|
||||
forceUpdate,
|
||||
this.recreateWidget
|
||||
)
|
||||
}
|
||||
|
||||
private _removeWidgets() {
|
||||
if (this.widgets) {
|
||||
// Allow widgets to cleanup
|
||||
for (const w of this.widgets) {
|
||||
if (w.onRemove) {
|
||||
w.onRemove()
|
||||
}
|
||||
}
|
||||
|
||||
// Temporarily store the current values in case the node is being recreated
|
||||
this.controlValues = []
|
||||
this.lastType = this.widgets[0]?.type
|
||||
for (let i = 1; i < this.widgets.length; i++) {
|
||||
this.controlValues.push(this.widgets[i].value)
|
||||
}
|
||||
setTimeout(() => {
|
||||
delete this.lastType
|
||||
delete this.controlValues
|
||||
}, 15)
|
||||
this.widgets.length = 0
|
||||
}
|
||||
}
|
||||
|
||||
onLastDisconnect() {
|
||||
// We can't remove + re-add the output here as if you drag a link over the same link
|
||||
// it removes, then re-adds, causing it to break
|
||||
this.outputs[0].type = '*'
|
||||
this.outputs[0].name = 'connect to widget input'
|
||||
delete this.outputs[0].widget
|
||||
|
||||
this._removeWidgets()
|
||||
}
|
||||
}
|
||||
|
||||
export function getWidgetConfig(
|
||||
slot: INodeInputSlot | INodeOutputSlot
|
||||
): InputSpec {
|
||||
return (slot.widget?.[CONFIG] ??
|
||||
(slot.widget?.[GET_CONFIG] as () => InputSpec)?.() ?? [
|
||||
'*',
|
||||
{}
|
||||
]) as InputSpec
|
||||
}
|
||||
|
||||
function getConfig(this: LGraphNode, widgetName: string) {
|
||||
const { nodeData } = this.constructor
|
||||
return (
|
||||
nodeData?.input?.required?.[widgetName] ??
|
||||
nodeData?.input?.optional?.[widgetName]
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a widget to an input slot.
|
||||
* @deprecated Widget to socket conversion is no longer necessary, as they co-exist now.
|
||||
* @param node The node to convert the widget to an input slot for.
|
||||
* @param widget The widget to convert to an input slot.
|
||||
* @returns The input slot that was converted from the widget or undefined if the widget is not found.
|
||||
* @knipIgnoreUnusedButUsedByCustomNodes
|
||||
*/
|
||||
export function convertToInput(
|
||||
node: LGraphNode,
|
||||
widget: IBaseWidget
|
||||
): INodeInputSlot | undefined {
|
||||
console.warn(
|
||||
'Please remove call to convertToInput. Widget to socket conversion is no longer necessary, as they co-exist now.'
|
||||
)
|
||||
return node.inputs.find((slot) => slot.widget?.name === widget.name)
|
||||
}
|
||||
|
||||
function getWidgetType(config: InputSpec) {
|
||||
// Special handling for COMBO so we restrict links based on the entries
|
||||
let type = config[0]
|
||||
if (type instanceof Array) {
|
||||
type = 'COMBO'
|
||||
}
|
||||
return { type }
|
||||
}
|
||||
|
||||
export function setWidgetConfig(slot: INodeInputSlot, config?: InputSpec) {
|
||||
if (!slot.widget) return
|
||||
if (config) {
|
||||
slot.widget[GET_CONFIG] = () => config
|
||||
} else {
|
||||
delete slot.widget
|
||||
}
|
||||
|
||||
if (!(slot instanceof NodeSlot)) return
|
||||
const graph = slot.node.graph
|
||||
if (!graph) return
|
||||
const link = graph.getLink(slot.link)
|
||||
if (!link) return
|
||||
const originNode = graph.getNodeById(link.origin_id)
|
||||
if (!originNode || !isPrimitiveNode(originNode)) return
|
||||
if (config) {
|
||||
originNode.recreateWidget()
|
||||
} else if (!app.configuringGraph) {
|
||||
originNode.disconnectOutput(0)
|
||||
originNode.onLastDisconnect()
|
||||
}
|
||||
}
|
||||
|
||||
export function mergeIfValid(
|
||||
output: INodeOutputSlot | INodeInputSlot,
|
||||
config2: InputSpec,
|
||||
forceUpdate?: boolean,
|
||||
recreateWidget?: () => void,
|
||||
config1?: InputSpec
|
||||
): { customConfig: InputSpec[1] } {
|
||||
if (!config1) {
|
||||
config1 = getWidgetConfig(output)
|
||||
}
|
||||
|
||||
const customSpec = mergeInputSpec(config1, config2)
|
||||
|
||||
if (customSpec || forceUpdate) {
|
||||
if (customSpec) {
|
||||
// @ts-expect-error fixme ts strict error
|
||||
output.widget[CONFIG] = customSpec
|
||||
}
|
||||
|
||||
// @ts-expect-error fixme ts strict error
|
||||
const widget = recreateWidget?.call(this)
|
||||
// When deleting a node this can be null
|
||||
if (widget) {
|
||||
// @ts-expect-error fixme ts strict error
|
||||
const min = widget.options.min
|
||||
// @ts-expect-error fixme ts strict error
|
||||
const max = widget.options.max
|
||||
// @ts-expect-error fixme ts strict error
|
||||
if (min != null && widget.value < min) widget.value = min
|
||||
// @ts-expect-error fixme ts strict error
|
||||
if (max != null && widget.value > max) widget.value = max
|
||||
// @ts-expect-error fixme ts strict error
|
||||
widget.callback(widget.value)
|
||||
}
|
||||
}
|
||||
|
||||
return { customConfig: customSpec?.[1] ?? {} }
|
||||
}
|
||||
|
||||
app.registerExtension({
|
||||
name: 'Comfy.WidgetInputs',
|
||||
async beforeRegisterNodeDef(
|
||||
nodeType: typeof LGraphNode,
|
||||
_nodeData: ComfyNodeDef
|
||||
) {
|
||||
// @ts-expect-error adding extra property
|
||||
nodeType.prototype.convertWidgetToInput = function (this: LGraphNode) {
|
||||
console.warn(
|
||||
'Please remove call to convertWidgetToInput. Widget to socket conversion is no longer necessary, as they co-exist now.'
|
||||
)
|
||||
return false
|
||||
}
|
||||
|
||||
nodeType.prototype.onGraphConfigured = useChainCallback(
|
||||
nodeType.prototype.onGraphConfigured,
|
||||
function (this: LGraphNode) {
|
||||
if (!this.inputs) return
|
||||
this.widgets ??= []
|
||||
|
||||
for (const input of this.inputs) {
|
||||
if (input.widget) {
|
||||
const name = input.widget.name
|
||||
if (!input.widget[GET_CONFIG]) {
|
||||
input.widget[GET_CONFIG] = () => getConfig.call(this, name)
|
||||
}
|
||||
|
||||
const w = this.widgets?.find((w) => w.name === name)
|
||||
if (!w) {
|
||||
this.removeInput(this.inputs.findIndex((i) => i === input))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
nodeType.prototype.onConfigure = useChainCallback(
|
||||
nodeType.prototype.onConfigure,
|
||||
function (this: LGraphNode) {
|
||||
if (!app.configuringGraph && this.inputs) {
|
||||
// On copy + paste of nodes, ensure that widget configs are set up
|
||||
for (const input of this.inputs) {
|
||||
if (input.widget && !input.widget[GET_CONFIG]) {
|
||||
const name = input.widget.name
|
||||
input.widget[GET_CONFIG] = () => getConfig.call(this, name)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
// Double click a widget input to automatically attach a primitive
|
||||
const origOnInputDblClick = nodeType.prototype.onInputDblClick
|
||||
nodeType.prototype.onInputDblClick = function (
|
||||
this: LGraphNode,
|
||||
...[slot, ...args]: Parameters<NonNullable<typeof origOnInputDblClick>>
|
||||
) {
|
||||
const r = origOnInputDblClick?.apply(this, [slot, ...args])
|
||||
|
||||
const input = this.inputs[slot]
|
||||
if (!input.widget) {
|
||||
// Not a widget input or already handled input
|
||||
if (
|
||||
!(input.type in ComfyWidgets) &&
|
||||
!(
|
||||
(
|
||||
input.widget?.[GET_CONFIG] as (() => InputSpec) | undefined
|
||||
)?.()?.[0] instanceof Array
|
||||
)
|
||||
) {
|
||||
return r //also Not a ComfyWidgets input or combo (do nothing)
|
||||
}
|
||||
}
|
||||
|
||||
// Create a primitive node
|
||||
const node = LiteGraph.createNode('PrimitiveNode')
|
||||
const graph = app.canvas.graph
|
||||
if (!node || !graph) return r
|
||||
|
||||
graph?.add(node)
|
||||
|
||||
// Calculate a position that won't directly overlap another node
|
||||
const pos: [number, number] = [
|
||||
this.pos[0] - node.size[0] - 30,
|
||||
this.pos[1]
|
||||
]
|
||||
while (graph.getNodeOnPos(pos[0], pos[1], graph.nodes))
|
||||
pos[1] += LiteGraph.NODE_TITLE_HEIGHT
|
||||
|
||||
node.pos = pos
|
||||
node.connect(0, this, slot)
|
||||
node.title = input.name
|
||||
|
||||
return r
|
||||
}
|
||||
},
|
||||
registerCustomNodes() {
|
||||
LiteGraph.registerNodeType(
|
||||
'PrimitiveNode',
|
||||
Object.assign(PrimitiveNode, {
|
||||
title: 'Primitive'
|
||||
})
|
||||
)
|
||||
PrimitiveNode.category = 'utilities/primitive'
|
||||
}
|
||||
})
|
||||
@@ -0,0 +1,507 @@
|
||||
import {
|
||||
SUBGRAPH_INPUT_ID,
|
||||
SUBGRAPH_OUTPUT_ID
|
||||
} from '@/lib/litegraph/src/constants'
|
||||
import type { SubgraphInput } from '@/lib/litegraph/src/subgraph/SubgraphInput'
|
||||
import type { SubgraphOutput } from '@/lib/litegraph/src/subgraph/SubgraphOutput'
|
||||
import { useLayoutMutations } from '@/renderer/core/layout/operations/layoutMutations'
|
||||
import { LayoutSource } from '@/renderer/core/layout/types'
|
||||
import { toLinkId } from '@/types/linkId'
|
||||
import { UNASSIGNED_NODE_ID, toNodeId, serializeNodeId } from '@/types/nodeId'
|
||||
import { toRerouteId } from '@/types/rerouteId'
|
||||
|
||||
import type { LinkId } from '@/types/linkId'
|
||||
import type { RerouteId } from '@/types/rerouteId'
|
||||
import type { LGraphNode } from './LGraphNode'
|
||||
import type { NodeId, SerializedNodeId } from '@/types/nodeId'
|
||||
import type { Reroute } from './Reroute'
|
||||
import type {
|
||||
CanvasColour,
|
||||
INodeInputSlot,
|
||||
INodeOutputSlot,
|
||||
ISlotType,
|
||||
LinkNetwork,
|
||||
LinkSegment,
|
||||
Point,
|
||||
ReadonlyLinkNetwork
|
||||
} from './interfaces'
|
||||
import type { Serialisable, SerialisableLLink } from './types/serialisation'
|
||||
|
||||
const layoutMutations = useLayoutMutations()
|
||||
|
||||
export type { LinkId } from '@/types/linkId'
|
||||
export type SerialisedLLinkArray = [
|
||||
id: number,
|
||||
origin_id: SerializedNodeId,
|
||||
origin_slot: number,
|
||||
target_id: SerializedNodeId,
|
||||
target_slot: number,
|
||||
type: ISlotType
|
||||
]
|
||||
|
||||
// Resolved connection union; eliminates subgraph in/out as a possibility
|
||||
export type ResolvedConnection = BaseResolvedConnection &
|
||||
(
|
||||
| (ResolvedSubgraphInput & ResolvedNormalOutput)
|
||||
| (ResolvedNormalInput & ResolvedSubgraphOutput)
|
||||
| (ResolvedNormalInput & ResolvedNormalOutput)
|
||||
)
|
||||
|
||||
interface BaseResolvedConnection {
|
||||
link: LLink
|
||||
/** The node on the input side of the link (owns {@link input}) */
|
||||
inputNode?: LGraphNode
|
||||
/** The input the link is connected to (mutually exclusive with {@link subgraphOutput}) */
|
||||
input?: INodeInputSlot
|
||||
/** The node on the output side of the link (owns {@link output}) */
|
||||
outputNode?: LGraphNode
|
||||
/** The output the link is connected to (mutually exclusive with {@link subgraphInput}) */
|
||||
output?: INodeOutputSlot
|
||||
/** The subgraph output the link is connected to (mutually exclusive with {@link input}) */
|
||||
subgraphOutput?: SubgraphOutput
|
||||
/** The subgraph input the link is connected to (mutually exclusive with {@link output}) */
|
||||
subgraphInput?: SubgraphInput
|
||||
}
|
||||
|
||||
interface ResolvedNormalInput {
|
||||
inputNode: LGraphNode | undefined
|
||||
input: INodeInputSlot | undefined
|
||||
subgraphOutput?: undefined
|
||||
}
|
||||
|
||||
interface ResolvedNormalOutput {
|
||||
outputNode: LGraphNode | undefined
|
||||
output: INodeOutputSlot | undefined
|
||||
subgraphInput?: undefined
|
||||
}
|
||||
|
||||
interface ResolvedSubgraphInput {
|
||||
inputNode?: undefined
|
||||
/** The actual input slot the link is connected to (mutually exclusive with {@link subgraphOutput}) */
|
||||
input?: undefined
|
||||
subgraphOutput: SubgraphOutput
|
||||
}
|
||||
|
||||
interface ResolvedSubgraphOutput {
|
||||
outputNode?: undefined
|
||||
output?: undefined
|
||||
subgraphInput: SubgraphInput
|
||||
}
|
||||
|
||||
type BasicReadonlyNetwork = Pick<
|
||||
ReadonlyLinkNetwork,
|
||||
'getNodeById' | 'links' | 'getLink' | 'inputNode' | 'outputNode'
|
||||
>
|
||||
|
||||
// this is the class in charge of storing link information
|
||||
export class LLink implements LinkSegment, Serialisable<SerialisableLLink> {
|
||||
static _drawDebug = false
|
||||
|
||||
/** Link ID */
|
||||
id: LinkId
|
||||
parentId?: RerouteId
|
||||
type: ISlotType
|
||||
/** Output node ID */
|
||||
origin_id: NodeId
|
||||
/** Output slot index */
|
||||
origin_slot: number
|
||||
/** Input node ID */
|
||||
target_id: NodeId
|
||||
/** Input slot index */
|
||||
target_slot: number
|
||||
|
||||
data?: number | string | boolean | { toToolTip?(): string }
|
||||
_data?: unknown
|
||||
/** Centre point of the link, calculated during render only - can be inaccurate */
|
||||
_pos: Point
|
||||
/** @todo Clean up - never implemented in comfy. */
|
||||
_last_time?: number
|
||||
/** The last canvas 2D path that was used to render this link */
|
||||
path?: Path2D
|
||||
/** @inheritdoc */
|
||||
_centreAngle?: number
|
||||
|
||||
/** @inheritdoc */
|
||||
_dragging?: boolean
|
||||
|
||||
private _color?: CanvasColour | null
|
||||
/** Custom colour for this link only */
|
||||
public get color(): CanvasColour | null | undefined {
|
||||
return this._color
|
||||
}
|
||||
|
||||
public set color(value: CanvasColour) {
|
||||
this._color = value === '' ? null : value
|
||||
}
|
||||
|
||||
public get isFloatingOutput(): boolean {
|
||||
return this.origin_id === UNASSIGNED_NODE_ID && this.origin_slot === -1
|
||||
}
|
||||
|
||||
public get isFloatingInput(): boolean {
|
||||
return this.target_id === UNASSIGNED_NODE_ID && this.target_slot === -1
|
||||
}
|
||||
|
||||
public get isFloating(): boolean {
|
||||
return this.isFloatingOutput || this.isFloatingInput
|
||||
}
|
||||
|
||||
/** `true` if this link is connected to a subgraph input node (the actual origin is in a different graph). */
|
||||
get originIsIoNode(): boolean {
|
||||
return this.origin_id === SUBGRAPH_INPUT_ID
|
||||
}
|
||||
|
||||
/** `true` if this link is connected to a subgraph output node (the actual target is in a different graph). */
|
||||
get targetIsIoNode(): boolean {
|
||||
return this.target_id === SUBGRAPH_OUTPUT_ID
|
||||
}
|
||||
|
||||
constructor(
|
||||
id: LinkId,
|
||||
type: ISlotType,
|
||||
origin_id: SerializedNodeId,
|
||||
origin_slot: number,
|
||||
target_id: SerializedNodeId,
|
||||
target_slot: number,
|
||||
parentId?: RerouteId
|
||||
) {
|
||||
this.id = id
|
||||
this.type = type
|
||||
this.origin_id = toNodeId(origin_id)
|
||||
this.origin_slot = origin_slot
|
||||
this.target_id = toNodeId(target_id)
|
||||
this.target_slot = target_slot
|
||||
this.parentId = parentId
|
||||
|
||||
this._data = null
|
||||
// center
|
||||
this._pos = [0, 0]
|
||||
}
|
||||
|
||||
/** @deprecated Use {@link LLink.create} */
|
||||
static createFromArray(data: SerialisedLLinkArray): LLink {
|
||||
return new LLink(
|
||||
toLinkId(data[0]),
|
||||
data[5],
|
||||
data[1],
|
||||
data[2],
|
||||
data[3],
|
||||
data[4]
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* LLink static factory: creates a new LLink from the provided data.
|
||||
* @param data Serialised LLink data to create the link from
|
||||
* @returns A new LLink
|
||||
*/
|
||||
static create(data: SerialisableLLink): LLink {
|
||||
return new LLink(
|
||||
toLinkId(data.id),
|
||||
data.type,
|
||||
data.origin_id,
|
||||
data.origin_slot,
|
||||
data.target_id,
|
||||
data.target_slot,
|
||||
data.parentId === undefined ? undefined : toRerouteId(data.parentId)
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets all reroutes from the output slot to this segment. If this segment is a reroute, it will not be included.
|
||||
* @returns An ordered array of all reroutes from the node output to
|
||||
* this reroute or the reroute before it. Otherwise, an empty array.
|
||||
*/
|
||||
static getReroutes(
|
||||
network: Pick<ReadonlyLinkNetwork, 'reroutes'>,
|
||||
linkSegment: LinkSegment
|
||||
): Reroute[] {
|
||||
if (linkSegment.parentId === undefined) return []
|
||||
return network.reroutes.get(linkSegment.parentId)?.getReroutes() ?? []
|
||||
}
|
||||
|
||||
static getFirstReroute(
|
||||
network: Pick<ReadonlyLinkNetwork, 'reroutes'>,
|
||||
linkSegment: LinkSegment
|
||||
): Reroute | undefined {
|
||||
return LLink.getReroutes(network, linkSegment).at(0)
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds the reroute in the chain after the provided reroute ID.
|
||||
* @param network The network this link belongs to
|
||||
* @param linkSegment The starting point of the search (input side).
|
||||
* Typically the LLink object itself, but can be any link segment.
|
||||
* @param rerouteId The matching reroute will have this set as its {@link parentId}.
|
||||
* @returns The reroute that was found, `undefined` if no reroute was found, or `null` if an infinite loop was detected.
|
||||
*/
|
||||
static findNextReroute(
|
||||
network: Pick<ReadonlyLinkNetwork, 'reroutes'>,
|
||||
linkSegment: LinkSegment,
|
||||
rerouteId: RerouteId
|
||||
): Reroute | null | undefined {
|
||||
if (linkSegment.parentId === undefined) return
|
||||
return network.reroutes
|
||||
.get(linkSegment.parentId)
|
||||
?.findNextReroute(rerouteId)
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the origin node of a link.
|
||||
* @param network The network to search
|
||||
* @param linkId The ID of the link to get the origin node of
|
||||
* @returns The origin node of the link, or `undefined` if the link is not found or the origin node is not found
|
||||
*/
|
||||
static getOriginNode(
|
||||
network: BasicReadonlyNetwork,
|
||||
linkId: LinkId
|
||||
): LGraphNode | undefined {
|
||||
const id = network.links.get(linkId)?.origin_id
|
||||
return network.getNodeById(id) ?? undefined
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the target node of a link.
|
||||
* @param network The network to search
|
||||
* @param linkId The ID of the link to get the target node of
|
||||
* @returns The target node of the link, or `undefined` if the link is not found or the target node is not found
|
||||
*/
|
||||
static getTargetNode(
|
||||
network: BasicReadonlyNetwork,
|
||||
linkId: LinkId
|
||||
): LGraphNode | undefined {
|
||||
const id = network.links.get(linkId)?.target_id
|
||||
return network.getNodeById(id) ?? undefined
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolves a link ID to the link, node, and slot objects.
|
||||
* @param linkId The {@link id} of the link to resolve
|
||||
* @param network The link network to search
|
||||
* @returns An object containing the input and output nodes, as well as the input and output slots.
|
||||
* @remarks This method is heavier than others; it will always resolve all objects.
|
||||
* Whilst the performance difference should in most cases be negligible,
|
||||
* it is recommended to use simpler methods where appropriate.
|
||||
*/
|
||||
static resolve(
|
||||
linkId: LinkId | null | undefined,
|
||||
network: BasicReadonlyNetwork
|
||||
): ResolvedConnection | undefined {
|
||||
return network.getLink(linkId)?.resolve(network)
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolves a list of link IDs to the link, node, and slot objects.
|
||||
* Discards invalid link IDs.
|
||||
* @param linkIds An iterable of link {@link id}s to resolve
|
||||
* @param network The link network to search
|
||||
* @returns An array of resolved connections. If a link is not found, it is not included in the array.
|
||||
* @see {@link LLink.resolve}
|
||||
*/
|
||||
static resolveMany(
|
||||
linkIds: Iterable<LinkId>,
|
||||
network: BasicReadonlyNetwork
|
||||
): ResolvedConnection[] {
|
||||
const resolved: ResolvedConnection[] = []
|
||||
for (const id of linkIds) {
|
||||
const r = network.getLink(id)?.resolve(network)
|
||||
if (r) resolved.push(r)
|
||||
}
|
||||
return resolved
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolves the primitive ID values stored in the link to the referenced objects.
|
||||
* @param network The link network to search
|
||||
* @returns An object containing the input and output nodes, as well as the input and output slots.
|
||||
* @remarks This method is heavier than others; it will always resolve all objects.
|
||||
* Whilst the performance difference should in most cases be negligible,
|
||||
* it is recommended to use simpler methods where appropriate.
|
||||
*/
|
||||
resolve(network: BasicReadonlyNetwork): ResolvedConnection {
|
||||
const inputNode =
|
||||
this.target_id === UNASSIGNED_NODE_ID
|
||||
? undefined
|
||||
: (network.getNodeById(this.target_id) ?? undefined)
|
||||
const input = inputNode?.inputs[this.target_slot]
|
||||
const subgraphInput = this.originIsIoNode
|
||||
? network.inputNode?.slots[this.origin_slot]
|
||||
: undefined
|
||||
if (subgraphInput) {
|
||||
return { inputNode, input, subgraphInput, link: this }
|
||||
}
|
||||
|
||||
const outputNode =
|
||||
this.origin_id === UNASSIGNED_NODE_ID
|
||||
? undefined
|
||||
: (network.getNodeById(this.origin_id) ?? undefined)
|
||||
const output = outputNode?.outputs[this.origin_slot]
|
||||
const subgraphOutput = this.targetIsIoNode
|
||||
? network.outputNode?.slots[this.target_slot]
|
||||
: undefined
|
||||
if (subgraphOutput) {
|
||||
return {
|
||||
outputNode,
|
||||
output,
|
||||
subgraphInput: undefined,
|
||||
subgraphOutput,
|
||||
link: this
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
inputNode,
|
||||
outputNode,
|
||||
input,
|
||||
output,
|
||||
subgraphInput,
|
||||
subgraphOutput,
|
||||
link: this
|
||||
}
|
||||
}
|
||||
|
||||
configure(o: LLink | SerialisedLLinkArray) {
|
||||
if (Array.isArray(o)) {
|
||||
this.id = toLinkId(o[0])
|
||||
this.origin_id = toNodeId(o[1])
|
||||
this.origin_slot = o[2]
|
||||
this.target_id = toNodeId(o[3])
|
||||
this.target_slot = o[4]
|
||||
this.type = o[5]
|
||||
} else {
|
||||
this.id = o.id
|
||||
this.type = o.type
|
||||
this.origin_id = o.origin_id
|
||||
this.origin_slot = o.origin_slot
|
||||
this.target_id = o.target_id
|
||||
this.target_slot = o.target_slot
|
||||
this.parentId = o.parentId
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the specified node id and output index are this link's origin (output side).
|
||||
* @param nodeId ID of the node to check
|
||||
* @param outputIndex The array index of the node output
|
||||
* @returns `true` if the origin matches, otherwise `false`.
|
||||
*/
|
||||
hasOrigin(nodeId: SerializedNodeId, outputIndex: number): boolean {
|
||||
return (
|
||||
this.origin_id === toNodeId(nodeId) && this.origin_slot === outputIndex
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the specified node id and input index are this link's target (input side).
|
||||
* @param nodeId ID of the node to check
|
||||
* @param inputIndex The array index of the node input
|
||||
* @returns `true` if the target matches, otherwise `false`.
|
||||
*/
|
||||
hasTarget(nodeId: SerializedNodeId, inputIndex: number): boolean {
|
||||
return (
|
||||
this.target_id === toNodeId(nodeId) && this.target_slot === inputIndex
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a floating link from this link.
|
||||
* @param slotType The side of the link that is still connected
|
||||
* @param parentId The parent reroute ID of the link
|
||||
* @returns A new LLink that is floating
|
||||
*/
|
||||
toFloating(slotType: 'input' | 'output', parentId: RerouteId): LLink {
|
||||
const exported = this.asSerialisable()
|
||||
exported.id = toLinkId(-1)
|
||||
exported.parentId = parentId
|
||||
|
||||
if (slotType === 'input') {
|
||||
exported.origin_id = UNASSIGNED_NODE_ID
|
||||
exported.origin_slot = -1
|
||||
} else {
|
||||
exported.target_id = UNASSIGNED_NODE_ID
|
||||
exported.target_slot = -1
|
||||
}
|
||||
|
||||
return LLink.create(exported)
|
||||
}
|
||||
|
||||
/**
|
||||
* Disconnects a link and removes it from the graph, cleaning up any reroutes that are no longer used
|
||||
* @param network The container (LGraph) where reroutes should be updated
|
||||
* @param keepReroutes If `undefined`, reroutes will be automatically removed if no links remain.
|
||||
* If `input` or `output`, reroutes will not be automatically removed, and retain a connection to the input or output, respectively.
|
||||
*/
|
||||
disconnect(network: LinkNetwork, keepReroutes?: 'input' | 'output'): void {
|
||||
const reroutes = LLink.getReroutes(network, this)
|
||||
|
||||
const lastReroute = reroutes.at(-1)
|
||||
|
||||
// When floating from output, 1-to-1 ratio of floating link to final reroute (tree-like)
|
||||
const outputFloating =
|
||||
keepReroutes === 'output' &&
|
||||
lastReroute?.linkIds.size === 1 &&
|
||||
lastReroute.floatingLinkIds.size === 0
|
||||
|
||||
// When floating from inputs, the final (input side) reroute may have many floating links
|
||||
if (outputFloating || (keepReroutes === 'input' && lastReroute)) {
|
||||
const newLink = LLink.create(this)
|
||||
newLink.id = toLinkId(-1)
|
||||
|
||||
if (keepReroutes === 'input') {
|
||||
newLink.origin_id = UNASSIGNED_NODE_ID
|
||||
newLink.origin_slot = -1
|
||||
|
||||
lastReroute.floating = { slotType: 'input' }
|
||||
} else {
|
||||
newLink.target_id = UNASSIGNED_NODE_ID
|
||||
newLink.target_slot = -1
|
||||
|
||||
lastReroute.floating = { slotType: 'output' }
|
||||
}
|
||||
|
||||
network.addFloatingLink(newLink)
|
||||
}
|
||||
|
||||
for (const reroute of reroutes) {
|
||||
reroute.linkIds.delete(this.id)
|
||||
if (!keepReroutes && !reroute.totalLinks) {
|
||||
network.reroutes.delete(reroute.id)
|
||||
// Delete reroute from Layout Store
|
||||
layoutMutations.setSource(LayoutSource.Canvas)
|
||||
layoutMutations.deleteReroute(reroute.id)
|
||||
}
|
||||
}
|
||||
network.links.delete(this.id)
|
||||
// Delete link from Layout Store
|
||||
layoutMutations.setSource(LayoutSource.Canvas)
|
||||
layoutMutations.deleteLink(this.id)
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated Prefer {@link LLink.asSerialisable} (returns an object, not an array)
|
||||
* @returns An array representing this LLink
|
||||
*/
|
||||
serialize(): SerialisedLLinkArray {
|
||||
return [
|
||||
this.id,
|
||||
serializeNodeId(this.origin_id),
|
||||
this.origin_slot,
|
||||
serializeNodeId(this.target_id),
|
||||
this.target_slot,
|
||||
this.type
|
||||
]
|
||||
}
|
||||
|
||||
asSerialisable(): SerialisableLLink {
|
||||
const copy: SerialisableLLink = {
|
||||
id: this.id,
|
||||
origin_id: serializeNodeId(this.origin_id),
|
||||
origin_slot: this.origin_slot,
|
||||
target_id: serializeNodeId(this.target_id),
|
||||
target_slot: this.target_slot,
|
||||
type: this.type
|
||||
}
|
||||
if (this.parentId !== undefined) copy.parentId = this.parentId
|
||||
return copy
|
||||
}
|
||||
}
|
||||
+429
@@ -0,0 +1,429 @@
|
||||
import { toLinkId } from '@/types/linkId'
|
||||
import type { LGraph } from '@/lib/litegraph/src/LGraph'
|
||||
import type { LGraphNode } from '@/lib/litegraph/src/LGraphNode'
|
||||
import type { SerializedNodeId } from '@/types/nodeId'
|
||||
import type { LinkId } from '@/lib/litegraph/src/LLink'
|
||||
import { InvalidLinkError } from '@/lib/litegraph/src/infrastructure/InvalidLinkError'
|
||||
import { useWidgetValueStore } from '@/stores/widgetValueStore'
|
||||
import { NullGraphError } from '@/lib/litegraph/src/infrastructure/NullGraphError'
|
||||
import { RecursionError } from '@/lib/litegraph/src/infrastructure/RecursionError'
|
||||
import { SlotIndexError } from '@/lib/litegraph/src/infrastructure/SlotIndexError'
|
||||
import type { ISlotType } from '@/lib/litegraph/src/interfaces'
|
||||
import { LGraphEventMode, LiteGraph } from '@/lib/litegraph/src/litegraph'
|
||||
|
||||
import type { Subgraph } from './Subgraph'
|
||||
import type { SubgraphNode } from './SubgraphNode'
|
||||
|
||||
export type ExecutionId = string
|
||||
|
||||
/**
|
||||
* Interface describing the data transfer objects used when compiling a graph for execution.
|
||||
*/
|
||||
export type ExecutableLGraphNode = Omit<
|
||||
ExecutableNodeDTO,
|
||||
'graph' | 'node' | 'subgraphNode'
|
||||
>
|
||||
|
||||
/**
|
||||
* The end result of resolving a DTO input.
|
||||
* When a widget value is returned, {@link widgetInfo} is present and {@link origin_slot} is `-1`.
|
||||
*/
|
||||
type ResolvedInput = {
|
||||
/** DTO for the node that the link originates from. */
|
||||
node: ExecutableLGraphNode
|
||||
/** Full unique execution ID of the node that the link originates from. In the case of a widget value, this is the ID of the subgraph node. */
|
||||
origin_id: ExecutionId
|
||||
/** The slot index of the output on the node that the link originates from. `-1` when widget value is set. */
|
||||
origin_slot: number
|
||||
/** Boxed widget value (e.g. for widgets). If this box is `undefined`, then an input link is connected, and widget values from the subgraph node are ignored. */
|
||||
widgetInfo?: { value: unknown }
|
||||
}
|
||||
|
||||
/**
|
||||
* Concrete implementation of {@link ExecutableLGraphNode}.
|
||||
* @remarks This is the class that is used to create the data transfer objects for executable nodes.
|
||||
*/
|
||||
export class ExecutableNodeDTO implements ExecutableLGraphNode {
|
||||
applyToGraph?(
|
||||
...args: Parameters<NonNullable<typeof this.node.applyToGraph>>
|
||||
): ReturnType<NonNullable<typeof this.node.applyToGraph>>
|
||||
|
||||
/** The graph that this node is a part of. */
|
||||
readonly graph: LGraph | Subgraph
|
||||
|
||||
inputs: { linkId: LinkId | null; name: string; type: ISlotType }[]
|
||||
|
||||
/** Backing field for {@link id}. */
|
||||
private _id: ExecutionId
|
||||
|
||||
/**
|
||||
* The path to the actual node through subgraph instances, represented as a list of all subgraph node IDs (instances),
|
||||
* followed by the actual original node ID within the subgraph. Each segment is separated by `:`.
|
||||
*
|
||||
* e.g. `1:2:3`:
|
||||
* - `1` is the node ID of the first subgraph node in the parent workflow
|
||||
* - `2` is the node ID of the second subgraph node in the first subgraph
|
||||
* - `3` is the node ID of the actual node in the subgraph definition
|
||||
*/
|
||||
get id() {
|
||||
return this._id
|
||||
}
|
||||
|
||||
get type() {
|
||||
return this.node.type
|
||||
}
|
||||
|
||||
get title() {
|
||||
return this.node.title
|
||||
}
|
||||
|
||||
get mode() {
|
||||
return this.node.mode
|
||||
}
|
||||
|
||||
get comfyClass() {
|
||||
return this.node.comfyClass
|
||||
}
|
||||
|
||||
get isVirtualNode() {
|
||||
return this.node.isVirtualNode
|
||||
}
|
||||
|
||||
get widgets() {
|
||||
return this.node.widgets
|
||||
}
|
||||
|
||||
get subgraphId() {
|
||||
return this.subgraphNode?.subgraph.id
|
||||
}
|
||||
|
||||
constructor(
|
||||
/** The actual node that this DTO wraps. */
|
||||
readonly node: LGraphNode | SubgraphNode,
|
||||
/** A list of subgraph instance node IDs from the root graph to the containing instance. @see {@link id} */
|
||||
readonly subgraphNodePath: readonly SerializedNodeId[],
|
||||
/** A flattened map of all DTOs in this node network. Subgraph instances have been expanded into their inner nodes. */
|
||||
readonly nodesByExecutionId: Map<ExecutionId, ExecutableLGraphNode>,
|
||||
/** The actual subgraph instance that contains this node, otherwise undefined. */
|
||||
readonly subgraphNode?: SubgraphNode
|
||||
) {
|
||||
if (!node.graph) throw new NullGraphError()
|
||||
|
||||
// Set the internal ID of the DTO
|
||||
this._id = [...this.subgraphNodePath, this.node.id].join(':')
|
||||
this.graph = node.graph
|
||||
this.inputs = this.node.inputs.map((x) => ({
|
||||
linkId: x.link == null ? null : toLinkId(x.link),
|
||||
name: x.name,
|
||||
type: x.type
|
||||
}))
|
||||
|
||||
// Only create a wrapper if the node has an applyToGraph method
|
||||
if (this.node.applyToGraph) {
|
||||
this.applyToGraph = (...args) => this.node.applyToGraph?.(...args)
|
||||
}
|
||||
}
|
||||
|
||||
/** Returns either the DTO itself, or the DTOs of the inner nodes of the subgraph. */
|
||||
getInnerNodes(): ExecutableLGraphNode[] {
|
||||
return this.node.isSubgraphNode()
|
||||
? this.node.getInnerNodes(this.nodesByExecutionId, this.subgraphNodePath)
|
||||
: [this]
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolves the executable node & link IDs for a given input slot.
|
||||
* @param slot The slot index of the input.
|
||||
* @param visited Leave empty unless overriding this method.
|
||||
* A set of unique IDs, used to guard against infinite recursion.
|
||||
* If overriding, ensure that the set is passed on all recursive calls.
|
||||
* @returns The node and the origin ID / slot index of the output.
|
||||
*/
|
||||
resolveInput(
|
||||
slot: number,
|
||||
visited = new Set<string>(),
|
||||
type?: ISlotType
|
||||
): ResolvedInput | undefined {
|
||||
const uniqueId = `${this.subgraphNode?.subgraph.id}:${this.node.id}[I]${slot}`
|
||||
if (visited.has(uniqueId)) {
|
||||
const nodeInfo = `${this.node.id}${this.node.title ? ` (${this.node.title})` : ''}`
|
||||
const pathInfo =
|
||||
this.subgraphNodePath.length > 0
|
||||
? ` at path ${this.subgraphNodePath.join(':')}`
|
||||
: ''
|
||||
throw new RecursionError(
|
||||
`Circular reference detected while resolving input ${slot} of node ${nodeInfo}${pathInfo}. ` +
|
||||
`This creates an infinite loop in link resolution. UniqueID: [${uniqueId}]`
|
||||
)
|
||||
}
|
||||
visited.add(uniqueId)
|
||||
|
||||
const input = this.inputs.at(slot)
|
||||
if (!input)
|
||||
throw new SlotIndexError(
|
||||
`No input found for flattened id [${this.id}] slot [${slot}]`
|
||||
)
|
||||
|
||||
// Nothing connected
|
||||
if (input.linkId == null) return
|
||||
|
||||
const link = this.graph.getLink(input.linkId)
|
||||
if (!link)
|
||||
throw new InvalidLinkError(
|
||||
`No link found in parent graph for id [${this.id}] slot [${slot}] ${input.name}`
|
||||
)
|
||||
|
||||
const { subgraphNode } = this
|
||||
|
||||
// Link goes up and out of this subgraph
|
||||
if (subgraphNode && link.originIsIoNode) {
|
||||
const subgraphNodeInput = subgraphNode.inputs.at(link.origin_slot)
|
||||
if (!subgraphNodeInput)
|
||||
throw new SlotIndexError(
|
||||
`No input found for slot [${link.origin_slot}] ${input.name}`
|
||||
)
|
||||
|
||||
// Nothing connected
|
||||
const linkId = subgraphNodeInput.link
|
||||
if (linkId == null) {
|
||||
const id = subgraphNodeInput.widgetId
|
||||
if (!id) return
|
||||
|
||||
return {
|
||||
node: this,
|
||||
origin_id: this.id,
|
||||
origin_slot: -1,
|
||||
widgetInfo: { value: useWidgetValueStore().getWidget(id)?.value }
|
||||
}
|
||||
}
|
||||
|
||||
if (!subgraphNode.graph)
|
||||
throw new NullGraphError(
|
||||
`SubgraphNode ${subgraphNode.id} has no graph during input resolution`
|
||||
)
|
||||
const outerLink = subgraphNode.graph.getLink(linkId)
|
||||
if (!outerLink)
|
||||
throw new InvalidLinkError(
|
||||
`No outer link found for slot [${link.origin_slot}] ${input.name}`
|
||||
)
|
||||
|
||||
const subgraphNodeExecutionId: ExecutionId =
|
||||
this.subgraphNodePath.join(':')
|
||||
const subgraphNodeDto = this.nodesByExecutionId.get(
|
||||
subgraphNodeExecutionId
|
||||
)
|
||||
if (!subgraphNodeDto)
|
||||
throw new Error(
|
||||
`No subgraph node DTO found for id [${subgraphNodeExecutionId}]`
|
||||
)
|
||||
|
||||
return subgraphNodeDto.resolveInput(outerLink.target_slot, visited)
|
||||
}
|
||||
|
||||
// Not part of a subgraph; use the original link
|
||||
const outputNode = this.graph.getNodeById(link.origin_id)
|
||||
if (!outputNode)
|
||||
throw new InvalidLinkError(
|
||||
`No input node found for id [${this.id}] slot [${slot}] ${input.name}`
|
||||
)
|
||||
|
||||
const outputNodeExecutionId: ExecutionId = [
|
||||
...this.subgraphNodePath,
|
||||
outputNode.id
|
||||
].join(':')
|
||||
const outputNodeDto = this.nodesByExecutionId.get(outputNodeExecutionId)
|
||||
if (!outputNodeDto)
|
||||
throw new Error(
|
||||
`No output node DTO found for id [${outputNodeExecutionId}]`
|
||||
)
|
||||
|
||||
return outputNodeDto.resolveOutput(
|
||||
link.origin_slot,
|
||||
type ?? input.type,
|
||||
visited
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines whether this output is a valid endpoint for a link (non-virtual, non-bypass).
|
||||
* @param slot The slot index of the output.
|
||||
* @param type The type of the input
|
||||
* @param visited A set of unique IDs to guard against infinite recursion. See {@link resolveInput}.
|
||||
* @returns The node and the origin ID / slot index of the output.
|
||||
*/
|
||||
resolveOutput(
|
||||
slot: number,
|
||||
type: ISlotType,
|
||||
visited: Set<string>
|
||||
): ResolvedInput | undefined {
|
||||
const uniqueId = `${this.subgraphNode?.subgraph.id}:${this.node.id}[O]${slot}`
|
||||
if (visited.has(uniqueId)) {
|
||||
const nodeInfo = `${this.node.id}${this.node.title ? ` (${this.node.title})` : ''}`
|
||||
const pathInfo =
|
||||
this.subgraphNodePath.length > 0
|
||||
? ` at path ${this.subgraphNodePath.join(':')}`
|
||||
: ''
|
||||
throw new RecursionError(
|
||||
`Circular reference detected while resolving output ${slot} of node ${nodeInfo}${pathInfo}. ` +
|
||||
`This creates an infinite loop in link resolution. UniqueID: [${uniqueId}]`
|
||||
)
|
||||
}
|
||||
visited.add(uniqueId)
|
||||
|
||||
// Muted nodes produce no output
|
||||
if (this.mode === LGraphEventMode.NEVER) return
|
||||
|
||||
// Upstreamed: Bypass nodes are bypassed using the first input with matching type
|
||||
if (this.mode === LGraphEventMode.BYPASS) {
|
||||
// Bypass nodes by finding first input with matching type
|
||||
const matchingIndex = this._getBypassSlotIndex(slot, type)
|
||||
|
||||
// No input types match - bypass not possible
|
||||
if (matchingIndex === -1) {
|
||||
console.warn(
|
||||
`[ExecutableNodeDTO.resolveOutput] No input types match type [${type}] for id [${this.id}] slot [${slot}]`,
|
||||
this
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
return this.resolveInput(matchingIndex, visited)
|
||||
}
|
||||
|
||||
const { node } = this
|
||||
if (node.isSubgraphNode())
|
||||
return this._resolveSubgraphOutput(slot, type, visited)
|
||||
|
||||
if (node.isVirtualNode) {
|
||||
// Cross-graph virtual nodes (e.g. Set/Get) resolve their source directly.
|
||||
const virtualSource = this.node.resolveVirtualOutput?.(slot)
|
||||
if (virtualSource) {
|
||||
const inputNodeDto = [...this.nodesByExecutionId.values()].find(
|
||||
(dto) =>
|
||||
dto instanceof ExecutableNodeDTO && dto.node === virtualSource.node
|
||||
)
|
||||
if (!inputNodeDto)
|
||||
throw new Error(
|
||||
`No DTO found for virtual source node [${virtualSource.node.id}]`
|
||||
)
|
||||
|
||||
return inputNodeDto.resolveOutput(virtualSource.slot, type, visited)
|
||||
}
|
||||
const virtualLink = this.node.getInputLink(slot)
|
||||
if (virtualLink) {
|
||||
const { inputNode } = virtualLink.resolve(this.graph)
|
||||
if (!inputNode)
|
||||
throw new InvalidLinkError(
|
||||
`Virtual node failed to resolve parent [${this.id}] slot [${slot}]`
|
||||
)
|
||||
|
||||
const inputNodeExecutionId: ExecutionId = [
|
||||
...this.subgraphNodePath,
|
||||
inputNode.id
|
||||
].join(':')
|
||||
const inputNodeDto = this.nodesByExecutionId.get(inputNodeExecutionId)
|
||||
if (!inputNodeDto)
|
||||
throw new Error(`No input node DTO found for id [${inputNode.id}]`)
|
||||
|
||||
return inputNodeDto.resolveInput(virtualLink.target_slot, visited, type)
|
||||
}
|
||||
|
||||
// Virtual nodes without a matching input should be discarded.
|
||||
return
|
||||
}
|
||||
|
||||
return {
|
||||
node: this,
|
||||
origin_id: this.id,
|
||||
origin_slot: slot
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds the index of the input slot on this node that matches the given output {@link slot} index.
|
||||
* Used when bypassing nodes.
|
||||
* @param slot The output slot index on this node
|
||||
* @param type The type of the final target input (so type list matches are accurate)
|
||||
* @returns The index of the input slot on this node, otherwise `-1`.
|
||||
*/
|
||||
private _getBypassSlotIndex(slot: number, type: ISlotType) {
|
||||
const { inputs } = this
|
||||
const oppositeInput = inputs[slot]
|
||||
const outputType = this.node.outputs[slot].type
|
||||
|
||||
// Any type short circuit - match slot ID, fallback to first slot
|
||||
if (type === '*' || type === '') {
|
||||
return inputs.length > slot ? slot : 0
|
||||
}
|
||||
|
||||
// Prefer input with the same slot ID
|
||||
if (
|
||||
oppositeInput &&
|
||||
LiteGraph.isValidConnection(oppositeInput.type, outputType) &&
|
||||
LiteGraph.isValidConnection(oppositeInput.type, type)
|
||||
) {
|
||||
return slot
|
||||
}
|
||||
|
||||
// Preserve legacy behaviour; use exact match first.
|
||||
const exactMatch = inputs.findIndex((input) => input.type === type)
|
||||
if (exactMatch !== -1) return exactMatch
|
||||
|
||||
// Find first matching slot - prefer exact type
|
||||
return inputs.findIndex(
|
||||
(input) =>
|
||||
LiteGraph.isValidConnection(input.type, outputType) &&
|
||||
LiteGraph.isValidConnection(input.type, type)
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolves the link inside a subgraph node, from the subgraph IO node to the node inside the subgraph.
|
||||
* @param slot The slot index of the output on the subgraph node.
|
||||
* @param visited A set of unique IDs to guard against infinite recursion. See {@link resolveInput}.
|
||||
* @returns A DTO for the node, and the origin ID / slot index of the output.
|
||||
*/
|
||||
private _resolveSubgraphOutput(
|
||||
slot: number,
|
||||
type: ISlotType,
|
||||
visited: Set<string>
|
||||
): ResolvedInput | undefined {
|
||||
const { node } = this
|
||||
const output = node.outputs.at(slot)
|
||||
|
||||
if (!output)
|
||||
throw new SlotIndexError(
|
||||
`No output found for flattened id [${this.id}] slot [${slot}]`
|
||||
)
|
||||
if (!node.isSubgraphNode())
|
||||
throw new TypeError(`Node is not a subgraph node: ${node.id}`)
|
||||
|
||||
// Link inside the subgraph
|
||||
const innerResolved = node.resolveSubgraphOutputLink(slot)
|
||||
if (!innerResolved) return
|
||||
|
||||
const innerNode = innerResolved.outputNode
|
||||
if (!innerNode)
|
||||
throw new Error(
|
||||
`No output node found for id [${this.id}] slot [${slot}] ${output.name}`
|
||||
)
|
||||
|
||||
// Recurse into the subgraph
|
||||
const innerNodeExecutionId: ExecutionId = [
|
||||
...this.subgraphNodePath,
|
||||
node.id,
|
||||
innerNode.id
|
||||
].join(':')
|
||||
const innerNodeDto = this.nodesByExecutionId.get(innerNodeExecutionId)
|
||||
if (!innerNodeDto)
|
||||
throw new Error(
|
||||
`No inner node DTO found for id [${innerNodeExecutionId}]`
|
||||
)
|
||||
|
||||
return innerNodeDto.resolveOutput(
|
||||
innerResolved.link.origin_slot,
|
||||
type,
|
||||
visited
|
||||
)
|
||||
}
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
// Re-export Subgraph and GraphOrSubgraph from LGraph.ts to maintain compatibility
|
||||
// This is a temporary fix to resolve circular dependency issues
|
||||
export { Subgraph, type GraphOrSubgraph } from '@/lib/litegraph/src/LGraph'
|
||||
+948
@@ -0,0 +1,948 @@
|
||||
import cloneDeep from 'es-toolkit/compat/cloneDeep'
|
||||
import type { BaseLGraph, LGraph, SubgraphId } from '@/lib/litegraph/src/LGraph'
|
||||
import type { LGraphButton } from '@/lib/litegraph/src/LGraphButton'
|
||||
import type { LGraphCanvas } from '@/lib/litegraph/src/LGraphCanvas'
|
||||
import { LGraphNode } from '@/lib/litegraph/src/LGraphNode'
|
||||
import type { DrawTitleBoxOptions } from '@/lib/litegraph/src/LGraphNode'
|
||||
import { LLink } from '@/lib/litegraph/src/LLink'
|
||||
import type { ResolvedConnection } from '@/lib/litegraph/src/LLink'
|
||||
import { NullGraphError } from '@/lib/litegraph/src/infrastructure/NullGraphError'
|
||||
import { RecursionError } from '@/lib/litegraph/src/infrastructure/RecursionError'
|
||||
import type {
|
||||
ISubgraphInput,
|
||||
IWidgetLocator
|
||||
} from '@/lib/litegraph/src/interfaces'
|
||||
import { LiteGraph } from '@/lib/litegraph/src/litegraph'
|
||||
import type { INodeInputSlot, ISlotType } from '@/lib/litegraph/src/litegraph'
|
||||
import { NodeInputSlot } from '@/lib/litegraph/src/node/NodeInputSlot'
|
||||
import { NodeOutputSlot } from '@/lib/litegraph/src/node/NodeOutputSlot'
|
||||
import { toNodeId } from '@/types/nodeId'
|
||||
import type { SerializedNodeId } from '@/types/nodeId'
|
||||
import type {
|
||||
GraphOrSubgraph,
|
||||
Subgraph
|
||||
} from '@/lib/litegraph/src/subgraph/Subgraph'
|
||||
import type {
|
||||
ExportedSubgraphInstance,
|
||||
ISerialisedNode
|
||||
} from '@/lib/litegraph/src/types/serialisation'
|
||||
import { NodeSlotType } from '@/lib/litegraph/src/types/globalEnums'
|
||||
import type {
|
||||
IBaseWidget,
|
||||
TWidgetValue
|
||||
} from '@/lib/litegraph/src/types/widgets'
|
||||
import { isWidgetValue } from '@/lib/litegraph/src/types/widgets'
|
||||
import { resolveConcretePromotedWidget } from '@/core/graph/subgraph/resolveConcretePromotedWidget'
|
||||
import { resolveSubgraphInputTarget } from '@/core/graph/subgraph/resolveSubgraphInputTarget'
|
||||
import { parsePreviewExposures } from '@/core/schemas/previewExposureSchema'
|
||||
import { parseProxyWidgetErrorQuarantine } from '@/core/schemas/proxyWidgetQuarantineSchema'
|
||||
import { usePreviewExposureStore } from '@/stores/previewExposureStore'
|
||||
import { useWidgetValueStore } from '@/stores/widgetValueStore'
|
||||
import { createNodeLocatorId } from '@/types/nodeIdentification'
|
||||
import type { WidgetId } from '@/types/widgetId'
|
||||
import { widgetId } from '@/types/widgetId'
|
||||
|
||||
import { ExecutableNodeDTO } from './ExecutableNodeDTO'
|
||||
import type { ExecutableLGraphNode, ExecutionId } from './ExecutableNodeDTO'
|
||||
import type { SubgraphInput } from './SubgraphInput'
|
||||
import { createBitmapCache } from './svgBitmapCache'
|
||||
|
||||
const workflowSvg = new Image()
|
||||
workflowSvg.src =
|
||||
"data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16' width='16' height='16'%3E%3Csvg xmlns='http://www.w3.org/2000/svg' fill='none' viewBox='0 0 16 16'%3E%3Cpath stroke='white' stroke-linecap='round' stroke-width='1.3' d='M9.18613 3.09999H6.81377M9.18613 12.9H7.55288c-3.08678 0-5.35171-2.99581-4.60305-6.08843l.3054-1.26158M14.7486 2.1721l-.5931 2.45c-.132.54533-.6065.92789-1.1508.92789h-2.2993c-.77173 0-1.33797-.74895-1.1508-1.5221l.5931-2.45c.132-.54533.6065-.9279 1.1508-.9279h2.2993c.7717 0 1.3379.74896 1.1508 1.52211Zm-8.3033 0-.59309 2.45c-.13201.54533-.60646.92789-1.15076.92789H2.4021c-.7717 0-1.33793-.74895-1.15077-1.5221l.59309-2.45c.13201-.54533.60647-.9279 1.15077-.9279h2.29935c.77169 0 1.33792.74896 1.15076 1.52211Zm8.3033 9.8-.5931 2.45c-.132.5453-.6065.9279-1.1508.9279h-2.2993c-.77173 0-1.33797-.749-1.1508-1.5221l.5931-2.45c.132-.5453.6065-.9279 1.1508-.9279h2.2993c.7717 0 1.3379.7489 1.1508 1.5221Z'/%3E%3C/svg%3E %3C/svg%3E"
|
||||
|
||||
const workflowBitmapCache = createBitmapCache(workflowSvg, 32)
|
||||
|
||||
export class SubgraphNode extends LGraphNode implements BaseLGraph {
|
||||
declare inputs: (INodeInputSlot & Partial<ISubgraphInput>)[]
|
||||
|
||||
override readonly type: SubgraphId
|
||||
override readonly isVirtualNode = true as const
|
||||
override graph: GraphOrSubgraph | null
|
||||
|
||||
get rootGraph(): LGraph {
|
||||
if (!this.graph)
|
||||
throw new NullGraphError(`SubgraphNode ${this.id} has no graph`)
|
||||
return this.graph.rootGraph
|
||||
}
|
||||
|
||||
get isDetached(): boolean {
|
||||
return !this.graph
|
||||
}
|
||||
|
||||
override get displayType(): string {
|
||||
return 'Subgraph node'
|
||||
}
|
||||
|
||||
override isSubgraphNode(): this is SubgraphNode {
|
||||
return true
|
||||
}
|
||||
|
||||
declare widgets: IBaseWidget[]
|
||||
|
||||
/**
|
||||
* Retained as a no-op for extension compatibility: promoted host widgets are
|
||||
* now store-backed and addressed by widgetId, so there is no view cache to
|
||||
* invalidate.
|
||||
*/
|
||||
invalidatePromotedViews(): void {}
|
||||
|
||||
private _eventAbortController = new AbortController()
|
||||
|
||||
constructor(
|
||||
graph: GraphOrSubgraph,
|
||||
readonly subgraph: Subgraph,
|
||||
instanceData: ExportedSubgraphInstance
|
||||
) {
|
||||
super(subgraph.name, subgraph.id)
|
||||
this.graph = graph
|
||||
|
||||
Object.defineProperty(this, 'widgets', {
|
||||
get: () =>
|
||||
this.inputs.flatMap((input) => {
|
||||
const widget = this._projectPromotedWidget(input)
|
||||
return widget ? [widget] : []
|
||||
}),
|
||||
set: () => {
|
||||
if (import.meta.env.DEV)
|
||||
console.warn(
|
||||
'Cannot manually set widgets on SubgraphNode; use the promotion system.'
|
||||
)
|
||||
},
|
||||
configurable: true,
|
||||
enumerable: true
|
||||
})
|
||||
|
||||
const subgraphEvents = this.subgraph.events
|
||||
const { signal } = this._eventAbortController
|
||||
|
||||
subgraphEvents.addEventListener(
|
||||
'input-added',
|
||||
(e) => {
|
||||
const subgraphInput = e.detail.input
|
||||
const { name, type } = subgraphInput
|
||||
const existingInput = this.inputs.find(
|
||||
(input) =>
|
||||
input._subgraphSlot === subgraphInput ||
|
||||
(input._subgraphSlot && input._subgraphSlot.id === subgraphInput.id)
|
||||
)
|
||||
if (existingInput) {
|
||||
this._addSubgraphInputListeners(subgraphInput, existingInput)
|
||||
const linkId = subgraphInput.linkIds[0]
|
||||
if (linkId === undefined) return
|
||||
|
||||
const link = this.subgraph.getLink(linkId)
|
||||
if (!link) return
|
||||
|
||||
const { inputNode, input } = link.resolve(subgraph)
|
||||
if (!inputNode || !input) return
|
||||
|
||||
const widget = inputNode.getWidgetFromSlot(input)
|
||||
if (widget)
|
||||
this._setWidget(subgraphInput, existingInput, widget, input.widget)
|
||||
return
|
||||
}
|
||||
const input = this.addInput(name, type, {
|
||||
_subgraphSlot: subgraphInput
|
||||
})
|
||||
this.invalidatePromotedViews()
|
||||
|
||||
this._addSubgraphInputListeners(subgraphInput, input)
|
||||
},
|
||||
{ signal }
|
||||
)
|
||||
|
||||
subgraphEvents.addEventListener(
|
||||
'removing-input',
|
||||
(e) => {
|
||||
const widget = e.detail.input._widget
|
||||
if (widget) this.ensureWidgetRemoved(widget)
|
||||
|
||||
this.removeInput(e.detail.index)
|
||||
this.invalidatePromotedViews()
|
||||
this.setDirtyCanvas(true, true)
|
||||
},
|
||||
{ signal }
|
||||
)
|
||||
|
||||
subgraphEvents.addEventListener(
|
||||
'output-added',
|
||||
(e) => {
|
||||
const { name, type } = e.detail.output
|
||||
this.addOutput(name, type)
|
||||
},
|
||||
{ signal }
|
||||
)
|
||||
|
||||
subgraphEvents.addEventListener(
|
||||
'removing-output',
|
||||
(e) => {
|
||||
this.removeOutput(e.detail.index)
|
||||
this.setDirtyCanvas(true, true)
|
||||
},
|
||||
{ signal }
|
||||
)
|
||||
|
||||
subgraphEvents.addEventListener(
|
||||
'renaming-input',
|
||||
(e) => {
|
||||
const { index, newName } = e.detail
|
||||
const input = this.inputs.at(index)
|
||||
if (!input) throw new Error('Subgraph input not found')
|
||||
|
||||
input.label = newName
|
||||
// Do NOT change input.widget.name — it is the stable internal
|
||||
// identifier used by onGraphConfigured (widgetInputs.ts) to match
|
||||
// inputs to widgets. Changing it to the display label would cause
|
||||
// collisions when two promoted inputs share the same label.
|
||||
if (input._widget) input._widget.label = newName
|
||||
if (input.widgetId) {
|
||||
const state = useWidgetValueStore().getWidget(input.widgetId)
|
||||
if (state) state.label = newName
|
||||
}
|
||||
this.invalidatePromotedViews()
|
||||
this.graph?.trigger('node:slot-label:changed', {
|
||||
nodeId: this.id,
|
||||
slotType: NodeSlotType.INPUT
|
||||
})
|
||||
},
|
||||
{ signal }
|
||||
)
|
||||
|
||||
subgraphEvents.addEventListener(
|
||||
'renaming-output',
|
||||
(e) => {
|
||||
const { index, newName } = e.detail
|
||||
const output = this.outputs.at(index)
|
||||
if (!output) throw new Error('Subgraph output not found')
|
||||
|
||||
output.label = newName
|
||||
this.graph?.trigger('node:slot-label:changed', {
|
||||
nodeId: this.id,
|
||||
slotType: NodeSlotType.OUTPUT
|
||||
})
|
||||
},
|
||||
{ signal }
|
||||
)
|
||||
|
||||
this.type = subgraph.id
|
||||
this.configure(instanceData)
|
||||
|
||||
this.addTitleButton({
|
||||
name: 'enter_subgraph',
|
||||
text: '\uE93B', // Unicode for pi-window-maximize
|
||||
yOffset: 0, // No vertical offset needed, button is centered
|
||||
xOffset: -10,
|
||||
fontSize: 16
|
||||
})
|
||||
}
|
||||
|
||||
override onTitleButtonClick(
|
||||
button: LGraphButton,
|
||||
canvas: LGraphCanvas
|
||||
): void {
|
||||
if (button.name === 'enter_subgraph') {
|
||||
canvas.openSubgraph(this.subgraph, this)
|
||||
} else {
|
||||
super.onTitleButtonClick(button, canvas)
|
||||
}
|
||||
}
|
||||
|
||||
private _projectPromotedWidget(
|
||||
input: INodeInputSlot
|
||||
): IBaseWidget | undefined {
|
||||
if (input._widget) return input._widget
|
||||
|
||||
const id = input.widgetId
|
||||
if (!id) return
|
||||
|
||||
const store = useWidgetValueStore()
|
||||
const widget: IBaseWidget = {
|
||||
get name() {
|
||||
return store.getWidget(id)?.name ?? input.name
|
||||
},
|
||||
get label() {
|
||||
return store.getWidget(id)?.label ?? input.label ?? input.name
|
||||
},
|
||||
set label(next) {
|
||||
const state = store.getWidget(id)
|
||||
if (state) state.label = next
|
||||
},
|
||||
get y() {
|
||||
return store.getWidget(id)?.y ?? 0
|
||||
},
|
||||
set y(next) {
|
||||
const state = store.getWidget(id)
|
||||
if (state) state.y = next
|
||||
},
|
||||
get type() {
|
||||
return store.getWidget(id)?.type ?? 'text'
|
||||
},
|
||||
get options() {
|
||||
return store.getWidget(id)?.options ?? {}
|
||||
},
|
||||
get value() {
|
||||
const value = store.getWidget(id)?.value
|
||||
return isWidgetValue(value) ? value : undefined
|
||||
},
|
||||
set value(next) {
|
||||
store.setValue(id, next)
|
||||
},
|
||||
// Canvas edits operate on a transient concrete widget (toConcreteWidget),
|
||||
// so the value setter above is never invoked; BaseWidget.setValue writes
|
||||
// its own local state and then calls this callback, which is the only
|
||||
// bridge back to the store.
|
||||
callback(next) {
|
||||
store.setValue(id, next)
|
||||
}
|
||||
}
|
||||
Object.defineProperty(widget, 'widgetId', {
|
||||
value: id,
|
||||
enumerable: false,
|
||||
configurable: true
|
||||
})
|
||||
input._widget = widget
|
||||
return input._widget
|
||||
}
|
||||
|
||||
private _addSubgraphInputListeners(
|
||||
subgraphInput: SubgraphInput,
|
||||
input: INodeInputSlot & Partial<ISubgraphInput>
|
||||
) {
|
||||
input._subgraphSlot = subgraphInput
|
||||
|
||||
if (
|
||||
input._listenerController &&
|
||||
typeof input._listenerController.abort === 'function'
|
||||
) {
|
||||
input._listenerController.abort()
|
||||
}
|
||||
input._listenerController = new AbortController()
|
||||
const { signal } = input._listenerController
|
||||
|
||||
subgraphInput.events.addEventListener(
|
||||
'input-connected',
|
||||
(e) => {
|
||||
input.shape = this.getSlotShape(subgraphInput, e.detail.input)
|
||||
if (!e.detail.widget || !e.detail.node) return
|
||||
|
||||
this._setWidget(
|
||||
subgraphInput,
|
||||
input,
|
||||
e.detail.widget,
|
||||
e.detail.input.widget
|
||||
)
|
||||
},
|
||||
{ signal }
|
||||
)
|
||||
|
||||
subgraphInput.events.addEventListener(
|
||||
'input-disconnected',
|
||||
() => {
|
||||
this.invalidatePromotedViews()
|
||||
input.shape = this.getSlotShape(subgraphInput)
|
||||
|
||||
const connectedWidgets = subgraphInput.getConnectedWidgets()
|
||||
if (connectedWidgets.length > 0) {
|
||||
this._resolveInputWidget(subgraphInput, input)
|
||||
this.invalidatePromotedViews()
|
||||
return
|
||||
}
|
||||
|
||||
if (input._widget) this.ensureWidgetRemoved(input._widget)
|
||||
if (input.widgetId) {
|
||||
const id = input.widgetId
|
||||
queueMicrotask(() => {
|
||||
if (this.inputs.some((input) => input.widgetId === id)) return
|
||||
useWidgetValueStore().deleteWidget(id)
|
||||
})
|
||||
}
|
||||
|
||||
delete input.pos
|
||||
delete input.widget
|
||||
delete input.widgetId
|
||||
input._widget = undefined
|
||||
this.invalidatePromotedViews()
|
||||
},
|
||||
{ signal }
|
||||
)
|
||||
}
|
||||
|
||||
private _rebindInputSubgraphSlots(): void {
|
||||
this.invalidatePromotedViews()
|
||||
|
||||
const subgraphSlots = [...this.subgraph.inputNode.slots]
|
||||
const slotsBySignature = new Map<string, SubgraphInput[]>()
|
||||
const slotsByName = new Map<string, SubgraphInput[]>()
|
||||
|
||||
for (const slot of subgraphSlots) {
|
||||
const signature = `${slot.name}:${String(slot.type)}`
|
||||
const signatureSlots = slotsBySignature.get(signature)
|
||||
if (signatureSlots) {
|
||||
signatureSlots.push(slot)
|
||||
} else {
|
||||
slotsBySignature.set(signature, [slot])
|
||||
}
|
||||
|
||||
const nameSlots = slotsByName.get(slot.name)
|
||||
if (nameSlots) {
|
||||
nameSlots.push(slot)
|
||||
} else {
|
||||
slotsByName.set(slot.name, [slot])
|
||||
}
|
||||
}
|
||||
|
||||
const assignedSlotIds = new Set<string>()
|
||||
const takeUnassignedSlot = (
|
||||
slots: SubgraphInput[] | undefined
|
||||
): SubgraphInput | undefined => {
|
||||
if (!slots) return undefined
|
||||
return slots.find((slot) => !assignedSlotIds.has(String(slot.id)))
|
||||
}
|
||||
|
||||
for (const input of this.inputs) {
|
||||
const existingSlot = input._subgraphSlot
|
||||
if (
|
||||
existingSlot &&
|
||||
this.subgraph.inputNode.slots.some((slot) => slot === existingSlot)
|
||||
) {
|
||||
assignedSlotIds.add(String(existingSlot.id))
|
||||
continue
|
||||
}
|
||||
|
||||
const signature = `${input.name}:${String(input.type)}`
|
||||
const matchedSlot =
|
||||
takeUnassignedSlot(slotsBySignature.get(signature)) ??
|
||||
takeUnassignedSlot(slotsByName.get(input.name))
|
||||
|
||||
if (matchedSlot) {
|
||||
input._subgraphSlot = matchedSlot
|
||||
assignedSlotIds.add(String(matchedSlot.id))
|
||||
} else {
|
||||
delete input._subgraphSlot
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override configure(info: ExportedSubgraphInstance): void {
|
||||
for (const input of this.inputs) {
|
||||
if (
|
||||
input._listenerController &&
|
||||
typeof input._listenerController.abort === 'function'
|
||||
) {
|
||||
input._listenerController.abort()
|
||||
}
|
||||
}
|
||||
|
||||
this.inputs.length = 0
|
||||
this.inputs.push(
|
||||
...this.subgraph.inputNode.slots.map((slot) =>
|
||||
Object.assign(
|
||||
new NodeInputSlot(
|
||||
{
|
||||
name: slot.name,
|
||||
localized_name: slot.localized_name,
|
||||
label: slot.label,
|
||||
shape: this.getSlotShape(slot),
|
||||
type: slot.type,
|
||||
link: null
|
||||
},
|
||||
this
|
||||
),
|
||||
{
|
||||
_subgraphSlot: slot
|
||||
}
|
||||
)
|
||||
)
|
||||
)
|
||||
this.invalidatePromotedViews()
|
||||
|
||||
this.outputs.length = 0
|
||||
this.outputs.push(
|
||||
...this.subgraph.outputNode.slots.map(
|
||||
(slot) =>
|
||||
new NodeOutputSlot(
|
||||
{
|
||||
name: slot.name,
|
||||
localized_name: slot.localized_name,
|
||||
label: slot.label,
|
||||
type: slot.type,
|
||||
links: null
|
||||
},
|
||||
this
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
super.configure(info)
|
||||
this._applyPromotedWidgetValues(info.widgets_values)
|
||||
}
|
||||
|
||||
private _applyPromotedWidgetValues(
|
||||
widgetValues: ExportedSubgraphInstance['widgets_values']
|
||||
): void {
|
||||
const quarantineValuesByInputName = this._readQuarantineHostValuesByName()
|
||||
|
||||
let valueIndex = 0
|
||||
for (const input of this.inputs) {
|
||||
if (!input.widgetId) continue
|
||||
const value =
|
||||
quarantineValuesByInputName.get(input.name) ??
|
||||
widgetValues?.[valueIndex]
|
||||
if (value !== undefined) {
|
||||
useWidgetValueStore().setValue(input.widgetId, value)
|
||||
}
|
||||
valueIndex += 1
|
||||
}
|
||||
}
|
||||
|
||||
private _readQuarantineHostValuesByName(): Map<string, TWidgetValue> {
|
||||
return new Map(
|
||||
parseProxyWidgetErrorQuarantine(
|
||||
this.properties.proxyWidgetErrorQuarantine
|
||||
)
|
||||
.toReversed()
|
||||
.flatMap(({ originalEntry: [sourceNodeId, name], hostValue }) =>
|
||||
sourceNodeId === '-1' &&
|
||||
hostValue !== undefined &&
|
||||
isWidgetValue(hostValue)
|
||||
? [[name, hostValue] as const]
|
||||
: []
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
override _internalConfigureAfterSlots() {
|
||||
this._rebindInputSubgraphSlots()
|
||||
|
||||
this.inputs = this.inputs.filter((input) => input._subgraphSlot)
|
||||
this.invalidatePromotedViews()
|
||||
|
||||
this._hydratePreviewExposures()
|
||||
|
||||
for (const input of this.inputs) {
|
||||
const subgraphInput = input._subgraphSlot
|
||||
if (!subgraphInput) {
|
||||
console.warn(
|
||||
`[SubgraphNode.configure] No subgraph input found for input ${input.name}, skipping`
|
||||
)
|
||||
continue
|
||||
}
|
||||
|
||||
this._addSubgraphInputListeners(subgraphInput, input)
|
||||
this._resolveInputWidget(subgraphInput, input)
|
||||
}
|
||||
}
|
||||
|
||||
private _hydratePreviewExposures() {
|
||||
const store = usePreviewExposureStore()
|
||||
const rootGraphId = this.rootGraph.id
|
||||
const hostLocator = String(this.id)
|
||||
const rawProperty = this.properties.previewExposures
|
||||
const hasExplicitProperty = Array.isArray(rawProperty)
|
||||
const fromProperty = parsePreviewExposures(rawProperty)
|
||||
if (fromProperty.length) {
|
||||
store.setExposures(rootGraphId, hostLocator, fromProperty)
|
||||
return
|
||||
}
|
||||
if (hasExplicitProperty) {
|
||||
store.setExposures(rootGraphId, hostLocator, [])
|
||||
return
|
||||
}
|
||||
const legacyKey = createNodeLocatorId(null, this.id)
|
||||
const legacy = store.getExposures(rootGraphId, legacyKey)
|
||||
if (legacy.length) {
|
||||
store.setExposures(rootGraphId, hostLocator, [...legacy])
|
||||
return
|
||||
}
|
||||
store.setExposures(rootGraphId, hostLocator, [])
|
||||
}
|
||||
|
||||
rebuildInputWidgetBindings(): void {
|
||||
this.invalidatePromotedViews()
|
||||
|
||||
for (const input of this.inputs) {
|
||||
delete input.widget
|
||||
delete input.pos
|
||||
delete input.widgetId
|
||||
this._clearPromotedWidget(input)
|
||||
const subgraphInput = input._subgraphSlot
|
||||
if (!subgraphInput) continue
|
||||
this._resolveInputWidget(subgraphInput, input)
|
||||
}
|
||||
|
||||
this.invalidatePromotedViews()
|
||||
}
|
||||
|
||||
private _resolveInputWidget(
|
||||
subgraphInput: SubgraphInput,
|
||||
input: INodeInputSlot
|
||||
) {
|
||||
for (const linkId of subgraphInput.linkIds) {
|
||||
const link = this.subgraph.getLink(linkId)
|
||||
if (!link) {
|
||||
console.warn(
|
||||
`[SubgraphNode.configure] No link found for link ID ${linkId}`,
|
||||
this
|
||||
)
|
||||
continue
|
||||
}
|
||||
|
||||
const { inputNode } = link.resolve(this.subgraph)
|
||||
if (!inputNode) {
|
||||
console.warn('Failed to resolve inputNode', link, this)
|
||||
continue
|
||||
}
|
||||
|
||||
const targetInput = inputNode.inputs.find((inp) => inp.link === linkId)
|
||||
if (!targetInput) {
|
||||
console.warn('Failed to find corresponding input', link, inputNode)
|
||||
continue
|
||||
}
|
||||
|
||||
const widget = inputNode.getWidgetFromSlot(targetInput)
|
||||
if (widget) {
|
||||
this._setWidget(subgraphInput, input, widget, targetInput.widget)
|
||||
break
|
||||
}
|
||||
|
||||
// Nested promotion: the source is itself a promoted subgraph input with
|
||||
// no concrete widget. Resolve the deepest interior widget so this input
|
||||
// still receives widget state and a widgetId.
|
||||
const nested = this._resolveNestedPromotedSource(inputNode, targetInput)
|
||||
if (nested) {
|
||||
this._setWidget(subgraphInput, input, nested.widget, targetInput.widget)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private _resolveNestedPromotedSource(
|
||||
inputNode: LGraphNode,
|
||||
targetInput: INodeInputSlot
|
||||
): { node: LGraphNode; widget: IBaseWidget } | undefined {
|
||||
if (!inputNode.isSubgraphNode()) return undefined
|
||||
|
||||
const target = resolveSubgraphInputTarget(inputNode, targetInput.name)
|
||||
if (!target) return undefined
|
||||
|
||||
const resolved = resolveConcretePromotedWidget(
|
||||
inputNode,
|
||||
target.nodeId,
|
||||
target.widgetName
|
||||
)
|
||||
return resolved.status === 'resolved' ? resolved.resolved : undefined
|
||||
}
|
||||
|
||||
private _setWidget(
|
||||
subgraphInput: Readonly<SubgraphInput>,
|
||||
input: INodeInputSlot,
|
||||
interiorWidget: Readonly<IBaseWidget>,
|
||||
inputWidget: IWidgetLocator | undefined
|
||||
) {
|
||||
this.invalidatePromotedViews()
|
||||
|
||||
this._clearPromotedWidget(input)
|
||||
|
||||
input.widget ??= { name: subgraphInput.name }
|
||||
input.widget.name = subgraphInput.name
|
||||
if (inputWidget) Object.setPrototypeOf(input.widget, inputWidget)
|
||||
|
||||
const id = widgetId(this.rootGraph.id, this.id, subgraphInput.name)
|
||||
input.widgetId = id
|
||||
useWidgetValueStore().registerWidget(id, {
|
||||
type: interiorWidget.type,
|
||||
value: interiorWidget.value,
|
||||
options: cloneDeep(interiorWidget.options ?? {}),
|
||||
label: input.label ?? subgraphInput.name,
|
||||
serialize: interiorWidget.serialize,
|
||||
disabled: interiorWidget.disabled,
|
||||
isDOMWidget:
|
||||
'isDOMWidget' in interiorWidget &&
|
||||
typeof interiorWidget.isDOMWidget === 'boolean'
|
||||
? interiorWidget.isDOMWidget
|
||||
: undefined
|
||||
})
|
||||
input._widget =
|
||||
this.createPromotedHostWidget(input, id, interiorWidget) ??
|
||||
this._projectPromotedWidget(input)
|
||||
this._setConcreteSlots()
|
||||
|
||||
this.subgraph.events.dispatch('widget-promoted', {
|
||||
widget: interiorWidget,
|
||||
subgraphNode: this
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* App-layer hook to build a promoted DOM widget; the default falls back to the
|
||||
* store-backed projection. Litegraph core stays free of Vue/Pinia/DOM.
|
||||
*/
|
||||
protected createPromotedHostWidget(
|
||||
_input: INodeInputSlot,
|
||||
_id: WidgetId,
|
||||
_sourceWidget: Readonly<IBaseWidget>
|
||||
): IBaseWidget | undefined {
|
||||
return undefined
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs the host widget's `onRemove` (unregistering DOM widgets) and clears it.
|
||||
* Unlike {@link ensureWidgetRemoved}, dispatches no demotion event, so it is
|
||||
* safe on re-resolution.
|
||||
*/
|
||||
private _clearPromotedWidget(input: INodeInputSlot): void {
|
||||
input._widget?.onRemove?.()
|
||||
input._widget = undefined
|
||||
}
|
||||
|
||||
override onAdded(_graph: LGraph): void {
|
||||
this.invalidatePromotedViews()
|
||||
}
|
||||
|
||||
/**
|
||||
* @param name The name of the input slot.
|
||||
* @param type The type of the input slot.
|
||||
* @param inputProperties Properties that are directly assigned to the created input. Default: a new, empty object.
|
||||
* @returns The new input slot.
|
||||
*/
|
||||
override addInput<TInput extends Partial<ISubgraphInput>>(
|
||||
name: string,
|
||||
type: ISlotType,
|
||||
inputProperties: TInput = {} as TInput
|
||||
): INodeInputSlot & TInput {
|
||||
return super.addInput(name, type, inputProperties)
|
||||
}
|
||||
|
||||
override getSlotFromWidget(
|
||||
widget: IBaseWidget | undefined
|
||||
): INodeInputSlot | undefined {
|
||||
if (widget?.widgetId) {
|
||||
const promotedInput = this.inputs.find(
|
||||
(input) => input.widgetId === widget.widgetId
|
||||
)
|
||||
if (promotedInput) return promotedInput
|
||||
}
|
||||
return super.getSlotFromWidget(widget)
|
||||
}
|
||||
|
||||
override getWidgetFromSlot(slot: INodeInputSlot): IBaseWidget | undefined {
|
||||
if (slot._widget) return slot._widget
|
||||
return super.getWidgetFromSlot(slot)
|
||||
}
|
||||
|
||||
override getInputLink(slot: number): LLink | null {
|
||||
const innerLink = this.subgraph.outputNode.slots[slot].getLinks().at(0)
|
||||
if (!innerLink) {
|
||||
console.warn(
|
||||
`SubgraphNode.getInputLink: no inner link found for slot ${slot}`
|
||||
)
|
||||
return null
|
||||
}
|
||||
|
||||
const newLink = LLink.create(innerLink)
|
||||
newLink.origin_id = toNodeId(`${this.id}:${innerLink.origin_id}`)
|
||||
newLink.origin_slot = innerLink.origin_slot
|
||||
|
||||
return newLink
|
||||
}
|
||||
|
||||
/**
|
||||
* @param slot The slot index
|
||||
* @returns The resolved connections, or undefined if no input node is found.
|
||||
*/
|
||||
resolveSubgraphInputLinks(slot: number): ResolvedConnection[] {
|
||||
const inputSlot = this.subgraph.inputNode.slots[slot]
|
||||
const innerLinks = inputSlot.getLinks()
|
||||
if (innerLinks.length === 0) {
|
||||
console.warn(
|
||||
`[SubgraphNode.resolveSubgraphInputLinks] No inner links found for input slot [${slot}] ${inputSlot.name}`,
|
||||
this
|
||||
)
|
||||
return []
|
||||
}
|
||||
return innerLinks.map((link) => link.resolve(this.subgraph))
|
||||
}
|
||||
|
||||
/**
|
||||
* @param slot The slot index
|
||||
* @returns The output node if found, otherwise undefined.
|
||||
*/
|
||||
resolveSubgraphOutputLink(slot: number): ResolvedConnection | undefined {
|
||||
const outputSlot = this.subgraph.outputNode.slots[slot]
|
||||
const innerLink = outputSlot.getLinks().at(0)
|
||||
if (innerLink) {
|
||||
return innerLink.resolve(this.subgraph)
|
||||
}
|
||||
console.warn(
|
||||
`[SubgraphNode.resolveSubgraphOutputLink] No inner link found for output slot [${slot}] ${outputSlot.name}`,
|
||||
this
|
||||
)
|
||||
}
|
||||
|
||||
override getInnerNodes(
|
||||
executableNodes: Map<ExecutionId, ExecutableLGraphNode>,
|
||||
subgraphNodePath: readonly SerializedNodeId[] = [],
|
||||
nodes: ExecutableLGraphNode[] = [],
|
||||
visited = new Set<SubgraphNode>()
|
||||
): ExecutableLGraphNode[] {
|
||||
if (visited.has(this)) {
|
||||
const nodeInfo = `${this.id}${this.title ? ` (${this.title})` : ''}`
|
||||
const subgraphInfo = `'${this.subgraph.name || 'Unnamed Subgraph'}'`
|
||||
const depth = subgraphNodePath.length
|
||||
throw new RecursionError(
|
||||
`Circular reference detected at depth ${depth} in node ${nodeInfo} of subgraph ${subgraphInfo}. ` +
|
||||
`This creates an infinite loop in the subgraph hierarchy.`
|
||||
)
|
||||
}
|
||||
visited.add(this)
|
||||
|
||||
const subgraphInstanceIdPath = [...subgraphNodePath, this.id]
|
||||
|
||||
const parentSubgraphNode = this.rootGraph
|
||||
.resolveSubgraphIdPath(subgraphNodePath.map(toNodeId))
|
||||
.at(-1)
|
||||
const subgraphNodeDto = new ExecutableNodeDTO(
|
||||
this,
|
||||
subgraphNodePath,
|
||||
executableNodes,
|
||||
parentSubgraphNode
|
||||
)
|
||||
executableNodes.set(subgraphNodeDto.id, subgraphNodeDto)
|
||||
|
||||
for (const node of this.subgraph.nodes) {
|
||||
if ('getInnerNodes' in node && node.getInnerNodes) {
|
||||
node.getInnerNodes(
|
||||
executableNodes,
|
||||
subgraphInstanceIdPath,
|
||||
nodes,
|
||||
new Set(visited)
|
||||
)
|
||||
} else {
|
||||
const aVeryRealNode = new ExecutableNodeDTO(
|
||||
node,
|
||||
subgraphInstanceIdPath,
|
||||
executableNodes,
|
||||
this
|
||||
)
|
||||
executableNodes.set(aVeryRealNode.id, aVeryRealNode)
|
||||
nodes.push(aVeryRealNode)
|
||||
}
|
||||
}
|
||||
return nodes
|
||||
}
|
||||
|
||||
override removeWidget(widget: IBaseWidget): void {
|
||||
this.ensureWidgetRemoved(widget)
|
||||
}
|
||||
|
||||
override ensureWidgetRemoved(widget: IBaseWidget): void {
|
||||
widget.onRemove?.()
|
||||
this.subgraph.events.dispatch('widget-demoted', {
|
||||
widget,
|
||||
subgraphNode: this
|
||||
})
|
||||
}
|
||||
|
||||
override onRemoved(): void {
|
||||
this._eventAbortController.abort()
|
||||
|
||||
for (const input of this.inputs) {
|
||||
if (
|
||||
input._listenerController &&
|
||||
typeof input._listenerController.abort === 'function'
|
||||
) {
|
||||
input._listenerController.abort()
|
||||
}
|
||||
this._clearPromotedWidget(input)
|
||||
}
|
||||
}
|
||||
override drawTitleBox(
|
||||
ctx: CanvasRenderingContext2D,
|
||||
{
|
||||
scale,
|
||||
low_quality = false,
|
||||
title_height = LiteGraph.NODE_TITLE_HEIGHT,
|
||||
box_size = 10
|
||||
}: DrawTitleBoxOptions
|
||||
): void {
|
||||
if (this.onDrawTitleBox) {
|
||||
this.onDrawTitleBox(ctx, title_height, this.renderingSize, scale)
|
||||
return
|
||||
}
|
||||
ctx.save()
|
||||
ctx.fillStyle = '#3b82f6'
|
||||
ctx.beginPath()
|
||||
ctx.roundRect(6, -24.5, 22, 20, 5)
|
||||
ctx.fill()
|
||||
if (!low_quality) {
|
||||
ctx.translate(25, 23)
|
||||
ctx.scale(-1.5, 1.5)
|
||||
ctx.drawImage(
|
||||
workflowBitmapCache.get(),
|
||||
0,
|
||||
-title_height,
|
||||
box_size,
|
||||
box_size
|
||||
)
|
||||
}
|
||||
ctx.restore()
|
||||
}
|
||||
|
||||
override serialize(): ISerialisedNode {
|
||||
const serialized = super.serialize()
|
||||
const serializedProperties = { ...(serialized.properties ?? {}) }
|
||||
const rootGraphId = this.rootGraph.id
|
||||
const hostLocator = String(this.id)
|
||||
|
||||
const previewExposures = usePreviewExposureStore().getExposures(
|
||||
rootGraphId,
|
||||
hostLocator
|
||||
)
|
||||
serializedProperties.previewExposures = previewExposures.map((entry) => ({
|
||||
...entry
|
||||
}))
|
||||
|
||||
const quarantine = parseProxyWidgetErrorQuarantine(
|
||||
this.properties.proxyWidgetErrorQuarantine
|
||||
)
|
||||
if (quarantine.length === 0) {
|
||||
delete serializedProperties.proxyWidgetErrorQuarantine
|
||||
} else {
|
||||
serializedProperties.proxyWidgetErrorQuarantine = quarantine.map(
|
||||
(entry) => ({ ...entry })
|
||||
)
|
||||
}
|
||||
|
||||
serialized.properties = serializedProperties
|
||||
|
||||
const widgetValues = this.inputs.flatMap((input) => {
|
||||
if (!input.widgetId) return []
|
||||
const value = useWidgetValueStore().getWidget(input.widgetId)?.value
|
||||
return [isWidgetValue(value) ? value : undefined]
|
||||
})
|
||||
|
||||
if (widgetValues.some((value) => value !== undefined)) {
|
||||
serialized.widgets_values = widgetValues
|
||||
} else {
|
||||
delete serialized.widgets_values
|
||||
}
|
||||
|
||||
return serialized
|
||||
}
|
||||
|
||||
override clone() {
|
||||
const clone = super.clone()
|
||||
|
||||
// TODO: Consider deep cloning subgraphs here.
|
||||
|
||||
return clone
|
||||
}
|
||||
getSlotShape(slot: SubgraphInput, extraInput?: INodeInputSlot) {
|
||||
const shapes = slot.linkIds.map(
|
||||
(id) => this.subgraph.links[id]?.resolve(this.subgraph)?.input?.shape
|
||||
)
|
||||
if (extraInput) shapes.push(extraInput.shape)
|
||||
return shapes.every((shape) => shape === shapes[0]) ? shapes[0] : undefined
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,160 @@
|
||||
import type {
|
||||
ExecutableLGraphNode,
|
||||
ExecutionId,
|
||||
LGraph
|
||||
} from '@/lib/litegraph/src/litegraph'
|
||||
import {
|
||||
ExecutableNodeDTO,
|
||||
LGraphEventMode
|
||||
} from '@/lib/litegraph/src/litegraph'
|
||||
import type {
|
||||
ComfyApiWorkflow,
|
||||
ComfyWorkflowJSON
|
||||
} from '@/platform/workflow/validation/schemas/workflowSchema'
|
||||
|
||||
import { compressWidgetInputSlots } from './litegraphUtil'
|
||||
|
||||
/**
|
||||
* Converts the current graph workflow for sending to the API.
|
||||
* @note Node widgets are updated before serialization to prepare queueing.
|
||||
*
|
||||
* @param graph The graph to convert.
|
||||
* @param options The options for the conversion.
|
||||
* - `sortNodes`: Whether to sort the nodes by execution order.
|
||||
* @returns The workflow and node links
|
||||
*/
|
||||
export const graphToPrompt = async (
|
||||
graph: LGraph,
|
||||
options: { sortNodes?: boolean } = {}
|
||||
): Promise<{ workflow: ComfyWorkflowJSON; output: ComfyApiWorkflow }> => {
|
||||
const { sortNodes = false } = options
|
||||
|
||||
for (const node of graph.computeExecutionOrder(false)) {
|
||||
const innerNodes = node.getInnerNodes
|
||||
? node.getInnerNodes(new Map())
|
||||
: [node]
|
||||
for (const innerNode of innerNodes) {
|
||||
if (innerNode.isVirtualNode) {
|
||||
innerNode.applyToGraph?.()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const workflow = graph.serialize({ sortNodes })
|
||||
|
||||
// Remove localized_name from the workflow
|
||||
for (const node of workflow.nodes) {
|
||||
for (const slot of node.inputs ?? []) {
|
||||
delete slot.localized_name
|
||||
}
|
||||
for (const slot of node.outputs ?? []) {
|
||||
delete slot.localized_name
|
||||
}
|
||||
}
|
||||
|
||||
compressWidgetInputSlots(workflow)
|
||||
workflow.extra ??= {}
|
||||
workflow.extra.frontendVersion = __COMFYUI_FRONTEND_VERSION__
|
||||
|
||||
const nodeDtoMap = new Map<ExecutionId, ExecutableLGraphNode>()
|
||||
for (const node of graph.computeExecutionOrder(false)) {
|
||||
const dto: ExecutableLGraphNode = new ExecutableNodeDTO(
|
||||
node,
|
||||
[],
|
||||
nodeDtoMap
|
||||
)
|
||||
|
||||
nodeDtoMap.set(dto.id, dto)
|
||||
|
||||
if (
|
||||
node.mode === LGraphEventMode.NEVER ||
|
||||
node.mode === LGraphEventMode.BYPASS
|
||||
) {
|
||||
continue
|
||||
}
|
||||
|
||||
for (const innerNode of dto.getInnerNodes()) {
|
||||
nodeDtoMap.set(innerNode.id, innerNode)
|
||||
}
|
||||
}
|
||||
|
||||
const output: ComfyApiWorkflow = {}
|
||||
// Process nodes in order of execution
|
||||
for (const node of nodeDtoMap.values()) {
|
||||
// Don't serialize muted nodes
|
||||
if (
|
||||
node.isVirtualNode ||
|
||||
node.mode === LGraphEventMode.NEVER ||
|
||||
node.mode === LGraphEventMode.BYPASS
|
||||
) {
|
||||
continue
|
||||
}
|
||||
|
||||
const inputs: ComfyApiWorkflow[string]['inputs'] = {}
|
||||
const { widgets } = node
|
||||
|
||||
// Store all widget values in the API prompt.
|
||||
// Note: widget.options.serialize controls prompt inclusion (checked here).
|
||||
// widget.serialize controls workflow persistence (checked by LGraphNode).
|
||||
if (widgets) {
|
||||
for (const [i, widget] of widgets.entries()) {
|
||||
if (!widget.name || widget.options?.serialize === false) continue
|
||||
|
||||
const widgetValue = widget.serializeValue
|
||||
? await widget.serializeValue(node, i)
|
||||
: widget.value
|
||||
// By default, Array values are reserved to represent node connections.
|
||||
// We need to wrap the array as an object to avoid the misinterpretation
|
||||
// of the array as a node connection.
|
||||
// The backend automatically unwraps the object to an array during
|
||||
// execution.
|
||||
inputs[widget.name] = Array.isArray(widgetValue)
|
||||
? widget.type === 'curve'
|
||||
? { __type__: 'CURVE', __value__: widgetValue }
|
||||
: { __value__: widgetValue }
|
||||
: widgetValue
|
||||
}
|
||||
}
|
||||
|
||||
// Store all node links
|
||||
for (const [i, input] of node.inputs.entries()) {
|
||||
const resolvedInput = node.resolveInput(i)
|
||||
if (!resolvedInput) continue
|
||||
|
||||
// Resolved to an actual widget value rather than a node connection
|
||||
if (resolvedInput.widgetInfo) {
|
||||
const { value } = resolvedInput.widgetInfo
|
||||
inputs[input.name] = Array.isArray(value) ? { __value__: value } : value
|
||||
continue
|
||||
}
|
||||
|
||||
inputs[input.name] = [
|
||||
String(resolvedInput.origin_id),
|
||||
// @ts-expect-error link.origin_slot is already number.
|
||||
parseInt(resolvedInput.origin_slot)
|
||||
]
|
||||
}
|
||||
|
||||
output[String(node.id)] = {
|
||||
inputs,
|
||||
// TODO(huchenlei): Filter out all nodes that cannot be mapped to a
|
||||
// comfyClass.
|
||||
class_type: node.comfyClass!,
|
||||
// Ignored by the backend.
|
||||
_meta: {
|
||||
title: node.title
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Remove inputs connected to removed nodes
|
||||
for (const { inputs } of Object.values(output)) {
|
||||
for (const [i, input] of Object.entries(inputs)) {
|
||||
if (Array.isArray(input) && input.length === 2 && !output[input[0]]) {
|
||||
delete inputs[i]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return { workflow: workflow as ComfyWorkflowJSON, output }
|
||||
}
|
||||
@@ -0,0 +1,674 @@
|
||||
GNU GENERAL PUBLIC LICENSE
|
||||
Version 3, 29 June 2007
|
||||
|
||||
Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
|
||||
Everyone is permitted to copy and distribute verbatim copies
|
||||
of this license document, but changing it is not allowed.
|
||||
|
||||
Preamble
|
||||
|
||||
The GNU General Public License is a free, copyleft license for
|
||||
software and other kinds of works.
|
||||
|
||||
The licenses for most software and other practical works are designed
|
||||
to take away your freedom to share and change the works. By contrast,
|
||||
the GNU General Public License is intended to guarantee your freedom to
|
||||
share and change all versions of a program--to make sure it remains free
|
||||
software for all its users. We, the Free Software Foundation, use the
|
||||
GNU General Public License for most of our software; it applies also to
|
||||
any other work released this way by its authors. You can apply it to
|
||||
your programs, too.
|
||||
|
||||
When we speak of free software, we are referring to freedom, not
|
||||
price. Our General Public Licenses are designed to make sure that you
|
||||
have the freedom to distribute copies of free software (and charge for
|
||||
them if you wish), that you receive source code or can get it if you
|
||||
want it, that you can change the software or use pieces of it in new
|
||||
free programs, and that you know you can do these things.
|
||||
|
||||
To protect your rights, we need to prevent others from denying you
|
||||
these rights or asking you to surrender the rights. Therefore, you have
|
||||
certain responsibilities if you distribute copies of the software, or if
|
||||
you modify it: responsibilities to respect the freedom of others.
|
||||
|
||||
For example, if you distribute copies of such a program, whether
|
||||
gratis or for a fee, you must pass on to the recipients the same
|
||||
freedoms that you received. You must make sure that they, too, receive
|
||||
or can get the source code. And you must show them these terms so they
|
||||
know their rights.
|
||||
|
||||
Developers that use the GNU GPL protect your rights with two steps:
|
||||
(1) assert copyright on the software, and (2) offer you this License
|
||||
giving you legal permission to copy, distribute and/or modify it.
|
||||
|
||||
For the developers' and authors' protection, the GPL clearly explains
|
||||
that there is no warranty for this free software. For both users' and
|
||||
authors' sake, the GPL requires that modified versions be marked as
|
||||
changed, so that their problems will not be attributed erroneously to
|
||||
authors of previous versions.
|
||||
|
||||
Some devices are designed to deny users access to install or run
|
||||
modified versions of the software inside them, although the manufacturer
|
||||
can do so. This is fundamentally incompatible with the aim of
|
||||
protecting users' freedom to change the software. The systematic
|
||||
pattern of such abuse occurs in the area of products for individuals to
|
||||
use, which is precisely where it is most unacceptable. Therefore, we
|
||||
have designed this version of the GPL to prohibit the practice for those
|
||||
products. If such problems arise substantially in other domains, we
|
||||
stand ready to extend this provision to those domains in future versions
|
||||
of the GPL, as needed to protect the freedom of users.
|
||||
|
||||
Finally, every program is threatened constantly by software patents.
|
||||
States should not allow patents to restrict development and use of
|
||||
software on general-purpose computers, but in those that do, we wish to
|
||||
avoid the special danger that patents applied to a free program could
|
||||
make it effectively proprietary. To prevent this, the GPL assures that
|
||||
patents cannot be used to render the program non-free.
|
||||
|
||||
The precise terms and conditions for copying, distribution and
|
||||
modification follow.
|
||||
|
||||
TERMS AND CONDITIONS
|
||||
|
||||
0. Definitions.
|
||||
|
||||
"This License" refers to version 3 of the GNU General Public License.
|
||||
|
||||
"Copyright" also means copyright-like laws that apply to other kinds of
|
||||
works, such as semiconductor masks.
|
||||
|
||||
"The Program" refers to any copyrightable work licensed under this
|
||||
License. Each licensee is addressed as "you". "Licensees" and
|
||||
"recipients" may be individuals or organizations.
|
||||
|
||||
To "modify" a work means to copy from or adapt all or part of the work
|
||||
in a fashion requiring copyright permission, other than the making of an
|
||||
exact copy. The resulting work is called a "modified version" of the
|
||||
earlier work or a work "based on" the earlier work.
|
||||
|
||||
A "covered work" means either the unmodified Program or a work based
|
||||
on the Program.
|
||||
|
||||
To "propagate" a work means to do anything with it that, without
|
||||
permission, would make you directly or secondarily liable for
|
||||
infringement under applicable copyright law, except executing it on a
|
||||
computer or modifying a private copy. Propagation includes copying,
|
||||
distribution (with or without modification), making available to the
|
||||
public, and in some countries other activities as well.
|
||||
|
||||
To "convey" a work means any kind of propagation that enables other
|
||||
parties to make or receive copies. Mere interaction with a user through
|
||||
a computer network, with no transfer of a copy, is not conveying.
|
||||
|
||||
An interactive user interface displays "Appropriate Legal Notices"
|
||||
to the extent that it includes a convenient and prominently visible
|
||||
feature that (1) displays an appropriate copyright notice, and (2)
|
||||
tells the user that there is no warranty for the work (except to the
|
||||
extent that warranties are provided), that licensees may convey the
|
||||
work under this License, and how to view a copy of this License. If
|
||||
the interface presents a list of user commands or options, such as a
|
||||
menu, a prominent item in the list meets this criterion.
|
||||
|
||||
1. Source Code.
|
||||
|
||||
The "source code" for a work means the preferred form of the work
|
||||
for making modifications to it. "Object code" means any non-source
|
||||
form of a work.
|
||||
|
||||
A "Standard Interface" means an interface that either is an official
|
||||
standard defined by a recognized standards body, or, in the case of
|
||||
interfaces specified for a particular programming language, one that
|
||||
is widely used among developers working in that language.
|
||||
|
||||
The "System Libraries" of an executable work include anything, other
|
||||
than the work as a whole, that (a) is included in the normal form of
|
||||
packaging a Major Component, but which is not part of that Major
|
||||
Component, and (b) serves only to enable use of the work with that
|
||||
Major Component, or to implement a Standard Interface for which an
|
||||
implementation is available to the public in source code form. A
|
||||
"Major Component", in this context, means a major essential component
|
||||
(kernel, window system, and so on) of the specific operating system
|
||||
(if any) on which the executable work runs, or a compiler used to
|
||||
produce the work, or an object code interpreter used to run it.
|
||||
|
||||
The "Corresponding Source" for a work in object code form means all
|
||||
the source code needed to generate, install, and (for an executable
|
||||
work) run the object code and to modify the work, including scripts to
|
||||
control those activities. However, it does not include the work's
|
||||
System Libraries, or general-purpose tools or generally available free
|
||||
programs which are used unmodified in performing those activities but
|
||||
which are not part of the work. For example, Corresponding Source
|
||||
includes interface definition files associated with source files for
|
||||
the work, and the source code for shared libraries and dynamically
|
||||
linked subprograms that the work is specifically designed to require,
|
||||
such as by intimate data communication or control flow between those
|
||||
subprograms and other parts of the work.
|
||||
|
||||
The Corresponding Source need not include anything that users
|
||||
can regenerate automatically from other parts of the Corresponding
|
||||
Source.
|
||||
|
||||
The Corresponding Source for a work in source code form is that
|
||||
same work.
|
||||
|
||||
2. Basic Permissions.
|
||||
|
||||
All rights granted under this License are granted for the term of
|
||||
copyright on the Program, and are irrevocable provided the stated
|
||||
conditions are met. This License explicitly affirms your unlimited
|
||||
permission to run the unmodified Program. The output from running a
|
||||
covered work is covered by this License only if the output, given its
|
||||
content, constitutes a covered work. This License acknowledges your
|
||||
rights of fair use or other equivalent, as provided by copyright law.
|
||||
|
||||
You may make, run and propagate covered works that you do not
|
||||
convey, without conditions so long as your license otherwise remains
|
||||
in force. You may convey covered works to others for the sole purpose
|
||||
of having them make modifications exclusively for you, or provide you
|
||||
with facilities for running those works, provided that you comply with
|
||||
the terms of this License in conveying all material for which you do
|
||||
not control copyright. Those thus making or running the covered works
|
||||
for you must do so exclusively on your behalf, under your direction
|
||||
and control, on terms that prohibit them from making any copies of
|
||||
your copyrighted material outside their relationship with you.
|
||||
|
||||
Conveying under any other circumstances is permitted solely under
|
||||
the conditions stated below. Sublicensing is not allowed; section 10
|
||||
makes it unnecessary.
|
||||
|
||||
3. Protecting Users' Legal Rights From Anti-Circumvention Law.
|
||||
|
||||
No covered work shall be deemed part of an effective technological
|
||||
measure under any applicable law fulfilling obligations under article
|
||||
11 of the WIPO copyright treaty adopted on 20 December 1996, or
|
||||
similar laws prohibiting or restricting circumvention of such
|
||||
measures.
|
||||
|
||||
When you convey a covered work, you waive any legal power to forbid
|
||||
circumvention of technological measures to the extent such circumvention
|
||||
is effected by exercising rights under this License with respect to
|
||||
the covered work, and you disclaim any intention to limit operation or
|
||||
modification of the work as a means of enforcing, against the work's
|
||||
users, your or third parties' legal rights to forbid circumvention of
|
||||
technological measures.
|
||||
|
||||
4. Conveying Verbatim Copies.
|
||||
|
||||
You may convey verbatim copies of the Program's source code as you
|
||||
receive it, in any medium, provided that you conspicuously and
|
||||
appropriately publish on each copy an appropriate copyright notice;
|
||||
keep intact all notices stating that this License and any
|
||||
non-permissive terms added in accord with section 7 apply to the code;
|
||||
keep intact all notices of the absence of any warranty; and give all
|
||||
recipients a copy of this License along with the Program.
|
||||
|
||||
You may charge any price or no price for each copy that you convey,
|
||||
and you may offer support or warranty protection for a fee.
|
||||
|
||||
5. Conveying Modified Source Versions.
|
||||
|
||||
You may convey a work based on the Program, or the modifications to
|
||||
produce it from the Program, in the form of source code under the
|
||||
terms of section 4, provided that you also meet all of these conditions:
|
||||
|
||||
a) The work must carry prominent notices stating that you modified
|
||||
it, and giving a relevant date.
|
||||
|
||||
b) The work must carry prominent notices stating that it is
|
||||
released under this License and any conditions added under section
|
||||
7. This requirement modifies the requirement in section 4 to
|
||||
"keep intact all notices".
|
||||
|
||||
c) You must license the entire work, as a whole, under this
|
||||
License to anyone who comes into possession of a copy. This
|
||||
License will therefore apply, along with any applicable section 7
|
||||
additional terms, to the whole of the work, and all its parts,
|
||||
regardless of how they are packaged. This License gives no
|
||||
permission to license the work in any other way, but it does not
|
||||
invalidate such permission if you have separately received it.
|
||||
|
||||
d) If the work has interactive user interfaces, each must display
|
||||
Appropriate Legal Notices; however, if the Program has interactive
|
||||
interfaces that do not display Appropriate Legal Notices, your
|
||||
work need not make them do so.
|
||||
|
||||
A compilation of a covered work with other separate and independent
|
||||
works, which are not by their nature extensions of the covered work,
|
||||
and which are not combined with it such as to form a larger program,
|
||||
in or on a volume of a storage or distribution medium, is called an
|
||||
"aggregate" if the compilation and its resulting copyright are not
|
||||
used to limit the access or legal rights of the compilation's users
|
||||
beyond what the individual works permit. Inclusion of a covered work
|
||||
in an aggregate does not cause this License to apply to the other
|
||||
parts of the aggregate.
|
||||
|
||||
6. Conveying Non-Source Forms.
|
||||
|
||||
You may convey a covered work in object code form under the terms
|
||||
of sections 4 and 5, provided that you also convey the
|
||||
machine-readable Corresponding Source under the terms of this License,
|
||||
in one of these ways:
|
||||
|
||||
a) Convey the object code in, or embodied in, a physical product
|
||||
(including a physical distribution medium), accompanied by the
|
||||
Corresponding Source fixed on a durable physical medium
|
||||
customarily used for software interchange.
|
||||
|
||||
b) Convey the object code in, or embodied in, a physical product
|
||||
(including a physical distribution medium), accompanied by a
|
||||
written offer, valid for at least three years and valid for as
|
||||
long as you offer spare parts or customer support for that product
|
||||
model, to give anyone who possesses the object code either (1) a
|
||||
copy of the Corresponding Source for all the software in the
|
||||
product that is covered by this License, on a durable physical
|
||||
medium customarily used for software interchange, for a price no
|
||||
more than your reasonable cost of physically performing this
|
||||
conveying of source, or (2) access to copy the
|
||||
Corresponding Source from a network server at no charge.
|
||||
|
||||
c) Convey individual copies of the object code with a copy of the
|
||||
written offer to provide the Corresponding Source. This
|
||||
alternative is allowed only occasionally and noncommercially, and
|
||||
only if you received the object code with such an offer, in accord
|
||||
with subsection 6b.
|
||||
|
||||
d) Convey the object code by offering access from a designated
|
||||
place (gratis or for a charge), and offer equivalent access to the
|
||||
Corresponding Source in the same way through the same place at no
|
||||
further charge. You need not require recipients to copy the
|
||||
Corresponding Source along with the object code. If the place to
|
||||
copy the object code is a network server, the Corresponding Source
|
||||
may be on a different server (operated by you or a third party)
|
||||
that supports equivalent copying facilities, provided you maintain
|
||||
clear directions next to the object code saying where to find the
|
||||
Corresponding Source. Regardless of what server hosts the
|
||||
Corresponding Source, you remain obligated to ensure that it is
|
||||
available for as long as needed to satisfy these requirements.
|
||||
|
||||
e) Convey the object code using peer-to-peer transmission, provided
|
||||
you inform other peers where the object code and Corresponding
|
||||
Source of the work are being offered to the general public at no
|
||||
charge under subsection 6d.
|
||||
|
||||
A separable portion of the object code, whose source code is excluded
|
||||
from the Corresponding Source as a System Library, need not be
|
||||
included in conveying the object code work.
|
||||
|
||||
A "User Product" is either (1) a "consumer product", which means any
|
||||
tangible personal property which is normally used for personal, family,
|
||||
or household purposes, or (2) anything designed or sold for incorporation
|
||||
into a dwelling. In determining whether a product is a consumer product,
|
||||
doubtful cases shall be resolved in favor of coverage. For a particular
|
||||
product received by a particular user, "normally used" refers to a
|
||||
typical or common use of that class of product, regardless of the status
|
||||
of the particular user or of the way in which the particular user
|
||||
actually uses, or expects or is expected to use, the product. A product
|
||||
is a consumer product regardless of whether the product has substantial
|
||||
commercial, industrial or non-consumer uses, unless such uses represent
|
||||
the only significant mode of use of the product.
|
||||
|
||||
"Installation Information" for a User Product means any methods,
|
||||
procedures, authorization keys, or other information required to install
|
||||
and execute modified versions of a covered work in that User Product from
|
||||
a modified version of its Corresponding Source. The information must
|
||||
suffice to ensure that the continued functioning of the modified object
|
||||
code is in no case prevented or interfered with solely because
|
||||
modification has been made.
|
||||
|
||||
If you convey an object code work under this section in, or with, or
|
||||
specifically for use in, a User Product, and the conveying occurs as
|
||||
part of a transaction in which the right of possession and use of the
|
||||
User Product is transferred to the recipient in perpetuity or for a
|
||||
fixed term (regardless of how the transaction is characterized), the
|
||||
Corresponding Source conveyed under this section must be accompanied
|
||||
by the Installation Information. But this requirement does not apply
|
||||
if neither you nor any third party retains the ability to install
|
||||
modified object code on the User Product (for example, the work has
|
||||
been installed in ROM).
|
||||
|
||||
The requirement to provide Installation Information does not include a
|
||||
requirement to continue to provide support service, warranty, or updates
|
||||
for a work that has been modified or installed by the recipient, or for
|
||||
the User Product in which it has been modified or installed. Access to a
|
||||
network may be denied when the modification itself materially and
|
||||
adversely affects the operation of the network or violates the rules and
|
||||
protocols for communication across the network.
|
||||
|
||||
Corresponding Source conveyed, and Installation Information provided,
|
||||
in accord with this section must be in a format that is publicly
|
||||
documented (and with an implementation available to the public in
|
||||
source code form), and must require no special password or key for
|
||||
unpacking, reading or copying.
|
||||
|
||||
7. Additional Terms.
|
||||
|
||||
"Additional permissions" are terms that supplement the terms of this
|
||||
License by making exceptions from one or more of its conditions.
|
||||
Additional permissions that are applicable to the entire Program shall
|
||||
be treated as though they were included in this License, to the extent
|
||||
that they are valid under applicable law. If additional permissions
|
||||
apply only to part of the Program, that part may be used separately
|
||||
under those permissions, but the entire Program remains governed by
|
||||
this License without regard to the additional permissions.
|
||||
|
||||
When you convey a copy of a covered work, you may at your option
|
||||
remove any additional permissions from that copy, or from any part of
|
||||
it. (Additional permissions may be written to require their own
|
||||
removal in certain cases when you modify the work.) You may place
|
||||
additional permissions on material, added by you to a covered work,
|
||||
for which you have or can give appropriate copyright permission.
|
||||
|
||||
Notwithstanding any other provision of this License, for material you
|
||||
add to a covered work, you may (if authorized by the copyright holders of
|
||||
that material) supplement the terms of this License with terms:
|
||||
|
||||
a) Disclaiming warranty or limiting liability differently from the
|
||||
terms of sections 15 and 16 of this License; or
|
||||
|
||||
b) Requiring preservation of specified reasonable legal notices or
|
||||
author attributions in that material or in the Appropriate Legal
|
||||
Notices displayed by works containing it; or
|
||||
|
||||
c) Prohibiting misrepresentation of the origin of that material, or
|
||||
requiring that modified versions of such material be marked in
|
||||
reasonable ways as different from the original version; or
|
||||
|
||||
d) Limiting the use for publicity purposes of names of licensors or
|
||||
authors of the material; or
|
||||
|
||||
e) Declining to grant rights under trademark law for use of some
|
||||
trade names, trademarks, or service marks; or
|
||||
|
||||
f) Requiring indemnification of licensors and authors of that
|
||||
material by anyone who conveys the material (or modified versions of
|
||||
it) with contractual assumptions of liability to the recipient, for
|
||||
any liability that these contractual assumptions directly impose on
|
||||
those licensors and authors.
|
||||
|
||||
All other non-permissive additional terms are considered "further
|
||||
restrictions" within the meaning of section 10. If the Program as you
|
||||
received it, or any part of it, contains a notice stating that it is
|
||||
governed by this License along with a term that is a further
|
||||
restriction, you may remove that term. If a license document contains
|
||||
a further restriction but permits relicensing or conveying under this
|
||||
License, you may add to a covered work material governed by the terms
|
||||
of that license document, provided that the further restriction does
|
||||
not survive such relicensing or conveying.
|
||||
|
||||
If you add terms to a covered work in accord with this section, you
|
||||
must place, in the relevant source files, a statement of the
|
||||
additional terms that apply to those files, or a notice indicating
|
||||
where to find the applicable terms.
|
||||
|
||||
Additional terms, permissive or non-permissive, may be stated in the
|
||||
form of a separately written license, or stated as exceptions;
|
||||
the above requirements apply either way.
|
||||
|
||||
8. Termination.
|
||||
|
||||
You may not propagate or modify a covered work except as expressly
|
||||
provided under this License. Any attempt otherwise to propagate or
|
||||
modify it is void, and will automatically terminate your rights under
|
||||
this License (including any patent licenses granted under the third
|
||||
paragraph of section 11).
|
||||
|
||||
However, if you cease all violation of this License, then your
|
||||
license from a particular copyright holder is reinstated (a)
|
||||
provisionally, unless and until the copyright holder explicitly and
|
||||
finally terminates your license, and (b) permanently, if the copyright
|
||||
holder fails to notify you of the violation by some reasonable means
|
||||
prior to 60 days after the cessation.
|
||||
|
||||
Moreover, your license from a particular copyright holder is
|
||||
reinstated permanently if the copyright holder notifies you of the
|
||||
violation by some reasonable means, this is the first time you have
|
||||
received notice of violation of this License (for any work) from that
|
||||
copyright holder, and you cure the violation prior to 30 days after
|
||||
your receipt of the notice.
|
||||
|
||||
Termination of your rights under this section does not terminate the
|
||||
licenses of parties who have received copies or rights from you under
|
||||
this License. If your rights have been terminated and not permanently
|
||||
reinstated, you do not qualify to receive new licenses for the same
|
||||
material under section 10.
|
||||
|
||||
9. Acceptance Not Required for Having Copies.
|
||||
|
||||
You are not required to accept this License in order to receive or
|
||||
run a copy of the Program. Ancillary propagation of a covered work
|
||||
occurring solely as a consequence of using peer-to-peer transmission
|
||||
to receive a copy likewise does not require acceptance. However,
|
||||
nothing other than this License grants you permission to propagate or
|
||||
modify any covered work. These actions infringe copyright if you do
|
||||
not accept this License. Therefore, by modifying or propagating a
|
||||
covered work, you indicate your acceptance of this License to do so.
|
||||
|
||||
10. Automatic Licensing of Downstream Recipients.
|
||||
|
||||
Each time you convey a covered work, the recipient automatically
|
||||
receives a license from the original licensors, to run, modify and
|
||||
propagate that work, subject to this License. You are not responsible
|
||||
for enforcing compliance by third parties with this License.
|
||||
|
||||
An "entity transaction" is a transaction transferring control of an
|
||||
organization, or substantially all assets of one, or subdividing an
|
||||
organization, or merging organizations. If propagation of a covered
|
||||
work results from an entity transaction, each party to that
|
||||
transaction who receives a copy of the work also receives whatever
|
||||
licenses to the work the party's predecessor in interest had or could
|
||||
give under the previous paragraph, plus a right to possession of the
|
||||
Corresponding Source of the work from the predecessor in interest, if
|
||||
the predecessor has it or can get it with reasonable efforts.
|
||||
|
||||
You may not impose any further restrictions on the exercise of the
|
||||
rights granted or affirmed under this License. For example, you may
|
||||
not impose a license fee, royalty, or other charge for exercise of
|
||||
rights granted under this License, and you may not initiate litigation
|
||||
(including a cross-claim or counterclaim in a lawsuit) alleging that
|
||||
any patent claim is infringed by making, using, selling, offering for
|
||||
sale, or importing the Program or any portion of it.
|
||||
|
||||
11. Patents.
|
||||
|
||||
A "contributor" is a copyright holder who authorizes use under this
|
||||
License of the Program or a work on which the Program is based. The
|
||||
work thus licensed is called the contributor's "contributor version".
|
||||
|
||||
A contributor's "essential patent claims" are all patent claims
|
||||
owned or controlled by the contributor, whether already acquired or
|
||||
hereafter acquired, that would be infringed by some manner, permitted
|
||||
by this License, of making, using, or selling its contributor version,
|
||||
but do not include claims that would be infringed only as a
|
||||
consequence of further modification of the contributor version. For
|
||||
purposes of this definition, "control" includes the right to grant
|
||||
patent sublicenses in a manner consistent with the requirements of
|
||||
this License.
|
||||
|
||||
Each contributor grants you a non-exclusive, worldwide, royalty-free
|
||||
patent license under the contributor's essential patent claims, to
|
||||
make, use, sell, offer for sale, import and otherwise run, modify and
|
||||
propagate the contents of its contributor version.
|
||||
|
||||
In the following three paragraphs, a "patent license" is any express
|
||||
agreement or commitment, however denominated, not to enforce a patent
|
||||
(such as an express permission to practice a patent or covenant not to
|
||||
sue for patent infringement). To "grant" such a patent license to a
|
||||
party means to make such an agreement or commitment not to enforce a
|
||||
patent against the party.
|
||||
|
||||
If you convey a covered work, knowingly relying on a patent license,
|
||||
and the Corresponding Source of the work is not available for anyone
|
||||
to copy, free of charge and under the terms of this License, through a
|
||||
publicly available network server or other readily accessible means,
|
||||
then you must either (1) cause the Corresponding Source to be so
|
||||
available, or (2) arrange to deprive yourself of the benefit of the
|
||||
patent license for this particular work, or (3) arrange, in a manner
|
||||
consistent with the requirements of this License, to extend the patent
|
||||
license to downstream recipients. "Knowingly relying" means you have
|
||||
actual knowledge that, but for the patent license, your conveying the
|
||||
covered work in a country, or your recipient's use of the covered work
|
||||
in a country, would infringe one or more identifiable patents in that
|
||||
country that you have reason to believe are valid.
|
||||
|
||||
If, pursuant to or in connection with a single transaction or
|
||||
arrangement, you convey, or propagate by procuring conveyance of, a
|
||||
covered work, and grant a patent license to some of the parties
|
||||
receiving the covered work authorizing them to use, propagate, modify
|
||||
or convey a specific copy of the covered work, then the patent license
|
||||
you grant is automatically extended to all recipients of the covered
|
||||
work and works based on it.
|
||||
|
||||
A patent license is "discriminatory" if it does not include within
|
||||
the scope of its coverage, prohibits the exercise of, or is
|
||||
conditioned on the non-exercise of one or more of the rights that are
|
||||
specifically granted under this License. You may not convey a covered
|
||||
work if you are a party to an arrangement with a third party that is
|
||||
in the business of distributing software, under which you make payment
|
||||
to the third party based on the extent of your activity of conveying
|
||||
the work, and under which the third party grants, to any of the
|
||||
parties who would receive the covered work from you, a discriminatory
|
||||
patent license (a) in connection with copies of the covered work
|
||||
conveyed by you (or copies made from those copies), or (b) primarily
|
||||
for and in connection with specific products or compilations that
|
||||
contain the covered work, unless you entered into that arrangement,
|
||||
or that patent license was granted, prior to 28 March 2007.
|
||||
|
||||
Nothing in this License shall be construed as excluding or limiting
|
||||
any implied license or other defenses to infringement that may
|
||||
otherwise be available to you under applicable patent law.
|
||||
|
||||
12. No Surrender of Others' Freedom.
|
||||
|
||||
If conditions are imposed on you (whether by court order, agreement or
|
||||
otherwise) that contradict the conditions of this License, they do not
|
||||
excuse you from the conditions of this License. If you cannot convey a
|
||||
covered work so as to satisfy simultaneously your obligations under this
|
||||
License and any other pertinent obligations, then as a consequence you may
|
||||
not convey it at all. For example, if you agree to terms that obligate you
|
||||
to collect a royalty for further conveying from those to whom you convey
|
||||
the Program, the only way you could satisfy both those terms and this
|
||||
License would be to refrain entirely from conveying the Program.
|
||||
|
||||
13. Use with the GNU Affero General Public License.
|
||||
|
||||
Notwithstanding any other provision of this License, you have
|
||||
permission to link or combine any covered work with a work licensed
|
||||
under version 3 of the GNU Affero General Public License into a single
|
||||
combined work, and to convey the resulting work. The terms of this
|
||||
License will continue to apply to the part which is the covered work,
|
||||
but the special requirements of the GNU Affero General Public License,
|
||||
section 13, concerning interaction through a network will apply to the
|
||||
combination as such.
|
||||
|
||||
14. Revised Versions of this License.
|
||||
|
||||
The Free Software Foundation may publish revised and/or new versions of
|
||||
the GNU General Public License from time to time. Such new versions will
|
||||
be similar in spirit to the present version, but may differ in detail to
|
||||
address new problems or concerns.
|
||||
|
||||
Each version is given a distinguishing version number. If the
|
||||
Program specifies that a certain numbered version of the GNU General
|
||||
Public License "or any later version" applies to it, you have the
|
||||
option of following the terms and conditions either of that numbered
|
||||
version or of any later version published by the Free Software
|
||||
Foundation. If the Program does not specify a version number of the
|
||||
GNU General Public License, you may choose any version ever published
|
||||
by the Free Software Foundation.
|
||||
|
||||
If the Program specifies that a proxy can decide which future
|
||||
versions of the GNU General Public License can be used, that proxy's
|
||||
public statement of acceptance of a version permanently authorizes you
|
||||
to choose that version for the Program.
|
||||
|
||||
Later license versions may give you additional or different
|
||||
permissions. However, no additional obligations are imposed on any
|
||||
author or copyright holder as a result of your choosing to follow a
|
||||
later version.
|
||||
|
||||
15. Disclaimer of Warranty.
|
||||
|
||||
THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
|
||||
APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
|
||||
HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
|
||||
OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
|
||||
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
|
||||
IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
|
||||
ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
|
||||
|
||||
16. Limitation of Liability.
|
||||
|
||||
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
|
||||
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
|
||||
THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
|
||||
GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
|
||||
USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
|
||||
DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
|
||||
PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
|
||||
EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
|
||||
SUCH DAMAGES.
|
||||
|
||||
17. Interpretation of Sections 15 and 16.
|
||||
|
||||
If the disclaimer of warranty and limitation of liability provided
|
||||
above cannot be given local legal effect according to their terms,
|
||||
reviewing courts shall apply local law that most closely approximates
|
||||
an absolute waiver of all civil liability in connection with the
|
||||
Program, unless a warranty or assumption of liability accompanies a
|
||||
copy of the Program in return for a fee.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
How to Apply These Terms to Your New Programs
|
||||
|
||||
If you develop a new program, and you want it to be of the greatest
|
||||
possible use to the public, the best way to achieve this is to make it
|
||||
free software which everyone can redistribute and change under these terms.
|
||||
|
||||
To do so, attach the following notices to the program. It is safest
|
||||
to attach them to the start of each source file to most effectively
|
||||
state the exclusion of warranty; and each file should have at least
|
||||
the "copyright" line and a pointer to where the full notice is found.
|
||||
|
||||
<one line to give the program's name and a brief idea of what it does.>
|
||||
Copyright (C) <year> <name of author>
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
Also add information on how to contact you by electronic and paper mail.
|
||||
|
||||
If the program does terminal interaction, make it output a short
|
||||
notice like this when it starts in an interactive mode:
|
||||
|
||||
<program> Copyright (C) <year> <name of author>
|
||||
This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
|
||||
This is free software, and you are welcome to redistribute it
|
||||
under certain conditions; type `show c' for details.
|
||||
|
||||
The hypothetical commands `show w' and `show c' should show the appropriate
|
||||
parts of the General Public License. Of course, your program's commands
|
||||
might be different; for a GUI interface, you would use an "about box".
|
||||
|
||||
You should also get your employer (if you work as a programmer) or school,
|
||||
if any, to sign a "copyright disclaimer" for the program, if necessary.
|
||||
For more information on this, and how to apply and follow the GNU GPL, see
|
||||
<https://www.gnu.org/licenses/>.
|
||||
|
||||
The GNU General Public License does not permit incorporating your program
|
||||
into proprietary programs. If your program is a subroutine library, you
|
||||
may consider it more useful to permit linking proprietary applications with
|
||||
the library. If this is what you want to do, use the GNU Lesser General
|
||||
Public License instead of this License. But first, please read
|
||||
<https://www.gnu.org/licenses/why-not-lgpl.html>.
|
||||
@@ -0,0 +1,675 @@
|
||||
import { intersection } from 'es-toolkit/compat'
|
||||
|
||||
import { useChainCallback } from '@/composables/functional/useChainCallback'
|
||||
import { createWidgetRestorationState } from '@/lib/litegraph/src/LGraphNode'
|
||||
import { LGraphNode, LiteGraph } from '@/lib/litegraph/src/litegraph'
|
||||
import type {
|
||||
INodeInputSlot,
|
||||
INodeOutputSlot,
|
||||
ISerialisedNode,
|
||||
ISlotType,
|
||||
LLink
|
||||
} from '@/lib/litegraph/src/litegraph'
|
||||
import type { IWidgetLocator } from '@/lib/litegraph/src/interfaces'
|
||||
import { NodeSlot } from '@/lib/litegraph/src/node/NodeSlot'
|
||||
import { outputHasLinks, outputLinks } from '@/lib/litegraph/src/node/slotLinks'
|
||||
import type { IBaseWidget } from '@/lib/litegraph/src/types/widgets'
|
||||
import { assetService } from '@/platform/assets/services/assetService'
|
||||
import { createAssetWidget } from '@/platform/assets/utils/createAssetWidget'
|
||||
import {
|
||||
getComboSpecComboOptions,
|
||||
isComboInputSpec
|
||||
} from '@/schemas/nodeDefSchema'
|
||||
import type { ComfyNodeDef, InputSpec } from '@/schemas/nodeDefSchema'
|
||||
import { app } from '@/scripts/app'
|
||||
import { useWidgetValueStore } from '@/stores/widgetValueStore'
|
||||
import type { WidgetValue } from '@/types/simplifiedWidget'
|
||||
import {
|
||||
ComfyWidgets,
|
||||
addValueControlWidgets,
|
||||
isValidWidgetType
|
||||
} from '@/scripts/widgets'
|
||||
import { isPrimitiveNode } from '@/renderer/utils/nodeTypeGuards'
|
||||
import { CONFIG, GET_CONFIG } from '@/services/litegraphService'
|
||||
import { mergeInputSpec } from '@/utils/nodeDefUtil'
|
||||
import { applyTextReplacements } from '@/utils/searchAndReplace'
|
||||
|
||||
import { applyFirstWidgetValueToGraph } from './widgetValuePropagation'
|
||||
|
||||
const replacePropertyName = 'Run widget replace on values'
|
||||
export class PrimitiveNode extends LGraphNode {
|
||||
controlValues?: WidgetValue[]
|
||||
lastType?: string
|
||||
static override category: string
|
||||
constructor(title: string) {
|
||||
super(title)
|
||||
this.addOutput('connect to widget input', '*')
|
||||
this.serialize_widgets = true
|
||||
this.isVirtualNode = true
|
||||
|
||||
if (!this.properties || !(replacePropertyName in this.properties)) {
|
||||
this.addProperty(replacePropertyName, false, 'boolean')
|
||||
}
|
||||
}
|
||||
|
||||
override applyToGraph(extraLinks: LLink[] = []) {
|
||||
const sourceWidget = this.widgets?.[0]
|
||||
const graph = this.graph
|
||||
if (!sourceWidget || !graph) return
|
||||
|
||||
let v = sourceWidget.value
|
||||
if (v && this.properties[replacePropertyName]) {
|
||||
v = applyTextReplacements(graph, v as string)
|
||||
}
|
||||
applyFirstWidgetValueToGraph(this, extraLinks, () => v)
|
||||
}
|
||||
|
||||
override refreshComboInNode(defs?: Record<string, ComfyNodeDef>): void {
|
||||
const widget = this.widgets?.[0]
|
||||
if (widget?.type !== 'combo') return
|
||||
|
||||
const newValues = this._resolveComboValues(defs)
|
||||
if (newValues === undefined) return
|
||||
|
||||
widget.options.values = newValues
|
||||
|
||||
const value = widget.value
|
||||
if (
|
||||
(typeof value !== 'string' && typeof value !== 'number') ||
|
||||
!newValues.includes(value)
|
||||
) {
|
||||
widget.value = newValues[0]
|
||||
widget.callback?.(widget.value)
|
||||
}
|
||||
}
|
||||
|
||||
private _resolveComboValues(
|
||||
defs?: Record<string, ComfyNodeDef>
|
||||
): (string | number)[] | undefined {
|
||||
const fromDefs = defs ? this._comboValuesFromDefs(defs) : undefined
|
||||
if (fromDefs !== undefined) return fromDefs
|
||||
|
||||
const slotWidget = this.outputs?.[0]?.widget
|
||||
const config = slotWidget?.[GET_CONFIG]?.()
|
||||
if (!config || !isComboInputSpec(config)) return undefined
|
||||
|
||||
return getComboSpecComboOptions(config)
|
||||
}
|
||||
|
||||
private _comboValuesFromDefs(
|
||||
defs: Record<string, ComfyNodeDef>
|
||||
): (string | number)[] | undefined {
|
||||
const graph = this.graph
|
||||
const links = graph ? outputLinks(graph, this.id, 0) : []
|
||||
if (!graph || !links?.length) return undefined
|
||||
|
||||
let values: (string | number)[] | undefined
|
||||
for (const link of links) {
|
||||
const targetNode = graph.getNodeById(link?.target_id)
|
||||
const targetType = targetNode?.type
|
||||
const targetInput = link
|
||||
? targetNode?.inputs[link.target_slot]
|
||||
: undefined
|
||||
const inputName = targetInput?.widget?.name ?? targetInput?.name
|
||||
if (!targetType || !inputName) return undefined
|
||||
|
||||
const def = defs[targetType]
|
||||
const inputSpec =
|
||||
def?.input?.required?.[inputName] ?? def?.input?.optional?.[inputName]
|
||||
if (!inputSpec || !isComboInputSpec(inputSpec)) return undefined
|
||||
|
||||
const options = getComboSpecComboOptions(inputSpec)
|
||||
values = values === undefined ? options : intersection(values, options)
|
||||
}
|
||||
|
||||
return values
|
||||
}
|
||||
|
||||
override configure(serialisedNode: ISerialisedNode) {
|
||||
const type = serialisedNode.outputs?.[0]?.type
|
||||
super.configure(serialisedNode)
|
||||
if (!this.graph || this.widgets?.length || typeof type !== 'string') return
|
||||
|
||||
useWidgetValueStore().setNodeWidgetRestoration(
|
||||
this.graph.rootGraph.id,
|
||||
this.id,
|
||||
createWidgetRestorationState(serialisedNode)
|
||||
)
|
||||
}
|
||||
|
||||
override onAfterGraphConfigured() {
|
||||
if (
|
||||
this.graph &&
|
||||
outputHasLinks(this.graph, this.id, 0) &&
|
||||
!this.widgets?.length
|
||||
) {
|
||||
this._onFirstConnection()
|
||||
|
||||
// Merge values if required
|
||||
this._mergeWidgetConfig()
|
||||
}
|
||||
}
|
||||
|
||||
private _clearWidgetRestoration() {
|
||||
if (this.graph)
|
||||
useWidgetValueStore().clearNodeWidgetRestoration(
|
||||
this.graph.rootGraph.id,
|
||||
this.id
|
||||
)
|
||||
}
|
||||
|
||||
override onConnectionsChange(
|
||||
_type: ISlotType,
|
||||
_index: number,
|
||||
connected: boolean
|
||||
) {
|
||||
if (app.configuringGraph) {
|
||||
// Dont run while the graph is still setting up
|
||||
return
|
||||
}
|
||||
|
||||
const hasLinks = this.graph ? outputHasLinks(this.graph, this.id, 0) : false
|
||||
if (connected) {
|
||||
if (hasLinks && !this.widgets?.length) {
|
||||
this._onFirstConnection()
|
||||
}
|
||||
} else {
|
||||
// We may have removed a link that caused the constraints to change
|
||||
this._mergeWidgetConfig()
|
||||
|
||||
if (!hasLinks) {
|
||||
this.onLastDisconnect()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override onConnectOutput(
|
||||
slot: number,
|
||||
_type: string,
|
||||
input: INodeInputSlot,
|
||||
target_node: LGraphNode,
|
||||
target_slot: number
|
||||
) {
|
||||
// Fires before the link is made allowing us to reject it if it isn't valid
|
||||
// No widget, we can't connect
|
||||
if (!input.widget && !(input.type in ComfyWidgets)) {
|
||||
return false
|
||||
}
|
||||
|
||||
if (this.graph && outputHasLinks(this.graph, this.id, slot)) {
|
||||
const valid = this._isValidConnection(input)
|
||||
if (valid) {
|
||||
// On connect of additional outputs, copy our value to their widget
|
||||
this.applyToGraph([{ target_id: target_node.id, target_slot } as LLink])
|
||||
}
|
||||
return valid
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
private _onFirstConnection(recreating = false) {
|
||||
// First connection can fire before the graph is ready on initial load so random things can be missing
|
||||
if (!this.graph) {
|
||||
this.onLastDisconnect()
|
||||
return
|
||||
}
|
||||
const [link] = outputLinks(this.graph, this.id, 0)
|
||||
if (!link) {
|
||||
if (outputHasLinks(this.graph, this.id, 0)) {
|
||||
console.warn(
|
||||
`PrimitiveNode ${this.id}: link store reports output 0 connected but no link resolves in the graph; resetting the widget.`
|
||||
)
|
||||
}
|
||||
this.onLastDisconnect()
|
||||
return
|
||||
}
|
||||
|
||||
const theirNode = this.graph.getNodeById(link.target_id)
|
||||
if (!theirNode || !theirNode.inputs) return
|
||||
|
||||
const input = theirNode.inputs[link.target_slot]
|
||||
if (!input) return
|
||||
|
||||
let widget: IWidgetLocator
|
||||
if (!input.widget) {
|
||||
const inputType = input.type
|
||||
if (typeof inputType !== 'string' || !(inputType in ComfyWidgets)) return
|
||||
widget = { name: input.name, [GET_CONFIG]: () => [inputType, {}] } //fake widget
|
||||
} else {
|
||||
widget = input.widget
|
||||
}
|
||||
|
||||
const config = widget[GET_CONFIG]?.()
|
||||
if (!config) return
|
||||
|
||||
const { type } = getWidgetType(config)
|
||||
if (recreating || this.outputs[0].type !== type)
|
||||
this._clearWidgetRestoration()
|
||||
// Update our output to restrict to the widget type
|
||||
this.outputs[0].type = type
|
||||
this.outputs[0].name = type
|
||||
this.outputs[0].widget = widget
|
||||
|
||||
this._createWidget(
|
||||
widget[CONFIG] ?? config,
|
||||
theirNode,
|
||||
widget.name,
|
||||
recreating
|
||||
)
|
||||
}
|
||||
|
||||
private _createWidget(
|
||||
inputData: InputSpec,
|
||||
node: LGraphNode,
|
||||
widgetName: string,
|
||||
recreating: boolean
|
||||
) {
|
||||
let type = inputData[0]
|
||||
|
||||
if (type instanceof Array) {
|
||||
type = 'COMBO'
|
||||
}
|
||||
|
||||
// Store current size as addWidget resizes the node
|
||||
const [oldWidth, oldHeight] = this.size
|
||||
let widget: IBaseWidget
|
||||
const restoredValue = this.graph
|
||||
? useWidgetValueStore().getRestoredWidgetValue(
|
||||
this.graph.rootGraph.id,
|
||||
this.id,
|
||||
'value',
|
||||
0
|
||||
)
|
||||
: undefined
|
||||
|
||||
try {
|
||||
if (
|
||||
type === 'COMBO' &&
|
||||
assetService.shouldUseAssetBrowser(node.comfyClass, widgetName)
|
||||
) {
|
||||
widget = this._createAssetWidget(node, widgetName, inputData)
|
||||
const theirWidget = node.widgets?.find((w) => w.name === widgetName)
|
||||
if (theirWidget) widget.value = theirWidget.value
|
||||
if (restoredValue) widget.value = restoredValue.value
|
||||
this._finalizeWidget(widget, oldWidth, oldHeight, recreating)
|
||||
return
|
||||
}
|
||||
|
||||
if (isValidWidgetType(type)) {
|
||||
widget = (ComfyWidgets[type](this, 'value', inputData, app) || {})
|
||||
.widget
|
||||
} else {
|
||||
// @ts-expect-error InputSpec is not typed correctly
|
||||
widget = this.addWidget(type, 'value', null, () => {}, {})
|
||||
}
|
||||
|
||||
if (node?.widgets && widget) {
|
||||
const theirWidget = node.widgets.find((w) => w.name === widgetName)
|
||||
if (theirWidget) {
|
||||
widget.value = theirWidget.value
|
||||
}
|
||||
}
|
||||
if (restoredValue) widget.value = restoredValue.value
|
||||
|
||||
if (
|
||||
!inputData?.[1]?.control_after_generate &&
|
||||
(widget.type === 'number' || widget.type === 'combo')
|
||||
) {
|
||||
addValueControlWidgets(this, widget, 'fixed', undefined, inputData)
|
||||
if (this.widgets?.[1]) widget.linkedWidgets = [this.widgets[1]]
|
||||
}
|
||||
|
||||
// Restore any saved control values
|
||||
const controlValues = this.controlValues
|
||||
if (
|
||||
this.widgets &&
|
||||
this.lastType === this.widgets[0]?.type &&
|
||||
controlValues?.length === this.widgets.length - 1
|
||||
) {
|
||||
for (let i = 0; i < controlValues.length; i++) {
|
||||
this.widgets[i + 1].value = controlValues[i]
|
||||
}
|
||||
}
|
||||
|
||||
this._finalizeWidget(widget, oldWidth, oldHeight, recreating)
|
||||
} finally {
|
||||
this._clearWidgetRestoration()
|
||||
}
|
||||
}
|
||||
|
||||
private _createAssetWidget(
|
||||
targetNode: LGraphNode,
|
||||
targetInputName: string,
|
||||
inputData: InputSpec
|
||||
): IBaseWidget {
|
||||
const defaultValue = inputData[1]?.default as string | undefined
|
||||
return createAssetWidget({
|
||||
node: this,
|
||||
widgetName: 'value',
|
||||
nodeTypeForBrowser: targetNode.comfyClass ?? '',
|
||||
inputNameForBrowser: targetInputName,
|
||||
defaultValue
|
||||
})
|
||||
}
|
||||
|
||||
private _finalizeWidget(
|
||||
widget: IBaseWidget,
|
||||
oldWidth: number,
|
||||
oldHeight: number,
|
||||
recreating: boolean
|
||||
) {
|
||||
widget.callback = useChainCallback(widget.callback, () => {
|
||||
this.applyToGraph()
|
||||
})
|
||||
|
||||
this.setSize([
|
||||
Math.max(this.size[0], oldWidth),
|
||||
Math.max(this.size[1], oldHeight)
|
||||
])
|
||||
|
||||
if (!recreating) {
|
||||
const sz = this.computeSize()
|
||||
this.size = [Math.max(this.size[0], sz[0]), Math.max(this.size[1], sz[1])]
|
||||
|
||||
requestAnimationFrame(() => {
|
||||
this.onResize?.(this.size)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
recreateWidget() {
|
||||
const values = this.widgets?.map((w) => w.value)
|
||||
this._removeWidgets()
|
||||
this._onFirstConnection(!!values?.length)
|
||||
if (values?.length && this.widgets) {
|
||||
for (let i = 0; i < this.widgets.length; i++)
|
||||
this.widgets[i].value = values[i]
|
||||
}
|
||||
return this.widgets?.[0]
|
||||
}
|
||||
|
||||
private _mergeWidgetConfig() {
|
||||
// Merge widget configs if the node has multiple outputs
|
||||
const output = this.outputs[0]
|
||||
const links = this.graph ? outputLinks(this.graph, this.id, 0) : []
|
||||
|
||||
const hasConfig = !!output.widget?.[CONFIG]
|
||||
if (hasConfig) {
|
||||
delete output.widget?.[CONFIG]
|
||||
}
|
||||
|
||||
if (links.length < 2 && hasConfig) {
|
||||
// Copy the widget options from the source
|
||||
if (links.length) {
|
||||
this.recreateWidget()
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
const config1 = output.widget?.[GET_CONFIG]?.()
|
||||
if (!config1) return
|
||||
const isNumber = config1[0] === 'INT' || config1[0] === 'FLOAT'
|
||||
if (!isNumber || !this.graph) return
|
||||
|
||||
for (const link of links) {
|
||||
const theirNode = this.graph.getNodeById(link.target_id)
|
||||
if (!theirNode) continue
|
||||
const theirInput = theirNode.inputs[link.target_slot]
|
||||
|
||||
// Call is valid connection so it can merge the configs when validating
|
||||
this._isValidConnection(theirInput, hasConfig)
|
||||
}
|
||||
}
|
||||
|
||||
private _isValidConnection(input: INodeInputSlot, forceUpdate?: boolean) {
|
||||
// Only allow connections where the configs match
|
||||
const output = this.outputs?.[0]
|
||||
const config2 = input.widget?.[GET_CONFIG]?.()
|
||||
if (!config2) return false
|
||||
|
||||
return !!mergeIfValid.call(
|
||||
this,
|
||||
output,
|
||||
config2,
|
||||
forceUpdate,
|
||||
this.recreateWidget
|
||||
)
|
||||
}
|
||||
|
||||
private _removeWidgets() {
|
||||
if (this.widgets) {
|
||||
// Allow widgets to cleanup
|
||||
for (const w of this.widgets) {
|
||||
if (w.onRemove) {
|
||||
w.onRemove()
|
||||
}
|
||||
}
|
||||
|
||||
// Temporarily store the current values in case the node is being recreated
|
||||
this.controlValues = []
|
||||
this.lastType = this.widgets[0]?.type
|
||||
for (let i = 1; i < this.widgets.length; i++) {
|
||||
this.controlValues.push(this.widgets[i].value)
|
||||
}
|
||||
setTimeout(() => {
|
||||
delete this.lastType
|
||||
delete this.controlValues
|
||||
}, 15)
|
||||
this.widgets.length = 0
|
||||
}
|
||||
}
|
||||
|
||||
onLastDisconnect() {
|
||||
this._clearWidgetRestoration()
|
||||
// We can't remove + re-add the output here as if you drag a link over the same link
|
||||
// it removes, then re-adds, causing it to break
|
||||
this.outputs[0].type = '*'
|
||||
this.outputs[0].name = 'connect to widget input'
|
||||
delete this.outputs[0].widget
|
||||
|
||||
this._removeWidgets()
|
||||
}
|
||||
}
|
||||
|
||||
export function getWidgetConfig(
|
||||
slot: INodeInputSlot | INodeOutputSlot
|
||||
): InputSpec {
|
||||
return slot.widget?.[CONFIG] ?? slot.widget?.[GET_CONFIG]?.() ?? ['*', {}]
|
||||
}
|
||||
|
||||
function getConfig(this: LGraphNode, widgetName: string) {
|
||||
const { nodeData } = this.constructor
|
||||
return (
|
||||
nodeData?.input?.required?.[widgetName] ??
|
||||
nodeData?.input?.optional?.[widgetName]
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a widget to an input slot.
|
||||
* @deprecated Widget to socket conversion is no longer necessary, as they co-exist now.
|
||||
* @param node The node to convert the widget to an input slot for.
|
||||
* @param widget The widget to convert to an input slot.
|
||||
* @returns The input slot that was converted from the widget or undefined if the widget is not found.
|
||||
*/
|
||||
export function convertToInput(
|
||||
node: LGraphNode,
|
||||
widget: IBaseWidget
|
||||
): INodeInputSlot | undefined {
|
||||
console.warn(
|
||||
'Please remove call to convertToInput. Widget to socket conversion is no longer necessary, as they co-exist now.'
|
||||
)
|
||||
return node.inputs.find((slot) => slot.widget?.name === widget.name)
|
||||
}
|
||||
|
||||
function getWidgetType(config: InputSpec) {
|
||||
// Special handling for COMBO so we restrict links based on the entries
|
||||
let type = config[0]
|
||||
if (type instanceof Array) {
|
||||
type = 'COMBO'
|
||||
}
|
||||
return { type }
|
||||
}
|
||||
|
||||
export function setWidgetConfig(slot: INodeInputSlot, config?: InputSpec) {
|
||||
if (!slot.widget) return
|
||||
if (config) {
|
||||
slot.widget[GET_CONFIG] = () => config
|
||||
} else {
|
||||
delete slot.widget
|
||||
}
|
||||
|
||||
if (!(slot instanceof NodeSlot)) return
|
||||
const { node } = slot
|
||||
if (!node.graph) return
|
||||
const link = node.getInputLink(node.inputs.indexOf(slot))
|
||||
if (!link) return
|
||||
const originNode = node.graph.getNodeById(link.origin_id)
|
||||
if (!originNode || !isPrimitiveNode(originNode)) return
|
||||
if (config) {
|
||||
originNode.recreateWidget()
|
||||
} else if (!app.configuringGraph) {
|
||||
originNode.disconnectOutput(0)
|
||||
originNode.onLastDisconnect()
|
||||
}
|
||||
}
|
||||
|
||||
export function mergeIfValid(
|
||||
output: INodeOutputSlot | INodeInputSlot,
|
||||
config2: InputSpec,
|
||||
forceUpdate?: boolean,
|
||||
recreateWidget?: () => void,
|
||||
config1?: InputSpec
|
||||
): { customConfig: InputSpec[1] } {
|
||||
if (!config1) {
|
||||
config1 = getWidgetConfig(output)
|
||||
}
|
||||
|
||||
const customSpec = mergeInputSpec(config1, config2)
|
||||
|
||||
if (customSpec || forceUpdate) {
|
||||
if (customSpec) {
|
||||
// @ts-expect-error fixme ts strict error
|
||||
output.widget[CONFIG] = customSpec
|
||||
}
|
||||
|
||||
// @ts-expect-error fixme ts strict error
|
||||
const widget = recreateWidget?.call(this)
|
||||
// When deleting a node this can be null
|
||||
if (widget) {
|
||||
// @ts-expect-error fixme ts strict error
|
||||
const min = widget.options.min
|
||||
// @ts-expect-error fixme ts strict error
|
||||
const max = widget.options.max
|
||||
// @ts-expect-error fixme ts strict error
|
||||
if (min != null && widget.value < min) widget.value = min
|
||||
// @ts-expect-error fixme ts strict error
|
||||
if (max != null && widget.value > max) widget.value = max
|
||||
// @ts-expect-error fixme ts strict error
|
||||
widget.callback(widget.value)
|
||||
}
|
||||
}
|
||||
|
||||
return { customConfig: customSpec?.[1] ?? {} }
|
||||
}
|
||||
|
||||
app.registerExtension({
|
||||
name: 'Comfy.WidgetInputs',
|
||||
async beforeRegisterNodeDef(
|
||||
nodeType: typeof LGraphNode,
|
||||
_nodeData: ComfyNodeDef
|
||||
) {
|
||||
// @ts-expect-error adding extra property
|
||||
nodeType.prototype.convertWidgetToInput = function (this: LGraphNode) {
|
||||
console.warn(
|
||||
'Please remove call to convertWidgetToInput. Widget to socket conversion is no longer necessary, as they co-exist now.'
|
||||
)
|
||||
return false
|
||||
}
|
||||
|
||||
nodeType.prototype.onGraphConfigured = useChainCallback(
|
||||
nodeType.prototype.onGraphConfigured,
|
||||
function (this: LGraphNode) {
|
||||
if (!this.inputs) return
|
||||
this.widgets ??= []
|
||||
|
||||
for (const input of this.inputs) {
|
||||
if (input.widget) {
|
||||
const name = input.widget.name
|
||||
if (!input.widget[GET_CONFIG]) {
|
||||
input.widget[GET_CONFIG] = () => getConfig.call(this, name)
|
||||
}
|
||||
|
||||
const w = this.widgets?.find((w) => w.name === name)
|
||||
if (!w) {
|
||||
this.removeInput(this.inputs.findIndex((i) => i === input))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
nodeType.prototype.onConfigure = useChainCallback(
|
||||
nodeType.prototype.onConfigure,
|
||||
function (this: LGraphNode) {
|
||||
if (!app.configuringGraph && this.inputs) {
|
||||
// On copy + paste of nodes, ensure that widget configs are set up
|
||||
for (const input of this.inputs) {
|
||||
if (input.widget && !input.widget[GET_CONFIG]) {
|
||||
const name = input.widget.name
|
||||
input.widget[GET_CONFIG] = () => getConfig.call(this, name)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
// Double click a widget input to automatically attach a primitive
|
||||
const origOnInputDblClick = nodeType.prototype.onInputDblClick
|
||||
nodeType.prototype.onInputDblClick = function (
|
||||
this: LGraphNode,
|
||||
...[slot, ...args]: Parameters<NonNullable<typeof origOnInputDblClick>>
|
||||
) {
|
||||
const r = origOnInputDblClick?.apply(this, [slot, ...args])
|
||||
|
||||
const input = this.inputs[slot]
|
||||
if (!input.widget) {
|
||||
// Not a widget input or already handled input
|
||||
if (typeof input.type !== 'string' || !(input.type in ComfyWidgets)) {
|
||||
return r //also Not a ComfyWidgets input or combo (do nothing)
|
||||
}
|
||||
}
|
||||
|
||||
// Create a primitive node
|
||||
const node = LiteGraph.createNode('PrimitiveNode')
|
||||
const graph = app.canvas.graph
|
||||
if (!node || !graph) return r
|
||||
|
||||
graph?.add(node)
|
||||
|
||||
// Calculate a position that won't directly overlap another node
|
||||
const pos: [number, number] = [
|
||||
this.pos[0] - node.size[0] - 30,
|
||||
this.pos[1]
|
||||
]
|
||||
while (graph.getNodeOnPos(pos[0], pos[1], graph.nodes))
|
||||
pos[1] += LiteGraph.NODE_TITLE_HEIGHT
|
||||
|
||||
node.pos = pos
|
||||
node.connect(0, this, slot)
|
||||
node.title = input.name
|
||||
|
||||
return r
|
||||
}
|
||||
},
|
||||
registerCustomNodes() {
|
||||
LiteGraph.registerNodeType(
|
||||
'PrimitiveNode',
|
||||
Object.assign(PrimitiveNode, {
|
||||
title: 'Primitive'
|
||||
})
|
||||
)
|
||||
PrimitiveNode.category = 'utilities/primitive'
|
||||
}
|
||||
})
|
||||
@@ -0,0 +1,730 @@
|
||||
import {
|
||||
SUBGRAPH_INPUT_ID,
|
||||
SUBGRAPH_OUTPUT_ID
|
||||
} from '@/lib/litegraph/src/constants'
|
||||
import type { SubgraphInput } from '@/lib/litegraph/src/subgraph/SubgraphInput'
|
||||
import type { SubgraphOutput } from '@/lib/litegraph/src/subgraph/SubgraphOutput'
|
||||
import { layoutStore } from '@/renderer/core/layout/store/layoutStore'
|
||||
import { useLinkStore } from '@/stores/linkStore'
|
||||
import { graphScopeOf, toOwningGraphId } from '@/types/graphScopeId'
|
||||
import type { GraphScope } from '@/types/graphScopeId'
|
||||
import { zeroUuid } from '@/utils/uuid'
|
||||
import { toLinkId } from '@/types/linkId'
|
||||
import { UNASSIGNED_NODE_ID, toNodeId, serializeNodeId } from '@/types/nodeId'
|
||||
import { toRerouteId } from '@/types/rerouteId'
|
||||
|
||||
import type { EndpointPatch } from '@/stores/linkStore'
|
||||
import type { LinkId } from '@/types/linkId'
|
||||
import type { LinkTopology } from '@/types/linkTopology'
|
||||
import type { RerouteId } from '@/types/rerouteId'
|
||||
import type { LGraph } from './LGraph'
|
||||
import type { LGraphNode } from './LGraphNode'
|
||||
import type { NodeId, SerializedNodeId } from '@/types/nodeId'
|
||||
import type { Reroute } from './Reroute'
|
||||
import type {
|
||||
CanvasColour,
|
||||
INodeInputSlot,
|
||||
INodeOutputSlot,
|
||||
ISlotType,
|
||||
LinkNetwork,
|
||||
LinkSegment,
|
||||
Point,
|
||||
ReadonlyLinkNetwork
|
||||
} from './interfaces'
|
||||
import type { Serialisable, SerialisableLLink } from './types/serialisation'
|
||||
import { toRaw } from 'vue'
|
||||
|
||||
export type { LinkId } from '@/types/linkId'
|
||||
export type SerialisedLLinkArray = [
|
||||
id: number,
|
||||
origin_id: SerializedNodeId,
|
||||
origin_slot: number,
|
||||
target_id: SerializedNodeId,
|
||||
target_slot: number,
|
||||
type: ISlotType
|
||||
]
|
||||
|
||||
const linkByTopology = new WeakMap<LinkTopology, LLink>()
|
||||
|
||||
let topologyFacadeDescriptors: PropertyDescriptorMap | undefined
|
||||
|
||||
export function resolveLinkTopology(topology: LinkTopology): LLink | undefined {
|
||||
return linkByTopology.get(toRaw(topology))
|
||||
}
|
||||
|
||||
function defineEnumerableTopologyFacade(link: LLink): void {
|
||||
topologyFacadeDescriptors ??= [
|
||||
'id',
|
||||
'type',
|
||||
'origin_id',
|
||||
'origin_slot',
|
||||
'target_id',
|
||||
'target_slot',
|
||||
'parentId'
|
||||
].reduce<PropertyDescriptorMap>((descriptors, key) => {
|
||||
const descriptor = Object.getOwnPropertyDescriptor(LLink.prototype, key)
|
||||
if (descriptor) descriptors[key] = { ...descriptor, enumerable: true }
|
||||
return descriptors
|
||||
}, {})
|
||||
Object.defineProperties(link, topologyFacadeDescriptors)
|
||||
}
|
||||
|
||||
// Resolved connection union; eliminates subgraph in/out as a possibility
|
||||
export type ResolvedConnection = BaseResolvedConnection &
|
||||
(
|
||||
| (ResolvedSubgraphInput & ResolvedNormalOutput)
|
||||
| (ResolvedNormalInput & ResolvedSubgraphOutput)
|
||||
| (ResolvedNormalInput & ResolvedNormalOutput)
|
||||
)
|
||||
|
||||
interface BaseResolvedConnection {
|
||||
link: LLink
|
||||
/** The node on the input side of the link (owns {@link input}) */
|
||||
inputNode?: LGraphNode
|
||||
/** The input the link is connected to (mutually exclusive with {@link subgraphOutput}) */
|
||||
input?: INodeInputSlot
|
||||
/** The node on the output side of the link (owns {@link output}) */
|
||||
outputNode?: LGraphNode
|
||||
/** The output the link is connected to (mutually exclusive with {@link subgraphInput}) */
|
||||
output?: INodeOutputSlot
|
||||
/** The subgraph output the link is connected to (mutually exclusive with {@link input}) */
|
||||
subgraphOutput?: SubgraphOutput
|
||||
/** The subgraph input the link is connected to (mutually exclusive with {@link output}) */
|
||||
subgraphInput?: SubgraphInput
|
||||
}
|
||||
|
||||
interface ResolvedNormalInput {
|
||||
inputNode: LGraphNode | undefined
|
||||
input: INodeInputSlot | undefined
|
||||
subgraphOutput?: undefined
|
||||
}
|
||||
|
||||
interface ResolvedNormalOutput {
|
||||
outputNode: LGraphNode | undefined
|
||||
output: INodeOutputSlot | undefined
|
||||
subgraphInput?: undefined
|
||||
}
|
||||
|
||||
interface ResolvedSubgraphInput {
|
||||
inputNode?: undefined
|
||||
/** The actual input slot the link is connected to (mutually exclusive with {@link subgraphOutput}) */
|
||||
input?: undefined
|
||||
subgraphOutput: SubgraphOutput
|
||||
}
|
||||
|
||||
interface ResolvedSubgraphOutput {
|
||||
outputNode?: undefined
|
||||
output?: undefined
|
||||
subgraphInput: SubgraphInput
|
||||
}
|
||||
|
||||
type BasicReadonlyNetwork = Pick<
|
||||
ReadonlyLinkNetwork,
|
||||
'getNodeById' | 'links' | 'getLink' | 'inputNode' | 'outputNode'
|
||||
>
|
||||
|
||||
// this is the class in charge of storing link information
|
||||
export class LLink implements LinkSegment, Serialisable<SerialisableLLink> {
|
||||
static _drawDebug = false
|
||||
|
||||
/**
|
||||
* The link's topology state. Once registered with {@link useLinkStore},
|
||||
* this is the store's reactive proxy, so field writes are tracked.
|
||||
*/
|
||||
_state: LinkTopology
|
||||
|
||||
/** The graph this link is registered with in {@link useLinkStore}, if any. */
|
||||
_graphScope?: GraphScope
|
||||
|
||||
/** Link ID */
|
||||
get id() {
|
||||
return this._state.id
|
||||
}
|
||||
|
||||
set id(value: LinkId) {
|
||||
if (this._graphScope) {
|
||||
if (value !== this._state.id) {
|
||||
console.error('LiteGraph: refusing to change a registered link id')
|
||||
}
|
||||
return
|
||||
}
|
||||
Object.assign(this._state, { id: value })
|
||||
}
|
||||
|
||||
get type() {
|
||||
return this._state.type
|
||||
}
|
||||
|
||||
set type(value: ISlotType) {
|
||||
this._state.type = value
|
||||
}
|
||||
|
||||
/** Output node ID */
|
||||
get origin_id() {
|
||||
return this._state.originNodeId
|
||||
}
|
||||
|
||||
set origin_id(value: NodeId) {
|
||||
this.updateEndpoints({ originNodeId: value })
|
||||
}
|
||||
|
||||
/** Output slot index */
|
||||
get origin_slot() {
|
||||
return this._state.originSlot
|
||||
}
|
||||
|
||||
set origin_slot(value: number) {
|
||||
this.updateEndpoints({ originSlot: value })
|
||||
}
|
||||
|
||||
/** Input node ID */
|
||||
get target_id() {
|
||||
return this._state.targetNodeId
|
||||
}
|
||||
|
||||
set target_id(value: NodeId) {
|
||||
this.updateEndpoints({ targetNodeId: value })
|
||||
}
|
||||
|
||||
/** Input slot index */
|
||||
get target_slot() {
|
||||
return this._state.targetSlot
|
||||
}
|
||||
|
||||
set target_slot(value: number) {
|
||||
this.updateEndpoints({ targetSlot: value })
|
||||
}
|
||||
|
||||
updateEndpoints(patch: EndpointPatch): void {
|
||||
if (!this._graphScope) {
|
||||
Object.assign(this._state, patch)
|
||||
return
|
||||
}
|
||||
|
||||
const result = useLinkStore().updateEndpoint(
|
||||
this._graphScope,
|
||||
this._state,
|
||||
patch
|
||||
)
|
||||
if (!result.ok)
|
||||
console.error('Failed to update link endpoints', result.error)
|
||||
}
|
||||
|
||||
get parentId() {
|
||||
return this._state.parentId
|
||||
}
|
||||
|
||||
set parentId(value: RerouteId | undefined) {
|
||||
this._state.parentId = value
|
||||
}
|
||||
|
||||
data?: number | string | boolean | { toToolTip?(): string }
|
||||
_data?: unknown
|
||||
/** Centre point of the link, calculated during render only - can be inaccurate */
|
||||
_pos: Point
|
||||
/** @todo Clean up - never implemented in comfy. */
|
||||
_last_time?: number
|
||||
/** The last canvas 2D path that was used to render this link */
|
||||
path?: Path2D
|
||||
/** @inheritdoc */
|
||||
_centreAngle?: number
|
||||
|
||||
/** @inheritdoc */
|
||||
_dragging?: boolean
|
||||
|
||||
private _color?: CanvasColour | null
|
||||
/** Custom colour for this link only */
|
||||
public get color(): CanvasColour | null | undefined {
|
||||
return this._color
|
||||
}
|
||||
|
||||
public set color(value: CanvasColour) {
|
||||
this._color = value === '' ? null : value
|
||||
}
|
||||
|
||||
public get isFloatingOutput(): boolean {
|
||||
return this.origin_id === UNASSIGNED_NODE_ID && this.origin_slot === -1
|
||||
}
|
||||
|
||||
public get isFloatingInput(): boolean {
|
||||
return this.target_id === UNASSIGNED_NODE_ID && this.target_slot === -1
|
||||
}
|
||||
|
||||
public get isFloating(): boolean {
|
||||
return this.isFloatingOutput || this.isFloatingInput
|
||||
}
|
||||
|
||||
/** `true` if this link is connected to a subgraph input node (the actual origin is in a different graph). */
|
||||
get originIsIoNode(): boolean {
|
||||
return this.origin_id === SUBGRAPH_INPUT_ID
|
||||
}
|
||||
|
||||
/** `true` if this link is connected to a subgraph output node (the actual target is in a different graph). */
|
||||
get targetIsIoNode(): boolean {
|
||||
return this.target_id === SUBGRAPH_OUTPUT_ID
|
||||
}
|
||||
|
||||
constructor(
|
||||
id: LinkId,
|
||||
type: ISlotType,
|
||||
origin_id: SerializedNodeId,
|
||||
origin_slot: number,
|
||||
target_id: SerializedNodeId,
|
||||
target_slot: number,
|
||||
parentId?: RerouteId
|
||||
) {
|
||||
this._state = {
|
||||
id,
|
||||
graphId: toOwningGraphId(zeroUuid),
|
||||
type,
|
||||
originNodeId: toNodeId(origin_id),
|
||||
originSlot: origin_slot,
|
||||
targetNodeId: toNodeId(target_id),
|
||||
targetSlot: target_slot,
|
||||
parentId
|
||||
}
|
||||
this._data = null
|
||||
this._pos = [0, 0]
|
||||
defineEnumerableTopologyFacade(this)
|
||||
}
|
||||
|
||||
/** @deprecated Use {@link LLink.create} */
|
||||
static createFromArray(data: SerialisedLLinkArray): LLink {
|
||||
return new LLink(
|
||||
toLinkId(data[0]),
|
||||
data[5],
|
||||
data[1],
|
||||
data[2],
|
||||
data[3],
|
||||
data[4]
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* LLink static factory: creates a new LLink from the provided data.
|
||||
* @param data Serialised LLink data to create the link from
|
||||
* @returns A new LLink
|
||||
*/
|
||||
static create(data: SerialisableLLink): LLink {
|
||||
return new LLink(
|
||||
toLinkId(data.id),
|
||||
data.type,
|
||||
data.origin_id,
|
||||
data.origin_slot,
|
||||
data.target_id,
|
||||
data.target_slot,
|
||||
data.parentId === undefined ? undefined : toRerouteId(data.parentId)
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets all reroutes from the output slot to this segment. If this segment is a reroute, it will not be included.
|
||||
* @returns An ordered array of all reroutes from the node output to
|
||||
* this reroute or the reroute before it. Otherwise, an empty array.
|
||||
*/
|
||||
static getReroutes(
|
||||
network: Pick<ReadonlyLinkNetwork, 'reroutes'>,
|
||||
linkSegment: LinkSegment
|
||||
): Reroute[] {
|
||||
if (linkSegment.parentId === undefined) return []
|
||||
return network.reroutes.get(linkSegment.parentId)?.getReroutes() ?? []
|
||||
}
|
||||
|
||||
static getFirstReroute(
|
||||
network: Pick<ReadonlyLinkNetwork, 'reroutes'>,
|
||||
linkSegment: LinkSegment
|
||||
): Reroute | undefined {
|
||||
return LLink.getReroutes(network, linkSegment).at(0)
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds the reroute in the chain after the provided reroute ID.
|
||||
* @param network The network this link belongs to
|
||||
* @param linkSegment The starting point of the search (input side).
|
||||
* Typically the LLink object itself, but can be any link segment.
|
||||
* @param rerouteId The matching reroute will have this set as its {@link parentId}.
|
||||
* @returns The reroute that was found, `undefined` if no reroute was found, or `null` if an infinite loop was detected.
|
||||
*/
|
||||
static findNextReroute(
|
||||
network: Pick<ReadonlyLinkNetwork, 'reroutes'>,
|
||||
linkSegment: LinkSegment,
|
||||
rerouteId: RerouteId
|
||||
): Reroute | null | undefined {
|
||||
if (linkSegment.parentId === undefined) return
|
||||
return network.reroutes
|
||||
.get(linkSegment.parentId)
|
||||
?.findNextReroute(rerouteId)
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the origin node of a link.
|
||||
* @param network The network to search
|
||||
* @param linkId The ID of the link to get the origin node of
|
||||
* @returns The origin node of the link, or `undefined` if the link is not found or the origin node is not found
|
||||
*/
|
||||
static getOriginNode(
|
||||
network: BasicReadonlyNetwork,
|
||||
linkId: LinkId
|
||||
): LGraphNode | undefined {
|
||||
const id = network.links.get(linkId)?.origin_id
|
||||
return network.getNodeById(id) ?? undefined
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the target node of a link.
|
||||
* @param network The network to search
|
||||
* @param linkId The ID of the link to get the target node of
|
||||
* @returns The target node of the link, or `undefined` if the link is not found or the target node is not found
|
||||
*/
|
||||
static getTargetNode(
|
||||
network: BasicReadonlyNetwork,
|
||||
linkId: LinkId
|
||||
): LGraphNode | undefined {
|
||||
const id = network.links.get(linkId)?.target_id
|
||||
return network.getNodeById(id) ?? undefined
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolves a link ID to the link, node, and slot objects.
|
||||
* @param linkId The {@link id} of the link to resolve
|
||||
* @param network The link network to search
|
||||
* @returns An object containing the input and output nodes, as well as the input and output slots.
|
||||
* @remarks This method is heavier than others; it will always resolve all objects.
|
||||
* Whilst the performance difference should in most cases be negligible,
|
||||
* it is recommended to use simpler methods where appropriate.
|
||||
*/
|
||||
static resolve(
|
||||
linkId: LinkId | null | undefined,
|
||||
network: BasicReadonlyNetwork
|
||||
): ResolvedConnection | undefined {
|
||||
return network.getLink(linkId)?.resolve(network)
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolves a list of link IDs to the link, node, and slot objects.
|
||||
* Discards invalid link IDs.
|
||||
* @param linkIds An iterable of link {@link id}s to resolve
|
||||
* @param network The link network to search
|
||||
* @returns An array of resolved connections. If a link is not found, it is not included in the array.
|
||||
* @see {@link LLink.resolve}
|
||||
*/
|
||||
static resolveMany(
|
||||
linkIds: Iterable<LinkId>,
|
||||
network: BasicReadonlyNetwork
|
||||
): ResolvedConnection[] {
|
||||
const resolved: ResolvedConnection[] = []
|
||||
for (const id of linkIds) {
|
||||
const r = network.getLink(id)?.resolve(network)
|
||||
if (r) resolved.push(r)
|
||||
}
|
||||
return resolved
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolves the primitive ID values stored in the link to the referenced objects.
|
||||
* @param network The link network to search
|
||||
* @returns An object containing the input and output nodes, as well as the input and output slots.
|
||||
* @remarks This method is heavier than others; it will always resolve all objects.
|
||||
* Whilst the performance difference should in most cases be negligible,
|
||||
* it is recommended to use simpler methods where appropriate.
|
||||
*/
|
||||
resolve(network: BasicReadonlyNetwork): ResolvedConnection {
|
||||
const inputNode =
|
||||
this.target_id === UNASSIGNED_NODE_ID
|
||||
? undefined
|
||||
: (network.getNodeById(this.target_id) ?? undefined)
|
||||
const input = inputNode?.inputs[this.target_slot]
|
||||
const subgraphInput = this.originIsIoNode
|
||||
? network.inputNode?.slots[this.origin_slot]
|
||||
: undefined
|
||||
if (subgraphInput) {
|
||||
return { inputNode, input, subgraphInput, link: this }
|
||||
}
|
||||
|
||||
const outputNode =
|
||||
this.origin_id === UNASSIGNED_NODE_ID
|
||||
? undefined
|
||||
: (network.getNodeById(this.origin_id) ?? undefined)
|
||||
const output = outputNode?.outputs[this.origin_slot]
|
||||
const subgraphOutput = this.targetIsIoNode
|
||||
? network.outputNode?.slots[this.target_slot]
|
||||
: undefined
|
||||
if (subgraphOutput) {
|
||||
return {
|
||||
outputNode,
|
||||
output,
|
||||
subgraphInput: undefined,
|
||||
subgraphOutput,
|
||||
link: this
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
inputNode,
|
||||
outputNode,
|
||||
input,
|
||||
output,
|
||||
subgraphInput,
|
||||
subgraphOutput,
|
||||
link: this
|
||||
}
|
||||
}
|
||||
|
||||
configure(o: LLink | SerialisedLLinkArray) {
|
||||
if (Array.isArray(o)) {
|
||||
this.id = toLinkId(o[0])
|
||||
this.updateEndpoints({
|
||||
originNodeId: toNodeId(o[1]),
|
||||
originSlot: o[2],
|
||||
targetNodeId: toNodeId(o[3]),
|
||||
targetSlot: o[4]
|
||||
})
|
||||
this.type = o[5]
|
||||
} else {
|
||||
this.id = o.id
|
||||
this.type = o.type
|
||||
this.updateEndpoints({
|
||||
originNodeId: o.origin_id,
|
||||
originSlot: o.origin_slot,
|
||||
targetNodeId: o.target_id,
|
||||
targetSlot: o.target_slot
|
||||
})
|
||||
this.parentId = o.parentId
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the specified node id and output index are this link's origin (output side).
|
||||
* @param nodeId ID of the node to check
|
||||
* @param outputIndex The array index of the node output
|
||||
* @returns `true` if the origin matches, otherwise `false`.
|
||||
*/
|
||||
hasOrigin(nodeId: SerializedNodeId, outputIndex: number): boolean {
|
||||
return (
|
||||
this.origin_id === toNodeId(nodeId) && this.origin_slot === outputIndex
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the specified node id and input index are this link's target (input side).
|
||||
* @param nodeId ID of the node to check
|
||||
* @param inputIndex The array index of the node input
|
||||
* @returns `true` if the target matches, otherwise `false`.
|
||||
*/
|
||||
hasTarget(nodeId: SerializedNodeId, inputIndex: number): boolean {
|
||||
return (
|
||||
this.target_id === toNodeId(nodeId) && this.target_slot === inputIndex
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a floating link from this link.
|
||||
* @param slotType The side of the link that is still connected
|
||||
* @param parentId The parent reroute ID of the link
|
||||
* @returns A new LLink that is floating
|
||||
*/
|
||||
toFloating(slotType: 'input' | 'output', parentId: RerouteId): LLink {
|
||||
const exported = this.asSerialisable()
|
||||
exported.id = toLinkId(-1)
|
||||
exported.parentId = parentId
|
||||
|
||||
if (slotType === 'input') {
|
||||
exported.origin_id = UNASSIGNED_NODE_ID
|
||||
exported.origin_slot = -1
|
||||
} else {
|
||||
exported.target_id = UNASSIGNED_NODE_ID
|
||||
exported.target_slot = -1
|
||||
}
|
||||
|
||||
return LLink.create(exported)
|
||||
}
|
||||
|
||||
/**
|
||||
* Disconnects a link and removes it from the graph, cleaning up any reroutes that are no longer used
|
||||
* @param network The container (LGraph) where reroutes should be updated
|
||||
* @param keepReroutes If `undefined`, reroutes will be automatically removed if no links remain.
|
||||
* If `input` or `output`, reroutes will not be automatically removed, and retain a connection to the input or output, respectively.
|
||||
*/
|
||||
disconnect(network: LinkNetwork, keepReroutes?: 'input' | 'output'): void {
|
||||
const reroutes = LLink.getReroutes(network, this)
|
||||
|
||||
const lastReroute = reroutes.at(-1)
|
||||
|
||||
// When floating from output, 1-to-1 ratio of floating link to final reroute (tree-like)
|
||||
const outputFloating =
|
||||
keepReroutes === 'output' &&
|
||||
lastReroute !== undefined &&
|
||||
(lastReroute.linkIds.size === 0 ||
|
||||
(lastReroute.linkIds.size === 1 && lastReroute.linkIds.has(this.id))) &&
|
||||
lastReroute.floatingLinkIds.size === 0
|
||||
|
||||
// When floating from inputs, the final (input side) reroute may have many floating links
|
||||
if (outputFloating || (keepReroutes === 'input' && lastReroute)) {
|
||||
const newLink = LLink.create(this)
|
||||
newLink.id = toLinkId(-1)
|
||||
|
||||
if (keepReroutes === 'input') {
|
||||
newLink.origin_id = UNASSIGNED_NODE_ID
|
||||
newLink.origin_slot = -1
|
||||
|
||||
lastReroute.floating = { slotType: 'input' }
|
||||
} else {
|
||||
newLink.target_id = UNASSIGNED_NODE_ID
|
||||
newLink.target_slot = -1
|
||||
|
||||
lastReroute.floating = { slotType: 'output' }
|
||||
}
|
||||
|
||||
network.addFloatingLink(newLink)
|
||||
}
|
||||
|
||||
if (network.links.get(this.id) === this) network.links.delete(this.id)
|
||||
unregisterLinkTopology(this)
|
||||
layoutStore.deleteLinkLayout(this.id)
|
||||
|
||||
for (const reroute of reroutes) {
|
||||
if (!keepReroutes && !reroute.totalLinks) {
|
||||
network._removeReroute(reroute.id)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated Prefer {@link LLink.asSerialisable} (returns an object, not an array)
|
||||
* @returns An array representing this LLink
|
||||
*/
|
||||
serialize(): SerialisedLLinkArray {
|
||||
return [
|
||||
this.id,
|
||||
serializeNodeId(this.origin_id),
|
||||
this.origin_slot,
|
||||
serializeNodeId(this.target_id),
|
||||
this.target_slot,
|
||||
this.type
|
||||
]
|
||||
}
|
||||
|
||||
asSerialisable(): SerialisableLLink {
|
||||
const copy: SerialisableLLink = {
|
||||
id: this.id,
|
||||
origin_id: serializeNodeId(this.origin_id),
|
||||
origin_slot: this.origin_slot,
|
||||
target_id: serializeNodeId(this.target_id),
|
||||
target_slot: this.target_slot,
|
||||
type: this.type
|
||||
}
|
||||
if (this.parentId !== undefined) copy.parentId = this.parentId
|
||||
return copy
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds the floating links attached to a slot. A floating link has exactly
|
||||
* one assigned endpoint, so its attachment is fully encoded in its own
|
||||
* origin/target fields; nothing is stored on the slot.
|
||||
* @param network The network whose floating links to search
|
||||
* @param side Which side of the slot's node the links attach to
|
||||
* @param nodeId The node (or subgraph IO node id) owning the slot
|
||||
* @param slot The slot index
|
||||
*/
|
||||
export function slotFloatingLinks(
|
||||
network: Pick<ReadonlyLinkNetwork, 'floatingLinks'>,
|
||||
side: 'input' | 'output',
|
||||
nodeId: NodeId,
|
||||
slot: number
|
||||
): LLink[] {
|
||||
const result: LLink[] = []
|
||||
for (const link of network.floatingLinks.values()) {
|
||||
const attached =
|
||||
side === 'input'
|
||||
? link.target_id === nodeId && link.target_slot === slot
|
||||
: link.origin_id === nodeId && link.origin_slot === slot
|
||||
if (attached) result.push(link)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers a link's topology into {@link useLinkStore} and adopts the
|
||||
* store's reactive proxy as {@link LLink._state}, so the store and the link
|
||||
* always agree and field writes are tracked. Call this at every site that
|
||||
* adds a link to a graph's link map (or floating link map).
|
||||
*
|
||||
* {@link LLink._graphScope} is only set when the store keeps this link's state:
|
||||
* a link that loses a first-wins id collision stays detached, so its writes
|
||||
* and removal cannot corrupt the winner's registration.
|
||||
* @param graph The graph (or subgraph) the link belongs to
|
||||
* @param link The link to register
|
||||
*/
|
||||
export function registerLinkTopology(
|
||||
graph: Pick<LGraph, 'rootGraph' | 'id'>,
|
||||
link: LLink
|
||||
): boolean {
|
||||
if (link.id === toLinkId(-1)) return false
|
||||
const scope = graphScopeOf(graph)
|
||||
const registered = useLinkStore().registerLink(scope, link._state)
|
||||
if (!registered) return false
|
||||
adoptLinkTopology(link, scope, registered)
|
||||
return true
|
||||
}
|
||||
|
||||
export function replaceLinkTopology(
|
||||
graph: Pick<LGraph, 'rootGraph' | 'id'>,
|
||||
incumbent: LLink | undefined,
|
||||
replacement: LLink
|
||||
): boolean {
|
||||
if (replacement.id === toLinkId(-1)) return false
|
||||
const scope = graphScopeOf(graph)
|
||||
const registered = useLinkStore().replaceLink(
|
||||
scope,
|
||||
incumbent?._state,
|
||||
replacement._state
|
||||
)
|
||||
if (!registered) return false
|
||||
if (incumbent) {
|
||||
linkByTopology.delete(toRaw(incumbent._state))
|
||||
incumbent._graphScope = undefined
|
||||
}
|
||||
adoptLinkTopology(replacement, scope, registered)
|
||||
return true
|
||||
}
|
||||
|
||||
function adoptLinkTopology(
|
||||
link: LLink,
|
||||
scope: GraphScope,
|
||||
registered: LinkTopology
|
||||
): void {
|
||||
link._state = registered
|
||||
link._graphScope = scope
|
||||
linkByTopology.set(toRaw(registered), link)
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes a link's topology from {@link useLinkStore} and detaches the link.
|
||||
* No-op for links that never won registration ({@link LLink._graphScope} unset),
|
||||
* so a first-wins collision loser cannot remove the winner's entry.
|
||||
* @param link The link to unregister
|
||||
*/
|
||||
export function unregisterLinkTopology(link: LLink): void {
|
||||
if (!link._graphScope) return
|
||||
useLinkStore().deleteLink(link._graphScope, link._state)
|
||||
linkByTopology.delete(toRaw(link._state))
|
||||
link._graphScope = undefined
|
||||
}
|
||||
|
||||
/**
|
||||
* Unregisters every link and floating link a graph owns. Used when a graph's
|
||||
* links leave the store without a whole-bucket wipe: subgraph-definition
|
||||
* removal, and clearing a graph that shares its bucket with other graphs.
|
||||
* @param graph The graph whose links should be unregistered
|
||||
*/
|
||||
export function unregisterAllLinkTopologies(
|
||||
graph: Pick<LGraph, 'links' | 'floatingLinks'>
|
||||
): void {
|
||||
for (const link of [
|
||||
...graph.links.values(),
|
||||
...graph.floatingLinks.values()
|
||||
]) {
|
||||
unregisterLinkTopology(link)
|
||||
}
|
||||
}
|
||||
+431
@@ -0,0 +1,431 @@
|
||||
import { inputLinkId } from '@/lib/litegraph/src/node/slotLinks'
|
||||
import type { LGraph } from '@/lib/litegraph/src/LGraph'
|
||||
import type { LGraphNode } from '@/lib/litegraph/src/LGraphNode'
|
||||
import type { SerializedNodeId } from '@/types/nodeId'
|
||||
import type { LinkId } from '@/lib/litegraph/src/LLink'
|
||||
import { InvalidLinkError } from '@/lib/litegraph/src/infrastructure/InvalidLinkError'
|
||||
import { useWidgetValueStore } from '@/stores/widgetValueStore'
|
||||
import { NullGraphError } from '@/lib/litegraph/src/infrastructure/NullGraphError'
|
||||
import { RecursionError } from '@/lib/litegraph/src/infrastructure/RecursionError'
|
||||
import { SlotIndexError } from '@/lib/litegraph/src/infrastructure/SlotIndexError'
|
||||
import type { ISlotType } from '@/lib/litegraph/src/interfaces'
|
||||
import { LGraphEventMode, LiteGraph } from '@/lib/litegraph/src/litegraph'
|
||||
|
||||
import type { Subgraph } from './Subgraph'
|
||||
import type { SubgraphNode } from './SubgraphNode'
|
||||
|
||||
export type ExecutionId = string
|
||||
|
||||
/**
|
||||
* Interface describing the data transfer objects used when compiling a graph for execution.
|
||||
*/
|
||||
export type ExecutableLGraphNode = Omit<
|
||||
ExecutableNodeDTO,
|
||||
'graph' | 'node' | 'subgraphNode'
|
||||
>
|
||||
|
||||
/**
|
||||
* The end result of resolving a DTO input.
|
||||
* When a widget value is returned, {@link widgetInfo} is present and {@link origin_slot} is `-1`.
|
||||
*/
|
||||
type ResolvedInput = {
|
||||
/** DTO for the node that the link originates from. */
|
||||
node: ExecutableLGraphNode
|
||||
/** Full unique execution ID of the node that the link originates from. In the case of a widget value, this is the ID of the subgraph node. */
|
||||
origin_id: ExecutionId
|
||||
/** The slot index of the output on the node that the link originates from. `-1` when widget value is set. */
|
||||
origin_slot: number
|
||||
/** Boxed widget value (e.g. for widgets). If this box is `undefined`, then an input link is connected, and widget values from the subgraph node are ignored. */
|
||||
widgetInfo?: { value: unknown }
|
||||
}
|
||||
|
||||
/**
|
||||
* Concrete implementation of {@link ExecutableLGraphNode}.
|
||||
* @remarks This is the class that is used to create the data transfer objects for executable nodes.
|
||||
*/
|
||||
export class ExecutableNodeDTO implements ExecutableLGraphNode {
|
||||
applyToGraph?(
|
||||
...args: Parameters<NonNullable<typeof this.node.applyToGraph>>
|
||||
): ReturnType<NonNullable<typeof this.node.applyToGraph>>
|
||||
|
||||
/** The graph that this node is a part of. */
|
||||
readonly graph: LGraph | Subgraph
|
||||
|
||||
inputs: { linkId: LinkId | null; name: string; type: ISlotType }[]
|
||||
|
||||
/** Backing field for {@link id}. */
|
||||
private _id: ExecutionId
|
||||
|
||||
/**
|
||||
* The path to the actual node through subgraph instances, represented as a list of all subgraph node IDs (instances),
|
||||
* followed by the actual original node ID within the subgraph. Each segment is separated by `:`.
|
||||
*
|
||||
* e.g. `1:2:3`:
|
||||
* - `1` is the node ID of the first subgraph node in the parent workflow
|
||||
* - `2` is the node ID of the second subgraph node in the first subgraph
|
||||
* - `3` is the node ID of the actual node in the subgraph definition
|
||||
*/
|
||||
get id() {
|
||||
return this._id
|
||||
}
|
||||
|
||||
get type() {
|
||||
return this.node.type
|
||||
}
|
||||
|
||||
get title() {
|
||||
return this.node.title
|
||||
}
|
||||
|
||||
get mode() {
|
||||
return this.node.mode
|
||||
}
|
||||
|
||||
get comfyClass() {
|
||||
return this.node.comfyClass
|
||||
}
|
||||
|
||||
get isVirtualNode() {
|
||||
return this.node.isVirtualNode
|
||||
}
|
||||
|
||||
get widgets() {
|
||||
return this.node.widgets
|
||||
}
|
||||
|
||||
get subgraphId() {
|
||||
return this.subgraphNode?.subgraph.id
|
||||
}
|
||||
|
||||
constructor(
|
||||
/** The actual node that this DTO wraps. */
|
||||
readonly node: LGraphNode | SubgraphNode,
|
||||
/** A list of subgraph instance node IDs from the root graph to the containing instance. @see {@link id} */
|
||||
readonly subgraphNodePath: readonly SerializedNodeId[],
|
||||
/** A flattened map of all DTOs in this node network. Subgraph instances have been expanded into their inner nodes. */
|
||||
readonly nodesByExecutionId: Map<ExecutionId, ExecutableLGraphNode>,
|
||||
/** The actual subgraph instance that contains this node, otherwise undefined. */
|
||||
readonly subgraphNode?: SubgraphNode
|
||||
) {
|
||||
if (!node.graph) throw new NullGraphError()
|
||||
|
||||
// Set the internal ID of the DTO
|
||||
this._id = [...this.subgraphNodePath, this.node.id].join(':')
|
||||
this.graph = node.graph
|
||||
this.inputs = this.node.inputs.map((x, index) => ({
|
||||
linkId: inputLinkId(node.graph!, node.id, index) ?? null,
|
||||
name: x.name,
|
||||
type: x.type
|
||||
}))
|
||||
|
||||
// Only create a wrapper if the node has an applyToGraph method
|
||||
if (this.node.applyToGraph) {
|
||||
this.applyToGraph = (...args) => this.node.applyToGraph?.(...args)
|
||||
}
|
||||
}
|
||||
|
||||
/** Returns either the DTO itself, or the DTOs of the inner nodes of the subgraph. */
|
||||
getInnerNodes(): ExecutableLGraphNode[] {
|
||||
return this.node.isSubgraphNode()
|
||||
? this.node.getInnerNodes(this.nodesByExecutionId, this.subgraphNodePath)
|
||||
: [this]
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolves the executable node & link IDs for a given input slot.
|
||||
* @param slot The slot index of the input.
|
||||
* @param visited Leave empty unless overriding this method.
|
||||
* A set of unique IDs, used to guard against infinite recursion.
|
||||
* If overriding, ensure that the set is passed on all recursive calls.
|
||||
* @returns The node and the origin ID / slot index of the output.
|
||||
*/
|
||||
resolveInput(
|
||||
slot: number,
|
||||
visited = new Set<string>(),
|
||||
type?: ISlotType
|
||||
): ResolvedInput | undefined {
|
||||
const uniqueId = `${this.subgraphNode?.subgraph.id}:${this.node.id}[I]${slot}`
|
||||
if (visited.has(uniqueId)) {
|
||||
const nodeInfo = `${this.node.id}${this.node.title ? ` (${this.node.title})` : ''}`
|
||||
const pathInfo =
|
||||
this.subgraphNodePath.length > 0
|
||||
? ` at path ${this.subgraphNodePath.join(':')}`
|
||||
: ''
|
||||
throw new RecursionError(
|
||||
`Circular reference detected while resolving input ${slot} of node ${nodeInfo}${pathInfo}. ` +
|
||||
`This creates an infinite loop in link resolution. UniqueID: [${uniqueId}]`
|
||||
)
|
||||
}
|
||||
visited.add(uniqueId)
|
||||
|
||||
const input = this.inputs.at(slot)
|
||||
if (!input)
|
||||
throw new SlotIndexError(
|
||||
`No input found for flattened id [${this.id}] slot [${slot}]`
|
||||
)
|
||||
|
||||
// Nothing connected
|
||||
if (input.linkId == null) return
|
||||
|
||||
const link = this.graph.getLink(input.linkId)
|
||||
if (!link)
|
||||
throw new InvalidLinkError(
|
||||
`No link found in parent graph for id [${this.id}] slot [${slot}] ${input.name}`
|
||||
)
|
||||
|
||||
const { subgraphNode } = this
|
||||
|
||||
// Link goes up and out of this subgraph
|
||||
if (subgraphNode && link.originIsIoNode) {
|
||||
const subgraphNodeInput = subgraphNode.inputs.at(link.origin_slot)
|
||||
if (!subgraphNodeInput)
|
||||
throw new SlotIndexError(
|
||||
`No input found for slot [${link.origin_slot}] ${input.name}`
|
||||
)
|
||||
|
||||
if (!subgraphNode.graph)
|
||||
throw new NullGraphError(
|
||||
`SubgraphNode ${subgraphNode.id} has no graph during input resolution`
|
||||
)
|
||||
// Nothing connected
|
||||
const linkId =
|
||||
inputLinkId(subgraphNode.graph, subgraphNode.id, link.origin_slot) ??
|
||||
null
|
||||
if (linkId === null) {
|
||||
const id = subgraphNodeInput.widgetId
|
||||
if (!id) return
|
||||
|
||||
return {
|
||||
node: this,
|
||||
origin_id: this.id,
|
||||
origin_slot: -1,
|
||||
widgetInfo: { value: useWidgetValueStore().getWidget(id)?.value }
|
||||
}
|
||||
}
|
||||
|
||||
const outerLink = subgraphNode.graph.getLink(linkId)
|
||||
if (!outerLink)
|
||||
throw new InvalidLinkError(
|
||||
`No outer link found for slot [${link.origin_slot}] ${input.name}`
|
||||
)
|
||||
|
||||
const subgraphNodeExecutionId: ExecutionId =
|
||||
this.subgraphNodePath.join(':')
|
||||
const subgraphNodeDto = this.nodesByExecutionId.get(
|
||||
subgraphNodeExecutionId
|
||||
)
|
||||
if (!subgraphNodeDto)
|
||||
throw new Error(
|
||||
`No subgraph node DTO found for id [${subgraphNodeExecutionId}]`
|
||||
)
|
||||
|
||||
return subgraphNodeDto.resolveInput(outerLink.target_slot, visited)
|
||||
}
|
||||
|
||||
// Not part of a subgraph; use the original link
|
||||
const outputNode = this.graph.getNodeById(link.origin_id)
|
||||
if (!outputNode)
|
||||
throw new InvalidLinkError(
|
||||
`No input node found for id [${this.id}] slot [${slot}] ${input.name}`
|
||||
)
|
||||
|
||||
const outputNodeExecutionId: ExecutionId = [
|
||||
...this.subgraphNodePath,
|
||||
outputNode.id
|
||||
].join(':')
|
||||
const outputNodeDto = this.nodesByExecutionId.get(outputNodeExecutionId)
|
||||
if (!outputNodeDto)
|
||||
throw new Error(
|
||||
`No output node DTO found for id [${outputNodeExecutionId}]`
|
||||
)
|
||||
|
||||
return outputNodeDto.resolveOutput(
|
||||
link.origin_slot,
|
||||
type ?? input.type,
|
||||
visited
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines whether this output is a valid endpoint for a link (non-virtual, non-bypass).
|
||||
* @param slot The slot index of the output.
|
||||
* @param type The type of the input
|
||||
* @param visited A set of unique IDs to guard against infinite recursion. See {@link resolveInput}.
|
||||
* @returns The node and the origin ID / slot index of the output.
|
||||
*/
|
||||
resolveOutput(
|
||||
slot: number,
|
||||
type: ISlotType,
|
||||
visited: Set<string>
|
||||
): ResolvedInput | undefined {
|
||||
const uniqueId = `${this.subgraphNode?.subgraph.id}:${this.node.id}[O]${slot}`
|
||||
if (visited.has(uniqueId)) {
|
||||
const nodeInfo = `${this.node.id}${this.node.title ? ` (${this.node.title})` : ''}`
|
||||
const pathInfo =
|
||||
this.subgraphNodePath.length > 0
|
||||
? ` at path ${this.subgraphNodePath.join(':')}`
|
||||
: ''
|
||||
throw new RecursionError(
|
||||
`Circular reference detected while resolving output ${slot} of node ${nodeInfo}${pathInfo}. ` +
|
||||
`This creates an infinite loop in link resolution. UniqueID: [${uniqueId}]`
|
||||
)
|
||||
}
|
||||
visited.add(uniqueId)
|
||||
|
||||
// Muted nodes produce no output
|
||||
if (this.mode === LGraphEventMode.NEVER) return
|
||||
|
||||
// Upstreamed: Bypass nodes are bypassed using the first input with matching type
|
||||
if (this.mode === LGraphEventMode.BYPASS) {
|
||||
// Bypass nodes by finding first input with matching type
|
||||
const matchingIndex = this._getBypassSlotIndex(slot, type)
|
||||
|
||||
// No input types match - bypass not possible
|
||||
if (matchingIndex === -1) {
|
||||
console.warn(
|
||||
`[ExecutableNodeDTO.resolveOutput] No input types match type [${type}] for id [${this.id}] slot [${slot}]`,
|
||||
this
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
return this.resolveInput(matchingIndex, visited)
|
||||
}
|
||||
|
||||
const { node } = this
|
||||
if (node.isSubgraphNode())
|
||||
return this._resolveSubgraphOutput(slot, type, visited)
|
||||
|
||||
if (node.isVirtualNode) {
|
||||
// Cross-graph virtual nodes (e.g. Set/Get) resolve their source directly.
|
||||
const virtualSource = this.node.resolveVirtualOutput?.(slot)
|
||||
if (virtualSource) {
|
||||
const inputNodeDto = [...this.nodesByExecutionId.values()].find(
|
||||
(dto) =>
|
||||
dto instanceof ExecutableNodeDTO && dto.node === virtualSource.node
|
||||
)
|
||||
if (!inputNodeDto)
|
||||
throw new Error(
|
||||
`No DTO found for virtual source node [${virtualSource.node.id}]`
|
||||
)
|
||||
|
||||
return inputNodeDto.resolveOutput(virtualSource.slot, type, visited)
|
||||
}
|
||||
const virtualLink = this.node.getInputLink(slot)
|
||||
if (virtualLink) {
|
||||
const { inputNode } = virtualLink.resolve(this.graph)
|
||||
if (!inputNode)
|
||||
throw new InvalidLinkError(
|
||||
`Virtual node failed to resolve parent [${this.id}] slot [${slot}]`
|
||||
)
|
||||
|
||||
const inputNodeExecutionId: ExecutionId = [
|
||||
...this.subgraphNodePath,
|
||||
inputNode.id
|
||||
].join(':')
|
||||
const inputNodeDto = this.nodesByExecutionId.get(inputNodeExecutionId)
|
||||
if (!inputNodeDto)
|
||||
throw new Error(`No input node DTO found for id [${inputNode.id}]`)
|
||||
|
||||
return inputNodeDto.resolveInput(virtualLink.target_slot, visited, type)
|
||||
}
|
||||
|
||||
// Virtual nodes without a matching input should be discarded.
|
||||
return
|
||||
}
|
||||
|
||||
return {
|
||||
node: this,
|
||||
origin_id: this.id,
|
||||
origin_slot: slot
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds the index of the input slot on this node that matches the given output {@link slot} index.
|
||||
* Used when bypassing nodes.
|
||||
* @param slot The output slot index on this node
|
||||
* @param type The type of the final target input (so type list matches are accurate)
|
||||
* @returns The index of the input slot on this node, otherwise `-1`.
|
||||
*/
|
||||
private _getBypassSlotIndex(slot: number, type: ISlotType) {
|
||||
const { inputs } = this
|
||||
const oppositeInput = inputs[slot]
|
||||
const outputType = this.node.outputs[slot].type
|
||||
|
||||
// Any type short circuit - match slot ID, fallback to first slot
|
||||
if (type === '*' || type === '') {
|
||||
return inputs.length > slot ? slot : 0
|
||||
}
|
||||
|
||||
// Prefer input with the same slot ID
|
||||
if (
|
||||
oppositeInput &&
|
||||
LiteGraph.isValidConnection(oppositeInput.type, outputType) &&
|
||||
LiteGraph.isValidConnection(oppositeInput.type, type)
|
||||
) {
|
||||
return slot
|
||||
}
|
||||
|
||||
// Preserve legacy behaviour; use exact match first.
|
||||
const exactMatch = inputs.findIndex((input) => input.type === type)
|
||||
if (exactMatch !== -1) return exactMatch
|
||||
|
||||
// Find first matching slot - prefer exact type
|
||||
return inputs.findIndex(
|
||||
(input) =>
|
||||
LiteGraph.isValidConnection(input.type, outputType) &&
|
||||
LiteGraph.isValidConnection(input.type, type)
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolves the link inside a subgraph node, from the subgraph IO node to the node inside the subgraph.
|
||||
* @param slot The slot index of the output on the subgraph node.
|
||||
* @param visited A set of unique IDs to guard against infinite recursion. See {@link resolveInput}.
|
||||
* @returns A DTO for the node, and the origin ID / slot index of the output.
|
||||
*/
|
||||
private _resolveSubgraphOutput(
|
||||
slot: number,
|
||||
type: ISlotType,
|
||||
visited: Set<string>
|
||||
): ResolvedInput | undefined {
|
||||
const { node } = this
|
||||
const output = node.outputs.at(slot)
|
||||
|
||||
if (!output)
|
||||
throw new SlotIndexError(
|
||||
`No output found for flattened id [${this.id}] slot [${slot}]`
|
||||
)
|
||||
if (!node.isSubgraphNode())
|
||||
throw new TypeError(`Node is not a subgraph node: ${node.id}`)
|
||||
|
||||
// Link inside the subgraph
|
||||
const innerResolved = node.resolveSubgraphOutputLink(slot)
|
||||
if (!innerResolved) return
|
||||
|
||||
const innerNode = innerResolved.outputNode
|
||||
if (!innerNode)
|
||||
throw new Error(
|
||||
`No output node found for id [${this.id}] slot [${slot}] ${output.name}`
|
||||
)
|
||||
|
||||
// Recurse into the subgraph
|
||||
const innerNodeExecutionId: ExecutionId = [
|
||||
...this.subgraphNodePath,
|
||||
node.id,
|
||||
innerNode.id
|
||||
].join(':')
|
||||
const innerNodeDto = this.nodesByExecutionId.get(innerNodeExecutionId)
|
||||
if (!innerNodeDto)
|
||||
throw new Error(
|
||||
`No inner node DTO found for id [${innerNodeExecutionId}]`
|
||||
)
|
||||
|
||||
return innerNodeDto.resolveOutput(
|
||||
innerResolved.link.origin_slot,
|
||||
type,
|
||||
visited
|
||||
)
|
||||
}
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
// Re-export Subgraph and GraphOrSubgraph from LGraph.ts to maintain compatibility
|
||||
// This is a temporary fix to resolve circular dependency issues
|
||||
export { Subgraph, type GraphOrSubgraph } from '@/lib/litegraph/src/LGraph'
|
||||
+1025
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,192 @@
|
||||
import type {
|
||||
ExecutableLGraphNode,
|
||||
ExecutionId,
|
||||
LGraph
|
||||
} from '@/lib/litegraph/src/litegraph'
|
||||
import {
|
||||
ExecutableNodeDTO,
|
||||
LGraphEventMode
|
||||
} from '@/lib/litegraph/src/litegraph'
|
||||
import type {
|
||||
ComfyApiWorkflow,
|
||||
ComfyWorkflowJSON
|
||||
} from '@/platform/workflow/validation/schemas/workflowSchema'
|
||||
|
||||
import { compressWidgetInputSlots } from './litegraphUtil'
|
||||
|
||||
type ExportedWidgetValueWrapper = {
|
||||
__type__?: unknown
|
||||
__value__: unknown
|
||||
}
|
||||
|
||||
function isExportedWidgetValueWrapper(
|
||||
value: unknown
|
||||
): value is ExportedWidgetValueWrapper {
|
||||
return (
|
||||
typeof value === 'object' &&
|
||||
value !== null &&
|
||||
!Array.isArray(value) &&
|
||||
'__value__' in value
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Inverse of the wrapping applied during Export (API). Curve values carry a
|
||||
* type marker and may be objects; untyped wrappers are reserved for arrays so
|
||||
* ordinary objects containing a `__value__` property pass through unchanged.
|
||||
*/
|
||||
export function unwrapExportedWidgetValue(value: unknown): unknown {
|
||||
if (
|
||||
isExportedWidgetValueWrapper(value) &&
|
||||
(value.__type__ === 'CURVE' || Array.isArray(value.__value__))
|
||||
) {
|
||||
return value.__value__
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the current graph workflow for sending to the API.
|
||||
* @note Node widgets are updated before serialization to prepare queueing.
|
||||
*
|
||||
* @param graph The graph to convert.
|
||||
* @param options The options for the conversion.
|
||||
* - `sortNodes`: Whether to sort the nodes by execution order.
|
||||
* @returns The workflow and node links
|
||||
*/
|
||||
export const graphToPrompt = async (
|
||||
graph: LGraph,
|
||||
options: { sortNodes?: boolean } = {}
|
||||
): Promise<{ workflow: ComfyWorkflowJSON; output: ComfyApiWorkflow }> => {
|
||||
const { sortNodes = false } = options
|
||||
|
||||
for (const node of graph.computeExecutionOrder(false)) {
|
||||
const innerNodes = node.getInnerNodes
|
||||
? node.getInnerNodes(new Map())
|
||||
: [node]
|
||||
for (const innerNode of innerNodes) {
|
||||
if (innerNode.isVirtualNode) {
|
||||
innerNode.applyToGraph?.()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const workflow = graph.serialize({ sortNodes })
|
||||
|
||||
// Remove localized_name from the workflow
|
||||
for (const node of workflow.nodes) {
|
||||
for (const slot of node.inputs ?? []) {
|
||||
delete slot.localized_name
|
||||
}
|
||||
for (const slot of node.outputs ?? []) {
|
||||
delete slot.localized_name
|
||||
}
|
||||
}
|
||||
|
||||
compressWidgetInputSlots(workflow)
|
||||
workflow.extra ??= {}
|
||||
workflow.extra.frontendVersion = __COMFYUI_FRONTEND_VERSION__
|
||||
|
||||
const nodeDtoMap = new Map<ExecutionId, ExecutableLGraphNode>()
|
||||
for (const node of graph.computeExecutionOrder(false)) {
|
||||
const dto: ExecutableLGraphNode = new ExecutableNodeDTO(
|
||||
node,
|
||||
[],
|
||||
nodeDtoMap
|
||||
)
|
||||
|
||||
nodeDtoMap.set(dto.id, dto)
|
||||
|
||||
if (
|
||||
node.mode === LGraphEventMode.NEVER ||
|
||||
node.mode === LGraphEventMode.BYPASS
|
||||
) {
|
||||
continue
|
||||
}
|
||||
|
||||
for (const innerNode of dto.getInnerNodes()) {
|
||||
nodeDtoMap.set(innerNode.id, innerNode)
|
||||
}
|
||||
}
|
||||
|
||||
const output: ComfyApiWorkflow = {}
|
||||
// Process nodes in order of execution
|
||||
for (const node of nodeDtoMap.values()) {
|
||||
// Don't serialize muted nodes
|
||||
if (
|
||||
node.isVirtualNode ||
|
||||
node.mode === LGraphEventMode.NEVER ||
|
||||
node.mode === LGraphEventMode.BYPASS
|
||||
) {
|
||||
continue
|
||||
}
|
||||
|
||||
const inputs: ComfyApiWorkflow[string]['inputs'] = {}
|
||||
const { widgets } = node
|
||||
|
||||
// Store all widget values in the API prompt.
|
||||
// Note: widget.options.serialize controls prompt inclusion (checked here).
|
||||
// widget.serialize controls workflow persistence (checked by LGraphNode).
|
||||
if (widgets) {
|
||||
for (const [i, widget] of widgets.entries()) {
|
||||
if (!widget.name || widget.options?.serialize === false) continue
|
||||
|
||||
const widgetValue = widget.serializeValue
|
||||
? await widget.serializeValue(node, i)
|
||||
: widget.value
|
||||
// By default, Array values are reserved to represent node connections.
|
||||
// We need to wrap the array as an object to avoid the misinterpretation
|
||||
// of the array as a node connection.
|
||||
// The backend automatically unwraps the object to an array during
|
||||
// execution.
|
||||
inputs[widget.name] =
|
||||
widget.type === 'curve' && widgetValue != null
|
||||
? { __type__: 'CURVE', __value__: widgetValue }
|
||||
: Array.isArray(widgetValue)
|
||||
? { __value__: widgetValue }
|
||||
: widgetValue
|
||||
}
|
||||
}
|
||||
|
||||
// Store all node links
|
||||
for (const [i, input] of node.inputs.entries()) {
|
||||
const resolvedInput = node.resolveInput(i)
|
||||
if (!resolvedInput) continue
|
||||
|
||||
// Resolved to an actual widget value rather than a node connection
|
||||
if (resolvedInput.widgetInfo) {
|
||||
const { value } = resolvedInput.widgetInfo
|
||||
inputs[input.name] = Array.isArray(value) ? { __value__: value } : value
|
||||
continue
|
||||
}
|
||||
|
||||
inputs[input.name] = [
|
||||
String(resolvedInput.origin_id),
|
||||
// @ts-expect-error link.origin_slot is already number.
|
||||
parseInt(resolvedInput.origin_slot)
|
||||
]
|
||||
}
|
||||
|
||||
output[String(node.id)] = {
|
||||
inputs,
|
||||
// TODO(huchenlei): Filter out all nodes that cannot be mapped to a
|
||||
// comfyClass.
|
||||
class_type: node.comfyClass!,
|
||||
// Ignored by the backend.
|
||||
_meta: {
|
||||
title: node.title
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Remove inputs connected to removed nodes
|
||||
for (const { inputs } of Object.values(output)) {
|
||||
for (const [i, input] of Object.entries(inputs)) {
|
||||
if (Array.isArray(input) && input.length === 2 && !output[input[0]]) {
|
||||
delete inputs[i]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return { workflow: workflow as ComfyWorkflowJSON, output }
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,46 @@
|
||||
import importlib
|
||||
import pkgutil
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
__repo_name__ = "ComfyUI-QwenASR"
|
||||
__version__ = "1.1.0"
|
||||
|
||||
# Locate current directory
|
||||
current_dir = Path(__file__).parent
|
||||
|
||||
# Ensure current directory is in sys.path
|
||||
if str(current_dir) not in sys.path:
|
||||
sys.path.insert(0, str(current_dir))
|
||||
|
||||
# Initialize node mappings
|
||||
NODE_CLASS_MAPPINGS = {}
|
||||
NODE_DISPLAY_NAME_MAPPINGS = {}
|
||||
WEB_DIRECTORY = "./web"
|
||||
|
||||
|
||||
def load_nodes():
|
||||
"""Automatically discover and load node definitions."""
|
||||
for (_, module_name, _) in pkgutil.iter_modules([str(current_dir)]):
|
||||
if not module_name.startswith("AILab_"):
|
||||
continue
|
||||
try:
|
||||
rel_import = f".{module_name}" if __package__ else module_name
|
||||
module = importlib.import_module(rel_import, package=__package__)
|
||||
if hasattr(module, "NODE_CLASS_MAPPINGS"):
|
||||
NODE_CLASS_MAPPINGS.update(module.NODE_CLASS_MAPPINGS)
|
||||
if hasattr(module, "NODE_DISPLAY_NAME_MAPPINGS"):
|
||||
NODE_DISPLAY_NAME_MAPPINGS.update(module.NODE_DISPLAY_NAME_MAPPINGS)
|
||||
except Exception as e:
|
||||
print(f"[{__repo_name__}] Error loading {module_name}: {e}")
|
||||
|
||||
|
||||
# Load all nodes
|
||||
load_nodes()
|
||||
|
||||
__all__ = ["NODE_CLASS_MAPPINGS", "NODE_DISPLAY_NAME_MAPPINGS", "WEB_DIRECTORY"]
|
||||
|
||||
print(f'\033[36m[{__repo_name__}]\033[0m v'
|
||||
f'\033[93m{__version__}\033[0m | '
|
||||
f'\033[37m{len(NODE_CLASS_MAPPINGS)} nodes\033[0m '
|
||||
f'\033[92mLoaded\033[0m')
|
||||
@@ -0,0 +1,674 @@
|
||||
GNU GENERAL PUBLIC LICENSE
|
||||
Version 3, 29 June 2007
|
||||
|
||||
Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
|
||||
Everyone is permitted to copy and distribute verbatim copies
|
||||
of this license document, but changing it is not allowed.
|
||||
|
||||
Preamble
|
||||
|
||||
The GNU General Public License is a free, copyleft license for
|
||||
software and other kinds of works.
|
||||
|
||||
The licenses for most software and other practical works are designed
|
||||
to take away your freedom to share and change the works. By contrast,
|
||||
the GNU General Public License is intended to guarantee your freedom to
|
||||
share and change all versions of a program--to make sure it remains free
|
||||
software for all its users. We, the Free Software Foundation, use the
|
||||
GNU General Public License for most of our software; it applies also to
|
||||
any other work released this way by its authors. You can apply it to
|
||||
your programs, too.
|
||||
|
||||
When we speak of free software, we are referring to freedom, not
|
||||
price. Our General Public Licenses are designed to make sure that you
|
||||
have the freedom to distribute copies of free software (and charge for
|
||||
them if you wish), that you receive source code or can get it if you
|
||||
want it, that you can change the software or use pieces of it in new
|
||||
free programs, and that you know you can do these things.
|
||||
|
||||
To protect your rights, we need to prevent others from denying you
|
||||
these rights or asking you to surrender the rights. Therefore, you have
|
||||
certain responsibilities if you distribute copies of the software, or if
|
||||
you modify it: responsibilities to respect the freedom of others.
|
||||
|
||||
For example, if you distribute copies of such a program, whether
|
||||
gratis or for a fee, you must pass on to the recipients the same
|
||||
freedoms that you received. You must make sure that they, too, receive
|
||||
or can get the source code. And you must show them these terms so they
|
||||
know their rights.
|
||||
|
||||
Developers that use the GNU GPL protect your rights with two steps:
|
||||
(1) assert copyright on the software, and (2) offer you this License
|
||||
giving you legal permission to copy, distribute and/or modify it.
|
||||
|
||||
For the developers' and authors' protection, the GPL clearly explains
|
||||
that there is no warranty for this free software. For both users' and
|
||||
authors' sake, the GPL requires that modified versions be marked as
|
||||
changed, so that their problems will not be attributed erroneously to
|
||||
authors of previous versions.
|
||||
|
||||
Some devices are designed to deny users access to install or run
|
||||
modified versions of the software inside them, although the manufacturer
|
||||
can do so. This is fundamentally incompatible with the aim of
|
||||
protecting users' freedom to change the software. The systematic
|
||||
pattern of such abuse occurs in the area of products for individuals to
|
||||
use, which is precisely where it is most unacceptable. Therefore, we
|
||||
have designed this version of the GPL to prohibit the practice for those
|
||||
products. If such problems arise substantially in other domains, we
|
||||
stand ready to extend this provision to those domains in future versions
|
||||
of the GPL, as needed to protect the freedom of users.
|
||||
|
||||
Finally, every program is threatened constantly by software patents.
|
||||
States should not allow patents to restrict development and use of
|
||||
software on general-purpose computers, but in those that do, we wish to
|
||||
avoid the special danger that patents applied to a free program could
|
||||
make it effectively proprietary. To prevent this, the GPL assures that
|
||||
patents cannot be used to render the program non-free.
|
||||
|
||||
The precise terms and conditions for copying, distribution and
|
||||
modification follow.
|
||||
|
||||
TERMS AND CONDITIONS
|
||||
|
||||
0. Definitions.
|
||||
|
||||
"This License" refers to version 3 of the GNU General Public License.
|
||||
|
||||
"Copyright" also means copyright-like laws that apply to other kinds of
|
||||
works, such as semiconductor masks.
|
||||
|
||||
"The Program" refers to any copyrightable work licensed under this
|
||||
License. Each licensee is addressed as "you". "Licensees" and
|
||||
"recipients" may be individuals or organizations.
|
||||
|
||||
To "modify" a work means to copy from or adapt all or part of the work
|
||||
in a fashion requiring copyright permission, other than the making of an
|
||||
exact copy. The resulting work is called a "modified version" of the
|
||||
earlier work or a work "based on" the earlier work.
|
||||
|
||||
A "covered work" means either the unmodified Program or a work based
|
||||
on the Program.
|
||||
|
||||
To "propagate" a work means to do anything with it that, without
|
||||
permission, would make you directly or secondarily liable for
|
||||
infringement under applicable copyright law, except executing it on a
|
||||
computer or modifying a private copy. Propagation includes copying,
|
||||
distribution (with or without modification), making available to the
|
||||
public, and in some countries other activities as well.
|
||||
|
||||
To "convey" a work means any kind of propagation that enables other
|
||||
parties to make or receive copies. Mere interaction with a user through
|
||||
a computer network, with no transfer of a copy, is not conveying.
|
||||
|
||||
An interactive user interface displays "Appropriate Legal Notices"
|
||||
to the extent that it includes a convenient and prominently visible
|
||||
feature that (1) displays an appropriate copyright notice, and (2)
|
||||
tells the user that there is no warranty for the work (except to the
|
||||
extent that warranties are provided), that licensees may convey the
|
||||
work under this License, and how to view a copy of this License. If
|
||||
the interface presents a list of user commands or options, such as a
|
||||
menu, a prominent item in the list meets this criterion.
|
||||
|
||||
1. Source Code.
|
||||
|
||||
The "source code" for a work means the preferred form of the work
|
||||
for making modifications to it. "Object code" means any non-source
|
||||
form of a work.
|
||||
|
||||
A "Standard Interface" means an interface that either is an official
|
||||
standard defined by a recognized standards body, or, in the case of
|
||||
interfaces specified for a particular programming language, one that
|
||||
is widely used among developers working in that language.
|
||||
|
||||
The "System Libraries" of an executable work include anything, other
|
||||
than the work as a whole, that (a) is included in the normal form of
|
||||
packaging a Major Component, but which is not part of that Major
|
||||
Component, and (b) serves only to enable use of the work with that
|
||||
Major Component, or to implement a Standard Interface for which an
|
||||
implementation is available to the public in source code form. A
|
||||
"Major Component", in this context, means a major essential component
|
||||
(kernel, window system, and so on) of the specific operating system
|
||||
(if any) on which the executable work runs, or a compiler used to
|
||||
produce the work, or an object code interpreter used to run it.
|
||||
|
||||
The "Corresponding Source" for a work in object code form means all
|
||||
the source code needed to generate, install, and (for an executable
|
||||
work) run the object code and to modify the work, including scripts to
|
||||
control those activities. However, it does not include the work's
|
||||
System Libraries, or general-purpose tools or generally available free
|
||||
programs which are used unmodified in performing those activities but
|
||||
which are not part of the work. For example, Corresponding Source
|
||||
includes interface definition files associated with source files for
|
||||
the work, and the source code for shared libraries and dynamically
|
||||
linked subprograms that the work is specifically designed to require,
|
||||
such as by intimate data communication or control flow between those
|
||||
subprograms and other parts of the work.
|
||||
|
||||
The Corresponding Source need not include anything that users
|
||||
can regenerate automatically from other parts of the Corresponding
|
||||
Source.
|
||||
|
||||
The Corresponding Source for a work in source code form is that
|
||||
same work.
|
||||
|
||||
2. Basic Permissions.
|
||||
|
||||
All rights granted under this License are granted for the term of
|
||||
copyright on the Program, and are irrevocable provided the stated
|
||||
conditions are met. This License explicitly affirms your unlimited
|
||||
permission to run the unmodified Program. The output from running a
|
||||
covered work is covered by this License only if the output, given its
|
||||
content, constitutes a covered work. This License acknowledges your
|
||||
rights of fair use or other equivalent, as provided by copyright law.
|
||||
|
||||
You may make, run and propagate covered works that you do not
|
||||
convey, without conditions so long as your license otherwise remains
|
||||
in force. You may convey covered works to others for the sole purpose
|
||||
of having them make modifications exclusively for you, or provide you
|
||||
with facilities for running those works, provided that you comply with
|
||||
the terms of this License in conveying all material for which you do
|
||||
not control copyright. Those thus making or running the covered works
|
||||
for you must do so exclusively on your behalf, under your direction
|
||||
and control, on terms that prohibit them from making any copies of
|
||||
your copyrighted material outside their relationship with you.
|
||||
|
||||
Conveying under any other circumstances is permitted solely under
|
||||
the conditions stated below. Sublicensing is not allowed; section 10
|
||||
makes it unnecessary.
|
||||
|
||||
3. Protecting Users' Legal Rights From Anti-Circumvention Law.
|
||||
|
||||
No covered work shall be deemed part of an effective technological
|
||||
measure under any applicable law fulfilling obligations under article
|
||||
11 of the WIPO copyright treaty adopted on 20 December 1996, or
|
||||
similar laws prohibiting or restricting circumvention of such
|
||||
measures.
|
||||
|
||||
When you convey a covered work, you waive any legal power to forbid
|
||||
circumvention of technological measures to the extent such circumvention
|
||||
is effected by exercising rights under this License with respect to
|
||||
the covered work, and you disclaim any intention to limit operation or
|
||||
modification of the work as a means of enforcing, against the work's
|
||||
users, your or third parties' legal rights to forbid circumvention of
|
||||
technological measures.
|
||||
|
||||
4. Conveying Verbatim Copies.
|
||||
|
||||
You may convey verbatim copies of the Program's source code as you
|
||||
receive it, in any medium, provided that you conspicuously and
|
||||
appropriately publish on each copy an appropriate copyright notice;
|
||||
keep intact all notices stating that this License and any
|
||||
non-permissive terms added in accord with section 7 apply to the code;
|
||||
keep intact all notices of the absence of any warranty; and give all
|
||||
recipients a copy of this License along with the Program.
|
||||
|
||||
You may charge any price or no price for each copy that you convey,
|
||||
and you may offer support or warranty protection for a fee.
|
||||
|
||||
5. Conveying Modified Source Versions.
|
||||
|
||||
You may convey a work based on the Program, or the modifications to
|
||||
produce it from the Program, in the form of source code under the
|
||||
terms of section 4, provided that you also meet all of these conditions:
|
||||
|
||||
a) The work must carry prominent notices stating that you modified
|
||||
it, and giving a relevant date.
|
||||
|
||||
b) The work must carry prominent notices stating that it is
|
||||
released under this License and any conditions added under section
|
||||
7. This requirement modifies the requirement in section 4 to
|
||||
"keep intact all notices".
|
||||
|
||||
c) You must license the entire work, as a whole, under this
|
||||
License to anyone who comes into possession of a copy. This
|
||||
License will therefore apply, along with any applicable section 7
|
||||
additional terms, to the whole of the work, and all its parts,
|
||||
regardless of how they are packaged. This License gives no
|
||||
permission to license the work in any other way, but it does not
|
||||
invalidate such permission if you have separately received it.
|
||||
|
||||
d) If the work has interactive user interfaces, each must display
|
||||
Appropriate Legal Notices; however, if the Program has interactive
|
||||
interfaces that do not display Appropriate Legal Notices, your
|
||||
work need not make them do so.
|
||||
|
||||
A compilation of a covered work with other separate and independent
|
||||
works, which are not by their nature extensions of the covered work,
|
||||
and which are not combined with it such as to form a larger program,
|
||||
in or on a volume of a storage or distribution medium, is called an
|
||||
"aggregate" if the compilation and its resulting copyright are not
|
||||
used to limit the access or legal rights of the compilation's users
|
||||
beyond what the individual works permit. Inclusion of a covered work
|
||||
in an aggregate does not cause this License to apply to the other
|
||||
parts of the aggregate.
|
||||
|
||||
6. Conveying Non-Source Forms.
|
||||
|
||||
You may convey a covered work in object code form under the terms
|
||||
of sections 4 and 5, provided that you also convey the
|
||||
machine-readable Corresponding Source under the terms of this License,
|
||||
in one of these ways:
|
||||
|
||||
a) Convey the object code in, or embodied in, a physical product
|
||||
(including a physical distribution medium), accompanied by the
|
||||
Corresponding Source fixed on a durable physical medium
|
||||
customarily used for software interchange.
|
||||
|
||||
b) Convey the object code in, or embodied in, a physical product
|
||||
(including a physical distribution medium), accompanied by a
|
||||
written offer, valid for at least three years and valid for as
|
||||
long as you offer spare parts or customer support for that product
|
||||
model, to give anyone who possesses the object code either (1) a
|
||||
copy of the Corresponding Source for all the software in the
|
||||
product that is covered by this License, on a durable physical
|
||||
medium customarily used for software interchange, for a price no
|
||||
more than your reasonable cost of physically performing this
|
||||
conveying of source, or (2) access to copy the
|
||||
Corresponding Source from a network server at no charge.
|
||||
|
||||
c) Convey individual copies of the object code with a copy of the
|
||||
written offer to provide the Corresponding Source. This
|
||||
alternative is allowed only occasionally and noncommercially, and
|
||||
only if you received the object code with such an offer, in accord
|
||||
with subsection 6b.
|
||||
|
||||
d) Convey the object code by offering access from a designated
|
||||
place (gratis or for a charge), and offer equivalent access to the
|
||||
Corresponding Source in the same way through the same place at no
|
||||
further charge. You need not require recipients to copy the
|
||||
Corresponding Source along with the object code. If the place to
|
||||
copy the object code is a network server, the Corresponding Source
|
||||
may be on a different server (operated by you or a third party)
|
||||
that supports equivalent copying facilities, provided you maintain
|
||||
clear directions next to the object code saying where to find the
|
||||
Corresponding Source. Regardless of what server hosts the
|
||||
Corresponding Source, you remain obligated to ensure that it is
|
||||
available for as long as needed to satisfy these requirements.
|
||||
|
||||
e) Convey the object code using peer-to-peer transmission, provided
|
||||
you inform other peers where the object code and Corresponding
|
||||
Source of the work are being offered to the general public at no
|
||||
charge under subsection 6d.
|
||||
|
||||
A separable portion of the object code, whose source code is excluded
|
||||
from the Corresponding Source as a System Library, need not be
|
||||
included in conveying the object code work.
|
||||
|
||||
A "User Product" is either (1) a "consumer product", which means any
|
||||
tangible personal property which is normally used for personal, family,
|
||||
or household purposes, or (2) anything designed or sold for incorporation
|
||||
into a dwelling. In determining whether a product is a consumer product,
|
||||
doubtful cases shall be resolved in favor of coverage. For a particular
|
||||
product received by a particular user, "normally used" refers to a
|
||||
typical or common use of that class of product, regardless of the status
|
||||
of the particular user or of the way in which the particular user
|
||||
actually uses, or expects or is expected to use, the product. A product
|
||||
is a consumer product regardless of whether the product has substantial
|
||||
commercial, industrial or non-consumer uses, unless such uses represent
|
||||
the only significant mode of use of the product.
|
||||
|
||||
"Installation Information" for a User Product means any methods,
|
||||
procedures, authorization keys, or other information required to install
|
||||
and execute modified versions of a covered work in that User Product from
|
||||
a modified version of its Corresponding Source. The information must
|
||||
suffice to ensure that the continued functioning of the modified object
|
||||
code is in no case prevented or interfered with solely because
|
||||
modification has been made.
|
||||
|
||||
If you convey an object code work under this section in, or with, or
|
||||
specifically for use in, a User Product, and the conveying occurs as
|
||||
part of a transaction in which the right of possession and use of the
|
||||
User Product is transferred to the recipient in perpetuity or for a
|
||||
fixed term (regardless of how the transaction is characterized), the
|
||||
Corresponding Source conveyed under this section must be accompanied
|
||||
by the Installation Information. But this requirement does not apply
|
||||
if neither you nor any third party retains the ability to install
|
||||
modified object code on the User Product (for example, the work has
|
||||
been installed in ROM).
|
||||
|
||||
The requirement to provide Installation Information does not include a
|
||||
requirement to continue to provide support service, warranty, or updates
|
||||
for a work that has been modified or installed by the recipient, or for
|
||||
the User Product in which it has been modified or installed. Access to a
|
||||
network may be denied when the modification itself materially and
|
||||
adversely affects the operation of the network or violates the rules and
|
||||
protocols for communication across the network.
|
||||
|
||||
Corresponding Source conveyed, and Installation Information provided,
|
||||
in accord with this section must be in a format that is publicly
|
||||
documented (and with an implementation available to the public in
|
||||
source code form), and must require no special password or key for
|
||||
unpacking, reading or copying.
|
||||
|
||||
7. Additional Terms.
|
||||
|
||||
"Additional permissions" are terms that supplement the terms of this
|
||||
License by making exceptions from one or more of its conditions.
|
||||
Additional permissions that are applicable to the entire Program shall
|
||||
be treated as though they were included in this License, to the extent
|
||||
that they are valid under applicable law. If additional permissions
|
||||
apply only to part of the Program, that part may be used separately
|
||||
under those permissions, but the entire Program remains governed by
|
||||
this License without regard to the additional permissions.
|
||||
|
||||
When you convey a copy of a covered work, you may at your option
|
||||
remove any additional permissions from that copy, or from any part of
|
||||
it. (Additional permissions may be written to require their own
|
||||
removal in certain cases when you modify the work.) You may place
|
||||
additional permissions on material, added by you to a covered work,
|
||||
for which you have or can give appropriate copyright permission.
|
||||
|
||||
Notwithstanding any other provision of this License, for material you
|
||||
add to a covered work, you may (if authorized by the copyright holders of
|
||||
that material) supplement the terms of this License with terms:
|
||||
|
||||
a) Disclaiming warranty or limiting liability differently from the
|
||||
terms of sections 15 and 16 of this License; or
|
||||
|
||||
b) Requiring preservation of specified reasonable legal notices or
|
||||
author attributions in that material or in the Appropriate Legal
|
||||
Notices displayed by works containing it; or
|
||||
|
||||
c) Prohibiting misrepresentation of the origin of that material, or
|
||||
requiring that modified versions of such material be marked in
|
||||
reasonable ways as different from the original version; or
|
||||
|
||||
d) Limiting the use for publicity purposes of names of licensors or
|
||||
authors of the material; or
|
||||
|
||||
e) Declining to grant rights under trademark law for use of some
|
||||
trade names, trademarks, or service marks; or
|
||||
|
||||
f) Requiring indemnification of licensors and authors of that
|
||||
material by anyone who conveys the material (or modified versions of
|
||||
it) with contractual assumptions of liability to the recipient, for
|
||||
any liability that these contractual assumptions directly impose on
|
||||
those licensors and authors.
|
||||
|
||||
All other non-permissive additional terms are considered "further
|
||||
restrictions" within the meaning of section 10. If the Program as you
|
||||
received it, or any part of it, contains a notice stating that it is
|
||||
governed by this License along with a term that is a further
|
||||
restriction, you may remove that term. If a license document contains
|
||||
a further restriction but permits relicensing or conveying under this
|
||||
License, you may add to a covered work material governed by the terms
|
||||
of that license document, provided that the further restriction does
|
||||
not survive such relicensing or conveying.
|
||||
|
||||
If you add terms to a covered work in accord with this section, you
|
||||
must place, in the relevant source files, a statement of the
|
||||
additional terms that apply to those files, or a notice indicating
|
||||
where to find the applicable terms.
|
||||
|
||||
Additional terms, permissive or non-permissive, may be stated in the
|
||||
form of a separately written license, or stated as exceptions;
|
||||
the above requirements apply either way.
|
||||
|
||||
8. Termination.
|
||||
|
||||
You may not propagate or modify a covered work except as expressly
|
||||
provided under this License. Any attempt otherwise to propagate or
|
||||
modify it is void, and will automatically terminate your rights under
|
||||
this License (including any patent licenses granted under the third
|
||||
paragraph of section 11).
|
||||
|
||||
However, if you cease all violation of this License, then your
|
||||
license from a particular copyright holder is reinstated (a)
|
||||
provisionally, unless and until the copyright holder explicitly and
|
||||
finally terminates your license, and (b) permanently, if the copyright
|
||||
holder fails to notify you of the violation by some reasonable means
|
||||
prior to 60 days after the cessation.
|
||||
|
||||
Moreover, your license from a particular copyright holder is
|
||||
reinstated permanently if the copyright holder notifies you of the
|
||||
violation by some reasonable means, this is the first time you have
|
||||
received notice of violation of this License (for any work) from that
|
||||
copyright holder, and you cure the violation prior to 30 days after
|
||||
your receipt of the notice.
|
||||
|
||||
Termination of your rights under this section does not terminate the
|
||||
licenses of parties who have received copies or rights from you under
|
||||
this License. If your rights have been terminated and not permanently
|
||||
reinstated, you do not qualify to receive new licenses for the same
|
||||
material under section 10.
|
||||
|
||||
9. Acceptance Not Required for Having Copies.
|
||||
|
||||
You are not required to accept this License in order to receive or
|
||||
run a copy of the Program. Ancillary propagation of a covered work
|
||||
occurring solely as a consequence of using peer-to-peer transmission
|
||||
to receive a copy likewise does not require acceptance. However,
|
||||
nothing other than this License grants you permission to propagate or
|
||||
modify any covered work. These actions infringe copyright if you do
|
||||
not accept this License. Therefore, by modifying or propagating a
|
||||
covered work, you indicate your acceptance of this License to do so.
|
||||
|
||||
10. Automatic Licensing of Downstream Recipients.
|
||||
|
||||
Each time you convey a covered work, the recipient automatically
|
||||
receives a license from the original licensors, to run, modify and
|
||||
propagate that work, subject to this License. You are not responsible
|
||||
for enforcing compliance by third parties with this License.
|
||||
|
||||
An "entity transaction" is a transaction transferring control of an
|
||||
organization, or substantially all assets of one, or subdividing an
|
||||
organization, or merging organizations. If propagation of a covered
|
||||
work results from an entity transaction, each party to that
|
||||
transaction who receives a copy of the work also receives whatever
|
||||
licenses to the work the party's predecessor in interest had or could
|
||||
give under the previous paragraph, plus a right to possession of the
|
||||
Corresponding Source of the work from the predecessor in interest, if
|
||||
the predecessor has it or can get it with reasonable efforts.
|
||||
|
||||
You may not impose any further restrictions on the exercise of the
|
||||
rights granted or affirmed under this License. For example, you may
|
||||
not impose a license fee, royalty, or other charge for exercise of
|
||||
rights granted under this License, and you may not initiate litigation
|
||||
(including a cross-claim or counterclaim in a lawsuit) alleging that
|
||||
any patent claim is infringed by making, using, selling, offering for
|
||||
sale, or importing the Program or any portion of it.
|
||||
|
||||
11. Patents.
|
||||
|
||||
A "contributor" is a copyright holder who authorizes use under this
|
||||
License of the Program or a work on which the Program is based. The
|
||||
work thus licensed is called the contributor's "contributor version".
|
||||
|
||||
A contributor's "essential patent claims" are all patent claims
|
||||
owned or controlled by the contributor, whether already acquired or
|
||||
hereafter acquired, that would be infringed by some manner, permitted
|
||||
by this License, of making, using, or selling its contributor version,
|
||||
but do not include claims that would be infringed only as a
|
||||
consequence of further modification of the contributor version. For
|
||||
purposes of this definition, "control" includes the right to grant
|
||||
patent sublicenses in a manner consistent with the requirements of
|
||||
this License.
|
||||
|
||||
Each contributor grants you a non-exclusive, worldwide, royalty-free
|
||||
patent license under the contributor's essential patent claims, to
|
||||
make, use, sell, offer for sale, import and otherwise run, modify and
|
||||
propagate the contents of its contributor version.
|
||||
|
||||
In the following three paragraphs, a "patent license" is any express
|
||||
agreement or commitment, however denominated, not to enforce a patent
|
||||
(such as an express permission to practice a patent or covenant not to
|
||||
sue for patent infringement). To "grant" such a patent license to a
|
||||
party means to make such an agreement or commitment not to enforce a
|
||||
patent against the party.
|
||||
|
||||
If you convey a covered work, knowingly relying on a patent license,
|
||||
and the Corresponding Source of the work is not available for anyone
|
||||
to copy, free of charge and under the terms of this License, through a
|
||||
publicly available network server or other readily accessible means,
|
||||
then you must either (1) cause the Corresponding Source to be so
|
||||
available, or (2) arrange to deprive yourself of the benefit of the
|
||||
patent license for this particular work, or (3) arrange, in a manner
|
||||
consistent with the requirements of this License, to extend the patent
|
||||
license to downstream recipients. "Knowingly relying" means you have
|
||||
actual knowledge that, but for the patent license, your conveying the
|
||||
covered work in a country, or your recipient's use of the covered work
|
||||
in a country, would infringe one or more identifiable patents in that
|
||||
country that you have reason to believe are valid.
|
||||
|
||||
If, pursuant to or in connection with a single transaction or
|
||||
arrangement, you convey, or propagate by procuring conveyance of, a
|
||||
covered work, and grant a patent license to some of the parties
|
||||
receiving the covered work authorizing them to use, propagate, modify
|
||||
or convey a specific copy of the covered work, then the patent license
|
||||
you grant is automatically extended to all recipients of the covered
|
||||
work and works based on it.
|
||||
|
||||
A patent license is "discriminatory" if it does not include within
|
||||
the scope of its coverage, prohibits the exercise of, or is
|
||||
conditioned on the non-exercise of one or more of the rights that are
|
||||
specifically granted under this License. You may not convey a covered
|
||||
work if you are a party to an arrangement with a third party that is
|
||||
in the business of distributing software, under which you make payment
|
||||
to the third party based on the extent of your activity of conveying
|
||||
the work, and under which the third party grants, to any of the
|
||||
parties who would receive the covered work from you, a discriminatory
|
||||
patent license (a) in connection with copies of the covered work
|
||||
conveyed by you (or copies made from those copies), or (b) primarily
|
||||
for and in connection with specific products or compilations that
|
||||
contain the covered work, unless you entered into that arrangement,
|
||||
or that patent license was granted, prior to 28 March 2007.
|
||||
|
||||
Nothing in this License shall be construed as excluding or limiting
|
||||
any implied license or other defenses to infringement that may
|
||||
otherwise be available to you under applicable patent law.
|
||||
|
||||
12. No Surrender of Others' Freedom.
|
||||
|
||||
If conditions are imposed on you (whether by court order, agreement or
|
||||
otherwise) that contradict the conditions of this License, they do not
|
||||
excuse you from the conditions of this License. If you cannot convey a
|
||||
covered work so as to satisfy simultaneously your obligations under this
|
||||
License and any other pertinent obligations, then as a consequence you may
|
||||
not convey it at all. For example, if you agree to terms that obligate you
|
||||
to collect a royalty for further conveying from those to whom you convey
|
||||
the Program, the only way you could satisfy both those terms and this
|
||||
License would be to refrain entirely from conveying the Program.
|
||||
|
||||
13. Use with the GNU Affero General Public License.
|
||||
|
||||
Notwithstanding any other provision of this License, you have
|
||||
permission to link or combine any covered work with a work licensed
|
||||
under version 3 of the GNU Affero General Public License into a single
|
||||
combined work, and to convey the resulting work. The terms of this
|
||||
License will continue to apply to the part which is the covered work,
|
||||
but the special requirements of the GNU Affero General Public License,
|
||||
section 13, concerning interaction through a network will apply to the
|
||||
combination as such.
|
||||
|
||||
14. Revised Versions of this License.
|
||||
|
||||
The Free Software Foundation may publish revised and/or new versions of
|
||||
the GNU General Public License from time to time. Such new versions will
|
||||
be similar in spirit to the present version, but may differ in detail to
|
||||
address new problems or concerns.
|
||||
|
||||
Each version is given a distinguishing version number. If the
|
||||
Program specifies that a certain numbered version of the GNU General
|
||||
Public License "or any later version" applies to it, you have the
|
||||
option of following the terms and conditions either of that numbered
|
||||
version or of any later version published by the Free Software
|
||||
Foundation. If the Program does not specify a version number of the
|
||||
GNU General Public License, you may choose any version ever published
|
||||
by the Free Software Foundation.
|
||||
|
||||
If the Program specifies that a proxy can decide which future
|
||||
versions of the GNU General Public License can be used, that proxy's
|
||||
public statement of acceptance of a version permanently authorizes you
|
||||
to choose that version for the Program.
|
||||
|
||||
Later license versions may give you additional or different
|
||||
permissions. However, no additional obligations are imposed on any
|
||||
author or copyright holder as a result of your choosing to follow a
|
||||
later version.
|
||||
|
||||
15. Disclaimer of Warranty.
|
||||
|
||||
THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
|
||||
APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
|
||||
HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
|
||||
OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
|
||||
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
|
||||
IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
|
||||
ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
|
||||
|
||||
16. Limitation of Liability.
|
||||
|
||||
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
|
||||
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
|
||||
THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
|
||||
GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
|
||||
USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
|
||||
DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
|
||||
PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
|
||||
EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
|
||||
SUCH DAMAGES.
|
||||
|
||||
17. Interpretation of Sections 15 and 16.
|
||||
|
||||
If the disclaimer of warranty and limitation of liability provided
|
||||
above cannot be given local legal effect according to their terms,
|
||||
reviewing courts shall apply local law that most closely approximates
|
||||
an absolute waiver of all civil liability in connection with the
|
||||
Program, unless a warranty or assumption of liability accompanies a
|
||||
copy of the Program in return for a fee.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
How to Apply These Terms to Your New Programs
|
||||
|
||||
If you develop a new program, and you want it to be of the greatest
|
||||
possible use to the public, the best way to achieve this is to make it
|
||||
free software which everyone can redistribute and change under these terms.
|
||||
|
||||
To do so, attach the following notices to the program. It is safest
|
||||
to attach them to the start of each source file to most effectively
|
||||
state the exclusion of warranty; and each file should have at least
|
||||
the "copyright" line and a pointer to where the full notice is found.
|
||||
|
||||
<one line to give the program's name and a brief idea of what it does.>
|
||||
Copyright (C) <year> <name of author>
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
Also add information on how to contact you by electronic and paper mail.
|
||||
|
||||
If the program does terminal interaction, make it output a short
|
||||
notice like this when it starts in an interactive mode:
|
||||
|
||||
<program> Copyright (C) <year> <name of author>
|
||||
This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
|
||||
This is free software, and you are welcome to redistribute it
|
||||
under certain conditions; type `show c' for details.
|
||||
|
||||
The hypothetical commands `show w' and `show c' should show the appropriate
|
||||
parts of the General Public License. Of course, your program's commands
|
||||
might be different; for a GUI interface, you would use an "about box".
|
||||
|
||||
You should also get your employer (if you work as a programmer) or school,
|
||||
if any, to sign a "copyright disclaimer" for the program, if necessary.
|
||||
For more information on this, and how to apply and follow the GNU GPL, see
|
||||
<https://www.gnu.org/licenses/>.
|
||||
|
||||
The GNU General Public License does not permit incorporating your program
|
||||
into proprietary programs. If your program is a subroutine library, you
|
||||
may consider it more useful to permit linking proprietary applications with
|
||||
the library. If this is what you want to do, use the GNU Lesser General
|
||||
Public License instead of this License. But first, please read
|
||||
<https://www.gnu.org/licenses/why-not-lgpl.html>.
|
||||
@@ -0,0 +1,42 @@
|
||||
from pathlib import Path
|
||||
import importlib.util
|
||||
import os
|
||||
import sys
|
||||
|
||||
_current_dir = Path(__file__).parent
|
||||
if str(_current_dir) not in sys.path:
|
||||
sys.path.insert(0, str(_current_dir))
|
||||
|
||||
NODE_CLASS_MAPPINGS = {}
|
||||
NODE_DISPLAY_NAME_MAPPINGS = {}
|
||||
WEB_DIRECTORY = "./web"
|
||||
|
||||
|
||||
def _load_module(path: Path):
|
||||
spec = importlib.util.spec_from_file_location(path.stem, path)
|
||||
if not spec or not spec.loader:
|
||||
return None
|
||||
module = importlib.util.module_from_spec(spec)
|
||||
spec.loader.exec_module(module)
|
||||
return module
|
||||
|
||||
|
||||
def _discover_nodes():
|
||||
for py in _current_dir.glob("*.py"):
|
||||
if py.name == "__init__.py":
|
||||
continue
|
||||
try:
|
||||
module = _load_module(py)
|
||||
if not module:
|
||||
continue
|
||||
if hasattr(module, "NODE_CLASS_MAPPINGS"):
|
||||
NODE_CLASS_MAPPINGS.update(module.NODE_CLASS_MAPPINGS)
|
||||
if hasattr(module, "NODE_DISPLAY_NAME_MAPPINGS"):
|
||||
NODE_DISPLAY_NAME_MAPPINGS.update(module.NODE_DISPLAY_NAME_MAPPINGS)
|
||||
except Exception as e:
|
||||
print(f"[Qwen3TTS] Failed to load {py.name}: {e}")
|
||||
|
||||
|
||||
_discover_nodes()
|
||||
|
||||
__all__ = ["NODE_CLASS_MAPPINGS", "NODE_DISPLAY_NAME_MAPPINGS", "WEB_DIRECTORY"]
|
||||
@@ -0,0 +1,674 @@
|
||||
GNU GENERAL PUBLIC LICENSE
|
||||
Version 3, 29 June 2007
|
||||
|
||||
Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
|
||||
Everyone is permitted to copy and distribute verbatim copies
|
||||
of this license document, but changing it is not allowed.
|
||||
|
||||
Preamble
|
||||
|
||||
The GNU General Public License is a free, copyleft license for
|
||||
software and other kinds of works.
|
||||
|
||||
The licenses for most software and other practical works are designed
|
||||
to take away your freedom to share and change the works. By contrast,
|
||||
the GNU General Public License is intended to guarantee your freedom to
|
||||
share and change all versions of a program--to make sure it remains free
|
||||
software for all its users. We, the Free Software Foundation, use the
|
||||
GNU General Public License for most of our software; it applies also to
|
||||
any other work released this way by its authors. You can apply it to
|
||||
your programs, too.
|
||||
|
||||
When we speak of free software, we are referring to freedom, not
|
||||
price. Our General Public Licenses are designed to make sure that you
|
||||
have the freedom to distribute copies of free software (and charge for
|
||||
them if you wish), that you receive source code or can get it if you
|
||||
want it, that you can change the software or use pieces of it in new
|
||||
free programs, and that you know you can do these things.
|
||||
|
||||
To protect your rights, we need to prevent others from denying you
|
||||
these rights or asking you to surrender the rights. Therefore, you have
|
||||
certain responsibilities if you distribute copies of the software, or if
|
||||
you modify it: responsibilities to respect the freedom of others.
|
||||
|
||||
For example, if you distribute copies of such a program, whether
|
||||
gratis or for a fee, you must pass on to the recipients the same
|
||||
freedoms that you received. You must make sure that they, too, receive
|
||||
or can get the source code. And you must show them these terms so they
|
||||
know their rights.
|
||||
|
||||
Developers that use the GNU GPL protect your rights with two steps:
|
||||
(1) assert copyright on the software, and (2) offer you this License
|
||||
giving you legal permission to copy, distribute and/or modify it.
|
||||
|
||||
For the developers' and authors' protection, the GPL clearly explains
|
||||
that there is no warranty for this free software. For both users' and
|
||||
authors' sake, the GPL requires that modified versions be marked as
|
||||
changed, so that their problems will not be attributed erroneously to
|
||||
authors of previous versions.
|
||||
|
||||
Some devices are designed to deny users access to install or run
|
||||
modified versions of the software inside them, although the manufacturer
|
||||
can do so. This is fundamentally incompatible with the aim of
|
||||
protecting users' freedom to change the software. The systematic
|
||||
pattern of such abuse occurs in the area of products for individuals to
|
||||
use, which is precisely where it is most unacceptable. Therefore, we
|
||||
have designed this version of the GPL to prohibit the practice for those
|
||||
products. If such problems arise substantially in other domains, we
|
||||
stand ready to extend this provision to those domains in future versions
|
||||
of the GPL, as needed to protect the freedom of users.
|
||||
|
||||
Finally, every program is threatened constantly by software patents.
|
||||
States should not allow patents to restrict development and use of
|
||||
software on general-purpose computers, but in those that do, we wish to
|
||||
avoid the special danger that patents applied to a free program could
|
||||
make it effectively proprietary. To prevent this, the GPL assures that
|
||||
patents cannot be used to render the program non-free.
|
||||
|
||||
The precise terms and conditions for copying, distribution and
|
||||
modification follow.
|
||||
|
||||
TERMS AND CONDITIONS
|
||||
|
||||
0. Definitions.
|
||||
|
||||
"This License" refers to version 3 of the GNU General Public License.
|
||||
|
||||
"Copyright" also means copyright-like laws that apply to other kinds of
|
||||
works, such as semiconductor masks.
|
||||
|
||||
"The Program" refers to any copyrightable work licensed under this
|
||||
License. Each licensee is addressed as "you". "Licensees" and
|
||||
"recipients" may be individuals or organizations.
|
||||
|
||||
To "modify" a work means to copy from or adapt all or part of the work
|
||||
in a fashion requiring copyright permission, other than the making of an
|
||||
exact copy. The resulting work is called a "modified version" of the
|
||||
earlier work or a work "based on" the earlier work.
|
||||
|
||||
A "covered work" means either the unmodified Program or a work based
|
||||
on the Program.
|
||||
|
||||
To "propagate" a work means to do anything with it that, without
|
||||
permission, would make you directly or secondarily liable for
|
||||
infringement under applicable copyright law, except executing it on a
|
||||
computer or modifying a private copy. Propagation includes copying,
|
||||
distribution (with or without modification), making available to the
|
||||
public, and in some countries other activities as well.
|
||||
|
||||
To "convey" a work means any kind of propagation that enables other
|
||||
parties to make or receive copies. Mere interaction with a user through
|
||||
a computer network, with no transfer of a copy, is not conveying.
|
||||
|
||||
An interactive user interface displays "Appropriate Legal Notices"
|
||||
to the extent that it includes a convenient and prominently visible
|
||||
feature that (1) displays an appropriate copyright notice, and (2)
|
||||
tells the user that there is no warranty for the work (except to the
|
||||
extent that warranties are provided), that licensees may convey the
|
||||
work under this License, and how to view a copy of this License. If
|
||||
the interface presents a list of user commands or options, such as a
|
||||
menu, a prominent item in the list meets this criterion.
|
||||
|
||||
1. Source Code.
|
||||
|
||||
The "source code" for a work means the preferred form of the work
|
||||
for making modifications to it. "Object code" means any non-source
|
||||
form of a work.
|
||||
|
||||
A "Standard Interface" means an interface that either is an official
|
||||
standard defined by a recognized standards body, or, in the case of
|
||||
interfaces specified for a particular programming language, one that
|
||||
is widely used among developers working in that language.
|
||||
|
||||
The "System Libraries" of an executable work include anything, other
|
||||
than the work as a whole, that (a) is included in the normal form of
|
||||
packaging a Major Component, but which is not part of that Major
|
||||
Component, and (b) serves only to enable use of the work with that
|
||||
Major Component, or to implement a Standard Interface for which an
|
||||
implementation is available to the public in source code form. A
|
||||
"Major Component", in this context, means a major essential component
|
||||
(kernel, window system, and so on) of the specific operating system
|
||||
(if any) on which the executable work runs, or a compiler used to
|
||||
produce the work, or an object code interpreter used to run it.
|
||||
|
||||
The "Corresponding Source" for a work in object code form means all
|
||||
the source code needed to generate, install, and (for an executable
|
||||
work) run the object code and to modify the work, including scripts to
|
||||
control those activities. However, it does not include the work's
|
||||
System Libraries, or general-purpose tools or generally available free
|
||||
programs which are used unmodified in performing those activities but
|
||||
which are not part of the work. For example, Corresponding Source
|
||||
includes interface definition files associated with source files for
|
||||
the work, and the source code for shared libraries and dynamically
|
||||
linked subprograms that the work is specifically designed to require,
|
||||
such as by intimate data communication or control flow between those
|
||||
subprograms and other parts of the work.
|
||||
|
||||
The Corresponding Source need not include anything that users
|
||||
can regenerate automatically from other parts of the Corresponding
|
||||
Source.
|
||||
|
||||
The Corresponding Source for a work in source code form is that
|
||||
same work.
|
||||
|
||||
2. Basic Permissions.
|
||||
|
||||
All rights granted under this License are granted for the term of
|
||||
copyright on the Program, and are irrevocable provided the stated
|
||||
conditions are met. This License explicitly affirms your unlimited
|
||||
permission to run the unmodified Program. The output from running a
|
||||
covered work is covered by this License only if the output, given its
|
||||
content, constitutes a covered work. This License acknowledges your
|
||||
rights of fair use or other equivalent, as provided by copyright law.
|
||||
|
||||
You may make, run and propagate covered works that you do not
|
||||
convey, without conditions so long as your license otherwise remains
|
||||
in force. You may convey covered works to others for the sole purpose
|
||||
of having them make modifications exclusively for you, or provide you
|
||||
with facilities for running those works, provided that you comply with
|
||||
the terms of this License in conveying all material for which you do
|
||||
not control copyright. Those thus making or running the covered works
|
||||
for you must do so exclusively on your behalf, under your direction
|
||||
and control, on terms that prohibit them from making any copies of
|
||||
your copyrighted material outside their relationship with you.
|
||||
|
||||
Conveying under any other circumstances is permitted solely under
|
||||
the conditions stated below. Sublicensing is not allowed; section 10
|
||||
makes it unnecessary.
|
||||
|
||||
3. Protecting Users' Legal Rights From Anti-Circumvention Law.
|
||||
|
||||
No covered work shall be deemed part of an effective technological
|
||||
measure under any applicable law fulfilling obligations under article
|
||||
11 of the WIPO copyright treaty adopted on 20 December 1996, or
|
||||
similar laws prohibiting or restricting circumvention of such
|
||||
measures.
|
||||
|
||||
When you convey a covered work, you waive any legal power to forbid
|
||||
circumvention of technological measures to the extent such circumvention
|
||||
is effected by exercising rights under this License with respect to
|
||||
the covered work, and you disclaim any intention to limit operation or
|
||||
modification of the work as a means of enforcing, against the work's
|
||||
users, your or third parties' legal rights to forbid circumvention of
|
||||
technological measures.
|
||||
|
||||
4. Conveying Verbatim Copies.
|
||||
|
||||
You may convey verbatim copies of the Program's source code as you
|
||||
receive it, in any medium, provided that you conspicuously and
|
||||
appropriately publish on each copy an appropriate copyright notice;
|
||||
keep intact all notices stating that this License and any
|
||||
non-permissive terms added in accord with section 7 apply to the code;
|
||||
keep intact all notices of the absence of any warranty; and give all
|
||||
recipients a copy of this License along with the Program.
|
||||
|
||||
You may charge any price or no price for each copy that you convey,
|
||||
and you may offer support or warranty protection for a fee.
|
||||
|
||||
5. Conveying Modified Source Versions.
|
||||
|
||||
You may convey a work based on the Program, or the modifications to
|
||||
produce it from the Program, in the form of source code under the
|
||||
terms of section 4, provided that you also meet all of these conditions:
|
||||
|
||||
a) The work must carry prominent notices stating that you modified
|
||||
it, and giving a relevant date.
|
||||
|
||||
b) The work must carry prominent notices stating that it is
|
||||
released under this License and any conditions added under section
|
||||
7. This requirement modifies the requirement in section 4 to
|
||||
"keep intact all notices".
|
||||
|
||||
c) You must license the entire work, as a whole, under this
|
||||
License to anyone who comes into possession of a copy. This
|
||||
License will therefore apply, along with any applicable section 7
|
||||
additional terms, to the whole of the work, and all its parts,
|
||||
regardless of how they are packaged. This License gives no
|
||||
permission to license the work in any other way, but it does not
|
||||
invalidate such permission if you have separately received it.
|
||||
|
||||
d) If the work has interactive user interfaces, each must display
|
||||
Appropriate Legal Notices; however, if the Program has interactive
|
||||
interfaces that do not display Appropriate Legal Notices, your
|
||||
work need not make them do so.
|
||||
|
||||
A compilation of a covered work with other separate and independent
|
||||
works, which are not by their nature extensions of the covered work,
|
||||
and which are not combined with it such as to form a larger program,
|
||||
in or on a volume of a storage or distribution medium, is called an
|
||||
"aggregate" if the compilation and its resulting copyright are not
|
||||
used to limit the access or legal rights of the compilation's users
|
||||
beyond what the individual works permit. Inclusion of a covered work
|
||||
in an aggregate does not cause this License to apply to the other
|
||||
parts of the aggregate.
|
||||
|
||||
6. Conveying Non-Source Forms.
|
||||
|
||||
You may convey a covered work in object code form under the terms
|
||||
of sections 4 and 5, provided that you also convey the
|
||||
machine-readable Corresponding Source under the terms of this License,
|
||||
in one of these ways:
|
||||
|
||||
a) Convey the object code in, or embodied in, a physical product
|
||||
(including a physical distribution medium), accompanied by the
|
||||
Corresponding Source fixed on a durable physical medium
|
||||
customarily used for software interchange.
|
||||
|
||||
b) Convey the object code in, or embodied in, a physical product
|
||||
(including a physical distribution medium), accompanied by a
|
||||
written offer, valid for at least three years and valid for as
|
||||
long as you offer spare parts or customer support for that product
|
||||
model, to give anyone who possesses the object code either (1) a
|
||||
copy of the Corresponding Source for all the software in the
|
||||
product that is covered by this License, on a durable physical
|
||||
medium customarily used for software interchange, for a price no
|
||||
more than your reasonable cost of physically performing this
|
||||
conveying of source, or (2) access to copy the
|
||||
Corresponding Source from a network server at no charge.
|
||||
|
||||
c) Convey individual copies of the object code with a copy of the
|
||||
written offer to provide the Corresponding Source. This
|
||||
alternative is allowed only occasionally and noncommercially, and
|
||||
only if you received the object code with such an offer, in accord
|
||||
with subsection 6b.
|
||||
|
||||
d) Convey the object code by offering access from a designated
|
||||
place (gratis or for a charge), and offer equivalent access to the
|
||||
Corresponding Source in the same way through the same place at no
|
||||
further charge. You need not require recipients to copy the
|
||||
Corresponding Source along with the object code. If the place to
|
||||
copy the object code is a network server, the Corresponding Source
|
||||
may be on a different server (operated by you or a third party)
|
||||
that supports equivalent copying facilities, provided you maintain
|
||||
clear directions next to the object code saying where to find the
|
||||
Corresponding Source. Regardless of what server hosts the
|
||||
Corresponding Source, you remain obligated to ensure that it is
|
||||
available for as long as needed to satisfy these requirements.
|
||||
|
||||
e) Convey the object code using peer-to-peer transmission, provided
|
||||
you inform other peers where the object code and Corresponding
|
||||
Source of the work are being offered to the general public at no
|
||||
charge under subsection 6d.
|
||||
|
||||
A separable portion of the object code, whose source code is excluded
|
||||
from the Corresponding Source as a System Library, need not be
|
||||
included in conveying the object code work.
|
||||
|
||||
A "User Product" is either (1) a "consumer product", which means any
|
||||
tangible personal property which is normally used for personal, family,
|
||||
or household purposes, or (2) anything designed or sold for incorporation
|
||||
into a dwelling. In determining whether a product is a consumer product,
|
||||
doubtful cases shall be resolved in favor of coverage. For a particular
|
||||
product received by a particular user, "normally used" refers to a
|
||||
typical or common use of that class of product, regardless of the status
|
||||
of the particular user or of the way in which the particular user
|
||||
actually uses, or expects or is expected to use, the product. A product
|
||||
is a consumer product regardless of whether the product has substantial
|
||||
commercial, industrial or non-consumer uses, unless such uses represent
|
||||
the only significant mode of use of the product.
|
||||
|
||||
"Installation Information" for a User Product means any methods,
|
||||
procedures, authorization keys, or other information required to install
|
||||
and execute modified versions of a covered work in that User Product from
|
||||
a modified version of its Corresponding Source. The information must
|
||||
suffice to ensure that the continued functioning of the modified object
|
||||
code is in no case prevented or interfered with solely because
|
||||
modification has been made.
|
||||
|
||||
If you convey an object code work under this section in, or with, or
|
||||
specifically for use in, a User Product, and the conveying occurs as
|
||||
part of a transaction in which the right of possession and use of the
|
||||
User Product is transferred to the recipient in perpetuity or for a
|
||||
fixed term (regardless of how the transaction is characterized), the
|
||||
Corresponding Source conveyed under this section must be accompanied
|
||||
by the Installation Information. But this requirement does not apply
|
||||
if neither you nor any third party retains the ability to install
|
||||
modified object code on the User Product (for example, the work has
|
||||
been installed in ROM).
|
||||
|
||||
The requirement to provide Installation Information does not include a
|
||||
requirement to continue to provide support service, warranty, or updates
|
||||
for a work that has been modified or installed by the recipient, or for
|
||||
the User Product in which it has been modified or installed. Access to a
|
||||
network may be denied when the modification itself materially and
|
||||
adversely affects the operation of the network or violates the rules and
|
||||
protocols for communication across the network.
|
||||
|
||||
Corresponding Source conveyed, and Installation Information provided,
|
||||
in accord with this section must be in a format that is publicly
|
||||
documented (and with an implementation available to the public in
|
||||
source code form), and must require no special password or key for
|
||||
unpacking, reading or copying.
|
||||
|
||||
7. Additional Terms.
|
||||
|
||||
"Additional permissions" are terms that supplement the terms of this
|
||||
License by making exceptions from one or more of its conditions.
|
||||
Additional permissions that are applicable to the entire Program shall
|
||||
be treated as though they were included in this License, to the extent
|
||||
that they are valid under applicable law. If additional permissions
|
||||
apply only to part of the Program, that part may be used separately
|
||||
under those permissions, but the entire Program remains governed by
|
||||
this License without regard to the additional permissions.
|
||||
|
||||
When you convey a copy of a covered work, you may at your option
|
||||
remove any additional permissions from that copy, or from any part of
|
||||
it. (Additional permissions may be written to require their own
|
||||
removal in certain cases when you modify the work.) You may place
|
||||
additional permissions on material, added by you to a covered work,
|
||||
for which you have or can give appropriate copyright permission.
|
||||
|
||||
Notwithstanding any other provision of this License, for material you
|
||||
add to a covered work, you may (if authorized by the copyright holders of
|
||||
that material) supplement the terms of this License with terms:
|
||||
|
||||
a) Disclaiming warranty or limiting liability differently from the
|
||||
terms of sections 15 and 16 of this License; or
|
||||
|
||||
b) Requiring preservation of specified reasonable legal notices or
|
||||
author attributions in that material or in the Appropriate Legal
|
||||
Notices displayed by works containing it; or
|
||||
|
||||
c) Prohibiting misrepresentation of the origin of that material, or
|
||||
requiring that modified versions of such material be marked in
|
||||
reasonable ways as different from the original version; or
|
||||
|
||||
d) Limiting the use for publicity purposes of names of licensors or
|
||||
authors of the material; or
|
||||
|
||||
e) Declining to grant rights under trademark law for use of some
|
||||
trade names, trademarks, or service marks; or
|
||||
|
||||
f) Requiring indemnification of licensors and authors of that
|
||||
material by anyone who conveys the material (or modified versions of
|
||||
it) with contractual assumptions of liability to the recipient, for
|
||||
any liability that these contractual assumptions directly impose on
|
||||
those licensors and authors.
|
||||
|
||||
All other non-permissive additional terms are considered "further
|
||||
restrictions" within the meaning of section 10. If the Program as you
|
||||
received it, or any part of it, contains a notice stating that it is
|
||||
governed by this License along with a term that is a further
|
||||
restriction, you may remove that term. If a license document contains
|
||||
a further restriction but permits relicensing or conveying under this
|
||||
License, you may add to a covered work material governed by the terms
|
||||
of that license document, provided that the further restriction does
|
||||
not survive such relicensing or conveying.
|
||||
|
||||
If you add terms to a covered work in accord with this section, you
|
||||
must place, in the relevant source files, a statement of the
|
||||
additional terms that apply to those files, or a notice indicating
|
||||
where to find the applicable terms.
|
||||
|
||||
Additional terms, permissive or non-permissive, may be stated in the
|
||||
form of a separately written license, or stated as exceptions;
|
||||
the above requirements apply either way.
|
||||
|
||||
8. Termination.
|
||||
|
||||
You may not propagate or modify a covered work except as expressly
|
||||
provided under this License. Any attempt otherwise to propagate or
|
||||
modify it is void, and will automatically terminate your rights under
|
||||
this License (including any patent licenses granted under the third
|
||||
paragraph of section 11).
|
||||
|
||||
However, if you cease all violation of this License, then your
|
||||
license from a particular copyright holder is reinstated (a)
|
||||
provisionally, unless and until the copyright holder explicitly and
|
||||
finally terminates your license, and (b) permanently, if the copyright
|
||||
holder fails to notify you of the violation by some reasonable means
|
||||
prior to 60 days after the cessation.
|
||||
|
||||
Moreover, your license from a particular copyright holder is
|
||||
reinstated permanently if the copyright holder notifies you of the
|
||||
violation by some reasonable means, this is the first time you have
|
||||
received notice of violation of this License (for any work) from that
|
||||
copyright holder, and you cure the violation prior to 30 days after
|
||||
your receipt of the notice.
|
||||
|
||||
Termination of your rights under this section does not terminate the
|
||||
licenses of parties who have received copies or rights from you under
|
||||
this License. If your rights have been terminated and not permanently
|
||||
reinstated, you do not qualify to receive new licenses for the same
|
||||
material under section 10.
|
||||
|
||||
9. Acceptance Not Required for Having Copies.
|
||||
|
||||
You are not required to accept this License in order to receive or
|
||||
run a copy of the Program. Ancillary propagation of a covered work
|
||||
occurring solely as a consequence of using peer-to-peer transmission
|
||||
to receive a copy likewise does not require acceptance. However,
|
||||
nothing other than this License grants you permission to propagate or
|
||||
modify any covered work. These actions infringe copyright if you do
|
||||
not accept this License. Therefore, by modifying or propagating a
|
||||
covered work, you indicate your acceptance of this License to do so.
|
||||
|
||||
10. Automatic Licensing of Downstream Recipients.
|
||||
|
||||
Each time you convey a covered work, the recipient automatically
|
||||
receives a license from the original licensors, to run, modify and
|
||||
propagate that work, subject to this License. You are not responsible
|
||||
for enforcing compliance by third parties with this License.
|
||||
|
||||
An "entity transaction" is a transaction transferring control of an
|
||||
organization, or substantially all assets of one, or subdividing an
|
||||
organization, or merging organizations. If propagation of a covered
|
||||
work results from an entity transaction, each party to that
|
||||
transaction who receives a copy of the work also receives whatever
|
||||
licenses to the work the party's predecessor in interest had or could
|
||||
give under the previous paragraph, plus a right to possession of the
|
||||
Corresponding Source of the work from the predecessor in interest, if
|
||||
the predecessor has it or can get it with reasonable efforts.
|
||||
|
||||
You may not impose any further restrictions on the exercise of the
|
||||
rights granted or affirmed under this License. For example, you may
|
||||
not impose a license fee, royalty, or other charge for exercise of
|
||||
rights granted under this License, and you may not initiate litigation
|
||||
(including a cross-claim or counterclaim in a lawsuit) alleging that
|
||||
any patent claim is infringed by making, using, selling, offering for
|
||||
sale, or importing the Program or any portion of it.
|
||||
|
||||
11. Patents.
|
||||
|
||||
A "contributor" is a copyright holder who authorizes use under this
|
||||
License of the Program or a work on which the Program is based. The
|
||||
work thus licensed is called the contributor's "contributor version".
|
||||
|
||||
A contributor's "essential patent claims" are all patent claims
|
||||
owned or controlled by the contributor, whether already acquired or
|
||||
hereafter acquired, that would be infringed by some manner, permitted
|
||||
by this License, of making, using, or selling its contributor version,
|
||||
but do not include claims that would be infringed only as a
|
||||
consequence of further modification of the contributor version. For
|
||||
purposes of this definition, "control" includes the right to grant
|
||||
patent sublicenses in a manner consistent with the requirements of
|
||||
this License.
|
||||
|
||||
Each contributor grants you a non-exclusive, worldwide, royalty-free
|
||||
patent license under the contributor's essential patent claims, to
|
||||
make, use, sell, offer for sale, import and otherwise run, modify and
|
||||
propagate the contents of its contributor version.
|
||||
|
||||
In the following three paragraphs, a "patent license" is any express
|
||||
agreement or commitment, however denominated, not to enforce a patent
|
||||
(such as an express permission to practice a patent or covenant not to
|
||||
sue for patent infringement). To "grant" such a patent license to a
|
||||
party means to make such an agreement or commitment not to enforce a
|
||||
patent against the party.
|
||||
|
||||
If you convey a covered work, knowingly relying on a patent license,
|
||||
and the Corresponding Source of the work is not available for anyone
|
||||
to copy, free of charge and under the terms of this License, through a
|
||||
publicly available network server or other readily accessible means,
|
||||
then you must either (1) cause the Corresponding Source to be so
|
||||
available, or (2) arrange to deprive yourself of the benefit of the
|
||||
patent license for this particular work, or (3) arrange, in a manner
|
||||
consistent with the requirements of this License, to extend the patent
|
||||
license to downstream recipients. "Knowingly relying" means you have
|
||||
actual knowledge that, but for the patent license, your conveying the
|
||||
covered work in a country, or your recipient's use of the covered work
|
||||
in a country, would infringe one or more identifiable patents in that
|
||||
country that you have reason to believe are valid.
|
||||
|
||||
If, pursuant to or in connection with a single transaction or
|
||||
arrangement, you convey, or propagate by procuring conveyance of, a
|
||||
covered work, and grant a patent license to some of the parties
|
||||
receiving the covered work authorizing them to use, propagate, modify
|
||||
or convey a specific copy of the covered work, then the patent license
|
||||
you grant is automatically extended to all recipients of the covered
|
||||
work and works based on it.
|
||||
|
||||
A patent license is "discriminatory" if it does not include within
|
||||
the scope of its coverage, prohibits the exercise of, or is
|
||||
conditioned on the non-exercise of one or more of the rights that are
|
||||
specifically granted under this License. You may not convey a covered
|
||||
work if you are a party to an arrangement with a third party that is
|
||||
in the business of distributing software, under which you make payment
|
||||
to the third party based on the extent of your activity of conveying
|
||||
the work, and under which the third party grants, to any of the
|
||||
parties who would receive the covered work from you, a discriminatory
|
||||
patent license (a) in connection with copies of the covered work
|
||||
conveyed by you (or copies made from those copies), or (b) primarily
|
||||
for and in connection with specific products or compilations that
|
||||
contain the covered work, unless you entered into that arrangement,
|
||||
or that patent license was granted, prior to 28 March 2007.
|
||||
|
||||
Nothing in this License shall be construed as excluding or limiting
|
||||
any implied license or other defenses to infringement that may
|
||||
otherwise be available to you under applicable patent law.
|
||||
|
||||
12. No Surrender of Others' Freedom.
|
||||
|
||||
If conditions are imposed on you (whether by court order, agreement or
|
||||
otherwise) that contradict the conditions of this License, they do not
|
||||
excuse you from the conditions of this License. If you cannot convey a
|
||||
covered work so as to satisfy simultaneously your obligations under this
|
||||
License and any other pertinent obligations, then as a consequence you may
|
||||
not convey it at all. For example, if you agree to terms that obligate you
|
||||
to collect a royalty for further conveying from those to whom you convey
|
||||
the Program, the only way you could satisfy both those terms and this
|
||||
License would be to refrain entirely from conveying the Program.
|
||||
|
||||
13. Use with the GNU Affero General Public License.
|
||||
|
||||
Notwithstanding any other provision of this License, you have
|
||||
permission to link or combine any covered work with a work licensed
|
||||
under version 3 of the GNU Affero General Public License into a single
|
||||
combined work, and to convey the resulting work. The terms of this
|
||||
License will continue to apply to the part which is the covered work,
|
||||
but the special requirements of the GNU Affero General Public License,
|
||||
section 13, concerning interaction through a network will apply to the
|
||||
combination as such.
|
||||
|
||||
14. Revised Versions of this License.
|
||||
|
||||
The Free Software Foundation may publish revised and/or new versions of
|
||||
the GNU General Public License from time to time. Such new versions will
|
||||
be similar in spirit to the present version, but may differ in detail to
|
||||
address new problems or concerns.
|
||||
|
||||
Each version is given a distinguishing version number. If the
|
||||
Program specifies that a certain numbered version of the GNU General
|
||||
Public License "or any later version" applies to it, you have the
|
||||
option of following the terms and conditions either of that numbered
|
||||
version or of any later version published by the Free Software
|
||||
Foundation. If the Program does not specify a version number of the
|
||||
GNU General Public License, you may choose any version ever published
|
||||
by the Free Software Foundation.
|
||||
|
||||
If the Program specifies that a proxy can decide which future
|
||||
versions of the GNU General Public License can be used, that proxy's
|
||||
public statement of acceptance of a version permanently authorizes you
|
||||
to choose that version for the Program.
|
||||
|
||||
Later license versions may give you additional or different
|
||||
permissions. However, no additional obligations are imposed on any
|
||||
author or copyright holder as a result of your choosing to follow a
|
||||
later version.
|
||||
|
||||
15. Disclaimer of Warranty.
|
||||
|
||||
THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
|
||||
APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
|
||||
HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
|
||||
OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
|
||||
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
|
||||
IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
|
||||
ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
|
||||
|
||||
16. Limitation of Liability.
|
||||
|
||||
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
|
||||
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
|
||||
THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
|
||||
GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
|
||||
USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
|
||||
DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
|
||||
PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
|
||||
EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
|
||||
SUCH DAMAGES.
|
||||
|
||||
17. Interpretation of Sections 15 and 16.
|
||||
|
||||
If the disclaimer of warranty and limitation of liability provided
|
||||
above cannot be given local legal effect according to their terms,
|
||||
reviewing courts shall apply local law that most closely approximates
|
||||
an absolute waiver of all civil liability in connection with the
|
||||
Program, unless a warranty or assumption of liability accompanies a
|
||||
copy of the Program in return for a fee.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
How to Apply These Terms to Your New Programs
|
||||
|
||||
If you develop a new program, and you want it to be of the greatest
|
||||
possible use to the public, the best way to achieve this is to make it
|
||||
free software which everyone can redistribute and change under these terms.
|
||||
|
||||
To do so, attach the following notices to the program. It is safest
|
||||
to attach them to the start of each source file to most effectively
|
||||
state the exclusion of warranty; and each file should have at least
|
||||
the "copyright" line and a pointer to where the full notice is found.
|
||||
|
||||
<one line to give the program's name and a brief idea of what it does.>
|
||||
Copyright (C) <year> <name of author>
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
Also add information on how to contact you by electronic and paper mail.
|
||||
|
||||
If the program does terminal interaction, make it output a short
|
||||
notice like this when it starts in an interactive mode:
|
||||
|
||||
<program> Copyright (C) <year> <name of author>
|
||||
This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
|
||||
This is free software, and you are welcome to redistribute it
|
||||
under certain conditions; type `show c' for details.
|
||||
|
||||
The hypothetical commands `show w' and `show c' should show the appropriate
|
||||
parts of the General Public License. Of course, your program's commands
|
||||
might be different; for a GUI interface, you would use an "about box".
|
||||
|
||||
You should also get your employer (if you work as a programmer) or school,
|
||||
if any, to sign a "copyright disclaimer" for the program, if necessary.
|
||||
For more information on this, and how to apply and follow the GNU GPL, see
|
||||
<https://www.gnu.org/licenses/>.
|
||||
|
||||
The GNU General Public License does not permit incorporating your program
|
||||
into proprietary programs. If your program is a subroutine library, you
|
||||
may consider it more useful to permit linking proprietary applications with
|
||||
the library. If this is what you want to do, use the GNU Lesser General
|
||||
Public License instead of this License. But first, please read
|
||||
<https://www.gnu.org/licenses/why-not-lgpl.html>.
|
||||
@@ -0,0 +1,693 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import av
|
||||
import torchaudio
|
||||
import torch
|
||||
import comfy.model_management
|
||||
import folder_paths
|
||||
import os
|
||||
import hashlib
|
||||
import node_helpers
|
||||
import logging
|
||||
from typing_extensions import override
|
||||
from comfy_api.latest import ComfyExtension, IO, UI
|
||||
|
||||
class EmptyLatentAudio(IO.ComfyNode):
|
||||
@classmethod
|
||||
def define_schema(cls):
|
||||
return IO.Schema(
|
||||
node_id="EmptyLatentAudio",
|
||||
display_name="Empty Latent Audio",
|
||||
category="latent/audio",
|
||||
inputs=[
|
||||
IO.Float.Input("seconds", default=47.6, min=1.0, max=1000.0, step=0.1),
|
||||
IO.Int.Input(
|
||||
"batch_size", default=1, min=1, max=4096, tooltip="The number of latent images in the batch."
|
||||
),
|
||||
],
|
||||
outputs=[IO.Latent.Output()],
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def execute(cls, seconds, batch_size) -> IO.NodeOutput:
|
||||
length = round((seconds * 44100 / 2048) / 2) * 2
|
||||
latent = torch.zeros([batch_size, 64, length], device=comfy.model_management.intermediate_device())
|
||||
return IO.NodeOutput({"samples":latent, "type": "audio"})
|
||||
|
||||
generate = execute # TODO: remove
|
||||
|
||||
|
||||
class ConditioningStableAudio(IO.ComfyNode):
|
||||
@classmethod
|
||||
def define_schema(cls):
|
||||
return IO.Schema(
|
||||
node_id="ConditioningStableAudio",
|
||||
category="conditioning",
|
||||
inputs=[
|
||||
IO.Conditioning.Input("positive"),
|
||||
IO.Conditioning.Input("negative"),
|
||||
IO.Float.Input("seconds_start", default=0.0, min=0.0, max=1000.0, step=0.1),
|
||||
IO.Float.Input("seconds_total", default=47.0, min=0.0, max=1000.0, step=0.1),
|
||||
],
|
||||
outputs=[
|
||||
IO.Conditioning.Output(display_name="positive"),
|
||||
IO.Conditioning.Output(display_name="negative"),
|
||||
],
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def execute(cls, positive, negative, seconds_start, seconds_total) -> IO.NodeOutput:
|
||||
positive = node_helpers.conditioning_set_values(positive, {"seconds_start": seconds_start, "seconds_total": seconds_total})
|
||||
negative = node_helpers.conditioning_set_values(negative, {"seconds_start": seconds_start, "seconds_total": seconds_total})
|
||||
return IO.NodeOutput(positive, negative)
|
||||
|
||||
append = execute # TODO: remove
|
||||
|
||||
|
||||
class VAEEncodeAudio(IO.ComfyNode):
|
||||
@classmethod
|
||||
def define_schema(cls):
|
||||
return IO.Schema(
|
||||
node_id="VAEEncodeAudio",
|
||||
search_aliases=["audio to latent"],
|
||||
display_name="VAE Encode Audio",
|
||||
category="latent/audio",
|
||||
inputs=[
|
||||
IO.Audio.Input("audio"),
|
||||
IO.Vae.Input("vae"),
|
||||
],
|
||||
outputs=[IO.Latent.Output()],
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def execute(cls, vae, audio) -> IO.NodeOutput:
|
||||
sample_rate = audio["sample_rate"]
|
||||
if 44100 != sample_rate:
|
||||
waveform = torchaudio.functional.resample(audio["waveform"], sample_rate, 44100)
|
||||
else:
|
||||
waveform = audio["waveform"]
|
||||
|
||||
t = vae.encode(waveform.movedim(1, -1))
|
||||
return IO.NodeOutput({"samples":t})
|
||||
|
||||
encode = execute # TODO: remove
|
||||
|
||||
|
||||
class VAEDecodeAudio(IO.ComfyNode):
|
||||
@classmethod
|
||||
def define_schema(cls):
|
||||
return IO.Schema(
|
||||
node_id="VAEDecodeAudio",
|
||||
search_aliases=["latent to audio"],
|
||||
display_name="VAE Decode Audio",
|
||||
category="latent/audio",
|
||||
inputs=[
|
||||
IO.Latent.Input("samples"),
|
||||
IO.Vae.Input("vae"),
|
||||
],
|
||||
outputs=[IO.Audio.Output()],
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def execute(cls, vae, samples) -> IO.NodeOutput:
|
||||
audio = vae.decode(samples["samples"]).movedim(-1, 1)
|
||||
std = torch.std(audio, dim=[1,2], keepdim=True) * 5.0
|
||||
std[std < 1.0] = 1.0
|
||||
audio /= std
|
||||
return IO.NodeOutput({"waveform": audio, "sample_rate": 44100 if "sample_rate" not in samples else samples["sample_rate"]})
|
||||
|
||||
decode = execute # TODO: remove
|
||||
|
||||
|
||||
class SaveAudio(IO.ComfyNode):
|
||||
@classmethod
|
||||
def define_schema(cls):
|
||||
return IO.Schema(
|
||||
node_id="SaveAudio",
|
||||
search_aliases=["export flac"],
|
||||
display_name="Save Audio (FLAC)",
|
||||
category="audio",
|
||||
inputs=[
|
||||
IO.Audio.Input("audio"),
|
||||
IO.String.Input("filename_prefix", default="audio/ComfyUI"),
|
||||
],
|
||||
hidden=[IO.Hidden.prompt, IO.Hidden.extra_pnginfo],
|
||||
is_output_node=True,
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def execute(cls, audio, filename_prefix="ComfyUI", format="flac") -> IO.NodeOutput:
|
||||
return IO.NodeOutput(
|
||||
ui=UI.AudioSaveHelper.get_save_audio_ui(audio, filename_prefix=filename_prefix, cls=cls, format=format)
|
||||
)
|
||||
|
||||
save_flac = execute # TODO: remove
|
||||
|
||||
|
||||
class SaveAudioMP3(IO.ComfyNode):
|
||||
@classmethod
|
||||
def define_schema(cls):
|
||||
return IO.Schema(
|
||||
node_id="SaveAudioMP3",
|
||||
search_aliases=["export mp3"],
|
||||
display_name="Save Audio (MP3)",
|
||||
category="audio",
|
||||
inputs=[
|
||||
IO.Audio.Input("audio"),
|
||||
IO.String.Input("filename_prefix", default="audio/ComfyUI"),
|
||||
IO.Combo.Input("quality", options=["V0", "128k", "320k"], default="V0"),
|
||||
],
|
||||
hidden=[IO.Hidden.prompt, IO.Hidden.extra_pnginfo],
|
||||
is_output_node=True,
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def execute(cls, audio, filename_prefix="ComfyUI", format="mp3", quality="128k") -> IO.NodeOutput:
|
||||
return IO.NodeOutput(
|
||||
ui=UI.AudioSaveHelper.get_save_audio_ui(
|
||||
audio, filename_prefix=filename_prefix, cls=cls, format=format, quality=quality
|
||||
)
|
||||
)
|
||||
|
||||
save_mp3 = execute # TODO: remove
|
||||
|
||||
|
||||
class SaveAudioOpus(IO.ComfyNode):
|
||||
@classmethod
|
||||
def define_schema(cls):
|
||||
return IO.Schema(
|
||||
node_id="SaveAudioOpus",
|
||||
search_aliases=["export opus"],
|
||||
display_name="Save Audio (Opus)",
|
||||
category="audio",
|
||||
inputs=[
|
||||
IO.Audio.Input("audio"),
|
||||
IO.String.Input("filename_prefix", default="audio/ComfyUI"),
|
||||
IO.Combo.Input("quality", options=["64k", "96k", "128k", "192k", "320k"], default="128k"),
|
||||
],
|
||||
hidden=[IO.Hidden.prompt, IO.Hidden.extra_pnginfo],
|
||||
is_output_node=True,
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def execute(cls, audio, filename_prefix="ComfyUI", format="opus", quality="V3") -> IO.NodeOutput:
|
||||
return IO.NodeOutput(
|
||||
ui=UI.AudioSaveHelper.get_save_audio_ui(
|
||||
audio, filename_prefix=filename_prefix, cls=cls, format=format, quality=quality
|
||||
)
|
||||
)
|
||||
|
||||
save_opus = execute # TODO: remove
|
||||
|
||||
|
||||
class PreviewAudio(IO.ComfyNode):
|
||||
@classmethod
|
||||
def define_schema(cls):
|
||||
return IO.Schema(
|
||||
node_id="PreviewAudio",
|
||||
search_aliases=["play audio"],
|
||||
display_name="Preview Audio",
|
||||
category="audio",
|
||||
inputs=[
|
||||
IO.Audio.Input("audio"),
|
||||
],
|
||||
hidden=[IO.Hidden.prompt, IO.Hidden.extra_pnginfo],
|
||||
is_output_node=True,
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def execute(cls, audio) -> IO.NodeOutput:
|
||||
return IO.NodeOutput(ui=UI.PreviewAudio(audio, cls=cls))
|
||||
|
||||
save_flac = execute # TODO: remove
|
||||
|
||||
|
||||
def f32_pcm(wav: torch.Tensor) -> torch.Tensor:
|
||||
"""Convert audio to float 32 bits PCM format."""
|
||||
if wav.dtype.is_floating_point:
|
||||
return wav
|
||||
elif wav.dtype == torch.int16:
|
||||
return wav.float() / (2 ** 15)
|
||||
elif wav.dtype == torch.int32:
|
||||
return wav.float() / (2 ** 31)
|
||||
raise ValueError(f"Unsupported wav dtype: {wav.dtype}")
|
||||
|
||||
def load(filepath: str) -> tuple[torch.Tensor, int]:
|
||||
with av.open(filepath) as af:
|
||||
if not af.streams.audio:
|
||||
raise ValueError("No audio stream found in the file.")
|
||||
|
||||
stream = af.streams.audio[0]
|
||||
sr = stream.codec_context.sample_rate
|
||||
n_channels = stream.channels
|
||||
|
||||
frames = []
|
||||
length = 0
|
||||
for frame in af.decode(streams=stream.index):
|
||||
buf = torch.from_numpy(frame.to_ndarray())
|
||||
if buf.shape[0] != n_channels:
|
||||
buf = buf.view(-1, n_channels).t()
|
||||
|
||||
frames.append(buf)
|
||||
length += buf.shape[1]
|
||||
|
||||
if not frames:
|
||||
raise ValueError("No audio frames decoded.")
|
||||
|
||||
wav = torch.cat(frames, dim=1)
|
||||
wav = f32_pcm(wav)
|
||||
return wav, sr
|
||||
|
||||
class LoadAudio(IO.ComfyNode):
|
||||
@classmethod
|
||||
def define_schema(cls):
|
||||
input_dir = folder_paths.get_input_directory()
|
||||
files = folder_paths.filter_files_content_types(os.listdir(input_dir), ["audio", "video"])
|
||||
return IO.Schema(
|
||||
node_id="LoadAudio",
|
||||
search_aliases=["import audio", "open audio", "audio file"],
|
||||
display_name="Load Audio",
|
||||
category="audio",
|
||||
inputs=[
|
||||
IO.Combo.Input("audio", upload=IO.UploadType.audio, options=sorted(files)),
|
||||
],
|
||||
outputs=[IO.Audio.Output()],
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def execute(cls, audio) -> IO.NodeOutput:
|
||||
audio_path = folder_paths.get_annotated_filepath(audio)
|
||||
waveform, sample_rate = load(audio_path)
|
||||
audio = {"waveform": waveform.unsqueeze(0), "sample_rate": sample_rate}
|
||||
return IO.NodeOutput(audio)
|
||||
|
||||
@classmethod
|
||||
def fingerprint_inputs(cls, audio):
|
||||
image_path = folder_paths.get_annotated_filepath(audio)
|
||||
m = hashlib.sha256()
|
||||
with open(image_path, 'rb') as f:
|
||||
m.update(f.read())
|
||||
return m.digest().hex()
|
||||
|
||||
@classmethod
|
||||
def validate_inputs(cls, audio):
|
||||
if not folder_paths.exists_annotated_filepath(audio):
|
||||
return "Invalid audio file: {}".format(audio)
|
||||
return True
|
||||
|
||||
load = execute # TODO: remove
|
||||
|
||||
|
||||
class RecordAudio(IO.ComfyNode):
|
||||
@classmethod
|
||||
def define_schema(cls):
|
||||
return IO.Schema(
|
||||
node_id="RecordAudio",
|
||||
search_aliases=["microphone input", "audio capture", "voice input"],
|
||||
display_name="Record Audio",
|
||||
category="audio",
|
||||
inputs=[
|
||||
IO.Custom("AUDIO_RECORD").Input("audio"),
|
||||
],
|
||||
outputs=[IO.Audio.Output()],
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def execute(cls, audio) -> IO.NodeOutput:
|
||||
audio_path = folder_paths.get_annotated_filepath(audio)
|
||||
|
||||
waveform, sample_rate = load(audio_path)
|
||||
audio = {"waveform": waveform.unsqueeze(0), "sample_rate": sample_rate}
|
||||
return IO.NodeOutput(audio)
|
||||
|
||||
load = execute # TODO: remove
|
||||
|
||||
|
||||
class TrimAudioDuration(IO.ComfyNode):
|
||||
@classmethod
|
||||
def define_schema(cls):
|
||||
return IO.Schema(
|
||||
node_id="TrimAudioDuration",
|
||||
search_aliases=["cut audio", "audio clip", "shorten audio"],
|
||||
display_name="Trim Audio Duration",
|
||||
description="Trim audio tensor into chosen time range.",
|
||||
category="audio",
|
||||
inputs=[
|
||||
IO.Audio.Input("audio"),
|
||||
IO.Float.Input(
|
||||
"start_index",
|
||||
default=0.0,
|
||||
min=-0xffffffffffffffff,
|
||||
max=0xffffffffffffffff,
|
||||
step=0.01,
|
||||
tooltip="Start time in seconds, can be negative to count from the end (supports sub-seconds).",
|
||||
),
|
||||
IO.Float.Input(
|
||||
"duration",
|
||||
default=60.0,
|
||||
min=0.0,
|
||||
step=0.01,
|
||||
tooltip="Duration in seconds",
|
||||
),
|
||||
],
|
||||
outputs=[IO.Audio.Output()],
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def execute(cls, audio, start_index, duration) -> IO.NodeOutput:
|
||||
waveform = audio["waveform"]
|
||||
sample_rate = audio["sample_rate"]
|
||||
audio_length = waveform.shape[-1]
|
||||
|
||||
if start_index < 0:
|
||||
start_frame = audio_length + int(round(start_index * sample_rate))
|
||||
else:
|
||||
start_frame = int(round(start_index * sample_rate))
|
||||
start_frame = max(0, min(start_frame, audio_length - 1))
|
||||
|
||||
end_frame = start_frame + int(round(duration * sample_rate))
|
||||
end_frame = max(0, min(end_frame, audio_length))
|
||||
|
||||
if start_frame >= end_frame:
|
||||
raise ValueError("AudioTrim: Start time must be less than end time and be within the audio length.")
|
||||
|
||||
return IO.NodeOutput({"waveform": waveform[..., start_frame:end_frame], "sample_rate": sample_rate})
|
||||
|
||||
trim = execute # TODO: remove
|
||||
|
||||
|
||||
class SplitAudioChannels(IO.ComfyNode):
|
||||
@classmethod
|
||||
def define_schema(cls):
|
||||
return IO.Schema(
|
||||
node_id="SplitAudioChannels",
|
||||
search_aliases=["stereo to mono"],
|
||||
display_name="Split Audio Channels",
|
||||
description="Separates the audio into left and right channels.",
|
||||
category="audio",
|
||||
inputs=[
|
||||
IO.Audio.Input("audio"),
|
||||
],
|
||||
outputs=[
|
||||
IO.Audio.Output(display_name="left"),
|
||||
IO.Audio.Output(display_name="right"),
|
||||
],
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def execute(cls, audio) -> IO.NodeOutput:
|
||||
waveform = audio["waveform"]
|
||||
sample_rate = audio["sample_rate"]
|
||||
|
||||
if waveform.shape[1] != 2:
|
||||
raise ValueError("AudioSplit: Input audio has only one channel.")
|
||||
|
||||
left_channel = waveform[..., 0:1, :]
|
||||
right_channel = waveform[..., 1:2, :]
|
||||
|
||||
return IO.NodeOutput({"waveform": left_channel, "sample_rate": sample_rate}, {"waveform": right_channel, "sample_rate": sample_rate})
|
||||
|
||||
separate = execute # TODO: remove
|
||||
|
||||
class JoinAudioChannels(IO.ComfyNode):
|
||||
@classmethod
|
||||
def define_schema(cls):
|
||||
return IO.Schema(
|
||||
node_id="JoinAudioChannels",
|
||||
display_name="Join Audio Channels",
|
||||
description="Joins left and right mono audio channels into a stereo audio.",
|
||||
category="audio",
|
||||
inputs=[
|
||||
IO.Audio.Input("audio_left"),
|
||||
IO.Audio.Input("audio_right"),
|
||||
],
|
||||
outputs=[
|
||||
IO.Audio.Output(display_name="audio"),
|
||||
],
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def execute(cls, audio_left, audio_right) -> IO.NodeOutput:
|
||||
waveform_left = audio_left["waveform"]
|
||||
sample_rate_left = audio_left["sample_rate"]
|
||||
waveform_right = audio_right["waveform"]
|
||||
sample_rate_right = audio_right["sample_rate"]
|
||||
|
||||
if waveform_left.shape[1] != 1 or waveform_right.shape[1] != 1:
|
||||
raise ValueError("AudioJoin: Both input audios must be mono.")
|
||||
|
||||
# Handle different sample rates by resampling to the higher rate
|
||||
waveform_left, waveform_right, output_sample_rate = match_audio_sample_rates(
|
||||
waveform_left, sample_rate_left, waveform_right, sample_rate_right
|
||||
)
|
||||
|
||||
# Handle different lengths by trimming to the shorter length
|
||||
length_left = waveform_left.shape[-1]
|
||||
length_right = waveform_right.shape[-1]
|
||||
|
||||
if length_left != length_right:
|
||||
min_length = min(length_left, length_right)
|
||||
if length_left > min_length:
|
||||
logging.info(f"JoinAudioChannels: Trimming left channel from {length_left} to {min_length} samples.")
|
||||
waveform_left = waveform_left[..., :min_length]
|
||||
if length_right > min_length:
|
||||
logging.info(f"JoinAudioChannels: Trimming right channel from {length_right} to {min_length} samples.")
|
||||
waveform_right = waveform_right[..., :min_length]
|
||||
|
||||
# Join the channels into stereo
|
||||
left_channel = waveform_left[..., 0:1, :]
|
||||
right_channel = waveform_right[..., 0:1, :]
|
||||
stereo_waveform = torch.cat([left_channel, right_channel], dim=1)
|
||||
|
||||
return IO.NodeOutput({"waveform": stereo_waveform, "sample_rate": output_sample_rate})
|
||||
|
||||
|
||||
def match_audio_sample_rates(waveform_1, sample_rate_1, waveform_2, sample_rate_2):
|
||||
if sample_rate_1 != sample_rate_2:
|
||||
if sample_rate_1 > sample_rate_2:
|
||||
waveform_2 = torchaudio.functional.resample(waveform_2, sample_rate_2, sample_rate_1)
|
||||
output_sample_rate = sample_rate_1
|
||||
logging.info(f"Resampling audio2 from {sample_rate_2}Hz to {sample_rate_1}Hz for merging.")
|
||||
else:
|
||||
waveform_1 = torchaudio.functional.resample(waveform_1, sample_rate_1, sample_rate_2)
|
||||
output_sample_rate = sample_rate_2
|
||||
logging.info(f"Resampling audio1 from {sample_rate_1}Hz to {sample_rate_2}Hz for merging.")
|
||||
else:
|
||||
output_sample_rate = sample_rate_1
|
||||
return waveform_1, waveform_2, output_sample_rate
|
||||
|
||||
|
||||
class AudioConcat(IO.ComfyNode):
|
||||
@classmethod
|
||||
def define_schema(cls):
|
||||
return IO.Schema(
|
||||
node_id="AudioConcat",
|
||||
search_aliases=["join audio", "combine audio", "append audio"],
|
||||
display_name="Audio Concat",
|
||||
description="Concatenates the audio1 to audio2 in the specified direction.",
|
||||
category="audio",
|
||||
inputs=[
|
||||
IO.Audio.Input("audio1"),
|
||||
IO.Audio.Input("audio2"),
|
||||
IO.Combo.Input(
|
||||
"direction",
|
||||
options=['after', 'before'],
|
||||
default="after",
|
||||
tooltip="Whether to append audio2 after or before audio1.",
|
||||
)
|
||||
],
|
||||
outputs=[IO.Audio.Output()],
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def execute(cls, audio1, audio2, direction) -> IO.NodeOutput:
|
||||
waveform_1 = audio1["waveform"]
|
||||
waveform_2 = audio2["waveform"]
|
||||
sample_rate_1 = audio1["sample_rate"]
|
||||
sample_rate_2 = audio2["sample_rate"]
|
||||
|
||||
if waveform_1.shape[1] == 1:
|
||||
waveform_1 = waveform_1.repeat(1, 2, 1)
|
||||
logging.info("AudioConcat: Converted mono audio1 to stereo by duplicating the channel.")
|
||||
if waveform_2.shape[1] == 1:
|
||||
waveform_2 = waveform_2.repeat(1, 2, 1)
|
||||
logging.info("AudioConcat: Converted mono audio2 to stereo by duplicating the channel.")
|
||||
|
||||
waveform_1, waveform_2, output_sample_rate = match_audio_sample_rates(waveform_1, sample_rate_1, waveform_2, sample_rate_2)
|
||||
|
||||
if direction == 'after':
|
||||
concatenated_audio = torch.cat((waveform_1, waveform_2), dim=2)
|
||||
elif direction == 'before':
|
||||
concatenated_audio = torch.cat((waveform_2, waveform_1), dim=2)
|
||||
|
||||
return IO.NodeOutput({"waveform": concatenated_audio, "sample_rate": output_sample_rate})
|
||||
|
||||
concat = execute # TODO: remove
|
||||
|
||||
|
||||
class AudioMerge(IO.ComfyNode):
|
||||
@classmethod
|
||||
def define_schema(cls):
|
||||
return IO.Schema(
|
||||
node_id="AudioMerge",
|
||||
search_aliases=["mix audio", "overlay audio", "layer audio"],
|
||||
display_name="Audio Merge",
|
||||
description="Combine two audio tracks by overlaying their waveforms.",
|
||||
category="audio",
|
||||
inputs=[
|
||||
IO.Audio.Input("audio1"),
|
||||
IO.Audio.Input("audio2"),
|
||||
IO.Combo.Input(
|
||||
"merge_method",
|
||||
options=["add", "mean", "subtract", "multiply"],
|
||||
tooltip="The method used to combine the audio waveforms.",
|
||||
)
|
||||
],
|
||||
outputs=[IO.Audio.Output()],
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def execute(cls, audio1, audio2, merge_method) -> IO.NodeOutput:
|
||||
waveform_1 = audio1["waveform"]
|
||||
waveform_2 = audio2["waveform"]
|
||||
sample_rate_1 = audio1["sample_rate"]
|
||||
sample_rate_2 = audio2["sample_rate"]
|
||||
|
||||
waveform_1, waveform_2, output_sample_rate = match_audio_sample_rates(waveform_1, sample_rate_1, waveform_2, sample_rate_2)
|
||||
|
||||
length_1 = waveform_1.shape[-1]
|
||||
length_2 = waveform_2.shape[-1]
|
||||
|
||||
if length_2 > length_1:
|
||||
logging.info(f"AudioMerge: Trimming audio2 from {length_2} to {length_1} samples to match audio1 length.")
|
||||
waveform_2 = waveform_2[..., :length_1]
|
||||
elif length_2 < length_1:
|
||||
logging.info(f"AudioMerge: Padding audio2 from {length_2} to {length_1} samples to match audio1 length.")
|
||||
pad_shape = list(waveform_2.shape)
|
||||
pad_shape[-1] = length_1 - length_2
|
||||
pad_tensor = torch.zeros(pad_shape, dtype=waveform_2.dtype, device=waveform_2.device)
|
||||
waveform_2 = torch.cat((waveform_2, pad_tensor), dim=-1)
|
||||
|
||||
if merge_method == "add":
|
||||
waveform = waveform_1 + waveform_2
|
||||
elif merge_method == "subtract":
|
||||
waveform = waveform_1 - waveform_2
|
||||
elif merge_method == "multiply":
|
||||
waveform = waveform_1 * waveform_2
|
||||
elif merge_method == "mean":
|
||||
waveform = (waveform_1 + waveform_2) / 2
|
||||
|
||||
max_val = waveform.abs().max()
|
||||
if max_val > 1.0:
|
||||
waveform = waveform / max_val
|
||||
|
||||
return IO.NodeOutput({"waveform": waveform, "sample_rate": output_sample_rate})
|
||||
|
||||
merge = execute # TODO: remove
|
||||
|
||||
|
||||
class AudioAdjustVolume(IO.ComfyNode):
|
||||
@classmethod
|
||||
def define_schema(cls):
|
||||
return IO.Schema(
|
||||
node_id="AudioAdjustVolume",
|
||||
search_aliases=["audio gain", "loudness", "audio level"],
|
||||
display_name="Audio Adjust Volume",
|
||||
category="audio",
|
||||
inputs=[
|
||||
IO.Audio.Input("audio"),
|
||||
IO.Int.Input(
|
||||
"volume",
|
||||
default=1,
|
||||
min=-100,
|
||||
max=100,
|
||||
tooltip="Volume adjustment in decibels (dB). 0 = no change, +6 = double, -6 = half, etc",
|
||||
)
|
||||
],
|
||||
outputs=[IO.Audio.Output()],
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def execute(cls, audio, volume) -> IO.NodeOutput:
|
||||
if volume == 0:
|
||||
return IO.NodeOutput(audio)
|
||||
waveform = audio["waveform"]
|
||||
sample_rate = audio["sample_rate"]
|
||||
|
||||
gain = 10 ** (volume / 20)
|
||||
waveform = waveform * gain
|
||||
|
||||
return IO.NodeOutput({"waveform": waveform, "sample_rate": sample_rate})
|
||||
|
||||
adjust_volume = execute # TODO: remove
|
||||
|
||||
|
||||
class EmptyAudio(IO.ComfyNode):
|
||||
@classmethod
|
||||
def define_schema(cls):
|
||||
return IO.Schema(
|
||||
node_id="EmptyAudio",
|
||||
search_aliases=["blank audio"],
|
||||
display_name="Empty Audio",
|
||||
category="audio",
|
||||
inputs=[
|
||||
IO.Float.Input(
|
||||
"duration",
|
||||
default=60.0,
|
||||
min=0.0,
|
||||
max=0xffffffffffffffff,
|
||||
step=0.01,
|
||||
tooltip="Duration of the empty audio clip in seconds",
|
||||
),
|
||||
IO.Int.Input(
|
||||
"sample_rate",
|
||||
default=44100,
|
||||
tooltip="Sample rate of the empty audio clip.",
|
||||
min=1,
|
||||
max=192000,
|
||||
),
|
||||
IO.Int.Input(
|
||||
"channels",
|
||||
default=2,
|
||||
min=1,
|
||||
max=2,
|
||||
tooltip="Number of audio channels (1 for mono, 2 for stereo).",
|
||||
),
|
||||
],
|
||||
outputs=[IO.Audio.Output()],
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def execute(cls, duration, sample_rate, channels) -> IO.NodeOutput:
|
||||
num_samples = int(round(duration * sample_rate))
|
||||
waveform = torch.zeros((1, channels, num_samples), dtype=torch.float32)
|
||||
return IO.NodeOutput({"waveform": waveform, "sample_rate": sample_rate})
|
||||
|
||||
create_empty_audio = execute # TODO: remove
|
||||
|
||||
|
||||
class AudioExtension(ComfyExtension):
|
||||
@override
|
||||
async def get_node_list(self) -> list[type[IO.ComfyNode]]:
|
||||
return [
|
||||
EmptyLatentAudio,
|
||||
VAEEncodeAudio,
|
||||
VAEDecodeAudio,
|
||||
SaveAudio,
|
||||
SaveAudioMP3,
|
||||
SaveAudioOpus,
|
||||
LoadAudio,
|
||||
PreviewAudio,
|
||||
ConditioningStableAudio,
|
||||
RecordAudio,
|
||||
TrimAudioDuration,
|
||||
SplitAudioChannels,
|
||||
JoinAudioChannels,
|
||||
AudioConcat,
|
||||
AudioMerge,
|
||||
AudioAdjustVolume,
|
||||
EmptyAudio,
|
||||
]
|
||||
|
||||
async def comfy_entrypoint() -> AudioExtension:
|
||||
return AudioExtension()
|
||||
+44
@@ -0,0 +1,44 @@
|
||||
import json
|
||||
from comfy.comfy_types.node_typing import IO
|
||||
|
||||
# Preview Any - original implement from
|
||||
# https://github.com/rgthree/rgthree-comfy/blob/main/py/display_any.py
|
||||
# upstream requested in https://github.com/Kosinkadink/rfcs/blob/main/rfcs/0000-corenodes.md#preview-nodes
|
||||
class PreviewAny():
|
||||
@classmethod
|
||||
def INPUT_TYPES(cls):
|
||||
return {
|
||||
"required": {"source": (IO.ANY, {})},
|
||||
}
|
||||
|
||||
RETURN_TYPES = ()
|
||||
FUNCTION = "main"
|
||||
OUTPUT_NODE = True
|
||||
|
||||
CATEGORY = "utils"
|
||||
SEARCH_ALIASES = ["show output", "inspect", "debug", "print value", "show text"]
|
||||
|
||||
def main(self, source=None):
|
||||
value = 'None'
|
||||
if isinstance(source, str):
|
||||
value = source
|
||||
elif isinstance(source, (int, float, bool)):
|
||||
value = str(source)
|
||||
elif source is not None:
|
||||
try:
|
||||
value = json.dumps(source, indent=4)
|
||||
except Exception:
|
||||
try:
|
||||
value = str(source)
|
||||
except Exception:
|
||||
value = 'source exists, but could not be serialized.'
|
||||
|
||||
return {"ui": {"text": (value,)}}
|
||||
|
||||
NODE_CLASS_MAPPINGS = {
|
||||
"PreviewAny": PreviewAny,
|
||||
}
|
||||
|
||||
NODE_DISPLAY_NAME_MAPPINGS = {
|
||||
"PreviewAny": "Preview as Text",
|
||||
}
|
||||
+552
@@ -0,0 +1,552 @@
|
||||
import type { operations } from '@comfyorg/registry-types'
|
||||
|
||||
export function formatCamelCase(str: string): string {
|
||||
// Check if the string is camel case
|
||||
const isCamelCase = /^([A-Z][a-z]*)+$/.test(str)
|
||||
|
||||
if (!isCamelCase) {
|
||||
return str // Return original string if not camel case
|
||||
}
|
||||
|
||||
// Split the string into words, keeping acronyms together
|
||||
const words = str.split(/(?=[A-Z][a-z])|\d+/)
|
||||
|
||||
// Process each word
|
||||
const processedWords = words.map((word) => {
|
||||
// If the word is all uppercase and longer than one character, it's likely an acronym
|
||||
if (word.length > 1 && word === word.toUpperCase()) {
|
||||
return word // Keep acronyms as is
|
||||
}
|
||||
// For other words, ensure the first letter is capitalized
|
||||
return word.charAt(0).toUpperCase() + word.slice(1)
|
||||
})
|
||||
|
||||
// Join the words with spaces
|
||||
return processedWords.join(' ')
|
||||
}
|
||||
|
||||
export function appendJsonExt(path: string) {
|
||||
if (!path.toLowerCase().endsWith('.json')) {
|
||||
path += '.json'
|
||||
}
|
||||
return path
|
||||
}
|
||||
|
||||
export function highlightQuery(text: string, query: string) {
|
||||
if (!query) return text
|
||||
|
||||
// Escape special regex characters in the query string
|
||||
const escapedQuery = query.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
|
||||
|
||||
const regex = new RegExp(`(${escapedQuery})`, 'gi')
|
||||
return text.replace(regex, '<span class="highlight">$1</span>')
|
||||
}
|
||||
|
||||
export function formatNumberWithSuffix(
|
||||
num: number,
|
||||
{
|
||||
precision = 1,
|
||||
roundToInt = false
|
||||
}: { precision?: number; roundToInt?: boolean } = {}
|
||||
): string {
|
||||
const suffixes = ['', 'k', 'm', 'b', 't']
|
||||
const absNum = Math.abs(num)
|
||||
|
||||
if (absNum < 1000) {
|
||||
return roundToInt ? Math.round(num).toString() : num.toFixed(precision)
|
||||
}
|
||||
|
||||
const exp = Math.min(Math.floor(Math.log10(absNum) / 3), suffixes.length - 1)
|
||||
const formattedNum = (num / Math.pow(1000, exp)).toFixed(precision)
|
||||
|
||||
return `${formattedNum}${suffixes[exp]}`
|
||||
}
|
||||
|
||||
export function formatSize(value?: number) {
|
||||
if (value === null || value === undefined) {
|
||||
return '-'
|
||||
}
|
||||
|
||||
const bytes = value
|
||||
if (bytes === 0) return '0 B'
|
||||
const k = 1024
|
||||
const sizes = ['B', 'KB', 'MB', 'GB']
|
||||
const i = Math.floor(Math.log(bytes) / Math.log(k))
|
||||
return `${parseFloat((bytes / Math.pow(k, i)).toFixed(2))} ${sizes[i]}`
|
||||
}
|
||||
|
||||
/**
|
||||
* Formats a commit hash by truncating long (40-char) hashes to 7 chars.
|
||||
* Returns the original string if not a valid full commit hash.
|
||||
*/
|
||||
export function formatCommitHash(value: string): string {
|
||||
if (/^[a-f0-9]{40}$/i.test(value)) {
|
||||
return value.slice(0, 7)
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns various filename components.
|
||||
* Example:
|
||||
* - fullFilename: 'file.txt'
|
||||
* - filename: 'file'
|
||||
* - suffix: 'txt'
|
||||
*/
|
||||
export function getFilenameDetails(fullFilename: string) {
|
||||
if (fullFilename.includes('.')) {
|
||||
return {
|
||||
filename: fullFilename.split('.').slice(0, -1).join('.'),
|
||||
suffix: fullFilename.split('.').pop() ?? null
|
||||
}
|
||||
} else {
|
||||
return { filename: fullFilename, suffix: null }
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns various path components.
|
||||
* Example:
|
||||
* - path: 'dir/file.txt'
|
||||
* - directory: 'dir'
|
||||
* - fullFilename: 'file.txt'
|
||||
* - filename: 'file'
|
||||
* - suffix: 'txt'
|
||||
*/
|
||||
export function getPathDetails(path: string) {
|
||||
const directory = path.split('/').slice(0, -1).join('/')
|
||||
const fullFilename = path.split('/').pop() ?? path
|
||||
return { directory, fullFilename, ...getFilenameDetails(fullFilename) }
|
||||
}
|
||||
|
||||
/**
|
||||
* Normalizes a string to be used as an i18n key.
|
||||
* Replaces dots with underscores.
|
||||
*/
|
||||
export function normalizeI18nKey(key: string) {
|
||||
return typeof key === 'string' ? key.replace(/\./g, '_') : ''
|
||||
}
|
||||
|
||||
/**
|
||||
* Takes a dynamic prompt in the format {opt1|opt2|{optA|optB}|} and randomly replaces groups. Supports C style comments.
|
||||
* @param input The dynamic prompt to process
|
||||
* @returns
|
||||
*/
|
||||
export function processDynamicPrompt(input: string): string {
|
||||
/*
|
||||
* Strips C-style line and block comments from a string
|
||||
*/
|
||||
function stripComments(str: string) {
|
||||
return str.replace(/\/\*[\s\S]*?\*\/|\/\/.*/g, '')
|
||||
}
|
||||
|
||||
let i = 0
|
||||
let result = ''
|
||||
input = stripComments(input)
|
||||
|
||||
const handleEscape = () => {
|
||||
const nextChar = input[i++]
|
||||
return '\\' + nextChar
|
||||
}
|
||||
|
||||
function parseChoiceBlock() {
|
||||
// Parse the content inside {}
|
||||
const options: string[] = []
|
||||
let choice = ''
|
||||
let depth = 0
|
||||
|
||||
while (i < input.length) {
|
||||
const char = input[i++]
|
||||
|
||||
if (char === '\\') {
|
||||
choice += handleEscape()
|
||||
continue
|
||||
} else if (char === '{') {
|
||||
depth++
|
||||
} else if (char === '}') {
|
||||
if (!depth) break
|
||||
depth--
|
||||
} else if (char === '|') {
|
||||
if (!depth) {
|
||||
options.push(choice)
|
||||
choice = ''
|
||||
continue
|
||||
}
|
||||
}
|
||||
choice += char
|
||||
}
|
||||
|
||||
options.push(choice)
|
||||
|
||||
const chosenOption = options[Math.floor(Math.random() * options.length)]
|
||||
return processDynamicPrompt(chosenOption)
|
||||
}
|
||||
|
||||
while (i < input.length) {
|
||||
const char = input[i++]
|
||||
if (char === '\\') {
|
||||
result += handleEscape()
|
||||
} else if (char === '{') {
|
||||
result += parseChoiceBlock()
|
||||
} else {
|
||||
result += char
|
||||
}
|
||||
}
|
||||
|
||||
return result.replace(/\\([{}|])/g, '$1')
|
||||
}
|
||||
|
||||
export function isValidUrl(url: string): boolean {
|
||||
try {
|
||||
new URL(url)
|
||||
return true
|
||||
} catch {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses a filepath into its filename and subfolder components.
|
||||
*
|
||||
* @example
|
||||
* parseFilePath('folder/file.txt') // → { filename: 'file.txt', subfolder: 'folder' }
|
||||
* parseFilePath('/folder/file.txt') // → { filename: 'file.txt', subfolder: 'folder' }
|
||||
* parseFilePath('file.txt') // → { filename: 'file.txt', subfolder: '' }
|
||||
* parseFilePath('folder//file.txt') // → { filename: 'file.txt', subfolder: 'folder' }
|
||||
*
|
||||
* @param filepath The filepath to parse
|
||||
* @returns Object containing filename and subfolder
|
||||
*/
|
||||
export function parseFilePath(filepath: string): {
|
||||
filename: string
|
||||
subfolder: string
|
||||
} {
|
||||
if (!filepath?.trim()) return { filename: '', subfolder: '' }
|
||||
|
||||
const normalizedPath = filepath
|
||||
.replace(/[\\/]+/g, '/') // Normalize path separators
|
||||
.replace(/^\//, '') // Remove leading slash
|
||||
.replace(/\/$/, '') // Remove trailing slash
|
||||
|
||||
const lastSlashIndex = normalizedPath.lastIndexOf('/')
|
||||
|
||||
if (lastSlashIndex === -1) {
|
||||
return {
|
||||
filename: normalizedPath,
|
||||
subfolder: ''
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
filename: normalizedPath.slice(lastSlashIndex + 1),
|
||||
subfolder: normalizedPath.slice(0, lastSlashIndex)
|
||||
}
|
||||
}
|
||||
|
||||
// Simple date formatter
|
||||
const parts = {
|
||||
d: (d: Date) => d.getDate(),
|
||||
M: (d: Date) => d.getMonth() + 1,
|
||||
h: (d: Date) => d.getHours(),
|
||||
m: (d: Date) => d.getMinutes(),
|
||||
s: (d: Date) => d.getSeconds()
|
||||
}
|
||||
const format =
|
||||
Object.keys(parts)
|
||||
.map((k) => k + k + '?')
|
||||
.join('|') + '|yyy?y?'
|
||||
|
||||
export function formatDate(text: string, date: Date) {
|
||||
return text.replace(new RegExp(format, 'g'), (text: string): string => {
|
||||
if (text === 'yy') return (date.getFullYear() + '').substring(2)
|
||||
if (text === 'yyyy') return date.getFullYear().toString()
|
||||
if (text[0] in parts) {
|
||||
const p = parts[text[0] as keyof typeof parts](date)
|
||||
return (p + '').padStart(text.length, '0')
|
||||
}
|
||||
return text
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a cache key from parameters
|
||||
* Sorts the parameters to ensure consistent keys regardless of parameter order
|
||||
*/
|
||||
export const paramsToCacheKey = (params: unknown): string => {
|
||||
if (typeof params === 'string') return params
|
||||
if (typeof params === 'object' && params !== null)
|
||||
return Object.keys(params)
|
||||
.sort((a, b) => a.localeCompare(b))
|
||||
.map((key) => `${key}:${params[key as keyof typeof params]}`)
|
||||
.join('&')
|
||||
|
||||
return String(params)
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a RFC4122 compliant UUID v4 using the native crypto API when available
|
||||
* @returns A properly formatted UUID string
|
||||
*/
|
||||
export const generateUUID = (): string => {
|
||||
// Use native crypto.randomUUID() if available (modern browsers)
|
||||
if (
|
||||
typeof crypto !== 'undefined' &&
|
||||
typeof crypto.randomUUID === 'function'
|
||||
) {
|
||||
return crypto.randomUUID()
|
||||
}
|
||||
|
||||
// Fallback implementation for older browsers
|
||||
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
|
||||
const r = (Math.random() * 16) | 0
|
||||
const v = c === 'x' ? r : (r & 0x3) | 0x8
|
||||
return v.toString(16)
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a URL is a Civitai model URL
|
||||
* @example
|
||||
* isCivitaiModelUrl('https://civitai.com/api/download/models/1234567890') // true
|
||||
* isCivitaiModelUrl('https://civitai.com/api/v1/models/1234567890') // true
|
||||
* isCivitaiModelUrl('https://civitai.com/api/v1/models-versions/15342') // true
|
||||
* isCivitaiModelUrl('https://example.com/model.safetensors') // false
|
||||
*/
|
||||
export const isCivitaiModelUrl = (url: string): boolean => {
|
||||
if (!isValidUrl(url)) return false
|
||||
if (!url.includes('civitai.com')) return false
|
||||
|
||||
const urlObj = new URL(url)
|
||||
const pathname = urlObj.pathname
|
||||
|
||||
return (
|
||||
/^\/api\/download\/models\/(\d+)$/.test(pathname) ||
|
||||
/^\/api\/v1\/models\/(\d+)$/.test(pathname) ||
|
||||
/^\/api\/v1\/models-versions\/(\d+)$/.test(pathname)
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a Hugging Face download URL to a repository page URL
|
||||
* @param url The download URL to convert
|
||||
* @returns The repository page URL or the original URL if conversion fails
|
||||
* @example
|
||||
* downloadUrlToHfRepoUrl(
|
||||
* 'https://huggingface.co/bfl/FLUX.1/resolve/main/flux1-canny-dev.safetensors?download=true'
|
||||
* ) // https://huggingface.co/bfl/FLUX.1
|
||||
*/
|
||||
export const downloadUrlToHfRepoUrl = (url: string): string => {
|
||||
try {
|
||||
const urlObj = new URL(url)
|
||||
const pathname = urlObj.pathname
|
||||
|
||||
// Use regex to match everything before /resolve/ or /blob/
|
||||
const regex = /^(.*?)(?:\/resolve\/|\/blob\/|$)/
|
||||
const repoPathMatch = regex.exec(pathname)
|
||||
|
||||
// Extract the repository path and remove leading slash if present
|
||||
const repoPath = repoPathMatch?.[1]?.replace(/^\//, '') || ''
|
||||
|
||||
return `https://huggingface.co/${repoPath}`
|
||||
} catch (error) {
|
||||
return url
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts Metronome's integer amount back to a formatted currency string.
|
||||
* For USD, converts from cents to dollars.
|
||||
* For all other currencies (including custom pricing units), returns the amount as is.
|
||||
* This is specific to Metronome's API requirements.
|
||||
*
|
||||
* @param amount - The amount in Metronome's integer format (cents for USD, base units for others)
|
||||
* @param currency - The currency to convert
|
||||
* @returns The formatted amount in currency with 2 decimal places for USD
|
||||
* @example
|
||||
* formatMetronomeCurrency(123, 'usd') // returns "1.23" (cents to USD)
|
||||
* formatMetronomeCurrency(1000, 'jpy') // returns "1000" (yen)
|
||||
*/
|
||||
export function formatMetronomeCurrency(
|
||||
amount: number,
|
||||
currency: string
|
||||
): string {
|
||||
if (currency === 'usd') {
|
||||
return (amount / 100).toFixed(2)
|
||||
}
|
||||
return amount.toString()
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a USD amount to microdollars (1/1,000,000 of a dollar).
|
||||
* This conversion is commonly used in financial systems to avoid floating-point precision issues
|
||||
* by representing monetary values as integers.
|
||||
*
|
||||
* @param usd - The amount in US dollars to convert
|
||||
* @returns The amount in microdollars (multiplied by 1,000,000)
|
||||
* @example
|
||||
* usdToMicros(1.23) // returns 1230000
|
||||
*/
|
||||
export function usdToMicros(usd: number): number {
|
||||
return Math.round(usd * 1_000_000)
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts URLs in a string to HTML links.
|
||||
* @param text - The string to convert
|
||||
* @returns The string with URLs converted to HTML links
|
||||
* @example
|
||||
* linkifyHtml('Visit https://example.com for more info') // returns 'Visit <a href="https://example.com" target="_blank" rel="noopener noreferrer" class="text-primary-400 hover:underline">https://example.com</a> for more info'
|
||||
*/
|
||||
export function linkifyHtml(text: string): string {
|
||||
if (!text) return ''
|
||||
const urlRegex =
|
||||
/(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#/%?=~_|!:,.;]*[-A-Z0-9+&@#/%?=~_|])|(\bwww\.[-A-Z0-9+&@#/%?=~_|!:,.;]*[-A-Z0-9+&@#/%?=~_|])/gi
|
||||
return text.replace(urlRegex, (_match, p1, _p2, p3) => {
|
||||
const url = p1 || p3
|
||||
const href = p3 ? `http://${url}` : url
|
||||
return `<a href="${href}" target="_blank" rel="noopener noreferrer" class="text-primary-400 hover:underline">${url}</a>`
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts newline characters to HTML <br> tags.
|
||||
* @param text - The string to convert
|
||||
* @returns The string with newline characters converted to <br> tags
|
||||
* @example
|
||||
* nl2br('Hello\nWorld') // returns 'Hello<br />World'
|
||||
*/
|
||||
export function nl2br(text: string): string {
|
||||
if (!text) return ''
|
||||
return text.replace(/\n/g, '<br />')
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a version string to an anchor-safe format by replacing dots with dashes.
|
||||
* @param version The version string (e.g., "1.0.0", "2.1.3-beta.1")
|
||||
* @returns The anchor-safe version string (e.g., "v1-0-0", "v2-1-3-beta-1")
|
||||
* @example
|
||||
* formatVersionAnchor("1.0.0") // returns "v1-0-0"
|
||||
* formatVersionAnchor("2.1.3-beta.1") // returns "v2-1-3-beta-1"
|
||||
*/
|
||||
export function formatVersionAnchor(version: string): string {
|
||||
return `v${version.replace(/\./g, '-')}`
|
||||
}
|
||||
|
||||
/**
|
||||
* Supported locale types for the application (from OpenAPI schema)
|
||||
*/
|
||||
type SupportedLocale = NonNullable<
|
||||
operations['getReleaseNotes']['parameters']['query']['locale']
|
||||
>
|
||||
|
||||
/**
|
||||
* Converts a string to a valid locale type with 'en' as default
|
||||
* @param locale - The locale string to validate and convert
|
||||
* @returns A valid SupportedLocale type, defaults to 'en' if invalid
|
||||
* @example
|
||||
* stringToLocale('fr') // returns 'fr'
|
||||
* stringToLocale('invalid') // returns 'en'
|
||||
* stringToLocale('') // returns 'en'
|
||||
*/
|
||||
export function stringToLocale(locale: string): SupportedLocale {
|
||||
const supportedLocales: SupportedLocale[] = [
|
||||
'en',
|
||||
'es',
|
||||
'fr',
|
||||
'ja',
|
||||
'ko',
|
||||
'ru',
|
||||
'zh'
|
||||
]
|
||||
return supportedLocales.includes(locale as SupportedLocale)
|
||||
? (locale as SupportedLocale)
|
||||
: 'en'
|
||||
}
|
||||
|
||||
export function formatDuration(milliseconds: number): string {
|
||||
if (!milliseconds || milliseconds < 0) return '0s'
|
||||
|
||||
const totalSeconds = Math.floor(milliseconds / 1000)
|
||||
const hours = Math.floor(totalSeconds / 3600)
|
||||
const minutes = Math.floor((totalSeconds % 3600) / 60)
|
||||
const remainingSeconds = Math.floor(totalSeconds % 60)
|
||||
|
||||
const parts: string[] = []
|
||||
|
||||
if (hours > 0) {
|
||||
parts.push(`${hours}h`)
|
||||
}
|
||||
if (minutes > 0) {
|
||||
parts.push(`${minutes}m`)
|
||||
}
|
||||
if (remainingSeconds > 0 || parts.length === 0) {
|
||||
parts.push(`${remainingSeconds}s`)
|
||||
}
|
||||
|
||||
return parts.join(' ')
|
||||
}
|
||||
|
||||
const IMAGE_EXTENSIONS = ['png', 'jpg', 'jpeg', 'gif', 'webp', 'bmp'] as const
|
||||
const VIDEO_EXTENSIONS = ['mp4', 'webm', 'mov', 'avi'] as const
|
||||
const AUDIO_EXTENSIONS = ['mp3', 'wav', 'ogg', 'flac'] as const
|
||||
const THREE_D_EXTENSIONS = ['obj', 'fbx', 'gltf', 'glb'] as const
|
||||
|
||||
const MEDIA_TYPES = ['image', 'video', 'audio', '3D'] as const
|
||||
type MediaType = (typeof MEDIA_TYPES)[number]
|
||||
|
||||
// Type guard helper for checking array membership
|
||||
type ImageExtension = (typeof IMAGE_EXTENSIONS)[number]
|
||||
type VideoExtension = (typeof VIDEO_EXTENSIONS)[number]
|
||||
type AudioExtension = (typeof AUDIO_EXTENSIONS)[number]
|
||||
type ThreeDExtension = (typeof THREE_D_EXTENSIONS)[number]
|
||||
|
||||
/**
|
||||
* Truncates a filename while preserving the extension
|
||||
* @param filename The filename to truncate
|
||||
* @param maxLength Maximum length for the filename without extension
|
||||
* @returns Truncated filename with extension preserved
|
||||
*/
|
||||
export function truncateFilename(
|
||||
filename: string,
|
||||
maxLength: number = 20
|
||||
): string {
|
||||
if (!filename || filename.length <= maxLength) {
|
||||
return filename
|
||||
}
|
||||
|
||||
const lastDotIndex = filename.lastIndexOf('.')
|
||||
const nameWithoutExt =
|
||||
lastDotIndex > -1 ? filename.substring(0, lastDotIndex) : filename
|
||||
const extension = lastDotIndex > -1 ? filename.substring(lastDotIndex) : ''
|
||||
|
||||
// If the name without extension is short enough, return as is
|
||||
if (nameWithoutExt.length <= maxLength) {
|
||||
return filename
|
||||
}
|
||||
|
||||
// Calculate how to split the truncation
|
||||
const halfLength = Math.floor((maxLength - 3) / 2) // -3 for '...'
|
||||
const start = nameWithoutExt.substring(0, halfLength)
|
||||
const end = nameWithoutExt.substring(nameWithoutExt.length - halfLength)
|
||||
|
||||
return `${start}...${end}${extension}`
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines the media type from a filename's extension (singular form)
|
||||
* @param filename The filename to analyze
|
||||
* @returns The media type: 'image', 'video', 'audio', or '3D'
|
||||
*/
|
||||
export function getMediaTypeFromFilename(filename: string): MediaType {
|
||||
if (!filename) return 'image'
|
||||
const ext = filename.split('.').pop()?.toLowerCase()
|
||||
if (!ext) return 'image'
|
||||
|
||||
// Type-safe array includes check using type assertion
|
||||
if (IMAGE_EXTENSIONS.includes(ext as ImageExtension)) return 'image'
|
||||
if (VIDEO_EXTENSIONS.includes(ext as VideoExtension)) return 'video'
|
||||
if (AUDIO_EXTENSIONS.includes(ext as AudioExtension)) return 'audio'
|
||||
if (THREE_D_EXTENSIONS.includes(ext as ThreeDExtension)) return '3D'
|
||||
|
||||
return 'image'
|
||||
}
|
||||
+83
@@ -0,0 +1,83 @@
|
||||
/*
|
||||
Preview Any - original implement from
|
||||
https://github.com/rgthree/rgthree-comfy/blob/main/py/display_any.py
|
||||
upstream requested in https://github.com/Kosinkadink/rfcs/blob/main/rfcs/0000-corenodes.md#preview-nodes
|
||||
*/
|
||||
import { app } from '@/scripts/app'
|
||||
import { type DOMWidget } from '@/scripts/domWidget'
|
||||
import { ComfyWidgets } from '@/scripts/widgets'
|
||||
import { useExtensionService } from '@/services/extensionService'
|
||||
|
||||
useExtensionService().registerExtension({
|
||||
name: 'Comfy.PreviewAny',
|
||||
async beforeRegisterNodeDef(nodeType, nodeData) {
|
||||
if (nodeData.name === 'PreviewAny') {
|
||||
const onNodeCreated = nodeType.prototype.onNodeCreated
|
||||
|
||||
nodeType.prototype.onNodeCreated = function () {
|
||||
onNodeCreated ? onNodeCreated.apply(this, []) : undefined
|
||||
|
||||
const showValueWidget = ComfyWidgets['MARKDOWN'](
|
||||
this,
|
||||
'preview',
|
||||
['MARKDOWN', {}],
|
||||
app
|
||||
).widget as DOMWidget<HTMLTextAreaElement, string>
|
||||
|
||||
const showValueWidgetPlain = ComfyWidgets['STRING'](
|
||||
this,
|
||||
'preview',
|
||||
['STRING', { multiline: true }],
|
||||
app
|
||||
).widget as DOMWidget<HTMLTextAreaElement, string>
|
||||
|
||||
const showAsPlaintextWidget = ComfyWidgets['BOOLEAN'](
|
||||
this,
|
||||
'previewMode',
|
||||
[
|
||||
'BOOLEAN',
|
||||
{ label_on: 'Markdown', label_off: 'Plaintext', default: false }
|
||||
],
|
||||
app
|
||||
)
|
||||
|
||||
showAsPlaintextWidget.widget.callback = (value: boolean) => {
|
||||
showValueWidget.hidden = !value
|
||||
showValueWidget.options.hidden = !value
|
||||
showValueWidgetPlain.hidden = value
|
||||
showValueWidgetPlain.options.hidden = value
|
||||
}
|
||||
|
||||
showValueWidget.hidden = true
|
||||
showValueWidget.options.hidden = true
|
||||
showValueWidget.options.read_only = true
|
||||
showValueWidget.element.readOnly = true
|
||||
showValueWidget.element.disabled = true
|
||||
showValueWidget.serialize = false
|
||||
|
||||
showValueWidgetPlain.hidden = false
|
||||
showValueWidgetPlain.options.hidden = false
|
||||
showValueWidgetPlain.options.read_only = true
|
||||
showValueWidgetPlain.element.readOnly = true
|
||||
showValueWidgetPlain.element.disabled = true
|
||||
showValueWidgetPlain.serialize = false
|
||||
}
|
||||
|
||||
const onExecuted = nodeType.prototype.onExecuted
|
||||
|
||||
nodeType.prototype.onExecuted = function (message) {
|
||||
onExecuted === null || onExecuted === void 0
|
||||
? void 0
|
||||
: onExecuted.apply(this, [message])
|
||||
|
||||
const previewWidgets =
|
||||
this.widgets?.filter((w) => w.name === 'preview') ?? []
|
||||
|
||||
for (const previewWidget of previewWidgets) {
|
||||
const text = message.text ?? ''
|
||||
previewWidget.value = Array.isArray(text) ? (text[0] ?? '') : text
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
+411
@@ -0,0 +1,411 @@
|
||||
import { MediaRecorder as ExtendableMediaRecorder } from 'extendable-media-recorder'
|
||||
|
||||
import { useChainCallback } from '@/composables/functional/useChainCallback'
|
||||
import { useNodeDragAndDrop } from '@/composables/node/useNodeDragAndDrop'
|
||||
import { useNodeFileInput } from '@/composables/node/useNodeFileInput'
|
||||
import { useNodePaste } from '@/composables/node/useNodePaste'
|
||||
import { t } from '@/i18n'
|
||||
import type { LGraphNode } from '@/lib/litegraph/src/litegraph'
|
||||
import type {
|
||||
IBaseWidget,
|
||||
IStringWidget
|
||||
} from '@/lib/litegraph/src/types/widgets'
|
||||
import { useToastStore } from '@/platform/updates/common/toastStore'
|
||||
import {
|
||||
getResourceURL,
|
||||
splitFilePath
|
||||
} from '@/renderer/extensions/vueNodes/widgets/utils/audioUtils'
|
||||
import type { ComfyNodeDef } from '@/schemas/nodeDefSchema'
|
||||
import type { DOMWidget } from '@/scripts/domWidget'
|
||||
import { useAudioService } from '@/services/audioService'
|
||||
import { type NodeLocatorId } from '@/types'
|
||||
import { getNodeByLocatorId } from '@/utils/graphTraversalUtil'
|
||||
|
||||
import { api } from '../../scripts/api'
|
||||
import { app } from '../../scripts/app'
|
||||
|
||||
async function uploadFile(
|
||||
audioWidget: IStringWidget,
|
||||
audioUIWidget: DOMWidget<HTMLAudioElement, string>,
|
||||
file: File,
|
||||
updateNode: boolean,
|
||||
pasted: boolean = false
|
||||
) {
|
||||
try {
|
||||
// Wrap file in formdata so it includes filename
|
||||
const body = new FormData()
|
||||
body.append('image', file)
|
||||
if (pasted) body.append('subfolder', 'pasted')
|
||||
const resp = await api.fetchApi('/upload/image', {
|
||||
method: 'POST',
|
||||
body
|
||||
})
|
||||
|
||||
if (resp.status === 200) {
|
||||
const data = await resp.json()
|
||||
// Add the file to the dropdown list and update the widget value
|
||||
let path = data.name
|
||||
if (data.subfolder) path = data.subfolder + '/' + path
|
||||
|
||||
// @ts-expect-error fixme ts strict error
|
||||
if (!audioWidget.options.values.includes(path)) {
|
||||
// @ts-expect-error fixme ts strict error
|
||||
audioWidget.options.values.push(path)
|
||||
}
|
||||
|
||||
if (updateNode) {
|
||||
audioUIWidget.element.src = api.apiURL(
|
||||
getResourceURL(...splitFilePath(path))
|
||||
)
|
||||
audioWidget.value = path
|
||||
|
||||
// Manually trigger the callback to update VueNodes
|
||||
audioWidget.callback?.(path)
|
||||
}
|
||||
} else {
|
||||
useToastStore().addAlert(resp.status + ' - ' + resp.statusText)
|
||||
}
|
||||
} catch (error) {
|
||||
// @ts-expect-error fixme ts strict error
|
||||
useToastStore().addAlert(error)
|
||||
}
|
||||
}
|
||||
|
||||
// AudioWidget MUST be registered first, as AUDIOUPLOAD depends on AUDIO_UI to be
|
||||
// present.
|
||||
app.registerExtension({
|
||||
name: 'Comfy.AudioWidget',
|
||||
async beforeRegisterNodeDef(nodeType, nodeData) {
|
||||
if (
|
||||
[
|
||||
'LoadAudio',
|
||||
'SaveAudio',
|
||||
'PreviewAudio',
|
||||
'SaveAudioMP3',
|
||||
'SaveAudioOpus'
|
||||
].includes(
|
||||
// @ts-expect-error fixme ts strict error
|
||||
nodeType.prototype.comfyClass
|
||||
)
|
||||
) {
|
||||
// @ts-expect-error fixme ts strict error
|
||||
nodeData.input.required.audioUI = ['AUDIO_UI', {}]
|
||||
}
|
||||
},
|
||||
getCustomWidgets() {
|
||||
return {
|
||||
AUDIO_UI(node: LGraphNode, inputName: string) {
|
||||
const audio = document.createElement('audio')
|
||||
audio.controls = true
|
||||
audio.classList.add('comfy-audio')
|
||||
audio.setAttribute('name', 'media')
|
||||
|
||||
const audioUIWidget: DOMWidget<HTMLAudioElement, string> =
|
||||
node.addDOMWidget(inputName, /* name=*/ 'audioUI', audio)
|
||||
audioUIWidget.serialize = false
|
||||
const { nodeData } = node.constructor
|
||||
if (nodeData == null) throw new TypeError('nodeData is null')
|
||||
|
||||
const isOutputNode = nodeData.output_node
|
||||
if (isOutputNode) {
|
||||
// Hide the audio widget when there is no audio initially.
|
||||
audioUIWidget.element.classList.add('empty-audio-widget')
|
||||
// Populate the audio widget UI on node execution.
|
||||
const onExecuted = node.onExecuted
|
||||
node.onExecuted = function (message: any) {
|
||||
// @ts-expect-error fixme ts strict error
|
||||
onExecuted?.apply(this, arguments)
|
||||
const audios = message.audio
|
||||
if (!audios) return
|
||||
const audio = audios[0]
|
||||
audioUIWidget.element.src = api.apiURL(
|
||||
getResourceURL(audio.subfolder, audio.filename, audio.type)
|
||||
)
|
||||
audioUIWidget.element.classList.remove('empty-audio-widget')
|
||||
}
|
||||
}
|
||||
|
||||
audioUIWidget.onRemove = useChainCallback(
|
||||
audioUIWidget.onRemove,
|
||||
() => {
|
||||
if (!audioUIWidget.element) return
|
||||
audioUIWidget.element.pause()
|
||||
audioUIWidget.element.src = ''
|
||||
audioUIWidget.element.remove()
|
||||
}
|
||||
)
|
||||
|
||||
return { widget: audioUIWidget }
|
||||
}
|
||||
}
|
||||
},
|
||||
onNodeOutputsUpdated(nodeOutputs: Record<NodeLocatorId, any>) {
|
||||
for (const [nodeLocatorId, output] of Object.entries(nodeOutputs)) {
|
||||
if ('audio' in output) {
|
||||
const node = getNodeByLocatorId(app.rootGraph, nodeLocatorId)
|
||||
if (!node) continue
|
||||
|
||||
// @ts-expect-error fixme ts strict error
|
||||
const audioUIWidget = node.widgets.find(
|
||||
(w) => w.name === 'audioUI'
|
||||
) as unknown as DOMWidget<HTMLAudioElement, string>
|
||||
const audio = output.audio[0]
|
||||
audioUIWidget.element.src = api.apiURL(
|
||||
getResourceURL(audio.subfolder, audio.filename, audio.type)
|
||||
)
|
||||
audioUIWidget.element.classList.remove('empty-audio-widget')
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
app.registerExtension({
|
||||
name: 'Comfy.UploadAudio',
|
||||
async beforeRegisterNodeDef(_nodeType, nodeData: ComfyNodeDef) {
|
||||
if (nodeData?.input?.required?.audio?.[1]?.audio_upload === true) {
|
||||
nodeData.input.required.upload = ['AUDIOUPLOAD', {}]
|
||||
}
|
||||
},
|
||||
getCustomWidgets() {
|
||||
return {
|
||||
AUDIOUPLOAD(node, inputName: string) {
|
||||
// The widget that allows user to select file.
|
||||
// @ts-expect-error fixme ts strict error
|
||||
const audioWidget = node.widgets.find(
|
||||
(w) => w.name === 'audio'
|
||||
) as IStringWidget
|
||||
// @ts-expect-error fixme ts strict error
|
||||
const audioUIWidget = node.widgets.find(
|
||||
(w) => w.name === 'audioUI'
|
||||
) as unknown as DOMWidget<HTMLAudioElement, string>
|
||||
audioUIWidget.options.canvasOnly = true
|
||||
|
||||
const onAudioWidgetUpdate = () => {
|
||||
if (typeof audioWidget.value !== 'string') return
|
||||
audioUIWidget.element.src = api.apiURL(
|
||||
getResourceURL(...splitFilePath(audioWidget.value))
|
||||
)
|
||||
}
|
||||
// Initially load default audio file to audioUIWidget.
|
||||
if (audioWidget.value) {
|
||||
onAudioWidgetUpdate()
|
||||
}
|
||||
audioWidget.callback = onAudioWidgetUpdate
|
||||
|
||||
// Load saved audio file widget values if restoring from workflow
|
||||
const onGraphConfigured = node.onGraphConfigured
|
||||
node.onGraphConfigured = function () {
|
||||
// @ts-expect-error fixme ts strict error
|
||||
onGraphConfigured?.apply(this, arguments)
|
||||
if (audioWidget.value) {
|
||||
onAudioWidgetUpdate()
|
||||
}
|
||||
}
|
||||
|
||||
const handleUpload = async (files: File[]) => {
|
||||
if (files?.length) {
|
||||
uploadFile(audioWidget, audioUIWidget, files[0], true)
|
||||
}
|
||||
return files
|
||||
}
|
||||
|
||||
const isAudioFile = (file: File) => file.type.startsWith('audio/')
|
||||
|
||||
const { openFileSelection } = useNodeFileInput(node, {
|
||||
accept: 'audio/*',
|
||||
onSelect: handleUpload
|
||||
})
|
||||
|
||||
// The widget to pop up the upload dialog.
|
||||
const uploadWidget = node.addWidget(
|
||||
'button',
|
||||
inputName,
|
||||
'',
|
||||
openFileSelection,
|
||||
{ serialize: false, canvasOnly: true }
|
||||
)
|
||||
uploadWidget.label = t('g.choose_file_to_upload')
|
||||
|
||||
useNodeDragAndDrop(node, {
|
||||
fileFilter: isAudioFile,
|
||||
onDrop: handleUpload
|
||||
})
|
||||
|
||||
useNodePaste(node, {
|
||||
fileFilter: isAudioFile,
|
||||
onPaste: handleUpload
|
||||
})
|
||||
|
||||
node.previewMediaType = 'audio'
|
||||
|
||||
return { widget: uploadWidget }
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
app.registerExtension({
|
||||
name: 'Comfy.RecordAudio',
|
||||
|
||||
getCustomWidgets() {
|
||||
return {
|
||||
AUDIO_RECORD(node, inputName: string) {
|
||||
const audio = document.createElement('audio')
|
||||
audio.controls = true
|
||||
audio.classList.add('comfy-audio')
|
||||
audio.setAttribute('name', 'media')
|
||||
const audioUIWidget: DOMWidget<HTMLAudioElement, string> =
|
||||
node.addDOMWidget(inputName, /* name=*/ 'audioUI', audio)
|
||||
audioUIWidget.options.canvasOnly = false
|
||||
|
||||
let mediaRecorder: MediaRecorder | null = null
|
||||
let isRecording = false
|
||||
let audioChunks: Blob[] = []
|
||||
let currentStream: MediaStream | null = null
|
||||
let recordWidget: IBaseWidget | null = null
|
||||
|
||||
let stopPromise: Promise<void> | null = null
|
||||
let stopResolve: (() => void) | null = null
|
||||
|
||||
audioUIWidget.serializeValue = async () => {
|
||||
if (isRecording && mediaRecorder) {
|
||||
stopPromise = new Promise((resolve) => {
|
||||
stopResolve = resolve
|
||||
})
|
||||
|
||||
mediaRecorder.stop()
|
||||
|
||||
await stopPromise
|
||||
}
|
||||
|
||||
const audioSrc = audioUIWidget.element.src
|
||||
|
||||
if (!audioSrc) {
|
||||
useToastStore().addAlert(t('g.noAudioRecorded'))
|
||||
return ''
|
||||
}
|
||||
|
||||
const blob = await fetch(audioSrc).then((r) => r.blob())
|
||||
|
||||
return await useAudioService().convertBlobToFileAndSubmit(blob)
|
||||
}
|
||||
|
||||
recordWidget = node.addWidget(
|
||||
'button',
|
||||
inputName,
|
||||
'',
|
||||
async () => {
|
||||
if (!isRecording) {
|
||||
try {
|
||||
currentStream = await navigator.mediaDevices.getUserMedia({
|
||||
audio: true
|
||||
})
|
||||
|
||||
mediaRecorder = new ExtendableMediaRecorder(currentStream, {
|
||||
mimeType: 'audio/wav'
|
||||
}) as unknown as MediaRecorder
|
||||
|
||||
audioChunks = []
|
||||
|
||||
mediaRecorder.ondataavailable = (event) => {
|
||||
audioChunks.push(event.data)
|
||||
}
|
||||
|
||||
mediaRecorder.onstop = async () => {
|
||||
const audioBlob = new Blob(audioChunks, { type: 'audio/wav' })
|
||||
|
||||
useAudioService().stopAllTracks(currentStream)
|
||||
|
||||
if (
|
||||
audioUIWidget.element.src &&
|
||||
audioUIWidget.element.src.startsWith('blob:')
|
||||
) {
|
||||
URL.revokeObjectURL(audioUIWidget.element.src)
|
||||
}
|
||||
|
||||
audioUIWidget.element.src = URL.createObjectURL(audioBlob)
|
||||
|
||||
isRecording = false
|
||||
|
||||
if (recordWidget) {
|
||||
recordWidget.label = t('g.startRecording')
|
||||
}
|
||||
|
||||
if (stopResolve) {
|
||||
stopResolve()
|
||||
stopResolve = null
|
||||
stopPromise = null
|
||||
}
|
||||
}
|
||||
|
||||
mediaRecorder.onerror = (event) => {
|
||||
console.error('MediaRecorder error:', event)
|
||||
useAudioService().stopAllTracks(currentStream)
|
||||
isRecording = false
|
||||
|
||||
if (recordWidget) {
|
||||
recordWidget.label = t('g.startRecording')
|
||||
}
|
||||
|
||||
if (stopResolve) {
|
||||
stopResolve()
|
||||
stopResolve = null
|
||||
stopPromise = null
|
||||
}
|
||||
}
|
||||
|
||||
mediaRecorder.start()
|
||||
isRecording = true
|
||||
if (recordWidget) {
|
||||
recordWidget.label = t('g.stopRecording')
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('Error accessing microphone:', err)
|
||||
useToastStore().addAlert(t('g.micPermissionDenied'))
|
||||
|
||||
if (mediaRecorder) {
|
||||
try {
|
||||
mediaRecorder.stop()
|
||||
} catch {}
|
||||
}
|
||||
useAudioService().stopAllTracks(currentStream)
|
||||
currentStream = null
|
||||
isRecording = false
|
||||
if (recordWidget) {
|
||||
recordWidget.label = t('g.startRecording')
|
||||
}
|
||||
}
|
||||
} else if (mediaRecorder && isRecording) {
|
||||
mediaRecorder.stop()
|
||||
}
|
||||
},
|
||||
{ serialize: false, canvasOnly: false }
|
||||
)
|
||||
|
||||
recordWidget.label = t('g.startRecording')
|
||||
// Override the type for Vue rendering while keeping 'button' for LiteGraph
|
||||
recordWidget.type = 'audiorecord'
|
||||
|
||||
const originalOnRemoved = node.onRemoved
|
||||
node.onRemoved = function () {
|
||||
if (isRecording && mediaRecorder) {
|
||||
mediaRecorder.stop()
|
||||
}
|
||||
useAudioService().stopAllTracks(currentStream)
|
||||
if (audioUIWidget.element.src?.startsWith('blob:')) {
|
||||
URL.revokeObjectURL(audioUIWidget.element.src)
|
||||
}
|
||||
originalOnRemoved?.call(this)
|
||||
}
|
||||
|
||||
return { widget: recordWidget }
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
async nodeCreated(node) {
|
||||
if (node.constructor.comfyClass !== 'RecordAudio') return
|
||||
|
||||
await useAudioService().registerWavEncoder()
|
||||
}
|
||||
})
|
||||
+4198
File diff suppressed because it is too large
Load Diff
+349
@@ -0,0 +1,349 @@
|
||||
import { drawTextInArea } from '@/lib/litegraph/src/draw'
|
||||
import { Rectangle } from '@/lib/litegraph/src/infrastructure/Rectangle'
|
||||
import type { Point } from '@/lib/litegraph/src/interfaces'
|
||||
import type {
|
||||
CanvasPointer,
|
||||
LGraphCanvas,
|
||||
LGraphNode,
|
||||
Size
|
||||
} from '@/lib/litegraph/src/litegraph'
|
||||
import { LiteGraph } from '@/lib/litegraph/src/litegraph'
|
||||
import type { CanvasPointerEvent } from '@/lib/litegraph/src/types/events'
|
||||
import type { IBaseWidget } from '@/lib/litegraph/src/types/widgets'
|
||||
|
||||
export interface DrawWidgetOptions {
|
||||
/** The width of the node where this widget will be displayed. */
|
||||
width: number
|
||||
/** Synonym for "low quality". */
|
||||
showText?: boolean
|
||||
}
|
||||
|
||||
interface DrawTruncatingTextOptions extends DrawWidgetOptions {
|
||||
/** The canvas context to draw the text on. */
|
||||
ctx: CanvasRenderingContext2D
|
||||
/** The amount of padding to add to the left of the text. */
|
||||
leftPadding?: number
|
||||
/** The amount of padding to add to the right of the text. */
|
||||
rightPadding?: number
|
||||
}
|
||||
|
||||
export interface WidgetEventOptions {
|
||||
e: CanvasPointerEvent
|
||||
node: LGraphNode
|
||||
canvas: LGraphCanvas
|
||||
}
|
||||
|
||||
export abstract class BaseWidget<
|
||||
TWidget extends IBaseWidget = IBaseWidget
|
||||
> implements IBaseWidget {
|
||||
/** From node edge to widget edge */
|
||||
static margin = 15
|
||||
/** From widget edge to tip of arrow button */
|
||||
static arrowMargin = 6
|
||||
/** Arrow button width */
|
||||
static arrowWidth = 10
|
||||
/** Absolute minimum display width of widget values */
|
||||
static minValueWidth = 42
|
||||
/** Minimum gap between label and value */
|
||||
static labelValueGap = 5
|
||||
|
||||
declare computedHeight?: number
|
||||
declare serialize?: boolean
|
||||
computeLayoutSize?(node: LGraphNode): {
|
||||
minHeight: number
|
||||
maxHeight?: number
|
||||
minWidth: number
|
||||
maxWidth?: number
|
||||
}
|
||||
|
||||
#node: LGraphNode
|
||||
/** The node that this widget belongs to. */
|
||||
get node() {
|
||||
return this.#node
|
||||
}
|
||||
|
||||
linkedWidgets?: IBaseWidget[]
|
||||
name: string
|
||||
options: TWidget['options']
|
||||
label?: string
|
||||
type: TWidget['type']
|
||||
y: number = 0
|
||||
last_y?: number
|
||||
width?: number
|
||||
disabled?: boolean
|
||||
computedDisabled?: boolean
|
||||
hidden?: boolean
|
||||
advanced?: boolean
|
||||
promoted?: boolean
|
||||
tooltip?: string
|
||||
element?: HTMLElement
|
||||
callback?(
|
||||
value: TWidget['value'],
|
||||
canvas?: LGraphCanvas,
|
||||
node?: LGraphNode,
|
||||
pos?: Point,
|
||||
e?: CanvasPointerEvent
|
||||
): void
|
||||
mouse?(
|
||||
event: CanvasPointerEvent,
|
||||
pointerOffset: Point,
|
||||
node: LGraphNode
|
||||
): boolean
|
||||
computeSize?(width?: number): Size
|
||||
onPointerDown?(
|
||||
pointer: CanvasPointer,
|
||||
node: LGraphNode,
|
||||
canvas: LGraphCanvas
|
||||
): boolean
|
||||
|
||||
#value?: TWidget['value']
|
||||
get value(): TWidget['value'] {
|
||||
return this.#value
|
||||
}
|
||||
|
||||
set value(value: TWidget['value']) {
|
||||
this.#value = value
|
||||
}
|
||||
|
||||
constructor(widget: TWidget & { node: LGraphNode })
|
||||
constructor(widget: TWidget, node: LGraphNode)
|
||||
constructor(widget: TWidget & { node: LGraphNode }, node?: LGraphNode) {
|
||||
// Private fields
|
||||
this.#node = node ?? widget.node
|
||||
|
||||
// The set and get functions for DOM widget values are hacked on to the options object;
|
||||
// attempting to set value before options will throw.
|
||||
// https://github.com/Comfy-Org/ComfyUI_frontend/blob/df86da3d672628a452baed3df3347a52c0c8d378/src/scripts/domWidget.ts#L125
|
||||
this.name = widget.name
|
||||
this.options = widget.options
|
||||
this.type = widget.type
|
||||
|
||||
// `node` has no setter - Object.assign will throw.
|
||||
// TODO: Resolve this workaround. Ref: https://github.com/Comfy-Org/litegraph.js/issues/1022
|
||||
const {
|
||||
node: _,
|
||||
// @ts-expect-error Prevent naming conflicts with custom nodes.
|
||||
outline_color,
|
||||
// @ts-expect-error Prevent naming conflicts with custom nodes.
|
||||
background_color,
|
||||
// @ts-expect-error Prevent naming conflicts with custom nodes.
|
||||
height,
|
||||
// @ts-expect-error Prevent naming conflicts with custom nodes.
|
||||
text_color,
|
||||
// @ts-expect-error Prevent naming conflicts with custom nodes.
|
||||
secondary_text_color,
|
||||
// @ts-expect-error Prevent naming conflicts with custom nodes.
|
||||
disabledTextColor,
|
||||
// @ts-expect-error Prevent naming conflicts with custom nodes.
|
||||
displayName,
|
||||
// @ts-expect-error Prevent naming conflicts with custom nodes.
|
||||
displayValue,
|
||||
// @ts-expect-error Prevent naming conflicts with custom nodes.
|
||||
labelBaseline,
|
||||
promoted,
|
||||
...safeValues
|
||||
} = widget
|
||||
|
||||
Object.assign(this, safeValues)
|
||||
}
|
||||
|
||||
get outline_color() {
|
||||
if (this.promoted) return LiteGraph.WIDGET_PROMOTED_OUTLINE_COLOR
|
||||
return this.advanced
|
||||
? LiteGraph.WIDGET_ADVANCED_OUTLINE_COLOR
|
||||
: LiteGraph.WIDGET_OUTLINE_COLOR
|
||||
}
|
||||
|
||||
get background_color() {
|
||||
return LiteGraph.WIDGET_BGCOLOR
|
||||
}
|
||||
|
||||
get height() {
|
||||
return LiteGraph.NODE_WIDGET_HEIGHT
|
||||
}
|
||||
|
||||
get text_color() {
|
||||
return LiteGraph.WIDGET_TEXT_COLOR
|
||||
}
|
||||
|
||||
get secondary_text_color() {
|
||||
return LiteGraph.WIDGET_SECONDARY_TEXT_COLOR
|
||||
}
|
||||
|
||||
get disabledTextColor() {
|
||||
return LiteGraph.WIDGET_DISABLED_TEXT_COLOR
|
||||
}
|
||||
|
||||
get displayName() {
|
||||
return this.label || this.name
|
||||
}
|
||||
|
||||
// TODO: Resolve this workaround. Ref: https://github.com/Comfy-Org/litegraph.js/issues/1022
|
||||
get _displayValue(): string {
|
||||
return this.computedDisabled ? '' : String(this.value)
|
||||
}
|
||||
|
||||
get labelBaseline() {
|
||||
return this.y + this.height * 0.7
|
||||
}
|
||||
|
||||
/**
|
||||
* Draws the widget
|
||||
* @param ctx The canvas context
|
||||
* @param options The options for drawing the widget
|
||||
* @remarks Not naming this `draw` as `draw` conflicts with the `draw` method in
|
||||
* custom widgets.
|
||||
*/
|
||||
abstract drawWidget(
|
||||
ctx: CanvasRenderingContext2D,
|
||||
options: DrawWidgetOptions
|
||||
): void
|
||||
|
||||
/**
|
||||
* Draws the standard widget shape - elongated capsule. The path of the widget shape is not
|
||||
* cleared, and may be used for further drawing.
|
||||
* @param ctx The canvas context
|
||||
* @param options The options for drawing the widget
|
||||
* @remarks Leaves {@link ctx} dirty.
|
||||
*/
|
||||
protected drawWidgetShape(
|
||||
ctx: CanvasRenderingContext2D,
|
||||
{ width, showText }: DrawWidgetOptions
|
||||
): void {
|
||||
const { height, y } = this
|
||||
const { margin } = BaseWidget
|
||||
|
||||
ctx.textAlign = 'left'
|
||||
ctx.strokeStyle = this.outline_color
|
||||
ctx.fillStyle = this.background_color
|
||||
ctx.beginPath()
|
||||
|
||||
if (showText) {
|
||||
ctx.roundRect(margin, y, width - margin * 2, height, [height * 0.5])
|
||||
} else {
|
||||
ctx.rect(margin, y, width - margin * 2, height)
|
||||
}
|
||||
ctx.fill()
|
||||
if (showText && !this.computedDisabled) ctx.stroke()
|
||||
}
|
||||
|
||||
/**
|
||||
* A shared routine for drawing a label and value as text, truncated
|
||||
* if they exceed the available width.
|
||||
*/
|
||||
protected drawTruncatingText({
|
||||
ctx,
|
||||
width,
|
||||
leftPadding = 5,
|
||||
rightPadding = 20
|
||||
}: DrawTruncatingTextOptions): void {
|
||||
const { height, y } = this
|
||||
const { margin } = BaseWidget
|
||||
|
||||
// Measure label and value
|
||||
const { displayName, _displayValue } = this
|
||||
const labelWidth = ctx.measureText(displayName).width
|
||||
const valueWidth = ctx.measureText(_displayValue).width
|
||||
|
||||
const gap = BaseWidget.labelValueGap
|
||||
const x = margin * 2 + leftPadding
|
||||
|
||||
const totalWidth = width - x - 2 * margin - rightPadding
|
||||
const requiredWidth = labelWidth + gap + valueWidth
|
||||
|
||||
const area = new Rectangle(x, y, totalWidth, height * 0.7)
|
||||
|
||||
ctx.fillStyle = this.secondary_text_color
|
||||
|
||||
if (requiredWidth <= totalWidth) {
|
||||
// Draw label & value normally
|
||||
drawTextInArea({ ctx, text: displayName, area, align: 'left' })
|
||||
} else if (LiteGraph.truncateWidgetTextEvenly) {
|
||||
// Label + value will not fit - scale evenly to fit
|
||||
const scale = (totalWidth - gap) / (requiredWidth - gap)
|
||||
area.width = labelWidth * scale
|
||||
|
||||
drawTextInArea({ ctx, text: displayName, area, align: 'left' })
|
||||
|
||||
// Move the area to the right to render the value
|
||||
area.right = x + totalWidth
|
||||
area.setWidthRightAnchored(valueWidth * scale)
|
||||
} else if (LiteGraph.truncateWidgetValuesFirst) {
|
||||
// Label + value will not fit - use legacy scaling of value first
|
||||
const cappedLabelWidth = Math.min(labelWidth, totalWidth)
|
||||
|
||||
area.width = cappedLabelWidth
|
||||
drawTextInArea({ ctx, text: displayName, area, align: 'left' })
|
||||
|
||||
area.right = x + totalWidth
|
||||
area.setWidthRightAnchored(
|
||||
Math.max(totalWidth - gap - cappedLabelWidth, 0)
|
||||
)
|
||||
} else {
|
||||
// Label + value will not fit - scale label first
|
||||
const cappedValueWidth = Math.min(valueWidth, totalWidth)
|
||||
|
||||
area.width = Math.max(totalWidth - gap - cappedValueWidth, 0)
|
||||
drawTextInArea({ ctx, text: displayName, area, align: 'left' })
|
||||
|
||||
area.right = x + totalWidth
|
||||
area.setWidthRightAnchored(cappedValueWidth)
|
||||
}
|
||||
ctx.fillStyle = this.text_color
|
||||
drawTextInArea({ ctx, text: _displayValue, area, align: 'right' })
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles the click event for the widget
|
||||
* @param options The options for handling the click event
|
||||
*/
|
||||
abstract onClick(options: WidgetEventOptions): void
|
||||
|
||||
/**
|
||||
* Handles the drag event for the widget
|
||||
* @param options The options for handling the drag event
|
||||
*/
|
||||
onDrag?(options: WidgetEventOptions): void
|
||||
|
||||
/**
|
||||
* Sets the value of the widget
|
||||
* @param value The value to set
|
||||
* @param options The options for setting the value
|
||||
*/
|
||||
setValue(
|
||||
value: TWidget['value'],
|
||||
{ e, node, canvas }: WidgetEventOptions
|
||||
): void {
|
||||
const oldValue = this.value
|
||||
if (value === this.value) return
|
||||
|
||||
const v = this.type === 'number' ? Number(value) : value
|
||||
this.value = v
|
||||
if (
|
||||
this.options?.property &&
|
||||
node.properties[this.options.property] !== undefined
|
||||
) {
|
||||
node.setProperty(this.options.property, v)
|
||||
}
|
||||
const pos = canvas.graph_mouse
|
||||
this.callback?.(this.value, canvas, node, pos, e)
|
||||
|
||||
node.onWidgetChanged?.(this.name ?? '', v, oldValue, this)
|
||||
if (node.graph) node.graph._version++
|
||||
}
|
||||
|
||||
/**
|
||||
* Clones the widget.
|
||||
* @param node The node that will own the cloned widget.
|
||||
* @returns A new widget with the same properties as the original
|
||||
* @remarks Subclasses with custom constructors must override this method.
|
||||
*
|
||||
* Correctly and safely typing this is currently not possible (practical?) in TypeScript 5.8.
|
||||
*/
|
||||
createCopyForNode(node: LGraphNode): this {
|
||||
// @ts-expect-error - Constructor type casting for widget cloning
|
||||
const cloned: this = new (this.constructor as typeof this)(this, node)
|
||||
cloned.value = this.value
|
||||
return cloned
|
||||
}
|
||||
}
|
||||
+40
@@ -0,0 +1,40 @@
|
||||
import type { LGraphNode } from '@/lib/litegraph/src/LGraphNode'
|
||||
import { LiteGraph } from '@/lib/litegraph/src/litegraph'
|
||||
import type { IBaseWidget } from '@/lib/litegraph/src/types/widgets'
|
||||
|
||||
import { BaseWidget } from './BaseWidget'
|
||||
import type { DrawWidgetOptions } from './BaseWidget'
|
||||
|
||||
/**
|
||||
* Wraps a legacy POJO custom widget, so that all widgets may be called via the same internal interface.
|
||||
*
|
||||
* Support will eventually be removed.
|
||||
* @remarks Expect this class to undergo breaking changes without warning.
|
||||
*/
|
||||
export class LegacyWidget<TWidget extends IBaseWidget = IBaseWidget>
|
||||
extends BaseWidget<TWidget>
|
||||
implements IBaseWidget
|
||||
{
|
||||
override draw?(
|
||||
ctx: CanvasRenderingContext2D,
|
||||
node: LGraphNode,
|
||||
widget_width: number,
|
||||
y: number,
|
||||
H: number,
|
||||
lowQuality?: boolean
|
||||
): void
|
||||
|
||||
override drawWidget(
|
||||
ctx: CanvasRenderingContext2D,
|
||||
options: DrawWidgetOptions
|
||||
) {
|
||||
const H = LiteGraph.NODE_WIDGET_HEIGHT
|
||||
this.draw?.(ctx, this.node, options.width, this.y, H, !!options.showText)
|
||||
}
|
||||
|
||||
override onClick() {
|
||||
console.warn(
|
||||
'Custom widget wrapper onClick was just called. Handling for third party widgets is done via LGraphCanvas - the mouse callback.'
|
||||
)
|
||||
}
|
||||
}
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
import type { LGraphNode } from '@/lib/litegraph/src/litegraph'
|
||||
import { isBooleanInputSpec } from '@/schemas/nodeDef/nodeDefSchemaV2'
|
||||
import type { InputSpec } from '@/schemas/nodeDef/nodeDefSchemaV2'
|
||||
import type { ComfyWidgetConstructorV2 } from '@/scripts/widgets'
|
||||
|
||||
export const useBooleanWidget = () => {
|
||||
const widgetConstructor: ComfyWidgetConstructorV2 = (
|
||||
node: LGraphNode,
|
||||
inputSpec: InputSpec
|
||||
) => {
|
||||
if (!isBooleanInputSpec(inputSpec)) {
|
||||
throw new Error(`Invalid input data: ${inputSpec}`)
|
||||
}
|
||||
|
||||
const defaultVal = inputSpec.default ?? false
|
||||
const options = {
|
||||
on: inputSpec.label_on,
|
||||
off: inputSpec.label_off
|
||||
}
|
||||
|
||||
return node.addWidget(
|
||||
'toggle',
|
||||
inputSpec.name,
|
||||
defaultVal,
|
||||
() => {},
|
||||
options
|
||||
)
|
||||
}
|
||||
|
||||
return widgetConstructor
|
||||
}
|
||||
+137
@@ -0,0 +1,137 @@
|
||||
import type { LGraphNode } from '@/lib/litegraph/src/litegraph'
|
||||
import { useSettingStore } from '@/platform/settings/settingStore'
|
||||
import { isStringInputSpec } from '@/schemas/nodeDef/nodeDefSchemaV2'
|
||||
import type { InputSpec } from '@/schemas/nodeDef/nodeDefSchemaV2'
|
||||
import { app } from '@/scripts/app'
|
||||
import type { ComfyWidgetConstructorV2 } from '@/scripts/widgets'
|
||||
|
||||
const TRACKPAD_DETECTION_THRESHOLD = 50
|
||||
|
||||
function addMultilineWidget(
|
||||
node: LGraphNode,
|
||||
name: string,
|
||||
opts: { defaultVal: string; placeholder?: string }
|
||||
) {
|
||||
const inputEl = document.createElement('textarea')
|
||||
inputEl.className = 'comfy-multiline-input'
|
||||
inputEl.value = opts.defaultVal
|
||||
inputEl.placeholder = opts.placeholder || name
|
||||
inputEl.spellcheck = useSettingStore().get('Comfy.TextareaWidget.Spellcheck')
|
||||
|
||||
const widget = node.addDOMWidget(name, 'customtext', inputEl, {
|
||||
getValue(): string {
|
||||
return inputEl.value
|
||||
},
|
||||
setValue(v: string) {
|
||||
inputEl.value = v
|
||||
}
|
||||
})
|
||||
|
||||
widget.inputEl = inputEl
|
||||
widget.options.minNodeSize = [400, 200]
|
||||
|
||||
inputEl.addEventListener('input', () => {
|
||||
widget.callback?.(widget.value)
|
||||
})
|
||||
|
||||
// Allow middle mouse button panning
|
||||
inputEl.addEventListener('pointerdown', (event: PointerEvent) => {
|
||||
if (event.button === 1) {
|
||||
app.canvas.processMouseDown(event)
|
||||
}
|
||||
})
|
||||
|
||||
inputEl.addEventListener('pointermove', (event: PointerEvent) => {
|
||||
if ((event.buttons & 4) === 4) {
|
||||
app.canvas.processMouseMove(event)
|
||||
}
|
||||
})
|
||||
|
||||
inputEl.addEventListener('pointerup', (event: PointerEvent) => {
|
||||
if (event.button === 1) {
|
||||
app.canvas.processMouseUp(event)
|
||||
}
|
||||
})
|
||||
|
||||
inputEl.addEventListener('wheel', (event: WheelEvent) => {
|
||||
const gesturesEnabled = useSettingStore().get(
|
||||
'LiteGraph.Pointer.TrackpadGestures'
|
||||
)
|
||||
const deltaX = event.deltaX
|
||||
const deltaY = event.deltaY
|
||||
|
||||
const canScrollY = inputEl.scrollHeight > inputEl.clientHeight
|
||||
const isHorizontal = Math.abs(deltaX) > Math.abs(deltaY)
|
||||
|
||||
// Prevent pinch zoom from zooming the page
|
||||
if (event.ctrlKey) {
|
||||
event.preventDefault()
|
||||
event.stopPropagation()
|
||||
app.canvas.processMouseWheel(event)
|
||||
return
|
||||
}
|
||||
|
||||
// Detect if this is likely a trackpad gesture vs mouse wheel
|
||||
// Trackpads usually have deltaX or smaller deltaY values (< TRACKPAD_DETECTION_THRESHOLD)
|
||||
// Mouse wheels typically have larger discrete deltaY values (>= TRACKPAD_DETECTION_THRESHOLD)
|
||||
const isLikelyTrackpad =
|
||||
Math.abs(deltaX) > 0 || Math.abs(deltaY) < TRACKPAD_DETECTION_THRESHOLD
|
||||
|
||||
// Trackpad gestures: when enabled, trackpad panning goes to canvas
|
||||
if (gesturesEnabled && isLikelyTrackpad) {
|
||||
event.preventDefault()
|
||||
event.stopPropagation()
|
||||
app.canvas.processMouseWheel(event)
|
||||
return
|
||||
}
|
||||
|
||||
// When gestures disabled: horizontal always goes to canvas (no horizontal scroll in textarea)
|
||||
if (isHorizontal) {
|
||||
event.preventDefault()
|
||||
event.stopPropagation()
|
||||
app.canvas.processMouseWheel(event)
|
||||
return
|
||||
}
|
||||
|
||||
// Vertical scrolling when gestures disabled: let textarea scroll if scrollable
|
||||
if (canScrollY) {
|
||||
event.stopPropagation()
|
||||
return
|
||||
}
|
||||
|
||||
// If textarea can't scroll vertically, pass to canvas
|
||||
event.preventDefault()
|
||||
app.canvas.processMouseWheel(event)
|
||||
})
|
||||
|
||||
return widget
|
||||
}
|
||||
|
||||
export const useStringWidget = () => {
|
||||
const widgetConstructor: ComfyWidgetConstructorV2 = (
|
||||
node: LGraphNode,
|
||||
inputSpec: InputSpec
|
||||
) => {
|
||||
if (!isStringInputSpec(inputSpec)) {
|
||||
throw new Error(`Invalid input data: ${inputSpec}`)
|
||||
}
|
||||
|
||||
const defaultVal = inputSpec.default ?? ''
|
||||
const multiline = inputSpec.multiline
|
||||
|
||||
const widget = multiline
|
||||
? addMultilineWidget(node, inputSpec.name, {
|
||||
defaultVal,
|
||||
placeholder: inputSpec.placeholder
|
||||
})
|
||||
: node.addWidget('text', inputSpec.name, defaultVal, () => {}, {})
|
||||
|
||||
if (typeof inputSpec.dynamicPrompts === 'boolean') {
|
||||
widget.dynamicPrompts = inputSpec.dynamicPrompts
|
||||
}
|
||||
|
||||
return widget
|
||||
}
|
||||
|
||||
return widgetConstructor
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
+394
@@ -0,0 +1,394 @@
|
||||
import _ from 'es-toolkit/compat'
|
||||
import { type Component, toRaw } from 'vue'
|
||||
|
||||
import { useChainCallback } from '@/composables/functional/useChainCallback'
|
||||
import {
|
||||
LGraphNode,
|
||||
LegacyWidget,
|
||||
LiteGraph
|
||||
} from '@/lib/litegraph/src/litegraph'
|
||||
import type {
|
||||
IBaseWidget,
|
||||
IWidgetOptions
|
||||
} from '@/lib/litegraph/src/types/widgets'
|
||||
import type { InputSpec } from '@/schemas/nodeDef/nodeDefSchemaV2'
|
||||
import { useDomWidgetStore } from '@/stores/domWidgetStore'
|
||||
import { generateUUID } from '@/utils/formatUtil'
|
||||
|
||||
export interface BaseDOMWidget<
|
||||
V extends object | string = object | string
|
||||
> extends IBaseWidget<V, string, DOMWidgetOptions<V>> {
|
||||
// ICustomWidget properties
|
||||
type: string
|
||||
options: DOMWidgetOptions<V>
|
||||
value: V
|
||||
callback?: (value: V) => void
|
||||
|
||||
// BaseDOMWidget properties
|
||||
/** The unique ID of the widget. */
|
||||
readonly id: string
|
||||
/** The node that the widget belongs to. */
|
||||
readonly node: LGraphNode
|
||||
/** Whether the widget is visible. */
|
||||
isVisible(): boolean
|
||||
/** The margin of the widget. */
|
||||
margin: number
|
||||
}
|
||||
|
||||
/**
|
||||
* A DOM widget that wraps a custom HTML element as a litegraph widget.
|
||||
*/
|
||||
export interface DOMWidget<
|
||||
T extends HTMLElement,
|
||||
V extends object | string
|
||||
> extends BaseDOMWidget<V> {
|
||||
element: T
|
||||
/**
|
||||
* @deprecated Legacy property used by some extensions for customtext
|
||||
* (textarea) widgets. Use {@link element} instead as it provides the same
|
||||
* functionality and works for all DOMWidget types.
|
||||
*/
|
||||
inputEl?: T
|
||||
}
|
||||
|
||||
/**
|
||||
* Additional props that can be passed to component widgets.
|
||||
* These are in addition to the standard props that are always provided:
|
||||
* - modelValue: The widget's value (handled by v-model)
|
||||
* - widget: Reference to the widget instance
|
||||
* - onUpdate:modelValue: The update handler for v-model
|
||||
*/
|
||||
type ComponentWidgetCustomProps = Record<string, unknown>
|
||||
|
||||
/**
|
||||
* Standard props that are handled separately by DomWidget.vue and should be
|
||||
* omitted when defining custom props for component widgets
|
||||
*/
|
||||
export type ComponentWidgetStandardProps =
|
||||
| 'modelValue'
|
||||
| 'widget'
|
||||
| 'onUpdate:modelValue'
|
||||
|
||||
/**
|
||||
* A DOM widget that wraps a Vue component as a litegraph widget.
|
||||
*/
|
||||
export interface ComponentWidget<
|
||||
V extends object | string,
|
||||
P extends ComponentWidgetCustomProps = ComponentWidgetCustomProps
|
||||
> extends BaseDOMWidget<V> {
|
||||
readonly component: Component
|
||||
readonly inputSpec: InputSpec
|
||||
readonly props?: P
|
||||
}
|
||||
|
||||
export interface DOMWidgetOptions<
|
||||
V extends object | string
|
||||
> extends IWidgetOptions {
|
||||
/**
|
||||
* Whether to render a placeholder rectangle when zoomed out.
|
||||
*/
|
||||
hideOnZoom?: boolean
|
||||
selectOn?: string[]
|
||||
onHide?: (widget: BaseDOMWidget<V>) => void
|
||||
getValue?: () => V
|
||||
setValue?: (value: V) => void
|
||||
getMinHeight?: () => number
|
||||
getMaxHeight?: () => number
|
||||
getHeight?: () => string | number
|
||||
onDraw?: (widget: BaseDOMWidget<V>) => void
|
||||
margin?: number
|
||||
/**
|
||||
* @deprecated Use `afterResize` instead. This callback is a legacy API
|
||||
* that fires before resize happens, but it is no longer supported. Now it
|
||||
* fires after resize happens.
|
||||
* The resize logic has been upstreamed to litegraph in
|
||||
* https://github.com/Comfy-Org/ComfyUI_frontend/pull/2557
|
||||
*/
|
||||
beforeResize?: (this: BaseDOMWidget<V>, node: LGraphNode) => void
|
||||
afterResize?: (this: BaseDOMWidget<V>, node: LGraphNode) => void
|
||||
}
|
||||
|
||||
export const isDOMWidget = <T extends HTMLElement, V extends object | string>(
|
||||
widget: IBaseWidget
|
||||
): widget is DOMWidget<T, V> => 'element' in widget && !!widget.element
|
||||
|
||||
export const isComponentWidget = <V extends object | string>(
|
||||
widget: IBaseWidget
|
||||
): widget is ComponentWidget<V> => 'component' in widget && !!widget.component
|
||||
|
||||
abstract class BaseDOMWidgetImpl<V extends object | string>
|
||||
extends LegacyWidget<IBaseWidget<V, string, DOMWidgetOptions<V>>>
|
||||
implements BaseDOMWidget<V>
|
||||
{
|
||||
static readonly DEFAULT_MARGIN = 10
|
||||
declare readonly name: string
|
||||
declare readonly options: DOMWidgetOptions<V>
|
||||
declare callback?: (value: V) => void
|
||||
|
||||
readonly id: string
|
||||
|
||||
constructor(obj: {
|
||||
node: LGraphNode
|
||||
name: string
|
||||
type: string
|
||||
options: DOMWidgetOptions<V>
|
||||
}) {
|
||||
const { node, name, type, options } = obj
|
||||
super({ y: 0, name, type, options }, node)
|
||||
|
||||
this.id = generateUUID()
|
||||
}
|
||||
|
||||
override get value(): V {
|
||||
return this.options.getValue?.() ?? ('' as V)
|
||||
}
|
||||
|
||||
override set value(v: V) {
|
||||
this.options.setValue?.(v)
|
||||
this.callback?.(this.value)
|
||||
}
|
||||
|
||||
get margin(): number {
|
||||
return this.options.margin ?? BaseDOMWidgetImpl.DEFAULT_MARGIN
|
||||
}
|
||||
|
||||
isVisible(): boolean {
|
||||
return !['hidden'].includes(this.type) && this.node.isWidgetVisible(this)
|
||||
}
|
||||
|
||||
override draw(
|
||||
ctx: CanvasRenderingContext2D,
|
||||
_node: LGraphNode,
|
||||
widget_width: number,
|
||||
y: number,
|
||||
widget_height: number,
|
||||
lowQuality?: boolean
|
||||
): void {
|
||||
if (this.options.hideOnZoom && lowQuality && this.isVisible()) {
|
||||
// Draw a placeholder rectangle
|
||||
const originalFillStyle = ctx.fillStyle
|
||||
ctx.beginPath()
|
||||
ctx.fillStyle = LiteGraph.WIDGET_BGCOLOR
|
||||
ctx.rect(
|
||||
this.margin,
|
||||
y + this.margin,
|
||||
widget_width - this.margin * 2,
|
||||
(this.computedHeight ?? widget_height) - 2 * this.margin
|
||||
)
|
||||
ctx.fill()
|
||||
ctx.fillStyle = originalFillStyle
|
||||
} else if (this.promoted && this.isVisible()) {
|
||||
ctx.save()
|
||||
const adjustedMargin = this.margin - 1
|
||||
ctx.beginPath()
|
||||
ctx.strokeStyle = LiteGraph.WIDGET_PROMOTED_OUTLINE_COLOR
|
||||
ctx.strokeRect(
|
||||
adjustedMargin,
|
||||
y + adjustedMargin,
|
||||
widget_width - adjustedMargin * 2,
|
||||
(this.computedHeight ?? widget_height) - 2 * adjustedMargin
|
||||
)
|
||||
ctx.restore()
|
||||
}
|
||||
this.options.onDraw?.(this)
|
||||
}
|
||||
|
||||
override onRemove(): void {
|
||||
useDomWidgetStore().unregisterWidget(this.id)
|
||||
}
|
||||
|
||||
override createCopyForNode(node: LGraphNode): this {
|
||||
// @ts-expect-error
|
||||
const cloned: this = new (this.constructor as typeof this)({
|
||||
node: node,
|
||||
name: this.name,
|
||||
type: this.type,
|
||||
options: this.options
|
||||
})
|
||||
cloned.value = this.value
|
||||
// Preserve the Y position from the original widget to maintain proper positioning
|
||||
// when widgets are promoted through subgraph nesting
|
||||
cloned.y = this.y
|
||||
return cloned
|
||||
}
|
||||
}
|
||||
|
||||
export class DOMWidgetImpl<T extends HTMLElement, V extends object | string>
|
||||
extends BaseDOMWidgetImpl<V>
|
||||
implements DOMWidget<T, V>
|
||||
{
|
||||
override readonly element: T
|
||||
|
||||
constructor(obj: {
|
||||
node: LGraphNode
|
||||
name: string
|
||||
type: string
|
||||
element: T
|
||||
options: DOMWidgetOptions<V>
|
||||
}) {
|
||||
super(obj)
|
||||
this.element = obj.element
|
||||
}
|
||||
|
||||
override createCopyForNode(node: LGraphNode): this {
|
||||
// @ts-expect-error
|
||||
const cloned: this = new (this.constructor as typeof this)({
|
||||
node: node,
|
||||
name: this.name,
|
||||
type: this.type,
|
||||
element: this.element, // Include the element!
|
||||
options: this.options
|
||||
})
|
||||
cloned.value = this.value
|
||||
// Preserve the Y position from the original widget to maintain proper positioning
|
||||
// when widgets are promoted through subgraph nesting
|
||||
cloned.y = this.y
|
||||
return cloned
|
||||
}
|
||||
|
||||
/** Extract DOM widget size info */
|
||||
override computeLayoutSize(node: LGraphNode) {
|
||||
if (this.type === 'hidden') {
|
||||
return {
|
||||
minHeight: 0,
|
||||
maxHeight: 0,
|
||||
minWidth: 0
|
||||
}
|
||||
}
|
||||
|
||||
const styles = getComputedStyle(this.element)
|
||||
let minHeight =
|
||||
this.options.getMinHeight?.() ??
|
||||
parseInt(styles.getPropertyValue('--comfy-widget-min-height'))
|
||||
let maxHeight =
|
||||
this.options.getMaxHeight?.() ??
|
||||
parseInt(styles.getPropertyValue('--comfy-widget-max-height'))
|
||||
|
||||
let prefHeight: string | number =
|
||||
this.options.getHeight?.() ??
|
||||
styles.getPropertyValue('--comfy-widget-height')
|
||||
|
||||
if (typeof prefHeight === 'string' && prefHeight.endsWith?.('%')) {
|
||||
prefHeight =
|
||||
node.size[1] *
|
||||
(parseFloat(prefHeight.substring(0, prefHeight.length - 1)) / 100)
|
||||
} else {
|
||||
prefHeight =
|
||||
typeof prefHeight === 'number' ? prefHeight : parseInt(prefHeight)
|
||||
|
||||
if (isNaN(minHeight)) {
|
||||
minHeight = prefHeight
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
minHeight: isNaN(minHeight) ? 50 : minHeight,
|
||||
maxHeight: isNaN(maxHeight) ? undefined : maxHeight,
|
||||
minWidth: 0
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export class ComponentWidgetImpl<
|
||||
V extends object | string,
|
||||
P extends ComponentWidgetCustomProps = ComponentWidgetCustomProps
|
||||
>
|
||||
extends BaseDOMWidgetImpl<V>
|
||||
implements ComponentWidget<V, P>
|
||||
{
|
||||
readonly component: Component
|
||||
readonly inputSpec: InputSpec
|
||||
readonly props?: P
|
||||
|
||||
constructor(obj: {
|
||||
node: LGraphNode
|
||||
name: string
|
||||
component: Component
|
||||
inputSpec: InputSpec
|
||||
props?: P
|
||||
options: DOMWidgetOptions<V>
|
||||
type?: string
|
||||
}) {
|
||||
super({
|
||||
type: 'custom',
|
||||
...obj
|
||||
})
|
||||
this.component = obj.component
|
||||
this.inputSpec = obj.inputSpec
|
||||
this.props = obj.props
|
||||
}
|
||||
|
||||
override computeLayoutSize() {
|
||||
const minHeight = this.options.getMinHeight?.() ?? 50
|
||||
const maxHeight = this.options.getMaxHeight?.()
|
||||
return {
|
||||
minHeight,
|
||||
maxHeight,
|
||||
minWidth: 0
|
||||
}
|
||||
}
|
||||
|
||||
override serializeValue(): V {
|
||||
return toRaw(this.value)
|
||||
}
|
||||
}
|
||||
|
||||
export const addWidget = <W extends BaseDOMWidget<object | string>>(
|
||||
node: LGraphNode,
|
||||
widget: W
|
||||
) => {
|
||||
node.addCustomWidget(widget)
|
||||
|
||||
if (node.graph) {
|
||||
useDomWidgetStore().registerWidget(widget)
|
||||
}
|
||||
|
||||
node.onAdded = useChainCallback(node.onAdded, () => {
|
||||
useDomWidgetStore().registerWidget(widget)
|
||||
})
|
||||
|
||||
node.onRemoved = useChainCallback(node.onRemoved, () => {
|
||||
widget.onRemove?.()
|
||||
})
|
||||
|
||||
node.onResize = useChainCallback(node.onResize, () => {
|
||||
widget.options.beforeResize?.call(widget, node)
|
||||
widget.options.afterResize?.call(widget, node)
|
||||
})
|
||||
}
|
||||
|
||||
LGraphNode.prototype.addDOMWidget = function <
|
||||
T extends HTMLElement,
|
||||
V extends object | string
|
||||
>(
|
||||
this: LGraphNode,
|
||||
name: string,
|
||||
type: string,
|
||||
element: T,
|
||||
options: DOMWidgetOptions<V> = {}
|
||||
): DOMWidget<T, V> {
|
||||
const widget = new DOMWidgetImpl({
|
||||
node: this,
|
||||
name,
|
||||
type,
|
||||
element,
|
||||
options: { hideOnZoom: true, ...options }
|
||||
})
|
||||
// Note: Before `LGraphNode.configure` is called, `this.id` is always `-1`.
|
||||
addWidget(this, widget as unknown as BaseDOMWidget<object | string>)
|
||||
|
||||
// Workaround for https://github.com/Comfy-Org/ComfyUI_frontend/issues/2493
|
||||
// Some custom nodes are explicitly expecting getter and setter of `value`
|
||||
// property to be on instance instead of prototype.
|
||||
Object.defineProperty(widget, 'value', {
|
||||
get(this: DOMWidgetImpl<T, V>): V {
|
||||
return this.options.getValue?.() ?? ('' as V)
|
||||
},
|
||||
set(this: DOMWidgetImpl<T, V>, v: V) {
|
||||
this.options.setValue?.(v)
|
||||
this.callback?.(this.value)
|
||||
}
|
||||
})
|
||||
|
||||
return widget
|
||||
}
|
||||
+308
@@ -0,0 +1,308 @@
|
||||
import { t } from '@/i18n'
|
||||
import { type LGraphNode, isComboWidget } from '@/lib/litegraph/src/litegraph'
|
||||
import type {
|
||||
IBaseWidget,
|
||||
IComboWidget,
|
||||
IStringWidget
|
||||
} from '@/lib/litegraph/src/types/widgets'
|
||||
import { useSettingStore } from '@/platform/settings/settingStore'
|
||||
import { dynamicWidgets } from '@/core/graph/widgets/dynamicWidgets'
|
||||
import { useBooleanWidget } from '@/renderer/extensions/vueNodes/widgets/composables/useBooleanWidget'
|
||||
import { useChartWidget } from '@/renderer/extensions/vueNodes/widgets/composables/useChartWidget'
|
||||
import { useColorWidget } from '@/renderer/extensions/vueNodes/widgets/composables/useColorWidget'
|
||||
import { useComboWidget } from '@/renderer/extensions/vueNodes/widgets/composables/useComboWidget'
|
||||
import { useFloatWidget } from '@/renderer/extensions/vueNodes/widgets/composables/useFloatWidget'
|
||||
import { useGalleriaWidget } from '@/renderer/extensions/vueNodes/widgets/composables/useGalleriaWidget'
|
||||
import { useImageCompareWidget } from '@/renderer/extensions/vueNodes/widgets/composables/useImageCompareWidget'
|
||||
import { useImageUploadWidget } from '@/renderer/extensions/vueNodes/widgets/composables/useImageUploadWidget'
|
||||
import { useIntWidget } from '@/renderer/extensions/vueNodes/widgets/composables/useIntWidget'
|
||||
import { useMarkdownWidget } from '@/renderer/extensions/vueNodes/widgets/composables/useMarkdownWidget'
|
||||
import { useStringWidget } from '@/renderer/extensions/vueNodes/widgets/composables/useStringWidget'
|
||||
import { useTextareaWidget } from '@/renderer/extensions/vueNodes/widgets/composables/useTextareaWidget'
|
||||
import { transformInputSpecV1ToV2 } from '@/schemas/nodeDef/migration'
|
||||
import type { InputSpec as InputSpecV2 } from '@/schemas/nodeDef/nodeDefSchemaV2'
|
||||
import type { InputSpec } from '@/schemas/nodeDefSchema'
|
||||
|
||||
import type { ComfyApp } from './app'
|
||||
import './domWidget'
|
||||
import './errorNodeWidgets'
|
||||
|
||||
export type ComfyWidgetConstructorV2 = (
|
||||
node: LGraphNode,
|
||||
inputSpec: InputSpecV2
|
||||
) => IBaseWidget
|
||||
|
||||
export type ComfyWidgetConstructor = (
|
||||
node: LGraphNode,
|
||||
inputName: string,
|
||||
inputData: InputSpec,
|
||||
app: ComfyApp,
|
||||
widgetName?: string
|
||||
) => { widget: IBaseWidget; minWidth?: number; minHeight?: number }
|
||||
|
||||
/**
|
||||
* Transforms a V2 widget constructor to a V1 widget constructor.
|
||||
* @param widgetConstructorV2 The V2 widget constructor to transform.
|
||||
* @returns The transformed V1 widget constructor.
|
||||
*/
|
||||
const transformWidgetConstructorV2ToV1 = (
|
||||
widgetConstructorV2: ComfyWidgetConstructorV2
|
||||
): ComfyWidgetConstructor => {
|
||||
return (node, inputName, inputData) => {
|
||||
const inputSpec = transformInputSpecV1ToV2(inputData, {
|
||||
name: inputName
|
||||
})
|
||||
const widget = widgetConstructorV2(node, inputSpec)
|
||||
return {
|
||||
widget,
|
||||
minWidth: widget.options.minNodeSize?.[0],
|
||||
minHeight: widget.options.minNodeSize?.[1]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function controlValueRunBefore() {
|
||||
return useSettingStore().get('Comfy.WidgetControlMode') === 'before'
|
||||
}
|
||||
|
||||
export function updateControlWidgetLabel(widget: IBaseWidget) {
|
||||
if (controlValueRunBefore()) {
|
||||
widget.label = t('g.control_before_generate')
|
||||
} else {
|
||||
widget.label = t('g.control_after_generate')
|
||||
}
|
||||
}
|
||||
|
||||
export const IS_CONTROL_WIDGET = Symbol()
|
||||
const HAS_EXECUTED = Symbol()
|
||||
|
||||
export function addValueControlWidget(
|
||||
node: LGraphNode,
|
||||
targetWidget: IBaseWidget,
|
||||
defaultValue?: string,
|
||||
_values?: unknown,
|
||||
widgetName?: string,
|
||||
inputData?: InputSpec
|
||||
): IComboWidget {
|
||||
let name = inputData?.[1]?.control_after_generate
|
||||
if (typeof name !== 'string') {
|
||||
name = widgetName
|
||||
}
|
||||
const widgets = addValueControlWidgets(
|
||||
node,
|
||||
targetWidget,
|
||||
defaultValue ?? 'randomize',
|
||||
{
|
||||
addFilterList: false,
|
||||
controlAfterGenerateName: name
|
||||
},
|
||||
inputData
|
||||
)
|
||||
return widgets[0]
|
||||
}
|
||||
|
||||
export function addValueControlWidgets(
|
||||
node: LGraphNode,
|
||||
targetWidget: IBaseWidget,
|
||||
defaultValue?: string,
|
||||
options?: Record<string, any>,
|
||||
inputData?: InputSpec
|
||||
): [IComboWidget, ...IStringWidget[]] {
|
||||
if (!defaultValue) defaultValue = 'randomize'
|
||||
if (!options) options = {}
|
||||
|
||||
const getName = (defaultName: string, optionName: string) => {
|
||||
let name = defaultName
|
||||
if (options[optionName]) {
|
||||
name = options[optionName]
|
||||
} else if (typeof inputData?.[1]?.[defaultName] === 'string') {
|
||||
name = inputData?.[1]?.[defaultName]
|
||||
} else if (inputData?.[1]?.control_prefix) {
|
||||
name = inputData?.[1]?.control_prefix + ' ' + name
|
||||
}
|
||||
return name
|
||||
}
|
||||
|
||||
const valueControl = node.addWidget(
|
||||
'combo',
|
||||
getName('control_after_generate', 'controlAfterGenerateName'),
|
||||
defaultValue,
|
||||
function () {},
|
||||
{
|
||||
values: ['fixed', 'increment', 'decrement', 'randomize'],
|
||||
serialize: false, // Don't include this in prompt.
|
||||
canvasOnly: true
|
||||
}
|
||||
) as IComboWidget
|
||||
|
||||
valueControl.tooltip =
|
||||
'Allows the linked widget to be changed automatically, for example randomizing the noise seed.'
|
||||
valueControl[IS_CONTROL_WIDGET] = true
|
||||
updateControlWidgetLabel(valueControl)
|
||||
const widgets: [IComboWidget, ...IStringWidget[]] = [valueControl]
|
||||
|
||||
const isCombo = isComboWidget(targetWidget)
|
||||
let comboFilter: IStringWidget
|
||||
if (isCombo && valueControl.options.values) {
|
||||
// @ts-expect-error Combo widget values may be a dictionary or legacy function type
|
||||
valueControl.options.values.push('increment-wrap')
|
||||
}
|
||||
if (isCombo && options.addFilterList !== false) {
|
||||
comboFilter = node.addWidget(
|
||||
'string',
|
||||
getName('control_filter_list', 'controlFilterListName'),
|
||||
'',
|
||||
function () {},
|
||||
{
|
||||
serialize: false // Don't include this in prompt.
|
||||
}
|
||||
) as IStringWidget
|
||||
updateControlWidgetLabel(comboFilter)
|
||||
comboFilter.tooltip =
|
||||
"Allows for filtering the list of values when changing the value via the control generate mode. Allows for RegEx matches in the format /abc/ to only filter to values containing 'abc'."
|
||||
|
||||
widgets.push(comboFilter)
|
||||
}
|
||||
|
||||
const applyWidgetControl = () => {
|
||||
var v = valueControl.value
|
||||
|
||||
if (isCombo && v !== 'fixed') {
|
||||
let values = targetWidget.options.values ?? []
|
||||
const filter = comboFilter?.value
|
||||
if (filter) {
|
||||
let check
|
||||
if (filter.startsWith('/') && filter.endsWith('/')) {
|
||||
try {
|
||||
const regex = new RegExp(filter.substring(1, filter.length - 1))
|
||||
check = (item: string) => regex.test(item)
|
||||
} catch (error) {
|
||||
console.error(
|
||||
'Error constructing RegExp filter for node ' + node.id,
|
||||
filter,
|
||||
error
|
||||
)
|
||||
}
|
||||
}
|
||||
if (!check) {
|
||||
const lower = filter.toLocaleLowerCase()
|
||||
check = (item: string) => item.toLocaleLowerCase().includes(lower)
|
||||
}
|
||||
// @ts-expect-error Combo widget values may be a dictionary or legacy function type
|
||||
values = values.filter((item: string) => check(item))
|
||||
if (!values.length && targetWidget.options.values?.length) {
|
||||
console.warn(
|
||||
'Filter for node ' + node.id + ' has filtered out all items',
|
||||
filter
|
||||
)
|
||||
}
|
||||
}
|
||||
// @ts-expect-error targetWidget.value can be number or string
|
||||
let current_index = values.indexOf(targetWidget.value)
|
||||
let current_length = values.length
|
||||
|
||||
switch (v) {
|
||||
case 'increment':
|
||||
current_index += 1
|
||||
break
|
||||
case 'increment-wrap':
|
||||
current_index += 1
|
||||
if (current_index >= current_length) {
|
||||
current_index = 0
|
||||
}
|
||||
break
|
||||
case 'decrement':
|
||||
current_index -= 1
|
||||
break
|
||||
case 'randomize':
|
||||
// @ts-expect-error Combo widget values may be a dictionary or legacy function type
|
||||
current_index = Math.floor(Math.random() * current_length)
|
||||
break
|
||||
default:
|
||||
break
|
||||
}
|
||||
current_index = Math.max(0, current_index)
|
||||
// @ts-expect-error Combo widget values may be a dictionary or legacy function type
|
||||
current_index = Math.min(current_length - 1, current_index)
|
||||
if (current_index >= 0) {
|
||||
// @ts-expect-error Combo widget values may be a dictionary or legacy function type
|
||||
let value = values[current_index]
|
||||
targetWidget.value = value
|
||||
targetWidget.callback?.(value)
|
||||
}
|
||||
} else {
|
||||
//number
|
||||
let { min = 0, max = 1, step2 = 1 } = targetWidget.options
|
||||
// limit to something that javascript can handle
|
||||
max = Math.min(1125899906842624, max)
|
||||
min = Math.max(-1125899906842624, min)
|
||||
let range = (max - min) / step2
|
||||
|
||||
//adjust values based on valueControl Behaviour
|
||||
switch (v) {
|
||||
case 'fixed':
|
||||
break
|
||||
case 'increment':
|
||||
// @ts-expect-error targetWidget.value can be number or string
|
||||
targetWidget.value += step2
|
||||
break
|
||||
case 'decrement':
|
||||
// @ts-expect-error targetWidget.value can be number or string
|
||||
targetWidget.value -= step2
|
||||
break
|
||||
case 'randomize':
|
||||
targetWidget.value = Math.floor(Math.random() * range) * step2 + min
|
||||
break
|
||||
default:
|
||||
break
|
||||
}
|
||||
/*check if values are over or under their respective
|
||||
* ranges and set them to min or max.*/
|
||||
// @ts-expect-error targetWidget.value can be number or string
|
||||
if (targetWidget.value < min) targetWidget.value = min
|
||||
// @ts-expect-error targetWidget.value can be number or string
|
||||
if (targetWidget.value > max) targetWidget.value = max
|
||||
targetWidget.callback?.(targetWidget.value)
|
||||
}
|
||||
}
|
||||
|
||||
valueControl.beforeQueued = () => {
|
||||
if (controlValueRunBefore()) {
|
||||
// Don't run on first execution
|
||||
if (valueControl[HAS_EXECUTED]) {
|
||||
applyWidgetControl()
|
||||
}
|
||||
}
|
||||
valueControl[HAS_EXECUTED] = true
|
||||
}
|
||||
|
||||
valueControl.afterQueued = () => {
|
||||
if (!controlValueRunBefore()) {
|
||||
applyWidgetControl()
|
||||
}
|
||||
}
|
||||
|
||||
return widgets
|
||||
}
|
||||
|
||||
export const ComfyWidgets = {
|
||||
INT: transformWidgetConstructorV2ToV1(useIntWidget()),
|
||||
FLOAT: transformWidgetConstructorV2ToV1(useFloatWidget()),
|
||||
BOOLEAN: transformWidgetConstructorV2ToV1(useBooleanWidget()),
|
||||
STRING: transformWidgetConstructorV2ToV1(useStringWidget()),
|
||||
MARKDOWN: transformWidgetConstructorV2ToV1(useMarkdownWidget()),
|
||||
COMBO: transformWidgetConstructorV2ToV1(useComboWidget()),
|
||||
IMAGEUPLOAD: useImageUploadWidget(),
|
||||
COLOR: transformWidgetConstructorV2ToV1(useColorWidget()),
|
||||
IMAGECOMPARE: transformWidgetConstructorV2ToV1(useImageCompareWidget()),
|
||||
CHART: transformWidgetConstructorV2ToV1(useChartWidget()),
|
||||
GALLERIA: transformWidgetConstructorV2ToV1(useGalleriaWidget()),
|
||||
TEXTAREA: transformWidgetConstructorV2ToV1(useTextareaWidget()),
|
||||
...dynamicWidgets
|
||||
} as const
|
||||
|
||||
export function isValidWidgetType(
|
||||
key: unknown
|
||||
): key is keyof typeof ComfyWidgets {
|
||||
return ComfyWidgets[key as keyof typeof ComfyWidgets] !== undefined
|
||||
}
|
||||
+140
@@ -0,0 +1,140 @@
|
||||
import _ from 'es-toolkit/compat'
|
||||
|
||||
import type {
|
||||
ComboInputSpec,
|
||||
ComboInputSpecV2,
|
||||
FloatInputSpec,
|
||||
InputSpec,
|
||||
IntInputSpec,
|
||||
NumericInputOptions
|
||||
} from '@/schemas/nodeDefSchema'
|
||||
import {
|
||||
getComboSpecComboOptions,
|
||||
getInputSpecType,
|
||||
isComboInputSpec,
|
||||
isFloatInputSpec,
|
||||
isIntInputSpec
|
||||
} from '@/schemas/nodeDefSchema'
|
||||
|
||||
import { lcm } from './mathUtil'
|
||||
|
||||
const IGNORE_KEYS = new Set<string>([
|
||||
'default',
|
||||
'forceInput',
|
||||
'defaultInput',
|
||||
'control_after_generate',
|
||||
'multiline',
|
||||
'tooltip',
|
||||
'dynamicPrompts'
|
||||
])
|
||||
|
||||
const getRange = (options: NumericInputOptions) => {
|
||||
const min = options.min ?? -Infinity
|
||||
const max = options.max ?? Infinity
|
||||
return { min, max }
|
||||
}
|
||||
|
||||
const mergeNumericInputSpec = <T extends IntInputSpec | FloatInputSpec>(
|
||||
spec1: T,
|
||||
spec2: T
|
||||
): T | null => {
|
||||
const type = spec1[0]
|
||||
const options1 = spec1[1] ?? {}
|
||||
const options2 = spec2[1] ?? {}
|
||||
|
||||
const range1 = getRange(options1)
|
||||
const range2 = getRange(options2)
|
||||
|
||||
// If the ranges do not overlap, return null
|
||||
if (range1.min > range2.max || range1.max < range2.min) {
|
||||
return null
|
||||
}
|
||||
|
||||
const step1 = options1.step ?? 1
|
||||
const step2 = options2.step ?? 1
|
||||
|
||||
const mergedOptions = {
|
||||
// Take intersection of ranges
|
||||
min: Math.max(range1.min, range2.min),
|
||||
max: Math.min(range1.max, range2.max),
|
||||
step: lcm(step1, step2)
|
||||
}
|
||||
|
||||
return mergeCommonInputSpec(
|
||||
[type, { ...options1, ...mergedOptions }] as T,
|
||||
[type, { ...options2, ...mergedOptions }] as T
|
||||
)
|
||||
}
|
||||
|
||||
const mergeComboInputSpec = <T extends ComboInputSpec | ComboInputSpecV2>(
|
||||
spec1: T,
|
||||
spec2: T
|
||||
): T | null => {
|
||||
const options1 = spec1[1] ?? {}
|
||||
const options2 = spec2[1] ?? {}
|
||||
|
||||
const comboOptions1 = getComboSpecComboOptions(spec1)
|
||||
const comboOptions2 = getComboSpecComboOptions(spec2)
|
||||
|
||||
const intersection = _.intersection(comboOptions1, comboOptions2)
|
||||
|
||||
// If the intersection is empty, return null
|
||||
if (intersection.length === 0) {
|
||||
return null
|
||||
}
|
||||
|
||||
return mergeCommonInputSpec(
|
||||
['COMBO', { ...options1, options: intersection }] as T,
|
||||
['COMBO', { ...options2, options: intersection }] as T
|
||||
)
|
||||
}
|
||||
|
||||
const mergeCommonInputSpec = <T extends InputSpec>(
|
||||
spec1: T,
|
||||
spec2: T
|
||||
): T | null => {
|
||||
const type = getInputSpecType(spec1)
|
||||
const options1 = spec1[1] ?? {}
|
||||
const options2 = spec2[1] ?? {}
|
||||
|
||||
const compareKeys = _.union(_.keys(options1), _.keys(options2)).filter(
|
||||
(key) => !IGNORE_KEYS.has(key)
|
||||
)
|
||||
|
||||
const mergeIsValid = compareKeys.every((key) => {
|
||||
const value1 = options1[key]
|
||||
const value2 = options2[key]
|
||||
return value1 === value2 || (_.isNil(value1) && _.isNil(value2))
|
||||
})
|
||||
|
||||
return mergeIsValid ? ([type, { ...options1, ...options2 }] as T) : null
|
||||
}
|
||||
|
||||
/**
|
||||
* Merges two input specs.
|
||||
*
|
||||
* @param spec1 - The first input spec.
|
||||
* @param spec2 - The second input spec.
|
||||
* @returns The merged input spec, or null if the specs are not mergeable.
|
||||
*/
|
||||
export const mergeInputSpec = (
|
||||
spec1: InputSpec,
|
||||
spec2: InputSpec
|
||||
): InputSpec | null => {
|
||||
const type1 = getInputSpecType(spec1)
|
||||
const type2 = getInputSpecType(spec2)
|
||||
|
||||
if (type1 !== type2) {
|
||||
return null
|
||||
}
|
||||
|
||||
if (isIntInputSpec(spec1) || isFloatInputSpec(spec1)) {
|
||||
return mergeNumericInputSpec(spec1, spec2 as typeof spec1)
|
||||
}
|
||||
|
||||
if (isComboInputSpec(spec1)) {
|
||||
return mergeComboInputSpec(spec1, spec2 as typeof spec1)
|
||||
}
|
||||
|
||||
return mergeCommonInputSpec(spec1, spec2)
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"ref": "refs/tags/v0.11.0",
|
||||
"node_id": "REF_kwDOIygeJrFyZWZzL3RhZ3MvdjAuMTEuMA",
|
||||
"url": "https://api.github.com/repos/Comfy-Org/ComfyUI/git/refs/tags/v0.11.0",
|
||||
"object": {
|
||||
"sha": "09725967cf76304371c390ca1d6483e04061da48",
|
||||
"type": "commit",
|
||||
"url": "https://api.github.com/repos/Comfy-Org/ComfyUI/git/commits/09725967cf76304371c390ca1d6483e04061da48"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
[
|
||||
{
|
||||
"version": "1.48.6",
|
||||
"commit": "7b4172a243e0d43f1679c0be1c307560cd83c00d",
|
||||
"request": "https://api.github.com/repos/Comfy-Org/ComfyUI_frontend/git/trees/7b4172a243e0d43f1679c0be1c307560cd83c00d?recursive=1",
|
||||
"response_sha256": "fbf97d9a418c271561117f5a39aa6e5f6deb06cb768358770e1331393a303dbc",
|
||||
"truncated": false,
|
||||
"role": "discovery_only; rules use pinned source files in sources.json"
|
||||
},
|
||||
{
|
||||
"version": "1.53.6",
|
||||
"commit": "7d533a4cd4c4904ffdb0537f6b80ae9793dd62f1",
|
||||
"request": "https://api.github.com/repos/Comfy-Org/ComfyUI_frontend/git/trees/7d533a4cd4c4904ffdb0537f6b80ae9793dd62f1?recursive=1",
|
||||
"response_sha256": "05d8f4d77b564d46829bbc258ec0e9d24f2cb38cd97f63f496ea2fe0c4689648",
|
||||
"truncated": false,
|
||||
"role": "discovery_only; rules use pinned source files in sources.json"
|
||||
},
|
||||
{
|
||||
"version": "1.48.7",
|
||||
"commit": "6d6af63c00f132cd25dc29307fc56bd2c094fa22",
|
||||
"request": "https://api.github.com/repos/Comfy-Org/ComfyUI_frontend/git/trees/6d6af63c00f132cd25dc29307fc56bd2c094fa22?recursive=1",
|
||||
"response_sha256": "4f3102fdb7a81e4df1d4a71eed54eaca87cb12e8093c11b8fd3d5ac273f54c73",
|
||||
"truncated": false,
|
||||
"role": "discovery_only; rules use pinned source files in sources.json"
|
||||
},
|
||||
{
|
||||
"version": "1.37.11",
|
||||
"commit": "4bf6a1960028cc5cda57d48f9fb01a74dfaade43",
|
||||
"request": "https://api.github.com/repos/Comfy-Org/ComfyUI_frontend/git/trees/4bf6a1960028cc5cda57d48f9fb01a74dfaade43?recursive=1",
|
||||
"response_sha256": "501f8b55d5853c40e91187428a1e8afb19a99d1938ecab6a6303b52037bff4ca",
|
||||
"truncated": false,
|
||||
"role": "discovery_only; rules use pinned source files in sources.json"
|
||||
}
|
||||
]
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,723 @@
|
||||
# ComfyUI-QwenASR
|
||||
# ComfyUI custom nodes for Qwen3-ASR speech-to-text models.
|
||||
# Models License Notice:
|
||||
# - Qwen3-ASR: Apache-2.0 License (https://huggingface.co/Qwen/Qwen3-ASR-1.7B)
|
||||
# This integration script follows GPL-3.0 License.
|
||||
|
||||
import os
|
||||
import shutil
|
||||
import sys
|
||||
import time
|
||||
from pathlib import Path
|
||||
from typing import Optional, Tuple, List
|
||||
|
||||
import numpy as np
|
||||
import torch
|
||||
import folder_paths
|
||||
import comfy.model_management as model_management
|
||||
|
||||
_CURRENT_DIR = Path(__file__).parent
|
||||
_QWEN_ASR_DIR = _CURRENT_DIR / "qwen_asr"
|
||||
if _QWEN_ASR_DIR.exists() and str(_CURRENT_DIR) not in sys.path:
|
||||
sys.path.insert(0, str(_CURRENT_DIR))
|
||||
|
||||
try:
|
||||
from qwen_asr import Qwen3ASRModel
|
||||
except Exception as _e:
|
||||
Qwen3ASRModel = None
|
||||
_IMPORT_ERROR = _e
|
||||
else:
|
||||
_IMPORT_ERROR = None
|
||||
|
||||
# ComfyUI model folder registration
|
||||
QWEN3_ASR_ROOT = os.path.join(folder_paths.models_dir, "Qwen3-ASR")
|
||||
os.makedirs(QWEN3_ASR_ROOT, exist_ok=True)
|
||||
folder_paths.add_model_folder_path("Qwen3-ASR", QWEN3_ASR_ROOT)
|
||||
|
||||
SUPPORTED_LANGUAGES = [
|
||||
"auto",
|
||||
"Chinese", "English", "Cantonese", "Arabic", "German", "French", "Spanish",
|
||||
"Portuguese", "Indonesian", "Italian", "Korean", "Russian", "Thai",
|
||||
"Vietnamese", "Japanese", "Turkish", "Hindi", "Malay", "Dutch", "Swedish",
|
||||
"Danish", "Finnish", "Polish", "Czech", "Filipino", "Persian", "Greek",
|
||||
"Hungarian", "Macedonian", "Romanian",
|
||||
]
|
||||
|
||||
_ASR_MODEL_CACHE = {}
|
||||
_CONFIG_CACHE = {"mtime": None, "data": None}
|
||||
|
||||
|
||||
def _default_config():
|
||||
return {
|
||||
"models": {
|
||||
"Qwen/Qwen3-ASR-1.7B": "Qwen3-ASR-1.7B",
|
||||
"Qwen/Qwen3-ASR-0.6B": "Qwen3-ASR-0.6B",
|
||||
},
|
||||
"aligners": {
|
||||
"None": None,
|
||||
"Qwen/Qwen3-ForcedAligner-0.6B": "Qwen3-ForcedAligner-0.6B",
|
||||
},
|
||||
"sources": ["HuggingFace", "ModelScope"],
|
||||
"defaults": {
|
||||
"repo_id": "Qwen/Qwen3-ASR-0.6B",
|
||||
"source": "HuggingFace",
|
||||
"precision": "bf16",
|
||||
"attention": "auto",
|
||||
"language": "auto",
|
||||
"forced_aligner": "Qwen/Qwen3-ForcedAligner-0.6B",
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
def _load_config():
|
||||
path = _CURRENT_DIR / "config.json"
|
||||
try:
|
||||
mtime = path.stat().st_mtime
|
||||
except Exception:
|
||||
mtime = None
|
||||
|
||||
cache = _CONFIG_CACHE
|
||||
if cache["data"] is not None and cache["mtime"] == mtime:
|
||||
return cache["data"]
|
||||
|
||||
data = _default_config()
|
||||
if mtime is not None:
|
||||
try:
|
||||
import json
|
||||
|
||||
loaded = json.loads(path.read_text(encoding="utf-8"))
|
||||
if isinstance(loaded, dict):
|
||||
data.update(loaded)
|
||||
except Exception as e:
|
||||
print(f"[Qwen3ASR] Failed to read config.json: {e}")
|
||||
|
||||
cache["mtime"] = mtime
|
||||
cache["data"] = data
|
||||
return data
|
||||
|
||||
|
||||
def _get_model_ids():
|
||||
models = _load_config().get("models") or {}
|
||||
if isinstance(models, dict) and models:
|
||||
return models
|
||||
return _default_config()["models"]
|
||||
|
||||
|
||||
def _get_aligner_ids():
|
||||
aligners = _load_config().get("aligners") or {}
|
||||
if isinstance(aligners, dict) and aligners:
|
||||
return aligners
|
||||
return _default_config()["aligners"]
|
||||
|
||||
|
||||
def _get_sources():
|
||||
sources = _load_config().get("sources") or []
|
||||
if isinstance(sources, list) and sources:
|
||||
return sources
|
||||
return _default_config()["sources"]
|
||||
|
||||
|
||||
def _get_defaults():
|
||||
defaults = _load_config().get("defaults") or {}
|
||||
if isinstance(defaults, dict) and defaults:
|
||||
return defaults
|
||||
return _default_config()["defaults"]
|
||||
_EXTRA_MODEL_PATHS = None
|
||||
|
||||
|
||||
def _normalize_paths(paths):
|
||||
normalized = []
|
||||
for p in paths:
|
||||
if not isinstance(p, str):
|
||||
continue
|
||||
p = p.strip()
|
||||
if not p:
|
||||
continue
|
||||
normalized.append(os.path.normpath(os.path.expanduser(p)))
|
||||
return normalized
|
||||
|
||||
|
||||
def _extract_yaml_paths(data):
|
||||
if not isinstance(data, dict):
|
||||
return []
|
||||
|
||||
def split_paths(value):
|
||||
if isinstance(value, str):
|
||||
lines = [line.strip() for line in value.splitlines()]
|
||||
return [line for line in lines if line]
|
||||
if isinstance(value, list):
|
||||
return [v for v in value if isinstance(v, str) and v.strip()]
|
||||
return []
|
||||
|
||||
def is_abs(p):
|
||||
if not p:
|
||||
return False
|
||||
if os.path.isabs(p):
|
||||
return True
|
||||
if len(p) > 1 and p[1] == ":":
|
||||
return True
|
||||
return False
|
||||
|
||||
def pull(d):
|
||||
found = []
|
||||
for key in ("paths", "roots", "folders", "models", "search_paths"):
|
||||
value = d.get(key)
|
||||
found.extend(split_paths(value))
|
||||
return found
|
||||
|
||||
paths = []
|
||||
paths.extend(pull(data))
|
||||
for section in data.values():
|
||||
if not isinstance(section, dict):
|
||||
continue
|
||||
base_path = section.get("base_path")
|
||||
if isinstance(base_path, str) and base_path.strip():
|
||||
paths.append(base_path)
|
||||
for key, value in section.items():
|
||||
if key == "base_path" or key == "is_default":
|
||||
continue
|
||||
for item in split_paths(value):
|
||||
if is_abs(item) or not isinstance(base_path, str):
|
||||
paths.append(item)
|
||||
else:
|
||||
paths.append(os.path.join(base_path, item))
|
||||
return paths
|
||||
|
||||
|
||||
def _load_extra_model_paths():
|
||||
global _EXTRA_MODEL_PATHS
|
||||
if _EXTRA_MODEL_PATHS is not None:
|
||||
return _EXTRA_MODEL_PATHS
|
||||
|
||||
candidates = []
|
||||
try:
|
||||
base_path = Path(getattr(folder_paths, "base_path", ""))
|
||||
if base_path:
|
||||
candidates.append(base_path / "extra_model_paths.yaml")
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
collected = []
|
||||
for path in candidates:
|
||||
if not path.exists():
|
||||
continue
|
||||
try:
|
||||
import yaml
|
||||
data = yaml.safe_load(path.read_text(encoding="utf-8")) or {}
|
||||
except Exception as e:
|
||||
print(f"[Qwen3ASR] Failed to read {path}: {e}")
|
||||
continue
|
||||
collected.extend(_extract_yaml_paths(data))
|
||||
|
||||
normalized = []
|
||||
for p in _normalize_paths(collected):
|
||||
if os.path.isdir(p) and p not in normalized:
|
||||
normalized.append(p)
|
||||
|
||||
_EXTRA_MODEL_PATHS = tuple(normalized)
|
||||
return _EXTRA_MODEL_PATHS
|
||||
|
||||
|
||||
def _find_local_model(repo_id: str) -> Optional[str]:
|
||||
model_name = _get_model_ids().get(repo_id) or _get_aligner_ids().get(repo_id) or repo_id.split("/")[-1]
|
||||
candidates = []
|
||||
|
||||
default_root = _model_storage_path(repo_id)
|
||||
if os.path.isdir(default_root):
|
||||
candidates.append(default_root)
|
||||
|
||||
try:
|
||||
asr_roots = folder_paths.get_folder_paths("Qwen3-ASR") or []
|
||||
for root in asr_roots:
|
||||
candidates.append(os.path.join(root, model_name))
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
for root in _load_extra_model_paths():
|
||||
candidates.append(os.path.join(root, model_name))
|
||||
candidates.append(os.path.join(root, "Qwen3-ASR", model_name))
|
||||
candidates.append(os.path.join(root, "ASR", "Qwen3-ASR", model_name))
|
||||
|
||||
for path in candidates:
|
||||
if os.path.isdir(path) and os.listdir(path):
|
||||
return path
|
||||
return None
|
||||
|
||||
|
||||
def _model_storage_path(repo_id: str) -> str:
|
||||
name = _get_model_ids().get(repo_id) or _get_aligner_ids().get(repo_id) or repo_id.replace("/", "_")
|
||||
return os.path.join(QWEN3_ASR_ROOT, name)
|
||||
|
||||
|
||||
def _try_copy_cached(repo_id: str, target_dir: str) -> bool:
|
||||
if os.path.isdir(target_dir) and os.listdir(target_dir):
|
||||
return True
|
||||
|
||||
hf_cache = os.path.join(Path.home(), ".cache", "huggingface", "hub")
|
||||
hf_dir = os.path.join(hf_cache, f"models--{repo_id.replace('/', '--')}")
|
||||
snapshots = os.path.join(hf_dir, "snapshots")
|
||||
if os.path.isdir(snapshots):
|
||||
entries = sorted(os.listdir(snapshots))
|
||||
if entries:
|
||||
source = os.path.join(snapshots, entries[-1])
|
||||
try:
|
||||
shutil.copytree(source, target_dir, dirs_exist_ok=True)
|
||||
return True
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
ms_cache = os.path.join(Path.home(), ".cache", "modelscope", "hub")
|
||||
ms_dir = os.path.join(ms_cache, repo_id.replace("/", os.sep))
|
||||
if os.path.isdir(ms_dir):
|
||||
try:
|
||||
shutil.copytree(ms_dir, target_dir, dirs_exist_ok=True)
|
||||
return True
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
return False
|
||||
|
||||
|
||||
def _download_to_local(repo_id: str, source: str, target_dir: str) -> str:
|
||||
os.makedirs(target_dir, exist_ok=True)
|
||||
if source == "ModelScope":
|
||||
try:
|
||||
from modelscope import snapshot_download
|
||||
except Exception as e:
|
||||
raise RuntimeError("modelscope is required for ModelScope downloads") from e
|
||||
snapshot_download(repo_id, local_dir=target_dir)
|
||||
else:
|
||||
try:
|
||||
from huggingface_hub import snapshot_download
|
||||
except Exception as e:
|
||||
raise RuntimeError("huggingface_hub is required for HuggingFace downloads") from e
|
||||
snapshot_download(repo_id, local_dir=target_dir)
|
||||
|
||||
return target_dir
|
||||
|
||||
|
||||
def _normalize_audio(audio) -> Optional[Tuple[np.ndarray, int]]:
|
||||
if audio is None:
|
||||
return None
|
||||
|
||||
waveform = audio.get("waveform")
|
||||
sample_rate = audio.get("sample_rate")
|
||||
if waveform is None or sample_rate is None:
|
||||
return None
|
||||
|
||||
wave = waveform[0]
|
||||
if wave.ndim == 2 and wave.shape[0] > 1:
|
||||
wave = torch.mean(wave, dim=0)
|
||||
else:
|
||||
wave = wave.squeeze(0)
|
||||
|
||||
return (wave.detach().cpu().numpy().astype(np.float32), int(sample_rate))
|
||||
|
||||
|
||||
def _resolve_model_path(repo_id: str, source: str) -> str:
|
||||
local_path = _find_local_model(repo_id)
|
||||
if local_path:
|
||||
return local_path
|
||||
|
||||
target_dir = _model_storage_path(repo_id)
|
||||
if os.path.isdir(target_dir) and os.listdir(target_dir):
|
||||
return target_dir
|
||||
|
||||
if _try_copy_cached(repo_id, target_dir):
|
||||
return target_dir
|
||||
|
||||
return _download_to_local(repo_id, source, target_dir)
|
||||
|
||||
|
||||
def _build_dtype(precision: str, device: torch.device) -> torch.dtype:
|
||||
if precision == "fp16":
|
||||
return torch.float16
|
||||
if precision == "bf16":
|
||||
if device.type == "mps":
|
||||
return torch.float16
|
||||
return torch.bfloat16
|
||||
return torch.float32
|
||||
|
||||
|
||||
def _cache_key(
|
||||
model_path: str,
|
||||
dtype: torch.dtype,
|
||||
device: torch.device,
|
||||
attention: str,
|
||||
forced_aligner_path: str,
|
||||
max_inference_batch_size: int,
|
||||
max_new_tokens: int,
|
||||
) -> tuple:
|
||||
return (
|
||||
model_path,
|
||||
str(dtype),
|
||||
str(device),
|
||||
attention,
|
||||
forced_aligner_path or "",
|
||||
int(max_inference_batch_size),
|
||||
int(max_new_tokens),
|
||||
)
|
||||
|
||||
|
||||
def _load_cached_model(
|
||||
model_path: str,
|
||||
dtype: torch.dtype,
|
||||
device: torch.device,
|
||||
attention: str,
|
||||
forced_aligner_path: str,
|
||||
max_inference_batch_size: int = 32,
|
||||
max_new_tokens: int = 256,
|
||||
):
|
||||
key = _cache_key(model_path, dtype, device, attention, forced_aligner_path, max_inference_batch_size, max_new_tokens)
|
||||
cached = _ASR_MODEL_CACHE.get(key)
|
||||
if cached is not None:
|
||||
return cached
|
||||
|
||||
model_kwargs = {
|
||||
"dtype": dtype,
|
||||
"device_map": str(device),
|
||||
"max_inference_batch_size": int(max_inference_batch_size),
|
||||
"max_new_tokens": int(max_new_tokens),
|
||||
}
|
||||
if attention != "auto":
|
||||
model_kwargs["attn_implementation"] = attention
|
||||
if forced_aligner_path:
|
||||
model_kwargs["forced_aligner"] = forced_aligner_path
|
||||
model_kwargs["forced_aligner_kwargs"] = {
|
||||
"dtype": dtype,
|
||||
"device_map": str(device),
|
||||
}
|
||||
if attention != "auto":
|
||||
model_kwargs["forced_aligner_kwargs"]["attn_implementation"] = attention
|
||||
|
||||
model = Qwen3ASRModel.from_pretrained(model_path, **model_kwargs)
|
||||
_ASR_MODEL_CACHE[key] = model
|
||||
return model
|
||||
|
||||
|
||||
def _format_srt_time(seconds: float) -> str:
|
||||
total_ms = max(0, int(round(seconds * 1000)))
|
||||
ms = total_ms % 1000
|
||||
total_s = total_ms // 1000
|
||||
s = total_s % 60
|
||||
total_m = total_s // 60
|
||||
m = total_m % 60
|
||||
h = total_m // 60
|
||||
return f"{h:02d}:{m:02d}:{s:02d},{ms:03d}"
|
||||
|
||||
|
||||
def _build_srt(time_stamps) -> str:
|
||||
if not time_stamps:
|
||||
return ""
|
||||
lines = []
|
||||
for idx, item in enumerate(time_stamps, start=1):
|
||||
lines.append(str(idx))
|
||||
lines.append(f"{_format_srt_time(item.start_time)} --> {_format_srt_time(item.end_time)}")
|
||||
lines.append(item.text or "")
|
||||
lines.append("")
|
||||
return "\n".join(lines).strip()
|
||||
|
||||
|
||||
def _join_tokens(a: str, b: str) -> str:
|
||||
if not a:
|
||||
return b
|
||||
if not b:
|
||||
return a
|
||||
# If either side contains CJK, join without space.
|
||||
for ch in (a[-1], b[0]):
|
||||
if "\u4e00" <= ch <= "\u9fff":
|
||||
return f"{a}{b}"
|
||||
return f"{a} {b}"
|
||||
|
||||
|
||||
def _group_time_stamps(time_stamps, max_gap_sec: float, max_chars: int, split_mode: str):
|
||||
if not time_stamps:
|
||||
return []
|
||||
groups = []
|
||||
cur = None
|
||||
punct = ("。", "!", "?", ".", "!", "?")
|
||||
for item in time_stamps:
|
||||
text = (item.text or "").strip()
|
||||
if not text:
|
||||
continue
|
||||
if cur is None:
|
||||
cur = {
|
||||
"start": item.start_time,
|
||||
"end": item.end_time,
|
||||
"text": text,
|
||||
}
|
||||
continue
|
||||
|
||||
gap = float(item.start_time) - float(cur["end"])
|
||||
too_far = gap > max_gap_sec
|
||||
too_long = max_chars > 0 and (len(cur["text"]) + len(text)) > max_chars
|
||||
end_sentence = any(cur["text"].endswith(p) for p in punct)
|
||||
|
||||
split_by_punct = split_mode in ("split_by_punctuation", "split_by_punctuation_or_length", "split_by_punctuation_or_pause", "split_by_punctuation_or_pause_or_length")
|
||||
split_by_length = split_mode in ("split_by_length", "split_by_punctuation_or_length", "split_by_punctuation_or_pause_or_length")
|
||||
split_by_pause = split_mode in ("split_by_pause", "split_by_punctuation_or_pause", "split_by_punctuation_or_pause_or_length")
|
||||
|
||||
should_split = False
|
||||
if split_by_punct and end_sentence:
|
||||
should_split = True
|
||||
if split_by_length and too_long:
|
||||
should_split = True
|
||||
if split_by_pause and too_far:
|
||||
should_split = True
|
||||
|
||||
if should_split:
|
||||
groups.append(cur)
|
||||
cur = {
|
||||
"start": item.start_time,
|
||||
"end": item.end_time,
|
||||
"text": text,
|
||||
}
|
||||
else:
|
||||
cur["text"] = _join_tokens(cur["text"], text).strip()
|
||||
cur["end"] = item.end_time
|
||||
|
||||
if cur is not None:
|
||||
groups.append(cur)
|
||||
return groups
|
||||
|
||||
|
||||
def _build_srt_from_groups(groups) -> str:
|
||||
if not groups:
|
||||
return ""
|
||||
lines = []
|
||||
for idx, g in enumerate(groups, start=1):
|
||||
lines.append(str(idx))
|
||||
lines.append(f"{_format_srt_time(g['start'])} --> {_format_srt_time(g['end'])}")
|
||||
lines.append(g["text"])
|
||||
lines.append("")
|
||||
return "\n".join(lines).strip()
|
||||
|
||||
|
||||
def _default_output_dir() -> str:
|
||||
base = folder_paths.get_output_directory()
|
||||
return os.path.join(base, "ComfyUI-QwenASR")
|
||||
|
||||
|
||||
def _is_dir_path(path: str) -> bool:
|
||||
if not path:
|
||||
return False
|
||||
if path.endswith(("/", "\\")):
|
||||
return True
|
||||
return os.path.isdir(path)
|
||||
|
||||
|
||||
def _make_default_filename(ext: str) -> str:
|
||||
stamp = time.strftime("%Y%m%d_%H%M%S")
|
||||
return f"qwenasr_subtitle_{stamp}{ext}"
|
||||
|
||||
|
||||
class AILab_Qwen3ASR:
|
||||
@classmethod
|
||||
def INPUT_TYPES(cls):
|
||||
defaults = _get_defaults()
|
||||
return {
|
||||
"required": {
|
||||
"audio": ("AUDIO", {"tooltip": "Audio input to transcribe."}),
|
||||
},
|
||||
"optional": {
|
||||
"model": (list(_get_model_ids().keys()), {"default": defaults.get("repo_id", "Qwen/Qwen3-ASR-0.6B"), "tooltip": "Choose the ASR model size."}),
|
||||
"precision": (["bf16", "fp16", "fp32"], {"default": defaults.get("precision", "bf16"), "tooltip": "Inference precision."}),
|
||||
"language": (SUPPORTED_LANGUAGES, {"default": defaults.get("language", "auto"), "tooltip": "Force language or auto-detect."}),
|
||||
"hints": ("STRING", {"default": "", "multiline": True, "tooltip": "Optional hints/keywords (names, terms) to improve recognition."}),
|
||||
"unload_models": ("BOOLEAN", {"default": True, "tooltip": "Unload cached model after inference."}),
|
||||
},
|
||||
}
|
||||
|
||||
RETURN_TYPES = ("STRING",)
|
||||
RETURN_NAMES = ("TEXT",)
|
||||
FUNCTION = "transcribe"
|
||||
CATEGORY = "🧪AILab/🎙️QwenASR"
|
||||
|
||||
def transcribe(
|
||||
self,
|
||||
audio,
|
||||
model="Qwen/Qwen3-ASR-0.6B",
|
||||
precision="bf16",
|
||||
language="auto",
|
||||
hints="",
|
||||
unload_models=True,
|
||||
):
|
||||
if Qwen3ASRModel is None:
|
||||
raise RuntimeError(f"qwen-asr not available: {_IMPORT_ERROR}")
|
||||
|
||||
device = model_management.get_torch_device()
|
||||
dtype = _build_dtype(precision, device)
|
||||
|
||||
source = _get_defaults().get("source", "HuggingFace")
|
||||
model_path = _resolve_model_path(model, source)
|
||||
|
||||
audio_data = _normalize_audio(audio)
|
||||
if audio_data is None:
|
||||
return ("",)
|
||||
|
||||
lang = None if language == "auto" else language
|
||||
ctx = hints.strip() if isinstance(hints, str) else ""
|
||||
|
||||
model = _load_cached_model(model_path, dtype, device, "auto", "")
|
||||
results = model.transcribe(
|
||||
audio=audio_data,
|
||||
language=lang,
|
||||
context=ctx if ctx else None,
|
||||
return_time_stamps=False,
|
||||
)
|
||||
|
||||
result = results[0]
|
||||
text = result.text or ""
|
||||
|
||||
if unload_models:
|
||||
_ASR_MODEL_CACHE.clear()
|
||||
try:
|
||||
model_management.soft_empty_cache()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
return (text,)
|
||||
|
||||
|
||||
class AILab_Qwen3ASRSubtitle:
|
||||
@classmethod
|
||||
def INPUT_TYPES(cls):
|
||||
defaults = _get_defaults()
|
||||
return {
|
||||
"required": {
|
||||
"audio": ("AUDIO", {"tooltip": "Audio input to transcribe."}),
|
||||
},
|
||||
"optional": {
|
||||
"model": (list(_get_model_ids().keys()), {"default": defaults.get("repo_id", "Qwen/Qwen3-ASR-0.6B"), "tooltip": "Choose the ASR model size."}),
|
||||
"precision": (["bf16", "fp16", "fp32"], {"default": defaults.get("precision", "bf16"), "tooltip": "Inference precision."}),
|
||||
"attention": (["auto", "flash_attention_2", "sdpa", "eager"], {"default": defaults.get("attention", "auto"), "tooltip": "Attention backend override."}),
|
||||
"forced_aligner": (list(_get_aligner_ids().keys()), {"default": defaults.get("forced_aligner", "Qwen/Qwen3-ForcedAligner-0.6B"), "tooltip": "Forced aligner for timestamped subtitles."}),
|
||||
"language": (SUPPORTED_LANGUAGES, {"default": defaults.get("language", "auto"), "tooltip": "Force language or auto-detect."}),
|
||||
"hints": ("STRING", {"default": "", "multiline": True, "tooltip": "Optional hints/keywords (names, terms) to improve recognition."}),
|
||||
"output_format": (["none", "txt", "srt"], {"default": "none", "tooltip": "File save format only (does not change subtitle output)."}),
|
||||
"output_path": ("STRING", {"default": "", "multiline": False, "tooltip": "Optional output file path (relative goes to ComfyUI output)."}),
|
||||
"split_mode": (["split_by_punctuation_or_pause_or_length", "split_by_punctuation_or_pause", "split_by_punctuation_or_length", "split_by_punctuation", "split_by_pause", "split_by_length"], {"default": "split_by_punctuation_or_pause_or_length", "tooltip": "Sentence splitting strategy."}),
|
||||
"max_gap_sec": ("FLOAT", {"default": 0.6, "min": 0.0, "max": 8.0, "step": 0.1, "tooltip": "Max silence gap to keep the same sentence."}),
|
||||
"max_chars": ("INT", {"default": 40, "min": 0, "max": 200, "tooltip": "Optional max characters per line (0 = no limit)."}),
|
||||
"max_inference_batch_size": ("INT", {"default": 32, "min": 1, "max": 256, "tooltip": "Batch size for inference/alignment to avoid OOM."}),
|
||||
"max_new_tokens": ("INT", {"default": 256, "min": 1, "max": 2048, "tooltip": "Max new tokens per chunk."}),
|
||||
"unload_models": ("BOOLEAN", {"default": True, "tooltip": "Unload cached model after inference."}),
|
||||
},
|
||||
}
|
||||
|
||||
RETURN_TYPES = ("STRING", "STRING", "STRING", "STRING")
|
||||
RETURN_NAMES = ("TEXT", "SUBTITLES", "LANUGAGE", "OUTPUT_PATH")
|
||||
FUNCTION = "transcribe"
|
||||
CATEGORY = "🧪AILab/🎙️QwenASR"
|
||||
|
||||
def transcribe(
|
||||
self,
|
||||
audio,
|
||||
model="Qwen/Qwen3-ASR-0.6B",
|
||||
precision="bf16",
|
||||
attention="auto",
|
||||
forced_aligner="Qwen/Qwen3-ForcedAligner-0.6B",
|
||||
language="auto",
|
||||
hints="",
|
||||
output_format="none",
|
||||
output_path="",
|
||||
split_mode="split_by_punctuation_or_pause_or_length",
|
||||
max_gap_sec=0.6,
|
||||
max_chars=60,
|
||||
max_inference_batch_size=32,
|
||||
max_new_tokens=256,
|
||||
unload_models=True,
|
||||
):
|
||||
if Qwen3ASRModel is None:
|
||||
raise RuntimeError(f"qwen-asr not available: {_IMPORT_ERROR}")
|
||||
|
||||
device = model_management.get_torch_device()
|
||||
dtype = _build_dtype(precision, device)
|
||||
|
||||
source = _get_defaults().get("source", "HuggingFace")
|
||||
model_path = _resolve_model_path(model, source)
|
||||
|
||||
forced_aligner_path = ""
|
||||
if forced_aligner and forced_aligner != "None":
|
||||
forced_aligner_path = _resolve_model_path(forced_aligner, source)
|
||||
|
||||
audio_data = _normalize_audio(audio)
|
||||
if audio_data is None:
|
||||
return ("", "", "")
|
||||
|
||||
lang = None if language == "auto" else language
|
||||
ctx = hints.strip() if isinstance(hints, str) else ""
|
||||
|
||||
model = _load_cached_model(
|
||||
model_path,
|
||||
dtype,
|
||||
device,
|
||||
attention,
|
||||
forced_aligner_path,
|
||||
max_inference_batch_size=max_inference_batch_size,
|
||||
max_new_tokens=max_new_tokens,
|
||||
)
|
||||
results = model.transcribe(
|
||||
audio=audio_data,
|
||||
language=lang,
|
||||
context=ctx if ctx else None,
|
||||
return_time_stamps=True,
|
||||
)
|
||||
|
||||
result = results[0]
|
||||
text = result.text or ""
|
||||
detected_lang = result.language or ""
|
||||
subtitles = ""
|
||||
file_path = ""
|
||||
time_stamps = getattr(result, "time_stamps", None)
|
||||
groups = _group_time_stamps(time_stamps, max_gap_sec=max_gap_sec, max_chars=max_chars, split_mode=split_mode)
|
||||
# Always build subtitle output
|
||||
lines = []
|
||||
for g in groups:
|
||||
lines.append(f"{g['start']:.2f}-{g['end']:.2f}: {g['text']}")
|
||||
subtitles = "\n".join(lines) if lines else ""
|
||||
|
||||
# Optional file save
|
||||
if output_format != "none":
|
||||
out_path = (output_path or "").strip()
|
||||
if not os.path.isabs(out_path):
|
||||
if out_path == "":
|
||||
out_path = _default_output_dir()
|
||||
out_path = os.path.join(folder_paths.get_output_directory(), out_path)
|
||||
|
||||
if _is_dir_path(out_path):
|
||||
ext = ".srt" if output_format == "srt" else ".txt"
|
||||
out_path = os.path.join(out_path, _make_default_filename(ext))
|
||||
else:
|
||||
root, ext = os.path.splitext(out_path)
|
||||
if not ext:
|
||||
ext = ".srt" if output_format == "srt" else ".txt"
|
||||
out_path = root + ext
|
||||
os.makedirs(os.path.dirname(out_path), exist_ok=True)
|
||||
if output_format == "srt":
|
||||
file_content = _build_srt_from_groups(groups)
|
||||
else:
|
||||
file_content = subtitles
|
||||
with open(out_path, "w", encoding="utf-8") as f:
|
||||
f.write(file_content)
|
||||
file_path = out_path
|
||||
|
||||
if unload_models:
|
||||
_ASR_MODEL_CACHE.clear()
|
||||
try:
|
||||
model_management.soft_empty_cache()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
return (text, subtitles, detected_lang, file_path)
|
||||
|
||||
|
||||
NODE_CLASS_MAPPINGS = {
|
||||
"AILab_Qwen3ASR": AILab_Qwen3ASR,
|
||||
"AILab_Qwen3ASRSubtitle": AILab_Qwen3ASRSubtitle,
|
||||
}
|
||||
|
||||
NODE_DISPLAY_NAME_MAPPINGS = {
|
||||
"AILab_Qwen3ASR": "ASR (QwenASR)",
|
||||
"AILab_Qwen3ASRSubtitle": "Subtitle (QwenASR)",
|
||||
}
|
||||
File diff suppressed because one or more lines are too long
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1 @@
|
||||
{"ref":"refs/tags/v1.37.11","node_id":"REF_kwDOMIrOxrJyZWZzL3RhZ3MvdjEuMzcuMTE","url":"https://api.github.com/repos/Comfy-Org/ComfyUI_frontend/git/refs/tags/v1.37.11","object":{"sha":"4bf6a1960028cc5cda57d48f9fb01a74dfaade43","type":"commit","url":"https://api.github.com/repos/Comfy-Org/ComfyUI_frontend/git/commits/4bf6a1960028cc5cda57d48f9fb01a74dfaade43"}}
|
||||
@@ -0,0 +1 @@
|
||||
{"ref":"refs/tags/v1.48.6","node_id":"REF_kwDOMIrOxrFyZWZzL3RhZ3MvdjEuNDguNg","url":"https://api.github.com/repos/Comfy-Org/ComfyUI_frontend/git/refs/tags/v1.48.6","object":{"sha":"7b4172a243e0d43f1679c0be1c307560cd83c00d","type":"commit","url":"https://api.github.com/repos/Comfy-Org/ComfyUI_frontend/git/commits/7b4172a243e0d43f1679c0be1c307560cd83c00d"}}
|
||||
@@ -0,0 +1 @@
|
||||
{"ref":"refs/tags/v1.48.7","node_id":"REF_kwDOMIrOxrFyZWZzL3RhZ3MvdjEuNDguNw","url":"https://api.github.com/repos/Comfy-Org/ComfyUI_frontend/git/refs/tags/v1.48.7","object":{"sha":"6d6af63c00f132cd25dc29307fc56bd2c094fa22","type":"commit","url":"https://api.github.com/repos/Comfy-Org/ComfyUI_frontend/git/commits/6d6af63c00f132cd25dc29307fc56bd2c094fa22"}}
|
||||
@@ -0,0 +1 @@
|
||||
{"ref":"refs/tags/v1.53.6","node_id":"REF_kwDOMIrOxrFyZWZzL3RhZ3MvdjEuNTMuNg","url":"https://api.github.com/repos/Comfy-Org/ComfyUI_frontend/git/refs/tags/v1.53.6","object":{"sha":"7d533a4cd4c4904ffdb0537f6b80ae9793dd62f1","type":"commit","url":"https://api.github.com/repos/Comfy-Org/ComfyUI_frontend/git/commits/7d533a4cd4c4904ffdb0537f6b80ae9793dd62f1"}}
|
||||
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"1.37.11": "4bf6a1960028cc5cda57d48f9fb01a74dfaade43",
|
||||
"1.48.7": "6d6af63c00f132cd25dc29307fc56bd2c094fa22",
|
||||
"1.53.6": "7d533a4cd4c4904ffdb0537f6b80ae9793dd62f1",
|
||||
"1.48.6": "7b4172a243e0d43f1679c0be1c307560cd83c00d"
|
||||
}
|
||||
Reference in New Issue
Block a user