From 430e342c48cbda1383bceff6d1d8d9c9b39f72ca Mon Sep 17 00:00:00 2001 From: Adriano Dal Pastro Date: Wed, 13 May 2026 10:07:18 +0200 Subject: [PATCH] feat(api): _deps factories for Open3DModel client + recipe cache Co-Authored-By: Claude Opus 4.7 (1M context) --- src/presa_mano/api/_deps.py | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/src/presa_mano/api/_deps.py b/src/presa_mano/api/_deps.py index c538a16..a3f61d2 100644 --- a/src/presa_mano/api/_deps.py +++ b/src/presa_mano/api/_deps.py @@ -3,12 +3,19 @@ updates (PUT /api/config) take effect without restart.""" from __future__ import annotations +import os from pathlib import Path from typing import Any -from ..clients import G1Client, MarkerClient, NuwaClient, Sam21Client, YoloClient +from ..clients import ( + G1Client, MarkerClient, NuwaClient, + Open3DModelClient, Sam21Client, YoloClient, +) +from ..clients.open3dmodel_cache import RecipeMeshCache from ..config import data_dir, get_config +_recipe_cache_singleton: RecipeMeshCache | None = None + def _svc(cfg: dict[str, Any], key: str) -> dict[str, Any]: return cfg.get("services", {}).get(key, {}) @@ -37,6 +44,29 @@ def sam21() -> Sam21Client: ) +def open3dmodel() -> Open3DModelClient: + cfg = get_config() + c = _svc(cfg, "open3dmodel") + api_key_env = c.get("api_key_env") + api_key = os.environ.get(api_key_env) if api_key_env else None + return Open3DModelClient( + base_url=c.get("base_url", "http://localhost:8878"), + api_key=api_key, + timeout_s=float(c.get("timeout_s", 30.0)), + max_stl_bytes=int(c.get("max_stl_bytes", 20_000_000)), + ) + + +def recipe_cache() -> RecipeMeshCache: + """Singleton mesh cache. Sopravvive a PUT /api/config; etag stale forza + refetch al prossimo accesso. Inizializzata lazy con il client corrente + al momento della prima get.""" + global _recipe_cache_singleton + if _recipe_cache_singleton is None: + _recipe_cache_singleton = RecipeMeshCache(open3dmodel(), capacity=8) + return _recipe_cache_singleton + + def active_robot_key() -> str: """Which robot the orchestrator is currently bound to ('g1' or 'h1'). Read from `robot.active` in config; defaults to 'g1' for backward compat."""