@microsoft/agents-hosting 1.1.0-alpha.8.g2362542eea → 1.1.0-alpha.85

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 (144) hide show
  1. package/dist/package.json +10 -6
  2. package/dist/src/activityWireCompat.d.ts +1 -1
  3. package/dist/src/activityWireCompat.js +11 -3
  4. package/dist/src/activityWireCompat.js.map +1 -1
  5. package/dist/src/agent-client/agentClient.js +7 -3
  6. package/dist/src/agent-client/agentClient.js.map +1 -1
  7. package/dist/src/agent-client/agentResponseHandler.js +6 -2
  8. package/dist/src/agent-client/agentResponseHandler.js.map +1 -1
  9. package/dist/src/app/agentApplication.d.ts +26 -11
  10. package/dist/src/app/agentApplication.js +90 -79
  11. package/dist/src/app/agentApplication.js.map +1 -1
  12. package/dist/src/app/agentApplicationBuilder.d.ts +2 -2
  13. package/dist/src/app/agentApplicationBuilder.js.map +1 -1
  14. package/dist/src/app/agentApplicationOptions.d.ts +9 -2
  15. package/dist/src/app/appRoute.d.ts +7 -0
  16. package/dist/src/app/{authorization.d.ts → auth/authorization.d.ts} +41 -139
  17. package/dist/src/app/auth/authorization.js +188 -0
  18. package/dist/src/app/auth/authorization.js.map +1 -0
  19. package/dist/src/app/auth/authorizationManager.d.ts +71 -0
  20. package/dist/src/app/auth/authorizationManager.js +170 -0
  21. package/dist/src/app/auth/authorizationManager.js.map +1 -0
  22. package/dist/src/app/auth/handlerStorage.d.ts +36 -0
  23. package/dist/src/app/auth/handlerStorage.js +62 -0
  24. package/dist/src/app/auth/handlerStorage.js.map +1 -0
  25. package/dist/src/app/auth/handlers/agenticAuthorization.d.ts +93 -0
  26. package/dist/src/app/auth/handlers/agenticAuthorization.js +134 -0
  27. package/dist/src/app/auth/handlers/agenticAuthorization.js.map +1 -0
  28. package/dist/src/app/auth/handlers/azureBotAuthorization.d.ts +222 -0
  29. package/dist/src/app/auth/handlers/azureBotAuthorization.js +428 -0
  30. package/dist/src/app/auth/handlers/azureBotAuthorization.js.map +1 -0
  31. package/dist/src/app/auth/handlers/index.d.ts +2 -0
  32. package/dist/src/app/auth/handlers/index.js +19 -0
  33. package/dist/src/app/auth/handlers/index.js.map +1 -0
  34. package/dist/src/app/auth/index.d.ts +2 -0
  35. package/dist/src/app/auth/index.js +19 -0
  36. package/dist/src/app/auth/index.js.map +1 -0
  37. package/dist/src/app/auth/types.d.ts +104 -0
  38. package/dist/src/app/auth/types.js +24 -0
  39. package/dist/src/app/auth/types.js.map +1 -0
  40. package/dist/src/app/index.d.ts +3 -3
  41. package/dist/src/app/index.js +2 -3
  42. package/dist/src/app/index.js.map +1 -1
  43. package/dist/src/app/routeList.d.ts +1 -1
  44. package/dist/src/app/routeList.js +22 -5
  45. package/dist/src/app/routeList.js.map +1 -1
  46. package/dist/src/app/streaming/streamingResponse.js +2 -1
  47. package/dist/src/app/streaming/streamingResponse.js.map +1 -1
  48. package/dist/src/auth/MemoryCache.d.ts +16 -0
  49. package/dist/src/auth/MemoryCache.js +58 -0
  50. package/dist/src/auth/MemoryCache.js.map +1 -0
  51. package/dist/src/auth/authConfiguration.d.ts +40 -2
  52. package/dist/src/auth/authConfiguration.js +209 -55
  53. package/dist/src/auth/authConfiguration.js.map +1 -1
  54. package/dist/src/auth/authConstants.d.ts +11 -0
  55. package/dist/src/auth/authConstants.js +15 -0
  56. package/dist/src/auth/authConstants.js.map +1 -0
  57. package/dist/src/auth/authProvider.d.ts +23 -0
  58. package/dist/src/auth/connections.d.ts +41 -0
  59. package/dist/src/auth/connections.js +7 -0
  60. package/dist/src/auth/connections.js.map +1 -0
  61. package/dist/src/auth/index.d.ts +2 -0
  62. package/dist/src/auth/index.js +2 -0
  63. package/dist/src/auth/index.js.map +1 -1
  64. package/dist/src/auth/jwt-middleware.js +31 -18
  65. package/dist/src/auth/jwt-middleware.js.map +1 -1
  66. package/dist/src/auth/msalConnectionManager.d.ts +64 -0
  67. package/dist/src/auth/msalConnectionManager.js +148 -0
  68. package/dist/src/auth/msalConnectionManager.js.map +1 -0
  69. package/dist/src/auth/msalTokenProvider.d.ts +24 -0
  70. package/dist/src/auth/msalTokenProvider.js +143 -16
  71. package/dist/src/auth/msalTokenProvider.js.map +1 -1
  72. package/dist/src/baseAdapter.d.ts +10 -25
  73. package/dist/src/baseAdapter.js +2 -15
  74. package/dist/src/baseAdapter.js.map +1 -1
  75. package/dist/src/cloudAdapter.d.ts +40 -23
  76. package/dist/src/cloudAdapter.js +141 -63
  77. package/dist/src/cloudAdapter.js.map +1 -1
  78. package/dist/src/connector-client/connectorClient.d.ts +15 -0
  79. package/dist/src/connector-client/connectorClient.js +49 -15
  80. package/dist/src/connector-client/connectorClient.js.map +1 -1
  81. package/dist/src/index.d.ts +0 -1
  82. package/dist/src/index.js +0 -1
  83. package/dist/src/index.js.map +1 -1
  84. package/dist/src/oauth/customUserTokenAPI.d.ts +1 -0
  85. package/dist/src/oauth/customUserTokenAPI.js +11 -0
  86. package/dist/src/oauth/customUserTokenAPI.js.map +1 -0
  87. package/dist/src/oauth/index.d.ts +0 -1
  88. package/dist/src/oauth/index.js +0 -1
  89. package/dist/src/oauth/index.js.map +1 -1
  90. package/dist/src/oauth/userTokenClient.d.ts +30 -13
  91. package/dist/src/oauth/userTokenClient.js +62 -26
  92. package/dist/src/oauth/userTokenClient.js.map +1 -1
  93. package/dist/src/oauth/userTokenClient.types.d.ts +19 -6
  94. package/dist/src/turnContext.d.ts +7 -1
  95. package/dist/src/turnContext.js +11 -4
  96. package/dist/src/turnContext.js.map +1 -1
  97. package/package.json +10 -6
  98. package/src/activityWireCompat.ts +12 -4
  99. package/src/agent-client/agentClient.ts +9 -3
  100. package/src/agent-client/agentResponseHandler.ts +5 -2
  101. package/src/app/agentApplication.ts +95 -74
  102. package/src/app/agentApplicationBuilder.ts +2 -2
  103. package/src/app/agentApplicationOptions.ts +10 -2
  104. package/src/app/appRoute.ts +8 -0
  105. package/src/app/auth/authorization.ts +261 -0
  106. package/src/app/auth/authorizationManager.ts +213 -0
  107. package/src/app/auth/handlerStorage.ts +61 -0
  108. package/src/app/auth/handlers/agenticAuthorization.ts +182 -0
  109. package/src/app/auth/handlers/azureBotAuthorization.ts +599 -0
  110. package/src/app/auth/handlers/index.ts +2 -0
  111. package/src/app/auth/index.ts +2 -0
  112. package/src/app/auth/types.ts +111 -0
  113. package/src/app/index.ts +3 -3
  114. package/src/app/routeList.ts +24 -5
  115. package/src/app/streaming/streamingResponse.ts +2 -1
  116. package/src/auth/MemoryCache.ts +59 -0
  117. package/src/auth/authConfiguration.ts +239 -53
  118. package/src/auth/authConstants.ts +11 -0
  119. package/src/auth/authProvider.ts +31 -0
  120. package/src/auth/connections.ts +47 -0
  121. package/src/auth/index.ts +2 -0
  122. package/src/auth/jwt-middleware.ts +38 -21
  123. package/src/auth/msalConnectionManager.ts +175 -0
  124. package/src/auth/msalTokenProvider.ts +185 -9
  125. package/src/baseAdapter.ts +10 -29
  126. package/src/cloudAdapter.ts +205 -71
  127. package/src/connector-client/connectorClient.ts +59 -15
  128. package/src/index.ts +0 -1
  129. package/src/oauth/customUserTokenAPI.ts +5 -0
  130. package/src/oauth/index.ts +0 -1
  131. package/src/oauth/userTokenClient.ts +76 -22
  132. package/src/oauth/userTokenClient.types.ts +20 -8
  133. package/src/turnContext.ts +16 -5
  134. package/dist/src/app/authorization.js +0 -387
  135. package/dist/src/app/authorization.js.map +0 -1
  136. package/dist/src/claimsIdentity.d.ts +0 -35
  137. package/dist/src/claimsIdentity.js +0 -43
  138. package/dist/src/claimsIdentity.js.map +0 -1
  139. package/dist/src/oauth/oAuthFlow.d.ts +0 -119
  140. package/dist/src/oauth/oAuthFlow.js +0 -316
  141. package/dist/src/oauth/oAuthFlow.js.map +0 -1
  142. package/src/app/authorization.ts +0 -432
  143. package/src/claimsIdentity.ts +0 -47
  144. package/src/oauth/oAuthFlow.ts +0 -378
