UNPKG

@microsoft/agents-copilotstudio-client

Version:

Microsoft Copilot Studio Client for JavaScript. Copilot Studio Client.

140 lines 6.94 kB
"use strict"; /** * Copyright (c) Microsoft Corporation. All rights reserved. * Licensed under the MIT License. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.getCopilotStudioConnectionUrl = getCopilotStudioConnectionUrl; const agentType_1 = require("./agentType"); const powerPlatformCloud_1 = require("./powerPlatformCloud"); const ApiVersion = '2022-03-01-preview'; /** * Generates the connection URL for Copilot Studio. * @param settings - The connection settings. * @param conversationId - Optional conversation ID. * @returns The connection URL. * @throws Will throw an error if required settings are missing or invalid. */ function getCopilotStudioConnectionUrl(settings, conversationId) { let cloudValue = powerPlatformCloud_1.PowerPlatformCloud.Prod; const isNotEmptyCloud = settings.cloud && settings.cloud.trim() !== ''; const isNotEmptyCustomPowerPlatformCloud = settings.customPowerPlatformCloud !== undefined && settings.customPowerPlatformCloud.trim() !== ''; if (isNotEmptyCloud && !Object.values(powerPlatformCloud_1.PowerPlatformCloud).includes(settings.cloud)) { throw new Error('Invalid PowerPlatformCloud enum key'); } const cloudSetting = isNotEmptyCloud ? powerPlatformCloud_1.PowerPlatformCloud[settings.cloud] : powerPlatformCloud_1.PowerPlatformCloud.Unknown; if (cloudSetting === powerPlatformCloud_1.PowerPlatformCloud.Other && isNotEmptyCustomPowerPlatformCloud) { throw new Error('customPowerPlatformCloud must be provided when PowerPlatformCloud is Other'); } if (settings.environmentId.trim() === '') { throw new Error('EnvironmentId must be provided'); } if (settings.agentIdentifier === undefined || settings.agentIdentifier.trim() === '') { throw new Error('AgentIdentifier must be provided'); } if (cloudSetting !== powerPlatformCloud_1.PowerPlatformCloud.Unknown) { cloudValue = cloudSetting; } if (cloudSetting === powerPlatformCloud_1.PowerPlatformCloud.Other) { if (isNotEmptyCustomPowerPlatformCloud && isValidUri(settings.customPowerPlatformCloud)) { cloudValue = powerPlatformCloud_1.PowerPlatformCloud.Other; } else { throw new Error('customPowerPlatformCloud must be provided when PowerPlatformCloud is Other'); } } let agentType; if (settings.copilotAgentType && settings.copilotAgentType.trim() !== '') { if (!Object.values(agentType_1.AgentType).includes(settings.copilotAgentType)) { throw new Error('Invalid AgentType enum key'); } else { agentType = agentType_1.AgentType[settings.copilotAgentType]; } } else { agentType = agentType_1.AgentType.Published; } settings.customPowerPlatformCloud = isNotEmptyCustomPowerPlatformCloud ? settings.customPowerPlatformCloud : 'api.unknown.powerplatform.com'; const host = getEnvironmentEndpoint(cloudValue, settings.environmentId, settings.customPowerPlatformCloud); return createUri(settings.agentIdentifier, host, agentType, conversationId); } function isValidUri(uri) { try { const newUri = new URL(uri); return !!newUri; } catch { return false; } } function createUri(agentIdentifier, host, agentType, conversationId) { const agentPathName = agentType === agentType_1.AgentType.Published ? 'dataverse-backed' : 'prebuilt'; const url = new URL(`https://${host}`); url.searchParams.set('api-version', ApiVersion); if (!conversationId) { url.pathname = `/copilotstudio/${agentPathName}/authenticated/bots/${agentIdentifier}/conversations`; } else { url.pathname = `/copilotstudio/${agentPathName}/authenticated/bots/${agentIdentifier}/conversations/${conversationId}`; } return url.toString(); } function getEnvironmentEndpoint(cloud, environmentId, cloudBaseAddress) { if (cloud === powerPlatformCloud_1.PowerPlatformCloud.Other && (!cloudBaseAddress || !cloudBaseAddress.trim())) { throw new Error('cloudBaseAddress must be provided when PowerPlatformCloud is Other'); } cloudBaseAddress = cloudBaseAddress !== null && cloudBaseAddress !== void 0 ? cloudBaseAddress : 'api.unknown.powerplatform.com'; const normalizedResourceId = environmentId.toLowerCase().replaceAll('-', ''); const idSuffixLength = getIdSuffixLength(cloud); const hexPrefix = normalizedResourceId.substring(0, normalizedResourceId.length - idSuffixLength); const hexSuffix = normalizedResourceId.substring(normalizedResourceId.length - idSuffixLength); return `${hexPrefix}.${hexSuffix}.environment.${getEndpointSuffix(cloud, cloudBaseAddress)}`; } function getEndpointSuffix(category, cloudBaseAddress) { switch (category) { case powerPlatformCloud_1.PowerPlatformCloud.Local: return 'api.powerplatform.localhost'; case powerPlatformCloud_1.PowerPlatformCloud.Exp: return 'api.exp.powerplatform.com'; case powerPlatformCloud_1.PowerPlatformCloud.Dev: return 'api.dev.powerplatform.com'; case powerPlatformCloud_1.PowerPlatformCloud.Prv: return 'api.prv.powerplatform.com'; case powerPlatformCloud_1.PowerPlatformCloud.Test: return 'api.test.powerplatform.com'; case powerPlatformCloud_1.PowerPlatformCloud.Preprod: return 'api.preprod.powerplatform.com'; case powerPlatformCloud_1.PowerPlatformCloud.FirstRelease: case powerPlatformCloud_1.PowerPlatformCloud.Prod: return 'api.powerplatform.com'; case powerPlatformCloud_1.PowerPlatformCloud.GovFR: return 'api.gov.powerplatform.microsoft.us'; case powerPlatformCloud_1.PowerPlatformCloud.Gov: return 'api.gov.powerplatform.microsoft.us'; case powerPlatformCloud_1.PowerPlatformCloud.High: return 'api.high.powerplatform.microsoft.us'; case powerPlatformCloud_1.PowerPlatformCloud.DoD: return 'api.appsplatform.us'; case powerPlatformCloud_1.PowerPlatformCloud.Mooncake: return 'api.powerplatform.partner.microsoftonline.cn'; case powerPlatformCloud_1.PowerPlatformCloud.Ex: return 'api.powerplatform.eaglex.ic.gov'; case powerPlatformCloud_1.PowerPlatformCloud.Rx: return 'api.powerplatform.microsoft.scloud'; case powerPlatformCloud_1.PowerPlatformCloud.Other: return cloudBaseAddress; default: throw new Error(`Invalid cluster category value: ${category}`); } } function getIdSuffixLength(cloud) { switch (cloud) { case powerPlatformCloud_1.PowerPlatformCloud.FirstRelease: case powerPlatformCloud_1.PowerPlatformCloud.Prod: return 2; default: return 1; } } //# sourceMappingURL=powerPlatformEnvironment.js.map