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
const timeout = setTimeout(() => { pending.delete(id); reject(new Error('timeout: ' + method)) }, 15000)
pending.set(id, msg => {
clearTimeout(timeout)
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 algos = [
{name: "category", features: [{type: "category", weight: 1.0}], threshold: 0.1, minSize: 2},
{name: "creator", features: [{type: "creator", weight: 1.0}], threshold: 0.1, minSize: 2},
{name: "activity", features: [{type: "activity", weight: 1.0}], threshold: 0.1, minSize: 2},
{name: "size", features: [{type: "size", weight: 1.0}], threshold: 0.1, minSize: 2},
{name: "topics", features: [{type: "topics", weight: 1.0}], threshold: 0.3, minSize: 2},
{name: "members", features: [{type: "members", weight: 1.0}], threshold: 0.5, minSize: 2},
// Combos — hierarchical clustering
{name: "creator+category", features: [{type: "creator", weight: 1.0}, {type: "category", weight: 0.8}], threshold: 0.3, minSize: 2},
{name: "activity+topics", features: [{type: "activity", weight: 1.0}, {type: "topics", weight: 0.7}], threshold: 0.3, minSize: 2},
{name: "category+activity", features: [{type: "category", weight: 1.0}, {type: "activity", weight: 0.6}], threshold: 0.2, minSize: 2},
]
for (const algo of algos) {
try {
const result = await call('graph.compute', { force: true, algo })
const clusters = result.clusters || []
const edges = result.edges || []
const clusterSummary = clusters.map(c => `${c.label || '?'}(${(c.rooms||[]).length})`).join(', ')
console.log(`\n${algo.name}: ${clusters.length} clusters, ${edges.length} edges`)
console.log(` clusters: ${clusterSummary || 'none'}`)
} catch (err) {
console.log(`\n${algo.name}: ERROR — ${err.message}`)
}
}
pipe.destroy()
setTimeout(() => Bare.exit(0), 500)