Mail-Tool: Zeitraum-Abfrage + bessere Tool-Beschreibungen fuer LLM
This commit is contained in:
parent
eb3fd58826
commit
983d93a67b
1 changed files with 53 additions and 18 deletions
|
|
@ -7,7 +7,7 @@ TOOLS = [
|
|||
"type": "function",
|
||||
"function": {
|
||||
"name": "get_mail_summary",
|
||||
"description": "E-Mail Zusammenfassung: Letzte Mails, Absender, Betreff.",
|
||||
"description": "E-Mail Zusammenfassung: Letzte Mails, Absender, Betreff + wichtige Mails der letzten 48h.",
|
||||
"parameters": {"type": "object", "properties": {}, "required": []},
|
||||
},
|
||||
},
|
||||
|
|
@ -15,7 +15,7 @@ TOOLS = [
|
|||
"type": "function",
|
||||
"function": {
|
||||
"name": "get_mail_count",
|
||||
"description": "Anzahl E-Mails: gesamt und ungelesen.",
|
||||
"description": "Anzahl E-Mails: gesamt und ungelesen im Postfach.",
|
||||
"parameters": {"type": "object", "properties": {}, "required": []},
|
||||
},
|
||||
},
|
||||
|
|
@ -37,20 +37,26 @@ TOOLS = [
|
|||
{
|
||||
"type": "function",
|
||||
"function": {
|
||||
"name": "get_todays_mails",
|
||||
"description": "Alle heutigen E-Mails auflisten.",
|
||||
"parameters": {"type": "object", "properties": {}, "required": []},
|
||||
"name": "get_mails_by_period",
|
||||
"description": "E-Mails eines bestimmten Zeitraums auflisten. Nutze dieses Tool wenn der User nach Mails der letzten X Tage/Stunden fragt, oder nach heutigen Mails.",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"hours": {"type": "number", "description": "Zeitraum in Stunden. 24 = heute, 168 = 7 Tage, 720 = 30 Tage.", "default": 24}
|
||||
},
|
||||
"required": [],
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
"type": "function",
|
||||
"function": {
|
||||
"name": "get_smart_mail_digest",
|
||||
"description": "KI-Zusammenfassung der letzten Mails: Was ist wichtig, was kann warten?",
|
||||
"description": "KI-Zusammenfassung: klassifiziert Mails in wichtig/aktion/info/newsletter/spam. Nutze bei 'wichtige Mails', 'was ist relevant', 'persoenliche Mails'.",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"hours": {"type": "number", "description": "Zeitraum in Stunden (default: 24)", "default": 24}
|
||||
"hours": {"type": "number", "description": "Zeitraum in Stunden. 24 = heute, 168 = 7 Tage.", "default": 24}
|
||||
},
|
||||
"required": [],
|
||||
},
|
||||
|
|
@ -58,6 +64,14 @@ TOOLS = [
|
|||
},
|
||||
]
|
||||
|
||||
SYSTEM_PROMPT_EXTRA = """E-Mail Tools:
|
||||
- 'Welche Mails heute/diese Woche' → get_mails_by_period (hours=24 bzw 168)
|
||||
- 'Wichtige/persoenliche Mails' → get_smart_mail_digest (klassifiziert per KI)
|
||||
- 'Wie viele Mails' (gesamt) → get_mail_count
|
||||
- Suche nach Absender/Betreff → search_mail
|
||||
WICHTIG: get_mail_count zeigt NUR die Gesamtzahl im Postfach, NICHT einen Zeitraum. Fuer Zeitraum-Fragen IMMER get_mails_by_period oder get_smart_mail_digest nutzen.
|
||||
"""
|
||||
|
||||
|
||||
def _cfg():
|
||||
cfg = config.parse_config()
|
||||
|
|
@ -75,25 +89,46 @@ def handle_get_mail_count(**kw):
|
|||
counts = mail_client.get_mail_count()
|
||||
if "error" in counts:
|
||||
return f"Mail-Fehler: {counts['error']}"
|
||||
return f"E-Mails: {counts['total']} gesamt, {counts['unread']} ungelesen ({counts['account']})"
|
||||
return f"E-Mails im Postfach: {counts['total']} gesamt, {counts['unread']} ungelesen ({counts['account']})\n(Das ist der gesamte Posteingang, nicht ein bestimmter Zeitraum.)"
|
||||
|
||||
|
||||
def handle_search_mail(query, days=30, **kw):
|
||||
def handle_search_mail(query="", days=30, **kw):
|
||||
_cfg()
|
||||
results = mail_client.search_mail(query, days=days)
|
||||
return mail_client.format_search_results(results)
|
||||
|
||||
|
||||
def handle_get_todays_mails(**kw):
|
||||
def handle_get_mails_by_period(hours=24, **kw):
|
||||
"""Mails eines Zeitraums auflisten."""
|
||||
from datetime import datetime, timedelta
|
||||
_cfg()
|
||||
mails = mail_client.get_todays_mails()
|
||||
m = mail_client._connect()
|
||||
if not m:
|
||||
return "IMAP-Verbindung fehlgeschlagen"
|
||||
try:
|
||||
m.select("INBOX", readonly=True)
|
||||
since = (datetime.now() - timedelta(hours=hours)).strftime("%d-%b-%Y")
|
||||
_, data = m.search(None, f'(SINCE "{since}")')
|
||||
ids = data[0].split() if data[0] else []
|
||||
mails = []
|
||||
for mid in ids:
|
||||
_, msg_data = m.fetch(mid, "(BODY.PEEK[HEADER])")
|
||||
parsed = mail_client._parse_mail(msg_data)
|
||||
if parsed:
|
||||
mails.append(parsed)
|
||||
mails.reverse()
|
||||
except Exception as e:
|
||||
return f"Fehler: {e}"
|
||||
finally:
|
||||
m.logout()
|
||||
|
||||
if not mails:
|
||||
return "Heute keine Mails eingegangen."
|
||||
if "error" in mails[0]:
|
||||
return f"Mail-Fehler: {mails[0]['error']}"
|
||||
lines = [f"{len(mails)} Mail(s) heute:\n"]
|
||||
for r in mails:
|
||||
lines.append(f" {r['date_str']} | {r['from'][:35]}")
|
||||
return f"Keine Mails in den letzten {hours} Stunden."
|
||||
|
||||
days_label = f"{hours}h" if hours < 48 else f"{hours // 24} Tagen"
|
||||
lines = [f"{len(mails)} Mail(s) in den letzten {days_label}:\n"]
|
||||
for i, r in enumerate(mails, 1):
|
||||
lines.append(f"{i:2d}. {r['date_str']} | {r['from'][:35]}")
|
||||
lines.append(f" -> {r['subject'][:70]}")
|
||||
return "\n".join(lines)
|
||||
|
||||
|
|
@ -109,6 +144,6 @@ HANDLERS = {
|
|||
"get_mail_summary": handle_get_mail_summary,
|
||||
"get_mail_count": handle_get_mail_count,
|
||||
"search_mail": handle_search_mail,
|
||||
"get_todays_mails": handle_get_todays_mails,
|
||||
"get_mails_by_period": handle_get_mails_by_period,
|
||||
"get_smart_mail_digest": handle_get_smart_mail_digest,
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue