NCS:
- _collect_worker_caps() fetches capability flags from node-worker /caps
- _derive_capabilities() merges served model types + worker provider flags
- installed_artifacts replaces inventory_only (disk scan with DISK_SCAN_PATHS env)
- New endpoints: /capabilities/caps, /capabilities/installed
Node Worker:
- STT_PROVIDER, TTS_PROVIDER, OCR_PROVIDER, IMAGE_PROVIDER env flags
- /caps endpoint returns capabilities + providers for NCS aggregation
- STT adapter (providers/stt_mlx_whisper.py) — remote + local mode
- TTS adapter (providers/tts_mlx_kokoro.py) — remote + local mode
- OCR handler via vision_prompted (ollama_vision with OCR prompt)
- NATS subjects: node.{id}.stt/tts/ocr/image.request
Router:
- POST /v1/capability/{stt,tts,ocr,image} — capability-based offload routing
- GET /v1/capabilities — global view with capabilities_by_node
- require_fresh_caps(ttl) preflight guard
- find_nodes_with_capability(cap) + load-based node selection
Ops:
- ops/fabric_snapshot.py — full runtime snapshot collector
- ops/fabric_preflight.sh — quick check + snapshot save + diff
- docs/fabric_contract.md — Dev Contract v0.1 (preflight-first)
- tests/test_fabric_contract.py — CI enforcement (6 tests)
Made-with: Cursor
96 lines
2.5 KiB
Python
96 lines
2.5 KiB
Python
"""Node Worker — NATS offload executor for cross-node inference."""
|
|
import logging
|
|
import os
|
|
|
|
from fastapi import FastAPI
|
|
|
|
import config
|
|
import worker
|
|
|
|
logging.basicConfig(level=logging.INFO)
|
|
logger = logging.getLogger("node-worker")
|
|
|
|
app = FastAPI(title="Node Worker", version="1.0.0")
|
|
|
|
_nats_client = None
|
|
|
|
|
|
@app.get("/healthz")
|
|
async def healthz():
|
|
connected = _nats_client is not None and _nats_client.is_connected if _nats_client else False
|
|
return {
|
|
"status": "ok" if connected else "degraded",
|
|
"node_id": config.NODE_ID,
|
|
"nats_connected": connected,
|
|
"max_concurrency": config.MAX_CONCURRENCY,
|
|
}
|
|
|
|
|
|
@app.get("/metrics")
|
|
async def metrics():
|
|
return worker.get_metrics()
|
|
|
|
|
|
@app.get("/prom_metrics")
|
|
async def prom_metrics():
|
|
from fastapi.responses import Response
|
|
import fabric_metrics as fm
|
|
data = fm.get_metrics_text()
|
|
if data:
|
|
return Response(content=data, media_type="text/plain; charset=utf-8")
|
|
return {"error": "prometheus_client not installed"}
|
|
|
|
|
|
@app.get("/caps")
|
|
async def caps():
|
|
"""Capability flags for NCS to aggregate."""
|
|
return {
|
|
"node_id": config.NODE_ID,
|
|
"capabilities": {
|
|
"llm": True,
|
|
"vision": True,
|
|
"stt": config.STT_PROVIDER != "none",
|
|
"tts": config.TTS_PROVIDER != "none",
|
|
"ocr": config.OCR_PROVIDER != "none",
|
|
"image": config.IMAGE_PROVIDER != "none",
|
|
},
|
|
"providers": {
|
|
"stt": config.STT_PROVIDER,
|
|
"tts": config.TTS_PROVIDER,
|
|
"ocr": config.OCR_PROVIDER,
|
|
"image": config.IMAGE_PROVIDER,
|
|
},
|
|
"defaults": {
|
|
"llm": config.DEFAULT_LLM,
|
|
"vision": config.DEFAULT_VISION,
|
|
},
|
|
"concurrency": config.MAX_CONCURRENCY,
|
|
}
|
|
|
|
|
|
@app.on_event("startup")
|
|
async def startup():
|
|
global _nats_client
|
|
try:
|
|
import nats as nats_lib
|
|
_nats_client = await nats_lib.connect(config.NATS_URL)
|
|
logger.info(f"✅ NATS connected: {config.NATS_URL}")
|
|
await worker.start(_nats_client)
|
|
logger.info(f"✅ Node Worker ready: node={config.NODE_ID} concurrency={config.MAX_CONCURRENCY}")
|
|
except Exception as e:
|
|
logger.error(f"❌ Startup failed: {e}")
|
|
|
|
|
|
@app.on_event("shutdown")
|
|
async def shutdown():
|
|
if _nats_client:
|
|
try:
|
|
await _nats_client.close()
|
|
except Exception:
|
|
pass
|
|
|
|
|
|
if __name__ == "__main__":
|
|
import uvicorn
|
|
uvicorn.run(app, host="0.0.0.0", port=config.PORT)
|