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.

fix(ui): heartbeat updates one message in place instead of stacking

Salman Qureshic8ae9850439c
diff --git a/src/channels/cli.ts b/src/channels/cli.ts
index 96a6a55d..605af3e9 100644
--- a/src/channels/cli.ts
+++ b/src/channels/cli.ts
@@ -87,6 +87,7 @@ export class CLIChannel extends BaseChannel {
private permissionResolver: ((value: string | boolean) => void) | null = null;
private menuDepth = 0;
private menuAbortController: AbortController | null = null;
+ private heartbeatMsgId: string | null = null;
private stepCount = 0;
private stepStartTime = 0;
private state: TuiState = { ...defaultState };
@@ -170,6 +171,15 @@ export class CLIChannel extends BaseChannel {
this.rerender();
}
+ /** Update an existing chat message's content in place (by ID). */
+ private updateMessage(id: string, content: string, extra?: Partial<ChatMessage>): void {
+ this.update({
+ chatMessages: this.state.chatMessages.map((m) =>
+ m.id === id ? { ...m, content, timestamp: Date.now(), ...extra } : m,
+ ),
+ });
+ }
+
private rerender(): void {
if (!this.inkInstance) return;
this.inkInstance.rerender(
@@ -358,13 +368,49 @@ export class CLIChannel extends BaseChannel {
content,
timestamp: Date.now(),
};
+ // Clear any lingering heartbeat message when we send a real response.
+ if (this.heartbeatMsgId) {
+ this.state.chatMessages = this.state.chatMessages.filter((m) => m.id !== this.heartbeatMsgId);
+ this.heartbeatMsgId = null;
+ }
this.update({
chatMessages: [...this.state.chatMessages, msg],
isThinking: false,
});
}
+ /**
+ * Send or replace a heartbeat progress message. First call creates a new
+ * message; subsequent calls update it in place. This avoids stacking
+ * multiple "⏳ Working..." messages in the chat history.
+ */
+ sendHeartbeat(content: string): void {
+ if (this.heartbeatMsgId) {
+ // Update existing heartbeat message in place.
+ this.updateMessage(this.heartbeatMsgId, content, { role: 'system' });
+ } else {
+ // First heartbeat — create the message.
+ const id = `heartbeat-${Date.now().toString(36)}`;
+ this.heartbeatMsgId = id;
+ const msg: ChatMessage = { id, role: 'system', content, timestamp: Date.now() };
+ this.update({
+ chatMessages: [...this.state.chatMessages, msg],
+ isThinking: true,
+ });
+ }
+ }
+
+ /** Clear the heartbeat message (called when processing completes). */
+ clearHeartbeat(): void {
+ if (this.heartbeatMsgId) {
+ this.state.chatMessages = this.state.chatMessages.filter((m) => m.id !== this.heartbeatMsgId);
+ this.heartbeatMsgId = null;
+ this.rerender();
+ }
+ }
+
sendCompletion(elapsedMs: number, stepCount: number, meta?: CompletionMeta): void {
+ this.clearHeartbeat();
const secs = Math.floor(elapsedMs / 1000);
const mins = Math.floor(secs / 60);
const remSecs = secs % 60;
@@ -451,6 +497,8 @@ export class CLIChannel extends BaseChannel {
let full = '';
let lastRender = 0;
+ this.clearHeartbeat();
+
const initialMsg: ChatMessage = {
id: msgId,
role: 'agent',
diff --git a/src/core/agent.ts b/src/core/agent.ts
index 86c0d4c9..c2fbc7c8 100644
--- a/src/core/agent.ts
+++ b/src/core/agent.ts
@@ -725,10 +725,15 @@ export class Agent {
: '';
const narrative = formatNarrative(this.stepNarrative, this.currentActivity, 3);
const narrativeBlock = narrative ? `\n${narrative}` : '';
- void channel.send(
- `⏳ Working... ${elapsedSec}s elapsed${stepInfo}.${narrativeBlock}${handoffHint}`,
- msg.channelId,
- ).catch((e) => logger.warn({ e }, 'channel send failed'));
+ const heartbeatText = `⏳ Working... ${elapsedSec}s elapsed${stepInfo}.${narrativeBlock}${handoffHint}`;
+
+ // CLI: update one message in place instead of stacking new ones.
+ // Other channels (Telegram): still send as separate messages.
+ if (channel instanceof CLIChannel) {
+ (channel as CLIChannel).sendHeartbeat(heartbeatText);
+ } else {
+ void channel.send(heartbeatText, msg.channelId).catch((e) => logger.warn({ e }, 'channel send failed'));
+ }
// Escalate: 20s → 30s → 45s → 60s (cap)
if (heartbeatCount <= 2) {