Soul-driven AI agent with permission-hardened tools, token budgets, and multi-channel access. Runs 24/7 from CLI, Telegram or More.
import type { ChannelMessage } from '../types/channel.js';
import type { OwnerApproval } from './owner-approvals.js';
/**
* How an owner approval reads on a messaging channel, and how a reply to it
* is understood. A reply works only from the owner (an admin of that
* channel), only with the code, and only once.
*/
const REPLY = /^\s*(approve|allow|yes|ok|deny|reject|no)\s+([A-Za-z2-9]{5})\s*[.!]?\s*$/i;
export function parseApprovalReply(text: string): { decision: 'allow' | 'deny'; code: string } | null {
const m = REPLY.exec(text);
if (!m) return null;
const word = m[1].toLowerCase();
return { decision: ['approve', 'allow', 'yes', 'ok'].includes(word) ? 'allow' : 'deny', code: m[2].toUpperCase() };
}
/** The sender is the owner on their channel: an admin (Telegram, Signal, WhatsApp roles; Discord/Slack admin flag). */
export function isOwnerSender(msg: ChannelMessage): boolean {
if (msg.senderRole === 'admin') return true;
return (msg.metadata as { isAdmin?: unknown } | undefined)?.isAdmin === true;
}
const minutes = (ms: number): string => `${Math.max(1, Math.round(ms / 60_000))} min`;
export function approvalRequestText(a: OwnerApproval, now = Date.now()): string {
const head = a.kind === 'pair'
? `π Mercury: a new device wants to connect: β${a.who}β${a.detail ? ` (${a.detail})` : ''}.`
: `π Mercury: β${a.who}β wants to: ${a.title}${a.detail ? `\n ${a.detail}` : ''}${a.ip ? `\n (from ${a.ip})` : ''}`;
return `${head}\nReply βapprove ${a.code}β to allow it, or βdeny ${a.code}β. It expires in ${minutes(a.expiresAt - now)}.${a.kind === 'pair' ? ' If you didnβt just sign in somewhere, deny it.' : ''}`;
}
export function approvalAnsweredText(a: OwnerApproval): string {
const what = a.kind === 'pair' ? `the device β${a.who}β` : `β${a.title}β for ${a.who}`;
return a.status === 'allowed' ? `β Approved ${what}.` : `β Denied ${what}.`;
}
export function newLocationText(name: string, ip: string): string {
return `π Mercury: your device β${name}β connected from a new address (${ip}). If that wasnβt you, revoke it with /server on the computer Mercury runs on.`;
}