@juspay/neurolink 9.14.0 → 9.16.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 (241) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/README.md +15 -15
  3. package/dist/adapters/video/videoAnalyzer.d.ts +1 -1
  4. package/dist/adapters/video/videoAnalyzer.js +10 -8
  5. package/dist/auth/anthropicOAuth.d.ts +377 -0
  6. package/dist/auth/anthropicOAuth.js +914 -0
  7. package/dist/auth/index.d.ts +20 -0
  8. package/dist/auth/index.js +29 -0
  9. package/dist/auth/tokenStore.d.ts +225 -0
  10. package/dist/auth/tokenStore.js +521 -0
  11. package/dist/cli/commands/auth.d.ts +50 -0
  12. package/dist/cli/commands/auth.js +1115 -0
  13. package/dist/cli/commands/setup-anthropic.js +1 -14
  14. package/dist/cli/commands/setup-azure.js +1 -12
  15. package/dist/cli/commands/setup-bedrock.js +1 -9
  16. package/dist/cli/commands/setup-google-ai.js +1 -12
  17. package/dist/cli/commands/setup-openai.js +1 -14
  18. package/dist/cli/commands/workflow.d.ts +27 -0
  19. package/dist/cli/commands/workflow.js +216 -0
  20. package/dist/cli/factories/authCommandFactory.d.ts +52 -0
  21. package/dist/cli/factories/authCommandFactory.js +146 -0
  22. package/dist/cli/factories/commandFactory.d.ts +6 -0
  23. package/dist/cli/factories/commandFactory.js +171 -22
  24. package/dist/cli/index.js +0 -1
  25. package/dist/cli/parser.js +14 -2
  26. package/dist/cli/utils/maskCredential.d.ts +11 -0
  27. package/dist/cli/utils/maskCredential.js +23 -0
  28. package/dist/constants/contextWindows.js +107 -16
  29. package/dist/constants/enums.d.ts +119 -15
  30. package/dist/constants/enums.js +182 -22
  31. package/dist/constants/index.d.ts +3 -1
  32. package/dist/constants/index.js +11 -1
  33. package/dist/context/budgetChecker.js +1 -1
  34. package/dist/context/contextCompactor.js +31 -4
  35. package/dist/context/emergencyTruncation.d.ts +21 -0
  36. package/dist/context/emergencyTruncation.js +88 -0
  37. package/dist/context/errorDetection.d.ts +16 -0
  38. package/dist/context/errorDetection.js +48 -1
  39. package/dist/context/errors.d.ts +19 -0
  40. package/dist/context/errors.js +21 -0
  41. package/dist/context/stages/slidingWindowTruncator.d.ts +6 -0
  42. package/dist/context/stages/slidingWindowTruncator.js +159 -24
  43. package/dist/core/baseProvider.js +306 -200
  44. package/dist/core/conversationMemoryManager.js +104 -61
  45. package/dist/core/evaluationProviders.js +16 -33
  46. package/dist/core/factory.js +237 -164
  47. package/dist/core/modules/GenerationHandler.js +175 -116
  48. package/dist/core/modules/MessageBuilder.js +222 -170
  49. package/dist/core/modules/StreamHandler.d.ts +1 -0
  50. package/dist/core/modules/StreamHandler.js +95 -27
  51. package/dist/core/modules/TelemetryHandler.d.ts +10 -1
  52. package/dist/core/modules/TelemetryHandler.js +25 -7
  53. package/dist/core/modules/ToolsManager.js +115 -191
  54. package/dist/core/redisConversationMemoryManager.js +418 -282
  55. package/dist/factories/providerRegistry.d.ts +5 -0
  56. package/dist/factories/providerRegistry.js +20 -2
  57. package/dist/index.d.ts +3 -3
  58. package/dist/index.js +4 -2
  59. package/dist/lib/adapters/video/videoAnalyzer.d.ts +1 -1
  60. package/dist/lib/adapters/video/videoAnalyzer.js +10 -8
  61. package/dist/lib/auth/anthropicOAuth.d.ts +377 -0
  62. package/dist/lib/auth/anthropicOAuth.js +915 -0
  63. package/dist/lib/auth/index.d.ts +20 -0
  64. package/dist/lib/auth/index.js +30 -0
  65. package/dist/lib/auth/tokenStore.d.ts +225 -0
  66. package/dist/lib/auth/tokenStore.js +522 -0
  67. package/dist/lib/constants/contextWindows.js +107 -16
  68. package/dist/lib/constants/enums.d.ts +119 -15
  69. package/dist/lib/constants/enums.js +182 -22
  70. package/dist/lib/constants/index.d.ts +3 -1
  71. package/dist/lib/constants/index.js +11 -1
  72. package/dist/lib/context/budgetChecker.js +1 -1
  73. package/dist/lib/context/contextCompactor.js +31 -4
  74. package/dist/lib/context/emergencyTruncation.d.ts +21 -0
  75. package/dist/lib/context/emergencyTruncation.js +89 -0
  76. package/dist/lib/context/errorDetection.d.ts +16 -0
  77. package/dist/lib/context/errorDetection.js +48 -1
  78. package/dist/lib/context/errors.d.ts +19 -0
  79. package/dist/lib/context/errors.js +22 -0
  80. package/dist/lib/context/stages/slidingWindowTruncator.d.ts +6 -0
  81. package/dist/lib/context/stages/slidingWindowTruncator.js +159 -24
  82. package/dist/lib/core/baseProvider.js +306 -200
  83. package/dist/lib/core/conversationMemoryManager.js +104 -61
  84. package/dist/lib/core/evaluationProviders.js +16 -33
  85. package/dist/lib/core/factory.js +237 -164
  86. package/dist/lib/core/modules/GenerationHandler.js +175 -116
  87. package/dist/lib/core/modules/MessageBuilder.js +222 -170
  88. package/dist/lib/core/modules/StreamHandler.d.ts +1 -0
  89. package/dist/lib/core/modules/StreamHandler.js +95 -27
  90. package/dist/lib/core/modules/TelemetryHandler.d.ts +10 -1
  91. package/dist/lib/core/modules/TelemetryHandler.js +25 -7
  92. package/dist/lib/core/modules/ToolsManager.js +115 -191
  93. package/dist/lib/core/redisConversationMemoryManager.js +418 -282
  94. package/dist/lib/factories/providerRegistry.d.ts +5 -0
  95. package/dist/lib/factories/providerRegistry.js +20 -2
  96. package/dist/lib/index.d.ts +3 -3
  97. package/dist/lib/index.js +4 -2
  98. package/dist/lib/mcp/externalServerManager.js +66 -0
  99. package/dist/lib/mcp/mcpCircuitBreaker.js +24 -0
  100. package/dist/lib/mcp/mcpClientFactory.js +16 -0
  101. package/dist/lib/mcp/toolDiscoveryService.js +32 -6
  102. package/dist/lib/mcp/toolRegistry.js +193 -123
  103. package/dist/lib/models/anthropicModels.d.ts +267 -0
  104. package/dist/lib/models/anthropicModels.js +528 -0
  105. package/dist/lib/neurolink.d.ts +6 -0
  106. package/dist/lib/neurolink.js +1162 -646
  107. package/dist/lib/providers/amazonBedrock.d.ts +1 -1
  108. package/dist/lib/providers/amazonBedrock.js +521 -319
  109. package/dist/lib/providers/anthropic.d.ts +123 -2
  110. package/dist/lib/providers/anthropic.js +873 -27
  111. package/dist/lib/providers/anthropicBaseProvider.js +77 -17
  112. package/dist/lib/providers/googleAiStudio.d.ts +1 -1
  113. package/dist/lib/providers/googleAiStudio.js +292 -227
  114. package/dist/lib/providers/googleVertex.d.ts +36 -1
  115. package/dist/lib/providers/googleVertex.js +553 -260
  116. package/dist/lib/providers/ollama.js +329 -278
  117. package/dist/lib/providers/openAI.js +77 -19
  118. package/dist/lib/providers/sagemaker/parsers.js +3 -3
  119. package/dist/lib/providers/sagemaker/streaming.js +3 -3
  120. package/dist/lib/proxy/proxyFetch.js +81 -48
  121. package/dist/lib/rag/ChunkerFactory.js +1 -1
  122. package/dist/lib/rag/chunkers/MarkdownChunker.d.ts +22 -0
  123. package/dist/lib/rag/chunkers/MarkdownChunker.js +213 -9
  124. package/dist/lib/rag/chunking/markdownChunker.d.ts +16 -0
  125. package/dist/lib/rag/chunking/markdownChunker.js +174 -2
  126. package/dist/lib/rag/pipeline/contextAssembly.js +2 -1
  127. package/dist/lib/rag/ragIntegration.d.ts +18 -1
  128. package/dist/lib/rag/ragIntegration.js +94 -14
  129. package/dist/lib/rag/retrieval/vectorQueryTool.js +21 -4
  130. package/dist/lib/server/abstract/baseServerAdapter.js +4 -1
  131. package/dist/lib/server/adapters/fastifyAdapter.js +35 -30
  132. package/dist/lib/services/server/ai/observability/instrumentation.d.ts +32 -0
  133. package/dist/lib/services/server/ai/observability/instrumentation.js +39 -0
  134. package/dist/lib/telemetry/attributes.d.ts +52 -0
  135. package/dist/lib/telemetry/attributes.js +61 -0
  136. package/dist/lib/telemetry/index.d.ts +3 -0
  137. package/dist/lib/telemetry/index.js +3 -0
  138. package/dist/lib/telemetry/telemetryService.d.ts +6 -0
  139. package/dist/lib/telemetry/telemetryService.js +6 -0
  140. package/dist/lib/telemetry/tracers.d.ts +15 -0
  141. package/dist/lib/telemetry/tracers.js +17 -0
  142. package/dist/lib/telemetry/withSpan.d.ts +9 -0
  143. package/dist/lib/telemetry/withSpan.js +35 -0
  144. package/dist/lib/types/contextTypes.d.ts +10 -0
  145. package/dist/lib/types/errors.d.ts +62 -0
  146. package/dist/lib/types/errors.js +107 -0
  147. package/dist/lib/types/index.d.ts +2 -1
  148. package/dist/lib/types/index.js +2 -0
  149. package/dist/lib/types/providers.d.ts +107 -0
  150. package/dist/lib/types/providers.js +69 -0
  151. package/dist/lib/types/streamTypes.d.ts +14 -0
  152. package/dist/lib/types/subscriptionTypes.d.ts +893 -0
  153. package/dist/lib/types/subscriptionTypes.js +8 -0
  154. package/dist/lib/utils/conversationMemory.js +121 -82
  155. package/dist/lib/utils/logger.d.ts +5 -0
  156. package/dist/lib/utils/logger.js +50 -2
  157. package/dist/lib/utils/messageBuilder.js +22 -42
  158. package/dist/lib/utils/modelDetection.js +3 -3
  159. package/dist/lib/utils/providerConfig.d.ts +167 -0
  160. package/dist/lib/utils/providerConfig.js +619 -9
  161. package/dist/lib/utils/providerRetry.d.ts +41 -0
  162. package/dist/lib/utils/providerRetry.js +114 -0
  163. package/dist/lib/utils/retryability.d.ts +14 -0
  164. package/dist/lib/utils/retryability.js +23 -0
  165. package/dist/lib/utils/sanitizers/svg.js +4 -5
  166. package/dist/lib/utils/tokenEstimation.d.ts +11 -1
  167. package/dist/lib/utils/tokenEstimation.js +19 -4
  168. package/dist/lib/utils/videoAnalysisProcessor.js +7 -3
  169. package/dist/mcp/externalServerManager.js +66 -0
  170. package/dist/mcp/mcpCircuitBreaker.js +24 -0
  171. package/dist/mcp/mcpClientFactory.js +16 -0
  172. package/dist/mcp/toolDiscoveryService.js +32 -6
  173. package/dist/mcp/toolRegistry.js +193 -123
  174. package/dist/models/anthropicModels.d.ts +267 -0
  175. package/dist/models/anthropicModels.js +527 -0
  176. package/dist/neurolink.d.ts +6 -0
  177. package/dist/neurolink.js +1162 -646
  178. package/dist/providers/amazonBedrock.d.ts +1 -1
  179. package/dist/providers/amazonBedrock.js +521 -319
  180. package/dist/providers/anthropic.d.ts +123 -2
  181. package/dist/providers/anthropic.js +873 -27
  182. package/dist/providers/anthropicBaseProvider.js +77 -17
  183. package/dist/providers/googleAiStudio.d.ts +1 -1
  184. package/dist/providers/googleAiStudio.js +292 -227
  185. package/dist/providers/googleVertex.d.ts +36 -1
  186. package/dist/providers/googleVertex.js +553 -260
  187. package/dist/providers/ollama.js +329 -278
  188. package/dist/providers/openAI.js +77 -19
  189. package/dist/providers/sagemaker/parsers.js +3 -3
  190. package/dist/providers/sagemaker/streaming.js +3 -3
  191. package/dist/proxy/proxyFetch.js +81 -48
  192. package/dist/rag/ChunkerFactory.js +1 -1
  193. package/dist/rag/chunkers/MarkdownChunker.d.ts +22 -0
  194. package/dist/rag/chunkers/MarkdownChunker.js +213 -9
  195. package/dist/rag/chunking/markdownChunker.d.ts +16 -0
  196. package/dist/rag/chunking/markdownChunker.js +174 -2
  197. package/dist/rag/pipeline/contextAssembly.js +2 -1
  198. package/dist/rag/ragIntegration.d.ts +18 -1
  199. package/dist/rag/ragIntegration.js +94 -14
  200. package/dist/rag/retrieval/vectorQueryTool.js +21 -4
  201. package/dist/server/abstract/baseServerAdapter.js +4 -1
  202. package/dist/server/adapters/fastifyAdapter.js +35 -30
  203. package/dist/services/server/ai/observability/instrumentation.d.ts +32 -0
  204. package/dist/services/server/ai/observability/instrumentation.js +39 -0
  205. package/dist/telemetry/attributes.d.ts +52 -0
  206. package/dist/telemetry/attributes.js +60 -0
  207. package/dist/telemetry/index.d.ts +3 -0
  208. package/dist/telemetry/index.js +3 -0
  209. package/dist/telemetry/telemetryService.d.ts +6 -0
  210. package/dist/telemetry/telemetryService.js +6 -0
  211. package/dist/telemetry/tracers.d.ts +15 -0
  212. package/dist/telemetry/tracers.js +16 -0
  213. package/dist/telemetry/withSpan.d.ts +9 -0
  214. package/dist/telemetry/withSpan.js +34 -0
  215. package/dist/types/contextTypes.d.ts +10 -0
  216. package/dist/types/errors.d.ts +62 -0
  217. package/dist/types/errors.js +107 -0
  218. package/dist/types/index.d.ts +2 -1
  219. package/dist/types/index.js +2 -0
  220. package/dist/types/providers.d.ts +107 -0
  221. package/dist/types/providers.js +69 -0
  222. package/dist/types/streamTypes.d.ts +14 -0
  223. package/dist/types/subscriptionTypes.d.ts +893 -0
  224. package/dist/types/subscriptionTypes.js +7 -0
  225. package/dist/utils/conversationMemory.js +121 -82
  226. package/dist/utils/logger.d.ts +5 -0
  227. package/dist/utils/logger.js +50 -2
  228. package/dist/utils/messageBuilder.js +22 -42
  229. package/dist/utils/modelDetection.js +3 -3
  230. package/dist/utils/providerConfig.d.ts +167 -0
  231. package/dist/utils/providerConfig.js +619 -9
  232. package/dist/utils/providerRetry.d.ts +41 -0
  233. package/dist/utils/providerRetry.js +113 -0
  234. package/dist/utils/retryability.d.ts +14 -0
  235. package/dist/utils/retryability.js +22 -0
  236. package/dist/utils/sanitizers/svg.js +4 -5
  237. package/dist/utils/tokenEstimation.d.ts +11 -1
  238. package/dist/utils/tokenEstimation.js +19 -4
  239. package/dist/utils/videoAnalysisProcessor.js +7 -3
  240. package/dist/workflow/config.d.ts +26 -26
  241. package/package.json +2 -1
