8.6 KiB
Adaptive React Mode: Process-Focused Implementation
Core Problem
Current React mode is blind - it doesn't understand what the user actually wants and can't learn from mistakes. It generates code when user wants data, and has no way to improve.
How the Adaptive Engine Works
The Process Flow
1. User Request → 2. Intent Analysis → 3. Action Planning → 4. Execution →
5. Content Validation → 6. Learning Update → 7. Progress Check → 8. Next Action
Step-by-Step Process
Step 1: Intent Analysis
What happens: System analyzes what user actually wants
- Input: User prompt "Calculate first 1000 prime numbers"
- Analysis: User wants DATA (numbers), not CODE (algorithms)
- Output: Intent object with dataType="numbers", format="raw_data"
Step 2: Action Planning
What happens: System plans action based on intent
- Input: Intent analysis + learned strategies
- Process: "User wants numbers → use ai.process with data delivery prompt"
- Output: Action plan with specific parameters
Step 3: Execution
What happens: Execute the planned action
- Input: Action plan
- Process: Call ai.process with "Deliver the first 1000 prime numbers as a list"
- Output: ActionResult with documents
Step 4: Content Validation
What happens: Check if delivered content matches intent
- Input: ActionResult + Intent analysis
- Process: "Does content contain actual numbers? Is it code or data?"
- Output: Validation result (success/failure + specific issues)
Step 5: Learning Update
What happens: Learn from the result
- Input: Validation result + action taken
- Process: "This action worked/didn't work for this type of request"
- Output: Updated strategy for future similar requests
Step 6: Progress Check
What happens: Determine if task is complete
- Input: Validation result + progress state
- Process: "Did we deliver what user wanted? What's still missing?"
- Output: Continue/Stop decision + next action suggestions
The Learning Mechanism
How Learning Works
- After each action: System records what was attempted and how well it worked
- Pattern recognition: System identifies successful vs failed approaches
- Strategy update: System updates its approach for similar future requests
- Adaptive planning: Next action is planned using learned strategies
Example Learning Process
Request: "Prime numbers"
Action 1: ai.process("Write code to generate primes") → Delivers code → FAIL
Learning: "For number requests, don't ask for code generation"
Action 2: ai.process("List the first 1000 prime numbers") → Delivers data → SUCCESS
Learning: "For number requests, ask for data delivery directly"
The Validation Process
What Gets Validated
- Data type match: Does content contain what user asked for?
- Format match: Is it in the expected format?
- Quality check: Is the content usable and complete?
- Success criteria: Does it meet the specific requirements?
Example Validation
User wants: "First 1000 prime numbers"
Content delivered: "def generate_primes(n): ..."
Validation:
- Data type: FAIL (code instead of numbers)
- Format: FAIL (code instead of data)
- Quality: FAIL (not usable as requested)
Result: FAIL - "Deliver actual numbers, not code"
The Progress Tracking
What Gets Tracked
- Completed objectives: What has been successfully delivered
- Partial achievements: What was partially successful
- Failed attempts: What didn't work and why
- Learning insights: What was learned from each attempt
Example Progress Tracking
Objective: "First 1000 prime numbers"
Attempt 1: Generated code → FAIL → Learned: Don't generate code for data requests
Attempt 2: Generated data → SUCCESS → Learned: Direct data delivery works
Progress: 100% complete, strategy updated for future number requests
Implementation Plan
Phase 1: Add Intent Analysis to React Mode
Goal: Make React mode understand what user actually wants
Changes to existing code:
-
Modify
_observeBuildinreactMode.py:- Add content analysis to observation
- Include actual content snippets, not just metadata
- Add intent match assessment
-
Enhance
extractReviewContentinpromptFactoryPlaceholders.py:- Include content analysis in review
- Add specific validation against user intent
- Provide clear improvement suggestions
New files:
gateway/modules/workflows/processing/adaptive/intentAnalyzer.pygateway/modules/workflows/processing/adaptive/contentValidator.py
Phase 2: Add Learning to React Mode
Goal: Make React mode learn from mistakes and improve
Changes to existing code:
-
Modify
_refineDecideinreactMode.py:- Use learned strategies for action planning
- Apply learned patterns to avoid repeated failures
- Update strategies based on validation results
-
Enhance action selection prompts:
- Include learned strategies in AI prompts
- Add specific guidance based on past failures
- Adapt prompts based on user intent type
New files:
gateway/modules/workflows/processing/adaptive/learningEngine.pygateway/modules/workflows/processing/adaptive/strategyStore.py
Phase 3: Add Progress Tracking
Goal: Make React mode understand what has been accomplished
Changes to existing code:
-
Modify
executeTaskinreactMode.py:- Track progress across steps
- Use progress to inform next actions
- Stop when objectives are actually met
-
Enhance stopping logic:
- Check if user intent has been fulfilled
- Consider partial achievements
- Make intelligent continue/stop decisions
New files:
gateway/modules/workflows/processing/adaptive/progressTracker.py
Integration with Existing Refactoring
Follows Generic Workflow Processing Structure
gateway/modules/workflows/processing/
├── core/ # Common functionality
│ ├── actionExecutor.py # Enhanced with adaptive execution
│ └── messageCreator.py # Enhanced with adaptive messaging
├── modes/ # Mode-specific implementations
│ └── reactMode.py # Enhanced with adaptive capabilities
├── shared/ # Shared utilities
│ └── promptFactoryPlaceholders.py # Enhanced with adaptive prompts
└── adaptive/ # NEW: Adaptive components
├── intentAnalyzer.py
├── contentValidator.py
├── learningEngine.py
└── progressTracker.py
Key Integration Points
- ActionExecutor: Enhanced to use learned strategies
- MessageCreator: Enhanced to show adaptive progress
- ReactMode: Enhanced with adaptive process flow
- PromptFactory: Enhanced with learned prompt patterns
Implementation Steps
Step 1: Create Adaptive Directory
mkdir -p gateway/modules/workflows/processing/adaptive
Step 2: Implement Core Adaptive Components
- Create
intentAnalyzer.py- analyzes user intent - Create
contentValidator.py- validates delivered content - Create
learningEngine.py- learns from feedback - Create
progressTracker.py- tracks progress
Step 3: Integrate with Existing React Mode
- Modify
reactMode.pyto use adaptive components - Enhance observation building with content analysis
- Add learning to action planning
- Add progress tracking to execution flow
Step 4: Test and Validate
- Test with various user intents
- Validate learning effectiveness
- Ensure integration with existing refactoring
Expected Results
Before (Current React Mode)
- Blind to user intent
- No learning from mistakes
- Fake success indicators
- No progress understanding
After (Adaptive React Mode)
- Understands user intent
- Learns and improves over time
- Real validation and success assessment
- Tracks progress and makes intelligent decisions
Example Improvement
User: "Calculate first 1000 prime numbers"
Current React Mode:
Step 1: ai.process("Write code to generate primes") → Code → FAIL
Step 2: ai.process("Write code to generate primes") → Code → FAIL
Step 3: ai.process("Write code to generate primes") → Code → FAIL
Result: User gets code, not numbers
Adaptive React Mode:
Step 1: ai.process("Write code to generate primes") → Code → FAIL
Learning: "For number requests, don't ask for code"
Step 2: ai.process("List the first 1000 prime numbers") → Numbers → SUCCESS
Learning: "For number requests, ask for data directly"
Result: User gets actual numbers