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: implement formatted tool execution feedback in CLI with spinner and improved output rendering

Salman Qureshi26f24cfef977
diff --git a/src/channels/cli.ts b/src/channels/cli.ts
index 94918e1c..5a796ace 100644
--- a/src/channels/cli.ts
+++ b/src/channels/cli.ts
@@ -6,6 +6,7 @@ import type { ChannelMessage } from '../types/channel.js';
import { BaseChannel } from './base.js';
import { logger } from '../utils/logger.js';
import { renderMarkdown } from '../utils/markdown.js';
+import { formatToolStep } from '../utils/tool-label.js';
import {
ArrowSelectCancelledError,
selectWithArrowKeys,
@@ -19,6 +20,7 @@ export class CLIChannel extends BaseChannel {
private menuDepth = 0;
private menuAbortController: AbortController | null = null;
private outputInProgress = 0;
+ private streamActive = false;
constructor(agentName: string = 'Mercury') {
super();
@@ -40,7 +42,7 @@ export class CLIChannel extends BaseChannel {
this.rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
- prompt: ' You: ',
+ prompt: '',
});
this.rl.on('line', (line) => {
@@ -73,12 +75,12 @@ export class CLIChannel extends BaseChannel {
this.beginOutput();
const timeStr = elapsedMs != null ? chalk.dim(` (${(elapsedMs / 1000).toFixed(1)}s)`) : '';
const rendered = renderMarkdown(content);
- console.log('');
- console.log(chalk.cyan(` ${this.agentName}:`) + timeStr);
const indented = rendered
.split('\n')
.map((line: string) => ` ${line}`)
.join('\n');
+ console.log('');
+ console.log(chalk.cyan(` ${this.agentName}:`) + timeStr);
console.log(indented);
console.log('');
this.endOutput();
@@ -107,9 +109,19 @@ export class CLIChannel extends BaseChannel {
this.endOutput();
}
+ async sendToolFeedback(toolName: string, args: Record<string, any>): Promise<void> {
+ const label = formatToolStep(toolName, args);
+ if (this.streamActive) {
+ process.stdout.write(chalk.dim(`\n ${label}\n`));
+ } else {
+ console.log(chalk.dim(` ${label}`));
+ }
+ }
+
async stream(content: AsyncIterable<string>, _targetId?: string): Promise<string> {
this.closeActiveMenu();
this.beginOutput();
+ this.streamActive = true;
if (!process.stdout.isTTY) {
process.stdout.write(chalk.cyan(` ${this.agentName}: `));
@@ -118,43 +130,77 @@ export class CLIChannel extends BaseChannel {
process.stdout.write(chunk);
full += chunk;
}
+ this.streamActive = false;
console.log('\n');
this.endOutput();
return full;
}
- console.log('');
- process.stdout.write(chalk.cyan(` ${this.agentName}: `));
- const prefixLines = 2;
+ const spinnerFrames = ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏'];
+ let frameIdx = 0;
+ let spinnerInterval: NodeJS.Timeout | null = null;
+ let hasContent = false;
+
+ process.stdout.write(chalk.cyan(` ${this.agentName}:`));
+
+ spinnerInterval = setInterval(() => {
+ if (!hasContent) {
+ process.stdout.write(`\r${chalk.cyan(` ${this.agentName}:`)} ${chalk.dim(spinnerFrames[frameIdx % spinnerFrames.length])} `);
+ frameIdx++;
+ }
+ }, 80);
let full = '';
for await (const chunk of content) {
+ if (!hasContent) {
+ hasContent = true;
+ if (spinnerInterval) {
+ clearInterval(spinnerInterval);
+ spinnerInterval = null;
+ }
+ process.stdout.write(`\r${chalk.cyan(` ${this.agentName}:`)} `);
+ }
process.stdout.write(chunk);
full += chunk;
}
- const termWidth = process.stdout.columns || 80;
- let wrappedLines = 0;
- for (const line of full.split('\n')) {
- const visualLen = line.replace(/\x1b\[[0-9;]*m/g, '').length;
- wrappedLines += Math.max(1, Math.ceil((visualLen + 4) / termWidth));
+ if (spinnerInterval) {
+ clearInterval(spinnerInterval);
}
- const totalLines = prefixLines + wrappedLines;
- process.stdout.write(`\x1b[${totalLines}A`);
- for (let i = 0; i < totalLines; i++) {
- process.stdout.write('\x1b[2K\x1b[1B');
+ this.streamActive = false;
+
+ if (!hasContent) {
+ process.stdout.write(`\r${chalk.cyan(` ${this.agentName}:`)} `);
+ }
+
+ if (full.trim()) {
+ const linesWritten = full.split('\n');
+ let textLineCount = 0;
+ const termWidth = process.stdout.columns || 80;
+ for (const line of linesWritten) {
+ const visualLen = line.replace(/\x1b\[[0-9;]*m/g, '').length;
+ textLineCount += Math.max(1, Math.ceil((visualLen + 2) / termWidth));
+ }
+ const totalLines = 1 + textLineCount + 1;
+ process.stdout.write(`\x1b[${totalLines}A`);
+ for (let i = 0; i < totalLines; i++) {
+ process.stdout.write('\x1b[2K\x1b[1B');
+ }
+ process.stdout.write(`\x1b[${totalLines}A`);
+
+ const rendered = renderMarkdown(full);
+ const indented = rendered
+ .split('\n')
+ .map((line: string) => ` ${line}`)
+ .join('\n');
+ console.log(chalk.cyan(` ${this.agentName}:`));
+ console.log(indented);
+ console.log('');
+ } else {
+ console.log('');
}
- process.stdout.write(`\x1b[${totalLines}A`);
- const rendered = renderMarkdown(full);
- const indented = rendered
- .split('\n')
- .map((line: string) => ` ${line}`)
- .join('\n');
- console.log(chalk.cyan(` ${this.agentName}:`));
- console.log(indented);
- console.log('');
this.endOutput();
return full;
}
@@ -165,7 +211,7 @@ export class CLIChannel extends BaseChannel {
showPrompt(): void {
if (this.rl) {
- this.rl.setPrompt(' You: ');
+ this.rl.setPrompt(chalk.yellow(' You: '));
this.rl.prompt();
}
}
@@ -242,4 +288,4 @@ export class CLIChannel extends BaseChannel {
});
});
}
-}
+}
\ No newline at end of file
diff --git a/src/core/agent.ts b/src/core/agent.ts
index df7a5608..90bc5958 100644
--- a/src/core/agent.ts
+++ b/src/core/agent.ts
@@ -12,6 +12,7 @@ import { Lifecycle } from './lifecycle.js';
import { Scheduler } from './scheduler.js';
import { logger } from '../utils/logger.js';
import { CLIChannel } from '../channels/cli.js';
+import { formatToolStep } from '../utils/tool-label.js';
import type { ArrowSelectOption } from '../utils/arrow-select.js';
import {
approveTelegramPendingRequest,
@@ -378,7 +379,15 @@ export class Agent {
}
loopAbortController.abort();
}
- await channel.send(` [Using: ${names}]`, msg.channelId).catch(() => {});
+ if (channel && msg.channelType !== 'internal') {
+ if (channel instanceof CLIChannel) {
+ for (const tc of toolCalls) {
+ await (channel as CLIChannel).sendToolFeedback(tc.toolName, tc.args as Record<string, any>).catch(() => {});
+ }
+ } else {
+ await channel.send(` [Using: ${names}]`, msg.channelId).catch(() => {});
+ }
+ }
}
},
});
@@ -434,7 +443,13 @@ export class Agent {
loopAbortController.abort();
}
if (channel && msg.channelType !== 'internal') {
- await channel.send(` [Using: ${names}]`, msg.channelId).catch(() => {});
+ if (channel instanceof CLIChannel) {
+ for (const tc of toolCalls) {
+ await (channel as CLIChannel).sendToolFeedback(tc.toolName, tc.args as Record<string, any>).catch(() => {});
+ }
+ } else {
+ await channel.send(` [Using: ${names}]`, msg.channelId).catch(() => {});
+ }
}
}
},
diff --git a/src/utils/tool-label.ts b/src/utils/tool-label.ts
new file mode 100644
index 00000000..22723384
--- /dev/null
+++ b/src/utils/tool-label.ts
@@ -0,0 +1,67 @@
+import { z } from 'zod';
+
+const TOOL_LABELS: Record<string, { icon: string; label: string; argKey?: string; argTransform?: (v: string) => string }> = {
+ fetch_url: { icon: '↗', label: 'Fetching', argKey: 'url', argTransform: extractDomain },
+ read_file: { icon: '📄', label: 'Reading', argKey: 'path', argTransform: basename },
+ write_file: { icon: '✏️', label: 'Writing', argKey: 'path', argTransform: basename },
+ create_file: { icon: '✨', label: 'Creating', argKey: 'path', argTransform: basename },
+ edit_file: { icon: '✂️', label: 'Editing', argKey: 'path', argTransform: basename },
+ delete_file: { icon: '🗑', label: 'Deleting', argKey: 'path', argTransform: basename },
+ list_dir: { icon: '📂', label: 'Listing', argKey: 'path', argTransform: basename },
+ approve_scope: { icon: '🔓', label: 'Approving scope' },
+ run_command: { icon: '⌨', label: 'Running command', argKey: 'command', argTransform: truncate(40) },
+ cd: { icon: '📂', label: 'Changing dir to', argKey: 'path' },
+ approve_command: { icon: '✅', label: 'Approving command' },
+ send_message: { icon: '💬', label: 'Sending message' },
+ send_file: { icon: '📎', label: 'Sending file', argKey: 'path', argTransform: basename },
+ git_status: { icon: '📊', label: 'Git status' },
+ git_diff: { icon: '📝', label: 'Git diff' },
+ git_log: { icon: '📋', label: 'Git log' },
+ git_add: { icon: '➕', label: 'Staging files' },
+ git_commit: { icon: '💾', label: 'Committing' },
+ git_push: { icon: '⬆', label: 'Pushing' },
+ create_pr: { icon: '🔀', label: 'Creating PR' },
+ review_pr: { icon: '👀', label: 'Reviewing PR' },
+ list_issues: { icon: '📋', label: 'Listing issues' },
+ create_issue: { icon: '🐛', label: 'Creating issue' },
+ github_api: { icon: '🔀', label: 'GitHub API', argKey: 'path', argTransform: truncate(30) },
+ schedule_task: { icon: '⏰', label: 'Scheduling task' },
+ cancel_task: { icon: '❌', label: 'Cancelling task' },
+ list_tasks: { icon: '📋', label: 'Listing tasks' },
+ use_skill: { icon: '🧠', label: 'Using skill', argKey: 'name' },
+ list_skills: { icon: '📋', label: 'Listing skills' },
+ install_skill: { icon: '📥', label: 'Installing skill' },
+ budget_status: { icon: '💰', label: 'Budget status' },
+};
+
+function extractDomain(url: string): string {
+ try {
+ return new URL(url).hostname;
+ } catch {
+ return url;
+ }
+}
+
+function basename(p: string): string {
+ const parts = p.replace(/\\/g, '/').split('/');
+ return parts[parts.length - 1] || p;
+}
+
+function truncate(maxLen: number): (v: string) => string {
+ return (v: string) => v.length > maxLen ? v.slice(0, maxLen) + '…' : v;
+}
+
+export function formatToolStep(toolName: string, args: Record<string, any>): string {
+ const config = TOOL_LABELS[toolName];
+ if (!config) {
+ return toolName;
+ }
+
+ let detail = '';
+ if (config.argKey && args[config.argKey] !== undefined) {
+ const raw = String(args[config.argKey]);
+ detail = config.argTransform ? config.argTransform(raw) : raw;
+ }
+
+ return detail ? `${config.icon} ${config.label} ${detail}` : `${config.icon} ${config.label}`;
+}
\ No newline at end of file