@@ -92,17 +92,23 @@ export class AgentClient {
92
92
  const stateChanges = JSON.stringify(convRef)
93
93
  logger.debug('stateChanges: ', stateChanges)
94
94
 
95
- const authProvider = new MsalTokenProvider()
96
- const token = await authProvider.getAccessToken(authConfig, this.agentClientConfig.clientId)
95
+ const authProvider = new MsalTokenProvider(authConfig)
96
+ const token = await authProvider.getAccessToken(this.agentClientConfig.clientId)
97
97
 
98
98
  logger.debug('agent request: ', activityCopy)
99
99
 
100
+ let authHeader = '' // Allow anonymous auth.
101
+
102
+ if (token.trim().length > 0) {
103
+ authHeader = `Bearer ${token}`
104
+ }
105
+
100
106
  await conversationState.saveChanges(context, false, { channelId: activityCopy.channelId!, conversationId: activityCopy.conversation!.id })
101
107
  const response = await fetch(this.agentClientConfig.endPoint, {
102
108
  method: 'POST',
103
109
  headers: {
104
110
  'Content-Type': 'application/json',
105
- Authorization: `Bearer ${token}`,
111
+ Authorization: authHeader,
106
112
  'x-ms-conversation-id': activityCopy.conversation!.id
107
113
  },
108
114
  body: JSON.stringify(activityCopy)
@@ -58,7 +58,10 @@ const handleResponse = (adapter: CloudAdapter, handler: ActivityHandler, convers
58
58
 
59
59
  logger.debug('received response: ', activity)
60
60
 
61
- const myTurnContext = new TurnContext(adapter, activity)
61
+ const connection = adapter.connectionManager.getDefaultConnection()
62
+ const appId = connection?.connectionSettings?.clientId ?? ''
63
+
64
+ const myTurnContext = new TurnContext(adapter, activity, CloudAdapter.createIdentity(appId))
62
65
  const conversationDataAccessor = conversationState.createProperty<ConversationReferenceState>(req.params!.conversationId)
63
66
  const conversationRefState = await conversationDataAccessor.get(myTurnContext, undefined, { channelId: activity.channelId!, conversationId: req.params!.conversationId })
64
67
 
@@ -82,7 +85,7 @@ const handleResponse = (adapter: CloudAdapter, handler: ActivityHandler, convers
82
85
  res.status(200).send(response)
83
86
  }
84
87
 
85
- await adapter.continueConversation(conversationRefState.conversationReference, callback, true)
88
+ await adapter.continueConversation(myTurnContext.identity, conversationRefState.conversationReference, callback)
86
89
  }
87
90
 
88
91
  const applyActivityToTurnContext = (turnContext : TurnContext, activity : Activity) => {
@@ -4,7 +4,6 @@
4
4
  */
5
5
 
6
6
  import { Activity, ActivityTypes, ConversationReference } from '@microsoft/agents-activity'
7
- import { BaseAdapter } from '../baseAdapter'
8
7
  import { ResourceResponse } from '../connector-client'
9
8
  import { debug } from '@microsoft/agents-activity/logger'
10
9
  import { TurnContext } from '../turnContext'
@@ -12,7 +11,6 @@ import { AdaptiveCardsActions } from './adaptiveCards'
12
11
  import { AgentApplicationOptions } from './agentApplicationOptions'
13
12
  import { ConversationUpdateEvents } from './conversationUpdateEvents'
14
13
  import { AgentExtension } from './extensions'
15
- import { Authorization, SignInState } from './authorization'
16
14
  import { RouteHandler } from './routeHandler'
17
15
  import { RouteSelector } from './routeSelector'
18
16
  import { TurnEvents } from './turnEvents'
@@ -21,6 +19,8 @@ import { RouteRank } from './routeRank'
21
19
  import { RouteList } from './routeList'
22
20
  import { TranscriptLoggerMiddleware } from '../transcript'
23
21
  import { CloudAdapter } from '../cloudAdapter'
22
+ import { Authorization, UserAuthorization, AuthorizationManager } from './auth'
23
+ import { JwtPayload } from 'jsonwebtoken'
24
24
 
25
25
  const logger = debug('agents:app')
26
26
 
@@ -74,6 +74,7 @@ export class AgentApplication<TState extends TurnState> {
74
74
  protected readonly _beforeTurn: ApplicationEventHandler<TState>[] = []
75
75
  protected readonly _afterTurn: ApplicationEventHandler<TState>[] = []
76
76
  private readonly _adapter?: CloudAdapter
77
+ private readonly _authorizationManager?: AuthorizationManager
77
78
  private readonly _authorization?: Authorization
78
79
  private _typingTimer: NodeJS.Timeout | undefined
79
80
  protected readonly _extensions: AgentExtension<TState>[] = []
@@ -125,7 +126,8 @@ export class AgentApplication<TState extends TurnState> {
125
126
  }
126
127
 
127
128
  if (this._options.authorization) {
128
- this._authorization = new Authorization(this._options.storage!, this._options.authorization, this._adapter?.userTokenClient!)
129
+ this._authorizationManager = new AuthorizationManager(this, this._adapter.connectionManager)
130
+ this._authorization = new UserAuthorization(this._authorizationManager)
129
131
  }
130
132
 
131
133
  if (this._options.longRunningMessages && !this._adapter && !this._options.agentAppId) {
@@ -169,7 +171,7 @@ export class AgentApplication<TState extends TurnState> {
169
171
  *
170
172
  * @returns The adapter instance.
171
173
  */
172
- public get adapter (): BaseAdapter {
174
+ public get adapter (): CloudAdapter {
173
175
  return this._adapter!
174
176
  }
175
177
 
@@ -227,6 +229,7 @@ export class AgentApplication<TState extends TurnState> {
227
229
  * @param isInvokeRoute - Whether this route is for invoke activities. Defaults to false.
228
230
  * @param rank - The rank of the route, used to determine the order of evaluation. Defaults to RouteRank.Unspecified.
229
231
  * @param authHandlers - Array of authentication handler names for this route. Defaults to empty array.
232
+ * @param isAgenticRoute - Whether this route is for agentic requests only. Defaults to false.
230
233
  * @returns The current instance of the application.
231
234
  *
232
235
  * @remarks
@@ -247,8 +250,8 @@ export class AgentApplication<TState extends TurnState> {
247
250
  * ```
248
251
  *
249
252
  */
250
- public addRoute (selector: RouteSelector, handler: RouteHandler<TState>, isInvokeRoute: boolean = false, rank: number = RouteRank.Unspecified, authHandlers: string[] = []): this {
251
- this._routes.addRoute(selector, handler, isInvokeRoute, rank, authHandlers)
253
+ public addRoute (selector: RouteSelector, handler: RouteHandler<TState>, isInvokeRoute: boolean = false, rank: number = RouteRank.Unspecified, authHandlers: string[] = [], isAgenticRoute: boolean = false): this {
254
+ this._routes.addRoute(selector, handler, isInvokeRoute, rank, authHandlers, isAgenticRoute)
252
255
  return this
253
256
  }
254
257
 
@@ -259,6 +262,7 @@ export class AgentApplication<TState extends TurnState> {
259
262
  * @param handler - The handler function that will be called when the specified activity type is received.
260
263
  * @param authHandlers - Array of authentication handler names for this activity. Defaults to empty array.
261
264
  * @param rank - The rank of the route, used to determine the order of evaluation. Defaults to RouteRank.Unspecified.
265
+ * @param isAgenticRoute - Indicates if this handler is for agentic requests only. Defaults to false.
262
266
  * @returns The current instance of the application.
263
267
  *
264
268
  * @remarks
@@ -277,11 +281,12 @@ export class AgentApplication<TState extends TurnState> {
277
281
  type: string | RegExp | RouteSelector | (string | RegExp | RouteSelector)[],
278
282
  handler: (context: TurnContext, state: TState) => Promise<void>,
279
283
  authHandlers: string[] = [],
280
- rank: RouteRank = RouteRank.Unspecified
284
+ rank: RouteRank = RouteRank.Unspecified,
285
+ isAgenticRoute: boolean = false
281
286
  ): this {
282
287
  (Array.isArray(type) ? type : [type]).forEach((t) => {
283
- const selector = this.createActivitySelector(t)
284
- this.addRoute(selector, handler, false, rank, authHandlers)
288
+ const selector = this.createActivitySelector(t, isAgenticRoute)
289
+ this.addRoute(selector, handler, false, rank, authHandlers, isAgenticRoute)
285
290
  })
286
291
  return this
287
292
  }
@@ -293,6 +298,7 @@ export class AgentApplication<TState extends TurnState> {
293
298
  * @param handler - The handler function that will be called when the specified event occurs.
294
299
  * @param authHandlers - Array of authentication handler names for this event. Defaults to empty array.
295
300
  * @param rank - The rank of the route, used to determine the order of evaluation. Defaults to RouteRank.Unspecified.
301
+ * @param isAgenticRoute - Indicates if this handler is for agentic requests only. Defaults to false.
296
302
  * @returns The current instance of the application.
297
303
  * @throws Error if the handler is not a function.
298
304
  *
@@ -316,7 +322,8 @@ export class AgentApplication<TState extends TurnState> {
316
322
  event: ConversationUpdateEvents,
317
323
  handler: (context: TurnContext, state: TState) => Promise<void>,
318
324
  authHandlers: string[] = [],
319
- rank: RouteRank = RouteRank.Unspecified
325
+ rank: RouteRank = RouteRank.Unspecified,
326
+ isAgenticRoute: boolean = false
320
327
  ): this {
321
328
  if (typeof handler !== 'function') {
322
329
  throw new Error(
@@ -324,8 +331,8 @@ export class AgentApplication<TState extends TurnState> {
324
331
  )
325
332
  }
326
333
 
327
- const selector = this.createConversationUpdateSelector(event)
328
- this.addRoute(selector, handler, false, rank, authHandlers)
334
+ const selector = this.createConversationUpdateSelector(event, isAgenticRoute)
335
+ this.addRoute(selector, handler, false, rank, authHandlers, isAgenticRoute)
329
336
  return this
330
337
  }
331
338
 
@@ -338,6 +345,7 @@ export class AgentApplication<TState extends TurnState> {
338
345
  * @throws Error if the adapter is not configured.
339
346
  */
340
347
  protected async continueConversationAsync (
348
+ botAppIdOrIdentity: string | JwtPayload,
341
349
  conversationReferenceOrContext: ConversationReference | TurnContext,
342
350
  logic: (context: TurnContext) => Promise<void>
343
351
  ): Promise<void> {
@@ -359,7 +367,7 @@ export class AgentApplication<TState extends TurnState> {
359
367
  reference = conversationReferenceOrContext
360
368
  }
361
369
 
362
- await this._adapter.continueConversation(reference, logic)
370
+ await this._adapter.continueConversation(botAppIdOrIdentity, reference, logic)
363
371
  }
364
372
 
365
373
  /**
@@ -370,6 +378,7 @@ export class AgentApplication<TState extends TurnState> {
370
378
  * @param handler - The handler function that will be called when a matching message is received.
371
379
  * @param authHandlers - Array of authentication handler names for this message handler. Defaults to empty array.
372
380
  * @param rank - The rank of the route, used to determine the order of evaluation. Defaults to RouteRank.Unspecified.
381
+ * @param isAgenticRoute - Indicates if this handler is for agentic requests only. Defaults to false.
373
382
  * @returns The current instance of the application.
374
383
  *
375
384
  * @remarks
@@ -394,11 +403,12 @@ export class AgentApplication<TState extends TurnState> {
394
403
  keyword: string | RegExp | RouteSelector | (string | RegExp | RouteSelector)[],
395
404
  handler: (context: TurnContext, state: TState) => Promise<void>,
396
405
  authHandlers: string[] = [],
397
- rank: RouteRank = RouteRank.Unspecified
406
+ rank: RouteRank = RouteRank.Unspecified,
407
+ isAgenticRoute: boolean = false
398
408
  ): this {
399
409
  (Array.isArray(keyword) ? keyword : [keyword]).forEach((k) => {
400
- const selector = this.createMessageSelector(k)
401
- this.addRoute(selector, handler, false, rank, authHandlers)
410
+ const selector = this.createMessageSelector(k, isAgenticRoute)
411
+ this.addRoute(selector, handler, false, rank, authHandlers, isAgenticRoute)
402
412
  })
403
413
  return this
404
414
  }
@@ -468,6 +478,7 @@ export class AgentApplication<TState extends TurnState> {
468
478
  *
469
479
  * @param handler - The handler function that will be called when a message reaction is added.
470
480
  * @param rank - The rank of the route, used to determine the order of evaluation. Defaults to RouteRank.Unspecified.
481
+ * @param isAgenticRoute - Indicates if this handler is for agentic requests only. Defaults to false.
471
482
  * @returns The current instance of the application.
472
483
  *
473
484
  * @remarks
@@ -487,14 +498,16 @@ export class AgentApplication<TState extends TurnState> {
487
498
  */
488
499
  public onMessageReactionAdded (
489
500
  handler: (context: TurnContext, state: TState) => Promise<void>,
490
- rank: RouteRank = RouteRank.Unspecified): this {
501
+ rank: RouteRank = RouteRank.Unspecified,
502
+ isAgenticRoute: boolean = false): this {
491
503
  const selector = async (context: TurnContext): Promise<boolean> => {
492
504
  return context.activity.type === ActivityTypes.MessageReaction &&
493
505
  Array.isArray(context.activity.reactionsAdded) &&
494
- context.activity.reactionsAdded.length > 0
506
+ context.activity.reactionsAdded.length > 0 &&
507
+ (!isAgenticRoute || (isAgenticRoute && context.activity.isAgenticRequest()))
495
508
  }
496
509
 
497
- this.addRoute(selector, handler, false, rank)
510
+ this.addRoute(selector, handler, false, rank, [], isAgenticRoute)
498
511
  return this
499
512
  }
500
513
 
@@ -503,6 +516,7 @@ export class AgentApplication<TState extends TurnState> {
503
516
  *
504
517
  * @param handler - The handler function that will be called when a message reaction is removed.
505
518
  * @param rank - The rank of the route, used to determine the order of evaluation. Defaults to RouteRank.Unspecified.
519
+ * @param isAgenticRoute - Indicates if this handler is for agentic requests only. Defaults to false.
506
520
  * @returns The current instance of the application.
507
521
  *
508
522
  * @remarks
@@ -522,14 +536,16 @@ export class AgentApplication<TState extends TurnState> {
522
536
  */
523
537
  public onMessageReactionRemoved (
524
538
  handler: (context: TurnContext, state: TState) => Promise<void>,
525
- rank: RouteRank = RouteRank.Unspecified): this {
539
+ rank: RouteRank = RouteRank.Unspecified,
540
+ isAgenticRoute: boolean = false): this {
526
541
  const selector = async (context: TurnContext): Promise<boolean> => {
527
542
  return context.activity.type === ActivityTypes.MessageReaction &&
528
543
  Array.isArray(context.activity.reactionsRemoved) &&
529
- context.activity.reactionsRemoved.length > 0
544
+ context.activity.reactionsRemoved.length > 0 &&
545
+ (!isAgenticRoute || (isAgenticRoute && context.activity.isAgenticRequest()))
530
546
  }
531
547
 
532
- this.addRoute(selector, handler, false, rank)
548
+ this.addRoute(selector, handler, false, rank, undefined, isAgenticRoute)
533
549
  return this
534
550
  }
535
551
 
@@ -587,6 +603,10 @@ export class AgentApplication<TState extends TurnState> {
587
603
  *
588
604
  */
589
605
  public async runInternal (turnContext: TurnContext): Promise<boolean> {
606
+ if (turnContext.activity.type === ActivityTypes.Typing) {
607
+ return false
608
+ }
609
+
590
610
  logger.info('Running application with activity:', turnContext.activity.id!)
591
611
  return await this.startLongRunningCall(turnContext, async (context) => {
592
612
  try {
@@ -606,23 +626,23 @@ export class AgentApplication<TState extends TurnState> {
606
626
  const state = turnStateFactory()
607
627
  await state.load(context, storage)
608
628
 
609
- const signInState : SignInState = state.getValue('user.__SIGNIN_STATE_')
610
- logger.debug('SignIn State:', signInState)
611
- if (this._authorization && signInState && signInState.completed === false) {
612
- const flowState = await this._authorization.authHandlers[signInState.handlerId!]?.flow?.getFlowState(context)
613
- logger.debug('Flow State:', flowState)
614
- if (flowState && flowState.flowStarted === true) {
615
- const tokenResponse = await this._authorization.beginOrContinueFlow(turnContext, state, signInState?.handlerId!)
616
- const savedAct = Activity.fromObject(signInState?.continuationActivity!)
617
- if (tokenResponse?.token && tokenResponse.token.length > 0) {
618
- logger.info('resending continuation activity:', savedAct.text)
619
- await this.run(new TurnContext(context.adapter, savedAct))
620
- await state.deleteValue('user.__SIGNIN_STATE_')
621
- return true
622
- }
623
- }
629
+ const { authorized } = await this._authorizationManager?.process(context, async activity => {
630
+ // The incoming activity may come from the storage, so we need to restore the auth handlers.
631
+ // Since the current route may not have auth handlers.
632
+ const route = await this.getRoute(new TurnContext(context.adapter, activity, turnContext.identity))
633
+ return route?.authHandlers ?? []
634
+ }) ?? { authorized: true } // Default to authorized if no auth manager
635
+
636
+ if (!authorized) {
637
+ await state.save(context, storage)
638
+ return false
639
+ }
640
+
641
+ const route = await this.getRoute(context)
624
642
 
625
- // return true
643
+ if (!route) {
644
+ logger.debug('No matching route found for activity:', context.activity)
645
+ return false
626
646
  }
627
647
 
628
648
  if (Array.isArray(this._options.fileDownloaders) && this._options.fileDownloaders.length > 0) {
@@ -636,38 +656,13 @@ export class AgentApplication<TState extends TurnState> {
636
656
  return false
637
657
  }
638
658
 
639
- for (const route of this._routes) {
640
- if (await route.selector(context)) {
641
- if (route.authHandlers === undefined || route.authHandlers.length === 0) {
642
- await route.handler(context, state)
643
- } else {
644
- let signInComplete = false
645
- for (const authHandlerId of route.authHandlers) {
646
- logger.info(`Executing route handler for authHandlerId: ${authHandlerId}`)
647
- const tokenResponse = await this._authorization?.beginOrContinueFlow(turnContext, state, authHandlerId)
648
- signInComplete = (tokenResponse?.token !== undefined && tokenResponse?.token.length > 0)
649
- if (!signInComplete) {
650
- break
651
- }
652
- }
653
- if (signInComplete) {
654
- await route.handler(context, state)
655
- }
656
- }
657
-
658
- if (await this.callEventHandlers(context, state, this._afterTurn)) {
659
- await state.save(context, storage)
660
- }
661
-
662
- return true
663
- }
664
- }
659
+ await route.handler(context, state)
665
660
 
666
661
  if (await this.callEventHandlers(context, state, this._afterTurn)) {
667
662
  await state.save(context, storage)
668
663
  }
669
664
 
670
- return false
665
+ return true
671
666
  } catch (err: any) {
672
667
  logger.error(err)
673
668
  throw err
@@ -677,6 +672,17 @@ export class AgentApplication<TState extends TurnState> {
677
672
  })
678
673
  }
679
674
 
675
+ /**
676
+ * Finds the appropriate route for the given context.
677
+ */
678
+ private async getRoute (context: TurnContext) {
679
+ for (const route of this._routes) {
680
+ if (await route.selector(context)) {
681
+ return route
682
+ }
683
+ }
684
+ }
685
+
680
686
  /**
681
687
  * Sends a proactive message to a conversation.
682
688
  *
@@ -700,13 +706,14 @@ export class AgentApplication<TState extends TurnState> {
700
706
  *
701
707
  */
702
708
  public async sendProactiveActivity (
709
+ botAppIdOrIdentity: string | JwtPayload,
703
710
  context: TurnContext | ConversationReference,
704
711
  activityOrText: string | Activity,
705
712
  speak?: string,
706
713
  inputHint?: string
707
714
  ): Promise<ResourceResponse | undefined> {
708
715
  let response: ResourceResponse | undefined
709
- await this.continueConversationAsync(context, async (ctx) => {
716
+ await this.continueConversationAsync(botAppIdOrIdentity, context, async (ctx) => {
710
717
  response = await ctx.sendActivity(activityOrText, speak, inputHint)
711
718
  })
712
719
 
@@ -905,7 +912,7 @@ export class AgentApplication<TState extends TurnState> {
905
912
  ): Promise<boolean> {
906
913
  if (context.activity.type === ActivityTypes.Message && this._options.longRunningMessages) {
907
914
  return new Promise<boolean>((resolve, reject) => {
908
- this.continueConversationAsync(context, async (ctx) => {
915
+ this.continueConversationAsync(context.identity, context, async (ctx) => {
909
916
  try {
910
917
  for (const key in context.activity) {
911
918
  (ctx.activity as any)[key] = (context.activity as any)[key]
@@ -928,20 +935,25 @@ export class AgentApplication<TState extends TurnState> {
928
935
  * Creates a selector function for activity types.
929
936
  *
930
937
  * @param type - The activity type to match. Can be a string, RegExp, or RouteSelector function.
938
+ * @param isAgenticRoute - Indicates if the route is for agentic requests only. Defaults to false.
931
939
  * @returns A RouteSelector function that matches the specified activity type.
932
940
  */
933
- private createActivitySelector (type: string | RegExp | RouteSelector): RouteSelector {
941
+ private createActivitySelector (type: string | RegExp | RouteSelector, isAgenticRoute: boolean = false): RouteSelector {
934
942
  if (typeof type === 'function') {
935
943
  return type
936
944
  } else if (type instanceof RegExp) {
937
945
  return (context: TurnContext) => {
938
- return Promise.resolve(context?.activity?.type ? type.test(context.activity.type) : false)
946
+ return Promise.resolve(context?.activity?.type
947
+ ? type.test(context.activity.type) && (!isAgenticRoute || (isAgenticRoute && context.activity.isAgenticRequest()))
948
+ : false)
939
949
  }
940
950
  } else {
941
951
  const typeName = type.toString().toLocaleLowerCase()
942
952
  return (context: TurnContext) => {
943
953
  return Promise.resolve(
944
- context?.activity?.type ? context.activity.type.toLocaleLowerCase() === typeName : false
954
+ context?.activity?.type
955
+ ? context.activity.type.toLocaleLowerCase() === typeName && (!isAgenticRoute || (isAgenticRoute && context.activity.isAgenticRequest()))
956
+ : false
945
957
  )
946
958
  }
947
959
  }
@@ -951,13 +963,15 @@ export class AgentApplication<TState extends TurnState> {
951
963
  * Creates a selector function for conversation update events.
952
964
  *
953
965
  * @param event - The conversation update event to match.
966
+ * @param isAgenticRoute - Indicates if the route is for agentic requests only. Defaults to false.
954
967
  * @returns A RouteSelector function that matches the specified conversation update event.
955
968
  */
956
- private createConversationUpdateSelector (event: ConversationUpdateEvents): RouteSelector {
969
+ private createConversationUpdateSelector (event: ConversationUpdateEvents, isAgenticRoute: boolean = false): RouteSelector {
957
970
  switch (event) {
958
971
  case 'membersAdded':
959
972
  return (context: TurnContext): Promise<boolean> => {
960
973
  return Promise.resolve(
974
+ (!isAgenticRoute || (isAgenticRoute && context.activity.isAgenticRequest())) &&
961
975
  context?.activity?.type === ActivityTypes.ConversationUpdate &&
962
976
  Array.isArray(context?.activity?.membersAdded) &&
963
977
  context.activity.membersAdded.length > 0
@@ -966,6 +980,7 @@ export class AgentApplication<TState extends TurnState> {
966
980
  case 'membersRemoved':
967
981
  return (context: TurnContext): Promise<boolean> => {
968
982
  return Promise.resolve(
983
+ (!isAgenticRoute || (isAgenticRoute && context.activity.isAgenticRequest())) &&
969
984
  context?.activity?.type === ActivityTypes.ConversationUpdate &&
970
985
  Array.isArray(context?.activity?.membersRemoved) &&
971
986
  context.activity.membersRemoved.length > 0
@@ -974,6 +989,7 @@ export class AgentApplication<TState extends TurnState> {
974
989
  default:
975
990
  return (context: TurnContext): Promise<boolean> => {
976
991
  return Promise.resolve(
992
+ (!isAgenticRoute || (isAgenticRoute && context.activity.isAgenticRequest())) &&
977
993
  context?.activity?.type === ActivityTypes.ConversationUpdate &&
978
994
  context?.activity?.channelData?.eventType === event
979
995
  )
@@ -985,14 +1001,17 @@ export class AgentApplication<TState extends TurnState> {
985
1001
  * Creates a selector function for message content matching.
986
1002
  *
987
1003
  * @param keyword - The keyword, pattern, or selector function to match against message text.
1004
+ * @param isAgenticRoute - Indicates if the route is for agentic requests only. Defaults to false.
988
1005
  * @returns A RouteSelector function that matches messages based on the specified keyword.
989
1006
  */
990
- private createMessageSelector (keyword: string | RegExp | RouteSelector): RouteSelector {
1007
+ private createMessageSelector (keyword: string | RegExp | RouteSelector, isAgenticRoute: boolean = false): RouteSelector {
991
1008
  if (typeof keyword === 'function') {
992
1009
  return keyword
993
1010
  } else if (keyword instanceof RegExp) {
994
1011
  return (context: TurnContext) => {
995
- if (context?.activity?.type === ActivityTypes.Message && context.activity.text) {
1012
+ if (context?.activity?.type === ActivityTypes.Message &&
1013
+ context.activity.text &&
1014
+ (!isAgenticRoute || (isAgenticRoute && context.activity.isAgenticRequest()))) {
996
1015
  return Promise.resolve(keyword.test(context.activity.text))
997
1016
  } else {
998
1017
  return Promise.resolve(false)
@@ -1001,7 +1020,9 @@ export class AgentApplication<TState extends TurnState> {
1001
1020
  } else {
1002
1021
  const k = keyword.toString().toLocaleLowerCase()
1003
1022
  return (context: TurnContext) => {
1004
- if (context?.activity?.type === ActivityTypes.Message && context.activity.text) {
1023
+ if (context?.activity?.type === ActivityTypes.Message &&
1024
+ context.activity.text &&
1025
+ (!isAgenticRoute || (isAgenticRoute && context.activity.isAgenticRequest()))) {
1005
1026
  return Promise.resolve(context.activity.text.toLocaleLowerCase() === k)
1006
1027
  } else {
1007
1028
  return Promise.resolve(false)
@@ -6,7 +6,7 @@
6
6
  import { Storage } from '../storage'
7
7
  import { AgentApplication } from './agentApplication'
8
8
  import { AgentApplicationOptions } from './agentApplicationOptions'
9
- import { AuthorizationHandlers } from './authorization'
9
+ import { AuthorizationOptions } from './auth/types'
10
10
  import { TurnState } from './turnState'
11
11
 
12
12
  /**
@@ -59,7 +59,7 @@ export class AgentApplicationBuilder<TState extends TurnState = TurnState> {
59
59
  * @param authHandlers The user identity authentication options
60
60
  * @returns This builder instance for chaining
61
61
  */
62
- public withAuthorization (authHandlers: AuthorizationHandlers): this {
62
+ public withAuthorization (authHandlers: AuthorizationOptions): this {
63
63
  this._options.authorization = authHandlers
64
64
  return this
65
65
  }
@@ -8,9 +8,10 @@ import { Storage } from '../storage'
8
8
  import { TranscriptLogger } from '../transcript'
9
9
  import { AdaptiveCardsOptions } from './adaptiveCards'
10
10
  import { InputFileDownloader } from './inputFileDownloader'
11
- import { AuthorizationHandlers } from './authorization'
12
11
  import { TurnState } from './turnState'
13
12
  import { HeaderPropagationDefinition } from '../headerPropagation'
13
+ import { AuthorizationOptions } from './auth/types'
14
+ import { Connections } from '../auth/connections'
14
15
 
15
16
  /**
16
17
  * Configuration options for creating and initializing an Agent Application.
@@ -95,7 +96,7 @@ export interface AgentApplicationOptions<TState extends TurnState> {
95
96
  *
96
97
  * @default undefined (no authorization required)
97
98
  */
98
- authorization?: AuthorizationHandlers;
99
+ authorization?: AuthorizationOptions;
99
100
 
100
101
  /**
101
102
  * Configuration options for handling Adaptive Card actions and interactions.
@@ -139,4 +140,11 @@ export interface AgentApplicationOptions<TState extends TurnState> {
139
140
  * @default undefined
140
141
  */
141
142
  headerPropagation?: HeaderPropagationDefinition
143
+
144
+ /**
145
+ * Optional. Configuration for managing multiple authentication connections within the agent.
146
+ * This allows the agent to handle authentication across different services or
147
+ * identity providers.
148
+ */
149
+ connections?: Connections
142
150
  }
@@ -51,6 +51,14 @@ export interface AppRoute<TState extends TurnState> {
51
51
  */
52
52
  handler: RouteHandler<TState>;
53
53
 
54
+ /**
55
+ * Indicates whether this route is an agentic-only route.
56
+ *
57
+ * @default false
58
+ *
59
+ */
60
+ isAgenticRoute?: boolean;
61
+
54
62
  /**
55
63
  * Indicates whether this route is an invoke route.
56
64
  *