routes_index.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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: '/inhabitants?filter=SUGGESTED', mod: 'inhabitantsMod', description: 'suggested inhabitants, recommendations, who to follow, similar people, people you might know, friend suggestions' },
  9. { path: '/inhabitants?filter=MATCHSKILLS', mod: 'inhabitantsMod', description: 'match skills, people with same skills, common skills, professional matches, find collaborators, who shares my expertise, skill overlap' },
  10. { path: '/inhabitants?filter=CVs', mod: 'inhabitantsMod', description: 'curriculums, CVs, resumes, professional profiles, people with experience, find expertise' },
  11. { path: '/inhabitants?filter=TOP%20KARMA', mod: 'inhabitantsMod', description: 'top karma, most active inhabitants, highest reputation, leaderboard' },
  12. { path: '/inhabitants?filter=TOP%20ECO', mod: 'inhabitantsMod', description: 'top eco, most ecological, least carbon footprint, sustainable users, efficient inhabitants' },
  13. { path: '/inhabitants?filter=TOP%20ACTIVITY', mod: 'inhabitantsMod', description: 'top activity, most recently active inhabitants, fresh users, recently online' },
  14. { path: '/inhabitants?filter=contacts', mod: 'inhabitantsMod', description: 'my contacts, who I follow, my network, friends list, mutuals' },
  15. { path: '/inhabitants?filter=GALLERY', mod: 'inhabitantsMod', description: 'gallery of inhabitants, all avatars, visual list, photos' },
  16. { path: '/tribes', mod: 'tribeMod', description: 'tribes, groups, communities, private rooms, sub-tribes, governance' },
  17. { path: '/chats', mod: 'chatMod', description: 'chats, messaging, encrypted rooms, group conversations' },
  18. { path: '/pads', mod: 'padMod', description: 'pads, collaborative editor, shared notes, encrypted documents' },
  19. { path: '/calendars', mod: 'calendarMod', description: 'calendar, events by date, schedule, reminders, recurring dates' },
  20. { path: '/maps', mod: 'mapMod', description: 'maps, locations, markers, geography, places' },
  21. { path: '/events', mod: 'eventMod', description: 'events, agenda, meetups, gatherings, RSVP' },
  22. { path: '/agenda', mod: 'agendaMod', description: 'agenda, scheduled items, upcoming, my dates' },
  23. { path: '/tasks', mod: 'taskMod', description: 'tasks, todo, assignments, work items, priorities' },
  24. { path: '/projects', mod: 'projectMod', description: 'projects, milestones, backers, crowdfunding, bounties' },
  25. { path: '/jobs', mod: 'jobMod', description: 'jobs, work, hiring, salaries, vacancies, applications' },
  26. { path: '/market', mod: 'marketMod', description: 'market, marketplace, buy, sell, items, auctions, ECO' },
  27. { path: '/shops', mod: 'shopMod', description: 'shops, stores, products, ecommerce, vendors' },
  28. { path: '/banking', mod: 'bankingMod', description: 'banking, wallet, ECO balance, send money, transfers, payments, UBI claim, karma score, eco tax penalty, ECOin value' },
  29. { path: '/transfers', mod: 'transferMod', description: 'transfers, payments, money movements, ECO transactions, history, smart contracts, contract PDF, export contract' },
  30. { path: '/wallet', mod: 'walletMod', description: 'wallet, ECOin address, send and receive, QR code, balance' },
  31. { path: '/parliament', mod: 'parliamentMod', description: 'parliament, governance, government, proposals, laws, leaders, voting' },
  32. { path: '/courts', mod: 'courtsMod', description: 'courts, judges, accusations, mediators, justice, disputes' },
  33. { path: '/votations', mod: 'votationsMod', description: 'votations, polls, surveys, multi-option votes' },
  34. { path: '/votes', mod: 'votesMod', description: 'votes, ballots, decisions, polling, voting' },
  35. { path: '/opinions', mod: 'opinionsMod', description: 'opinions, reactions, ratings, sentiment, expressing views' },
  36. { path: '/trending', mod: 'trendingMod', description: 'trending, popular, hot, top voted, what is being discussed' },
  37. { path: '/reports', mod: 'reportsMod', description: 'reports, bug reports, abuse, incidents, severity, confirmations' },
  38. { path: '/audios', mod: 'audioMod', description: 'audios, music, podcasts, voice recordings, sound files' },
  39. { path: '/videos', mod: 'videoMod', description: 'videos, films, clips, recordings, watch' },
  40. { path: '/images', mod: 'imageMod', description: 'images, photos, pictures, gallery, memes' },
  41. { path: '/documents', mod: 'documentMod', description: 'documents, PDFs, files, papers, references' },
  42. { path: '/bookmarks', mod: 'bookmarkMod', description: 'bookmarks, links, saved websites, favorites' },
  43. { path: '/torrents', mod: 'torrentMod', description: 'torrents, magnet links, file sharing, downloads' },
  44. { path: '/tags', mod: 'tagsMod', description: 'tags, hashtags, topics, categories, labels' },
  45. { path: '/search', mod: null, description: 'search, find, query, lookup' },
  46. { path: '/inbox', mod: null, description: 'inbox, notifications, mentions, alerts, messages addressed to me' },
  47. { path: '/pm', mod: 'privateMessageMod', description: 'private messages, direct messages, DMs, encrypted PM' },
  48. { path: '/publish', mod: null, description: 'publish, write, create post, new entry, compose' },
  49. { path: '/games', mod: 'gameMod', description: 'games, play, mini-games, scoring, fun' },
  50. { path: '/pixelia', mod: 'pixeliaMod', description: 'pixelia, pixel canvas, draw, collaborative pixel art' },
  51. { path: '/cv', mod: 'cvMod', description: 'cv, curriculum, resume, my profile, skills, experiences' },
  52. { path: '/legacy', mod: 'legacyMod', description: 'legacy, export data, import, backup, restore identity' },
  53. { path: '/cipher', mod: 'cipherMod', description: 'cipher, encrypt, decrypt, password, vault' },
  54. { path: '/stats', mod: 'statsMod', description: 'stats, statistics, KPIs, metrics, dashboard, carbon footprint' },
  55. { path: '/blockchain', mod: 'blockchainMod', description: 'blockchain, blocks, explorer, ledger, chain' },
  56. { path: '/peers', mod: 'peersMod', description: 'peers, connections, network, nodes, who am I connected to, LAN, refresh discovery, export peer list, import peer list, remove idle' },
  57. { path: '/invites', mod: 'invitesMod', description: 'invites, pub invitations, join code, follow PUB, federations, federated networks, import pubs, export pubs, unreachable pubs' },
  58. { path: '/graphos', mod: 'graphosMod', description: 'graphos, network map, visualization, relationship graph' },
  59. { path: '/modules', mod: null, description: 'modules, features, enable disable plugins, settings' },
  60. { path: '/settings', mod: null, description: 'settings, preferences, language, theme, configuration' },
  61. { path: '/favorites', mod: 'favoritesMod', description: 'favorites, starred items, saved content' },
  62. { path: '/logs', mod: 'logsMod', description: 'logs, life log, personal records, journal, experiences' },
  63. { path: '/melody', mod: 'melodyMod', description: 'melody, sound of my blockchain, music, generate sound, audio of blocks, sonification' },
  64. { path: '/profile', mod: null, description: 'my profile, my avatar, my page, my identity, my data' },
  65. { path: '/profile/edit', mod: null, description: 'edit profile, edit avatar, change name, change description, visibility prefs, sensors, eco tax toggle' },
  66. { path: '/blockexplorer', mod: 'blockchainMod', description: 'blockexplorer, blockchain explorer, blocks, ledger, carbon footprint per block, chain history' },
  67. { path: '/stats?filter=ALL', mod: 'statsMod', description: 'global stats, network kpis, total carbon footprint, total inhabitants, network size' },
  68. { path: '/stats?filter=MINE', mod: 'statsMod', description: 'my stats, my carbon footprint, my activity numbers, personal kpis' },
  69. { path: '/tribes/new', mod: 'tribeMod', description: 'create tribe, new tribe, new group, start community, create private room' },
  70. { path: '/chats/new', mod: 'chatMod', description: 'create chat, new chat, start conversation, new encrypted room' },
  71. { path: '/pads/new', mod: 'padMod', description: 'create pad, new pad, new collaborative document, start shared note' },
  72. { path: '/calendars/new', mod: 'calendarMod', description: 'create calendar, new calendar, start schedule' },
  73. { path: '/maps/new', mod: 'mapMod', description: 'create map, new map, new offline map' },
  74. { path: '/events/new', mod: 'eventMod', description: 'create event, new event, schedule meetup' },
  75. { path: '/projects/new', mod: 'projectMod', description: 'create project, new project, start crowdfunding' },
  76. { path: '/jobs/new', mod: 'jobMod', description: 'create job, post job offer, new vacancy, hire' },
  77. { path: '/market/new', mod: 'marketMod', description: 'create market item, sell something, new auction, list for sale' },
  78. { path: '/shops/new', mod: 'shopMod', description: 'create shop, open store, new vendor, list products' },
  79. { path: '/tasks/new', mod: 'taskMod', description: 'create task, new todo, new assignment' },
  80. { path: '/reports/new', mod: 'reportsMod', description: 'create report, file bug, report issue, report abuse' }
  81. ]
  82. const CACHE_FILE = path.join(__dirname, 'embeddings', 'routes_cache.json')
  83. let cache = null
  84. const buildCacheKey = () => ROUTES.map(r => r.path + '|' + r.description).join('\n')
  85. const loadCache = () => {
  86. try {
  87. if (!fs.existsSync(CACHE_FILE)) return null
  88. const data = JSON.parse(fs.readFileSync(CACHE_FILE, 'utf8'))
  89. if (data.key === buildCacheKey() && Array.isArray(data.entries)) return data.entries
  90. return null
  91. } catch (_) {
  92. return null
  93. }
  94. }
  95. const saveCache = (entries) => {
  96. try {
  97. fs.mkdirSync(path.dirname(CACHE_FILE), { recursive: true })
  98. fs.writeFileSync(CACHE_FILE, JSON.stringify({ key: buildCacheKey(), entries }, null, 2), 'utf8')
  99. } catch (_) {}
  100. }
  101. const ensureIndex = async ({ embed }) => {
  102. if (cache) return cache
  103. const cached = loadCache()
  104. if (cached) { cache = cached; return cache }
  105. const entries = []
  106. for (const r of ROUTES) {
  107. const vec = await embed(r.description)
  108. if (!vec) return null
  109. entries.push({ path: r.path, mod: r.mod, vector: vec })
  110. }
  111. cache = entries
  112. saveCache(entries)
  113. return cache
  114. }
  115. const dot = (a, b) => {
  116. let s = 0
  117. const n = Math.min(a.length, b.length)
  118. for (let i = 0; i < n; i++) s += a[i] * b[i]
  119. return s
  120. }
  121. const descriptionByPath = (p) => {
  122. const r = ROUTES.find(x => x.path === p)
  123. return r ? r.description : ''
  124. }
  125. const resolveBest = async (queryVector, { isModuleEnabled, threshold = 0.4, embed } = {}) => {
  126. const idx = await ensureIndex({ embed })
  127. if (!idx) return null
  128. let best = null
  129. for (const entry of idx) {
  130. if (entry.mod && typeof isModuleEnabled === 'function' && !isModuleEnabled(entry.mod)) continue
  131. const score = dot(queryVector, entry.vector)
  132. if (!best || score > best.score) best = { path: entry.path, score }
  133. }
  134. if (!best || best.score < threshold) return null
  135. return best
  136. }
  137. const resolveTopK = async (queryVector, { isModuleEnabled, threshold = 0.35, embed } = {}, k = 5) => {
  138. const idx = await ensureIndex({ embed })
  139. if (!idx) return []
  140. const all = []
  141. for (const entry of idx) {
  142. if (entry.mod && typeof isModuleEnabled === 'function' && !isModuleEnabled(entry.mod)) continue
  143. const score = dot(queryVector, entry.vector)
  144. if (score < threshold) continue
  145. all.push({ path: entry.path, mod: entry.mod, score, description: descriptionByPath(entry.path) })
  146. }
  147. all.sort((a, b) => b.score - a.score)
  148. return all.slice(0, Math.max(1, k|0))
  149. }
  150. const resolveKeywordTopK = ({ isModuleEnabled } = {}, query, k = 8) => {
  151. const tokens = String(query || '').toLowerCase().split(/[^a-z0-9À-ſ]+/).filter(t => t && t.length >= 2)
  152. if (!tokens.length) return []
  153. const all = []
  154. for (const entry of ROUTES) {
  155. if (entry.mod && typeof isModuleEnabled === 'function' && !isModuleEnabled(entry.mod)) continue
  156. const haystack = (entry.description || '').toLowerCase() + ' ' + entry.path.toLowerCase()
  157. let hits = 0
  158. for (const t of tokens) {
  159. if (haystack.includes(t)) hits += 1
  160. }
  161. if (hits === 0) continue
  162. all.push({ path: entry.path, mod: entry.mod, score: hits / tokens.length, description: entry.description })
  163. }
  164. all.sort((a, b) => b.score - a.score)
  165. return all.slice(0, Math.max(1, k|0))
  166. }
  167. module.exports = { ROUTES, ensureIndex, resolveBest, resolveTopK, resolveKeywordTopK }