From d97f4f6899b45a48e740504c5e58988c0daf88c5 Mon Sep 17 00:00:00 2001
From: Anupam Kumar <kyteinsky@gmail.com>
Date: Fri, 10 Jan 2025 22:35:54 +0530
Subject: [PATCH] more fixes

Signed-off-by: Anupam Kumar <kyteinsky@gmail.com>
---
 context_chat_backend/controller.py        |  6 +++++-
 context_chat_backend/vectordb/pgvector.py | 20 ++++++++++++--------
 2 files changed, 17 insertions(+), 9 deletions(-)

diff --git a/context_chat_backend/controller.py b/context_chat_backend/controller.py
index 3485c9e..1399c97 100644
--- a/context_chat_backend/controller.py
+++ b/context_chat_backend/controller.py
@@ -322,7 +322,11 @@ def _(sources: list[UploadFile]):
 	# wait for 10 minutes before failing the request
 	semres = doc_parse_semaphore.acquire(block=True, timeout=10*60)
 	if not semres:
-		return JSONResponse('Document parser worker limit reached, try again in some time', 503)
+		return JSONResponse(
+			'Document parser worker limit reached, try again in some time',
+			503,
+			headers={'cc-retry': 'true'}
+		)
 	added_sources = exec_in_proc(target=embed_sources, args=(vectordb_loader, app.extra['CONFIG'], sources))
 	doc_parse_semaphore.release()
 
diff --git a/context_chat_backend/vectordb/pgvector.py b/context_chat_backend/vectordb/pgvector.py
index 513c8aa..e2a125f 100644
--- a/context_chat_backend/vectordb/pgvector.py
+++ b/context_chat_backend/vectordb/pgvector.py
@@ -198,14 +198,18 @@ def decl_update_access(self, user_ids: list[str], source_id: str, session_: orm.
 		session.execute(stmt)
 		session.commit()
 
-		access = [
-			AccessListStore(
-				uid=user_id,
-				source_id=source_id,
-			)
-			for user_id in user_ids
-		]
-		session.add_all(access)
+		stmt = (
+			sa.dialects.postgresql.insert(AccessListStore)
+			.values([
+				{
+					'uid': user_id,
+					'source_id': source_id,
+				}
+				for user_id in user_ids
+			])
+			.on_conflict_do_nothing(index_elements=['uid', 'source_id'])
+		)
+		session.execute(stmt)
 		session.commit()
 
 		if session_ is None: