Files
Dorod-Sky/integrations/langchain/skyvern_langchain/agent.py

61 lines
2.2 KiB
Python
Raw Normal View History

2025-05-19 16:07:02 +08:00
from typing import Any, Type
2025-02-21 15:56:06 +08:00
2025-03-03 10:38:00 +08:00
from langchain.tools import BaseTool
from litellm import BaseModel
from pydantic import Field
from skyvern_langchain.schema import CreateTaskInput, GetTaskInput
from skyvern_langchain.settings import settings
2025-02-21 15:56:06 +08:00
2025-05-19 16:07:02 +08:00
from skyvern import Skyvern
from skyvern.client.types.get_run_response import GetRunResponse
2025-05-19 16:07:02 +08:00
from skyvern.client.types.task_run_response import TaskRunResponse
from skyvern.schemas.runs import RunEngine
2025-02-21 15:56:06 +08:00
2025-03-03 10:38:00 +08:00
class SkyvernTaskBaseTool(BaseTool):
2025-05-19 16:07:02 +08:00
engine: RunEngine = Field(default=settings.engine)
2025-03-03 23:05:35 +08:00
run_task_timeout_seconds: int = Field(default=settings.run_task_timeout_seconds)
2025-05-19 16:07:02 +08:00
agent: Skyvern = Skyvern(base_url=None, api_key=None)
2025-02-21 15:56:06 +08:00
2025-03-03 10:38:00 +08:00
def _run(self, *args: Any, **kwargs: Any) -> None:
raise NotImplementedError("skyvern task tool does not support sync")
2025-02-21 15:56:06 +08:00
2025-03-03 10:38:00 +08:00
class RunTask(SkyvernTaskBaseTool):
name: str = "run-skyvern-agent-task"
description: str = """Use Skyvern agent to run a task. This function won't return until the task is finished."""
args_schema: Type[BaseModel] = CreateTaskInput
2025-02-21 15:56:06 +08:00
2025-05-19 16:07:02 +08:00
async def _arun(self, user_prompt: str, url: str | None = None) -> TaskRunResponse:
return await self.agent.run_task(
prompt=user_prompt,
url=url,
engine=self.engine,
timeout=self.run_task_timeout_seconds,
wait_for_completion=True,
2025-03-03 23:05:35 +08:00
)
2025-02-21 15:56:06 +08:00
2025-03-03 10:38:00 +08:00
class DispatchTask(SkyvernTaskBaseTool):
name: str = "dispatch-skyvern-agent-task"
description: str = """Use Skyvern agent to dispatch a task. This function will return immediately and the task will be running in the background."""
args_schema: Type[BaseModel] = CreateTaskInput
2025-05-19 16:07:02 +08:00
async def _arun(self, user_prompt: str, url: str | None = None) -> TaskRunResponse:
return await self.agent.run_task(
prompt=user_prompt,
url=url,
engine=self.engine,
timeout=self.run_task_timeout_seconds,
wait_for_completion=False,
)
2025-03-03 10:38:00 +08:00
class GetTask(SkyvernTaskBaseTool):
name: str = "get-skyvern-agent-task"
description: str = """Use Skyvern agent to get a task."""
args_schema: Type[BaseModel] = GetTaskInput
async def _arun(self, task_id: str) -> GetRunResponse | None:
2025-05-19 16:07:02 +08:00
return await self.agent.get_run(run_id=task_id)