diff --git a/src/hooks/useAIChat.ts b/src/hooks/useAIChat.ts index a313d999..ff3cc410 100644 --- a/src/hooks/useAIChat.ts +++ b/src/hooks/useAIChat.ts @@ -2,7 +2,7 @@ import { useState, useEffect, useCallback } from 'react'; import { useSessionManager } from '@/hooks/useSessionManager'; import { useSessionSubscription } from '@/hooks/useSessionSubscription'; import { useAISettings } from '@/hooks/useAISettings'; -import type { SessionConfig, AIMessage } from '@/lib/SessionManager'; +import type { AIMessage } from '@/lib/SessionManager'; import type OpenAI from 'openai'; import type { Tool } from '@/lib/tools/Tool'; @@ -58,15 +58,7 @@ export function useAIChat({ setLastInputTokens(existingSession.lastInputTokens || 0); } else { // Create new session - const config: SessionConfig = { - projectId, - tools, - customTools, - systemPrompt, - maxSteps - }; - - await sessionManager.createSession(config); + await sessionManager.createSession(projectId, tools, customTools, systemPrompt, maxSteps); const newSession = sessionManager.getSession(projectId); if (newSession) { diff --git a/src/lib/SessionManager.ts b/src/lib/SessionManager.ts index 81c74740..d4fd2685 100644 --- a/src/lib/SessionManager.ts +++ b/src/lib/SessionManager.ts @@ -6,16 +6,12 @@ import type { Tool } from './tools/Tool'; export type AIMessage = OpenAI.Chat.Completions.ChatCompletionMessageParam; -export interface SessionConfig { +export interface SessionState { projectId: string; tools: Record; customTools: Record>; systemPrompt?: string; maxSteps?: number; -} - -export interface SessionState { - projectId: string; messages: AIMessage[]; streamingMessage?: { role: 'assistant'; @@ -46,7 +42,6 @@ export interface SessionManagerEvents { */ export class SessionManager { private sessions = new Map(); - private sessionConfigs = new Map(); private listeners: Partial void>>> = {}; private fs: JSRuntimeFS; private aiSettings: { providers: Record }; @@ -69,9 +64,19 @@ export class SessionManager { /** * Create a new session for a project */ - async createSession(config: SessionConfig): Promise { + async createSession( + projectId: string, + tools: Record, + customTools: Record>, + systemPrompt?: string, + maxSteps?: number + ): Promise { const sessionState: SessionState = { - projectId: config.projectId, + projectId, + tools, + customTools, + systemPrompt, + maxSteps, messages: [], isLoading: false, sessionName: DotAI.generateSessionName(), @@ -82,7 +87,7 @@ export class SessionManager { // Try to load existing history try { - const dotAI = new DotAI(this.fs, `/projects/${config.projectId}`); + const dotAI = new DotAI(this.fs, `/projects/${projectId}`); const lastSession = await dotAI.readLastSessionHistory(); if (lastSession) { sessionState.messages = lastSession.messages; @@ -92,12 +97,11 @@ export class SessionManager { console.warn('Failed to load session history:', error); } - this.sessions.set(config.projectId, sessionState); - this.sessionConfigs.set(config.projectId, config); + this.sessions.set(projectId, sessionState); - this.emit('sessionCreated', config.projectId); + this.emit('sessionCreated', projectId); - return config.projectId; + return projectId; } /** @@ -129,7 +133,6 @@ export class SessionManager { session?.abortController?.abort(); this.sessions.delete(projectId); - this.sessionConfigs.delete(projectId); this.emit('sessionDeleted', projectId); } @@ -162,9 +165,8 @@ export class SessionManager { */ async sendMessage(projectId: string, content: string, providerModel: string): Promise { const session = this.sessions.get(projectId); - const config = this.sessionConfigs.get(projectId); - if (!session || !config || session.isLoading) return; + if (!session || session.isLoading) return; await this.addMessage(projectId, { role: 'user', content }); await this.startGeneration(projectId, providerModel); @@ -175,9 +177,8 @@ export class SessionManager { */ async startGeneration(projectId: string, providerModel: string): Promise { const session = this.sessions.get(projectId); - const config = this.sessionConfigs.get(projectId); - if (!session || !config || session.isLoading || session.messages.length === 0) return; + if (!session || session.isLoading || session.messages.length === 0) return; // Check if last message is from user const lastMessage = session.messages[session.messages.length - 1]; @@ -204,16 +205,16 @@ export class SessionManager { // Prepare messages for AI const modelMessages: AIMessage[] = [...session.messages]; - if (config.systemPrompt) { + if (session.systemPrompt) { modelMessages.unshift({ role: 'system', - content: config.systemPrompt + content: session.systemPrompt }); } const conversationMessages = [...modelMessages]; let stepCount = 0; - const maxSteps = config.maxSteps || 50; + const maxSteps = session.maxSteps || 50; // Main AI generation loop while (stepCount < maxSteps && session.isLoading) { @@ -230,8 +231,8 @@ export class SessionManager { const completionOptions: OpenAI.Chat.Completions.ChatCompletionCreateParams = { model: modelName, messages: conversationMessages, - tools: config.tools && Object.keys(config.tools).length > 0 ? Object.values(config.tools) : undefined, - tool_choice: config.tools && Object.keys(config.tools).length > 0 ? 'auto' : undefined, + tools: session.tools && Object.keys(session.tools).length > 0 ? Object.values(session.tools) : undefined, + tool_choice: session.tools && Object.keys(session.tools).length > 0 ? 'auto' : undefined, stream: true }; @@ -331,7 +332,7 @@ export class SessionManager { const functionToolCall = toolCall as OpenAI.Chat.Completions.ChatCompletionMessageFunctionToolCall; const toolName = functionToolCall.function.name; - const tool = config.customTools[toolName]; + const tool = session.customTools[toolName]; if (!tool) { await this.addToolMessage(projectId, conversationMessages, functionToolCall.id, `Tool ${toolName} not found`); @@ -534,7 +535,6 @@ export class SessionManager { this.sessions.forEach((_, projectId) => this.stopGeneration(projectId)); this.sessions.clear(); - this.sessionConfigs.clear(); this.listeners = {}; } } \ No newline at end of file