Files
medreport_mrb2b/app/home/(user)/_lib/server/load-life-style.ts
2025-10-21 09:36:29 +03:00

42 lines
1.1 KiB
TypeScript

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);