File size: 2,017 Bytes
560752c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eaa79f0
560752c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
"""
Tool generation helpers for the warehouse fulfillment environment.
"""

from __future__ import annotations

from typing import Any, Dict, List

from .env import WarehouseFulfillmentEnv
from .models import WarehouseAction


_TOOL_DESCRIPTIONS: Dict[str, str] = {
    "turn_left": "Rotate the robot 90 degrees left in place.",
    "turn_right": "Rotate the robot 90 degrees right in place.",
    "move_forward": "Move one cell forward if not blocked.",
    "scan_bin": "Scan the bin in front to validate its contents.",
    "pick_item": "Pick an item from the bin in front.",
    "pack_item": "Pack the carried item at the packing station.",
    "recharge": "Recharge the battery at the charging dock.",
    "rest": "Rest at the rest area to restore stamina.",
    "wait": "Stay in place and consume time.",
}


def build_tools() -> List[Dict[str, Any]]:
    """Return OpenAI-compatible tool definitions for warehouse actions."""
    tools = []
    for command in WarehouseFulfillmentEnv.action_space:
        tools.append(_tool_for_command(command))
    return tools


def _tool_for_command(command: str) -> Dict[str, Any]:
    return {
        "type": "function",
        "function": {
            "name": command,
            "description": _TOOL_DESCRIPTIONS.get(command, "Warehouse action."),
            "parameters": {
                "type": "object",
                "properties": {},
                "additionalProperties": False,
            },
        },
    }


def tool_call_to_action(tool_call: Dict[str, Any]) -> WarehouseAction:
    """Convert a tool call payload into a WarehouseAction."""
    name = tool_call.get("name")
    if name is None and isinstance(tool_call.get("function"), dict):
        name = tool_call["function"].get("name")
    if name not in WarehouseFulfillmentEnv.action_space:
        raise ValueError(f"Unknown tool/action: {name}")
    return WarehouseAction(command=name)


def is_valid_tool_name(name: str) -> bool:
    return name in WarehouseFulfillmentEnv.action_space