From 204c0a4b74448fb0b3e91a248894bf4fb6266566 Mon Sep 17 00:00:00 2001 From: alex-mcgovern Date: Tue, 4 Mar 2025 15:21:35 +0000 Subject: [PATCH 1/2] fix: deduplicate response --- src/codegate/api/v1.py | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/src/codegate/api/v1.py b/src/codegate/api/v1.py index 00fe45c2..ef019473 100644 --- a/src/codegate/api/v1.py +++ b/src/codegate/api/v1.py @@ -419,19 +419,36 @@ async def get_workspace_messages( raise HTTPException(status_code=500, detail="Internal server error") offset = (page - 1) * page_size - fetched_messages = [] + fetched_messages: List[v1_models.Conversation] = [] while len(fetched_messages) < page_size: messages_batch = await dbreader.get_prompts_with_output_alerts_usage_by_workspace_id( - ws.id, AlertSeverity.CRITICAL.value, page_size, offset, filter_by_ids + ws.id, + AlertSeverity.CRITICAL.value, + page_size - len(fetched_messages), + offset, + filter_by_ids, ) if not messages_batch: break parsed_conversations, _ = await v1_processing.parse_messages_in_conversations( messages_batch ) - fetched_messages.extend(parsed_conversations) - offset += page_size + + for conversation in parsed_conversations: + existing_conversation = next( + (msg for msg in fetched_messages if msg.chat_id == conversation.chat_id), None + ) + if existing_conversation: + existing_conversation.alerts.extend( + alert + for alert in conversation.alerts + if alert not in existing_conversation.alerts + ) + else: + fetched_messages.append(conversation) + + offset += len(messages_batch) final_messages = fetched_messages[:page_size] From 96f6eb15c505ffadf240e2f75443d27f125e9518 Mon Sep 17 00:00:00 2001 From: alex-mcgovern Date: Tue, 4 Mar 2025 15:26:13 +0000 Subject: [PATCH 2/2] lint fix --- src/codegate/db/connection.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/codegate/db/connection.py b/src/codegate/db/connection.py index 9dc67b8b..8b301404 100644 --- a/src/codegate/db/connection.py +++ b/src/codegate/db/connection.py @@ -639,10 +639,10 @@ async def get_prompts_with_output_alerts_usage_by_workspace_id( sql = sql.bindparams(bindparam("filter_ids", expanding=True)) conditions["filter_ids"] = filter_by_ids - fetched_rows: List[ - IntermediatePromptWithOutputUsageAlerts - ] = await self._exec_select_conditions_to_pydantic( - IntermediatePromptWithOutputUsageAlerts, sql, conditions, should_raise=True + fetched_rows: List[IntermediatePromptWithOutputUsageAlerts] = ( + await self._exec_select_conditions_to_pydantic( + IntermediatePromptWithOutputUsageAlerts, sql, conditions, should_raise=True + ) ) prompts_dict: Dict[str, GetPromptWithOutputsRow] = {}