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 streaming support for agent responses in CLI channel

Salman Qureshi7214095b8a85
diff --git a/src/channels/base.ts b/src/channels/base.ts
index c0c0989a..845bd29c 100644
--- a/src/channels/base.ts
+++ b/src/channels/base.ts
@@ -6,7 +6,7 @@ export interface Channel {
stop(): Promise<void>;
send(content: string, targetId?: string, elapsedMs?: number): Promise<void>;
sendFile(filePath: string, targetId?: string): Promise<void>;
- stream(content: AsyncIterable<string>, targetId?: string): Promise<void>;
+ stream(content: AsyncIterable<string>, targetId?: string): Promise<string>;
typing(targetId?: string): Promise<void>;
isReady(): boolean;
onMessage(handler: (msg: ChannelMessage) => void): void;
@@ -21,7 +21,7 @@ export abstract class BaseChannel implements Channel {
abstract stop(): Promise<void>;
abstract send(content: string, targetId?: string, elapsedMs?: number): Promise<void>;
abstract sendFile(filePath: string, targetId?: string): Promise<void>;
- abstract stream(content: AsyncIterable<string>, targetId?: string): Promise<void>;
+ abstract stream(content: AsyncIterable<string>, targetId?: string): Promise<string>;
abstract typing(targetId?: string): Promise<void>;
isReady(): boolean {
diff --git a/src/channels/cli.ts b/src/channels/cli.ts
index 4c0fea46..aa888a7f 100644
--- a/src/channels/cli.ts
+++ b/src/channels/cli.ts
@@ -91,15 +91,17 @@ export class CLIChannel extends BaseChannel {
this.showPrompt();
}
- async stream(content: AsyncIterable<string>, _targetId?: string): Promise<void> {
+ async stream(content: AsyncIterable<string>, _targetId?: string): Promise<string> {
+ console.log('');
process.stdout.write(chalk.cyan(` ${this.agentName}: `));
let full = '';
for await (const chunk of content) {
process.stdout.write(chunk);
full += chunk;
}
- process.stdout.write('\n\n');
+ console.log('\n');
this.showPrompt();
+ return full;
}
async typing(_targetId?: string): Promise<void> {
diff --git a/src/channels/telegram.ts b/src/channels/telegram.ts
index df478554..7a286499 100644
--- a/src/channels/telegram.ts
+++ b/src/channels/telegram.ts
@@ -128,9 +128,9 @@ export class TelegramChannel extends BaseChannel {
}
}
- async stream(content: AsyncIterable<string>, targetId?: string): Promise<void> {
+ async stream(content: AsyncIterable<string>, targetId?: string): Promise<string> {
const chatId = this.parseChatId(targetId);
- if (!chatId || !this.bot) return;
+ if (!chatId || !this.bot) return '';
let full = '';
for await (const chunk of content) {
@@ -142,6 +142,7 @@ export class TelegramChannel extends BaseChannel {
} catch (err: any) {
await this.bot.api.sendMessage(chatId, this.stripHtml(html));
}
+ return full;
}
async typing(targetId?: string): Promise<void> {
diff --git a/src/core/agent.ts b/src/core/agent.ts
index 7b5290aa..6fc11503 100644
--- a/src/core/agent.ts
+++ b/src/core/agent.ts
@@ -1,4 +1,4 @@
-import { generateText } from 'ai';
+import { generateText, streamText } from 'ai';
import type { ChannelMessage } from '../types/channel.js';
import type { ProviderRegistry } from '../providers/registry.js';
import type { Identity } from '../soul/identity.js';
@@ -174,27 +174,56 @@ export class Agent {
let result: any = null;
let usedProvider: { name: string; model: string } | null = null;
let lastError: any = null;
+ let streamedText = '';
+
+ const canStream = msg.channelType === 'cli';
for (const provider of fallbackIterator) {
try {
- logger.info({ provider: provider.name, model: provider.getModel(), steps: MAX_STEPS }, 'Generating agentic response');
-
- result = await generateText({
- model: provider.getModelInstance(),
- system: systemPrompt,
- messages,
- tools: this.capabilities.getTools(),
- maxSteps: MAX_STEPS,
- onStepFinish: async ({ toolCalls, text }) => {
- if (toolCalls && toolCalls.length > 0) {
- const names = toolCalls.map((tc: any) => tc.toolName).join(', ');
- logger.info({ tools: names }, 'Tool call step');
- if (channel && msg.channelType !== 'internal') {
+ logger.info({ provider: provider.name, model: provider.getModel(), steps: MAX_STEPS, stream: canStream }, 'Generating agentic response');
+
+ if (canStream && channel) {
+ const streamResult = streamText({
+ model: provider.getModelInstance(),
+ system: systemPrompt,
+ messages,
+ tools: this.capabilities.getTools(),
+ maxSteps: MAX_STEPS,
+ onStepFinish: async ({ toolCalls }) => {
+ if (toolCalls && toolCalls.length > 0) {
+ const names = toolCalls.map((tc: any) => tc.toolName).join(', ');
+ logger.info({ tools: names }, 'Tool call step');
await channel.send(` [Using: ${names}]`, msg.channelId).catch(() => {});
}
- }
- },
- });
+ },
+ });
+
+ const fullText = await channel.stream(streamResult.textStream, msg.channelId);
+
+ const [usage] = await Promise.all([
+ streamResult.usage,
+ ]);
+
+ result = { text: fullText, usage };
+ streamedText = fullText;
+ } else {
+ result = await generateText({
+ model: provider.getModelInstance(),
+ system: systemPrompt,
+ messages,
+ tools: this.capabilities.getTools(),
+ maxSteps: MAX_STEPS,
+ onStepFinish: async ({ toolCalls, text }) => {
+ if (toolCalls && toolCalls.length > 0) {
+ const names = toolCalls.map((tc: any) => tc.toolName).join(', ');
+ logger.info({ tools: names }, 'Tool call step');
+ if (channel && msg.channelType !== 'internal') {
+ await channel.send(` [Using: ${names}]`, msg.channelId).catch(() => {});
+ }
+ }
+ },
+ });
+ }
usedProvider = { name: provider.name, model: provider.getModel() };
this.providers.markSuccess(provider.name);
@@ -218,7 +247,7 @@ export class Agent {
return;
}
- const finalText = result.text;
+ const finalText = streamedText || result.text;
this.tokenBudget.recordUsage({
provider: usedProvider!.name,
@@ -257,9 +286,13 @@ export class Agent {
}
if (channel && msg.channelType !== 'internal') {
- logger.info({ channelType: msg.channelType, targetId: msg.channelId }, 'Sending response');
const elapsed = Date.now() - startTime;
- await channel.send(finalText, msg.channelId, elapsed);
+ if (streamedText) {
+ logger.info({ channelType: msg.channelType, elapsed }, 'Streamed response completed');
+ } else {
+ logger.info({ channelType: msg.channelType, targetId: msg.channelId }, 'Sending response');
+ await channel.send(finalText, msg.channelId, elapsed);
+ }
} else {
logger.debug('Internal prompt processed, no channel response needed');
}