mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-05 16:23:36 +00:00
Fix tool calls, remove dead code
This commit is contained in:
1 parent
efb5bef74c
commit
2525814144
4 files changed
+22
-235
No files matched your search
@@ -1,8 +1,7 @@
|
||||
import { memo } from 'react';
|
||||
import ReactMarkdown from 'react-markdown';
|
||||
import remarkGfm from 'remark-gfm';
|
||||
import { Code } from 'lucide-react';
|
||||
import { Card, CardContent } from '@/components/ui/card';
|
||||
import { Response } from '@/components/ai-elements/response';
|
||||
import { DiffRenderer } from '@/components/ai/DiffRenderer';
|
||||
import { ScriptRunner } from '@/components/ai/ScriptRunner';
|
||||
import { FileViewer } from '@/components/ai/FileViewer';
|
||||
@@ -10,22 +9,19 @@ import { FileWriter } from '@/components/ai/FileWriter';
|
||||
import { ShellCommand } from '@/components/ai/ShellCommand';
|
||||
import { PackageManager } from '@/components/ai/PackageManager';
|
||||
import { GitCommit } from '@/components/ai/GitCommit';
|
||||
import type { CoreAssistantMessage, CoreToolMessage } from 'ai';
|
||||
import type { AssistantModelMessage } from 'ai';
|
||||
|
||||
interface AssistantContentProps {
|
||||
content: CoreAssistantMessage['content'];
|
||||
toolResults?: CoreToolMessage[];
|
||||
content: AssistantModelMessage['content'];
|
||||
}
|
||||
|
||||
export const AssistantContent = memo(({ content, toolResults = [] }: AssistantContentProps) => {
|
||||
export const AssistantContent = memo(({ content }: AssistantContentProps) => {
|
||||
// Helper function to find tool result by call ID
|
||||
const findToolResult = (toolCallId: string) => {
|
||||
for (const toolMessage of toolResults) {
|
||||
if (toolMessage.content) {
|
||||
for (const resultPart of toolMessage.content) {
|
||||
if (resultPart.toolCallId === toolCallId) {
|
||||
return resultPart;
|
||||
}
|
||||
if (Array.isArray(content)) {
|
||||
for (const part of content) {
|
||||
if (part.type === 'tool-result' && part.toolCallId === toolCallId) {
|
||||
return part;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -41,9 +37,9 @@ export const AssistantContent = memo(({ content, toolResults = [] }: AssistantCo
|
||||
}
|
||||
|
||||
// Handle direct string results
|
||||
if (toolResult.output.type === 'text') {
|
||||
if (typeof toolResult.output === 'string') {
|
||||
return {
|
||||
result: toolResult.output.value,
|
||||
result: toolResult.output,
|
||||
isError: false
|
||||
};
|
||||
}
|
||||
@@ -54,47 +50,7 @@ export const AssistantContent = memo(({ content, toolResults = [] }: AssistantCo
|
||||
if (typeof content === 'string') {
|
||||
return (
|
||||
<div className="prose prose-sm max-w-none dark:prose-invert">
|
||||
<ReactMarkdown
|
||||
remarkPlugins={[remarkGfm]}
|
||||
components={{
|
||||
// Customize code blocks to match the existing tool styling
|
||||
pre: ({ children }) => (
|
||||
<pre className="bg-muted/50 rounded p-3 overflow-x-auto text-sm">
|
||||
{children}
|
||||
</pre>
|
||||
),
|
||||
code: ({ children, className }) => {
|
||||
const isInline = !className;
|
||||
if (isInline) {
|
||||
return (
|
||||
<code className="bg-muted px-1.5 py-0.5 rounded text-sm font-mono">
|
||||
{children}
|
||||
</code>
|
||||
);
|
||||
}
|
||||
return <code className={className}>{children}</code>;
|
||||
},
|
||||
// Style links
|
||||
a: ({ href, children }) => (
|
||||
<a
|
||||
href={href}
|
||||
target="_blank"
|
||||
rel="noopener noreferrer"
|
||||
className="text-primary hover:underline"
|
||||
>
|
||||
{children}
|
||||
</a>
|
||||
),
|
||||
// Style blockquotes
|
||||
blockquote: ({ children }) => (
|
||||
<blockquote className="border-l-4 border-muted-foreground/20 pl-4 italic text-muted-foreground">
|
||||
{children}
|
||||
</blockquote>
|
||||
),
|
||||
}}
|
||||
>
|
||||
{content}
|
||||
</ReactMarkdown>
|
||||
<Response>{content}</Response>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -105,47 +61,7 @@ export const AssistantContent = memo(({ content, toolResults = [] }: AssistantCo
|
||||
if (item.type === 'text') {
|
||||
return (
|
||||
<div key={index} className="prose prose-sm max-w-none dark:prose-invert">
|
||||
<ReactMarkdown
|
||||
remarkPlugins={[remarkGfm]}
|
||||
components={{
|
||||
// Customize code blocks to match the existing tool styling
|
||||
pre: ({ children }) => (
|
||||
<pre className="bg-muted/50 rounded p-3 overflow-x-auto text-sm">
|
||||
{children}
|
||||
</pre>
|
||||
),
|
||||
code: ({ children, className }) => {
|
||||
const isInline = !className;
|
||||
if (isInline) {
|
||||
return (
|
||||
<code className="bg-muted px-1.5 py-0.5 rounded text-sm font-mono">
|
||||
{children}
|
||||
</code>
|
||||
);
|
||||
}
|
||||
return <code className={className}>{children}</code>;
|
||||
},
|
||||
// Style links
|
||||
a: ({ href, children }) => (
|
||||
<a
|
||||
href={href}
|
||||
target="_blank"
|
||||
rel="noopener noreferrer"
|
||||
className="text-primary hover:underline"
|
||||
>
|
||||
{children}
|
||||
</a>
|
||||
),
|
||||
// Style blockquotes
|
||||
blockquote: ({ children }) => (
|
||||
<blockquote className="border-l-4 border-muted-foreground/20 pl-4 italic text-muted-foreground">
|
||||
{children}
|
||||
</blockquote>
|
||||
),
|
||||
}}
|
||||
>
|
||||
{item.text}
|
||||
</ReactMarkdown>
|
||||
<Response>{item.text}</Response>
|
||||
</div>
|
||||
);
|
||||
} else if (item.type === 'tool-call') {
|
||||
|
||||
@@ -1,74 +0,0 @@
|
||||
import { memo } from 'react';
|
||||
import { Avatar, AvatarFallback, AvatarImage } from '@/components/ui/avatar';
|
||||
import { AssistantContent } from '@/components/ai/AssistantContent';
|
||||
import { Bot, User } from 'lucide-react';
|
||||
import type { CoreUserMessage, CoreAssistantMessage, CoreToolMessage } from 'ai';
|
||||
|
||||
interface MessageItemProps {
|
||||
message: (CoreUserMessage | CoreAssistantMessage | CoreToolMessage) & { id: string };
|
||||
userDisplayName?: string;
|
||||
userProfileImage?: string;
|
||||
toolResults?: CoreToolMessage[];
|
||||
}
|
||||
|
||||
export const MessageItem = memo(({
|
||||
message,
|
||||
userDisplayName = 'You',
|
||||
userProfileImage,
|
||||
toolResults = []
|
||||
}: MessageItemProps) => {
|
||||
// Don't render tool messages as separate items - they're integrated into assistant messages
|
||||
if (message.role === 'tool') {
|
||||
return null;
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="flex gap-3">
|
||||
<Avatar className="h-8 w-8 flex-shrink-0">
|
||||
{message.role === 'user' ? (
|
||||
<>
|
||||
<AvatarImage src={userProfileImage} alt={userDisplayName} />
|
||||
<AvatarFallback>
|
||||
<User className="h-4 w-4" />
|
||||
</AvatarFallback>
|
||||
</>
|
||||
) : (
|
||||
<>
|
||||
<AvatarFallback>
|
||||
<Bot className="h-4 w-4" />
|
||||
</AvatarFallback>
|
||||
</>
|
||||
)}
|
||||
</Avatar>
|
||||
<div className="flex-1 min-w-0">
|
||||
<div className="flex items-center gap-2 mb-1">
|
||||
<span className="font-medium text-sm">
|
||||
{message.role === 'user' ? userDisplayName : 'Assistant'}
|
||||
</span>
|
||||
<span className="text-xs text-muted-foreground">
|
||||
{message.role === 'user' ? 'User' : 'AI'}
|
||||
</span>
|
||||
</div>
|
||||
<div className="text-sm">
|
||||
{message.role === 'user' ? (
|
||||
<div className="whitespace-pre-wrap break-words">
|
||||
{typeof message.content === 'string' ? message.content :
|
||||
message.content.map((part, _i) => {
|
||||
if (part.type === 'text') return part.text;
|
||||
return null;
|
||||
}).join('')
|
||||
}
|
||||
</div>
|
||||
) : message.role === 'assistant' ? (
|
||||
<AssistantContent
|
||||
content={(message as CoreAssistantMessage).content}
|
||||
toolResults={toolResults}
|
||||
/>
|
||||
) : null}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
});
|
||||
|
||||
MessageItem.displayName = 'MessageItem';
|
||||
@@ -2,7 +2,6 @@ import { memo } from 'react';
|
||||
import { Avatar, AvatarFallback, AvatarImage } from '@/components/ui/avatar';
|
||||
import { Bot, User, Square } from 'lucide-react';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { Response } from '@/components/ai-elements/response';
|
||||
import { ChatMessage } from '@/hooks/useStreamingChat';
|
||||
import { AssistantContent } from '@/components/ai/AssistantContent';
|
||||
|
||||
@@ -73,67 +72,7 @@ export const StreamingMessageItem = memo(({
|
||||
''}
|
||||
</div>
|
||||
) : message.role === 'assistant' ? (
|
||||
<div>
|
||||
{/* Handle assistant content */}
|
||||
{typeof message.content === 'string' ? (
|
||||
<div className="mb-2 prose prose-sm max-w-none dark:prose-invert">
|
||||
<Response>{message.content}</Response>
|
||||
{message.isStreaming && (
|
||||
<span className="inline-block w-2 h-4 bg-current animate-pulse ml-1" />
|
||||
)}
|
||||
</div>
|
||||
) : Array.isArray(message.content) ? (
|
||||
<div className="space-y-2">
|
||||
{message.content.map((part, index) => {
|
||||
if (part.type === 'text') {
|
||||
return (
|
||||
<div className="mb-2 prose prose-sm max-w-none dark:prose-invert">
|
||||
<Response>{part.text}</Response>
|
||||
{message.isStreaming && (
|
||||
<span className="inline-block w-2 h-4 bg-current animate-pulse ml-1" />
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
} else if (part.type === 'tool-call') {
|
||||
return (
|
||||
<AssistantContent
|
||||
key={index}
|
||||
content={[part]}
|
||||
toolResults={[]}
|
||||
/>
|
||||
);
|
||||
} else if (part.type === 'tool-result') {
|
||||
return (
|
||||
<AssistantContent
|
||||
key={index}
|
||||
content={[]}
|
||||
toolResults={[{
|
||||
role: 'tool',
|
||||
content: [part]
|
||||
}]}
|
||||
/>
|
||||
);
|
||||
}
|
||||
return null;
|
||||
})}
|
||||
{message.isStreaming && (
|
||||
<span className="inline-block w-2 h-4 bg-current animate-pulse ml-1" />
|
||||
)}
|
||||
</div>
|
||||
) : (
|
||||
/* Streaming indicator for when there's no content yet */
|
||||
message.isStreaming && (
|
||||
<div className="flex items-center gap-2 text-muted-foreground">
|
||||
<div className="flex space-x-1">
|
||||
<div className="h-1 w-1 bg-current rounded-full animate-bounce [animation-delay:-0.3s]"></div>
|
||||
<div className="h-1 w-1 bg-current rounded-full animate-bounce [animation-delay:-0.15s]"></div>
|
||||
<div className="h-1 w-1 bg-current rounded-full animate-bounce"></div>
|
||||
</div>
|
||||
<span className="text-xs">Thinking...</span>
|
||||
</div>
|
||||
)
|
||||
)}
|
||||
</div>
|
||||
<AssistantContent content={message.content} />
|
||||
) : message.role === 'system' ? (
|
||||
<div className="whitespace-pre-wrap break-words text-muted-foreground">
|
||||
{typeof message.content === 'string' ? message.content : ''}
|
||||
|
||||
@@ -33,7 +33,7 @@ export function useStreamingChat({
|
||||
const [messages, setMessages] = useState<ChatMessage[]>([]);
|
||||
const [isStreaming, setIsStreaming] = useState(false);
|
||||
const [currentStreamingMessageId, setCurrentStreamingMessageId] = useState<string | null>(null);
|
||||
const [sessionName, setSessionName] = useState<string>('');
|
||||
const [sessionName, setSessionName] = useState<string>(DotAI.generateSessionName());
|
||||
const [loadedProjectId, setLoadedProjectId] = useState<string | null>(null);
|
||||
const abortControllerRef = useRef<AbortController | null>(null);
|
||||
const { settings, isConfigured } = useAISettings();
|
||||
@@ -67,7 +67,7 @@ export function useStreamingChat({
|
||||
|
||||
// Clear messages when switching to a new project
|
||||
setMessages([]);
|
||||
setSessionName('');
|
||||
setSessionName(DotAI.generateSessionName());
|
||||
|
||||
try {
|
||||
const historyDir = `/projects/${projectId}/.ai/history`;
|
||||
@@ -134,7 +134,13 @@ export function useStreamingChat({
|
||||
|
||||
// Save message to history
|
||||
const saveMessageToHistory = useCallback(async (message: ChatMessage) => {
|
||||
if (!dotAIRef.current || !sessionName) return;
|
||||
if (!dotAIRef.current || !sessionName) {
|
||||
console.error("Unable to save message to history:", {
|
||||
dotAIRef,
|
||||
sessionName,
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
// Convert ChatMessage to DotAI format
|
||||
|
||||
Reference in new issue
Block a user