feat(app): answer registered chat channels in chat

- chatChannels[].answer: wakeWord (default) or everything. Chat uses
  call.chatWakeWords (else the voice wake words) with the shared
  designated-people switches.
- The brain's answer (reply via chat, not spoken) is posted as a Discord
  reply to the message, pinging nobody; failed or empty answers post
  nothing. Bots, channels with sending off and stops are not answered.
- Chat runs beside the voice conversation and never takes its turn.
- Core: send_chat_reply, chat_answer and the ReplyText event; the worker
  client's reply takes via.
- Specs (conversation, discord core) and the roadmap updated.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
javis-bot
2026-11-11 22:36:43 +09:00
parent de35573e5f
commit a70ccda37e
11 changed files with 284 additions and 16 deletions

View File

@@ -178,7 +178,7 @@ mod tests {
fn ch(id: u64, read: bool, send: bool) -> ChatChannelSettings { fn ch(id: u64, read: bool, send: bool) -> ChatChannelSettings {
ChatChannelSettings { id, name: String::new(), description: String::new(), read, send, edit: true, ChatChannelSettings { id, name: String::new(), description: String::new(), read, send, edit: true,
react: true, delete: false, pin: false } react: true, delete: false, pin: false, answer_all: false }
} }
#[test] #[test]

View File

@@ -30,6 +30,10 @@ pub trait Voice {
fn stop_all(&self); fn stop_all(&self);
fn ask_addressed(&self, id: &str, user: u64, text: &str) -> bool; fn ask_addressed(&self, id: &str, user: u64, text: &str) -> bool;
fn ask_rank(&self, id: &str, candidates: &[(String, u64, String)]) -> bool; fn ask_rank(&self, id: &str, candidates: &[(String, u64, String)]) -> bool;
/// Have the brain answer a chat message (not spoken); its text comes as `CoreEvent::ReplyText`.
fn chat_answer(&self, id: &str, user: u64, text: &str) -> bool;
/// Post `text` in `channel` as a reply to `message` (never pinging anyone).
fn send_reply(&self, channel: u64, message: u64, text: &str);
} }
impl Voice for DiscordCore { impl Voice for DiscordCore {
@@ -48,8 +52,53 @@ impl Voice for DiscordCore {
fn ask_rank(&self, id: &str, candidates: &[(String, u64, String)]) -> bool { fn ask_rank(&self, id: &str, candidates: &[(String, u64, String)]) -> bool {
DiscordCore::ask_rank(self, id, candidates) DiscordCore::ask_rank(self, id, candidates)
} }
fn chat_answer(&self, id: &str, user: u64, text: &str) -> bool {
DiscordCore::chat_answer(self, id, user, text)
}
fn send_reply(&self, channel: u64, message: u64, text: &str) {
let core = self.clone();
let text = text.to_owned();
tokio::spawn(async move {
if let Err(e) = core.send_chat_reply(channel, message, &text).await {
crate::error_log!("conversation", "could not post the chat answer in {channel}: {e}");
}
});
}
} }
/// How a registered chat channel is answered (`chatChannels[].answer`).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ChatChannelCall {
/// Answer every message, not only those with a wake word.
pub everything: bool,
/// Sending is on; without it no answer could be posted, so none is asked for.
pub send: bool,
}
/// Call detection for chat: the chat wake words (`call.chatWakeWords`, else the voice ones) with the
/// shared designated-people switches, and each registered channel's answer setting.
#[derive(Debug, Clone, Default)]
pub struct ChatCalls {
pub calls: CallSettings,
pub channels: HashMap<u64, ChatChannelCall>,
}
impl ChatCalls {
pub fn from_settings(s: &crate::settings::Settings) -> Self {
let mut calls = s.call.clone();
if let Some(w) = &s.chat_wake_words {
calls.wake_words = w.clone();
}
let channels = s.chat_channels.iter()
.map(|c| (c.id, ChatChannelCall { everything: c.answer_all, send: c.send }))
.collect();
Self { calls, channels }
}
}
/// Chat answers waited for longer than this are forgotten (the brain or the worker went away).
const CHAT_ANSWER_KEPT: Duration = Duration::from_secs(300);
/// A request that was not answered, or an answer that was stopped. No text: only who, why and when. /// A request that was not answered, or an answer that was stopped. No text: only who, why and when.
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq)]
pub struct Missed { pub struct Missed {
@@ -70,12 +119,49 @@ pub struct Conversation<V: Voice> {
/// Ask id -> the request it becomes on a yes, and when it was asked. /// Ask id -> the request it becomes on a yes, and when it was asked.
asks: HashMap<String, (Request, Instant)>, asks: HashMap<String, (Request, Instant)>,
missed: VecDeque<Missed>, missed: VecDeque<Missed>,
chat: ChatCalls,
/// Chat answer id -> the message it answers (channel, message) and when it was asked.
chat_answers: HashMap<String, (u64, u64, Instant)>,
} }
impl<V: Voice> Conversation<V> { impl<V: Voice> Conversation<V> {
pub fn new(voice: V, calls: CallSettings, policy: PolicySettings) -> Self { pub fn new(voice: V, calls: CallSettings, policy: PolicySettings) -> Self {
Self { voice, calls, policy: Policy::new(policy), next_id: 0, answers: HashMap::new(), Self { voice, calls, policy: Policy::new(policy), next_id: 0, answers: HashMap::new(),
by_speech: HashMap::new(), asks: HashMap::new(), missed: VecDeque::new() } by_speech: HashMap::new(), asks: HashMap::new(), missed: VecDeque::new(), chat: ChatCalls::default(),
chat_answers: HashMap::new() }
}
/// Answer the registered chat channels too.
pub fn with_chat(mut self, chat: ChatCalls) -> Self {
self.chat = chat;
self
}
fn on_chat(&mut self, m: &crate::chat::ChatMessage, now: Instant) {
// Other bots are never answered, so two bots cannot keep answering each other.
if m.author_is_bot || m.from_self {
return;
}
let Some(ch) = self.chat.channels.get(&m.channel_id).copied() else { return };
if !ch.send {
return;
}
let calls = CallSettings {
mode: if ch.everything { crate::calls::CallMode::Everything } else { crate::calls::CallMode::WakeWord },
..self.chat.calls.clone()
};
let call = calls.judge(m.author_id, &m.content);
debug_log!("conversation", "chat {} in {} from {} call={call:?}", m.message_id, m.channel_id, m.author_id);
// A chat message is answered in chat; a stop there is not a question.
if !matches!(call, Call::Request { .. }) {
return;
}
self.chat_answers.retain(|_, (_, _, at)| now < *at + CHAT_ANSWER_KEPT);
self.next_id += 1;
let id = format!("c{}", self.next_id);
if self.voice.chat_answer(&id, m.author_id, &m.content) {
self.chat_answers.insert(id, (m.channel_id, m.message_id, now));
}
} }
pub fn missed(&self) -> impl Iterator<Item = &Missed> { pub fn missed(&self) -> impl Iterator<Item = &Missed> {
@@ -85,6 +171,15 @@ impl<V: Voice> Conversation<V> {
pub fn on_event(&mut self, ev: &CoreEvent, now: Instant) { pub fn on_event(&mut self, ev: &CoreEvent, now: Instant) {
match ev { match ev {
CoreEvent::SpeechStart(h) => self.policy.utterance_open(h.speaker_id), CoreEvent::SpeechStart(h) => self.policy.utterance_open(h.speaker_id),
CoreEvent::Chat(m) => self.on_chat(m, now),
CoreEvent::ReplyText { id, text, failed } => {
if let Some((channel, message, _)) = self.chat_answers.remove(id) {
debug_log!("conversation", "chat answer {id} chars={} failed={failed}", text.chars().count());
if !failed && !text.trim().is_empty() {
self.voice.send_reply(channel, message, text);
}
}
}
CoreEvent::SpeakingStopped(user) => self.policy.speaking_stopped(*user, now), CoreEvent::SpeakingStopped(user) => self.policy.speaking_stopped(*user, now),
CoreEvent::Final(f) => { CoreEvent::Final(f) => {
let speaker = f.heard.speaker_id; let speaker = f.heard.speaker_id;
@@ -266,6 +361,8 @@ mod tests {
StopAll, StopAll,
Addressed(String, u64), Addressed(String, u64),
Rank(String, Vec<String>), Rank(String, Vec<String>),
ChatAnswer(String, u64, String),
SendReply(u64, u64, String),
} }
#[derive(Clone, Default)] #[derive(Clone, Default)]
@@ -293,6 +390,13 @@ mod tests {
self.did.borrow_mut().push(Did::Rank(id.into(), c.iter().map(|x| x.0.clone()).collect())); self.did.borrow_mut().push(Did::Rank(id.into(), c.iter().map(|x| x.0.clone()).collect()));
true true
} }
fn chat_answer(&self, id: &str, user: u64, text: &str) -> bool {
self.did.borrow_mut().push(Did::ChatAnswer(id.into(), user, text.into()));
true
}
fn send_reply(&self, channel: u64, message: u64, text: &str) {
self.did.borrow_mut().push(Did::SendReply(channel, message, text.into()));
}
} }
impl Fake { impl Fake {
fn take(&self) -> Vec<Did> { fn take(&self) -> Vec<Did> {
@@ -445,6 +549,82 @@ mod tests {
assert!(f.take().is_empty()); assert!(f.take().is_empty());
} }
const CH_WAKE: u64 = 100;
const CH_ALL: u64 = 200;
const CH_MUTE: u64 = 300;
fn chat_conv(calls: CallSettings) -> (Conversation<Fake>, Fake, Instant) {
let f = Fake::default();
let chat = ChatCalls { calls, channels: [
(CH_WAKE, ChatChannelCall { everything: false, send: true }),
(CH_ALL, ChatChannelCall { everything: true, send: true }),
(CH_MUTE, ChatChannelCall { everything: true, send: false }),
].into() };
let c = Conversation::new(f.clone(), CallSettings::default(), PolicySettings::default()).with_chat(chat);
(c, f, Instant::now())
}
fn chat(channel: u64, message: u64, author: u64, bot: bool, text: &str) -> CoreEvent {
CoreEvent::Chat(crate::chat::ChatMessage { channel_id: channel, message_id: message, author_id: author,
author_name: "x".into(), author_is_bot: bot, from_self: false, content: text.into(),
created_at: String::new() })
}
#[test]
fn a_chat_call_is_answered_as_a_reply_to_its_message() {
let (mut c, f, t) = chat_conv(CallSettings::default());
c.on_event(&chat(CH_WAKE, 5, A, false, "자비스 내일 일정 알려 줘"), t);
let Did::ChatAnswer(id, user, text) = f.take().remove(0) else { panic!() };
assert_eq!((user, text.as_str()), (A, "자비스 내일 일정 알려 줘"));
c.on_event(&CoreEvent::ReplyText { id: id.clone(), text: "내일은 비어 있어요.".into(), failed: false }, t);
assert_eq!(f.take(), vec![Did::SendReply(CH_WAKE, 5, "내일은 비어 있어요.".into())]);
// A second reply_text for the same id sends nothing.
c.on_event(&CoreEvent::ReplyText { id, text: "x".into(), failed: false }, t);
assert!(f.take().is_empty());
}
#[test]
fn chat_answers_follow_each_channels_switch_and_never_answer_bots() {
let (mut c, f, t) = chat_conv(CallSettings::default());
c.on_event(&chat(CH_WAKE, 1, A, false, "오늘 뭐 먹지"), t);
assert!(f.take().is_empty(), "wake word only");
c.on_event(&chat(CH_ALL, 2, A, false, "오늘 뭐 먹지"), t);
assert!(matches!(f.take()[..], [Did::ChatAnswer(..)]), "every message");
c.on_event(&chat(CH_MUTE, 3, A, false, "자비스 안녕"), t);
assert!(f.take().is_empty(), "sending is off: no answer could be posted");
c.on_event(&chat(CH_ALL, 4, B, true, "자비스 안녕"), t);
assert!(f.take().is_empty(), "other bots are never answered");
c.on_event(&chat(999, 6, A, false, "자비스 안녕"), t);
assert!(f.take().is_empty(), "not a registered channel");
c.on_event(&chat(CH_ALL, 7, A, false, "자비스 멈춰"), t);
assert!(f.take().is_empty(), "a stop in chat is not a question");
}
#[test]
fn chat_uses_its_own_wake_words_and_the_designated_people() {
let calls = CallSettings { wake_words: vec!["봇".into()], designated: [A].into(), designated_only: true,
..CallSettings::default() };
let (mut c, f, t) = chat_conv(calls);
c.on_event(&chat(CH_WAKE, 1, A, false, "봇 안녕"), t);
assert_eq!(f.take().len(), 1);
c.on_event(&chat(CH_WAKE, 2, B, false, "봇 안녕"), t);
assert!(f.take().is_empty(), "only designated people");
c.on_event(&chat(CH_WAKE, 3, A, false, "자비스 안녕"), t);
assert!(f.take().is_empty(), "the voice wake word is not the chat one here");
}
#[test]
fn a_failed_chat_answer_posts_nothing_and_chat_leaves_voice_alone() {
let (mut c, f, t) = chat_conv(CallSettings::default());
c.on_event(&fin(A, "자비스 날씨", 100), t);
c.on_event(&chat(CH_ALL, 9, B, false, "안녕"), t);
let did = f.take();
assert!(matches!(did[0], Did::Answer(..)) && matches!(did[1], Did::ChatAnswer(..)), "{did:?}");
let Did::ChatAnswer(id, ..) = &did[1] else { panic!() };
c.on_event(&CoreEvent::ReplyText { id: id.clone(), text: String::new(), failed: true }, t);
assert!(f.take().is_empty());
assert_eq!(c.missed().count(), 0, "a chat message never takes the voice turn");
}
#[test] #[test]
fn end_times_come_from_the_finals_wait() { fn end_times_come_from_the_finals_wait() {
let (mut c, f, t) = conv(PolicyKind::InOrder, CallMode::WakeWord); let (mut c, f, t) = conv(PolicyKind::InOrder, CallMode::WakeWord);

View File

@@ -114,12 +114,36 @@ request first, the answer stops (missed, `interrupted`) and the new request is a
running answer first, or does not come in time, the answer goes on and the new request waits. The running answer first, or does not come in time, the answer goes on and the new request waits. The
ranking is dropped when the running answer ends first. ranking is dropped when the running answer ends first.
## Chat calls
Registered chat channels are answered in chat, by text, beside the voice conversation: a chat message
never takes the voice turn and never goes through the response policy.
| Key | Default | Meaning |
|---|---|---|
| `chatChannels[].answer` | `"wakeWord"` | `wakeWord`: answer messages that call a wake word. `everything`: answer every message. |
- The wake words are `call.chatWakeWords` (or `voice.wakeWords` when it is `null`), with the same
similarity and the same designated-people switches as voice; `call.mode` is voice only.
- Only messages of a registered channel with reading on reach the loop (the core's rule), and only a
channel with sending on is answered: there an answer could not be posted, so none is asked for.
- Messages from bots (other bots and Jarvis itself) are never answered, so two bots cannot keep
answering each other.
- Only a request is answered: a stop in chat ("자비스 멈춰") is not a question and gets no answer.
- The brain answers through `reply` with `via: "chat"` and `speak: false`; the answer is posted in the
same channel as a Discord reply to the message (so the history records it as an answer to that
person), pinging nobody. A failed or empty answer posts nothing. Answers waited for over 5 minutes
are forgotten.
- Known limit: the worker writes one reply at a time, so a voice answer waits while a chat answer is
being written (the LLM's parallel requests for "answer everything" channels are M5/M6 settings).
## The conversation loop ## The conversation loop
`app/src/conversation.rs` runs between the core's events and everything else that reads them (every `app/src/conversation.rs` runs between the core's events and everything else that reads them (every
event is passed on unchanged). It talks to the core through a small `Voice` interface (answer, cancel event is passed on unchanged). It talks to the core through a small `Voice` interface (answer, cancel
one request, stop all, ask "addressed", ask "rank"), so the glue is tested with a fake. one request, stop all, ask "addressed", ask "rank"), so the glue is tested with a fake.
- `Chat` and `ReplyText`: chat calls, above.
- `SpeechStart` opens the speaker's utterance; `SpeakingStopped` marks it ending. - `SpeechStart` opens the speaker's utterance; `SpeakingStopped` marks it ending.
- `Final`: a request gets the next id, `ended_at` = arrival minus the final's `wait_ms`, and goes - `Final`: a request gets the next id, `ended_at` = arrival minus the final's `wait_ms`, and goes
through call detection. Ignore closes the utterance. Stop stops all speech, runs the policy's stop, through call detection. Ignore closes the utterance. Stop stops all speech, runs the policy's stop,
@@ -149,5 +173,6 @@ modes) and every policy rule above with fake times (settling, ordering by end ti
interruption, ranking tie, ranking timeout, stop dropping the queue), and the conversation loop with a interruption, ranking tie, ranking timeout, stop dropping the queue), and the conversation loop with a
fake voice side (answering and queueing, a busy drop recorded without text, voice stop, addressed yes, fake voice side (answering and queueing, a busy drop recorded without text, voice stop, addressed yes,
no and timeout, interruption cancelling the right speech, a ranking deciding, end times from no and timeout, interruption cancelling the right speech, a ranking deciding, end times from
`wait_ms`). `tests/worker_protocol.rs` checks an answer spoken under its id and both judgements against `wait_ms`, and chat: an answer posted as a reply once, per-channel switches, no answers to bots or to
a stop, chat wake words and designated people, a failed answer posting nothing). `tests/worker_protocol.rs` checks an answer spoken under its id and both judgements against
the real Python worker with a fake brain. the real Python worker with a fake brain.

View File

@@ -67,6 +67,8 @@ pub enum CoreEvent {
/// `SpeakEnd`s of what it stopped. /// `SpeakEnd`s of what it stopped.
Stopped, Stopped,
Chat(ChatMessage), Chat(ChatMessage),
/// The brain's answer text for a `reply` (`answer()` or `chat_answer()`).
ReplyText { id: String, text: String, failed: bool },
/// The brain's answer to `addressed()`. /// The brain's answer to `addressed()`.
Addressed { id: String, addressed: bool }, Addressed { id: String, addressed: bool },
/// The brain's answer to `rank()`: candidate ids, most important first (empty: failed). /// The brain's answer to `rank()`: candidate ids, most important first (empty: failed).
@@ -405,10 +407,15 @@ impl DiscordCore {
pub fn answer(&self, user: u64, text: &str) -> Option<String> { pub fn answer(&self, user: u64, text: &str) -> Option<String> {
self.voice_channel_id()?; self.voice_channel_id()?;
let id = with_playback(&self.inner, |p| p.adopt()); let id = with_playback(&self.inner, |p| p.adopt());
self.inner.worker.reply(&id, user, text, true); self.inner.worker.reply(&id, user, text, "voice", true);
Some(id) Some(id)
} }
/// Have the brain answer a chat message under `id`; the text comes as `CoreEvent::ReplyText`.
pub fn chat_answer(&self, id: &str, user: u64, text: &str) -> bool {
self.inner.worker.reply(id, user, text, "chat", false)
}
/// Stop one request (an answer or a `say`); the rest of the queue goes on. /// Stop one request (an answer or a `say`); the rest of the queue goes on.
pub fn cancel(&self, id: &str) { pub fn cancel(&self, id: &str) {
with_playback(&self.inner, |p| p.cancel(id)); with_playback(&self.inner, |p| p.cancel(id));
@@ -474,6 +481,27 @@ impl DiscordCore {
Ok(ids) Ok(ids)
} }
/// Send `text` to a registered channel as a reply to `message` (the first part carries the reply;
/// nobody is pinged, not even the person answered). The message ids.
pub async fn send_chat_reply(&self, channel_id: u64, message: u64, text: &str) -> Result<Vec<u64>, CoreError> {
self.inner.chat.check_send(channel_id)?;
if text.trim().is_empty() {
return Err(CoreError("nothing to send".into()));
}
let http = self.http()?;
let mut ids = vec![];
for (i, part) in split_message(text, MESSAGE_LIMIT).into_iter().enumerate() {
let mut m = CreateMessage::new().content(part).allowed_mentions(CreateAllowedMentions::new().replied_user(false));
if i == 0 {
m = m.reference_message((ChannelId::new(channel_id), MessageId::new(message)));
}
let sent = ChannelId::new(channel_id).send_message(http.as_ref(), m).await.map_err(cerr("could not send"))?;
ids.push(sent.id.get());
}
debug_log!("chat", "chat answer sent to {channel_id} parts={} chars={}", ids.len(), text.chars().count());
Ok(ids)
}
/// Do what the brain's chat channel tool asked (chat_channels.spec.md). The message ids made or /// Do what the brain's chat channel tool asked (chat_channels.spec.md). The message ids made or
/// touched. Every action needs its channel switch; edits only the bot's own messages. /// touched. Every action needs its channel switch; edits only the bot's own messages.
pub async fn chat_action(&self, a: &ChatAction) -> Result<Vec<u64>, CoreError> { pub async fn chat_action(&self, a: &ChatAction) -> Result<Vec<u64>, CoreError> {
@@ -660,6 +688,7 @@ async fn worker_task(core: std::sync::Weak<Inner>, mut rx: mpsc::UnboundedReceiv
WorkerEvent::TtsDone(d) => with_playback(&inner, |p| p.done(&d)), WorkerEvent::TtsDone(d) => with_playback(&inner, |p| p.done(&d)),
WorkerEvent::ReplyText { id, text, failed } => { WorkerEvent::ReplyText { id, text, failed } => {
debug_log!("voice", "reply {id} chars={} failed={failed}", text.chars().count()); debug_log!("voice", "reply {id} chars={} failed={failed}", text.chars().count());
let _ = inner.events.send(CoreEvent::ReplyText { id, text, failed });
} }
WorkerEvent::Addressed { id, addressed } => { WorkerEvent::Addressed { id, addressed } => {
let _ = inner.events.send(CoreEvent::Addressed { id, addressed }); let _ = inner.events.send(CoreEvent::Addressed { id, addressed });

View File

@@ -67,7 +67,7 @@ strings (numbers would lose precision).
| `voice.joinNotice` | "자비스가 이 음성 채널에 들어왔습니다. 이 채널의 대화는 자비스가 받아 적습니다." | Posted in the voice channel's chat on every join. Must not be empty. | | `voice.joinNotice` | "자비스가 이 음성 채널에 들어왔습니다. 이 채널의 대화는 자비스가 받아 적습니다." | Posted in the voice channel's chat on every join. Must not be empty. |
| `voice.wakeWords` | `["자비스"]` | Sent to the worker as STT `hotwords`, and the voice wake words of call detection. | | `voice.wakeWords` | `["자비스"]` | Sent to the worker as STT `hotwords`, and the voice wake words of call detection. |
| `voice.stt` | `{}` | Other worker STT settings (`partials`, `end_silence_ms`, `final_beam`, `language`, ...); the worker validates them. | | `voice.stt` | `{}` | Other worker STT settings (`partials`, `end_silence_ms`, `final_beam`, `language`, ...); the worker validates them. |
| `chatChannels` | `[]` | `{id, name, description, read (true), send (true), edit (true), react (true), delete (false), pin (false)}` per channel. | | `chatChannels` | `[]` | `{id, name, description, read (true), send (true), edit (true), react (true), delete (false), pin (false), answer ("wakeWord")}` per channel; `answer` is in `conversation.spec.md`. |
| `voiceWorker.python` | `python3` (`python.exe` on Windows) | Python of the worker venv. | | `voiceWorker.python` | `python3` (`python.exe` on Windows) | Python of the worker venv. |
| `voiceWorker.cwd` | the repo's `src/` (development builds) | Folder holding the `voice_worker` package. | | `voiceWorker.cwd` | the repo's `src/` (development builds) | Folder holding the `voice_worker` package. |
| `voiceWorker.args` | `[]` | Extra worker arguments, e.g. `["--melo-python", "<MeloTTS venv python>"]`. | | `voiceWorker.args` | `[]` | Extra worker arguments, e.g. `["--melo-python", "<MeloTTS venv python>"]`. |
@@ -188,6 +188,9 @@ registered until it is removed (M5 setup).
- **Send**: `send_chat(channel_id, text)` sends to a registered channel with sending on, split at - **Send**: `send_chat(channel_id, text)` sends to a registered channel with sending on, split at
Discord's 2,000-character limit at the last newline or space (never inside a character), and returns Discord's 2,000-character limit at the last newline or space (never inside a character), and returns
the message ids. the message ids.
- **Answer** (M4): `send_chat_reply(channel_id, message_id, text)` sends like Send, with the first
part as a Discord reply to the message; nobody is pinged, not even its author. `chat_answer(id, user,
text)` asks the brain for a chat answer (`reply` with `via: "chat"`); its text comes as `ReplyText`.
- A channel that is not registered, or has the needed switch off, is refused with an access error. - A channel that is not registered, or has the needed switch off, is refused with an access error.
- **Chat tool actions** (M3, `chat_channels.spec.md`): when the worker link is ready and after login, - **Chat tool actions** (M3, `chat_channels.spec.md`): when the worker link is ready and after login,
the core sends `chat_channels` (every registered channel with its switches, and the bot's user ID). the core sends `chat_channels` (every registered channel with its switches, and the bot's user ID).

View File

@@ -56,10 +56,12 @@ pub async fn start_services(data_dir: &Path) -> Result<Services, Box<dyn std::er
}); });
let (calls, response) = (settings.call.clone(), settings.response.clone()); let (calls, response) = (settings.call.clone(), settings.response.clone());
let chat_calls = conversation::ChatCalls::from_settings(&settings);
let (core, core_events) = DiscordCore::new(settings, token, worker.clone(), worker_events); let (core, core_events) = DiscordCore::new(settings, token, worker.clone(), worker_events);
// Call detection and the response policy answer what is heard; every event still reaches `events`. // Call detection and the response policy answer what is heard; every event still reaches `events`.
let (out, events) = mpsc::unbounded_channel(); let (out, events) = mpsc::unbounded_channel();
tokio::spawn(conversation::run(Conversation::new(core.clone(), calls, response), core_events, out)); let conv = Conversation::new(core.clone(), calls, response).with_chat(chat_calls);
tokio::spawn(conversation::run(conv, core_events, out));
if let Err(e) = core.start().await { if let Err(e) = core.start().await {
worker.close(); worker.close();
worker_process.stop().await; worker_process.stop().await;

View File

@@ -30,6 +30,9 @@ pub struct ChatChannelSettings {
pub delete: bool, pub delete: bool,
/// The assistant may pin and unpin messages here. Off by default. /// The assistant may pin and unpin messages here. Off by default.
pub pin: bool, pub pin: bool,
/// Answer every message here (`answer: "everything"`), not only those calling a wake word (`"wakeWord"`,
/// the default; conversation.spec.md).
pub answer_all: bool,
} }
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq)]
@@ -349,6 +352,11 @@ pub fn parse_settings(raw: &Value) -> Result<Settings, SettingsError> {
react: get_bool(Some(m), "react", &path, true)?, react: get_bool(Some(m), "react", &path, true)?,
delete: get_bool(Some(m), "delete", &path, false)?, delete: get_bool(Some(m), "delete", &path, false)?,
pin: get_bool(Some(m), "pin", &path, false)?, pin: get_bool(Some(m), "pin", &path, false)?,
answer_all: match get_str(Some(m), "answer", &path, "wakeWord")?.as_str() {
"wakeWord" => false,
"everything" => true,
_ => return err(format!("{path}.answer must be wakeWord or everything")),
},
}); });
} }
@@ -435,7 +443,7 @@ mod tests {
assert_eq!(s.voice.stt.get("end_silence_ms"), Some(&json!(250))); assert_eq!(s.voice.stt.get("end_silence_ms"), Some(&json!(250)));
assert_eq!(s.chat_channels, vec![ChatChannelSettings { id: IDN, name: "일정".into(), assert_eq!(s.chat_channels, vec![ChatChannelSettings { id: IDN, name: "일정".into(),
description: "일정 올리는 곳".into(), read: true, send: false, edit: true, react: true, delete: false, description: "일정 올리는 곳".into(), read: true, send: false, edit: true, react: true, delete: false,
pin: false }]); pin: false, answer_all: false }]);
assert_eq!(s.voice_worker.args, vec!["--melo-python", "/m/python"]); assert_eq!(s.voice_worker.args, vec!["--melo-python", "/m/python"]);
assert_eq!(s.voice_worker.cwd, PathBuf::from("/repo/src")); assert_eq!(s.voice_worker.cwd, PathBuf::from("/repo/src"));
} }
@@ -453,6 +461,7 @@ mod tests {
(json!({"chatChannels": [{"id": ID}, {"id": ID}]}), "twice"), (json!({"chatChannels": [{"id": ID}, {"id": ID}]}), "twice"),
(json!({"chatChannels": [{"id": "1"}]}), "chatChannels[0].id"), (json!({"chatChannels": [{"id": "1"}]}), "chatChannels[0].id"),
(json!({"chatChannels": {}}), "chatChannels"), (json!({"chatChannels": {}}), "chatChannels"),
(json!({"chatChannels": [{"id": ID, "answer": "sometimes"}]}), "chatChannels[0].answer"),
(json!({"voice": []}), "voice"), (json!({"voice": []}), "voice"),
(json!([]), "object"), (json!([]), "object"),
]; ];
@@ -483,6 +492,8 @@ mod tests {
assert_eq!(s.response.collect, Duration::from_millis(500)); assert_eq!(s.response.collect, Duration::from_millis(500));
assert_eq!(s.response.settle, Duration::ZERO); assert_eq!(s.response.settle, Duration::ZERO);
assert_eq!(s.response.priorities.get(&IDN), Some(&2)); assert_eq!(s.response.priorities.get(&IDN), Some(&2));
let s = parse_settings(&json!({"chatChannels": [{"id": ID, "answer": "everything"}]})).unwrap();
assert!(s.chat_channels[0].answer_all);
let cases = [ let cases = [
(json!({"call": {"similarity": 0.9}}), "call.similarity"), (json!({"call": {"similarity": 0.9}}), "call.similarity"),

View File

@@ -312,12 +312,13 @@ impl WorkerClient {
/// Ask the brain to answer `user`. With `speak` the answer is spoken under `id` (its audio and /// Ask the brain to answer `user`. With `speak` the answer is spoken under `id` (its audio and
/// `tts_done` come like a TTS request's); it ends as failed when it cannot be sent, the brain gives /// `tts_done` come like a TTS request's); it ends as failed when it cannot be sent, the brain gives
/// no answer, or the worker refuses it. /// no answer, or the worker refuses it.
pub fn reply(&self, id: &str, user: u64, text: &str, speak: bool) -> bool { /// `via`: `voice` or `chat` (the brain's history scope and where the answer is stored).
pub fn reply(&self, id: &str, user: u64, text: &str, via: &str, speak: bool) -> bool {
if speak { if speak {
self.inner.state.lock().unwrap().open_tts.insert(id.to_owned()); self.inner.state.lock().unwrap().open_tts.insert(id.to_owned());
} }
let sent = self.send_json(json!({"type": "reply", "id": id, "user": user.to_string(), "text": text, let sent = self.send_json(json!({"type": "reply", "id": id, "user": user.to_string(), "text": text,
"via": "voice", "speak": speak})); "via": via, "speak": speak}));
if !sent && speak { if !sent && speak {
self.end_failed(id); self.end_failed(id);
} }

View File

@@ -268,7 +268,7 @@ async fn chat_channels_go_out_and_chat_actions_come_back_with_their_results() {
c.start(); c.start();
c.wait_ready(Duration::from_secs(2)).await.expect("ready"); c.wait_ready(Duration::from_secs(2)).await.expect("ready");
c.chat_channels(Some(900), &[ChatChannelSettings { id: 5, name: "일정".into(), description: "약속".into(), c.chat_channels(Some(900), &[ChatChannelSettings { id: 5, name: "일정".into(), description: "약속".into(),
read: true, send: true, edit: true, react: true, delete: false, pin: false }]); read: true, send: true, edit: true, react: true, delete: false, pin: false, answer_all: false }]);
w.send(Message::text(json!({"type": "chat_action", "id": "a1", "action": "react", "channel": "5", w.send(Message::text(json!({"type": "chat_action", "id": "a1", "action": "react", "channel": "5",
"message_id": "77", "emoji": "👍"}).to_string())); "message_id": "77", "emoji": "👍"}).to_string()));
let ev = next_matching(&mut rx, |e| matches!(e, WorkerEvent::ChatAction(_))).await; let ev = next_matching(&mut rx, |e| matches!(e, WorkerEvent::ChatAction(_))).await;
@@ -293,8 +293,8 @@ async fn a_spoken_reply_takes_its_audio_and_a_failed_one_ends_its_speech() {
let (c, mut rx) = client(&w.url(), json!({})); let (c, mut rx) = client(&w.url(), json!({}));
c.start(); c.start();
c.wait_ready(Duration::from_secs(2)).await.expect("ready"); c.wait_ready(Duration::from_secs(2)).await.expect("ready");
assert!(c.reply("r1", 42, "자비스 날씨 어때", true)); assert!(c.reply("r1", 42, "자비스 날씨 어때", "voice", true));
assert!(c.reply("r2", 43, "자비스 안녕", true)); assert!(c.reply("r2", 43, "자비스 안녕", "voice", true));
until(|| w.got().len() >= 3).await; until(|| w.got().len() >= 3).await;
assert_eq!(w.got()[1], Got::Json(json!({"type": "reply", "id": "r1", "user": "42", "text": "자비스 날씨 어때", assert_eq!(w.got()[1], Got::Json(json!({"type": "reply", "id": "r1", "user": "42", "text": "자비스 날씨 어때",
"via": "voice", "speak": true}))); "via": "voice", "speak": true})));
@@ -317,14 +317,14 @@ async fn a_reply_the_worker_cannot_answer_or_send_ends_as_failed() {
let (c, mut rx) = client(&w.url(), json!({})); let (c, mut rx) = client(&w.url(), json!({}));
c.start(); c.start();
c.wait_ready(Duration::from_secs(2)).await.expect("ready"); c.wait_ready(Duration::from_secs(2)).await.expect("ready");
c.reply("r3", 42, "x", true); c.reply("r3", 42, "x", "voice", true);
w.send(Message::text(json!({"type": "error", "code": "brain_unavailable", "message": "no", "id": "r3"}).to_string())); w.send(Message::text(json!({"type": "error", "code": "brain_unavailable", "message": "no", "id": "r3"}).to_string()));
let e = next_matching(&mut rx, |e| matches!(e, WorkerEvent::TtsDone(_))).await; let e = next_matching(&mut rx, |e| matches!(e, WorkerEvent::TtsDone(_))).await;
assert_eq!(e, WorkerEvent::TtsDone(TtsDone { id: "r3".into(), chunks: 0, cancelled: false, failed: true })); assert_eq!(e, WorkerEvent::TtsDone(TtsDone { id: "r3".into(), chunks: 0, cancelled: false, failed: true }));
c.close(); c.close();
let (c, mut rx) = client("ws://127.0.0.1:1", json!({})); let (c, mut rx) = client("ws://127.0.0.1:1", json!({}));
assert!(!c.reply("r4", 42, "x", true)); assert!(!c.reply("r4", 42, "x", "voice", true));
let e = next_matching(&mut rx, |_| true).await; let e = next_matching(&mut rx, |_| true).await;
assert_eq!(e, WorkerEvent::TtsDone(TtsDone { id: "r4".into(), chunks: 0, cancelled: false, failed: true })); assert_eq!(e, WorkerEvent::TtsDone(TtsDone { id: "r4".into(), chunks: 0, cancelled: false, failed: true }));
} }
@@ -359,3 +359,20 @@ async fn addressed_and_rank_questions_go_out_and_their_answers_come_back() {
assert_eq!(e, WorkerEvent::Ranked { id: "k4".into(), order: vec![] }); assert_eq!(e, WorkerEvent::Ranked { id: "k4".into(), order: vec![] });
c.close(); c.close();
} }
#[tokio::test]
async fn a_chat_reply_is_not_spoken_and_its_text_comes_back() {
let w = FakeWorker::start(0).await;
let (c, mut rx) = client(&w.url(), json!({}));
c.start();
c.wait_ready(Duration::from_secs(2)).await.expect("ready");
assert!(c.reply("c1", 42, "자비스 일정", "chat", false));
until(|| w.got().len() >= 2).await;
assert_eq!(w.got()[1], Got::Json(json!({"type": "reply", "id": "c1", "user": "42", "text": "자비스 일정",
"via": "chat", "speak": false})));
w.send(tts_frame("c1", 0)); // not a speech the app asked for
w.send(Message::text(json!({"type": "reply_text", "id": "c1", "text": "", "failed": true}).to_string()));
let e = next_matching(&mut rx, |e| !matches!(e, WorkerEvent::Ready(_))).await;
assert_eq!(e, WorkerEvent::ReplyText { id: "c1".into(), text: String::new(), failed: true });
c.close();
}

View File

@@ -108,7 +108,7 @@ async fn audio_and_tts_round_trip_through_the_python_server() {
// A spoken brain answer plays under the adopted id, like a TTS request. // A spoken brain answer plays under the adopted id, like a TTS request.
let id = p.adopt(); let id = p.adopt();
assert!(c.reply(&id, 42, "자비스 안녕", true)); assert!(c.reply(&id, 42, "자비스 안녕", "voice", true));
let end = tokio::time::timeout(Duration::from_secs(5), async { let end = tokio::time::timeout(Duration::from_secs(5), async {
loop { loop {
match rx.recv().await.unwrap() { match rx.recv().await.unwrap() {

File diff suppressed because one or more lines are too long