feat: chatbot

This commit is contained in:
OpenSquared
2026-07-14 17:31:34 +02:00
parent 2dc4d8e91b
commit c4de6957ea
9 changed files with 639 additions and 0 deletions

View File

@@ -135,6 +135,14 @@ def init_db():
price_at_signal REAL
)""",
"ALTER TABLE cycle_runs ADD COLUMN wavelet_signals_count INTEGER DEFAULT 0",
# AI Chat widget — persisted conversation turns, one growing thread per session_id
"""CREATE TABLE IF NOT EXISTS ai_chat_messages (
id INTEGER PRIMARY KEY AUTOINCREMENT,
session_id TEXT NOT NULL,
role TEXT NOT NULL,
content TEXT NOT NULL,
created_at TEXT DEFAULT (datetime('now'))
)""",
]:
try:
c.execute(_sql)
@@ -146,6 +154,11 @@ def init_db():
except Exception:
pass
try:
c.execute("CREATE INDEX IF NOT EXISTS idx_chat_session_date ON ai_chat_messages(session_id, created_at)")
except Exception:
pass
# Specialist Reports — surprise index + text sentiment columns
try:
c.execute("ALTER TABLE specialist_reports ADD COLUMN consensus_estimate REAL")
@@ -3213,6 +3226,35 @@ def get_wavelet_signals_history(ticker: str, days: int = 30) -> List[Dict]:
return [dict(r) for r in rows]
# ── AI Chat widget — persisted conversation ────────────────────────────────────
def save_chat_message(session_id: str, role: str, content: str) -> None:
conn = get_conn()
conn.execute(
"INSERT INTO ai_chat_messages (session_id, role, content) VALUES (?, ?, ?)",
(session_id, role, content),
)
conn.commit()
conn.close()
def get_chat_messages(session_id: str, limit: int = 100) -> List[Dict]:
conn = get_conn()
rows = conn.execute(
"SELECT role, content, created_at FROM ai_chat_messages WHERE session_id=? ORDER BY id ASC LIMIT ?",
(session_id, limit),
).fetchall()
conn.close()
return [dict(r) for r in rows]
def clear_chat_session(session_id: str) -> None:
conn = get_conn()
conn.execute("DELETE FROM ai_chat_messages WHERE session_id=?", (session_id,))
conn.commit()
conn.close()
# ── System Logs ───────────────────────────────────────────────────────────────
def log_system_event(