File size: 21,865 Bytes
1dbc34b | 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 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 | import { useState, useEffect, useCallback, useRef } from 'react';
import { createLogger } from '@automaker/utils/logger';
import { useQueryClient } from '@tanstack/react-query';
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card';
const logger = createLogger('SessionManager');
import { Button } from '@/components/ui/button';
import { HotkeyButton } from '@/components/ui/hotkey-button';
import { Input } from '@/components/ui/input';
import { Tabs, TabsList, TabsTrigger } from '@/components/ui/tabs';
import {
Plus,
MessageSquare,
Archive,
Trash2,
Edit2,
Check,
X,
ArchiveRestore,
} from 'lucide-react';
import { Spinner } from '@/components/ui/spinner';
import { cn, pathsEqual } from '@/lib/utils';
import type { SessionListItem } from '@/types/electron';
import { useKeyboardShortcutsConfig } from '@/hooks/use-keyboard-shortcuts';
import { getElectronAPI } from '@/lib/electron';
import { useSessions } from '@/hooks/queries';
import { queryKeys } from '@/lib/query-keys';
import { DeleteSessionDialog } from '@/components/dialogs/delete-session-dialog';
import { DeleteAllArchivedSessionsDialog } from '@/components/dialogs/delete-all-archived-sessions-dialog';
// Random session name generator
const adjectives = [
'Swift',
'Bright',
'Clever',
'Dynamic',
'Eager',
'Focused',
'Gentle',
'Happy',
'Inventive',
'Jolly',
'Keen',
'Lively',
'Mighty',
'Noble',
'Optimal',
'Peaceful',
'Quick',
'Radiant',
'Smart',
'Tranquil',
'Unique',
'Vibrant',
'Wise',
'Zealous',
];
const nouns = [
'Agent',
'Builder',
'Coder',
'Developer',
'Explorer',
'Forge',
'Garden',
'Helper',
'Innovator',
'Journey',
'Kernel',
'Lighthouse',
'Mission',
'Navigator',
'Oracle',
'Project',
'Quest',
'Runner',
'Spark',
'Task',
'Unicorn',
'Voyage',
'Workshop',
];
function generateRandomSessionName(): string {
const adjective = adjectives[Math.floor(Math.random() * adjectives.length)];
const noun = nouns[Math.floor(Math.random() * nouns.length)];
const number = Math.floor(Math.random() * 100);
return `${adjective} ${noun} ${number}`;
}
interface SessionManagerProps {
currentSessionId: string | null;
onSelectSession: (sessionId: string | null) => void;
projectPath: string;
workingDirectory?: string; // Current worktree path for scoping sessions
isCurrentSessionThinking?: boolean;
onQuickCreateRef?: React.MutableRefObject<(() => Promise<void>) | null>;
}
export function SessionManager({
currentSessionId,
onSelectSession,
projectPath,
workingDirectory,
isCurrentSessionThinking = false,
onQuickCreateRef,
}: SessionManagerProps) {
const shortcuts = useKeyboardShortcutsConfig();
const queryClient = useQueryClient();
const [activeTab, setActiveTab] = useState<'active' | 'archived'>('active');
const [editingSessionId, setEditingSessionId] = useState<string | null>(null);
const [editingName, setEditingName] = useState('');
const [isCreating, setIsCreating] = useState(false);
const [newSessionName, setNewSessionName] = useState('');
const [runningSessions, setRunningSessions] = useState<Set<string>>(new Set());
const [isDeleteDialogOpen, setIsDeleteDialogOpen] = useState(false);
const [sessionToDelete, setSessionToDelete] = useState<SessionListItem | null>(null);
const [isDeleteAllArchivedDialogOpen, setIsDeleteAllArchivedDialogOpen] = useState(false);
// Use React Query for sessions list - always include archived, filter client-side
const { data: sessions = [], refetch: refetchSessions } = useSessions(true);
// Ref to track if we've done the initial running sessions check
const hasCheckedInitialRef = useRef(false);
// Check running state for all sessions
const checkRunningSessions = useCallback(async (sessionList: SessionListItem[]) => {
const api = getElectronAPI();
if (!api?.agent) return;
const runningIds = new Set<string>();
// Check each session's running state
for (const session of sessionList) {
try {
const result = await api.agent.getHistory(session.id);
if (result.success && result.isRunning) {
runningIds.add(session.id);
}
} catch (err) {
// Ignore errors for individual session checks
logger.warn(`Failed to check running state for ${session.id}:`, err);
}
}
setRunningSessions(runningIds);
}, []);
// Helper to invalidate sessions cache and refetch
const invalidateSessions = useCallback(async () => {
await queryClient.invalidateQueries({ queryKey: queryKeys.sessions.all(true) });
// Also check running state after invalidation
const result = await refetchSessions();
if (result.data) {
await checkRunningSessions(result.data);
}
return result;
}, [queryClient, refetchSessions, checkRunningSessions]);
// Check running state on initial load (runs only once when sessions first load)
useEffect(() => {
if (sessions.length > 0 && !hasCheckedInitialRef.current) {
hasCheckedInitialRef.current = true;
checkRunningSessions(sessions);
}
}, [sessions, checkRunningSessions]);
// Periodically check running state for sessions (useful for detecting when agents finish)
useEffect(() => {
// Only poll if there are running sessions
if (runningSessions.size === 0 && !isCurrentSessionThinking) return;
const interval = setInterval(async () => {
if (sessions.length > 0) {
await checkRunningSessions(sessions);
}
}, 3000); // Check every 3 seconds
return () => clearInterval(interval);
}, [sessions, runningSessions.size, isCurrentSessionThinking, checkRunningSessions]);
// Effective working directory for session creation (worktree path or project path)
const effectiveWorkingDirectory = workingDirectory || projectPath;
// Create new session with random name
const handleCreateSession = async () => {
const api = getElectronAPI();
if (!api?.sessions) return;
const sessionName = newSessionName.trim() || generateRandomSessionName();
const result = await api.sessions.create(sessionName, projectPath, effectiveWorkingDirectory);
if (result.success && result.session?.id) {
setNewSessionName('');
setIsCreating(false);
// Select the new session immediately before invalidating the cache to avoid
// a race condition where the cache re-render resets the selected session.
onSelectSession(result.session.id);
await invalidateSessions();
}
};
// Create new session directly with a random name (one-click)
const handleQuickCreateSession = useCallback(async () => {
const api = getElectronAPI();
if (!api?.sessions) return;
const sessionName = generateRandomSessionName();
const result = await api.sessions.create(sessionName, projectPath, effectiveWorkingDirectory);
if (result.success && result.session?.id) {
// Select the new session immediately before invalidating the cache to avoid
// a race condition where the cache re-render resets the selected session.
onSelectSession(result.session.id);
await invalidateSessions();
}
}, [effectiveWorkingDirectory, projectPath, invalidateSessions, onSelectSession]);
// Expose the quick create function via ref for keyboard shortcuts
useEffect(() => {
if (onQuickCreateRef) {
onQuickCreateRef.current = handleQuickCreateSession;
}
return () => {
if (onQuickCreateRef) {
onQuickCreateRef.current = null;
}
};
}, [onQuickCreateRef, handleQuickCreateSession]);
// Rename session
const handleRenameSession = async (sessionId: string) => {
const api = getElectronAPI();
if (!editingName.trim() || !api?.sessions) return;
const result = await api.sessions.update(sessionId, editingName, undefined);
if (result.success) {
setEditingSessionId(null);
setEditingName('');
await invalidateSessions();
}
};
// Archive session
const handleArchiveSession = async (sessionId: string) => {
const api = getElectronAPI();
if (!api?.sessions) {
logger.error('[SessionManager] Sessions API not available');
return;
}
try {
const result = await api.sessions.archive(sessionId);
if (result.success) {
// If the archived session was currently selected, deselect it
if (currentSessionId === sessionId) {
onSelectSession(null);
}
await invalidateSessions();
} else {
logger.error('[SessionManager] Archive failed:', result.error);
}
} catch (error) {
logger.error('[SessionManager] Archive error:', error);
}
};
// Unarchive session
const handleUnarchiveSession = async (sessionId: string) => {
const api = getElectronAPI();
if (!api?.sessions) {
logger.error('[SessionManager] Sessions API not available');
return;
}
try {
const result = await api.sessions.unarchive(sessionId);
if (result.success) {
await invalidateSessions();
} else {
logger.error('[SessionManager] Unarchive failed:', result.error);
}
} catch (error) {
logger.error('[SessionManager] Unarchive error:', error);
}
};
// Open delete session dialog
const handleDeleteSession = (session: SessionListItem) => {
setSessionToDelete(session);
setIsDeleteDialogOpen(true);
};
// Confirm delete session
const confirmDeleteSession = async (sessionId: string) => {
const api = getElectronAPI();
if (!api?.sessions) return;
const result = await api.sessions.delete(sessionId);
if (result.success) {
const refetchResult = await invalidateSessions();
if (currentSessionId === sessionId) {
// Switch to another session using fresh data, excluding the deleted session
// Filter to sessions within the same worktree to avoid jumping to a different worktree
const freshSessions = refetchResult?.data ?? [];
const activeSessionsList = freshSessions.filter((s) => {
if (s.isArchived || s.id === sessionId) return false;
const sessionDir = s.workingDirectory || s.projectPath;
return pathsEqual(sessionDir, effectiveWorkingDirectory);
});
if (activeSessionsList.length > 0) {
onSelectSession(activeSessionsList[0].id);
} else {
onSelectSession(null);
}
}
}
setSessionToDelete(null);
};
// Delete all archived sessions
const handleDeleteAllArchivedSessions = async () => {
const api = getElectronAPI();
if (!api?.sessions) return;
// Delete each archived session
for (const session of archivedSessions) {
await api.sessions.delete(session.id);
}
await invalidateSessions();
setIsDeleteAllArchivedDialogOpen(false);
};
// Filter sessions by current working directory (worktree scoping)
const scopedSessions = sessions.filter((s) => {
const sessionDir = s.workingDirectory || s.projectPath;
// Match sessions whose workingDirectory matches the current effective directory
// Use pathsEqual for cross-platform path normalization (trailing slashes, separators)
return pathsEqual(sessionDir, effectiveWorkingDirectory);
});
const activeSessions = scopedSessions.filter((s) => !s.isArchived);
const archivedSessions = scopedSessions.filter((s) => s.isArchived);
const displayedSessions = activeTab === 'active' ? activeSessions : archivedSessions;
return (
<Card className="h-full flex flex-col rounded-none">
<CardHeader className="pb-3">
<div className="flex items-center justify-between mb-4">
<CardTitle>Agent Sessions</CardTitle>
<HotkeyButton
variant="default"
size="sm"
onClick={() => {
// Switch to active tab if on archived tab
if (activeTab === 'archived') {
setActiveTab('active');
}
handleQuickCreateSession();
}}
hotkey={shortcuts.newSession}
hotkeyActive={false}
data-testid="new-session-button"
title={`New Session (${shortcuts.newSession})`}
>
<Plus className="w-4 h-4 mr-1" />
New
</HotkeyButton>
</div>
<Tabs
value={activeTab}
onValueChange={(value) => setActiveTab(value as 'active' | 'archived')}
className="w-full"
>
<TabsList className="w-full">
<TabsTrigger value="active" className="flex-1">
<MessageSquare className="w-4 h-4 mr-2" />
Active ({activeSessions.length})
</TabsTrigger>
<TabsTrigger value="archived" className="flex-1">
<Archive className="w-4 h-4 mr-2" />
Archived ({archivedSessions.length})
</TabsTrigger>
</TabsList>
</Tabs>
</CardHeader>
<CardContent className="flex-1 overflow-y-auto space-y-2" data-testid="session-list">
{/* Create new session */}
{isCreating && (
<div className="p-3 border rounded-lg bg-muted/50">
<div className="flex gap-2">
<Input
placeholder="Session name..."
value={newSessionName}
onChange={(e) => setNewSessionName(e.target.value)}
onKeyDown={(e) => {
if (e.key === 'Enter') handleCreateSession();
if (e.key === 'Escape') {
setIsCreating(false);
setNewSessionName('');
}
}}
autoFocus
/>
<Button size="sm" onClick={handleCreateSession}>
<Check className="w-4 h-4" />
</Button>
<Button
size="sm"
variant="ghost"
onClick={() => {
setIsCreating(false);
setNewSessionName('');
}}
>
<X className="w-4 h-4" />
</Button>
</div>
</div>
)}
{/* Delete All Archived button - shown at the top of archived sessions */}
{activeTab === 'archived' && archivedSessions.length > 0 && (
<div className="pb-2 border-b mb-2">
<Button
variant="destructive"
size="sm"
className="w-full"
onClick={() => setIsDeleteAllArchivedDialogOpen(true)}
data-testid="delete-all-archived-sessions-button"
>
<Trash2 className="w-4 h-4 mr-2" />
Delete All Archived Sessions
</Button>
</div>
)}
{/* Session list */}
{displayedSessions.map((session) => (
<div
key={session.id}
className={cn(
'p-3 border rounded-lg cursor-pointer transition-colors hover:bg-accent/50',
currentSessionId === session.id && 'bg-primary/10 border-primary',
session.isArchived && 'opacity-60'
)}
onClick={() => !session.isArchived && onSelectSession(session.id)}
data-testid={`session-item-${session.id}`}
>
<div className="flex items-start justify-between gap-2">
<div className="flex-1 min-w-0">
{editingSessionId === session.id ? (
<div className="flex gap-2 mb-2">
<Input
value={editingName}
onChange={(e) => setEditingName(e.target.value)}
onKeyDown={(e) => {
if (e.key === 'Enter') handleRenameSession(session.id);
if (e.key === 'Escape') {
setEditingSessionId(null);
setEditingName('');
}
}}
onClick={(e) => e.stopPropagation()}
autoFocus
className="h-7"
/>
<Button
size="sm"
onClick={(e) => {
e.stopPropagation();
handleRenameSession(session.id);
}}
className="h-7"
>
<Check className="w-3 h-3" />
</Button>
<Button
size="sm"
variant="ghost"
onClick={(e) => {
e.stopPropagation();
setEditingSessionId(null);
setEditingName('');
}}
className="h-7"
>
<X className="w-3 h-3" />
</Button>
</div>
) : (
<>
<div className="flex items-center gap-2 mb-1">
{/* Show loading indicator if this session is running (either current session thinking or any session in runningSessions) */}
{(currentSessionId === session.id && isCurrentSessionThinking) ||
runningSessions.has(session.id) ? (
<Spinner size="sm" className="shrink-0" />
) : (
<MessageSquare className="w-4 h-4 text-muted-foreground shrink-0" />
)}
<h3 className="font-medium truncate">{session.name}</h3>
{((currentSessionId === session.id && isCurrentSessionThinking) ||
runningSessions.has(session.id)) && (
<span className="text-xs text-primary bg-primary/10 px-2 py-0.5 rounded-full">
thinking...
</span>
)}
</div>
{session.preview && (
<p className="text-xs text-muted-foreground truncate">{session.preview}</p>
)}
<div className="flex items-center gap-2 mt-2">
<span className="text-xs text-muted-foreground">
{session.messageCount} messages
</span>
<span className="text-xs text-muted-foreground">·</span>
<span className="text-xs text-muted-foreground">
{new Date(session.updatedAt).toLocaleDateString()}
</span>
</div>
</>
)}
</div>
{/* Actions */}
{!session.isArchived && (
<div className="flex gap-1" onClick={(e) => e.stopPropagation()}>
<Button
size="sm"
variant="ghost"
onClick={() => {
setEditingSessionId(session.id);
setEditingName(session.name);
}}
className="h-7 w-7 p-0"
>
<Edit2 className="w-3 h-3" />
</Button>
<Button
size="sm"
variant="ghost"
onClick={() => handleArchiveSession(session.id)}
className="h-7 w-7 p-0"
data-testid={`archive-session-${session.id}`}
>
<Archive className="w-3 h-3" />
</Button>
</div>
)}
{session.isArchived && (
<div className="flex gap-1" onClick={(e) => e.stopPropagation()}>
<Button
size="sm"
variant="ghost"
onClick={() => handleUnarchiveSession(session.id)}
className="h-7 w-7 p-0"
>
<ArchiveRestore className="w-3 h-3" />
</Button>
<Button
size="sm"
variant="ghost"
onClick={() => handleDeleteSession(session)}
className="h-7 w-7 p-0 text-destructive"
data-testid={`delete-session-${session.id}`}
>
<Trash2 className="w-3 h-3" />
</Button>
</div>
)}
</div>
</div>
))}
{displayedSessions.length === 0 && (
<div className="text-center py-8 text-muted-foreground">
<MessageSquare className="w-12 h-12 mx-auto mb-2 opacity-50" />
<p className="text-sm">
{activeTab === 'active' ? 'No active sessions' : 'No archived sessions'}
</p>
<p className="text-xs">
{activeTab === 'active'
? 'Create your first session to get started'
: 'Archive sessions to see them here'}
</p>
</div>
)}
</CardContent>
{/* Delete Session Confirmation Dialog */}
<DeleteSessionDialog
open={isDeleteDialogOpen}
onOpenChange={setIsDeleteDialogOpen}
session={sessionToDelete}
onConfirm={confirmDeleteSession}
/>
{/* Delete All Archived Sessions Confirmation Dialog */}
<DeleteAllArchivedSessionsDialog
open={isDeleteAllArchivedDialogOpen}
onOpenChange={setIsDeleteAllArchivedDialogOpen}
archivedCount={archivedSessions.length}
onConfirm={handleDeleteAllArchivedSessions}
/>
</Card>
);
}
|