Skip to content

Commit 29625e0

Browse files
added session id for all custom events
1 parent 621df54 commit 29625e0

1 file changed

Lines changed: 56 additions & 48 deletions

File tree

src/backend/v4/api/router.py

Lines changed: 56 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -273,9 +273,10 @@ async def process_request(
273273
authenticated_user = get_authenticated_user_details(request_headers=request.headers)
274274
user_id = authenticated_user["user_principal_id"]
275275
if not user_id:
276-
track_event_if_configured(
277-
"Error_User_Not_Found", {"status_code": 400, "detail": "no user"}
278-
)
276+
event_props = {"status_code": 400, "detail": "no user"}
277+
if input_task and hasattr(input_task, 'session_id') and input_task.session_id:
278+
event_props["session_id"] = input_task.session_id
279+
track_event_if_configured("Error_User_Not_Found", event_props)
279280
raise HTTPException(status_code=400, detail="no user found")
280281
try:
281282
memory_store = await DatabaseFactory.get_database(user_id=user_id)
@@ -453,15 +454,17 @@ async def plan_approval(
453454
status_code=401, detail="Missing or invalid user information"
454455
)
455456

456-
# Attach session_id to span if plan_id is available
457+
# Attach session_id to span if plan_id is available and capture for events
458+
session_id = None
457459
if human_feedback.plan_id:
458460
try:
459461
memory_store = await DatabaseFactory.get_database(user_id=user_id)
460462
plan = await memory_store.get_plan_by_plan_id(plan_id=human_feedback.plan_id)
461463
if plan and plan.session_id:
464+
session_id = plan.session_id
462465
span = trace.get_current_span()
463466
if span:
464-
span.set_attribute("session_id", plan.session_id)
467+
span.set_attribute("session_id", session_id)
465468
except Exception:
466469
pass # Don't fail request if span attribute fails
467470

@@ -516,16 +519,16 @@ async def plan_approval(
516519
# Use dynamic event name based on approval status
517520
approval_status = "Approved" if human_feedback.approved else "Rejected"
518521
event_name = f"Plan_{approval_status}"
519-
track_event_if_configured(
520-
event_name,
521-
{
522-
"plan_id": human_feedback.plan_id,
523-
"m_plan_id": human_feedback.m_plan_id,
524-
"approved": human_feedback.approved,
525-
"user_id": user_id,
526-
"feedback": human_feedback.feedback,
527-
},
528-
)
522+
event_props = {
523+
"plan_id": human_feedback.plan_id,
524+
"m_plan_id": human_feedback.m_plan_id,
525+
"approved": human_feedback.approved,
526+
"user_id": user_id,
527+
"feedback": human_feedback.feedback,
528+
}
529+
if session_id:
530+
event_props["session_id"] = session_id
531+
track_event_if_configured(event_name, event_props)
529532

530533
return {"status": "approval recorded"}
531534
else:
@@ -615,20 +618,24 @@ async def user_clarification(
615618
status_code=401, detail="Missing or invalid user information"
616619
)
617620

618-
# Attach session_id to span if plan_id is available
621+
# Attach session_id to span if plan_id is available and capture for events
622+
session_id = None
619623
if human_feedback.plan_id:
620624
try:
621625
memory_store = await DatabaseFactory.get_database(user_id=user_id)
622626
plan = await memory_store.get_plan_by_plan_id(plan_id=human_feedback.plan_id)
623627
if plan and plan.session_id:
628+
session_id = plan.session_id
624629
span = trace.get_current_span()
625630
if span:
626-
span.set_attribute("session_id", plan.session_id)
631+
span.set_attribute("session_id", session_id)
627632
except Exception:
628633
pass # Don't fail request if span attribute fails
629634

630635
try:
631-
memory_store = await DatabaseFactory.get_database(user_id=user_id)
636+
if not human_feedback.plan_id:
637+
memory_store = await DatabaseFactory.get_database(user_id=user_id)
638+
# else: memory_store already initialized above
632639
user_current_team = await memory_store.get_current_team(user_id=user_id)
633640
team_id = None
634641
if user_current_team:
@@ -649,14 +656,14 @@ async def user_clarification(
649656
# validate rai
650657
if human_feedback.answer is not None or human_feedback.answer != "":
651658
if not await rai_success(human_feedback.answer, team, memory_store):
652-
track_event_if_configured(
653-
"Error_RAI_Check_Failed",
654-
{
655-
"status": "Plan Clarification ",
656-
"description": human_feedback.answer,
657-
"request_id": human_feedback.request_id,
658-
},
659-
)
659+
event_props = {
660+
"status": "Plan Clarification ",
661+
"description": human_feedback.answer,
662+
"request_id": human_feedback.request_id,
663+
}
664+
if session_id:
665+
event_props["session_id"] = session_id
666+
track_event_if_configured("Error_RAI_Check_Failed", event_props)
660667
raise HTTPException(
661668
status_code=400,
662669
detail={
@@ -690,14 +697,14 @@ async def user_clarification(
690697
print(f"ValueError processing human clarification: {ve}")
691698
except Exception as e:
692699
print(f"Error processing human clarification: {e}")
693-
track_event_if_configured(
694-
"Human_Clarification_Received",
695-
{
696-
"request_id": human_feedback.request_id,
697-
"answer": human_feedback.answer,
698-
"user_id": user_id,
699-
},
700-
)
700+
event_props = {
701+
"request_id": human_feedback.request_id,
702+
"answer": human_feedback.answer,
703+
"user_id": user_id,
704+
}
705+
if session_id:
706+
event_props["session_id"] = session_id
707+
track_event_if_configured("Human_Clarification_Received", event_props)
701708
return {
702709
"status": "clarification recorded",
703710
}
@@ -770,15 +777,17 @@ async def agent_message_user(
770777
status_code=401, detail="Missing or invalid user information"
771778
)
772779

773-
# Attach session_id to span if plan_id is available
780+
# Attach session_id to span if plan_id is available and capture for events
781+
session_id = None
774782
if agent_message.plan_id:
775783
try:
776784
memory_store = await DatabaseFactory.get_database(user_id=user_id)
777785
plan = await memory_store.get_plan_by_plan_id(plan_id=agent_message.plan_id)
778786
if plan and plan.session_id:
787+
session_id = plan.session_id
779788
span = trace.get_current_span()
780789
if span:
781-
span.set_attribute("session_id", plan.session_id)
790+
span.set_attribute("session_id", session_id)
782791
except Exception:
783792
pass # Don't fail request if span attribute fails
784793

@@ -795,14 +804,14 @@ async def agent_message_user(
795804

796805
# Use dynamic event name with agent identifier
797806
event_name = f"Agent_Message_From_{agent_message.agent.replace(' ', '_')}"
798-
track_event_if_configured(
799-
event_name,
800-
{
801-
"agent": agent_message.agent,
802-
"content": agent_message.content,
803-
"user_id": user_id,
804-
},
805-
)
807+
event_props = {
808+
"agent": agent_message.agent,
809+
"content": agent_message.content,
810+
"user_id": user_id,
811+
}
812+
if session_id:
813+
event_props["session_id"] = session_id
814+
track_event_if_configured(event_name, event_props)
806815
return {
807816
"status": "message recorded",
808817
}
@@ -1489,10 +1498,9 @@ async def get_plan_by_id(
14891498
if plan_id:
14901499
plan = await memory_store.get_plan_by_plan_id(plan_id=plan_id)
14911500
if not plan:
1492-
track_event_if_configured(
1493-
"Error_Plan_Not_Found",
1494-
{"status_code": 400, "detail": "Plan not found"},
1495-
)
1501+
event_props = {"status_code": 400, "detail": "Plan not found"}
1502+
# No session_id available since plan not found
1503+
track_event_if_configured("Error_Plan_Not_Found", event_props)
14961504
raise HTTPException(status_code=404, detail="Plan not found")
14971505

14981506
# Attach session_id to span

0 commit comments

Comments
 (0)