This repository was archived by the owner on Oct 26, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathclient.py
More file actions
244 lines (222 loc) · 7.65 KB
/
Copy pathclient.py
File metadata and controls
244 lines (222 loc) · 7.65 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
"""
B2A Client — Framework Integration Core
======================================
HTTP client for Agent Middleware API.
"""
from dataclasses import dataclass
from typing import Any, Optional
import httpx
@dataclass
class B2AConfig:
"""Configuration for B2A client."""
api_url: str = "http://localhost:8000"
api_key: str = ""
wallet_id: str = ""
timeout: int = 30
class B2AClient:
"""
Client for Agent Middleware API.
Provides methods for:
- Billing operations
- Telemetry
- Agent communication
- AI decision making
- MCP tool discovery and execution
- AWI session management
"""
def __init__(self, config: Optional[B2AConfig] = None, **kwargs):
if config is None:
config = B2AConfig(**kwargs)
self.config = config
self._client = httpx.AsyncClient(timeout=config.timeout)
def _headers(self) -> dict[str, str]:
return {
"X-API-Key": self.config.api_key,
"Content-Type": "application/json",
}
async def close(self):
await self._client.aclose()
async def get_balance(self) -> float:
"""Get current wallet balance."""
response = await self._client.get(
f"{self.config.api_url}/v1/billing/wallets/{self.config.wallet_id}",
headers=self._headers(),
)
response.raise_for_status()
data = response.json()
return float(data.get("balance", 0))
async def emit_telemetry(
self,
event: str,
properties: Optional[dict[str, Any]] = None,
) -> dict[str, Any]:
"""Emit a telemetry event."""
payload = {
"event": event,
"agent_id": self.config.wallet_id,
"properties": properties or {},
}
response = await self._client.post(
f"{self.config.api_url}/v1/telemetry/events",
headers=self._headers(),
json=payload,
)
response.raise_for_status()
return response.json()
async def send_message(
self,
to_agent_id: str,
content: dict[str, Any],
priority: str = "normal",
) -> dict[str, Any]:
"""Send a message to another agent."""
payload = {
"from_agent_id": self.config.wallet_id,
"to_agent_id": to_agent_id,
"content": content,
"priority": priority,
}
response = await self._client.post(
f"{self.config.api_url}/v1/comms/messages",
headers=self._headers(),
json=payload,
)
response.raise_for_status()
return response.json()
async def decide(
self,
context: dict[str, Any],
options: list[str],
) -> str:
"""Make an AI-powered decision."""
payload = {
"agent_id": self.config.wallet_id,
"context": context,
"options": options,
}
response = await self._client.post(
f"{self.config.api_url}/v1/ai/decide",
headers=self._headers(),
json=payload,
)
response.raise_for_status()
data = response.json()
return data.get("decision", options[0])
async def heal(self, issue: str, context: dict[str, Any]) -> dict[str, Any]:
"""AI-powered self-healing diagnostics."""
payload = {
"issue": issue,
"context": context,
}
response = await self._client.post(
f"{self.config.api_url}/v1/ai/heal",
headers=self._headers(),
json=payload,
)
response.raise_for_status()
return response.json()
async def create_awi_session(
self,
target_url: str,
max_steps: int = 100,
) -> dict[str, Any]:
"""Create an AWI session for web automation."""
payload = {
"target_url": target_url,
"max_steps": max_steps,
}
response = await self._client.post(
f"{self.config.api_url}/v1/awi/sessions",
headers=self._headers(),
json=payload,
)
response.raise_for_status()
return response.json()
async def execute_awi_action(
self,
session_id: str,
action: str,
parameters: dict[str, Any],
*,
permit_id: str,
idempotency_key: str,
) -> dict[str, Any]:
"""Call the proof-only AWI route with its required governance headers.
Reuse the caller-owned idempotency key verbatim for one logical action.
The server remains responsible for authorization and accounting.
"""
if not isinstance(permit_id, str) or not permit_id.strip():
raise ValueError("permit_id must not be blank")
if not isinstance(idempotency_key, str) or not idempotency_key.strip():
raise ValueError("idempotency_key must not be blank")
if len(idempotency_key) > 128:
raise ValueError("idempotency_key must be at most 128 characters")
payload = {
"session_id": session_id,
"action": action,
"parameters": parameters,
}
response = await self._client.post(
f"{self.config.api_url}/v1/awi/execute",
headers={
**self._headers(),
"X-Permit-Id": permit_id,
"Idempotency-Key": idempotency_key,
},
json=payload,
)
response.raise_for_status()
return response.json()
async def get_mcp_tools(self) -> list[dict[str, Any]]:
"""Get available MCP tools."""
response = await self._client.get(
f"{self.config.api_url}/mcp/tools.json",
headers=self._headers(),
)
response.raise_for_status()
data = response.json()
return data.get("tools", [])
async def discover(self) -> dict[str, Any]:
"""Get the discovery manifest."""
response = await self._client.get(
f"{self.config.api_url}/v1/discover",
headers=self._headers(),
)
response.raise_for_status()
return response.json()
async def charge(
self,
service_category: str,
units: float = 1.0,
description: str = "",
*,
idempotency_key: str,
request_path: Optional[str] = None,
) -> dict[str, Any]:
"""Charge the configured wallet for metered usage.
Follows the ``POST /v1/billing/charge`` contract: the wallet, service
category and units travel as query parameters, and the server prices
the units (a caller cannot name an amount). ``idempotency_key`` is
required and caller-owned: retrying with the same key replays the
original outcome instead of debiting the wallet a second time.
"""
# The server treats an empty Idempotency-Key as "no key", which would
# silently drop the double-charge protection, so refuse it here.
if not idempotency_key or not idempotency_key.strip():
raise ValueError("idempotency_key is required and must not be blank")
params: dict[str, str | float] = {
"wallet_id": self.config.wallet_id,
"service": service_category,
"units": units,
}
if description:
params["description"] = description
if request_path:
params["request_path"] = request_path
response = await self._client.post(
f"{self.config.api_url}/v1/billing/charge",
headers={**self._headers(), "Idempotency-Key": idempotency_key},
params=params,
)
response.raise_for_status()
return response.json()