Fix attachment rendering

This commit is contained in:
Alex Gleason committed 2025-12-21 03:41:25 -06:00
1 parent 94d62b254d
commit d1cdcb0fd9
3 files changed
+34 -87

No files matched your search

+1 -9
View File
@@ -80,14 +80,6 @@ export const AIMessageItem = memo(({
// User messages: right-aligned bubble without avatar/name
// Extract text and image parts from message content
const contentArray = Array.isArray(message.content) ? message.content : null;
const textParts = contentArray
? contentArray.filter(part => part.type === 'text') as OpenAI.Chat.Completions.ChatCompletionContentPartText[]
: [];
const userContent = typeof message.content === 'string'
? message.content
: textParts.length > 0
? textParts.map(part => part.text).join('\n')
: '';
// Extract image URLs from content array
const imageUrls = contentArray
@@ -102,7 +94,7 @@ export const AIMessageItem = memo(({
<div className="max-w-[80%] bg-secondary rounded-2xl rounded-br-md px-4 py-3">
<div className="text-sm break-words space-y-2">
{/* Render text content and attachments first */}
{userContent && <UserMessage content={userContent} />}
<UserMessage content={message.content} />
{/* Render images after text and attachments */}
{imageUrls.length > 0 && (
<div className="space-y-2">
+32 -77
View File
@@ -3,7 +3,7 @@ import { Badge } from '@/components/ui/badge';
import type OpenAI from 'openai';
interface UserMessageProps {
content: string | Array<OpenAI.Chat.Completions.ChatCompletionContentPartText>;
content: string | Array<OpenAI.Chat.Completions.ChatCompletionContentPart>;
}
/**
@@ -13,71 +13,12 @@ interface UserMessageProps {
*/
export function UserMessage({ content }: UserMessageProps) {
// Convert content to array format for uniform processing
const contentParts = typeof content === 'string'
const parts = typeof content === 'string'
? [{ type: 'text' as const, text: content }]
: content.filter(part => part.type === 'text') as Array<{ type: 'text'; text: string }>;
// Process each content part and categorize as text or file
const processedParts: Array<{ type: 'text' | 'file'; content: string; filepath?: string }> = [];
contentParts.forEach((part, index) => {
const text = part.text.trim();
// First content part is always treated as text
if (index === 0) {
processedParts.push({ type: 'text', content: text });
return;
}
// Matches the pattern anywhere in the text
// Regular expression to match "Added file: <filename>" patterns
const filePattern = /^Added file: (\/[^\s\n]+$)/g;
// Find all file patterns in this text part
const fileMatches = [...text.matchAll(filePattern)].map(match => ({
filepath: match[1], // "/tmp/filename.txt"
index: match.index,
length: match[0].length,
}));
if (fileMatches.length > 0) {
// Split text around file patterns and process each segment
let lastIndex = 0;
fileMatches.forEach((fileMatch) => {
// Add text before the file pattern
if (fileMatch.index > lastIndex) {
const textBefore = text.substring(lastIndex, fileMatch.index).trim();
if (textBefore) {
processedParts.push({ type: 'text', content: textBefore });
}
}
// Add the file attachment badge (without showing "Added file:" text)
processedParts.push({
type: 'file',
content: '', // Empty - badge will show filename only
filepath: fileMatch.filepath
});
lastIndex = fileMatch.index + fileMatch.length;
});
// Add remaining text after the last file pattern
if (lastIndex < text.length) {
const textAfter = text.substring(lastIndex).trim();
if (textAfter) {
processedParts.push({ type: 'text', content: textAfter });
}
}
} else if (text) {
// No file patterns found, treat as regular text
processedParts.push({ type: 'text', content: text });
}
});
: content.filter(part => part.type === 'text');
// If no content, return empty
if (processedParts.length === 0) {
if (parts.length === 0) {
return null;
}
@@ -87,28 +28,42 @@ export function UserMessage({ content }: UserMessageProps) {
return (
<div className="space-y-2">
{processedParts.map((part, index) => {
{parts.map((part, index) => {
if (part.type === 'text') {
// Only render text if it has content
if (!part.content.trim()) return null;
if (!part.text.trim()) return null;
// Detect "Added file: <filename>" pattern
const fileAddedMatch = /^Added file: (\/[^\s\n]+$)/g.exec(part.text);
console.log('fileAddedMatch', part, fileAddedMatch);
if (fileAddedMatch && fileAddedMatch[1]) {
const path = fileAddedMatch[1];
const filename = getFileName(path);
return (
<div key={index} className="flex items-center gap-2 max-w-full min-w-0">
<Badge variant="secondary" className="inline-flex items-center gap-1.5 px-2 py-1 max-w-full">
<span className="flex-shrink-0">
<Paperclip className="h-3 w-3" />
</span>
<span className="text-xs font-medium truncate min-w-0" title={filename}>
{filename}
</span>
</Badge>
</div>
);
}
// Regular text rendering
return (
<div key={index} className="whitespace-pre-wrap">
{part.content}
{part.text}
</div>
);
} else {
// Render file attachment badge (no "Added file:" text)
const filename = getFileName(part.filepath!);
// Unsupported content type
return (
<div key={index} className="flex items-center gap-2 max-w-full min-w-0">
<Badge variant="secondary" className="inline-flex items-center gap-1.5 px-2 py-1 max-w-full">
<span className="flex-shrink-0">
<Paperclip className="h-3 w-3" />
</span>
<span className="text-xs font-medium truncate min-w-0" title={filename}>
{filename}
</span>
</Badge>
<div key={index} className="text-sm text-red-500">
[Unsupported content type]
</div>
);
}
+1 -1
View File
@@ -347,7 +347,7 @@ export class SessionManager {
if (textParts.length > 0) {
return {
...msg,
content: textParts.map(part => (part as OpenAI.Chat.Completions.ChatCompletionContentPartText).text).join('\n')
content: textParts,
};
}