-
Notifications
You must be signed in to change notification settings - Fork 711
Expand file tree
/
Copy pathrouter.py
More file actions
1537 lines (1402 loc) · 53.9 KB
/
Copy pathrouter.py
File metadata and controls
1537 lines (1402 loc) · 53.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import asyncio
import json
import logging
import uuid
from typing import Optional
from opentelemetry import trace
import v4.models.messages as messages
from v4.models.messages import WebsocketMessageType
from auth.auth_utils import get_authenticated_user_details
from common.database.database_factory import DatabaseFactory
from common.models.messages_af import (
InputTask,
Plan,
PlanStatus,
TeamSelectionRequest,
)
from common.utils.event_utils import track_event_if_configured
from common.utils.utils_af import (
find_first_available_team,
rai_success,
rai_validate_team_config,
)
from fastapi import (
APIRouter,
BackgroundTasks,
File,
HTTPException,
Query,
Request,
UploadFile,
WebSocket,
WebSocketDisconnect,
)
from v4.common.services.plan_service import PlanService
from v4.common.services.team_service import TeamService
from v4.config.settings import (
connection_config,
orchestration_config,
team_config,
)
from v4.orchestration.orchestration_manager import OrchestrationManager
router = APIRouter()
logger = logging.getLogger(__name__)
app_v4 = APIRouter(
prefix="/api/v4",
responses={404: {"description": "Not found"}},
)
@app_v4.websocket("/socket/{process_id}")
async def start_comms(
websocket: WebSocket, process_id: str, user_id: str = Query(None)
):
"""Web-Socket endpoint for real-time process status updates."""
# Always accept the WebSocket connection first
await websocket.accept()
user_id = user_id or "00000000-0000-0000-0000-000000000000"
# Manually create a span for WebSocket since excluded_urls suppresses auto-instrumentation.
# Without this, all track_event_if_configured calls inside WebSocket would get operation_Id = 0.
tracer = trace.get_tracer(__name__)
with tracer.start_as_current_span(
"WebSocket_Connection",
attributes={"process_id": process_id, "user_id": user_id},
) as ws_span:
# Resolve session_id from plan for telemetry
session_id = None
try:
memory_store = await DatabaseFactory.get_database(user_id=user_id)
plan = await memory_store.get_plan_by_plan_id(plan_id=process_id)
if plan:
session_id = getattr(plan, 'session_id', None)
if session_id:
ws_span.set_attribute("session_id", session_id)
except Exception as e:
logging.warning(f"[websocket] Failed to resolve session_id: {e}")
# Add to the connection manager for backend updates
connection_config.add_connection(
process_id=process_id, connection=websocket, user_id=user_id
)
ws_props = {"process_id": process_id, "user_id": user_id}
if session_id:
ws_props["session_id"] = session_id
track_event_if_configured("WebSocket_Connected", ws_props)
# Keep the connection open - FastAPI will close the connection if this returns
try:
# Keep the connection open - FastAPI will close the connection if this returns
while True:
# no expectation that we will receive anything from the client but this keeps
# the connection open and does not take cpu cycle
try:
message = await websocket.receive_text()
logging.debug(f"Received WebSocket message from {user_id}: {message}")
except asyncio.TimeoutError:
# Ignore timeouts to keep the WebSocket connection open, but avoid a tight loop.
logging.debug(
f"WebSocket receive timeout for user {user_id}, process {process_id}"
)
await asyncio.sleep(0.1)
except WebSocketDisconnect:
dc_props = {"process_id": process_id, "user_id": user_id}
if session_id:
dc_props["session_id"] = session_id
track_event_if_configured("WebSocket_Disconnected", dc_props)
logging.info(f"Client disconnected from batch {process_id}")
break
except Exception as e:
# Fixed logging syntax - removed the error= parameter
logging.error(f"Error in WebSocket connection: {str(e)}")
finally:
# Always clean up the connection
await connection_config.close_connection(process_id=process_id)
@app_v4.get("/init_team")
async def init_team(
request: Request,
team_switched: bool = Query(False),
): # add team_switched: bool parameter
"""Initialize the user's current team of agents"""
# Get first available team from 4 to 1 (RFP -> Retail -> Marketing -> HR)
# Falls back to HR if no teams are available.
print(f"Init team called, team_switched={team_switched}")
try:
authenticated_user = get_authenticated_user_details(
request_headers=request.headers
)
user_id = authenticated_user["user_principal_id"]
if not user_id:
track_event_if_configured(
"Error_User_Not_Found", {"status_code": 400, "detail": "no user"}
)
raise HTTPException(status_code=400, detail="no user")
# Initialize memory store and service
memory_store = await DatabaseFactory.get_database(user_id=user_id)
team_service = TeamService(memory_store)
init_team_id = await find_first_available_team(team_service, user_id)
# Get current team if user has one
user_current_team = await memory_store.get_current_team(user_id=user_id)
# If no teams available and no current team, return empty state to allow custom team upload
if not init_team_id and not user_current_team:
print("No teams found in database. System ready for custom team upload.")
return {
"status": "No teams configured. Please upload a team configuration to get started.",
"team_id": None,
"team": None,
"requires_team_upload": True,
}
# Use current team if available, otherwise use found team
if user_current_team:
init_team_id = user_current_team.team_id
print(f"Using user's current team: {init_team_id}")
elif init_team_id:
print(f"Using first available team: {init_team_id}")
user_current_team = await team_service.handle_team_selection(
user_id=user_id, team_id=init_team_id
)
if user_current_team:
init_team_id = user_current_team.team_id
# Verify the team exists and user has access to it
team_configuration = await team_service.get_team_configuration(
init_team_id, user_id
)
if team_configuration is None:
# If team doesn't exist, clear current team and return empty state
await memory_store.delete_current_team(user_id)
print(f"Team configuration '{init_team_id}' not found. Cleared current team.")
return {
"status": "Current team configuration not found. Please select or upload a team configuration.",
"team_id": None,
"team": None,
"requires_team_upload": True,
}
# Set as current team in memory
team_config.set_current_team(
user_id=user_id, team_configuration=team_configuration
)
# Initialize agent team for this user session
await OrchestrationManager.get_current_or_new_orchestration(
user_id=user_id,
team_config=team_configuration,
team_switched=team_switched,
team_service=team_service,
)
return {
"status": "Request started successfully",
"team_id": init_team_id,
"team": team_configuration,
}
except Exception as e:
track_event_if_configured(
"Error_Init_Team_Failed",
{
"error": str(e),
},
)
raise HTTPException(
status_code=400, detail=f"Error starting request: {e}"
) from e
@app_v4.post("/process_request")
async def process_request(
background_tasks: BackgroundTasks, input_task: InputTask, request: Request
):
"""
Create a new plan without full processing.
---
tags:
- Plans
parameters:
- name: user_principal_id
in: header
type: string
required: true
description: User ID extracted from the authentication header
- name: body
in: body
required: true
schema:
type: object
properties:
session_id:
type: string
description: Session ID for the plan
description:
type: string
description: The task description to validate and create plan for
responses:
200:
description: Plan created successfully
schema:
type: object
properties:
plan_id:
type: string
description: The ID of the newly created plan
status:
type: string
description: Success message
session_id:
type: string
description: Session ID associated with the plan
400:
description: RAI check failed or invalid input
schema:
type: object
properties:
detail:
type: string
description: Error message
"""
authenticated_user = get_authenticated_user_details(request_headers=request.headers)
user_id = authenticated_user["user_principal_id"]
if not user_id:
event_props = {"status_code": 400, "detail": "no user"}
if input_task and hasattr(input_task, 'session_id') and input_task.session_id:
event_props["session_id"] = input_task.session_id
track_event_if_configured("Error_User_Not_Found", event_props)
raise HTTPException(status_code=400, detail="no user found")
try:
memory_store = await DatabaseFactory.get_database(user_id=user_id)
user_current_team = await memory_store.get_current_team(user_id=user_id)
team_id = None
if user_current_team:
team_id = user_current_team.team_id
team = await memory_store.get_team_by_id(team_id=team_id)
if not team:
raise HTTPException(
status_code=404,
detail=f"Team configuration '{team_id}' not found or access denied",
)
except Exception as e:
raise HTTPException(
status_code=400,
detail=f"Error retrieving team configuration: {e}",
) from e
if not await rai_success(input_task.description, team, memory_store):
track_event_if_configured(
"Error_RAI_Check_Failed",
{
"status": "Plan not created - RAI check failed",
"description": input_task.description,
"session_id": input_task.session_id,
},
)
raise HTTPException(
status_code=400,
detail="Request contains content that doesn't meet our safety guidelines, try again.",
)
if not input_task.session_id:
input_task.session_id = str(uuid.uuid4())
# Attach session_id to current span for Application Insights
span = trace.get_current_span()
if span:
span.set_attribute("session_id", input_task.session_id)
try:
plan_id = str(uuid.uuid4())
# Initialize memory store and service
plan = Plan(
id=plan_id,
plan_id=plan_id,
user_id=user_id,
session_id=input_task.session_id,
team_id=team_id,
initial_goal=input_task.description,
overall_status=PlanStatus.in_progress,
)
await memory_store.add_plan(plan)
# Ensure orchestration is initialized before running
# Force rebuild for each new task since Magentic workflows cannot be reused after completion
team_service = TeamService(memory_store)
await OrchestrationManager.get_current_or_new_orchestration(
user_id=user_id,
team_config=team,
team_switched=False,
team_service=team_service,
force_rebuild=True, # Always rebuild workflow for new tasks
)
track_event_if_configured(
"Plan_Created",
{
"status": "success",
"plan_id": plan.plan_id,
"session_id": input_task.session_id,
"user_id": user_id,
"team_id": team_id,
"description": input_task.description,
},
)
except Exception as e:
print(f"Error creating plan: {e}")
track_event_if_configured(
"Error_Plan_Creation_Failed",
{
"status": "error",
"description": input_task.description,
"session_id": input_task.session_id,
"user_id": user_id,
"error": str(e),
},
)
raise HTTPException(status_code=500, detail="Failed to create plan") from e
try:
async def run_orchestration_task():
try:
await OrchestrationManager().run_orchestration(user_id, input_task, plan_id=plan_id)
except Exception as orch_error:
logger.error("Background orchestration failed for plan '%s': %s", plan_id, orch_error)
track_event_if_configured(
"Error_Orchestration_Failed",
{
"plan_id": plan_id,
"session_id": input_task.session_id,
"user_id": user_id,
"error": str(orch_error),
"error_type": type(orch_error).__name__,
},
)
background_tasks.add_task(run_orchestration_task)
return {
"status": "Request started successfully",
"session_id": input_task.session_id,
"plan_id": plan_id,
}
except Exception as e:
track_event_if_configured(
"Error_Request_Start_Failed",
{
"session_id": input_task.session_id,
"description": input_task.description,
"error": str(e),
},
)
raise HTTPException(
status_code=400, detail=f"Error starting request: {e}"
) from e
@app_v4.post("/plan_approval")
async def plan_approval(
human_feedback: messages.PlanApprovalResponse, request: Request
):
"""
Endpoint to receive plan approval or rejection from the user.
---
tags:
- Plans
parameters:
- name: user_principal_id
in: header
type: string
required: true
description: User ID extracted from the authentication header
requestBody:
description: Plan approval payload
required: true
content:
application/json:
schema:
type: object
properties:
m_plan_id:
type: string
description: The internal m_plan id for the plan (required)
approved:
type: boolean
description: Whether the plan is approved (true) or rejected (false)
feedback:
type: string
description: Optional feedback or comment from the user
plan_id:
type: string
description: Optional user-facing plan_id
responses:
200:
description: Approval recorded successfully
content:
application/json:
schema:
type: object
properties:
status:
type: string
401:
description: Missing or invalid user information
404:
description: No active plan found for approval
500:
description: Internal server error
"""
authenticated_user = get_authenticated_user_details(request_headers=request.headers)
user_id = authenticated_user["user_principal_id"]
if not user_id:
raise HTTPException(
status_code=401, detail="Missing or invalid user information"
)
# Attach session_id to span if plan_id is available and capture for events
session_id = None
if human_feedback.plan_id:
try:
memory_store = await DatabaseFactory.get_database(user_id=user_id)
plan = await memory_store.get_plan_by_plan_id(plan_id=human_feedback.plan_id)
if plan and plan.session_id:
session_id = plan.session_id
span = trace.get_current_span()
if span:
span.set_attribute("session_id", session_id)
except Exception:
pass # Don't fail request if span attribute fails
# Set the approval in the orchestration config
try:
if user_id and human_feedback.m_plan_id:
if (
orchestration_config
and human_feedback.m_plan_id in orchestration_config.approvals
):
orchestration_config.set_approval_result(
human_feedback.m_plan_id, human_feedback.approved
)
print("Plan approval received:", human_feedback)
try:
result = await PlanService.handle_plan_approval(
human_feedback, user_id
)
print("Plan approval processed:", result)
except ValueError as ve:
logger.error(f"ValueError processing plan approval: {ve}")
await connection_config.send_status_update_async(
{
"type": WebsocketMessageType.ERROR_MESSAGE,
"data": {
"content": "Approval failed due to invalid input.",
"status": "error",
"timestamp": asyncio.get_event_loop().time(),
},
},
user_id,
message_type=WebsocketMessageType.ERROR_MESSAGE,
)
except Exception:
logger.error("Error processing plan approval", exc_info=True)
await connection_config.send_status_update_async(
{
"type": WebsocketMessageType.ERROR_MESSAGE,
"data": {
"content": "An unexpected error occurred while processing the approval.",
"status": "error",
"timestamp": asyncio.get_event_loop().time(),
},
},
user_id,
message_type=WebsocketMessageType.ERROR_MESSAGE,
)
# Use dynamic event name based on approval status
approval_status = "Approved" if human_feedback.approved else "Rejected"
event_name = f"Plan_{approval_status}"
event_props = {
"plan_id": human_feedback.plan_id,
"m_plan_id": human_feedback.m_plan_id,
"approved": human_feedback.approved,
"user_id": user_id,
"feedback": human_feedback.feedback,
}
if session_id:
event_props["session_id"] = session_id
track_event_if_configured(event_name, event_props)
return {"status": "approval recorded"}
else:
logging.warning(
"No orchestration or plan found for plan_id: %s",
human_feedback.m_plan_id
)
raise HTTPException(
status_code=404, detail="No active plan found for approval"
)
except Exception as e:
logging.error(f"Error processing plan approval: {e}")
try:
await connection_config.send_status_update_async(
{
"type": WebsocketMessageType.ERROR_MESSAGE,
"data": {
"content": "An error occurred while processing your approval request.",
"status": "error",
"timestamp": asyncio.get_event_loop().time(),
},
},
user_id,
message_type=WebsocketMessageType.ERROR_MESSAGE,
)
except Exception as ws_error:
# Don't let WebSocket send failure break the HTTP response
logging.warning(f"Failed to send WebSocket error: {ws_error}")
raise HTTPException(status_code=500, detail="Internal server error")
return None
@app_v4.post("/user_clarification")
async def user_clarification(
human_feedback: messages.UserClarificationResponse, request: Request
):
"""
Endpoint to receive user clarification responses for clarification requests sent by the system.
---
tags:
- Plans
parameters:
- name: user_principal_id
in: header
type: string
required: true
description: User ID extracted from the authentication header
requestBody:
description: User clarification payload
required: true
content:
application/json:
schema:
type: object
properties:
request_id:
type: string
description: The clarification request id sent by the system (required)
answer:
type: string
description: The user's answer or clarification text
plan_id:
type: string
description: (Optional) Associated plan_id
m_plan_id:
type: string
description: (Optional) Internal m_plan id
responses:
200:
description: Clarification recorded successfully
400:
description: RAI check failed or invalid input
401:
description: Missing or invalid user information
404:
description: No active plan found for clarification
500:
description: Internal server error
"""
authenticated_user = get_authenticated_user_details(request_headers=request.headers)
user_id = authenticated_user["user_principal_id"]
if not user_id:
raise HTTPException(
status_code=401, detail="Missing or invalid user information"
)
# Attach session_id to span if plan_id is available and capture for events
session_id = None
try:
memory_store = await DatabaseFactory.get_database(user_id=user_id)
if human_feedback.plan_id:
try:
plan = await memory_store.get_plan_by_plan_id(plan_id=human_feedback.plan_id)
if plan and plan.session_id:
session_id = plan.session_id
span = trace.get_current_span()
if span:
span.set_attribute("session_id", session_id)
except Exception:
pass # Don't fail request if span attribute fails
user_current_team = await memory_store.get_current_team(user_id=user_id)
team_id = None
if user_current_team:
team_id = user_current_team.team_id
team = await memory_store.get_team_by_id(team_id=team_id)
if not team:
raise HTTPException(
status_code=404,
detail=f"Team configuration '{team_id}' not found or access denied",
)
except Exception as e:
raise HTTPException(
status_code=400,
detail=f"Error retrieving team configuration: {e}",
) from e
# Set the approval in the orchestration config
if user_id and human_feedback.request_id:
# validate rai
if human_feedback.answer is not None and str(human_feedback.answer).strip() != "":
if not await rai_success(human_feedback.answer, team, memory_store):
event_props = {
"status": "Plan Clarification ",
"description": human_feedback.answer,
"request_id": human_feedback.request_id,
}
if session_id:
event_props["session_id"] = session_id
track_event_if_configured("Error_RAI_Check_Failed", event_props)
raise HTTPException(
status_code=400,
detail={
"error_type": "RAI_VALIDATION_FAILED",
"message": "Content Safety Check Failed",
"description": "Your request contains content that doesn't meet our safety guidelines. Please modify your request to ensure it's appropriate and try again.",
"suggestions": [
"Remove any potentially harmful, inappropriate, or unsafe content",
"Use more professional and constructive language",
"Focus on legitimate business or educational objectives",
"Ensure your request complies with content policies",
],
"user_action": "Please revise your request and try again",
},
)
if (
orchestration_config
and human_feedback.request_id in orchestration_config.clarifications
):
# Use the new event-driven method to set clarification result
orchestration_config.set_clarification_result(
human_feedback.request_id, human_feedback.answer
)
try:
result = await PlanService.handle_human_clarification(
human_feedback, user_id
)
print("Human clarification processed:", result)
except ValueError as ve:
print(f"ValueError processing human clarification: {ve}")
except Exception as e:
print(f"Error processing human clarification: {e}")
event_props = {
"request_id": human_feedback.request_id,
"answer": human_feedback.answer,
"user_id": user_id,
}
if session_id:
event_props["session_id"] = session_id
track_event_if_configured("Human_Clarification_Received", event_props)
return {
"status": "clarification recorded",
}
else:
logging.warning(
f"No orchestration or plan found for request_id: {human_feedback.request_id}"
)
raise HTTPException(
status_code=404, detail="No active plan found for clarification"
)
return None
@app_v4.post("/agent_message")
async def agent_message_user(
agent_message: messages.AgentMessageResponse, request: Request
):
"""
Endpoint to receive messages from agents (agent -> user communication).
---
tags:
- Agents
parameters:
- name: user_principal_id
in: header
type: string
required: true
description: User ID extracted from the authentication header
requestBody:
description: Agent message payload
required: true
content:
application/json:
schema:
type: object
properties:
plan_id:
type: string
description: ID of the plan this message relates to
agent:
type: string
description: Name or identifier of the agent sending the message
content:
type: string
description: The message content
agent_type:
type: string
description: Type of agent (AI/Human)
m_plan_id:
type: string
description: Optional internal m_plan id
responses:
200:
description: Message recorded successfully
schema:
type: object
properties:
status:
type: string
401:
description: Missing or invalid user information
"""
authenticated_user = get_authenticated_user_details(request_headers=request.headers)
user_id = authenticated_user["user_principal_id"]
if not user_id:
raise HTTPException(
status_code=401, detail="Missing or invalid user information"
)
# Attach session_id to span if plan_id is available and capture for events
session_id = None
if agent_message.plan_id:
try:
memory_store = await DatabaseFactory.get_database(user_id=user_id)
plan = await memory_store.get_plan_by_plan_id(plan_id=agent_message.plan_id)
if plan and plan.session_id:
session_id = plan.session_id
span = trace.get_current_span()
if span:
span.set_attribute("session_id", session_id)
except Exception:
pass # Don't fail request if span attribute fails
# Set the approval in the orchestration config
try:
result = await PlanService.handle_agent_messages(agent_message, user_id)
print("Agent message processed:", result)
except ValueError as ve:
print(f"ValueError processing agent message: {ve}")
except Exception as e:
print(f"Error processing agent message: {e}")
# Use dynamic event name with agent identifier
event_name = f"Agent_Message_From_{agent_message.agent.replace(' ', '_')}"
event_props = {
"agent": agent_message.agent,
"content": agent_message.content,
"user_id": user_id,
}
if session_id:
event_props["session_id"] = session_id
track_event_if_configured(event_name, event_props)
return {
"status": "message recorded",
}
@app_v4.post("/upload_team_config")
async def upload_team_config(
request: Request,
file: UploadFile = File(...),
team_id: Optional[str] = Query(None),
):
"""
Upload and save a team configuration JSON file.
---
tags:
- Team Configuration
parameters:
- name: user_principal_id
in: header
type: string
required: true
description: User ID extracted from the authentication header
- name: file
in: formData
type: file
required: true
description: JSON file containing team configuration
responses:
200:
description: Team configuration uploaded successfully
400:
description: Invalid request or file format
401:
description: Missing or invalid user information
500:
description: Internal server error
"""
# Validate user authentication
authenticated_user = get_authenticated_user_details(request_headers=request.headers)
user_id = authenticated_user["user_principal_id"]
if not user_id:
track_event_if_configured(
"Error_User_Not_Found", {"status_code": 400, "detail": "no user"}
)
raise HTTPException(status_code=400, detail="no user found")
try:
memory_store = await DatabaseFactory.get_database(user_id=user_id)
except Exception as e:
raise HTTPException(
status_code=400,
detail=f"Error retrieving team configuration: {e}",
) from e
# Validate file is provided and is JSON
if not file:
raise HTTPException(status_code=400, detail="No file provided")
if not file.filename.endswith(".json"):
raise HTTPException(status_code=400, detail="File must be a JSON file")
try:
# Read and parse JSON content
content = await file.read()
try:
json_data = json.loads(content.decode("utf-8"))
except json.JSONDecodeError as e:
raise HTTPException(
status_code=400, detail=f"Invalid JSON format: {str(e)}"
) from e
# Validate content with RAI before processing
if not team_id:
rai_valid, rai_error = await rai_validate_team_config(json_data, memory_store)
if not rai_valid:
track_event_if_configured(
"Error_Config_RAI_Validation_Failed",
{
"status": "failed",
"user_id": user_id,
"filename": file.filename,
"reason": rai_error,
},
)
raise HTTPException(status_code=400, detail=rai_error)
track_event_if_configured(
"Config_RAI_Validation_Passed",
{"status": "passed", "user_id": user_id, "filename": file.filename},
)
team_service = TeamService(memory_store)
# Validate model deployments
models_valid, missing_models = await team_service.validate_team_models(
json_data
)
if not models_valid:
error_message = (
f"The following required models are not deployed in your Azure AI project: {', '.join(missing_models)}. "
f"Please deploy these models in Azure AI Foundry before uploading this team configuration."
)
track_event_if_configured(
"Error_Config_Model_Validation_Failed",
{
"status": "failed",
"user_id": user_id,
"filename": file.filename,
"missing_models": missing_models,
},
)
raise HTTPException(status_code=400, detail=error_message)
track_event_if_configured(
"Config_Model_Validation_Passed",
{"status": "passed", "user_id": user_id, "filename": file.filename},
)
# Validate search indexes
logger.info(f"🔍 Validating search indexes for user: {user_id}")
search_valid, search_errors = await team_service.validate_team_search_indexes(
json_data
)
if not search_valid:
logger.warning(f"❌ Search validation failed for user {user_id}: {search_errors}")
error_message = (
f"Search index validation failed:\n\n{chr(10).join([f'• {error}' for error in search_errors])}\n\n"
f"Please ensure all referenced search indexes exist in your Azure AI Search service."
)
track_event_if_configured(
"Error_Config_Search_Validation_Failed",
{
"status": "failed",
"user_id": user_id,
"filename": file.filename,
"search_errors": search_errors,
},
)
raise HTTPException(status_code=400, detail=error_message)
logger.info(f"✅ Search validation passed for user: {user_id}")
track_event_if_configured(
"Config_Search_Validation_Passed",
{"status": "passed", "user_id": user_id, "filename": file.filename},
)
# Validate and parse the team configuration
try:
team_config = await team_service.validate_and_parse_team_config(
json_data, user_id
)
except ValueError as e:
raise HTTPException(status_code=400, detail=str(e)) from e
# Save the configuration
try:
print("Saving team configuration...", team_id)
if team_id:
team_config.team_id = team_id
team_config.id = team_id # Ensure id is also set for updates
team_id = await team_service.save_team_configuration(team_config)
except ValueError as e:
raise HTTPException(
status_code=500, detail=f"Failed to save configuration: {str(e)}"
) from e
track_event_if_configured(
"Config_Team_Uploaded",
{
"status": "success",
"team_id": team_id,
"user_id": user_id,
"agents_count": len(team_config.agents),
"tasks_count": len(team_config.starting_tasks),
},
)