Skip to content

Commit 0061d11

Browse files
refactor: remove add_conversation endpoint and related tests
1 parent 0eeacfa commit 0061d11

File tree

4 files changed

+0
-169
lines changed

4 files changed

+0
-169
lines changed

src/api/api/history_routes.py

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -43,32 +43,6 @@
4343
history_service = HistoryService()
4444

4545

46-
@router.post("/generate")
47-
async def add_conversation(request: Request):
48-
try:
49-
authenticated_user = get_authenticated_user_details(
50-
request_headers=request.headers)
51-
user_id = authenticated_user["user_principal_id"]
52-
53-
# Parse request body
54-
request_json = await request.json()
55-
56-
response = await history_service.add_conversation(user_id, request_json)
57-
track_event_if_configured("ConversationCreated", {
58-
"user_id": user_id,
59-
"request": request_json,
60-
})
61-
return response
62-
63-
except Exception as e:
64-
logger.exception("Exception in /generate: %s", str(e))
65-
span = trace.get_current_span()
66-
if span is not None:
67-
span.record_exception(e)
68-
span.set_status(Status(StatusCode.ERROR, str(e)))
69-
return JSONResponse(content={"error": "An internal error has occurred!"}, status_code=500)
70-
71-
7246
@router.post("/update")
7347
async def update_conversation(request: Request):
7448
try:

src/api/services/history_service.py

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
from azure.ai.agents.models import MessageRole, ListSortOrder
77
from common.config.config import Config
88
from common.database.cosmosdb_service import CosmosConversationClient
9-
from helpers.chat_helper import complete_chat_request
109
from helpers.azure_credential_utils import get_azure_credential
1110

1211
# Configure logging
@@ -123,41 +122,6 @@ async def generate_title(self, conversation_messages):
123122
return user_messages[-1]["content"][:50]
124123
return "New Conversation"
125124

126-
async def add_conversation(self, user_id: str, request_json: dict):
127-
try:
128-
conversation_id = request_json.get("conversation_id")
129-
messages = request_json.get("messages", [])
130-
131-
history_metadata = {}
132-
133-
# make sure cosmos is configured
134-
cosmos_conversation_client = self.init_cosmosdb_client()
135-
if not cosmos_conversation_client:
136-
raise ValueError("CosmosDB is not configured or unavailable")
137-
138-
if not conversation_id:
139-
title = await self.generate_title(messages)
140-
conversation_dict = await cosmos_conversation_client.create_conversation(user_id, title)
141-
conversation_id = conversation_dict["id"]
142-
history_metadata["title"] = title
143-
history_metadata["date"] = conversation_dict["createdAt"]
144-
145-
if messages and messages[-1]["role"] == "user":
146-
created_message = await cosmos_conversation_client.create_message(conversation_id, user_id, messages[-1])
147-
if created_message == "Conversation not found":
148-
raise ValueError(
149-
f"Conversation not found for ID: {conversation_id}")
150-
else:
151-
raise ValueError("No user message found")
152-
153-
request_body = {
154-
"messages": messages, "history_metadata": {
155-
"conversation_id": conversation_id}}
156-
return await complete_chat_request(request_body)
157-
except Exception:
158-
logger.exception("Error in add_conversation")
159-
raise
160-
161125
async def update_conversation(self, user_id: str, request_json: dict):
162126
conversation_id = request_json.get("conversation_id")
163127
messages = request_json.get("messages", [])

src/tests/api/api/test_history_routes.py

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,6 @@ async def client():
2727
yield ac
2828

2929

30-
@pytest.mark.asyncio
31-
@patch("auth.auth_utils.get_authenticated_user_details")
32-
@patch("services.history_service.HistoryService.add_conversation", new_callable=AsyncMock)
33-
@patch("common.logging.event_utils.track_event_if_configured")
34-
async def test_add_conversation(mock_track, mock_add, mock_auth, client, headers):
35-
mock_auth.return_value = {"user_principal_id": "user123"}
36-
mock_add.return_value = {"result": "ok"}
37-
38-
res = await client.post("/generate", json={"message": "hello"}, headers=headers)
39-
assert res.status_code == 200
40-
assert res.json() == {"result": "ok"}
41-
42-
4330
@pytest.mark.asyncio
4431
@patch("auth.auth_utils.get_authenticated_user_details")
4532
@patch("services.history_service.HistoryService.update_conversation", new_callable=AsyncMock)
@@ -311,8 +298,3 @@ async def test_ensure_cosmos_unknown_error(mock_track, mock_ensure, client):
311298
assert res.json()["error"] == "CosmosDB is not configured or not working"
312299

313300

