Spaces:
Sleeping
Sleeping
File size: 14,691 Bytes
6aeba60 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 | /**
* RightPanel β Manus-style tabbed right panel.
* Tabs: Actions (tool timeline), Files (explorer), Preview (live viewport).
*/
import { useState, useCallback, useEffect, useRef } from "react";
import {
Activity,
FolderTree,
Monitor,
Folder,
File,
ChevronRight,
ChevronDown,
RefreshCw,
Home,
ArrowUp,
FileText,
FileCode,
FileJson,
Image as ImageIcon,
Loader2,
ExternalLink,
} from "lucide-react";
import { ActionTree } from "./ActionTree";
import type { ChatMessage } from "@/hooks/useChat";
import { cn } from "@/lib/utils";
// βββ Tab definitions ββββββββββββββββββββββββββββββββββββββββββββββββββ
type TabId = "actions" | "files" | "preview";
const TABS: { id: TabId; label: string; icon: typeof Activity }[] = [
{ id: "actions", label: "Actions", icon: Activity },
{ id: "files", label: "Files", icon: FolderTree },
{ id: "preview", label: "Preview", icon: Monitor },
];
// βββ File Explorer types ββββββββββββββββββββββββββββββββββββββββββββββ
interface FileNode {
name: string;
path: string;
isDirectory: boolean;
size?: number;
}
function getFileIcon(name: string) {
const ext = name.split(".").pop()?.toLowerCase() || "";
if (["ts", "tsx", "js", "jsx", "py", "rs", "go", "java", "c", "cpp", "rb", "sh"].includes(ext))
return FileCode;
if (["json", "yaml", "yml", "toml", "xml", "env"].includes(ext))
return FileJson;
if (["png", "jpg", "jpeg", "gif", "svg", "webp", "ico"].includes(ext))
return ImageIcon;
if (["md", "txt", "log", "csv"].includes(ext))
return FileText;
return File;
}
function formatSize(bytes?: number): string {
if (bytes === undefined) return "";
if (bytes < 1024) return `${bytes}B`;
if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)}K`;
return `${(bytes / (1024 * 1024)).toFixed(1)}M`;
}
// βββ Inline File Explorer βββββββββββββββββββββββββββββββββββββββββββββ
function FileExplorer() {
const [currentPath, setCurrentPath] = useState("/home/user");
const [files, setFiles] = useState<FileNode[]>([]);
const [loading, setLoading] = useState(false);
const [selectedFile, setSelectedFile] = useState<string | null>(null);
const [fileContent, setFileContent] = useState<string | null>(null);
const [loadingContent, setLoadingContent] = useState(false);
const fetchFiles = useCallback(async (dirPath: string) => {
setLoading(true);
try {
const response = await fetch("/api/chat/slash", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ command: "/files", args: `list ${dirPath}` }),
});
const data = await response.json();
if (data.files) {
setFiles(data.files);
} else if (data.result) {
// Parse text listing
const lines = data.result.split("\n").filter((l: string) => l.trim());
const parsed: FileNode[] = lines
.filter((l: string) => !l.startsWith("##") && !l.startsWith("---"))
.map((l: string) => {
const isDir = l.includes("[DIR]") || l.endsWith("/");
const name = l.replace("[DIR]", "").replace(/\/$/, "").trim().split(/\s+/).pop() || l.trim();
return { name, path: `${dirPath}/${name}`.replace("//", "/"), isDirectory: isDir };
})
.filter((f: FileNode) => f.name && f.name !== "." && f.name !== "..");
setFiles(parsed);
}
} catch (err) {
console.error("Failed to fetch files:", err);
setFiles([]);
} finally {
setLoading(false);
}
}, []);
const fetchFileContent = useCallback(async (filePath: string) => {
setLoadingContent(true);
try {
const response = await fetch("/api/chat/slash", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ command: "/files", args: `read ${filePath}` }),
});
const data = await response.json();
setFileContent(data.content || data.result || "Unable to read file");
} catch {
setFileContent("Error reading file");
} finally {
setLoadingContent(false);
}
}, []);
useEffect(() => {
fetchFiles(currentPath);
}, [currentPath, fetchFiles]);
const navigateUp = () => {
const parent = currentPath.split("/").slice(0, -1).join("/") || "/";
setCurrentPath(parent);
setSelectedFile(null);
setFileContent(null);
};
const handleClick = (file: FileNode) => {
if (file.isDirectory) {
setCurrentPath(file.path);
setSelectedFile(null);
setFileContent(null);
} else {
setSelectedFile(file.path);
fetchFileContent(file.path);
}
};
return (
<div className="h-full flex flex-col">
{/* Path bar */}
<div className="flex items-center gap-1 px-2 py-1.5 border-b border-border bg-secondary/20">
<button
onClick={() => { setCurrentPath("/home/user"); setSelectedFile(null); setFileContent(null); }}
className="p-1 rounded hover:bg-accent/50"
title="Home"
>
<Home className="size-3 text-muted-foreground" />
</button>
<button
onClick={navigateUp}
className="p-1 rounded hover:bg-accent/50"
title="Up"
>
<ArrowUp className="size-3 text-muted-foreground" />
</button>
<div className="flex-1 text-[10px] font-mono text-muted-foreground truncate px-1">
{currentPath}
</div>
<button
onClick={() => fetchFiles(currentPath)}
className="p-1 rounded hover:bg-accent/50"
title="Refresh"
>
<RefreshCw className={cn("size-3 text-muted-foreground", loading && "animate-spin")} />
</button>
</div>
{/* File list or content preview */}
{selectedFile && fileContent !== null ? (
<div className="flex-1 flex flex-col overflow-hidden">
<div className="flex items-center gap-1 px-2 py-1 border-b border-border bg-secondary/10">
<button
onClick={() => { setSelectedFile(null); setFileContent(null); }}
className="text-[10px] text-primary hover:underline"
>
Back
</button>
<span className="text-[10px] text-muted-foreground truncate">
{selectedFile.split("/").pop()}
</span>
</div>
<div className="flex-1 overflow-auto">
{loadingContent ? (
<div className="flex items-center justify-center h-full">
<Loader2 className="size-4 animate-spin text-muted-foreground" />
</div>
) : (
<pre className="text-[11px] font-mono p-2 whitespace-pre-wrap break-all text-foreground/80">
{fileContent.length > 10000
? fileContent.slice(0, 10000) + "\n\n[...truncated]"
: fileContent}
</pre>
)}
</div>
</div>
) : (
<div className="flex-1 overflow-y-auto">
{loading ? (
<div className="flex items-center justify-center h-32">
<Loader2 className="size-4 animate-spin text-muted-foreground" />
</div>
) : files.length === 0 ? (
<div className="flex items-center justify-center h-32 text-[11px] text-muted-foreground">
Empty directory
</div>
) : (
<div className="py-0.5">
{files
.sort((a, b) => {
if (a.isDirectory !== b.isDirectory) return a.isDirectory ? -1 : 1;
return a.name.localeCompare(b.name);
})
.map((file) => {
const Icon = file.isDirectory ? Folder : getFileIcon(file.name);
return (
<button
key={file.path}
onClick={() => handleClick(file)}
className={cn(
"flex items-center gap-1.5 w-full px-2 py-1 text-left hover:bg-accent/50 transition-colors",
selectedFile === file.path && "bg-accent"
)}
>
<Icon
className={cn(
"size-3.5 shrink-0",
file.isDirectory ? "text-amber-400" : "text-muted-foreground"
)}
/>
<span className="text-[11px] truncate flex-1">{file.name}</span>
{!file.isDirectory && file.size !== undefined && (
<span className="text-[9px] text-muted-foreground/50 font-mono">
{formatSize(file.size)}
</span>
)}
{file.isDirectory && (
<ChevronRight className="size-3 text-muted-foreground/30" />
)}
</button>
);
})}
</div>
)}
</div>
)}
</div>
);
}
// βββ Preview Panel ββββββββββββββββββββββββββββββββββββββββββββββββββββ
function PreviewPanel({ messages }: { messages: ChatMessage[] }) {
// Extract the latest tool output that could be previewed
// (bash output, web fetch results, file contents)
const latestPreviewable = (() => {
for (let i = messages.length - 1; i >= 0; i--) {
const msg = messages[i];
if (msg.role === "assistant" && msg.toolCalls) {
for (let j = msg.toolCalls.length - 1; j >= 0; j--) {
const tc = msg.toolCalls[j];
if (tc.result && !tc.isError) {
if (tc.name === "bash" || tc.name === "PowerShell") {
return { type: "terminal" as const, name: tc.name, content: tc.result, command: (() => { try { return JSON.parse(tc.arguments).command; } catch { return ""; } })() };
}
if (tc.name === "WebFetch" || tc.name === "WebSearch") {
return { type: "web" as const, name: tc.name, content: tc.result, url: (() => { try { return JSON.parse(tc.arguments).url || JSON.parse(tc.arguments).query; } catch { return ""; } })() };
}
if (tc.name === "read_file") {
return { type: "file" as const, name: tc.name, content: tc.result, path: (() => { try { return JSON.parse(tc.arguments).path; } catch { return ""; } })() };
}
}
}
}
}
return null;
})();
if (!latestPreviewable) {
return (
<div className="h-full flex flex-col items-center justify-center text-muted-foreground/40 gap-2 p-4">
<Monitor className="size-8" />
<p className="text-xs text-center">
Live preview will appear here when the agent runs commands or fetches content
</p>
</div>
);
}
return (
<div className="h-full flex flex-col">
{/* Preview header */}
<div className="flex items-center gap-2 px-3 py-1.5 border-b border-border bg-secondary/20">
{latestPreviewable.type === "terminal" && (
<>
<div className="flex gap-1">
<div className="size-2 rounded-full bg-red-500/70" />
<div className="size-2 rounded-full bg-yellow-500/70" />
<div className="size-2 rounded-full bg-green-500/70" />
</div>
<span className="text-[10px] font-mono text-muted-foreground truncate">
$ {latestPreviewable.command?.slice(0, 60)}
</span>
</>
)}
{latestPreviewable.type === "web" && (
<>
<ExternalLink className="size-3 text-muted-foreground" />
<span className="text-[10px] font-mono text-muted-foreground truncate">
{latestPreviewable.url}
</span>
</>
)}
{latestPreviewable.type === "file" && (
<>
<FileText className="size-3 text-muted-foreground" />
<span className="text-[10px] font-mono text-muted-foreground truncate">
{latestPreviewable.path}
</span>
</>
)}
</div>
{/* Preview content */}
<div className="flex-1 overflow-auto bg-[#0d1117]">
<pre className="text-[11px] font-mono p-3 whitespace-pre-wrap break-all text-green-400/90">
{latestPreviewable.content.length > 15000
? latestPreviewable.content.slice(0, 15000) + "\n\n[...truncated]"
: latestPreviewable.content}
</pre>
</div>
</div>
);
}
// βββ Main RightPanel ββββββββββββββββββββββββββββββββββββββββββββββββββ
interface RightPanelProps {
messages: ChatMessage[];
isStreaming: boolean;
}
export function RightPanel({ messages, isStreaming }: RightPanelProps) {
const [activeTab, setActiveTab] = useState<TabId>("actions");
return (
<div className="h-full flex flex-col bg-background">
{/* Tab bar */}
<div className="flex border-b border-border bg-secondary/10">
{TABS.map((tab) => {
const Icon = tab.icon;
const isActive = activeTab === tab.id;
return (
<button
key={tab.id}
onClick={() => setActiveTab(tab.id)}
className={cn(
"flex items-center gap-1.5 px-3 py-2 text-[11px] font-medium transition-colors border-b-2 -mb-px",
isActive
? "border-primary text-foreground"
: "border-transparent text-muted-foreground hover:text-foreground hover:border-border"
)}
>
<Icon className="size-3.5" />
{tab.label}
</button>
);
})}
</div>
{/* Tab content */}
<div className="flex-1 overflow-hidden">
{activeTab === "actions" && (
<ActionTree messages={messages} isStreaming={isStreaming} />
)}
{activeTab === "files" && (
<FileExplorer />
)}
{activeTab === "preview" && (
<PreviewPanel messages={messages} />
)}
</div>
</div>
);
}
|