840e88daad
Key monitors use 5m close triggers with WeChat alerts and box/convergence auto orders; add pending-order worker, structured WeChat notify, AI settings/messages, session clock, CTP margin sizing, and dual-layer position limits. Co-authored-by: Cursor <cursoragent@cursor.com>
69 lines
1.9 KiB
Python
69 lines
1.9 KiB
Python
# Copyright (c) 2025-2026 马建军. All rights reserved.
|
|
# 专有软件 — 未经授权禁止复制、传播、转售。
|
|
# 严禁用于:带单/代客理财、向他人推荐期货品种或买卖建议、融资配资等业务。
|
|
# 详见 LICENSE.zh-CN.txt 与 docs/软件购买与使用协议.md
|
|
|
|
"""AI 消息存储与展示。"""
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import sqlite3
|
|
from datetime import datetime
|
|
from typing import Any, Callable, Optional
|
|
from zoneinfo import ZoneInfo
|
|
|
|
TZ = ZoneInfo("Asia/Shanghai")
|
|
|
|
CREATE_SQL = """
|
|
CREATE TABLE IF NOT EXISTS ai_messages (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
kind TEXT NOT NULL,
|
|
title TEXT,
|
|
content TEXT NOT NULL,
|
|
meta_json TEXT,
|
|
created_at TEXT NOT NULL
|
|
)
|
|
"""
|
|
|
|
|
|
def ensure_ai_messages_table(conn: sqlite3.Connection) -> None:
|
|
conn.execute(CREATE_SQL)
|
|
conn.execute(
|
|
"CREATE INDEX IF NOT EXISTS idx_ai_messages_created ON ai_messages(created_at DESC)"
|
|
)
|
|
|
|
|
|
def insert_ai_message(
|
|
conn: sqlite3.Connection,
|
|
*,
|
|
kind: str,
|
|
title: str,
|
|
content: str,
|
|
meta: Optional[dict[str, Any]] = None,
|
|
) -> int:
|
|
ensure_ai_messages_table(conn)
|
|
now = datetime.now(TZ).strftime("%Y-%m-%d %H:%M:%S")
|
|
cur = conn.execute(
|
|
"""INSERT INTO ai_messages (kind, title, content, meta_json, created_at)
|
|
VALUES (?,?,?,?,?)""",
|
|
(kind, title, content, json.dumps(meta or {}, ensure_ascii=False), now),
|
|
)
|
|
return int(cur.lastrowid)
|
|
|
|
|
|
def list_ai_messages(conn: sqlite3.Connection, *, limit: int = 100) -> list[dict]:
|
|
ensure_ai_messages_table(conn)
|
|
rows = conn.execute(
|
|
"SELECT * FROM ai_messages ORDER BY id DESC LIMIT ?",
|
|
(max(1, min(500, int(limit))),),
|
|
).fetchall()
|
|
out = []
|
|
for r in rows:
|
|
item = dict(r)
|
|
try:
|
|
item["meta"] = json.loads(item.get("meta_json") or "{}")
|
|
except Exception:
|
|
item["meta"] = {}
|
|
out.append(item)
|
|
return out
|