add openai validation

This commit is contained in:
Danel Kungla
2025-09-26 16:18:50 +03:00
parent 0a61371271
commit c99beea46a
2 changed files with 24 additions and 11 deletions

View File

@@ -16,6 +16,7 @@ import Dashboard from '../_components/dashboard';
import DashboardCards from '../_components/dashboard-cards';
import Recommendations from '../_components/recommendations';
import RecommendationsSkeleton from '../_components/recommendations-skeleton';
import { isValidOpenAiEnv } from '../_lib/server/is-valid-open-ai-env';
import { loadCurrentUserAccount } from '../_lib/server/load-user-account';
export const generateMetadata = async () => {
@@ -52,17 +53,16 @@ async function UserHomePage() {
/>
<PageBody>
<Dashboard account={account} bmiThresholds={bmiThresholds} />
{process.env.OPENAI_API_KEY &&
process.env.PROMPT_ID_ANALYSIS_RECOMMENDATIONS && (
<>
<h4>
<Trans i18nKey="dashboard:recommendations.title" />
</h4>
<Suspense fallback={<RecommendationsSkeleton />}>
<Recommendations account={account} />
</Suspense>
</>
)}
{(await isValidOpenAiEnv()) && (
<>
<h4>
<Trans i18nKey="dashboard:recommendations.title" />
</h4>
<Suspense fallback={<RecommendationsSkeleton />}>
<Recommendations account={account} />
</Suspense>
</>
)}
</PageBody>
</>
);

View File

@@ -0,0 +1,13 @@
import OpenAI from 'openai';
export const isValidOpenAiEnv = async () => {
const client = new OpenAI();
try {
await client.models.list();
return true;
} catch (e) {
console.log('No openAI env');
return false;
}
};