55from ..models import CognitionMessage
66from ..util import prevent_sql_injection
77from .pipeline_version import get_current_version
8+ from sqlalchemy .orm .attributes import flag_modified
89
910
1011DEFAULT_TIME_ELAPSED = {
@@ -268,6 +269,51 @@ def get_message_feedback_overview(
268269 return general .execute_all (query )
269270
270271
272+ def get_show_shield_dict_by_conversation_ids (
273+ project_id : str , conversation_ids : List [str ]
274+ ) -> Dict [str , bool ]:
275+ if not conversation_ids or not project_id :
276+ return {}
277+ project_id = prevent_sql_injection (project_id , isinstance (project_id , str ))
278+ conversation_ids = [
279+ prevent_sql_injection (conversation_id , isinstance (conversation_id , str ))
280+ for conversation_id in conversation_ids
281+ ]
282+ conversation_id_filter = "('" + "','" .join (conversation_ids ) + "')"
283+ query = f"""
284+ SELECT jsonb_object_agg(t.conversation_id::text, t.show_shield_icon) AS convo_shields
285+ FROM (
286+ SELECT
287+ c.id AS conversation_id,
288+ COALESCE(
289+ bool_and(
290+ COALESCE(
291+ (m.additional_data->'privacy_report'->>'is_private') IN ('A+','A','A-'),
292+ false
293+ )
294+ )
295+ AND
296+ bool_and(
297+ COALESCE(
298+ (m.additional_data->'privacy_report'->>'any_privatemode_ai')::boolean,
299+ false
300+ )
301+ ),
302+ false
303+ ) AS show_shield_icon
304+ FROM cognition.conversation c
305+ LEFT JOIN cognition.message m
306+ ON c.project_id = m.project_id AND m.conversation_id = c.id
307+ WHERE c.project_id = '{ project_id } ' AND c.id IN { conversation_id_filter }
308+ GROUP BY c.id
309+ ) t;"""
310+
311+ show_shield_dict = general .execute_first (query )
312+ if show_shield_dict and show_shield_dict [0 ]:
313+ return show_shield_dict [0 ]
314+ return {}
315+
316+
271317def create (
272318 conversation_id : str ,
273319 project_id : str ,
@@ -276,6 +322,7 @@ def create(
276322 initiated_via : str ,
277323 with_commit : bool = True ,
278324 created_at : Optional [datetime ] = None ,
325+ additional_data : Optional [Dict [str , Any ]] = None ,
279326) -> CognitionMessage :
280327 version_id = None
281328 current_version = get_current_version (project_id )
@@ -289,6 +336,7 @@ def create(
289336 question = question ,
290337 facts = [],
291338 version_id = version_id ,
339+ additional_data = additional_data or {},
292340 initiated_via = initiated_via ,
293341 )
294342
@@ -306,6 +354,7 @@ def update(
306354 feedback_value : Optional [str ] = None ,
307355 feedback_category : Optional [str ] = None ,
308356 feedback_message : Optional [str ] = None ,
357+ additional_data : Optional [Union [Dict [str , Any ], str ]] = None ,
309358 with_commit : bool = True ,
310359) -> CognitionMessage :
311360 message = get (project_id , message_id )
@@ -321,6 +370,14 @@ def update(
321370 message .feedback_category = feedback_category
322371 if feedback_message is not None :
323372 message .feedback_message = feedback_message
373+ if additional_data is not None :
374+ if additional_data == "NULL" :
375+ message .additional_data = {}
376+ if message .additional_data is None :
377+ message .additional_data = {}
378+ for key , value in additional_data .items ():
379+ message .additional_data [key ] = value
380+ flag_modified (message , "additional_data" )
324381
325382 general .flush_or_commit (with_commit )
326383
0 commit comments