Skip to content

Commit f113288

Browse files
authored
refactor: clean up code formatting and improve readability across multiple files (#31)
1 parent 8e19c38 commit f113288

File tree

23 files changed

+98
-68
lines changed

23 files changed

+98
-68
lines changed

src/ii_agent/agents/anthropic_fc.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,10 @@ async def _process_messages(self):
105105
)
106106

107107
# Only send to websocket if this is not an event from the client and websocket exists
108-
if message.type != EventType.USER_MESSAGE and self.websocket is not None:
108+
if (
109+
message.type != EventType.USER_MESSAGE
110+
and self.websocket is not None
111+
):
109112
try:
110113
await self.websocket.send_json(message.model_dump())
111114
except Exception as e:
@@ -169,7 +172,9 @@ def run_impl(
169172
if ext == "jpg":
170173
ext = "jpeg"
171174
if ext in ["png", "gif", "jpeg", "webp"]:
172-
base64_image = encode_image(str(self.workspace_manager.workspace_path(file)))
175+
base64_image = encode_image(
176+
str(self.workspace_manager.workspace_path(file))
177+
)
173178
image_blocks.append(
174179
{
175180
"source": {
@@ -215,7 +220,7 @@ def run_impl(
215220
tools=all_tool_params,
216221
system_prompt=self.system_prompt,
217222
)
218-
223+
219224
if len(model_response) == 0:
220225
model_response = [TextResult(text=COMPLETE_MESSAGE)]
221226

@@ -226,7 +231,6 @@ def run_impl(
226231
pending_tool_calls = self.history.get_pending_tool_calls()
227232

228233
if len(pending_tool_calls) == 0:
229-
230234
# No tools were called, so assume the task is complete
231235
self.logger_for_agent_logs.info("[no tools were called]")
232236
self.message_queue.put_nowait(

src/ii_agent/browser/browser.py

Lines changed: 38 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -214,18 +214,24 @@ async def _on_page_change(self, page: Page):
214214

215215
self._cdp_session = await self.context.new_cdp_session(page)
216216

217-
# set viewport size
218-
await self._cdp_session.send("Emulation.setDeviceMetricsOverride", {
219-
"width": self.config.viewport_size["width"],
220-
"height": self.config.viewport_size["height"],
221-
"deviceScaleFactor": 1,
222-
"mobile": False,
223-
})
217+
# set viewport size
218+
await self._cdp_session.send(
219+
"Emulation.setDeviceMetricsOverride",
220+
{
221+
"width": self.config.viewport_size["width"],
222+
"height": self.config.viewport_size["height"],
223+
"deviceScaleFactor": 1,
224+
"mobile": False,
225+
},
226+
)
224227
# Optional: adjust visible size (for headless rendering)
225-
await self._cdp_session.send("Emulation.setVisibleSize", {
226-
"width": self.config.viewport_size["width"],
227-
"height": self.config.viewport_size["height"],
228-
})
228+
await self._cdp_session.send(
229+
"Emulation.setVisibleSize",
230+
{
231+
"width": self.config.viewport_size["width"],
232+
"height": self.config.viewport_size["height"],
233+
},
234+
)
229235

230236
self.current_page = page
231237

@@ -484,18 +490,24 @@ async def get_cdp_session(self):
484490
):
485491
self._cdp_session = await self.context.new_cdp_session(self.current_page)
486492

487-
# set viewport size
488-
await self._cdp_session.send("Emulation.setDeviceMetricsOverride", {
489-
"width": self.config.viewport_size["width"],
490-
"height": self.config.viewport_size["height"],
491-
"deviceScaleFactor": 1,
492-
"mobile": False,
493-
})
493+
# set viewport size
494+
await self._cdp_session.send(
495+
"Emulation.setDeviceMetricsOverride",
496+
{
497+
"width": self.config.viewport_size["width"],
498+
"height": self.config.viewport_size["height"],
499+
"deviceScaleFactor": 1,
500+
"mobile": False,
501+
},
502+
)
494503
# Optional: adjust visible size (for headless rendering)
495-
await self._cdp_session.send("Emulation.setVisibleSize", {
496-
"width": self.config.viewport_size["width"],
497-
"height": self.config.viewport_size["height"],
498-
})
504+
await self._cdp_session.send(
505+
"Emulation.setVisibleSize",
506+
{
507+
"width": self.config.viewport_size["width"],
508+
"height": self.config.viewport_size["height"],
509+
},
510+
)
499511

500512
# Store reference to the page this session belongs to
501513
self._cdp_session._page = self.current_page
@@ -547,15 +559,15 @@ async def get_storage_state(self) -> dict[str, Any]:
547559
async def handle_pdf_url_navigation(self):
548560
page = await self.get_current_page()
549561
if is_pdf_url(page.url):
550-
await asyncio.sleep(5) # Long sleep to ensure PDF is loaded
562+
await asyncio.sleep(5) # Long sleep to ensure PDF is loaded
551563
await page.keyboard.press("Escape")
552564
await asyncio.sleep(0.1)
553565
await page.keyboard.press("Control+\\")
554566
await asyncio.sleep(0.1)
555567
await page.mouse.click(
556568
self.config.viewport_size["width"] * 0.75, # Right side of screen
557-
self.config.viewport_size["height"] * 0.25 # Upper portion
569+
self.config.viewport_size["height"] * 0.25, # Upper portion
558570
)
559-
571+
560572
state = await self.update_state()
561-
return state
573+
return state

src/ii_agent/llm/context_manager/file_based.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ class FileBasedContextManager(ContextManager):
2323
TRUNCATED_TOOL_INPUT_MSG = (
2424
"[Truncated...re-run tool if you need to see input/output again.]"
2525
)
26-
TRUNCATED_FILE_MSG = "[Truncated...content saved to {relative_path}. You can view it if needed.]"
26+
TRUNCATED_FILE_MSG = (
27+
"[Truncated...content saved to {relative_path}. You can view it if needed.]"
28+
)
2729

2830
def __init__(
2931
self,
@@ -111,12 +113,18 @@ def apply_truncation_if_needed(
111113
content_hash = self._get_content_hash(message.tool_output)
112114
if message.tool_name == VisitWebpageTool.name:
113115
# NOTE: assume that the previous message is a tool call
114-
previous_message = truncated_message_lists[turn_idx - 1][0]
116+
previous_message = truncated_message_lists[
117+
turn_idx - 1
118+
][0]
115119
if isinstance(previous_message, ToolCall):
116-
url = previous_message.tool_input.get("url", "unknown_url")
120+
url = previous_message.tool_input.get(
121+
"url", "unknown_url"
122+
)
117123
else:
118124
url = "unknown_url"
119-
print(f"Previous message is not a tool call: {previous_message}")
125+
print(
126+
f"Previous message is not a tool call: {previous_message}"
127+
)
120128
filename = self._generate_filename_from_url(
121129
url, content_hash
122130
)
@@ -143,7 +151,9 @@ def apply_truncation_if_needed(
143151

144152
# Update message with reference to file
145153
message.tool_output = self.TRUNCATED_FILE_MSG.format(
146-
relative_path=self.workspace_manager.relative_path(filepath),
154+
relative_path=self.workspace_manager.relative_path(
155+
filepath
156+
),
147157
)
148158
self.logger.info(f"Saved {filename} to {filepath}")
149159
else:

src/ii_agent/prompts/system_prompt.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,4 +204,4 @@
204204
</tool_use_rules>
205205
206206
Today is {datetime.now().strftime("%Y-%m-%d")}. The first step of a task is to use sequential thinking module to plan the task. then regularly update the todo.md file to track the progress.
207-
"""
207+
"""

src/ii_agent/tools/advanced_tools/gemini/audio_tool.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ class AudioUnderstandingTool(GeminiTool):
6363
- Describe, summarize, or answer questions about audio content
6464
- Analyze specific segments of the audio
6565
66-
Provide one query at a time. Supported formats: {', '.join(SUPPORTED_FORMATS)}
66+
Provide one query at a time. Supported formats: {", ".join(SUPPORTED_FORMATS)}
6767
"""
6868

6969
input_schema = {

src/ii_agent/tools/advanced_tools/gemini/base.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@
22

33
from typing import Optional
44
from google import genai
5-
from google.genai import types
65
from ii_agent.tools.base import (
76
LLMTool,
8-
ToolImplOutput,
97
)
108
from ii_agent.utils import WorkspaceManager
119

src/ii_agent/tools/bash_tool.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -429,4 +429,4 @@ def create_docker_bash_tool(
429429
cwd=cwd,
430430
command_filters=[docker_filter],
431431
additional_banned_command_strs=additional_banned_command_strs,
432-
)
432+
)

src/ii_agent/tools/browser_tools/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,4 @@ def run_impl(
3737
message_history: Optional[MessageHistory] = None,
3838
) -> ToolImplOutput:
3939
loop = get_event_loop()
40-
return loop.run_until_complete(self._run(tool_input, message_history))
40+
return loop.run_until_complete(self._run(tool_input, message_history))

src/ii_agent/tools/browser_tools/click.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,5 @@ async def _run(
5757

5858
state = await self.browser.update_state()
5959
state = await self.browser.handle_pdf_url_navigation()
60-
60+
6161
return utils.format_screenshot_tool_output(state.screenshot, msg)

src/ii_agent/tools/browser_tools/dropdown.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ async def _run(
8383
msg = "\n".join(formatted_options)
8484
msg += "\nIf you decide to use this select element, use the exact option name in select_dropdown_option"
8585
state = await self.browser.update_state()
86-
86+
8787
return utils.format_screenshot_tool_output(state.screenshot, msg)
8888

8989

0 commit comments

Comments
 (0)