@microsoft/teams-ai
Version:
SDK focused on building AI based applications for Microsoft Teams.
76 lines • 3.14 kB
JavaScript
;
/**
* @module teams-ai
*/
/**
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.sayCommand = sayCommand;
const botbuilder_1 = require("botbuilder");
const Utilities_1 = require("../Utilities");
/**
* @private
* @param {boolean} feedbackLoopEnabled - If true, the feedback loop UI for Teams will be enabled.
* @param {'default' | 'custom'} feedbackLoopType - the type of UI to use for feedback loop
* @returns {''} - An empty string.
*/
function sayCommand(feedbackLoopEnabled = false, feedbackLoopType = 'default') {
return async (context, _state, data) => {
if (!data.response?.content) {
return '';
}
let content = data.response.content;
const isTeamsChannel = context.activity.channelId === botbuilder_1.Channels.Msteams;
if (isTeamsChannel) {
content = content.split('\n').join('<br>');
}
// If the response from AI includes citations, those citations will be parsed and added to the SAY command.
let citations = undefined;
if (data.response.context && data.response.context.citations.length > 0) {
citations = data.response.context.citations.map((citation, i) => {
const clientCitation = {
'@type': 'Claim',
position: i + 1,
appearance: {
'@type': 'DigitalDocument',
name: citation.title || `Document #${i + 1}`,
abstract: Utilities_1.Utilities.snippet(citation.content, 477)
}
};
return clientCitation;
});
}
// If there are citations, modify the content so that the sources are numbers instead of [doc1], [doc2], etc.
const contentText = !citations ? content : Utilities_1.Utilities.formatCitationsResponse(content);
// If there are citations, filter out the citations unused in content.
const referencedCitations = citations ? Utilities_1.Utilities.getUsedCitations(contentText, citations) : undefined;
const channelData = feedbackLoopEnabled && feedbackLoopType
? {
feedbackLoop: {
type: feedbackLoopType
}
}
: { feedbackLoopEnabled };
const entities = [
{
type: 'https://schema.org/Message',
'@type': 'Message',
'@context': 'https://schema.org',
'@id': '',
additionalType: ['AIGeneratedContent'],
...(referencedCitations ? { citation: referencedCitations } : {})
}
];
const activity = {
type: botbuilder_1.ActivityTypes.Message,
text: contentText,
...(isTeamsChannel ? { channelData } : {}),
entities: entities
};
await context.sendActivity(activity);
return '';
};
}
//# sourceMappingURL=SayCommand.js.map