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
llm/project_manifest.py
ADDED
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
"""Project capability manifests for LLM tool-calling integration."""
|
|
2
|
+
|
|
3
|
+
from typing import Any
|
|
4
|
+
|
|
5
|
+
from pydantic import BaseModel, Field
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class APIEndpoint(BaseModel):
|
|
9
|
+
"""An API endpoint exposed by a project."""
|
|
10
|
+
|
|
11
|
+
method: str = Field(..., description="HTTP method (GET, POST, etc.)")
|
|
12
|
+
path: str = Field(..., description="URL path (e.g., '/api/v1/cameras')")
|
|
13
|
+
description: str = Field(default="")
|
|
14
|
+
parameters: dict[str, Any] = Field(default_factory=dict)
|
|
15
|
+
auth_required: bool = Field(default=True)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class ProjectCapability(BaseModel):
|
|
19
|
+
"""A capability that a project provides to the ecosystem."""
|
|
20
|
+
|
|
21
|
+
name: str = Field(..., description="Capability name (e.g., 'motion_detection')")
|
|
22
|
+
description: str = Field(default="")
|
|
23
|
+
endpoints: list[APIEndpoint] = Field(default_factory=list)
|
|
24
|
+
event_types: list[str] = Field(
|
|
25
|
+
default_factory=list,
|
|
26
|
+
description="Event types this capability can publish",
|
|
27
|
+
)
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
class ProjectManifest(BaseModel):
|
|
31
|
+
"""
|
|
32
|
+
Full capability manifest for a project.
|
|
33
|
+
|
|
34
|
+
Used by the LLM discovery agent to understand what each project
|
|
35
|
+
can do and how to interact with it via API calls.
|
|
36
|
+
"""
|
|
37
|
+
|
|
38
|
+
name: str
|
|
39
|
+
description: str = ""
|
|
40
|
+
version: str = "0.1.0"
|
|
41
|
+
base_url: str = ""
|
|
42
|
+
capabilities: list[ProjectCapability] = Field(default_factory=list)
|
|
43
|
+
tags: list[str] = Field(default_factory=list)
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
# Static manifests for known projects
|
|
47
|
+
KNOWN_MANIFESTS: dict[str, ProjectManifest] = {
|
|
48
|
+
"openeye": ProjectManifest(
|
|
49
|
+
name="OpenEye",
|
|
50
|
+
description="OpenCV home security surveillance system with motion/person detection",
|
|
51
|
+
base_url="http://localhost:8200",
|
|
52
|
+
capabilities=[
|
|
53
|
+
ProjectCapability(
|
|
54
|
+
name="camera_monitoring",
|
|
55
|
+
description="Real-time camera feed monitoring with motion and person detection",
|
|
56
|
+
endpoints=[
|
|
57
|
+
APIEndpoint(method="GET", path="/api/cameras", description="List all cameras"),
|
|
58
|
+
APIEndpoint(method="GET", path="/api/cameras/{id}/status", description="Camera status"),
|
|
59
|
+
],
|
|
60
|
+
event_types=["security.motion_detected", "security.person_detected"],
|
|
61
|
+
),
|
|
62
|
+
ProjectCapability(
|
|
63
|
+
name="security_alerts",
|
|
64
|
+
description="Security alert management",
|
|
65
|
+
endpoints=[
|
|
66
|
+
APIEndpoint(method="GET", path="/api/alerts", description="List recent alerts"),
|
|
67
|
+
APIEndpoint(method="POST", path="/api/alerts/acknowledge", description="Acknowledge alert"),
|
|
68
|
+
],
|
|
69
|
+
event_types=["security.alert", "security.intrusion"],
|
|
70
|
+
),
|
|
71
|
+
],
|
|
72
|
+
tags=["security", "surveillance", "opencv", "cameras"],
|
|
73
|
+
),
|
|
74
|
+
"magicmirror": ProjectManifest(
|
|
75
|
+
name="MagicMirror",
|
|
76
|
+
description="Smart mirror with modular widget system and display management",
|
|
77
|
+
base_url="http://localhost:8080",
|
|
78
|
+
capabilities=[
|
|
79
|
+
ProjectCapability(
|
|
80
|
+
name="display_management",
|
|
81
|
+
description="Control what is displayed on the smart mirror",
|
|
82
|
+
endpoints=[
|
|
83
|
+
APIEndpoint(method="GET", path="/api/v1/modules", description="List active modules"),
|
|
84
|
+
APIEndpoint(method="POST", path="/api/v1/notification", description="Send notification to mirror"),
|
|
85
|
+
],
|
|
86
|
+
event_types=["display.update", "display.alert"],
|
|
87
|
+
),
|
|
88
|
+
],
|
|
89
|
+
tags=["display", "dashboard", "widgets", "electron"],
|
|
90
|
+
),
|
|
91
|
+
"ai_for_survival": ProjectManifest(
|
|
92
|
+
name="AI_For_Survival",
|
|
93
|
+
description="AI-powered survival and security assistant with RAG pipeline and playbooks",
|
|
94
|
+
base_url="http://localhost:8000",
|
|
95
|
+
capabilities=[
|
|
96
|
+
ProjectCapability(
|
|
97
|
+
name="ai_analysis",
|
|
98
|
+
description="Run AI analysis using RAG pipeline and tool-calling agents",
|
|
99
|
+
endpoints=[
|
|
100
|
+
APIEndpoint(method="POST", path="/api/chat", description="Send chat/analysis request"),
|
|
101
|
+
APIEndpoint(method="GET", path="/api/playbooks", description="List available playbooks"),
|
|
102
|
+
APIEndpoint(method="POST", path="/api/playbooks/run", description="Execute a playbook"),
|
|
103
|
+
],
|
|
104
|
+
event_types=["ai.analysis_complete", "ai.recommendation"],
|
|
105
|
+
),
|
|
106
|
+
],
|
|
107
|
+
tags=["ai", "llm", "security", "analysis", "playbooks"],
|
|
108
|
+
),
|
|
109
|
+
"aegissiem": ProjectManifest(
|
|
110
|
+
name="AegisSIEM",
|
|
111
|
+
description="Router log analysis and network security monitoring",
|
|
112
|
+
base_url="http://localhost:8088",
|
|
113
|
+
capabilities=[
|
|
114
|
+
ProjectCapability(
|
|
115
|
+
name="log_analysis",
|
|
116
|
+
description="Analyze router and network logs for security threats",
|
|
117
|
+
endpoints=[
|
|
118
|
+
APIEndpoint(method="GET", path="/api/status", description="System status"),
|
|
119
|
+
APIEndpoint(method="GET", path="/api/logs/recent", description="Recent log entries"),
|
|
120
|
+
APIEndpoint(method="POST", path="/api/analyze", description="Trigger log analysis"),
|
|
121
|
+
],
|
|
122
|
+
event_types=["network.anomaly", "security.threat_blocked"],
|
|
123
|
+
),
|
|
124
|
+
ProjectCapability(
|
|
125
|
+
name="network_monitoring",
|
|
126
|
+
description="Monitor connected devices and bandwidth",
|
|
127
|
+
endpoints=[
|
|
128
|
+
APIEndpoint(method="GET", path="/api/devices", description="List connected devices"),
|
|
129
|
+
],
|
|
130
|
+
event_types=["network.device_connected", "network.device_disconnected"],
|
|
131
|
+
),
|
|
132
|
+
],
|
|
133
|
+
tags=["network", "logs", "router", "security"],
|
|
134
|
+
),
|
|
135
|
+
}
|
registry/__init__.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"""Ecosystem service registry and health monitor."""
|
registry/app.py
ADDED
|
@@ -0,0 +1,556 @@
|
|
|
1
|
+
"""FastAPI application for the ecosystem service registry."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import asyncio
|
|
6
|
+
import logging
|
|
7
|
+
import os
|
|
8
|
+
from contextlib import asynccontextmanager
|
|
9
|
+
from typing import TYPE_CHECKING
|
|
10
|
+
|
|
11
|
+
from typing import Optional
|
|
12
|
+
|
|
13
|
+
from fastapi import Body, FastAPI, HTTPException, Request, status, Depends
|
|
14
|
+
from fastapi.middleware.cors import CORSMiddleware
|
|
15
|
+
from fastapi.security import HTTPAuthorizationCredentials
|
|
16
|
+
from pydantic import BaseModel
|
|
17
|
+
from ecosystem_auth.middleware import (
|
|
18
|
+
authenticate_request, require_ecosystem_auth, security_scheme,
|
|
19
|
+
)
|
|
20
|
+
|
|
21
|
+
from .app_store import AppStore, RESERVED_NAMES, default_apps_path
|
|
22
|
+
from .credential_store import SUPPORTED_PROVIDERS, ProviderCredentialStore
|
|
23
|
+
from .health_monitor import HealthMonitor
|
|
24
|
+
from .models import HealthCheckResult, ServiceRecord, ServiceRegistration
|
|
25
|
+
from .registry import ServiceRegistry
|
|
26
|
+
|
|
27
|
+
if TYPE_CHECKING:
|
|
28
|
+
from events.event_bus import EventBus
|
|
29
|
+
|
|
30
|
+
logger = logging.getLogger(__name__)
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
@asynccontextmanager
|
|
34
|
+
async def lifespan(app: FastAPI):
|
|
35
|
+
from .logging_config import configure_logging
|
|
36
|
+
configure_logging()
|
|
37
|
+
|
|
38
|
+
persistence_path = os.environ.get(
|
|
39
|
+
"ECOSYSTEM_REGISTRY_FILE", "data/registry.json"
|
|
40
|
+
)
|
|
41
|
+
registry = ServiceRegistry(persistence_path=persistence_path)
|
|
42
|
+
|
|
43
|
+
# Pre-register projects from ecosystem.yaml if available
|
|
44
|
+
_register_static_projects(registry)
|
|
45
|
+
|
|
46
|
+
try:
|
|
47
|
+
from events.event_bus import EventBus
|
|
48
|
+
event_bus = EventBus(registry=registry)
|
|
49
|
+
except ImportError:
|
|
50
|
+
event_bus = None
|
|
51
|
+
logger.debug("Event bus not available — unhealthy events won't be published")
|
|
52
|
+
|
|
53
|
+
interval = int(os.environ.get("ECOSYSTEM_HEALTH_INTERVAL", "30"))
|
|
54
|
+
max_failures = int(os.environ.get("ECOSYSTEM_MAX_FAILURES", "5"))
|
|
55
|
+
health_monitor = HealthMonitor(
|
|
56
|
+
registry=registry,
|
|
57
|
+
interval_seconds=interval,
|
|
58
|
+
max_failures=max_failures,
|
|
59
|
+
event_bus=event_bus,
|
|
60
|
+
)
|
|
61
|
+
await health_monitor.start()
|
|
62
|
+
|
|
63
|
+
# Shared AI profile (single source of truth for ecosystem LLM settings).
|
|
64
|
+
from .profile_store import AIProfileStore
|
|
65
|
+
ai_profile_path = os.environ.get("ECOSYSTEM_AI_PROFILE_FILE", "data/ai_profile.json")
|
|
66
|
+
ai_profile_store = AIProfileStore(persistence_path=ai_profile_path)
|
|
67
|
+
|
|
68
|
+
# Per-app credential store for third-party participants (owner path uses the
|
|
69
|
+
# shared secret; enrolled apps use their own key). Live-reloads from disk.
|
|
70
|
+
app_store = AppStore(persistence_path=default_apps_path())
|
|
71
|
+
|
|
72
|
+
# Cloud AI provider API keys (Anthropic/OpenAI/Gemini), file-backed at
|
|
73
|
+
# ~/.config/ecosystem/provider_keys.json. Local-only surface — see the
|
|
74
|
+
# /ai/providers routes below, which gate writes to loopback callers.
|
|
75
|
+
provider_credential_store = ProviderCredentialStore()
|
|
76
|
+
|
|
77
|
+
# Stash on app.state so request handlers resolve them via dependencies
|
|
78
|
+
# rather than module globals (which could be None before startup).
|
|
79
|
+
app.state.registry = registry
|
|
80
|
+
app.state.health_monitor = health_monitor
|
|
81
|
+
app.state.event_bus = event_bus
|
|
82
|
+
app.state.ai_profile_store = ai_profile_store
|
|
83
|
+
app.state.app_store = app_store
|
|
84
|
+
app.state.provider_credential_store = provider_credential_store
|
|
85
|
+
|
|
86
|
+
logger.info("Ecosystem registry started")
|
|
87
|
+
yield
|
|
88
|
+
|
|
89
|
+
await health_monitor.stop()
|
|
90
|
+
if event_bus:
|
|
91
|
+
await event_bus.close()
|
|
92
|
+
logger.info("Ecosystem registry stopped")
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
def get_registry(request: Request) -> ServiceRegistry:
|
|
96
|
+
"""Dependency: resolve the registry, 503 if startup has not completed."""
|
|
97
|
+
registry = getattr(request.app.state, "registry", None)
|
|
98
|
+
if registry is None:
|
|
99
|
+
raise HTTPException(
|
|
100
|
+
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
|
|
101
|
+
detail="Registry not initialized",
|
|
102
|
+
)
|
|
103
|
+
return registry
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
def get_health_monitor(request: Request) -> HealthMonitor:
|
|
107
|
+
"""Dependency: resolve the health monitor, 503 if startup has not completed."""
|
|
108
|
+
monitor = getattr(request.app.state, "health_monitor", None)
|
|
109
|
+
if monitor is None:
|
|
110
|
+
raise HTTPException(
|
|
111
|
+
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
|
|
112
|
+
detail="Health monitor not initialized",
|
|
113
|
+
)
|
|
114
|
+
return monitor
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
def get_profile_store(request: Request):
|
|
118
|
+
"""Dependency: resolve the AI profile store, 503 before startup completes."""
|
|
119
|
+
store = getattr(request.app.state, "ai_profile_store", None)
|
|
120
|
+
if store is None:
|
|
121
|
+
raise HTTPException(
|
|
122
|
+
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
|
|
123
|
+
detail="AI profile store not initialized",
|
|
124
|
+
)
|
|
125
|
+
return store
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
def _read_auth_required() -> bool:
|
|
129
|
+
"""Whether read endpoints (service listings) require authentication.
|
|
130
|
+
|
|
131
|
+
Off by default so discovery stays frictionless on a trusted/loopback
|
|
132
|
+
network; enable with ECOSYSTEM_REQUIRE_READ_AUTH to avoid leaking topology.
|
|
133
|
+
"""
|
|
134
|
+
return os.environ.get("ECOSYSTEM_REQUIRE_READ_AUTH", "false").lower() in (
|
|
135
|
+
"1", "true", "yes",
|
|
136
|
+
)
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
def _key_resolver(request: Request):
|
|
140
|
+
"""Build a per-app key resolver from the request's AppStore (or None)."""
|
|
141
|
+
store = getattr(request.app.state, "app_store", None)
|
|
142
|
+
if store is None:
|
|
143
|
+
return None
|
|
144
|
+
|
|
145
|
+
def resolve(key_id: str):
|
|
146
|
+
return store.get_by_key_id(key_id) # None if unknown/suspended
|
|
147
|
+
|
|
148
|
+
return resolve
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
async def require_registry_auth(
|
|
152
|
+
request: Request,
|
|
153
|
+
credentials: Optional[HTTPAuthorizationCredentials] = Depends(security_scheme),
|
|
154
|
+
) -> dict:
|
|
155
|
+
"""Registry auth: per-app key when present, else shared-secret owner."""
|
|
156
|
+
return await authenticate_request(request, credentials, key_resolver=_key_resolver(request))
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
def require_name_owner(name: str, auth: dict) -> None:
|
|
160
|
+
"""403 unless the caller owns `name` (owner may use any non-collision name)."""
|
|
161
|
+
owned = auth.get("owned_names", [])
|
|
162
|
+
if owned == ["*"]:
|
|
163
|
+
return
|
|
164
|
+
if name in RESERVED_NAMES:
|
|
165
|
+
raise HTTPException(
|
|
166
|
+
status_code=status.HTTP_403_FORBIDDEN,
|
|
167
|
+
detail=f"'{name}' is a reserved first-party service name",
|
|
168
|
+
)
|
|
169
|
+
if name not in owned:
|
|
170
|
+
raise HTTPException(
|
|
171
|
+
status_code=status.HTTP_403_FORBIDDEN,
|
|
172
|
+
detail=f"app '{auth.get('app_id')}' does not own service name '{name}'",
|
|
173
|
+
)
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
async def require_read_auth(
|
|
177
|
+
request: Request,
|
|
178
|
+
credentials: Optional[HTTPAuthorizationCredentials] = Depends(security_scheme),
|
|
179
|
+
):
|
|
180
|
+
"""Conditionally enforce ecosystem auth on read endpoints."""
|
|
181
|
+
if not _read_auth_required():
|
|
182
|
+
return None
|
|
183
|
+
return await require_registry_auth(request, credentials)
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
app = FastAPI(
|
|
187
|
+
title="Ecosystem Registry",
|
|
188
|
+
version="0.3.0",
|
|
189
|
+
description="Service discovery and health monitoring for the app ecosystem",
|
|
190
|
+
lifespan=lifespan,
|
|
191
|
+
)
|
|
192
|
+
|
|
193
|
+
def _cors_origins() -> list[str]:
|
|
194
|
+
"""Resolve allowed CORS origins from the environment.
|
|
195
|
+
|
|
196
|
+
Defaults to "*" for local development but should be set to an explicit,
|
|
197
|
+
comma-separated origin list in production.
|
|
198
|
+
"""
|
|
199
|
+
raw = os.environ.get("ECOSYSTEM_CORS_ORIGINS", "*").strip()
|
|
200
|
+
if raw == "*" or not raw:
|
|
201
|
+
return ["*"]
|
|
202
|
+
return [origin.strip() for origin in raw.split(",") if origin.strip()]
|
|
203
|
+
|
|
204
|
+
|
|
205
|
+
app.add_middleware(
|
|
206
|
+
CORSMiddleware,
|
|
207
|
+
allow_origins=_cors_origins(),
|
|
208
|
+
allow_methods=["GET", "POST", "PUT", "DELETE", "OPTIONS"],
|
|
209
|
+
allow_headers=["Authorization", "Content-Type", "X-Ecosystem-Signature"],
|
|
210
|
+
)
|
|
211
|
+
|
|
212
|
+
|
|
213
|
+
@app.get("/health")
|
|
214
|
+
async def health():
|
|
215
|
+
"""Registry's own health endpoint."""
|
|
216
|
+
return {"status": "healthy", "service": "ecosystem-registry"}
|
|
217
|
+
|
|
218
|
+
|
|
219
|
+
@app.get("/metrics")
|
|
220
|
+
async def metrics_endpoint(request: Request):
|
|
221
|
+
"""Prometheus-format metrics (counters + live service gauges)."""
|
|
222
|
+
from . import metrics
|
|
223
|
+
from fastapi.responses import PlainTextResponse
|
|
224
|
+
|
|
225
|
+
reg = getattr(request.app.state, "registry", None)
|
|
226
|
+
return PlainTextResponse(metrics.render_prometheus(reg))
|
|
227
|
+
|
|
228
|
+
|
|
229
|
+
@app.get("/ai-profile")
|
|
230
|
+
async def get_ai_profile(
|
|
231
|
+
store=Depends(get_profile_store),
|
|
232
|
+
_auth=Depends(require_read_auth),
|
|
233
|
+
):
|
|
234
|
+
"""Return the shared ecosystem AI/LLM profile (the synced source of truth)."""
|
|
235
|
+
return store.get()
|
|
236
|
+
|
|
237
|
+
|
|
238
|
+
@app.get("/ai-placement")
|
|
239
|
+
async def ai_placement(
|
|
240
|
+
registry: ServiceRegistry = Depends(get_registry),
|
|
241
|
+
_auth=Depends(require_read_auth),
|
|
242
|
+
):
|
|
243
|
+
"""Recommend the best host for LLM workloads from reported resources.
|
|
244
|
+
|
|
245
|
+
Returns null when no registered service has reported its hardware."""
|
|
246
|
+
from .placement import recommend_llm_host
|
|
247
|
+
return recommend_llm_host(registry.get_all())
|
|
248
|
+
|
|
249
|
+
|
|
250
|
+
@app.put("/ai-profile")
|
|
251
|
+
async def update_ai_profile(
|
|
252
|
+
changes: dict,
|
|
253
|
+
request: Request,
|
|
254
|
+
auth: dict = Depends(require_registry_auth),
|
|
255
|
+
store=Depends(get_profile_store),
|
|
256
|
+
):
|
|
257
|
+
"""Apply changes to the shared AI profile, bump its version, and broadcast an
|
|
258
|
+
`ecosystem.ai_profile_changed` event so other apps update live.
|
|
259
|
+
|
|
260
|
+
Owner-only in Phase 1: the shared AI profile is a trust-critical surface, so
|
|
261
|
+
third-party (enrolled) keys are rejected regardless of scope."""
|
|
262
|
+
if auth.get("app_id") != "__owner__":
|
|
263
|
+
raise HTTPException(
|
|
264
|
+
status_code=status.HTTP_403_FORBIDDEN,
|
|
265
|
+
detail="AI profile changes are owner-only",
|
|
266
|
+
)
|
|
267
|
+
updated_by = (auth or {}).get("app_id") or "unknown"
|
|
268
|
+
profile = store.update(changes, updated_by=updated_by)
|
|
269
|
+
|
|
270
|
+
event_bus = getattr(request.app.state, "event_bus", None)
|
|
271
|
+
if event_bus:
|
|
272
|
+
try:
|
|
273
|
+
from events.schemas import EventEnvelope
|
|
274
|
+
await event_bus.publish(EventEnvelope(
|
|
275
|
+
type="ecosystem.ai_profile_changed",
|
|
276
|
+
source="registry",
|
|
277
|
+
data={
|
|
278
|
+
"version": profile.get("version"),
|
|
279
|
+
"updated_by": updated_by,
|
|
280
|
+
"changed": [k for k in changes if k in profile],
|
|
281
|
+
"profile": profile,
|
|
282
|
+
},
|
|
283
|
+
))
|
|
284
|
+
except Exception as e:
|
|
285
|
+
logger.debug("Failed to publish ai_profile_changed event: %s", e)
|
|
286
|
+
|
|
287
|
+
return profile
|
|
288
|
+
|
|
289
|
+
|
|
290
|
+
class ProviderKeyBody(BaseModel):
|
|
291
|
+
api_key: str
|
|
292
|
+
|
|
293
|
+
|
|
294
|
+
def _provider_store(request: Request) -> ProviderCredentialStore:
|
|
295
|
+
return request.app.state.provider_credential_store
|
|
296
|
+
|
|
297
|
+
|
|
298
|
+
def _require_loopback(request: Request) -> None:
|
|
299
|
+
"""Key writes are loopback-only, 404-cloaked (mirrors the secret endpoints).
|
|
300
|
+
|
|
301
|
+
A forwarded header means the request crossed a proxy, so it is not a genuine
|
|
302
|
+
local caller even when request.client.host looks local. 404 (not 403) so the
|
|
303
|
+
existence of these write routes isn't disclosed to a remote caller.
|
|
304
|
+
"""
|
|
305
|
+
if request.headers.get("x-forwarded-for"):
|
|
306
|
+
raise HTTPException(status_code=404, detail="Not Found")
|
|
307
|
+
host = (request.client.host if request.client else "") or ""
|
|
308
|
+
if host not in ("127.0.0.1", "::1", "localhost"):
|
|
309
|
+
raise HTTPException(status_code=404, detail="Not Found")
|
|
310
|
+
|
|
311
|
+
|
|
312
|
+
@app.get("/ai/providers")
|
|
313
|
+
async def list_provider_keys(
|
|
314
|
+
request: Request,
|
|
315
|
+
_auth=Depends(require_read_auth),
|
|
316
|
+
):
|
|
317
|
+
"""Non-secret status for every supported provider. Never returns a key."""
|
|
318
|
+
return {"providers": _provider_store(request).status()}
|
|
319
|
+
|
|
320
|
+
|
|
321
|
+
@app.put("/ai/providers/{provider}/key")
|
|
322
|
+
async def set_provider_key(
|
|
323
|
+
provider: str,
|
|
324
|
+
request: Request,
|
|
325
|
+
body: ProviderKeyBody = Body(...),
|
|
326
|
+
auth: dict = Depends(require_registry_auth),
|
|
327
|
+
):
|
|
328
|
+
"""Store (or replace) a provider's API key.
|
|
329
|
+
|
|
330
|
+
Loopback-only AND registry-auth-gated: loopback stops remote callers,
|
|
331
|
+
auth stops unauthorized local ones (e.g. a different local user/process).
|
|
332
|
+
"""
|
|
333
|
+
_require_loopback(request)
|
|
334
|
+
store = _provider_store(request)
|
|
335
|
+
try:
|
|
336
|
+
store.set_key(provider, body.api_key)
|
|
337
|
+
except ValueError as e:
|
|
338
|
+
# ValueError text is about the provider name or emptiness - never the key.
|
|
339
|
+
raise HTTPException(status_code=400, detail=str(e))
|
|
340
|
+
return {"status": "stored", "provider": provider, "last4": store.status()[provider]["last4"]}
|
|
341
|
+
|
|
342
|
+
|
|
343
|
+
@app.delete("/ai/providers/{provider}/key")
|
|
344
|
+
async def delete_provider_key(
|
|
345
|
+
provider: str,
|
|
346
|
+
request: Request,
|
|
347
|
+
auth: dict = Depends(require_registry_auth),
|
|
348
|
+
):
|
|
349
|
+
"""Remove a provider's stored API key, if any.
|
|
350
|
+
|
|
351
|
+
Loopback-only AND registry-auth-gated (see set_provider_key)."""
|
|
352
|
+
_require_loopback(request)
|
|
353
|
+
if provider not in SUPPORTED_PROVIDERS:
|
|
354
|
+
raise HTTPException(status_code=400, detail=f"Unknown provider {provider!r}")
|
|
355
|
+
removed = _provider_store(request).delete_key(provider)
|
|
356
|
+
return {"status": "deleted" if removed else "not_found", "provider": provider}
|
|
357
|
+
|
|
358
|
+
|
|
359
|
+
@app.post("/register", response_model=ServiceRecord, status_code=status.HTTP_201_CREATED)
|
|
360
|
+
async def register_service(
|
|
361
|
+
registration: ServiceRegistration,
|
|
362
|
+
auth: dict = Depends(require_registry_auth),
|
|
363
|
+
registry: ServiceRegistry = Depends(get_registry),
|
|
364
|
+
):
|
|
365
|
+
"""Register a new service with the ecosystem."""
|
|
366
|
+
require_name_owner(registration.name, auth)
|
|
367
|
+
record = registry.register(registration)
|
|
368
|
+
return record
|
|
369
|
+
|
|
370
|
+
|
|
371
|
+
@app.delete("/deregister/{name}")
|
|
372
|
+
async def deregister_service(
|
|
373
|
+
name: str,
|
|
374
|
+
auth: dict = Depends(require_registry_auth),
|
|
375
|
+
registry: ServiceRegistry = Depends(get_registry),
|
|
376
|
+
):
|
|
377
|
+
"""Remove a service from the registry."""
|
|
378
|
+
require_name_owner(name, auth)
|
|
379
|
+
if not registry.deregister(name):
|
|
380
|
+
raise HTTPException(status_code=404, detail=f"Service '{name}' not found")
|
|
381
|
+
return {"status": "deregistered", "name": name}
|
|
382
|
+
|
|
383
|
+
|
|
384
|
+
@app.get("/services", response_model=list[ServiceRecord])
|
|
385
|
+
async def list_services(
|
|
386
|
+
registry: ServiceRegistry = Depends(get_registry),
|
|
387
|
+
_auth=Depends(require_read_auth),
|
|
388
|
+
):
|
|
389
|
+
"""List all registered services."""
|
|
390
|
+
return registry.get_all()
|
|
391
|
+
|
|
392
|
+
|
|
393
|
+
@app.get("/services/priority", response_model=list[ServiceRecord])
|
|
394
|
+
async def list_services_by_priority(
|
|
395
|
+
registry: ServiceRegistry = Depends(get_registry),
|
|
396
|
+
_auth=Depends(require_read_auth),
|
|
397
|
+
):
|
|
398
|
+
"""List all services sorted by priority (highest first, healthy first)."""
|
|
399
|
+
return registry.get_by_priority()
|
|
400
|
+
|
|
401
|
+
|
|
402
|
+
@app.get("/services/{name}", response_model=ServiceRecord)
|
|
403
|
+
async def get_service(
|
|
404
|
+
name: str,
|
|
405
|
+
registry: ServiceRegistry = Depends(get_registry),
|
|
406
|
+
_auth=Depends(require_read_auth),
|
|
407
|
+
):
|
|
408
|
+
"""Get a specific service by name."""
|
|
409
|
+
record = registry.get(name)
|
|
410
|
+
if not record:
|
|
411
|
+
raise HTTPException(status_code=404, detail=f"Service '{name}' not found")
|
|
412
|
+
return record
|
|
413
|
+
|
|
414
|
+
|
|
415
|
+
@app.get("/services/{name}/health", response_model=HealthCheckResult)
|
|
416
|
+
async def check_service_health(
|
|
417
|
+
name: str,
|
|
418
|
+
registry: ServiceRegistry = Depends(get_registry),
|
|
419
|
+
health_monitor: HealthMonitor = Depends(get_health_monitor),
|
|
420
|
+
_auth=Depends(require_read_auth),
|
|
421
|
+
):
|
|
422
|
+
"""Run an on-demand health check for a specific service."""
|
|
423
|
+
record = registry.get(name)
|
|
424
|
+
if not record:
|
|
425
|
+
raise HTTPException(status_code=404, detail=f"Service '{name}' not found")
|
|
426
|
+
result = await health_monitor.check_one(name)
|
|
427
|
+
return result
|
|
428
|
+
|
|
429
|
+
|
|
430
|
+
@app.post("/events/publish")
|
|
431
|
+
async def publish_event(request: Request, envelope: dict = Body(...)):
|
|
432
|
+
"""
|
|
433
|
+
Accept a signed event from an ecosystem service and fan it out to subscribers.
|
|
434
|
+
|
|
435
|
+
This is the registry-side counterpart of ecosystem-client's registry-mode
|
|
436
|
+
publisher (`event_publisher.py::_publish_via_registry`), which POSTs here. The
|
|
437
|
+
client had no fallback, so before this route existed every cross-app event 404'd
|
|
438
|
+
and was silently dropped (see todos_changelog.md, 2026-07-25).
|
|
439
|
+
|
|
440
|
+
Auth: the event is HMAC-signed by the publisher (the envelope's `signature`
|
|
441
|
+
field). We verify it against the shared ecosystem secret — no separate
|
|
442
|
+
Authorization credential — then delegate to the existing EventBus, which
|
|
443
|
+
re-signs and delivers to each subscriber's webhook by subscription.
|
|
444
|
+
"""
|
|
445
|
+
event_bus = getattr(request.app.state, "event_bus", None)
|
|
446
|
+
if event_bus is None:
|
|
447
|
+
raise HTTPException(
|
|
448
|
+
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
|
|
449
|
+
detail="event bus not configured",
|
|
450
|
+
)
|
|
451
|
+
|
|
452
|
+
from events.schemas import EventEnvelope
|
|
453
|
+
from ecosystem_auth.tokens import verify_signature
|
|
454
|
+
|
|
455
|
+
try:
|
|
456
|
+
event = EventEnvelope(**envelope)
|
|
457
|
+
except Exception:
|
|
458
|
+
raise HTTPException(
|
|
459
|
+
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
|
|
460
|
+
detail="invalid event envelope",
|
|
461
|
+
)
|
|
462
|
+
|
|
463
|
+
if not event.signature or not verify_signature(
|
|
464
|
+
event.signable_dict(), event.signature, event_bus.hmac_secret
|
|
465
|
+
):
|
|
466
|
+
raise HTTPException(
|
|
467
|
+
status_code=status.HTTP_401_UNAUTHORIZED,
|
|
468
|
+
detail="invalid or missing event signature",
|
|
469
|
+
)
|
|
470
|
+
|
|
471
|
+
# Accept-and-ack: fan out to subscribers in the BACKGROUND. A publish to the
|
|
472
|
+
# bus must never block on subscriber delivery — awaiting it here meant a slow
|
|
473
|
+
# or unreachable subscriber (its own retries: 3 x 5s) made every publish
|
|
474
|
+
# exceed the client's 5s request_timeout, so the publisher logged a
|
|
475
|
+
# ReadTimeout warning per event (log spam) even though the event was valid.
|
|
476
|
+
# The bus already has per-subscriber retries + logging; we only ack acceptance.
|
|
477
|
+
task = asyncio.create_task(event_bus.publish(event))
|
|
478
|
+
_pending = getattr(request.app.state, "_event_delivery_tasks", None)
|
|
479
|
+
if _pending is None:
|
|
480
|
+
_pending = set()
|
|
481
|
+
request.app.state._event_delivery_tasks = _pending
|
|
482
|
+
_pending.add(task)
|
|
483
|
+
task.add_done_callback(_pending.discard)
|
|
484
|
+
return {"queued": True}
|
|
485
|
+
|
|
486
|
+
|
|
487
|
+
def _register_static_projects(reg: ServiceRegistry) -> None:
|
|
488
|
+
"""Pre-register projects defined in ecosystem.yaml that are present locally.
|
|
489
|
+
|
|
490
|
+
Subset/multi-device tolerance: only projects whose repo actually exists on
|
|
491
|
+
this device are pre-registered, so a partial install does not show
|
|
492
|
+
not-installed apps as "unhealthy". Apps deployed on other devices self-
|
|
493
|
+
register over the network when they start. Set
|
|
494
|
+
``ECOSYSTEM_REGISTER_ALL_STATIC=1`` to force pre-registering every project
|
|
495
|
+
regardless of local presence (e.g. a single-host dev box with all repos)."""
|
|
496
|
+
try:
|
|
497
|
+
import yaml
|
|
498
|
+
from pathlib import Path
|
|
499
|
+
|
|
500
|
+
config_path = os.environ.get("ECOSYSTEM_CONFIG", "ecosystem.yaml")
|
|
501
|
+
if not os.path.exists(config_path):
|
|
502
|
+
return
|
|
503
|
+
with open(config_path) as f:
|
|
504
|
+
config = yaml.safe_load(f)
|
|
505
|
+
|
|
506
|
+
# In lan mode, loopback hosts in committed config are advertised on this
|
|
507
|
+
# host's LAN IP so other devices can health-check co-located services.
|
|
508
|
+
try:
|
|
509
|
+
from ecosystem_client.topology import resolve_static_host
|
|
510
|
+
except Exception:
|
|
511
|
+
def resolve_static_host(host: str) -> str: # type: ignore
|
|
512
|
+
return host or "localhost"
|
|
513
|
+
|
|
514
|
+
# Resolve the sibling-repo base path the same way the CLI does so the
|
|
515
|
+
# local-presence check matches where apps are actually installed.
|
|
516
|
+
register_all = os.environ.get(
|
|
517
|
+
"ECOSYSTEM_REGISTER_ALL_STATIC", ""
|
|
518
|
+
).lower() in ("1", "true", "yes")
|
|
519
|
+
base_str = (
|
|
520
|
+
os.environ.get("ECOSYSTEM_BASE_PATH")
|
|
521
|
+
or (config.get("ecosystem") or {}).get("base_path", "")
|
|
522
|
+
)
|
|
523
|
+
# ecosystem.yaml lives at <repo>/ecosystem.yaml; siblings live in <repo>/..
|
|
524
|
+
base = Path(base_str) if base_str else Path(config_path).resolve().parent.parent
|
|
525
|
+
|
|
526
|
+
for key, proj in (config.get("projects") or {}).items():
|
|
527
|
+
# Validate per-project so one malformed entry doesn't abort the
|
|
528
|
+
# entire static load.
|
|
529
|
+
if "port" not in proj:
|
|
530
|
+
logger.warning(f"Skipping static project '{key}': missing 'port'")
|
|
531
|
+
continue
|
|
532
|
+
rel_path = proj.get("path")
|
|
533
|
+
if not register_all and rel_path and not (base / rel_path).exists():
|
|
534
|
+
logger.info(
|
|
535
|
+
"Skipping static project '%s': not installed locally (%s). "
|
|
536
|
+
"It will appear if it self-registers from its own device.",
|
|
537
|
+
key, base / rel_path,
|
|
538
|
+
)
|
|
539
|
+
continue
|
|
540
|
+
try:
|
|
541
|
+
reg.register(
|
|
542
|
+
ServiceRegistration(
|
|
543
|
+
name=key,
|
|
544
|
+
host=resolve_static_host(proj.get("host", "localhost")),
|
|
545
|
+
port=proj["port"],
|
|
546
|
+
health_endpoint=proj.get("health_endpoint", "/health"),
|
|
547
|
+
priority=proj.get("priority", 0),
|
|
548
|
+
metadata={"stack": proj.get("stack", ""), "description": proj.get("description", "")},
|
|
549
|
+
static=True,
|
|
550
|
+
)
|
|
551
|
+
)
|
|
552
|
+
logger.info(f"Pre-registered static project: {key}")
|
|
553
|
+
except Exception as e:
|
|
554
|
+
logger.warning(f"Skipping invalid static project '{key}': {e}")
|
|
555
|
+
except Exception as e:
|
|
556
|
+
logger.warning(f"Could not load static projects: {e}")
|