UNPKG

@microsoft/agents-hosting-teams

Version:

Microsoft 365 Agents SDK for JavaScript

253 lines 14.1 kB
"use strict"; /** * Copyright (c) Microsoft Corporation. All rights reserved. * Licensed under the MIT License. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.AdaptiveCardsActions = exports.ACTION_INVOKE_NAME = void 0; const agents_activity_1 = require("@microsoft/agents-activity"); const agents_hosting_1 = require("@microsoft/agents-hosting"); const adaptiveCardActionExecuteResponseType_1 = require("./adaptiveCardActionExecuteResponseType"); const adaptiveCardInvokeResponseType_1 = require("./adaptiveCardInvokeResponseType"); const parsers_1 = require("../../parsers"); exports.ACTION_INVOKE_NAME = 'adaptiveCard/action'; const ACTION_EXECUTE_TYPE = 'Action.Execute'; const DEFAULT_ACTION_SUBMIT_FILTER = 'verb'; const SEARCH_INVOKE_NAME = 'application/search'; /** * A class to handle Adaptive Card actions such as executing actions, submitting actions, and performing searches. * @template TState - The type of the TurnState used in the application. */ class AdaptiveCardsActions { /** * Constructs an instance of AdaptiveCardsActions. * @param app - The Teams application instance. */ constructor(app) { this._app = app; } /** * Registers a handler for the Action.Execute event. * @template TData - The type of the data passed to the handler. * @param verb - A string, RegExp, RouteSelector, or an array of these to match the action verb. * @param handler - A function to handle the action execution. * @returns The Teams application instance. */ actionExecute(verb, handler) { var _a, _b; let actionExecuteResponseType = (_b = (_a = this._app.teamsOptions.adaptiveCards) === null || _a === void 0 ? void 0 : _a.actionExecuteResponseType) !== null && _b !== void 0 ? _b : adaptiveCardActionExecuteResponseType_1.AdaptiveCardActionExecuteResponseType.REPLACE_FOR_INTERACTOR; (Array.isArray(verb) ? verb : [verb]).forEach((v) => { const selector = createActionExecuteSelector(v); this._app.addRoute(selector, async (context, state) => { var _a; const a = context === null || context === void 0 ? void 0 : context.activity; const invokeAction = (0, parsers_1.parseValueActionExecuteSelector)(a.value); if ((a === null || a === void 0 ? void 0 : a.type) !== agents_activity_1.ActivityTypes.Invoke || (a === null || a === void 0 ? void 0 : a.name) !== exports.ACTION_INVOKE_NAME || (invokeAction.action.type !== ACTION_EXECUTE_TYPE)) { throw new Error(`Unexpected AdaptiveCards.actionExecute() triggered for activity type: ${invokeAction.action.type}`); } const result = await handler(context, state, (_a = invokeAction.action) !== null && _a !== void 0 ? _a : {}); if (!context.turnState.get(agents_hosting_1.INVOKE_RESPONSE_KEY)) { let response; if (typeof result === 'string') { response = { statusCode: 200, type: adaptiveCardInvokeResponseType_1.AdaptiveCardInvokeResponseType.MESSAGE, value: result }; await sendInvokeResponse(context, response); } else { if (result.refresh && actionExecuteResponseType !== adaptiveCardActionExecuteResponseType_1.AdaptiveCardActionExecuteResponseType.NEW_MESSAGE_FOR_ALL) { actionExecuteResponseType = adaptiveCardActionExecuteResponseType_1.AdaptiveCardActionExecuteResponseType.REPLACE_FOR_ALL; } const activity = agents_hosting_1.MessageFactory.attachment(agents_hosting_1.CardFactory.adaptiveCard(result)); response = { statusCode: 200, type: adaptiveCardInvokeResponseType_1.AdaptiveCardInvokeResponseType.ADAPTIVE, value: result }; if (actionExecuteResponseType === adaptiveCardActionExecuteResponseType_1.AdaptiveCardActionExecuteResponseType.NEW_MESSAGE_FOR_ALL) { await sendInvokeResponse(context, { statusCode: 200, type: adaptiveCardInvokeResponseType_1.AdaptiveCardInvokeResponseType.MESSAGE, value: 'Your response was sent to the app' }); await context.sendActivity(activity); } else if (actionExecuteResponseType === adaptiveCardActionExecuteResponseType_1.AdaptiveCardActionExecuteResponseType.REPLACE_FOR_ALL) { activity.id = context.activity.replyToId; await context.updateActivity(activity); await sendInvokeResponse(context, response); } else { await sendInvokeResponse(context, response); } } } }, true); }); return this._app; } /** * Registers a handler for the Action.Submit event. * @template TData - The type of the data passed to the handler. * @param verb - A string, RegExp, RouteSelector, or an array of these to match the action verb. * @param handler - A function to handle the action submission. * @returns The Teams application instance. */ actionSubmit(verb, handler) { var _a, _b; const filter = (_b = (_a = this._app.teamsOptions.adaptiveCards) === null || _a === void 0 ? void 0 : _a.actionSubmitFilter) !== null && _b !== void 0 ? _b : DEFAULT_ACTION_SUBMIT_FILTER; (Array.isArray(verb) ? verb : [verb]).forEach((v) => { const selector = createActionSubmitSelector(v, filter); this._app.addRoute(selector, async (context, state) => { var _a; const a = context === null || context === void 0 ? void 0 : context.activity; if ((a === null || a === void 0 ? void 0 : a.type) !== agents_activity_1.ActivityTypes.Message || (a === null || a === void 0 ? void 0 : a.text) || typeof (a === null || a === void 0 ? void 0 : a.value) !== 'object') { throw new Error(`Unexpected AdaptiveCards.actionSubmit() triggered for activity type: ${a === null || a === void 0 ? void 0 : a.type}`); } await handler(context, state, (_a = ((0, parsers_1.parseAdaptiveCardInvokeAction)(a.value)).data) !== null && _a !== void 0 ? _a : {}); }); }); return this._app; } /** * Registers a handler for the search event. * @param dataset - A string, RegExp, RouteSelector, or an array of these to match the dataset. * @param handler - A function to handle the search query. * @returns The Teams application instance. */ search(dataset, handler) { (Array.isArray(dataset) ? dataset : [dataset]).forEach((ds) => { const selector = createSearchSelector(ds); this._app.addRoute(selector, async (context, state) => { var _a, _b, _c, _d, _e, _f; const a = context === null || context === void 0 ? void 0 : context.activity; if ((a === null || a === void 0 ? void 0 : a.type) !== agents_activity_1.ActivityTypes.Invoke || (a === null || a === void 0 ? void 0 : a.name) !== SEARCH_INVOKE_NAME) { throw new Error(`Unexpected AdaptiveCards.search() triggered for activity type: ${a === null || a === void 0 ? void 0 : a.type}`); } const parsedQuery = (0, parsers_1.parseValueSearchQuery)(a.value); const query = { count: (_b = (_a = parsedQuery.queryOptions) === null || _a === void 0 ? void 0 : _a.top) !== null && _b !== void 0 ? _b : 25, skip: (_d = (_c = parsedQuery.queryOptions) === null || _c === void 0 ? void 0 : _c.skip) !== null && _d !== void 0 ? _d : 0, parameters: { queryText: (_e = parsedQuery.queryText) !== null && _e !== void 0 ? _e : '', dataset: (_f = parsedQuery.dataset) !== null && _f !== void 0 ? _f : '' } }; const results = await handler(context, state, query); if (!context.turnState.get(agents_hosting_1.INVOKE_RESPONSE_KEY)) { const response = { type: adaptiveCardInvokeResponseType_1.AdaptiveCardInvokeResponseType.SEARCH, value: { results } }; await context.sendActivity({ value: { body: response, status: 200 }, type: agents_activity_1.ActivityTypes.InvokeResponse }); } }, true); }); return this._app; } } exports.AdaptiveCardsActions = AdaptiveCardsActions; function createActionExecuteSelector(verb) { if (typeof verb === 'function') { return verb; } else if (verb instanceof RegExp) { return (context) => { var _a; const a = context === null || context === void 0 ? void 0 : context.activity; const valueAction = (0, parsers_1.parseValueActionExecuteSelector)(a.value); const isInvoke = (a === null || a === void 0 ? void 0 : a.type) === agents_activity_1.ActivityTypes.Invoke && (a === null || a === void 0 ? void 0 : a.name) === exports.ACTION_INVOKE_NAME && ((_a = valueAction.action) === null || _a === void 0 ? void 0 : _a.type) === ACTION_EXECUTE_TYPE; if (isInvoke && typeof valueAction.action.verb === 'string') { return Promise.resolve(verb.test(valueAction.action.verb)); } else { return Promise.resolve(false); } }; } else { return (context) => { var _a, _b; const a = context === null || context === void 0 ? void 0 : context.activity; const valueAction = (0, parsers_1.parseValueActionExecuteSelector)(a.value); const isInvoke = (a === null || a === void 0 ? void 0 : a.type) === agents_activity_1.ActivityTypes.Invoke && (a === null || a === void 0 ? void 0 : a.name) === exports.ACTION_INVOKE_NAME && ((_a = valueAction.action) === null || _a === void 0 ? void 0 : _a.type) === ACTION_EXECUTE_TYPE; if (isInvoke && ((_b = valueAction.action) === null || _b === void 0 ? void 0 : _b.verb) === verb) { return Promise.resolve(true); } else { return Promise.resolve(false); } }; } } function createActionSubmitSelector(verb, filter) { if (typeof verb === 'function') { return verb; } else if (verb instanceof RegExp) { return (context) => { const a = context === null || context === void 0 ? void 0 : context.activity; const isSubmit = (a === null || a === void 0 ? void 0 : a.type) === agents_activity_1.ActivityTypes.Message && !(a === null || a === void 0 ? void 0 : a.text) && typeof (a === null || a === void 0 ? void 0 : a.value) === 'object'; if (isSubmit && typeof (a === null || a === void 0 ? void 0 : a.value)[filter] === 'string') { return Promise.resolve(verb.test(a.value[filter])); } else { return Promise.resolve(false); } }; } else { return (context) => { const a = context === null || context === void 0 ? void 0 : context.activity; const isSubmit = (a === null || a === void 0 ? void 0 : a.type) === agents_activity_1.ActivityTypes.Message && !(a === null || a === void 0 ? void 0 : a.text) && typeof (a === null || a === void 0 ? void 0 : a.value) === 'object'; return Promise.resolve(isSubmit && (a === null || a === void 0 ? void 0 : a.value)[filter] === verb); }; } } function createSearchSelector(dataset) { if (typeof dataset === 'function') { return dataset; } else if (dataset instanceof RegExp) { return (context) => { const a = context === null || context === void 0 ? void 0 : context.activity; const valueDataset = (0, parsers_1.parseValueDataset)(a.value); const isSearch = (a === null || a === void 0 ? void 0 : a.type) === agents_activity_1.ActivityTypes.Invoke && (a === null || a === void 0 ? void 0 : a.name) === SEARCH_INVOKE_NAME; if (isSearch && typeof valueDataset.dataset === 'string') { return Promise.resolve(dataset.test(valueDataset.dataset)); } else { return Promise.resolve(false); } }; } else { return (context) => { const a = context === null || context === void 0 ? void 0 : context.activity; const valueDataset = (0, parsers_1.parseValueDataset)(a.value); const isSearch = (a === null || a === void 0 ? void 0 : a.type) === agents_activity_1.ActivityTypes.Invoke && (a === null || a === void 0 ? void 0 : a.name) === SEARCH_INVOKE_NAME; return Promise.resolve(isSearch && valueDataset.dataset === dataset); }; } } async function sendInvokeResponse(context, response) { await context.sendActivity({ value: { body: response, status: 200 }, type: agents_activity_1.ActivityTypes.InvokeResponse }); } //# sourceMappingURL=adaptiveCardsActions.js.map