diff --git a/app/home/(user)/_lib/server/load-recommendations.ts b/app/home/(user)/_lib/server/load-recommendations.ts index 0744697..7c222b2 100644 --- a/app/home/(user)/_lib/server/load-recommendations.ts +++ b/app/home/(user)/_lib/server/load-recommendations.ts @@ -96,6 +96,10 @@ async function recommendationsLoader( .eq('prompt_id', analysesRecommendationsPromptId) .eq('latest_data_change', latestISO); + if (previouslyRecommended.data?.[0]?.response) { + return previouslyRecommended.data[0].response; + } + const openAIClient = new OpenAI(); const { gender, age } = PersonalCode.parsePersonalCode(account.personal_code); const weight = account.accountParams?.weight || 'unknown'; @@ -110,7 +114,6 @@ async function recommendationsLoader( })); const response = await openAIClient.responses.create({ - model: 'gpt-5-mini', store: false, prompt: { id: analysesRecommendationsPromptId, @@ -126,6 +129,23 @@ async function recommendationsLoader( }); const json = JSON.parse(response.output_text); - console.log('response.output_text', response.output_text); + await supabaseClient + .schema('medreport') + .from('ai_responses') + .insert({ + account_id: account.id, + prompt_name: 'Analysis Recommendations', + prompt_id: analysesRecommendationsPromptId, + input: JSON.stringify({ + analyses: formattedAnalyses, + results: formattedAnalysisResponses, + gender, + age, + weight, + }), + latest_data_change: latestISO, + response: JSON.stringify(response.output_text), + }); + return json.recommended; } diff --git a/packages/supabase/src/database.types.ts b/packages/supabase/src/database.types.ts index 6907fac..e620ad9 100644 --- a/packages/supabase/src/database.types.ts +++ b/packages/supabase/src/database.types.ts @@ -198,6 +198,7 @@ export type Database = { action: string changed_by: string created_at: string + extra_data: Json | null id: number } Insert: { @@ -205,6 +206,7 @@ export type Database = { action: string changed_by: string created_at?: string + extra_data?: Json | null id?: number } Update: { @@ -212,6 +214,7 @@ export type Database = { action?: string changed_by?: string created_at?: string + extra_data?: Json | null id?: number } Relationships: [] @@ -517,6 +520,61 @@ export type Database = { }, ] } + ai_responses: { + Row: { + account_id: string + created_at: string + id: string + input: Json + latest_data_change: string + prompt_id: string + prompt_name: string + response: Json + } + Insert: { + account_id: string + created_at?: string + id?: string + input: Json + latest_data_change: string + prompt_id: string + prompt_name: string + response: Json + } + Update: { + account_id?: string + created_at?: string + id?: string + input?: Json + latest_data_change?: string + prompt_id?: string + prompt_name?: string + response?: Json + } + Relationships: [ + { + foreignKeyName: "ai_responses_account_id_fkey" + columns: ["account_id"] + isOneToOne: false + referencedRelation: "accounts" + referencedColumns: ["id"] + }, + { + foreignKeyName: "ai_responses_account_id_fkey" + columns: ["account_id"] + isOneToOne: false + referencedRelation: "user_account_workspace" + referencedColumns: ["id"] + }, + { + foreignKeyName: "ai_responses_account_id_fkey" + columns: ["account_id"] + isOneToOne: false + referencedRelation: "user_accounts" + referencedColumns: ["id"] + }, + ] + } analyses: { Row: { analysis_id_oid: string @@ -685,11 +743,11 @@ export type Database = { norm_upper: number | null norm_upper_included: boolean | null original_response_element: Json - response_time: string + response_time: string | null response_value: number | null - response_value_is_negative?: boolean | null - response_value_is_within_norm?: boolean | null - status: string + response_value_is_negative: boolean | null + response_value_is_within_norm: boolean | null + status: string | null unit: string | null updated_at: string | null } @@ -706,11 +764,11 @@ export type Database = { norm_upper?: number | null norm_upper_included?: boolean | null original_response_element: Json - response_time: string - response_value: number | null + response_time?: string | null + response_value?: number | null response_value_is_negative?: boolean | null response_value_is_within_norm?: boolean | null - status: string + status?: string | null unit?: string | null updated_at?: string | null } @@ -727,11 +785,11 @@ export type Database = { norm_upper?: number | null norm_upper_included?: boolean | null original_response_element?: Json - response_time?: string + response_time?: string | null response_value?: number | null response_value_is_negative?: boolean | null response_value_is_within_norm?: boolean | null - status: string + status?: string | null unit?: string | null updated_at?: string | null } @@ -1159,7 +1217,7 @@ export type Database = { doctor_user_id: string | null id: number status: Database["medreport"]["Enums"]["analysis_feedback_status"] - updated_at: string + updated_at: string | null updated_by: string | null user_id: string value: string | null @@ -1171,7 +1229,7 @@ export type Database = { doctor_user_id?: string | null id?: number status?: Database["medreport"]["Enums"]["analysis_feedback_status"] - updated_at?: string + updated_at?: string | null updated_by?: string | null user_id: string value?: string | null @@ -1183,7 +1241,7 @@ export type Database = { doctor_user_id?: string | null id?: number status?: Database["medreport"]["Enums"]["analysis_feedback_status"] - updated_at?: string + updated_at?: string | null updated_by?: string | null user_id?: string value?: string | null @@ -1268,27 +1326,42 @@ export type Database = { } medipost_actions: { Row: { - created_at: string - id: number action: string - xml: string + created_at: string | null has_analysis_results: boolean - medipost_external_order_id: string - medipost_private_message_id: string - medusa_order_id: string - response_xml: string has_error: boolean + id: string + medipost_external_order_id: string | null + medipost_private_message_id: string | null + medusa_order_id: string | null + response_xml: string | null + xml: string | null } Insert: { action: string - xml: string - has_analysis_results: boolean - medipost_external_order_id: string - medipost_private_message_id: string - medusa_order_id: string - response_xml: string - has_error: boolean + created_at?: string | null + has_analysis_results?: boolean + has_error?: boolean + id?: string + medipost_external_order_id?: string | null + medipost_private_message_id?: string | null + medusa_order_id?: string | null + response_xml?: string | null + xml?: string | null } + Update: { + action?: string + created_at?: string | null + has_analysis_results?: boolean + has_error?: boolean + id?: string + medipost_external_order_id?: string | null + medipost_private_message_id?: string | null + medusa_order_id?: string | null + response_xml?: string | null + xml?: string | null + } + Relationships: [] } medreport_product_groups: { Row: { @@ -1957,6 +2030,13 @@ export type Database = { personal_code: string }[] } + get_latest_medipost_dispatch_state_for_order: { + Args: { medusa_order_id: string } + Returns: { + action_date: string + has_success: boolean + }[] + } get_medipost_dispatch_tries: { Args: { p_medusa_order_id: string } Returns: number @@ -2049,9 +2129,9 @@ export type Database = { Args: { account_id: string; user_id: string } Returns: boolean } - medipost_retry_dispatch: { - Args: { order_id: string } - Returns: Json + order_has_medipost_dispatch_error: { + Args: { medusa_order_id: string } + Returns: boolean } revoke_nonce: { Args: { p_id: string; p_reason?: string } @@ -2078,16 +2158,26 @@ export type Database = { Returns: undefined } update_account: { - Args: { - p_city: string - p_has_consent_personal_data: boolean - p_last_name: string - p_name: string - p_personal_code: string - p_phone: string - p_uid: string - p_email: string - } + Args: + | { + p_city: string + p_email: string + p_has_consent_personal_data: boolean + p_last_name: string + p_name: string + p_personal_code: string + p_phone: string + p_uid: string + } + | { + p_city: string + p_has_consent_personal_data: boolean + p_last_name: string + p_name: string + p_personal_code: string + p_phone: string + p_uid: string + } Returns: undefined } update_analysis_order_status: {