-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
623 lines (546 loc) · 26.9 KB
/
Copy pathapp.py
File metadata and controls
623 lines (546 loc) · 26.9 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
import sys
import os
from typing import List, Optional
from datetime import datetime, timedelta
import yaml
# For agent
from agno.agent import Agent
from agno.team.team import Team
from agno.tools import tool
from agno.db.sqlite import SqliteDb
# For environment variables
from dotenv import load_dotenv
load_dotenv() # Load environment variables
# Import calendar utilities
from calendar_utils import generate_all_calendar_links, format_calendar_links_html
# Import enhanced email utilities
from enhanced_email_utils import (
send_enhanced_apartment_listings_email,
send_enhanced_apartment_appointment_email,
create_enhanced_apartment_listings_email
)
# Import simplified email tools that work reliably
from simple_email_tools import (
send_beautiful_apartment_email,
send_apartment_search_results,
schedule_apartment_viewing,
search_apartments_by_criteria
)
# Default email configuration from environment
DEFAULT_EMAIL = os.getenv("DEFAULT_EMAIL")
# Load email template from external file
def load_email_template(template_name: str) -> str:
"""
Load an email template from the email_templates directory.
Args:
template_name (str): Name of the template file (without .html extension)
Returns:
str: Template content
"""
template_path = os.path.join("email_templates", f"{template_name}.html")
try:
with open(template_path, "r", encoding="utf-8") as file:
return file.read()
except FileNotFoundError:
print(f"⚠️ Template file not found: {template_path}")
# Fallback to a simple template
return """
<html>
<body>
<h2>Apartment Viewing Appointment</h2>
<p><strong>Apartment:</strong> {name_of_apartment}</p>
<p><strong>When:</strong> {appointment_time}</p>
<p><strong>Where:</strong> {address_of_apartment}</p>
<p><strong>Price:</strong> {price_of_apartment}</p>
<p>{calendar_link} | {maps_link}</p>
<p>Best regards,<br>Rent Hunting AI Assistant</p>
</body>
</html>
"""
except Exception as e:
print(f"❌ Error loading template: {e}")
return "Error loading email template"
# Default email template for apartment appointments
DEFAULT_APARTMENT_EMAIL_TEMPLATE = load_email_template("apartment_appointment")
# For model configuration
from models import load_model_instance
# For custom tools
from city_data_extractor import extract_city_data
from tidb_customer_tool import fetch_apartments
# Get current date and time for instructions
from datetime import datetime
current_datetime = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
# Load city data
with open("data/country_and_city_urls.yaml", "r", encoding="utf-8") as file:
country_and_city_urls = yaml.safe_load(file)
# Store agent sessions in a SQLite database
# Delete the existing database file if it exists
if os.path.exists("tmp/agent.db"):
os.remove("tmp/agent.db")
# Try to load model instance
try:
model_instance = load_model_instance("gemini")
print("✅ Model loaded successfully")
except Exception as e:
print(f"❌ Error loading model: {e}")
sys.exit(1)
# Try to import and setup MCP servers
MCP_AVAILABLE = False
try:
from email_server import EmailMCPServer
from mcp_tools import MCPRegistry, MCPClient
# Setup MCP servers
email_server = EmailMCPServer()
registry = MCPRegistry()
registry.register_server(email_server)
mcp_client = MCPClient(registry)
MCP_AVAILABLE = True
print("✅ MCP servers initialized successfully")
except Exception as e:
print(f"⚠️ MCP servers not available: {e}")
print("Agent will run without email/calendar functionality")
# Helper function to format apartment appointment emails
def format_apartment_email(name_of_apartment: str, address_of_apartment: str,
price_of_apartment: str, appointment_time: str = "10 AM tomorrow",
calendar_link: str = "Add to Calendar", maps_link: str = "View on Maps") -> str:
"""
Format an apartment appointment email using the default template.
Args:
name_of_apartment (str): Name of the apartment/property
address_of_apartment (str): Full address of the apartment
price_of_apartment (str): Price information (e.g., "$3000/month")
appointment_time (str): Appointment time (default: "10 AM tomorrow")
calendar_link (str): Calendar link text or HTML
maps_link (str): Maps link text or HTML
Returns:
str: Formatted email content
"""
# Use string replacement to avoid format string issues
template = DEFAULT_APARTMENT_EMAIL_TEMPLATE
template = template.replace("{name_of_apartment}", name_of_apartment)
template = template.replace("{appointment_time}", appointment_time)
template = template.replace("{address_of_apartment}", address_of_apartment)
template = template.replace("{price_of_apartment}", price_of_apartment)
template = template.replace("{calendar_link}", calendar_link)
template = template.replace("{maps_link}", maps_link)
return template
def _parse_appointment_datetime(appointment_time: str, appointment_date: str, duration_minutes: int = 60) -> tuple:
"""
Parse human-readable appointment time and date into ISO datetime strings.
Args:
appointment_time (str): Time like "10 AM", "2 PM", "14:30"
appointment_date (str): Date like "tomorrow", "2025-09-15", "today"
duration_minutes (int): Duration in minutes
Returns:
tuple: (start_datetime_iso, end_datetime_iso)
"""
import re
from datetime import datetime, timedelta
try:
# Parse the date
base_date = datetime.now()
if appointment_date.lower() in ["today"]:
target_date = base_date.date()
elif appointment_date.lower() in ["tomorrow"]:
target_date = (base_date + timedelta(days=1)).date()
else:
# Try to parse as ISO date
try:
target_date = datetime.fromisoformat(appointment_date).date()
except:
# Default to tomorrow if parsing fails
target_date = (base_date + timedelta(days=1)).date()
# Parse the time
time_str = appointment_time.strip().upper()
# Handle formats like "10 AM", "2 PM", "10:30 AM"
am_pm_match = re.match(r'(\d{1,2})(?::(\d{2}))?\s*(AM|PM)', time_str)
if am_pm_match:
hour = int(am_pm_match.group(1))
minute = int(am_pm_match.group(2)) if am_pm_match.group(2) else 0
is_pm = am_pm_match.group(3) == 'PM'
if is_pm and hour != 12:
hour += 12
elif not is_pm and hour == 12:
hour = 0
else:
# Handle 24-hour format like "14:30"
time_match = re.match(r'(\d{1,2}):(\d{2})', time_str)
if time_match:
hour = int(time_match.group(1))
minute = int(time_match.group(2))
else:
# Default to 10 AM if parsing fails
hour, minute = 10, 0
# Create start datetime
start_dt = datetime.combine(target_date, datetime.min.time().replace(hour=hour, minute=minute))
end_dt = start_dt + timedelta(minutes=duration_minutes)
# Convert to ISO format
return start_dt.isoformat(), end_dt.isoformat()
except Exception as e:
# Fallback to reasonable defaults
print(f"Warning: Could not parse appointment datetime: {e}")
start_dt = datetime.now() + timedelta(days=1, hours=10-datetime.now().hour, minutes=-datetime.now().minute)
end_dt = start_dt + timedelta(minutes=duration_minutes)
return start_dt.isoformat(), end_dt.isoformat()
def send_apartment_appointment_email(name_of_apartment: str, address_of_apartment: str,
price_of_apartment: str, appointment_time: str = "10 AM tomorrow",
appointment_date: str = "tomorrow", duration_minutes: int = 60,
to_email: str = None) -> str:
"""
Send an apartment appointment email using the default template with functional calendar links.
Args:
name_of_apartment (str): Name of the apartment/property
address_of_apartment (str): Full address of the apartment
price_of_apartment (str): Price information
appointment_time (str): Appointment time (e.g., "10 AM tomorrow", "2 PM")
appointment_date (str): Appointment date (e.g., "tomorrow", "2025-09-15")
duration_minutes (int): Duration of appointment in minutes (default: 60)
to_email (str): Recipient email (defaults to DEFAULT_EMAIL)
Returns:
str: Status of email sending
"""
if to_email is None:
to_email = DEFAULT_EMAIL
# Create calendar and maps links
maps_link = f'<a href="https://maps.google.com/maps?q={address_of_apartment.replace(" ", "+")}" target="_blank">View on Maps</a>'
# Generate proper calendar functionality
try:
# Parse the appointment time to create proper datetime strings
start_datetime, end_datetime = _parse_appointment_datetime(appointment_time, appointment_date, duration_minutes)
# Generate calendar links for all services
calendar_links = generate_all_calendar_links(
title=f"Apartment Viewing: {name_of_apartment}",
start_datetime=start_datetime,
end_datetime=end_datetime,
description=f"Apartment viewing appointment for {name_of_apartment}\nPrice: {price_of_apartment}\nLocation: {address_of_apartment}",
location=address_of_apartment,
timezone="America/New_York"
)
# Create HTML calendar links
calendar_link = format_calendar_links_html(calendar_links, f"Apartment Viewing: {name_of_apartment}")
except Exception as e:
# Fallback to simple text if calendar generation fails
print(f"Warning: Could not generate calendar links: {e}")
calendar_link = f'<a href="#" onclick="alert(\'Please manually add this appointment to your calendar: {appointment_time} at {address_of_apartment}\')">Add to Calendar</a>'
email_content = format_apartment_email(
name_of_apartment=name_of_apartment,
address_of_apartment=address_of_apartment,
price_of_apartment=price_of_apartment,
appointment_time=appointment_time,
calendar_link=calendar_link,
maps_link=maps_link
)
subject = f"Apartment Viewing Appointment: {name_of_apartment}"
if MCP_AVAILABLE:
return send_email_tool(
to_email=to_email,
subject=subject,
message=email_content,
is_html=True
)
else:
from app import send_email
return send_email(subject=subject, body=email_content, to_address=to_email)
# Define custom tools that use MCP servers (only if available)
def send_email_tool(to_email: str = DEFAULT_EMAIL, subject: str = "", message: str = "",
event_title: str = "", event_start: str = "", event_end: str = "",
event_description: str = "", event_location: str = "",
event_timezone: str = "America/New_York", is_html: bool = True) -> str:
"""
Send an email using the email MCP server with optional calendar event links.
Args:
to_email (str): Recipient's email address
subject (str): Email subject
message (str): Email message content
event_title (str): Calendar event title (optional, if provided, adds calendar links)
event_start (str): Event start time in ISO format (e.g., '2025-09-20T14:00:00')
event_end (str): Event end time in ISO format (e.g., '2025-09-20T15:00:00')
event_description (str): Event description (optional)
event_location (str): Event location (optional)
event_timezone (str): Event timezone (default: 'America/New_York')
is_html (bool): Whether message is HTML formatted (default: True)
Returns:
str: Status of the email sending operation
"""
if not MCP_AVAILABLE:
return "❌ Email functionality not available - MCP server not initialized"
try:
# Prepare parameters
email_params = {
"to_email": to_email,
"subject": subject,
"message": message,
"is_html": is_html
}
# Add calendar event parameters if provided
if event_title and event_start and event_end:
email_params.update({
"event_title": event_title,
"event_start": event_start,
"event_end": event_end,
"event_description": event_description,
"event_location": event_location,
"event_timezone": event_timezone
})
result = mcp_client.call("email-server", "send_email", **email_params)
if result['status'] == 'success':
calendar_note = " with calendar links" if event_title else ""
return f"✅ Email{calendar_note} successfully sent to {result['to_email']}"
else:
return f"❌ Failed to send email: {result['error']}"
except Exception as e:
return f"❌ Error sending email: {str(e)}"
def create_calendar_event_tool(title: str, description: str, start_datetime: str,
end_datetime: str, location: str = "",
attendees_emails: str = "", timezone: str = "America/New_York") -> str:
"""
Create a calendar event using the Google Calendar MCP server.
Args:
title (str): Event title
description (str): Event description
start_datetime (str): Start time in ISO format (e.g., "2025-09-20T14:00:00")
end_datetime (str): End time in ISO format (e.g., "2025-09-20T15:00:00")
location (str): Event location (optional)
attendees_emails (str): Comma-separated list of attendee email addresses (optional)
timezone (str): Timezone for the event (default: "America/New_York")
Returns:
str: Details of the created event
"""
if not MCP_AVAILABLE:
return "❌ Calendar functionality not available - MCP server not initialized"
try:
# Convert comma-separated string to list
attendees = []
if attendees_emails.strip():
attendees = [email.strip() for email in attendees_emails.split(",")]
result = mcp_client.call("google-calendar-server", "create_event",
title=title,
description=description,
start_datetime=start_datetime,
end_datetime=end_datetime,
location=location,
attendees=attendees,
timezone=timezone)
return f"📅 Event created successfully!\nEvent ID: {result['event_id']}\nLink: {result['event_link']}"
except Exception as e:
return f"❌ Error creating calendar event: {str(e)}"
def send_email_with_calendar_event_tool(to_email: str = DEFAULT_EMAIL, subject: str = "", message: str = "",
event_title: str = "", event_start: str = "", event_end: str = "",
event_description: str = "", event_location: str = "",
event_timezone: str = "America/New_York") -> str:
"""
Send an email with calendar event "Add to Calendar" links.
Args:
to_email (str): Recipient's email address
subject (str): Email subject
message (str): Email message content
event_title (str): Calendar event title
event_start (str): Event start time in ISO format (e.g., '2025-09-20T14:00:00')
event_end (str): Event end time in ISO format (e.g., '2025-09-20T15:00:00')
event_description (str): Event description (optional)
event_location (str): Event location (optional)
event_timezone (str): Event timezone (default: 'America/New_York')
Returns:
str: Status of the email sending operation
"""
return send_email_tool(
to_email=to_email,
subject=subject,
message=message,
event_title=event_title,
event_start=event_start,
event_end=event_end,
event_description=event_description,
event_location=event_location,
event_timezone=event_timezone,
is_html=True # Use HTML for better calendar link formatting
)
def send_gmail_calendar_email_tool(to_email: str = DEFAULT_EMAIL, subject: str = "", message: str = "",
event_title: str = "", event_start: str = "", event_end: str = "",
event_description: str = "", event_location: str = "",
event_timezone: str = "America/New_York", sender_name: str = "") -> str:
"""
Send a Gmail-optimized email with calendar event integration.
This tool creates professional-looking emails that render perfectly in Gmail
with prominent Google Calendar integration.
Args:
to_email (str): Recipient's email address
subject (str): Email subject
message (str): Email message content
event_title (str): Calendar event title
event_start (str): Event start time in ISO format (e.g., '2025-09-20T14:00:00')
event_end (str): Event end time in ISO format (e.g., '2025-09-20T15:00:00')
event_description (str): Event description (optional)
event_location (str): Event location (optional)
event_timezone (str): Event timezone (default: 'America/New_York')
sender_name (str): Name of the sender (optional)
Returns:
str: Status of the email sending operation
"""
if not MCP_AVAILABLE:
return "❌ Email functionality not available - MCP server not initialized"
try:
result = mcp_client.call("email-server", "send_gmail_calendar_email",
to_email=to_email,
subject=subject,
message=message,
event_title=event_title,
event_start=event_start,
event_end=event_end,
event_description=event_description,
event_location=event_location,
event_timezone=event_timezone,
sender_name=sender_name,
use_simple_button=True)
if result['status'] == 'success':
return f"✅ Gmail-optimized email with Google Calendar integration sent to {result['to_email']}"
else:
return f"❌ Failed to send Gmail email: {result['error']}"
except Exception as e:
return f"❌ Error sending Gmail email: {str(e)}"
# Prepare tools based on MCP availability
email_tools = []
if MCP_AVAILABLE:
email_tools = [send_apartment_search_results, schedule_apartment_viewing, send_gmail_calendar_email_tool]
else:
# Fallback simple email function
def send_email(subject: str = "", body: str = "", to_address: str = DEFAULT_EMAIL) -> str:
"""
Send an email.
Args:
subject (str): The subject of the email.
body (str): The body of the email.
to_address (str): The address to send the email to (defaults to value from .env).
"""
return f"Sent email to {to_address} with subject {subject} and body {body}"
email_tools = [send_apartment_search_results, schedule_apartment_viewing]
# Create the web agent
web_agent = Agent(
name="Web Search Agent",
model=model_instance,
tools=[extract_city_data],
instructions=[
"Search your knowledge before answering the question.",
"When providing data, format it in a tabular format.",
"Include sources in your response.",
"Generate a brief and concise summary of the search results.",
"Only include the report in your response. No other text.",
f"Source: {country_and_city_urls}",
],
markdown=True,
)
# Create the apartment agent
apartment_agent = Agent(
name="Apartment Data Agent",
role="Analyze and provide list of apartments that match user criteria",
model=model_instance,
tools=[search_apartments_by_criteria],
instructions=[
"Query apartment database for relevant property information",
"Present data in clear, structured tables with key metrics",
"Include state, name, address, price, bed_info, phone",
],
markdown=True,
)
email_agent = Agent(
name="Email Agent",
role="Send email notifications and manage calendar events",
model=model_instance,
tools=email_tools,
instructions=[
f"You are a proactive email assistant. Current date/time: {current_datetime}",
f"Default email recipient: {DEFAULT_EMAIL}",
"SEND EMAILS IMMEDIATELY when requested - no delays, no apologies, no asking permission.",
"When user asks for apartment listings via email, IMMEDIATELY call send_apartment_search_results.",
"",
"PRIMARY FUNCTION - send_apartment_search_results:",
"- Automatically searches database for ANY city (Houston, Austin, Dallas, Texas City, etc.)",
"- Creates beautiful HTML emails with Google Maps integration",
"- Parameters: search_location (user's city), budget (user's max rent), recipient_email",
"- ALWAYS use the exact city and budget the user specifies",
"- Works for any city in the database - not just Texas City",
"",
"BEHAVIOR:",
"- Extract city, budget, and email from user request",
"- Call send_apartment_search_results immediately",
"- Confirm what was sent after completion",
"- Offer additional services like appointment scheduling",
"",
"NEVER say 'I apologize' or 'I cannot' - just send the email immediately.",
"The tool handles everything: database search, HTML formatting, Google Maps, and delivery."
] + (
[
"You can send emails to any recipient with a subject and message.",
"You can create calendar events with title, description, date/time, location, and attendees.",
"You can send emails WITH calendar event links that allow recipients to easily add events to their calendars.",
"IMPORTANT: Since users primarily use Gmail, ALWAYS prefer send_gmail_calendar_email_tool for meeting invitations as it creates professional Gmail-optimized emails with prominent Google Calendar integration.",
"The Gmail-optimized emails render perfectly in Gmail with beautiful formatting, event details, and a prominent 'Add to Google Calendar' button.",
"For Gmail users, the send_gmail_calendar_email_tool provides the best experience with table-based HTML that works perfectly in Gmail's interface.",
"When sending meeting invitations or event notifications, prioritize send_gmail_calendar_email_tool over other email tools.",
"Calendar links support Google Calendar (primary), Outlook, Yahoo Calendar, and ICS files for other calendar apps.",
"When scheduling events, always ask for essential details like title, date, time, and duration.",
"For email sending, ensure you have the recipient, subject, and message content.",
"Use proper date/time formats for calendar events (ISO format: YYYY-MM-DDTHH:MM:SS).",
"For attendees, use comma-separated email addresses if multiple attendees are needed.",
"Be helpful in suggesting appropriate email subjects and event titles if not provided.",
"Always confirm the details before sending emails or creating events.",
"Include sender name when using Gmail-optimized emails for a professional touch.",
] if MCP_AVAILABLE else [
"Email and calendar functionality is currently not available, but I can help answer questions and provide assistance."
]
),
markdown=True,
)
# Create the rent hunting team
rent_hunting_team = Team(
name="Rent Hunting AI Team",
model=model_instance,
members=[apartment_agent, web_agent, email_agent],
instructions=[
"You are a collaborative team of AI agents specializing in apartment hunting and rental research.",
"Work systematically to help users find the perfect rental property based on their criteria.",
"WORKFLOW:",
"1. Apartment Agent: Search database for properties matching user criteria (location, budget, bedrooms, etc.)",
"2. Email Agent: IMMEDIATELY send beautiful apartment listings to user's email using send_apartment_search_results",
"3. Web Agent: Supplement with additional market research if needed",
"",
"IMPORTANT EMAIL BEHAVIOR:",
"- When user requests apartment listings via email, IMMEDIATELY send them using send_apartment_search_results",
"- Do NOT apologize or ask permission - just send the beautiful apartment listings right away",
"- Extract the city, budget, and email from user request and send immediately",
"- After sending, confirm what was sent and offer additional services",
"",
"COLLABORATION GUIDELINES:",
"- Work quickly and efficiently to fulfill user requests",
"- Share findings between agents to build comprehensive property profiles",
"- Prioritize immediate action when user requests email delivery",
"- Include practical details: pricing, availability, contact information, and location benefits",
"",
"EMAIL SERVICES:",
"- Use send_apartment_search_results for apartment listings (takes: search_location, budget, recipient_email)",
"- Use schedule_apartment_viewing for scheduling property viewings",
"- ALWAYS send emails immediately when requested - no delays or apologies",
"",
"Always maintain a helpful, professional tone focused on finding the best rental solutions."
],
markdown=True,
show_members_responses=True,
db=SqliteDb(db_file="tmp/agent.db"),
add_history_to_context=True,
)
if __name__ == "__main__":
print("="*80)
print("🤖 Welcome to the Rent Hunting AI Team!")
print("You can ask questions about apartment hunting, rental properties, and market research.")
print("Type 'exit' or 'quit' to end the session.")
while True:
user_input = input("\n📝 Enter your apartment search query (or 'exit' to quit): ").strip()
if user_input.lower() in ['exit', 'quit'] or user_input == '':
print("👋 Exiting the Rent Hunting AI Team. Goodbye!")
break
rent_hunting_team.print_response(
user_input,
stream=True,
show_full_reasoning=True,
stream_intermediate_steps=True,
)