ruflo 3.5.2 → 3.5.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (521) hide show
  1. package/dist/rvf.manifest.json +295 -0
  2. package/package.json +16 -2
  3. package/src/chat-ui/Dockerfile +25 -0
  4. package/src/chat-ui/patch-mcp-url-safety.sh +28 -0
  5. package/src/chat-ui/static/chatui/icon-144x144.png +0 -0
  6. package/src/chat-ui/static/chatui/omni-welcome.gif +0 -0
  7. package/src/config/config.example.json +76 -0
  8. package/src/mcp-bridge/Dockerfile +45 -0
  9. package/src/mcp-bridge/index.js +1668 -0
  10. package/src/mcp-bridge/mcp-stdio-kernel.js +159 -0
  11. package/src/mcp-bridge/package.json +17 -0
  12. package/src/mcp-bridge/test-harness.js +470 -0
  13. package/src/nginx/Dockerfile +10 -0
  14. package/src/nginx/nginx.conf +67 -0
  15. package/src/nginx/static/favicon-dark.svg +4 -0
  16. package/src/nginx/static/favicon.svg +4 -0
  17. package/src/nginx/static/icon.svg +5 -0
  18. package/src/nginx/static/logo.svg +9 -0
  19. package/src/nginx/static/manifest.json +22 -0
  20. package/src/nginx/static/welcome.js +184 -0
  21. package/src/ruvocal/.claude/skills/add-model-descriptions/SKILL.md +73 -0
  22. package/src/ruvocal/.devcontainer/Dockerfile +9 -0
  23. package/src/ruvocal/.devcontainer/devcontainer.json +36 -0
  24. package/src/ruvocal/.dockerignore +13 -0
  25. package/src/ruvocal/.env +194 -0
  26. package/src/ruvocal/.env.ci +1 -0
  27. package/src/ruvocal/.eslintignore +13 -0
  28. package/src/ruvocal/.eslintrc.cjs +45 -0
  29. package/src/ruvocal/.github/ISSUE_TEMPLATE/bug-report--chat-ui-.md +43 -0
  30. package/src/ruvocal/.github/ISSUE_TEMPLATE/config-support.md +9 -0
  31. package/src/ruvocal/.github/ISSUE_TEMPLATE/feature-request--chat-ui-.md +17 -0
  32. package/src/ruvocal/.github/ISSUE_TEMPLATE/huggingchat.md +11 -0
  33. package/src/ruvocal/.github/release.yml +16 -0
  34. package/src/ruvocal/.github/workflows/build-docs.yml +18 -0
  35. package/src/ruvocal/.github/workflows/build-image.yml +142 -0
  36. package/src/ruvocal/.github/workflows/build-pr-docs.yml +20 -0
  37. package/src/ruvocal/.github/workflows/deploy-dev.yml +63 -0
  38. package/src/ruvocal/.github/workflows/deploy-prod.yml +78 -0
  39. package/src/ruvocal/.github/workflows/lint-and-test.yml +84 -0
  40. package/src/ruvocal/.github/workflows/slugify.yaml +72 -0
  41. package/src/ruvocal/.github/workflows/trufflehog.yml +17 -0
  42. package/src/ruvocal/.github/workflows/upload-pr-documentation.yml +16 -0
  43. package/src/ruvocal/.husky/lint-stage-config.js +4 -0
  44. package/src/ruvocal/.husky/pre-commit +2 -0
  45. package/src/ruvocal/.prettierignore +14 -0
  46. package/src/ruvocal/.prettierrc +7 -0
  47. package/src/ruvocal/.vscode/launch.json +11 -0
  48. package/src/ruvocal/.vscode/settings.json +14 -0
  49. package/src/ruvocal/CLAUDE.md +126 -0
  50. package/src/ruvocal/Dockerfile +93 -0
  51. package/src/ruvocal/LICENSE +203 -0
  52. package/src/ruvocal/PRIVACY.md +41 -0
  53. package/src/ruvocal/README.md +190 -0
  54. package/src/ruvocal/chart/Chart.yaml +5 -0
  55. package/src/ruvocal/chart/env/dev.yaml +260 -0
  56. package/src/ruvocal/chart/env/prod.yaml +273 -0
  57. package/src/ruvocal/chart/templates/_helpers.tpl +22 -0
  58. package/src/ruvocal/chart/templates/config.yaml +10 -0
  59. package/src/ruvocal/chart/templates/deployment.yaml +81 -0
  60. package/src/ruvocal/chart/templates/hpa.yaml +45 -0
  61. package/src/ruvocal/chart/templates/infisical.yaml +24 -0
  62. package/src/ruvocal/chart/templates/ingress-internal.yaml +32 -0
  63. package/src/ruvocal/chart/templates/ingress.yaml +32 -0
  64. package/src/ruvocal/chart/templates/network-policy.yaml +36 -0
  65. package/src/ruvocal/chart/templates/service-account.yaml +13 -0
  66. package/src/ruvocal/chart/templates/service-monitor.yaml +17 -0
  67. package/src/ruvocal/chart/templates/service.yaml +21 -0
  68. package/src/ruvocal/chart/values.yaml +73 -0
  69. package/src/ruvocal/docker-compose.yml +21 -0
  70. package/src/ruvocal/docs/adr/ADR-029-HUGGINGFACE-CHAT-UI-CLOUD-RUN.md +1236 -0
  71. package/src/ruvocal/docs/adr/ADR-033-RUVECTOR-RUFLO-MCP-INTEGRATION.md +111 -0
  72. package/src/ruvocal/docs/adr/ADR-034-OPTIONAL-MCP-BACKENDS.md +117 -0
  73. package/src/ruvocal/docs/adr/ADR-035-MCP-TOOL-GROUPS.md +186 -0
  74. package/src/ruvocal/docs/adr/ADR-037-AUTOPILOT-CHAT-MODE.md +1500 -0
  75. package/src/ruvocal/docs/adr/ADR-038-RUVOCAL-FORK.md +286 -0
  76. package/src/ruvocal/docs/source/_toctree.yml +30 -0
  77. package/src/ruvocal/docs/source/configuration/common-issues.md +38 -0
  78. package/src/ruvocal/docs/source/configuration/llm-router.md +105 -0
  79. package/src/ruvocal/docs/source/configuration/mcp-tools.md +84 -0
  80. package/src/ruvocal/docs/source/configuration/metrics.md +9 -0
  81. package/src/ruvocal/docs/source/configuration/open-id.md +57 -0
  82. package/src/ruvocal/docs/source/configuration/overview.md +89 -0
  83. package/src/ruvocal/docs/source/configuration/theming.md +20 -0
  84. package/src/ruvocal/docs/source/developing/architecture.md +48 -0
  85. package/src/ruvocal/docs/source/index.md +53 -0
  86. package/src/ruvocal/docs/source/installation/docker.md +43 -0
  87. package/src/ruvocal/docs/source/installation/helm.md +43 -0
  88. package/src/ruvocal/docs/source/installation/local.md +62 -0
  89. package/src/ruvocal/entrypoint.sh +19 -0
  90. package/src/ruvocal/mcp-bridge/.claude-flow/agents/store.json +27 -0
  91. package/src/ruvocal/mcp-bridge/.claude-flow/daemon-state.json +130 -0
  92. package/src/ruvocal/mcp-bridge/.claude-flow/daemon.log +0 -0
  93. package/src/ruvocal/mcp-bridge/.claude-flow/daemon.pid +1 -0
  94. package/src/ruvocal/mcp-bridge/.claude-flow/tasks/store.json +21 -0
  95. package/src/ruvocal/mcp-bridge/.swarm/hnsw.index +0 -0
  96. package/src/ruvocal/mcp-bridge/.swarm/hnsw.metadata.json +1 -0
  97. package/src/ruvocal/mcp-bridge/.swarm/memory.db +0 -0
  98. package/src/ruvocal/mcp-bridge/.swarm/model-router-state.json +14 -0
  99. package/src/ruvocal/mcp-bridge/.swarm/schema.sql +305 -0
  100. package/src/ruvocal/mcp-bridge/Dockerfile +45 -0
  101. package/src/ruvocal/mcp-bridge/cloudbuild.yaml +49 -0
  102. package/src/ruvocal/mcp-bridge/index.js +1864 -0
  103. package/src/ruvocal/mcp-bridge/mcp-stdio-kernel.js +159 -0
  104. package/src/ruvocal/mcp-bridge/package-lock.json +762 -0
  105. package/src/ruvocal/mcp-bridge/package.json +17 -0
  106. package/src/ruvocal/mcp-bridge/test-harness.js +470 -0
  107. package/src/ruvocal/models/add-your-models-here.txt +1 -0
  108. package/src/ruvocal/package-lock.json +11741 -0
  109. package/src/ruvocal/package.json +121 -0
  110. package/src/ruvocal/postcss.config.js +6 -0
  111. package/src/ruvocal/rvf.manifest.json +204 -0
  112. package/src/ruvocal/scripts/config.ts +64 -0
  113. package/src/ruvocal/scripts/generate-welcome.mjs +181 -0
  114. package/src/ruvocal/scripts/populate.ts +288 -0
  115. package/src/ruvocal/scripts/samples.txt +194 -0
  116. package/src/ruvocal/scripts/setups/vitest-setup-client.ts +0 -0
  117. package/src/ruvocal/scripts/setups/vitest-setup-server.ts +44 -0
  118. package/src/ruvocal/scripts/updateLocalEnv.ts +48 -0
  119. package/src/ruvocal/src/ambient.d.ts +7 -0
  120. package/src/ruvocal/src/app.d.ts +29 -0
  121. package/src/ruvocal/src/app.html +53 -0
  122. package/src/ruvocal/src/hooks.server.ts +32 -0
  123. package/src/ruvocal/src/hooks.ts +6 -0
  124. package/src/ruvocal/src/lib/APIClient.ts +148 -0
  125. package/src/ruvocal/src/lib/actions/clickOutside.ts +18 -0
  126. package/src/ruvocal/src/lib/actions/snapScrollToBottom.ts +346 -0
  127. package/src/ruvocal/src/lib/buildPrompt.ts +33 -0
  128. package/src/ruvocal/src/lib/components/AnnouncementBanner.svelte +20 -0
  129. package/src/ruvocal/src/lib/components/BackgroundGenerationPoller.svelte +168 -0
  130. package/src/ruvocal/src/lib/components/CodeBlock.svelte +73 -0
  131. package/src/ruvocal/src/lib/components/CopyToClipBoardBtn.svelte +92 -0
  132. package/src/ruvocal/src/lib/components/DeleteConversationModal.svelte +75 -0
  133. package/src/ruvocal/src/lib/components/EditConversationModal.svelte +100 -0
  134. package/src/ruvocal/src/lib/components/ExpandNavigation.svelte +22 -0
  135. package/src/ruvocal/src/lib/components/HoverTooltip.svelte +44 -0
  136. package/src/ruvocal/src/lib/components/HtmlPreviewModal.svelte +143 -0
  137. package/src/ruvocal/src/lib/components/InfiniteScroll.svelte +50 -0
  138. package/src/ruvocal/src/lib/components/MobileNav.svelte +300 -0
  139. package/src/ruvocal/src/lib/components/Modal.svelte +115 -0
  140. package/src/ruvocal/src/lib/components/ModelCardMetadata.svelte +71 -0
  141. package/src/ruvocal/src/lib/components/NavConversationItem.svelte +151 -0
  142. package/src/ruvocal/src/lib/components/NavMenu.svelte +295 -0
  143. package/src/ruvocal/src/lib/components/Pagination.svelte +97 -0
  144. package/src/ruvocal/src/lib/components/PaginationArrow.svelte +27 -0
  145. package/src/ruvocal/src/lib/components/Portal.svelte +24 -0
  146. package/src/ruvocal/src/lib/components/RetryBtn.svelte +18 -0
  147. package/src/ruvocal/src/lib/components/RuFloUniverse.svelte +185 -0
  148. package/src/ruvocal/src/lib/components/ScrollToBottomBtn.svelte +47 -0
  149. package/src/ruvocal/src/lib/components/ScrollToPreviousBtn.svelte +77 -0
  150. package/src/ruvocal/src/lib/components/ShareConversationModal.svelte +182 -0
  151. package/src/ruvocal/src/lib/components/StopGeneratingBtn.svelte +69 -0
  152. package/src/ruvocal/src/lib/components/SubscribeModal.svelte +87 -0
  153. package/src/ruvocal/src/lib/components/Switch.svelte +36 -0
  154. package/src/ruvocal/src/lib/components/SystemPromptModal.svelte +44 -0
  155. package/src/ruvocal/src/lib/components/Toast.svelte +27 -0
  156. package/src/ruvocal/src/lib/components/Tooltip.svelte +30 -0
  157. package/src/ruvocal/src/lib/components/WelcomeModal.svelte +46 -0
  158. package/src/ruvocal/src/lib/components/chat/Alternatives.svelte +77 -0
  159. package/src/ruvocal/src/lib/components/chat/BlockWrapper.svelte +72 -0
  160. package/src/ruvocal/src/lib/components/chat/ChatInput.svelte +490 -0
  161. package/src/ruvocal/src/lib/components/chat/ChatIntroduction.svelte +123 -0
  162. package/src/ruvocal/src/lib/components/chat/ChatMessage.svelte +548 -0
  163. package/src/ruvocal/src/lib/components/chat/ChatWindow.svelte +939 -0
  164. package/src/ruvocal/src/lib/components/chat/FileDropzone.svelte +92 -0
  165. package/src/ruvocal/src/lib/components/chat/ImageLightbox.svelte +66 -0
  166. package/src/ruvocal/src/lib/components/chat/MarkdownBlock.svelte +23 -0
  167. package/src/ruvocal/src/lib/components/chat/MarkdownRenderer.svelte +69 -0
  168. package/src/ruvocal/src/lib/components/chat/MarkdownRenderer.svelte.test.ts +58 -0
  169. package/src/ruvocal/src/lib/components/chat/MessageAvatar.svelte +103 -0
  170. package/src/ruvocal/src/lib/components/chat/ModelSwitch.svelte +64 -0
  171. package/src/ruvocal/src/lib/components/chat/OpenReasoningResults.svelte +81 -0
  172. package/src/ruvocal/src/lib/components/chat/TaskGroup.svelte +88 -0
  173. package/src/ruvocal/src/lib/components/chat/ToolUpdate.svelte +273 -0
  174. package/src/ruvocal/src/lib/components/chat/UploadedFile.svelte +253 -0
  175. package/src/ruvocal/src/lib/components/chat/UrlFetchModal.svelte +203 -0
  176. package/src/ruvocal/src/lib/components/chat/VoiceRecorder.svelte +214 -0
  177. package/src/ruvocal/src/lib/components/icons/IconBurger.svelte +20 -0
  178. package/src/ruvocal/src/lib/components/icons/IconCheap.svelte +20 -0
  179. package/src/ruvocal/src/lib/components/icons/IconChevron.svelte +24 -0
  180. package/src/ruvocal/src/lib/components/icons/IconDazzled.svelte +40 -0
  181. package/src/ruvocal/src/lib/components/icons/IconFast.svelte +20 -0
  182. package/src/ruvocal/src/lib/components/icons/IconLoading.svelte +22 -0
  183. package/src/ruvocal/src/lib/components/icons/IconMCP.svelte +28 -0
  184. package/src/ruvocal/src/lib/components/icons/IconMoon.svelte +21 -0
  185. package/src/ruvocal/src/lib/components/icons/IconNew.svelte +20 -0
  186. package/src/ruvocal/src/lib/components/icons/IconOmni.svelte +90 -0
  187. package/src/ruvocal/src/lib/components/icons/IconPaperclip.svelte +24 -0
  188. package/src/ruvocal/src/lib/components/icons/IconPro.svelte +37 -0
  189. package/src/ruvocal/src/lib/components/icons/IconShare.svelte +21 -0
  190. package/src/ruvocal/src/lib/components/icons/IconSun.svelte +93 -0
  191. package/src/ruvocal/src/lib/components/icons/Logo.svelte +68 -0
  192. package/src/ruvocal/src/lib/components/icons/LogoHuggingFaceBorderless.svelte +54 -0
  193. package/src/ruvocal/src/lib/components/mcp/AddServerForm.svelte +250 -0
  194. package/src/ruvocal/src/lib/components/mcp/MCPServerManager.svelte +185 -0
  195. package/src/ruvocal/src/lib/components/mcp/ServerCard.svelte +203 -0
  196. package/src/ruvocal/src/lib/components/players/AudioPlayer.svelte +82 -0
  197. package/src/ruvocal/src/lib/components/voice/AudioWaveform.svelte +96 -0
  198. package/src/ruvocal/src/lib/constants/mcpExamples.ts +135 -0
  199. package/src/ruvocal/src/lib/constants/mime.ts +11 -0
  200. package/src/ruvocal/src/lib/constants/pagination.ts +1 -0
  201. package/src/ruvocal/src/lib/constants/publicSepToken.ts +1 -0
  202. package/src/ruvocal/src/lib/constants/routerExamples.ts +209 -0
  203. package/src/ruvocal/src/lib/createShareLink.ts +27 -0
  204. package/src/ruvocal/src/lib/jobs/refresh-conversation-stats.ts +297 -0
  205. package/src/ruvocal/src/lib/migrations/lock.ts +56 -0
  206. package/src/ruvocal/src/lib/migrations/migrations.spec.ts +74 -0
  207. package/src/ruvocal/src/lib/migrations/migrations.ts +109 -0
  208. package/src/ruvocal/src/lib/migrations/routines/01-update-search-assistants.ts +50 -0
  209. package/src/ruvocal/src/lib/migrations/routines/02-update-assistants-models.ts +48 -0
  210. package/src/ruvocal/src/lib/migrations/routines/04-update-message-updates.ts +151 -0
  211. package/src/ruvocal/src/lib/migrations/routines/05-update-message-files.ts +56 -0
  212. package/src/ruvocal/src/lib/migrations/routines/06-trim-message-updates.ts +56 -0
  213. package/src/ruvocal/src/lib/migrations/routines/08-update-featured-to-review.ts +32 -0
  214. package/src/ruvocal/src/lib/migrations/routines/09-delete-empty-conversations.spec.ts +214 -0
  215. package/src/ruvocal/src/lib/migrations/routines/09-delete-empty-conversations.ts +88 -0
  216. package/src/ruvocal/src/lib/migrations/routines/10-update-reports-assistantid.ts +29 -0
  217. package/src/ruvocal/src/lib/migrations/routines/index.ts +15 -0
  218. package/src/ruvocal/src/lib/server/__tests__/conversation-stop-generating.spec.ts +103 -0
  219. package/src/ruvocal/src/lib/server/abortRegistry.ts +57 -0
  220. package/src/ruvocal/src/lib/server/abortedGenerations.ts +43 -0
  221. package/src/ruvocal/src/lib/server/adminToken.ts +62 -0
  222. package/src/ruvocal/src/lib/server/api/__tests__/conversations-id.spec.ts +296 -0
  223. package/src/ruvocal/src/lib/server/api/__tests__/conversations-message.spec.ts +216 -0
  224. package/src/ruvocal/src/lib/server/api/__tests__/conversations.spec.ts +235 -0
  225. package/src/ruvocal/src/lib/server/api/__tests__/misc.spec.ts +72 -0
  226. package/src/ruvocal/src/lib/server/api/__tests__/testHelpers.ts +86 -0
  227. package/src/ruvocal/src/lib/server/api/__tests__/user-reports.spec.ts +78 -0
  228. package/src/ruvocal/src/lib/server/api/__tests__/user.spec.ts +239 -0
  229. package/src/ruvocal/src/lib/server/api/types.ts +37 -0
  230. package/src/ruvocal/src/lib/server/api/utils/requireAuth.ts +22 -0
  231. package/src/ruvocal/src/lib/server/api/utils/resolveConversation.ts +69 -0
  232. package/src/ruvocal/src/lib/server/api/utils/resolveModel.ts +27 -0
  233. package/src/ruvocal/src/lib/server/api/utils/superjsonResponse.ts +15 -0
  234. package/src/ruvocal/src/lib/server/apiToken.ts +11 -0
  235. package/src/ruvocal/src/lib/server/auth.ts +554 -0
  236. package/src/ruvocal/src/lib/server/config.ts +187 -0
  237. package/src/ruvocal/src/lib/server/conversation.ts +83 -0
  238. package/src/ruvocal/src/lib/server/database/__tests__/rvf.spec.ts +709 -0
  239. package/src/ruvocal/src/lib/server/database/postgres.ts +700 -0
  240. package/src/ruvocal/src/lib/server/database/rvf.ts +1078 -0
  241. package/src/ruvocal/src/lib/server/database.ts +145 -0
  242. package/src/ruvocal/src/lib/server/endpoints/document.ts +68 -0
  243. package/src/ruvocal/src/lib/server/endpoints/endpoints.ts +43 -0
  244. package/src/ruvocal/src/lib/server/endpoints/images.ts +211 -0
  245. package/src/ruvocal/src/lib/server/endpoints/openai/endpointOai.ts +266 -0
  246. package/src/ruvocal/src/lib/server/endpoints/openai/openAIChatToTextGenerationStream.ts +212 -0
  247. package/src/ruvocal/src/lib/server/endpoints/openai/openAICompletionToTextGenerationStream.ts +32 -0
  248. package/src/ruvocal/src/lib/server/endpoints/preprocessMessages.ts +61 -0
  249. package/src/ruvocal/src/lib/server/exitHandler.ts +59 -0
  250. package/src/ruvocal/src/lib/server/files/downloadFile.ts +34 -0
  251. package/src/ruvocal/src/lib/server/files/uploadFile.ts +29 -0
  252. package/src/ruvocal/src/lib/server/findRepoRoot.ts +13 -0
  253. package/src/ruvocal/src/lib/server/fonts/Inter-Black.ttf +0 -0
  254. package/src/ruvocal/src/lib/server/fonts/Inter-Bold.ttf +0 -0
  255. package/src/ruvocal/src/lib/server/fonts/Inter-ExtraBold.ttf +0 -0
  256. package/src/ruvocal/src/lib/server/fonts/Inter-ExtraLight.ttf +0 -0
  257. package/src/ruvocal/src/lib/server/fonts/Inter-Light.ttf +0 -0
  258. package/src/ruvocal/src/lib/server/fonts/Inter-Medium.ttf +0 -0
  259. package/src/ruvocal/src/lib/server/fonts/Inter-Regular.ttf +0 -0
  260. package/src/ruvocal/src/lib/server/fonts/Inter-SemiBold.ttf +0 -0
  261. package/src/ruvocal/src/lib/server/fonts/Inter-Thin.ttf +0 -0
  262. package/src/ruvocal/src/lib/server/generateFromDefaultEndpoint.ts +46 -0
  263. package/src/ruvocal/src/lib/server/hooks/error.ts +37 -0
  264. package/src/ruvocal/src/lib/server/hooks/fetch.ts +22 -0
  265. package/src/ruvocal/src/lib/server/hooks/handle.ts +250 -0
  266. package/src/ruvocal/src/lib/server/hooks/init.ts +51 -0
  267. package/src/ruvocal/src/lib/server/isURLLocal.spec.ts +31 -0
  268. package/src/ruvocal/src/lib/server/isURLLocal.ts +74 -0
  269. package/src/ruvocal/src/lib/server/logger.ts +42 -0
  270. package/src/ruvocal/src/lib/server/mcp/clientPool.ts +70 -0
  271. package/src/ruvocal/src/lib/server/mcp/hf.ts +32 -0
  272. package/src/ruvocal/src/lib/server/mcp/httpClient.ts +122 -0
  273. package/src/ruvocal/src/lib/server/mcp/registry.ts +76 -0
  274. package/src/ruvocal/src/lib/server/mcp/tools.ts +196 -0
  275. package/src/ruvocal/src/lib/server/metrics.ts +255 -0
  276. package/src/ruvocal/src/lib/server/models.ts +518 -0
  277. package/src/ruvocal/src/lib/server/requestContext.ts +55 -0
  278. package/src/ruvocal/src/lib/server/router/arch.ts +230 -0
  279. package/src/ruvocal/src/lib/server/router/endpoint.ts +316 -0
  280. package/src/ruvocal/src/lib/server/router/multimodal.ts +28 -0
  281. package/src/ruvocal/src/lib/server/router/policy.ts +49 -0
  282. package/src/ruvocal/src/lib/server/router/toolsRoute.ts +51 -0
  283. package/src/ruvocal/src/lib/server/router/types.ts +21 -0
  284. package/src/ruvocal/src/lib/server/sendSlack.ts +23 -0
  285. package/src/ruvocal/src/lib/server/textGeneration/generate.ts +258 -0
  286. package/src/ruvocal/src/lib/server/textGeneration/index.ts +95 -0
  287. package/src/ruvocal/src/lib/server/textGeneration/mcp/fileRefs.ts +155 -0
  288. package/src/ruvocal/src/lib/server/textGeneration/mcp/routerResolution.ts +108 -0
  289. package/src/ruvocal/src/lib/server/textGeneration/mcp/runMcpFlow.ts +822 -0
  290. package/src/ruvocal/src/lib/server/textGeneration/mcp/toolInvocation.ts +349 -0
  291. package/src/ruvocal/src/lib/server/textGeneration/reasoning.ts +23 -0
  292. package/src/ruvocal/src/lib/server/textGeneration/title.ts +83 -0
  293. package/src/ruvocal/src/lib/server/textGeneration/types.ts +26 -0
  294. package/src/ruvocal/src/lib/server/textGeneration/utils/prepareFiles.ts +88 -0
  295. package/src/ruvocal/src/lib/server/textGeneration/utils/routing.ts +21 -0
  296. package/src/ruvocal/src/lib/server/textGeneration/utils/toolPrompt.ts +49 -0
  297. package/src/ruvocal/src/lib/server/urlSafety.ts +72 -0
  298. package/src/ruvocal/src/lib/server/usageLimits.ts +30 -0
  299. package/src/ruvocal/src/lib/stores/autopilotStore.svelte.ts +175 -0
  300. package/src/ruvocal/src/lib/stores/backgroundGenerations.svelte.ts +32 -0
  301. package/src/ruvocal/src/lib/stores/backgroundGenerations.ts +1 -0
  302. package/src/ruvocal/src/lib/stores/errors.ts +9 -0
  303. package/src/ruvocal/src/lib/stores/isAborted.ts +3 -0
  304. package/src/ruvocal/src/lib/stores/isPro.ts +4 -0
  305. package/src/ruvocal/src/lib/stores/loading.ts +3 -0
  306. package/src/ruvocal/src/lib/stores/mcpServers.ts +345 -0
  307. package/src/ruvocal/src/lib/stores/pendingChatInput.ts +3 -0
  308. package/src/ruvocal/src/lib/stores/pendingMessage.ts +9 -0
  309. package/src/ruvocal/src/lib/stores/settings.ts +182 -0
  310. package/src/ruvocal/src/lib/stores/shareModal.ts +13 -0
  311. package/src/ruvocal/src/lib/stores/titleUpdate.ts +8 -0
  312. package/src/ruvocal/src/lib/switchTheme.ts +124 -0
  313. package/src/ruvocal/src/lib/types/AbortedGeneration.ts +8 -0
  314. package/src/ruvocal/src/lib/types/Assistant.ts +31 -0
  315. package/src/ruvocal/src/lib/types/AssistantStats.ts +11 -0
  316. package/src/ruvocal/src/lib/types/ConfigKey.ts +4 -0
  317. package/src/ruvocal/src/lib/types/ConvSidebar.ts +9 -0
  318. package/src/ruvocal/src/lib/types/Conversation.ts +27 -0
  319. package/src/ruvocal/src/lib/types/ConversationStats.ts +13 -0
  320. package/src/ruvocal/src/lib/types/Message.ts +41 -0
  321. package/src/ruvocal/src/lib/types/MessageEvent.ts +10 -0
  322. package/src/ruvocal/src/lib/types/MessageUpdate.ts +139 -0
  323. package/src/ruvocal/src/lib/types/MigrationResult.ts +7 -0
  324. package/src/ruvocal/src/lib/types/Model.ts +23 -0
  325. package/src/ruvocal/src/lib/types/Report.ts +12 -0
  326. package/src/ruvocal/src/lib/types/Review.ts +6 -0
  327. package/src/ruvocal/src/lib/types/Semaphore.ts +19 -0
  328. package/src/ruvocal/src/lib/types/Session.ts +22 -0
  329. package/src/ruvocal/src/lib/types/Settings.ts +86 -0
  330. package/src/ruvocal/src/lib/types/SharedConversation.ts +9 -0
  331. package/src/ruvocal/src/lib/types/Template.ts +6 -0
  332. package/src/ruvocal/src/lib/types/Timestamps.ts +4 -0
  333. package/src/ruvocal/src/lib/types/TokenCache.ts +6 -0
  334. package/src/ruvocal/src/lib/types/Tool.ts +74 -0
  335. package/src/ruvocal/src/lib/types/UrlDependency.ts +5 -0
  336. package/src/ruvocal/src/lib/types/User.ts +14 -0
  337. package/src/ruvocal/src/lib/utils/PublicConfig.svelte.ts +75 -0
  338. package/src/ruvocal/src/lib/utils/auth.ts +17 -0
  339. package/src/ruvocal/src/lib/utils/chunk.ts +33 -0
  340. package/src/ruvocal/src/lib/utils/cookiesAreEnabled.ts +13 -0
  341. package/src/ruvocal/src/lib/utils/debounce.ts +17 -0
  342. package/src/ruvocal/src/lib/utils/deepestChild.ts +6 -0
  343. package/src/ruvocal/src/lib/utils/favicon.ts +21 -0
  344. package/src/ruvocal/src/lib/utils/fetchJSON.ts +23 -0
  345. package/src/ruvocal/src/lib/utils/file2base64.ts +14 -0
  346. package/src/ruvocal/src/lib/utils/formatUserCount.ts +37 -0
  347. package/src/ruvocal/src/lib/utils/generationState.spec.ts +75 -0
  348. package/src/ruvocal/src/lib/utils/generationState.ts +26 -0
  349. package/src/ruvocal/src/lib/utils/getHref.ts +41 -0
  350. package/src/ruvocal/src/lib/utils/getReturnFromGenerator.ts +7 -0
  351. package/src/ruvocal/src/lib/utils/haptics.ts +64 -0
  352. package/src/ruvocal/src/lib/utils/hashConv.ts +12 -0
  353. package/src/ruvocal/src/lib/utils/hf.ts +17 -0
  354. package/src/ruvocal/src/lib/utils/isDesktop.ts +7 -0
  355. package/src/ruvocal/src/lib/utils/isUrl.ts +8 -0
  356. package/src/ruvocal/src/lib/utils/isVirtualKeyboard.ts +16 -0
  357. package/src/ruvocal/src/lib/utils/loadAttachmentsFromUrls.ts +115 -0
  358. package/src/ruvocal/src/lib/utils/marked.spec.ts +96 -0
  359. package/src/ruvocal/src/lib/utils/marked.ts +531 -0
  360. package/src/ruvocal/src/lib/utils/mcpValidation.ts +147 -0
  361. package/src/ruvocal/src/lib/utils/mergeAsyncGenerators.ts +38 -0
  362. package/src/ruvocal/src/lib/utils/messageUpdates.spec.ts +262 -0
  363. package/src/ruvocal/src/lib/utils/messageUpdates.ts +324 -0
  364. package/src/ruvocal/src/lib/utils/mime.ts +56 -0
  365. package/src/ruvocal/src/lib/utils/models.ts +14 -0
  366. package/src/ruvocal/src/lib/utils/parseBlocks.ts +120 -0
  367. package/src/ruvocal/src/lib/utils/parseIncompleteMarkdown.ts +644 -0
  368. package/src/ruvocal/src/lib/utils/parseStringToList.ts +10 -0
  369. package/src/ruvocal/src/lib/utils/randomUuid.ts +14 -0
  370. package/src/ruvocal/src/lib/utils/searchTokens.ts +33 -0
  371. package/src/ruvocal/src/lib/utils/sha256.ts +7 -0
  372. package/src/ruvocal/src/lib/utils/stringifyError.ts +12 -0
  373. package/src/ruvocal/src/lib/utils/sum.ts +3 -0
  374. package/src/ruvocal/src/lib/utils/template.spec.ts +59 -0
  375. package/src/ruvocal/src/lib/utils/template.ts +53 -0
  376. package/src/ruvocal/src/lib/utils/timeout.ts +9 -0
  377. package/src/ruvocal/src/lib/utils/toolProgress.spec.ts +46 -0
  378. package/src/ruvocal/src/lib/utils/toolProgress.ts +11 -0
  379. package/src/ruvocal/src/lib/utils/tree/addChildren.spec.ts +102 -0
  380. package/src/ruvocal/src/lib/utils/tree/addChildren.ts +48 -0
  381. package/src/ruvocal/src/lib/utils/tree/addSibling.spec.ts +81 -0
  382. package/src/ruvocal/src/lib/utils/tree/addSibling.ts +41 -0
  383. package/src/ruvocal/src/lib/utils/tree/buildSubtree.spec.ts +110 -0
  384. package/src/ruvocal/src/lib/utils/tree/buildSubtree.ts +24 -0
  385. package/src/ruvocal/src/lib/utils/tree/convertLegacyConversation.spec.ts +31 -0
  386. package/src/ruvocal/src/lib/utils/tree/convertLegacyConversation.ts +36 -0
  387. package/src/ruvocal/src/lib/utils/tree/isMessageId.spec.ts +15 -0
  388. package/src/ruvocal/src/lib/utils/tree/isMessageId.ts +5 -0
  389. package/src/ruvocal/src/lib/utils/tree/tree.d.ts +14 -0
  390. package/src/ruvocal/src/lib/utils/tree/treeHelpers.spec.ts +167 -0
  391. package/src/ruvocal/src/lib/utils/updates.ts +39 -0
  392. package/src/ruvocal/src/lib/utils/urlParams.ts +13 -0
  393. package/src/ruvocal/src/lib/workers/autopilotWorker.ts +221 -0
  394. package/src/ruvocal/src/lib/workers/detailFetchWorker.ts +100 -0
  395. package/src/ruvocal/src/lib/workers/markdownWorker.ts +61 -0
  396. package/src/ruvocal/src/routes/+error.svelte +20 -0
  397. package/src/ruvocal/src/routes/+layout.svelte +324 -0
  398. package/src/ruvocal/src/routes/+layout.ts +91 -0
  399. package/src/ruvocal/src/routes/+page.svelte +168 -0
  400. package/src/ruvocal/src/routes/.well-known/oauth-cimd/+server.ts +37 -0
  401. package/src/ruvocal/src/routes/__debug/openai/+server.ts +21 -0
  402. package/src/ruvocal/src/routes/admin/export/+server.ts +159 -0
  403. package/src/ruvocal/src/routes/admin/stats/compute/+server.ts +16 -0
  404. package/src/ruvocal/src/routes/api/conversation/[id]/+server.ts +40 -0
  405. package/src/ruvocal/src/routes/api/conversation/[id]/message/[messageId]/+server.ts +42 -0
  406. package/src/ruvocal/src/routes/api/conversations/+server.ts +48 -0
  407. package/src/ruvocal/src/routes/api/fetch-url/+server.ts +147 -0
  408. package/src/ruvocal/src/routes/api/mcp/health/+server.ts +292 -0
  409. package/src/ruvocal/src/routes/api/mcp/servers/+server.ts +32 -0
  410. package/src/ruvocal/src/routes/api/models/+server.ts +25 -0
  411. package/src/ruvocal/src/routes/api/transcribe/+server.ts +104 -0
  412. package/src/ruvocal/src/routes/api/user/+server.ts +15 -0
  413. package/src/ruvocal/src/routes/api/user/validate-token/+server.ts +20 -0
  414. package/src/ruvocal/src/routes/api/v2/conversations/+server.ts +48 -0
  415. package/src/ruvocal/src/routes/api/v2/conversations/[id]/+server.ts +94 -0
  416. package/src/ruvocal/src/routes/api/v2/conversations/[id]/message/[messageId]/+server.ts +43 -0
  417. package/src/ruvocal/src/routes/api/v2/conversations/import-share/+server.ts +23 -0
  418. package/src/ruvocal/src/routes/api/v2/debug/config/+server.ts +16 -0
  419. package/src/ruvocal/src/routes/api/v2/debug/refresh/+server.ts +30 -0
  420. package/src/ruvocal/src/routes/api/v2/export/+server.ts +196 -0
  421. package/src/ruvocal/src/routes/api/v2/feature-flags/+server.ts +14 -0
  422. package/src/ruvocal/src/routes/api/v2/models/+server.ts +38 -0
  423. package/src/ruvocal/src/routes/api/v2/models/[namespace]/+server.ts +8 -0
  424. package/src/ruvocal/src/routes/api/v2/models/[namespace]/[model]/+server.ts +8 -0
  425. package/src/ruvocal/src/routes/api/v2/models/[namespace]/[model]/subscribe/+server.ts +28 -0
  426. package/src/ruvocal/src/routes/api/v2/models/[namespace]/subscribe/+server.ts +28 -0
  427. package/src/ruvocal/src/routes/api/v2/models/old/+server.ts +7 -0
  428. package/src/ruvocal/src/routes/api/v2/models/refresh/+server.ts +33 -0
  429. package/src/ruvocal/src/routes/api/v2/public-config/+server.ts +7 -0
  430. package/src/ruvocal/src/routes/api/v2/user/+server.ts +17 -0
  431. package/src/ruvocal/src/routes/api/v2/user/billing-orgs/+server.ts +73 -0
  432. package/src/ruvocal/src/routes/api/v2/user/reports/+server.ts +17 -0
  433. package/src/ruvocal/src/routes/api/v2/user/settings/+server.ts +103 -0
  434. package/src/ruvocal/src/routes/conversation/+server.ts +115 -0
  435. package/src/ruvocal/src/routes/conversation/[id]/+page.svelte +582 -0
  436. package/src/ruvocal/src/routes/conversation/[id]/+page.ts +60 -0
  437. package/src/ruvocal/src/routes/conversation/[id]/+server.ts +736 -0
  438. package/src/ruvocal/src/routes/conversation/[id]/message/[messageId]/prompt/+server.ts +66 -0
  439. package/src/ruvocal/src/routes/conversation/[id]/output/[sha256]/+server.ts +58 -0
  440. package/src/ruvocal/src/routes/conversation/[id]/share/+server.ts +69 -0
  441. package/src/ruvocal/src/routes/conversation/[id]/stop-generating/+server.ts +35 -0
  442. package/src/ruvocal/src/routes/healthcheck/+server.ts +3 -0
  443. package/src/ruvocal/src/routes/login/+server.ts +5 -0
  444. package/src/ruvocal/src/routes/login/callback/+server.ts +103 -0
  445. package/src/ruvocal/src/routes/login/callback/updateUser.spec.ts +157 -0
  446. package/src/ruvocal/src/routes/login/callback/updateUser.ts +215 -0
  447. package/src/ruvocal/src/routes/logout/+server.ts +18 -0
  448. package/src/ruvocal/src/routes/metrics/+server.ts +18 -0
  449. package/src/ruvocal/src/routes/models/+page.svelte +233 -0
  450. package/src/ruvocal/src/routes/models/[...model]/+page.svelte +161 -0
  451. package/src/ruvocal/src/routes/models/[...model]/+page.ts +14 -0
  452. package/src/ruvocal/src/routes/models/[...model]/thumbnail.png/+server.ts +64 -0
  453. package/src/ruvocal/src/routes/models/[...model]/thumbnail.png/ModelThumbnail.svelte +28 -0
  454. package/src/ruvocal/src/routes/privacy/+page.svelte +11 -0
  455. package/src/ruvocal/src/routes/r/[id]/+page.ts +34 -0
  456. package/src/ruvocal/src/routes/settings/(nav)/+layout.svelte +282 -0
  457. package/src/ruvocal/src/routes/settings/(nav)/+layout.ts +1 -0
  458. package/src/ruvocal/src/routes/settings/(nav)/+page.svelte +0 -0
  459. package/src/ruvocal/src/routes/settings/(nav)/+server.ts +53 -0
  460. package/src/ruvocal/src/routes/settings/(nav)/[...model]/+page.svelte +464 -0
  461. package/src/ruvocal/src/routes/settings/(nav)/[...model]/+page.ts +14 -0
  462. package/src/ruvocal/src/routes/settings/(nav)/application/+page.svelte +362 -0
  463. package/src/ruvocal/src/routes/settings/+layout.svelte +40 -0
  464. package/src/ruvocal/src/styles/highlight-js.css +195 -0
  465. package/src/ruvocal/src/styles/main.css +144 -0
  466. package/src/ruvocal/static/chatui/apple-touch-icon.png +0 -0
  467. package/src/ruvocal/static/chatui/favicon-dark.svg +3 -0
  468. package/src/ruvocal/static/chatui/favicon-dev.svg +3 -0
  469. package/src/ruvocal/static/chatui/favicon.ico +0 -0
  470. package/src/ruvocal/static/chatui/favicon.svg +3 -0
  471. package/src/ruvocal/static/chatui/icon-128x128.png +0 -0
  472. package/src/ruvocal/static/chatui/icon-144x144.png +0 -0
  473. package/src/ruvocal/static/chatui/icon-192x192.png +0 -0
  474. package/src/ruvocal/static/chatui/icon-256x256.png +0 -0
  475. package/src/ruvocal/static/chatui/icon-36x36.png +0 -0
  476. package/src/ruvocal/static/chatui/icon-48x48.png +0 -0
  477. package/src/ruvocal/static/chatui/icon-512x512.png +0 -0
  478. package/src/ruvocal/static/chatui/icon-72x72.png +0 -0
  479. package/src/ruvocal/static/chatui/icon-96x96.png +0 -0
  480. package/src/ruvocal/static/chatui/icon.svg +3 -0
  481. package/src/ruvocal/static/chatui/logo.svg +7 -0
  482. package/src/ruvocal/static/chatui/manifest.json +54 -0
  483. package/src/ruvocal/static/chatui/omni-welcome.gif +0 -0
  484. package/src/ruvocal/static/chatui/omni-welcome.png +0 -0
  485. package/src/ruvocal/static/chatui/welcome.js +184 -0
  486. package/src/ruvocal/static/chatui/welcome.svg +1 -0
  487. package/src/ruvocal/static/huggingchat/apple-touch-icon.png +0 -0
  488. package/src/ruvocal/static/huggingchat/assistants-thumbnail.png +0 -0
  489. package/src/ruvocal/static/huggingchat/castle-example.jpg +0 -0
  490. package/src/ruvocal/static/huggingchat/favicon-dark.svg +4 -0
  491. package/src/ruvocal/static/huggingchat/favicon-dev.svg +4 -0
  492. package/src/ruvocal/static/huggingchat/favicon.ico +0 -0
  493. package/src/ruvocal/static/huggingchat/favicon.svg +4 -0
  494. package/src/ruvocal/static/huggingchat/fulltext-logo.svg +2 -0
  495. package/src/ruvocal/static/huggingchat/icon-128x128.png +0 -0
  496. package/src/ruvocal/static/huggingchat/icon-144x144.png +0 -0
  497. package/src/ruvocal/static/huggingchat/icon-192x192.png +0 -0
  498. package/src/ruvocal/static/huggingchat/icon-256x256.png +0 -0
  499. package/src/ruvocal/static/huggingchat/icon-36x36.png +0 -0
  500. package/src/ruvocal/static/huggingchat/icon-48x48.png +0 -0
  501. package/src/ruvocal/static/huggingchat/icon-512x512.png +0 -0
  502. package/src/ruvocal/static/huggingchat/icon-72x72.png +0 -0
  503. package/src/ruvocal/static/huggingchat/icon-96x96.png +0 -0
  504. package/src/ruvocal/static/huggingchat/icon.svg +4 -0
  505. package/src/ruvocal/static/huggingchat/logo.svg +4 -0
  506. package/src/ruvocal/static/huggingchat/manifest.json +54 -0
  507. package/src/ruvocal/static/huggingchat/omni-welcome.gif +0 -0
  508. package/src/ruvocal/static/huggingchat/routes.chat.json +226 -0
  509. package/src/ruvocal/static/huggingchat/thumbnail.png +0 -0
  510. package/src/ruvocal/static/huggingchat/tools-thumbnail.png +0 -0
  511. package/src/ruvocal/static/robots.txt +10 -0
  512. package/src/ruvocal/stub/@reflink/reflink/index.js +0 -0
  513. package/src/ruvocal/stub/@reflink/reflink/package.json +5 -0
  514. package/src/ruvocal/svelte.config.js +53 -0
  515. package/src/ruvocal/tailwind.config.cjs +30 -0
  516. package/src/ruvocal/tsconfig.json +19 -0
  517. package/src/ruvocal/vite.config.ts +87 -0
  518. package/src/scripts/deploy.sh +116 -0
  519. package/src/scripts/generate-config.js +245 -0
  520. package/src/scripts/generate-welcome.js +187 -0
  521. package/src/scripts/package-rvf.sh +116 -0
