per-campaign settings, default campaign folders, custom prompt fix
All checks were successful
Build and Push / build (push) Successful in 12m10s

This commit is contained in:
KansaiGaijin
2026-07-21 17:46:37 +12:00
parent 0daca2f208
commit ca13989ca0
14 changed files with 181 additions and 99 deletions

View File

@@ -6,6 +6,7 @@ import uuid
from contextlib import contextmanager
from . import config
from .config import campaign_audio_dir, campaign_transcript_dir, campaign_notes_dir
_local = threading.local()
@@ -94,6 +95,14 @@ def init_db():
created_at REAL NOT NULL
)
""")
conn.execute("""
CREATE TABLE IF NOT EXISTS campaign_settings (
campaign_id TEXT NOT NULL REFERENCES campaigns(id) ON DELETE CASCADE,
key TEXT NOT NULL,
value TEXT NOT NULL,
UNIQUE(campaign_id, key)
)
""")
try:
conn.execute("ALTER TABLE sessions ADD COLUMN campaign_id TEXT REFERENCES campaigns(id) ON DELETE SET NULL")
except Exception:
@@ -105,13 +114,30 @@ def init_db():
("default", "Default Campaign", now()),
)
conn.execute("UPDATE sessions SET campaign_id = 'default' WHERE campaign_id IS NULL")
campaign_audio_dir("default").mkdir(parents=True, exist_ok=True)
campaign_transcript_dir("default").mkdir(parents=True, exist_ok=True)
campaign_notes_dir("default").mkdir(parents=True, exist_ok=True)
# seed defaults if empty
existing = {r["key"] for r in conn.execute("SELECT key FROM settings")}
for k, v in config.DEFAULT_SETTINGS.items():
if k not in existing:
# seed global settings (just onboarding_completed)
existing_global = {r["key"] for r in conn.execute("SELECT key FROM settings")}
for k, v in config.GLOBAL_SETTINGS.items():
if k not in existing_global:
conn.execute("INSERT INTO settings (key, value) VALUES (?, ?)", (k, v))
# seed default campaign settings from CAMPAIGN_SETTINGS (migrate old global rows)
existing_cs = {r["key"] for r in conn.execute(
"SELECT key FROM campaign_settings WHERE campaign_id = 'default'"
)}
for k, v in config.CAMPAIGN_SETTINGS.items():
if k not in existing_cs:
# If this key was previously stored in global settings, carry it over
old_row = conn.execute("SELECT value FROM settings WHERE key = ?", (k,)).fetchone()
val = old_row["value"] if old_row else v
conn.execute(
"INSERT INTO campaign_settings (campaign_id, key, value) VALUES (?, ?, ?)",
("default", k, val),
)
def get_settings() -> dict:
conn = get_conn()
@@ -122,6 +148,8 @@ def get_settings() -> dict:
def update_settings(patch: dict):
with tx() as conn:
for k, v in patch.items():
if k not in config.GLOBAL_SETTINGS:
continue
conn.execute(
"INSERT INTO settings (key, value) VALUES (?, ?) "
"ON CONFLICT(key) DO UPDATE SET value=excluded.value",
@@ -129,6 +157,24 @@ def update_settings(patch: dict):
)
def get_campaign_settings(campaign_id: str) -> dict:
conn = get_conn()
rows = conn.execute(
"SELECT key, value FROM campaign_settings WHERE campaign_id = ?", (campaign_id,)
).fetchall()
return {r["key"]: r["value"] for r in rows}
def update_campaign_settings(campaign_id: str, patch: dict):
with tx() as conn:
for k, v in patch.items():
conn.execute(
"INSERT INTO campaign_settings (campaign_id, key, value) VALUES (?, ?, ?) "
"ON CONFLICT(campaign_id, key) DO UPDATE SET value=excluded.value",
(campaign_id, k, str(v)),
)
def new_id() -> str:
return uuid.uuid4().hex[:12]
@@ -168,7 +214,17 @@ def create_campaign(name: str, description: str = "") -> dict:
"INSERT INTO campaigns (id, name, description, created_at) VALUES (?, ?, ?, ?)",
(campaign_id, name, description, now()),
)
from .config import campaign_audio_dir, campaign_transcript_dir, campaign_notes_dir
# Inherit CAMPAIGN_SETTINGS from the "default" campaign as a starting point
parent = conn.execute(
"SELECT key, value FROM campaign_settings WHERE campaign_id = 'default'"
).fetchall()
inherited = {r["key"]: r["value"] for r in parent}
for k, v in config.CAMPAIGN_SETTINGS.items():
val = inherited.get(k, v)
conn.execute(
"INSERT INTO campaign_settings (campaign_id, key, value) VALUES (?, ?, ?)",
(campaign_id, k, val),
)
campaign_audio_dir(campaign_id).mkdir(parents=True, exist_ok=True)
campaign_transcript_dir(campaign_id).mkdir(parents=True, exist_ok=True)
campaign_notes_dir(campaign_id).mkdir(parents=True, exist_ok=True)