Spaces:
Running
Running
Update main.py
Browse files
main.py
CHANGED
|
@@ -1026,6 +1026,94 @@ def delete_project(project_id):
|
|
| 1026 |
#-------------------------
|
| 1027 |
import math
|
| 1028 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1029 |
@app.route('/api/debug/test-agent', methods=['GET'])
|
| 1030 |
def test_agent():
|
| 1031 |
"""
|
|
|
|
| 1026 |
#-------------------------
|
| 1027 |
import math
|
| 1028 |
|
| 1029 |
+
# This code should be added to your existing main.py file.
|
| 1030 |
+
# It assumes 'db_ref', 'client', 'logger', and 'verify_token' are already defined.
|
| 1031 |
+
|
| 1032 |
+
def summarize_user_history(uid):
|
| 1033 |
+
"""
|
| 1034 |
+
Fetches all of a user's past transcripts, sends them to Gemini for analysis,
|
| 1035 |
+
and returns a concise, actionable summary for the AI agent.
|
| 1036 |
+
"""
|
| 1037 |
+
try:
|
| 1038 |
+
logger.info(f"[BRIEFING] Fetching transcript history for user: {uid}")
|
| 1039 |
+
transcripts_ref = db_ref.child(f'transcripts/{uid}')
|
| 1040 |
+
all_transcripts = transcripts_ref.get()
|
| 1041 |
+
|
| 1042 |
+
if not all_transcripts:
|
| 1043 |
+
logger.info(f"[BRIEFING] No history found for user {uid}. Returning 'new user' summary.")
|
| 1044 |
+
return "This is a new user."
|
| 1045 |
+
|
| 1046 |
+
history_list = []
|
| 1047 |
+
for transcript_data in all_transcripts.values():
|
| 1048 |
+
history_list.append({
|
| 1049 |
+
"date": transcript_data.get("createdAt"),
|
| 1050 |
+
"project_title": transcript_data.get("projectId"),
|
| 1051 |
+
"transcript": transcript_data.get("transcript")
|
| 1052 |
+
})
|
| 1053 |
+
|
| 1054 |
+
history_json = json.dumps(history_list, indent=2)
|
| 1055 |
+
|
| 1056 |
+
analyst_prompt = """
|
| 1057 |
+
You are a world-class executive assistant and data analyst. Your job is to analyze a user's conversation history with a DIY expert AI named Alfred. Your goal is to produce a 'Pre-Call Briefing' for Alfred that is concise, insightful, and focuses on personal details.
|
| 1058 |
+
|
| 1059 |
+
The history will be a JSON string containing a list of past conversations. Each conversation has a date, project title, and a full transcript.
|
| 1060 |
+
|
| 1061 |
+
Your task is to identify patterns and extract the following key details. If a detail is not mentioned, omit that line from the output.
|
| 1062 |
+
- Personal Context: Identify who the projects are for (e.g., 'wife', 'son') and any mentioned personal details (e.g., 'has a dog', 'works as a teacher').
|
| 1063 |
+
- Preferences: Note any mentioned tastes or preferences (e.g., 'likes rustic style', 'favorite color is green').
|
| 1064 |
+
- Skill Assessment: Assess their DIY skill level (e.g., 'Beginner, hesitant with power tools', 'Intermediate, comfortable with plumbing').
|
| 1065 |
+
- Tool Inventory: List any specific tools they have mentioned owning or needing (e.g., 'Owns a power drill', 'Needed to buy a special wrench').
|
| 1066 |
+
- Recurring Themes: Identify any patterns in their questions or struggles (e.g., 'Often asks for clarification on measurements', 'Struggles with painting techniques').
|
| 1067 |
+
|
| 1068 |
+
Your output MUST be a series of bullet points. Do not add any conversational text or greetings. Start the entire response with 'Here is your briefing on this user:'.
|
| 1069 |
+
|
| 1070 |
+
Example Output:
|
| 1071 |
+
Here is your briefing on this user:
|
| 1072 |
+
* Is building this project for: their daughter
|
| 1073 |
+
* Personal context: has a golden retriever
|
| 1074 |
+
* Assessed skill level: Beginner, but learns quickly
|
| 1075 |
+
* Known tools: owns a basic screwdriver set
|
| 1076 |
+
* Recurring themes: frequently asks for pet-safe material recommendations
|
| 1077 |
+
"""
|
| 1078 |
+
|
| 1079 |
+
logger.info(f"[BRIEFING] Sending {len(history_list)} transcripts to Gemini for summarization.")
|
| 1080 |
+
|
| 1081 |
+
# ✅ THE FIX: Using the exact model name and API call structure you provided.
|
| 1082 |
+
response = client.models.generate_content(
|
| 1083 |
+
model='gemini-2.0-flash-lite',
|
| 1084 |
+
contents=[analyst_prompt, history_json]
|
| 1085 |
+
)
|
| 1086 |
+
|
| 1087 |
+
summary = response.text.strip()
|
| 1088 |
+
logger.info(f"[BRIEFING] Received summary from Gemini: {summary}")
|
| 1089 |
+
return summary
|
| 1090 |
+
|
| 1091 |
+
except Exception as e:
|
| 1092 |
+
logger.error(f"[BRIEFING] Failed to generate user summary for {uid}: {e}")
|
| 1093 |
+
return "Could not retrieve user history."
|
| 1094 |
+
|
| 1095 |
+
|
| 1096 |
+
@app.route('/api/user/call-briefing', methods=['GET'])
|
| 1097 |
+
def get_call_briefing():
|
| 1098 |
+
"""
|
| 1099 |
+
The single endpoint for the frontend to call before starting a conversation.
|
| 1100 |
+
It orchestrates getting the user's history and summarizing it.
|
| 1101 |
+
"""
|
| 1102 |
+
logger.info("[BRIEFING] Received request for a new call briefing.")
|
| 1103 |
+
|
| 1104 |
+
uid = verify_token(request.headers.get('Authorization'))
|
| 1105 |
+
if not uid:
|
| 1106 |
+
return jsonify({'error': 'Unauthorized'}), 401
|
| 1107 |
+
|
| 1108 |
+
try:
|
| 1109 |
+
memory_summary = summarize_user_history(uid)
|
| 1110 |
+
|
| 1111 |
+
return jsonify({"memory_summary": memory_summary}), 200
|
| 1112 |
+
|
| 1113 |
+
except Exception as e:
|
| 1114 |
+
logger.error(f"An unexpected error occurred in get_call_briefing for user {uid}: {e}")
|
| 1115 |
+
return jsonify({'error': 'Failed to generate call briefing.'}), 500
|
| 1116 |
+
|
| 1117 |
@app.route('/api/debug/test-agent', methods=['GET'])
|
| 1118 |
def test_agent():
|
| 1119 |
"""
|