File size: 10,086 Bytes
b6145cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e45fdd8
b6145cd
 
36f8fda
 
b6145cd
 
 
 
 
e45fdd8
b6145cd
 
8e973ef
b6145cd
 
 
 
 
e45fdd8
b6145cd
 
 
 
8e973ef
e45fdd8
b6145cd
36f8fda
b6145cd
 
 
 
e45fdd8
b6145cd
8e973ef
b6145cd
e45fdd8
8e973ef
b6145cd
8e973ef
e45fdd8
 
b6145cd
 
 
36f8fda
b6145cd
 
8e973ef
b6145cd
 
36f8fda
b6145cd
 
 
36f8fda
 
 
b6145cd
 
 
 
 
 
 
 
 
 
 
 
 
 
e45fdd8
b6145cd
 
e45fdd8
b6145cd
 
 
 
 
e45fdd8
b6145cd
 
 
 
e45fdd8
b6145cd
36f8fda
b6145cd
 
 
e45fdd8
 
 
 
 
 
 
 
 
 
 
b6145cd
36f8fda
 
b6145cd
 
 
 
 
 
 
 
 
e45fdd8
 
 
 
 
 
 
8e973ef
e45fdd8
8e973ef
 
 
e45fdd8
 
 
 
 
8e973ef
e45fdd8
 
 
 
 
8e973ef
e45fdd8
 
 
 
8e973ef
 
 
e45fdd8
 
 
 
 
 
 
 
 
 
 
8e973ef
b6145cd
8e973ef
b6145cd
 
 
 
 
 
 
 
 
8e973ef
 
b6145cd
 
 
 
8e973ef
b6145cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8e973ef
b6145cd
8e973ef
b6145cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8e973ef
b6145cd
8e973ef
36f8fda
 
b6145cd
 
 
 
8e973ef
 
36f8fda
 
b6145cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8e973ef
b6145cd
 
 
8e973ef
b6145cd
 
8e973ef
b6145cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
# agent/graph.py
"""
LangGraph Workflow Definition

This module defines the LangGraph workflow for the operations agent.
It specifies how nodes are connected and under what conditions the
workflow transitions between different processing steps.

Purpose:
- Define the agent workflow as a LangGraph StateGraph
- Specify node connections and conditional routing
- Handle workflow compilation and execution
- Support different execution paths based on intent and results

Dependencies:
- langgraph: Core graph framework
- state.py: Agent state definitions
- nodes.py: Node function implementations

Used by:
- agent.py: Main agent execution
- Direct usage: Workflow testing and debugging
"""

from typing import Dict, Any
from langgraph.graph import StateGraph, END

from .state import AgentState
from .nodes import (
    llm_routing_node,
    tool_execution_node,
    response_generation_node,
    error_handling_node,
    llm_summarization_node
)


def create_operations_workflow() -> Any:
    """
    Create and compile the operations agent workflow with LLM router
    
    This function defines the complete workflow for processing user requests,
    including LLM-based routing, tool execution, and response generation.
    
    Returns:
        Compiled LangGraph workflow ready for execution
    """
    
    print("πŸ”§ Creating operations agent workflow with LLM router...")
    
    # Create the state graph
    workflow = StateGraph(AgentState)
    
    # Add nodes to the workflow (tool_planning node removed as redundant)
    workflow.add_node("llm_routing", llm_routing_node)
    workflow.add_node("tool_execution", tool_execution_node)
    workflow.add_node("llm_summarization", llm_summarization_node)
    workflow.add_node("response_generation", response_generation_node)
    workflow.add_node("error_handling", error_handling_node)
    
    # Set the entry point
    workflow.set_entry_point("llm_routing")
    
    # Define workflow transitions using simplified routing
    workflow.add_conditional_edges(
        "llm_routing",
        _simplified_llm_router,
        {
            "execute_tools": "tool_execution",
            "direct_response": "response_generation",
            "error": "error_handling"
        }
    )
    
    # Tool execution can lead to error handling or LLM summarization
    workflow.add_conditional_edges(
        "tool_execution", 
        _simple_execution_router,
        {
            "has_errors": "error_handling",
            "success": "llm_summarization"
        }
    )
    
    # LLM summarization leads to response generation
    workflow.add_edge("llm_summarization", "response_generation")
    
    # Both response generation and error handling end the workflow
    workflow.add_edge("response_generation", END)
    workflow.add_edge("error_handling", END)
    
    # Compile the workflow
    compiled_workflow = workflow.compile()
    
    print("βœ… Operations workflow compiled successfully")
    
    return compiled_workflow


def create_simple_workflow() -> Any:
    """
    Create a simplified workflow for testing and development with LLM router
    
    This workflow is useful for debugging and development as it
    follows a more linear path with the LLM router.
    
    Returns:
        Compiled simplified workflow
    """
    
    print("πŸ”§ Creating simple operations workflow with LLM router...")
    
    workflow = StateGraph(AgentState)
    
    # Add nodes
    workflow.add_node("llm_routing", llm_routing_node)
    workflow.add_node("tool_execution", tool_execution_node)
    workflow.add_node("llm_summarization", llm_summarization_node)
    workflow.add_node("response_generation", response_generation_node)
    
    # Set entry point
    workflow.set_entry_point("llm_routing")
    
    # Simple conditional flow
    workflow.add_conditional_edges(
        "llm_routing",
        _simple_router,
        {
            "execute_tools": "tool_execution", 
            "direct_response": "response_generation"
        }
    )
    
    workflow.add_edge("tool_execution", "llm_summarization")
    workflow.add_edge("llm_summarization", "response_generation")
    workflow.add_edge("response_generation", END)
    
    compiled_workflow = workflow.compile()
    
    print("βœ… Simple workflow compiled successfully")
    
    return compiled_workflow


