Files
voice-to-notes/python/voice_to_notes/main.py
Claude db770c341d
All checks were successful
Build Sidecars / Bump sidecar version and tag (push) Successful in 9s
Release / Bump version and tag (push) Successful in 5s
Build Sidecars / Build Sidecar (macOS) (push) Successful in 3m37s
Release / Build App (macOS) (push) Successful in 1m16s
Build Sidecars / Build Sidecar (Linux) (push) Successful in 14m3s
Release / Build App (Linux) (push) Successful in 4m45s
Build Sidecars / Build Sidecar (Windows) (push) Successful in 24m32s
Release / Build App (Windows) (push) Successful in 3m12s
Fix CSP blocking IPC/assets + fix pyannote AudioDecoder crash
CSP: Add connect-src for ipc.localhost and asset.localhost so Tauri IPC
commands and local file loading (waveform, audio playback) work.

pyannote: Block torchcodec in sys.modules at startup so pyannote.audio
falls back to torchaudio for audio decoding. pyannote has a bug where
it uses AudioDecoder unconditionally even when torchcodec import fails.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-22 09:54:21 -07:00

82 lines
2.7 KiB
Python

"""Main entry point for the Voice to Notes Python sidecar."""
from __future__ import annotations
import signal
import sys
# Block torchcodec before anything imports it. pyannote.audio has a bug where
# it conditionally imports AudioDecoder from torchcodec but uses it
# unconditionally. Making torchcodec fully unimportable forces the torchaudio
# fallback path.
sys.modules["torchcodec"] = None # type: ignore[assignment]
sys.modules["torchcodec.decoders"] = None # type: ignore[assignment]
# CRITICAL: Capture real stdout for IPC *before* importing any ML libraries
# that might print to stdout and corrupt the JSON-line protocol.
from voice_to_notes.ipc.protocol import init_ipc
init_ipc()
from voice_to_notes.ipc.handlers import ( # noqa: E402
HandlerRegistry,
hardware_detect_handler,
make_ai_chat_handler,
make_diarize_download_handler,
make_diarize_handler,
make_export_handler,
make_pipeline_handler,
make_transcribe_handler,
ping_handler,
)
from voice_to_notes.ipc.messages import ready_message # noqa: E402
from voice_to_notes.ipc.protocol import read_message, write_message # noqa: E402
def create_registry() -> HandlerRegistry:
"""Set up the message handler registry."""
registry = HandlerRegistry()
registry.register("ping", ping_handler)
registry.register("transcribe.start", make_transcribe_handler())
registry.register("hardware.detect", hardware_detect_handler)
registry.register("diarize.start", make_diarize_handler())
registry.register("diarize.download", make_diarize_download_handler())
registry.register("pipeline.start", make_pipeline_handler())
registry.register("export.start", make_export_handler())
registry.register("ai.chat", make_ai_chat_handler())
return registry
def main() -> None:
"""Main loop: read messages from stdin, dispatch to handlers, write responses to stdout."""
# Handle clean shutdown
def shutdown(signum: int, frame: object) -> None:
print("[sidecar] Shutting down...", file=sys.stderr, flush=True)
sys.exit(0)
signal.signal(signal.SIGTERM, shutdown)
signal.signal(signal.SIGINT, shutdown)
registry = create_registry()
# Signal to Rust that we're ready
write_message(ready_message())
print("[sidecar] Ready and waiting for messages", file=sys.stderr, flush=True)
# Message loop
while True:
msg = read_message()
if msg is None:
# EOF — parent closed stdin, time to exit
print("[sidecar] EOF on stdin, exiting", file=sys.stderr, flush=True)
break
response = registry.handle(msg)
if response is not None:
write_message(response)
if __name__ == "__main__":
main()