Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- aworld/tools/human/actions.py +13 -0
- aworld/tools/human/human.py +99 -0
aworld/tools/human/actions.py
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# coding: utf-8
|
| 2 |
+
# Copyright (c) 2025 inclusionAI.
|
| 3 |
+
|
| 4 |
+
from aworld.core.tool.action_factory import ActionFactory
|
| 5 |
+
from aworld.core.tool.action import ExecutableAction
|
| 6 |
+
from aworld.tools.tool_action import HumanExecuteAction
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
@ActionFactory.register(name=HumanExecuteAction.HUMAN_CONFIRM.value.name,
|
| 10 |
+
desc=HumanExecuteAction.HUMAN_CONFIRM.value.desc,
|
| 11 |
+
tool_name="human_confirm")
|
| 12 |
+
class ExecuteAction(ExecutableAction):
|
| 13 |
+
"""Only one action, define it, implemented can be omitted. Act in tool."""
|
aworld/tools/human/human.py
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# coding: utf-8
|
| 2 |
+
# Copyright (c) 2025 inclusionAI.
|
| 3 |
+
from typing import Any, Dict, Tuple
|
| 4 |
+
from aworld.config import ToolConfig
|
| 5 |
+
from aworld.core.common import Observation, ActionModel, ActionResult
|
| 6 |
+
from aworld.core.context.base import Context
|
| 7 |
+
from aworld.core.event import eventbus
|
| 8 |
+
from aworld.core.event.base import Message, Constants, TopicType
|
| 9 |
+
from aworld.core.tool.base import ToolFactory, AsyncTool
|
| 10 |
+
from aworld.logs.util import logger
|
| 11 |
+
|
| 12 |
+
from aworld.tools.utils import build_observation
|
| 13 |
+
from aworld.tools.human.actions import HumanExecuteAction
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
@ToolFactory.register(name="human_confirm",
|
| 17 |
+
desc="human confirm",
|
| 18 |
+
supported_action=HumanExecuteAction)
|
| 19 |
+
class HumanTool(AsyncTool):
|
| 20 |
+
def __init__(self, conf: ToolConfig, **kwargs) -> None:
|
| 21 |
+
"""Init document tool."""
|
| 22 |
+
super(HumanTool, self).__init__(conf, **kwargs)
|
| 23 |
+
self.cur_observation = None
|
| 24 |
+
self.content = None
|
| 25 |
+
self.keyframes = []
|
| 26 |
+
self.init()
|
| 27 |
+
self.step_finished = True
|
| 28 |
+
|
| 29 |
+
async def reset(self, *, seed: int | None = None, options: Dict[str, str] | None = None) -> Tuple[
|
| 30 |
+
Observation, dict[str, Any]]:
|
| 31 |
+
await super().reset(seed=seed, options=options)
|
| 32 |
+
|
| 33 |
+
await self.close()
|
| 34 |
+
self.step_finished = True
|
| 35 |
+
return build_observation(observer=self.name(),
|
| 36 |
+
ability=HumanExecuteAction.HUMAN_CONFIRM.value.name), {}
|
| 37 |
+
|
| 38 |
+
def init(self) -> None:
|
| 39 |
+
self.initialized = True
|
| 40 |
+
|
| 41 |
+
async def close(self) -> None:
|
| 42 |
+
pass
|
| 43 |
+
|
| 44 |
+
async def finished(self) -> bool:
|
| 45 |
+
return self.step_finished
|
| 46 |
+
|
| 47 |
+
async def do_step(self, actions: list[ActionModel], **kwargs) -> Tuple[
|
| 48 |
+
Observation, float, bool, bool, Dict[str, Any]]:
|
| 49 |
+
self.step_finished = False
|
| 50 |
+
reward = 0.
|
| 51 |
+
fail_error = ""
|
| 52 |
+
observation = build_observation(observer=self.name(),
|
| 53 |
+
ability=HumanExecuteAction.HUMAN_CONFIRM.value.name)
|
| 54 |
+
info = {}
|
| 55 |
+
try:
|
| 56 |
+
if not actions:
|
| 57 |
+
raise ValueError("actions is empty")
|
| 58 |
+
action = actions[0]
|
| 59 |
+
confirm_content = action.params.get("confirm_content", "")
|
| 60 |
+
if not confirm_content:
|
| 61 |
+
raise ValueError("content invalid")
|
| 62 |
+
output, error = await self.human_confirm(confirm_content)
|
| 63 |
+
observation.content = output
|
| 64 |
+
observation.action_result.append(
|
| 65 |
+
ActionResult(is_done=True,
|
| 66 |
+
success=False if error else True,
|
| 67 |
+
content=f"{output}",
|
| 68 |
+
error=f"{error}",
|
| 69 |
+
keep=False))
|
| 70 |
+
reward = 1.
|
| 71 |
+
except Exception as e:
|
| 72 |
+
fail_error = str(e)
|
| 73 |
+
finally:
|
| 74 |
+
self.step_finished = True
|
| 75 |
+
info["exception"] = fail_error
|
| 76 |
+
info.update(kwargs)
|
| 77 |
+
return (observation, reward, kwargs.get("terminated", False),
|
| 78 |
+
kwargs.get("truncated", False), info)
|
| 79 |
+
|
| 80 |
+
async def human_confirm(self, confirm_content):
|
| 81 |
+
error = None
|
| 82 |
+
self.content = None
|
| 83 |
+
try:
|
| 84 |
+
self.content = confirm_content
|
| 85 |
+
await eventbus.publish(Message(
|
| 86 |
+
category=Constants.TASK,
|
| 87 |
+
payload=confirm_content,
|
| 88 |
+
sender=self.name(),
|
| 89 |
+
session_id=Context.instance().session_id,
|
| 90 |
+
topic=TopicType.HUMAN_CONFIRM
|
| 91 |
+
))
|
| 92 |
+
return self.content, error
|
| 93 |
+
except Exception as e:
|
| 94 |
+
error = str(e)
|
| 95 |
+
logger.warning(f"human_confirm error: {str(e)}")
|
| 96 |
+
finally:
|
| 97 |
+
pass
|
| 98 |
+
|
| 99 |
+
return self.content, error
|