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:
javis-bot
2026-11-11 14:01:33 +09:00
parent 3dbf1e8c56
commit fe4e296361
25 changed files with 1058 additions and 26 deletions

View File

@@ -1137,7 +1137,13 @@ def run_reply_engine(db: "Database", cfg, tts: Optional[Any],
# A positive single-step ``["Reply to the user."]`` plan is NOT the
# same as ``[]``: it's the planner deciding no memory or tools are
# needed. Both cases are preserved for the engine to distinguish.
_all_builtin_names = list(BUILTIN_TOOLS.keys())
# chatChannels exists only when the v2 brain gave this turn a chat backend
# (chat_channels.spec.md); otherwise it is not in the catalogue at all.
from .turn_state import get_chat as _get_chat
_chat_backend = _get_chat()
_builtin_tools = {k: v for k, v in BUILTIN_TOOLS.items()
if k != "chatChannels" or _chat_backend is not None}
_all_builtin_names = list(_builtin_tools.keys())
_all_mcp_names = list(mcp_tools.keys())
_full_catalog_names = _all_builtin_names + _all_mcp_names
@@ -1171,7 +1177,7 @@ def run_reply_engine(db: "Database", cfg, tts: Optional[Any],
_router_cache_key = (
f"router:{redacted}|"
f"{strategy.value}|"
f"{','.join(sorted(BUILTIN_TOOLS.keys()))}|"
f"{','.join(sorted(_builtin_tools.keys()))}|"
f"{','.join(sorted((mcp_tools or {}).keys()))}"
)
_cached_routed = (
@@ -1184,7 +1190,7 @@ def run_reply_engine(db: "Database", cfg, tts: Optional[Any],
else:
routed_tools = select_tools(
query=redacted,
builtin_tools=BUILTIN_TOOLS,
builtin_tools=_builtin_tools,
mcp_tools=mcp_tools,
strategy=strategy,
llm_base_url=cfg.ollama_base_url,
@@ -1205,8 +1211,7 @@ def run_reply_engine(db: "Database", cfg, tts: Optional[Any],
# next turn is cheap and almost always recovers.
_router_returned_full_catalog = (
routed_tools is not None
and len(routed_tools) == len(_full_catalog_names)
and set(routed_tools) == set(_full_catalog_names)
and set(routed_tools) >= set(_full_catalog_names)
)
if (
dialogue_memory
@@ -1268,6 +1273,12 @@ def run_reply_engine(db: "Database", cfg, tts: Optional[Any],
and "setBroadcast" not in routed_tools:
routed_tools = routed_tools + ["setBroadcast"]
# v2: offer the chat channel tool on every turn that has a chat backend.
# "채팅 채널에 올려 줘" is language-agnostic intent the router may not
# surface; the tool does nothing unless called.
if _chat_backend is not None and "chatChannels" not in routed_tools:
routed_tools = routed_tools + ["chatChannels"]
# In screen-share mode, always offer the on-screen browser control too. The
# small router reflexively picks webSearch for any "search/open/find" intent
# and never surfaces controlBrowser, so the model never gets the option to
@@ -2630,7 +2641,7 @@ def run_reply_engine(db: "Database", cfg, tts: Optional[Any],
# tool in the registry; otherwise stray prose lines
# like "No additional tools found for that description."
# get treated as tool names and pollute the allow-list.
_valid_names = set(BUILTIN_TOOLS.keys())
_valid_names = set(_builtin_tools.keys())
if mcp_tools:
_valid_names.update(mcp_tools.keys())
for line in (result.reply_text or "").splitlines():

View File

@@ -19,6 +19,9 @@ user : Optional[str]
v2: the Discord user ID of the person asking, set by the voice worker's
brain. Tools that keep personal data (meals) store and read it under this
person. None is v1's single user.
chat : Optional[object]
v2: the chat channel backend for this turn (chat_channels.spec.md). None
means the chatChannels tool is not offered.
"""
from __future__ import annotations
@@ -34,6 +37,15 @@ def reset() -> None:
_state.broadcasting = None
_state.broadcast_action = None
_state.user = None
_state.chat = None
def set_chat(backend) -> None:
_state.chat = backend
def get_chat():
return getattr(_state, "chat", None)
def set_user(user_id: Optional[str]) -> None: