Repository navigation
Expand file tree
/
Copy pathregistry.py
More file actions
360 lines (291 loc) · 13.4 KB
/
Copy pathregistry.py
File metadata and controls
360 lines (291 loc) · 13.4 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
"""Server-side command registry — stdlib-only, zero external dependencies.
Provides a lightweight command registration mechanism that lives entirely
within the ``server`` package and has **no** imports from the ``declarative``,
``models``, or any other SDK layer. This removes the previous circular
dependency where ``routes.py`` had to lazy-import
``declarative.decorator.sandbox._registry`` at request time.
Named commands have exactly one mechanism: a function registered here is listed by
``GET /commands`` and run by ``POST /commands/{name}``. Inside a sandbox template's
``commands.py`` register with the decorator::
from agent_sandbox.server import command
@command("greet", description="Say hello")
def greet(name: str) -> str:
return f"Hello, {name}!"
Argument types come from ``str``, ``int``, ``float``, ``bool`` and ``list[str]`` annotations.
The client SDK's ``@sandbox.register`` ends in the same registry. A private
:class:`CommandRegistry` can be handed to
:class:`~agent_sandbox.server.SandboxServer` for tests.
"""
from __future__ import annotations
import dataclasses
import inspect
import json
import typing
from typing import Any
__all__ = [
"CommandArg",
"RegisteredCommand",
"CommandRegistry",
"command",
"default_registry",
]
# ---------------------------------------------------------------------------
# Valid scalar and homogeneous string-array types
# ---------------------------------------------------------------------------
_VALID_ARG_TYPES: frozenset[str] = frozenset(
{"string", "integer", "float", "boolean", "string_array"}
)
# ---------------------------------------------------------------------------
# Data models (stdlib dataclasses only)
# ---------------------------------------------------------------------------
@dataclasses.dataclass
class CommandArg:
"""A single argument definition for a registered command.
Attributes:
name: Argument name (must match the function parameter name).
type: Argument type hint — one of ``"string"``, ``"integer"``,
``"float"``, ``"boolean"``, or ``"string_array"`` (native JSON list of strings).
required: Whether the caller *must* supply this argument.
default: Default value (as a string) when the argument is omitted.
description: Human-readable description shown in ``GET /commands``.
"""
name: str
type: str = "string"
required: bool = False
default: str | None = None
description: str = ""
def __post_init__(self) -> None:
"""Validate the argument type and any string-array default."""
if self.type not in _VALID_ARG_TYPES:
raise ValueError(f"Invalid arg type {self.type!r}; allowed: {sorted(_VALID_ARG_TYPES)}")
if self.type == "string_array" and self.default is not None:
try:
value = json.loads(self.default)
except (ValueError, TypeError) as exc:
raise ValueError("string_array default must be a JSON array of strings") from exc
if not isinstance(value, list) or not all(isinstance(item, str) for item in value):
raise ValueError("string_array default must be a JSON array of strings")
@dataclasses.dataclass
class RegisteredCommand:
"""Metadata for a command registered with :class:`CommandRegistry`.
Attributes:
name: Unique command name used in ``POST /commands/{name}``.
fn: Direct reference to the callable that implements the command.
args: Ordered list of :class:`CommandArg` definitions.
description: Human-readable description shown in ``GET /commands``.
hidden: When ``True`` the command is executable but omitted from the
``GET /commands`` listing (see :meth:`CommandRegistry.list_visible`).
scopes: Extra permission scopes (all-of) a caller needs, on top of
``commands:run``, when a server policy is active.
"""
name: str
fn: Any # Callable[..., Any] — kept as Any to avoid typing import
args: list[CommandArg] = dataclasses.field(default_factory=list)
description: str = ""
hidden: bool = False
scopes: tuple[str, ...] = ()
# ---------------------------------------------------------------------------
# Registry
# ---------------------------------------------------------------------------
class CommandRegistry:
"""Server-side command registry — independent of the declarative layer.
Maintains a mapping of command names to :class:`RegisteredCommand`
entries. The registry can optionally be *frozen* to prevent
runtime modifications after the server has started.
Example::
reg = CommandRegistry()
@reg.command("greet")
def greet(name: str) -> str:
return f"Hello, {name}!"
reg.freeze()
assert "greet" in reg
"""
def __init__(self) -> None:
self._commands: dict[str, RegisteredCommand] = {}
self._frozen: bool = False
# ------------------------------------------------------------------ #
# Mutation
# ------------------------------------------------------------------ #
def register(
self,
name: str,
fn: Any,
*,
args: list[CommandArg] | None = None,
description: str = "",
hidden: bool = False,
scopes: tuple[str, ...] | list[str] = (),
) -> None:
"""Register a command by *name*.
Args:
name: Unique command name.
fn: The callable that implements the command.
args: Optional list of :class:`CommandArg` definitions.
Defaults to an empty list.
description: Human-readable description.
hidden: When ``True`` the command is executable but excluded from
the ``GET /commands`` listing.
scopes: Extra permission scopes required to run the command when a
server policy is active (``GET /commands`` lists only commands
the caller may run).
Raises:
RuntimeError: If the registry has been frozen via
:meth:`freeze`.
"""
if self._frozen:
raise RuntimeError("Cannot register command after the registry has been frozen")
self._commands[name] = RegisteredCommand(
name=name,
fn=fn,
args=args if args is not None else [],
description=description,
hidden=hidden,
scopes=tuple(scopes),
)
def command(
self,
name: str | None = None,
*,
hidden: bool = False,
**kwargs: Any,
) -> Any:
"""Decorator for registering a command.
Supports both ``@registry.command("my_cmd")`` and
``@registry.command()`` (name inferred from the function).
Args:
name: Explicit command name. When ``None``, the decorated
function's ``__name__`` is used.
hidden: When ``True`` the command is executable but excluded from
the ``GET /commands`` listing.
**kwargs: Forwarded to :meth:`register` (e.g. ``args``,
``description``).
Returns:
A decorator that registers the function and returns it
unchanged.
Example::
@registry.command("add")
def add(a: int, b: int) -> int:
return a + b
@registry.command()
def greet(name: str) -> str:
return f"Hello, {name}!"
"""
def decorator(fn: Any) -> Any:
cmd_name = name if name is not None else fn.__name__
# If no explicit args were provided, infer from function signature.
if "args" not in kwargs or kwargs["args"] is None:
sig = inspect.signature(fn)
# typing.get_type_hints() resolves stringified annotations
# (PEP 563 / ``from __future__ import annotations``) back to
# real types so that int/float/bool params are correctly
# recognised instead of being treated as plain strings.
try:
resolved_hints = typing.get_type_hints(fn)
except Exception:
resolved_hints = {}
# Python3.10 get_type_hints adds Optional for a None default.
# Inspect the declared list annotation before that implicit widening.
try:
declared_hints = inspect.get_annotations(fn, eval_str=True)
except Exception:
declared_hints = {}
inferred_args: list[CommandArg] = []
for param_name, param in sig.parameters.items():
arg_type = "string" # default
# Prefer resolved hint; fall back to raw annotation
annotation = resolved_hints.get(param_name, param.annotation)
declared = declared_hints.get(param_name, param.annotation)
if typing.get_origin(declared) is list and typing.get_args(declared) == (str,):
annotation = declared
if annotation != inspect.Parameter.empty:
_annotation_map = {
int: "integer",
float: "float",
bool: "boolean",
}
if typing.get_origin(annotation) is list and typing.get_args(
annotation
) == (str,):
arg_type = "string_array"
else:
arg_type = _annotation_map.get(annotation, "string")
required = param.default is inspect.Parameter.empty
default = None
if not required and (param.default is not None or arg_type == "string_array"):
default = (
json.dumps(param.default)
if arg_type == "string_array"
else str(param.default)
)
inferred_args.append(
CommandArg(
name=param_name,
type=arg_type,
required=required,
default=default,
)
)
kwargs["args"] = inferred_args
self.register(cmd_name, fn, hidden=hidden, **kwargs)
return fn
return decorator
def freeze(self) -> None:
"""Lock the registry to prevent further modifications.
Once frozen, any call to :meth:`register` (including via
:meth:`command`) will raise :class:`RuntimeError`.
"""
self._frozen = True
# ------------------------------------------------------------------ #
# Lookup
# ------------------------------------------------------------------ #
def get(self, name: str) -> RegisteredCommand | None:
"""Return the :class:`RegisteredCommand` for *name*, or ``None``.
Args:
name: The command name to look up.
Returns:
The registered command, or ``None`` if not found.
"""
return self._commands.get(name)
def list_all(self) -> dict[str, RegisteredCommand]:
"""Return a shallow copy of all registered commands.
Returns:
A new ``dict`` mapping command names to
:class:`RegisteredCommand` instances.
"""
return dict(self._commands)
def list_visible(self) -> dict[str, RegisteredCommand]:
"""Return all registered commands that are not marked ``hidden``.
Returns:
A new ``dict`` mapping command names to
:class:`RegisteredCommand` instances, excluding any registered
with ``hidden=True``.
"""
return {name: cmd for name, cmd in self._commands.items() if not cmd.hidden}
# ------------------------------------------------------------------ #
# Dunder helpers
# ------------------------------------------------------------------ #
def __contains__(self, name: str) -> bool:
"""Return ``True`` if *name* is registered."""
return name in self._commands
def __len__(self) -> int:
"""Return the number of registered commands."""
return len(self._commands)
def __repr__(self) -> str:
frozen_tag = " (frozen)" if self._frozen else ""
return f"<CommandRegistry commands={list(self._commands.keys())}{frozen_tag}>"
# ---------------------------------------------------------------------------
# Module-level default registry
# ---------------------------------------------------------------------------
_default_registry = CommandRegistry()
def default_registry() -> CommandRegistry:
"""Return the module-level default :class:`CommandRegistry` instance.
Returns:
The singleton :data:`_default_registry`.
"""
return _default_registry
def command(name: str | None = None, *, hidden: bool = False, **kwargs: Any) -> Any:
"""Register the decorated function as a named command on the default registry.
``name`` defaults to the function name; ``hidden``, ``description``, ``scopes`` and ``args``
are those of :meth:`CommandRegistry.register`. Returns the function unchanged.
"""
return _default_registry.command(name, hidden=hidden, **kwargs)