|
2 | 2 | import asyncio |
3 | 3 | import logging |
4 | 4 | import os |
| 5 | + |
5 | 6 | # Azure monitoring |
6 | 7 | import re |
7 | 8 | import uuid |
|
12 | 13 | from azure.monitor.opentelemetry import configure_azure_monitor |
13 | 14 | from common.config.app_config import config |
14 | 15 | from common.database.database_factory import DatabaseFactory |
15 | | -from common.models.messages_kernel import (AgentMessage, AgentType, |
16 | | - HumanClarification, HumanFeedback, |
17 | | - InputTask, Plan, PlanStatus, |
18 | | - PlanWithSteps, Step, UserLanguage) |
| 16 | +from common.models.messages_kernel import ( |
| 17 | + AgentMessage, |
| 18 | + AgentType, |
| 19 | + HumanClarification, |
| 20 | + HumanFeedback, |
| 21 | + InputTask, |
| 22 | + Plan, |
| 23 | + PlanStatus, |
| 24 | + PlanWithSteps, |
| 25 | + Step, |
| 26 | + UserLanguage, |
| 27 | +) |
19 | 28 | from common.utils.event_utils import track_event_if_configured |
20 | 29 | from common.utils.utils_date import format_dates_in_messages |
| 30 | + |
21 | 31 | # Updated import for KernelArguments |
22 | 32 | from common.utils.utils_kernel import rai_success |
| 33 | + |
23 | 34 | # FastAPI imports |
24 | 35 | from fastapi import FastAPI, HTTPException, Query, Request, WebSocket |
25 | 36 | from fastapi.middleware.cors import CORSMiddleware |
26 | 37 | from kernel_agents.agent_factory import AgentFactory |
| 38 | + |
27 | 39 | # Local imports |
28 | 40 | from middleware.health_check import HealthCheckMiddleware |
29 | 41 | from v3.api.router import app_v3 |
30 | 42 | from common.utils.websocket_streaming import websocket_streaming_endpoint, ws_manager |
| 43 | + |
31 | 44 | # Semantic Kernel imports |
32 | | -from v3.config.settings import orchestration_config |
33 | | -from v3.magentic_agents.magentic_agent_factory import (cleanup_all_agents, |
34 | | - get_agents) |
| 45 | +# from v3.config.settings import orchestration_config |
| 46 | +# from v3.magentic_agents.magentic_agent_factory import cleanup_all_agents, get_agents |
35 | 47 |
|
36 | 48 | # Check if the Application Insights Instrumentation Key is set in the environment variables |
37 | 49 | connection_string = config.APPLICATIONINSIGHTS_CONNECTION_STRING |
|
91 | 103 | app.include_router(app_v3) |
92 | 104 | logging.info("Added health check middleware") |
93 | 105 |
|
| 106 | + |
94 | 107 | # WebSocket streaming endpoint |
95 | 108 | @app.websocket("/ws/streaming") |
96 | 109 | async def websocket_endpoint(websocket: WebSocket): |
@@ -604,6 +617,7 @@ async def approve_step_endpoint( |
604 | 617 |
|
605 | 618 | return {"status": "All steps approved"} |
606 | 619 |
|
| 620 | + |
607 | 621 | # Get plans is called in the initial side rendering of the frontend |
608 | 622 | @app.get("/api/plans") |
609 | 623 | async def get_plans( |
@@ -677,7 +691,7 @@ async def get_plans( |
677 | 691 | "UserIdNotFound", {"status_code": 400, "detail": "no user"} |
678 | 692 | ) |
679 | 693 | raise HTTPException(status_code=400, detail="no user") |
680 | | - |
| 694 | + |
681 | 695 | # Initialize agent team for this user session |
682 | 696 | await orchestration_config.get_current_orchestration(user_id=user_id) |
683 | 697 |
|
@@ -906,66 +920,73 @@ async def test_streaming_updates(plan_id: str): |
906 | 920 | Test endpoint to simulate streaming updates for a plan. |
907 | 921 | This is for testing the WebSocket streaming functionality. |
908 | 922 | """ |
909 | | - from common.utils.websocket_streaming import send_plan_update, send_agent_message, send_step_update |
910 | | - |
| 923 | + from common.utils.websocket_streaming import ( |
| 924 | + send_plan_update, |
| 925 | + send_agent_message, |
| 926 | + send_step_update, |
| 927 | + ) |
| 928 | + |
911 | 929 | try: |
912 | 930 | # Simulate a series of streaming updates |
913 | 931 | await send_agent_message( |
914 | 932 | plan_id=plan_id, |
915 | 933 | agent_name="Data Analyst", |
916 | 934 | content="Starting analysis of the data...", |
917 | | - message_type="thinking" |
| 935 | + message_type="thinking", |
918 | 936 | ) |
919 | | - |
| 937 | + |
920 | 938 | await asyncio.sleep(1) |
921 | | - |
| 939 | + |
922 | 940 | await send_plan_update( |
923 | 941 | plan_id=plan_id, |
924 | 942 | step_id="step_1", |
925 | 943 | agent_name="Data Analyst", |
926 | 944 | content="Analyzing customer data patterns...", |
927 | 945 | status="in_progress", |
928 | | - message_type="action" |
| 946 | + message_type="action", |
929 | 947 | ) |
930 | | - |
| 948 | + |
931 | 949 | await asyncio.sleep(2) |
932 | | - |
| 950 | + |
933 | 951 | await send_agent_message( |
934 | 952 | plan_id=plan_id, |
935 | | - agent_name="Data Analyst", |
| 953 | + agent_name="Data Analyst", |
936 | 954 | content="Found 3 key insights in the customer data. Processing recommendations...", |
937 | | - message_type="result" |
| 955 | + message_type="result", |
938 | 956 | ) |
939 | | - |
| 957 | + |
940 | 958 | await asyncio.sleep(1) |
941 | | - |
| 959 | + |
942 | 960 | await send_step_update( |
943 | 961 | plan_id=plan_id, |
944 | 962 | step_id="step_1", |
945 | 963 | status="completed", |
946 | | - content="Data analysis completed successfully!" |
| 964 | + content="Data analysis completed successfully!", |
947 | 965 | ) |
948 | | - |
| 966 | + |
949 | 967 | await send_agent_message( |
950 | 968 | plan_id=plan_id, |
951 | 969 | agent_name="Business Advisor", |
952 | 970 | content="Reviewing the analysis results and preparing strategic recommendations...", |
953 | | - message_type="thinking" |
| 971 | + message_type="thinking", |
954 | 972 | ) |
955 | | - |
| 973 | + |
956 | 974 | await asyncio.sleep(2) |
957 | | - |
| 975 | + |
958 | 976 | await send_plan_update( |
959 | 977 | plan_id=plan_id, |
960 | | - step_id="step_2", |
| 978 | + step_id="step_2", |
961 | 979 | agent_name="Business Advisor", |
962 | 980 | content="Based on the data analysis, I recommend focusing on customer retention strategies for the identified high-value segments.", |
963 | 981 | status="completed", |
964 | | - message_type="result" |
| 982 | + message_type="result", |
965 | 983 | ) |
966 | | - |
967 | | - return {"status": "success", "message": f"Test streaming updates sent for plan {plan_id}"} |
968 | | - |
| 984 | + |
| 985 | + return { |
| 986 | + "status": "success", |
| 987 | + "message": f"Test streaming updates sent for plan {plan_id}", |
| 988 | + } |
| 989 | + |
969 | 990 | except Exception as e: |
970 | 991 | logging.error(f"Error sending test streaming updates: {e}") |
971 | 992 | raise HTTPException(status_code=500, detail=str(e)) |
|
0 commit comments