WIP: add lifestyle block

This commit is contained in:
Danel Kungla
2025-10-21 09:36:29 +03:00
parent fbdfdaf0c1
commit 6dcc91a206
10 changed files with 233 additions and 89 deletions

View File

@@ -0,0 +1,41 @@
import { cache } from 'react';
import { AccountWithParams } from '@/packages/features/accounts/src/types/accounts';
import OpenAI from 'openai';
import PersonalCode from '~/lib/utils';
const failedResponse = {
lifeStyle: null,
summary: null,
};
async function lifeStyleLoader(account: AccountWithParams) {
if (!account?.personal_code) {
return failedResponse;
}
const openAIClient = new OpenAI();
const { gender, age } = PersonalCode.parsePersonalCode(account.personal_code);
try {
const response = await openAIClient.responses.create({
store: false,
prompt: {
id: analysesRecommendationsPromptId,
variables: {
analyses: JSON.stringify(formattedAnalyses),
results: JSON.stringify(formattedAnalysisResponses),
gender: gender.value,
age: age.toString(),
weight: weight.toString(),
},
},
});
return response;
} catch (error) {
console.error('Error calling OpenAI: ', error);
return failedResponse;
}
}
export const loadLifeStyle = cache(lifeStyleLoader);