@@ -1,13 +1,213 @@
1
1
  import { createAnthropic } from "@ai-sdk/anthropic";
2
2
  import { streamText } from "ai";
3
+ import { trace, SpanKind, SpanStatusCode } from "@opentelemetry/api";
3
4
  import { AnthropicModels } from "../constants/enums.js";
4
5
  import { BaseProvider } from "../core/baseProvider.js";
5
6
  import { DEFAULT_MAX_STEPS } from "../core/constants.js";
6
7
  import { createProxyFetch } from "../proxy/proxyFetch.js";
7
8
  import { AuthenticationError, NetworkError, ProviderError, RateLimitError, } from "../types/errors.js";
8
9
  import { logger } from "../utils/logger.js";
10
+ import { calculateCost } from "../utils/pricing.js";
9
11
  import { createAnthropicConfig, getProviderModel, validateApiKey, } from "../utils/providerConfig.js";
10
12
  import { composeAbortSignals, createTimeoutController, TimeoutError, } from "../utils/timeout.js";
13
+ import { isModelAvailableForTier, getRecommendedModelForTier, getModelCapabilities, } from "../models/anthropicModels.js";
14
+ import { CLAUDE_CLI_USER_AGENT, CLAUDE_CODE_CLIENT_ID, ANTHROPIC_TOKEN_URL, MCP_TOOL_PREFIX, } from "../auth/anthropicOAuth.js";
15
+ import { homedir } from "os";
16
+ import { readFileSync, existsSync, writeFileSync, mkdirSync, renameSync, } from "fs";
17
+ import { join } from "path";
18
+ import { TOKEN_EXPIRY_BUFFER_MS } from "../constants/enums.js";
19
+ /**
20
+ * Beta headers for Claude Code integration.
21
+ * These enable experimental features:
22
+ * - claude-code-20250219: Claude Code specific features
23
+ * - interleaved-thinking-2025-05-14: Interleaved thinking mode
24
+ * - fine-grained-tool-streaming-2025-05-14: Fine-grained tool streaming
25
+ */
26
+ const ANTHROPIC_BETA_HEADERS = {
27
+ "anthropic-beta": [
28
+ "claude-code-20250219",
29
+ "interleaved-thinking-2025-05-14",
30
+ "fine-grained-tool-streaming-2025-05-14",
31
+ ].join(","),
32
+ };
33
+ /**
34
+ * Creates a custom fetch function for OAuth-authenticated requests.
35
+ * This wrapper applies all required transformations for OAuth mode:
36
+ * - Uses Authorization: Bearer header (NOT x-api-key)
37
+ * - Adds OAuth-required beta headers (oauth-2025-04-20 is critical)
38
+ * - Sets User-Agent to Claude CLI
39
+ * - Adds ?beta=true query parameter to /v1/messages
40
+ * - Prefixes tool names with mcp_
41
+ * - Strips mcp_ prefix from tool names in responses
42
+ *
43
+ * Accepts a getter function instead of a static token so that refreshed
44
+ * tokens are picked up automatically on each request.
45
+ */
46
+ function createOAuthFetch(getToken, includeOptionalBetas = true) {
47
+ return async (input, init) => {
48
+ // Build the URL
49
+ let requestUrl = null;
50
+ try {
51
+ if (typeof input === "string" || input instanceof URL) {
52
+ requestUrl = new URL(input.toString());
53
+ }
54
+ else if (input instanceof Request) {
55
+ requestUrl = new URL(input.url);
56
+ }
57
+ }
58
+ catch {
59
+ requestUrl = null;
60
+ }
61
+ // Add ?beta=true to /v1/messages endpoint
62
+ if (requestUrl &&
63
+ requestUrl.pathname === "/v1/messages" &&
64
+ !requestUrl.searchParams.has("beta")) {
65
+ requestUrl.searchParams.set("beta", "true");
66
+ }
67
+ // Build new headers
68
+ const requestHeaders = new Headers();
69
+ // Copy headers from Request object if present
70
+ if (input instanceof Request) {
71
+ input.headers.forEach((value, key) => {
72
+ requestHeaders.set(key, value);
73
+ });
74
+ }
75
+ // Copy headers from init if present
76
+ if (init?.headers) {
77
+ if (init.headers instanceof Headers) {
78
+ init.headers.forEach((value, key) => {
79
+ requestHeaders.set(key, value);
80
+ });
81
+ }
82
+ else if (Array.isArray(init.headers)) {
83
+ for (const [key, value] of init.headers) {
84
+ if (typeof value !== "undefined") {
85
+ requestHeaders.set(key, String(value));
86
+ }
87
+ }
88
+ }
89
+ else {
90
+ for (const [key, value] of Object.entries(init.headers)) {
91
+ if (typeof value !== "undefined") {
92
+ requestHeaders.set(key, String(value));
93
+ }
94
+ }
95
+ }
96
+ }
97
+ // Check if claude-code beta was requested
98
+ const incomingBeta = requestHeaders.get("anthropic-beta") || "";
99
+ const incomingBetasList = incomingBeta
100
+ .split(",")
101
+ .map((b) => b.trim())
102
+ .filter(Boolean);
103
+ const includeClaudeCode = incomingBetasList.includes("claude-code-20250219");
104
+ // Merge beta headers — oauth-2025-04-20 is always required for OAuth;
105
+ // optional betas (interleaved-thinking) gated on includeOptionalBetas.
106
+ const mergedBetas = [
107
+ "oauth-2025-04-20",
108
+ ...(includeOptionalBetas ? ["interleaved-thinking-2025-05-14"] : []),
109
+ ...(includeClaudeCode ? ["claude-code-20250219"] : []),
110
+ ].join(",");
111
+ // Set OAuth authorization (Bearer token, NOT x-api-key)
112
+ // Call getToken() each time so refreshed tokens are used automatically.
113
+ requestHeaders.set("authorization", `Bearer ${getToken()}`);
114
+ requestHeaders.set("anthropic-beta", mergedBetas);
115
+ requestHeaders.set("user-agent", CLAUDE_CLI_USER_AGENT);
116
+ requestHeaders.delete("x-api-key");
117
+ logger.debug("[createOAuthFetch] Making OAuth request:", {
118
+ url: requestUrl?.toString() || input.toString(),
119
+ hasAuthorization: requestHeaders.has("authorization"),
120
+ authType: "Bearer",
121
+ anthropicBeta: requestHeaders.get("anthropic-beta"),
122
+ userAgent: requestHeaders.get("user-agent"),
123
+ });
124
+ // Add mcp_ prefix to tool names in request body
125
+ let body = init?.body;
126
+ if (body && typeof body === "string") {
127
+ try {
128
+ const parsed = JSON.parse(body);
129
+ // Add prefix to tools definitions
130
+ if (parsed.tools && Array.isArray(parsed.tools)) {
131
+ parsed.tools = parsed.tools.map((tool) => ({
132
+ ...tool,
133
+ name: tool.name ? `${MCP_TOOL_PREFIX}${tool.name}` : tool.name,
134
+ }));
135
+ }
136
+ // Add prefix to tool_use blocks in messages
137
+ if (parsed.messages && Array.isArray(parsed.messages)) {
138
+ parsed.messages = parsed.messages.map((msg) => {
139
+ if (msg.content && Array.isArray(msg.content)) {
140
+ msg.content = msg.content.map((block) => {
141
+ const b = block;
142
+ if (b.type === "tool_use" && b.name) {
143
+ return {
144
+ ...b,
145
+ name: `${MCP_TOOL_PREFIX}${b.name}`,
146
+ };
147
+ }
148
+ return block;
149
+ });
150
+ }
151
+ return msg;
152
+ });
153
+ }
154
+ body = JSON.stringify(parsed);
155
+ }
156
+ catch {
157
+ // Ignore JSON parse errors
158
+ }
159
+ }
160
+ // Make the request
161
+ const response = await fetch(requestUrl?.toString() ||
162
+ (input instanceof Request ? input.url : input.toString()), {
163
+ ...init,
164
+ body,
165
+ headers: requestHeaders,
166
+ });
167
+ // Transform streaming response to rename tools back (remove mcp_ prefix).
168
+ // Uses a small carry buffer to handle cross-chunk boundary splits of
169
+ // `"name": "mcp_..."` patterns.
170
+ if (response.body) {
171
+ const reader = response.body.getReader();
172
+ const decoder = new TextDecoder();
173
+ const encoder = new TextEncoder();
174
+ // Carry tail covers the longest possible partial match: `"name": "mcp_`
175
+ const CARRY_TAIL = 24;
176
+ let carry = "";
177
+ const stream = new ReadableStream({
178
+ async pull(controller) {
179
+ const { done, value } = await reader.read();
180
+ if (done) {
181
+ // Flush any remaining carry
182
+ if (carry) {
183
+ const flushed = carry.replace(/"name"\s*:\s*"mcp_([^"]+)"/g, '"name": "$1"');
184
+ controller.enqueue(encoder.encode(flushed));
185
+ carry = "";
186
+ }
187
+ controller.close();
188
+ return;
189
+ }
190
+ const chunkText = decoder.decode(value, { stream: true });
191
+ const combined = carry + chunkText;
192
+ const replaced = combined.replace(/"name"\s*:\s*"mcp_([^"]+)"/g, '"name": "$1"');
193
+ // Keep a small tail as carry to cover partial matches at chunk boundary
194
+ const safeLen = Math.max(0, replaced.length - CARRY_TAIL);
195
+ carry = replaced.slice(safeLen);
196
+ const toEmit = replaced.slice(0, safeLen);
197
+ if (toEmit) {
198
+ controller.enqueue(encoder.encode(toEmit));
199
+ }
200
+ },
201
+ });
202
+ return new Response(stream, {
203
+ status: response.status,
204
+ statusText: response.statusText,
205
+ headers: response.headers,
206
+ });
207
+ }
208
+ return response;
209
+ };
210
+ }
11
211
  // Configuration helpers - now using consolidated utility
