55from app .agents .nodes import (
66 agent_host ,
77 context_builder ,
8- fallback ,
9- guard ,
8+ fallback_final ,
9+ fallback_inicial ,
10+ generator ,
11+ guard_final ,
12+ guard_inicial ,
1013 parafraseo ,
1114 retriever ,
1215)
16+ from app .agents .routing import (
17+ route_after_guard_final ,
18+ route_after_guard_inicial ,
19+ )
1320from app .agents .state import AgentState
1421from app .agents .routing import route_after_guard
1522
@@ -18,15 +25,18 @@ def create_agent_graph() -> StateGraph:
1825 Create and configure the LangGraph agent graph.
1926
2027 The graph implements the following flow:
21- 1. START -> agent_host (Nodo 1)
22- 2. agent_host -> guard (Nodo 2)
23- 3. guard -> [conditional] -> fallback (Nodo 3) or END
24- 4. fallback -> parafraseo (Nodo 4)
28+ 1. START -> agent_host (Nodo 1) - Prepares state, no DB operations
29+ 2. agent_host -> guard (Nodo 2) - Validates for malicious content
30+ 3. guard -> [conditional]:
31+ - malicious -> fallback -> END (stops processing, no DB save)
32+ - continue -> parafraseo (Nodo 4)
33+ 4. parafraseo -> Saves message to DB, retrieves chat history, paraphrases
2534 5. parafraseo -> retriever (Nodo 5)
2635 6. retriever -> context_builder (Nodo 6)
27- 7. context_builder -> generator (Nodo 7)
28- 8. generator -> fallback (Nodo 8)
29- 9. fallback -> [conditional] -> END (with final_response) or END (with error)
36+ 7. context_builder -> guard (validates response)
37+ 8. guard -> [conditional]:
38+ - malicious -> fallback -> END
39+ - continue -> END (success)
3040
3141 Returns:
3242 Configured StateGraph instance ready for execution
@@ -36,29 +46,35 @@ def create_agent_graph() -> StateGraph:
3646
3747 # Add nodes
3848 workflow .add_node ("agent_host" , agent_host )
39- workflow .add_node ("guard " , guard )
40- workflow .add_node ("fallback " , fallback )
49+ workflow .add_node ("guard_inicial " , guard_inicial )
50+ workflow .add_node ("fallback_inicial " , fallback_inicial )
4151 workflow .add_node ("parafraseo" , parafraseo )
4252 workflow .add_node ("retriever" , retriever )
4353 workflow .add_node ("context_builder" , context_builder )
54+ workflow .add_node ("generator" , generator )
55+ workflow .add_node ("guard_final" , guard_final )
56+ workflow .add_node ("fallback_final" , fallback_final )
4457
4558 # Define edges
4659 # Start -> agent_host
4760 workflow .add_edge (START , "agent_host" )
4861
49- # agent_host -> guard
50- workflow .add_edge ("agent_host" , "guard " )
62+ # agent_host -> guard_inicial
63+ workflow .add_edge ("agent_host" , "guard_inicial " )
5164
52- # guard -> conditional routing
65+ # guard_inicial -> conditional routing
5366 workflow .add_conditional_edges (
54- "guard " ,
55- route_after_guard ,
67+ "guard_inicial " ,
68+ route_after_guard_inicial ,
5669 {
57- "malicious" : "fallback " , # go to fallback if malicious
58- "continue" : "parafraseo" , # Continue to parafraseo if valid
70+ "malicious" : "fallback_inicial " , # Exception path: malicious content detected
71+ "continue" : "parafraseo" , # Normal path: continue processing
5972 },
6073 )
6174
75+ # fallback_inicial -> END (stop flow with error message)
76+ workflow .add_edge ("fallback_inicial" , END )
77+
6278 # parafraseo -> retriever
6379 workflow .add_edge ("parafraseo" , "retriever" )
6480
@@ -68,15 +84,21 @@ def create_agent_graph() -> StateGraph:
6884 # context_builder -> guard
6985 workflow .add_edge ("context_builder" , "guard" )
7086
71- # guard -> conditional routing
87+ # generator -> guard_final
88+ workflow .add_edge ("generator" , "guard_final" )
89+
90+ # guard_final -> conditional routing
7291 workflow .add_conditional_edges (
73- "guard " ,
74- route_after_guard ,
92+ "guard_final " ,
93+ route_after_guard_final ,
7594 {
76- "malicious " : "fallback " , # go to fallback if malicious
77- "continue" : END , # if there's no error ends
95+ "risky " : "fallback_final " , # Exception path: risky content detected
96+ "continue" : END , # Normal path: end successfully
7897 },
7998 )
80- workflow .add_edge ("fallback" , END )
99+
100+ # fallback_final -> END (stop flow with error message)
101+ workflow .add_edge ("fallback_final" , END )
102+
81103 # Compile the graph
82104 return workflow .compile ()
0 commit comments