Improve chats icon

This commit is contained in:
Alex Gleason committed 2025-12-03 01:11:39 -06:00
1 parent ab039d2fb7
commit d3187d8c77
4 files changed
+60 -15

No files matched your search

+52 -10
View File
@@ -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);
}