From 605317b8c2793a5fe9b0d3bc9c00add1819381aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D0=B8=D0=BA=D1=82=D0=BE=D1=80?= <78488229+viktor138irk@users.noreply.github.com> Date: Mon, 11 May 2026 22:17:10 +0900 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=BE=20=D1=85=D1=80=D0=B0=D0=BD=D0=B8=D0=BB=D0=B8=D1=89?= =?UTF-8?q?=D0=B5=20=D0=BD=D0=B0=D1=81=D1=82=D1=80=D0=BE=D0=B5=D0=BA=20Dev?= =?UTF-8?q?Console?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/config_store.py | 57 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 backend/config_store.py diff --git a/backend/config_store.py b/backend/config_store.py new file mode 100644 index 0000000..d78ad91 --- /dev/null +++ b/backend/config_store.py @@ -0,0 +1,57 @@ +import os +import sqlite3 +from pathlib import Path +from typing import Optional + +DATA_DIR = Path(os.getenv('DEVCONSOLE_DATA_DIR', '/var/lib/devconsole')) +DB_PATH = DATA_DIR / 'devconsole.db' + + +def _connect() -> sqlite3.Connection: + DATA_DIR.mkdir(parents=True, exist_ok=True) + conn = sqlite3.connect(DB_PATH) + conn.row_factory = sqlite3.Row + conn.execute( + ''' + CREATE TABLE IF NOT EXISTS settings ( + key TEXT PRIMARY KEY, + value TEXT NOT NULL, + updated_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP + ) + ''' + ) + conn.commit() + return conn + + +def set_setting(key: str, value: str) -> None: + with _connect() as conn: + conn.execute( + ''' + INSERT INTO settings(key, value, updated_at) + VALUES (?, ?, CURRENT_TIMESTAMP) + ON CONFLICT(key) DO UPDATE SET + value = excluded.value, + updated_at = CURRENT_TIMESTAMP + ''', + (key, value), + ) + conn.commit() + + +def get_setting(key: str) -> Optional[str]: + with _connect() as conn: + row = conn.execute('SELECT value FROM settings WHERE key = ?', (key,)).fetchone() + return row['value'] if row else None + + +def has_openai_key() -> bool: + return bool(get_openai_key()) + + +def get_openai_key() -> Optional[str]: + return get_setting('OPENAI_API_KEY') or os.getenv('OPENAI_API_KEY') or None + + +def get_openai_model() -> str: + return get_setting('OPENAI_MODEL') or os.getenv('OPENAI_MODEL') or 'gpt-5'