|
| 1 | +"""Execution Intents — bindings, execute, and execution history.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from typing import TYPE_CHECKING, Any |
| 6 | + |
| 7 | +if TYPE_CHECKING: |
| 8 | + from oneclaw.http_client import HttpClient |
| 9 | + from oneclaw.types import OneclawResponse |
| 10 | + |
| 11 | + |
| 12 | +class BindingsResource: |
| 13 | + """Manage agent bindings and execute intents against external services.""" |
| 14 | + |
| 15 | + def __init__(self, http: HttpClient) -> None: |
| 16 | + self._http = http |
| 17 | + |
| 18 | + def create( |
| 19 | + self, |
| 20 | + agent_id: str, |
| 21 | + *, |
| 22 | + name: str, |
| 23 | + binding_type: str, |
| 24 | + config: dict[str, Any] | None = None, |
| 25 | + guardrails: dict[str, Any] | None = None, |
| 26 | + credential: dict[str, Any] | None = None, |
| 27 | + ) -> OneclawResponse[Any]: |
| 28 | + """Create a binding for an agent.""" |
| 29 | + body: dict[str, Any] = {"name": name, "binding_type": binding_type} |
| 30 | + for key, val in { |
| 31 | + "config": config, |
| 32 | + "guardrails": guardrails, |
| 33 | + "credential": credential, |
| 34 | + }.items(): |
| 35 | + if val is not None: |
| 36 | + body[key] = val |
| 37 | + return self._http.request("POST", f"/v1/agents/{agent_id}/bindings", body=body) |
| 38 | + |
| 39 | + def list(self, agent_id: str) -> OneclawResponse[Any]: |
| 40 | + """List all bindings for an agent.""" |
| 41 | + return self._http.request("GET", f"/v1/agents/{agent_id}/bindings") |
| 42 | + |
| 43 | + def get(self, agent_id: str, binding_id: str) -> OneclawResponse[Any]: |
| 44 | + """Get a binding by ID.""" |
| 45 | + return self._http.request("GET", f"/v1/agents/{agent_id}/bindings/{binding_id}") |
| 46 | + |
| 47 | + def update( |
| 48 | + self, |
| 49 | + agent_id: str, |
| 50 | + binding_id: str, |
| 51 | + **kwargs: Any, |
| 52 | + ) -> OneclawResponse[Any]: |
| 53 | + """Update a binding's configuration, guardrails, or active status.""" |
| 54 | + body = {k: v for k, v in kwargs.items() if v is not None} |
| 55 | + return self._http.request( |
| 56 | + "PATCH", f"/v1/agents/{agent_id}/bindings/{binding_id}", body=body, |
| 57 | + ) |
| 58 | + |
| 59 | + def delete(self, agent_id: str, binding_id: str) -> OneclawResponse[Any]: |
| 60 | + """Delete a binding.""" |
| 61 | + return self._http.request("DELETE", f"/v1/agents/{agent_id}/bindings/{binding_id}") |
| 62 | + |
| 63 | + def test( |
| 64 | + self, |
| 65 | + agent_id: str, |
| 66 | + binding_id: str, |
| 67 | + *, |
| 68 | + timeout_ms: int | None = None, |
| 69 | + ) -> OneclawResponse[Any]: |
| 70 | + """Test connectivity of a binding.""" |
| 71 | + body: dict[str, Any] = {} |
| 72 | + if timeout_ms is not None: |
| 73 | + body["timeout_ms"] = timeout_ms |
| 74 | + return self._http.request( |
| 75 | + "POST", f"/v1/agents/{agent_id}/bindings/{binding_id}/test", body=body, |
| 76 | + ) |
| 77 | + |
| 78 | + def execute( |
| 79 | + self, |
| 80 | + agent_id: str, |
| 81 | + *, |
| 82 | + binding: str, |
| 83 | + intent_type: str, |
| 84 | + params: dict[str, Any] | None = None, |
| 85 | + execution_mode: str | None = None, |
| 86 | + ) -> OneclawResponse[Any]: |
| 87 | + """Execute an intent against a binding.""" |
| 88 | + body: dict[str, Any] = { |
| 89 | + "binding": binding, |
| 90 | + "intent_type": intent_type, |
| 91 | + "params": params or {}, |
| 92 | + } |
| 93 | + if execution_mode is not None: |
| 94 | + body["execution_mode"] = execution_mode |
| 95 | + return self._http.request("POST", f"/v1/agents/{agent_id}/execute", body=body) |
| 96 | + |
| 97 | + def list_executions( |
| 98 | + self, |
| 99 | + agent_id: str, |
| 100 | + *, |
| 101 | + limit: int | None = None, |
| 102 | + offset: int | None = None, |
| 103 | + ) -> OneclawResponse[Any]: |
| 104 | + """List execution events for an agent.""" |
| 105 | + query: dict[str, Any] = {} |
| 106 | + if limit is not None: |
| 107 | + query["limit"] = limit |
| 108 | + if offset is not None: |
| 109 | + query["offset"] = offset |
| 110 | + return self._http.request( |
| 111 | + "GET", f"/v1/agents/{agent_id}/executions", query=query or None, |
| 112 | + ) |
0 commit comments