File size: 2,739 Bytes
2a7f679
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from __future__ import annotations
import hashlib
import json
import os
from datetime import datetime, timezone
from typing import Any, Dict, Optional

import httpx

from .models import A2ACall


MOCK_AGENTS = {
    "source-verifier-mock": {
        "url": None,  # None = use local mock
        "capabilities": ["verify_sources"],
    },
    "security-auditor-mock": {
        "url": None,
        "capabilities": ["scan_dependencies"],
    },
}


async def call_cap_agent(
    provider_agent: str,
    capability: str,
    payload: Dict[str, Any],
    timeout: int = 60,
) -> A2ACall:
    """Call another CAP agent. Falls back to local mock if no URL configured."""
    call = A2ACall(
        provider_agent=provider_agent,
        cap_order_id=f"mock_order_{hashlib.md5(json.dumps(payload, sort_keys=True).encode()).hexdigest()[:8]}",
        task=capability,
        status="pending",
    )

    agent_info = MOCK_AGENTS.get(provider_agent, {})
    agent_url = agent_info.get("url") or os.getenv(f"A2A_{provider_agent.upper().replace('-', '_')}_URL")

    if agent_url:
        try:
            async with httpx.AsyncClient(timeout=timeout) as client:
                resp = await client.post(f"{agent_url}/jobs/{capability}", json=payload)
                resp.raise_for_status()
                result = resp.json()
                call.result_hash = result.get("result_hash", "")
                call.status = "cleared"
                call.completed_at = datetime.now(timezone.utc)
        except Exception as e:
            call.status = f"failed: {e}"
            call.completed_at = datetime.now(timezone.utc)
    else:
        # Local mock: simulate a successful call
        mock_result = _mock_agent_result(provider_agent, capability, payload)
        call.result_hash = f"sha256:{hashlib.sha256(json.dumps(mock_result, sort_keys=True).encode()).hexdigest()}"
        call.status = "cleared"
        call.completed_at = datetime.now(timezone.utc)

    return call


def _mock_agent_result(provider_agent: str, capability: str, payload: Dict[str, Any]) -> Dict[str, Any]:
    """Return a mock result for development/demo purposes."""
    if "source-verifier" in provider_agent:
        return {
            "verified_urls": payload.get("evidence_urls", []),
            "reachable": len(payload.get("evidence_urls", [])),
            "summary": "All provided URLs are reachable and contain relevant content.",
        }
    elif "security-auditor" in provider_agent:
        return {
            "dependencies_scanned": 0,
            "vulnerabilities": [],
            "summary": "No known vulnerabilities detected in dependency list.",
        }
    return {"status": "ok", "message": f"Mock result from {provider_agent}"}