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>
This commit is contained in:
@@ -74,11 +74,18 @@ def main() -> int:
|
||||
collector = Collector(load_config(os.environ["JARVIS_COLLECT_PATH"]))
|
||||
collector.start()
|
||||
|
||||
# Voice recordings for checking what was heard and said, only when the app turns them on.
|
||||
recorder = None
|
||||
from .recordings import RECORDINGS_ENV, Recorder
|
||||
if os.environ.get(RECORDINGS_ENV):
|
||||
recorder = Recorder(os.environ[RECORDINGS_ENV])
|
||||
|
||||
async def run() -> None:
|
||||
worker = VoiceWorker(token, transcriber, vad.stream, tts, history=history, brain=brain)
|
||||
worker = VoiceWorker(token, transcriber, vad.stream, tts, history=history, brain=brain, recorder=recorder)
|
||||
server = await worker.serve(args.host, args.port)
|
||||
debug_log(f"voice worker listening on {args.host}:{args.port} tts={'on' if tts else 'off'} "
|
||||
f"history={'on' if history else 'off'} brain={'on' if brain else 'off'}",
|
||||
f"history={'on' if history else 'off'} brain={'on' if brain else 'off'} "
|
||||
f"recordings={'on' if recorder else 'off'}",
|
||||
"voice_worker")
|
||||
print(f"READY {args.host}:{args.port}", flush=True)
|
||||
await server.serve_forever()
|
||||
@@ -94,6 +101,8 @@ def main() -> int:
|
||||
tts.close()
|
||||
if history:
|
||||
history.close()
|
||||
if recorder:
|
||||
recorder.close()
|
||||
return 0
|
||||
|
||||
|
||||
|
||||
93
src/voice_worker/recordings.py
Normal file
93
src/voice_worker/recordings.py
Normal file
@@ -0,0 +1,93 @@
|
||||
"""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")
|
||||
@@ -93,7 +93,7 @@ class VoiceWorker:
|
||||
def __init__(self, token: str, transcriber: Transcriber | None, vad_factory: Callable[[], VadStream],
|
||||
tts_engine: TtsEngine | None = None, hello_timeout_s: float = 5.0, tick_s: float = 0.05,
|
||||
history=None, brain: Brain | None = None, chat_timeout_s: float = 15.0,
|
||||
delivery_timeout_s: float = 30.0):
|
||||
delivery_timeout_s: float = 30.0, recorder=None):
|
||||
if not token:
|
||||
raise ValueError("a connection token is required")
|
||||
self.token = token
|
||||
@@ -103,6 +103,7 @@ class VoiceWorker:
|
||||
self.hello_timeout_s = hello_timeout_s
|
||||
self.tick_s = tick_s
|
||||
self.history = history # jarvis.memory.history.HistoryStore or None
|
||||
self.recorder = recorder # recordings.Recorder (voice.record on) or None
|
||||
self.brain = brain
|
||||
# One reply at a time per lane: voice answers never wait behind another person's chat answer
|
||||
# (conversation.md: with an "answer everything" chat channel the LLM takes two or more
|
||||
@@ -245,6 +246,8 @@ class Session:
|
||||
self.names: dict[str, str] = {}
|
||||
self.speakers: dict[str, _Speaker] = {}
|
||||
self.tts_requests: dict[str, TtsRequest] = {}
|
||||
# With recordings on: each spoken answer's text and mono samples until it is done.
|
||||
self.tts_recorded: dict[str, dict] = {}
|
||||
# Spoken replies whose speech was cancelled while the brain was still answering.
|
||||
self.cancelled_replies: set[str] = set()
|
||||
self.thinking_replies: set[str] = set()
|
||||
@@ -368,6 +371,8 @@ class Session:
|
||||
store = self.worker.history
|
||||
if store is not None and channel and res.text.strip():
|
||||
store.add_voice(channel, speaker, name, res.text, ts)
|
||||
if self.worker.recorder is not None:
|
||||
self.worker.recorder.said(ts, speaker, name, channel, res.text, ev.audio, SR)
|
||||
self.send({"type": "final", "speaker": speaker, "utt": ev.utt, "text": res.text,
|
||||
"end_reason": ev.reason, "audio_ms": len(ev.audio) * 1000 // SR,
|
||||
"wait_ms": int((time.monotonic() - ev.decided_at) * 1000), "stt_ms": res.stt_ms})
|
||||
@@ -456,6 +461,9 @@ class Session:
|
||||
elif req.closed:
|
||||
self.error("bad_message", "this tts id is already closed", id=rid)
|
||||
return
|
||||
if self.worker.recorder is not None:
|
||||
rec = self.tts_recorded.setdefault(rid, {"text": "", "pcm": [], "at": None})
|
||||
rec["text"] += text
|
||||
tts.feed(req, text, more)
|
||||
|
||||
def _on_tts_cancel(self, msg: dict) -> None:
|
||||
@@ -936,6 +944,10 @@ class Session:
|
||||
def _tts_chunk_cb(self, rid: str):
|
||||
def cb(seq: int, pcm: np.ndarray) -> None:
|
||||
pcm = np.asarray(pcm, dtype="<i2")
|
||||
rec = self.tts_recorded.get(rid)
|
||||
if rec is not None:
|
||||
rec["at"] = rec["at"] or time.time()
|
||||
rec["pcm"].append(pcm.copy())
|
||||
if self.tts_format["channels"] == 2:
|
||||
pcm = np.repeat(pcm, 2)
|
||||
self.send(encode_tts_frame(rid, seq, pcm.tobytes()))
|
||||
@@ -943,6 +955,10 @@ class Session:
|
||||
|
||||
def _tts_done(self, req: TtsRequest) -> None:
|
||||
self.tts_requests.pop(req.id, None)
|
||||
rec = self.tts_recorded.pop(req.id, None)
|
||||
if rec is not None and rec["pcm"] and self.worker.recorder is not None:
|
||||
self.worker.recorder.spoke(rec["at"], req.id, rec["text"], np.concatenate(rec["pcm"]), req.rate,
|
||||
req.cancelled or req.failed)
|
||||
self.send({"type": "tts_done", "id": req.id, "chunks": req.chunks, "cancelled": req.cancelled,
|
||||
"failed": req.failed})
|
||||
self._delivered(req.id, not (req.cancelled or req.failed))
|
||||
|
||||
@@ -466,6 +466,24 @@ The app restarts the LLM with another placement when a game starts or ends (app/
|
||||
- The app sends its latest `llm` message again first on every connection, so a worker that missed
|
||||
one (a dropped connection, a restarted worker) catches up.
|
||||
|
||||
### Recordings
|
||||
|
||||
For checking what was heard against what was transcribed, and what Jarvis said. Off unless the app
|
||||
sets `JARVIS_RECORDINGS_DIR` (setting `voice.record`, off by default; the app passes
|
||||
`<data folder>/recordings` at start). Then `voice_worker/recordings.py` keeps:
|
||||
|
||||
- each finished utterance (every final, also one with no text) as a 16 kHz mono WAV of the clip as it
|
||||
was cut for transcription, before loudness levelling: `<day>/<HHMMSS>-<ms>-said-<speaker>.wav`;
|
||||
- each spoken answer (every `tts` request that made sound) as a mono WAV at the session's TTS rate,
|
||||
as far as it was made (also when cancelled or failed): `<day>/<HHMMSS>-<ms>-jarvis.wav`, timed at
|
||||
its first sentence;
|
||||
- one JSON line per file in `<day>/index.jsonl`: `{file, at, ms, kind: said|spoke, who, name,
|
||||
channel, text, id, cancelled}` (the transcript, or the text sent to `tts`).
|
||||
|
||||
`<day>` is the local day. Files are written on a thread of their own, so the voice path never waits,
|
||||
and a failed write is a debug line (no text) that changes nothing else. Day folders older than 7 days
|
||||
are removed when the worker starts. Nothing is recorded without the variable.
|
||||
|
||||
### Errors
|
||||
|
||||
`{"type": "error", "code": "...", "message": "...", "id": "..."}`. Codes: `bad_message` (unknown type,
|
||||
|
||||
Reference in New Issue
Block a user