Skip to content

Commit

Permalink
refactor(browser): add action param to web interaction hitl call
Browse files Browse the repository at this point in the history
  • Loading branch information
idiotWu committed Jan 26, 2025
1 parent e2f2a85 commit da37fda
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 7 deletions.
4 changes: 3 additions & 1 deletion npiai/core/__test__/captcha_detection.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import asyncio
from typing import Literal

from npiai import BrowserTool, HITL
from npiai.utils.test_utils import DebugContext
Expand Down Expand Up @@ -38,8 +39,9 @@ async def web_interaction(
tool_name: str,
message: str,
url: str,
action: Literal["captcha", "login"],
) -> str:
print(f"[HITL] web_interaction: {message=}, {url=}")
print(f"[HITL] web_interaction: {message=}, {url=}, {action=}")
return "web_interaction"


Expand Down
3 changes: 2 additions & 1 deletion npiai/core/hitl.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from abc import ABC, abstractmethod
from typing import List
from typing import List, Literal

from npiai.context import Context

Expand Down Expand Up @@ -41,4 +41,5 @@ async def web_interaction(
tool_name: str,
message: str,
url: str,
action: Literal["captcha", "login"],
) -> str: ...
9 changes: 5 additions & 4 deletions npiai/core/tool/_browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ async def load_page(
locator = self.playwright.page.locator(wait_for_selector)
await locator.first.wait_for(state="attached", timeout=timeout)
except TimeoutError:
await self.detect_captcha(ctx)
# if the selector is not found, check if a captcha is present
force_capcha_detection = True
# wait for the page to become stable
elif timeout is not None:
try:
Expand All @@ -68,9 +69,7 @@ async def load_page(

# await self.playwright.page.wait_for_timeout(wait)

# capcha detection will be done (or unnecessary if elements matched) if selector is provided
# so we only do it if no selector is provided
if not wait_for_selector and force_capcha_detection:
if force_capcha_detection:
await self.detect_captcha(ctx)

@function
Expand Down Expand Up @@ -306,12 +305,14 @@ async def handle_captcha(captcha_type: Literal["none", "captcha", "login"]):
tool_name=self.name,
message="Would you please help me solve the captcha?",
url=url,
action="captcha",
)
case "login":
await ctx.hitl.web_interaction(
tool_name=self.name,
message="Would you please help me login to the website?",
url=url,
action="login",
)

return captcha_type
Expand Down
4 changes: 3 additions & 1 deletion npiai/tools/scrapers/web/__test__/interactive.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import asyncio
from typing import Literal

from npiai.utils.test_utils import DebugContext

Expand Down Expand Up @@ -42,8 +43,9 @@ async def web_interaction(
tool_name: str,
message: str,
url: str,
action: Literal["captcha", "login"],
) -> str:
print(f"[HITL] web_interaction: {message=}, {url=}")
print(f"[HITL] web_interaction: {message=}, {url=}, {action=}")
return "web_interaction"


Expand Down

0 comments on commit da37fda

Please sign in to comment.