dynamic-interaction
Version:
Dynamic interaction 动态交互mcp,用于cursor、windsurf、trae 等 AI 智能编辑器 Agent 运行时交互使用
79 lines (78 loc) • 2.71 kB
JavaScript
;
/**
* 通知存储
* 重构并移动到notifications目录
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.notificationStore = exports.NotificationStore = void 0;
const logger_1 = require("../../logger");
class NotificationStore {
static instance;
notifications = new Map();
constructor() { }
static getInstance() {
if (!NotificationStore.instance) {
NotificationStore.instance = new NotificationStore();
}
return NotificationStore.instance;
}
addNotification(notification) {
this.notifications.set(notification.id, notification);
logger_1.logger.info(`通知已添加到存储,ID: ${notification.id}`);
}
getNotification(id) {
return this.notifications.get(id);
}
acknowledgeNotification(id) {
const notification = this.notifications.get(id);
if (!notification) {
logger_1.logger.warn(`尝试确认不存在的通知,ID: ${id}`);
return false;
}
notification.acknowledged = true;
notification.acknowledgedAt = Date.now();
logger_1.logger.info(`通知已确认,ID: ${id}`);
return true;
}
removeNotification(id) {
const success = this.notifications.delete(id);
if (success) {
logger_1.logger.info(`通知已从存储中移除,ID: ${id}`);
}
return success;
}
getUnacknowledgedNotifications() {
return Array.from(this.notifications.values())
.filter(notification => !notification.acknowledged);
}
getAllNotifications() {
return Array.from(this.notifications.values());
}
getNotificationCount() {
return this.notifications.size;
}
getUnacknowledgedCount() {
return this.getUnacknowledgedNotifications().length;
}
clearOldNotifications(maxAge = 24 * 60 * 60 * 1000) {
const now = Date.now();
let removedCount = 0;
for (const [id, notification] of this.notifications.entries()) {
if (now - notification.createdAt > maxAge && notification.acknowledged) {
this.notifications.delete(id);
removedCount++;
}
}
if (removedCount > 0) {
logger_1.logger.info(`已清理 ${removedCount} 个过期通知`);
}
return removedCount;
}
clear() {
const count = this.notifications.size;
this.notifications.clear();
logger_1.logger.info(`通知存储已清空,清理了 ${count} 个通知`);
}
}
exports.NotificationStore = NotificationStore;
exports.notificationStore = NotificationStore.getInstance();