@polybiouslabs/polybious
Version:
Polybius is a next-generation intelligent agent framework built for adaptability across diverse domains. It merges contextual awareness, multi-agent collaboration, and predictive reasoning to deliver dynamic, self-optimizing performance.
67 lines (66 loc) • 2.23 kB
JavaScript
import fs from 'fs/promises';
import path from 'path';
import { logger } from '../config/logger';
export class HistoryService {
historyPath = 'data/tweet-history.json';
startTime;
tweetsPosted = 0;
lastTweetTime;
nextScheduledTweet;
constructor() {
this.startTime = new Date();
this.initializeHistoryFile();
}
async initializeHistoryFile() {
try {
await fs.mkdir(path.dirname(this.historyPath), { recursive: true });
const exists = await fs.access(this.historyPath).then(() => true).catch(() => false);
if (!exists) {
await fs.writeFile(this.historyPath, JSON.stringify([]));
}
}
catch (error) {
logger.error('Failed to initialize history file', { error });
}
}
async recordTweet(record) {
try {
const history = await this.getHistory();
history.unshift(record);
await fs.writeFile(this.historyPath, JSON.stringify(history, null, 2));
this.tweetsPosted++;
this.lastTweetTime = record.timestamp;
this.nextScheduledTweet = record.metadata.nextScheduledTweet;
logger.info('Tweet recorded in history', { tweetId: record.id });
}
catch (error) {
logger.error('Failed to record tweet in history', { error });
}
}
async getHistory(limit = 10) {
try {
const data = await fs.readFile(this.historyPath, 'utf-8');
const history = JSON.parse(data);
return history.slice(0, limit);
}
catch (error) {
logger.error('Failed to read tweet history', { error });
return [];
}
}
getSystemStatus() {
return {
isOnline: true,
startTime: this.startTime.toISOString(),
lastTweet: this.lastTweetTime || 'Never',
tweetsPosted: this.tweetsPosted,
nextScheduledTweet: this.nextScheduledTweet || 'Not scheduled'
};
}
getLastPostTime() {
return this.lastTweetTime ? new Date(this.lastTweetTime) : null;
}
getTotalPosts() {
return this.tweetsPosted;
}
}