feat:新增对话历史列表
This commit is contained in:
@@ -5,7 +5,17 @@ from typing import Protocol
|
||||
from uuid import uuid4
|
||||
|
||||
from app.config import settings
|
||||
from app.schemas.conversation import Turn
|
||||
from app.schemas.conversation import ConversationSummary, Turn
|
||||
|
||||
TITLE_MAX_LENGTH = 30
|
||||
|
||||
|
||||
def make_title(prompt: str) -> str:
|
||||
"""会话标题取首轮用户输入,合并空白后截断"""
|
||||
title = " ".join(prompt.split())
|
||||
if len(title) <= TITLE_MAX_LENGTH:
|
||||
return title
|
||||
return title[:TITLE_MAX_LENGTH] + "…"
|
||||
|
||||
|
||||
class ConversationStore(Protocol):
|
||||
@@ -23,6 +33,12 @@ class ConversationStore(Protocol):
|
||||
|
||||
async def save_turn(self, turn: Turn) -> None: ...
|
||||
|
||||
async def list_by_user(
|
||||
self, user_id: int, limit: int, offset: int
|
||||
) -> list[ConversationSummary]:
|
||||
"""该用户已产生对话的会话,按最近更新时间倒序;没有对话的空会话不返回"""
|
||||
...
|
||||
|
||||
|
||||
def _now() -> datetime:
|
||||
return datetime.now(UTC)
|
||||
@@ -77,3 +93,26 @@ class InMemoryConversationStore:
|
||||
if existing is not None:
|
||||
turn = turn.model_copy(update={"created_at": existing.created_at})
|
||||
self._turns[turn.conversation_id][turn.thread_id] = turn
|
||||
|
||||
async def list_by_user(
|
||||
self, user_id: int, limit: int, offset: int
|
||||
) -> list[ConversationSummary]:
|
||||
summaries = []
|
||||
for conversation_id, owner in self._owner.items():
|
||||
turns = sorted(
|
||||
self._turns.get(conversation_id, {}).values(),
|
||||
key=lambda turn: turn.created_at,
|
||||
)
|
||||
if owner != user_id or not turns:
|
||||
continue
|
||||
summaries.append(
|
||||
ConversationSummary(
|
||||
conversation_id=conversation_id,
|
||||
title=make_title(turns[0].prompt),
|
||||
updated_at=turns[-1].created_at,
|
||||
)
|
||||
)
|
||||
summaries.sort(
|
||||
key=lambda item: (item.updated_at, item.conversation_id), reverse=True
|
||||
)
|
||||
return summaries[offset : offset + limit]
|
||||
|
||||
Reference in New Issue
Block a user