fix: lazy-init OpenAI client to avoid build failure without API key

This commit is contained in:
Christopher Mayor
2026-04-24 14:37:28 -07:00
parent 3539a5f3eb
commit 37c07e468d

View File

@@ -6,9 +6,14 @@ import type {
ItemResearch, ItemResearch,
} from "../types"; } from "../types";
const client = new OpenAI({ let _client: OpenAI | null = null;
apiKey: process.env.OPENAI_API_KEY,
}); function getClient(): OpenAI {
if (!_client) {
_client = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });
}
return _client;
}
const SYSTEM_PROMPT = `You are an expert research analyst. Your job is to compare items across multiple dimensions and produce structured, insightful comparison data. const SYSTEM_PROMPT = `You are an expert research analyst. Your job is to compare items across multiple dimensions and produce structured, insightful comparison data.
@@ -105,7 +110,7 @@ Provide a comprehensive comparison with scores, pros/cons, and a recommendation.
for (let attempt = 1; attempt <= MAX_RETRIES; attempt++) { for (let attempt = 1; attempt <= MAX_RETRIES; attempt++) {
try { try {
const response = await client.chat.completions.create({ const response = await getClient().chat.completions.create({
model: "gpt-4o-mini", model: "gpt-4o-mini",
messages: [ messages: [
{ role: "system", content: SYSTEM_PROMPT }, { role: "system", content: SYSTEM_PROMPT },