ruflo 3.10.36 → 3.10.37

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 (498) hide show
  1. package/README.md +416 -416
  2. package/bin/ruflo.js +77 -77
  3. package/package.json +113 -113
  4. package/src/chat-ui/Dockerfile +25 -25
  5. package/src/chat-ui/patch-mcp-url-safety.sh +28 -28
  6. package/src/config/config.example.json +76 -76
  7. package/src/mcp-bridge/Dockerfile +45 -45
  8. package/src/mcp-bridge/index.js +1692 -1692
  9. package/src/mcp-bridge/mcp-stdio-kernel.js +159 -159
  10. package/src/mcp-bridge/package.json +17 -17
  11. package/src/mcp-bridge/test-harness.js +470 -470
  12. package/src/nginx/Dockerfile +10 -10
  13. package/src/nginx/nginx.conf +67 -67
  14. package/src/nginx/static/favicon-dark.svg +4 -4
  15. package/src/nginx/static/favicon.svg +4 -4
  16. package/src/nginx/static/icon.svg +5 -5
  17. package/src/nginx/static/logo.svg +9 -9
  18. package/src/nginx/static/manifest.json +22 -22
  19. package/src/nginx/static/welcome.js +184 -184
  20. package/src/ruvocal/.claude/skills/add-model-descriptions/SKILL.md +73 -73
  21. package/src/ruvocal/.devcontainer/Dockerfile +9 -9
  22. package/src/ruvocal/.devcontainer/devcontainer.json +36 -36
  23. package/src/ruvocal/.dockerignore +16 -16
  24. package/src/ruvocal/.eslintignore +13 -13
  25. package/src/ruvocal/.eslintrc.cjs +45 -45
  26. package/src/ruvocal/.gcloudignore +18 -18
  27. package/src/ruvocal/.github/ISSUE_TEMPLATE/bug-report--chat-ui-.md +43 -43
  28. package/src/ruvocal/.github/ISSUE_TEMPLATE/config-support.md +9 -9
  29. package/src/ruvocal/.github/ISSUE_TEMPLATE/feature-request--chat-ui-.md +17 -17
  30. package/src/ruvocal/.github/ISSUE_TEMPLATE/huggingchat.md +11 -11
  31. package/src/ruvocal/.github/release.yml +16 -16
  32. package/src/ruvocal/.github/workflows/build-docs.yml +18 -18
  33. package/src/ruvocal/.github/workflows/build-image.yml +142 -142
  34. package/src/ruvocal/.github/workflows/build-pr-docs.yml +20 -20
  35. package/src/ruvocal/.github/workflows/deploy-dev.yml +63 -63
  36. package/src/ruvocal/.github/workflows/deploy-prod.yml +78 -78
  37. package/src/ruvocal/.github/workflows/lint-and-test.yml +84 -84
  38. package/src/ruvocal/.github/workflows/slugify.yaml +72 -72
  39. package/src/ruvocal/.github/workflows/trufflehog.yml +17 -17
  40. package/src/ruvocal/.github/workflows/upload-pr-documentation.yml +16 -16
  41. package/src/ruvocal/.husky/lint-stage-config.js +4 -4
  42. package/src/ruvocal/.husky/pre-commit +2 -2
  43. package/src/ruvocal/.prettierignore +14 -14
  44. package/src/ruvocal/.prettierrc +7 -7
  45. package/src/ruvocal/CLAUDE.md +126 -126
  46. package/src/ruvocal/Dockerfile +96 -96
  47. package/src/ruvocal/LICENSE +202 -202
  48. package/src/ruvocal/PRIVACY.md +41 -41
  49. package/src/ruvocal/README.md +164 -164
  50. package/src/ruvocal/chart/Chart.yaml +5 -5
  51. package/src/ruvocal/chart/env/dev.yaml +260 -260
  52. package/src/ruvocal/chart/env/prod.yaml +273 -273
  53. package/src/ruvocal/chart/templates/_helpers.tpl +22 -22
  54. package/src/ruvocal/chart/templates/config.yaml +10 -10
  55. package/src/ruvocal/chart/templates/deployment.yaml +81 -81
  56. package/src/ruvocal/chart/templates/hpa.yaml +45 -45
  57. package/src/ruvocal/chart/templates/infisical.yaml +24 -24
  58. package/src/ruvocal/chart/templates/ingress-internal.yaml +32 -32
  59. package/src/ruvocal/chart/templates/ingress.yaml +32 -32
  60. package/src/ruvocal/chart/templates/network-policy.yaml +36 -36
  61. package/src/ruvocal/chart/templates/service-account.yaml +13 -13
  62. package/src/ruvocal/chart/templates/service-monitor.yaml +17 -17
  63. package/src/ruvocal/chart/templates/service.yaml +21 -21
  64. package/src/ruvocal/chart/values.yaml +73 -73
  65. package/src/ruvocal/cloudbuild.yaml +68 -68
  66. package/src/ruvocal/config/branding.env.example +19 -19
  67. package/src/ruvocal/docker-compose.yml +21 -21
  68. package/src/ruvocal/docs/adr/ADR-029-HUGGINGFACE-CHAT-UI-CLOUD-RUN.md +1236 -1236
  69. package/src/ruvocal/docs/adr/ADR-033-RUVECTOR-RUFLO-MCP-INTEGRATION.md +111 -111
  70. package/src/ruvocal/docs/adr/ADR-034-OPTIONAL-MCP-BACKENDS.md +117 -117
  71. package/src/ruvocal/docs/adr/ADR-035-MCP-TOOL-GROUPS.md +186 -186
  72. package/src/ruvocal/docs/adr/ADR-037-AUTOPILOT-CHAT-MODE.md +1500 -1500
  73. package/src/ruvocal/docs/adr/ADR-038-RUVOCAL-FORK.md +286 -286
  74. package/src/ruvocal/docs/source/_toctree.yml +30 -30
  75. package/src/ruvocal/docs/source/configuration/common-issues.md +38 -38
  76. package/src/ruvocal/docs/source/configuration/llm-router.md +105 -105
  77. package/src/ruvocal/docs/source/configuration/mcp-tools.md +84 -84
  78. package/src/ruvocal/docs/source/configuration/metrics.md +9 -9
  79. package/src/ruvocal/docs/source/configuration/open-id.md +57 -57
  80. package/src/ruvocal/docs/source/configuration/overview.md +89 -89
  81. package/src/ruvocal/docs/source/configuration/theming.md +20 -20
  82. package/src/ruvocal/docs/source/developing/architecture.md +48 -48
  83. package/src/ruvocal/docs/source/index.md +53 -53
  84. package/src/ruvocal/docs/source/installation/docker.md +43 -43
  85. package/src/ruvocal/docs/source/installation/helm.md +43 -43
  86. package/src/ruvocal/docs/source/installation/local.md +62 -62
  87. package/src/ruvocal/entrypoint.sh +18 -18
  88. package/src/ruvocal/mcp-bridge/Dockerfile +45 -45
  89. package/src/ruvocal/mcp-bridge/cloudbuild.yaml +49 -49
  90. package/src/ruvocal/mcp-bridge/index.js +1902 -1902
  91. package/src/ruvocal/mcp-bridge/mcp-stdio-kernel.js +159 -159
  92. package/src/ruvocal/mcp-bridge/package-lock.json +762 -762
  93. package/src/ruvocal/mcp-bridge/package.json +17 -17
  94. package/src/ruvocal/mcp-bridge/test-harness.js +470 -470
  95. package/src/ruvocal/package-lock.json +11741 -11741
  96. package/src/ruvocal/package.json +121 -121
  97. package/src/ruvocal/postcss.config.js +6 -6
  98. package/src/ruvocal/rvf.manifest.json +204 -204
  99. package/src/ruvocal/scripts/config.ts +64 -64
  100. package/src/ruvocal/scripts/generate-welcome.mjs +181 -181
  101. package/src/ruvocal/scripts/populate.ts +288 -288
  102. package/src/ruvocal/scripts/samples.txt +194 -194
  103. package/src/ruvocal/scripts/setups/vitest-setup-server.ts +44 -44
  104. package/src/ruvocal/scripts/updateLocalEnv.ts +48 -48
  105. package/src/ruvocal/src/ambient.d.ts +7 -7
  106. package/src/ruvocal/src/app.d.ts +29 -29
  107. package/src/ruvocal/src/app.html +53 -53
  108. package/src/ruvocal/src/hooks.server.ts +32 -32
  109. package/src/ruvocal/src/hooks.ts +6 -6
  110. package/src/ruvocal/src/lib/APIClient.ts +148 -148
  111. package/src/ruvocal/src/lib/actions/clickOutside.ts +18 -18
  112. package/src/ruvocal/src/lib/actions/snapScrollToBottom.ts +346 -346
  113. package/src/ruvocal/src/lib/buildPrompt.ts +33 -33
  114. package/src/ruvocal/src/lib/components/AnnouncementBanner.svelte +20 -20
  115. package/src/ruvocal/src/lib/components/BackgroundGenerationPoller.svelte +168 -168
  116. package/src/ruvocal/src/lib/components/CodeBlock.svelte +73 -73
  117. package/src/ruvocal/src/lib/components/CopyToClipBoardBtn.svelte +92 -92
  118. package/src/ruvocal/src/lib/components/DeleteConversationModal.svelte +75 -75
  119. package/src/ruvocal/src/lib/components/EditConversationModal.svelte +100 -100
  120. package/src/ruvocal/src/lib/components/ExpandNavigation.svelte +22 -22
  121. package/src/ruvocal/src/lib/components/FoundationBackground.svelte +242 -242
  122. package/src/ruvocal/src/lib/components/HoverTooltip.svelte +44 -44
  123. package/src/ruvocal/src/lib/components/HtmlPreviewModal.svelte +143 -143
  124. package/src/ruvocal/src/lib/components/InfiniteScroll.svelte +50 -50
  125. package/src/ruvocal/src/lib/components/MobileNav.svelte +300 -300
  126. package/src/ruvocal/src/lib/components/Modal.svelte +115 -115
  127. package/src/ruvocal/src/lib/components/ModelCardMetadata.svelte +71 -71
  128. package/src/ruvocal/src/lib/components/NavConversationItem.svelte +151 -151
  129. package/src/ruvocal/src/lib/components/NavMenu.svelte +313 -313
  130. package/src/ruvocal/src/lib/components/Pagination.svelte +97 -97
  131. package/src/ruvocal/src/lib/components/PaginationArrow.svelte +27 -27
  132. package/src/ruvocal/src/lib/components/Portal.svelte +24 -24
  133. package/src/ruvocal/src/lib/components/RetryBtn.svelte +18 -18
  134. package/src/ruvocal/src/lib/components/RuFloUniverse.svelte +185 -185
  135. package/src/ruvocal/src/lib/components/RufloHelpModal.svelte +411 -411
  136. package/src/ruvocal/src/lib/components/ScrollToBottomBtn.svelte +47 -47
  137. package/src/ruvocal/src/lib/components/ScrollToPreviousBtn.svelte +77 -77
  138. package/src/ruvocal/src/lib/components/ShareConversationModal.svelte +182 -182
  139. package/src/ruvocal/src/lib/components/StopGeneratingBtn.svelte +69 -69
  140. package/src/ruvocal/src/lib/components/SubscribeModal.svelte +87 -87
  141. package/src/ruvocal/src/lib/components/Switch.svelte +36 -36
  142. package/src/ruvocal/src/lib/components/SystemPromptModal.svelte +44 -44
  143. package/src/ruvocal/src/lib/components/Toast.svelte +27 -27
  144. package/src/ruvocal/src/lib/components/Tooltip.svelte +30 -30
  145. package/src/ruvocal/src/lib/components/WelcomeModal.svelte +46 -46
  146. package/src/ruvocal/src/lib/components/chat/Alternatives.svelte +77 -77
  147. package/src/ruvocal/src/lib/components/chat/BlockWrapper.svelte +72 -72
  148. package/src/ruvocal/src/lib/components/chat/ChatInput.svelte +490 -490
  149. package/src/ruvocal/src/lib/components/chat/ChatIntroduction.svelte +123 -123
  150. package/src/ruvocal/src/lib/components/chat/ChatMessage.svelte +548 -548
  151. package/src/ruvocal/src/lib/components/chat/ChatWindow.svelte +1057 -1057
  152. package/src/ruvocal/src/lib/components/chat/FileDropzone.svelte +92 -92
  153. package/src/ruvocal/src/lib/components/chat/ImageLightbox.svelte +66 -66
  154. package/src/ruvocal/src/lib/components/chat/MarkdownBlock.svelte +23 -23
  155. package/src/ruvocal/src/lib/components/chat/MarkdownRenderer.svelte +69 -69
  156. package/src/ruvocal/src/lib/components/chat/MarkdownRenderer.svelte.test.ts +58 -58
  157. package/src/ruvocal/src/lib/components/chat/MessageAvatar.svelte +103 -103
  158. package/src/ruvocal/src/lib/components/chat/ModelSwitch.svelte +64 -64
  159. package/src/ruvocal/src/lib/components/chat/OpenReasoningResults.svelte +81 -81
  160. package/src/ruvocal/src/lib/components/chat/TaskGroup.svelte +88 -88
  161. package/src/ruvocal/src/lib/components/chat/ToolUpdate.svelte +273 -273
  162. package/src/ruvocal/src/lib/components/chat/UploadedFile.svelte +253 -253
  163. package/src/ruvocal/src/lib/components/chat/UrlFetchModal.svelte +203 -203
  164. package/src/ruvocal/src/lib/components/chat/VoiceRecorder.svelte +214 -214
  165. package/src/ruvocal/src/lib/components/icons/IconBurger.svelte +20 -20
  166. package/src/ruvocal/src/lib/components/icons/IconCheap.svelte +20 -20
  167. package/src/ruvocal/src/lib/components/icons/IconChevron.svelte +24 -24
  168. package/src/ruvocal/src/lib/components/icons/IconDazzled.svelte +40 -40
  169. package/src/ruvocal/src/lib/components/icons/IconFast.svelte +20 -20
  170. package/src/ruvocal/src/lib/components/icons/IconLoading.svelte +22 -22
  171. package/src/ruvocal/src/lib/components/icons/IconMCP.svelte +28 -28
  172. package/src/ruvocal/src/lib/components/icons/IconMoon.svelte +21 -21
  173. package/src/ruvocal/src/lib/components/icons/IconNew.svelte +20 -20
  174. package/src/ruvocal/src/lib/components/icons/IconOmni.svelte +90 -90
  175. package/src/ruvocal/src/lib/components/icons/IconPaperclip.svelte +24 -24
  176. package/src/ruvocal/src/lib/components/icons/IconPro.svelte +37 -37
  177. package/src/ruvocal/src/lib/components/icons/IconShare.svelte +21 -21
  178. package/src/ruvocal/src/lib/components/icons/IconSun.svelte +93 -93
  179. package/src/ruvocal/src/lib/components/icons/Logo.svelte +68 -68
  180. package/src/ruvocal/src/lib/components/icons/LogoHuggingFaceBorderless.svelte +54 -54
  181. package/src/ruvocal/src/lib/components/mcp/AddServerForm.svelte +250 -250
  182. package/src/ruvocal/src/lib/components/mcp/MCPServerManager.svelte +185 -185
  183. package/src/ruvocal/src/lib/components/mcp/ServerCard.svelte +203 -203
  184. package/src/ruvocal/src/lib/components/players/AudioPlayer.svelte +82 -82
  185. package/src/ruvocal/src/lib/components/voice/AudioWaveform.svelte +96 -96
  186. package/src/ruvocal/src/lib/components/wasm/GalleryPanel.svelte +357 -357
  187. package/src/ruvocal/src/lib/constants/mcpExamples.ts +114 -114
  188. package/src/ruvocal/src/lib/constants/mime.ts +11 -11
  189. package/src/ruvocal/src/lib/constants/pagination.ts +1 -1
  190. package/src/ruvocal/src/lib/constants/publicSepToken.ts +1 -1
  191. package/src/ruvocal/src/lib/constants/routerExamples.ts +133 -133
  192. package/src/ruvocal/src/lib/constants/rvagentPresets.ts +206 -206
  193. package/src/ruvocal/src/lib/createShareLink.ts +27 -27
  194. package/src/ruvocal/src/lib/jobs/refresh-conversation-stats.ts +297 -297
  195. package/src/ruvocal/src/lib/migrations/lock.ts +56 -56
  196. package/src/ruvocal/src/lib/migrations/migrations.spec.ts +74 -74
  197. package/src/ruvocal/src/lib/migrations/migrations.ts +109 -109
  198. package/src/ruvocal/src/lib/migrations/routines/01-update-search-assistants.ts +50 -50
  199. package/src/ruvocal/src/lib/migrations/routines/02-update-assistants-models.ts +48 -48
  200. package/src/ruvocal/src/lib/migrations/routines/04-update-message-updates.ts +151 -151
  201. package/src/ruvocal/src/lib/migrations/routines/05-update-message-files.ts +56 -56
  202. package/src/ruvocal/src/lib/migrations/routines/06-trim-message-updates.ts +56 -56
  203. package/src/ruvocal/src/lib/migrations/routines/08-update-featured-to-review.ts +32 -32
  204. package/src/ruvocal/src/lib/migrations/routines/09-delete-empty-conversations.spec.ts +214 -214
  205. package/src/ruvocal/src/lib/migrations/routines/09-delete-empty-conversations.ts +88 -88
  206. package/src/ruvocal/src/lib/migrations/routines/10-update-reports-assistantid.ts +29 -29
  207. package/src/ruvocal/src/lib/migrations/routines/index.ts +15 -15
  208. package/src/ruvocal/src/lib/server/__tests__/conversation-stop-generating.spec.ts +103 -103
  209. package/src/ruvocal/src/lib/server/abortRegistry.ts +57 -57
  210. package/src/ruvocal/src/lib/server/abortedGenerations.ts +43 -43
  211. package/src/ruvocal/src/lib/server/adminToken.ts +62 -62
  212. package/src/ruvocal/src/lib/server/api/__tests__/conversations-id.spec.ts +296 -296
  213. package/src/ruvocal/src/lib/server/api/__tests__/conversations-message.spec.ts +216 -216
  214. package/src/ruvocal/src/lib/server/api/__tests__/conversations.spec.ts +235 -235
  215. package/src/ruvocal/src/lib/server/api/__tests__/misc.spec.ts +72 -72
  216. package/src/ruvocal/src/lib/server/api/__tests__/testHelpers.ts +86 -86
  217. package/src/ruvocal/src/lib/server/api/__tests__/user-reports.spec.ts +78 -78
  218. package/src/ruvocal/src/lib/server/api/__tests__/user.spec.ts +239 -239
  219. package/src/ruvocal/src/lib/server/api/types.ts +37 -37
  220. package/src/ruvocal/src/lib/server/api/utils/requireAuth.ts +22 -22
  221. package/src/ruvocal/src/lib/server/api/utils/resolveConversation.ts +69 -69
  222. package/src/ruvocal/src/lib/server/api/utils/resolveModel.ts +27 -27
  223. package/src/ruvocal/src/lib/server/api/utils/superjsonResponse.ts +15 -15
  224. package/src/ruvocal/src/lib/server/apiToken.ts +11 -11
  225. package/src/ruvocal/src/lib/server/auth.ts +554 -554
  226. package/src/ruvocal/src/lib/server/config.ts +187 -187
  227. package/src/ruvocal/src/lib/server/conversation.ts +83 -83
  228. package/src/ruvocal/src/lib/server/database/__tests__/rvf.spec.ts +709 -709
  229. package/src/ruvocal/src/lib/server/database/postgres.ts +700 -700
  230. package/src/ruvocal/src/lib/server/database/rvf.ts +1078 -1078
  231. package/src/ruvocal/src/lib/server/database.ts +145 -145
  232. package/src/ruvocal/src/lib/server/endpoints/document.ts +68 -68
  233. package/src/ruvocal/src/lib/server/endpoints/endpoints.ts +43 -43
  234. package/src/ruvocal/src/lib/server/endpoints/images.ts +211 -211
  235. package/src/ruvocal/src/lib/server/endpoints/openai/endpointOai.ts +266 -266
  236. package/src/ruvocal/src/lib/server/endpoints/openai/openAIChatToTextGenerationStream.ts +212 -212
  237. package/src/ruvocal/src/lib/server/endpoints/openai/openAICompletionToTextGenerationStream.ts +32 -32
  238. package/src/ruvocal/src/lib/server/endpoints/preprocessMessages.ts +61 -61
  239. package/src/ruvocal/src/lib/server/exitHandler.ts +59 -59
  240. package/src/ruvocal/src/lib/server/files/downloadFile.ts +34 -34
  241. package/src/ruvocal/src/lib/server/files/uploadFile.ts +29 -29
  242. package/src/ruvocal/src/lib/server/findRepoRoot.ts +13 -13
  243. package/src/ruvocal/src/lib/server/generateFromDefaultEndpoint.ts +46 -46
  244. package/src/ruvocal/src/lib/server/hooks/error.ts +37 -37
  245. package/src/ruvocal/src/lib/server/hooks/fetch.ts +22 -22
  246. package/src/ruvocal/src/lib/server/hooks/handle.ts +250 -250
  247. package/src/ruvocal/src/lib/server/hooks/init.ts +51 -51
  248. package/src/ruvocal/src/lib/server/isURLLocal.spec.ts +31 -31
  249. package/src/ruvocal/src/lib/server/isURLLocal.ts +74 -74
  250. package/src/ruvocal/src/lib/server/logger.ts +42 -42
  251. package/src/ruvocal/src/lib/server/mcp/clientPool.spec.ts +175 -175
  252. package/src/ruvocal/src/lib/server/mcp/hf.ts +32 -32
  253. package/src/ruvocal/src/lib/server/mcp/httpClient.ts +122 -122
  254. package/src/ruvocal/src/lib/server/mcp/registry.ts +76 -76
  255. package/src/ruvocal/src/lib/server/mcp/tools.ts +196 -196
  256. package/src/ruvocal/src/lib/server/metrics.ts +255 -255
  257. package/src/ruvocal/src/lib/server/models.ts +518 -518
  258. package/src/ruvocal/src/lib/server/requestContext.ts +55 -55
  259. package/src/ruvocal/src/lib/server/router/arch.ts +230 -230
  260. package/src/ruvocal/src/lib/server/router/endpoint.ts +316 -316
  261. package/src/ruvocal/src/lib/server/router/multimodal.ts +28 -28
  262. package/src/ruvocal/src/lib/server/router/policy.ts +49 -49
  263. package/src/ruvocal/src/lib/server/router/toolsRoute.ts +51 -51
  264. package/src/ruvocal/src/lib/server/router/types.ts +21 -21
  265. package/src/ruvocal/src/lib/server/sendSlack.ts +23 -23
  266. package/src/ruvocal/src/lib/server/textGeneration/generate.ts +258 -258
  267. package/src/ruvocal/src/lib/server/textGeneration/index.ts +96 -96
  268. package/src/ruvocal/src/lib/server/textGeneration/mcp/fileRefs.ts +155 -155
  269. package/src/ruvocal/src/lib/server/textGeneration/mcp/routerResolution.ts +108 -108
  270. package/src/ruvocal/src/lib/server/textGeneration/mcp/runMcpFlow.ts +831 -831
  271. package/src/ruvocal/src/lib/server/textGeneration/mcp/toolInvocation.ts +349 -349
  272. package/src/ruvocal/src/lib/server/textGeneration/mcp/wasmTools.test.ts +633 -633
  273. package/src/ruvocal/src/lib/server/textGeneration/reasoning.ts +23 -23
  274. package/src/ruvocal/src/lib/server/textGeneration/title.ts +83 -83
  275. package/src/ruvocal/src/lib/server/textGeneration/types.ts +28 -28
  276. package/src/ruvocal/src/lib/server/textGeneration/utils/prepareFiles.ts +88 -88
  277. package/src/ruvocal/src/lib/server/textGeneration/utils/routing.ts +21 -21
  278. package/src/ruvocal/src/lib/server/textGeneration/utils/toolPrompt.ts +49 -49
  279. package/src/ruvocal/src/lib/server/urlSafety.ts +77 -77
  280. package/src/ruvocal/src/lib/server/usageLimits.ts +30 -30
  281. package/src/ruvocal/src/lib/stores/autopilotStore.svelte.ts +175 -175
  282. package/src/ruvocal/src/lib/stores/backgroundGenerations.svelte.ts +32 -32
  283. package/src/ruvocal/src/lib/stores/backgroundGenerations.ts +1 -1
  284. package/src/ruvocal/src/lib/stores/errors.ts +9 -9
  285. package/src/ruvocal/src/lib/stores/isAborted.ts +3 -3
  286. package/src/ruvocal/src/lib/stores/isPro.ts +4 -4
  287. package/src/ruvocal/src/lib/stores/loading.ts +3 -3
  288. package/src/ruvocal/src/lib/stores/mcpServers.ts +534 -534
  289. package/src/ruvocal/src/lib/stores/pendingChatInput.ts +3 -3
  290. package/src/ruvocal/src/lib/stores/pendingMessage.ts +9 -9
  291. package/src/ruvocal/src/lib/stores/settings.ts +182 -182
  292. package/src/ruvocal/src/lib/stores/shareModal.ts +13 -13
  293. package/src/ruvocal/src/lib/stores/titleUpdate.ts +8 -8
  294. package/src/ruvocal/src/lib/stores/wasmMcp.ts +472 -472
  295. package/src/ruvocal/src/lib/switchTheme.ts +124 -124
  296. package/src/ruvocal/src/lib/types/AbortedGeneration.ts +8 -8
  297. package/src/ruvocal/src/lib/types/Assistant.ts +31 -31
  298. package/src/ruvocal/src/lib/types/AssistantStats.ts +11 -11
  299. package/src/ruvocal/src/lib/types/ConfigKey.ts +4 -4
  300. package/src/ruvocal/src/lib/types/ConvSidebar.ts +9 -9
  301. package/src/ruvocal/src/lib/types/Conversation.ts +27 -27
  302. package/src/ruvocal/src/lib/types/ConversationStats.ts +13 -13
  303. package/src/ruvocal/src/lib/types/Message.ts +41 -41
  304. package/src/ruvocal/src/lib/types/MessageEvent.ts +10 -10
  305. package/src/ruvocal/src/lib/types/MessageUpdate.ts +139 -139
  306. package/src/ruvocal/src/lib/types/MigrationResult.ts +7 -7
  307. package/src/ruvocal/src/lib/types/Model.ts +23 -23
  308. package/src/ruvocal/src/lib/types/Report.ts +12 -12
  309. package/src/ruvocal/src/lib/types/Review.ts +6 -6
  310. package/src/ruvocal/src/lib/types/Semaphore.ts +19 -19
  311. package/src/ruvocal/src/lib/types/Session.ts +22 -22
  312. package/src/ruvocal/src/lib/types/Settings.ts +93 -93
  313. package/src/ruvocal/src/lib/types/SharedConversation.ts +9 -9
  314. package/src/ruvocal/src/lib/types/Template.ts +6 -6
  315. package/src/ruvocal/src/lib/types/Timestamps.ts +4 -4
  316. package/src/ruvocal/src/lib/types/TokenCache.ts +6 -6
  317. package/src/ruvocal/src/lib/types/Tool.ts +77 -77
  318. package/src/ruvocal/src/lib/types/UrlDependency.ts +5 -5
  319. package/src/ruvocal/src/lib/types/User.ts +14 -14
  320. package/src/ruvocal/src/lib/utils/PublicConfig.svelte.ts +75 -75
  321. package/src/ruvocal/src/lib/utils/auth.ts +17 -17
  322. package/src/ruvocal/src/lib/utils/chunk.ts +33 -33
  323. package/src/ruvocal/src/lib/utils/cookiesAreEnabled.ts +13 -13
  324. package/src/ruvocal/src/lib/utils/debounce.ts +17 -17
  325. package/src/ruvocal/src/lib/utils/deepestChild.ts +6 -6
  326. package/src/ruvocal/src/lib/utils/favicon.ts +21 -21
  327. package/src/ruvocal/src/lib/utils/fetchJSON.ts +23 -23
  328. package/src/ruvocal/src/lib/utils/file2base64.ts +14 -14
  329. package/src/ruvocal/src/lib/utils/formatUserCount.ts +37 -37
  330. package/src/ruvocal/src/lib/utils/generationState.spec.ts +75 -75
  331. package/src/ruvocal/src/lib/utils/generationState.ts +26 -26
  332. package/src/ruvocal/src/lib/utils/getHref.ts +41 -41
  333. package/src/ruvocal/src/lib/utils/getReturnFromGenerator.ts +7 -7
  334. package/src/ruvocal/src/lib/utils/haptics.ts +64 -64
  335. package/src/ruvocal/src/lib/utils/hashConv.ts +12 -12
  336. package/src/ruvocal/src/lib/utils/hf.ts +17 -17
  337. package/src/ruvocal/src/lib/utils/isDesktop.ts +7 -7
  338. package/src/ruvocal/src/lib/utils/isUrl.ts +8 -8
  339. package/src/ruvocal/src/lib/utils/isVirtualKeyboard.ts +16 -16
  340. package/src/ruvocal/src/lib/utils/loadAttachmentsFromUrls.ts +115 -115
  341. package/src/ruvocal/src/lib/utils/marked.spec.ts +96 -96
  342. package/src/ruvocal/src/lib/utils/marked.ts +531 -531
  343. package/src/ruvocal/src/lib/utils/mcpValidation.ts +147 -147
  344. package/src/ruvocal/src/lib/utils/mergeAsyncGenerators.ts +38 -38
  345. package/src/ruvocal/src/lib/utils/messageUpdates.spec.ts +262 -262
  346. package/src/ruvocal/src/lib/utils/messageUpdates.ts +324 -324
  347. package/src/ruvocal/src/lib/utils/mime.ts +56 -56
  348. package/src/ruvocal/src/lib/utils/models.ts +14 -14
  349. package/src/ruvocal/src/lib/utils/parseBlocks.ts +120 -120
  350. package/src/ruvocal/src/lib/utils/parseIncompleteMarkdown.ts +644 -644
  351. package/src/ruvocal/src/lib/utils/parseStringToList.ts +10 -10
  352. package/src/ruvocal/src/lib/utils/randomUuid.ts +14 -14
  353. package/src/ruvocal/src/lib/utils/searchTokens.ts +33 -33
  354. package/src/ruvocal/src/lib/utils/sha256.ts +7 -7
  355. package/src/ruvocal/src/lib/utils/stringifyError.ts +12 -12
  356. package/src/ruvocal/src/lib/utils/sum.ts +3 -3
  357. package/src/ruvocal/src/lib/utils/template.spec.ts +59 -59
  358. package/src/ruvocal/src/lib/utils/template.ts +53 -53
  359. package/src/ruvocal/src/lib/utils/timeout.ts +9 -9
  360. package/src/ruvocal/src/lib/utils/toolProgress.spec.ts +46 -46
  361. package/src/ruvocal/src/lib/utils/toolProgress.ts +11 -11
  362. package/src/ruvocal/src/lib/utils/tree/addChildren.spec.ts +102 -102
  363. package/src/ruvocal/src/lib/utils/tree/addChildren.ts +48 -48
  364. package/src/ruvocal/src/lib/utils/tree/addSibling.spec.ts +81 -81
  365. package/src/ruvocal/src/lib/utils/tree/addSibling.ts +41 -41
  366. package/src/ruvocal/src/lib/utils/tree/buildSubtree.spec.ts +110 -110
  367. package/src/ruvocal/src/lib/utils/tree/buildSubtree.ts +24 -24
  368. package/src/ruvocal/src/lib/utils/tree/convertLegacyConversation.spec.ts +31 -31
  369. package/src/ruvocal/src/lib/utils/tree/convertLegacyConversation.ts +36 -36
  370. package/src/ruvocal/src/lib/utils/tree/isMessageId.spec.ts +15 -15
  371. package/src/ruvocal/src/lib/utils/tree/isMessageId.ts +5 -5
  372. package/src/ruvocal/src/lib/utils/tree/tree.d.ts +14 -14
  373. package/src/ruvocal/src/lib/utils/tree/treeHelpers.spec.ts +167 -167
  374. package/src/ruvocal/src/lib/utils/updates.ts +39 -39
  375. package/src/ruvocal/src/lib/utils/urlParams.ts +13 -13
  376. package/src/ruvocal/src/lib/wasm/idb.ts +438 -438
  377. package/src/ruvocal/src/lib/wasm/index.ts +1213 -1213
  378. package/src/ruvocal/src/lib/wasm/tests/wasm-capabilities.test.ts +565 -565
  379. package/src/ruvocal/src/lib/wasm/wasm.worker.ts +332 -332
  380. package/src/ruvocal/src/lib/wasm/workerClient.ts +166 -166
  381. package/src/ruvocal/src/lib/workers/autopilotWorker.ts +221 -221
  382. package/src/ruvocal/src/lib/workers/detailFetchWorker.ts +100 -100
  383. package/src/ruvocal/src/lib/workers/markdownWorker.ts +61 -61
  384. package/src/ruvocal/src/routes/+error.svelte +20 -20
  385. package/src/ruvocal/src/routes/+layout.svelte +324 -324
  386. package/src/ruvocal/src/routes/+layout.ts +91 -91
  387. package/src/ruvocal/src/routes/+page.svelte +168 -168
  388. package/src/ruvocal/src/routes/.well-known/oauth-cimd/+server.ts +37 -37
  389. package/src/ruvocal/src/routes/__debug/openai/+server.ts +21 -21
  390. package/src/ruvocal/src/routes/admin/export/+server.ts +159 -159
  391. package/src/ruvocal/src/routes/admin/stats/compute/+server.ts +16 -16
  392. package/src/ruvocal/src/routes/api/conversation/[id]/+server.ts +40 -40
  393. package/src/ruvocal/src/routes/api/conversation/[id]/message/[messageId]/+server.ts +42 -42
  394. package/src/ruvocal/src/routes/api/conversations/+server.ts +48 -48
  395. package/src/ruvocal/src/routes/api/fetch-url/+server.ts +147 -147
  396. package/src/ruvocal/src/routes/api/mcp/health/+server.ts +292 -292
  397. package/src/ruvocal/src/routes/api/mcp/servers/+server.ts +32 -32
  398. package/src/ruvocal/src/routes/api/models/+server.ts +25 -25
  399. package/src/ruvocal/src/routes/api/transcribe/+server.ts +104 -104
  400. package/src/ruvocal/src/routes/api/user/+server.ts +15 -15
  401. package/src/ruvocal/src/routes/api/user/validate-token/+server.ts +20 -20
  402. package/src/ruvocal/src/routes/api/v2/conversations/+server.ts +48 -48
  403. package/src/ruvocal/src/routes/api/v2/conversations/[id]/+server.ts +94 -94
  404. package/src/ruvocal/src/routes/api/v2/conversations/[id]/message/[messageId]/+server.ts +43 -43
  405. package/src/ruvocal/src/routes/api/v2/conversations/import-share/+server.ts +23 -23
  406. package/src/ruvocal/src/routes/api/v2/debug/config/+server.ts +16 -16
  407. package/src/ruvocal/src/routes/api/v2/debug/refresh/+server.ts +30 -30
  408. package/src/ruvocal/src/routes/api/v2/export/+server.ts +196 -196
  409. package/src/ruvocal/src/routes/api/v2/feature-flags/+server.ts +14 -14
  410. package/src/ruvocal/src/routes/api/v2/models/+server.ts +38 -38
  411. package/src/ruvocal/src/routes/api/v2/models/[namespace]/+server.ts +8 -8
  412. package/src/ruvocal/src/routes/api/v2/models/[namespace]/[model]/+server.ts +8 -8
  413. package/src/ruvocal/src/routes/api/v2/models/[namespace]/[model]/subscribe/+server.ts +28 -28
  414. package/src/ruvocal/src/routes/api/v2/models/[namespace]/subscribe/+server.ts +28 -28
  415. package/src/ruvocal/src/routes/api/v2/models/old/+server.ts +7 -7
  416. package/src/ruvocal/src/routes/api/v2/models/refresh/+server.ts +33 -33
  417. package/src/ruvocal/src/routes/api/v2/public-config/+server.ts +7 -7
  418. package/src/ruvocal/src/routes/api/v2/user/+server.ts +17 -17
  419. package/src/ruvocal/src/routes/api/v2/user/billing-orgs/+server.ts +73 -73
  420. package/src/ruvocal/src/routes/api/v2/user/reports/+server.ts +17 -17
  421. package/src/ruvocal/src/routes/api/v2/user/settings/+server.ts +110 -110
  422. package/src/ruvocal/src/routes/conversation/+server.ts +115 -115
  423. package/src/ruvocal/src/routes/conversation/[id]/+page.svelte +586 -586
  424. package/src/ruvocal/src/routes/conversation/[id]/+page.ts +60 -60
  425. package/src/ruvocal/src/routes/conversation/[id]/+server.ts +740 -740
  426. package/src/ruvocal/src/routes/conversation/[id]/message/[messageId]/prompt/+server.ts +66 -66
  427. package/src/ruvocal/src/routes/conversation/[id]/share/+server.ts +69 -69
  428. package/src/ruvocal/src/routes/conversation/[id]/stop-generating/+server.ts +35 -35
  429. package/src/ruvocal/src/routes/healthcheck/+server.ts +3 -3
  430. package/src/ruvocal/src/routes/login/+server.ts +5 -5
  431. package/src/ruvocal/src/routes/login/callback/+server.ts +103 -103
  432. package/src/ruvocal/src/routes/login/callback/updateUser.spec.ts +157 -157
  433. package/src/ruvocal/src/routes/login/callback/updateUser.ts +215 -215
  434. package/src/ruvocal/src/routes/logout/+server.ts +18 -18
  435. package/src/ruvocal/src/routes/metrics/+server.ts +18 -18
  436. package/src/ruvocal/src/routes/models/+page.svelte +233 -233
  437. package/src/ruvocal/src/routes/models/[...model]/+page.svelte +161 -161
  438. package/src/ruvocal/src/routes/models/[...model]/+page.ts +14 -14
  439. package/src/ruvocal/src/routes/models/[...model]/thumbnail.png/+server.ts +64 -64
  440. package/src/ruvocal/src/routes/models/[...model]/thumbnail.png/ModelThumbnail.svelte +28 -28
  441. package/src/ruvocal/src/routes/privacy/+page.svelte +11 -11
  442. package/src/ruvocal/src/routes/r/[id]/+page.ts +34 -34
  443. package/src/ruvocal/src/routes/settings/(nav)/+layout.svelte +282 -282
  444. package/src/ruvocal/src/routes/settings/(nav)/+layout.ts +1 -1
  445. package/src/ruvocal/src/routes/settings/(nav)/+server.ts +59 -59
  446. package/src/ruvocal/src/routes/settings/(nav)/[...model]/+page.svelte +464 -464
  447. package/src/ruvocal/src/routes/settings/(nav)/[...model]/+page.ts +14 -14
  448. package/src/ruvocal/src/routes/settings/(nav)/application/+page.svelte +362 -362
  449. package/src/ruvocal/src/routes/settings/+layout.svelte +40 -40
  450. package/src/ruvocal/src/styles/highlight-js.css +195 -195
  451. package/src/ruvocal/src/styles/main.css +144 -144
  452. package/src/ruvocal/static/chatui/favicon-dark.svg +3 -3
  453. package/src/ruvocal/static/chatui/favicon-dev.svg +3 -3
  454. package/src/ruvocal/static/chatui/favicon.svg +3 -3
  455. package/src/ruvocal/static/chatui/icon.svg +3 -3
  456. package/src/ruvocal/static/chatui/logo.svg +7 -7
  457. package/src/ruvocal/static/chatui/manifest.json +54 -54
  458. package/src/ruvocal/static/chatui/welcome.js +184 -184
  459. package/src/ruvocal/static/huggingchat/favicon-dark.svg +4 -4
  460. package/src/ruvocal/static/huggingchat/favicon-dev.svg +4 -4
  461. package/src/ruvocal/static/huggingchat/favicon.svg +4 -4
  462. package/src/ruvocal/static/huggingchat/fulltext-logo.svg +1 -1
  463. package/src/ruvocal/static/huggingchat/icon.svg +4 -4
  464. package/src/ruvocal/static/huggingchat/logo.svg +4 -4
  465. package/src/ruvocal/static/huggingchat/manifest.json +54 -54
  466. package/src/ruvocal/static/huggingchat/routes.chat.json +226 -226
  467. package/src/ruvocal/static/robots.txt +10 -10
  468. package/src/ruvocal/static/wasm/rvagent_wasm.js +1539 -1539
  469. package/src/ruvocal/stub/@reflink/reflink/package.json +5 -5
  470. package/src/ruvocal/svelte.config.js +53 -53
  471. package/src/ruvocal/tailwind.config.cjs +30 -30
  472. package/src/ruvocal/tsconfig.json +19 -19
  473. package/src/ruvocal/vite.config.ts +87 -87
  474. package/src/scripts/deploy.sh +116 -116
  475. package/src/scripts/generate-config.js +245 -245
  476. package/src/scripts/generate-welcome.js +187 -187
  477. package/src/scripts/package-rvf.sh +116 -116
  478. package/src/ruvocal/.claude-flow/daemon-state.json +0 -135
  479. package/src/ruvocal/.claude-flow/data/pending-insights.jsonl +0 -0
  480. package/src/ruvocal/.claude-flow/data/ranked-context.json +0 -5
  481. package/src/ruvocal/.claude-flow/logs/daemon.log +0 -31
  482. package/src/ruvocal/.claude-flow/logs/headless/audit_1777949411822_juxau0_prompt.log +0 -989
  483. package/src/ruvocal/.claude-flow/logs/headless/audit_1777949411822_juxau0_result.log +0 -67
  484. package/src/ruvocal/.claude-flow/logs/headless/audit_1777950042278_jvj5xq_prompt.log +0 -989
  485. package/src/ruvocal/.claude-flow/logs/headless/audit_1777950042278_jvj5xq_result.log +0 -93
  486. package/src/ruvocal/.claude-flow/logs/headless/optimize_1777949531823_yt5yc2_prompt.log +0 -1498
  487. package/src/ruvocal/.claude-flow/logs/headless/optimize_1777949531823_yt5yc2_result.log +0 -93
  488. package/src/ruvocal/.claude-flow/logs/headless/testgaps_1777949771821_elw1j4_prompt.log +0 -1498
  489. package/src/ruvocal/.claude-flow/logs/headless/testgaps_1777949771821_elw1j4_result.log +0 -100
  490. package/src/ruvocal/.claude-flow/metrics/codebase-map.json +0 -11
  491. package/src/ruvocal/.claude-flow/metrics/consolidation.json +0 -6
  492. package/src/ruvocal/.claude-flow/neural/stats.json +0 -6
  493. package/src/ruvocal/.claude-flow/sessions/current.json +0 -13
  494. package/src/ruvocal/.swarm/attestation.db +0 -0
  495. package/src/ruvocal/.swarm/hnsw.index +0 -0
  496. package/src/ruvocal/.swarm/hnsw.metadata.json +0 -1
  497. package/src/ruvocal/.swarm/memory.db +0 -0
  498. package/src/ruvocal/.swarm/schema.sql +0 -305
