mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-04 08:13:34 +00:00
Compare commits
13
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5f76931941 | ||
|
|
6527f42e2f | ||
|
|
a42ea724fa | ||
|
|
ad217937a3 | ||
|
|
6457fbda76 | ||
|
|
b78a912af1 | ||
|
|
b1bbf58867 | ||
|
|
ecf6b72b21 | ||
|
|
1b287b90aa | ||
|
|
e9a6177c5b | ||
|
|
361b6dae3c | ||
|
|
37071cde85 | ||
|
|
fc6ddbf8d6 |
No files matched your search
@@ -1,4 +1,6 @@
|
||||
import { MessageCircle } from 'lucide-react';
|
||||
import { useState, useEffect } from 'react';
|
||||
import { useLocation } from 'react-router-dom';
|
||||
import { MessageCircle, X } from 'lucide-react';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { useGlobalChat } from '@/hooks/useGlobalChat';
|
||||
import { useAppContext } from '@/hooks/useAppContext';
|
||||
@@ -7,30 +9,75 @@ import { cn } from '@/lib/utils';
|
||||
export function FloatingChatButton() {
|
||||
const { isOpen, setIsOpen, hasUnread } = useGlobalChat();
|
||||
const { config } = useAppContext();
|
||||
const location = useLocation();
|
||||
const [isHovered, setIsHovered] = useState(false);
|
||||
const [isHiddenTemporarily, setIsHiddenTemporarily] = useState(false);
|
||||
|
||||
// Reset hidden state when navigating to a different page
|
||||
useEffect(() => {
|
||||
setIsHiddenTemporarily(false);
|
||||
}, [location.pathname]);
|
||||
|
||||
// Don't render if disabled in settings
|
||||
if (config.globalChatEnabled === false) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Don't render on settings pages
|
||||
if (location.pathname.startsWith('/settings')) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Don't render if temporarily hidden
|
||||
if (isHiddenTemporarily) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const handleClose = (e: React.MouseEvent) => {
|
||||
e.stopPropagation();
|
||||
setIsHiddenTemporarily(true);
|
||||
};
|
||||
|
||||
return (
|
||||
<Button
|
||||
onClick={() => setIsOpen(!isOpen)}
|
||||
size="lg"
|
||||
className={cn(
|
||||
'fixed bottom-6 right-6 z-40 h-14 w-14 rounded-full shadow-lg',
|
||||
'bg-primary hover:bg-primary/90 text-primary-foreground',
|
||||
'transition-all duration-200 hover:scale-105',
|
||||
'flex items-center justify-center p-0',
|
||||
isOpen && 'ring-2 ring-ring ring-offset-2'
|
||||
)}
|
||||
aria-label={isOpen ? 'Close chat' : 'Open chat'}
|
||||
<div
|
||||
className="fixed bottom-6 right-6 z-40"
|
||||
onMouseEnter={() => setIsHovered(true)}
|
||||
onMouseLeave={() => setIsHovered(false)}
|
||||
>
|
||||
<MessageCircle className="h-6 w-6" />
|
||||
{/* Unread indicator - small dot when there are unread messages */}
|
||||
{!isOpen && hasUnread && (
|
||||
<span className="absolute top-0 right-0 h-3 w-3 rounded-full bg-destructive ring-2 ring-background" />
|
||||
)}
|
||||
</Button>
|
||||
{/* Main chat button */}
|
||||
<Button
|
||||
onClick={() => setIsOpen(!isOpen)}
|
||||
size="lg"
|
||||
className={cn(
|
||||
'h-14 w-14 rounded-full shadow-lg',
|
||||
'bg-primary hover:bg-primary/90 text-primary-foreground',
|
||||
'transition-all duration-200 hover:scale-105',
|
||||
'flex items-center justify-center p-0',
|
||||
isOpen && 'ring-2 ring-ring ring-offset-2'
|
||||
)}
|
||||
aria-label={isOpen ? 'Close chat' : 'Open chat'}
|
||||
>
|
||||
<MessageCircle className="h-6 w-6" />
|
||||
{/* Unread indicator - small dot when there are unread messages */}
|
||||
{!isOpen && hasUnread && (
|
||||
<span className="absolute top-0 right-0 h-3 w-3 rounded-full bg-destructive ring-2 ring-background" />
|
||||
)}
|
||||
</Button>
|
||||
|
||||
{/* Close button - appears on hover, rendered last to be on top */}
|
||||
<Button
|
||||
onClick={handleClose}
|
||||
size="icon"
|
||||
variant="secondary"
|
||||
className={cn(
|
||||
'absolute -top-2 -right-2 h-6 w-6 rounded-full shadow-md border',
|
||||
'transition-all duration-200',
|
||||
isHovered ? 'opacity-100 scale-100' : 'opacity-0 scale-75 pointer-events-none'
|
||||
)}
|
||||
aria-label="Hide chat button"
|
||||
>
|
||||
<X className="h-3 w-3" />
|
||||
</Button>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
import { useState, useRef, useEffect, useCallback } from 'react';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import { X, Minimize2, Trash2, Copy, Check, ChevronDown, ArrowUp, Square, Loader2 } from 'lucide-react';
|
||||
import { useLocation } from 'react-router-dom';
|
||||
import { X, Trash2, Copy, Check, ChevronDown, ArrowUp, Square, Loader2 } from 'lucide-react';
|
||||
import { Streamdown } from 'streamdown';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { Textarea } from '@/components/ui/textarea';
|
||||
@@ -95,12 +96,18 @@ export function GlobalChatPane() {
|
||||
const { config } = useAppContext();
|
||||
const { settings } = useAISettings();
|
||||
const isMobile = useIsMobile();
|
||||
const location = useLocation();
|
||||
|
||||
const [input, setInput] = useState('');
|
||||
const [showScrollToBottom, setShowScrollToBottom] = useState(false);
|
||||
const scrollAreaRef = useRef<HTMLDivElement>(null);
|
||||
const textareaRef = useRef<HTMLTextAreaElement>(null);
|
||||
|
||||
// Track if user has manually scrolled up
|
||||
const userScrolledUp = useRef(false);
|
||||
// Track the last message content to detect streaming updates
|
||||
const lastMessageContent = messages[messages.length - 1]?.content || '';
|
||||
|
||||
// Initialize provider model from settings if not set
|
||||
useEffect(() => {
|
||||
if (!providerModel && settings.recentlyUsedModels?.length) {
|
||||
@@ -108,23 +115,41 @@ export function GlobalChatPane() {
|
||||
}
|
||||
}, [providerModel, settings.recentlyUsedModels, setProviderModel]);
|
||||
|
||||
// Auto-scroll to bottom when new messages arrive
|
||||
// Scroll to bottom and focus textarea when chat opens
|
||||
useEffect(() => {
|
||||
if (scrollAreaRef.current) {
|
||||
const container = scrollAreaRef.current;
|
||||
const isNearBottom = container.scrollTop + container.clientHeight >= container.scrollHeight - 100;
|
||||
|
||||
if (isNearBottom || messages.length <= 1) {
|
||||
container.scrollTop = container.scrollHeight;
|
||||
}
|
||||
if (isOpen && scrollAreaRef.current) {
|
||||
// Small delay to ensure content is rendered
|
||||
requestAnimationFrame(() => {
|
||||
if (scrollAreaRef.current) {
|
||||
scrollAreaRef.current.scrollTop = scrollAreaRef.current.scrollHeight;
|
||||
}
|
||||
// Focus the textarea so user can start typing immediately
|
||||
textareaRef.current?.focus();
|
||||
});
|
||||
// Reset scroll tracking when opening
|
||||
userScrolledUp.current = false;
|
||||
}
|
||||
}, [messages]);
|
||||
}, [isOpen]);
|
||||
|
||||
// Handle scroll to show/hide scroll-to-bottom button
|
||||
// Auto-scroll to bottom when messages change or streaming content updates
|
||||
useEffect(() => {
|
||||
if (!scrollAreaRef.current || !isOpen) return;
|
||||
|
||||
// Only auto-scroll if user hasn't manually scrolled up
|
||||
if (!userScrolledUp.current) {
|
||||
scrollAreaRef.current.scrollTop = scrollAreaRef.current.scrollHeight;
|
||||
}
|
||||
}, [messages, lastMessageContent, isLoading, isOpen]);
|
||||
|
||||
// Handle scroll to detect user scrolling and show/hide scroll-to-bottom button
|
||||
const handleScroll = useCallback(() => {
|
||||
if (scrollAreaRef.current) {
|
||||
const container = scrollAreaRef.current;
|
||||
const isNearBottom = container.scrollTop + container.clientHeight >= container.scrollHeight - 100;
|
||||
const threshold = 100;
|
||||
const isNearBottom = container.scrollTop + container.clientHeight >= container.scrollHeight - threshold;
|
||||
|
||||
// Track if user scrolled away from bottom
|
||||
userScrolledUp.current = !isNearBottom;
|
||||
setShowScrollToBottom(!isNearBottom && messages.length > 0);
|
||||
}
|
||||
}, [messages.length]);
|
||||
@@ -132,6 +157,8 @@ export function GlobalChatPane() {
|
||||
const scrollToBottom = useCallback(() => {
|
||||
if (scrollAreaRef.current) {
|
||||
scrollAreaRef.current.scrollTop = scrollAreaRef.current.scrollHeight;
|
||||
// Reset the flag so auto-scroll resumes
|
||||
userScrolledUp.current = false;
|
||||
}
|
||||
}, []);
|
||||
|
||||
@@ -141,12 +168,19 @@ export function GlobalChatPane() {
|
||||
const messageContent = input.trim();
|
||||
setInput('');
|
||||
|
||||
await sendMessage(messageContent, providerModel);
|
||||
|
||||
// Focus back on textarea
|
||||
// Focus immediately so user can keep typing
|
||||
textareaRef.current?.focus();
|
||||
|
||||
await sendMessage(messageContent, providerModel);
|
||||
}, [input, isLoading, providerModel, sendMessage]);
|
||||
|
||||
// Refocus textarea when AI finishes responding
|
||||
useEffect(() => {
|
||||
if (!isLoading && isOpen && messages.length > 0) {
|
||||
textareaRef.current?.focus();
|
||||
}
|
||||
}, [isLoading, isOpen, messages.length]);
|
||||
|
||||
const handleKeyDown = useCallback((e: React.KeyboardEvent<HTMLTextAreaElement>) => {
|
||||
if (e.key === 'Enter' && !e.shiftKey && !isMobile) {
|
||||
e.preventDefault();
|
||||
@@ -159,6 +193,11 @@ export function GlobalChatPane() {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Don't render on settings pages
|
||||
if (location.pathname.startsWith('/settings')) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const hasProviders = settings.providers.length > 0;
|
||||
|
||||
// Mobile: Full screen drawer from bottom
|
||||
@@ -313,7 +352,7 @@ export function GlobalChatPane() {
|
||||
className="h-7 w-7"
|
||||
onClick={() => setIsOpen(false)}
|
||||
>
|
||||
<Minimize2 className="h-3.5 w-3.5" />
|
||||
<X className="h-3.5 w-3.5" />
|
||||
</Button>
|
||||
</TooltipTrigger>
|
||||
<TooltipContent>{t('minimizeChat')}</TooltipContent>
|
||||
|
||||
@@ -28,6 +28,7 @@ export function GlobalChatProvider({ children }: GlobalChatProviderProps) {
|
||||
const [hasUnread, setHasUnread] = useState(false);
|
||||
const abortControllerRef = useRef<AbortController | null>(null);
|
||||
const isOpenRef = useRef(false);
|
||||
const loadingReleasedRef = useRef(false);
|
||||
|
||||
// Separate model storage for global chat (independent from project chat)
|
||||
const [globalChatModel, setGlobalChatModel] = useLocalStorage<string>('shakespeare-global-chat-model', '');
|
||||
@@ -66,8 +67,6 @@ export function GlobalChatProvider({ children }: GlobalChatProviderProps) {
|
||||
}, []);
|
||||
|
||||
const sendMessage = useCallback(async (content: string, providerModelString: string) => {
|
||||
if (!content.trim() || isLoading) return;
|
||||
|
||||
// Parse provider and model
|
||||
const { model: modelId, provider } = parseProviderModel(providerModelString, settings.providers);
|
||||
|
||||
@@ -84,23 +83,30 @@ export function GlobalChatProvider({ children }: GlobalChatProviderProps) {
|
||||
timestamp: Date.now(),
|
||||
};
|
||||
|
||||
// Add user message and trim if necessary
|
||||
setMessages(prev => trimMessages([...prev, userMessage]));
|
||||
// Add user message and capture current messages for API call
|
||||
let currentMessages: GlobalChatMessage[] = [];
|
||||
setMessages(prev => {
|
||||
currentMessages = prev;
|
||||
return trimMessages([...prev, userMessage]);
|
||||
});
|
||||
setIsLoading(true);
|
||||
|
||||
// Create abort controller for this request
|
||||
abortControllerRef.current = new AbortController();
|
||||
|
||||
// Reset the loading released flag for this new request
|
||||
loadingReleasedRef.current = false;
|
||||
|
||||
try {
|
||||
// Create AI client
|
||||
const client = createAIClient(provider, config.corsProxy, user?.signer);
|
||||
|
||||
// Build messages for API
|
||||
// Build messages for API using captured current messages
|
||||
const systemPrompt = config.globalChatSystemPrompt || DEFAULT_GLOBAL_CHAT_SYSTEM_PROMPT;
|
||||
|
||||
const apiMessages: OpenAI.Chat.Completions.ChatCompletionMessageParam[] = [
|
||||
{ role: 'system', content: systemPrompt },
|
||||
...messages.map(msg => ({
|
||||
...currentMessages.map(msg => ({
|
||||
role: msg.role as 'user' | 'assistant',
|
||||
content: msg.content,
|
||||
})),
|
||||
@@ -141,6 +147,14 @@ export function GlobalChatProvider({ children }: GlobalChatProviderProps) {
|
||||
}
|
||||
return updated;
|
||||
});
|
||||
|
||||
// Release loading as soon as finish_reason is received
|
||||
// Don't break - let the stream drain naturally to avoid async iterator cleanup delays
|
||||
const finishReason = chunk.choices[0]?.finish_reason;
|
||||
if (finishReason && !loadingReleasedRef.current) {
|
||||
loadingReleasedRef.current = true;
|
||||
setIsLoading(false);
|
||||
}
|
||||
}
|
||||
|
||||
// Final trim after response is complete
|
||||
@@ -173,10 +187,13 @@ export function GlobalChatProvider({ children }: GlobalChatProviderProps) {
|
||||
return [...filtered, errorMessage];
|
||||
});
|
||||
} finally {
|
||||
setIsLoading(false);
|
||||
// Only set loading to false if we haven't already released it
|
||||
if (!loadingReleasedRef.current) {
|
||||
setIsLoading(false);
|
||||
}
|
||||
abortControllerRef.current = null;
|
||||
}
|
||||
}, [config.corsProxy, config.globalChatSystemPrompt, isLoading, messages, settings.providers, trimMessages, user?.signer]);
|
||||
}, [config.corsProxy, config.globalChatSystemPrompt, settings.providers, trimMessages, user?.signer]);
|
||||
|
||||
const stopGeneration = useCallback(() => {
|
||||
if (abortControllerRef.current) {
|
||||
|
||||
@@ -377,6 +377,8 @@ export const ChatPane = forwardRef<ChatPaneRef, ChatPaneProps>(({
|
||||
const scrollToBottom = useCallback(() => {
|
||||
if (scrollAreaRef.current) {
|
||||
scrollAreaRef.current.scrollTop = scrollAreaRef.current.scrollHeight;
|
||||
// Reset the flag so auto-scroll resumes
|
||||
userScrolledUp.current = false;
|
||||
}
|
||||
}, []);
|
||||
|
||||
@@ -427,17 +429,34 @@ export const ChatPane = forwardRef<ChatPaneRef, ChatPaneProps>(({
|
||||
};
|
||||
}, [messages]); // Re-run when messages change to ensure proper setup
|
||||
|
||||
// Track whether user has manually scrolled up
|
||||
const userScrolledUp = useRef(false);
|
||||
|
||||
// Detect when user manually scrolls
|
||||
useEffect(() => {
|
||||
if (scrollAreaRef.current && (messages || streamingMessage)) {
|
||||
// Check if user was already at or near the bottom (within 100px threshold)
|
||||
const container = scrollAreaRef.current;
|
||||
if (!container) return;
|
||||
|
||||
const handleUserScroll = () => {
|
||||
const threshold = 100;
|
||||
const container = scrollAreaRef.current;
|
||||
const isNearBottom = container.scrollTop + container.clientHeight >= container.scrollHeight - threshold;
|
||||
|
||||
// Only auto-scroll if user was already near the bottom
|
||||
if (isNearBottom) {
|
||||
container.scrollTop = container.scrollHeight;
|
||||
}
|
||||
// If user scrolled away from bottom, mark it
|
||||
// If user scrolled back to bottom, reset the flag
|
||||
userScrolledUp.current = !isNearBottom;
|
||||
};
|
||||
|
||||
container.addEventListener('scroll', handleUserScroll);
|
||||
return () => container.removeEventListener('scroll', handleUserScroll);
|
||||
}, []);
|
||||
|
||||
// Auto-scroll when messages change or streaming content updates
|
||||
useEffect(() => {
|
||||
if (!scrollAreaRef.current) return;
|
||||
|
||||
// Only auto-scroll if user hasn't manually scrolled up
|
||||
if (!userScrolledUp.current) {
|
||||
scrollAreaRef.current.scrollTop = scrollAreaRef.current.scrollHeight;
|
||||
}
|
||||
}, [messages, streamingMessage, isLoading]);
|
||||
|
||||
@@ -448,6 +467,8 @@ export const ChatPane = forwardRef<ChatPaneRef, ChatPaneProps>(({
|
||||
const timer = setTimeout(() => {
|
||||
scrollToBottom();
|
||||
scrolledProjects.add(projectId);
|
||||
// Reset the userScrolledUp flag when switching projects
|
||||
userScrolledUp.current = false;
|
||||
}, 100);
|
||||
|
||||
return () => clearTimeout(timer);
|
||||
|
||||
Reference in new issue
Block a user