@@ -24,6 +24,7 @@ Manages the conversation loop with Claude SDK:
2424- Handles streaming responses
2525- Parses SDK messages into structured AgentMessage objects
2626- Emits AgentMessageType events (STREAM_EVENT, ASSISTANT, RESULT)
27+ - Manages session persistence via session_id
2728
2829#### Message Bus (` message_bus.py ` )
2930Routes agent messages to appropriate UI components:
@@ -32,6 +33,19 @@ Routes agent messages to appropriate UI components:
3233- Controls thinking indicator state
3334- Manages scroll-to-bottom behavior
3435
36+ #### Actions (` actions.py ` )
37+ Centralizes all user-initiated actions and controls:
38+ - ** quit()** : Exits the application
39+ - ** query(user_input)** : Sends user query to agent loop queue
40+ - ** interrupt()** : Stops streaming mid-execution by setting interrupt flag and calling SDK interrupt
41+ - ** new()** : Starts new conversation by sending NEW_CONVERSATION control command
42+ - Manages UI state (thinking indicator, chat history clearing)
43+ - Directly accesses agent_loop internals (query_queue, client, interrupting flag)
44+
45+ Actions are triggered via:
46+ - Keybindings in app.py (ESC → action_interrupt, Ctrl+N → action_new)
47+ - Text commands in user_input.py ("exit", "clear")
48+
3549#### Config (` config.py ` )
3650Loads and validates YAML configuration:
3751- Filters disabled MCP servers
@@ -48,7 +62,7 @@ UserInput.on_input_submitted
4862 ↓
4963MessagePosted event → ChatHistory (immediate UI update)
5064 ↓
51- AgentLoop .query (added to queue )
65+ Actions .query(user_input) → AgentLoop.query_queue.put( )
5266 ↓
5367Claude SDK (streaming response)
5468 ↓
@@ -62,6 +76,20 @@ Match on AgentMessageType:
6276 - RESULT → Reset thinking indicator
6377```
6478
79+ ### Control Commands Flow
80+ ```
81+ User Action (ESC, Ctrl+N, "clear", "exit")
82+ ↓
83+ App.action_* (keybinding) OR UserInput (text command)
84+ ↓
85+ Actions.interrupt() OR Actions.new() OR Actions.quit()
86+ ↓
87+ AgentLoop internals:
88+ - interrupt: Set interrupting flag + SDK interrupt
89+ - new: Put ControlCommand.NEW_CONVERSATION on queue
90+ - quit: App.exit()
91+ ```
92+
6593## Key Types
6694
6795### Enums (` utils/enums.py ` )
@@ -78,6 +106,11 @@ Match on AgentMessageType:
78106- CONTENT_BLOCK_DELTA: SDK streaming event type
79107- TEXT_DELTA: SDK text delta type
80108
109+ ** ControlCommand** : Control commands for agent loop
110+ - NEW_CONVERSATION: Disconnect and reconnect SDK to start fresh session
111+ - EXIT: User command to quit application
112+ - CLEAR: User command to start new conversation
113+
81114** MessageType** (` components/messages.py ` ): UI message types
82115- SYSTEM, USER, AGENT, TOOL
83116
@@ -113,6 +146,43 @@ Configuration is loaded from `agent-chat-cli.config.yaml`:
113146
114147MCP server prompts are automatically appended to the system prompt.
115148
149+ ## User Commands
150+
151+ ### Text Commands
152+ - ** exit** : Quits the application
153+ - ** clear** : Starts a new conversation (clears history and reconnects)
154+
155+ ### Keybindings
156+ - ** Ctrl+C** : Quit application
157+ - ** ESC** : Interrupt streaming response
158+ - ** Ctrl+N** : Start new conversation
159+
160+ ## Session Management
161+
162+ The agent loop supports session persistence and resumption via ` session_id ` :
163+
164+ ### Initialization
165+ - ` AgentLoop.__init__ ` accepts an optional ` session_id ` parameter
166+ - If provided, the session_id is passed to Claude SDK via the ` resume ` config option
167+ - This allows resuming a previous conversation with full context
168+
169+ ### Session Capture
170+ - During SDK initialization, a SystemMessage with subtype "init" is received
171+ - The message contains a ` session_id ` in its data payload
172+ - AgentLoop extracts and stores this session_id: ` agent_loop.py:65 `
173+ - The session_id can be persisted and used to resume the session later
174+
175+ ### Resume Flow
176+ ```
177+ AgentLoop(session_id="abc123")
178+ ↓
179+ config_dict["resume"] = session_id
180+ ↓
181+ ClaudeSDKClient initialized with resume option
182+ ↓
183+ SDK reconnects to previous session with full history
184+ ```
185+
116186## Event Flow
117187
118188### User Message Flow
@@ -135,3 +205,7 @@ MCP server prompts are automatically appended to the system prompt.
135205- Message bus manages stateful streaming (tracks current_agent_message)
136206- Config loading combines multiple prompts into final system_prompt
137207- Tool names follow format: ` mcp__servername__toolname `
208+ - Actions class provides single interface for all user-initiated operations
209+ - Control commands are queued alongside user queries to ensure proper task ordering
210+ - Agent loop processes both strings (user queries) and ControlCommands from the same queue
211+ - Interrupt flag is checked on each streaming message to enable immediate stop
0 commit comments