@@ -14,18 +14,18 @@ Sofos is a terminal-based AI coding assistant powered by Anthropic's Claude API.
1414
1515### Key Design Decisions
1616
17- 1. **Dual Session Storage Format** (src/history.rs)
17+ 1. **Dual Session Storage Format** (src/session/ history.rs)
1818 - `api_messages`: Anthropic API format for continuing conversations
1919 - `display_messages`: UI-friendly format for showing conversation history
2020 - This separation ensures Claude sees proper API format while users see original UI
2121
22- 2. **Tool Calling Pattern** (src/repl.rs)
22+ 2. **Tool Calling Pattern** (src/repl/mod .rs)
2323 - Assistant returns content blocks (text + tool_use)
2424 - REPL executes tools and collects results
2525 - Results sent back as user message with tool_result blocks
2626 - **Loop-based handling** allows Claude to use multiple tools in sequence iteratively
2727
28- 3. **Two-Level Instructions** (src/history.rs)
28+ 3. **Two-Level Instructions** (src/session/ history.rs)
2929 - `.sofosrc`: Project-level, version controlled
3030 - `.sofos/instructions.md`: Personal, gitignored
3131 - Both appended to system prompt at startup
@@ -43,27 +43,47 @@ Sofos is a terminal-based AI coding assistant powered by Anthropic's Claude API.
4343
4444```
4545src/
46- ├── api/ # Anthropic API integration
47- │ ├── client.rs # HTTP client for Claude API
48- │ ├── morph.rs # Morph Apply API client (optional)
49- │ ├── types.rs # Message types and serialization
50- │ └── mod.rs
51- ├── tools/ # Tool implementations
52- │ ├── filesystem.rs # File operations (read, write, list, etc)
53- │ ├── bashexec.rs # Sandboxed bash execution
54- │ ├── codesearch.rs # Ripgrep integration
55- │ ├── permissions.rs # 3-tier command permission system
56- │ ├── types.rs # Tool definitions for API
57- │ └── mod.rs
58- ├── conversation.rs # Message history management
59- ├── diff.rs # Contextual diff generation and display
60- ├── history.rs # Session persistence + custom instructions
61- ├── repl.rs # Main REPL loop and display logic
62- ├── syntax.rs # Markdown/code syntax highlighting
63- ├── cli.rs # Command-line argument parsing
64- ├── error.rs # Error types
65- ├── session_selector.rs # TUI for session selection
66- └── main.rs # Entry point
46+ ├── main.rs # Entry point
47+ ├── cli.rs # CLI argument parsing
48+ ├── error.rs # Error types
49+ ├── error_ext.rs # Error extensions
50+ ├── config.rs # Configuration (SofosConfig, ModelConfig)
51+ │
52+ ├── api/ # API clients
53+ │ ├── anthropic.rs # Claude API client
54+ │ ├── openai.rs # OpenAI API client
55+ │ ├── morph.rs # Morph Apply API client
56+ │ ├── types.rs # Message types and serialization
57+ │ └── utils.rs # API utilities
58+ │
59+ ├── repl/ # REPL components
60+ │ ├── mod.rs # Main REPL loop and Repl struct
61+ │ ├── conversation.rs # Message history management
62+ │ ├── prompt.rs # Prompt rendering
63+ │ ├── request_builder.rs # API request construction
64+ │ └── response_handler.rs # Response processing
65+ │
66+ ├── session/ # Session management
67+ │ ├── history.rs # Session persistence + custom instructions
68+ │ ├── state.rs # Runtime session state
69+ │ └── selector.rs # Session selection TUI
70+ │
71+ ├── tools/ # Tool implementations
72+ │ ├── filesystem.rs # File operations (read, write, list, etc)
73+ │ ├── bashexec.rs # Sandboxed bash execution
74+ │ ├── codesearch.rs # Ripgrep integration
75+ │ ├── image.rs # Image handling
76+ │ ├── permissions.rs # 3-tier command permission system
77+ │ ├── types.rs # Tool definitions for API
78+ │ └── utils.rs # Tool utilities
79+ │
80+ ├── ui/ # UI components
81+ │ ├── mod.rs # Main UI utilities and display logic
82+ │ ├── syntax.rs # Markdown/code syntax highlighting
83+ │ └── diff.rs # Contextual diff generation and display
84+ │
85+ └── commands/ # Built-in commands
86+ └── builtin.rs # Command implementations
6787```
6888
6989### Key Files
7393- Handles serialization/deserialization for Anthropic API
7494- Supports both regular and server-side tools (like web_search)
7595
76- **src/diff.rs**
96+ **src/ui/ diff.rs**
7797- Generate contextual diffs showing only changed code blocks
7898- Uses `similar` crate for accurate line-by-line diffing
7999- Formats output with colored backgrounds (red for deletions, blue for additions)
80100- Context lines (default: 2) show unchanged code around changes
81101- Used by morph_edit_file tool to display what changed
82102
83- **src/history.rs**
103+ **src/session/ history.rs**
84104- SessionMetadata: Preview and timestamps for session list
85105- Session: Dual storage (api_messages + display_messages)
86106- DisplayMessage: Enum for user messages, assistant responses, and tool executions
87107
88- **src/repl.rs**
108+ **src/repl/mod .rs**
89109- Main event loop (run method)
90- - handle_response: Recursively processes assistant responses and tool calls
91- - display_session: Reconstructs conversation UI when resuming
92- - Max recursion depth: 50 (prevents infinite loops)
110+ - Manages REPL state and user interaction
111+ - Coordinates conversation, tools, and UI
93112
94- **src/conversation.rs**
113+ **src/repl/response_handler.rs**
114+ - Iteratively processes assistant responses and tool calls
115+ - Max iterations: 200 (prevents infinite loops)
116+
117+ **src/repl/conversation.rs**
95118- Manages in-memory message history
96119- Trims to MAX_MESSAGES (500) to prevent token overflow
97120- Builds system prompt with features list and custom instructions
222245
223246**Problem:** Claude can make infinite tool calls if it gets stuck in a loop
224247
225- **Solution:** (repl.rs:handle_response )
248+ **Solution:** (src/ repl/response_handler .rs)
226249- Use an **iterative loop** instead of recursion for constant stack space
227250- Track iteration count in the loop
228251- MAX_TOOL_ITERATIONS = 200
0 commit comments