import Pipe from 'bare-pipe'
import FramedStream from 'framed-stream'
import b4a from 'b4a'
let rpcId = 0
const pipe = new Pipe('/tmp/keet-daemon.sock')
const framed = new FramedStream(pipe)
const pending = new Map()
framed.on('data', buf => {
const msg = JSON.parse(b4a.toString(buf))
const cb = pending.get(msg.id)
if (cb) { pending.delete(msg.id); cb(msg) }
})
function call (method, params = {}) {
return new Promise((resolve, reject) => {
const id = ++rpcId
pending.set(id, msg => {
if (msg.ok !== undefined && msg.data !== undefined) msg.result = msg.data
if (msg.error) reject(new Error(msg.error.message || String(msg.error)))
else resolve(msg.result)
})
framed.write(b4a.from(JSON.stringify({ jsonrpc: '2.0', id, method, params })))
})
}
const result = await call('rooms')
const rooms = Array.isArray(result) ? result : []
console.log('Room metadata audit:\n')
const catCounts = {}
for (const r of rooms) {
const rid = r.roomId || r.id
const title = r.title || '(untitled)'
let meta = {}
try { meta = await call('meta.get', { roomId: rid }) } catch {}
if (!meta) meta = {}
const cat = meta.category || 'NONE'
const topics = meta.topics || []
catCounts[cat] = (catCounts[cat] || 0) + 1
console.log(title.padEnd(28), 'cat=' + cat.padEnd(14), 'topics=' + JSON.stringify(topics))
}
console.log('\nCategory distribution:')
for (const [cat, count] of Object.entries(catCounts).sort((a, b) => b[1] - a[1])) {
console.log(' ' + cat.padEnd(16) + count + ' rooms')
}
pipe.destroy()
setTimeout(() => Bare.exit(0), 500)