sessionManager.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. import { promises as fs } from 'fs';
  2. import path from 'path';
  3. import os from 'os';
  4. class SessionManager {
  5. constructor() {
  6. // Store sessions in memory with conversation history
  7. this.sessions = new Map();
  8. this.maxSessions = 100;
  9. this.sessionsDir = path.join(os.homedir(), '.gemini', 'sessions');
  10. this.ready = this.init();
  11. }
  12. async init() {
  13. await this.initSessionsDir();
  14. await this.loadSessions();
  15. }
  16. async initSessionsDir() {
  17. try {
  18. await fs.mkdir(this.sessionsDir, { recursive: true });
  19. } catch (error) {
  20. // console.error('Error creating sessions directory:', error);
  21. }
  22. }
  23. // Create a new session
  24. createSession(sessionId, projectPath) {
  25. const session = {
  26. id: sessionId,
  27. projectPath: projectPath,
  28. messages: [],
  29. createdAt: new Date(),
  30. lastActivity: new Date()
  31. };
  32. // Evict oldest session from memory if we exceed limit
  33. if (this.sessions.size >= this.maxSessions) {
  34. const oldestKey = this.sessions.keys().next().value;
  35. if (oldestKey) this.sessions.delete(oldestKey);
  36. }
  37. this.sessions.set(sessionId, session);
  38. this.saveSession(sessionId);
  39. return session;
  40. }
  41. // Add a message to session
  42. addMessage(sessionId, role, content) {
  43. let session = this.sessions.get(sessionId);
  44. if (!session) {
  45. // Create session if it doesn't exist
  46. session = this.createSession(sessionId, '');
  47. }
  48. const message = {
  49. role: role, // 'user' or 'assistant'
  50. content: content,
  51. timestamp: new Date()
  52. };
  53. session.messages.push(message);
  54. session.lastActivity = new Date();
  55. this.saveSession(sessionId);
  56. return session;
  57. }
  58. // Get session by ID
  59. getSession(sessionId) {
  60. return this.sessions.get(sessionId);
  61. }
  62. // Get all sessions for a project
  63. getProjectSessions(projectPath) {
  64. const sessions = [];
  65. for (const [id, session] of this.sessions) {
  66. if (session.projectPath === projectPath) {
  67. sessions.push({
  68. id: session.id,
  69. summary: this.getSessionSummary(session),
  70. messageCount: session.messages.length,
  71. lastActivity: session.lastActivity
  72. });
  73. }
  74. }
  75. return sessions.sort((a, b) =>
  76. new Date(b.lastActivity) - new Date(a.lastActivity)
  77. );
  78. }
  79. // Get session summary
  80. getSessionSummary(session) {
  81. if (session.messages.length === 0) {
  82. return 'New Session';
  83. }
  84. // Find first user message
  85. const firstUserMessage = session.messages.find(m => m.role === 'user');
  86. if (firstUserMessage) {
  87. const content = firstUserMessage.content;
  88. return content.length > 50 ? content.substring(0, 50) + '...' : content;
  89. }
  90. return 'New Session';
  91. }
  92. // Build conversation context for Gemini
  93. buildConversationContext(sessionId, maxMessages = 10) {
  94. const session = this.sessions.get(sessionId);
  95. if (!session || session.messages.length === 0) {
  96. return '';
  97. }
  98. // Get last N messages for context
  99. const recentMessages = session.messages.slice(-maxMessages);
  100. let context = 'Here is the conversation history:\n\n';
  101. for (const msg of recentMessages) {
  102. if (msg.role === 'user') {
  103. context += `User: ${msg.content}\n`;
  104. } else {
  105. context += `Assistant: ${msg.content}\n`;
  106. }
  107. }
  108. context += '\nBased on the conversation history above, please answer the following:\n';
  109. return context;
  110. }
  111. // Prevent path traversal
  112. _safeFilePath(sessionId) {
  113. const safeId = String(sessionId).replace(/[/\\]|\.\./g, '');
  114. return path.join(this.sessionsDir, `${safeId}.json`);
  115. }
  116. // Save session to disk
  117. async saveSession(sessionId) {
  118. const session = this.sessions.get(sessionId);
  119. if (!session) return;
  120. try {
  121. const filePath = this._safeFilePath(sessionId);
  122. await fs.writeFile(filePath, JSON.stringify(session, null, 2));
  123. } catch (error) {
  124. // console.error('Error saving session:', error);
  125. }
  126. }
  127. // Load sessions from disk
  128. async loadSessions() {
  129. try {
  130. const files = await fs.readdir(this.sessionsDir);
  131. for (const file of files) {
  132. if (file.endsWith('.json')) {
  133. try {
  134. const filePath = path.join(this.sessionsDir, file);
  135. const data = await fs.readFile(filePath, 'utf8');
  136. const session = JSON.parse(data);
  137. // Convert dates
  138. session.createdAt = new Date(session.createdAt);
  139. session.lastActivity = new Date(session.lastActivity);
  140. session.messages.forEach(msg => {
  141. msg.timestamp = new Date(msg.timestamp);
  142. });
  143. this.sessions.set(session.id, session);
  144. } catch (error) {
  145. // console.error(`Error loading session ${file}:`, error);
  146. }
  147. }
  148. }
  149. // Enforce eviction after loading to prevent massive memory usage
  150. while (this.sessions.size > this.maxSessions) {
  151. const oldestKey = this.sessions.keys().next().value;
  152. if (oldestKey) this.sessions.delete(oldestKey);
  153. }
  154. } catch (error) {
  155. // console.error('Error loading sessions:', error);
  156. }
  157. }
  158. // Delete a session
  159. async deleteSession(sessionId) {
  160. this.sessions.delete(sessionId);
  161. try {
  162. const filePath = this._safeFilePath(sessionId);
  163. await fs.unlink(filePath);
  164. } catch (error) {
  165. // console.error('Error deleting session file:', error);
  166. }
  167. }
  168. // Get session messages for display
  169. getSessionMessages(sessionId) {
  170. const session = this.sessions.get(sessionId);
  171. if (!session) return [];
  172. return session.messages.map(msg => ({
  173. type: 'message',
  174. message: {
  175. role: msg.role,
  176. content: msg.content
  177. },
  178. timestamp: msg.timestamp.toISOString()
  179. }));
  180. }
  181. }
  182. // Singleton instance
  183. const sessionManager = new SessionManager();
  184. export const ready = sessionManager.ready;
  185. export default sessionManager;