Completed HIGH-001 through HIGH-004: HIGH-001: Discord bot with channel message routing - bot.py: 244 lines with ClaudeCoordinator class - @mention trigger mode for safe operation - Session lifecycle integration with SessionManager - Typing indicators and error handling - 20/20 tests passing HIGH-002: Response formatter with intelligent chunking - response_formatter.py: expanded to 329 lines - format_response() with smart boundary detection - Code block preservation and splitting - 26/26 tests passing HIGH-003: Slash commands for bot management - commands.py: 411 lines with ClaudeCommands cog - /reset with interactive confirmation dialog - /status with Discord embed display - /model for runtime model switching - 18/18 tests passing HIGH-004: Concurrent message handling - Per-channel asyncio.Lock implementation - Same-channel serialization (prevents race conditions) - Cross-channel parallelization (maintains performance) - 7/7 concurrency tests passing Total: 134/135 tests passing (99.3%) Production-ready Discord bot MVP Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
7.2 KiB
HIGH-002: Discord Response Formatter Implementation
Status: COMPLETED ✅
Implemented: 2026-02-13 Location: LXC 301 (discord-bot@10.10.0.230) Project: /opt/projects/claude-coordinator
Summary
Successfully implemented the format_response() method in ResponseFormatter class with intelligent chunking, code block preservation, and comprehensive edge case handling.
Implementation Details
Core Method: format_response()
Signature:
def format_response(
self,
text: str,
max_length: int = 2000,
split_on_code_blocks: bool = True
) -> List[str]
Features:
-
Intelligent Chunking - Splits on natural boundaries:
- Paragraph breaks (double newlines) - priority 1
- Single newlines - priority 2
- Sentence endings (. ! ?) - priority 3
- Word boundaries (spaces) - priority 4
- Character splits (last resort) - priority 5
-
Code Block Preservation:
- Detects code blocks using regex:
```language\ncontent\n``` - Never splits inside code blocks
- Large code blocks split with proper markers
- Preserves language identifiers when splitting
- Handles multiple consecutive code blocks
- Detects code blocks using regex:
-
Edge Case Handling:
- Empty/whitespace-only input → returns empty list
- Single line longer than max_length → force splits
- Code block exactly at max_length → handled gracefully
- Mixed markdown (bold, italic, lists) → preserved
- Custom max_length parameter → respected
Helper Methods
_split_preserving_code_blocks()
- Main logic for code block-aware splitting
- Finds all code blocks using regex
- Processes text between code blocks separately
- Delegates to
_split_large_code_block()for oversized blocks
_split_large_code_block()
- Splits code blocks > max_length
- Maintains proper ``` markers with language
- Splits on line boundaries when possible
- Handles extremely long single lines
_split_smart()
- Intelligent splitting on natural boundaries
- Used for non-code text segments
- Delegates to
_find_best_split_point()for boundary detection
_find_best_split_point()
- Finds optimal split position in text
- Prioritizes readability (paragraph > sentence > word)
- Returns 0 if no good split point found
Existing Methods (Preserved)
format_code_block()- Wraps content in Discord code blockschunk_response()- Simple line-based chunkingformat_error()- Formats error messages for Discord
Test Coverage
Test Suite: tests/test_response_formatter.py
Total Tests: 26
Pass Rate: 100% (26/26)
Test Categories:
-
Basic Functionality (4 tests)
- Short responses
- Empty/whitespace input
- Exactly max_length input
-
Smart Chunking (5 tests)
- Long responses without code
- Paragraph boundaries
- Sentence boundaries
- Word boundaries
- Very long single lines
-
Code Block Preservation (5 tests)
- Single code block
- Multiple code blocks
- Code block at chunk boundary
- Large code blocks (>2000 chars)
- Code blocks without language
-
Mixed Content (2 tests)
- Mixed markdown preservation
- Multiple paragraphs
-
Code Block Splitting (2 tests)
- split_on_code_blocks=False
- split_on_code_blocks=True
-
Edge Cases (4 tests)
- Code block exactly max_length
- Consecutive code blocks
- Very long single word
- Custom max_length
-
Helper Methods (4 tests)
- format_code_block() with/without language
- format_error()
- chunk_response()
Integration Testing
Bot Tests: All 20 bot.py tests pass with new formatter Full Suite: 109/110 tests pass (1 unrelated failure in claude_runner)
Example Outputs
Example 1: Short Response
Input: 57 chars Output: 1 chunk
Example 2: Long Text with Paragraphs (3524 chars)
Output: 3 chunks
- Chunk 1: 1159 chars
- Chunk 2: 1199 chars
- Chunk 3: 1160 chars Split on paragraph boundaries (\n\n)
Example 3: Text with Code Block
Input: 1336 chars (text + code + text) Output: 1 chunk (fits comfortably) Code block preserved intact
Example 4: Large Code Block (2341 chars)
Output: 2 chunks
- Chunk 1: 1984 chars (
python...) - Chunk 2: 370 chars (
python...) Both chunks have proper code block markers
Example 5: Multiple Code Blocks
Input: 146 chars (3 small code blocks) Output: 1 chunk All code blocks preserved
Example 6: Mixed Markdown (1150 chars)
Output: 1 chunk Bold, italic, lists, and code all preserved
Files Modified
-
claude_coordinator/response_formatter.py
- Added
format_response()method - Added 4 private helper methods
- Preserved existing methods
- Total lines: ~372 (up from 73)
- Added
-
tests/test_response_formatter.py (NEW)
- 26 comprehensive test cases
- 6 test classes covering all scenarios
- Total lines: ~364
Validation Commands
# Run response formatter tests
ssh discord-coordinator "cd /opt/projects/claude-coordinator && .venv/bin/python -m pytest tests/test_response_formatter.py -v"
# Run bot tests to verify integration
ssh discord-coordinator "cd /opt/projects/claude-coordinator && .venv/bin/python -m pytest tests/test_bot.py -v"
# Run all tests
ssh discord-coordinator "cd /opt/projects/claude-coordinator && .venv/bin/python -m pytest tests/ -v"
# Run demo examples
ssh discord-coordinator "cd /opt/projects/claude-coordinator && python3 /tmp/demo_formatter.py"
Technical Decisions
-
Regex for Code Block Detection
- Pattern:
r'```(\w*)\n(.*?)\n```'withre.DOTALL - Captures language identifier and content separately
- Handles code blocks without language (empty group)
- Pattern:
-
Split Point Thresholds
- Paragraph: Must be >50% through text
- Line: Must be >30% through text
- Sentence: Must be >30% through text
- Word: Must be >20% through text
- Prevents tiny leading chunks
-
Code Block Overhead Calculation
- Delimiter:
```language\n\n```= ~14 chars base - Dynamic based on language string length
- Conservative to prevent edge cases
- Delimiter:
-
Empty Input Handling
- Returns empty list (not single empty string)
- Allows caller to check
if chunks:cleanly - Matches Discord behavior (no empty messages)
Known Limitations
-
Nested Code Blocks
- Regex doesn't handle markdown inside code blocks
- Rare edge case in typical Claude output
-
Split Point Optimization
- Uses simple heuristics (50%, 30%, 20%)
- Could be tuned based on real-world usage
-
Language-Specific Syntax
- Doesn't parse code syntax for smart splits
- Splits on line boundaries regardless of language
Future Enhancements (Optional)
- Add support for nested markdown structures
- Language-aware code splitting (e.g., split Python on function boundaries)
- Configurable split point thresholds
- Statistics/logging for chunk distribution
- Support for Discord embeds (2048 char limit)
Deployment Notes
- Implementation is backward compatible
- No configuration changes required
- No database migrations needed
- Bot automatically uses new formatter
- Zero downtime deployment
Engineer: Atlas (Principal Software Engineer) Validated: 2026-02-13 Test Results: 26/26 tests passing (100%) Integration: All bot tests passing