Skip to content

armgit / camel

Public Imported from GitHub
0

🐫 CAMEL: The first and the best multi-agent framework. Finding the Scaling Law of Agents. https://www.camel-ai.org

feat: Enhanced BrowserToolkit (#1845)

Sun Tao6b0361387695
diff --git a/camel/toolkits/browser_toolkit.py b/camel/toolkits/browser_toolkit.py
index 54dd0db2f..27ada10a2 100644
--- a/camel/toolkits/browser_toolkit.py
+++ b/camel/toolkits/browser_toolkit.py
@@ -1049,7 +1049,7 @@ check the history actions to avoid the same mistakes.
- `action_code`: The action code you want to take. It is only one step action
code, without any other texts (such as annotation)
-Here are an example of the output:
+Here is two example of the output:
```json
{{
"observation": [IMAGE_DESCRIPTION],
@@ -1057,6 +1057,12 @@ Here are an example of the output:
"action_code": "fill_input_id([ID], [TEXT])"
}}
+{{
+ "observation": "The current page is a CAPTCHA verification page on Amazon. It asks the user to ..",
+ "reasoning": "To proceed with the task of searching for products, I need to complete..",
+ "action_code": "fill_input_id(3, 'AUXPMR')"
+}}
+
Here are some tips for you:
- Never forget the overall question: **{task_prompt}**
- Maybe after a certain operation (e.g. click_id), the page content has not
@@ -1150,6 +1156,58 @@ out the information you need. Sometimes they are extremely useful.
return False
+ def _fix_action_code(action_code: str) -> str:
+ r"""Fix potential missing quotes in action code"""
+
+ match = re.match(r'(\w+)\((.*)\)', action_code)
+ if not match:
+ return action_code
+
+ func_name, args_str = match.groups()
+
+ args = []
+ current_arg = ""
+ in_quotes = False
+ quote_char = None
+
+ for char in args_str:
+ if char in ['"', "'"]:
+ if not in_quotes:
+ in_quotes = True
+ quote_char = char
+ current_arg += char
+ elif char == quote_char:
+ in_quotes = False
+ quote_char = None
+ current_arg += char
+ else:
+ current_arg += char
+ elif char == ',' and not in_quotes:
+ args.append(current_arg.strip())
+ current_arg = ""
+ else:
+ current_arg += char
+
+ if current_arg:
+ args.append(current_arg.strip())
+
+ fixed_args = []
+ for arg in args:
+ if (
+ (arg.startswith('"') and arg.endswith('"'))
+ or (arg.startswith("'") and arg.endswith("'"))
+ or re.match(r'^-?\d+(\.\d+)?$', arg)
+ or re.match(r'^-?\d+\.?\d*[eE][-+]?\d+$', arg)
+ or re.match(r'^0[xX][0-9a-fA-F]+$', arg)
+ ):
+ fixed_args.append(arg)
+
+ else:
+ fixed_args.append(f"'{arg}'")
+
+ return f"{func_name}({', '.join(fixed_args)})"
+
+ action_code = _fix_action_code(action_code)
prefix = "self.browser."
code = f"{prefix}{action_code}"