From 953bb897c9295032e806afb963f815da1e6ca674 Mon Sep 17 00:00:00 2001 From: Lightning Pixel Date: Mon, 16 Nov 2026 11:25:25 +0100 Subject: [PATCH 1/2] feat(queue): add a Queue page with running, queued and past runs A new Queue page (Manage, after Library) follows every job from one place: - Running: the workflow started in this window with its live steps and elapsed time, plus any generation the backend is running. - Queued: generations waiting for the single backend worker. - History: past runs, newest first, with "Load more". Each run shows its name, an input or output thumbnail, step progress, elapsed time and status; a finished mesh opens in Generate or is shown in the Library (which now selects and scrolls to it). Backend: run records get parent_run_id. Model nodes of a workflow pass it when they start a generation, so the history lists the workflow once and its generations as its steps (GET /workflow-runs?top_level=true, GET /workflow-runs/{id}/children). Workflow records now take their input image from the Image nodes (they were empty when started from Generate). Closes #412 --- api/routers/generation.py | 27 +- api/routers/workflow_runs.py | 19 +- api/schemas/generation.py | 1 + api/schemas/runs.py | 3 + api/services/run_history.py | 22 +- api/tests/test_run_history.py | 135 ++++- package.json | 2 +- src/areas/library/LibraryGrid.tsx | 26 +- src/areas/library/LibraryPage.tsx | 29 ++ src/areas/queue/QueuePage.tsx | 462 ++++++++++++++++++ src/areas/queue/inputPreview.ts | 65 +++ src/areas/queue/queueModel.test.ts | 147 ++++++ src/areas/queue/queueModel.ts | 216 ++++++++ src/areas/workflows/workflowRunRecord.test.ts | 30 ++ src/areas/workflows/workflowRunRecord.ts | 87 ++++ src/areas/workflows/workflowRunStore.ts | 27 +- src/areas/workflows/workflowSceneRun.test.mjs | 2 + .../components/agent/clientTools.test.ts | 2 +- src/shared/pages/pageRegistry.test.ts | 2 +- src/shared/pages/pageRegistry.ts | 14 + .../thumbnails/assetThumbnailService.ts | 9 + src/shared/types/runs.ts | 2 + 22 files changed, 1310 insertions(+), 19 deletions(-) create mode 100644 src/areas/queue/QueuePage.tsx create mode 100644 src/areas/queue/inputPreview.ts create mode 100644 src/areas/queue/queueModel.test.ts create mode 100644 src/areas/queue/queueModel.ts diff --git a/api/routers/generation.py b/api/routers/generation.py index b1dd63b8c..dc8ad3183 100644 --- a/api/routers/generation.py +++ b/api/routers/generation.py @@ -63,14 +63,29 @@ def record_job_filed( inputs: dict, params: dict, collection: str, + parent_run_id: Optional[str] = None, ) -> None: """File the persisted history record of a generation job (see services.run_history).""" run_history.begin( job_id, "generation", model_id=model_id, inputs=inputs, - params={**params, "collection": collection}, + params={**params, "collection": collection}, parent_run_id=parent_run_id, ) +MAX_RUN_ID_CHARS = 200 + + +def normalize_parent_run_id(value: object) -> Optional[str]: + """The optional ``parent_run_id`` a caller sent: the workflow run that launched this + generation. Blank means none (a multipart form sends "" rather than omitting it).""" + if not isinstance(value, str) or not value.strip(): + return None + value = value.strip() + if len(value) > MAX_RUN_ID_CHARS: + raise HTTPException(400, "parent_run_id is too long") + return value + + def _record_job_end(job_id: str) -> None: """Copy a job's final state into its history record once its worker returned.""" job = _jobs.get(job_id) @@ -134,9 +149,11 @@ async def generate_from_image( enable_texture: bool = Form(False), texture_resolution: int = Form(1024), params: str = Form("{}"), + parent_run_id: Optional[str] = Form(None), ): if not image.content_type or not image.content_type.startswith("image/"): raise HTTPException(400, "File must be an image") + parent_run_id = normalize_parent_run_id(parent_run_id) if remesh not in VALID_REMESH_MODES: raise HTTPException(400, "remesh must be 'quad', 'triangle', or 'none'") @@ -171,7 +188,9 @@ async def generate_from_image( _jobs[job_id] = job _cancel_events[job_id] = threading.Event() - record_job_filed(job_id, model_id, {"image": image.filename}, full_params, collection) + record_job_filed( + job_id, model_id, {"image": image.filename}, full_params, collection, parent_run_id + ) background_tasks.add_task( _run_generation, job_id, image_bytes, full_params, collection, output_kind, model_id @@ -186,6 +205,7 @@ async def generate_from_artifact( background_tasks: BackgroundTasks, ): """Queue a validated typed artifact without serializing it as image bytes.""" + parent_run_id = normalize_parent_run_id(request.parent_run_id) try: manifest = generator_registry.get_manifest(request.model_id) except (KeyError, ValueError) as exc: @@ -202,7 +222,8 @@ async def generate_from_artifact( collection = sanitize_collection(request.collection) job_id = str(uuid.uuid4()) record_job_filed( - job_id, request.model_id, {request.input_kind: request.input_path}, params, collection + job_id, request.model_id, {request.input_kind: request.input_path}, params, collection, + parent_run_id, ) params["scene_manifest_path"] = str(artifact.path) _purge_old_jobs() diff --git a/api/routers/workflow_runs.py b/api/routers/workflow_runs.py index b928c9ae3..60b58446d 100644 --- a/api/routers/workflow_runs.py +++ b/api/routers/workflow_runs.py @@ -49,6 +49,10 @@ class WorkflowRunRecordIn(BaseModel): ended_at: Optional[float] = None +class RunRecordChildren(BaseModel): + runs: list[RunRecord] + + MAX_PAGE = 200 @@ -58,12 +62,23 @@ async def list_runs( kind: Optional[list[RunKind]] = Query(None), limit: int = Query(50, ge=1, le=MAX_PAGE), offset: int = Query(0, ge=0), + top_level: bool = False, ): - """Persisted run history, newest first. ``status``/``kind`` may repeat to match any.""" - runs, total = run_history.list(statuses=status, kinds=kind, limit=limit, offset=offset) + """Persisted run history, newest first. ``status``/``kind`` may repeat to match any. + ``top_level`` leaves out runs launched by another run (a workflow's generations); + fetch those with ``/{run_id}/children``. A running record carries its live steps.""" + runs, total = run_history.list( + statuses=status, kinds=kind, limit=limit, offset=offset, top_level=top_level + ) return RunRecordPage(runs=runs, total=total, limit=limit, offset=offset) +@router.get("/{run_id}/children", response_model=RunRecordChildren) +async def list_run_children(run_id: str): + """The runs a run launched (a workflow's generations), in execution order.""" + return RunRecordChildren(runs=run_history.children(run_id)) + + @router.put("/{run_id}", response_model=RunRecord) async def put_workflow_run_record(run_id: str, body: WorkflowRunRecordIn): """Insert or replace the history record of a renderer-driven workflow run.""" diff --git a/api/schemas/generation.py b/api/schemas/generation.py index 04c18c858..c4dd50a1d 100644 --- a/api/schemas/generation.py +++ b/api/schemas/generation.py @@ -18,3 +18,4 @@ class GenerateFromArtifactRequest(BaseModel): model_id: str collection: str = "Workflows" params: dict[str, Any] = Field(default_factory=dict) + parent_run_id: Optional[str] = None # the workflow run this generation belongs to diff --git a/api/schemas/runs.py b/api/schemas/runs.py index 3d95902fc..ff6e965ed 100644 --- a/api/schemas/runs.py +++ b/api/schemas/runs.py @@ -37,6 +37,9 @@ class RunRecord(BaseModel): workflow_id: Optional[str] = None workflow_name: Optional[str] = None op_id: Optional[str] = None # mesh operation id, e.g. "decimate" + # The run this one was launched by: a workflow run's model nodes each file their own + # generation, linked back here so a history feed can fold them under the workflow. + parent_run_id: Optional[str] = None inputs: dict[str, Any] = Field(default_factory=dict) params: dict[str, Any] = Field(default_factory=dict) steps: list[RunStep] = Field(default_factory=list) diff --git a/api/services/run_history.py b/api/services/run_history.py index 77c7261b6..2bfa9740f 100644 --- a/api/services/run_history.py +++ b/api/services/run_history.py @@ -113,13 +113,14 @@ def begin( op_id: Optional[str] = None, inputs: Optional[dict[str, Any]] = None, params: Optional[dict[str, Any]] = None, + parent_run_id: Optional[str] = None, ) -> None: """File a pending run (persisted, so a crash before it starts still shows).""" if not self.enabled: return record = RunRecord( id=run_id, kind=kind, status="pending", model_id=model_id, op_id=op_id, - inputs=inputs or {}, params=params or {}, + inputs=inputs or {}, params=params or {}, parent_run_id=parent_run_id, created_at=time.time(), ) self.save(record) @@ -135,7 +136,9 @@ def mark_running(self, run_id: str) -> None: def step(self, run_id: str, name: str) -> None: """Note the current step label. In memory only -- labels can change many times - a second; the steps are written out with the next status change.""" + a second; the steps are written out with the next status change. Readers still + see them live: a live record is the very object ``_records`` holds, and + ``get``/``list`` copy it at read time.""" if not name: return with self._lock: @@ -214,8 +217,10 @@ def list( kinds: Optional[Iterable[str]] = None, limit: int = 50, offset: int = 0, + top_level: bool = False, ) -> tuple[list[RunRecord], int]: - """Newest first. Returns (page, total matching).""" + """Newest first. Returns (page, total matching). ``top_level`` leaves out runs + launched by another run (see ``children``).""" if not self.enabled: return [], 0 wanted_status = set(statuses) if statuses else None @@ -226,11 +231,22 @@ def list( r for r in self._records.values() if (wanted_status is None or r.status in wanted_status) and (wanted_kind is None or r.kind in wanted_kind) + and not (top_level and r.parent_run_id) ] matching.sort(key=lambda r: r.created_at, reverse=True) page = matching[offset:offset + limit] return [r.model_copy(deep=True) for r in page], len(matching) + def children(self, parent_run_id: str) -> list[RunRecord]: + """The runs launched by ``parent_run_id``, in execution order (oldest first).""" + if not self.enabled: + return [] + with self._lock: + self._sync_path() + matching = [r for r in self._records.values() if r.parent_run_id == parent_run_id] + matching.sort(key=lambda r: r.created_at) + return [r.model_copy(deep=True) for r in matching] + # ------------------------------------------------------------------ # # Internals (lock held) # ------------------------------------------------------------------ # diff --git a/api/tests/test_run_history.py b/api/tests/test_run_history.py index 8c67f95f4..0d38786aa 100644 --- a/api/tests/test_run_history.py +++ b/api/tests/test_run_history.py @@ -7,13 +7,13 @@ from pathlib import Path from unittest.mock import patch -from fastapi import HTTPException +from fastapi import BackgroundTasks, HTTPException import routers.generation as generation import routers.optimize as optimize import routers.workflow_runs as workflow_runs import services.run_history as run_history_module -from schemas.generation import JobStatus +from schemas.generation import GenerateFromArtifactRequest, JobStatus from schemas.runs import RunRecord, RunStep from services.mesh_ops import MeshOpExecutionError, MeshOpResult from services.run_history import ( @@ -190,6 +190,57 @@ def test_follows_a_relocated_workspace(self) -> None: self.assertEqual(reloaded.get("live").status, "done") +class ParentRunTests(_TempStoreCase): + def _child(self, run_id: str, parent: str, created_at: float) -> RunRecord: + record = _record(run_id, created_at=created_at) + record.parent_run_id = parent + return record + + def test_parent_run_id_is_persisted(self) -> None: + self.store.begin("child", "generation", model_id="sf3d", parent_run_id="wf-run") + self.assertEqual(self.reopen().get("child").parent_run_id, "wf-run") + + def test_old_history_without_parent_run_id_loads(self) -> None: + self.path.write_text(json.dumps({"version": 1, "runs": [ + {"id": "legacy", "kind": "generation", "status": "done", "created_at": 1000.0}, + ]}), encoding="utf-8") + record = self.reopen().get("legacy") + self.assertEqual(record.status, "done") + self.assertIsNone(record.parent_run_id) + + def test_top_level_leaves_out_children_and_counts_accordingly(self) -> None: + self.store.save(_record("wf", kind="workflow", created_at=1000.0)) + self.store.save(self._child("c1", "wf", 1001.0)) + self.store.save(self._child("c2", "wf", 1002.0)) + self.store.save(_record("solo", created_at=1003.0)) + page, total = self.store.list(top_level=True) + self.assertEqual(([r.id for r in page], total), (["solo", "wf"], 2)) + page, total = self.store.list() + self.assertEqual(total, 4) + + def test_children_are_in_execution_order(self) -> None: + self.store.save(self._child("late", "wf", 1005.0)) + self.store.save(self._child("early", "wf", 1001.0)) + self.store.save(self._child("other", "wf-2", 1002.0)) + self.store.save(_record("wf", kind="workflow")) + self.assertEqual([r.id for r in self.store.children("wf")], ["early", "late"]) + self.assertEqual(self.store.children("nobody"), []) + + def test_running_record_lists_its_live_steps_without_rewriting_the_file(self) -> None: + self.store.begin("live", "generation") + self.store.mark_running("live") + on_disk = self.path.read_text(encoding="utf-8") + self.store.step("live", "Loading") + self.store.step("live", "Sampling 3/50") + page, _ = self.store.list(statuses=["running"]) + self.assertEqual([s.name for s in page[0].steps], ["Loading", "Sampling 3/50"]) + self.assertIsNone(page[0].steps[-1].ended_at) + self.assertEqual(self.path.read_text(encoding="utf-8"), on_disk) + # The list hands out copies: a caller cannot corrupt the live record. + page[0].steps.clear() + self.assertEqual(len(self.store.get("live").steps), 2) + + class _RouterHistoryCase(_TempStoreCase): """Points the routers' shared store at a temp workspace for one test.""" @@ -221,6 +272,29 @@ def test_route_is_mounted_at_the_collection_root(self) -> None: paths = {(route.path, tuple(sorted(route.methods))) for route in workflow_runs.router.routes} self.assertIn(("", ("GET",)), paths) self.assertIn(("/{run_id}", ("PUT",)), paths) + self.assertIn(("/{run_id}/children", ("GET",)), paths) + + def test_top_level_filter_and_children_route(self) -> None: + self.shared.save(_record("wf", kind="workflow", created_at=1000.0)) + for i in range(2): + child = _record(f"c{i}", created_at=1001.0 + i) + child.parent_run_id = "wf" + self.shared.save(child) + page = asyncio.run(workflow_runs.list_runs(status=None, kind=None, limit=50, offset=0, top_level=True)) + self.assertEqual(([r.id for r in page.runs], page.total), (["wf"], 1)) + page = asyncio.run(workflow_runs.list_runs(status=None, kind=None, limit=50, offset=0)) + self.assertEqual(page.total, 3) + self.assertEqual(page.runs[0].parent_run_id, "wf") + children = asyncio.run(workflow_runs.list_run_children("wf")) + self.assertEqual([r.id for r in children.runs], ["c0", "c1"]) + self.assertEqual(asyncio.run(workflow_runs.list_run_children("c0")).runs, []) + + def test_children_path_does_not_resolve_to_get_run(self) -> None: + from starlette.routing import Match + scope = {"type": "http", "method": "GET", "path": "/wf/children"} + matched = [r.endpoint for r in workflow_runs.router.routes + if r.matches(scope)[0] == Match.FULL] + self.assertEqual(matched, [workflow_runs.list_run_children]) def test_get_run_falls_back_to_history_after_purge(self) -> None: record = _record("old", "done") @@ -331,6 +405,63 @@ def test_cancel_is_recorded_immediately(self) -> None: self.assertEqual(self.reopen().get("c").status, "cancelled") +class _FakeUpload: + filename = "in.png" + content_type = "image/png" + + async def read(self) -> bytes: + return b"img" + + +class _EndpointRegistry: + def get_generator(self, model_id): + return object() + + def get_manifest(self, model_id): + return {"output": "mesh", "inputs": ["image", "scene"]} + + +class GenerationParentRunTests(_RouterHistoryCase): + def setUp(self) -> None: + super().setUp() + for patcher in (patch.object(generation.registry, "WORKSPACE_DIR", self.workspace), + patch.object(generation, "generator_registry", _EndpointRegistry())): + patcher.start() + self.addCleanup(patcher.stop) + + def _from_image(self, **extra) -> str: + response = asyncio.run(generation.generate_from_image( + BackgroundTasks(), image=_FakeUpload(), model_id="demo/model", collection="Workflows", + remesh="none", enable_texture=False, texture_resolution=1024, params="{}", **extra, + )) + return response["job_id"] + + def test_from_image_records_the_parent_run(self) -> None: + job_id = self._from_image(parent_run_id="wf-run") + self.assertEqual(self.reopen().get(job_id).parent_run_id, "wf-run") + self.assertEqual([r.id for r in self.shared.children("wf-run")], [job_id]) + + def test_from_image_without_or_with_blank_parent_is_top_level(self) -> None: + self.assertIsNone(self.shared.get(self._from_image()).parent_run_id) + self.assertIsNone(self.shared.get(self._from_image(parent_run_id=" ")).parent_run_id) + + def test_from_image_refuses_an_oversized_parent_id(self) -> None: + with self.assertRaises(HTTPException) as ctx: + self._from_image(parent_run_id="x" * 201) + self.assertEqual(ctx.exception.status_code, 400) + + def test_from_artifact_records_the_parent_run(self) -> None: + scene = self.workspace / "Workflows" / "room" + scene.mkdir(parents=True) + (scene / "scene-manifest.json").write_text( + json.dumps({"schema": "modly.scene-manifest.v1", "sceneRoot": ".", "assets": []})) + response = asyncio.run(generation.generate_from_artifact(GenerateFromArtifactRequest( + input_kind="scene", input_path="Workflows/room", model_id="demo/scene", + parent_run_id="wf-run", + ), BackgroundTasks())) + self.assertEqual(self.reopen().get(response["job_id"]).parent_run_id, "wf-run") + + class MeshOpRecordingTests(_RouterHistoryCase): def setUp(self) -> None: super().setUp() diff --git a/package.json b/package.json index 0ef0908f1..dbb876925 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,7 @@ "prepare-resources": "node scripts/download-python-embed.js", "test": "npm run test:py && npm run test:node", "test:py": "node scripts/run-pytests.mjs", - "test:node": "node --test --experimental-strip-types --experimental-loader ./scripts/node-ts-extensionless-loader.mjs src/shared/types/assetLibrary.test.ts src/areas/generate/assetLibraryProjection.test.ts src/areas/generate/assetLibraryService.test.ts src/areas/generate/assetLibraryUi.test.ts src/areas/library/libraryView.test.ts src/areas/library/gridWindow.test.ts src/areas/generate/orcaSlicerLink.test.ts src/shared/utils/agentStream.test.ts src/shared/thumbnails/assetThumbnailQueue.test.ts src/shared/thumbnails/assetThumbnailStore.test.ts src/shared/components/agent/clientTools.test.ts src/shared/components/agent/agentContext.test.ts src/shared/utils/ollamaHealth.test.ts src/shared/utils/notification.test.ts src/shared/utils/agentConversations.test.ts electron/main/artifact-registry-service.test.ts electron/main/asset-thumbnail-cache.test.ts electron/main/extension-path-guard.test.ts electron/main/storage-directory-guard.test.ts electron/main/storage-ownership.test.ts electron/main/startup-error.test.ts electron/main/api-port.test.ts electron/main/agent-conversations.test.ts electron/main/gpu-memory.test.ts electron/preload/artifact-registry-preload.test.ts src/shared/stores/meshHistory.test.ts src/areas/generate/meshEditPanel.test.ts src/areas/generate/sculpt/SculptMesh.test.ts src/areas/generate/sculpt/SculptMesh.perf.test.ts src/areas/generate/sculpt/sculptLogic.test.ts src/shared/pages/pageRegistry.test.ts src/shared/pages/startPage.test.ts src/areas/home/recents.test.ts src/areas/workflows/workflowRunRecord.test.ts && node --test electron/main/*.test.mjs src/**/*.test.mjs", + "test:node": "node --test --experimental-strip-types --experimental-loader ./scripts/node-ts-extensionless-loader.mjs src/shared/types/assetLibrary.test.ts src/areas/generate/assetLibraryProjection.test.ts src/areas/generate/assetLibraryService.test.ts src/areas/generate/assetLibraryUi.test.ts src/areas/library/libraryView.test.ts src/areas/library/gridWindow.test.ts src/areas/generate/orcaSlicerLink.test.ts src/shared/utils/agentStream.test.ts src/shared/thumbnails/assetThumbnailQueue.test.ts src/shared/thumbnails/assetThumbnailStore.test.ts src/shared/components/agent/clientTools.test.ts src/shared/components/agent/agentContext.test.ts src/shared/utils/ollamaHealth.test.ts src/shared/utils/notification.test.ts src/shared/utils/agentConversations.test.ts electron/main/artifact-registry-service.test.ts electron/main/asset-thumbnail-cache.test.ts electron/main/extension-path-guard.test.ts electron/main/storage-directory-guard.test.ts electron/main/storage-ownership.test.ts electron/main/startup-error.test.ts electron/main/api-port.test.ts electron/main/agent-conversations.test.ts electron/main/gpu-memory.test.ts electron/preload/artifact-registry-preload.test.ts src/shared/stores/meshHistory.test.ts src/areas/generate/meshEditPanel.test.ts src/areas/generate/sculpt/SculptMesh.test.ts src/areas/generate/sculpt/SculptMesh.perf.test.ts src/areas/generate/sculpt/sculptLogic.test.ts src/shared/pages/pageRegistry.test.ts src/shared/pages/startPage.test.ts src/areas/home/recents.test.ts src/areas/workflows/workflowRunRecord.test.ts src/areas/queue/queueModel.test.ts && node --test electron/main/*.test.mjs src/**/*.test.mjs", "package": "cross-env CSC_IDENTITY_AUTO_DISCOVERY=false npm run build && npm run prepare-resources && electron-builder", "package:mac": "cross-env CSC_IDENTITY_AUTO_DISCOVERY=false npm run build && npm run prepare-resources && electron-builder --mac --arm64", "lint": "eslint ." diff --git a/src/areas/library/LibraryGrid.tsx b/src/areas/library/LibraryGrid.tsx index 5feba2266..141e3daac 100644 --- a/src/areas/library/LibraryGrid.tsx +++ b/src/areas/library/LibraryGrid.tsx @@ -1,4 +1,4 @@ -import { createElement, useEffect, useLayoutEffect, useRef, useState, type CSSProperties, type KeyboardEvent, type RefObject } from 'react' +import { createElement, useEffect, useLayoutEffect, useMemo, useRef, useState, type CSSProperties, type KeyboardEvent, type RefObject } from 'react' import type { IconShape } from '@shared/pages/pageRegistry' import type { AssetCapability } from '@shared/types/assetLibrary' import { useAssetThumbnailUrl } from '@shared/thumbnails/assetThumbnailStore' @@ -210,19 +210,22 @@ export interface LibraryGridProps { openingId: string | null onSelect: (entryId: string) => void onOpen: (entry: ProjectedAssetLibraryEntry) => void + /** Scrolls this entry into view once (e.g. opened from another page); bump `revealKey` to repeat. */ + revealId?: string | null + revealKey?: number } /** * Windowed grid/list: the scroll area has the full height of every row, but * only the rows in (or just around) the viewport are mounted. */ -export default function LibraryGrid({ entries, view, selectedId, openingId, onSelect, onOpen }: LibraryGridProps): JSX.Element { +export default function LibraryGrid({ entries, view, selectedId, openingId, onSelect, onOpen, revealId = null, revealKey = 0 }: LibraryGridProps): JSX.Element { const scrollRef = useRef(null) const { width, height } = useElementSize(scrollRef) const [scrollTop, setScrollTop] = useState(0) const focusAfterRender = useRef(null) - const layout = layoutFor(view, Math.max(0, width - PADDING_X * 2), entries.length) + const layout = useMemo(() => layoutFor(view, Math.max(0, width - PADDING_X * 2), entries.length), [view, width, entries.length]) const range = visibleRowRange(layout, scrollTop - PADDING_Y, height, OVERSCAN_ROWS) const firstIndex = range.start * layout.columns const rendered = entries.slice(firstIndex, Math.min(entries.length, range.end * layout.columns)) @@ -234,6 +237,23 @@ export default function LibraryGrid({ entries, view, selectedId, openingId, onSe setScrollTop(0) }, [view, entries]) + // Reveal a tile asked for from outside, once the grid has a size to lay it out in. + const revealedKey = useRef(null) + useEffect(() => { + const id = revealId + const scroller = scrollRef.current + if (!id || revealedKey.current === revealKey || !scroller || width === 0 || height === 0) return + revealedKey.current = revealKey + const index = entries.findIndex((entry) => entry.id === id) + if (index < 0) return + const reveal = scrollTopToReveal(layout, Math.floor(index / layout.columns), scroller.scrollTop - PADDING_Y, scroller.clientHeight) + if (reveal !== null) { + scroller.scrollTop = reveal + PADDING_Y + setScrollTop(scroller.scrollTop) + } + focusAfterRender.current = id + }, [revealId, revealKey, width, height, entries, layout]) + // Keyboard moves can target a tile that was not mounted yet: focus it once it is. useEffect(() => { const id = focusAfterRender.current diff --git a/src/areas/library/LibraryPage.tsx b/src/areas/library/LibraryPage.tsx index 90fa31d53..0e31566ba 100644 --- a/src/areas/library/LibraryPage.tsx +++ b/src/areas/library/LibraryPage.tsx @@ -79,6 +79,10 @@ export default function LibraryPage(): JSX.Element { const setCurrentJob = useAppStore((s) => s.setCurrentJob) const pushMeshUrl = useAppStore((s) => s.pushMeshUrl) const navigate = useNavStore((s) => s.navigate) + const showToast = useAppStore((s) => s.showToast) + // Another page (Queue: "Show in Library") opened the Library on one asset. + const pathToSelect = useNavStore((s) => s.params?.selected ?? null) + const clearParams = useNavStore((s) => s.clearParams) const [entries, setEntries] = useState([]) const [loading, setLoading] = useState(true) @@ -88,6 +92,8 @@ export default function LibraryPage(): JSX.Element { const [selectedId, setSelectedId] = useState(null) const [openingId, setOpeningId] = useState(null) const [openError, setOpenError] = useState(null) + const [pendingSelect, setPendingSelect] = useState(null) + const [reveal, setReveal] = useState<{ id: string, key: number } | null>(null) const searchRef = useRef(null) const load = useCallback(async () => { @@ -111,6 +117,27 @@ export default function LibraryPage(): JSX.Element { useEffect(() => { void load() }, [load]) + useEffect(() => { + if (!pathToSelect) return + clearParams() + setPendingSelect(pathToSelect) + // Clear the filters so the asset is in view. + setQuery((current) => ({ ...DEFAULT_LIBRARY_QUERY, sort: current.sort })) + }, [pathToSelect, clearParams]) + + useEffect(() => { + if (!pendingSelect || loading) return + setPendingSelect(null) + const entry = entries.find((e) => e.workspacePath === pendingSelect || e.source?.workspacePath === pendingSelect) + if (!entry) { + showToast('That file is not in the library anymore.') + return + } + setSelectedId(entry.id) + setOpenError(null) + setReveal((current) => ({ id: entry.id, key: (current?.key ?? 0) + 1 })) + }, [pendingSelect, loading, entries, showToast]) + // "/" focuses the search, as on the Extensions page. useEffect(() => { const handler = (event: KeyboardEvent) => { @@ -301,6 +328,8 @@ export default function LibraryPage(): JSX.Element { openingId={openingId} onSelect={selectEntry} onOpen={(entry) => { void openEntry(entry) }} + revealId={reveal?.id ?? null} + revealKey={reveal?.key ?? 0} /> )} diff --git a/src/areas/queue/QueuePage.tsx b/src/areas/queue/QueuePage.tsx new file mode 100644 index 000000000..54fdc6874 --- /dev/null +++ b/src/areas/queue/QueuePage.tsx @@ -0,0 +1,462 @@ +import { useCallback, useEffect, useMemo, useRef, useState } from 'react' +import axios from 'axios' +import { useAppStore } from '@shared/stores/appStore' +import { useNavStore } from '@shared/stores/navStore' +import { useExtensionsStore } from '@shared/stores/extensionsStore' +import { useAssetThumbnailUrl } from '@shared/thumbnails/assetThumbnailStore' +import { ensureThumbnail } from '@shared/thumbnails/assetThumbnailService' +import { showImportedMesh } from '@shared/utils/meshJob' +import { useLiveWorkflowRun } from '@areas/workflows/workflowRunStore' +import type { RunRecord, RunRecordPage, RunStatus } from '@shared/types/runs' +import { useInputPreview } from './inputPreview' +import { + buildQueueSections, + formatDuration, + formatStepLabel, + isFinishedStatus, + modelNameResolver, + rowFromRecord, + runElapsedSeconds, + runOpenTarget, + type ModelNameResolver, + type QueueRow, +} from './queueModel' + +const POLL_MS = 2000 +const PAGE_SIZE = 50 +/** The list endpoint's largest page. */ +const MAX_LIMIT = 200 + +const nowSeconds = () => Date.now() / 1000 + +// ─── Data ───────────────────────────────────────────────────────────────────── + +/** + * Top-level run history, refreshed every 2 s while the page is mounted. The + * first MAX_LIMIT runs are polled; older pages ("Load more" past that) are + * fetched once and kept. + */ +function useRunHistory(apiUrl: string) { + const [polled, setPolled] = useState([]) + const [older, setOlder] = useState([]) + const [total, setTotal] = useState(0) + const [limit, setLimit] = useState(PAGE_SIZE) + const [loaded, setLoaded] = useState(false) + const [error, setError] = useState(null) + const [loadingMore, setLoadingMore] = useState(false) + const inFlight = useRef(false) + + const refresh = useCallback(async () => { + if (inFlight.current) return + inFlight.current = true + try { + const { data } = await axios.get(`${apiUrl}/workflow-runs`, { + params: { top_level: true, limit, offset: 0 }, + }) + setPolled(data.runs) + setTotal(data.total) + setError(null) + } catch (err) { + setError(err instanceof Error ? err.message : String(err)) + } finally { + inFlight.current = false + setLoaded(true) + } + }, [apiUrl, limit]) + + useEffect(() => { + void refresh() + const timer = setInterval(() => { void refresh() }, POLL_MS) + return () => clearInterval(timer) + }, [refresh]) + + const loadMore = useCallback(async () => { + if (limit < MAX_LIMIT) { + setLimit((current) => Math.min(MAX_LIMIT, current + PAGE_SIZE)) + return + } + setLoadingMore(true) + try { + const { data } = await axios.get(`${apiUrl}/workflow-runs`, { + params: { top_level: true, limit: MAX_LIMIT, offset: MAX_LIMIT + older.length }, + }) + setOlder((current) => [...current, ...data.runs]) + setTotal(data.total) + } catch (err) { + setError(err instanceof Error ? err.message : String(err)) + } finally { + setLoadingMore(false) + } + }, [apiUrl, limit, older.length]) + + const records = useMemo(() => { + const ids = new Set(polled.map((r) => r.id)) + return [...polled, ...older.filter((r) => !ids.has(r.id))] + }, [polled, older]) + + return { records, hasMore: total > polled.length + older.length, loaded, error, loadingMore, loadMore } +} + +/** A workflow run's child runs (its model generations), re-fetched while it runs. */ +function useChildRuns(apiUrl: string, runId: string, live: boolean) { + const [children, setChildren] = useState(null) + const [error, setError] = useState(null) + useEffect(() => { + let alive = true + const fetchChildren = async () => { + try { + const { data } = await axios.get<{ runs: RunRecord[] }>(`${apiUrl}/workflow-runs/${encodeURIComponent(runId)}/children`) + if (alive) { setChildren(data.runs); setError(null) } + } catch (err) { + if (alive) setError(err instanceof Error ? err.message : String(err)) + } + } + void fetchChildren() + if (!live) return () => { alive = false } + const timer = setInterval(() => { void fetchChildren() }, POLL_MS) + return () => { alive = false; clearInterval(timer) } + }, [apiUrl, runId, live]) + return { children, error } +} + +/** Seconds since the epoch, re-rendering every second while `ticking`. */ +function useNow(ticking: boolean): number { + const [now, setNow] = useState(nowSeconds) + useEffect(() => { + if (!ticking) return + setNow(nowSeconds()) + const timer = setInterval(() => setNow(nowSeconds()), 1000) + return () => clearInterval(timer) + }, [ticking]) + return now +} + +// ─── Pieces ─────────────────────────────────────────────────────────────────── + +function Icon({ d, size = 14 }: { d: string, size?: number }): JSX.Element { + return ( + + ) +} + +const ICONS = { + mesh: 'M12 2 3 7v10l9 5 9-5V7zM3 7l9 5 9-5M12 12v10', + workflow: 'M4 5h5v5H4zM15 14h5v5h-5zM9 7.5h3.5a2 2 0 0 1 2 2V14', + chevron: 'm9 6 6 6-6 6', + queue: 'M4 6h11M4 12h7M4 18h7M17 13v3l2 1.5', +} + +const STATUS_STYLES: Record = { + pending: { label: 'Queued', className: 'bg-zinc-800 text-zinc-300' }, + running: { label: 'Running', className: 'bg-accent/15 text-accent-light' }, + paused: { label: 'Waiting', className: 'bg-amber-500/10 text-amber-300' }, + done: { label: 'Done', className: 'bg-emerald-500/10 text-emerald-300' }, + error: { label: 'Failed', className: 'bg-red-500/10 text-red-300' }, + cancelled: { label: 'Cancelled', className: 'bg-zinc-800 text-zinc-400' }, + interrupted: { label: 'Interrupted', className: 'bg-amber-500/10 text-amber-300' }, +} + +function StatusPill({ status, paused = false }: { status: RunStatus, paused?: boolean }): JSX.Element { + const style = STATUS_STYLES[paused && status === 'running' ? 'paused' : status] + return ( + + {status === 'running' && !paused && + ) +} + +function SectionHeader({ title, count }: { title: string, count: number }): JSX.Element { + return ( +
+

