react learning mode

This commit is contained in:
ValueOn AG 2025-10-04 02:54:40 +02:00
parent d20ab7ab0e
commit c93a5d28c4
3 changed files with 964 additions and 0 deletions

View file

@ -0,0 +1,339 @@
# Refactoring Recommendations for Workflow Processing
## Current Issues
### 1. File Size Problem
- **handlingTasks.py**: 2,192 lines - too large for maintainability
- **Mixed responsibilities**: Task planning, execution, messaging, validation all in one class
- **Complex conditional logic**: Mode switching creates nested conditions
### 2. Code Duplication
- **Message creation**: Similar logic in `createActionMessage()` and `createReactActionMessage()`
- **AI calls**: Repeated patterns for different prompt types
- **Validation**: Similar validation logic across modes
### 3. Tight Coupling
- **Mode-specific logic**: Mixed with common functionality
- **Hard to test**: Large methods with multiple responsibilities
- **Hard to extend**: Adding new modes requires modifying existing code
## Proposed Refactoring Structure
```
gateway/modules/workflows/processing/
├── core/ # Common functionality
│ ├── __init__.py
│ ├── taskPlanner.py # Task planning (common)
│ ├── actionExecutor.py # Action execution (common)
│ ├── messageCreator.py # Generic message creation (all workflow phases)
│ ├── validator.py # Validation logic (common)
│ └── workflowCoordinator.py # Main workflow coordination
├── modes/ # Mode-specific implementations
│ ├── __init__.py
│ ├── baseMode.py # Abstract base class
│ ├── actionplanMode.py # Actionplan mode implementation
│ └── reactMode.py # React mode implementation
├── shared/ # Shared utilities
│ ├── __init__.py
│ ├── executionState.py # State management
│ ├── promptFactory.py # Prompt generation (reusable functions)
│ └── promptFactoryPlaceholders.py
└── workflowProcessor.py # Main processor (renamed from handlingTasks.py)
```
## Detailed Refactoring Plan
### Phase 1: Extract Common Functionality
#### 1.1 Create Core Modules
**taskPlanner.py**
```python
class TaskPlanner:
async def generateTaskPlan(self, userInput: str, workflow) -> TaskPlan:
# Move from handlingTasks.py:87-267
# Common task planning logic
```
**actionExecutor.py**
```python
class ActionExecutor:
async def executeSingleAction(self, action, workflow, task_step, ...):
# Move from handlingTasks.py:1562-1710
# Common action execution logic
```
**messageCreator.py**
```python
class MessageCreator:
async def createTaskPlanMessage(self, task_plan, workflow):
# Move from handlingTasks.py:269-308
# Generic task plan messaging
async def createTaskStartMessage(self, task_step, workflow, task_index, total_tasks):
# Move from handlingTasks.py:882-910
# Generic task start messaging
async def createActionMessage(self, action, result, workflow, ...):
# Move from handlingTasks.py:1712-1789
# Generic action messaging (success/failure)
async def createTaskCompletionMessage(self, task_step, workflow, task_index, total_tasks, review_result):
# Move from handlingTasks.py:1069-1103
# Generic task completion messaging
async def createRetryMessage(self, task_step, workflow, task_index, review_result):
# Move from handlingTasks.py:1176-1195
# Generic retry messaging
async def createErrorMessage(self, task_step, workflow, task_index, error_details):
# Move from handlingTasks.py:1201-1252
# Generic error messaging
```
**validator.py**
```python
class WorkflowValidator:
def validateTask(self, task_plan: Dict[str, Any]) -> bool:
# Move from handlingTasks.py:1793-1850
# Renamed from _validateTaskPlan, removed underscore
def validateAction(self, actions: List[Dict[str, Any]], context) -> bool:
# Move from handlingTasks.py:1906-1938
# Renamed from _validateActions, removed underscore, singular form
```
### Phase 2: Create Mode-Specific Implementations
#### 2.1 Base Mode Class
**baseMode.py**
```python
from abc import ABC, abstractmethod
class BaseMode(ABC):
def __init__(self, services, workflow):
self.services = services
self.workflow = workflow
self.taskPlanner = TaskPlanner(services)
self.actionExecutor = ActionExecutor(services)
self.messageCreator = MessageCreator(services)
self.validator = WorkflowValidator(services)
@abstractmethod
async def executeTask(self, task_step, workflow, context, ...) -> TaskResult:
pass
@abstractmethod
async def generateTaskActions(self, task_step, workflow, ...) -> List[TaskAction]:
pass
```
#### 2.2 Actionplan Mode
**actionplanMode.py**
```python
class ActionplanMode(BaseMode):
async def executeTask(self, task_step, workflow, context, ...) -> TaskResult:
# Move from handlingTasks.py:972-1261
# Actionplan-specific execution logic
async def generateTaskActions(self, task_step, workflow, ...) -> List[TaskAction]:
# Move from handlingTasks.py:445-643
# Batch action generation
```
#### 2.3 React Mode
**reactMode.py**
```python
class ReactMode(BaseMode):
async def executeTask(self, task_step, workflow, context, ...) -> TaskResult:
# Move from handlingTasks.py:916-970
# React-specific execution logic
async def plan_select(self, context: TaskContext) -> Dict[str, Any]:
# Move from handlingTasks.py:647-692
async def act_execute(self, context: TaskContext, selection: Dict[str, Any], ...):
# Move from handlingTasks.py:694-773
def observe_build(self, action_result: ActionResult) -> Dict[str, Any]:
# Move from handlingTasks.py:775-822
async def refine_decide(self, context: TaskContext, observation: Dict[str, Any]) -> Dict[str, Any]:
# Move from handlingTasks.py:824-863
```
### Phase 3: Simplify Main Coordinator
**workflowProcessor.py** (Simplified)
```python
class WorkflowProcessor:
def __init__(self, services, workflow=None):
self.services = services
self.workflow = workflow
self.mode = self._createMode(workflow.workflowMode)
def _createMode(self, workflowMode: str) -> BaseMode:
if workflowMode == "React":
return ReactMode(self.services, self.workflow)
else:
return ActionplanMode(self.services, self.workflow)
async def generateTaskPlan(self, userInput: str, workflow) -> TaskPlan:
return await self.mode.taskPlanner.generateTaskPlan(userInput, workflow)
async def executeTask(self, task_step, workflow, context, ...) -> TaskResult:
return await self.mode.executeTask(task_step, workflow, context, ...)
# Delegate other methods to appropriate components
```
### Phase 4: Create Shared Utilities
#### 4.1 Enhanced Prompt Factory (Reusable Functions)
**promptFactory.py** (Enhanced)
```python
class PromptFactory:
def __init__(self, services):
self.services = services
def createPrompt(self, template_type: str, context: Any) -> str:
# Centralized prompt creation
# Move from promptFactory.py and promptFactoryPlaceholders.py
def extractPlaceholders(self, context: Any) -> Dict[str, str]:
# Centralized placeholder extraction
# Reusable prompt element functions (called by both modes)
def getAvailableDocuments(self, context: Any) -> str:
# Move from promptFactory.py:_getAvailableDocuments()
def getWorkflowHistory(self, services, context: Any) -> str:
# Move from promptFactory.py:_getPreviousRoundContext()
def getAvailableMethods(self, services) -> str:
# Move from promptFactory.py:getMethodsList()
def getEnhancedDocumentContext(self, services) -> str:
# Move from promptFactory.py:getEnhancedDocumentContext()
def getConnectionReferenceList(self, services) -> str:
# Move from promptFactory.py:_getConnectionReferenceList()
```
## Benefits of Refactoring
### 1. Maintainability
- **Smaller files**: Each file has single responsibility
- **Clear separation**: Mode-specific vs common logic
- **Easier debugging**: Isolated functionality
### 2. Testability
- **Unit testing**: Each component can be tested independently
- **Mocking**: Easier to mock dependencies
- **Integration testing**: Clear interfaces between components
### 3. Extensibility
- **New modes**: Add new mode by implementing BaseMode
- **New features**: Add to appropriate component
- **Configuration**: Mode-specific configuration
### 4. Code Reuse
- **Common logic**: Shared across modes
- **Reduced duplication**: Single implementation of common functionality
- **Consistent behavior**: Same logic for same operations
## Migration Strategy
### Step 1: Create New Structure
1. Create new directory structure
2. Create base classes and interfaces
3. Move common functionality to core modules
### Step 2: Extract Mode-Specific Logic
1. Create ActionplanMode class
2. Create ReactMode class
3. Move mode-specific methods
### Step 3: Update Main Coordinator
1. Simplify HandlingTasks class
2. Use composition instead of inheritance
3. Delegate to appropriate components
### Step 4: Testing and Validation
1. Create unit tests for each component
2. Integration tests for each mode
3. End-to-end workflow tests
### Step 5: Cleanup
1. Remove old code
2. Update imports
3. Update documentation
## File Size Reduction
### Current State
- **handlingTasks.py**: 2,192 lines
### After Refactoring
- **workflowProcessor.py**: ~200 lines (main processor)
- **core/taskPlanner.py**: ~300 lines
- **core/actionExecutor.py**: ~400 lines
- **core/messageCreator.py**: ~400 lines (generic messages for all workflow phases)
- **modes/actionplanMode.py**: ~400 lines
- **modes/reactMode.py**: ~350 lines
- **shared/promptFactory.py**: ~300 lines (reusable prompt element functions)
- **shared/executionState.py**: ~100 lines
**Total**: ~2,450 lines (slightly more due to better organization and generic message handling)
## Implementation Priority
1. **High Priority**: Extract common functionality (Phase 1)
2. **Medium Priority**: Create mode-specific implementations (Phase 2)
3. **Low Priority**: Create shared utilities (Phase 4)
4. **Final**: Simplify main coordinator (Phase 3)
This refactoring will make the codebase more maintainable, testable, and extensible while preserving all existing functionality.
## Key Changes Based on Review
### ✅ Prompt Elements as Reusable Functions
- **Before**: Duplicated prompt content in each mode
- **After**: Reusable functions like `getAvailableDocuments()`, `getWorkflowHistory()`, etc.
- **Benefit**: Single source of truth, easier maintenance
### ✅ Group Imports with `__init__.py`
- **Pattern**: `import core` then `core.taskPlanner.xxx()`
- **Benefit**: Clear calling references, better maintainability
- **Files**: All modules have `__init__.py` for group imports
### ✅ Generic MessageCreator
- **Before**: Mode-specific messages (`createReactActionMessage`)
- **After**: Generic messages for all workflow phases
- **Messages**: Task plan, task start/end, action start/end, retry, error
- **Benefit**: Consistent messaging across modes
### ✅ Clean Function Names
- **Before**: `_validateTaskPlan()`, `_validateActions()`
- **After**: `validateTask()`, `validateAction()`
- **Benefit**: No underscores, singular forms, clearer naming
### ✅ No Unnecessary AICallManager
- **Removed**: AICallManager wrapper
- **Reason**: AI calls already well-parameterized with centralized service
- **Benefit**: Avoid over-engineering, keep existing good patterns
### ✅ Enhanced PromptFactory
- **Focus**: Reusable prompt element functions
- **Functions**: `getAvailableDocuments()`, `getWorkflowHistory()`, `getAvailableMethods()`, etc.
- **Benefit**: Both modes use same prompt building logic
### ✅ Better Naming: workflowProcessor.py
- **Before**: `handlingTasks.py` (unclear role)
- **After**: `workflowProcessor.py` (clear processing role)
- **Class**: `HandlingTasks``WorkflowProcessor`
- **Benefit**: Clear relationship with `workflowManager.py`

