File size: 3,886 Bytes
891fe52
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
"""Data models for agent orchestration framework."""

from dataclasses import dataclass, field
from typing import List, Dict, Any, Optional
from datetime import datetime
from enum import Enum


class StepStatus(Enum):
    """Status of a workflow step."""
    PENDING = "pending"
    RUNNING = "running"
    COMPLETED = "completed"
    FAILED = "failed"
    SKIPPED = "skipped"


class IntentType(Enum):
    """Type of research intent."""
    RESEARCH_SURVEY = "research_survey"
    PAPER_COMPARISON = "paper_comparison"
    GAP_ANALYSIS = "gap_analysis"
    CITATION_FINDING = "citation_finding"
    UNKNOWN = "unknown"


@dataclass
class Intent:
    """
    Parsed user intent.
    
    Attributes:
        type: Intent type
        topic: Main research topic
        parameters: Additional parameters
        confidence: Parsing confidence (0-1)
        raw_command: Original command text
    """
    type: IntentType
    topic: str
    parameters: Dict[str, Any]
    confidence: float
    raw_command: str


@dataclass
class WorkflowStep:
    """
    A single step in a workflow.
    
    Attributes:
        name: Step identifier
        tool: Tool to invoke
        arguments: Tool arguments (may reference previous step results)
        critical: If True, failure halts workflow
        timeout: Max execution time in seconds
        retry_count: Number of retries on failure
        description: Human-readable step description
    """
    name: str
    tool: str
    arguments: Dict[str, Any]
    critical: bool = True
    timeout: float = 60.0
    retry_count: int = 0
    description: Optional[str] = None


@dataclass
class Workflow:
    """
    A multi-step research workflow.
    
    Attributes:
        name: Workflow identifier
        description: Human-readable description
        steps: Ordered list of steps
        metadata: Additional workflow metadata
    """
    name: str
    description: str
    steps: List[WorkflowStep]
    metadata: Dict[str, Any] = field(default_factory=dict)
    
    def validate(self) -> None:
        """
        Validate workflow definition.
        
        Raises:
            ValueError: If workflow is invalid
        """
        if not self.name:
            raise ValueError("Workflow name is required")
        
        if not self.steps:
            raise ValueError("Workflow must have at least one step")
        
        # Check for duplicate step names
        step_names = [step.name for step in self.steps]
        if len(step_names) != len(set(step_names)):
            raise ValueError("Workflow has duplicate step names")


@dataclass
class WorkflowState:
    """
    Current state of an executing workflow.
    
    Attributes:
        session_id: Unique session identifier
        workflow: Workflow being executed
        status: Current status (pending, running, completed, failed)
        current_step: Index of current/next step
        completed_steps: List of completed step names
        step_results: Results from each step
        errors: List of errors encountered
        started_at: Workflow start time
    """
    session_id: str
    workflow: Optional[Workflow]
    status: str = "pending"
    current_step: int = 0
    completed_steps: List[str] = field(default_factory=list)
    step_results: Dict[str, Any] = field(default_factory=dict)
    errors: List[str] = field(default_factory=list)
    started_at: datetime = field(default_factory=datetime.now)


@dataclass
class WorkflowResult:
    """
    Result of workflow execution.
    
    Attributes:
        success: Whether workflow completed successfully
        results: Results from all steps
        errors: List of errors
        message: Summary message
        elapsed_seconds: Total execution time
    """
    success: bool
    results: Dict[str, Any]
    errors: List[str]
    message: str
    elapsed_seconds: Optional[float] = None