Skip to content

Commit eadcf5d

Browse files
committed
running in terminal with background task - added temp scenario examples
1 parent f34ea95 commit eadcf5d

File tree

19 files changed

+738
-1156
lines changed

19 files changed

+738
-1156
lines changed

data/agent_teams/hr.json

Lines changed: 89 additions & 0 deletions
Large diffs are not rendered by default.

data/agent_teams/marketing.json

Lines changed: 89 additions & 0 deletions
Large diffs are not rendered by default.

data/agent_teams/oil&gas.json

Lines changed: 89 additions & 0 deletions
Large diffs are not rendered by default.

data/agent_teams/retail.json

Lines changed: 89 additions & 0 deletions
Large diffs are not rendered by default.

data/agent_teams/sample.json

Lines changed: 89 additions & 0 deletions
Large diffs are not rendered by default.

src/backend/app_kernel.py

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,7 @@
2828
from middleware.health_check import HealthCheckMiddleware
2929
from v3.api.router import app_v3
3030
# Semantic Kernel imports
31-
from v3.config.settings import orchestration_config
32-
from v3.magentic_agents.magentic_agent_factory import (cleanup_all_agents,
33-
get_agents)
31+
from v3.orchestration.orchestration_manager import OrchestrationManager
3432

3533
# Check if the Application Insights Instrumentation Key is set in the environment variables
3634
connection_string = config.APPLICATIONINSIGHTS_CONNECTION_STRING
@@ -60,14 +58,6 @@
6058
logging.WARNING
6159
)
6260

63-
64-
# @asynccontextmanager
65-
# async def lifespan(app: FastAPI):
66-
# """Lifespan event handler to create and clean up agents."""
67-
# config.agents = await get_agents()
68-
# yield
69-
# await cleanup_all_agents()
70-
7161
# Initialize the FastAPI app
7262
app = FastAPI()
7363

@@ -671,7 +661,7 @@ async def get_plans(
671661
raise HTTPException(status_code=400, detail="no user")
672662

673663
# Initialize agent team for this user session
674-
await orchestration_config.get_current_orchestration(user_id=user_id)
664+
await OrchestrationManager.get_current_orchestration(user_id=user_id)
675665

676666
# Initialize memory context
677667
memory_store = await DatabaseFactory.get_database(user_id=user_id)

src/backend/common/config/app_config.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ def __init__(self):
7171
# Example: http://127.0.0.1:8000/mcp
7272
self.MCP_SERVER_ENDPOINT = self._get_optional("MCP_SERVER_ENDPOINT")
7373

74+
test_team_json = self._get_optional("TEST_TEAM_JSON")
75+
76+
self.AGENT_TEAM_FILE = f"../../data/agent_teams/{test_team_json}.json"
77+
7478
# Cached clients and resources
7579
self._azure_credentials = None
7680
self._cosmos_client = None

src/backend/v3/api/router.py

Lines changed: 9 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,13 @@
99
Plan, PlanStatus)
1010
from common.utils.event_utils import track_event_if_configured
1111
from common.utils.utils_kernel import rai_success, rai_validate_team_config
12-
from fastapi import (APIRouter, Depends, File, HTTPException, Request,
13-
UploadFile)
12+
from fastapi import (APIRouter, BackgroundTasks, Depends, FastAPI, File,
13+
HTTPException, Request, UploadFile, WebSocket,
14+
WebSocketDisconnect)
1415
from kernel_agents.agent_factory import AgentFactory
1516
from semantic_kernel.agents.runtime import InProcessRuntime
1617
from v3.common.services.team_service import TeamService
17-
from v3.config.settings import orchestration_config
18-
from v3.models.models import MPlan, MStep
19-
from v3.models.orchestration_models import AgentType
18+
from v3.orchestration.orchestration_manager import OrchestrationManager
2019

