The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(desktop): keep single line breaks in assistant replies
Adolan9b17e8097097
diff --git a/apps/desktop/src/components/assistant-ui/markdown-text.tsx b/apps/desktop/src/components/assistant-ui/markdown-text.tsxindex c081edff6f5..ada919bdfb2 100644--- a/apps/desktop/src/components/assistant-ui/markdown-text.tsx+++ b/apps/desktop/src/components/assistant-ui/markdown-text.tsx@@ -9,6 +9,7 @@ import {} from '@assistant-ui/react-streamdown'import type { code as streamdownCode } from '@streamdown/code'import { type ComponentProps, isValidElement, memo, type ReactNode, useEffect, useMemo, useState } from 'react'+import { defaultRemarkPlugins } from 'streamdown'import { ExpandableBlock } from '@/components/chat/expandable-block'import { PreviewAttachment } from '@/components/chat/preview-attachment'@@ -37,6 +38,7 @@ import {} from '@/lib/media'import { isOnboardingEnabled } from '@/lib/onboarding-enabled'import { previewTargetFromMarkdownHref } from '@/lib/preview-targets'+import { remarkSoftBreaks } from '@/lib/remark-soft-breaks'import { sessionRefFromMarkdownHref } from '@/lib/session-refs'import { isDirectiveInProgress } from '@/lib/transcript-directives'import { cn } from '@/lib/utils'@@ -99,6 +101,8 @@ function useCodePlugin(): CodePlugin | null {return plugin}+const REMARK_PLUGINS = [...Object.values(defaultRemarkPlugins), remarkSoftBreaks]+// Replaces Streamdown's `parseIncompleteMarkdown` (full-text remend per// flush) with a tail-bounded repair. Must stay module-scope so the prop// identity is stable across renders.@@ -800,6 +804,7 @@ function MarkdownTextSurface({parseMarkdownIntoBlocksFn={parseMarkdownIntoBlocksCached}plugins={plugins}preprocess={preprocessWithTailRepair}+ remarkPlugins={REMARK_PLUGINS}/></ErrorBoundary>)diff --git a/apps/desktop/src/lib/remark-soft-breaks.test.ts b/apps/desktop/src/lib/remark-soft-breaks.test.tsnew file mode 100644index 00000000000..40a1dd4030f--- /dev/null+++ b/apps/desktop/src/lib/remark-soft-breaks.test.ts@@ -0,0 +1,43 @@+import { describe, expect, it } from 'vitest'++import { type MdastNode, remarkSoftBreaks } from './remark-soft-breaks'++const paragraph = (...children: MdastNode[]) => ({ type: 'root', children: [{ type: 'paragraph', children }] })++describe('remarkSoftBreaks', () => {+ it('turns single newlines in prose into line breaks', () => {+ const tree = paragraph({ type: 'text', value: '1\n2 \n3' })++ remarkSoftBreaks()(tree)++ expect(tree.children[0]!.children).toEqual([+ { type: 'text', value: '1' },+ { type: 'break' },+ { type: 'text', value: '2' },+ { type: 'break' },+ { type: 'text', value: '3' }+ ])+ })++ it('leaves code and math nodes untouched', () => {+ const code = { type: 'code', value: 'a\nb' }+ const math = { type: 'inlineMath', value: 'x\ny' }+ const tree: MdastNode = { type: 'root', children: [code, paragraph(math).children[0]!] }++ remarkSoftBreaks()(tree)++ expect(tree.children?.[0]).toEqual({ type: 'code', value: 'a\nb' })+ expect(tree.children?.[1]).toEqual({ type: 'paragraph', children: [{ type: 'inlineMath', value: 'x\ny' }] })+ })++ it('splits text nested inside emphasis', () => {+ const tree = paragraph({ type: 'emphasis', children: [{ type: 'text', value: 'a\nb' }] })++ remarkSoftBreaks()(tree)++ expect(tree.children[0]!.children[0]).toEqual({+ type: 'emphasis',+ children: [{ type: 'text', value: 'a' }, { type: 'break' }, { type: 'text', value: 'b' }]+ })+ })+})diff --git a/apps/desktop/src/lib/remark-soft-breaks.ts b/apps/desktop/src/lib/remark-soft-breaks.tsnew file mode 100644index 00000000000..c4a17a90789--- /dev/null+++ b/apps/desktop/src/lib/remark-soft-breaks.ts@@ -0,0 +1,35 @@+export interface MdastNode {+ children?: MdastNode[]+ type: string+ value?: string+}++/** Render single newlines in prose as line breaks, the way chat replies read+ * (poems, addresses, one-item-per-line lists). Only `text` nodes are split,+ * so code, inline code and math keep their own whitespace. */+export function remarkSoftBreaks() {+ return (tree: MdastNode) => {+ const visit = (node: MdastNode) => {+ if (!node.children) {+ return+ }++ node.children = node.children.flatMap(child => {+ if (child.type !== 'text' || !child.value?.includes('\n')) {+ visit(child)++ return [child]+ }++ return child.value+ .split(/[ \t]*\r?\n[ \t]*/)+ .flatMap((value, index): MdastNode[] => [+ ...(index > 0 ? [{ type: 'break' }] : []),+ ...(value ? [{ type: 'text', value }] : [])+ ])+ })+ }++ visit(tree)+ }+}