@juspay/neurolink 9.32.0 → 9.32.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 (467) hide show
  1. package/CHANGELOG.md +6 -0
  2. package/dist/auth/anthropicOAuth.js +1 -1
  3. package/dist/cli/commands/proxy.js +18 -5
  4. package/dist/client/aiSdkAdapter.js +1 -1
  5. package/dist/client/index.js +137 -501
  6. package/dist/core/factory.js +0 -1
  7. package/dist/core/redisConversationMemoryManager.js +1 -1
  8. package/dist/features/ppt/slideGenerator.js +0 -1
  9. package/dist/features/ppt/utils.js +0 -1
  10. package/dist/lib/server/routes/claudeProxyRoutes.js +45 -9
  11. package/dist/mcp/elicitationProtocol.js +1 -1
  12. package/dist/mcp/servers/agent/directToolsServer.js +0 -1
  13. package/dist/providers/azureOpenai.js +1 -1
  14. package/dist/providers/huggingFace.js +0 -1
  15. package/dist/providers/openaiCompatible.js +0 -1
  16. package/dist/sdk/toolRegistration.js +0 -1
  17. package/dist/server/openapi/generator.js +1 -1
  18. package/dist/server/routes/claudeProxyRoutes.js +45 -9
  19. package/dist/types/configTypes.js +0 -5
  20. package/dist/types/modelTypes.js +0 -1
  21. package/dist/types/tools.js +0 -1
  22. package/dist/types/typeAliases.js +0 -1
  23. package/dist/types/utilities.js +1 -1
  24. package/dist/types/workflowTypes.js +0 -1
  25. package/dist/utils/providerRetry.js +0 -1
  26. package/dist/utils/providerUtils.js +0 -1
  27. package/package.json +2 -2
  28. package/dist/client/adapters/providerImageAdapter.js +0 -588
  29. package/dist/client/adapters/tts/googleTTSHandler.js +0 -344
  30. package/dist/client/adapters/video/directorPipeline.js +0 -516
  31. package/dist/client/adapters/video/ffmpegAdapter.js +0 -206
  32. package/dist/client/adapters/video/frameExtractor.js +0 -143
  33. package/dist/client/adapters/video/vertexVideoHandler.js +0 -763
  34. package/dist/client/adapters/video/videoAnalyzer.js +0 -238
  35. package/dist/client/adapters/video/videoMerger.js +0 -171
  36. package/dist/client/agent/directTools.js +0 -840
  37. package/dist/client/auth/AuthProviderFactory.js +0 -111
  38. package/dist/client/auth/AuthProviderRegistry.js +0 -190
  39. package/dist/client/auth/RequestContext.js +0 -78
  40. package/dist/client/auth/accountPool.js +0 -178
  41. package/dist/client/auth/anthropicOAuth.js +0 -974
  42. package/dist/client/auth/authContext.js +0 -314
  43. package/dist/client/auth/errors.js +0 -39
  44. package/dist/client/auth/index.js +0 -61
  45. package/dist/client/auth/middleware/AuthMiddleware.js +0 -519
  46. package/dist/client/auth/middleware/rateLimitByUser.js +0 -554
  47. package/dist/client/auth/providers/BaseAuthProvider.js +0 -723
  48. package/dist/client/auth/providers/CognitoProvider.js +0 -304
  49. package/dist/client/auth/providers/KeycloakProvider.js +0 -393
  50. package/dist/client/auth/providers/auth0.js +0 -274
  51. package/dist/client/auth/providers/betterAuth.js +0 -182
  52. package/dist/client/auth/providers/clerk.js +0 -317
  53. package/dist/client/auth/providers/custom.js +0 -112
  54. package/dist/client/auth/providers/firebase.js +0 -226
  55. package/dist/client/auth/providers/jwt.js +0 -212
  56. package/dist/client/auth/providers/oauth2.js +0 -303
  57. package/dist/client/auth/providers/supabase.js +0 -259
  58. package/dist/client/auth/providers/workos.js +0 -284
  59. package/dist/client/auth/serverBridge.js +0 -25
  60. package/dist/client/auth/sessionManager.js +0 -437
  61. package/dist/client/auth/tokenStore.js +0 -799
  62. package/dist/client/client/aiSdkAdapter.js +0 -487
  63. package/dist/client/client/auth.js +0 -473
  64. package/dist/client/client/errors.js +0 -552
  65. package/dist/client/client/httpClient.js +0 -837
  66. package/dist/client/client/index.js +0 -172
  67. package/dist/client/client/interceptors.js +0 -601
  68. package/dist/client/client/sseClient.js +0 -545
  69. package/dist/client/client/streamingClient.js +0 -917
  70. package/dist/client/client/wsClient.js +0 -369
  71. package/dist/client/config/configManager.js +0 -303
  72. package/dist/client/config/conversationMemory.js +0 -86
  73. package/dist/client/config/taskClassificationConfig.js +0 -148
  74. package/dist/client/constants/contextWindows.js +0 -295
  75. package/dist/client/constants/enums.js +0 -853
  76. package/dist/client/constants/index.js +0 -207
  77. package/dist/client/constants/performance.js +0 -389
  78. package/dist/client/constants/retry.js +0 -266
  79. package/dist/client/constants/timeouts.js +0 -182
  80. package/dist/client/constants/tokens.js +0 -380
  81. package/dist/client/constants/videoErrors.js +0 -46
  82. package/dist/client/context/budgetChecker.js +0 -98
  83. package/dist/client/context/contextCompactor.js +0 -205
  84. package/dist/client/context/emergencyTruncation.js +0 -88
  85. package/dist/client/context/errorDetection.js +0 -171
  86. package/dist/client/context/errors.js +0 -21
  87. package/dist/client/context/fileTokenBudget.js +0 -127
  88. package/dist/client/context/prompts/summarizationPrompt.js +0 -117
  89. package/dist/client/context/stages/fileReadDeduplicator.js +0 -66
  90. package/dist/client/context/stages/slidingWindowTruncator.js +0 -190
  91. package/dist/client/context/stages/structuredSummarizer.js +0 -99
  92. package/dist/client/context/stages/toolOutputPruner.js +0 -52
  93. package/dist/client/context/summarizationEngine.js +0 -136
  94. package/dist/client/context/toolOutputLimits.js +0 -78
  95. package/dist/client/context/toolPairRepair.js +0 -66
  96. package/dist/client/core/analytics.js +0 -88
  97. package/dist/client/core/baseProvider.js +0 -1385
  98. package/dist/client/core/constants.js +0 -140
  99. package/dist/client/core/conversationMemoryFactory.js +0 -141
  100. package/dist/client/core/conversationMemoryInitializer.js +0 -128
  101. package/dist/client/core/conversationMemoryManager.js +0 -344
  102. package/dist/client/core/dynamicModels.js +0 -358
  103. package/dist/client/core/evaluation.js +0 -309
  104. package/dist/client/core/evaluationProviders.js +0 -248
  105. package/dist/client/core/factory.js +0 -412
  106. package/dist/client/core/infrastructure/baseError.js +0 -22
  107. package/dist/client/core/infrastructure/baseFactory.js +0 -54
  108. package/dist/client/core/infrastructure/baseRegistry.js +0 -53
  109. package/dist/client/core/infrastructure/index.js +0 -5
  110. package/dist/client/core/infrastructure/retry.js +0 -20
  111. package/dist/client/core/infrastructure/typedEventEmitter.js +0 -23
  112. package/dist/client/core/modelConfiguration.js +0 -851
  113. package/dist/client/core/modules/GenerationHandler.js +0 -588
  114. package/dist/client/core/modules/MessageBuilder.js +0 -273
  115. package/dist/client/core/modules/StreamHandler.js +0 -185
  116. package/dist/client/core/modules/TelemetryHandler.js +0 -203
  117. package/dist/client/core/modules/ToolsManager.js +0 -499
  118. package/dist/client/core/modules/Utilities.js +0 -331
  119. package/dist/client/core/redisConversationMemoryManager.js +0 -1435
  120. package/dist/client/core/streamAnalytics.js +0 -131
  121. package/dist/client/evaluation/contextBuilder.js +0 -134
  122. package/dist/client/evaluation/index.js +0 -61
  123. package/dist/client/evaluation/prompts.js +0 -73
  124. package/dist/client/evaluation/ragasEvaluator.js +0 -110
  125. package/dist/client/evaluation/retryManager.js +0 -78
  126. package/dist/client/evaluation/scoring.js +0 -61
  127. package/dist/client/factories/providerFactory.js +0 -166
  128. package/dist/client/factories/providerRegistry.js +0 -166
  129. package/dist/client/features/ppt/constants.js +0 -896
  130. package/dist/client/features/ppt/contentPlanner.js +0 -529
  131. package/dist/client/features/ppt/presentationOrchestrator.js +0 -236
  132. package/dist/client/features/ppt/slideGenerator.js +0 -532
  133. package/dist/client/features/ppt/slideRenderers.js +0 -2383
  134. package/dist/client/features/ppt/slideTypeInference.js +0 -405
  135. package/dist/client/features/ppt/types.js +0 -13
  136. package/dist/client/features/ppt/utils.js +0 -443
  137. package/dist/client/files/fileReferenceRegistry.js +0 -1543
  138. package/dist/client/files/fileTools.js +0 -450
  139. package/dist/client/files/streamingReader.js +0 -321
  140. package/dist/client/files/types.js +0 -23
  141. package/dist/client/hitl/hitlErrors.js +0 -54
  142. package/dist/client/hitl/hitlManager.js +0 -460
  143. package/dist/client/mcp/agentExposure.js +0 -356
  144. package/dist/client/mcp/auth/index.js +0 -11
  145. package/dist/client/mcp/auth/oauthClientProvider.js +0 -325
  146. package/dist/client/mcp/auth/tokenStorage.js +0 -134
  147. package/dist/client/mcp/batching/index.js +0 -10
  148. package/dist/client/mcp/batching/requestBatcher.js +0 -441
  149. package/dist/client/mcp/caching/index.js +0 -10
  150. package/dist/client/mcp/caching/toolCache.js +0 -433
  151. package/dist/client/mcp/elicitation/elicitationManager.js +0 -376
  152. package/dist/client/mcp/elicitation/index.js +0 -11
  153. package/dist/client/mcp/elicitation/types.js +0 -10
  154. package/dist/client/mcp/elicitationProtocol.js +0 -375
  155. package/dist/client/mcp/enhancedToolDiscovery.js +0 -481
  156. package/dist/client/mcp/externalServerManager.js +0 -1478
  157. package/dist/client/mcp/factory.js +0 -161
  158. package/dist/client/mcp/flexibleToolValidator.js +0 -161
  159. package/dist/client/mcp/httpRateLimiter.js +0 -391
  160. package/dist/client/mcp/httpRetryHandler.js +0 -178
  161. package/dist/client/mcp/index.js +0 -74
  162. package/dist/client/mcp/mcpCircuitBreaker.js +0 -427
  163. package/dist/client/mcp/mcpClientFactory.js +0 -708
  164. package/dist/client/mcp/mcpRegistryClient.js +0 -488
  165. package/dist/client/mcp/mcpServerBase.js +0 -373
  166. package/dist/client/mcp/multiServerManager.js +0 -579
  167. package/dist/client/mcp/registry.js +0 -158
  168. package/dist/client/mcp/routing/index.js +0 -10
  169. package/dist/client/mcp/routing/toolRouter.js +0 -416
  170. package/dist/client/mcp/serverCapabilities.js +0 -502
  171. package/dist/client/mcp/servers/agent/directToolsServer.js +0 -150
  172. package/dist/client/mcp/toolAnnotations.js +0 -239
  173. package/dist/client/mcp/toolConverter.js +0 -258
  174. package/dist/client/mcp/toolDiscoveryService.js +0 -798
  175. package/dist/client/mcp/toolIntegration.js +0 -334
  176. package/dist/client/mcp/toolRegistry.js +0 -729
  177. package/dist/client/memory/hippocampusInitializer.js +0 -19
  178. package/dist/client/memory/memoryRetrievalTools.js +0 -166
  179. package/dist/client/middleware/builtin/analytics.js +0 -132
  180. package/dist/client/middleware/builtin/autoEvaluation.js +0 -203
  181. package/dist/client/middleware/builtin/guardrails.js +0 -109
  182. package/dist/client/middleware/builtin/lifecycle.js +0 -168
  183. package/dist/client/middleware/factory.js +0 -327
  184. package/dist/client/middleware/registry.js +0 -295
  185. package/dist/client/middleware/utils/guardrailsUtils.js +0 -396
  186. package/dist/client/models/anthropicModels.js +0 -527
  187. package/dist/client/neurolink.js +0 -8233
  188. package/dist/client/observability/exporterRegistry.js +0 -413
  189. package/dist/client/observability/exporters/arizeExporter.js +0 -138
  190. package/dist/client/observability/exporters/baseExporter.js +0 -190
  191. package/dist/client/observability/exporters/braintrustExporter.js +0 -154
  192. package/dist/client/observability/exporters/datadogExporter.js +0 -196
  193. package/dist/client/observability/exporters/laminarExporter.js +0 -302
  194. package/dist/client/observability/exporters/langfuseExporter.js +0 -209
  195. package/dist/client/observability/exporters/langsmithExporter.js +0 -143
  196. package/dist/client/observability/exporters/otelExporter.js +0 -164
  197. package/dist/client/observability/exporters/posthogExporter.js +0 -287
  198. package/dist/client/observability/exporters/sentryExporter.js +0 -165
  199. package/dist/client/observability/index.js +0 -31
  200. package/dist/client/observability/metricsAggregator.js +0 -556
  201. package/dist/client/observability/otelBridge.js +0 -131
  202. package/dist/client/observability/retryPolicy.js +0 -383
  203. package/dist/client/observability/sampling/samplers.js +0 -216
  204. package/dist/client/observability/spanProcessor.js +0 -303
  205. package/dist/client/observability/tokenTracker.js +0 -413
  206. package/dist/client/observability/types/exporterTypes.js +0 -5
  207. package/dist/client/observability/types/index.js +0 -4
  208. package/dist/client/observability/types/spanTypes.js +0 -92
  209. package/dist/client/observability/utils/safeMetadata.js +0 -25
  210. package/dist/client/observability/utils/spanSerializer.js +0 -292
  211. package/dist/client/processors/archive/ArchiveProcessor.js +0 -1308
  212. package/dist/client/processors/base/BaseFileProcessor.js +0 -614
  213. package/dist/client/processors/base/types.js +0 -82
  214. package/dist/client/processors/config/fileTypes.js +0 -520
  215. package/dist/client/processors/config/index.js +0 -92
  216. package/dist/client/processors/config/languageMap.js +0 -410
  217. package/dist/client/processors/config/mimeTypes.js +0 -363
  218. package/dist/client/processors/config/sizeLimits.js +0 -258
  219. package/dist/client/processors/document/ExcelProcessor.js +0 -590
  220. package/dist/client/processors/document/OpenDocumentProcessor.js +0 -212
  221. package/dist/client/processors/document/PptxProcessor.js +0 -157
  222. package/dist/client/processors/document/RtfProcessor.js +0 -361
  223. package/dist/client/processors/document/WordProcessor.js +0 -353
  224. package/dist/client/processors/errors/FileErrorCode.js +0 -255
  225. package/dist/client/processors/errors/errorHelpers.js +0 -386
  226. package/dist/client/processors/errors/errorSerializer.js +0 -507
  227. package/dist/client/processors/errors/index.js +0 -49
  228. package/dist/client/processors/markup/SvgProcessor.js +0 -240
  229. package/dist/client/processors/media/AudioProcessor.js +0 -707
  230. package/dist/client/processors/media/VideoProcessor.js +0 -1045
  231. package/dist/client/providers/amazonBedrock.js +0 -1512
  232. package/dist/client/providers/amazonSagemaker.js +0 -162
  233. package/dist/client/providers/anthropic.js +0 -831
  234. package/dist/client/providers/azureOpenai.js +0 -143
  235. package/dist/client/providers/googleAiStudio.js +0 -1200
  236. package/dist/client/providers/googleNativeGemini3.js +0 -543
  237. package/dist/client/providers/googleVertex.js +0 -2936
  238. package/dist/client/providers/huggingFace.js +0 -315
  239. package/dist/client/providers/litellm.js +0 -488
  240. package/dist/client/providers/mistral.js +0 -157
  241. package/dist/client/providers/ollama.js +0 -1579
  242. package/dist/client/providers/openAI.js +0 -627
  243. package/dist/client/providers/openRouter.js +0 -543
  244. package/dist/client/providers/openaiCompatible.js +0 -290
  245. package/dist/client/providers/providerTypeUtils.js +0 -46
  246. package/dist/client/providers/sagemaker/adaptive-semaphore.js +0 -215
  247. package/dist/client/providers/sagemaker/client.js +0 -472
  248. package/dist/client/providers/sagemaker/config.js +0 -317
  249. package/dist/client/providers/sagemaker/detection.js +0 -606
  250. package/dist/client/providers/sagemaker/error-constants.js +0 -227
  251. package/dist/client/providers/sagemaker/errors.js +0 -299
  252. package/dist/client/providers/sagemaker/language-model.js +0 -775
  253. package/dist/client/providers/sagemaker/parsers.js +0 -634
  254. package/dist/client/providers/sagemaker/streaming.js +0 -331
  255. package/dist/client/providers/sagemaker/structured-parser.js +0 -625
  256. package/dist/client/proxy/accountQuota.js +0 -162
  257. package/dist/client/proxy/claudeFormat.js +0 -595
  258. package/dist/client/proxy/modelRouter.js +0 -29
  259. package/dist/client/proxy/oauthFetch.js +0 -367
  260. package/dist/client/proxy/proxyFetch.js +0 -586
  261. package/dist/client/proxy/requestLogger.js +0 -207
  262. package/dist/client/proxy/tokenRefresh.js +0 -124
  263. package/dist/client/proxy/usageStats.js +0 -74
  264. package/dist/client/proxy/utils/noProxyUtils.js +0 -149
  265. package/dist/client/rag/ChunkerFactory.js +0 -320
  266. package/dist/client/rag/ChunkerRegistry.js +0 -421
  267. package/dist/client/rag/chunkers/BaseChunker.js +0 -143
  268. package/dist/client/rag/chunkers/CharacterChunker.js +0 -28
  269. package/dist/client/rag/chunkers/HTMLChunker.js +0 -38
  270. package/dist/client/rag/chunkers/JSONChunker.js +0 -68
  271. package/dist/client/rag/chunkers/LaTeXChunker.js +0 -63
  272. package/dist/client/rag/chunkers/MarkdownChunker.js +0 -306
  273. package/dist/client/rag/chunkers/RecursiveChunker.js +0 -139
  274. package/dist/client/rag/chunkers/SemanticMarkdownChunker.js +0 -138
  275. package/dist/client/rag/chunkers/SentenceChunker.js +0 -66
  276. package/dist/client/rag/chunkers/TokenChunker.js +0 -61
  277. package/dist/client/rag/chunkers/index.js +0 -15
  278. package/dist/client/rag/chunking/characterChunker.js +0 -142
  279. package/dist/client/rag/chunking/chunkerRegistry.js +0 -194
  280. package/dist/client/rag/chunking/htmlChunker.js +0 -247
  281. package/dist/client/rag/chunking/index.js +0 -17
  282. package/dist/client/rag/chunking/jsonChunker.js +0 -281
  283. package/dist/client/rag/chunking/latexChunker.js +0 -251
  284. package/dist/client/rag/chunking/markdownChunker.js +0 -373
  285. package/dist/client/rag/chunking/recursiveChunker.js +0 -148
  286. package/dist/client/rag/chunking/semanticChunker.js +0 -306
  287. package/dist/client/rag/chunking/sentenceChunker.js +0 -230
  288. package/dist/client/rag/chunking/tokenChunker.js +0 -183
  289. package/dist/client/rag/document/MDocument.js +0 -392
  290. package/dist/client/rag/document/index.js +0 -5
  291. package/dist/client/rag/document/loaders.js +0 -500
  292. package/dist/client/rag/errors/RAGError.js +0 -274
  293. package/dist/client/rag/errors/index.js +0 -6
  294. package/dist/client/rag/graphRag/graphRAG.js +0 -401
  295. package/dist/client/rag/graphRag/index.js +0 -4
  296. package/dist/client/rag/index.js +0 -141
  297. package/dist/client/rag/metadata/MetadataExtractorFactory.js +0 -418
  298. package/dist/client/rag/metadata/MetadataExtractorRegistry.js +0 -362
  299. package/dist/client/rag/metadata/index.js +0 -9
  300. package/dist/client/rag/metadata/metadataExtractor.js +0 -280
  301. package/dist/client/rag/pipeline/RAGPipeline.js +0 -436
  302. package/dist/client/rag/pipeline/contextAssembly.js +0 -341
  303. package/dist/client/rag/pipeline/index.js +0 -5
  304. package/dist/client/rag/ragIntegration.js +0 -321
  305. package/dist/client/rag/reranker/RerankerFactory.js +0 -430
  306. package/dist/client/rag/reranker/RerankerRegistry.js +0 -402
  307. package/dist/client/rag/reranker/index.js +0 -9
  308. package/dist/client/rag/reranker/reranker.js +0 -277
  309. package/dist/client/rag/resilience/CircuitBreaker.js +0 -431
  310. package/dist/client/rag/resilience/RetryHandler.js +0 -304
  311. package/dist/client/rag/resilience/index.js +0 -7
  312. package/dist/client/rag/retrieval/hybridSearch.js +0 -335
  313. package/dist/client/rag/retrieval/index.js +0 -5
  314. package/dist/client/rag/retrieval/vectorQueryTool.js +0 -307
  315. package/dist/client/rag/types.js +0 -8
  316. package/dist/client/sdk/toolRegistration.js +0 -377
  317. package/dist/client/server/abstract/baseServerAdapter.js +0 -575
  318. package/dist/client/server/adapters/expressAdapter.js +0 -486
  319. package/dist/client/server/adapters/fastifyAdapter.js +0 -472
  320. package/dist/client/server/adapters/honoAdapter.js +0 -632
  321. package/dist/client/server/adapters/koaAdapter.js +0 -510
  322. package/dist/client/server/errors.js +0 -486
  323. package/dist/client/server/factory/serverAdapterFactory.js +0 -160
  324. package/dist/client/server/index.js +0 -108
  325. package/dist/client/server/middleware/abortSignal.js +0 -111
  326. package/dist/client/server/middleware/auth.js +0 -388
  327. package/dist/client/server/middleware/cache.js +0 -359
  328. package/dist/client/server/middleware/common.js +0 -281
  329. package/dist/client/server/middleware/deprecation.js +0 -190
  330. package/dist/client/server/middleware/mcpBodyAttachment.js +0 -63
  331. package/dist/client/server/middleware/rateLimit.js +0 -227
  332. package/dist/client/server/middleware/validation.js +0 -388
  333. package/dist/client/server/openapi/generator.js +0 -398
  334. package/dist/client/server/openapi/index.js +0 -36
  335. package/dist/client/server/openapi/schemas.js +0 -695
  336. package/dist/client/server/openapi/templates.js +0 -374
  337. package/dist/client/server/routes/agentRoutes.js +0 -189
  338. package/dist/client/server/routes/claudeProxyRoutes.js +0 -1600
  339. package/dist/client/server/routes/healthRoutes.js +0 -187
  340. package/dist/client/server/routes/index.js +0 -57
  341. package/dist/client/server/routes/mcpRoutes.js +0 -342
  342. package/dist/client/server/routes/memoryRoutes.js +0 -350
  343. package/dist/client/server/routes/openApiRoutes.js +0 -126
  344. package/dist/client/server/routes/toolRoutes.js +0 -199
  345. package/dist/client/server/streaming/dataStream.js +0 -486
  346. package/dist/client/server/streaming/index.js +0 -11
  347. package/dist/client/server/types.js +0 -67
  348. package/dist/client/server/utils/redaction.js +0 -334
  349. package/dist/client/server/utils/validation.js +0 -243
  350. package/dist/client/server/websocket/WebSocketHandler.js +0 -383
  351. package/dist/client/server/websocket/index.js +0 -4
  352. package/dist/client/services/server/ai/observability/instrumentation.js +0 -808
  353. package/dist/client/telemetry/attributes.js +0 -100
  354. package/dist/client/telemetry/index.js +0 -26
  355. package/dist/client/telemetry/telemetryService.js +0 -308
  356. package/dist/client/telemetry/tracers.js +0 -17
  357. package/dist/client/telemetry/withSpan.js +0 -34
  358. package/dist/client/types/actionTypes.js +0 -6
  359. package/dist/client/types/analytics.js +0 -5
  360. package/dist/client/types/authTypes.js +0 -9
  361. package/dist/client/types/circuitBreakerErrors.js +0 -34
  362. package/dist/client/types/cli.js +0 -21
  363. package/dist/client/types/clientTypes.js +0 -10
  364. package/dist/client/types/common.js +0 -51
  365. package/dist/client/types/configTypes.js +0 -49
  366. package/dist/client/types/content.js +0 -19
  367. package/dist/client/types/contextTypes.js +0 -400
  368. package/dist/client/types/conversation.js +0 -47
  369. package/dist/client/types/conversationMemoryInterface.js +0 -6
  370. package/dist/client/types/domainTypes.js +0 -5
  371. package/dist/client/types/errors.js +0 -167
  372. package/dist/client/types/evaluation.js +0 -5
  373. package/dist/client/types/evaluationProviders.js +0 -5
  374. package/dist/client/types/evaluationTypes.js +0 -1
  375. package/dist/client/types/externalMcp.js +0 -6
  376. package/dist/client/types/fileReferenceTypes.js +0 -8
  377. package/dist/client/types/fileTypes.js +0 -4
  378. package/dist/client/types/generateTypes.js +0 -1
  379. package/dist/client/types/guardrails.js +0 -1
  380. package/dist/client/types/hitlTypes.js +0 -8
  381. package/dist/client/types/index.js +0 -57
  382. package/dist/client/types/mcpTypes.js +0 -5
  383. package/dist/client/types/middlewareTypes.js +0 -1
  384. package/dist/client/types/modelTypes.js +0 -30
  385. package/dist/client/types/multimodal.js +0 -135
  386. package/dist/client/types/observability.js +0 -6
  387. package/dist/client/types/pptTypes.js +0 -82
  388. package/dist/client/types/providers.js +0 -111
  389. package/dist/client/types/proxyTypes.js +0 -16
  390. package/dist/client/types/ragTypes.js +0 -7
  391. package/dist/client/types/sdkTypes.js +0 -8
  392. package/dist/client/types/serviceTypes.js +0 -5
  393. package/dist/client/types/streamTypes.js +0 -1
  394. package/dist/client/types/subscriptionTypes.js +0 -9
  395. package/dist/client/types/taskClassificationTypes.js +0 -5
  396. package/dist/client/types/tools.js +0 -24
  397. package/dist/client/types/ttsTypes.js +0 -57
  398. package/dist/client/types/typeAliases.js +0 -48
  399. package/dist/client/types/utilities.js +0 -4
  400. package/dist/client/types/workflowTypes.js +0 -30
  401. package/dist/client/utils/async/withTimeout.js +0 -98
  402. package/dist/client/utils/asyncMutex.js +0 -60
  403. package/dist/client/utils/conversationMemory.js +0 -431
  404. package/dist/client/utils/csvProcessor.js +0 -846
  405. package/dist/client/utils/errorHandling.js +0 -936
  406. package/dist/client/utils/evaluationUtils.js +0 -131
  407. package/dist/client/utils/factoryProcessing.js +0 -589
  408. package/dist/client/utils/fileDetector.js +0 -2161
  409. package/dist/client/utils/imageCache.js +0 -376
  410. package/dist/client/utils/imageProcessor.js +0 -704
  411. package/dist/client/utils/logger.js +0 -491
  412. package/dist/client/utils/mcpDefaults.js +0 -134
  413. package/dist/client/utils/messageBuilder.js +0 -1653
  414. package/dist/client/utils/modelAliasResolver.js +0 -54
  415. package/dist/client/utils/modelDetection.js +0 -80
  416. package/dist/client/utils/modelRouter.js +0 -292
  417. package/dist/client/utils/multimodalOptionsBuilder.js +0 -65
  418. package/dist/client/utils/observabilityHelpers.js +0 -47
  419. package/dist/client/utils/parameterValidation.js +0 -966
  420. package/dist/client/utils/pdfProcessor.js +0 -410
  421. package/dist/client/utils/performance.js +0 -222
  422. package/dist/client/utils/pricing.js +0 -340
  423. package/dist/client/utils/promptRedaction.js +0 -62
  424. package/dist/client/utils/providerConfig.js +0 -1009
  425. package/dist/client/utils/providerHealth.js +0 -1237
  426. package/dist/client/utils/providerRetry.js +0 -112
  427. package/dist/client/utils/providerUtils.js +0 -434
  428. package/dist/client/utils/rateLimiter.js +0 -200
  429. package/dist/client/utils/redis.js +0 -368
  430. package/dist/client/utils/retryHandler.js +0 -269
  431. package/dist/client/utils/retryability.js +0 -22
  432. package/dist/client/utils/sanitizers/svg.js +0 -481
  433. package/dist/client/utils/schemaConversion.js +0 -255
  434. package/dist/client/utils/taskClassificationUtils.js +0 -149
  435. package/dist/client/utils/taskClassifier.js +0 -94
  436. package/dist/client/utils/thinkingConfig.js +0 -104
  437. package/dist/client/utils/timeout.js +0 -359
  438. package/dist/client/utils/tokenEstimation.js +0 -142
  439. package/dist/client/utils/tokenLimits.js +0 -125
  440. package/dist/client/utils/tokenUtils.js +0 -239
  441. package/dist/client/utils/toolUtils.js +0 -75
  442. package/dist/client/utils/transformationUtils.js +0 -554
  443. package/dist/client/utils/ttsProcessor.js +0 -286
  444. package/dist/client/utils/typeUtils.js +0 -97
  445. package/dist/client/utils/videoAnalysisProcessor.js +0 -67
  446. package/dist/client/workflow/config.js +0 -398
  447. package/dist/client/workflow/core/ensembleExecutor.js +0 -407
  448. package/dist/client/workflow/core/judgeScorer.js +0 -544
  449. package/dist/client/workflow/core/responseConditioner.js +0 -225
  450. package/dist/client/workflow/core/types/conditionerTypes.js +0 -7
  451. package/dist/client/workflow/core/types/ensembleTypes.js +0 -7
  452. package/dist/client/workflow/core/types/index.js +0 -7
  453. package/dist/client/workflow/core/types/judgeTypes.js +0 -7
  454. package/dist/client/workflow/core/types/layerTypes.js +0 -7
  455. package/dist/client/workflow/core/types/registryTypes.js +0 -7
  456. package/dist/client/workflow/core/workflowRegistry.js +0 -304
  457. package/dist/client/workflow/core/workflowRunner.js +0 -586
  458. package/dist/client/workflow/index.js +0 -50
  459. package/dist/client/workflow/types.js +0 -9
  460. package/dist/client/workflow/utils/types/index.js +0 -7
  461. package/dist/client/workflow/utils/workflowMetrics.js +0 -311
  462. package/dist/client/workflow/utils/workflowValidation.js +0 -420
  463. package/dist/client/workflow/workflows/adaptiveWorkflow.js +0 -366
  464. package/dist/client/workflow/workflows/consensusWorkflow.js +0 -192
  465. package/dist/client/workflow/workflows/fallbackWorkflow.js +0 -225
  466. package/dist/client/workflow/workflows/multiJudgeWorkflow.js +0 -351
  467. /package/dist/client/{client/reactHooks.js → reactHooks.js} +0 -0
