fix(openai.ts): default to an empty string if undefined

Change:

const urlHost = new URL(this.options.openAiBaseUrl).host;

To:

const urlHost = new URL(this.options.openAiBaseUrl ?? "").host;

because the nullish coalescing operator (??) to default to an empty string if this.options.openAiBaseUrl is undefined.

It ensures that the URL constructor always receives a valid string.
This commit is contained in:
lloydchang
2024-12-11 02:37:26 -08:00
committed by GitHub
parent 7c870733a1
commit 9abe3f6916

View File

@@ -17,7 +17,7 @@ export class OpenAiHandler implements ApiHandler {
constructor(options: ApiHandlerOptions) { constructor(options: ApiHandlerOptions) {
this.options = options this.options = options
// Azure API shape slightly differs from the core API shape: https://github.com/openai/openai-node?tab=readme-ov-file#microsoft-azure-openai // Azure API shape slightly differs from the core API shape: https://github.com/openai/openai-node?tab=readme-ov-file#microsoft-azure-openai
const urlHost = new URL(this.options.openAiBaseUrl).host; const urlHost = new URL(this.options.openAiBaseUrl ?? "").host;
if (urlHost === "azure.com" || urlHost.endsWith(".azure.com")) { if (urlHost === "azure.com" || urlHost.endsWith(".azure.com")) {
this.client = new AzureOpenAI({ this.client = new AzureOpenAI({
baseURL: this.options.openAiBaseUrl, baseURL: this.options.openAiBaseUrl,