aiDetection.ts 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. // Shared shape + class mapping for the Obico AI failure-detection surfaces
  2. // (the printer card badge and the detail modal), so the two cannot disagree
  3. // about what a given backend class means.
  4. export type AiDetectionClass = 'failure' | 'warning' | 'safe' | 'error' | 'unknown' | 'idle';
  5. export interface AiDetection {
  6. class: string;
  7. frame_count: number;
  8. score: number;
  9. // Why the most recent poll produced no verdict. null when the last poll
  10. // succeeded, and also when the viewer lacks settings:read — the backend
  11. // withholds the reason (it can name configured URLs) but still sends the
  12. // 'error' class, because "your print is not being watched" is not
  13. // configuration.
  14. error?: string | null;
  15. }
  16. /**
  17. * Canonical display class for a printer's detection state.
  18. *
  19. * `undefined` means the printer has no monitored print right now -> 'idle'.
  20. *
  21. * Anything unrecognised falls back to 'unknown', deliberately NOT to 'safe'.
  22. * Collapsing every non-failure/warning class into a green "Safe" badge is
  23. * exactly what made a printer whose detection had never once succeeded look
  24. * identical to a healthy one (#2952).
  25. */
  26. export function aiDetectionClass(detection?: AiDetection): AiDetectionClass {
  27. if (!detection) return 'idle';
  28. switch (detection.class) {
  29. case 'failure':
  30. case 'warning':
  31. case 'safe':
  32. case 'error':
  33. return detection.class;
  34. default:
  35. return 'unknown';
  36. }
  37. }
  38. /** True when the class represents an actual verdict, so score/frames mean something. */
  39. export function hasVerdict(cls: AiDetectionClass): boolean {
  40. return cls === 'failure' || cls === 'warning' || cls === 'safe';
  41. }