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(code): step narrations render as separate blocks in the live tail

Salman Qureshie7b1d0dd551c
diff --git a/src/core/agent.ts b/src/core/agent.ts
index cbb52078..fbc2aef9 100644
--- a/src/core/agent.ts
+++ b/src/core/agent.ts
@@ -94,7 +94,7 @@ import { whatsNewText } from '../utils/whats-new.js';
* fullStream and inserts paragraph breaks at step boundaries so each
* step's narration reads as its own block.
*/
-function stepAwareTextStream(
+export function stepAwareTextStream(
fullStream: AsyncIterable<any>,
onReasoning?: (preview: string | null) => void,
): AsyncIterable<string> {
@@ -103,7 +103,11 @@ function stepAwareTextStream(
let sawTextInStep = false;
let reasoningBuf = '';
for await (const part of fullStream) {
- if (part.type === 'step-start') {
+ // Step boundary: AI SDK 6 emits 'start-step' (the v4/v5 name was
+ // 'step-start'). Matching only the old name meant the separator never
+ // fired — every step's narration ran into the next as word salad
+ // ("…in this project.Found the situation…") in the live tail.
+ if (part.type === 'start-step' || part.type === 'step-start') {
if (!firstStep && sawTextInStep) yield '\n\n';
firstStep = false;
sawTextInStep = false;
diff --git a/src/core/step-aware-text-stream.test.ts b/src/core/step-aware-text-stream.test.ts
new file mode 100644
index 00000000..05b280a3
--- /dev/null
+++ b/src/core/step-aware-text-stream.test.ts
@@ -0,0 +1,71 @@
+import { describe, expect, it } from 'vitest';
+import { stepAwareTextStream } from './agent.js';
+
+/**
+ * The step-aware text stream inserts a paragraph break at every agentic step
+ * boundary — otherwise the SDK's raw concatenation produced word salad in the
+ * live tail ("…in this project.Found the situation…"). AI SDK 6 renamed the
+ * step-boundary part from 'step-start' to 'start-step'; both names must
+ * produce the separator (this test locks the fix for the v6 rename).
+ */
+async function collect(stream: AsyncIterable<string>): Promise<string> {
+ let out = '';
+ for await (const chunk of stream) out += chunk;
+ return out;
+}
+
+describe('stepAwareTextStream', () => {
+ it('separates narration of consecutive steps under the AI SDK 6 name (start-step)', async () => {
+ const fullStream = [
+ { type: 'start' },
+ { type: 'start-step', request: {}, warnings: [] },
+ { type: 'text-delta', id: 't1', text: 'Let me investigate the cloud sync situation in this project.' },
+ { type: 'tool-call', toolCallId: 'c1', toolName: 'run_command', input: {} },
+ { type: 'finish-step', usage: {}, finishReason: 'tool-calls', rawFinishReason: 'stop', providerMetadata: undefined },
+ { type: 'start-step', request: {}, warnings: [] },
+ { type: 'text-delta', id: 't2', text: 'Found the situation. Let me look deeper at the data layer.' },
+ { type: 'finish-step', usage: {}, finishReason: 'stop', rawFinishReason: 'stop', providerMetadata: undefined },
+ { type: 'finish', finishReason: 'stop', rawFinishReason: 'stop', totalUsage: {} },
+ ];
+ const text = await collect(stepAwareTextStream(fullStream as any));
+ expect(text).toBe(
+ 'Let me investigate the cloud sync situation in this project.' +
+ '\n\n' +
+ 'Found the situation. Let me look deeper at the data layer.',
+ );
+ });
+
+ it('keeps the legacy v4/v5 name (step-start) working too', async () => {
+ const fullStream = [
+ { type: 'start-step' },
+ { type: 'text-delta', id: 't1', text: 'first' },
+ { type: 'step-start' },
+ { type: 'text-delta', id: 't2', text: 'second' },
+ ];
+ expect(await collect(stepAwareTextStream(fullStream as any))).toBe('first\n\nsecond');
+ });
+
+ it('no leading break before the first step, none after a step that only called tools', async () => {
+ const fullStream = [
+ { type: 'start-step' },
+ { type: 'tool-call', toolCallId: 'c1', toolName: 'list_dir', input: {} },
+ { type: 'start-step' },
+ { type: 'text-delta', id: 't1', text: 'narration only here' },
+ ];
+ expect(await collect(stepAwareTextStream(fullStream as any))).toBe('narration only here');
+ });
+
+ it('reasoning deltas surface via the preview callback and never reach the text', async () => {
+ const previews: Array<string | null> = [];
+ const fullStream = [
+ { type: 'start-step' },
+ { type: 'reasoning-delta', id: 'r1', text: 'thinking hard' },
+ { type: 'text-delta', id: 't1', text: 'answer text' },
+ { type: 'finish-step', usage: {}, finishReason: 'stop', rawFinishReason: 'stop', providerMetadata: undefined },
+ ];
+ const text = await collect(stepAwareTextStream(fullStream as any, (p) => previews.push(p)));
+ expect(text).toBe('answer text');
+ expect(previews.length).toBeGreaterThan(0);
+ expect(previews.at(-1)).toBeNull(); // cleared once text starts
+ });
+});
\ No newline at end of file