Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.PHONY: help backend frontend dev install-backend install-frontend install clean delete-db test
.PHONY: help backend frontend dev install-backend install-frontend install clean delete-db test test-evals

# Default target - show help
help:
Expand All @@ -18,7 +18,8 @@ help:
@echo "Utilities:"
@echo " make clean - Remove build artifacts and data"
@echo " make delete-db - Delete SQLite database file"
@echo " make test - Run tests"
@echo " make test - Run all backend tests"
@echo " make test-evals - Run evaluation tests (requires server running)"
@echo " make help - Show this help message"

# Start backend server
Expand Down Expand Up @@ -85,3 +86,11 @@ test:
cd backend && go test ./...
@echo "Tests complete!"

# Run evaluation tests (requires server to be running)
test-evals:
@echo "Running evaluation tests..."
@echo "Note: Make sure the backend server is running on http://localhost:8080"
@echo ""
cd backend && go test -v ./evals -run TestSystemMessages
@echo "Evaluation tests complete!"

Binary file added __pycache__/agent_eval.cpython-312.pyc
Binary file not shown.
4 changes: 4 additions & 0 deletions backend/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
*.db-wal
data/

# Log files
logs/
*.log

# Go
*.exe
*.exe~
Expand Down
178 changes: 178 additions & 0 deletions backend/SUMMARY.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
# Master Node Refactoring Summary

## Overview

This document summarizes the refactoring of the master node architecture to improve code clarity, eliminate duplication, enhance extensibility, and fix streaming issues.

## Date
December 2024

## Changes Made

### 1. Architecture Refactoring

#### New Files Created

**`agentFactory.go`**
- Centralized agent creation logic
- `AgentFactory` struct with `CreateThinkingAgent()` and `CreateResponseAgent()` methods
- Moved `ThinkingAgentSystemPrompt` constant
- Eliminates duplicate agent creation code

**`responseValidator.go`**
- Pluggable response validation interface
- `ResponseValidator` interface with `Validate()` and `ToPrompt()` methods
- `MasterNodeResponseValidator` implementation with robust field validation
- Validates required fields based on success status
- `ParseMasterNodeResponse()` as convenience wrapper

**`retryExecutor.go`**
- Configurable retry logic with exponential backoff
- `RetryConfig` struct with `MaxAttempts`, `BaseDelay`, `MaxDelay`, `BackoffFactor`
- `RetryExecutor` with `ExecuteWithRetry()` and `ExecuteWithRetryResult[T]()` methods
- Reusable across all nodes

**`agentExecutor.go`**
- Unified agent execution strategy
- `AgentExecutionStrategy` consolidates thinking and response agent patterns
- Handles streaming, completion, and validation
- Supports async execution and retry logic
- Methods: `ExecuteAsync()`, `ExecuteWithRetry()`, `ExecuteThinkingPlan()`

**`masterProcessor.go`**
- Main processing orchestration
- `MasterNodeProcessor` orchestrates the complete workflow
- Methods: `Process()`, `generateThinkingPlan()`, `executeMainRequest()`, `handleValidatedResponse()`
- Clear separation of concerns

#### Files Modified

**`masterNode.go`**
- Simplified main `MasterNode` function (reduced from ~200 lines to ~30 lines)
- Removed inline agent creation and execution logic
- Removed `ThinkingAgentSystemPrompt` and `MasterAgentSystemPrompt` constants (moved to `agentFactory.go`)
- Updated `StreamResponse()` and `StreamThinking()` to remove tag wrapping (fixes fragmentation issue)
- Now uses `MasterNodeProcessor` for orchestration

**`responseModels.go`**
- Removed `ParseMasterNodeResponse()` function (moved to `responseValidator.go`)
- Removed unused imports

**`utils.go`**
- Updated `attemptRequest()` to use `RetryExecutor` internally
- Marked as deprecated in favor of `RetryExecutor`
- Removed unused `log` import

### 2. Streaming Fix

**Problem**: Each streaming chunk was wrapped in `<response>` or `<thinking>` tags, causing fragmentation:
```
<response>{</response><response> </response><response>"</response>...
```

**Solution**: Removed tag wrapping from `StreamResponse()` and `StreamThinking()` methods. The `status` field already indicates content type (`"thinking"` or `"streaming"`), so tags are unnecessary.

**Result**: Clean, unfragmented streaming content.

### 3. Test Script

**`test_stream.go`**
- Test script for streaming agent chat endpoint
- Sends POST request to `/api/agent/chat` with "test" message
- Displays:
- Thinking trace in real-time
- Tool execution notifications
- Streaming response content
- Final JSON response object
- Handles SSE parsing and JSON formatting

## Benefits

### Extensibility
- Interface-based design allows pluggable validators
- Configurable retry strategies
- Factory pattern for agent creation
- Easy to add new response types or execution strategies

### Clarity
- Single-responsibility components
- Clear separation of concerns
- Well-documented code
- Reduced complexity in main function

### Robustness
- Improved error handling
- Context cancellation support
- Field validation for responses
- Configurable retry with exponential backoff

### Readability
- Eliminated duplicate code patterns
- Better code organization
- Consistent patterns across components
- In-code documentation

## Backward Compatibility

All changes maintain backward compatibility:
- `MasterNode` function signature unchanged
- `MasterNodeStreamHandler` API unchanged
- `ExtractMasterNodeState` function unchanged
- All existing state keys and routing logic preserved

## Testing

The refactoring was verified with:
- `test_stream.go` script showing clean output
- Server health checks
- Multiple test runs confirming consistent behavior
- Verification that tags are no longer fragmenting responses

## Files Summary

### Created
- `backend/internal/workflows/nodes/agentFactory.go`
- `backend/internal/workflows/nodes/responseValidator.go`
- `backend/internal/workflows/nodes/retryExecutor.go`
- `backend/internal/workflows/nodes/agentExecutor.go`
- `backend/internal/workflows/nodes/masterProcessor.go`
- `backend/test_stream.go`
- `backend/SUMMARY.md` (this file)

### Modified
- `backend/internal/workflows/nodes/masterNode.go`
- `backend/internal/workflows/nodes/responseModels.go`
- `backend/internal/workflows/nodes/utils.go`

## Key Improvements

1. **Eliminated Duplication**: Agent creation and execution patterns consolidated
2. **Fixed Streaming**: Removed tag wrapping that caused fragmentation
3. **Improved Testability**: Each component can be tested independently
4. **Better Error Handling**: Configurable retry with proper error propagation
5. **Enhanced Maintainability**: Clear separation of concerns and single responsibility

## Usage

### Running Tests
```bash
cd backend
go run test_stream.go
```

### Server Restart
```bash
# Stop server
ps aux | grep "go run cmd/server/main.go" | grep -v grep | awk '{print $2}' | xargs kill

# Start server
cd backend && nohup go run cmd/server/main.go > /tmp/server.log 2>&1 &
```

## Notes

- All code follows established patterns (interface-first design, dependency injection, in-code documentation)
- The refactoring maintains the existing API contracts
- Streaming now works correctly without tag fragmentation
- Test script provides easy way to verify streaming functionality

Loading