// Deterministic, explainable signal extraction from one creator post. No model calls: same input -> same output.
export const RULES_VERSION = 'signaltrail-rules-2026-10-22';
const BULL = ['bullish', 'send it', 'undervalued', 'accumulating', 'accumulate', 'breakout', 'breaking out', 'higher', 'moon', 'strong', 'buying', 'bought', 'long', 'adding', 'loaded', 'early', 'ripping', 'uptrend', 'outperform', 'love', 'cheap', 'bid', 'reclaim', 'squeeze', 'growth', 'leader', 'winner'];
const BEAR = ['bearish', 'dump', 'dumping', 'rug', 'scam', 'sell', 'selling', 'sold', 'short', 'avoid', 'overvalued', 'exit', 'careful', 'topped', 'weak', 'dead', 'unlock', 'downtrend', 'trimmed', 'trimming', 'fade', 'exhausted', 'red flag', 'honeypot'];
const NEGATORS = ['not', "isn't", "don't", 'no', 'never', "aren't", "wasn't"];
const CONVICTION = ['high conviction', 'all in', 'biggest position', 'largest position', 'loaded', 'size', 'sizing up', 'adding more', "i'm in", 'max', 'core holding', 'conviction', 'never selling', 'generational'];
const URGENCY = ['now', 'today', 'tonight', 'asap', 'before', 'last chance', "don't miss", 'hours', 'fast', 'quick', 'immediately', 'hurry', 'right now', 'next 24h', 'this week'];
export const THEMES = {
'ai-agents': ['agent', 'agents', 'ai', 'autonomous', 'agentic'],
'base-defi': ['dex', 'volume', 'fees', 'liquidity', 'tvl', 'emissions', 've', 'yield', 'revenue'],
'memes': ['meme', 'memecoin', 'frog', 'dog', 'cat', 'community', 'culture'],
'pump-hype': ['100x', '1000x', 'gem', 'moonshot', 'lambo', 'guaranteed', 'free money'],
};
const words = (t) => t.toLowerCase().replace(/[^a-z0-9$'\s.-]/g, ' ').split(/\s+/).filter(Boolean);
function findTerms(text, terms) {
const low = ' ' + text.toLowerCase().replace(/[^a-z0-9$'\s-]/g, ' ').replace(/\s+/g, ' ') + ' ';
const hits = [];
for (const t of terms) {
let i = low.indexOf(' ' + t + ' ');
while (i >= 0) { hits.push({ term: t, at: i }); i = low.indexOf(' ' + t + ' ', i + 1); }
}
return { low, hits };
}
function polarity(text) {
const b = findTerms(text, BULL), r = findTerms(text, BEAR);
const negated = (low, at) => { const before = low.slice(Math.max(0, at - 14), at).trim().split(' ').pop(); return NEGATORS.includes(before); };
const bull = [], bear = [];
for (const h of b.hits) (negated(b.low, h.at) ? bear : bull).push(negated(b.low, h.at) ? 'not ' + h.term : h.term);
for (const h of r.hits) (negated(r.low, h.at) ? bull : bear).push(negated(r.low, h.at) ? 'not ' + h.term : h.term);
const n = bull.length + bear.length;
return { score: n ? Math.round(((bull.length - bear.length) / n) * 100) / 100 : 0, bull, bear };
}
const clamp01 = (x) => Math.max(0, Math.min(1, Math.round(x * 100) / 100));
// registry: [{symbol, address, chain, names:[]}]
export function extract(post, registry) {
const text = String(post.text || '');
const tokens = new Map();
for (const m of text.matchAll(/\$([A-Za-z][A-Za-z0-9]{1,11})\b/g)) {
const r = registry.find((x) => x.symbol.toLowerCase() === m[1].toLowerCase());
tokens.set(r ? r.symbol : m[1].toUpperCase(), { symbol: r ? r.symbol : m[1].toUpperCase(), address: r?.address || null, via: '$' + m[1], listed: !!r });
}
for (const m of text.matchAll(/\b0x[0-9a-fA-F]{40}\b/g)) {
const r = registry.find((x) => x.address.toLowerCase() === m[0].toLowerCase());
const sym = r ? r.symbol : m[0].toLowerCase();
tokens.set(sym, { symbol: sym, address: m[0].toLowerCase(), via: 'contract address', listed: !!r });
}
const w = words(text);
for (const r of registry) for (const n of r.names || []) if (!tokens.has(r.symbol) && w.includes(n.toLowerCase())) tokens.set(r.symbol, { symbol: r.symbol, address: r.address, via: 'name "' + n + '"', listed: true });
const pol = polarity(text);
const conv = findTerms(text, CONVICTION).hits.map((h) => h.term);
const urg = findTerms(text, URGENCY).hits.map((h) => h.term);
const letters = text.replace(/[^A-Za-z]/g, '');
const caps = letters.length ? letters.replace(/[^A-Z]/g, '').length / letters.length : 0;
const bangs = (text.match(/!/g) || []).length;
const themes = Object.entries(THEMES).filter(([, ts]) => ts.some((t) => w.includes(t))).map(([k]) => k);
return {
post_id: post.id, author: post.author, source: post.source || 'x', ts: post.ts,
tokens: [...tokens.values()],
sentiment: { score: pol.score, label: pol.score > 0.2 ? 'bullish' : pol.score < -0.2 ? 'bearish' : 'neutral', bull_terms: pol.bull, bear_terms: pol.bear },
conviction: { score: clamp01(conv.length * 0.35 + (caps > 0.5 ? 0.15 : 0)), terms: conv },
urgency: { score: clamp01(urg.length * 0.3 + Math.min(bangs, 4) * 0.1 + (caps > 0.5 ? 0.2 : 0)), terms: urg, exclamations: bangs },
themes,
};
}
agentgate-source: code mirror on Cloudflare Pages. All repos