Skip to content

Soul-driven AI agent with permission-hardened tools, token budgets, and multi-channel access. Runs 24/7 from CLI, Telegram or More.

feat(whatsapp): approve actions during a call, by voice or in the chat

Salman Qureshi10d9cb3f0d02
diff --git a/plugins/whatsapp/companion/src/main.rs b/plugins/whatsapp/companion/src/main.rs
index fee44dad..f863b950 100644
--- a/plugins/whatsapp/companion/src/main.rs
+++ b/plugins/whatsapp/companion/src/main.rs
@@ -94,6 +94,8 @@ pub struct State {
pub call_activity: std::sync::Mutex<HashMap<String, String>>,
/// Things that happened in the chat during a call (a photo arrived), to mention at the next pause.
pub call_context: std::sync::Mutex<HashMap<String, Vec<String>>>,
+ /// Approvals Mercury needs on a call: (approval id, what it wants to do), per call.
+ pub call_approvals: std::sync::Mutex<HashMap<String, Vec<(String, String)>>>,
}
pub fn log(msg: impl AsRef<str>) {
@@ -141,6 +143,7 @@ async fn main() -> anyhow::Result<()> {
call_watchers: Arc::new(Mutex::new(HashMap::new())),
call_activity: std::sync::Mutex::new(HashMap::new()),
call_context: std::sync::Mutex::new(HashMap::new()),
+ call_approvals: std::sync::Mutex::new(HashMap::new()),
});
let mut lines = BufReader::new(tokio::io::stdin()).lines();
@@ -178,6 +181,34 @@ async fn handle(st: Arc<State>, msg: Value) {
}
return;
}
+ if method == "mercury/call/approval" {
+ if let (Some(call), Some(id), Some(prompt)) = (
+ params.get("callId").and_then(Value::as_str),
+ params.get("approvalId").and_then(Value::as_str),
+ params.get("prompt").and_then(Value::as_str),
+ ) {
+ st.call_approvals.lock().unwrap().entry(call.to_string()).or_default().push((id.to_string(), prompt.chars().take(600).collect()));
+ }
+ return;
+ }
+ if method == "mercury/call/approval-resolved" {
+ if let (Some(call), Some(id)) = (params.get("callId").and_then(Value::as_str), params.get("approvalId").and_then(Value::as_str)) {
+ if let Some(list) = st.call_approvals.lock().unwrap().get_mut(call) {
+ list.retain(|(a, _)| a != id);
+ }
+ let via = params.get("via").and_then(Value::as_str).unwrap_or("");
+ if via != "voice" {
+ let approved = params.get("decision").and_then(Value::as_str) != Some("no");
+ let note = if via == "chat" {
+ format!("RESOLVED:{id}:The owner {} that in the WhatsApp chat.", if approved { "approved" } else { "declined" })
+ } else {
+ format!("RESOLVED:{id}:Nobody answered that approval in time, so it wasn't done. Offer to try again.")
+ };
+ st.call_context.lock().unwrap().entry(call.to_string()).or_default().push(note);
+ }
+ }
+ return;
+ }
if method == "mercury/call/context" {
if let (Some(call), Some(text)) = (params.get("callId").and_then(Value::as_str), params.get("text").and_then(Value::as_str)) {
st.call_context.lock().unwrap().entry(call.to_string()).or_default().push(text.chars().take(400).collect());
diff --git a/plugins/whatsapp/companion/src/voice.rs b/plugins/whatsapp/companion/src/voice.rs
index 433f5526..aac64d45 100644
--- a/plugins/whatsapp/companion/src/voice.rs
+++ b/plugins/whatsapp/companion/src/voice.rs
@@ -29,7 +29,9 @@ const WA_RATE: u32 = 16_000;
const AI_RATE: u32 = 24_000;
const FRAME: usize = 960;
const RING_TIMEOUT: Duration = Duration::from_secs(45);
-const ASK_TIMEOUT: Duration = Duration::from_secs(100);
+// Long enough for a turn that waits on the owner's approval; Mercury itself replies
+// "still working" well before this when a turn is merely slow.
+const ASK_TIMEOUT: Duration = Duration::from_secs(300);
/// Outcomes of outgoing calls, keyed by call id, fed from the client event bus.
pub type CallWatchers = Arc<Mutex<HashMap<String, mpsc::UnboundedSender<&'static str>>>>;
@@ -205,6 +207,8 @@ fn session_config(instructions: &str) -> Value {
(for example: 'Let me look that up, it'll take a few seconds, stay with me'). Then speak its answer in your own words. \
If you get a status update while Mercury is still working, give the user one short, natural sentence about it so they know you're still there. \
The user can send photos and documents in the WhatsApp chat during the call: Mercury can see them, so for 'what's in this photo?' call ask_mercury. \
+ Some actions need the user's approval. When you get an approval request, ask the user plainly whether to go ahead, wait for their answer, \
+ then call answer_approval. Never answer an approval yourself: only the user's own words count. \
Only actions that change something need the user's approval, which can't be given on a call; if ask_mercury says so, offer to do it by message. When the user says goodbye or is done, say goodbye and call end_call.";
json!({
"type": "session.update",
@@ -227,6 +231,12 @@ fn session_config(instructions: &str) -> Value {
"parameters": { "type": "object", "properties": { "question": { "type": "string" } }, "required": ["question"] },
},
{ "type": "function", "name": "end_call", "description": "Hang up after saying goodbye.", "parameters": { "type": "object", "properties": {} } },
+ {
+ "type": "function",
+ "name": "answer_approval",
+ "description": "Report the user's answer to the approval you just asked them about. Only call it after they answered out loud.",
+ "parameters": { "type": "object", "properties": { "approved": { "type": "boolean", "description": "true only if the user clearly said yes" } }, "required": ["approved"] },
+ },
],
},
})
@@ -340,6 +350,11 @@ async fn bridge(
// Answers from Mercury, spoken as soon as the voice is free (only one response may be active).
let (answer_tx, mut answer_rx) = mpsc::unbounded_channel::<(String, String)>();
let mut ready_answers: VecDeque<(String, String)> = VecDeque::new();
+ // The approval being asked out loud right now, and what the user has said since (transcripts).
+ let mut awaiting: Option<(String, Instant)> = None;
+ let mut user_said: Vec<(Instant, String)> = Vec::new();
+ // A spoken "yes" waiting for its transcript: (function call id, approval id, asked at, deadline).
+ let mut verifying: Option<(String, String, Instant, Instant)> = None;
let mut status_tick = tokio::time::interval(Duration::from_secs(1));
status_tick.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);
@@ -359,14 +374,39 @@ async fn bridge(
if !responding { deliver(&send_q, &mut ready_answers, &mut responding); }
}
_ = status_tick.tick() => {
+ if let Some((fc, _id, _asked, deadline)) = verifying.clone() {
+ if Instant::now() >= deadline {
+ verifying = None;
+ ready_answers.push_back((fc, "Not approved: the user hasn't clearly said yes in their own words. Ask them again, or they can reply 1 in the WhatsApp chat.".to_string()));
+ }
+ }
if !responding && !ready_answers.is_empty() {
deliver(&send_q, &mut ready_answers, &mut responding);
continue;
}
if responding || !playout.lock().unwrap().is_empty() { continue; }
- // Something arrived in the chat (a photo, a document): let the voice mention it.
+ // Mercury needs the user's go-ahead: ask out loud.
+ if awaiting.is_none() {
+ let next = st.call_approvals.lock().unwrap().get_mut(call_id).and_then(|l| if l.is_empty() { None } else { Some(l.remove(0)) });
+ if let Some((id, prompt)) = next {
+ awaiting = Some((id, Instant::now()));
+ let text = format!("[Approval needed, not said by the user] Mercury wants to: {prompt}. Ask the user plainly, in one or two sentences, whether to go ahead. Then wait for their answer and call answer_approval. (They can also reply 1 in the WhatsApp chat.)");
+ let _ = send_q.send(json!({ "type": "conversation.item.create", "item": { "type": "message", "role": "user", "content": [{ "type": "input_text", "text": text }] } }));
+ let _ = send_q.send(json!({ "type": "response.create" }));
+ responding = true;
+ continue;
+ }
+ }
+ // Something arrived in the chat (a photo, a document, an approval given there): let the voice mention it.
let context = st.call_context.lock().unwrap().remove(call_id);
- if let Some(notes) = context.filter(|n| !n.is_empty()) {
+ if let Some(mut notes) = context.filter(|n| !n.is_empty()) {
+ for n in notes.iter_mut() {
+ if let Some(rest) = n.strip_prefix("RESOLVED:") {
+ let (id, text) = rest.split_once(':').unwrap_or(("", rest));
+ if awaiting.as_ref().map(|(a, _)| a == id).unwrap_or(false) { awaiting = None; }
+ *n = text.to_string();
+ }
+ }
let text = format!("[Status update, not said by the user] {} Acknowledge it in one short sentence (for example 'Got your photo, want me to take a look?'), unless they're mid-sentence.", notes.join(" "));
let _ = send_q.send(json!({ "type": "conversation.item.create", "item": { "type": "message", "role": "user", "content": [{ "type": "input_text", "text": text }] } }));
let _ = send_q.send(json!({ "type": "response.create" }));
@@ -425,6 +465,17 @@ async fn bridge(
"conversation.item.input_audio_transcription.completed" => {
if let Some(t) = ev.get("transcript").and_then(Value::as_str) {
transcript.lock().await.push(json!({ "role": "owner", "text": t.trim() }));
+ user_said.push((Instant::now(), t.trim().to_string()));
+ if user_said.len() > 20 { user_said.remove(0); }
+ if let Some((fc, id, asked_at, _)) = verifying.clone() {
+ if let Some(v) = spoken_verdict(&user_said, asked_at) {
+ verifying = None;
+ awaiting = None;
+ let out = finalize_approval(st, call_id, &id, v).await;
+ ready_answers.push_back((fc, out));
+ if !responding { deliver(&send_q, &mut ready_answers, &mut responding); }
+ }
+ }
}
}
"response.output_audio_transcript.done" | "response.audio_transcript.done" => {
@@ -445,6 +496,35 @@ async fn bridge(
hang_up = true;
break Ok(());
}
+ if name == "answer_approval" {
+ let approved = args.get("approved").and_then(Value::as_bool).unwrap_or(false);
+ match awaiting.clone() {
+ None => {
+ ready_answers.push_back((fc_id, "No approval is waiting right now.".to_string()));
+ }
+ Some((id, asked_at)) => {
+ // The user's own words decide, not the model. Transcription can lag the
+ // model a little, so an undecided "yes" waits up to 3s for the transcript.
+ match (approved, spoken_verdict(&user_said, asked_at)) {
+ (false, _) | (true, Some(false)) => {
+ awaiting = None;
+ let out = finalize_approval(st, call_id, &id, false).await;
+ ready_answers.push_back((fc_id, out));
+ }
+ (true, Some(true)) => {
+ awaiting = None;
+ let out = finalize_approval(st, call_id, &id, true).await;
+ ready_answers.push_back((fc_id, out));
+ }
+ (true, None) => {
+ verifying = Some((fc_id, id, asked_at, Instant::now() + Duration::from_secs(3)));
+ }
+ }
+ }
+ }
+ if !responding { deliver(&send_q, &mut ready_answers, &mut responding); }
+ continue;
+ }
if name == "ask_mercury" {
let q = args.get("question").and_then(Value::as_str).unwrap_or("").to_string();
transcript.lock().await.push(json!({ "role": "ask_mercury", "text": q }));
@@ -483,6 +563,30 @@ async fn bridge(
result
}
+async fn finalize_approval(st: &Arc<State>, call_id: &str, approval_id: &str, yes: bool) -> String {
+ match st.out.request("mercury/call/approve", json!({ "callId": call_id, "approvalId": approval_id, "approved": yes }), Duration::from_secs(10)).await {
+ Ok(v) if v.get("ok").and_then(Value::as_bool) == Some(true) => {
+ if yes { "Approved. Mercury is carrying on; tell the user briefly.".into() } else { "Declined. Tell the user it won't be done.".into() }
+ }
+ Ok(v) => format!("That approval was already settled ({}).", v.get("reason").and_then(Value::as_str).unwrap_or("closed")),
+ Err(e) => format!("Couldn't reach Mercury: {e}"),
+ }
+}
+
+/// Did the user say yes (or no) out loud since the approval question?
+/// Only their transcribed speech counts; the model's own reading doesn't.
+pub fn spoken_verdict(said: &[(Instant, String)], since: Instant) -> Option<bool> {
+ for (at, text) in said.iter().rev() {
+ if *at < since { break; }
+ let t = format!(" {} ", text.to_lowercase().replace(|c: char| !c.is_alphanumeric() && !c.is_whitespace() && (c as u32) < 128, " "));
+ const NO: &[&str] = &[" no ", " nope ", " don't ", " dont ", " do not ", " cancel ", " stop ", " wait ", " nahi ", " nahin ", " mat ", "नहीं", "मत", "نہیں"];
+ const YES: &[&str] = &[" yes ", " yeah ", " yep ", " yup ", " sure ", " ok ", " okay ", " go ahead ", " do it ", " approve", " confirm", " please do ", " haan ", " han ", " ha ji ", " ji ", " theek ", " thik ", " kar do ", " karo ", "हाँ", "हां", "ठीक", "कर दो", "ہاں", "جی", "ٹھیک"];
+ if NO.iter().any(|w| t.contains(w)) { return Some(false); }
+ if YES.iter().any(|w| t.contains(w)) { return Some(true); }
+ }
+ None
+}
+
/// Hand Mercury's answer to the voice model and have it speak.
fn deliver(send_q: &mpsc::UnboundedSender<Value>, ready: &mut VecDeque<(String, String)>, responding: &mut bool) {
if let Some((fc_id, answer)) = ready.pop_front() {
@@ -520,6 +624,18 @@ mod tests {
assert_eq!(resample(&[0i16; 1440], AI_RATE, WA_RATE).len(), 960);
}
+ #[test]
+ fn only_the_users_own_yes_counts() {
+ let asked = Instant::now();
+ let after = asked + Duration::from_millis(10);
+ assert_eq!(spoken_verdict(&[(after, "Yes, go ahead".into())], asked), Some(true));
+ assert_eq!(spoken_verdict(&[(after, "haan kar do".into())], asked), Some(true));
+ assert_eq!(spoken_verdict(&[(after, "No, don't do that".into())], asked), Some(false));
+ assert_eq!(spoken_verdict(&[(after, "what did you say?".into())], asked), None);
+ // Words from before the question don't count.
+ assert_eq!(spoken_verdict(&[(asked - Duration::from_secs(5), "yes".into())], asked), None);
+ }
+
#[test]
fn close_reasons_are_explained() {
assert!(explain_close("insufficient_quota.credit_balance_exhausted").contains("out of credit"));
diff --git a/src/plugins/channel-bridge.ts b/src/plugins/channel-bridge.ts
index 2ce15b91..3ccad0fc 100644
--- a/src/plugins/channel-bridge.ts
+++ b/src/plugins/channel-bridge.ts
@@ -27,9 +27,14 @@
* Mercury → plugin during a call
* mercury/call/progress notification { callId, activity } (what an ask_mercury turn is doing now)
* mercury/call/context notification { callId, text } (something happened in the chat, e.g. a photo arrived)
+ * mercury/call/approval notification { callId, approvalId, prompt } (ask the owner by voice)
+ * mercury/call/approval-resolved notification { callId, approvalId, decision, via }
+ * plugin → Mercury
+ * mercury/call/approve request { callId, approvalId, approved } (after checking the owner said yes)
*
- * During a call nobody can approve anything: approvals asked on a call are
- * refused (fail-closed), and the model is told to offer doing it by message.
+ * Approvals during a call are asked by voice and in the chat at once; the
+ * first answer wins. The plugin accepts a spoken yes only when the owner's own
+ * transcribed words say yes, and a spoken yes never becomes "always".
*/
import { basename } from 'node:path';
import { existsSync, readFileSync, statSync } from 'node:fs';
@@ -156,6 +161,14 @@ export class PluginChannel extends BaseChannel {
readonly type: ChannelType;
private status: ChannelStatus = { state: 'starting', at: Date.now() };
private pending = new Map<string, { resolve: (v: string) => void; timer: ReturnType<typeof setTimeout> }>();
+ /**
+ * Approvals asked on a call. Answerable by voice (the plugin checks the
+ * owner actually said yes) or by replying in the chat; first answer wins.
+ */
+ private callApprovals = new Map<string, { callId: string; prompt: string; resolve: (v: string) => void; timer: ReturnType<typeof setTimeout> }>();
+ private approvalSeq = 0;
+ /** The owner's chat as WhatsApp addresses it (often a LID, not their phone JID). */
+ private ownerChatId: string | undefined;
private seen = new Set<string>();
private statusListeners = new Set<(s: ChannelStatus) => void>();
/** In-flight `ask_mercury` turns, by call target (`call:<callId>`). */
@@ -255,6 +268,12 @@ export class PluginChannel extends BaseChannel {
case 'mercury/call/briefing':
this.callStarted(String(p.callId ?? ''));
return { instructions: voiceBriefing() };
+ case 'mercury/call/approve': {
+ // The plugin only sends this after checking the owner's own words on the call.
+ const approvalId = String(p.approvalId ?? '');
+ const ok = this.resolveCallApproval(approvalId, p.approved === true ? 'yes' : 'no', 'voice');
+ return { ok, reason: ok ? undefined : 'that approval is no longer open (answered in the chat, or timed out)' };
+ }
case 'mercury/call/ask': {
const callId = typeof p.callId === 'string' ? p.callId.replace(/[^\w.-]/g, '').slice(0, 80) : '';
const question = typeof p.question === 'string' ? p.question.trim().slice(0, 4000) : '';
@@ -277,11 +296,18 @@ export class PluginChannel extends BaseChannel {
clearTimeout(previous.timer);
previous.resolve('(superseded by a newer question)');
}
- const timer = setTimeout(() => {
+ const giveUp = () => {
+ // Waiting on the owner's approval isn't "stuck": keep the question open.
+ if ([...this.callApprovals.values()].some((a) => a.callId === callId)) {
+ const entry = this.callAsks.get(target);
+ if (entry) entry.timer = setTimeout(giveUp, 30_000);
+ return;
+ }
this.callAsks.delete(target);
this.overflow.add(target);
resolve('Mercury is still working on that. It will send the answer to WhatsApp when it\'s done.');
- }, 90_000);
+ };
+ const timer = setTimeout(giveUp, 90_000);
this.callAsks.set(target, { chunks: [], resolve, timer });
const files = this.activeCalls.get(callId)?.files ?? [];
const sentNote = files.length
@@ -293,7 +319,7 @@ export class PluginChannel extends BaseChannel {
channelType: this.type,
senderId: digits(owner),
senderRole: 'admin',
- content: `[Voice call: the owner asked this out loud. Use your tools to get the real answer: web_search for news and anything current, fetch_url, memory, skills. Then answer in one to three short spoken sentences, with no markdown, lists, links or code. Only actions that change something (files, commands, sending messages, purchases) need approval, and that can't be given on a call; for those, say you'll do it once they confirm by message.]${sentNote}\n${question}`,
+ content: `[Voice call: the owner asked this out loud. Use your tools to get the real answer: web_search for news and anything current, fetch_url, memory, skills. Then answer in one to three short spoken sentences, with no markdown, lists, links or code. Go ahead and do what they ask, including saving files or running things: if an action needs approval, Mercury asks the owner on the call and in the chat, and continues once they say yes. Don't ask them to confirm by message yourself.]${sentNote}\n${question}`,
timestamp: Date.now(),
metadata: { pluginId: this.manifest.id, voiceCall: true, callId, attachments: files.slice(-4), channelAccess: this.accessFor('admin'), channelGuidance: this.guidance('admin', false, true) },
});
@@ -316,6 +342,7 @@ export class PluginChannel extends BaseChannel {
private onCallEnded(params: unknown): void {
const p = (params ?? {}) as Record<string, unknown>;
this.activeCalls.delete(String(p.callId ?? ''));
+ for (const [id, a] of this.callApprovals) if (a.callId === String(p.callId ?? '')) this.resolveCallApproval(id, 'no', 'timeout');
const secs = typeof p.durationSec === 'number' ? Math.round(p.durationSec) : 0;
const dur = secs >= 60 ? `${Math.floor(secs / 60)}m ${secs % 60}s` : `${secs}s`;
const outcome = String(p.outcome ?? '');
@@ -397,6 +424,16 @@ export class PluginChannel extends BaseChannel {
}
const text = typeof p.text === 'string' ? p.text.slice(0, 20_000) : '';
+ if (role === 'admin' && p.isGroup !== true) this.ownerChatId = p.chatId;
+ if (role === 'admin' && text && this.callApprovals.size) {
+ const answer = normalizeReply(text);
+ if (answer) {
+ // The oldest open approval from a call; the chat reply answers it.
+ const [approvalId] = this.callApprovals.keys();
+ this.resolveCallApproval(approvalId, answer, 'chat');
+ return;
+ }
+ }
const pending = this.pending.get(p.chatId);
if (pending && text) {
const answer = normalizeReply(text);
@@ -518,7 +555,7 @@ export class PluginChannel extends BaseChannel {
`## Channel: ${name}`,
`You're talking with ${who} on ${where}.`,
voiceCall
- ? 'Your answer is spoken aloud: one to three short sentences, no markdown, lists, links or code. Looking things up (web_search, fetch_url, memory, skills) is fine during a call.'
+ ? 'Your answer is spoken aloud: one to three short sentences, no markdown, lists, links or code. Use your tools as usual during a call; approvals are asked on the call and answered by voice or in the chat.'
: `Write like a text message: short and conversational. ${name} formatting only (*bold*, _italic_, ~strike~). No code blocks, tables, headings or long lists. If they need code, a document or long output, write it to a file and send it with send_file, then summarise it in a sentence. Never paste raw file contents, logs or stack traces.`,
];
if (this.accessFor(role) === 'extended') {
@@ -631,11 +668,41 @@ export class PluginChannel extends BaseChannel {
/** Approval prompt answered by text: 1/yes, 2/no, 3/always. No reply in 2 minutes is no. */
async askPermission(prompt: string, targetId?: string): Promise<string> {
- // Nobody can approve mid-call: refuse (fail-closed). The model offers to do it by message.
- if (this.callTarget(targetId)) return 'no';
+ const call = this.callTarget(targetId);
+ if (call) return this.askOnCall(call.slice('call:'.length), prompt);
return this.ask(`🔐 ${prompt}\n\nReply *1* (yes), *2* (no) or *3* (always for this chat).`, targetId);
}
+ /**
+ * An approval needed by a turn running for a call: asked by voice and in
+ * the chat at once. A spoken yes approves this once (never "always").
+ */
+ private askOnCall(callId: string, prompt: string): Promise<string> {
+ const approvalId = `ap${++this.approvalSeq}`;
+ return new Promise((resolve) => {
+ const timer = setTimeout(() => this.resolveCallApproval(approvalId, 'no', 'timeout'), APPROVAL_TIMEOUT_MS);
+ this.callApprovals.set(approvalId, { callId, prompt, resolve, timer });
+ this.transport.notify?.('mercury/call/approval', { callId, approvalId, prompt: prompt.slice(0, 600) });
+ const chat = this.ownerChatId ? `${this.type}:${this.ownerChatId}` : undefined;
+ void this.send(`🔐 From your call: ${prompt}\n\nSay *yes* on the call, or reply *1* (yes) or *2* (no) here.`, chat).catch(() => {});
+ });
+ }
+
+ private resolveCallApproval(approvalId: string, answer: string, via: 'voice' | 'chat' | 'timeout'): boolean {
+ const a = this.callApprovals.get(approvalId);
+ if (!a) return false;
+ clearTimeout(a.timer);
+ this.callApprovals.delete(approvalId);
+ // A spoken answer never widens to "always".
+ const decision = via === 'voice' && answer === 'always' ? 'yes' : answer;
+ a.resolve(decision);
+ this.transport.notify?.('mercury/call/approval-resolved', { callId: a.callId, approvalId, decision, via });
+ if (via === 'chat') {
+ void this.send(decision === 'no' ? '✖ Declined. I told the call.' : '✅ Approved. Carrying on in the call.', this.ownerChatId ? `${this.type}:${this.ownerChatId}` : undefined).catch(() => {});
+ }
+ return true;
+ }
+
async askToContinue(question: string, targetId?: string): Promise<boolean> {
if (this.callTarget(targetId)) return false;
return (await this.ask(`${question}\n\nReply *yes* to continue or *no* to stop.`, targetId)) !== 'no';
diff --git a/src/plugins/channel.test.ts b/src/plugins/channel.test.ts
index e211f953..4fb223bd 100644
--- a/src/plugins/channel.test.ts
+++ b/src/plugins/channel.test.ts
@@ -61,6 +61,9 @@ rl.on('line', (line) => {
return log('progress.jsonl', msg.params);
case 'mercury/call/context':
return log('context.jsonl', msg.params);
+ case 'mercury/call/approval':
+ case 'mercury/call/approval-resolved':
+ return log('approvals.jsonl', { method: msg.method, ...msg.params });
case 'mercury/channel/unlink':
ok({});
return notify('mercury/channel/status', { state: 'logged-out', detail: 'unlinked by owner' });
@@ -225,7 +228,7 @@ describe('plugin channels (plugin API 1.1)', () => {
describe('voice calls', () => {
const replies = () => {
const f = join(pluginDataDir('whatsapp'), 'replies.jsonl');
- return existsSync(f) ? readFileSync(f, 'utf8').trim().split('\n').map((l) => JSON.parse(l)) : [];
+ return existsSync(f) ? readFileSync(f, 'utf8').split('\n').filter((l) => l.trim().endsWith('}')).map((l) => JSON.parse(l)) : [];
};
const sent = () => {
const f = join(pluginDataDir('whatsapp'), 'out.jsonl');
@@ -256,7 +259,6 @@ describe('plugin channels (plugin API 1.1)', () => {
expect(msg.senderRole).toBe('admin');
expect(msg.content).toMatch(/Voice call[\s\S]*What is the weather\?/);
// The agent replies on the call target, then finishes the turn.
- expect(await ch.askPermission('Run git push?', msg.channelId)).toBe('no');
await ch.send('☿ Working on it…', msg.channelId);
await ch.send('It is sunny and 24 degrees.', msg.channelId);
ch.endTask(msg.channelId);
@@ -289,6 +291,51 @@ describe('plugin channels (plugin API 1.1)', () => {
expect(received[1].channelId).toBe('whatsapp:[email protected]');
});
+ describe('approvals during a call', () => {
+ const approvals = () => {
+ const f = join(pluginDataDir('whatsapp'), 'approvals.jsonl');
+ return existsSync(f) ? readFileSync(f, 'utf8').split('\n').filter((l) => l.trim().endsWith('}')).map((l) => JSON.parse(l)) : [];
+ };
+
+ it('are asked by voice and in the chat; a chat reply answers that exact approval', async () => {
+ const ch = await installChannel();
+ await inject({ id: 'o1', senderPhone: '15550002', chatId: '229012387459114@lid', text: 'hi' }); // the owner's chat, as WhatsApp addresses it
+ const answer = ch.askPermission('Write ~/Desktop/research.md', 'whatsapp:call:c3');
+ await waitFor(() => approvals().length === 1 && sent().some((m) => /From your call/.test(m.text ?? '')));
+ expect(approvals()[0]).toMatchObject({ method: 'mercury/call/approval', callId: 'c3', prompt: 'Write ~/Desktop/research.md' });
+ const prompt = sent().find((m) => /From your call/.test(m.text ?? ''));
+ expect(prompt).toMatchObject({ chatId: '229012387459114@lid' });
+ await inject({ id: 'o2', senderPhone: '15550002', chatId: '229012387459114@lid', text: '1' });
+ expect(await answer).toBe('yes');
+ expect(received.map((m) => m.content)).toEqual(['hi']); // "1" answered the approval, not a new turn
+ await waitFor(() => approvals().some((a) => a.method === 'mercury/call/approval-resolved'));
+ expect(approvals()[1]).toMatchObject({ approvalId: approvals()[0].approvalId, decision: 'yes', via: 'chat' });
+ });
+
+ it('a spoken answer approves once and never becomes "always"', async () => {
+ const ch = await installChannel();
+ const answer = ch.askPermission('Run the build', 'whatsapp:call:c4');
+ await waitFor(() => approvals().length === 1);
+ const { approvalId } = approvals()[0];
+ await callsim('mercury/call/approve', { callId: 'c4', approvalId, approved: true });
+ expect(await answer).toBe('yes');
+ await waitFor(() => replies().length === 1);
+ expect(replies()[0].result).toEqual({ ok: true });
+ // Answering again (e.g. in the chat afterwards) finds nothing open.
+ await callsim('mercury/call/approve', { callId: 'c4', approvalId, approved: true });
+ await waitFor(() => replies().length === 2);
+ expect(replies()[1].result.ok).toBe(false);
+ });
+
+ it('hanging up declines anything still waiting', async () => {
+ const ch = await installChannel();
+ const answer = ch.askPermission('Delete old logs', 'whatsapp:call:c5');
+ await waitFor(() => approvals().length === 1);
+ await inject({ callId: 'c5', direction: 'incoming', outcome: 'completed', durationSec: 10, transcript: [] }, 'callend');
+ expect(await answer).toBe('no');
+ });
+ });
+
it('tells the plugin what Mercury is doing during a call, in words a voice can say', async () => {
const ch = await installChannel();
ch.toolStarted('whatsapp:call:c7', 'web_search');