change recommendations to update through doctor
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
'use server';
|
||||
|
||||
import { AccountWithParams } from '@/packages/features/accounts/src/types/accounts';
|
||||
import { createUserAnalysesApi } from '@/packages/features/user-analyses/src/server/api';
|
||||
import { getSupabaseServerClient } from '@/packages/supabase/src/clients/server-client';
|
||||
import OpenAI from 'openai';
|
||||
|
||||
@@ -12,8 +11,9 @@ import {
|
||||
ILifeStyleResponse,
|
||||
PROMPT_NAME,
|
||||
} from '../../_components/ai/types';
|
||||
import { OrderAnalysisCard } from '../../_components/order-analyses-cards';
|
||||
|
||||
async function getLatestResponseTime(items: AnalysisResponses) {
|
||||
async function getLatestResponseTime(items?: AnalysisResponses) {
|
||||
if (!items?.length) return null;
|
||||
|
||||
let latest = null;
|
||||
@@ -29,8 +29,10 @@ async function getLatestResponseTime(items: AnalysisResponses) {
|
||||
|
||||
export async function updateLifeStyle({
|
||||
account,
|
||||
analysisResponses,
|
||||
}: {
|
||||
account: AccountWithParams;
|
||||
analysisResponses?: AnalysisResponses;
|
||||
}): Promise<ILifeStyleResponse> {
|
||||
const LIFE_STYLE_PROMPT_ID = process.env.PROMPT_ID_LIFE_STYLE;
|
||||
if (!LIFE_STYLE_PROMPT_ID || !account?.personal_code) {
|
||||
@@ -42,13 +44,26 @@ export async function updateLifeStyle({
|
||||
|
||||
const openAIClient = new OpenAI();
|
||||
const supabaseClient = getSupabaseServerClient();
|
||||
const userAnalysesApi = createUserAnalysesApi(supabaseClient);
|
||||
|
||||
const analysisResponses = await userAnalysesApi.getAllUserAnalysisResponses();
|
||||
const { gender, age } = PersonalCode.parsePersonalCode(account.personal_code);
|
||||
const weight = account.accountParams?.weight || 'unknown';
|
||||
const height = account.accountParams?.height || 'unknown';
|
||||
const isSmoker = !!account.accountParams?.isSmoker;
|
||||
const cholesterol =
|
||||
analysisResponses
|
||||
?.find((ar) => ar.analysis_name_lab === 'Kolesterool')
|
||||
?.response_value.toString() || 'unknown';
|
||||
const ldl =
|
||||
analysisResponses
|
||||
?.find((ar) => ar.analysis_name_lab === 'LDL kolesterool')
|
||||
?.response_value.toString() || 'unknown';
|
||||
const hdl =
|
||||
analysisResponses
|
||||
?.find((ar) => ar.analysis_name_lab === 'HDL kolesterool')
|
||||
?.response_value.toString() || 'unknown';
|
||||
const vitamind =
|
||||
analysisResponses
|
||||
?.find((ar) => ar.analysis_name_lab === 'Vitamiin D (25-OH)')
|
||||
?.response_value.toString() || 'unknown';
|
||||
|
||||
const latestResponseTime = await getLatestResponseTime(analysisResponses);
|
||||
const latestISO = latestResponseTime
|
||||
@@ -65,10 +80,10 @@ export async function updateLifeStyle({
|
||||
age: age.toString(),
|
||||
weight: weight.toString(),
|
||||
height: height.toString(),
|
||||
cholesterol: '',
|
||||
ldl: '',
|
||||
hdl: '',
|
||||
vitamind: '',
|
||||
cholesterol,
|
||||
ldl,
|
||||
hdl,
|
||||
vitamind,
|
||||
is_smoker: isSmoker.toString(),
|
||||
},
|
||||
},
|
||||
@@ -85,10 +100,10 @@ export async function updateLifeStyle({
|
||||
gender: gender.value,
|
||||
age: age.toString(),
|
||||
weight: weight.toString(),
|
||||
cholesterol: '',
|
||||
ldl: '',
|
||||
hdl: '',
|
||||
vitamind: '',
|
||||
cholesterol,
|
||||
ldl,
|
||||
hdl,
|
||||
vitamind,
|
||||
is_smoker: isSmoker.toString(),
|
||||
}),
|
||||
latest_data_change: latestISO,
|
||||
@@ -106,3 +121,92 @@ export async function updateLifeStyle({
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
export async function updateRecommendations({
|
||||
analyses,
|
||||
analysisResponses,
|
||||
account,
|
||||
}: {
|
||||
analyses: OrderAnalysisCard[];
|
||||
analysisResponses?: AnalysisResponses;
|
||||
account: AccountWithParams;
|
||||
}) {
|
||||
const RECOMMENDATIONS_PROMPT_IT =
|
||||
process.env.PROMPT_ID_ANALYSIS_RECOMMENDATIONS;
|
||||
|
||||
if (!RECOMMENDATIONS_PROMPT_IT || !account?.personal_code) {
|
||||
console.error('No prompt ID for analysis recommendations');
|
||||
return [];
|
||||
}
|
||||
|
||||
const openAIClient = new OpenAI();
|
||||
const supabaseClient = getSupabaseServerClient();
|
||||
|
||||
const { gender, age } = PersonalCode.parsePersonalCode(account.personal_code);
|
||||
const weight = account.accountParams?.weight || 'unknown';
|
||||
|
||||
const formattedAnalysisResponses = analysisResponses?.map(
|
||||
({
|
||||
analysis_name_lab,
|
||||
response_value,
|
||||
norm_upper,
|
||||
norm_lower,
|
||||
norm_status,
|
||||
}) => ({
|
||||
name: analysis_name_lab,
|
||||
value: response_value,
|
||||
normUpper: norm_upper,
|
||||
normLower: norm_lower,
|
||||
normStatus: norm_status,
|
||||
}),
|
||||
);
|
||||
const formattedAnalyses = analyses.map(({ description, title }) => ({
|
||||
description,
|
||||
title,
|
||||
}));
|
||||
const latestResponseTime = await getLatestResponseTime(analysisResponses);
|
||||
const latestISO = latestResponseTime
|
||||
? new Date(latestResponseTime).toISOString()
|
||||
: new Date('2025').toISOString();
|
||||
|
||||
try {
|
||||
const response = await openAIClient.responses.create({
|
||||
store: false,
|
||||
prompt: {
|
||||
id: RECOMMENDATIONS_PROMPT_IT,
|
||||
variables: {
|
||||
analyses: JSON.stringify(formattedAnalyses),
|
||||
results: JSON.stringify(formattedAnalysisResponses),
|
||||
gender: gender.value,
|
||||
age: age.toString(),
|
||||
weight: weight.toString(),
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
await supabaseClient
|
||||
.schema('medreport')
|
||||
.from('ai_responses')
|
||||
.insert({
|
||||
account_id: account.id,
|
||||
prompt_name: PROMPT_NAME.ANALYSIS_RECOMMENDATIONS,
|
||||
prompt_id: RECOMMENDATIONS_PROMPT_IT,
|
||||
input: JSON.stringify({
|
||||
analyses: formattedAnalyses,
|
||||
results: formattedAnalysisResponses,
|
||||
gender,
|
||||
age,
|
||||
weight,
|
||||
}),
|
||||
latest_data_change: latestISO,
|
||||
response: response.output_text,
|
||||
});
|
||||
|
||||
const json = JSON.parse(response.output_text);
|
||||
|
||||
return json.recommended;
|
||||
} catch (error) {
|
||||
console.error('Error getting recommendations: ', error);
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user