@microsoft/agents-copilotstudio-client 0.1.25-gcaee57b821

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (34) hide show
  1. package/README.md +48 -0
  2. package/dist/src/botType.d.ts +17 -0
  3. package/dist/src/botType.js +22 -0
  4. package/dist/src/botType.js.map +1 -0
  5. package/dist/src/connectionSettings.d.ts +28 -0
  6. package/dist/src/connectionSettings.js +41 -0
  7. package/dist/src/connectionSettings.js.map +1 -0
  8. package/dist/src/copilotStudioClient.d.ts +25 -0
  9. package/dist/src/copilotStudioClient.js +120 -0
  10. package/dist/src/copilotStudioClient.js.map +1 -0
  11. package/dist/src/directToEngineConnectionSettings.d.ts +21 -0
  12. package/dist/src/directToEngineConnectionSettings.js +7 -0
  13. package/dist/src/directToEngineConnectionSettings.js.map +1 -0
  14. package/dist/src/executeTurnRequest.d.ts +14 -0
  15. package/dist/src/executeTurnRequest.js +18 -0
  16. package/dist/src/executeTurnRequest.js.map +1 -0
  17. package/dist/src/index.d.ts +7 -0
  18. package/dist/src/index.js +24 -0
  19. package/dist/src/index.js.map +1 -0
  20. package/dist/src/powerPlatformCloud.d.ts +77 -0
  21. package/dist/src/powerPlatformCloud.js +82 -0
  22. package/dist/src/powerPlatformCloud.js.map +1 -0
  23. package/dist/src/powerPlatformEnvironment.d.ts +13 -0
  24. package/dist/src/powerPlatformEnvironment.js +140 -0
  25. package/dist/src/powerPlatformEnvironment.js.map +1 -0
  26. package/package.json +43 -0
  27. package/src/botType.ts +18 -0
  28. package/src/connectionSettings.ts +40 -0
  29. package/src/copilotStudioClient.ts +134 -0
  30. package/src/directToEngineConnectionSettings.ts +23 -0
  31. package/src/executeTurnRequest.ts +19 -0
  32. package/src/index.ts +7 -0
  33. package/src/powerPlatformCloud.ts +78 -0
  34. package/src/powerPlatformEnvironment.ts +175 -0
