Files
javis_bot/src/jarvis/reply/turn_state.py
javis-bot fe4e296361 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>
2026-11-11 14:01:33 +09:00

74 lines
2.3 KiB
Python

"""Per-turn, request-scoped state shared between the bridge, the reply engine
and tools — without threading extra parameters through the whole engine call
chain (run_reply_engine -> run_tool_with_retries -> execute_tool -> execute).
Flask serves each HTTP request on its own thread and the reply engine + tools
run synchronously on that thread, so a ``threading.local`` cleanly scopes a
single turn. Anything that can't see the thread-local (a worker thread, an eval,
a unit test) just reads ``None`` and the caller falls back to its default.
Fields
------
broadcasting : Optional[bool]
The live screen-share (Go-Live) state the bot sends with the request. The
webSearch routing reads it to pick on-screen Chrome (live) vs Gemini (off).
broadcast_action : Optional[str]
A directive ("start"/"stop") recorded by the setBroadcast tool for the
bridge to return to the bot, which owns the actual broadcast.
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
import threading
from typing import Optional
_state = threading.local()
def reset() -> None:
"""Clear per-turn state. Call at the start of each request."""
_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:
_state.user = user_id
def get_user() -> Optional[str]:
return getattr(_state, "user", None)
def set_broadcasting(value: Optional[bool]) -> None:
_state.broadcasting = value
def get_broadcasting() -> Optional[bool]:
return getattr(_state, "broadcasting", None)
def request_broadcast(action: str) -> None:
"""Record a broadcast directive. ``action`` is "start" or "stop"."""
_state.broadcast_action = action
def get_broadcast_action() -> Optional[str]:
return getattr(_state, "broadcast_action", None)