feat(voice_worker): record history from the app and answer reply requests with the brain

Worker (voice_worker.spec.md "History and brain"):
- --history <file> keeps the conversation history store; --brain hosts the
  jarvis reply engine (JarvisBrain, settings point ollama_base_url at /v1).
- voice_join / voice_leave / member_join / member_leave keep presence; the
  session end closes it. Non-empty finals are stored under the speaker's name
  in the current voice channel.
- chat_message / chat_edit / chat_delete keep registered chat channels.
- reply builds the asker's scoped history context, runs the brain on a thread
  (one at a time), answers reply_text, stores a voice answer, and with
  speak=true says it under the same id. memory settings in hello and
  memory_config. brain_unavailable without --brain.

App (discord_core.spec.md "History reporting"):
- WorkerClient sends the new messages; the core reports the voice channel
  and its heard members after a join, a move and every worker reconnect,
  member joins and leaves, voice_leave when a session ends, and every message
  of a readable registered channel (its own too, with `to` on replies), edits
  and deletes.
- live_discord runs the worker with --history and checks the store at the
  end: 8 finals under the speaker's name, the edited chat message only,
  Jarvis's own messages, 5 presence intervals, none left open (passed, 107.8 s).

Eval: history recall gains a chat-record case and fails a reply that reads a
record stamp out (field run with qwen2.5:7b did once in four).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
javis-bot
2026-11-11 13:16:24 +09:00
parent 7a288129de
commit 864b2e7fdb
13 changed files with 775 additions and 18 deletions

View File

@@ -24,6 +24,7 @@ WebSocket. Design background lives in `docs/v2/voice-and-llm.md` and `docs/v2/ar
JARVIS_VOICE_TOKEN=<secret> python -m voice_worker --host 127.0.0.1 --port 8771 \
[--stt-model large-v3-turbo] [--stt-device cuda] [--stt-compute int8_float16] \
[--melo-python <path to the MeloTTS venv python>] [--melo-device cuda] [--melo-speed 1.5]
[--history <history.sqlite>] [--brain]
```
Run from `src/` (or with `src/` on `PYTHONPATH`) in a venv built from `voice_worker/requirements.txt`. MeloTTS needs its own Python 3.11 venv
@@ -176,10 +177,57 @@ is ready. Requests are served first come, first served by one TTS engine. `tts_c
request's unsent sentences, and `tts_done` follows with `cancelled: true`. A sentence that fails to
synthesise is reported with `error` (`code: "tts_failed"`) and the request ends with `failed: true`.
### History and brain (M3)
The worker also hosts the brain (the existing `jarvis` reply engine) and the conversation history
store (`src/jarvis/memory/history.spec.md`). Start it with `--history <file>` to keep history and
`--brain` to answer (`--brain` needs `--history`; it loads the jarvis settings, whose
`ollama_base_url` points at Strata's `/v1`). Without them the messages below are accepted and
ignored, except `reply`, which answers `error` `brain_unavailable`.
The app tells the worker where Jarvis is and who is there. Speaker IDs are Discord user IDs, the
same strings as in audio frames. Times are taken by the worker on arrival, except chat messages,
which carry Discord's own time.
```
-> {"type": "voice_join", "channel": "<voice channel id>"} // Jarvis joined (or moved to) this channel
-> {"type": "voice_leave"} // Jarvis left the voice channel
-> {"type": "member_join", "user": "<id>", "name": "<display name>"} // also for everyone already there
-> {"type": "member_leave", "user": "<id>"}
-> {"type": "chat_message", "channel": "...", "message_id": "...", "author": "<id>", "name": "...",
"text": "...", "ts": 1762830000.5, "to": "<id>"} // to: only on Jarvis's own answer to someone
-> {"type": "chat_edit", "channel": "...", "message_id": "...", "text": "..."}
-> {"type": "chat_delete", "channel": "...", "message_id": "..."}
-> {"type": "reply", "id": "r1", "user": "<id>", "text": "...", "via": "voice", "speak": true}
<- {"type": "reply_text", "id": "r1", "text": "...", "failed": false}
```
- `voice_join` closes the presence of everyone in the previous channel and starts a new one;
`voice_leave` and the end of the session close it, because nothing said after that is heard.
`member_join` opens the user's presence in the current channel and remembers their name;
`member_leave` closes it. Members outside a voice channel are ignored.
- Every non-empty `final` is stored as a `voice` record of the current channel under the speaker's
name (the user ID until `member_join` names them). A final outside a voice channel is not stored.
- Chat messages are stored only as given; the app sends only registered channels.
- `reply` asks the brain to answer `user`'s `text`. The worker builds the history context for that
user (their scope only) and runs the reply engine on a worker thread, one reply at a time.
`reply_text` carries the answer (`failed: true` with empty text when the brain gave none). A voice
answer (`via: "voice"`) is stored as a `reply` record to the user in the current channel; a chat
answer is stored when the app reports the sent message as `chat_message` with `to`. With
`speak: true` the answer is also spoken through the TTS path under the same id (audio frames and
`tts_done`), and `tts_cancel` with that id stops it. Each user keeps their own short-term dialogue
memory (tool carry-over) across replies.
- Memory settings, optional in `hello` as `"memory": {...}` and changeable with
`{"type": "memory_config", ...}`: `chat` (true), `others` (true), `off` (false), `since_days`
(null: whole history), `recent_minutes` (30), `recent_records` (20), `matches` (5),
`budget_tokens` (4000).
- Logs carry IDs, counts and timings only, never record, question or answer text.
### Errors
`{"type": "error", "code": "...", "message": "...", "id": "..."}`. Codes: `bad_message` (unknown type,
bad JSON, bad field), `bad_frame` (malformed binary frame), `tts_unavailable`, `tts_failed`. Errors
bad JSON, bad field), `bad_frame` (malformed binary frame), `tts_unavailable`, `tts_failed`,
`brain_unavailable`. Errors
never close the connection after the handshake.
## Debug logging