-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph.py
More file actions
57 lines (41 loc) · 1.24 KB
/
graph.py
File metadata and controls
57 lines (41 loc) · 1.24 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
from typing import List, Optional
from typing_extensions import TypedDict
from langgraph.graph import StateGraph, END
from agents.planner import planner
from agents.analyst import analyst
from agents.writer import writer
from agents.tool_agent import tool_agent
from runtime.tracer import Tracer
class State(TypedDict, total=False):
# 输入 / 上下文
user_input: str
persona: object
goal_store: object
memories: List[str]
dreams: List[str]
# 规划 / 工具 / 分析 / 回复
current_goal: str
plan: str
plan_ok: bool
tool_result: Optional[str]
analysis: str
reply: str
tracer = Tracer()
def wrap(name, func):
"""包装每个节点函数,执行后把 state 记录到 tracer。"""
def _wrapped(state: State) -> State:
out = func(state)
tracer.record(name, out)
return out
return _wrapped
g = StateGraph(State)
g.add_node("planner", wrap("planner", planner))
g.add_node("tool", wrap("tool", tool_agent))
g.add_node("analyst", wrap("analyst", analyst))
g.add_node("writer", wrap("writer", writer))
g.set_entry_point("planner")
g.add_edge("planner", "tool")
g.add_edge("tool", "analyst")
g.add_edge("analyst", "writer")
g.add_edge("writer", END)
app = g.compile()