公众号验证码功能完成 yeah!!!

This commit is contained in:
2025-11-21 15:33:03 +08:00
parent ad35173301
commit 6330f6faa1
614 changed files with 31574 additions and 4571 deletions

View File

@@ -5,12 +5,20 @@ export class OpenAIRealtimeWS extends OpenAIRealtimeEmitter {
constructor(props, client) {
super();
client ?? (client = new OpenAI());
const hasProvider = typeof client?._options?.apiKey === 'function';
if (hasProvider && !props.__resolvedApiKey) {
throw new Error([
'Cannot open Realtime WebSocket with a function-based apiKey.',
'Use the .create() method so that the key is resolved before connecting:',
'await OpenAIRealtimeWS.create(client, { model })',
].join('\n'));
}
this.url = buildRealtimeURL(client, props.model);
this.socket = new WS.WebSocket(this.url, {
...props.options,
headers: {
...props.options?.headers,
...(isAzure(client) ? {} : { Authorization: `Bearer ${client.apiKey}` }),
...(isAzure(client) && !props.__resolvedApiKey ? {} : { Authorization: `Bearer ${client.apiKey}` }),
'OpenAI-Beta': 'realtime=v1',
},
});
@@ -39,12 +47,26 @@ export class OpenAIRealtimeWS extends OpenAIRealtimeEmitter {
this._onError(null, err.message, err);
});
}
static async azure(client, options = {}) {
const deploymentName = options.deploymentName ?? client.deploymentName;
static async create(client, props) {
return new OpenAIRealtimeWS({ ...props, __resolvedApiKey: await client._callApiKey() }, client);
}
static async azure(client, props = {}) {
const isApiKeyProvider = await client._callApiKey();
const deploymentName = props.deploymentName ?? client.deploymentName;
if (!deploymentName) {
throw new Error('No deployment name provided');
}
return new OpenAIRealtimeWS({ model: deploymentName, options: { headers: await getAzureHeaders(client) } }, client);
return new OpenAIRealtimeWS({
model: deploymentName,
options: {
...props.options,
headers: {
...props.options?.headers,
...(isApiKeyProvider ? {} : { 'api-key': client.apiKey }),
},
},
__resolvedApiKey: isApiKeyProvider,
}, client);
}
send(event) {
try {
@@ -63,18 +85,4 @@ export class OpenAIRealtimeWS extends OpenAIRealtimeEmitter {
}
}
}
async function getAzureHeaders(client) {
if (client.apiKey !== '<Missing Key>') {
return { 'api-key': client.apiKey };
}
else {
const token = await client._getAzureADToken();
if (token) {
return { Authorization: `Bearer ${token}` };
}
else {
throw new Error('AzureOpenAI is not instantiated correctly. No API key or token provided.');
}
}
}
//# sourceMappingURL=ws.mjs.map