UNPKG

autoagent-cli

Version:

Run autonomous AI agents using Claude or Gemini for task execution

195 lines (194 loc) 7.17 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.SessionManager = void 0; const fs = __importStar(require("fs/promises")); const fs_1 = require("fs"); const path = __importStar(require("path")); const os_1 = require("os"); const crypto_1 = require("crypto"); class SessionManager { constructor(baseDir) { const homeDir = process.env.HOME ?? process.env.USERPROFILE ?? ''; const rootDir = baseDir ?? path.join(homeDir, '.autoagent'); this.sessionsDir = path.join(rootDir, 'sessions'); this.currentSessionFile = path.join(this.sessionsDir, 'current'); } async initializeDirectory() { try { await fs.mkdir(this.sessionsDir, { recursive: true }); } catch (error) { throw new Error(`Failed to initialize sessions directory: ${String(error)}`); } } async saveSession(session) { await this.initializeDirectory(); const filename = this.getSessionFilename(session); const filepath = path.join(this.sessionsDir, filename); try { await fs.writeFile(filepath, JSON.stringify(session, null, 2), 'utf8'); } catch (error) { throw new Error(`Failed to save session: ${String(error)}`); } } async setCurrentSession(sessionId) { await this.initializeDirectory(); const sessionFiles = await this.findSessionFile(sessionId); if (!sessionFiles.length) { throw new Error(`Session ${sessionId} not found`); } const sessionFile = sessionFiles[0]; if (sessionFile === undefined || sessionFile === null || sessionFile === '') { throw new Error(`Session ${sessionId} file not found`); } const isWindows = (0, os_1.platform)() === 'win32'; try { try { await fs.unlink(this.currentSessionFile); } catch { } if (isWindows) { const sessionPath = path.join(this.sessionsDir, sessionFile); await fs.copyFile(sessionPath, this.currentSessionFile); } else { await fs.symlink(sessionFile, this.currentSessionFile); } } catch (error) { throw new Error(`Failed to set current session: ${String(error)}`); } } async getCurrentSession() { try { const data = await fs.readFile(this.currentSessionFile, 'utf8'); return JSON.parse(data); } catch { return null; } } async listSessions(limit) { try { const files = await fs.readdir(this.sessionsDir); const sessionFiles = files.filter(f => f.startsWith('session-') && f.endsWith('.json')); const sessions = []; for (const file of sessionFiles) { try { const filepath = path.join(this.sessionsDir, file); const data = await fs.readFile(filepath, 'utf8'); sessions.push(JSON.parse(data)); } catch { } } sessions.sort((a, b) => b.startTime - a.startTime); if (limit !== undefined && limit > 0) { return sessions.slice(0, limit); } return sessions; } catch { return []; } } async endSession(sessionId, status, error) { const sessionFiles = await this.findSessionFile(sessionId); if (!sessionFiles.length) { throw new Error(`Session ${sessionId} not found`); } const sessionFile = sessionFiles[0]; if (sessionFile === undefined || sessionFile === null || sessionFile === '') { throw new Error(`Session ${sessionId} file not found`); } const filepath = path.join(this.sessionsDir, sessionFile); try { const data = await fs.readFile(filepath, 'utf8'); const session = JSON.parse(data); session.endTime = Date.now(); session.status = status; if (error !== undefined) { session.error = error; } await fs.writeFile(filepath, JSON.stringify(session, null, 2), 'utf8'); } catch (error) { throw new Error(`Failed to end session: ${String(error)}`); } } getSessionFilename(session) { const timestamp = session.startTime; const randomId = (0, crypto_1.randomBytes)(4).toString('hex'); return `session-${timestamp}-${randomId}.json`; } async findSessionFile(sessionId) { try { const files = await fs.readdir(this.sessionsDir); return files.filter(f => { if (!f.startsWith('session-') || !f.endsWith('.json')) { return false; } try { const filepath = path.join(this.sessionsDir, f); const data = (0, fs_1.readFileSync)(filepath, 'utf8'); const session = JSON.parse(data); return session.id === sessionId; } catch { return false; } }); } catch { return []; } } createSession(workspace, issueNumber, provider) { const id = `${Date.now()}-${(0, crypto_1.randomBytes)(8).toString('hex')}`; return { id, startTime: Date.now(), endTime: null, status: 'active', workspace, issueNumber, provider, }; } } exports.SessionManager = SessionManager;