mirrorneuron-api 1.0.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.
@@ -0,0 +1,65 @@
1
+ Metadata-Version: 2.4
2
+ Name: mirrorneuron-api
3
+ Version: 1.0.0
4
+ Summary: MirrorNeuron REST API
5
+ License-Expression: MIT
6
+ Classifier: Programming Language :: Python :: 3
7
+ Requires-Python: >=3.9
8
+ Description-Content-Type: text/markdown
9
+ License-File: LICENSE
10
+ Requires-Dist: mirrorneuron-python-sdk
11
+ Requires-Dist: fastapi>=0.100.0
12
+ Requires-Dist: python-multipart>=0.0.9
13
+ Requires-Dist: uvicorn>=0.23.0
14
+ Provides-Extra: test
15
+ Requires-Dist: httpx>=0.27.0; extra == "test"
16
+ Dynamic: license-file
17
+
18
+ # MirrorNeuron API
19
+
20
+ The RESTful HTTP Gateway for the MirrorNeuron distributed runtime system.
21
+
22
+ ## Architecture
23
+ Built with FastAPI and Uvicorn, this component acts as an HTTP abstraction over the core `mn-python-sdk` gRPC Client. It allows external microservices, web dashboards, or non-Python applications to easily interact with the MirrorNeuron cluster without speaking Protobuf.
24
+
25
+ ## Installation
26
+ *Note: This API is installed automatically and symlinked globally as `mn-api` by the MirrorNeuron `install.sh` script.*
27
+
28
+ ```bash
29
+ pip install mirrorneuron-api
30
+ ```
31
+
32
+ ## Running the Server
33
+
34
+ ```bash
35
+ mn-api
36
+ ```
37
+ This runs the Uvicorn server on port `4001` locally.
38
+
39
+ ## Configuration
40
+
41
+ All overrides use `MN_` env vars:
42
+
43
+ - `MN_ENV=prod` requires `MN_API_TOKEN`.
44
+ - `MN_API_HOST`, `MN_API_PORT` control binding; default `localhost:4001`.
45
+ - `MN_CORE_HOST` controls the default core gRPC host; default `localhost`.
46
+ - `MN_GRPC_TARGET` or `MN_CORE_GRPC_TARGET` can override the full core gRPC target.
47
+ - `MN_GRPC_TIMEOUT_SECONDS` controls SDK calls.
48
+ - `MN_API_REQUEST_SIZE_LIMIT_BYTES` bounds request bodies.
49
+ - `MN_API_CORS_ALLOW_ORIGINS` is a comma-separated allowlist.
50
+
51
+ Protected endpoints accept `Authorization: Bearer <MN_API_TOKEN>`.
52
+
53
+ ## Endpoints
54
+
55
+ | Method | Route | Description |
56
+ |---|---|---|
57
+ | `GET` | `/api/v1/health` | Service health check |
58
+ | `GET` | `/api/v1/system/summary` | Cluster hardware and pool state |
59
+ | `GET` | `/api/v1/metrics` | Runtime metrics summary |
60
+ | `POST`| `/api/v1/jobs` | Submit a new workflow via JSON |
61
+ | `GET` | `/api/v1/jobs` | List recent workflows |
62
+ | `GET` | `/api/v1/jobs/{job_id}` | Fetch workflow status |
63
+ | `GET` | `/api/v1/jobs/{job_id}/events` | Fetch job events |
64
+ | `GET` | `/api/v1/jobs/{job_id}/dead-letters` | Inspect dead-letter events |
65
+ | `POST`| `/api/v1/jobs/{job_id}/cancel`| Cancel a running job |
@@ -0,0 +1,10 @@
1
+ mirrorneuron_api-1.0.0.dist-info/licenses/LICENSE,sha256=wdEmA-XJ6EaXHsjFyuRKhT-owNT5-ucLf6jbtOz0yOM,1072
2
+ mn_api/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
3
+ mn_api/config.py,sha256=1YFLyA2eclRDtO7oBi46D7UOPq4lVzovu7qRiJZ-stg,2610
4
+ mn_api/logging_config.py,sha256=UtXRdQROfRNo7DQepHRQGbqWk1NErfTTSrql3ZYWj6Q,1111
5
+ mn_api/main.py,sha256=AD-wsf5wBRfFHDSsO1I8yUsKc1zRGF46k6RF-xDySOI,18498
6
+ mirrorneuron_api-1.0.0.dist-info/METADATA,sha256=oq1Bjfb0sWsiR2MyiXrd4pMKJj8DwY4UK_F0LN2qfxI,2318
7
+ mirrorneuron_api-1.0.0.dist-info/WHEEL,sha256=aeYiig01lYGDzBgS8HxWXOg3uV61G9ijOsup-k9o1sk,91
8
+ mirrorneuron_api-1.0.0.dist-info/entry_points.txt,sha256=GYLz4okC4sSmLef-uNnS16uOE60B4rGWHFR8aqEDC5g,45
9
+ mirrorneuron_api-1.0.0.dist-info/top_level.txt,sha256=XE0OQ8OQYqO-VUg5-8f-2qyKUSrmS-NNtvUhs547t74,7
10
+ mirrorneuron_api-1.0.0.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (82.0.1)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1,2 @@
1
+ [console_scripts]
2
+ mn-api = mn_api.main:start
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 MirrorNeuronLab
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1 @@
1
+ mn_api
mn_api/__init__.py ADDED
File without changes
mn_api/config.py ADDED
@@ -0,0 +1,86 @@
1
+ from __future__ import annotations
2
+
3
+ import os
4
+ from dataclasses import dataclass
5
+ from typing import Iterable
6
+
7
+
8
+ TRUE_VALUES = {"1", "true", "yes", "on"}
9
+
10
+
11
+ @dataclass(frozen=True)
12
+ class ApiConfig:
13
+ env: str
14
+ host: str
15
+ port: int
16
+ grpc_target: str
17
+ grpc_timeout_seconds: float | None
18
+ api_token: str
19
+ request_size_limit_bytes: int
20
+ cors_allow_origins: list[str]
21
+
22
+ @classmethod
23
+ def from_env(cls) -> "ApiConfig":
24
+ env = os.getenv("MN_ENV", "dev")
25
+ timeout = _optional_float("MN_GRPC_TIMEOUT_SECONDS", "10")
26
+ core_host = os.getenv("MN_CORE_HOST", "localhost")
27
+ config = cls(
28
+ env=env,
29
+ host=os.getenv("MN_API_HOST", "localhost"),
30
+ port=_int("MN_API_PORT", "4001"),
31
+ grpc_target=os.getenv(
32
+ "MN_GRPC_TARGET",
33
+ os.getenv("MN_CORE_GRPC_TARGET", f"{core_host}:50051"),
34
+ ),
35
+ grpc_timeout_seconds=timeout,
36
+ api_token=os.getenv("MN_API_TOKEN", ""),
37
+ request_size_limit_bytes=_int(
38
+ "MN_API_REQUEST_SIZE_LIMIT_BYTES",
39
+ str(5 * 1024 * 1024),
40
+ ),
41
+ cors_allow_origins=_csv(
42
+ os.getenv("MN_API_CORS_ALLOW_ORIGINS", "")
43
+ ),
44
+ )
45
+ config.validate()
46
+ return config
47
+
48
+ @property
49
+ def prod(self) -> bool:
50
+ return self.env == "prod"
51
+
52
+ def validate(self) -> None:
53
+ if self.env not in {"dev", "test", "prod"}:
54
+ raise ValueError("MN_ENV must be one of dev, test, or prod")
55
+ if not 1 <= self.port <= 65535:
56
+ raise ValueError("MN_API_PORT must be between 1 and 65535")
57
+ if self.request_size_limit_bytes <= 0:
58
+ raise ValueError("MN_API_REQUEST_SIZE_LIMIT_BYTES must be > 0")
59
+ if self.prod and not self.api_token:
60
+ raise ValueError("MN_API_TOKEN is required when MN_ENV=prod")
61
+
62
+
63
+ def _int(name: str, default: str) -> int:
64
+ value = os.getenv(name, default)
65
+ try:
66
+ return int(value)
67
+ except ValueError as exc:
68
+ raise ValueError(f"{name} must be an integer") from exc
69
+
70
+
71
+ def _optional_float(name: str, default: str) -> float | None:
72
+ value = os.getenv(name, default)
73
+ if value.lower() in {"", "0", "none"}:
74
+ return None
75
+ try:
76
+ return float(value)
77
+ except ValueError as exc:
78
+ raise ValueError(f"{name} must be a number, 0, or none") from exc
79
+
80
+
81
+ def _csv(value: str) -> list[str]:
82
+ return [item.strip() for item in value.split(",") if item.strip()]
83
+
84
+
85
+ def auth_enabled(config: ApiConfig) -> bool:
86
+ return bool(config.api_token)
@@ -0,0 +1,39 @@
1
+ from __future__ import annotations
2
+
3
+ import logging
4
+ import os
5
+ from logging.handlers import RotatingFileHandler
6
+ from pathlib import Path
7
+
8
+
9
+ def configure_logging(name: str = "mn-api", default_file: str = "api.log") -> logging.Logger:
10
+ logger = logging.getLogger(name)
11
+ logger.setLevel(os.getenv("MN_LOG_LEVEL", "INFO").upper())
12
+ logger.propagate = False
13
+
14
+ if logger.handlers:
15
+ return logger
16
+
17
+ formatter = logging.Formatter(
18
+ "%(asctime)s %(levelname)s [%(name)s] %(message)s"
19
+ )
20
+ log_path = Path(
21
+ os.getenv(
22
+ "MN_API_LOG_PATH",
23
+ str(Path.home() / ".mn" / "logs" / default_file),
24
+ )
25
+ ).expanduser()
26
+
27
+ try:
28
+ log_path.parent.mkdir(parents=True, exist_ok=True)
29
+ handler: logging.Handler = RotatingFileHandler(
30
+ log_path,
31
+ maxBytes=int(os.getenv("MN_LOG_MAX_BYTES", "1048576")),
32
+ backupCount=int(os.getenv("MN_LOG_BACKUP_COUNT", "5")),
33
+ )
34
+ except OSError:
35
+ handler = logging.StreamHandler()
36
+
37
+ handler.setFormatter(formatter)
38
+ logger.addHandler(handler)
39
+ return logger
mn_api/main.py ADDED
@@ -0,0 +1,532 @@
1
+ from fastapi import Depends, FastAPI, File, Header, HTTPException, Request, UploadFile
2
+ from fastapi.middleware.cors import CORSMiddleware
3
+ from fastapi.responses import JSONResponse
4
+ from pydantic import BaseModel, Field
5
+ from typing import Dict, Any, Optional
6
+ import json
7
+ import tempfile
8
+ import uvicorn
9
+ import zipfile
10
+ from pathlib import Path
11
+ from mn_sdk import Client
12
+ import grpc
13
+ from mn_api.config import ApiConfig, auth_enabled
14
+ from mn_api.logging_config import configure_logging
15
+
16
+ config = ApiConfig.from_env()
17
+ logger = configure_logging()
18
+ app = FastAPI(title="MirrorNeuron API", version="1.0")
19
+ client = Client(target=config.grpc_target, timeout=config.grpc_timeout_seconds)
20
+ BUNDLE_UPLOAD_ROOT = Path(tempfile.gettempdir()) / "mirror_neuron_api_bundles"
21
+
22
+ if config.cors_allow_origins:
23
+ app.add_middleware(
24
+ CORSMiddleware,
25
+ allow_origins=config.cors_allow_origins,
26
+ allow_credentials=True,
27
+ allow_methods=["*"],
28
+ allow_headers=["*"],
29
+ )
30
+
31
+
32
+ @app.middleware("http")
33
+ async def enforce_request_size(request: Request, call_next):
34
+ content_length = request.headers.get("content-length")
35
+ try:
36
+ request_size = int(content_length) if content_length else 0
37
+ except ValueError:
38
+ return JSONResponse(
39
+ status_code=400,
40
+ content={"error": "invalid_content_length"},
41
+ )
42
+
43
+ if request_size > config.request_size_limit_bytes:
44
+ return JSONResponse(
45
+ status_code=413,
46
+ content={
47
+ "error": "request_too_large",
48
+ "limit_bytes": config.request_size_limit_bytes,
49
+ },
50
+ )
51
+ return await call_next(request)
52
+
53
+
54
+ def require_auth(authorization: str = Header(default="")):
55
+ if not auth_enabled(config):
56
+ return None
57
+
58
+ expected = f"Bearer {config.api_token}"
59
+ if authorization != expected:
60
+ raise HTTPException(status_code=401, detail="missing or invalid bearer token")
61
+ return None
62
+
63
+ class SubmitJobRequest(BaseModel):
64
+ manifest_json: Optional[str] = None
65
+ payloads: Optional[Dict[str, str]] = {}
66
+ bundle_path: Optional[str] = Field(default=None, alias="_bundle_path")
67
+
68
+ def handle_grpc_error(e: Exception):
69
+ logger.exception("Request failed")
70
+ if isinstance(e, grpc.RpcError) and e.code() == grpc.StatusCode.RESOURCE_EXHAUSTED:
71
+ return JSONResponse(
72
+ status_code=503,
73
+ content={"error": "resource_overloaded", "detail": e.details()},
74
+ )
75
+
76
+ if hasattr(e, 'details'):
77
+ return JSONResponse(status_code=500, content={"error": e.details()})
78
+ return JSONResponse(status_code=500, content={"error": str(e)})
79
+
80
+ @app.get("/api/v1/health")
81
+ def health():
82
+ return {"status": "ok", "auth": "enabled" if auth_enabled(config) else "disabled"}
83
+
84
+ @app.get("/api/v1/system/summary")
85
+ def get_system_summary(_auth=Depends(require_auth)):
86
+ try:
87
+ summary_json = client.get_system_summary()
88
+ return json.loads(summary_json)
89
+ except Exception as e:
90
+ return handle_grpc_error(e)
91
+
92
+ @app.post("/api/v1/jobs")
93
+ def submit_job(req: SubmitJobRequest, _auth=Depends(require_auth)):
94
+ try:
95
+ if req.bundle_path:
96
+ manifest_json, payloads_bytes = _load_uploaded_bundle(req.bundle_path)
97
+ elif req.manifest_json is not None:
98
+ manifest_json = req.manifest_json
99
+ payloads_bytes = (
100
+ {k: v.encode("utf-8") for k, v in req.payloads.items()}
101
+ if req.payloads
102
+ else {}
103
+ )
104
+ else:
105
+ raise HTTPException(
106
+ status_code=422,
107
+ detail="manifest_json or _bundle_path is required",
108
+ )
109
+
110
+ job_id = client.submit_job(manifest_json, payloads_bytes)
111
+ return {"id": job_id, "status": "pending"}
112
+ except HTTPException:
113
+ raise
114
+ except Exception as e:
115
+ return handle_grpc_error(e)
116
+
117
+
118
+ @app.post("/api/v1/bundles/upload")
119
+ async def upload_bundle(bundle: UploadFile = File(...), _auth=Depends(require_auth)):
120
+ try:
121
+ if not bundle.filename or not bundle.filename.lower().endswith(".zip"):
122
+ raise HTTPException(status_code=400, detail="bundle must be a .zip file")
123
+
124
+ BUNDLE_UPLOAD_ROOT.mkdir(parents=True, exist_ok=True)
125
+ target_dir = Path(tempfile.mkdtemp(prefix="bundle_", dir=BUNDLE_UPLOAD_ROOT))
126
+ archive_path = target_dir / "bundle.zip"
127
+ archive_path.write_bytes(await bundle.read())
128
+
129
+ with zipfile.ZipFile(archive_path) as archive:
130
+ for member in archive.infolist():
131
+ if member.is_dir():
132
+ continue
133
+ destination = _safe_extract_path(target_dir, member.filename)
134
+ destination.parent.mkdir(parents=True, exist_ok=True)
135
+ with archive.open(member) as source:
136
+ destination.write_bytes(source.read())
137
+
138
+ archive_path.unlink(missing_ok=True)
139
+ bundle_root = _find_bundle_root(target_dir)
140
+ manifest_path = bundle_root / "manifest.json"
141
+ payloads_path = bundle_root / "payloads"
142
+
143
+ if not manifest_path.is_file() or not payloads_path.is_dir():
144
+ raise HTTPException(
145
+ status_code=400,
146
+ detail="bundle zip must contain manifest.json and payloads/",
147
+ )
148
+
149
+ return {
150
+ "bundle_path": str(bundle_root),
151
+ "manifest": json.loads(manifest_path.read_text()),
152
+ }
153
+ except HTTPException:
154
+ raise
155
+ except zipfile.BadZipFile as exc:
156
+ raise HTTPException(status_code=400, detail="invalid zip bundle") from exc
157
+
158
+
159
+ def _load_uploaded_bundle(bundle_path: str) -> tuple[str, Dict[str, bytes]]:
160
+ bundle_root = Path(bundle_path).resolve()
161
+ upload_root = BUNDLE_UPLOAD_ROOT.resolve()
162
+ if not _inside_path(bundle_root, upload_root) or not bundle_root.is_dir():
163
+ raise HTTPException(status_code=400, detail="unknown uploaded bundle")
164
+
165
+ manifest_path = bundle_root / "manifest.json"
166
+ payloads_path = bundle_root / "payloads"
167
+ if not manifest_path.is_file() or not payloads_path.is_dir():
168
+ raise HTTPException(status_code=400, detail="invalid uploaded bundle")
169
+
170
+ payloads = {}
171
+ for path in payloads_path.rglob("*"):
172
+ if path.is_file():
173
+ payloads[path.relative_to(payloads_path).as_posix()] = path.read_bytes()
174
+
175
+ return manifest_path.read_text(), payloads
176
+
177
+
178
+ def _safe_extract_path(root: Path, member_name: str) -> Path:
179
+ member_path = Path(member_name)
180
+ if member_path.is_absolute() or ".." in member_path.parts:
181
+ raise HTTPException(status_code=400, detail="bundle contains unsafe paths")
182
+
183
+ destination = (root / member_path).resolve()
184
+ if not _inside_path(destination, root.resolve()):
185
+ raise HTTPException(status_code=400, detail="bundle contains unsafe paths")
186
+ return destination
187
+
188
+
189
+ def _find_bundle_root(extracted_root: Path) -> Path:
190
+ if (extracted_root / "manifest.json").is_file():
191
+ return extracted_root
192
+
193
+ children = [path for path in extracted_root.iterdir() if path.is_dir()]
194
+ if len(children) == 1 and (children[0] / "manifest.json").is_file():
195
+ return children[0]
196
+
197
+ return extracted_root
198
+
199
+
200
+ def _inside_path(path: Path, root: Path) -> bool:
201
+ try:
202
+ path.relative_to(root)
203
+ return True
204
+ except ValueError:
205
+ return False
206
+
207
+ @app.get("/api/v1/jobs")
208
+ def list_jobs(limit: int = 20, include_terminal: bool = True, _auth=Depends(require_auth)):
209
+ try:
210
+ jobs_json = client.list_jobs(limit, include_terminal)
211
+ return json.loads(jobs_json)
212
+ except Exception as e:
213
+ return handle_grpc_error(e)
214
+
215
+ @app.post("/api/v1/jobs:cleanup")
216
+ @app.post("/api/v1/jobs/cleanup")
217
+ def cleanup_jobs(_auth=Depends(require_auth)):
218
+ try:
219
+ cleared_count = client.clear_jobs()
220
+ return {"cleared_count": cleared_count}
221
+ except Exception as e:
222
+ return handle_grpc_error(e)
223
+
224
+ @app.get("/api/v1/jobs/{job_id}")
225
+ def get_job(job_id: str, _auth=Depends(require_auth)):
226
+ try:
227
+ job_json = client.get_job(job_id)
228
+ return json.loads(job_json)
229
+ except Exception as e:
230
+ return handle_grpc_error(e)
231
+
232
+
233
+ @app.get("/api/v1/jobs/{job_id}/agent-graph")
234
+ def get_job_agent_graph(job_id: str, _auth=Depends(require_auth)):
235
+ try:
236
+ details = json.loads(client.get_job(job_id))
237
+ events = [json.loads(event_json) for event_json in client.stream_events(job_id)]
238
+ return _build_agent_graph(job_id, details, events)
239
+ except Exception as e:
240
+ return handle_grpc_error(e)
241
+
242
+ @app.get("/api/v1/jobs/{job_id}/events")
243
+ def get_job_events(job_id: str, _auth=Depends(require_auth)):
244
+ try:
245
+ events = []
246
+ for event_json in client.stream_events(job_id):
247
+ events.append(json.loads(event_json))
248
+ return {"data": events}
249
+ except Exception as e:
250
+ return handle_grpc_error(e)
251
+
252
+
253
+ @app.get("/api/v1/jobs/{job_id}/dead-letters")
254
+ def get_job_dead_letters(job_id: str, _auth=Depends(require_auth)):
255
+ try:
256
+ dead_letters = []
257
+ for event_index, event_json in enumerate(client.stream_events(job_id)):
258
+ event = json.loads(event_json)
259
+ if event.get("type") == "dead_letter":
260
+ dead_letters.append(
261
+ {
262
+ "index": len(dead_letters),
263
+ "event_index": event_index,
264
+ "agent_id": event.get("agent_id"),
265
+ "reason": event.get("reason") or event.get("error"),
266
+ "timestamp": event.get("timestamp"),
267
+ "message": event.get("message"),
268
+ }
269
+ )
270
+ return {"job_id": job_id, "data": dead_letters}
271
+ except Exception as e:
272
+ return handle_grpc_error(e)
273
+
274
+
275
+ @app.post("/api/v1/jobs/{job_id}/dead-letters/{index}/replay")
276
+ def replay_job_dead_letter(job_id: str, index: int, _auth=Depends(require_auth)):
277
+ raise HTTPException(
278
+ status_code=501,
279
+ detail={
280
+ "error": "dead_letter_replay_not_exposed",
281
+ "job_id": job_id,
282
+ "index": index,
283
+ "message": "core replay is available in-process; gRPC replay will be added to expose it over REST",
284
+ },
285
+ )
286
+
287
+ @app.post("/api/v1/jobs/{job_id}/cancel")
288
+ def cancel_job(job_id: str, _auth=Depends(require_auth)):
289
+ try:
290
+ status = client.cancel_job(job_id)
291
+ return {"status": status, "job_id": job_id}
292
+ except Exception as e:
293
+ return handle_grpc_error(e)
294
+
295
+ @app.post("/api/v1/jobs/{job_id}/pause")
296
+ def pause_job(job_id: str, _auth=Depends(require_auth)):
297
+ try:
298
+ status = client.pause_job(job_id)
299
+ return {"status": status, "job_id": job_id}
300
+ except Exception as e:
301
+ return handle_grpc_error(e)
302
+
303
+ @app.post("/api/v1/jobs/{job_id}/resume")
304
+ def resume_job(job_id: str, _auth=Depends(require_auth)):
305
+ try:
306
+ status = client.resume_job(job_id)
307
+ return {"status": status, "job_id": job_id}
308
+ except Exception as e:
309
+ return handle_grpc_error(e)
310
+
311
+ @app.get("/api/v1/metrics")
312
+ def get_metrics(_auth=Depends(require_auth)):
313
+ try:
314
+ summary = json.loads(client.get_system_summary())
315
+ if "metrics" in summary:
316
+ return summary["metrics"]
317
+
318
+ jobs = summary.get("jobs", [])
319
+ return {
320
+ "jobs": {
321
+ "total": len(jobs),
322
+ "by_status": _counts(job.get("status", "unknown") for job in jobs),
323
+ },
324
+ "nodes": {"total": len(summary.get("nodes", []))},
325
+ "source": "system_summary",
326
+ }
327
+ except Exception as e:
328
+ return handle_grpc_error(e)
329
+
330
+
331
+ def _counts(values):
332
+ counts = {}
333
+ for value in values:
334
+ counts[value] = counts.get(value, 0) + 1
335
+ return counts
336
+
337
+
338
+ def _build_agent_graph(job_id: str, details: Dict[str, Any], events: list[Dict[str, Any]]):
339
+ agents = details.get("agents", []) or []
340
+ job = details.get("job", {}) or {}
341
+ manifest = job.get("topology") or _load_manifest_for_job(job)
342
+ agent_by_id: Dict[str, Dict[str, Any]] = {}
343
+
344
+ for agent in agents:
345
+ agent_id = agent.get("agent_id") or agent.get("node_id")
346
+ if agent_id:
347
+ agent_by_id[agent_id] = agent
348
+
349
+ for node in manifest.get("nodes", []) if isinstance(manifest, dict) else []:
350
+ node_id = node.get("node_id") or node.get("agent_id")
351
+ if node_id:
352
+ agent_by_id.setdefault(
353
+ node_id,
354
+ {
355
+ "agent_id": node_id,
356
+ "agent_type": node.get("agent_type") or "unknown",
357
+ "type": node.get("type") or "unknown",
358
+ "status": "declared",
359
+ "assigned_node": "unassigned",
360
+ "processed_messages": 0,
361
+ "mailbox_depth": 0,
362
+ },
363
+ )
364
+
365
+ edge_counts: Dict[tuple[str, str, str], Dict[str, Any]] = {}
366
+
367
+ for edge in manifest.get("edges", []) if isinstance(manifest, dict) else []:
368
+ source = edge.get("from_node")
369
+ target = edge.get("to_node")
370
+ message_type = edge.get("message_type") or "*"
371
+ if not source or not target:
372
+ continue
373
+
374
+ _ensure_graph_agent(agent_by_id, source)
375
+ _ensure_graph_agent(agent_by_id, target)
376
+ key = (source, target, message_type)
377
+ edge_counts.setdefault(
378
+ key,
379
+ {
380
+ "id": edge.get("edge_id") or f"{source}->{target}:{message_type}",
381
+ "source": source,
382
+ "target": target,
383
+ "message_type": message_type,
384
+ "count": 0,
385
+ "last_seen_at": None,
386
+ "source_event": "manifest",
387
+ },
388
+ )
389
+
390
+ for event in events:
391
+ message = _event_message_summary(event)
392
+ if not message:
393
+ continue
394
+
395
+ source = message.get("from")
396
+ target = message.get("to") or event.get("agent_id")
397
+ message_type = message.get("type") or event.get("type") or "message"
398
+
399
+ if not source or not target:
400
+ continue
401
+
402
+ _ensure_graph_agent(agent_by_id, source)
403
+ _ensure_graph_agent(agent_by_id, target)
404
+ key = (source, target, message_type)
405
+ existing = edge_counts.setdefault(
406
+ key,
407
+ {
408
+ "id": f"{source}->{target}:{message_type}",
409
+ "source": source,
410
+ "target": target,
411
+ "message_type": message_type,
412
+ "count": 0,
413
+ "last_seen_at": None,
414
+ "source_event": "agent_message_received",
415
+ },
416
+ )
417
+ existing["count"] += 1
418
+ existing["last_seen_at"] = event.get("timestamp") or existing["last_seen_at"]
419
+ if existing.get("source_event") == "manifest":
420
+ existing["source_event"] = "manifest+events"
421
+
422
+ for agent in agents:
423
+ source = agent.get("agent_id") or agent.get("node_id")
424
+ outbound_edges = (agent.get("metadata") or {}).get("outbound_edges") or []
425
+ for target in outbound_edges:
426
+ if not source or not target:
427
+ continue
428
+ _ensure_graph_agent(agent_by_id, source)
429
+ _ensure_graph_agent(agent_by_id, target)
430
+ key = (source, target, "*")
431
+ edge_counts.setdefault(
432
+ key,
433
+ {
434
+ "id": f"{source}->{target}:*",
435
+ "source": source,
436
+ "target": target,
437
+ "message_type": "*",
438
+ "count": 0,
439
+ "last_seen_at": None,
440
+ "source_event": "outbound_edges",
441
+ },
442
+ )
443
+
444
+ nodes = [
445
+ {
446
+ "id": agent_id,
447
+ "label": agent_id,
448
+ "agent_type": agent.get("agent_type") or "unknown",
449
+ "type": agent.get("type") or "unknown",
450
+ "status": agent.get("status") or "unknown",
451
+ "assigned_node": agent.get("assigned_node") or "unassigned",
452
+ "processed_messages": agent.get("processed_messages", 0),
453
+ "mailbox_depth": agent.get("mailbox_depth", 0),
454
+ }
455
+ for agent_id, agent in sorted(agent_by_id.items())
456
+ ]
457
+
458
+ edges = sorted(edge_counts.values(), key=lambda edge: (edge["source"], edge["target"], edge["message_type"]))
459
+
460
+ return {
461
+ "job_id": job_id,
462
+ "graph_id": job.get("graph_id") or (details.get("summary") or {}).get("graph_id"),
463
+ "status": job.get("status") or "unknown",
464
+ "nodes": nodes,
465
+ "edges": edges,
466
+ "stats": {
467
+ "agent_count": len(nodes),
468
+ "edge_count": len(edges),
469
+ "message_count": sum(edge.get("count", 0) for edge in edges),
470
+ "event_count": len(events),
471
+ },
472
+ }
473
+
474
+
475
+ def _load_manifest_for_job(job: Dict[str, Any]) -> Dict[str, Any]:
476
+ manifest_ref = job.get("manifest_ref") or {}
477
+ manifest_path = manifest_ref.get("manifest_path")
478
+ if not manifest_path:
479
+ return {}
480
+
481
+ path = Path(manifest_path)
482
+ if not path.is_file():
483
+ return {}
484
+
485
+ try:
486
+ manifest = json.loads(path.read_text())
487
+ except Exception:
488
+ logger.exception("Failed to load manifest for graph from %s", manifest_path)
489
+ return {}
490
+
491
+ return manifest if isinstance(manifest, dict) else {}
492
+
493
+
494
+ def _event_message_summary(event: Dict[str, Any]) -> Optional[Dict[str, Any]]:
495
+ payload = event.get("payload")
496
+ if event.get("type") == "agent_message_received" and isinstance(payload, dict):
497
+ return payload
498
+
499
+ if event.get("type") in {"backpressure_signal", "delivery_failed", "backpressure_rejected"} and isinstance(payload, dict):
500
+ return payload
501
+
502
+ message = event.get("message")
503
+ if isinstance(message, dict):
504
+ envelope = message.get("envelope")
505
+ if isinstance(envelope, dict):
506
+ return envelope
507
+ return message
508
+
509
+ return None
510
+
511
+
512
+ def _ensure_graph_agent(agent_by_id: Dict[str, Dict[str, Any]], agent_id: str):
513
+ agent_by_id.setdefault(
514
+ agent_id,
515
+ {
516
+ "agent_id": agent_id,
517
+ "agent_type": "external",
518
+ "type": "message",
519
+ "status": "observed",
520
+ "assigned_node": "unknown",
521
+ "processed_messages": 0,
522
+ "mailbox_depth": 0,
523
+ },
524
+ )
525
+
526
+
527
+ def start():
528
+ logger.info("Starting API server on %s:%s", config.host, config.port)
529
+ uvicorn.run("mn_api.main:app", host=config.host, port=config.port, reload=False)
530
+
531
+ if __name__ == "__main__":
532
+ start()