公众号验证码功能完成 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,13 +5,21 @@ import { isRunningInBrowser } from "../../internal/detect-platform.mjs";
export class OpenAIRealtimeWebSocket extends OpenAIRealtimeEmitter {
constructor(props, client) {
super();
const hasProvider = typeof client?._options?.apiKey === 'function';
const dangerouslyAllowBrowser = props.dangerouslyAllowBrowser ??
client?._options?.dangerouslyAllowBrowser ??
(client?.apiKey.startsWith('ek_') ? true : null);
(client?.apiKey?.startsWith('ek_') ? true : null);
if (!dangerouslyAllowBrowser && isRunningInBrowser()) {
throw new OpenAIError("It looks like you're running in a browser-like environment.\n\nThis is disabled by default, as it risks exposing your secret API credentials to attackers.\n\nYou can avoid this error by creating an ephemeral session token:\nhttps://platform.openai.com/docs/api-reference/realtime-sessions\n");
}
client ?? (client = new OpenAI({ dangerouslyAllowBrowser }));
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 OpenAIRealtimeWebSocket.create(client, { model })',
].join('\n'));
}
this.url = buildRealtimeURL(client, props.model);
props.onURL?.(this.url);
// @ts-ignore
@@ -53,19 +61,17 @@ export class OpenAIRealtimeWebSocket extends OpenAIRealtimeEmitter {
}
}
}
static async create(client, props) {
return new OpenAIRealtimeWebSocket({ ...props, __resolvedApiKey: await client._callApiKey() }, client);
}
static async azure(client, options = {}) {
const token = await client._getAzureADToken();
const isApiKeyProvider = await client._callApiKey();
function onURL(url) {
if (client.apiKey !== '<Missing Key>') {
url.searchParams.set('api-key', client.apiKey);
if (isApiKeyProvider) {
url.searchParams.set('Authorization', `Bearer ${client.apiKey}`);
}
else {
if (token) {
url.searchParams.set('Authorization', `Bearer ${token}`);
}
else {
throw new Error('AzureOpenAI is not instantiated correctly. No API key or token provided.');
}
url.searchParams.set('api-key', client.apiKey);
}
}
const deploymentName = options.deploymentName ?? client.deploymentName;
@@ -77,6 +83,7 @@ export class OpenAIRealtimeWebSocket extends OpenAIRealtimeEmitter {
model: deploymentName,
onURL,
...(dangerouslyAllowBrowser ? { dangerouslyAllowBrowser } : {}),
__resolvedApiKey: isApiKeyProvider,
}, client);
}
send(event) {