|
22 | 22 | from veadk.a2a.registry_client import ( |
23 | 23 | AgentKitA2ARegistryConfig, |
24 | 24 | RegistryError, |
| 25 | + _OAUTH_TOKEN_CACHE, |
25 | 26 | _agent_auth_headers, |
26 | 27 | _volc_sign_v4, |
27 | 28 | create_task, |
@@ -68,6 +69,41 @@ def _agent_card() -> dict: |
68 | 69 | } |
69 | 70 |
|
70 | 71 |
|
| 72 | +def _oauth_agent_card() -> dict: |
| 73 | + token_url = ( |
| 74 | + "https://userpool-61597ac7-4bcb-4acf-a1d8-fdbfb95333ad." |
| 75 | + "userpool.auth.id.cn-beijing.volces.com/oauth/token" |
| 76 | + ) |
| 77 | + return { |
| 78 | + "name": "Finance Policy Remote Agent", |
| 79 | + "description": "Finance policy agent", |
| 80 | + "version": "1.0.0", |
| 81 | + "url": " https://oauth-agent.test/a2a/ ", |
| 82 | + "security": [{"oauth2": []}], |
| 83 | + "securitySchemes": { |
| 84 | + "oauth2": { |
| 85 | + "type": "oauth2", |
| 86 | + "description": "OAuth2 client credentials flow", |
| 87 | + "flows": { |
| 88 | + "clientCredentials": { |
| 89 | + "tokenUrl": f" `{token_url}` ", |
| 90 | + "refreshUrl": f" `{token_url}` ", |
| 91 | + "scopes": {}, |
| 92 | + } |
| 93 | + }, |
| 94 | + } |
| 95 | + }, |
| 96 | + "skills": [ |
| 97 | + { |
| 98 | + "id": "finance-policy", |
| 99 | + "name": "Finance policy", |
| 100 | + "description": "Answer finance policy questions", |
| 101 | + "tags": ["finance", "policy"], |
| 102 | + } |
| 103 | + ], |
| 104 | + } |
| 105 | + |
| 106 | + |
71 | 107 | @patch.dict( |
72 | 108 | "os.environ", |
73 | 109 | { |
@@ -190,6 +226,84 @@ def test_create_task_gets_agent_and_sends_message(post: Mock): |
190 | 226 | assert "Authorization" not in serialized |
191 | 227 |
|
192 | 228 |
|
| 229 | +@patch.dict( |
| 230 | + "os.environ", |
| 231 | + { |
| 232 | + "AGENTKIT_ACCESS_KEY": "ak-test", |
| 233 | + "AGENTKIT_SECRET_KEY": "sk-test", |
| 234 | + }, |
| 235 | + clear=False, |
| 236 | +) |
| 237 | +@patch("veadk.a2a.registry_client.requests.post") |
| 238 | +def test_create_task_gets_oauth_agent_token_and_sends_message(post: Mock): |
| 239 | + _OAUTH_TOKEN_CACHE.clear() |
| 240 | + card = _oauth_agent_card() |
| 241 | + post.side_effect = [ |
| 242 | + _mock_response( |
| 243 | + { |
| 244 | + "ResponseMetadata": {"RequestId": "get-req"}, |
| 245 | + "Result": { |
| 246 | + "Id": "agent-id", |
| 247 | + "Status": "running", |
| 248 | + "AgentCard": json.dumps(card), |
| 249 | + }, |
| 250 | + } |
| 251 | + ), |
| 252 | + _mock_response( |
| 253 | + { |
| 254 | + "ResponseMetadata": {"RequestId": "list-client-req"}, |
| 255 | + "Result": {"Data": [{"Uid": "m2m-client-id"}]}, |
| 256 | + } |
| 257 | + ), |
| 258 | + _mock_response( |
| 259 | + { |
| 260 | + "ResponseMetadata": {"RequestId": "get-client-req"}, |
| 261 | + "Result": {"ClientSecret": "m2m-client-secret"}, |
| 262 | + } |
| 263 | + ), |
| 264 | + _mock_response({"access_token": "oauth-access-token", "expires_in": 3600}), |
| 265 | + _mock_response( |
| 266 | + { |
| 267 | + "result": { |
| 268 | + "kind": "message", |
| 269 | + "parts": [{"kind": "text", "text": "需要财务审批。"}], |
| 270 | + } |
| 271 | + } |
| 272 | + ), |
| 273 | + ] |
| 274 | + |
| 275 | + result = create_task( |
| 276 | + "Finance Policy Remote Agent", |
| 277 | + "这笔支出是否需要审批?", |
| 278 | + config=AgentKitA2ARegistryConfig( |
| 279 | + space_id="space-test", |
| 280 | + endpoint="https://open.volcengineapi.com/", |
| 281 | + ), |
| 282 | + ) |
| 283 | + |
| 284 | + assert result["outcome"] == "success" |
| 285 | + assert result["selected_agent"]["name"] == "Finance Policy Remote Agent" |
| 286 | + assert result["response"]["text"] == "需要财务审批。" |
| 287 | + |
| 288 | + assert post.call_args_list[0].kwargs["params"]["Action"] == "GetA2aAgent" |
| 289 | + assert post.call_args_list[1].kwargs["params"]["Action"] == "ListUserPoolClients" |
| 290 | + assert post.call_args_list[1].kwargs["params"]["Version"] == "2025-10-30" |
| 291 | + assert post.call_args_list[2].kwargs["params"]["Action"] == "GetUserPoolClient" |
| 292 | + assert post.call_args_list[2].kwargs["params"]["Version"] == "2025-10-30" |
| 293 | + assert post.call_args_list[3].args[0].endswith("/oauth/token") |
| 294 | + assert ( |
| 295 | + post.call_args_list[3].kwargs["headers"]["Authorization"].startswith("Basic ") |
| 296 | + ) |
| 297 | + assert post.call_args_list[4].args[0] == "https://oauth-agent.test/a2a/" |
| 298 | + assert post.call_args_list[4].kwargs["headers"]["Authorization"] == ( |
| 299 | + "Bearer oauth-access-token" |
| 300 | + ) |
| 301 | + |
| 302 | + serialized = json.dumps(result, ensure_ascii=False) |
| 303 | + assert "oauth-access-token" not in serialized |
| 304 | + assert "m2m-client-secret" not in serialized |
| 305 | + |
| 306 | + |
193 | 307 | @patch.dict( |
194 | 308 | "os.environ", |
195 | 309 | { |
|
0 commit comments