11# frozen_string_literal: true
22
33class AiPersona < ActiveRecord ::Base
4- # TODO remove this line 01-11-2024
5- self . ignored_columns = [ :commands ]
4+ # TODO remove this line 01-1-2025
5+ self . ignored_columns = [ :commands , :allow_chat , :mentionable ]
66
77 # places a hard limit, so per site we cache a maximum of 500 classes
88 MAX_PERSONAS_PER_SITE = 500
@@ -52,49 +52,54 @@ def self.persona_users(user: nil)
5252 persona_cache [ :persona_users ] ||= AiPersona
5353 . where ( enabled : true )
5454 . joins ( :user )
55- . pluck (
56- "ai_personas.id, users.id, users.username_lower, allowed_group_ids, default_llm, mentionable, allow_chat" ,
57- )
58- . map do |id , user_id , username , allowed_group_ids , default_llm , mentionable , allow_chat |
55+ . map do |persona |
5956 {
60- id : id ,
61- user_id : user_id ,
62- username : username ,
63- allowed_group_ids : allowed_group_ids ,
64- default_llm : default_llm ,
65- mentionable : mentionable ,
66- allow_chat : allow_chat ,
57+ id : persona . id ,
58+ user_id : persona . user_id ,
59+ username : persona . user . username_lower ,
60+ allowed_group_ids : persona . allowed_group_ids ,
61+ default_llm : persona . default_llm ,
62+ force_default_llm : persona . force_default_llm ,
63+ allow_chat_channel_mentions : persona . allow_chat_channel_mentions ,
64+ allow_chat_direct_messages : persona . allow_chat_direct_messages ,
65+ allow_topic_mentions : persona . allow_topic_mentions ,
66+ allow_personal_messages : persona . allow_personal_messages ,
6767 }
6868 end
6969
7070 if user
71- persona_users . select { |mentionable | user . in_any_groups? ( mentionable [ :allowed_group_ids ] ) }
71+ persona_users . select { |user | user . in_any_groups? ( mentionable [ :allowed_group_ids ] ) }
7272 else
7373 persona_users
7474 end
7575 end
7676
77- def self . allowed_chat ( user : nil )
78- personas = persona_cache [ :allowed_chat ] ||= persona_users . select { |u | u [ :allow_chat ] }
77+ def self . allowed_modalities (
78+ user : nil ,
79+ allow_chat_channel_mentions : false ,
80+ allow_chat_direct_messages : false ,
81+ allow_topic_mentions : false ,
82+ allow_personal_messages : false
83+ )
84+ index =
85+ "modality-#{ allow_chat_channel_mentions } -#{ allow_chat_direct_messages } -#{ allow_topic_mentions } -#{ allow_personal_messages } "
86+
87+ personas =
88+ persona_cache [ index . to_sym ] ||= persona_users . select do |persona |
89+ next true if allow_chat_channel_mentions && persona [ :allow_chat_channel_mentions ]
90+ next true if allow_chat_direct_messages && persona [ :allow_chat_direct_messages ]
91+ next true if allow_topic_mentions && persona [ :allow_topic_mentions ]
92+ next true if allow_personal_messages && persona [ :allow_personal_messages ]
93+ false
94+ end
95+
7996 if user
8097 personas . select { |u | user . in_any_groups? ( u [ :allowed_group_ids ] ) }
8198 else
8299 personas
83100 end
84101 end
85102
86- def self . mentionables ( user : nil )
87- all_mentionables =
88- persona_cache [ :mentionables ] ||= persona_users . select do |mentionable |
89- mentionable [ :mentionable ]
90- end
91- if user
92- all_mentionables . select { |mentionable | user . in_any_groups? ( mentionable [ :allowed_group_ids ] ) }
93- else
94- all_mentionables
95- end
96- end
97-
98103 after_commit :bump_cache
99104
100105 def bump_cache
@@ -113,7 +118,11 @@ def class_instance
113118 vision_max_pixels
114119 rag_conversation_chunks
115120 question_consolidator_llm
116- allow_chat
121+ allow_chat_channel_mentions
122+ allow_chat_direct_messages
123+ allow_topic_mentions
124+ allow_personal_messages
125+ force_default_llm
117126 name
118127 description
119128 allowed_group_ids
@@ -243,7 +252,7 @@ def regenerate_rag_fragments
243252 private
244253
245254 def chat_preconditions
246- if allow_chat && !default_llm
255+ if ( allow_chat_channel_mentions || allow_chat_direct_messages ) && !default_llm
247256 errors . add ( :default_llm , I18n . t ( "discourse_ai.ai_bot.personas.default_llm_required" ) )
248257 end
249258 end
@@ -281,7 +290,6 @@ def ensure_not_system
281290# temperature :float
282291# top_p :float
283292# user_id :integer
284- # mentionable :boolean default(FALSE), not null
285293# default_llm :text
286294# max_context_posts :integer
287295# max_post_context_tokens :integer
@@ -291,16 +299,15 @@ def ensure_not_system
291299# rag_chunk_tokens :integer default(374), not null
292300# rag_chunk_overlap_tokens :integer default(10), not null
293301# rag_conversation_chunks :integer default(10), not null
294- # role :enum default("bot"), not null
295- # role_category_ids :integer default([]), not null, is an Array
296- # role_tags :string default([]), not null, is an Array
297- # role_group_ids :integer default([]), not null, is an Array
298- # role_whispers :boolean default(FALSE), not null
299- # role_max_responses_per_hour :integer default(50), not null
300302# question_consolidator_llm :text
301- # allow_chat :boolean default(FALSE), not null
302303# tool_details :boolean default(TRUE), not null
303304# tools :json not null
305+ # forced_tool_count :integer default(-1), not null
306+ # allow_chat_channel_mentions :boolean default(FALSE), not null
307+ # allow_chat_direct_messages :boolean default(FALSE), not null
308+ # allow_topic_mentions :boolean default(FALSE), not null
309+ # allow_personal_message :boolean default(TRUE), not null
310+ # force_default_llm :boolean default(FALSE), not null
304311#
305312# Indexes
306313#
0 commit comments