# erickjtorres/app_use architecture

> Architecture diagram of the GitHub repository erickjtorres/app_use, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-21). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/erickjtorres/app_use
- Repository: https://github.com/erickjtorres/app_use

## Overview

##### Project Type and Purpose
- **Type**: An open-source Python library/tool for AI‐powered UI automation of mobile apps (currently Flutter, with React Native planned).
- **Purpose**: Expose a simple API so that AI agents (via LLMs) can inspect and interact with a running mobile app’s UI tree over the Flutter Dart VM service (and future frameworks).

##### File Structure
- **app_use/agent/**: Core “Agent” abstractions, orchestration of prompts, LLM integration, history, and interaction flows.
- **app_use/app/**: “App” layer—connects to the mobile app’s VM service (WebSocket), provides low-level commands.
- **app_use/controller/**: Registry of controllers, command dispatch, high-level API for actions like tap, scroll, find.
- **app_use/agent/message_manager/**: Handles message formatting, retries, error management, and view rendering of results.
- **app_use/nodes/**: Builds and traverses UI element trees (WidgetTreeBuilder, ElementTreeBuilder), abstracts UI hierarchy.
- **examples/**: Quickstart scripts showing integration with ChatOpenAI and the AppUse API.

##### Extract Key Insights from README
- **External Dependencies**: LLM (ChatOpenAI), WebSocket VM service (FlutterDriver), Python libraries.
- **Core Workflow**:
1. To VM service
2. Pull UI tree
3. Agent issues high-level tasks
4. Controller translates to low-level commands
5. App layer executes commands over WebSocket
6. Feedback loop to LLM
- **Extensibility**: Custom Agents, new frameworks (React Native), widget interaction patterns.

##### Main Components & Their Relationships
a. **Agent Layer**
- Contains: task prompt, LLM client (ChatOpenAI), run loop, History object
- Outbound: sends “intents” or “actions” to Message Manager/Controller
- Inbound: receives UI‐state snapshots, command results
b. **Message Manager**
- Role: formats messages, handles retries, structures prompts/ responses
c. **Controller Layer**
- Role: Maps high‐level intents to platform commands (e.g., tap(widget_id), scroll)
d. **App Layer**
- Role: Opens WebSocket to Dart VM service, sends raw JSON RPC commands
e. **External Systems**
- LLM Provider (OpenAI)
- Flutter App (with driver extension enabled)
- WebSocket VM Service (Dart VM)
f. **Nodes / UI Tree**
- Role: Construct in‐memory representation of UI hierarchy for the Agent’s decision making

##### Architectural Patterns & Principles
- **Layered Architecture**: clear separation—Agent (orchestration), Controller (translation), App (transport), Nodes (model).
- **Adapter/Bridge**: App layer adapts between Python and the Dart VM service.
- **Extensibility Points**: Agent subclasses, controller registry, app adapters for new frameworks.
- **Context Management**: `async with` patterns for resource safety.

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% External Systems
    subgraph "External Systems"
        OpenAI["OpenAI LLM API"]:::external
        VMService["Dart VM Service\n(WebSocket)"]:::external
        FlutterApp["Flutter App\n(driver extension)"]:::external
    end

    %% Agent Layer
    subgraph "Agent Layer"
        AgentService["Agent Service\napp_use/agent/service.py"]:::internal
        AgentViews["Agent Views\napp_use/agent/views.py"]:::internal
    end

    %% Message Manager
    subgraph "Message Manager"
        MMService["MessageManager Service\napp_use/agent/message_manager/service.py"]:::internal
        MMUtils["MessageManager Utils\napp_use/agent/message_manager/utils.py"]:::internal
        MMViews["MessageManager Views\napp_use/agent/message_manager/views.py"]:::internal
    end

    %% Controller Layer
    subgraph "Controller Layer"
        ControllerService["Controller Service\napp_use/controller/service.py"]:::internal
        RegistryService["Registry Service\napp_use/controller/registry/service.py"]:::internal
        ControllerViews["Controller Views\napp_use/controller/views.py"]:::internal
    end

    %% App Layer
    subgraph "App Layer"
        AppCore["App Core\napp_use/app/app.py"]:::internal
        FlutterAdapter["Flutter App Adapter\napp_use/app/flutter_app.py"]:::internal
        RNAdapter["React Native Adapter\napp_use/app/react_native_app.py"]:::internal
    end

    %% Nodes / UI Tree
    subgraph "Nodes / UI Tree"
        ElementBuilder["ElementTreeBuilder\napp_use/nodes/element_tree_builder.py"]:::internal
        WidgetBuilder["WidgetTreeBuilder\napp_use/nodes/widget_tree_builder.py"]:::internal
        AppNode["AppNode\napp_use/nodes/app_node.py"]:::internal
    end

    %% Examples
    subgraph "Examples"
        FlutterExample["flutter_agent_example.py"]:::internal
        RNExample["react_agent_example.py"]:::internal
    end

    %% Connections
    AgentService -->|"formats prompts"| MMService
    MMService -->|"parsed action"| ControllerService
    ControllerService -->|"dispatch commands"| AppCore
    AppCore -->|"JSON-RPC over WebSocket"| VMService
    VMService -->|"forwards to driver"| FlutterApp
    FlutterApp -->|"UI tree & responses"| VMService
    VMService -->|"responses JSON-RPC"| AppCore
    AppCore -->|"raw results"| ControllerService
    ControllerService -->|"results"| MMService
    MMService -->|"formatted feedback"| AgentService
    AgentService -->|"LLM prompt/response"| OpenAI
    OpenAI -->|"LLM response"| AgentService

    %% Click Events
    click AgentService "https://github.com/erickjtorres/app_use/blob/main/app_use/agent/service.py"
    click AgentViews "https://github.com/erickjtorres/app_use/blob/main/app_use/agent/views.py"
    click MMService "https://github.com/erickjtorres/app_use/blob/main/app_use/agent/message_manager/service.py"
    click MMUtils "https://github.com/erickjtorres/app_use/blob/main/app_use/agent/message_manager/utils.py"
    click MMViews "https://github.com/erickjtorres/app_use/blob/main/app_use/agent/message_manager/views.py"
    click ControllerService "https://github.com/erickjtorres/app_use/blob/main/app_use/controller/service.py"
    click RegistryService "https://github.com/erickjtorres/app_use/blob/main/app_use/controller/registry/service.py"
    click ControllerViews "https://github.com/erickjtorres/app_use/blob/main/app_use/controller/views.py"
    click AppCore "https://github.com/erickjtorres/app_use/blob/main/app_use/app/app.py"
    click FlutterAdapter "https://github.com/erickjtorres/app_use/blob/main/app_use/app/flutter_app.py"
    click RNAdapter "https://github.com/erickjtorres/app_use/blob/main/app_use/app/react_native_app.py"
    click ElementBuilder "https://github.com/erickjtorres/app_use/blob/main/app_use/nodes/element_tree_builder.py"
    click WidgetBuilder "https://github.com/erickjtorres/app_use/blob/main/app_use/nodes/widget_tree_builder.py"
    click AppNode "https://github.com/erickjtorres/app_use/blob/main/app_use/nodes/app_node.py"
    click FlutterExample "https://github.com/erickjtorres/app_use/blob/main/examples/flutter_agent_example.py"
    click RNExample "https://github.com/erickjtorres/app_use/blob/main/examples/react_agent_example.py"

    %% Styles
    classDef internal fill:#BBDEFB,stroke:#2196F3,color:#0D47A1
    classDef external fill:#C8E6C9,stroke:#388E3C,color:#1B5E20
```
