fix(library): validate folder existence when updating agent folder ID

- Enhanced the `update_library_agent` function to verify that the specified folder ID belongs to the user and is not deleted before updating the agent's folder ID.
- This change improves error handling by raising a `NotFoundError` if the folder is not found, ensuring better data integrity and user feedback during agent updates.
This commit is contained in:
abhi1992002
2026-02-16 13:51:27 +05:30
parent 09f74594ab
commit 01f18acba8

View File

@@ -690,7 +690,20 @@ async def update_library_agent(
update_fields["settings"] = SafeJson(settings.model_dump())
if folder_id is not None:
# Empty string means "move to root" (no folder)
update_fields["folderId"] = None if folder_id == "" else folder_id
if folder_id == "":
update_fields["folderId"] = None
else:
# Verify folder belongs to user
folder = await prisma.models.LibraryFolder.prisma().find_first(
where={
"id": folder_id,
"userId": user_id,
"isDeleted": False,
}
)
if not folder:
raise NotFoundError(f"Folder #{folder_id} not found")
update_fields["folderId"] = folder_id
try:
# If graph_version is provided, update to that specific version