@microsoft/agents-hosting 1.5.0-beta.6.ga236d9a19c → 1.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (157) hide show
  1. package/dist/package.json +10 -9
  2. package/dist/src/activityHandler.js +2 -2
  3. package/dist/src/activityHandler.js.map +1 -1
  4. package/dist/src/agent-client/agentClient.js +49 -40
  5. package/dist/src/agent-client/agentClient.js.map +1 -1
  6. package/dist/src/agent-client/agentResponseHandler.js +2 -2
  7. package/dist/src/agent-client/agentResponseHandler.js.map +1 -1
  8. package/dist/src/app/agentApplication.d.ts +36 -10
  9. package/dist/src/app/agentApplication.js +169 -99
  10. package/dist/src/app/agentApplication.js.map +1 -1
  11. package/dist/src/app/agentApplicationBuilder.d.ts +15 -0
  12. package/dist/src/app/agentApplicationBuilder.js +22 -4
  13. package/dist/src/app/agentApplicationBuilder.js.map +1 -1
  14. package/dist/src/app/agentApplicationOptions.d.ts +38 -0
  15. package/dist/src/app/attachmentDownloader.js +2 -2
  16. package/dist/src/app/attachmentDownloader.js.map +1 -1
  17. package/dist/src/app/auth/authorization.js +12 -9
  18. package/dist/src/app/auth/authorization.js.map +1 -1
  19. package/dist/src/app/auth/authorizationManager.d.ts +18 -5
  20. package/dist/src/app/auth/authorizationManager.js +258 -45
  21. package/dist/src/app/auth/authorizationManager.js.map +1 -1
  22. package/dist/src/app/auth/handlerStorage.js +3 -1
  23. package/dist/src/app/auth/handlerStorage.js.map +1 -1
  24. package/dist/src/app/auth/handlers/agenticAuthorization.d.ts +19 -16
  25. package/dist/src/app/auth/handlers/agenticAuthorization.js +46 -52
  26. package/dist/src/app/auth/handlers/agenticAuthorization.js.map +1 -1
  27. package/dist/src/app/auth/handlers/azureBotAuthorization.d.ts +51 -75
  28. package/dist/src/app/auth/handlers/azureBotAuthorization.js +217 -192
  29. package/dist/src/app/auth/handlers/azureBotAuthorization.js.map +1 -1
  30. package/dist/src/app/auth/types.d.ts +100 -1
  31. package/dist/src/app/auth/utils.d.ts +10 -0
  32. package/dist/src/app/auth/utils.js +21 -0
  33. package/dist/src/app/auth/utils.js.map +1 -0
  34. package/dist/src/app/index.d.ts +1 -0
  35. package/dist/src/app/index.js +1 -0
  36. package/dist/src/app/index.js.map +1 -1
  37. package/dist/src/app/proactive/conversation.d.ts +43 -0
  38. package/dist/src/app/proactive/conversation.js +67 -0
  39. package/dist/src/app/proactive/conversation.js.map +1 -0
  40. package/dist/src/app/proactive/conversationBuilder.d.ts +54 -0
  41. package/dist/src/app/proactive/conversationBuilder.js +110 -0
  42. package/dist/src/app/proactive/conversationBuilder.js.map +1 -0
  43. package/dist/src/app/proactive/conversationReferenceBuilder.d.ts +68 -0
  44. package/dist/src/app/proactive/conversationReferenceBuilder.js +125 -0
  45. package/dist/src/app/proactive/conversationReferenceBuilder.js.map +1 -0
  46. package/dist/src/app/proactive/createConversationOptions.d.ts +30 -0
  47. package/dist/src/app/proactive/createConversationOptions.js +10 -0
  48. package/dist/src/app/proactive/createConversationOptions.js.map +1 -0
  49. package/dist/src/app/proactive/createConversationOptionsBuilder.d.ts +69 -0
  50. package/dist/src/app/proactive/createConversationOptionsBuilder.js +141 -0
  51. package/dist/src/app/proactive/createConversationOptionsBuilder.js.map +1 -0
  52. package/dist/src/app/proactive/index.d.ts +7 -0
  53. package/dist/src/app/proactive/index.js +26 -0
  54. package/dist/src/app/proactive/index.js.map +1 -0
  55. package/dist/src/app/proactive/proactive.d.ts +248 -0
  56. package/dist/src/app/proactive/proactive.js +310 -0
  57. package/dist/src/app/proactive/proactive.js.map +1 -0
  58. package/dist/src/app/proactive/proactiveOptions.d.ts +19 -0
  59. package/dist/src/app/proactive/proactiveOptions.js +5 -0
  60. package/dist/src/app/proactive/proactiveOptions.js.map +1 -0
  61. package/dist/src/app/streaming/streamingResponse.js +2 -2
  62. package/dist/src/app/streaming/streamingResponse.js.map +1 -1
  63. package/dist/src/app/teamsAttachmentDownloader.js +2 -2
  64. package/dist/src/app/teamsAttachmentDownloader.js.map +1 -1
  65. package/dist/src/app/turnState.js +2 -2
  66. package/dist/src/app/turnState.js.map +1 -1
  67. package/dist/src/auth/authConfiguration.d.ts +61 -0
  68. package/dist/src/auth/authConfiguration.js +52 -3
  69. package/dist/src/auth/authConfiguration.js.map +1 -1
  70. package/dist/src/auth/jwt-middleware.js +2 -2
  71. package/dist/src/auth/jwt-middleware.js.map +1 -1
  72. package/dist/src/auth/msalConnectionManager.js +20 -0
  73. package/dist/src/auth/msalConnectionManager.js.map +1 -1
  74. package/dist/src/auth/msalTokenCredential.js +3 -0
  75. package/dist/src/auth/msalTokenCredential.js.map +1 -1
  76. package/dist/src/auth/msalTokenProvider.js +136 -110
  77. package/dist/src/auth/msalTokenProvider.js.map +1 -1
  78. package/dist/src/baseAdapter.js +2 -2
  79. package/dist/src/baseAdapter.js.map +1 -1
  80. package/dist/src/cloudAdapter.js +201 -154
  81. package/dist/src/cloudAdapter.js.map +1 -1
  82. package/dist/src/connector-client/connectorClient.js +176 -127
  83. package/dist/src/connector-client/connectorClient.js.map +1 -1
  84. package/dist/src/errorHelper.js +108 -0
  85. package/dist/src/errorHelper.js.map +1 -1
  86. package/dist/src/middlewareSet.js +2 -2
  87. package/dist/src/middlewareSet.js.map +1 -1
  88. package/dist/src/oauth/userTokenClient.js +78 -48
  89. package/dist/src/oauth/userTokenClient.js.map +1 -1
  90. package/dist/src/observability/index.d.ts +2 -0
  91. package/dist/src/observability/index.js +21 -0
  92. package/dist/src/observability/index.js.map +1 -0
  93. package/dist/src/observability/metrics.d.ts +21 -0
  94. package/dist/src/observability/metrics.js +87 -0
  95. package/dist/src/observability/metrics.js.map +1 -0
  96. package/dist/src/observability/traces.d.ts +234 -0
  97. package/dist/src/observability/traces.js +962 -0
  98. package/dist/src/observability/traces.js.map +1 -0
  99. package/dist/src/state/agentState.js +2 -2
  100. package/dist/src/state/agentState.js.map +1 -1
  101. package/dist/src/storage/fileStorage.js +38 -28
  102. package/dist/src/storage/fileStorage.js.map +1 -1
  103. package/dist/src/storage/memoryStorage.js +41 -30
  104. package/dist/src/storage/memoryStorage.js.map +1 -1
  105. package/dist/src/transcript/fileTranscriptLogger.js +2 -2
  106. package/dist/src/transcript/fileTranscriptLogger.js.map +1 -1
  107. package/dist/src/transcript/transcriptLoggerMiddleware.js +2 -2
  108. package/dist/src/transcript/transcriptLoggerMiddleware.js.map +1 -1
  109. package/dist/src/turnContext.js +48 -42
  110. package/dist/src/turnContext.js.map +1 -1
  111. package/package.json +10 -9
  112. package/src/activityHandler.ts +1 -1
  113. package/src/agent-client/agentClient.ts +53 -42
  114. package/src/agent-client/agentResponseHandler.ts +1 -1
  115. package/src/app/agentApplication.ts +212 -86
  116. package/src/app/agentApplicationBuilder.ts +26 -4
  117. package/src/app/agentApplicationOptions.ts +43 -0
  118. package/src/app/attachmentDownloader.ts +1 -1
  119. package/src/app/auth/authorization.ts +11 -8
  120. package/src/app/auth/authorizationManager.ts +297 -45
  121. package/src/app/auth/handlerStorage.ts +3 -1
  122. package/src/app/auth/handlers/agenticAuthorization.ts +68 -72
  123. package/src/app/auth/handlers/azureBotAuthorization.ts +260 -264
  124. package/src/app/auth/types.ts +102 -1
  125. package/src/app/auth/utils.ts +22 -0
  126. package/src/app/index.ts +1 -0
  127. package/src/app/proactive/conversation.ts +87 -0
  128. package/src/app/proactive/conversationBuilder.ts +139 -0
  129. package/src/app/proactive/conversationReferenceBuilder.ts +161 -0
  130. package/src/app/proactive/createConversationOptions.ts +35 -0
  131. package/src/app/proactive/createConversationOptionsBuilder.ts +181 -0
  132. package/src/app/proactive/index.ts +10 -0
  133. package/src/app/proactive/proactive.ts +524 -0
  134. package/src/app/proactive/proactiveOptions.ts +24 -0
  135. package/src/app/streaming/streamingResponse.ts +1 -1
  136. package/src/app/teamsAttachmentDownloader.ts +1 -1
  137. package/src/app/turnState.ts +1 -1
  138. package/src/auth/authConfiguration.ts +58 -1
  139. package/src/auth/jwt-middleware.ts +1 -1
  140. package/src/auth/msalConnectionManager.ts +22 -0
  141. package/src/auth/msalTokenCredential.ts +4 -0
  142. package/src/auth/msalTokenProvider.ts +138 -107
  143. package/src/baseAdapter.ts +1 -1
  144. package/src/cloudAdapter.ts +239 -184
  145. package/src/connector-client/connectorClient.ts +169 -126
  146. package/src/errorHelper.ts +124 -0
  147. package/src/middlewareSet.ts +1 -1
  148. package/src/oauth/userTokenClient.ts +70 -46
  149. package/src/observability/index.ts +5 -0
  150. package/src/observability/metrics.ts +103 -0
  151. package/src/observability/traces.ts +988 -0
  152. package/src/state/agentState.ts +1 -1
  153. package/src/storage/fileStorage.ts +36 -26
  154. package/src/storage/memoryStorage.ts +40 -29
  155. package/src/transcript/fileTranscriptLogger.ts +1 -1
  156. package/src/transcript/transcriptLoggerMiddleware.ts +1 -1
  157. package/src/turnContext.ts +47 -41
