@@ -120,7 +120,7 @@ def destroy
120120 model_id : llm_model . id ,
121121 display_name : llm_model . display_name ,
122122 name : llm_model . name ,
123- provider : llm_model . provider
123+ provider : llm_model . provider ,
124124 }
125125
126126 # Clean up companion users
@@ -206,97 +206,87 @@ def ai_llm_params(updating: nil)
206206 permitted
207207 end
208208
209- def log_llm_model_creation ( llm_model )
210- # Create basic entity details with important attributes
211- log_details = {
212- model_id : llm_model . id ,
213- model_name : llm_model . name ,
214- display_name : llm_model . display_name ,
215- provider : llm_model . provider ,
216- tokenizer : llm_model . tokenizer
209+ def ai_llm_logger_fields
210+ {
211+ display_name : {
212+ } ,
213+ name : {
214+ } ,
215+ provider : {
216+ } ,
217+ tokenizer : {
218+ } ,
219+ url : {
220+ } ,
221+ max_prompt_tokens : {
222+ } ,
223+ max_output_tokens : {
224+ } ,
225+ enabled_chat_bot : {
226+ } ,
227+ vision_enabled : {
228+ } ,
229+ api_key : {
230+ type : :sensitive ,
231+ } ,
232+ input_cost : {
233+ } ,
234+ output_cost : {
235+ } ,
236+ # JSON fields should be tracked as simple changes
237+ json_fields : [ :provider_params ] ,
217238 }
218-
219- # Add API key indication if present (without showing the key)
220- if llm_model . api_key . present?
221- log_details [ :api_key_set ] = true
222- end
239+ end
240+
241+ def log_llm_model_creation ( llm_model )
242+ logger = DiscourseAi :: Utils :: AiStaffActionLogger . new ( current_user )
243+ entity_details = { model_id : llm_model . id , subject : llm_model . display_name }
223244
224- # Add token limits if set
225- if llm_model . max_prompt_tokens . present?
226- log_details [ :max_prompt_tokens ] = llm_model . max_prompt_tokens
227- end
228-
229- if llm_model . max_output_tokens . present?
230- log_details [ :max_output_tokens ] = llm_model . max_output_tokens
231- end
232-
233245 # Add quota information as a special case
234246 if llm_model . llm_quotas . any?
235- log_details [ :quotas ] = llm_model . llm_quotas . map do |quota |
236- "Group #{ quota . group_id } : #{ quota . max_tokens } tokens, #{ quota . max_usages } usages, #{ quota . duration_seconds } s"
237- end . join ( "; " )
247+ entity_details [ :quotas ] = llm_model
248+ . llm_quotas
249+ . map do |quota |
250+ "Group #{ quota . group_id } : #{ quota . max_tokens } tokens, #{ quota . max_usages } usages, #{ quota . duration_seconds } s"
251+ end
252+ . join ( "; " )
238253 end
239-
240- # Use StaffActionLogger directly with the proper subject
241- StaffActionLogger . new ( current_user ) . log_custom ( "create_ai_llm_model" , log_details . merge ( subject : llm_model . display_name ) )
254+
255+ logger . log_creation ( "llm_model" , llm_model , ai_llm_logger_fields , entity_details )
242256 end
243257
244258 def log_llm_model_update ( llm_model , initial_attributes , initial_quotas )
245- # Create basic entity details
246- log_details = {
247- model_id : llm_model . id ,
248- model_name : llm_model . name ,
249- display_name : llm_model . display_name
250- }
251-
252- # Track simple changes
253- %w[ display_name name provider tokenizer url max_prompt_tokens max_output_tokens enabled_chat_bot vision_enabled ] . each do |field |
254- if initial_attributes [ field ] != llm_model . attributes [ field ]
255- log_details [ "#{ field } _changed" ] = true
256-
257- # For simple fields, show the before/after values
258- unless field == "api_key" # Skip sensitive fields
259- initial_value = initial_attributes [ field ]
260- current_value = llm_model . attributes [ field ]
261- log_details [ field ] = "#{ initial_value } → #{ current_value } "
262- end
263- end
264- end
265-
266- # Handle API key changes specially
267- if initial_attributes [ "api_key" ] . present? != llm_model . api_key . present?
268- log_details [ :api_key_changed ] = true
269- if llm_model . api_key . present?
270- log_details [ :api_key_status ] = "set"
271- else
272- log_details [ :api_key_status ] = "removed"
273- end
274- end
275-
259+ logger = DiscourseAi ::Utils ::AiStaffActionLogger . new ( current_user )
260+ entity_details = { model_id : llm_model . id , subject : llm_model . display_name }
261+
276262 # Track quota changes separately as they're a special case
277263 current_quotas = llm_model . llm_quotas . reload . map ( &:attributes )
278264 if initial_quotas != current_quotas
279- initial_quota_summary = initial_quotas . map { |q | "Group #{ q [ 'group_id' ] } : #{ q [ 'max_tokens' ] } tokens" } . join ( "; " )
280- current_quota_summary = current_quotas . map { |q | "Group #{ q [ 'group_id' ] } : #{ q [ 'max_tokens' ] } tokens" } . join ( "; " )
281- log_details [ :quotas_changed ] = true
282- log_details [ :quotas ] = "#{ initial_quota_summary } → #{ current_quota_summary } "
283- end
284-
285- # Check JSON fields like provider_params
286- if initial_attributes [ "provider_params" ] . to_s != llm_model . attributes [ "provider_params" ] . to_s
287- log_details [ :provider_params_changed ] = true
288- end
289-
290- # Only log if there are actual changes
291- if log_details . keys . any? { |k | k . to_s . end_with? ( "_changed" ) }
292- # Use StaffActionLogger directly with the proper subject
293- StaffActionLogger . new ( current_user ) . log_custom ( "update_ai_llm_model" , log_details . merge ( subject : llm_model . display_name ) )
265+ initial_quota_summary =
266+ initial_quotas
267+ . map { |q | "Group #{ q [ "group_id" ] } : #{ q [ "max_tokens" ] } tokens" }
268+ . join ( "; " )
269+ current_quota_summary =
270+ current_quotas
271+ . map { |q | "Group #{ q [ "group_id" ] } : #{ q [ "max_tokens" ] } tokens" }
272+ . join ( "; " )
273+ entity_details [ :quotas_changed ] = true
274+ entity_details [ :quotas ] = "#{ initial_quota_summary } → #{ current_quota_summary } "
294275 end
276+
277+ logger . log_update (
278+ "llm_model" ,
279+ llm_model ,
280+ initial_attributes ,
281+ ai_llm_logger_fields ,
282+ entity_details ,
283+ )
295284 end
296285
297286 def log_llm_model_deletion ( model_details )
298- # Use StaffActionLogger directly with the proper subject
299- StaffActionLogger . new ( current_user ) . log_custom ( "delete_ai_llm_model" , model_details . merge ( subject : model_details [ :display_name ] ) )
287+ logger = DiscourseAi ::Utils ::AiStaffActionLogger . new ( current_user )
288+ model_details [ :subject ] = model_details [ :display_name ]
289+ logger . log_deletion ( "llm_model" , model_details )
300290 end
301291 end
302292 end
0 commit comments