Merge branch 'refs/heads/release/v0.2.8' into fix/features_028
This commit is contained in:
@@ -33,35 +33,47 @@ def get_memory_count(
|
|||||||
@router.get("/{end_user_id}/conversations", response_model=ApiResponse)
|
@router.get("/{end_user_id}/conversations", response_model=ApiResponse)
|
||||||
def get_conversations(
|
def get_conversations(
|
||||||
end_user_id: uuid.UUID,
|
end_user_id: uuid.UUID,
|
||||||
|
page: int = 1,
|
||||||
|
pagesize: int = 20,
|
||||||
current_user: User = Depends(get_current_user),
|
current_user: User = Depends(get_current_user),
|
||||||
db: Session = Depends(get_db)
|
db: Session = Depends(get_db)
|
||||||
):
|
):
|
||||||
"""
|
"""
|
||||||
Retrieve all conversations for the current user in a specific group.
|
Retrieve conversations for the current user in a specific group with pagination.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
end_user_id (UUID): The group identifier.
|
end_user_id (UUID): The group identifier.
|
||||||
|
page (int): Page number (1-based). Defaults to 1.
|
||||||
|
pagesize (int): Number of items per page. Defaults to 20.
|
||||||
current_user (User, optional): The authenticated user.
|
current_user (User, optional): The authenticated user.
|
||||||
db (Session, optional): SQLAlchemy session.
|
db (Session, optional): SQLAlchemy session.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
ApiResponse: Contains a list of conversation IDs.
|
ApiResponse: Contains a paginated list of conversations.
|
||||||
|
|
||||||
Notes:
|
|
||||||
- Initializes the ConversationService with the current DB session.
|
|
||||||
- Returns only conversation IDs for lightweight response.
|
|
||||||
- Logs can be added to trace requests in production.
|
|
||||||
"""
|
"""
|
||||||
|
page = max(1, page)
|
||||||
|
page_size = max(1, min(pagesize, 100)) # Limit page size between 1 and 100
|
||||||
conversation_service = ConversationService(db)
|
conversation_service = ConversationService(db)
|
||||||
conversations = conversation_service.get_user_conversations(
|
conversations, total = conversation_service.get_user_conversations(
|
||||||
end_user_id
|
end_user_id,
|
||||||
|
page=page,
|
||||||
|
page_size=page_size
|
||||||
)
|
)
|
||||||
return success(data=[
|
return success(data={
|
||||||
{
|
"items": [
|
||||||
"id": conversation.id,
|
{
|
||||||
"title": conversation.title
|
"id": conversation.id,
|
||||||
} for conversation in conversations
|
"title": conversation.title
|
||||||
], msg="get conversations success")
|
} for conversation in conversations
|
||||||
|
],
|
||||||
|
"total": total,
|
||||||
|
"page": {
|
||||||
|
"page": page,
|
||||||
|
"pagesize": page_size,
|
||||||
|
"total": total,
|
||||||
|
"hasnext": (page * page_size) < total
|
||||||
|
},
|
||||||
|
}, msg="get conversations success")
|
||||||
|
|
||||||
|
|
||||||
@router.get("/{end_user_id}/messages", response_model=ApiResponse)
|
@router.get("/{end_user_id}/messages", response_model=ApiResponse)
|
||||||
|
|||||||
@@ -90,27 +90,27 @@ class ConversationRepository:
|
|||||||
self,
|
self,
|
||||||
user_id: uuid.UUID,
|
user_id: uuid.UUID,
|
||||||
workspace_id: uuid.UUID = None,
|
workspace_id: uuid.UUID = None,
|
||||||
limit: int = 10,
|
is_activate: bool = True,
|
||||||
is_activate: bool = True
|
page: int = 1,
|
||||||
) -> list[Conversation]:
|
page_size: int = 20
|
||||||
|
) -> tuple[list[Conversation], int]:
|
||||||
"""
|
"""
|
||||||
Retrieve recent conversations for a specific user.
|
Retrieve recent conversations for a specific user with pagination.
|
||||||
|
|
||||||
This method queries conversations associated with the given user ID,
|
This method queries conversations associated with the given user ID,
|
||||||
optionally scoped to a specific workspace. Results are ordered by the
|
optionally scoped to a specific workspace. Results are ordered by the
|
||||||
most recently updated conversations and limited to a fixed number.
|
most recently updated conversations.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
user_id (uuid.UUID): Unique identifier of the user.
|
user_id (uuid.UUID): Unique identifier of the user.
|
||||||
workspace_id (uuid.UUID, optional): Workspace scope for the query.
|
workspace_id (uuid.UUID, optional): Workspace scope for the query.
|
||||||
If provided, only conversations under this workspace will be returned.
|
If provided, only conversations under this workspace will be returned.
|
||||||
limit (int): Maximum number of conversations to return.
|
is_activate (bool): Conversation State limit.
|
||||||
Defaults to 10.
|
page (int): Page number (1-based). Defaults to 1.
|
||||||
is_activate (bool): Convsersation State limit
|
page_size (int): Number of items per page. Defaults to 20.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
list[Conversation]: A list of conversation entities ordered by
|
tuple[list[Conversation], int]: A list of conversation entities and total count.
|
||||||
last updated time (descending).
|
|
||||||
"""
|
"""
|
||||||
logger.info(f"Fetching conversation by user_id: {user_id}")
|
logger.info(f"Fetching conversation by user_id: {user_id}")
|
||||||
|
|
||||||
@@ -122,18 +122,25 @@ class ConversationRepository:
|
|||||||
if workspace_id:
|
if workspace_id:
|
||||||
stmt = stmt.where(Conversation.workspace_id == workspace_id)
|
stmt = stmt.where(Conversation.workspace_id == workspace_id)
|
||||||
|
|
||||||
stmt = stmt.order_by(desc(Conversation.updated_at))
|
# Calculate total count
|
||||||
stmt = stmt.limit(limit)
|
total = int(self.db.execute(
|
||||||
|
select(func.count()).select_from(stmt.subquery())
|
||||||
|
).scalar_one())
|
||||||
|
|
||||||
convsersations = list(self.db.scalars(stmt).all())
|
# Apply ordering and pagination
|
||||||
|
stmt = stmt.order_by(desc(Conversation.updated_at))
|
||||||
|
stmt = stmt.offset((page - 1) * page_size).limit(page_size)
|
||||||
|
|
||||||
|
conversations = list(self.db.scalars(stmt).all())
|
||||||
logger.info(
|
logger.info(
|
||||||
"Conversation fetched successfully",
|
"Conversation fetched successfully",
|
||||||
extra={
|
extra={
|
||||||
"user_id": str(user_id),
|
"user_id": str(user_id),
|
||||||
"workspace_id": str(workspace_id),
|
"workspace_id": str(workspace_id),
|
||||||
|
"total": total,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
return convsersations
|
return conversations, total
|
||||||
|
|
||||||
def list_conversations(
|
def list_conversations(
|
||||||
self,
|
self,
|
||||||
|
|||||||
@@ -119,25 +119,27 @@ class ConversationService:
|
|||||||
|
|
||||||
def get_user_conversations(
|
def get_user_conversations(
|
||||||
self,
|
self,
|
||||||
user_id: uuid.UUID
|
user_id: uuid.UUID,
|
||||||
) -> list[Conversation]:
|
page: int = 1,
|
||||||
|
page_size: int = 20
|
||||||
|
) -> tuple[list[Conversation], int]:
|
||||||
"""
|
"""
|
||||||
Retrieve recent conversations for a specific user
|
Retrieve recent conversations for a specific user with pagination.
|
||||||
|
|
||||||
This method delegates persistence logic to the repository layer and
|
|
||||||
applies service-level defaults (e.g. recent conversation limit).
|
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
user_id (uuid.UUID): Unique identifier of the user.
|
user_id (uuid.UUID): Unique identifier of the user.
|
||||||
|
page (int): Page number (1-based). Defaults to 1.
|
||||||
|
page_size (int): Number of items per page. Defaults to 20.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
list[Conversation]: A list of recent conversation entities.
|
tuple[list[Conversation], int]: A list of recent conversation entities and total count.
|
||||||
"""
|
"""
|
||||||
conversations = self.conversation_repo.get_conversation_by_user_id(
|
conversations, total = self.conversation_repo.get_conversation_by_user_id(
|
||||||
user_id,
|
user_id,
|
||||||
limit=10
|
page=page,
|
||||||
|
page_size=page_size
|
||||||
)
|
)
|
||||||
return conversations
|
return conversations, total
|
||||||
|
|
||||||
def list_conversations(
|
def list_conversations(
|
||||||
self,
|
self,
|
||||||
|
|||||||
@@ -619,7 +619,7 @@ class MemoryForgetService:
|
|||||||
recent_trends.append({
|
recent_trends.append({
|
||||||
'date': date_str,
|
'date': date_str,
|
||||||
'merged_count': record.merged_count,
|
'merged_count': record.merged_count,
|
||||||
'average_activation': record.average_activation_value,
|
'average_activation': round(record.average_activation_value, 2) if record.average_activation_value is not None else None,
|
||||||
'total_nodes': record.total_nodes,
|
'total_nodes': record.total_nodes,
|
||||||
'execution_time': int(record.execution_time.timestamp() * 1000)
|
'execution_time': int(record.execution_time.timestamp() * 1000)
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -1408,12 +1408,11 @@ async def analytics_memory_types(
|
|||||||
if end_user_id:
|
if end_user_id:
|
||||||
try:
|
try:
|
||||||
conversation_repo = ConversationRepository(db)
|
conversation_repo = ConversationRepository(db)
|
||||||
conversations = conversation_repo.get_conversation_by_user_id(
|
conversations, total = conversation_repo.get_conversation_by_user_id(
|
||||||
user_id=uuid.UUID(end_user_id),
|
user_id=uuid.UUID(end_user_id),
|
||||||
limit=100, # 获取更多会话以准确统计
|
|
||||||
is_activate=True
|
is_activate=True
|
||||||
)
|
)
|
||||||
work_count = len(conversations)
|
work_count = total
|
||||||
logger.debug(f"工作记忆数量(会话数): {work_count} (end_user_id={end_user_id})")
|
logger.debug(f"工作记忆数量(会话数): {work_count} (end_user_id={end_user_id})")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.warning(f"获取会话数量失败,工作记忆数量设为0: {str(e)}")
|
logger.warning(f"获取会话数量失败,工作记忆数量设为0: {str(e)}")
|
||||||
|
|||||||
Reference in New Issue
Block a user