Skip to content

Commit 3e1f565

Browse files
committed
adding a limit for rendering large blocks of text when pasted by the user
1 parent 70ef855 commit 3e1f565

2 files changed

Lines changed: 261 additions & 0 deletions

File tree

‎src/ui/ink/UserMessage.tsx‎

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,87 @@ export interface UserMessageProps {
1717
/** Maximum number of lines to show before collapsing */
1818
const MAX_DISPLAY_LINES = 5;
1919

20+
/** Threshold for treating text as "large pasted content" */
21+
const LARGE_TEXT_LINES_THRESHOLD = 15;
22+
const LARGE_TEXT_CHARS_THRESHOLD = 1500;
23+
24+
/**
25+
* Format byte size to human readable string
26+
*/
27+
function formatSize(bytes: number): string {
28+
if (bytes < 1024) return `${bytes}B`;
29+
if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)}KB`;
30+
return `${(bytes / (1024 * 1024)).toFixed(1)}MB`;
31+
}
32+
33+
/**
34+
* Detect if text looks like pasted content (code block, log, etc.)
35+
*/
36+
function detectContentType(text: string): string {
37+
const trimmed = text.trim();
38+
39+
// Check for code blocks
40+
if (trimmed.startsWith('```') || trimmed.includes('\n```')) {
41+
return 'Code block';
42+
}
43+
44+
// Check for JSON
45+
if ((trimmed.startsWith('{') && trimmed.endsWith('}')) ||
46+
(trimmed.startsWith('[') && trimmed.endsWith(']'))) {
47+
try {
48+
JSON.parse(trimmed);
49+
return 'JSON';
50+
} catch {
51+
// Not valid JSON
52+
}
53+
}
54+
55+
// Check for stack trace
56+
if (trimmed.includes('at ') && trimmed.includes(':') &&
57+
(trimmed.includes('Error:') || trimmed.includes('Exception:') ||
58+
trimmed.includes('\n at '))) {
59+
return 'Stack trace';
60+
}
61+
62+
// Check for log output
63+
const lines = trimmed.split('\n');
64+
const logPattern = /^\d{4}-\d{2}-\d{2}|^\[\d{4}-\d{2}-\d{2}|^\d{2}:\d{2}:\d{2}|^\[INFO\]|^\[WARN\]|^\[ERROR\]|^\[DEBUG\]/;
65+
const logLines = lines.filter(l => logPattern.test(l.trim()));
66+
if (logLines.length > lines.length * 0.5 && lines.length > 3) {
67+
return 'Log output';
68+
}
69+
70+
// Check for diff/patch
71+
if (trimmed.startsWith('diff --git') ||
72+
(trimmed.includes('\n--- ') && trimmed.includes('\n+++ '))) {
73+
return 'Diff';
74+
}
75+
76+
return 'Text';
77+
}
78+
2079
/**
2180
* UserMessage displays a user's prompt with a styled background.
2281
* Similar to how Codex displays user messages with a light gray background.
2382
*
2483
* Features:
2584
* - Full-width background using space padding
2685
* - Compacts long messages to max 5 lines with "..." indicator
86+
* - Renders large pasted content as a compact bordered box
2787
*/
2888
function UserMessageComponent({ children, isQueued = false }: UserMessageProps) {
2989
const { colors } = useTheme();
3090
const { stdout } = useStdout();
3191

3292
const terminalWidth = stdout?.columns ?? 80;
3393

94+
// Check if this is large text that should be compacted
95+
const isLargeText = useMemo(() => {
96+
const lines = children.split('\n');
97+
const charCount = children.length;
98+
return lines.length > LARGE_TEXT_LINES_THRESHOLD || charCount > LARGE_TEXT_CHARS_THRESHOLD;
99+
}, [children]);
100+
34101
// Process message: wrap to terminal width and limit to max lines
35102
const displayLines = useMemo(() => {
36103
const prefix = isQueued ? '(queued) ' : '';
@@ -68,6 +135,61 @@ function UserMessageComponent({ children, isQueued = false }: UserMessageProps)
68135
return wrappedLines;
69136
}, [children, isQueued, terminalWidth]);
70137

138+
// Compact display for large pasted content
139+
const compactInfo = useMemo(() => {
140+
if (!isLargeText) return null;
141+
142+
const lines = children.split('\n');
143+
const charCount = children.length;
144+
const byteSize = Buffer.byteLength(children, 'utf-8');
145+
const contentType = detectContentType(children);
146+
147+
return {
148+
lines: lines.length,
149+
size: formatSize(byteSize),
150+
chars: charCount,
151+
type: contentType,
152+
};
153+
}, [children, isLargeText]);
154+
155+
// Render compact box for large text
156+
if (isLargeText && compactInfo) {
157+
const prefix = isQueued ? '(queued) ' : '';
158+
const label = `${prefix}${compactInfo.type}`;
159+
const stats = `${compactInfo.lines} lines, ${compactInfo.size}`;
160+
161+
return (
162+
<Box
163+
marginTop={1}
164+
flexDirection="column"
165+
borderStyle="round"
166+
borderColor={colors.userMessageBg || '#757575'}
167+
paddingX={1}
168+
>
169+
<Box>
170+
<Text
171+
color={colors.userMessageText || '#f5f5f5'}
172+
backgroundColor={colors.userMessageBg || '#757575'}
173+
bold
174+
>
175+
{' '}
176+
{label}
177+
{' '}
178+
</Text>
179+
<Text dimColor>
180+
{' '}
181+
{stats}
182+
</Text>
183+
</Box>
184+
<Box marginTop={1}>
185+
<Text dimColor italic>
186+
Content sent to assistant (collapsed for readability)
187+
</Text>
188+
</Box>
189+
</Box>
190+
);
191+
}
192+
71193
return (
72194
<Box marginTop={1} flexDirection="column">
73195
{displayLines.map((line, idx) => (

‎tests/ui/UserMessage.test.tsx‎

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
/**
2+
* @license
3+
* Copyright 2025 Autohand AI LLC
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
import { describe, it, expect } from 'bun:test';
8+
import React from 'react';
9+
import { render } from 'ink-testing-library';
10+
import { UserMessage } from '../../src/ui/ink/UserMessage.js';
11+
import { ThemeProvider } from '../../src/ui/theme/ThemeContext.js';
12+
import { I18nProvider } from '../../src/ui/i18n/index.js';
13+
14+
function renderWithProviders(element: React.ReactElement) {
15+
return render(
16+
<I18nProvider>
17+
<ThemeProvider>
18+
{element}
19+
</ThemeProvider>
20+
</I18nProvider>
21+
);
22+
}
23+
24+
describe('UserMessage', () => {
25+
describe('normal messages', () => {
26+
it('renders short messages with full background', () => {
27+
const { lastFrame } = renderWithProviders(<UserMessage>Hello world</UserMessage>);
28+
const output = lastFrame();
29+
expect(output).toContain('Hello world');
30+
});
31+
32+
it('renders queued messages with prefix', () => {
33+
const { lastFrame } = renderWithProviders(<UserMessage isQueued>Test message</UserMessage>);
34+
const output = lastFrame();
35+
expect(output).toContain('(queued)');
36+
expect(output).toContain('Test message');
37+
});
38+
});
39+
40+
describe('large text handling', () => {
41+
it('collapses text with more than 15 lines', () => {
42+
const largeText = Array(20).fill('Line of text').join('\n');
43+
const { lastFrame } = renderWithProviders(<UserMessage>{largeText}</UserMessage>);
44+
const output = lastFrame();
45+
46+
// Should show compact box, not all lines
47+
expect(output).toContain('Text');
48+
expect(output).toContain('20 lines');
49+
expect(output).toContain('collapsed for readability');
50+
});
51+
52+
it('collapses text with more than 1500 characters', () => {
53+
const largeText = 'x'.repeat(2000);
54+
const { lastFrame } = renderWithProviders(<UserMessage>{largeText}</UserMessage>);
55+
const output = lastFrame();
56+
57+
// Should show compact box
58+
expect(output).toContain('Text');
59+
expect(output).toContain('collapsed for readability');
60+
});
61+
62+
it('detects code blocks', () => {
63+
const codeBlock = '```javascript\n' + Array(20).fill('const x = 1;').join('\n') + '\n```';
64+
const { lastFrame } = renderWithProviders(<UserMessage>{codeBlock}</UserMessage>);
65+
const output = lastFrame();
66+
67+
expect(output).toContain('Code block');
68+
});
69+
70+
it('detects JSON content', () => {
71+
const json = JSON.stringify({ data: Array(50).fill({ key: 'value' }) }, null, 2);
72+
const { lastFrame } = renderWithProviders(<UserMessage>{json}</UserMessage>);
73+
const output = lastFrame();
74+
75+
expect(output).toContain('JSON');
76+
});
77+
78+
it('detects stack traces', () => {
79+
const stackTrace = `Error: Something went wrong
80+
at Function.execute (file.js:10:15)
81+
at Object.<anonymous> (file.js:20:5)
82+
at Module._compile (module.js:653:30)
83+
${Array(15).fill(' at someFunction (another.js:5:10)').join('\n')}`;
84+
85+
const { lastFrame } = renderWithProviders(<UserMessage>{stackTrace}</UserMessage>);
86+
const output = lastFrame();
87+
88+
expect(output).toContain('Stack trace');
89+
});
90+
91+
it('detects log output', () => {
92+
const logs = Array(20).fill('[2024-01-15 10:30:45] [INFO] Processing request').join('\n');
93+
const { lastFrame } = renderWithProviders(<UserMessage>{logs}</UserMessage>);
94+
const output = lastFrame();
95+
96+
expect(output).toContain('Log output');
97+
});
98+
99+
it('detects diff/patch content', () => {
100+
const diff = `diff --git a/file.ts b/file.ts
101+
--- a/file.ts
102+
+++ b/file.ts
103+
@@ -1,5 +1,5 @@
104+
${Array(20).fill('+ new line').join('\n')}`;
105+
106+
const { lastFrame } = renderWithProviders(<UserMessage>{diff}</UserMessage>);
107+
const output = lastFrame();
108+
109+
expect(output).toContain('Diff');
110+
});
111+
112+
it('shows byte size for large content', () => {
113+
const largeText = 'x'.repeat(5000);
114+
const { lastFrame } = renderWithProviders(<UserMessage>{largeText}</UserMessage>);
115+
const output = lastFrame();
116+
117+
expect(output).toContain('KB');
118+
});
119+
120+
it('shows queued indicator in collapsed view', () => {
121+
const largeText = Array(20).fill('Line of text').join('\n');
122+
const { lastFrame } = renderWithProviders(<UserMessage isQueued>{largeText}</UserMessage>);
123+
const output = lastFrame();
124+
125+
expect(output).toContain('(queued)');
126+
});
127+
});
128+
129+
describe('truncation for medium messages', () => {
130+
it('truncates messages between 5 and 15 lines with ellipsis', () => {
131+
const mediumText = Array(10).fill('Line of text here').join('\n');
132+
const { lastFrame } = renderWithProviders(<UserMessage>{mediumText}</UserMessage>);
133+
const output = lastFrame();
134+
135+
// Should show truncated with ...
136+
expect(output).toContain('...');
137+
});
138+
});
139+
});

0 commit comments

Comments
 (0)