Skip to content

Commit

Permalink
feat: add endpoint and tests for updating session ID in messages (lan…
Browse files Browse the repository at this point in the history
…gflow-ai#3946)

* Add endpoint to update session ID for messages in monitor API

* Add tests for updating session ID in messages endpoint

- Added test to successfully update session ID for all messages with the old session ID.
- Added test to handle case where no messages are found with the given session ID.

---------

Co-authored-by: anovazzi1 <otavio2204@gmail.com>
  • Loading branch information
2 people authored and diogocabral committed Nov 26, 2024
1 parent 7817c13 commit 5de0811
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/backend/base/langflow/api/v1/monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,39 @@ async def update_message(
raise HTTPException(status_code=500, detail=str(e))


@router.patch("/messages/session/{old_session_id}", response_model=list[MessageResponse])
async def update_session_id(
old_session_id: str,
new_session_id: str = Query(..., description="The new session ID to update to"),
session: Session = Depends(get_session),
current_user: User = Depends(get_current_active_user),
):
try:
# Get all messages with the old session ID
stmt = select(MessageTable).where(MessageTable.session_id == old_session_id)
messages = session.exec(stmt).all()

if not messages:
raise HTTPException(status_code=404, detail="No messages found with the given session ID")

# Update all messages with the new session ID
for message in messages:
message.session_id = new_session_id

session.add_all(messages)

session.commit()
message_responses = []
for message in messages:
session.refresh(message)
message_responses.append(MessageResponse.model_validate(message, from_attributes=True))
return message_responses
except HTTPException as e:
raise e
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))


@router.delete("/messages/session/{session_id}", status_code=204)
async def delete_messages_session(
session_id: str,
Expand Down
37 changes: 37 additions & 0 deletions src/backend/tests/unit/test_messages_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,40 @@ def test_delete_messages_session(client: TestClient, created_messages, logged_in
response = client.get("api/v1/monitor/messages", headers=logged_in_headers)
assert response.status_code == 200
assert len(response.json()) == 0


# Successfully update session ID for all messages with the old session ID
def test_successfully_update_session_id(client, session, logged_in_headers, created_messages):
old_session_id = "session_id2"
new_session_id = "new_session_id"

response = client.patch(
f"api/v1/monitor/messages/session/{old_session_id}",
params={"new_session_id": new_session_id},
headers=logged_in_headers,
)

assert response.status_code == 200, response.text
updated_messages = response.json()
assert len(updated_messages) == len(created_messages)
for message in updated_messages:
assert message["session_id"] == new_session_id

response = client.get("api/v1/monitor/messages", headers=logged_in_headers, params={"session_id": new_session_id})
assert response.status_code == 200
assert len(response.json()) == len(created_messages)
for message in response.json():
assert message["session_id"] == new_session_id


# No messages found with the given session ID
def test_no_messages_found_with_given_session_id(client, session, logged_in_headers):
old_session_id = "non_existent_session_id"
new_session_id = "new_session_id"

response = client.patch(
f"/messages/session/{old_session_id}", params={"new_session_id": new_session_id}, headers=logged_in_headers
)

assert response.status_code == 404, response.text
assert response.json()["detail"] == "Not Found"

0 comments on commit 5de0811

Please sign in to comment.