def _simple_router(state: AgentState) -> str:
    """Simple router for the simplified workflow"""
    if state.get("processing_status") == "approved" and state.get("planned_tools"):
        return "execute_tools"
    return "direct_response"


def _simplified_llm_router(state: AgentState) -> str:
    """
    Simplified router function to determine path after LLM routing
    
    Trust the LLM router decisions with minimal additional logic.
    
    Args:
        state: Current agent state after LLM routing
        
    Returns:
        "execute_tools" if tools are planned, "direct_response" for help/clarification, "error" for errors
    """
    
    status = state.get("processing_status", "unknown")
    
    # Check for errors from LLM routing
    if status == "error":
        print("🚨 Routing to error handling")
        return "error"
    
    # Check if tools are planned and approved
    if status == "approved" and state.get("planned_tools"):
        print("🎯 Routing to tool execution")
        return "execute_tools"
    
    # Help requests, clarification needs, or rejections go to direct response
    if status in ["needs_clarification", "help_requested", "rejected"]:
        print(f"ℹ️  Routing to direct response ({status})")
        return "direct_response"
    
    # Default fallback
    print("❓ Routing to direct response (fallback)")
    return "direct_response"


def _simple_execution_router(state: AgentState) -> str:
    """
    Simplified router function to determine post-execution path
    
    Args:
        state: Current agent state after tool execution
        
    Returns:
        "has_errors" if errors occurred, "success" otherwise
    """
    
    # Check for errors in execution
    errors = state.get("errors", [])
    if errors:
        print("🚨 Routing to error handling")
        return "has_errors"
    
    # Check if all tool results are successful
    tool_results = state.get("tool_results", [])
    if tool_results and all(result.success for result in tool_results):
        print("βœ… Routing to success response")
        return "success"
    
    # If no tool results or mixed results, treat as partial success
    print("⚠️  Routing to response generation (partial success)")
    return "success"


# Workflow introspection functions
def get_workflow_nodes(workflow: Any) -> list:
    """
    Get list of nodes in the workflow
    
    Args:
        workflow: Compiled workflow
        
    Returns:
        List of node names
    """
    
    # Return the actual nodes in the simplified workflow
    return [
        "llm_routing",
        "tool_execution",
        "response_generation",
        "error_handling"
    ]


def print_workflow_summary(workflow: Any):
    """
    Print a summary of the workflow structure
    
    Args:
        workflow: Compiled workflow to summarize
    """
    
    print("πŸ”„ Operations Agent Workflow Summary:")
    print("   Entry Point: llm_routing")
    print("   Nodes:")
    print("     β€’ llm_routing β†’ [tool_execution | response_generation | error_handling]")
    print("     β€’ tool_execution β†’ [error_handling | llm_summarization]")
    print("     β€’ llm_summarization β†’ response_generation")
    print("     β€’ response_generation β†’ END")
    print("     β€’ error_handling β†’ END")
    print()
    print("   Conditional Routing:")
    print("     β€’ LLM approved with tools? β†’ Tool Execution")
    print("     β€’ LLM clarification/help/rejected? β†’ Direct Response")
    print("     β€’ Tool execution errors? β†’ Error Handling")
    print("     β€’ Tool execution success? β†’ LLM Summarization β†’ Response")
    print()


# Workflow validation
def validate_workflow_state(state: AgentState, node_name: str) -> bool:
    """
    Validate that the state is appropriate for a given node
    
    Args:
        state: Agent state to validate
        node_name: Name of the node about to be executed
        
    Returns:
        True if state is valid for the node, False otherwise
    """
    
    if node_name == "llm_routing":
        return "user_message" in state and state["user_message"]
    
    elif node_name == "tool_execution":
        return "planned_tools" in state and state.get("planned_tools")
    
    elif node_name == "response_generation":
        return "tool_results" in state or "processing_status" in state
    
    elif node_name == "error_handling":
        return "errors" in state and len(state["errors"]) > 0
    
    return True


# Factory functions for different use cases
def create_omirl_focused_workflow() -> Any:
    """
    Create a workflow optimized for OMIRL operations
    
    This workflow assumes most requests are OMIRL-related
    and optimizes the routing accordingly.
    
    Returns:
        Compiled OMIRL-focused workflow
    """
    
    # For now, use the standard workflow
    # Could be customized with different routing logic
    return create_operations_workflow()


def create_debug_workflow() -> Any:
    """
    Create a workflow with extensive debugging and logging
    
    Returns:
        Compiled debug workflow
    """
    
    # For now, use the simple workflow which is easier to debug
    return create_simple_workflow()


# Default workflow factory
def get_default_workflow() -> Any:
    """
    Get the default operations workflow
    
    Returns:
        Default compiled workflow
    """
    return create_operations_workflow()


if __name__ == "__main__":
    # Test workflow compilation
    workflow = create_operations_workflow()
    print_workflow_summary(workflow)
    
    # Test simple workflow
    simple_workflow = create_simple_workflow()
    print("\n" + "="*50)
    print("Simple workflow compiled successfully")