From d3187d8c77397f9648bd572f10a6cff76282a2e9 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Wed, 3 Dec 2025 01:11:39 -0600 Subject: [PATCH] Improve chats icon --- src/components/ProjectSidebar.tsx | 4 +- src/components/Shakespeare/ChatPane.tsx | 1 + src/hooks/useAIChat.ts | 8 ++-- src/lib/SessionManager.ts | 62 +++++++++++++++++++++---- 4 files changed, 60 insertions(+), 15 deletions(-) diff --git a/src/components/ProjectSidebar.tsx b/src/components/ProjectSidebar.tsx index 980ba8e5..7532306c 100644 --- a/src/components/ProjectSidebar.tsx +++ b/src/components/ProjectSidebar.tsx @@ -1,7 +1,7 @@ import React, { useEffect, useState, useMemo } from 'react'; import { useNavigate } from 'react-router-dom'; import { useTranslation } from 'react-i18next'; -import { Plus, Folder, GitBranch, Loader2, ChevronDown, Star, Columns2, X, Settings, HelpCircle, Search, MessageSquare } from 'lucide-react'; +import { Plus, Folder, GitBranch, Loader2, ChevronDown, Star, Columns2, X, Settings, HelpCircle, Search, MessageSquareText } from 'lucide-react'; import { Button } from '@/components/ui/button'; import { Skeleton } from '@/components/ui/skeleton'; import { Input } from '@/components/ui/input'; @@ -105,7 +105,7 @@ const ChatItem: React.FC = ({ chat, isSelected, onSelect }) => { />
- +
diff --git a/src/components/Shakespeare/ChatPane.tsx b/src/components/Shakespeare/ChatPane.tsx index c3f244b9..fce517b7 100644 --- a/src/components/Shakespeare/ChatPane.tsx +++ b/src/components/Shakespeare/ChatPane.tsx @@ -273,6 +273,7 @@ export const ChatPane = forwardRef(({ customTools, onUpdateMetadata, onAIError, + workingDir: isChat ? cwd : undefined, // Pass working dir for chats }); // Check if we should show the template info banner diff --git a/src/hooks/useAIChat.ts b/src/hooks/useAIChat.ts index 5a2d3ac5..81dd7704 100644 --- a/src/hooks/useAIChat.ts +++ b/src/hooks/useAIChat.ts @@ -13,6 +13,7 @@ interface UseAIChatSessionOptions { maxSteps?: number; onUpdateMetadata?: (title: string, description: string) => void; onAIError?: (error: Error) => void; + workingDir?: string; // Optional working directory (for chats) } interface StreamingMessage { @@ -32,7 +33,8 @@ export function useAIChat({ customTools = {}, maxSteps = 50, onUpdateMetadata, - onAIError + onAIError, + workingDir }: UseAIChatSessionOptions) { const sessionManager = useSessionManager(); const { isConfigured } = useAISettings(); @@ -46,14 +48,14 @@ export function useAIChat({ const initSession = useCallback(async () => { // Load or update session - const session = await sessionManager.loadSession(projectId, tools, customTools, maxSteps); + const session = await sessionManager.loadSession(projectId, tools, customTools, maxSteps, workingDir); // Initialize individual state variables from existing session setMessages([...session.messages]); setStreamingMessage(session.streamingMessage ? { ...session.streamingMessage } : undefined); setIsLoading(session.isLoading); setTotalCost(session.totalCost || 0); setLastInputTokens(session.lastInputTokens || 0); - }, [sessionManager, projectId, tools, customTools, maxSteps]); + }, [sessionManager, projectId, tools, customTools, maxSteps, workingDir]); // Initialize session useEffect(() => { diff --git a/src/lib/SessionManager.ts b/src/lib/SessionManager.ts index a007c939..4c20b10b 100644 --- a/src/lib/SessionManager.ts +++ b/src/lib/SessionManager.ts @@ -41,6 +41,7 @@ export interface SessionState { totalCost?: number; // Total cost in USD for this session lastInputTokens?: number; // Input tokens from the last AI request imagesNotSupported?: boolean; // Track if this session's model doesn't support images + workingDir?: string; // Working directory (for chats, different from projects) } export interface SessionManagerEvents { @@ -93,13 +94,14 @@ export class SessionManager { } /** - * Create a new session for a project + * Create a new session for a project or chat */ async loadSession( projectId: string, tools: Record, customTools: Record>, - maxSteps?: number + maxSteps?: number, + workingDir?: string // Optional working directory (for chats, use chatsPath instead of projectsPath) ): Promise { let messages: OpenAI.Chat.Completions.ChatCompletionMessageParam[] = []; let sessionName = DotAI.generateSessionName(); @@ -107,11 +109,36 @@ export class SessionManager { // Try to load existing history try { const config = this.getConfig(); - const dotAI = new DotAI(this.fs, `${config.fsPathProjects}/${projectId}`); - const lastSession = await dotAI.readLastSessionHistory(); - if (lastSession) { - messages = lastSession.messages; - sessionName = lastSession.sessionName; + const dir = workingDir || `${config.fsPathProjects}/${projectId}`; + + // For chats, load messages from messages.jsonl directly + if (workingDir && workingDir.includes(config.fsPathChats)) { + const messagesPath = `${dir}/messages.jsonl`; + try { + const content = await this.fs.readFile(messagesPath, 'utf8'); + const lines = content.trim().split('\n').filter(line => line.trim()); + + for (const line of lines) { + try { + const message = JSON.parse(line) as OpenAI.Chat.Completions.ChatCompletionMessageParam; + messages.push(message); + } catch (parseError) { + console.warn('Failed to parse message from chat:', parseError); + } + } + + sessionName = 'messages'; // Use a fixed session name for chats + } catch (readError) { + console.warn('Failed to read chat messages:', readError); + } + } else { + // For projects, use DotAI to load from .git/ai/history + const dotAI = new DotAI(this.fs, dir); + const lastSession = await dotAI.readLastSessionHistory(); + if (lastSession) { + messages = lastSession.messages; + sessionName = lastSession.sessionName; + } } } catch (error) { console.warn('Failed to load session history:', error); @@ -129,6 +156,7 @@ export class SessionManager { ...this.sessions.get(projectId), messages, sessionName, + workingDir, }; // Update session configuration @@ -732,9 +760,23 @@ export class SessionManager { try { const config = this.getConfig(); - const dotAI = new DotAI(this.fs, `${config.fsPathProjects}/${session.projectId}`); - // Cast to ChatCompletionMessageParam[] - user messages with image arrays are valid - await dotAI.setHistory(session.sessionName, session.messages as OpenAI.Chat.Completions.ChatCompletionMessageParam[]); + + // For chats, save directly to messages.jsonl + if (session.workingDir && session.workingDir.includes(config.fsPathChats)) { + const messagesPath = `${session.workingDir}/messages.jsonl`; + + // Convert messages to JSONL format + const jsonlContent = session.messages.map(message => JSON.stringify(message)).join('\n'); + const finalContent = jsonlContent + (session.messages.length > 0 ? '\n' : ''); + + // Write the entire file + await this.fs.writeFile(messagesPath, finalContent); + } else { + // For projects, use DotAI to save to .git/ai/history + const dotAI = new DotAI(this.fs, session.workingDir || `${config.fsPathProjects}/${session.projectId}`); + // Cast to ChatCompletionMessageParam[] - user messages with image arrays are valid + await dotAI.setHistory(session.sessionName, session.messages as OpenAI.Chat.Completions.ChatCompletionMessageParam[]); + } } catch (error) { console.warn('Failed to save session history:', error); }