109 lines
3.0 KiB
TypeScript
109 lines
3.0 KiB
TypeScript
'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,
|
|
};
|
|
}
|
|
}
|