@@ -0,0 +1,1864 @@
1
+ import express from "express";
2
+ import { spawn } from "child_process";
3
+ import { randomUUID } from "crypto";
4
+
5
+ // =============================================================================
6
+ // CONFIGURATION
7
+ // =============================================================================
8
+
9
+ const CLOUD_FUNCTIONS = {
10
+ search: process.env.SEARCH_API_URL || null,
11
+ research: process.env.RESEARCH_API_URL || null,
12
+ };
13
+
14
+ const PORT = parseInt(process.env.PORT || "3001", 10);
15
+
16
+ // =============================================================================
17
+ // TOOL GROUPS — Enable/disable categories of tools independently
18
+ // =============================================================================
19
+ // Groups map tool name prefixes from backends to logical categories.
20
+ // Each group can be toggled via env var. The AI sees only enabled tools.
21
+
22
+ const TOOL_GROUPS = {
23
+ // --- Core (always on, built-in) ---
24
+ core: {
25
+ enabled: true, // cannot be disabled
26
+ description: "Search, research, and guidance tools",
27
+ source: "builtin",
28
+ },
29
+
30
+ // --- Intelligence (ruvector) ---
31
+ intelligence: {
32
+ enabled: process.env.MCP_GROUP_INTELLIGENCE !== "false",
33
+ description: "Self-learning intelligence — routing, memory, pattern training (ruvector)",
34
+ source: "ruvector",
35
+ prefixes: ["hooks_"],
36
+ },
37
+
38
+ // --- Agents & Orchestration (ruflo) ---
39
+ agents: {
40
+ enabled: process.env.MCP_GROUP_AGENTS !== "false",
41
+ description: "Agent lifecycle, swarm coordination, task management, workflows (ruflo)",
42
+ source: "ruflo",
43
+ prefixes: ["agent_", "swarm_", "task_", "session_", "hive-mind_", "workflow_", "coordination_"],
44
+ },
45
+
46
+ // --- Memory & Knowledge (ruflo) ---
47
+ memory: {
48
+ enabled: process.env.MCP_GROUP_MEMORY !== "false",
49
+ description: "Vector memory, AgentDB, embeddings, semantic search (ruflo)",
50
+ source: "ruflo",
51
+ prefixes: ["memory_", "agentdb_", "embeddings_"],
52
+ },
53
+
54
+ // --- Dev Tools (ruflo) ---
55
+ devtools: {
56
+ enabled: process.env.MCP_GROUP_DEVTOOLS !== "false",
57
+ description: "Hooks, code analysis, performance profiling, GitHub integration (ruflo)",
58
+ source: "ruflo",
59
+ prefixes: ["hooks_", "analyze_", "performance_", "github_", "terminal_", "config_", "system_", "progress_"],
60
+ },
61
+
62
+ // --- Security & Safety (ruflo) ---
63
+ security: {
64
+ enabled: process.env.MCP_GROUP_SECURITY === "true",
65
+ description: "AI defence, PII detection, claims management, pattern transfer (ruflo)",
66
+ source: "ruflo",
67
+ prefixes: ["aidefence_", "claims_", "transfer_"],
68
+ },
69
+
70
+ // --- Browser Automation (ruflo) ---
71
+ browser: {
72
+ enabled: process.env.MCP_GROUP_BROWSER === "true",
73
+ description: "Headless browser control — navigate, click, fill, screenshot (ruflo)",
74
+ source: "ruflo",
75
+ prefixes: ["browser_"],
76
+ },
77
+
78
+ // --- Neural & DAA (ruflo) ---
79
+ neural: {
80
+ enabled: process.env.MCP_GROUP_NEURAL === "true",
81
+ description: "Neural network training, DAA autonomous agents, cognitive patterns (ruflo)",
82
+ source: "ruflo",
83
+ prefixes: ["neural_", "daa_"],
84
+ },
85
+
86
+ // --- Agentic Flow (agentic-flow@alpha) ---
87
+ "agentic-flow": {
88
+ enabled: process.env.MCP_GROUP_AGENTIC_FLOW === "true",
89
+ description: "Execute 66+ specialized agents, batch code editing, AgentDB patterns (agentic-flow)",
90
+ source: "agentic-flow",
91
+ prefixes: ["agentic_flow_", "agent_booster_", "agentdb_"],
92
+ },
93
+
94
+ // --- Claude Code ---
95
+ "claude-code": {
96
+ enabled: process.env.MCP_GROUP_CLAUDE_CODE === "true",
97
+ description: "Anthropic Claude Code — file editing, bash execution, code analysis (requires ANTHROPIC_API_KEY)",
98
+ source: "claude",
99
+ },
100
+
101
+ // --- Gemini MCP ---
102
+ gemini: {
103
+ enabled: process.env.MCP_GROUP_GEMINI === "true",
104
+ description: "Google Gemini conversation context, multimodal capabilities (requires GOOGLE_API_KEY)",
105
+ source: "gemini-mcp",
106
+ },
107
+
108
+ // --- OpenAI Codex ---
109
+ codex: {
110
+ enabled: process.env.MCP_GROUP_CODEX === "true",
111
+ description: "OpenAI Codex coding agent — code generation and execution (requires OPENAI_API_KEY)",
112
+ source: "codex",
113
+ },
114
+ };
115
+
116
+ // =============================================================================
117
+ // STDIO MCP CLIENT — Connects to external MCP servers via child process
118
+ // =============================================================================
119
+
120
+ class StdioMcpClient {
121
+ constructor(name, command, args = []) {
122
+ this.name = name;
123
+ this.command = command;
124
+ this.args = args;
125
+ this.process = null;
126
+ this.tools = [];
127
+ this.ready = false;
128
+ this.pending = new Map();
129
+ this.buffer = "";
130
+ }
131
+
132
+ async start() {
133
+ return new Promise((resolve) => {
134
+ try {
135
+ this.process = spawn(this.command, this.args, {
136
+ stdio: ["pipe", "pipe", "pipe"],
137
+ env: { ...process.env },
138
+ });
139
+
140
+ this.process.stdout.on("data", (data) => this._onData(data.toString()));
141
+ this.process.stderr.on("data", (data) => {
142
+ const msg = data.toString().trim();
143
+ if (msg && !msg.startsWith("npm WARN")) console.error(`[${this.name}] ${msg}`);
144
+ });
145
+ this.process.on("error", (err) => {
146
+ console.error(`[${this.name}] spawn error:`, err.message);
147
+ this.ready = false;
148
+ resolve(false);
149
+ });
150
+ this.process.on("exit", (code) => {
151
+ console.log(`[${this.name}] exited with code ${code}`);
152
+ this.ready = false;
153
+ });
154
+
155
+ this._send("initialize", {
156
+ protocolVersion: "2024-11-05",
157
+ capabilities: {},
158
+ clientInfo: { name: "mcp-bridge", version: "2.0.0" },
159
+ }).then((result) => {
160
+ if (result && !result.error) {
161
+ this._notify("notifications/initialized", {});
162
+ return this._send("tools/list", {});
163
+ }
164
+ return null;
165
+ }).then((result) => {
166
+ if (result && result.tools) {
167
+ this.tools = result.tools.map(t => ({
168
+ ...t,
169
+ _originalName: t.name,
170
+ _backend: this.name,
171
+ }));
172
+ this.ready = true;
173
+ console.log(`[${this.name}] ${this.tools.length} tools loaded`);
174
+ }
175
+ resolve(this.ready);
176
+ }).catch((err) => {
177
+ console.error(`[${this.name}] init failed:`, err.message);
178
+ resolve(false);
179
+ });
180
+
181
+ setTimeout(() => { if (!this.ready) resolve(false); }, 60000);
182
+ } catch (err) {
183
+ console.error(`[${this.name}] failed to start:`, err.message);
184
+ resolve(false);
185
+ }
186
+ });
187
+ }
188
+
189
+ _onData(chunk) {
190
+ this.buffer += chunk;
191
+ const lines = this.buffer.split("\n");
192
+ this.buffer = lines.pop() || "";
193
+ for (const line of lines) {
194
+ const trimmed = line.trim();
195
+ if (!trimmed) continue;
196
+ try {
197
+ const msg = JSON.parse(trimmed);
198
+ if (msg.id && this.pending.has(msg.id)) {
199
+ const { resolve } = this.pending.get(msg.id);
200
+ this.pending.delete(msg.id);
201
+ resolve(msg.result || msg.error || {});
202
+ }
203
+ } catch { /* skip non-JSON */ }
204
+ }
205
+ }
206
+
207
+ _send(method, params) {
208
+ return new Promise((resolve, reject) => {
209
+ if (!this.process || this.process.killed) {
210
+ return reject(new Error(`${this.name} process not running`));
211
+ }
212
+ const id = randomUUID();
213
+ const msg = JSON.stringify({ jsonrpc: "2.0", id, method, params }) + "\n";
214
+ this.pending.set(id, { resolve, reject });
215
+ this.process.stdin.write(msg);
216
+ setTimeout(() => {
217
+ if (this.pending.has(id)) {
218
+ this.pending.delete(id);
219
+ reject(new Error(`${this.name} timeout for ${method}`));
220
+ }
221
+ }, 30000);
222
+ });
223
+ }
224
+
225
+ _notify(method, params) {
226
+ if (!this.process || this.process.killed) return;
227
+ this.process.stdin.write(JSON.stringify({ jsonrpc: "2.0", method, params }) + "\n");
228
+ }
229
+
230
+ async callTool(originalName, args) {
231
+ if (!this.ready) return { error: `${this.name} backend not available` };
232
+ try {
233
+ return await this._send("tools/call", { name: originalName, arguments: args });
234
+ } catch (err) {
235
+ return { error: err.message };
236
+ }
237
+ }
238
+
239
+ stop() {
240
+ if (this.process && !this.process.killed) {
241
+ this.process.kill("SIGTERM");
242
+ this.process = null;
243
+ }
244
+ this.ready = false;
245
+ this.tools = [];
246
+ }
247
+ }
248
+
249
+ // =============================================================================
250
+ // BACKEND REGISTRY
251
+ // =============================================================================
252
+
253
+ const BACKEND_DEFS = [
254
+ { name: "ruvector", command: "npx", args: ["-y", "ruvector", "mcp", "start"], groups: ["intelligence"] },
255
+ { name: "ruflo", command: "npx", args: ["-y", "ruflo", "mcp", "start"], groups: ["agents", "memory", "devtools", "security", "browser", "neural"] },
256
+ { name: "agentic-flow", command: "npx", args: ["-y", "agentic-flow@alpha", "mcp", "start"], groups: ["agentic-flow"] },
257
+ { name: "claude", command: "claude", args: ["mcp", "serve"], groups: ["claude-code"] },
258
+ { name: "gemini-mcp", command: "npx", args: ["-y", "gemini-mcp-server"], groups: ["gemini"] },
259
+ { name: "codex", command: "npx", args: ["-y", "@openai/codex", "mcp", "serve"], groups: ["codex"] },
260
+ ];
261
+
262
+ const mcpBackends = new Map();
263
+ let allBackendTools = []; // all tools from all backends (pre-filter)
264
+
265
+ function isBackendNeeded(backendDef) {
266
+ return backendDef.groups.some(g => TOOL_GROUPS[g]?.enabled);
267
+ }
268
+
269
+ // Filter tools from a backend based on which groups are enabled
270
+ function filterToolsByGroups(tools, backendName) {
271
+ const enabledGroups = Object.entries(TOOL_GROUPS)
272
+ .filter(([, g]) => g.enabled && g.source === backendName);
273
+
274
+ if (enabledGroups.length === 0) return [];
275
+
276
+ // If any enabled group has no prefixes defined, include all tools from that backend
277
+ const hasWildcard = enabledGroups.some(([, g]) => !g.prefixes);
278
+ if (hasWildcard) return tools;
279
+
280
+ const enabledPrefixes = enabledGroups.flatMap(([, g]) => g.prefixes || []);
281
+ return tools.filter(t => enabledPrefixes.some(p => t._originalName.startsWith(p)));
282
+ }
283
+
284
+ // Get the final filtered tool list with namespaced names
285
+ function getActiveTools() {
286
+ const filtered = [];
287
+ for (const [backendName, client] of mcpBackends) {
288
+ const accepted = filterToolsByGroups(client.tools, backendName);
289
+ for (const t of accepted) {
290
+ filtered.push({ ...t, name: `${backendName}__${t._originalName}` });
291
+ }
292
+ }
293
+ return filtered;
294
+ }
295
+
296
+ async function initBackends() {
297
+ const needed = BACKEND_DEFS.filter(isBackendNeeded);
298
+ if (needed.length === 0) return;
299
+
300
+ console.log(`Starting ${needed.length} MCP backends: ${needed.map(b => b.name).join(", ")}`);
301
+
302
+ await Promise.allSettled(
303
+ needed.map(async (b) => {
304
+ const client = new StdioMcpClient(b.name, b.command, b.args);
305
+ const ok = await client.start();
306
+ if (ok) {
307
+ mcpBackends.set(b.name, client);
308
+ } else {
309
+ console.warn(`[${b.name}] failed to start`);
310
+ }
311
+ })
312
+ );
313
+
314
+ allBackendTools = getActiveTools();
315
+ console.log(`MCP backends: ${mcpBackends.size} active, ${allBackendTools.length} tools (filtered by groups)`);
316
+ }
317
+
318
+ process.on("SIGTERM", () => { for (const [, c] of mcpBackends) c.stop(); process.exit(0); });
319
+ process.on("SIGINT", () => { for (const [, c] of mcpBackends) c.stop(); process.exit(0); });
320
+
321
+ // =============================================================================
322
+ // BUILT-IN TOOLS (core group — always on)
323
+ // =============================================================================
324
+
325
+ const BUILTIN_TOOLS = [
326
+ {
327
+ name: "search",
328
+ description: "Search your knowledge base for relevant information.",
329
+ inputSchema: {
330
+ type: "object",
331
+ properties: {
332
+ query: { type: "string", description: "Natural language search query" },
333
+ limit: { type: "number", description: "Max results (default 5)", default: 5 },
334
+ },
335
+ required: ["query"],
336
+ },
337
+ },
338
+ {
339
+ name: "web_research",
340
+ description: "Search the web, fact-check claims, compare items, or conduct deep research. Actions: 'search' (quick), 'research' (deep report), 'compare' (side-by-side), 'fact_check' (verify claims), 'goap' (comprehensive multi-step research with verification).",
341
+ inputSchema: {
342
+ type: "object",
343
+ properties: {
344
+ action: { type: "string", enum: ["search", "research", "compare", "fact_check", "goap"], description: "Research action type", default: "search" },
345
+ query: { type: "string", description: "Search query or topic" },
346
+ items: { type: "array", items: { type: "string" }, description: "Items to compare (for 'compare')" },
347
+ claim: { type: "string", description: "Claim to verify (for 'fact_check')" },
348
+ verify: { type: "boolean", description: "Verify results in goap mode", default: true },
349
+ },
350
+ required: ["query"],
351
+ },
352
+ },
353
+ {
354
+ name: "guidance",
355
+ description: "Get instructions on how to use the available tool groups and services. Call this FIRST when unsure which tool to use, when a user asks 'what can you do?', or when you need to understand a specific tool group. Returns structured guidance for the AI on tool selection and usage patterns.",
356
+ inputSchema: {
357
+ type: "object",
358
+ properties: {
359
+ topic: {
360
+ type: "string",
361
+ enum: ["overview", "groups", "intelligence", "agents", "memory", "devtools", "security", "browser", "neural", "agentic-flow", "claude-code", "gemini", "codex", "tool"],
362
+ description: "What to get guidance on. Use 'overview' for capabilities summary, 'groups' to see all tool groups and their status, or a specific group name for detailed usage instructions.",
363
+ default: "overview",
364
+ },
365
+ tool_name: { type: "string", description: "Specific tool name to get detailed usage info (when topic='tool')" },
366
+ },
367
+ },
368
+ },
369
+ ];
370
+
371
+ // =============================================================================
372
+ // GUIDANCE ENGINE — AI-facing instruction system
373
+ // =============================================================================
374
+
375
+ function getGuidance(topic, toolName) {
376
+ const activeGroups = Object.entries(TOOL_GROUPS).filter(([, g]) => g.enabled);
377
+ const inactiveGroups = Object.entries(TOOL_GROUPS).filter(([, g]) => !g.enabled);
378
+ const externalTools = getActiveTools();
379
+
380
+ if (topic === "overview") {
381
+ return {
382
+ guidance: `# Tool Capabilities Overview
383
+
384
+ You have access to ${BUILTIN_TOOLS.length + externalTools.length} tools organized into ${activeGroups.length} active groups.
385
+
386
+ ## Active Groups
387
+ ${activeGroups.map(([name, g]) => {
388
+ const count = name === "core" ? BUILTIN_TOOLS.length : externalTools.filter(t => t.name.startsWith(g.source + "__")).length;
389
+ return `- **${name}** (${count} tools) — ${g.description}`;
390
+ }).join("\n")}
391
+
392
+ ## Inactive Groups (can be enabled)
393
+ ${inactiveGroups.map(([name, g]) => `- **${name}** — ${g.description}`).join("\n") || "None"}
394
+
395
+ ## Quick Decision Guide
396
+ - **Knowledge questions** → use \`search\` first, then \`web_research\` if needed
397
+ - **Current events / facts** → use \`web_research\` with action 'search' or 'goap'
398
+ - **Complex research** → use \`web_research\` with action 'goap' (multi-step pipeline)
399
+ - **"What can you do?"** → call \`guidance\` with topic 'groups'
400
+ - **Memory / recall** → use tools from the \`memory\` group
401
+ - **Agent orchestration** → use tools from the \`agents\` group
402
+ - **Code analysis / performance** → use tools from the \`devtools\` group
403
+
404
+ ## Rules
405
+ 1. Call tools FIRST, then present results conversationally
406
+ 2. Never show raw JSON — synthesize results naturally
407
+ 3. For complex questions, prefer GOAP pipeline (web_research action='goap')
408
+ 4. Call \`guidance\` with a specific group name to learn how to use that group's tools`,
409
+ topic: "overview",
410
+ };
411
+ }
412
+
413
+ if (topic === "groups") {
414
+ const groupList = Object.entries(TOOL_GROUPS).map(([name, g]) => {
415
+ const status = g.enabled ? "ACTIVE" : "INACTIVE";
416
+ const toolCount = name === "core" ? BUILTIN_TOOLS.length :
417
+ externalTools.filter(t => {
418
+ const backend = t._backend;
419
+ return g.source === backend && (!g.prefixes || g.prefixes.some(p => t._originalName.startsWith(p)));
420
+ }).length;
421
+ return `| ${name} | ${status} | ${toolCount} | ${g.description} |`;
422
+ });
423
+
424
+ return {
425
+ guidance: `# Tool Groups\n\n| Group | Status | Tools | Description |\n|-------|--------|-------|-------------|\n${groupList.join("\n")}`,
426
+ topic: "groups",
427
+ };
428
+ }
429
+
430
+ // Specific group guidance
431
+ const groupGuides = {
432
+ intelligence: `# Intelligence Group (ruvector)
433
+
434
+ Self-learning intelligence tools for routing and vector memory.
435
+
436
+ ## Key Tools
437
+ - **ruvector__hooks_route** — Route a task to the best agent type. Call with a task description.
438
+ - **ruvector__hooks_remember** — Store context/knowledge in vector memory for later recall.
439
+ - **ruvector__hooks_recall** — Search vector memory semantically. Good for finding past context.
440
+ - **ruvector__hooks_pretrain** — Bootstrap intelligence from a code repository.
441
+ - **ruvector__hooks_build_agents** — Generate optimized agent configurations.
442
+ - **ruvector__hooks_stats** — Get intelligence statistics and learning metrics.
443
+
444
+ ## When to Use
445
+ - Before starting complex tasks: route to find the best agent approach
446
+ - To store important findings for cross-session memory
447
+ - To recall previously stored patterns or solutions`,
448
+
449
+ agents: `# Agents & Orchestration Group (ruflo)
450
+
451
+ Multi-agent lifecycle management, swarm coordination, and task workflows.
452
+
453
+ ## Key Tools
454
+ - **ruflo__agent_spawn** — Create a new agent with specific capabilities
455
+ - **ruflo__agent_list** — List all active agents
456
+ - **ruflo__swarm_init** — Initialize a swarm with a topology (mesh, hierarchical, ring, star)
457
+ - **ruflo__task_create** — Create and assign tasks
458
+ - **ruflo__workflow_create** — Define multi-step workflows
459
+ - **ruflo__workflow_execute** — Execute a workflow
460
+ - **ruflo__hive-mind_init** — Start collective intelligence coordination
461
+ - **ruflo__coordination_orchestrate** — Multi-agent coordination
462
+
463
+ ## When to Use
464
+ - Complex tasks requiring multiple agents working together
465
+ - Pipeline workflows with sequential or parallel steps
466
+ - Distributed task management`,
467
+
468
+ memory: `# Memory & Knowledge Group (ruflo)
469
+
470
+ Vector storage, semantic search, AgentDB pattern learning, and embeddings.
471
+
472
+ ## Key Tools
473
+ - **ruflo__memory_store** — Store a value with vector embedding for semantic search
474
+ - **ruflo__memory_search** — Semantic search across stored memories (HNSW-indexed)
475
+ - **ruflo__memory_list** — List stored memory entries
476
+ - **ruflo__agentdb_pattern-store** — Store a reasoning pattern for learning
477
+ - **ruflo__agentdb_pattern-search** — Search for similar reasoning patterns
478
+ - **ruflo__agentdb_context-synthesize** — Synthesize context from stored memories
479
+ - **ruflo__embeddings_generate** — Generate vector embeddings for text
480
+ - **ruflo__embeddings_search** — Semantic similarity search
481
+
482
+ ## When to Use
483
+ - Persistent knowledge storage across sessions
484
+ - Finding similar past solutions or patterns
485
+ - Building semantic search over custom data`,
486
+
487
+ devtools: `# Dev Tools Group (ruflo)
488
+
489
+ Code analysis, performance profiling, GitHub integration, and terminal access.
490
+
491
+ ## Key Tools
492
+ - **ruflo__analyze_diff** — Analyze git diff for risk and change classification
493
+ - **ruflo__performance_benchmark** — Run performance benchmarks
494
+ - **ruflo__performance_bottleneck** — Detect performance bottlenecks
495
+ - **ruflo__github_repo_analyze** — Analyze a GitHub repository
496
+ - **ruflo__github_pr_manage** — Manage pull requests
497
+ - **ruflo__terminal_execute** — Execute commands in a terminal session
498
+
499
+ ## When to Use
500
+ - Code review and change risk assessment
501
+ - Performance analysis and optimization
502
+ - GitHub repository management`,
503
+
504
+ security: `# Security & Safety Group (ruflo)
505
+
506
+ AI defence, PII detection, and claims-based authorization.
507
+
508
+ ## Key Tools
509
+ - **ruflo__aidefence_scan** — Scan text for AI manipulation attempts
510
+ - **ruflo__aidefence_has_pii** — Check for PII (emails, phones, SSNs)
511
+ - **ruflo__aidefence_is_safe** — Quick safety check on input
512
+ - **ruflo__claims_claim** — Claim an issue for work
513
+ - **ruflo__claims_board** — Visual board of all claims
514
+
515
+ ## When to Use
516
+ - Input validation and safety checking
517
+ - PII detection before processing sensitive data
518
+ - Work item management across agents`,
519
+
520
+ browser: `# Browser Automation Group (ruflo)
521
+
522
+ Headless browser control for web interaction and testing.
523
+
524
+ ## Key Tools
525
+ - **ruflo__browser_open** — Navigate to a URL
526
+ - **ruflo__browser_click** — Click elements by reference
527
+ - **ruflo__browser_fill** — Fill form inputs
528
+ - **ruflo__browser_screenshot** — Capture page screenshots
529
+ - **ruflo__browser_snapshot** — Get accessibility tree for AI parsing
530
+ - **ruflo__browser_eval** — Execute JavaScript in page context
531
+
532
+ ## When to Use
533
+ - Web scraping and data extraction
534
+ - Automated testing (E2E)
535
+ - Form filling and web interaction`,
536
+
537
+ neural: `# Neural & DAA Group (ruflo)
538
+
539
+ Neural network operations and Decentralized Autonomous Agents.
540
+
541
+ ## Key Tools
542
+ - **ruflo__neural_train** — Train a neural model
543
+ - **ruflo__neural_predict** — Make predictions
544
+ - **ruflo__daa_agent_create** — Create an autonomous agent
545
+ - **ruflo__daa_workflow_create** — Create autonomous workflows
546
+ - **ruflo__daa_knowledge_share** — Share knowledge between agents
547
+
548
+ ## When to Use
549
+ - Pattern learning and prediction
550
+ - Autonomous agent workflows
551
+ - Knowledge transfer between agents`,
552
+
553
+ "agentic-flow": `# Agentic Flow Group (agentic-flow@alpha)
554
+
555
+ Execute 66+ specialized agents with boosted code editing and AgentDB.
556
+
557
+ ## Key Tools
558
+ - **agentic-flow__agentic_flow_agent** — Execute any of 66+ specialized agents
559
+ - **agentic-flow__agentic_flow_list_agents** — List available agent types
560
+ - **agentic-flow__agent_booster_edit_file** — 352x faster code editing
561
+ - **agentic-flow__agent_booster_batch_edit** — Multi-file refactoring
562
+ - **agentic-flow__agentdb_pattern_store** — Store reasoning patterns
563
+ - **agentic-flow__agentdb_pattern_search** — Search similar patterns
564
+
565
+ ## When to Use
566
+ - Complex code generation with specialized agents
567
+ - Batch code refactoring across files
568
+ - Agent selection when you need the right specialist`,
569
+
570
+ "claude-code": `# Claude Code Group
571
+
572
+ Anthropic Claude Code MCP server — full coding agent capabilities.
573
+
574
+ Requires: ANTHROPIC_API_KEY environment variable.
575
+
576
+ ## Capabilities
577
+ - File reading and editing
578
+ - Bash command execution
579
+ - Code analysis and generation
580
+ - Project exploration
581
+
582
+ ## When to Use
583
+ - When you need a second AI perspective on code
584
+ - Complex refactoring tasks
585
+ - Code review and analysis`,
586
+
587
+ gemini: `# Gemini MCP Group
588
+
589
+ Google Gemini with conversation context management.
590
+
591
+ Requires: GOOGLE_API_KEY environment variable (already set for Gemini models).
592
+
593
+ ## Capabilities
594
+ - Conversation context management
595
+ - Multimodal processing
596
+ - Google Search grounding
597
+
598
+ ## When to Use
599
+ - Extended context conversations
600
+ - Multimodal content processing`,
601
+
602
+ codex: `# Codex Group
603
+
604
+ OpenAI Codex coding agent.
605
+
606
+ Requires: OPENAI_API_KEY environment variable (already set for OpenAI models).
607
+
608
+ ## Capabilities
609
+ - Code generation and execution
610
+ - Code completion
611
+ - Code explanation
612
+
613
+ ## When to Use
614
+ - Code generation tasks
615
+ - Quick code completions
616
+ - Code explanation and documentation`,
617
+ };
618
+
619
+ if (topic === "tool" && toolName) {
620
+ const allTools = [...BUILTIN_TOOLS, ...externalTools];
621
+ const tool = allTools.find(t => t.name === toolName);
622
+ if (tool) {
623
+ const props = Object.entries(tool.inputSchema?.properties || {})
624
+ .map(([k, v]) => `- **${k}** (${v.type}) — ${v.description || ""}`)
625
+ .join("\n");
626
+ return { guidance: `# ${tool.name}\n\n${tool.description}\n\n## Parameters\n${props}`, topic: "tool" };
627
+ }
628
+ return { guidance: `Tool '${toolName}' not found. Call guidance with topic='groups' to see available tools.`, topic: "tool" };
629
+ }
630
+
631
+ if (groupGuides[topic]) {
632
+ const group = TOOL_GROUPS[topic];
633
+ if (!group?.enabled) {
634
+ return { guidance: `# ${topic} — INACTIVE\n\n${group?.description || ""}\n\nThis group is not enabled. Set the appropriate MCP_GROUP_* env var to "true" to activate it.`, topic };
635
+ }
636
+ return { guidance: groupGuides[topic], topic };
637
+ }
638
+
639
+ return { guidance: `Unknown topic '${topic}'. Use 'overview', 'groups', or a specific group name.`, topic };
640
+ }
641
+
642
+ // =============================================================================
643
+ // GEMINI GROUNDED SEARCH — Uses Gemini's built-in Google Search when no
644
+ // dedicated search Cloud Function is configured
645
+ // =============================================================================
646
+
647
+ async function geminiGroundedSearch(query, mode = "search") {
648
+ const apiKey = process.env.GOOGLE_API_KEY;
649
+ if (!apiKey) return { error: "No GOOGLE_API_KEY configured for search" };
650
+
651
+ const model = "gemini-2.5-flash";
652
+ const url = `https://generativelanguage.googleapis.com/v1beta/models/${model}:generateContent?key=${apiKey}`;
653
+
654
+ const systemInstructions = {
655
+ search: `You are a helpful search assistant. Answer the query using Google Search results. Include key facts, sources, and relevant details. Be concise but thorough.`,
656
+ research: `You are a research analyst. Provide a comprehensive research report on the topic using Google Search results. Include: key findings, analysis, multiple perspectives, and source citations. Be thorough.`,
657
+ compare: `You are a comparison analyst. Compare the items using Google Search results. Create a structured comparison with pros/cons, key differences, and a recommendation.`,
658
+ fact_check: `You are a fact-checker. Verify the claim using Google Search results. Provide a verdict (TRUE/FALSE/PARTIALLY TRUE/UNVERIFIABLE), evidence, and sources.`,
659
+ };
660
+
661
+ const prompt = mode === "fact_check"
662
+ ? `Fact-check this claim: "${query}"`
663
+ : mode === "compare"
664
+ ? `Compare these items: ${query}`
665
+ : mode === "research"
666
+ ? `Research this topic thoroughly: ${query}`
667
+ : query;
668
+
669
+ try {
670
+ const resp = await fetch(url, {
671
+ method: "POST",
672
+ headers: { "Content-Type": "application/json" },
673
+ body: JSON.stringify({
674
+ system_instruction: { parts: [{ text: systemInstructions[mode] || systemInstructions.search }] },
675
+ contents: [{ parts: [{ text: prompt }] }],
676
+ tools: [{ google_search: {} }],
677
+ generationConfig: { temperature: 0.2 },
678
+ }),
679
+ signal: AbortSignal.timeout(30000),
680
+ });
681
+
682
+ if (!resp.ok) {
683
+ const errText = await resp.text();
684
+ console.error(`[gemini-search] API error ${resp.status}:`, errText.substring(0, 200));
685
+ return { error: `Search API error: ${resp.status}` };
686
+ }
687
+
688
+ const data = await resp.json();
689
+ const candidate = data.candidates?.[0];
690
+ const answer = candidate?.content?.parts?.map(p => p.text).filter(Boolean).join("\n") || "";
691
+
692
+ // Extract grounding metadata (sources)
693
+ const grounding = candidate?.groundingMetadata || {};
694
+ const chunks = grounding.groundingChunks || [];
695
+ const sources = chunks
696
+ .filter(c => c.web)
697
+ .map(c => ({ title: c.web.title || "", url: c.web.uri || "" }))
698
+ .filter(s => s.url);
699
+
700
+ // Extract search queries used
701
+ const searchQueries = (grounding.webSearchQueries || []);
702
+
703
+ return {
704
+ success: true,
705
+ answer,
706
+ sources: sources.slice(0, 8),
707
+ searchQueries,
708
+ groundingMetadata: { sources, searchQueries },
709
+ mode,
710
+ };
711
+ } catch (err) {
712
+ if (err.name === "AbortError" || err.name === "TimeoutError") return { error: "Search timed out" };
713
+ return { error: err.message };
714
+ }
715
+ }
716
+
717
+ // =============================================================================
718
+ // HELPER — Call a backend Cloud Function / API
719
+ // =============================================================================
720
+
721
+ async function callCloudFunction(url, payload, timeoutMs = 25000) {
722
+ const controller = new AbortController();
723
+ const timer = setTimeout(() => controller.abort(), timeoutMs);
724
+ try {
725
+ const resp = await fetch(url, {
726
+ method: "POST",
727
+ headers: { "Content-Type": "application/json" },
728
+ body: JSON.stringify(payload),
729
+ signal: controller.signal,
730
+ });
731
+ return await resp.json();
732
+ } catch (err) {
733
+ if (err.name === "AbortError") return { error: "Request timed out", timeout: timeoutMs };
734
+ return { error: err.message };
735
+ } finally {
736
+ clearTimeout(timer);
737
+ }
738
+ }
739
+
740
+ // =============================================================================
741
+ // GOAP SEARCH PIPELINE
742
+ // =============================================================================
743
+
744
+ async function executeGoapSearch(query, args) {
745
+ const researchUrl = CLOUD_FUNCTIONS.research;
746
+ if (!researchUrl) return { error: "GOAP requires a 'research' URL in CLOUD_FUNCTIONS" };
747
+
748
+ const startTime = Date.now();
749
+
750
+ const composeResult = await callCloudFunction(researchUrl, {
751
+ action: "search",
752
+ query: `Break this question into 3-4 distinct search queries that would help answer it comprehensively. Return ONLY the queries, one per line:\n\n${query}`,
753
+ }, 30000);
754
+
755
+ let searchQueries = [query];
756
+ if (composeResult && !composeResult.error) {
757
+ const answer = composeResult.result?.answer || composeResult.answer || "";
758
+ const lines = answer.split("\n").map(l => l.replace(/^[\d\-\*\.\)]+\s*/, "").trim()).filter(l => l.length > 5 && l.length < 200);
759
+ if (lines.length >= 2) searchQueries = lines.slice(0, 4);
760
+ }
761
+
762
+ const searchResults = await Promise.all(
763
+ searchQueries.map(q => callCloudFunction(researchUrl, { action: "search", query: q }, 30000))
764
+ );
765
+
766
+ const allSources = [], allAnswers = [];
767
+ for (let i = 0; i < searchResults.length; i++) {
768
+ const r = searchResults[i];
769
+ if (r && !r.error && r.success !== false) {
770
+ const answer = r.result?.answer || r.answer || "";
771
+ if (answer) allAnswers.push({ query: searchQueries[i], answer });
772
+ const gm = r.result?.groundingMetadata || r.groundingMetadata || {};
773
+ if (gm.sources) allSources.push(...gm.sources);
774
+ }
775
+ }
776
+
777
+ const uniqueSources = [];
778
+ const seenUrls = new Set();
779
+ for (const src of allSources) {
780
+ const url = src.url || src.uri || "";
781
+ if (url && !seenUrls.has(url)) { seenUrls.add(url); uniqueSources.push(src); }
782
+ }
783
+
784
+ const synthesisInput = allAnswers.map(a => `## ${a.query}\n${a.answer}`).join("\n\n");
785
+ const synthesisResult = await callCloudFunction(researchUrl, {
786
+ action: "research",
787
+ topic: `Synthesize these findings into a comprehensive answer to: "${query}"\n\nFindings:\n${synthesisInput}`,
788
+ }, 60000);
789
+
790
+ const synthesizedAnswer = synthesisResult?.result?.answer || synthesisResult?.answer || synthesisInput;
791
+ const synthGm = synthesisResult?.result?.groundingMetadata || {};
792
+ if (synthGm.sources) {
793
+ for (const src of synthGm.sources) {
794
+ const url = src.url || src.uri || "";
795
+ if (url && !seenUrls.has(url)) { seenUrls.add(url); uniqueSources.push(src); }
796
+ }
797
+ }
798
+
799
+ let verification = { verified: true, confidence: "high" };
800
+ if (args.verify !== false && synthesizedAnswer.length > 100) {
801
+ const vr = await callCloudFunction(researchUrl, {
802
+ action: "fact_check", claim: synthesizedAnswer.substring(0, 500),
803
+ }, 30000);
804
+ if (vr && !vr.error && vr.result) {
805
+ verification = {
806
+ verified: vr.result.verdict !== "FALSE",
807
+ verdict: vr.result.verdict,
808
+ confidence: vr.result.confidence || "medium",
809
+ details: vr.result.analysis,
810
+ };
811
+ }
812
+ }
813
+
814
+ return {
815
+ answer: synthesizedAnswer, pipeline: "goap",
816
+ steps: { queries_composed: searchQueries.length, searches_executed: searchResults.filter(r => !r?.error).length, sources_found: uniqueSources.length, verification },
817
+ sources: uniqueSources.slice(0, 10), searchQueries, duration_ms: Date.now() - startTime,
818
+ };
819
+ }
820
+
821
+ // =============================================================================
822
+ // GOAP SEARCH PIPELINE — Gemini fallback (no Cloud Function needed)
823
+ // =============================================================================
824
+
825
+ async function executeGoapSearchGemini(query, args) {
826
+ const startTime = Date.now();
827
+
828
+ // Step 1: Decompose into sub-queries
829
+ const decompose = await geminiGroundedSearch(
830
+ `Break this question into 3-4 distinct search queries that would help answer it comprehensively. Return ONLY the queries, one per line:\n\n${query}`,
831
+ "search"
832
+ );
833
+
834
+ let searchQueries = [query];
835
+ if (decompose?.answer) {
836
+ const lines = decompose.answer.split("\n")
837
+ .map(l => l.replace(/^[\d\-\*\.\)]+\s*/, "").trim())
838
+ .filter(l => l.length > 5 && l.length < 200);
839
+ if (lines.length >= 2) searchQueries = lines.slice(0, 4);
840
+ }
841
+
842
+ // Step 2: Parallel searches
843
+ const searchResults = await Promise.all(
844
+ searchQueries.map(q => geminiGroundedSearch(q, "search"))
845
+ );
846
+
847
+ const allSources = [], allAnswers = [];
848
+ for (let i = 0; i < searchResults.length; i++) {
849
+ const r = searchResults[i];
850
+ if (r && !r.error && r.answer) {
851
+ allAnswers.push({ query: searchQueries[i], answer: r.answer });
852
+ if (r.sources) allSources.push(...r.sources);
853
+ }
854
+ }
855
+
856
+ // Dedupe sources
857
+ const seenUrls = new Set();
858
+ const uniqueSources = allSources.filter(s => {
859
+ if (seenUrls.has(s.url)) return false;
860
+ seenUrls.add(s.url);
861
+ return true;
862
+ });
863
+
864
+ // Step 3: Synthesize
865
+ const synthesisInput = allAnswers.map(a => `## ${a.query}\n${a.answer}`).join("\n\n");
866
+ const synthesis = await geminiGroundedSearch(
867
+ `Synthesize these findings into a comprehensive answer to: "${query}"\n\nFindings:\n${synthesisInput}`,
868
+ "research"
869
+ );
870
+
871
+ const finalAnswer = synthesis?.answer || synthesisInput;
872
+ if (synthesis?.sources) {
873
+ for (const s of synthesis.sources) {
874
+ if (!seenUrls.has(s.url)) { seenUrls.add(s.url); uniqueSources.push(s); }
875
+ }
876
+ }
877
+
878
+ // Step 4: Verify if requested
879
+ let verification = { verified: true, confidence: "high" };
880
+ if (args.verify !== false && finalAnswer.length > 100) {
881
+ const vr = await geminiGroundedSearch(finalAnswer.substring(0, 500), "fact_check");
882
+ if (vr && !vr.error) {
883
+ verification = { verified: true, confidence: "medium", details: vr.answer };
884
+ }
885
+ }
886
+
887
+ return {
888
+ success: true,
889
+ answer: finalAnswer,
890
+ pipeline: "goap-gemini",
891
+ steps: {
892
+ queries_composed: searchQueries.length,
893
+ searches_executed: searchResults.filter(r => !r?.error).length,
894
+ sources_found: uniqueSources.length,
895
+ verification,
896
+ },
897
+ sources: uniqueSources.slice(0, 10),
898
+ searchQueries,
899
+ duration_ms: Date.now() - startTime,
900
+ };
901
+ }
902
+
903
+ // =============================================================================
904
+ // TOOL EXECUTOR
905
+ // =============================================================================
906
+
907
+ async function executeTool(name, args) {
908
+ switch (name) {
909
+ case "search": {
910
+ if (CLOUD_FUNCTIONS.search) {
911
+ return callCloudFunction(CLOUD_FUNCTIONS.search, { query: args.query, limit: args.limit || 5 });
912
+ }
913
+ // Fallback: use Gemini grounded search
914
+ return geminiGroundedSearch(args.query, "search");
915
+ }
916
+
917
+ case "web_research": {
918
+ const action = args.action || "search";
919
+ if (action === "goap") {
920
+ // GOAP needs research endpoint — fall back to multi-search via Gemini
921
+ if (CLOUD_FUNCTIONS.research) return executeGoapSearch(args.query, args);
922
+ return executeGoapSearchGemini(args.query, args);
923
+ }
924
+ if (CLOUD_FUNCTIONS.research) {
925
+ const payload = { action };
926
+ if (action === "search") payload.query = args.query;
927
+ else if (action === "research") payload.topic = args.query;
928
+ else if (action === "compare") payload.items = args.items;
929
+ else if (action === "fact_check") payload.claim = args.claim || args.query;
930
+ return callCloudFunction(CLOUD_FUNCTIONS.research, payload, 60000);
931
+ }
932
+ // Fallback: Gemini grounded search
933
+ const mode = action === "fact_check" ? "fact_check" : action === "compare" ? "compare" : action === "research" ? "research" : "search";
934
+ const query = action === "compare" ? (args.items || []).join(" vs ") : (args.claim || args.query);
935
+ return geminiGroundedSearch(query, mode);
936
+ }
937
+
938
+ case "guidance":
939
+ return getGuidance(args.topic || "overview", args.tool_name);
940
+
941
+ default: {
942
+ // Route to external MCP backend
943
+ const activeTools = getActiveTools();
944
+ const extTool = activeTools.find(t => t.name === name);
945
+ if (extTool) {
946
+ const backend = mcpBackends.get(extTool._backend);
947
+ if (backend) return backend.callTool(extTool._originalName, args);
948
+ return { error: `Backend ${extTool._backend} not available` };
949
+ }
950
+ return { error: `Unknown tool: ${name}. Call 'guidance' with topic='groups' to see available tools.` };
951
+ }
952
+ }
953
+ }
954
+
955
+ // =============================================================================
956
+ // PER-GROUP TOOL HELPERS
957
+ // =============================================================================
958
+
959
+ // Get tools for a specific group only
960
+ function getToolsForGroup(groupName) {
961
+ const group = TOOL_GROUPS[groupName];
962
+ if (!group || !group.enabled) return [];
963
+ if (groupName === "core") return BUILTIN_TOOLS;
964
+
965
+ const allActive = getActiveTools();
966
+ if (!group.prefixes) {
967
+ // No prefix filter — return all tools from this backend
968
+ return allActive.filter(t => t._backend === group.source);
969
+ }
970
+ return allActive.filter(t =>
971
+ t._backend === group.source && group.prefixes.some(p => t._originalName.startsWith(p))
972
+ );
973
+ }
974
+
975
+ // Group display names for the Chat UI
976
+ const GROUP_DISPLAY_NAMES = {
977
+ core: "Core Tools",
978
+ intelligence: "Intelligence & Learning",
979
+ agents: "Agents & Orchestration",
980
+ memory: "Memory & Knowledge",
981
+ devtools: "Dev Tools & Analysis",
982
+ security: "Security & Safety",
983
+ browser: "Browser Automation",
984
+ neural: "Neural & DAA",
985
+ "agentic-flow": "Agentic Flow",
986
+ "claude-code": "Claude Code",
987
+ gemini: "Gemini",
988
+ codex: "Codex",
989
+ };
990
+
991
+ // =============================================================================
992
+ // MCP SERVER — Multiple endpoints per group
993
+ // =============================================================================
994
+
995
+ const app = express();
996
+ app.use(express.json({ limit: "10mb" }));
997
+
998
+ // ---------- CORS middleware ----------
999
+ app.use((req, res, next) => {
1000
+ res.setHeader("Access-Control-Allow-Origin", "*");
1001
+ res.setHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS");
1002
+ res.setHeader("Access-Control-Allow-Headers", "Content-Type, Authorization");
1003
+ if (req.method === "OPTIONS") return res.sendStatus(204);
1004
+ next();
1005
+ });
1006
+
1007
+ // ---------- Shared MCP handler ----------
1008
+ function createMcpHandler(groupName) {
1009
+ return async (req, res) => {
1010
+ const { method, id, params } = req.body;
1011
+ try {
1012
+ switch (method) {
1013
+ case "initialize":
1014
+ return res.json({
1015
+ jsonrpc: "2.0", id,
1016
+ result: {
1017
+ protocolVersion: "2024-11-05",
1018
+ capabilities: { tools: {} },
1019
+ serverInfo: { name: `mcp-bridge/${groupName}`, version: "2.0.0" },
1020
+ },
1021
+ });
1022
+ case "tools/list": {
1023
+ const tools = getToolsForGroup(groupName);
1024
+ return res.json({ jsonrpc: "2.0", id, result: { tools } });
1025
+ }
1026
+ case "tools/call": {
1027
+ const { name, arguments: toolArgs } = params;
1028
+ const result = await executeTool(name, toolArgs || {});
1029
+ return res.json({
1030
+ jsonrpc: "2.0", id,
1031
+ result: {
1032
+ content: [{ type: "text", text: typeof result === "string" ? result : JSON.stringify(result, null, 2) }],
1033
+ },
1034
+ });
1035
+ }
1036
+ case "notifications/initialized":
1037
+ return res.json({ jsonrpc: "2.0", id, result: {} });
1038
+ default:
1039
+ return res.json({ jsonrpc: "2.0", id, error: { code: -32601, message: `Method not found: ${method}` } });
1040
+ }
1041
+ } catch (err) {
1042
+ console.error(`MCP error [${groupName}/${method}]:`, err);
1043
+ return res.json({ jsonrpc: "2.0", id, error: { code: -32603, message: err.message } });
1044
+ }
1045
+ };
1046
+ }
1047
+
1048
+ function createMcpSseHandler(groupName) {
1049
+ return (req, res) => {
1050
+ res.setHeader("Content-Type", "text/event-stream");
1051
+ res.setHeader("Cache-Control", "no-cache");
1052
+ res.setHeader("Connection", "keep-alive");
1053
+ res.write(`data: ${JSON.stringify({ type: "endpoint", url: `/mcp/${groupName}` })}\n\n`);
1054
+ };
1055
+ }
1056
+
1057
+ // ---------- Register per-group endpoints ----------
1058
+ for (const groupName of Object.keys(TOOL_GROUPS)) {
1059
+ app.post(`/mcp/${groupName}`, createMcpHandler(groupName));
1060
+ app.get(`/mcp/${groupName}`, createMcpSseHandler(groupName));
1061
+ }
1062
+
1063
+ // ---------- Catch-all /mcp — serves ALL enabled tools (backwards-compatible) ----------
1064
+ app.post("/mcp", async (req, res) => {
1065
+ const { method, id, params } = req.body;
1066
+ try {
1067
+ switch (method) {
1068
+ case "initialize":
1069
+ return res.json({
1070
+ jsonrpc: "2.0", id,
1071
+ result: {
1072
+ protocolVersion: "2024-11-05",
1073
+ capabilities: { tools: {} },
1074
+ serverInfo: { name: "mcp-bridge", version: "2.0.0" },
1075
+ },
1076
+ });
1077
+ case "tools/list": {
1078
+ const activeTools = getActiveTools();
1079
+ return res.json({ jsonrpc: "2.0", id, result: { tools: [...BUILTIN_TOOLS, ...activeTools] } });
1080
+ }
1081
+ case "tools/call": {
1082
+ const { name, arguments: toolArgs } = params;
1083
+ const result = await executeTool(name, toolArgs || {});
1084
+ return res.json({
1085
+ jsonrpc: "2.0", id,
1086
+ result: {
1087
+ content: [{ type: "text", text: typeof result === "string" ? result : JSON.stringify(result, null, 2) }],
1088
+ },
1089
+ });
1090
+ }
1091
+ case "notifications/initialized":
1092
+ return res.json({ jsonrpc: "2.0", id, result: {} });
1093
+ default:
1094
+ return res.json({ jsonrpc: "2.0", id, error: { code: -32601, message: `Method not found: ${method}` } });
1095
+ }
1096
+ } catch (err) {
1097
+ console.error(`MCP error [${method}]:`, err);
1098
+ return res.json({ jsonrpc: "2.0", id, error: { code: -32603, message: err.message } });
1099
+ }
1100
+ });
1101
+
1102
+ app.get("/mcp", (req, res) => {
1103
+ res.setHeader("Content-Type", "text/event-stream");
1104
+ res.setHeader("Cache-Control", "no-cache");
1105
+ res.setHeader("Connection", "keep-alive");
1106
+ res.write(`data: ${JSON.stringify({ type: "endpoint", url: "/mcp" })}\n\n`);
1107
+ });
1108
+
1109
+ // ---------- GET /mcp-servers — returns MCP_SERVERS JSON for Chat UI config ----------
1110
+ app.get("/mcp-servers", (_, res) => {
1111
+ const servers = [];
1112
+ for (const [name, group] of Object.entries(TOOL_GROUPS)) {
1113
+ if (!group.enabled) continue;
1114
+ const tools = getToolsForGroup(name);
1115
+ if (tools.length === 0) continue;
1116
+ servers.push({
1117
+ name: GROUP_DISPLAY_NAMES[name] || name,
1118
+ url: `/mcp/${name}`,
1119
+ tools: tools.length,
1120
+ group: name,
1121
+ });
1122
+ }
1123
+ res.json(servers);
1124
+ });
1125
+
1126
+ // =============================================================================
1127
+ // CHAT COMPLETIONS PROXY
1128
+ // =============================================================================
1129
+
1130
+ const PROVIDER_ROUTES = {
1131
+ openai: { baseURL: "https://api.openai.com/v1/chat/completions", getKey: () => process.env.OPENAI_API_KEY },
1132
+ gemini: { baseURL: "https://generativelanguage.googleapis.com/v1beta/openai/chat/completions", getKey: () => process.env.GOOGLE_API_KEY },
1133
+ openrouter: { baseURL: "https://openrouter.ai/api/v1/chat/completions", getKey: () => process.env.OPENROUTER_API_KEY },
1134
+ };
1135
+
1136
+ function resolveProvider(model) {
1137
+ if (typeof model === "string") {
1138
+ if (model.startsWith("gemini-")) return "gemini";
1139
+ if (model.includes("/")) return "openrouter";
1140
+ }
1141
+ return "openai";
1142
+ }
1143
+
1144
+ // =============================================================================
1145
+ // SYSTEM PROMPT — Injected server-side into every chat completion request
1146
+ // =============================================================================
1147
+ // This comprehensive prompt teaches the AI how to use all 200+ MCP tools
1148
+ // across 5 groups. It is injected as the first system message, ensuring
1149
+ // consistent behavior regardless of what preprompt the Chat UI sends.
1150
+
1151
+ function buildSystemPrompt() {
1152
+ // Build dynamic group status
1153
+ const enabledGroups = Object.entries(TOOL_GROUPS)
1154
+ .filter(([, g]) => g.enabled)
1155
+ .map(([name]) => name);
1156
+
1157
+ return `You are an intelligent AI assistant with powerful tools organized into ${enabledGroups.length} active groups.
1158
+
1159
+ # CRITICAL RULES
1160
+ 1. Use tools proactively — NEVER guess answers, ALWAYS search first
1161
+ 2. For ANY factual question, current event, or research request → call \`web_research\` IMMEDIATELY
1162
+ 3. NEVER say "I don't have access to real-time information" — you DO via web_research
1163
+ 4. NEVER say "I can't search the web" — you CAN via web_research
1164
+ 5. Call tools FIRST, present results conversationally AFTER
1165
+ 6. When multiple tools could help, call them ALL in parallel
1166
+
1167
+ # Tool Groups
1168
+
1169
+ Tools prefixed with backend name (e.g., \`ruflo__agent_spawn\`). Always use the full prefixed name.
1170
+
1171
+ ## Group 1: Core Tools (always on)
1172
+
1173
+ - **search** — Search your knowledge base (documents, workflows, how-tos).
1174
+ Use for: internal knowledge, company docs, past conversations.
1175
+ \`{"query": "how to process a claim"}\`
1176
+
1177
+ - **web_research** — Search the internet via Google. This is your primary research tool.
1178
+ ALWAYS use this for: current events, facts, comparisons, any external knowledge.
1179
+ Actions:
1180
+ - \`search\` — Quick web search. Default, fast. \`{"action": "search", "query": "latest news on X"}\`
1181
+ - \`research\` — Deep report with synthesis. \`{"action": "research", "query": "comprehensive analysis of X"}\`
1182
+ - \`compare\` — Side-by-side comparison. \`{"action": "compare", "query": "X", "items": ["A", "B"]}\`
1183
+ - \`fact_check\` — Verify a claim. \`{"action": "fact_check", "query": "claim to verify"}\`
1184
+ - \`goap\` — Multi-step pipeline: decomposes → parallel searches → synthesizes → verifies. BEST for important questions.
1185
+ \`{"action": "goap", "query": "complex question requiring thorough research"}\`
1186
+
1187
+ - **guidance** — Get help on tool groups or specific tool usage.
1188
+ Topics: \`overview\`, \`groups\`, \`agents\`, \`memory\`, \`intelligence\`, \`devtools\`
1189
+ \`{"topic": "overview"}\` or \`{"topic": "tool", "tool_name": "ruflo__agent_spawn"}\`
1190
+
1191
+ ## Group 2: Intelligence & Learning (ruvector)
1192
+ Pattern learning, routing, code analysis, and trajectory tracking. ${TOOL_GROUPS.intelligence.enabled ? "ACTIVE" : "DISABLED"}
1193
+
1194
+ ### Essential Intelligence Tools:
1195
+ - **ruvector__hooks_route** — Route a task to the optimal agent type. Call this FIRST for complex tasks.
1196
+ \`{"task": "describe what needs to be done", "context": ["relevant info"]}\`
1197
+ Returns ranked agent recommendations with confidence scores.
1198
+ - **ruvector__hooks_remember** — Store a key-value pair in persistent memory for cross-session recall.
1199
+ \`{"key": "pattern-name", "value": "what to remember", "namespace": "patterns"}\`
1200
+ - **ruvector__hooks_recall** — Retrieve a previously stored memory by key.
1201
+ - **ruvector__hooks_suggest_context** — Get contextual suggestions based on current work.
1202
+ - **ruvector__hooks_swarm_recommend** — Get swarm topology recommendation for a task type.
1203
+ - **ruvector__hooks_capabilities** — List all intelligence system capabilities.
1204
+
1205
+ ### Code Analysis:
1206
+ - **ruvector__hooks_ast_analyze** — Analyze code structure (AST) of a file.
1207
+ - **ruvector__hooks_ast_complexity** — Get complexity metrics for code.
1208
+ - **ruvector__hooks_security_scan** — Scan code for security vulnerabilities.
1209
+ - **ruvector__hooks_diff_analyze** — Analyze a code diff for risk and impact.
1210
+ - **ruvector__hooks_diff_similar** — Find similar past diffs/changes.
1211
+
1212
+ ### Trajectory Learning (for multi-step tasks):
1213
+ - **ruvector__hooks_trajectory_begin** — Start tracking a multi-step task for learning.
1214
+ - **ruvector__hooks_trajectory_step** — Record a step in the current trajectory.
1215
+ - **ruvector__hooks_trajectory_end** — End trajectory, triggering pattern extraction.
1216
+
1217
+ ### Memory & Compression:
1218
+ - **ruvector__hooks_compress** — Compress/summarize long text for efficient storage.
1219
+ - **ruvector__hooks_rag_context** — Get RAG context for a query from stored knowledge.
1220
+ - **ruvector__hooks_learn** — Force the system to learn from provided examples.
1221
+ - **ruvector__hooks_batch_learn** — Learn from multiple examples at once.
1222
+ - **ruvector__hooks_stats** — View learning statistics and metrics.
1223
+ - **ruvector__hooks_doctor** — Run diagnostics on the intelligence system.
1224
+
1225
+ ## Group 3: Agents & Orchestration (ruflo)
1226
+ Spawn agents, coordinate swarms, manage tasks and workflows. ${TOOL_GROUPS.agents.enabled ? "ACTIVE" : "DISABLED"}
1227
+
1228
+ ### Agent Lifecycle:
1229
+ - **ruflo__agent_spawn** — Create a new specialized agent.
1230
+ \`{"type": "coder|researcher|tester|reviewer|architect|security", "name": "optional-name"}\`
1231
+ Agent types and when to use them:
1232
+ - \`coder\` — Write code, implement features, fix bugs
1233
+ - \`researcher\` — Find information, analyze documentation, investigate
1234
+ - \`tester\` — Write tests, run test suites, validate behavior
1235
+ - \`reviewer\` — Review code quality, security, best practices
1236
+ - \`architect\` — Design systems, plan architectures, evaluate trade-offs
1237
+ - \`security\` — Audit security, find vulnerabilities, recommend fixes
1238
+ - **ruflo__agent_status** — Check an agent's current state. \`{"agentId": "agent-xxx"}\`
1239
+ - **ruflo__agent_list** — List all active agents with their states.
1240
+ - **ruflo__agent_terminate** — Stop an agent. \`{"agentId": "agent-xxx"}\`
1241
+ - **ruflo__agent_health** — Health check across all agents.
1242
+ - **ruflo__agent_pool** — View the agent pool and available capacity.
1243
+
1244
+ ### Swarm Coordination:
1245
+ - **ruflo__swarm_init** — Initialize a multi-agent swarm.
1246
+ \`{"topology": "hierarchical|mesh|ring|star", "maxAgents": 8, "strategy": "balanced|specialized|adaptive"}\`
1247
+ - \`hierarchical\` — Coordinator + workers, best for structured tasks (anti-drift)
1248
+ - \`mesh\` — Peer-to-peer, best for collaborative work
1249
+ - \`ring\` — Sequential pipeline, best for ordered processing
1250
+ - \`star\` — Central hub, best for fan-out parallel work
1251
+ - **ruflo__swarm_status** — Get swarm health, topology, and agent states.
1252
+ - **ruflo__swarm_health** — Detailed health metrics for the swarm.
1253
+ - **ruflo__swarm_shutdown** — Tear down a swarm and all its agents.
1254
+
1255
+ ### Task Management:
1256
+ - **ruflo__task_create** — Create a tracked task.
1257
+ \`{"description": "what needs to be done", "priority": "low|normal|high|critical"}\`
1258
+ - **ruflo__task_status** — Check task progress. \`{"taskId": "task-xxx"}\`
1259
+ - **ruflo__task_list** — List all tasks with their statuses.
1260
+ - **ruflo__task_complete** — Mark a task as done. \`{"taskId": "task-xxx"}\`
1261
+ - **ruflo__task_update** — Update task details, status, or assignment.
1262
+ - **ruflo__task_cancel** — Cancel a task.
1263
+
1264
+ ### Workflow Orchestration:
1265
+ - **ruflo__workflow_create** — Define a multi-step workflow with dependencies.
1266
+ - **ruflo__workflow_execute** — Run a workflow. \`{"workflowId": "wf-xxx"}\`
1267
+ - **ruflo__workflow_status** — Check workflow progress.
1268
+ - **ruflo__workflow_template** — Use a pre-built workflow template.
1269
+ - **ruflo__workflow_pause** / **ruflo__workflow_resume** — Control workflow execution.
1270
+
1271
+ ### Hive-Mind (Distributed Consensus):
1272
+ - **ruflo__hive-mind_init** — Start distributed consensus system.
1273
+ - **ruflo__hive-mind_spawn** — Add an agent to the hive.
1274
+ - **ruflo__hive-mind_consensus** — Run consensus vote across agents.
1275
+ - **ruflo__hive-mind_broadcast** — Send message to all hive agents.
1276
+ - **ruflo__hive-mind_memory** — Access shared hive memory.
1277
+
1278
+ ### Coordination:
1279
+ - **ruflo__coordination_topology** — View/change coordination topology.
1280
+ - **ruflo__coordination_load_balance** — Distribute work across agents.
1281
+ - **ruflo__coordination_orchestrate** — Orchestrate complex multi-agent tasks.
1282
+ - **ruflo__coordination_sync** — Synchronize state across agents.
1283
+
1284
+ ### Session Management:
1285
+ - **ruflo__session_save** — Save current session state.
1286
+ - **ruflo__session_restore** — Restore a previous session.
1287
+ - **ruflo__session_list** — List available sessions.
1288
+
1289
+ ## Group 4: Memory & Knowledge (ruflo)
1290
+ Persistent memory, vector search, embeddings, and pattern storage. ${TOOL_GROUPS.memory.enabled ? "ACTIVE" : "DISABLED"}
1291
+
1292
+ ### Memory Operations:
1293
+ - **ruflo__memory_store** — Store data in persistent memory.
1294
+ \`{"key": "my-key", "value": "data to store", "namespace": "default", "tags": ["tag1"]}\`
1295
+ - **ruflo__memory_retrieve** — Get stored data by key. \`{"key": "my-key"}\`
1296
+ - **ruflo__memory_search** — Semantic vector search across stored memories.
1297
+ \`{"query": "what to search for", "limit": 5, "namespace": "default"}\`
1298
+ - **ruflo__memory_list** — List all stored keys in a namespace.
1299
+ - **ruflo__memory_delete** — Remove a stored memory.
1300
+ - **ruflo__memory_stats** — View memory usage statistics.
1301
+
1302
+ ### Embeddings:
1303
+ - **ruflo__embeddings_generate** — Generate vector embeddings for text.
1304
+ - **ruflo__embeddings_compare** — Compare semantic similarity of two texts.
1305
+ - **ruflo__embeddings_search** — Search embeddings database by similarity.
1306
+ - **ruflo__embeddings_neural** — Generate neural embeddings.
1307
+ - **ruflo__embeddings_hyperbolic** — Generate hyperbolic embeddings for hierarchical data.
1308
+
1309
+ ### AgentDB (Advanced Pattern Storage):
1310
+ - **ruflo__agentdb_pattern-store** — Store a learned pattern with metadata.
1311
+ \`{"pattern": "description", "category": "code|debug|architecture", "confidence": 0.9}\`
1312
+ - **ruflo__agentdb_pattern-search** — Search patterns by similarity.
1313
+ - **ruflo__agentdb_route** — Route a query to the most relevant stored pattern.
1314
+ - **ruflo__agentdb_feedback** — Provide feedback on a pattern (reinforcement learning).
1315
+ - **ruflo__agentdb_context-synthesize** — Synthesize context from multiple sources.
1316
+ - **ruflo__agentdb_semantic-route** — Semantic routing based on stored knowledge.
1317
+ - **ruflo__agentdb_consolidate** — Consolidate and deduplicate stored patterns.
1318
+ - **ruflo__agentdb_batch** — Batch operations on patterns.
1319
+ - **ruflo__agentdb_session-start** / **ruflo__agentdb_session-end** — Session tracking.
1320
+ - **ruflo__agentdb_hierarchical-store** / **ruflo__agentdb_hierarchical-recall** — Hierarchical memory.
1321
+
1322
+ ## Group 5: Dev Tools & Analysis (ruflo)
1323
+ Performance, system health, GitHub integration, code analysis, terminal. ${TOOL_GROUPS.devtools.enabled ? "ACTIVE" : "DISABLED"}
1324
+
1325
+ ### System & Performance:
1326
+ - **ruflo__system_status** — System health overview.
1327
+ - **ruflo__system_metrics** — Detailed performance metrics.
1328
+ - **ruflo__system_health** — Health check across all subsystems.
1329
+ - **ruflo__performance_report** — Generate performance report.
1330
+ - **ruflo__performance_bottleneck** — Identify performance bottlenecks.
1331
+ - **ruflo__performance_benchmark** — Run benchmarks.
1332
+ - **ruflo__performance_optimize** — Get optimization recommendations.
1333
+ - **ruflo__performance_profile** — Profile specific operations.
1334
+
1335
+ ### Code Analysis:
1336
+ - **ruflo__analyze_diff** — Analyze a code diff.
1337
+ - **ruflo__analyze_diff-risk** — Assess risk level of changes.
1338
+ - **ruflo__analyze_diff-classify** — Classify type of changes (feature, bugfix, refactor).
1339
+ - **ruflo__analyze_diff-reviewers** — Suggest code reviewers.
1340
+ - **ruflo__analyze_file-risk** — Assess risk of a specific file.
1341
+
1342
+ ### GitHub Integration:
1343
+ - **ruflo__github_repo_analyze** — Analyze a GitHub repository.
1344
+ \`{"repo": "owner/repo", "analysis_type": "code_quality|performance|security"}\`
1345
+ - **ruflo__github_pr_manage** — Manage pull requests (create, review, merge).
1346
+ - **ruflo__github_issue_track** — Track and manage issues.
1347
+ - **ruflo__github_workflow** — Manage GitHub Actions workflows.
1348
+ - **ruflo__github_metrics** — Repository metrics and insights.
1349
+
1350
+ ### Terminal Access:
1351
+ - **ruflo__terminal_create** — Create a terminal session.
1352
+ - **ruflo__terminal_execute** — Execute a command. \`{"command": "ls -la"}\`
1353
+ - **ruflo__terminal_list** — List active terminals.
1354
+ - **ruflo__terminal_history** — View command history.
1355
+
1356
+ ### Development Hooks:
1357
+ - **ruflo__hooks_pre-task** / **ruflo__hooks_post-task** — Task lifecycle hooks for learning.
1358
+ - **ruflo__hooks_pre-edit** / **ruflo__hooks_post-edit** — File edit hooks.
1359
+ - **ruflo__hooks_session-start** / **ruflo__hooks_session-end** — Session lifecycle.
1360
+ - **ruflo__hooks_worker-dispatch** — Dispatch background workers.
1361
+ Workers: \`optimize\`, \`audit\`, \`testgaps\`, \`document\`, \`map\`, \`deepdive\`, \`benchmark\`
1362
+ - **ruflo__hooks_model-route** — Route to optimal AI model for a task.
1363
+ - **ruflo__hooks_explain** — Explain a routing or intelligence decision.
1364
+
1365
+ ### Configuration:
1366
+ - **ruflo__config_get** / **ruflo__config_set** / **ruflo__config_list** — Manage settings.
1367
+
1368
+ ### Progress Tracking:
1369
+ - **ruflo__progress_check** — Check implementation progress.
1370
+ - **ruflo__progress_summary** — Summarize overall progress.
1371
+
1372
+ # Decision Framework — FOLLOW THIS EXACTLY
1373
+
1374
+ 1. **ANY factual question** → \`web_research(action='search')\` IMMEDIATELY. Do NOT answer from memory.
1375
+ 2. **"What is X?"** → \`web_research(action='search', query='X')\`
1376
+ 3. **"Compare X vs Y"** → \`web_research(action='compare', query='X vs Y', items=['X', 'Y'])\`
1377
+ 4. **"Is it true that..."** → \`web_research(action='fact_check', query='...')\`
1378
+ 5. **Complex research** → \`web_research(action='goap', query='...')\` — auto multi-step pipeline
1379
+ 6. **Internal docs/procedures** → \`search(query='...')\` first, then web_research if not found
1380
+ 7. **Code task** → \`ruvector__hooks_route\` + \`ruflo__agent_spawn\`
1381
+ 8. **Remember something** → \`ruflo__memory_store\` / \`ruflo__memory_search\`
1382
+ 9. **"What can you do?"** → \`guidance(topic='overview')\`
1383
+ 10. **Unknown** → \`guidance(topic='overview')\`
1384
+
1385
+ IMPORTANT: For questions 1-5, ALWAYS call web_research. Never say "I don't have access to search" — you DO.
1386
+
1387
+ # Execution Patterns
1388
+
1389
+ ### Simple Question
1390
+ \`search\` or \`web_research\` → synthesize → respond
1391
+
1392
+ ### Complex Research
1393
+ \`web_research(action='goap')\` → analyze → respond with citations
1394
+
1395
+ ### Code Implementation
1396
+ \`ruvector__hooks_route\` → \`ruflo__agent_spawn(coder)\` → track with \`ruflo__task_create\` → report
1397
+
1398
+ ### Multi-Agent Analysis
1399
+ \`ruflo__swarm_init(hierarchical)\` → spawn agents → coordinate → synthesize results
1400
+
1401
+ ### Learning & Memory
1402
+ \`ruflo__memory_search\` (check existing) → do work → \`ruflo__memory_store\` (save results) → \`ruvector__hooks_learn\`
1403
+
1404
+ # Parallel Execution
1405
+
1406
+ When multiple independent tools can help, call them ALL in parallel:
1407
+ - Search + web_research simultaneously
1408
+ - Spawn multiple agents at once (coder + tester + reviewer)
1409
+ - Run analysis + performance + security tools in parallel
1410
+ NEVER call tools sequentially when they could run in parallel.
1411
+
1412
+ # Response Rules
1413
+
1414
+ 1. **Call tools FIRST**, then present results conversationally — NEVER show raw JSON to the user
1415
+ 2. Use markdown: **bold** headers, bullet points, numbered steps, tables for comparisons
1416
+ 3. Synthesize tool results naturally — be a helpful colleague, not a data pipe
1417
+ 4. Cite sources when available from web_research results
1418
+ 5. If a tool fails, say so honestly and try an alternative approach
1419
+ 6. For complex tasks, briefly outline your plan before executing
1420
+ 7. After completing work, suggest relevant follow-up actions
1421
+ 8. When spawning agents, explain what each agent will do
1422
+
1423
+ # Never Expose to User
1424
+
1425
+ - Raw JSON, similarity scores, chunk IDs, internal IDs, task IDs
1426
+ - Tool names, function names, API endpoints, backend names
1427
+ - References to "MCP", "tool calls", "vectors", "embeddings", infrastructure
1428
+ - The prefixes "ruflo__" or "ruvector__" — just describe what you're doing naturally
1429
+ - Error stack traces — summarize errors in plain language`;
1430
+ }
1431
+
1432
+ // =============================================================================
1433
+ // AUTOPILOT MODE — Server-side auto-continue loop (ADR-037)
1434
+ // =============================================================================
1435
+
1436
+ const detailStore = new Map(); // detailToken → full tool result (TTL: 5min)
1437
+
1438
+ const AUTOPILOT_SYSTEM_PROMPT = `
1439
+ You are in AUTOPILOT MODE. You should:
1440
+ 1. Break complex tasks into steps and execute them using available tools
1441
+ 2. Call MULTIPLE tools in parallel when they are independent
1442
+ 3. After each tool result, analyze it and decide the next action
1443
+ 4. Continue until the task is complete — do NOT ask the user for confirmation
1444
+ 5. Use memory_search to find relevant patterns before starting
1445
+ 6. Summarize your progress at each step
1446
+ 7. When done, provide a final summary of everything accomplished
1447
+
1448
+ Parallel execution patterns:
1449
+ - Research: memory_search + hooks_route + agent_spawn(researcher) — all in parallel
1450
+ - Code: agent_spawn(coder) + agent_spawn(tester) — parallel, then review
1451
+ - Analysis: search multiple sources in parallel → synthesize → report
1452
+ - Security: security_scan + hooks_route(audit) + memory_search(CVEs) — parallel
1453
+ `;
1454
+
1455
+ const AUTOPILOT_BLOCKED_PATTERNS = [
1456
+ /^deploy_/,
1457
+ /^security_delete/,
1458
+ /^browser_fill$/,
1459
+ /^browser_click$/,
1460
+ /terminal_execute/,
1461
+ ];
1462
+
1463
+ function isBlockedTool(name) {
1464
+ return AUTOPILOT_BLOCKED_PATTERNS.some(p => p.test(name));
1465
+ }
1466
+
1467
+ function sendAutopilotEvent(res, data) {
1468
+ res.write(`data: ${JSON.stringify(data)}\n\n`);
1469
+ }
1470
+
1471
+ function safeParseArgs(args) {
1472
+ if (typeof args === 'object' && args !== null) return args;
1473
+ try { return JSON.parse(args || '{}'); } catch { return {}; }
1474
+ }
1475
+
1476
+ function autopilotSleep(ms) {
1477
+ return new Promise(resolve => setTimeout(resolve, ms));
1478
+ }
1479
+
1480
+ async function handleAutopilot(req, res, provider, body) {
1481
+ const maxSteps = Math.min(parseInt(req.headers['x-autopilot-max-steps'] || '20', 10), 50);
1482
+ const cooldownMs = parseInt(process.env.AUTOPILOT_COOLDOWN || '500', 10);
1483
+ const stepTimeoutMs = parseInt(process.env.AUTOPILOT_STEP_TIMEOUT || '30000', 10);
1484
+
1485
+ // SSE setup
1486
+ res.setHeader('Content-Type', 'text/event-stream');
1487
+ res.setHeader('Cache-Control', 'no-cache');
1488
+ res.setHeader('Connection', 'keep-alive');
1489
+ res.setHeader('X-Accel-Buffering', 'no');
1490
+
1491
+ let messages = [...body.messages];
1492
+ let step = 0;
1493
+ let aborted = false;
1494
+ let totalTasks = 0;
1495
+ const startTime = Date.now();
1496
+
1497
+ req.on('close', () => { aborted = true; });
1498
+
1499
+ sendAutopilotEvent(res, { type: 'autopilot_start', maxSteps });
1500
+
1501
+ // Get the tools list for the AI provider (OpenAI function calling format)
1502
+ // Limit to BUILTIN_TOOLS + top external tools to avoid overwhelming the model
1503
+ // (Gemini struggles with 100+ tool definitions)
1504
+ const MAX_AUTOPILOT_TOOLS = 30;
1505
+ const externalTools = getActiveTools();
1506
+ // Prioritize tools from enabled high-value groups
1507
+ const priorityPrefixes = ['ruvector__hooks', 'ruvector__memory', 'ruflo__memory', 'ruflo__agent'];
1508
+ const prioritized = externalTools
1509
+ .sort((a, b) => {
1510
+ const aP = priorityPrefixes.some(p => a.name.startsWith(p)) ? 0 : 1;
1511
+ const bP = priorityPrefixes.some(p => b.name.startsWith(p)) ? 0 : 1;
1512
+ return aP - bP;
1513
+ })
1514
+ .slice(0, MAX_AUTOPILOT_TOOLS - BUILTIN_TOOLS.length);
1515
+ const allTools = [...BUILTIN_TOOLS, ...prioritized];
1516
+ const toolDefs = allTools.map(t => ({
1517
+ type: 'function',
1518
+ function: {
1519
+ name: t.name,
1520
+ description: t.description || '',
1521
+ parameters: t.inputSchema || { type: 'object', properties: {} },
1522
+ },
1523
+ }));
1524
+ console.log(`[autopilot] ${allTools.length} tools (${BUILTIN_TOOLS.length} builtin + ${prioritized.length} external)`);
1525
+
1526
+ while (step < maxSteps && !aborted) {
1527
+ // 1. Call upstream AI provider (non-streaming for tool call parsing)
1528
+ const apiKey = provider.getKey();
1529
+ let aiResult;
1530
+ try {
1531
+ const aiResponse = await fetch(provider.baseURL, {
1532
+ method: 'POST',
1533
+ headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${apiKey}` },
1534
+ body: JSON.stringify({
1535
+ ...body,
1536
+ messages,
1537
+ stream: false,
1538
+ tools: toolDefs.length > 0 ? toolDefs : undefined,
1539
+ }),
1540
+ signal: AbortSignal.timeout(stepTimeoutMs),
1541
+ });
1542
+ aiResult = await aiResponse.json();
1543
+ } catch (err) {
1544
+ sendAutopilotEvent(res, { type: 'autopilot_error', error: `AI call failed: ${err.message}` });
1545
+ break;
1546
+ }
1547
+
1548
+ const choice = aiResult.choices?.[0];
1549
+ if (!choice) {
1550
+ sendAutopilotEvent(res, { type: 'autopilot_error', error: 'No response from AI' });
1551
+ break;
1552
+ }
1553
+
1554
+ // 2. Check for tool calls
1555
+ const toolCalls = choice.message?.tool_calls;
1556
+
1557
+ if (!toolCalls || toolCalls.length === 0) {
1558
+ // Final text response — send it
1559
+ sendAutopilotEvent(res, { type: 'autopilot_text', content: choice.message?.content || '' });
1560
+ break;
1561
+ }
1562
+
1563
+ // 3. Execute ALL tool calls in parallel
1564
+ step++;
1565
+ const groupId = `g${step}`;
1566
+ const taskEvents = toolCalls.map((tc, i) => ({
1567
+ taskId: `t${totalTasks + i + 1}`,
1568
+ tool: tc.function.name,
1569
+ args: safeParseArgs(tc.function.arguments),
1570
+ status: 'running',
1571
+ }));
1572
+ totalTasks += taskEvents.length;
1573
+
1574
+ // If the AI also included text content, stream it before tools
1575
+ if (choice.message?.content) {
1576
+ sendAutopilotEvent(res, { type: 'autopilot_text', content: choice.message.content });
1577
+ }
1578
+
1579
+ // Stream group start
1580
+ sendAutopilotEvent(res, { type: 'task_group_start', groupId, step, tasks: taskEvents });
1581
+
1582
+ // Append assistant message to conversation
1583
+ messages.push(choice.message);
1584
+
1585
+ // Execute tools in parallel
1586
+ const groupStart = Date.now();
1587
+ const results = await Promise.allSettled(
1588
+ toolCalls.map(async (tc, i) => {
1589
+ const taskId = taskEvents[i].taskId;
1590
+ const toolName = tc.function.name;
1591
+ const toolArgs = safeParseArgs(tc.function.arguments);
1592
+ const taskStart = Date.now();
1593
+
1594
+ // Check blocklist
1595
+ if (isBlockedTool(toolName)) {
1596
+ sendAutopilotEvent(res, {
1597
+ type: 'task_update', taskId, status: 'blocked',
1598
+ summary: `${toolName} requires confirmation`,
1599
+ duration: Date.now() - taskStart,
1600
+ });
1601
+ return { toolCallId: tc.id, blocked: true, toolName };
1602
+ }
1603
+
1604
+ try {
1605
+ const result = await executeTool(toolName, toolArgs);
1606
+ const resultStr = typeof result === 'string' ? result : JSON.stringify(result, null, 2);
1607
+
1608
+ // Store full detail, generate token for lazy loading
1609
+ const detailToken = `dt_${taskId}_${Date.now()}`;
1610
+ detailStore.set(detailToken, resultStr);
1611
+
1612
+ // Stream task completion with summary only
1613
+ const summary = resultStr.length > 120
1614
+ ? resultStr.substring(0, 120).replace(/\n/g, ' ') + '...'
1615
+ : resultStr.replace(/\n/g, ' ');
1616
+
1617
+ sendAutopilotEvent(res, {
1618
+ type: 'task_update', taskId, status: 'completed',
1619
+ summary, duration: Date.now() - taskStart, detailToken,
1620
+ });
1621
+
1622
+ return { toolCallId: tc.id, content: resultStr };
1623
+ } catch (err) {
1624
+ sendAutopilotEvent(res, {
1625
+ type: 'task_update', taskId, status: 'failed',
1626
+ summary: err.message, duration: Date.now() - taskStart,
1627
+ });
1628
+ return { toolCallId: tc.id, content: `Error: ${err.message}` };
1629
+ }
1630
+ })
1631
+ );
1632
+
1633
+ // Stream group end
1634
+ sendAutopilotEvent(res, { type: 'task_group_end', groupId, step, duration: Date.now() - groupStart });
1635
+
1636
+ // Check if any tools were blocked — pause autopilot
1637
+ const blockedResults = results
1638
+ .filter(r => r.status === 'fulfilled' && r.value.blocked)
1639
+ .map(r => r.value);
1640
+ if (blockedResults.length > 0) {
1641
+ sendAutopilotEvent(res, {
1642
+ type: 'autopilot_paused',
1643
+ reason: 'blocked_tools',
1644
+ tools: blockedResults.map(b => b.toolName),
1645
+ });
1646
+ break;
1647
+ }
1648
+
1649
+ // Append tool results to messages
1650
+ for (const r of results) {
1651
+ if (r.status === 'fulfilled' && !r.value.blocked) {
1652
+ messages.push({
1653
+ role: 'tool',
1654
+ tool_call_id: r.value.toolCallId,
1655
+ content: r.value.content,
1656
+ });
1657
+ }
1658
+ }
1659
+
1660
+ // Cooldown to prevent runaway
1661
+ await autopilotSleep(cooldownMs);
1662
+ }
1663
+
1664
+ if (step >= maxSteps && !aborted) {
1665
+ sendAutopilotEvent(res, {
1666
+ type: 'autopilot_text',
1667
+ content: `\n⚠️ Autopilot reached max steps (${maxSteps}). Stopping.\n`,
1668
+ });
1669
+ }
1670
+
1671
+ sendAutopilotEvent(res, {
1672
+ type: 'autopilot_end',
1673
+ totalSteps: step,
1674
+ totalTasks,
1675
+ duration: Date.now() - startTime,
1676
+ });
1677
+
1678
+ res.write('data: [DONE]\n\n');
1679
+ res.end();
1680
+
1681
+ // Clean up detail store after 5 minutes
1682
+ const detailTTL = parseInt(process.env.AUTOPILOT_DETAIL_TTL || '300000', 10);
1683
+ setTimeout(() => {
1684
+ for (const [key] of detailStore) {
1685
+ if (key.startsWith('dt_')) detailStore.delete(key);
1686
+ }
1687
+ }, detailTTL);
1688
+ }
1689
+
1690
+ // Lazy detail loading endpoint
1691
+ app.get('/autopilot/detail/:token', (req, res) => {
1692
+ const content = detailStore.get(req.params.token);
1693
+ if (content) {
1694
+ res.json({ content });
1695
+ } else {
1696
+ res.status(404).json({ error: 'Detail expired or not found' });
1697
+ }
1698
+ });
1699
+
1700
+ // =============================================================================
1701
+ // CHAT COMPLETIONS PROXY
1702
+ // =============================================================================
1703
+
1704
+ app.post("/chat/completions", async (req, res) => {
1705
+ const model = req.body?.model;
1706
+ const providerName = resolveProvider(model);
1707
+ const provider = PROVIDER_ROUTES[providerName];
1708
+ const apiKey = provider.getKey();
1709
+
1710
+ if (!apiKey) return res.status(401).json({ error: { message: `No API key for provider: ${providerName}` } });
1711
+
1712
+ // Inject comprehensive system prompt as the first message
1713
+ const body = { ...req.body };
1714
+ if (body.messages && Array.isArray(body.messages)) {
1715
+ let systemPrompt = buildSystemPrompt();
1716
+ // Add autopilot instructions if autopilot mode is active
1717
+ const isAutopilot = req.headers['x-autopilot'] === 'true';
1718
+ if (isAutopilot) {
1719
+ systemPrompt = AUTOPILOT_SYSTEM_PROMPT + '\n\n' + systemPrompt;
1720
+ }
1721
+ // Prepend our system prompt before any existing messages
1722
+ const hasSystemMsg = body.messages[0]?.role === "system";
1723
+ if (hasSystemMsg) {
1724
+ // Merge with existing system message
1725
+ body.messages = [
1726
+ { role: "system", content: systemPrompt + "\n\n" + body.messages[0].content },
1727
+ ...body.messages.slice(1),
1728
+ ];
1729
+ } else {
1730
+ body.messages = [{ role: "system", content: systemPrompt }, ...body.messages];
1731
+ }
1732
+ }
1733
+
1734
+ // Route to autopilot handler if x-autopilot header is set
1735
+ if (req.headers['x-autopilot'] === 'true') {
1736
+ return handleAutopilot(req, res, provider, body);
1737
+ }
1738
+
1739
+ try {
1740
+ const upstream = await fetch(provider.baseURL, {
1741
+ method: "POST",
1742
+ headers: { "Content-Type": "application/json", Authorization: `Bearer ${apiKey}` },
1743
+ body: JSON.stringify(body),
1744
+ });
1745
+
1746
+ if (!upstream.ok) {
1747
+ const errBody = await upstream.text();
1748
+ console.error(`Proxy error [${providerName}/${model}]: ${upstream.status} ${errBody.substring(0, 200)}`);
1749
+ // Normalize all upstream errors into OpenAI-compatible format so the
1750
+ // Chat UI's OpenAI SDK can parse them instead of "400 (no body)".
1751
+ let errorMessage = `Upstream ${providerName} error (${upstream.status})`;
1752
+ try {
1753
+ const parsed = JSON.parse(errBody);
1754
+ // Gemini returns [{"error": {"message": "..."}}]
1755
+ if (Array.isArray(parsed) && parsed[0]?.error?.message) {
1756
+ errorMessage = parsed[0].error.message;
1757
+ // OpenAI/OpenRouter return {"error": {"message": "..."}}
1758
+ } else if (parsed.error?.message) {
1759
+ errorMessage = parsed.error.message;
1760
+ }
1761
+ } catch {}
1762
+ return res.status(upstream.status).json({
1763
+ error: { message: errorMessage, type: "upstream_error", code: upstream.status },
1764
+ });
1765
+ }
1766
+
1767
+ res.setHeader("Content-Type", upstream.headers.get("content-type") || "application/json");
1768
+
1769
+ if (req.body?.stream && upstream.body) {
1770
+ const reader = upstream.body.getReader();
1771
+ const decoder = new TextDecoder();
1772
+ try {
1773
+ while (true) {
1774
+ const { done, value } = await reader.read();
1775
+ if (done) break;
1776
+ res.write(decoder.decode(value, { stream: true }));
1777
+ }
1778
+ } catch (e) { /* stream closed */ }
1779
+ finally { res.end(); }
1780
+ } else {
1781
+ res.send(await upstream.text());
1782
+ }
1783
+ } catch (err) {
1784
+ console.error(`Proxy error [${providerName}/${model}]:`, err.message);
1785
+ res.status(502).json({ error: { message: `Upstream error: ${err.message}` } });
1786
+ }
1787
+ });
1788
+
1789
+ // =============================================================================
1790
+ // MODELS & HEALTH
1791
+ // =============================================================================
1792
+
1793
+ const KNOWN_MODELS = [
1794
+ "gemini-2.5-pro", "gemini-2.5-flash",
1795
+ "gpt-4.1", "gpt-4.1-mini", "gpt-4o", "gpt-4o-mini",
1796
+ "o3-mini", "o1-mini",
1797
+ ];
1798
+
1799
+ app.get("/models", (_, res) => {
1800
+ res.json({ object: "list", data: KNOWN_MODELS.map(id => ({
1801
+ id,
1802
+ object: "model",
1803
+ owned_by: "system",
1804
+ providers: [{ supports_tools: true }],
1805
+ })) });
1806
+ });
1807
+
1808
+ app.get("/health", (_, res) => {
1809
+ const backends = {};
1810
+ for (const [name, client] of mcpBackends) {
1811
+ backends[name] = { ready: client.ready, tools: client.tools.length };
1812
+ }
1813
+ const activeTools = getActiveTools();
1814
+ const groups = {};
1815
+ for (const [name, g] of Object.entries(TOOL_GROUPS)) {
1816
+ groups[name] = { enabled: g.enabled, source: g.source };
1817
+ }
1818
+ res.json({
1819
+ status: "ok", service: "mcp-bridge", version: "2.0.0",
1820
+ tools: { builtin: BUILTIN_TOOLS.length, external: activeTools.length, total: BUILTIN_TOOLS.length + activeTools.length },
1821
+ groups, backends,
1822
+ });
1823
+ });
1824
+
1825
+ // GET /groups — list tool groups and their status
1826
+ app.get("/groups", (_, res) => {
1827
+ const activeTools = getActiveTools();
1828
+ const result = {};
1829
+ for (const [name, g] of Object.entries(TOOL_GROUPS)) {
1830
+ const tools = name === "core" ? BUILTIN_TOOLS :
1831
+ activeTools.filter(t => {
1832
+ if (g.source !== t._backend) return false;
1833
+ if (!g.prefixes) return true;
1834
+ return g.prefixes.some(p => t._originalName.startsWith(p));
1835
+ });
1836
+ result[name] = {
1837
+ enabled: g.enabled,
1838
+ description: g.description,
1839
+ tools: tools.length,
1840
+ toolNames: tools.map(t => t.name).slice(0, 10),
1841
+ };
1842
+ }
1843
+ res.json(result);
1844
+ });
1845
+
1846
+ // =============================================================================
1847
+ // STARTUP
1848
+ // =============================================================================
1849
+
1850
+ async function main() {
1851
+ app.listen(PORT, () => {
1852
+ console.log(`MCP Bridge v2.0.0 on port ${PORT}`);
1853
+ const enabled = Object.entries(TOOL_GROUPS).filter(([, g]) => g.enabled).map(([n]) => n);
1854
+ console.log(`Active groups: ${enabled.join(", ")}`);
1855
+ });
1856
+
1857
+ const anyBackendNeeded = BACKEND_DEFS.some(isBackendNeeded);
1858
+ if (anyBackendNeeded) {
1859
+ console.log("Initializing MCP backends...");
1860
+ await initBackends();
1861
+ }
1862
+ }
1863
+
1864
+ main().catch(err => { console.error("Fatal:", err); process.exit(1); });