|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Script to generate graph visualization from the LangGraph structure. |
| 4 | +This creates both a PNG image and a Mermaid markdown file. |
| 5 | +It updates the README.md file with the generated graph. |
| 6 | +""" |
| 7 | + |
| 8 | +import asyncio |
| 9 | +import sys |
| 10 | +from pathlib import Path |
| 11 | + |
| 12 | +from langchain_core.runnables.graph import CurveStyle, MermaidDrawMethod, NodeStyles |
| 13 | +from langchain_core.runnables.graph_mermaid import draw_mermaid_png |
| 14 | +from langgraph.graph.state import CompiledStateGraph |
| 15 | + |
| 16 | +from minitap.mobile_use.clients.device_hardware_client import DeviceHardwareClient |
| 17 | +from minitap.mobile_use.clients.screen_api_client import ScreenApiClient |
| 18 | +from minitap.mobile_use.config import get_default_llm_config |
| 19 | +from minitap.mobile_use.context import ( |
| 20 | + DeviceContext, |
| 21 | + DevicePlatform, |
| 22 | +) |
| 23 | + |
| 24 | +sys.path.append(str(Path(__file__).parent.parent)) |
| 25 | + |
| 26 | + |
| 27 | +async def generate_graph_docs(): |
| 28 | + """Generate graph visualization as PNG.""" |
| 29 | + from minitap.mobile_use.context import MobileUseContext |
| 30 | + from minitap.mobile_use.graph.graph import get_graph |
| 31 | + |
| 32 | + print("Loading graph structure...") |
| 33 | + ctx = MobileUseContext( |
| 34 | + device=DeviceContext( |
| 35 | + host_platform="LINUX", |
| 36 | + mobile_platform=DevicePlatform.ANDROID, |
| 37 | + device_id="device_id", |
| 38 | + device_width=1080, |
| 39 | + device_height=1920, |
| 40 | + ), |
| 41 | + hw_bridge_client=DeviceHardwareClient(base_url="http://localhost:8000"), |
| 42 | + screen_api_client=ScreenApiClient(base_url="http://localhost:8000"), |
| 43 | + llm_config=get_default_llm_config(), |
| 44 | + ) |
| 45 | + |
| 46 | + print("Generating graph...") |
| 47 | + graph: CompiledStateGraph = await get_graph(ctx) |
| 48 | + |
| 49 | + png_path = Path(__file__).parent.parent.parent / "doc" / "graph.png" |
| 50 | + print(f"Generating PNG at {png_path}...") |
| 51 | + |
| 52 | + mermaid_text = graph.get_graph().draw_mermaid( |
| 53 | + node_colors=NodeStyles( |
| 54 | + default="fill:#d0c4f2,stroke:#b3b3b3,stroke-width:1px,color:#ffffff", |
| 55 | + first="fill:#9998e1,stroke:#b3b3b3,stroke-width:1px,color:#ffffff", |
| 56 | + last="fill:#9998e1,stroke:#b3b3b3,stroke-width:1px,color:#ffffff", |
| 57 | + ), |
| 58 | + curve_style=CurveStyle.LINEAR, |
| 59 | + frontmatter_config={ |
| 60 | + "config": { |
| 61 | + "themeVariables": { |
| 62 | + "lineColor": "#ffffff", |
| 63 | + }, |
| 64 | + } |
| 65 | + }, |
| 66 | + ) |
| 67 | + |
| 68 | + draw_mermaid_png( |
| 69 | + mermaid_syntax=mermaid_text, |
| 70 | + output_file_path=str(png_path), |
| 71 | + draw_method=MermaidDrawMethod.API, |
| 72 | + background_color=None, |
| 73 | + ) |
| 74 | + |
| 75 | + |
| 76 | +if __name__ == "__main__": |
| 77 | + asyncio.run(generate_graph_docs()) |
0 commit comments