@lenne.tech/nest-server 11.25.6 → 11.26.1

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 (340) hide show
  1. package/.claude/rules/configurable-features.md +2 -0
  2. package/CLAUDE.md +18 -0
  3. package/FRAMEWORK-API.md +48 -1
  4. package/dist/config.env.js +35 -1
  5. package/dist/config.env.js.map +1 -1
  6. package/dist/core/common/helpers/config.helper.js +2 -2
  7. package/dist/core/common/helpers/config.helper.js.map +1 -1
  8. package/dist/core/common/interceptors/check-security.interceptor.js +9 -2
  9. package/dist/core/common/interceptors/check-security.interceptor.js.map +1 -1
  10. package/dist/core/common/interfaces/server-options.interface.d.ts +89 -0
  11. package/dist/core/modules/ai/core-ai-mcp.controller.d.ts +21 -0
  12. package/dist/core/modules/ai/core-ai-mcp.controller.js +191 -0
  13. package/dist/core/modules/ai/core-ai-mcp.controller.js.map +1 -0
  14. package/dist/core/modules/ai/core-ai.controller.d.ts +90 -0
  15. package/dist/core/modules/ai/core-ai.controller.js +576 -0
  16. package/dist/core/modules/ai/core-ai.controller.js.map +1 -0
  17. package/dist/core/modules/ai/core-ai.module.d.ts +43 -0
  18. package/dist/core/modules/ai/core-ai.module.js +164 -0
  19. package/dist/core/modules/ai/core-ai.module.js.map +1 -0
  20. package/dist/core/modules/ai/core-ai.resolver.d.ts +84 -0
  21. package/dist/core/modules/ai/core-ai.resolver.js +530 -0
  22. package/dist/core/modules/ai/core-ai.resolver.js.map +1 -0
  23. package/dist/core/modules/ai/helpers/ai-mcp-oauth.helper.d.ts +7 -0
  24. package/dist/core/modules/ai/helpers/ai-mcp-oauth.helper.js +16 -0
  25. package/dist/core/modules/ai/helpers/ai-mcp-oauth.helper.js.map +1 -0
  26. package/dist/core/modules/ai/hooks/ai-hook.base.d.ts +10 -0
  27. package/dist/core/modules/ai/hooks/ai-hook.base.js +30 -0
  28. package/dist/core/modules/ai/hooks/ai-hook.base.js.map +1 -0
  29. package/dist/core/modules/ai/hooks/ai-hook.registry.d.ts +22 -0
  30. package/dist/core/modules/ai/hooks/ai-hook.registry.js +98 -0
  31. package/dist/core/modules/ai/hooks/ai-hook.registry.js.map +1 -0
  32. package/dist/core/modules/ai/index.d.ts +69 -0
  33. package/dist/core/modules/ai/index.js +86 -0
  34. package/dist/core/modules/ai/index.js.map +1 -0
  35. package/dist/core/modules/ai/inputs/core-ai-budget-limit-create.input.d.ts +5 -0
  36. package/dist/core/modules/ai/inputs/core-ai-budget-limit-create.input.js +43 -0
  37. package/dist/core/modules/ai/inputs/core-ai-budget-limit-create.input.js.map +1 -0
  38. package/dist/core/modules/ai/inputs/core-ai-budget-limit.input.d.ts +7 -0
  39. package/dist/core/modules/ai/inputs/core-ai-budget-limit.input.js +74 -0
  40. package/dist/core/modules/ai/inputs/core-ai-budget-limit.input.js.map +1 -0
  41. package/dist/core/modules/ai/inputs/core-ai-connection-create.input.d.ts +6 -0
  42. package/dist/core/modules/ai/inputs/core-ai-connection-create.input.js +49 -0
  43. package/dist/core/modules/ai/inputs/core-ai-connection-create.input.js.map +1 -0
  44. package/dist/core/modules/ai/inputs/core-ai-connection-preference.input.d.ts +6 -0
  45. package/dist/core/modules/ai/inputs/core-ai-connection-preference.input.js +60 -0
  46. package/dist/core/modules/ai/inputs/core-ai-connection-preference.input.js.map +1 -0
  47. package/dist/core/modules/ai/inputs/core-ai-connection.input.d.ts +23 -0
  48. package/dist/core/modules/ai/inputs/core-ai-connection.input.js +229 -0
  49. package/dist/core/modules/ai/inputs/core-ai-connection.input.js.map +1 -0
  50. package/dist/core/modules/ai/inputs/core-ai-conversation-create.input.d.ts +3 -0
  51. package/dist/core/modules/ai/inputs/core-ai-conversation-create.input.js +21 -0
  52. package/dist/core/modules/ai/inputs/core-ai-conversation-create.input.js.map +1 -0
  53. package/dist/core/modules/ai/inputs/core-ai-conversation.input.d.ts +4 -0
  54. package/dist/core/modules/ai/inputs/core-ai-conversation.input.js +42 -0
  55. package/dist/core/modules/ai/inputs/core-ai-conversation.input.js.map +1 -0
  56. package/dist/core/modules/ai/inputs/core-ai-prompt-create.input.d.ts +10 -0
  57. package/dist/core/modules/ai/inputs/core-ai-prompt-create.input.js +74 -0
  58. package/dist/core/modules/ai/inputs/core-ai-prompt-create.input.js.map +1 -0
  59. package/dist/core/modules/ai/inputs/core-ai-prompt-hint-create.input.d.ts +5 -0
  60. package/dist/core/modules/ai/inputs/core-ai-prompt-hint-create.input.js +41 -0
  61. package/dist/core/modules/ai/inputs/core-ai-prompt-hint-create.input.js.map +1 -0
  62. package/dist/core/modules/ai/inputs/core-ai-prompt-hint.input.d.ts +6 -0
  63. package/dist/core/modules/ai/inputs/core-ai-prompt-hint.input.js +63 -0
  64. package/dist/core/modules/ai/inputs/core-ai-prompt-hint.input.js.map +1 -0
  65. package/dist/core/modules/ai/inputs/core-ai-prompt-update.input.d.ts +10 -0
  66. package/dist/core/modules/ai/inputs/core-ai-prompt-update.input.js +70 -0
  67. package/dist/core/modules/ai/inputs/core-ai-prompt-update.input.js.map +1 -0
  68. package/dist/core/modules/ai/inputs/core-ai-prompt.input.d.ts +18 -0
  69. package/dist/core/modules/ai/inputs/core-ai-prompt.input.js +131 -0
  70. package/dist/core/modules/ai/inputs/core-ai-prompt.input.js.map +1 -0
  71. package/dist/core/modules/ai/inputs/core-ai-slot-create.input.d.ts +5 -0
  72. package/dist/core/modules/ai/inputs/core-ai-slot-create.input.js +41 -0
  73. package/dist/core/modules/ai/inputs/core-ai-slot-create.input.js.map +1 -0
  74. package/dist/core/modules/ai/inputs/core-ai-slot-update.input.d.ts +9 -0
  75. package/dist/core/modules/ai/inputs/core-ai-slot-update.input.js +91 -0
  76. package/dist/core/modules/ai/inputs/core-ai-slot-update.input.js.map +1 -0
  77. package/dist/core/modules/ai/interfaces/ai-hook.interface.d.ts +28 -0
  78. package/dist/core/modules/ai/interfaces/ai-hook.interface.js +3 -0
  79. package/dist/core/modules/ai/interfaces/ai-hook.interface.js.map +1 -0
  80. package/dist/core/modules/ai/interfaces/ai-placeholder.interface.d.ts +21 -0
  81. package/dist/core/modules/ai/interfaces/ai-placeholder.interface.js +3 -0
  82. package/dist/core/modules/ai/interfaces/ai-placeholder.interface.js.map +1 -0
  83. package/dist/core/modules/ai/interfaces/ai-tool.interface.d.ts +30 -0
  84. package/dist/core/modules/ai/interfaces/ai-tool.interface.js +3 -0
  85. package/dist/core/modules/ai/interfaces/ai-tool.interface.js.map +1 -0
  86. package/dist/core/modules/ai/interfaces/llm-provider.interface.d.ts +55 -0
  87. package/dist/core/modules/ai/interfaces/llm-provider.interface.js +3 -0
  88. package/dist/core/modules/ai/interfaces/llm-provider.interface.js.map +1 -0
  89. package/dist/core/modules/ai/interfaces/resolved-ai-connection.interface.d.ts +16 -0
  90. package/dist/core/modules/ai/interfaces/resolved-ai-connection.interface.js +3 -0
  91. package/dist/core/modules/ai/interfaces/resolved-ai-connection.interface.js.map +1 -0
  92. package/dist/core/modules/ai/models/core-ai-action.model.d.ts +6 -0
  93. package/dist/core/modules/ai/models/core-ai-action.model.js +44 -0
  94. package/dist/core/modules/ai/models/core-ai-action.model.js.map +1 -0
  95. package/dist/core/modules/ai/models/core-ai-available-connection.model.d.ts +8 -0
  96. package/dist/core/modules/ai/models/core-ai-available-connection.model.js +53 -0
  97. package/dist/core/modules/ai/models/core-ai-available-connection.model.js.map +1 -0
  98. package/dist/core/modules/ai/models/core-ai-budget-limit.model.d.ts +97 -0
  99. package/dist/core/modules/ai/models/core-ai-budget-limit.model.js +79 -0
  100. package/dist/core/modules/ai/models/core-ai-budget-limit.model.js.map +1 -0
  101. package/dist/core/modules/ai/models/core-ai-connection-preference.model.d.ts +91 -0
  102. package/dist/core/modules/ai/models/core-ai-connection-preference.model.js +67 -0
  103. package/dist/core/modules/ai/models/core-ai-connection-preference.model.js.map +1 -0
  104. package/dist/core/modules/ai/models/core-ai-connection.model.d.ts +200 -0
  105. package/dist/core/modules/ai/models/core-ai-connection.model.js +263 -0
  106. package/dist/core/modules/ai/models/core-ai-connection.model.js.map +1 -0
  107. package/dist/core/modules/ai/models/core-ai-conversation.model.d.ts +93 -0
  108. package/dist/core/modules/ai/models/core-ai-conversation.model.js +82 -0
  109. package/dist/core/modules/ai/models/core-ai-conversation.model.js.map +1 -0
  110. package/dist/core/modules/ai/models/core-ai-interaction.model.d.ts +133 -0
  111. package/dist/core/modules/ai/models/core-ai-interaction.model.js +137 -0
  112. package/dist/core/modules/ai/models/core-ai-interaction.model.js.map +1 -0
  113. package/dist/core/modules/ai/models/core-ai-message.model.d.ts +5 -0
  114. package/dist/core/modules/ai/models/core-ai-message.model.js +38 -0
  115. package/dist/core/modules/ai/models/core-ai-message.model.js.map +1 -0
  116. package/dist/core/modules/ai/models/core-ai-mode.model.d.ts +109 -0
  117. package/dist/core/modules/ai/models/core-ai-mode.model.js +100 -0
  118. package/dist/core/modules/ai/models/core-ai-mode.model.js.map +1 -0
  119. package/dist/core/modules/ai/models/core-ai-prompt-hint.model.d.ts +103 -0
  120. package/dist/core/modules/ai/models/core-ai-prompt-hint.model.js +89 -0
  121. package/dist/core/modules/ai/models/core-ai-prompt-hint.model.js.map +1 -0
  122. package/dist/core/modules/ai/models/core-ai-prompt.model.d.ts +122 -0
  123. package/dist/core/modules/ai/models/core-ai-prompt.model.js +117 -0
  124. package/dist/core/modules/ai/models/core-ai-prompt.model.js.map +1 -0
  125. package/dist/core/modules/ai/models/core-ai-response.model.d.ts +29 -0
  126. package/dist/core/modules/ai/models/core-ai-response.model.js +105 -0
  127. package/dist/core/modules/ai/models/core-ai-response.model.js.map +1 -0
  128. package/dist/core/modules/ai/models/core-ai-slot.model.d.ts +121 -0
  129. package/dist/core/modules/ai/models/core-ai-slot.model.js +119 -0
  130. package/dist/core/modules/ai/models/core-ai-slot.model.js.map +1 -0
  131. package/dist/core/modules/ai/models/core-ai-tool-grant.model.d.ts +103 -0
  132. package/dist/core/modules/ai/models/core-ai-tool-grant.model.js +88 -0
  133. package/dist/core/modules/ai/models/core-ai-tool-grant.model.js.map +1 -0
  134. package/dist/core/modules/ai/models/core-ai-tool-policy.model.d.ts +122 -0
  135. package/dist/core/modules/ai/models/core-ai-tool-policy.model.js +121 -0
  136. package/dist/core/modules/ai/models/core-ai-tool-policy.model.js.map +1 -0
  137. package/dist/core/modules/ai/models/core-ai-usage-info.model.d.ts +24 -0
  138. package/dist/core/modules/ai/models/core-ai-usage-info.model.js +127 -0
  139. package/dist/core/modules/ai/models/core-ai-usage-info.model.js.map +1 -0
  140. package/dist/core/modules/ai/models/core-ai-usage.model.d.ts +5 -0
  141. package/dist/core/modules/ai/models/core-ai-usage.model.js +38 -0
  142. package/dist/core/modules/ai/models/core-ai-usage.model.js.map +1 -0
  143. package/dist/core/modules/ai/providers/claude-cli.provider.d.ts +16 -0
  144. package/dist/core/modules/ai/providers/claude-cli.provider.js +151 -0
  145. package/dist/core/modules/ai/providers/claude-cli.provider.js.map +1 -0
  146. package/dist/core/modules/ai/providers/llm-provider.factory.d.ts +10 -0
  147. package/dist/core/modules/ai/providers/llm-provider.factory.js +45 -0
  148. package/dist/core/modules/ai/providers/llm-provider.factory.js.map +1 -0
  149. package/dist/core/modules/ai/providers/openai-compatible.provider.d.ts +32 -0
  150. package/dist/core/modules/ai/providers/openai-compatible.provider.js +236 -0
  151. package/dist/core/modules/ai/providers/openai-compatible.provider.js.map +1 -0
  152. package/dist/core/modules/ai/services/ai-crypto.service.d.ts +11 -0
  153. package/dist/core/modules/ai/services/ai-crypto.service.js +84 -0
  154. package/dist/core/modules/ai/services/ai-crypto.service.js.map +1 -0
  155. package/dist/core/modules/ai/services/core-ai-budget.service.d.ts +42 -0
  156. package/dist/core/modules/ai/services/core-ai-budget.service.js +211 -0
  157. package/dist/core/modules/ai/services/core-ai-budget.service.js.map +1 -0
  158. package/dist/core/modules/ai/services/core-ai-connection-preference.service.d.ts +18 -0
  159. package/dist/core/modules/ai/services/core-ai-connection-preference.service.js +53 -0
  160. package/dist/core/modules/ai/services/core-ai-connection-preference.service.js.map +1 -0
  161. package/dist/core/modules/ai/services/core-ai-connection-resolver.service.d.ts +55 -0
  162. package/dist/core/modules/ai/services/core-ai-connection-resolver.service.js +177 -0
  163. package/dist/core/modules/ai/services/core-ai-connection-resolver.service.js.map +1 -0
  164. package/dist/core/modules/ai/services/core-ai-connection.service.d.ts +43 -0
  165. package/dist/core/modules/ai/services/core-ai-connection.service.js +272 -0
  166. package/dist/core/modules/ai/services/core-ai-connection.service.js.map +1 -0
  167. package/dist/core/modules/ai/services/core-ai-conversation.service.d.ts +26 -0
  168. package/dist/core/modules/ai/services/core-ai-conversation.service.js +72 -0
  169. package/dist/core/modules/ai/services/core-ai-conversation.service.js.map +1 -0
  170. package/dist/core/modules/ai/services/core-ai-interaction.service.d.ts +13 -0
  171. package/dist/core/modules/ai/services/core-ai-interaction.service.js +52 -0
  172. package/dist/core/modules/ai/services/core-ai-interaction.service.js.map +1 -0
  173. package/dist/core/modules/ai/services/core-ai-mcp-client.service.d.ts +51 -0
  174. package/dist/core/modules/ai/services/core-ai-mcp-client.service.js +123 -0
  175. package/dist/core/modules/ai/services/core-ai-mcp-client.service.js.map +1 -0
  176. package/dist/core/modules/ai/services/core-ai-mcp-oauth.service.d.ts +68 -0
  177. package/dist/core/modules/ai/services/core-ai-mcp-oauth.service.js +279 -0
  178. package/dist/core/modules/ai/services/core-ai-mcp-oauth.service.js.map +1 -0
  179. package/dist/core/modules/ai/services/core-ai-mcp.service.d.ts +22 -0
  180. package/dist/core/modules/ai/services/core-ai-mcp.service.js +59 -0
  181. package/dist/core/modules/ai/services/core-ai-mcp.service.js.map +1 -0
  182. package/dist/core/modules/ai/services/core-ai-mode.service.d.ts +14 -0
  183. package/dist/core/modules/ai/services/core-ai-mode.service.js +56 -0
  184. package/dist/core/modules/ai/services/core-ai-mode.service.js.map +1 -0
  185. package/dist/core/modules/ai/services/core-ai-placeholder.registry.d.ts +15 -0
  186. package/dist/core/modules/ai/services/core-ai-placeholder.registry.js +102 -0
  187. package/dist/core/modules/ai/services/core-ai-placeholder.registry.js.map +1 -0
  188. package/dist/core/modules/ai/services/core-ai-prompt-builder.service.d.ts +42 -0
  189. package/dist/core/modules/ai/services/core-ai-prompt-builder.service.js +136 -0
  190. package/dist/core/modules/ai/services/core-ai-prompt-builder.service.js.map +1 -0
  191. package/dist/core/modules/ai/services/core-ai-prompt-hint.service.d.ts +29 -0
  192. package/dist/core/modules/ai/services/core-ai-prompt-hint.service.js +98 -0
  193. package/dist/core/modules/ai/services/core-ai-prompt-hint.service.js.map +1 -0
  194. package/dist/core/modules/ai/services/core-ai-prompt.service.d.ts +21 -0
  195. package/dist/core/modules/ai/services/core-ai-prompt.service.js +143 -0
  196. package/dist/core/modules/ai/services/core-ai-prompt.service.js.map +1 -0
  197. package/dist/core/modules/ai/services/core-ai-slot.service.d.ts +51 -0
  198. package/dist/core/modules/ai/services/core-ai-slot.service.js +372 -0
  199. package/dist/core/modules/ai/services/core-ai-slot.service.js.map +1 -0
  200. package/dist/core/modules/ai/services/core-ai-tool-grant.service.d.ts +22 -0
  201. package/dist/core/modules/ai/services/core-ai-tool-grant.service.js +97 -0
  202. package/dist/core/modules/ai/services/core-ai-tool-grant.service.js.map +1 -0
  203. package/dist/core/modules/ai/services/core-ai-tool-policy.service.d.ts +23 -0
  204. package/dist/core/modules/ai/services/core-ai-tool-policy.service.js +102 -0
  205. package/dist/core/modules/ai/services/core-ai-tool-policy.service.js.map +1 -0
  206. package/dist/core/modules/ai/services/core-ai.service.d.ts +144 -0
  207. package/dist/core/modules/ai/services/core-ai.service.js +991 -0
  208. package/dist/core/modules/ai/services/core-ai.service.js.map +1 -0
  209. package/dist/core/modules/ai/tools/ai-tool.base.d.ts +15 -0
  210. package/dist/core/modules/ai/tools/ai-tool.base.js +30 -0
  211. package/dist/core/modules/ai/tools/ai-tool.base.js.map +1 -0
  212. package/dist/core/modules/ai/tools/ai-tool.registry.d.ts +19 -0
  213. package/dist/core/modules/ai/tools/ai-tool.registry.js +71 -0
  214. package/dist/core/modules/ai/tools/ai-tool.registry.js.map +1 -0
  215. package/dist/core/modules/ai/tools/ask-user-question.tool.d.ts +38 -0
  216. package/dist/core/modules/ai/tools/ask-user-question.tool.js +65 -0
  217. package/dist/core/modules/ai/tools/ask-user-question.tool.js.map +1 -0
  218. package/dist/core/modules/ai/tools/search-tools.tool.d.ts +24 -0
  219. package/dist/core/modules/ai/tools/search-tools.tool.js +68 -0
  220. package/dist/core/modules/ai/tools/search-tools.tool.js.map +1 -0
  221. package/dist/core/modules/better-auth/core-better-auth.controller.d.ts +2 -1
  222. package/dist/core/modules/better-auth/core-better-auth.controller.js +12 -1
  223. package/dist/core/modules/better-auth/core-better-auth.controller.js.map +1 -1
  224. package/dist/core/modules/error-code/error-codes.d.ts +81 -0
  225. package/dist/core/modules/error-code/error-codes.js +72 -0
  226. package/dist/core/modules/error-code/error-codes.js.map +1 -1
  227. package/dist/core.module.js +8 -0
  228. package/dist/core.module.js.map +1 -1
  229. package/dist/index.d.ts +1 -0
  230. package/dist/index.js +1 -0
  231. package/dist/index.js.map +1 -1
  232. package/dist/server/modules/ai/ai-tools.module.d.ts +5 -0
  233. package/dist/server/modules/ai/ai-tools.module.js +36 -0
  234. package/dist/server/modules/ai/ai-tools.module.js.map +1 -0
  235. package/dist/server/modules/ai/tools/delete-user.tool.d.ts +30 -0
  236. package/dist/server/modules/ai/tools/delete-user.tool.js +49 -0
  237. package/dist/server/modules/ai/tools/delete-user.tool.js.map +1 -0
  238. package/dist/server/modules/ai/tools/find-users.tool.d.ts +30 -0
  239. package/dist/server/modules/ai/tools/find-users.tool.js +52 -0
  240. package/dist/server/modules/ai/tools/find-users.tool.js.map +1 -0
  241. package/dist/server/modules/ai/tools/get-user.tool.d.ts +26 -0
  242. package/dist/server/modules/ai/tools/get-user.tool.js +48 -0
  243. package/dist/server/modules/ai/tools/get-user.tool.js.map +1 -0
  244. package/dist/server/modules/ai/tools/update-user-job-title.tool.d.ts +32 -0
  245. package/dist/server/modules/ai/tools/update-user-job-title.tool.js +54 -0
  246. package/dist/server/modules/ai/tools/update-user-job-title.tool.js.map +1 -0
  247. package/dist/server/modules/error-code/error-codes.d.ts +9 -0
  248. package/dist/server/server.module.js +2 -0
  249. package/dist/server/server.module.js.map +1 -1
  250. package/dist/tsconfig.build.tsbuildinfo +1 -1
  251. package/docs/REQUEST-LIFECYCLE.md +10 -0
  252. package/migration-guides/11.25.x-to-11.26.0.md +278 -0
  253. package/migration-guides/11.26.0-to-11.26.1.md +193 -0
  254. package/package.json +2 -1
  255. package/src/config.env.ts +48 -1
  256. package/src/core/common/helpers/config.helper.ts +5 -2
  257. package/src/core/common/interceptors/check-security.interceptor.ts +13 -3
  258. package/src/core/common/interfaces/server-options.interface.ts +312 -0
  259. package/src/core/modules/ai/INTEGRATION-CHECKLIST.md +220 -0
  260. package/src/core/modules/ai/README.md +526 -0
  261. package/src/core/modules/ai/core-ai-mcp.controller.ts +223 -0
  262. package/src/core/modules/ai/core-ai.controller.ts +547 -0
  263. package/src/core/modules/ai/core-ai.module.ts +269 -0
  264. package/src/core/modules/ai/core-ai.resolver.ts +526 -0
  265. package/src/core/modules/ai/helpers/ai-mcp-oauth.helper.ts +35 -0
  266. package/src/core/modules/ai/hooks/ai-hook.base.ts +23 -0
  267. package/src/core/modules/ai/hooks/ai-hook.registry.ts +119 -0
  268. package/src/core/modules/ai/index.ts +70 -0
  269. package/src/core/modules/ai/inputs/core-ai-budget-limit-create.input.ts +27 -0
  270. package/src/core/modules/ai/inputs/core-ai-budget-limit.input.ts +52 -0
  271. package/src/core/modules/ai/inputs/core-ai-connection-create.input.ts +32 -0
  272. package/src/core/modules/ai/inputs/core-ai-connection-preference.input.ts +42 -0
  273. package/src/core/modules/ai/inputs/core-ai-connection.input.ts +235 -0
  274. package/src/core/modules/ai/inputs/core-ai-conversation-create.input.ts +13 -0
  275. package/src/core/modules/ai/inputs/core-ai-conversation.input.ts +33 -0
  276. package/src/core/modules/ai/inputs/core-ai-prompt-create.input.ts +50 -0
  277. package/src/core/modules/ai/inputs/core-ai-prompt-hint-create.input.ts +26 -0
  278. package/src/core/modules/ai/inputs/core-ai-prompt-hint.input.ts +43 -0
  279. package/src/core/modules/ai/inputs/core-ai-prompt-update.input.ts +42 -0
  280. package/src/core/modules/ai/inputs/core-ai-prompt.input.ts +144 -0
  281. package/src/core/modules/ai/inputs/core-ai-slot-create.input.ts +26 -0
  282. package/src/core/modules/ai/inputs/core-ai-slot-update.input.ts +65 -0
  283. package/src/core/modules/ai/interfaces/ai-hook.interface.ts +74 -0
  284. package/src/core/modules/ai/interfaces/ai-placeholder.interface.ts +49 -0
  285. package/src/core/modules/ai/interfaces/ai-tool.interface.ts +112 -0
  286. package/src/core/modules/ai/interfaces/llm-provider.interface.ts +187 -0
  287. package/src/core/modules/ai/interfaces/resolved-ai-connection.interface.ts +55 -0
  288. package/src/core/modules/ai/models/core-ai-action.model.ts +37 -0
  289. package/src/core/modules/ai/models/core-ai-available-connection.model.ts +42 -0
  290. package/src/core/modules/ai/models/core-ai-budget-limit.model.ts +82 -0
  291. package/src/core/modules/ai/models/core-ai-connection-preference.model.ts +72 -0
  292. package/src/core/modules/ai/models/core-ai-connection.model.ts +330 -0
  293. package/src/core/modules/ai/models/core-ai-conversation.model.ts +87 -0
  294. package/src/core/modules/ai/models/core-ai-interaction.model.ts +145 -0
  295. package/src/core/modules/ai/models/core-ai-message.model.ts +29 -0
  296. package/src/core/modules/ai/models/core-ai-mode.model.ts +93 -0
  297. package/src/core/modules/ai/models/core-ai-prompt-hint.model.ts +98 -0
  298. package/src/core/modules/ai/models/core-ai-prompt.model.ts +120 -0
  299. package/src/core/modules/ai/models/core-ai-response.model.ts +119 -0
  300. package/src/core/modules/ai/models/core-ai-slot.model.ts +146 -0
  301. package/src/core/modules/ai/models/core-ai-tool-grant.model.ts +87 -0
  302. package/src/core/modules/ai/models/core-ai-tool-policy.model.ts +115 -0
  303. package/src/core/modules/ai/models/core-ai-usage-info.model.ts +107 -0
  304. package/src/core/modules/ai/models/core-ai-usage.model.ts +29 -0
  305. package/src/core/modules/ai/providers/claude-cli.provider.ts +240 -0
  306. package/src/core/modules/ai/providers/llm-provider.factory.ts +63 -0
  307. package/src/core/modules/ai/providers/openai-compatible.provider.ts +329 -0
  308. package/src/core/modules/ai/services/ai-crypto.service.ts +120 -0
  309. package/src/core/modules/ai/services/core-ai-budget.service.ts +297 -0
  310. package/src/core/modules/ai/services/core-ai-connection-preference.service.ts +74 -0
  311. package/src/core/modules/ai/services/core-ai-connection-resolver.service.ts +308 -0
  312. package/src/core/modules/ai/services/core-ai-connection.service.ts +387 -0
  313. package/src/core/modules/ai/services/core-ai-conversation.service.ts +105 -0
  314. package/src/core/modules/ai/services/core-ai-interaction.service.ts +56 -0
  315. package/src/core/modules/ai/services/core-ai-mcp-client.service.ts +169 -0
  316. package/src/core/modules/ai/services/core-ai-mcp-oauth.service.ts +445 -0
  317. package/src/core/modules/ai/services/core-ai-mcp.service.ts +94 -0
  318. package/src/core/modules/ai/services/core-ai-mode.service.ts +43 -0
  319. package/src/core/modules/ai/services/core-ai-placeholder.registry.ts +123 -0
  320. package/src/core/modules/ai/services/core-ai-prompt-builder.service.ts +228 -0
  321. package/src/core/modules/ai/services/core-ai-prompt-hint.service.ts +133 -0
  322. package/src/core/modules/ai/services/core-ai-prompt.service.ts +164 -0
  323. package/src/core/modules/ai/services/core-ai-slot.service.ts +526 -0
  324. package/src/core/modules/ai/services/core-ai-tool-grant.service.ts +107 -0
  325. package/src/core/modules/ai/services/core-ai-tool-policy.service.ts +124 -0
  326. package/src/core/modules/ai/services/core-ai.service.ts +1396 -0
  327. package/src/core/modules/ai/tools/ai-tool.base.ts +46 -0
  328. package/src/core/modules/ai/tools/ai-tool.registry.ts +124 -0
  329. package/src/core/modules/ai/tools/ask-user-question.tool.ts +76 -0
  330. package/src/core/modules/ai/tools/search-tools.tool.ts +70 -0
  331. package/src/core/modules/better-auth/core-better-auth.controller.ts +20 -3
  332. package/src/core/modules/error-code/error-codes.ts +86 -0
  333. package/src/core.module.ts +12 -0
  334. package/src/index.ts +6 -0
  335. package/src/server/modules/ai/ai-tools.module.ts +33 -0
  336. package/src/server/modules/ai/tools/delete-user.tool.ts +44 -0
  337. package/src/server/modules/ai/tools/find-users.tool.ts +49 -0
  338. package/src/server/modules/ai/tools/get-user.tool.ts +43 -0
  339. package/src/server/modules/ai/tools/update-user-job-title.tool.ts +62 -0
  340. package/src/server/server.module.ts +5 -0
