UNPKG

openai

Version:

The official TypeScript library for the OpenAI API

94 lines • 4.88 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.realtimeRoutingProtocols = realtimeRoutingProtocols; exports.setRealtimeAPIKeyCacheContext = setRealtimeAPIKeyCacheContext; exports.getDeferredRealtimeAPIKeyCache = getDeferredRealtimeAPIKeyCache; exports.validateCapturedAPIKey = validateCapturedAPIKey; exports.getRealtimeAPIKey = getRealtimeAPIKey; exports.resolveRealtimeAPIKey = resolveRealtimeAPIKey; const bedrock_1 = require("./bedrock.js"); const ws_1 = require("./ws.js"); /** Native Realtime accepts routing metadata through these two server-supported subprotocols. */ function realtimeRoutingProtocols(client, apiKey) { const protocols = []; // oxlint-disable-next-line anti-slop/no-runtime-typeof -- Structural partial clients may omit the callable header hook; check it before using the client's header contract. if ('_buildWebSocketHeaders' in client && typeof client._buildWebSocketHeaders === 'function') { // SAFETY: The checked client hook owns header precedence; no other OpenAI members are used. const { headers } = (0, ws_1.buildWebSocketOptions)(client, { Authorization: `Bearer ${apiKey}` }, null); for (const field of ['organization', 'project']) { const value = headers[`openai-${field}`]; if (value !== undefined) { protocols.push(`openai-${field}.${value}`); } } return protocols; } // oxlint-disable-next-line anti-slop/no-runtime-typeof -- Realtime accepts structural partial clients; optional routing fields enter from that untyped client boundary. if ('organization' in client && typeof client.organization === 'string') { protocols.push(`openai-organization.${client.organization}`); } // oxlint-disable-next-line anti-slop/no-runtime-typeof -- Only the supported string field can be emitted to the native protocol boundary. if ('project' in client && typeof client.project === 'string') { protocols.push(`openai-project.${client.project}`); } return protocols; } // Only the key crosses module formats: the context belongs to each participating client. const realtimeCacheContext = Symbol.for('openai.realtimeAPIKeyCacheContext'); let cacheContext; /** Installs the Node transport's invocation context without loading Node in the base client. @internal */ function setRealtimeAPIKeyCacheContext(context) { cacheContext = context; } /** Reserves a deferred commit when the base hook is entered for this invocation. @internal */ function getDeferredRealtimeAPIKeyCache(client) { const deferred = client[realtimeCacheContext]?.getStore(); return deferred?.client === client ? deferred : undefined; } /** Applies the Bedrock getter's validation when a captured credential does not enter the cache. @internal */ function validateCapturedAPIKey(client, credential) { if (credential !== null && bedrock_1.brand_privateBedrockClient in client) { (0, bedrock_1.assertValidBedrockBearerCredential)(credential); } return credential; } /** Selects a captured credential without treating an explicit null as absent. @internal */ function getRealtimeAPIKey(client, captured) { return captured === undefined ? client?.apiKey : captured; } /** * Captures the key belonging to this request or factory invocation while retaining the * existing boolean credential-hook contract. Legacy overrides that do not * capture a key keep their shared-property behavior and remain responsible for * synchronizing concurrent credential updates. * @internal */ async function resolveRealtimeAPIKey(client, deferCache = false) { let apiKey; const current = { client, commit: () => (apiKey === undefined ? client.apiKey : apiKey), }; const capture = (resolved) => { apiKey = resolved; }; const invoke = () => client._callApiKey(capture); const context = client[realtimeCacheContext] ?? cacheContext; if (deferCache && context && !client[realtimeCacheContext]) { Object.defineProperty(client, realtimeCacheContext, { value: context }); } // An HTTP or ordinary Realtime request nested inside a WebSocket hook owns its own cache writes. const isProvider = await (context ? context.run(deferCache ? current : undefined, invoke) : invoke()); return { apiKey: apiKey === undefined ? client.apiKey : apiKey, isProvider, commit: () => { const hookKey = current.providerKey !== undefined && apiKey !== undefined && apiKey !== current.providerKey ? validateCapturedAPIKey(client, apiKey) : undefined; const cached = current.commit(); return hookKey === undefined ? validateCapturedAPIKey(client, cached) : hookKey; }, }; } //# sourceMappingURL=realtime-credentials.js.map