signaltrail / tree / src/narrative.js · raw (this view has old GitHub links rewritten for the mirror; raw is byte-exact)
// Groups extracted posts by token and decides whether trusted voices CONVERGE on it (deterministic).
const r2 = (x) => Math.round(x * 100) / 100;
// creators: [{handle, weight 0..1}], config.signal: {window_hours, min_voices, min_weighted_score, max_bearish_voices}
export function narratives(extracted, creators, signal, nowIso) {
  const now = Date.parse(nowIso);
  const W = signal.window_hours * 3600e3;
  const weight = (h) => creators.find((c) => c.handle.toLowerCase() === String(h).toLowerCase())?.weight ?? 0;
  const by = new Map();
  for (const e of extracted) {
    if (Date.parse(e.ts) < now - W || Date.parse(e.ts) > now + 60e3) continue; // outside the signal window
    if (!weight(e.author)) continue; // only user-selected creators count
    for (const t of e.tokens) {
      const k = t.symbol;
      if (!by.has(k)) by.set(k, { token: t, posts: [] });
      by.get(k).posts.push(e);
    }
  }
  const out = [];
  for (const [symbol, { token, posts }] of by) {
    // one stance per creator: their latest post on this token
    const latest = new Map();
    for (const p of [...posts].sort((a, b) => Date.parse(a.ts) - Date.parse(b.ts))) latest.set(p.author.toLowerCase(), p);
    const stances = [...latest.values()];
    const bulls = stances.filter((p) => p.sentiment.label === 'bullish');
    const bears = stances.filter((p) => p.sentiment.label === 'bearish');
    const wsum = stances.reduce((s, p) => s + weight(p.author), 0) || 1;
    const weighted = r2(stances.reduce((s, p) => s + weight(p.author) * p.sentiment.score * (0.5 + 0.5 * p.conviction.score), 0) / wsum);
    const themeCount = {};
    for (const p of stances) for (const th of p.themes) themeCount[th] = (themeCount[th] || 0) + 1;
    const theme = Object.entries(themeCount).sort((a, b) => b[1] - a[1] || a[0].localeCompare(b[0]))[0]?.[0] || 'unlabelled';
    const avg = (f) => r2(stances.reduce((s, p) => s + f(p), 0) / stances.length);
    const converged = bulls.length >= signal.min_voices && weighted >= signal.min_weighted_score && bears.length <= signal.max_bearish_voices;
    out.push({
      symbol, address: token.address, listed: token.listed, narrative: theme,
      voices: { bullish: bulls.map((p) => p.author), bearish: bears.map((p) => p.author), neutral: stances.filter((p) => p.sentiment.label === 'neutral').map((p) => p.author) },
      weighted_score: weighted, avg_conviction: avg((p) => p.conviction.score), avg_urgency: avg((p) => p.urgency.score),
      first_seen: stances.map((p) => p.ts).sort()[0], post_ids: stances.map((p) => p.post_id),
      converged,
      why: converged
        ? `${bulls.length} tracked voices bullish within ${signal.window_hours}h (need ${signal.min_voices}), weighted score ${weighted} ≥ ${signal.min_weighted_score}, ${bears.length} bearish (max ${signal.max_bearish_voices})`
        : `no convergence: ${bulls.length} bullish (need ${signal.min_voices}), weighted ${weighted} (need ${signal.min_weighted_score}), ${bears.length} bearish (max ${signal.max_bearish_voices})`,
    });
  }
  return out.sort((a, b) => b.weighted_score - a.weighted_score || a.symbol.localeCompare(b.symbol));
}

agentgate-source: code mirror on Cloudflare Pages. All repos