slicer_presets.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. """Unified slicer-preset listing for the SliceModal (#wiki / Cloud-aware presets).
  2. Returns the printer/process/filament options grouped by source tier in
  3. priority order — cloud (per-user, live-fetched) > local (DB-backed
  4. imports) > standard (slicer-bundled stock fallback). Name-based dedup is
  5. applied so a preset that exists in multiple tiers only appears in the
  6. highest-priority one. Cloud failure modes (signed out / expired / network)
  7. are surfaced via a status field so the modal can render a precise banner
  8. without faking an "ok with empty list" response.
  9. """
  10. from __future__ import annotations
  11. import hashlib
  12. import json
  13. import logging
  14. import time
  15. from fastapi import APIRouter, Depends
  16. from sqlalchemy import select
  17. from sqlalchemy.ext.asyncio import AsyncSession
  18. from backend.app.api.routes.cloud import get_stored_token, resolve_api_key_cloud_owner
  19. from backend.app.core.auth import RequirePermissionIfAuthEnabled
  20. from backend.app.core.config import settings as app_settings
  21. from backend.app.core.database import get_db
  22. from backend.app.core.permissions import Permission
  23. from backend.app.models.local_preset import LocalPreset
  24. from backend.app.models.user import User
  25. from backend.app.schemas.slicer_presets import (
  26. UnifiedPreset,
  27. UnifiedPresetsBySlot,
  28. UnifiedPresetsResponse,
  29. )
  30. from backend.app.services.bambu_cloud import (
  31. BambuCloudAuthError,
  32. BambuCloudError,
  33. BambuCloudService,
  34. )
  35. from backend.app.services.slicer_api import SlicerApiError, SlicerApiService
  36. logger = logging.getLogger(__name__)
  37. router = APIRouter(prefix="/slicer", tags=["Slicer Presets"])
  38. # In-process cache for the bundled-profile list. The slicer sidecar walks a
  39. # read-only filesystem inside its own container, so the list only changes
  40. # across sidecar rebuilds — a long TTL is safe and avoids a sidecar round-trip
  41. # on every modal open. Per-user cache is unnecessary because bundled profiles
  42. # are global.
  43. _BUNDLED_TTL_S = 3600.0
  44. _bundled_cache: tuple[float, dict[str, list[UnifiedPreset]]] | None = None
  45. # Per-user cache for the cloud preset list. Cache key is (user_id, token_hash):
  46. # keying on the token hash means a logout/login or token-change automatically
  47. # invalidates the entry without needing the cloud-auth route handlers to call
  48. # back into this module. 5 minutes balances "users see their freshly-saved
  49. # presets quickly" against "a busy install doesn't hit the cloud once per
  50. # modal open per user".
  51. _CLOUD_TTL_S = 300.0
  52. _cloud_cache: dict[tuple[int, str], tuple[float, dict[str, list[UnifiedPreset]]]] = {}
  53. def _token_fingerprint(token: str) -> str:
  54. """Short stable hash of the cloud token for use as a cache-key component.
  55. Storing only the hash means we can safely keep multiple per-(user, token)
  56. entries without leaking the token via the in-process dict."""
  57. return hashlib.sha256(token.encode("utf-8")).hexdigest()[:16]
  58. _CLOUD_TYPE_TO_SLOT = {
  59. "filament": "filament",
  60. "printer": "printer",
  61. "print": "process", # Bambu Cloud calls process presets "print"
  62. }
  63. def _empty_slots() -> dict[str, list[UnifiedPreset]]:
  64. return {"printer": [], "process": [], "filament": []}
  65. async def _fetch_cloud_presets(db: AsyncSession, user: User | None) -> tuple[dict[str, list[UnifiedPreset]], str]:
  66. """Return (slots, cloud_status). Slots are empty when cloud_status != 'ok'.
  67. Defence-in-depth: even if a stored cloud_token survived a permission
  68. revocation (admin reset, legacy state), users without ``CLOUD_AUTH`` are
  69. treated as not-authenticated for this endpoint — the cloud tier never
  70. surfaces for them. This keeps the per-tier visibility consistent with the
  71. /cloud/* endpoint suite that already gates on CLOUD_AUTH.
  72. """
  73. if user is not None and not user.has_permission(Permission.CLOUD_AUTH.value):
  74. return _empty_slots(), "not_authenticated"
  75. token, _email, region = await get_stored_token(db, user)
  76. if not token:
  77. return _empty_slots(), "not_authenticated"
  78. user_key = user.id if user is not None else 0
  79. cache_key = (user_key, _token_fingerprint(token))
  80. now = time.monotonic()
  81. cached = _cloud_cache.get(cache_key)
  82. if cached and now - cached[0] < _CLOUD_TTL_S:
  83. return cached[1], "ok"
  84. cloud = BambuCloudService(region=region)
  85. cloud.set_token(token)
  86. try:
  87. try:
  88. raw = await cloud.get_slicer_settings()
  89. except BambuCloudAuthError:
  90. # Don't clear the token here — the cloud-status endpoint owns that
  91. # lifecycle. Just report expired so the UI can prompt re-auth.
  92. return _empty_slots(), "expired"
  93. except BambuCloudError as e:
  94. logger.warning("Cloud preset fetch failed for user %s: %s", user_key, e)
  95. return _empty_slots(), "unreachable"
  96. except Exception as e: # noqa: BLE001 — defensive: never crash the modal
  97. logger.warning("Cloud preset fetch unexpected error for user %s: %s", user_key, e)
  98. return _empty_slots(), "unreachable"
  99. slots = _empty_slots()
  100. for cloud_type, slot in _CLOUD_TYPE_TO_SLOT.items():
  101. type_data = raw.get(cloud_type, {})
  102. # The cloud splits presets into "private" (the user's own) and "public"
  103. # (Bambu's stock cloud presets). Both are valid choices — surface them
  104. # in the natural order private → public so a user's customisations
  105. # appear above the stock entries with the same names. Stock entries
  106. # that share names with private ones get deduped out within the cloud
  107. # tier itself.
  108. seen_names: set[str] = set()
  109. for entry in type_data.get("private", []) + type_data.get("public", []):
  110. name = entry.get("name")
  111. setting_id = entry.get("setting_id") or entry.get("id")
  112. if not name or not setting_id or name in seen_names:
  113. continue
  114. seen_names.add(name)
  115. slots[slot].append(UnifiedPreset(id=setting_id, name=name, source="cloud"))
  116. # Cloud filament presets carry no metadata in this response on
  117. # purpose: the per-preset detail endpoint
  118. # (/v1/iot-service/api/slicer/setting/{id}) is rate-limited at roughly
  119. # 10/sec per token, so fetching N filament presets to enrich them
  120. # one-by-one trips Bambu's limiter and returns 429 on every request
  121. # for users with large preset libraries (#1150 follow-up).
  122. #
  123. # The dedup pass (see _dedupe_by_name) compensates: when a cloud entry
  124. # wins over a same-named local entry, the cloud entry inherits the
  125. # local entry's filament_type / filament_colour. So cloud presets that
  126. # also exist locally still get metadata-aware pre-pick in the
  127. # SliceModal; cloud-only presets fall back to plain priority order.
  128. _cloud_cache[cache_key] = (now, slots)
  129. return slots, "ok"
  130. finally:
  131. await cloud.close()
  132. async def _fetch_local_presets(db: AsyncSession) -> dict[str, list[UnifiedPreset]]:
  133. """Local imports — no caching needed, single indexed DB read."""
  134. result = await db.execute(select(LocalPreset).order_by(LocalPreset.name))
  135. presets = result.scalars().all()
  136. slots = _empty_slots()
  137. type_to_slot = {"filament": "filament", "printer": "printer", "process": "process"}
  138. for p in presets:
  139. slot = type_to_slot.get(p.preset_type)
  140. if slot is None:
  141. continue
  142. extra: dict[str, str | None] = {}
  143. if slot == "filament":
  144. extra["filament_type"], extra["filament_colour"] = _parse_filament_metadata(p.setting)
  145. slots[slot].append(
  146. UnifiedPreset(id=str(p.id), name=p.name, source="local", **extra),
  147. )
  148. return slots
  149. def _parse_filament_metadata(setting_json: str | None) -> tuple[str | None, str | None]:
  150. """Extract first-slot ``filament_type`` and ``filament_colour`` from a
  151. stored preset JSON. OrcaSlicer stores both as arrays (per-extruder) — we
  152. take the first entry since pre-pick matching is one-slot-at-a-time.
  153. Defensive parse: any error returns (None, None) so a corrupt row never
  154. breaks the listing."""
  155. if not setting_json:
  156. return None, None
  157. try:
  158. data = json.loads(setting_json)
  159. except (ValueError, TypeError):
  160. return None, None
  161. if not isinstance(data, dict):
  162. return None, None
  163. return _first_scalar(data.get("filament_type")), _first_scalar(data.get("filament_colour"))
  164. def _first_scalar(value: object) -> str | None:
  165. if isinstance(value, list) and value:
  166. return value[0] if isinstance(value[0], str) else None
  167. if isinstance(value, str) and value:
  168. return value
  169. return None
  170. async def _fetch_bundled_presets(db: AsyncSession) -> dict[str, list[UnifiedPreset]]:
  171. """Standard slicer-bundled profiles via the sidecar's /profiles/bundled."""
  172. global _bundled_cache
  173. now = time.monotonic()
  174. if _bundled_cache and now - _bundled_cache[0] < _BUNDLED_TTL_S:
  175. return _bundled_cache[1]
  176. api_url = await _resolve_slicer_api_url(db)
  177. if not api_url:
  178. # No sidecar configured at all — return empty rather than caching, so
  179. # users who configure one mid-session see results on next open.
  180. return _empty_slots()
  181. try:
  182. async with SlicerApiService(base_url=api_url) as svc:
  183. raw = await svc.list_bundled_profiles()
  184. except SlicerApiError as e:
  185. logger.info("Bundled preset fetch from sidecar at %s failed: %s", api_url, e)
  186. return _empty_slots()
  187. except Exception as e: # noqa: BLE001 — never break the modal on sidecar issues
  188. logger.warning("Bundled preset fetch unexpected error: %s", e)
  189. return _empty_slots()
  190. slots = _empty_slots()
  191. for slot in ("printer", "process", "filament"):
  192. for entry in raw.get(slot, []) or []:
  193. name = entry.get("name")
  194. if not name:
  195. continue
  196. # Bundled presets are addressed by name (the slicer resolves them
  197. # by name during the `inherits:` walk), so name doubles as id.
  198. extra: dict[str, str | None] = {}
  199. if slot == "filament":
  200. extra["filament_type"] = entry.get("filament_type")
  201. extra["filament_colour"] = entry.get("filament_colour")
  202. slots[slot].append(
  203. UnifiedPreset(id=name, name=name, source="standard", **extra),
  204. )
  205. _bundled_cache = (now, slots)
  206. return slots
  207. async def _resolve_slicer_api_url(db: AsyncSession) -> str | None:
  208. """Pick the sidecar URL the bundled-listing fetch should hit.
  209. Mirrors the slice route's resolution at ``library.py:_run_slicer_with_fallback``:
  210. the user's ``preferred_slicer`` setting decides which sidecar Bambuddy
  211. talks to, and the per-install URL setting overrides the env default.
  212. A user who prefers Bambu Studio gets the *bambu-studio-api* sidecar's
  213. bundled list; a user who prefers OrcaSlicer gets the *orca-slicer-api*
  214. sidecar's bundled list. Without this branch the listing would always
  215. hit OrcaSlicer (port 3003) even for BambuStudio installs (port 3001),
  216. leaving the Standard tier permanently empty for them.
  217. """
  218. from backend.app.api.routes.settings import get_setting
  219. preferred = (await get_setting(db, "preferred_slicer")) or "bambu_studio"
  220. if preferred == "orcaslicer":
  221. configured = await get_setting(db, "orcaslicer_api_url")
  222. url = (configured or app_settings.slicer_api_url).strip()
  223. elif preferred == "bambu_studio":
  224. configured = await get_setting(db, "bambu_studio_api_url")
  225. url = (configured or app_settings.bambu_studio_api_url).strip()
  226. else:
  227. # Unknown preference — return None so the bundled tier is empty
  228. # rather than crashing the modal. The slice route raises 400 here;
  229. # we degrade silently because the modal's listing is informational.
  230. logger.warning("Unknown preferred_slicer setting: %r — bundled tier disabled", preferred)
  231. return None
  232. return url or None
  233. def _dedupe_by_name(
  234. cloud: dict[str, list[UnifiedPreset]],
  235. local: dict[str, list[UnifiedPreset]],
  236. standard: dict[str, list[UnifiedPreset]],
  237. ) -> tuple[
  238. dict[str, list[UnifiedPreset]],
  239. dict[str, list[UnifiedPreset]],
  240. dict[str, list[UnifiedPreset]],
  241. ]:
  242. """Filter so each preset name appears in exactly one tier (cloud > local > standard).
  243. Order within each tier is preserved as-is — only "lower-priority duplicates"
  244. are dropped. A preset shared across tiers (e.g. "Bambu PLA Basic" in cloud
  245. public AND standard bundled) only renders once, in the cloud tier.
  246. Filament metadata is **merged across tiers** during dedup: when a cloud
  247. entry wins over a same-named local entry, the cloud entry inherits the
  248. local entry's ``filament_type`` and ``filament_colour`` (cloud entries
  249. carry no metadata themselves because we deliberately don't fetch each
  250. setting's content — see _fetch_cloud_presets). Without this merge, the
  251. SliceModal's metadata-aware pre-pick would silently lose match data for
  252. every preset the user has both cloud-synced and locally imported, and
  253. fall back to plain priority selection.
  254. """
  255. # Build a lookup: filament name → metadata from the highest-quality tier
  256. # that has it. Local + standard both expose parsed metadata; cloud
  257. # doesn't. Take whichever non-empty entry shows up first.
  258. metadata_by_name: dict[str, tuple[str | None, str | None]] = {}
  259. for tier in (local, standard):
  260. for p in tier["filament"]:
  261. if p.name in metadata_by_name:
  262. continue
  263. if p.filament_type or p.filament_colour:
  264. metadata_by_name[p.name] = (p.filament_type, p.filament_colour)
  265. # Backfill cloud entries that don't have their own metadata.
  266. for p in cloud["filament"]:
  267. if (p.filament_type is None or p.filament_colour is None) and p.name in metadata_by_name:
  268. t, c = metadata_by_name[p.name]
  269. if p.filament_type is None and t is not None:
  270. p.filament_type = t
  271. if p.filament_colour is None and c is not None:
  272. p.filament_colour = c
  273. deduped_local = _empty_slots()
  274. deduped_standard = _empty_slots()
  275. for slot in ("printer", "process", "filament"):
  276. seen = {p.name for p in cloud[slot]}
  277. for p in local[slot]:
  278. if p.name in seen:
  279. continue
  280. deduped_local[slot].append(p)
  281. seen.add(p.name)
  282. for p in standard[slot]:
  283. if p.name in seen:
  284. continue
  285. deduped_standard[slot].append(p)
  286. seen.add(p.name)
  287. return cloud, deduped_local, deduped_standard
  288. @router.get("/presets", response_model=UnifiedPresetsResponse)
  289. async def list_unified_presets(
  290. db: AsyncSession = Depends(get_db),
  291. current_user: User | None = RequirePermissionIfAuthEnabled(Permission.LIBRARY_UPLOAD),
  292. api_key_cloud_owner: User | None = Depends(resolve_api_key_cloud_owner),
  293. ) -> UnifiedPresetsResponse:
  294. """List slicer presets across cloud / local / standard tiers, deduped by name.
  295. Drives the SliceModal preset dropdowns. Permission gate matches the
  296. slice action itself (``LIBRARY_UPLOAD``) so any user who can slice can
  297. see the preset options for the dialog. The cloud branch is independently
  298. gated on ``CLOUD_AUTH`` inside ``_fetch_cloud_presets`` so a user with
  299. only ``LIBRARY_UPLOAD`` doesn't see cloud presets they shouldn't have
  300. access to.
  301. API-keyed callers (which return None from ``current_user``) get the
  302. owner User via ``resolve_api_key_cloud_owner`` when the key has the
  303. cloud-access scope, so the cloud tier surfaces correctly for them
  304. too — matching the slice route (#1182 follow-up).
  305. """
  306. cloud_token_user = current_user or api_key_cloud_owner
  307. cloud, cloud_status = await _fetch_cloud_presets(db, cloud_token_user)
  308. local = await _fetch_local_presets(db)
  309. standard = await _fetch_bundled_presets(db)
  310. cloud, local, standard = _dedupe_by_name(cloud, local, standard)
  311. return UnifiedPresetsResponse(
  312. cloud=UnifiedPresetsBySlot(**cloud),
  313. local=UnifiedPresetsBySlot(**local),
  314. standard=UnifiedPresetsBySlot(**standard),
  315. cloud_status=cloud_status,
  316. )
  317. @router.get("/preview-progress/{request_id}")
  318. async def get_preview_slice_progress(
  319. request_id: str,
  320. db: AsyncSession = Depends(get_db),
  321. _: User | None = RequirePermissionIfAuthEnabled(Permission.LIBRARY_READ),
  322. ):
  323. """Proxy to the sidecar's ``GET /slice/progress/:requestId``.
  324. The SliceModal's filament-requirements call kicks off a real preview
  325. slice on the sidecar to discover which AMS slots the picked plate
  326. actually consumes. That HTTP call holds open for the full slice
  327. duration (multi-second to multi-minute on complex models), and the
  328. browser can't reach the sidecar directly thanks to the same-origin
  329. policy + the sidecar's CORS allowlist. This endpoint forwards the
  330. poll so the modal's inline spinner can show "Generating G-code (45%)"
  331. instead of an opaque elapsed-time counter while the preview runs.
  332. Returns the sidecar's snapshot verbatim, or 404 when the request_id
  333. is unknown / completed and grace-window-expired.
  334. """
  335. import httpx
  336. api_url = await _resolve_slicer_api_url(db)
  337. if not api_url:
  338. from fastapi import HTTPException
  339. raise HTTPException(status_code=503, detail="No slicer sidecar configured")
  340. url = f"{api_url}/slice/progress/{request_id}"
  341. try:
  342. async with httpx.AsyncClient(timeout=5.0) as client:
  343. response = await client.get(url)
  344. except httpx.RequestError:
  345. # Sidecar unreachable: surface as 503 instead of 500 so the
  346. # frontend's poller can keep trying without flagging a hard error.
  347. from fastapi import HTTPException
  348. raise HTTPException(status_code=503, detail="Slicer sidecar unreachable") from None
  349. if response.status_code == 404:
  350. from fastapi import HTTPException
  351. raise HTTPException(status_code=404, detail="Progress unavailable")
  352. return response.json()