preset_resolver.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. """Resolve a `PresetRef` (source + id) to the JSON-string content the
  2. slicer-api sidecar's `/slice` endpoint expects.
  3. Three sources, three paths:
  4. - **local** — read ``LocalPreset.setting`` from the DB. Existing pre-PR
  5. behaviour for the slicer integration; preserved verbatim
  6. so clients still sending bare integer ids see no change.
  7. - **cloud** — fetch ``BambuCloudService.get_setting_detail(id)`` for the
  8. caller's stored cloud token. Result is the full slicer-shape
  9. preset JSON the sidecar can ingest directly.
  10. - **standard** — emit a stub ``{inherits: <name>, from: "system"}``. The
  11. sidecar's `bambuddy/profile-resolver` branch already walks
  12. ``inherits:`` against ``BUNDLED_PROFILES_PATH/<category>/<name>.json``
  13. during ``materializeProfile`` and merges parent-then-child,
  14. so the stub flattens out to the bundled content with no
  15. round-trip needed for the JSON itself.
  16. All three return the JSON as a *string* because that's what
  17. ``SlicerApiService.slice_with_profiles`` accepts as
  18. ``printer_profile_json`` etc. — the sidecar parses it once.
  19. """
  20. from __future__ import annotations
  21. import json
  22. import logging
  23. from fastapi import HTTPException
  24. from sqlalchemy.ext.asyncio import AsyncSession
  25. from backend.app.api.routes.cloud import get_stored_token
  26. from backend.app.core.permissions import Permission
  27. from backend.app.models.local_preset import LocalPreset
  28. from backend.app.models.user import User
  29. from backend.app.schemas.slicer import PresetRef
  30. from backend.app.services.bambu_cloud import (
  31. BambuCloudAuthError,
  32. BambuCloudError,
  33. BambuCloudService,
  34. )
  35. logger = logging.getLogger(__name__)
  36. _SLOT_TO_BUNDLED_CATEGORY = {
  37. "printer": "machine",
  38. "process": "process",
  39. "filament": "filament",
  40. }
  41. # The CLI's --load-settings parser uses the JSON's `type` field to decide
  42. # how to interpret each file (machine/process/filament). Without it the
  43. # CLI logs `operator(): unknown config type ... in load-settings`,
  44. # writes `error_string: "The input preset file is invalid and can not be
  45. # parsed.", return_code: -5` to result.json, and exits 0 — which the
  46. # Node sidecar's child_process treats as silent success producing no
  47. # output, then bubbles up as a generic "Failed to slice the model" 5xx.
  48. # Bambuddy then falls back to the embedded-settings path for every 3MF
  49. # slice, silently using whatever printer the source file was originally
  50. # bound to. Setting `type` correctly per slot fixes the silent fallback.
  51. _SLOT_TO_PROFILE_TYPE = {
  52. "printer": "machine",
  53. "process": "process",
  54. "filament": "filament",
  55. }
  56. async def resolve_preset_ref(
  57. db: AsyncSession,
  58. user: User | None,
  59. ref: PresetRef,
  60. slot: str,
  61. ) -> str:
  62. """Return the JSON-string content for `ref` so the sidecar can ingest it.
  63. `slot` is one of ``"printer"`` / ``"process"`` / ``"filament"``; it's
  64. only used to generate friendly error messages and to pick the bundled
  65. category for the standard tier.
  66. Raises ``HTTPException`` for any caller-facing error (invalid id, wrong
  67. preset type, cloud auth failure, network error fetching cloud detail).
  68. """
  69. if ref.source == "local":
  70. return await _resolve_local(db, ref, slot)
  71. if ref.source == "cloud":
  72. return await _resolve_cloud(db, user, ref, slot)
  73. if ref.source == "standard":
  74. return _resolve_standard(ref, slot)
  75. raise HTTPException(
  76. status_code=400,
  77. detail=f"Unknown preset source for {slot}: {ref.source!r}",
  78. )
  79. async def _resolve_local(db: AsyncSession, ref: PresetRef, slot: str) -> str:
  80. try:
  81. local_id = int(ref.id)
  82. except (ValueError, TypeError):
  83. raise HTTPException(status_code=400, detail=f"Invalid local preset id for {slot}: {ref.id!r}") from None
  84. preset = await db.get(LocalPreset, local_id)
  85. if preset is None or preset.preset_type != slot:
  86. raise HTTPException(
  87. status_code=400,
  88. detail=f"Invalid {slot} preset id (expected preset_type='{slot}')",
  89. )
  90. return preset.setting
  91. async def _resolve_cloud(db: AsyncSession, user: User | None, ref: PresetRef, slot: str) -> str:
  92. """Fetch a single cloud preset detail. Permission gate matches the
  93. rest of the cloud surface (`CLOUD_AUTH`) so a user with `LIBRARY_UPLOAD`
  94. but no `CLOUD_AUTH` can't slice using cloud presets even if their
  95. ``User.cloud_token`` survived a permission revocation."""
  96. if user is not None and not user.has_permission(Permission.CLOUD_AUTH.value):
  97. raise HTTPException(
  98. status_code=403,
  99. detail=f"Cloud presets require the cloud:auth permission ({slot})",
  100. )
  101. token, _email, region = await get_stored_token(db, user)
  102. if not token:
  103. raise HTTPException(
  104. status_code=400,
  105. detail=(
  106. f"Cloud preset selected for {slot}, but no Bambu Cloud session is "
  107. "stored. Sign in to Bambu Cloud and retry."
  108. ),
  109. )
  110. cloud = BambuCloudService(region=region)
  111. cloud.set_token(token)
  112. try:
  113. detail = await cloud.get_setting_detail(ref.id)
  114. except BambuCloudAuthError:
  115. raise HTTPException(
  116. status_code=401,
  117. detail=(f"Bambu Cloud session expired while fetching {slot} preset. Sign in again and retry."),
  118. ) from None
  119. except BambuCloudError as e:
  120. raise HTTPException(
  121. status_code=502,
  122. detail=f"Bambu Cloud unreachable while fetching {slot} preset: {e}",
  123. ) from e
  124. finally:
  125. await cloud.close()
  126. # `get_setting_detail` returns the wrapper envelope; the actual preset
  127. # JSON lives under `.setting`. The sidecar wants the preset content, not
  128. # the envelope.
  129. payload = detail.get("setting") if isinstance(detail, dict) else None
  130. if not isinstance(payload, dict):
  131. # Some endpoints return the preset at the top level instead of
  132. # nested under `setting`. Fall back to the whole response in that
  133. # case rather than failing — the sidecar will reject it cleanly if
  134. # the shape is genuinely wrong, and we log the unusual response.
  135. logger.info(
  136. "Cloud preset %r for %s returned unexpected shape, forwarding raw payload",
  137. ref.id,
  138. slot,
  139. )
  140. payload = detail
  141. return json.dumps(payload)
  142. def _resolve_standard(ref: PresetRef, slot: str) -> str:
  143. """Build a minimal `{name, inherits, from, type}` stub. The sidecar's
  144. resolver walks `BUNDLED_PROFILES_PATH/<category>/<name>.json` and merges,
  145. yielding the full bundled preset without us round-tripping the content
  146. through Bambuddy."""
  147. if slot not in _SLOT_TO_BUNDLED_CATEGORY:
  148. raise HTTPException(status_code=400, detail=f"Unknown slot for standard preset: {slot!r}")
  149. return json.dumps(
  150. {
  151. # `name` must be set so the sidecar's compatibility checks see a
  152. # populated value. Reusing the bundled name keeps the resolved
  153. # profile's identity consistent with what the user picked.
  154. "name": ref.id,
  155. "inherits": ref.id,
  156. # `from: "system"` skips the User/system compatibility rejection
  157. # the resolver was designed to fix for OrcaSlicer GUI exports —
  158. # we never want a bundled preset to be treated as User-authored.
  159. "from": "system",
  160. # `type` is required by the CLI's --load-settings parser — see
  161. # _SLOT_TO_PROFILE_TYPE above for the silent-failure mode.
  162. "type": _SLOT_TO_PROFILE_TYPE[slot],
  163. }
  164. )