# lsdefine/genericagent architecture

> Architecture diagram of the GitHub repository lsdefine/genericagent, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-04-17). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/lsdefine/genericagent
- Repository: https://github.com/lsdefine/genericagent

## Overview

1. **Overall shape**
   GenericAgent is a local, Python-based agent runtime. The architecture centers on one autonomous loop that talks to an LLM, chooses from a small toolset, executes actions on the local machine or browser, and then writes results back into layered memory for reuse.

2. **Runtime entry points**
   The main launch surfaces are `launch.pyw`, `agentmain.py`, `hub.pyw`, and `agent_loop.py`. Those entry points wire together model access, UI/frontends, tool execution, and memory persistence. The `.pyw` files suggest the project is meant to run as a desktop app on systems where a GUI is available.

3. **Core reasoning loop**
   `agent_loop.py` is the central orchestration layer described in the README as a ~100-line loop. It performs the repeated cycle: observe state, ask the LLM what to do next, call a tool, inspect the result, and continue until the task is complete. `llmcore.py` is the model-facing boundary for Claude/Gemini/Kimi/MiniMax-style backends.

4. **Tool layer**
   The agent’s capabilities are intentionally narrow and atomic: code execution, file read/write/patch, web scan, browser JavaScript execution, user confirmation, plus memory checkpoint/update operations. The tool definitions live in `assets/tools_schema*.json` and the usable-tool history in `assets/tool_usable_history.json`, which means the loop can stay generic while the available actions are externally described.

5. **Browser control subsystem**
   `TMWebDriver.py` and `assets/tmwd_cdp_bridge/` form a browser-injection boundary. Instead of using a headless sandbox, the agent controls a real browser session and preserves login state. The Chrome extension bridge (`manifest.json`, `background.js`, `content.js`, etc.) is the glue between the Python agent and browser-side events/CDP-style control.

6. **Memory and skill architecture**
   The `memory/` tree is the main long-term knowledge subsystem. The README’s L0–L4 model maps to behavior rules, insight index, global facts, reusable SOPs/skills, and archived sessions. This repo stores those as markdown templates, SOP docs, and archived session artifacts, with `memory/skill_search/` providing skill indexing/lookup and `memory/L4_raw_sessions/` handling session compression.

7. **Self-evolution boundary**
   The key architectural loop is not just “act” but “distill.” New task solutions are expected to be crystallized into reusable skills, written into the memory layers, and then recalled later via skill search. That is the main mechanism by which the agent becomes more capable without growing the core codebase.

8. **Reflection and scheduling**
   `reflect/autonomous.py` and `reflect/scheduler.py` indicate a second control plane for background or periodic work. Combined with the cron-oriented memory SOPs and the README’s scheduler note, the agent can move from one-off task execution into scheduled autonomous routines.

9. **Frontends and message ingress**
   The `frontends/` directory is a set of adapters that normalize different user entry channels into the same agent core. There is a Streamlit UI, a Qt desktop UI, and bot frontends for Telegram, WeChat, WeCom, DingTalk, QQ, and Feishu. `frontends/chatapp_common.py` appears to hold shared chat orchestration so those channels all feed the same agent pipeline.

10. **Vision, OCR, and device automation**
   The `memory/` utilities such as `ocr_utils.py`, `ui_detect.py`, `adb_ui.py`, and related controller/scanner helpers show that the agent can operate beyond the browser, including mobile/Android workflows and screen-based interaction. This is the perception layer that supports tasks requiring screenshots, OCR, UI element detection, or ADB-driven phone control.

11. **Dynamic code execution and extensibility**
   `code_run` is a structural escape hatch: the agent can install packages, write scripts, call APIs, or create temporary automation code at runtime. That means the repo is not just a fixed-agent system; it is a bootstrap environment for generating new capabilities and then folding them back into persistent skills.

12. **Configuration and trust boundaries**
   Secrets and provider configuration are intentionally separated into `mykey_template.py` / `mykey.py`. The repo also includes setup docs and platform-specific install helpers in `assets/`, showing that external dependencies are mostly runtime integrations rather than built-in service infrastructure.

