ForcePilot/backend/package/yuxi/channel/extensions/wecom/persistent_dedupe.py
Kris 7215418610 feat(channel): 添加企业微信、微博、WhatsApp 和 Workplace 渠道扩展
新增企业微信、微博、WhatsApp、Workplace 四个渠道扩展。

企业微信渠道扩展主要模块:config, gateway, webhook, webhook_bot, outbound, streaming, pairing, security, crypto, dedupe, persistent_dedupe, card, directory, events, externalcontact, media, mentions, menu, message, oauth, status

微博渠道扩展主要模块:config, gateway, webhook, outbound, streaming, pairing, security, dedupe, passive_reply, broadcast, message, menu, media, subscription, template, status

WhatsApp 渠道扩展主要模块:config, gateway, webhook, outbound, streaming, pairing, security, dedupe, actions, monitor, status

Workplace 渠道扩展主要模块:config, gateway, webhook, outbound, streaming, pairing, security, dedupe, actions, challenge, groups, media, mentions, menu, monitor, persona, quick_reply, signature, subscriptions, template, threading, users, status
2026-05-21 12:01:56 +08:00

76 lines
2.3 KiB
Python

import logging
import sqlite3
import threading
import time
from pathlib import Path
logger = logging.getLogger(__name__)
DEFAULT_DB_DIR = Path.home() / ".forcepilot" / "wecom"
DEFAULT_TTL_SECONDS = 7200
class PersistentDeduplicator:
def __init__(
self,
db_path: str | None = None,
ttl: int = DEFAULT_TTL_SECONDS,
):
if db_path is None:
DEFAULT_DB_DIR.mkdir(parents=True, exist_ok=True)
db_path = str(DEFAULT_DB_DIR / "dedupe.db")
self._db_path = db_path
self._ttl = ttl
self._lock = threading.Lock()
self._conn: sqlite3.Connection | None = None
self._init_db()
def _init_db(self):
with self._lock:
conn = sqlite3.connect(self._db_path)
conn.execute("CREATE TABLE IF NOT EXISTS dedupe ( msg_id TEXT PRIMARY KEY, created_at REAL NOT NULL)")
conn.execute("CREATE INDEX IF NOT EXISTS idx_dedupe_created ON dedupe(created_at)")
conn.commit()
self._conn = conn
def is_duplicate(self, msg_id: str) -> bool:
if not msg_id:
return False
self._evict_expired()
with self._lock:
try:
cur = self._conn.execute("SELECT 1 FROM dedupe WHERE msg_id = ?", (msg_id,))
if cur.fetchone():
return True
self._conn.execute(
"INSERT OR REPLACE INTO dedupe(msg_id, created_at) VALUES (?, ?)",
(msg_id, time.time()),
)
self._conn.commit()
return False
except Exception:
logger.exception("PersistentDeduplicator error for msg_id=%s", msg_id)
return False
def _evict_expired(self):
cutoff = time.time() - self._ttl
with self._lock:
try:
self._conn.execute("DELETE FROM dedupe WHERE created_at < ?", (cutoff,))
self._conn.commit()
except Exception:
logger.exception("PersistentDeduplicator evict error")
def close(self):
with self._lock:
if self._conn:
try:
self._conn.close()
except Exception:
pass
self._conn = None