Files
P42_godDaemons/GPS/agentStore.js
T
2026-06-12 17:05:35 +00:00

59 lines
1.9 KiB
JavaScript

export class AgentStore {
constructor(systemCnx, storage, debug = false) {
this.cnx = systemCnx
this.storage = storage
this.debug = debug
}
agentHashKey(agentId) {
return(this.storage.agentHashKey.replace(/\[UID\]/g, agentId))
}
async exportSegment(agent, eventType) {
try {
const record = {
eventType,
id: agent.id,
position: { ...agent.position },
vector: { ...agent.vector },
since: agent.since,
generation: agent.generation ?? 0,
at: new Date().toISOString(),
}
await this.cnx.redisHset(this.agentHashKey(agent.id), 'segment', record)
await this.cnx.redisSadd(this.storage.agentsIndexKey, agent.id)
await this.cnx.redisXadd(
this.storage.positionsStream,
record,
this.storage.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) {
try {
const record = {
eventType: 'remove',
id: agentId,
at: new Date().toISOString(),
}
await this.cnx.redisDel(this.agentHashKey(agentId))
await this.cnx.redisSrem(this.storage.agentsIndexKey, agentId)
await this.cnx.redisXadd(
this.storage.positionsStream,
record,
this.storage.streamMaxLen ?? ''
)
if(this.debug) console.log(`[GPS] Exported remove ${agentId}`)
} catch(err) {
console.error(`[GPS] Failed to export remove for ${agentId}:`, err)
}
}
}