2120
app_v3 = APIRouter(
2221
prefix="/api/v3",
@@ -25,7 +24,7 @@
2524

2625
# To do: change endpoint to process request
2726
@app_v3.post("/create_plan")
28-
async def process_request(input_task: InputTask, request: Request):
27+
async def process_request(background_tasks: BackgroundTasks, input_task: InputTask, request: Request):
2928
"""
3029
Create a new plan without full processing.
3130
@@ -121,59 +120,23 @@ async def process_request(input_task: InputTask, request: Request):
121120
input_task.session_id = str(uuid.uuid4())
122121

123122
try:
124-
# Initialize memory store
125-
memory_store = await DatabaseFactory.get_database(user_id=user_id)
126-
127-
# Create a new Plan object
128-
plan = MPlan()
129-
# session_id=input_task.session_id,
130-
# team_id=input_task.team_id,
131-
# user_id=user_id,
132-
# initial_goal=input_task.description,
133-
# overall_status=PlanStatus.in_progress,
134-
# source=AgentType.PLANNER.value,
135-
# )
136-
137-
# setup and call the magentic orchestration
138-
magentic_orchestration = await orchestration_config.get_current_orchestration(user_id)
139-
140-
runtime = InProcessRuntime()
141-
runtime.start()
142-
143-
# invoke returns immediately, wait on result.get
144-
orchestration_result = await magentic_orchestration.invoke(task=input_task.description,runtime=runtime)
145-
team_result = await orchestration_result.get()
146-
147-
# Save the plan to the database
148-
await memory_store.add_plan(plan)
149-
150-
# Log successful plan creation
151-
track_event_if_configured(
152-
"PlanCreated",
153-
{
154-
"status": f"Plan created with ID: {plan.id}",
155-
"session_id": input_task.session_id,
156-
"plan_id": plan.id,
157-
"description": input_task.description,
158-
},
159-
)
123+
background_tasks.add_task(OrchestrationManager.run_orchestration, user_id, input_task)
160124

161125
return {
162-
"plan_id": plan.id,
163-
"status": "Plan created successfully",
126+
"status": "Request started successfully",
164127
"session_id": input_task.session_id,
165128
}
166129

167130
except Exception as e:
168131
track_event_if_configured(
169-
"CreatePlanError",
132+
"RequestStartFailed",
170133
{
171134
"session_id": input_task.session_id,
172135
"description": input_task.description,
173136
"error": str(e),
174137
},
175138
)
176-
raise HTTPException(status_code=400, detail=f"Error creating plan: {e}") from e
139+
raise HTTPException(status_code=400, detail=f"Error starting request: {e}") from e
177140

178141

179142
@app_v3.post("/upload_team_config")

src/backend/v3/config/settings.py

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,10 @@
33
Handles Azure OpenAI, MCP, and environment setup.
44
"""
55

6-
import os
7-
86
from common.config.app_config import config
97
from semantic_kernel.agents.orchestration.magentic import MagenticOrchestration
108
from semantic_kernel.connectors.ai.open_ai import (
119
AzureChatCompletion, OpenAIChatPromptExecutionSettings)
12-
from v3.magentic_agents.magentic_agent_factory import (cleanup_all_agents,
13-
get_agents)
14-
from v3.orchestration.manager import init_orchestration
1510

1611

1712
class AzureConfig:
@@ -64,13 +59,13 @@ class OrchestrationConfig:
6459

6560
def __init__(self):
6661
self._orchestrations = {}
62+
self.plans = {} # job_id -> current plan
63+
self.approvals = {} # job_id -> True/False/None
64+
self.sockets = {} # job_id -> WebSocket
6765

68-
async def get_current_orchestration(self, user_id: str) -> MagenticOrchestration:
69-
"""Initialize or get existing orchestration instance."""
70-
if user_id not in self._orchestrations:
71-
agents = await get_agents()
72-
self._orchestrations[user_id] = await init_orchestration(agents)
73-
return self._orchestrations[user_id]
66+
def get_current_orchestration(self, user_id: str) -> MagenticOrchestration:
67+
"""get existing orchestration instance."""
68+
return self._orchestrations.get(user_id, None)
7469

7570

7671
# Global config instances

0 commit comments

Comments
 (0)