"CLI-Anything: Making ALL Software Agent-Native" -- CLI-Hub: https://clianything.cc/
Python
fix: #132 add drawio custom id
Xilonng Zhang18fece7771c9
diff --git a/drawio/agent-harness/cli_anything/drawio/core/connectors.py b/drawio/agent-harness/cli_anything/drawio/core/connectors.pyindex 1027baff3..478a8ea68 100644--- a/drawio/agent-harness/cli_anything/drawio/core/connectors.py+++ b/drawio/agent-harness/cli_anything/drawio/core/connectors.py@@ -14,7 +14,7 @@ def list_connectors(session: Session, diagram_index: int = 0) -> list[dict]:def add_connector(session: Session, source_id: str, target_id: str,edge_style: str = "orthogonal", label: str = "",- diagram_index: int = 0) -> dict:+ diagram_index: int = 0, edge_id: str = None) -> dict:"""Add a connector between two shapes.Args:@@ -24,6 +24,7 @@ def add_connector(session: Session, source_id: str, target_id: str,edge_style: Edge style preset (straight, orthogonal, curved,entity-relation) or raw style string.label: Optional label on the edge.+ edge_id: Optional custom ID. Auto-generated if not provided.Returns:Dict with action and new edge info.@@ -40,7 +41,7 @@ def add_connector(session: Session, source_id: str, target_id: str,session.checkpoint()edge_id = drawio_xml.add_edge(session.root, source_id, target_id, edge_style, label,- diagram_index=diagram_index,+ diagram_index=diagram_index, edge_id=edge_id,)return {diff --git a/drawio/agent-harness/cli_anything/drawio/core/shapes.py b/drawio/agent-harness/cli_anything/drawio/core/shapes.pyindex 495c7d99d..64b455a09 100644--- a/drawio/agent-harness/cli_anything/drawio/core/shapes.py+++ b/drawio/agent-harness/cli_anything/drawio/core/shapes.py@@ -15,7 +15,8 @@ def list_shapes(session: Session, diagram_index: int = 0) -> list[dict]:def add_shape(session: Session, shape_type: str = "rectangle",x: float = 100, y: float = 100,width: float = 120, height: float = 60,- label: str = "", diagram_index: int = 0) -> dict:+ label: str = "", diagram_index: int = 0,+ cell_id: str = None) -> dict:"""Add a shape to the diagram.Args:@@ -26,6 +27,7 @@ def add_shape(session: Session, shape_type: str = "rectangle",x, y: Position.width, height: Dimensions.label: Text label.+ cell_id: Optional custom ID. Auto-generated if not provided.Returns:Dict with action and new shape info.@@ -36,7 +38,7 @@ def add_shape(session: Session, shape_type: str = "rectangle",session.checkpoint()cell_id = drawio_xml.add_vertex(session.root, shape_type, x, y, width, height, label,- diagram_index=diagram_index,+ diagram_index=diagram_index, cell_id=cell_id,)return {diff --git a/drawio/agent-harness/cli_anything/drawio/drawio_cli.py b/drawio/agent-harness/cli_anything/drawio/drawio_cli.pyindex 42a23d553..f2ec1d435 100644--- a/drawio/agent-harness/cli_anything/drawio/drawio_cli.py+++ b/drawio/agent-harness/cli_anything/drawio/drawio_cli.py@@ -255,15 +255,16 @@ def shape():@click.option("--width", "-w", type=float, default=120, help="Width")@click.option("--height", "-h", type=float, default=60, help="Height")@click.option("--page", type=int, default=0, help="Page index")[email protected]("--id", "cell_id", default=None, help="Custom cell ID (auto-generated if omitted)")@handle_error-def shape_add(shape_type, label, x, y, width, height, page):+def shape_add(shape_type, label, x, y, width, height, page, cell_id):"""Add a shape to the diagram.SHAPE_TYPE: rectangle, rounded, ellipse, diamond, triangle, hexagon,cylinder, cloud, parallelogram, process, document, callout, note, actor, text"""session = get_session()- result = shapes_mod.add_shape(session, shape_type, x, y, width, height, label, page)+ result = shapes_mod.add_shape(session, shape_type, x, y, width, height, label, page, cell_id)output(result, f"Added {shape_type}: {result['id']}")@@ -380,11 +381,12 @@ def connect():help="Edge style")@click.option("--label", "-l", default="", help="Edge label")@click.option("--page", type=int, default=0, help="Page index")[email protected]("--id", "edge_id", default=None, help="Custom edge ID (auto-generated if omitted)")@handle_error-def connect_add(source_id, target_id, edge_style, label, page):+def connect_add(source_id, target_id, edge_style, label, page, edge_id):"""Add a connector between two shapes."""session = get_session()- result = conn_mod.add_connector(session, source_id, target_id, edge_style, label, page)+ result = conn_mod.add_connector(session, source_id, target_id, edge_style, label, page, edge_id)output(result, f"Connected: {source_id} → {target_id}")diff --git a/drawio/agent-harness/cli_anything/drawio/tests/test_core.py b/drawio/agent-harness/cli_anything/drawio/tests/test_core.pyindex 1d2a29978..706edfb55 100644--- a/drawio/agent-harness/cli_anything/drawio/tests/test_core.py+++ b/drawio/agent-harness/cli_anything/drawio/tests/test_core.py@@ -66,6 +66,34 @@ class TestDrawioXml:assert edges[0].get("target") == v2assert edges[0].get("value") == "connects"+ def test_add_vertex_custom_id(self):+ root = drawio_xml.create_blank_diagram()+ cell_id = drawio_xml.add_vertex(root, "rectangle", 0, 0, 100, 50, "X", cell_id="my-node")+ assert cell_id == "my-node"+ assert drawio_xml.find_cell_by_id(root, "my-node") is not None++ def test_add_vertex_duplicate_id_raises(self):+ root = drawio_xml.create_blank_diagram()+ drawio_xml.add_vertex(root, "rectangle", 0, 0, 100, 50, cell_id="dup")+ with pytest.raises(ValueError, match="already exists"):+ drawio_xml.add_vertex(root, "ellipse", 200, 0, 100, 50, cell_id="dup")++ def test_add_edge_custom_id(self):+ root = drawio_xml.create_blank_diagram()+ v1 = drawio_xml.add_vertex(root, "rectangle", 0, 0, 100, 50)+ v2 = drawio_xml.add_vertex(root, "rectangle", 200, 0, 100, 50)+ edge_id = drawio_xml.add_edge(root, v1, v2, edge_id="my-edge")+ assert edge_id == "my-edge"+ assert drawio_xml.find_cell_by_id(root, "my-edge") is not None++ def test_add_edge_duplicate_id_raises(self):+ root = drawio_xml.create_blank_diagram()+ v1 = drawio_xml.add_vertex(root, "rectangle", 0, 0, 100, 50)+ v2 = drawio_xml.add_vertex(root, "rectangle", 200, 0, 100, 50)+ drawio_xml.add_edge(root, v1, v2, edge_id="dup-edge")+ with pytest.raises(ValueError, match="already exists"):+ drawio_xml.add_edge(root, v1, v2, edge_id="dup-edge")+def test_remove_cell(self):root = drawio_xml.create_blank_diagram()v1 = drawio_xml.add_vertex(root, "rectangle", 10, 20, 120, 60, "A")diff --git a/drawio/agent-harness/cli_anything/drawio/utils/drawio_xml.py b/drawio/agent-harness/cli_anything/drawio/utils/drawio_xml.pyindex dc827b607..fc031bd61 100644--- a/drawio/agent-harness/cli_anything/drawio/utils/drawio_xml.py+++ b/drawio/agent-harness/cli_anything/drawio/utils/drawio_xml.py@@ -301,7 +301,8 @@ def _new_id(prefix: str = "cell") -> str:def add_vertex(mxfile: ET.Element, shape_type: str,x: float, y: float, width: float, height: float,label: str = "", parent: str = "1",- diagram_index: int = 0) -> str:+ diagram_index: int = 0,+ cell_id: Optional[str] = None) -> str:"""Add a shape (vertex) to the diagram.Args:@@ -311,12 +312,16 @@ def add_vertex(mxfile: ET.Element, shape_type: str,width, height: Dimensions.label: Text label for the shape.parent: Parent cell ID (default "1" = default layer).+ cell_id: Optional custom ID. Auto-generated if not provided.Returns:The new cell's ID."""root = get_root(mxfile, diagram_index)- cell_id = _new_id("v")+ if cell_id is None:+ cell_id = _new_id("v")+ elif find_cell_by_id(mxfile, cell_id, diagram_index) is not None:+ raise ValueError(f"Cell ID already exists: {cell_id}")cell = ET.SubElement(root, "mxCell")cell.set("id", cell_id)@@ -343,7 +348,8 @@ def add_vertex(mxfile: ET.Element, shape_type: str,def add_edge(mxfile: ET.Element, source_id: str, target_id: str,edge_style: str = "orthogonal", label: str = "",- parent: str = "1", diagram_index: int = 0) -> str:+ parent: str = "1", diagram_index: int = 0,+ edge_id: Optional[str] = None) -> str:"""Add an edge (connector) between two cells.Args:@@ -353,12 +359,16 @@ def add_edge(mxfile: ET.Element, source_id: str, target_id: str,edge_style: Edge style preset name (see EDGE_STYLES) or raw style string.label: Optional edge label.parent: Parent cell ID.+ edge_id: Optional custom ID. Auto-generated if not provided.Returns:The new edge's ID."""root = get_root(mxfile, diagram_index)- edge_id = _new_id("e")+ if edge_id is None:+ edge_id = _new_id("e")+ elif find_cell_by_id(mxfile, edge_id, diagram_index) is not None:+ raise ValueError(f"Cell ID already exists: {edge_id}")cell = ET.SubElement(root, "mxCell")cell.set("id", edge_id)