routes_index.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. const path = require('path')
  2. const fs = require('fs')
  3. const ROUTES = [
  4. { path: '/public/latest', mod: null, description: 'home, latest posts, public timeline, news, recent activity' },
  5. { path: '/feed', mod: 'feedMod', description: 'feed, microblog, opinions, share thoughts, vote on posts, refeeds' },
  6. { path: '/forum', mod: 'forumMod', description: 'forum, discussions, threads, debates, conversation by category' },
  7. { path: '/inhabitants', mod: 'inhabitantsMod', description: 'inhabitants, users, people, profiles, contacts, follow, block' },
  8. { path: '/tribes', mod: 'tribeMod', description: 'tribes, groups, communities, private rooms, sub-tribes, governance' },
  9. { path: '/chats', mod: 'chatMod', description: 'chats, messaging, encrypted rooms, group conversations' },
  10. { path: '/pads', mod: 'padMod', description: 'pads, collaborative editor, shared notes, encrypted documents' },
  11. { path: '/calendars', mod: 'calendarMod', description: 'calendar, events by date, schedule, reminders, recurring dates' },
  12. { path: '/maps', mod: 'mapMod', description: 'maps, locations, markers, geography, places' },
  13. { path: '/events', mod: 'eventMod', description: 'events, agenda, meetups, gatherings, RSVP' },
  14. { path: '/agenda', mod: 'agendaMod', description: 'agenda, scheduled items, upcoming, my dates' },
  15. { path: '/tasks', mod: 'taskMod', description: 'tasks, todo, assignments, work items, priorities' },
  16. { path: '/projects', mod: 'projectMod', description: 'projects, milestones, backers, crowdfunding, bounties' },
  17. { path: '/jobs', mod: 'jobMod', description: 'jobs, work, hiring, salaries, vacancies, applications' },
  18. { path: '/market', mod: 'marketMod', description: 'market, marketplace, buy, sell, items, auctions, ECO' },
  19. { path: '/shops', mod: 'shopMod', description: 'shops, stores, products, ecommerce, vendors' },
  20. { path: '/banking', mod: 'bankingMod', description: 'banking, wallet, ECO balance, send money, transfers, payments, UBI claim' },
  21. { path: '/transfers', mod: 'transferMod', description: 'transfers, payments, money movements, ECO transactions, history' },
  22. { path: '/wallet', mod: 'walletMod', description: 'wallet, ECOin address, send and receive, QR code, balance' },
  23. { path: '/parliament', mod: 'parliamentMod', description: 'parliament, governance, government, proposals, laws, leaders, voting' },
  24. { path: '/courts', mod: 'courtsMod', description: 'courts, judges, accusations, mediators, justice, disputes' },
  25. { path: '/votations', mod: 'votationsMod', description: 'votations, polls, surveys, multi-option votes' },
  26. { path: '/votes', mod: 'votesMod', description: 'votes, ballots, decisions, polling, voting' },
  27. { path: '/opinions', mod: 'opinionsMod', description: 'opinions, reactions, ratings, sentiment, expressing views' },
  28. { path: '/trending', mod: 'trendingMod', description: 'trending, popular, hot, top voted, what is being discussed' },
  29. { path: '/reports', mod: 'reportsMod', description: 'reports, bug reports, abuse, incidents, severity, confirmations' },
  30. { path: '/audios', mod: 'audioMod', description: 'audios, music, podcasts, voice recordings, sound files' },
  31. { path: '/videos', mod: 'videoMod', description: 'videos, films, clips, recordings, watch' },
  32. { path: '/images', mod: 'imageMod', description: 'images, photos, pictures, gallery, memes' },
  33. { path: '/documents', mod: 'documentMod', description: 'documents, PDFs, files, papers, references' },
  34. { path: '/bookmarks', mod: 'bookmarkMod', description: 'bookmarks, links, saved websites, favorites' },
  35. { path: '/torrents', mod: 'torrentMod', description: 'torrents, magnet links, file sharing, downloads' },
  36. { path: '/tags', mod: 'tagsMod', description: 'tags, hashtags, topics, categories, labels' },
  37. { path: '/search', mod: null, description: 'search, find, query, lookup' },
  38. { path: '/inbox', mod: null, description: 'inbox, notifications, mentions, alerts, messages addressed to me' },
  39. { path: '/pm', mod: 'privateMessageMod', description: 'private messages, direct messages, DMs, encrypted PM' },
  40. { path: '/publish', mod: null, description: 'publish, write, create post, new entry, compose' },
  41. { path: '/games', mod: 'gameMod', description: 'games, play, mini-games, scoring, fun' },
  42. { path: '/pixelia', mod: 'pixeliaMod', description: 'pixelia, pixel canvas, draw, collaborative pixel art' },
  43. { path: '/cv', mod: 'cvMod', description: 'cv, curriculum, resume, my profile, skills, experiences' },
  44. { path: '/legacy', mod: 'legacyMod', description: 'legacy, export data, import, backup, restore identity' },
  45. { path: '/cipher', mod: 'cipherMod', description: 'cipher, encrypt, decrypt, password, vault' },
  46. { path: '/stats', mod: 'statsMod', description: 'stats, statistics, KPIs, metrics, dashboard, carbon footprint' },
  47. { path: '/blockchain', mod: 'blockchainMod', description: 'blockchain, blocks, explorer, ledger, chain' },
  48. { path: '/peers', mod: 'peersMod', description: 'peers, connections, network, nodes, who am I connected to' },
  49. { path: '/invites', mod: 'invitesMod', description: 'invites, pub invitations, join code, follow PUB' },
  50. { path: '/graphos', mod: 'graphosMod', description: 'graphos, network map, visualization, relationship graph' },
  51. { path: '/modules', mod: null, description: 'modules, features, enable disable plugins, settings' },
  52. { path: '/settings', mod: null, description: 'settings, preferences, language, theme, configuration' },
  53. { path: '/favorites', mod: 'favoritesMod', description: 'favorites, starred items, saved content' },
  54. { path: '/logs', mod: 'logsMod', description: 'logs, life log, personal records, journal, experiences' }
  55. ]
  56. const CACHE_FILE = path.join(__dirname, 'embeddings', 'routes_cache.json')
  57. let cache = null
  58. const buildCacheKey = () => ROUTES.map(r => r.path + '|' + r.description).join('\n')
  59. const loadCache = () => {
  60. try {
  61. if (!fs.existsSync(CACHE_FILE)) return null
  62. const data = JSON.parse(fs.readFileSync(CACHE_FILE, 'utf8'))
  63. if (data.key === buildCacheKey() && Array.isArray(data.entries)) return data.entries
  64. return null
  65. } catch (_) {
  66. return null
  67. }
  68. }
  69. const saveCache = (entries) => {
  70. try {
  71. fs.mkdirSync(path.dirname(CACHE_FILE), { recursive: true })
  72. fs.writeFileSync(CACHE_FILE, JSON.stringify({ key: buildCacheKey(), entries }, null, 2), 'utf8')
  73. } catch (_) {}
  74. }
  75. const ensureIndex = async ({ embed }) => {
  76. if (cache) return cache
  77. const cached = loadCache()
  78. if (cached) { cache = cached; return cache }
  79. const entries = []
  80. for (const r of ROUTES) {
  81. const vec = await embed(r.description)
  82. if (!vec) return null
  83. entries.push({ path: r.path, mod: r.mod, vector: vec })
  84. }
  85. cache = entries
  86. saveCache(entries)
  87. return cache
  88. }
  89. const resolveBest = async (queryVector, { isModuleEnabled, threshold = 0.4, embed } = {}) => {
  90. const idx = await ensureIndex({ embed })
  91. if (!idx) return null
  92. let best = null
  93. for (const entry of idx) {
  94. if (entry.mod && typeof isModuleEnabled === 'function' && !isModuleEnabled(entry.mod)) continue
  95. const score = (() => {
  96. let s = 0
  97. for (let i = 0; i < queryVector.length; i++) s += queryVector[i] * entry.vector[i]
  98. return s
  99. })()
  100. if (!best || score > best.score) best = { path: entry.path, score }
  101. }
  102. if (!best || best.score < threshold) return null
  103. return best
  104. }
  105. module.exports = { ROUTES, ensureIndex, resolveBest }