fix: inconsistent usage of base URL and API key (#507)

A recent commit introduced the ability to use third-party model
providers. (Really appreciate it!)

However, the usage is inconsistent: some pieces of code use the custom
providers, whereas others still have the old behavior. Additionally,
`OPENAI_BASE_URL` is now being disregarded when it shouldn't be.

This PR normalizes the usage to `getApiKey` and `getBaseUrl`, and
enables the use of `OPENAI_BASE_URL` if present.

---------

Co-authored-by: Gabriel Bianconi <GabrielBianconi@users.noreply.github.com>
This commit is contained in:
Gabriel Bianconi
2025-04-22 10:51:26 -04:00
committed by GitHub
parent d78f77edb7
commit 98a22273d9
5 changed files with 39 additions and 19 deletions

View File

@@ -1,8 +1,8 @@
import type { AppConfig } from "./config.js";
import type { ResponseItem } from "openai/resources/responses/responses.mjs";
import { OPENAI_BASE_URL } from "./config.js";
import { getBaseUrl, getApiKey } from "./config.js";
import OpenAI from "openai";
/**
* Generate a condensed summary of the conversation items.
* @param items The list of conversation items to summarize
@@ -14,16 +14,18 @@ import OpenAI from "openai";
* @param items The list of conversation items to summarize
* @param model The model to use for generating the summary
* @param flexMode Whether to use the flex-mode service tier
* @param config The configuration object
* @returns A concise structured summary string
*/
export async function generateCompactSummary(
items: Array<ResponseItem>,
model: string,
flexMode = false,
config: AppConfig,
): Promise<string> {
const oai = new OpenAI({
apiKey: process.env["OPENAI_API_KEY"],
baseURL: OPENAI_BASE_URL,
apiKey: getApiKey(config.provider),
baseURL: getBaseUrl(config.provider),
});
const conversationText = items

View File

@@ -41,15 +41,26 @@ export function setApiKey(apiKey: string): void {
OPENAI_API_KEY = apiKey;
}
export function getBaseUrl(provider: string): string | undefined {
export function getBaseUrl(provider: string = "openai"): string | undefined {
// If the provider is `openai` and `OPENAI_BASE_URL` is set, use it
if (provider === "openai" && OPENAI_BASE_URL !== "") {
return OPENAI_BASE_URL;
}
const providerInfo = providers[provider.toLowerCase()];
if (providerInfo) {
return providerInfo.baseURL;
}
// If the provider not found in the providers list and `OPENAI_BASE_URL` is set, use it
if (OPENAI_BASE_URL !== "") {
return OPENAI_BASE_URL;
}
return undefined;
}
export function getApiKey(provider: string): string | undefined {
export function getApiKey(provider: string = "openai"): string | undefined {
const providerInfo = providers[provider.toLowerCase()];
if (providerInfo) {
if (providerInfo.name === "Ollama") {
@@ -58,6 +69,11 @@ export function getApiKey(provider: string): string | undefined {
return process.env[providerInfo.envKey];
}
// If the provider not found in the providers list and `OPENAI_API_KEY` is set, use it
if (OPENAI_API_KEY !== "") {
return OPENAI_API_KEY;
}
return undefined;
}

View File

@@ -20,9 +20,11 @@ async function fetchModels(provider: string): Promise<Array<string>> {
throw new Error("No API key configured for provider: " + provider);
}
const baseURL = getBaseUrl(provider);
try {
const openai = new OpenAI({ apiKey: getApiKey(provider), baseURL });
const openai = new OpenAI({
apiKey: getApiKey(provider),
baseURL: getBaseUrl(provider),
});
const list = await openai.models.list();
const models: Array<string> = [];
for await (const model of list as AsyncIterable<{ id?: string }>) {