Skip to content
This repository was archived by the owner on Jun 5, 2025. It is now read-only.

Commit 0403f14

Browse files
committed
decouple alerts from question/answer
1 parent a1a3efe commit 0403f14

File tree

5 files changed

+24
-16
lines changed

5 files changed

+24
-16
lines changed

src/codegate/api/v1.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,9 @@ async def get_workspace_alerts(workspace_name: str) -> List[Optional[v1_models.A
420420
raise HTTPException(status_code=500, detail="Internal server error")
421421

422422
try:
423-
alerts = await dbreader.get_alerts_by_workspace(ws.id, AlertSeverity.CRITICAL.value)
423+
alerts = await dbreader.get_alerts_by_workspace_or_prompt_id(
424+
workspace_id=ws.id, trigger_category=AlertSeverity.CRITICAL.value
425+
)
424426
prompts_outputs = await dbreader.get_prompts_with_output(ws.id)
425427
return await v1_processing.parse_get_alert_conversation(alerts, prompts_outputs)
426428
except Exception:
@@ -576,10 +578,19 @@ async def get_messages_by_prompt_id(
576578
prompts_outputs = await dbreader.get_prompts_with_output(
577579
workspace_id=ws.id, prompt_id=prompt_id
578580
)
581+
582+
# get all alerts for the prompt
583+
alerts = await dbreader.get_alerts_by_workspace_or_prompt_id(
584+
workspace_id=ws.id, prompt_id=prompt_id, trigger_category=AlertSeverity.CRITICAL.value
585+
)
586+
deduped_alerts = await v1_processing.remove_duplicate_alerts(alerts)
579587
conversations, _ = await v1_processing.parse_messages_in_conversations(prompts_outputs)
580588
if not conversations:
581589
raise HTTPException(status_code=404, detail="Conversation not found")
582-
return conversations[0]
590+
591+
conversation = conversations[0]
592+
conversation.alerts = deduped_alerts
593+
return conversation
583594

584595

585596
@v1.get(

src/codegate/api/v1_models.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,6 @@ class PartialQuestionAnswer(pydantic.BaseModel):
202202
partial_questions: PartialQuestions
203203
answer: Optional[ChatMessage]
204204
model_token_usage: TokenUsageByModel
205-
alerts: List[Alert] = []
206205

207206

208207
class Conversation(pydantic.BaseModel):
@@ -216,7 +215,7 @@ class Conversation(pydantic.BaseModel):
216215
chat_id: str
217216
conversation_timestamp: datetime.datetime
218217
token_usage_agg: Optional[TokenUsageAggregate]
219-
alerts: List[Alert] = []
218+
alerts: Optional[List[Alert]] = []
220219

221220

222221
class ConversationSummary(pydantic.BaseModel):

src/codegate/api/v1_processing.py

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -202,15 +202,10 @@ async def _get_partial_question_answer(
202202
model=model, token_usage=token_usage, provider_type=provider
203203
)
204204

205-
alerts: List[v1_models.Alert] = [
206-
v1_models.Alert.from_db_model(db_alert) for db_alert in row.alerts
207-
]
208-
209205
return PartialQuestionAnswer(
210206
partial_questions=request_message,
211207
answer=output_message,
212208
model_token_usage=model_token_usage,
213-
alerts=alerts,
214209
)
215210

216211

@@ -374,7 +369,7 @@ async def match_conversations(
374369
for group in grouped_partial_questions:
375370
questions_answers: List[QuestionAnswer] = []
376371
token_usage_agg = TokenUsageAggregate(tokens_by_model={}, token_usage=TokenUsage())
377-
alerts: List[v1_models.Alert] = []
372+
378373
first_partial_qa = None
379374
for partial_question in sorted(group, key=lambda x: x.timestamp):
380375
# Partial questions don't contain the answer, so we need to find the corresponding
@@ -398,8 +393,6 @@ async def match_conversations(
398393
qa = _get_question_answer_from_partial(selected_partial_qa)
399394
qa.question.message = parse_question_answer(qa.question.message)
400395
questions_answers.append(qa)
401-
deduped_alerts = await remove_duplicate_alerts(selected_partial_qa.alerts)
402-
alerts.extend(deduped_alerts)
403396
token_usage_agg.add_model_token_usage(selected_partial_qa.model_token_usage)
404397

405398
# if we have a conversation with at least one question and answer
@@ -413,7 +406,6 @@ async def match_conversations(
413406
chat_id=first_partial_qa.partial_questions.message_id,
414407
conversation_timestamp=first_partial_qa.partial_questions.timestamp,
415408
token_usage_agg=token_usage_agg,
416-
alerts=alerts,
417409
)
418410
for qa in questions_answers:
419411
map_q_id_to_conversation[qa.question.message_id] = conversation

src/codegate/db/connection.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -854,8 +854,11 @@ async def get_total_messages_count_by_workspace_id(
854854
logger.error(f"Failed to fetch message count. Error: {e}")
855855
return 0 # Return 0 in case of failure
856856

857-
async def get_alerts_by_workspace(
858-
self, workspace_id: str, trigger_category: Optional[str] = None
857+
async def get_alerts_by_workspace_or_prompt_id(
858+
self,
859+
workspace_id: str,
860+
prompt_id: Optional[str] = None,
861+
trigger_category: Optional[str] = None,
859862
) -> List[Alert]:
860863
sql = text(
861864
"""
@@ -874,6 +877,10 @@ async def get_alerts_by_workspace(
874877
)
875878
conditions = {"workspace_id": workspace_id}
876879

880+
if prompt_id:
881+
sql = text(sql.text + " AND a.prompt_id = :prompt_id")
882+
conditions["prompt_id"] = prompt_id
883+
877884
if trigger_category:
878885
sql = text(sql.text + " AND a.trigger_category = :trigger_category")
879886
conditions["trigger_category"] = trigger_category

src/codegate/db/models.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,4 +352,3 @@ class GetMessagesRow(BaseModel):
352352
output_tokens: Optional[int]
353353
input_cost: Optional[float]
354354
output_cost: Optional[float]
355-
alerts: List[Alert] = []

0 commit comments

Comments
 (0)