@juspay/neurolink 9.31.1 → 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 (162) hide show
  1. package/CHANGELOG.md +12 -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/commands/proxy.js +4 -2
  49. package/dist/cli/factories/authCommandFactory.d.ts +23 -5
  50. package/dist/cli/factories/authCommandFactory.js +108 -5
  51. package/dist/cli/parser.js +1 -1
  52. package/dist/client/auth/AuthProviderFactory.js +111 -0
  53. package/dist/client/auth/AuthProviderRegistry.js +190 -0
  54. package/dist/client/auth/RequestContext.js +78 -0
  55. package/dist/client/auth/accountPool.js +178 -0
  56. package/dist/client/auth/authContext.js +314 -0
  57. package/dist/client/auth/errors.js +39 -0
  58. package/dist/client/auth/index.js +61 -0
  59. package/dist/client/auth/middleware/AuthMiddleware.js +519 -0
  60. package/dist/client/auth/middleware/rateLimitByUser.js +554 -0
  61. package/dist/client/auth/providers/BaseAuthProvider.js +723 -0
  62. package/dist/client/auth/providers/CognitoProvider.js +304 -0
  63. package/dist/client/auth/providers/KeycloakProvider.js +393 -0
  64. package/dist/client/auth/providers/auth0.js +274 -0
  65. package/dist/client/auth/providers/betterAuth.js +182 -0
  66. package/dist/client/auth/providers/clerk.js +317 -0
  67. package/dist/client/auth/providers/custom.js +112 -0
  68. package/dist/client/auth/providers/firebase.js +226 -0
  69. package/dist/client/auth/providers/jwt.js +212 -0
  70. package/dist/client/auth/providers/oauth2.js +303 -0
  71. package/dist/client/auth/providers/supabase.js +259 -0
  72. package/dist/client/auth/providers/workos.js +284 -0
  73. package/dist/client/auth/serverBridge.js +25 -0
  74. package/dist/client/auth/sessionManager.js +437 -0
  75. package/dist/client/core/infrastructure/baseRegistry.js +5 -1
  76. package/dist/client/index.js +25 -0
  77. package/dist/client/mcp/toolRegistry.js +11 -1
  78. package/dist/client/neurolink.js +218 -0
  79. package/dist/client/rag/ChunkerRegistry.js +2 -2
  80. package/dist/client/rag/metadata/MetadataExtractorRegistry.js +2 -2
  81. package/dist/client/rag/reranker/RerankerRegistry.js +2 -2
  82. package/dist/client/server/routes/agentRoutes.js +20 -2
  83. package/dist/client/types/authTypes.js +2 -1
  84. package/dist/core/infrastructure/baseRegistry.d.ts +3 -1
  85. package/dist/core/infrastructure/baseRegistry.js +5 -1
  86. package/dist/index.d.ts +1 -0
  87. package/dist/index.js +25 -0
  88. package/dist/lib/auth/AuthProviderFactory.d.ts +71 -0
  89. package/dist/lib/auth/AuthProviderFactory.js +112 -0
  90. package/dist/lib/auth/AuthProviderRegistry.d.ts +33 -0
  91. package/dist/lib/auth/AuthProviderRegistry.js +191 -0
  92. package/dist/lib/auth/RequestContext.d.ts +23 -0
  93. package/dist/lib/auth/RequestContext.js +79 -0
  94. package/dist/lib/auth/authContext.d.ts +198 -0
  95. package/dist/lib/auth/authContext.js +315 -0
  96. package/dist/lib/auth/errors.d.ts +63 -0
  97. package/dist/lib/auth/errors.js +40 -0
  98. package/dist/lib/auth/index.d.ts +20 -8
  99. package/dist/lib/auth/index.js +35 -7
  100. package/dist/lib/auth/middleware/AuthMiddleware.d.ts +181 -0
  101. package/dist/lib/auth/middleware/AuthMiddleware.js +520 -0
  102. package/dist/lib/auth/middleware/rateLimitByUser.d.ts +282 -0
  103. package/dist/lib/auth/middleware/rateLimitByUser.js +555 -0
  104. package/dist/lib/auth/providers/BaseAuthProvider.d.ts +259 -0
  105. package/dist/lib/auth/providers/BaseAuthProvider.js +724 -0
  106. package/dist/lib/auth/providers/CognitoProvider.d.ts +61 -0
  107. package/dist/lib/auth/providers/CognitoProvider.js +305 -0
  108. package/dist/lib/auth/providers/KeycloakProvider.d.ts +61 -0
  109. package/dist/lib/auth/providers/KeycloakProvider.js +394 -0
  110. package/dist/lib/auth/providers/auth0.d.ts +59 -0
  111. package/dist/lib/auth/providers/auth0.js +275 -0
  112. package/dist/lib/auth/providers/betterAuth.d.ts +51 -0
  113. package/dist/lib/auth/providers/betterAuth.js +183 -0
  114. package/dist/lib/auth/providers/clerk.d.ts +65 -0
  115. package/dist/lib/auth/providers/clerk.js +318 -0
  116. package/dist/lib/auth/providers/custom.d.ts +64 -0
  117. package/dist/lib/auth/providers/custom.js +113 -0
  118. package/dist/lib/auth/providers/firebase.d.ts +63 -0
  119. package/dist/lib/auth/providers/firebase.js +227 -0
  120. package/dist/lib/auth/providers/jwt.d.ts +68 -0
  121. package/dist/lib/auth/providers/jwt.js +213 -0
  122. package/dist/lib/auth/providers/oauth2.d.ts +73 -0
  123. package/dist/lib/auth/providers/oauth2.js +304 -0
  124. package/dist/lib/auth/providers/supabase.d.ts +63 -0
  125. package/dist/lib/auth/providers/supabase.js +260 -0
  126. package/dist/lib/auth/providers/workos.d.ts +61 -0
  127. package/dist/lib/auth/providers/workos.js +285 -0
  128. package/dist/lib/auth/serverBridge.d.ts +14 -0
  129. package/dist/lib/auth/serverBridge.js +26 -0
  130. package/dist/lib/auth/sessionManager.d.ts +142 -0
  131. package/dist/lib/auth/sessionManager.js +438 -0
  132. package/dist/lib/core/infrastructure/baseRegistry.d.ts +3 -1
  133. package/dist/lib/core/infrastructure/baseRegistry.js +5 -1
  134. package/dist/lib/index.d.ts +1 -0
  135. package/dist/lib/index.js +25 -0
  136. package/dist/lib/mcp/toolRegistry.js +11 -1
  137. package/dist/lib/neurolink.d.ts +42 -1
  138. package/dist/lib/neurolink.js +218 -0
  139. package/dist/lib/rag/ChunkerRegistry.js +2 -2
  140. package/dist/lib/rag/metadata/MetadataExtractorRegistry.js +2 -2
  141. package/dist/lib/rag/reranker/RerankerRegistry.js +2 -2
  142. package/dist/lib/server/routes/agentRoutes.js +20 -2
  143. package/dist/lib/types/authTypes.d.ts +937 -1
  144. package/dist/lib/types/authTypes.js +2 -1
  145. package/dist/lib/types/configTypes.d.ts +46 -0
  146. package/dist/lib/types/generateTypes.d.ts +6 -0
  147. package/dist/lib/types/index.d.ts +1 -0
  148. package/dist/lib/types/streamTypes.d.ts +6 -0
  149. package/dist/mcp/toolRegistry.js +11 -1
  150. package/dist/neurolink.d.ts +42 -1
  151. package/dist/neurolink.js +218 -0
  152. package/dist/rag/ChunkerRegistry.js +2 -2
  153. package/dist/rag/metadata/MetadataExtractorRegistry.js +2 -2
  154. package/dist/rag/reranker/RerankerRegistry.js +2 -2
  155. package/dist/server/routes/agentRoutes.js +20 -2
  156. package/dist/types/authTypes.d.ts +937 -1
  157. package/dist/types/authTypes.js +2 -1
  158. package/dist/types/configTypes.d.ts +46 -0
  159. package/dist/types/generateTypes.d.ts +6 -0
  160. package/dist/types/index.d.ts +1 -0
  161. package/dist/types/streamTypes.d.ts +6 -0
  162. package/package.json +2 -1
