appecosystem 0.3.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- appecosystem-0.3.0.dist-info/METADATA +103 -0
- appecosystem-0.3.0.dist-info/RECORD +30 -0
- appecosystem-0.3.0.dist-info/WHEEL +5 -0
- appecosystem-0.3.0.dist-info/entry_points.txt +2 -0
- appecosystem-0.3.0.dist-info/licenses/LICENSE +21 -0
- appecosystem-0.3.0.dist-info/top_level.txt +4 -0
- cli/__init__.py +1 -0
- cli/__main__.py +4 -0
- cli/commands.py +635 -0
- cli/main.py +99 -0
- events/__init__.py +6 -0
- events/event_bus.py +137 -0
- events/event_types.py +48 -0
- events/schemas.py +40 -0
- llm/__init__.py +1 -0
- llm/command_router.py +188 -0
- llm/discovery_agent.py +107 -0
- llm/project_manifest.py +135 -0
- registry/__init__.py +1 -0
- registry/app.py +556 -0
- registry/app_store.py +153 -0
- registry/credential_store.py +160 -0
- registry/discovery.py +112 -0
- registry/health_monitor.py +182 -0
- registry/logging_config.py +49 -0
- registry/metrics.py +86 -0
- registry/models.py +73 -0
- registry/placement.py +54 -0
- registry/profile_store.py +109 -0
- registry/registry.py +153 -0
cli/main.py
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
"""CLI entry point: python -m ecosystem"""
|
|
2
|
+
|
|
3
|
+
import argparse
|
|
4
|
+
import sys
|
|
5
|
+
|
|
6
|
+
from .commands import (
|
|
7
|
+
cmd_apps, cmd_install, cmd_logs, cmd_monitor, cmd_partner, cmd_provider,
|
|
8
|
+
cmd_restart, cmd_secret, cmd_start, cmd_start_all, cmd_status, cmd_stop,
|
|
9
|
+
cmd_stop_all, cmd_uninstall,
|
|
10
|
+
)
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def main():
|
|
14
|
+
parser = argparse.ArgumentParser(
|
|
15
|
+
prog="ecosystem",
|
|
16
|
+
description="App Ecosystem management CLI",
|
|
17
|
+
)
|
|
18
|
+
sub = parser.add_subparsers(dest="command", help="Available commands")
|
|
19
|
+
|
|
20
|
+
sub.add_parser("start", help="Start the ecosystem registry only")
|
|
21
|
+
sub.add_parser("stop", help="Stop the ecosystem registry only")
|
|
22
|
+
sub.add_parser("restart", help="Restart the ecosystem registry")
|
|
23
|
+
sub.add_parser("start-all", help="Start registry and all projects")
|
|
24
|
+
sub.add_parser("stop-all", help="Stop all projects and registry")
|
|
25
|
+
sub.add_parser("status", help="Show health status of all services")
|
|
26
|
+
|
|
27
|
+
monitor_parser = sub.add_parser("monitor", help="Live health dashboard")
|
|
28
|
+
monitor_parser.add_argument("-i", "--interval", type=int, default=5,
|
|
29
|
+
help="Refresh interval in seconds (default: 5)")
|
|
30
|
+
monitor_parser.add_argument("--once", action="store_true",
|
|
31
|
+
help="Render a single snapshot and exit")
|
|
32
|
+
|
|
33
|
+
logs_parser = sub.add_parser("logs", help="View project logs")
|
|
34
|
+
logs_parser.add_argument("project", nargs="?", default=None,
|
|
35
|
+
help="Project key (omit to list available logs)")
|
|
36
|
+
logs_parser.add_argument("-n", "--lines", type=int, default=50,
|
|
37
|
+
help="Number of lines to show (default: 50)")
|
|
38
|
+
|
|
39
|
+
sub.add_parser("install", help="Install ecosystem as a system service (launchd)")
|
|
40
|
+
sub.add_parser("uninstall", help="Remove ecosystem system service")
|
|
41
|
+
|
|
42
|
+
apps_parser = sub.add_parser("apps", help="List which ecosystem apps are installed on this device")
|
|
43
|
+
apps_parser.add_argument("--json", action="store_true", dest="as_json",
|
|
44
|
+
help="Output the per-device app record as JSON")
|
|
45
|
+
|
|
46
|
+
secret_parser = sub.add_parser("secret", help="Manage the shared ecosystem HMAC secret")
|
|
47
|
+
secret_parser.add_argument("action", choices=["generate", "show", "import", "path"],
|
|
48
|
+
help="generate (if absent), show (to copy elsewhere), import <value>, path")
|
|
49
|
+
secret_parser.add_argument("value", nargs="?", default=None,
|
|
50
|
+
help="secret value (for 'import')")
|
|
51
|
+
|
|
52
|
+
provider_parser = sub.add_parser("provider", help="Manage cloud AI provider API keys")
|
|
53
|
+
provider_parser.add_argument("action", choices=["list", "set", "delete"])
|
|
54
|
+
provider_parser.add_argument("name", nargs="?", help="anthropic | openai | gemini")
|
|
55
|
+
provider_parser.add_argument("value", nargs="?", help="key (omit to be prompted without echo)")
|
|
56
|
+
|
|
57
|
+
partner_parser = sub.add_parser("partner", help="Manage third-party partner app credentials")
|
|
58
|
+
partner_parser.add_argument("action",
|
|
59
|
+
choices=["add", "list", "show", "suspend", "resume", "remove"])
|
|
60
|
+
partner_parser.add_argument("app_id", nargs="?", default=None)
|
|
61
|
+
partner_parser.add_argument("--name", default=None, help="Display name (add)")
|
|
62
|
+
partner_parser.add_argument("--owner", default=None, help="Owner contact (add)")
|
|
63
|
+
partner_parser.add_argument("--service-names", dest="service_names", default=None,
|
|
64
|
+
help="Comma-separated service names the app may manage (add)")
|
|
65
|
+
|
|
66
|
+
args = parser.parse_args()
|
|
67
|
+
|
|
68
|
+
commands = {
|
|
69
|
+
"start": cmd_start,
|
|
70
|
+
"stop": cmd_stop,
|
|
71
|
+
"restart": cmd_restart,
|
|
72
|
+
"start-all": cmd_start_all,
|
|
73
|
+
"stop-all": cmd_stop_all,
|
|
74
|
+
"status": cmd_status,
|
|
75
|
+
"install": cmd_install,
|
|
76
|
+
"uninstall": cmd_uninstall,
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
if args.command == "logs":
|
|
80
|
+
sys.exit(cmd_logs(args.project, args.lines))
|
|
81
|
+
elif args.command == "monitor":
|
|
82
|
+
sys.exit(cmd_monitor(args.interval, args.once))
|
|
83
|
+
elif args.command == "secret":
|
|
84
|
+
sys.exit(cmd_secret(args.action, args.value))
|
|
85
|
+
elif args.command == "apps":
|
|
86
|
+
sys.exit(cmd_apps(args.as_json))
|
|
87
|
+
elif args.command == "provider":
|
|
88
|
+
sys.exit(cmd_provider(args.action, args.name, args.value))
|
|
89
|
+
elif args.command == "partner":
|
|
90
|
+
sys.exit(cmd_partner(args.action, args.app_id, args.name, args.owner, args.service_names))
|
|
91
|
+
elif args.command in commands:
|
|
92
|
+
sys.exit(commands[args.command]())
|
|
93
|
+
else:
|
|
94
|
+
parser.print_help()
|
|
95
|
+
sys.exit(1)
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
if __name__ == "__main__":
|
|
99
|
+
main()
|
events/__init__.py
ADDED
events/event_bus.py
ADDED
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
"""Webhook-based event bus with HMAC signing, async fan-out, and retry."""
|
|
2
|
+
|
|
3
|
+
import asyncio
|
|
4
|
+
import logging
|
|
5
|
+
from typing import Optional
|
|
6
|
+
|
|
7
|
+
import httpx
|
|
8
|
+
|
|
9
|
+
from .schemas import EventEnvelope
|
|
10
|
+
|
|
11
|
+
logger = logging.getLogger(__name__)
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class EventBus:
|
|
15
|
+
"""
|
|
16
|
+
Publishes events to subscriber webhooks via HTTP POST.
|
|
17
|
+
|
|
18
|
+
Features:
|
|
19
|
+
- HMAC-SHA256 signing of payloads
|
|
20
|
+
- Async fan-out to all matching subscribers
|
|
21
|
+
- Retry with configurable attempts and delay
|
|
22
|
+
"""
|
|
23
|
+
|
|
24
|
+
def __init__(
|
|
25
|
+
self,
|
|
26
|
+
registry=None,
|
|
27
|
+
hmac_secret: Optional[str] = None,
|
|
28
|
+
retry_attempts: int = 3,
|
|
29
|
+
retry_delay: float = 5.0,
|
|
30
|
+
timeout: float = 10.0,
|
|
31
|
+
):
|
|
32
|
+
from ecosystem_auth.tokens import get_ecosystem_secret
|
|
33
|
+
|
|
34
|
+
self.registry = registry
|
|
35
|
+
self.hmac_secret = get_ecosystem_secret(hmac_secret)
|
|
36
|
+
self.retry_attempts = retry_attempts
|
|
37
|
+
self.retry_delay = retry_delay
|
|
38
|
+
self.timeout = timeout
|
|
39
|
+
self._client = httpx.AsyncClient(timeout=timeout)
|
|
40
|
+
|
|
41
|
+
async def close(self) -> None:
|
|
42
|
+
"""Close the persistent HTTP connection pool."""
|
|
43
|
+
await self._client.aclose()
|
|
44
|
+
|
|
45
|
+
async def publish(self, event: EventEnvelope) -> dict:
|
|
46
|
+
"""
|
|
47
|
+
Publish an event to all subscribed services.
|
|
48
|
+
|
|
49
|
+
Signs the event payload and delivers via HTTP POST to each
|
|
50
|
+
subscriber's webhook_url. Returns delivery results.
|
|
51
|
+
"""
|
|
52
|
+
# Sign the event
|
|
53
|
+
from ecosystem_auth.tokens import sign_payload
|
|
54
|
+
|
|
55
|
+
event.signature = sign_payload(event.signable_dict(), self.hmac_secret)
|
|
56
|
+
|
|
57
|
+
# Find subscribers
|
|
58
|
+
if not self.registry:
|
|
59
|
+
logger.warning("No registry attached - cannot deliver events")
|
|
60
|
+
return {"delivered": 0, "failed": 0, "subscribers": []}
|
|
61
|
+
|
|
62
|
+
subscribers = self.registry.get_subscribers(event.type)
|
|
63
|
+
if not subscribers:
|
|
64
|
+
logger.debug(f"No subscribers for event type: {event.type}")
|
|
65
|
+
return {"delivered": 0, "failed": 0, "subscribers": []}
|
|
66
|
+
|
|
67
|
+
# Fan out delivery
|
|
68
|
+
tasks = [
|
|
69
|
+
self._deliver(event, sub.webhook_url, sub.name)
|
|
70
|
+
for sub in subscribers
|
|
71
|
+
if sub.webhook_url
|
|
72
|
+
]
|
|
73
|
+
results = await asyncio.gather(*tasks, return_exceptions=True)
|
|
74
|
+
|
|
75
|
+
delivered = sum(1 for r in results if r is True)
|
|
76
|
+
failed = len(results) - delivered
|
|
77
|
+
|
|
78
|
+
self._record_metrics(delivered, failed)
|
|
79
|
+
|
|
80
|
+
if failed:
|
|
81
|
+
logger.warning(
|
|
82
|
+
f"Event {event.type} delivery: {delivered} ok, {failed} failed"
|
|
83
|
+
)
|
|
84
|
+
|
|
85
|
+
return {
|
|
86
|
+
"delivered": delivered,
|
|
87
|
+
"failed": failed,
|
|
88
|
+
"subscribers": [s.name for s in subscribers],
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
@staticmethod
|
|
92
|
+
def _record_metrics(delivered: int, failed: int) -> None:
|
|
93
|
+
"""Record delivery metrics. Soft dependency: no-op if registry.metrics
|
|
94
|
+
is unavailable so the event bus stays usable standalone."""
|
|
95
|
+
try:
|
|
96
|
+
from registry import metrics
|
|
97
|
+
except Exception:
|
|
98
|
+
return
|
|
99
|
+
metrics.inc(metrics.EVENTS_PUBLISHED)
|
|
100
|
+
metrics.inc(metrics.EVENTS_DELIVERED, delivered)
|
|
101
|
+
metrics.inc(metrics.EVENTS_FAILED, failed)
|
|
102
|
+
|
|
103
|
+
async def _deliver(
|
|
104
|
+
self, event: EventEnvelope, webhook_url: str, service_name: str
|
|
105
|
+
) -> bool:
|
|
106
|
+
"""Deliver an event to a single webhook with retry."""
|
|
107
|
+
payload = event.model_dump()
|
|
108
|
+
headers = {
|
|
109
|
+
"Content-Type": "application/json",
|
|
110
|
+
"X-Ecosystem-Signature": event.signature or "",
|
|
111
|
+
"X-Ecosystem-Event": event.type,
|
|
112
|
+
"X-Ecosystem-Source": event.source,
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
for attempt in range(1, self.retry_attempts + 1):
|
|
116
|
+
try:
|
|
117
|
+
resp = await self._client.post(webhook_url, json=payload, headers=headers)
|
|
118
|
+
if resp.status_code < 400:
|
|
119
|
+
logger.debug(
|
|
120
|
+
f"Delivered {event.type} to {service_name} (attempt {attempt})"
|
|
121
|
+
)
|
|
122
|
+
return True
|
|
123
|
+
logger.warning(
|
|
124
|
+
f"Webhook {service_name} returned {resp.status_code} (attempt {attempt})"
|
|
125
|
+
)
|
|
126
|
+
except Exception as e:
|
|
127
|
+
logger.warning(
|
|
128
|
+
f"Webhook delivery to {service_name} failed (attempt {attempt}): {e}"
|
|
129
|
+
)
|
|
130
|
+
|
|
131
|
+
if attempt < self.retry_attempts:
|
|
132
|
+
await asyncio.sleep(self.retry_delay)
|
|
133
|
+
|
|
134
|
+
logger.error(
|
|
135
|
+
f"Failed to deliver {event.type} to {service_name} after {self.retry_attempts} attempts"
|
|
136
|
+
)
|
|
137
|
+
return False
|
events/event_types.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"""Canonical event types for the ecosystem event bus."""
|
|
2
|
+
|
|
3
|
+
from enum import Enum
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class EventType(str, Enum):
|
|
7
|
+
"""
|
|
8
|
+
All ecosystem event types follow a dot-delimited namespace convention:
|
|
9
|
+
<domain>.<action>
|
|
10
|
+
|
|
11
|
+
Projects publish events under their domain; the event bus fans out
|
|
12
|
+
to all subscribers registered for that event type (or wildcard).
|
|
13
|
+
"""
|
|
14
|
+
|
|
15
|
+
# Security events (OpenEye, AegisSIEM)
|
|
16
|
+
SECURITY_ALERT = "security.alert"
|
|
17
|
+
SECURITY_MOTION_DETECTED = "security.motion_detected"
|
|
18
|
+
SECURITY_PERSON_DETECTED = "security.person_detected"
|
|
19
|
+
SECURITY_INTRUSION = "security.intrusion"
|
|
20
|
+
SECURITY_THREAT_BLOCKED = "security.threat_blocked"
|
|
21
|
+
|
|
22
|
+
# System events (any project)
|
|
23
|
+
SYSTEM_STARTUP = "system.startup"
|
|
24
|
+
SYSTEM_SHUTDOWN = "system.shutdown"
|
|
25
|
+
SYSTEM_HEALTH_CHANGED = "system.health_changed"
|
|
26
|
+
SYSTEM_ERROR = "system.error"
|
|
27
|
+
SYSTEM_CONFIG_CHANGED = "system.config_changed"
|
|
28
|
+
|
|
29
|
+
# Network events (AegisSIEM)
|
|
30
|
+
NETWORK_DEVICE_CONNECTED = "network.device_connected"
|
|
31
|
+
NETWORK_DEVICE_DISCONNECTED = "network.device_disconnected"
|
|
32
|
+
NETWORK_ANOMALY = "network.anomaly"
|
|
33
|
+
NETWORK_BANDWIDTH_ALERT = "network.bandwidth_alert"
|
|
34
|
+
|
|
35
|
+
# Display events (MagicMirror)
|
|
36
|
+
DISPLAY_UPDATE = "display.update"
|
|
37
|
+
DISPLAY_ALERT = "display.alert"
|
|
38
|
+
DISPLAY_WIDGET_CHANGED = "display.widget_changed"
|
|
39
|
+
|
|
40
|
+
# AI events (AI_For_Survival)
|
|
41
|
+
AI_ANALYSIS_COMPLETE = "ai.analysis_complete"
|
|
42
|
+
AI_RECOMMENDATION = "ai.recommendation"
|
|
43
|
+
AI_PLAYBOOK_TRIGGERED = "ai.playbook_triggered"
|
|
44
|
+
|
|
45
|
+
# Ecosystem events (registry)
|
|
46
|
+
ECOSYSTEM_SERVICE_REGISTERED = "ecosystem.service_registered"
|
|
47
|
+
ECOSYSTEM_SERVICE_DEREGISTERED = "ecosystem.service_deregistered"
|
|
48
|
+
ECOSYSTEM_SERVICE_UNHEALTHY = "ecosystem.service_unhealthy"
|
events/schemas.py
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"""Event envelope schema for the ecosystem event bus."""
|
|
2
|
+
|
|
3
|
+
import time
|
|
4
|
+
import uuid
|
|
5
|
+
from typing import Any, Optional
|
|
6
|
+
|
|
7
|
+
from pydantic import BaseModel, Field
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class EventEnvelope(BaseModel):
|
|
11
|
+
"""
|
|
12
|
+
Standard event envelope wrapping all ecosystem events.
|
|
13
|
+
|
|
14
|
+
Every event published through the bus is wrapped in this envelope
|
|
15
|
+
to provide consistent metadata, tracing, and signature verification.
|
|
16
|
+
"""
|
|
17
|
+
|
|
18
|
+
id: str = Field(default_factory=lambda: str(uuid.uuid4()))
|
|
19
|
+
type: str = Field(..., description="Event type from EventType enum (e.g., 'security.alert')")
|
|
20
|
+
source: str = Field(..., description="Service name that published the event")
|
|
21
|
+
timestamp: float = Field(default_factory=time.time)
|
|
22
|
+
data: dict[str, Any] = Field(default_factory=dict, description="Event payload")
|
|
23
|
+
correlation_id: Optional[str] = Field(
|
|
24
|
+
default=None,
|
|
25
|
+
description="Optional ID linking related events across services",
|
|
26
|
+
)
|
|
27
|
+
signature: Optional[str] = Field(
|
|
28
|
+
default=None,
|
|
29
|
+
description="HMAC-SHA256 signature of the data payload",
|
|
30
|
+
)
|
|
31
|
+
|
|
32
|
+
def signable_dict(self) -> dict:
|
|
33
|
+
"""Return the dict that should be signed (excludes the signature itself)."""
|
|
34
|
+
return {
|
|
35
|
+
"id": self.id,
|
|
36
|
+
"type": self.type,
|
|
37
|
+
"source": self.source,
|
|
38
|
+
"timestamp": self.timestamp,
|
|
39
|
+
"data": self.data,
|
|
40
|
+
}
|
llm/__init__.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"""LLM integration layer for cross-project AI coordination."""
|
llm/command_router.py
ADDED
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
"""Routes LLM commands/intents to project API calls."""
|
|
2
|
+
|
|
3
|
+
import logging
|
|
4
|
+
import os
|
|
5
|
+
import time
|
|
6
|
+
from typing import Any, Optional
|
|
7
|
+
from urllib.parse import quote
|
|
8
|
+
|
|
9
|
+
import httpx
|
|
10
|
+
|
|
11
|
+
from .discovery_agent import DiscoveryAgent
|
|
12
|
+
from .project_manifest import ProjectManifest
|
|
13
|
+
|
|
14
|
+
logger = logging.getLogger(__name__)
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class CommandRouter:
|
|
18
|
+
"""
|
|
19
|
+
Translates high-level LLM intents into concrete API calls
|
|
20
|
+
against ecosystem services.
|
|
21
|
+
|
|
22
|
+
The LLM produces intents like:
|
|
23
|
+
{"action": "check_cameras", "project": "openeye"}
|
|
24
|
+
|
|
25
|
+
The router resolves this to the correct HTTP call using
|
|
26
|
+
the project's capability manifest.
|
|
27
|
+
"""
|
|
28
|
+
|
|
29
|
+
_HARNESS_CAPABILITIES = frozenset({
|
|
30
|
+
"threat_analysis", "log_analysis", "security_scan",
|
|
31
|
+
"incident_triage", "block_ip", "security_status",
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
@staticmethod
|
|
35
|
+
def _default_harness_url() -> str:
|
|
36
|
+
"""Resolve the harness daemon URL from the environment (evaluated per
|
|
37
|
+
instance, not at import time). ECOSYSTEM_HARNESS_URL wins; otherwise it
|
|
38
|
+
is built from AEGISSIEM_PORT."""
|
|
39
|
+
explicit = os.environ.get("ECOSYSTEM_HARNESS_URL")
|
|
40
|
+
if explicit:
|
|
41
|
+
return explicit.rstrip("/")
|
|
42
|
+
return f"http://localhost:{os.environ.get('AEGISSIEM_PORT', '8088')}"
|
|
43
|
+
|
|
44
|
+
def __init__(
|
|
45
|
+
self,
|
|
46
|
+
discovery: Optional[DiscoveryAgent] = None,
|
|
47
|
+
hmac_secret: Optional[str] = None,
|
|
48
|
+
harness_url: Optional[str] = None,
|
|
49
|
+
):
|
|
50
|
+
from ecosystem_auth.tokens import get_ecosystem_secret
|
|
51
|
+
|
|
52
|
+
self.discovery = discovery or DiscoveryAgent()
|
|
53
|
+
self.hmac_secret = get_ecosystem_secret(hmac_secret)
|
|
54
|
+
self._harness_base_url = harness_url or self._default_harness_url()
|
|
55
|
+
self._manifests: dict[str, ProjectManifest] = {}
|
|
56
|
+
self._harness_ok: bool | None = None
|
|
57
|
+
self._harness_checked_at: float = 0
|
|
58
|
+
self._harness_cache_ttl: float = 30.0
|
|
59
|
+
# Single shared, persistent connection pool. Per-request timeouts are
|
|
60
|
+
# passed at call sites so one client covers fast probes and slow analyses.
|
|
61
|
+
self._client = httpx.AsyncClient(timeout=10.0)
|
|
62
|
+
|
|
63
|
+
async def aclose(self) -> None:
|
|
64
|
+
"""Close the shared HTTP connection pool."""
|
|
65
|
+
await self._client.aclose()
|
|
66
|
+
|
|
67
|
+
async def refresh_manifests(self) -> None:
|
|
68
|
+
"""Refresh the cached capability manifests from the registry."""
|
|
69
|
+
manifests = await self.discovery.discover_all()
|
|
70
|
+
self._manifests = {m.name.lower(): m for m in manifests}
|
|
71
|
+
logger.info(f"Refreshed {len(self._manifests)} project manifests")
|
|
72
|
+
|
|
73
|
+
async def route(
|
|
74
|
+
self,
|
|
75
|
+
project: str,
|
|
76
|
+
capability: str,
|
|
77
|
+
method: str = "GET",
|
|
78
|
+
path_params: Optional[dict] = None,
|
|
79
|
+
body: Optional[dict] = None,
|
|
80
|
+
) -> dict[str, Any]:
|
|
81
|
+
"""
|
|
82
|
+
Route a command to the appropriate project API.
|
|
83
|
+
|
|
84
|
+
Args:
|
|
85
|
+
project: Project name (e.g., 'openeye')
|
|
86
|
+
capability: Capability name (e.g., 'camera_monitoring')
|
|
87
|
+
method: HTTP method
|
|
88
|
+
path_params: URL path parameters to substitute
|
|
89
|
+
body: Request body for POST/PUT
|
|
90
|
+
|
|
91
|
+
Returns:
|
|
92
|
+
Dict with status, data, and any error info.
|
|
93
|
+
"""
|
|
94
|
+
# Try cyber-claude-harness for security capabilities
|
|
95
|
+
if capability in self._HARNESS_CAPABILITIES and await self._harness_available():
|
|
96
|
+
result = await self._route_via_harness(project, capability, body or {})
|
|
97
|
+
if result.get("status") == "ok":
|
|
98
|
+
return result
|
|
99
|
+
logger.debug("Harness routing failed for %s/%s, falling back to direct API", project, capability)
|
|
100
|
+
|
|
101
|
+
if not self._manifests:
|
|
102
|
+
await self.refresh_manifests()
|
|
103
|
+
|
|
104
|
+
manifest = self._manifests.get(project.lower())
|
|
105
|
+
if not manifest:
|
|
106
|
+
return {"status": "error", "detail": f"Unknown project: {project}"}
|
|
107
|
+
|
|
108
|
+
# Find matching capability and endpoint
|
|
109
|
+
for cap in manifest.capabilities:
|
|
110
|
+
if cap.name == capability:
|
|
111
|
+
for endpoint in cap.endpoints:
|
|
112
|
+
if endpoint.method.upper() == method.upper():
|
|
113
|
+
return await self._execute(
|
|
114
|
+
manifest.base_url, endpoint, path_params, body
|
|
115
|
+
)
|
|
116
|
+
|
|
117
|
+
return {"status": "error", "detail": f"No matching endpoint for {capability}/{method}"}
|
|
118
|
+
|
|
119
|
+
async def _execute(
|
|
120
|
+
self,
|
|
121
|
+
base_url: str,
|
|
122
|
+
endpoint,
|
|
123
|
+
path_params: Optional[dict],
|
|
124
|
+
body: Optional[dict],
|
|
125
|
+
) -> dict[str, Any]:
|
|
126
|
+
"""Execute an HTTP call to a project endpoint."""
|
|
127
|
+
url = f"{base_url}{endpoint.path}"
|
|
128
|
+
if path_params:
|
|
129
|
+
for key, value in path_params.items():
|
|
130
|
+
# URL-encode substituted values so untrusted (LLM-supplied)
|
|
131
|
+
# params cannot inject path traversal or extra path segments.
|
|
132
|
+
url = url.replace(f"{{{key}}}", quote(str(value), safe=""))
|
|
133
|
+
|
|
134
|
+
headers = {}
|
|
135
|
+
if endpoint.auth_required:
|
|
136
|
+
from ecosystem_auth.tokens import sign_request
|
|
137
|
+
|
|
138
|
+
sign_body = None if endpoint.method.upper() in ("GET", "DELETE") else body
|
|
139
|
+
headers.update(
|
|
140
|
+
sign_request(endpoint.method, url, self.hmac_secret, sign_body)
|
|
141
|
+
)
|
|
142
|
+
|
|
143
|
+
try:
|
|
144
|
+
if endpoint.method.upper() in ("POST", "PUT", "PATCH"):
|
|
145
|
+
resp = await self._client.request(
|
|
146
|
+
endpoint.method, url, json=body, headers=headers
|
|
147
|
+
)
|
|
148
|
+
else:
|
|
149
|
+
resp = await self._client.request(endpoint.method, url, headers=headers)
|
|
150
|
+
|
|
151
|
+
return {
|
|
152
|
+
"status": "ok" if resp.status_code < 400 else "error",
|
|
153
|
+
"http_status": resp.status_code,
|
|
154
|
+
"data": resp.json() if resp.headers.get("content-type", "").startswith("application/json") else resp.text,
|
|
155
|
+
}
|
|
156
|
+
except Exception as e:
|
|
157
|
+
return {"status": "error", "detail": str(e)}
|
|
158
|
+
|
|
159
|
+
async def _harness_available(self) -> bool:
|
|
160
|
+
"""Check if the cyber-claude-harness daemon is reachable (cached)."""
|
|
161
|
+
now = time.monotonic()
|
|
162
|
+
if self._harness_ok is not None and (now - self._harness_checked_at) < self._harness_cache_ttl:
|
|
163
|
+
return self._harness_ok
|
|
164
|
+
try:
|
|
165
|
+
resp = await self._client.get(
|
|
166
|
+
f"{self._harness_base_url}/api/status", timeout=2.0
|
|
167
|
+
)
|
|
168
|
+
self._harness_ok = resp.status_code == 200
|
|
169
|
+
except Exception:
|
|
170
|
+
self._harness_ok = False
|
|
171
|
+
self._harness_checked_at = now
|
|
172
|
+
return self._harness_ok
|
|
173
|
+
|
|
174
|
+
async def _route_via_harness(
|
|
175
|
+
self, project: str, capability: str, body: dict
|
|
176
|
+
) -> dict[str, Any]:
|
|
177
|
+
"""Route a security capability through the harness daemon."""
|
|
178
|
+
try:
|
|
179
|
+
resp = await self._client.post(
|
|
180
|
+
f"{self._harness_base_url}/api/analyze",
|
|
181
|
+
json={"project": project, "capability": capability, **body},
|
|
182
|
+
timeout=30.0,
|
|
183
|
+
)
|
|
184
|
+
if resp.status_code == 200:
|
|
185
|
+
return {"status": "ok", "data": resp.json(), "source": "harness"}
|
|
186
|
+
except Exception as e:
|
|
187
|
+
logger.debug("Harness request failed: %s", e)
|
|
188
|
+
return {"status": "error", "detail": "harness unavailable"}
|
llm/discovery_agent.py
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
"""Discovery agent that queries the registry and builds capability manifests."""
|
|
2
|
+
|
|
3
|
+
import logging
|
|
4
|
+
from typing import Optional
|
|
5
|
+
|
|
6
|
+
import httpx
|
|
7
|
+
|
|
8
|
+
from .project_manifest import KNOWN_MANIFESTS, ProjectManifest
|
|
9
|
+
|
|
10
|
+
logger = logging.getLogger(__name__)
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class DiscoveryAgent:
|
|
14
|
+
"""
|
|
15
|
+
Discovers ecosystem projects via the registry and builds
|
|
16
|
+
capability manifests suitable for LLM tool-calling.
|
|
17
|
+
|
|
18
|
+
Used by AI_For_Survival's RAG pipeline to understand what
|
|
19
|
+
other projects can do and how to interact with them.
|
|
20
|
+
"""
|
|
21
|
+
|
|
22
|
+
def __init__(self, registry_url: str = "http://localhost:8500",
|
|
23
|
+
hmac_secret: Optional[str] = None):
|
|
24
|
+
self.registry_url = registry_url
|
|
25
|
+
from ecosystem_auth.tokens import get_ecosystem_secret
|
|
26
|
+
self.hmac_secret = get_ecosystem_secret(hmac_secret)
|
|
27
|
+
|
|
28
|
+
def _signed_headers(self, url: str) -> dict:
|
|
29
|
+
"""Sign a GET so discovery works when read endpoints require auth."""
|
|
30
|
+
from ecosystem_auth.tokens import sign_request
|
|
31
|
+
return sign_request("GET", url, self.hmac_secret)
|
|
32
|
+
|
|
33
|
+
async def discover_all(self) -> list[ProjectManifest]:
|
|
34
|
+
"""
|
|
35
|
+
Query the registry for all services and return their capability manifests.
|
|
36
|
+
|
|
37
|
+
Merges registry data (live URLs, health) with known static manifests.
|
|
38
|
+
"""
|
|
39
|
+
manifests = []
|
|
40
|
+
try:
|
|
41
|
+
url = f"{self.registry_url}/services"
|
|
42
|
+
async with httpx.AsyncClient(timeout=5.0) as client:
|
|
43
|
+
resp = await client.get(url, headers=self._signed_headers(url))
|
|
44
|
+
resp.raise_for_status()
|
|
45
|
+
services = resp.json()
|
|
46
|
+
except Exception as e:
|
|
47
|
+
logger.warning(f"Registry unavailable, using static manifests: {e}")
|
|
48
|
+
return list(KNOWN_MANIFESTS.values())
|
|
49
|
+
|
|
50
|
+
for service in services:
|
|
51
|
+
name = service.get("name", "")
|
|
52
|
+
manifest = self._build_manifest(name, service)
|
|
53
|
+
if manifest:
|
|
54
|
+
manifests.append(manifest)
|
|
55
|
+
|
|
56
|
+
return manifests
|
|
57
|
+
|
|
58
|
+
async def discover_one(self, service_name: str) -> Optional[ProjectManifest]:
|
|
59
|
+
"""Get a single service's capability manifest."""
|
|
60
|
+
try:
|
|
61
|
+
url = f"{self.registry_url}/services/{service_name}"
|
|
62
|
+
async with httpx.AsyncClient(timeout=5.0) as client:
|
|
63
|
+
resp = await client.get(url, headers=self._signed_headers(url))
|
|
64
|
+
resp.raise_for_status()
|
|
65
|
+
service = resp.json()
|
|
66
|
+
except Exception as e:
|
|
67
|
+
logger.warning(f"Could not fetch {service_name} from registry: {e}")
|
|
68
|
+
return KNOWN_MANIFESTS.get(service_name)
|
|
69
|
+
|
|
70
|
+
return self._build_manifest(service_name, service)
|
|
71
|
+
|
|
72
|
+
def _build_manifest(self, name: str, service_data: dict) -> Optional[ProjectManifest]:
|
|
73
|
+
"""Build a manifest from registry data merged with known capabilities."""
|
|
74
|
+
known = KNOWN_MANIFESTS.get(name)
|
|
75
|
+
if known:
|
|
76
|
+
# Update base_url from live registry data
|
|
77
|
+
base_url = service_data.get("base_url", known.base_url)
|
|
78
|
+
return known.model_copy(update={"base_url": base_url})
|
|
79
|
+
|
|
80
|
+
# Unknown service — create a minimal manifest
|
|
81
|
+
return ProjectManifest(
|
|
82
|
+
name=name,
|
|
83
|
+
description=service_data.get("metadata", {}).get("description", ""),
|
|
84
|
+
base_url=service_data.get("base_url", ""),
|
|
85
|
+
tags=[],
|
|
86
|
+
)
|
|
87
|
+
|
|
88
|
+
def manifests_to_tool_descriptions(self, manifests: list[ProjectManifest]) -> list[dict]:
|
|
89
|
+
"""
|
|
90
|
+
Convert manifests into a format suitable for LLM tool-calling.
|
|
91
|
+
|
|
92
|
+
Returns a list of tool descriptions that can be fed to an LLM
|
|
93
|
+
as available tools/functions.
|
|
94
|
+
"""
|
|
95
|
+
tools = []
|
|
96
|
+
for manifest in manifests:
|
|
97
|
+
for cap in manifest.capabilities:
|
|
98
|
+
for endpoint in cap.endpoints:
|
|
99
|
+
tools.append({
|
|
100
|
+
"name": f"{manifest.name.lower()}_{cap.name}_{endpoint.method.lower()}",
|
|
101
|
+
"description": f"[{manifest.name}] {cap.description}: {endpoint.description}",
|
|
102
|
+
"method": endpoint.method,
|
|
103
|
+
"url": f"{manifest.base_url}{endpoint.path}",
|
|
104
|
+
"parameters": endpoint.parameters,
|
|
105
|
+
"auth_required": endpoint.auth_required,
|
|
106
|
+
})
|
|
107
|
+
return tools
|