114 lines
3.4 KiB
Python
114 lines
3.4 KiB
Python
"""E-Mail Tools (IMAP)."""
|
|
|
|
from core import config, mail_client
|
|
|
|
TOOLS = [
|
|
{
|
|
"type": "function",
|
|
"function": {
|
|
"name": "get_mail_summary",
|
|
"description": "E-Mail Zusammenfassung: Letzte Mails, Absender, Betreff.",
|
|
"parameters": {"type": "object", "properties": {}, "required": []},
|
|
},
|
|
},
|
|
{
|
|
"type": "function",
|
|
"function": {
|
|
"name": "get_mail_count",
|
|
"description": "Anzahl E-Mails: gesamt und ungelesen.",
|
|
"parameters": {"type": "object", "properties": {}, "required": []},
|
|
},
|
|
},
|
|
{
|
|
"type": "function",
|
|
"function": {
|
|
"name": "search_mail",
|
|
"description": "Volltextsuche in E-Mails nach Betreff/Absender.",
|
|
"parameters": {
|
|
"type": "object",
|
|
"properties": {
|
|
"query": {"type": "string", "description": "Suchbegriff"},
|
|
"days": {"type": "number", "description": "Zeitraum in Tagen (default: 30)", "default": 30}
|
|
},
|
|
"required": ["query"],
|
|
},
|
|
},
|
|
},
|
|
{
|
|
"type": "function",
|
|
"function": {
|
|
"name": "get_todays_mails",
|
|
"description": "Alle heutigen E-Mails auflisten.",
|
|
"parameters": {"type": "object", "properties": {}, "required": []},
|
|
},
|
|
},
|
|
{
|
|
"type": "function",
|
|
"function": {
|
|
"name": "get_smart_mail_digest",
|
|
"description": "KI-Zusammenfassung der letzten Mails: Was ist wichtig, was kann warten?",
|
|
"parameters": {
|
|
"type": "object",
|
|
"properties": {
|
|
"hours": {"type": "number", "description": "Zeitraum in Stunden (default: 24)", "default": 24}
|
|
},
|
|
"required": [],
|
|
},
|
|
},
|
|
},
|
|
]
|
|
|
|
|
|
def _cfg():
|
|
cfg = config.parse_config()
|
|
mail_client.init(cfg)
|
|
return cfg
|
|
|
|
|
|
def handle_get_mail_summary(**kw):
|
|
cfg = _cfg()
|
|
return mail_client.format_summary()
|
|
|
|
|
|
def handle_get_mail_count(**kw):
|
|
_cfg()
|
|
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']})"
|
|
|
|
|
|
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):
|
|
_cfg()
|
|
mails = mail_client.get_todays_mails()
|
|
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]}")
|
|
lines.append(f" -> {r['subject'][:70]}")
|
|
return "\n".join(lines)
|
|
|
|
|
|
def handle_get_smart_mail_digest(hours=24, **kw):
|
|
cfg = _cfg()
|
|
api_key = cfg.api_keys.get("openrouter_key", "")
|
|
digest = mail_client.get_smart_digest(hours=hours, api_key=api_key)
|
|
return mail_client.format_smart_digest(digest)
|
|
|
|
|
|
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_smart_mail_digest": handle_get_smart_mail_digest,
|
|
}
|