@juspay/neurolink 9.2.0 → 9.4.0

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 (433) hide show
  1. package/CHANGELOG.md +27 -0
  2. package/README.md +60 -38
  3. package/dist/agent/directTools.d.ts +8 -8
  4. package/dist/cli/commands/rag.d.ts +19 -0
  5. package/dist/cli/commands/rag.js +756 -0
  6. package/dist/cli/factories/commandFactory.js +146 -83
  7. package/dist/cli/index.js +1 -0
  8. package/dist/cli/parser.js +4 -1
  9. package/dist/core/baseProvider.d.ts +43 -30
  10. package/dist/core/baseProvider.js +98 -138
  11. package/dist/core/conversationMemoryFactory.d.ts +2 -2
  12. package/dist/core/conversationMemoryFactory.js +2 -2
  13. package/dist/core/conversationMemoryInitializer.d.ts +1 -2
  14. package/dist/core/conversationMemoryInitializer.js +2 -2
  15. package/dist/core/infrastructure/baseError.d.ts +21 -0
  16. package/dist/core/infrastructure/baseError.js +22 -0
  17. package/dist/core/infrastructure/baseFactory.d.ts +21 -0
  18. package/dist/core/infrastructure/baseFactory.js +54 -0
  19. package/dist/core/infrastructure/baseRegistry.d.ts +21 -0
  20. package/dist/core/infrastructure/baseRegistry.js +49 -0
  21. package/dist/core/infrastructure/index.d.ts +5 -0
  22. package/dist/core/infrastructure/index.js +5 -0
  23. package/dist/core/infrastructure/retry.d.ts +7 -0
  24. package/dist/core/infrastructure/retry.js +20 -0
  25. package/dist/core/infrastructure/typedEventEmitter.d.ts +8 -0
  26. package/dist/core/infrastructure/typedEventEmitter.js +23 -0
  27. package/dist/core/redisConversationMemoryManager.d.ts +1 -6
  28. package/dist/core/redisConversationMemoryManager.js +7 -19
  29. package/dist/factories/providerFactory.d.ts +5 -3
  30. package/dist/factories/providerFactory.js +31 -24
  31. package/dist/index.d.ts +81 -12
  32. package/dist/index.js +105 -36
  33. package/dist/lib/core/baseProvider.d.ts +43 -30
  34. package/dist/lib/core/baseProvider.js +98 -138
  35. package/dist/lib/core/conversationMemoryFactory.d.ts +2 -2
  36. package/dist/lib/core/conversationMemoryFactory.js +2 -2
  37. package/dist/lib/core/conversationMemoryInitializer.d.ts +1 -2
  38. package/dist/lib/core/conversationMemoryInitializer.js +2 -2
  39. package/dist/lib/core/infrastructure/baseError.d.ts +21 -0
  40. package/dist/lib/core/infrastructure/baseError.js +23 -0
  41. package/dist/lib/core/infrastructure/baseFactory.d.ts +21 -0
  42. package/dist/lib/core/infrastructure/baseFactory.js +55 -0
  43. package/dist/lib/core/infrastructure/baseRegistry.d.ts +21 -0
  44. package/dist/lib/core/infrastructure/baseRegistry.js +50 -0
  45. package/dist/lib/core/infrastructure/index.d.ts +5 -0
  46. package/dist/lib/core/infrastructure/index.js +6 -0
  47. package/dist/lib/core/infrastructure/retry.d.ts +7 -0
  48. package/dist/lib/core/infrastructure/retry.js +21 -0
  49. package/dist/lib/core/infrastructure/typedEventEmitter.d.ts +8 -0
  50. package/dist/lib/core/infrastructure/typedEventEmitter.js +24 -0
  51. package/dist/lib/core/redisConversationMemoryManager.d.ts +1 -6
  52. package/dist/lib/core/redisConversationMemoryManager.js +7 -19
  53. package/dist/lib/factories/providerFactory.d.ts +5 -3
  54. package/dist/lib/factories/providerFactory.js +31 -24
  55. package/dist/lib/index.d.ts +81 -12
  56. package/dist/lib/index.js +105 -36
  57. package/dist/lib/mcp/index.d.ts +6 -5
  58. package/dist/lib/mcp/index.js +7 -5
  59. package/dist/lib/neurolink.d.ts +22 -13
  60. package/dist/lib/neurolink.js +358 -31
  61. package/dist/lib/providers/amazonBedrock.d.ts +15 -2
  62. package/dist/lib/providers/amazonBedrock.js +65 -8
  63. package/dist/lib/providers/anthropic.d.ts +3 -3
  64. package/dist/lib/providers/anthropic.js +10 -7
  65. package/dist/lib/providers/googleAiStudio.d.ts +5 -5
  66. package/dist/lib/providers/googleAiStudio.js +10 -7
  67. package/dist/lib/providers/googleVertex.d.ts +16 -4
  68. package/dist/lib/providers/googleVertex.js +72 -16
  69. package/dist/lib/providers/litellm.d.ts +3 -3
  70. package/dist/lib/providers/litellm.js +10 -10
  71. package/dist/lib/providers/mistral.d.ts +3 -3
  72. package/dist/lib/providers/mistral.js +7 -6
  73. package/dist/lib/providers/ollama.d.ts +3 -4
  74. package/dist/lib/providers/ollama.js +7 -8
  75. package/dist/lib/providers/openAI.d.ts +14 -2
  76. package/dist/lib/providers/openAI.js +60 -6
  77. package/dist/lib/providers/openRouter.d.ts +2 -2
  78. package/dist/lib/providers/openRouter.js +10 -6
  79. package/dist/lib/rag/ChunkerFactory.d.ts +91 -0
  80. package/dist/lib/rag/ChunkerFactory.js +321 -0
  81. package/dist/lib/rag/ChunkerRegistry.d.ts +91 -0
  82. package/dist/lib/rag/ChunkerRegistry.js +422 -0
  83. package/dist/lib/rag/chunkers/BaseChunker.d.ts +53 -0
  84. package/dist/lib/rag/chunkers/BaseChunker.js +144 -0
  85. package/dist/lib/rag/chunkers/CharacterChunker.d.ts +18 -0
  86. package/dist/lib/rag/chunkers/CharacterChunker.js +29 -0
  87. package/dist/lib/rag/chunkers/HTMLChunker.d.ts +19 -0
  88. package/dist/lib/rag/chunkers/HTMLChunker.js +39 -0
  89. package/dist/lib/rag/chunkers/JSONChunker.d.ts +19 -0
  90. package/dist/lib/rag/chunkers/JSONChunker.js +69 -0
  91. package/dist/lib/rag/chunkers/LaTeXChunker.d.ts +15 -0
  92. package/dist/lib/rag/chunkers/LaTeXChunker.js +64 -0
  93. package/dist/lib/rag/chunkers/MarkdownChunker.d.ts +15 -0
  94. package/dist/lib/rag/chunkers/MarkdownChunker.js +103 -0
  95. package/dist/lib/rag/chunkers/RecursiveChunker.d.ts +27 -0
  96. package/dist/lib/rag/chunkers/RecursiveChunker.js +140 -0
  97. package/dist/lib/rag/chunkers/SemanticMarkdownChunker.d.ts +22 -0
  98. package/dist/lib/rag/chunkers/SemanticMarkdownChunker.js +139 -0
  99. package/dist/lib/rag/chunkers/SentenceChunker.d.ts +19 -0
  100. package/dist/lib/rag/chunkers/SentenceChunker.js +67 -0
  101. package/dist/lib/rag/chunkers/TokenChunker.d.ts +19 -0
  102. package/dist/lib/rag/chunkers/TokenChunker.js +62 -0
  103. package/dist/lib/rag/chunkers/index.d.ts +15 -0
  104. package/dist/lib/rag/chunkers/index.js +16 -0
  105. package/dist/lib/rag/chunking/characterChunker.d.ts +16 -0
  106. package/dist/lib/rag/chunking/characterChunker.js +143 -0
  107. package/dist/lib/rag/chunking/chunkerRegistry.d.ts +67 -0
  108. package/dist/lib/rag/chunking/chunkerRegistry.js +195 -0
  109. package/dist/lib/rag/chunking/htmlChunker.d.ts +34 -0
  110. package/dist/lib/rag/chunking/htmlChunker.js +248 -0
  111. package/dist/lib/rag/chunking/index.d.ts +15 -0
  112. package/dist/lib/rag/chunking/index.js +18 -0
  113. package/dist/lib/rag/chunking/jsonChunker.d.ts +20 -0
  114. package/dist/lib/rag/chunking/jsonChunker.js +282 -0
  115. package/dist/lib/rag/chunking/latexChunker.d.ts +26 -0
  116. package/dist/lib/rag/chunking/latexChunker.js +252 -0
  117. package/dist/lib/rag/chunking/markdownChunker.d.ts +19 -0
  118. package/dist/lib/rag/chunking/markdownChunker.js +202 -0
  119. package/dist/lib/rag/chunking/recursiveChunker.d.ts +19 -0
  120. package/dist/lib/rag/chunking/recursiveChunker.js +149 -0
  121. package/dist/lib/rag/chunking/semanticChunker.d.ts +41 -0
  122. package/dist/lib/rag/chunking/semanticChunker.js +307 -0
  123. package/dist/lib/rag/chunking/sentenceChunker.d.ts +25 -0
  124. package/dist/lib/rag/chunking/sentenceChunker.js +231 -0
  125. package/dist/lib/rag/chunking/tokenChunker.d.ts +36 -0
  126. package/dist/lib/rag/chunking/tokenChunker.js +184 -0
  127. package/dist/lib/rag/document/MDocument.d.ts +198 -0
  128. package/dist/lib/rag/document/MDocument.js +393 -0
  129. package/dist/lib/rag/document/index.d.ts +5 -0
  130. package/dist/lib/rag/document/index.js +6 -0
  131. package/dist/lib/rag/document/loaders.d.ts +201 -0
  132. package/dist/lib/rag/document/loaders.js +501 -0
  133. package/dist/lib/rag/errors/RAGError.d.ts +244 -0
  134. package/dist/lib/rag/errors/RAGError.js +275 -0
  135. package/dist/lib/rag/errors/index.d.ts +6 -0
  136. package/dist/lib/rag/errors/index.js +7 -0
  137. package/dist/lib/rag/graphRag/graphRAG.d.ts +115 -0
  138. package/dist/lib/rag/graphRag/graphRAG.js +385 -0
  139. package/dist/lib/rag/graphRag/index.d.ts +4 -0
  140. package/dist/lib/rag/graphRag/index.js +5 -0
  141. package/dist/lib/rag/index.d.ts +103 -0
  142. package/dist/lib/rag/index.js +142 -0
  143. package/dist/lib/rag/metadata/MetadataExtractorFactory.d.ts +157 -0
  144. package/dist/lib/rag/metadata/MetadataExtractorFactory.js +419 -0
  145. package/dist/lib/rag/metadata/MetadataExtractorRegistry.d.ts +99 -0
  146. package/dist/lib/rag/metadata/MetadataExtractorRegistry.js +363 -0
  147. package/dist/lib/rag/metadata/index.d.ts +6 -0
  148. package/dist/lib/rag/metadata/index.js +10 -0
  149. package/dist/lib/rag/metadata/metadataExtractor.d.ts +69 -0
  150. package/dist/lib/rag/metadata/metadataExtractor.js +278 -0
  151. package/dist/lib/rag/pipeline/RAGPipeline.d.ts +235 -0
  152. package/dist/lib/rag/pipeline/RAGPipeline.js +402 -0
  153. package/dist/lib/rag/pipeline/contextAssembly.d.ts +126 -0
  154. package/dist/lib/rag/pipeline/contextAssembly.js +338 -0
  155. package/dist/lib/rag/pipeline/index.d.ts +5 -0
  156. package/dist/lib/rag/pipeline/index.js +6 -0
  157. package/dist/lib/rag/ragIntegration.d.ts +38 -0
  158. package/dist/lib/rag/ragIntegration.js +212 -0
  159. package/dist/lib/rag/reranker/RerankerFactory.d.ts +184 -0
  160. package/dist/lib/rag/reranker/RerankerFactory.js +431 -0
  161. package/dist/lib/rag/reranker/RerankerRegistry.d.ts +119 -0
  162. package/dist/lib/rag/reranker/RerankerRegistry.js +403 -0
  163. package/dist/lib/rag/reranker/index.d.ts +6 -0
  164. package/dist/lib/rag/reranker/index.js +10 -0
  165. package/dist/lib/rag/reranker/reranker.d.ts +71 -0
  166. package/dist/lib/rag/reranker/reranker.js +278 -0
  167. package/dist/lib/rag/resilience/CircuitBreaker.d.ts +215 -0
  168. package/dist/lib/rag/resilience/CircuitBreaker.js +432 -0
  169. package/dist/lib/rag/resilience/RetryHandler.d.ts +115 -0
  170. package/dist/lib/rag/resilience/RetryHandler.js +301 -0
  171. package/dist/lib/rag/resilience/index.d.ts +7 -0
  172. package/dist/lib/rag/resilience/index.js +8 -0
  173. package/dist/lib/rag/retrieval/hybridSearch.d.ts +94 -0
  174. package/dist/lib/rag/retrieval/hybridSearch.js +314 -0
  175. package/dist/lib/rag/retrieval/index.d.ts +5 -0
  176. package/dist/lib/rag/retrieval/index.js +6 -0
  177. package/dist/lib/rag/retrieval/vectorQueryTool.d.ts +93 -0
  178. package/dist/lib/rag/retrieval/vectorQueryTool.js +290 -0
  179. package/dist/lib/rag/types.d.ts +768 -0
  180. package/dist/lib/rag/types.js +9 -0
  181. package/dist/lib/server/index.d.ts +15 -11
  182. package/dist/lib/server/index.js +55 -51
  183. package/dist/lib/types/common.d.ts +0 -1
  184. package/dist/lib/types/generateTypes.d.ts +70 -8
  185. package/dist/lib/types/generateTypes.js +1 -1
  186. package/dist/lib/types/index.d.ts +6 -0
  187. package/dist/lib/types/index.js +12 -0
  188. package/dist/lib/types/streamTypes.d.ts +63 -8
  189. package/dist/lib/types/streamTypes.js +1 -1
  190. package/dist/lib/types/workflowTypes.d.ts +558 -0
  191. package/dist/lib/types/workflowTypes.js +32 -0
  192. package/dist/lib/utils/modelRouter.d.ts +4 -4
  193. package/dist/lib/utils/modelRouter.js +4 -4
  194. package/dist/lib/workflow/LAYER-EXAMPLES.d.ts +13 -0
  195. package/dist/lib/workflow/LAYER-EXAMPLES.js +312 -0
  196. package/dist/lib/workflow/PROMPT-EXAMPLES.d.ts +117 -0
  197. package/dist/lib/workflow/PROMPT-EXAMPLES.js +246 -0
  198. package/dist/lib/workflow/config.d.ts +1569 -0
  199. package/dist/lib/workflow/config.js +399 -0
  200. package/dist/lib/workflow/core/ensembleExecutor.d.ts +56 -0
  201. package/dist/lib/workflow/core/ensembleExecutor.js +398 -0
  202. package/dist/lib/workflow/core/judgeScorer.d.ts +26 -0
  203. package/dist/lib/workflow/core/judgeScorer.js +527 -0
  204. package/dist/lib/workflow/core/responseConditioner.d.ts +22 -0
  205. package/dist/lib/workflow/core/responseConditioner.js +226 -0
  206. package/dist/lib/workflow/core/types/conditionerTypes.d.ts +7 -0
  207. package/dist/lib/workflow/core/types/conditionerTypes.js +8 -0
  208. package/dist/lib/workflow/core/types/ensembleTypes.d.ts +7 -0
  209. package/dist/lib/workflow/core/types/ensembleTypes.js +8 -0
  210. package/dist/lib/workflow/core/types/index.d.ts +7 -0
  211. package/dist/lib/workflow/core/types/index.js +8 -0
  212. package/dist/lib/workflow/core/types/judgeTypes.d.ts +7 -0
  213. package/dist/lib/workflow/core/types/judgeTypes.js +8 -0
  214. package/dist/lib/workflow/core/types/layerTypes.d.ts +7 -0
  215. package/dist/lib/workflow/core/types/layerTypes.js +8 -0
  216. package/dist/lib/workflow/core/types/registryTypes.d.ts +7 -0
  217. package/dist/lib/workflow/core/types/registryTypes.js +8 -0
  218. package/dist/lib/workflow/core/workflowRegistry.d.ts +73 -0
  219. package/dist/lib/workflow/core/workflowRegistry.js +305 -0
  220. package/dist/lib/workflow/core/workflowRunner.d.ts +115 -0
  221. package/dist/lib/workflow/core/workflowRunner.js +554 -0
  222. package/dist/lib/workflow/index.d.ts +36 -0
  223. package/dist/lib/workflow/index.js +51 -0
  224. package/dist/lib/workflow/types.d.ts +19 -0
  225. package/dist/lib/workflow/types.js +10 -0
  226. package/dist/lib/workflow/utils/types/index.d.ts +7 -0
  227. package/dist/lib/workflow/utils/types/index.js +8 -0
  228. package/dist/lib/workflow/utils/types/metricsTypes.d.ts +7 -0
  229. package/dist/lib/workflow/utils/types/metricsTypes.js +8 -0
  230. package/dist/lib/workflow/utils/types/validationTypes.d.ts +7 -0
  231. package/dist/lib/workflow/utils/types/validationTypes.js +8 -0
  232. package/dist/lib/workflow/utils/workflowMetrics.d.ts +76 -0
  233. package/dist/lib/workflow/utils/workflowMetrics.js +312 -0
  234. package/dist/lib/workflow/utils/workflowValidation.d.ts +29 -0
  235. package/dist/lib/workflow/utils/workflowValidation.js +421 -0
  236. package/dist/lib/workflow/workflows/adaptiveWorkflow.d.ts +72 -0
  237. package/dist/lib/workflow/workflows/adaptiveWorkflow.js +367 -0
  238. package/dist/lib/workflow/workflows/consensusWorkflow.d.ts +69 -0
  239. package/dist/lib/workflow/workflows/consensusWorkflow.js +193 -0
  240. package/dist/lib/workflow/workflows/fallbackWorkflow.d.ts +49 -0
  241. package/dist/lib/workflow/workflows/fallbackWorkflow.js +226 -0
  242. package/dist/lib/workflow/workflows/multiJudgeWorkflow.d.ts +70 -0
  243. package/dist/lib/workflow/workflows/multiJudgeWorkflow.js +352 -0
  244. package/dist/mcp/index.d.ts +6 -5
  245. package/dist/mcp/index.js +7 -5
  246. package/dist/neurolink.d.ts +22 -13
  247. package/dist/neurolink.js +358 -31
  248. package/dist/providers/amazonBedrock.d.ts +15 -2
  249. package/dist/providers/amazonBedrock.js +65 -8
  250. package/dist/providers/anthropic.d.ts +3 -3
  251. package/dist/providers/anthropic.js +10 -7
  252. package/dist/providers/googleAiStudio.d.ts +5 -5
  253. package/dist/providers/googleAiStudio.js +10 -7
  254. package/dist/providers/googleVertex.d.ts +16 -4
  255. package/dist/providers/googleVertex.js +72 -16
  256. package/dist/providers/litellm.d.ts +3 -3
  257. package/dist/providers/litellm.js +10 -10
  258. package/dist/providers/mistral.d.ts +3 -3
  259. package/dist/providers/mistral.js +7 -6
  260. package/dist/providers/ollama.d.ts +3 -4
  261. package/dist/providers/ollama.js +7 -8
  262. package/dist/providers/openAI.d.ts +14 -2
  263. package/dist/providers/openAI.js +60 -6
  264. package/dist/providers/openRouter.d.ts +2 -2
  265. package/dist/providers/openRouter.js +10 -6
  266. package/dist/rag/ChunkerFactory.d.ts +91 -0
  267. package/dist/rag/ChunkerFactory.js +320 -0
  268. package/dist/rag/ChunkerRegistry.d.ts +91 -0
  269. package/dist/rag/ChunkerRegistry.js +421 -0
  270. package/dist/rag/chunkers/BaseChunker.d.ts +53 -0
  271. package/dist/rag/chunkers/BaseChunker.js +143 -0
  272. package/dist/rag/chunkers/CharacterChunker.d.ts +18 -0
  273. package/dist/rag/chunkers/CharacterChunker.js +28 -0
  274. package/dist/rag/chunkers/HTMLChunker.d.ts +19 -0
  275. package/dist/rag/chunkers/HTMLChunker.js +38 -0
  276. package/dist/rag/chunkers/JSONChunker.d.ts +19 -0
  277. package/dist/rag/chunkers/JSONChunker.js +68 -0
  278. package/dist/rag/chunkers/LaTeXChunker.d.ts +15 -0
  279. package/dist/rag/chunkers/LaTeXChunker.js +63 -0
  280. package/dist/rag/chunkers/MarkdownChunker.d.ts +15 -0
  281. package/dist/rag/chunkers/MarkdownChunker.js +102 -0
  282. package/dist/rag/chunkers/RecursiveChunker.d.ts +27 -0
  283. package/dist/rag/chunkers/RecursiveChunker.js +139 -0
  284. package/dist/rag/chunkers/SemanticMarkdownChunker.d.ts +22 -0
  285. package/dist/rag/chunkers/SemanticMarkdownChunker.js +138 -0
  286. package/dist/rag/chunkers/SentenceChunker.d.ts +19 -0
  287. package/dist/rag/chunkers/SentenceChunker.js +66 -0
  288. package/dist/rag/chunkers/TokenChunker.d.ts +19 -0
  289. package/dist/rag/chunkers/TokenChunker.js +61 -0
  290. package/dist/rag/chunkers/index.d.ts +15 -0
  291. package/dist/rag/chunkers/index.js +15 -0
  292. package/dist/rag/chunking/characterChunker.d.ts +16 -0
  293. package/dist/rag/chunking/characterChunker.js +142 -0
  294. package/dist/rag/chunking/chunkerRegistry.d.ts +67 -0
  295. package/dist/rag/chunking/chunkerRegistry.js +194 -0
  296. package/dist/rag/chunking/htmlChunker.d.ts +34 -0
  297. package/dist/rag/chunking/htmlChunker.js +247 -0
  298. package/dist/rag/chunking/index.d.ts +15 -0
  299. package/dist/rag/chunking/index.js +17 -0
  300. package/dist/rag/chunking/jsonChunker.d.ts +20 -0
  301. package/dist/rag/chunking/jsonChunker.js +281 -0
  302. package/dist/rag/chunking/latexChunker.d.ts +26 -0
  303. package/dist/rag/chunking/latexChunker.js +251 -0
  304. package/dist/rag/chunking/markdownChunker.d.ts +19 -0
  305. package/dist/rag/chunking/markdownChunker.js +201 -0
  306. package/dist/rag/chunking/recursiveChunker.d.ts +19 -0
  307. package/dist/rag/chunking/recursiveChunker.js +148 -0
  308. package/dist/rag/chunking/semanticChunker.d.ts +41 -0
  309. package/dist/rag/chunking/semanticChunker.js +306 -0
  310. package/dist/rag/chunking/sentenceChunker.d.ts +25 -0
  311. package/dist/rag/chunking/sentenceChunker.js +230 -0
  312. package/dist/rag/chunking/tokenChunker.d.ts +36 -0
  313. package/dist/rag/chunking/tokenChunker.js +183 -0
  314. package/dist/rag/document/MDocument.d.ts +198 -0
  315. package/dist/rag/document/MDocument.js +392 -0
  316. package/dist/rag/document/index.d.ts +5 -0
  317. package/dist/rag/document/index.js +5 -0
  318. package/dist/rag/document/loaders.d.ts +201 -0
  319. package/dist/rag/document/loaders.js +500 -0
  320. package/dist/rag/errors/RAGError.d.ts +244 -0
  321. package/dist/rag/errors/RAGError.js +274 -0
  322. package/dist/rag/errors/index.d.ts +6 -0
  323. package/dist/rag/errors/index.js +6 -0
  324. package/dist/rag/graphRag/graphRAG.d.ts +115 -0
  325. package/dist/rag/graphRag/graphRAG.js +384 -0
  326. package/dist/rag/graphRag/index.d.ts +4 -0
  327. package/dist/rag/graphRag/index.js +4 -0
  328. package/dist/rag/index.d.ts +103 -0
  329. package/dist/rag/index.js +141 -0
  330. package/dist/rag/metadata/MetadataExtractorFactory.d.ts +157 -0
  331. package/dist/rag/metadata/MetadataExtractorFactory.js +418 -0
  332. package/dist/rag/metadata/MetadataExtractorRegistry.d.ts +99 -0
  333. package/dist/rag/metadata/MetadataExtractorRegistry.js +362 -0
  334. package/dist/rag/metadata/index.d.ts +6 -0
  335. package/dist/rag/metadata/index.js +9 -0
  336. package/dist/rag/metadata/metadataExtractor.d.ts +69 -0
  337. package/dist/rag/metadata/metadataExtractor.js +277 -0
  338. package/dist/rag/pipeline/RAGPipeline.d.ts +235 -0
  339. package/dist/rag/pipeline/RAGPipeline.js +401 -0
  340. package/dist/rag/pipeline/contextAssembly.d.ts +126 -0
  341. package/dist/rag/pipeline/contextAssembly.js +337 -0
  342. package/dist/rag/pipeline/index.d.ts +5 -0
  343. package/dist/rag/pipeline/index.js +5 -0
  344. package/dist/rag/ragIntegration.d.ts +38 -0
  345. package/dist/rag/ragIntegration.js +211 -0
  346. package/dist/rag/reranker/RerankerFactory.d.ts +184 -0
  347. package/dist/rag/reranker/RerankerFactory.js +430 -0
  348. package/dist/rag/reranker/RerankerRegistry.d.ts +119 -0
  349. package/dist/rag/reranker/RerankerRegistry.js +402 -0
  350. package/dist/rag/reranker/index.d.ts +6 -0
  351. package/dist/rag/reranker/index.js +9 -0
  352. package/dist/rag/reranker/reranker.d.ts +71 -0
  353. package/dist/rag/reranker/reranker.js +277 -0
  354. package/dist/rag/resilience/CircuitBreaker.d.ts +215 -0
  355. package/dist/rag/resilience/CircuitBreaker.js +431 -0
  356. package/dist/rag/resilience/RetryHandler.d.ts +115 -0
  357. package/dist/rag/resilience/RetryHandler.js +300 -0
  358. package/dist/rag/resilience/index.d.ts +7 -0
  359. package/dist/rag/resilience/index.js +7 -0
  360. package/dist/rag/retrieval/hybridSearch.d.ts +94 -0
  361. package/dist/rag/retrieval/hybridSearch.js +313 -0
  362. package/dist/rag/retrieval/index.d.ts +5 -0
  363. package/dist/rag/retrieval/index.js +5 -0
  364. package/dist/rag/retrieval/vectorQueryTool.d.ts +93 -0
  365. package/dist/rag/retrieval/vectorQueryTool.js +289 -0
  366. package/dist/rag/types.d.ts +768 -0
  367. package/dist/rag/types.js +8 -0
  368. package/dist/server/index.d.ts +15 -11
  369. package/dist/server/index.js +55 -51
  370. package/dist/server/utils/validation.d.ts +2 -2
  371. package/dist/types/common.d.ts +0 -1
  372. package/dist/types/generateTypes.d.ts +70 -8
  373. package/dist/types/generateTypes.js +1 -1
  374. package/dist/types/index.d.ts +6 -0
  375. package/dist/types/index.js +12 -0
  376. package/dist/types/modelTypes.d.ts +20 -20
  377. package/dist/types/streamTypes.d.ts +63 -8
  378. package/dist/types/streamTypes.js +1 -1
  379. package/dist/types/workflowTypes.d.ts +558 -0
  380. package/dist/types/workflowTypes.js +31 -0
  381. package/dist/utils/modelRouter.d.ts +4 -4
  382. package/dist/utils/modelRouter.js +4 -4
  383. package/dist/workflow/LAYER-EXAMPLES.d.ts +13 -0
  384. package/dist/workflow/LAYER-EXAMPLES.js +311 -0
  385. package/dist/workflow/PROMPT-EXAMPLES.d.ts +117 -0
  386. package/dist/workflow/PROMPT-EXAMPLES.js +245 -0
  387. package/dist/workflow/config.d.ts +1569 -0
  388. package/dist/workflow/config.js +398 -0
  389. package/dist/workflow/core/ensembleExecutor.d.ts +56 -0
  390. package/dist/workflow/core/ensembleExecutor.js +397 -0
  391. package/dist/workflow/core/judgeScorer.d.ts +26 -0
  392. package/dist/workflow/core/judgeScorer.js +526 -0
  393. package/dist/workflow/core/responseConditioner.d.ts +22 -0
  394. package/dist/workflow/core/responseConditioner.js +225 -0
  395. package/dist/workflow/core/types/conditionerTypes.d.ts +7 -0
  396. package/dist/workflow/core/types/conditionerTypes.js +7 -0
  397. package/dist/workflow/core/types/ensembleTypes.d.ts +7 -0
  398. package/dist/workflow/core/types/ensembleTypes.js +7 -0
  399. package/dist/workflow/core/types/index.d.ts +7 -0
  400. package/dist/workflow/core/types/index.js +7 -0
  401. package/dist/workflow/core/types/judgeTypes.d.ts +7 -0
  402. package/dist/workflow/core/types/judgeTypes.js +7 -0
  403. package/dist/workflow/core/types/layerTypes.d.ts +7 -0
  404. package/dist/workflow/core/types/layerTypes.js +7 -0
  405. package/dist/workflow/core/types/registryTypes.d.ts +7 -0
  406. package/dist/workflow/core/types/registryTypes.js +7 -0
  407. package/dist/workflow/core/workflowRegistry.d.ts +73 -0
  408. package/dist/workflow/core/workflowRegistry.js +304 -0
  409. package/dist/workflow/core/workflowRunner.d.ts +115 -0
  410. package/dist/workflow/core/workflowRunner.js +553 -0
  411. package/dist/workflow/index.d.ts +36 -0
  412. package/dist/workflow/index.js +50 -0
  413. package/dist/workflow/types.d.ts +19 -0
  414. package/dist/workflow/types.js +9 -0
  415. package/dist/workflow/utils/types/index.d.ts +7 -0
  416. package/dist/workflow/utils/types/index.js +7 -0
  417. package/dist/workflow/utils/types/metricsTypes.d.ts +7 -0
  418. package/dist/workflow/utils/types/metricsTypes.js +7 -0
  419. package/dist/workflow/utils/types/validationTypes.d.ts +7 -0
  420. package/dist/workflow/utils/types/validationTypes.js +7 -0
  421. package/dist/workflow/utils/workflowMetrics.d.ts +76 -0
  422. package/dist/workflow/utils/workflowMetrics.js +311 -0
  423. package/dist/workflow/utils/workflowValidation.d.ts +29 -0
  424. package/dist/workflow/utils/workflowValidation.js +420 -0
  425. package/dist/workflow/workflows/adaptiveWorkflow.d.ts +72 -0
  426. package/dist/workflow/workflows/adaptiveWorkflow.js +366 -0
  427. package/dist/workflow/workflows/consensusWorkflow.d.ts +69 -0
  428. package/dist/workflow/workflows/consensusWorkflow.js +192 -0
  429. package/dist/workflow/workflows/fallbackWorkflow.d.ts +49 -0
  430. package/dist/workflow/workflows/fallbackWorkflow.js +225 -0
  431. package/dist/workflow/workflows/multiJudgeWorkflow.d.ts +70 -0
  432. package/dist/workflow/workflows/multiJudgeWorkflow.js +351 -0
  433. package/package.json +3 -2
