# tsingliuwin/autoclaw architecture

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

- Interactive diagram: https://gitdiagram.com/tsingliuwin/autoclaw
- Repository: https://github.com/tsingliuwin/autoclaw

## Overview

1. **What this repo is**
   AutoClaw is a TypeScript/Node.js CLI agent framework, not a web app. Its architecture is centered on a headless command loop that lets an LLM plan actions and invoke local or external tools.

2. **Top-level control flow**
   The entry point in `src/index.ts` boots the CLI, parses flags like model selection, non-interactive mode, and auto-confirmation, then hands execution to the agent layer in `src/agent.ts`.

3. **Agent orchestration**
   `src/agent.ts` is the core runtime coordinator. It likely owns the conversation state, sends prompts to the LLM via the OpenAI-compatible SDK, receives tool calls, executes them, and feeds results back into the model until the task is complete.

4. **Tool abstraction boundary**
   `src/tools/interface.ts` and `src/tools/index.ts` define and register the tool contract. This is the main extensibility boundary: the agent doesn’t hardcode capabilities; it routes actions through a tool interface and a registry.

5. **Local system-capability tools**
   `src/tools/core.ts` is the likely foundation for low-level filesystem and shell-style operations. These are the “command-driven execution” primitives that make the framework deterministic in headless environments.

6. **External-service integrations**
   Several tools bridge the agent to outside systems:
   - `src/tools/search.ts` for Tavily web search
   - `src/tools/email.ts` for SMTP email sending
   - `src/tools/notify.ts` for webhook notifications such as Feishu, DingTalk, or WeCom  
   These are separate from core execution so the agent can mix local automation with outbound communication.

7. **Context and media helpers**
   `src/tools/image.ts` and `src/tools/screenshot.ts` provide image-related utilities, likely for capturing or processing visual artifacts even though the framework itself is headless. They are support tools rather than the primary interaction model.

8. **Prompt and context shaping**
   `src/tools/prompt-optimizer.ts` suggests a preprocessing or prompt-refinement step before the LLM is called. This acts as an intelligence layer to improve instruction quality and tool selection rather than as an execution tool itself.

9. **Runtime modes and user interaction**
   The README describes both interactive chat and one-shot headless execution. The architecture therefore has two outer modes around the same agent core: a REPL-like interactive loop and a non-interactive batch path suitable for CI/CD or containerized runs.

10. **Configuration flow**
   Configuration is hierarchical: CLI arguments override environment variables, which override project-local `.autoclaw/setting.json`, which overrides global config in `~/.autoclaw/setting.json`. This matters architecturally because the agent is designed to run in diverse environments without code changes.

11. **Security and execution gating**
   Auto-confirmation (`-y`) is an explicit boundary around side-effecting tool execution. In normal mode, the framework is meant to pause before running actions, which is important for diagramming trust boundaries between the LLM, the user, and the host system.

12. **Packaging/deployment shape**
   The repo is built as an npm-distributed TypeScript package with shell installers and no server component. That means the deployed unit is a single CLI binary/process, typically run locally, in containers, or in automation pipelines rather than as a multi-service backend.

