|
| 1 | +"""Base classes for Project Workflow nodes and common utilities.""" |
| 2 | + |
| 3 | +import logging |
| 4 | +from typing import Dict, List, Any, Optional |
| 5 | +from pydantic import BaseModel, ConfigDict, Field |
| 6 | + |
| 7 | +from labelbox.schema.workflow.enums import WorkflowDefinitionId, NodeOutput |
| 8 | + |
| 9 | +logger = logging.getLogger(__name__) |
| 10 | + |
| 11 | + |
| 12 | +class NodePosition(BaseModel): |
| 13 | + """Position of a node in the workflow canvas.""" |
| 14 | + |
| 15 | + x: float |
| 16 | + y: float |
| 17 | + |
| 18 | + |
| 19 | +class BaseWorkflowNode(BaseModel): |
| 20 | + """Base class for all workflow node types.""" |
| 21 | + |
| 22 | + id: str |
| 23 | + position: NodePosition |
| 24 | + raw_data: Dict[str, Any] = Field( |
| 25 | + default_factory=lambda: {}, |
| 26 | + description="Store the original dict for reference", |
| 27 | + ) |
| 28 | + inputs: List[str] = Field( |
| 29 | + default_factory=lambda: [], description="Input connection identifiers" |
| 30 | + ) |
| 31 | + output_if: Optional[str] = Field( |
| 32 | + default=None, description="Primary/if output connection identifier" |
| 33 | + ) |
| 34 | + output_else: Optional[str] = Field( |
| 35 | + default=None, description="Secondary/else output connection identifier" |
| 36 | + ) |
| 37 | + definition_id: WorkflowDefinitionId = Field( |
| 38 | + default=WorkflowDefinitionId.Unknown, |
| 39 | + frozen=True, |
| 40 | + alias="definitionId", |
| 41 | + ) |
| 42 | + model_config = ConfigDict( |
| 43 | + arbitrary_types_allowed=True, |
| 44 | + populate_by_name=True, |
| 45 | + ) |
| 46 | + |
| 47 | + def _update_node_in_workflow(self) -> None: |
| 48 | + """Update the node data in the workflow's config.""" |
| 49 | + workflow = self.raw_data.get("_workflow") |
| 50 | + if workflow and hasattr(workflow, "config"): |
| 51 | + for node_data in workflow.config.get("nodes", []): |
| 52 | + if node_data.get("id") == self.id: |
| 53 | + # Update the workflow config with current node state |
| 54 | + if hasattr(self, "label"): |
| 55 | + node_data["label"] = self.label |
| 56 | + if hasattr(self, "custom_fields"): |
| 57 | + node_data["customFields"] = self.custom_fields |
| 58 | + if ( |
| 59 | + hasattr(self, "instructions") |
| 60 | + and self.instructions is not None |
| 61 | + ): |
| 62 | + # Ensure customFields exists |
| 63 | + if "customFields" not in node_data: |
| 64 | + node_data["customFields"] = {} |
| 65 | + node_data["customFields"]["description"] = ( |
| 66 | + self.instructions |
| 67 | + ) |
| 68 | + break |
| 69 | + |
| 70 | + @property |
| 71 | + def name(self) -> Optional[str]: |
| 72 | + """Get the node's name (label).""" |
| 73 | + return self.raw_data.get("label") |
| 74 | + |
| 75 | + @name.setter |
| 76 | + def name(self, value: str) -> None: |
| 77 | + """Set the node's name (label).""" |
| 78 | + if hasattr(self, "label"): |
| 79 | + self.label = value |
| 80 | + self.raw_data["label"] = value |
| 81 | + self._update_node_in_workflow() |
| 82 | + |
| 83 | + @property |
| 84 | + def supported_outputs(self) -> List[NodeOutput]: |
| 85 | + """Returns the list of supported output types for this node.""" |
| 86 | + return [] |
| 87 | + |
| 88 | + @property |
| 89 | + def config(self) -> Dict[str, Any]: |
| 90 | + """Returns the node's configuration.""" |
| 91 | + return self.raw_data |
| 92 | + |
| 93 | + def __repr__(self): |
| 94 | + """String representation of the node.""" |
| 95 | + return f"{self.__class__.__name__}(id={self.id}, name={self.name})" |
0 commit comments