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

180 lines
5.4 KiB
TypeScript
Raw Normal View History

2025-04-16 15:16:38 +08:00
import { Button } from "@/components/ui/button";
2025-04-18 20:28:11 +08:00
import { X, SkipBack, SkipForward } 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-16 15:16:38 +08:00
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-18 21:42:23 +08:00
// Helper function to format timestamp
function formatTimestamp(isoString?: string): string {
if (!isoString) return 'No timestamp';
try {
return new Date(isoString).toLocaleString();
} catch (e) {
return 'Invalid date';
}
}
2025-04-18 18:50:39 +08:00
// Simplified generic tool view
2025-04-18 21:42:23 +08:00
function GenericToolView({
name,
assistantContent,
toolContent,
isSuccess = true,
assistantTimestamp,
toolTimestamp
}: {
2025-04-18 18:50:39 +08:00
name?: string;
assistantContent?: string;
2025-04-18 20:28:11 +08:00
toolContent?: string;
isSuccess?: boolean;
2025-04-18 21:42:23 +08:00
assistantTimestamp?: string;
toolTimestamp?: string;
2025-04-18 18:50:39 +08:00
}) {
2025-04-18 20:28:11 +08:00
const toolName = name || 'Unknown Tool';
2025-04-17 08:43:33 +08:00
return (
2025-04-18 18:50:39 +08:00
<div className="space-y-4 p-4">
<div className="flex items-center justify-between">
<div className="flex items-center gap-2">
<div className="h-8 w-8 rounded-full bg-muted flex items-center justify-center">
{React.createElement(getToolIcon(toolName), { className: "h-4 w-4" })}
2025-04-18 20:28:11 +08:00
</div>
2025-04-18 18:50:39 +08:00
<div>
<h4 className="text-sm font-medium">{toolName}</h4>
2025-04-18 20:28:11 +08:00
</div>
</div>
2025-04-18 18:50:39 +08:00
2025-04-18 20:28:11 +08:00
{toolContent && (
2025-04-18 18:50:39 +08:00
<div className={`px-2 py-1 rounded-full text-xs ${
isSuccess ? 'bg-green-50 text-green-700' : 'bg-red-50 text-red-700'
}`}>
{isSuccess ? 'Success' : 'Failed'}
2025-04-18 20:28:11 +08:00
</div>
2025-04-17 08:43:33 +08:00
)}
</div>
2025-04-18 20:28:11 +08:00
{/* Assistant Message */}
2025-04-18 18:50:39 +08:00
<div className="space-y-1">
2025-04-18 21:42:23 +08:00
<div className="flex justify-between items-center">
<div className="text-xs font-medium text-muted-foreground">Assistant Message</div>
{assistantTimestamp && (
<div className="text-xs text-muted-foreground">{formatTimestamp(assistantTimestamp)}</div>
)}
</div>
2025-04-18 18:50:39 +08:00
<div className="rounded-md border bg-muted/50 p-3">
2025-04-18 20:28:11 +08:00
<pre className="text-xs overflow-auto whitespace-pre-wrap break-words">{assistantContent}</pre>
2025-04-17 08:43:33 +08:00
</div>
</div>
2025-04-18 20:28:11 +08:00
{/* Tool Result */}
{toolContent && (
2025-04-18 18:50:39 +08:00
<div className="space-y-1">
2025-04-18 21:42:23 +08:00
<div className="flex justify-between items-center">
<div className="text-xs font-medium text-muted-foreground">Tool Result</div>
{toolTimestamp && (
<div className="text-xs text-muted-foreground">{formatTimestamp(toolTimestamp)}</div>
)}
</div>
2025-04-18 18:50:39 +08:00
<div className={`rounded-md border p-3 ${isSuccess ? 'bg-muted/50' : 'bg-red-50'}`}>
2025-04-18 20:28:11 +08:00
<pre className="text-xs overflow-auto whitespace-pre-wrap break-words">{toolContent}</pre>
</div>
2025-04-18 06:17:48 +08:00
</div>
)}
</div>
);
}
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-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,
project
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 18:50:39 +08:00
const renderContent = () => {
2025-04-18 20:28:11 +08:00
if (!currentToolCall) {
return (
2025-04-18 18:50:39 +08:00
<div className="flex items-center justify-center h-full">
<p className="text-sm text-muted-foreground">No tool call selected</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-18 20:28:11 +08:00
return (
<GenericToolView
name={currentToolCall.assistantCall.name}
assistantContent={currentToolCall.assistantCall.content}
2025-04-18 21:42:23 +08:00
assistantTimestamp={currentToolCall.assistantCall.timestamp}
2025-04-18 20:28:11 +08:00
toolContent={currentToolCall.toolResult?.content}
isSuccess={currentToolCall.toolResult?.isSuccess}
2025-04-18 21:42:23 +08:00
toolTimestamp={currentToolCall.toolResult?.timestamp}
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 21:42:23 +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">
2025-04-18 20:28:11 +08:00
<div className="p-4 flex items-center justify-between">
2025-04-18 18:50:39 +08:00
<h3 className="text-sm font-medium">Tool Details</h3>
<Button variant="ghost" size="icon" onClick={onClose}>
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 18:50:39 +08:00
<div className="p-4 border-t flex items-center justify-between">
2025-04-18 20:28:11 +08:00
<Button
2025-04-18 18:50:39 +08:00
variant="outline"
size="sm"
2025-04-18 20:28:11 +08:00
onClick={() => onNavigate(currentIndex - 1)}
disabled={currentIndex === 0}
>
2025-04-18 18:50:39 +08:00
<SkipBack className="h-4 w-4 mr-2" /> Previous
2025-04-18 20:28:11 +08:00
</Button>
2025-04-18 18:50:39 +08:00
<span className="text-xs text-muted-foreground">
2025-04-18 20:28:11 +08:00
{currentIndex + 1} of {totalCalls}
2025-04-18 18:50:39 +08:00
</span>
2025-04-18 20:28:11 +08:00
<Button
2025-04-18 18:50:39 +08:00
variant="outline"
size="sm"
2025-04-18 20:28:11 +08:00
onClick={() => onNavigate(currentIndex + 1)}
disabled={currentIndex === totalCalls - 1}
>
2025-04-18 18:50:39 +08:00
Next <SkipForward className="h-4 w-4 ml-2" />
2025-04-18 20:28:11 +08:00
</Button>
</div>
2025-04-16 15:16:38 +08:00
)}
</div>
);
}