feat: improve chat feedback navigation

This commit is contained in:
2026-08-31 11:41:56 +08:00
parent 5740bd26e7
commit f4b2836c8a
11 changed files with 422 additions and 41 deletions

View File

@@ -0,0 +1,32 @@
from __future__ import annotations
from sqlalchemy import func, or_, select
from sqlalchemy.orm import Session
from app.models.chat import ChatMessage
class ChatMessageNavigationService:
"""Shared chronological paging rules for locating a message in a conversation."""
@staticmethod
def page_for_message(
db: Session,
*,
session_id: int,
message: ChatMessage,
page_size: int,
) -> int:
position = db.scalar(
select(func.count()).select_from(ChatMessage).where(
ChatMessage.session_id == session_id,
or_(
ChatMessage.created_at < message.created_at,
(
(ChatMessage.created_at == message.created_at)
& (ChatMessage.id <= message.id)
),
),
)
) or 1
return (position - 1) // page_size + 1