# xlrpa/workbot architecture

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

- Interactive diagram: https://gitdiagram.com/xlrpa/workbot
- Repository: https://github.com/xlrpa/workbot

## Overview

##### Project type and purpose
- This is a multi‐module Android client application (the “APP” side of WorkBot) that implements an RPA (Robotic Process Automation) framework based on Android’s official SDK and the WeChat/Enterprise WeChat SDK.
- Key features: runs JavaScript “scripts” (via Rhino) to drive AccessibilityService and shell commands, orchestrates UI automation (via automator), exposes a DevPlugin WebSocket for remote control, and bundles dozens of JS utility modules for scripts (axios, bluebird, paddleOCR, etc.).
- There is no separate server in this repo; the “task scheduling platform” is external.

##### High‐level modules (from file tree)
- app – the Android UI and entry point (Activities, Fragments, Compose UI, ViewModels, user settings, script editor, build tools).
- autojs – the core script engine service: Rhino‐based JS engine, script execution manager, runtime APIs (Console, UI, Shell, Events, Files, Floaty, HTTP), code generator, encryption, preprocessor.
- automator – UI automation DSL: global action automator, UiObject/UiSelector, filters/search algorithms.
- common – shared utilities and compatibility layers (I/O, concurrency, events, app‐permission helpers).
- inrt – an integration variant (“in‐real‐time” or plugin client) that embeds a DevPluginService and WebSocket client to receive remote script commands.

##### Key runtime components
- UI layer (app module): Activities/Fragments invoke and control scripts, display logs, code editor, script list.
- ScriptEngineService (autojs): bound service that runs JS scripts on an event loop, provides native→JS bridges.
- AccessibilityService (autojs core & automator): listens to UI events, performs click/swipe/drag.
- DevPlugin WebSocket (autojs.devplugin & inrt): runs a Ktor WebSocket server inside the app to accept remote commands, dispatches to the script engine.
- ForegroundService: keeps app alive, handles background script execution.
- Storage: custom file storage (TmpScriptFiles), SQLite databases for timed tasks, intent tasks.
- Networking: update checks (GitHub APIs, Apifox), NodeBB forum integration, downloading scripts/assets.
- Assets: dozens of JS modules under autojs/src/main/assets/modules plus sample scripts under app/src/main/assets/sample.

##### Relationships and interactions
- The app’s UI binds to ScriptEngineService to start/stop scripts.
- Scripts execute in Rhino (autojs) → call into runtime API classes → use automator to drive AccessibilityService or shell.
- The DevPlugin WebSocket receives JSON commands → routes through Router → invokes ScriptExecutionTask in the engine.
- The engine writes logs to the Console view; UI observes ScriptExecutionObserver.
- Database/storage layers persist timed tasks and script metadata; UI reads via ViewModels.
- Networking modules fetch updates or forum posts; results routed into UI or script environment.