314-
@pytest.mark.asyncio
315-
@patch("auth.auth_utils.get_authenticated_user_details", side_effect=Exception("auth error"))
316-
async def test_add_conversation_exception(mock_auth, client, headers):
317-
res = await client.post("/generate", json={"message": "hi"}, headers=headers)
318-
assert res.status_code == 500

src/tests/api/services/test_history_service.py

Lines changed: 0 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -146,95 +146,6 @@ async def test_generate_title_exception(self, history_service):
146146
result = await history_service.generate_title(conversation_messages)
147147
assert result == "Fallback content"
148148

149-
@pytest.mark.asyncio
150-
async def test_add_conversation_new(self, history_service):
151-
"""Test adding a new conversation"""
152-
user_id = "test-user-id"
153-
request_json = {
154-
"conversation_id": None,
155-
"messages": [{"role": "user", "content": "Hello"}]
156-
}
157-
158-
mock_cosmos_client = AsyncMock()
159-
mock_cosmos_client.create_conversation = AsyncMock(
160-
return_value={"id": "new-conv-id", "title": "Test Title", "createdAt": "2023-01-01T00:00:00Z"}
161-
)
162-
mock_cosmos_client.create_message = AsyncMock(return_value="success")
163-
164-
with patch.object(history_service, "init_cosmosdb_client", return_value=mock_cosmos_client):
165-
with patch.object(history_service, "generate_title", AsyncMock(return_value="Test Title")):
166-
with patch("services.history_service.complete_chat_request", AsyncMock(return_value={"response": "test"})):
167-
result = await history_service.add_conversation(user_id, request_json)
168-
assert result == {"response": "test"}
169-
170-
# Verify calls
171-
mock_cosmos_client.create_conversation.assert_awaited_once()
172-
mock_cosmos_client.create_message.assert_awaited_once()
173-
174-
@pytest.mark.asyncio
175-
async def test_add_conversation_existing(self, history_service):
176-
"""Test adding to an existing conversation"""
177-
user_id = "test-user-id"
178-
request_json = {
179-
"conversation_id": "existing-id",
180-
"messages": [{"role": "user", "content": "Hello"}]
181-
}
182-
183-
mock_cosmos_client = AsyncMock()
184-
mock_cosmos_client.create_message = AsyncMock(return_value="success")
185-
186-
with patch.object(history_service, "init_cosmosdb_client", return_value=mock_cosmos_client):
187-
with patch("services.history_service.complete_chat_request", AsyncMock(return_value={"response": "test"})):
188-
result = await history_service.add_conversation(user_id, request_json)
189-
assert result == {"response": "test"}
190-
191-
# Verify calls
192-
mock_cosmos_client.create_message.assert_awaited_once()
193-
194-
@pytest.mark.asyncio
195-
async def test_add_conversation_cosmos_not_configured(self, history_service):
196-
"""Test adding conversation when cosmos is not configured"""
197-
user_id = "test-user-id"
198-
request_json = {
199-
"conversation_id": "existing-id",
200-
"messages": [{"role": "user", "content": "Hello"}]
201-
}
202-
203-
with patch.object(history_service, "init_cosmosdb_client", return_value=None):
204-
with pytest.raises(ValueError, match="CosmosDB is not configured or unavailable"):
205-
await history_service.add_conversation(user_id, request_json)
206-
207-
@pytest.mark.asyncio
208-
async def test_add_conversation_no_user_message(self, history_service):
209-
"""Test adding conversation with no user message"""
210-
user_id = "test-user-id"
211-
request_json = {
212-
"conversation_id": "existing-id",
213-
"messages": [{"role": "assistant", "content": "Hello"}]
214-
}
215-
216-
mock_cosmos_client = AsyncMock()
217-
218-
with patch.object(history_service, "init_cosmosdb_client", return_value=mock_cosmos_client):
219-
with pytest.raises(ValueError, match="No user message found"):
220-
await history_service.add_conversation(user_id, request_json)
221-
222-
@pytest.mark.asyncio
223-
async def test_add_conversation_conversation_not_found(self, history_service):
224-
"""Test adding to a non-existent conversation"""
225-
user_id = "test-user-id"
226-
request_json = {
227-
"conversation_id": "non-existent-id",
228-
"messages": [{"role": "user", "content": "Hello"}]
229-
}
230-
231-
mock_cosmos_client = AsyncMock()
232-
mock_cosmos_client.create_message = AsyncMock(return_value="Conversation not found")
233-
234-
with patch.object(history_service, "init_cosmosdb_client", return_value=mock_cosmos_client):
235-
with pytest.raises(ValueError, match="Conversation not found"):
236-
await history_service.add_conversation(user_id, request_json)
237-
238149
@pytest.mark.asyncio
239150
async def test_update_conversation(self, history_service):
240151
"""Test updating an existing conversation"""

0 commit comments

Comments
 (0)