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.

refactor(core): move /bg, /saver, /session, fast-path and CLI menus out of Agent

Salman Qureshi18eaea77a7c8
diff --git a/src/core/agent.ts b/src/core/agent.ts
index 046e13f6..2de86359 100644
--- a/src/core/agent.ts
+++ b/src/core/agent.ts
@@ -97,6 +97,13 @@ import { buildFileChangePreview } from '../utils/file-preview.js';
import { whatsNewText } from '../utils/whats-new.js';
import { handleBotsCommand as handleBotsCommandImpl } from './commands/bots-command.js';
import { handleChatCommand as handleChatCommandImpl } from './commands/chat-command.js';
+import { handleBgCommand as handleBgCommandImpl } from './commands/bg-command.js';
+import { handleSaverCommand as handleSaverCommandImpl } from './commands/saver-command.js';
+import { handleSessionCommand as handleSessionCommandImpl } from './commands/session-command.js';
+import { handleFastPathCommand as handleFastPathCommandImpl } from './commands/fast-path-command.js';
+import { openCliMemoryMenu as openCliMemoryMenuImpl } from './commands/cli-memory-menu.js';
+import { openMemoryChoiceMenu as openMemoryChoiceMenuImpl } from './commands/memory-choice-menu.js';
+import { openCliCommandMenu as openCliCommandMenuImpl } from './commands/cli-command-menu.js';
/**
* Step-aware text stream for the TUI. The SDK's textStream concatenates
@@ -506,8 +513,8 @@ export class Agent {
processing = false;
telegramStreaming: boolean;
currentMessage: ChannelMessage | null = null;
- private currentAbort: AbortController | null = null;
- private currentAbortReason: 'stalled' | 'time-limit' | 'memory-pressure' | 'backgrounded' | 'stopped' | 'halted' | null = null;
+ currentAbort: AbortController | null = null;
+ currentAbortReason: 'stalled' | 'time-limit' | 'memory-pressure' | 'backgrounded' | 'stopped' | 'halted' | null = null;
private lastProgressAt = 0;
currentActivity = '';
completedStepCount = 0;
@@ -534,7 +541,7 @@ export class Agent {
private statusVerbTurnCount = 0;
private statusVerbsInFlight = false;
private statusVerbsDisabled = false;
- private sessionSyncEnabled = false;
+ sessionSyncEnabled = false;
private readonly workLedger: WorkLedger;
private currentWorkKey: string | null = null;
private outboxRetryTimer: ReturnType<typeof setInterval> | null = null;
@@ -552,7 +559,7 @@ export class Agent {
public tokenBudget: TokenBudget,
capabilities: CapabilityRegistry,
scheduler: Scheduler,
- private sessions: SessionRepository,
+ public sessions: SessionRepository,
private generateTitle?: (input: { userMessage: string; assistantMessage: string; providerName: string }) => Promise<string>,
) {
this.lifecycle = new Lifecycle();
@@ -880,7 +887,7 @@ export class Agent {
]);
}
- private queueMessage(message: ChannelMessage, workKey?: string): void {
+ queueMessage(message: ChannelMessage, workKey?: string): void {
if (workKey && this.queuedWorkKeys.has(workKey)) return;
this.messageQueue.push({ message, workKey });
if (workKey) this.queuedWorkKeys.add(workKey);
@@ -975,102 +982,10 @@ export class Agent {
}
private async handleFastPathCommand(msg: ChannelMessage): Promise<void> {
- const trimmed = msg.content.trim();
- const channel = this.channels.getChannelForMessage(msg);
- if (!channel) return;
-
- const activeAgents = this.supervisor ? this.supervisor.getActiveAgents() : [];
- const hasActiveAgents = activeAgents.length > 0;
- const busyPrefix = hasActiveAgents ? '' : '';
-
- if (trimmed === '/sessions' || trimmed.startsWith('/session')) {
- await this.handleSessionCommand(trimmed, msg.channelType, msg.channelId);
- return;
- }
-
- if (trimmed === '/agents' || trimmed === '/status') {
- if (this.supervisor) {
- const agents = this.supervisor.getActiveAgents();
- if (agents.length === 0) {
- await channel.send('No active sub-agents.', msg.channelId);
- } else {
- let text = '**Sub-Agents:**\n\n';
- for (const a of agents) {
- const icon = a.status === 'running' ? 'πŸ”„' : a.status === 'pending' ? '⏳' : a.status === 'completed' ? 'βœ…' : '❌';
- text += `${icon} **${a.id}**: ${a.task.slice(0, 60)}${a.task.length > 60 ? '...' : ''} β€” ${a.status}${a.progress ? ` (${a.progress})` : ''}\n`;
- }
- await channel.send(text, msg.channelId);
- }
- } else {
- await channel.send('Sub-agents not enabled.', msg.channelId);
- }
- return;
- }
-
- if (trimmed === '/halt' || trimmed === '/stop') {
- await channel.send(await this.stopAllWork(trimmed === '/stop' ? 'stopped' : 'halted'), msg.channelId);
- return;
- }
-
- // Bots run outside the main queue β€” /bots commands are always fast-path.
- if (trimmed.startsWith('/bots')) {
- await this.handleBotsCommand(trimmed, msg, channel);
- return;
- }
-
- if (trimmed.startsWith('/bg')) {
- await this.handleBgCommand(trimmed, msg, channel);
- return;
- }
-
- if (trimmed === '/progress' || trimmed === '/still') {
- if (!this.processing || !this.currentMessage) {
- await channel.send('No active foreground task.', msg.channelId);
- return;
- }
- const elapsedSec = Math.round((Date.now() - this.currentMessage.timestamp) / 1000);
- const stepInfo = this.completedStepCount > 0 ? ` Β· step ${this.completedStepCount}/${MAX_STEPS}` : '';
- const narrative = formatNarrative(this.stepNarrative, this.currentActivity, 10);
- const narrativeBlock = narrative ? `\n${narrative}` : '';
- await channel.send(
- `⏳ Task in progress (${elapsedSec}s${stepInfo})${narrativeBlock}\nUse /bg current to move it to background.`,
- msg.channelId,
- );
- return;
- }
-
- if (trimmed === '/help') {
- await channel.send('Agent is busy. Available: /sessions, /session, /agents, /halt, /stop, /progress, /spotify, /code, /research, /memory, /bg', msg.channelId);
- return;
- }
-
- if (trimmed.startsWith('/spotify')) {
- await this.handleFastPathSpotify(trimmed, msg, channel);
- return;
- }
-
- if (trimmed.startsWith('/code')) {
- await this.handleFastPathCode(trimmed, msg, channel);
- return;
- }
-
- if (trimmed === '/memory') {
- await channel.send('Agent is busy. Memory management will be available after current task completes.', msg.channelId);
- return;
- }
-
- if (hasActiveAgents) {
- const agentList = activeAgents.map(a => `**${a.id}**: ${a.task.slice(0, 40)}`).join(', ');
- await channel.send(`I'm busy working on sub-agent tasks (${agentList}). Your message has been queued β€” I'll respond once I'm free. Use /agents to check status.`, msg.channelId);
- } else {
- const elapsedSec = this.currentMessage ? Math.round((Date.now() - this.currentMessage.timestamp) / 1000) : 0;
- await channel.send(`I'm busy processing${elapsedSec > 0 ? ` (${elapsedSec}s elapsed)` : ''}. Use /progress for live status or /bg current to move this task to the background.`, msg.channelId);
- }
-
- this.queueMessage(msg);
+ return handleFastPathCommandImpl(this, msg);
}
- private async handleFastPathSpotify(trimmed: string, msg: ChannelMessage, channel: any): Promise<void> {
+ async handleFastPathSpotify(trimmed: string, msg: ChannelMessage, channel: any): Promise<void> {
if (!this.spotifyClient) {
await channel.send('Spotify is not connected.', msg.channelId);
return;
@@ -1104,7 +1019,7 @@ export class Agent {
await channel.send('Agent is busy. Full Spotify controls will be available after current task completes.', msg.channelId);
}
- private async handleFastPathCode(trimmed: string, msg: ChannelMessage, channel: any): Promise<void> {
+ async handleFastPathCode(trimmed: string, msg: ChannelMessage, channel: any): Promise<void> {
const rawArgs = trimmed.slice('/code'.length).trim().toLowerCase();
if (rawArgs === 'status') {
await channel.send(this.programmingMode.getStatusText(), msg.channelId);
@@ -1420,157 +1335,12 @@ export class Agent {
* Always fast-path: bots live outside the main message queue, so bot
* control stays responsive while the agent is busy.
*/
- private async handleBotsCommand(trimmed: string, msg: ChannelMessage, channel: any): Promise<void> {
+ async handleBotsCommand(trimmed: string, msg: ChannelMessage, channel: any): Promise<void> {
return handleBotsCommandImpl(this, trimmed, msg, channel);
}
async handleBgCommand(trimmed: string, msg: ChannelMessage, channel: any): Promise<void> {
- const parts = trimmed.trim().split(/\s+/);
- const sub = parts.length > 1 ? parts[1] : '';
- const args = parts.slice(1).join(' ');
-
- if (sub === 'current') {
- if (!this.processing || !this.currentMessage) {
- await channel.send('No active task to background.', msg.channelId);
- return;
- }
- const taskDescription = this.currentMessage.content.trim();
- const sourceChannelId = this.currentMessage.channelId;
- const sourceChannelType = this.currentMessage.channelType as any;
-
- if (this.currentAbort) {
- this.currentAbortReason = 'backgrounded';
- this.currentAbort.abort();
- }
-
- if (this.supervisor) {
- const agentId = await this.supervisor.spawn({
- task: taskDescription,
- sourceChannelId,
- sourceChannelType,
- });
- const bgId = this.backgroundTasks.spawnAgent(taskDescription, this.capabilities.getCwd(), agentId);
- await channel.send(`πŸ“‹ Active task moved to background as ${bgId}. I'll notify you when it completes.`, msg.channelId);
- } else {
- await channel.send('Cannot background: sub-agents not available. The active task has been aborted.', msg.channelId);
- }
-
- this.syncBgTasksToTui();
- return;
- }
-
- if (sub === 'list' || sub === '' || sub === 'ls') {
- const tasks = this.backgroundTasks.getAllSummaries();
- if (tasks.length === 0) {
- await channel.send('No background tasks.', msg.channelId);
- return;
- }
- const lines = tasks.map((t) => {
- const icon = t.status === 'running' ? '⏳' : t.status === 'completed' ? 'βœ…' : t.status === 'failed' ? '❌' : t.status === 'timed_out' ? '⏱' : 'β›”';
- const label = t.command || t.task || t.id;
- const elapsed = t.runningMs ? ` (${Math.round(t.runningMs / 1000)}s)` : t.completedAt ? ` (${((t.completedAt - t.startedAt) / 1000).toFixed(1)}s)` : '';
- const short = label.length > 60 ? label.slice(0, 57) + '...' : label;
- return `${icon} ${t.id}: ${short}${elapsed} β€” ${t.status}`;
- });
- await channel.send(`**Background Tasks:**\n${lines.join('\n')}\n\nUse /bg <id> for details, /bg cancel <id> to cancel, /bg clear to prune completed tasks.`, msg.channelId);
- return;
- }
-
- if (sub === 'clear') {
- const cleared = this.backgroundTasks.clearCompleted();
- await channel.send(`Cleared ${cleared} completed task(s).`, msg.channelId);
- this.syncBgTasksToTui();
- return;
- }
-
- if (sub === 'cancel' || sub === 'stop' || sub === 'kill') {
- const taskId = parts[2];
- if (!taskId) {
- await channel.send(`Usage: /bg ${sub} <id>`, msg.channelId);
- return;
- }
- const cancelled = this.backgroundTasks.cancel(taskId);
- if (cancelled) {
- await channel.send(`β›” Stopped background task ${taskId}.`, msg.channelId);
- } else {
- await channel.send(`Task "${taskId}" not found or not running.`, msg.channelId);
- }
- this.syncBgTasksToTui();
- return;
- }
-
- if (sub === 'killall' || sub === 'stopall') {
- const count = this.backgroundTasks.cancelAll();
- if (count === 0) {
- await channel.send('No running background tasks to stop.', msg.channelId);
- } else {
- await channel.send(`β›” Stopped ${count} background task${count === 1 ? '' : 's'}.`, msg.channelId);
- }
- this.syncBgTasksToTui();
- return;
- }
-
- const specificTask = this.backgroundTasks.getSummary(sub);
- if (specificTask) {
- const task = this.backgroundTasks.get(sub)!;
- const label = task.command || task.task || task.id;
- const elapsed = task.status === 'running'
- ? `Running for ${Math.round((Date.now() - task.startedAt) / 1000)}s`
- : task.completedAt
- ? `Completed in ${((task.completedAt - task.startedAt) / 1000).toFixed(1)}s`
- : task.status;
- const output = (task.stdout + '\n' + task.stderr).trim();
- const preview = output.length > 2000 ? output.slice(-2000) : output;
- await channel.send(`**${specificTask.id}**: ${label}\nStatus: ${elapsed}\nExit code: ${task.exitCode ?? 'N/A'}\n\n${preview || '(no output)'}`, msg.channelId);
- return;
- }
-
- const colonIdx = trimmed.indexOf(':');
- if (colonIdx !== -1 && trimmed[colonIdx + 1] === ' ') {
- const taskDescription = trimmed.slice(colonIdx + 1).trim();
- if (!taskDescription) {
- await channel.send('Usage: /bg: <natural language task> or /bg <shell command>', msg.channelId);
- return;
- }
- if (!this.supervisor) {
- await channel.send('Sub-agents are not available. Use /bg <command> for shell commands.', msg.channelId);
- return;
- }
- const agentId = await this.supervisor.spawn({
- task: taskDescription,
- sourceChannelId: msg.channelId,
- sourceChannelType: msg.channelType as any,
- });
- const bgId = this.backgroundTasks.spawnAgent(taskDescription, this.capabilities.getCwd(), agentId);
- this.backgroundTasks.registerComplete(bgId, (task) => {
- if (task.status === 'running') return;
- });
- await channel.send(`πŸ“‹ Background agent ${bgId} started: "${taskDescription.slice(0, 50)}${taskDescription.length > 50 ? '...' : ''}"`, msg.channelId);
- this.syncBgTasksToTui();
- return;
- }
-
- const command = args || '';
- if (!command) {
- await channel.send('Usage:\nβ€’ /bg <command> β€” run a shell command in the background\nβ€’ /bg: <task> β€” delegate an LLM task to the background\nβ€’ /bg current β€” move the active task to the background\nβ€’ /bg list β€” show all background tasks\nβ€’ /bg <id> β€” show task details\nβ€’ /bg stop <id> β€” stop a running task\nβ€’ /bg killall β€” stop all running tasks\nβ€’ /bg clear β€” prune completed tasks', msg.channelId);
- return;
- }
-
- // `/bg <command>` is an alternate shell entry point: it must honor the
- // same approval boundary as the run_command tool, otherwise an
- // authenticated chat user can bypass Ask-Me approval and run arbitrary
- // commands. checkShellCommand() also enforces the blocked list and any
- // cwd/scope containment rules.
- const check = await this.capabilities.permissions.checkShellCommand(command);
- if (!check.allowed) {
- await channel.send(`β›” Not running in background: ${check.reason ?? 'command requires approval'}\nApprove the command first (or run it without /bg) and try again.`, msg.channelId);
- return;
- }
-
- const cwd = this.capabilities.getCwd();
- const bgId = this.backgroundTasks.spawnShell(command, cwd);
- await channel.send(`πŸ“‹ Background task ${bgId} started: "${command.slice(0, 50)}${command.length > 50 ? '...' : ''}"`, msg.channelId);
- this.syncBgTasksToTui();
+ return handleBgCommandImpl(this, trimmed, msg, channel);
}
syncBgTasksToTui(): void {
@@ -4661,118 +4431,11 @@ Is this productive iteration or a stuck loop?`,
* Subcommands: (empty)|status|on|off|toggle|threshold <n>|auto on|off|routing on|off|stats
*/
async handleSaverCommand(subcommand: string, channelType: string, channelId: string): Promise<void> {
- const channel = this.channels.get(channelType as any);
- if (!channel) return;
-
- const parts = subcommand.trim().split(/\s+/).filter(Boolean);
- const action = (parts[0] || '').toLowerCase();
- const arg = (parts[1] || '').toLowerCase();
-
- const showStatus = async () => {
- const text = this.saverMode.getStatusText(
- this.tokenBudget.getSavedLifetime(),
- this.tokenBudget.getSavedToday(),
- );
- const usagePct = Math.round(this.tokenBudget.getUsagePercentage());
- await channel.send(`${text}\nCurrent daily usage: ${usagePct}%`, channelId);
- this.syncSaverToCli();
- };
-
- if (!action || action === 'status' || action === 'stats') {
- await showStatus();
- return;
- }
-
- if (action === 'on' || action === 'enable') {
- this.saverMode.enable();
- await channel.send(
- '⚑ Token Saver Mode enabled. Responses will be terser, step limits lower, and history window shorter to conserve tokens.',
- channelId,
- );
- this.syncSaverToCli();
- return;
- }
-
- if (action === 'off' || action === 'disable') {
- this.saverMode.disable();
- await channel.send('Token Saver Mode disabled. Normal response settings restored.', channelId);
- this.syncSaverToCli();
- return;
- }
-
- if (action === 'toggle') {
- const next = this.saverMode.toggle();
- await channel.send(
- next === 'on'
- ? '⚑ Token Saver Mode enabled.'
- : 'Token Saver Mode disabled.',
- channelId,
- );
- this.syncSaverToCli();
- return;
- }
-
- if (action === 'threshold') {
- const n = parseInt(parts[1], 10);
- if (isNaN(n) || n < 0 || n > 100) {
- await channel.send('Usage: /saver threshold <0-100> β€” percentage of daily budget at which saver auto-engages. Set 0 to disable.', channelId);
- return;
- }
- this.saverMode.setAutoThreshold(n);
- await channel.send(
- n === 0
- ? 'Saver auto-engage disabled (threshold set to 0).'
- : `Saver auto-engage threshold set to ${n}% of daily budget.`,
- channelId,
- );
- return;
- }
-
- if (action === 'auto') {
- if (arg === 'on' || arg === 'enable') {
- this.saverMode.setAutoEnabled(true);
- await channel.send(`Saver auto-engage enabled (at ${this.saverMode.getAutoThreshold()}% usage).`, channelId);
- } else if (arg === 'off' || arg === 'disable') {
- this.saverMode.setAutoEnabled(false);
- await channel.send('Saver auto-engage disabled. Saver will only activate when you run /saver on.', channelId);
- this.syncSaverToCli();
- } else {
- await channel.send(
- `Saver auto-engage is currently ${this.saverMode.isAutoEnabled() ? 'ON' : 'OFF'} (threshold: ${this.saverMode.getAutoThreshold()}%).\nUse /saver auto on|off to change.`,
- channelId,
- );
- }
- return;
- }
-
- if (action === 'routing') {
- if (arg === 'on' || arg === 'enable') {
- this.saverMode.setRoutingEnabled(true);
- await channel.send('Saver cheap-provider routing enabled (when saver is active, cheaper providers will be preferred).', channelId);
- } else if (arg === 'off' || arg === 'disable') {
- this.saverMode.setRoutingEnabled(false);
- await channel.send('Saver cheap-provider routing disabled.', channelId);
- } else {
- await channel.send(`Saver cheap-provider routing is currently ${this.saverMode.isRoutingEnabled() ? 'ON' : 'OFF'}.\nUse /saver routing on|off to change.`, channelId);
- }
- return;
- }
-
- await channel.send(
- 'Unknown saver command. Available:\n' +
- ' /saver β€” show status and savings\n' +
- ' /saver on β€” manually enable\n' +
- ' /saver off β€” disable\n' +
- ' /saver toggle β€” flip on/off\n' +
- ' /saver threshold <0-100> β€” auto-engage threshold (default 75)\n' +
- ' /saver auto on|off β€” enable/disable auto-engagement\n' +
- ' /saver routing on|off β€” prefer cheap providers while active (opt-in)',
- channelId,
- );
+ return handleSaverCommandImpl(this, subcommand, channelType, channelId);
}
/** Push the current saver state to the CLI status bar if present. */
- private syncSaverToCli(): void {
+ syncSaverToCli(): void {
const ch = this.channels.get('cli');
if (ch && (ch as any).setSaverMode) {
(ch as any).setSaverMode(
@@ -4988,7 +4651,7 @@ Is this productive iteration or a stuck loop?`,
}
}
- private async handleChatCommand(content: string, channelType: string, channelId: string): Promise<boolean> {
+ async handleChatCommand(content: string, channelType: string, channelId: string): Promise<boolean> {
return handleChatCommandImpl(this, content, channelType, channelId);
}
@@ -5022,88 +4685,7 @@ Is this productive iteration or a stuck loop?`,
}
async handleSessionCommand(content: string, channelType: ChannelType, channelId: string): Promise<void> {
- const channel = this.channels.get(channelType);
- if (!channel) return;
- const bindingId = channelType === 'cli' ? 'current' : channelId;
- const format = (session: { alias: string; shortId: string; title: string }) => `${session.alias} [${session.shortId}] ${session.title}`;
- const transcript = (session: ReturnType<SessionRepository['get']>) => {
- const recent = session.messages
- .filter((message) => message.kind === 'message' && (message.role === 'user' || message.role === 'assistant'))
- .slice(-8);
- if (recent.length === 0) return 'No messages yet.';
- return recent.map((message) => {
- const label = message.role === 'user' ? 'You' : 'Mercury';
- const text = message.content.replace(/\s+/g, ' ').trim();
- return `${label}: ${text.length > 280 ? `${text.slice(0, 277)}...` : text}`;
- }).join('\n');
- };
- const syncCliSession = (session: ReturnType<SessionRepository['get']>) => {
- if (channelType === 'cli' && channel instanceof CLIChannel) {
- channel.setCurrentSession(session);
- }
- };
- try {
- if (content.trim().toLowerCase() === '/sessions') {
- const current = this.sessions.getByBinding(channelType, bindingId);
- const sessions = this.sessions.list();
- await channel.send(sessions.length
- ? sessions.map((session) => `${session.id === current?.id ? '*' : ' '} ${format(session)}`).join('\n')
- : 'No active sessions. Use /session new.', channelId);
- return;
- }
- const argument = content.trim().slice('/session'.length).trim();
- if (argument.toLowerCase() === 'new') {
- const session = this.sessions.create();
- this.sessions.bind(session.id, channelType, bindingId);
- syncCliSession(session);
- await channel.send(`New session: ${format(session)}`, channelId);
- return;
- }
- if (!argument || argument.toLowerCase() === 'current') {
- const current = this.sessions.getByBinding(channelType, bindingId);
- await channel.send(current ? `Current session: ${format(current)}\n\n${transcript(current)}` : 'No current session. Use /session new.', channelId);
- return;
- }
- if (argument.toLowerCase().startsWith('delete ')) {
- const session = this.sessions.resolve(argument.slice('delete '.length).trim());
- const wasCurrent = this.sessions.getByBinding(channelType, bindingId)?.id === session.id;
- const confirmed = await channel.askToContinue(
- `Permanently delete ${format(session)} and all ${session.messages.length} messages everywhere? This cannot be undone.`,
- channelId,
- );
- if (!confirmed) {
- await channel.send('Session deletion cancelled.', channelId);
- return;
- }
- if (this.sessionSyncEnabled) this.sessions.markDeleted(session.id);
- else this.sessions.deletePermanently(session.id);
- const replacement = wasCurrent ? this.sessions.create({ binding: { channelType, externalConversationId: bindingId } }) : null;
- if (replacement) syncCliSession(replacement);
- await channel.send(
- `Deleted session ${session.alias} [${session.shortId}].${this.sessionSyncEnabled ? ' Cloud deletion is queued.' : ''}${replacement ? ` New session: ${format(replacement)}` : ''}`,
- channelId,
- );
- return;
- }
- if (argument.toLowerCase().startsWith('archive ')) {
- const session = this.sessions.archive(argument.slice('archive '.length).trim());
- await channel.send(`Archived: ${format(session)}`, channelId);
- return;
- }
- let session;
- try {
- session = this.sessions.resolve(argument);
- } catch (error) {
- if (channelType !== 'web' || !/^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test(argument)) throw error;
- session = this.sessions.create({ id: argument });
- }
- this.sessions.bind(session.id, channelType, bindingId);
- syncCliSession(session);
- await channel.send(`Switched session: ${format(session)}\n\n${transcript(session)}`, channelId);
- } catch (error) {
- const message = error instanceof SessionResolutionError ? error.message : error instanceof Error ? error.message : String(error);
- await channel.send(message, channelId);
- }
+ return handleSessionCommandImpl(this, content, channelType, channelId);
}
private scheduleSessionTitleGeneration(sessionId: string, providerName: string): void {
@@ -5243,78 +4825,7 @@ Is this productive iteration or a stuck loop?`,
}
async openCliCommandMenu(channel: CLIChannel, channelId: string): Promise<void> {
- const ctx = this.capabilities.getChatCommandContext();
- if (!ctx) return;
-
- await channel.withMenu(async (select) => {
- while (true) {
- const streamLabel = this.telegramStreaming ? 'Disable Telegram Streaming' : 'Enable Telegram Streaming';
- const permLabel = this.capabilities.permissions.isAutoApproveAll() ? 'Switch to Ask Me' : 'Switch to Allow All';
- const action = await select('Mercury Commands', [
- { value: 'status', label: 'Status' },
- { value: 'memory', label: 'Memory' },
- { value: 'permissions', label: permLabel },
- { value: 'telegram', label: 'Telegram' },
- { value: 'tools', label: 'Tools' },
- { value: 'skills', label: 'Skills' },
- { value: 'stream', label: streamLabel },
- { value: 'help', label: 'Help' },
- { value: 'exit', label: 'Exit' },
- ]);
-
- if (action === 'exit') {
- return;
- }
-
- if (action === 'status') {
- await this.handleChatCommand('/status', 'cli', channelId);
- continue;
- }
-
- if (action === 'memory') {
- if (this.userMemory) {
- await this.openCliMemoryMenu(channel, channelId, select);
- } else {
- const cfg = ctx.config();
- if (cfg.memory.secondBrain?.enabled === false) {
- await channel.send('Second brain is disabled in configuration.', channelId);
- } else {
- await channel.send('Second brain dependency issue: SQLite backend (better-sqlite3) is not available.', channelId);
- }
- }
- continue;
- }
-
- if (action === 'permissions') {
- await this.handleChatCommand('/permissions', 'cli', channelId);
- continue;
- }
-
- if (action === 'telegram') {
- await this.openCliTelegramMenu(channel, channelId, select);
- continue;
- }
-
- if (action === 'tools') {
- await this.handleChatCommand('/tools', 'cli', channelId);
- continue;
- }
-
- if (action === 'skills') {
- await this.handleChatCommand('/skills', 'cli', channelId);
- continue;
- }
-
- if (action === 'stream') {
- await this.handleChatCommand('/stream', 'cli', channelId);
- continue;
- }
-
- if (action === 'help') {
- await channel.send(ctx.manual(), channelId);
- }
- }
- });
+ return openCliCommandMenuImpl(this, channel, channelId);
}
async sendMemoryOverview(channel: any, channelId: string): Promise<void> {
@@ -5346,217 +4857,11 @@ Is this productive iteration or a stuck loop?`,
}
async openCliMemoryMenu(channel: CLIChannel, channelId: string, select?: (title: string, options: ArrowSelectOption[]) => Promise<string>): Promise<void> {
- if (!this.userMemory) return;
-
- const runMenu = async (sel: (title: string, options: ArrowSelectOption[]) => Promise<string>) => {
- while (true) {
- const learningLabel = this.userMemory!.isLearningPaused() ? 'Resume Learning' : 'Pause Learning';
- const shareLabel = this.userMemory!.isShareLearning() ? 'Shared Learning: ON' : 'Shared Learning: OFF';
- const action = await sel('Memory', [
- { value: 'overview', label: 'Overview' },
- { value: 'recent', label: 'Recent Memories' },
- { value: 'shared', label: 'Shared Memories' },
- { value: 'search', label: 'Search' },
- { value: 'toggle', label: learningLabel },
- { value: 'share', label: shareLabel },
- { value: 'clear', label: 'Clear All Memories' },
- { value: 'back', label: 'Back' },
- ]);
-
- if (action === 'back') return;
-
- if (action === 'overview') {
- await this.sendMemoryOverview(channel, channelId);
- continue;
- }
-
- if (action === 'recent') {
- const recent = this.userMemory!.getRecent(10);
- if (recent.length === 0) {
- await channel.send('No memories yet.', channelId);
- continue;
- }
- const lines = ['**Recent Memories:**', ''];
- for (const r of recent) {
- const scope = r.scope === 'active' ? '⏳' : 'πŸ“Œ';
- const kind = r.evidenceKind === 'direct' ? 'direct' : r.evidenceKind === 'inferred' ? 'inferred' : r.evidenceKind;
- lines.push(`${scope} [${r.type}] ${r.summary}`);
- lines.push(` Confidence: ${r.confidence.toFixed(2)} | Evidence: ${kind} | Seen: ${r.evidenceCount}x`);
- }
- await channel.send(lines.join('\n'), channelId);
- continue;
- }
-
- if (action === 'shared') {
- const shared = this.userMemory!.getShareable(20);
- if (shared.length === 0) {
- await channel.send('No shared memories yet. Enable shared learning to mark new memories as shareable for cloud fetch.', channelId);
- continue;
- }
- const lines = [`**Shared Memories (${shared.length}):**`, ''];
- for (const r of shared) {
- const scope = r.scope === 'active' ? '⏳' : 'πŸ“Œ';
- const cats = r.categories.length > 0 ? ` {${r.categories.join(', ')}}` : '';
- lines.push(`${scope} [${r.type}]${cats} ${r.summary}`);
- lines.push(` Confidence: ${r.confidence.toFixed(2)} | Evidence: ${r.evidenceKind} | Seen: ${r.evidenceCount}x`);
- }
- await channel.send(lines.join('\n'), channelId);
- continue;
- }
-
- if (action === 'search') {
- const query = await channel.prompt('Search memories: ');
- if (!query) continue;
- const results = this.userMemory!.search(query, 10);
- if (results.length === 0) {
- await channel.send(`No memories found matching "${query}".`, channelId);
- continue;
- }
- const lines = [`**Search results for "${query}":**`, ''];
- for (const r of results) {
- const scope = r.scope === 'active' ? '⏳' : 'πŸ“Œ';
- lines.push(`${scope} [${r.type}] ${r.summary}`);
- lines.push(` Confidence: ${r.confidence.toFixed(2)} | Evidence: ${r.evidenceKind} | Seen: ${r.evidenceCount}x`);
- }
- await channel.send(lines.join('\n'), channelId);
- continue;
- }
-
- if (action === 'toggle') {
- const currentlyPaused = this.userMemory!.isLearningPaused();
- this.userMemory!.setLearningPaused(!currentlyPaused);
- await channel.send(currentlyPaused ? 'Learning resumed. Mercury will remember new things from conversations.' : 'Learning paused. Mercury will not store new memories until resumed.', channelId);
- continue;
- }
-
- if (action === 'share') {
- const currently = this.userMemory!.isShareLearning();
- this.userMemory!.setShareLearning(!currently);
- const cfg = loadConfig();
- if (!cfg.memory.collaborativeKnowledge) cfg.memory.collaborativeKnowledge = {};
- cfg.memory.collaborativeKnowledge.shareLearning = !currently;
- saveConfig(cfg);
- const count = this.userMemory!.countShareable();
- await channel.send(
- currently
- ? `Shared learning disabled. New memories will stay private. (${count} memories already shareable are unchanged.)`
- : `Shared learning enabled. New memories will be marked shareable for cloud fetch. (${count} memories currently shareable.)`,
- channelId,
- );
- continue;
- }
-
- if (action === 'clear') {
- const confirm = await sel('Clear all memories?', [
- { value: 'cancel', label: 'Cancel' },
- { value: 'confirm', label: 'Clear everything' },
- ]);
- if (confirm === 'confirm') {
- const cleared = this.userMemory!.clear();
- await channel.send(`Cleared ${cleared} memories.`, channelId);
- }
- continue;
- }
- }
- };
-
- if (select) {
- await runMenu(select);
- } else {
- await channel.withMenu(runMenu);
- }
+ return openCliMemoryMenuImpl(this, channel, channelId, select);
}
async openMemoryChoiceMenu(channel: any, channelId: string): Promise<void> {
- if (!this.userMemory) return;
-
- const learningLabel = this.userMemory.isLearningPaused() ? 'Resume Learning' : 'Pause Learning';
- const shareLabel = this.userMemory.isShareLearning() ? 'Shared Learning: ON' : 'Shared Learning: OFF';
- const action = await channel.presentChoicePrompt('Memory', [
- { value: 'overview', label: 'Overview' },
- { value: 'recent', label: 'Recent Memories' },
- { value: 'shared', label: 'Shared Memories' },
- { value: 'toggle', label: learningLabel },
- { value: 'share', label: shareLabel },
- { value: 'clear', label: 'Clear All Memories' },
- { value: 'cancel', label: 'Cancel' },
- ], channelId);
-
- if (action === 'cancel') return;
-
- if (action === 'overview') {
- await this.sendMemoryOverview(channel, channelId);
- return;
- }
-
- if (action === 'recent') {
- const recent = this.userMemory.getRecent(10);
- if (recent.length === 0) {
- await channel.send('No memories yet.', channelId);
- return;
- }
- const lines = ['**Recent Memories:**', ''];
- for (const r of recent) {
- const scope = r.scope === 'active' ? '⏳' : 'πŸ“Œ';
- const kind = r.evidenceKind === 'direct' ? 'direct' : r.evidenceKind === 'inferred' ? 'inferred' : r.evidenceKind;
- lines.push(`${scope} [${r.type}] ${r.summary}`);
- lines.push(` Confidence: ${r.confidence.toFixed(2)} | Evidence: ${kind} | Seen: ${r.evidenceCount}x`);
- }
- await channel.send(lines.join('\n'), channelId);
- return;
- }
-
- if (action === 'shared') {
- const shared = this.userMemory.getShareable(20);
- if (shared.length === 0) {
- await channel.send('No shared memories yet. Enable shared learning to mark new memories as shareable for cloud fetch.', channelId);
- return;
- }
- const lines = [`**Shared Memories (${shared.length}):**`, ''];
- for (const r of shared) {
- const scope = r.scope === 'active' ? '⏳' : 'πŸ“Œ';
- const cats = r.categories.length > 0 ? ` {${r.categories.join(', ')}}` : '';
- lines.push(`${scope} [${r.type}]${cats} ${r.summary}`);
- lines.push(` Confidence: ${r.confidence.toFixed(2)} | Evidence: ${r.evidenceKind} | Seen: ${r.evidenceCount}x`);
- }
- await channel.send(lines.join('\n'), channelId);
- return;
- }
-
- if (action === 'toggle') {
- const currentlyPaused = this.userMemory.isLearningPaused();
- this.userMemory.setLearningPaused(!currentlyPaused);
- await channel.send(currentlyPaused ? 'Learning resumed. Mercury will remember new things from conversations.' : 'Learning paused. Mercury will not store new memories until resumed.', channelId);
- return;
- }
-
- if (action === 'share') {
- const currently = this.userMemory.isShareLearning();
- this.userMemory.setShareLearning(!currently);
- const cfg = loadConfig();
- if (!cfg.memory.collaborativeKnowledge) cfg.memory.collaborativeKnowledge = {};
- cfg.memory.collaborativeKnowledge.shareLearning = !currently;
- saveConfig(cfg);
- const count = this.userMemory.countShareable();
- await channel.send(
- currently
- ? `Shared learning disabled. New memories will stay private. (${count} memories already shareable are unchanged.)`
- : `Shared learning enabled. New memories will be marked shareable for cloud fetch. (${count} memories currently shareable.)`,
- channelId,
- );
- return;
- }
-
- if (action === 'clear') {
- const confirm = await channel.presentChoicePrompt('Clear all memories?', [
- { value: 'cancel', label: 'Cancel' },
- { value: 'confirm', label: 'Clear everything' },
- ], channelId);
- if (confirm === 'confirm') {
- const cleared = this.userMemory.clear();
- await channel.send(`Cleared ${cleared} memories.`, channelId);
- }
- }
+ return openMemoryChoiceMenuImpl(this, channel, channelId);
}
async openCliTelegramMenu(
diff --git a/src/core/bg-shell-approval.test.ts b/src/core/bg-shell-approval.test.ts
index 0d77e41c..f622aa07 100644
--- a/src/core/bg-shell-approval.test.ts
+++ b/src/core/bg-shell-approval.test.ts
@@ -3,7 +3,8 @@ import { readFileSync } from 'node:fs';
import { fileURLToPath } from 'node:url';
import { dirname, join } from 'node:path';
-const agentSrc = readFileSync(join(dirname(fileURLToPath(import.meta.url)), 'agent.ts'), 'utf8');
+// /bg lives in core/commands/bg-command.ts since the P2.1 split.
+const agentSrc = readFileSync(join(dirname(fileURLToPath(import.meta.url)), 'commands', 'bg-command.ts'), 'utf8');
/**
* `/bg <command>` is an alternate shell entry point. It used to call
diff --git a/src/core/commands/bg-command.ts b/src/core/commands/bg-command.ts
new file mode 100644
index 00000000..7935c99b
--- /dev/null
+++ b/src/core/commands/bg-command.ts
@@ -0,0 +1,156 @@
+/**
+ * The /bg background-task command.
+ * Moved out of Agent (P2.1 split); `agent` is the Agent instance.
+ */
+import type { Agent } from '../agent.js';
+import type { ChannelMessage, ChannelType } from '../../types/channel.js';
+
+
+export async function handleBgCommand(agent: Agent, trimmed: string, msg: ChannelMessage, channel: any): Promise<void> {
+ const parts = trimmed.trim().split(/\s+/);
+ const sub = parts.length > 1 ? parts[1] : '';
+ const args = parts.slice(1).join(' ');
+
+ if (sub === 'current') {
+ if (!agent.processing || !agent.currentMessage) {
+ await channel.send('No active task to background.', msg.channelId);
+ return;
+ }
+ const taskDescription = agent.currentMessage.content.trim();
+ const sourceChannelId = agent.currentMessage.channelId;
+ const sourceChannelType = agent.currentMessage.channelType as any;
+
+ if (agent.currentAbort) {
+ agent.currentAbortReason = 'backgrounded';
+ agent.currentAbort.abort();
+ }
+
+ if (agent.supervisor) {
+ const agentId = await agent.supervisor.spawn({
+ task: taskDescription,
+ sourceChannelId,
+ sourceChannelType,
+ });
+ const bgId = agent.backgroundTasks.spawnAgent(taskDescription, agent.capabilities.getCwd(), agentId);
+ await channel.send(`πŸ“‹ Active task moved to background as ${bgId}. I'll notify you when it completes.`, msg.channelId);
+ } else {
+ await channel.send('Cannot background: sub-agents not available. The active task has been aborted.', msg.channelId);
+ }
+
+ agent.syncBgTasksToTui();
+ return;
+ }
+
+ if (sub === 'list' || sub === '' || sub === 'ls') {
+ const tasks = agent.backgroundTasks.getAllSummaries();
+ if (tasks.length === 0) {
+ await channel.send('No background tasks.', msg.channelId);
+ return;
+ }
+ const lines = tasks.map((t) => {
+ const icon = t.status === 'running' ? '⏳' : t.status === 'completed' ? 'βœ…' : t.status === 'failed' ? '❌' : t.status === 'timed_out' ? '⏱' : 'β›”';
+ const label = t.command || t.task || t.id;
+ const elapsed = t.runningMs ? ` (${Math.round(t.runningMs / 1000)}s)` : t.completedAt ? ` (${((t.completedAt - t.startedAt) / 1000).toFixed(1)}s)` : '';
+ const short = label.length > 60 ? label.slice(0, 57) + '...' : label;
+ return `${icon} ${t.id}: ${short}${elapsed} β€” ${t.status}`;
+ });
+ await channel.send(`**Background Tasks:**\n${lines.join('\n')}\n\nUse /bg <id> for details, /bg cancel <id> to cancel, /bg clear to prune completed tasks.`, msg.channelId);
+ return;
+ }
+
+ if (sub === 'clear') {
+ const cleared = agent.backgroundTasks.clearCompleted();
+ await channel.send(`Cleared ${cleared} completed task(s).`, msg.channelId);
+ agent.syncBgTasksToTui();
+ return;
+ }
+
+ if (sub === 'cancel' || sub === 'stop' || sub === 'kill') {
+ const taskId = parts[2];
+ if (!taskId) {
+ await channel.send(`Usage: /bg ${sub} <id>`, msg.channelId);
+ return;
+ }
+ const cancelled = agent.backgroundTasks.cancel(taskId);
+ if (cancelled) {
+ await channel.send(`β›” Stopped background task ${taskId}.`, msg.channelId);
+ } else {
+ await channel.send(`Task "${taskId}" not found or not running.`, msg.channelId);
+ }
+ agent.syncBgTasksToTui();
+ return;
+ }
+
+ if (sub === 'killall' || sub === 'stopall') {
+ const count = agent.backgroundTasks.cancelAll();
+ if (count === 0) {
+ await channel.send('No running background tasks to stop.', msg.channelId);
+ } else {
+ await channel.send(`β›” Stopped ${count} background task${count === 1 ? '' : 's'}.`, msg.channelId);
+ }
+ agent.syncBgTasksToTui();
+ return;
+ }
+
+ const specificTask = agent.backgroundTasks.getSummary(sub);
+ if (specificTask) {
+ const task = agent.backgroundTasks.get(sub)!;
+ const label = task.command || task.task || task.id;
+ const elapsed = task.status === 'running'
+ ? `Running for ${Math.round((Date.now() - task.startedAt) / 1000)}s`
+ : task.completedAt
+ ? `Completed in ${((task.completedAt - task.startedAt) / 1000).toFixed(1)}s`
+ : task.status;
+ const output = (task.stdout + '\n' + task.stderr).trim();
+ const preview = output.length > 2000 ? output.slice(-2000) : output;
+ await channel.send(`**${specificTask.id}**: ${label}\nStatus: ${elapsed}\nExit code: ${task.exitCode ?? 'N/A'}\n\n${preview || '(no output)'}`, msg.channelId);
+ return;
+ }
+
+ const colonIdx = trimmed.indexOf(':');
+ if (colonIdx !== -1 && trimmed[colonIdx + 1] === ' ') {
+ const taskDescription = trimmed.slice(colonIdx + 1).trim();
+ if (!taskDescription) {
+ await channel.send('Usage: /bg: <natural language task> or /bg <shell command>', msg.channelId);
+ return;
+ }
+ if (!agent.supervisor) {
+ await channel.send('Sub-agents are not available. Use /bg <command> for shell commands.', msg.channelId);
+ return;
+ }
+ const agentId = await agent.supervisor.spawn({
+ task: taskDescription,
+ sourceChannelId: msg.channelId,
+ sourceChannelType: msg.channelType as any,
+ });
+ const bgId = agent.backgroundTasks.spawnAgent(taskDescription, agent.capabilities.getCwd(), agentId);
+ agent.backgroundTasks.registerComplete(bgId, (task) => {
+ if (task.status === 'running') return;
+ });
+ await channel.send(`πŸ“‹ Background agent ${bgId} started: "${taskDescription.slice(0, 50)}${taskDescription.length > 50 ? '...' : ''}"`, msg.channelId);
+ agent.syncBgTasksToTui();
+ return;
+ }
+
+ const command = args || '';
+ if (!command) {
+ await channel.send('Usage:\nβ€’ /bg <command> β€” run a shell command in the background\nβ€’ /bg: <task> β€” delegate an LLM task to the background\nβ€’ /bg current β€” move the active task to the background\nβ€’ /bg list β€” show all background tasks\nβ€’ /bg <id> β€” show task details\nβ€’ /bg stop <id> β€” stop a running task\nβ€’ /bg killall β€” stop all running tasks\nβ€’ /bg clear β€” prune completed tasks', msg.channelId);
+ return;
+ }
+
+ // `/bg <command>` is an alternate shell entry point: it must honor the
+ // same approval boundary as the run_command tool, otherwise an
+ // authenticated chat user can bypass Ask-Me approval and run arbitrary
+ // commands. checkShellCommand() also enforces the blocked list and any
+ // cwd/scope containment rules.
+ const check = await agent.capabilities.permissions.checkShellCommand(command);
+ if (!check.allowed) {
+ await channel.send(`β›” Not running in background: ${check.reason ?? 'command requires approval'}\nApprove the command first (or run it without /bg) and try again.`, msg.channelId);
+ return;
+ }
+
+ const cwd = agent.capabilities.getCwd();
+ const bgId = agent.backgroundTasks.spawnShell(command, cwd);
+ await channel.send(`πŸ“‹ Background task ${bgId} started: "${command.slice(0, 50)}${command.length > 50 ? '...' : ''}"`, msg.channelId);
+ agent.syncBgTasksToTui();
+}
diff --git a/src/core/commands/cli-command-menu.ts b/src/core/commands/cli-command-menu.ts
new file mode 100644
index 00000000..ffab51cc
--- /dev/null
+++ b/src/core/commands/cli-command-menu.ts
@@ -0,0 +1,83 @@
+/**
+ * The arrow-key /menu command picker on the CLI.
+ * Moved out of Agent (P2.1 split); `agent` is the Agent instance.
+ */
+import type { Agent } from '../agent.js';
+import { CLIChannel } from '../../channels/cli.js';
+import { handleChatCommand as handleChatCommandImpl } from '../commands/chat-command.js';
+import { openCliMemoryMenu as openCliMemoryMenuImpl } from '../commands/cli-memory-menu.js';
+
+export async function openCliCommandMenu(agent: Agent, channel: CLIChannel, channelId: string): Promise<void> {
+ const ctx = agent.capabilities.getChatCommandContext();
+ if (!ctx) return;
+
+ await channel.withMenu(async (select) => {
+ while (true) {
+ const streamLabel = agent.telegramStreaming ? 'Disable Telegram Streaming' : 'Enable Telegram Streaming';
+ const permLabel = agent.capabilities.permissions.isAutoApproveAll() ? 'Switch to Ask Me' : 'Switch to Allow All';
+ const action = await select('Mercury Commands', [
+ { value: 'status', label: 'Status' },
+ { value: 'memory', label: 'Memory' },
+ { value: 'permissions', label: permLabel },
+ { value: 'telegram', label: 'Telegram' },
+ { value: 'tools', label: 'Tools' },
+ { value: 'skills', label: 'Skills' },
+ { value: 'stream', label: streamLabel },
+ { value: 'help', label: 'Help' },
+ { value: 'exit', label: 'Exit' },
+ ]);
+
+ if (action === 'exit') {
+ return;
+ }
+
+ if (action === 'status') {
+ await agent.handleChatCommand('/status', 'cli', channelId);
+ continue;
+ }
+
+ if (action === 'memory') {
+ if (agent.userMemory) {
+ await agent.openCliMemoryMenu(channel, channelId, select);
+ } else {
+ const cfg = ctx.config();
+ if (cfg.memory.secondBrain?.enabled === false) {
+ await channel.send('Second brain is disabled in configuration.', channelId);
+ } else {
+ await channel.send('Second brain dependency issue: SQLite backend (better-sqlite3) is not available.', channelId);
+ }
+ }
+ continue;
+ }
+
+ if (action === 'permissions') {
+ await agent.handleChatCommand('/permissions', 'cli', channelId);
+ continue;
+ }
+
+ if (action === 'telegram') {
+ await agent.openCliTelegramMenu(channel, channelId, select);
+ continue;
+ }
+
+ if (action === 'tools') {
+ await agent.handleChatCommand('/tools', 'cli', channelId);
+ continue;
+ }
+
+ if (action === 'skills') {
+ await agent.handleChatCommand('/skills', 'cli', channelId);
+ continue;
+ }
+
+ if (action === 'stream') {
+ await agent.handleChatCommand('/stream', 'cli', channelId);
+ continue;
+ }
+
+ if (action === 'help') {
+ await channel.send(ctx.manual(), channelId);
+ }
+ }
+ });
+}
diff --git a/src/core/commands/cli-memory-menu.ts b/src/core/commands/cli-memory-menu.ts
new file mode 100644
index 00000000..db8dfac5
--- /dev/null
+++ b/src/core/commands/cli-memory-menu.ts
@@ -0,0 +1,160 @@
+/**
+ * The interactive /memory menu on the CLI.
+ * Moved out of Agent (P2.1 split); `agent` is the Agent instance.
+ */
+import type { Agent } from '../agent.js';
+import { CLIChannel } from '../../channels/cli.js';
+import {
+ approveTelegramPendingRequest,
+ approveTelegramPendingRequestByPairingCode,
+ clearTelegramAccess,
+ demoteTelegramAdmin,
+ getTelegramAccessSummary,
+ getTelegramApprovedUsers,
+ getTelegramPendingRequests,
+ promoteTelegramUserToAdmin,
+ rejectTelegramPendingRequest,
+ removeTelegramUser,
+ saveConfig,
+ loadConfig,
+ getActiveProviders,
+ getDiscordAccessSummary,
+ hasDiscordAdmins,
+ findDiscordPendingRequest,
+ approveDiscordPendingRequest,
+ approveDiscordPendingRequestByPairingCode,
+ rejectDiscordPendingRequest as rejectDiscordPendingRequestConfig,
+ removeDiscordUser,
+ clearDiscordAccess,
+ getSlackAccessSummary,
+ hasSlackAdmins,
+ findSlackPendingRequest,
+ approveSlackPendingRequest,
+ approveSlackPendingRequestByPairingCode,
+ rejectSlackPendingRequest as rejectSlackPendingRequestConfig,
+ removeSlackUser,
+ clearSlackAccess,
+} from '../../utils/config.js';
+import type { ArrowSelectOption } from '../../utils/arrow-select.js';
+
+export async function openCliMemoryMenu(agent: Agent, channel: CLIChannel, channelId: string, select?: (title: string, options: ArrowSelectOption[]) => Promise<string>): Promise<void> {
+ if (!agent.userMemory) return;
+
+ const runMenu = async (sel: (title: string, options: ArrowSelectOption[]) => Promise<string>) => {
+ while (true) {
+ const learningLabel = agent.userMemory!.isLearningPaused() ? 'Resume Learning' : 'Pause Learning';
+ const shareLabel = agent.userMemory!.isShareLearning() ? 'Shared Learning: ON' : 'Shared Learning: OFF';
+ const action = await sel('Memory', [
+ { value: 'overview', label: 'Overview' },
+ { value: 'recent', label: 'Recent Memories' },
+ { value: 'shared', label: 'Shared Memories' },
+ { value: 'search', label: 'Search' },
+ { value: 'toggle', label: learningLabel },
+ { value: 'share', label: shareLabel },
+ { value: 'clear', label: 'Clear All Memories' },
+ { value: 'back', label: 'Back' },
+ ]);
+
+ if (action === 'back') return;
+
+ if (action === 'overview') {
+ await agent.sendMemoryOverview(channel, channelId);
+ continue;
+ }
+
+ if (action === 'recent') {
+ const recent = agent.userMemory!.getRecent(10);
+ if (recent.length === 0) {
+ await channel.send('No memories yet.', channelId);
+ continue;
+ }
+ const lines = ['**Recent Memories:**', ''];
+ for (const r of recent) {
+ const scope = r.scope === 'active' ? '⏳' : 'πŸ“Œ';
+ const kind = r.evidenceKind === 'direct' ? 'direct' : r.evidenceKind === 'inferred' ? 'inferred' : r.evidenceKind;
+ lines.push(`${scope} [${r.type}] ${r.summary}`);
+ lines.push(` Confidence: ${r.confidence.toFixed(2)} | Evidence: ${kind} | Seen: ${r.evidenceCount}x`);
+ }
+ await channel.send(lines.join('\n'), channelId);
+ continue;
+ }
+
+ if (action === 'shared') {
+ const shared = agent.userMemory!.getShareable(20);
+ if (shared.length === 0) {
+ await channel.send('No shared memories yet. Enable shared learning to mark new memories as shareable for cloud fetch.', channelId);
+ continue;
+ }
+ const lines = [`**Shared Memories (${shared.length}):**`, ''];
+ for (const r of shared) {
+ const scope = r.scope === 'active' ? '⏳' : 'πŸ“Œ';
+ const cats = r.categories.length > 0 ? ` {${r.categories.join(', ')}}` : '';
+ lines.push(`${scope} [${r.type}]${cats} ${r.summary}`);
+ lines.push(` Confidence: ${r.confidence.toFixed(2)} | Evidence: ${r.evidenceKind} | Seen: ${r.evidenceCount}x`);
+ }
+ await channel.send(lines.join('\n'), channelId);
+ continue;
+ }
+
+ if (action === 'search') {
+ const query = await channel.prompt('Search memories: ');
+ if (!query) continue;
+ const results = agent.userMemory!.search(query, 10);
+ if (results.length === 0) {
+ await channel.send(`No memories found matching "${query}".`, channelId);
+ continue;
+ }
+ const lines = [`**Search results for "${query}":**`, ''];
+ for (const r of results) {
+ const scope = r.scope === 'active' ? '⏳' : 'πŸ“Œ';
+ lines.push(`${scope} [${r.type}] ${r.summary}`);
+ lines.push(` Confidence: ${r.confidence.toFixed(2)} | Evidence: ${r.evidenceKind} | Seen: ${r.evidenceCount}x`);
+ }
+ await channel.send(lines.join('\n'), channelId);
+ continue;
+ }
+
+ if (action === 'toggle') {
+ const currentlyPaused = agent.userMemory!.isLearningPaused();
+ agent.userMemory!.setLearningPaused(!currentlyPaused);
+ await channel.send(currentlyPaused ? 'Learning resumed. Mercury will remember new things from conversations.' : 'Learning paused. Mercury will not store new memories until resumed.', channelId);
+ continue;
+ }
+
+ if (action === 'share') {
+ const currently = agent.userMemory!.isShareLearning();
+ agent.userMemory!.setShareLearning(!currently);
+ const cfg = loadConfig();
+ if (!cfg.memory.collaborativeKnowledge) cfg.memory.collaborativeKnowledge = {};
+ cfg.memory.collaborativeKnowledge.shareLearning = !currently;
+ saveConfig(cfg);
+ const count = agent.userMemory!.countShareable();
+ await channel.send(
+ currently
+ ? `Shared learning disabled. New memories will stay private. (${count} memories already shareable are unchanged.)`
+ : `Shared learning enabled. New memories will be marked shareable for cloud fetch. (${count} memories currently shareable.)`,
+ channelId,
+ );
+ continue;
+ }
+
+ if (action === 'clear') {
+ const confirm = await sel('Clear all memories?', [
+ { value: 'cancel', label: 'Cancel' },
+ { value: 'confirm', label: 'Clear everything' },
+ ]);
+ if (confirm === 'confirm') {
+ const cleared = agent.userMemory!.clear();
+ await channel.send(`Cleared ${cleared} memories.`, channelId);
+ }
+ continue;
+ }
+ }
+ };
+
+ if (select) {
+ await runMenu(select);
+ } else {
+ await channel.withMenu(runMenu);
+ }
+}
diff --git a/src/core/commands/fast-path-command.ts b/src/core/commands/fast-path-command.ts
new file mode 100644
index 00000000..452bb55c
--- /dev/null
+++ b/src/core/commands/fast-path-command.ts
@@ -0,0 +1,108 @@
+/**
+ * Fast-path commands answered without a model call.
+ * Moved out of Agent (P2.1 split); `agent` is the Agent instance.
+ */
+import type { Agent } from '../agent.js';
+import { MAX_STEPS } from '../agent.js';
+import type { ChannelMessage, ChannelType } from '../../types/channel.js';
+import path from 'node:path';
+import { formatToolStep, formatNarrative, type NarrativeStep } from '../../utils/tool-label.js';
+import { handleBotsCommand as handleBotsCommandImpl } from '../commands/bots-command.js';
+import { handleBgCommand as handleBgCommandImpl } from '../commands/bg-command.js';
+import { handleSessionCommand as handleSessionCommandImpl } from '../commands/session-command.js';
+
+export async function handleFastPathCommand(agent: Agent, msg: ChannelMessage): Promise<void> {
+ const trimmed = msg.content.trim();
+ const channel = agent.channels.getChannelForMessage(msg);
+ if (!channel) return;
+
+ const activeAgents = agent.supervisor ? agent.supervisor.getActiveAgents() : [];
+ const hasActiveAgents = activeAgents.length > 0;
+ const busyPrefix = hasActiveAgents ? '' : '';
+
+ if (trimmed === '/sessions' || trimmed.startsWith('/session')) {
+ await agent.handleSessionCommand(trimmed, msg.channelType, msg.channelId);
+ return;
+ }
+
+ if (trimmed === '/agents' || trimmed === '/status') {
+ if (agent.supervisor) {
+ const agents = agent.supervisor.getActiveAgents();
+ if (agents.length === 0) {
+ await channel.send('No active sub-agents.', msg.channelId);
+ } else {
+ let text = '**Sub-Agents:**\n\n';
+ for (const a of agents) {
+ const icon = a.status === 'running' ? 'πŸ”„' : a.status === 'pending' ? '⏳' : a.status === 'completed' ? 'βœ…' : '❌';
+ text += `${icon} **${a.id}**: ${a.task.slice(0, 60)}${a.task.length > 60 ? '...' : ''} β€” ${a.status}${a.progress ? ` (${a.progress})` : ''}\n`;
+ }
+ await channel.send(text, msg.channelId);
+ }
+ } else {
+ await channel.send('Sub-agents not enabled.', msg.channelId);
+ }
+ return;
+ }
+
+ if (trimmed === '/halt' || trimmed === '/stop') {
+ await channel.send(await agent.stopAllWork(trimmed === '/stop' ? 'stopped' : 'halted'), msg.channelId);
+ return;
+ }
+
+ // Bots run outside the main queue β€” /bots commands are always fast-path.
+ if (trimmed.startsWith('/bots')) {
+ await agent.handleBotsCommand(trimmed, msg, channel);
+ return;
+ }
+
+ if (trimmed.startsWith('/bg')) {
+ await agent.handleBgCommand(trimmed, msg, channel);
+ return;
+ }
+
+ if (trimmed === '/progress' || trimmed === '/still') {
+ if (!agent.processing || !agent.currentMessage) {
+ await channel.send('No active foreground task.', msg.channelId);
+ return;
+ }
+ const elapsedSec = Math.round((Date.now() - agent.currentMessage.timestamp) / 1000);
+ const stepInfo = agent.completedStepCount > 0 ? ` Β· step ${agent.completedStepCount}/${MAX_STEPS}` : '';
+ const narrative = formatNarrative(agent.stepNarrative, agent.currentActivity, 10);
+ const narrativeBlock = narrative ? `\n${narrative}` : '';
+ await channel.send(
+ `⏳ Task in progress (${elapsedSec}s${stepInfo})${narrativeBlock}\nUse /bg current to move it to background.`,
+ msg.channelId,
+ );
+ return;
+ }
+
+ if (trimmed === '/help') {
+ await channel.send('Agent is busy. Available: /sessions, /session, /agents, /halt, /stop, /progress, /spotify, /code, /research, /memory, /bg', msg.channelId);
+ return;
+ }
+
+ if (trimmed.startsWith('/spotify')) {
+ await agent.handleFastPathSpotify(trimmed, msg, channel);
+ return;
+ }
+
+ if (trimmed.startsWith('/code')) {
+ await agent.handleFastPathCode(trimmed, msg, channel);
+ return;
+ }
+
+ if (trimmed === '/memory') {
+ await channel.send('Agent is busy. Memory management will be available after current task completes.', msg.channelId);
+ return;
+ }
+
+ if (hasActiveAgents) {
+ const agentList = activeAgents.map(a => `**${a.id}**: ${a.task.slice(0, 40)}`).join(', ');
+ await channel.send(`I'm busy working on sub-agent tasks (${agentList}). Your message has been queued β€” I'll respond once I'm free. Use /agents to check status.`, msg.channelId);
+ } else {
+ const elapsedSec = agent.currentMessage ? Math.round((Date.now() - agent.currentMessage.timestamp) / 1000) : 0;
+ await channel.send(`I'm busy processing${elapsedSec > 0 ? ` (${elapsedSec}s elapsed)` : ''}. Use /progress for live status or /bg current to move this task to the background.`, msg.channelId);
+ }
+
+ agent.queueMessage(msg);
+}
diff --git a/src/core/commands/memory-choice-menu.ts b/src/core/commands/memory-choice-menu.ts
new file mode 100644
index 00000000..4a34aca9
--- /dev/null
+++ b/src/core/commands/memory-choice-menu.ts
@@ -0,0 +1,129 @@
+/**
+ * Memory choice picker shared by the CLI menus.
+ * Moved out of Agent (P2.1 split); `agent` is the Agent instance.
+ */
+import type { Agent } from '../agent.js';
+import {
+ approveTelegramPendingRequest,
+ approveTelegramPendingRequestByPairingCode,
+ clearTelegramAccess,
+ demoteTelegramAdmin,
+ getTelegramAccessSummary,
+ getTelegramApprovedUsers,
+ getTelegramPendingRequests,
+ promoteTelegramUserToAdmin,
+ rejectTelegramPendingRequest,
+ removeTelegramUser,
+ saveConfig,
+ loadConfig,
+ getActiveProviders,
+ getDiscordAccessSummary,
+ hasDiscordAdmins,
+ findDiscordPendingRequest,
+ approveDiscordPendingRequest,
+ approveDiscordPendingRequestByPairingCode,
+ rejectDiscordPendingRequest as rejectDiscordPendingRequestConfig,
+ removeDiscordUser,
+ clearDiscordAccess,
+ getSlackAccessSummary,
+ hasSlackAdmins,
+ findSlackPendingRequest,
+ approveSlackPendingRequest,
+ approveSlackPendingRequestByPairingCode,
+ rejectSlackPendingRequest as rejectSlackPendingRequestConfig,
+ removeSlackUser,
+ clearSlackAccess,
+} from '../../utils/config.js';
+
+
+export async function openMemoryChoiceMenu(agent: Agent, channel: any, channelId: string): Promise<void> {
+ if (!agent.userMemory) return;
+
+ const learningLabel = agent.userMemory.isLearningPaused() ? 'Resume Learning' : 'Pause Learning';
+ const shareLabel = agent.userMemory.isShareLearning() ? 'Shared Learning: ON' : 'Shared Learning: OFF';
+ const action = await channel.presentChoicePrompt('Memory', [
+ { value: 'overview', label: 'Overview' },
+ { value: 'recent', label: 'Recent Memories' },
+ { value: 'shared', label: 'Shared Memories' },
+ { value: 'toggle', label: learningLabel },
+ { value: 'share', label: shareLabel },
+ { value: 'clear', label: 'Clear All Memories' },
+ { value: 'cancel', label: 'Cancel' },
+ ], channelId);
+
+ if (action === 'cancel') return;
+
+ if (action === 'overview') {
+ await agent.sendMemoryOverview(channel, channelId);
+ return;
+ }
+
+ if (action === 'recent') {
+ const recent = agent.userMemory.getRecent(10);
+ if (recent.length === 0) {
+ await channel.send('No memories yet.', channelId);
+ return;
+ }
+ const lines = ['**Recent Memories:**', ''];
+ for (const r of recent) {
+ const scope = r.scope === 'active' ? '⏳' : 'πŸ“Œ';
+ const kind = r.evidenceKind === 'direct' ? 'direct' : r.evidenceKind === 'inferred' ? 'inferred' : r.evidenceKind;
+ lines.push(`${scope} [${r.type}] ${r.summary}`);
+ lines.push(` Confidence: ${r.confidence.toFixed(2)} | Evidence: ${kind} | Seen: ${r.evidenceCount}x`);
+ }
+ await channel.send(lines.join('\n'), channelId);
+ return;
+ }
+
+ if (action === 'shared') {
+ const shared = agent.userMemory.getShareable(20);
+ if (shared.length === 0) {
+ await channel.send('No shared memories yet. Enable shared learning to mark new memories as shareable for cloud fetch.', channelId);
+ return;
+ }
+ const lines = [`**Shared Memories (${shared.length}):**`, ''];
+ for (const r of shared) {
+ const scope = r.scope === 'active' ? '⏳' : 'πŸ“Œ';
+ const cats = r.categories.length > 0 ? ` {${r.categories.join(', ')}}` : '';
+ lines.push(`${scope} [${r.type}]${cats} ${r.summary}`);
+ lines.push(` Confidence: ${r.confidence.toFixed(2)} | Evidence: ${r.evidenceKind} | Seen: ${r.evidenceCount}x`);
+ }
+ await channel.send(lines.join('\n'), channelId);
+ return;
+ }
+
+ if (action === 'toggle') {
+ const currentlyPaused = agent.userMemory.isLearningPaused();
+ agent.userMemory.setLearningPaused(!currentlyPaused);
+ await channel.send(currentlyPaused ? 'Learning resumed. Mercury will remember new things from conversations.' : 'Learning paused. Mercury will not store new memories until resumed.', channelId);
+ return;
+ }
+
+ if (action === 'share') {
+ const currently = agent.userMemory.isShareLearning();
+ agent.userMemory.setShareLearning(!currently);
+ const cfg = loadConfig();
+ if (!cfg.memory.collaborativeKnowledge) cfg.memory.collaborativeKnowledge = {};
+ cfg.memory.collaborativeKnowledge.shareLearning = !currently;
+ saveConfig(cfg);
+ const count = agent.userMemory.countShareable();
+ await channel.send(
+ currently
+ ? `Shared learning disabled. New memories will stay private. (${count} memories already shareable are unchanged.)`
+ : `Shared learning enabled. New memories will be marked shareable for cloud fetch. (${count} memories currently shareable.)`,
+ channelId,
+ );
+ return;
+ }
+
+ if (action === 'clear') {
+ const confirm = await channel.presentChoicePrompt('Clear all memories?', [
+ { value: 'cancel', label: 'Cancel' },
+ { value: 'confirm', label: 'Clear everything' },
+ ], channelId);
+ if (confirm === 'confirm') {
+ const cleared = agent.userMemory.clear();
+ await channel.send(`Cleared ${cleared} memories.`, channelId);
+ }
+ }
+}
diff --git a/src/core/commands/saver-command.ts b/src/core/commands/saver-command.ts
new file mode 100644
index 00000000..972d4380
--- /dev/null
+++ b/src/core/commands/saver-command.ts
@@ -0,0 +1,117 @@
+/**
+ * The /saver token-saver command.
+ * Moved out of Agent (P2.1 split); `agent` is the Agent instance.
+ */
+import type { Agent } from '../agent.js';
+
+
+export async function handleSaverCommand(agent: Agent, subcommand: string, channelType: string, channelId: string): Promise<void> {
+ const channel = agent.channels.get(channelType as any);
+ if (!channel) return;
+
+ const parts = subcommand.trim().split(/\s+/).filter(Boolean);
+ const action = (parts[0] || '').toLowerCase();
+ const arg = (parts[1] || '').toLowerCase();
+
+ const showStatus = async () => {
+ const text = agent.saverMode.getStatusText(
+ agent.tokenBudget.getSavedLifetime(),
+ agent.tokenBudget.getSavedToday(),
+ );
+ const usagePct = Math.round(agent.tokenBudget.getUsagePercentage());
+ await channel.send(`${text}\nCurrent daily usage: ${usagePct}%`, channelId);
+ agent.syncSaverToCli();
+ };
+
+ if (!action || action === 'status' || action === 'stats') {
+ await showStatus();
+ return;
+ }
+
+ if (action === 'on' || action === 'enable') {
+ agent.saverMode.enable();
+ await channel.send(
+ '⚑ Token Saver Mode enabled. Responses will be terser, step limits lower, and history window shorter to conserve tokens.',
+ channelId,
+ );
+ agent.syncSaverToCli();
+ return;
+ }
+
+ if (action === 'off' || action === 'disable') {
+ agent.saverMode.disable();
+ await channel.send('Token Saver Mode disabled. Normal response settings restored.', channelId);
+ agent.syncSaverToCli();
+ return;
+ }
+
+ if (action === 'toggle') {
+ const next = agent.saverMode.toggle();
+ await channel.send(
+ next === 'on'
+ ? '⚑ Token Saver Mode enabled.'
+ : 'Token Saver Mode disabled.',
+ channelId,
+ );
+ agent.syncSaverToCli();
+ return;
+ }
+
+ if (action === 'threshold') {
+ const n = parseInt(parts[1], 10);
+ if (isNaN(n) || n < 0 || n > 100) {
+ await channel.send('Usage: /saver threshold <0-100> β€” percentage of daily budget at which saver auto-engages. Set 0 to disable.', channelId);
+ return;
+ }
+ agent.saverMode.setAutoThreshold(n);
+ await channel.send(
+ n === 0
+ ? 'Saver auto-engage disabled (threshold set to 0).'
+ : `Saver auto-engage threshold set to ${n}% of daily budget.`,
+ channelId,
+ );
+ return;
+ }
+
+ if (action === 'auto') {
+ if (arg === 'on' || arg === 'enable') {
+ agent.saverMode.setAutoEnabled(true);
+ await channel.send(`Saver auto-engage enabled (at ${agent.saverMode.getAutoThreshold()}% usage).`, channelId);
+ } else if (arg === 'off' || arg === 'disable') {
+ agent.saverMode.setAutoEnabled(false);
+ await channel.send('Saver auto-engage disabled. Saver will only activate when you run /saver on.', channelId);
+ agent.syncSaverToCli();
+ } else {
+ await channel.send(
+ `Saver auto-engage is currently ${agent.saverMode.isAutoEnabled() ? 'ON' : 'OFF'} (threshold: ${agent.saverMode.getAutoThreshold()}%).\nUse /saver auto on|off to change.`,
+ channelId,
+ );
+ }
+ return;
+ }
+
+ if (action === 'routing') {
+ if (arg === 'on' || arg === 'enable') {
+ agent.saverMode.setRoutingEnabled(true);
+ await channel.send('Saver cheap-provider routing enabled (when saver is active, cheaper providers will be preferred).', channelId);
+ } else if (arg === 'off' || arg === 'disable') {
+ agent.saverMode.setRoutingEnabled(false);
+ await channel.send('Saver cheap-provider routing disabled.', channelId);
+ } else {
+ await channel.send(`Saver cheap-provider routing is currently ${agent.saverMode.isRoutingEnabled() ? 'ON' : 'OFF'}.\nUse /saver routing on|off to change.`, channelId);
+ }
+ return;
+ }
+
+ await channel.send(
+ 'Unknown saver command. Available:\n' +
+ ' /saver β€” show status and savings\n' +
+ ' /saver on β€” manually enable\n' +
+ ' /saver off β€” disable\n' +
+ ' /saver toggle β€” flip on/off\n' +
+ ' /saver threshold <0-100> β€” auto-engage threshold (default 75)\n' +
+ ' /saver auto on|off β€” enable/disable auto-engagement\n' +
+ ' /saver routing on|off β€” prefer cheap providers while active (opt-in)',
+ channelId,
+ );
+}
diff --git a/src/core/commands/session-command.ts b/src/core/commands/session-command.ts
new file mode 100644
index 00000000..adef6cc2
--- /dev/null
+++ b/src/core/commands/session-command.ts
@@ -0,0 +1,93 @@
+/**
+ * The /session and /sessions commands (list, new, switch, archive, delete).
+ * Moved out of Agent (P2.1 split); `agent` is the Agent instance.
+ */
+import type { Agent } from '../agent.js';
+import type { ChannelMessage, ChannelType } from '../../types/channel.js';
+import { CLIChannel } from '../../channels/cli.js';
+import { normalizeGeneratedSessionTitle, SessionResolutionError, type SessionRepository } from '../../sessions/index.js';
+
+export async function handleSessionCommand(agent: Agent, content: string, channelType: ChannelType, channelId: string): Promise<void> {
+ const channel = agent.channels.get(channelType);
+ if (!channel) return;
+ const bindingId = channelType === 'cli' ? 'current' : channelId;
+ const format = (session: { alias: string; shortId: string; title: string }) => `${session.alias} [${session.shortId}] ${session.title}`;
+ const transcript = (session: ReturnType<SessionRepository['get']>) => {
+ const recent = session.messages
+ .filter((message) => message.kind === 'message' && (message.role === 'user' || message.role === 'assistant'))
+ .slice(-8);
+ if (recent.length === 0) return 'No messages yet.';
+ return recent.map((message) => {
+ const label = message.role === 'user' ? 'You' : 'Mercury';
+ const text = message.content.replace(/\s+/g, ' ').trim();
+ return `${label}: ${text.length > 280 ? `${text.slice(0, 277)}...` : text}`;
+ }).join('\n');
+ };
+ const syncCliSession = (session: ReturnType<SessionRepository['get']>) => {
+ if (channelType === 'cli' && channel instanceof CLIChannel) {
+ channel.setCurrentSession(session);
+ }
+ };
+ try {
+ if (content.trim().toLowerCase() === '/sessions') {
+ const current = agent.sessions.getByBinding(channelType, bindingId);
+ const sessions = agent.sessions.list();
+ await channel.send(sessions.length
+ ? sessions.map((session) => `${session.id === current?.id ? '*' : ' '} ${format(session)}`).join('\n')
+ : 'No active sessions. Use /session new.', channelId);
+ return;
+ }
+ const argument = content.trim().slice('/session'.length).trim();
+ if (argument.toLowerCase() === 'new') {
+ const session = agent.sessions.create();
+ agent.sessions.bind(session.id, channelType, bindingId);
+ syncCliSession(session);
+ await channel.send(`New session: ${format(session)}`, channelId);
+ return;
+ }
+ if (!argument || argument.toLowerCase() === 'current') {
+ const current = agent.sessions.getByBinding(channelType, bindingId);
+ await channel.send(current ? `Current session: ${format(current)}\n\n${transcript(current)}` : 'No current session. Use /session new.', channelId);
+ return;
+ }
+ if (argument.toLowerCase().startsWith('delete ')) {
+ const session = agent.sessions.resolve(argument.slice('delete '.length).trim());
+ const wasCurrent = agent.sessions.getByBinding(channelType, bindingId)?.id === session.id;
+ const confirmed = await channel.askToContinue(
+ `Permanently delete ${format(session)} and all ${session.messages.length} messages everywhere? This cannot be undone.`,
+ channelId,
+ );
+ if (!confirmed) {
+ await channel.send('Session deletion cancelled.', channelId);
+ return;
+ }
+ if (agent.sessionSyncEnabled) agent.sessions.markDeleted(session.id);
+ else agent.sessions.deletePermanently(session.id);
+ const replacement = wasCurrent ? agent.sessions.create({ binding: { channelType, externalConversationId: bindingId } }) : null;
+ if (replacement) syncCliSession(replacement);
+ await channel.send(
+ `Deleted session ${session.alias} [${session.shortId}].${agent.sessionSyncEnabled ? ' Cloud deletion is queued.' : ''}${replacement ? ` New session: ${format(replacement)}` : ''}`,
+ channelId,
+ );
+ return;
+ }
+ if (argument.toLowerCase().startsWith('archive ')) {
+ const session = agent.sessions.archive(argument.slice('archive '.length).trim());
+ await channel.send(`Archived: ${format(session)}`, channelId);
+ return;
+ }
+ let session;
+ try {
+ session = agent.sessions.resolve(argument);
+ } catch (error) {
+ if (channelType !== 'web' || !/^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test(argument)) throw error;
+ session = agent.sessions.create({ id: argument });
+ }
+ agent.sessions.bind(session.id, channelType, bindingId);
+ syncCliSession(session);
+ await channel.send(`Switched session: ${format(session)}\n\n${transcript(session)}`, channelId);
+ } catch (error) {
+ const message = error instanceof SessionResolutionError ? error.message : error instanceof Error ? error.message : String(error);
+ await channel.send(message, channelId);
+ }
+}