|
| 1 | +from typing import Dict, List, Optional, Any |
| 2 | + |
| 3 | +from ..business_objects import general |
| 4 | +from ..session import session |
| 5 | +from ..models import CognitionConversationTag, CognitionConversationTagAssociation |
| 6 | +from ..util import sql_alchemy_to_dict, prevent_sql_injection |
| 7 | +from sqlalchemy.orm.attributes import flag_modified |
| 8 | +from sqlalchemy.types import Boolean |
| 9 | +from sqlalchemy import or_ |
| 10 | + |
| 11 | + |
| 12 | +BLACKLIST_CONVERSATION_TAG_ASSOCIATION = {"id", "conversation_id"} |
| 13 | + |
| 14 | + |
| 15 | +def get(tag_id: str) -> CognitionConversationTag: |
| 16 | + return ( |
| 17 | + session.query(CognitionConversationTag) |
| 18 | + .filter( |
| 19 | + CognitionConversationTag.id == tag_id, |
| 20 | + ) |
| 21 | + .first() |
| 22 | + ) |
| 23 | + |
| 24 | + |
| 25 | +def get_all_by_user(user_id: str) -> List[CognitionConversationTag]: |
| 26 | + return ( |
| 27 | + session.query(CognitionConversationTag) |
| 28 | + .filter( |
| 29 | + CognitionConversationTag.created_by == user_id, |
| 30 | + ) |
| 31 | + .all() |
| 32 | + ) |
| 33 | + |
| 34 | + |
| 35 | +def get_all_relevant(user_id: str, project_id: str): |
| 36 | + return ( |
| 37 | + session.query(CognitionConversationTag) |
| 38 | + .filter( |
| 39 | + CognitionConversationTag.created_by == user_id, |
| 40 | + or_( |
| 41 | + # global_tag is boolean true |
| 42 | + CognitionConversationTag.config["global_tag"].astext.cast(Boolean), |
| 43 | + # use_for_projects contains project_id |
| 44 | + CognitionConversationTag.config["use_for_projects"].contains( |
| 45 | + [project_id] |
| 46 | + ), |
| 47 | + ), |
| 48 | + ) |
| 49 | + .all() |
| 50 | + ) |
| 51 | + |
| 52 | + |
| 53 | +def create( |
| 54 | + user_id: str, |
| 55 | + name: str, |
| 56 | + config: Dict[str, Any], |
| 57 | + with_commit: bool = True, |
| 58 | +) -> CognitionConversationTag: |
| 59 | + tag: CognitionConversationTag = CognitionConversationTag( |
| 60 | + created_by=user_id, |
| 61 | + name=name, |
| 62 | + config=config, |
| 63 | + ) |
| 64 | + general.add(tag, with_commit) |
| 65 | + return tag |
| 66 | + |
| 67 | + |
| 68 | +def update( |
| 69 | + user_id: str, |
| 70 | + tag_id: str, |
| 71 | + name: Optional[str] = None, |
| 72 | + config: Optional[Dict[str, Any]] = None, |
| 73 | + with_commit: bool = True, |
| 74 | +) -> CognitionConversationTag: |
| 75 | + tag_entity = get(tag_id) |
| 76 | + if tag_entity is None: |
| 77 | + return |
| 78 | + if str(tag_entity.created_by) != user_id: |
| 79 | + raise ValueError("You are not allowed to update this tag.") |
| 80 | + if name is not None: |
| 81 | + tag_entity.name = name |
| 82 | + if config is not None and len(config) > 0: |
| 83 | + for key, value in config.items(): |
| 84 | + if value is None: |
| 85 | + tag_entity.config.pop(key, None) |
| 86 | + else: |
| 87 | + tag_entity.config[key] = value |
| 88 | + flag_modified(tag_entity, "config") |
| 89 | + general.flush_or_commit(with_commit) |
| 90 | + return tag_entity |
| 91 | + |
| 92 | + |
| 93 | +def delete(tag_id: str, with_commit: bool = True) -> None: |
| 94 | + session.query(CognitionConversationTag).filter( |
| 95 | + CognitionConversationTag.id == tag_id, |
| 96 | + ).delete() |
| 97 | + general.flush_or_commit(with_commit) |
| 98 | + |
| 99 | + |
| 100 | +def delete_many(tag_ids: List[str], with_commit: bool = True) -> None: |
| 101 | + session.query(CognitionConversationTag).filter( |
| 102 | + CognitionConversationTag.id.in_(tag_ids), |
| 103 | + ).delete(synchronize_session=False) |
| 104 | + general.flush_or_commit(with_commit) |
| 105 | + |
| 106 | + |
| 107 | +def create_association( |
| 108 | + conversation_id: str, |
| 109 | + tag_id: str, |
| 110 | + with_commit: bool = True, |
| 111 | +) -> None: |
| 112 | + association = CognitionConversationTagAssociation( |
| 113 | + conversation_id=conversation_id, |
| 114 | + tag_id=tag_id, |
| 115 | + ) |
| 116 | + general.add(association, with_commit) |
| 117 | + |
| 118 | + |
| 119 | +def delete_association( |
| 120 | + conversation_id: str, |
| 121 | + tag_id: str, |
| 122 | + with_commit: bool = True, |
| 123 | +) -> None: |
| 124 | + session.query(CognitionConversationTagAssociation).filter( |
| 125 | + CognitionConversationTagAssociation.conversation_id == conversation_id, |
| 126 | + CognitionConversationTagAssociation.tag_id == tag_id, |
| 127 | + ).delete(synchronize_session=False) |
| 128 | + general.flush_or_commit(with_commit) |
| 129 | + |
| 130 | + |
| 131 | +def get_lookup_by_conversation_ids( |
| 132 | + conversation_ids: List[str], |
| 133 | +) -> Dict[str, List[Dict[str, Any]]]: |
| 134 | + associations = ( |
| 135 | + session.query(CognitionConversationTagAssociation) |
| 136 | + .filter( |
| 137 | + CognitionConversationTagAssociation.conversation_id.in_(conversation_ids) |
| 138 | + ) |
| 139 | + .all() |
| 140 | + ) |
| 141 | + tag_lookup: Dict[str, List[Dict[str, Any]]] = {} |
| 142 | + |
| 143 | + for association in associations: |
| 144 | + if str(association.conversation_id) not in tag_lookup: |
| 145 | + tag_lookup[str(association.conversation_id)] = [] |
| 146 | + tag_lookup[str(association.conversation_id)].append( |
| 147 | + sql_alchemy_to_dict( |
| 148 | + association, column_blacklist=BLACKLIST_CONVERSATION_TAG_ASSOCIATION |
| 149 | + ) |
| 150 | + ) |
| 151 | + return tag_lookup |
| 152 | + |
| 153 | + |
| 154 | +def get_tag_counts(project_id: str, user_id: str) -> Dict[str, int]: |
| 155 | + |
| 156 | + project_id = prevent_sql_injection(project_id, isinstance(project_id, str)) |
| 157 | + user_id = prevent_sql_injection(user_id, isinstance(user_id, str)) |
| 158 | + |
| 159 | + query = f""" |
| 160 | + SELECT json_object_agg(tid,t_count) |
| 161 | + FROM ( |
| 162 | + SELECT COALESCE(cta.tag_id::TEXT,'<untagged>') tid, COUNT(*) t_count |
| 163 | + FROM cognition.conversation C |
| 164 | + LEFT JOIN cognition.conversation_tag_association cta |
| 165 | + ON c.id = cta.conversation_id |
| 166 | + WHERE c.project_id = '{project_id}' AND c.created_by = '{user_id}' |
| 167 | + group BY cta.tag_id |
| 168 | + ) x """ |
| 169 | + |
| 170 | + value = general.execute_first(query) |
| 171 | + if value and value[0]: |
| 172 | + return value[0] |
| 173 | + return {} |
0 commit comments