-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
1701 lines (1531 loc) · 68.9 KB
/
Copy pathapp.py
File metadata and controls
1701 lines (1531 loc) · 68.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
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import secrets
import requests
from flask import Flask, request, jsonify, redirect, make_response, abort
from config import (
SLACK_SIGNING_SECRET, SLACK_BOT_TOKEN, API_ENDPOINT, BACKEND_API_KEY, APP_VERSION,
STATE_SIGNING_SECRET, GOOGLE_CLIENT_ID, GOOGLE_CLIENT_SECRET, APP_EXTERNAL_URL,
)
from agent.parser import process_message, process_clarification
import databases.db as db_module
from databases.db import (
save_task,
init_db,
get_tasks_for_owner,
get_all_tasks,
mark_all_tasks_complete,
clear_all_tasks,
mark_initial_email_sent,
)
from agent.scheduler import start_scheduler
from utils.gmail_utils import send_email
from utils.email_templates import format_task_email_body
from utils.slack import (
send_slack_message_with_fallback,
FALLBACK_MESSAGE,
get_user_timezone,
BOT_USER_ID,
resolve_owner_mentions_to_emails,
)
from utils.time import zoneinfo_or_utc
from utils.gemini import call_gemini, take_last_gemini_user_hint
from slack_sdk import WebClient
from slack_sdk.signature import SignatureVerifier
from collections import OrderedDict, defaultdict
import threading
from concurrent.futures import ThreadPoolExecutor, as_completed
import re
import os
import json
import time
import hmac
import base64
import hashlib
import html as html_module
import urllib.request as urllib_request
from datetime import datetime, timedelta, time as dtime, timezone
from email.mime.text import MIMEText
from pydantic import BaseModel
from typing import Optional
from google_auth_oauthlib.flow import Flow
from google.oauth2.credentials import Credentials
from google.auth.transport.requests import Request as GoogleRequest
from googleapiclient.discovery import build
from databases.db import conn as db_conn
app = Flask(__name__)
init_db()
# ── Google OAuth / Calendar / Drive (merged from services/google_api.py) ──────
GOOGLE_SCOPES = [
"https://www.googleapis.com/auth/drive.readonly",
"https://www.googleapis.com/auth/calendar.readonly",
"https://www.googleapis.com/auth/calendar.events",
"https://www.googleapis.com/auth/gmail.send",
"https://www.googleapis.com/auth/userinfo.email",
"openid",
]
# In-memory OAuth state stores (reset on restart — acceptable for short-lived flows)
_FLOW_STORE: dict = {}
_PENDING_STATE_PROFILES: dict = {}
_PENDING_FLOWS_BY_STATE_ID: dict = {}
def _google_client_config() -> dict:
return {
"web": {
"client_id": GOOGLE_CLIENT_ID,
"client_secret": GOOGLE_CLIENT_SECRET,
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"redirect_uris": [f"{APP_EXTERNAL_URL}/auth/google/callback"],
}
}
def _sign_state(payload: dict) -> str:
secret = STATE_SIGNING_SECRET.encode("utf-8")
raw = json.dumps(payload, separators=(",", ":"), sort_keys=True).encode("utf-8")
sig = hmac.new(secret, raw, hashlib.sha256).digest()
blob = base64.urlsafe_b64encode(raw).decode("utf-8").rstrip("=")
sigb = base64.urlsafe_b64encode(sig).decode("utf-8").rstrip("=")
return f"{blob}.{sigb}"
def _verify_state(state: str) -> dict:
secret = STATE_SIGNING_SECRET.encode("utf-8")
try:
blob, sigb = state.split(".", 1)
raw = base64.urlsafe_b64decode(blob + "==")
sig = base64.urlsafe_b64decode(sigb + "==")
except Exception:
abort(400, description="Bad state format.")
expected = hmac.new(secret, raw, hashlib.sha256).digest()
if not hmac.compare_digest(sig, expected):
abort(400, description="Bad state signature.")
return json.loads(raw.decode("utf-8"))
def _load_creds(profile: str) -> Credentials:
c = db_conn()
try:
with c, c.cursor() as cur:
cur.execute("SELECT creds_json FROM profiles WHERE profile = %s", (profile,))
row = cur.fetchone()
if not row:
abort(404, description=f"Unknown profile '{profile}'. Connect it first.")
return Credentials.from_authorized_user_info(json.loads(row[0]), scopes=GOOGLE_SCOPES)
finally:
c.close()
def _save_creds(profile: str, creds: Credentials):
c = db_conn()
try:
with c, c.cursor() as cur:
cur.execute(
"""
INSERT INTO profiles(profile, creds_json, created_at)
VALUES (%s, %s, %s)
ON CONFLICT (profile)
DO UPDATE SET creds_json = EXCLUDED.creds_json, created_at = EXCLUDED.created_at
""",
(profile, creds.to_json(), datetime.now(timezone.utc).isoformat()),
)
finally:
c.close()
def _ensure_fresh(creds: Credentials) -> Credentials:
if creds.expired and creds.refresh_token:
creds.refresh(GoogleRequest())
return creds
def _db_store_pending_state(state_id: str, email: str):
c = db_conn()
try:
with c, c.cursor() as cur:
cur.execute(
"INSERT INTO profiles(profile, creds_json, created_at) VALUES (%s, %s, %s) ON CONFLICT (profile) DO UPDATE SET creds_json = EXCLUDED.creds_json",
(f"__pending__:{state_id}", json.dumps({"email": email}), datetime.now(timezone.utc).isoformat()),
)
finally:
c.close()
def _db_read_pending_state(state_id: str) -> str | None:
c = db_conn()
try:
with c, c.cursor() as cur:
cur.execute("SELECT creds_json FROM profiles WHERE profile = %s", (f"__pending__:{state_id}",))
row = cur.fetchone()
if row:
cur.execute("DELETE FROM profiles WHERE profile = %s", (f"__pending__:{state_id}",))
return json.loads(row[0]).get("email")
return None
finally:
c.close()
def _err_html(message: str):
escaped = html_module.escape(str(message))
return make_response(
f"<!DOCTYPE html><html><head><meta charset='utf-8'><title>Sign-in error</title></head>"
f"<body style='font-family:sans-serif;max-width:520px;margin:2em auto;padding:1em'>"
f"<h2>Sign-in error</h2><p>{escaped}</p>"
f"<p>Close this tab and try connecting again.</p></body></html>",
200,
)
_scheduler = None
def _should_start_scheduler() -> bool:
# Flask dev reloader imports the module twice; start only in the reloader child.
if os.environ.get("FLASK_RUN_FROM_CLI") == "true":
return os.environ.get("WERKZEUG_RUN_MAIN") == "true"
return True
if _should_start_scheduler():
try:
_scheduler = start_scheduler(db_module)
print("Reminder scheduler started.")
except Exception as e:
print(f"Failed to start reminder scheduler: {e}")
verifier = SignatureVerifier(SLACK_SIGNING_SECRET)
slack_client = WebClient(token=SLACK_BOT_TOKEN)
# --- Duplicate event prevention ---
# Used for in memory deduplication of Slack events to prevent double-processing on retries.
# For bigger scale, use a DB with a unique constraint on event_id.
# 1. Resets on restart — Slack may retry unprocessed events causing duplicates
# 2. Not thread safe — multiple threads could pass the duplicate check simultaneously
# 3. Fixed size is arbitrary
processed_events = OrderedDict()
MAX_EVENTS = 1000
def is_duplicate(event_id: str) -> bool:
if event_id in processed_events:
return True
processed_events[event_id] = True
if len(processed_events) > MAX_EVENTS:
processed_events.popitem(last=False)
return False
# --- Rate limiting ---
# Same caveats as processed_events — in-memory, resets on restart, not thread safe.
user_request_times = defaultdict(list)
MAX_REQUESTS = 5
WINDOW_SECONDS = 60
def is_rate_limited(user_id: str) -> bool:
now = time.time()
user_request_times[user_id] = [
t for t in user_request_times[user_id]
if now - t < WINDOW_SECONDS
]
if len(user_request_times[user_id]) >= MAX_REQUESTS:
return True
user_request_times[user_id].append(now)
return False
# --- Session management ---
# - Keyed by thread_ts since app_mention replies stay in the same thread.
# - In-memory — sessions are lost on restart, which is acceptable since abandoned tasks are never saved.
# - Sessions expire after 15 minutes of inactivity (so follow-ups are not lost between messages).
sessions = {}
MAX_SESSIONS = 500
SESSION_TIMEOUT = 15 * 60
meeting_followups = {}
materials_sessions = {}
class BookingWindow(BaseModel):
can_book: bool = False
start_iso: Optional[str] = None
end_iso: Optional[str] = None
def _extract_urls(text: str) -> list[str]:
urls = re.findall(r"(https?://[^\s>]+)", text)
# Strip trailing punctuation common in chat.
cleaned = []
for u in urls:
cleaned.append(u.rstrip(").,;!?>\"'"))
# de-dupe preserving order
return list(dict.fromkeys(cleaned))
def _extract_initial_email_pref(text: str) -> bool | None:
t = (text or "").lower()
opt_out_markers = [
"no initial email",
"don't send initial email",
"dont send initial email",
"no email",
"skip email",
"without email",
]
opt_in_markers = [
"send email",
"yes email",
"with email",
]
if any(m in t for m in opt_out_markers):
return False
if any(m in t for m in opt_in_markers):
return True
return None
def _send_initial_email_and_mark(task_id: int | None, recipients: list[str], subject: str, body: str) -> None:
"""Send initial email and mark DB only after successful send."""
send_email(to=recipients, subject=subject, body=body)
if task_id:
mark_initial_email_sent(task_id)
def _fixed_offset_for_timezone(timezone: str) -> str:
# Lightweight fallback for common local dev zones.
return "-04:00" if timezone == "America/New_York" else "+00:00"
def _parse_hour_minute(text_lower: str) -> tuple[int, int] | None:
"""Parse '9am', '9:30pm', or '14:00' from lowercase text."""
m = re.search(r"\b(\d{1,2})(?::(\d{2}))?\s*(am|pm)\b", text_lower)
if m:
h, mn = int(m.group(1)), int(m.group(2) or 0)
ap = m.group(3)
if ap == "pm" and h != 12:
h += 12
if ap == "am" and h == 12:
h = 0
if not (0 <= h <= 23 and 0 <= mn <= 59):
return None
return (h, mn)
m = re.search(r"\b([01]?\d|2[0-3]):([0-5]\d)\b", text_lower)
if m:
return (int(m.group(1)), int(m.group(2)))
return None
def _regex_weekday_and_time(text: str, timezone: str) -> dict | None:
"""
Handles replies like 'Monday 9AM' or 'Tuesday 14:30' (no 'HH:MM-HH:MM' range).
Picks the next occurrence of that weekday in the user's timezone (must be strictly in the future).
"""
day_map = {
"monday": 0,
"tuesday": 1,
"wednesday": 2,
"thursday": 3,
"friday": 4,
"saturday": 5,
"sunday": 6,
}
lower = text.lower()
target_weekday = None
for name, idx in day_map.items():
if re.search(r"\b" + re.escape(name) + r"\b", lower):
target_weekday = idx
break
if target_weekday is None:
return None
# Let the range-style regex handle "14:00-15:00"
if re.search(r"\d{1,2}:\d{2}\s*-\s*\d{1,2}:\d{2}", text):
return None
hm = _parse_hour_minute(lower)
if hm is None:
return None
h, minute = hm
tz = zoneinfo_or_utc(timezone)
now = datetime.now(tz)
today = now.date()
dow = today.weekday()
days_ahead = (target_weekday - dow) % 7
target_date = today + timedelta(days=days_ahead)
start_dt = datetime.combine(target_date, dtime(h, minute), tzinfo=tz)
if start_dt <= now:
start_dt += timedelta(days=7)
end_dt = start_dt + timedelta(hours=1)
return {
"can_book": True,
"start_iso": start_dt.isoformat(timespec="seconds"),
"end_iso": end_dt.isoformat(timespec="seconds"),
}
def _regex_extract_booking_window(text: str, timezone: str) -> dict | None:
# Example handled: "book Tuesday next week 14:00-15:00"
m = re.search(r"(\d{1,2}):(\d{2})\s*-\s*(\d{1,2}):(\d{2})", text)
if not m:
return None
now = datetime.now(zoneinfo_or_utc(timezone))
# Choose the next Tuesday when user says "Tuesday next week"; otherwise next day with that weekday if present.
day_map = {
"monday": 0,
"tuesday": 1,
"wednesday": 2,
"thursday": 3,
"friday": 4,
"saturday": 5,
"sunday": 6,
}
lower = text.lower()
target_weekday = None
for name, idx in day_map.items():
if name in lower:
target_weekday = idx
break
if target_weekday is None:
return None
days_ahead = (target_weekday - now.weekday()) % 7
if days_ahead == 0:
days_ahead = 7
if "next week" in lower:
days_ahead += 7
target_date = now.date() + timedelta(days=days_ahead)
sh, sm, eh, em = map(int, m.groups())
offset = _fixed_offset_for_timezone(timezone)
start_iso = f"{target_date.isoformat()}T{sh:02d}:{sm:02d}:00{offset}"
end_iso = f"{target_date.isoformat()}T{eh:02d}:{em:02d}:00{offset}"
return {"can_book": True, "start_iso": start_iso, "end_iso": end_iso}
def get_session(thread_ts: str) -> dict | None:
session = sessions.get(thread_ts)
if session is None:
return None
if time.time() - session.get("last_active", 0) > SESSION_TIMEOUT:
sessions.pop(thread_ts)
return None
return session
def _connect_url(state_id: str) -> str:
return f"{APP_EXTERNAL_URL.rstrip('/')}/connect/google?state_id={state_id}"
def _poll_email_for_state(state_id: str, timeout_sec: int = 600, poll_every_sec: int = 2) -> str | None:
deadline = time.time() + timeout_sec
while time.time() < deadline:
email = _PENDING_STATE_PROFILES.pop(state_id, None) or _db_read_pending_state(state_id)
if email:
return email
time.sleep(poll_every_sec)
return None
def _calendar_list(profile_email: str, max_results: int = 80) -> list[dict]:
creds = _ensure_fresh(_load_creds(profile_email))
service = build("calendar", "v3", credentials=creds)
now = datetime.now(timezone.utc).isoformat()
events_result = service.events().list(
calendarId="primary",
timeMin=now,
maxResults=int(max_results),
singleEvents=True,
orderBy="startTime",
).execute()
events = events_result.get("items", [])
return [
{
"summary": e.get("summary", "(no title)"),
"start": e["start"].get("dateTime", e["start"].get("date")),
"end": e["end"].get("dateTime", e["end"].get("date")),
}
for e in events
]
def _drive_search(profile_email: str, query: str, page_size: int = 10) -> list[dict]:
creds = _ensure_fresh(_load_creds(profile_email))
service = build("drive", "v3", credentials=creds)
query = (query or "").strip()
words = [w.lower() for w in query.replace("\n", " ").split(" ") if len(w.strip()) >= 3]
seen = []
for w in words:
w = w.strip(" ,.;:()[]{}\"")
if not w or w in seen:
continue
seen.append(w)
if len(seen) >= 6:
break
if not seen:
seen = [query[:40]] if query else ["meeting"]
clauses = [f"fullText contains '{w.replace(chr(39), chr(92)+chr(39))}'" for w in seen]
results = service.files().list(
q=" or ".join(clauses),
pageSize=10,
fields="files(id, name, mimeType, webViewLink)",
).execute()
return [
{"name": it.get("name"), "id": it.get("id"), "mimeType": it.get("mimeType"), "webViewLink": it.get("webViewLink")}
for it in results.get("files", [])
if it.get("id") and it.get("name")
]
def _calendar_update_description(profile_email: str, event_id: str, description: str) -> dict:
creds = _ensure_fresh(_load_creds(profile_email))
service = build("calendar", "v3", credentials=creds)
ev = service.events().patch(
calendarId="primary", eventId=event_id, body={"description": description or ""}
).execute()
return {"id": ev.get("id"), "htmlLink": ev.get("htmlLink")}
def _calendar_create(
profile_email: str,
summary: str,
description: str,
start_iso: str,
end_iso: str,
timezone_name: str,
) -> dict:
creds = _ensure_fresh(_load_creds(profile_email))
service = build("calendar", "v3", credentials=creds)
body = {
"summary": summary.strip() or "(no title)",
"description": description or "",
"start": {"dateTime": start_iso, "timeZone": timezone_name},
"end": {"dateTime": end_iso, "timeZone": timezone_name},
}
ev = service.events().insert(calendarId="primary", body=body).execute()
return {
"id": ev.get("id"),
"htmlLink": ev.get("htmlLink"),
"summary": ev.get("summary"),
"start": ev.get("start", {}).get("dateTime"),
"end": ev.get("end", {}).get("dateTime"),
}
def _iso_to_dt(iso_text: str) -> datetime:
# Support Google-style UTC timestamps ending with Z.
dt = datetime.fromisoformat(iso_text.replace("Z", "+00:00"))
# Normalize naive datetimes (e.g., all-day events) to UTC so comparisons are valid.
if dt.tzinfo is None:
return dt.replace(tzinfo=datetime.now().astimezone().tzinfo)
return dt
def _format_conflicts(conflicts: list[dict]) -> str:
if not conflicts:
return ""
lines = []
for c in conflicts[:8]:
try:
start = _iso_to_dt(c["start"]).strftime("%-I:%M %p")
end = _iso_to_dt(c["end"]).strftime("%-I:%M %p, %b %-d")
except Exception:
start, end = c["start"], c["end"]
lines.append(f"- {c['summary']} ({start}–{end})")
return "\n".join(lines)
def _find_conflicts(emails: list[str], start_iso: str, end_iso: str) -> list[dict]:
start_dt = _iso_to_dt(start_iso)
end_dt = _iso_to_dt(end_iso)
conflicts = []
for email in emails:
events = _calendar_list(email)
for ev in events:
ev_start = ev.get("start")
ev_end = ev.get("end")
if not ev_start or not ev_end:
continue
ev_start_dt = _iso_to_dt(ev_start)
ev_end_dt = _iso_to_dt(ev_end)
# overlap iff start < other_end and other_start < end
if start_dt < ev_end_dt and ev_start_dt < end_dt:
conflicts.append(
{
"email": email,
"summary": ev.get("summary", "(busy)"),
"start": ev_start,
"end": ev_end,
}
)
return conflicts
def _extract_suggested_slots(raw: str, timezone: str) -> list[dict]:
tz = zoneinfo_or_utc(timezone)
now = datetime.now(tz)
window_end = now + timedelta(days=7)
slots = []
seen = set()
pattern = re.compile(
r"(?:(Monday|Tuesday|Wednesday|Thursday|Friday|Saturday|Sunday),?\s+)?"
r"(\d{4}-\d{2}-\d{2})\s+(\d{1,2}):(\d{2})\s*-\s*(\d{1,2}):(\d{2})",
re.IGNORECASE,
)
for m in pattern.finditer(raw or ""):
date_text = m.group(2)
sh, sm = int(m.group(3)), int(m.group(4))
eh, em = int(m.group(5)), int(m.group(6))
try:
y, mo, d = [int(x) for x in date_text.split("-")]
start = datetime(y, mo, d, sh, sm, tzinfo=tz)
end = datetime(y, mo, d, eh, em, tzinfo=tz)
if end <= start:
end = start + timedelta(hours=1)
except Exception:
continue
# Enforce the same policy we asked for: next 7 days, Mon-Fri, 09:00-17:00.
if start < now or start > window_end:
continue
if start.weekday() >= 5: # 5=Sat, 6=Sun
continue
if start.hour < 9 or (end.hour > 17 or (end.hour == 17 and end.minute > 0)):
continue
key = (start.isoformat(), end.isoformat())
if key in seen:
continue
seen.add(key)
slots.append(
{
"start_iso": start.isoformat(timespec="seconds"),
"end_iso": end.isoformat(timespec="seconds"),
"weekday": start.strftime("%A").lower(),
"label": f"{start.strftime('%A, %Y-%m-%d %H:%M')}-{end.strftime('%H:%M')} ({timezone})",
}
)
if len(slots) >= 5:
break
return slots
def _format_slot_options(slots: list[dict], timezone: str) -> str:
if not slots:
return "I couldn't format time suggestions right now. Please share a specific date/time."
lines = ["*Here are some available time slots:*"]
for i, s in enumerate(slots, start=1):
lines.append(f"{i}. {s['label']}")
lines.append("")
lines.append("Reply `pick 1`–`pick 5` (or just the number) to book a slot.")
return "\n".join(lines)
def _booking_from_suggested_choice(text: str, slots: list[dict]) -> dict | None:
if not slots:
return None
lower = (text or "").lower()
nums = [int(n) for n in re.findall(r"\b([1-9]\d*)\b", lower)]
if nums:
idx = nums[0] - 1
if 0 <= idx < min(len(slots), 5):
return {"can_book": True, "start_iso": slots[idx]["start_iso"], "end_iso": slots[idx]["end_iso"]}
for day in ["monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"]:
if day in lower:
for s in slots:
if s.get("weekday") == day:
return {"can_book": True, "start_iso": s["start_iso"], "end_iso": s["end_iso"]}
return None
def _wants_meeting_slot_regeneration(text: str) -> bool:
"""
True when the user is asking for a fresh list of common meeting times.
Uses word-boundary style checks so we do not mis-trigger on words like
"sometimes" (contains "time") or "unavailable" (contains "available").
"""
t = (text or "").lower()
if re.search(r"\b(re-?suggest|resuggest)\b", t):
return True
if re.search(r"\bsuggest(s|ing|ed|ions?)?\b", t):
return True
if re.search(r"\b(again|another|more|new|fresh|other)\b", t) and re.search(
r"\b(slots?|times?|options?|availability)\b", t
):
return True
if re.search(r"\bfree\b", t) and re.search(r"\b(slots?|times?)\b", t):
return True
if re.search(r"\bavailable\b", t) and re.search(r"\b(slots?|times?)\b", t):
return True
if re.search(r"\btime\s+slots?\b", t):
return True
if re.search(r"\bcommon\s+(times?|slots?)\b", t):
return True
return False
def _send_common_time_suggestions(channel: str, thread_ts: str, emails: list[str], timezone: str) -> list[dict]:
try:
events_by_email = {email: _calendar_list(email) for email in emails}
prompt = f"""
You are a scheduling assistant.
Find common meeting times for the next 7 days within business hours:
- Mon-Fri
- 09:00-17:00
Timezone: {timezone}
Busy events per attendee:
{json.dumps(events_by_email, indent=2)}
Return only 5 lines in this exact format:
Weekday, YYYY-MM-DD HH:MM-HH:MM ({timezone})
Do not include explanations, availability dumps, or extra text.
"""
suggestions = call_gemini(prompt)
if isinstance(suggestions, dict):
suggestions = ""
elif not isinstance(suggestions, str):
suggestions = str(suggestions or "")
if not suggestions.strip():
hint = take_last_gemini_user_hint()
send_slack_message_with_fallback(
channel,
hint
or (
"I couldn't generate time suggestions just now. "
"Please try again in a few minutes, or say a specific day/time to book."
),
thread_ts=thread_ts,
)
return []
slots = _extract_suggested_slots(suggestions, timezone)
if not slots:
send_slack_message_with_fallback(
channel,
"I couldn't parse suggested slots cleanly. Please share a specific date/time (e.g. Friday 2pm).",
thread_ts=thread_ts,
)
return []
send_slack_message_with_fallback(
channel,
_format_slot_options(slots, timezone),
thread_ts=thread_ts,
)
return slots
except Exception as e:
print(f"Error generating common time suggestions: {e}")
send_slack_message_with_fallback(
channel,
"I hit an error while fetching calendars or generating suggestions. "
"Please try again in a moment, or say a specific day/time to book.",
thread_ts=thread_ts,
)
return []
def _drive_query_from_task(title: str, description: str) -> str:
bits = []
if title:
bits.append(title)
if description:
bits.append(description)
q = " ".join(bits).strip()
return q[:180] if len(q) > 180 else q
def _render_drive_results(items: list[dict]) -> str:
if not items:
return "No matching files found."
lines = []
for i, it in enumerate(items[:5], start=1):
name = it.get("name") or "(unnamed)"
link = it.get("webViewLink") or (f"https://drive.google.com/open?id={it.get('id')}" if it.get("id") else "")
lines.append(f"{i}. {name}" + (f"\n {link}" if link else ""))
return "\n".join(lines)
def _build_materials_description(base_description: str, pasted_links: list[str], selected_drive_items: list[dict]) -> str:
base = (base_description or "").strip()
lines = [base] if base else []
materials = []
for u in pasted_links:
materials.append(f"- {u}")
for it in selected_drive_items:
name = it.get("name") or "(file)"
link = it.get("webViewLink") or (f"https://drive.google.com/open?id={it.get('id')}" if it.get("id") else "")
if link:
materials.append(f"- {name}: {link}")
else:
materials.append(f"- {name}")
if materials:
lines.append("")
lines.append("Materials:")
lines.extend(materials)
return "\n".join(lines).strip()
def _append_materials_bullets(existing_description: str, bullets: list[str]) -> str:
"""
Append bullets under an existing 'Materials:' section if present.
Otherwise create a new Materials section at the end.
"""
desc = (existing_description or "").rstrip()
new_lines = [f"- {b}" if not b.strip().startswith("-") else b for b in bullets if b.strip()]
if not new_lines:
return desc
marker = "\nMaterials:\n"
if marker in f"\n{desc}\n":
# Append at end (Materials section is already last in our formatting).
return (desc + "\n" + "\n".join(new_lines)).strip()
parts = [desc] if desc else []
parts.append("")
parts.append("Materials:")
parts.extend(new_lines)
return "\n".join(parts).strip()
def _snap_booking_if_llm_past(booking: dict, timezone: str) -> dict:
"""If Gemini used a past year, roll start/end forward until start is in the future."""
tz = zoneinfo_or_utc(timezone)
now = datetime.now(tz)
try:
start = _iso_to_dt(booking["start_iso"])
end = _iso_to_dt(booking["end_iso"])
except Exception:
return booking
if start.tzinfo is None:
start = start.replace(tzinfo=tz)
if end.tzinfo is None:
end = end.replace(tzinfo=tz)
if start >= now - timedelta(minutes=1):
return booking
duration = end - start
if duration.total_seconds() <= 0:
duration = timedelta(hours=1)
for _ in range(6):
try:
start = start.replace(year=start.year + 1)
except ValueError:
start = start.replace(year=start.year + 1, month=2, day=28)
end = start + duration
if start >= now - timedelta(minutes=1):
break
booking["start_iso"] = start.isoformat(timespec="seconds")
booking["end_iso"] = end.isoformat(timespec="seconds")
return booking
def _extract_booking_window(text: str, timezone: str, *, allow_llm: bool = True) -> dict | None:
wd = _regex_weekday_and_time(text, timezone)
if wd:
return wd
regex_booking = _regex_extract_booking_window(text, timezone)
if regex_booking:
return regex_booking
if not allow_llm:
return None
now = datetime.now(zoneinfo_or_utc(timezone)).replace(microsecond=0)
prompt = f"""
You are extracting a meeting time from user text.
Timezone: {timezone}
Current date and time in that timezone (use this year/month — do not use 2024 unless the user explicitly says 2024): {now.isoformat()}
Text: {text}
Return ONLY compact JSON with this schema:
{{
"can_book": true/false,
"start_iso": "YYYY-MM-DDTHH:MM:SS±HH:MM",
"end_iso": "YYYY-MM-DDTHH:MM:SS±HH:MM"
}}
start_iso and end_iso must be on or after {now.isoformat()}. If the user picks a weekday and time (e.g. Monday 9am), use the next occurrence of that weekday on or after today.
If no clear time range exists, return can_book=false and empty strings.
"""
data = call_gemini(prompt, schema=BookingWindow)
if not data or not isinstance(data, dict):
return None
if not data.get("can_book"):
return None
if not data.get("start_iso") or not data.get("end_iso"):
return None
return _snap_booking_if_llm_past(data, timezone)
def handle_event(text: str, user_id: str, channel: str, thread_ts: str, timezone: str) -> None:
try:
normalized_text = re.sub(r"[^a-z0-9\s]", " ", text.lower())
normalized_text = re.sub(r"\s+", " ", normalized_text).strip()
if (
"my tasks" in normalized_text
or "what are my tasks" in normalized_text
or "what am i assigned to" in normalized_text
):
tasks = get_tasks_for_owner(f"<@{user_id}>", limit=10)
if not tasks:
send_slack_message_with_fallback(
channel,
"I couldn't find any tasks assigned to you yet.",
thread_ts=thread_ts,
)
return
lines = [f"Here are your tasks ({min(len(tasks), 5)} of {len(tasks)}):"]
for index, task in enumerate(tasks[:5], start=1):
title = task.get("title") or "(no title)"
deadline_text = task.get("deadline")
deadline = "no deadline"
if deadline_text:
try:
deadline = datetime.fromisoformat(deadline_text).strftime("%b %d, %Y %I:%M %p")
except Exception:
deadline = deadline_text
status = task.get("status") or "pending"
urgency = task.get("urgency") or "n/a"
lines.append(f"{index}. {title} | due: {deadline} | status: {status} | urgency: {urgency}")
if len(tasks) > 5:
lines.append(f"...and {len(tasks) - 5} more.")
send_slack_message_with_fallback(channel, "\n".join(lines), thread_ts=thread_ts)
return
if (
"task log" in normalized_text
or "overview" in normalized_text
or "all tasks" in normalized_text
or "show tasks" in normalized_text
):
all_tasks = get_all_tasks()
if not all_tasks:
send_slack_message_with_fallback(
channel,
"No tasks found in the database.",
thread_ts=thread_ts,
)
return
pending = [t for t in all_tasks if (t.get("status") or "pending") != "completed"]
completed = [t for t in all_tasks if (t.get("status") or "pending") == "completed"]
todos = [t for t in all_tasks if t.get("task") == "todo"]
meetings = [t for t in all_tasks if t.get("task") == "meeting"]
lines = [
f"*Task Overview* — {len(all_tasks)} total | {len(pending)} open | {len(completed)} completed | {len(todos)} todos | {len(meetings)} meetings",
"",
]
if pending:
lines.append("*Open Tasks:*")
for index, task in enumerate(pending[:15], start=1):
title = task.get("title") or "(no title)"
task_type = task.get("task") or "?"
deadline_text = task.get("deadline")
deadline = "no deadline"
if deadline_text:
try:
deadline = datetime.fromisoformat(deadline_text).strftime("%b %d %I:%M %p")
except Exception:
deadline = deadline_text
urgency = task.get("urgency") or "n/a"
owners = ", ".join(task.get("owners") or []) or "unassigned"
lines.append(f"{index}. [{task_type}] {title} | due: {deadline} | urgency: {urgency} | owners: {owners}")
if len(pending) > 15:
lines.append(f"...and {len(pending) - 15} more open tasks.")
if completed:
lines.append(f"\n_{len(completed)} completed task(s) not shown. Say \"clear tasks\" to wipe or \"mark all complete\" to close everything._")
send_slack_message_with_fallback(channel, "\n".join(lines), thread_ts=thread_ts)
return
if (
"clear tasks" in normalized_text
or "wipe tasks" in normalized_text
or "delete all tasks" in normalized_text
or "clear the db" in normalized_text
or "wipe the db" in normalized_text
):
count = clear_all_tasks()
send_slack_message_with_fallback(
channel,
f"Done. Permanently deleted {count} task(s) from the database.",
thread_ts=thread_ts,
)
return
if (
"mark all complete" in normalized_text
or "mark all tasks complete" in normalized_text
or "complete all tasks" in normalized_text
or "close all tasks" in normalized_text
):
count = mark_all_tasks_complete()
send_slack_message_with_fallback(
channel,
f"Done. Marked {count} task(s) as completed.",
thread_ts=thread_ts,
)
return
materials = materials_sessions.get(thread_ts)
if materials:
phase = materials.get("phase")
lower = text.strip().lower()
if phase == "awaiting_materials":
urls = _extract_urls(text)
if lower in {"suggest", "drive", "files", "file"}:
materials["pasted_links"] = []
materials["drive_suggest_attempted"] = True
q = _drive_query_from_task(materials.get("title") or "", materials.get("base_description") or "")
organizer = materials.get("organizer_email")
try:
items = _drive_search(organizer, q)
except Exception as e:
print(f"Drive search error: {e}")
items = []
materials["drive_items"] = items[:5]
if not materials["drive_items"]:
materials["phase"] = "awaiting_materials"
send_slack_message_with_fallback(
channel,
"No relevant files found in Drive. Paste link(s) to include, or reply `none` to skip.",
thread_ts=thread_ts,
)
return
materials["phase"] = "awaiting_drive_selection"
send_slack_message_with_fallback(
channel,
"*Here are some relevant files from Drive:*\n"
f"{_render_drive_results(materials['drive_items'])}\n\n"
"Reply with the numbers to include (e.g. `1,3`) or `none` to skip.",
thread_ts=thread_ts,
)
return
if lower in {"no", "none", "nope", "nah"}:
materials["pasted_links"] = []