@@ -246,6 +246,10 @@ export class NeuroLink {
246
246
  conversationMemoryConfig;
247
247
  // Add orchestration property
248
248
  enableOrchestration;
249
+ // Authentication provider for secure access control
250
+ authProvider;
251
+ pendingAuthConfig;
252
+ authInitPromise;
249
253
  // HITL (Human-in-the-Loop) support
250
254
  hitlManager;
251
255
  // Accumulated cost in USD across all generate() calls on this instance
@@ -451,6 +455,10 @@ export class NeuroLink {
451
455
  this.initializeLangfuse(constructorId, constructorStartTime, constructorHrTimeStart);
452
456
  this.initializeMetricsListeners();
453
457
  this.logConstructorComplete(constructorId, constructorStartTime, constructorHrTimeStart);
458
+ // Store auth config for lazy initialization
459
+ if (config?.auth) {
460
+ this.pendingAuthConfig = config.auth;
461
+ }
454
462
  }
455
463
  /**
456
464
  * Initialize provider registry with security settings
@@ -2225,6 +2233,61 @@ Current user's request: ${currentInput}`;
2225
2233
  },
2226
2234
  };
2227
2235
  }
2236
+ // Handle per-call auth token validation
2237
+ if (options.auth?.token) {
2238
+ const { AuthError } = await import("./auth/errors.js");
2239
+ await this.ensureAuthProvider();
2240
+ if (!this.authProvider) {
2241
+ throw AuthError.create("PROVIDER_ERROR", "No auth provider configured. Set auth in constructor or via setAuthProvider() before using auth: { token }.");
2242
+ }
2243
+ let authResult;
2244
+ try {
2245
+ authResult = await withTimeout(this.authProvider.authenticateToken(options.auth.token), 5000, AuthError.create("PROVIDER_ERROR", "Auth token validation timed out after 5000ms"));
2246
+ }
2247
+ catch (err) {
2248
+ // Rethrow auth errors as-is; wrap anything else
2249
+ if (err instanceof Error &&
2250
+ "feature" in err &&
2251
+ err.feature === "Auth") {
2252
+ throw err;
2253
+ }
2254
+ throw AuthError.create("PROVIDER_ERROR", `Auth token validation failed: ${err instanceof Error ? err.message : String(err)}`);
2255
+ }
2256
+ if (!authResult.valid) {
2257
+ throw AuthError.create("INVALID_TOKEN", authResult.error || "Token validation failed");
2258
+ }
2259
+ // Fail closed: token valid but no user identity is a provider bug
2260
+ if (!authResult.user) {
2261
+ throw AuthError.create("INVALID_TOKEN", "Token validated but no user identity returned");
2262
+ }
2263
+ if (!authResult.user.id) {
2264
+ throw AuthError.create("INVALID_TOKEN", "Token validated but user identity missing required 'id' field");
2265
+ }
2266
+ // Merge validated user into context
2267
+ options.context = {
2268
+ ...(options.context || {}),
2269
+ userId: authResult.user.id,
2270
+ userEmail: authResult.user.email,
2271
+ userRoles: authResult.user.roles,
2272
+ };
2273
+ }
2274
+ // Handle pre-validated requestContext
2275
+ if (options.requestContext) {
2276
+ // When auth token was validated, token-derived identity fields
2277
+ // MUST take precedence over requestContext to prevent privilege escalation.
2278
+ const tokenDerivedFields = options.auth?.token && this.authProvider
2279
+ ? {
2280
+ userId: options.context?.userId,
2281
+ userEmail: options.context?.userEmail,
2282
+ userRoles: options.context?.userRoles,
2283
+ }
2284
+ : {};
2285
+ options.context = {
2286
+ ...(options.context || {}),
2287
+ ...options.requestContext,
2288
+ ...tokenDerivedFields,
2289
+ };
2290
+ }
2228
2291
  // Check if workflow is requested
2229
2292
  if (options.workflow || options.workflowConfig) {
2230
2293
  return await this.generateWithWorkflow(options);
@@ -3997,6 +4060,61 @@ Current user's request: ${currentInput}`;
3997
4060
  },
3998
4061
  });
3999
4062
  }
4063
+ // Handle per-call auth token validation
4064
+ if (options.auth?.token) {
4065
+ const { AuthError } = await import("./auth/errors.js");
4066
+ await this.ensureAuthProvider();
4067
+ if (!this.authProvider) {
4068
+ throw AuthError.create("PROVIDER_ERROR", "No auth provider configured. Set auth in constructor or via setAuthProvider() before using auth: { token }.");
4069
+ }
4070
+ let authResult;
4071
+ try {
4072
+ authResult = await withTimeout(this.authProvider.authenticateToken(options.auth.token), 5000, AuthError.create("PROVIDER_ERROR", "Auth token validation timed out after 5000ms"));
4073
+ }
4074
+ catch (err) {
4075
+ // Rethrow auth errors as-is; wrap anything else
4076
+ if (err instanceof Error &&
4077
+ "feature" in err &&
4078
+ err.feature === "Auth") {
4079
+ throw err;
4080
+ }
4081
+ throw AuthError.create("PROVIDER_ERROR", `Auth token validation failed: ${err instanceof Error ? err.message : String(err)}`);
4082
+ }
4083
+ if (!authResult.valid) {
4084
+ throw AuthError.create("INVALID_TOKEN", authResult.error || "Token validation failed");
4085
+ }
4086
+ // Fail closed: token valid but no user identity is a provider bug
4087
+ if (!authResult.user) {
4088
+ throw AuthError.create("INVALID_TOKEN", "Token validated but no user identity returned");
4089
+ }
4090
+ if (!authResult.user.id) {
4091
+ throw AuthError.create("INVALID_TOKEN", "Token validated but user identity missing required 'id' field");
4092
+ }
4093
+ // Merge validated user into context
4094
+ options.context = {
4095
+ ...(options.context || {}),
4096
+ userId: authResult.user.id,
4097
+ userEmail: authResult.user.email,
4098
+ userRoles: authResult.user.roles,
4099
+ };
4100
+ }
4101
+ // Handle pre-validated requestContext
4102
+ if (options.requestContext) {
4103
+ // When auth token was validated, token-derived identity fields
4104
+ // MUST take precedence over requestContext to prevent privilege escalation.
4105
+ const tokenDerivedFields = options.auth?.token && this.authProvider
4106
+ ? {
4107
+ userId: options.context?.userId,
4108
+ userEmail: options.context?.userEmail,
4109
+ userRoles: options.context?.userRoles,
4110
+ }
4111
+ : {};
4112
+ options.context = {
4113
+ ...(options.context || {}),
4114
+ ...options.requestContext,
4115
+ ...tokenDerivedFields,
4116
+ };
4117
+ }
4000
4118
  this.emitStreamStartEvents(options, startTime);
4001
4119
  // Auto-inject lifecycle middleware when callbacks are provided
4002
4120
  // (must happen before workflow early return so that path gets middleware too)
@@ -8001,6 +8119,106 @@ Current user's request: ${currentInput}`;
8001
8119
  });
8002
8120
  return budgetResult.shouldCompact;
8003
8121
  }
8122
+ // ============================================================================
8123
+ // Authentication Methods
8124
+ // ============================================================================
8125
+ /**
8126
+ * Set the authentication provider for the NeuroLink instance
8127
+ *
8128
+ * @param config - Auth provider or configuration to create one
8129
+ */
8130
+ async setAuthProvider(config) {
8131
+ // Clear any pending lazy-init promise so it does not race with this call.
8132
+ this.authInitPromise = undefined;
8133
+ // Duck-type check: direct MastraAuthProvider instance
8134
+ if ("authenticateToken" in config &&
8135
+ typeof config.authenticateToken === "function") {
8136
+ this.authProvider = config;
8137
+ logger.info(`Auth provider set: ${this.authProvider.type}`);
8138
+ }
8139
+ else if ("provider" in config) {
8140
+ this.authProvider = config.provider;
8141
+ logger.info(`Auth provider set: ${this.authProvider.type}`);
8142
+ }
8143
+ else {
8144
+ const typedConfig = config;
8145
+ const { AuthProviderFactory } = await import("./auth/AuthProviderFactory.js");
8146
+ this.authProvider = await AuthProviderFactory.createProvider(typedConfig.type, typedConfig.config);
8147
+ logger.info(`Auth provider created and set: ${typedConfig.type}`);
8148
+ }
8149
+ if (this.authProvider) {
8150
+ this.emitter.emit("auth:provider:set", {
8151
+ type: this.authProvider.type,
8152
+ timestamp: Date.now(),
8153
+ });
8154
+ }
8155
+ }
8156
+ /**
8157
+ * Get the currently configured authentication provider
8158
+ */
8159
+ getAuthProvider() {
8160
+ return this.authProvider;
8161
+ }
8162
+ /**
8163
+ * Lazily initialize the auth provider from pendingAuthConfig.
8164
+ * Called on first use (generate/stream with auth token) to avoid
8165
+ * async work in the synchronous constructor.
8166
+ */
8167
+ async ensureAuthProvider() {
8168
+ if (this.authProvider || !this.pendingAuthConfig) {
8169
+ return;
8170
+ }
8171
+ this.authInitPromise ??= (async () => {
8172
+ try {
8173
+ await this.setAuthProvider(this.pendingAuthConfig);
8174
+ this.pendingAuthConfig = undefined;
8175
+ }
8176
+ catch (err) {
8177
+ this.authInitPromise = undefined;
8178
+ throw err;
8179
+ }
8180
+ })();
8181
+ await this.authInitPromise;
8182
+ }
8183
+ /**
8184
+ * Set the current authentication context for request handling.
8185
+ *
8186
+ * Delegates to the global AuthContextHolder so that auth state is NOT
8187
+ * stored as an instance field (which would leak between concurrent requests
8188
+ * sharing the same NeuroLink singleton). Prefer `runWithAuthContext()` from
8189
+ * `authContext.ts` for proper request-scoped context via AsyncLocalStorage.
8190
+ *
8191
+ * @param context - The authenticated user context
8192
+ */
8193
+ async setAuthContext(context) {
8194
+ const { globalAuthContext } = await import("./auth/authContext.js");
8195
+ globalAuthContext.set(context);
8196
+ logger.debug("Auth context set", {
8197
+ userId: context.user.id,
8198
+ provider: context.provider,
8199
+ sessionId: context.session?.id,
8200
+ });
8201
+ }
8202
+ /**
8203
+ * Get the current authentication context.
8204
+ *
8205
+ * Checks AsyncLocalStorage first, then falls back to the global holder.
8206
+ */
8207
+ async getAuthContext() {
8208
+ const { getAuthContext: getCtx } = await import("./auth/authContext.js");
8209
+ return getCtx();
8210
+ }
8211
+ /**
8212
+ * Clear the current authentication context
8213
+ */
8214
+ async clearAuthContext() {
8215
+ const { globalAuthContext } = await import("./auth/authContext.js");
8216
+ const userId = globalAuthContext.get()?.user.id;
8217
+ globalAuthContext.clear();
8218
+ if (userId) {
8219
+ logger.debug(`Auth context cleared for user: ${userId}`);
8220
+ }
8221
+ }
8004
8222
  /**
8005
8223
  * Get the external server manager instance
8006
8224
  * Used internally by server adapters for external MCP server management
@@ -280,8 +280,8 @@ export class ChunkerRegistry extends BaseRegistry {
280
280
  * Register a chunker with aliases
281
281
  */
282
282
  registerChunker(strategy, factory, metadata) {
283
- this.register(strategy, factory, metadata);
284
- // Register aliases
283
+ this.register(strategy, factory, metadata.aliases ?? [], { metadata });
284
+ // Register aliases in local alias map for strategy resolution
285
285
  if (metadata.aliases) {
286
286
  for (const alias of metadata.aliases) {
287
287
  this.aliasMap.set(alias.toLowerCase(), strategy);
@@ -212,8 +212,8 @@ export class MetadataExtractorRegistry extends BaseRegistry {
212
212
  * Register an extractor with aliases
213
213
  */
214
214
  registerExtractor(type, factory, metadata) {
215
- this.register(type, factory, metadata);
216
- // Register aliases
215
+ this.register(type, factory, metadata.aliases, { metadata });
216
+ // Register aliases in local alias map for type resolution
217
217
  for (const alias of metadata.aliases) {
218
218
  this.aliasMap.set(alias.toLowerCase(), type);
219
219
  logger.debug(`[MetadataExtractorRegistry] Registered alias '${alias}' -> '${type}'`);
@@ -237,8 +237,8 @@ export class RerankerRegistry extends BaseRegistry {
237
237
  * Register a reranker with aliases
238
238
  */
239
239
  registerReranker(type, factory, metadata) {
240
- this.register(type, factory, metadata);
241
- // Register aliases
240
+ this.register(type, factory, metadata.aliases, { metadata });
241
+ // Register aliases in local alias map for type resolution
242
242
  for (const alias of metadata.aliases) {
243
243
  this.aliasMap.set(alias.toLowerCase(), type);
244
244
  logger.debug(`[RerankerRegistry] Registered alias '${alias}' -> '${type}'`);
@@ -36,8 +36,15 @@ export function createAgentRoutes(basePath = "/api") {
36
36
  // Note: tools should be passed as Record<string, Tool> in generate options
37
37
  // If request.tools is an array of tool names, we skip them
38
38
  context: {
39
- sessionId: request.sessionId,
40
- userId: request.userId,
39
+ // When an authenticated user context exists (set by auth middleware),
40
+ // always use its IDs to prevent caller-supplied impersonation.
41
+ sessionId: ctx.user
42
+ ? ctx.session?.id
43
+ : (ctx.session?.id ?? request.sessionId),
44
+ userId: ctx.user ? ctx.user.id : request.userId,
45
+ userEmail: ctx.user?.email,
46
+ userRoles: ctx.user?.roles,
47
+ requestId: ctx.requestId,
41
48
  },
42
49
  });
43
50
  // Map tool calls from SDK format to API format
@@ -78,6 +85,17 @@ export function createAgentRoutes(basePath = "/api") {
78
85
  systemPrompt: request.systemPrompt,
79
86
  temperature: request.temperature,
80
87
  maxTokens: request.maxTokens,
88
+ context: {
89
+ // When an authenticated user context exists (set by auth middleware),
90
+ // always use its IDs to prevent caller-supplied impersonation.
91
+ sessionId: ctx.user
92
+ ? ctx.session?.id
93
+ : (ctx.session?.id ?? request.sessionId),
94
+ userId: ctx.user ? ctx.user.id : request.userId,
95
+ userEmail: ctx.user?.email,
96
+ userRoles: ctx.user?.roles,
97
+ requestId: ctx.requestId,
98
+ },
81
99
  });
82
100
  // Create redactor (no-op if redaction is not enabled)
83
101
  const redactor = createStreamRedactor(ctx.redaction);