1+ from fenn .agents import Node
2+ from fenn .agents .tools import TOOLS
3+
4+ class ThinkNode (Node ):
5+ def prep (self , shared ):
6+ return {"llm" : shared ["llm" ], "messages" : shared ["messages" ]}
7+
8+ def exec (self , prep_res ):
9+ llm = prep_res ["llm" ]
10+ response = llm .chat_complete (prep_res ["messages" ])
11+ return response
12+
13+ def post (self , shared , prep_res , exec_res ):
14+ shared ["last_thought" ] = exec_res
15+ shared ["messages" ].append ({"role" : "assistant" , "content" : exec_res })
16+ if "Action:" in exec_res :
17+ return "act"
18+ return "done"
19+
20+ class ActNode (Node ):
21+ def prep (self , shared ):
22+ return shared ["last_thought" ]
23+
24+ def exec (self , thought ):
25+ line = [l for l in thought .split ("\n " ) if l .startswith ("Action:" )][0 ]
26+ tool_call = line .replace ("Action:" , "" ).strip ()
27+
28+ tool_name = tool_call .split ("(" )[0 ]
29+ tool_arg = tool_call .split ("(" )[1 ].rstrip (")" )
30+
31+ result = TOOLS [tool_name ](tool_arg )
32+ return result
33+
34+ def post (self , shared , prep_res , exec_res ):
35+ shared ["last_observation" ] = exec_res
36+ return "observe"
37+
38+ class ObserveNode (Node ):
39+ def prep (self , shared ):
40+ return shared ["last_observation" ]
41+
42+ def exec (self , observation ):
43+ return observation
44+
45+ def post (self , shared , prep_res , exec_res ):
46+ shared ["messages" ].append ({
47+ "role" : "user" ,
48+ "content" : f"Observation: { exec_res } "
49+ })
50+ shared ["iterations" ] += 1
51+ if shared ["iterations" ] >= shared ["max_iterations" ]:
52+ return "done"
53+ return "think"
0 commit comments