@@ -1,1600 +0,0 @@
1
- /**
2
- * Claude-Compatible Proxy Routes
3
- *
4
- * Exposes Anthropic-compatible /v1/messages, /v1/models, and /v1/messages/count_tokens
5
- * endpoints. ALL requests are routed through ctx.neurolink.generate() / ctx.neurolink.stream()
6
- * -- no direct HTTP calls to Anthropic.
7
- *
8
- * An optional ModelRouter can remap incoming model names to different
9
- * provider/model pairs (e.g. "claude-sonnet-4-20250514" -> vertex/gemini-2.5-pro).
10
- * Without a router, models are passed through to the Anthropic provider.
11
- */
12
- import { parseClaudeRequest, serializeClaudeResponse, ClaudeStreamSerializer, buildClaudeError, generateToolUseId, } from "../../proxy/claudeFormat.js";
13
- import { logger } from "../../utils/logger.js";
14
- import { recordRequest, recordSuccess, recordError, recordCooldown, } from "../../proxy/usageStats.js";
15
- import { logRequest, logFullRequestResponse, logStreamError, } from "../../proxy/requestLogger.js";
16
- import { parseQuotaHeaders, saveAccountQuota, } from "../../proxy/accountQuota.js";
17
- import { needsRefresh, refreshToken, persistTokens, } from "../../proxy/tokenRefresh.js";
18
- // ---------------------------------------------------------------------------
19
- // Helpers
20
- // ---------------------------------------------------------------------------
21
- /** Header names whose values must be masked in debug logs. */
22
- const SENSITIVE_HEADERS = new Set(["authorization", "x-api-key"]);
23
- /** Headers that must never be forwarded upstream to Anthropic. */
24
- const BLOCKED_UPSTREAM_HEADERS = new Set([
25
- "cookie",
26
- "proxy-authorization",
27
- "host",
28
- "connection",
29
- "content-length",
30
- "transfer-encoding",
31
- ]);
32
- /** Return a shallow copy of `headers` with sensitive values redacted. */
33
- function redactSensitiveHeaders(headers) {
34
- const redacted = {};
35
- for (const [key, value] of Object.entries(headers)) {
36
- if (SENSITIVE_HEADERS.has(key.toLowerCase()) && value.length > 8) {
37
- redacted[key] = value.substring(0, 8) + "...";
38
- }
39
- else if (SENSITIVE_HEADERS.has(key.toLowerCase())) {
40
- redacted[key] = "***";
41
- }
42
- else {
43
- redacted[key] = value;
44
- }
45
- }
46
- return redacted;
47
- }
48
- // ---------------------------------------------------------------------------
49
- // Module-level state
50
- // ---------------------------------------------------------------------------
51
- /** Fill-first: index of the current primary account. Only advances when
52
- * the current account hits a 429 or auth failure that puts it on cooldown. */
53
- let primaryAccountIndex = 0;
54
- const MAX_AUTH_RETRIES = 5;
55
- const MAX_CONSECUTIVE_REFRESH_FAILURES = 15;
56
- /** Decision 8: Cooldowns only for 401 and 429. */
57
- const AUTH_COOLDOWN_MS = 5 * 60 * 1000; // 5 minutes for 401
58
- const RATE_LIMIT_BACKOFF_BASE_MS = 1000; // 1 second base for 429
59
- const RATE_LIMIT_BACKOFF_CAP_MS = 10 * 60 * 1000; // 10 minute cap for 429
60
- /** Timeout for upstream requests to Anthropic. Generous to allow long-running
61
- * streaming responses to start, but prevents infinite hangs. */
62
- const UPSTREAM_FETCH_TIMEOUT_MS = 5 * 60 * 1000; // 5 minutes
63
- const accountRuntimeState = new Map();
64
- /** Track whether we've run the one-time startup prune. */
65
- let startupPruneDone = false;
66
- /** Advance the primary account index when the current primary is put on cooldown.
67
- * This is what makes fill-first work: we stick to one account until it's unusable.
68
- * Only advances when the account being cooled IS the current primary; otherwise
69
- * it's already a fallback and advancing would disrupt the fill-first ordering. */
70
- function advancePrimaryIfCurrent(accountKey, enabledCount, primaryAccountKey) {
71
- if (enabledCount <= 1) {
72
- return;
73
- }
74
- // Only advance if the cooled account is the current primary
75
- if (accountKey !== primaryAccountKey) {
76
- return;
77
- }
78
- primaryAccountIndex = (primaryAccountIndex + 1) % enabledCount;
79
- }
80
- // ---------------------------------------------------------------------------
81
- // Legacy credential refresh helper (extracted to reduce block nesting)
82
- // ---------------------------------------------------------------------------
83
- async function tryLoadLegacyAccount(creds, legacyCredPath) {
84
- if (!creds.oauth?.accessToken) {
85
- return undefined;
86
- }
87
- let legacyToken = creds.oauth.accessToken;
88
- let legacyRefresh = creds.oauth.refreshToken;
89
- let legacyExpiry = creds.oauth.expiresAt;
90
- const legacyExpired = legacyExpiry ? legacyExpiry < Date.now() : false;
91
- if (!legacyExpired) {
92
- return {
93
- key: "anthropic:legacy-default",
94
- label: "default",
95
- token: legacyToken,
96
- refreshToken: legacyRefresh,
97
- expiresAt: legacyExpiry,
98
- type: "oauth",
99
- persistTarget: { credPath: legacyCredPath },
100
- };
101
- }
102
- if (!legacyRefresh) {
103
- logger.always("[proxy] skipping legacy account (expired, no refresh token)");
104
- return undefined;
105
- }
106
- const tmp = {
107
- token: legacyToken,
108
- refreshToken: legacyRefresh,
109
- expiresAt: legacyExpiry,
110
- label: "default",
111
- };
112
- const ok = await refreshToken(tmp);
113
- if (!ok.success) {
114
- logger.always(`[proxy] skipping legacy account (expired, refresh failed: ${ok.error?.slice(0, 200) ?? "unknown"})`);
115
- return undefined;
116
- }
117
- legacyToken = tmp.token;
118
- legacyRefresh = tmp.refreshToken;
119
- legacyExpiry = tmp.expiresAt;
120
- await persistTokens(legacyCredPath, tmp);
121
- logger.always("[proxy] refreshed legacy account at startup");
122
- return {
123
- key: "anthropic:legacy-default",
124
- label: "default",
125
- token: legacyToken,
126
- refreshToken: legacyRefresh,
127
- expiresAt: legacyExpiry,
128
- type: "oauth",
129
- persistTarget: { credPath: legacyCredPath },
130
- };
131
- }
132
- // ---------------------------------------------------------------------------
133
- // Route factory
134
- // ---------------------------------------------------------------------------
135
- /**
136
- * Create Claude-compatible proxy routes.
137
- *
138
- * Every request flows through ctx.neurolink.generate() or ctx.neurolink.stream().
139
- * No direct fetch() calls to api.anthropic.com.
140
- *
141
- * @param modelRouter - Optional model router for remapping model names.
142
- * @param basePath - Base path prefix (default: "" since Claude API uses /v1/...).
143
- * @returns RouteGroup with Claude-compatible endpoints.
144
- */
145
- export function createClaudeProxyRoutes(modelRouter, basePath = "", accountStrategy = "fill-first") {
146
- return {
147
- prefix: `${basePath}/v1`,
148
- routes: [
149
- // =====================================================================
150
- // POST /v1/messages -- Main chat completions endpoint
151
- // =====================================================================
152
- {
153
- method: "POST",
154
- path: `${basePath}/v1/messages`,
155
- handler: async (ctx) => {
156
- const body = ctx.body;
157
- // 1. Validate
158
- if (!body?.model || !body?.messages) {
159
- return buildClaudeError(400, "Missing required fields: model, messages");
160
- }
161
- // 2. Resolve model via router (or pass through to anthropic)
162
- const route = modelRouter?.resolve(body.model) ?? {
163
- provider: "anthropic",
164
- model: body.model,
165
- };
166
- try {
167
- // 3. Route based on target provider
168
- const isClaudeTarget = route.provider === "anthropic" || route.provider === null;
169
- if (isClaudeTarget) {
170
- // ─── PASSTHROUGH MODE (Claude → Claude) ───────────────
171
- const fs = await import("fs");
172
- const os = await import("os");
173
- const accounts = [];
174
- const legacyCredPath = `${os.homedir()}/.neurolink/anthropic-credentials.json`;
175
- // 1. Compound keys from TokenStore
176
- // Skip accounts with expired tokens and no refresh token.
177
- // For expired tokens WITH a refresh token, attempt ONE refresh
178
- // before adding — if it fails, skip the account entirely.
179
- const { tokenStore } = await import("../../auth/tokenStore.js");
180
- // Decision 10D: Auto-prune dead entries once on first request (startup)
181
- if (!startupPruneDone) {
182
- await tokenStore.pruneExpired();
183
- startupPruneDone = true;
184
- }
185
- const compoundKeys = await tokenStore.listByPrefix("anthropic:");
186
- for (const key of compoundKeys) {
187
- // Decision 10D + Hot-reload: Skip disabled accounts UNLESS credentials changed
188
- if (await tokenStore.isDisabled(key)) {
189
- const existingState = getOrCreateRuntimeState(key);
190
- // Check if credentials were refreshed/re-authed since disable.
191
- // On cold start, lastToken is empty — don't treat that as a
192
- // credential change; only compare on subsequent reloads.
193
- const tokens = await tokenStore.loadTokens(key);
194
- const hasTrackedTokens = existingState.lastToken !== undefined &&
195
- existingState.lastToken !== "";
196
- const tokenChanged = tokens &&
197
- hasTrackedTokens &&
198
- (existingState.lastToken !== tokens.accessToken ||
199
- existingState.lastRefreshToken !== tokens.refreshToken);
200
- if (tokenChanged) {
201
- // Credentials changed — auto-enable and use this account
202
- await tokenStore.markEnabled(key);
203
- logger.always(`[proxy] account=${key.split(":")[1] ?? key} re-enabled (credentials changed)`);
204
- existingState.permanentlyDisabled = false;
205
- existingState.coolingUntil = undefined;
206
- existingState.backoffLevel = 0;
207
- existingState.consecutiveRefreshFailures = 0;
208
- }
209
- else {
210
- logger.debug(`[proxy] skipping disabled account=${key.split(":")[1] ?? key}`);
211
- existingState.permanentlyDisabled = true;
212
- continue;
213
- }
214
- }
215
- const tokens = await tokenStore.loadTokens(key);
216
- if (!tokens) {
217
- continue;
218
- }
219
- let accessToken = tokens.accessToken;
220
- let refreshTok = tokens.refreshToken;
221
- let expiresAt = tokens.expiresAt;
222
- // Check if token is expired
223
- const isExpired = expiresAt ? expiresAt < Date.now() : false;
224
- if (isExpired) {
225
- const label = key.split(":")[1] ?? key;
226
- // Check if already marked dead from a previous request
227
- const existingState = getOrCreateRuntimeState(key);
228
- if (existingState.permanentlyDisabled) {
229
- // Already known dead — skip silently (no log spam)
230
- continue;
231
- }
232
- if (!refreshTok) {
233
- logger.always(`[proxy] skipping account=${label} (expired, no refresh token)`);
234
- await disableAccountUntilReauth({ key, label, token: accessToken, type: "oauth" }, existingState);
235
- continue;
236
- }
237
- // Try ONE refresh before adding
238
- const tempAccount = {
239
- token: accessToken,
240
- refreshToken: refreshTok,
241
- expiresAt,
242
- label,
243
- };
244
- const refreshed = await refreshToken(tempAccount);
245
- if (!refreshed.success) {
246
- logger.always(`[proxy] skipping account=${label} (expired, refresh failed: ${refreshed.error?.slice(0, 200) ?? "unknown"})`);
247
- await disableAccountUntilReauth({ key, label, token: accessToken, type: "oauth" }, existingState);
248
- continue;
249
- }
250
- // Refresh succeeded — use new token and persist
251
- accessToken = tempAccount.token;
252
- refreshTok = tempAccount.refreshToken;
253
- expiresAt = tempAccount.expiresAt;
254
- await tokenStore.saveTokens(key, {
255
- accessToken,
256
- refreshToken: refreshTok,
257
- expiresAt: expiresAt ?? Date.now() + 3600_000,
258
- tokenType: "Bearer",
259
- });
260
- logger.always(`[proxy] refreshed expired account=${key.split(":")[1] ?? key} at startup`);
261
- }
262
- // Detect whether this is an API key or an OAuth token.
263
- // Use the stored tokenType (set at auth time) rather than a
264
- // prefix heuristic — both API keys (sk-ant-api03-…) and OAuth
265
- // access tokens (sk-ant-oat01-…) share the "sk-ant-" prefix.
266
- const accountType = tokens.tokenType === "Bearer" ? "oauth" : "api_key";
267
- accounts.push({
268
- key,
269
- label: key.split(":")[1] ?? key,
270
- token: accessToken,
271
- refreshToken: refreshTok,
272
- expiresAt,
273
- type: accountType,
274
- persistTarget: { providerKey: key },
275
- });
276
- }
277
- // 2. Legacy credentials file (only if no usable compound account was loaded)
278
- if (accounts.length === 0) {
279
- try {
280
- const creds = JSON.parse(fs.readFileSync(legacyCredPath, "utf8"));
281
- const legacyAccount = await tryLoadLegacyAccount(creds, legacyCredPath);
282
- if (legacyAccount) {
283
- accounts.push(legacyAccount);
284
- }
285
- }
286
- catch {
287
- // no-op: file absent or invalid
288
- }
289
- }
290
- // 3. Env var — only use as fallback when no OAuth accounts are available.
291
- if (process.env.ANTHROPIC_API_KEY && accounts.length === 0) {
292
- accounts.push({
293
- key: "anthropic:env",
294
- label: "env",
295
- token: process.env.ANTHROPIC_API_KEY,
296
- type: "api_key",
297
- });
298
- }
299
- if (accounts.length === 0) {
300
- return buildClaudeError(401, "No Anthropic credentials found");
301
- }
302
- // Sync in-memory runtime state with current token material.
303
- for (const account of accounts) {
304
- const state = getOrCreateRuntimeState(account.key);
305
- const tokenChanged = state.lastToken !== account.token ||
306
- state.lastRefreshToken !== account.refreshToken;
307
- if (tokenChanged) {
308
- if (state.permanentlyDisabled) {
309
- logger.always(`[proxy] account=${account.label} credentials changed, re-enabling`);
310
- }
311
- state.coolingUntil = undefined;
312
- state.backoffLevel = 0;
313
- state.consecutiveRefreshFailures = 0;
314
- state.permanentlyDisabled = false;
315
- }
316
- state.lastToken = account.token;
317
- state.lastRefreshToken = account.refreshToken;
318
- }
319
- const enabledAccounts = accounts.filter((account) => {
320
- return !getOrCreateRuntimeState(account.key)
321
- .permanentlyDisabled;
322
- });
323
- if (enabledAccounts.length === 0) {
324
- return buildClaudeError(401, formatReauthMessage(accounts.map((account) => account.label)));
325
- }
326
- // Order accounts based on the configured strategy.
327
- // - fill-first: always start with the primary account;
328
- // only fall over when the primary is cooling down (429/401).
329
- // - round-robin: rotate the starting index on every request
330
- // so traffic is spread evenly across accounts.
331
- const orderedAccounts = [...enabledAccounts];
332
- if (orderedAccounts.length > 1) {
333
- if (accountStrategy === "round-robin") {
334
- // Advance the index on every request for even distribution
335
- const idx = primaryAccountIndex % orderedAccounts.length;
336
- primaryAccountIndex =
337
- (primaryAccountIndex + 1) % orderedAccounts.length;
338
- if (idx > 0) {
339
- const head = orderedAccounts.splice(0, idx);
340
- orderedAccounts.push(...head);
341
- }
342
- }
343
- else {
344
- // fill-first (default): clamp primaryAccountIndex
345
- const idx = primaryAccountIndex % orderedAccounts.length;
346
- if (idx > 0) {
347
- const head = orderedAccounts.splice(0, idx);
348
- orderedAccounts.push(...head);
349
- }
350
- }
351
- }
352
- let lastError;
353
- let sawRateLimit = false;
354
- let sawNetworkError = false;
355
- let sawTransientFailure = false;
356
- let authFailureMessage = null;
357
- const bodyStr = JSON.stringify(body);
358
- const requestStart = Date.now();
359
- const toolCount = Array.isArray(body.tools)
360
- ? body.tools.length
361
- : 0;
362
- const url = "https://api.anthropic.com/v1/messages?beta=true";
363
- const clientHeaders = ctx.headers ?? {};
364
- for (const account of orderedAccounts) {
365
- const accountState = getOrCreateRuntimeState(account.key);
366
- if (accountState.coolingUntil &&
367
- accountState.coolingUntil > Date.now()) {
368
- continue;
369
- }
370
- const logAttempt = (status, errorType, errorMessage) => {
371
- logRequest({
372
- timestamp: new Date().toISOString(),
373
- requestId: ctx.requestId,
374
- method: ctx.method,
375
- path: ctx.path,
376
- model: body.model,
377
- stream: !!body.stream,
378
- toolCount,
379
- account: account.label,
380
- accountType: account.type,
381
- responseStatus: status,
382
- responseTimeMs: Date.now() - requestStart,
383
- ...(errorType ? { errorType } : {}),
384
- ...(errorMessage ? { errorMessage } : {}),
385
- });
386
- };
387
- // Auto-refresh expiring access tokens once before making the request.
388
- if (needsRefresh(account)) {
389
- const refreshed = await refreshToken(account);
390
- if (refreshed.success) {
391
- if (account.persistTarget) {
392
- await persistTokens(account.persistTarget, account);
393
- }
394
- accountState.consecutiveRefreshFailures = 0;
395
- }
396
- else {
397
- accountState.consecutiveRefreshFailures += 1;
398
- lastError = `token refresh failed for account=${account.label}: ${refreshed.error?.slice(0, 200) ?? "unknown"}`;
399
- logger.debug(`[proxy] preflight refresh failed account=${account.label} failures=${accountState.consecutiveRefreshFailures}`);
400
- if (accountState.consecutiveRefreshFailures >=
401
- MAX_CONSECUTIVE_REFRESH_FAILURES) {
402
- await disableAccountUntilReauth(account, accountState);
403
- authFailureMessage = formatReauthMessage(account.label);
404
- logAttempt(401, "authentication_error", String(lastError));
405
- continue;
406
- }
407
- }
408
- }
409
- const isOAuth = account.type === "oauth";
410
- // Decision 6: Passthrough client headers, fill gaps only.
411
- // Start with a copy of incoming client headers, then set
412
- // defaults for anything the client didn't send. Always
413
- // override auth + content-type.
414
- const headers = {};
415
- for (const [hk, hv] of Object.entries(clientHeaders)) {
416
- const lower = hk.toLowerCase();
417
- if (typeof hv === "string" &&
418
- !BLOCKED_UPSTREAM_HEADERS.has(lower)) {
419
- headers[lower] = hv;
420
- }
421
- }
422
- // Always set (override) — auth and content-type are proxy-controlled
423
- headers["content-type"] = "application/json";
424
- if (isOAuth) {
425
- headers["authorization"] = `Bearer ${account.token}`;
426
- }
427
- else {
428
- headers["x-api-key"] = account.token;
429
- delete headers["authorization"];
430
- }
431
- // Defaults: only set when client didn't send them
432
- if (!headers["user-agent"]) {
433
- headers["user-agent"] = "claude-cli/2.1.80 (external, cli)";
434
- }
435
- if (!headers["anthropic-version"]) {
436
- headers["anthropic-version"] = "2023-06-01";
437
- }
438
- // Ensure oauth beta is always present in the beta list
439
- const existingBetas = headers["anthropic-beta"] ?? "";
440
- if (!existingBetas) {
441
- headers["anthropic-beta"] = "oauth-2025-04-20";
442
- }
443
- else if (!existingBetas.includes("oauth")) {
444
- headers["anthropic-beta"] =
445
- `${existingBetas},oauth-2025-04-20`;
446
- }
447
- logger.always(`[proxy] → account=${account.label} (${account.type})`);
448
- recordRequest(account.label, account.type);
449
- // Log full request for debugging (written to ~/.neurolink/logs/proxy-debug-*.jsonl)
450
- const fetchStartMs = Date.now();
451
- let response;
452
- try {
453
- response = await fetch(url, {
454
- method: "POST",
455
- headers,
456
- body: bodyStr,
457
- signal: AbortSignal.timeout(UPSTREAM_FETCH_TIMEOUT_MS),
458
- });
459
- }
460
- catch (fetchErr) {
461
- if (!isRetryableNetworkError(fetchErr)) {
462
- throw fetchErr;
463
- }
464
- // Decision 8: Network errors — immediate rotation, no cooldown
465
- sawNetworkError = true;
466
- recordError(account.label, account.type, 502);
467
- const errorCode = getErrorCode(fetchErr) ?? "unknown";
468
- const errorMessage = fetchErr instanceof Error
469
- ? fetchErr.message
470
- : String(fetchErr);
471
- lastError = errorMessage;
472
- logger.always(`[proxy] fetch error account=${account.label} code=${errorCode} (rotating): ${errorMessage}`);
473
- logAttempt(502, "network_error", errorMessage);
474
- continue;
475
- }
476
- // Check 429 (with Retry-After + exponential backoff) → continue.
477
- if (response.status === 429) {
478
- sawRateLimit = true;
479
- const retryAfter = response.headers.get("retry-after");
480
- let cooldownMs = 0;
481
- if (retryAfter) {
482
- const seconds = parseInt(retryAfter, 10);
483
- if (!Number.isNaN(seconds)) {
484
- cooldownMs = seconds * 1000;
485
- }
486
- else {
487
- const date = new Date(retryAfter);
488
- if (!Number.isNaN(date.getTime())) {
489
- cooldownMs = Math.max(date.getTime() - Date.now(), 1000);
490
- }
491
- }
492
- }
493
- const level = accountState.backoffLevel;
494
- const baseCooldown = cooldownMs > 0 ? cooldownMs : RATE_LIMIT_BACKOFF_BASE_MS;
495
- const backoffMs = Math.min(baseCooldown * Math.pow(2, level), RATE_LIMIT_BACKOFF_CAP_MS);
496
- accountState.coolingUntil = Date.now() + backoffMs;
497
- accountState.backoffLevel += 1;
498
- advancePrimaryIfCurrent(account.key, enabledAccounts.length, orderedAccounts[0]?.key);
499
- recordError(account.label, account.type, 429);
500
- recordCooldown(account.label, account.type, accountState.coolingUntil, accountState.backoffLevel);
501
- lastError = await response.text();
502
- logger.always(`[proxy] ← 429 account=${account.label} backoff-level=${accountState.backoffLevel} cooldown=${Math.round(backoffMs / 1000)}s`);
503
- logAttempt(429, "rate_limit_error", String(lastError));
504
- continue;
505
- }
506
- // On 401 for refreshable OAuth: refresh token and retry before failing over.
507
- if (response.status === 401 &&
508
- account.type === "oauth" &&
509
- account.refreshToken) {
510
- recordError(account.label, account.type, 401);
511
- let authRetrySucceeded = false;
512
- let authRetryError = "received 401 from Anthropic";
513
- for (let authRetry = 0; authRetry < MAX_AUTH_RETRIES; authRetry++) {
514
- logger.always(`[proxy] ← 401 account=${account.label} refreshing (attempt ${authRetry + 1}/${MAX_AUTH_RETRIES})`);
515
- const refreshSucceeded = await refreshToken(account);
516
- if (!refreshSucceeded.success) {
517
- accountState.consecutiveRefreshFailures += 1;
518
- authRetryError = `refresh failed for account=${account.label} attempt ${authRetry + 1}/${MAX_AUTH_RETRIES}: ${refreshSucceeded.error?.slice(0, 200) ?? "unknown"}`;
519
- lastError = authRetryError;
520
- logger.always(`[proxy] ⚠ account=${account.label} refresh failed on attempt ${authRetry + 1}`);
521
- if (accountState.consecutiveRefreshFailures >=
522
- MAX_CONSECUTIVE_REFRESH_FAILURES) {
523
- await disableAccountUntilReauth(account, accountState);
524
- authFailureMessage = formatReauthMessage(account.label);
525
- break;
526
- }
527
- if (authRetry < MAX_AUTH_RETRIES - 1) {
528
- await sleep(2000);
529
- }
530
- continue;
531
- }
532
- if (account.persistTarget) {
533
- await persistTokens(account.persistTarget, account);
534
- }
535
- headers.authorization = `Bearer ${account.token}`;
536
- try {
537
- const retryResp = await fetch(url, {
538
- method: "POST",
539
- headers,
540
- body: bodyStr,
541
- signal: AbortSignal.timeout(UPSTREAM_FETCH_TIMEOUT_MS),
542
- });
543
- if (retryResp.ok) {
544
- authRetrySucceeded = true;
545
- accountState.consecutiveRefreshFailures = 0;
546
- accountState.backoffLevel = 0;
547
- accountState.coolingUntil = undefined;
548
- logger.always(`[proxy] ← 200 account=${account.label} (after ${authRetry + 1} refresh(es))`);
549
- recordSuccess(account.label, account.type);
550
- logAttempt(retryResp.status);
551
- // Capture quota headers after successful auth-retry
552
- {
553
- const retryQuota = parseQuotaHeaders(retryResp.headers);
554
- // eslint-disable-next-line max-depth
555
- if (retryQuota) {
556
- saveAccountQuota(account.label, retryQuota).catch(() => { });
557
- }
558
- }
559
- // eslint-disable-next-line max-depth
560
- if (body.stream && retryResp.body) {
561
- const retryReader = retryResp.body.getReader();
562
- const retryStream = new ReadableStream({
563
- async pull(controller) {
564
- try {
565
- const { done, value } = await retryReader.read();
566
- if (done) {
567
- controller.close();
568
- return;
569
- }
570
- controller.enqueue(value);
571
- }
572
- catch (streamErr) {
573
- const errMsg = streamErr instanceof Error
574
- ? streamErr.message
575
- : String(streamErr);
576
- logger.always(`[proxy] mid-stream error (auth-retry) account=${account.label}: ${errMsg}`);
577
- logStreamError({
578
- timestamp: new Date().toISOString(),
579
- requestId: ctx.requestId,
580
- account: account.label,
581
- model: body.model,
582
- errorMessage: errMsg,
583
- durationMs: Date.now() - fetchStartMs,
584
- });
585
- const errorEvent = `event: error\ndata: ${JSON.stringify({ type: "error", error: { type: "api_error", message: `Upstream stream interrupted: ${errMsg}` } })}\n\n`;
586
- controller.enqueue(new TextEncoder().encode(errorEvent));
587
- controller.close();
588
- }
589
- },
590
- cancel() {
591
- retryReader.cancel();
592
- },
593
- });
594
- const responseHeaders = {
595
- "content-type": "text/event-stream",
596
- "cache-control": "no-cache",
597
- connection: "keep-alive",
598
- };
599
- // eslint-disable-next-line max-depth
600
- for (const h of [
601
- "retry-after",
602
- "anthropic-ratelimit-requests-remaining",
603
- "anthropic-ratelimit-requests-limit",
604
- "anthropic-ratelimit-tokens-remaining",
605
- "anthropic-ratelimit-tokens-limit",
606
- ]) {
607
- const val = retryResp.headers.get(h);
608
- // eslint-disable-next-line max-depth
609
- if (val) {
610
- responseHeaders[h] = val;
611
- }
612
- }
613
- return new Response(retryStream, {
614
- status: retryResp.status,
615
- headers: responseHeaders,
616
- });
617
- }
618
- return retryResp.json();
619
- }
620
- const retryStatus = retryResp.status;
621
- const retryBody = await retryResp.text();
622
- authRetryError = `retry ${authRetry + 1}/${MAX_AUTH_RETRIES} failed with status ${retryStatus}`;
623
- lastError = retryBody;
624
- logger.debug(`[proxy] retry ${authRetry + 1} failed: ${retryStatus} ${retryBody.substring(0, 120)}`);
625
- recordError(account.label, account.type, retryStatus);
626
- if (retryStatus === 429) {
627
- sawRateLimit = true;
628
- const retryAfter = retryResp.headers.get("retry-after");
629
- const parsedRetryAfter = parseInt(retryAfter ?? "", 10);
630
- const cooldownMs = Number.isNaN(parsedRetryAfter)
631
- ? 60_000
632
- : Math.max(1, parsedRetryAfter) * 1000;
633
- accountState.coolingUntil = Date.now() + cooldownMs;
634
- advancePrimaryIfCurrent(account.key, enabledAccounts.length, orderedAccounts[0]?.key);
635
- recordCooldown(account.label, account.type, accountState.coolingUntil, accountState.backoffLevel);
636
- break;
637
- }
638
- if (retryStatus === 401 ||
639
- retryStatus === 402 ||
640
- retryStatus === 403) {
641
- // eslint-disable-next-line max-depth
642
- if (authRetry < MAX_AUTH_RETRIES - 1) {
643
- await sleep(1000);
644
- }
645
- continue;
646
- }
647
- if (isTransientHttpFailure(retryStatus, retryBody)) {
648
- // Decision 8: No cooldown for transient errors — rotate immediately
649
- sawTransientFailure = true;
650
- break;
651
- }
652
- logAttempt(retryStatus, "api_error", summarizeErrorMessage(retryBody));
653
- try {
654
- return JSON.parse(retryBody);
655
- }
656
- catch {
657
- return buildClaudeError(retryStatus, retryBody);
658
- }
659
- }
660
- catch (retryFetchErr) {
661
- // Decision 8: No cooldown for network errors — rotate immediately
662
- sawNetworkError = true;
663
- recordError(account.label, account.type, 502);
664
- const message = retryFetchErr instanceof Error
665
- ? retryFetchErr.message
666
- : String(retryFetchErr);
667
- authRetryError = `network error on retry ${authRetry + 1}: ${message}`;
668
- lastError = authRetryError;
669
- logger.debug(`[proxy] ${authRetryError}`);
670
- break;
671
- }
672
- }
673
- if (!authRetrySucceeded) {
674
- if (!accountState.permanentlyDisabled) {
675
- if (!accountState.coolingUntil ||
676
- accountState.coolingUntil <= Date.now()) {
677
- accountState.coolingUntil =
678
- Date.now() + AUTH_COOLDOWN_MS;
679
- }
680
- recordCooldown(account.label, account.type, accountState.coolingUntil, accountState.backoffLevel);
681
- }
682
- lastError = authRetryError;
683
- logger.always(`[proxy] ⚠ account=${account.label} auth retries exhausted, cooldown=5min`);
684
- logAttempt(401, "authentication_error", authRetryError);
685
- continue;
686
- }
687
- }
688
- if (!response.ok) {
689
- const errBody = await response.text();
690
- // Log full error for debugging
691
- const errRespHeaders = {};
692
- response.headers.forEach((v, k) => {
693
- errRespHeaders[k] = v;
694
- });
695
- logFullRequestResponse({
696
- timestamp: new Date().toISOString(),
697
- requestId: ctx.requestId,
698
- account: account.label,
699
- model: body.model,
700
- stream: !!body.stream,
701
- requestHeaders: redactSensitiveHeaders(headers),
702
- requestBody: {
703
- model: body.model,
704
- max_tokens: body.max_tokens,
705
- stream: body.stream,
706
- system: Array.isArray(body.system)
707
- ? `[${body.system.length} blocks]`
708
- : typeof body.system,
709
- messages: Array.isArray(body.messages)
710
- ? `[${body.messages.length} messages]`
711
- : "?",
712
- tools: Array.isArray(body.tools)
713
- ? `[${body.tools.length} tools]`
714
- : "none",
715
- tool_choice: body.tool_choice,
716
- thinking: body.thinking,
717
- },
718
- requestBodySize: bodyStr.length,
719
- responseStatus: response.status,
720
- responseHeaders: errRespHeaders,
721
- responseBody: errBody.substring(0, 2000),
722
- responseBodySize: errBody.length,
723
- durationMs: Date.now() - fetchStartMs,
724
- });
725
- // Request-shape errors (do not retry).
726
- if (isInvalidRequestError(response.status, errBody)) {
727
- logger.always(`[proxy] ← ${response.status} request-shape error (no retry)`);
728
- logAttempt(response.status, "invalid_request_error", summarizeErrorMessage(errBody));
729
- try {
730
- return JSON.parse(errBody);
731
- }
732
- catch {
733
- return buildClaudeError(response.status, errBody);
734
- }
735
- }
736
- // Auth failures for OAuth accounts without refresh token.
737
- if ((response.status === 401 ||
738
- response.status === 402 ||
739
- response.status === 403) &&
740
- account.type === "oauth" &&
741
- !account.refreshToken) {
742
- recordError(account.label, account.type, response.status);
743
- accountState.consecutiveRefreshFailures += 1;
744
- accountState.coolingUntil = Date.now() + AUTH_COOLDOWN_MS;
745
- recordCooldown(account.label, account.type, accountState.coolingUntil, accountState.backoffLevel);
746
- if (accountState.consecutiveRefreshFailures >=
747
- MAX_CONSECUTIVE_REFRESH_FAILURES) {
748
- await disableAccountUntilReauth(account, accountState);
749
- }
750
- authFailureMessage = formatReauthMessage(account.label);
751
- logger.always(`[proxy] ← ${response.status} account=${account.label} cooldown=5min`);
752
- lastError = errBody;
753
- logAttempt(response.status, "authentication_error", summarizeErrorMessage(errBody));
754
- continue;
755
- }
756
- // Auth failures for API-key accounts.
757
- if ((response.status === 401 ||
758
- response.status === 402 ||
759
- response.status === 403) &&
760
- account.type === "api_key") {
761
- recordError(account.label, account.type, response.status);
762
- authFailureMessage =
763
- "Authentication failed for Anthropic API key credentials. Update ANTHROPIC_API_KEY or re-login with OAuth.";
764
- accountState.coolingUntil = Date.now() + AUTH_COOLDOWN_MS;
765
- recordCooldown(account.label, account.type, accountState.coolingUntil, accountState.backoffLevel);
766
- logger.always(`[proxy] ← ${response.status} account=${account.label} cooldown=5min`);
767
- lastError = errBody;
768
- logAttempt(response.status, "authentication_error", summarizeErrorMessage(errBody));
769
- continue;
770
- }
771
- // 404 is generally model/account specific; return immediately (no cooldown per Decision 8).
772
- if (response.status === 404) {
773
- recordError(account.label, account.type, response.status);
774
- logger.always(`[proxy] ← 404 account=${account.label}`);
775
- logAttempt(404, "not_found_error", summarizeErrorMessage(errBody));
776
- try {
777
- return JSON.parse(errBody);
778
- }
779
- catch {
780
- return buildClaudeError(404, errBody);
781
- }
782
- }
783
- // Decision 8: Transient upstream failures — immediate rotation, NO cooldown.
784
- if (isTransientHttpFailure(response.status, errBody)) {
785
- recordError(account.label, account.type, response.status);
786
- sawTransientFailure = true;
787
- // No cooldown for transient errors (502, 503, etc.) — rotate immediately
788
- logger.always(`[proxy] ← ${response.status} account=${account.label} (transient, rotating)`);
789
- lastError = errBody;
790
- logAttempt(response.status, "api_error", summarizeErrorMessage(errBody));
791
- continue;
792
- }
793
- // Other non-ok errors → return as-is.
794
- recordError(account.label, account.type, response.status);
795
- logger.always(`[proxy] ← ${response.status} account=${account.label}`);
796
- logger.debug(`[claude-proxy] error body: ${errBody.substring(0, 200)}`);
797
- logAttempt(response.status, "api_error", summarizeErrorMessage(errBody));
798
- try {
799
- return JSON.parse(errBody);
800
- }
801
- catch {
802
- return buildClaudeError(response.status, errBody);
803
- }
804
- }
805
- // Success path.
806
- accountState.backoffLevel = 0;
807
- accountState.coolingUntil = undefined;
808
- accountState.consecutiveRefreshFailures = 0;
809
- recordSuccess(account.label, account.type);
810
- logger.always(`[proxy] ← ${response.status} account=${account.label}`);
811
- logAttempt(response.status);
812
- // Capture quota/utilisation headers (fire-and-forget).
813
- const quota = parseQuotaHeaders(response.headers);
814
- if (quota) {
815
- saveAccountQuota(account.label, quota).catch(() => {
816
- // Non-fatal: quota persistence is best-effort
817
- });
818
- }
819
- // Log full request + response headers for debugging
820
- const respHeaders = {};
821
- response.headers.forEach((v, k) => {
822
- respHeaders[k] = v;
823
- });
824
- logFullRequestResponse({
825
- timestamp: new Date().toISOString(),
826
- requestId: ctx.requestId,
827
- account: account.label,
828
- model: body.model,
829
- stream: !!body.stream,
830
- requestHeaders: redactSensitiveHeaders(headers),
831
- requestBody: {
832
- model: body.model,
833
- max_tokens: body.max_tokens,
834
- stream: body.stream,
835
- system: Array.isArray(body.system)
836
- ? `[${body.system.length} blocks]`
837
- : typeof body.system,
838
- messages: Array.isArray(body.messages)
839
- ? `[${body.messages.length} messages]`
840
- : "?",
841
- tools: Array.isArray(body.tools)
842
- ? `[${body.tools.length} tools]`
843
- : "none",
844
- tool_choice: body.tool_choice,
845
- thinking: body.thinking,
846
- metadata: body.metadata ? "present" : "absent",
847
- },
848
- requestBodySize: bodyStr.length,
849
- responseStatus: response.status,
850
- responseHeaders: respHeaders,
851
- durationMs: Date.now() - fetchStartMs,
852
- });
853
- if (body.stream) {
854
- // Bootstrap retry: read first chunk to verify stream is valid.
855
- if (response.body) {
856
- const reader = response.body.getReader();
857
- const firstChunk = await reader.read();
858
- if (firstChunk.done ||
859
- !firstChunk.value ||
860
- firstChunk.value.length === 0) {
861
- // Empty stream — retry with next account.
862
- reader.cancel();
863
- accountState.coolingUntil = Date.now() + 10_000;
864
- recordCooldown(account.label, account.type, accountState.coolingUntil, accountState.backoffLevel);
865
- logger.always(`[proxy] ← empty stream from account=${account.label}, trying next`);
866
- continue;
867
- }
868
- // Stream is valid — create a new ReadableStream with first chunk prepended.
869
- const remainingStream = new ReadableStream({
870
- start(controller) {
871
- controller.enqueue(firstChunk.value);
872
- },
873
- async pull(controller) {
874
- try {
875
- const { done, value } = await reader.read();
876
- if (done) {
877
- controller.close();
878
- return;
879
- }
880
- controller.enqueue(value);
881
- }
882
- catch (streamErr) {
883
- const errMsg = streamErr instanceof Error
884
- ? streamErr.message
885
- : String(streamErr);
886
- logger.always(`[proxy] mid-stream error account=${account.label}: ${errMsg}`);
887
- logStreamError({
888
- timestamp: new Date().toISOString(),
889
- requestId: ctx.requestId,
890
- account: account.label,
891
- model: body.model,
892
- errorMessage: errMsg,
893
- durationMs: Date.now() - fetchStartMs,
894
- });
895
- // Send SSE error event so the client gets a meaningful error
896
- // instead of a raw connection drop
897
- const errorEvent = `event: error\ndata: ${JSON.stringify({ type: "error", error: { type: "api_error", message: `Upstream stream interrupted: ${errMsg}` } })}\n\n`;
898
- controller.enqueue(new TextEncoder().encode(errorEvent));
899
- controller.close();
900
- }
901
- },
902
- cancel() {
903
- reader.cancel();
904
- },
905
- });
906
- // Forward rate limit headers from Anthropic.
907
- const responseHeaders = {
908
- "content-type": "text/event-stream",
909
- "cache-control": "no-cache",
910
- connection: "keep-alive",
911
- };
912
- for (const h of [
913
- "retry-after",
914
- "anthropic-ratelimit-requests-remaining",
915
- "anthropic-ratelimit-requests-limit",
916
- "anthropic-ratelimit-tokens-remaining",
917
- "anthropic-ratelimit-tokens-limit",
918
- ]) {
919
- const val = response.headers.get(h);
920
- if (val) {
921
- responseHeaders[h] = val;
922
- }
923
- }
924
- return new Response(remainingStream, {
925
- status: response.status,
926
- headers: responseHeaders,
927
- });
928
- }
929
- return buildClaudeError(502, "No response body from upstream");
930
- }
931
- // Non-streaming: return JSON directly.
932
- return response.json();
933
- }
934
- // All accounts exhausted — compute earliest recovery time.
935
- const earliestRecovery = orderedAccounts.reduce((min, account) => {
936
- const coolingUntil = getOrCreateRuntimeState(account.key).coolingUntil;
937
- return coolingUntil ? Math.min(min, coolingUntil) : min;
938
- }, Infinity);
939
- const retryAfterSec = Number.isFinite(earliestRecovery)
940
- ? Math.max(1, Math.ceil((earliestRecovery - Date.now()) / 1000))
941
- : 60;
942
- // Try fallback chain (alternative providers)
943
- const chain = modelRouter?.getFallbackChain() ?? [];
944
- for (const fallback of chain) {
945
- try {
946
- logger.always(`[proxy] fallback → ${fallback.provider}/${fallback.model}`);
947
- const parsed = parseClaudeRequest(body);
948
- const opts = {
949
- input: {
950
- text: parsed.prompt,
951
- ...(parsed.images.length > 0
952
- ? { images: parsed.images }
953
- : {}),
954
- },
955
- provider: fallback.provider,
956
- model: fallback.model,
957
- systemPrompt: parsed.systemPrompt,
958
- maxTokens: parsed.maxTokens,
959
- ...(parsed.temperature !== undefined
960
- ? { temperature: parsed.temperature }
961
- : {}),
962
- ...(parsed.topP !== undefined ? { topP: parsed.topP } : {}),
963
- ...(parsed.topK !== undefined ? { topK: parsed.topK } : {}),
964
- ...(parsed.stopSequences?.length
965
- ? { stopSequences: parsed.stopSequences }
966
- : {}),
967
- tools: parsed.tools,
968
- ...(parsed.toolChoice
969
- ? { toolChoice: parsed.toolChoice }
970
- : {}),
971
- ...(parsed.thinkingConfig
972
- ? { thinkingConfig: parsed.thinkingConfig }
973
- : {}),
974
- ...(parsed.conversationMessages?.length
975
- ? {
976
- conversationMessages: parsed.conversationMessages.slice(0, -1),
977
- }
978
- : {}),
979
- maxSteps: 1,
980
- };
981
- if (body.stream) {
982
- const streamResult = await ctx.neurolink.stream(opts);
983
- const serializer = new ClaudeStreamSerializer(body.model, 0);
984
- async function* sseGenerator() {
985
- for (const frame of serializer.start()) {
986
- yield frame;
987
- }
988
- for await (const chunk of streamResult.stream) {
989
- const text = extractText(chunk);
990
- if (text) {
991
- for (const frame of serializer.pushDelta(text)) {
992
- yield frame;
993
- }
994
- }
995
- }
996
- // Emit tool_use blocks if model wants to call tools
997
- if (streamResult.toolCalls?.length) {
998
- for (const tc of streamResult.toolCalls) {
999
- const toolName = tc.toolName ??
1000
- tc.name ??
1001
- "unknown";
1002
- const toolArgs = tc.args ??
1003
- tc.parameters ??
1004
- {};
1005
- for (const frame of serializer.pushToolUse(generateToolUseId(), toolName, toolArgs)) {
1006
- yield frame;
1007
- }
1008
- }
1009
- }
1010
- const reason = streamResult.finishReason ?? "end_turn";
1011
- for (const frame of serializer.finish(0, reason)) {
1012
- yield frame;
1013
- }
1014
- }
1015
- return sseGenerator();
1016
- }
1017
- const streamResult = await ctx.neurolink.stream(opts);
1018
- let collectedText = "";
1019
- for await (const chunk of streamResult.stream) {
1020
- const text = extractText(chunk);
1021
- if (text) {
1022
- collectedText += text;
1023
- }
1024
- }
1025
- const internal = {
1026
- content: collectedText,
1027
- model: streamResult.model,
1028
- finishReason: streamResult.finishReason ?? "end_turn",
1029
- reasoning: undefined,
1030
- usage: streamResult.usage
1031
- ? {
1032
- input: streamResult.usage.input ??
1033
- 0,
1034
- output: streamResult.usage
1035
- .output ?? 0,
1036
- total: streamResult.usage.total ??
1037
- 0,
1038
- }
1039
- : undefined,
1040
- toolCalls: streamResult.toolCalls,
1041
- };
1042
- return serializeClaudeResponse(internal, body.model);
1043
- }
1044
- catch (fallbackErr) {
1045
- logger.debug(`[proxy] fallback ${fallback.provider}/${fallback.model} failed: ${fallbackErr instanceof Error ? fallbackErr.message : String(fallbackErr)}`);
1046
- continue;
1047
- }
1048
- }
1049
- // If no explicit fallback chain is configured, try SDK auto-provider fallback.
1050
- if (chain.length === 0) {
1051
- try {
1052
- logger.always("[proxy] fallback → auto-provider");
1053
- const parsed = parseClaudeRequest(body);
1054
- const opts = {
1055
- input: {
1056
- text: parsed.prompt,
1057
- ...(parsed.images.length > 0
1058
- ? { images: parsed.images }
1059
- : {}),
1060
- },
1061
- systemPrompt: parsed.systemPrompt,
1062
- maxTokens: parsed.maxTokens,
1063
- ...(parsed.temperature !== undefined
1064
- ? { temperature: parsed.temperature }
1065
- : {}),
1066
- ...(parsed.topP !== undefined ? { topP: parsed.topP } : {}),
1067
- ...(parsed.topK !== undefined ? { topK: parsed.topK } : {}),
1068
- ...(parsed.stopSequences?.length
1069
- ? { stopSequences: parsed.stopSequences }
1070
- : {}),
1071
- tools: parsed.tools,
1072
- ...(parsed.toolChoice
1073
- ? { toolChoice: parsed.toolChoice }
1074
- : {}),
1075
- ...(parsed.thinkingConfig
1076
- ? { thinkingConfig: parsed.thinkingConfig }
1077
- : {}),
1078
- ...(parsed.conversationMessages?.length
1079
- ? {
1080
- conversationMessages: parsed.conversationMessages.slice(0, -1),
1081
- }
1082
- : {}),
1083
- maxSteps: 1,
1084
- };
1085
- if (body.stream) {
1086
- const streamResult = await ctx.neurolink.stream(opts);
1087
- const serializer = new ClaudeStreamSerializer(body.model, 0);
1088
- async function* sseGenerator() {
1089
- for (const frame of serializer.start()) {
1090
- yield frame;
1091
- }
1092
- for await (const chunk of streamResult.stream) {
1093
- const text = extractText(chunk);
1094
- if (text) {
1095
- for (const frame of serializer.pushDelta(text)) {
1096
- yield frame;
1097
- }
1098
- }
1099
- }
1100
- // Emit tool_use blocks if model wants to call tools
1101
- if (streamResult.toolCalls?.length) {
1102
- for (const tc of streamResult.toolCalls) {
1103
- const toolName = tc.toolName ??
1104
- tc.name ??
1105
- "unknown";
1106
- const toolArgs = tc.args ??
1107
- tc.parameters ??
1108
- {};
1109
- for (const frame of serializer.pushToolUse(generateToolUseId(), toolName, toolArgs)) {
1110
- yield frame;
1111
- }
1112
- }
1113
- }
1114
- const reason = streamResult.finishReason ?? "end_turn";
1115
- for (const frame of serializer.finish(0, reason)) {
1116
- yield frame;
1117
- }
1118
- }
1119
- return sseGenerator();
1120
- }
1121
- const streamResult = await ctx.neurolink.stream(opts);
1122
- let collectedText = "";
1123
- for await (const chunk of streamResult.stream) {
1124
- const text = extractText(chunk);
1125
- if (text) {
1126
- collectedText += text;
1127
- }
1128
- }
1129
- const internal = {
1130
- content: collectedText,
1131
- model: streamResult.model,
1132
- finishReason: streamResult.finishReason ?? "end_turn",
1133
- reasoning: undefined,
1134
- usage: streamResult.usage
1135
- ? {
1136
- input: streamResult.usage.input ??
1137
- 0,
1138
- output: streamResult.usage
1139
- .output ?? 0,
1140
- total: streamResult.usage.total ??
1141
- 0,
1142
- }
1143
- : undefined,
1144
- toolCalls: streamResult.toolCalls,
1145
- };
1146
- return serializeClaudeResponse(internal, body.model);
1147
- }
1148
- catch (fallbackErr) {
1149
- logger.debug(`[proxy] fallback auto-provider failed: ${fallbackErr instanceof Error
1150
- ? fallbackErr.message
1151
- : String(fallbackErr)}`);
1152
- }
1153
- }
1154
- if (authFailureMessage && !sawRateLimit) {
1155
- return buildClaudeError(401, authFailureMessage);
1156
- }
1157
- if ((sawNetworkError || sawTransientFailure) && !sawRateLimit) {
1158
- return buildClaudeError(502, `All Anthropic accounts failed due to transient upstream/network errors. Last error: ${lastError instanceof Error
1159
- ? lastError.message
1160
- : String(lastError ?? "unknown")}`);
1161
- }
1162
- if (!sawRateLimit) {
1163
- return buildClaudeError(502, `All Anthropic accounts failed. Last error: ${lastError instanceof Error
1164
- ? lastError.message
1165
- : String(lastError ?? "unknown")}`);
1166
- }
1167
- // All accounts AND all fallbacks exhausted — return 429 with Retry-After
1168
- logger.always(`[proxy] all accounts rate-limited, retry in ${retryAfterSec}s`);
1169
- const errorBody = buildClaudeError(429, `All accounts rate-limited. Earliest recovery in ${retryAfterSec}s.`, "overloaded_error");
1170
- return new Response(JSON.stringify(errorBody), {
1171
- status: 429,
1172
- headers: {
1173
- "content-type": "application/json",
1174
- "retry-after": String(retryAfterSec),
1175
- },
1176
- });
1177
- }
1178
- else {
1179
- // ─── TRANSLATION MODE (Claude → Other Provider) ───────
1180
- // Parse into NeuroLink format, call generate/stream, serialize back
1181
- const parsed = parseClaudeRequest(body);
1182
- const historyMessages = parsed.conversationMessages.slice(0, -1);
1183
- const options = {
1184
- input: {
1185
- text: parsed.prompt,
1186
- ...(parsed.images.length > 0
1187
- ? { images: parsed.images }
1188
- : {}),
1189
- },
1190
- provider: route.provider,
1191
- model: route.model,
1192
- systemPrompt: parsed.systemPrompt,
1193
- maxTokens: parsed.maxTokens,
1194
- ...(parsed.temperature !== undefined
1195
- ? { temperature: parsed.temperature }
1196
- : {}),
1197
- ...(parsed.topP !== undefined ? { topP: parsed.topP } : {}),
1198
- ...(parsed.topK !== undefined ? { topK: parsed.topK } : {}),
1199
- ...(parsed.stopSequences?.length
1200
- ? { stopSequences: parsed.stopSequences }
1201
- : {}),
1202
- ...(parsed.thinkingConfig
1203
- ? { thinkingConfig: parsed.thinkingConfig }
1204
- : {}),
1205
- tools: parsed.tools,
1206
- ...(parsed.toolChoice ? { toolChoice: parsed.toolChoice } : {}),
1207
- maxSteps: 1,
1208
- ...(historyMessages.length > 0
1209
- ? { conversationMessages: historyMessages }
1210
- : {}),
1211
- };
1212
- if (body.stream) {
1213
- const streamResult = await ctx.neurolink.stream(options);
1214
- const serializer = new ClaudeStreamSerializer(body.model, 0);
1215
- const KEEPALIVE_INTERVAL_MS = 15_000; // 15 seconds
1216
- // Return a ReadableStream that emits SSE keep-alive comments
1217
- // every ~15s independently of upstream chunk arrival, so
1218
- // intermediaries don't drop the connection during stalls.
1219
- const encoder = new TextEncoder();
1220
- let translationKeepAliveTimer;
1221
- let translationCancelled = false;
1222
- // Hold a reference to the upstream async iterator so
1223
- // we can abort it when the client disconnects.
1224
- let upstreamIterator;
1225
- const translationStream = new ReadableStream({
1226
- async start(controller) {
1227
- // Emit start frames
1228
- for (const frame of serializer.start()) {
1229
- controller.enqueue(encoder.encode(frame));
1230
- }
1231
- // Keep-alive interval — fires even when upstream is stalled
1232
- translationKeepAliveTimer = setInterval(() => {
1233
- try {
1234
- controller.enqueue(encoder.encode(": keep-alive\n\n"));
1235
- }
1236
- catch {
1237
- // Controller already closed — ignore
1238
- }
1239
- }, KEEPALIVE_INTERVAL_MS);
1240
- try {
1241
- const iterable = streamResult.stream;
1242
- upstreamIterator = iterable[Symbol.asyncIterator]();
1243
- // Manually drive the async iterator so we can cancel it
1244
- while (true) {
1245
- if (translationCancelled) {
1246
- break;
1247
- }
1248
- const { value: chunk, done } = await upstreamIterator.next();
1249
- if (done) {
1250
- break;
1251
- }
1252
- if (translationCancelled) {
1253
- break;
1254
- }
1255
- const text = extractText(chunk);
1256
- if (text) {
1257
- for (const frame of serializer.pushDelta(text)) {
1258
- controller.enqueue(encoder.encode(frame));
1259
- }
1260
- }
1261
- }
1262
- // Emit tool_use blocks if model wants to call tools
1263
- if (!translationCancelled &&
1264
- streamResult.toolCalls?.length) {
1265
- for (const tc of streamResult.toolCalls) {
1266
- const toolName = tc.toolName ??
1267
- tc.name ??
1268
- "unknown";
1269
- const toolArgs = tc.args ??
1270
- tc.parameters ??
1271
- {};
1272
- for (const frame of serializer.pushToolUse(generateToolUseId(), toolName, toolArgs)) {
1273
- controller.enqueue(encoder.encode(frame));
1274
- }
1275
- }
1276
- }
1277
- if (!translationCancelled) {
1278
- const reason = streamResult.finishReason ?? "end_turn";
1279
- for (const frame of serializer.finish(0, reason)) {
1280
- controller.enqueue(encoder.encode(frame));
1281
- }
1282
- }
1283
- }
1284
- catch (streamErr) {
1285
- if (translationCancelled) {
1286
- return;
1287
- }
1288
- const errMsg = streamErr instanceof Error
1289
- ? streamErr.message
1290
- : String(streamErr);
1291
- logger.always(`[proxy] mid-stream error (translation mode): ${errMsg}`);
1292
- const errorEvent = `event: error\ndata: ${JSON.stringify({ type: "error", error: { type: "api_error", message: `Upstream stream interrupted: ${errMsg}` } })}\n\n`;
1293
- controller.enqueue(encoder.encode(errorEvent));
1294
- }
1295
- finally {
1296
- if (translationKeepAliveTimer) {
1297
- clearInterval(translationKeepAliveTimer);
1298
- }
1299
- if (!translationCancelled) {
1300
- controller.close();
1301
- }
1302
- }
1303
- },
1304
- cancel() {
1305
- translationCancelled = true;
1306
- if (translationKeepAliveTimer) {
1307
- clearInterval(translationKeepAliveTimer);
1308
- translationKeepAliveTimer = undefined;
1309
- }
1310
- // Propagate cancellation to the upstream provider stream
1311
- if (upstreamIterator?.return) {
1312
- upstreamIterator.return(undefined).catch((cancelErr) => {
1313
- logger.debug(`[proxy] upstream cancel error: ${cancelErr instanceof Error ? cancelErr.message : String(cancelErr)}`);
1314
- });
1315
- }
1316
- },
1317
- });
1318
- return new Response(translationStream, {
1319
- headers: {
1320
- "content-type": "text/event-stream",
1321
- "cache-control": "no-cache",
1322
- connection: "keep-alive",
1323
- },
1324
- });
1325
- }
1326
- const streamResult = await ctx.neurolink.stream(options);
1327
- let collectedText = "";
1328
- for await (const chunk of streamResult.stream) {
1329
- const text = extractText(chunk);
1330
- if (text) {
1331
- collectedText += text;
1332
- }
1333
- }
1334
- const internal = {
1335
- content: collectedText,
1336
- model: streamResult.model,
1337
- finishReason: streamResult.finishReason ?? "end_turn",
1338
- reasoning: undefined,
1339
- usage: streamResult.usage
1340
- ? {
1341
- input: streamResult.usage.input ?? 0,
1342
- output: streamResult.usage.output ?? 0,
1343
- total: streamResult.usage.total ?? 0,
1344
- }
1345
- : undefined,
1346
- toolCalls: streamResult.toolCalls,
1347
- };
1348
- return serializeClaudeResponse(internal, body.model);
1349
- }
1350
- }
1351
- catch (error) {
1352
- logger.error(`[claude-proxy] Generation error for ${body.model}: ${error instanceof Error ? error.message : String(error)}`);
1353
- return buildClaudeError(502, `Generation failed: ${error instanceof Error ? error.message : "unknown error"}`);
1354
- }
1355
- },
1356
- description: "Claude-compatible messages endpoint routed through NeuroLink",
1357
- tags: ["claude-proxy", "messages"],
1358
- streaming: { enabled: true, contentType: "text/event-stream" },
1359
- },
1360
- // =====================================================================
1361
- // GET /v1/models -- List available models
1362
- // =====================================================================
1363
- {
1364
- method: "GET",
1365
- path: `${basePath}/v1/models`,
1366
- handler: async (_ctx) => {
1367
- const models = [
1368
- "claude-sonnet-4-20250514",
1369
- "claude-sonnet-4-5-20250929",
1370
- "claude-haiku-4-5-20241022",
1371
- "claude-opus-4-20250514",
1372
- ];
1373
- return {
1374
- object: "list",
1375
- data: models.map((id) => ({
1376
- id,
1377
- object: "model",
1378
- created: 1700000000,
1379
- owned_by: "anthropic",
1380
- })),
1381
- };
1382
- },
1383
- description: "List available Claude models",
1384
- tags: ["claude-proxy", "models"],
1385
- },
1386
- // =====================================================================
1387
- // POST /v1/messages/count_tokens -- Token counting endpoint
1388
- // =====================================================================
1389
- {
1390
- method: "POST",
1391
- path: `${basePath}/v1/messages/count_tokens`,
1392
- handler: async (ctx) => {
1393
- const body = ctx.body;
1394
- if (!body?.model || !body?.messages) {
1395
- return buildClaudeError(400, "Missing required fields: model, messages");
1396
- }
1397
- // Simple estimation using character-to-token heuristic
1398
- const text = body.messages
1399
- .map((m) => typeof m.content === "string"
1400
- ? m.content
1401
- : JSON.stringify(m.content))
1402
- .join(" ");
1403
- return { input_tokens: Math.ceil(text.length / 4) };
1404
- },
1405
- description: "Count tokens for a messages request",
1406
- tags: ["claude-proxy", "tokens"],
1407
- },
1408
- ],
1409
- };
1410
- }
1411
- // ---------------------------------------------------------------------------
1412
- // Helpers
1413
- // ---------------------------------------------------------------------------
1414
- /**
1415
- * Extract text content from a stream chunk (handles various chunk formats).
1416
- */
1417
- function extractText(chunk) {
1418
- if (typeof chunk === "string") {
1419
- return chunk;
1420
- }
1421
- if (chunk && typeof chunk === "object") {
1422
- const c = chunk;
1423
- // NeuroLink StreamResult chunk format: { content: string }
1424
- if (typeof c.content === "string") {
1425
- return c.content;
1426
- }
1427
- // Vercel AI SDK text delta format
1428
- if (c.type === "text-delta" && typeof c.textDelta === "string") {
1429
- return c.textDelta;
1430
- }
1431
- // Direct text field
1432
- if (typeof c.text === "string") {
1433
- return c.text;
1434
- }
1435
- }
1436
- return null;
1437
- }
1438
- function getOrCreateRuntimeState(accountKey) {
1439
- const existing = accountRuntimeState.get(accountKey);
1440
- if (existing) {
1441
- return existing;
1442
- }
1443
- const initial = {
1444
- coolingUntil: undefined,
1445
- backoffLevel: 0,
1446
- consecutiveRefreshFailures: 0,
1447
- permanentlyDisabled: false,
1448
- };
1449
- accountRuntimeState.set(accountKey, initial);
1450
- return initial;
1451
- }
1452
- async function disableAccountUntilReauth(account, state) {
1453
- state.permanentlyDisabled = true;
1454
- state.coolingUntil = undefined;
1455
- state.backoffLevel = 0;
1456
- // Decision 7 (usage): Persist disabled state to disk so it survives restarts
1457
- try {
1458
- const { tokenStore } = await import("../../auth/tokenStore.js");
1459
- await tokenStore.markDisabled(account.key, "refresh_failed");
1460
- }
1461
- catch (e) {
1462
- logger.debug(`[proxy] failed to persist disabled state for ${account.label}: ${e instanceof Error ? e.message : String(e)}`);
1463
- }
1464
- logger.always(`[proxy] account=${account.label} disabled until re-authentication. Run: neurolink auth login anthropic --method oauth`);
1465
- }
1466
- function formatReauthMessage(labels) {
1467
- const value = Array.isArray(labels) ? labels.join(", ") : labels;
1468
- return `Account(s) require re-authentication: ${value}. Run: neurolink auth login anthropic --method oauth`;
1469
- }
1470
- function summarizeErrorMessage(message, maxLength = 180) {
1471
- const compact = message.replace(/\s+/g, " ").trim();
1472
- if (compact.length <= maxLength) {
1473
- return compact;
1474
- }
1475
- return `${compact.slice(0, maxLength)}...`;
1476
- }
1477
- async function sleep(ms) {
1478
- await new Promise((resolve) => setTimeout(resolve, ms));
1479
- }
1480
- /**
1481
- * Get low-level network error code from an unknown error shape.
1482
- */
1483
- function getErrorCode(error) {
1484
- if (!error || typeof error !== "object") {
1485
- return undefined;
1486
- }
1487
- const directCode = error.code;
1488
- if (typeof directCode === "string") {
1489
- return directCode;
1490
- }
1491
- const cause = error.cause;
1492
- if (!cause || typeof cause !== "object") {
1493
- return undefined;
1494
- }
1495
- const causeCode = cause.code;
1496
- return typeof causeCode === "string" ? causeCode : undefined;
1497
- }
1498
- /**
1499
- * Determine whether a thrown fetch error is a transient connectivity issue.
1500
- */
1501
- function isRetryableNetworkError(error) {
1502
- const code = getErrorCode(error);
1503
- // Check non-retryable codes FIRST — before the string-based heuristic
1504
- // which could false-positive on error messages containing these strings.
1505
- const NON_RETRYABLE_CODES = ["ENOTFOUND"];
1506
- if (code && NON_RETRYABLE_CODES.includes(code)) {
1507
- return false;
1508
- }
1509
- if (code &&
1510
- [
1511
- "ECONNREFUSED",
1512
- "ECONNRESET",
1513
- "ETIMEDOUT",
1514
- "EHOSTUNREACH",
1515
- "UND_ERR_CONNECT_TIMEOUT",
1516
- "UND_ERR_CONNECT",
1517
- "UND_ERR_SOCKET",
1518
- "UND_ERR_HEADERS_TIMEOUT",
1519
- ].includes(code)) {
1520
- return true;
1521
- }
1522
- const message = error instanceof Error ? error.message : String(error);
1523
- const normalized = message.toLowerCase();
1524
- // Exclude ENOTFOUND from string-based heuristic — DNS failures are permanent
1525
- // and rotating accounts won't help since they all hit the same host.
1526
- if (normalized.includes("enotfound")) {
1527
- return false;
1528
- }
1529
- return (normalized.includes("econnrefused") ||
1530
- normalized.includes("econnreset") ||
1531
- normalized.includes("etimedout") ||
1532
- normalized.includes("connection error") ||
1533
- normalized.includes("connect error") ||
1534
- normalized.includes("fetch failed") ||
1535
- normalized.includes("socket hang up"));
1536
- }
1537
- const TRANSIENT_HTTP_STATUSES = new Set([
1538
- 408, 500, 502, 503, 504, 520, 521, 522, 523, 524, 525, 526, 529,
1539
- ]);
1540
- /**
1541
- * Parse a Claude error payload when available.
1542
- */
1543
- export function parseClaudeErrorBody(errBody) {
1544
- try {
1545
- const parsed = JSON.parse(errBody);
1546
- if (parsed &&
1547
- parsed.type === "error" &&
1548
- parsed.error &&
1549
- typeof parsed.error === "object") {
1550
- return {
1551
- errorType: typeof parsed.error.type === "string" ? parsed.error.type : undefined,
1552
- message: typeof parsed.error.message === "string"
1553
- ? parsed.error.message
1554
- : undefined,
1555
- };
1556
- }
1557
- }
1558
- catch {
1559
- // ignore parse errors; caller will use heuristics
1560
- }
1561
- return {};
1562
- }
1563
- /**
1564
- * Detect malformed request errors that should not trigger account/provider failover.
1565
- */
1566
- export function isInvalidRequestError(status, errBody) {
1567
- if (status === 422) {
1568
- return true;
1569
- }
1570
- const parsed = parseClaudeErrorBody(errBody);
1571
- return (parsed.errorType === "invalid_request_error" ||
1572
- errBody.includes("invalid_request_error"));
1573
- }
1574
- /**
1575
- * Detect transient upstream failures that should trigger account/provider failover.
1576
- *
1577
- * Includes Cloudflare 52x statuses and Anthropic 400/api_error wrappers that
1578
- * carry transient HTML responses (e.g. 520 pages) inside `error.message`.
1579
- */
1580
- export function isTransientHttpFailure(status, errBody) {
1581
- if (TRANSIENT_HTTP_STATUSES.has(status)) {
1582
- return true;
1583
- }
1584
- if (status !== 400) {
1585
- return false;
1586
- }
1587
- const parsed = parseClaudeErrorBody(errBody);
1588
- if (parsed.errorType === "overloaded_error") {
1589
- return true;
1590
- }
1591
- if (parsed.errorType !== "api_error") {
1592
- return false;
1593
- }
1594
- const normalized = (parsed.message ?? errBody).toLowerCase();
1595
- return (normalized.includes("<!doctype html") ||
1596
- normalized.includes("error code 520") ||
1597
- normalized.includes("web server is returning an unknown error") ||
1598
- normalized.includes("cloudflare") ||
1599
- normalized.includes("internal server error"));
1600
- }