View file

@ -0,0 +1,241 @@
# 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
1. **After each action**: System records what was attempted and how well it worked
2. **Pattern recognition**: System identifies successful vs failed approaches
3. **Strategy update**: System updates its approach for similar future requests
4. **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
1. **Data type match**: Does content contain what user asked for?
2. **Format match**: Is it in the expected format?
3. **Quality check**: Is the content usable and complete?
4. **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
1. **Completed objectives**: What has been successfully delivered
2. **Partial achievements**: What was partially successful
3. **Failed attempts**: What didn't work and why
4. **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**:
1. **Modify `_observeBuild`** in `reactMode.py`:
- Add content analysis to observation
- Include actual content snippets, not just metadata
- Add intent match assessment
2. **Enhance `extractReviewContent`** in `promptFactoryPlaceholders.py`:
- Include content analysis in review
- Add specific validation against user intent
- Provide clear improvement suggestions
**New files**:
- `gateway/modules/workflows/processing/adaptive/intentAnalyzer.py`
- `gateway/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**:
1. **Modify `_refineDecide`** in `reactMode.py`:
- Use learned strategies for action planning
- Apply learned patterns to avoid repeated failures
- Update strategies based on validation results
2. **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.py`
- `gateway/modules/workflows/processing/adaptive/strategyStore.py`
### Phase 3: Add Progress Tracking
**Goal**: Make React mode understand what has been accomplished
**Changes to existing code**:
1. **Modify `executeTask`** in `reactMode.py`:
- Track progress across steps
- Use progress to inform next actions
- Stop when objectives are actually met
2. **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
1. **ActionExecutor**: Enhanced to use learned strategies
2. **MessageCreator**: Enhanced to show adaptive progress
3. **ReactMode**: Enhanced with adaptive process flow
4. **PromptFactory**: Enhanced with learned prompt patterns
## Implementation Steps
### Step 1: Create Adaptive Directory
```bash
mkdir -p gateway/modules/workflows/processing/adaptive
```
### Step 2: Implement Core Adaptive Components
1. Create `intentAnalyzer.py` - analyzes user intent
2. Create `contentValidator.py` - validates delivered content
3. Create `learningEngine.py` - learns from feedback
4. Create `progressTracker.py` - tracks progress
### Step 3: Integrate with Existing React Mode
1. Modify `reactMode.py` to use adaptive components
2. Enhance observation building with content analysis
3. Add learning to action planning
4. Add progress tracking to execution flow
### Step 4: Test and Validate
1. Test with various user intents
2. Validate learning effectiveness
3. 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
```

View file

@ -0,0 +1,384 @@
# Adaptive React Mode: Technical Implementation
## How the Adaptive Engine Works
### The Core Process Flow
```
User Request → Intent Analysis → Action Planning → Execution →
Content Validation → Learning Update → Progress Check → Next Action
```
### Step 1: Intent Analysis Process
**What happens**: System analyzes what user actually wants
**Input**: User prompt "Calculate first 1000 prime numbers"
**Process**:
1. Check for data type keywords (numbers, list, calculate)
2. Check for format preferences (raw data vs formatted)
3. Extract success criteria (first 1000, prime numbers)
4. Determine quality requirements (accuracy, completeness)
**Output**: Intent object
```python
{
"dataType": "numbers",
"expectedFormat": "raw_data",
"successCriteria": ["first 1000", "prime numbers"],
"qualityRequirements": {"accuracy": 0.95, "completeness": 0.95}
}
```
### Step 2: Action Planning Process
**What happens**: System plans action based on intent + learned strategies
**Input**: Intent analysis + learned strategies
**Process**:
1. Check learned strategies for similar intents
2. If learned: Use successful strategy
3. If not learned: Use default strategy
4. Generate specific action parameters
**Output**: Action plan
```python
{
"method": "ai",
"action": "process",
"parameters": {
"aiPrompt": "Deliver the first 1000 prime numbers as a list"
}
}
```
### Step 3: Content Validation Process
**What happens**: Check if delivered content matches intent
**Input**: ActionResult + Intent analysis
**Process**:
1. Extract content from documents
2. Check data type match (numbers vs code)
3. Check format match (raw data vs formatted)
4. Check success criteria (first 1000, prime numbers)
5. Calculate quality score
**Output**: Validation result
```python
{
"overallSuccess": true,
"dataTypeMatch": true,
"formatMatch": true,
"successCriteriaMet": [true, true],
"qualityScore": 0.95,
"issues": [],
"improvementSuggestions": []
}
```
### Step 4: Learning Update Process
**What happens**: Learn from the result and update strategies
**Input**: Validation result + action taken
**Process**:
1. Record what was attempted
2. Record how well it worked
3. Update strategy for similar future requests
4. Store pattern for future use
**Output**: Updated strategy
```python
{
"pattern": "numbers_request",
"successfulAction": "ai.process with data delivery prompt",
"successRate": 0.95,
"lastUpdated": "2025-01-04T10:30:00Z"
}
```
### Step 5: Progress Check Process
**What happens**: Determine if task is complete
**Input**: Validation result + progress state
**Process**:
1. Check if all success criteria are met
2. Check if quality requirements are satisfied
3. Determine if more actions are needed
4. Suggest next action if needed
**Output**: Continue/Stop decision
```python
{
"decision": "stop",
"reason": "All success criteria met",
"progress": 100,
"nextAction": null
}
```
## Implementation Details
### 1. Intent Analyzer
**Purpose**: Understand what user actually wants
**Location**: `gateway/modules/workflows/processing/adaptive/intentAnalyzer.py`
**Key Methods**:
- `analyzeUserIntent(userPrompt, context)` - Main analysis method
- `_classifyDataType(prompt)` - Determine if user wants numbers, text, documents, etc.
- `_extractSuccessCriteria(prompt, context)` - Extract specific requirements
- `_assessQualityRequirements(prompt)` - Determine quality needs
**Integration**: Called in `reactMode.py` before action planning
### 2. Content Validator
**Purpose**: Validate delivered content against user intent
**Location**: `gateway/modules/workflows/processing/adaptive/contentValidator.py`
**Key Methods**:
- `validateContent(documents, intent)` - Main validation method
- `_checkDataTypeMatch(content, dataType)` - Check if content matches expected type
- `_checkSuccessCriteria(content, criteria)` - Check if requirements are met
- `_calculateQualityScore(content, intent)` - Calculate overall quality
**Integration**: Called in `reactMode.py` after action execution
### 3. Learning Engine
**Purpose**: Learn from feedback and adapt strategies
**Location**: `gateway/modules/workflows/processing/adaptive/learningEngine.py`
**Key Methods**:
- `learnFromFeedback(feedback, context, intent)` - Learn from action result
- `getImprovedStrategy(context, intent)` - Get learned strategy for planning
- `_updateStrategy(pattern, success)` - Update strategy based on result
- `_findRelevantPatterns(context, intent)` - Find applicable learned patterns
**Integration**: Called in `reactMode.py` after validation and before next action
### 4. Progress Tracker
**Purpose**: Track what has been accomplished
**Location**: `gateway/modules/workflows/processing/adaptive/progressTracker.py`
**Key Methods**:
- `updateProgress(result, validation, intent)` - Update progress state
- `getCurrentProgress()` - Get current progress status
- `shouldContinue(progress, validation)` - Determine if should continue
- `getNextActionSuggestions(progress, intent)` - Suggest next actions
**Integration**: Called in `reactMode.py` for progress tracking and stopping decisions
## Integration with Existing React Mode
### Modified Methods in `reactMode.py`
#### 1. `executeTask` Method
**Changes**:
- Add intent analysis at start
- Add progress tracking throughout
- Add learning updates after each action
- Use adaptive stopping logic
```python
async def executeTask(self, taskStep, workflow, context, taskIndex=None, totalTasks=None):
# NEW: Analyze user intent
intentAnalysis = self.intentAnalyzer.analyzeUserIntent(taskStep.objective, context)
# NEW: Initialize progress tracking
progressState = ProgressState()
while step <= max_steps:
# NEW: Get learned strategy
strategy = self.learningEngine.getImprovedStrategy(context, intentAnalysis)
# Existing: Execute action
result = await self._actExecute(context, selection, taskStep, workflow, step)
# NEW: Validate content
validationResult = self.contentValidator.validateContent(result.documents, intentAnalysis)
# NEW: Learn from result
feedback = self._collectFeedback(result, validationResult, intentAnalysis)
self.learningEngine.learnFromFeedback(feedback, context, intentAnalysis)
# NEW: Update progress
self.progressTracker.updateProgress(result, validationResult, intentAnalysis)
# NEW: Check if should continue
if not self.progressTracker.shouldContinue(progressState, validationResult):
break
```
#### 2. `_observeBuild` Method
**Changes**:
- Add content analysis to observation
- Include actual content snippets
- Add intent match assessment
```python
def _observeBuild(self, actionResult):
# Existing: Basic observation
observation = {
"success": bool(actionResult.success),
"documentsCount": len(actionResult.documents),
"previews": self._buildDocumentPreviews(actionResult.documents)
}
# NEW: Add content analysis
if actionResult.documents:
content = self._extractContent(actionResult.documents[0])
observation["contentAnalysis"] = {
"contentType": self._classifyContent(content),
"contentSnippet": content[:200] + "..." if len(content) > 200 else content,
"intentMatch": self._assessIntentMatch(content, self.currentIntent)
}
return observation
```
#### 3. `_refineDecide` Method
**Changes**:
- Use learned strategies for decision making
- Consider progress state in decisions
- Apply learned patterns to avoid repeated failures
```python
async def _refineDecide(self, context, observation, intentAnalysis, progressState):
# NEW: Get learned strategies
strategies = self.learningEngine.getImprovedStrategy(context, intentAnalysis)
# NEW: Check progress state
if self.progressTracker.shouldContinue(progressState, observation):
return {"decision": "continue", "reason": "More work needed"}
# NEW: Apply learned patterns
if strategies and strategies.get("avoidActions"):
# Avoid actions that have failed before
pass
return {"decision": "stop", "reason": "Objective completed"}
```
### Enhanced Prompt Templates
#### 1. Action Selection Prompt
**Enhancement**: Include learned strategies and intent-specific guidance
```python
def createActionSelectionPromptTemplate():
return """
Select exactly one action to advance the task.
OBJECTIVE: {{KEY:USER_PROMPT}}
USER INTENT: {{KEY:USER_INTENT}} # NEW: Intent analysis
LEARNED STRATEGIES: {{KEY:LEARNED_STRATEGIES}} # NEW: Learned patterns
AVAILABLE METHODS: {{KEY:AVAILABLE_METHODS}}
CRITICAL RULES:
- If user wants DATA (numbers, lists, calculations): Use appropriate method to DELIVER the actual data, not code
- If user wants DOCUMENTS (Word, PDF, Excel): Use appropriate method to create the document
- If user wants ANALYSIS: Use appropriate method to analyze and deliver insights
- NEVER ask AI to write code when user wants data - deliver the data directly
- Apply learned strategies: {{KEY:LEARNED_STRATEGIES}}
"""
```
#### 2. Refinement Prompt
**Enhancement**: Include content validation and progress assessment
```python
def createRefinementPromptTemplate():
return """
Decide next step based on observation.
OBJECTIVE: {{KEY:USER_PROMPT}}
USER INTENT: {{KEY:USER_INTENT}} # NEW: Intent analysis
CONTENT VALIDATION: {{KEY:CONTENT_VALIDATION}} # NEW: Validation results
PROGRESS STATE: {{KEY:PROGRESS_STATE}} # NEW: Progress tracking
CRITICAL RULES:
- If user wants DATA (numbers, lists, calculations): Ensure AI delivers the actual data, not code
- If user wants DOCUMENTS (Word, PDF, Excel): Ensure appropriate method is used to create the document
- If user wants ANALYSIS: Ensure AI analyzes and delivers insights
- NEVER accept code when user wants data - demand the actual data
- Consider progress: {{KEY:PROGRESS_STATE}}
"""
```
## Implementation Steps
### Step 1: Create Adaptive Directory Structure
```bash
mkdir -p gateway/modules/workflows/processing/adaptive
```
### Step 2: Implement Core Components
1. **Intent Analyzer** (`intentAnalyzer.py`)
- Analyze user prompts for intent
- Classify data types and formats
- Extract success criteria
2. **Content Validator** (`contentValidator.py`)
- Validate delivered content
- Check against user intent
- Calculate quality scores
3. **Learning Engine** (`learningEngine.py`)
- Learn from feedback
- Store and retrieve strategies
- Adapt future behavior
4. **Progress Tracker** (`progressTracker.py`)
- Track task progress
- Make continue/stop decisions
- Suggest next actions
### Step 3: Integrate with React Mode
1. **Modify `reactMode.py`**:
- Add adaptive components
- Enhance observation building
- Add learning to action planning
- Add progress tracking
2. **Enhance prompt templates**:
- Add intent analysis to prompts
- Include learned strategies
- Add content validation results
### Step 4: Test and Validate
1. Test with various user intents
2. Validate learning effectiveness
3. 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
```