| 
									
										
										
										
											2023-08-09 07:19:56 +10:00
										 |  |  | #frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module DiscourseAi::AiBot::Commands | 
					
						
							|  |  |  |   class ReadCommand < Command | 
					
						
							|  |  |  |     class << self | 
					
						
							|  |  |  |       def name | 
					
						
							|  |  |  |         "read" | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       def desc | 
					
						
							|  |  |  |         "Will read a topic or a post on this Discourse instance" | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       def parameters | 
					
						
							|  |  |  |         [ | 
					
						
							|  |  |  |           Parameter.new( | 
					
						
							|  |  |  |             name: "topic_id", | 
					
						
							|  |  |  |             description: "the id of the topic to read", | 
					
						
							|  |  |  |             type: "integer", | 
					
						
							|  |  |  |             required: true, | 
					
						
							|  |  |  |           ), | 
					
						
							|  |  |  |           Parameter.new( | 
					
						
							|  |  |  |             name: "post_number", | 
					
						
							|  |  |  |             description: "the post number to read", | 
					
						
							|  |  |  |             type: "integer", | 
					
						
							|  |  |  |             required: false, | 
					
						
							|  |  |  |           ), | 
					
						
							|  |  |  |         ] | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def description_args | 
					
						
							| 
									
										
										
										
											2023-08-09 16:01:48 +10:00
										 |  |  |       { title: @title, url: @url } | 
					
						
							| 
									
										
										
										
											2023-08-09 07:19:56 +10:00
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def process(topic_id:, post_number: nil) | 
					
						
							|  |  |  |       not_found = { topic_id: topic_id, description: "Topic not found" } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       @title = "" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       topic_id = topic_id.to_i | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       topic = Topic.find_by(id: topic_id) | 
					
						
							|  |  |  |       return not_found if !topic || !Guardian.new.can_see?(topic) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       @title = topic.title | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       posts = Post.secured(Guardian.new).where(topic_id: topic_id).order(:post_number).limit(40) | 
					
						
							| 
									
										
										
										
											2023-08-09 16:01:48 +10:00
										 |  |  |       @url = topic.relative_url(post_number) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-09 07:19:56 +10:00
										 |  |  |       posts = posts.where("post_number = ?", post_number) if post_number | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       content = +"title: #{topic.title}\n\n" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       posts.each { |post| content << "\n\n#{post.username} said:\n\n#{post.raw}" } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       # TODO: 16k or 100k models can handle a lot more tokens | 
					
						
							|  |  |  |       content = ::DiscourseAi::Tokenizer::BertTokenizer.truncate(content, 1500).squish | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-09 16:01:48 +10:00
										 |  |  |       result = { topic_id: topic_id, content: content, complete: true } | 
					
						
							|  |  |  |       result[:post_number] = post_number if post_number | 
					
						
							|  |  |  |       result | 
					
						
							| 
									
										
										
										
											2023-08-09 07:19:56 +10:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end |