discourse-ai/lib/ai_bot/commands/read_command.rb
Sam 6ddc17fd61
DEV: port directory structure to Zeitwerk (#319)
Previous to this change we relied on explicit loading for a files in Discourse AI.

This had a few downsides:

- Busywork whenever you add a file (an extra require relative)
- We were not keeping to conventions internally ... some places were OpenAI others are OpenAi
- Autoloader did not work which lead to lots of full application broken reloads when developing.

This moves all of DiscourseAI into a Zeitwerk compatible structure.

It also leaves some minimal amount of manual loading (automation - which is loading into an existing namespace that may or may not be there)

To avoid needing /lib/discourse_ai/... we mount a namespace thus we are able to keep /lib pointed at ::DiscourseAi

Various files were renamed to get around zeitwerk rules and minimize usage of custom inflections

Though we can get custom inflections to work it is not worth it, will require a Discourse core patch which means we create a hard dependency.
2023-11-29 15:17:46 +11:00

78 lines
2.0 KiB
Ruby

#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
{ title: @title, url: @url }
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)
@url = topic.relative_url(post_number)
posts = posts.where("post_number = ?", post_number) if post_number
content = +<<~TEXT.strip
title: #{topic.title}
TEXT
category_names = [topic.category&.parent_category&.name, topic.category&.name].compact.join(
" ",
)
content << "\ncategories: #{category_names}" if category_names.present?
if topic.tags.length > 0
tags = DiscourseTagging.filter_visible(topic.tags, Guardian.new)
content << "\ntags: #{tags.map(&:name).join(", ")}\n\n" if tags.length > 0
end
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 = tokenizer.truncate(content, 1500).squish
result = { topic_id: topic_id, content: content, complete: true }
result[:post_number] = post_number if post_number
result
end
end
end