Files
javis_bot/src/voice_worker/recordings.py
javis-bot cbe898d3d0 feat(app): log screen saves (shown lines, conversation, voice recordings), stop-or-finish on a new request, video pages that play
- 로그 screen: "보이는 글 txt로 저장" (logs_save_text), a 대화 view that saves the days'
  conversation with every trace in full (history_export), and a 목소리 view with the
  voice.record switch and the days' recordings as a zip with an index (voice_export).
- Voice worker: with JARVIS_RECORDINGS_DIR (voice.record, off by default) each final and each
  spoken answer is kept as a WAV with an index line; day folders older than 7 days removed.
- response.interrupt (default on): off, latest/important finish the running answer first.
- Bot browser: Chrome lets pages start video and sound; botBrowser reports each page's media
  state, waits for a player to start after open/click, and presses play on a video page a click
  led to when its player stayed paused (live: a clicked YouTube video stayed paused).
- webSearch reads `query` when `search_query` is missing (live: both searches of an answer failed).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 15:27:57 +09:00

94 lines
4.1 KiB
Python

"""Voice recordings for checking what was heard and said (voice_worker.spec.md, "Recordings").
Off unless the app sets JARVIS_RECORDINGS_DIR (setting `voice.record`). Each finished utterance and
each spoken answer becomes a WAV file in a folder per local day, with one JSON line per file in that
day's `index.jsonl` (time, who, text). Day folders older than KEEP_DAYS are removed when the worker
starts. Writing happens on a thread of its own and a failure never stops the voice path.
"""
from __future__ import annotations
import json
import re
import shutil
import time
import wave
from concurrent.futures import ThreadPoolExecutor
from datetime import date, timedelta
from pathlib import Path
import numpy as np
from .log import debug_log
RECORDINGS_ENV = "JARVIS_RECORDINGS_DIR"
KEEP_DAYS = 7
_DAY = re.compile(r"^\d{4}-\d{2}-\d{2}$")
def _safe_id(value: str) -> str:
return re.sub(r"[^0-9A-Za-z_-]", "_", value)[:40] or "x"
class Recorder:
def __init__(self, root: str | Path, keep_days: int = KEEP_DAYS, now: float | None = None):
self.root = Path(root)
self.keep_days = keep_days
self.root.mkdir(parents=True, exist_ok=True)
self._pool = ThreadPoolExecutor(max_workers=1, thread_name_prefix="recordings")
self.purge(time.time() if now is None else now)
def purge(self, now: float) -> int:
"""Remove day folders older than keep_days (by their name, the local day)."""
oldest = date.fromtimestamp(now) - timedelta(days=self.keep_days - 1)
removed = 0
for d in self.root.iterdir():
if d.is_dir() and _DAY.match(d.name) and date.fromisoformat(d.name) < oldest:
shutil.rmtree(d, ignore_errors=True)
removed += 1
if removed:
debug_log(f"recordings: removed {removed} old day folders", "voice_worker")
return removed
def said(self, ts: float, speaker: str, name: str, channel: str | None, text: str, audio: np.ndarray,
rate: int = 16000) -> None:
"""A finished utterance: float samples in -1..1 at `rate`, as it was cut for transcription."""
pcm = (np.clip(np.asarray(audio, dtype=np.float32), -1.0, 1.0) * 32767).astype("<i2")
self._submit(ts, f"said-{_safe_id(speaker)}", pcm, rate,
{"kind": "said", "who": speaker, "name": name, "channel": channel, "text": text})
def spoke(self, ts: float, rid: str, text: str, pcm: np.ndarray, rate: int, cancelled: bool) -> None:
"""A spoken answer: int16 mono samples at `rate`, as far as it was made."""
self._submit(ts, "jarvis", np.asarray(pcm, dtype="<i2"), rate,
{"kind": "spoke", "who": "jarvis", "id": rid, "text": text, "cancelled": cancelled})
def close(self) -> None:
self._pool.shutdown(wait=True)
def _submit(self, ts: float, tag: str, pcm: np.ndarray, rate: int, entry: dict) -> None:
try:
self._pool.submit(self._write, ts, tag, pcm.copy(), rate, entry)
except RuntimeError: # closed
pass
def _write(self, ts: float, tag: str, pcm: np.ndarray, rate: int, entry: dict) -> None:
try:
lt = time.localtime(ts)
day = self.root / time.strftime("%Y-%m-%d", lt)
day.mkdir(parents=True, exist_ok=True)
stem = f"{time.strftime('%H%M%S', lt)}-{int((ts % 1) * 1000):03d}-{tag}"
path, n = day / f"{stem}.wav", 1
while path.exists():
n += 1
path = day / f"{stem}-{n}.wav"
with wave.open(str(path), "wb") as w:
w.setnchannels(1)
w.setsampwidth(2)
w.setframerate(rate)
w.writeframes(pcm.tobytes())
line = {"file": path.name, "at": ts, "ms": len(pcm) * 1000 // rate, **entry}
with open(day / "index.jsonl", "a", encoding="utf-8") as f:
f.write(json.dumps(line, ensure_ascii=False) + "\n")
except Exception as e: # noqa: BLE001 - a recording never stops the voice path
debug_log(f"recordings: could not write ({type(e).__name__})", "voice_worker")