suna/frontend/src/components/thread/tool-call-side-panel.tsx

254 lines
7.9 KiB
TypeScript
Raw Normal View History

2025-04-16 15:16:38 +08:00
import { Button } from "@/components/ui/button";
2025-04-19 01:21:48 +08:00
import { X } from "lucide-react";
2025-04-17 08:43:33 +08:00
import { Project } from "@/lib/api";
2025-04-18 18:50:39 +08:00
import { getToolIcon } from "@/components/thread/utils";
import React from "react";
2025-04-18 22:29:27 +08:00
import { Slider } from "@/components/ui/slider";
2025-04-16 15:16:38 +08:00
2025-04-19 01:21:48 +08:00
// Import tool view components from the tool-views directory
import { CommandToolView } from "./tool-views/CommandToolView";
import { StrReplaceToolView } from "./tool-views/StrReplaceToolView";
import { GenericToolView } from "./tool-views/GenericToolView";
2025-04-19 01:38:55 +08:00
import { FileOperationToolView } from "./tool-views/FileOperationToolView";
2025-04-19 01:21:48 +08:00
import { BrowserToolView } from "./tool-views/BrowserToolView";
import { WebSearchToolView } from "./tool-views/WebSearchToolView";
import { WebCrawlToolView } from "./tool-views/WebCrawlToolView";
2025-04-18 20:28:11 +08:00
// Simple input interface
export interface ToolCallInput {
assistantCall: {
content?: string;
name?: string;
2025-04-18 21:42:23 +08:00
timestamp?: string;
2025-04-18 20:28:11 +08:00
};
toolResult?: {
content?: string;
isSuccess?: boolean;
2025-04-18 21:42:23 +08:00
timestamp?: string;
2025-04-18 20:28:11 +08:00
};
2025-04-17 08:43:33 +08:00
}
2025-04-19 01:21:48 +08:00
// Get the specialized tool view component based on the tool name
function getToolView(
toolName: string | undefined,
assistantContent: string | undefined,
toolContent: string | undefined,
assistantTimestamp: string | undefined,
toolTimestamp: string | undefined,
isSuccess: boolean = true,
project?: Project
) {
if (!toolName) return null;
2025-04-17 08:43:33 +08:00
2025-04-19 01:21:48 +08:00
const normalizedToolName = toolName.toLowerCase();
switch (normalizedToolName) {
case 'execute-command':
return (
<CommandToolView
assistantContent={assistantContent}
toolContent={toolContent}
assistantTimestamp={assistantTimestamp}
toolTimestamp={toolTimestamp}
isSuccess={isSuccess}
/>
);
case 'str-replace':
return (
<StrReplaceToolView
assistantContent={assistantContent}
toolContent={toolContent}
assistantTimestamp={assistantTimestamp}
toolTimestamp={toolTimestamp}
isSuccess={isSuccess}
/>
);
case 'create-file':
case 'full-file-rewrite':
2025-04-19 01:38:55 +08:00
case 'delete-file':
2025-04-19 01:21:48 +08:00
return (
2025-04-19 01:38:55 +08:00
<FileOperationToolView
2025-04-19 01:21:48 +08:00
assistantContent={assistantContent}
toolContent={toolContent}
assistantTimestamp={assistantTimestamp}
toolTimestamp={toolTimestamp}
isSuccess={isSuccess}
2025-04-19 01:38:55 +08:00
name={normalizedToolName}
2025-04-19 01:21:48 +08:00
/>
);
2025-04-19 01:38:55 +08:00
case 'browser-navigate':
case 'browser-click':
case 'browser-extract':
case 'browser-fill':
case 'browser-wait':
2025-04-19 01:21:48 +08:00
return (
2025-04-19 01:38:55 +08:00
<BrowserToolView
name={normalizedToolName}
2025-04-19 01:21:48 +08:00
assistantContent={assistantContent}
toolContent={toolContent}
assistantTimestamp={assistantTimestamp}
toolTimestamp={toolTimestamp}
isSuccess={isSuccess}
2025-04-19 01:38:55 +08:00
project={project}
2025-04-19 01:21:48 +08:00
/>
);
case 'web-search':
return (
<WebSearchToolView
assistantContent={assistantContent}
toolContent={toolContent}
assistantTimestamp={assistantTimestamp}
toolTimestamp={toolTimestamp}
isSuccess={isSuccess}
/>
);
2025-04-19 01:38:55 +08:00
case 'web-crawl':
2025-04-19 01:21:48 +08:00
return (
<WebCrawlToolView
assistantContent={assistantContent}
toolContent={toolContent}
assistantTimestamp={assistantTimestamp}
toolTimestamp={toolTimestamp}
isSuccess={isSuccess}
/>
);
default:
// Check if it's a browser operation
if (normalizedToolName.startsWith('browser-')) {
return (
<BrowserToolView
name={toolName}
assistantContent={assistantContent}
toolContent={toolContent}
assistantTimestamp={assistantTimestamp}
toolTimestamp={toolTimestamp}
isSuccess={isSuccess}
project={project}
/>
);
}
2025-04-17 08:43:33 +08:00
2025-04-19 01:21:48 +08:00
// Fallback to generic view
return (
<GenericToolView
name={toolName}
assistantContent={assistantContent}
toolContent={toolContent}
assistantTimestamp={assistantTimestamp}
toolTimestamp={toolTimestamp}
isSuccess={isSuccess}
/>
);
}
2025-04-18 06:17:48 +08:00
}
2025-04-18 20:28:11 +08:00
interface ToolCallSidePanelProps {
isOpen: boolean;
onClose: () => void;
toolCalls: ToolCallInput[];
currentIndex: number;
onNavigate: (newIndex: number) => void;
project?: Project;
2025-04-20 04:02:44 +08:00
renderAssistantMessage?: (assistantContent?: string, toolContent?: string) => React.ReactNode;
renderToolResult?: (toolContent?: string, isSuccess?: boolean) => React.ReactNode;
2025-04-18 20:28:11 +08:00
}
2025-04-17 01:04:54 +08:00
export function ToolCallSidePanel({
isOpen,
onClose,
2025-04-18 20:28:11 +08:00
toolCalls,
2025-04-17 01:04:54 +08:00
currentIndex,
2025-04-17 08:43:33 +08:00
onNavigate,
2025-04-20 04:02:44 +08:00
project,
renderAssistantMessage,
renderToolResult
2025-04-17 01:04:54 +08:00
}: ToolCallSidePanelProps) {
2025-04-18 18:50:39 +08:00
if (!isOpen) return null;
2025-04-17 08:43:33 +08:00
2025-04-18 20:28:11 +08:00
const currentToolCall = toolCalls[currentIndex];
const totalCalls = toolCalls.length;
2025-04-18 22:29:27 +08:00
const currentToolName = currentToolCall?.assistantCall?.name || 'Tool Call';
const CurrentToolIcon = getToolIcon(currentToolName === 'Tool Call' ? 'unknown' : currentToolName);
2025-04-18 20:28:11 +08:00
2025-04-20 04:02:44 +08:00
// Determine if this is a streaming tool call
const isStreaming = currentToolCall?.toolResult?.content === "STREAMING";
// Set up a pulse animation for streaming
const [dots, setDots] = React.useState('');
React.useEffect(() => {
if (!isStreaming) return;
// Create a loading animation with dots
const interval = setInterval(() => {
setDots(prev => {
if (prev === '...') return '';
return prev + '.';
});
}, 500);
return () => clearInterval(interval);
}, [isStreaming]);
2025-04-18 18:50:39 +08:00
const renderContent = () => {
2025-04-18 20:28:11 +08:00
if (!currentToolCall) {
return (
2025-04-18 22:29:27 +08:00
<div className="flex items-center justify-center h-full p-4">
<p className="text-sm text-muted-foreground text-center">No tool call details available.</p>
2025-04-18 20:28:11 +08:00
</div>
2025-04-18 18:50:39 +08:00
);
2025-04-18 06:17:48 +08:00
}
2025-04-19 01:21:48 +08:00
// Get the specific tool view based on the tool name
return getToolView(
currentToolCall.assistantCall.name,
currentToolCall.assistantCall.content,
currentToolCall.toolResult?.content,
currentToolCall.assistantCall.timestamp,
currentToolCall.toolResult?.timestamp,
2025-04-20 04:02:44 +08:00
isStreaming ? true : (currentToolCall.toolResult?.isSuccess ?? true),
2025-04-19 01:21:48 +08:00
project
2025-04-18 20:28:11 +08:00
);
2025-04-18 18:50:39 +08:00
};
2025-04-16 15:16:38 +08:00
return (
2025-04-18 22:54:14 +08:00
<div className="fixed inset-y-0 right-0 w-[90%] sm:w-[450px] md:w-[500px] lg:w-[550px] xl:w-[600px] bg-background border-l flex flex-col z-10">
<div className="p-4 flex items-center justify-between">
2025-04-20 04:02:44 +08:00
<h3 className="text-sm font-semibold">
{isStreaming
? `Suna's Computer (Running${dots})`
: "Suna's Computer"}
</h3>
2025-04-18 22:29:27 +08:00
<Button variant="ghost" size="icon" onClick={onClose} className="text-muted-foreground hover:text-foreground">
2025-04-18 20:28:11 +08:00
<X className="h-4 w-4" />
</Button>
</div>
2025-04-18 18:50:39 +08:00
<div className="flex-1 overflow-auto">
{renderContent()}
2025-04-18 20:28:11 +08:00
</div>
{totalCalls > 1 && (
2025-04-18 22:29:27 +08:00
<div className="p-4 border-t bg-muted/30 space-y-3">
<div className="flex justify-between items-center gap-4">
<div className="flex items-center gap-2 min-w-0">
<CurrentToolIcon className="h-4 w-4 text-muted-foreground flex-shrink-0" />
<span className="text-xs font-medium text-foreground truncate" title={currentToolName}>
2025-04-20 04:02:44 +08:00
{currentToolName} {isStreaming && `(Running${dots})`}
2025-04-18 22:29:27 +08:00
</span>
</div>
<span className="text-xs text-muted-foreground flex-shrink-0">
Step {currentIndex + 1} of {totalCalls}
</span>
</div>
<Slider
min={0}
max={totalCalls - 1}
step={1}
value={[currentIndex]}
onValueChange={([newValue]) => onNavigate(newValue)}
className="w-full [&>span:first-child]:h-1.5 [&>span:first-child>span]:h-1.5"
/>
2025-04-18 20:28:11 +08:00
</div>
2025-04-16 15:16:38 +08:00
)}
</div>
);
}