Repository navigation
Expand file tree
/
Copy pathgraph.py
More file actions
75 lines (64 loc) · 2.07 KB
/
Copy pathgraph.py
File metadata and controls
75 lines (64 loc) · 2.07 KB
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
import logging
import config
from langgraph.graph import StateGraph, END
from state import AgentState
from processor.processor import process
from decision.decision import is_relevant
from analysis.analyzer import analyze_node
from discovery.discoverer import discovery_node
from notifier.notifier import notify_node
logger = logging.getLogger(__name__)
def process_node(state: AgentState):
"""Wraps existing processor logic."""
item = {
"title": state.get("title"),
"content": state.get("text"),
"link": state.get("link")
}
processed = process(item)
return {
"normalized_content": processed.get("normalized_content")
}
def decide_node(state: AgentState):
"""Wraps existing decision logic."""
text = state.get("normalized_content") or state.get("text")
is_rel, summary = is_relevant(text, config.KEYWORDS)
return {
"is_relevant": is_rel,
"summary": summary
}
def should_analyze(state: AgentState):
"""Conditional logic for the graph."""
if state.get("is_relevant"):
return "analyze"
return END
def create_graph():
"""
Creates and compiles the LangGraph for the monitoring agent.
Flow: process -> decide -> (if relevant: analyze -> discover -> notify) -> END
"""
workflow = StateGraph(AgentState)
# Add Nodes
workflow.add_node("process", process_node)
workflow.add_node("decide", decide_node)
workflow.add_node("analyze", analyze_node)
workflow.add_node("discover", discovery_node)
workflow.add_node("notify", notify_node)
# Build Edges
workflow.set_entry_point("process")
workflow.add_edge("process", "decide")
# Conditional Edge
workflow.add_conditional_edges(
"decide",
should_analyze,
{
"analyze": "analyze",
END: END
}
)
workflow.add_edge("analyze", "discover")
workflow.add_edge("discover", "notify")
workflow.add_edge("notify", END)
return workflow.compile()
# Singleton instance of the compiled graph
agent_graph = create_graph()