UNPKG

@microsoft/agents-hosting-teams

Version:

Microsoft 365 Agents SDK for JavaScript

232 lines 13.6 kB
"use strict"; /** * Copyright (c) Microsoft Corporation. All rights reserved. * Licensed under the MIT License. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.TaskModules = void 0; const agents_activity_1 = require("@microsoft/agents-activity"); const agents_hosting_1 = require("@microsoft/agents-hosting"); const taskModuleInvokeNames_1 = require("./taskModuleInvokeNames"); /** * Manages task modules for Teams applications, handling fetch and submit operations. * @template TState Type extending TurnState to be used by the application */ class TaskModules { /** * Creates a new TaskModules instance. * @param app The TeamsApplication instance to associate with this TaskModules instance */ constructor(app) { this._app = app; } /** * Handles task module fetch requests, which occur when a task module is initially requested. * @template TData Type of data expected in the task module request * @param verb Identifier for the task module (string, RegExp, or RouteSelector) * @param handler Function to handle the task module fetch request * @returns The TeamsApplication instance for chaining */ fetch(verb, handler) { (Array.isArray(verb) ? verb : [verb]).forEach((v) => { var _a, _b; const { DEFAULT_TASK_DATA_FILTER, FETCH_INVOKE_NAME } = taskModuleInvokeNames_1.TaskModuleInvokeNames; const filterField = (_b = (_a = this._app.teamsOptions.taskModules) === null || _a === void 0 ? void 0 : _a.taskDataFilter) !== null && _b !== void 0 ? _b : DEFAULT_TASK_DATA_FILTER; const selector = createTaskSelector(v, filterField, FETCH_INVOKE_NAME); this._app.addRoute(selector, async (context, state) => { var _a, _b, _c, _d, _e, _f; if (((_a = context === null || context === void 0 ? void 0 : context.activity) === null || _a === void 0 ? void 0 : _a.channelId) === agents_activity_1.Channels.Msteams) { if (((_b = context === null || context === void 0 ? void 0 : context.activity) === null || _b === void 0 ? void 0 : _b.type) !== agents_activity_1.ActivityTypes.Invoke || ((_c = context === null || context === void 0 ? void 0 : context.activity) === null || _c === void 0 ? void 0 : _c.name) !== FETCH_INVOKE_NAME) { throw new Error(`Unexpected TaskModules.fetch() triggered for activity type: ${(_d = context === null || context === void 0 ? void 0 : context.activity) === null || _d === void 0 ? void 0 : _d.type}`); } const result = await handler(context, state, (_f = (_e = context.activity.value) === null || _e === void 0 ? void 0 : _e.data) !== null && _f !== void 0 ? _f : {}); if (!context.turnState.get(agents_hosting_1.INVOKE_RESPONSE_KEY)) { let response; if (typeof result === 'string') { response = { task: { type: 'message', value: result } }; } else { response = { task: { type: 'continue', value: result } }; } await context.sendActivity({ value: { body: response, status: 200 }, type: agents_activity_1.ActivityTypes.InvokeResponse }); } } }, true); }); return this._app; } /** * Handles task module submit requests, which occur when a task module form is submitted. * @template TData Type of data expected in the task module submit request * @param verb Identifier for the task module (string, RegExp, or RouteSelector) * @param handler Function to handle the task module submit request * @returns The TeamsApplication instance for chaining */ submit(verb, handler) { (Array.isArray(verb) ? verb : [verb]).forEach((v) => { var _a, _b; const { DEFAULT_TASK_DATA_FILTER, SUBMIT_INVOKE_NAME } = taskModuleInvokeNames_1.TaskModuleInvokeNames; const filterField = (_b = (_a = this._app.teamsOptions.taskModules) === null || _a === void 0 ? void 0 : _a.taskDataFilter) !== null && _b !== void 0 ? _b : DEFAULT_TASK_DATA_FILTER; const selector = createTaskSelector(v, filterField, SUBMIT_INVOKE_NAME); this._app.addRoute(selector, async (context, state) => { var _a, _b, _c, _d, _e, _f; if (((_a = context === null || context === void 0 ? void 0 : context.activity) === null || _a === void 0 ? void 0 : _a.channelId) === agents_activity_1.Channels.Msteams) { if (((_b = context === null || context === void 0 ? void 0 : context.activity) === null || _b === void 0 ? void 0 : _b.type) !== agents_activity_1.ActivityTypes.Invoke || ((_c = context === null || context === void 0 ? void 0 : context.activity) === null || _c === void 0 ? void 0 : _c.name) !== SUBMIT_INVOKE_NAME) { throw new Error(`Unexpected TaskModules.submit() triggered for activity type: ${(_d = context === null || context === void 0 ? void 0 : context.activity) === null || _d === void 0 ? void 0 : _d.type}`); } const result = await handler(context, state, (_f = (_e = context.activity.value) === null || _e === void 0 ? void 0 : _e.data) !== null && _f !== void 0 ? _f : {}); if (!result) { await context.sendActivity({ value: { status: 200 }, type: agents_activity_1.ActivityTypes.InvokeResponse }); } if (!context.turnState.get(agents_hosting_1.INVOKE_RESPONSE_KEY)) { let response; if (typeof result === 'string') { response = { task: { type: 'message', value: result } }; } else if (typeof result === 'object') { response = { task: { type: 'continue', value: result } }; } await context.sendActivity({ value: { body: response, status: 200 }, type: agents_activity_1.ActivityTypes.InvokeResponse }); } } }, true); }); return this._app; } /** * Handles configuration fetch requests for agent configuration in Teams. * @template TData Type of data expected in the configuration fetch request * @param handler Function to handle the configuration fetch request * @returns The TeamsApplication instance for chaining */ configFetch(handler) { const selector = (context) => { var _a, _b; const { CONFIG_FETCH_INVOKE_NAME } = taskModuleInvokeNames_1.TaskModuleInvokeNames; return Promise.resolve(((_a = context === null || context === void 0 ? void 0 : context.activity) === null || _a === void 0 ? void 0 : _a.type) === agents_activity_1.ActivityTypes.Invoke && ((_b = context === null || context === void 0 ? void 0 : context.activity) === null || _b === void 0 ? void 0 : _b.name) === CONFIG_FETCH_INVOKE_NAME); }; this._app.addRoute(selector, async (context, state) => { var _a, _b, _c; if (((_a = context === null || context === void 0 ? void 0 : context.activity) === null || _a === void 0 ? void 0 : _a.channelId) === agents_activity_1.Channels.Msteams) { const result = await handler(context, state, (_c = (_b = context.activity.value) === null || _b === void 0 ? void 0 : _b.data) !== null && _c !== void 0 ? _c : {}); let response; if (!context.turnState.get(agents_hosting_1.INVOKE_RESPONSE_KEY)) { response = { responseType: 'config', config: result }; if ('cacheInfo' in result) { response.cacheInfo = result.cacheInfo; } await context.sendActivity({ value: { body: response, status: 200 }, type: agents_activity_1.ActivityTypes.InvokeResponse }); } } }, true); return this._app; } /** * Handles configuration submit requests for agent configuration in Teams. * @template TData Type of data expected in the configuration submit request * @param handler Function to handle the configuration submit request * @returns The TeamsApplication instance for chaining */ configSubmit(handler) { const selector = (context) => { var _a, _b; const { CONFIG_SUBMIT_INVOKE_NAME } = taskModuleInvokeNames_1.TaskModuleInvokeNames; return Promise.resolve(((_a = context === null || context === void 0 ? void 0 : context.activity) === null || _a === void 0 ? void 0 : _a.type) === agents_activity_1.ActivityTypes.Invoke && ((_b = context === null || context === void 0 ? void 0 : context.activity) === null || _b === void 0 ? void 0 : _b.name) === CONFIG_SUBMIT_INVOKE_NAME); }; this._app.addRoute(selector, async (context, state) => { var _a, _b, _c; if (((_a = context === null || context === void 0 ? void 0 : context.activity) === null || _a === void 0 ? void 0 : _a.channelId) === agents_activity_1.Channels.Msteams) { const result = await handler(context, state, (_c = (_b = context.activity.value) === null || _b === void 0 ? void 0 : _b.data) !== null && _c !== void 0 ? _c : {}); let response; if (!context.turnState.get(agents_hosting_1.INVOKE_RESPONSE_KEY)) { response = { responseType: 'config', config: result }; if ('cacheInfo' in result) { response.cacheInfo = result.cacheInfo; } await context.sendActivity({ value: { body: response, status: 200 }, type: agents_activity_1.ActivityTypes.InvokeResponse }); } } }, true); return this._app; } } exports.TaskModules = TaskModules; /** * Creates a route selector for task modules based on the provided verb and invoke name. * @param verb Identifier for the task module (string, RegExp, or RouteSelector) * @param filterField Field name to filter on in the task data * @param invokeName Name of the invoke activity * @returns A RouteSelector function */ function createTaskSelector(verb, filterField, invokeName) { if (typeof verb === 'function') { return verb; } else if (verb instanceof RegExp) { return (context) => { var _a, _b, _c, _d; const isTeams = context.activity.channelId === agents_activity_1.Channels.Msteams; const isInvoke = ((_a = context === null || context === void 0 ? void 0 : context.activity) === null || _a === void 0 ? void 0 : _a.type) === agents_activity_1.ActivityTypes.Invoke && ((_b = context === null || context === void 0 ? void 0 : context.activity) === null || _b === void 0 ? void 0 : _b.name) === invokeName; const data = (_d = (_c = context === null || context === void 0 ? void 0 : context.activity) === null || _c === void 0 ? void 0 : _c.value) === null || _d === void 0 ? void 0 : _d.data; if (isInvoke && isTeams && typeof data === 'object' && typeof data[filterField] === 'string') { return Promise.resolve(verb.test(data[filterField])); } else { return Promise.resolve(false); } }; } else { return (context) => { var _a, _b, _c, _d; const isInvoke = ((_a = context === null || context === void 0 ? void 0 : context.activity) === null || _a === void 0 ? void 0 : _a.type) === agents_activity_1.ActivityTypes.Invoke && ((_b = context === null || context === void 0 ? void 0 : context.activity) === null || _b === void 0 ? void 0 : _b.name) === invokeName; const data = (_d = (_c = context === null || context === void 0 ? void 0 : context.activity) === null || _c === void 0 ? void 0 : _c.value) === null || _d === void 0 ? void 0 : _d.data; return Promise.resolve(isInvoke && typeof data === 'object' && data[filterField] === verb); }; } } //# sourceMappingURL=taskModules.js.map