Main entry points: [`src/tools/index.ts`](https://github.com/tsingliuwin/autoclaw/blob/main/src/tools/index.ts) (Tool registry), [`src/agent.ts`](https://github.com/tsingliuwin/autoclaw/blob/main/src/agent.ts) (Agent core), [`src/index.ts`](https://github.com/tsingliuwin/autoclaw/blob/main/src/index.ts) (CLI entry), [`src/tools/core.ts`](https://github.com/tsingliuwin/autoclaw/blob/main/src/tools/core.ts) (Core tools)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_runtime["Runtime"]
  node_src_index["CLI entry<br/>entrypoint<br/>[index.ts]"]
  node_config_flow["Config flow<br/>settings"]
  node_interactive_mode(("Interactive mode<br/>repl"))
  node_headless_mode(("Headless mode<br/>batch"))
  node_autoconfirm_gate{{"Confirm gate<br/>safety"}}
  node_agent_runtime["Agent core<br/>orchestrator<br/>[agent.ts]"]
  node_llm_api[("LLM API<br/>model backend")]
end

subgraph group_tools["Tools"]
  node_tool_registry["Tool registry<br/>dispatcher<br/>[index.ts]"]
  node_tool_contract["Tool contract<br/>interface<br/>[interface.ts]"]
  node_core_tools["Core tools<br/>local exec<br/>[core.ts]"]
  node_browser_tool["Browser tool<br/>web automation<br/>[browser.ts]"]
  node_search_tool["Search tool<br/>external search<br/>[search.ts]"]
  node_email_tool["Email tool<br/>smtp<br/>[email.ts]"]
  node_notify_tool["Notify tool<br/>webhook<br/>[notify.ts]"]
  node_image_tools["Image utils<br/>media helper<br/>[image.ts]"]
  node_screenshot_tool["Screenshot<br/>capture<br/>[screenshot.ts]"]
  node_prompt_optimizer["Prompt optimizer<br/>preprocessor"]
end

node_host_system[("Host system<br/>filesystem+shell")]
node_external_services[("External services<br/>third-party apis")]

node_src_index -->|"loads settings"| node_config_flow
node_src_index -->|"selects"| node_interactive_mode
node_src_index -->|"selects"| node_headless_mode
node_interactive_mode -->|"drives"| node_agent_runtime
node_headless_mode -->|"invokes"| node_agent_runtime
node_config_flow -->|"configures"| node_agent_runtime
node_agent_runtime -->|"calls"| node_llm_api
node_agent_runtime -->|"checks"| node_autoconfirm_gate
node_autoconfirm_gate -->|"permits"| node_tool_registry
node_agent_runtime -->|"dispatches"| node_tool_registry
node_tool_registry -->|"implements"| node_tool_contract
node_tool_registry -->|"exposes"| node_core_tools
node_tool_registry -->|"exposes"| node_browser_tool
node_tool_registry -->|"exposes"| node_search_tool
node_tool_registry -->|"exposes"| node_email_tool
node_tool_registry -->|"exposes"| node_notify_tool
node_tool_registry -->|"exposes"| node_image_tools
node_tool_registry -->|"exposes"| node_screenshot_tool
node_tool_registry -->|"exposes"| node_prompt_optimizer
node_core_tools -->|"executes on"| node_host_system
node_search_tool -->|"queries"| node_external_services
node_email_tool -->|"sends via"| node_external_services
node_notify_tool -->|"posts to"| node_external_services
node_browser_tool -->|"browses"| node_external_services
node_screenshot_tool -->|"captures"| node_host_system
node_image_tools -->|"processes"| node_host_system
node_prompt_optimizer -->|"prepares input"| node_llm_api

click node_src_index "https://github.com/tsingliuwin/autoclaw/blob/main/src/index.ts"
click node_agent_runtime "https://github.com/tsingliuwin/autoclaw/blob/main/src/agent.ts"
click node_tool_registry "https://github.com/tsingliuwin/autoclaw/blob/main/src/tools/index.ts"
click node_tool_contract "https://github.com/tsingliuwin/autoclaw/blob/main/src/tools/interface.ts"
click node_core_tools "https://github.com/tsingliuwin/autoclaw/blob/main/src/tools/core.ts"
click node_browser_tool "https://github.com/tsingliuwin/autoclaw/blob/main/src/tools/browser.ts"
click node_search_tool "https://github.com/tsingliuwin/autoclaw/blob/main/src/tools/search.ts"
click node_email_tool "https://github.com/tsingliuwin/autoclaw/blob/main/src/tools/email.ts"
click node_notify_tool "https://github.com/tsingliuwin/autoclaw/blob/main/src/tools/notify.ts"
click node_image_tools "https://github.com/tsingliuwin/autoclaw/blob/main/src/tools/image.ts"
click node_screenshot_tool "https://github.com/tsingliuwin/autoclaw/blob/main/src/tools/screenshot.ts"
click node_prompt_optimizer "https://github.com/tsingliuwin/autoclaw/blob/main/src/tools/prompt-optimizer.ts"

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_src_index,node_config_flow,node_interactive_mode,node_headless_mode,node_autoconfirm_gate,node_agent_runtime,node_llm_api toneBlue
class node_tool_registry,node_tool_contract,node_core_tools,node_browser_tool,node_search_tool,node_email_tool,node_notify_tool,node_image_tools,node_screenshot_tool,node_prompt_optimizer toneAmber
class node_host_system,node_external_services toneNeutral
```

## Components

### Runtime

CLI entrypoint, modes, and orchestration

- [CLI entry](https://github.com/tsingliuwin/autoclaw/blob/main/src/index.ts): entrypoint · Boots the command-line app and hands control to the agent runtime · `src/index.ts`
- **Config flow**: settings · Resolves CLI flags, environment variables, and persisted settings
- **Interactive mode**: repl · Chat-like loop for ongoing user interaction
- **Headless mode**: batch · One-shot non-interactive execution path for automation and CI
- **Confirm gate**: safety · Boundary for approving side-effecting tool execution
- [Agent core](https://github.com/tsingliuwin/autoclaw/blob/main/src/agent.ts): orchestrator · Coordinates conversation state, model calls, tool use, and result feedback · `src/agent.ts`
- **LLM API**: model backend · OpenAI-compatible model interface used by the agent

### Tools

Tool contract, registry, and integrations

- [Tool registry](https://github.com/tsingliuwin/autoclaw/blob/main/src/tools/index.ts): dispatcher · Registers available tools and routes tool calls · `src/tools/index.ts`
- [Tool contract](https://github.com/tsingliuwin/autoclaw/blob/main/src/tools/interface.ts): interface · Defines the common tool shape and execution boundary · `src/tools/interface.ts`
- [Core tools](https://github.com/tsingliuwin/autoclaw/blob/main/src/tools/core.ts): local exec · Filesystem and shell-style primitives for local automation · `src/tools/core.ts`
- [Browser tool](https://github.com/tsingliuwin/autoclaw/blob/main/src/tools/browser.ts): web automation · Browser-oriented automation capability · `src/tools/browser.ts`
- [Search tool](https://github.com/tsingliuwin/autoclaw/blob/main/src/tools/search.ts): external search · Tavily-backed web search integration · `src/tools/search.ts`
- [Email tool](https://github.com/tsingliuwin/autoclaw/blob/main/src/tools/email.ts): smtp · Sends email through SMTP · `src/tools/email.ts`
- [Notify tool](https://github.com/tsingliuwin/autoclaw/blob/main/src/tools/notify.ts): webhook · Sends outbound notifications to chat and alert services · `src/tools/notify.ts`
- [Image utils](https://github.com/tsingliuwin/autoclaw/blob/main/src/tools/image.ts): media helper · Image-related utilities for processing visual artifacts · `src/tools/image.ts`
- [Screenshot](https://github.com/tsingliuwin/autoclaw/blob/main/src/tools/screenshot.ts): capture · Captures visual state for the agent to inspect · `src/tools/screenshot.ts`
- [Prompt optimizer](https://github.com/tsingliuwin/autoclaw/blob/main/src/tools/prompt-optimizer.ts): preprocessor · Refines prompts before they are sent to the model · `src/tools/prompt-optimizer.ts`

### Other components

- **Host system**: filesystem+shell · Local machine environment where tools execute
- **External services**: third-party apis · Web search, SMTP, and notification endpoints used by tools

## Connections

- CLI entry → Config flow: loads settings · Reads flags and runtime configuration
- CLI entry → Interactive mode: selects · Starts the REPL-style path when interactive
- CLI entry → Headless mode: selects · Starts the non-interactive batch path
- Interactive mode → Agent core: drives · Feeds user turns into the agent loop
- Headless mode → Agent core: invokes · Runs the agent for a single task or scriptable job
- Config flow → Agent core: configures · Supplies model, confirmation, and execution settings
- Agent core → LLM API: calls · Sends prompts and receives model/tool decisions
- Agent core → Confirm gate: checks · Pauses or auto-approves side-effecting actions
- Confirm gate → Tool registry: permits · Allows approved tool execution to proceed
- Agent core → Tool registry: dispatches · Routes tool calls to the matching implementation
- Tool registry → Tool contract: implements · Uses the shared tool interface to normalize tools
- Tool registry → Core tools: exposes · Registers local command and filesystem primitives
- Tool registry → Browser tool: exposes · Registers browser automation capability
- Tool registry → Search tool: exposes · Registers web search capability
- Tool registry → Email tool: exposes · Registers outbound email capability
- Tool registry → Notify tool: exposes · Registers notification integrations
- Tool registry → Image utils: exposes · Registers image processing helpers
- Tool registry → Screenshot: exposes · Registers screenshot capture capability
- Tool registry → Prompt optimizer: exposes · Registers prompt-shaping support
- Core tools → Host system: executes on · Reads and writes the local machine and shells out locally
- Search tool → External services: queries · Uses third-party search endpoints
- Email tool → External services: sends via · Delivers mail through SMTP infrastructure
- Notify tool → External services: posts to · Delivers webhook notifications to external services
- Browser tool → External services: browses · Interacts with remote web resources through browser automation
- Screenshot → Host system: captures · Uses the local runtime to capture screen state
- Image utils → Host system: processes · Handles local media artifacts
- Prompt optimizer → LLM API: prepares input · Improves prompt quality before model invocation
