Skip to content

Commit 1356976

Browse files
Merge pull request #105 from DhruvK278/enhancement/persistentDB-#78
feat:Implement Supabase storage for conversation summaries
2 parents 519c9ae + 172401d commit 1356976

1 file changed

Lines changed: 51 additions & 5 deletions

File tree

backend/app/agents/devrel/nodes/summarization.py

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
import logging
22
from datetime import datetime, timedelta
3-
from typing import Dict, Any
3+
from typing import Dict, Any, Optional
44
from app.agents.state import AgentState
55
from langchain_core.messages import HumanMessage
66
from app.agents.devrel.prompts.summarization_prompt import CONVERSATION_SUMMARY_PROMPT
7+
from app.database.supabase.client import get_supabase_client
8+
9+
supabase = get_supabase_client()
10+
11+
def isoformat_or_none(dt: Optional[datetime]) -> Optional[str]:
12+
return dt.isoformat() if dt else None
13+
714

815
logger = logging.getLogger(__name__)
916

@@ -129,7 +136,46 @@ async def _extract_key_topics(summary: str, llm) -> list[str]:
129136
return []
130137

131138
async def store_summary_to_database(state: AgentState) -> None:
132-
# TODO: Implement database storage to user_interactions
133-
"""Store the summary in PostgreSQL database"""
134-
logger.info(f"[PLACEHOLDER] Storing summary for session {state.session_id}")
135-
pass
139+
"""Store the summary in Supabase database"""
140+
logger.info(f"Storing summary for session {state.session_id} into conversation_context")
141+
142+
try:
143+
# Validate required fields
144+
if not state.user_id or not state.platform:
145+
logger.error(f"Missing required fields: user_id={state.user_id}, platform={state.platform}")
146+
return
147+
148+
platform_id = state.user_id
149+
platform_column = f"{state.platform}_id"
150+
151+
# Fetch the user's UUID from the 'users' table
152+
user_response = await supabase.table("users").select("id").eq(platform_column, platform_id).limit(1).execute()
153+
154+
if not user_response.data:
155+
logger.error(f"User with {platform_column} '{platform_id}' not found in users table.")
156+
return
157+
158+
user_uuid = user_response.data[0]['id']
159+
logger.info(f"Found user UUID: {user_uuid} for {platform_column}: {platform_id}")
160+
161+
# Record to insert/update
162+
record = {
163+
"user_id": user_uuid,
164+
"conversation_summary": state.conversation_summary,
165+
"key_topics": state.key_topics,
166+
"total_interactions": state.interaction_count,
167+
"session_start_time": isoformat_or_none(state.session_start_time),
168+
"session_end_time": isoformat_or_none(datetime.now()),
169+
}
170+
171+
# Upsert based on the user_id to ensure one summary per user
172+
response = await supabase.table("conversation_context").upsert(record, on_conflict="user_id").execute()
173+
174+
# Checks
175+
if response.data and len(response.data) > 0:
176+
logger.info(f"✅ Summary stored successfully for session {state.session_id}")
177+
else:
178+
logger.error(f"❌ Supabase upsert failed for session {state.session_id}: {response}")
179+
180+
except Exception as e:
181+
logger.error(f"Unexpected error while storing summary: {str(e)}")

0 commit comments

Comments
 (0)