12
212
  const getAnthropicApiKey = () => {
13
213
  return validateApiKey(createAnthropicConfig());
@@ -15,28 +215,601 @@ const getAnthropicApiKey = () => {
15
215
  const getDefaultAnthropicModel = () => {
16
216
  return getProviderModel("ANTHROPIC_MODEL", AnthropicModels.CLAUDE_3_5_SONNET);
17
217
  };
218
+ const streamTracer = trace.getTracer("neurolink.provider.anthropic");
219
+ /**
220
+ * Get OAuth token from stored credentials file or environment.
221
+ * Priority:
222
+ * 1. Stored credentials file (~/.neurolink/anthropic-credentials.json)
223
+ * 2. Environment variables (ANTHROPIC_OAUTH_TOKEN or CLAUDE_OAUTH_TOKEN)
224
+ */
225
+ const getOAuthToken = () => {
226
+ // First, check stored credentials file (highest priority)
227
+ try {
228
+ const credentialsPath = join(homedir(), ".neurolink", "anthropic-credentials.json");
229
+ if (existsSync(credentialsPath)) {
230
+ const credentialsContent = readFileSync(credentialsPath, "utf-8");
231
+ const credentials = JSON.parse(credentialsContent);
232
+ if (credentials.type === "oauth" && credentials.oauth?.accessToken) {
233
+ logger.debug("[AnthropicProvider] Using OAuth token from stored credentials file");
234
+ return credentials.oauth;
235
+ }
236
+ }
237
+ }
238
+ catch (error) {
239
+ logger.debug("[AnthropicProvider] Failed to read stored credentials:", error);
240
+ }
241
+ // Fallback to environment variables
242
+ const tokenString = process.env.ANTHROPIC_OAUTH_TOKEN || process.env.CLAUDE_OAUTH_TOKEN;
243
+ if (!tokenString) {
244
+ return null;
245
+ }
246
+ // Try to parse as JSON (for full token object with refresh token and expiry)
247
+ try {
248
+ const parsed = JSON.parse(tokenString);
249
+ if (typeof parsed === "object" && parsed.accessToken) {
250
+ return parsed;
251
+ }
252
+ // If it's a simple string in JSON, use it as access token
253
+ if (typeof parsed === "string") {
254
+ return { accessToken: parsed };
255
+ }
256
+ }
257
+ catch {
258
+ // Not JSON, treat as plain access token string
259
+ }
260
+ // Treat as plain access token string
261
+ return { accessToken: tokenString };
262
+ };
263
+ /**
264
+ * Detect subscription tier from environment or token.
265
+ * Environment variable ANTHROPIC_SUBSCRIPTION_TIER takes precedence.
266
+ */
267
+ const detectSubscriptionTier = (oauthToken) => {
268
+ // Check explicit environment variable first
269
+ const envTier = process.env.ANTHROPIC_SUBSCRIPTION_TIER?.toLowerCase();
270
+ if (envTier) {
271
+ const validTiers = [
272
+ "free",
273
+ "pro",
274
+ "max",
275
+ "max_5",
276
+ "max_20",
277
+ "api",
278
+ ];
279
+ if (validTiers.includes(envTier)) {
280
+ logger.debug("[detectSubscriptionTier] Using environment override", {
281
+ tier: envTier,
282
+ });
283
+ return envTier;
284
+ }
285
+ logger.warn("[detectSubscriptionTier] Invalid ANTHROPIC_SUBSCRIPTION_TIER", {
286
+ value: envTier,
287
+ validTiers,
288
+ });
289
+ }
290
+ // If using OAuth, default to 'pro' (most common subscription tier)
291
+ if (oauthToken) {
292
+ // Check if token scopes indicate tier (future-proofing)
293
+ const scopes = oauthToken.scopes ?? [];
294
+ let detectedTier = "pro";
295
+ if (scopes.includes("max_20")) {
296
+ detectedTier = "max_20";
297
+ }
298
+ else if (scopes.includes("max_5")) {
299
+ detectedTier = "max_5";
300
+ }
301
+ else if (scopes.includes("max")) {
302
+ detectedTier = "max";
303
+ }
304
+ logger.debug("[detectSubscriptionTier] Detected from OAuth token", {
305
+ tier: detectedTier,
306
+ scopes,
307
+ });
308
+ return detectedTier;
309
+ }
310
+ // Default to 'api' for API key authentication
311
+ logger.debug("[detectSubscriptionTier] No OAuth token, defaulting to API tier");
312
+ return "api";
313
+ };
314
+ /**
315
+ * Determine authentication method based on available credentials.
316
+ * OAuth takes precedence over API key if both are available.
317
+ */
318
+ const detectAuthMethod = (oauthToken) => {
319
+ // OAuth takes precedence if available
320
+ const method = oauthToken ? "oauth" : "api_key";
321
+ logger.debug("[detectAuthMethod] Auth method resolved", {
322
+ method,
323
+ hasOAuthToken: !!oauthToken,
324
+ });
325
+ return method;
326
+ };
327
+ /**
328
+ * Parse rate limit information from Anthropic API response headers.
329
+ * @param headers - Response headers from Anthropic API
330
+ * @returns Parsed rate limit information
331
+ */
332
+ const parseRateLimitHeaders = (headers) => {
333
+ const getHeader = (name) => {
334
+ if (headers instanceof Headers) {
335
+ return headers.get(name);
336
+ }
337
+ return headers[name] || headers[name.toLowerCase()] || null;
338
+ };
339
+ const parseNumber = (value) => {
340
+ if (!value) {
341
+ return undefined;
342
+ }
343
+ const num = parseInt(value, 10);
344
+ return isNaN(num) ? undefined : num;
345
+ };
346
+ return {
347
+ requestsLimit: parseNumber(getHeader("anthropic-ratelimit-requests-limit")),
348
+ requestsRemaining: parseNumber(getHeader("anthropic-ratelimit-requests-remaining")),
349
+ requestsReset: getHeader("anthropic-ratelimit-requests-reset") || undefined,
350
+ tokensLimit: parseNumber(getHeader("anthropic-ratelimit-tokens-limit")),
351
+ tokensRemaining: parseNumber(getHeader("anthropic-ratelimit-tokens-remaining")),
352
+ tokensReset: getHeader("anthropic-ratelimit-tokens-reset") || undefined,
353
+ retryAfter: parseNumber(getHeader("retry-after")),
354
+ };
355
+ };
18
356
  /**
19
357
  * Anthropic Provider v2 - BaseProvider Implementation
20
- * Fixed syntax and enhanced with proper error handling
358
+ * Enhanced with OAuth support, subscription tiers, and beta headers for Claude Code integration.
21
359
  */
22
360
  export class AnthropicProvider extends BaseProvider {
23
361
  model;
24
- constructor(modelName, sdk) {
25
- super(modelName, "anthropic", sdk);
26
- // Initialize Anthropic model with API key validation and proxy support
27
- const apiKey = getAnthropicApiKey();
28
- // Create Anthropic instance with proxy fetch
29
- const anthropic = createAnthropic({
30
- apiKey: apiKey,
31
- fetch: createProxyFetch(),
362
+ authMethod;
363
+ subscriptionTier;
364
+ enableBetaFeatures;
365
+ oauthToken;
366
+ lastResponseMetadata = null;
367
+ usageInfo = null;
368
+ refreshPromise;
369
+ /**
370
+ * Create a new Anthropic provider instance.
371
+ *
372
+ * @param modelName - Optional model name to use (defaults to CLAUDE_3_5_SONNET)
373
+ * @param sdk - Optional NeuroLink SDK instance
374
+ * @param config - Optional configuration options for auth, subscription tier, and beta features
375
+ */
376
+ constructor(modelName, sdk, config) {
377
+ // Pre-compute effective model with tier validation before calling super
378
+ const oauthToken = config?.oauthToken ?? getOAuthToken();
379
+ const subscriptionTier = config?.subscriptionTier ?? detectSubscriptionTier(oauthToken);
380
+ const targetModel = modelName || getDefaultAnthropicModel();
381
+ // Determine effective model based on tier access
382
+ let effectiveModel = targetModel;
383
+ if (subscriptionTier !== "api" &&
384
+ !isModelAvailableForTier(targetModel, subscriptionTier)) {
385
+ effectiveModel = getRecommendedModelForTier(subscriptionTier);
386
+ logger.warn("Model not available for subscription tier, using recommended model", {
387
+ requestedModel: targetModel,
388
+ subscriptionTier,
389
+ recommendedModel: effectiveModel,
390
+ });
391
+ }
392
+ super(effectiveModel, "anthropic", sdk);
393
+ // Apply configuration with defaults
394
+ this.enableBetaFeatures = config?.enableBetaFeatures ?? true;
395
+ // Store computed values
396
+ this.oauthToken = oauthToken;
397
+ this.subscriptionTier = subscriptionTier;
398
+ // Determine auth method - config takes precedence, then auto-detect
399
+ this.authMethod = config?.authMethod ?? detectAuthMethod(this.oauthToken);
400
+ // Build headers based on auth method and subscription tier
401
+ const headers = this.getAuthHeaders();
402
+ // Create Anthropic instance based on auth method
403
+ let anthropic;
404
+ logger.debug("[AnthropicProvider] Constructor - checking OAuth:", {
405
+ authMethod: this.authMethod,
406
+ hasOAuthToken: !!this.oauthToken,
407
+ hasAccessToken: !!this.oauthToken?.accessToken,
32
408
  });
33
- // Initialize Anthropic model with proxy-aware instance
409
+ if (this.authMethod === "oauth" && this.oauthToken) {
410
+ // OAuth authentication - use custom fetch wrapper that handles:
411
+ // - Bearer token authorization
412
+ // - OAuth beta headers (oauth-2025-04-20, NOT claude-code-20250219)
413
+ // - User-Agent spoofing
414
+ // - ?beta=true query param
415
+ // - Tool name prefixing/stripping
416
+ logger.debug("[AnthropicProvider] Creating OAuth fetch wrapper...");
417
+ // Pass a getter so the fetch wrapper always uses the current token,
418
+ // even after an automatic token refresh.
419
+ // oauthToken is guaranteed non-null here (checked by the enclosing if-guard).
420
+ const tokenRef = this.oauthToken;
421
+ const oauthFetch = createOAuthFetch(() => tokenRef.accessToken, this.enableBetaFeatures);
422
+ // For OAuth, we use a dummy API key since our fetch wrapper handles auth
423
+ // IMPORTANT: Do NOT pass beta headers here - our fetch wrapper handles them
424
+ // The claude-code-20250219 beta header triggers "credential only for Claude Code" error
425
+ anthropic = createAnthropic({
426
+ apiKey: "oauth-authenticated", // Placeholder, actual auth is in fetch wrapper
427
+ // Note: No headers passed - fetch wrapper sets oauth-2025-04-20 beta header
428
+ fetch: oauthFetch,
429
+ });
430
+ logger.debug("[AnthropicProvider] Anthropic SDK created with OAuth fetch wrapper");
431
+ logger.debug("Anthropic Provider initialized with OAuth", {
432
+ modelName: this.modelName,
433
+ provider: this.providerName,
434
+ authMethod: this.authMethod,
435
+ subscriptionTier: this.subscriptionTier,
436
+ enableBetaFeatures: this.enableBetaFeatures,
437
+ hasRefreshToken: !!this.oauthToken.refreshToken,
438
+ tokenExpiry: this.oauthToken.expiresAt
439
+ ? new Date(this.oauthToken.expiresAt).toISOString()
440
+ : "none",
441
+ });
442
+ }
443
+ else {
444
+ // Traditional API key authentication
445
+ const apiKeyToUse = config?.apiKey ?? getAnthropicApiKey();
446
+ anthropic = createAnthropic({
447
+ apiKey: apiKeyToUse,
448
+ headers,
449
+ fetch: createProxyFetch(),
450
+ });
451
+ logger.debug("Anthropic Provider initialized with API key", {
452
+ modelName: this.modelName,
453
+ provider: this.providerName,
454
+ authMethod: this.authMethod,
455
+ subscriptionTier: this.subscriptionTier,
456
+ enableBetaFeatures: this.enableBetaFeatures,
457
+ });
458
+ }
459
+ // Initialize Anthropic model with configured instance
34
460
  this.model = anthropic(this.modelName || getDefaultAnthropicModel());
461
+ // Initialize usage tracking
462
+ this.usageInfo = {
463
+ messagesUsed: 0,
464
+ messagesRemaining: -1, // Unknown until we get rate limit headers
465
+ tokensUsed: 0,
466
+ tokensRemaining: -1,
467
+ inputTokensUsed: 0,
468
+ outputTokensUsed: 0,
469
+ lastRequestTimestamp: 0,
470
+ isRateLimited: false,
471
+ requestCount: 0,
472
+ messageQuotaPercent: 0,
473
+ tokenQuotaPercent: 0,
474
+ };
35
475
  logger.debug("Anthropic Provider v2 initialized", {
36
476
  modelName: this.modelName,
37
477
  provider: this.providerName,
478
+ authMethod: this.authMethod,
479
+ subscriptionTier: this.subscriptionTier,
480
+ enableBetaFeatures: this.enableBetaFeatures,
481
+ betaFeatures: this.enableBetaFeatures
482
+ ? ANTHROPIC_BETA_HEADERS["anthropic-beta"]
483
+ : "disabled",
38
484
  });
39
485
  }
486
+ /**
487
+ * Get authentication headers based on current auth method and configuration.
488
+ *
489
+ * @returns Headers object containing auth and beta feature headers
490
+ */
491
+ getAuthHeaders() {
492
+ const headers = {};
493
+ // Add beta headers if enabled
494
+ if (this.enableBetaFeatures) {
495
+ headers["anthropic-beta"] = ANTHROPIC_BETA_HEADERS["anthropic-beta"];
496
+ }
497
+ // Add subscription-specific headers if applicable
498
+ if (this.subscriptionTier !== "api") {
499
+ headers["x-subscription-tier"] = this.subscriptionTier;
500
+ }
501
+ return headers;
502
+ }
503
+ /**
504
+ * Validate if a model is accessible with the current subscription tier.
505
+ *
506
+ * @param model - The model ID to validate
507
+ * @returns true if the model is accessible, false otherwise
508
+ *
509
+ * @example
510
+ * ```typescript
511
+ * const provider = new AnthropicProvider();
512
+ * if (provider.validateModelAccess("claude-opus-4-5-20251101")) {
513
+ * // Use the model
514
+ * } else {
515
+ * // Fall back to a different model or show upgrade prompt
516
+ * }
517
+ * ```
518
+ */
519
+ validateModelAccess(model) {
520
+ // API tier has access to all models
521
+ if (this.subscriptionTier === "api") {
522
+ return true;
523
+ }
524
+ const hasAccess = isModelAvailableForTier(model, this.subscriptionTier);
525
+ if (!hasAccess) {
526
+ logger.debug("[validateModelAccess] Model not available for tier", {
527
+ model,
528
+ tier: this.subscriptionTier,
529
+ });
530
+ }
531
+ return hasAccess;
532
+ }
533
+ /**
534
+ * Get current usage information.
535
+ *
536
+ * Returns usage tracking data including messages sent, tokens consumed,
537
+ * and remaining quotas. This information is updated after each API request.
538
+ *
539
+ * @returns Current usage info or null if no requests have been made
540
+ *
541
+ * @example
542
+ * ```typescript
543
+ * const usage = provider.getUsageInfo();
544
+ * if (usage && usage.tokenQuotaPercent > 80) {
545
+ * console.warn("Approaching token quota limit");
546
+ * }
547
+ * ```
548
+ */
549
+ getUsageInfo() {
550
+ return this.usageInfo;
551
+ }
552
+ /**
553
+ * Check if beta features are enabled for this provider instance.
554
+ *
555
+ * @returns true if beta features are enabled
556
+ */
557
+ areBetaFeaturesEnabled() {
558
+ return this.enableBetaFeatures;
559
+ }
560
+ /**
561
+ * Get model capabilities for the current model.
562
+ *
563
+ * @returns The model capabilities or undefined if not found
564
+ */
565
+ getModelCapabilities() {
566
+ return getModelCapabilities(this.modelName || this.getDefaultModel());
567
+ }
568
+ /**
569
+ * Get the current subscription tier.
570
+ * @returns The detected or configured subscription tier
571
+ */
572
+ getSubscriptionTier() {
573
+ return this.subscriptionTier;
574
+ }
575
+ /**
576
+ * Get the authentication method being used.
577
+ * @returns The current authentication method
578
+ */
579
+ getAuthMethod() {
580
+ return this.authMethod;
581
+ }
582
+ /**
583
+ * Refresh OAuth token if needed and possible.
584
+ * This method checks if the token is expired or about to expire,
585
+ * and attempts to refresh it using the refresh token if available.
586
+ *
587
+ * @returns Promise that resolves when refresh is complete (or not needed)
588
+ * @throws Error if refresh is needed but fails
589
+ */
590
+ async refreshAuthIfNeeded() {
591
+ // Only applicable for OAuth authentication
592
+ if (this.authMethod !== "oauth" || !this.oauthToken) {
593
+ logger.debug("Token refresh not applicable for API key authentication");
594
+ return;
595
+ }
596
+ // Check if token has expiry information
597
+ if (!this.oauthToken.expiresAt) {
598
+ logger.debug("Token has no expiry information, assuming valid");
599
+ return;
600
+ }
601
+ // expiresAt is stored as Unix milliseconds (matching how auth status/refresh stores it).
602
+ // Compare against Date.now() so both sides are in milliseconds.
603
+ const now = Date.now();
604
+ const isExpired = this.oauthToken.expiresAt <= now;
605
+ const isExpiringSoon = this.oauthToken.expiresAt <= now + TOKEN_EXPIRY_BUFFER_MS;
606
+ if (!isExpired && !isExpiringSoon) {
607
+ logger.debug("OAuth token is still valid", {
608
+ expiresInMs: this.oauthToken.expiresAt - now,
609
+ });
610
+ return;
611
+ }
612
+ // Check if we have a refresh token
613
+ if (!this.oauthToken.refreshToken) {
614
+ if (isExpired) {
615
+ throw new AuthenticationError("OAuth token expired and no refresh token available. Please re-authenticate.", this.providerName);
616
+ }
617
+ logger.warn("OAuth token expiring soon but no refresh token available", {
618
+ expiresInMs: this.oauthToken.expiresAt - now,
619
+ });
620
+ return;
621
+ }
622
+ // Serialize concurrent refresh attempts — if a refresh is already in flight,
623
+ // wait for it rather than issuing a duplicate request.
624
+ if (this.refreshPromise) {
625
+ await this.refreshPromise;
626
+ return;
627
+ }
628
+ // Attempt to refresh the token using the correct Anthropic token endpoint.
629
+ logger.info("Refreshing OAuth token", {
630
+ isExpired,
631
+ expiresInMs: this.oauthToken.expiresAt - now,
632
+ });
633
+ // Capture the token reference before entering the async IIFE;
634
+ // the enclosing guards already verified both fields are non-null.
635
+ const tokenRef = this.oauthToken;
636
+ const refreshToken = tokenRef.refreshToken;
637
+ this.refreshPromise = (async () => {
638
+ const REFRESH_TIMEOUT_MS = 30_000;
639
+ const controller = new AbortController();
640
+ const timeoutId = setTimeout(() => controller.abort(), REFRESH_TIMEOUT_MS);
641
+ const response = await fetch(ANTHROPIC_TOKEN_URL, {
642
+ method: "POST",
643
+ headers: {
644
+ "Content-Type": "application/x-www-form-urlencoded",
645
+ "User-Agent": CLAUDE_CLI_USER_AGENT,
646
+ },
647
+ body: new URLSearchParams({
648
+ grant_type: "refresh_token",
649
+ refresh_token: refreshToken,
650
+ client_id: CLAUDE_CODE_CLIENT_ID,
651
+ }),
652
+ signal: controller.signal,
653
+ });
654
+ clearTimeout(timeoutId);
655
+ if (!response.ok) {
656
+ const errorText = await response.text();
657
+ throw new AuthenticationError(`Failed to refresh OAuth token: ${response.status} ${errorText}`, this.providerName);
658
+ }
659
+ const newToken = (await response.json());
660
+ // Mutate the existing oauthToken object in-place so that the fetch wrapper
661
+ // closure (which captured the object reference, not a copy) picks up the
662
+ // new accessToken automatically on the next request.
663
+ // Store expiresAt as milliseconds to match the format used by auth status/refresh.
664
+ tokenRef.accessToken = newToken.access_token;
665
+ tokenRef.refreshToken = newToken.refresh_token || tokenRef.refreshToken;
666
+ tokenRef.expiresAt = newToken.expires_in
667
+ ? Date.now() + newToken.expires_in * 1000
668
+ : undefined;
669
+ tokenRef.tokenType = newToken.token_type || "Bearer";
670
+ const updatedToken = tokenRef;
671
+ // Persist the refreshed token to disk atomically (tmp + rename) so
672
+ // subsequent provider instances and the CLI pick up the new credentials.
673
+ try {
674
+ const credentialsDir = join(homedir(), ".neurolink");
675
+ if (!existsSync(credentialsDir)) {
676
+ mkdirSync(credentialsDir, { recursive: true });
677
+ }
678
+ const credentialsPath = join(credentialsDir, "anthropic-credentials.json");
679
+ const tmpPath = `${credentialsPath}.tmp`;
680
+ const existingRaw = existsSync(credentialsPath)
681
+ ? JSON.parse(readFileSync(credentialsPath, "utf-8"))
682
+ : {};
683
+ const updated = {
684
+ ...existingRaw,
685
+ type: "oauth",
686
+ oauth: updatedToken,
687
+ updatedAt: Date.now(),
688
+ };
689
+ writeFileSync(tmpPath, JSON.stringify(updated, null, 2), {
690
+ mode: 0o600,
691
+ });
692
+ renameSync(tmpPath, credentialsPath);
693
+ logger.debug("Refreshed OAuth credentials persisted to disk");
694
+ }
695
+ catch (persistError) {
696
+ // Non-fatal: in-memory token is already updated; next CLI start will
697
+ // need a manual refresh but the current session will work.
698
+ logger.warn("Failed to persist refreshed OAuth token to disk", {
699
+ error: persistError instanceof Error
700
+ ? persistError.message
701
+ : String(persistError),
702
+ });
703
+ }
704
+ logger.info("OAuth token refreshed successfully", {
705
+ hasNewRefreshToken: !!newToken.refresh_token,
706
+ expiresIn: newToken.expires_in,
707
+ });
708
+ })();
709
+ try {
710
+ await this.refreshPromise;
711
+ }
712
+ catch (error) {
713
+ if (error instanceof AuthenticationError) {
714
+ throw error;
715
+ }
716
+ throw new AuthenticationError(`Failed to refresh OAuth token: ${error instanceof Error ? error.message : String(error)}`, this.providerName);
717
+ }
718
+ finally {
719
+ this.refreshPromise = undefined;
720
+ }
721
+ }
722
+ /**
723
+ * Get the last response metadata including rate limit information.
724
+ * @returns The last response metadata or null if no request has been made
725
+ */
726
+ getLastResponseMetadata() {
727
+ return this.lastResponseMetadata;
728
+ }
729
+ /**
730
+ * Update response metadata from API response headers.
731
+ * This should be called after each API request to track rate limits.
732
+ * @param headers - Response headers from the API
733
+ * @param requestId - Optional request ID
734
+ */
735
+ updateResponseMetadata(headers, requestId, usageUpdate) {
736
+ this.lastResponseMetadata = {
737
+ rateLimit: parseRateLimitHeaders(headers),
738
+ requestId: requestId ||
739
+ (headers instanceof Headers
740
+ ? headers.get("x-request-id") || undefined
741
+ : headers["x-request-id"]),
742
+ serverTiming: headers instanceof Headers
743
+ ? headers.get("server-timing") || undefined
744
+ : headers["server-timing"],
745
+ };
746
+ // Update usage tracking
747
+ const rateLimit = this.lastResponseMetadata.rateLimit;
748
+ if (this.usageInfo) {
749
+ this.usageInfo.requestCount++;
750
+ this.usageInfo.messagesUsed++;
751
+ this.usageInfo.lastRequestTimestamp = Date.now();
752
+ // Update token usage if provided
753
+ if (usageUpdate) {
754
+ if (usageUpdate.inputTokens !== undefined) {
755
+ this.usageInfo.inputTokensUsed += usageUpdate.inputTokens;
756
+ this.usageInfo.tokensUsed += usageUpdate.inputTokens;
757
+ }
758
+ if (usageUpdate.outputTokens !== undefined) {
759
+ this.usageInfo.outputTokensUsed += usageUpdate.outputTokens;
760
+ this.usageInfo.tokensUsed += usageUpdate.outputTokens;
761
+ }
762
+ }
763
+ // Update remaining quotas from rate limit headers
764
+ if (rateLimit?.requestsRemaining !== undefined) {
765
+ this.usageInfo.messagesRemaining = rateLimit.requestsRemaining;
766
+ }
767
+ if (rateLimit?.tokensRemaining !== undefined) {
768
+ this.usageInfo.tokensRemaining = rateLimit.tokensRemaining;
769
+ }
770
+ // Calculate quota percentages
771
+ if (rateLimit?.requestsLimit && rateLimit.requestsLimit > 0) {
772
+ this.usageInfo.messageQuotaPercent = Math.round(((rateLimit.requestsLimit - (rateLimit.requestsRemaining ?? 0)) /
773
+ rateLimit.requestsLimit) *
774
+ 100);
775
+ }
776
+ if (rateLimit?.tokensLimit && rateLimit.tokensLimit > 0) {
777
+ this.usageInfo.tokenQuotaPercent = Math.round(((rateLimit.tokensLimit - (rateLimit.tokensRemaining ?? 0)) /
778
+ rateLimit.tokensLimit) *
779
+ 100);
780
+ }
781
+ // Check for rate limiting
782
+ if (rateLimit?.retryAfter !== undefined) {
783
+ this.usageInfo.isRateLimited = true;
784
+ this.usageInfo.rateLimitExpiresAt =
785
+ Date.now() + rateLimit.retryAfter * 1000;
786
+ }
787
+ else {
788
+ this.usageInfo.isRateLimited = false;
789
+ this.usageInfo.rateLimitExpiresAt = undefined;
790
+ }
791
+ }
792
+ // Log rate limit warnings if approaching limits
793
+ if (rateLimit?.requestsRemaining !== undefined) {
794
+ if (rateLimit.requestsRemaining <= 5) {
795
+ logger.warn("Approaching Anthropic request rate limit", {
796
+ remaining: rateLimit.requestsRemaining,
797
+ limit: rateLimit.requestsLimit,
798
+ reset: rateLimit.requestsReset,
799
+ });
800
+ }
801
+ }
802
+ if (rateLimit?.tokensRemaining !== undefined) {
803
+ if (rateLimit.tokensLimit &&
804
+ rateLimit.tokensRemaining < rateLimit.tokensLimit * 0.1) {
805
+ logger.warn("Approaching Anthropic token rate limit", {
806
+ remaining: rateLimit.tokensRemaining,
807
+ limit: rateLimit.tokensLimit,
808
+ reset: rateLimit.tokensReset,
809
+ });
810
+ }
811
+ }
812
+ }
40
813
  getProviderName() {
41
814
  return "anthropic";
42
815
  }
@@ -83,7 +856,17 @@ export class AnthropicProvider extends BaseProvider {
83
856
  return new ProviderError(`Anthropic error: ${message}`, this.providerName);
84
857
  }
85
858
  // executeGenerate removed - BaseProvider handles all generation with tools
859
+ /**
860
+ * Override generate to refresh the OAuth token before delegating to
861
+ * BaseProvider so that expired tokens are renewed automatically.
862
+ */
863
+ async generate(optionsOrPrompt, analysisSchema) {
864
+ await this.refreshAuthIfNeeded();
865
+ return super.generate(optionsOrPrompt, analysisSchema);
866
+ }
86
867
  async executeStream(options, _analysisSchema) {
868
+ // Refresh OAuth token if needed before making any API request.
869
+ await this.refreshAuthIfNeeded();
87
870
  this.validateStreamOptions(options);
88
871
  const timeout = this.getTimeout(options);
89
872
  const timeoutController = createTimeoutController(timeout, this.providerName, "stream");
@@ -98,25 +881,78 @@ export class AnthropicProvider extends BaseProvider {
98
881
  // Using protected helper from BaseProvider to eliminate code duplication
99
882
  const messages = await this.buildMessagesForStream(options);
100
883
  const model = await this.getAISDKModelWithMiddleware(options);
101
- const result = await streamText({
102
- model: model,
103
- messages: messages,
104
- temperature: options.temperature,
105
- maxTokens: options.maxTokens, // No default limit - unlimited unless specified
106
- tools,
107
- maxSteps: options.maxSteps || DEFAULT_MAX_STEPS,
108
- toolChoice: shouldUseTools ? "auto" : "none",
109
- abortSignal: composeAbortSignals(options.abortSignal, timeoutController?.controller.signal),
110
- experimental_telemetry: this.telemetryHandler.getTelemetryConfig(options),
111
- onStepFinish: ({ toolCalls, toolResults }) => {
112
- this.handleToolExecutionStorage(toolCalls, toolResults, options, new Date()).catch((error) => {
113
- logger.warn("[AnthropicProvider] Failed to store tool executions", {
114
- provider: this.providerName,
115
- error: error instanceof Error ? error.message : String(error),
116
- });
117
- });
884
+ // Wrap streamText in an OTel span to capture provider-level latency and token usage
885
+ const streamSpan = streamTracer.startSpan("neurolink.provider.streamText", {
886
+ kind: SpanKind.CLIENT,
887
+ attributes: {
888
+ "gen_ai.system": "anthropic",
889
+ "gen_ai.request.model": model.modelId || this.modelName || "unknown",
118
890
  },
119
891
  });
892
+ let result;
893
+ try {
894
+ result = streamText({
895
+ model: model,
896
+ messages: messages,
897
+ temperature: options.temperature,
898
+ maxTokens: options.maxTokens, // No default limit - unlimited unless specified
899
+ maxRetries: 0, // NL11: Disable AI SDK's invisible internal retries; we handle retries with OTel instrumentation
900
+ tools,
901
+ maxSteps: options.maxSteps || DEFAULT_MAX_STEPS,
902
+ toolChoice: shouldUseTools ? "auto" : "none",
903
+ abortSignal: composeAbortSignals(options.abortSignal, timeoutController?.controller.signal),
904
+ experimental_telemetry: this.telemetryHandler.getTelemetryConfig(options),
905
+ onStepFinish: ({ toolCalls, toolResults }) => {
906
+ this.handleToolExecutionStorage(toolCalls, toolResults, options, new Date()).catch((error) => {
907
+ logger.warn("[AnthropicProvider] Failed to store tool executions", {
908
+ provider: this.providerName,
909
+ error: error instanceof Error ? error.message : String(error),
910
+ });
911
+ });
912
+ },
913
+ });
914
+ }
915
+ catch (streamError) {
916
+ streamSpan.end();
917
+ throw streamError;
918
+ }
919
+ // Collect token usage and finish reason asynchronously when the stream completes,
920
+ // then end the span. This avoids blocking the stream consumer.
921
+ result.usage
922
+ .then((usage) => {
923
+ streamSpan.setAttribute("gen_ai.usage.input_tokens", usage.promptTokens || 0);
924
+ streamSpan.setAttribute("gen_ai.usage.output_tokens", usage.completionTokens || 0);
925
+ const cost = calculateCost(this.providerName, this.modelName, {
926
+ input: usage.promptTokens || 0,
927
+ output: usage.completionTokens || 0,
928
+ total: (usage.promptTokens || 0) + (usage.completionTokens || 0),
929
+ });
930
+ if (cost && cost > 0) {
931
+ streamSpan.setAttribute("neurolink.cost", cost);
932
+ }
933
+ })
934
+ .catch(() => {
935
+ // Usage may not be available if the stream is aborted
936
+ });
937
+ result.finishReason
938
+ .then((reason) => {
939
+ streamSpan.setAttribute("gen_ai.response.finish_reason", reason || "unknown");
940
+ })
941
+ .catch(() => {
942
+ // Finish reason may not be available if the stream is aborted
943
+ });
944
+ // End the span when the stream text resolves (stream fully consumed)
945
+ result.text
946
+ .then(() => {
947
+ streamSpan.end();
948
+ })
949
+ .catch((err) => {
950
+ streamSpan.setStatus({
951
+ code: SpanStatusCode.ERROR,
952
+ message: err instanceof Error ? err.message : String(err),
953
+ });
954
+ streamSpan.end();
955
+ });
120
956
  timeoutController?.cleanup();
121
957
  const transformedStream = this.createTextStream(result);
122
958
  // ✅ Note: Vercel AI SDK's streamText() method limitations with tools
@@ -140,6 +976,12 @@ export class AnthropicProvider extends BaseProvider {
140
976
  }
141
977
  async isAvailable() {
142
978
  try {
979
+ // Check OAuth token first
980
+ const oauthToken = getOAuthToken();
981
+ if (oauthToken) {
982
+ return true;
983
+ }
984
+ // Fall back to API key check
143
985
  getAnthropicApiKey();
144
986
  return true;
145
987
  }
@@ -151,5 +993,9 @@ export class AnthropicProvider extends BaseProvider {
151
993
  return this.model;
152
994
  }
153
995
  }
996
+ // Re-export types and utilities for convenience
997
+ export { ModelAccessError, isModelAvailableForTier, getRecommendedModelForTier, getModelCapabilities, } from "../models/anthropicModels.js";
998
+ // Export beta headers constant for external use
999
+ export { ANTHROPIC_BETA_HEADERS };
154
1000
  export default AnthropicProvider;
155
1001
  //# sourceMappingURL=anthropic.js.map