Skip to content

Commit 5a2746c

Browse files
committed
style: format code with black
1 parent 73a1480 commit 5a2746c

3 files changed

Lines changed: 51 additions & 30 deletions

File tree

app/agent/base.py

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -194,31 +194,39 @@ def is_stuck(self) -> bool:
194194
# Check if last 5 messages contain 3+ error messages
195195
recent_messages = self.memory.messages[-5:]
196196
error_messages = [
197-
msg for msg in recent_messages
197+
msg
198+
for msg in recent_messages
198199
if msg.role == "assistant" and "Error" in msg.content
199200
]
200201
if len(error_messages) >= 3:
201-
logger.debug(f"Detected {len(error_messages)} error messages in recent history")
202+
logger.debug(
203+
f"Detected {len(error_messages)} error messages in recent history"
204+
)
202205
return True
203206

204207
# Criterion 3: Repeated error patterns in tool observations
205208
# Check for repeated "failed" or "error" patterns suggesting tool failure loop
206209
recent_assistant_messages = [
207-
msg for msg in recent_messages
208-
if msg.role == "assistant"
210+
msg for msg in recent_messages if msg.role == "assistant"
209211
]
210212
error_pattern_count = sum(
211-
1 for msg in recent_assistant_messages
212-
if any(pattern in msg.content for pattern in [
213-
"failed:",
214-
"Error:",
215-
"error:",
216-
"not found",
217-
"initialization failed"
218-
])
213+
1
214+
for msg in recent_assistant_messages
215+
if any(
216+
pattern in msg.content
217+
for pattern in [
218+
"failed:",
219+
"Error:",
220+
"error:",
221+
"not found",
222+
"initialization failed",
223+
]
224+
)
219225
)
220226
if error_pattern_count >= 3:
221-
logger.debug(f"Detected repeated error patterns ({error_pattern_count} in recent messages)")
227+
logger.debug(
228+
f"Detected repeated error patterns ({error_pattern_count} in recent messages)"
229+
)
222230
return True
223231

224232
return False

app/agent/toolcall.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
from app.schema import TOOL_CHOICE_TYPE, AgentState, Message, ToolCall, ToolChoice
1212
from app.tool import CreateChatCompletion, Terminate, ToolCollection
1313

14-
1514
TOOL_CALL_REQUIRED = "Tool calls required but none provided"
1615

1716

@@ -32,7 +31,9 @@ class ToolCallAgent(ReActAgent):
3231

3332
tool_calls: List[ToolCall] = Field(default_factory=list)
3433
_current_base64_image: Optional[str] = None
35-
_tool_failures: dict = PrivateAttr(default_factory=dict) # Track consecutive failures per tool
34+
_tool_failures: dict = PrivateAttr(
35+
default_factory=dict
36+
) # Track consecutive failures per tool
3637

3738
max_steps: int = 30
3839
max_observe: Optional[Union[int, bool]] = None
@@ -151,9 +152,13 @@ async def act(self) -> str:
151152
if "Error:" in result:
152153
failure_count = self._increment_tool_failure(tool_name)
153154
if failure_count > self.max_tool_failures:
154-
result += f"\n⚠️ Tool '{tool_name}' has failed {failure_count} consecutive times. " \
155-
f"Try a different approach or use alternative tools."
156-
logger.warning(f"Tool '{tool_name}' has failed {failure_count} times consecutively")
155+
result += (
156+
f"\n⚠️ Tool '{tool_name}' has failed {failure_count} consecutive times. "
157+
f"Try a different approach or use alternative tools."
158+
)
159+
logger.warning(
160+
f"Tool '{tool_name}' has failed {failure_count} times consecutively"
161+
)
157162
else:
158163
# Reset failure count on successful execution
159164
self._reset_tool_failure(tool_name)

app/tool/browser_use_tool.py

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
from app.tool.base import BaseTool, ToolResult
1616
from app.tool.web_search import WebSearch
1717

18-
1918
_BROWSER_DESCRIPTION = """\
2019
A powerful browser automation tool that allows interaction with web pages through various actions.
2120
* This tool provides commands for controlling a browser session, navigating web pages, and extracting information
@@ -151,15 +150,20 @@ def _classify_error(self, error: Exception) -> tuple[str, str]:
151150
error_str = str(error)
152151

153152
# Check for Playwright initialization errors
154-
if any(phrase in error_str for phrase in [
155-
"BrowserType.launch",
156-
"Executable doesn't exist",
157-
"playwright install",
158-
"Browser binary not found",
159-
]):
160-
return ("INIT_FAILED",
161-
"Browser initialization failed. Browser engine not available. "
162-
"Try using 'web_search' tool instead to search for information.")
153+
if any(
154+
phrase in error_str
155+
for phrase in [
156+
"BrowserType.launch",
157+
"Executable doesn't exist",
158+
"playwright install",
159+
"Browser binary not found",
160+
]
161+
):
162+
return (
163+
"INIT_FAILED",
164+
"Browser initialization failed. Browser engine not available. "
165+
"Try using 'web_search' tool instead to search for information.",
166+
)
163167

164168
# Check for operation errors
165169
return ("OPERATION_FAILED", f"Browser action failed: {error_str}")
@@ -258,7 +262,9 @@ async def execute(
258262
return ToolResult(error=error_msg)
259263
else:
260264
# Other initialization errors
261-
return ToolResult(error=f"Browser initialization failed: {str(init_error)}")
265+
return ToolResult(
266+
error=f"Browser initialization failed: {str(init_error)}"
267+
)
262268

263269
# Get max content length from config
264270
max_content_length = getattr(
@@ -514,7 +520,9 @@ async def execute(
514520
if error_type == "INIT_FAILED":
515521
return ToolResult(error=error_msg)
516522
else:
517-
return ToolResult(error=f"Browser action '{action}' failed: {error_msg}")
523+
return ToolResult(
524+
error=f"Browser action '{action}' failed: {error_msg}"
525+
)
518526

519527
async def get_current_state(
520528
self, context: Optional[BrowserContext] = None

0 commit comments

Comments
 (0)