@juspay/neurolink 9.31.2 → 9.32.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 (161) hide show
  1. package/CHANGELOG.md +6 -0
  2. package/dist/auth/AuthProviderFactory.d.ts +71 -0
  3. package/dist/auth/AuthProviderFactory.js +111 -0
  4. package/dist/auth/AuthProviderRegistry.d.ts +33 -0
  5. package/dist/auth/AuthProviderRegistry.js +190 -0
  6. package/dist/auth/RequestContext.d.ts +23 -0
  7. package/dist/auth/RequestContext.js +78 -0
  8. package/dist/auth/authContext.d.ts +198 -0
  9. package/dist/auth/authContext.js +314 -0
  10. package/dist/auth/errors.d.ts +63 -0
  11. package/dist/auth/errors.js +39 -0
  12. package/dist/auth/index.d.ts +20 -8
  13. package/dist/auth/index.js +35 -7
  14. package/dist/auth/middleware/AuthMiddleware.d.ts +181 -0
  15. package/dist/auth/middleware/AuthMiddleware.js +519 -0
  16. package/dist/auth/middleware/rateLimitByUser.d.ts +282 -0
  17. package/dist/auth/middleware/rateLimitByUser.js +554 -0
  18. package/dist/auth/providers/BaseAuthProvider.d.ts +259 -0
  19. package/dist/auth/providers/BaseAuthProvider.js +723 -0
  20. package/dist/auth/providers/CognitoProvider.d.ts +61 -0
  21. package/dist/auth/providers/CognitoProvider.js +304 -0
  22. package/dist/auth/providers/KeycloakProvider.d.ts +61 -0
  23. package/dist/auth/providers/KeycloakProvider.js +393 -0
  24. package/dist/auth/providers/auth0.d.ts +59 -0
  25. package/dist/auth/providers/auth0.js +274 -0
  26. package/dist/auth/providers/betterAuth.d.ts +51 -0
  27. package/dist/auth/providers/betterAuth.js +182 -0
  28. package/dist/auth/providers/clerk.d.ts +65 -0
  29. package/dist/auth/providers/clerk.js +317 -0
  30. package/dist/auth/providers/custom.d.ts +64 -0
  31. package/dist/auth/providers/custom.js +112 -0
  32. package/dist/auth/providers/firebase.d.ts +63 -0
  33. package/dist/auth/providers/firebase.js +226 -0
  34. package/dist/auth/providers/jwt.d.ts +68 -0
  35. package/dist/auth/providers/jwt.js +212 -0
  36. package/dist/auth/providers/oauth2.d.ts +73 -0
  37. package/dist/auth/providers/oauth2.js +303 -0
  38. package/dist/auth/providers/supabase.d.ts +63 -0
  39. package/dist/auth/providers/supabase.js +259 -0
  40. package/dist/auth/providers/workos.d.ts +61 -0
  41. package/dist/auth/providers/workos.js +284 -0
  42. package/dist/auth/serverBridge.d.ts +14 -0
  43. package/dist/auth/serverBridge.js +25 -0
  44. package/dist/auth/sessionManager.d.ts +142 -0
  45. package/dist/auth/sessionManager.js +437 -0
  46. package/dist/cli/commands/authProviders.d.ts +43 -0
  47. package/dist/cli/commands/authProviders.js +399 -0
  48. package/dist/cli/factories/authCommandFactory.d.ts +23 -5
  49. package/dist/cli/factories/authCommandFactory.js +108 -5
  50. package/dist/cli/parser.js +1 -1
  51. package/dist/client/auth/AuthProviderFactory.js +111 -0
  52. package/dist/client/auth/AuthProviderRegistry.js +190 -0
  53. package/dist/client/auth/RequestContext.js +78 -0
  54. package/dist/client/auth/accountPool.js +178 -0
  55. package/dist/client/auth/authContext.js +314 -0
  56. package/dist/client/auth/errors.js +39 -0
  57. package/dist/client/auth/index.js +61 -0
  58. package/dist/client/auth/middleware/AuthMiddleware.js +519 -0
  59. package/dist/client/auth/middleware/rateLimitByUser.js +554 -0
  60. package/dist/client/auth/providers/BaseAuthProvider.js +723 -0
  61. package/dist/client/auth/providers/CognitoProvider.js +304 -0
  62. package/dist/client/auth/providers/KeycloakProvider.js +393 -0
  63. package/dist/client/auth/providers/auth0.js +274 -0
  64. package/dist/client/auth/providers/betterAuth.js +182 -0
  65. package/dist/client/auth/providers/clerk.js +317 -0
  66. package/dist/client/auth/providers/custom.js +112 -0
  67. package/dist/client/auth/providers/firebase.js +226 -0
  68. package/dist/client/auth/providers/jwt.js +212 -0
  69. package/dist/client/auth/providers/oauth2.js +303 -0
  70. package/dist/client/auth/providers/supabase.js +259 -0
  71. package/dist/client/auth/providers/workos.js +284 -0
  72. package/dist/client/auth/serverBridge.js +25 -0
  73. package/dist/client/auth/sessionManager.js +437 -0
  74. package/dist/client/core/infrastructure/baseRegistry.js +5 -1
  75. package/dist/client/index.js +25 -0
  76. package/dist/client/mcp/toolRegistry.js +11 -1
  77. package/dist/client/neurolink.js +218 -0
  78. package/dist/client/rag/ChunkerRegistry.js +2 -2
  79. package/dist/client/rag/metadata/MetadataExtractorRegistry.js +2 -2
  80. package/dist/client/rag/reranker/RerankerRegistry.js +2 -2
  81. package/dist/client/server/routes/agentRoutes.js +20 -2
  82. package/dist/client/types/authTypes.js +2 -1
  83. package/dist/core/infrastructure/baseRegistry.d.ts +3 -1
  84. package/dist/core/infrastructure/baseRegistry.js +5 -1
  85. package/dist/index.d.ts +1 -0
  86. package/dist/index.js +25 -0
  87. package/dist/lib/auth/AuthProviderFactory.d.ts +71 -0
  88. package/dist/lib/auth/AuthProviderFactory.js +112 -0
  89. package/dist/lib/auth/AuthProviderRegistry.d.ts +33 -0
  90. package/dist/lib/auth/AuthProviderRegistry.js +191 -0
  91. package/dist/lib/auth/RequestContext.d.ts +23 -0
  92. package/dist/lib/auth/RequestContext.js +79 -0
  93. package/dist/lib/auth/authContext.d.ts +198 -0
  94. package/dist/lib/auth/authContext.js +315 -0
  95. package/dist/lib/auth/errors.d.ts +63 -0
  96. package/dist/lib/auth/errors.js +40 -0
  97. package/dist/lib/auth/index.d.ts +20 -8
  98. package/dist/lib/auth/index.js +35 -7
  99. package/dist/lib/auth/middleware/AuthMiddleware.d.ts +181 -0
  100. package/dist/lib/auth/middleware/AuthMiddleware.js +520 -0
  101. package/dist/lib/auth/middleware/rateLimitByUser.d.ts +282 -0
  102. package/dist/lib/auth/middleware/rateLimitByUser.js +555 -0
  103. package/dist/lib/auth/providers/BaseAuthProvider.d.ts +259 -0
  104. package/dist/lib/auth/providers/BaseAuthProvider.js +724 -0
  105. package/dist/lib/auth/providers/CognitoProvider.d.ts +61 -0
  106. package/dist/lib/auth/providers/CognitoProvider.js +305 -0
  107. package/dist/lib/auth/providers/KeycloakProvider.d.ts +61 -0
  108. package/dist/lib/auth/providers/KeycloakProvider.js +394 -0
  109. package/dist/lib/auth/providers/auth0.d.ts +59 -0
  110. package/dist/lib/auth/providers/auth0.js +275 -0
  111. package/dist/lib/auth/providers/betterAuth.d.ts +51 -0
  112. package/dist/lib/auth/providers/betterAuth.js +183 -0
  113. package/dist/lib/auth/providers/clerk.d.ts +65 -0
  114. package/dist/lib/auth/providers/clerk.js +318 -0
  115. package/dist/lib/auth/providers/custom.d.ts +64 -0
  116. package/dist/lib/auth/providers/custom.js +113 -0
  117. package/dist/lib/auth/providers/firebase.d.ts +63 -0
  118. package/dist/lib/auth/providers/firebase.js +227 -0
  119. package/dist/lib/auth/providers/jwt.d.ts +68 -0
  120. package/dist/lib/auth/providers/jwt.js +213 -0
  121. package/dist/lib/auth/providers/oauth2.d.ts +73 -0
  122. package/dist/lib/auth/providers/oauth2.js +304 -0
  123. package/dist/lib/auth/providers/supabase.d.ts +63 -0
  124. package/dist/lib/auth/providers/supabase.js +260 -0
  125. package/dist/lib/auth/providers/workos.d.ts +61 -0
  126. package/dist/lib/auth/providers/workos.js +285 -0
  127. package/dist/lib/auth/serverBridge.d.ts +14 -0
  128. package/dist/lib/auth/serverBridge.js +26 -0
  129. package/dist/lib/auth/sessionManager.d.ts +142 -0
  130. package/dist/lib/auth/sessionManager.js +438 -0
  131. package/dist/lib/core/infrastructure/baseRegistry.d.ts +3 -1
  132. package/dist/lib/core/infrastructure/baseRegistry.js +5 -1
  133. package/dist/lib/index.d.ts +1 -0
  134. package/dist/lib/index.js +25 -0
  135. package/dist/lib/mcp/toolRegistry.js +11 -1
  136. package/dist/lib/neurolink.d.ts +42 -1
  137. package/dist/lib/neurolink.js +218 -0
  138. package/dist/lib/rag/ChunkerRegistry.js +2 -2
  139. package/dist/lib/rag/metadata/MetadataExtractorRegistry.js +2 -2
  140. package/dist/lib/rag/reranker/RerankerRegistry.js +2 -2
  141. package/dist/lib/server/routes/agentRoutes.js +20 -2
  142. package/dist/lib/types/authTypes.d.ts +937 -1
  143. package/dist/lib/types/authTypes.js +2 -1
  144. package/dist/lib/types/configTypes.d.ts +46 -0
  145. package/dist/lib/types/generateTypes.d.ts +6 -0
  146. package/dist/lib/types/index.d.ts +1 -0
  147. package/dist/lib/types/streamTypes.d.ts +6 -0
  148. package/dist/mcp/toolRegistry.js +11 -1
  149. package/dist/neurolink.d.ts +42 -1
  150. package/dist/neurolink.js +218 -0
  151. package/dist/rag/ChunkerRegistry.js +2 -2
  152. package/dist/rag/metadata/MetadataExtractorRegistry.js +2 -2
  153. package/dist/rag/reranker/RerankerRegistry.js +2 -2
  154. package/dist/server/routes/agentRoutes.js +20 -2
  155. package/dist/types/authTypes.d.ts +937 -1
  156. package/dist/types/authTypes.js +2 -1
  157. package/dist/types/configTypes.d.ts +46 -0
  158. package/dist/types/generateTypes.d.ts +6 -0
  159. package/dist/types/index.d.ts +1 -0
  160. package/dist/types/streamTypes.d.ts +6 -0
  161. package/package.json +2 -1
