Skip to content

Commit 4ee39c0

Browse files
committed
Refactor database and model usage; remove unused files
Replaced usage of deprecated database models with direct references to message models in CosmosDB implementation. Refactored app_kernel.py to use DatabaseFactory for memory store initialization, removing dependency on initialize_runtime_and_context. Deleted obsolete files: CustomizeSolution.md, database_models.py, and several __init__.py and handler files. Cleaned up and streamlined CosmosDBClient methods for session, plan, step, and team configuration management.
1 parent 0de96db commit 4ee39c0

28 files changed

+460
-1994
lines changed

docs/CustomizeSolution.md

Lines changed: 0 additions & 617 deletions
This file was deleted.

src/backend/app_kernel.py

Lines changed: 8 additions & 183 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@
3737

3838
# Updated import for KernelArguments
3939
from common.utils.utils_kernel import (
40-
initialize_runtime_and_context,
4140
rai_success,
4241
)
4342

43+
from common.database.database_factory import DatabaseFactory
4444
from v3.api.router import app_v3
4545

4646
# Check if the Application Insights Instrumentation Key is set in the environment variables
@@ -223,9 +223,7 @@ async def input_task_endpoint(input_task: InputTask, request: Request):
223223
try:
224224
# Create all agents instead of just the planner agent
225225
# This ensures other agents are created first and the planner has access to them
226-
kernel, memory_store = await initialize_runtime_and_context(
227-
input_task.session_id, user_id
228-
)
226+
memory_store = await DatabaseFactory.get_database(user_id=user_id)
229227
client = None
230228
try:
231229
client = config.get_ai_project_client()
@@ -368,9 +366,7 @@ async def human_feedback_endpoint(human_feedback: HumanFeedback, request: Reques
368366
)
369367
raise HTTPException(status_code=400, detail="no user")
370368

371-
kernel, memory_store = await initialize_runtime_and_context(
372-
human_feedback.session_id, user_id
373-
)
369+
memory_store = await DatabaseFactory.get_database(user_id=user_id)
374370

375371
client = None
376372
try:
@@ -501,9 +497,7 @@ async def human_clarification_endpoint(
501497
)
502498
raise HTTPException(status_code=400, detail="no user")
503499

