|
| 1 | +# Architecture |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +Agent Chat CLI is a Python TUI application built with Textual that provides an interactive chat interface for Claude AI with MCP (Model Context Protocol) server support. |
| 6 | + |
| 7 | +## Core Components |
| 8 | + |
| 9 | +### App Layer (`app.py`) |
| 10 | +Main Textual application that initializes and coordinates all components. |
| 11 | + |
| 12 | +### Components Layer |
| 13 | +Textual widgets responsible for UI rendering: |
| 14 | +- **ChatHistory**: Container that displays message widgets |
| 15 | +- **Message widgets**: SystemMessage, UserMessage, AgentMessage, ToolMessage |
| 16 | +- **UserInput**: Handles user text input and submission |
| 17 | +- **ThinkingIndicator**: Shows when agent is processing |
| 18 | + |
| 19 | +### Utils Layer |
| 20 | + |
| 21 | +#### Agent Loop (`agent_loop.py`) |
| 22 | +Manages the conversation loop with Claude SDK: |
| 23 | +- Maintains async queue for user queries |
| 24 | +- Handles streaming responses |
| 25 | +- Parses SDK messages into structured AgentMessage objects |
| 26 | +- Emits AgentMessageType events (STREAM_EVENT, ASSISTANT, RESULT) |
| 27 | + |
| 28 | +#### Message Bus (`message_bus.py`) |
| 29 | +Routes agent messages to appropriate UI components: |
| 30 | +- Handles streaming text updates |
| 31 | +- Mounts tool use messages |
| 32 | +- Controls thinking indicator state |
| 33 | +- Manages scroll-to-bottom behavior |
| 34 | + |
| 35 | +#### Config (`config.py`) |
| 36 | +Loads and validates YAML configuration: |
| 37 | +- Filters disabled MCP servers |
| 38 | +- Loads prompts from files |
| 39 | +- Expands environment variables |
| 40 | +- Combines system prompt with MCP server prompts |
| 41 | + |
| 42 | +## Data Flow |
| 43 | + |
| 44 | +``` |
| 45 | +User Input |
| 46 | + ↓ |
| 47 | +UserInput.on_input_submitted |
| 48 | + ↓ |
| 49 | +MessagePosted event → ChatHistory (immediate UI update) |
| 50 | + ↓ |
| 51 | +AgentLoop.query (added to queue) |
| 52 | + ↓ |
| 53 | +Claude SDK (streaming response) |
| 54 | + ↓ |
| 55 | +AgentLoop._handle_message |
| 56 | + ↓ |
| 57 | +AgentMessage (typed message) → MessageBus.handle_agent_message |
| 58 | + ↓ |
| 59 | +Match on AgentMessageType: |
| 60 | + - STREAM_EVENT → Update streaming message widget |
| 61 | + - ASSISTANT → Mount tool use widgets |
| 62 | + - RESULT → Reset thinking indicator |
| 63 | +``` |
| 64 | + |
| 65 | +## Key Types |
| 66 | + |
| 67 | +### Enums (`utils/enums.py`) |
| 68 | + |
| 69 | +**AgentMessageType**: Agent communication events |
| 70 | +- ASSISTANT: Assistant message with content blocks |
| 71 | +- STREAM_EVENT: Streaming text chunk |
| 72 | +- RESULT: Response complete |
| 73 | +- INIT, SYSTEM: Initialization and system events |
| 74 | + |
| 75 | +**ContentType**: Content block types |
| 76 | +- TEXT: Text content |
| 77 | +- TOOL_USE: Tool call |
| 78 | +- CONTENT_BLOCK_DELTA: SDK streaming event type |
| 79 | +- TEXT_DELTA: SDK text delta type |
| 80 | + |
| 81 | +**MessageType** (`components/messages.py`): UI message types |
| 82 | +- SYSTEM, USER, AGENT, TOOL |
| 83 | + |
| 84 | +### Data Classes |
| 85 | + |
| 86 | +**AgentMessage** (`utils/agent_loop.py`): Structured message from agent loop |
| 87 | +```python |
| 88 | +@dataclass |
| 89 | +class AgentMessage: |
| 90 | + type: AgentMessageType |
| 91 | + data: Any |
| 92 | +``` |
| 93 | + |
| 94 | +**Message** (`components/messages.py`): UI message data |
| 95 | +```python |
| 96 | +@dataclass |
| 97 | +class Message: |
| 98 | + type: MessageType |
| 99 | + content: str |
| 100 | + metadata: dict[str, Any] | None = None |
| 101 | +``` |
| 102 | + |
| 103 | +## Configuration System |
| 104 | + |
| 105 | +Configuration is loaded from `agent-chat-cli.config.yaml`: |
| 106 | +- **system_prompt**: Base system prompt (supports file paths) |
| 107 | +- **model**: Claude model to use |
| 108 | +- **include_partial_messages**: Enable streaming |
| 109 | +- **mcp_servers**: MCP server configurations (filtered by enabled flag) |
| 110 | +- **agents**: Named agent configurations |
| 111 | +- **disallowed_tools**: Tool filtering |
| 112 | +- **permission_mode**: Permission handling mode |
| 113 | + |
| 114 | +MCP server prompts are automatically appended to the system prompt. |
| 115 | + |
| 116 | +## Event Flow |
| 117 | + |
| 118 | +### User Message Flow |
| 119 | +1. User submits text → UserInput |
| 120 | +2. MessagePosted event → App |
| 121 | +3. App → MessageBus.on_message_posted |
| 122 | +4. MessageBus → ChatHistory.add_message |
| 123 | +5. MessageBus → Scroll to bottom |
| 124 | + |
| 125 | +### Agent Response Flow |
| 126 | +1. AgentLoop receives SDK message |
| 127 | +2. Parse into AgentMessage with AgentMessageType |
| 128 | +3. MessageBus.handle_agent_message (match/case on type) |
| 129 | +4. Update UI components based on type |
| 130 | +5. Scroll to bottom |
| 131 | + |
| 132 | +## Notes |
| 133 | + |
| 134 | +- Two distinct MessageType enums exist for different purposes (UI vs Agent events) |
| 135 | +- Message bus manages stateful streaming (tracks current_agent_message) |
| 136 | +- Config loading combines multiple prompts into final system_prompt |
| 137 | +- Tool names follow format: `mcp__servername__toolname` |
0 commit comments