@@ -0,0 +1,553 @@
1
+ /**
2
+ * Workflow Runner - Main Orchestrator
3
+ * ===================================
4
+ *
5
+ * Coordinates the complete workflow execution pipeline:
6
+ * 1. Model execution (layer-based or flat)
7
+ * 2. Judge scoring with hierarchical prompts
8
+ * 3. Response conditioning (stub)
9
+ * 4. Metrics collection
10
+ * 5. Result assembly
11
+ *
12
+ * @module workflow/core/workflowRunner
13
+ */
14
+ import { logger } from "../../utils/logger.js";
15
+ import { getModelGroups, PLACEHOLDER_MODEL, PLACEHOLDER_PROVIDER, usesModelGroups, } from "../config.js";
16
+ import { validateWorkflow } from "../utils/workflowValidation.js";
17
+ import { executeEnsemble, executeModelGroups } from "./ensembleExecutor.js";
18
+ import { scoreEnsemble } from "./judgeScorer.js";
19
+ import { conditionResponse } from "./responseConditioner.js";
20
+ /**
21
+ * Execute a complete workflow
22
+ *
23
+ * This is the main entry point that orchestrates:
24
+ * - Model execution (respects modelGroups or flat models)
25
+ * - Judge scoring (with hierarchical prompt resolution)
26
+ * - Response conditioning (currently stub)
27
+ * - Metrics calculation
28
+ * - Result assembly
29
+ *
30
+ * @param config - Validated workflow configuration
31
+ * @param options - Execution options including prompt
32
+ * @returns Complete workflow result with scores and metrics
33
+ *
34
+ * @example
35
+ * ```typescript
36
+ * const result = await runWorkflow(config, {
37
+ * prompt: 'Explain quantum entanglement',
38
+ * timeout: 30000,
39
+ * verbose: true,
40
+ * });
41
+ *
42
+ * console.log('Best response:', result.content);
43
+ * console.log('Score:', result.score);
44
+ * ```
45
+ */
46
+ export async function runWorkflow(config, options) {
47
+ const startTime = Date.now();
48
+ // Validate configuration
49
+ const validation = validateWorkflow(config);
50
+ if (!validation.valid) {
51
+ throw new Error(`Invalid workflow configuration: ${validation.errors.map((err) => err.message).join(", ")}`);
52
+ }
53
+ if (options.verbose) {
54
+ logger.debug(`[WorkflowRunner] Starting workflow: ${config.name}`);
55
+ logger.debug(`[WorkflowRunner] Type: ${config.type}`);
56
+ logger.debug(`[WorkflowRunner] Uses layer-based execution: ${usesModelGroups(config)}`);
57
+ }
58
+ try {
59
+ // Step 1: Execute models (layer-based or flat)
60
+ const ensembleResult = await executeModels(config, options);
61
+ if (options.verbose) {
62
+ logger.debug(`[WorkflowRunner] Received ${ensembleResult.responses.length} model responses`);
63
+ logger.debug(`[WorkflowRunner] Successful: ${ensembleResult.successCount}`);
64
+ }
65
+ // Step 2: Score responses with judge(s)
66
+ const scoreResult = await scoreResponses(config, ensembleResult.responses, options);
67
+ if (options.verbose) {
68
+ logger.debug(`[WorkflowRunner] Scoring complete`);
69
+ logger.debug(`[WorkflowRunner] Scores:`, scoreResult.scores);
70
+ }
71
+ // Step 3: Select best response
72
+ const bestResponse = selectBestResponse(ensembleResult.responses, scoreResult.scores);
73
+ if (options.verbose) {
74
+ logger.debug(`[WorkflowRunner] Best response: ${bestResponse.model}`);
75
+ const bestScore = extractScore(scoreResult.scores, bestResponse, ensembleResult.responses);
76
+ logger.debug(`[WorkflowRunner] Best score: ${bestScore}`);
77
+ }
78
+ // CRITICAL: Store original content BEFORE any processing
79
+ const originalContent = bestResponse.content;
80
+ // Step 4: Get processed content
81
+ // Priority: Judge-synthesized > Separate conditioning > Original
82
+ let processedContent;
83
+ let conditioningTime = 0;
84
+ const judgeScores = isJudgeScores(scoreResult.scores)
85
+ ? scoreResult.scores
86
+ : convertToJudgeScores(scoreResult.scores);
87
+ if (judgeScores.synthesizedResponse) {
88
+ // Judge already synthesized improved response
89
+ processedContent = judgeScores.synthesizedResponse;
90
+ logger.debug(`[WorkflowRunner] Using judge-synthesized response`);
91
+ }
92
+ else if (config.conditioning) {
93
+ // Fall back to separate conditioning if configured
94
+ const conditionedContent = await conditionFinalResponse(bestResponse, scoreResult.scores, config, options, ensembleResult.responses);
95
+ processedContent = conditionedContent.content;
96
+ conditioningTime = conditionedContent.conditioningTime;
97
+ logger.debug(`[WorkflowRunner] Using separate conditioning`);
98
+ }
99
+ else {
100
+ // No processing, use original
101
+ processedContent = originalContent;
102
+ logger.debug(`[WorkflowRunner] No conditioning applied`);
103
+ }
104
+ // Step 5: Calculate execution metrics
105
+ const executionTime = Date.now() - startTime;
106
+ const ensembleTime = ensembleResult.totalTime;
107
+ const judgeTime = scoreResult.judgeTime;
108
+ // Step 6: Assemble complete result
109
+ const result = {
110
+ // Primary output (processed version)
111
+ content: processedContent,
112
+ // IMPORTANT: Store original unmodified response separately
113
+ originalContent: originalContent,
114
+ // Evaluation metrics (0-100 scale)
115
+ score: extractScore(scoreResult.scores, bestResponse, ensembleResult.responses),
116
+ reasoning: extractReasoning(scoreResult.scores),
117
+ // Ensemble data
118
+ ensembleResponses: ensembleResult.responses,
119
+ // Judge data
120
+ judgeScores: judgeScores,
121
+ selectedResponse: bestResponse,
122
+ // Quality metrics
123
+ confidence: extractConfidence(scoreResult.scores),
124
+ consensus: extractConsensus(scoreResult.scores),
125
+ // Performance metrics
126
+ totalTime: executionTime,
127
+ ensembleTime,
128
+ judgeTime,
129
+ conditioningTime: conditioningTime,
130
+ // Workflow metadata
131
+ workflow: config.id,
132
+ workflowName: config.name,
133
+ workflowVersion: config.version,
134
+ // Resource usage
135
+ usage: {
136
+ totalInputTokens: calculateInputTokens(ensembleResult.responses),
137
+ totalOutputTokens: calculateOutputTokens(ensembleResult.responses),
138
+ totalTokens: calculateTotalTokens(ensembleResult.responses),
139
+ byModel: [], // TODO: Populate per-model breakdown
140
+ },
141
+ // Additional metadata
142
+ metadata: options.metadata,
143
+ timestamp: new Date().toISOString(),
144
+ };
145
+ if (options.verbose) {
146
+ logger.debug(`[WorkflowRunner] Workflow complete in ${executionTime}ms`);
147
+ logger.debug(`[WorkflowRunner] Total tokens: ${result.usage?.totalTokens || 0}`);
148
+ }
149
+ return result;
150
+ }
151
+ catch (error) {
152
+ const executionTime = Date.now() - startTime;
153
+ const errorMessage = error instanceof Error ? error.message : String(error);
154
+ if (options.verbose) {
155
+ logger.error(`[WorkflowRunner] Workflow failed:`, errorMessage);
156
+ }
157
+ // Return error result with dummy data
158
+ const dummyResponse = {
159
+ provider: PLACEHOLDER_PROVIDER,
160
+ model: PLACEHOLDER_MODEL,
161
+ content: "",
162
+ responseTime: 0,
163
+ status: "failure",
164
+ error: errorMessage,
165
+ timestamp: new Date().toISOString(),
166
+ };
167
+ return {
168
+ content: "",
169
+ score: 0,
170
+ reasoning: `Workflow execution failed: ${errorMessage}`,
171
+ ensembleResponses: [dummyResponse],
172
+ confidence: 0,
173
+ totalTime: executionTime,
174
+ ensembleTime: 0,
175
+ workflow: config.id,
176
+ workflowName: config.name,
177
+ workflowVersion: config.version,
178
+ metadata: options.metadata,
179
+ timestamp: new Date().toISOString(),
180
+ };
181
+ }
182
+ }
183
+ /**
184
+ * Execute models using layer-based or flat execution
185
+ */
186
+ async function executeModels(config, options) {
187
+ const executionConfig = {
188
+ timeout: options.timeout || config.execution?.timeout,
189
+ parallelism: options.parallelism || config.execution?.parallelism,
190
+ modelTimeout: config.execution?.modelTimeout,
191
+ minResponses: config.execution?.minResponses,
192
+ };
193
+ // Use layer-based execution if modelGroups defined
194
+ if (usesModelGroups(config)) {
195
+ const modelGroups = getModelGroups(config);
196
+ if (options.verbose) {
197
+ logger.debug(`[WorkflowRunner] Using layer-based execution`);
198
+ logger.debug(`[WorkflowRunner] Groups: ${modelGroups.length}`);
199
+ }
200
+ return executeModelGroups(modelGroups, options.prompt, executionConfig, undefined, // systemPrompt override
201
+ config.defaultSystemPrompt);
202
+ }
203
+ // Use flat parallel execution (backward compatible)
204
+ if (options.verbose) {
205
+ logger.debug(`[WorkflowRunner] Using flat parallel execution`);
206
+ logger.debug(`[WorkflowRunner] Models: ${config.models.length}`);
207
+ }
208
+ return executeEnsemble({
209
+ prompt: options.prompt,
210
+ models: config.models,
211
+ executionConfig,
212
+ workflowDefaults: {
213
+ systemPrompt: config.defaultSystemPrompt,
214
+ },
215
+ });
216
+ }
217
+ /**
218
+ * Score responses using judge(s)
219
+ */
220
+ async function scoreResponses(config, responses, options) {
221
+ // Use judges array if defined, otherwise use single judge
222
+ const judges = config.judges && config.judges.length > 0
223
+ ? config.judges
224
+ : config.judge
225
+ ? [config.judge]
226
+ : [];
227
+ if (judges.length === 0) {
228
+ // No judges configured - return neutral scores
229
+ if (options.verbose) {
230
+ logger.debug(`[WorkflowRunner] No judges configured, using neutral scores`);
231
+ }
232
+ const neutralScores = {
233
+ judgeProvider: PLACEHOLDER_PROVIDER,
234
+ judgeModel: PLACEHOLDER_MODEL,
235
+ scores: {},
236
+ criteria: [],
237
+ reasoning: "No judge configured",
238
+ judgeTime: 0,
239
+ timestamp: new Date().toISOString(),
240
+ };
241
+ // Assign neutral score (50) to each response
242
+ for (const response of responses) {
243
+ const responseId = `${response.provider}-${response.model}`;
244
+ neutralScores.scores[responseId] = 50;
245
+ }
246
+ return {
247
+ scores: neutralScores,
248
+ judgeTime: 0,
249
+ };
250
+ }
251
+ if (options.verbose) {
252
+ logger.debug(`[WorkflowRunner] Using ${judges.length} judge(s)`);
253
+ }
254
+ return scoreEnsemble({
255
+ judges,
256
+ responses,
257
+ originalPrompt: options.prompt,
258
+ timeout: config.execution?.timeout,
259
+ workflowDefaults: {
260
+ judgePrompt: config.defaultJudgePrompt,
261
+ },
262
+ });
263
+ }
264
+ /**
265
+ * Select best response based on scores
266
+ */
267
+ function selectBestResponse(responses, scores) {
268
+ // Filter to successful responses only
269
+ const successful = responses.filter((r) => r.status === "success");
270
+ if (successful.length === 0) {
271
+ // No successful responses - return first response
272
+ return (responses[0] || {
273
+ provider: PLACEHOLDER_PROVIDER,
274
+ model: PLACEHOLDER_MODEL,
275
+ content: "",
276
+ responseTime: 0,
277
+ status: "failure",
278
+ error: "No responses received",
279
+ timestamp: new Date().toISOString(),
280
+ });
281
+ }
282
+ // Find response with highest score
283
+ let bestResponse = successful[0];
284
+ let bestScore = getResponseScore(bestResponse, scores, responses);
285
+ for (const response of successful) {
286
+ const score = getResponseScore(response, scores, responses);
287
+ if (score > bestScore) {
288
+ bestScore = score;
289
+ bestResponse = response;
290
+ }
291
+ }
292
+ return bestResponse;
293
+ }
294
+ /**
295
+ * Get score for a specific response
296
+ */
297
+ function getResponseScore(response, scores, responses) {
298
+ // Judge scores only successful responses, so we need to find the index
299
+ // in the filtered successful responses array, not the original array
300
+ const successfulResponses = responses.filter((r) => r.status === "success");
301
+ const successfulIndex = successfulResponses.indexOf(response);
302
+ logger.debug(`[WorkflowRunner] getResponseScore`, {
303
+ responseProvider: response.provider,
304
+ responseModel: response.model,
305
+ responseStatus: response.status,
306
+ originalIndex: responses.indexOf(response),
307
+ successfulIndex,
308
+ totalResponses: responses.length,
309
+ successfulCount: successfulResponses.length,
310
+ lookupKey: `response-${successfulIndex}`,
311
+ availableKeys: Object.keys(scores.scores),
312
+ });
313
+ if (successfulIndex >= 0) {
314
+ const indexKey = `response-${successfulIndex}`;
315
+ if (indexKey in scores.scores) {
316
+ const score = scores.scores[indexKey];
317
+ logger.debug(`[WorkflowRunner] Found score ${score} for ${indexKey}`);
318
+ return score;
319
+ }
320
+ }
321
+ // Fallback to provider-model format
322
+ const responseId = `${response.provider}-${response.model}`;
323
+ logger.debug(`[WorkflowRunner] No index-based score found, trying provider-model: ${responseId}`);
324
+ return scores.scores[responseId] || 0;
325
+ }
326
+ /**
327
+ * Condition the final response - synthesize improved response using judge feedback
328
+ */
329
+ async function conditionFinalResponse(response, scores, config, options, allResponses) {
330
+ // Use conditioner if configured
331
+ if (config.conditioning) {
332
+ if (options.verbose) {
333
+ logger.debug(`[WorkflowRunner] Applying response conditioner with synthesis`);
334
+ }
335
+ return conditionResponse({
336
+ content: response.content,
337
+ selectedResponse: response,
338
+ allResponses,
339
+ judgeScores: scores,
340
+ config: config.conditioning,
341
+ originalPrompt: options.prompt,
342
+ });
343
+ }
344
+ // No conditioning - return original with metadata
345
+ return {
346
+ content: response.content,
347
+ conditioningTime: 0,
348
+ metadata: {
349
+ conditioningApplied: false,
350
+ originalLength: response.content.length,
351
+ finalLength: response.content.length,
352
+ },
353
+ };
354
+ }
355
+ // ============================================================================
356
+ // HELPER FUNCTIONS FOR RESULT ASSEMBLY
357
+ // ============================================================================
358
+ /**
359
+ * Type guard to check if scores are JudgeScores (not MultiJudgeScores)
360
+ */
361
+ function isJudgeScores(scores) {
362
+ return !("judges" in scores);
363
+ }
364
+ /**
365
+ * Convert MultiJudgeScores to JudgeScores format for WorkflowResult
366
+ */
367
+ function convertToJudgeScores(scores) {
368
+ return {
369
+ judgeProvider: scores.judgeProvider || "multi-judge",
370
+ judgeModel: scores.judgeModel || "consensus",
371
+ scores: scores.scores,
372
+ ranking: scores.ranking,
373
+ bestResponse: scores.bestResponse,
374
+ criteria: scores.criteria,
375
+ reasoning: scores.reasoning,
376
+ confidenceInJudgment: scores.confidenceInJudgment,
377
+ judgeTime: scores.judgeTime,
378
+ timestamp: scores.timestamp,
379
+ };
380
+ }
381
+ /**
382
+ * Extract best score from judge result
383
+ */
384
+ function extractScore(scores, bestResponse, responses) {
385
+ return getResponseScore(bestResponse, scores, responses);
386
+ }
387
+ /**
388
+ * Extract reasoning from judge result
389
+ */
390
+ function extractReasoning(scores) {
391
+ return scores.reasoning || "No reasoning provided";
392
+ }
393
+ /**
394
+ * Extract confidence from judge result
395
+ */
396
+ function extractConfidence(scores) {
397
+ return scores.confidenceInJudgment || 0.5;
398
+ }
399
+ /**
400
+ * Extract consensus level from scores
401
+ */
402
+ function extractConsensus(scores) {
403
+ if ("consensusLevel" in scores) {
404
+ return scores.consensusLevel;
405
+ }
406
+ return undefined;
407
+ }
408
+ /**
409
+ * Calculate total input tokens
410
+ */
411
+ function calculateInputTokens(responses) {
412
+ return responses.reduce((sum, r) => sum + (r.usage?.inputTokens || 0), 0);
413
+ }
414
+ /**
415
+ * Calculate total output tokens
416
+ */
417
+ function calculateOutputTokens(responses) {
418
+ return responses.reduce((sum, r) => sum + (r.usage?.outputTokens || 0), 0);
419
+ }
420
+ /**
421
+ * Calculate total tokens
422
+ */
423
+ function calculateTotalTokens(responses) {
424
+ return responses.reduce((sum, r) => sum + (r.usage?.totalTokens || 0), 0);
425
+ }
426
+ /**
427
+ * Execute workflow with progressive streaming support
428
+ * Yields preliminary response (first completed model) and final synthesized response
429
+ *
430
+ * @param config - Validated workflow configuration
431
+ * @param options - Execution options with streaming enabled
432
+ * @returns AsyncGenerator yielding preliminary and final responses
433
+ *
434
+ * @example
435
+ * ```typescript
436
+ * for await (const chunk of runWorkflowWithStreaming(config, options)) {
437
+ * if (chunk.type === 'preliminary') {
438
+ * console.log('Fast response:', chunk.content);
439
+ * } else {
440
+ * console.log('Final synthesis:', chunk.content);
441
+ * }
442
+ * }
443
+ * ```
444
+ */
445
+ export async function* runWorkflowWithStreaming(config, options) {
446
+ const startTime = Date.now();
447
+ // Validate configuration
448
+ const validation = validateWorkflow(config);
449
+ if (!validation.valid) {
450
+ throw new Error(`Invalid workflow configuration: ${validation.errors.map((err) => err.message).join(", ")}`);
451
+ }
452
+ if (options.verbose) {
453
+ logger.debug(`[WorkflowRunner] Starting streaming workflow: ${config.name}`);
454
+ }
455
+ try {
456
+ // Step 1: Execute models
457
+ const ensembleResult = await executeModels(config, options);
458
+ if (options.verbose) {
459
+ logger.debug(`[WorkflowRunner] Ensemble complete with ${ensembleResult.successCount} successful responses`);
460
+ }
461
+ // Yield preliminary response (first successful model)
462
+ if (ensembleResult.successCount > 0) {
463
+ const firstResponse = ensembleResult.responses.find((r) => r.status === "success" && Boolean(r.content));
464
+ if (firstResponse) {
465
+ if (options.verbose) {
466
+ logger.debug(`[WorkflowRunner] Yielding preliminary response from ${firstResponse.model}`);
467
+ }
468
+ yield {
469
+ type: "preliminary",
470
+ content: firstResponse.content,
471
+ partialResult: {
472
+ ensembleResponses: [firstResponse],
473
+ workflow: config.id,
474
+ workflowName: config.name,
475
+ },
476
+ };
477
+ }
478
+ }
479
+ // Step 2: Continue with full workflow execution (judge + synthesis)
480
+ const scoreResult = await scoreResponses(config, ensembleResult.responses, options);
481
+ const bestResponse = selectBestResponse(ensembleResult.responses, scoreResult.scores);
482
+ const originalContent = bestResponse.content;
483
+ // Step 3: Get processed content
484
+ let processedContent;
485
+ let conditioningTime = 0;
486
+ const judgeScores = isJudgeScores(scoreResult.scores)
487
+ ? scoreResult.scores
488
+ : convertToJudgeScores(scoreResult.scores);
489
+ if (judgeScores.synthesizedResponse) {
490
+ processedContent = judgeScores.synthesizedResponse;
491
+ if (options.verbose) {
492
+ logger.debug(`[WorkflowRunner] Using judge-synthesized response`);
493
+ }
494
+ }
495
+ else if (config.conditioning) {
496
+ const conditionedContent = await conditionFinalResponse(bestResponse, scoreResult.scores, config, options, ensembleResult.responses);
497
+ processedContent = conditionedContent.content;
498
+ conditioningTime = conditionedContent.conditioningTime;
499
+ if (options.verbose) {
500
+ logger.debug(`[WorkflowRunner] Using separate conditioning`);
501
+ }
502
+ }
503
+ else {
504
+ processedContent = originalContent;
505
+ if (options.verbose) {
506
+ logger.debug(`[WorkflowRunner] No conditioning applied`);
507
+ }
508
+ }
509
+ const executionTime = Date.now() - startTime;
510
+ const ensembleTime = ensembleResult.totalTime;
511
+ const judgeTime = scoreResult.judgeTime;
512
+ // Yield final synthesized response
513
+ if (options.verbose) {
514
+ logger.debug(`[WorkflowRunner] Yielding final synthesized response`);
515
+ }
516
+ yield {
517
+ type: "final",
518
+ content: processedContent,
519
+ partialResult: {
520
+ content: processedContent,
521
+ originalContent: originalContent,
522
+ score: extractScore(scoreResult.scores, bestResponse, ensembleResult.responses),
523
+ reasoning: extractReasoning(scoreResult.scores),
524
+ ensembleResponses: ensembleResult.responses,
525
+ judgeScores: judgeScores,
526
+ selectedResponse: bestResponse,
527
+ confidence: extractConfidence(scoreResult.scores),
528
+ consensus: extractConsensus(scoreResult.scores),
529
+ totalTime: executionTime,
530
+ ensembleTime,
531
+ judgeTime,
532
+ conditioningTime: conditioningTime,
533
+ workflow: config.id,
534
+ workflowName: config.name,
535
+ workflowVersion: config.version,
536
+ usage: {
537
+ totalInputTokens: calculateInputTokens(ensembleResult.responses),
538
+ totalOutputTokens: calculateOutputTokens(ensembleResult.responses),
539
+ totalTokens: calculateTotalTokens(ensembleResult.responses),
540
+ byModel: [],
541
+ },
542
+ metadata: options.metadata,
543
+ timestamp: new Date().toISOString(),
544
+ },
545
+ };
546
+ }
547
+ catch (error) {
548
+ logger.error(`[WorkflowRunner] Streaming workflow failed`, {
549
+ error: error instanceof Error ? error.message : String(error),
550
+ });
551
+ throw error;
552
+ }
553
+ }
@@ -0,0 +1,36 @@
1
+ /**
2
+ * Workflow Engine - Public API
3
+ * ============================
4
+ *
5
+ * Central exports for the Neurolink Workflow Engine
6
+ *
7
+ * @module workflow
8
+ */
9
+ export type { AggregatedUsage, ConditioningConfig, EnsembleResponse, ExecutionConfig, ExecutionStrategy, JudgeConfig, JudgeOutputFormat, JudgeScores, ModelConfig, ModelGroup, MultiJudgeScores, ToneAdjustment, WorkflowAnalytics, WorkflowConfig, WorkflowError, WorkflowErrorDetails, WorkflowGenerateOptions, WorkflowInput, WorkflowResult, WorkflowType, WorkflowValidationError, WorkflowValidationResult, WorkflowValidationWarning, } from "./types.js";
10
+ export { createWorkflowConfig, DEFAULT_EXECUTION_CONFIG, DEFAULT_JUDGE_CONFIG, getAllModels, getModelGroups, usesModelGroups, WorkflowConfigSchema, } from "./config.js";
11
+ export { clearRegistry, getRegistryStats, getWorkflow, listWorkflows, registerWorkflow, unregisterWorkflow, } from "./core/workflowRegistry.js";
12
+ export type { RunWorkflowOptions } from "./core/workflowRunner.js";
13
+ export { runWorkflow } from "./core/workflowRunner.js";
14
+ export { calculateConfidence, calculateConsensus, calculateModelMetrics, compareWorkflows, formatMetricsForLogging, generateSummaryStats, } from "./utils/workflowMetrics.js";
15
+ export { validateForExecution, validateWorkflow, } from "./utils/workflowValidation.js";
16
+ export { BALANCED_ADAPTIVE_WORKFLOW, createAdaptiveWorkflow, QUALITY_MAX_WORKFLOW, SPEED_FIRST_WORKFLOW, } from "./workflows/adaptiveWorkflow.js";
17
+ export { CONSENSUS_3_FAST_WORKFLOW, CONSENSUS_3_WORKFLOW, createConsensus3WithPrompt, } from "./workflows/consensusWorkflow.js";
18
+ export { AGGRESSIVE_FALLBACK_WORKFLOW, FAST_FALLBACK_WORKFLOW, } from "./workflows/fallbackWorkflow.js";
19
+ export { createMultiJudgeWorkflow, MULTI_JUDGE_3_WORKFLOW, MULTI_JUDGE_5_WORKFLOW, } from "./workflows/multiJudgeWorkflow.js";
20
+ export { executeEnsemble, executeModelGroups, } from "./core/ensembleExecutor.js";
21
+ export { scoreEnsemble } from "./core/judgeScorer.js";
22
+ export { conditionResponse, isConditioningEnabled, } from "./core/responseConditioner.js";
23
+ export type { ConditionOptions, ConditionResult, } from "./core/types/conditionerTypes.js";
24
+ export type { EnsembleExecutionResult, ExecuteEnsembleOptions, } from "./core/types/ensembleTypes.js";
25
+ export type { ScoreOptions, ScoreResult } from "./core/types/judgeTypes.js";
26
+ export type { ExecuteLayerOptions, LayerExecutionResult, } from "./core/types/layerTypes.js";
27
+ export type { RegisterOptions, RegisterResult, RegistryStats, } from "./core/types/registryTypes.js";
28
+ /**
29
+ * Workflow Engine version
30
+ */
31
+ export declare const WORKFLOW_ENGINE_VERSION = "1.0.0";
32
+ export { DEFAULT_SCORE_SCALE } from "./config.js";
33
+ /**
34
+ * Testing phase flag (returns original output unchanged)
35
+ */
36
+ export declare const IS_TESTING_PHASE = true;
@@ -0,0 +1,50 @@
1
+ /**
2
+ * Workflow Engine - Public API
3
+ * ============================
4
+ *
5
+ * Central exports for the Neurolink Workflow Engine
6
+ *
7
+ * @module workflow
8
+ */
9
+ // ============================================================================
10
+ // CORE FUNCTIONS
11
+ // ============================================================================
12
+ // Configuration and validation
13
+ export { createWorkflowConfig, DEFAULT_EXECUTION_CONFIG, DEFAULT_JUDGE_CONFIG, getAllModels, getModelGroups, usesModelGroups, WorkflowConfigSchema, } from "./config.js";
14
+ // Workflow registry
15
+ export { clearRegistry, getRegistryStats, getWorkflow, listWorkflows, registerWorkflow, unregisterWorkflow, } from "./core/workflowRegistry.js";
16
+ // Workflow execution
17
+ export { runWorkflow } from "./core/workflowRunner.js";
18
+ // Metrics and analytics
19
+ export { calculateConfidence, calculateConsensus, calculateModelMetrics, compareWorkflows, formatMetricsForLogging, generateSummaryStats, } from "./utils/workflowMetrics.js";
20
+ export { validateForExecution, validateWorkflow, } from "./utils/workflowValidation.js";
21
+ // ============================================================================
22
+ // PRE-BUILT WORKFLOWS
23
+ // ============================================================================
24
+ // Adaptive workflows
25
+ export { BALANCED_ADAPTIVE_WORKFLOW, createAdaptiveWorkflow, QUALITY_MAX_WORKFLOW, SPEED_FIRST_WORKFLOW, } from "./workflows/adaptiveWorkflow.js";
26
+ // Consensus workflows
27
+ export { CONSENSUS_3_FAST_WORKFLOW, CONSENSUS_3_WORKFLOW, createConsensus3WithPrompt, } from "./workflows/consensusWorkflow.js";
28
+ // Fallback workflows
29
+ export { AGGRESSIVE_FALLBACK_WORKFLOW, FAST_FALLBACK_WORKFLOW, } from "./workflows/fallbackWorkflow.js";
30
+ // Multi-judge workflows
31
+ export { createMultiJudgeWorkflow, MULTI_JUDGE_3_WORKFLOW, MULTI_JUDGE_5_WORKFLOW, } from "./workflows/multiJudgeWorkflow.js";
32
+ // ============================================================================
33
+ // LOW-LEVEL COMPONENTS (Advanced usage)
34
+ // ============================================================================
35
+ export { executeEnsemble, executeModelGroups, } from "./core/ensembleExecutor.js";
36
+ export { scoreEnsemble } from "./core/judgeScorer.js";
37
+ export { conditionResponse, isConditioningEnabled, } from "./core/responseConditioner.js";
38
+ // ============================================================================
39
+ // CONSTANTS
40
+ // ============================================================================
41
+ /**
42
+ * Workflow Engine version
43
+ */
44
+ export const WORKFLOW_ENGINE_VERSION = "1.0.0";
45
+ // Re-export from config to avoid duplication
46
+ export { DEFAULT_SCORE_SCALE } from "./config.js";
47
+ /**
48
+ * Testing phase flag (returns original output unchanged)
49
+ */
50
+ export const IS_TESTING_PHASE = true;
@@ -0,0 +1,19 @@
1
+ /**
2
+ * workflow/types.ts
3
+ * Re-exports workflow types from the central types folder
4
+ *
5
+ * All workflow types are now defined in src/lib/types/workflowTypes.ts
6
+ * This file maintains backward compatibility for existing imports.
7
+ */
8
+ export type { WorkflowType, JudgeOutputFormat, ToneAdjustment, ExecutionStrategy, ModelGroup, WorkflowConfig, WorkflowModelConfig, JudgeConfig, ConditioningConfig, ExecutionConfig, WorkflowInput, WorkflowGenerateOptions, WorkflowResult, EnsembleResponse, JudgeScores, MultiJudgeScores, AggregatedUsage, WorkflowAnalytics, WorkflowEvaluationData, WorkflowValidationResult, WorkflowValidationError, WorkflowValidationWarning, WorkflowErrorDetails, ExecuteEnsembleOptions, EnsembleExecutionResult, ExecuteModelOptions, ScoreOptions, ScoreResult, ParsedJudgeResponse, ConditionOptions, ConditionResult, LayerExecutionResult, ExecuteLayerOptions, RegistryEntry, RegisterOptions, RegisterResult, ListOptions, WorkflowMetadata, RegistryStats, WorkflowExecutionMetrics, SummaryStats, WorkflowComparison, ValidationIssues, } from "../types/workflowTypes.js";
9
+ export { WorkflowError } from "../types/workflowTypes.js";
10
+ import type { WorkflowModelConfig, WorkflowEvaluationData } from "../types/workflowTypes.js";
11
+ /**
12
+ * Alias for ModelConfig to maintain backward compatibility with code
13
+ * that imports ModelConfig from workflow types
14
+ */
15
+ export type ModelConfig = WorkflowModelConfig;
16
+ /**
17
+ * Alias for EvaluationData to maintain backward compatibility
18
+ */
19
+ export type EvaluationData = WorkflowEvaluationData;
@@ -0,0 +1,9 @@
1
+ /**
2
+ * workflow/types.ts
3
+ * Re-exports workflow types from the central types folder
4
+ *
5
+ * All workflow types are now defined in src/lib/types/workflowTypes.ts
6
+ * This file maintains backward compatibility for existing imports.
7
+ */
8
+ // Re-export the error class
9
+ export { WorkflowError } from "../types/workflowTypes.js";
@@ -0,0 +1,7 @@
1
+ /**
2
+ * workflow/utils/types/index.ts
3
+ * Centralized export for all workflow utility types
4
+ *
5
+ * Re-exports from the central types folder for backward compatibility
6
+ */
7
+ export type { SummaryStats, WorkflowComparison, WorkflowExecutionMetrics, ValidationIssues, } from "../../../types/workflowTypes.js";