213 lines
5.8 KiB
TypeScript
213 lines
5.8 KiB
TypeScript
'use server';
|
|
|
|
import { AccountWithParams } from '@/packages/features/accounts/src/types/accounts';
|
|
import { getSupabaseServerClient } from '@/packages/supabase/src/clients/server-client';
|
|
import OpenAI from 'openai';
|
|
|
|
import PersonalCode from '~/lib/utils';
|
|
|
|
import {
|
|
AnalysisResponses,
|
|
ILifeStyleResponse,
|
|
PROMPT_NAME,
|
|
} from '../../_components/ai/types';
|
|
import { OrderAnalysisCard } from '../../_components/order-analyses-cards';
|
|
|
|
async function getLatestResponseTime(items?: AnalysisResponses) {
|
|
if (!items?.length) return null;
|
|
|
|
let latest = null;
|
|
for (const it of items) {
|
|
const d = new Date(it.response_time);
|
|
const t = d.getTime();
|
|
if (!Number.isNaN(t) && (latest === null || t > latest.getTime())) {
|
|
latest = d;
|
|
}
|
|
}
|
|
return latest;
|
|
}
|
|
|
|
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) {
|
|
return {
|
|
lifestyle: [],
|
|
summary: null,
|
|
};
|
|
}
|
|
|
|
const openAIClient = new OpenAI();
|
|
const supabaseClient = getSupabaseServerClient();
|
|
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
|
|
? new Date(latestResponseTime).toISOString()
|
|
: new Date('2025').toISOString();
|
|
|
|
try {
|
|
const response = await openAIClient.responses.create({
|
|
store: false,
|
|
prompt: {
|
|
id: LIFE_STYLE_PROMPT_ID,
|
|
variables: {
|
|
gender: gender.value,
|
|
age: age.toString(),
|
|
weight: weight.toString(),
|
|
height: height.toString(),
|
|
cholesterol,
|
|
ldl,
|
|
hdl,
|
|
vitamind,
|
|
is_smoker: isSmoker.toString(),
|
|
},
|
|
},
|
|
});
|
|
|
|
await supabaseClient
|
|
.schema('medreport')
|
|
.from('ai_responses')
|
|
.insert({
|
|
account_id: account.id,
|
|
prompt_name: PROMPT_NAME.LIFE_STYLE,
|
|
prompt_id: LIFE_STYLE_PROMPT_ID,
|
|
input: JSON.stringify({
|
|
gender: gender.value,
|
|
age: age.toString(),
|
|
weight: weight.toString(),
|
|
cholesterol,
|
|
ldl,
|
|
hdl,
|
|
vitamind,
|
|
is_smoker: isSmoker.toString(),
|
|
}),
|
|
latest_data_change: latestISO,
|
|
response: response.output_text,
|
|
});
|
|
|
|
const json = JSON.parse(response.output_text);
|
|
|
|
return json;
|
|
} catch (error) {
|
|
console.error('Error calling OpenAI: ', error);
|
|
return {
|
|
lifestyle: [],
|
|
summary: null,
|
|
};
|
|
}
|
|
}
|
|
|
|
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 [];
|
|
}
|
|
}
|