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.

Merge branch 'main' into feat/social-media

LoWandererbdf316633254
diff --cc docs/docs.html
index 41db3448,7c493981..fa26d9a7
--- a/docs/docs.html
+++ b/docs/docs.html
@@@ -336,12 -337,9 +337,14 @@@
<ul>
<li><strong>Your name</strong> — used by the agent to address you</li>
<li><strong>Agent name</strong> — defaults to "Mercury", customize it</li>
+ <li><strong>LLM providers</strong> — choose one or more: DeepSeek, OpenAI, Anthropic, Grok, Ollama Cloud, or Ollama Local</li>
+ <li><strong>Model selection</strong> — after each valid key, Mercury fetches recommended chat models and lets you choose a default</li>
+ <li><strong>Telegram bot token</strong> — optional, for remote access in private 1:1 chats</li>
+ <li><strong>LLM API keys</strong> — at least one: DeepSeek, OpenAI, Anthropic, Grok, or Ollama</li>
+ <li><strong>Telegram bot token</strong> — optional, for remote access</li>
</ul>
+ <p>Provider keys are validated by format and then by fetching available models. If model discovery fails, Mercury asks you to re-enter the key instead of accepting random text.</p>
+ <p>If you enable Telegram during first-time setup, Mercury starts a temporary pairing flow: send <code>/start</code> to your bot in Telegram, copy the pairing code you receive there, then paste that code into the CLI to finish setup.</p>
<p>All config is stored in <code>~/.mercury/</code> — nothing in your project directory.</p>
<div class="callout">
@@@ -897,9 -850,102 +900,104 @@@
any other credential: don't commit it, don't share it, and rotate the token if you suspect it's compromised.</p>
</div>
+ <h2 id="telegram">Telegram</h2>
+ <p>Mercury connects to Telegram via a bot token, providing a mobile-first interface with streaming, file uploads,
+ and inline approval keyboards.</p>
+
+ <h3>Pairing</h3>
+ <p>Mercury uses a <strong>single-owner pairing model</strong>. Only one Telegram account can be paired to a Mercury
+ instance at a time. This ensures your agent only responds to you.</p>
+
+ <div class="callout" style="border-left-color: var(--cyan); background: rgba(0, 212, 255, 0.06);">
+ <div class="callout-title">Initial message only</div>
+ <p>When the bot is <strong>not yet paired</strong>, only the <code>/start</code> or <code>/pair</code> command
+ is processed. All other messages from unpaired users are ignored with a prompt to pair first. This prevents
+ unauthorized access — nobody can send commands to your Mercury instance without pairing.</p>
+ </div>
+
+ <p><strong>To pair:</strong> Send <code>/start</code> or <code>/pair</code> to your Mercury bot in a private chat.
+ The bot records your Telegram user ID and chat ID, and from that point forward, only your messages are processed.</p>
+
+ <p><strong>To unpair:</strong> Send <code>/unpair</code> to remove the pairing. The bot will then require
+ <code>/start</code> again to re-pair.</p>
+
+ <div class="callout callout-warn">
+ <div class="callout-title">Private chats only</div>
+ <p>Mercury only works in private (one-to-one) chats. Messages in groups or channels are ignored.</p>
+ </div>
+
+ <h3>Features</h3>
+ <table class="cmd-table">
+ <thead>
+ <tr>
+ <th>Feature</th>
+ <th>Description</th>
+ </tr>
+ </thead>
+ <tbody>
+ <tr>
+ <td>Text streaming</td>
+ <td>Real-time response editing with typing indicators (toggle with <code>/stream</code>)</td>
+ </tr>
+ <tr>
+ <td>File uploads</td>
+ <td>Photos, audio, video, and documents auto-detected and sent with the right API</td>
+ </tr>
+ <tr>
+ <td>HTML formatting</td>
+ <td>Markdown automatically converted to Telegram-compatible HTML</td>
+ </tr>
+ <tr>
+ <td>Inline keyboards</td>
+ <td>Shell command and scope approvals shown as Allow / Always / Deny buttons</td>
+ </tr>
+ <tr>
+ <td>Paired messaging</td>
+ <td>The <code>send_message</code> tool lets Mercury proactively send you messages</td>
+ </tr>
+ <tr>
+ <td>Bot commands menu</td>
+ <td>12 commands registered in Telegram's bot menu for quick access</td>
+ </tr>
+ </tbody>
+ </table>
+
+ <h3>Configuration</h3>
+ <p>Telegram is set up during <code>mercury doctor</code> or initial onboarding. You need a bot token from
+ <a href="https://t.me/BotFather" target="_blank" rel="noopener">@BotFather</a>.</p>
+
+ <table class="cmd-table">
+ <thead>
+ <tr>
+ <th>Variable</th>
+ <th>Description</th>
+ <th>Default</th>
+ </tr>
+ </thead>
+ <tbody>
+ <tr>
+ <td>TELEGRAM_BOT_TOKEN</td>
+ <td>Bot token from @BotFather</td>
+ <td>—</td>
+ </tr>
+ <tr>
+ <td>TELEGRAM_ENABLED</td>
+ <td>Enable/disable Telegram channel</td>
+ <td>false</td>
+ </tr>
+ <tr>
+ <td>TELEGRAM_STREAMING</td>
+ <td>Enable real-time text streaming</td>
+ <td>true</td>
+ </tr>
+ </tbody>
+ </table>
+
+ <p>Type <code>none</code> as the bot token during <code>mercury doctor</code> to disable Telegram.</p>
+
<h2 id="tools">Built-in Tools</h2>
+ <p>Mercury has a built-in toolset it can use during conversations. These are not CLI commands — the agent decides
+ when to call them.</p>
<h3>Filesystem</h3>
<table class="cmd-table">
diff --cc docs/index.html
index c4acb287,a1d98cb1..628d0b85
--- a/docs/index.html
+++ b/docs/index.html
@@@ -233,16 -237,12 +236,18 @@@
</ul>
</div>
<div class="cap-group" data-reveal>
- <h3>🐚 Shell &amp; Messaging</h3>
+ <h3>💬 Messaging</h3>
+ <ul>
+ <li><code>send_message</code> — Send a message to approved Telegram recipients</li>
+ </ul>
+ </div>
+ <div class="cap-group" data-reveal>
+ <h3>🐚 Shell</h3>
<ul>
<li><code>run_command</code> — Execute shell commands</li>
+ <li><code>cd</code> — Change working directory (persists)</li>
<li><code>approve_command</code> — Permanently approve a command</li>
+ <li><code>send_message</code> — Message paired Telegram chat</li>
</ul>
</div>
<div class="cap-group" data-reveal>
diff --cc src/core/agent.ts
index f2116b80,714ed1b3..25219364
--- a/src/core/agent.ts
+++ b/src/core/agent.ts
@@@ -11,22 -11,57 +11,72 @@@ import type { ScheduledTaskManifest } f
import { Lifecycle } from './lifecycle.js';
import { Scheduler } from './scheduler.js';
import { logger } from '../utils/logger.js';
+import { CLIChannel } from '../channels/cli.js';
+import type { ArrowSelectOption } from '../utils/arrow-select.js';
+import {
+ approveTelegramPendingRequest,
+ approveTelegramPendingRequestByPairingCode,
+ clearTelegramAccess,
+ demoteTelegramAdmin,
+ getTelegramAccessSummary,
+ getTelegramApprovedUsers,
+ getTelegramPendingRequests,
+ promoteTelegramUserToAdmin,
+ rejectTelegramPendingRequest,
+ removeTelegramUser,
+ saveConfig,
+} from '../utils/config.js';
+ class ToolCallLoopDetector {
+ private recentCalls: Array<{ tool: string; params: string }> = [];
+ private maxEntries = 10;
+
+ record(toolName: string, params: Record<string, any>): void {
+ const paramsKey = JSON.stringify(params).slice(0, 100);
+ this.recentCalls.push({ tool: toolName, params: paramsKey });
+ if (this.recentCalls.length > this.maxEntries) {
+ this.recentCalls.shift();
+ }
+ }
+
+ detect(): { tool: string; count: number } | null {
+ if (this.recentCalls.length < 3) return null;
+
+ const last = this.recentCalls[this.recentCalls.length - 1];
+ let consecutiveCount = 0;
+ for (let i = this.recentCalls.length - 1; i >= 0; i--) {
+ if (this.recentCalls[i].tool === last.tool && this.recentCalls[i].params === last.params) {
+ consecutiveCount++;
+ } else {
+ break;
+ }
+ }
+
+ if (consecutiveCount >= 3) {
+ return { tool: last.tool, count: consecutiveCount };
+ }
+
+ const lastTool = last.tool;
+ let toolCount = 0;
+ for (let i = this.recentCalls.length - 1; i >= 0; i--) {
+ if (this.recentCalls[i].tool === lastTool) {
+ toolCount++;
+ } else {
+ break;
+ }
+ }
+ if (toolCount >= 4) {
+ return { tool: lastTool, count: toolCount };
+ }
+
+ return null;
+ }
+
+ reset(): void {
+ this.recentCalls = [];
+ }
+ }
+
const MAX_STEPS = 10;
export class Agent {