mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-05 16:23:36 +00:00
Fix handling of empty assistant messages
This commit is contained in:
3 files changed
+23
-47
No files matched your search
@@ -16,14 +16,12 @@ interface AIMessageItemProps {
|
||||
message: AIMessage;
|
||||
isCurrentlyLoading?: boolean;
|
||||
toolCall?: OpenAI.Chat.Completions.ChatCompletionMessageToolCall | undefined; // Tool call data passed from the assistant message
|
||||
reasoningContent?: string; // Reasoning content for streaming messages
|
||||
}
|
||||
|
||||
export const AIMessageItem = memo(({
|
||||
message,
|
||||
isCurrentlyLoading = false,
|
||||
toolCall,
|
||||
reasoningContent
|
||||
}: AIMessageItemProps) => {
|
||||
const [isToolExpanded, setIsToolExpanded] = useState(false);
|
||||
const [isReasoningExpanded, setIsReasoningExpanded] = useState(false);
|
||||
@@ -233,16 +231,19 @@ export const AIMessageItem = memo(({
|
||||
);
|
||||
}
|
||||
|
||||
// If there's no reasoning content and the main content is empty, render nothing
|
||||
if (!hasReasoningContent(message) && !getContent().trim()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Assistant messages: left-aligned without avatar/name
|
||||
return (
|
||||
<div className="flex">
|
||||
<div className="flex-1 min-w-0">
|
||||
<div className="text-sm">
|
||||
<div className="text-sm space-y-3">
|
||||
{/* Reasoning content display */}
|
||||
{((reasoningContent && reasoningContent.trim()) ||
|
||||
(hasReasoningContent(message) && message.reasoning_content.trim()) ||
|
||||
(reasoningContent !== undefined && !getContent().trim() && isCurrentlyLoading)) && (
|
||||
<div className="mb-3">
|
||||
{hasReasoningContent(message) && message.reasoning_content.trim() && (
|
||||
<div>
|
||||
<button
|
||||
onClick={() => setIsReasoningExpanded(!isReasoningExpanded)}
|
||||
className={cn(
|
||||
@@ -250,7 +251,7 @@ export const AIMessageItem = memo(({
|
||||
"hover:bg-muted/30 rounded transition-colors duration-200"
|
||||
)}
|
||||
>
|
||||
{reasoningContent !== undefined && !getContent().trim() && isCurrentlyLoading ? (
|
||||
{!getContent().trim() && isCurrentlyLoading ? (
|
||||
<Loader2 className="h-3 w-3 text-muted-foreground flex-shrink-0 animate-spin" />
|
||||
) : (
|
||||
<Lightbulb className="h-3 w-3 text-muted-foreground flex-shrink-0" />
|
||||
@@ -260,7 +261,7 @@ export const AIMessageItem = memo(({
|
||||
</span>
|
||||
</button>
|
||||
|
||||
{isReasoningExpanded && (reasoningContent?.trim() || (hasReasoningContent(message) && message.reasoning_content.trim())) && (
|
||||
{isReasoningExpanded && (
|
||||
<div className="mt-1 p-3 bg-muted/30 rounded border text-xs">
|
||||
<div className="whitespace-pre-wrap break-words">
|
||||
<Streamdown
|
||||
@@ -268,7 +269,7 @@ export const AIMessageItem = memo(({
|
||||
parseIncompleteMarkdown={isCurrentlyLoading}
|
||||
shikiTheme={displayTheme === 'dark' ? 'github-dark' : 'github-light'}
|
||||
>
|
||||
{reasoningContent || (hasReasoningContent(message) ? message.reasoning_content : '') || ''}
|
||||
{message.reasoning_content}
|
||||
</Streamdown>
|
||||
</div>
|
||||
</div>
|
||||
@@ -276,15 +277,18 @@ export const AIMessageItem = memo(({
|
||||
</div>
|
||||
)}
|
||||
|
||||
<div className="break-words">
|
||||
<Streamdown
|
||||
className='size-full [&>*:first-child]:mt-0 [&>*:last-child]:mb-0'
|
||||
parseIncompleteMarkdown={isCurrentlyLoading}
|
||||
shikiTheme={displayTheme === 'dark' ? 'github-dark' : 'github-light'}
|
||||
>
|
||||
{getContent()}
|
||||
</Streamdown>
|
||||
</div>
|
||||
{/* Main content display */}
|
||||
{getContent().trim() && (
|
||||
<div className="break-words">
|
||||
<Streamdown
|
||||
className='size-full [&>*:first-child]:mt-0 [&>*:last-child]:mb-0'
|
||||
parseIncompleteMarkdown={isCurrentlyLoading}
|
||||
shikiTheme={displayTheme === 'dark' ? 'github-dark' : 'github-light'}
|
||||
>
|
||||
{getContent()}
|
||||
</Streamdown>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Tool calls are now hidden from assistant messages */}
|
||||
</div>
|
||||
|
||||
@@ -37,7 +37,6 @@ import { toolToOpenAI } from '@/lib/tools/openai-adapter';
|
||||
import { Tool } from '@/lib/tools/Tool';
|
||||
import OpenAI from 'openai';
|
||||
import { makeSystemPrompt } from '@/lib/system';
|
||||
import { assistantContentEmpty } from '@/lib/ai-messages';
|
||||
import { saveFileToTmp } from '@/lib/fileUtils';
|
||||
import { useGit } from '@/hooks/useGit';
|
||||
|
||||
@@ -503,11 +502,6 @@ export const ChatPane = forwardRef<ChatPaneRef, ChatPaneProps>(({
|
||||
)}
|
||||
|
||||
{messages.map((message, index) => {
|
||||
// Skip assistant messages with no content
|
||||
if (message.role === 'assistant' && assistantContentEmpty(message.content)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Find the corresponding tool call for tool messages
|
||||
let toolCall: OpenAI.Chat.Completions.ChatCompletionMessageToolCall | undefined = undefined;
|
||||
if (message.role === 'tool') {
|
||||
@@ -535,7 +529,6 @@ export const ChatPane = forwardRef<ChatPaneRef, ChatPaneProps>(({
|
||||
key="streaming-message"
|
||||
message={streamingMessage}
|
||||
isCurrentlyLoading={isLoading}
|
||||
reasoningContent={streamingMessage.reasoning_content}
|
||||
/>
|
||||
) : (
|
||||
!(streamingMessage?.tool_calls?.[0]?.type === 'function') && (
|
||||
|
||||
@@ -1,21 +0,0 @@
|
||||
import OpenAI from "openai";
|
||||
|
||||
export function assistantContentEmpty(
|
||||
content: OpenAI.ChatCompletionAssistantMessageParam['content'],
|
||||
): boolean {
|
||||
if (!content) return true;
|
||||
const text: string[] = [];
|
||||
|
||||
if (typeof content === 'string') {
|
||||
text.push(content);
|
||||
} else if (Array.isArray(content)) {
|
||||
for (const part of content) {
|
||||
if (part.type === 'text') {
|
||||
text.push(part.text);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const result = text.join('\n\n');
|
||||
return !result.trim();
|
||||
}
|
||||
Reference in new issue
Block a user