mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-06 08:37:22 +00:00
Fix attachment rendering
This commit is contained in:
3 files changed
+34
-87
No files matched your search
@@ -80,14 +80,6 @@ export const AIMessageItem = memo(({
|
|||||||
// User messages: right-aligned bubble without avatar/name
|
// User messages: right-aligned bubble without avatar/name
|
||||||
// Extract text and image parts from message content
|
// Extract text and image parts from message content
|
||||||
const contentArray = Array.isArray(message.content) ? message.content : null;
|
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
|
// Extract image URLs from content array
|
||||||
const imageUrls = contentArray
|
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="max-w-[80%] bg-secondary rounded-2xl rounded-br-md px-4 py-3">
|
||||||
<div className="text-sm break-words space-y-2">
|
<div className="text-sm break-words space-y-2">
|
||||||
{/* Render text content and attachments first */}
|
{/* Render text content and attachments first */}
|
||||||
{userContent && <UserMessage content={userContent} />}
|
<UserMessage content={message.content} />
|
||||||
{/* Render images after text and attachments */}
|
{/* Render images after text and attachments */}
|
||||||
{imageUrls.length > 0 && (
|
{imageUrls.length > 0 && (
|
||||||
<div className="space-y-2">
|
<div className="space-y-2">
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ import { Badge } from '@/components/ui/badge';
|
|||||||
import type OpenAI from 'openai';
|
import type OpenAI from 'openai';
|
||||||
|
|
||||||
interface UserMessageProps {
|
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) {
|
export function UserMessage({ content }: UserMessageProps) {
|
||||||
// Convert content to array format for uniform processing
|
// Convert content to array format for uniform processing
|
||||||
const contentParts = typeof content === 'string'
|
const parts = typeof content === 'string'
|
||||||
? [{ type: 'text' as const, text: content }]
|
? [{ type: 'text' as const, text: content }]
|
||||||
: content.filter(part => part.type === 'text') as Array<{ type: 'text'; text: string }>;
|
: content.filter(part => part.type === 'text');
|
||||||
|
|
||||||
// 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 });
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// If no content, return empty
|
// If no content, return empty
|
||||||
if (processedParts.length === 0) {
|
if (parts.length === 0) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -87,28 +28,42 @@ export function UserMessage({ content }: UserMessageProps) {
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="space-y-2">
|
<div className="space-y-2">
|
||||||
{processedParts.map((part, index) => {
|
{parts.map((part, index) => {
|
||||||
if (part.type === 'text') {
|
if (part.type === 'text') {
|
||||||
// Only render text if it has content
|
// 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 (
|
return (
|
||||||
<div key={index} className="whitespace-pre-wrap">
|
<div key={index} className="whitespace-pre-wrap">
|
||||||
{part.content}
|
{part.text}
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
// Render file attachment badge (no "Added file:" text)
|
// Unsupported content type
|
||||||
const filename = getFileName(part.filepath!);
|
|
||||||
return (
|
return (
|
||||||
<div key={index} className="flex items-center gap-2 max-w-full min-w-0">
|
<div key={index} className="text-sm text-red-500">
|
||||||
<Badge variant="secondary" className="inline-flex items-center gap-1.5 px-2 py-1 max-w-full">
|
[Unsupported content type]
|
||||||
<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>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -347,7 +347,7 @@ export class SessionManager {
|
|||||||
if (textParts.length > 0) {
|
if (textParts.length > 0) {
|
||||||
return {
|
return {
|
||||||
...msg,
|
...msg,
|
||||||
content: textParts.map(part => (part as OpenAI.Chat.Completions.ChatCompletionContentPartText).text).join('\n')
|
content: textParts,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user