13. **Tests and model integration**
   `tests/test_minimax.py` and `tests/test_minimax_integration.py` show that at least some of the external-model behavior is validated with automated tests. That reinforces that the critical boundary is the LLM provider interface, not a web backend or database service.

14. **How to draw the diagram**
   A good architecture diagram would place the LLM-driven `agent_loop` in the center, with arrows out to: frontends/bot adapters, browser control, file/code tools, vision/ADB tools, and memory/skill storage. Then show a feedback arrow from execution results back into the layered memory and skill search system, because that persistence loop is the project’s defining architectural feature.

Main entry points: [`agent_loop.py`](https://github.com/lsdefine/genericagent/blob/main/agent_loop.py) (Agent loop), [`frontends/chatapp_common.py`](https://github.com/lsdefine/genericagent/blob/main/frontends/chatapp_common.py) (Chat common), [`memory`](https://github.com/lsdefine/genericagent/tree/main/memory) (Memory tree), [`memory/skill_search/skill_search/engine.py`](https://github.com/lsdefine/genericagent/blob/main/memory/skill_search/skill_search/engine.py) (Skill search)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_core["Core runtime"]
  node_launch["Launch<br/>entrypoint<br/>[launch.pyw]"]
  node_agentmain["Agent main<br/>entrypoint<br/>[agentmain.py]"]
  node_hub["Hub<br/>entrypoint<br/>[hub.pyw]"]
  node_loop(("Agent loop<br/>orchestrator<br/>[agent_loop.py]"))
  node_llm["LLM core<br/>model gateway<br/>[llmcore.py]"]
  node_ga["GA<br/>runtime support<br/>[ga.py]"]
end

subgraph group_interfaces["Frontends"]
  node_stapp["Streamlit UI<br/>frontend<br/>[stapp.py]"]
  node_qtapp["Qt UI<br/>frontend<br/>[qtapp.py]"]
  node_chatcommon["Chat common<br/>adapter core<br/>[chatapp_common.py]"]
  node_bots["Bot adapters<br/>frontends<br/>[tgapp.py]"]
end

subgraph group_memory["Memory"]
  node_memorycore[("Memory tree<br/>knowledge base")]
  node_skillsearch["Skill search<br/>retrieval index<br/>[engine.py]"]
  node_sessions["Raw sessions<br/>session archive"]
end

subgraph group_browser["Browser control"]
  node_browserdrv["WebDriver<br/>browser controller<br/>[TMWebDriver.py]"]
  node_bridge["CDP bridge<br/>browser extension<br/>[manifest.json]"]
end

subgraph group_ops["Automation"]
  node_reflect(("Reflection<br/>autonomy controller<br/>[autonomous.py]"))
  node_scheduler["Scheduler<br/>schedule engine<br/>[scheduler.py]"]
end

subgraph group_assets["Assets"]
  node_toolschema["Tool schema<br/>tool contract<br/>[tools_schema.json]"]
  node_toolhist["Tool history<br/>tool metadata"]
end

node_mykey["Secrets<br/>config boundary<br/>[mykey_template.py]"]

node_launch -->|"starts"| node_loop
node_agentmain -->|"boots"| node_loop
node_hub -->|"routes"| node_loop
node_loop -->|"queries"| node_llm
node_loop -->|"selects"| node_toolschema
node_loop -->|"drives"| node_browserdrv
node_browserdrv -->|"injects"| node_bridge
node_loop -->|"writes"| node_memorycore
node_loop -->|"recalls"| node_skillsearch
node_memorycore -->|"archives"| node_sessions
node_memorycore -->|"indexes"| node_skillsearch
node_skillsearch -->|"feeds"| node_loop
node_reflect -->|"coordinates"| node_scheduler
node_scheduler -->|"triggers"| node_loop
node_stapp -->|"uses"| node_chatcommon
node_qtapp -->|"uses"| node_chatcommon
node_bots -->|"uses"| node_chatcommon
node_chatcommon -->|"hands off"| node_loop
node_toolschema -->|"constrains"| node_loop
node_toolhist -->|"guides"| node_loop
node_mykey -->|"configures"| node_llm

click node_launch "https://github.com/lsdefine/genericagent/blob/main/launch.pyw"
click node_agentmain "https://github.com/lsdefine/genericagent/blob/main/agentmain.py"
click node_hub "https://github.com/lsdefine/genericagent/blob/main/hub.pyw"
click node_loop "https://github.com/lsdefine/genericagent/blob/main/agent_loop.py"
click node_llm "https://github.com/lsdefine/genericagent/blob/main/llmcore.py"
click node_ga "https://github.com/lsdefine/genericagent/blob/main/ga.py"
click node_stapp "https://github.com/lsdefine/genericagent/blob/main/frontends/stapp.py"
click node_qtapp "https://github.com/lsdefine/genericagent/blob/main/frontends/qtapp.py"
click node_chatcommon "https://github.com/lsdefine/genericagent/blob/main/frontends/chatapp_common.py"
click node_bots "https://github.com/lsdefine/genericagent/blob/main/frontends/tgapp.py"
click node_browserdrv "https://github.com/lsdefine/genericagent/blob/main/TMWebDriver.py"
click node_bridge "https://github.com/lsdefine/genericagent/blob/main/assets/tmwd_cdp_bridge/manifest.json"
click node_toolschema "https://github.com/lsdefine/genericagent/blob/main/assets/tools_schema.json"
click node_toolhist "https://github.com/lsdefine/genericagent/blob/main/assets/tool_usable_history.json"
click node_memorycore "https://github.com/lsdefine/genericagent/tree/main/memory"
click node_skillsearch "https://github.com/lsdefine/genericagent/blob/main/memory/skill_search/skill_search/engine.py"
click node_sessions "https://github.com/lsdefine/genericagent/blob/main/memory/L4_raw_sessions/compress_session.py"
click node_reflect "https://github.com/lsdefine/genericagent/blob/main/reflect/autonomous.py"
click node_scheduler "https://github.com/lsdefine/genericagent/blob/main/reflect/scheduler.py"
click node_mykey "https://github.com/lsdefine/genericagent/blob/main/mykey_template.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_launch,node_agentmain,node_hub,node_loop,node_llm,node_ga toneBlue
class node_stapp,node_qtapp,node_chatcommon,node_bots toneAmber
class node_memorycore,node_skillsearch,node_sessions toneMint
class node_browserdrv,node_bridge toneRose
class node_reflect,node_scheduler toneIndigo
class node_toolschema,node_toolhist toneTeal
class node_mykey toneNeutral
```

## Components

### Core runtime

Agent loop, model boundary, and launch surfaces.

- [Launch](https://github.com/lsdefine/genericagent/blob/main/launch.pyw): entrypoint · Desktop launch surface that starts the agent runtime. · `launch.pyw`
- [Agent main](https://github.com/lsdefine/genericagent/blob/main/agentmain.py): entrypoint · Main application bootstrap and wiring. · `agentmain.py`
- [Hub](https://github.com/lsdefine/genericagent/blob/main/hub.pyw): entrypoint · Alternate entry surface for coordinating the agent. · `hub.pyw`
- [Agent loop](https://github.com/lsdefine/genericagent/blob/main/agent_loop.py): orchestrator · Central observe-decide-act loop that selects tools and advances tasks. · `agent_loop.py`
- [LLM core](https://github.com/lsdefine/genericagent/blob/main/llmcore.py): model gateway · Provider boundary for Claude, Gemini, Kimi, MiniMax, and similar backends. · `llmcore.py`
- [GA](https://github.com/lsdefine/genericagent/blob/main/ga.py): runtime support · Supporting runtime module used by the agent system. · `ga.py`

### Frontends

User and bot ingress adapters into the same agent core.

- [Streamlit UI](https://github.com/lsdefine/genericagent/blob/main/frontends/stapp.py): frontend · Web-style user interface adapter. · `frontends/stapp.py`
- [Qt UI](https://github.com/lsdefine/genericagent/blob/main/frontends/qtapp.py): frontend · Desktop GUI adapter. · `frontends/qtapp.py`
- [Chat common](https://github.com/lsdefine/genericagent/blob/main/frontends/chatapp_common.py): adapter core · Shared chat orchestration used by bot frontends. · `frontends/chatapp_common.py`
- [Bot adapters](https://github.com/lsdefine/genericagent/blob/main/frontends/tgapp.py): frontends · Telegram, WeChat, WeCom, DingTalk, and QQ ingress adapters. · `frontends/tgapp.py`

### Memory

Long-term knowledge, skills, and operational SOPs.

- [Memory tree](https://github.com/lsdefine/genericagent/tree/main/memory): knowledge base · Layered long-term memory and reusable skill archive. · `memory`
- [Skill search](https://github.com/lsdefine/genericagent/blob/main/memory/skill_search/skill_search/engine.py): retrieval index · Skill indexing and lookup subsystem. · `memory/skill_search/skill_search/engine.py`
- [Raw sessions](https://github.com/lsdefine/genericagent/blob/main/memory/L4_raw_sessions/compress_session.py): session archive · Compressed and archived interaction sessions for later distillation. · `memory/L4_raw_sessions/compress_session.py`

### Browser control

Real-browser automation and extension bridge.

- [WebDriver](https://github.com/lsdefine/genericagent/blob/main/TMWebDriver.py): browser controller · Python-side browser control boundary. · `TMWebDriver.py`
- [CDP bridge](https://github.com/lsdefine/genericagent/blob/main/assets/tmwd_cdp_bridge/manifest.json): browser extension · Chrome extension bridge for injecting control and relaying browser events. · `assets/tmwd_cdp_bridge/manifest.json`

### Automation

Background scheduling and reflection flows.

- [Reflection](https://github.com/lsdefine/genericagent/blob/main/reflect/autonomous.py): autonomy controller · Background autonomous routines and scheduling. · `reflect/autonomous.py`
- [Scheduler](https://github.com/lsdefine/genericagent/blob/main/reflect/scheduler.py): schedule engine · Periodic task scheduling and execution. · `reflect/scheduler.py`

### Assets

External tool schemas, prompts, and support resources.

- [Tool schema](https://github.com/lsdefine/genericagent/blob/main/assets/tools_schema.json): tool contract · Externally described tool set for execution, file ops, web, and confirmation. · `assets/tools_schema.json`
- [Tool history](https://github.com/lsdefine/genericagent/blob/main/assets/tool_usable_history.json): tool metadata · History of usable tools used to keep the loop generic. · `assets/tool_usable_history.json`

### Other components

- [Secrets](https://github.com/lsdefine/genericagent/blob/main/mykey_template.py): config boundary · Local provider credentials and environment-specific configuration. · `mykey_template.py`

## Connections

- Launch → Agent loop: starts · Launches the main agent loop.
- Agent main → Agent loop: boots · Initializes the runtime and enters the loop.
- Hub → Agent loop: routes · Hands off control to the shared agent core.
- Agent loop → LLM core: queries · Requests the next action or response from the model.
- Agent loop → Tool schema: selects · Uses the external tool contract to choose actions.
- Agent loop → WebDriver: drives · Controls local browser sessions when needed.
- WebDriver → CDP bridge: injects · Uses the browser extension bridge for CDP-style coordination.
- Agent loop → Memory tree: writes · Stores outcomes and distilled knowledge into layered memory.
- Agent loop → Skill search: recalls · Retrieves reusable skills and prior solutions.
- Memory tree → Raw sessions: archives · Compresses and stores raw session artifacts.
- Memory tree → Skill search: indexes · Promotes reusable knowledge into skill search.
- Skill search → Agent loop: feeds · Returns relevant skills to guide the next step.
- Reflection → Scheduler: coordinates · Background autonomous work is scheduled and executed.
- Scheduler → Agent loop: triggers · Can invoke the agent loop for periodic jobs.
- Streamlit UI → Chat common: uses · Shares common chat orchestration.
- Qt UI → Chat common: uses · Shares common chat orchestration.
- Bot adapters → Chat common: uses · Bot frontends normalize into the shared chat path.
- Chat common → Agent loop: hands off · Forwards user intents into the core agent loop.
- Tool schema → Agent loop: constrains · Defines the actions available to the agent.
- Tool history → Agent loop: guides · Narrows tool selection based on prior usability.
- Secrets → LLM core: configures · Provides local secrets and provider credentials.
