Skip to content

Image Pipeline Playground

feat(shared/types): mark ports as optional in PortSpec

Mohammad Rajaei0abecb592962
diff --git a/backend/app/models/graph.py b/backend/app/models/graph.py
index 10fddd2..b802699 100644
--- a/backend/app/models/graph.py
+++ b/backend/app/models/graph.py
@@ -19,6 +19,7 @@ class PortSpec(BaseModel):
direction: PortDirection
data_type: str = "image"
multiple: bool = False
+ optional: bool = False
class NodeInstance(BaseModel):
diff --git a/backend/app/nodes/common.py b/backend/app/nodes/common.py
index 6d0e3f5..e626951 100644
--- a/backend/app/nodes/common.py
+++ b/backend/app/nodes/common.py
@@ -9,8 +9,19 @@ import numpy as np
from app.models.graph import ParamField, PortDirection, PortSpec
-def image_in(port_id: str = "image", name: str = "Image") -> PortSpec:
- return PortSpec(id=port_id, name=name, direction=PortDirection.INPUT, data_type="image")
+def image_in(
+ port_id: str = "image",
+ name: str = "Image",
+ *,
+ optional: bool = False,
+) -> PortSpec:
+ return PortSpec(
+ id=port_id,
+ name=name,
+ direction=PortDirection.INPUT,
+ data_type="image",
+ optional=optional,
+ )
def image_out(
diff --git a/backend/tests/test_port_optional.py b/backend/tests/test_port_optional.py
new file mode 100644
index 0000000..d41fb2f
--- /dev/null
+++ b/backend/tests/test_port_optional.py
@@ -0,0 +1,18 @@
+"""Tests for optional PortSpec metadata."""
+
+from __future__ import annotations
+
+from app.models.graph import PortDirection, PortSpec
+from app.nodes.common import image_in
+
+
+def test_port_spec_optional_defaults_false() -> None:
+ port = PortSpec(id="image", name="Image", direction=PortDirection.INPUT)
+ assert port.optional is False
+
+
+def test_image_in_optional_flag() -> None:
+ port = image_in("size_ref", "Size Ref", optional=True)
+ assert port.id == "size_ref"
+ assert port.optional is True
+ assert port.direction == PortDirection.INPUT
diff --git a/frontend/src/features/canvas/PipelineNode.tsx b/frontend/src/features/canvas/PipelineNode.tsx
index 8c0d7ac..8ddf508 100644
--- a/frontend/src/features/canvas/PipelineNode.tsx
+++ b/frontend/src/features/canvas/PipelineNode.tsx
@@ -469,7 +469,8 @@ export function PipelineNodeView({ id, data, selected }: NodeProps<PipelineFlowN
{inputPorts.map((port, index) => {
const top = sideHandleTop(index, Math.max(inputPorts.length, 1), contentHeight)
- const showLabel = inputPorts.length > 1 || port.id !== 'image'
+ const showLabel =
+ inputPorts.length > 1 || port.id !== 'image' || Boolean(port.optional)
return (
<Box key={`in-${port.id}`}>
<Handle
@@ -483,7 +484,13 @@ export function PipelineNodeView({ id, data, selected }: NodeProps<PipelineFlowN
background: '#7dcea0',
}}
/>
- {showLabel && <PortTag label={port.name} top={top} side="left" />}
+ {showLabel && (
+ <PortTag
+ label={port.optional ? `${port.name} (optional)` : port.name}
+ top={top}
+ side="left"
+ />
+ )}
</Box>
)
})}
@@ -504,7 +511,13 @@ export function PipelineNodeView({ id, data, selected }: NodeProps<PipelineFlowN
background: '#e67e22',
}}
/>
- {showLabel && <PortTag label={port.name} top={top} side="right" />}
+ {showLabel && (
+ <PortTag
+ label={port.optional ? `${port.name} (optional)` : port.name}
+ top={top}
+ side="right"
+ />
+ )}
</Box>
)
})}
diff --git a/frontend/src/types/graph.ts b/frontend/src/types/graph.ts
index bc361be..c1dd27f 100644
--- a/frontend/src/types/graph.ts
+++ b/frontend/src/types/graph.ts
@@ -6,6 +6,7 @@ export interface PortSpec {
direction: PortDirection
data_type: string
multiple: boolean
+ optional?: boolean
}
export interface ParamField {