@dailyautomations/terminal-logger
Version:
Terminal command logger with Supabase sync for swarm prompts
125 lines (124 loc) • 4.42 kB
JavaScript
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.LocalStorage = void 0;
const fs = __importStar(require("fs/promises"));
const path = __importStar(require("path"));
const logger_1 = require("./logger");
const uuid_1 = require("uuid");
const os = __importStar(require("os"));
class LocalStorage {
logFilePath;
commandLog;
constructor(logFilePath) {
this.logFilePath = logFilePath;
this.commandLog = this.createEmptyLog();
}
async initialize() {
try {
await this.ensureLogDirectory();
await this.loadOrCreateLog();
logger_1.logger.info('Local storage initialized', { path: this.logFilePath });
}
catch (error) {
logger_1.logger.error('Failed to initialize local storage', error);
throw error;
}
}
async ensureLogDirectory() {
const dir = path.dirname(this.logFilePath);
await fs.mkdir(dir, { recursive: true });
}
createEmptyLog() {
const sessionInfo = {
id: (0, uuid_1.v4)(),
startTime: new Date(),
user: os.userInfo().username,
hostname: os.hostname(),
shell: process.env.SHELL || 'unknown',
terminal: process.env.TERM || 'unknown'
};
return {
version: '1.0.0',
session: sessionInfo,
commands: []
};
}
async loadOrCreateLog() {
try {
const data = await fs.readFile(this.logFilePath, 'utf-8');
this.commandLog = JSON.parse(data);
logger_1.logger.info('Loaded existing command log', {
commands: this.commandLog.commands.length
});
}
catch (error) {
if (error.code === 'ENOENT') {
await this.saveLog();
logger_1.logger.info('Created new command log file');
}
else {
throw error;
}
}
}
async addCommand(command) {
this.commandLog.commands.push(command);
await this.saveLog();
logger_1.logger.debug('Command added to log', { command: command.command });
}
async getCommands(limit) {
const commands = this.commandLog.commands;
if (limit) {
return commands.slice(-limit);
}
return commands;
}
async getUnsyncedCommands(lastSyncTime) {
return this.commandLog.commands.filter(cmd => new Date(cmd.timestamp) > lastSyncTime);
}
async markCommandsSynced(commandIds) {
// In a more sophisticated implementation, we might track sync status
// For now, we just log the operation
logger_1.logger.info('Commands marked as synced', { count: commandIds.length });
}
async saveLog() {
await fs.writeFile(this.logFilePath, JSON.stringify(this.commandLog, null, 2), 'utf-8');
}
async getSessionInfo() {
return this.commandLog.session;
}
}
exports.LocalStorage = LocalStorage;
;