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:
@@ -28,8 +28,9 @@ class FakeBrain:
|
||||
self.answer = answer
|
||||
self.calls = []
|
||||
|
||||
def reply(self, user, text, history):
|
||||
def reply(self, user, text, history, chat=None):
|
||||
self.calls.append((user, text, history))
|
||||
self.chat = chat
|
||||
return self.answer(user, text)
|
||||
|
||||
|
||||
@@ -369,3 +370,90 @@ def test_the_brain_tells_tools_who_is_asking(tmp_path, monkeypatch):
|
||||
b.reply("200", "q", HistoryContext())
|
||||
assert seen == ["100", "200"]
|
||||
assert turn_state.get_user() is None # nothing left behind on this thread
|
||||
|
||||
|
||||
CHANNELS = {"type": "chat_channels", "bot": "900", "channels": [
|
||||
{"id": "c1", "name": "일정", "description": "약속", "read": True, "send": True, "edit": True,
|
||||
"react": True, "delete": False, "pin": False}]}
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
def test_the_brain_gets_the_registered_channels_and_their_messages(store):
|
||||
seen = {}
|
||||
|
||||
def answer(user, text):
|
||||
chat = brain.chat
|
||||
seen["channels"] = chat.channels()
|
||||
seen["read"] = chat.read("c1", 5)
|
||||
seen["search"] = chat.search(["c1"], "회의", 5)
|
||||
seen["turn"] = chat.turn
|
||||
return "ok"
|
||||
|
||||
brain = FakeBrain(answer)
|
||||
|
||||
async def body(url):
|
||||
async with connect(url) as ws:
|
||||
await hello(ws)
|
||||
await ws.send(json.dumps(CHANNELS))
|
||||
await send(ws, type="chat_message", channel="c1", message_id="m1", author="200", name="Bob",
|
||||
text="회의는 3시", ts=1_800_000_000.0)
|
||||
await send(ws, type="reply", id="r1", user="100", text="q", via="chat", speak=False)
|
||||
await recv_until(ws, lambda m: isinstance(m, dict) and m.get("type") == "reply_text")
|
||||
await send(ws, type="reply", id="r2", user="100", text="q", via="chat", speak=False)
|
||||
await recv_until(ws, lambda m: isinstance(m, dict) and m.get("id") == "r2")
|
||||
|
||||
asyncio.run(with_worker(body, store=store, brain=brain))
|
||||
assert [c["name"] for c in seen["channels"]] == ["일정"] and seen["channels"][0]["send"] is True
|
||||
assert [(m["message_id"], m["author_name"], m["text"]) for m in seen["read"]] == [("m1", "Bob", "회의는 3시")]
|
||||
assert [m["text"] for m in seen["search"]] == ["회의는 3시"]
|
||||
assert seen["turn"] == 2 # the same person's second reply
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
def test_a_chat_action_goes_to_the_app_and_its_answer_comes_back(store):
|
||||
results = []
|
||||
brain = FakeBrain(lambda u, t: results.append(brain.chat.act("send", "c1", text="안녕")) or "done")
|
||||
|
||||
async def body(url):
|
||||
async with connect(url) as ws:
|
||||
await hello(ws)
|
||||
await ws.send(json.dumps(CHANNELS))
|
||||
await send(ws, type="reply", id="r1", user="100", text="q", via="chat", speak=False)
|
||||
seen = await recv_until(ws, lambda m: isinstance(m, dict) and m.get("type") == "chat_action")
|
||||
action = seen[-1]
|
||||
await send(ws, type="chat_action_result", id=action["id"], ok=True, message_ids=["555"])
|
||||
await recv_until(ws, lambda m: isinstance(m, dict) and m.get("type") == "reply_text")
|
||||
return action
|
||||
|
||||
action = asyncio.run(with_worker(body, store=store, brain=brain))
|
||||
assert {k: action[k] for k in ("action", "channel", "text")} == {"action": "send", "channel": "c1", "text": "안녕"}
|
||||
assert results == [{"ok": True, "message_ids": ["555"]}]
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
def test_a_chat_action_the_app_never_answers_fails(store):
|
||||
results = []
|
||||
brain = FakeBrain(lambda u, t: results.append(brain.chat.act("send", "c1", text="x")) or "done")
|
||||
|
||||
async def body(url):
|
||||
async with connect(url) as ws:
|
||||
await hello(ws)
|
||||
await ws.send(json.dumps(CHANNELS))
|
||||
await send(ws, type="reply", id="r1", user="100", text="q", via="chat", speak=False)
|
||||
await recv_until(ws, lambda m: isinstance(m, dict) and m.get("type") == "reply_text")
|
||||
|
||||
worker = VoiceWorker(TOKEN, LevelTranscriber(), AmplitudeVad, None, history=store, brain=brain,
|
||||
chat_timeout_s=0.3)
|
||||
|
||||
async def run():
|
||||
server = await worker.serve("127.0.0.1", 0)
|
||||
port = server.sockets[0].getsockname()[1]
|
||||
try:
|
||||
await asyncio.wait_for(body(f"ws://127.0.0.1:{port}"), 15)
|
||||
finally:
|
||||
server.close()
|
||||
await server.wait_closed()
|
||||
await worker.stop()
|
||||
|
||||
asyncio.run(run())
|
||||
assert results and results[0]["ok"] is False and results[0]["error"]
|
||||
|
||||
Reference in New Issue
Block a user