Lark AI Agent 🚀

This commit is contained in:
cvcat
2025-04-17 21:40:51 +08:00
commit ce1804b35a
39 changed files with 3389 additions and 0 deletions

3
app/db/__init__.py Normal file
View File

@ -0,0 +1,3 @@
"""
Database connection and models
"""

23
app/db/models.py Normal file
View File

@ -0,0 +1,23 @@
from sqlalchemy import Column, Integer, String, Text, Boolean, DateTime
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.sql import func
from datetime import datetime
Base = declarative_base()
class Message(Base):
"""Model for storing Lark chat messages"""
__tablename__ = "messages"
id = Column(Integer, primary_key=True, autoincrement=True)
user_name = Column(String(255), nullable=False, comment="User display name")
user_id = Column(String(255), nullable=False, comment="User ID from Lark")
content = Column(Text, nullable=False, comment="Message content")
is_group_chat = Column(Boolean, default=False, comment="Whether the message is from a group chat")
group_name = Column(String(255), nullable=True, comment="Group chat name (if applicable)")
chat_id = Column(String(255), nullable=False, comment="Chat ID")
message_time = Column(DateTime, default=datetime.now, comment="Time when the message was sent")
created_at = Column(DateTime, default=func.now(), comment="Record creation time")
def __repr__(self):
return f"<Message(id={self.id}, user_name='{self.user_name}', content='{self.content[:20]}...', is_group_chat={self.is_group_chat})>"

67
app/db/session.py Normal file
View File

@ -0,0 +1,67 @@
"""
Database session management and initialization
"""
from loguru import logger
from app.db.models import Base
from app.config.settings import settings
from sqlalchemy import create_engine, text, exc
from sqlalchemy.orm import sessionmaker, scoped_session
from sqlalchemy.pool import QueuePool
def get_db_uri_without_db():
"""获取不包含特定数据库名的URI"""
uri_parts = settings.SQLALCHEMY_DATABASE_URI.split('/')
uri_parts[-1] = ''
return '/'.join(uri_parts)
def create_database_if_not_exists():
"""如果数据库不存在则创建"""
try:
uri = get_db_uri_without_db()
temp_engine = create_engine(uri)
with temp_engine.connect() as conn:
create_db_sql = text(f"CREATE DATABASE IF NOT EXISTS {settings.DB_NAME} CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci")
conn.execute(create_db_sql)
conn.commit()
logger.info(f"数据库 '{settings.DB_NAME}' 已确认存在或已创建")
except Exception as e:
logger.error(f"创建数据库时出错: {str(e)}")
raise
engine = create_engine(
settings.SQLALCHEMY_DATABASE_URI,
pool_size=10,
max_overflow=20,
pool_recycle=3600,
pool_pre_ping=True,
pool_timeout=30,
poolclass=QueuePool
)
session_factory = sessionmaker(bind=engine)
SessionLocal = scoped_session(session_factory)
def init_db():
"""初始化数据库,如果不存在则创建"""
try:
create_database_if_not_exists()
Base.metadata.create_all(bind=engine)
except Exception as e:
logger.error(f"初始化数据库时出错: {str(e)}")
raise
def get_db_session():
"""获取数据库会话"""
db = SessionLocal()
try:
return db
except Exception:
db.close()
raise
def close_db_session(db):
"""关闭数据库会话"""
if db:
db.close()