{title}

+ {count} +
+ ) +} + +function Empty({ text }: { text: string }): JSX.Element { + return ( +
+

{text}

+
+ ) +} + +/** The run's input image, else its output mesh's thumbnail, else a kind icon. */ +function RunThumbnail({ row }: { row: QueueRow }): JSX.Element { + const inputUrl = useInputPreview(row.inputImage) + const target = runOpenTarget(row) + const meshPath = !row.inputImage && target.openable ? target.workspacePath : '' + const meshUrl = useAssetThumbnailUrl({ workspacePath: meshPath }) + useEffect(() => { if (meshPath) ensureThumbnail(meshPath) }, [meshPath]) + const src = inputUrl ?? (meshPath ? meshUrl : undefined) + return ( +
+ {src + ? + : } +
+ ) +} + +function ProgressBar({ progress }: { progress: number }): JSX.Element { + // A negative progress means unknown: an indeterminate shimmer. + const known = progress >= 0 + return ( +
+
+
+ ) +} + +function OutputActions({ row, compact = false }: { row: QueueRow, compact?: boolean }): JSX.Element | null { + const navigate = useNavStore((s) => s.navigate) + if (!isFinishedStatus(row.status)) return null + const target = runOpenTarget(row) + const tooltip = target.openable ? undefined : target.reason + const button = 'rounded-lg px-2.5 py-1 text-[11.5px] font-medium transition-colors disabled:cursor-not-allowed' + return ( +
+ + +
+ ) +} + +function stepSummary(row: QueueRow): string { + if (row.current) return formatStepLabel(row.current) + if (row.status === 'pending') return 'Waiting to start' + if (row.paused) return 'Waiting for you to continue a branch' + if (row.status === 'error' && row.error) return row.error + const count = row.steps.length + if (count === 0) return row.status === 'running' ? 'Starting…' : '' + return `${count} ${count === 1 ? 'step' : 'steps'}` +} + +function StepList({ row, now }: { row: QueueRow, now: number }): JSX.Element { + if (row.steps.length === 0) return

No steps recorded.

+ return ( +
    + {row.steps.map((step, index) => ( +
  1. + {index + 1} + {step.name} + + {formatDuration(runElapsedSeconds({ startedAt: step.started_at, endedAt: step.ended_at ?? null }, now))} + + +
  2. + ))} +
+ ) +} + +function ChildRuns({ row, apiUrl, resolveModel, now }: { row: QueueRow, apiUrl: string, resolveModel: ModelNameResolver, now: number }): JSX.Element { + const { children, error } = useChildRuns(apiUrl, row.id, !isFinishedStatus(row.status)) + if (error && !children) return

Could not load the generations: {error}

+ if (!children) return

Loading generations…

+ if (children.length === 0) return

No model generations in this run.

+ return ( +
    + {children.map((record) => { + const child = rowFromRecord(record, resolveModel) + return ( +
  1. + {child.name} + {formatDuration(runElapsedSeconds(child, now))} + + +
  2. + ) + })} +
+ ) +} + +function RunCard({ row, now, apiUrl, resolveModel }: { row: QueueRow, now: number, apiUrl: string, resolveModel: ModelNameResolver }): JSX.Element { + const [expanded, setExpanded] = useState(false) + const expandable = row.kind === 'workflow' + const summary = stepSummary(row) + const failed = row.status === 'error' && !row.current + return ( +
  • +
    setExpanded((v) => !v) : undefined} + > + {expandable ? ( + + ) :
    + + {expanded && ( +
    +
    +

    Steps

    + +
    +
    +

    Generations

    + +
    +
    + )} +
  • + ) +} + +function Section({ title, rows, empty, now, apiUrl, resolveModel, footer }: { + title: string + rows: QueueRow[] + empty: string + now: number + apiUrl: string + resolveModel: ModelNameResolver + footer?: JSX.Element | null +}): JSX.Element { + return ( +
    + + {rows.length > 0 ? ( +
      + {rows.map((row) => )} +
    + ) : } + {footer} +
    + ) +} + +// ─── Page ───────────────────────────────────────────────────────────────────── + +export default function QueuePage(): JSX.Element { + const apiUrl = useAppStore((s) => s.apiUrl) + const navigate = useNavStore((s) => s.navigate) + const modelExtensions = useExtensionsStore((s) => s.modelExtensions) + const live = useLiveWorkflowRun() + const { records, hasMore, loaded, error, loadingMore, loadMore } = useRunHistory(apiUrl) + + const resolveModel = useMemo(() => modelNameResolver(modelExtensions), [modelExtensions]) + const sections = useMemo(() => buildQueueSections(live, records, resolveModel), [live, records, resolveModel]) + const now = useNow(sections.running.length > 0) + + const nothingYet = loaded && sections.running.length + sections.queued.length + sections.history.length === 0 + + return ( +
    +
    +
    +

    Queue

    +

    + {!loaded + ? 'Loading runs…' + : sections.running.length > 0 + ? <>{sections.running.length} running{sections.queued.length > 0 ? <>, {sections.queued.length} queued : null} + : 'Nothing running right now'} +

    +
    + {error && ( +

    + Could not refresh the run history +

    + )} +
    + + {nothingYet ? ( +
    +
    + +
    +

    No runs yet

    +

    Generations and workflow runs show up here while they run, and stay listed once they finish.

    + +
    + ) : ( + <> +
    +
    +
    + +
    + ) : null} + /> + + )} +
    + ) +} diff --git a/src/areas/queue/inputPreview.ts b/src/areas/queue/inputPreview.ts new file mode 100644 index 000000000..f8ab29a03 --- /dev/null +++ b/src/areas/queue/inputPreview.ts @@ -0,0 +1,65 @@ +import { useEffect, useState } from 'react' + +// Small previews of run input images. The file is read once through the fs +// IPC bridge, drawn into a tiny canvas and kept as a data URL in memory, so a +// list of runs costs one read per image per session. Reads run one at a time. + +const PREVIEW_SIZE = 96 +const MAX_CACHED = 200 + +const cache = new Map>() +let tail: Promise = Promise.resolve() + +function mimeFor(path: string): string { + const ext = path.split('.').pop()?.toLowerCase() + if (ext === 'jpg' || ext === 'jpeg') return 'image/jpeg' + if (ext === 'webp') return 'image/webp' + return 'image/png' +} + +function loadImage(src: string): Promise { + return new Promise((resolve, reject) => { + const image = new Image() + image.onload = () => resolve(image) + image.onerror = () => reject(new Error('Could not decode the image')) + image.src = src + }) +} + +async function renderPreview(path: string): Promise { + try { + const base64 = await window.electron.fs.readFileBase64(path) + const image = await loadImage(`data:${mimeFor(path)};base64,${base64}`) + const scale = PREVIEW_SIZE / Math.max(image.naturalWidth, image.naturalHeight, 1) + const canvas = document.createElement('canvas') + canvas.width = Math.max(1, Math.round(image.naturalWidth * Math.min(1, scale))) + canvas.height = Math.max(1, Math.round(image.naturalHeight * Math.min(1, scale))) + canvas.getContext('2d')?.drawImage(image, 0, 0, canvas.width, canvas.height) + return canvas.toDataURL('image/webp', 0.8) + } catch { + return null + } +} + +/** A downscaled data URL of the image at `path`, or null when it cannot be read. */ +export function loadInputPreview(path: string): Promise { + const cached = cache.get(path) + if (cached) return cached + if (cache.size >= MAX_CACHED) cache.delete(cache.keys().next().value as string) + const job = tail.then(() => renderPreview(path)) + tail = job + cache.set(path, job) + return job +} + +export function useInputPreview(path: string | null): string | null { + const [url, setUrl] = useState(null) + useEffect(() => { + setUrl(null) + if (!path) return + let alive = true + void loadInputPreview(path).then((result) => { if (alive) setUrl(result) }) + return () => { alive = false } + }, [path]) + return url +} diff --git a/src/areas/queue/queueModel.test.ts b/src/areas/queue/queueModel.test.ts new file mode 100644 index 000000000..825edc0be --- /dev/null +++ b/src/areas/queue/queueModel.test.ts @@ -0,0 +1,147 @@ +import assert from 'node:assert/strict' +import test from 'node:test' + +import type { RunRecord } from '../../shared/types/runs' +import type { LiveWorkflowRun } from '../workflows/workflowRunRecord' +import { + buildQueueSections, + formatDuration, + formatStepLabel, + inputImagePath, + modelNameResolver, + runDisplayName, + runElapsedSeconds, + runOpenTarget, +} from './queueModel' + +function record(id: string, overrides: Partial = {}): RunRecord { + return { + id, + kind: 'workflow', + status: 'done', + workflow_name: `wf ${id}`, + inputs: {}, + params: {}, + steps: [], + created_at: 100, + started_at: 100, + ended_at: 160, + ...overrides, + } +} + +function live(overrides: Partial = {}): LiveWorkflowRun { + return { + runId: 'live', + workflowId: 'wf-1', + workflowName: 'Image to mesh', + status: 'running', + paused: false, + startedAt: 500, + endedAt: null, + inputs: { image: 'C:/in/cat.png' }, + steps: [{ name: 'Generate Mesh', node_id: 'gen', status: 'running', started_at: 501 }], + current: { index: 2, total: 4, name: 'Generate Mesh', detail: 'Generating…', progress: 40 }, + outputPath: null, + error: null, + ...overrides, + } +} + +const ids = (rows: { id: string }[]) => rows.map((r) => r.id) + +test('records split into running, queued and history', () => { + const sections = buildQueueSections(null, [ + record('r1', { status: 'running', started_at: 300, ended_at: null }), + record('q2', { status: 'pending', created_at: 250, started_at: null, ended_at: null }), + record('q1', { status: 'pending', created_at: 200, started_at: null, ended_at: null }), + record('d1', { status: 'done', started_at: 150 }), + record('e1', { status: 'error', started_at: 180, error: 'boom' }), + record('c1', { status: 'interrupted', started_at: 120 }), + ]) + assert.deepEqual(ids(sections.running), ['r1']) + // Next up first. + assert.deepEqual(ids(sections.queued), ['q1', 'q2']) + assert.deepEqual(ids(sections.history), ['e1', 'd1', 'c1']) + assert.equal(sections.history[0].error, 'boom') +}) + +test('the live run replaces its own running record, with its live progress', () => { + const sections = buildQueueSections(live(), [ + record('live', { status: 'running', started_at: 500, ended_at: null }), + record('d1'), + ]) + assert.deepEqual(ids(sections.running), ['live']) + assert.equal(sections.running[0].live, true) + assert.equal(sections.running[0].current?.progress, 40) + assert.equal(sections.running[0].inputImage, 'C:/in/cat.png') + assert.deepEqual(ids(sections.history), ['d1']) +}) + +test('a finished live run shows once in history: the record wins, the live copy stands in until it is listed', () => { + const ended = live({ status: 'done', endedAt: 560, current: null, outputPath: 'Workflows/a.glb' }) + const listed = buildQueueSections(ended, [record('live', { started_at: 500, ended_at: 561 }), record('d1')]) + assert.deepEqual(ids(listed.history), ['live', 'd1']) + assert.equal(listed.history[0].live, false) + assert.equal(listed.running.length, 0) + + const notYetListed = buildQueueSections(ended, [record('d1')]) + assert.deepEqual(ids(notYetListed.history), ['live', 'd1']) + assert.equal(notYetListed.history[0].live, true) +}) + +test('child runs never show at the top level', () => { + const sections = buildQueueSections(null, [record('child', { kind: 'generation', parent_run_id: 'p' }), record('p')]) + assert.deepEqual(ids(sections.history), ['p']) +}) + +test('a running record without live progress still names its open step', () => { + const { running } = buildQueueSections(null, [record('g', { + kind: 'generation', status: 'running', ended_at: null, model_id: 'triposg', + steps: [{ name: 'Load', status: 'done', started_at: 1, ended_at: 2 }, { name: 'Mesh', status: 'running', started_at: 2 }], + })]) + assert.equal(running[0].current?.name, 'Mesh') + assert.equal(formatStepLabel(running[0].current!), 'Step 2 · Mesh') +}) + +test('display names', () => { + const resolve = modelNameResolver([{ id: 'hunyuan', name: 'Hunyuan3D', nodes: [{ id: 'mini', name: 'Hunyuan3D Mini' }] }]) + assert.equal(runDisplayName(record('w'), resolve), 'wf w') + assert.equal(runDisplayName(record('w', { workflow_name: ' ' })), 'Untitled workflow') + assert.equal(runDisplayName(record('g', { kind: 'generation', model_id: 'hunyuan/mini' }), resolve), 'Hunyuan3D Mini') + assert.equal(runDisplayName(record('g', { kind: 'generation', model_id: 'hunyuan' }), resolve), 'Hunyuan3D') + assert.equal(runDisplayName(record('g', { kind: 'generation', model_id: 'trip_osr' }), resolve), 'Trip osr') + assert.equal(runDisplayName(record('m', { kind: 'mesh_op', op_id: 'decimate' })), 'Decimate') +}) + +test('input image paths are absolute files only', () => { + assert.equal(inputImagePath({ image: 'C:\\in\\a.png' }), 'C:\\in\\a.png') + assert.equal(inputImagePath({ image: '/home/u/a.png' }), '/home/u/a.png') + assert.equal(inputImagePath({ image: 'a.png' }), null) + assert.equal(inputImagePath({ image: '(attached image)' }), null) + assert.equal(inputImagePath({}), null) +}) + +test('elapsed and duration formatting', () => { + assert.equal(runElapsedSeconds({ startedAt: 100, endedAt: null }, 142.5), 42.5) + assert.equal(runElapsedSeconds({ startedAt: 100, endedAt: 160 }, 999), 60) + assert.equal(runElapsedSeconds({ startedAt: null, endedAt: null }, 999), null) + assert.equal(formatDuration(42.9), '42s') + assert.equal(formatDuration(185), '3m 05s') + assert.equal(formatDuration(3720), '1h 02m') + assert.equal(formatDuration(null), '—') + assert.equal(formatStepLabel({ index: 2, total: 4, name: 'Generate Mesh' }), 'Step 2/4 · Generate Mesh') +}) + +test('open targets: finished workspace meshes only', () => { + assert.deepEqual(runOpenTarget({ status: 'done', outputPath: 'Workflows/run/a.glb' }), { + openable: true, workspacePath: 'Workflows/run/a.glb', url: '/workspace/Workflows/run/a.glb', + }) + assert.equal(runOpenTarget({ status: 'running', outputPath: 'a.glb' }).openable, false) + assert.equal(runOpenTarget({ status: 'done', outputPath: null }).openable, false) + assert.equal(runOpenTarget({ status: 'done', outputPath: 'C:/out/a.glb' }).openable, false) + assert.equal(runOpenTarget({ status: 'done', outputPath: '../a.glb' }).openable, false) + assert.equal(runOpenTarget({ status: 'done', outputPath: 'Workflows/voice.wav' }).openable, false) + assert.equal(runOpenTarget({ status: 'done', outputPath: 'Scenes/s/scene.json' }).openable, false) + assert.equal(runOpenTarget({ status: 'done', outputPath: 'Workflows/a.obj' }).openable, false) +}) diff --git a/src/areas/queue/queueModel.ts b/src/areas/queue/queueModel.ts new file mode 100644 index 000000000..85a519104 --- /dev/null +++ b/src/areas/queue/queueModel.ts @@ -0,0 +1,216 @@ +// Pure model of the Queue page: merges the workflow run this window is +// executing with the persisted run history (GET /workflow-runs) into Running, +// Queued and History sections, and derives what each row shows and opens. +// Timestamps are epoch seconds, as in the run records. + +import type { RunKind, RunRecord, RunStatus, RunStep } from '../../shared/types/runs' +import type { LiveRunCurrentStep, LiveWorkflowRun } from '../workflows/workflowRunRecord' + +export interface QueueRow { + id: string + kind: RunKind + name: string + status: RunStatus + /** Running but waiting on the user (a Wait branch, a paused loop). */ + paused: boolean + /** Absolute path of the input image, when the run had one on disk. */ + inputImage: string | null + createdAt: number + startedAt: number | null + endedAt: number | null + steps: RunStep[] + /** The step running now, with live progress when this window runs it. */ + current: LiveRunCurrentStep | null + outputPath: string | null + error: string | null + /** Fed by this window's runner rather than by the history list. */ + live: boolean +} + +export interface QueueSections { + running: QueueRow[] + queued: QueueRow[] + history: QueueRow[] +} + +/** Resolves a generation's model id to a display name. */ +export type ModelNameResolver = (modelId: string) => string | undefined + +const TERMINAL: ReadonlySet = new Set(['done', 'error', 'cancelled', 'interrupted']) + +export function isFinishedStatus(status: RunStatus): boolean { + return TERMINAL.has(status) +} + +/** Model extensions → a resolver for `model_id` ("ext" or "ext/node"). */ +export function modelNameResolver( + extensions: readonly { id: string, name: string, nodes?: readonly { id: string, name?: string }[] }[], +): ModelNameResolver { + return (modelId) => { + const [extId, nodeId] = modelId.split('/') + const ext = extensions.find((e) => e.id === extId) + if (!ext) return undefined + const node = nodeId ? ext.nodes?.find((n) => n.id === nodeId) : undefined + return node?.name ?? ext.name + } +} + +function prettyId(id: string): string { + const last = id.split('/').pop() ?? id + const words = last.replace(/[_-]+/g, ' ').trim() + return words ? words[0].toUpperCase() + words.slice(1) : id +} + +export function runDisplayName(record: RunRecord, resolveModel?: ModelNameResolver): string { + if (record.kind === 'workflow') return record.workflow_name?.trim() || 'Untitled workflow' + if (record.kind === 'mesh_op') return record.op_id ? prettyId(record.op_id) : 'Mesh operation' + if (!record.model_id) return 'Generation' + return resolveModel?.(record.model_id) ?? prettyId(record.model_id) +} + +const ABSOLUTE_PATH = /^(?:[a-zA-Z]:[\\/]|[\\/])/ + +/** The input image a run read from disk; generations only record a file name. */ +export function inputImagePath(inputs: Record | undefined): string | null { + const image = inputs?.image + return typeof image === 'string' && ABSOLUTE_PATH.test(image) ? image : null +} + +/** The open step of a history record (its progress is unknown here). */ +function recordCurrentStep(record: RunRecord): LiveRunCurrentStep | null { + if (record.status !== 'running') return null + const last = record.steps[record.steps.length - 1] + if (!last || last.ended_at != null) return null + return { index: record.steps.length, total: 0, name: last.name, detail: '', progress: -1 } +} + +export function rowFromRecord(record: RunRecord, resolveModel?: ModelNameResolver): QueueRow { + return { + id: record.id, + kind: record.kind, + name: runDisplayName(record, resolveModel), + status: record.status, + paused: false, + inputImage: inputImagePath(record.inputs), + createdAt: record.created_at, + startedAt: record.started_at ?? null, + endedAt: record.ended_at ?? null, + steps: record.steps ?? [], + current: recordCurrentStep(record), + outputPath: record.output_path ?? null, + error: record.error ?? null, + live: false, + } +} + +export function rowFromLiveRun(run: LiveWorkflowRun): QueueRow { + return { + id: run.runId, + kind: 'workflow', + name: run.workflowName.trim() || 'Untitled workflow', + status: run.status, + paused: run.paused, + inputImage: inputImagePath(run.inputs), + createdAt: run.startedAt, + startedAt: run.startedAt, + endedAt: run.endedAt, + steps: run.steps, + current: run.current, + outputPath: run.outputPath, + error: run.error, + live: true, + } +} + +const newestFirst = (a: QueueRow, b: QueueRow) => (b.startedAt ?? b.createdAt) - (a.startedAt ?? a.createdAt) + +/** + * Splits runs into sections. The live run and its own history record are the + * same run: while it runs the live copy wins (it has step progress the record + * lacks); once it has ended the record wins, and the live copy only stands in + * until the history lists it. Queued runs come oldest first (next up on top); + * Running and History newest first. + */ +export function buildQueueSections( + live: LiveWorkflowRun | null, + records: readonly RunRecord[], + resolveModel?: ModelNameResolver, +): QueueSections { + const running: QueueRow[] = [] + const queued: QueueRow[] = [] + const history: QueueRow[] = [] + const seen = new Set() + + const liveRow = live ? rowFromLiveRun(live) : null + const liveRunning = liveRow !== null && !isFinishedStatus(liveRow.status) + if (liveRow && liveRunning) { + running.push(liveRow) + seen.add(liveRow.id) + } + + for (const record of records) { + if (seen.has(record.id) || record.parent_run_id) continue + seen.add(record.id) + const row = rowFromRecord(record, resolveModel) + if (row.status === 'pending') queued.push(row) + else if (row.status === 'running') running.push(row) + else history.push(row) + } + + if (liveRow && !seen.has(liveRow.id)) history.push(liveRow) + + running.sort(newestFirst) + queued.sort((a, b) => a.createdAt - b.createdAt) + history.sort(newestFirst) + return { running, queued, history } +} + +/** Seconds a run has been going (running) or took (finished); null before it starts. */ +export function runElapsedSeconds(row: Pick, now: number): number | null { + if (row.startedAt == null) return null + const end = row.endedAt ?? now + return Math.max(0, end - row.startedAt) +} + +/** "42s", "3m 05s", "1h 02m". */ +export function formatDuration(seconds: number | null | undefined): string { + if (seconds == null || !Number.isFinite(seconds)) return '—' + const total = Math.max(0, Math.floor(seconds)) + if (total < 60) return `${total}s` + const minutes = Math.floor(total / 60) + if (minutes < 60) return `${minutes}m ${String(total % 60).padStart(2, '0')}s` + const hours = Math.floor(minutes / 60) + return `${hours}h ${String(minutes % 60).padStart(2, '0')}m` +} + +/** "Step 2/4 · Generate Mesh", or "Step 2 · Generate Mesh" when the total is unknown. */ +export function formatStepLabel(step: Pick): string { + const position = step.total >= step.index ? `${step.index}/${step.total}` : `${step.index}` + return `Step ${position} · ${step.name}` +} + +export type RunOpenTarget = + | { openable: true, workspacePath: string, url: string } + | { openable: false, reason: string } + +const MESH_EXTENSIONS = /\.(glb|gltf)$/i +const AUDIO_EXTENSIONS = /\.(wav|mp3|ogg|flac|m4a)$/i +const SCENE_EXTENSIONS = /\.json$/i + +/** + * What a finished run's output opens as. Only workspace meshes (.glb/.gltf) + * open in Generate and show in the Library; output_path is workspace-relative + * for those, absolute when the output lives elsewhere. + */ +export function runOpenTarget(row: Pick): RunOpenTarget { + if (!isFinishedStatus(row.status)) return { openable: false, reason: 'The run has not finished yet.' } + const path = row.outputPath?.trim().replace(/\\/g, '/') + if (!path) return { openable: false, reason: 'This run did not produce an output.' } + if (ABSOLUTE_PATH.test(path) || path.split('/').includes('..')) { + return { openable: false, reason: 'The output is outside the workspace.' } + } + if (AUDIO_EXTENSIONS.test(path)) return { openable: false, reason: 'Audio outputs cannot be opened in the 3D viewer.' } + if (SCENE_EXTENSIONS.test(path)) return { openable: false, reason: 'Scene outputs cannot be opened from the Queue yet.' } + if (!MESH_EXTENSIONS.test(path)) return { openable: false, reason: 'Only .glb and .gltf outputs can be opened.' } + return { openable: true, workspacePath: path, url: `/workspace/${path}` } +} diff --git a/src/areas/workflows/workflowRunRecord.test.ts b/src/areas/workflows/workflowRunRecord.test.ts index 099a608e9..7309c6b02 100644 --- a/src/areas/workflows/workflowRunRecord.test.ts +++ b/src/areas/workflows/workflowRunRecord.test.ts @@ -9,6 +9,8 @@ import { reopenWorkflowRunRecord, runOutputPath, startRecordStep, + toLiveWorkflowRun, + workflowRunInputs, workflowRunParams, } from './workflowRunRecord' @@ -93,3 +95,31 @@ test('runOutputPath prefers a workspace-relative path', () => { assert.equal(runOutputPath('/optimize/serve-file?path=x', 'D:/x.glb'), 'D:/x.glb') assert.equal(runOutputPath(), undefined) }) + +test("inputs record the Image nodes' files, or an attached image", () => { + assert.deepEqual(workflowRunInputs(workflow, false), { image: 'C:/a.png' }) + assert.deepEqual(workflowRunInputs(workflow, true), { image: '(attached image)' }) + const two = { ...workflow, nodes: [...workflow.nodes, { id: 'img2', type: 'imageNode', data: { params: { filePath: 'C:/b.png' } } }] } + assert.deepEqual(workflowRunInputs(two, false), { image: 'C:/a.png', images: ['C:/a.png', 'C:/b.png'] }) + const none = { ...workflow, nodes: [{ id: 'img', type: 'imageNode', data: { params: {} } }] } + assert.deepEqual(workflowRunInputs(none, false), {}) +}) + +test("the live run exposes the record with the current step's progress", () => { + let record = newWorkflowRunRecord('run-1', workflow, { image: 'C:/a.png' }, 100) + record = startRecordStep(record, 'TripoSG', 'gen', 101) + const progress = { status: 'running' as const, blockIndex: 0, blockTotal: 3, blockProgress: 40, blockStep: 'Generating…' } + const run = toLiveWorkflowRun(record, progress) + assert.equal(run.runId, 'run-1') + assert.equal(run.workflowName, 'Image to mesh') + assert.equal(run.startedAt, 100) + assert.equal(run.paused, false) + assert.deepEqual(run.current, { index: 1, total: 3, name: 'TripoSG', detail: 'Generating…', progress: 40 }) + + assert.equal(toLiveWorkflowRun(record, { ...progress, status: 'paused' }).paused, true) + const done = finishWorkflowRunRecord(record, 'done', 130, { outputPath: 'Workflows/a.glb' }) + const ended = toLiveWorkflowRun(done, { ...progress, status: 'done' }) + assert.equal(ended.current, null) + assert.equal(ended.endedAt, 130) + assert.equal(ended.outputPath, 'Workflows/a.glb') +}) diff --git a/src/areas/workflows/workflowRunRecord.ts b/src/areas/workflows/workflowRunRecord.ts index e236e823e..af51731e6 100644 --- a/src/areas/workflows/workflowRunRecord.ts +++ b/src/areas/workflows/workflowRunRecord.ts @@ -93,3 +93,90 @@ export function runOutputPath(outputUrl?: string, outputPath?: string): string | if (outputUrl?.startsWith('/workspace/')) return outputUrl.slice('/workspace/'.length) return outputPath } + +/** What `inputs.image` holds when the run got its image as data, not as a file. */ +export const ATTACHED_IMAGE_INPUT = '(attached image)' + +/** + * A run's inputs: the image file of each Image node (`image` is the first, + * `images` lists them all when there are several). An image handed over as + * data replaces those files, so it is recorded as attached. + */ +export function workflowRunInputs(workflow: WorkflowLike, attachedImage: boolean): Record { + if (attachedImage) return { image: ATTACHED_IMAGE_INPUT } + const images: string[] = [] + for (const node of workflow.nodes) { + if (node.type !== 'imageNode') continue + const filePath = node.data.params?.filePath + if (typeof filePath === 'string' && filePath.trim() !== '' && !images.includes(filePath)) images.push(filePath) + } + if (images.length === 0) return {} + return images.length === 1 ? { image: images[0] } : { image: images[0], images } +} + +/** The step the runner is on, as the Queue page shows it ("Step 2/4 · Generate Mesh"). */ +export interface LiveRunCurrentStep { + /** 1-based position of the step in the current pass (main run or Wait branch). */ + index: number + /** Steps in the current pass; 0 when unknown. */ + total: number + name: string + /** What the step is doing right now, e.g. "Generating…". */ + detail: string + /** 0-100. */ + progress: number +} + +/** The workflow run this window is executing, with its live progress. */ +export interface LiveWorkflowRun { + runId: string + workflowId: string | null + workflowName: string + status: RunStatus + /** Running but waiting on the user (a Wait branch, a paused loop). */ + paused: boolean + startedAt: number + endedAt: number | null + inputs: Record + /** Steps run so far, in order; the open one is `running`. */ + steps: RunRecord['steps'] + current: LiveRunCurrentStep | null + outputPath: string | null + error: string | null +} + +/** The runner's progress fields this needs (WorkflowRunState). */ +interface RunProgress { + status: 'idle' | 'running' | 'paused' | 'done' | 'error' + blockIndex: number + blockTotal: number + blockProgress: number + blockStep: string +} + +export function toLiveWorkflowRun(record: WorkflowRunRecord, progress: RunProgress): LiveWorkflowRun { + const open = record.status === 'running' ? record.steps[record.steps.length - 1] : undefined + const current = open && open.ended_at == null + ? { + index: Math.max(1, progress.blockIndex + 1), + total: Math.max(0, progress.blockTotal), + name: open.name, + detail: progress.blockStep, + progress: Math.max(0, Math.min(100, progress.blockProgress)), + } + : null + return { + runId: record.id, + workflowId: record.workflow_id ?? null, + workflowName: record.workflow_name ?? 'Workflow', + status: record.status, + paused: record.status === 'running' && progress.status === 'paused', + startedAt: record.started_at ?? record.created_at, + endedAt: record.ended_at ?? null, + inputs: record.inputs, + steps: record.steps, + current, + outputPath: record.output_path ?? null, + error: record.error ?? null, + } +} diff --git a/src/areas/workflows/workflowRunStore.ts b/src/areas/workflows/workflowRunStore.ts index 87076f629..e7125fd75 100644 --- a/src/areas/workflows/workflowRunStore.ts +++ b/src/areas/workflows/workflowRunStore.ts @@ -1,3 +1,4 @@ +import { useMemo } from 'react' import { create } from 'zustand' import axios, { AxiosInstance } from 'axios' import { useAppStore } from '@shared/stores/appStore' @@ -11,9 +12,9 @@ import { assignSlotFilePaths } from './slotInputs' import type { SlotInputType } from './slotInputs' import { endRecordStep, finishWorkflowRunRecord, isTerminalRunStatus, newWorkflowRunRecord, - reopenWorkflowRunRecord, runOutputPath, startRecordStep, + reopenWorkflowRunRecord, runOutputPath, startRecordStep, toLiveWorkflowRun, workflowRunInputs, } from './workflowRunRecord' -import type { WorkflowRunRecord } from './workflowRunRecord' +import type { LiveWorkflowRun, WorkflowRunRecord } from './workflowRunRecord' import type { RunStatus } from '@shared/types/runs' // ─── Types ──────────────────────────────────────────────────────────────────── @@ -64,6 +65,7 @@ function updateRecord(fn: (record: WorkflowRunRecord) => WorkflowRunRecord): voi if (!_record.current) return const record = fn(_record.current) _record.current = record + useWorkflowRunStore.setState({ liveRecord: record }) const { id, ...body } = record const apiUrl = useAppStore.getState().apiUrl _recordWrites = _recordWrites @@ -468,6 +470,7 @@ async function executeExtensionNode( input_kind: 'scene', input_path: inputPath, model_id: node.data.extensionId ?? '', collection: 'Workflows', params: { ...effectiveParams, ...extraParams }, + parent_run_id: _record.current?.id ?? null, }) } else { const fd = new FormData() @@ -478,6 +481,8 @@ async function executeExtensionNode( fd.append('enable_texture', 'false') fd.append('texture_resolution', '1024') fd.append('params', JSON.stringify({ ...effectiveParams, ...extraParams })) + // Files this generation under the workflow run (Queue page: the run's steps). + if (_record.current) fd.append('parent_run_id', _record.current.id) submission = await client.post('/generate/from-image', fd, { headers: { 'Content-Type': 'multipart/form-data' } }) } const { data } = submission @@ -599,6 +604,11 @@ function pushBranchSceneMesh(ctx: RunContext, waitId: string): void { interface WorkflowRunStore { runState: WorkflowRunState + /** + * History record of the latest run started in this window (kept after it + * ends, until the next run). Read it through useLiveWorkflowRun(). + */ + liveRecord: WorkflowRunRecord | null activeNodeId: string | null activeWorkflowId: string | null nodeImageOutputs: Record @@ -694,6 +704,7 @@ export const useWorkflowRunStore = create((set, get) => { return { runState: IDLE, + liveRecord: null, activeNodeId: null, activeWorkflowId: null, nodeImageOutputs: {}, @@ -716,7 +727,7 @@ export const useWorkflowRunStore = create((set, get) => { _record.current = newWorkflowRunRecord( crypto.randomUUID(), workflow, - overrideImageData ? { image: '(attached image)' } : appState.selectedImagePath ? { image: appState.selectedImagePath } : {}, + workflowRunInputs(workflow, !!overrideImageData), nowSeconds(), ) updateRecord((r) => r) @@ -1173,3 +1184,13 @@ export const useWorkflowRunStore = create((set, get) => { }, } }) + +/** + * The latest workflow run of this window with its live progress (step list, + * current step, elapsed-time origin), or null before the first run. + */ +export function useLiveWorkflowRun(): LiveWorkflowRun | null { + const record = useWorkflowRunStore((s) => s.liveRecord) + const runState = useWorkflowRunStore((s) => s.runState) + return useMemo(() => (record ? toLiveWorkflowRun(record, runState) : null), [record, runState]) +} diff --git a/src/areas/workflows/workflowSceneRun.test.mjs b/src/areas/workflows/workflowSceneRun.test.mjs index 86600a841..7f5da8622 100644 --- a/src/areas/workflows/workflowSceneRun.test.mjs +++ b/src/areas/workflows/workflowSceneRun.test.mjs @@ -54,6 +54,8 @@ test('scene model uses typed artifact route and preserves scene output', async ( assert.deepEqual(posts[0].body, { input_kind: 'scene', input_path: 'Workflows/input/scene-manifest.json', model_id: 'pixal/world', collection: 'Workflows', params: {}, + // The generation is filed under the workflow run that started it. + parent_run_id: useWorkflowRunStore.getState().liveRecord.id, }) assert.equal(useWorkflowRunStore.getState().runState.outputPath, '/workspace/Workflows/result/scene-manifest.json') assert.equal(useWorkflowRunStore.getState().runState.outputUrl, undefined) diff --git a/src/shared/components/agent/clientTools.test.ts b/src/shared/components/agent/clientTools.test.ts index 92703792f..0bc4276ad 100644 --- a/src/shared/components/agent/clientTools.test.ts +++ b/src/shared/components/agent/clientTools.test.ts @@ -339,7 +339,7 @@ test('navigate changes page and validates it', async () => { const f = fake() assert.equal(await run('navigate', { page: 'models' }, f), 'Opened the Extensions page.') assert.deepEqual(f.calls, [['navigate', 'models']]) - assert.match(await run('navigate', { page: 'nope' }, f), /^Error: page must be one of: home, generate, workflows, library, models, settings/) + assert.match(await run('navigate', { page: 'nope' }, f), /^Error: page must be one of: home, generate, workflows, library, queue, models, settings/) }) test('a throwing dependency comes back as an error text', async () => { diff --git a/src/shared/pages/pageRegistry.test.ts b/src/shared/pages/pageRegistry.test.ts index eec0988bb..2446809d1 100644 --- a/src/shared/pages/pageRegistry.test.ts +++ b/src/shared/pages/pageRegistry.test.ts @@ -23,7 +23,7 @@ test('the pages keep their ids, labels and sidebar order', () => { [ ['home', ['home:Home']], ['create', ['generate:Generate', 'workflows:Workflows']], - ['manage', ['library:Library']], + ['manage', ['library:Library', 'queue:Queue']], ['system', ['models:Extensions', 'settings:Settings']], ], ) diff --git a/src/shared/pages/pageRegistry.ts b/src/shared/pages/pageRegistry.ts index c886fb23c..71602a961 100644 --- a/src/shared/pages/pageRegistry.ts +++ b/src/shared/pages/pageRegistry.ts @@ -76,6 +76,20 @@ export const PAGES = [ load: () => import('@areas/library/LibraryPage'), wrapperClass: 'flex flex-1 overflow-hidden', }, + { + id: 'queue', + label: 'Queue', + group: 'manage', + icon: [ + ['path', { d: 'M4 6h11' }], + ['path', { d: 'M4 12h6' }], + ['path', { d: 'M4 18h6' }], + ['circle', { cx: 17, cy: 15.5, r: 4.5 }], + ['path', { d: 'M17 13.5v2.2l1.4 1.1' }], + ], + load: () => import('@areas/queue/QueuePage'), + wrapperClass: 'flex-1 overflow-y-auto', + }, { id: 'models', label: 'Extensions', diff --git a/src/shared/thumbnails/assetThumbnailService.ts b/src/shared/thumbnails/assetThumbnailService.ts index 710e17e72..8bcb77329 100644 --- a/src/shared/thumbnails/assetThumbnailService.ts +++ b/src/shared/thumbnails/assetThumbnailService.ts @@ -39,6 +39,15 @@ export async function requestLibraryThumbnails(entries: AssetLibraryEntry[]): Pr await Promise.all(entriesNeedingThumbnails(entries).map((entry) => getQueue().ensure(entry.workspacePath))) } +/** + * Queues a thumbnail for one workspace mesh (.glb/.gltf) unless it is cached; + * the result lands in the thumbnail store, keyed by `workspacePath`. + */ +export function ensureThumbnail(workspacePath: string): void { + if (!/\.(glb|gltf)$/i.test(workspacePath)) return + void getQueue().ensure(workspacePath) +} + async function backfillLibraryThumbnails(): Promise { try { const result = await window.electron.workspace.library.list() diff --git a/src/shared/types/runs.ts b/src/shared/types/runs.ts index c8fda5b63..5665a6e36 100644 --- a/src/shared/types/runs.ts +++ b/src/shared/types/runs.ts @@ -34,6 +34,8 @@ export interface RunRecord { ended_at?: number | null /** Computed by the backend; null while the run has not ended. */ duration_s?: number | null + /** The run that launched this one (a workflow's model step); null for top-level runs. */ + parent_run_id?: string | null } export interface RunRecordPage { From 8583b5869021b522ccf31e041b1bd4ed1f6db1e1 Mon Sep 17 00:00:00 2001 From: Lightning Pixel Date: Mon, 16 Nov 2026 12:46:14 +0100 Subject: [PATCH 2/2] feat(generate): paste an image into Generate's image input with Ctrl+V Ctrl+V outside text fields puts a clipboard image into Generate's first image input; the empty input hints at it. The image node needs a file path, so the pasted image is saved to /Images/ through a narrow workspace:images:save IPC (PNG, JPEG or WebP checked by signature, 40 MB cap, sanitized unique names, never overwrites, never in tmp/). Part of #420 (the Image Studio page ships with background removal, #421). --- electron/main/ipc-handlers.ts | 6 + electron/main/workspace-images.test.ts | 123 ++++++++++++++ electron/main/workspace-images.ts | 152 ++++++++++++++++++ electron/preload/electron-api.ts | 7 + package.json | 2 +- .../generate/components/WorkflowPanel.tsx | 32 +++- src/shared/types/electron.d.ts | 5 + src/shared/utils/imageInput.test.ts | 25 +++ src/shared/utils/imageInput.ts | 46 ++++++ 9 files changed, 394 insertions(+), 4 deletions(-) create mode 100644 electron/main/workspace-images.test.ts create mode 100644 electron/main/workspace-images.ts create mode 100644 src/shared/utils/imageInput.test.ts diff --git a/electron/main/ipc-handlers.ts b/electron/main/ipc-handlers.ts index a43d1efa9..c14132b1a 100644 --- a/electron/main/ipc-handlers.ts +++ b/electron/main/ipc-handlers.ts @@ -96,6 +96,7 @@ import { import { registerWorkspaceAssetLibraryIpcHandlers } from './artifact-registry-service' import { getThumbnailsDir } from './asset-thumbnail-protocol' import { registerAssetThumbnailIpcHandlers, removeThumbnails } from './asset-thumbnail-cache' +import { registerWorkspaceImageIpcHandlers } from './workspace-images' import { updatesSupported } from './updater' import { ModelWeightOperations } from './model-weight-operations' import { readLocalFileBase64 } from './bounded-file-reader' @@ -1011,6 +1012,11 @@ export function setupIpcHandlers(pythonBridge: PythonBridge, getWindow: WindowGe getWorkspaceDir: () => getSettings(app.getPath('userData')).workspaceDir, thumbnailsDir: getThumbnailsDir(), }) + registerWorkspaceImageIpcHandlers({ + ipcMain, + getWorkspaceDir: () => getSettings(app.getPath('userData')).workspaceDir, + showItemInFolder: (path) => shell.showItemInFolder(path), + }) ipcMain.handle('workspace:listCollections', async () => { const base = workspacePath() diff --git a/electron/main/workspace-images.test.ts b/electron/main/workspace-images.test.ts new file mode 100644 index 000000000..e3e7c9d52 --- /dev/null +++ b/electron/main/workspace-images.test.ts @@ -0,0 +1,123 @@ +import assert from 'node:assert/strict' +import { mkdtemp, readFile, readdir, rm } from 'node:fs/promises' +import { tmpdir } from 'node:os' +import path from 'node:path' +import test from 'node:test' + +import { + MAX_WORKSPACE_IMAGE_BYTES, + isRevealableImagePath, + registerWorkspaceImageIpcHandlers, + sanitizeImageBaseName, + saveWorkspaceImage, + sniffImageMime, + validateWorkspaceImageRequest, + workspaceImageFileName, +} from './workspace-images' + +const PNG = new Uint8Array([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0, 0, 0, 0]) +const JPEG = new Uint8Array([0xff, 0xd8, 0xff, 0xe0, 0, 0]) +const WEBP = new Uint8Array([0x52, 0x49, 0x46, 0x46, 1, 0, 0, 0, 0x57, 0x45, 0x42, 0x50, 0]) + +async function withWorkspace(run: (workspaceDir: string) => Promise) { + const workspaceDir = await mkdtemp(path.join(tmpdir(), 'modly-images-')) + try { + await run(workspaceDir) + } finally { + await rm(workspaceDir, { recursive: true, force: true }) + } +} + +test('sniffImageMime recognises PNG, JPEG and WebP signatures only', () => { + assert.equal(sniffImageMime(PNG), 'image/png') + assert.equal(sniffImageMime(JPEG), 'image/jpeg') + assert.equal(sniffImageMime(WEBP), 'image/webp') + assert.equal(sniffImageMime(new TextEncoder().encode('GIF89a')), null) + assert.equal(sniffImageMime(new Uint8Array([0x52, 0x49, 0x46, 0x46, 1, 0, 0, 0, 0x57, 0x41, 0x56, 0x45])), null) + assert.equal(sniffImageMime(new Uint8Array()), null) +}) + +test('sanitizeImageBaseName keeps a short safe stem and never a path or device name', () => { + assert.equal(sanitizeImageBaseName('My photo (1).png'), 'My-photo-1') + assert.equal(sanitizeImageBaseName('../../etc/passwd'), 'passwd') + assert.equal(sanitizeImageBaseName('C:\\Users\\me\\shot.webp'), 'shot') + assert.equal(sanitizeImageBaseName('Café déjà vu.jpg'), 'Cafe-deja-vu') + assert.equal(sanitizeImageBaseName('CON.png'), 'image') + assert.equal(sanitizeImageBaseName('..'), 'image') + assert.equal(sanitizeImageBaseName('.png'), 'png') + assert.equal(sanitizeImageBaseName('***'), 'image') + assert.equal(sanitizeImageBaseName(undefined), 'image') + assert.equal(sanitizeImageBaseName(42), 'image') + assert.ok(sanitizeImageBaseName('a'.repeat(300)).length <= 60) +}) + +test('workspaceImageFileName is time-stamped and suffixed', () => { + assert.equal(workspaceImageFileName('pasted', 'png', new Date(2026, 10, 16, 9, 5, 7), 'a1b2c3'), 'pasted-20261116-090507-a1b2c3.png') +}) + +test('validateWorkspaceImageRequest refuses missing, oversized, unsupported and mislabelled bytes', () => { + assert.ok('error' in validateWorkspaceImageRequest(undefined)) + assert.ok('error' in validateWorkspaceImageRequest({ bytes: 'abc', mime: 'image/png' })) + assert.ok('error' in validateWorkspaceImageRequest({ bytes: new Uint8Array(), mime: 'image/png' })) + assert.ok('error' in validateWorkspaceImageRequest({ bytes: PNG, mime: 'image/gif' })) + assert.ok('error' in validateWorkspaceImageRequest({ bytes: JPEG, mime: 'image/png' })) + const big = new Uint8Array(MAX_WORKSPACE_IMAGE_BYTES + 1) + big.set(PNG) + assert.ok('error' in validateWorkspaceImageRequest({ bytes: big, mime: 'image/png' })) + assert.deepEqual(validateWorkspaceImageRequest({ bytes: JPEG, mime: 'image/jpeg', name: 'x.jpeg' }), { bytes: JPEG, extension: 'jpg', baseName: 'x' }) +}) + +test('saveWorkspaceImage writes into /Images and never overwrites', async () => { + await withWorkspace(async (workspaceDir) => { + const suffixes = ['aaaaaa', 'aaaaaa', 'bbbbbb'] + const deps = { now: () => new Date(2026, 0, 2, 3, 4, 5), randomSuffix: () => suffixes.shift() ?? 'zzzzzz' } + const first = await saveWorkspaceImage(workspaceDir, { bytes: PNG, mime: 'image/png', name: '../evil' }, deps) + assert.deepEqual(first, { success: true, path: path.join(workspaceDir, 'Images', 'evil-20260102-030405-aaaaaa.png') }) + // Same name and suffix: the clash draws another suffix instead of overwriting. + const second = await saveWorkspaceImage(workspaceDir, { bytes: PNG, mime: 'image/png', name: 'evil.png' }, deps) + assert.ok(second.success) + assert.equal(path.basename(second.path), 'evil-20260102-030405-bbbbbb.png') + assert.deepEqual((await readdir(path.join(workspaceDir, 'Images'))).sort(), ['evil-20260102-030405-aaaaaa.png', 'evil-20260102-030405-bbbbbb.png']) + assert.deepEqual(new Uint8Array(await readFile(first.success ? first.path : '')), PNG) + }) +}) + +test('saveWorkspaceImage reports invalid requests without writing', async () => { + await withWorkspace(async (workspaceDir) => { + const result = await saveWorkspaceImage(workspaceDir, { bytes: new TextEncoder().encode(''), mime: 'image/svg+xml' }) + assert.equal(result.success, false) + assert.deepEqual(await readdir(workspaceDir), []) + assert.equal((await saveWorkspaceImage('', { bytes: PNG, mime: 'image/png' })).success, false) + }) +}) + +test('isRevealableImagePath accepts only existing absolute image files', async () => { + await withWorkspace(async (workspaceDir) => { + const saved = await saveWorkspaceImage(workspaceDir, { bytes: PNG, mime: 'image/png' }) + assert.ok(saved.success) + assert.equal(await isRevealableImagePath(saved.path), true) + assert.equal(await isRevealableImagePath(path.join(workspaceDir, 'missing.png')), false) + assert.equal(await isRevealableImagePath(path.join(workspaceDir, 'Images')), false) + assert.equal(await isRevealableImagePath('relative.png'), false) + assert.equal(await isRevealableImagePath(path.join(workspaceDir, 'script.exe')), false) + assert.equal(await isRevealableImagePath(null), false) + }) +}) + +test('the IPC handlers save into the current workspace and reveal only images', async () => { + await withWorkspace(async (workspaceDir) => { + const handlers = new Map Promise>() + const revealed: string[] = [] + registerWorkspaceImageIpcHandlers({ + ipcMain: { handle: (channel, handler) => { handlers.set(channel, handler) } }, + getWorkspaceDir: () => workspaceDir, + showItemInFolder: (p) => { revealed.push(p) }, + }) + const saved = await handlers.get('workspace:images:save')?.({}, { bytes: PNG, mime: 'image/png', name: 'pasted' }) as { success: boolean, path: string } + assert.ok(saved.success) + assert.ok(saved.path.startsWith(path.join(workspaceDir, 'Images') + path.sep)) + assert.deepEqual(await handlers.get('workspace:images:reveal')?.({}, saved.path), { success: true }) + assert.equal((await handlers.get('workspace:images:reveal')?.({}, workspaceDir) as { success: boolean }).success, false) + assert.deepEqual(revealed, [saved.path]) + }) +}) diff --git a/electron/main/workspace-images.ts b/electron/main/workspace-images.ts new file mode 100644 index 000000000..30dd1b5a4 --- /dev/null +++ b/electron/main/workspace-images.ts @@ -0,0 +1,152 @@ +import { randomBytes } from 'node:crypto' +import { mkdir, stat, writeFile } from 'node:fs/promises' +import { extname, isAbsolute, join } from 'node:path' + +import { resolvePathWithinRoot } from './extension-path-guard' + +// Images the renderer hands over as bytes (pasted from the clipboard, dropped +// from a browser) need a file on disk, since every image input in Modly is a +// path. They land in /Images/, never in the workspace's tmp/ +// folder, which is wiped at each workflow run. Images picked from disk are not +// copied: their own path is used. + +export const WORKSPACE_IMAGES_DIR = 'Images' +export const MAX_WORKSPACE_IMAGE_BYTES = 40 * 1024 * 1024 + +const EXTENSION_BY_MIME = { + 'image/png': 'png', + 'image/jpeg': 'jpg', + 'image/webp': 'webp', +} as const + +type ImageMime = keyof typeof EXTENSION_BY_MIME +type ImageExtension = (typeof EXTENSION_BY_MIME)[ImageMime] + +const REVEALABLE_EXTENSIONS = new Set(['.png', '.jpg', '.jpeg', '.webp']) +const WINDOWS_RESERVED_NAME = /^(con|prn|aux|nul|com[0-9]|lpt[0-9])$/i +const MAX_BASE_NAME_LENGTH = 60 + +export type WorkspaceImageSaveResult = + | { success: true, path: string } + | { success: false, error: string } + +function hasPrefix(bytes: Uint8Array, prefix: readonly number[], offset = 0): boolean { + return bytes.length >= offset + prefix.length && prefix.every((value, i) => bytes[offset + i] === value) +} + +/** The format the bytes actually are, from their signature; null when not PNG, JPEG or WebP. */ +export function sniffImageMime(bytes: Uint8Array): ImageMime | null { + if (hasPrefix(bytes, [0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a])) return 'image/png' + if (hasPrefix(bytes, [0xff, 0xd8, 0xff])) return 'image/jpeg' + if (hasPrefix(bytes, [0x52, 0x49, 0x46, 0x46]) && hasPrefix(bytes, [0x57, 0x45, 0x42, 0x50], 8)) return 'image/webp' + return null +} + +/** + * A short, filesystem-safe stem from a user-facing name: no extension, no path, + * only [A-Za-z0-9_-], never a reserved Windows device name. Falls back to "image". + */ +export function sanitizeImageBaseName(name: unknown): string { + if (typeof name !== 'string') return 'image' + const leaf = name.split(/[\\/]/).pop() ?? '' + const stem = leaf.slice(0, leaf.length - extname(leaf).length) || leaf + const safe = stem + .normalize('NFKD') + .replace(/[̀-ͯ]/g, '') + .replace(/[^A-Za-z0-9_-]+/g, '-') + .replace(/-{2,}/g, '-') + .replace(/^[-_]+|[-_]+$/g, '') + .slice(0, MAX_BASE_NAME_LENGTH) + .replace(/[-_]+$/g, '') + if (!safe || WINDOWS_RESERVED_NAME.test(safe)) return 'image' + return safe +} + +function pad(value: number): string { + return String(value).padStart(2, '0') +} + +/** `-YYYYMMDD-HHMMSS-.`: sortable by time, unique by suffix. */ +export function workspaceImageFileName(baseName: string, extension: ImageExtension, now: Date, suffix: string): string { + const stamp = `${now.getFullYear()}${pad(now.getMonth() + 1)}${pad(now.getDate())}-${pad(now.getHours())}${pad(now.getMinutes())}${pad(now.getSeconds())}` + return `${baseName}-${stamp}-${suffix}.${extension}` +} + +export interface ValidatedImageRequest { + bytes: Uint8Array + extension: ImageExtension + baseName: string +} + +/** + * Checks a save request from the renderer: bytes under the size cap whose + * signature is a supported image and agrees with the declared MIME type. + */ +export function validateWorkspaceImageRequest(payload: unknown): ValidatedImageRequest | { error: string } { + const values = (payload ?? {}) as { bytes?: unknown, name?: unknown, mime?: unknown } + if (!(values.bytes instanceof Uint8Array) || values.bytes.byteLength === 0) return { error: 'Image bytes are required.' } + if (values.bytes.byteLength > MAX_WORKSPACE_IMAGE_BYTES) return { error: 'Image is larger than 40 MB.' } + if (typeof values.mime !== 'string' || !(values.mime in EXTENSION_BY_MIME)) return { error: 'Only PNG, JPEG and WebP images can be saved.' } + const sniffed = sniffImageMime(values.bytes) + if (sniffed !== values.mime) return { error: 'Image content does not match its type.' } + return { bytes: values.bytes, extension: EXTENSION_BY_MIME[sniffed], baseName: sanitizeImageBaseName(values.name) } +} + +export interface SaveWorkspaceImageDeps { + now?: () => Date + randomSuffix?: () => string +} + +/** Writes the image to /Images/ under a fresh name and returns its absolute path. */ +export async function saveWorkspaceImage(workspaceDir: string, payload: unknown, deps: SaveWorkspaceImageDeps = {}): Promise { + const request = validateWorkspaceImageRequest(payload) + if ('error' in request) return { success: false, error: request.error } + if (typeof workspaceDir !== 'string' || !workspaceDir.trim()) return { success: false, error: 'No workspace folder is set.' } + + const now = deps.now ?? (() => new Date()) + const randomSuffix = deps.randomSuffix ?? (() => randomBytes(3).toString('hex')) + try { + const dir = resolvePathWithinRoot(workspaceDir, WORKSPACE_IMAGES_DIR) + await mkdir(dir, { recursive: true }) + for (let attempt = 0; attempt < 5; attempt++) { + const fileName = workspaceImageFileName(request.baseName, request.extension, now(), randomSuffix()) + const target = resolvePathWithinRoot(workspaceDir, join(WORKSPACE_IMAGES_DIR, fileName)) + try { + // 'wx' never overwrites: a clash just draws another suffix. + await writeFile(target, request.bytes, { flag: 'wx' }) + return { success: true, path: target } + } catch (error) { + if ((error as NodeJS.ErrnoException).code !== 'EEXIST') throw error + } + } + return { success: false, error: 'Could not find a free file name for the image.' } + } catch (error) { + return { success: false, error: error instanceof Error ? error.message : String(error) } + } +} + +/** An absolute path to an existing PNG/JPEG/WebP file, safe to reveal in the OS file manager. */ +export async function isRevealableImagePath(path: unknown): Promise { + if (typeof path !== 'string' || !isAbsolute(path)) return false + if (!REVEALABLE_EXTENSIONS.has(extname(path).toLowerCase())) return false + try { + return (await stat(path)).isFile() + } catch { + return false + } +} + +export interface WorkspaceImageIpcDeps { + ipcMain: { handle(channel: string, handler: (event: unknown, payload?: unknown) => Promise): void } + getWorkspaceDir: () => string + showItemInFolder: (path: string) => void +} + +export function registerWorkspaceImageIpcHandlers(deps: WorkspaceImageIpcDeps): void { + deps.ipcMain.handle('workspace:images:save', async (_event, payload) => saveWorkspaceImage(deps.getWorkspaceDir(), payload)) + deps.ipcMain.handle('workspace:images:reveal', async (_event, payload) => { + if (!(await isRevealableImagePath(payload))) return { success: false, error: 'Not an image file.' } + deps.showItemInFolder(payload as string) + return { success: true } + }) +} diff --git a/electron/preload/electron-api.ts b/electron/preload/electron-api.ts index 9a309e27e..6259d8604 100644 --- a/electron/preload/electron-api.ts +++ b/electron/preload/electron-api.ts @@ -259,6 +259,13 @@ export function createElectronApi(ipcRenderer: IpcRendererLike, webFrame: WebFra status: (workspacePath: string): Promise => ipcRenderer.invoke('workspace:thumbnails:status', { workspacePath }) as Promise, save: (request: AssetThumbnailSaveRequest): Promise => ipcRenderer.invoke('workspace:thumbnails:save', request) as Promise, }, + // Images handed over as bytes (pasted, dropped from a browser), saved to /Images/. + images: { + save: (request: { bytes: Uint8Array; name?: string; mime: string }): Promise<{ success: true; path: string } | { success: false; error: string }> => + ipcRenderer.invoke('workspace:images:save', request) as Promise<{ success: true; path: string } | { success: false; error: string }>, + reveal: (path: string): Promise<{ success: boolean; error?: string }> => + ipcRenderer.invoke('workspace:images:reveal', path) as Promise<{ success: boolean; error?: string }>, + }, }, // Extensions diff --git a/package.json b/package.json index dbb876925..34d094ba1 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,7 @@ "prepare-resources": "node scripts/download-python-embed.js", "test": "npm run test:py && npm run test:node", "test:py": "node scripts/run-pytests.mjs", - "test:node": "node --test --experimental-strip-types --experimental-loader ./scripts/node-ts-extensionless-loader.mjs src/shared/types/assetLibrary.test.ts src/areas/generate/assetLibraryProjection.test.ts src/areas/generate/assetLibraryService.test.ts src/areas/generate/assetLibraryUi.test.ts src/areas/library/libraryView.test.ts src/areas/library/gridWindow.test.ts src/areas/generate/orcaSlicerLink.test.ts src/shared/utils/agentStream.test.ts src/shared/thumbnails/assetThumbnailQueue.test.ts src/shared/thumbnails/assetThumbnailStore.test.ts src/shared/components/agent/clientTools.test.ts src/shared/components/agent/agentContext.test.ts src/shared/utils/ollamaHealth.test.ts src/shared/utils/notification.test.ts src/shared/utils/agentConversations.test.ts electron/main/artifact-registry-service.test.ts electron/main/asset-thumbnail-cache.test.ts electron/main/extension-path-guard.test.ts electron/main/storage-directory-guard.test.ts electron/main/storage-ownership.test.ts electron/main/startup-error.test.ts electron/main/api-port.test.ts electron/main/agent-conversations.test.ts electron/main/gpu-memory.test.ts electron/preload/artifact-registry-preload.test.ts src/shared/stores/meshHistory.test.ts src/areas/generate/meshEditPanel.test.ts src/areas/generate/sculpt/SculptMesh.test.ts src/areas/generate/sculpt/SculptMesh.perf.test.ts src/areas/generate/sculpt/sculptLogic.test.ts src/shared/pages/pageRegistry.test.ts src/shared/pages/startPage.test.ts src/areas/home/recents.test.ts src/areas/workflows/workflowRunRecord.test.ts src/areas/queue/queueModel.test.ts && node --test electron/main/*.test.mjs src/**/*.test.mjs", + "test:node": "node --test --experimental-strip-types --experimental-loader ./scripts/node-ts-extensionless-loader.mjs src/shared/types/assetLibrary.test.ts src/areas/generate/assetLibraryProjection.test.ts src/areas/generate/assetLibraryService.test.ts src/areas/generate/assetLibraryUi.test.ts src/areas/library/libraryView.test.ts src/areas/library/gridWindow.test.ts src/areas/generate/orcaSlicerLink.test.ts src/shared/utils/agentStream.test.ts src/shared/thumbnails/assetThumbnailQueue.test.ts src/shared/thumbnails/assetThumbnailStore.test.ts src/shared/components/agent/clientTools.test.ts src/shared/components/agent/agentContext.test.ts src/shared/utils/ollamaHealth.test.ts src/shared/utils/notification.test.ts src/shared/utils/agentConversations.test.ts electron/main/artifact-registry-service.test.ts electron/main/asset-thumbnail-cache.test.ts electron/main/extension-path-guard.test.ts electron/main/storage-directory-guard.test.ts electron/main/storage-ownership.test.ts electron/main/startup-error.test.ts electron/main/api-port.test.ts electron/main/agent-conversations.test.ts electron/main/gpu-memory.test.ts electron/preload/artifact-registry-preload.test.ts src/shared/stores/meshHistory.test.ts src/areas/generate/meshEditPanel.test.ts src/areas/generate/sculpt/SculptMesh.test.ts src/areas/generate/sculpt/SculptMesh.perf.test.ts src/areas/generate/sculpt/sculptLogic.test.ts src/shared/pages/pageRegistry.test.ts src/shared/pages/startPage.test.ts src/areas/home/recents.test.ts src/areas/workflows/workflowRunRecord.test.ts src/areas/queue/queueModel.test.ts electron/main/workspace-images.test.ts src/shared/utils/imageInput.test.ts && node --test electron/main/*.test.mjs src/**/*.test.mjs", "package": "cross-env CSC_IDENTITY_AUTO_DISCOVERY=false npm run build && npm run prepare-resources && electron-builder", "package:mac": "cross-env CSC_IDENTITY_AUTO_DISCOVERY=false npm run build && npm run prepare-resources && electron-builder --mac --arm64", "lint": "eslint ." diff --git a/src/areas/generate/components/WorkflowPanel.tsx b/src/areas/generate/components/WorkflowPanel.tsx index 493bf42c6..8d0241aa0 100644 --- a/src/areas/generate/components/WorkflowPanel.tsx +++ b/src/areas/generate/components/WorkflowPanel.tsx @@ -14,7 +14,7 @@ import { useWaitButton } from '@areas/workflows/useWaitButton' import { buildAllWorkflowExtensions, getWorkflowExtension } from '@areas/workflows/mockExtensions' import { validateWorkflowPreflight } from '@areas/workflows/preflight' import { mimeFromPath } from '@areas/workflows/nodes/imageUtils' -import { SUPPORTED_IMAGE_TYPES, useImageHandoff } from '@shared/utils/imageInput' +import { SUPPORTED_IMAGE_TYPES, saveImageToWorkspace, takePastedImage, useImageHandoff } from '@shared/utils/imageInput' import type { WorkflowExtension } from '@areas/workflows/mockExtensions' import type { Workflow, WFNode, WFEdge, ParamSchema } from '@shared/types/electron.d' import { PICKER_LABELS, openParamPicker, resolvePickerIntent } from '@shared/utils/paramPicker' @@ -160,7 +160,13 @@ async function readImageParams(path: string): Promise<{ filePath: string, previe return { filePath: path, preview: `data:${mimeFromPath(path)};base64,${base64}` } } -function ImageParamRow({ nodeId, nodes, onPatch }: { nodeId: string; nodes: FlowNode[]; onPatch: PatchFn }) { +function ImageParamRow({ nodeId, nodes, onPatch, pasteTarget = false }: { + nodeId: string + nodes: FlowNode[] + onPatch: PatchFn + /** Takes images pasted with Ctrl+V (only one row of the panel does). */ + pasteTarget?: boolean +}) { const node = nodes.find((n) => n.id === nodeId) const data = node?.data as { params: Record } | undefined const preview = data?.params.preview as string | undefined @@ -183,6 +189,23 @@ function ImageParamRow({ nodeId, nodes, onPatch }: { nodeId: string; nodes: Flow await applyImagePath(await window.electron.fs.selectImage()) }, [applyImagePath]) + // Ctrl+V outside text fields: the clipboard image is saved to the workspace + // (the node needs a file path), then loaded like a browsed one. + const applyRef = useRef(applyImagePath) + applyRef.current = applyImagePath + useEffect(() => { + if (!pasteTarget) return + const onPaste = (event: ClipboardEvent) => { + const file = takePastedImage(event) + if (!file) return + saveImageToWorkspace(file, 'pasted-image') + .then((path) => applyRef.current(path)) + .catch((err: unknown) => showToast(err instanceof Error ? err.message : 'Unable to paste the image')) + } + window.addEventListener('paste', onPaste) + return () => window.removeEventListener('paste', onPaste) + }, [pasteTarget, showToast]) + return (
    Browse image… + {pasteTarget && or paste with Ctrl+V} )}
    @@ -526,6 +550,8 @@ function EmbeddedCanvas({ workflow, allExtensions }: { // An image handed over by another page (Home) goes into the first image input // shown here, exactly as if it had been browsed in this panel. const pendingImage = useImageHandoff((s) => s.pendingPath) + // Ctrl+V goes to the same input. + const pasteTargetId = paramNodes.find((n) => n.type === 'imageNode')?.id useEffect(() => { const path = useImageHandoff.getState().take() if (!path) return @@ -567,7 +593,7 @@ function EmbeddedCanvas({ workflow, allExtensions }: { const isLast = i === paramNodes.length - 1 return (
    - {node.type === 'imageNode' && } + {node.type === 'imageNode' && } {node.type === 'textNode' && } {node.type === 'meshNode' && } {node.type === 'waitNode' && } diff --git a/src/shared/types/electron.d.ts b/src/shared/types/electron.d.ts index 49c8915b3..0935069ca 100644 --- a/src/shared/types/electron.d.ts +++ b/src/shared/types/electron.d.ts @@ -323,6 +323,11 @@ declare global { status: (workspacePath: string) => Promise save: (request: AssetThumbnailSaveRequest) => Promise } + /** Saves image bytes (PNG/JPEG/WebP, max 40 MB) to /Images/ and returns the new file's absolute path. */ + images: { + save: (request: { bytes: Uint8Array; name?: string; mime: string }) => Promise<{ success: true; path: string } | { success: false; error: string }> + reveal: (path: string) => Promise<{ success: boolean; error?: string }> + } } setup: { check: () => Promise<{ needed: boolean; defaultDataDir: string; platform: string; arch: string }> diff --git a/src/shared/utils/imageInput.test.ts b/src/shared/utils/imageInput.test.ts new file mode 100644 index 000000000..0874ec19e --- /dev/null +++ b/src/shared/utils/imageInput.test.ts @@ -0,0 +1,25 @@ +import test from 'node:test' +import assert from 'node:assert/strict' +import { clipboardImage, isTextEntryTarget } from './imageInput' + +const item = (kind: string, type: string, file: unknown = { name: type }) => ({ kind, type, getAsFile: () => file as File }) + +test('clipboardImage returns the first supported image file', () => { + assert.equal(clipboardImage(null), null) + assert.equal(clipboardImage({ items: [item('string', 'text/plain')] }), null) + assert.equal(clipboardImage({ items: [item('file', 'image/gif')] }), null) + const png = { name: 'image.png' } + assert.equal(clipboardImage({ items: [item('string', 'text/html'), item('file', 'image/png', png), item('file', 'image/jpeg')] }), png) +}) + +test('isTextEntryTarget is true only where Ctrl+V should paste text', () => { + assert.equal(isTextEntryTarget(null), false) + assert.equal(isTextEntryTarget({ tagName: 'BODY' }), false) + assert.equal(isTextEntryTarget({ tagName: 'BUTTON' }), false) + assert.equal(isTextEntryTarget({ tagName: 'TEXTAREA' }), true) + assert.equal(isTextEntryTarget({ tagName: 'INPUT', type: 'text' }), true) + assert.equal(isTextEntryTarget({ tagName: 'INPUT', type: 'number' }), true) + assert.equal(isTextEntryTarget({ tagName: 'INPUT', type: 'checkbox' }), false) + assert.equal(isTextEntryTarget({ tagName: 'INPUT', type: 'range' }), false) + assert.equal(isTextEntryTarget({ tagName: 'DIV', isContentEditable: true }), true) +}) diff --git a/src/shared/utils/imageInput.ts b/src/shared/utils/imageInput.ts index 2aeba8846..84f9ba2e0 100644 --- a/src/shared/utils/imageInput.ts +++ b/src/shared/utils/imageInput.ts @@ -28,3 +28,49 @@ export function droppedImagePath(file: File | undefined): string | null { if (!file || !SUPPORTED_IMAGE_TYPES.has(file.type)) return null return window.electron.fs.getPathForFile(file) || null } + +/** Where a supported image dropped or pasted without a disk path is saved: a workspace file. */ +export async function saveImageToWorkspace(blob: Blob, name?: string): Promise { + const bytes = new Uint8Array(await blob.arrayBuffer()) + const result = await window.electron.workspace.images.save({ bytes, name, mime: blob.type }) + if (!result.success) throw new Error(result.error) + return result.path +} + +interface ClipboardItemLike { + kind: string + type: string + getAsFile: () => File | null +} + +/** The first supported image in a paste's clipboard data, or null. */ +export function clipboardImage(data: { items: ArrayLike } | null | undefined): File | null { + if (!data) return null + for (const item of Array.from(data.items)) { + if (item.kind === 'file' && SUPPORTED_IMAGE_TYPES.has(item.type)) return item.getAsFile() + } + return null +} + +/** Whether a paste lands in something that takes text, where Ctrl+V must keep its usual meaning. */ +export function isTextEntryTarget(target: unknown): boolean { + const el = target as { tagName?: unknown, isContentEditable?: unknown, type?: unknown } | null + if (!el || typeof el !== 'object') return false + if (el.isContentEditable === true) return true + const tag = typeof el.tagName === 'string' ? el.tagName.toUpperCase() : '' + if (tag === 'TEXTAREA' || tag === 'SELECT') return true + if (tag !== 'INPUT') return false + const type = typeof el.type === 'string' ? el.type.toLowerCase() : 'text' + return !['button', 'checkbox', 'color', 'file', 'image', 'radio', 'range', 'reset', 'submit'].includes(type) +} + +/** + * The image a paste event carries, unless it was aimed at a text field. Call + * from a window 'paste' listener; it prevents the default when it takes one. + */ +export function takePastedImage(event: ClipboardEvent): File | null { + if (isTextEntryTarget(event.target) || isTextEntryTarget(document.activeElement)) return null + const file = clipboardImage(event.clipboardData) + if (file) event.preventDefault() + return file +}