feat(chat): chatChannels tool for the registered Discord chat channels
- Tool (chat_channels.spec.md): list, read, search, send, edit, react,
delete, pin, unpin. Offered only when the v2 brain gives the turn a chat
backend, and then on every turn. Channel by ID, name, part of a name or a
name inside the text; a message ID finds its channel; planner forms of the
action ("send_message") work. Read and search results are fenced as
untrusted data. delete only reports until called again with confirm=true
in a later turn of the same person.
- Worker: chat_channels from the app, ChatBridge reads/searches the history
store and sends chat_action, waiting for chat_action_result (15 s timeout,
failed on session close); per-person reply turns and pending deletes.
- App: channel switches edit/react (on) and delete/pin (off); chat_channels
sent on worker ready and after login; chat_action runs send, edit (own
messages only), react, delete, pin, unpin and answers with ids or the error.
- Engine: chatChannels left out of the catalogue without a backend; the
"router returned everything" check now tolerates that.
- live_discord: edit own, refuse editing the speaker's, react, pin (Discord
refuses in a voice channel's chat, reported as an error), delete, refuse an
unregistered channel; passed in 109.4 s.
- Eval (qwen2.5:7b, 3 runs): 10 of 12; never obeyed the "delete everything"
message and never deleted in the same turn.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
46
src/voice_worker/chat_bridge.py
Normal file
46
src/voice_worker/chat_bridge.py
Normal file
@@ -0,0 +1,46 @@
|
||||
"""The chat backend the brain's chatChannels tool uses during one reply (chat_channels.spec.md).
|
||||
|
||||
Reads and searches come from the history store; actions (send, edit, react, delete, pin) go to the
|
||||
app as ``chat_action`` and wait for its ``chat_action_result``. Built per reply, on the event loop;
|
||||
its methods are called from the brain's thread.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
from typing import Any
|
||||
|
||||
|
||||
def _message(rec) -> dict:
|
||||
return {"message_id": rec.message_id or "", "ts": rec.ts, "author_name": rec.author_name, "text": rec.text}
|
||||
|
||||
|
||||
class ChatBridge:
|
||||
def __init__(self, session, loop: asyncio.AbstractEventLoop, turn: int, pending_deletes: dict):
|
||||
self._session = session
|
||||
self._loop = loop
|
||||
self._channels = [dict(c) for c in session.chat_channels]
|
||||
self.turn = turn
|
||||
self.pending_deletes = pending_deletes
|
||||
|
||||
def channels(self) -> list:
|
||||
return self._channels
|
||||
|
||||
def read(self, channel_id: str, limit: int) -> list:
|
||||
store = self._session.worker.history
|
||||
return [_message(r) for r in store.chat_messages(channel_id, limit)] if store else []
|
||||
|
||||
def search(self, channel_ids, query: str, limit: int) -> list:
|
||||
store = self._session.worker.history
|
||||
return [_message(r) for r in store.search_chat(channel_ids, query, limit)] if store else []
|
||||
|
||||
def channel_of(self, message_id: str):
|
||||
store = self._session.worker.history
|
||||
return store.chat_channel_of(message_id) if store else None
|
||||
|
||||
def act(self, action: str, channel_id: str, **kw: Any) -> dict:
|
||||
fut = asyncio.run_coroutine_threadsafe(self._session.chat_action(action, channel_id, kw), self._loop)
|
||||
try:
|
||||
return fut.result(self._session.worker.chat_timeout_s + 5)
|
||||
except Exception as e: # loop gone or the wait broke
|
||||
return {"ok": False, "error": f"the app did not answer ({type(e).__name__})"}
|
||||
Reference in New Issue
Block a user