@microsoft/agents-hosting 1.6.0-beta.9.gce9d8facd2 → 1.7.0-beta.1.g8bcd4f11a7
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.
- package/dist/package.json +4 -9
- package/dist/src/agent-client/agentClient.js +2 -2
- package/dist/src/agent-client/agentClient.js.map +1 -1
- package/dist/src/agent-client/agentResponseHandler.js +2 -2
- package/dist/src/agent-client/agentResponseHandler.js.map +1 -1
- package/dist/src/app/agentApplication.js +28 -2
- package/dist/src/app/agentApplication.js.map +1 -1
- package/dist/src/app/agentApplicationOptions.d.ts +10 -0
- package/dist/src/app/attachmentDownloader.js +5 -10
- package/dist/src/app/attachmentDownloader.js.map +1 -1
- package/dist/src/app/auth/authorizationManager.d.ts +4 -0
- package/dist/src/app/auth/authorizationManager.js +94 -67
- package/dist/src/app/auth/authorizationManager.js.map +1 -1
- package/dist/src/app/auth/handlers/azureBotAuthorization.js +2 -2
- package/dist/src/app/auth/handlers/azureBotAuthorization.js.map +1 -1
- package/dist/src/app/proactive/createConversationOptionsBuilder.js +10 -6
- package/dist/src/app/proactive/createConversationOptionsBuilder.js.map +1 -1
- package/dist/src/app/proactive/proactive.d.ts +1 -0
- package/dist/src/app/proactive/proactive.js +30 -15
- package/dist/src/app/proactive/proactive.js.map +1 -1
- package/dist/src/app/streaming/streamingResponse.d.ts +11 -0
- package/dist/src/app/streaming/streamingResponse.js +21 -0
- package/dist/src/app/streaming/streamingResponse.js.map +1 -1
- package/dist/src/app/teamsAttachmentDownloader.js +6 -14
- package/dist/src/app/teamsAttachmentDownloader.js.map +1 -1
- package/dist/src/auth/MemoryCache.d.ts +2 -1
- package/dist/src/auth/MemoryCache.js +7 -1
- package/dist/src/auth/MemoryCache.js.map +1 -1
- package/dist/src/auth/authConfiguration.d.ts +2 -143
- package/dist/src/auth/authConfiguration.js +336 -257
- package/dist/src/auth/authConfiguration.js.map +1 -1
- package/dist/src/auth/authConstants.d.ts +7 -7
- package/dist/src/auth/authConstants.js.map +1 -1
- package/dist/src/auth/connectionManager.d.ts +92 -0
- package/dist/src/auth/{msalConnectionManager.js → connectionManager.js} +48 -33
- package/dist/src/auth/connectionManager.js.map +1 -0
- package/dist/src/auth/index.d.ts +5 -3
- package/dist/src/auth/index.js +5 -3
- package/dist/src/auth/index.js.map +1 -1
- package/dist/src/auth/jwt-middleware.d.ts +6 -0
- package/dist/src/auth/jwt-middleware.js +34 -2
- package/dist/src/auth/jwt-middleware.js.map +1 -1
- package/dist/src/auth/msal/msalConnectionManager.d.ts +17 -0
- package/dist/src/auth/msal/msalConnectionManager.js +50 -0
- package/dist/src/auth/msal/msalConnectionManager.js.map +1 -0
- package/dist/src/auth/msal/msalConnectionSettings.d.ts +68 -0
- package/dist/src/auth/msal/msalConnectionSettings.js +7 -0
- package/dist/src/auth/msal/msalConnectionSettings.js.map +1 -0
- package/dist/src/auth/{msalTokenCredential.d.ts → msal/msalTokenCredential.d.ts} +1 -1
- package/dist/src/auth/{msalTokenCredential.js → msal/msalTokenCredential.js} +2 -2
- package/dist/src/auth/msal/msalTokenCredential.js.map +1 -0
- package/dist/src/auth/{msalTokenProvider.d.ts → msal/msalTokenProvider.d.ts} +30 -3
- package/dist/src/auth/msal/msalTokenProvider.js +733 -0
- package/dist/src/auth/msal/msalTokenProvider.js.map +1 -0
- package/dist/src/auth/settings.d.ts +336 -0
- package/dist/src/auth/settings.js +202 -0
- package/dist/src/auth/settings.js.map +1 -0
- package/dist/src/auth/sidecar/sidecarAuthProvider.d.ts +74 -0
- package/dist/src/auth/sidecar/sidecarAuthProvider.js +181 -0
- package/dist/src/auth/sidecar/sidecarAuthProvider.js.map +1 -0
- package/dist/src/auth/sidecar/sidecarConnectionSettings.d.ts +65 -0
- package/dist/src/auth/sidecar/sidecarConnectionSettings.js +7 -0
- package/dist/src/auth/sidecar/sidecarConnectionSettings.js.map +1 -0
- package/dist/src/auth/sidecar/sidecarHttpClient.d.ts +95 -0
- package/dist/src/auth/sidecar/sidecarHttpClient.js +406 -0
- package/dist/src/auth/sidecar/sidecarHttpClient.js.map +1 -0
- package/dist/src/auth/sidecar/sidecarModels.d.ts +110 -0
- package/dist/src/auth/sidecar/sidecarModels.js +48 -0
- package/dist/src/auth/sidecar/sidecarModels.js.map +1 -0
- package/dist/src/auth/sidecar/sidecarTokenExpiry.d.ts +13 -0
- package/dist/src/auth/sidecar/sidecarTokenExpiry.js +39 -0
- package/dist/src/auth/sidecar/sidecarTokenExpiry.js.map +1 -0
- package/dist/src/cloudAdapter.d.ts +58 -1
- package/dist/src/cloudAdapter.js +233 -53
- package/dist/src/cloudAdapter.js.map +1 -1
- package/dist/src/connector-client/connectorClient.d.ts +12 -8
- package/dist/src/connector-client/connectorClient.js +99 -82
- package/dist/src/connector-client/connectorClient.js.map +1 -1
- package/dist/src/errorHelper.js +181 -7
- package/dist/src/errorHelper.js.map +1 -1
- package/dist/src/getProductInfo.d.ts +23 -0
- package/dist/src/getProductInfo.js +71 -1
- package/dist/src/getProductInfo.js.map +1 -1
- package/dist/src/headerPropagation.d.ts +17 -2
- package/dist/src/headerPropagation.js +46 -24
- package/dist/src/headerPropagation.js.map +1 -1
- package/dist/src/httpClient.d.ts +60 -0
- package/dist/src/httpClient.js +173 -0
- package/dist/src/httpClient.js.map +1 -0
- package/dist/src/index.d.ts +3 -2
- package/dist/src/index.js +8 -2
- package/dist/src/index.js.map +1 -1
- package/dist/src/oauth/userTokenClient.d.ts +17 -4
- package/dist/src/oauth/userTokenClient.js +119 -80
- package/dist/src/oauth/userTokenClient.js.map +1 -1
- package/dist/src/observability/traces.d.ts +11 -4
- package/dist/src/observability/traces.js +39 -7
- package/dist/src/observability/traces.js.map +1 -1
- package/dist/src/utils/env.d.ts +50 -0
- package/dist/src/utils/env.js +113 -0
- package/dist/src/utils/env.js.map +1 -0
- package/dist/src/utils.d.ts +10 -0
- package/dist/src/utils.js +18 -0
- package/dist/src/utils.js.map +1 -0
- package/package.json +4 -9
- package/src/agent-client/agentClient.ts +2 -2
- package/src/agent-client/agentResponseHandler.ts +2 -2
- package/src/app/agentApplication.ts +30 -3
- package/src/app/agentApplicationOptions.ts +11 -0
- package/src/app/attachmentDownloader.ts +6 -8
- package/src/app/auth/authorizationManager.ts +84 -41
- package/src/app/auth/handlers/azureBotAuthorization.ts +2 -2
- package/src/app/proactive/createConversationOptionsBuilder.ts +8 -4
- package/src/app/proactive/proactive.ts +39 -23
- package/src/app/streaming/streamingResponse.ts +24 -1
- package/src/app/teamsAttachmentDownloader.ts +7 -11
- package/src/auth/MemoryCache.ts +7 -1
- package/src/auth/authConfiguration.ts +350 -356
- package/src/auth/authConstants.ts +7 -7
- package/src/auth/{msalConnectionManager.ts → connectionManager.ts} +65 -44
- package/src/auth/index.ts +5 -3
- package/src/auth/jwt-middleware.ts +32 -2
- package/src/auth/msal/msalConnectionManager.ts +55 -0
- package/src/auth/msal/msalConnectionSettings.ts +79 -0
- package/src/auth/{msalTokenCredential.ts → msal/msalTokenCredential.ts} +2 -1
- package/src/auth/msal/msalTokenProvider.ts +881 -0
- package/src/auth/settings.ts +378 -0
- package/src/auth/sidecar/sidecarAuthProvider.ts +234 -0
- package/src/auth/sidecar/sidecarConnectionSettings.ts +72 -0
- package/src/auth/sidecar/sidecarHttpClient.ts +443 -0
- package/src/auth/sidecar/sidecarModels.ts +153 -0
- package/src/auth/sidecar/sidecarTokenExpiry.ts +35 -0
- package/src/cloudAdapter.ts +292 -13
- package/src/connector-client/connectorClient.ts +116 -94
- package/src/errorHelper.ts +206 -7
- package/src/getProductInfo.ts +79 -1
- package/src/headerPropagation.ts +55 -23
- package/src/httpClient.ts +223 -0
- package/src/index.ts +6 -2
- package/src/oauth/userTokenClient.ts +137 -84
- package/src/observability/traces.ts +39 -8
- package/src/utils/env.ts +105 -0
- package/src/utils.ts +14 -0
- package/dist/src/auth/msalConnectionManager.d.ts +0 -64
- package/dist/src/auth/msalConnectionManager.js.map +0 -1
- package/dist/src/auth/msalTokenCredential.js.map +0 -1
- package/dist/src/auth/msalTokenProvider.js +0 -485
- package/dist/src/auth/msalTokenProvider.js.map +0 -1
- package/src/auth/msalTokenProvider.ts +0 -562
package/src/getProductInfo.ts
CHANGED
|
@@ -1,5 +1,15 @@
|
|
|
1
|
+
// Copyright (c) Microsoft Corporation. All rights reserved.
|
|
2
|
+
// Licensed under the MIT License.
|
|
3
|
+
|
|
1
4
|
import { version } from '../package.json'
|
|
2
5
|
import os from 'os'
|
|
6
|
+
import { Activity, ExceptionHelper } from '@microsoft/agents-activity'
|
|
7
|
+
import { Errors } from './errorHelper'
|
|
8
|
+
import { HeaderPropagationCollection } from './headerPropagation'
|
|
9
|
+
|
|
10
|
+
const AGENT_NAME_PATTERN = /^[A-Za-z0-9 _-]+$/
|
|
11
|
+
const AGENT_REGISTRAR = 'A365'
|
|
12
|
+
const DEFAULT_AGENT_NAME = 'Agents-SDK-JS'
|
|
3
13
|
|
|
4
14
|
/**
|
|
5
15
|
* Generates a string containing information about the SDK version and runtime environment.
|
|
@@ -7,4 +17,72 @@ import os from 'os'
|
|
|
7
17
|
*
|
|
8
18
|
* @returns A formatted string containing the SDK version, Node.js version, and OS details
|
|
9
19
|
*/
|
|
10
|
-
export const getProductInfo = () : string =>
|
|
20
|
+
export const getProductInfo = () : string => `${DEFAULT_AGENT_NAME.toLowerCase()}/${version} nodejs/${process.version} ${os.platform()}-${os.arch()}/${os.release()}`
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Applies the SDK product information to the User-Agent header of outgoing requests.
|
|
24
|
+
* @param headers The HeaderPropagationCollection to which the User-Agent header will be added or updated
|
|
25
|
+
*/
|
|
26
|
+
export function applyUserAgentHeader (headers: HeaderPropagationCollection): void {
|
|
27
|
+
const userAgentKey = headers.key?.('User-Agent') ?? 'User-Agent'
|
|
28
|
+
const userAgent = headers.outgoing[userAgentKey] ?? headers.incoming[userAgentKey]
|
|
29
|
+
const productInfo = getProductInfo()
|
|
30
|
+
if (!userAgent) {
|
|
31
|
+
headers.add({ [userAgentKey]: productInfo })
|
|
32
|
+
} else if (!userAgent.includes(productInfo)) {
|
|
33
|
+
headers.concat({ [userAgentKey]: productInfo })
|
|
34
|
+
} else {
|
|
35
|
+
headers.propagate([userAgentKey])
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Applies standardized agent-related headers for agentic requests.
|
|
41
|
+
*
|
|
42
|
+
* @param headers The HeaderPropagationCollection to which the agent headers will be applied.
|
|
43
|
+
* @param activity The incoming Activity containing agentic request information.
|
|
44
|
+
* @param agentName An optional human-friendly name for the agent, which will be validated and included in the headers.
|
|
45
|
+
*
|
|
46
|
+
* @remarks
|
|
47
|
+
* The function sets the following headers:
|
|
48
|
+
* - `AgentRegistrar`: A fixed value indicating the registrar of the agent (e.g., "A365").
|
|
49
|
+
* - `AgentName`: A human-friendly name for the agent, validated against a pattern to ensure it only contains allowed characters.
|
|
50
|
+
* - `AgentID`: A unique identifier for the agent instance, resolved from the activity's agentic instance ID or agentic user.
|
|
51
|
+
* - `Agent-Referrer`: The channel ID from the incoming activity, indicating the source of the request.
|
|
52
|
+
* The function ensures that the agent headers are consistently applied to outgoing requests for proper identification and tracing.
|
|
53
|
+
*/
|
|
54
|
+
export function applyAgenticHeaders (
|
|
55
|
+
headers: HeaderPropagationCollection,
|
|
56
|
+
activity: Activity,
|
|
57
|
+
agentName?: string
|
|
58
|
+
): void {
|
|
59
|
+
headers.override({
|
|
60
|
+
AgentRegistrar: AGENT_REGISTRAR,
|
|
61
|
+
AgentName: normalizeAgentName(agentName),
|
|
62
|
+
AgentID: resolveAgentId(activity),
|
|
63
|
+
'Agent-Referrer': activity.channelId ?? '',
|
|
64
|
+
})
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function resolveAgentId (activity: Activity): string {
|
|
68
|
+
const agentId = activity.getAgenticInstanceId() ?? activity.getAgenticUser()
|
|
69
|
+
|
|
70
|
+
if (!agentId) {
|
|
71
|
+
throw ExceptionHelper.generateException(Error, Errors.AgentIdRequired)
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return agentId
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function normalizeAgentName (agentName?: string): string {
|
|
78
|
+
const normalizedAgentName = agentName?.trim()
|
|
79
|
+
if (!normalizedAgentName) {
|
|
80
|
+
return DEFAULT_AGENT_NAME
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
if (!AGENT_NAME_PATTERN.test(normalizedAgentName)) {
|
|
84
|
+
throw ExceptionHelper.generateException(Error, Errors.AgentNameInvalid, undefined, { agentName: normalizedAgentName })
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
return normalizedAgentName
|
|
88
|
+
}
|
package/src/headerPropagation.ts
CHANGED
|
@@ -8,10 +8,11 @@
|
|
|
8
8
|
* It filters the incoming request headers based on the definition provided and loads them into the outgoing headers collection.
|
|
9
9
|
*/
|
|
10
10
|
export class HeaderPropagation implements HeaderPropagationCollection {
|
|
11
|
+
private _keys: string[] = []
|
|
11
12
|
private _incomingRequests: Record<string, string>
|
|
12
13
|
private _outgoingHeaders: Record<string, string> = {}
|
|
13
14
|
|
|
14
|
-
private
|
|
15
|
+
private _protectedHeaders = ['x-ms-correlation-id']
|
|
15
16
|
|
|
16
17
|
public get incoming (): Record<string, string> {
|
|
17
18
|
return this._incomingRequests
|
|
@@ -27,54 +28,78 @@ export class HeaderPropagation implements HeaderPropagationCollection {
|
|
|
27
28
|
}
|
|
28
29
|
|
|
29
30
|
this._incomingRequests = this.normalizeHeaders(headers)
|
|
30
|
-
|
|
31
|
+
|
|
32
|
+
// Ensure protected headers are propagated from incoming to outgoing by default without allowing modifications.
|
|
33
|
+
const protectedHeaders = [...this._protectedHeaders]
|
|
34
|
+
this._protectedHeaders = [] // Temporarily clear protected headers to allow propagation without restriction
|
|
35
|
+
this.propagate(protectedHeaders)
|
|
36
|
+
this._protectedHeaders = protectedHeaders // Restore protected headers list after propagation
|
|
31
37
|
}
|
|
32
38
|
|
|
33
39
|
propagate (headers: string[]) {
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
this._outgoingHeaders[lowerKey] = this._incomingRequests[lowerKey]
|
|
40
|
+
this.process(headers.map(h => [h, '']), key => {
|
|
41
|
+
if (this._incomingRequests[key] && !this._outgoingHeaders[key]) {
|
|
42
|
+
this._outgoingHeaders[key] = this._incomingRequests[key]
|
|
38
43
|
}
|
|
39
|
-
}
|
|
44
|
+
})
|
|
40
45
|
}
|
|
41
46
|
|
|
42
47
|
add (headers: Record<string, string>) {
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
this._outgoingHeaders[lowerKey] = value
|
|
48
|
+
this.process(Object.entries(headers ?? {}), (key, value) => {
|
|
49
|
+
if (!this._incomingRequests[key] && !this._outgoingHeaders[key]) {
|
|
50
|
+
this._outgoingHeaders[key] = value
|
|
47
51
|
}
|
|
48
|
-
}
|
|
52
|
+
})
|
|
49
53
|
}
|
|
50
54
|
|
|
51
55
|
concat (headers: Record<string, string>) {
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
this._outgoingHeaders[lowerKey] = `${this._outgoingHeaders[lowerKey] ?? this._incomingRequests[lowerKey]} ${value}`.trim()
|
|
56
|
+
this.process(Object.entries(headers ?? {}), (key, value) => {
|
|
57
|
+
if (this._incomingRequests[key] || this._outgoingHeaders[key]) {
|
|
58
|
+
this._outgoingHeaders[key] = `${this._outgoingHeaders[key] ?? this._incomingRequests[key]} ${value}`.trim()
|
|
56
59
|
}
|
|
57
|
-
}
|
|
60
|
+
})
|
|
58
61
|
}
|
|
59
62
|
|
|
60
63
|
override (headers: Record<string, string>) {
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
64
|
+
this.process(Object.entries(headers ?? {}), (key, value) => {
|
|
65
|
+
this._outgoingHeaders[key] = value
|
|
66
|
+
})
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
key (key: string) {
|
|
70
|
+
return this._keys.find(k => k.toLowerCase() === key.toLowerCase())
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* A helper method to process headers based on a provided callback function. It iterates over the headers and applies the callback to each header key and value.
|
|
75
|
+
* @param headers An array of header key-value pairs to process.
|
|
76
|
+
* @param callback A function that takes a header key and value, allowing for custom processing logic to be applied to each header.
|
|
77
|
+
*/
|
|
78
|
+
private process (headers: [string, string][], callback: (key: string, value: string) => void) {
|
|
79
|
+
for (const [key, value] of headers) {
|
|
80
|
+
const savedKey = this.key(key)
|
|
81
|
+
const realKey = savedKey ?? key
|
|
82
|
+
if (!savedKey) {
|
|
83
|
+
this._keys.push(realKey)
|
|
84
|
+
}
|
|
85
|
+
if (!this._protectedHeaders.includes(key.toLowerCase())) {
|
|
86
|
+
callback(realKey, value)
|
|
65
87
|
}
|
|
66
88
|
}
|
|
67
89
|
}
|
|
68
90
|
|
|
69
91
|
/**
|
|
70
|
-
* Normalizes the headers by
|
|
92
|
+
* Normalizes the headers by ensuring the values are strings.
|
|
71
93
|
* @param headers The headers to normalize.
|
|
72
94
|
* @returns A new object with normalized headers.
|
|
73
95
|
*/
|
|
74
96
|
private normalizeHeaders (headers: Record<string, string | string[] | undefined>) {
|
|
75
97
|
return Object.entries(headers).reduce((acc, [key, value]) => {
|
|
76
98
|
if (value) {
|
|
77
|
-
acc[key
|
|
99
|
+
acc[key] = Array.isArray(value) ? value.join(' ') : value
|
|
100
|
+
if (!this._keys.includes(key)) {
|
|
101
|
+
this._keys.push(key) // Store the original casing of the header keys
|
|
102
|
+
}
|
|
78
103
|
}
|
|
79
104
|
return acc
|
|
80
105
|
}, {} as Record<string, string>)
|
|
@@ -138,4 +163,11 @@ export interface HeaderPropagationCollection {
|
|
|
138
163
|
* If the header does not exist in the incoming headers, it will be added to the outgoing collection.
|
|
139
164
|
*/
|
|
140
165
|
override(headers: Record<string, string>): void
|
|
166
|
+
/**
|
|
167
|
+
* Resolves the actual header key in a case-insensitive manner.
|
|
168
|
+
* This is useful for ensuring that headers are accessed correctly regardless of their case in the incoming request.
|
|
169
|
+
* @param key The header key to resolve.
|
|
170
|
+
* @returns The resolved header key or undefined if not found.
|
|
171
|
+
*/
|
|
172
|
+
key?(key: string): string | undefined
|
|
141
173
|
}
|
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Microsoft Corporation. All rights reserved.
|
|
3
|
+
* Licensed under the MIT License.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { ExceptionHelper } from '@microsoft/agents-activity'
|
|
7
|
+
import { Readable } from 'node:stream'
|
|
8
|
+
import { Errors } from './errorHelper'
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Configuration for an HTTP request.
|
|
12
|
+
*/
|
|
13
|
+
export interface HttpRequestConfig {
|
|
14
|
+
method: string
|
|
15
|
+
url: string
|
|
16
|
+
headers?: Record<string, string>
|
|
17
|
+
data?: unknown
|
|
18
|
+
params?: Record<string, string | undefined>
|
|
19
|
+
responseType?: 'json' | 'arraybuffer' | 'stream'
|
|
20
|
+
timeout?: number
|
|
21
|
+
signal?: AbortSignal
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Represents an HTTP response.
|
|
26
|
+
*/
|
|
27
|
+
export interface HttpResponse<T = unknown> {
|
|
28
|
+
data: T
|
|
29
|
+
status: number
|
|
30
|
+
statusText: string
|
|
31
|
+
headers: Headers
|
|
32
|
+
config: HttpRequestConfig
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Options for creating an HttpClient instance.
|
|
37
|
+
*/
|
|
38
|
+
export interface HttpClientOptions {
|
|
39
|
+
baseURL?: string
|
|
40
|
+
headers?: Record<string, string>
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* A lightweight HTTP client built on native fetch.
|
|
45
|
+
*/
|
|
46
|
+
export class HttpClient {
|
|
47
|
+
private _baseURL: string
|
|
48
|
+
private _defaultHeaders: Record<string, string>
|
|
49
|
+
|
|
50
|
+
constructor (options: HttpClientOptions = {}) {
|
|
51
|
+
this._baseURL = options.baseURL ?? ''
|
|
52
|
+
this._defaultHeaders = this.normalizeHeaders(options.headers)
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
get baseURL (): string {
|
|
56
|
+
return this._baseURL
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
get defaultHeaders (): Record<string, string> {
|
|
60
|
+
return this._defaultHeaders
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
set defaultHeaders (headers: Record<string, string>) {
|
|
64
|
+
this._defaultHeaders = this.normalizeHeaders(headers)
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
setHeader (name: string, value: string): void {
|
|
68
|
+
this._defaultHeaders[name.toLowerCase()] = value
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
async request<T = unknown> (config: HttpRequestConfig): Promise<HttpResponse<T>> {
|
|
72
|
+
const url = this.buildUrl(config.url, config.params)
|
|
73
|
+
const headers = this.normalizeHeaders({ ...this._defaultHeaders, ...config.headers })
|
|
74
|
+
const requestHeaders = new Headers(headers)
|
|
75
|
+
const fetchOptions: RequestInit = {
|
|
76
|
+
method: config.method.toUpperCase(),
|
|
77
|
+
headers: requestHeaders,
|
|
78
|
+
signal: config.signal ??
|
|
79
|
+
(config.timeout ? AbortSignal.timeout(config.timeout) : undefined)
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
if (config.data !== undefined && config.data !== null) {
|
|
83
|
+
const contentType = requestHeaders.get('content-type') ?? ''
|
|
84
|
+
if (contentType.includes('application/x-www-form-urlencoded')) {
|
|
85
|
+
fetchOptions.body = new URLSearchParams(config.data as Record<string, string>).toString()
|
|
86
|
+
} else if (config.data instanceof URLSearchParams) {
|
|
87
|
+
if (!contentType) {
|
|
88
|
+
requestHeaders.set('content-type', 'application/x-www-form-urlencoded;charset=utf-8')
|
|
89
|
+
}
|
|
90
|
+
fetchOptions.body = config.data.toString()
|
|
91
|
+
} else if (typeof config.data === 'string') {
|
|
92
|
+
fetchOptions.body = config.data
|
|
93
|
+
} else if (config.data instanceof Uint8Array || config.data instanceof ArrayBuffer ||
|
|
94
|
+
config.data instanceof Blob || config.data instanceof FormData ||
|
|
95
|
+
config.data instanceof ReadableStream) {
|
|
96
|
+
fetchOptions.body = config.data
|
|
97
|
+
} else {
|
|
98
|
+
if (!contentType) {
|
|
99
|
+
requestHeaders.set('content-type', 'application/json')
|
|
100
|
+
}
|
|
101
|
+
fetchOptions.body = JSON.stringify(config.data)
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
const response = await fetch(url, fetchOptions)
|
|
106
|
+
|
|
107
|
+
let data: T
|
|
108
|
+
if (config.responseType === 'stream') {
|
|
109
|
+
if (!response.ok) {
|
|
110
|
+
data = await response.text() as T
|
|
111
|
+
} else {
|
|
112
|
+
data = Readable.fromWeb(response.body ?? new ReadableStream()) as T
|
|
113
|
+
}
|
|
114
|
+
} else if (config.responseType === 'arraybuffer') {
|
|
115
|
+
data = await response.arrayBuffer() as T
|
|
116
|
+
} else {
|
|
117
|
+
const text = await response.text()
|
|
118
|
+
try {
|
|
119
|
+
data = JSON.parse(text) as T
|
|
120
|
+
} catch {
|
|
121
|
+
data = text as T
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
const httpResponse: HttpResponse<T> = {
|
|
126
|
+
data,
|
|
127
|
+
status: response.status,
|
|
128
|
+
statusText: response.statusText,
|
|
129
|
+
headers: response.headers,
|
|
130
|
+
config,
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
if (!response.ok) {
|
|
134
|
+
const error: HttpError = new HttpError(
|
|
135
|
+
`Request failed with status ${response.status}`,
|
|
136
|
+
httpResponse,
|
|
137
|
+
config
|
|
138
|
+
)
|
|
139
|
+
throw error
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
return httpResponse
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
async get<T = unknown> (url: string, options?: Partial<HttpRequestConfig>): Promise<HttpResponse<T>> {
|
|
146
|
+
return this.request<T>({ method: 'get', url, ...options })
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
private buildUrl (path: string, params?: Record<string, string | undefined>): string {
|
|
150
|
+
let url: string
|
|
151
|
+
if (path.startsWith('http://') || path.startsWith('https://')) {
|
|
152
|
+
url = path
|
|
153
|
+
} else {
|
|
154
|
+
if (!this._baseURL) {
|
|
155
|
+
throw ExceptionHelper.generateException(Error, Errors.HttpClientRelativeUrlRequiresBaseUrl, undefined, { url: path })
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
const base = this._baseURL.endsWith('/') ? this._baseURL.slice(0, -1) : this._baseURL
|
|
159
|
+
const cleanPath = path.startsWith('/') ? path : `/${path}`
|
|
160
|
+
url = `${base}${cleanPath}`
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
if (params) {
|
|
164
|
+
const searchParams = new URLSearchParams()
|
|
165
|
+
for (const [key, value] of Object.entries(params)) {
|
|
166
|
+
if (value !== undefined && value !== null) {
|
|
167
|
+
searchParams.set(key, value)
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
const queryString = searchParams.toString()
|
|
171
|
+
if (queryString) {
|
|
172
|
+
url += `${url.includes('?') ? '&' : '?'}${queryString}`
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
return url
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
private normalizeHeaders (headers?: Record<string, string>): Record<string, string> {
|
|
180
|
+
if (!headers) {
|
|
181
|
+
return {}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
return Object.entries(headers).reduce((normalized, [name, value]) => {
|
|
185
|
+
normalized[name.toLowerCase()] = value
|
|
186
|
+
return normalized
|
|
187
|
+
}, {} as Record<string, string>)
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* Error thrown when an HTTP request fails.
|
|
193
|
+
*/
|
|
194
|
+
export class HttpError extends Error {
|
|
195
|
+
public readonly response: HttpResponse
|
|
196
|
+
public readonly config: HttpRequestConfig
|
|
197
|
+
public readonly status: number
|
|
198
|
+
|
|
199
|
+
constructor (message: string, response: HttpResponse, config: HttpRequestConfig) {
|
|
200
|
+
super(message)
|
|
201
|
+
this.name = 'HttpError'
|
|
202
|
+
this.response = response
|
|
203
|
+
this.config = config
|
|
204
|
+
this.status = response.status
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
toJSON (): Record<string, unknown> {
|
|
208
|
+
return {
|
|
209
|
+
message: this.message,
|
|
210
|
+
status: this.status,
|
|
211
|
+
config: {
|
|
212
|
+
url: this.config.url,
|
|
213
|
+
method: this.config.method,
|
|
214
|
+
data: this.config.data,
|
|
215
|
+
},
|
|
216
|
+
response: {
|
|
217
|
+
status: this.response.status,
|
|
218
|
+
statusText: this.response.statusText,
|
|
219
|
+
data: this.response.data,
|
|
220
|
+
},
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
6
|
export * from './auth/'
|
|
7
|
-
export { authorizeJWT, buildJwksUri } from './auth/jwt-middleware'
|
|
7
|
+
export { authorizeJWT, buildJwksUri, clearJwksClients } from './auth/jwt-middleware'
|
|
8
8
|
|
|
9
9
|
export * from './app'
|
|
10
10
|
export * from './cards'
|
|
@@ -18,7 +18,10 @@ export * from './transcript'
|
|
|
18
18
|
|
|
19
19
|
export * from './activityHandler'
|
|
20
20
|
export * from './baseAdapter'
|
|
21
|
-
export
|
|
21
|
+
// Note: re-export the public surface of `cloudAdapter` explicitly so the
|
|
22
|
+
// test-only internals (e.g., the per-process warning dedup set) do not leak
|
|
23
|
+
// into the package's public API.
|
|
24
|
+
export { CloudAdapter, type CloudAdapterOptions } from './cloudAdapter'
|
|
22
25
|
export * from './middlewareSet'
|
|
23
26
|
export * from './messageFactory'
|
|
24
27
|
export * from './statusCodes'
|
|
@@ -26,5 +29,6 @@ export * from './turnContext'
|
|
|
26
29
|
export * from './turnContextStateCollection'
|
|
27
30
|
export * from './storage/storage'
|
|
28
31
|
export * from './headerPropagation'
|
|
32
|
+
export * from './httpClient'
|
|
29
33
|
|
|
30
34
|
export * from './agent-client'
|