Skip to content

fix: deduplicate response in paginated messages #1210

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions src/codegate/api/v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down
8 changes: 4 additions & 4 deletions src/codegate/db/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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] = {}
Expand Down