mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-05 16:23:36 +00:00
Improve chats icon
This commit is contained in:
4 files changed
+60
-15
No files matched your search
@@ -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<ChatItemProps> = ({ chat, isSelected, onSelect }) => {
|
||||
/>
|
||||
<div className="flex items-center gap-3">
|
||||
<div className="flex-shrink-0">
|
||||
<MessageSquare className="h-4 w-4 text-primary" />
|
||||
<MessageSquareText className="h-4 w-4 text-blue-600 dark:text-blue-400" />
|
||||
</div>
|
||||
<div className="flex justify-between gap-2 flex-1 min-w-0">
|
||||
<div className="min-w-0 flex-1 flex items-center">
|
||||
|
||||
@@ -273,6 +273,7 @@ export const ChatPane = forwardRef<ChatPaneRef, ChatPaneProps>(({
|
||||
customTools,
|
||||
onUpdateMetadata,
|
||||
onAIError,
|
||||
workingDir: isChat ? cwd : undefined, // Pass working dir for chats
|
||||
});
|
||||
|
||||
// Check if we should show the template info banner
|
||||
|
||||
@@ -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(() => {
|
||||
|
||||
+52
-10
@@ -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<string, OpenAI.Chat.Completions.ChatCompletionTool>,
|
||||
customTools: Record<string, Tool<unknown>>,
|
||||
maxSteps?: number
|
||||
maxSteps?: number,
|
||||
workingDir?: string // Optional working directory (for chats, use chatsPath instead of projectsPath)
|
||||
): Promise<SessionState> {
|
||||
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);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user