Skip to content

Commit 9860bcd

Browse files
committed
refactor long method to simpler implementation
1 parent 863d2cd commit 9860bcd

File tree

1 file changed

+40
-167
lines changed

1 file changed

+40
-167
lines changed

app/models/ai_persona.rb

Lines changed: 40 additions & 167 deletions
Original file line numberDiff line numberDiff line change
@@ -101,206 +101,79 @@ def bump_cache
101101
self.class.persona_cache.flush!
102102
end
103103

104-
105104
def class_instance
106-
allowed_group_ids = self.allowed_group_ids
107-
id = self.id
108-
system = self.system
109-
user_id = self.user_id
110-
mentionable = self.mentionable
111-
default_llm = self.default_llm
112-
max_context_posts = self.max_context_posts
113-
vision_enabled = self.vision_enabled
114-
vision_max_pixels = self.vision_max_pixels
115-
rag_conversation_chunks = self.rag_conversation_chunks
116-
question_consolidator_llm = self.question_consolidator_llm
117-
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+
]
118121

119122
persona_class = DiscourseAi::AiBot::Personas::Persona.system_personas_by_id[self.id]
120-
if persona_class
121-
persona_class.define_singleton_method :allowed_group_ids do
122-
allowed_group_ids
123-
end
124-
125-
persona_class.define_singleton_method :id do
126-
id
127-
end
128-
129-
persona_class.define_singleton_method :system do
130-
system
131-
end
132-
133-
persona_class.define_singleton_method :user_id do
134-
user_id
135-
end
136-
137-
persona_class.define_singleton_method :allow_chat do
138-
allow_chat
139-
end
140-
141-
persona_class.define_singleton_method :mentionable do
142-
mentionable
143-
end
144-
145-
persona_class.define_singleton_method :default_llm do
146-
default_llm
147-
end
148123

149-
persona_class.define_singleton_method :max_context_posts do
150-
max_context_posts
151-
end
152-
153-
persona_class.define_singleton_method :vision_enabled do
154-
vision_enabled
155-
end
156-
157-
persona_class.define_singleton_method :vision_max_pixels do
158-
vision_max_pixels
159-
end
160-
161-
persona_class.define_singleton_method :question_consolidator_llm do
162-
question_consolidator_llm
163-
end
124+
instance_attributes = {}
125+
attributes.each do |attr|
126+
value = self.read_attribute(attr)
127+
instance_attributes[attr] = value
128+
end
164129

165-
persona_class.define_singleton_method :rag_conversation_chunks do
166-
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
167134
end
168-
169135
return persona_class
170136
end
171137

172-
name = self.name
173-
description = self.description
174-
ai_persona_id = self.id
175-
176138
options = {}
177-
178139
tools = self.respond_to?(:commands) ? self.commands : self.tools
179-
180140
tools =
181141
tools.filter_map do |element|
182-
inner_name = element
183-
current_options = nil
184-
185-
if element.is_a?(Array)
186-
inner_name = element[0]
187-
current_options = element[1]
188-
end
189-
190-
# Won't migrate data yet. Let's rewrite to the tool name.
142+
inner_name, current_options = element.is_a?(Array) ? element : [element, nil]
191143
inner_name = inner_name.gsub("Command", "")
192144
inner_name = "List#{inner_name}" if %w[Categories Tags].include?(inner_name)
193145

194146
begin
195-
klass = ("DiscourseAi::AiBot::Tools::#{inner_name}").constantize
147+
klass = "DiscourseAi::AiBot::Tools::#{inner_name}".constantize
196148
options[klass] = current_options if current_options
197149
klass
198150
rescue StandardError
199151
nil
200152
end
201153
end
202154

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

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

264-
define_singleton_method :to_s do
265-
"#<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(",")}>"
266162
end
267163

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

272-
define_method :initialize do |*args, **kwargs|
166+
define_method(:initialize) do |*args, **kwargs|
273167
@ai_persona = AiPersona.find_by(id: ai_persona_id)
274168
super(*args, **kwargs)
275169
end
276170

277-
define_method :persona_id do
278-
@ai_persona&.id
279-
end
280-
281-
define_method :tools do
282-
tools
283-
end
284-
285-
define_method :options do
286-
options
287-
end
288-
289-
define_method :temperature do
290-
@ai_persona&.temperature
291-
end
292-
293-
define_method :top_p do
294-
@ai_persona&.top_p
295-
end
296-
297-
define_method :system_prompt do
298-
@ai_persona&.system_prompt || "You are a helpful bot."
299-
end
300-
301-
define_method :uploads do
302-
@ai_persona&.uploads
303-
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 }
304177
end
305178
end
306179

0 commit comments

Comments
 (0)