Files
javis_bot/src/voice_worker/chat_bridge.py
javis-bot 04b603facd feat(app): an activity log of what Jarvis did (chat actions, schedule deliveries)
Each chat channel action the brain's tool asked for and each schedule
delivery is recorded in activity/<day>.jsonl by kind, target, who asked and
outcome, never its text, kept 30 days. The worker's chat_action carries the
asker. A 한 일 screen lists the newest entries.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-14 16:51:18 +09:00

50 lines
2.1 KiB
Python

"""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,
user: str | None = None):
self._session = session
self._user = user # who asked: the app records it with the action (admin.spec.md, "Activity")
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:
args = {**kw, "user": self._user} if self._user else kw
fut = asyncio.run_coroutine_threadsafe(self._session.chat_action(action, channel_id, args), 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__})"}