export class AgentStore { constructor(systemCnx, gpsStorage, debug = false) { this.cnx = systemCnx this.gpsStorage = gpsStorage this.debug = debug } agentHashKey(agentId) { return(this.gpsStorage.agentHashKey.replace(/\[UID\]/g, agentId)) } async clearAll() { try { const ids = await this.cnx.redisSmembers(this.gpsStorage.agentsIndexKey) for(const id of ids) { await this.cnx.redisDel(this.agentHashKey(id)) } await this.cnx.redisDel(this.gpsStorage.agentsIndexKey) if(this.debug) console.log(`[GPS] Cleared system agent store (${ids.length} agent(s))`) } catch(err) { console.error('[GPS] Failed to clear system agent store:', err) } } async exportSegment(agent, eventType, simT = 0, simulationId = null) { try { const record = { eventType, id: agent.id, position: { ...agent.position }, vector: { ...agent.vector }, since: agent.since, generation: agent.generation ?? 0, t: simT, simulationId, } await this.cnx.redisHset(this.agentHashKey(agent.id), 'segment', record) await this.cnx.redisSadd(this.gpsStorage.agentsIndexKey, agent.id) await this.cnx.redisXadd( this.gpsStorage.positionsStream, record, this.gpsStorage.streamMaxLen ?? '' ) if(this.debug) console.log(`[GPS] Exported segment ${agent.id} (${eventType})`) } catch(err) { console.error(`[GPS] Failed to export segment for ${agent.id}:`, err) } } async exportRemove(agentId, simT = 0) { try { const record = { eventType: 'remove', id: agentId, t: simT, } await this.cnx.redisDel(this.agentHashKey(agentId)) await this.cnx.redisSrem(this.gpsStorage.agentsIndexKey, agentId) await this.cnx.redisXadd( this.gpsStorage.positionsStream, record, this.gpsStorage.streamMaxLen ?? '' ) if(this.debug) console.log(`[GPS] Exported remove ${agentId}`) } catch(err) { console.error(`[GPS] Failed to export remove for ${agentId}:`, err) } } }