@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,175 @@
1
+ /**
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+
6
+ import { BotType } from './botType'
7
+ import { ConnectionSettings } from './connectionSettings'
8
+ import { PowerPlatformCloud } from './powerPlatformCloud'
9
+
10
+ const ApiVersion: string = '2022-03-01-preview'
11
+
12
+ /**
13
+ * Generates the connection URL for Copilot Studio.
14
+ * @param settings - The connection settings.
15
+ * @param conversationId - Optional conversation ID.
16
+ * @returns The connection URL.
17
+ * @throws Will throw an error if required settings are missing or invalid.
18
+ */
19
+ export function getCopilotStudioConnectionUrl (
20
+ settings: ConnectionSettings,
21
+ conversationId?: string
22
+ ): string {
23
+ let cloudValue: PowerPlatformCloud = PowerPlatformCloud.Prod
24
+
25
+ const isNotEmptyCloud = settings.cloud && settings.cloud.trim() !== ''
26
+ const isNotEmptyCustomPowerPlatformCloud = settings.customPowerPlatformCloud !== undefined && settings.customPowerPlatformCloud.trim() !== ''
27
+
28
+ if (isNotEmptyCloud && !Object.values(PowerPlatformCloud).includes(settings.cloud)) {
29
+ throw new Error('Invalid PowerPlatformCloud enum key')
30
+ }
31
+
32
+ const cloudSetting = isNotEmptyCloud ? PowerPlatformCloud[settings.cloud as keyof typeof PowerPlatformCloud] : PowerPlatformCloud.Unknown
33
+
34
+ if (cloudSetting === PowerPlatformCloud.Other && isNotEmptyCustomPowerPlatformCloud) {
35
+ throw new Error('customPowerPlatformCloud must be provided when PowerPlatformCloud is Other')
36
+ }
37
+
38
+ if (settings.environmentId.trim() === '') {
39
+ throw new Error('EnvironmentId must be provided')
40
+ }
41
+
42
+ if (settings.botIdentifier === undefined || settings.botIdentifier.trim() === '') {
43
+ throw new Error('BotIdentifier must be provided')
44
+ }
45
+
46
+ if (cloudSetting !== PowerPlatformCloud.Unknown) {
47
+ cloudValue = cloudSetting
48
+ }
49
+
50
+ if (cloudSetting === PowerPlatformCloud.Other) {
51
+ if (isNotEmptyCustomPowerPlatformCloud && isValidUri(settings.customPowerPlatformCloud!)) {
52
+ cloudValue = PowerPlatformCloud.Other
53
+ } else {
54
+ throw new Error(
55
+ 'customPowerPlatformCloud must be provided when PowerPlatformCloud is Other'
56
+ )
57
+ }
58
+ }
59
+
60
+ let botType: BotType
61
+
62
+ if (settings.copilotBotType && settings.copilotBotType.trim() !== '') {
63
+ if (!Object.values(BotType).includes(settings.copilotBotType as unknown as BotType)) {
64
+ throw new Error('Invalid BotType enum key')
65
+ } else {
66
+ botType = BotType[settings.copilotBotType as keyof typeof BotType]
67
+ }
68
+ } else {
69
+ botType = BotType.Published
70
+ }
71
+
72
+ settings.customPowerPlatformCloud = isNotEmptyCustomPowerPlatformCloud ? settings.customPowerPlatformCloud : 'api.unknown.powerplatform.com'
73
+
74
+ const host = getEnvironmentEndpoint(cloudValue, settings.environmentId, settings.customPowerPlatformCloud)
75
+ return createUri(settings.botIdentifier, host, botType, conversationId)
76
+ }
77
+
78
+ function isValidUri (uri: string): boolean {
79
+ try {
80
+ const newUri = new URL(uri)
81
+ return !!newUri
82
+ } catch {
83
+ return false
84
+ }
85
+ }
86
+
87
+ function createUri (
88
+ botIdentifier: string,
89
+ host: string,
90
+ botType: BotType,
91
+ conversationId?: string
92
+ ): string {
93
+ const botPathName = botType === BotType.Published ? 'dataverse-backed' : 'prebuilt'
94
+
95
+ const url = new URL(`https://${host}`)
96
+ url.searchParams.set('api-version', ApiVersion)
97
+
98
+ if (!conversationId) {
99
+ url.pathname = `/copilotstudio/${botPathName}/authenticated/bots/${botIdentifier}/conversations`
100
+ } else {
101
+ url.pathname = `/copilotstudio/${botPathName}/authenticated/bots/${botIdentifier}/conversations/${conversationId}`
102
+ }
103
+
104
+ return url.toString()
105
+ }
106
+
107
+ function getEnvironmentEndpoint (
108
+ cloud: PowerPlatformCloud,
109
+ environmentId: string,
110
+ cloudBaseAddress?: string
111
+ ): string {
112
+ if (cloud === PowerPlatformCloud.Other && (!cloudBaseAddress || !cloudBaseAddress.trim())) {
113
+ throw new Error('cloudBaseAddress must be provided when PowerPlatformCloud is Other')
114
+ }
115
+
116
+ cloudBaseAddress = cloudBaseAddress ?? 'api.unknown.powerplatform.com'
117
+
118
+ const normalizedResourceId = environmentId.toLowerCase().replaceAll('-', '')
119
+ const idSuffixLength = getIdSuffixLength(cloud)
120
+ const hexPrefix = normalizedResourceId.substring(0, normalizedResourceId.length - idSuffixLength)
121
+ const hexSuffix = normalizedResourceId.substring(normalizedResourceId.length - idSuffixLength)
122
+
123
+ return `${hexPrefix}.${hexSuffix}.environment.${getEndpointSuffix(cloud, cloudBaseAddress)}`
124
+ }
125
+
126
+ function getEndpointSuffix (
127
+ category: PowerPlatformCloud,
128
+ cloudBaseAddress: string
129
+ ): string {
130
+ switch (category) {
131
+ case PowerPlatformCloud.Local:
132
+ return 'api.powerplatform.localhost'
133
+ case PowerPlatformCloud.Exp:
134
+ return 'api.exp.powerplatform.com'
135
+ case PowerPlatformCloud.Dev:
136
+ return 'api.dev.powerplatform.com'
137
+ case PowerPlatformCloud.Prv:
138
+ return 'api.prv.powerplatform.com'
139
+ case PowerPlatformCloud.Test:
140
+ return 'api.test.powerplatform.com'
141
+ case PowerPlatformCloud.Preprod:
142
+ return 'api.preprod.powerplatform.com'
143
+ case PowerPlatformCloud.FirstRelease:
144
+ case PowerPlatformCloud.Prod:
145
+ return 'api.powerplatform.com'
146
+ case PowerPlatformCloud.GovFR:
147
+ return 'api.gov.powerplatform.microsoft.us'
148
+ case PowerPlatformCloud.Gov:
149
+ return 'api.gov.powerplatform.microsoft.us'
150
+ case PowerPlatformCloud.High:
151
+ return 'api.high.powerplatform.microsoft.us'
152
+ case PowerPlatformCloud.DoD:
153
+ return 'api.appsplatform.us'
154
+ case PowerPlatformCloud.Mooncake:
155
+ return 'api.powerplatform.partner.microsoftonline.cn'
156
+ case PowerPlatformCloud.Ex:
157
+ return 'api.powerplatform.eaglex.ic.gov'
158
+ case PowerPlatformCloud.Rx:
159
+ return 'api.powerplatform.microsoft.scloud'
160
+ case PowerPlatformCloud.Other:
161
+ return cloudBaseAddress
162
+ default:
163
+ throw new Error(`Invalid cluster category value: ${category}`)
164
+ }
165
+ }
166
+
167
+ function getIdSuffixLength (cloud: PowerPlatformCloud): number {
168
+ switch (cloud) {
169
+ case PowerPlatformCloud.FirstRelease:
170
+ case PowerPlatformCloud.Prod:
171
+ return 2
172
+ default:
173
+ return 1
174
+ }
175
+ }