slicer_presets.py 27 KB

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