@@ -0,0 +1,988 @@
1
+ // Copyright (c) Microsoft Corporation. All rights reserved.
2
+ // Licensed under the MIT License.
3
+
4
+ import { SpanNames, trace } from '@microsoft/agents-telemetry'
5
+ import { HostingMetrics } from './metrics'
6
+ import { Activity, ConversationReference } from '@microsoft/agents-activity'
7
+ import { HandlerStorage } from '../app/auth/handlerStorage'
8
+
9
+ export const AgentApplicationTraceDefinitions = {
10
+ run: trace.define({
11
+ name: SpanNames.AGENTS_APP_RUN,
12
+ record: {
13
+ authorized: false,
14
+ activity: Activity.fromObject({ type: 'unknown' }),
15
+ routeMatched: false,
16
+ },
17
+ end ({ span, record, duration, error }) {
18
+ const { activity } = record
19
+
20
+ const attributes = {
21
+ 'activity.type': activity.type ?? 'unknown',
22
+ 'activity.channel_id': activity.channelId ?? 'unknown'
23
+ }
24
+
25
+ span.setAttributes({
26
+ 'route.authorized': record.authorized,
27
+ 'route.matched': record.routeMatched,
28
+ ...attributes
29
+ })
30
+
31
+ HostingMetrics.turnsTotalCounter.add(1, attributes)
32
+ HostingMetrics.turnDuration.record(duration, attributes)
33
+
34
+ if (error) {
35
+ HostingMetrics.turnsErrorsCounter.add(1, {
36
+ 'error.type': error instanceof Error ? error.constructor.name : typeof error
37
+ })
38
+ }
39
+ }
40
+ }),
41
+ downloadFiles: trace.define({
42
+ name: SpanNames.AGENTS_APP_DOWNLOAD_FILES,
43
+ record: {
44
+ attachmentsCount: 0,
45
+ },
46
+ end ({ span, record }) {
47
+ span.setAttribute('agents.attachments.count', record.attachmentsCount ?? 0)
48
+ }
49
+ }),
50
+ beforeTurn: trace.define({
51
+ name: SpanNames.AGENTS_APP_BEFORE_TURN,
52
+ record: {},
53
+ end () {}
54
+ }),
55
+ routeHandler: trace.define({
56
+ name: SpanNames.AGENTS_APP_ROUTE_HANDLER,
57
+ record: {
58
+ isInvoke: false,
59
+ isAgentic: false,
60
+ },
61
+ end ({ span, record }) {
62
+ span.setAttributes({
63
+ 'route.is_invoke': record.isInvoke,
64
+ 'route.is_agentic': record.isAgentic,
65
+ })
66
+ }
67
+ }),
68
+ afterTurn: trace.define({
69
+ name: SpanNames.AGENTS_APP_AFTER_TURN,
70
+ record: {},
71
+ end () {}
72
+ }),
73
+ }
74
+
75
+ export const TurnContextTraceDefinitions = {
76
+ sendActivities: trace.define({
77
+ name: SpanNames.TURN_SEND_ACTIVITIES,
78
+ record: {
79
+ activityCount: 0
80
+ },
81
+ actions: ({ span }) => ({
82
+ recordActivity (activity: Activity) {
83
+ span.addEvent('activity.sent', {
84
+ 'activity.id': activity.id ?? 'unknown',
85
+ 'activity.type': activity.type ?? 'unknown',
86
+ 'activity.delivery_mode': activity.deliveryMode ?? 'unknown'
87
+ })
88
+ }
89
+ }),
90
+ end ({ span, record }) {
91
+ span.setAttribute('activity.count', record.activityCount)
92
+ }
93
+ }),
94
+ }
95
+
96
+ export const AgentClientTraceDefinitions = {
97
+ postActivity: trace.define({
98
+ name: SpanNames.AGENT_CLIENT_POST_ACTIVITY,
99
+ record: {
100
+ endpoint: '',
101
+ clientId: '',
102
+ httpStatusCode: 'unknown',
103
+ },
104
+ end ({ span, record, duration }) {
105
+ const attributes = {
106
+ 'target.endpoint': record.endpoint ?? 'unknown',
107
+ 'target.client_id': record.clientId ?? 'unknown',
108
+ 'http.status_code': record.httpStatusCode ?? 'unknown'
109
+ }
110
+
111
+ span.setAttributes(attributes)
112
+
113
+ HostingMetrics.agentClientRequestsCounter.add(1, attributes)
114
+ HostingMetrics.agentClientRequestDuration.record(duration, attributes)
115
+ }
116
+ }),
117
+ }
118
+
119
+ export const AdapterTraceDefinitions = {
120
+ createConnectorClient: trace.define({
121
+ name: SpanNames.ADAPTER_CREATE_CONNECTOR_CLIENT,
122
+ record: {
123
+ serviceUrl: '',
124
+ scope: '',
125
+ activityIsAgentic: false,
126
+ },
127
+ end ({ span, record }) {
128
+ span.setAttributes({
129
+ service_url: record.serviceUrl ?? 'unknown',
130
+ 'auth.scope': record.scope ?? 'unknown',
131
+ })
132
+
133
+ if (record.activityIsAgentic !== undefined) {
134
+ span.setAttribute('activity.is_agentic', record.activityIsAgentic)
135
+ }
136
+ }
137
+ }),
138
+ createUserTokenClient: trace.define({
139
+ name: SpanNames.ADAPTER_CREATE_USER_TOKEN_CLIENT,
140
+ record: {
141
+ tokenServiceEndpoint: '',
142
+ authScope: '',
143
+ },
144
+ end ({ span, record }) {
145
+ span.setAttributes({
146
+ 'token.service.endpoint': record.tokenServiceEndpoint ?? 'unknown',
147
+ 'auth.scope': record.authScope ?? 'unknown',
148
+ })
149
+ }
150
+ }),
151
+ sendActivities: trace.define({
152
+ name: SpanNames.ADAPTER_SEND_ACTIVITIES,
153
+ record: {
154
+ activityCount: 0,
155
+ },
156
+ actions: ({ span }) => ({
157
+ recordActivity (activity: Activity) {
158
+ span.addEvent('activity.sent', {
159
+ 'activity.id': activity.id ?? 'unknown',
160
+ 'activity.type': activity.type ?? 'unknown',
161
+ 'activity.channel_id': activity.channelId ?? 'unknown',
162
+ 'activity.conversation_id': activity.conversation?.id ?? 'unknown',
163
+ })
164
+ HostingMetrics.activitiesSentCounter.add(1, {
165
+ 'activity.type': activity.type ?? 'unknown',
166
+ 'activity.channel_id': activity.channelId ?? 'unknown',
167
+ 'activity.conversation_id': activity.conversation?.id ?? 'unknown',
168
+ })
169
+ }
170
+ }),
171
+ end ({ span, record }) {
172
+ span.setAttributes({
173
+ 'activity.count': record.activityCount,
174
+ })
175
+ }
176
+ }),
177
+ process: trace.define({
178
+ name: SpanNames.ADAPTER_PROCESS,
179
+ record: {
180
+ activity: Activity.fromObject({ type: 'unknown' }),
181
+ },
182
+ end ({ span, record, duration }) {
183
+ const { activity } = record
184
+ const attributes = {
185
+ 'activity.type': activity.type ?? 'unknown',
186
+ 'activity.channel_id': activity.channelId ?? 'unknown',
187
+ 'activity.delivery_mode': activity.deliveryMode ?? 'unknown',
188
+ 'activity.conversation_id': activity.conversation?.id ?? 'unknown',
189
+ }
190
+
191
+ span.setAttributes({
192
+ ...attributes,
193
+ 'activity.is_agentic': activity.isAgenticRequest(),
194
+ })
195
+
196
+ HostingMetrics.adapterProcessDuration.record(duration, {
197
+ 'activity.type': attributes['activity.type'],
198
+ })
199
+
200
+ HostingMetrics.activitiesReceivedCounter.add(1, {
201
+ 'activity.type': attributes['activity.type'],
202
+ 'activity.channel_id': attributes['activity.channel_id']
203
+ })
204
+ }
205
+ }),
206
+ updateActivity: trace.define({
207
+ name: SpanNames.ADAPTER_UPDATE_ACTIVITY,
208
+ record: {
209
+ activity: Activity.fromObject({ type: 'unknown' }),
210
+ },
211
+ end ({ span, record }) {
212
+ span.setAttributes({
213
+ 'activity.id': record.activity.id ?? 'unknown',
214
+ 'activity.conversation_id': record.activity.conversation?.id ?? 'unknown',
215
+ })
216
+
217
+ HostingMetrics.activitiesUpdatedCounter.add(1, {
218
+ 'activity.channel_id': record.activity.channelId ?? 'unknown'
219
+ })
220
+ }
221
+ }),
222
+ deleteActivity: trace.define({
223
+ name: SpanNames.ADAPTER_DELETE_ACTIVITY,
224
+ record: {
225
+ reference: {} as Partial<ConversationReference>,
226
+ },
227
+ end ({ span, record }) {
228
+ const { reference } = record
229
+ span.setAttributes({
230
+ 'activity.id': reference.activityId ?? 'unknown',
231
+ 'activity.conversation_id': reference.conversation?.id ?? 'unknown',
232
+ })
233
+
234
+ HostingMetrics.activitiesDeletedCounter.add(1, {
235
+ 'activity.channel_id': reference.channelId ?? 'unknown'
236
+ })
237
+ }
238
+ }),
239
+ continueConversation: trace.define({
240
+ name: SpanNames.ADAPTER_CONTINUE_CONVERSATION,
241
+ record: {
242
+ botAppId: '',
243
+ conversationId: '',
244
+ isAgentic: false,
245
+ },
246
+ end ({ span, record }) {
247
+ span.setAttributes({
248
+ 'bot.app_id': record.botAppId ?? 'unknown',
249
+ 'activity.conversation_id': record.conversationId ?? 'unknown',
250
+ 'activity.is_agentic': record.isAgentic,
251
+ })
252
+ }
253
+ }),
254
+ }
255
+
256
+ export const ProactiveTraceDefinitions = {
257
+ storeConversation: trace.define({
258
+ name: SpanNames.PROACTIVE_STORE_CONVERSATION,
259
+ record: {
260
+ conversationId: '',
261
+ },
262
+ end ({ span, record }) {
263
+ span.setAttributes({
264
+ 'activity.conversation_id': record.conversationId ?? 'unknown',
265
+ })
266
+ }
267
+ }),
268
+ getConversation: trace.define({
269
+ name: SpanNames.PROACTIVE_GET_CONVERSATION,
270
+ record: {
271
+ conversationId: '',
272
+ found: false,
273
+ },
274
+ end ({ span, record }) {
275
+ span.setAttributes({
276
+ 'activity.conversation_id': record.conversationId ?? 'unknown',
277
+ 'proactive.conversation_found': record.found,
278
+ })
279
+ }
280
+ }),
281
+ getConversationOrThrow: trace.define({
282
+ name: SpanNames.PROACTIVE_GET_CONVERSATION_OR_THROW,
283
+ record: {
284
+ conversationId: '',
285
+ },
286
+ end ({ span, record }) {
287
+ span.setAttributes({
288
+ 'activity.conversation_id': record.conversationId ?? 'unknown',
289
+ })
290
+ }
291
+ }),
292
+ deleteConversation: trace.define({
293
+ name: SpanNames.PROACTIVE_DELETE_CONVERSATION,
294
+ record: {
295
+ conversationId: '',
296
+ },
297
+ end ({ span, record }) {
298
+ span.setAttributes({
299
+ 'activity.conversation_id': record.conversationId ?? 'unknown',
300
+ })
301
+ }
302
+ }),
303
+ sendActivity: trace.define({
304
+ name: SpanNames.PROACTIVE_SEND_ACTIVITY,
305
+ record: {
306
+ conversationId: '',
307
+ channelId: '',
308
+ activityType: '',
309
+ },
310
+ end ({ span, record, duration, error }) {
311
+ const attributes = {
312
+ 'activity.channel_id': record.channelId ?? 'unknown',
313
+ 'activity.type': record.activityType ?? 'unknown'
314
+ }
315
+
316
+ span.setAttributes({
317
+ ...attributes,
318
+ 'activity.conversation_id': record.conversationId ?? 'unknown',
319
+ })
320
+
321
+ const metricsAttributes = {
322
+ ...attributes,
323
+ operation: 'send.activity',
324
+ 'operation.success': error === undefined,
325
+ }
326
+
327
+ HostingMetrics.proactiveOperationCounter.add(1, metricsAttributes)
328
+ HostingMetrics.proactiveOperationDuration.record(duration, metricsAttributes)
329
+ }
330
+ }),
331
+ continueConversation: trace.define({
332
+ name: SpanNames.PROACTIVE_CONTINUE_CONVERSATION,
333
+ record: {
334
+ conversationId: '',
335
+ channelId: '',
336
+ hasAutoSignIn: false,
337
+ },
338
+ end ({ span, record, duration, error }) {
339
+ const attributes = {
340
+ 'activity.channel_id': record.channelId ?? 'unknown',
341
+ 'proactive.has_auto_sign_in': record.hasAutoSignIn,
342
+ }
343
+
344
+ span.setAttributes({
345
+ ...attributes,
346
+ 'activity.conversation_id': record.conversationId ?? 'unknown',
347
+ })
348
+
349
+ const metricsAttributes = {
350
+ ...attributes,
351
+ operation: 'continue.conversation',
352
+ 'operation.success': error === undefined,
353
+ }
354
+
355
+ HostingMetrics.proactiveOperationCounter.add(1, metricsAttributes)
356
+ HostingMetrics.proactiveOperationDuration.record(duration, metricsAttributes)
357
+ }
358
+ }),
359
+ createConversation: trace.define({
360
+ name: SpanNames.PROACTIVE_CREATE_CONVERSATION,
361
+ record: {
362
+ channelId: '',
363
+ membersCount: 0,
364
+ storeConversation: false,
365
+ hasHandler: false,
366
+ },
367
+ end ({ span, record, duration, error }) {
368
+ const attributes = {
369
+ 'activity.channel_id': record.channelId ?? 'unknown',
370
+ 'proactive.store_conversation': record.storeConversation,
371
+ 'proactive.has_handler': record.hasHandler,
372
+ }
373
+
374
+ span.setAttributes({
375
+ ...attributes,
376
+ 'proactive.members_count': record.membersCount,
377
+ })
378
+
379
+ const metricsAttributes = {
380
+ ...attributes,
381
+ operation: 'create.conversation',
382
+ 'operation.success': error === undefined,
383
+ }
384
+
385
+ HostingMetrics.proactiveOperationCounter.add(1, metricsAttributes)
386
+ HostingMetrics.proactiveOperationDuration.record(duration, metricsAttributes)
387
+ }
388
+ }),
389
+ }
390
+
391
+ export const ConnectorClientTraceDefinitions = {
392
+ getConversations: trace.define({
393
+ name: SpanNames.CONNECTOR_GET_CONVERSATIONS,
394
+ record: {
395
+ httpStatusCode: 'unknown',
396
+ },
397
+ end ({ record, duration }) {
398
+ const attributes = {
399
+ operation: 'get.conversations',
400
+ 'http.method': 'GET',
401
+ 'http.status_code': record.httpStatusCode ?? 'unknown'
402
+ }
403
+
404
+ HostingMetrics.connectorRequestsCounter.add(1, attributes)
405
+ HostingMetrics.connectorRequestDuration.record(duration, attributes)
406
+ }
407
+ }),
408
+ getConversationMember: trace.define({
409
+ name: SpanNames.CONNECTOR_GET_CONVERSATION_MEMBER,
410
+ record: {
411
+ httpStatusCode: 'unknown',
412
+ },
413
+ end ({ record, duration }) {
414
+ const attributes = {
415
+ operation: 'get.conversation.member',
416
+ 'http.method': 'GET',
417
+ 'http.status_code': record.httpStatusCode ?? 'unknown'
418
+ }
419
+
420
+ HostingMetrics.connectorRequestsCounter.add(1, attributes)
421
+ HostingMetrics.connectorRequestDuration.record(duration, attributes)
422
+ }
423
+ }),
424
+ createConversation: trace.define({
425
+ name: SpanNames.CONNECTOR_CREATE_CONVERSATION,
426
+ record: {
427
+ httpStatusCode: 'unknown',
428
+ },
429
+ end ({ record, duration }) {
430
+ const attributes = {
431
+ operation: 'create.conversation',
432
+ 'http.method': 'POST',
433
+ 'http.status_code': record.httpStatusCode ?? 'unknown'
434
+ }
435
+
436
+ HostingMetrics.connectorRequestsCounter.add(1, attributes)
437
+ HostingMetrics.connectorRequestDuration.record(duration, attributes)
438
+ }
439
+ }),
440
+ replyToActivity: trace.define({
441
+ name: SpanNames.CONNECTOR_REPLY_TO_ACTIVITY,
442
+ record: {
443
+ conversationId: '',
444
+ activityId: '',
445
+ httpStatusCode: 'unknown',
446
+ },
447
+ end ({ span, record, duration }) {
448
+ span.setAttributes({
449
+ 'activity.conversation_id': record.conversationId ?? 'unknown',
450
+ 'activity.id': record.activityId ?? 'unknown',
451
+ })
452
+
453
+ const attributes = {
454
+ operation: 'reply.to.activity',
455
+ 'http.method': 'POST',
456
+ 'http.status_code': record.httpStatusCode ?? 'unknown'
457
+ }
458
+
459
+ HostingMetrics.connectorRequestsCounter.add(1, attributes)
460
+ HostingMetrics.connectorRequestDuration.record(duration, attributes)
461
+ }
462
+ }),
463
+ sendToConversation: trace.define({
464
+ name: SpanNames.CONNECTOR_SEND_TO_CONVERSATION,
465
+ record: {
466
+ conversationId: '',
467
+ httpStatusCode: 'unknown',
468
+ },
469
+ end ({ span, record, duration }) {
470
+ span.setAttribute('activity.conversation_id', record.conversationId ?? 'unknown')
471
+
472
+ const attributes = {
473
+ operation: 'send.to.conversation',
474
+ 'http.method': 'POST',
475
+ 'http.status_code': record.httpStatusCode ?? 'unknown'
476
+ }
477
+
478
+ HostingMetrics.connectorRequestsCounter.add(1, attributes)
479
+ HostingMetrics.connectorRequestDuration.record(duration, attributes)
480
+ }
481
+ }),
482
+ updateActivity: trace.define({
483
+ name: SpanNames.CONNECTOR_UPDATE_ACTIVITY,
484
+ record: {
485
+ conversationId: '',
486
+ activityId: '',
487
+ httpStatusCode: 'unknown',
488
+ },
489
+ end ({ span, record, duration }) {
490
+ span.setAttributes({
491
+ 'activity.conversation_id': record.conversationId ?? 'unknown',
492
+ 'activity.id': record.activityId ?? 'unknown',
493
+ })
494
+
495
+ const attributes = {
496
+ operation: 'update.activity',
497
+ 'http.method': 'PUT',
498
+ 'http.status_code': record.httpStatusCode ?? 'unknown'
499
+ }
500
+
501
+ HostingMetrics.connectorRequestsCounter.add(1, attributes)
502
+ HostingMetrics.connectorRequestDuration.record(duration, attributes)
503
+ }
504
+ }),
505
+ deleteActivity: trace.define({
506
+ name: SpanNames.CONNECTOR_DELETE_ACTIVITY,
507
+ record: {
508
+ conversationId: '',
509
+ activityId: '',
510
+ httpStatusCode: 'unknown',
511
+ },
512
+ end ({ span, record, duration }) {
513
+ span.setAttributes({
514
+ 'activity.conversation_id': record.conversationId ?? 'unknown',
515
+ 'activity.id': record.activityId ?? 'unknown',
516
+ })
517
+
518
+ const attributes = {
519
+ operation: 'delete.activity',
520
+ 'http.method': 'DELETE',
521
+ 'http.status_code': record.httpStatusCode ?? 'unknown'
522
+ }
523
+
524
+ HostingMetrics.connectorRequestsCounter.add(1, attributes)
525
+ HostingMetrics.connectorRequestDuration.record(duration, attributes)
526
+ }
527
+ }),
528
+ uploadAttachment: trace.define({
529
+ name: SpanNames.CONNECTOR_UPLOAD_ATTACHMENT,
530
+ record: {
531
+ conversationId: '',
532
+ httpStatusCode: 'unknown',
533
+ },
534
+ end ({ span, record, duration }) {
535
+ span.setAttribute('activity.conversation_id', record.conversationId ?? 'unknown')
536
+
537
+ const attributes = {
538
+ operation: 'upload.attachment',
539
+ 'http.method': 'POST',
540
+ 'http.status_code': record.httpStatusCode ?? 'unknown'
541
+ }
542
+
543
+ HostingMetrics.connectorRequestsCounter.add(1, attributes)
544
+ HostingMetrics.connectorRequestDuration.record(duration, attributes)
545
+ }
546
+ }),
547
+ getAttachmentInfo: trace.define({
548
+ name: SpanNames.CONNECTOR_GET_ATTACHMENT_INFO,
549
+ record: {
550
+ attachmentId: '',
551
+ httpStatusCode: 'unknown',
552
+ },
553
+ end ({ span, record, duration }) {
554
+ span.setAttribute('attachment.id', record.attachmentId ?? 'unknown')
555
+
556
+ const attributes = {
557
+ operation: 'get.attachment.info',
558
+ 'http.method': 'GET',
559
+ 'http.status_code': record.httpStatusCode ?? 'unknown'
560
+ }
561
+
562
+ HostingMetrics.connectorRequestsCounter.add(1, attributes)
563
+ HostingMetrics.connectorRequestDuration.record(duration, attributes)
564
+ }
565
+ }),
566
+ getAttachment: trace.define({
567
+ name: SpanNames.CONNECTOR_GET_ATTACHMENT,
568
+ record: {
569
+ attachmentId: '',
570
+ viewId: '',
571
+ httpStatusCode: 'unknown',
572
+ },
573
+ end ({ span, record, duration }) {
574
+ span.setAttributes({
575
+ 'attachment.id': record.attachmentId ?? 'unknown',
576
+ 'view.id': record.viewId ?? 'unknown',
577
+ })
578
+
579
+ const attributes = {
580
+ operation: 'get.attachment',
581
+ 'http.method': 'GET',
582
+ 'http.status_code': record.httpStatusCode ?? 'unknown'
583
+ }
584
+
585
+ HostingMetrics.connectorRequestsCounter.add(1, attributes)
586
+ HostingMetrics.connectorRequestDuration.record(duration, attributes)
587
+ }
588
+ }),
589
+ }
590
+
591
+ export const StorageTraceDefinitions = {
592
+ read: trace.define({
593
+ name: SpanNames.STORAGE_READ,
594
+ record: {
595
+ keyCount: 0,
596
+ },
597
+ end ({ span, record, duration }) {
598
+ const attributes = {
599
+ 'storage.operation': 'read',
600
+ 'storage.key.count': record.keyCount ?? 0
601
+ }
602
+
603
+ span.setAttributes(attributes)
604
+ HostingMetrics.storageOperationDuration.record(duration, attributes)
605
+ }
606
+ }),
607
+ write: trace.define({
608
+ name: SpanNames.STORAGE_WRITE,
609
+ record: {
610
+ keyCount: 0,
611
+ },
612
+ end ({ span, record, duration }) {
613
+ const attributes = {
614
+ 'storage.operation': 'write',
615
+ 'storage.key.count': record.keyCount ?? 0
616
+ }
617
+
618
+ span.setAttributes(attributes)
619
+ HostingMetrics.storageOperationDuration.record(duration, attributes)
620
+ }
621
+ }),
622
+ delete: trace.define({
623
+ name: SpanNames.STORAGE_DELETE,
624
+ record: {
625
+ keyCount: 0,
626
+ },
627
+ end ({ span, record, duration }) {
628
+ const attributes = {
629
+ 'storage.operation': 'delete',
630
+ 'storage.key.count': record.keyCount ?? 0
631
+ }
632
+
633
+ span.setAttributes(attributes)
634
+ HostingMetrics.storageOperationDuration.record(duration, attributes)
635
+ }
636
+ }),
637
+ }
638
+
639
+ export const AuthenticationTraceDefinitions = {
640
+ getAccessToken: trace.define({
641
+ name: SpanNames.AUTHENTICATION_GET_ACCESS_TOKEN,
642
+ record: {
643
+ scope: '',
644
+ method: 'unknown',
645
+ },
646
+ end ({ span, record, duration, error }) {
647
+ const attributes = {
648
+ 'auth.scope': record.scope ?? 'unknown',
649
+ 'auth.method': record.method ?? 'unknown',
650
+ }
651
+
652
+ span.setAttributes(attributes)
653
+
654
+ HostingMetrics.authTokenRequestsCounter.add(1, {
655
+ 'auth.method': attributes['auth.method'],
656
+ 'auth.success': error === undefined
657
+ })
658
+
659
+ HostingMetrics.authTokenDuration.record(duration, {
660
+ 'auth.method': attributes['auth.method']
661
+ })
662
+ }
663
+ }),
664
+ acquireTokenOnBehalfOf: trace.define({
665
+ name: SpanNames.AUTHENTICATION_ACQUIRE_TOKEN_ON_BEHALF_OF,
666
+ record: {
667
+ scopes: [] as string[],
668
+ },
669
+ end ({ span, record, duration, error }) {
670
+ const method = 'obo'
671
+ span.setAttribute('auth.scopes', record.scopes)
672
+
673
+ HostingMetrics.authTokenRequestsCounter.add(1, {
674
+ 'auth.method': method,
675
+ 'auth.success': error === undefined
676
+ })
677
+
678
+ HostingMetrics.authTokenDuration.record(duration, {
679
+ 'auth.method': method
680
+ })
681
+ }
682
+ }),
683
+ getAgenticInstanceToken: trace.define({
684
+ name: SpanNames.AUTHENTICATION_GET_AGENTIC_INSTANCE_TOKEN,
685
+ record: {
686
+ agenticInstanceId: '',
687
+ },
688
+ end ({ span, record, duration, error }) {
689
+ const method = 'agentic_instance'
690
+ span.setAttribute('agentic.instance_id', record.agenticInstanceId ?? 'unknown')
691
+ HostingMetrics.authTokenRequestsCounter.add(1, {
692
+ 'auth.method': method,
693
+ 'auth.success': error === undefined
694
+ })
695
+
696
+ HostingMetrics.authTokenDuration.record(duration, {
697
+ 'auth.method': method
698
+ })
699
+ }
700
+ }),
701
+ getAgenticUserToken: trace.define({
702
+ name: SpanNames.AUTHENTICATION_GET_AGENTIC_USER_TOKEN,
703
+ record: {
704
+ agenticInstanceId: '',
705
+ agenticUserId: '',
706
+ scopes: [] as string[],
707
+ },
708
+ end ({ span, record, duration, error }) {
709
+ const method = 'agentic_user'
710
+ span.setAttributes({
711
+ 'agentic.instance_id': record.agenticInstanceId ?? 'unknown',
712
+ 'agentic.user_id': record.agenticUserId ?? 'unknown',
713
+ 'auth.scopes': record.scopes
714
+ })
715
+ HostingMetrics.authTokenRequestsCounter.add(1, {
716
+ 'auth.method': method,
717
+ 'auth.success': error === undefined
718
+ })
719
+
720
+ HostingMetrics.authTokenDuration.record(duration, {
721
+ 'auth.method': method
722
+ })
723
+ }
724
+ }),
725
+ }
726
+
727
+ export const AuthorizationTraceDefinitions = {
728
+ azureBotToken: trace.define({
729
+ name: SpanNames.AUTHORIZATION_AZURE_BOT_TOKEN,
730
+ record: {
731
+ handlerId: '',
732
+ connectionName: '',
733
+ },
734
+ end ({ span, record }) {
735
+ span.setAttributes({
736
+ 'auth.handler.id': record.handlerId ?? 'unknown',
737
+ 'auth.connection.name': record.connectionName ?? 'unknown',
738
+ })
739
+ }
740
+ }),
741
+ azureBotOBOToken: trace.define({
742
+ name: SpanNames.AUTHORIZATION_AZURE_BOT_OBO_TOKEN,
743
+ record: {
744
+ handlerId: '',
745
+ connectionName: '',
746
+ authScopes: [] as string[],
747
+ },
748
+ end ({ span, record }) {
749
+ span.setAttributes({
750
+ 'auth.handler.id': record.handlerId ?? 'unknown',
751
+ 'auth.connection.name': record.connectionName ?? 'unknown',
752
+ 'auth.scopes': record.authScopes
753
+ })
754
+ }
755
+ }),
756
+ azureBotSignout: trace.define({
757
+ name: SpanNames.AUTHORIZATION_AZURE_BOT_SIGNOUT,
758
+ record: {
759
+ handlerId: '',
760
+ connectionName: '',
761
+ channelId: '',
762
+ },
763
+ end ({ span, record }) {
764
+ span.setAttributes({
765
+ 'auth.handler.id': record.handlerId ?? 'unknown',
766
+ 'auth.connection.name': record.connectionName ?? 'unknown',
767
+ 'activity.channel_id': record.channelId ?? 'unknown',
768
+ })
769
+ }
770
+ }),
771
+ azureBotSignin: trace.define({
772
+ name: SpanNames.AUTHORIZATION_AZURE_BOT_SIGNIN,
773
+ record: {
774
+ handlerId: '',
775
+ status: 'unknown',
776
+ statusReason: '',
777
+ connectionName: '',
778
+ },
779
+ actions: ({ span }) => ({
780
+ async link (storage: HandlerStorage<any>) {
781
+ const active = await storage.read()
782
+ if (!active) {
783
+ return
784
+ }
785
+
786
+ if (active.__link) {
787
+ span.addLink({ context: active.__link })
788
+ }
789
+
790
+ active.__link = span.spanContext()
791
+ await storage.write(active)
792
+ }
793
+ }),
794
+ end ({ span, record }) {
795
+ span.setAttributes({
796
+ 'auth.handler.id': record.handlerId ?? 'unknown',
797
+ 'auth.handler.status': record.status ?? 'unknown',
798
+ 'auth.handler.status.reason': record.statusReason ?? '',
799
+ 'auth.connection.name': record.connectionName ?? 'unknown',
800
+ })
801
+ }
802
+ }),
803
+ agenticToken: trace.define({
804
+ name: SpanNames.AUTHORIZATION_AGENTIC_TOKEN,
805
+ record: {
806
+ handlerId: '',
807
+ connectionName: '',
808
+ authScopes: [] as string[],
809
+ },
810
+ end ({ span, record }) {
811
+ span.setAttributes({
812
+ 'auth.handler.id': record.handlerId ?? 'unknown',
813
+ 'auth.connection.name': record.connectionName ?? 'unknown',
814
+ 'auth.scopes': record.authScopes
815
+ })
816
+ }
817
+ }),
818
+ }
819
+
820
+ export const UserTokenClientTraceDefinitions = {
821
+ getUserToken: trace.define({
822
+ name: SpanNames.USER_TOKEN_CLIENT_GET_USER_TOKEN,
823
+ record: {
824
+ userId: '',
825
+ connectionName: '',
826
+ channelId: '',
827
+ httpStatusCode: 'unknown',
828
+ },
829
+ end ({ span, record, duration }) {
830
+ span.setAttributes({
831
+ 'user.id': record.userId ?? 'unknown',
832
+ 'auth.connection.name': record.connectionName ?? 'unknown',
833
+ 'activity.channel_id': record.channelId ?? 'unknown',
834
+ })
835
+
836
+ const attributes = {
837
+ operation: 'get.user.token',
838
+ 'http.method': 'GET',
839
+ 'http.status_code': record.httpStatusCode ?? 'unknown'
840
+ }
841
+
842
+ HostingMetrics.userTokenClientRequestsCounter.add(1, attributes)
843
+ HostingMetrics.userTokenClientRequestDuration.record(duration, attributes)
844
+ }
845
+ }),
846
+ signOut: trace.define({
847
+ name: SpanNames.USER_TOKEN_CLIENT_SIGN_OUT,
848
+ record: {
849
+ userId: '',
850
+ connectionName: '',
851
+ channelId: '',
852
+ httpStatusCode: 'unknown',
853
+ },
854
+ end ({ span, record, duration }) {
855
+ span.setAttributes({
856
+ 'user.id': record.userId ?? 'unknown',
857
+ 'auth.connection.name': record.connectionName ?? 'unknown',
858
+ 'activity.channel_id': record.channelId ?? 'unknown',
859
+ })
860
+
861
+ const attributes = {
862
+ operation: 'sign.out',
863
+ 'http.method': 'DELETE',
864
+ 'http.status_code': record.httpStatusCode ?? 'unknown'
865
+ }
866
+
867
+ HostingMetrics.userTokenClientRequestsCounter.add(1, attributes)
868
+ HostingMetrics.userTokenClientRequestDuration.record(duration, attributes)
869
+ }
870
+ }),
871
+ getSignInResource: trace.define({
872
+ name: SpanNames.USER_TOKEN_CLIENT_GET_SIGN_IN_RESOURCE,
873
+ record: {
874
+ connectionName: '',
875
+ httpStatusCode: 'unknown',
876
+ },
877
+ end ({ span, record, duration }) {
878
+ span.setAttribute('auth.connection.name', record.connectionName ?? 'unknown')
879
+
880
+ const attributes = {
881
+ operation: 'get.sign.in.resource',
882
+ 'http.method': 'GET',
883
+ 'http.status_code': record.httpStatusCode ?? 'unknown'
884
+ }
885
+
886
+ HostingMetrics.userTokenClientRequestsCounter.add(1, attributes)
887
+ HostingMetrics.userTokenClientRequestDuration.record(duration, attributes)
888
+ }
889
+ }),
890
+ exchangeToken: trace.define({
891
+ name: SpanNames.USER_TOKEN_CLIENT_EXCHANGE_TOKEN,
892
+ record: {
893
+ userId: '',
894
+ connectionName: '',
895
+ channelId: '',
896
+ httpStatusCode: 'unknown',
897
+ },
898
+ end ({ span, record, duration }) {
899
+ span.setAttributes({
900
+ 'user.id': record.userId ?? 'unknown',
901
+ 'auth.connection.name': record.connectionName ?? 'unknown',
902
+ 'activity.channel_id': record.channelId ?? 'unknown',
903
+ })
904
+
905
+ const attributes = {
906
+ operation: 'exchange.token',
907
+ 'http.method': 'POST',
908
+ 'http.status_code': record.httpStatusCode ?? 'unknown'
909
+ }
910
+
911
+ HostingMetrics.userTokenClientRequestsCounter.add(1, attributes)
912
+ HostingMetrics.userTokenClientRequestDuration.record(duration, attributes)
913
+ }
914
+ }),
915
+ getTokenOrSignInResource: trace.define({
916
+ name: SpanNames.USER_TOKEN_CLIENT_GET_TOKEN_OR_SIGN_IN_RESOURCE,
917
+ record: {
918
+ userId: '',
919
+ connectionName: '',
920
+ channelId: '',
921
+ httpStatusCode: 'unknown',
922
+ },
923
+ end ({ span, record, duration }) {
924
+ span.setAttributes({
925
+ 'user.id': record.userId ?? 'unknown',
926
+ 'auth.connection.name': record.connectionName ?? 'unknown',
927
+ 'activity.channel_id': record.channelId ?? 'unknown',
928
+ })
929
+
930
+ const attributes = {
931
+ operation: 'get.token.or.sign.in.resource',
932
+ 'http.method': 'GET',
933
+ 'http.status_code': record.httpStatusCode ?? 'unknown'
934
+ }
935
+
936
+ HostingMetrics.userTokenClientRequestsCounter.add(1, attributes)
937
+ HostingMetrics.userTokenClientRequestDuration.record(duration, attributes)
938
+ }
939
+ }),
940
+ getTokenStatus: trace.define({
941
+ name: SpanNames.USER_TOKEN_CLIENT_GET_TOKEN_STATUS,
942
+ record: {
943
+ userId: '',
944
+ channelId: '',
945
+ httpStatusCode: 'unknown',
946
+ },
947
+ end ({ span, record, duration }) {
948
+ span.setAttributes({
949
+ 'user.id': record.userId ?? 'unknown',
950
+ 'activity.channel_id': record.channelId ?? 'unknown',
951
+ })
952
+
953
+ const attributes = {
954
+ operation: 'get.token.status',
955
+ 'http.method': 'GET',
956
+ 'http.status_code': record.httpStatusCode ?? 'unknown'
957
+ }
958
+
959
+ HostingMetrics.userTokenClientRequestsCounter.add(1, attributes)
960
+ HostingMetrics.userTokenClientRequestDuration.record(duration, attributes)
961
+ }
962
+ }),
963
+ getAadTokens: trace.define({
964
+ name: SpanNames.USER_TOKEN_CLIENT_GET_AAD_TOKENS,
965
+ record: {
966
+ userId: '',
967
+ connectionName: '',
968
+ channelId: '',
969
+ httpStatusCode: 'unknown',
970
+ },
971
+ end ({ span, record, duration }) {
972
+ span.setAttributes({
973
+ 'user.id': record.userId ?? 'unknown',
974
+ 'auth.connection.name': record.connectionName ?? 'unknown',
975
+ 'activity.channel_id': record.channelId ?? 'unknown',
976
+ })
977
+
978
+ const attributes = {
979
+ operation: 'get.aad.tokens',
980
+ 'http.method': 'POST',
981
+ 'http.status_code': record.httpStatusCode ?? 'unknown'
982
+ }
983
+
984
+ HostingMetrics.userTokenClientRequestsCounter.add(1, attributes)
985
+ HostingMetrics.userTokenClientRequestDuration.record(duration, attributes)
986
+ }
987
+ }),
988
+ }