-
Notifications
You must be signed in to change notification settings - Fork 536
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
874 additions
and
283 deletions.
There are no files selected for viewing
6 changes: 3 additions & 3 deletions
6
lavague-core/lavague/core/__init__.py → lavague-core/lavague/__init__.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 3 additions & 2 deletions
5
lavague-core/lavague/core/action/__init__.py → lavague-core/lavague/action/__init__.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,12 @@ | ||
from lavague.core.action.base import ( | ||
from lavague.action.base import ( | ||
Action, | ||
ActionStatus, | ||
ActionParser, | ||
DEFAULT_PARSER, | ||
UnhandledTypeException, | ||
ActionTranslator, | ||
) | ||
|
||
from lavague.core.action.navigation import NavigationAction | ||
from lavague.action.navigation import NavigationAction | ||
|
||
DEFAULT_PARSER.register("navigation", NavigationAction) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
from typing import Dict, Type, Optional, Callable, TypeVar, Self | ||
from pydantic import BaseModel, validate_call | ||
from enum import Enum | ||
|
||
|
||
class ActionStatus(Enum): | ||
COMPLETED = "completed" | ||
FAILED = "failed" | ||
|
||
|
||
class Action(BaseModel): | ||
"""Action performed by the agent.""" | ||
|
||
engine: str | ||
action: str | ||
status: ActionStatus | ||
|
||
@classmethod | ||
def parse(cls, action_dict: Dict) -> "Action": | ||
return cls(**action_dict) | ||
|
||
@classmethod | ||
def add_translator(cls, name: str, translator: "ActionTranslator[Self]"): | ||
setattr(cls, name, translator) | ||
|
||
|
||
class ActionParser(BaseModel): | ||
engine_action_builders: Dict[str, Type[Action]] | ||
|
||
def __init__(self): | ||
super().__init__(engine_action_builders={}) | ||
|
||
@validate_call | ||
def register(self, engine: str, action: Type[Action]): | ||
self.engine_action_builders[engine] = action | ||
|
||
def unregister(self, engine: str): | ||
if engine in self.engine_action_builders: | ||
del self.engine_action_builders[engine] | ||
|
||
def parse(self, action_dict: Dict) -> Action: | ||
engine = action_dict.get("engine", "") | ||
target_type: Type[Action] = self.engine_action_builders.get(engine, Action) | ||
try: | ||
return target_type.parse(action_dict) | ||
except UnhandledTypeException: | ||
return Action.parse(action_dict) | ||
|
||
|
||
class UnhandledTypeException(Exception): | ||
pass | ||
|
||
|
||
T = TypeVar("T", bound=Action) | ||
|
||
|
||
ActionTranslator = Callable[[T], Optional[str]] | ||
|
||
|
||
DEFAULT_PARSER = ActionParser() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
from lavague.action import Action | ||
from typing import ClassVar, Dict, Type, Optional, TypeVar | ||
|
||
T = TypeVar("T", bound="NavigationAction") | ||
|
||
|
||
class NavigationAction(Action): | ||
"""Navigation action performed by the agent.""" | ||
|
||
subtypes: ClassVar[Dict[str, Type["NavigationAction"]]] = {} | ||
|
||
xpath: str | ||
value: Optional[str] = None | ||
|
||
@classmethod | ||
def parse(cls, action_dict: Dict) -> "NavigationAction": | ||
action_name = action_dict.get("action", "") | ||
target_type = cls.subtypes.get(action_name, NavigationAction) | ||
return target_type(**action_dict) | ||
|
||
@classmethod | ||
def register_subtype(cls, subtype: str, action: Type[T]): | ||
cls.subtypes[subtype] = action | ||
return cls | ||
|
||
|
||
def register_navigation(name: str): | ||
def wrapper(cls: Type[T]) -> Type[T]: | ||
NavigationAction.register_subtype(name, cls) | ||
return cls | ||
|
||
return wrapper | ||
|
||
|
||
class NavigationWithValueAction(NavigationAction): | ||
"""Navigation action performed by the agent with a value.""" | ||
|
||
value: str | ||
|
||
|
||
@register_navigation("click") | ||
class ClickAction(NavigationAction): | ||
pass | ||
|
||
|
||
@register_navigation("hover") | ||
class HoverAction(NavigationAction): | ||
pass | ||
|
||
|
||
@register_navigation("setValue") | ||
class SetValueAction(NavigationWithValueAction): | ||
pass | ||
|
||
|
||
@register_navigation("setValueAndEnter") | ||
class SetValueAndEnterAction(SetValueAction): | ||
pass | ||
|
||
|
||
@register_navigation("dropdownSelect") | ||
class DropdownSelectAction(NavigationWithValueAction): | ||
pass | ||
|
||
|
||
@register_navigation("scroll_down") | ||
class ScrollDownAction(NavigationAction): | ||
pass | ||
|
||
|
||
@register_navigation("scroll_up") | ||
class ScrollUpAction(NavigationAction): | ||
pass | ||
|
||
|
||
@register_navigation("back") | ||
class BackAction(NavigationAction): | ||
pass | ||
|
||
|
||
@register_navigation("switch_tab") | ||
class SwitchTabAction(NavigationAction): | ||
pass |
6 changes: 3 additions & 3 deletions
6
lavague-core/lavague/core/agent.py → lavague-core/lavague/agent.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import click | ||
from lavague import WebAgent | ||
import sys | ||
from typing import Optional | ||
|
||
|
||
def run(url: str, objective: str, file: Optional[str] = None): | ||
agent = WebAgent() | ||
trajectory = agent.run(url, objective) | ||
if file: | ||
trajectory.write_to_file(file) | ||
else: | ||
print(trajectory.model_dump_json(indent=2)) | ||
|
||
|
||
@click.command() | ||
@click.argument("url", required=True) | ||
@click.argument("objective", required=True) | ||
@click.option("--file", "-f", required=False) | ||
def cli_run(url: str, objective: str, file: Optional[str]): | ||
run(url, objective, file) | ||
|
||
|
||
if __name__ == "__main__": | ||
url = sys.argv[1] | ||
objective = sys.argv[2] | ||
run(url, objective) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.