Skip to content

Commit 4eb562a

Browse files
committed
refactor long method to simpler implementation
1 parent 875bb04 commit 4eb562a

File tree

1 file changed

+40
-166
lines changed

1 file changed

+40
-166
lines changed

app/models/ai_persona.rb

Lines changed: 40 additions & 166 deletions
Original file line numberDiff line numberDiff line change
@@ -102,204 +102,78 @@ def bump_cache
102102
end
103103

104104
def class_instance
105-
allowed_group_ids = self.allowed_group_ids
106-
id = self.id
107-
system = self.system
108-
user_id = self.user_id
109-
mentionable = self.mentionable
110-
default_llm = self.default_llm
111-
max_context_posts = self.max_context_posts
112-
vision_enabled = self.vision_enabled
113-
vision_max_pixels = self.vision_max_pixels
114-
rag_conversation_chunks = self.rag_conversation_chunks
115-
question_consolidator_llm = self.question_consolidator_llm
116-
allow_chat = self.allow_chat
105+
attributes = %i[
106+
id
107+
user_id
108+
system
109+
mentionable
110+
default_llm
111+
max_context_posts
112+
vision_enabled
113+
vision_max_pixels
114+
rag_conversation_chunks
115+
question_consolidator_llm
116+
allow_chat
117+
name
118+
description
119+
allowed_group_ids
120+
]
117121

118122
persona_class = DiscourseAi::AiBot::Personas::Persona.system_personas_by_id[self.id]
119-
if persona_class
120-
persona_class.define_singleton_method :allowed_group_ids do
121-
allowed_group_ids
122-
end
123-
124-
persona_class.define_singleton_method :id do
125-
id
126-
end
127-
128-
persona_class.define_singleton_method :system do
129-
system
130-
end
131-
132-
persona_class.define_singleton_method :user_id do
133-
user_id
134-
end
135-
136-
persona_class.define_singleton_method :allow_chat do
137-
allow_chat
138-
end
139-
140-
persona_class.define_singleton_method :mentionable do
141-
mentionable
142-
end
143-
144-
persona_class.define_singleton_method :default_llm do
145-
default_llm
146-
end
147-
148-
persona_class.define_singleton_method :max_context_posts do
149-
max_context_posts
150-
end
151-
152-
persona_class.define_singleton_method :vision_enabled do
153-
vision_enabled
154-
end
155-
156-
persona_class.define_singleton_method :vision_max_pixels do
157-
vision_max_pixels
158-
end
159123

160-
persona_class.define_singleton_method :question_consolidator_llm do
161-
question_consolidator_llm
162-
end
124+
instance_attributes = {}
125+
attributes.each do |attr|
126+
value = self.read_attribute(attr)
127+
instance_attributes[attr] = value
128+
end
163129

164-
persona_class.define_singleton_method :rag_conversation_chunks do
165-
rag_conversation_chunks
130+
if persona_class
131+
instance_attributes.each do |key, value|
132+
# description/name are localized
133+
persona_class.define_singleton_method(key) { value } if key != :description && key != :name
166134
end
167-
168135
return persona_class
169136
end
170137

171-
name = self.name
172-
description = self.description
173-
ai_persona_id = self.id
174-
175138
options = {}
176-
177139
tools = self.respond_to?(:commands) ? self.commands : self.tools
178-
179140
tools =
180141
tools.filter_map do |element|
181-
inner_name = element
182-
current_options = nil
183-
184-
if element.is_a?(Array)
185-
inner_name = element[0]
186-
current_options = element[1]
187-
end
188-
189-
# Won't migrate data yet. Let's rewrite to the tool name.
142+
inner_name, current_options = element.is_a?(Array) ? element : [element, nil]
190143
inner_name = inner_name.gsub("Command", "")
191144
inner_name = "List#{inner_name}" if %w[Categories Tags].include?(inner_name)
192145

