File size: 1,111 Bytes
41039aa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
from supabase import create_client, Client
import os
import json
from dotenv import load_dotenv

load_dotenv()

# Initialize Supabase client
supabase: Client = create_client(os.getenv("SUPABASE_URL"), os.getenv("SUPABASE_KEY"))

def get_user_history(user_id):
    response = supabase.table("history").select("messages").eq("user", user_id).order("created_at", desc=True).limit(1).execute()
    if response.data:
        return response.data[0]['messages']
    return []

def update_user_history(user_id, new_history):
    # Filter out messages with image type
    filtered_history = [
        message for message in new_history
        if not (message["role"] == "user" and
                isinstance(message["content"], list) and
                any(item.get("type") == "image" for item in message["content"]))
    ]
    
    # Insert the filtered history into the database
    supabase.table("history").insert({"user": user_id, "messages": filtered_history}).execute()

def delete_user_history(user_id):
    response = supabase.table("history").delete().eq("user", user_id).execute()
    return response.data