-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent_runner.py
More file actions
205 lines (168 loc) · 7.7 KB
/
Copy pathagent_runner.py
File metadata and controls
205 lines (168 loc) · 7.7 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
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
from __future__ import annotations
import copy
import threading
from typing import List, Optional
from app.client import ApiClient
from src.agent.multiturn_agent import MultiTurnAgent
from src.tools.helper import Config
from src.physicalop import auto_parse_op
from src.module.executor import Executor
from chatapp.operator_tree import OperatorTree
from chatapp.state import InMemoryStore, TrialEventHub
def _split_chain(chain: str) -> List[str]:
return [x.strip() for x in chain.split("-->") if x.strip()]
class InteractiveMultiTurnRunner:
"""Runs MultiTurnAgent turn-by-turn and publishes events for UI."""
def __init__(
self,
*,
cfg: dict,
store: InMemoryStore,
hub: TrialEventHub,
trial_id: str,
):
self.cfg = cfg
self.store = store
self.hub = hub
self.trial_id = trial_id
self.tree = OperatorTree()
# MultiTurnAgent internally creates ApiClient(), which will use DS_AGENT_API_BASE_URL
self.agent = MultiTurnAgent(cfg=cfg, log_file=f"CHATAPP_{trial_id}")
def _publish_tree(self):
self.hub.publish(self.trial_id, {"type": "tree", "tree": self.tree.to_dict()})
def _publish_chat(self, role: str, content: str):
self.hub.publish(self.trial_id, {"type": "chat", "role": role, "content": content})
def _publish_status(self, status: str):
self.hub.publish(self.trial_id, {"type": "status", "status": status})
def _enrich_chain_via_simulate(self, trial_id: str, ops: List[str]) -> List[dict]:
"""Re-run /simulate to collect per-op outputs (table name + preview).
MultiTurnAgent internally calls /simulate but only returns text observations.
Here we call it again to get structured metadata used by the operator tree UI.
"""
try:
hist = self.agent.client.simulate_trial(
trial_id=trial_id,
operators=ops,
mode=self.cfg.get("execute_mode", "rule"),
)
# ApiClient implementations vary: some return a list of rows, others return {"history": [...]}.
rows = hist
if isinstance(hist, dict):
rows = hist.get("history")
if not isinstance(rows, list):
rows = []
# Ensure the minimum keys exist
steps: List[dict] = []
for r in rows:
if not isinstance(r, dict):
continue
steps.append(
{
"op": r.get("op"),
"out_table": r.get("out_table"),
"out_preview": r.get("out_preview"),
}
)
# If simulate didn't return anything, fall back to bare ops.
return steps if steps else [{"op": x} for x in ops]
except Exception:
return [{"op": x} for x in ops]
def _execute_solution_locally(self, solution: str):
state = self.store.get_trial(self.trial_id)
base_trial = state.trial
tmp = copy.deepcopy(base_trial)
executor = Executor(cfg=Config.load_current_config(), debug=False, log_file=f"CHATAPP_{self.trial_id}")
last_df = None
last_table_name = None
for op_str in _split_chain(solution):
op = auto_parse_op(op_str)
out_name, out_df = executor.execute_op(op, tmp, mode=self.cfg.get("execute_mode", "rule"))
executor.step_op(op, tmp, out_name, out_df)
last_df = out_df
last_table_name = out_name
state.result_df = last_df
state.result_table_name = last_table_name
def run(self):
state = self.store.get_trial(self.trial_id)
trial = state.trial
messages = []
try:
max_turn = int(self.cfg.get("max_explore_turn", 5) or 5)
except Exception:
max_turn = 5
self._publish_status("running")
self._publish_chat("assistant", "Agent started. Generating operator chains...")
self._publish_tree()
final_solution: Optional[str] = None
for i in range(max_turn + 1):
cur_turn = i + 1
self.agent._clear_state()
while True:
try:
# Let UI know we are actively waiting on the LLM for this turn.
self._publish_status(f"thinking (turn {cur_turn})")
think, operator_chain, solution, obs, messages = self.agent._multiturn_step(trial, cur_turn, messages)
trial.record("think", think)
trial.record("operator", operator_chain)
trial.record("solution", solution)
trial.record("observation", obs)
# Back to running once we have a response.
self._publish_status("running")
break
except Exception as e:
# MultiTurnAgent internally retries on some HTTP errors; we keep it simple here.
self.agent._raise_error(str(e))
continue
# Emit assistant content
if think:
self._publish_chat("assistant", f"<think> {think} </think>")
if operator_chain:
ops = _split_chain(operator_chain)
steps = self._enrich_chain_via_simulate(trial.exp_id, ops)
for s in steps:
s["turn"] = cur_turn
self.tree.add_chain(steps, is_solution=False)
self._publish_chat("assistant", f"<operator> {operator_chain} </operator>")
self._publish_tree()
if solution:
final_solution = solution
ops = _split_chain(solution)
steps = self._enrich_chain_via_simulate(trial.exp_id, ops)
for s in steps:
s["turn"] = cur_turn
path_ids = self.tree.add_chain(steps, is_solution=True)
self._publish_chat("assistant", f"<solution> {solution} </solution>")
self._publish_tree()
self.hub.publish(self.trial_id, {"type": "highlight", "path": path_ids})
break
if not final_solution:
self._publish_status("failed")
self._publish_chat("assistant", f"Agent failed to generate a solution within {max_turn} turns.")
return
# Execute to produce target table
try:
self._publish_status("executing")
self._execute_solution_locally(final_solution)
state = self.store.get_trial(self.trial_id)
preview = {
"columns": list(state.result_df.columns.astype(str))[:50] if state.result_df is not None else [],
"rows": state.result_df.head(20).fillna("").astype(str).values.tolist() if state.result_df is not None else [],
"shape": [int(state.result_df.shape[0]), int(state.result_df.shape[1])] if state.result_df is not None else [0, 0],
}
self.hub.publish(
self.trial_id,
{
"type": "result",
"tableName": state.result_table_name,
"preview": preview,
},
)
self._publish_status("done")
except Exception as e:
self._publish_status("failed")
self._publish_chat("assistant", f"Failed to execute final solution: {e}")
def start_runner_in_background(*, cfg: dict, store: InMemoryStore, hub: TrialEventHub, trial_id: str) -> threading.Thread:
runner = InteractiveMultiTurnRunner(cfg=cfg, store=store, hub=hub, trial_id=trial_id)
t = threading.Thread(target=runner.run, daemon=True)
t.start()
return t