Skip to content

Commit 0ab7cb8

Browse files
authored
Merge pull request #105 from MeshkatShB/feature/save-agent-history
Feature/save agent history
2 parents 761fa42 + 845d5dd commit 0ab7cb8

File tree

2 files changed

+47
-13
lines changed

2 files changed

+47
-13
lines changed

.gitignore

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,4 +177,10 @@ cookies.json
177177
AgentHistory.json
178178
cv_04_24.pdf
179179
AgentHistoryList.json
180-
*.gif
180+
*.gif
181+
182+
# For Sharing (.pem files)
183+
.gradio/
184+
185+
# For Docker
186+
data/

webui.py

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ async def run_browser_agent(
9292
window_w,
9393
window_h,
9494
save_recording_path,
95+
save_agent_history_path,
9596
save_trace_path,
9697
enable_recording,
9798
task,
@@ -130,7 +131,7 @@ async def run_browser_agent(
130131
api_key=llm_api_key,
131132
)
132133
if agent_type == "org":
133-
final_result, errors, model_actions, model_thoughts, trace_file = await run_org_agent(
134+
final_result, errors, model_actions, model_thoughts, trace_file, history_file = await run_org_agent(
134135
llm=llm,
135136
use_own_browser=use_own_browser,
136137
keep_browser_open=keep_browser_open,
@@ -139,6 +140,7 @@ async def run_browser_agent(
139140
window_w=window_w,
140141
window_h=window_h,
141142
save_recording_path=save_recording_path,
143+
save_agent_history_path=save_agent_history_path,
142144
save_trace_path=save_trace_path,
143145
task=task,
144146
max_steps=max_steps,
@@ -147,7 +149,7 @@ async def run_browser_agent(
147149
tool_call_in_content=tool_call_in_content
148150
)
149151
elif agent_type == "custom":
150-
final_result, errors, model_actions, model_thoughts, trace_file = await run_custom_agent(
152+
final_result, errors, model_actions, model_thoughts, trace_file, history_file = await run_custom_agent(
151153
llm=llm,
152154
use_own_browser=use_own_browser,
153155
keep_browser_open=keep_browser_open,
@@ -156,6 +158,7 @@ async def run_browser_agent(
156158
window_w=window_w,
157159
window_h=window_h,
158160
save_recording_path=save_recording_path,
161+
save_agent_history_path=save_agent_history_path,
159162
save_trace_path=save_trace_path,
160163
task=task,
161164
add_infos=add_infos,
@@ -184,6 +187,7 @@ async def run_browser_agent(
184187
model_thoughts,
185188
latest_video,
186189
trace_file,
190+
history_file,
187191
gr.update(value="Stop", interactive=True), # Re-enable stop button
188192
gr.update(value="Run", interactive=True) # Re-enable run button
189193
)
@@ -198,6 +202,7 @@ async def run_browser_agent(
198202
'', # model_actions
199203
'', # model_thoughts
200204
None, # latest_video
205+
None, # history_file
201206
None, # trace_file
202207
gr.update(value="Stop", interactive=True), # Re-enable stop button
203208
gr.update(value="Run", interactive=True) # Re-enable run button
@@ -213,6 +218,7 @@ async def run_org_agent(
213218
window_w,
214219
window_h,
215220
save_recording_path,
221+
save_agent_history_path,
216222
save_trace_path,
217223
task,
218224
max_steps,
@@ -266,14 +272,17 @@ async def run_org_agent(
266272
)
267273
history = await agent.run(max_steps=max_steps)
268274

275+
history_file = os.path.join(save_agent_history_path, f"{agent.agent_id}.json")
276+
agent.save_history(history_file)
277+
269278
final_result = history.final_result()
270279
errors = history.errors()
271280
model_actions = history.model_actions()
272281
model_thoughts = history.model_thoughts()
273-
282+
274283
trace_file = get_latest_files(save_trace_path)
275-
276-
return final_result, errors, model_actions, model_thoughts, trace_file.get('.zip')
284+
285+
return final_result, errors, model_actions, model_thoughts, trace_file.get('.zip'), history_file
277286
except Exception as e:
278287
import traceback
279288
traceback.print_exc()
@@ -299,6 +308,7 @@ async def run_custom_agent(
299308
window_w,
300309
window_h,
301310
save_recording_path,
311+
save_agent_history_path,
302312
save_trace_path,
303313
task,
304314
add_infos,
@@ -361,19 +371,22 @@ async def run_custom_agent(
361371
)
362372
history = await agent.run(max_steps=max_steps)
363373

374+
history_file = os.path.join(save_agent_history_path, f"{agent.agent_id}.json")
375+
agent.save_history(history_file)
376+
364377
final_result = history.final_result()
365378
errors = history.errors()
366379
model_actions = history.model_actions()
367380
model_thoughts = history.model_thoughts()
368381

369382
trace_file = get_latest_files(save_trace_path)
370383

371-
return final_result, errors, model_actions, model_thoughts, trace_file.get('.zip')
384+
return final_result, errors, model_actions, model_thoughts, trace_file.get('.zip'), history_file
372385
except Exception as e:
373386
import traceback
374387
traceback.print_exc()
375388
errors = str(e) + "\n" + traceback.format_exc()
376-
return '', errors, '', '', None
389+
return '', errors, '', '', None, None
377390
finally:
378391
# Handle cleanup based on persistence configuration
379392
if not keep_browser_open:
@@ -399,6 +412,7 @@ async def run_with_stream(
399412
window_w,
400413
window_h,
401414
save_recording_path,
415+
save_agent_history_path,
402416
save_trace_path,
403417
enable_recording,
404418
task,
@@ -425,6 +439,7 @@ async def run_with_stream(
425439
window_w=window_w,
426440
window_h=window_h,
427441
save_recording_path=save_recording_path,
442+
save_agent_history_path=save_agent_history_path,
428443
save_trace_path=save_trace_path,
429444
enable_recording=enable_recording,
430445
task=task,
@@ -455,6 +470,7 @@ async def run_with_stream(
455470
window_w=window_w,
456471
window_h=window_h,
457472
save_recording_path=save_recording_path,
473+
save_agent_history_path=save_agent_history_path,
458474
save_trace_path=save_trace_path,
459475
enable_recording=enable_recording,
460476
task=task,
@@ -725,6 +741,14 @@ def create_ui(theme_name="Ocean"):
725741
interactive=True,
726742
)
727743

744+
save_agent_history_path = gr.Textbox(
745+
label="Agent History Save Path",
746+
placeholder="e.g., ./tmp/agent_history",
747+
value="./tmp/agent_history",
748+
info="Specify the directory where agent history should be saved.",
749+
interactive=True,
750+
)
751+
728752
with gr.TabItem("🤖 Run Agent", id=4):
729753
task = gr.Textbox(
730754
label="Task Description",
@@ -777,6 +801,8 @@ def create_ui(theme_name="Ocean"):
777801

778802
trace_file = gr.File(label="Trace File")
779803

804+
agent_history_file = gr.File(label="Agent History")
805+
780806
# Bind the stop button click event after errors_output is defined
781807
stop_button.click(
782808
fn=stop_agent,
@@ -787,11 +813,12 @@ def create_ui(theme_name="Ocean"):
787813
# Run button click handler
788814
run_button.click(
789815
fn=run_with_stream,
790-
inputs=[
791-
agent_type, llm_provider, llm_model_name, llm_temperature, llm_base_url, llm_api_key,
792-
use_own_browser, keep_browser_open, headless, disable_security, window_w, window_h, save_recording_path, save_trace_path,
793-
enable_recording, task, add_infos, max_steps, use_vision, max_actions_per_step, tool_call_in_content
794-
],
816+
inputs=[
817+
agent_type, llm_provider, llm_model_name, llm_temperature, llm_base_url, llm_api_key,
818+
use_own_browser, keep_browser_open, headless, disable_security, window_w, window_h,
819+
save_recording_path, save_agent_history_path, save_trace_path, # Include the new path
820+
enable_recording, task, add_infos, max_steps, use_vision, max_actions_per_step, tool_call_in_content
821+
],
795822
outputs=[
796823
browser_view, # Browser view
797824
final_result_output, # Final result
@@ -800,6 +827,7 @@ def create_ui(theme_name="Ocean"):
800827
model_thoughts_output, # Model thoughts
801828
recording_display, # Latest recording
802829
trace_file, # Trace file
830+
agent_history_file, # Agent history file
803831
stop_button, # Stop button
804832
run_button # Run button
805833
],

0 commit comments

Comments
 (0)