From 73657be5503598c1c60fc949cce81dde072e9e37 Mon Sep 17 00:00:00 2001 From: Juergen Kunz Date: Tue, 20 Jan 2026 03:16:02 +0000 Subject: [PATCH] fix(driveragent): prevent duplicate thinking/output markers during token streaming and mark transitions --- changelog.md | 9 ++++++++ ts/00_commitinfo_data.ts | 2 +- ts/smartagent.classes.driveragent.ts | 31 ++++++++++++++++++++++++++-- 3 files changed, 39 insertions(+), 3 deletions(-) diff --git a/changelog.md b/changelog.md index d207dc2..e96c502 100644 --- a/changelog.md +++ b/changelog.md @@ -1,5 +1,14 @@ # Changelog +## 2026-01-20 - 1.5.4 - fix(driveragent) +prevent duplicate thinking/output markers during token streaming and mark transitions + +- Add isInThinkingMode flag to track thinking vs output state +- Emit "\n[THINKING] " only when transitioning into thinking mode (avoids repeated thinking markers) +- Emit "\n[OUTPUT] " when transitioning out of thinking mode to mark content output +- Reset thinking state after response completes to ensure correct markers for subsequent responses +- Applied the same streaming marker logic to both response handling paths + ## 2026-01-20 - 1.5.3 - fix(driveragent) prefix thinking tokens with [THINKING] when forwarding streaming chunks to onToken diff --git a/ts/00_commitinfo_data.ts b/ts/00_commitinfo_data.ts index dbe9ebb..5adf1a9 100644 --- a/ts/00_commitinfo_data.ts +++ b/ts/00_commitinfo_data.ts @@ -3,6 +3,6 @@ */ export const commitinfo = { name: '@push.rocks/smartagent', - version: '1.5.3', + version: '1.5.4', description: 'an agentic framework built on top of @push.rocks/smartai' } diff --git a/ts/smartagent.classes.driveragent.ts b/ts/smartagent.classes.driveragent.ts index 85bbb27..08e48b9 100644 --- a/ts/smartagent.classes.driveragent.ts +++ b/ts/smartagent.classes.driveragent.ts @@ -25,6 +25,7 @@ export class DriverAgent { private messageHistory: plugins.smartai.ChatMessage[] = []; private tools: Map = new Map(); private onToken?: (token: string) => void; + private isInThinkingMode = false; // Track thinking/content state for markers constructor( provider: plugins.smartai.MultiModalModel, @@ -494,14 +495,27 @@ Your complete output here // Pass onToken callback through onChunk for streaming with thinking markers this.onToken ? (chunk: any) => { if (chunk.thinking && this.onToken) { - this.onToken(`[THINKING] ${chunk.thinking}`); + // Add marker only when transitioning INTO thinking mode + if (!this.isInThinkingMode) { + this.onToken('\n[THINKING] '); + this.isInThinkingMode = true; + } + this.onToken(chunk.thinking); } if (chunk.content && this.onToken) { + // Add marker when transitioning OUT of thinking mode + if (this.isInThinkingMode) { + this.onToken('\n[OUTPUT] '); + this.isInThinkingMode = false; + } this.onToken(chunk.content); } } : undefined ); + // Reset thinking state after response completes + this.isInThinkingMode = false; + // Add assistant response to history const historyMessage: plugins.smartai.ChatMessage = { role: 'assistant', @@ -581,14 +595,27 @@ Your complete output here // Pass onToken callback through onChunk for streaming with thinking markers this.onToken ? (chunk: any) => { if (chunk.thinking && this.onToken) { - this.onToken(`[THINKING] ${chunk.thinking}`); + // Add marker only when transitioning INTO thinking mode + if (!this.isInThinkingMode) { + this.onToken('\n[THINKING] '); + this.isInThinkingMode = true; + } + this.onToken(chunk.thinking); } if (chunk.content && this.onToken) { + // Add marker when transitioning OUT of thinking mode + if (this.isInThinkingMode) { + this.onToken('\n[OUTPUT] '); + this.isInThinkingMode = false; + } this.onToken(chunk.content); } } : undefined ); + // Reset thinking state after response completes + this.isInThinkingMode = false; + // Add assistant response to history this.messageHistory.push({ role: 'assistant',