mirror of
https://github.com/willmiao/ComfyUI-Lora-Manager.git
synced 2026-09-20 18:51:26 -03:00
Compare commits
5 Commits
77109b3cf8
...
b309becdf9
| Author | SHA1 | Date | |
|---|---|---|---|
| b309becdf9 | |||
| 1e375bb8d9 | |||
| 14da8a6f17 | |||
| da71985c3e | |||
| 7c4c8b8f30 |
+26
-1
@@ -50,6 +50,27 @@
|
||||
"mb": "MB",
|
||||
"gb": "GB",
|
||||
"tb": "TB"
|
||||
},
|
||||
"scanProgress": {
|
||||
"refreshing": "{type} werden aktualisiert...",
|
||||
"fullRebuilding": "{type} werden vollständig neu aufgebaut...",
|
||||
"actionRefresh": "Aktualisierung",
|
||||
"actionFullRebuild": "Vollständiger Neuaufbau",
|
||||
"actionRefreshLower": "Aktualisieren",
|
||||
"actionRebuildLower": "Neuaufbau",
|
||||
"stages": {
|
||||
"scan_folders": "Ordner werden gescannt...",
|
||||
"count_models": "{total} Dateien gefunden",
|
||||
"process_models": "Modelle werden verarbeitet",
|
||||
"reconcile_scan": "Änderungen werden geprüft...",
|
||||
"process_new": "Neue Modelle werden verarbeitet",
|
||||
"finalizing": "Abschließen..."
|
||||
},
|
||||
"eta": {
|
||||
"lessThanMinute": "Weniger als eine Minute verbleibend",
|
||||
"minutes": "~{minutes} Min. verbleibend",
|
||||
"hours": "~{hours} Std. {minutes} Min. verbleibend"
|
||||
}
|
||||
}
|
||||
},
|
||||
"onboarding": {
|
||||
@@ -1593,7 +1614,11 @@
|
||||
"clipSkip": "Clip Skip",
|
||||
"valuePlaceholder": "Wert",
|
||||
"add": "Hinzufügen",
|
||||
"invalidRange": "Ungültiges Bereichsformat. Verwenden Sie x.x-y.y"
|
||||
"invalidRange": "Ungültiges Bereichsformat. Verwenden Sie x.x-y.y",
|
||||
"invalidValue": "Bitte geben Sie eine gültige Zahl ein",
|
||||
"saveFailed": "Fehler beim Speichern des voreingestellten Parameters",
|
||||
"added": "Voreingestellter Parameter hinzugefügt",
|
||||
"updated": "Voreingestellter Parameter aktualisiert"
|
||||
},
|
||||
"triggerWords": {
|
||||
"label": "Trigger Words",
|
||||
|
||||
+26
-1
@@ -50,6 +50,27 @@
|
||||
"mb": "MB",
|
||||
"gb": "GB",
|
||||
"tb": "TB"
|
||||
},
|
||||
"scanProgress": {
|
||||
"refreshing": "Refreshing {type}s...",
|
||||
"fullRebuilding": "Full rebuild {type}s...",
|
||||
"actionRefresh": "Refresh",
|
||||
"actionFullRebuild": "Full rebuild",
|
||||
"actionRefreshLower": "refresh",
|
||||
"actionRebuildLower": "rebuild",
|
||||
"stages": {
|
||||
"scan_folders": "Scanning folders...",
|
||||
"count_models": "Found {total} files",
|
||||
"process_models": "Processing models",
|
||||
"reconcile_scan": "Checking for changes...",
|
||||
"process_new": "Processing new models",
|
||||
"finalizing": "Finalizing..."
|
||||
},
|
||||
"eta": {
|
||||
"lessThanMinute": "Less than a minute remaining",
|
||||
"minutes": "~{minutes} min remaining",
|
||||
"hours": "~{hours} hr {minutes} min remaining"
|
||||
}
|
||||
}
|
||||
},
|
||||
"onboarding": {
|
||||
@@ -1593,7 +1614,11 @@
|
||||
"clipSkip": "Clip Skip",
|
||||
"valuePlaceholder": "Value",
|
||||
"add": "Add",
|
||||
"invalidRange": "Invalid range format. Use x.x-y.y"
|
||||
"invalidRange": "Invalid range format. Use x.x-y.y",
|
||||
"invalidValue": "Please enter a valid number",
|
||||
"saveFailed": "Failed to save preset parameter",
|
||||
"added": "Preset parameter added",
|
||||
"updated": "Preset parameter updated"
|
||||
},
|
||||
"triggerWords": {
|
||||
"label": "Trigger Words",
|
||||
|
||||
+26
-1
@@ -50,6 +50,27 @@
|
||||
"mb": "MB",
|
||||
"gb": "GB",
|
||||
"tb": "TB"
|
||||
},
|
||||
"scanProgress": {
|
||||
"refreshing": "Actualizando {type}...",
|
||||
"fullRebuilding": "Reconstrucción completa de {type}...",
|
||||
"actionRefresh": "Actualización",
|
||||
"actionFullRebuild": "Reconstrucción completa",
|
||||
"actionRefreshLower": "actualizar",
|
||||
"actionRebuildLower": "reconstruir",
|
||||
"stages": {
|
||||
"scan_folders": "Escaneando carpetas...",
|
||||
"count_models": "Se encontraron {total} archivos",
|
||||
"process_models": "Procesando modelos",
|
||||
"reconcile_scan": "Comprobando cambios...",
|
||||
"process_new": "Procesando modelos nuevos",
|
||||
"finalizing": "Finalizando..."
|
||||
},
|
||||
"eta": {
|
||||
"lessThanMinute": "Queda menos de un minuto",
|
||||
"minutes": "Quedan ~{minutes} min",
|
||||
"hours": "Quedan ~{hours} h {minutes} min"
|
||||
}
|
||||
}
|
||||
},
|
||||
"onboarding": {
|
||||
@@ -1593,7 +1614,11 @@
|
||||
"clipSkip": "Clip Skip",
|
||||
"valuePlaceholder": "Valor",
|
||||
"add": "Añadir",
|
||||
"invalidRange": "Formato de rango inválido. Use x.x-y.y"
|
||||
"invalidRange": "Formato de rango inválido. Use x.x-y.y",
|
||||
"invalidValue": "Introduce un número válido",
|
||||
"saveFailed": "Error al guardar el parámetro preajustado",
|
||||
"added": "Parámetro preajustado añadido",
|
||||
"updated": "Parámetro preajustado actualizado"
|
||||
},
|
||||
"triggerWords": {
|
||||
"label": "Palabras clave",
|
||||
|
||||
+26
-1
@@ -50,6 +50,27 @@
|
||||
"mb": "Mo",
|
||||
"gb": "Go",
|
||||
"tb": "To"
|
||||
},
|
||||
"scanProgress": {
|
||||
"refreshing": "Actualisation des {type}...",
|
||||
"fullRebuilding": "Reconstruction complète des {type}...",
|
||||
"actionRefresh": "Actualisation",
|
||||
"actionFullRebuild": "Reconstruction complète",
|
||||
"actionRefreshLower": "l’actualisation",
|
||||
"actionRebuildLower": "la reconstruction",
|
||||
"stages": {
|
||||
"scan_folders": "Scan des dossiers...",
|
||||
"count_models": "{total} fichiers trouvés",
|
||||
"process_models": "Traitement des modèles",
|
||||
"reconcile_scan": "Vérification des modifications...",
|
||||
"process_new": "Traitement des nouveaux modèles",
|
||||
"finalizing": "Finalisation..."
|
||||
},
|
||||
"eta": {
|
||||
"lessThanMinute": "Moins d’une minute restante",
|
||||
"minutes": "~{minutes} min restantes",
|
||||
"hours": "~{hours} h {minutes} min restantes"
|
||||
}
|
||||
}
|
||||
},
|
||||
"onboarding": {
|
||||
@@ -1593,7 +1614,11 @@
|
||||
"clipSkip": "Clip Skip",
|
||||
"valuePlaceholder": "Valeur",
|
||||
"add": "Ajouter",
|
||||
"invalidRange": "Format de plage invalide. Utilisez x.x-y.y"
|
||||
"invalidRange": "Format de plage invalide. Utilisez x.x-y.y",
|
||||
"invalidValue": "Veuillez saisir un nombre valide",
|
||||
"saveFailed": "Échec de l'enregistrement du paramètre préréglé",
|
||||
"added": "Paramètre préréglé ajouté",
|
||||
"updated": "Paramètre préréglé mis à jour"
|
||||
},
|
||||
"triggerWords": {
|
||||
"label": "Mots-clés",
|
||||
|
||||
+26
-1
@@ -50,6 +50,27 @@
|
||||
"mb": "MB",
|
||||
"gb": "GB",
|
||||
"tb": "TB"
|
||||
},
|
||||
"scanProgress": {
|
||||
"refreshing": "מרענן {type}...",
|
||||
"fullRebuilding": "בונה מחדש את כל ה-{type}...",
|
||||
"actionRefresh": "רענון",
|
||||
"actionFullRebuild": "רענון מלא",
|
||||
"actionRefreshLower": "רענון",
|
||||
"actionRebuildLower": "רענון מלא",
|
||||
"stages": {
|
||||
"scan_folders": "סורק תיקיות...",
|
||||
"count_models": "נמצאו {total} קבצים",
|
||||
"process_models": "מעבד מודלים",
|
||||
"reconcile_scan": "בודק שינויים...",
|
||||
"process_new": "מעבד מודלים חדשים",
|
||||
"finalizing": "מסיים..."
|
||||
},
|
||||
"eta": {
|
||||
"lessThanMinute": "נותרה פחות מדקה",
|
||||
"minutes": "נותרו ~{minutes} דקות",
|
||||
"hours": "נותרו ~{hours} שעות ו-{minutes} דקות"
|
||||
}
|
||||
}
|
||||
},
|
||||
"onboarding": {
|
||||
@@ -1593,7 +1614,11 @@
|
||||
"clipSkip": "Clip Skip",
|
||||
"valuePlaceholder": "ערך",
|
||||
"add": "הוסף",
|
||||
"invalidRange": "פורמט טווח לא תקין. השתמש ב-x.x-y.y"
|
||||
"invalidRange": "פורמט טווח לא תקין. השתמש ב-x.x-y.y",
|
||||
"invalidValue": "נא להזין מספר תקין",
|
||||
"saveFailed": "שמירת הפרמטר הקבוע מראש נכשלה",
|
||||
"added": "הפרמטר הקבוע מראש נוסף",
|
||||
"updated": "הפרמטר הקבוע מראש עודכן"
|
||||
},
|
||||
"triggerWords": {
|
||||
"label": "מילות טריגר",
|
||||
|
||||
+26
-1
@@ -50,6 +50,27 @@
|
||||
"mb": "MB",
|
||||
"gb": "GB",
|
||||
"tb": "TB"
|
||||
},
|
||||
"scanProgress": {
|
||||
"refreshing": "{type}を更新中...",
|
||||
"fullRebuilding": "{type}を完全に再構築中...",
|
||||
"actionRefresh": "更新",
|
||||
"actionFullRebuild": "完全な再構築",
|
||||
"actionRefreshLower": "更新",
|
||||
"actionRebuildLower": "再構築",
|
||||
"stages": {
|
||||
"scan_folders": "フォルダをスキャン中...",
|
||||
"count_models": "{total} 件のファイルが見つかりました",
|
||||
"process_models": "モデルを処理中",
|
||||
"reconcile_scan": "変更を確認中...",
|
||||
"process_new": "新しいモデルを処理中",
|
||||
"finalizing": "最終処理中..."
|
||||
},
|
||||
"eta": {
|
||||
"lessThanMinute": "残り1分未満",
|
||||
"minutes": "残り約 {minutes} 分",
|
||||
"hours": "残り約 {hours} 時間 {minutes} 分"
|
||||
}
|
||||
}
|
||||
},
|
||||
"onboarding": {
|
||||
@@ -1593,7 +1614,11 @@
|
||||
"clipSkip": "Clip Skip",
|
||||
"valuePlaceholder": "値",
|
||||
"add": "追加",
|
||||
"invalidRange": "無効な範囲形式です。x.x-y.y を使用してください"
|
||||
"invalidRange": "無効な範囲形式です。x.x-y.y を使用してください",
|
||||
"invalidValue": "有効な数値を入力してください",
|
||||
"saveFailed": "プリセットパラメータの保存に失敗しました",
|
||||
"added": "プリセットパラメータを追加しました",
|
||||
"updated": "プリセットパラメータを更新しました"
|
||||
},
|
||||
"triggerWords": {
|
||||
"label": "トリガーワード",
|
||||
|
||||
+26
-1
@@ -50,6 +50,27 @@
|
||||
"mb": "MB",
|
||||
"gb": "GB",
|
||||
"tb": "TB"
|
||||
},
|
||||
"scanProgress": {
|
||||
"refreshing": "{type} 새로고침 중...",
|
||||
"fullRebuilding": "{type} 전체 재구성 중...",
|
||||
"actionRefresh": "새로고침",
|
||||
"actionFullRebuild": "전체 재구성",
|
||||
"actionRefreshLower": "새로고침",
|
||||
"actionRebuildLower": "재구성",
|
||||
"stages": {
|
||||
"scan_folders": "폴더 스캔 중...",
|
||||
"count_models": "파일 {total}개 발견",
|
||||
"process_models": "모델 처리 중",
|
||||
"reconcile_scan": "변경 사항 확인 중...",
|
||||
"process_new": "새 모델 처리 중",
|
||||
"finalizing": "마무리 중..."
|
||||
},
|
||||
"eta": {
|
||||
"lessThanMinute": "남은 시간 1분 미만",
|
||||
"minutes": "약 {minutes}분 남음",
|
||||
"hours": "약 {hours}시간 {minutes}분 남음"
|
||||
}
|
||||
}
|
||||
},
|
||||
"onboarding": {
|
||||
@@ -1593,7 +1614,11 @@
|
||||
"clipSkip": "클립 스킵",
|
||||
"valuePlaceholder": "값",
|
||||
"add": "추가",
|
||||
"invalidRange": "잘못된 범위 형식입니다. x.x-y.y를 사용하세요"
|
||||
"invalidRange": "잘못된 범위 형식입니다. x.x-y.y를 사용하세요",
|
||||
"invalidValue": "유효한 숫자를 입력하세요",
|
||||
"saveFailed": "프리셋 매개변수 저장에 실패했습니다",
|
||||
"added": "프리셋 매개변수가 추가되었습니다",
|
||||
"updated": "프리셋 매개변수가 업데이트되었습니다"
|
||||
},
|
||||
"triggerWords": {
|
||||
"label": "트리거 단어",
|
||||
|
||||
+26
-1
@@ -50,6 +50,27 @@
|
||||
"mb": "МБ",
|
||||
"gb": "ГБ",
|
||||
"tb": "ТБ"
|
||||
},
|
||||
"scanProgress": {
|
||||
"refreshing": "Обновление {type}...",
|
||||
"fullRebuilding": "Полная пересборка {type}...",
|
||||
"actionRefresh": "Обновление",
|
||||
"actionFullRebuild": "Полная пересборка",
|
||||
"actionRefreshLower": "обновить",
|
||||
"actionRebuildLower": "пересобрать",
|
||||
"stages": {
|
||||
"scan_folders": "Сканирование папок...",
|
||||
"count_models": "Найдено файлов: {total}",
|
||||
"process_models": "Обработка моделей",
|
||||
"reconcile_scan": "Проверка изменений...",
|
||||
"process_new": "Обработка новых моделей",
|
||||
"finalizing": "Завершение..."
|
||||
},
|
||||
"eta": {
|
||||
"lessThanMinute": "Осталось меньше минуты",
|
||||
"minutes": "Осталось ~{minutes} мин",
|
||||
"hours": "Осталось ~{hours} ч {minutes} мин"
|
||||
}
|
||||
}
|
||||
},
|
||||
"onboarding": {
|
||||
@@ -1593,7 +1614,11 @@
|
||||
"clipSkip": "Clip Skip",
|
||||
"valuePlaceholder": "Значение",
|
||||
"add": "Добавить",
|
||||
"invalidRange": "Неверный формат диапазона. Используйте x.x-y.y"
|
||||
"invalidRange": "Неверный формат диапазона. Используйте x.x-y.y",
|
||||
"invalidValue": "Введите корректное число",
|
||||
"saveFailed": "Не удалось сохранить предустановленный параметр",
|
||||
"added": "Предустановленный параметр добавлен",
|
||||
"updated": "Предустановленный параметр обновлён"
|
||||
},
|
||||
"triggerWords": {
|
||||
"label": "Триггерные слова",
|
||||
|
||||
+26
-1
@@ -50,6 +50,27 @@
|
||||
"mb": "MB",
|
||||
"gb": "GB",
|
||||
"tb": "TB"
|
||||
},
|
||||
"scanProgress": {
|
||||
"refreshing": "正在刷新 {type}...",
|
||||
"fullRebuilding": "正在完全重建 {type}...",
|
||||
"actionRefresh": "刷新",
|
||||
"actionFullRebuild": "完全重建",
|
||||
"actionRefreshLower": "刷新",
|
||||
"actionRebuildLower": "重建",
|
||||
"stages": {
|
||||
"scan_folders": "正在扫描文件夹...",
|
||||
"count_models": "找到 {total} 个文件",
|
||||
"process_models": "正在处理模型",
|
||||
"reconcile_scan": "正在检查变更...",
|
||||
"process_new": "正在处理新模型",
|
||||
"finalizing": "正在收尾..."
|
||||
},
|
||||
"eta": {
|
||||
"lessThanMinute": "剩余时间不到一分钟",
|
||||
"minutes": "剩余约 {minutes} 分钟",
|
||||
"hours": "剩余约 {hours} 小时 {minutes} 分钟"
|
||||
}
|
||||
}
|
||||
},
|
||||
"onboarding": {
|
||||
@@ -1593,7 +1614,11 @@
|
||||
"clipSkip": "Clip Skip",
|
||||
"valuePlaceholder": "数值",
|
||||
"add": "添加",
|
||||
"invalidRange": "无效的范围格式。请使用 x.x-y.y"
|
||||
"invalidRange": "无效的范围格式。请使用 x.x-y.y",
|
||||
"invalidValue": "请输入有效的数值",
|
||||
"saveFailed": "保存预设参数失败",
|
||||
"added": "已添加预设参数",
|
||||
"updated": "已更新预设参数"
|
||||
},
|
||||
"triggerWords": {
|
||||
"label": "触发词",
|
||||
|
||||
+26
-1
@@ -50,6 +50,27 @@
|
||||
"mb": "MB",
|
||||
"gb": "GB",
|
||||
"tb": "TB"
|
||||
},
|
||||
"scanProgress": {
|
||||
"refreshing": "正在重新整理 {type}...",
|
||||
"fullRebuilding": "正在完整重建 {type}...",
|
||||
"actionRefresh": "重新整理",
|
||||
"actionFullRebuild": "完整重建",
|
||||
"actionRefreshLower": "重新整理",
|
||||
"actionRebuildLower": "重建",
|
||||
"stages": {
|
||||
"scan_folders": "正在掃描資料夾...",
|
||||
"count_models": "找到 {total} 個檔案",
|
||||
"process_models": "正在處理模型",
|
||||
"reconcile_scan": "正在檢查變更...",
|
||||
"process_new": "正在處理新模型",
|
||||
"finalizing": "正在收尾..."
|
||||
},
|
||||
"eta": {
|
||||
"lessThanMinute": "剩餘時間不到一分鐘",
|
||||
"minutes": "剩餘約 {minutes} 分鐘",
|
||||
"hours": "剩餘約 {hours} 小時 {minutes} 分鐘"
|
||||
}
|
||||
}
|
||||
},
|
||||
"onboarding": {
|
||||
@@ -1593,7 +1614,11 @@
|
||||
"clipSkip": "Clip Skip",
|
||||
"valuePlaceholder": "數值",
|
||||
"add": "新增",
|
||||
"invalidRange": "無效的範圍格式。請使用 x.x-y.y"
|
||||
"invalidRange": "無效的範圍格式。請使用 x.x-y.y",
|
||||
"invalidValue": "請輸入有效的數值",
|
||||
"saveFailed": "儲存預設參數失敗",
|
||||
"added": "已新增預設參數",
|
||||
"updated": "已更新預設參數"
|
||||
},
|
||||
"triggerWords": {
|
||||
"label": "觸發詞",
|
||||
|
||||
@@ -1128,15 +1128,21 @@ class RecipeManagementHandler:
|
||||
image_id = extract_civitai_image_id(source_path) if source_path else None
|
||||
|
||||
# Local re-import sources: an explicit local source_path, or — when
|
||||
# no source_path was recorded (drag & drop / file-picker imports) —
|
||||
# the recipe's own saved image, which still carries the original
|
||||
# no usable source_path was recorded (drag & drop / file-picker
|
||||
# imports, or a dangling path left by an earlier re-import) — the
|
||||
# recipe's own saved image, which still carries the original
|
||||
# embedded generation metadata next to the recipe metadata block.
|
||||
# In the fallback case nothing is persisted as source_path: the
|
||||
# recipe's own previous preview is not an external source, and it
|
||||
# is deleted together with the old recipe below.
|
||||
local_source = None
|
||||
persisted_source_path = ""
|
||||
if not image_id and source_path and os.path.isfile(source_path):
|
||||
local_source = source_path
|
||||
persisted_source_path = source_path
|
||||
elif (
|
||||
not image_id
|
||||
and not source_path
|
||||
and not source_path.startswith(("http://", "https://"))
|
||||
and old_file_path
|
||||
and os.path.isfile(old_file_path)
|
||||
):
|
||||
@@ -1170,6 +1176,7 @@ class RecipeManagementHandler:
|
||||
target_dir=old_folder,
|
||||
user_edits=user_edits,
|
||||
old_title=old_recipe.get("title", ""),
|
||||
persisted_source_path=persisted_source_path,
|
||||
)
|
||||
|
||||
async with self._import_semaphore:
|
||||
@@ -2512,6 +2519,7 @@ class RecipeManagementHandler:
|
||||
target_dir: str | None,
|
||||
user_edits: dict[str, Any],
|
||||
old_title: str,
|
||||
persisted_source_path: str,
|
||||
) -> web.Response:
|
||||
"""Re-import a recipe from a local image file.
|
||||
|
||||
@@ -2519,6 +2527,12 @@ class RecipeManagementHandler:
|
||||
generation metadata (the appended recipe metadata block is ignored so
|
||||
the current parser gets a fresh pass), saves a new recipe, then deletes
|
||||
the old one.
|
||||
|
||||
``persisted_source_path`` is the source_path recorded on the new
|
||||
recipe: the external source file when one exists, or empty when the
|
||||
re-import fell back to the recipe's own previous preview image (that
|
||||
file is deleted with the old recipe, so recording it would leave a
|
||||
dangling path that blocks future re-imports).
|
||||
"""
|
||||
normalized = os.path.normpath(file_path)
|
||||
if not os.path.isfile(normalized):
|
||||
@@ -2547,7 +2561,7 @@ class RecipeManagementHandler:
|
||||
"base_model": base_model,
|
||||
"loras": loras,
|
||||
"gen_params": gen_params,
|
||||
"source_path": normalized,
|
||||
"source_path": persisted_source_path,
|
||||
}
|
||||
if checkpoint:
|
||||
metadata["checkpoint"] = checkpoint
|
||||
@@ -2610,7 +2624,7 @@ class RecipeManagementHandler:
|
||||
"success": True,
|
||||
"old_recipe_id": recipe_id,
|
||||
"recipe_id": new_recipe_id,
|
||||
"source_path": normalized,
|
||||
"source_path": persisted_source_path,
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
+137
-16
@@ -66,6 +66,14 @@ def _is_hidden_relative_path(rel_path: str) -> bool:
|
||||
# requests (modal open + autocomplete) do not re-walk the model roots.
|
||||
ALL_FOLDERS_CACHE_TTL_SECONDS = 5.0
|
||||
|
||||
# Maps a scanner model type to the manager page type used in progress
|
||||
# broadcasts (e.g. 'lora' -> 'loras').
|
||||
PAGE_TYPE_MAP = {
|
||||
'lora': 'loras',
|
||||
'checkpoint': 'checkpoints',
|
||||
'embedding': 'embeddings',
|
||||
}
|
||||
|
||||
|
||||
def _is_pending_delete_path(path: str) -> bool:
|
||||
"""Return True when any path component is the pending-delete staging dir."""
|
||||
@@ -149,6 +157,38 @@ class ModelScanner:
|
||||
# Register this service
|
||||
asyncio.create_task(self._register_service())
|
||||
|
||||
@property
|
||||
def page_type(self) -> str:
|
||||
"""Manager page type used in progress broadcasts (e.g. 'loras')."""
|
||||
return PAGE_TYPE_MAP.get(self.model_type, self.model_type)
|
||||
|
||||
async def _broadcast_scan_progress(
|
||||
self,
|
||||
status: str,
|
||||
stage: str,
|
||||
progress: int,
|
||||
full_rebuild: bool,
|
||||
**extra: Any,
|
||||
) -> None:
|
||||
"""Broadcast manual-refresh scan progress on the generic WS channel.
|
||||
|
||||
Best-effort only: broadcast failures must never affect the scan itself.
|
||||
"""
|
||||
payload: Dict[str, Any] = {
|
||||
'type': 'scan_progress',
|
||||
'status': status,
|
||||
'model_type': self.model_type,
|
||||
'pageType': self.page_type,
|
||||
'stage': stage,
|
||||
'full_rebuild': full_rebuild,
|
||||
'progress': progress,
|
||||
}
|
||||
payload.update(extra)
|
||||
try:
|
||||
await ws_manager.broadcast(payload)
|
||||
except Exception as exc: # pragma: no cover - defensive logging
|
||||
logger.error(f"Error broadcasting scan progress for {self.model_type}: {exc}")
|
||||
|
||||
@property
|
||||
def cache_version(self) -> int:
|
||||
"""Monotonic version counter for the in-memory cache.
|
||||
@@ -434,12 +474,7 @@ class ModelScanner:
|
||||
self._is_initializing = True
|
||||
|
||||
# Determine the page type based on model type
|
||||
page_type_map = {
|
||||
'lora': 'loras',
|
||||
'checkpoint': 'checkpoints',
|
||||
'embedding': 'embeddings'
|
||||
}
|
||||
page_type = page_type_map.get(self.model_type, self.model_type)
|
||||
page_type = self.page_type
|
||||
|
||||
# First, try to load from cache
|
||||
await ws_manager.broadcast_init_progress({
|
||||
@@ -804,7 +839,7 @@ class ModelScanner:
|
||||
last_progress_time = time.time()
|
||||
last_progress_percent = 0
|
||||
|
||||
async def progress_callback(processed_files: int, expected_total: int) -> None:
|
||||
async def progress_callback(processed_files: int, expected_total: int, current_name: str = '') -> None:
|
||||
nonlocal last_progress_time, last_progress_percent
|
||||
|
||||
if expected_total <= 0:
|
||||
@@ -871,32 +906,84 @@ class ModelScanner:
|
||||
async def _initialize_cache(self) -> None:
|
||||
"""Initialize or refresh the cache"""
|
||||
self._is_initializing = True # Set flag
|
||||
last_progress_percent = 0
|
||||
try:
|
||||
start_time = time.time()
|
||||
|
||||
|
||||
await self._broadcast_scan_progress('started', 'scan_folders', 0, True)
|
||||
|
||||
# Manually trigger a symlink rescan during a full rebuild.
|
||||
# This ensures that any new symlink mappings are correctly picked up.
|
||||
config.rebuild_symlink_cache()
|
||||
|
||||
# Determine the page type based on model type
|
||||
# Count files in a thread so the event loop stays responsive
|
||||
loop = asyncio.get_running_loop()
|
||||
total_files = await loop.run_in_executor(None, self._count_model_files)
|
||||
await self._broadcast_scan_progress(
|
||||
'processing', 'count_models', 1, True,
|
||||
processed=0, total=total_files,
|
||||
)
|
||||
|
||||
last_progress_time = time.time()
|
||||
|
||||
async def progress_callback(processed_files: int, expected_total: int, current_name: str = '') -> None:
|
||||
nonlocal last_progress_time, last_progress_percent
|
||||
|
||||
if expected_total <= 0:
|
||||
return
|
||||
|
||||
current_time = time.time()
|
||||
progress_percent = min(99, int(1 + (processed_files / expected_total) * 98))
|
||||
|
||||
if progress_percent <= last_progress_percent:
|
||||
return
|
||||
|
||||
if current_time - last_progress_time <= 0.5 and processed_files != expected_total:
|
||||
return
|
||||
|
||||
last_progress_percent = progress_percent
|
||||
last_progress_time = current_time
|
||||
|
||||
await self._broadcast_scan_progress(
|
||||
'processing', 'process_models', progress_percent, True,
|
||||
processed=processed_files, total=expected_total,
|
||||
current_name=current_name,
|
||||
)
|
||||
|
||||
# Scan for new data
|
||||
scan_result = await self._gather_model_data()
|
||||
scan_result = await self._gather_model_data(
|
||||
total_files=total_files,
|
||||
progress_callback=progress_callback,
|
||||
)
|
||||
if not self.is_cancelled():
|
||||
await self._broadcast_scan_progress('finalizing', 'finalizing', 99, True)
|
||||
await self._apply_scan_result(scan_result)
|
||||
await self._save_persistent_cache(scan_result)
|
||||
await self._sync_download_history(scan_result.raw_data, source='scan')
|
||||
await self._broadcast_scan_progress(
|
||||
'completed', 'finalizing', 100, True,
|
||||
elapsed_seconds=time.time() - start_time,
|
||||
)
|
||||
|
||||
logger.info(
|
||||
f"{self.model_type.capitalize()} Scanner: Cache initialization completed in {time.time() - start_time:.2f} seconds, "
|
||||
f"found {len(scan_result.raw_data)} models"
|
||||
)
|
||||
else:
|
||||
await self._broadcast_scan_progress(
|
||||
'cancelled', 'process_models', last_progress_percent, True,
|
||||
elapsed_seconds=time.time() - start_time,
|
||||
)
|
||||
logger.info(
|
||||
f"{self.model_type.capitalize()} Scanner: Cache initialization cancelled "
|
||||
f"after {time.time() - start_time:.2f} seconds"
|
||||
)
|
||||
except Exception as e:
|
||||
logger.error(f"{self.model_type.capitalize()} Scanner: Error initializing cache: {e}")
|
||||
await self._broadcast_scan_progress(
|
||||
'error', 'process_models', last_progress_percent, True,
|
||||
error=str(e),
|
||||
)
|
||||
# Ensure cache is at least an empty structure on error
|
||||
if self._cache is None:
|
||||
self._cache = ModelCache(
|
||||
@@ -914,6 +1001,8 @@ class ModelScanner:
|
||||
try:
|
||||
start_time = time.time()
|
||||
logger.info(f"{self.model_type.capitalize()} Scanner: Starting fast cache reconciliation...")
|
||||
|
||||
await self._broadcast_scan_progress('started', 'reconcile_scan', 0, False)
|
||||
|
||||
# Get current cached file paths
|
||||
cached_paths = {item['file_path'] for item in self._cache.raw_data}
|
||||
@@ -987,6 +1076,10 @@ class ModelScanner:
|
||||
await asyncio.sleep(0)
|
||||
if self.is_cancelled():
|
||||
logger.info(f"{self.model_type.capitalize()} Scanner: Reconcile scan cancelled")
|
||||
await self._broadcast_scan_progress(
|
||||
'cancelled', 'reconcile_scan', 0, False,
|
||||
elapsed_seconds=time.time() - start_time,
|
||||
)
|
||||
return
|
||||
|
||||
# Process new files in batches
|
||||
@@ -994,10 +1087,14 @@ class ModelScanner:
|
||||
if new_files:
|
||||
logger.info(f"{self.model_type.capitalize()} Scanner: Found {len(new_files)} new files to process")
|
||||
batch_size = 50
|
||||
for i in range(0, len(new_files), batch_size):
|
||||
total_new = len(new_files)
|
||||
processed_new = 0
|
||||
last_progress_time = time.time()
|
||||
for i in range(0, total_new, batch_size):
|
||||
batch = new_files[i:i+batch_size]
|
||||
for path in batch:
|
||||
logger.info(f"{self.model_type.capitalize()} Scanner: Processing {path}")
|
||||
processed_new += 1
|
||||
try:
|
||||
# Find the appropriate root path for this file
|
||||
root_path = None
|
||||
@@ -1053,9 +1150,24 @@ class ModelScanner:
|
||||
logger.error(f"Could not determine root path for {path}")
|
||||
except Exception as e:
|
||||
logger.error(f"Error adding {path} to cache: {e}")
|
||||
|
||||
|
||||
current_time = time.time()
|
||||
if current_time - last_progress_time > 0.5 or processed_new == total_new:
|
||||
last_progress_time = current_time
|
||||
await self._broadcast_scan_progress(
|
||||
'processing', 'process_new',
|
||||
min(99, int(1 + (processed_new / total_new) * 98)), False,
|
||||
processed=processed_new, total=total_new,
|
||||
current_name=os.path.basename(path),
|
||||
)
|
||||
|
||||
if self.is_cancelled():
|
||||
logger.info(f"{self.model_type.capitalize()} Scanner: Reconcile processing cancelled")
|
||||
await self._broadcast_scan_progress(
|
||||
'cancelled', 'process_new',
|
||||
min(99, int(1 + (processed_new / total_new) * 98)), False,
|
||||
elapsed_seconds=time.time() - start_time,
|
||||
)
|
||||
return
|
||||
|
||||
# Find missing files (in cache but not in filesystem)
|
||||
@@ -1121,8 +1233,17 @@ class ModelScanner:
|
||||
await self._persist_current_cache()
|
||||
|
||||
logger.info(f"{self.model_type.capitalize()} Scanner: Cache reconciliation completed in {time.time() - start_time:.2f} seconds. Added {total_added}, removed {total_removed} models.")
|
||||
await self._broadcast_scan_progress(
|
||||
'completed', 'process_new', 100, False,
|
||||
added=total_added, removed=total_removed,
|
||||
elapsed_seconds=time.time() - start_time,
|
||||
)
|
||||
except Exception as e:
|
||||
logger.error(f"{self.model_type.capitalize()} Scanner: Error reconciling cache: {e}", exc_info=True)
|
||||
await self._broadcast_scan_progress(
|
||||
'error', 'reconcile_scan', 0, False,
|
||||
error=str(e),
|
||||
)
|
||||
finally:
|
||||
self._is_initializing = False # Unset flag
|
||||
self.bump_cache_version()
|
||||
@@ -1498,7 +1619,7 @@ class ModelScanner:
|
||||
self,
|
||||
*,
|
||||
total_files: int = 0,
|
||||
progress_callback: Optional[Callable[[int, int], Awaitable[None]]] = None
|
||||
progress_callback: Optional[Callable[[int, int, str], Awaitable[None]]] = None
|
||||
) -> CacheBuildResult:
|
||||
"""Collect metadata for all model files."""
|
||||
|
||||
@@ -1510,11 +1631,11 @@ class ModelScanner:
|
||||
processed_real_files: Set[str] = set()
|
||||
visited_real_dirs: Set[str] = set()
|
||||
|
||||
async def handle_progress() -> None:
|
||||
async def handle_progress(current_name: str = '') -> None:
|
||||
if progress_callback is None:
|
||||
return
|
||||
try:
|
||||
await progress_callback(processed_files, total_files)
|
||||
await progress_callback(processed_files, total_files, current_name)
|
||||
except Exception as exc: # pragma: no cover - defensive logging
|
||||
logger.error(f"Error reporting progress for {self.model_type}: {exc}")
|
||||
|
||||
@@ -1580,7 +1701,7 @@ class ModelScanner:
|
||||
for tag in result.get('tags') or []:
|
||||
tags_count[tag] = tags_count.get(tag, 0) + 1
|
||||
|
||||
await handle_progress()
|
||||
await handle_progress(entry.name)
|
||||
await asyncio.sleep(0)
|
||||
if self.is_cancelled():
|
||||
return
|
||||
|
||||
@@ -1753,7 +1753,36 @@ class RecipeScanner:
|
||||
# Mark initialization as complete regardless of outcome
|
||||
self._is_initializing = False
|
||||
|
||||
def _initialize_recipe_cache_sync(self):
|
||||
async def _broadcast_scan_progress(
|
||||
self,
|
||||
status: str,
|
||||
stage: str,
|
||||
progress: int,
|
||||
full_rebuild: bool,
|
||||
**extra: Any,
|
||||
) -> None:
|
||||
"""Broadcast manual-refresh scan progress on the generic WS channel.
|
||||
|
||||
Mirrors ``ModelScanner._broadcast_scan_progress`` so the recipes page
|
||||
can reuse the same frontend contract. Best-effort only: broadcast
|
||||
failures must never affect the scan itself.
|
||||
"""
|
||||
payload: Dict[str, Any] = {
|
||||
'type': 'scan_progress',
|
||||
'status': status,
|
||||
'model_type': 'recipe',
|
||||
'pageType': 'recipes',
|
||||
'stage': stage,
|
||||
'full_rebuild': full_rebuild,
|
||||
'progress': progress,
|
||||
}
|
||||
payload.update(extra)
|
||||
try:
|
||||
await ws_manager.broadcast(payload)
|
||||
except Exception as exc: # pragma: no cover - defensive logging
|
||||
logger.error(f"Error broadcasting scan progress for recipe: {exc}")
|
||||
|
||||
def _initialize_recipe_cache_sync(self, report_progress: bool = False):
|
||||
"""Synchronous version of recipe cache initialization for thread pool execution.
|
||||
|
||||
Uses persistent cache for fast startup when available:
|
||||
@@ -1761,8 +1790,14 @@ class RecipeScanner:
|
||||
2. Reconcile with filesystem (check mtime/size for changes)
|
||||
3. Fall back to full directory scan if cache miss or reconciliation fails
|
||||
4. Persist results for next startup
|
||||
|
||||
Args:
|
||||
report_progress: When True (manual force-refresh only), broadcast
|
||||
scan_progress messages during the full directory scan. Startup
|
||||
initialization leaves this False and behaves as before.
|
||||
"""
|
||||
loop = None
|
||||
scan_start_time: Optional[float] = None
|
||||
try:
|
||||
# Ensure cache exists to avoid None reference errors
|
||||
if self._cache is None:
|
||||
@@ -1844,7 +1879,17 @@ class RecipeScanner:
|
||||
|
||||
# Fall back to full directory scan
|
||||
logger.info("Recipe cache miss: performing full directory scan")
|
||||
recipes, json_paths = self._full_directory_scan_sync(recipes_dir)
|
||||
if report_progress:
|
||||
scan_start_time = time.time()
|
||||
# Broadcast from the worker thread via its own event loop,
|
||||
# mirroring ModelScanner._initialize_cache_sync.
|
||||
loop.run_until_complete(
|
||||
self._broadcast_scan_progress('started', 'scan_folders', 0, True)
|
||||
)
|
||||
recipes, json_paths = self._full_directory_scan_sync(
|
||||
recipes_dir,
|
||||
progress_loop=loop if report_progress else None,
|
||||
)
|
||||
self._json_path_map = json_paths
|
||||
|
||||
# Update cache with the collected data
|
||||
@@ -1858,12 +1903,30 @@ class RecipeScanner:
|
||||
recipes, json_paths, self._cache.image_id_map
|
||||
)
|
||||
|
||||
if report_progress:
|
||||
loop.run_until_complete(
|
||||
self._broadcast_scan_progress(
|
||||
'completed', 'finalizing', 100, True,
|
||||
elapsed_seconds=time.time() - (scan_start_time or time.time()),
|
||||
total=len(recipes),
|
||||
)
|
||||
)
|
||||
|
||||
return self._cache
|
||||
except Exception as e:
|
||||
logger.error(f"Error in thread-based recipe cache initialization: {e}")
|
||||
import traceback
|
||||
|
||||
traceback.print_exc(file=sys.stderr)
|
||||
if report_progress and loop is not None:
|
||||
try:
|
||||
loop.run_until_complete(
|
||||
self._broadcast_scan_progress(
|
||||
'error', 'process_models', 0, True, error=str(e)
|
||||
)
|
||||
)
|
||||
except Exception: # pragma: no cover - defensive logging
|
||||
logger.error("Error broadcasting recipe scan failure", exc_info=True)
|
||||
return self._cache if hasattr(self, "_cache") else None
|
||||
finally:
|
||||
# Clean up the event loop
|
||||
@@ -2017,12 +2080,16 @@ class RecipeScanner:
|
||||
return updated
|
||||
|
||||
def _full_directory_scan_sync(
|
||||
self, recipes_dir: str
|
||||
self,
|
||||
recipes_dir: str,
|
||||
progress_loop: Optional[asyncio.AbstractEventLoop] = None,
|
||||
) -> Tuple[List[Dict[str, Any]], Dict[str, str]]:
|
||||
"""Perform a full synchronous directory scan for recipes.
|
||||
|
||||
Args:
|
||||
recipes_dir: Path to the recipes directory.
|
||||
progress_loop: When set (manual force-refresh only), broadcast
|
||||
scan_progress messages through this thread-local event loop.
|
||||
|
||||
Returns:
|
||||
Tuple of (recipes list, json_paths dict).
|
||||
@@ -2037,6 +2104,17 @@ class RecipeScanner:
|
||||
if file.lower().endswith(".recipe.json"):
|
||||
recipe_files.append(os.path.join(root, file))
|
||||
|
||||
total_files = len(recipe_files)
|
||||
if progress_loop is not None:
|
||||
progress_loop.run_until_complete(
|
||||
self._broadcast_scan_progress(
|
||||
'processing', 'count_models', 1, True,
|
||||
processed=0, total=total_files,
|
||||
)
|
||||
)
|
||||
|
||||
last_progress_time = time.time()
|
||||
|
||||
# Process each recipe file
|
||||
for i, recipe_path in enumerate(recipe_files):
|
||||
recipe_data = self._load_recipe_file_sync(recipe_path)
|
||||
@@ -2044,6 +2122,23 @@ class RecipeScanner:
|
||||
recipe_id = str(recipe_data.get("id", ""))
|
||||
recipes.append(recipe_data)
|
||||
json_paths[recipe_id] = recipe_path
|
||||
if progress_loop is not None and total_files > 0:
|
||||
processed = i + 1
|
||||
current_time = time.time()
|
||||
# Throttle to one update per 0.5s; always send the final one.
|
||||
if (
|
||||
processed == total_files
|
||||
or current_time - last_progress_time > 0.5
|
||||
):
|
||||
last_progress_time = current_time
|
||||
progress_percent = min(99, int(1 + (processed / total_files) * 98))
|
||||
progress_loop.run_until_complete(
|
||||
self._broadcast_scan_progress(
|
||||
'processing', 'process_models', progress_percent, True,
|
||||
processed=processed, total=total_files,
|
||||
current_name=os.path.basename(recipe_path),
|
||||
)
|
||||
)
|
||||
# Periodically release GIL so the event loop thread can run
|
||||
if i % 100 == 0:
|
||||
time.sleep(0)
|
||||
@@ -2613,11 +2708,14 @@ class RecipeScanner:
|
||||
start_time = time.time()
|
||||
|
||||
# Run the heavy lifting in a thread pool – same path
|
||||
# used by initialize_in_background().
|
||||
# used by initialize_in_background(). Pass
|
||||
# report_progress=True so manual refreshes broadcast
|
||||
# scan_progress updates; startup init keeps it off.
|
||||
loop = asyncio.get_event_loop()
|
||||
cache = await loop.run_in_executor(
|
||||
None,
|
||||
self._initialize_recipe_cache_sync,
|
||||
True,
|
||||
)
|
||||
if cache is not None:
|
||||
self._cache = cache
|
||||
|
||||
@@ -65,4 +65,13 @@
|
||||
|
||||
.add-preset-btn:hover {
|
||||
opacity: 0.9;
|
||||
}
|
||||
|
||||
.add-preset-btn:disabled {
|
||||
opacity: 0.5;
|
||||
cursor: not-allowed;
|
||||
}
|
||||
|
||||
.add-preset-btn:hover:disabled {
|
||||
opacity: 0.5;
|
||||
}
|
||||
@@ -12,6 +12,11 @@ import {
|
||||
} from './apiConfig.js';
|
||||
import { resetAndReload } from './modelApiFactory.js';
|
||||
import { sidebarManager } from '../components/SidebarManager.js';
|
||||
// Shared scan ETA helpers live in a dependency-light module so pages that do
|
||||
// not use BaseModelApiClient (e.g. recipes) can reuse them without pulling
|
||||
// this module's import cycle (modelApiFactory -> loraApi -> baseModelApi).
|
||||
import { createScanEtaTracker, formatScanRemainingTime } from '../utils/scanEtaUtils.js';
|
||||
export { createScanEtaTracker, formatScanRemainingTime };
|
||||
|
||||
/**
|
||||
* Abstract base class for all model API clients
|
||||
@@ -507,23 +512,67 @@ export class BaseModelApiClient {
|
||||
|
||||
async refreshModels(fullRebuild = false) {
|
||||
const abortController = new AbortController();
|
||||
try {
|
||||
state.loadingManager.show(
|
||||
`${fullRebuild ? 'Full rebuild' : 'Refreshing'} ${this.apiConfig.config.displayName}s...`,
|
||||
0
|
||||
const displayName = this.apiConfig.config.displayName;
|
||||
const singularName = this.apiConfig.config.singularName;
|
||||
const actionText = translate(
|
||||
fullRebuild ? 'common.scanProgress.actionFullRebuild' : 'common.scanProgress.actionRefresh',
|
||||
{},
|
||||
fullRebuild ? 'Full rebuild' : 'Refresh'
|
||||
);
|
||||
const actionLowerText = translate(
|
||||
fullRebuild ? 'common.scanProgress.actionRebuildLower' : 'common.scanProgress.actionRefreshLower',
|
||||
{},
|
||||
fullRebuild ? 'rebuild' : 'refresh'
|
||||
);
|
||||
const initialMessage = translate(
|
||||
fullRebuild ? 'common.scanProgress.fullRebuilding' : 'common.scanProgress.refreshing',
|
||||
{ type: displayName },
|
||||
`${fullRebuild ? 'Full rebuild' : 'Refreshing'} ${displayName}s...`
|
||||
);
|
||||
const etaTracker = createScanEtaTracker();
|
||||
let ws = null;
|
||||
|
||||
const handleScanProgress = (data) => {
|
||||
if (typeof data.progress === 'number') {
|
||||
state.loadingManager.setProgress(data.progress);
|
||||
}
|
||||
let statusText = translate(
|
||||
`common.scanProgress.stages.${data.stage}`,
|
||||
{ total: data.total },
|
||||
data.stage || ''
|
||||
);
|
||||
if (data.status === 'processing' && data.total > 0) {
|
||||
statusText += ` (${data.processed}/${data.total})`;
|
||||
if (data.current_name) {
|
||||
statusText += ` ${data.current_name}`;
|
||||
}
|
||||
const etaText = etaTracker.update(data.processed, data.total);
|
||||
if (etaText) {
|
||||
statusText += ` | ${etaText}`;
|
||||
}
|
||||
}
|
||||
state.loadingManager.setStatus(statusText);
|
||||
};
|
||||
|
||||
try {
|
||||
state.loadingManager.show(initialMessage, 0);
|
||||
state.loadingManager.showCancelButton(() => {
|
||||
this.cancelTask();
|
||||
abortController.abort();
|
||||
});
|
||||
|
||||
// Connect to the shared progress channel for live scan updates.
|
||||
// Failure to connect must not block the refresh itself — fall back
|
||||
// to the plain loading indicator.
|
||||
ws = await this._connectScanProgressSocket(handleScanProgress, singularName);
|
||||
|
||||
const url = new URL(this.apiConfig.endpoints.scan, window.location.origin);
|
||||
url.searchParams.append('full_rebuild', fullRebuild);
|
||||
|
||||
const response = await fetch(url, { signal: abortController.signal });
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`Failed to refresh ${this.apiConfig.config.displayName}s: ${response.status} ${response.statusText}`);
|
||||
throw new Error(`Failed to refresh ${displayName}s: ${response.status} ${response.statusText}`);
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
@@ -534,20 +583,69 @@ export class BaseModelApiClient {
|
||||
|
||||
resetAndReload(true);
|
||||
|
||||
showToast('toast.api.refreshComplete', { action: fullRebuild ? 'Full rebuild' : 'Refresh' }, 'success');
|
||||
showToast('toast.api.refreshComplete', { action: actionText }, 'success');
|
||||
} catch (error) {
|
||||
if (error.name === 'AbortError') {
|
||||
showToast('toast.api.operationCancelled', {}, 'info');
|
||||
return;
|
||||
}
|
||||
console.error('Refresh failed:', error);
|
||||
showToast('toast.api.refreshFailed', { action: fullRebuild ? 'rebuild' : 'refresh', type: this.apiConfig.config.displayName }, 'error');
|
||||
showToast('toast.api.refreshFailed', { action: actionLowerText, type: displayName }, 'error');
|
||||
} finally {
|
||||
if (ws) {
|
||||
ws.close();
|
||||
}
|
||||
state.loadingManager.hide();
|
||||
state.loadingManager.restoreProgressBar();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Connect to the shared fetch-progress WebSocket for scan progress updates.
|
||||
* Returns null when the connection cannot be established (silent fallback).
|
||||
* @param {Function} onScanProgress - Handler for scan_progress messages
|
||||
* @param {string} singularName - Model type filter (e.g. 'lora')
|
||||
* @returns {Promise<WebSocket|null>}
|
||||
*/
|
||||
async _connectScanProgressSocket(onScanProgress, singularName) {
|
||||
let socket = null;
|
||||
try {
|
||||
const wsProtocol = window.location.protocol === 'https:' ? 'wss://' : 'ws://';
|
||||
socket = new WebSocket(`${wsProtocol}${window.location.host}${WS_ENDPOINTS.fetchProgress}`);
|
||||
|
||||
await new Promise((resolve, reject) => {
|
||||
socket.onopen = resolve;
|
||||
socket.onerror = reject;
|
||||
});
|
||||
|
||||
socket.onmessage = (event) => {
|
||||
let data;
|
||||
try {
|
||||
data = JSON.parse(event.data);
|
||||
} catch (parseError) {
|
||||
return;
|
||||
}
|
||||
// Only handle scan progress for this client's model type;
|
||||
// other operations share this channel and must be ignored.
|
||||
if (data.type !== 'scan_progress' || data.model_type !== singularName) {
|
||||
return;
|
||||
}
|
||||
onScanProgress(data);
|
||||
};
|
||||
|
||||
return socket;
|
||||
} catch (error) {
|
||||
if (socket) {
|
||||
try {
|
||||
socket.close();
|
||||
} catch (closeError) {
|
||||
// Ignore close errors during fallback
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
async refreshSingleModelMetadata(filePath) {
|
||||
try {
|
||||
state.loadingManager.showSimpleLoading('Refreshing metadata...');
|
||||
@@ -605,6 +703,9 @@ export class BaseModelApiClient {
|
||||
ws.onmessage = (event) => {
|
||||
const data = JSON.parse(event.data);
|
||||
|
||||
// Scan progress shares this channel; it is handled by refreshModels
|
||||
if (data.type === 'scan_progress') return;
|
||||
|
||||
switch (data.status) {
|
||||
case 'started':
|
||||
loading.setStatus('Starting metadata fetch...');
|
||||
|
||||
+100
-5
@@ -1,7 +1,12 @@
|
||||
import { RecipeCard } from '../components/RecipeCard.js';
|
||||
import { state, getCurrentPageState } from '../state/index.js';
|
||||
import { showToast } from '../utils/uiHelpers.js';
|
||||
import { translate } from '../utils/i18nHelpers.js';
|
||||
import { captureScrollPosition, restoreScrollPosition } from '../utils/infiniteScroll.js';
|
||||
import { WS_ENDPOINTS } from './apiConfig.js';
|
||||
// Import from the dependency-light utils module, not baseModelApi.js, to
|
||||
// avoid the baseModelApi <-> modelApiFactory import cycle on this page.
|
||||
import { createScanEtaTracker } from '../utils/scanEtaUtils.js';
|
||||
|
||||
const RECIPE_ENDPOINTS = {
|
||||
list: '/api/lm/recipes',
|
||||
@@ -333,11 +338,53 @@ export async function syncChanges() {
|
||||
}
|
||||
|
||||
export async function refreshRecipes(fullRebuild = true) {
|
||||
const actionLabel = fullRebuild ? 'Rebuilding recipe cache' : 'Refreshing recipes';
|
||||
const actionToast = fullRebuild ? 'Full rebuild' : 'Refresh';
|
||||
const actionText = translate(
|
||||
fullRebuild ? 'common.scanProgress.actionFullRebuild' : 'common.scanProgress.actionRefresh',
|
||||
{},
|
||||
fullRebuild ? 'Full rebuild' : 'Refresh'
|
||||
);
|
||||
const actionLowerText = translate(
|
||||
fullRebuild ? 'common.scanProgress.actionRebuildLower' : 'common.scanProgress.actionRefreshLower',
|
||||
{},
|
||||
fullRebuild ? 'rebuild' : 'refresh'
|
||||
);
|
||||
const initialMessage = translate(
|
||||
fullRebuild ? 'common.scanProgress.fullRebuilding' : 'common.scanProgress.refreshing',
|
||||
{ type: RECIPE_SIDEBAR_CONFIG.config.displayName },
|
||||
`${fullRebuild ? 'Full rebuild' : 'Refreshing'} Recipes...`
|
||||
);
|
||||
const etaTracker = createScanEtaTracker();
|
||||
let ws = null;
|
||||
|
||||
const handleScanProgress = (data) => {
|
||||
if (typeof data.progress === 'number') {
|
||||
state.loadingManager.setProgress(data.progress);
|
||||
}
|
||||
let statusText = translate(
|
||||
`common.scanProgress.stages.${data.stage}`,
|
||||
{ total: data.total },
|
||||
data.stage || ''
|
||||
);
|
||||
if (data.status === 'processing' && data.total > 0) {
|
||||
statusText += ` (${data.processed}/${data.total})`;
|
||||
if (data.current_name) {
|
||||
statusText += ` ${data.current_name}`;
|
||||
}
|
||||
const etaText = etaTracker.update(data.processed, data.total);
|
||||
if (etaText) {
|
||||
statusText += ` | ${etaText}`;
|
||||
}
|
||||
}
|
||||
state.loadingManager.setStatus(statusText);
|
||||
};
|
||||
|
||||
try {
|
||||
state.loadingManager.show(`${actionLabel}...`, 0);
|
||||
state.loadingManager.show(initialMessage, 0);
|
||||
|
||||
// Connect to the shared progress channel for live scan updates.
|
||||
// Failure to connect must not block the refresh itself — fall back
|
||||
// to the plain loading indicator.
|
||||
ws = await connectScanProgressSocket(handleScanProgress);
|
||||
|
||||
const url = new URL(RECIPE_ENDPOINTS.scan, window.location.origin);
|
||||
url.searchParams.append('full_rebuild', fullRebuild);
|
||||
@@ -356,16 +403,64 @@ export async function refreshRecipes(fullRebuild = true) {
|
||||
|
||||
await resetAndReload(false);
|
||||
|
||||
showToast('toast.api.refreshComplete', { action: actionToast }, 'success');
|
||||
showToast('toast.api.refreshComplete', { action: actionText }, 'success');
|
||||
} catch (error) {
|
||||
console.error('Error refreshing recipes:', error);
|
||||
showToast('toast.api.refreshFailed', { action: fullRebuild ? 'rebuild' : 'refresh', type: 'recipe' }, 'error');
|
||||
showToast('toast.api.refreshFailed', { action: actionLowerText, type: 'recipe' }, 'error');
|
||||
} finally {
|
||||
if (ws) {
|
||||
ws.close();
|
||||
}
|
||||
state.loadingManager.hide();
|
||||
state.loadingManager.restoreProgressBar();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Connect to the shared fetch-progress WebSocket for recipe scan progress.
|
||||
* Returns null when the connection cannot be established (silent fallback).
|
||||
* @param {Function} onScanProgress - Handler for scan_progress messages
|
||||
* @returns {Promise<WebSocket|null>}
|
||||
*/
|
||||
async function connectScanProgressSocket(onScanProgress) {
|
||||
let socket = null;
|
||||
try {
|
||||
const wsProtocol = window.location.protocol === 'https:' ? 'wss://' : 'ws://';
|
||||
socket = new WebSocket(`${wsProtocol}${window.location.host}${WS_ENDPOINTS.fetchProgress}`);
|
||||
|
||||
await new Promise((resolve, reject) => {
|
||||
socket.onopen = resolve;
|
||||
socket.onerror = reject;
|
||||
});
|
||||
|
||||
socket.onmessage = (event) => {
|
||||
let data;
|
||||
try {
|
||||
data = JSON.parse(event.data);
|
||||
} catch (parseError) {
|
||||
return;
|
||||
}
|
||||
// Only handle recipe scan progress; other operations share this
|
||||
// channel and must be ignored.
|
||||
if (data.type !== 'scan_progress' || data.model_type !== 'recipe') {
|
||||
return;
|
||||
}
|
||||
onScanProgress(data);
|
||||
};
|
||||
|
||||
return socket;
|
||||
} catch (error) {
|
||||
if (socket) {
|
||||
try {
|
||||
socket.close();
|
||||
} catch (closeError) {
|
||||
// Ignore close errors during fallback
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Load more recipes with pagination - updated to work with VirtualScroller
|
||||
* @param {boolean} resetPage - Whether to reset to the first page
|
||||
|
||||
@@ -877,8 +877,9 @@ function renderLoraSpecificContent(lora, escapedWords) {
|
||||
<option value="clip_strength">${translate('modals.model.usageTips.clipStrength', {}, 'Clip Strength')}</option>
|
||||
<option value="clip_skip">${translate('modals.model.usageTips.clipSkip', {}, 'Clip Skip')}</option>
|
||||
</select>
|
||||
<input type="number" id="preset-value" step="0.01" placeholder="${translate('modals.model.usageTips.valuePlaceholder', {}, 'Value')}" style="display:none;">
|
||||
<button class="add-preset-btn">${translate('modals.model.usageTips.add', {}, 'Add')}</button>
|
||||
<!-- autofill opt-out attrs prevent password managers / email-alias extensions from attaching popups -->
|
||||
<input type="number" id="preset-value" step="0.01" placeholder="${translate('modals.model.usageTips.valuePlaceholder', {}, 'Value')}" style="display:none;" autocomplete="off" data-1p-ignore data-lpignore="true" data-bwignore data-form-type="other">
|
||||
<button class="add-preset-btn" disabled>${translate('modals.model.usageTips.add', {}, 'Add')}</button>
|
||||
</div>
|
||||
<div class="preset-tags">
|
||||
${renderPresetTags(parsePresets(lora.usage_tips))}
|
||||
@@ -1086,6 +1087,11 @@ function setupLoraSpecificFields(filePath) {
|
||||
|
||||
if (!presetSelector || !presetValue || !addPresetBtn || !presetTags) return;
|
||||
|
||||
// Add button stays disabled until both a parameter and a value are provided
|
||||
const updateAddPresetButtonState = () => {
|
||||
addPresetBtn.disabled = !(presetSelector.value && presetValue.value.trim());
|
||||
};
|
||||
|
||||
presetSelector.addEventListener('change', function () {
|
||||
const selected = this.value;
|
||||
if (selected) {
|
||||
@@ -1111,12 +1117,16 @@ function setupLoraSpecificFields(filePath) {
|
||||
} else {
|
||||
presetValue.style.display = 'none';
|
||||
}
|
||||
updateAddPresetButtonState();
|
||||
});
|
||||
|
||||
presetValue.addEventListener('input', updateAddPresetButtonState);
|
||||
|
||||
addPresetBtn.addEventListener('click', async function () {
|
||||
const key = presetSelector.value;
|
||||
const value = presetValue.value;
|
||||
const value = presetValue.value.trim();
|
||||
|
||||
// Unreachable via UI while the button is disabled; kept as a safety net
|
||||
if (!key || !value) return;
|
||||
|
||||
const currentPath = resolveFilePath();
|
||||
@@ -1131,9 +1141,11 @@ function setupLoraSpecificFields(filePath) {
|
||||
document.querySelector(`.model-card[data-filepath="${escapedFilePath}"]`);
|
||||
const currentPresets = parsePresets(loraCard?.dataset.usage_tips);
|
||||
|
||||
let isUpdate;
|
||||
if (key === 'strength_range') {
|
||||
const rangeMatch = value.match(/^(-?\d*\.?\d+)\s*[-~]\s*(-?\d*\.?\d+)$/);
|
||||
if (rangeMatch) {
|
||||
isUpdate = 'strength_min' in currentPresets || 'strength_max' in currentPresets;
|
||||
currentPresets['strength_min'] = parseFloat(rangeMatch[1]);
|
||||
currentPresets['strength_max'] = parseFloat(rangeMatch[2]);
|
||||
} else {
|
||||
@@ -1141,17 +1153,36 @@ function setupLoraSpecificFields(filePath) {
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
currentPresets[key] = parseFloat(value);
|
||||
const numericValue = parseFloat(value);
|
||||
if (!Number.isFinite(numericValue)) {
|
||||
showToast('modals.model.usageTips.invalidValue', {}, 'error', 'Please enter a valid number');
|
||||
return;
|
||||
}
|
||||
isUpdate = key in currentPresets;
|
||||
currentPresets[key] = numericValue;
|
||||
}
|
||||
const newPresetsJson = JSON.stringify(currentPresets);
|
||||
|
||||
await getModelApiClient().saveModelMetadata(currentPath, { usage_tips: newPresetsJson });
|
||||
try {
|
||||
await getModelApiClient().saveModelMetadata(currentPath, { usage_tips: newPresetsJson });
|
||||
} catch (error) {
|
||||
console.error('Failed to save preset parameter:', error);
|
||||
showToast('modals.model.usageTips.saveFailed', {}, 'error', 'Failed to save preset parameter');
|
||||
return;
|
||||
}
|
||||
|
||||
presetTags.innerHTML = renderPresetTags(currentPresets);
|
||||
showToast(
|
||||
isUpdate ? 'modals.model.usageTips.updated' : 'modals.model.usageTips.added',
|
||||
{},
|
||||
'success',
|
||||
isUpdate ? 'Preset parameter updated' : 'Preset parameter added'
|
||||
);
|
||||
|
||||
presetSelector.value = '';
|
||||
presetValue.value = '';
|
||||
presetValue.style.display = 'none';
|
||||
addPresetBtn.disabled = true;
|
||||
});
|
||||
|
||||
// Add keydown event for preset value
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
import { translate } from './i18nHelpers.js';
|
||||
|
||||
/**
|
||||
* Format a remaining-time estimate for scan progress display.
|
||||
* @param {number} remainingMs - Estimated remaining time in milliseconds
|
||||
* @returns {string} Localized ETA text
|
||||
*/
|
||||
export function formatScanRemainingTime(remainingMs) {
|
||||
if (remainingMs < 60000) {
|
||||
return translate('common.scanProgress.eta.lessThanMinute', {}, 'Less than a minute remaining');
|
||||
}
|
||||
if (remainingMs < 3600000) {
|
||||
const minutes = Math.round(remainingMs / 60000);
|
||||
return translate('common.scanProgress.eta.minutes', { minutes }, `~${minutes} min remaining`);
|
||||
}
|
||||
const hours = Math.floor(remainingMs / 3600000);
|
||||
const minutes = Math.round((remainingMs % 3600000) / 60000);
|
||||
return translate('common.scanProgress.eta.hours', { hours, minutes }, `~${hours} hr ${minutes} min remaining`);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an ETA tracker for scan progress. Uses an exponential moving
|
||||
* average (0.7/0.3) over the observed per-file processing time, mirroring
|
||||
* the estimator in components/initialization.js.
|
||||
* @returns {{ update: (processed: number, total: number) => (string|null) }}
|
||||
*/
|
||||
export function createScanEtaTracker() {
|
||||
let startTime = null;
|
||||
let lastProcessed = 0;
|
||||
let averageMsPerFile = null;
|
||||
|
||||
return {
|
||||
/**
|
||||
* Update with the latest counters.
|
||||
* @returns {string|null} Localized ETA text, or null when not applicable
|
||||
*/
|
||||
update(processed, total) {
|
||||
if (!total || total <= 0 || processed >= total) {
|
||||
return null;
|
||||
}
|
||||
const now = Date.now();
|
||||
if (startTime === null) {
|
||||
// First sample only anchors the timer; not enough data yet
|
||||
startTime = now;
|
||||
lastProcessed = processed;
|
||||
return translate('initialization.estimatingTime', {}, 'Estimating time...');
|
||||
}
|
||||
if (processed > lastProcessed) {
|
||||
const msPerFile = (now - startTime) / processed;
|
||||
averageMsPerFile = averageMsPerFile === null
|
||||
? msPerFile
|
||||
: averageMsPerFile * 0.7 + msPerFile * 0.3;
|
||||
lastProcessed = processed;
|
||||
}
|
||||
if (averageMsPerFile === null) {
|
||||
return translate('initialization.estimatingTime', {}, 'Estimating time...');
|
||||
}
|
||||
return formatScanRemainingTime((total - lastProcessed) * averageMsPerFile);
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,348 @@
|
||||
import { describe, it, beforeEach, afterEach, expect, vi } from 'vitest';
|
||||
|
||||
const {
|
||||
BASE_MODEL_API_MODULE,
|
||||
STATE_MODULE,
|
||||
UI_HELPERS_MODULE,
|
||||
I18N_MODULE,
|
||||
STORAGE_MODULE,
|
||||
API_CONFIG_MODULE,
|
||||
API_FACTORY_MODULE,
|
||||
SIDEBAR_MANAGER_MODULE,
|
||||
} = vi.hoisted(() => ({
|
||||
BASE_MODEL_API_MODULE: new URL('../../../static/js/api/baseModelApi.js', import.meta.url).pathname,
|
||||
STATE_MODULE: new URL('../../../static/js/state/index.js', import.meta.url).pathname,
|
||||
UI_HELPERS_MODULE: new URL('../../../static/js/utils/uiHelpers.js', import.meta.url).pathname,
|
||||
I18N_MODULE: new URL('../../../static/js/utils/i18nHelpers.js', import.meta.url).pathname,
|
||||
STORAGE_MODULE: new URL('../../../static/js/utils/storageHelpers.js', import.meta.url).pathname,
|
||||
API_CONFIG_MODULE: new URL('../../../static/js/api/apiConfig.js', import.meta.url).pathname,
|
||||
API_FACTORY_MODULE: new URL('../../../static/js/api/modelApiFactory.js', import.meta.url).pathname,
|
||||
SIDEBAR_MANAGER_MODULE: new URL('../../../static/js/components/SidebarManager.js', import.meta.url).pathname,
|
||||
}));
|
||||
|
||||
const showToastMock = vi.fn();
|
||||
const showMock = vi.fn();
|
||||
const showCancelButtonMock = vi.fn();
|
||||
const hideMock = vi.fn();
|
||||
const restoreProgressBarMock = vi.fn();
|
||||
const setProgressMock = vi.fn();
|
||||
const setStatusMock = vi.fn();
|
||||
const resetAndReloadMock = vi.fn();
|
||||
|
||||
vi.mock(STATE_MODULE, () => ({
|
||||
state: {
|
||||
loadingManager: {
|
||||
show: showMock,
|
||||
showCancelButton: showCancelButtonMock,
|
||||
hide: hideMock,
|
||||
restoreProgressBar: restoreProgressBarMock,
|
||||
setProgress: setProgressMock,
|
||||
setStatus: setStatusMock,
|
||||
},
|
||||
},
|
||||
getCurrentPageState: vi.fn(() => ({})),
|
||||
}));
|
||||
|
||||
vi.mock(UI_HELPERS_MODULE, () => ({
|
||||
showToast: showToastMock,
|
||||
}));
|
||||
|
||||
vi.mock(I18N_MODULE, () => ({
|
||||
translate: vi.fn((key, params, fallback) => {
|
||||
if (fallback) {
|
||||
return Object.entries(params || {}).reduce(
|
||||
(text, [name, value]) => text.replaceAll(`{${name}}`, value),
|
||||
fallback
|
||||
);
|
||||
}
|
||||
return key;
|
||||
}),
|
||||
}));
|
||||
|
||||
vi.mock(STORAGE_MODULE, () => ({
|
||||
getStorageItem: vi.fn(),
|
||||
getSessionItem: vi.fn(),
|
||||
removeSessionItem: vi.fn(),
|
||||
saveMapToStorage: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock(API_CONFIG_MODULE, () => ({
|
||||
getCompleteApiConfig: vi.fn(() => ({
|
||||
endpoints: { scan: '/api/lm/loras/scan' },
|
||||
config: { displayName: 'LoRA', singularName: 'lora' },
|
||||
})),
|
||||
getCurrentModelType: vi.fn(() => 'loras'),
|
||||
isValidModelType: vi.fn(() => true),
|
||||
DOWNLOAD_ENDPOINTS: {},
|
||||
HF_ENDPOINTS: {},
|
||||
WS_ENDPOINTS: { fetchProgress: '/ws/fetch-progress' },
|
||||
}));
|
||||
|
||||
vi.mock(API_FACTORY_MODULE, () => ({
|
||||
resetAndReload: resetAndReloadMock,
|
||||
}));
|
||||
|
||||
vi.mock(SIDEBAR_MANAGER_MODULE, () => ({
|
||||
sidebarManager: { refresh: vi.fn() },
|
||||
}));
|
||||
|
||||
class FakeWebSocket {
|
||||
static instances = [];
|
||||
static failNextConnection = false;
|
||||
|
||||
constructor(url) {
|
||||
this.url = url;
|
||||
this.onopen = null;
|
||||
this.onerror = null;
|
||||
this.onmessage = null;
|
||||
this.close = vi.fn();
|
||||
FakeWebSocket.instances.push(this);
|
||||
const shouldFail = FakeWebSocket.failNextConnection;
|
||||
FakeWebSocket.failNextConnection = false;
|
||||
queueMicrotask(() => {
|
||||
if (shouldFail) {
|
||||
this.onerror?.(new Error('connection refused'));
|
||||
} else {
|
||||
this.onopen?.();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
emit(data) {
|
||||
this.onmessage?.({ data: JSON.stringify(data) });
|
||||
}
|
||||
}
|
||||
|
||||
async function createClient() {
|
||||
const { BaseModelApiClient } = await import(BASE_MODEL_API_MODULE);
|
||||
class TestClient extends BaseModelApiClient {}
|
||||
return new TestClient('loras');
|
||||
}
|
||||
|
||||
async function flushMicrotasks() {
|
||||
await new Promise((resolve) => setTimeout(resolve, 0));
|
||||
}
|
||||
|
||||
describe('BaseModelApiClient.refreshModels scan progress', () => {
|
||||
beforeEach(() => {
|
||||
showToastMock.mockReset();
|
||||
showMock.mockReset();
|
||||
showCancelButtonMock.mockReset();
|
||||
hideMock.mockReset();
|
||||
restoreProgressBarMock.mockReset();
|
||||
setProgressMock.mockReset();
|
||||
setStatusMock.mockReset();
|
||||
resetAndReloadMock.mockReset();
|
||||
FakeWebSocket.instances = [];
|
||||
FakeWebSocket.failNextConnection = false;
|
||||
vi.stubGlobal('WebSocket', FakeWebSocket);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
delete global.fetch;
|
||||
vi.unstubAllGlobals();
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
|
||||
function mockFetchPending() {
|
||||
let resolveFetch;
|
||||
global.fetch = vi.fn(() => new Promise((resolve) => { resolveFetch = resolve; }));
|
||||
return {
|
||||
resolveOk: (payload = { status: 'success' }) =>
|
||||
resolveFetch({ ok: true, json: async () => payload }),
|
||||
};
|
||||
}
|
||||
|
||||
async function startRefresh(client, fullRebuild = false) {
|
||||
const promise = client.refreshModels(fullRebuild);
|
||||
await vi.waitFor(() => {
|
||||
expect(FakeWebSocket.instances.length).toBe(1);
|
||||
});
|
||||
await flushMicrotasks();
|
||||
const socket = FakeWebSocket.instances[0];
|
||||
await vi.waitFor(() => {
|
||||
expect(socket.onmessage).toBeTruthy();
|
||||
});
|
||||
return { promise, socket };
|
||||
}
|
||||
|
||||
it('shows scan progress updates from the WebSocket channel', async () => {
|
||||
const fetchControl = mockFetchPending();
|
||||
const client = await createClient();
|
||||
const { promise, socket } = await startRefresh(client);
|
||||
|
||||
expect(socket.url).toBe(`ws://${window.location.host}/ws/fetch-progress`);
|
||||
|
||||
socket.emit({
|
||||
type: 'scan_progress',
|
||||
status: 'started',
|
||||
stage: 'scan_folders',
|
||||
model_type: 'lora',
|
||||
pageType: 'loras',
|
||||
full_rebuild: false,
|
||||
progress: 0,
|
||||
});
|
||||
socket.emit({
|
||||
type: 'scan_progress',
|
||||
status: 'processing',
|
||||
stage: 'process_models',
|
||||
model_type: 'lora',
|
||||
pageType: 'loras',
|
||||
full_rebuild: false,
|
||||
progress: 50,
|
||||
processed: 5,
|
||||
total: 10,
|
||||
current_name: 'style.safetensors',
|
||||
});
|
||||
|
||||
expect(setProgressMock).toHaveBeenCalledWith(0);
|
||||
expect(setProgressMock).toHaveBeenCalledWith(50);
|
||||
const lastStatus = setStatusMock.mock.calls.at(-1)[0];
|
||||
expect(lastStatus).toContain('(5/10)');
|
||||
expect(lastStatus).toContain('style.safetensors');
|
||||
// First ETA sample only anchors the timer
|
||||
expect(lastStatus).toContain('Estimating time...');
|
||||
|
||||
fetchControl.resolveOk();
|
||||
await promise;
|
||||
|
||||
expect(resetAndReloadMock).toHaveBeenCalledWith(true);
|
||||
expect(showToastMock).toHaveBeenCalledWith(
|
||||
'toast.api.refreshComplete',
|
||||
{ action: 'Refresh' },
|
||||
'success'
|
||||
);
|
||||
expect(socket.close).toHaveBeenCalled();
|
||||
expect(hideMock).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('ignores messages for other types or other model types', async () => {
|
||||
const fetchControl = mockFetchPending();
|
||||
const client = await createClient();
|
||||
const { promise, socket } = await startRefresh(client);
|
||||
|
||||
socket.emit({
|
||||
type: 'scan_progress',
|
||||
status: 'processing',
|
||||
stage: 'process_models',
|
||||
model_type: 'checkpoint',
|
||||
progress: 33,
|
||||
processed: 1,
|
||||
total: 3,
|
||||
});
|
||||
socket.emit({
|
||||
type: 'example_images_progress',
|
||||
status: 'running',
|
||||
model_type: 'lora',
|
||||
progress: 66,
|
||||
processed: 2,
|
||||
total: 3,
|
||||
});
|
||||
|
||||
expect(setProgressMock).not.toHaveBeenCalled();
|
||||
expect(setStatusMock).not.toHaveBeenCalled();
|
||||
|
||||
fetchControl.resolveOk();
|
||||
await promise;
|
||||
});
|
||||
|
||||
it('falls back to plain loading when the WebSocket connection fails', async () => {
|
||||
FakeWebSocket.failNextConnection = true;
|
||||
global.fetch = vi.fn().mockResolvedValue({
|
||||
ok: true,
|
||||
json: async () => ({ status: 'success' }),
|
||||
});
|
||||
|
||||
const client = await createClient();
|
||||
await client.refreshModels(true);
|
||||
|
||||
expect(global.fetch).toHaveBeenCalled();
|
||||
const [url] = global.fetch.mock.calls[0];
|
||||
expect(url.searchParams.get('full_rebuild')).toBe('true');
|
||||
expect(showMock).toHaveBeenCalledWith('Full rebuild LoRAs...', 0);
|
||||
expect(showToastMock).toHaveBeenCalledWith(
|
||||
'toast.api.refreshComplete',
|
||||
{ action: 'Full rebuild' },
|
||||
'success'
|
||||
);
|
||||
});
|
||||
|
||||
it('computes an ETA with EMA smoothing once enough samples arrive', async () => {
|
||||
const fetchControl = mockFetchPending();
|
||||
let now = 1000;
|
||||
vi.spyOn(Date, 'now').mockImplementation(() => now);
|
||||
|
||||
const client = await createClient();
|
||||
const { promise, socket } = await startRefresh(client);
|
||||
|
||||
const emitProcessing = (processed, total) => socket.emit({
|
||||
type: 'scan_progress',
|
||||
status: 'processing',
|
||||
stage: 'process_models',
|
||||
model_type: 'lora',
|
||||
progress: Math.floor((processed / total) * 100),
|
||||
processed,
|
||||
total,
|
||||
});
|
||||
|
||||
// First sample anchors the timer
|
||||
emitProcessing(1, 10);
|
||||
expect(setStatusMock.mock.calls.at(-1)[0]).toContain('Estimating time...');
|
||||
|
||||
// 100s elapsed for 2 files -> 50s per file -> 400s remaining -> ~7 min
|
||||
now = 101000;
|
||||
emitProcessing(2, 10);
|
||||
expect(setStatusMock.mock.calls.at(-1)[0]).toContain('~7 min remaining');
|
||||
|
||||
// 110s elapsed for 4 files -> EMA = 50000*0.7 + 27500*0.3 = 43250ms/file
|
||||
// remaining 6 files -> 259.5s -> ~4 min
|
||||
now = 111000;
|
||||
emitProcessing(4, 10);
|
||||
expect(setStatusMock.mock.calls.at(-1)[0]).toContain('~4 min remaining');
|
||||
|
||||
fetchControl.resolveOk();
|
||||
await promise;
|
||||
});
|
||||
|
||||
it('shows the cancelled toast when the server reports cancellation', async () => {
|
||||
const fetchControl = mockFetchPending();
|
||||
const client = await createClient();
|
||||
const { promise } = await startRefresh(client);
|
||||
|
||||
fetchControl.resolveOk({ status: 'cancelled' });
|
||||
await promise;
|
||||
|
||||
expect(showToastMock).toHaveBeenCalledWith('toast.api.operationCancelled', {}, 'info');
|
||||
expect(resetAndReloadMock).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe('createScanEtaTracker / formatScanRemainingTime', () => {
|
||||
it('estimates remaining time from EMA of per-file cost', async () => {
|
||||
const { createScanEtaTracker } = await import(BASE_MODEL_API_MODULE);
|
||||
let now = 0;
|
||||
vi.spyOn(Date, 'now').mockImplementation(() => now);
|
||||
|
||||
const tracker = createScanEtaTracker();
|
||||
expect(tracker.update(1, 10)).toBe('Estimating time...');
|
||||
|
||||
now = 60000; // 60s for 3 files -> 20s/file -> 7 * 20s = 140s -> ~2 min
|
||||
expect(tracker.update(3, 10)).toBe('~2 min remaining');
|
||||
|
||||
now = 61000; // tiny delta keeps EMA near 20s/file
|
||||
expect(tracker.update(4, 10)).toBe('~2 min remaining');
|
||||
|
||||
// Done: no ETA
|
||||
expect(tracker.update(10, 10)).toBeNull();
|
||||
expect(tracker.update(0, 0)).toBeNull();
|
||||
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
|
||||
it('formats hours and sub-minute remainders', async () => {
|
||||
const { formatScanRemainingTime } = await import(BASE_MODEL_API_MODULE);
|
||||
expect(formatScanRemainingTime(30000)).toBe('Less than a minute remaining');
|
||||
expect(formatScanRemainingTime(5 * 60000)).toBe('~5 min remaining');
|
||||
expect(formatScanRemainingTime(3600000 + 30 * 60000)).toBe('~1 hr 30 min remaining');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,285 @@
|
||||
import { describe, it, beforeEach, afterEach, expect, vi } from 'vitest';
|
||||
|
||||
const showToastMock = vi.hoisted(() => vi.fn());
|
||||
const loadingManagerMock = vi.hoisted(() => ({
|
||||
show: vi.fn(),
|
||||
hide: vi.fn(),
|
||||
restoreProgressBar: vi.fn(),
|
||||
setProgress: vi.fn(),
|
||||
setStatus: vi.fn(),
|
||||
}));
|
||||
const virtualScrollerMock = vi.hoisted(() => ({
|
||||
refreshWithData: vi.fn(),
|
||||
}));
|
||||
const getCurrentPageStateMock = vi.hoisted(() => vi.fn());
|
||||
const etaUpdateMock = vi.hoisted(() => vi.fn(() => 'ETA soon'));
|
||||
|
||||
vi.mock('../../../static/js/components/RecipeCard.js', () => ({
|
||||
RecipeCard: vi.fn(() => ({ element: document.createElement('div') })),
|
||||
}));
|
||||
|
||||
vi.mock('../../../static/js/state/index.js', () => ({
|
||||
state: {
|
||||
loadingManager: loadingManagerMock,
|
||||
virtualScroller: virtualScrollerMock,
|
||||
},
|
||||
getCurrentPageState: getCurrentPageStateMock,
|
||||
}));
|
||||
|
||||
vi.mock('../../../static/js/utils/uiHelpers.js', () => ({
|
||||
showToast: showToastMock,
|
||||
}));
|
||||
|
||||
vi.mock('../../../static/js/utils/i18nHelpers.js', () => ({
|
||||
translate: vi.fn((key, params, fallback) => {
|
||||
if (fallback) {
|
||||
return Object.entries(params || {}).reduce(
|
||||
(text, [name, value]) => text.replaceAll(`{${name}}`, value),
|
||||
fallback
|
||||
);
|
||||
}
|
||||
return key;
|
||||
}),
|
||||
}));
|
||||
|
||||
vi.mock('../../../static/js/utils/infiniteScroll.js', () => ({
|
||||
captureScrollPosition: vi.fn(),
|
||||
restoreScrollPosition: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock('../../../static/js/api/apiConfig.js', () => ({
|
||||
WS_ENDPOINTS: { fetchProgress: '/ws/fetch-progress' },
|
||||
}));
|
||||
|
||||
vi.mock('../../../static/js/utils/scanEtaUtils.js', () => ({
|
||||
createScanEtaTracker: () => ({ update: etaUpdateMock }),
|
||||
}));
|
||||
|
||||
import { refreshRecipes } from '../../../static/js/api/recipeApi.js';
|
||||
|
||||
class FakeWebSocket {
|
||||
static instances = [];
|
||||
static failNextConnection = false;
|
||||
|
||||
constructor(url) {
|
||||
this.url = url;
|
||||
this.onopen = null;
|
||||
this.onerror = null;
|
||||
this.onmessage = null;
|
||||
this.close = vi.fn();
|
||||
FakeWebSocket.instances.push(this);
|
||||
const shouldFail = FakeWebSocket.failNextConnection;
|
||||
FakeWebSocket.failNextConnection = false;
|
||||
queueMicrotask(() => {
|
||||
if (shouldFail) {
|
||||
this.onerror?.(new Error('connection refused'));
|
||||
} else {
|
||||
this.onopen?.();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
emit(data) {
|
||||
this.onmessage?.({ data: JSON.stringify(data) });
|
||||
}
|
||||
}
|
||||
|
||||
async function flushMicrotasks() {
|
||||
await new Promise((resolve) => setTimeout(resolve, 0));
|
||||
}
|
||||
|
||||
describe('refreshRecipes scan progress', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
getCurrentPageStateMock.mockReturnValue({
|
||||
pageSize: 50,
|
||||
currentPage: 1,
|
||||
hasMore: true,
|
||||
isLoading: false,
|
||||
sortBy: 'date:desc',
|
||||
showFavoritesOnly: false,
|
||||
activeFolder: null,
|
||||
searchOptions: { recursive: true },
|
||||
customFilter: { active: false },
|
||||
filters: {},
|
||||
});
|
||||
FakeWebSocket.instances = [];
|
||||
FakeWebSocket.failNextConnection = false;
|
||||
vi.stubGlobal('WebSocket', FakeWebSocket);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
delete global.fetch;
|
||||
vi.unstubAllGlobals();
|
||||
});
|
||||
|
||||
function mockFetchPendingScan() {
|
||||
let resolveScan;
|
||||
global.fetch = vi.fn((input) => {
|
||||
const url = String(input);
|
||||
if (url.includes('/scan')) {
|
||||
return new Promise((resolve) => { resolveScan = resolve; });
|
||||
}
|
||||
// Recipe list reload after the scan completes
|
||||
return Promise.resolve({
|
||||
ok: true,
|
||||
json: async () => ({ items: [], total: 0, total_pages: 0 }),
|
||||
});
|
||||
});
|
||||
return {
|
||||
resolveOk: (payload = { status: 'success' }) =>
|
||||
resolveScan({ ok: true, json: async () => payload }),
|
||||
resolveNotOk: () =>
|
||||
resolveScan({ ok: false, status: 500, statusText: 'Server Error' }),
|
||||
};
|
||||
}
|
||||
|
||||
async function startRefresh(fullRebuild = true) {
|
||||
const promise = refreshRecipes(fullRebuild);
|
||||
await vi.waitFor(() => {
|
||||
expect(FakeWebSocket.instances.length).toBe(1);
|
||||
});
|
||||
await flushMicrotasks();
|
||||
const socket = FakeWebSocket.instances[0];
|
||||
await vi.waitFor(() => {
|
||||
expect(socket.onmessage).toBeTruthy();
|
||||
});
|
||||
return { promise, socket };
|
||||
}
|
||||
|
||||
it('shows scan progress updates from the WebSocket channel', async () => {
|
||||
const fetchControl = mockFetchPendingScan();
|
||||
const { promise, socket } = await startRefresh();
|
||||
|
||||
expect(socket.url).toBe(`ws://${window.location.host}/ws/fetch-progress`);
|
||||
|
||||
socket.emit({
|
||||
type: 'scan_progress',
|
||||
status: 'started',
|
||||
stage: 'scan_folders',
|
||||
model_type: 'recipe',
|
||||
pageType: 'recipes',
|
||||
full_rebuild: true,
|
||||
progress: 0,
|
||||
});
|
||||
socket.emit({
|
||||
type: 'scan_progress',
|
||||
status: 'processing',
|
||||
stage: 'process_models',
|
||||
model_type: 'recipe',
|
||||
pageType: 'recipes',
|
||||
full_rebuild: true,
|
||||
progress: 50,
|
||||
processed: 5,
|
||||
total: 10,
|
||||
current_name: 'style.recipe.json',
|
||||
});
|
||||
|
||||
expect(loadingManagerMock.setProgress).toHaveBeenCalledWith(0);
|
||||
expect(loadingManagerMock.setProgress).toHaveBeenCalledWith(50);
|
||||
const lastStatus = loadingManagerMock.setStatus.mock.calls.at(-1)[0];
|
||||
expect(lastStatus).toContain('(5/10)');
|
||||
expect(lastStatus).toContain('style.recipe.json');
|
||||
expect(lastStatus).toContain('ETA soon');
|
||||
expect(etaUpdateMock).toHaveBeenCalledWith(5, 10);
|
||||
|
||||
fetchControl.resolveOk();
|
||||
await promise;
|
||||
|
||||
expect(showToastMock).toHaveBeenCalledWith(
|
||||
'toast.api.refreshComplete',
|
||||
{ action: 'Full rebuild' },
|
||||
'success'
|
||||
);
|
||||
expect(socket.close).toHaveBeenCalled();
|
||||
expect(loadingManagerMock.hide).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('ignores messages for other types or other model types', async () => {
|
||||
const fetchControl = mockFetchPendingScan();
|
||||
const { promise, socket } = await startRefresh();
|
||||
|
||||
socket.emit({
|
||||
type: 'scan_progress',
|
||||
status: 'processing',
|
||||
stage: 'process_models',
|
||||
model_type: 'lora',
|
||||
progress: 33,
|
||||
processed: 1,
|
||||
total: 3,
|
||||
});
|
||||
socket.emit({
|
||||
type: 'example_images_progress',
|
||||
status: 'running',
|
||||
model_type: 'recipe',
|
||||
progress: 66,
|
||||
processed: 2,
|
||||
total: 3,
|
||||
});
|
||||
|
||||
expect(loadingManagerMock.setProgress).not.toHaveBeenCalled();
|
||||
expect(loadingManagerMock.setStatus).not.toHaveBeenCalled();
|
||||
|
||||
fetchControl.resolveOk();
|
||||
await promise;
|
||||
});
|
||||
|
||||
it('falls back to plain loading when the WebSocket connection fails', async () => {
|
||||
FakeWebSocket.failNextConnection = true;
|
||||
global.fetch = vi.fn((input) => {
|
||||
const url = String(input);
|
||||
if (url.includes('/scan')) {
|
||||
return Promise.resolve({
|
||||
ok: true,
|
||||
json: async () => ({ status: 'success' }),
|
||||
});
|
||||
}
|
||||
return Promise.resolve({
|
||||
ok: true,
|
||||
json: async () => ({ items: [], total: 0, total_pages: 0 }),
|
||||
});
|
||||
});
|
||||
|
||||
await refreshRecipes(false);
|
||||
|
||||
expect(global.fetch).toHaveBeenCalled();
|
||||
const [url] = global.fetch.mock.calls[0];
|
||||
expect(url.searchParams.get('full_rebuild')).toBe('false');
|
||||
expect(loadingManagerMock.show).toHaveBeenCalledWith('Refreshing Recipes...', 0);
|
||||
expect(showToastMock).toHaveBeenCalledWith(
|
||||
'toast.api.refreshComplete',
|
||||
{ action: 'Refresh' },
|
||||
'success'
|
||||
);
|
||||
});
|
||||
|
||||
it('shows the cancelled toast when the server reports cancellation', async () => {
|
||||
const fetchControl = mockFetchPendingScan();
|
||||
const { promise } = await startRefresh();
|
||||
|
||||
fetchControl.resolveOk({ status: 'cancelled' });
|
||||
await promise;
|
||||
|
||||
expect(showToastMock).toHaveBeenCalledWith('toast.api.operationCancelled', {}, 'info');
|
||||
expect(showToastMock).not.toHaveBeenCalledWith(
|
||||
'toast.api.refreshComplete',
|
||||
expect.anything(),
|
||||
expect.anything()
|
||||
);
|
||||
});
|
||||
|
||||
it('reports refresh failures through the error toast', async () => {
|
||||
const fetchControl = mockFetchPendingScan();
|
||||
const { promise } = await startRefresh();
|
||||
|
||||
fetchControl.resolveNotOk();
|
||||
await promise;
|
||||
|
||||
expect(showToastMock).toHaveBeenCalledWith(
|
||||
'toast.api.refreshFailed',
|
||||
{ action: 'rebuild', type: 'recipe' },
|
||||
'error'
|
||||
);
|
||||
expect(loadingManagerMock.hide).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,234 @@
|
||||
import { describe, it, expect, vi } from 'vitest';
|
||||
|
||||
const {
|
||||
API_MODULE,
|
||||
APP_MODULE,
|
||||
CARET_HELPER_MODULE,
|
||||
PREVIEW_COMPONENT_MODULE,
|
||||
AUTOCOMPLETE_MODULE,
|
||||
} = vi.hoisted(() => ({
|
||||
API_MODULE: new URL('../../../scripts/api.js', import.meta.url).pathname,
|
||||
APP_MODULE: new URL('../../../scripts/app.js', import.meta.url).pathname,
|
||||
CARET_HELPER_MODULE: new URL('../../../web/comfyui/textarea_caret_helper.js', import.meta.url).pathname,
|
||||
PREVIEW_COMPONENT_MODULE: new URL('../../../web/comfyui/preview_tooltip.js', import.meta.url).pathname,
|
||||
AUTOCOMPLETE_MODULE: new URL('../../../web/comfyui/autocomplete.js', import.meta.url).pathname,
|
||||
}));
|
||||
|
||||
vi.mock(API_MODULE, () => ({
|
||||
api: { fetchApi: vi.fn() },
|
||||
}));
|
||||
|
||||
vi.mock(APP_MODULE, () => ({
|
||||
app: {
|
||||
canvas: { ds: { scale: 1 } },
|
||||
extensionManager: {
|
||||
setting: { get: vi.fn(), set: vi.fn() },
|
||||
},
|
||||
registerExtension: vi.fn(),
|
||||
},
|
||||
}));
|
||||
|
||||
vi.mock(CARET_HELPER_MODULE, () => ({
|
||||
TextAreaCaretHelper: vi.fn(() => ({
|
||||
getBeforeCursor: vi.fn(() => ''),
|
||||
getCursorOffset: vi.fn(() => ({ left: 0, top: 0 })),
|
||||
})),
|
||||
}));
|
||||
|
||||
vi.mock(PREVIEW_COMPONENT_MODULE, () => ({
|
||||
PreviewTooltip: vi.fn(() => ({ show: vi.fn(), hide: vi.fn(), cleanup: vi.fn() })),
|
||||
}));
|
||||
|
||||
const METADATA_NAME = '__lm_autocomplete_meta_text';
|
||||
|
||||
function makeMetadataValue() {
|
||||
return {
|
||||
version: 1,
|
||||
textWidgetName: 'text',
|
||||
lastAccepted: {
|
||||
start: 0,
|
||||
end: 6,
|
||||
insertedText: '1girl ',
|
||||
textSnapshot: 'old prompt text, 1girl ',
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
describe('stripAutocompleteLastAccepted', () => {
|
||||
let stripAutocompleteLastAccepted;
|
||||
|
||||
beforeAll(async () => {
|
||||
const module = await import(AUTOCOMPLETE_MODULE);
|
||||
stripAutocompleteLastAccepted = module.stripAutocompleteLastAccepted;
|
||||
});
|
||||
|
||||
it('removes lastAccepted while keeping the metadata base fields', () => {
|
||||
const value = makeMetadataValue();
|
||||
const stripped = stripAutocompleteLastAccepted(value);
|
||||
|
||||
expect(stripped).toEqual({ version: 1, textWidgetName: 'text' });
|
||||
expect('lastAccepted' in stripped).toBe(false);
|
||||
// Original value must not be mutated
|
||||
expect(value.lastAccepted).toBeDefined();
|
||||
});
|
||||
|
||||
it('returns values without lastAccepted as-is (same reference)', () => {
|
||||
const value = { version: 1, textWidgetName: 'text' };
|
||||
expect(stripAutocompleteLastAccepted(value)).toBe(value);
|
||||
});
|
||||
|
||||
it('returns non-object values as-is', () => {
|
||||
expect(stripAutocompleteLastAccepted(null)).toBe(null);
|
||||
expect(stripAutocompleteLastAccepted(undefined)).toBe(undefined);
|
||||
expect(stripAutocompleteLastAccepted('text')).toBe('text');
|
||||
expect(stripAutocompleteLastAccepted([1, 2])).toEqual([1, 2]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('stripAutocompleteMetadataFromPromptResult', () => {
|
||||
let stripResult;
|
||||
|
||||
beforeAll(async () => {
|
||||
const module = await import(AUTOCOMPLETE_MODULE);
|
||||
stripResult = module.stripAutocompleteMetadataFromPromptResult;
|
||||
});
|
||||
|
||||
function makeWorkflowNode() {
|
||||
const metadataValue = makeMetadataValue();
|
||||
return {
|
||||
properties: { __lm_widget_ids: ['text', METADATA_NAME] },
|
||||
widgets_values: ['current text', metadataValue],
|
||||
widgets_values_named: {
|
||||
text: 'current text',
|
||||
[METADATA_NAME]: metadataValue,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
it('strips lastAccepted from workflow widgets_values using __lm_widget_ids alignment', () => {
|
||||
const result = {
|
||||
workflow: { nodes: [makeWorkflowNode()] },
|
||||
output: {},
|
||||
};
|
||||
|
||||
const returned = stripResult(result);
|
||||
|
||||
expect(returned).toBe(result);
|
||||
expect(result.workflow.nodes[0].widgets_values[1])
|
||||
.toEqual({ version: 1, textWidgetName: 'text' });
|
||||
});
|
||||
|
||||
it('strips lastAccepted from widgets_values_named and leaves other widgets untouched', () => {
|
||||
const result = {
|
||||
workflow: { nodes: [makeWorkflowNode()] },
|
||||
output: {},
|
||||
};
|
||||
|
||||
stripResult(result);
|
||||
|
||||
const node = result.workflow.nodes[0];
|
||||
expect(node.widgets_values_named[METADATA_NAME])
|
||||
.toEqual({ version: 1, textWidgetName: 'text' });
|
||||
expect(node.widgets_values_named.text).toBe('current text');
|
||||
expect(node.widgets_values[0]).toBe('current text');
|
||||
});
|
||||
|
||||
it('handles null entries in widgets_values (bypass compatibility padding)', () => {
|
||||
const node = makeWorkflowNode();
|
||||
node.properties.__lm_widget_ids = ['text', 'seed', METADATA_NAME];
|
||||
node.widgets_values = ['current text', null, makeMetadataValue()];
|
||||
const result = { workflow: { nodes: [node] }, output: {} };
|
||||
|
||||
stripResult(result);
|
||||
|
||||
expect(result.workflow.nodes[0].widgets_values[1]).toBe(null);
|
||||
expect(result.workflow.nodes[0].widgets_values[2])
|
||||
.toEqual({ version: 1, textWidgetName: 'text' });
|
||||
});
|
||||
|
||||
it('still strips widgets_values_named when __lm_widget_ids is missing (legacy files)', () => {
|
||||
const node = makeWorkflowNode();
|
||||
delete node.properties;
|
||||
const arrayValue = node.widgets_values[1];
|
||||
const result = { workflow: { nodes: [node] }, output: {} };
|
||||
|
||||
stripResult(result);
|
||||
|
||||
// Array entries cannot be located without widget ids — left untouched
|
||||
expect(result.workflow.nodes[0].widgets_values[1]).toBe(arrayValue);
|
||||
expect(result.workflow.nodes[0].widgets_values_named[METADATA_NAME])
|
||||
.toEqual({ version: 1, textWidgetName: 'text' });
|
||||
});
|
||||
|
||||
it('strips lastAccepted from output (API prompt) inputs', () => {
|
||||
const result = {
|
||||
workflow: { nodes: [] },
|
||||
output: {
|
||||
'7': {
|
||||
class_type: 'Prompt (LoraManager)',
|
||||
inputs: {
|
||||
text: 'current text',
|
||||
[METADATA_NAME]: makeMetadataValue(),
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
stripResult(result);
|
||||
|
||||
const inputs = result.output['7'].inputs;
|
||||
expect(inputs[METADATA_NAME]).toEqual({ version: 1, textWidgetName: 'text' });
|
||||
expect(inputs.text).toBe('current text');
|
||||
});
|
||||
|
||||
it('strips lastAccepted inside subgraph definitions', () => {
|
||||
const result = {
|
||||
workflow: {
|
||||
nodes: [],
|
||||
definitions: {
|
||||
subgraphs: [{ nodes: [makeWorkflowNode()] }],
|
||||
},
|
||||
},
|
||||
output: {},
|
||||
};
|
||||
|
||||
stripResult(result);
|
||||
|
||||
const subgraphNode = result.workflow.definitions.subgraphs[0].nodes[0];
|
||||
expect(subgraphNode.widgets_values_named[METADATA_NAME])
|
||||
.toEqual({ version: 1, textWidgetName: 'text' });
|
||||
});
|
||||
|
||||
it('leaves results without lastAccepted unchanged', () => {
|
||||
const metadataValue = { version: 1, textWidgetName: 'text' };
|
||||
const result = {
|
||||
workflow: {
|
||||
nodes: [{
|
||||
properties: { __lm_widget_ids: ['text', METADATA_NAME] },
|
||||
widgets_values: ['abc', metadataValue],
|
||||
widgets_values_named: { text: 'abc', [METADATA_NAME]: metadataValue },
|
||||
}],
|
||||
},
|
||||
output: {
|
||||
'1': { inputs: { text: 'abc', [METADATA_NAME]: metadataValue } },
|
||||
},
|
||||
};
|
||||
|
||||
stripResult(result);
|
||||
|
||||
expect(result.workflow.nodes[0].widgets_values[1]).toBe(metadataValue);
|
||||
expect(result.output['1'].inputs[METADATA_NAME]).toBe(metadataValue);
|
||||
});
|
||||
|
||||
it('tolerates malformed results', () => {
|
||||
expect(stripResult(null)).toBe(null);
|
||||
expect(stripResult(undefined)).toBe(undefined);
|
||||
expect(stripResult({})).toEqual({});
|
||||
|
||||
const result = {
|
||||
workflow: { nodes: [null, { widgets_values: null }] },
|
||||
output: { '1': { inputs: null }, '2': {} },
|
||||
};
|
||||
expect(() => stripResult(result)).not.toThrow();
|
||||
});
|
||||
});
|
||||
@@ -2222,12 +2222,94 @@ async def test_reimport_without_source_path_falls_back_to_recipe_file(
|
||||
# The already-optimized preview image must be stored verbatim.
|
||||
assert harness.persistence.save_calls[-1]["skip_optimize"] is True
|
||||
assert harness.persistence.save_calls[-1]["image_bytes"] == b"fake-image"
|
||||
# The fallback source is the recipe's own previous preview, which gets
|
||||
# deleted with the old recipe — it must not be recorded as source_path.
|
||||
assert harness.persistence.save_calls[-1]["metadata"]["source_path"] == ""
|
||||
# User edits (title, tags) are carried over to the new recipe.
|
||||
assert harness.persistence.update_calls[-1]["recipe_id"] == "new-rec"
|
||||
assert harness.persistence.update_calls[-1]["updates"]["title"] == "Old title"
|
||||
assert harness.persistence.update_calls[-1]["updates"]["tags"] == ["tag1"]
|
||||
|
||||
|
||||
async def test_reimport_with_dangling_source_path_falls_back_to_recipe_file(
|
||||
monkeypatch, tmp_path: Path
|
||||
) -> None:
|
||||
"""A source_path pointing to a deleted file (left by an earlier re-import)
|
||||
must not block re-import: fall back to the recipe's own saved image and
|
||||
clear the dangling source_path."""
|
||||
async with recipe_harness(monkeypatch, tmp_path) as harness:
|
||||
recipe_file = harness.tmp_dir / "recipes" / "rec3.webp"
|
||||
recipe_file.parent.mkdir(parents=True, exist_ok=True)
|
||||
recipe_file.write_bytes(b"fake-image")
|
||||
|
||||
harness.scanner.recipes["rec3"] = {
|
||||
"id": "rec3",
|
||||
"title": "Dangling source",
|
||||
"file_path": str(recipe_file),
|
||||
"tags": [],
|
||||
# Dangling local path: the file no longer exists.
|
||||
"source_path": str(harness.tmp_dir / "recipes" / "deleted.webp"),
|
||||
}
|
||||
harness.analysis.result = SimpleNamespace(
|
||||
payload={"success": True, "recipe_id": "new-rec-3", "loras": []},
|
||||
status=200,
|
||||
)
|
||||
harness.persistence.save_result = SimpleNamespace(
|
||||
payload={"success": True, "recipe_id": "new-rec-3"}, status=200
|
||||
)
|
||||
|
||||
response = await harness.client.post("/api/lm/recipe/rec3/reimport")
|
||||
payload = await response.json()
|
||||
|
||||
assert response.status == 200
|
||||
assert payload["success"] is True
|
||||
assert payload["recipe_id"] == "new-rec-3"
|
||||
assert harness.analysis.local_calls == [str(recipe_file)]
|
||||
assert harness.persistence.delete_calls == ["rec3"]
|
||||
# The dangling path is not carried over to the new recipe.
|
||||
assert harness.persistence.save_calls[-1]["metadata"]["source_path"] == ""
|
||||
|
||||
|
||||
async def test_reimport_with_accessible_local_source_keeps_source_path(
|
||||
monkeypatch, tmp_path: Path
|
||||
) -> None:
|
||||
"""When the recorded source_path is an existing external file, it remains
|
||||
the source of truth and stays recorded on the new recipe."""
|
||||
async with recipe_harness(monkeypatch, tmp_path) as harness:
|
||||
source_file = harness.tmp_dir / "imports" / "original.png"
|
||||
source_file.parent.mkdir(parents=True, exist_ok=True)
|
||||
source_file.write_bytes(b"original-image")
|
||||
recipe_file = harness.tmp_dir / "recipes" / "rec4.webp"
|
||||
recipe_file.parent.mkdir(parents=True, exist_ok=True)
|
||||
recipe_file.write_bytes(b"fake-image")
|
||||
|
||||
harness.scanner.recipes["rec4"] = {
|
||||
"id": "rec4",
|
||||
"title": "External source",
|
||||
"file_path": str(recipe_file),
|
||||
"tags": [],
|
||||
"source_path": str(source_file),
|
||||
}
|
||||
harness.analysis.result = SimpleNamespace(
|
||||
payload={"success": True, "recipe_id": "new-rec-4", "loras": []},
|
||||
status=200,
|
||||
)
|
||||
harness.persistence.save_result = SimpleNamespace(
|
||||
payload={"success": True, "recipe_id": "new-rec-4"}, status=200
|
||||
)
|
||||
|
||||
response = await harness.client.post("/api/lm/recipe/rec4/reimport")
|
||||
payload = await response.json()
|
||||
|
||||
assert response.status == 200
|
||||
assert payload["success"] is True
|
||||
# The external source file is re-parsed, not the recipe preview.
|
||||
assert harness.analysis.local_calls == [str(source_file)]
|
||||
assert harness.persistence.save_calls[-1]["metadata"]["source_path"] == str(
|
||||
source_file
|
||||
)
|
||||
|
||||
|
||||
async def test_reimport_without_any_source_returns_400(
|
||||
monkeypatch, tmp_path: Path
|
||||
) -> None:
|
||||
|
||||
@@ -30,10 +30,14 @@ from py.utils.models import BaseModelMetadata
|
||||
class RecordingWebSocketManager:
|
||||
def __init__(self) -> None:
|
||||
self.payloads: List[Dict[str, Any]] = []
|
||||
self.broadcasts: List[Dict[str, Any]] = []
|
||||
|
||||
async def broadcast_init_progress(self, payload: Dict[str, Any]) -> None:
|
||||
self.payloads.append(payload)
|
||||
|
||||
async def broadcast(self, payload: Dict[str, Any]) -> None:
|
||||
self.broadcasts.append(payload)
|
||||
|
||||
|
||||
def _normalize_path(path: Path) -> str:
|
||||
return str(path).replace(os.sep, "/")
|
||||
@@ -1395,3 +1399,185 @@ async def test_get_all_folders_invalidated_after_move(tmp_path: Path):
|
||||
assert "new" in all_folders
|
||||
assert "new/deep" in all_folders
|
||||
assert set(cache.folders) <= set(all_folders)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_initialize_cache_broadcasts_scan_progress(tmp_path: Path, monkeypatch):
|
||||
_create_files(tmp_path)
|
||||
scanner = DummyScanner(tmp_path)
|
||||
|
||||
ws_stub = RecordingWebSocketManager()
|
||||
monkeypatch.setattr(model_scanner, "ws_manager", ws_stub)
|
||||
|
||||
await scanner._initialize_cache()
|
||||
|
||||
messages = ws_stub.broadcasts
|
||||
assert messages, "expected scan_progress broadcasts"
|
||||
|
||||
started = messages[0]
|
||||
assert started["type"] == "scan_progress"
|
||||
assert started["status"] == "started"
|
||||
assert started["stage"] == "scan_folders"
|
||||
assert started["progress"] == 0
|
||||
assert started["model_type"] == "dummy"
|
||||
assert started["pageType"] == "dummy"
|
||||
assert started["full_rebuild"] is True
|
||||
|
||||
count_messages = [m for m in messages if m["stage"] == "count_models"]
|
||||
assert count_messages and count_messages[0]["total"] == 3
|
||||
|
||||
process_messages = [
|
||||
m for m in messages
|
||||
if m["stage"] == "process_models" and m["status"] == "processing"
|
||||
]
|
||||
assert process_messages, "expected at least one process_models update"
|
||||
final_process = process_messages[-1]
|
||||
assert final_process["processed"] == 3
|
||||
assert final_process["total"] == 3
|
||||
assert final_process["current_name"].endswith(".txt")
|
||||
for message in process_messages:
|
||||
assert 0 < message["progress"] <= 99
|
||||
|
||||
stages = [m["stage"] for m in messages]
|
||||
assert "finalizing" in stages
|
||||
completed = messages[-1]
|
||||
assert completed["status"] == "completed"
|
||||
assert completed["progress"] == 100
|
||||
assert completed["elapsed_seconds"] >= 0
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_initialize_cache_broadcasts_cancelled(tmp_path: Path, monkeypatch):
|
||||
_create_files(tmp_path)
|
||||
scanner = DummyScanner(tmp_path)
|
||||
|
||||
ws_stub = RecordingWebSocketManager()
|
||||
monkeypatch.setattr(model_scanner, "ws_manager", ws_stub)
|
||||
|
||||
original_process = DummyScanner._process_model_file
|
||||
|
||||
async def cancelling_process(self, file_path, root_path, **kwargs):
|
||||
scanner.cancel_task()
|
||||
return await original_process(self, file_path, root_path, **kwargs)
|
||||
|
||||
monkeypatch.setattr(DummyScanner, "_process_model_file", cancelling_process)
|
||||
|
||||
await scanner._initialize_cache()
|
||||
|
||||
messages = ws_stub.broadcasts
|
||||
assert messages[0]["status"] == "started"
|
||||
assert messages[-1]["status"] == "cancelled"
|
||||
assert messages[-1]["elapsed_seconds"] >= 0
|
||||
assert not any(m["status"] == "completed" for m in messages)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_initialize_cache_broadcasts_error(tmp_path: Path, monkeypatch):
|
||||
scanner = DummyScanner(tmp_path)
|
||||
|
||||
ws_stub = RecordingWebSocketManager()
|
||||
monkeypatch.setattr(model_scanner, "ws_manager", ws_stub)
|
||||
|
||||
async def raising_gather(**_kwargs):
|
||||
raise RuntimeError("boom")
|
||||
|
||||
monkeypatch.setattr(scanner, "_gather_model_data", raising_gather)
|
||||
|
||||
await scanner._initialize_cache()
|
||||
|
||||
messages = ws_stub.broadcasts
|
||||
assert messages[0]["status"] == "started"
|
||||
assert messages[-1]["status"] == "error"
|
||||
assert messages[-1]["error"] == "boom"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_reconcile_cache_broadcasts_scan_progress(tmp_path: Path, monkeypatch):
|
||||
_create_files(tmp_path)
|
||||
scanner = DummyScanner(tmp_path)
|
||||
await scanner._initialize_cache()
|
||||
|
||||
ws_stub = RecordingWebSocketManager()
|
||||
monkeypatch.setattr(model_scanner, "ws_manager", ws_stub)
|
||||
|
||||
new_file = tmp_path / "three.txt"
|
||||
new_file.write_text("three", encoding="utf-8")
|
||||
|
||||
await scanner._reconcile_cache()
|
||||
|
||||
messages = ws_stub.broadcasts
|
||||
assert messages, "expected scan_progress broadcasts"
|
||||
|
||||
started = messages[0]
|
||||
assert started["type"] == "scan_progress"
|
||||
assert started["status"] == "started"
|
||||
assert started["stage"] == "reconcile_scan"
|
||||
assert started["progress"] == 0
|
||||
assert started["full_rebuild"] is False
|
||||
|
||||
process_messages = [
|
||||
m for m in messages
|
||||
if m["stage"] == "process_new" and m["status"] == "processing"
|
||||
]
|
||||
assert process_messages, "expected process_new progress updates"
|
||||
assert process_messages[-1]["processed"] == 1
|
||||
assert process_messages[-1]["total"] == 1
|
||||
assert process_messages[-1]["current_name"] == "three.txt"
|
||||
|
||||
completed = messages[-1]
|
||||
assert completed["status"] == "completed"
|
||||
assert completed["progress"] == 100
|
||||
assert completed["added"] == 1
|
||||
assert completed["removed"] == 0
|
||||
assert completed["elapsed_seconds"] >= 0
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_reconcile_cache_broadcasts_cancelled(tmp_path: Path, monkeypatch):
|
||||
_create_files(tmp_path)
|
||||
scanner = DummyScanner(tmp_path)
|
||||
await scanner._initialize_cache()
|
||||
|
||||
ws_stub = RecordingWebSocketManager()
|
||||
monkeypatch.setattr(model_scanner, "ws_manager", ws_stub)
|
||||
|
||||
new_file = tmp_path / "four.txt"
|
||||
new_file.write_text("four", encoding="utf-8")
|
||||
|
||||
original_process = DummyScanner._process_model_file
|
||||
|
||||
async def cancelling_process(self, file_path, root_path, **kwargs):
|
||||
scanner.cancel_task()
|
||||
return await original_process(self, file_path, root_path, **kwargs)
|
||||
|
||||
monkeypatch.setattr(DummyScanner, "_process_model_file", cancelling_process)
|
||||
|
||||
await scanner._reconcile_cache()
|
||||
|
||||
messages = ws_stub.broadcasts
|
||||
assert messages[0]["status"] == "started"
|
||||
assert messages[-1]["status"] == "cancelled"
|
||||
assert messages[-1]["elapsed_seconds"] >= 0
|
||||
assert not any(m["status"] == "completed" for m in messages)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_reconcile_cache_broadcasts_error(tmp_path: Path, monkeypatch):
|
||||
_create_files(tmp_path)
|
||||
scanner = DummyScanner(tmp_path)
|
||||
await scanner._initialize_cache()
|
||||
|
||||
ws_stub = RecordingWebSocketManager()
|
||||
monkeypatch.setattr(model_scanner, "ws_manager", ws_stub)
|
||||
|
||||
def raising_walk(*_args, **_kwargs):
|
||||
raise RuntimeError("walk failed")
|
||||
|
||||
monkeypatch.setattr(model_scanner.os, "walk", raising_walk)
|
||||
|
||||
await scanner._reconcile_cache()
|
||||
|
||||
messages = ws_stub.broadcasts
|
||||
assert messages[0]["status"] == "started"
|
||||
assert messages[-1]["status"] == "error"
|
||||
assert messages[-1]["error"] == "walk failed"
|
||||
|
||||
@@ -9,6 +9,7 @@ import pytest
|
||||
|
||||
from py.config import config
|
||||
from py.services import model_scanner as model_scanner_module
|
||||
from py.services import recipe_scanner as recipe_scanner_module
|
||||
from py.services.model_cache import ModelCache
|
||||
from py.services.model_hash_index import ModelHashIndex
|
||||
from py.services.model_scanner import CacheBuildResult, ModelScanner
|
||||
@@ -4965,3 +4966,133 @@ async def test_find_all_duplicate_recipes_include_prompt_missing_gen_params(reci
|
||||
groups = await scanner.find_all_duplicate_recipes(include_prompt=True)
|
||||
# Recipes without gen_params/prompt normalize to empty prompt and match
|
||||
assert groups == {"abc:0.8\x1f": ["r1", "r2"]}
|
||||
|
||||
|
||||
class RecordingRecipeWebSocketManager:
|
||||
"""Minimal ws_manager stand-in that records broadcasts."""
|
||||
|
||||
def __init__(self) -> None:
|
||||
self.payloads: list[Dict[str, Any]] = []
|
||||
self.broadcasts: list[Dict[str, Any]] = []
|
||||
|
||||
async def broadcast_init_progress(self, payload: Dict[str, Any]) -> None:
|
||||
self.payloads.append(payload)
|
||||
|
||||
async def broadcast(self, payload: Dict[str, Any]) -> None:
|
||||
self.broadcasts.append(payload)
|
||||
|
||||
|
||||
def _write_progress_recipe_files(recipes_dir: Path, count: int) -> None:
|
||||
recipes_dir.mkdir(parents=True, exist_ok=True)
|
||||
for idx in range(count):
|
||||
recipe_path = recipes_dir / f"progress-recipe-{idx}.recipe.json"
|
||||
recipe_path.write_text(
|
||||
json.dumps(
|
||||
{
|
||||
"id": f"progress-recipe-{idx}",
|
||||
"file_path": str(recipes_dir / f"img-{idx}.png"),
|
||||
"title": f"Recipe {idx}",
|
||||
"modified": 0.0,
|
||||
"created_date": 0.0,
|
||||
"loras": [],
|
||||
}
|
||||
),
|
||||
encoding="utf-8",
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_force_refresh_broadcasts_scan_progress(
|
||||
tmp_path: Path, monkeypatch, recipe_scanner
|
||||
):
|
||||
scanner, _stub = recipe_scanner
|
||||
recipes_dir = Path(config.loras_roots[0]) / "recipes"
|
||||
_write_progress_recipe_files(recipes_dir, 3)
|
||||
|
||||
ws_stub = RecordingRecipeWebSocketManager()
|
||||
monkeypatch.setattr(recipe_scanner_module, "ws_manager", ws_stub)
|
||||
|
||||
await scanner.get_cached_data(force_refresh=True)
|
||||
# Wait for the FTS index build so no background task outlives the loop.
|
||||
if scanner._fts_index_task:
|
||||
await scanner._fts_index_task
|
||||
|
||||
messages = ws_stub.broadcasts
|
||||
assert messages, "expected scan_progress broadcasts"
|
||||
|
||||
started = messages[0]
|
||||
assert started["type"] == "scan_progress"
|
||||
assert started["status"] == "started"
|
||||
assert started["stage"] == "scan_folders"
|
||||
assert started["progress"] == 0
|
||||
assert started["model_type"] == "recipe"
|
||||
assert started["pageType"] == "recipes"
|
||||
assert started["full_rebuild"] is True
|
||||
|
||||
count_messages = [m for m in messages if m["stage"] == "count_models"]
|
||||
assert count_messages and count_messages[0]["total"] == 3
|
||||
|
||||
process_messages = [
|
||||
m
|
||||
for m in messages
|
||||
if m["stage"] == "process_models" and m["status"] == "processing"
|
||||
]
|
||||
assert process_messages, "expected at least one process_models update"
|
||||
final_process = process_messages[-1]
|
||||
assert final_process["processed"] == 3
|
||||
assert final_process["total"] == 3
|
||||
assert final_process["current_name"].endswith(".recipe.json")
|
||||
for message in process_messages:
|
||||
assert 0 < message["progress"] <= 99
|
||||
|
||||
completed = messages[-1]
|
||||
assert completed["status"] == "completed"
|
||||
assert completed["progress"] == 100
|
||||
assert completed["elapsed_seconds"] >= 0
|
||||
assert completed["total"] == 3
|
||||
|
||||
|
||||
def test_sync_init_without_report_progress_does_not_broadcast(
|
||||
tmp_path: Path, monkeypatch, recipe_scanner
|
||||
):
|
||||
"""Startup path (initialize_in_background) must not emit scan_progress."""
|
||||
scanner, _stub = recipe_scanner
|
||||
recipes_dir = Path(config.loras_roots[0]) / "recipes"
|
||||
_write_progress_recipe_files(recipes_dir, 2)
|
||||
|
||||
ws_stub = RecordingRecipeWebSocketManager()
|
||||
monkeypatch.setattr(recipe_scanner_module, "ws_manager", ws_stub)
|
||||
|
||||
# Invalidate the persistent cache so the sync path performs a full
|
||||
# directory scan, exactly like a force refresh but without progress
|
||||
# reporting (this is how initialize_in_background invokes it).
|
||||
scanner._persistent_cache.save_cache([], {})
|
||||
|
||||
scanner._initialize_recipe_cache_sync()
|
||||
|
||||
assert ws_stub.broadcasts == []
|
||||
|
||||
|
||||
def test_sync_init_reports_error_broadcast(
|
||||
tmp_path: Path, monkeypatch, recipe_scanner
|
||||
):
|
||||
scanner, _stub = recipe_scanner
|
||||
recipes_dir = Path(config.loras_roots[0]) / "recipes"
|
||||
_write_progress_recipe_files(recipes_dir, 1)
|
||||
|
||||
ws_stub = RecordingRecipeWebSocketManager()
|
||||
monkeypatch.setattr(recipe_scanner_module, "ws_manager", ws_stub)
|
||||
|
||||
scanner._persistent_cache.save_cache([], {})
|
||||
|
||||
def raising_scan(self, recipes_dir, progress_loop=None):
|
||||
raise RuntimeError("boom")
|
||||
|
||||
monkeypatch.setattr(RecipeScanner, "_full_directory_scan_sync", raising_scan)
|
||||
|
||||
scanner._initialize_recipe_cache_sync(report_progress=True)
|
||||
|
||||
messages = ws_stub.broadcasts
|
||||
assert messages[0]["status"] == "started"
|
||||
assert messages[-1]["status"] == "error"
|
||||
assert messages[-1]["error"] == "boom"
|
||||
|
||||
@@ -45,6 +45,23 @@ import { app } from '../../../scripts/app.js'
|
||||
import { api } from '../../../scripts/api.js'
|
||||
// @ts-ignore
|
||||
import { getPoolConfigFromConnectedNode, getActiveLorasFromNode, updateConnectedTriggerWords, updateDownstreamLoaders } from '../../web/comfyui/utils.js'
|
||||
// @ts-ignore
|
||||
import { stripAutocompleteMetadataFromPromptResult } from '../../web/comfyui/autocomplete.js'
|
||||
|
||||
// Strip the autocomplete lastAccepted boundary from exported workflows.
|
||||
// lastAccepted carries old prompt text (insertedText/textSnapshot) and is
|
||||
// session-only state; it must not leak into exported JSON (#1093).
|
||||
// graphToPrompt is shared by workflow export, Export API and queueing.
|
||||
// Local saves go through the change-tracker snapshot (no graphToPrompt) and
|
||||
// are intentionally left untouched so cross-session caret continuity is kept.
|
||||
// Post-processing the resolved result avoids any window/race with
|
||||
// change-tracker or copy/paste serialization of live state.
|
||||
const originalGraphToPrompt = app.graphToPrompt.bind(app)
|
||||
app.graphToPrompt = async (...args: unknown[]) => {
|
||||
const result = await originalGraphToPrompt(...args)
|
||||
stripAutocompleteMetadataFromPromptResult(result)
|
||||
return result
|
||||
}
|
||||
|
||||
function forwardMiddleMouseToCanvas(container: HTMLElement) {
|
||||
if (!container) return
|
||||
|
||||
@@ -278,6 +278,112 @@ function createAutocompleteMetadataBase(textWidgetName = 'text') {
|
||||
};
|
||||
}
|
||||
|
||||
const AUTOCOMPLETE_METADATA_WIDGET_PREFIX = '__lm_autocomplete_meta_';
|
||||
const LORA_MANAGER_WIDGET_IDS_PROPERTY = '__lm_widget_ids'; // Must match vue-widgets/src/main.ts
|
||||
|
||||
/**
|
||||
* Return a copy of an autocomplete metadata value without the lastAccepted
|
||||
* boundary. lastAccepted carries insertedText/textSnapshot (old prompt text)
|
||||
* and is session-only state; it must not leak into exported workflow JSON.
|
||||
* Values without lastAccepted are returned as-is.
|
||||
*
|
||||
* @param {*} value - Widget metadata value (or any other widget value)
|
||||
* @returns {*} The stripped copy, or the original value when untouched
|
||||
*/
|
||||
export function stripAutocompleteLastAccepted(value) {
|
||||
if (!value || typeof value !== 'object' || Array.isArray(value)) {
|
||||
return value;
|
||||
}
|
||||
if (!('lastAccepted' in value)) {
|
||||
return value;
|
||||
}
|
||||
const stripped = { ...value };
|
||||
delete stripped.lastAccepted;
|
||||
return stripped;
|
||||
}
|
||||
|
||||
/**
|
||||
* Strip lastAccepted from autocomplete metadata widgets on a serialized
|
||||
* node's widgets_values / widgets_values_named. Array entries are aligned
|
||||
* via properties.__lm_widget_ids (written by the extension's onSerialize).
|
||||
* Operates on graph.serialize() output, which is already a deep copy.
|
||||
*
|
||||
* @param {Array} nodes - Serialized node array
|
||||
*/
|
||||
function stripAutocompleteMetadataFromNodes(nodes) {
|
||||
if (!Array.isArray(nodes)) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (const node of nodes) {
|
||||
if (!node || typeof node !== 'object') {
|
||||
continue;
|
||||
}
|
||||
|
||||
const widgetIds = node.properties?.[LORA_MANAGER_WIDGET_IDS_PROPERTY];
|
||||
if (Array.isArray(node.widgets_values) && Array.isArray(widgetIds)) {
|
||||
for (let i = 0; i < node.widgets_values.length && i < widgetIds.length; i++) {
|
||||
if (typeof widgetIds[i] === 'string'
|
||||
&& widgetIds[i].startsWith(AUTOCOMPLETE_METADATA_WIDGET_PREFIX)) {
|
||||
node.widgets_values[i] = stripAutocompleteLastAccepted(node.widgets_values[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const named = node.widgets_values_named;
|
||||
if (named && typeof named === 'object') {
|
||||
for (const [key, value] of Object.entries(named)) {
|
||||
if (key.startsWith(AUTOCOMPLETE_METADATA_WIDGET_PREFIX)) {
|
||||
named[key] = stripAutocompleteLastAccepted(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Strip lastAccepted from autocomplete metadata widgets in a graphToPrompt()
|
||||
* result (both the workflow document and the API prompt). Used by the widget
|
||||
* bundle to keep exported workflows free of old prompt text while leaving
|
||||
* live node state untouched.
|
||||
*
|
||||
* @param {*} result - graphToPrompt() result: { workflow, output }
|
||||
* @returns {*} The same result object, with metadata entries replaced in place
|
||||
*/
|
||||
export function stripAutocompleteMetadataFromPromptResult(result) {
|
||||
if (!result || typeof result !== 'object') {
|
||||
return result;
|
||||
}
|
||||
|
||||
const workflow = result.workflow;
|
||||
if (workflow && typeof workflow === 'object') {
|
||||
stripAutocompleteMetadataFromNodes(workflow.nodes);
|
||||
const subgraphs = workflow.definitions?.subgraphs;
|
||||
if (Array.isArray(subgraphs)) {
|
||||
for (const subgraph of subgraphs) {
|
||||
stripAutocompleteMetadataFromNodes(subgraph?.nodes);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const output = result.output;
|
||||
if (output && typeof output === 'object') {
|
||||
for (const nodeOutput of Object.values(output)) {
|
||||
const inputs = nodeOutput?.inputs;
|
||||
if (!inputs || typeof inputs !== 'object') {
|
||||
continue;
|
||||
}
|
||||
for (const [key, value] of Object.entries(inputs)) {
|
||||
if (key.startsWith(AUTOCOMPLETE_METADATA_WIDGET_PREFIX)) {
|
||||
inputs[key] = stripAutocompleteLastAccepted(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
function createDefaultBehavior(modelType) {
|
||||
return {
|
||||
enablePreview: false,
|
||||
|
||||
@@ -15795,6 +15795,77 @@ function _initLoraSyntaxFormatReactive() {
|
||||
});
|
||||
}
|
||||
_initLoraSyntaxFormatReactive();
|
||||
const AUTOCOMPLETE_METADATA_WIDGET_PREFIX = "__lm_autocomplete_meta_";
|
||||
const LORA_MANAGER_WIDGET_IDS_PROPERTY$1 = "__lm_widget_ids";
|
||||
function stripAutocompleteLastAccepted(value) {
|
||||
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
||||
return value;
|
||||
}
|
||||
if (!("lastAccepted" in value)) {
|
||||
return value;
|
||||
}
|
||||
const stripped = { ...value };
|
||||
delete stripped.lastAccepted;
|
||||
return stripped;
|
||||
}
|
||||
function stripAutocompleteMetadataFromNodes(nodes) {
|
||||
var _a2;
|
||||
if (!Array.isArray(nodes)) {
|
||||
return;
|
||||
}
|
||||
for (const node of nodes) {
|
||||
if (!node || typeof node !== "object") {
|
||||
continue;
|
||||
}
|
||||
const widgetIds = (_a2 = node.properties) == null ? void 0 : _a2[LORA_MANAGER_WIDGET_IDS_PROPERTY$1];
|
||||
if (Array.isArray(node.widgets_values) && Array.isArray(widgetIds)) {
|
||||
for (let i2 = 0; i2 < node.widgets_values.length && i2 < widgetIds.length; i2++) {
|
||||
if (typeof widgetIds[i2] === "string" && widgetIds[i2].startsWith(AUTOCOMPLETE_METADATA_WIDGET_PREFIX)) {
|
||||
node.widgets_values[i2] = stripAutocompleteLastAccepted(node.widgets_values[i2]);
|
||||
}
|
||||
}
|
||||
}
|
||||
const named = node.widgets_values_named;
|
||||
if (named && typeof named === "object") {
|
||||
for (const [key, value] of Object.entries(named)) {
|
||||
if (key.startsWith(AUTOCOMPLETE_METADATA_WIDGET_PREFIX)) {
|
||||
named[key] = stripAutocompleteLastAccepted(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
function stripAutocompleteMetadataFromPromptResult(result) {
|
||||
var _a2;
|
||||
if (!result || typeof result !== "object") {
|
||||
return result;
|
||||
}
|
||||
const workflow = result.workflow;
|
||||
if (workflow && typeof workflow === "object") {
|
||||
stripAutocompleteMetadataFromNodes(workflow.nodes);
|
||||
const subgraphs = (_a2 = workflow.definitions) == null ? void 0 : _a2.subgraphs;
|
||||
if (Array.isArray(subgraphs)) {
|
||||
for (const subgraph of subgraphs) {
|
||||
stripAutocompleteMetadataFromNodes(subgraph == null ? void 0 : subgraph.nodes);
|
||||
}
|
||||
}
|
||||
}
|
||||
const output = result.output;
|
||||
if (output && typeof output === "object") {
|
||||
for (const nodeOutput of Object.values(output)) {
|
||||
const inputs = nodeOutput == null ? void 0 : nodeOutput.inputs;
|
||||
if (!inputs || typeof inputs !== "object") {
|
||||
continue;
|
||||
}
|
||||
for (const [key, value] of Object.entries(inputs)) {
|
||||
if (key.startsWith(AUTOCOMPLETE_METADATA_WIDGET_PREFIX)) {
|
||||
inputs[key] = stripAutocompleteLastAccepted(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
const ROOT_GRAPH_ID = "root";
|
||||
const LORA_PROVIDER_NODE_TYPES = [
|
||||
"Lora Stacker (LoraManager)",
|
||||
@@ -16037,6 +16108,12 @@ const AUTOCOMPLETE_TEXT_MIN_WIDTH_DEFAULT = 400;
|
||||
const AUTOCOMPLETE_TEXT_MIN_HEIGHT_DEFAULT = 300;
|
||||
const AUTOCOMPLETE_METADATA_VERSION = 1;
|
||||
const LORA_MANAGER_WIDGET_IDS_PROPERTY = "__lm_widget_ids";
|
||||
const originalGraphToPrompt = app$1.graphToPrompt.bind(app$1);
|
||||
app$1.graphToPrompt = async (...args) => {
|
||||
const result = await originalGraphToPrompt(...args);
|
||||
stripAutocompleteMetadataFromPromptResult(result);
|
||||
return result;
|
||||
};
|
||||
function forwardMiddleMouseToCanvas(container) {
|
||||
if (!container) return;
|
||||
container.addEventListener("pointerdown", (event) => {
|
||||
|
||||
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user