@@ -0,0 +1,111 @@
1
+ /**
2
+ * AuthProviderFactory - Static factory for authentication providers
3
+ *
4
+ * Matches the ProviderFactory pattern: all-static class, no BaseFactory
5
+ * extension, no singleton instance. Providers are registered by
6
+ * AuthProviderRegistry using dynamic imports to avoid circular dependencies.
7
+ */
8
+ import { logger } from "../utils/logger.js";
9
+ import { AuthError } from "./errors.js";
10
+ // =============================================================================
11
+ // FACTORY IMPLEMENTATION
12
+ // =============================================================================
13
+ /**
14
+ * AuthProviderFactory - Creates authentication provider instances
15
+ *
16
+ * Pure static factory with no hardcoded imports. All providers are
17
+ * registered dynamically by AuthProviderRegistry to avoid circular
18
+ * dependencies and enable lazy loading.
19
+ *
20
+ * @example
21
+ * ```typescript
22
+ * // Create a provider (after AuthProviderRegistry.registerAllProviders())
23
+ * const provider = await AuthProviderFactory.createProvider("auth0", {
24
+ * type: "auth0",
25
+ * domain: "your-tenant.auth0.com",
26
+ * clientId: "your-client-id",
27
+ * });
28
+ * ```
29
+ */
30
+ export class AuthProviderFactory {
31
+ static providers = new Map();
32
+ static aliasMap = new Map();
33
+ /**
34
+ * Register a provider with the factory
35
+ */
36
+ static registerProvider(type, factory, aliases = [], metadata) {
37
+ AuthProviderFactory.providers.set(type, { factory, aliases, metadata });
38
+ for (const alias of aliases) {
39
+ AuthProviderFactory.aliasMap.set(alias.toLowerCase(), type);
40
+ }
41
+ logger.debug(`Registered auth provider: ${type}`);
42
+ }
43
+ /**
44
+ * Create a provider instance
45
+ */
46
+ static async createProvider(typeOrAlias, config) {
47
+ const resolvedType = AuthProviderFactory.resolveType(typeOrAlias);
48
+ const registration = AuthProviderFactory.providers.get(resolvedType);
49
+ if (!registration) {
50
+ throw AuthError.create("PROVIDER_NOT_FOUND", `Auth provider not found: ${typeOrAlias}. Available: ${AuthProviderFactory.getAvailableProviders().join(", ")}`);
51
+ }
52
+ try {
53
+ return await registration.factory(config);
54
+ }
55
+ catch (error) {
56
+ throw AuthError.create("CREATION_FAILED", `Failed to create auth provider ${typeOrAlias}: ${error instanceof Error ? error.message : String(error)}`, { cause: error instanceof Error ? error : undefined });
57
+ }
58
+ }
59
+ /**
60
+ * Check if a provider is registered
61
+ */
62
+ static hasProvider(typeOrAlias) {
63
+ return (AuthProviderFactory.providers.has(typeOrAlias) ||
64
+ AuthProviderFactory.aliasMap.has(typeOrAlias.toLowerCase()));
65
+ }
66
+ /**
67
+ * Get list of available provider types (excludes aliases)
68
+ */
69
+ static getAvailableProviders() {
70
+ return Array.from(AuthProviderFactory.providers.keys());
71
+ }
72
+ /**
73
+ * Get provider metadata
74
+ */
75
+ static getProviderMetadata(typeOrAlias) {
76
+ const resolvedType = AuthProviderFactory.resolveType(typeOrAlias);
77
+ return AuthProviderFactory.providers.get(resolvedType)?.metadata;
78
+ }
79
+ /**
80
+ * Get all registered providers with their metadata
81
+ */
82
+ static getAllProviderInfo() {
83
+ return Array.from(AuthProviderFactory.providers.entries()).map(([type, reg]) => ({
84
+ type,
85
+ aliases: reg.aliases,
86
+ metadata: reg.metadata,
87
+ }));
88
+ }
89
+ /**
90
+ * Clear all registrations (for testing)
91
+ */
92
+ static clearRegistrations() {
93
+ AuthProviderFactory.providers.clear();
94
+ AuthProviderFactory.aliasMap.clear();
95
+ }
96
+ /**
97
+ * Resolve an alias to its canonical provider type
98
+ */
99
+ static resolveType(typeOrAlias) {
100
+ return (AuthProviderFactory.aliasMap.get(typeOrAlias.toLowerCase()) || typeOrAlias);
101
+ }
102
+ }
103
+ // =============================================================================
104
+ // CONVENIENCE EXPORTS
105
+ // =============================================================================
106
+ /**
107
+ * Create an auth provider using the factory
108
+ */
109
+ export async function createAuthProvider(type, config) {
110
+ return AuthProviderFactory.createProvider(type, config);
111
+ }
@@ -0,0 +1,190 @@
1
+ /**
2
+ * AuthProviderRegistry - Static one-shot registry for authentication providers
3
+ *
4
+ * Matches the ProviderRegistry pattern: static class with a single
5
+ * `registerAllProviders()` entry point that registers all 11 auth
6
+ * providers with AuthProviderFactory using dynamic imports.
7
+ */
8
+ import { logger } from "../utils/logger.js";
9
+ import { AuthProviderFactory } from "./AuthProviderFactory.js";
10
+ /**
11
+ * Narrow `AuthProviderConfig` to a specific provider variant.
12
+ *
13
+ * Each factory lambda calls this to assert the config is the expected
14
+ * variant — safe because the factory is only ever invoked with a config
15
+ * whose `type` matches the registered provider key.
16
+ */
17
+ function narrowConfig(config) {
18
+ return config;
19
+ }
20
+ /**
21
+ * AuthProviderRegistry - registers all auth providers with the factory
22
+ *
23
+ * Call `AuthProviderRegistry.registerAllProviders()` once during
24
+ * application startup. The method is idempotent and concurrency-safe.
25
+ */
26
+ export class AuthProviderRegistry {
27
+ static registered = false;
28
+ static registrationPromise = null;
29
+ /**
30
+ * Register all auth providers with the factory
31
+ */
32
+ static async registerAllProviders() {
33
+ if (AuthProviderRegistry.registered) {
34
+ return;
35
+ }
36
+ if (AuthProviderRegistry.registrationPromise) {
37
+ return AuthProviderRegistry.registrationPromise;
38
+ }
39
+ AuthProviderRegistry.registrationPromise =
40
+ AuthProviderRegistry._doRegister();
41
+ try {
42
+ await AuthProviderRegistry.registrationPromise;
43
+ AuthProviderRegistry.registered = true;
44
+ }
45
+ catch (error) {
46
+ AuthProviderRegistry.registrationPromise = null;
47
+ throw error;
48
+ }
49
+ }
50
+ /**
51
+ * Internal registration implementation
52
+ */
53
+ static async _doRegister() {
54
+ // Auth0 Provider
55
+ AuthProviderFactory.registerProvider("auth0", async (config) => {
56
+ const { Auth0Provider } = await import("./providers/auth0.js");
57
+ return new Auth0Provider(narrowConfig(config));
58
+ }, ["auth0-jwt", "auth0-oauth"], {
59
+ type: "auth0",
60
+ name: "Auth0",
61
+ description: "Auth0 identity platform integration",
62
+ documentation: "https://auth0.com/docs",
63
+ aliases: ["auth0-jwt", "auth0-oauth"],
64
+ });
65
+ // Clerk Provider
66
+ AuthProviderFactory.registerProvider("clerk", async (config) => {
67
+ const { ClerkProvider } = await import("./providers/clerk.js");
68
+ return new ClerkProvider(narrowConfig(config));
69
+ }, ["clerk-jwt"], {
70
+ type: "clerk",
71
+ name: "Clerk",
72
+ description: "Clerk authentication platform integration",
73
+ documentation: "https://clerk.com/docs",
74
+ aliases: ["clerk-jwt"],
75
+ });
76
+ // Firebase Provider
77
+ AuthProviderFactory.registerProvider("firebase", async (config) => {
78
+ const { FirebaseAuthProvider } = await import("./providers/firebase.js");
79
+ return new FirebaseAuthProvider(narrowConfig(config));
80
+ }, ["firebase-auth", "google-firebase"], {
81
+ type: "firebase",
82
+ name: "Firebase",
83
+ description: "Firebase Authentication integration",
84
+ documentation: "https://firebase.google.com/docs/auth",
85
+ aliases: ["firebase-auth", "google-firebase"],
86
+ });
87
+ // Supabase Provider
88
+ AuthProviderFactory.registerProvider("supabase", async (config) => {
89
+ const { SupabaseAuthProvider } = await import("./providers/supabase.js");
90
+ return new SupabaseAuthProvider(narrowConfig(config));
91
+ }, ["supabase-auth"], {
92
+ type: "supabase",
93
+ name: "Supabase",
94
+ description: "Supabase Auth integration",
95
+ documentation: "https://supabase.com/docs/guides/auth",
96
+ aliases: ["supabase-auth"],
97
+ });
98
+ // AWS Cognito Provider
99
+ AuthProviderFactory.registerProvider("cognito", async (config) => {
100
+ const { CognitoProvider } = await import("./providers/CognitoProvider.js");
101
+ return new CognitoProvider(config);
102
+ }, ["aws-cognito", "amazon-cognito"], {
103
+ type: "cognito",
104
+ name: "AWS Cognito",
105
+ description: "Amazon Cognito User Pools integration",
106
+ documentation: "https://docs.aws.amazon.com/cognito",
107
+ aliases: ["aws-cognito", "amazon-cognito"],
108
+ });
109
+ // Keycloak Provider
110
+ AuthProviderFactory.registerProvider("keycloak", async (config) => {
111
+ const { KeycloakProvider } = await import("./providers/KeycloakProvider.js");
112
+ return new KeycloakProvider(config);
113
+ }, ["keycloak-oidc"], {
114
+ type: "keycloak",
115
+ name: "Keycloak",
116
+ description: "Keycloak OpenID Connect integration",
117
+ documentation: "https://www.keycloak.org/documentation",
118
+ aliases: ["keycloak-oidc"],
119
+ });
120
+ // Better Auth Provider
121
+ AuthProviderFactory.registerProvider("better-auth", async (config) => {
122
+ const { BetterAuthProvider } = await import("./providers/betterAuth.js");
123
+ return new BetterAuthProvider(narrowConfig(config));
124
+ }, ["betterauth", "better_auth"], {
125
+ type: "better-auth",
126
+ name: "Better Auth",
127
+ description: "Self-hosted open-source authentication solution",
128
+ documentation: "https://better-auth.com/docs",
129
+ aliases: ["betterauth", "better_auth"],
130
+ });
131
+ // WorkOS Provider
132
+ AuthProviderFactory.registerProvider("workos", async (config) => {
133
+ const { WorkOSProvider } = await import("./providers/workos.js");
134
+ return new WorkOSProvider(narrowConfig(config));
135
+ }, ["workos-sso", "work-os"], {
136
+ type: "workos",
137
+ name: "WorkOS",
138
+ description: "Enterprise SSO and user management",
139
+ documentation: "https://workos.com/docs",
140
+ aliases: ["workos-sso", "work-os"],
141
+ });
142
+ // OAuth2 Provider
143
+ AuthProviderFactory.registerProvider("oauth2", async (config) => {
144
+ const { OAuth2Provider } = await import("./providers/oauth2.js");
145
+ return new OAuth2Provider(narrowConfig(config));
146
+ }, ["oauth", "oidc", "openid-connect"], {
147
+ type: "oauth2",
148
+ name: "OAuth2",
149
+ description: "Generic OAuth2/OIDC provider with JWKS and userinfo support",
150
+ documentation: "https://oauth.net/2/",
151
+ aliases: ["oauth", "oidc", "openid-connect"],
152
+ });
153
+ // JWT Provider
154
+ AuthProviderFactory.registerProvider("jwt", async (config) => {
155
+ const { JWTProvider } = await import("./providers/jwt.js");
156
+ return new JWTProvider(narrowConfig(config));
157
+ }, ["jwt-auth", "jwt-token"], {
158
+ type: "jwt",
159
+ name: "JWT",
160
+ description: "Generic JWT token validation with symmetric/asymmetric keys",
161
+ documentation: "https://jwt.io/",
162
+ aliases: ["jwt-auth", "jwt-token"],
163
+ });
164
+ // Custom Provider
165
+ AuthProviderFactory.registerProvider("custom", async (config) => {
166
+ const { CustomAuthProvider } = await import("./providers/custom.js");
167
+ return new CustomAuthProvider(narrowConfig(config));
168
+ }, ["custom-auth"], {
169
+ type: "custom",
170
+ name: "Custom",
171
+ description: "Custom authentication with user-provided validation logic",
172
+ aliases: ["custom-auth"],
173
+ });
174
+ logger.debug("All auth providers registered");
175
+ }
176
+ /**
177
+ * Check if providers are registered
178
+ */
179
+ static isRegistered() {
180
+ return AuthProviderRegistry.registered;
181
+ }
182
+ /**
183
+ * Clear registrations (for testing)
184
+ */
185
+ static clearRegistrations() {
186
+ AuthProviderRegistry.registered = false;
187
+ AuthProviderRegistry.registrationPromise = null;
188
+ AuthProviderFactory.clearRegistrations();
189
+ }
190
+ }
@@ -0,0 +1,78 @@
1
+ // src/lib/auth/RequestContext.ts
2
+ /**
3
+ * Type-safe Map wrapper for request-scoped context.
4
+ * Flows from auth middleware through generate/stream/tools/memory.
5
+ * Reserved keys (prefixed neurolink__) cannot be overridden by client code.
6
+ */
7
+ export const NEUROLINK_RESOURCE_ID_KEY = "neurolink__resourceId";
8
+ export const NEUROLINK_THREAD_ID_KEY = "neurolink__threadId";
9
+ const RESERVED_PREFIX = "neurolink__";
10
+ export class RequestContext {
11
+ registry = new Map();
12
+ constructor(initial) {
13
+ if (Array.isArray(initial)) {
14
+ for (const [key, value] of initial) {
15
+ this.registry.set(key, value);
16
+ }
17
+ }
18
+ else if (initial) {
19
+ for (const [key, value] of Object.entries(initial)) {
20
+ this.registry.set(key, value);
21
+ }
22
+ }
23
+ }
24
+ set(key, value) {
25
+ this.registry.set(key, value);
26
+ }
27
+ get(key) {
28
+ return this.registry.get(key);
29
+ }
30
+ has(key) {
31
+ return this.registry.has(key);
32
+ }
33
+ delete(key) {
34
+ return this.registry.delete(key);
35
+ }
36
+ get size() {
37
+ return this.registry.size;
38
+ }
39
+ /**
40
+ * Merge client-provided values, but SKIP reserved keys that are already set.
41
+ * This prevents clients from overriding auth middleware values.
42
+ */
43
+ mergeClientContext(clientContext) {
44
+ for (const [key, value] of Object.entries(clientContext)) {
45
+ if (key.startsWith(RESERVED_PREFIX) && this.registry.has(key)) {
46
+ continue; // Server-set reserved keys cannot be overridden
47
+ }
48
+ if (!this.registry.has(key)) {
49
+ this.registry.set(key, value);
50
+ }
51
+ }
52
+ }
53
+ toJSON() {
54
+ const result = {};
55
+ for (const [key, value] of this.registry.entries()) {
56
+ if (this.isSerializable(value)) {
57
+ result[key] = value;
58
+ }
59
+ }
60
+ return result;
61
+ }
62
+ isSerializable(value) {
63
+ if (value === null || value === undefined) {
64
+ return true;
65
+ }
66
+ const type = typeof value;
67
+ if (type === "function" || type === "symbol") {
68
+ return false;
69
+ }
70
+ try {
71
+ JSON.stringify(value);
72
+ return true;
73
+ }
74
+ catch {
75
+ return false;
76
+ }
77
+ }
78
+ }
@@ -0,0 +1,178 @@
1
+ /**
2
+ * AccountPool — manages a pool of proxy accounts with round-robin / fill-first
3
+ * selection and exponential-backoff cooldowns for quota-exceeded accounts.
4
+ *
5
+ * @module auth/accountPool
6
+ */
7
+ import { tokenStore } from "./tokenStore.js";
8
+ // =============================================================================
9
+ // CONSTANTS
10
+ // =============================================================================
11
+ const DEFAULT_COOLDOWN_MS = 300_000;
12
+ const DEFAULT_MAX_COOLDOWN_MS = 1_800_000;
13
+ // =============================================================================
14
+ // IMPLEMENTATION
15
+ // =============================================================================
16
+ export class AccountPool {
17
+ accounts = new Map();
18
+ cursor = 0;
19
+ config;
20
+ constructor(config = {}) {
21
+ this.config = {
22
+ strategy: config.strategy ?? "round-robin",
23
+ defaultCooldownMs: config.defaultCooldownMs ?? DEFAULT_COOLDOWN_MS,
24
+ maxCooldownMs: config.maxCooldownMs ?? DEFAULT_MAX_COOLDOWN_MS,
25
+ maxRetryAccounts: config.maxRetryAccounts ?? 0,
26
+ };
27
+ }
28
+ /** Add (or replace) an account in the pool. */
29
+ addAccount(account) {
30
+ // Clear any stale token refresher for a previously-registered account
31
+ // with the same id — wireTokenRefresh() captures the old account object
32
+ // in its closure, so leaving it behind would refresh the wrong tokens.
33
+ tokenStore.clearTokenRefresher(account.id);
34
+ this.accounts.set(account.id, { ...account });
35
+ }
36
+ /** Remove an account from the pool by id. */
37
+ removeAccount(id) {
38
+ tokenStore.clearTokenRefresher(id);
39
+ this.accounts.delete(id);
40
+ }
41
+ /**
42
+ * Return the next healthy account according to the configured strategy.
43
+ * Returns `null` when no healthy accounts are available.
44
+ */
45
+ getNextAccount() {
46
+ this._expireCooldowns();
47
+ const healthy = this._getHealthyAccounts();
48
+ if (healthy.length === 0) {
49
+ return null;
50
+ }
51
+ if (this.config.strategy === "fill-first") {
52
+ const account = healthy[0];
53
+ account.requestCount++;
54
+ account.lastUsed = Date.now();
55
+ return account;
56
+ }
57
+ // round-robin
58
+ const index = this.cursor % healthy.length;
59
+ this.cursor++;
60
+ const account = healthy[index];
61
+ account.requestCount++;
62
+ account.lastUsed = Date.now();
63
+ return account;
64
+ }
65
+ /**
66
+ * Mark an account as quota-exceeded. The account enters a cooldown state
67
+ * with exponential backoff capped at `maxCooldownMs`.
68
+ *
69
+ * @deprecated Proxy routes now use `RuntimeAccountState` in `claudeProxyRoutes.ts`
70
+ * for runtime account state management. This method is retained for public API compatibility.
71
+ *
72
+ * @param id Account id
73
+ * @param retryAfterMs Optional explicit retry-after duration (from server header)
74
+ */
75
+ markQuotaExceeded(id, retryAfterMs) {
76
+ const account = this.accounts.get(id);
77
+ if (!account) {
78
+ return;
79
+ }
80
+ account.consecutiveFailures++;
81
+ const backoff = Math.min(this.config.defaultCooldownMs *
82
+ Math.pow(2, account.consecutiveFailures - 1), this.config.maxCooldownMs);
83
+ account.cooldownUntil = Date.now() + (retryAfterMs ?? backoff);
84
+ account.status = "cooling";
85
+ }
86
+ /**
87
+ * Manually mark an account as available (healthy), clearing its cooldown.
88
+ *
89
+ * @deprecated Proxy routes now use `RuntimeAccountState` in `claudeProxyRoutes.ts`
90
+ * for runtime account state management. This method is retained for public API compatibility.
91
+ */
92
+ markAvailable(id) {
93
+ const account = this.accounts.get(id);
94
+ if (!account) {
95
+ return;
96
+ }
97
+ account.status = "healthy";
98
+ account.cooldownUntil = undefined;
99
+ }
100
+ /**
101
+ * Mark an account as having completed a request successfully.
102
+ *
103
+ * @deprecated Proxy routes now use `RuntimeAccountState` in `claudeProxyRoutes.ts`
104
+ * for runtime account state management. This method is retained for public API compatibility.
105
+ */
106
+ markSuccess(id) {
107
+ const account = this.accounts.get(id);
108
+ if (!account) {
109
+ return;
110
+ }
111
+ account.status = "healthy";
112
+ account.cooldownUntil = undefined;
113
+ account.consecutiveFailures = 0;
114
+ }
115
+ /** Return the number of currently healthy (not cooling/disabled) accounts. */
116
+ getHealthyCount() {
117
+ this._expireCooldowns();
118
+ return this._getHealthyAccounts().length;
119
+ }
120
+ /** Return a snapshot of all accounts in the pool. */
121
+ getAllAccounts() {
122
+ return Array.from(this.accounts.values());
123
+ }
124
+ /** Return the configured selection strategy. */
125
+ getStrategy() {
126
+ return this.config.strategy;
127
+ }
128
+ /**
129
+ * Wire automatic token refresh for an OAuth account.
130
+ * Call after addAccount() for OAuth accounts that have a refresh token.
131
+ *
132
+ * @param accountId The account id to wire refresh for
133
+ * @param refreshFn Function that takes a refresh token and returns new token data
134
+ */
135
+ wireTokenRefresh(accountId, refreshFn) {
136
+ const account = this.accounts.get(accountId);
137
+ if (!account || account.type !== "oauth" || !account.tokens?.refreshToken) {
138
+ return;
139
+ }
140
+ tokenStore.setTokenRefresher(accountId, async (refreshToken) => {
141
+ const result = await refreshFn(refreshToken);
142
+ const newTokens = {
143
+ accessToken: result.accessToken,
144
+ refreshToken: result.refreshToken ?? refreshToken,
145
+ expiresAt: result.expiresAt instanceof Date
146
+ ? result.expiresAt.getTime()
147
+ : result.expiresAt,
148
+ tokenType: result.tokenType ?? "Bearer",
149
+ };
150
+ // Update in-memory account
151
+ if (account.tokens) {
152
+ account.tokens.accessToken = newTokens.accessToken;
153
+ account.tokens.expiresAt = newTokens.expiresAt;
154
+ if (newTokens.refreshToken) {
155
+ account.tokens.refreshToken = newTokens.refreshToken;
156
+ }
157
+ }
158
+ return newTokens;
159
+ });
160
+ }
161
+ // ---------------------------------------------------------------------------
162
+ // Private helpers
163
+ // ---------------------------------------------------------------------------
164
+ _getHealthyAccounts() {
165
+ return Array.from(this.accounts.values()).filter((a) => a.status === "healthy");
166
+ }
167
+ _expireCooldowns() {
168
+ const now = Date.now();
169
+ for (const account of this.accounts.values()) {
170
+ if (account.status === "cooling" &&
171
+ account.cooldownUntil &&
172
+ now >= account.cooldownUntil) {
173
+ account.status = "healthy";
174
+ account.cooldownUntil = undefined;
175
+ }
176
+ }
177
+ }
178
+ }