File size: 1,531 Bytes
9389ab4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Action and observation models. One generic action serves every task."""

from typing import Any

from openenv.core.env_server.types import Action, Observation
from pydantic import Field, model_validator


class DeskAction(Action):
    tool: str = Field(
        description="One of: list, read, python, submit.",
    )
    args: dict[str, Any] = Field(
        default_factory=dict,
        description=(
            'Tool arguments. read: {"path": "file.csv"} or {"paths": ["a.csv", "b.json"]}. '
            'python: {"code": "print(1+1)"}. submit: {"answer": {...the documented keys...}}. list: {}.'
        ),
    )

    @model_validator(mode="before")
    @classmethod
    def _fold_stray_keys(cls, data: Any) -> Any:
        # Forgiving parse: {"tool": "read", "path": "x"} means {"tool": "read", "args": {"path": "x"}}.
        if isinstance(data, dict):
            known = {"tool", "args", "metadata"}
            stray = {k: v for k, v in data.items() if k not in known}
            if stray:
                data = {k: v for k, v in data.items() if k in known}
                args = dict(data.get("args") or {})
                for k, v in stray.items():
                    args.setdefault(k, v)
                data["args"] = args
        return data


class DeskObservation(Observation):
    output: str = Field(default="", description="Task brief at reset, otherwise the result of the last tool call.")
    steps_left: int = Field(default=0, description="Tool calls left before the episode ends with reward 0.")