Expose sim/live matching mode in env config UI.

Saving SIM_DEFAULT_MODE also switches runtime trading.mode immediately.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-08-14 18:14:17 +08:00
parent 56618a6655
commit 919ac70bc2
5 changed files with 67 additions and 4 deletions
+28
View File
@@ -20,6 +20,34 @@ def default_trading_mode() -> str:
return MODE_SIM
def peek_persisted_trading_mode(db_path: str | None = None) -> str | None:
"""只读 SQLite 中的 trading.mode(供 env 配置页展示当前生效值)."""
import sqlite3
path = (db_path or os.getenv("DB_PATH") or "crypto.db").strip()
if not path:
return None
if not os.path.isabs(path):
path = os.path.abspath(path)
if not os.path.isfile(path):
return None
try:
conn = sqlite3.connect(path, timeout=2)
try:
row = conn.execute(
"SELECT value FROM app_runtime_settings WHERE key=?",
(TRADING_MODE_KEY,),
).fetchone()
finally:
conn.close()
except Exception:
return None
if not row:
return None
m = str(row[0] or "").strip().lower()
return m if m in VALID_MODES else None
def normalize_mode(mode: str | None) -> str:
m = (mode or "").strip().lower()
if m in VALID_MODES: