grix-connector
Version:
Connect local AI coding agents (Claude, Codex, Gemini, Qwen, DeepSeek, Cursor, OpenCode, Pi, OpenHuman, Reasonix) to the Grix scheduling platform. Also serves as an OpenClaw plugin for Grix channel transport.
2 lines (1 loc) • 1.38 kB
JavaScript
import{readJSONFile as n,writeJSONFileAtomic as h}from"../util/json-file.js";import{log as d}from"../log/index.js";const r=5e3,f=1440*60*1e3;class p{data=new Map;filePath;persistQueue=Promise.resolve();constructor(t){this.filePath=t??null,this.load()}get(t,s){return this.data.get(this.key(t,s))}set(t){this.data.set(this.key(t.sessionId,t.eventId),t),this.prune(),this.schedulePersist()}has(t,s){return this.data.has(this.key(t,s))}key(t,s){return`${t}:${s}`}load(){if(!this.filePath)return;const t=n(this.filePath);if(Array.isArray(t)){for(const s of t)if(s&&typeof s=="object"){const e=s;e.sessionId&&e.eventId&&this.data.set(this.key(e.sessionId,e.eventId),e)}}}prune(){const t=Date.now(),s=Array.from(this.data.values()).sort((e,a)=>a.updatedAt-e.updatedAt);for(const e of s)t-e.updatedAt>f&&this.data.delete(this.key(e.sessionId,e.eventId));if(this.data.size>r){const e=Array.from(this.data.values()).sort((i,o)=>i.updatedAt-o.updatedAt),a=this.data.size-r;for(let i=0;i<a;i++)this.data.delete(this.key(e[i].sessionId,e[i].eventId))}}schedulePersist(){this.filePath&&(this.persistQueue=this.persistQueue.catch(()=>{}).then(()=>this.persist()).catch(t=>{d.error("event-results-store",`Persist failed: ${t instanceof Error?t.message:t}`)}))}async persist(){if(!this.filePath)return;const t=Array.from(this.data.values());await h(this.filePath,t)}}export{p as EventResultsStore};