feat: gate post generation on training flag

This commit is contained in:
admin committed 2026-10-09 20:11:41 +02:00
1 parent e847a4b390
commit a43ededa1b
5 files changed
+132 -6

No files matched your search

+31
View File
@@ -28,6 +28,7 @@ def init_db(db_path: str | None = None) -> None:
CREATE TABLE IF NOT EXISTS users (
telegram_user_id INTEGER PRIMARY KEY,
tone_of_voice TEXT NOT NULL DEFAULT '',
is_trained INTEGER NOT NULL DEFAULT 0,
created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP
)
@@ -94,6 +95,19 @@ def init_db(db_path: str | None = None) -> None:
)
"""
)
user_columns = {
row["name"] for row in connection.execute("PRAGMA table_info(users)").fetchall()
}
if "is_trained" not in user_columns:
connection.execute("ALTER TABLE users ADD COLUMN is_trained INTEGER NOT NULL DEFAULT 0")
connection.execute(
"""
UPDATE users SET is_trained = 1
WHERE TRIM(tone_of_voice) != ''
OR EXISTS (SELECT 1 FROM user_post_samples p WHERE p.telegram_user_id = users.telegram_user_id)
OR EXISTS (SELECT 1 FROM user_training_notes n WHERE n.telegram_user_id = users.telegram_user_id)
"""
)
api_log_columns = {
row["name"] for row in connection.execute("PRAGMA table_info(api_call_logs)").fetchall()
}
@@ -218,6 +232,11 @@ def add_post_samples(user_id: int, posts: list[str], db_path: str | None = None)
samples = [post.strip() for post in posts if post and post.strip()]
with closing(connect(db_path)) as connection, connection:
connection.execute("INSERT OR IGNORE INTO users (telegram_user_id) VALUES (?)", (user_id,))
if samples:
connection.execute(
"UPDATE users SET is_trained = 1, updated_at = CURRENT_TIMESTAMP WHERE telegram_user_id = ?",
(user_id,),
)
connection.executemany(
"INSERT OR IGNORE INTO user_post_samples (telegram_user_id, post_text) VALUES (?, ?)",
[(user_id, post) for post in samples],
@@ -242,6 +261,10 @@ def add_training_note(user_id: int, note: str, db_path: str | None = None) -> li
return get_training_notes(user_id, db_path)
with closing(connect(db_path)) as connection, connection:
connection.execute("INSERT OR IGNORE INTO users (telegram_user_id) VALUES (?)", (user_id,))
connection.execute(
"UPDATE users SET is_trained = 1, updated_at = CURRENT_TIMESTAMP WHERE telegram_user_id = ?",
(user_id,),
)
connection.execute(
"INSERT OR IGNORE INTO user_training_notes (telegram_user_id, note_text) VALUES (?, ?)",
(user_id, text),
@@ -407,6 +430,14 @@ def get_tone_of_voice(user_id: int, db_path: str | None = None) -> str:
return str(row["tone_of_voice"] or "") if row else ""
def is_user_trained(user_id: int, db_path: str | None = None) -> bool:
with closing(connect(db_path)) as connection:
row = connection.execute(
"SELECT is_trained FROM users WHERE telegram_user_id = ?", (user_id,)
).fetchone()
return bool(row["is_trained"]) if row else False
def get_deploy_notice_version(db_path: str | None = None) -> str:
with closing(connect(db_path)) as connection, connection:
connection.execute(