UNPKG

@microsoft/agents-copilotstudio-client

Version:

Microsoft Copilot Studio Client for JavaScript. Copilot Studio Client.

126 lines 5.27 kB
"use strict"; /** * Copyright (c) Microsoft Corporation. All rights reserved. * Licensed under the MIT License. */ var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.CopilotStudioClient = void 0; const axios_1 = __importDefault(require("axios")); const powerPlatformEnvironment_1 = require("./powerPlatformEnvironment"); const agents_activity_1 = require("@microsoft/agents-activity"); const executeTurnRequest_1 = require("./executeTurnRequest"); const debug_1 = __importDefault(require("debug")); const package_json_1 = __importDefault(require("@microsoft/agents-copilotstudio-client/package.json")); const os_1 = __importDefault(require("os")); class CopilotStudioClient { /** * Creates an instance of CopilotStudioClient. * @param settings The connection settings. * @param token The authentication token. */ constructor(settings, token) { /** The ID of the current conversation. */ this.conversationId = ''; this.settings = settings; this.client = axios_1.default.create(); this.client.defaults.headers.common.Authorization = `Bearer ${token}`; this.logger = (0, debug_1.default)('copilot-studio-client'); } async postRequestAsync(axiosConfig) { const activities = []; const response = await this.client(axiosConfig); const stream = response.data; const reader = stream.pipeThrough(new TextDecoderStream()).getReader(); let result = ''; const results = []; const processEvents = async ({ done, value }) => { if (done) { this.logger('Stream complete'); result += value; results.push(result); return results; } this.logger('Agent is typing...'); result += value; return await processEvents(await reader.read()); }; const events = await reader.read().then(processEvents); events.forEach(event => { const values = event.toString().split('\n'); const validEvents = values.filter(e => e.substring(0, 4) === 'data' && e !== 'data: end\r'); validEvents.forEach(ve => { try { const act = agents_activity_1.Activity.fromJson(ve.substring(5, ve.length)); if (act.type === agents_activity_1.ActivityTypes.Message) { activities.push(act); } else { this.logger('Activity type: ', act.type); } } catch (e) { this.logger('Error: ', e); throw e; } }); }); return activities; } static getProductInfo() { return `CopilotStudioClient.agents-sdk-js/${package_json_1.default.version} nodejs/${process.version} ${os_1.default.platform()}-${os_1.default.arch()}/${os_1.default.release()}`; } async startConversationAsync(emitStartConversationEvent = true) { var _a; const uriStart = (0, powerPlatformEnvironment_1.getCopilotStudioConnectionUrl)(this.settings); const body = { emitStartConversationEvent }; const config = { method: 'post', url: uriStart, headers: { Accept: 'text/event-stream', 'Content-Type': 'application/json', 'User-Agent': CopilotStudioClient.getProductInfo(), }, data: body, responseType: 'stream', adapter: 'fetch' }; const values = await this.postRequestAsync(config); const act = values[0]; this.conversationId = (_a = act.conversation) === null || _a === void 0 ? void 0 : _a.id; return act; } async askQuestionAsync(question, conversationId = this.conversationId) { var _a, _b; const conversationAccount = { id: conversationId }; const activityObj = { type: 'message', text: question, conversation: conversationAccount }; const activity = agents_activity_1.Activity.fromObject(activityObj); const localConversationId = (_b = (_a = activity.conversation) === null || _a === void 0 ? void 0 : _a.id) !== null && _b !== void 0 ? _b : conversationId; const uriExecute = (0, powerPlatformEnvironment_1.getCopilotStudioConnectionUrl)(this.settings, localConversationId); const qbody = new executeTurnRequest_1.ExecuteTurnRequest(activity); const config = { method: 'post', url: uriExecute, headers: { Accept: 'text/event-stream', 'Content-Type': 'application/json' }, data: qbody, responseType: 'stream', adapter: 'fetch' }; const values = await this.postRequestAsync(config); return values; } } exports.CopilotStudioClient = CopilotStudioClient; //# sourceMappingURL=copilotStudioClient.js.map