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

LoWandererc4b446bdc35b
diff --cc .env.example
index dd39ac0c,5157a8bc..47d8716e
--- a/.env.example
+++ b/.env.example
@@@ -53,3 -38,13 +53,13 @@@ HEARTBEAT_INTERVAL_MINUTES=6
# ── Token Budget ──
DAILY_TOKEN_BUDGET=50000
+
+ # ── GitHub (optional) ──
+ # Fine-grained personal access token with repo scope
+ # Create at: https://github.com/settings/tokens?type=beta
+ GITHUB_TOKEN=
+ GITHUB_USERNAME=
+ # Default repo for GitHub tools (optional — you can also specify per-call)
+ GITHUB_DEFAULT_OWNER=
-GITHUB_DEFAULT_REPO=
++GITHUB_DEFAULT_REPO=
diff --cc package-lock.json
index 322c1434,178b8226..01a15ef1
--- a/package-lock.json
+++ b/package-lock.json
@@@ -22,10 -21,7 +21,8 @@@
"js-tiktoken": "^1.0.19",
"marked": "^14.1.4",
"node-cron": "^3.0.3",
+ "ollama-ai-provider": "^1.2.0",
"pino": "^9.6.0",
- "react": "^18.3.1",
"yaml": "^2.7.0",
"zod": "^3.25.76"
},
diff --cc package.json
index e8852da2,2fd14aa1..0fd85bfb
--- a/package.json
+++ b/package.json
@@@ -67,10 -65,7 +65,8 @@@
"js-tiktoken": "^1.0.19",
"marked": "^14.1.4",
"node-cron": "^3.0.3",
+ "ollama-ai-provider": "^1.2.0",
"pino": "^9.6.0",
- "react": "^18.3.1",
"yaml": "^2.7.0",
"zod": "^3.25.76"
},
@@@ -83,6 -78,6 +79,6 @@@
"vitest": "^3.0.0"
},
"engines": {
- "node": ">=20.0.0"
+ "node": ">=18.0.0"
}
-}
+}
diff --cc src/capabilities/registry.ts
index 437693a4,0b7e139a..a8609f29
--- a/src/capabilities/registry.ts
+++ b/src/capabilities/registry.ts
@@@ -7,7 -7,7 +7,8 @@@ import { createListDirTool } from './fi
import { createDeleteFileTool } from './filesystem/delete-file.js';
import { createEditFileTool } from './filesystem/edit-file.js';
import { createSendFileTool } from './filesystem/send-file.js';
+import { createSendMessageTool } from './messaging/send-message.js';
+ import { createApproveScopeTool } from './filesystem/approve-scope.js';
import { createRunCommandTool } from './shell/run-command.js';
import { createApproveCommandTool } from './shell/approve-command.js';
import { createInstallSkillTool } from './skills/install-skill.js';
diff --cc src/index.ts
index 4378e0e7,fb4c3f88..436a39ed
--- a/src/index.ts
+++ b/src/index.ts
@@@ -73,194 -78,27 +80,215 @@@ function maskKey(key: string): string
return key.slice(0, 4) + '••••' + key.slice(-4);
}
+const PROVIDER_OPTIONS: Array<{ key: ProviderName; label: string }> = [
+ { key: 'deepseek', label: 'DeepSeek' },
+ { key: 'openai', label: 'OpenAI' },
+ { key: 'anthropic', label: 'Anthropic' },
+ { key: 'grok', label: 'Grok (xAI)' },
+ { key: 'ollamaCloud', label: 'Ollama Cloud' },
+ { key: 'ollamaLocal', label: 'Ollama Local' },
+];
+
+function getConfiguredProviderNames(config: MercuryConfig): ProviderName[] {
+ return PROVIDER_OPTIONS
+ .map((option) => option.key)
+ .filter((key) => isProviderConfigured(config.providers[key]));
+}
+
+function getProviderLabel(name: ProviderName): string {
+ return PROVIDER_OPTIONS.find((option) => option.key === name)?.label || name;
+}
+
+function parseProviderSelection(input: string): ProviderName[] | null {
+ const values = input.split(/[\s,]+/).map((value) => value.trim()).filter(Boolean);
+ if (values.length === 0) return [];
+
+ const selected: ProviderName[] = [];
+ for (const value of values) {
+ const index = parseInt(value, 10);
+ if (isNaN(index) || index < 1 || index > PROVIDER_OPTIONS.length) {
+ return null;
+ }
+ const provider = PROVIDER_OPTIONS[index - 1].key;
+ if (!selected.includes(provider)) {
+ selected.push(provider);
+ }
+ }
+ return selected;
+}
+
+async function chooseProvidersToConfigure(config: MercuryConfig, isReconfig: boolean): Promise<ProviderName[]> {
+ const configured = getConfiguredProviderNames(config);
+
+ while (true) {
+ for (let i = 0; i < PROVIDER_OPTIONS.length; i++) {
+ const option = PROVIDER_OPTIONS[i];
+ const status = configured.includes(option.key) ? ' (configured)' : '';
+ console.log(chalk.white(` ${i + 1}. ${option.label}${status}`));
+ }
+ console.log('');
+
+ const prompt = isReconfig
+ ? chalk.white(' Choose providers to configure [comma-separated, Enter keeps current]: ')
+ : chalk.white(' Choose providers to configure [comma-separated, Enter for DeepSeek]: ');
+
+ const input = await ask(prompt);
+ const parsed = parseProviderSelection(input);
+ if (parsed === null) {
+ console.log(chalk.red(' Please choose valid provider numbers, like `1` or `1,3,5`.'));
+ console.log('');
+ continue;
+ }
+
+ if (parsed.length > 0) return parsed;
+ if (!isReconfig) return ['deepseek'];
+ return configured.length > 0 ? configured : ['deepseek'];
+ }
+}
+
+async function chooseDefaultProvider(config: MercuryConfig): Promise<void> {
+ const configured = getConfiguredProviderNames(config);
+
+ if (configured.length === 0) {
+ return;
+ }
+
+ if (configured.length === 1) {
+ config.providers.default = configured[0];
+ console.log(chalk.dim(` Default provider set to ${getProviderLabel(configured[0])}`));
+ return;
+ }
+
+ const suggested = configured.includes('deepseek') ? 'deepseek' : configured[0];
+
+ console.log('');
+ console.log(chalk.bold.white(' Default Provider'));
+ console.log(chalk.dim(' Select the LLM provider Mercury should use first.'));
+ console.log('');
+ for (let i = 0; i < configured.length; i++) {
+ const provider = configured[i];
+ const recommended = provider === suggested ? ' (recommended)' : '';
+ const current = provider === config.providers.default ? ' (current)' : '';
+ console.log(chalk.white(` ${i + 1}. ${getProviderLabel(provider)}${recommended}${current}`));
+ }
+ console.log('');
+
+ while (true) {
+ const choice = await ask(chalk.white(` Choose [1-${configured.length}] [Enter for ${getProviderLabel(suggested)}]: `));
+ if (!choice) {
+ config.providers.default = suggested;
+ return;
+ }
+
+ const num = parseInt(choice, 10);
+ if (num >= 1 && num <= configured.length) {
+ config.providers.default = configured[num - 1];
+ return;
+ }
+
+ console.log(chalk.red(' Please choose a valid number from the list above.'));
+ }
+}
+
+function looksLikeToken(value: string, minLength: number = 20): boolean {
+ return value.length >= minLength && !/\s/.test(value) && /[A-Za-z]/.test(value) && /\d/.test(value);
+}
+
+function validateApiKey(provider: ProviderName, value: string): string | null {
+ if (provider === 'openai') {
+ return /^sk-(proj-|svcacct-)?[A-Za-z0-9_-]{16,}$/i.test(value)
+ ? null
+ : 'OpenAI keys must start with `sk-`, `sk-proj-`, or `sk-svcacct-`.';
+ }
+
+ if (provider === 'anthropic') {
+ return /^sk-ant-[A-Za-z0-9_-]{16,}$/i.test(value)
+ ? null
+ : 'Anthropic keys must start with `sk-ant-`.';
+ }
+
+ if (provider === 'deepseek') {
+ return /^sk-[A-Za-z0-9_-]{16,}$/i.test(value)
+ ? null
+ : 'DeepSeek keys must start with `sk-`.';
+ }
+
+ if (provider === 'grok') {
+ return looksLikeToken(value)
+ ? null
+ : 'Grok keys must look like a real API token: long, no spaces, and not plain text.';
+ }
+
+ if (provider === 'ollamaCloud') {
+ return looksLikeToken(value)
+ ? null
+ : 'Ollama Cloud keys must look like a real API token: long, no spaces, and not plain text.';
+ }
+
+ return null;
+}
+
+function validateBaseUrl(value: string): string | null {
+ try {
+ const parsed = new URL(value);
+ if (parsed.protocol !== 'http:' && parsed.protocol !== 'https:') {
+ return 'Base URL must start with http:// or https://.';
+ }
+ return null;
+ } catch {
+ return 'Please enter a valid URL.';
+ }
+}
+
+function validateModelName(value: string): string | null {
+ if (!value.trim()) return 'Model name is required.';
+ if (/\s/.test(value)) return 'Model name cannot contain spaces.';
+ return null;
+}
+
+async function promptValidatedValue(
+ prompt: string,
+ validator: (value: string) => string | null,
+ existingValue?: string,
+ options?: { allowSkip?: boolean },
+): Promise<string | undefined> {
+ while (true) {
+ const value = await ask(prompt);
+ if (!value) {
+ if (existingValue) return existingValue;
+ if (options?.allowSkip) return undefined;
+ console.log(chalk.red(' A value is required here.'));
+ continue;
+ }
+
+ const error = validator(value);
+ if (!error) return value;
+
+ console.log(chalk.red(` ${error}`));
+ }
+}
+
+ function appendToEnv(key: string, value: string): void {
+ const envPath = join(getMercuryHome(), '.env');
+ let envContent = '';
+ if (existsSync(envPath)) {
+ envContent = readFileSync(envPath, 'utf-8');
+ }
+ const lines = envContent.split('\n').filter((l: string) => !l.startsWith(`${key}=`) && l.trim() !== '');
+ lines.push(`${key}=${value}`);
+ writeFileSync(envPath, lines.join('\n') + '\n', 'utf-8');
+ process.env[key] = value;
+ }
+
+ function parseGithubRepo(input: string): { owner: string; repo: string } | null {
+ const trimmed = input.trim().replace(/\/+$/, '');
+ const urlMatch = trimmed.match(/github\.com\/([^/]+)\/([^/]+)/);
+ if (urlMatch) return { owner: urlMatch[1], repo: urlMatch[2] };
+ const shortMatch = trimmed.match(/^([^/\s]+)\/([^/\s]+)$/);
+ if (shortMatch) return { owner: shortMatch[1], repo: shortMatch[2] };
+ return null;
+ }
+
async function configure(existingConfig?: MercuryConfig): Promise<void> {
const isReconfig = !!existingConfig;
const config = existingConfig ?? loadConfig();
@@@ -571,21 -383,9 +646,24 @@@ async function runAgent(isDaemon: boole
}
});
+ capabilities.setSendMessageHandler(async (content: string) => {
+ const telegram = channels.get('telegram');
+ const pairedChatId = config.channels.telegram.pairedChatId;
+ const pairedUserId = config.channels.telegram.pairedUserId;
+
+ if (!config.channels.telegram.enabled || !telegram) {
+ throw new Error('Telegram is not configured. Add a bot token in setup or run `mercury doctor`.');
+ }
+
+ if (pairedChatId == null || pairedUserId == null) {
+ throw new Error('Telegram is not paired. Complete the pairing flow with /start or /pair from the Telegram owner account.');
+ }
+
+ await telegram.send(content, `telegram:${pairedChatId}`);
+ });
+ if (process.env.GITHUB_TOKEN) {
+ setGitHubToken(process.env.GITHUB_TOKEN);
+ }
capabilities.registerAll();
diff --cc src/utils/config.ts
index 22d2b5e6,e187fd42..8079b5fe
--- a/src/utils/config.ts
+++ b/src/utils/config.ts
@@@ -50,11 -43,14 +54,17 @@@ export interface MercuryConfig
webhookUrl?: string;
allowedChatIds?: number[];
streaming?: boolean;
+ pairedUserId?: number;
+ pairedChatId?: number;
+ pairedUsername?: string;
};
};
+ github: {
+ username: string;
+ email: string;
+ defaultOwner: string;
+ defaultRepo: string;
+ };
memory: {
dir: string;
shortTermMaxMessages: number;