Skip to content
This repository was archived by the owner on Jul 22, 2025. It is now read-only.

Commit b40badc

Browse files
committed
patch 2
1 parent d25e6aa commit b40badc

File tree

6 files changed

+67
-11
lines changed

6 files changed

+67
-11
lines changed

app/controllers/discourse_ai/admin/ai_embeddings_controller.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,19 @@ def log_ai_embedding_creation(embedding_def)
181181
def log_ai_embedding_update(embedding_def, initial_attributes)
182182
logger = DiscourseAi::Utils::AiStaffActionLogger.new(current_user)
183183
entity_details = { embedding_id: embedding_def.id, subject: embedding_def.display_name }
184+
185+
# Add flags for specific field changes
186+
if initial_attributes["provider"] != embedding_def.provider
187+
entity_details[:provider_changed] = true
188+
end
189+
190+
# Add a list of changed fields
191+
changed_fields = []
192+
embedding_def.previous_changes.each_key do |field|
193+
changed_fields << field
194+
end
195+
entity_details[:changed_fields] = changed_fields.join(", ") if changed_fields.any?
196+
184197
logger.log_update(
185198
"embedding",
186199
embedding_def,

app/controllers/discourse_ai/admin/ai_llms_controller.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,13 @@ def log_llm_model_creation(llm_model)
258258
def log_llm_model_update(llm_model, initial_attributes, initial_quotas)
259259
logger = DiscourseAi::Utils::AiStaffActionLogger.new(current_user)
260260
entity_details = { model_id: llm_model.id, subject: llm_model.display_name }
261+
262+
# Track specific field changes
263+
%w[name display_name provider api_key enabled_chat_bot vision_enabled].each do |field|
264+
if initial_attributes[field].to_s != llm_model.read_attribute(field).to_s
265+
entity_details["#{field}_changed"] = true
266+
end
267+
end
261268

262269
# Track quota changes separately as they're a special case
263270
current_quotas = llm_model.llm_quotas.reload.map(&:attributes)

app/controllers/discourse_ai/admin/ai_personas_controller.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,18 @@ def log_ai_persona_update(ai_persona, initial_attributes)
342342
logger = DiscourseAi::Utils::AiStaffActionLogger.new(current_user)
343343
entity_details = { persona_id: ai_persona.id, subject: ai_persona.name }
344344
entity_details[:tools_count] = ai_persona.tools.size if ai_persona.tools.present?
345+
346+
# Check for name changes
347+
if initial_attributes["name"] != ai_persona.name
348+
entity_details[:name_changed] = true
349+
end
350+
351+
# Add flags for other important changes
352+
%w[system_prompt description default_llm_id tools].each do |field|
353+
if initial_attributes[field].to_s != ai_persona.public_send(field).to_s
354+
entity_details["#{field}_changed"] = true
355+
end
356+
end
345357

346358
logger.log_update(
347359
"persona",

app/controllers/discourse_ai/admin/ai_spam_controller.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,15 +129,20 @@ def log_ai_spam_update(initial_llm_model_id, initial_custom_instructions, params
129129
LlmModel.find_by(id: params[:llm_model_id])&.display_name || params[:llm_model_id]
130130

131131
changes_to_log[:llm_model_id] = "#{old_model_name}#{new_model_name}"
132+
changes_to_log[:llm_model_changed] = true
132133
end
133134

134135
if params.key?(:custom_instructions) &&
135136
initial_custom_instructions != params[:custom_instructions]
136137
changes_to_log[:custom_instructions] = params[:custom_instructions]
138+
changes_to_log[:custom_instructions_changed] = true
137139
end
138140

139141
if changes_to_log.present?
140-
StaffActionLogger.new(current_user).log_custom("update_ai_spam_settings", changes_to_log)
142+
# Add a subject for the history record
143+
changes_to_log[:subject] = "AI Spam Settings"
144+
logger = DiscourseAi::Utils::AiStaffActionLogger.new(current_user)
145+
logger.log_custom("update_ai_spam_settings", changes_to_log)
141146
end
142147
end
143148

app/controllers/discourse_ai/admin/ai_tools_controller.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,12 @@ def log_ai_tool_creation(ai_tool)
148148
def log_ai_tool_update(ai_tool, initial_attributes)
149149
logger = DiscourseAi::Utils::AiStaffActionLogger.new(current_user)
150150
entity_details = { tool_id: ai_tool.id, subject: ai_tool.name }
151-
151+
152+
# Add flags for specific field changes
153+
if initial_attributes["name"] != ai_tool.name
154+
entity_details[:name_changed] = true
155+
end
156+
152157
logger.log_update(
153158
"tool",
154159
ai_tool,

lib/utils/ai_staff_action_logger.rb

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,19 @@ def log_creation(entity_type, entity, field_config = {}, entity_details = {})
1616
# Start with provided entity details (id, name, etc.)
1717
# Convert all keys to strings for consistent handling in StaffActionLogger
1818
log_details = {}
19-
entity_details.each { |k, v| log_details[k.to_s] = v }
19+
20+
# Extract subject for StaffActionLogger.base_attrs
21+
subject = entity_details[:subject] || (entity.respond_to?(:display_name) ? entity.display_name : nil)
22+
23+
# Add the entity details but preserve subject as a top-level attribute
24+
entity_details.each { |k, v| log_details[k.to_s] = v unless k == :subject }
2025

2126
# Extract attributes based on field configuration and ensure string keys
2227
extract_entity_attributes(entity, field_config).each do |key, value|
2328
log_details[key.to_s] = value
2429
end
2530

26-
@staff_logger.log_custom("create_ai_#{entity_type}", log_details)
31+
@staff_logger.log_custom("create_ai_#{entity_type}", log_details.merge(subject: subject))
2732
end
2833

2934
# Log update of an AI entity with before/after comparison
@@ -71,32 +76,41 @@ def log_update(
7176

7277
# Only log if there are actual changes
7378
if changes.any?
79+
# Extract subject for StaffActionLogger.base_attrs
80+
subject = entity_details[:subject] || (entity.respond_to?(:display_name) ? entity.display_name : nil)
81+
7482
log_details = {}
75-
# Convert entity_details keys to strings
76-
entity_details.each { |k, v| log_details[k.to_s] = v }
83+
# Convert entity_details keys to strings, but preserve subject as a top-level attribute
84+
entity_details.each { |k, v| log_details[k.to_s] = v unless k == :subject }
7785
# Merge changes (already with string keys)
7886
log_details.merge!(changes)
7987

80-
@staff_logger.log_custom("update_ai_#{entity_type}", log_details)
88+
@staff_logger.log_custom("update_ai_#{entity_type}", log_details.merge(subject: subject))
8189
end
8290
end
8391

8492
# Log deletion of an AI entity
8593
def log_deletion(entity_type, entity_details)
94+
# Extract subject for StaffActionLogger.base_attrs
95+
subject = entity_details[:subject]
96+
8697
# Convert all keys to strings for consistent handling in StaffActionLogger
8798
string_details = {}
88-
entity_details.each { |k, v| string_details[k.to_s] = v }
99+
entity_details.each { |k, v| string_details[k.to_s] = v unless k == :subject }
89100

90-
@staff_logger.log_custom("delete_ai_#{entity_type}", string_details)
101+
@staff_logger.log_custom("delete_ai_#{entity_type}", string_details.merge(subject: subject))
91102
end
92103

93104
# Direct custom logging for complex cases
94105
def log_custom(action_type, log_details)
106+
# Extract subject for StaffActionLogger.base_attrs if present
107+
subject = log_details[:subject]
108+
95109
# Convert all keys to strings for consistent handling in StaffActionLogger
96110
string_details = {}
97-
log_details.each { |k, v| string_details[k.to_s] = v }
111+
log_details.each { |k, v| string_details[k.to_s] = v unless k == :subject }
98112

99-
@staff_logger.log_custom(action_type, string_details)
113+
@staff_logger.log_custom(action_type, string_details.merge(subject: subject))
100114
end
101115

102116
private

0 commit comments

Comments
 (0)