openai 7.7.0 → 7.8.0
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/CHANGELOG.md +18 -0
- package/README.md +15 -30
- package/auth/index.d.mts +1 -1
- package/auth/index.d.mts.map +1 -1
- package/auth/index.d.ts +1 -1
- package/auth/index.d.ts.map +1 -1
- package/auth/index.js.map +1 -1
- package/auth/index.mjs.map +1 -1
- package/auth/types.d.mts +9 -4
- package/auth/types.d.mts.map +1 -1
- package/auth/types.d.ts +9 -4
- package/auth/types.d.ts.map +1 -1
- package/auth/workload-identity-auth.d.mts +1 -1
- package/auth/workload-identity-auth.d.mts.map +1 -1
- package/auth/workload-identity-auth.d.ts +1 -1
- package/auth/workload-identity-auth.d.ts.map +1 -1
- package/auth/workload-identity-auth.js +23 -6
- package/auth/workload-identity-auth.js.map +1 -1
- package/auth/workload-identity-auth.mjs +23 -6
- package/auth/workload-identity-auth.mjs.map +1 -1
- package/auth/x509-transport.d.mts +36 -1
- package/auth/x509-transport.d.mts.map +1 -1
- package/auth/x509-transport.d.ts +36 -1
- package/auth/x509-transport.d.ts.map +1 -1
- package/auth/x509-transport.js +223 -0
- package/auth/x509-transport.js.map +1 -1
- package/auth/x509-transport.mjs +222 -1
- package/auth/x509-transport.mjs.map +1 -1
- package/azure.d.mts +3 -1
- package/azure.d.mts.map +1 -1
- package/azure.d.ts +3 -1
- package/azure.d.ts.map +1 -1
- package/azure.js.map +1 -1
- package/azure.mjs.map +1 -1
- package/bedrock.d.mts +3 -1
- package/bedrock.d.mts.map +1 -1
- package/bedrock.d.ts +3 -1
- package/bedrock.d.ts.map +1 -1
- package/bedrock.js.map +1 -1
- package/bedrock.mjs.map +1 -1
- package/beta/realtime/ws.d.mts.map +1 -1
- package/beta/realtime/ws.d.ts.map +1 -1
- package/beta/realtime/ws.js +2 -0
- package/beta/realtime/ws.js.map +1 -1
- package/beta/realtime/ws.mjs +2 -0
- package/beta/realtime/ws.mjs.map +1 -1
- package/client.d.mts +3 -1
- package/client.d.mts.map +1 -1
- package/client.d.ts +3 -1
- package/client.d.ts.map +1 -1
- package/client.js +30 -37
- package/client.js.map +1 -1
- package/client.mjs +31 -38
- package/client.mjs.map +1 -1
- package/core/EventEmitter.d.mts.map +1 -1
- package/core/EventEmitter.d.ts.map +1 -1
- package/core/EventEmitter.js +14 -1
- package/core/EventEmitter.js.map +1 -1
- package/core/EventEmitter.mjs +14 -1
- package/core/EventEmitter.mjs.map +1 -1
- package/internal/auth/x509-api-origin.d.mts +5 -0
- package/internal/auth/x509-api-origin.d.mts.map +1 -0
- package/internal/auth/x509-api-origin.d.ts +5 -0
- package/internal/auth/x509-api-origin.d.ts.map +1 -0
- package/internal/auth/x509-api-origin.js +28 -0
- package/internal/auth/x509-api-origin.js.map +1 -0
- package/internal/auth/x509-api-origin.mjs +24 -0
- package/internal/auth/x509-api-origin.mjs.map +1 -0
- package/internal/auth/x509-credential-options.d.mts +13 -0
- package/internal/auth/x509-credential-options.d.mts.map +1 -0
- package/internal/auth/x509-credential-options.d.ts +13 -0
- package/internal/auth/x509-credential-options.d.ts.map +1 -0
- package/internal/auth/x509-credential-options.js +122 -0
- package/internal/auth/x509-credential-options.js.map +1 -0
- package/internal/auth/x509-credential-options.mjs +118 -0
- package/internal/auth/x509-credential-options.mjs.map +1 -0
- package/internal/auth/x509-token-exchange.mjs +1 -1
- package/internal/auth/x509-transport-capability.d.mts +12 -5
- package/internal/auth/x509-transport-capability.d.mts.map +1 -1
- package/internal/auth/x509-transport-capability.d.ts +12 -5
- package/internal/auth/x509-transport-capability.d.ts.map +1 -1
- package/internal/auth/x509-transport-capability.js +5 -4
- package/internal/auth/x509-transport-capability.js.map +1 -1
- package/internal/auth/x509-transport-capability.mjs +6 -5
- package/internal/auth/x509-transport-capability.mjs.map +1 -1
- package/internal/auth/x509-transport-registry.d.mts +8 -0
- package/internal/auth/x509-transport-registry.d.mts.map +1 -1
- package/internal/auth/x509-transport-registry.d.ts +8 -0
- package/internal/auth/x509-transport-registry.d.ts.map +1 -1
- package/internal/auth/x509-transport-registry.js.map +1 -1
- package/internal/auth/x509-transport-registry.mjs +1 -1
- package/internal/auth/x509-transport-registry.mjs.map +1 -1
- package/internal/auth/x509-transport-state-browser.d.mts +4 -0
- package/internal/auth/x509-transport-state-browser.d.mts.map +1 -1
- package/internal/auth/x509-transport-state-browser.d.ts +4 -0
- package/internal/auth/x509-transport-state-browser.d.ts.map +1 -1
- package/internal/auth/x509-transport-state-browser.js +6 -1
- package/internal/auth/x509-transport-state-browser.js.map +1 -1
- package/internal/auth/x509-transport-state-browser.mjs +5 -0
- package/internal/auth/x509-transport-state-browser.mjs.map +1 -1
- package/internal/auth/x509-transport-state.cjs +6 -1
- package/internal/auth/x509-transport-state.cjs.map +1 -1
- package/internal/auth/x509-transport-state.d.cts +4 -0
- package/internal/auth/x509-transport-state.d.cts.map +1 -1
- package/internal/auth/x509-transport-state.js +36 -0
- package/internal/auth/x509-transport-state.mjs +19 -0
- package/internal/auth/x509-workload-identity-auth.d.mts +2 -4
- package/internal/auth/x509-workload-identity-auth.d.mts.map +1 -1
- package/internal/auth/x509-workload-identity-auth.d.ts +2 -4
- package/internal/auth/x509-workload-identity-auth.d.ts.map +1 -1
- package/internal/auth/x509-workload-identity-auth.js +43 -34
- package/internal/auth/x509-workload-identity-auth.js.map +1 -1
- package/internal/auth/x509-workload-identity-auth.mjs +40 -30
- package/internal/auth/x509-workload-identity-auth.mjs.map +1 -1
- package/internal/uploads.js +4 -2
- package/internal/uploads.js.map +1 -1
- package/internal/uploads.mjs +4 -2
- package/internal/uploads.mjs.map +1 -1
- package/internal/utils/log.d.mts +6 -0
- package/internal/utils/log.d.mts.map +1 -1
- package/internal/utils/log.d.ts +6 -0
- package/internal/utils/log.d.ts.map +1 -1
- package/internal/utils/log.js +72 -11
- package/internal/utils/log.js.map +1 -1
- package/internal/utils/log.mjs +69 -11
- package/internal/utils/log.mjs.map +1 -1
- package/lib/EventEmitter.d.mts.map +1 -1
- package/lib/EventEmitter.d.ts.map +1 -1
- package/lib/EventEmitter.js +14 -1
- package/lib/EventEmitter.js.map +1 -1
- package/lib/EventEmitter.mjs +14 -1
- package/lib/EventEmitter.mjs.map +1 -1
- package/package.json +5 -5
- package/realtime/ws.d.mts.map +1 -1
- package/realtime/ws.d.ts.map +1 -1
- package/realtime/ws.js +2 -0
- package/realtime/ws.js.map +1 -1
- package/realtime/ws.mjs +2 -0
- package/realtime/ws.mjs.map +1 -1
- package/resources/admin/organization/audit-logs.d.mts +2 -2
- package/resources/admin/organization/audit-logs.d.mts.map +1 -1
- package/resources/admin/organization/audit-logs.d.ts +2 -2
- package/resources/admin/organization/audit-logs.d.ts.map +1 -1
- package/resources/beta/responses/responses.d.mts +4 -0
- package/resources/beta/responses/responses.d.mts.map +1 -1
- package/resources/beta/responses/responses.d.ts +4 -0
- package/resources/beta/responses/responses.d.ts.map +1 -1
- package/resources/beta/responses/responses.js.map +1 -1
- package/resources/beta/responses/responses.mjs.map +1 -1
- package/resources/beta/responses/ws.d.mts.map +1 -1
- package/resources/beta/responses/ws.d.ts.map +1 -1
- package/resources/beta/responses/ws.js +2 -0
- package/resources/beta/responses/ws.js.map +1 -1
- package/resources/beta/responses/ws.mjs +2 -0
- package/resources/beta/responses/ws.mjs.map +1 -1
- package/resources/completions.d.mts +4 -0
- package/resources/completions.d.mts.map +1 -1
- package/resources/completions.d.ts +4 -0
- package/resources/completions.d.ts.map +1 -1
- package/resources/responses/responses.d.mts +4 -0
- package/resources/responses/responses.d.mts.map +1 -1
- package/resources/responses/responses.d.ts +4 -0
- package/resources/responses/responses.d.ts.map +1 -1
- package/resources/responses/responses.js.map +1 -1
- package/resources/responses/responses.mjs.map +1 -1
- package/resources/responses/ws.d.mts.map +1 -1
- package/resources/responses/ws.d.ts.map +1 -1
- package/resources/responses/ws.js +2 -0
- package/resources/responses/ws.js.map +1 -1
- package/resources/responses/ws.mjs +2 -0
- package/resources/responses/ws.mjs.map +1 -1
- package/src/_vendor/partial-json-parser/LICENSE +21 -0
- package/src/_vendor/partial-json-parser/README.md +11 -1
- package/src/auth/index.ts +1 -0
- package/src/auth/types.ts +10 -4
- package/src/auth/workload-identity-auth.ts +30 -6
- package/src/auth/x509-transport.ts +311 -2
- package/src/azure.ts +4 -1
- package/src/bedrock.ts +4 -1
- package/src/beta/realtime/ws.ts +2 -0
- package/src/client.ts +39 -34
- package/src/core/EventEmitter.ts +13 -1
- package/src/internal/auth/x509-api-origin.ts +27 -0
- package/src/internal/auth/x509-credential-options.ts +146 -0
- package/src/internal/auth/x509-transport-capability.ts +12 -5
- package/src/internal/auth/x509-transport-registry.ts +9 -0
- package/src/internal/auth/x509-transport-state-browser.ts +7 -0
- package/src/internal/auth/x509-transport-state.cts +7 -0
- package/src/internal/auth/x509-workload-identity-auth.ts +42 -33
- package/src/internal/uploads.ts +4 -2
- package/src/internal/utils/log.ts +76 -11
- package/src/lib/EventEmitter.ts +13 -1
- package/src/realtime/ws.ts +2 -0
- package/src/resources/admin/organization/audit-logs.ts +4 -0
- package/src/resources/beta/responses/responses.ts +5 -0
- package/src/resources/beta/responses/ws.ts +3 -0
- package/src/resources/completions.ts +5 -0
- package/src/resources/responses/responses.ts +5 -0
- package/src/resources/responses/ws.ts +3 -0
- package/src/version.ts +1 -1
- package/version.d.mts +1 -1
- package/version.d.ts +1 -1
- package/version.js +1 -1
- package/version.mjs +1 -1
package/src/auth/types.ts
CHANGED
|
@@ -21,7 +21,7 @@ export interface WorkloadIdentity {
|
|
|
21
21
|
/** Provider that obtains the external subject token for each token exchange. */
|
|
22
22
|
provider: SubjectTokenProvider;
|
|
23
23
|
|
|
24
|
-
/** Seconds before expiration when access-token refresh begins; defaults to 1,200 seconds. */
|
|
24
|
+
/** Seconds before expiration when access-token refresh begins; defaults to 1,200 seconds and is capped at half of the actual token lifetime. */
|
|
25
25
|
refreshBufferSeconds?: number;
|
|
26
26
|
}
|
|
27
27
|
|
|
@@ -36,7 +36,10 @@ export interface X509WorkloadIdentity {
|
|
|
36
36
|
/** OpenAI service account authorized for the verified certificate identity. */
|
|
37
37
|
serviceAccountId: string;
|
|
38
38
|
|
|
39
|
-
/**
|
|
39
|
+
/** Seconds before expiration when access-token refresh begins; defaults to 1,200 seconds. */
|
|
40
|
+
refreshBufferSeconds?: number;
|
|
41
|
+
|
|
42
|
+
/** @deprecated Use refreshBufferSeconds to match other workload-identity credentials. */
|
|
40
43
|
refreshBufferMs?: number;
|
|
41
44
|
|
|
42
45
|
/** X.509 federation proves certificate possession instead of supplying a subject token. */
|
|
@@ -44,9 +47,12 @@ export interface X509WorkloadIdentity {
|
|
|
44
47
|
|
|
45
48
|
/** X.509 federation does not send an OAuth client identifier. */
|
|
46
49
|
clientId?: never;
|
|
50
|
+
}
|
|
47
51
|
|
|
48
|
-
|
|
49
|
-
|
|
52
|
+
/** An SDK-owned certificate credential created by the Node-only X.509 authentication helper. */
|
|
53
|
+
export interface X509Credential {
|
|
54
|
+
/** Closes the credential's owned certificate transport after requests have finished. */
|
|
55
|
+
close: () => Promise<void>;
|
|
50
56
|
}
|
|
51
57
|
|
|
52
58
|
/** OAuth token-exchange response returned by the OpenAI workload-identity endpoint. */
|
|
@@ -6,6 +6,7 @@ import { APIError, OAuthError, OpenAIError } from '../core/error';
|
|
|
6
6
|
interface CachedToken {
|
|
7
7
|
token: string;
|
|
8
8
|
expiresAt: number;
|
|
9
|
+
refreshAt: number;
|
|
9
10
|
}
|
|
10
11
|
|
|
11
12
|
const SUBJECT_TOKEN_TYPES: Record<WorkloadIdentity['provider']['tokenType'], string> = {
|
|
@@ -14,6 +15,20 @@ const SUBJECT_TOKEN_TYPES: Record<WorkloadIdentity['provider']['tokenType'], str
|
|
|
14
15
|
};
|
|
15
16
|
|
|
16
17
|
const TOKEN_EXCHANGE_GRANT_TYPE = 'urn:ietf:params:oauth:grant-type:token-exchange';
|
|
18
|
+
// Cap the refresh buffer at half the actual token lifetime, matching the X.509
|
|
19
|
+
// workload-identity path, so short-lived tokens keep a usable cache window.
|
|
20
|
+
const MAX_REFRESH_BUFFER_FRACTION = 0.5;
|
|
21
|
+
|
|
22
|
+
function calculateRefreshAt(
|
|
23
|
+
expiresAt: number,
|
|
24
|
+
now: number,
|
|
25
|
+
refreshBufferSeconds: number | undefined,
|
|
26
|
+
): number {
|
|
27
|
+
const configuredBufferMs = (refreshBufferSeconds ?? 1200) * 1000;
|
|
28
|
+
const effectiveBufferMs = Math.min(configuredBufferMs, (expiresAt - now) * MAX_REFRESH_BUFFER_FRACTION);
|
|
29
|
+
return expiresAt - effectiveBufferMs;
|
|
30
|
+
}
|
|
31
|
+
|
|
17
32
|
const NATIVE_RESPONSE_PROTOTYPE = Response.prototype;
|
|
18
33
|
const READ_NATIVE_RESPONSE_BODY = NATIVE_RESPONSE_PROTOTYPE.arrayBuffer;
|
|
19
34
|
|
|
@@ -133,7 +148,17 @@ export class WorkloadIdentityAuth {
|
|
|
133
148
|
* @param fetch Optional fetch implementation for calls to the OpenAI token endpoint.
|
|
134
149
|
*/
|
|
135
150
|
constructor(config: WorkloadIdentity, fetch?: Fetch) {
|
|
136
|
-
|
|
151
|
+
const { identityProviderId, serviceAccountId, clientId, refreshBufferSeconds, provider } = config;
|
|
152
|
+
this.config = {
|
|
153
|
+
identityProviderId,
|
|
154
|
+
serviceAccountId,
|
|
155
|
+
...(clientId === undefined ? {} : { clientId }),
|
|
156
|
+
...(refreshBufferSeconds === undefined ? {} : { refreshBufferSeconds }),
|
|
157
|
+
provider: {
|
|
158
|
+
tokenType: provider.tokenType,
|
|
159
|
+
getToken: provider.getToken.bind(provider),
|
|
160
|
+
},
|
|
161
|
+
};
|
|
137
162
|
this.fetch = fetch ?? Shims.getDefaultFetch();
|
|
138
163
|
}
|
|
139
164
|
|
|
@@ -165,7 +190,7 @@ export class WorkloadIdentityAuth {
|
|
|
165
190
|
}
|
|
166
191
|
}
|
|
167
192
|
|
|
168
|
-
if (
|
|
193
|
+
if (WorkloadIdentityAuth.needsRefresh(this.cachedToken) && !this.refreshPromise) {
|
|
169
194
|
const refreshPromise = this.refreshToken(this.tokenGeneration).finally(() => {
|
|
170
195
|
if (this.refreshPromise === refreshPromise) {
|
|
171
196
|
this.refreshPromise = null;
|
|
@@ -250,6 +275,7 @@ export class WorkloadIdentityAuth {
|
|
|
250
275
|
this.cachedToken = {
|
|
251
276
|
token: accessToken,
|
|
252
277
|
expiresAt,
|
|
278
|
+
refreshAt: calculateRefreshAt(expiresAt, now, this.config.refreshBufferSeconds),
|
|
253
279
|
};
|
|
254
280
|
}
|
|
255
281
|
|
|
@@ -260,10 +286,8 @@ export class WorkloadIdentityAuth {
|
|
|
260
286
|
return Date.now() >= cachedToken.expiresAt;
|
|
261
287
|
}
|
|
262
288
|
|
|
263
|
-
private needsRefresh(cachedToken: CachedToken): boolean {
|
|
264
|
-
|
|
265
|
-
const bufferMs = bufferSeconds * 1000;
|
|
266
|
-
return Date.now() >= cachedToken.expiresAt - bufferMs;
|
|
289
|
+
private static needsRefresh(cachedToken: CachedToken): boolean {
|
|
290
|
+
return Date.now() >= cachedToken.refreshAt;
|
|
267
291
|
}
|
|
268
292
|
|
|
269
293
|
/** Discards the cached access token so the next request performs a fresh exchange. */
|
|
@@ -1,15 +1,284 @@
|
|
|
1
1
|
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
2
|
+
import { createPrivateKey, X509Certificate } from 'node:crypto';
|
|
2
3
|
import { setTimeout as delay } from 'node:timers/promises';
|
|
4
|
+
import { types } from 'node:util';
|
|
5
|
+
import { Agent, ProxyAgent } from 'undici';
|
|
3
6
|
import {
|
|
4
7
|
createX509Transport as createCapability,
|
|
5
8
|
registerX509Transport,
|
|
6
9
|
sendX509Request,
|
|
7
10
|
} from '../internal/auth/x509-transport-capability';
|
|
8
|
-
import type {
|
|
11
|
+
import type {
|
|
12
|
+
X509ProxyMode,
|
|
13
|
+
X509Transport,
|
|
14
|
+
X509TransportOptions,
|
|
15
|
+
} from '../internal/auth/x509-transport-capability';
|
|
9
16
|
import { exchangeX509Token } from '../internal/auth/x509-token-exchange';
|
|
10
17
|
import { isRetryableX509TransportFailure } from '../internal/auth/x509-transport-registry';
|
|
11
18
|
import type { X509RequestScope } from '../internal/auth/x509-transport-registry';
|
|
12
|
-
import { markTransientX509ConnectionError } from '#x509-transport-state';
|
|
19
|
+
import { markTransientX509ConnectionError, rememberX509Credential } from '#x509-transport-state';
|
|
20
|
+
import type { X509Credential, X509WorkloadIdentity } from './types';
|
|
21
|
+
|
|
22
|
+
/** Explicit, separately trusted CONNECT configuration for an SDK-owned X.509 credential. */
|
|
23
|
+
export interface X509CredentialProxyOptions {
|
|
24
|
+
/** CONNECT proxy endpoint; its protocol must match the selected mode. */
|
|
25
|
+
url: string | URL;
|
|
26
|
+
|
|
27
|
+
/** Whether the connection to the CONNECT proxy itself is encrypted. */
|
|
28
|
+
mode: Exclude<X509ProxyMode, 'direct'>;
|
|
29
|
+
|
|
30
|
+
/** Optional private trust roots for the HTTPS proxy; never used for workload TLS. */
|
|
31
|
+
ca?: string | string[] | undefined;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** Private certificate material and enrolled selectors for one SDK-owned workload credential. */
|
|
35
|
+
export interface X509CredentialOptions {
|
|
36
|
+
/** Leaf client certificate followed by its required PEM intermediate chain. */
|
|
37
|
+
certificateChain: string;
|
|
38
|
+
|
|
39
|
+
/** PEM private key matching the leaf client certificate. */
|
|
40
|
+
privateKey: string;
|
|
41
|
+
|
|
42
|
+
/** Existing OpenAI identity-provider resource enrolled for the certificate. */
|
|
43
|
+
identityProviderId: string;
|
|
44
|
+
|
|
45
|
+
/** OpenAI service account authorized for the verified certificate identity. */
|
|
46
|
+
serviceAccountId: string;
|
|
47
|
+
|
|
48
|
+
/** Optional private certificate authorities trusted for OpenAI's issuer and API. */
|
|
49
|
+
ca?: string | string[] | undefined;
|
|
50
|
+
|
|
51
|
+
/** Optional passphrase used to decrypt an encrypted PEM private key. */
|
|
52
|
+
passphrase?: string | undefined;
|
|
53
|
+
|
|
54
|
+
/** Optional CONNECT proxy with separately scoped target and proxy TLS settings. */
|
|
55
|
+
proxy?: X509CredentialProxyOptions | undefined;
|
|
56
|
+
|
|
57
|
+
/** Seconds before expiration when access-token refresh begins; defaults to 1,200. */
|
|
58
|
+
refreshBufferSeconds?: number | undefined;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const credentialOptionNames = new Set([
|
|
62
|
+
'certificateChain',
|
|
63
|
+
'privateKey',
|
|
64
|
+
'identityProviderId',
|
|
65
|
+
'serviceAccountId',
|
|
66
|
+
'ca',
|
|
67
|
+
'passphrase',
|
|
68
|
+
'proxy',
|
|
69
|
+
'refreshBufferSeconds',
|
|
70
|
+
]);
|
|
71
|
+
const proxyOptionNames = new Set(['url', 'mode', 'ca']);
|
|
72
|
+
|
|
73
|
+
function safeOptionRecord(
|
|
74
|
+
value: unknown,
|
|
75
|
+
allowed: ReadonlySet<string>,
|
|
76
|
+
label: string,
|
|
77
|
+
): Record<string, unknown> {
|
|
78
|
+
if (!value || typeof value !== 'object' || types.isProxy(value)) {
|
|
79
|
+
throw new Error(`X.509 ${label} options must be a non-proxy object.`);
|
|
80
|
+
}
|
|
81
|
+
const prototype: unknown = Object.getPrototypeOf(value);
|
|
82
|
+
if (prototype !== Object.prototype && prototype !== null) {
|
|
83
|
+
throw new Error(`X.509 ${label} options must have only own plain data properties.`);
|
|
84
|
+
}
|
|
85
|
+
const snapshot = Object.create(null) as Record<string, unknown>;
|
|
86
|
+
for (const name of Reflect.ownKeys(value)) {
|
|
87
|
+
if (typeof name !== 'string' || !allowed.has(name)) {
|
|
88
|
+
throw new Error(`Unsupported X.509 ${label} option: \`${String(name)}\`.`);
|
|
89
|
+
}
|
|
90
|
+
const descriptor = Object.getOwnPropertyDescriptor(value, name);
|
|
91
|
+
if (!descriptor || !('value' in descriptor)) {
|
|
92
|
+
throw new Error(`X.509 ${label} option \`${name}\` must be a plain data property.`);
|
|
93
|
+
}
|
|
94
|
+
snapshot[name] = descriptor.value;
|
|
95
|
+
}
|
|
96
|
+
return snapshot;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function requiredCredentialValue(options: Record<string, unknown>, name: string): string {
|
|
100
|
+
const value = options[name];
|
|
101
|
+
if (typeof value !== 'string' || value.trim().length === 0) {
|
|
102
|
+
throw new Error(`X.509 credential requires a nonempty own \`${name}\` value.`);
|
|
103
|
+
}
|
|
104
|
+
return value;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function snapshotCertificateAuthorities(value: unknown): string | string[] | undefined {
|
|
108
|
+
if (value === undefined) {
|
|
109
|
+
return undefined;
|
|
110
|
+
}
|
|
111
|
+
if (typeof value === 'string') {
|
|
112
|
+
if (value.trim().length === 0) {
|
|
113
|
+
throw new Error('X.509 certificate authorities must contain nonempty PEM values.');
|
|
114
|
+
}
|
|
115
|
+
return value;
|
|
116
|
+
}
|
|
117
|
+
if (!Array.isArray(value) || types.isProxy(value) || value.length === 0) {
|
|
118
|
+
throw new Error('X.509 certificate authorities must be a PEM string or plain PEM string array.');
|
|
119
|
+
}
|
|
120
|
+
const authorities: string[] = [];
|
|
121
|
+
for (let index = 0; index < value.length; index += 1) {
|
|
122
|
+
const entry = Object.getOwnPropertyDescriptor(value, String(index));
|
|
123
|
+
if (!entry || !('value' in entry) || typeof entry.value !== 'string' || entry.value.trim().length === 0) {
|
|
124
|
+
throw new Error('X.509 certificate authorities require own plain nonempty PEM strings.');
|
|
125
|
+
}
|
|
126
|
+
authorities.push(entry.value);
|
|
127
|
+
}
|
|
128
|
+
return authorities;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
class OwnedX509Credential implements X509Credential {
|
|
132
|
+
readonly #dispatcher: Agent | ProxyAgent;
|
|
133
|
+
#closing: Promise<void> | undefined;
|
|
134
|
+
|
|
135
|
+
constructor(dispatcher: Agent | ProxyAgent) {
|
|
136
|
+
this.#dispatcher = dispatcher;
|
|
137
|
+
Object.freeze(this);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/** Closes this credential's owned transport once all in-flight requests have drained. */
|
|
141
|
+
close(): Promise<void> {
|
|
142
|
+
this.#closing ??= this.#dispatcher.close();
|
|
143
|
+
return this.#closing;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
interface ValidatedX509CredentialOptions {
|
|
148
|
+
certificateChain: string;
|
|
149
|
+
privateKey: string;
|
|
150
|
+
identityProviderId: string;
|
|
151
|
+
serviceAccountId: string;
|
|
152
|
+
refreshBufferSeconds: number | undefined;
|
|
153
|
+
passphrase: string | undefined;
|
|
154
|
+
ca: string | string[] | undefined;
|
|
155
|
+
proxy: unknown;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
function validatedCredentialOptions(options: X509CredentialOptions): ValidatedX509CredentialOptions {
|
|
159
|
+
const configured = safeOptionRecord(options, credentialOptionNames, 'credential');
|
|
160
|
+
const certificateChain = requiredCredentialValue(configured, 'certificateChain');
|
|
161
|
+
const privateKeyPEM = requiredCredentialValue(configured, 'privateKey');
|
|
162
|
+
const identityProviderId = requiredCredentialValue(configured, 'identityProviderId');
|
|
163
|
+
const serviceAccountId = requiredCredentialValue(configured, 'serviceAccountId');
|
|
164
|
+
const { refreshBufferSeconds, passphrase } = configured;
|
|
165
|
+
if (passphrase !== undefined && typeof passphrase !== 'string') {
|
|
166
|
+
throw new Error('X.509 credential requires a string private-key passphrase.');
|
|
167
|
+
}
|
|
168
|
+
if (
|
|
169
|
+
refreshBufferSeconds !== undefined &&
|
|
170
|
+
(typeof refreshBufferSeconds !== 'number' ||
|
|
171
|
+
!Number.isSafeInteger(refreshBufferSeconds) ||
|
|
172
|
+
refreshBufferSeconds < 0 ||
|
|
173
|
+
!Number.isSafeInteger(refreshBufferSeconds * 1000))
|
|
174
|
+
) {
|
|
175
|
+
throw new Error('X.509 credential requires a nonnegative integer refreshBufferSeconds.');
|
|
176
|
+
}
|
|
177
|
+
const ca = snapshotCertificateAuthorities(configured['ca']);
|
|
178
|
+
|
|
179
|
+
const leaf = new X509Certificate(certificateChain);
|
|
180
|
+
const privateKey = createPrivateKey({
|
|
181
|
+
key: privateKeyPEM,
|
|
182
|
+
...(passphrase === undefined ? {} : { passphrase }),
|
|
183
|
+
});
|
|
184
|
+
if (!leaf.checkPrivateKey(privateKey)) {
|
|
185
|
+
throw new Error('X.509 credential private key must match its leaf client certificate.');
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
return {
|
|
189
|
+
certificateChain,
|
|
190
|
+
privateKey: privateKeyPEM,
|
|
191
|
+
identityProviderId,
|
|
192
|
+
serviceAccountId,
|
|
193
|
+
refreshBufferSeconds,
|
|
194
|
+
passphrase,
|
|
195
|
+
ca,
|
|
196
|
+
proxy: configured['proxy'],
|
|
197
|
+
};
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
interface VerifiedX509TLSOptions {
|
|
201
|
+
cert: string;
|
|
202
|
+
key: string;
|
|
203
|
+
rejectUnauthorized: true;
|
|
204
|
+
passphrase?: string;
|
|
205
|
+
ca?: string | string[];
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
function proxyAuthentication(url: URL): string | undefined {
|
|
209
|
+
if (url.username === '' && url.password === '') {
|
|
210
|
+
return undefined;
|
|
211
|
+
}
|
|
212
|
+
let username: string;
|
|
213
|
+
let password: string;
|
|
214
|
+
try {
|
|
215
|
+
username = decodeURIComponent(url.username);
|
|
216
|
+
password = decodeURIComponent(url.password);
|
|
217
|
+
} catch {
|
|
218
|
+
throw new Error('X.509 CONNECT proxy credentials contain invalid URL encoding.');
|
|
219
|
+
}
|
|
220
|
+
if (username.includes(':')) {
|
|
221
|
+
throw new Error('X.509 CONNECT proxy username cannot contain a colon.');
|
|
222
|
+
}
|
|
223
|
+
return Buffer.from(`${username}:${password}`, 'utf-8').toString('base64');
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
function normalizeProxyURL(value: unknown): URL {
|
|
227
|
+
if (typeof value !== 'string' && (typeof value !== 'object' || value === null || types.isProxy(value))) {
|
|
228
|
+
throw new Error('X.509 CONNECT proxy requires an own URL string or URL value.');
|
|
229
|
+
}
|
|
230
|
+
try {
|
|
231
|
+
return new URL(typeof value === 'string' ? value : URL.prototype.toString.call(value));
|
|
232
|
+
} catch {
|
|
233
|
+
throw new Error('X.509 CONNECT proxy requires a valid proxy URL.');
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
function credentialDispatcher(
|
|
238
|
+
proxyOptionsInput: unknown,
|
|
239
|
+
requestTls: VerifiedX509TLSOptions,
|
|
240
|
+
): { dispatcher: Agent | ProxyAgent; proxy: X509ProxyMode } {
|
|
241
|
+
if (proxyOptionsInput === undefined) {
|
|
242
|
+
return { dispatcher: new Agent({ connect: requestTls }), proxy: 'direct' };
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
const proxyOptions = safeOptionRecord(proxyOptionsInput, proxyOptionNames, 'proxy');
|
|
246
|
+
const url = normalizeProxyURL(proxyOptions['url']);
|
|
247
|
+
const selected = proxyOptions['mode'];
|
|
248
|
+
const proxy: X509ProxyMode =
|
|
249
|
+
selected === 'http-connect' || selected === 'https-connect' ? selected : 'direct';
|
|
250
|
+
if (
|
|
251
|
+
(proxy !== 'http-connect' && proxy !== 'https-connect') ||
|
|
252
|
+
url.protocol !== (proxy === 'https-connect' ? 'https:' : 'http:') ||
|
|
253
|
+
url.pathname !== '/' ||
|
|
254
|
+
url.search ||
|
|
255
|
+
url.hash
|
|
256
|
+
) {
|
|
257
|
+
throw new Error('X.509 CONNECT proxy URL protocol must match its selected secure proxy mode.');
|
|
258
|
+
}
|
|
259
|
+
const proxyCA = snapshotCertificateAuthorities(proxyOptions['ca']);
|
|
260
|
+
if (proxy === 'http-connect' && proxyCA !== undefined) {
|
|
261
|
+
throw new Error('A plaintext X.509 CONNECT proxy cannot configure proxy TLS authorities.');
|
|
262
|
+
}
|
|
263
|
+
const auth = proxyAuthentication(url);
|
|
264
|
+
|
|
265
|
+
return {
|
|
266
|
+
proxy,
|
|
267
|
+
dispatcher: new ProxyAgent({
|
|
268
|
+
uri: url.href,
|
|
269
|
+
...(auth === undefined ? {} : { auth }),
|
|
270
|
+
requestTls,
|
|
271
|
+
...(proxy === 'https-connect'
|
|
272
|
+
? {
|
|
273
|
+
proxyTls: {
|
|
274
|
+
rejectUnauthorized: true,
|
|
275
|
+
...(proxyCA === undefined ? {} : { ca: proxyCA }),
|
|
276
|
+
},
|
|
277
|
+
}
|
|
278
|
+
: {}),
|
|
279
|
+
}),
|
|
280
|
+
};
|
|
281
|
+
}
|
|
13
282
|
|
|
14
283
|
/** Creates one frozen, caller-attested Node.js transport for X.509 workload authentication. */
|
|
15
284
|
export function createX509Transport(options: X509TransportOptions): X509Transport {
|
|
@@ -42,6 +311,46 @@ export function createX509Transport(options: X509TransportOptions): X509Transpor
|
|
|
42
311
|
return capability;
|
|
43
312
|
}
|
|
44
313
|
|
|
314
|
+
/** Creates a first-class certificate credential with SDK-owned, verified TLS and CONNECT policy. */
|
|
315
|
+
export function fromX509(options: X509CredentialOptions): X509Credential {
|
|
316
|
+
const configured = validatedCredentialOptions(options);
|
|
317
|
+
|
|
318
|
+
const requestTls = {
|
|
319
|
+
cert: configured.certificateChain,
|
|
320
|
+
key: configured.privateKey,
|
|
321
|
+
rejectUnauthorized: true as const,
|
|
322
|
+
...(configured.passphrase === undefined ? {} : { passphrase: configured.passphrase }),
|
|
323
|
+
...(configured.ca === undefined ? {} : { ca: configured.ca }),
|
|
324
|
+
};
|
|
325
|
+
const { dispatcher, proxy } = credentialDispatcher(configured.proxy, requestTls);
|
|
326
|
+
|
|
327
|
+
try {
|
|
328
|
+
const transport = createX509Transport({
|
|
329
|
+
runtime: 'node',
|
|
330
|
+
dispatcher,
|
|
331
|
+
certificateIdentity: 'static',
|
|
332
|
+
proxy,
|
|
333
|
+
});
|
|
334
|
+
const identity: X509WorkloadIdentity = Object.freeze({
|
|
335
|
+
type: 'x509',
|
|
336
|
+
identityProviderId: configured.identityProviderId,
|
|
337
|
+
serviceAccountId: configured.serviceAccountId,
|
|
338
|
+
...(configured.refreshBufferSeconds === undefined
|
|
339
|
+
? {}
|
|
340
|
+
: { refreshBufferSeconds: configured.refreshBufferSeconds }),
|
|
341
|
+
});
|
|
342
|
+
const credential = new OwnedX509Credential(dispatcher);
|
|
343
|
+
rememberX509Credential(credential, Object.freeze({ identity, transport }));
|
|
344
|
+
return credential;
|
|
345
|
+
} catch (error) {
|
|
346
|
+
void dispatcher.close();
|
|
347
|
+
throw error;
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
/** Namespaced first-class credential factory, isolated from ordinary browser-safe auth imports. */
|
|
352
|
+
export const workloadIdentity = Object.freeze({ fromX509 });
|
|
353
|
+
|
|
45
354
|
export type {
|
|
46
355
|
X509ProxyMode,
|
|
47
356
|
X509Transport,
|
package/src/azure.ts
CHANGED
|
@@ -13,7 +13,7 @@ import { assertNoDataResidency } from './internal/data-residency';
|
|
|
13
13
|
/** API Client for interfacing with the Azure OpenAI API. */
|
|
14
14
|
export interface AzureClientOptions extends Omit<
|
|
15
15
|
ClientOptions,
|
|
16
|
-
'provider' | 'dataResidency' | 'workloadIdentity' | 'x509Transport'
|
|
16
|
+
'provider' | 'dataResidency' | 'credential' | 'workloadIdentity' | 'x509Transport'
|
|
17
17
|
> {
|
|
18
18
|
/** AzureOpenAI does not support third-party provider configuration. */
|
|
19
19
|
provider?: never;
|
|
@@ -21,6 +21,9 @@ export interface AzureClientOptions extends Omit<
|
|
|
21
21
|
/** OpenAI data residency cannot be combined with Azure routing. */
|
|
22
22
|
dataResidency?: never;
|
|
23
23
|
|
|
24
|
+
/** Azure cannot receive an SDK-owned OpenAI X.509 certificate credential. */
|
|
25
|
+
credential?: never;
|
|
26
|
+
|
|
24
27
|
/** Azure cannot receive OpenAI X.509 workload-identity certificate transports. */
|
|
25
28
|
x509Transport?: never;
|
|
26
29
|
|
package/src/bedrock.ts
CHANGED
|
@@ -20,7 +20,7 @@ import type * as ResponsesAPI from './resources/responses/responses';
|
|
|
20
20
|
/** Configures Amazon Bedrock's OpenAI-compatible endpoint and bearer-token authentication. */
|
|
21
21
|
export interface BedrockClientOptions extends Omit<
|
|
22
22
|
ClientOptions,
|
|
23
|
-
'apiKey' | 'adminAPIKey' | 'baseURL' | 'workloadIdentity' | 'x509Transport' | 'dataResidency'
|
|
23
|
+
'apiKey' | 'adminAPIKey' | 'baseURL' | 'credential' | 'workloadIdentity' | 'x509Transport' | 'dataResidency'
|
|
24
24
|
> {
|
|
25
25
|
/**
|
|
26
26
|
* Bedrock bearer token used for authentication.
|
|
@@ -46,6 +46,9 @@ export interface BedrockClientOptions extends Omit<
|
|
|
46
46
|
/** OpenAI data residency cannot be combined with Bedrock routing. */
|
|
47
47
|
dataResidency?: never;
|
|
48
48
|
|
|
49
|
+
/** Bedrock cannot receive an SDK-owned OpenAI X.509 certificate credential. */
|
|
50
|
+
credential?: never;
|
|
51
|
+
|
|
49
52
|
/**
|
|
50
53
|
* BedrockOpenAI only supports Bedrock bearer token authentication.
|
|
51
54
|
*/
|
package/src/beta/realtime/ws.ts
CHANGED
|
@@ -3,6 +3,7 @@ import { assertBedrockWebSocketOrigin } from '../../internal/bedrock';
|
|
|
3
3
|
import { protectWebSocketOptionsFromCredentialRedirects } from '../../internal/ws';
|
|
4
4
|
import type { AzureOpenAI } from '../../index';
|
|
5
5
|
import { OpenAI } from '../../index';
|
|
6
|
+
import { VERSION } from '../../version';
|
|
6
7
|
import type { RealtimeClientEvent } from '../../resources/beta/realtime/realtime';
|
|
7
8
|
import { OpenAIRealtimeEmitter, buildRealtimeURL, isAzure, parseRealtimeEvent } from './internal-base';
|
|
8
9
|
import type { RealtimeConnectionConfig } from './internal-base';
|
|
@@ -68,6 +69,7 @@ export class OpenAIRealtimeWS extends OpenAIRealtimeEmitter {
|
|
|
68
69
|
assertTrustedRealtimeURL(client, this.url);
|
|
69
70
|
assertBedrockWebSocketOrigin(client, this.url);
|
|
70
71
|
const headers = {
|
|
72
|
+
'User-Agent': `${client.constructor.name}/JS ${VERSION}`,
|
|
71
73
|
...props.options?.headers,
|
|
72
74
|
...(isAzure(client) && !props.__resolvedApiKey ? {} : { Authorization: `Bearer ${client.apiKey}` }),
|
|
73
75
|
'OpenAI-Beta': 'realtime=v1',
|
package/src/client.ts
CHANGED
|
@@ -17,17 +17,20 @@ import { resolveDataResidency, type DataResidency } from './internal/data-reside
|
|
|
17
17
|
export type { DataResidency } from './internal/data-residency';
|
|
18
18
|
import * as Errors from './core/error';
|
|
19
19
|
import * as Pagination from './core/pagination';
|
|
20
|
-
import type { WorkloadIdentity, X509WorkloadIdentity } from './auth/types';
|
|
20
|
+
import type { WorkloadIdentity, X509Credential, X509WorkloadIdentity } from './auth/types';
|
|
21
21
|
import { WorkloadIdentityAuth } from './auth/workload-identity-auth';
|
|
22
|
+
import { X509_API_BASE_URL, assertX509APIOrigin } from './internal/auth/x509-api-origin';
|
|
22
23
|
import {
|
|
23
|
-
X509_API_BASE_URL,
|
|
24
24
|
X509WorkloadIdentityAuth,
|
|
25
|
-
assertX509APIOrigin,
|
|
26
25
|
assertX509RequestOptions,
|
|
27
26
|
isX509WorkloadIdentity,
|
|
28
27
|
snapshotX509RequestOptions,
|
|
29
28
|
} from './internal/auth/x509-workload-identity-auth';
|
|
30
29
|
import type { X509Transport } from './internal/auth/x509-transport-registry';
|
|
30
|
+
import {
|
|
31
|
+
normalizeX509CredentialOptions,
|
|
32
|
+
prepareX509ClientClone,
|
|
33
|
+
} from './internal/auth/x509-credential-options';
|
|
31
34
|
import { isTransientX509ConnectionError, markApprovedX509Client } from '#x509-transport-state';
|
|
32
35
|
import { OAuthError, SubjectTokenProviderError } from './core/error';
|
|
33
36
|
import {
|
|
@@ -258,6 +261,7 @@ import {
|
|
|
258
261
|
formatRequestDetails,
|
|
259
262
|
loggerFor,
|
|
260
263
|
parseLogLevel,
|
|
264
|
+
redactURL,
|
|
261
265
|
} from './internal/utils/log';
|
|
262
266
|
import { isEmptyObj } from './internal/utils/values';
|
|
263
267
|
|
|
@@ -430,6 +434,9 @@ export interface ClientOptions {
|
|
|
430
434
|
/** Approved, frozen Node.js certificate transport required only for X.509 workload identity. */
|
|
431
435
|
x509Transport?: X509Transport | undefined;
|
|
432
436
|
|
|
437
|
+
/** First-class certificate credential created with `fromX509` from `openai/auth/x509-transport`. */
|
|
438
|
+
credential?: X509Credential | undefined;
|
|
439
|
+
|
|
433
440
|
/**
|
|
434
441
|
* Configure this client to use a third-party API provider.
|
|
435
442
|
* Mutually exclusive with top-level authentication and `baseURL` options.
|
|
@@ -457,6 +464,7 @@ export class OpenAI {
|
|
|
457
464
|
private fetch: Fetch;
|
|
458
465
|
#encoder: Opts.RequestEncoder;
|
|
459
466
|
#x509Authentication: X509WorkloadIdentityAuth | undefined;
|
|
467
|
+
#x509Credential: X509Credential | undefined;
|
|
460
468
|
#x509Fetch: Fetch | undefined;
|
|
461
469
|
// Preserve an explicit global selection without storing a second routing URL.
|
|
462
470
|
#explicitDataResidency = false;
|
|
@@ -495,6 +503,8 @@ export class OpenAI {
|
|
|
495
503
|
* @param {boolean} [opts.dangerouslyAllowBrowser=false] - By default, client-side use of this library is not allowed, as it risks exposing your secret API credentials to attackers.
|
|
496
504
|
*/
|
|
497
505
|
constructor(clientOptions: ClientOptions = {}) {
|
|
506
|
+
const { credential, options: normalizedOptions } = normalizeX509CredentialOptions(clientOptions);
|
|
507
|
+
clientOptions = normalizedOptions;
|
|
498
508
|
const residencyBaseURL = resolveDataResidency(clientOptions);
|
|
499
509
|
const provider = clientOptions.provider;
|
|
500
510
|
const {
|
|
@@ -508,6 +518,7 @@ export class OpenAI {
|
|
|
508
518
|
webhookSecret = readEnv('OPENAI_WEBHOOK_SECRET') ?? null,
|
|
509
519
|
workloadIdentity,
|
|
510
520
|
x509Transport,
|
|
521
|
+
credential: _credential,
|
|
511
522
|
...opts
|
|
512
523
|
} = clientOptions as InternalClientOptions;
|
|
513
524
|
if (provider) {
|
|
@@ -601,7 +612,7 @@ export class OpenAI {
|
|
|
601
612
|
this.fetch = options.fetch ?? Shims.getDefaultFetch();
|
|
602
613
|
this.#encoder = Opts.FallbackEncoder;
|
|
603
614
|
|
|
604
|
-
const customHeadersEnv = provider ? undefined : readEnv('OPENAI_CUSTOM_HEADERS');
|
|
615
|
+
const customHeadersEnv = provider || credential ? undefined : readEnv('OPENAI_CUSTOM_HEADERS');
|
|
605
616
|
if (customHeadersEnv) {
|
|
606
617
|
const parsed: Record<string, string> = {};
|
|
607
618
|
for (const line of customHeadersEnv.split('\n')) {
|
|
@@ -620,6 +631,7 @@ export class OpenAI {
|
|
|
620
631
|
const authentication = new X509WorkloadIdentityAuth(x509Identity, x509Transport, organization, project);
|
|
621
632
|
this._workloadIdentityAuth = authentication;
|
|
622
633
|
this.#x509Authentication = authentication;
|
|
634
|
+
this.#x509Credential = credential;
|
|
623
635
|
this.#x509Fetch = authentication.fetch();
|
|
624
636
|
this.fetch = this.#x509Fetch;
|
|
625
637
|
markApprovedX509Client(this);
|
|
@@ -639,8 +651,6 @@ export class OpenAI {
|
|
|
639
651
|
*/
|
|
640
652
|
withOptions(options: Partial<ClientOptions>): this {
|
|
641
653
|
const residencyBaseURL = resolveDataResidency(options);
|
|
642
|
-
const inheritedProvider = this._options.provider;
|
|
643
|
-
const provider = options.provider ?? inheritedProvider;
|
|
644
654
|
const x509Authentication = this.#x509Authentication;
|
|
645
655
|
const inheritedOptions: ClientOptions = {
|
|
646
656
|
...this._options,
|
|
@@ -659,47 +669,41 @@ export class OpenAI {
|
|
|
659
669
|
project: this.project,
|
|
660
670
|
webhookSecret: this.webhookSecret,
|
|
661
671
|
};
|
|
662
|
-
const
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
delete inheritedOptions.fetch;
|
|
669
|
-
delete inheritedOptions.baseURL;
|
|
670
|
-
if (nextX509) {
|
|
671
|
-
inheritedOptions.apiKey = null;
|
|
672
|
-
} else {
|
|
673
|
-
delete inheritedOptions.x509Transport;
|
|
674
|
-
}
|
|
675
|
-
}
|
|
672
|
+
const { credential, provider } = prepareX509ClientClone(
|
|
673
|
+
inheritedOptions,
|
|
674
|
+
options,
|
|
675
|
+
this.#x509Credential,
|
|
676
|
+
x509Authentication !== undefined,
|
|
677
|
+
);
|
|
676
678
|
if (residencyBaseURL !== undefined) {
|
|
677
679
|
delete inheritedOptions.baseURL;
|
|
678
680
|
}
|
|
679
|
-
if (provider) {
|
|
680
|
-
delete inheritedOptions.apiKey;
|
|
681
|
-
delete inheritedOptions.adminAPIKey;
|
|
682
|
-
delete inheritedOptions.workloadIdentity;
|
|
683
|
-
delete inheritedOptions.x509Transport;
|
|
684
|
-
delete inheritedOptions.baseURL;
|
|
685
|
-
if (provider !== inheritedProvider) {
|
|
686
|
-
delete inheritedOptions.organization;
|
|
687
|
-
delete inheritedOptions.project;
|
|
688
|
-
delete inheritedOptions.defaultHeaders;
|
|
689
|
-
}
|
|
690
|
-
}
|
|
691
681
|
|
|
692
682
|
const clientOptions: InternalClientOptions = {
|
|
693
683
|
...inheritedOptions,
|
|
694
684
|
...options,
|
|
685
|
+
credential,
|
|
695
686
|
provider,
|
|
696
687
|
[inheritedDataResidencySelection]:
|
|
697
688
|
this.#explicitDataResidency &&
|
|
698
689
|
residencyBaseURL === undefined &&
|
|
699
690
|
!hasOwn(options, 'baseURL') &&
|
|
691
|
+
options.credential === undefined &&
|
|
700
692
|
!provider,
|
|
701
693
|
};
|
|
702
694
|
const client = new (this.constructor as any as new (props: ClientOptions) => typeof this)(clientOptions);
|
|
695
|
+
if (provider && new URL(client.baseURL).origin !== new URL(this.baseURL).origin) {
|
|
696
|
+
Object.assign(client._options, {
|
|
697
|
+
defaultHeaders: options.defaultHeaders,
|
|
698
|
+
defaultQuery: options.defaultQuery,
|
|
699
|
+
fetchOptions: options.fetchOptions,
|
|
700
|
+
fetch: options.fetch,
|
|
701
|
+
});
|
|
702
|
+
client.fetchOptions = options.fetchOptions;
|
|
703
|
+
client.fetch = options.fetch ?? Shims.getDefaultFetch();
|
|
704
|
+
client.organization = options.organization ?? null;
|
|
705
|
+
client.project = options.project ?? null;
|
|
706
|
+
}
|
|
703
707
|
if (
|
|
704
708
|
this.#x509Authentication &&
|
|
705
709
|
client.#x509Authentication &&
|
|
@@ -1151,9 +1155,10 @@ export class OpenAI {
|
|
|
1151
1155
|
retriesRemaining = maxRetries;
|
|
1152
1156
|
}
|
|
1153
1157
|
|
|
1158
|
+
const x509Authentication = this.#x509Authentication;
|
|
1159
|
+
x509Authentication?.beginRequestPreparation();
|
|
1154
1160
|
await this.prepareOptions(options);
|
|
1155
1161
|
|
|
1156
|
-
const x509Authentication = this.#x509Authentication;
|
|
1157
1162
|
x509Authentication?.beginRequestPlanning();
|
|
1158
1163
|
let built: { req: FinalizedRequestInit; url: string; timeout: number };
|
|
1159
1164
|
try {
|
|
@@ -1346,7 +1351,7 @@ export class OpenAI {
|
|
|
1346
1351
|
.filter(([name]) => name === 'x-request-id')
|
|
1347
1352
|
.map(([name, value]) => ', ' + name + ': ' + JSON.stringify(value))
|
|
1348
1353
|
.join('');
|
|
1349
|
-
const responseInfo = `[${requestLogID}${retryLogStr}${specialHeaders}] ${req.method} ${url} ${
|
|
1354
|
+
const responseInfo = `[${requestLogID}${retryLogStr}${specialHeaders}] ${req.method} ${redactURL(url)} ${
|
|
1350
1355
|
response.ok ? 'succeeded' : 'failed'
|
|
1351
1356
|
} with status ${response.status} in ${headersTime - startTime}ms`;
|
|
1352
1357
|
|