193146
begin
194-
klass = ("DiscourseAi::AiBot::Tools::#{inner_name}").constantize
147+
klass = "DiscourseAi::AiBot::Tools::#{inner_name}".constantize
195148
options[klass] = current_options if current_options
196149
klass
197150
rescue StandardError
198151
nil
199152
end
200153
end
201154

202-
Class.new(DiscourseAi::AiBot::Personas::Persona) do
203-
define_singleton_method :id do
204-
id
205-
end
206-
207-
define_singleton_method :name do
208-
name
209-
end
210-
211-
define_singleton_method :user_id do
212-
user_id
213-
end
214-
215-
define_singleton_method :description do
216-
description
217-
end
218-
219-
define_singleton_method :system do
220-
system
221-
end
222-
223-
define_singleton_method :allowed_group_ids do
224-
allowed_group_ids
225-
end
226-
227-
define_singleton_method :user_id do
228-
user_id
229-
end
230-
231-
define_singleton_method :mentionable do
232-
mentionable
233-
end
234-
235-
define_singleton_method :default_llm do
236-
default_llm
237-
end
238-
239-
define_singleton_method :max_context_posts do
240-
max_context_posts
241-
end
242-
243-
define_singleton_method :vision_enabled do
244-
vision_enabled
245-
end
246-
247-
define_singleton_method :vision_max_pixels do
248-
vision_max_pixels
249-
end
250-
251-
define_singleton_method :rag_conversation_chunks do
252-
rag_conversation_chunks
253-
end
254-
255-
define_singleton_method :question_consolidator_llm do
256-
question_consolidator_llm
257-
end
155+
ai_persona_id = self.id
258156

259-
define_singleton_method :allow_chat do
260-
allow_chat
261-
end
157+
Class.new(DiscourseAi::AiBot::Personas::Persona) do
158+
instance_attributes.each { |key, value| define_singleton_method(key) { value } }
262159

263-
define_singleton_method :to_s do
264-
"#<DiscourseAi::AiBot::Personas::Persona::Custom @name=#{self.name} @allowed_group_ids=#{self.allowed_group_ids.join(",")}>"
160+
define_singleton_method(:to_s) do
161+
"#<#{self.class.name} @name=#{name} @allowed_group_ids=#{allowed_group_ids.join(",")}>"
265162
end
266163

267-
define_singleton_method :inspect do
268-
"#<DiscourseAi::AiBot::Personas::Persona::Custom @name=#{self.name} @allowed_group_ids=#{self.allowed_group_ids.join(",")}>"
269-
end
164+
define_singleton_method(:inspect) { to_s }
270165

271-
define_method :initialize do |*args, **kwargs|
166+
define_method(:initialize) do |*args, **kwargs|
272167
@ai_persona = AiPersona.find_by(id: ai_persona_id)
273168
super(*args, **kwargs)
274169
end
275170

276-
define_method :persona_id do
277-
@ai_persona&.id
278-
end
279-
280-
define_method :tools do
281-
tools
282-
end
283-
284-
define_method :options do
285-
options
286-
end
287-
288-
define_method :temperature do
289-
@ai_persona&.temperature
290-
end
291-
292-
define_method :top_p do
293-
@ai_persona&.top_p
294-
end
295-
296-
define_method :system_prompt do
297-
@ai_persona&.system_prompt || "You are a helpful bot."
298-
end
299-
300-
define_method :uploads do
301-
@ai_persona&.uploads
302-
end
171+
define_method(:tools) { tools }
172+
define_method(:options) { options }
173+
define_method(:temperature) { @ai_persona&.temperature }
174+
define_method(:top_p) { @ai_persona&.top_p }
175+
define_method(:system_prompt) { @ai_persona&.system_prompt || "You are a helpful bot." }
176+
define_method(:uploads) { @ai_persona&.uploads }
303177
end
304178
end
305179

0 commit comments

Comments
 (0)