@microsoft/agents-hosting 0.4.3 → 0.5.4-ga4d0401645

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 (107) hide show
  1. package/dist/src/activityWireCompat.js +1 -1
  2. package/dist/src/activityWireCompat.js.map +1 -1
  3. package/dist/src/app/adaptiveCards/activityValueParsers.d.ts +115 -0
  4. package/dist/src/app/adaptiveCards/activityValueParsers.js +224 -0
  5. package/dist/src/app/adaptiveCards/activityValueParsers.js.map +1 -0
  6. package/dist/src/app/adaptiveCards/adaptiveCardActionExecuteResponseType.d.ts +21 -0
  7. package/dist/src/app/adaptiveCards/adaptiveCardActionExecuteResponseType.js +26 -0
  8. package/dist/src/app/adaptiveCards/adaptiveCardActionExecuteResponseType.js.map +1 -0
  9. package/dist/src/app/adaptiveCards/adaptiveCardsActions.d.ts +57 -0
  10. package/dist/src/app/adaptiveCards/adaptiveCardsActions.js +272 -0
  11. package/dist/src/app/adaptiveCards/adaptiveCardsActions.js.map +1 -0
  12. package/dist/src/app/adaptiveCards/adaptiveCardsOptions.d.ts +20 -0
  13. package/dist/src/app/adaptiveCards/adaptiveCardsOptions.js +7 -0
  14. package/dist/src/app/adaptiveCards/adaptiveCardsOptions.js.map +1 -0
  15. package/dist/src/app/adaptiveCards/adaptiveCardsSearchParams.d.ts +31 -0
  16. package/dist/src/app/adaptiveCards/adaptiveCardsSearchParams.js +16 -0
  17. package/dist/src/app/adaptiveCards/adaptiveCardsSearchParams.js.map +1 -0
  18. package/dist/src/app/adaptiveCards/index.d.ts +3 -0
  19. package/dist/src/app/adaptiveCards/index.js +20 -0
  20. package/dist/src/app/adaptiveCards/index.js.map +1 -0
  21. package/dist/src/app/adaptiveCards/query.d.ts +22 -0
  22. package/dist/src/app/adaptiveCards/query.js +7 -0
  23. package/dist/src/app/adaptiveCards/query.js.map +1 -0
  24. package/dist/src/app/agentApplication.d.ts +56 -8
  25. package/dist/src/app/agentApplication.js +88 -11
  26. package/dist/src/app/agentApplication.js.map +1 -1
  27. package/dist/src/app/agentApplicationBuilder.d.ts +2 -2
  28. package/dist/src/app/agentApplicationBuilder.js.map +1 -1
  29. package/dist/src/app/agentApplicationOptions.d.ts +17 -2
  30. package/dist/src/app/appRoute.d.ts +5 -0
  31. package/dist/src/app/attachmentDownloader.js +1 -0
  32. package/dist/src/app/attachmentDownloader.js.map +1 -1
  33. package/dist/src/app/extensions.d.ts +9 -0
  34. package/dist/src/app/extensions.js +16 -0
  35. package/dist/src/app/extensions.js.map +1 -0
  36. package/dist/src/app/index.d.ts +2 -0
  37. package/dist/src/app/index.js +2 -0
  38. package/dist/src/app/index.js.map +1 -1
  39. package/dist/src/app/streaming/AIEntity.d.ts +36 -0
  40. package/dist/src/app/streaming/AIEntity.js +7 -0
  41. package/dist/src/app/streaming/AIEntity.js.map +1 -0
  42. package/dist/src/app/streaming/actionCall.d.ts +33 -0
  43. package/dist/src/app/streaming/actionCall.js +7 -0
  44. package/dist/src/app/streaming/actionCall.js.map +1 -0
  45. package/dist/src/app/streaming/clientCitation.d.ts +68 -0
  46. package/dist/src/app/streaming/clientCitation.js +10 -0
  47. package/dist/src/app/streaming/clientCitation.js.map +1 -0
  48. package/dist/src/app/streaming/message.d.ts +106 -0
  49. package/dist/src/app/streaming/message.js +7 -0
  50. package/dist/src/app/streaming/message.js.map +1 -0
  51. package/dist/src/app/streaming/sensitivityUsageInfo.d.ts +40 -0
  52. package/dist/src/app/streaming/sensitivityUsageInfo.js +3 -0
  53. package/dist/src/app/streaming/sensitivityUsageInfo.js.map +1 -0
  54. package/dist/src/app/streaming/streamingResponse.d.ts +141 -0
  55. package/dist/src/app/streaming/streamingResponse.js +331 -0
  56. package/dist/src/app/streaming/streamingResponse.js.map +1 -0
  57. package/dist/src/app/streaming/utilities.d.ts +31 -0
  58. package/dist/src/app/streaming/utilities.js +101 -0
  59. package/dist/src/app/streaming/utilities.js.map +1 -0
  60. package/dist/src/auth/jwt-middleware.js +1 -0
  61. package/dist/src/auth/jwt-middleware.js.map +1 -1
  62. package/dist/src/baseAdapter.js +1 -1
  63. package/dist/src/baseAdapter.js.map +1 -1
  64. package/dist/src/cards/adaptiveCard.d.ts +15 -0
  65. package/dist/src/cards/adaptiveCard.js +7 -0
  66. package/dist/src/cards/adaptiveCard.js.map +1 -0
  67. package/dist/src/cards/index.d.ts +1 -0
  68. package/dist/src/cards/index.js +1 -0
  69. package/dist/src/cards/index.js.map +1 -1
  70. package/dist/src/cloudAdapter.js +6 -7
  71. package/dist/src/cloudAdapter.js.map +1 -1
  72. package/dist/src/connector-client/connectorClient.d.ts +6 -4
  73. package/dist/src/connector-client/connectorClient.js +34 -17
  74. package/dist/src/connector-client/connectorClient.js.map +1 -1
  75. package/dist/src/turnContext.d.ts +3 -0
  76. package/dist/src/turnContext.js +5 -0
  77. package/dist/src/turnContext.js.map +1 -1
  78. package/package.json +2 -2
  79. package/src/activityWireCompat.ts +1 -1
  80. package/src/app/adaptiveCards/activityValueParsers.ts +249 -0
  81. package/src/app/adaptiveCards/adaptiveCardActionExecuteResponseType.ts +24 -0
  82. package/src/app/adaptiveCards/adaptiveCardsActions.ts +320 -0
  83. package/src/app/adaptiveCards/adaptiveCardsOptions.ts +23 -0
  84. package/src/app/adaptiveCards/adaptiveCardsSearchParams.ts +28 -0
  85. package/src/app/adaptiveCards/index.ts +3 -0
  86. package/src/app/adaptiveCards/query.ts +25 -0
  87. package/src/app/agentApplication.ts +109 -29
  88. package/src/app/agentApplicationBuilder.ts +2 -2
  89. package/src/app/agentApplicationOptions.ts +20 -2
  90. package/src/app/appRoute.ts +6 -0
  91. package/src/app/attachmentDownloader.ts +1 -0
  92. package/src/app/extensions.ts +19 -0
  93. package/src/app/index.ts +2 -0
  94. package/src/app/streaming/AIEntity.ts +44 -0
  95. package/src/app/streaming/actionCall.ts +37 -0
  96. package/src/app/streaming/clientCitation.ts +102 -0
  97. package/src/app/streaming/message.ts +125 -0
  98. package/src/app/streaming/sensitivityUsageInfo.ts +48 -0
  99. package/src/app/streaming/streamingResponse.ts +406 -0
  100. package/src/app/streaming/utilities.ts +108 -0
  101. package/src/auth/jwt-middleware.ts +1 -1
  102. package/src/baseAdapter.ts +2 -2
  103. package/src/cards/adaptiveCard.ts +16 -0
  104. package/src/cards/index.ts +1 -0
  105. package/src/cloudAdapter.ts +5 -7
  106. package/src/connector-client/connectorClient.ts +38 -19
  107. package/src/turnContext.ts +7 -1
