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: built-in web_search; Mercury looks things up on WhatsApp and calls

Salman Qureshi347c804ee348
diff --git a/plugins/whatsapp/companion/src/voice.rs b/plugins/whatsapp/companion/src/voice.rs
index f55a2501..eb6a90e3 100644
--- a/plugins/whatsapp/companion/src/voice.rs
+++ b/plugins/whatsapp/companion/src/voice.rs
@@ -199,9 +199,10 @@ pub async fn call_owner(st: Arc<State>, args: &Value) -> Result<String, String>
fn session_config(instructions: &str) -> Value {
let base = "You are Mercury, the user's AI assistant, talking on a WhatsApp voice call. \
Speak naturally and briefly: one or two sentences at a time, no lists, no markdown, no URLs read aloud. \
- For anything that needs Mercury's memory, files, tools, the web, or doing something, call ask_mercury with a clear question, \
- and say a short filler like 'one moment' first. Nothing that needs the user's approval can be done during a call; \
- if ask_mercury says so, offer to do it by message instead. When the user says goodbye or is done, say goodbye and call end_call.";
+ You don't know today's news or anything current yourself: for news, facts that may have changed, the user's own things \
+ (memory, files, calendar) or doing something, call ask_mercury with a clear, complete question; Mercury searches the web and uses its tools. \
+ Say a short filler like 'one moment, let me check' first, then speak its answer in your own words. \
+ 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",
"session": {
diff --git a/src/capabilities/registry.ts b/src/capabilities/registry.ts
index b8d95953..1dd7145c 100644
--- a/src/capabilities/registry.ts
+++ b/src/capabilities/registry.ts
@@ -34,6 +34,7 @@ import { createListIssuesTool } from './github/list-issues.js';
import { createCreateIssueTool } from './github/create-issue.js';
import { createGithubApiTool } from './github/github-api.js';
import { createFetchUrlTool } from './web/fetch-url.js';
+import { createWebSearchTool } from './web/web-search.js';
import {
createSpotifySearchTool,
createSpotifyPlayTool,
@@ -281,6 +282,7 @@ export class CapabilityRegistry {
}
this.tools.fetch_url = createFetchUrlTool({ isResearchMode: () => this.isResearchMode() });
+ this.tools.web_search = createWebSearchTool();
logger.info('Web fetch tool registered');
if (this.supervisor) {
diff --git a/src/capabilities/web/web-search.test.ts b/src/capabilities/web/web-search.test.ts
new file mode 100644
index 00000000..d63f0c66
--- /dev/null
+++ b/src/capabilities/web/web-search.test.ts
@@ -0,0 +1,56 @@
+import { afterEach, describe, expect, it, vi } from 'vitest';
+
+const GOOGLE_NEWS = `<?xml version="1.0"?><rss><channel><title>Google News</title>
+<item><title>Envoy meets leader in Moscow - BBC</title><link>https://news.google.com/rss/articles/abc</link><pubDate>Wed, 21 Oct 2026 08:00:00 GMT</pubDate><description>&lt;a href="x"&gt;Envoy meets leader&lt;/a&gt;&amp;nbsp;BBC</description><source url="https://www.bbc.co.uk">BBC</source></item>
+<item><title>Markets rally - Reuters</title><link>https://news.google.com/rss/articles/def</link><pubDate>Wed, 21 Oct 2026 07:00:00 GMT</pubDate><source url="https://reuters.com">Reuters</source></item>
+</channel></rss>`;
+
+const BING = `<?xml version="1.0"?><rss><channel>
+<item><title>what is the capital of australia - Bing</title><link>http://www.bing.com:80/search?q=x</link></item>
+<item><title>Canberra - Wikipedia</title><link>https://en.wikipedia.org/wiki/Canberra</link><description>Canberra is the capital city of Australia.</description></item>
+</channel></rss>`;
+
+vi.mock('../../utils/ssrf.js', () => ({
+ guardedFetch: vi.fn(async (url: string) => {
+ if (url.includes('news.google.com')) return new Response(GOOGLE_NEWS, { status: 200 });
+ if (url.includes('bing.com')) return new Response(BING, { status: 200 });
+ return new Response('', { status: 404 });
+ }),
+}));
+
+import { createWebSearchTool, parseRss, webSearch } from './web-search.js';
+import { guardedFetch } from '../../utils/ssrf.js';
+
+afterEach(() => vi.mocked(guardedFetch).mockClear());
+
+describe('web_search', () => {
+ it('parses Google News items, strips the source suffix, keeps the date', () => {
+ const r = parseRss(GOOGLE_NEWS, 5);
+ expect(r).toHaveLength(2);
+ expect(r[0]).toMatchObject({ title: 'Envoy meets leader in Moscow', source: 'BBC', date: 'Wed, 21 Oct 2026 08:00:00 GMT' });
+ expect(r[0].snippet).toBe('Envoy meets leader BBC');
+ });
+
+ it('drops Bing\'s self-link item', () => {
+ expect(parseRss(BING, 5).map((x) => x.url)).toEqual(['https://en.wikipedia.org/wiki/Canberra']);
+ });
+
+ it('news uses Google News; web uses Bing; both through the SSRF guard', async () => {
+ expect((await webSearch('', { kind: 'news' })).engine).toBe('Google News');
+ expect(vi.mocked(guardedFetch).mock.calls[0][0]).toBe('https://news.google.com/rss?hl=en-US&gl=US&ceid=US:en');
+ expect((await webSearch('capital of australia')).engine).toBe('Bing');
+ });
+
+ it('falls back to the other engine when one fails', async () => {
+ vi.mocked(guardedFetch).mockImplementationOnce(async () => new Response('', { status: 503 }));
+ expect((await webSearch('capital of australia')).engine).toBe('Google News');
+ });
+
+ it('the tool returns a readable list, or a clear error', async () => {
+ const t = createWebSearchTool() as unknown as { execute: (a: unknown, o: unknown) => Promise<string> };
+ const out = await t.execute({ query: '', kind: 'news' }, {});
+ expect(out).toMatch(/News results from Google News[\s\S]*1\. Envoy meets leader in Moscow \(BBC · Wed, 21 Oct 2026\)/);
+ vi.mocked(guardedFetch).mockImplementation(async () => new Response('', { status: 500 }));
+ expect(await t.execute({ query: 'x' }, {})).toMatch(/^Error: web search is unavailable/);
+ });
+});
diff --git a/src/capabilities/web/web-search.ts b/src/capabilities/web/web-search.ts
new file mode 100644
index 00000000..1ac04692
--- /dev/null
+++ b/src/capabilities/web/web-search.ts
@@ -0,0 +1,139 @@
+import { tool, zodSchema } from 'ai';
+import { z } from 'zod';
+import { guardedFetch } from '../../utils/ssrf.js';
+
+/**
+ * web_search: free, keyless search for the model.
+ *
+ * web → Bing's RSS results feed (falls back to Google News)
+ * news → Google News RSS, recent first (falls back to Bing)
+ *
+ * Read-only and through the same SSRF guard as fetch_url. Returns titles,
+ * links, snippets and dates; the model follows up with fetch_url to read a
+ * page. (DuckDuckGo's HTML endpoint answers automated requests with a 202
+ * challenge, which is why the old search skill often came back empty.)
+ */
+
+export interface SearchResult {
+ title: string;
+ url: string;
+ snippet: string;
+ source?: string;
+ date?: string;
+}
+
+const UA = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126 Safari/537.36';
+const TIMEOUT_MS = 12_000;
+
+function unescape(s: string): string {
+ return s
+ .replace(/&lt;/g, '<')
+ .replace(/&gt;/g, '>')
+ .replace(/&quot;/g, '"')
+ .replace(/&#39;|&apos;/g, "'")
+ .replace(/&#(\d+);/g, (_m, n) => String.fromCharCode(Number(n)))
+ .replace(/&amp;/g, '&');
+}
+
+function decode(s: string): string {
+ // Feeds often carry escaped HTML inside a field (Google News descriptions):
+ // unescape, strip tags, then unescape what was inside the markup.
+ return unescape(unescape(s.replace(/<!\[CDATA\[([\s\S]*?)\]\]>/g, '$1')).replace(/<[^>]+>/g, ' '))
+ .replace(/&nbsp;/g, ' ')
+ .replace(/<[^>]+>/g, ' ')
+ .replace(/\s+/g, ' ')
+ .trim();
+}
+
+function tag(item: string, name: string): string {
+ const m = new RegExp(`<${name}[^>]*>([\\s\\S]*?)</${name}>`, 'i').exec(item);
+ return m ? decode(m[1]) : '';
+}
+
+export function parseRss(xml: string, max: number): SearchResult[] {
+ const out: SearchResult[] = [];
+ for (const m of xml.matchAll(/<item>([\s\S]*?)<\/item>/gi)) {
+ const item = m[1];
+ const url = tag(item, 'link');
+ const title = tag(item, 'title');
+ if (!url || !title || /bing\.com(:\d+)?\/search\?/.test(url)) continue;
+ const source = tag(item, 'source');
+ out.push({
+ title: source && title.endsWith(` - ${source}`) ? title.slice(0, -(source.length + 3)) : title,
+ url,
+ snippet: tag(item, 'description').slice(0, 300),
+ source: source || undefined,
+ date: tag(item, 'pubDate') || undefined,
+ });
+ if (out.length >= max) break;
+ }
+ return out;
+}
+
+async function fetchText(url: string): Promise<string> {
+ const ac = new AbortController();
+ const timer = setTimeout(() => ac.abort(), TIMEOUT_MS);
+ try {
+ const res = await guardedFetch(url, ac.signal, { 'User-Agent': UA, Accept: 'application/rss+xml, application/xml, text/xml, */*' });
+ if (!res.ok) throw new Error(`HTTP ${res.status}`);
+ return await res.text();
+ } finally {
+ clearTimeout(timer);
+ }
+}
+
+async function bing(query: string, max: number): Promise<SearchResult[]> {
+ return parseRss(await fetchText(`https://www.bing.com/search?q=${encodeURIComponent(query)}&format=rss&count=${Math.min(20, max + 2)}`), max);
+}
+
+async function googleNews(query: string, max: number): Promise<SearchResult[]> {
+ const url = query.trim()
+ ? `https://news.google.com/rss/search?q=${encodeURIComponent(query)}&hl=en-US&gl=US&ceid=US:en`
+ : 'https://news.google.com/rss?hl=en-US&gl=US&ceid=US:en';
+ return parseRss(await fetchText(url), max);
+}
+
+export async function webSearch(query: string, opts: { kind?: 'web' | 'news'; max?: number } = {}): Promise<{ results: SearchResult[]; engine: string }> {
+ const max = Math.max(1, Math.min(opts.max ?? 8, 15));
+ const engines: Array<[string, (q: string, n: number) => Promise<SearchResult[]>]> = opts.kind === 'news'
+ ? [['Google News', googleNews], ['Bing', bing]]
+ : [['Bing', bing], ['Google News', googleNews]];
+ const errors: string[] = [];
+ for (const [name, run] of engines) {
+ try {
+ const results = await run(query, max);
+ if (results.length) return { results, engine: name };
+ errors.push(`${name}: no results`);
+ } catch (err) {
+ errors.push(`${name}: ${(err as Error).message}`);
+ }
+ }
+ throw new Error(`search failed (${errors.join('; ')})`);
+}
+
+export function createWebSearchTool() {
+ return tool({
+ description:
+ 'Search the web for current information: news, facts, prices, people, products, anything recent. ' +
+ 'kind "news" for headlines and recent events (query can be empty for top stories). ' +
+ 'Returns titles, links, snippets and dates; use fetch_url on a link to read the full page. ' +
+ 'Use this instead of guessing about anything that may have changed.',
+ inputSchema: zodSchema(z.object({
+ query: z.string().max(300).describe('What to search for. For top news, use kind "news" and an empty query.'),
+ kind: z.enum(['web', 'news']).default('web').optional(),
+ max: z.number().int().min(1).max(15).default(8).optional(),
+ })),
+ execute: async ({ query, kind, max }) => {
+ try {
+ const { results, engine } = await webSearch(query, { kind: kind ?? 'web', max: max ?? 8 });
+ const lines = results.map((r, i) => {
+ const meta = [r.source, r.date ? new Date(r.date).toUTCString().slice(0, 16) : ''].filter(Boolean).join(' · ');
+ return `${i + 1}. ${r.title}${meta ? ` (${meta})` : ''}\n ${r.url}${r.snippet ? `\n ${r.snippet}` : ''}`;
+ });
+ return `${kind === 'news' ? 'News' : 'Web'} results from ${engine} for "${query || 'top stories'}":\n\n${lines.join('\n')}`;
+ } catch (err) {
+ return `Error: web search is unavailable right now: ${(err as Error).message}`;
+ }
+ },
+ });
+}
diff --git a/src/plugins/channel-bridge.ts b/src/plugins/channel-bridge.ts
index e1b75d9e..c9b74944 100644
--- a/src/plugins/channel-bridge.ts
+++ b/src/plugins/channel-bridge.ts
@@ -249,7 +249,7 @@ export class PluginChannel extends BaseChannel {
channelType: this.type,
senderId: digits(owner),
senderRole: 'admin',
- content: `[Voice call — the owner asked this out loud. Answer in one to three short spoken sentences: no markdown, lists, links or code. Anything that needs approval can't be done during a call; say so and offer to do it by message.]\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. 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.]\n${question}`,
timestamp: Date.now(),
metadata: { pluginId: this.manifest.id, voiceCall: true, callId, channelAccess: this.accessFor('admin'), channelGuidance: this.guidance('admin', false, true) },
});
@@ -420,12 +420,12 @@ 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.'
+ ? '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.'
: `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') {
lines.push(role === 'admin'
- ? 'Access: Extended. Use your tools, files and memory as you normally would. Actions that need approval are confirmed by the owner replying 1 (yes), 2 (no) or 3 (always) in this chat: ask once, clearly, saying what will happen.'
+ ? 'Access: Extended. Use your tools, skills, files and memory exactly as you would anywhere else; don\'t hold back because this is a chat app. For news, current events, prices or facts that may have changed, call web_search (kind "news" for headlines) and answer from the results. Actions that need approval are confirmed by the owner replying 1 (yes), 2 (no) or 3 (always) in this chat: ask once, clearly, saying what will happen.'
: 'Access: Extended for members. You may use tools, but never share the owner\'s private details (memory, files, contacts, accounts) with this member unless the owner told you to.');
} else {
lines.push(role === 'admin'
diff --git a/src/plugins/integration.test.ts b/src/plugins/integration.test.ts
index 13cd2be0..f81a6513 100644
--- a/src/plugins/integration.test.ts
+++ b/src/plugins/integration.test.ts
@@ -91,3 +91,12 @@ describe('general access disables every tool for the turn', () => {
expect(Object.keys(reg.getTools()).length).toBeGreaterThan(0);
});
});
+
+describe('web_search is a built-in tool', () => {
+ it('is registered alongside fetch_url', async () => {
+ const { CapabilityRegistry } = await import('../capabilities/registry.js');
+ const reg = new CapabilityRegistry();
+ reg.registerAll();
+ expect(Object.keys(reg.getTools())).toEqual(expect.arrayContaining(['web_search', 'fetch_url']));
+ });
+});
diff --git a/src/skills/default-skills.ts b/src/skills/default-skills.ts
index 4ff20e42..abb68b31 100644
--- a/src/skills/default-skills.ts
+++ b/src/skills/default-skills.ts
@@ -19,11 +19,12 @@ export function getCategoryLabel(category: string): string {
return CATEGORY_LABELS[category] || category.charAt(0).toUpperCase() + category.slice(1);
}
-export const DEFAULT_SKILL_SEEDS: DefaultSkillSeed[] = [
- {
- dirName: 'web-search',
- fileName: 'SKILL.md',
- content: `---
+/**
+ * Earlier contents of built-in skills. A user's copy that still matches one
+ * exactly was never edited, so it is upgraded to the current seed.
+ */
+export const PREVIOUS_SEED_CONTENTS: Record<string, string[]> = {
+ 'web-search': [`---
name: web-search
description: Perform web searches using DuckDuckGo HTML and summarize sources.
version: 1.0.0
@@ -69,6 +70,61 @@ Use this skill when the user asks for current events, external facts, or web res
- If information is uncertain or conflicting, say so explicitly.
- Include source URLs in the response.
- Avoid fabricated citations.
+`],
+};
+
+export const DEFAULT_SKILL_SEEDS: DefaultSkillSeed[] = [
+ {
+ dirName: 'web-search',
+ fileName: 'SKILL.md',
+ content: `---
+name: web-search
+description: Search the web and the news with the web_search tool, read the best sources, and answer with links.
+version: 1.1.0
+category: web
+categories:
+ - web
+ - research
+intents:
+ - search the web
+ - look up
+ - find online
+ - what is
+ - current news
+ - search for
+ - find information about
+ - research
+ - web search
+ - latest news
+ - what's happening
+tags:
+ - search
+ - web
+ - research
+ - news
+allowed-tools:
+ - web_search
+ - fetch_url
+---
+
+# Web Search
+
+Use this skill when the user asks for current events, external facts, or web research.
+
+## Workflow
+
+1. Call web_search with a focused query. For news and recent events use kind "news"
+ (an empty query gives today's top stories).
+2. Open the most relevant one to three results with fetch_url when the snippets aren't enough.
+3. Cross-check key facts across at least 2 sources when possible.
+4. Return a concise answer with source links and clear caveats.
+
+## Rules
+
+- Prefer reliable sources (official docs, primary sources, reputable publications).
+- If information is uncertain or conflicting, say so explicitly.
+- Include source URLs in the response (on phone chats, keep it to the one or two that matter).
+- Avoid fabricated citations.
`,
},
{
diff --git a/src/skills/loader.ts b/src/skills/loader.ts
index fa57cca9..f5916a7a 100644
--- a/src/skills/loader.ts
+++ b/src/skills/loader.ts
@@ -5,7 +5,7 @@ import { getMercuryHome } from '../utils/config.js';
import type { SkillDiscovery, Skill, SkillMeta } from './types.js';
import { IntentRouter } from './intent-router.js';
import { logger } from '../utils/logger.js';
-import { DEFAULT_SKILL_SEEDS, getCategoryLabel } from './default-skills.js';
+import { DEFAULT_SKILL_SEEDS, PREVIOUS_SEED_CONTENTS, getCategoryLabel } from './default-skills.js';
const SKILL_FILE = 'SKILL.md';
const DISABLED_FILE = '.disabled';
@@ -407,7 +407,17 @@ Describe what this skill enables Mercury to do. When invoked via the use_skill t
for (const seed of DEFAULT_SKILL_SEEDS) {
const skillDir = join(this.skillsDir, seed.dirName);
const skillFile = join(skillDir, seed.fileName);
- if (existsSync(skillFile)) continue;
+ if (existsSync(skillFile)) {
+ // Upgrade an untouched copy of an older built-in version; never overwrite edits.
+ try {
+ const current = readFileSync(skillFile, 'utf-8');
+ if (current !== seed.content && (PREVIOUS_SEED_CONTENTS[seed.dirName] ?? []).includes(current)) {
+ writeFileSync(skillFile, seed.content, 'utf-8');
+ logger.info({ skill: seed.dirName }, 'Upgraded default skill');
+ }
+ } catch { /* leave it */ }
+ continue;
+ }
mkdirSync(skillDir, { recursive: true });
writeFileSync(skillFile, seed.content, 'utf-8');