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 { DiffRenderer } from '@/components/ai/DiffRenderer';
import { ScriptRunner } from '@/components/ai/ScriptRunner';
import { FileViewer } from '@/components/ai/FileViewer';
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 { BuildAction } from '@/components/ai/BuildAction';
import type { CoreAssistantMessage, CoreToolMessage } from 'ai';
interface AssistantContentProps {
content: CoreAssistantMessage['content'];
toolResults?: CoreToolMessage[];
}
export const AssistantContent = memo(({ content, toolResults = [] }: 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;
}
}
}
}
return null;
};
// Helper function to extract result text and error status
const extractToolResult = (toolCallId: string): { result?: string; isError: boolean } => {
const toolResult = findToolResult(toolCallId);
if (!toolResult) {
return { isError: false };
}
// Handle direct string results
if (typeof toolResult.result === 'string') {
return {
result: toolResult.result,
isError: toolResult.isError || false
};
}
// Handle CallToolResult structure
if (toolResult.result && typeof toolResult.result === 'object') {
const callResult = toolResult.result as {
content?: Array<{ type: string; text: string }>;
isError?: boolean;
};
if (callResult.content && Array.isArray(callResult.content) && callResult.content[0]?.text) {
return {
result: callResult.content[0].text,
isError: callResult.isError || false
};
}
}
return { isError: toolResult.isError || false };
};
// Helper function to check and render build action
const renderBuildAction = (text: string, key: number | string) => {
if (!/(β
|β|βΈοΈ|π|π|β οΈ|π).*?(Agent completed successfully|Auto-building|Build|Deploy|Auto-fix|Building|Deploying|Fixing|Preview updated)/i.test(text)) {
return null;
}
const getActionType = (): 'build' | 'deploy' | 'auto-build' | 'auto-fix' => {
if (text.includes('Auto-build') || text.includes('Auto-building') || (text.includes('Agent completed successfully') && text.includes('built'))) {
return 'auto-build';
}
if (text.includes('Auto-fix') || text.includes('Fixing')) {
return 'auto-fix';
}
if (text.includes('Deploy') || text.includes('Deploying')) {
return 'deploy';
}
return 'build';
};
const getStatus = () => {
const icon = text.match(/(β
|β|βΈοΈ|π|π|β οΈ|π)/)?.[1];
if (icon === 'β' || text.includes('failed')) {
return { isError: true, isLoading: false };
}
if (icon === 'βΈοΈ' || icon === 'π' || /(Building|Deploying|Fixing)/i.test(text)) {
return { isError: false, isLoading: true };
}
return { isError: false, isLoading: false };
};
const actionType = getActionType();
const { isError, isLoading } = getStatus();
return (
{children}
),
code: ({ children, className }) => {
const isInline = !className;
if (isInline) {
return (
{children}
);
}
return {children};
},
// Style links
a: ({ href, children }) => (
{children}
),
// Style blockquotes
blockquote: ({ children }) => (
{children}), }} > {content}
{children}
),
code: ({ children, className }) => {
const isInline = !className;
if (isInline) {
return (
{children}
);
}
return {children};
},
// Style links
a: ({ href, children }) => (
{children}
),
// Style blockquotes
blockquote: ({ children }) => (
{children}), }} > {item.text}
{String(item.toolName)}
{JSON.stringify(item.args, null, 2)}