@@ -0,0 +1,249 @@
1
+ /**
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+
6
+ import { z } from 'zod'
7
+ import { activityZodSchema, AdaptiveCardInvokeAction, adaptiveCardInvokeActionZodSchema } from '@microsoft/agents-activity'
8
+ // import { MessagingExtensionQuery, messagingExtensionQueryZodSchema } from '../messageExtension/messagingExtensionQuery'
9
+ import { adaptiveCardsSearchParamsZodSchema } from './adaptiveCardsSearchParams'
10
+
11
+ /**
12
+ * Parses the given value as a value action.
13
+ *
14
+ * @param {unknown} value - The value to parse.
15
+ * @returns {string} - The parsed value action.
16
+ */
17
+ export function parseValueAction (value: unknown): string {
18
+ const valueActionZodSchema = z.object({
19
+ action: z.string().min(1)
20
+ })
21
+ valueActionZodSchema.passthrough().parse(value)
22
+ return value as string
23
+ }
24
+
25
+ /**
26
+ * Parses the given value as a value action name.
27
+ *
28
+ * @param {unknown} value - The value to parse.
29
+ * @returns {string} - The parsed value action name.
30
+ */
31
+ export function parseValueActionName (value: unknown): string {
32
+ const valueActionNameZodSchema = z.object({
33
+ actionName: z.string().min(1),
34
+ })
35
+ valueActionNameZodSchema.passthrough().parse(value)
36
+ return value as string
37
+ }
38
+
39
+ /**
40
+ * Parses the given value as a value continuation.
41
+ *
42
+ * @param {unknown} value - The value to parse.
43
+ * @returns {string} - The parsed value continuation.
44
+ */
45
+ export function parseValueContinuation (value: unknown): string {
46
+ const valueContinuationZodSchema = z.object({
47
+ continuation: z.string().min(1)
48
+ })
49
+ valueContinuationZodSchema.passthrough().parse(value)
50
+ return value as string
51
+ }
52
+
53
+ export interface ValueAction {
54
+ action: {
55
+ type: string;
56
+ verb: string;
57
+ }
58
+ }
59
+
60
+ /**
61
+ * Parses the given value as a value action execute selector.
62
+ *
63
+ * @param {unknown} value - The value to parse.
64
+ * @returns {object} - The parsed value action execute selector.
65
+ */
66
+ export function parseValueActionExecuteSelector (value: unknown): ValueAction | undefined {
67
+ if (value === undefined || value === null) {
68
+ return undefined
69
+ }
70
+ const actionZodSchema = z.object({
71
+ type: z.string().min(1),
72
+ verb: z.string().min(1)
73
+ })
74
+ const actionValueExecuteSelector = z.object({
75
+ action: actionZodSchema
76
+ })
77
+ const safeParsedValue = actionValueExecuteSelector.passthrough().safeParse(value)
78
+ if (!safeParsedValue.success) {
79
+ throw new Error(`Invalid action value: ${safeParsedValue.error}`)
80
+ }
81
+ const parsedValue = safeParsedValue.data
82
+ return {
83
+ action: {
84
+ type: parsedValue.action.type,
85
+ verb: parsedValue.action.verb
86
+ }
87
+ }
88
+ }
89
+
90
+ /**
91
+ * Parses the given value as a dataset.
92
+ *
93
+ * @param {unknown} value - The value to parse.
94
+ * @returns {object} - The parsed dataset.
95
+ */
96
+ export function parseValueDataset (value: unknown): {
97
+ dataset: string;
98
+ } | undefined {
99
+ if (value === undefined || value === null) {
100
+ return undefined
101
+ }
102
+ const datasetZodSchema = z.object({
103
+ dataset: z.string().min(1)
104
+ })
105
+ const parsedValue = datasetZodSchema.passthrough().parse(value)
106
+ return {
107
+ dataset: parsedValue.dataset
108
+ }
109
+ }
110
+
111
+ /**
112
+ * Parses the given value as action feedback loop data.
113
+ *
114
+ * @param {unknown} value - The value to parse.
115
+ * @returns {object} - The parsed action feedback loop data.
116
+ */
117
+ export function parseValueActionFeedbackLoopData (value: unknown): {
118
+ actionValue: {
119
+ reaction: 'like' | 'dislike';
120
+ feedback: string | Record<string, any>;
121
+ }
122
+ } {
123
+ const feedbackLoopDataActionValueZodSchema = z.object({
124
+ actionValue: z.object({
125
+ reaction: z.union([z.literal('like'), z.literal('dislike')]),
126
+ feedback: z.union([z.string().min(1), z.record(z.string(), z.any())])
127
+ })
128
+ })
129
+ const parsedValue = feedbackLoopDataActionValueZodSchema.passthrough().parse(value)
130
+ return {
131
+ actionValue: {
132
+ reaction: parsedValue.actionValue.reaction,
133
+ feedback: parsedValue.actionValue.feedback
134
+ }
135
+ }
136
+ }
137
+
138
+ /**
139
+ * Parses the given value as an adaptive card invoke action.
140
+ *
141
+ * @param {unknown} value - The value to parse.
142
+ * @returns {AdaptiveCardInvokeAction} - The parsed adaptive card invoke action.
143
+ */
144
+ export function parseAdaptiveCardInvokeAction (value: unknown): AdaptiveCardInvokeAction {
145
+ adaptiveCardInvokeActionZodSchema.passthrough().parse(value)
146
+ return value as AdaptiveCardInvokeAction
147
+ }
148
+
149
+ /**
150
+ * Parses the given value as a search query.
151
+ *
152
+ * @param {unknown} value - The value to parse.
153
+ * @returns {object} - The parsed search query.
154
+ */
155
+ export function parseValueSearchQuery (value: unknown): {
156
+ queryOptions: {
157
+ top: number;
158
+ skip: number;
159
+ };
160
+ queryText: string;
161
+ dataset: string;
162
+ } {
163
+ const queryOptionsZodSchema = z.object({
164
+ top: z.number(),
165
+ skip: z.number(),
166
+ })
167
+ const searchValueZodSchema = adaptiveCardsSearchParamsZodSchema.extend({
168
+ queryOptions: queryOptionsZodSchema
169
+ })
170
+ const validSearchValue = searchValueZodSchema.passthrough().parse(value)
171
+ return {
172
+ queryOptions: {
173
+ top: validSearchValue.queryOptions.top,
174
+ skip: validSearchValue.queryOptions.skip
175
+ },
176
+ queryText: validSearchValue.queryText,
177
+ dataset: validSearchValue.dataset
178
+ }
179
+ }
180
+
181
+ /**
182
+ * Parses the given value as a query.
183
+ *
184
+ * @param {unknown} value - The value to parse.
185
+ * @returns {object} - The parsed query.
186
+ */
187
+ export function parseValueQuery (value: unknown): {
188
+ url: string;
189
+ } {
190
+ const urlZodSchema = z.object({
191
+ url: z.string().min(1)
192
+ })
193
+ const parsedValue = urlZodSchema.passthrough().parse(value)
194
+ return {
195
+ url: parsedValue.url
196
+ }
197
+ }
198
+
199
+ /**
200
+ * Parses the given value as an activity message preview action.
201
+ *
202
+ * @param {unknown} value - The value to parse.
203
+ * @returns {object} - The parsed message preview action.
204
+ */
205
+ export function parseValueAgentMessagePreviewAction (value: unknown): {
206
+ botMessagePreviewAction: string;
207
+ } {
208
+ const botMessagePreviewActionZodSchema = z.object({
209
+ botMessagePreviewAction: z.string().min(1)
210
+ })
211
+ const parsedValue = botMessagePreviewActionZodSchema.passthrough().parse(value)
212
+ return {
213
+ botMessagePreviewAction: parsedValue.botMessagePreviewAction
214
+ }
215
+ }
216
+
217
+ /**
218
+ * Parses the given value as an activity preview.
219
+ *
220
+ * @param {unknown} value - The value to parse.
221
+ * @returns {object} - The parsed activity preview.
222
+ */
223
+ export function parseValueAgentActivityPreview (value: unknown): object {
224
+ const botActivityPreviewZodSchema = z.object({
225
+ botActivityPreview: z.array(activityZodSchema.partial())
226
+ })
227
+ const parsedValue = botActivityPreviewZodSchema.passthrough().parse(value)
228
+ return {
229
+ botActivityPreview: parsedValue.botActivityPreview
230
+ }
231
+ }
232
+
233
+ /**
234
+ * Parses the given value as a command ID.
235
+ *
236
+ * @param {unknown} value - The value to parse.
237
+ * @returns {object} - The parsed command ID.
238
+ */
239
+ export function parseValueCommandId (value: unknown): {
240
+ commandId: string;
241
+ } {
242
+ const commandIdZodSchema = z.object({
243
+ commandId: z.string().min(1)
244
+ })
245
+ const parsedValue = commandIdZodSchema.passthrough().parse(value)
246
+ return {
247
+ commandId: parsedValue.commandId
248
+ }
249
+ }
@@ -0,0 +1,24 @@
1
+ /**
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+
6
+ /**
7
+ * Defines the types of responses that can be returned after executing an Adaptive Card action.
8
+ */
9
+ export enum AdaptiveCardActionExecuteResponseType {
10
+ /**
11
+ * Replaces the Adaptive Card only for the user who interacted with it.
12
+ */
13
+ REPLACE_FOR_INTERACTOR,
14
+
15
+ /**
16
+ * Replaces the Adaptive Card for all users.
17
+ */
18
+ REPLACE_FOR_ALL,
19
+
20
+ /**
21
+ * Sends a new message containing an Adaptive Card to all users.
22
+ */
23
+ NEW_MESSAGE_FOR_ALL
24
+ }
@@ -0,0 +1,320 @@
1
+ /**
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+
6
+ import { Activity, ActivityTypes } from '@microsoft/agents-activity'
7
+ import { AdaptiveCardInvokeResponse, AgentApplication, CardFactory, INVOKE_RESPONSE_KEY, InvokeResponse, MessageFactory, RouteSelector, TurnContext, TurnState } from '../../'
8
+ import { AdaptiveCardActionExecuteResponseType } from './adaptiveCardActionExecuteResponseType'
9
+ import { parseAdaptiveCardInvokeAction, parseValueActionExecuteSelector, parseValueDataset, parseValueSearchQuery } from './activityValueParsers'
10
+ import { AdaptiveCardsSearchParams } from './adaptiveCardsSearchParams'
11
+ import { AdaptiveCard } from '../../cards/adaptiveCard'
12
+ import { Query } from './query'
13
+
14
+ export const ACTION_INVOKE_NAME = 'adaptiveCard/action'
15
+ const ACTION_EXECUTE_TYPE = 'Action.Execute'
16
+ const DEFAULT_ACTION_SUBMIT_FILTER = 'verb'
17
+ const SEARCH_INVOKE_NAME = 'application/search'
18
+
19
+ enum AdaptiveCardInvokeResponseType {
20
+ /**
21
+ * Indicates a response containing an Adaptive Card.
22
+ */
23
+ ADAPTIVE = 'application/vnd.microsoft.card.adaptive',
24
+
25
+ /**
26
+ * Indicates a response containing a message activity.
27
+ */
28
+ MESSAGE = 'application/vnd.microsoft.activity.message',
29
+
30
+ /**
31
+ * Indicates a response containing a search result.
32
+ */
33
+ SEARCH = 'application/vnd.microsoft.search.searchResponse'
34
+ }
35
+
36
+ export interface AdaptiveCardSearchResult {
37
+ /**
38
+ * The title of the search result.
39
+ */
40
+ title: string;
41
+
42
+ /**
43
+ * The value associated with the search result.
44
+ */
45
+ value: string;
46
+ }
47
+
48
+ /**
49
+ * A class to handle Adaptive Card actions such as executing actions, submitting actions, and performing searches.
50
+ * @template TState - The type of the TurnState used in the application.
51
+ */
52
+ export class AdaptiveCardsActions<TState extends TurnState> {
53
+ /**
54
+ * The Teams application instance associated with this class.
55
+ */
56
+ private readonly _app: AgentApplication<TState>
57
+
58
+ /**
59
+ * Constructs an instance of AdaptiveCardsActions.
60
+ * @param app - The Teams application instance.
61
+ */
62
+ public constructor (app: AgentApplication<TState>) {
63
+ this._app = app
64
+ }
65
+
66
+ /**
67
+ * Registers a handler for the Action.Execute event.
68
+ * @template TData - The type of the data passed to the handler.
69
+ * @param verb - A string, RegExp, RouteSelector, or an array of these to match the action verb.
70
+ * @param handler - A function to handle the action execution.
71
+ * @returns The Teams application instance.
72
+ */
73
+ public actionExecute<TData = Record<string, any>>(
74
+ verb: string | RegExp | RouteSelector | (string | RegExp | RouteSelector)[],
75
+ handler: (context: TurnContext, state: TState, data: TData) => Promise<AdaptiveCard | string>
76
+ ): AgentApplication<TState> {
77
+ let actionExecuteResponseType = this._app.options.adaptiveCardsOptions?.actionExecuteResponseType ?? AdaptiveCardActionExecuteResponseType.REPLACE_FOR_INTERACTOR;
78
+ (Array.isArray(verb) ? verb : [verb]).forEach((v) => {
79
+ const selector = createActionExecuteSelector(v)
80
+ this._app.addRoute(
81
+ selector,
82
+ async (context, state) => {
83
+ const a = context?.activity
84
+ const invokeAction = parseValueActionExecuteSelector(a.value)
85
+ if (
86
+ a?.type !== ActivityTypes.Invoke ||
87
+ a?.name !== ACTION_INVOKE_NAME ||
88
+ (invokeAction?.action.type !== ACTION_EXECUTE_TYPE)
89
+ ) {
90
+ throw new Error(`Unexpected AdaptiveCards.actionExecute() triggered for activity type: ${invokeAction?.action.type}`
91
+ )
92
+ }
93
+
94
+ if (invokeAction.action.verb !== v) {
95
+ // TODO: add logger to this class
96
+ console.log(`AdaptiveCards.actionExecute() triggered for verb: ${invokeAction.action.verb} does not match expected verb: ${v}`)
97
+ }
98
+
99
+ // TODO: review any, and check verb
100
+ const result = await handler(context, state, ((a.value as any).action as TData) ?? {} as TData)
101
+ if (!context.turnState.get(INVOKE_RESPONSE_KEY)) {
102
+ let response: AdaptiveCardInvokeResponse
103
+ if (typeof result === 'string') {
104
+ response = {
105
+ statusCode: 200,
106
+ type: AdaptiveCardInvokeResponseType.MESSAGE,
107
+ value: result as any
108
+ }
109
+ await sendInvokeResponse(context, response)
110
+ } else {
111
+ if (
112
+ result.refresh &&
113
+ actionExecuteResponseType !== AdaptiveCardActionExecuteResponseType.NEW_MESSAGE_FOR_ALL
114
+ ) {
115
+ actionExecuteResponseType = AdaptiveCardActionExecuteResponseType.REPLACE_FOR_ALL
116
+ }
117
+
118
+ const activity = MessageFactory.attachment(CardFactory.adaptiveCard(result))
119
+ response = {
120
+ statusCode: 200,
121
+ type: AdaptiveCardInvokeResponseType.ADAPTIVE,
122
+ value: result
123
+ }
124
+ if (
125
+ actionExecuteResponseType === AdaptiveCardActionExecuteResponseType.NEW_MESSAGE_FOR_ALL
126
+ ) {
127
+ await sendInvokeResponse(context, {
128
+ statusCode: 200,
129
+ type: AdaptiveCardInvokeResponseType.MESSAGE,
130
+ value: 'Your response was sent to the app' as any
131
+ })
132
+ await context.sendActivity(activity)
133
+ } else if (
134
+ actionExecuteResponseType === AdaptiveCardActionExecuteResponseType.REPLACE_FOR_ALL
135
+ ) {
136
+ activity.id = context.activity.replyToId
137
+ await context.updateActivity(activity)
138
+ await sendInvokeResponse(context, response)
139
+ } else {
140
+ await sendInvokeResponse(context, response)
141
+ }
142
+ }
143
+ }
144
+ },
145
+ true
146
+ )
147
+ })
148
+ return this._app
149
+ }
150
+
151
+ /**
152
+ * Registers a handler for the Action.Submit event.
153
+ * @template TData - The type of the data passed to the handler.
154
+ * @param verb - A string, RegExp, RouteSelector, or an array of these to match the action verb.
155
+ * @param handler - A function to handle the action submission.
156
+ * @returns The Teams application instance.
157
+ */
158
+ public actionSubmit<TData = Record<string, any>>(
159
+ verb: string | RegExp | RouteSelector | (string | RegExp | RouteSelector)[],
160
+ handler: (context: TurnContext, state: TState, data: TData) => Promise<void>
161
+ ): AgentApplication<TState> {
162
+ const filter = this._app.options.adaptiveCardsOptions?.actionSubmitFilter ?? DEFAULT_ACTION_SUBMIT_FILTER;
163
+ (Array.isArray(verb) ? verb : [verb]).forEach((v) => {
164
+ const selector = createActionSubmitSelector(v, filter)
165
+ this._app.addRoute(selector, async (context, state) => {
166
+ const a = context?.activity
167
+ if (a?.type !== ActivityTypes.Message || a?.text || typeof a?.value !== 'object') {
168
+ throw new Error(`Unexpected AdaptiveCards.actionSubmit() triggered for activity type: ${a?.type}`)
169
+ }
170
+
171
+ await handler(context, state as TState, (parseAdaptiveCardInvokeAction(a.value)) as TData ?? {} as TData)
172
+ })
173
+ })
174
+ return this._app
175
+ }
176
+
177
+ /**
178
+ * Registers a handler for the search event.
179
+ * @param dataset - A string, RegExp, RouteSelector, or an array of these to match the dataset.
180
+ * @param handler - A function to handle the search query.
181
+ * @returns The Teams application instance.
182
+ */
183
+ public search (
184
+ dataset: string | RegExp | RouteSelector | (string | RegExp | RouteSelector)[],
185
+ handler: (
186
+ context: TurnContext,
187
+ state: TState,
188
+ query: Query<AdaptiveCardsSearchParams>
189
+ ) => Promise<AdaptiveCardSearchResult[]>
190
+ ): AgentApplication<TState> {
191
+ (Array.isArray(dataset) ? dataset : [dataset]).forEach((ds) => {
192
+ const selector = createSearchSelector(ds)
193
+ this._app.addRoute(
194
+ selector,
195
+ async (context, state) => {
196
+ const a = context?.activity
197
+ if (a?.type !== 'invoke' || a?.name !== SEARCH_INVOKE_NAME) {
198
+ throw new Error(`Unexpected AdaptiveCards.search() triggered for activity type: ${a?.type}`)
199
+ }
200
+
201
+ const parsedQuery = parseValueSearchQuery(a.value)
202
+ const query: Query<AdaptiveCardsSearchParams> = {
203
+ count: parsedQuery.queryOptions?.top ?? 25,
204
+ skip: parsedQuery.queryOptions?.skip ?? 0,
205
+
206
+ parameters: {
207
+ queryText: parsedQuery.queryText ?? '',
208
+ dataset: parsedQuery.dataset ?? ''
209
+
210
+ }
211
+ }
212
+
213
+ const results = await handler(context, state, query)
214
+ if (!context.turnState.get(INVOKE_RESPONSE_KEY)) {
215
+ const response = {
216
+ type: AdaptiveCardInvokeResponseType.SEARCH,
217
+ value: {
218
+ results
219
+ }
220
+ }
221
+
222
+ await context.sendActivity({
223
+ value: { body: response, status: 200 } as InvokeResponse,
224
+ type: ActivityTypes.InvokeResponse
225
+ } as Activity)
226
+ }
227
+ },
228
+ true
229
+ )
230
+ })
231
+ return this._app
232
+ }
233
+ }
234
+
235
+ function createActionExecuteSelector (verb: string | RegExp | RouteSelector): RouteSelector {
236
+ if (typeof verb === 'function') {
237
+ return verb
238
+ } else if (verb instanceof RegExp) {
239
+ return (context: TurnContext) => {
240
+ const a = context?.activity
241
+ const valueAction = parseValueActionExecuteSelector(a.value)
242
+ const isInvoke =
243
+ a?.type === ActivityTypes.Invoke &&
244
+ a?.name === ACTION_INVOKE_NAME &&
245
+ valueAction?.action?.type === ACTION_EXECUTE_TYPE
246
+ if (isInvoke && typeof valueAction.action.verb === 'string') {
247
+ return Promise.resolve(verb.test(valueAction.action.verb))
248
+ } else {
249
+ return Promise.resolve(false)
250
+ }
251
+ }
252
+ } else {
253
+ return (context: TurnContext) => {
254
+ const a = context?.activity
255
+ const valueAction = parseValueActionExecuteSelector(a.value)
256
+ const isInvoke =
257
+ a?.type === ActivityTypes.Invoke &&
258
+ a?.name === ACTION_INVOKE_NAME &&
259
+ valueAction?.action?.type === ACTION_EXECUTE_TYPE
260
+ if (isInvoke && valueAction.action?.verb === verb) {
261
+ return Promise.resolve(true)
262
+ } else {
263
+ return Promise.resolve(false)
264
+ }
265
+ }
266
+ }
267
+ }
268
+
269
+ function createActionSubmitSelector (verb: string | RegExp | RouteSelector, filter: string): RouteSelector {
270
+ if (typeof verb === 'function') {
271
+ return verb
272
+ } else if (verb instanceof RegExp) {
273
+ return (context: TurnContext) => {
274
+ const a = context?.activity
275
+ const isSubmit = a?.type === ActivityTypes.Message && !a?.text && typeof a?.value === 'object'
276
+ if (isSubmit && typeof (a?.value as any)[filter] === 'string') {
277
+ return Promise.resolve(verb.test((a.value as any)[filter]))
278
+ } else {
279
+ return Promise.resolve(false)
280
+ }
281
+ }
282
+ } else {
283
+ return (context: TurnContext) => {
284
+ const a = context?.activity
285
+ const isSubmit = a?.type === ActivityTypes.Message && !a?.text && typeof a?.value === 'object'
286
+ return Promise.resolve(isSubmit && (a?.value as any)[filter] === verb)
287
+ }
288
+ }
289
+ }
290
+
291
+ function createSearchSelector (dataset: string | RegExp | RouteSelector): RouteSelector {
292
+ if (typeof dataset === 'function') {
293
+ return dataset
294
+ } else if (dataset instanceof RegExp) {
295
+ return (context: TurnContext) => {
296
+ const a = context?.activity
297
+ const valueDataset = parseValueDataset(a.value)
298
+ const isSearch = a?.type === ActivityTypes.Invoke && a?.name === SEARCH_INVOKE_NAME
299
+ if (isSearch && typeof valueDataset?.dataset === 'string') {
300
+ return Promise.resolve(dataset.test(valueDataset?.dataset))
301
+ } else {
302
+ return Promise.resolve(false)
303
+ }
304
+ }
305
+ } else {
306
+ return (context: TurnContext) => {
307
+ const a = context?.activity
308
+ // const valueDataset = parseAdaptiveCardInvokeAction(a.value)
309
+ const isSearch = a?.type === ActivityTypes.Invoke && a?.name === SEARCH_INVOKE_NAME
310
+ return Promise.resolve(isSearch)
311
+ }
312
+ }
313
+ }
314
+
315
+ async function sendInvokeResponse (context: TurnContext, response: AdaptiveCardInvokeResponse) {
316
+ await context.sendActivity({
317
+ value: { body: response, status: 200 } as InvokeResponse,
318
+ type: ActivityTypes.InvokeResponse
319
+ } as Activity)
320
+ }
@@ -0,0 +1,23 @@
1
+ /**
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+
6
+ import { AdaptiveCardActionExecuteResponseType } from './adaptiveCardActionExecuteResponseType'
7
+
8
+ /**
9
+ * Configuration options for Adaptive Cards.
10
+ */
11
+ export interface AdaptiveCardsOptions {
12
+ /**
13
+ * Specifies the filter key used for Action.Submit events.
14
+ * If not provided, a default filter key will be used.
15
+ */
16
+ actionSubmitFilter?: string;
17
+
18
+ /**
19
+ * Specifies the response type for Action.Execute events.
20
+ * Determines how the response is handled after an action is executed.
21
+ */
22
+ actionExecuteResponseType?: AdaptiveCardActionExecuteResponseType;
23
+ }
@@ -0,0 +1,28 @@
1
+ /**
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+
6
+ import { z } from 'zod'
7
+
8
+ /**
9
+ * Interface representing the search parameters for adaptive cards.
10
+ */
11
+ export interface AdaptiveCardsSearchParams {
12
+ /**
13
+ * The text query for the search.
14
+ */
15
+ queryText: string;
16
+ /**
17
+ * The dataset to search within.
18
+ */
19
+ dataset: string;
20
+ }
21
+
22
+ /**
23
+ * Zod schema for validating AdaptiveCardsSearchParams.
24
+ */
25
+ export const adaptiveCardsSearchParamsZodSchema = z.object({
26
+ queryText: z.string(),
27
+ dataset: z.string(),
28
+ })
@@ -0,0 +1,3 @@
1
+ export * from './adaptiveCardsActions'
2
+ export * from './adaptiveCardsOptions'
3
+ export * from './adaptiveCardActionExecuteResponseType'
@@ -0,0 +1,25 @@
1
+ /**
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+
6
+ /**
7
+ * Represents a query with pagination and parameters.
8
+ * @template TParams - The type of the query parameters.
9
+ */
10
+ export interface Query<TParams extends Record<string, any>> {
11
+ /**
12
+ * The number of items to retrieve.
13
+ */
14
+ count: number;
15
+
16
+ /**
17
+ * The number of items to skip.
18
+ */
19
+ skip: number;
20
+
21
+ /**
22
+ * The parameters for the query.
23
+ */
24
+ parameters: TParams;
25
+ }