504-
kernel, memory_store = await initialize_runtime_and_context(
505-
human_clarification.session_id, user_id
506-
)
500+
memory_store = await DatabaseFactory.get_database(user_id=user_id)
507501
client = None
508502
try:
509503
client = config.get_ai_project_client()
@@ -615,9 +609,7 @@ async def approve_step_endpoint(
615609
raise HTTPException(status_code=400, detail="no user")
616610

617611
# Get the agents for this session
618-
kernel, memory_store = await initialize_runtime_and_context(
619-
human_feedback.session_id, user_id
620-
)
612+
memory_store = await DatabaseFactory.get_database(user_id=user_id)
621613
client = None
622614
try:
623615
client = config.get_ai_project_client()
@@ -733,10 +725,7 @@ async def get_plans(
733725
raise HTTPException(status_code=400, detail="no user")
734726

735727
# Initialize memory context
736-
kernel, memory_store = await initialize_runtime_and_context(
737-
session_id or "", user_id
738-
)
739-
728+
memory_store = await DatabaseFactory.get_database(user_id=user_id)
740729
if session_id:
741730
plan = await memory_store.get_plan_by_session(session_id=session_id)
742731
if not plan:
@@ -851,7 +840,7 @@ async def get_steps_by_plan(plan_id: str, request: Request) -> List[Step]:
851840
raise HTTPException(status_code=400, detail="no user")
852841

853842
# Initialize memory context
854-
kernel, memory_store = await initialize_runtime_and_context("", user_id)
843+
memory_store = await DatabaseFactory.get_database(user_id=user_id)
855844
steps = await memory_store.get_steps_for_plan(plan_id=plan_id)
856845
return steps
857846

@@ -917,175 +906,11 @@ async def get_agent_messages(session_id: str, request: Request) -> List[AgentMes
917906
raise HTTPException(status_code=400, detail="no user")
918907

919908
# Initialize memory context
920-
kernel, memory_store = await initialize_runtime_and_context(
921-
session_id or "", user_id
922-
)
909+
memory_store = await DatabaseFactory.get_database(user_id=user_id)
923910
agent_messages = await memory_store.get_data_by_type("agent_message")
924911
return agent_messages
925912

926913

927-
@app.get("/api/agent_messages_by_plan/{plan_id}", response_model=List[AgentMessage])
928-
async def get_agent_messages_by_plan(
929-
plan_id: str, request: Request
930-
) -> List[AgentMessage]:
931-
"""
932-
Retrieve agent messages for a specific session.
933-
934-
---
935-
tags:
936-
- Agent Messages
937-
parameters:
938-
- name: session_id
939-
in: path
940-
type: string
941-
required: true
942-
in: path
943-
type: string
944-
required: true
945-
description: The ID of the session to retrieve agent messages for
946-
responses:
947-
200:
948-
description: List of agent messages associated with the specified session
949-
schema:
950-
type: array
951-
items:
952-
type: object
953-
properties:
954-
id:
955-
type: string
956-
description: Unique ID of the agent message
957-
session_id:
958-
type: string
959-
description: Session ID associated with the message
960-
plan_id:
961-
type: string
962-
description: Plan ID related to the agent message
963-
content:
964-
type: string
965-
description: Content of the message
966-
source:
967-
type: string
968-
description: Source of the message (e.g., agent type)
969-
timestamp:
970-
type: string
971-
format: date-time
972-
description: Timestamp of the message
973-
step_id:
974-
type: string
975-
description: Optional step ID associated with the message
976-
400:
977-
description: Missing or invalid user information
978-
404:
979-
description: Agent messages not found
980-
"""
981-
authenticated_user = get_authenticated_user_details(request_headers=request.headers)
982-
user_id = authenticated_user["user_principal_id"]
983-
if not user_id:
984-
track_event_if_configured(
985-
"UserIdNotFound", {"status_code": 400, "detail": "no user"}
986-
)
987-
raise HTTPException(status_code=400, detail="no user")
988-
989-
# Initialize memory context
990-
kernel, memory_store = await initialize_runtime_and_context("", user_id)
991-
agent_messages = await memory_store.get_data_by_type_and_plan_id("agent_message")
992-
return agent_messages
993-
994-
995-
@app.delete("/api/messages")
996-
async def delete_all_messages(request: Request) -> Dict[str, str]:
997-
"""
998-
Delete all messages across sessions.
999-
1000-
---
1001-
tags:
1002-
- Messages
1003-
responses:
1004-
200:
1005-
description: Confirmation of deletion
1006-
schema:
1007-
type: object
1008-
properties:
1009-
status:
1010-
type: string
1011-
description: Status message indicating all messages were deleted
1012-
400:
1013-
description: Missing or invalid user information
1014-
"""
1015-
authenticated_user = get_authenticated_user_details(request_headers=request.headers)
1016-
user_id = authenticated_user["user_principal_id"]
1017-
if not user_id:
1018-
track_event_if_configured(
1019-
"UserIdNotFound", {"status_code": 400, "detail": "no user"}
1020-
)
1021-
raise HTTPException(status_code=400, detail="no user")
1022-
1023-
# Initialize memory context
1024-
kernel, memory_store = await initialize_runtime_and_context("", user_id)
1025-
1026-
await memory_store.delete_all_items("plan")
1027-
await memory_store.delete_all_items("session")
1028-
await memory_store.delete_all_items("step")
1029-
await memory_store.delete_all_items("agent_message")
1030-
1031-
# Clear the agent factory cache
1032-
AgentFactory.clear_cache()
1033-
1034-
return {"status": "All messages deleted"}
1035-
1036-
1037-
@app.get("/api/messages")
1038-
async def get_all_messages(request: Request):
1039-
"""
1040-
Retrieve all messages across sessions.
1041-
1042-
---
1043-
tags:
1044-
- Messages
1045-
responses:
1046-
200:
1047-
description: List of all messages across sessions
1048-
schema:
1049-
type: array
1050-
items:
1051-
type: object
1052-
properties:
1053-
id:
1054-
type: string
1055-
description: Unique ID of the message
1056-
data_type:
1057-
type: string
1058-
description: Type of the message (e.g., session, step, plan, agent_message)
1059-
session_id:
1060-
type: string
1061-
description: Session ID associated with the message
1062-
user_id:
1063-
type: string
1064-
description: User ID associated with the message
1065-
content:
1066-
type: string
1067-
description: Content of the message
1068-
timestamp:
1069-
type: string
1070-
format: date-time
1071-
description: Timestamp of the message
1072-
400:
1073-
description: Missing or invalid user information
1074-
"""
1075-
authenticated_user = get_authenticated_user_details(request_headers=request.headers)
1076-
user_id = authenticated_user["user_principal_id"]
1077-
if not user_id:
1078-
track_event_if_configured(
1079-
"UserIdNotFound", {"status_code": 400, "detail": "no user"}
1080-
)
1081-
raise HTTPException(status_code=400, detail="no user")
1082-
1083-
# Initialize memory context
1084-
kernel, memory_store = await initialize_runtime_and_context("", user_id)
1085-
message_list = await memory_store.get_all_items()
1086-
return message_list
1087-
1088-
1089914
@app.get("/api/agent-tools")
1090915
async def get_agent_tools():
1091916
"""

src/backend/common/config/app_config.py

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ def __init__(self):
5757
self._cosmos_database = None
5858
self._ai_project_client = None
5959

60+
def get_azure_credentials(self):
61+
"""Retrieve Azure credentials, either from environment variables or managed identity."""
62+
if self._azure_credentials is None:
63+
self._azure_credentials = get_azure_credential()
64+
return self._azure_credentials
65+
6066
def _get_required(self, name: str, default: Optional[str] = None) -> str:
6167
"""Get a required configuration value from environment variables.
6268
@@ -106,31 +112,6 @@ def _get_bool(self, name: str) -> bool:
106112
"""
107113
return name in os.environ and os.environ[name].lower() in ["true", "1"]
108114

109-
def get_cosmos_database_client(self):
110-
"""Get a Cosmos DB client for the configured database.
111-
112-
Returns:
113-
A Cosmos DB database client
114-
"""
115-
try:
116-
if self._cosmos_client is None:
117-
self._cosmos_client = CosmosClient(
118-
self.COSMOSDB_ENDPOINT, credential=get_azure_credential()
119-
)
120-
121-
if self._cosmos_database is None:
122-
self._cosmos_database = self._cosmos_client.get_database_client(
123-
self.COSMOSDB_DATABASE
124-
)
125-
126-
return self._cosmos_database
127-
except Exception as exc:
128-
logging.error(
129-
"Failed to create CosmosDB client: %s. CosmosDB is required for this application.",
130-
exc,
131-
)
132-
raise
133-
134115
def create_kernel(self):
135116
"""Creates a new Semantic Kernel instance.
136117

0 commit comments

Comments
 (0)