@@ -0,0 +1,177 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var __metadata = (this && this.__metadata) || function (k, v) {
9
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
+ };
11
+ var __param = (this && this.__param) || function (paramIndex, decorator) {
12
+ return function (target, key) { decorator(target, key, paramIndex); }
13
+ };
14
+ var CoreAiConnectionResolverService_1;
15
+ Object.defineProperty(exports, "__esModule", { value: true });
16
+ exports.CoreAiConnectionResolverService = void 0;
17
+ const common_1 = require("@nestjs/common");
18
+ const error_code_1 = require("../../error-code");
19
+ const core_ai_available_connection_model_1 = require("../models/core-ai-available-connection.model");
20
+ const core_ai_connection_preference_service_1 = require("./core-ai-connection-preference.service");
21
+ const core_ai_connection_service_1 = require("./core-ai-connection.service");
22
+ let CoreAiConnectionResolverService = CoreAiConnectionResolverService_1 = class CoreAiConnectionResolverService {
23
+ connectionService;
24
+ preferenceService;
25
+ logger = new common_1.Logger(CoreAiConnectionResolverService_1.name);
26
+ tenantPrefCache = new WeakMap();
27
+ constructor(connectionService, preferenceService) {
28
+ this.connectionService = connectionService;
29
+ this.preferenceService = preferenceService;
30
+ }
31
+ async resolveConnection(ctx) {
32
+ const id = await this.resolveConnectionId(ctx);
33
+ return id ? this.connectionService.resolve(id) : undefined;
34
+ }
35
+ async resolveConnectionId(ctx) {
36
+ const connections = await this.connectionService.listUsable();
37
+ return (await this.resolveFrom(connections, ctx)).selected;
38
+ }
39
+ async listAvailable(ctx) {
40
+ const connections = await this.connectionService.listUsable();
41
+ if (!connections.length) {
42
+ return [];
43
+ }
44
+ const available = this.availableConnections(connections, ctx.tenantId);
45
+ const { locked, selected } = await this.resolveFrom(connections, ctx);
46
+ return available.map((c) => {
47
+ const item = new core_ai_available_connection_model_1.CoreAiAvailableConnection();
48
+ item.id = c.id;
49
+ item.isDefault = !!c.isDefault;
50
+ item.locked = locked;
51
+ item.model = c.model;
52
+ item.name = c.name;
53
+ item.selected = c.id === selected;
54
+ return item;
55
+ });
56
+ }
57
+ async setUserConnection(userId, connectionId, tenantId) {
58
+ if (!this.preferenceService) {
59
+ throw new common_1.ForbiddenException(error_code_1.ErrorCode.AI_PREFERENCES_UNAVAILABLE);
60
+ }
61
+ const connections = await this.connectionService.listUsable();
62
+ const available = this.availableConnections(connections, tenantId);
63
+ if (!available.some((c) => c.id === connectionId)) {
64
+ throw new common_1.ForbiddenException(error_code_1.ErrorCode.AI_CONNECTION_NOT_AVAILABLE);
65
+ }
66
+ await this.preferenceService.upsertPreference('user', userId, connectionId);
67
+ }
68
+ async setPreference(scope, refId, connectionId, enforced = false) {
69
+ if (!this.preferenceService) {
70
+ throw new common_1.ForbiddenException(error_code_1.ErrorCode.AI_PREFERENCES_UNAVAILABLE);
71
+ }
72
+ await this.assertConnectionUsable(connectionId);
73
+ return this.preferenceService.upsertPreference(scope, refId, connectionId, enforced);
74
+ }
75
+ async assertConnectionUsable(connectionId) {
76
+ const connections = await this.connectionService.listUsable();
77
+ if (!connections.some((c) => c.id === connectionId)) {
78
+ throw new common_1.NotFoundException(error_code_1.ErrorCode.AI_CONNECTION_NOT_FOUND);
79
+ }
80
+ }
81
+ async resolveFrom(connections, ctx) {
82
+ if (!connections.length) {
83
+ return { locked: false };
84
+ }
85
+ const available = this.availableConnections(connections, ctx.tenantId);
86
+ const availableIds = new Set(available.map((c) => c.id));
87
+ const allIds = new Set(connections.map((c) => c.id));
88
+ let locked = false;
89
+ let selected;
90
+ for (const layer of this.resolutionLayers()) {
91
+ const candidate = await layer.resolve(connections, available, ctx);
92
+ if (!candidate) {
93
+ continue;
94
+ }
95
+ if (layer.soft && !availableIds.has(candidate)) {
96
+ continue;
97
+ }
98
+ selected = candidate;
99
+ locked = !layer.soft;
100
+ }
101
+ if (selected && !allIds.has(selected)) {
102
+ this.logger.warn(`AI connection "${selected}" selected by the resolution chain no longer exists or is disabled; falling back.`);
103
+ locked = false;
104
+ selected = undefined;
105
+ }
106
+ if (!selected && available.length) {
107
+ selected = available[0].id;
108
+ }
109
+ return { locked, selected };
110
+ }
111
+ resolutionLayers() {
112
+ return [
113
+ { resolve: (_all, available) => this.globalDefault(available), soft: true },
114
+ { resolve: (_all, _available, ctx) => this.tenantDefault(ctx), soft: true },
115
+ { resolve: (_all, _available, ctx) => this.userDefault(ctx), soft: true },
116
+ { resolve: (_all, _available, ctx) => this.clientSelection(ctx), soft: true },
117
+ { resolve: (_all, _available, ctx) => this.tenantEnforced(ctx), soft: false },
118
+ { resolve: (all) => this.adminEnforcedGlobal(all), soft: false },
119
+ { resolve: (all, _available, ctx) => this.adminEnforcedForTenant(all, ctx), soft: false },
120
+ { resolve: (_all, _available, ctx) => this.codeOverride(ctx), soft: false },
121
+ ];
122
+ }
123
+ availableConnections(connections, tenantId) {
124
+ return connections.filter((c) => !c.tenantIds?.length || (tenantId ? c.tenantIds.includes(tenantId) : false));
125
+ }
126
+ globalDefault(available) {
127
+ return available.find((c) => c.isDefault)?.id;
128
+ }
129
+ async tenantDefault(ctx) {
130
+ const pref = await this.loadTenantPreference(ctx);
131
+ return pref && !pref.enforced ? pref.connectionId : undefined;
132
+ }
133
+ async userDefault(ctx) {
134
+ if (!ctx.userId || !this.preferenceService) {
135
+ return undefined;
136
+ }
137
+ return (await this.preferenceService.getPreference('user', ctx.userId))?.connectionId;
138
+ }
139
+ clientSelection(ctx) {
140
+ return ctx.requested;
141
+ }
142
+ async tenantEnforced(ctx) {
143
+ const pref = await this.loadTenantPreference(ctx);
144
+ return pref?.enforced ? pref.connectionId : undefined;
145
+ }
146
+ loadTenantPreference(ctx) {
147
+ if (!ctx.tenantId || !this.preferenceService) {
148
+ return Promise.resolve(null);
149
+ }
150
+ let cached = this.tenantPrefCache.get(ctx);
151
+ if (!cached) {
152
+ cached = this.preferenceService.getPreference('tenant', ctx.tenantId);
153
+ this.tenantPrefCache.set(ctx, cached);
154
+ }
155
+ return cached;
156
+ }
157
+ adminEnforcedGlobal(all) {
158
+ return all.find((c) => c.enforced)?.id;
159
+ }
160
+ adminEnforcedForTenant(all, ctx) {
161
+ if (!ctx.tenantId) {
162
+ return undefined;
163
+ }
164
+ return all.find((c) => c.enforcedTenantIds?.includes(ctx.tenantId))?.id;
165
+ }
166
+ codeOverride(ctx) {
167
+ return ctx.codeOverride;
168
+ }
169
+ };
170
+ exports.CoreAiConnectionResolverService = CoreAiConnectionResolverService;
171
+ exports.CoreAiConnectionResolverService = CoreAiConnectionResolverService = CoreAiConnectionResolverService_1 = __decorate([
172
+ (0, common_1.Injectable)(),
173
+ __param(1, (0, common_1.Optional)()),
174
+ __metadata("design:paramtypes", [core_ai_connection_service_1.CoreAiConnectionService,
175
+ core_ai_connection_preference_service_1.CoreAiConnectionPreferenceService])
176
+ ], CoreAiConnectionResolverService);
177
+ //# sourceMappingURL=core-ai-connection-resolver.service.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"core-ai-connection-resolver.service.js","sourceRoot":"","sources":["../../../../../src/core/modules/ai/services/core-ai-connection-resolver.service.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,2CAAqG;A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,IAAI,CAAC,eAAe,CAAC,GAAG,CAAC,GAAG,EAAE,MAAM,CAAC,CAAC;QACxC,CAAC;QACD,OAAO,MAAM,CAAC;IAChB,CAAC;IAES,mBAAmB,CAAC,GAA6B;QACzD,OAAO,GAAG,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,QAAQ,CAAC,EAAE,EAAE,CAAC;IACzC,CAAC;IAES,sBAAsB,CAAC,GAA6B,EAAE,GAA+B;QAC7F,IAAI,CAAC,GAAG,CAAC,QAAQ,EAAE,CAAC;YAClB,OAAO,SAAS,CAAC;QACnB,CAAC;QACD,OAAO,GAAG,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,iBAAiB,EAAE,QAAQ,CAAC,GAAG,CAAC,QAAkB,CAAC,CAAC,EAAE,EAAE,CAAC;IACpF,CAAC;IAES,YAAY,CAAC,GAA+B;QACpD,OAAO,GAAG,CAAC,YAAY,CAAC;IAC1B,CAAC;CACF,CAAA;AAnPY,0EAA+B;0CAA/B,+BAA+B;IAD3C,IAAA,mBAAU,GAAE;IAYR,WAAA,IAAA,iBAAQ,GAAE,CAAA;qCAD2B,oDAAuB;QACV,yEAAiC;GAX3E,+BAA+B,CAmP3C"}
@@ -0,0 +1,43 @@
1
+ import { Logger, OnModuleInit } from '@nestjs/common';
2
+ import { Model } from 'mongoose';
3
+ import { ServiceOptions } from '../../../common/interfaces/service-options.interface';
4
+ import { CrudService } from '../../../common/services/crud.service';
5
+ import { CoreModelConstructor } from '../../../common/types/core-model-constructor.type';
6
+ import { CoreAiConnectionCreateInput } from '../inputs/core-ai-connection-create.input';
7
+ import { CoreAiConnectionInput } from '../inputs/core-ai-connection.input';
8
+ import { ResolvedAiConnection } from '../interfaces/resolved-ai-connection.interface';
9
+ import { AiConnectionDocument, CoreAiConnection } from '../models/core-ai-connection.model';
10
+ import { LlmProviderFactory } from '../providers/llm-provider.factory';
11
+ import { AiCryptoService } from './ai-crypto.service';
12
+ import { CoreAiConnectionPreferenceService } from './core-ai-connection-preference.service';
13
+ export declare const AI_CONNECTION_MODEL = "AiConnection";
14
+ export declare const AI_CONNECTION_CLASS = "AI_CONNECTION_CLASS";
15
+ export declare class CoreAiConnectionService extends CrudService<CoreAiConnection, CoreAiConnectionCreateInput, CoreAiConnectionInput> implements OnModuleInit {
16
+ protected readonly aiCryptoService: AiCryptoService;
17
+ protected readonly mainDbModel: Model<AiConnectionDocument>;
18
+ protected readonly mainModelConstructor: CoreModelConstructor<CoreAiConnection>;
19
+ protected readonly preferenceService?: CoreAiConnectionPreferenceService;
20
+ protected readonly providerFactory?: LlmProviderFactory;
21
+ protected readonly logger: Logger;
22
+ constructor(aiCryptoService: AiCryptoService, mainDbModel: Model<AiConnectionDocument>, mainModelConstructor: CoreModelConstructor<CoreAiConnection>, preferenceService?: CoreAiConnectionPreferenceService, providerFactory?: LlmProviderFactory);
23
+ onModuleInit(): Promise<void>;
24
+ protected seedDefaultConnection(): Promise<void>;
25
+ protected assertStoredKeysDecryptable(): Promise<void>;
26
+ create(input: CoreAiConnectionCreateInput, serviceOptions?: ServiceOptions): Promise<CoreAiConnection>;
27
+ update(id: string, input: CoreAiConnectionInput, serviceOptions?: ServiceOptions): Promise<CoreAiConnection>;
28
+ delete(id: string, serviceOptions?: ServiceOptions): Promise<CoreAiConnection>;
29
+ detectAndPersistCapabilities(connectionId: string): Promise<ResolvedAiConnection>;
30
+ listUsable(): Promise<{
31
+ enforced?: boolean;
32
+ enforcedTenantIds?: string[];
33
+ id: string;
34
+ isDefault?: boolean;
35
+ model?: string;
36
+ name?: string;
37
+ tenantIds?: string[];
38
+ }[]>;
39
+ resolve(connectionId?: string): Promise<ResolvedAiConnection>;
40
+ protected applyApiKey(id: string, apiKey: string): Promise<void>;
41
+ protected resolveApiKeyFromDoc(doc: Pick<CoreAiConnection, 'apiKeyEncrypted' | 'apiKeyEnv'>): string | undefined;
42
+ protected unsetOtherDefaults(id: string): Promise<void>;
43
+ }
@@ -0,0 +1,272 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var __metadata = (this && this.__metadata) || function (k, v) {
9
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
+ };
11
+ var __param = (this && this.__param) || function (paramIndex, decorator) {
12
+ return function (target, key) { decorator(target, key, paramIndex); }
13
+ };
14
+ var CoreAiConnectionService_1;
15
+ Object.defineProperty(exports, "__esModule", { value: true });
16
+ exports.CoreAiConnectionService = exports.AI_CONNECTION_CLASS = exports.AI_CONNECTION_MODEL = void 0;
17
+ const common_1 = require("@nestjs/common");
18
+ const mongoose_1 = require("@nestjs/mongoose");
19
+ const mongoose_2 = require("mongoose");
20
+ const crud_service_1 = require("../../../common/services/crud.service");
21
+ const config_service_1 = require("../../../common/services/config.service");
22
+ const error_code_1 = require("../../error-code");
23
+ const llm_provider_factory_1 = require("../providers/llm-provider.factory");
24
+ const ai_crypto_service_1 = require("./ai-crypto.service");
25
+ const core_ai_connection_preference_service_1 = require("./core-ai-connection-preference.service");
26
+ exports.AI_CONNECTION_MODEL = 'AiConnection';
27
+ exports.AI_CONNECTION_CLASS = 'AI_CONNECTION_CLASS';
28
+ let CoreAiConnectionService = CoreAiConnectionService_1 = class CoreAiConnectionService extends crud_service_1.CrudService {
29
+ aiCryptoService;
30
+ mainDbModel;
31
+ mainModelConstructor;
32
+ preferenceService;
33
+ providerFactory;
34
+ logger = new common_1.Logger(CoreAiConnectionService_1.name);
35
+ constructor(aiCryptoService, mainDbModel, mainModelConstructor, preferenceService, providerFactory) {
36
+ super();
37
+ this.aiCryptoService = aiCryptoService;
38
+ this.mainDbModel = mainDbModel;
39
+ this.mainModelConstructor = mainModelConstructor;
40
+ this.preferenceService = preferenceService;
41
+ this.providerFactory = providerFactory;
42
+ }
43
+ async onModuleInit() {
44
+ await this.seedDefaultConnection();
45
+ await this.assertStoredKeysDecryptable();
46
+ }
47
+ async seedDefaultConnection() {
48
+ const seed = config_service_1.ConfigService.get('ai.defaultConnection');
49
+ if (!seed) {
50
+ return;
51
+ }
52
+ if (!seed.baseUrl || !seed.model || !seed.name) {
53
+ this.logger.warn(`ai.defaultConnection seed skipped: incomplete config — need baseUrl, model and name ` +
54
+ `(got baseUrl=${!!seed.baseUrl}, model=${!!seed.model}, name=${!!seed.name}). ` +
55
+ `Set the missing AI_* env vars or create the connection via the admin UI.`);
56
+ return;
57
+ }
58
+ const count = await this.mainDbModel.countDocuments().exec();
59
+ if (count > 0) {
60
+ return;
61
+ }
62
+ const { apiKey, ...rest } = seed;
63
+ await this.mainDbModel.create({
64
+ ...rest,
65
+ apiKeyEncrypted: apiKey ? this.aiCryptoService.encrypt(apiKey) : undefined,
66
+ isDefault: true,
67
+ providerType: rest.providerType || 'openai-compatible',
68
+ });
69
+ this.logger.log(`Seeded default AI connection "${seed.name}"`);
70
+ }
71
+ async assertStoredKeysDecryptable() {
72
+ try {
73
+ const docs = await this.mainDbModel
74
+ .find({ apiKeyEncrypted: { $exists: true, $ne: '' } }, { apiKeyEncrypted: 1, name: 1 })
75
+ .lean()
76
+ .exec();
77
+ const broken = [];
78
+ for (const doc of docs) {
79
+ try {
80
+ this.aiCryptoService.decrypt(doc.apiKeyEncrypted ?? '');
81
+ }
82
+ catch {
83
+ broken.push(doc.name || String(doc._id));
84
+ }
85
+ }
86
+ if (broken.length) {
87
+ this.logger.error(`AI connection API key(s) could not be decrypted (encryption secret changed or value corrupted): ` +
88
+ `${broken.join(', ')}. Re-enter the API key for these connections — prompts using them will fail until fixed.`);
89
+ }
90
+ }
91
+ catch (err) {
92
+ this.logger.warn(`AI key decryptability self-check skipped: ${err.message}`);
93
+ }
94
+ }
95
+ async create(input, serviceOptions) {
96
+ const { apiKey, ...rest } = input;
97
+ let created = await super.create(rest, serviceOptions);
98
+ if (input.isDefault) {
99
+ await this.unsetOtherDefaults(created.id);
100
+ }
101
+ if (apiKey !== undefined) {
102
+ await this.applyApiKey(created.id, apiKey);
103
+ created = await this.get(created.id, serviceOptions);
104
+ }
105
+ if (this.providerFactory && (input.supportsJsonResponse === undefined || input.supportsNativeTools === undefined)) {
106
+ await this.detectAndPersistCapabilities(created.id).catch(() => undefined);
107
+ created = await this.get(created.id, serviceOptions);
108
+ }
109
+ return created;
110
+ }
111
+ async update(id, input, serviceOptions) {
112
+ const { apiKey, ...rest } = input;
113
+ let updated = await super.update(id, rest, serviceOptions);
114
+ if (input.isDefault) {
115
+ await this.unsetOtherDefaults(id);
116
+ }
117
+ if (apiKey !== undefined) {
118
+ await this.applyApiKey(id, apiKey);
119
+ updated = await this.get(id, serviceOptions);
120
+ }
121
+ return updated;
122
+ }
123
+ async delete(id, serviceOptions) {
124
+ const deleted = await super.delete(id, serviceOptions);
125
+ if (this.preferenceService) {
126
+ try {
127
+ await this.preferenceService.deleteByConnectionId(id);
128
+ }
129
+ catch (err) {
130
+ this.logger.warn(`Failed to clean up AI connection preferences for "${id}": ${err.message}`);
131
+ }
132
+ }
133
+ return deleted;
134
+ }
135
+ async detectAndPersistCapabilities(connectionId) {
136
+ const resolved = await this.resolve(connectionId);
137
+ const needsCapabilities = resolved.supportsJsonResponse === undefined || resolved.supportsNativeTools === undefined;
138
+ const needsContextWindow = resolved.contextWindow === undefined;
139
+ if (!this.providerFactory || (!needsCapabilities && !needsContextWindow)) {
140
+ return resolved;
141
+ }
142
+ let provider;
143
+ try {
144
+ provider = this.providerFactory.create(resolved);
145
+ }
146
+ catch (err) {
147
+ this.logger.warn(`AI capability detection skipped for "${resolved.name}": ${err.message}`);
148
+ return resolved;
149
+ }
150
+ const update = {};
151
+ if (needsCapabilities && typeof provider.detectCapabilities === 'function') {
152
+ try {
153
+ const detected = await provider.detectCapabilities();
154
+ if (resolved.supportsJsonResponse === undefined && typeof detected.jsonResponse === 'boolean') {
155
+ update.supportsJsonResponse = detected.jsonResponse;
156
+ resolved.supportsJsonResponse = detected.jsonResponse;
157
+ }
158
+ if (resolved.supportsNativeTools === undefined && typeof detected.nativeTools === 'boolean') {
159
+ update.supportsNativeTools = detected.nativeTools;
160
+ resolved.supportsNativeTools = detected.nativeTools;
161
+ }
162
+ }
163
+ catch (err) {
164
+ this.logger.warn(`AI capability detection failed for "${resolved.name}": ${err.message}`);
165
+ }
166
+ }
167
+ if (needsContextWindow && typeof provider.detectContextWindow === 'function') {
168
+ try {
169
+ const window = await provider.detectContextWindow();
170
+ if (typeof window === 'number' && window > 0) {
171
+ update.contextWindow = window;
172
+ resolved.contextWindow = window;
173
+ }
174
+ }
175
+ catch (err) {
176
+ this.logger.warn(`AI context-window detection failed for "${resolved.name}": ${err.message}`);
177
+ }
178
+ }
179
+ if (Object.keys(update).length) {
180
+ await this.mainDbModel.findByIdAndUpdate(connectionId, { $set: update }).exec();
181
+ this.logger.log(`Auto-detected AI connection metadata for "${resolved.name}": ${JSON.stringify(update)}`);
182
+ }
183
+ return resolved;
184
+ }
185
+ async listUsable() {
186
+ const docs = await this.mainDbModel
187
+ .find({ enabled: { $ne: false } })
188
+ .select('enforced enforcedTenantIds isDefault model name tenantIds')
189
+ .sort({ createdAt: 1 })
190
+ .lean()
191
+ .exec();
192
+ return docs.map((doc) => ({
193
+ enforced: doc.enforced,
194
+ enforcedTenantIds: doc.enforcedTenantIds,
195
+ id: String(doc._id),
196
+ isDefault: doc.isDefault,
197
+ model: doc.model,
198
+ name: doc.name,
199
+ tenantIds: doc.tenantIds,
200
+ }));
201
+ }
202
+ async resolve(connectionId) {
203
+ let doc = connectionId
204
+ ? await this.mainDbModel.findById(connectionId).lean().exec()
205
+ : await this.mainDbModel
206
+ .findOne({ enabled: { $ne: false }, isDefault: true })
207
+ .lean()
208
+ .exec();
209
+ if (!doc && !connectionId) {
210
+ doc = await this.mainDbModel
211
+ .findOne({ enabled: { $ne: false } })
212
+ .sort({ createdAt: 1 })
213
+ .lean()
214
+ .exec();
215
+ }
216
+ if (!doc) {
217
+ throw new common_1.NotFoundException(error_code_1.ErrorCode.AI_NO_CONNECTION);
218
+ }
219
+ if (doc.enabled === false) {
220
+ throw new common_1.ServiceUnavailableException(error_code_1.ErrorCode.AI_CONNECTION_DISABLED);
221
+ }
222
+ return {
223
+ apiKey: this.resolveApiKeyFromDoc(doc) ?? '',
224
+ baseUrl: doc.baseUrl,
225
+ contextWindow: doc.contextWindow,
226
+ defaultMaxTokens: doc.defaultMaxTokens,
227
+ defaultTemperature: doc.defaultTemperature,
228
+ defaultUserMaxPeriod: doc.defaultUserMaxPeriod,
229
+ defaultUserMaxTokens: doc.defaultUserMaxTokens,
230
+ id: String(doc._id),
231
+ model: doc.model,
232
+ name: doc.name,
233
+ providerType: doc.providerType || 'openai-compatible',
234
+ supportsJsonResponse: doc.supportsJsonResponse,
235
+ supportsNativeTools: doc.supportsNativeTools,
236
+ };
237
+ }
238
+ async applyApiKey(id, apiKey) {
239
+ if (apiKey === '') {
240
+ await this.mainDbModel.findByIdAndUpdate(id, { $unset: { apiKeyEncrypted: 1 } }).exec();
241
+ }
242
+ else {
243
+ await this.mainDbModel
244
+ .findByIdAndUpdate(id, { $set: { apiKeyEncrypted: this.aiCryptoService.encrypt(apiKey) } })
245
+ .exec();
246
+ }
247
+ }
248
+ resolveApiKeyFromDoc(doc) {
249
+ if (doc?.apiKeyEncrypted) {
250
+ return this.aiCryptoService.decrypt(doc.apiKeyEncrypted);
251
+ }
252
+ if (doc?.apiKeyEnv) {
253
+ return process.env[doc.apiKeyEnv];
254
+ }
255
+ return undefined;
256
+ }
257
+ async unsetOtherDefaults(id) {
258
+ await this.mainDbModel.updateMany({ _id: { $ne: id } }, { $set: { isDefault: false } }).exec();
259
+ }
260
+ };
261
+ exports.CoreAiConnectionService = CoreAiConnectionService;
262
+ exports.CoreAiConnectionService = CoreAiConnectionService = CoreAiConnectionService_1 = __decorate([
263
+ (0, common_1.Injectable)(),
264
+ __param(1, (0, mongoose_1.InjectModel)(exports.AI_CONNECTION_MODEL)),
265
+ __param(2, (0, common_1.Inject)(exports.AI_CONNECTION_CLASS)),
266
+ __param(3, (0, common_1.Optional)()),
267
+ __param(4, (0, common_1.Optional)()),
268
+ __metadata("design:paramtypes", [ai_crypto_service_1.AiCryptoService,
269
+ mongoose_2.Model, Object, core_ai_connection_preference_service_1.CoreAiConnectionPreferenceService,
270
+ llm_provider_factory_1.LlmProviderFactory])
271
+ ], CoreAiConnectionService);
272
+ //# sourceMappingURL=core-ai-connection.service.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"core-ai-connection.service.js","sourceRoot":"","sources":["../../../../../src/core/modules/ai/services/core-ai-connection.service.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,2CAQwB;AACxB,+CAA+C;AAC/C,uCAAiC;AAGjC,wEAAoE;AACpE,4EAAwE;AAExE,iDAA6C;AAK7C,4EAAuE;AACvE,2DAAsD;AACtD,mGAA4F;AAK/E,QAAA,mBAAmB,GAAG,cAAc,CAAC;AAKrC,QAAA,mBAAmB,GAAG,qBAAqB,CAAC;AAkBlD,IAAM,uBAAuB,+BAA7B,MAAM,uBACX,SAAQ,0BAAiF;IAMpE;IAC2C;IAElC;IAEG;IAEA;IAVd,MAAM,GAAG,IAAI,eAAM,CAAC,yBAAuB,CAAC,IAAI,CAAC,CAAC;IAErE,YACqB,eAAgC,EACW,WAAwC,EAE1E,oBAA4D,EAEzD,iBAAqD,EAErD,eAAoC;QAEnE,KAAK,EAAE,CAAC;QATW,oBAAe,GAAf,eAAe,CAAiB;QACW,gBAAW,GAAX,WAAW,CAA6B;QAE1E,yBAAoB,GAApB,oBAAoB,C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AE,EAAE,CAAC,CAAC,IAAI,EAAE,CAAC;IACjG,CAAC;CACF,CAAA;AA/UY,0DAAuB;kCAAvB,uBAAuB;IADnC,IAAA,mBAAU,GAAE;IASR,WAAA,IAAA,sBAAW,EAAC,2BAAmB,CAAC,CAAA;IAChC,WAAA,IAAA,eAAM,EAAC,2BAAmB,CAAC,CAAA;IAG3B,WAAA,IAAA,iBAAQ,GAAE,CAAA;IAEV,WAAA,IAAA,iBAAQ,GAAE,CAAA;qCAPyB,mCAAe;QACwB,gBAAK,UAI7B,yEAAiC;QAEnC,yCAAkB;GAd1D,uBAAuB,CA+UnC"}
@@ -0,0 +1,26 @@
1
+ import { Model } from 'mongoose';
2
+ import { CrudService } from '../../../common/services/crud.service';
3
+ import { CoreModelConstructor } from '../../../common/types/core-model-constructor.type';
4
+ import { CoreAiConversationCreateInput } from '../inputs/core-ai-conversation-create.input';
5
+ import { CoreAiConversationInput } from '../inputs/core-ai-conversation.input';
6
+ import { AiConversationDocument, CoreAiConversation } from '../models/core-ai-conversation.model';
7
+ export declare const AI_CONVERSATION_MODEL = "AiConversation";
8
+ export declare const AI_CONVERSATION_CLASS = "AI_CONVERSATION_CLASS";
9
+ export declare class CoreAiConversationService extends CrudService<CoreAiConversation, CoreAiConversationCreateInput, CoreAiConversationInput> {
10
+ protected readonly mainDbModel: Model<AiConversationDocument>;
11
+ protected readonly mainModelConstructor: CoreModelConstructor<CoreAiConversation>;
12
+ protected readonly maxRetainedMessages = 500;
13
+ constructor(mainDbModel: Model<AiConversationDocument>, mainModelConstructor: CoreModelConstructor<CoreAiConversation>);
14
+ appendMessage(id: string, message: {
15
+ content: string;
16
+ createdAt?: Date;
17
+ role: string;
18
+ }): Promise<void>;
19
+ loadRecentMessages(id: string, currentUser: {
20
+ id?: string;
21
+ roles?: string[];
22
+ } | undefined, limit?: number): Promise<{
23
+ content: string;
24
+ role: string;
25
+ }[]>;
26
+ }
@@ -0,0 +1,72 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var __metadata = (this && this.__metadata) || function (k, v) {
9
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
+ };
11
+ var __param = (this && this.__param) || function (paramIndex, decorator) {
12
+ return function (target, key) { decorator(target, key, paramIndex); }
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.CoreAiConversationService = exports.AI_CONVERSATION_CLASS = exports.AI_CONVERSATION_MODEL = void 0;
16
+ const common_1 = require("@nestjs/common");
17
+ const mongoose_1 = require("@nestjs/mongoose");
18
+ const mongoose_2 = require("mongoose");
19
+ const crud_service_1 = require("../../../common/services/crud.service");
20
+ exports.AI_CONVERSATION_MODEL = 'AiConversation';
21
+ exports.AI_CONVERSATION_CLASS = 'AI_CONVERSATION_CLASS';
22
+ let CoreAiConversationService = class CoreAiConversationService extends crud_service_1.CrudService {
23
+ mainDbModel;
24
+ mainModelConstructor;
25
+ maxRetainedMessages = 500;
26
+ constructor(mainDbModel, mainModelConstructor) {
27
+ super();
28
+ this.mainDbModel = mainDbModel;
29
+ this.mainModelConstructor = mainModelConstructor;
30
+ }
31
+ async appendMessage(id, message) {
32
+ await this.mainDbModel
33
+ .findByIdAndUpdate(id, {
34
+ $push: {
35
+ messages: {
36
+ $each: [{ ...message, createdAt: message.createdAt ?? new Date() }],
37
+ $slice: -this.maxRetainedMessages,
38
+ },
39
+ },
40
+ })
41
+ .exec();
42
+ }
43
+ async loadRecentMessages(id, currentUser, limit = 20) {
44
+ if (!id || typeof id !== 'string' || !mongoose_2.Types.ObjectId.isValid(id)) {
45
+ return [];
46
+ }
47
+ const doc = await this.mainDbModel
48
+ .findById(id, { createdBy: 1, messages: { $slice: -limit } })
49
+ .lean()
50
+ .exec();
51
+ if (!doc) {
52
+ return [];
53
+ }
54
+ const isOwner = currentUser?.id && String(doc.createdBy) === String(currentUser.id);
55
+ const isAdmin = currentUser?.roles?.includes('admin');
56
+ if (!isOwner && !isAdmin) {
57
+ return [];
58
+ }
59
+ return (doc.messages ?? []).map((m) => ({
60
+ content: m.content,
61
+ role: m.role,
62
+ }));
63
+ }
64
+ };
65
+ exports.CoreAiConversationService = CoreAiConversationService;
66
+ exports.CoreAiConversationService = CoreAiConversationService = __decorate([
67
+ (0, common_1.Injectable)(),
68
+ __param(0, (0, mongoose_1.InjectModel)(exports.AI_CONVERSATION_MODEL)),
69
+ __param(1, (0, common_1.Inject)(exports.AI_CONVERSATION_CLASS)),
70
+ __metadata("design:paramtypes", [mongoose_2.Model, Object])
71
+ ], CoreAiConversationService);
72
+ //# sourceMappingURL=core-ai-conversation.service.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"core-ai-conversation.service.js","sourceRoot":"","sources":["../../../../../src/core/modules/ai/services/core-ai-conversation.service.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,2CAAoD;AACpD,+CAA+C;AAC/C,uCAAwC;AAExC,wEAAoE;AASvD,QAAA,qBAAqB,GAAG,gBAAgB,CAAC;AAKzC,QAAA,qBAAqB,GAAG,uBAAuB,CAAC;AAUtD,IAAM,yBAAyB,GAA/B,MAAM,yBAA0B,SAAQ,0BAI9C;IAKmE;IAEpC;IALX,mBAAmB,GAAG,GAAG,CAAC;IAE7C,YACkE,WAA0C,EAE9E,oBAA8D;QAE1F,KAAK,EAAE,CAAC;QAJwD,gBAAW,GAAX,WAAW,CAA+B;QAE9E,yBAAoB,GAApB,oBAAoB,CAA0C;IAG5F,CAAC;IAQD,KAAK,CAAC,aAAa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C,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;YAC9F,OAAO,EAAE,CAAC,CAAC,OAAO;YAClB,IAAI,EAAE,CAAC,CAAC,IAAI;SACb,CAAC,CAAC,CAAC;IACN,CAAC;CACF,CAAA;AA5EY,8DAAyB;oCAAzB,yBAAyB;IADrC,IAAA,mBAAU,GAAE;IAUR,WAAA,IAAA,sBAAW,EAAC,6BAAqB,CAAC,CAAA;IAClC,WAAA,IAAA,eAAM,EAAC,6BAAqB,CAAC,CAAA;qCAD+C,gBAAK;GATzE,yBAAyB,CA4ErC"}
@@ -0,0 +1,13 @@
1
+ import { Model } from 'mongoose';
2
+ import { CrudService } from '../../../common/services/crud.service';
3
+ import { CoreModelConstructor } from '../../../common/types/core-model-constructor.type';
4
+ import { AiInteractionDocument, CoreAiInteraction } from '../models/core-ai-interaction.model';
5
+ import type { AiInteractionRecord } from './core-ai.service';
6
+ export declare const AI_INTERACTION_MODEL = "AiInteraction";
7
+ export declare const AI_INTERACTION_CLASS = "AI_INTERACTION_CLASS";
8
+ export declare class CoreAiInteractionService extends CrudService<CoreAiInteraction> {
9
+ protected readonly mainDbModel: Model<AiInteractionDocument>;
10
+ protected readonly mainModelConstructor: CoreModelConstructor<CoreAiInteraction>;
11
+ constructor(mainDbModel: Model<AiInteractionDocument>, mainModelConstructor: CoreModelConstructor<CoreAiInteraction>);
12
+ record(rec: AiInteractionRecord): Promise<void>;
13
+ }