Repository navigation
Expand file tree
/
Copy pathagents.py
More file actions
183 lines (151 loc) · 6.02 KB
/
Copy pathagents.py
File metadata and controls
183 lines (151 loc) · 6.02 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
"""Agent management operations for Agent Control SDK."""
from typing import Any, Literal, cast
from agent_control_engine import ensure_evaluators_discovered
from agent_control_models import Agent
from agent_control_models.server import AgentControlsResponse
from .client import AgentControlClient
from .validation import ensure_agent_name
async def register_agent(
client: AgentControlClient,
agent: Agent,
steps: list[dict[str, Any]] | None = None,
conflict_mode: Literal["strict", "overwrite"] = "overwrite",
) -> dict[str, Any]:
"""Register an agent with the server via /initAgent endpoint.
"""
ensure_evaluators_discovered()
agent_dict = agent.to_dict()
agent_dict["agent_name"] = ensure_agent_name(str(agent_dict.get("agent_name", "")))
payload = {
"agent": agent_dict,
"steps": steps or [],
"conflict_mode": conflict_mode,
}
headers = None
response = await client.http_client.post(
"/api/v1/agents/initAgent",
json=payload,
headers=headers,
)
response.raise_for_status()
return cast(dict[str, Any], response.json())
async def get_agent(client: AgentControlClient, agent_name: str) -> dict[str, Any]:
"""Get agent details by name from the server."""
normalized_name = ensure_agent_name(agent_name)
response = await client.http_client.get(f"/api/v1/agents/{normalized_name}")
response.raise_for_status()
return cast(dict[str, Any], response.json())
async def list_agents(
client: AgentControlClient,
cursor: str | None = None,
limit: int = 20,
) -> dict[str, Any]:
"""List all registered agents from the server."""
params: dict[str, Any] = {"limit": limit}
if cursor:
params["cursor"] = ensure_agent_name(cursor)
response = await client.http_client.get("/api/v1/agents", params=params)
response.raise_for_status()
return cast(dict[str, Any], response.json())
async def get_agent_policies(
client: AgentControlClient,
agent_name: str,
) -> dict[str, Any]:
"""List policy IDs associated with an agent."""
normalized_name = ensure_agent_name(agent_name)
response = await client.http_client.get(f"/api/v1/agents/{normalized_name}/policies")
response.raise_for_status()
return cast(dict[str, Any], response.json())
async def add_agent_policy(
client: AgentControlClient,
agent_name: str,
policy_id: int,
) -> dict[str, Any]:
"""Associate a policy with an agent (additive, idempotent)."""
normalized_name = ensure_agent_name(agent_name)
response = await client.http_client.post(
f"/api/v1/agents/{normalized_name}/policies/{policy_id}"
)
response.raise_for_status()
return cast(dict[str, Any], response.json())
async def remove_agent_policy_association(
client: AgentControlClient,
agent_name: str,
policy_id: int,
) -> dict[str, Any]:
"""Remove one policy association from an agent (idempotent)."""
normalized_name = ensure_agent_name(agent_name)
response = await client.http_client.delete(
f"/api/v1/agents/{normalized_name}/policies/{policy_id}"
)
response.raise_for_status()
return cast(dict[str, Any], response.json())
async def remove_all_agent_policies(
client: AgentControlClient,
agent_name: str,
) -> dict[str, Any]:
"""Remove all policy associations from an agent."""
normalized_name = ensure_agent_name(agent_name)
response = await client.http_client.delete(f"/api/v1/agents/{normalized_name}/policies")
response.raise_for_status()
return cast(dict[str, Any], response.json())
async def get_agent_policy(
client: AgentControlClient,
agent_name: str,
) -> dict[str, Any]:
"""Get the primary policy assigned to an agent (compatibility endpoint)."""
normalized_name = ensure_agent_name(agent_name)
response = await client.http_client.get(f"/api/v1/agents/{normalized_name}/policy")
response.raise_for_status()
return cast(dict[str, Any], response.json())
async def remove_agent_policy(
client: AgentControlClient,
agent_name: str,
) -> dict[str, Any]:
"""Remove all policy associations via singular compatibility endpoint."""
normalized_name = ensure_agent_name(agent_name)
response = await client.http_client.delete(f"/api/v1/agents/{normalized_name}/policy")
response.raise_for_status()
return cast(dict[str, Any], response.json())
async def add_agent_control(
client: AgentControlClient,
agent_name: str,
control_id: int,
) -> dict[str, Any]:
"""Associate a control directly with an agent (idempotent)."""
normalized_name = ensure_agent_name(agent_name)
response = await client.http_client.post(
f"/api/v1/agents/{normalized_name}/controls/{control_id}"
)
response.raise_for_status()
return cast(dict[str, Any], response.json())
async def remove_agent_control(
client: AgentControlClient,
agent_name: str,
control_id: int,
) -> dict[str, Any]:
"""Remove a direct control association from an agent (idempotent)."""
normalized_name = ensure_agent_name(agent_name)
response = await client.http_client.delete(
f"/api/v1/agents/{normalized_name}/controls/{control_id}"
)
response.raise_for_status()
return cast(dict[str, Any], response.json())
async def list_agent_controls(
client: AgentControlClient,
agent_name: str,
) -> dict[str, Any]:
"""List active controls associated with an agent."""
normalized_name = ensure_agent_name(agent_name)
response = await client.http_client.get(f"/api/v1/agents/{normalized_name}/controls")
response.raise_for_status()
return cast(dict[str, Any], response.json())
async def list_agent_controls_typed(
client: AgentControlClient,
agent_name: str,
) -> AgentControlsResponse:
"""List active controls associated with an agent (typed response)."""
normalized_name = ensure_agent_name(agent_name)
response = await client.http_client.get(f"/api/v1/agents/{normalized_name}/controls")
response.raise_for_status()
return AgentControlsResponse.model_validate(response.json())