@@ -0,0 +1,140 @@
1
+ "use strict";
2
+ /**
3
+ * Copyright (c) Microsoft Corporation. All rights reserved.
4
+ * Licensed under the MIT License.
5
+ */
6
+ Object.defineProperty(exports, "__esModule", { value: true });
7
+ exports.getCopilotStudioConnectionUrl = getCopilotStudioConnectionUrl;
8
+ const botType_1 = require("./botType");
9
+ const powerPlatformCloud_1 = require("./powerPlatformCloud");
10
+ const ApiVersion = '2022-03-01-preview';
11
+ /**
12
+ * Generates the connection URL for Copilot Studio.
13
+ * @param settings - The connection settings.
14
+ * @param conversationId - Optional conversation ID.
15
+ * @returns The connection URL.
16
+ * @throws Will throw an error if required settings are missing or invalid.
17
+ */
18
+ function getCopilotStudioConnectionUrl(settings, conversationId) {
19
+ let cloudValue = powerPlatformCloud_1.PowerPlatformCloud.Prod;
20
+ const isNotEmptyCloud = settings.cloud && settings.cloud.trim() !== '';
21
+ const isNotEmptyCustomPowerPlatformCloud = settings.customPowerPlatformCloud !== undefined && settings.customPowerPlatformCloud.trim() !== '';
22
+ if (isNotEmptyCloud && !Object.values(powerPlatformCloud_1.PowerPlatformCloud).includes(settings.cloud)) {
23
+ throw new Error('Invalid PowerPlatformCloud enum key');
24
+ }
25
+ const cloudSetting = isNotEmptyCloud ? powerPlatformCloud_1.PowerPlatformCloud[settings.cloud] : powerPlatformCloud_1.PowerPlatformCloud.Unknown;
26
+ if (cloudSetting === powerPlatformCloud_1.PowerPlatformCloud.Other && isNotEmptyCustomPowerPlatformCloud) {
27
+ throw new Error('customPowerPlatformCloud must be provided when PowerPlatformCloud is Other');
28
+ }
29
+ if (settings.environmentId.trim() === '') {
30
+ throw new Error('EnvironmentId must be provided');
31
+ }
32
+ if (settings.botIdentifier === undefined || settings.botIdentifier.trim() === '') {
33
+ throw new Error('BotIdentifier must be provided');
34
+ }
35
+ if (cloudSetting !== powerPlatformCloud_1.PowerPlatformCloud.Unknown) {
36
+ cloudValue = cloudSetting;
37
+ }
38
+ if (cloudSetting === powerPlatformCloud_1.PowerPlatformCloud.Other) {
39
+ if (isNotEmptyCustomPowerPlatformCloud && isValidUri(settings.customPowerPlatformCloud)) {
40
+ cloudValue = powerPlatformCloud_1.PowerPlatformCloud.Other;
41
+ }
42
+ else {
43
+ throw new Error('customPowerPlatformCloud must be provided when PowerPlatformCloud is Other');
44
+ }
45
+ }
46
+ let botType;
47
+ if (settings.copilotBotType && settings.copilotBotType.trim() !== '') {
48
+ if (!Object.values(botType_1.BotType).includes(settings.copilotBotType)) {
49
+ throw new Error('Invalid BotType enum key');
50
+ }
51
+ else {
52
+ botType = botType_1.BotType[settings.copilotBotType];
53
+ }
54
+ }
55
+ else {
56
+ botType = botType_1.BotType.Published;
57
+ }
58
+ settings.customPowerPlatformCloud = isNotEmptyCustomPowerPlatformCloud ? settings.customPowerPlatformCloud : 'api.unknown.powerplatform.com';
59
+ const host = getEnvironmentEndpoint(cloudValue, settings.environmentId, settings.customPowerPlatformCloud);
60
+ return createUri(settings.botIdentifier, host, botType, conversationId);
61
+ }
62
+ function isValidUri(uri) {
63
+ try {
64
+ const newUri = new URL(uri);
65
+ return !!newUri;
66
+ }
67
+ catch {
68
+ return false;
69
+ }
70
+ }
71
+ function createUri(botIdentifier, host, botType, conversationId) {
72
+ const botPathName = botType === botType_1.BotType.Published ? 'dataverse-backed' : 'prebuilt';
73
+ const url = new URL(`https://${host}`);
74
+ url.searchParams.set('api-version', ApiVersion);
75
+ if (!conversationId) {
76
+ url.pathname = `/copilotstudio/${botPathName}/authenticated/bots/${botIdentifier}/conversations`;
77
+ }
78
+ else {
79
+ url.pathname = `/copilotstudio/${botPathName}/authenticated/bots/${botIdentifier}/conversations/${conversationId}`;
80
+ }
81
+ return url.toString();
82
+ }
83
+ function getEnvironmentEndpoint(cloud, environmentId, cloudBaseAddress) {
84
+ if (cloud === powerPlatformCloud_1.PowerPlatformCloud.Other && (!cloudBaseAddress || !cloudBaseAddress.trim())) {
85
+ throw new Error('cloudBaseAddress must be provided when PowerPlatformCloud is Other');
86
+ }
87
+ cloudBaseAddress = cloudBaseAddress !== null && cloudBaseAddress !== void 0 ? cloudBaseAddress : 'api.unknown.powerplatform.com';
88
+ const normalizedResourceId = environmentId.toLowerCase().replaceAll('-', '');
89
+ const idSuffixLength = getIdSuffixLength(cloud);
90
+ const hexPrefix = normalizedResourceId.substring(0, normalizedResourceId.length - idSuffixLength);
91
+ const hexSuffix = normalizedResourceId.substring(normalizedResourceId.length - idSuffixLength);
92
+ return `${hexPrefix}.${hexSuffix}.environment.${getEndpointSuffix(cloud, cloudBaseAddress)}`;
93
+ }
94
+ function getEndpointSuffix(category, cloudBaseAddress) {
95
+ switch (category) {
96
+ case powerPlatformCloud_1.PowerPlatformCloud.Local:
97
+ return 'api.powerplatform.localhost';
98
+ case powerPlatformCloud_1.PowerPlatformCloud.Exp:
99
+ return 'api.exp.powerplatform.com';
100
+ case powerPlatformCloud_1.PowerPlatformCloud.Dev:
101
+ return 'api.dev.powerplatform.com';
102
+ case powerPlatformCloud_1.PowerPlatformCloud.Prv:
103
+ return 'api.prv.powerplatform.com';
104
+ case powerPlatformCloud_1.PowerPlatformCloud.Test:
105
+ return 'api.test.powerplatform.com';
106
+ case powerPlatformCloud_1.PowerPlatformCloud.Preprod:
107
+ return 'api.preprod.powerplatform.com';
108
+ case powerPlatformCloud_1.PowerPlatformCloud.FirstRelease:
109
+ case powerPlatformCloud_1.PowerPlatformCloud.Prod:
110
+ return 'api.powerplatform.com';
111
+ case powerPlatformCloud_1.PowerPlatformCloud.GovFR:
112
+ return 'api.gov.powerplatform.microsoft.us';
113
+ case powerPlatformCloud_1.PowerPlatformCloud.Gov:
114
+ return 'api.gov.powerplatform.microsoft.us';
115
+ case powerPlatformCloud_1.PowerPlatformCloud.High:
116
+ return 'api.high.powerplatform.microsoft.us';
117
+ case powerPlatformCloud_1.PowerPlatformCloud.DoD:
118
+ return 'api.appsplatform.us';
119
+ case powerPlatformCloud_1.PowerPlatformCloud.Mooncake:
120
+ return 'api.powerplatform.partner.microsoftonline.cn';
121
+ case powerPlatformCloud_1.PowerPlatformCloud.Ex:
122
+ return 'api.powerplatform.eaglex.ic.gov';
123
+ case powerPlatformCloud_1.PowerPlatformCloud.Rx:
124
+ return 'api.powerplatform.microsoft.scloud';
125
+ case powerPlatformCloud_1.PowerPlatformCloud.Other:
126
+ return cloudBaseAddress;
127
+ default:
128
+ throw new Error(`Invalid cluster category value: ${category}`);
129
+ }
130
+ }
131
+ function getIdSuffixLength(cloud) {
132
+ switch (cloud) {
133
+ case powerPlatformCloud_1.PowerPlatformCloud.FirstRelease:
134
+ case powerPlatformCloud_1.PowerPlatformCloud.Prod:
135
+ return 2;
136
+ default:
137
+ return 1;
138
+ }
139
+ }
140
+ //# sourceMappingURL=powerPlatformEnvironment.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"powerPlatformEnvironment.js","sourceRoot":"","sources":["../../src/powerPlatformEnvironment.ts"],"names":[],"mappings":";AAAA;;;GAGG;;AAeH,sEAyDC;AAtED,uCAAmC;AAEnC,6DAAyD;AAEzD,MAAM,UAAU,GAAW,oBAAoB,CAAA;AAE/C;;;;;;GAMG;AACH,SAAgB,6BAA6B,CAC3C,QAA4B,EAC5B,cAAuB;IAEvB,IAAI,UAAU,GAAuB,uCAAkB,CAAC,IAAI,CAAA;IAE5D,MAAM,eAAe,GAAG,QAAQ,CAAC,KAAK,IAAI,QAAQ,CAAC,KAAK,CAAC,IAAI,EAAE,KAAK,EAAE,CAAA;IACtE,MAAM,kCAAkC,GAAG,QAAQ,CAAC,wBAAwB,KAAK,SAAS,IAAI,QAAQ,CAAC,wBAAwB,CAAC,IAAI,EAAE,KAAK,EAAE,CAAA;IAE7I,IAAI,eAAe,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,uCAAkB,CAAC,CAAC,QAAQ,CAAC,QAAQ,CAAC,KAAK,CAAC,EAAE,CAAC;QACnF,MAAM,IAAI,KAAK,CAAC,qCAAqC,CAAC,CAAA;IACxD,CAAC;IAED,MAAM,YAAY,GAAG,eAAe,CAAC,CAAC,CAAC,uCAAkB,CAAC,QAAQ,CAAC,KAAwC,CAAC,CAAC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AO;YAC7B,OAAO,+BAA+B,CAAA;QACxC,KAAK,uCAAkB,CAAC,YAAY,CAAC;QACrC,KAAK,uCAAkB,CAAC,IAAI;YAC1B,OAAO,uBAAuB,CAAA;QAChC,KAAK,uCAAkB,CAAC,KAAK;YAC3B,OAAO,oCAAoC,CAAA;QAC7C,KAAK,uCAAkB,CAAC,GAAG;YACzB,OAAO,oCAAoC,CAAA;QAC7C,KAAK,uCAAkB,CAAC,IAAI;YAC1B,OAAO,qCAAqC,CAAA;QAC9C,KAAK,uCAAkB,CAAC,GAAG;YACzB,OAAO,qBAAqB,CAAA;QAC9B,KAAK,uCAAkB,CAAC,QAAQ;YAC9B,OAAO,8CAA8C,CAAA;QACvD,KAAK,uCAAkB,CAAC,EAAE;YACxB,OAAO,iCAAiC,CAAA;QAC1C,KAAK,uCAAkB,CAAC,EAAE;YACxB,OAAO,oCAAoC,CAAA;QAC7C,KAAK,uCAAkB,CAAC,KAAK;YAC3B,OAAO,gBAAgB,CAAA;QACzB;YACE,MAAM,IAAI,KAAK,CAAC,mCAAmC,QAAQ,EAAE,CAAC,CAAA;IAClE,CAAC;AACH,CAAC;AAED,SAAS,iBAAiB,CAAE,KAAyB;IACnD,QAAQ,KAAK,EAAE,CAAC;QACd,KAAK,uCAAkB,CAAC,YAAY,CAAC;QACrC,KAAK,uCAAkB,CAAC,IAAI;YAC1B,OAAO,CAAC,CAAA;QACV;YACE,OAAO,CAAC,CAAA;IACZ,CAAC;AACH,CAAC"}
package/package.json ADDED
@@ -0,0 +1,43 @@
1
+ {
2
+ "name": "@microsoft/agents-copilotstudio-client",
3
+ "version": "0.1.25-gcaee57b821",
4
+ "homepage": "https://github.com/microsoft/Agents-for-js",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "https://github.com/microsoft/Agents-for-js"
8
+ },
9
+ "author": {
10
+ "name": "Microsoft",
11
+ "email": "agentssdk@microsoft.com",
12
+ "url": "https://aka.ms/Agents"
13
+ },
14
+ "description": "Microsoft Copilot Studio Client for JavaScript",
15
+ "keywords": [
16
+ "Agents",
17
+ "copilotstudio",
18
+ "powerplatform"
19
+ ],
20
+ "main": "dist/src/index.js",
21
+ "types": "dist/src/index.d.ts",
22
+ "dependencies": {
23
+ "@microsoft/agents-bot-activity": "0.1.25-gcaee57b821",
24
+ "axios": "^1.8.1",
25
+ "debug": "^4.3.7"
26
+ },
27
+ "license": "MIT",
28
+ "files": [
29
+ "README.md",
30
+ "dist/src",
31
+ "src"
32
+ ],
33
+ "exports": {
34
+ ".": {
35
+ "import": "./dist/src/index.js",
36
+ "require": "./dist/src/index.js"
37
+ },
38
+ "./package.json": "./package.json"
39
+ },
40
+ "engines": {
41
+ "node": ">=18.0.0"
42
+ }
43
+ }
package/src/botType.ts ADDED
@@ -0,0 +1,18 @@
1
+ /**
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+
6
+ /**
7
+ * Enum representing the type of bot.
8
+ */
9
+ export enum BotType {
10
+ /**
11
+ * Represents a published bot.
12
+ */
13
+ Published = 0,
14
+ /**
15
+ * Represents a prebuilt bot.
16
+ */
17
+ Prebuilt = 1
18
+ }
@@ -0,0 +1,40 @@
1
+ /**
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+
6
+ /**
7
+ * Represents the settings required to establish a connection to Copilot Studio.
8
+ */
9
+ export class ConnectionSettings {
10
+ /** The client ID of the application. */
11
+ public appClientId: string = ''
12
+ /** The tenant ID of the application. */
13
+ public tenantId: string = ''
14
+ /** The environment ID of the application. */
15
+ public environmentId: string = ''
16
+ /** The cloud environment of the application. */
17
+ public cloud: string = ''
18
+ /** The custom Power Platform cloud URL, if any. */
19
+ public customPowerPlatformCloud?: string
20
+ /** The identifier of the bot. */
21
+ public botIdentifier?: string
22
+ /** The type of the Copilot bot. */
23
+ public copilotBotType?: string
24
+ }
25
+
26
+ /**
27
+ * Loads the connection settings for Copilot Studio from environment variables.
28
+ * @returns The connection settings.
29
+ */
30
+ export const loadCopilotStudioConnectionSettingsFromEnv: () => ConnectionSettings = () => {
31
+ return {
32
+ appClientId: process.env.appClientId ?? '',
33
+ tenantId: process.env.tenantId ?? '',
34
+ environmentId: process.env.environmentId ?? '',
35
+ cloud: process.env.cloud,
36
+ customPowerPlatformCloud: process.env.customPowerPlatformCloud,
37
+ botIdentifier: process.env.botIdentifier,
38
+ copilotBotType: process.env.copilotBotType
39
+ } as ConnectionSettings
40
+ }
@@ -0,0 +1,134 @@
1
+ /**
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+
6
+ import { ConnectionSettings } from './connectionSettings'
7
+ import axios, { AxiosInstance, AxiosRequestConfig } from 'axios'
8
+ import { getCopilotStudioConnectionUrl } from './powerPlatformEnvironment'
9
+ import { Activity, ActivityTypes, ConversationAccount } from '@microsoft/agents-bot-activity'
10
+ import { ExecuteTurnRequest } from './executeTurnRequest'
11
+ import createDebug, { Debugger } from 'debug'
12
+
13
+ interface streamRead {
14
+ done: boolean,
15
+ value: string
16
+ }
17
+
18
+ export class CopilotStudioClient {
19
+ /** The ID of the current conversation. */
20
+ private conversationId: string = ''
21
+ /** The connection settings for the client. */
22
+ private readonly settings: ConnectionSettings
23
+ /** The Axios instance used for HTTP requests. */
24
+ private readonly client: AxiosInstance
25
+ /** The logger for debugging. */
26
+ private readonly logger: Debugger
27
+
28
+ /**
29
+ * Creates an instance of CopilotStudioClient.
30
+ * @param settings The connection settings.
31
+ * @param token The authentication token.
32
+ */
33
+ constructor (settings: ConnectionSettings, token: string) {
34
+ this.settings = settings
35
+ this.client = axios.create()
36
+ this.client.defaults.headers.common.Authorization = `Bearer ${token}`
37
+ this.logger = createDebug('copilot-studio-client')
38
+ }
39
+
40
+ private async postRequestAsync (axiosConfig: AxiosRequestConfig): Promise<Activity[]> {
41
+ const activities: Activity[] = []
42
+ const response = await this.client(axiosConfig)
43
+ const stream = response.data
44
+ const reader = stream.pipeThrough(new TextDecoderStream()).getReader()
45
+ let result: string = ''
46
+ const results: string[] = []
47
+
48
+ const processEvents = async ({ done, value }: streamRead): Promise<string[]> => {
49
+ if (done) {
50
+ this.logger('Stream complete')
51
+ result += value
52
+ results.push(result)
53
+ return results
54
+ }
55
+ this.logger('Bot is typing...')
56
+ result += value
57
+
58
+ return await processEvents(await reader.read())
59
+ }
60
+
61
+ const events: string[] = await reader.read().then(processEvents)
62
+
63
+ events.forEach(event => {
64
+ const values: string[] = event.toString().split('\n')
65
+ const validEvents = values.filter(e => e.substring(0, 4) === 'data' && e !== 'data: end\r')
66
+ validEvents.forEach(ve => {
67
+ try {
68
+ const act = Activity.fromJson(ve.substring(5, ve.length))
69
+ if (act.type === ActivityTypes.Message) {
70
+ activities.push(act)
71
+ } else {
72
+ this.logger('Activity type: ', act.type)
73
+ }
74
+ } catch (e) {
75
+ this.logger('Error: ', e)
76
+ throw e
77
+ }
78
+ })
79
+ })
80
+ return activities
81
+ }
82
+
83
+ public async startConversationAsync (emitStartConversationEvent: boolean = true): Promise<Activity> {
84
+ const uriStart: string = getCopilotStudioConnectionUrl(this.settings)
85
+ const body = { emitStartConversationEvent }
86
+
87
+ const config: AxiosRequestConfig = {
88
+ method: 'post',
89
+ url: uriStart,
90
+ headers: {
91
+ Accept: 'text/event-stream',
92
+ 'Content-Type': 'application/json'
93
+ },
94
+ data: body,
95
+ responseType: 'stream',
96
+ adapter: 'fetch'
97
+ }
98
+
99
+ const values = await this.postRequestAsync(config)
100
+ const act = values[0]
101
+ this.conversationId = act.conversation?.id!
102
+ return act
103
+ }
104
+
105
+ public async askQuestionAsync (question: string, conversationId: string = this.conversationId) {
106
+ const conversationAccount: ConversationAccount = {
107
+ id: conversationId
108
+ }
109
+ const activityObj = {
110
+ type: 'message',
111
+ text: question,
112
+ conversation: conversationAccount
113
+ }
114
+ const activity = Activity.fromObject(activityObj)
115
+
116
+ const localConversationId = activity.conversation?.id ?? conversationId
117
+ const uriExecute = getCopilotStudioConnectionUrl(this.settings, localConversationId)
118
+ const qbody: ExecuteTurnRequest = new ExecuteTurnRequest(activity)
119
+
120
+ const config: AxiosRequestConfig = {
121
+ method: 'post',
122
+ url: uriExecute,
123
+ headers: {
124
+ Accept: 'text/event-stream',
125
+ 'Content-Type': 'application/json'
126
+ },
127
+ data: qbody,
128
+ responseType: 'stream',
129
+ adapter: 'fetch'
130
+ }
131
+ const values = await this.postRequestAsync(config)
132
+ return values
133
+ }
134
+ }
@@ -0,0 +1,23 @@
1
+ /**
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+
6
+ import { BotType } from './botType'
7
+ import { PowerPlatformCloud } from './powerPlatformCloud'
8
+
9
+ /**
10
+ * Represents the settings required to establish a direct connection to the engine.
11
+ */
12
+ export interface DirectToEngineConnectionSettings {
13
+ /** The identifier of the bot. */
14
+ botIdentifier: string
15
+ /** The custom Power Platform cloud URL. */
16
+ customPowerPlatformCloud: string
17
+ /** The environment ID of the application. */
18
+ environmentId: string
19
+ /** The cloud environment of the application. */
20
+ cloud: PowerPlatformCloud
21
+ /** The type of the Copilot bot. */
22
+ copilotBotType: BotType
23
+ }
@@ -0,0 +1,19 @@
1
+ /**
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+
6
+ import { Activity } from '@microsoft/agents-bot-activity'
7
+
8
+ export class ExecuteTurnRequest {
9
+ /** The activity to be executed. */
10
+ activity?: Activity
11
+
12
+ /**
13
+ * Creates an instance of ExecuteTurnRequest.
14
+ * @param activity The activity to be executed.
15
+ */
16
+ constructor (activity?: Activity) {
17
+ this.activity = activity
18
+ }
19
+ }
package/src/index.ts ADDED
@@ -0,0 +1,7 @@
1
+ export * from './botType'
2
+ export * from './connectionSettings'
3
+ export * from './copilotStudioClient'
4
+ export * from './directToEngineConnectionSettings'
5
+ export * from './executeTurnRequest'
6
+ export * from './powerPlatformCloud'
7
+ export * from './powerPlatformEnvironment'
@@ -0,0 +1,78 @@
1
+ /**
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+
6
+ /**
7
+ * Enum representing different Power Platform cloud environments.
8
+ */
9
+ export enum PowerPlatformCloud {
10
+ /**
11
+ * Unknown cloud environment.
12
+ */
13
+ Unknown = -1,
14
+ /**
15
+ * Experimental cloud environment.
16
+ */
17
+ Exp = 0,
18
+ /**
19
+ * Development cloud environment.
20
+ */
21
+ Dev = 1,
22
+ /**
23
+ * Test cloud environment.
24
+ */
25
+ Test = 2,
26
+ /**
27
+ * Pre-production cloud environment.
28
+ */
29
+ Preprod = 3,
30
+ /**
31
+ * First release cloud environment.
32
+ */
33
+ FirstRelease = 4,
34
+ /**
35
+ * Production cloud environment.
36
+ */
37
+ Prod = 5,
38
+ /**
39
+ * Government cloud environment.
40
+ */
41
+ Gov = 6,
42
+ /**
43
+ * High security cloud environment.
44
+ */
45
+ High = 7,
46
+ /**
47
+ * Department of Defense cloud environment.
48
+ */
49
+ DoD = 8,
50
+ /**
51
+ * Mooncake cloud environment.
52
+ */
53
+ Mooncake = 9,
54
+ /**
55
+ * Ex cloud environment.
56
+ */
57
+ Ex = 10,
58
+ /**
59
+ * Rx cloud environment.
60
+ */
61
+ Rx = 11,
62
+ /**
63
+ * Private cloud environment.
64
+ */
65
+ Prv = 12,
66
+ /**
67
+ * Local cloud environment.
68
+ */
69
+ Local = 13,
70
+ /**
71
+ * French government cloud environment.
72
+ */
73
+ GovFR = 14,
74
+ /**
75
+ * Other cloud environment.
76
+ */
77
+ Other = 100
78
+ }