@@ -1,831 +1,831 @@
1
- import { config } from "$lib/server/config";
2
- import { MessageUpdateType, type MessageUpdate } from "$lib/types/MessageUpdate";
3
- import { getMcpServers } from "$lib/server/mcp/registry";
4
- import { isValidUrl } from "$lib/server/urlSafety";
5
- import { resetMcpToolsCache } from "$lib/server/mcp/tools";
6
- import { getOpenAiToolsForMcp } from "$lib/server/mcp/tools";
7
- import type {
8
- ChatCompletionChunk,
9
- ChatCompletionCreateParamsStreaming,
10
- ChatCompletionMessageParam,
11
- ChatCompletionMessageToolCall,
12
- } from "openai/resources/chat/completions";
13
- import type { Stream } from "openai/streaming";
14
- import { buildToolPreprompt } from "../utils/toolPrompt";
15
- import type { EndpointMessage } from "../../endpoints/endpoints";
16
- import { resolveRouterTarget } from "./routerResolution";
17
- import { executeToolCalls, type NormalizedToolCall } from "./toolInvocation";
18
- import { drainPool } from "$lib/server/mcp/clientPool";
19
- import type { TextGenerationContext } from "../types";
20
- import {
21
- hasAuthHeader,
22
- isStrictHfMcpLogin,
23
- hasNonEmptyToken,
24
- isExaMcpServer,
25
- } from "$lib/server/mcp/hf";
26
- import { buildImageRefResolver } from "./fileRefs";
27
- import { prepareMessagesWithFiles } from "$lib/server/textGeneration/utils/prepareFiles";
28
- import { makeImageProcessor } from "$lib/server/endpoints/images";
29
- import { logger } from "$lib/server/logger";
30
- import { AbortedGenerations } from "$lib/server/abortedGenerations";
31
-
32
- export type RunMcpFlowContext = Pick<
33
- TextGenerationContext,
34
- "model" | "conv" | "assistant" | "forceMultimodal" | "forceTools" | "provider" | "locals"
35
- > & { messages: EndpointMessage[] };
36
-
37
- // Return type: "completed" = MCP ran successfully, "not_applicable" = MCP didn't run, "aborted" = user aborted
38
- export type McpFlowResult = "completed" | "not_applicable" | "aborted";
39
-
40
- export async function* runMcpFlow({
41
- model,
42
- conv,
43
- messages,
44
- assistant,
45
- forceMultimodal,
46
- forceTools,
47
- provider,
48
- locals,
49
- preprompt,
50
- abortSignal,
51
- abortController,
52
- promptedAt,
53
- autopilot,
54
- autopilotMaxSteps,
55
- }: RunMcpFlowContext & {
56
- preprompt?: string;
57
- abortSignal?: AbortSignal;
58
- abortController?: AbortController;
59
- promptedAt?: Date;
60
- autopilot?: boolean;
61
- autopilotMaxSteps?: number;
62
- }): AsyncGenerator<MessageUpdate, McpFlowResult, undefined> {
63
- // Helper to check if generation should be aborted via DB polling
64
- // Also triggers the abort controller to cancel active streams/requests
65
- const checkAborted = (): boolean => {
66
- if (abortSignal?.aborted) return true;
67
- const abortTime = AbortedGenerations.getInstance().getAbortTime(conv._id.toString());
68
- if (abortTime && promptedAt && abortTime > promptedAt) {
69
- // Trigger the abort controller to cancel active streams
70
- if (abortController && !abortController.signal.aborted) {
71
- abortController.abort();
72
- }
73
- return true;
74
- }
75
- return false;
76
- };
77
- // Start from env-configured servers
78
- let servers = getMcpServers();
79
- try {
80
- logger.debug(
81
- { baseServers: servers.map((s) => ({ name: s.name, url: s.url })), count: servers.length },
82
- "[mcp] base servers loaded"
83
- );
84
- } catch {}
85
-
86
- // Merge in request-provided custom servers (if any)
87
- try {
88
- const reqMcp = (
89
- locals as unknown as {
90
- mcp?: {
91
- selectedServers?: Array<{ name: string; url: string; headers?: Record<string, string> }>;
92
- selectedServerNames?: string[];
93
- };
94
- }
95
- )?.mcp;
96
- const custom = Array.isArray(reqMcp?.selectedServers) ? reqMcp?.selectedServers : [];
97
- if (custom.length > 0) {
98
- // Invalidate cached tool list when the set of servers changes at request-time
99
- resetMcpToolsCache();
100
- // Deduplicate by server name (request takes precedence)
101
- const byName = new Map<
102
- string,
103
- { name: string; url: string; headers?: Record<string, string> }
104
- >();
105
- for (const s of servers) byName.set(s.name, s);
106
- for (const s of custom) byName.set(s.name, s);
107
- servers = [...byName.values()];
108
- try {
109
- logger.debug(
110
- {
111
- customProvidedCount: custom.length,
112
- mergedServers: servers.map((s) => ({
113
- name: s.name,
114
- url: s.url,
115
- hasAuth: !!s.headers?.Authorization,
116
- })),
117
- },
118
- "[mcp] merged request-provided servers"
119
- );
120
- } catch {}
121
- }
122
-
123
- // If the client specified a selection by name, filter to those
124
- const names = Array.isArray(reqMcp?.selectedServerNames)
125
- ? reqMcp?.selectedServerNames
126
- : undefined;
127
- if (Array.isArray(names)) {
128
- const before = servers.map((s) => s.name);
129
- servers = servers.filter((s) => names.includes(s.name));
130
- try {
131
- logger.debug(
132
- { selectedNames: names, before, after: servers.map((s) => s.name) },
133
- "[mcp] applied name selection"
134
- );
135
- } catch {}
136
- }
137
- } catch {
138
- // ignore selection merge errors and proceed with env servers
139
- }
140
-
141
- // If selection/merge yielded no servers, bail early with clearer log
142
- if (servers.length === 0) {
143
- logger.warn({}, "[mcp] no MCP servers selected after merge/name filter");
144
- return "not_applicable";
145
- }
146
-
147
- // Enforce server-side safety (public HTTPS only, no private ranges)
148
- {
149
- const before = servers.slice();
150
- servers = servers.filter((s) => {
151
- try {
152
- return isValidUrl(s.url);
153
- } catch {
154
- return false;
155
- }
156
- });
157
- try {
158
- const rejected = before.filter((b) => !servers.includes(b));
159
- if (rejected.length > 0) {
160
- logger.warn(
161
- { rejected: rejected.map((r) => ({ name: r.name, url: r.url })) },
162
- "[mcp] rejected servers by URL safety"
163
- );
164
- }
165
- } catch {}
166
- }
167
- if (servers.length === 0) {
168
- logger.warn({}, "[mcp] all selected MCP servers rejected by URL safety guard");
169
- return "not_applicable";
170
- }
171
-
172
- // Optionally attach the logged-in user's HF token to the official HF MCP server only.
173
- // Never override an explicit Authorization header, and require token to look like an HF token.
174
- try {
175
- const shouldForward = config.MCP_FORWARD_HF_USER_TOKEN === "true";
176
- const userToken =
177
- (locals as unknown as { hfAccessToken?: string } | undefined)?.hfAccessToken ??
178
- (locals as unknown as { token?: string } | undefined)?.token;
179
-
180
- if (shouldForward && hasNonEmptyToken(userToken)) {
181
- const overlayApplied: string[] = [];
182
- servers = servers.map((s) => {
183
- try {
184
- if (isStrictHfMcpLogin(s.url) && !hasAuthHeader(s.headers)) {
185
- overlayApplied.push(s.name);
186
- return {
187
- ...s,
188
- headers: { ...(s.headers ?? {}), Authorization: `Bearer ${userToken}` },
189
- };
190
- }
191
- } catch {
192
- // ignore URL parse errors and leave server unchanged
193
- }
194
- return s;
195
- });
196
- if (overlayApplied.length > 0) {
197
- try {
198
- logger.debug({ overlayApplied }, "[mcp] forwarded HF token to servers");
199
- } catch {}
200
- }
201
- }
202
- } catch {
203
- // best-effort overlay; continue if anything goes wrong
204
- }
205
-
206
- // Inject Exa API key for mcp.exa.ai servers via URL param (mcp.exa.ai doesn't support headers)
207
- try {
208
- const exaApiKey = config.EXA_API_KEY;
209
- if (hasNonEmptyToken(exaApiKey)) {
210
- const overlayApplied: string[] = [];
211
- servers = servers.map((s) => {
212
- try {
213
- if (isExaMcpServer(s.url)) {
214
- const url = new URL(s.url);
215
- if (!url.searchParams.has("exaApiKey")) {
216
- url.searchParams.set("exaApiKey", exaApiKey);
217
- overlayApplied.push(s.name);
218
- return { ...s, url: url.toString() };
219
- }
220
- }
221
- } catch {}
222
- return s;
223
- });
224
- if (overlayApplied.length > 0) {
225
- logger.debug({ overlayApplied }, "[mcp] injected Exa API key to servers");
226
- }
227
- }
228
- } catch {
229
- // best-effort injection; continue if anything goes wrong
230
- }
231
-
232
- logger.debug(
233
- { count: servers.length, servers: servers.map((s) => s.name) },
234
- "[mcp] servers configured"
235
- );
236
- if (servers.length === 0) {
237
- return "not_applicable";
238
- }
239
-
240
- // Gate MCP flow based on model tool support (aggregated) with user override
241
- try {
242
- const supportsTools = Boolean((model as unknown as { supportsTools?: boolean }).supportsTools);
243
- const toolsEnabled = Boolean(forceTools) || supportsTools;
244
- logger.debug(
245
- {
246
- model: model.id ?? model.name,
247
- supportsTools,
248
- forceTools: Boolean(forceTools),
249
- toolsEnabled,
250
- },
251
- "[mcp] tools gate evaluation"
252
- );
253
- if (!toolsEnabled) {
254
- logger.info(
255
- { model: model.id ?? model.name },
256
- "[mcp] tools disabled for model; skipping MCP flow"
257
- );
258
- return "not_applicable";
259
- }
260
- } catch {
261
- // If anything goes wrong reading the flag, proceed (previous behavior)
262
- }
263
-
264
- const resolveFileRef = buildImageRefResolver(messages);
265
- const imageProcessor = makeImageProcessor({
266
- supportedMimeTypes: ["image/png", "image/jpeg"],
267
- preferredMimeType: "image/jpeg",
268
- maxSizeInMB: 1,
269
- maxWidth: 1024,
270
- maxHeight: 1024,
271
- });
272
-
273
- const hasImageInput = messages.some((msg) =>
274
- (msg.files ?? []).some(
275
- (file) => typeof file?.mime === "string" && file.mime.startsWith("image/")
276
- )
277
- );
278
-
279
- const { runMcp, targetModel, candidateModelId, resolvedRoute } = await resolveRouterTarget({
280
- model,
281
- messages,
282
- conversationId: conv._id.toString(),
283
- hasImageInput,
284
- locals,
285
- });
286
-
287
- if (!runMcp) {
288
- logger.info(
289
- { model: targetModel.id ?? targetModel.name, resolvedRoute },
290
- "[mcp] runMcp=false (routing chose non-tools candidate)"
291
- );
292
- return "not_applicable";
293
- }
294
-
295
- try {
296
- const { tools: oaTools, mapping } = await getOpenAiToolsForMcp(servers, {
297
- signal: abortSignal,
298
- });
299
- try {
300
- logger.info(
301
- { toolCount: oaTools.length, toolNames: oaTools.map((t) => t.function.name) },
302
- "[mcp] openai tool defs built"
303
- );
304
- } catch {}
305
- if (oaTools.length === 0) {
306
- logger.warn({}, "[mcp] zero tools available after listing; skipping MCP flow");
307
- return "not_applicable";
308
- }
309
-
310
- const { OpenAI } = await import("openai");
311
-
312
- // Capture provider header (x-inference-provider) from the upstream OpenAI-compatible server.
313
- let providerHeader: string | undefined;
314
- const captureProviderFetch = async (
315
- input: RequestInfo | URL,
316
- init?: RequestInit
317
- ): Promise<Response> => {
318
- const res = await fetch(input, init);
319
- const p = res.headers.get("x-inference-provider");
320
- if (p && !providerHeader) providerHeader = p;
321
- return res;
322
- };
323
-
324
- const openai = new OpenAI({
325
- apiKey: config.OPENAI_API_KEY || config.HF_TOKEN || "sk-",
326
- baseURL: config.OPENAI_BASE_URL,
327
- fetch: captureProviderFetch,
328
- defaultHeaders: {
329
- // Bill to organization if configured (HuggingChat only)
330
- ...(config.isHuggingChat && locals?.billingOrganization
331
- ? { "X-HF-Bill-To": locals.billingOrganization }
332
- : {}),
333
- },
334
- });
335
-
336
- const mmEnabled = (forceMultimodal ?? false) || targetModel.multimodal;
337
- logger.info(
338
- {
339
- targetModel: targetModel.id ?? targetModel.name,
340
- mmEnabled,
341
- route: resolvedRoute,
342
- candidateModelId,
343
- toolCount: oaTools.length,
344
- hasUserToken: Boolean((locals as unknown as { token?: string })?.token),
345
- },
346
- "[mcp] starting completion with tools"
347
- );
348
- let messagesOpenAI: ChatCompletionMessageParam[] = await prepareMessagesWithFiles(
349
- messages,
350
- imageProcessor,
351
- mmEnabled
352
- );
353
- const toolPreprompt = buildToolPreprompt(oaTools, autopilot);
354
- const prepromptPieces: string[] = [];
355
- if (toolPreprompt.trim().length > 0) {
356
- prepromptPieces.push(toolPreprompt);
357
- }
358
- if (typeof preprompt === "string" && preprompt.trim().length > 0) {
359
- prepromptPieces.push(preprompt);
360
- }
361
- const mergedPreprompt = prepromptPieces.join("\n\n");
362
- const hasSystemMessage = messagesOpenAI.length > 0 && messagesOpenAI[0]?.role === "system";
363
- if (hasSystemMessage) {
364
- if (mergedPreprompt.length > 0) {
365
- const existing = messagesOpenAI[0].content ?? "";
366
- const existingText = typeof existing === "string" ? existing : "";
367
- messagesOpenAI[0].content = mergedPreprompt + (existingText ? "\n\n" + existingText : "");
368
- }
369
- } else if (mergedPreprompt.length > 0) {
370
- messagesOpenAI = [{ role: "system", content: mergedPreprompt }, ...messagesOpenAI];
371
- }
372
-
373
- // Work around servers that reject `system` role
374
- if (
375
- typeof config.OPENAI_BASE_URL === "string" &&
376
- config.OPENAI_BASE_URL.length > 0 &&
377
- (config.OPENAI_BASE_URL.includes("hf.space") ||
378
- config.OPENAI_BASE_URL.includes("gradio.app")) &&
379
- messagesOpenAI[0]?.role === "system"
380
- ) {
381
- messagesOpenAI[0] = { ...messagesOpenAI[0], role: "user" };
382
- }
383
-
384
- const parameters = { ...targetModel.parameters, ...assistant?.generateSettings } as Record<
385
- string,
386
- unknown
387
- >;
388
- const maxTokens =
389
- (parameters?.max_tokens as number | undefined) ??
390
- (parameters?.max_new_tokens as number | undefined) ??
391
- (parameters?.max_completion_tokens as number | undefined);
392
-
393
- const stopSequences =
394
- typeof parameters?.stop === "string"
395
- ? parameters.stop
396
- : Array.isArray(parameters?.stop)
397
- ? (parameters.stop as string[])
398
- : undefined;
399
-
400
- // Build model ID with optional provider suffix (e.g., "model:fastest" or "model:together")
401
- const baseModelId = targetModel.id ?? targetModel.name;
402
- const modelIdWithProvider =
403
- provider && provider !== "auto" ? `${baseModelId}:${provider}` : baseModelId;
404
-
405
- const completionBase: Omit<ChatCompletionCreateParamsStreaming, "messages"> = {
406
- model: modelIdWithProvider,
407
- stream: true,
408
- temperature: typeof parameters?.temperature === "number" ? parameters.temperature : undefined,
409
- top_p: typeof parameters?.top_p === "number" ? parameters.top_p : undefined,
410
- frequency_penalty:
411
- typeof parameters?.frequency_penalty === "number"
412
- ? parameters.frequency_penalty
413
- : typeof parameters?.repetition_penalty === "number"
414
- ? parameters.repetition_penalty
415
- : undefined,
416
- presence_penalty:
417
- typeof parameters?.presence_penalty === "number" ? parameters.presence_penalty : undefined,
418
- stop: stopSequences,
419
- max_tokens: typeof maxTokens === "number" ? maxTokens : undefined,
420
- tools: oaTools,
421
- tool_choice: "auto",
422
- };
423
-
424
- const toPrimitive = (value: unknown) => {
425
- if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") {
426
- return value;
427
- }
428
- return undefined;
429
- };
430
-
431
- const parseArgs = (raw: unknown): Record<string, unknown> => {
432
- if (typeof raw !== "string" || raw.trim().length === 0) return {};
433
- try {
434
- return JSON.parse(raw);
435
- } catch {
436
- return {};
437
- }
438
- };
439
-
440
- const processToolOutput = (
441
- text: string
442
- ): {
443
- annotated: string;
444
- sources: { index: number; link: string }[];
445
- } => ({ annotated: text, sources: [] });
446
-
447
- let lastAssistantContent = "";
448
- let streamedContent = false;
449
- // Track whether we're inside a <think> block when the upstream streams
450
- // provider-specific reasoning tokens (e.g. `reasoning` or `reasoning_content`).
451
- let thinkOpen = false;
452
-
453
- if (resolvedRoute && candidateModelId) {
454
- yield {
455
- type: MessageUpdateType.RouterMetadata,
456
- route: resolvedRoute,
457
- model: candidateModelId,
458
- };
459
- logger.debug(
460
- { route: resolvedRoute, model: candidateModelId },
461
- "[mcp] router metadata emitted"
462
- );
463
- }
464
-
465
- // Autopilot loop cap is user-configurable (Settings.autopilotMaxSteps);
466
- // fall back to 30 for back-compat when the client doesn't send a value.
467
- // Non-autopilot mode keeps a tight 10-loop safety net regardless of caller input.
468
- const autopilotCap =
469
- typeof autopilotMaxSteps === "number" && autopilotMaxSteps > 0
470
- ? Math.min(autopilotMaxSteps, 100)
471
- : 30;
472
- const maxLoops = autopilot ? autopilotCap : 10;
473
- for (let loop = 0; loop < maxLoops; loop += 1) {
474
- // Check for abort at the start of each loop iteration
475
- if (checkAborted()) {
476
- logger.info({ loop }, "[mcp] aborting at start of loop iteration");
477
- return "aborted";
478
- }
479
-
480
- lastAssistantContent = "";
481
- streamedContent = false;
482
-
483
- const completionRequest: ChatCompletionCreateParamsStreaming = {
484
- ...completionBase,
485
- messages: messagesOpenAI,
486
- };
487
-
488
- const completionStream: Stream<ChatCompletionChunk> = await openai.chat.completions.create(
489
- completionRequest,
490
- {
491
- signal: abortSignal,
492
- headers: {
493
- "ChatUI-Conversation-ID": conv._id.toString(),
494
- "X-use-cache": "false",
495
- ...(locals?.token ? { Authorization: `Bearer ${locals.token}` } : {}),
496
- },
497
- }
498
- );
499
-
500
- // If provider header was exposed, notify UI so it can render "via {provider}".
501
- if (providerHeader) {
502
- yield {
503
- type: MessageUpdateType.RouterMetadata,
504
- route: "",
505
- model: "",
506
- provider: providerHeader as unknown as import("@huggingface/inference").InferenceProvider,
507
- };
508
- logger.debug({ provider: providerHeader }, "[mcp] provider metadata emitted");
509
- }
510
-
511
- const toolCallState: Record<number, { id?: string; name?: string; arguments: string }> = {};
512
- let firstToolDeltaLogged = false;
513
- let sawToolCall = false;
514
- let tokenCount = 0;
515
- for await (const chunk of completionStream) {
516
- const choice = chunk.choices?.[0];
517
- const delta = choice?.delta;
518
- if (!delta) continue;
519
-
520
- const chunkToolCalls = delta.tool_calls ?? [];
521
- if (chunkToolCalls.length > 0) {
522
- sawToolCall = true;
523
- for (const call of chunkToolCalls) {
524
- const toolCall = call as unknown as {
525
- index?: number;
526
- id?: string;
527
- function?: { name?: string; arguments?: string };
528
- };
529
- const index = toolCall.index ?? 0;
530
- const current = toolCallState[index] ?? { arguments: "" };
531
- if (toolCall.id) current.id = toolCall.id;
532
- if (toolCall.function?.name) current.name = toolCall.function.name;
533
- if (toolCall.function?.arguments) current.arguments += toolCall.function.arguments;
534
- toolCallState[index] = current;
535
- }
536
- if (!firstToolDeltaLogged) {
537
- try {
538
- const first =
539
- toolCallState[
540
- Object.keys(toolCallState)
541
- .map((k) => Number(k))
542
- .sort((a, b) => a - b)[0] ?? 0
543
- ];
544
- logger.info(
545
- { firstCallName: first?.name, hasId: Boolean(first?.id) },
546
- "[mcp] observed streamed tool_call delta"
547
- );
548
- firstToolDeltaLogged = true;
549
- } catch {}
550
- }
551
- }
552
-
553
- const deltaContent = (() => {
554
- if (typeof delta.content === "string") return delta.content;
555
- const maybeParts = delta.content as unknown;
556
- if (Array.isArray(maybeParts)) {
557
- return maybeParts
558
- .map((part) =>
559
- typeof part === "object" &&
560
- part !== null &&
561
- "text" in part &&
562
- typeof (part as Record<string, unknown>).text === "string"
563
- ? String((part as Record<string, unknown>).text)
564
- : ""
565
- )
566
- .join("");
567
- }
568
- return "";
569
- })();
570
-
571
- // Provider-dependent reasoning fields (e.g., `reasoning` or `reasoning_content`).
572
- const deltaReasoning: string =
573
- typeof (delta as unknown as Record<string, unknown>)?.reasoning === "string"
574
- ? ((delta as unknown as { reasoning?: string }).reasoning as string)
575
- : typeof (delta as unknown as Record<string, unknown>)?.reasoning_content === "string"
576
- ? ((delta as unknown as { reasoning_content?: string }).reasoning_content as string)
577
- : "";
578
-
579
- // Merge reasoning + content into a single combined token stream, mirroring
580
- // the OpenAI adapter so the UI can auto-detect <think> blocks.
581
- let combined = "";
582
- if (deltaReasoning.trim().length > 0) {
583
- if (!thinkOpen) {
584
- combined += "<think>" + deltaReasoning;
585
- thinkOpen = true;
586
- } else {
587
- combined += deltaReasoning;
588
- }
589
- }
590
-
591
- if (deltaContent && deltaContent.length > 0) {
592
- if (thinkOpen) {
593
- combined += "</think>" + deltaContent;
594
- thinkOpen = false;
595
- } else {
596
- combined += deltaContent;
597
- }
598
- }
599
-
600
- if (combined.length > 0) {
601
- lastAssistantContent += combined;
602
- if (!sawToolCall) {
603
- streamedContent = true;
604
- yield { type: MessageUpdateType.Stream, token: combined };
605
- tokenCount += combined.length;
606
- }
607
- }
608
-
609
- // Periodic abort check during streaming
610
- if (checkAborted()) {
611
- logger.info({ loop, tokenCount }, "[mcp] aborting during stream");
612
- return "aborted";
613
- }
614
- }
615
- logger.info(
616
- { sawToolCalls: Object.keys(toolCallState).length > 0, tokens: tokenCount, loop },
617
- "[mcp] completion stream closed"
618
- );
619
-
620
- // Check abort after stream completes
621
- if (checkAborted()) {
622
- logger.info({ loop }, "[mcp] aborting after stream completed");
623
- return "aborted";
624
- }
625
-
626
- // Auto-close any unclosed <think> block so reasoning from this loop
627
- // doesn't swallow content from subsequent iterations. The client-side
628
- // regex matches `<think>` to end-of-string, so an unclosed block would
629
- // hide everything that follows.
630
- if (thinkOpen) {
631
- if (streamedContent) {
632
- yield { type: MessageUpdateType.Stream, token: "</think>" };
633
- }
634
- lastAssistantContent += "</think>";
635
- thinkOpen = false;
636
- }
637
-
638
- if (Object.keys(toolCallState).length > 0) {
639
- // If any streamed call is missing id, perform a quick non-stream retry to recover full tool_calls with ids
640
- const missingId = Object.values(toolCallState).some((c) => c?.name && !c?.id);
641
- let calls: NormalizedToolCall[];
642
- if (missingId) {
643
- logger.debug(
644
- { loop },
645
- "[mcp] missing tool_call id in stream; retrying non-stream to recover ids"
646
- );
647
- const nonStream = await openai.chat.completions.create(
648
- { ...completionBase, messages: messagesOpenAI, stream: false },
649
- {
650
- signal: abortSignal,
651
- headers: {
652
- "ChatUI-Conversation-ID": conv._id.toString(),
653
- "X-use-cache": "false",
654
- ...(locals?.token ? { Authorization: `Bearer ${locals.token}` } : {}),
655
- },
656
- }
657
- );
658
- const tc = nonStream.choices?.[0]?.message?.tool_calls ?? [];
659
- calls = tc.map((t) => ({
660
- id: t.id,
661
- name: t.function?.name ?? "",
662
- arguments: t.function?.arguments ?? "",
663
- }));
664
- } else {
665
- calls = Object.values(toolCallState)
666
- .map((c) => (c?.id && c?.name ? c : undefined))
667
- .filter(Boolean)
668
- .map((c) => ({
669
- id: c?.id ?? "",
670
- name: c?.name ?? "",
671
- arguments: c?.arguments ?? "",
672
- })) as NormalizedToolCall[];
673
- }
674
-
675
- // Include the assistant message with tool_calls so the next round
676
- // sees both the calls and their outputs, matching MCP branch behavior.
677
- const toolCalls: ChatCompletionMessageToolCall[] = calls.map((call) => ({
678
- id: call.id,
679
- type: "function",
680
- function: { name: call.name, arguments: call.arguments },
681
- }));
682
-
683
- // Avoid sending <think> content back to the model alongside tool_calls
684
- // to prevent confusing follow-up reasoning. Strip any think blocks.
685
- const assistantContentForToolMsg = lastAssistantContent.replace(
686
- /<think>[\s\S]*?(?:<\/think>|$)/g,
687
- ""
688
- );
689
- const assistantToolMessage: ChatCompletionMessageParam = {
690
- role: "assistant",
691
- content: assistantContentForToolMsg,
692
- tool_calls: toolCalls,
693
- };
694
-
695
- const exec = executeToolCalls({
696
- calls,
697
- mapping,
698
- servers,
699
- parseArgs,
700
- resolveFileRef,
701
- toPrimitive,
702
- processToolOutput,
703
- abortSignal,
704
- });
705
- let toolMsgCount = 0;
706
- let toolRunCount = 0;
707
- for await (const event of exec) {
708
- if (event.type === "update") {
709
- yield event.update;
710
- } else {
711
- messagesOpenAI = [
712
- ...messagesOpenAI,
713
- assistantToolMessage,
714
- ...(event.summary.toolMessages ?? []),
715
- ];
716
- toolMsgCount = event.summary.toolMessages?.length ?? 0;
717
- toolRunCount = event.summary.toolRuns?.length ?? 0;
718
- logger.info(
719
- { toolMsgCount, toolRunCount },
720
- "[mcp] tools executed; continuing loop for follow-up completion"
721
- );
722
- }
723
-
724
- // Check abort during tool execution
725
- if (checkAborted()) {
726
- logger.info({ loop, toolMsgCount }, "[mcp] aborting during tool execution");
727
- return "aborted";
728
- }
729
- }
730
-
731
- // Check abort after all tools complete before continuing loop
732
- if (checkAborted()) {
733
- logger.info({ loop }, "[mcp] aborting after tool execution");
734
- return "aborted";
735
- }
736
- // Emit autopilot step event so the UI can show progress
737
- if (autopilot) {
738
- yield {
739
- type: MessageUpdateType.AutopilotStep,
740
- step: loop + 1,
741
- maxSteps: maxLoops,
742
- toolCount: toolRunCount,
743
- };
744
- }
745
- // Continue loop: next iteration will use tool messages to get the final content
746
- continue;
747
- }
748
-
749
- // No tool calls in this iteration
750
- // If a <think> block is still open, close it for the final output
751
- if (thinkOpen) {
752
- lastAssistantContent += "</think>";
753
- thinkOpen = false;
754
- }
755
-
756
- // Autopilot auto-continue: if the model stopped to ask a question or
757
- // explain what it plans to do instead of calling tools, re-prompt it
758
- // to continue executing autonomously.
759
- if (autopilot && loop < maxLoops - 1) {
760
- const trimmed = lastAssistantContent.replace(/<think>[\s\S]*?(?:<\/think>|$)/g, "").trim();
761
- const looksLikeQuestion =
762
- trimmed.endsWith("?") ||
763
- /\b(shall I|should I|would you like|do you want|let me know|can I)\b/i.test(trimmed);
764
- const looksLikePartial =
765
- /\b(first|next|then|now I'll|I will|let me|I'm going to|here's my plan)\b/i.test(trimmed);
766
-
767
- if (looksLikeQuestion || looksLikePartial) {
768
- // Stream the partial content so user sees what the model said
769
- if (!streamedContent && trimmed.length > 0) {
770
- yield { type: MessageUpdateType.Stream, token: lastAssistantContent };
771
- }
772
- // Add the assistant's response and a continuation prompt
773
- messagesOpenAI = [
774
- ...messagesOpenAI,
775
- { role: "assistant", content: lastAssistantContent },
776
- {
777
- role: "user",
778
- content:
779
- "Continue executing autonomously. Do not ask questions — make reasonable assumptions and use tools to complete the task. Proceed now.",
780
- },
781
- ];
782
- logger.info(
783
- { loop, looksLikeQuestion, looksLikePartial },
784
- "[mcp] autopilot auto-continue: re-prompting model to keep going"
785
- );
786
- // Emit autopilot step
787
- yield {
788
- type: MessageUpdateType.AutopilotStep,
789
- step: loop + 1,
790
- maxSteps: maxLoops,
791
- toolCount: 0,
792
- };
793
- continue;
794
- }
795
- }
796
-
797
- if (!streamedContent && lastAssistantContent.trim().length > 0) {
798
- yield { type: MessageUpdateType.Stream, token: lastAssistantContent };
799
- }
800
- yield {
801
- type: MessageUpdateType.FinalAnswer,
802
- text: lastAssistantContent,
803
- interrupted: false,
804
- };
805
- logger.info(
806
- { length: lastAssistantContent.length, loop },
807
- "[mcp] final answer emitted (no tool_calls)"
808
- );
809
- return "completed";
810
- }
811
- logger.warn({}, "[mcp] exceeded tool-followup loops; falling back");
812
- } catch (err) {
813
- const msg = String(err ?? "");
814
- const isAbort =
815
- (abortSignal && abortSignal.aborted) ||
816
- msg.includes("AbortError") ||
817
- msg.includes("APIUserAbortError") ||
818
- msg.includes("Request was aborted");
819
- if (isAbort) {
820
- // Expected on user stop; keep logs quiet and do not treat as error
821
- logger.debug({}, "[mcp] aborted by user");
822
- return "aborted";
823
- }
824
- logger.warn({ err: msg }, "[mcp] flow failed, falling back to default endpoint");
825
- } finally {
826
- // ensure MCP clients are closed after the turn
827
- await drainPool();
828
- }
829
-
830
- return "not_applicable";
831
- }
1
+ import { config } from "$lib/server/config";
2
+ import { MessageUpdateType, type MessageUpdate } from "$lib/types/MessageUpdate";
3
+ import { getMcpServers } from "$lib/server/mcp/registry";
4
+ import { isValidUrl } from "$lib/server/urlSafety";
5
+ import { resetMcpToolsCache } from "$lib/server/mcp/tools";
6
+ import { getOpenAiToolsForMcp } from "$lib/server/mcp/tools";
7
+ import type {
8
+ ChatCompletionChunk,
9
+ ChatCompletionCreateParamsStreaming,
10
+ ChatCompletionMessageParam,
11
+ ChatCompletionMessageToolCall,
12
+ } from "openai/resources/chat/completions";
13
+ import type { Stream } from "openai/streaming";
14
+ import { buildToolPreprompt } from "../utils/toolPrompt";
15
+ import type { EndpointMessage } from "../../endpoints/endpoints";
16
+ import { resolveRouterTarget } from "./routerResolution";
17
+ import { executeToolCalls, type NormalizedToolCall } from "./toolInvocation";
18
+ import { drainPool } from "$lib/server/mcp/clientPool";
19
+ import type { TextGenerationContext } from "../types";
20
+ import {
21
+ hasAuthHeader,
22
+ isStrictHfMcpLogin,
23
+ hasNonEmptyToken,
24
+ isExaMcpServer,
25
+ } from "$lib/server/mcp/hf";
26
+ import { buildImageRefResolver } from "./fileRefs";
27
+ import { prepareMessagesWithFiles } from "$lib/server/textGeneration/utils/prepareFiles";
28
+ import { makeImageProcessor } from "$lib/server/endpoints/images";
29
+ import { logger } from "$lib/server/logger";
30
+ import { AbortedGenerations } from "$lib/server/abortedGenerations";
31
+
32
+ export type RunMcpFlowContext = Pick<
33
+ TextGenerationContext,
34
+ "model" | "conv" | "assistant" | "forceMultimodal" | "forceTools" | "provider" | "locals"
35
+ > & { messages: EndpointMessage[] };
36
+
37
+ // Return type: "completed" = MCP ran successfully, "not_applicable" = MCP didn't run, "aborted" = user aborted
38
+ export type McpFlowResult = "completed" | "not_applicable" | "aborted";
39
+
40
+ export async function* runMcpFlow({
41
+ model,
42
+ conv,
43
+ messages,
44
+ assistant,
45
+ forceMultimodal,
46
+ forceTools,
47
+ provider,
48
+ locals,
49
+ preprompt,
50
+ abortSignal,
51
+ abortController,
52
+ promptedAt,
53
+ autopilot,
54
+ autopilotMaxSteps,
55
+ }: RunMcpFlowContext & {
56
+ preprompt?: string;
57
+ abortSignal?: AbortSignal;
58
+ abortController?: AbortController;
59
+ promptedAt?: Date;
60
+ autopilot?: boolean;
61
+ autopilotMaxSteps?: number;
62
+ }): AsyncGenerator<MessageUpdate, McpFlowResult, undefined> {
63
+ // Helper to check if generation should be aborted via DB polling
64
+ // Also triggers the abort controller to cancel active streams/requests
65
+ const checkAborted = (): boolean => {
66
+ if (abortSignal?.aborted) return true;
67
+ const abortTime = AbortedGenerations.getInstance().getAbortTime(conv._id.toString());
68
+ if (abortTime && promptedAt && abortTime > promptedAt) {
69
+ // Trigger the abort controller to cancel active streams
70
+ if (abortController && !abortController.signal.aborted) {
71
+ abortController.abort();
72
+ }
73
+ return true;
74
+ }
75
+ return false;
76
+ };
77
+ // Start from env-configured servers
78
+ let servers = getMcpServers();
79
+ try {
80
+ logger.debug(
81
+ { baseServers: servers.map((s) => ({ name: s.name, url: s.url })), count: servers.length },
82
+ "[mcp] base servers loaded"
83
+ );
84
+ } catch {}
85
+
86
+ // Merge in request-provided custom servers (if any)
87
+ try {
88
+ const reqMcp = (
89
+ locals as unknown as {
90
+ mcp?: {
91
+ selectedServers?: Array<{ name: string; url: string; headers?: Record<string, string> }>;
92
+ selectedServerNames?: string[];
93
+ };
94
+ }
95
+ )?.mcp;
96
+ const custom = Array.isArray(reqMcp?.selectedServers) ? reqMcp?.selectedServers : [];
97
+ if (custom.length > 0) {
98
+ // Invalidate cached tool list when the set of servers changes at request-time
99
+ resetMcpToolsCache();
100
+ // Deduplicate by server name (request takes precedence)
101
+ const byName = new Map<
102
+ string,
103
+ { name: string; url: string; headers?: Record<string, string> }
104
+ >();
105
+ for (const s of servers) byName.set(s.name, s);
106
+ for (const s of custom) byName.set(s.name, s);
107
+ servers = [...byName.values()];
108
+ try {
109
+ logger.debug(
110
+ {
111
+ customProvidedCount: custom.length,
112
+ mergedServers: servers.map((s) => ({
113
+ name: s.name,
114
+ url: s.url,
115
+ hasAuth: !!s.headers?.Authorization,
116
+ })),
117
+ },
118
+ "[mcp] merged request-provided servers"
119
+ );
120
+ } catch {}
121
+ }
122
+
123
+ // If the client specified a selection by name, filter to those
124
+ const names = Array.isArray(reqMcp?.selectedServerNames)
125
+ ? reqMcp?.selectedServerNames
126
+ : undefined;
127
+ if (Array.isArray(names)) {
128
+ const before = servers.map((s) => s.name);
129
+ servers = servers.filter((s) => names.includes(s.name));
130
+ try {
131
+ logger.debug(
132
+ { selectedNames: names, before, after: servers.map((s) => s.name) },
133
+ "[mcp] applied name selection"
134
+ );
135
+ } catch {}
136
+ }
137
+ } catch {
138
+ // ignore selection merge errors and proceed with env servers
139
+ }
140
+
141
+ // If selection/merge yielded no servers, bail early with clearer log
142
+ if (servers.length === 0) {
143
+ logger.warn({}, "[mcp] no MCP servers selected after merge/name filter");
144
+ return "not_applicable";
145
+ }
146
+
147
+ // Enforce server-side safety (public HTTPS only, no private ranges)
148
+ {
149
+ const before = servers.slice();
150
+ servers = servers.filter((s) => {
151
+ try {
152
+ return isValidUrl(s.url);
153
+ } catch {
154
+ return false;
155
+ }
156
+ });
157
+ try {
158
+ const rejected = before.filter((b) => !servers.includes(b));
159
+ if (rejected.length > 0) {
160
+ logger.warn(
161
+ { rejected: rejected.map((r) => ({ name: r.name, url: r.url })) },
162
+ "[mcp] rejected servers by URL safety"
163
+ );
164
+ }
165
+ } catch {}
166
+ }
167
+ if (servers.length === 0) {
168
+ logger.warn({}, "[mcp] all selected MCP servers rejected by URL safety guard");
169
+ return "not_applicable";
170
+ }
171
+
172
+ // Optionally attach the logged-in user's HF token to the official HF MCP server only.
173
+ // Never override an explicit Authorization header, and require token to look like an HF token.
174
+ try {
175
+ const shouldForward = config.MCP_FORWARD_HF_USER_TOKEN === "true";
176
+ const userToken =
177
+ (locals as unknown as { hfAccessToken?: string } | undefined)?.hfAccessToken ??
178
+ (locals as unknown as { token?: string } | undefined)?.token;
179
+
180
+ if (shouldForward && hasNonEmptyToken(userToken)) {
181
+ const overlayApplied: string[] = [];
182
+ servers = servers.map((s) => {
183
+ try {
184
+ if (isStrictHfMcpLogin(s.url) && !hasAuthHeader(s.headers)) {
185
+ overlayApplied.push(s.name);
186
+ return {
187
+ ...s,
188
+ headers: { ...(s.headers ?? {}), Authorization: `Bearer ${userToken}` },
189
+ };
190
+ }
191
+ } catch {
192
+ // ignore URL parse errors and leave server unchanged
193
+ }
194
+ return s;
195
+ });
196
+ if (overlayApplied.length > 0) {
197
+ try {
198
+ logger.debug({ overlayApplied }, "[mcp] forwarded HF token to servers");
199
+ } catch {}
200
+ }
201
+ }
202
+ } catch {
203
+ // best-effort overlay; continue if anything goes wrong
204
+ }
205
+
206
+ // Inject Exa API key for mcp.exa.ai servers via URL param (mcp.exa.ai doesn't support headers)
207
+ try {
208
+ const exaApiKey = config.EXA_API_KEY;
209
+ if (hasNonEmptyToken(exaApiKey)) {
210
+ const overlayApplied: string[] = [];
211
+ servers = servers.map((s) => {
212
+ try {
213
+ if (isExaMcpServer(s.url)) {
214
+ const url = new URL(s.url);
215
+ if (!url.searchParams.has("exaApiKey")) {
216
+ url.searchParams.set("exaApiKey", exaApiKey);
217
+ overlayApplied.push(s.name);
218
+ return { ...s, url: url.toString() };
219
+ }
220
+ }
221
+ } catch {}
222
+ return s;
223
+ });
224
+ if (overlayApplied.length > 0) {
225
+ logger.debug({ overlayApplied }, "[mcp] injected Exa API key to servers");
226
+ }
227
+ }
228
+ } catch {
229
+ // best-effort injection; continue if anything goes wrong
230
+ }
231
+
232
+ logger.debug(
233
+ { count: servers.length, servers: servers.map((s) => s.name) },
234
+ "[mcp] servers configured"
235
+ );
236
+ if (servers.length === 0) {
237
+ return "not_applicable";
238
+ }
239
+
240
+ // Gate MCP flow based on model tool support (aggregated) with user override
241
+ try {
242
+ const supportsTools = Boolean((model as unknown as { supportsTools?: boolean }).supportsTools);
243
+ const toolsEnabled = Boolean(forceTools) || supportsTools;
244
+ logger.debug(
245
+ {
246
+ model: model.id ?? model.name,
247
+ supportsTools,
248
+ forceTools: Boolean(forceTools),
249
+ toolsEnabled,
250
+ },
251
+ "[mcp] tools gate evaluation"
252
+ );
253
+ if (!toolsEnabled) {
254
+ logger.info(
255
+ { model: model.id ?? model.name },
256
+ "[mcp] tools disabled for model; skipping MCP flow"
257
+ );
258
+ return "not_applicable";
259
+ }
260
+ } catch {
261
+ // If anything goes wrong reading the flag, proceed (previous behavior)
262
+ }
263
+
264
+ const resolveFileRef = buildImageRefResolver(messages);
265
+ const imageProcessor = makeImageProcessor({
266
+ supportedMimeTypes: ["image/png", "image/jpeg"],
267
+ preferredMimeType: "image/jpeg",
268
+ maxSizeInMB: 1,
269
+ maxWidth: 1024,
270
+ maxHeight: 1024,
271
+ });
272
+
273
+ const hasImageInput = messages.some((msg) =>
274
+ (msg.files ?? []).some(
275
+ (file) => typeof file?.mime === "string" && file.mime.startsWith("image/")
276
+ )
277
+ );
278
+
279
+ const { runMcp, targetModel, candidateModelId, resolvedRoute } = await resolveRouterTarget({
280
+ model,
281
+ messages,
282
+ conversationId: conv._id.toString(),
283
+ hasImageInput,
284
+ locals,
285
+ });
286
+
287
+ if (!runMcp) {
288
+ logger.info(
289
+ { model: targetModel.id ?? targetModel.name, resolvedRoute },
290
+ "[mcp] runMcp=false (routing chose non-tools candidate)"
291
+ );
292
+ return "not_applicable";
293
+ }
294
+
295
+ try {
296
+ const { tools: oaTools, mapping } = await getOpenAiToolsForMcp(servers, {
297
+ signal: abortSignal,
298
+ });
299
+ try {
300
+ logger.info(
301
+ { toolCount: oaTools.length, toolNames: oaTools.map((t) => t.function.name) },
302
+ "[mcp] openai tool defs built"
303
+ );
304
+ } catch {}
305
+ if (oaTools.length === 0) {
306
+ logger.warn({}, "[mcp] zero tools available after listing; skipping MCP flow");
307
+ return "not_applicable";
308
+ }
309
+
310
+ const { OpenAI } = await import("openai");
311
+
312
+ // Capture provider header (x-inference-provider) from the upstream OpenAI-compatible server.
313
+ let providerHeader: string | undefined;
314
+ const captureProviderFetch = async (
315
+ input: RequestInfo | URL,
316
+ init?: RequestInit
317
+ ): Promise<Response> => {
318
+ const res = await fetch(input, init);
319
+ const p = res.headers.get("x-inference-provider");
320
+ if (p && !providerHeader) providerHeader = p;
321
+ return res;
322
+ };
323
+
324
+ const openai = new OpenAI({
325
+ apiKey: config.OPENAI_API_KEY || config.HF_TOKEN || "sk-",
326
+ baseURL: config.OPENAI_BASE_URL,
327
+ fetch: captureProviderFetch,
328
+ defaultHeaders: {
329
+ // Bill to organization if configured (HuggingChat only)
330
+ ...(config.isHuggingChat && locals?.billingOrganization
331
+ ? { "X-HF-Bill-To": locals.billingOrganization }
332
+ : {}),
333
+ },
334
+ });
335
+
336
+ const mmEnabled = (forceMultimodal ?? false) || targetModel.multimodal;
337
+ logger.info(
338
+ {
339
+ targetModel: targetModel.id ?? targetModel.name,
340
+ mmEnabled,
341
+ route: resolvedRoute,
342
+ candidateModelId,
343
+ toolCount: oaTools.length,
344
+ hasUserToken: Boolean((locals as unknown as { token?: string })?.token),
345
+ },
346
+ "[mcp] starting completion with tools"
347
+ );
348
+ let messagesOpenAI: ChatCompletionMessageParam[] = await prepareMessagesWithFiles(
349
+ messages,
350
+ imageProcessor,
351
+ mmEnabled
352
+ );
353
+ const toolPreprompt = buildToolPreprompt(oaTools, autopilot);
354
+ const prepromptPieces: string[] = [];
355
+ if (toolPreprompt.trim().length > 0) {
356
+ prepromptPieces.push(toolPreprompt);
357
+ }
358
+ if (typeof preprompt === "string" && preprompt.trim().length > 0) {
359
+ prepromptPieces.push(preprompt);
360
+ }
361
+ const mergedPreprompt = prepromptPieces.join("\n\n");
362
+ const hasSystemMessage = messagesOpenAI.length > 0 && messagesOpenAI[0]?.role === "system";
363
+ if (hasSystemMessage) {
364
+ if (mergedPreprompt.length > 0) {
365
+ const existing = messagesOpenAI[0].content ?? "";
366
+ const existingText = typeof existing === "string" ? existing : "";
367
+ messagesOpenAI[0].content = mergedPreprompt + (existingText ? "\n\n" + existingText : "");
368
+ }
369
+ } else if (mergedPreprompt.length > 0) {
370
+ messagesOpenAI = [{ role: "system", content: mergedPreprompt }, ...messagesOpenAI];
371
+ }
372
+
373
+ // Work around servers that reject `system` role
374
+ if (
375
+ typeof config.OPENAI_BASE_URL === "string" &&
376
+ config.OPENAI_BASE_URL.length > 0 &&
377
+ (config.OPENAI_BASE_URL.includes("hf.space") ||
378
+ config.OPENAI_BASE_URL.includes("gradio.app")) &&
379
+ messagesOpenAI[0]?.role === "system"
380
+ ) {
381
+ messagesOpenAI[0] = { ...messagesOpenAI[0], role: "user" };
382
+ }
383
+
384
+ const parameters = { ...targetModel.parameters, ...assistant?.generateSettings } as Record<
385
+ string,
386
+ unknown
387
+ >;
388
+ const maxTokens =
389
+ (parameters?.max_tokens as number | undefined) ??
390
+ (parameters?.max_new_tokens as number | undefined) ??
391
+ (parameters?.max_completion_tokens as number | undefined);
392
+
393
+ const stopSequences =
394
+ typeof parameters?.stop === "string"
395
+ ? parameters.stop
396
+ : Array.isArray(parameters?.stop)
397
+ ? (parameters.stop as string[])
398
+ : undefined;
399
+
400
+ // Build model ID with optional provider suffix (e.g., "model:fastest" or "model:together")
401
+ const baseModelId = targetModel.id ?? targetModel.name;
402
+ const modelIdWithProvider =
403
+ provider && provider !== "auto" ? `${baseModelId}:${provider}` : baseModelId;
404
+
405
+ const completionBase: Omit<ChatCompletionCreateParamsStreaming, "messages"> = {
406
+ model: modelIdWithProvider,
407
+ stream: true,
408
+ temperature: typeof parameters?.temperature === "number" ? parameters.temperature : undefined,
409
+ top_p: typeof parameters?.top_p === "number" ? parameters.top_p : undefined,
410
+ frequency_penalty:
411
+ typeof parameters?.frequency_penalty === "number"
412
+ ? parameters.frequency_penalty
413
+ : typeof parameters?.repetition_penalty === "number"
414
+ ? parameters.repetition_penalty
415
+ : undefined,
416
+ presence_penalty:
417
+ typeof parameters?.presence_penalty === "number" ? parameters.presence_penalty : undefined,
418
+ stop: stopSequences,
419
+ max_tokens: typeof maxTokens === "number" ? maxTokens : undefined,
420
+ tools: oaTools,
421
+ tool_choice: "auto",
422
+ };
423
+
424
+ const toPrimitive = (value: unknown) => {
425
+ if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") {
426
+ return value;
427
+ }
428
+ return undefined;
429
+ };
430
+
431
+ const parseArgs = (raw: unknown): Record<string, unknown> => {
432
+ if (typeof raw !== "string" || raw.trim().length === 0) return {};
433
+ try {
434
+ return JSON.parse(raw);
435
+ } catch {
436
+ return {};
437
+ }
438
+ };
439
+
440
+ const processToolOutput = (
441
+ text: string
442
+ ): {
443
+ annotated: string;
444
+ sources: { index: number; link: string }[];
445
+ } => ({ annotated: text, sources: [] });
446
+
447
+ let lastAssistantContent = "";
448
+ let streamedContent = false;
449
+ // Track whether we're inside a <think> block when the upstream streams
450
+ // provider-specific reasoning tokens (e.g. `reasoning` or `reasoning_content`).
451
+ let thinkOpen = false;
452
+
453
+ if (resolvedRoute && candidateModelId) {
454
+ yield {
455
+ type: MessageUpdateType.RouterMetadata,
456
+ route: resolvedRoute,
457
+ model: candidateModelId,
458
+ };
459
+ logger.debug(
460
+ { route: resolvedRoute, model: candidateModelId },
461
+ "[mcp] router metadata emitted"
462
+ );
463
+ }
464
+
465
+ // Autopilot loop cap is user-configurable (Settings.autopilotMaxSteps);
466
+ // fall back to 30 for back-compat when the client doesn't send a value.
467
+ // Non-autopilot mode keeps a tight 10-loop safety net regardless of caller input.
468
+ const autopilotCap =
469
+ typeof autopilotMaxSteps === "number" && autopilotMaxSteps > 0
470
+ ? Math.min(autopilotMaxSteps, 100)
471
+ : 30;
472
+ const maxLoops = autopilot ? autopilotCap : 10;
473
+ for (let loop = 0; loop < maxLoops; loop += 1) {
474
+ // Check for abort at the start of each loop iteration
475
+ if (checkAborted()) {
476
+ logger.info({ loop }, "[mcp] aborting at start of loop iteration");
477
+ return "aborted";
478
+ }
479
+
480
+ lastAssistantContent = "";
481
+ streamedContent = false;
482
+
483
+ const completionRequest: ChatCompletionCreateParamsStreaming = {
484
+ ...completionBase,
485
+ messages: messagesOpenAI,
486
+ };
487
+
488
+ const completionStream: Stream<ChatCompletionChunk> = await openai.chat.completions.create(
489
+ completionRequest,
490
+ {
491
+ signal: abortSignal,
492
+ headers: {
493
+ "ChatUI-Conversation-ID": conv._id.toString(),
494
+ "X-use-cache": "false",
495
+ ...(locals?.token ? { Authorization: `Bearer ${locals.token}` } : {}),
496
+ },
497
+ }
498
+ );
499
+
500
+ // If provider header was exposed, notify UI so it can render "via {provider}".
501
+ if (providerHeader) {
502
+ yield {
503
+ type: MessageUpdateType.RouterMetadata,
504
+ route: "",
505
+ model: "",
506
+ provider: providerHeader as unknown as import("@huggingface/inference").InferenceProvider,
507
+ };
508
+ logger.debug({ provider: providerHeader }, "[mcp] provider metadata emitted");
509
+ }
510
+
511
+ const toolCallState: Record<number, { id?: string; name?: string; arguments: string }> = {};
512
+ let firstToolDeltaLogged = false;
513
+ let sawToolCall = false;
514
+ let tokenCount = 0;
515
+ for await (const chunk of completionStream) {
516
+ const choice = chunk.choices?.[0];
517
+ const delta = choice?.delta;
518
+ if (!delta) continue;
519
+
520
+ const chunkToolCalls = delta.tool_calls ?? [];
521
+ if (chunkToolCalls.length > 0) {
522
+ sawToolCall = true;
523
+ for (const call of chunkToolCalls) {
524
+ const toolCall = call as unknown as {
525
+ index?: number;
526
+ id?: string;
527
+ function?: { name?: string; arguments?: string };
528
+ };
529
+ const index = toolCall.index ?? 0;
530
+ const current = toolCallState[index] ?? { arguments: "" };
531
+ if (toolCall.id) current.id = toolCall.id;
532
+ if (toolCall.function?.name) current.name = toolCall.function.name;
533
+ if (toolCall.function?.arguments) current.arguments += toolCall.function.arguments;
534
+ toolCallState[index] = current;
535
+ }
536
+ if (!firstToolDeltaLogged) {
537
+ try {
538
+ const first =
539
+ toolCallState[
540
+ Object.keys(toolCallState)
541
+ .map((k) => Number(k))
542
+ .sort((a, b) => a - b)[0] ?? 0
543
+ ];
544
+ logger.info(
545
+ { firstCallName: first?.name, hasId: Boolean(first?.id) },
546
+ "[mcp] observed streamed tool_call delta"
547
+ );
548
+ firstToolDeltaLogged = true;
549
+ } catch {}
550
+ }
551
+ }
552
+
553
+ const deltaContent = (() => {
554
+ if (typeof delta.content === "string") return delta.content;
555
+ const maybeParts = delta.content as unknown;
556
+ if (Array.isArray(maybeParts)) {
557
+ return maybeParts
558
+ .map((part) =>
559
+ typeof part === "object" &&
560
+ part !== null &&
561
+ "text" in part &&
562
+ typeof (part as Record<string, unknown>).text === "string"
563
+ ? String((part as Record<string, unknown>).text)
564
+ : ""
565
+ )
566
+ .join("");
567
+ }
568
+ return "";
569
+ })();
570
+
571
+ // Provider-dependent reasoning fields (e.g., `reasoning` or `reasoning_content`).
572
+ const deltaReasoning: string =
573
+ typeof (delta as unknown as Record<string, unknown>)?.reasoning === "string"
574
+ ? ((delta as unknown as { reasoning?: string }).reasoning as string)
575
+ : typeof (delta as unknown as Record<string, unknown>)?.reasoning_content === "string"
576
+ ? ((delta as unknown as { reasoning_content?: string }).reasoning_content as string)
577
+ : "";
578
+
579
+ // Merge reasoning + content into a single combined token stream, mirroring
580
+ // the OpenAI adapter so the UI can auto-detect <think> blocks.
581
+ let combined = "";
582
+ if (deltaReasoning.trim().length > 0) {
583
+ if (!thinkOpen) {
584
+ combined += "<think>" + deltaReasoning;
585
+ thinkOpen = true;
586
+ } else {
587
+ combined += deltaReasoning;
588
+ }
589
+ }
590
+
591
+ if (deltaContent && deltaContent.length > 0) {
592
+ if (thinkOpen) {
593
+ combined += "</think>" + deltaContent;
594
+ thinkOpen = false;
595
+ } else {
596
+ combined += deltaContent;
597
+ }
598
+ }
599
+
600
+ if (combined.length > 0) {
601
+ lastAssistantContent += combined;
602
+ if (!sawToolCall) {
603
+ streamedContent = true;
604
+ yield { type: MessageUpdateType.Stream, token: combined };
605
+ tokenCount += combined.length;
606
+ }
607
+ }
608
+
609
+ // Periodic abort check during streaming
610
+ if (checkAborted()) {
611
+ logger.info({ loop, tokenCount }, "[mcp] aborting during stream");
612
+ return "aborted";
613
+ }
614
+ }
615
+ logger.info(
616
+ { sawToolCalls: Object.keys(toolCallState).length > 0, tokens: tokenCount, loop },
617
+ "[mcp] completion stream closed"
618
+ );
619
+
620
+ // Check abort after stream completes
621
+ if (checkAborted()) {
622
+ logger.info({ loop }, "[mcp] aborting after stream completed");
623
+ return "aborted";
624
+ }
625
+
626
+ // Auto-close any unclosed <think> block so reasoning from this loop
627
+ // doesn't swallow content from subsequent iterations. The client-side
628
+ // regex matches `<think>` to end-of-string, so an unclosed block would
629
+ // hide everything that follows.
630
+ if (thinkOpen) {
631
+ if (streamedContent) {
632
+ yield { type: MessageUpdateType.Stream, token: "</think>" };
633
+ }
634
+ lastAssistantContent += "</think>";
635
+ thinkOpen = false;
636
+ }
637
+
638
+ if (Object.keys(toolCallState).length > 0) {
639
+ // If any streamed call is missing id, perform a quick non-stream retry to recover full tool_calls with ids
640
+ const missingId = Object.values(toolCallState).some((c) => c?.name && !c?.id);
641
+ let calls: NormalizedToolCall[];
642
+ if (missingId) {
643
+ logger.debug(
644
+ { loop },
645
+ "[mcp] missing tool_call id in stream; retrying non-stream to recover ids"
646
+ );
647
+ const nonStream = await openai.chat.completions.create(
648
+ { ...completionBase, messages: messagesOpenAI, stream: false },
649
+ {
650
+ signal: abortSignal,
651
+ headers: {
652
+ "ChatUI-Conversation-ID": conv._id.toString(),
653
+ "X-use-cache": "false",
654
+ ...(locals?.token ? { Authorization: `Bearer ${locals.token}` } : {}),
655
+ },
656
+ }
657
+ );
658
+ const tc = nonStream.choices?.[0]?.message?.tool_calls ?? [];
659
+ calls = tc.map((t) => ({
660
+ id: t.id,
661
+ name: t.function?.name ?? "",
662
+ arguments: t.function?.arguments ?? "",
663
+ }));
664
+ } else {
665
+ calls = Object.values(toolCallState)
666
+ .map((c) => (c?.id && c?.name ? c : undefined))
667
+ .filter(Boolean)
668
+ .map((c) => ({
669
+ id: c?.id ?? "",
670
+ name: c?.name ?? "",
671
+ arguments: c?.arguments ?? "",
672
+ })) as NormalizedToolCall[];
673
+ }
674
+
675
+ // Include the assistant message with tool_calls so the next round
676
+ // sees both the calls and their outputs, matching MCP branch behavior.
677
+ const toolCalls: ChatCompletionMessageToolCall[] = calls.map((call) => ({
678
+ id: call.id,
679
+ type: "function",
680
+ function: { name: call.name, arguments: call.arguments },
681
+ }));
682
+
683
+ // Avoid sending <think> content back to the model alongside tool_calls
684
+ // to prevent confusing follow-up reasoning. Strip any think blocks.
685
+ const assistantContentForToolMsg = lastAssistantContent.replace(
686
+ /<think>[\s\S]*?(?:<\/think>|$)/g,
687
+ ""
688
+ );
689
+ const assistantToolMessage: ChatCompletionMessageParam = {
690
+ role: "assistant",
691
+ content: assistantContentForToolMsg,
692
+ tool_calls: toolCalls,
693
+ };
694
+
695
+ const exec = executeToolCalls({
696
+ calls,
697
+ mapping,
698
+ servers,
699
+ parseArgs,
700
+ resolveFileRef,
701
+ toPrimitive,
702
+ processToolOutput,
703
+ abortSignal,
704
+ });
705
+ let toolMsgCount = 0;
706
+ let toolRunCount = 0;
707
+ for await (const event of exec) {
708
+ if (event.type === "update") {
709
+ yield event.update;
710
+ } else {
711
+ messagesOpenAI = [
712
+ ...messagesOpenAI,
713
+ assistantToolMessage,
714
+ ...(event.summary.toolMessages ?? []),
715
+ ];
716
+ toolMsgCount = event.summary.toolMessages?.length ?? 0;
717
+ toolRunCount = event.summary.toolRuns?.length ?? 0;
718
+ logger.info(
719
+ { toolMsgCount, toolRunCount },
720
+ "[mcp] tools executed; continuing loop for follow-up completion"
721
+ );
722
+ }
723
+
724
+ // Check abort during tool execution
725
+ if (checkAborted()) {
726
+ logger.info({ loop, toolMsgCount }, "[mcp] aborting during tool execution");
727
+ return "aborted";
728
+ }
729
+ }
730
+
731
+ // Check abort after all tools complete before continuing loop
732
+ if (checkAborted()) {
733
+ logger.info({ loop }, "[mcp] aborting after tool execution");
734
+ return "aborted";
735
+ }
736
+ // Emit autopilot step event so the UI can show progress
737
+ if (autopilot) {
738
+ yield {
739
+ type: MessageUpdateType.AutopilotStep,
740
+ step: loop + 1,
741
+ maxSteps: maxLoops,
742
+ toolCount: toolRunCount,
743
+ };
744
+ }
745
+ // Continue loop: next iteration will use tool messages to get the final content
746
+ continue;
747
+ }
748
+
749
+ // No tool calls in this iteration
750
+ // If a <think> block is still open, close it for the final output
751
+ if (thinkOpen) {
752
+ lastAssistantContent += "</think>";
753
+ thinkOpen = false;
754
+ }
755
+
756
+ // Autopilot auto-continue: if the model stopped to ask a question or
757
+ // explain what it plans to do instead of calling tools, re-prompt it
758
+ // to continue executing autonomously.
759
+ if (autopilot && loop < maxLoops - 1) {
760
+ const trimmed = lastAssistantContent.replace(/<think>[\s\S]*?(?:<\/think>|$)/g, "").trim();
761
+ const looksLikeQuestion =
762
+ trimmed.endsWith("?") ||
763
+ /\b(shall I|should I|would you like|do you want|let me know|can I)\b/i.test(trimmed);
764
+ const looksLikePartial =
765
+ /\b(first|next|then|now I'll|I will|let me|I'm going to|here's my plan)\b/i.test(trimmed);
766
+
767
+ if (looksLikeQuestion || looksLikePartial) {
768
+ // Stream the partial content so user sees what the model said
769
+ if (!streamedContent && trimmed.length > 0) {
770
+ yield { type: MessageUpdateType.Stream, token: lastAssistantContent };
771
+ }
772
+ // Add the assistant's response and a continuation prompt
773
+ messagesOpenAI = [
774
+ ...messagesOpenAI,
775
+ { role: "assistant", content: lastAssistantContent },
776
+ {
777
+ role: "user",
778
+ content:
779
+ "Continue executing autonomously. Do not ask questions — make reasonable assumptions and use tools to complete the task. Proceed now.",
780
+ },
781
+ ];
782
+ logger.info(
783
+ { loop, looksLikeQuestion, looksLikePartial },
784
+ "[mcp] autopilot auto-continue: re-prompting model to keep going"
785
+ );
786
+ // Emit autopilot step
787
+ yield {
788
+ type: MessageUpdateType.AutopilotStep,
789
+ step: loop + 1,
790
+ maxSteps: maxLoops,
791
+ toolCount: 0,
792
+ };
793
+ continue;
794
+ }
795
+ }
796
+
797
+ if (!streamedContent && lastAssistantContent.trim().length > 0) {
798
+ yield { type: MessageUpdateType.Stream, token: lastAssistantContent };
799
+ }
800
+ yield {
801
+ type: MessageUpdateType.FinalAnswer,
802
+ text: lastAssistantContent,
803
+ interrupted: false,
804
+ };
805
+ logger.info(
806
+ { length: lastAssistantContent.length, loop },
807
+ "[mcp] final answer emitted (no tool_calls)"
808
+ );
809
+ return "completed";
810
+ }
811
+ logger.warn({}, "[mcp] exceeded tool-followup loops; falling back");
812
+ } catch (err) {
813
+ const msg = String(err ?? "");
814
+ const isAbort =
815
+ (abortSignal && abortSignal.aborted) ||
816
+ msg.includes("AbortError") ||
817
+ msg.includes("APIUserAbortError") ||
818
+ msg.includes("Request was aborted");
819
+ if (isAbort) {
820
+ // Expected on user stop; keep logs quiet and do not treat as error
821
+ logger.debug({}, "[mcp] aborted by user");
822
+ return "aborted";
823
+ }
824
+ logger.warn({ err: msg }, "[mcp] flow failed, falling back to default endpoint");
825
+ } finally {
826
+ // ensure MCP clients are closed after the turn
827
+ await drainPool();
828
+ }
829
+
830
+ return "not_applicable";
831
+ }