slicer_presets.py 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  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, File, HTTPException, UploadFile
  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 (
  36. BundleNotFoundError,
  37. BundleSummary,
  38. SlicerApiError,
  39. SlicerApiService,
  40. SlicerApiUnavailableError,
  41. SlicerInputError,
  42. )
  43. from backend.app.utils.printer_models import PRINTER_MODEL_MAP
  44. logger = logging.getLogger(__name__)
  45. router = APIRouter(prefix="/slicer", tags=["Slicer Presets"])
  46. # In-process cache for the bundled-profile list. The slicer sidecar walks a
  47. # read-only filesystem inside its own container, so the list only changes
  48. # across sidecar rebuilds — a long TTL is safe and avoids a sidecar round-trip
  49. # on every modal open. Per-user cache is unnecessary because bundled profiles
  50. # are global.
  51. _BUNDLED_TTL_S = 3600.0
  52. _bundled_cache: tuple[float, dict[str, list[UnifiedPreset]]] | None = None
  53. # Per-user cache for the cloud preset list. Cache key is (user_id, token_hash):
  54. # keying on the token hash means a logout/login or token-change automatically
  55. # invalidates the entry without needing the cloud-auth route handlers to call
  56. # back into this module. 5 minutes balances "users see their freshly-saved
  57. # presets quickly" against "a busy install doesn't hit the cloud once per
  58. # modal open per user".
  59. _CLOUD_TTL_S = 300.0
  60. _cloud_cache: dict[tuple[int, str], tuple[float, dict[str, list[UnifiedPreset]]]] = {}
  61. def _token_fingerprint(token: str) -> str:
  62. """Short stable hash of the cloud token for use as a cache-key component.
  63. Storing only the hash means we can safely keep multiple per-(user, token)
  64. entries without leaking the token via the in-process dict."""
  65. return hashlib.sha256(token.encode("utf-8")).hexdigest()[:16]
  66. _CLOUD_TYPE_TO_SLOT = {
  67. "filament": "filament",
  68. "printer": "printer",
  69. "print": "process", # Bambu Cloud calls process presets "print"
  70. }
  71. def _empty_slots() -> dict[str, list[UnifiedPreset]]:
  72. return {"printer": [], "process": [], "filament": []}
  73. async def _fetch_cloud_presets(db: AsyncSession, user: User | None) -> tuple[dict[str, list[UnifiedPreset]], str]:
  74. """Return (slots, cloud_status). Slots are empty when cloud_status != 'ok'.
  75. Defence-in-depth: even if a stored cloud_token survived a permission
  76. revocation (admin reset, legacy state), users without ``CLOUD_AUTH`` are
  77. treated as not-authenticated for this endpoint — the cloud tier never
  78. surfaces for them. This keeps the per-tier visibility consistent with the
  79. /cloud/* endpoint suite that already gates on CLOUD_AUTH.
  80. """
  81. if user is not None and not user.has_permission(Permission.CLOUD_AUTH.value):
  82. return _empty_slots(), "not_authenticated"
  83. token, _email, region = await get_stored_token(db, user)
  84. if not token:
  85. return _empty_slots(), "not_authenticated"
  86. user_key = user.id if user is not None else 0
  87. cache_key = (user_key, _token_fingerprint(token))
  88. now = time.monotonic()
  89. cached = _cloud_cache.get(cache_key)
  90. if cached and now - cached[0] < _CLOUD_TTL_S:
  91. return cached[1], "ok"
  92. cloud = BambuCloudService(region=region)
  93. cloud.set_token(token)
  94. try:
  95. try:
  96. raw = await cloud.get_slicer_settings()
  97. except BambuCloudAuthError:
  98. # Don't clear the token here — the cloud-status endpoint owns that
  99. # lifecycle. Just report expired so the UI can prompt re-auth.
  100. return _empty_slots(), "expired"
  101. except BambuCloudError as e:
  102. logger.warning("Cloud preset fetch failed for user %s: %s", user_key, e)
  103. return _empty_slots(), "unreachable"
  104. except Exception as e: # noqa: BLE001 — defensive: never crash the modal
  105. logger.warning("Cloud preset fetch unexpected error for user %s: %s", user_key, e)
  106. return _empty_slots(), "unreachable"
  107. slots = _empty_slots()
  108. for cloud_type, slot in _CLOUD_TYPE_TO_SLOT.items():
  109. type_data = raw.get(cloud_type, {})
  110. # The cloud splits presets into "private" (the user's own) and "public"
  111. # (Bambu's stock cloud presets). Both are valid choices — surface them
  112. # in the natural order private → public so a user's customisations
  113. # appear above the stock entries with the same names. Stock entries
  114. # that share names with private ones get deduped out within the cloud
  115. # tier itself.
  116. seen_names: set[str] = set()
  117. for entry in type_data.get("private", []) + type_data.get("public", []):
  118. name = entry.get("name")
  119. setting_id = entry.get("setting_id") or entry.get("id")
  120. if not name or not setting_id or name in seen_names:
  121. continue
  122. seen_names.add(name)
  123. slots[slot].append(UnifiedPreset(id=setting_id, name=name, source="cloud"))
  124. # Cloud filament presets carry no metadata in this response on
  125. # purpose: the per-preset detail endpoint
  126. # (/v1/iot-service/api/slicer/setting/{id}) is rate-limited at roughly
  127. # 10/sec per token, so fetching N filament presets to enrich them
  128. # one-by-one trips Bambu's limiter and returns 429 on every request
  129. # for users with large preset libraries (#1150 follow-up).
  130. #
  131. # The dedup pass (see _dedupe_by_name) compensates: when a cloud entry
  132. # wins over a same-named local entry, the cloud entry inherits the
  133. # local entry's filament_type / filament_colour. So cloud presets that
  134. # also exist locally still get metadata-aware pre-pick in the
  135. # SliceModal; cloud-only presets fall back to plain priority order.
  136. _cloud_cache[cache_key] = (now, slots)
  137. return slots, "ok"
  138. finally:
  139. await cloud.close()
  140. async def _fetch_local_presets(db: AsyncSession) -> dict[str, list[UnifiedPreset]]:
  141. """Local imports — no caching needed, single indexed DB read."""
  142. result = await db.execute(select(LocalPreset).order_by(LocalPreset.name))
  143. presets = result.scalars().all()
  144. slots = _empty_slots()
  145. type_to_slot = {"filament": "filament", "printer": "printer", "process": "process"}
  146. for p in presets:
  147. slot = type_to_slot.get(p.preset_type)
  148. if slot is None:
  149. continue
  150. preset = UnifiedPreset(id=str(p.id), name=p.name, source="local")
  151. if slot == "filament":
  152. preset.filament_type, preset.filament_colour = _parse_filament_metadata(p.setting)
  153. if slot in ("process", "filament"):
  154. # Precise compatibility link — the slicer's own compatible_printers
  155. # list, captured at import time. Lets the SliceModal filter the
  156. # process / filament dropdowns by the selected printer without
  157. # falling back to the uploaded-bundle index.
  158. preset.compatible_printers = _parse_compatible_printers(p.compatible_printers)
  159. slots[slot].append(preset)
  160. return slots
  161. def _parse_compatible_printers(raw: str | None) -> list[str] | None:
  162. """``LocalPreset.compatible_printers`` stores a JSON array of printer-preset
  163. names. Return the parsed list, or ``None`` on missing / malformed data so
  164. the SliceModal falls back to the uploaded-bundle index for that preset."""
  165. if not raw:
  166. return None
  167. try:
  168. data = json.loads(raw)
  169. except (ValueError, TypeError):
  170. return None
  171. if not isinstance(data, list):
  172. return None
  173. names = [s for s in data if isinstance(s, str) and s.strip()]
  174. return names or None
  175. def _parse_filament_metadata(setting_json: str | None) -> tuple[str | None, str | None]:
  176. """Extract first-slot ``filament_type`` and ``filament_colour`` from a
  177. stored preset JSON. OrcaSlicer stores both as arrays (per-extruder) — we
  178. take the first entry since pre-pick matching is one-slot-at-a-time.
  179. Defensive parse: any error returns (None, None) so a corrupt row never
  180. breaks the listing."""
  181. if not setting_json:
  182. return None, None
  183. try:
  184. data = json.loads(setting_json)
  185. except (ValueError, TypeError):
  186. return None, None
  187. if not isinstance(data, dict):
  188. return None, None
  189. return _first_scalar(data.get("filament_type")), _first_scalar(data.get("filament_colour"))
  190. def _first_scalar(value: object) -> str | None:
  191. if isinstance(value, list) and value:
  192. return value[0] if isinstance(value[0], str) else None
  193. if isinstance(value, str) and value:
  194. return value
  195. return None
  196. async def _fetch_bundled_presets(db: AsyncSession) -> dict[str, list[UnifiedPreset]]:
  197. """Standard slicer-bundled profiles via the sidecar's /profiles/bundled."""
  198. global _bundled_cache
  199. now = time.monotonic()
  200. if _bundled_cache and now - _bundled_cache[0] < _BUNDLED_TTL_S:
  201. return _bundled_cache[1]
  202. api_url = await _resolve_slicer_api_url(db)
  203. if not api_url:
  204. # No sidecar configured at all — return empty rather than caching, so
  205. # users who configure one mid-session see results on next open.
  206. return _empty_slots()
  207. try:
  208. async with SlicerApiService(base_url=api_url) as svc:
  209. raw = await svc.list_bundled_profiles()
  210. except SlicerApiError as e:
  211. logger.info("Bundled preset fetch from sidecar at %s failed: %s", api_url, e)
  212. return _empty_slots()
  213. except Exception as e: # noqa: BLE001 — never break the modal on sidecar issues
  214. logger.warning("Bundled preset fetch unexpected error: %s", e)
  215. return _empty_slots()
  216. slots = _empty_slots()
  217. for slot in ("printer", "process", "filament"):
  218. for entry in raw.get(slot, []) or []:
  219. name = entry.get("name")
  220. if not name:
  221. continue
  222. # Bundled presets are addressed by name (the slicer resolves them
  223. # by name during the `inherits:` walk), so name doubles as id.
  224. extra: dict[str, str | None] = {}
  225. if slot == "filament":
  226. extra["filament_type"] = entry.get("filament_type")
  227. extra["filament_colour"] = entry.get("filament_colour")
  228. slots[slot].append(
  229. UnifiedPreset(id=name, name=name, source="standard", **extra),
  230. )
  231. _bundled_cache = (now, slots)
  232. return slots
  233. async def _resolve_slicer_api_url(db: AsyncSession) -> str | None:
  234. """Pick the sidecar URL the bundled-listing fetch should hit.
  235. Mirrors the slice route's resolution at ``library.py:_run_slicer_with_fallback``:
  236. the user's ``preferred_slicer`` setting decides which sidecar Bambuddy
  237. talks to, and the per-install URL setting overrides the env default.
  238. A user who prefers Bambu Studio gets the *bambu-studio-api* sidecar's
  239. bundled list; a user who prefers OrcaSlicer gets the *orca-slicer-api*
  240. sidecar's bundled list. Without this branch the listing would always
  241. hit OrcaSlicer (port 3003) even for BambuStudio installs (port 3001),
  242. leaving the Standard tier permanently empty for them.
  243. """
  244. from backend.app.api.routes.settings import get_setting
  245. preferred = (await get_setting(db, "preferred_slicer")) or "bambu_studio"
  246. if preferred == "orcaslicer":
  247. configured = await get_setting(db, "orcaslicer_api_url")
  248. url = (configured or app_settings.slicer_api_url).strip()
  249. elif preferred == "bambu_studio":
  250. configured = await get_setting(db, "bambu_studio_api_url")
  251. url = (configured or app_settings.bambu_studio_api_url).strip()
  252. else:
  253. # Unknown preference — return None so the bundled tier is empty
  254. # rather than crashing the modal. The slice route raises 400 here;
  255. # we degrade silently because the modal's listing is informational.
  256. logger.warning("Unknown preferred_slicer setting: %r — bundled tier disabled", preferred)
  257. return None
  258. return url or None
  259. def _dedupe_by_name(
  260. cloud: dict[str, list[UnifiedPreset]],
  261. local: dict[str, list[UnifiedPreset]],
  262. standard: dict[str, list[UnifiedPreset]],
  263. ) -> tuple[
  264. dict[str, list[UnifiedPreset]],
  265. dict[str, list[UnifiedPreset]],
  266. dict[str, list[UnifiedPreset]],
  267. ]:
  268. """Filter so each preset name appears in exactly one tier (cloud > local > standard).
  269. Order within each tier is preserved as-is — only "lower-priority duplicates"
  270. are dropped. A preset shared across tiers (e.g. "Bambu PLA Basic" in cloud
  271. public AND standard bundled) only renders once, in the cloud tier.
  272. Filament metadata is **merged across tiers** during dedup: when a cloud
  273. entry wins over a same-named local entry, the cloud entry inherits the
  274. local entry's ``filament_type`` and ``filament_colour`` (cloud entries
  275. carry no metadata themselves because we deliberately don't fetch each
  276. setting's content — see _fetch_cloud_presets). Without this merge, the
  277. SliceModal's metadata-aware pre-pick would silently lose match data for
  278. every preset the user has both cloud-synced and locally imported, and
  279. fall back to plain priority selection.
  280. """
  281. # Build a lookup: filament name → metadata from the highest-quality tier
  282. # that has it. Local + standard both expose parsed metadata; cloud
  283. # doesn't. Take whichever non-empty entry shows up first.
  284. metadata_by_name: dict[str, tuple[str | None, str | None]] = {}
  285. for tier in (local, standard):
  286. for p in tier["filament"]:
  287. if p.name in metadata_by_name:
  288. continue
  289. if p.filament_type or p.filament_colour:
  290. metadata_by_name[p.name] = (p.filament_type, p.filament_colour)
  291. # Backfill cloud entries that don't have their own metadata.
  292. for p in cloud["filament"]:
  293. if (p.filament_type is None or p.filament_colour is None) and p.name in metadata_by_name:
  294. t, c = metadata_by_name[p.name]
  295. if p.filament_type is None and t is not None:
  296. p.filament_type = t
  297. if p.filament_colour is None and c is not None:
  298. p.filament_colour = c
  299. deduped_local = _empty_slots()
  300. deduped_standard = _empty_slots()
  301. for slot in ("printer", "process", "filament"):
  302. seen = {p.name for p in cloud[slot]}
  303. for p in local[slot]:
  304. if p.name in seen:
  305. continue
  306. deduped_local[slot].append(p)
  307. seen.add(p.name)
  308. for p in standard[slot]:
  309. if p.name in seen:
  310. continue
  311. deduped_standard[slot].append(p)
  312. seen.add(p.name)
  313. return cloud, deduped_local, deduped_standard
  314. @router.get("/printer-models")
  315. def list_printer_models() -> dict[str, str]:
  316. """Canonical Bambu printer-model registry, surfaced for the SliceModal.
  317. Returns the backend's ``PRINTER_MODEL_MAP`` unmodified: keys are the long
  318. "Bambu Lab <model>" form that appears in 3MF metadata and in slicer
  319. printer-preset names, values are the normalized short codes used in
  320. BambuStudio's `@BBL <code>` cloud-preset filenames. The frontend uses this
  321. mapping to classify cloud / standard presets against the selected printer
  322. when no slicer bundle has been uploaded that covers the preset (#1325
  323. follow-up) - avoiding a second, manually-maintained model table on the
  324. frontend. No auth gate: this is a static reference dictionary, not
  325. user data.
  326. """
  327. return dict(PRINTER_MODEL_MAP)
  328. @router.get("/presets", response_model=UnifiedPresetsResponse)
  329. async def list_unified_presets(
  330. db: AsyncSession = Depends(get_db),
  331. current_user: User | None = RequirePermissionIfAuthEnabled(Permission.LIBRARY_UPLOAD),
  332. api_key_cloud_owner: User | None = Depends(resolve_api_key_cloud_owner),
  333. ) -> UnifiedPresetsResponse:
  334. """List slicer presets across cloud / local / standard tiers, deduped by name.
  335. Drives the SliceModal preset dropdowns. Permission gate matches the
  336. slice action itself (``LIBRARY_UPLOAD``) so any user who can slice can
  337. see the preset options for the dialog. The cloud branch is independently
  338. gated on ``CLOUD_AUTH`` inside ``_fetch_cloud_presets`` so a user with
  339. only ``LIBRARY_UPLOAD`` doesn't see cloud presets they shouldn't have
  340. access to.
  341. API-keyed callers (which return None from ``current_user``) get the
  342. owner User via ``resolve_api_key_cloud_owner`` when the key has the
  343. cloud-access scope, so the cloud tier surfaces correctly for them
  344. too — matching the slice route (#1182 follow-up).
  345. """
  346. cloud_token_user = current_user or api_key_cloud_owner
  347. cloud, cloud_status = await _fetch_cloud_presets(db, cloud_token_user)
  348. local = await _fetch_local_presets(db)
  349. standard = await _fetch_bundled_presets(db)
  350. cloud, local, standard = _dedupe_by_name(cloud, local, standard)
  351. return UnifiedPresetsResponse(
  352. cloud=UnifiedPresetsBySlot(**cloud),
  353. local=UnifiedPresetsBySlot(**local),
  354. standard=UnifiedPresetsBySlot(**standard),
  355. cloud_status=cloud_status,
  356. )
  357. def _bundle_summary_to_dict(b: BundleSummary) -> dict:
  358. """Serialize a BundleSummary for the JSON response. The frontend uses
  359. these arrays to populate the preset dropdowns when a user picks the
  360. bundle as the slice source.
  361. """
  362. return {
  363. "id": b.id,
  364. "printer_preset_name": b.printer_preset_name,
  365. "printer": b.printer,
  366. "process": b.process,
  367. "filament": b.filament,
  368. "version": b.version,
  369. }
  370. @router.post("/bundles", status_code=201)
  371. async def import_slicer_bundle(
  372. file: UploadFile = File(...),
  373. db: AsyncSession = Depends(get_db),
  374. _: User | None = RequirePermissionIfAuthEnabled(Permission.LIBRARY_UPLOAD),
  375. ):
  376. """Forward a BambuStudio Printer Preset Bundle (.bbscfg) to the sidecar.
  377. The user exports their printer's preset bundle from BambuStudio (File
  378. -> Export -> Export Preset Bundle, "Printer preset bundle" option).
  379. Uploading it here unpacks the bundle on the sidecar and exposes its
  380. inner printer / process / filament presets to subsequent slice
  381. requests via the bundle-id selector.
  382. Idempotent: re-uploading the same file yields the same id (sidecar
  383. hashes the zip content), so duplicate uploads collapse rather than
  384. accumulate.
  385. """
  386. api_url = await _resolve_slicer_api_url(db)
  387. if not api_url:
  388. raise HTTPException(status_code=503, detail="No slicer sidecar configured")
  389. # Multer on the sidecar caps bundle uploads at 50MB. We don't enforce
  390. # that here — let the sidecar's filter own the limit so it stays in
  391. # one place — but we do reject empty / huge files at the FastAPI
  392. # layer to avoid pointlessly streaming them to the sidecar first.
  393. contents = await file.read()
  394. if not contents:
  395. raise HTTPException(status_code=400, detail="Bundle file is empty")
  396. filename = file.filename or "bundle.bbscfg"
  397. try:
  398. async with SlicerApiService(base_url=api_url) as svc:
  399. summary = await svc.import_bundle(contents, filename=filename)
  400. except SlicerInputError as e:
  401. # Sidecar's 4xx — most likely a non-.bbscfg upload, a corrupt zip,
  402. # or a path-traversal entry that the manifest validator caught.
  403. # Log the detail so it lands in the support bundle: the FE-only
  404. # toast was leaving us blind during triage (#1312).
  405. logger.warning(
  406. "Bundle import rejected by sidecar (%s, %d bytes): %s",
  407. filename,
  408. len(contents),
  409. e,
  410. )
  411. raise HTTPException(status_code=400, detail=str(e)) from e
  412. except SlicerApiUnavailableError as e:
  413. logger.warning("Bundle import: sidecar unreachable (%s): %s", api_url, e)
  414. raise HTTPException(status_code=503, detail=str(e)) from e
  415. except SlicerApiError as e:
  416. logger.warning(
  417. "Bundle import: sidecar server error (%s, %d bytes): %s",
  418. filename,
  419. len(contents),
  420. e,
  421. )
  422. # 5xx from the sidecar's import path is rare — usually a disk
  423. # write failure inside DATA_PATH/bundles. 502 (bad gateway) is
  424. # closer to the truth than 500 here, since we're proxying.
  425. raise HTTPException(status_code=502, detail=str(e)) from e
  426. return _bundle_summary_to_dict(summary)
  427. @router.get("/bundles")
  428. async def list_slicer_bundles(
  429. db: AsyncSession = Depends(get_db),
  430. _: User | None = RequirePermissionIfAuthEnabled(Permission.LIBRARY_UPLOAD),
  431. ):
  432. """List every Printer Preset Bundle currently stored on the sidecar.
  433. Drives the SliceModal's "Bundle" tier and a Settings panel where
  434. users can review / delete imported bundles. Returns ``[]`` when the
  435. sidecar has no bundles imported yet.
  436. """
  437. api_url = await _resolve_slicer_api_url(db)
  438. if not api_url:
  439. # No sidecar configured: empty list rather than 503 so the modal
  440. # renders cleanly. Same shape as the bundled-presets fallback.
  441. return []
  442. try:
  443. async with SlicerApiService(base_url=api_url) as svc:
  444. bundles = await svc.list_bundles()
  445. except SlicerApiUnavailableError as e:
  446. # Sidecar offline: surface as 503 so the frontend can show a
  447. # banner. Differs from the bundled-tier behaviour because that
  448. # path also has cloud + local fallbacks; bundles is the only
  449. # source for its tier.
  450. raise HTTPException(status_code=503, detail=str(e)) from e
  451. except SlicerApiError as e:
  452. raise HTTPException(status_code=502, detail=str(e)) from e
  453. return [_bundle_summary_to_dict(b) for b in bundles]
  454. @router.get("/bundles/{bundle_id}")
  455. async def get_slicer_bundle(
  456. bundle_id: str,
  457. db: AsyncSession = Depends(get_db),
  458. _: User | None = RequirePermissionIfAuthEnabled(Permission.LIBRARY_UPLOAD),
  459. ):
  460. """Return one bundle by id. 404 if it doesn't exist on the sidecar."""
  461. api_url = await _resolve_slicer_api_url(db)
  462. if not api_url:
  463. raise HTTPException(status_code=503, detail="No slicer sidecar configured")
  464. try:
  465. async with SlicerApiService(base_url=api_url) as svc:
  466. summary = await svc.get_bundle(bundle_id)
  467. except BundleNotFoundError as e:
  468. raise HTTPException(status_code=404, detail=str(e)) from e
  469. except SlicerApiUnavailableError as e:
  470. raise HTTPException(status_code=503, detail=str(e)) from e
  471. except SlicerApiError as e:
  472. raise HTTPException(status_code=502, detail=str(e)) from e
  473. return _bundle_summary_to_dict(summary)
  474. @router.delete("/bundles/{bundle_id}", status_code=204)
  475. async def delete_slicer_bundle(
  476. bundle_id: str,
  477. db: AsyncSession = Depends(get_db),
  478. _: User | None = RequirePermissionIfAuthEnabled(Permission.LIBRARY_UPLOAD),
  479. ):
  480. """Remove a stored bundle from the sidecar. Future slice requests
  481. referencing this id will fail with 404 from the sidecar.
  482. """
  483. api_url = await _resolve_slicer_api_url(db)
  484. if not api_url:
  485. raise HTTPException(status_code=503, detail="No slicer sidecar configured")
  486. try:
  487. async with SlicerApiService(base_url=api_url) as svc:
  488. await svc.delete_bundle(bundle_id)
  489. except BundleNotFoundError as e:
  490. raise HTTPException(status_code=404, detail=str(e)) from e
  491. except SlicerApiUnavailableError as e:
  492. raise HTTPException(status_code=503, detail=str(e)) from e
  493. except SlicerApiError as e:
  494. raise HTTPException(status_code=502, detail=str(e)) from e
  495. @router.get("/preview-progress/{request_id}")
  496. async def get_preview_slice_progress(
  497. request_id: str,
  498. db: AsyncSession = Depends(get_db),
  499. _: User | None = RequirePermissionIfAuthEnabled(Permission.LIBRARY_READ),
  500. ):
  501. """Proxy to the sidecar's ``GET /slice/progress/:requestId``.
  502. The SliceModal's filament-requirements call kicks off a real preview
  503. slice on the sidecar to discover which AMS slots the picked plate
  504. actually consumes. That HTTP call holds open for the full slice
  505. duration (multi-second to multi-minute on complex models), and the
  506. browser can't reach the sidecar directly thanks to the same-origin
  507. policy + the sidecar's CORS allowlist. This endpoint forwards the
  508. poll so the modal's inline spinner can show "Generating G-code (45%)"
  509. instead of an opaque elapsed-time counter while the preview runs.
  510. Returns the sidecar's snapshot verbatim, or 404 when the request_id
  511. is unknown / completed and grace-window-expired.
  512. """
  513. import httpx
  514. api_url = await _resolve_slicer_api_url(db)
  515. if not api_url:
  516. raise HTTPException(status_code=503, detail="No slicer sidecar configured")
  517. url = f"{api_url}/slice/progress/{request_id}"
  518. try:
  519. async with httpx.AsyncClient(timeout=5.0) as client:
  520. response = await client.get(url)
  521. except httpx.RequestError:
  522. # Sidecar unreachable: surface as 503 instead of 500 so the
  523. # frontend's poller can keep trying without flagging a hard error.
  524. raise HTTPException(status_code=503, detail="Slicer sidecar unreachable") from None
  525. if response.status_code == 404:
  526. raise HTTPException(status_code=404, detail="Progress unavailable")
  527. return response.json()