-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
618 lines (542 loc) · 20.5 KB
/
Copy pathapp.py
File metadata and controls
618 lines (542 loc) · 20.5 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
"""
Sidekick AI — FastAPI application entry point.
"""
from fastapi.staticfiles import StaticFiles
from fastapi.responses import FileResponse, HTMLResponse
import os
import logging
from contextlib import asynccontextmanager
import traceback
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
# Prevent oauthlib from failing on resolved alias scope changes
os.environ['OAUTHLIB_RELAX_TOKEN_SCOPE'] = 'True'
from config import settings
from database import Base, engine
# Routes
from routes.auth import router as auth_router
from routes.gmail import router as gmail_router
from routes.summary import router as summary_router
from routes.reply import router as reply_router
from routes.priority import router as priority_router
from routes.memory import router as memory_router
from routes.planner import router as planner_router
from routes.calendar import router as calendar_router
from routes.twitter import router as twitter_router
from routes.settings import router as settings_router
from routes.telegram import router as telegram_router
from routes.slack import router as slack_router
from routes.ai import router as ai_router
# ---------------------------------------------------------------------------
# Logging
# ---------------------------------------------------------------------------
logging.basicConfig(
level=logging.DEBUG if settings.DEBUG else logging.INFO,
format="%(asctime)s | %(levelname)-8s | %(name)s | %(message)s",
)
logger = logging.getLogger(__name__)
# ---------------------------------------------------------------------------
# Lifespan
# ---------------------------------------------------------------------------
@asynccontextmanager
async def lifespan(app: FastAPI):
logger.info("Starting Sidekick AI v%s …", settings.VERSION)
try:
# Create database tables if missing
try:
Base.metadata.create_all(bind=engine)
logger.info("Database schema initialized successfully.")
except Exception as db_exc:
logger.warning("Database schema creation warning (non-fatal): %s", db_exc)
# Run Alembic migrations automatically on startup
try:
from alembic.config import Config
from alembic import command
alembic_cfg = Config("alembic.ini")
command.upgrade(alembic_cfg, "head")
logger.info("Alembic database migrations applied successfully on startup.")
except Exception as exc:
logger.warning("Alembic startup migration warning (non-fatal): %s", exc)
# Start background poller task
import asyncio
polling_task = None
try:
from services.poller import start_polling
polling_task = asyncio.create_task(start_polling())
except Exception as poll_exc:
logger.warning("Background poller failed to start (non-fatal): %s", poll_exc)
# Start persistent Telegram client tasks
try:
from services.telegram_manager import telegram_manager
asyncio.create_task(telegram_manager.start_all_clients())
except Exception as tg_exc:
logger.warning("Telegram manager failed to start (non-fatal): %s", tg_exc)
logger.info("Sidekick AI backend is ready.")
except Exception as e:
print("FATAL STARTUP ERROR:")
traceback.print_exc()
logger.fatal("FATAL STARTUP ERROR: %s", e, exc_info=True)
raise
yield
# Shutdown background poller
if polling_task is not None:
polling_task.cancel()
try:
await polling_task
except asyncio.CancelledError:
logger.info("Background poller task stopped.")
# Stop persistent Telegram client connections
for user_id in list(telegram_manager._clients.keys()):
await telegram_manager.stop_client(user_id)
logger.info("Persistent Telegram clients stopped.")
# Shutdown — close the LLM async client
from services.llm import llm
await llm.close()
logger.info("Sidekick AI backend shut down.")
# ---------------------------------------------------------------------------
# Application
# ---------------------------------------------------------------------------
app = FastAPI(
title=settings.PROJECT_NAME,
version=settings.VERSION,
description=settings.DESCRIPTION,
lifespan=lifespan,
docs_url="/docs",
redoc_url="/redoc",
)
# ---------------------------------------------------------------------------
# CORS Middleware (Added before any routers are included)
# ---------------------------------------------------------------------------
origins = ["*"]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_origin_regex=".*",
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# ---------------------------------------------------------------------------
# Caching Control Middleware
# ---------------------------------------------------------------------------
@app.middleware("http")
async def add_cache_control_headers(request, call_next):
response = await call_next(request)
path = request.url.path
# Prevent aggressive browser caching of frontend static pages and bundles
if (
path.startswith("/_next")
or path.startswith("/static")
or path in ("/", "/privacy", "/terms")
or path.endswith(".html")
or path.endswith(".js")
or path.endswith(".css")
):
response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
response.headers["Pragma"] = "no-cache"
response.headers["Expires"] = "0"
return response
# ---------------------------------------------------------------------------
# Routes
# ---------------------------------------------------------------------------
API_PREFIX = "/api/v1"
app.include_router(auth_router, prefix=API_PREFIX)
# Fallback alias mounts for Auth endpoints (/api/auth/login and /auth/login)
app.include_router(auth_router, prefix="/api")
app.include_router(auth_router, prefix="")
app.include_router(gmail_router, prefix=API_PREFIX)
app.include_router(summary_router, prefix=API_PREFIX)
app.include_router(reply_router, prefix=API_PREFIX)
app.include_router(priority_router, prefix=API_PREFIX)
app.include_router(memory_router, prefix=API_PREFIX)
app.include_router(planner_router, prefix=API_PREFIX)
app.include_router(calendar_router, prefix=API_PREFIX)
app.include_router(twitter_router, prefix=API_PREFIX)
app.include_router(settings_router, prefix=API_PREFIX)
app.include_router(telegram_router, prefix=API_PREFIX)
app.include_router(slack_router, prefix=API_PREFIX)
# Fallback alias mounts for Slack OAuth callback URLs
app.include_router(slack_router, prefix="/api")
app.include_router(slack_router, prefix="")
app.include_router(ai_router, prefix=API_PREFIX)
# ---------------------------------------------------------------------------
# Root / Frontend UI + Health
# ---------------------------------------------------------------------------
# Mount the frontend directory to serve static assets (CSS, JS, images, etc.)
app.mount("/static", StaticFiles(directory="frontend"), name="static")
# Serve Next.js static build assets at /_next to support static frontend routes
next_dir = os.path.join("frontend", "out", "_next")
if not os.path.exists(next_dir):
os.makedirs(next_dir, exist_ok=True)
app.mount("/_next", StaticFiles(directory=next_dir), name="next")
@app.get("/logo.png", tags=["Static"])
async def serve_logo():
"""Serve the canonical logo.png file."""
paths = [
os.path.join("frontend", "out", "logo.png"),
os.path.join("frontend", "public", "logo.png"),
]
for p in paths:
if os.path.exists(p):
return FileResponse(p)
return HTMLResponse(status_code=404, content="Logo not found")
@app.get("/manifest.json", tags=["Static"])
async def serve_manifest():
"""Serve the manifest.json file."""
paths = [
os.path.join("frontend", "out", "manifest.json"),
os.path.join("frontend", "public", "manifest.json"),
]
for p in paths:
if os.path.exists(p):
return FileResponse(p)
return HTMLResponse(status_code=404, content="Manifest not found")
@app.get("/favicon.ico", tags=["Static"])
async def serve_favicon():
"""Serve the favicon.ico file."""
paths = [
os.path.join("frontend", "out", "favicon.ico"),
os.path.join("frontend", "public", "favicon.ico"),
os.path.join("frontend", "src", "app", "favicon.ico"),
]
for p in paths:
if os.path.exists(p):
return FileResponse(p)
return HTMLResponse(status_code=404, content="Favicon not found")
@app.get("/sw.js", tags=["Static"])
async def serve_sw():
"""Serve the service worker sw.js file."""
paths = [
os.path.join("frontend", "out", "sw.js"),
os.path.join("frontend", "public", "sw.js"),
]
for p in paths:
if os.path.exists(p):
return FileResponse(p, media_type="application/javascript")
return HTMLResponse(status_code=404, content="Service worker not found")
@app.get("/", tags=["Root"])
async def serve_frontend():
"""Serve the frontend user interface index.html file at root URL."""
paths = [
os.path.join("frontend", "out", "index.html"),
os.path.join("frontend", "index.html"),
]
for p in paths:
if os.path.exists(p):
return FileResponse(p)
return {
"success": True,
"project": settings.PROJECT_NAME,
"version": settings.VERSION,
"status": "running",
"docs": "/docs",
}
# ---------------------------------------------------------------------------
# Legal / Static route fallback content generators
# ---------------------------------------------------------------------------
def markdown_to_html(md_path: str) -> tuple[str, str, str]:
"""Parses a simple markdown file and returns (title, effective_date, content_html)."""
import re
if not os.path.exists(md_path):
return "", "", ""
with open(md_path, "r", encoding="utf-8") as f:
content = f.read()
lines = content.splitlines()
title = ""
effective_date = ""
html_parts = []
in_list = False
in_sub_list = False
bold_pat = re.compile(r'\*\*(.*?)\*\*')
code_pat = re.compile(r'`(.*?)`')
link_pat = re.compile(r'\[(.*?)\]\((.*?)\)')
def clean_inline(text: str) -> str:
text = bold_pat.sub(r'<strong>\1</strong>', text)
text = code_pat.sub(r'<code>\1</code>', text)
text = link_pat.sub(r'<a href="\2">\1</a>', text)
return text
for line in lines:
stripped = line.strip()
if line.startswith("# ") and not title:
title = line[2:].strip()
continue
if "Effective Date:" in line or "effective date" in line.lower():
date_match = re.search(r'\*\*Effective Date:\*\*\s*(.*)', line, re.IGNORECASE)
if date_match:
effective_date = date_match.group(1).strip()
continue
if not stripped:
continue
if stripped == "---":
if in_sub_list:
html_parts.append(" </ul>")
in_sub_list = False
if in_list:
html_parts.append("</ul>")
in_list = False
html_parts.append("<hr />")
continue
if stripped.startswith("## "):
if in_sub_list:
html_parts.append(" </ul>")
in_sub_list = False
if in_list:
html_parts.append("</ul>")
in_list = False
html_parts.append(f"<h2>{clean_inline(stripped[3:])}</h2>")
continue
if stripped.startswith("### "):
if in_sub_list:
html_parts.append(" </ul>")
in_sub_list = False
if in_list:
html_parts.append("</ul>")
in_list = False
html_parts.append(f"<h3>{clean_inline(stripped[4:])}</h3>")
continue
if stripped.startswith("* ") or stripped.startswith("- "):
indent = len(line) - len(line.lstrip())
item_text = clean_inline(stripped[2:])
if indent >= 2:
if not in_sub_list:
html_parts.append(" <ul>")
in_sub_list = True
html_parts.append(f" <li>{item_text}</li>")
else:
if in_sub_list:
html_parts.append(" </ul>")
in_sub_list = False
if not in_list:
html_parts.append("<ul>")
in_list = True
html_parts.append(f" <li>{item_text}</li>")
continue
if in_sub_list:
html_parts.append(" </ul>")
in_sub_list = False
if in_list:
html_parts.append("</ul>")
in_list = False
html_parts.append(f"<p>{clean_inline(stripped)}</p>")
if in_sub_list:
html_parts.append(" </ul>")
if in_list:
html_parts.append("</ul>")
return title, effective_date, "\n".join(html_parts)
# ---------------------------------------------------------------------------
# Legal / Static route fallback content generators
# ---------------------------------------------------------------------------
def get_legal_page_html(title: str, effective_date: str, content_html: str) -> str:
return f"""<!DOCTYPE html>
<html lang="en" class="dark">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{title} - SidekickAI</title>
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap" rel="stylesheet">
<style>
:root {{
--bg-color: #09090b;
--card-bg: #18181b;
--border-color: #27272a;
--text-color: #e4e4e7;
--text-muted: #a1a1aa;
--accent-color: #6366f1;
}}
body {{
background-color: var(--bg-color);
color: var(--text-color);
font-family: 'Inter', sans-serif;
margin: 0;
padding: 0;
min-height: 100vh;
display: flex;
flex-direction: column;
}}
.header {{
border-bottom: 1px solid var(--border-color);
background-color: rgba(9, 9, 11, 0.7);
backdrop-filter: blur(12px);
position: sticky;
top: 0;
z-index: 50;
padding: 1rem 2rem;
display: flex;
justify-content: space-between;
align-items: center;
}}
.logo-container {{
display: flex;
align-items: center;
gap: 0.5rem;
text-decoration: none;
color: #fff;
font-weight: 600;
font-size: 1.25rem;
}}
.back-link {{
color: var(--text-muted);
text-decoration: none;
font-size: 0.875rem;
font-weight: 500;
transition: color 0.2s;
border: 1px solid var(--border-color);
padding: 0.5rem 1rem;
border-radius: 0.5rem;
background-color: var(--card-bg);
}}
.back-link:hover {{
color: #fff;
border-color: #52525b;
}}
.container {{
max-width: 800px;
margin: 3rem auto;
padding: 0 1.5rem;
flex: 1;
}}
.card {{
background-color: rgba(24, 24, 27, 0.4);
border: 1px solid var(--border-color);
border-radius: 0.75rem;
padding: 2.5rem;
box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
backdrop-filter: blur(8px);
}}
h1 {{
font-size: 2rem;
font-weight: 700;
margin-top: 0;
margin-bottom: 0.5rem;
color: #fff;
letter-spacing: -0.025em;
}}
.effective-date {{
color: var(--text-muted);
font-size: 0.875rem;
margin-bottom: 2rem;
border-bottom: 1px solid var(--border-color);
padding-bottom: 1rem;
}}
h2 {{
font-size: 1.25rem;
font-weight: 600;
margin-top: 2rem;
margin-bottom: 0.75rem;
color: #fff;
}}
p {{
line-height: 1.625;
color: var(--text-color);
margin-top: 0;
margin-bottom: 1.25rem;
}}
ul {{
padding-left: 1.5rem;
margin-bottom: 1.5rem;
}}
li {{
margin-bottom: 0.5rem;
line-height: 1.625;
}}
.footer {{
border-top: 1px solid var(--border-color);
padding: 2rem;
text-align: center;
color: var(--text-muted);
font-size: 0.875rem;
}}
.footer a {{
color: var(--text-muted);
text-decoration: none;
margin: 0 0.75rem;
transition: color 0.2s;
}}
.footer a:hover {{
color: #fff;
}}
</style>
</head>
<body>
<header class="header">
<a href="/" class="logo-container">
<img src="/logo.png" alt="SidekickAI Logo" style="width:24px; height:24px; object-fit:contain; border-radius:4px;" />
<span>Sidekick<span style="color:#a1a1aa; font-weight:400;">AI</span></span>
</a>
<a href="/" class="back-link">Back to App</a>
</header>
<main class="container">
<article class="card">
<h1>{title}</h1>
<div class="effective-date">Effective Date: {effective_date}</div>
{content_html}
</article>
</main>
<footer class="footer">
<p>© 2026 SidekickAI. All rights reserved.</p>
<p>
<a href="/privacy">Privacy Policy</a>
<a href="/terms">Terms of Service</a>
</p>
</footer>
</body>
</html>"""
@app.get("/privacy", tags=["Static"])
async def serve_privacy():
"""Serve the Privacy Policy page."""
# Attempt to serve static exported file from Next.js build
paths = [
os.path.join("frontend", "out", "privacy.html"),
os.path.join("frontend", "out", "privacy", "index.html"),
os.path.join("frontend", "privacy.html"),
os.path.join("frontend", "privacy", "index.html"),
]
for p in paths:
if os.path.exists(p):
return FileResponse(p)
# Fallback dynamic rendering of PRIVACY.md
title, effective_date, content_html = markdown_to_html("PRIVACY.md")
if not title:
title = "Privacy Policy"
effective_date = "August 3, 2026"
content_html = "<p>Privacy Policy could not be loaded from PRIVACY.md.</p>"
return HTMLResponse(content=get_legal_page_html(title, effective_date, content_html))
@app.get("/terms", tags=["Static"])
async def serve_terms():
"""Serve the Terms of Service page."""
# Attempt to serve static exported file from Next.js build
paths = [
os.path.join("frontend", "out", "terms.html"),
os.path.join("frontend", "out", "terms", "index.html"),
os.path.join("frontend", "terms.html"),
os.path.join("frontend", "terms", "index.html"),
]
for p in paths:
if os.path.exists(p):
return FileResponse(p)
# Fallback dynamic rendering of TERMS.md
title, effective_date, content_html = markdown_to_html("TERMS.md")
if not title:
title = "Terms of Service"
effective_date = "August 3, 2026"
content_html = "<p>Terms of Service could not be loaded from TERMS.md.</p>"
return HTMLResponse(content=get_legal_page_html(title, effective_date, content_html))
@app.get("/health", tags=["Health"])
async def health():
return {
"status": "healthy",
"version": settings.VERSION,
"database": "connected",
}
# ---------------------------------------------------------------------------
# Dev entrypoint
# ---------------------------------------------------------------------------
if __name__ == "__main__":
import uvicorn
uvicorn.run(
"app:app",
host=settings.HOST,
port=settings.PORT,
reload=settings.DEBUG,
log_level="debug" if settings.DEBUG else "info",
)