mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-05 16:23:36 +00:00
baseline session management
This commit is contained in:
1 parent
39c526baf4
commit
c39db40cf0
18 files changed
+1459
-222
No files matched your search
@@ -0,0 +1,29 @@
|
||||
import { Loader2 } from 'lucide-react';
|
||||
import { useProjectSessionStatus } from '@/hooks/useProjectSessionStatus';
|
||||
import { cn } from '@/lib/utils';
|
||||
|
||||
interface ProjectSessionIndicatorProps {
|
||||
projectId: string;
|
||||
className?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Small indicator that shows if a project has active AI sessions
|
||||
*/
|
||||
export function ProjectSessionIndicator({ projectId, className }: ProjectSessionIndicatorProps) {
|
||||
const { hasActiveSessions, hasRunningSessions } = useProjectSessionStatus(projectId);
|
||||
|
||||
if (!hasActiveSessions) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return (
|
||||
<div className={cn("flex items-center", className)}>
|
||||
{hasRunningSessions ? (
|
||||
<Loader2 className="h-3 w-3 animate-spin text-blue-600 dark:text-blue-400" />
|
||||
) : (
|
||||
<div className="h-2 w-2 rounded-full bg-green-500" title="AI session active" />
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -7,6 +7,8 @@ import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigge
|
||||
import { LoginArea } from '@/components/auth/LoginArea';
|
||||
import { StarButton } from '@/components/StarButton';
|
||||
import { AISettingsDialog } from '@/components/ai/AISettingsDialog';
|
||||
import { SessionStatusIndicator } from '@/components/SessionStatusIndicator';
|
||||
import { ProjectSessionIndicator } from '@/components/ProjectSessionIndicator';
|
||||
import { useProjectsManager } from '@/hooks/useProjectsManager';
|
||||
import { useLocalStorage } from '@/hooks/useLocalStorage';
|
||||
import { useToast } from '@/hooks/useToast';
|
||||
@@ -161,7 +163,9 @@ export function ProjectSidebar({
|
||||
Shakespeare
|
||||
</h1>
|
||||
</button>
|
||||
<DropdownMenu>
|
||||
<div className="flex items-center gap-1">
|
||||
<SessionStatusIndicator currentProjectId={selectedProject?.id} />
|
||||
<DropdownMenu>
|
||||
<DropdownMenuTrigger asChild>
|
||||
<Button variant="ghost" size="sm" className="h-8 w-8 p-0 hover:bg-primary/10">
|
||||
<MoreVertical className="h-4 w-4 text-primary/60 hover:text-primary" />
|
||||
@@ -192,7 +196,8 @@ export function ProjectSidebar({
|
||||
AI Settings
|
||||
</DropdownMenuItem>
|
||||
</DropdownMenuContent>
|
||||
</DropdownMenu>
|
||||
</DropdownMenu>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -252,6 +257,7 @@ export function ProjectSidebar({
|
||||
<h3 className="font-medium text-sm truncate">
|
||||
{project.name}
|
||||
</h3>
|
||||
<ProjectSessionIndicator projectId={project.id} />
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
import { ReactNode, useMemo, useEffect } from 'react';
|
||||
import { SessionManagerContext } from '@/contexts/SessionManagerContext';
|
||||
import { SessionManager } from '@/lib/SessionManager';
|
||||
import { useFS } from '@/hooks/useFS';
|
||||
import { useAISettings } from '@/hooks/useAISettings';
|
||||
|
||||
interface SessionManagerProviderProps {
|
||||
children: ReactNode;
|
||||
}
|
||||
|
||||
/**
|
||||
* Provider that creates and manages the global session manager instance
|
||||
*/
|
||||
export function SessionManagerProvider({ children }: SessionManagerProviderProps) {
|
||||
const { fs } = useFS();
|
||||
const { settings } = useAISettings();
|
||||
|
||||
const sessionManager = useMemo(() => {
|
||||
return new SessionManager(fs, settings);
|
||||
}, [fs, settings]);
|
||||
|
||||
// Cleanup on unmount
|
||||
useEffect(() => {
|
||||
return () => {
|
||||
sessionManager.cleanup();
|
||||
};
|
||||
}, [sessionManager]);
|
||||
|
||||
return (
|
||||
<SessionManagerContext.Provider value={sessionManager}>
|
||||
{children}
|
||||
</SessionManagerContext.Provider>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
import { useEffect, useRef } from 'react';
|
||||
import { useSessionManager } from '@/hooks/useSessionManager';
|
||||
import { useToast } from '@/hooks/useToast';
|
||||
import { useNavigate, useLocation } from 'react-router-dom';
|
||||
|
||||
/**
|
||||
* Global component that monitors AI sessions and shows notifications
|
||||
* when sessions complete in the background
|
||||
*/
|
||||
export function SessionMonitor() {
|
||||
const sessionManager = useSessionManager();
|
||||
const { toast } = useToast();
|
||||
const navigate = useNavigate();
|
||||
const location = useLocation();
|
||||
const previousLoadingStates = useRef<Map<string, boolean>>(new Map());
|
||||
|
||||
useEffect(() => {
|
||||
const handleLoadingChanged = (sessionId: string, isLoading: boolean) => {
|
||||
const wasLoading = previousLoadingStates.current.get(sessionId);
|
||||
previousLoadingStates.current.set(sessionId, isLoading);
|
||||
|
||||
// If session just finished loading (was loading, now not loading)
|
||||
if (wasLoading && !isLoading) {
|
||||
const session = sessionManager.getSession(sessionId);
|
||||
if (!session) return;
|
||||
|
||||
// Check if user is currently viewing this project
|
||||
const currentProjectId = location.pathname.split('/')[2]; // /project/{projectId}
|
||||
const isCurrentProject = session.projectId === currentProjectId;
|
||||
|
||||
// Only show notification if user is not currently viewing this project
|
||||
if (!isCurrentProject) {
|
||||
toast({
|
||||
title: "AI Session Complete",
|
||||
description: `Finished working on ${session.projectName}`,
|
||||
action: (
|
||||
<button
|
||||
onClick={() => navigate(`/project/${session.projectId}`)}
|
||||
className="inline-flex h-8 shrink-0 items-center justify-center rounded-md border bg-transparent px-3 text-sm font-medium ring-offset-background transition-colors hover:bg-secondary focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:pointer-events-none disabled:opacity-50 group-[.destructive]:border-muted/40 group-[.destructive]:hover:border-destructive/30 group-[.destructive]:hover:bg-destructive group-[.destructive]:hover:text-destructive-foreground group-[.destructive]:focus-visible:ring-destructive"
|
||||
>
|
||||
View
|
||||
</button>
|
||||
)
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const handleSessionCreated = (sessionId: string) => {
|
||||
const session = sessionManager.getSession(sessionId);
|
||||
if (session) {
|
||||
previousLoadingStates.current.set(sessionId, session.isLoading);
|
||||
}
|
||||
};
|
||||
|
||||
const handleSessionDeleted = (sessionId: string) => {
|
||||
previousLoadingStates.current.delete(sessionId);
|
||||
};
|
||||
|
||||
// Initialize with current sessions
|
||||
sessionManager.getAllSessions().forEach(session => {
|
||||
previousLoadingStates.current.set(session.id, session.isLoading);
|
||||
});
|
||||
|
||||
// Subscribe to events
|
||||
sessionManager.on('loadingChanged', handleLoadingChanged);
|
||||
sessionManager.on('sessionCreated', handleSessionCreated);
|
||||
sessionManager.on('sessionDeleted', handleSessionDeleted);
|
||||
|
||||
return () => {
|
||||
sessionManager.off('loadingChanged', handleLoadingChanged);
|
||||
sessionManager.off('sessionCreated', handleSessionCreated);
|
||||
sessionManager.off('sessionDeleted', handleSessionDeleted);
|
||||
};
|
||||
}, [sessionManager, toast, navigate, location.pathname]);
|
||||
|
||||
// This component doesn't render anything
|
||||
return null;
|
||||
}
|
||||
@@ -0,0 +1,166 @@
|
||||
import { useState, useEffect } from 'react';
|
||||
import { useSessionManager } from '@/hooks/useSessionManager';
|
||||
import { Badge } from '@/components/ui/badge';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { Popover, PopoverContent, PopoverTrigger } from '@/components/ui/popover';
|
||||
import { Activity, Clock, Loader2, MessageSquare, X } from 'lucide-react';
|
||||
import type { SessionState } from '@/lib/SessionManager';
|
||||
import { cn } from '@/lib/utils';
|
||||
|
||||
interface SessionStatusIndicatorProps {
|
||||
className?: string;
|
||||
currentProjectId?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Component that shows the status of all active AI sessions
|
||||
*/
|
||||
export function SessionStatusIndicator({ className, currentProjectId }: SessionStatusIndicatorProps) {
|
||||
const sessionManager = useSessionManager();
|
||||
const [sessions, setSessions] = useState<SessionState[]>([]);
|
||||
const [isOpen, setIsOpen] = useState(false);
|
||||
|
||||
// Update sessions when they change
|
||||
useEffect(() => {
|
||||
const updateSessions = () => {
|
||||
setSessions(sessionManager.getAllSessions());
|
||||
};
|
||||
|
||||
// Initial load
|
||||
updateSessions();
|
||||
|
||||
// Subscribe to session changes
|
||||
sessionManager.on('sessionCreated', updateSessions);
|
||||
sessionManager.on('sessionUpdated', updateSessions);
|
||||
sessionManager.on('sessionDeleted', updateSessions);
|
||||
|
||||
return () => {
|
||||
sessionManager.off('sessionCreated', updateSessions);
|
||||
sessionManager.off('sessionUpdated', updateSessions);
|
||||
sessionManager.off('sessionDeleted', updateSessions);
|
||||
};
|
||||
}, [sessionManager]);
|
||||
|
||||
const activeSessions = sessions.filter(session => session.isLoading);
|
||||
const totalSessions = sessions.length;
|
||||
|
||||
if (totalSessions === 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const handleStopSession = (sessionId: string, e: React.MouseEvent) => {
|
||||
e.stopPropagation();
|
||||
sessionManager.stopGeneration(sessionId);
|
||||
};
|
||||
|
||||
const handleDeleteSession = (sessionId: string, e: React.MouseEvent) => {
|
||||
e.stopPropagation();
|
||||
sessionManager.deleteSession(sessionId);
|
||||
};
|
||||
|
||||
const formatLastActivity = (date: Date) => {
|
||||
const now = new Date();
|
||||
const diff = now.getTime() - date.getTime();
|
||||
const minutes = Math.floor(diff / 60000);
|
||||
const hours = Math.floor(diff / 3600000);
|
||||
|
||||
if (minutes < 1) return 'Just now';
|
||||
if (minutes < 60) return `${minutes}m ago`;
|
||||
if (hours < 24) return `${hours}h ago`;
|
||||
return date.toLocaleDateString();
|
||||
};
|
||||
|
||||
return (
|
||||
<Popover open={isOpen} onOpenChange={setIsOpen}>
|
||||
<PopoverTrigger asChild>
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
className={cn(
|
||||
"h-8 px-2 gap-1 text-xs",
|
||||
activeSessions.length > 0 && "text-blue-600 dark:text-blue-400",
|
||||
className
|
||||
)}
|
||||
>
|
||||
{activeSessions.length > 0 ? (
|
||||
<Loader2 className="h-3 w-3 animate-spin" />
|
||||
) : (
|
||||
<Activity className="h-3 w-3" />
|
||||
)}
|
||||
<span>{totalSessions}</span>
|
||||
{activeSessions.length > 0 && (
|
||||
<Badge variant="secondary" className="h-4 px-1 text-xs">
|
||||
{activeSessions.length}
|
||||
</Badge>
|
||||
)}
|
||||
</Button>
|
||||
</PopoverTrigger>
|
||||
<PopoverContent className="w-80 p-0" align="end">
|
||||
<div className="p-3 border-b">
|
||||
<h4 className="font-semibold text-sm">AI Sessions</h4>
|
||||
<p className="text-xs text-muted-foreground">
|
||||
{totalSessions} active session{totalSessions !== 1 ? 's' : ''}
|
||||
{activeSessions.length > 0 && ` (${activeSessions.length} running)`}
|
||||
</p>
|
||||
</div>
|
||||
<div className="max-h-64 overflow-y-auto">
|
||||
{sessions.map((session) => (
|
||||
<div
|
||||
key={session.id}
|
||||
className={cn(
|
||||
"p-3 border-b last:border-b-0 hover:bg-muted/50",
|
||||
session.projectId === currentProjectId && "bg-primary/5"
|
||||
)}
|
||||
>
|
||||
<div className="flex items-start justify-between gap-2">
|
||||
<div className="flex-1 min-w-0">
|
||||
<div className="flex items-center gap-2">
|
||||
<h5 className="font-medium text-sm truncate">
|
||||
{session.projectName}
|
||||
</h5>
|
||||
{session.isLoading && (
|
||||
<Loader2 className="h-3 w-3 animate-spin text-blue-600" />
|
||||
)}
|
||||
</div>
|
||||
<div className="flex items-center gap-2 mt-1 text-xs text-muted-foreground">
|
||||
<MessageSquare className="h-3 w-3" />
|
||||
<span>{session.messages.length} messages</span>
|
||||
<Clock className="h-3 w-3 ml-1" />
|
||||
<span>{formatLastActivity(session.lastActivity)}</span>
|
||||
</div>
|
||||
{session.streamingMessage?.content && (
|
||||
<p className="text-xs text-muted-foreground mt-1 truncate">
|
||||
{session.streamingMessage.content.slice(0, 50)}...
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
<div className="flex items-center gap-1">
|
||||
{session.isLoading && (
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
className="h-6 w-6 p-0"
|
||||
onClick={(e) => handleStopSession(session.id, e)}
|
||||
title="Stop generation"
|
||||
>
|
||||
<X className="h-3 w-3" />
|
||||
</Button>
|
||||
)}
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
className="h-6 w-6 p-0 text-destructive hover:text-destructive"
|
||||
onClick={(e) => handleDeleteSession(session.id, e)}
|
||||
title="Delete session"
|
||||
>
|
||||
<X className="h-3 w-3" />
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
</PopoverContent>
|
||||
</Popover>
|
||||
);
|
||||
}
|
||||
@@ -8,7 +8,7 @@ import { useAISettings } from '@/hooks/useAISettings';
|
||||
import { useFS } from '@/hooks/useFS';
|
||||
|
||||
import { useKeepAlive } from '@/hooks/useKeepAlive';
|
||||
import { useAIChat } from '@/hooks/useAIChat';
|
||||
import { useAIChatSession } from '@/hooks/useAIChatSession';
|
||||
import { ModelSelector } from '@/components/ModelSelector';
|
||||
|
||||
|
||||
@@ -126,7 +126,7 @@ export const ChatPane = forwardRef<ChatPaneRef, ChatPaneProps>(({
|
||||
startGeneration,
|
||||
stopGeneration,
|
||||
startNewSession: internalStartNewSession,
|
||||
} = useAIChat({
|
||||
} = useAIChatSession({
|
||||
projectId,
|
||||
projectName,
|
||||
tools,
|
||||
@@ -310,7 +310,7 @@ export const ChatPane = forwardRef<ChatPaneRef, ChatPaneProps>(({
|
||||
streamingMessage.content ? (
|
||||
<AIMessageItem
|
||||
key="streaming-message"
|
||||
message={streamingMessage}
|
||||
message={streamingMessage as OpenAI.Chat.Completions.ChatCompletionMessageParam}
|
||||
isCurrentlyLoading
|
||||
/>
|
||||
) : (
|
||||
@@ -329,7 +329,7 @@ export const ChatPane = forwardRef<ChatPaneRef, ChatPaneProps>(({
|
||||
<div className="flex items-center gap-2 px-2 py-1 text-xs text-muted-foreground">
|
||||
<Loader2 className="h-3 w-3 animate-spin flex-shrink-0" />
|
||||
<span className="font-medium">
|
||||
Running {streamingMessage.tool_calls[0].function.name}...
|
||||
Running {(streamingMessage.tool_calls[0] as OpenAI.Chat.Completions.ChatCompletionMessageFunctionToolCall).function.name}...
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
Reference in new issue
Block a user