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: add response latency tracking to channel send interface and UI

Salman Qureshi184251bbe847
diff --git a/src/channels/base.ts b/src/channels/base.ts
index b68d8514..26cd3fbf 100644
--- a/src/channels/base.ts
+++ b/src/channels/base.ts
@@ -4,7 +4,7 @@ export interface Channel {
readonly type: ChannelType;
start(): Promise<void>;
stop(): Promise<void>;
- send(content: string, targetId?: string): Promise<void>;
+ send(content: string, targetId?: string, elapsedMs?: number): Promise<void>;
stream(content: AsyncIterable<string>, targetId?: string): Promise<void>;
typing(targetId?: string): Promise<void>;
isReady(): boolean;
@@ -18,7 +18,7 @@ export abstract class BaseChannel implements Channel {
abstract start(): Promise<void>;
abstract stop(): Promise<void>;
- abstract send(content: string, targetId?: string): Promise<void>;
+ abstract send(content: string, targetId?: string, elapsedMs?: number): Promise<void>;
abstract stream(content: AsyncIterable<string>, targetId?: string): Promise<void>;
abstract typing(targetId?: string): Promise<void>;
diff --git a/src/channels/cli.ts b/src/channels/cli.ts
index 0e0136b7..ad58e4e9 100644
--- a/src/channels/cli.ts
+++ b/src/channels/cli.ts
@@ -22,12 +22,15 @@ export class CLIChannel extends BaseChannel {
this.rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
- prompt: '',
+ prompt: ' You: ',
});
this.rl.on('line', (line) => {
const trimmed = line.trim();
- if (!trimmed) return;
+ if (!trimmed) {
+ this.showPrompt();
+ return;
+ }
const msg: ChannelMessage = {
id: Date.now().toString(36),
@@ -50,9 +53,11 @@ export class CLIChannel extends BaseChannel {
this.ready = false;
}
- async send(content: string, _targetId?: string): Promise<void> {
+ async send(content: string, _targetId?: string, elapsedMs?: number): Promise<void> {
+ const timeStr = elapsedMs != null ? chalk.dim(` (${(elapsedMs / 1000).toFixed(1)}s)`) : '';
console.log('');
- console.log(chalk.cyan(` ${this.agentName}: `) + content);
+ console.log(chalk.cyan(` ${this.agentName}:`) + timeStr);
+ console.log(` ${content}`);
console.log('');
this.showPrompt();
}
@@ -71,7 +76,10 @@ export class CLIChannel extends BaseChannel {
}
showPrompt(): void {
- process.stdout.write(' You: ');
+ if (this.rl) {
+ this.rl.setPrompt(' You: ');
+ this.rl.prompt();
+ }
}
async prompt(question: string): Promise<string> {
diff --git a/src/channels/telegram.ts b/src/channels/telegram.ts
index 8bb4a86b..92fab5ad 100644
--- a/src/channels/telegram.ts
+++ b/src/channels/telegram.ts
@@ -65,14 +65,15 @@ export class TelegramChannel extends BaseChannel {
this.stopTypingLoop();
}
- async send(content: string, targetId?: string): Promise<void> {
+ async send(content: string, targetId?: string, elapsedMs?: number): Promise<void> {
const chatId = this.parseChatId(targetId);
if (!chatId || !this.bot) {
logger.warn({ targetId, chatId }, 'Telegram send: no valid chat ID');
return;
}
+ const timeSuffix = elapsedMs != null ? `\n⏱ ${(elapsedMs / 1000).toFixed(1)}s` : '';
logger.debug({ chatId, textLen: content.length }, 'Telegram sending message');
- await this.bot.api.sendMessage(chatId, content);
+ await this.bot.api.sendMessage(chatId, content + timeSuffix);
}
async stream(content: AsyncIterable<string>, targetId?: string): Promise<void> {
diff --git a/src/core/agent.ts b/src/core/agent.ts
index 4e8e28d5..7ec7c036 100644
--- a/src/core/agent.ts
+++ b/src/core/agent.ts
@@ -101,6 +101,7 @@ export class Agent {
private async handleMessage(msg: ChannelMessage): Promise<void> {
this.lifecycle.transition('thinking');
+ const startTime = Date.now();
try {
const provider = this.providers.getDefault();
@@ -189,7 +190,8 @@ export class Agent {
if (channel && msg.channelType !== 'internal') {
logger.info({ channelType: msg.channelType, targetId: msg.channelId }, 'Sending response');
- await channel.send(finalText, msg.channelId);
+ const elapsed = Date.now() - startTime;
+ await channel.send(finalText, msg.channelId, elapsed);
} else {
logger.debug('Internal prompt processed, no channel response needed');
}
diff --git a/src/index.ts b/src/index.ts
index c6048b26..2a098cca 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -20,9 +20,9 @@ function hr() {
function banner() {
console.log('');
- console.log(chalk.cyan(' ┌──────────────────────────┐'));
- console.log(chalk.cyan(' │') + chalk.bold.white(' M E R C U R Y ') + chalk.cyan('│'));
- console.log(chalk.cyan(' └──────────────────────────┘'));
+ console.log(chalk.cyan(' ┌────────────────────────────────────┐'));
+ console.log(chalk.cyan(' │') + chalk.bold.white(' M E R C U R Y ') + chalk.cyan('│'));
+ console.log(chalk.cyan(' └────────────────────────────────────┘'));
console.log(chalk.dim(' v0.1.0 — an AI agent for personal tasks'));
console.log(chalk.dim(' by Cosmic Stack — mercury.cosmicstack.org'));
console.log('');
@@ -30,9 +30,9 @@ function banner() {
function splashScreen() {
console.log('');
- console.log(chalk.cyan(' ┌──────────────────────────┐'));
- console.log(chalk.cyan(' │') + chalk.bold.white(' M E R C U R Y ') + chalk.cyan('│'));
- console.log(chalk.cyan(' └──────────────────────────┘'));
+ console.log(chalk.cyan(' ┌────────────────────────────────────┐'));
+ console.log(chalk.cyan(' │') + chalk.bold.white(' M E R C U R Y ') + chalk.cyan('│'));
+ console.log(chalk.cyan(' └────────────────────────────────────┘'));
console.log('');
console.log(chalk.dim(' an AI agent for personal tasks'));
console.log(chalk.cyan(' by Cosmic Stack'));