lifestyle development

This commit is contained in:
Danel Kungla
2025-10-21 16:04:01 +03:00
parent 6dcc91a206
commit 76c2382e11
14 changed files with 374 additions and 129 deletions

View File

@@ -0,0 +1,108 @@
'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';
import PersonalCode from '~/lib/utils';
import {
AnalysisResponses,
ILifeStyleResponse,
PROMPT_NAME,
} from '../../_components/ai/types';
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,
}: {
account: AccountWithParams;
}): 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 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 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,
};
}
}

View File

@@ -1,41 +1,49 @@
import { cache } from 'react';
import { AccountWithParams } from '@/packages/features/accounts/src/types/accounts';
import OpenAI from 'openai';
import { getSupabaseServerClient } from '@/packages/supabase/src/clients/server-client';
import PersonalCode from '~/lib/utils';
import { ILifeStyleResponse, PROMPT_NAME } from '../../_components/ai/types';
import { updateLifeStyle } from './ai-actions';
const failedResponse = {
lifeStyle: null,
lifestyle: [],
summary: null,
};
async function lifeStyleLoader(account: AccountWithParams) {
async function lifeStyleLoader(
account: AccountWithParams,
): Promise<ILifeStyleResponse> {
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(),
},
},
});
const lifeStylePromptId = process.env.PROMPT_ID_LIFE_STYLE;
return response;
} catch (error) {
console.error('Error calling OpenAI: ', error);
if (!lifeStylePromptId) {
return failedResponse;
}
const supabaseClient = getSupabaseServerClient();
const { data, error } = await supabaseClient
.schema('medreport')
.from('ai_responses')
.select('response')
.eq('account_id', account.id)
.eq('prompt_name', PROMPT_NAME.LIFE_STYLE)
.order('latest_data_change', { ascending: false, nullsFirst: false })
.limit(1)
.maybeSingle();
if (error) {
console.error('Error fetching AI response from DB: ', error);
return failedResponse;
}
if (data?.response) {
return JSON.parse(data.response as string);
} else {
return await updateLifeStyle({ account });
}
}
export const loadLifeStyle = cache(lifeStyleLoader);

View File

@@ -8,6 +8,7 @@ import OpenAI from 'openai';
import PersonalCode from '~/lib/utils';
import { PROMPT_NAME } from '../../_components/ai/types';
import { OrderAnalysisCard } from '../../_components/order-analyses-cards';
export const loadRecommendations = cache(recommendationsLoader);
@@ -120,7 +121,7 @@ async function recommendationsLoader(
.from('ai_responses')
.insert({
account_id: account.id,
prompt_name: 'Analysis Recommendations',
prompt_name: PROMPT_NAME.ANALYSIS_RECOMMENDATIONS,
prompt_id: analysesRecommendationsPromptId,
input: JSON.stringify({
analyses: formattedAnalyses,