feat : stop button integration

This commit is contained in:
martin legrand
2025-05-20 16:47:35 +02:00
parent 102dc60efb
commit e3d01083d9
12 changed files with 122 additions and 22 deletions
Binary file not shown.

After

Width:  |  Height:  |  Size: 294 KiB

+8
View File
@@ -46,6 +46,7 @@ class Agent():
self.last_answer = ""
self.last_reasoning = ""
self.status_message = "Haven't started yet"
self.stop = False
self.verbose = verbose
self.executor = ThreadPoolExecutor(max_workers=1)
@@ -119,6 +120,13 @@ class Agent():
except Exception as e:
raise e
def request_stop(self) -> None:
"""
Request the agent to stop.
"""
self.stop = True
self.status_message = "Stopped"
@abstractmethod
def process(self, prompt, speech_module) -> str:
"""
+6 -3
View File
@@ -181,6 +181,7 @@ class BrowserAgent(Agent):
animate_thinking("Thinking...", color="status")
self.memory.push('user', prompt)
answer, reasoning = await self.llm_request()
self.last_reasoning = reasoning
if show_reasoning:
pretty_print(reasoning, color="failure")
pretty_print(answer, color="output")
@@ -349,11 +350,13 @@ class BrowserAgent(Agent):
self.show_search_results(search_result)
prompt = self.make_newsearch_prompt(user_prompt, search_result)
unvisited = [None]
while not complete and len(unvisited) > 0:
while not complete and len(unvisited) > 0 and not self.stop:
self.memory.clear()
unvisited = self.select_unvisited(search_result)
answer, reasoning = await self.llm_decide(prompt, show_reasoning = False)
if self.stop:
pretty_print(f"Requested stop.", color="failure")
break
if self.last_answer == answer:
prompt = self.stuck_prompt(user_prompt, unvisited)
continue
@@ -424,4 +427,4 @@ class BrowserAgent(Agent):
return answer, reasoning
if __name__ == "__main__":
pass
pass
+3 -1
View File
@@ -51,10 +51,12 @@ class CoderAgent(Agent):
self.memory.push('user', prompt)
clarify_trigger = "REQUEST_CLARIFICATION"
while attempt < max_attempts:
while attempt < max_attempts and not self.stop:
print("Stopped?", self.stop)
animate_thinking("Thinking...", color="status")
await self.wait_message(speech_module)
answer, reasoning = await self.llm_request()
self.last_reasoning = reasoning
if clarify_trigger in answer:
self.last_answer = answer
await asyncio.sleep(0)
+2 -1
View File
@@ -28,10 +28,11 @@ class FileAgent(Agent):
exec_success = False
prompt += f"\nYou must work in directory: {self.work_dir}"
self.memory.push('user', prompt)
while exec_success is False:
while exec_success is False and not self.stop:
await self.wait_message(speech_module)
animate_thinking("Thinking...", color="status")
answer, reasoning = await self.llm_request()
self.last_reasoning = reasoning
exec_success, _ = self.execute_modules(answer)
answer = self.remove_blocks(answer)
self.last_answer = answer
+18 -7
View File
@@ -83,11 +83,15 @@ class PlannerAgent(Agent):
self.logger.warning(f"Agent {task['agent']} does not exist.")
pretty_print(f"Agent {task['agent']} does not exist.", color="warning")
return []
agent = {
'agent': task['agent'],
'id': task['id'],
'task': task['task']
}
try:
agent = {
'agent': task['agent'],
'id': task['id'],
'task': task['task']
}
except:
self.logger.warning("Missing field in json plan.")
return []
self.logger.info(f"Created agent {task['agent']} with task: {task['task']}")
if 'need' in task:
self.logger.info(f"Agent {task['agent']} was given info:\n {task['need']}")
@@ -156,6 +160,7 @@ class PlannerAgent(Agent):
return []
agents_tasks = self.parse_agent_tasks(answer)
if agents_tasks == []:
self.show_plan(agents_tasks, answer)
prompt = f"Failed to parse the tasks. Please write down your task followed by a json plan within ```json. Do not ask for clarification.\n"
pretty_print("Failed to make plan. Retrying...", color="warning")
continue
@@ -178,7 +183,11 @@ class PlannerAgent(Agent):
last_agent_work = agents_work_result[id]
tool_success_str = "success" if success else "failure"
pretty_print(f"Agent {id} work {tool_success_str}.", color="success" if success else "failure")
if int(id) == len(agents_tasks):
try:
id_int = int(id)
except Exception as e:
return agents_tasks
if id_int == len(agents_tasks):
next_task = "No task follow, this was the last step. If it failed add a task to recover."
else:
next_task = f"Next task is: {agents_tasks[int(id)][0]}."
@@ -258,7 +267,7 @@ class PlannerAgent(Agent):
return "Failed to parse the tasks.", ""
i = 0
steps = len(agents_tasks)
while i < steps:
while i < steps and not self.stop:
task_name, task = agents_tasks[i][0], agents_tasks[i][1]
self.status_message = "Starting agents..."
pretty_print(f"I will {task_name}.", color="info")
@@ -272,6 +281,8 @@ class PlannerAgent(Agent):
answer, success = await self.start_agent_process(task, required_infos)
except Exception as e:
raise e
if self.stop:
pretty_print(f"Requested stop.", color="failure")
agents_work_result[task['id']] = answer
agents_tasks = await self.update_plan(goal, agents_tasks, agents_work_result, task['id'], success)
steps = len(agents_tasks)
+15 -5
View File
@@ -206,7 +206,11 @@ class Browser:
def setup_tabs(self):
self.tabs = self.driver.window_handles
self.driver.get("https://www.google.com")
try:
self.driver.get("https://www.google.com")
except Exception as e:
self.logger.log(f"Failed to setup initial tab:" + str(e))
pass
self.screenshot()
def switch_control_tab(self):
@@ -215,7 +219,11 @@ class Browser:
def load_anticatpcha_manually(self):
pretty_print("You might want to install the AntiCaptcha extension for captchas.", color="warning")
self.driver.get(self.anticaptcha)
try:
self.driver.get(self.anticaptcha)
except Exception as e:
self.logger.log(f"Failed to setup initial tab:" + str(e))
pass
def human_move(element):
actions = ActionChains(driver)
@@ -685,15 +693,17 @@ class Browser:
input_elements = self.driver.execute_script(script)
if __name__ == "__main__":
driver = create_driver(headless=False, stealth_mode=True)
browser = Browser(driver, anticaptcha_manual_install=False)
driver = create_driver(headless=False, stealth_mode=True, crx_path="../crx/nopecha.crx")
browser = Browser(driver, anticaptcha_manual_install=True)
input("press enter to continue")
print("AntiCaptcha / Form Test")
browser.go_to("https://www.biorxiv.org/content/10.1101/2025.05.19.654955v1")
time.sleep(55)
browser.go_to("https://www.google.com/recaptcha/api2/demo")
browser.go_to("https://bot.sannysoft.com")
time.sleep(5)
#txt = browser.get_text()
#browser.go_to("https://www.google.com/recaptcha/api2/demo")
browser.go_to("https://home.openweathermap.org/users/sign_up")
inputs_visible = browser.get_form_inputs()
print("inputs:", inputs_visible)
+35 -2
View File
@@ -32,11 +32,12 @@ class Provider:
"deepseek": self.deepseek_fn,
"together": self.together_fn,
"dsk_deepseek": self.dsk_deepseek,
"test": self.test_fn
"test": self.test_fn,
"anthropic": self.anthropic_fn
}
self.logger = Logger("provider.log")
self.api_key = None
self.unsafe_providers = ["openai", "deepseek", "dsk_deepseek", "together", "google"]
self.unsafe_providers = ["openai", "deepseek", "dsk_deepseek", "together", "google", "anthropic"]
if self.provider_name not in self.available_providers:
raise ValueError(f"Unknown provider: {provider_name}")
if self.provider_name in self.unsafe_providers and self.is_local == False:
@@ -57,6 +58,38 @@ class Provider:
exit(1)
return api_key
def anthropic_fn(self, history, verbose=False):
"""
Use Anthropic to generate text.
"""
from anthropic import Anthropic
client = Anthropic(api_key=self.api_key)
system_message = None
messages = []
for message in history:
clean_message = {'role': message['role'], 'content': message['content']}
if message['role'] == 'system':
system_message = message['content']
else:
messages.append(clean_message)
try:
response = client.messages.create(
model=self.model,
max_tokens=1024,
messages=messages,
system=system_message
)
if response is None:
raise Exception("Anthropic response is empty.")
thought = response.content[0].text
if verbose:
print(thought)
return thought
except Exception as e:
raise Exception(f"Anthropic API error: {str(e)}") from e
def respond(self, history, verbose=True):
"""
Use the choosen provider to generate text.
+4 -3
View File
@@ -8,7 +8,7 @@ if __name__ == "__main__": # if running as a script for individual testing
sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))
from sources.tools.tools import Tools
from sources.tools.safety import is_unsafe
from sources.tools.safety import is_any_unsafe
class BashInterpreter(Tools):
"""
@@ -43,9 +43,9 @@ class BashInterpreter(Tools):
for command in commands:
command = f"cd {self.work_dir} && {command}"
command = command.replace('\n', '')
if self.safe_mode and is_unsafe(commands):
if self.safe_mode and is_any_unsafe(commands):
print(f"Unsafe command rejected: {command}")
return "Unsafe command detected, execution aborted."
return "\nUnsafe command detected, execution aborted."
if self.language_bash_attempt(command) and self.allow_language_exec_bash == False:
continue
try:
@@ -100,6 +100,7 @@ class BashInterpreter(Tools):
r"not permitted",
r"not installed",
r"not found",
r"aborted",
r"no such",
r"too many",
r"too few",
+9
View File
@@ -66,6 +66,15 @@ unsafe_commands_windows = [
"bootcfg"
]
def is_any_unsafe(cmds):
"""
check if any bash command is unsafe.
"""
for cmd in cmds:
if is_unsafe(cmd):
return True
return False
def is_unsafe(cmd):
"""
check if a bash command is unsafe.