feat : small frontend improvement & planner auto fix
This commit is contained in:
@@ -76,8 +76,7 @@ class PlannerAgent(Agent):
|
||||
"""
|
||||
return prompt
|
||||
|
||||
def show_plan(self, answer: dict) -> None:
|
||||
agents_tasks = self.parse_agent_tasks(answer)
|
||||
def show_plan(self, agents_tasks: dict, answer: str) -> None:
|
||||
if agents_tasks == (None, None):
|
||||
pretty_print(answer, color="warning")
|
||||
pretty_print("Failed to make a plan. This can happen with (too) small LLM. Clarify your request and insist on it making a plan within ```json.", color="failure")
|
||||
@@ -94,12 +93,13 @@ class PlannerAgent(Agent):
|
||||
animate_thinking("Thinking...", color="status")
|
||||
self.memory.push('user', prompt)
|
||||
answer, _ = self.llm_request()
|
||||
self.show_plan(answer)
|
||||
ok_str = input("Is the plan ok? (y/n): ")
|
||||
if ok_str == 'y':
|
||||
ok = True
|
||||
else:
|
||||
prompt = input("Please reformulate: ")
|
||||
agents_tasks = self.parse_agent_tasks(answer)
|
||||
if agents_tasks == (None, None):
|
||||
prompt = f"Failed to parse the tasks. Please make a plan within ```json.\n"
|
||||
pretty_print("Failed to make plan. Retrying...", color="warning")
|
||||
continue
|
||||
self.show_plan(agents_tasks, answer)
|
||||
ok = True
|
||||
return answer
|
||||
|
||||
def start_agent_process(self, task: str, required_infos: dict | None) -> str:
|
||||
|
||||
+26
-6
@@ -123,16 +123,25 @@ class Browser:
|
||||
self.js_scripts_folder = "./sources/web_scripts/" if not __name__ == "__main__" else "./web_scripts/"
|
||||
self.anticaptcha = "https://chrome.google.com/webstore/detail/nopecha-captcha-solver/dknlfmjaanfblgfdfebhijalfmhmjjjo/related"
|
||||
self.logger = Logger("browser.log")
|
||||
self.screenshot_folder = os.path.join(os.getcwd(), ".screenshots")
|
||||
self.tabs = []
|
||||
try:
|
||||
self.driver = driver
|
||||
self.wait = WebDriverWait(self.driver, 10)
|
||||
except Exception as e:
|
||||
raise Exception(f"Failed to initialize browser: {str(e)}")
|
||||
self.screenshot_folder = os.path.join(os.getcwd(), ".screenshots")
|
||||
self.screenshot()
|
||||
self.driver.get("https://www.google.com")
|
||||
self.setup_tabs()
|
||||
if anticaptcha_manual_install:
|
||||
self.load_anticatpcha_manually()
|
||||
|
||||
def setup_tabs(self):
|
||||
self.tabs = self.driver.window_handles
|
||||
self.driver.get("https://www.google.com")
|
||||
self.screenshot()
|
||||
|
||||
def switch_control_tab(self):
|
||||
self.logger.log("Switching to control tab.")
|
||||
self.driver.switch_to.window(self.tabs[0])
|
||||
|
||||
def load_anticatpcha_manually(self):
|
||||
pretty_print("You might want to install the AntiCaptcha extension for captchas.", color="warning")
|
||||
@@ -154,6 +163,7 @@ class Browser:
|
||||
)
|
||||
self.apply_web_safety()
|
||||
self.logger.log(f"Navigated to: {url}")
|
||||
self.logger.info(f"Navigated to: {self.get_page_title()}")
|
||||
self.screenshot()
|
||||
return True
|
||||
except TimeoutException as e:
|
||||
@@ -201,6 +211,8 @@ class Browser:
|
||||
lines.append(cleaned)
|
||||
result = "[Start of page]\n\n" + "\n\n".join(lines) + "\n\n[End of page]"
|
||||
result = re.sub(r'!\[(.*?)\]\(.*?\)', r'[IMAGE: \1]', result)
|
||||
self.logger.info(f"Extracted text: {result[:100]}...")
|
||||
self.logger.info(f"Extracted text length: {len(result)}")
|
||||
return result[:8192]
|
||||
except Exception as e:
|
||||
self.logger.error(f"Error getting text: {str(e)}")
|
||||
@@ -226,9 +238,11 @@ class Browser:
|
||||
def is_link_valid(self, url:str) -> bool:
|
||||
"""Check if a URL is a valid link (page, not related to icon or metadata)."""
|
||||
if len(url) > 64:
|
||||
self.logger.warning(f"URL too long: {url}")
|
||||
return False
|
||||
parsed_url = urlparse(url)
|
||||
if not parsed_url.scheme or not parsed_url.netloc:
|
||||
self.logger.warning(f"Invalid URL: {url}")
|
||||
return False
|
||||
if re.search(r'/\d+$', parsed_url.path):
|
||||
return False
|
||||
@@ -360,6 +374,7 @@ class Browser:
|
||||
Wait for a submission outcome (e.g., URL change or new element).
|
||||
"""
|
||||
try:
|
||||
self.logger.info("Waiting for submission outcome...")
|
||||
wait = WebDriverWait(self.driver, timeout)
|
||||
wait.until(
|
||||
lambda driver: driver.current_url != self.driver.current_url or
|
||||
@@ -387,8 +402,10 @@ class Browser:
|
||||
message=f"Button with XPath '{xpath}' not clickable within {timeout} seconds"
|
||||
)
|
||||
if self.click_element(xpath):
|
||||
self.logger.info(f"Clicked button '{button_text}' at XPath: {xpath}")
|
||||
return True
|
||||
else:
|
||||
self.logger.warning(f"Button '{button_text}' at XPath: {xpath} not clickable")
|
||||
return False
|
||||
except TimeoutException:
|
||||
self.logger.warning(f"Timeout waiting for '{button_text}' button at XPath: {xpath}")
|
||||
@@ -424,9 +441,9 @@ class Browser:
|
||||
self.logger.info(f"Ticked checkbox {index}")
|
||||
except ElementClickInterceptedException:
|
||||
self.driver.execute_script("arguments[0].click();", checkbox)
|
||||
self.logger.info(f"Ticked checkbox {index} using JavaScript")
|
||||
self.logger.warning(f"Click checkbox {index} intercepted")
|
||||
else:
|
||||
self.logger.debug(f"Checkbox {index} already ticked")
|
||||
self.logger.info(f"Checkbox {index} already ticked")
|
||||
except TimeoutException:
|
||||
self.logger.warning(f"Timeout waiting for checkbox {index} to be clickable")
|
||||
continue
|
||||
@@ -534,6 +551,7 @@ class Browser:
|
||||
def scroll_bottom(self) -> bool:
|
||||
"""Scroll to the bottom of the page."""
|
||||
try:
|
||||
self.logger.info("Scrolling to the bottom of the page...")
|
||||
self.driver.execute_script(
|
||||
"window.scrollTo(0, document.body.scrollHeight);"
|
||||
)
|
||||
@@ -549,6 +567,7 @@ class Browser:
|
||||
|
||||
def screenshot(self, filename:str = 'updated_screen.png') -> bool:
|
||||
"""Take a screenshot of the current page."""
|
||||
self.logger.info("Taking screenshot...")
|
||||
try:
|
||||
path = os.path.join(self.screenshot_folder, filename)
|
||||
if not os.path.exists(self.screenshot_folder):
|
||||
@@ -564,11 +583,12 @@ class Browser:
|
||||
"""
|
||||
Apply security measures to block any website malicious/annoying execution, privacy violation etc..
|
||||
"""
|
||||
self.logger.info("Applying web safety measures...")
|
||||
script = self.load_js("inject_safety_script.js")
|
||||
input_elements = self.driver.execute_script(script)
|
||||
|
||||
if __name__ == "__main__":
|
||||
driver = create_driver(headless=True, stealth_mode=True)
|
||||
driver = create_driver(headless=False, stealth_mode=True)
|
||||
browser = Browser(driver, anticaptcha_manual_install=True)
|
||||
|
||||
#browser.go_to("https://github.com/Fosowl/agenticSeek")
|
||||
|
||||
+1
-4
@@ -1,12 +1,11 @@
|
||||
|
||||
from typing import Tuple, Callable
|
||||
from pydantic import BaseModel
|
||||
from sources.utility import pretty_print
|
||||
|
||||
class QueryRequest(BaseModel):
|
||||
query: str
|
||||
lang: str = "en"
|
||||
tts_enabled: bool = True
|
||||
stt_enabled: bool = False
|
||||
|
||||
def __str__(self):
|
||||
return f"Query: {self.query}, Language: {self.lang}, TTS: {self.tts_enabled}, STT: {self.stt_enabled}"
|
||||
@@ -14,9 +13,7 @@ class QueryRequest(BaseModel):
|
||||
def jsonify(self):
|
||||
return {
|
||||
"query": self.query,
|
||||
"lang": self.lang,
|
||||
"tts_enabled": self.tts_enabled,
|
||||
"stt_enabled": self.stt_enabled
|
||||
}
|
||||
|
||||
class QueryResponse(BaseModel):
|
||||
|
||||
Reference in New Issue
Block a user