Skip to content

The agent that grows with you

fix(desktop): decode reasoning-details envelopes

LordMelkor6808bca1f4ef
diff --git a/apps/desktop/src/lib/chat-messages/hydration.ts b/apps/desktop/src/lib/chat-messages/hydration.ts
index 04fa5744375..7c7c8cfd9f6 100644
--- a/apps/desktop/src/lib/chat-messages/hydration.ts
+++ b/apps/desktop/src/lib/chat-messages/hydration.ts
@@ -11,6 +11,7 @@ import {
chatMessageText,
dedupeRepeatedTextInParts,
reasoningPart,
+ reasoningTextFromDetails,
renderMediaTags,
textPart
} from './parts'
@@ -456,7 +457,7 @@ export function toChatMessages(messages: SessionMessage[]): ChatMessage[] {
const rawReasoning =
message.reasoning ||
message.reasoning_content ||
- (typeof message.reasoning_details === 'string' ? message.reasoning_details : '')
+ reasoningTextFromDetails(message.reasoning_details)
const reasoning = message.display_reasoning !== undefined ? message.display_reasoning : rawReasoning
diff --git a/apps/desktop/src/lib/chat-messages/index.ts b/apps/desktop/src/lib/chat-messages/index.ts
index 981f0b91b70..dd4309bce11 100644
--- a/apps/desktop/src/lib/chat-messages/index.ts
+++ b/apps/desktop/src/lib/chat-messages/index.ts
@@ -12,6 +12,7 @@ export {
mergeFinalAssistantText,
normalizeWs,
reasoningPart,
+ reasoningTextFromDetails,
renderMediaTags,
textPart
} from './parts'
diff --git a/apps/desktop/src/lib/chat-messages/parts.ts b/apps/desktop/src/lib/chat-messages/parts.ts
index 8a8af3f70e8..9f6d595b930 100644
--- a/apps/desktop/src/lib/chat-messages/parts.ts
+++ b/apps/desktop/src/lib/chat-messages/parts.ts
@@ -10,6 +10,47 @@ export function reasoningPart(text: string, timestamp?: number): ChatMessagePart
return { type: 'reasoning', text, ...(timestamp !== undefined ? { timestamp } : {}) }
}
+/** Extract display text from a provider reasoning-details envelope. */
+export function reasoningTextFromDetails(details: unknown): string {
+ let blocks = details
+
+ if (typeof blocks === 'string') {
+ const trimmed = blocks.trim()
+
+ // Some persisted rows contain plain reasoning text rather than a JSON envelope.
+ if (!trimmed.startsWith('[') && !trimmed.startsWith('{')) {
+ return trimmed
+ }
+
+ try {
+ blocks = JSON.parse(trimmed) as unknown
+ } catch {
+ // A malformed structured envelope is replay metadata, not display text.
+ return ''
+ }
+ }
+
+ const text: string[] = []
+
+ for (const block of Array.isArray(blocks) ? blocks : [blocks]) {
+ if (typeof block === 'string') {
+ if (block.trim()) text.push(block)
+ continue
+ }
+
+ if (!block || typeof block !== 'object') continue
+
+ const record = block as Record<string, unknown>
+ const value = [record.summary, record.thinking, record.content, record.text].find(
+ candidate => typeof candidate === 'string' && candidate.trim()
+ )
+
+ if (typeof value === 'string') text.push(value)
+ }
+
+ return text.join('\n\n').trim()
+}
+
/**
* Known deliverable file extensions — mirrors the Python-side
* `MEDIA_DELIVERY_EXTS` in `gateway/platforms/base.py` so the two surfaces
diff --git a/apps/desktop/src/lib/chat-messages/reasoning-details.test.ts b/apps/desktop/src/lib/chat-messages/reasoning-details.test.ts
new file mode 100644
index 00000000000..04b3acdf850
--- /dev/null
+++ b/apps/desktop/src/lib/chat-messages/reasoning-details.test.ts
@@ -0,0 +1,47 @@
+import { describe, expect, it } from 'vitest'
+
+import type { SessionMessage } from '@/types/hermes'
+
+import { toChatMessages } from './hydration'
+import { reasoningTextFromDetails } from './parts'
+
+describe('reasoning-details display text', () => {
+ it('extracts prose from decoded or serialized envelopes without exposing opaque fields', () => {
+ const details = [
+ { type: 'reasoning.summary', summary: 'Check the inputs.', signature: 'opaque-token' },
+ { type: 'thinking', thinking: '', signature: 'opaque-token-2' },
+ { type: 'redacted_thinking', data: 'encrypted-replay-data' },
+ { type: 'reasoning.text', text: 'Return the result.' }
+ ]
+
+ for (const value of [details, JSON.stringify(details)]) {
+ const text = reasoningTextFromDetails(value)
+
+ expect(text).toBe('Check the inputs.\n\nReturn the result.')
+ expect(text).not.toContain('opaque-token')
+ expect(text).not.toContain('encrypted-replay-data')
+ }
+
+ expect(reasoningTextFromDetails('Plain persisted reasoning.')).toBe('Plain persisted reasoning.')
+ expect(reasoningTextFromDetails('[{"type":"thinking","thinking":"truncated"')).toBe('')
+ })
+
+ it('hydrates only extracted reasoning text and preserves canonical-field precedence', () => {
+ const reasoningParts = (message: Partial<SessionMessage>): string[] =>
+ toChatMessages([{ role: 'assistant', content: 'Answer.', ...message } as SessionMessage])
+ .flatMap(item => item.parts)
+ .filter(part => part.type === 'reasoning')
+ .map(part => (part as { text: string }).text)
+
+ expect(reasoningParts({ reasoning_details: [{ type: 'thinking', signature: 'opaque-only' }] })).toEqual([])
+ expect(
+ reasoningParts({ reasoning_details: JSON.stringify([{ type: 'thinking', thinking: 'Visible thought.' }]) })
+ ).toEqual(['Visible thought.'])
+ expect(
+ reasoningParts({
+ reasoning: 'Canonical reasoning.',
+ reasoning_details: JSON.stringify([{ type: 'thinking', thinking: 'Fallback reasoning.' }])
+ })
+ ).toEqual(['Canonical reasoning.'])
+ })
+})