MusePi

Natives media + system utilities

This document covers the media/system/conversion exports currently present in @musepi/pi-natives: terminal SIXEL image encoding, HTML conversion, clipboard access, token counting, macOS appearance/power helpers, and work profiling.

Implementation files

There is no native PhotonImage class, image.rs, or ProjFS overlay helper module in the current pi-natives addon. General-purpose image decode/resize/encode is expected to live outside this native surface; the native image export here is only terminal SIXEL encoding.

JS API ↔ Rust export/module mapping

JS export Rust N-API export Rust module
encodeSixel(bytes, width, height) encode_sixel sixel.rs
htmlToMarkdown(html, options?) html_to_markdown html.rs
copyToClipboard(text) copy_to_clipboard clipboard.rs
readImageFromClipboard() read_image_from_clipboard clipboard.rs
countTokens(input, encoding?) count_tokens tokens.rs
detectMacOSAppearance() detect_macos_appearance appearance.rs
MacAppearanceObserver.start(cb) MacAppearanceObserver::start appearance.rs
MacOSPowerAssertion.start(options?) MacOSPowerAssertion::start power.rs
getWorkProfile(lastSeconds) get_work_profile prof.rs

Data format boundaries and conversions

SIXEL image encoding (sixel)

Supported decode formats are whatever the compiled image crate supports for ImageReader in this build (commonly PNG/JPEG/WebP/GIF). Invalid target dimensions (0 width or height) fail with Target SIXEL dimensions must be greater than zero.

HTML conversion (html)

Conversion behavior:

Clipboard (clipboard)

There is no current packages/natives TS wrapper that emits OSC52, handles Termux, or suppresses native clipboard failures. Any best-effort clipboard policy must live in consumers.

Tokens (tokens)

macOS appearance and power helpers

Work profiling (prof)

Lifecycle and state transitions

SIXEL lifecycle

  1. encodeSixel(bytes, targetWidthPx, targetHeightPx) validates target dimensions.
  2. Rust guesses and decodes the encoded image.
  3. Image is resized exactly to the target dimensions when needed.
  4. Pixels are converted to RGBA8 and encoded with icy_sixel::sixel_encode.
  5. The SIXEL escape string is returned synchronously.

Failure transitions:

HTML lifecycle

  1. htmlToMarkdown(html, options) schedules a blocking conversion task.
  2. Conversion runs with defaulted options (cleanContent=false, skipImages=false) unless specified.
  3. The upstream converter owns normalization and preprocessing, makes affected auxiliary traversals iterative, and caps remaining recursive DOM traversal at 64; hitting that cap rejects the conversion instead of returning partial Markdown.
  4. Returns markdown string or rejects with Conversion error: ....

Clipboard lifecycle

Work profiling lifecycle

  1. No explicit start: profiling is active when task helpers execute.
  2. Every instrumented task scope records one sample on guard drop.
  3. Samples overwrite oldest entries after buffer capacity is reached.
  4. getWorkProfile(lastSeconds) reads a time window and derives folded/summary/svg artifacts.

Failure transitions:

Unsupported operations and error propagation

SIXEL

HTML

Clipboard

Work profiling

Platform caveats