Files
aiagent/backend/app/core/database.py
renjianbo a06082480a
Some checks failed
CI/CD Pipeline / Backend — Lint & Test (push) Has been cancelled
CI/CD Pipeline / Frontend — Lint & Build (push) Has been cancelled
CI/CD Pipeline / Docker — Build Check (push) Has been cancelled
feat: persistent chat message storage + Android pull-to-load history
Backend:
- Add ChatMessage model + Alembic migration 024
- Add on_message callback to AgentRuntime for persisting messages during SSE streaming
- Plumb session_id from ChatRequest to AgentContext in all 4 chat endpoints
- Add GET /agent-chat/{id}/sessions and /sessions/{sid}/messages with cursor pagination

Android:
- Add DTOs/ApiService/MessageDao for server-side chat history
- ChatRepository: fetchOlderMessages (API + Room cache), offline fallback
- ChatViewModel: loadMoreHistory with isLoadingMore/hasMoreMessages state
- ChatScreen: scroll-to-top detection + top loading indicator

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-06-30 00:07:26 +08:00

81 lines
2.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""
数据库配置
"""
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from app.core.config import settings
# 创建数据库引擎MySQL
engine = create_engine(
settings.DATABASE_URL,
pool_pre_ping=True,
pool_size=10,
max_overflow=20,
echo=settings.SQL_ECHO # SQL 日志独立开关,默认关闭
)
# 创建会话工厂
# expire_on_commit=Falsecommit 后仍可读取已加载标量,避免 FastAPI 在序列化 ExecutionResponse 时
# 因会话已关闭而再次触发懒加载,从而出现「仅 HTTP 报 DATABASE_ERROR、TestClient 正常」的现象。
SessionLocal = sessionmaker(
autocommit=False, autoflush=False, bind=engine, expire_on_commit=False
)
# 创建基础模型类
Base = declarative_base()
def get_db():
"""获取数据库会话"""
db = SessionLocal()
try:
yield db
finally:
db.close()
def init_db():
"""初始化数据库,创建所有表"""
# 导入所有模型,确保它们被注册
import app.models.user
import app.models.workflow
import app.models.agent
import app.models.execution
import app.models.model_config
import app.models.workflow_template
import app.models.permission
import app.models.alert_rule
import app.models.agent_llm_log
import app.models.agent_vector_memory
import app.models.agent_learning_pattern
import app.models.agent_schedule
import app.models.knowledge_base
import app.models.notification
import app.models.orchestration_template
import app.models.plugin
import app.models.goal
import app.models.task
import app.models.tool
import app.models.data_source
import app.models.execution_log
import app.models.agent_execution_log
import app.models.feedback_record
import app.models.knowledge_entry
import app.models.node_template
import app.models.persistent_user_memory
import app.models.shadow_comparison
import app.models.user_behavior
import app.models.user_feishu_open_id
import app.models.user_fingerprint
import app.models.workflow_version
import app.models.audit_log
import app.models.conversation_branch
import app.models.push_subscription
import app.models.fcm_token
import app.models.workspace
import app.models.scene_contract
import app.models.team
import app.models.chat_message
Base.metadata.create_all(bind=engine)