Build latest book artifacts / build (push) Canceled after 0s
dependency resolution / resolve (3.11) (push) Canceled after 0s
dependency resolution / resolve (3.13) (push) Canceled after 0s
deploy-pages / build (push) Canceled after 0s
deploy-pages / deploy (push) Canceled after 0s
i18n consistency check / check (push) Canceled after 0s
provider adoption tests / test (chapter2/context-compression) (push) Canceled after 0s
provider adoption tests / test (chapter2/prompt-injection) (push) Canceled after 0s
provider adoption tests / test (chapter2/system-hint) (push) Canceled after 0s
provider adoption tests / test (chapter3/log-sanitization) (push) Canceled after 0s
web-search-agent tests / test (push) Canceled after 0s
web-search-agent tests / agentbook (push) Canceled after 0s
117 lines
3.7 KiB
Python
117 lines
3.7 KiB
Python
# coding: utf-8
|
|
# Copyright (c) 2025 inclusionAI.
|
|
import asyncio
|
|
import logging
|
|
from concurrent.futures.process import ProcessPoolExecutor
|
|
from typing import List, Dict, Union
|
|
|
|
from aworld.config import RunConfig
|
|
from aworld.config.conf import TaskConfig
|
|
from aworld.agents.llm_agent import Agent
|
|
from aworld.core.agent.swarm import Swarm
|
|
from aworld.core.common import Config
|
|
from aworld.core.task import Task, TaskResponse, Runner
|
|
from aworld.output import StreamingOutputs
|
|
from aworld.utils.common import sync_exec
|
|
from aworld.utils.run_util import exec_tasks
|
|
|
|
|
|
class Runners:
|
|
"""Unified entrance to the utility class of the runnable task of execution."""
|
|
|
|
@staticmethod
|
|
def streamed_run_task(task: Task) -> StreamingOutputs:
|
|
"""Run the task in stream output."""
|
|
if not task.conf:
|
|
task.conf = TaskConfig()
|
|
|
|
streamed_result = StreamingOutputs(
|
|
input=task.input,
|
|
usage={},
|
|
is_complete=False
|
|
)
|
|
task.outputs = streamed_result
|
|
streamed_result.task_id = task.id
|
|
|
|
logging.info(f"[Runners]streamed_run_task start task_id={task.id}, agent={task.agent}, swarm = {task.swarm} ")
|
|
|
|
streamed_result._run_impl_task = asyncio.create_task(
|
|
Runners.run_task(task)
|
|
)
|
|
return streamed_result
|
|
|
|
@staticmethod
|
|
async def run_task(task: Union[Task, List[Task]], run_conf: RunConfig = None) -> Dict[str, TaskResponse]:
|
|
"""Run tasks for some complex scenarios where agents cannot be directly used.
|
|
|
|
Args:
|
|
task: User task define.
|
|
run_conf:
|
|
"""
|
|
if isinstance(task, Task):
|
|
task = [task]
|
|
|
|
logging.debug(f"[Runners]run_task start task_id={task[0].id} start")
|
|
result = await exec_tasks(task, run_conf)
|
|
logging.debug(f"[Runners]run_task end task_id={task[0].id} end")
|
|
return result
|
|
|
|
@staticmethod
|
|
def sync_run_task(task: Union[Task, List[Task]], run_conf: Config = None) -> Dict[str, TaskResponse]:
|
|
return sync_exec(Runners.run_task, task=task, run_conf=run_conf)
|
|
|
|
@staticmethod
|
|
def sync_run(
|
|
input: str,
|
|
agent: Agent = None,
|
|
swarm: Swarm = None,
|
|
tool_names: List[str] = [],
|
|
session_id: str = None,
|
|
run_conf: RunConfig = None
|
|
) -> TaskResponse:
|
|
return sync_exec(
|
|
Runners.run,
|
|
input=input,
|
|
agent=agent,
|
|
swarm=swarm,
|
|
tool_names=tool_names,
|
|
session_id=session_id,
|
|
run_conf=run_conf
|
|
)
|
|
|
|
@staticmethod
|
|
async def run(
|
|
input: str,
|
|
agent: Agent = None,
|
|
swarm: Swarm = None,
|
|
tool_names: List[str] = [],
|
|
session_id: str = None,
|
|
run_conf: RunConfig = None
|
|
) -> TaskResponse:
|
|
"""Run agent directly with input and tool names.
|
|
|
|
Args:
|
|
input: User query.
|
|
agent: An agent with AI model configured, prompts, tools, mcp servers and other agents.
|
|
swarm: Multi-agent topo.
|
|
tool_names: Tool name list.
|
|
session_id: Session id.
|
|
|
|
Returns:
|
|
TaskResponse: Task response.
|
|
"""
|
|
if agent and swarm:
|
|
raise ValueError("`agent` and `swarm` only choose one.")
|
|
|
|
if not input:
|
|
raise ValueError('`input` is empty.')
|
|
|
|
if agent:
|
|
agent.task = input
|
|
swarm = Swarm(agent)
|
|
|
|
task = Task(input=input, swarm=swarm, tool_names=tool_names,
|
|
event_driven=swarm.event_driven, session_id=session_id)
|
|
res = await Runners.run_task(task, run_conf=run_conf)
|
|
return res.get(task.id)
|