##### Architectural patterns and principles
- Multi‐module Gradle separation for core engine (autojs), UI (app), automation DSL (automator), common utilities.
- Service‐oriented design: ScriptEngineService, AccessibilityService, ForegroundService decouple background work.
- MVVM in app UI: Activities/Fragments + ViewModels + LiveData/Observers.
- Event‐driven script runtime: EventEmitter, Continuation support in Rhino.
- Plugin architecture: DevPlugin WebSocket and Router for extensibility.
- Asset‐based scripting: bundles JS modules and sample scripts in APK assets.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Modules
    subgraph "Modules"
        UI["UI App (app module)"]:::module
        Automator["Automator DSL"]:::module
        Storage["Storage & Database"]:::module
        Networking["Networking Layer"]:::module
        JSAssets["JS Asset Modules"]:::module
        SampleScripts["Sample Scripts"]:::module
        CommonUtils["Common Utilities"]:::module
        IntegrationVariant["inrt Integration Variant"]:::module
    end

    %% Services
    subgraph "Services"
        ScriptEngine["ScriptEngineService"]:::service
        Accessibility["AccessibilityService"]:::service
        DevPluginServer["DevPlugin WS Server"]:::service
        DevPluginClient["DevPlugin Client"]:::service
        ForegroundService["ForegroundService"]:::service
    end

    %% External Systems
    subgraph "External"
        WeChat["WeChat/Enterprise WeChat App"]:::external
        RemoteScheduler["Remote Scheduler Platform"]:::external
        GitHubAPI["GitHub/Apifox Update API"]:::external
    end

    %% Interactions
    UI -->|"binds AIDL/Binder"| ScriptEngine
    ScriptEngine -->|"loads scripts"| JSAssets
    ScriptEngine -->|"calls API"| Automator
    Automator -->|"drives"| Accessibility
    Accessibility -->|"AccessibilityEvent/Shell"| WeChat
    RemoteScheduler -->|"WebSocket JSON"| DevPluginServer
    DevPluginClient -->|"WebSocket JSON"| DevPluginServer
    DevPluginServer -->|"routes JSON"| ScriptEngine
    ScriptEngine -->|"read/write"| Storage
    Networking -->|"HTTP REST"| GitHubAPI
    Networking -->|"results"| UI
    Networking -->|"callbacks"| ScriptEngine
    UI -->|"loads samples"| SampleScripts
    ForegroundService -->|"keeps alive"| ScriptEngine
    ForegroundService -->|"keeps alive"| Accessibility
    CommonUtils -.-> UI
    CommonUtils -.-> ScriptEngine
    CommonUtils -.-> Automator
    CommonUtils -.-> Networking
    IntegrationVariant -->|"embedded DevPlugin"| DevPluginClient

    %% Click Events
    click UI "https://github.com/xlrpa/workbot/blob/master/app/src/main/java/org/autojs/autojs/ui/main/MainActivity.kt"
    click ScriptEngine "https://github.com/xlrpa/workbot/blob/master/autojs/src/main/java/com/stardust/autojs/ScriptEngineService.java"
    click Accessibility "https://github.com/xlrpa/workbot/blob/master/autojs/src/main/java/com/stardust/autojs/core/accessibility/AccessibilityService.kt"
    click GlobalAutomator "https://github.com/xlrpa/workbot/blob/master/automator/src/main/java/com/stardust/automator/GlobalActionAutomator.kt"
    click UiObject "https://github.com/xlrpa/workbot/blob/master/automator/src/main/java/com/stardust/automator/UiObject.kt"
    click Filter "https://github.com/xlrpa/workbot/tree/master/automator/src/main/java/com/stardust/automator/filter"
    click Search "https://github.com/xlrpa/workbot/tree/master/automator/src/main/java/com/stardust/automator/search"
    click KtorWebSocket "https://github.com/xlrpa/workbot/blob/master/autojs/src/main/java/com/stardust/autojs/devplugin/KtorWebSocket.kt"
    click RouterServer "https://github.com/xlrpa/workbot/blob/master/autojs/src/main/java/com/stardust/autojs/devplugin/Router.kt"
    click DevPluginResponseHandler "https://github.com/xlrpa/workbot/blob/master/autojs/src/main/java/com/stardust/autojs/devplugin/DevPluginResponseHandler.kt"
    click DevPluginServiceClient "https://github.com/xlrpa/workbot/blob/master/inrt/src/main/java/com/stardust/auojs/inrt/pluginclient/DevPluginService.java"
    click RouterClient "https://github.com/xlrpa/workbot/blob/master/inrt/src/main/java/com/stardust/auojs/inrt/pluginclient/Router.java"
    click JsonWebSocket "https://github.com/xlrpa/workbot/blob/master/inrt/src/main/java/com/stardust/auojs/inrt/pluginclient/JsonWebSocket.java"
    click ForegroundService "https://github.com/xlrpa/workbot/blob/master/autojs/src/main/java/com/stardust/autojs/external/foreground/ForegroundService.java"
    click Database "https://github.com/xlrpa/workbot/tree/master/autojs/src/main/java/com/stardust/autojs/core/database"
    click TmpScriptFiles "https://github.com/xlrpa/workbot/blob/master/autojs/src/main/java/com/stardust/autojs/storage/file/TmpScriptFiles.java"
    click TimedTaskDB "https://github.com/xlrpa/workbot/blob/master/autojs/src/main/java/com/stardust/autojs/storage/database/TimedTaskDatabase.java"
    click IntentTaskDB "https://github.com/xlrpa/workbot/blob/master/autojs/src/main/java/com/stardust/autojs/storage/database/IntentTaskDatabase.java"
    click NetworkLayer "https://github.com/xlrpa/workbot/tree/master/autojs/src/main/java/com/stardust/autojs/network"
    click NetworkAPI "https://github.com/xlrpa/workbot/tree/master/autojs/src/main/java/com/stardust/autojs/network/api"
    click DownloadManager "https://github.com/xlrpa/workbot/blob/master/autojs/src/main/java/com/stardust/autojs/network/download/DownloadManager.java"
    click JSAssets "https://github.com/xlrpa/workbot/tree/master/autojs/src/main/assets/modules"
    click SampleScripts "https://github.com/xlrpa/workbot/tree/master/app/src/main/assets/sample"
    click CommonUtils "https://github.com/xlrpa/workbot/tree/master/common/src/main/java/com/stardust/pio"
    click IntegrationVariant "https://github.com/xlrpa/workbot/blob/master/inrt/src/main/java/com/stardust/auojs/inrt/App.kt"

    %% Styles
    classDef module fill:#ADD8E6,stroke:#000,stroke-width:1px
    classDef service fill:#90EE90,stroke:#000,stroke-width:1px
    classDef external fill:#FFA500,stroke:#000,stroke-width:1px
```
