2024-01-04 08:44:07 -05:00
|
|
|
#frozen_string_literal: true
|
|
|
|
|
|
|
|
module DiscourseAi
|
|
|
|
module AiBot
|
2024-05-29 16:33:50 -04:00
|
|
|
MAX_POSTS = 100
|
|
|
|
|
2024-01-04 08:44:07 -05:00
|
|
|
module Tools
|
|
|
|
class Read < Tool
|
|
|
|
def self.signature
|
|
|
|
{
|
|
|
|
name: name,
|
|
|
|
description: "Will read a topic or a post on this Discourse instance",
|
|
|
|
parameters: [
|
|
|
|
{
|
|
|
|
name: "topic_id",
|
|
|
|
description: "the id of the topic to read",
|
|
|
|
type: "integer",
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
{
|
2024-05-29 16:33:50 -04:00
|
|
|
name: "post_numbers",
|
|
|
|
description: "the post numbers to read (optional)",
|
|
|
|
type: "array",
|
|
|
|
item_type: "integer",
|
|
|
|
required: false,
|
2024-01-04 08:44:07 -05:00
|
|
|
},
|
|
|
|
],
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2024-06-19 01:49:36 -04:00
|
|
|
def self.accepted_options
|
|
|
|
[option(:read_private, type: :boolean)]
|
|
|
|
end
|
|
|
|
|
2024-01-04 08:44:07 -05:00
|
|
|
def self.name
|
|
|
|
"read"
|
|
|
|
end
|
|
|
|
|
|
|
|
attr_reader :title, :url
|
|
|
|
|
|
|
|
def topic_id
|
|
|
|
parameters[:topic_id]
|
|
|
|
end
|
|
|
|
|
2024-05-29 16:33:50 -04:00
|
|
|
def post_numbers
|
|
|
|
parameters[:post_numbers]
|
2024-01-04 08:44:07 -05:00
|
|
|
end
|
|
|
|
|
2024-05-07 07:55:46 -04:00
|
|
|
def invoke
|
2024-01-04 08:44:07 -05:00
|
|
|
not_found = { topic_id: topic_id, description: "Topic not found" }
|
2024-06-19 01:49:36 -04:00
|
|
|
guardian = Guardian.new(context[:user]) if options[:read_private] && context[:user]
|
|
|
|
guardian ||= Guardian.new
|
2024-01-04 08:44:07 -05:00
|
|
|
|
|
|
|
@title = ""
|
|
|
|
|
|
|
|
topic = Topic.find_by(id: topic_id.to_i)
|
2024-06-19 01:49:36 -04:00
|
|
|
return not_found if !topic || !guardian.can_see?(topic)
|
2024-01-04 08:44:07 -05:00
|
|
|
|
|
|
|
@title = topic.title
|
|
|
|
|
2024-05-29 16:33:50 -04:00
|
|
|
posts =
|
2024-06-19 01:49:36 -04:00
|
|
|
Post.secured(guardian).where(topic_id: topic_id).order(:post_number).limit(MAX_POSTS)
|
2024-05-29 16:33:50 -04:00
|
|
|
|
|
|
|
post_number = 1
|
|
|
|
post_number = post_numbers.first if post_numbers.present?
|
|
|
|
|
2024-01-04 08:44:07 -05:00
|
|
|
@url = topic.relative_url(post_number)
|
|
|
|
|
2024-05-29 16:33:50 -04:00
|
|
|
posts = posts.where("post_number in (?)", post_numbers) if post_numbers.present?
|
2024-01-04 08:44:07 -05:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2024-05-29 16:33:50 -04:00
|
|
|
posts.each do |post|
|
|
|
|
content << "\n\n#{post.user&.name}(#{post.username}) said:\n\n#{post.raw}"
|
|
|
|
end
|
2024-01-04 08:44:07 -05:00
|
|
|
|
2024-05-29 16:33:50 -04:00
|
|
|
truncated_content =
|
|
|
|
truncate(content, max_length: 20_000, percent_length: 0.3, llm: llm).squish
|
2024-01-04 08:44:07 -05:00
|
|
|
|
2024-05-29 16:33:50 -04:00
|
|
|
result = { topic_id: topic_id, content: truncated_content }
|
|
|
|
result[:post_numbers] = post_numbers if post_numbers.present?
|
2024-01-04 08:44:07 -05:00
|
|
|
result
|
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
def description_args
|
|
|
|
{ title: title, url: url }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|