A platform for community discussion. Free, open, simple.
FEATURE: Add 13 AI persona moderation tools (#38334)
Rafael dos Santos Silva406dfc45af56
diff --git a/plugins/discourse-ai/config/locales/server.en.yml b/plugins/discourse-ai/config/locales/server.en.ymlindex 9051cf209c2..7066ac8ab7f 100644--- a/plugins/discourse-ai/config/locales/server.en.yml+++ b/plugins/discourse-ai/config/locales/server.en.yml@@ -529,6 +529,19 @@ en:random_picker: "Random Picker"categories: "List categories"flag_post: "Flag post"+ close_topic: "Close topic"+ unlist_topic: "Unlist topic"+ lock_post: "Lock post"+ delete_topic: "Delete topic"+ edit_post: "Edit post"+ edit_tags: "Edit tags"+ edit_category: "Edit category"+ set_topic_timer: "Set topic timer"+ set_slow_mode: "Set slow mode"+ move_posts: "Move posts"+ grant_badge: "Grant badge"+ assign: "Assign topic"+ mark_as_solved: "Mark as solved"search: "Search"tags: "List tags"time: "Time"@@ -558,6 +571,19 @@ en:random_picker: "Pick a random number or a random element of a list"categories: "List all publicly visible categories on the forum"flag_post: "Flag the current post for staff review"+ close_topic: "Close or open a topic"+ unlist_topic: "List or unlist a topic"+ lock_post: "Lock or unlock a post"+ delete_topic: "Delete a topic"+ edit_post: "Edit the content of a post"+ edit_tags: "Edit the tags on a topic"+ edit_category: "Move a topic to a different category"+ set_topic_timer: "Set a timer on a topic to close, open, delete, or bump it"+ set_slow_mode: "Enable or disable slow mode on a topic"+ move_posts: "Move posts to a new or existing topic"+ grant_badge: "Grant a badge to a user"+ assign: "Assign or unassign a topic to a user or group"+ mark_as_solved: "Mark or unmark a post as the accepted solution"search: "Search all public topics on the forum"tags: "List all tags on the forum"time: "Find time in various time zones"@@ -585,6 +611,19 @@ en:github_file_content: "Retrieved content of %{file_paths} from %{repo_name}@%{branch}"random_picker: "Picking from %{options}, picked: %{result}"flag_post: "Flagging post %{post_id} (flag_post: %{flag_post})"+ close_topic: "Closing topic %{topic_id} (closed: %{closed})"+ unlist_topic: "Unlisting topic %{topic_id} (unlisted: %{unlisted})"+ lock_post: "Locking post %{post_id} (locked: %{locked})"+ delete_topic: "Deleting topic %{topic_id} (deleted: %{deleted})"+ edit_post: "Editing post %{post_id}"+ edit_tags: "Editing tags on topic %{topic_id}: %{tags}"+ edit_category: "Moving topic %{topic_id} to category %{category_id}"+ set_topic_timer: "Setting %{timer_type} timer on topic %{topic_id} (%{duration_hours}h)"+ set_slow_mode: "Setting slow mode on topic %{topic_id} (%{slow_mode_seconds}s)"+ move_posts: "Moving posts %{post_ids} from topic %{topic_id}"+ grant_badge: "Granting badge '%{badge_name}' to %{username}"+ assign: "Assigning topic %{topic_id} (assigned: %{assigned})"+ mark_as_solved: "Marking post %{post_id} as solved (solved: %{solved})"read: "Reading: <a href='%{url}'>%{title}</a>"time: "Time in %{timezone} is %{time}"summarize: "Summarized <a href='%{url}'>%{title}</a>"@@ -633,6 +672,112 @@ en:flagged: "Post flagged for review."reason: "AI bot flagged this post.\n\nReason: %{reason}"+ close_topic:+ errors:+ not_found: "Topic not found."+ not_allowed: "You are not allowed to close or open this topic."+ no_reason: "A reason is required to close or open a topic."+ success: "Topic updated successfully."++ unlist_topic:+ errors:+ not_found: "Topic not found."+ not_allowed: "You are not allowed to unlist or list this topic."+ no_reason: "A reason is required to unlist or list a topic."+ success: "Topic updated successfully."++ lock_post:+ errors:+ not_found: "Post not found."+ not_allowed: "You are not allowed to lock or unlock this post."+ no_reason: "A reason is required to lock or unlock a post."+ success: "Post updated successfully."++ delete_topic:+ errors:+ not_found: "Topic not found."+ not_allowed: "You are not allowed to delete or recover this topic."+ no_reason: "A reason is required to delete or recover a topic."+ success: "Topic updated successfully."++ edit_post:+ errors:+ not_found: "Post not found."+ not_allowed: "You are not allowed to edit this post."+ nothing_to_edit: "Either raw or title must be provided."+ no_reason: "An edit reason is required."+ revision_failed: "Failed to revise the post."+ success: "Post edited successfully."++ edit_tags:+ errors:+ not_found: "Topic not found."+ not_allowed: "You are not allowed to edit tags on this topic."+ no_reason: "A reason is required to edit tags."+ tagging_disabled: "Tagging is not enabled on this site."+ success: "Tags updated successfully."++ edit_category:+ errors:+ not_found: "Topic not found."+ not_allowed: "You are not allowed to move this topic."+ category_not_found: "Category not found."+ no_reason: "A reason is required to move a topic."+ revision_failed: "Failed to move the topic."+ success: "Topic moved successfully."++ set_topic_timer:+ errors:+ not_found: "Topic not found."+ not_allowed: "You are not allowed to set a timer on this topic."+ no_reason: "A reason is required to set a topic timer."+ invalid_timer_type: "Invalid timer type. Must be one of: close, open, delete, silent_close, bump."+ success: "Topic timer updated successfully."++ set_slow_mode:+ errors:+ not_found: "Topic not found."+ not_allowed: "You are not allowed to set slow mode on this topic."+ no_reason: "A reason is required to set slow mode."+ success: "Slow mode updated successfully."++ move_posts:+ errors:+ not_found: "Topic not found."+ not_allowed: "You are not allowed to move posts in this topic."+ no_reason: "A reason is required to move posts."+ no_posts: "No post IDs provided."+ no_destination: "Either destination_topic_id or new_title must be provided."+ move_failed: "Failed to move posts."+ success: "Posts moved successfully."++ grant_badge:+ errors:+ user_not_found: "User not found."+ badge_not_found: "Badge not found."+ not_allowed: "You are not allowed to grant badges."+ no_reason: "A reason is required to grant a badge."+ badge_disabled: "Badge is not enabled."+ grant_failed: "Failed to grant badge."+ success: "Badge granted successfully."++ assign:+ errors:+ plugin_not_installed: "The discourse-assign plugin is not installed."+ not_found: "Topic not found."+ not_allowed: "You are not allowed to assign this topic."+ no_reason: "A reason is required to assign or unassign a topic."+ assignee_not_found: "User or group not found."+ assign_failed: "Failed to assign topic."+ success: "Topic assignment updated successfully."++ mark_as_solved:+ errors:+ plugin_not_installed: "The discourse-solved plugin is not installed."+ no_reason: "A reason is required to mark or unmark a post as solved."+ action_failed: "Failed to update the solved status."+ success: "Solved status updated successfully."+summarization:configuration_hint:one: "Configure the `%{setting}` setting first."diff --git a/plugins/discourse-ai/lib/personas/persona.rb b/plugins/discourse-ai/lib/personas/persona.rbindex b8835e41ef0..dbd4096ffc6 100644--- a/plugins/discourse-ai/lib/personas/persona.rb+++ b/plugins/discourse-ai/lib/personas/persona.rb@@ -127,6 +127,16 @@ module DiscourseAiTools::Search,Tools::Read,Tools::FlagPost,+ Tools::CloseTopic,+ Tools::UnlistTopic,+ Tools::LockPost,+ Tools::DeleteTopic,+ Tools::EditPost,+ Tools::EditCategory,+ Tools::SetTopicTimer,+ Tools::SetSlowMode,+ Tools::MovePosts,+ Tools::GrantBadge,Tools::DbSchema,Tools::SearchSettings,Tools::SettingContext,@@ -148,7 +158,10 @@ module DiscourseAitools << Tools::GithubSearchCode if SiteSetting.ai_bot_github_access_token.present?- tools << Tools::ListTags if SiteSetting.tagging_enabled+ if SiteSetting.tagging_enabled+ tools << Tools::ListTags+ tools << Tools::EditTags+ end# Image generation tools - use custom UI-configured toolsif Tools::Tool.available_custom_image_tools.present?@@ -162,6 +175,9 @@ module DiscourseAitools << Tools::Googleend+ tools << Tools::Assign if defined?(::Assigner)+ tools << Tools::MarkAsSolved if defined?(::DiscourseSolved)+toolsendenddiff --git a/plugins/discourse-ai/lib/personas/tools/assign.rb b/plugins/discourse-ai/lib/personas/tools/assign.rbnew file mode 100644index 00000000000..de0527c4636--- /dev/null+++ b/plugins/discourse-ai/lib/personas/tools/assign.rb@@ -0,0 +1,109 @@+# frozen_string_literal: true++module DiscourseAi+ module Personas+ module Tools+ class Assign < Tool+ def self.signature+ {+ name: name,+ description:+ "Assigns or unassigns a topic to a user or group based on the assigned parameter.",+ parameters: [+ {+ name: "topic_id",+ description: "The ID of the topic",+ type: "integer",+ required: true,+ },+ {+ name: "assigned",+ description: "true to assign, false to unassign",+ type: "boolean",+ required: true,+ },+ {+ name: "username",+ description: "The username to assign the topic to (required when assigning)",+ type: "string",+ },+ {+ name: "group_name",+ description: "The group name to assign the topic to, as an alternative to username",+ type: "string",+ },+ {+ name: "note",+ description: "An optional note to include with the assignment",+ type: "string",+ },+ {+ name: "reason",+ description: "Short explanation of why the topic is being assigned or unassigned",+ type: "string",+ required: true,+ },+ ],+ }+ end++ def self.name+ "assign"+ end++ def invoke+ if !defined?(::Assigner)+ return(error_response(I18n.t("discourse_ai.ai_bot.assign.errors.plugin_not_installed")))+ end++ topic = Topic.find_by(id: parameters[:topic_id])+ return error_response(I18n.t("discourse_ai.ai_bot.assign.errors.not_found")) if !topic++ if !guardian.can_assign?+ return error_response(I18n.t("discourse_ai.ai_bot.assign.errors.not_allowed"))+ end++ if reason.blank?+ return error_response(I18n.t("discourse_ai.ai_bot.assign.errors.no_reason"))+ end++ assigner = ::Assigner.new(topic, acting_user)++ if !parameters[:assigned]+ assigner.unassign+ return { status: "success", message: I18n.t("discourse_ai.ai_bot.assign.success") }+ end++ assign_to = find_assign_to+ if !assign_to+ return error_response(I18n.t("discourse_ai.ai_bot.assign.errors.assignee_not_found"))+ end++ result = assigner.assign(assign_to, note: parameters[:note])++ if result[:success]+ { status: "success", message: I18n.t("discourse_ai.ai_bot.assign.success") }+ else+ error_response(+ result[:error] || I18n.t("discourse_ai.ai_bot.assign.errors.assign_failed"),+ )+ end+ end++ def description_args+ { topic_id: parameters[:topic_id], assigned: parameters[:assigned] }+ end++ private++ def find_assign_to+ if parameters[:username].present?+ User.find_by(username: parameters[:username])+ elsif parameters[:group_name].present?+ Group.find_by(name: parameters[:group_name])+ end+ end+ end+ end+ end+enddiff --git a/plugins/discourse-ai/lib/personas/tools/close_topic.rb b/plugins/discourse-ai/lib/personas/tools/close_topic.rbnew file mode 100644index 00000000000..1eff78d6bf5--- /dev/null+++ b/plugins/discourse-ai/lib/personas/tools/close_topic.rb@@ -0,0 +1,73 @@+# frozen_string_literal: true++module DiscourseAi+ module Personas+ module Tools+ class CloseTopic < Tool+ def self.signature+ {+ name: name,+ description: "Closes or opens a topic based on the closed parameter.",+ parameters: [+ {+ name: "topic_id",+ description: "The ID of the topic",+ type: "integer",+ required: true,+ },+ {+ name: "closed",+ description: "true to close the topic, false to open it",+ type: "boolean",+ required: true,+ },+ {+ name: "reason",+ description: "Short explanation of why the topic is being closed or opened",+ type: "string",+ required: true,+ },+ {+ name: "public_reason",+ description:+ "Whether the reason should be posted as a small action post visible to topic participants",+ type: "boolean",+ },+ ],+ }+ end++ def self.name+ "close_topic"+ end++ def invoke+ topic = Topic.find_by(id: parameters[:topic_id])+ if !topic+ return error_response(I18n.t("discourse_ai.ai_bot.close_topic.errors.not_found"))+ end++ if !guardian.can_close_topic?(topic)+ return error_response(I18n.t("discourse_ai.ai_bot.close_topic.errors.not_allowed"))+ end++ if reason.blank?+ return error_response(I18n.t("discourse_ai.ai_bot.close_topic.errors.no_reason"))+ end++ closed = !!parameters[:closed]++ opts = {}+ opts[:message] = reason if !!parameters[:public_reason]+ TopicStatusUpdater.new(topic, acting_user).update!("closed", closed, opts)++ { status: "success", message: I18n.t("discourse_ai.ai_bot.close_topic.success") }+ end++ def description_args+ { topic_id: parameters[:topic_id], closed: parameters[:closed] }+ end+ end+ end+ end+enddiff --git a/plugins/discourse-ai/lib/personas/tools/create_artifact.rb b/plugins/discourse-ai/lib/personas/tools/create_artifact.rbindex c478b0511ec..b70b1aef620 100644--- a/plugins/discourse-ai/lib/personas/tools/create_artifact.rb+++ b/plugins/discourse-ai/lib/personas/tools/create_artifact.rb@@ -362,10 +362,6 @@ module DiscourseAidef success_response(artifact){ status: "success", artifact_id: artifact.id, message: "Artifact created successfully." }end-- def error_response(message)- { status: "error", error: message }- endendendenddiff --git a/plugins/discourse-ai/lib/personas/tools/delete_topic.rb b/plugins/discourse-ai/lib/personas/tools/delete_topic.rbnew file mode 100644index 00000000000..203b99e7ad7--- /dev/null+++ b/plugins/discourse-ai/lib/personas/tools/delete_topic.rb@@ -0,0 +1,78 @@+# frozen_string_literal: true++module DiscourseAi+ module Personas+ module Tools+ class DeleteTopic < Tool+ def self.signature+ {+ name: name,+ description: "Deletes or recovers a topic based on the deleted parameter.",+ parameters: [+ {+ name: "topic_id",+ description: "The ID of the topic",+ type: "integer",+ required: true,+ },+ {+ name: "deleted",+ description: "true to delete the topic, false to recover it",+ type: "boolean",+ required: true,+ },+ {+ name: "reason",+ description: "Short explanation of why the topic is being deleted or recovered",+ type: "string",+ required: true,+ },+ ],+ }+ end++ def self.name+ "delete_topic"+ end++ def invoke+ topic = Topic.with_deleted.find_by(id: parameters[:topic_id])+ if !topic+ return error_response(I18n.t("discourse_ai.ai_bot.delete_topic.errors.not_found"))+ end++ allowed =+ if !!parameters[:deleted]+ guardian.can_delete_topic?(topic)+ else+ guardian.can_recover_topic?(topic)+ end+ if !allowed+ return error_response(I18n.t("discourse_ai.ai_bot.delete_topic.errors.not_allowed"))+ end++ if reason.blank?+ return error_response(I18n.t("discourse_ai.ai_bot.delete_topic.errors.no_reason"))+ end++ first_post = Post.with_deleted.find_by(topic_id: topic.id, post_number: 1)+ if !first_post+ return error_response(I18n.t("discourse_ai.ai_bot.delete_topic.errors.not_found"))+ end++ if !!parameters[:deleted]+ PostDestroyer.new(acting_user, first_post, context: reason).destroy+ else+ PostDestroyer.new(acting_user, first_post, context: reason).recover+ end++ { status: "success", message: I18n.t("discourse_ai.ai_bot.delete_topic.success") }+ end++ def description_args+ { topic_id: parameters[:topic_id], deleted: parameters[:deleted] }+ end+ end+ end+ end+enddiff --git a/plugins/discourse-ai/lib/personas/tools/edit_category.rb b/plugins/discourse-ai/lib/personas/tools/edit_category.rbnew file mode 100644index 00000000000..a0a9bf6dccc--- /dev/null+++ b/plugins/discourse-ai/lib/personas/tools/edit_category.rb@@ -0,0 +1,91 @@+# frozen_string_literal: true++module DiscourseAi+ module Personas+ module Tools+ class EditCategory < Tool+ def self.signature+ {+ name: name,+ description: "Moves a topic to a different category.",+ parameters: [+ {+ name: "topic_id",+ description: "The ID of the topic to move",+ type: "integer",+ required: true,+ },+ {+ name: "category_id",+ description: "The ID of the target category",+ type: "integer",+ required: true,+ },+ {+ name: "reason",+ description: "Short explanation of why the topic is being moved",+ type: "string",+ required: true,+ },+ {+ name: "public_edit_reason",+ description: "Whether the reason should be visible in the post's revision history",+ type: "boolean",+ },+ ],+ }+ end++ def self.name+ "edit_category"+ end++ def invoke+ topic = Topic.find_by(id: parameters[:topic_id])+ if !topic+ return error_response(I18n.t("discourse_ai.ai_bot.edit_category.errors.not_found"))+ end++ if !guardian.can_edit_topic?(topic)+ return error_response(I18n.t("discourse_ai.ai_bot.edit_category.errors.not_allowed"))+ end++ category = Category.find_by(id: parameters[:category_id])+ if !category+ return(+ error_response(I18n.t("discourse_ai.ai_bot.edit_category.errors.category_not_found"))+ )+ end++ if reason.blank?+ return error_response(I18n.t("discourse_ai.ai_bot.edit_category.errors.no_reason"))+ end++ first_post = topic.first_post+ if !first_post+ return error_response(I18n.t("discourse_ai.ai_bot.edit_category.errors.not_found"))+ end++ revisor = PostRevisor.new(first_post, topic)+ result =+ revisor.revise!(+ acting_user,+ { category_id: category.id }.tap do |f|+ f[:edit_reason] = reason if !!parameters[:public_edit_reason]+ end,+ )++ if result+ { status: "success", message: I18n.t("discourse_ai.ai_bot.edit_category.success") }+ else+ error_response(I18n.t("discourse_ai.ai_bot.edit_category.errors.revision_failed"))+ end+ end++ def description_args+ { topic_id: parameters[:topic_id], category_id: parameters[:category_id] }+ end+ end+ end+ end+enddiff --git a/plugins/discourse-ai/lib/personas/tools/edit_post.rb b/plugins/discourse-ai/lib/personas/tools/edit_post.rbnew file mode 100644index 00000000000..f69d7992702--- /dev/null+++ b/plugins/discourse-ai/lib/personas/tools/edit_post.rb@@ -0,0 +1,85 @@+# frozen_string_literal: true++module DiscourseAi+ module Personas+ module Tools+ class EditPost < Tool+ def self.signature+ {+ name: name,+ description: "Edits the content of a post, and optionally the topic title.",+ parameters: [+ {+ name: "post_id",+ description: "The ID of the post to edit",+ type: "integer",+ required: true,+ },+ { name: "raw", description: "The new raw content for the post", type: "string" },+ {+ name: "title",+ description: "The new title for the topic (only applies to the first post)",+ type: "string",+ },+ {+ name: "edit_reason",+ description: "Short explanation of what was changed",+ type: "string",+ required: true,+ },+ {+ name: "public_edit_reason",+ description:+ "Whether the edit reason should be visible in the post's revision history",+ type: "boolean",+ },+ ],+ }+ end++ def self.name+ "edit_post"+ end++ def invoke+ post = Post.find_by(id: parameters[:post_id])+ return error_response(I18n.t("discourse_ai.ai_bot.edit_post.errors.not_found")) if !post++ if !guardian.can_edit_post?(post)+ return error_response(I18n.t("discourse_ai.ai_bot.edit_post.errors.not_allowed"))+ end++ raw = parameters[:raw]+ title = parameters[:title]+ edit_reason = parameters[:edit_reason].to_s.strip++ if raw.blank? && title.blank?+ return error_response(I18n.t("discourse_ai.ai_bot.edit_post.errors.nothing_to_edit"))+ end++ if edit_reason.blank?+ return error_response(I18n.t("discourse_ai.ai_bot.edit_post.errors.no_reason"))+ end++ fields = {}+ fields[:edit_reason] = edit_reason if !!parameters[:public_edit_reason]+ fields[:raw] = raw if raw.present?+ fields[:title] = title if title.present?++ revisor = PostRevisor.new(post, post.topic)+ result = revisor.revise!(acting_user, fields)++ if result+ { status: "success", message: I18n.t("discourse_ai.ai_bot.edit_post.success") }+ else+ error_response(I18n.t("discourse_ai.ai_bot.edit_post.errors.revision_failed"))+ end+ end++ def description_args+ { post_id: parameters[:post_id] }+ end+ end+ end+ end+enddiff --git a/plugins/discourse-ai/lib/personas/tools/edit_tags.rb b/plugins/discourse-ai/lib/personas/tools/edit_tags.rbnew file mode 100644index 00000000000..ba7579a7dbf--- /dev/null+++ b/plugins/discourse-ai/lib/personas/tools/edit_tags.rb@@ -0,0 +1,93 @@+# frozen_string_literal: true++module DiscourseAi+ module Personas+ module Tools+ class EditTags < Tool+ def self.signature+ {+ name: name,+ description: "Adds tags to a topic. By default appends to existing tags.",+ parameters: [+ {+ name: "topic_id",+ description: "The ID of the topic",+ type: "integer",+ required: true,+ },+ {+ name: "tags",+ description: "Array of tag names to add to the topic",+ type: "array",+ item_type: "string",+ required: true,+ },+ {+ name: "reason",+ description: "Short explanation of why the tags are being changed",+ type: "string",+ required: true,+ },+ {+ name: "replace",+ description:+ "When true, replaces all existing tags with the given list. Defaults to false (append).",+ type: "boolean",+ },+ {+ name: "public_edit_reason",+ description: "Whether the reason should be visible in the post's revision history",+ type: "boolean",+ },+ ],+ }+ end++ def self.name+ "edit_tags"+ end++ def invoke+ if !SiteSetting.tagging_enabled+ return error_response(I18n.t("discourse_ai.ai_bot.edit_tags.errors.tagging_disabled"))+ end++ topic = Topic.find_by(id: parameters[:topic_id])+ return error_response(I18n.t("discourse_ai.ai_bot.edit_tags.errors.not_found")) if !topic++ if !guardian.can_edit_tags?(topic)+ return error_response(I18n.t("discourse_ai.ai_bot.edit_tags.errors.not_allowed"))+ end++ if reason.blank?+ return error_response(I18n.t("discourse_ai.ai_bot.edit_tags.errors.no_reason"))+ end++ tag_names = parameters[:tags] || []+ tag_names = (topic.tags.pluck(:name) + tag_names).uniq if !parameters[:replace]++ fields = { tags: tag_names }+ fields[:edit_reason] = reason if !!parameters[:public_edit_reason]++ first_post = topic.first_post+ if !first_post+ return error_response(I18n.t("discourse_ai.ai_bot.edit_tags.errors.not_found"))+ end++ revisor = PostRevisor.new(first_post, topic)+ result = revisor.revise!(acting_user, fields)++ if result+ { status: "success", message: I18n.t("discourse_ai.ai_bot.edit_tags.success") }+ else+ error_response(topic.errors.full_messages.join(", "))+ end+ end++ def description_args+ { topic_id: parameters[:topic_id], tags: (parameters[:tags] || []).join(", ") }+ end+ end+ end+ end+enddiff --git a/plugins/discourse-ai/lib/personas/tools/flag_post.rb b/plugins/discourse-ai/lib/personas/tools/flag_post.rbindex 194fccd9033..d75b822bdd7 100644--- a/plugins/discourse-ai/lib/personas/tools/flag_post.rb+++ b/plugins/discourse-ai/lib/personas/tools/flag_post.rb@@ -46,10 +46,6 @@ module DiscourseAi!!parameters[:flag_post]end- def reason- parameters[:reason].to_s.strip- end-def flag_typepersona_flag_type = persona_option(:flag_type)(persona_flag_type.presence || feature_context[:flag_type].presence || "review").to_s@@ -170,10 +166,6 @@ module DiscourseAiendend- def error_response(message)- { status: "error", error: message }- end-def feature_contextreturn {} if !context.respond_to?(:feature_context)diff --git a/plugins/discourse-ai/lib/personas/tools/grant_badge.rb b/plugins/discourse-ai/lib/personas/tools/grant_badge.rbnew file mode 100644index 00000000000..55ff8b2e6ac--- /dev/null+++ b/plugins/discourse-ai/lib/personas/tools/grant_badge.rb@@ -0,0 +1,76 @@+# frozen_string_literal: true++module DiscourseAi+ module Personas+ module Tools+ class GrantBadge < Tool+ def self.signature+ {+ name: name,+ description: "Grants a badge to a user.",+ parameters: [+ {+ name: "username",+ description: "The username of the user to grant the badge to",+ type: "string",+ required: true,+ },+ {+ name: "badge_name",+ description: "The name of the badge to grant",+ type: "string",+ required: true,+ },+ {+ name: "reason",+ description: "Short explanation of why the badge is being granted",+ type: "string",+ required: true,+ },+ ],+ }+ end++ def self.name+ "grant_badge"+ end++ def invoke+ if !guardian.can_grant_badges?(nil)+ return error_response(I18n.t("discourse_ai.ai_bot.grant_badge.errors.not_allowed"))+ end++ user = User.find_by(username: parameters[:username])+ if !user+ return error_response(I18n.t("discourse_ai.ai_bot.grant_badge.errors.user_not_found"))+ end++ badge = Badge.find_by(name: parameters[:badge_name])+ if !badge+ return(error_response(I18n.t("discourse_ai.ai_bot.grant_badge.errors.badge_not_found")))+ end++ if reason.blank?+ return error_response(I18n.t("discourse_ai.ai_bot.grant_badge.errors.no_reason"))+ end++ if !badge.enabled?+ return(error_response(I18n.t("discourse_ai.ai_bot.grant_badge.errors.badge_disabled")))+ end++ user_badge = BadgeGranter.grant(badge, user, granted_by: acting_user)++ if user_badge+ { status: "success", message: I18n.t("discourse_ai.ai_bot.grant_badge.success") }+ else+ error_response(I18n.t("discourse_ai.ai_bot.grant_badge.errors.grant_failed"))+ end+ end++ def description_args+ { username: parameters[:username], badge_name: parameters[:badge_name] }+ end+ end+ end+ end+enddiff --git a/plugins/discourse-ai/lib/personas/tools/lock_post.rb b/plugins/discourse-ai/lib/personas/tools/lock_post.rbnew file mode 100644index 00000000000..69983cc1110--- /dev/null+++ b/plugins/discourse-ai/lib/personas/tools/lock_post.rb@@ -0,0 +1,67 @@+# frozen_string_literal: true++module DiscourseAi+ module Personas+ module Tools+ class LockPost < Tool+ def self.signature+ {+ name: name,+ description: "Locks or unlocks a post based on the locked parameter.",+ parameters: [+ {+ name: "post_id",+ description: "The ID of the post",+ type: "integer",+ required: true,+ },+ {+ name: "locked",+ description: "true to lock the post, false to unlock it",+ type: "boolean",+ required: true,+ },+ {+ name: "reason",+ description: "Short explanation of why the post is being locked or unlocked",+ type: "string",+ required: true,+ },+ ],+ }+ end++ def self.name+ "lock_post"+ end++ def invoke+ post = Post.find_by(id: parameters[:post_id])+ return error_response(I18n.t("discourse_ai.ai_bot.lock_post.errors.not_found")) if !post++ if !guardian.can_lock_post?(post)+ return error_response(I18n.t("discourse_ai.ai_bot.lock_post.errors.not_allowed"))+ end++ if reason.blank?+ return error_response(I18n.t("discourse_ai.ai_bot.lock_post.errors.no_reason"))+ end++ locker = PostLocker.new(post, acting_user)++ if !!parameters[:locked]+ locker.lock+ else+ locker.unlock+ end++ { status: "success", message: I18n.t("discourse_ai.ai_bot.lock_post.success") }+ end++ def description_args+ { post_id: parameters[:post_id], locked: parameters[:locked] }+ end+ end+ end+ end+enddiff --git a/plugins/discourse-ai/lib/personas/tools/mark_as_solved.rb b/plugins/discourse-ai/lib/personas/tools/mark_as_solved.rbnew file mode 100644index 00000000000..3bc5fb64cdc--- /dev/null+++ b/plugins/discourse-ai/lib/personas/tools/mark_as_solved.rb@@ -0,0 +1,84 @@+# frozen_string_literal: true++module DiscourseAi+ module Personas+ module Tools+ class MarkAsSolved < Tool+ def self.signature+ {+ name: name,+ description:+ "Marks or unmarks a post as the accepted solution for its topic based on the solved parameter.",+ parameters: [+ {+ name: "post_id",+ description: "The ID of the post to mark or unmark as the solution",+ type: "integer",+ required: true,+ },+ {+ name: "solved",+ description: "true to mark as solved, false to unmark",+ type: "boolean",+ required: true,+ },+ {+ name: "reason",+ description:+ "Short explanation of why the post is being marked or unmarked as solved",+ type: "string",+ required: true,+ },+ ],+ }+ end++ def self.name+ "mark_as_solved"+ end++ def invoke+ if !defined?(::DiscourseSolved)+ return(+ error_response(+ I18n.t("discourse_ai.ai_bot.mark_as_solved.errors.plugin_not_installed"),+ )+ )+ end++ if reason.blank?+ return(error_response(I18n.t("discourse_ai.ai_bot.mark_as_solved.errors.no_reason")))+ end++ if !!parameters[:solved]+ result =+ DiscourseSolved::AcceptAnswer.call(+ params: {+ post_id: parameters[:post_id],+ },+ guardian: guardian,+ )+ else+ result =+ DiscourseSolved::UnacceptAnswer.call(+ params: {+ post_id: parameters[:post_id],+ },+ guardian: guardian,+ )+ end++ if result.success?+ { status: "success", message: I18n.t("discourse_ai.ai_bot.mark_as_solved.success") }+ else+ error_response(I18n.t("discourse_ai.ai_bot.mark_as_solved.errors.action_failed"))+ end+ end++ def description_args+ { post_id: parameters[:post_id], solved: parameters[:solved] }+ end+ end+ end+ end+enddiff --git a/plugins/discourse-ai/lib/personas/tools/move_posts.rb b/plugins/discourse-ai/lib/personas/tools/move_posts.rbnew file mode 100644index 00000000000..dc55a0cc576--- /dev/null+++ b/plugins/discourse-ai/lib/personas/tools/move_posts.rb@@ -0,0 +1,107 @@+# frozen_string_literal: true++module DiscourseAi+ module Personas+ module Tools+ class MovePosts < Tool+ def self.signature+ {+ name: name,+ description: "Moves posts from one topic to another existing topic, or to a new topic.",+ parameters: [+ {+ name: "topic_id",+ description: "The ID of the source topic containing the posts to move",+ type: "integer",+ required: true,+ },+ {+ name: "post_ids",+ description: "Array of post IDs to move",+ type: "array",+ item_type: "integer",+ required: true,+ },+ {+ name: "destination_topic_id",+ description:+ "The ID of the existing topic to move posts to. Either this or new_title must be provided.",+ type: "integer",+ },+ {+ name: "new_title",+ description:+ "Title for a new topic to create and move posts into. Either this or destination_topic_id must be provided.",+ type: "string",+ },+ {+ name: "category_id",+ description: "Category ID for the new topic (only used with new_title)",+ type: "integer",+ },+ {+ name: "reason",+ description: "Short explanation of why the posts are being moved",+ type: "string",+ required: true,+ },+ ],+ }+ end++ def self.name+ "move_posts"+ end++ def invoke+ topic = Topic.find_by(id: parameters[:topic_id])+ return error_response(I18n.t("discourse_ai.ai_bot.move_posts.errors.not_found")) if !topic++ if !guardian.can_move_posts?(topic)+ return error_response(I18n.t("discourse_ai.ai_bot.move_posts.errors.not_allowed"))+ end++ if reason.blank?+ return error_response(I18n.t("discourse_ai.ai_bot.move_posts.errors.no_reason"))+ end++ post_ids = parameters[:post_ids]+ if post_ids.blank?+ return error_response(I18n.t("discourse_ai.ai_bot.move_posts.errors.no_posts"))+ end++ destination_topic_id = parameters[:destination_topic_id]+ new_title = parameters[:new_title]++ if destination_topic_id.blank? && new_title.blank?+ return(error_response(I18n.t("discourse_ai.ai_bot.move_posts.errors.no_destination")))+ end++ opts = {}+ if destination_topic_id.present?+ opts[:destination_topic_id] = destination_topic_id+ else+ opts[:title] = new_title+ opts[:category_id] = parameters[:category_id] if parameters[:category_id].present?+ end++ destination_topic = topic.move_posts(acting_user, post_ids, opts)++ if destination_topic.present?+ {+ status: "success",+ message: I18n.t("discourse_ai.ai_bot.move_posts.success"),+ destination_topic_id: destination_topic.id,+ }+ else+ error_response(I18n.t("discourse_ai.ai_bot.move_posts.errors.move_failed"))+ end+ end++ def description_args+ { topic_id: parameters[:topic_id], post_ids: (parameters[:post_ids] || []).join(", ") }+ end+ end+ end+ end+enddiff --git a/plugins/discourse-ai/lib/personas/tools/set_slow_mode.rb b/plugins/discourse-ai/lib/personas/tools/set_slow_mode.rbnew file mode 100644index 00000000000..59b371c7b34--- /dev/null+++ b/plugins/discourse-ai/lib/personas/tools/set_slow_mode.rb@@ -0,0 +1,82 @@+# frozen_string_literal: true++module DiscourseAi+ module Personas+ module Tools+ class SetSlowMode < Tool+ def self.signature+ {+ name: name,+ description:+ "Enables or disables slow mode on a topic. Slow mode limits how frequently users can post.",+ parameters: [+ {+ name: "topic_id",+ description: "The ID of the topic",+ type: "integer",+ required: true,+ },+ {+ name: "slow_mode_seconds",+ description: "Number of seconds between posts. Set to 0 to disable slow mode.",+ type: "integer",+ required: true,+ },+ {+ name: "duration_hours",+ description:+ "Number of hours until slow mode automatically expires. Omit for no expiration.",+ type: "integer",+ },+ {+ name: "reason",+ description: "Short explanation of why slow mode is being set",+ type: "string",+ required: true,+ },+ ],+ }+ end++ def self.name+ "set_slow_mode"+ end++ def invoke+ topic = Topic.find_by(id: parameters[:topic_id])+ if !topic+ return error_response(I18n.t("discourse_ai.ai_bot.set_slow_mode.errors.not_found"))+ end++ if !guardian.can_moderate_topic?(topic)+ return error_response(I18n.t("discourse_ai.ai_bot.set_slow_mode.errors.not_allowed"))+ end++ if reason.blank?+ return error_response(I18n.t("discourse_ai.ai_bot.set_slow_mode.errors.no_reason"))+ end++ slow_mode_seconds = parameters[:slow_mode_seconds].to_i+ topic.update!(slow_mode_seconds: slow_mode_seconds)++ enabled_until =+ if slow_mode_seconds > 0 && parameters[:duration_hours]+ parameters[:duration_hours].to_i.hours.from_now+ end++ topic.set_or_create_timer(+ TopicTimer.types[:clear_slow_mode],+ enabled_until,+ by_user: acting_user,+ )++ { status: "success", message: I18n.t("discourse_ai.ai_bot.set_slow_mode.success") }+ end++ def description_args+ { topic_id: parameters[:topic_id], slow_mode_seconds: parameters[:slow_mode_seconds] }+ end+ end+ end+ end+enddiff --git a/plugins/discourse-ai/lib/personas/tools/set_topic_timer.rb b/plugins/discourse-ai/lib/personas/tools/set_topic_timer.rbnew file mode 100644index 00000000000..987da896288--- /dev/null+++ b/plugins/discourse-ai/lib/personas/tools/set_topic_timer.rb@@ -0,0 +1,91 @@+# frozen_string_literal: true++module DiscourseAi+ module Personas+ module Tools+ class SetTopicTimer < Tool+ TIMER_TYPES = %w[close open delete silent_close bump].freeze++ def self.signature+ {+ name: name,+ description:+ "Sets or removes a timer on a topic. Timers can close, open, delete, silently close, or bump a topic after a specified duration.",+ parameters: [+ {+ name: "topic_id",+ description: "The ID of the topic",+ type: "integer",+ required: true,+ },+ {+ name: "timer_type",+ description: "The type of timer: close, open, delete, silent_close, or bump",+ type: "string",+ required: true,+ },+ {+ name: "duration_hours",+ description:+ "Number of hours from now until the timer fires. Set to null to remove an existing timer.",+ type: "integer",+ },+ {+ name: "reason",+ description: "Short explanation of why the timer is being set",+ type: "string",+ required: true,+ },+ ],+ }+ end++ def self.name+ "set_topic_timer"+ end++ def invoke+ topic = Topic.find_by(id: parameters[:topic_id])+ if !topic+ return error_response(I18n.t("discourse_ai.ai_bot.set_topic_timer.errors.not_found"))+ end++ if !guardian.can_moderate_topic?(topic)+ return(error_response(I18n.t("discourse_ai.ai_bot.set_topic_timer.errors.not_allowed")))+ end++ if reason.blank?+ return error_response(I18n.t("discourse_ai.ai_bot.set_topic_timer.errors.no_reason"))+ end++ timer_type = parameters[:timer_type].to_s+ if !TIMER_TYPES.include?(timer_type)+ return(+ error_response(+ I18n.t("discourse_ai.ai_bot.set_topic_timer.errors.invalid_timer_type"),+ )+ )+ end++ duration_hours = parameters[:duration_hours]++ topic.set_or_create_timer(+ TopicTimer.types[timer_type.to_sym],+ duration_hours,+ by_user: acting_user,+ )++ { status: "success", message: I18n.t("discourse_ai.ai_bot.set_topic_timer.success") }+ end++ def description_args+ {+ topic_id: parameters[:topic_id],+ timer_type: parameters[:timer_type],+ duration_hours: parameters[:duration_hours],+ }+ end+ end+ end+ end+enddiff --git a/plugins/discourse-ai/lib/personas/tools/tool.rb b/plugins/discourse-ai/lib/personas/tools/tool.rbindex 3897207fa6d..f3a2eff64ef 100644--- a/plugins/discourse-ai/lib/personas/tools/tool.rb+++ b/plugins/discourse-ai/lib/personas/tools/tool.rb@@ -276,6 +276,22 @@ module DiscourseAiOption.new(tool: self, name: name, type: type)end+ def acting_user+ bot_user || Discourse.system_user+ end++ def guardian+ Guardian.new(context.user || acting_user)+ end++ def reason+ parameters[:reason].to_s.strip+ end++ def error_response(message)+ { status: "error", error: message }+ end+def description_args{}enddiff --git a/plugins/discourse-ai/lib/personas/tools/unlist_topic.rb b/plugins/discourse-ai/lib/personas/tools/unlist_topic.rbnew file mode 100644index 00000000000..43aedc8477f--- /dev/null+++ b/plugins/discourse-ai/lib/personas/tools/unlist_topic.rb@@ -0,0 +1,73 @@+# frozen_string_literal: true++module DiscourseAi+ module Personas+ module Tools+ class UnlistTopic < Tool+ def self.signature+ {+ name: name,+ description: "Unlists or lists a topic based on the unlisted parameter.",+ parameters: [+ {+ name: "topic_id",+ description: "The ID of the topic",+ type: "integer",+ required: true,+ },+ {+ name: "unlisted",+ description: "true to unlist the topic, false to list it",+ type: "boolean",+ required: true,+ },+ {+ name: "reason",+ description: "Short explanation of why the topic is being unlisted or listed",+ type: "string",+ required: true,+ },+ {+ name: "public_reason",+ description:+ "Whether the reason should be posted as a small action post visible to topic participants",+ type: "boolean",+ },+ ],+ }+ end++ def self.name+ "unlist_topic"+ end++ def invoke+ topic = Topic.find_by(id: parameters[:topic_id])+ if !topic+ return error_response(I18n.t("discourse_ai.ai_bot.unlist_topic.errors.not_found"))+ end++ if !guardian.can_toggle_topic_visibility?(topic)+ return error_response(I18n.t("discourse_ai.ai_bot.unlist_topic.errors.not_allowed"))+ end++ if reason.blank?+ return error_response(I18n.t("discourse_ai.ai_bot.unlist_topic.errors.no_reason"))+ end++ unlisted = !!parameters[:unlisted]++ opts = {}+ opts[:message] = reason if !!parameters[:public_reason]+ TopicStatusUpdater.new(topic, acting_user).update!("visible", !unlisted, opts)++ { status: "success", message: I18n.t("discourse_ai.ai_bot.unlist_topic.success") }+ end++ def description_args+ { topic_id: parameters[:topic_id], unlisted: parameters[:unlisted] }+ end+ end+ end+ end+enddiff --git a/plugins/discourse-ai/spec/lib/personas/tools/assign_spec.rb b/plugins/discourse-ai/spec/lib/personas/tools/assign_spec.rbnew file mode 100644index 00000000000..0fbae175154--- /dev/null+++ b/plugins/discourse-ai/spec/lib/personas/tools/assign_spec.rb@@ -0,0 +1,100 @@+# frozen_string_literal: true++return unless defined?(::Assigner)++RSpec.describe DiscourseAi::Personas::Tools::Assign do+ fab!(:llm_model)+ let(:bot_user) { DiscourseAi::AiBot::EntryPoint.find_user_from_model(llm_model.name) }+ let(:llm) { DiscourseAi::Completions::Llm.proxy(llm_model) }+ fab!(:post)+ fab!(:user, :admin)+ fab!(:group)++ before do+ enable_current_plugin+ SiteSetting.ai_bot_enabled = true+ SiteSetting.assign_enabled = true+ end++ def tool(params = nil, **kwargs)+ params ||= kwargs+ described_class.new(params, bot_user: bot_user, llm: llm, context: context)+ end++ let(:context) { DiscourseAi::Personas::BotContext.new }+ let(:topic) { post.topic }++ it "assigns the topic to a user" do+ result =+ tool(+ topic_id: topic.id,+ assigned: true,+ username: user.username,+ reason: "Needs attention",+ ).invoke++ expect(result[:status]).to eq("success")+ expect(topic.reload.assignment).to be_present+ expect(topic.assignment.assigned_to).to eq(user)+ end++ it "assigns the topic to a group" do+ group.update!(assignable_level: Group::ALIAS_LEVELS[:everyone])++ result =+ tool(+ topic_id: topic.id,+ assigned: true,+ group_name: group.name,+ reason: "Team responsibility",+ ).invoke++ expect(result[:status]).to eq("success")+ expect(topic.reload.assignment).to be_present+ expect(topic.assignment.assigned_to).to eq(group)+ end++ it "unassigns the topic" do+ ::Assigner.new(topic, Discourse.system_user).assign(user)++ result = tool(topic_id: topic.id, assigned: false, reason: "No longer needed").invoke++ expect(result[:status]).to eq("success")+ expect(topic.reload.assignment).to be_nil+ end++ it "returns an error when topic is not found" do+ result = tool(topic_id: -1, assigned: true, username: user.username, reason: "Test").invoke++ expect(result[:status]).to eq("error")+ end++ it "returns an error when assignee is not found" do+ result =+ tool(topic_id: topic.id, assigned: true, username: "nonexistent", reason: "Test").invoke++ expect(result[:status]).to eq("error")+ end++ it "returns an error when reason is blank" do+ result = tool(topic_id: topic.id, assigned: true, username: user.username, reason: " ").invoke++ expect(result[:status]).to eq("error")+ end++ it "returns an error when context user lacks permission" do+ regular_user = Fabricate(:user, trust_level: TrustLevel[0])+ ctx = DiscourseAi::Personas::BotContext.new(user: regular_user)+ t =+ described_class.new(+ { topic_id: topic.id, assigned: true, username: user.username, reason: "test" },+ bot_user: bot_user,+ llm: llm,+ context: ctx,+ )+ result = t.invoke++ expect(result[:status]).to eq("error")+ expect(result[:error]).to include("not allowed")+ end+enddiff --git a/plugins/discourse-ai/spec/lib/personas/tools/close_topic_spec.rb b/plugins/discourse-ai/spec/lib/personas/tools/close_topic_spec.rbnew file mode 100644index 00000000000..a93fba57377--- /dev/null+++ b/plugins/discourse-ai/spec/lib/personas/tools/close_topic_spec.rb@@ -0,0 +1,93 @@+# frozen_string_literal: true++RSpec.describe DiscourseAi::Personas::Tools::CloseTopic do+ fab!(:llm_model)+ let(:bot_user) { DiscourseAi::AiBot::EntryPoint.find_user_from_model(llm_model.name) }+ let(:llm) { DiscourseAi::Completions::Llm.proxy(llm_model) }+ fab!(:topic)++ before do+ enable_current_plugin+ SiteSetting.ai_bot_enabled = true+ end++ def tool(params = nil, **kwargs)+ params ||= kwargs+ described_class.new(params, bot_user: bot_user, llm: llm, context: context)+ end++ let(:context) { DiscourseAi::Personas::BotContext.new }++ it "closes the topic when closed is true" do+ result = tool(topic_id: topic.id, closed: true, reason: "Off-topic discussion").invoke++ expect(result[:status]).to eq("success")+ expect(topic.reload.closed).to eq(true)+ end++ it "opens the topic when closed is false" do+ topic.update!(closed: true)++ result = tool(topic_id: topic.id, closed: false, reason: "Reopening for discussion").invoke++ expect(result[:status]).to eq("success")+ expect(topic.reload.closed).to eq(false)+ end++ it "posts the reason as a small action when public_reason is true" do+ result =+ tool(+ topic_id: topic.id,+ closed: true,+ reason: "Off-topic discussion",+ public_reason: true,+ ).invoke++ expect(result[:status]).to eq("success")+ small_action = topic.ordered_posts.last+ expect(small_action.post_type).to eq(Post.types[:small_action])+ expect(small_action.raw).to eq("Off-topic discussion")+ end++ it "does not post the reason when public_reason is false" do+ result =+ tool(+ topic_id: topic.id,+ closed: true,+ reason: "Off-topic discussion",+ public_reason: false,+ ).invoke++ expect(result[:status]).to eq("success")+ small_action = topic.ordered_posts.last+ expect(small_action.raw).to be_blank+ end++ it "returns an error when topic is not found" do+ result = tool(topic_id: -1, closed: true, reason: "Test").invoke++ expect(result[:status]).to eq("error")+ end++ it "returns an error when reason is blank" do+ result = tool(topic_id: topic.id, closed: true, reason: " ").invoke++ expect(result[:status]).to eq("error")+ end++ it "returns an error when context user lacks permission" do+ regular_user = Fabricate(:user, trust_level: TrustLevel[0])+ ctx = DiscourseAi::Personas::BotContext.new(user: regular_user)+ t =+ described_class.new(+ { topic_id: topic.id, closed: true, reason: "test" },+ bot_user: bot_user,+ llm: llm,+ context: ctx,+ )+ result = t.invoke++ expect(result[:status]).to eq("error")+ expect(result[:error]).to include("not allowed")+ end+enddiff --git a/plugins/discourse-ai/spec/lib/personas/tools/delete_topic_spec.rb b/plugins/discourse-ai/spec/lib/personas/tools/delete_topic_spec.rbnew file mode 100644index 00000000000..b953a0d029c--- /dev/null+++ b/plugins/discourse-ai/spec/lib/personas/tools/delete_topic_spec.rb@@ -0,0 +1,67 @@+# frozen_string_literal: true++RSpec.describe DiscourseAi::Personas::Tools::DeleteTopic do+ fab!(:llm_model)+ let(:bot_user) { DiscourseAi::AiBot::EntryPoint.find_user_from_model(llm_model.name) }+ let(:llm) { DiscourseAi::Completions::Llm.proxy(llm_model) }+ fab!(:post)++ before do+ enable_current_plugin+ SiteSetting.ai_bot_enabled = true+ end++ def tool(params = nil, **kwargs)+ params ||= kwargs+ described_class.new(params, bot_user: bot_user, llm: llm, context: context)+ end++ let(:context) { DiscourseAi::Personas::BotContext.new }++ it "deletes the topic when deleted is true" do+ topic_id = post.topic_id++ result = tool(topic_id: topic_id, deleted: true, reason: "Spam content").invoke++ expect(result[:status]).to eq("success")+ expect(post.reload.deleted_at).not_to be_nil+ end++ it "recovers the topic when deleted is false" do+ PostDestroyer.new(Discourse.system_user, post, context: "test setup").destroy+ post.reload++ result = tool(topic_id: post.topic_id, deleted: false, reason: "Restored after review").invoke++ expect(result[:status]).to eq("success")+ expect(post.reload.deleted_at).to be_nil+ end++ it "returns an error when topic is not found" do+ result = tool(topic_id: -1, deleted: true, reason: "Test").invoke++ expect(result[:status]).to eq("error")+ end++ it "returns an error when reason is blank" do+ result = tool(topic_id: post.topic_id, deleted: true, reason: " ").invoke++ expect(result[:status]).to eq("error")+ end++ it "returns an error when context user lacks permission" do+ regular_user = Fabricate(:user, trust_level: TrustLevel[0])+ ctx = DiscourseAi::Personas::BotContext.new(user: regular_user)+ t =+ described_class.new(+ { topic_id: post.topic_id, deleted: true, reason: "test" },+ bot_user: bot_user,+ llm: llm,+ context: ctx,+ )+ result = t.invoke++ expect(result[:status]).to eq("error")+ expect(result[:error]).to include("not allowed")+ end+enddiff --git a/plugins/discourse-ai/spec/lib/personas/tools/edit_category_spec.rb b/plugins/discourse-ai/spec/lib/personas/tools/edit_category_spec.rbnew file mode 100644index 00000000000..4300bee346f--- /dev/null+++ b/plugins/discourse-ai/spec/lib/personas/tools/edit_category_spec.rb@@ -0,0 +1,83 @@+# frozen_string_literal: true++RSpec.describe DiscourseAi::Personas::Tools::EditCategory do+ fab!(:llm_model)+ let(:bot_user) { DiscourseAi::AiBot::EntryPoint.find_user_from_model(llm_model.name) }+ let(:llm) { DiscourseAi::Completions::Llm.proxy(llm_model) }+ fab!(:post)+ fab!(:category)+ fab!(:target_category, :category)++ before do+ enable_current_plugin+ SiteSetting.ai_bot_enabled = true+ end++ def tool(params = nil, **kwargs)+ params ||= kwargs+ described_class.new(params, bot_user: bot_user, llm: llm, context: context)+ end++ let(:context) { DiscourseAi::Personas::BotContext.new }++ it "moves the topic to a different category" do+ topic = post.topic+ topic.update!(category: category)++ result = tool(topic_id: topic.id, category_id: target_category.id, reason: "Better fit").invoke++ expect(result[:status]).to eq("success")+ expect(topic.reload.category_id).to eq(target_category.id)+ expect(post.reload.edit_reason).to be_nil+ end++ it "sets a public edit reason when public_edit_reason is true" do+ topic = post.topic+ topic.update!(category: category)++ result =+ tool(+ topic_id: topic.id,+ category_id: target_category.id,+ reason: "Better fit",+ public_edit_reason: true,+ ).invoke++ expect(result[:status]).to eq("success")+ expect(post.reload.edit_reason).to eq("Better fit")+ end++ it "returns an error when topic is not found" do+ result = tool(topic_id: -1, category_id: target_category.id, reason: "Test").invoke++ expect(result[:status]).to eq("error")+ end++ it "returns an error when category is not found" do+ result = tool(topic_id: post.topic_id, category_id: -1, reason: "Test").invoke++ expect(result[:status]).to eq("error")+ end++ it "returns an error when reason is blank" do+ result = tool(topic_id: post.topic_id, category_id: target_category.id, reason: " ").invoke++ expect(result[:status]).to eq("error")+ end++ it "returns an error when context user lacks permission" do+ regular_user = Fabricate(:user, trust_level: TrustLevel[0])+ ctx = DiscourseAi::Personas::BotContext.new(user: regular_user)+ t =+ described_class.new(+ { topic_id: post.topic_id, category_id: target_category.id, reason: "test" },+ bot_user: bot_user,+ llm: llm,+ context: ctx,+ )+ result = t.invoke++ expect(result[:status]).to eq("error")+ expect(result[:error]).to include("not allowed")+ end+enddiff --git a/plugins/discourse-ai/spec/lib/personas/tools/edit_post_spec.rb b/plugins/discourse-ai/spec/lib/personas/tools/edit_post_spec.rbnew file mode 100644index 00000000000..dcfa429f85e--- /dev/null+++ b/plugins/discourse-ai/spec/lib/personas/tools/edit_post_spec.rb@@ -0,0 +1,86 @@+# frozen_string_literal: true++RSpec.describe DiscourseAi::Personas::Tools::EditPost do+ fab!(:llm_model)+ let(:bot_user) { DiscourseAi::AiBot::EntryPoint.find_user_from_model(llm_model.name) }+ let(:llm) { DiscourseAi::Completions::Llm.proxy(llm_model) }+ fab!(:post) { Fabricate(:post, raw: "Original content") }++ before do+ enable_current_plugin+ SiteSetting.ai_bot_enabled = true+ end++ def tool(params = nil, **kwargs)+ params ||= kwargs+ described_class.new(params, bot_user: bot_user, llm: llm, context: context)+ end++ let(:context) { DiscourseAi::Personas::BotContext.new }++ it "edits the post content" do+ result = tool(post_id: post.id, raw: "Updated content", edit_reason: "Fixing typo").invoke++ expect(result[:status]).to eq("success")+ expect(post.reload.raw).to eq("Updated content")+ expect(post.edit_reason).to be_nil+ end++ it "sets a public edit reason when public_edit_reason is true" do+ result =+ tool(+ post_id: post.id,+ raw: "Updated content",+ edit_reason: "Fixing typo",+ public_edit_reason: true,+ ).invoke++ expect(result[:status]).to eq("success")+ expect(post.reload.edit_reason).to eq("Fixing typo")+ end++ it "edits the topic title via the first post" do+ first_post = post.topic.first_post++ result =+ tool(post_id: first_post.id, title: "New Topic Title", edit_reason: "Better title").invoke++ expect(result[:status]).to eq("success")+ expect(first_post.topic.reload.title).to eq("New Topic Title")+ end++ it "returns an error when post is not found" do+ result = tool(post_id: -1, raw: "Content", edit_reason: "Reason").invoke++ expect(result[:status]).to eq("error")+ end++ it "returns an error when neither raw nor title is provided" do+ result = tool(post_id: post.id, edit_reason: "No changes").invoke++ expect(result[:status]).to eq("error")+ end++ it "returns an error when edit_reason is blank" do+ result = tool(post_id: post.id, raw: "Content", edit_reason: " ").invoke++ expect(result[:status]).to eq("error")+ end++ it "returns an error when context user lacks permission" do+ regular_user = Fabricate(:user, trust_level: TrustLevel[0])+ other_post = Fabricate(:post)+ ctx = DiscourseAi::Personas::BotContext.new(user: regular_user)+ t =+ described_class.new(+ { post_id: other_post.id, raw: "New content", edit_reason: "test" },+ bot_user: bot_user,+ llm: llm,+ context: ctx,+ )+ result = t.invoke++ expect(result[:status]).to eq("error")+ expect(result[:error]).to include("not allowed")+ end+enddiff --git a/plugins/discourse-ai/spec/lib/personas/tools/edit_tags_spec.rb b/plugins/discourse-ai/spec/lib/personas/tools/edit_tags_spec.rbnew file mode 100644index 00000000000..a005d12edea--- /dev/null+++ b/plugins/discourse-ai/spec/lib/personas/tools/edit_tags_spec.rb@@ -0,0 +1,105 @@+# frozen_string_literal: true++RSpec.describe DiscourseAi::Personas::Tools::EditTags do+ fab!(:llm_model)+ let(:bot_user) { DiscourseAi::AiBot::EntryPoint.find_user_from_model(llm_model.name) }+ let(:llm) { DiscourseAi::Completions::Llm.proxy(llm_model) }+ fab!(:post)+ fab!(:tag1, :tag) { Fabricate(:tag, name: "alpha") }+ fab!(:tag2, :tag) { Fabricate(:tag, name: "beta") }++ before do+ enable_current_plugin+ SiteSetting.ai_bot_enabled = true+ SiteSetting.tagging_enabled = true+ end++ def tool(params = nil, **kwargs)+ params ||= kwargs+ described_class.new(params, bot_user: bot_user, llm: llm, context: context)+ end++ let(:context) { DiscourseAi::Personas::BotContext.new }+ let(:topic) { post.topic }++ it "sets tags on the topic" do+ result = tool(topic_id: topic.id, tags: %w[alpha beta], reason: "Adding relevant tags").invoke++ expect(result[:status]).to eq("success")+ expect(topic.reload.tags.pluck(:name)).to contain_exactly("alpha", "beta")+ end++ it "appends to existing tags by default" do+ topic.tags << tag1++ result = tool(topic_id: topic.id, tags: %w[beta], reason: "Adding tag").invoke++ expect(result[:status]).to eq("success")+ expect(topic.reload.tags.pluck(:name)).to contain_exactly("alpha", "beta")+ end++ it "replaces existing tags when replace is true" do+ topic.tags << tag1++ result = tool(topic_id: topic.id, tags: %w[beta], reason: "Retagging", replace: true).invoke++ expect(result[:status]).to eq("success")+ expect(topic.reload.tags.pluck(:name)).to contain_exactly("beta")+ end++ it "sets a public edit reason when public_edit_reason is true" do+ result =+ tool(+ topic_id: topic.id,+ tags: %w[alpha],+ reason: "Categorizing",+ public_edit_reason: true,+ ).invoke++ expect(result[:status]).to eq("success")+ expect(post.reload.edit_reason).to eq("Categorizing")+ end++ it "does not set a public edit reason by default" do+ result = tool(topic_id: topic.id, tags: %w[alpha], reason: "Categorizing").invoke++ expect(result[:status]).to eq("success")+ expect(post.reload.edit_reason).to be_nil+ end++ it "returns an error when topic is not found" do+ result = tool(topic_id: -1, tags: %w[alpha], reason: "Test").invoke++ expect(result[:status]).to eq("error")+ end++ it "returns an error when reason is blank" do+ result = tool(topic_id: topic.id, tags: %w[alpha], reason: " ").invoke++ expect(result[:status]).to eq("error")+ end++ it "returns an error when tagging is disabled" do+ SiteSetting.tagging_enabled = false++ result = tool(topic_id: topic.id, tags: %w[alpha], reason: "Test").invoke++ expect(result[:status]).to eq("error")+ end++ it "returns an error when context user lacks permission" do+ regular_user = Fabricate(:user, trust_level: TrustLevel[0])+ ctx = DiscourseAi::Personas::BotContext.new(user: regular_user)+ t =+ described_class.new(+ { topic_id: topic.id, tags: %w[alpha], reason: "test" },+ bot_user: bot_user,+ llm: llm,+ context: ctx,+ )+ result = t.invoke++ expect(result[:status]).to eq("error")+ expect(result[:error]).to include("not allowed")+ end+enddiff --git a/plugins/discourse-ai/spec/lib/personas/tools/grant_badge_spec.rb b/plugins/discourse-ai/spec/lib/personas/tools/grant_badge_spec.rbnew file mode 100644index 00000000000..45ccc2d9ac9--- /dev/null+++ b/plugins/discourse-ai/spec/lib/personas/tools/grant_badge_spec.rb@@ -0,0 +1,75 @@+# frozen_string_literal: true++RSpec.describe DiscourseAi::Personas::Tools::GrantBadge do+ fab!(:llm_model)+ let(:bot_user) { DiscourseAi::AiBot::EntryPoint.find_user_from_model(llm_model.name) }+ let(:llm) { DiscourseAi::Completions::Llm.proxy(llm_model) }+ fab!(:user)+ fab!(:badge) { Fabricate(:badge, name: "Helpful Member") }++ before do+ enable_current_plugin+ SiteSetting.ai_bot_enabled = true+ end++ def tool(params = nil, **kwargs)+ params ||= kwargs+ described_class.new(params, bot_user: bot_user, llm: llm, context: context)+ end++ let(:context) { DiscourseAi::Personas::BotContext.new }++ it "grants the badge to the user" do+ result =+ tool(+ username: user.username,+ badge_name: "Helpful Member",+ reason: "Consistently helpful",+ ).invoke++ expect(result[:status]).to eq("success")+ expect(UserBadge.exists?(user: user, badge: badge)).to eq(true)+ end++ it "returns an error when user is not found" do+ result = tool(username: "nonexistent", badge_name: "Helpful Member", reason: "Test").invoke++ expect(result[:status]).to eq("error")+ end++ it "returns an error when badge is not found" do+ result = tool(username: user.username, badge_name: "Nonexistent Badge", reason: "Test").invoke++ expect(result[:status]).to eq("error")+ end++ it "returns an error when badge is disabled" do+ badge.update!(enabled: false)++ result = tool(username: user.username, badge_name: "Helpful Member", reason: "Test").invoke++ expect(result[:status]).to eq("error")+ end++ it "returns an error when reason is blank" do+ result = tool(username: user.username, badge_name: "Helpful Member", reason: " ").invoke++ expect(result[:status]).to eq("error")+ end++ it "returns an error when context user lacks permission" do+ regular_user = Fabricate(:user, trust_level: TrustLevel[0])+ ctx = DiscourseAi::Personas::BotContext.new(user: regular_user)+ t =+ described_class.new(+ { username: user.username, badge_name: badge.name, reason: "test" },+ bot_user: bot_user,+ llm: llm,+ context: ctx,+ )+ result = t.invoke++ expect(result[:status]).to eq("error")+ expect(result[:error]).to include("not allowed")+ end+enddiff --git a/plugins/discourse-ai/spec/lib/personas/tools/lock_post_spec.rb b/plugins/discourse-ai/spec/lib/personas/tools/lock_post_spec.rbnew file mode 100644index 00000000000..4e98f4bb287--- /dev/null+++ b/plugins/discourse-ai/spec/lib/personas/tools/lock_post_spec.rb@@ -0,0 +1,64 @@+# frozen_string_literal: true++RSpec.describe DiscourseAi::Personas::Tools::LockPost do+ fab!(:llm_model)+ let(:bot_user) { DiscourseAi::AiBot::EntryPoint.find_user_from_model(llm_model.name) }+ let(:llm) { DiscourseAi::Completions::Llm.proxy(llm_model) }+ fab!(:post)++ before do+ enable_current_plugin+ SiteSetting.ai_bot_enabled = true+ end++ def tool(params = nil, **kwargs)+ params ||= kwargs+ described_class.new(params, bot_user: bot_user, llm: llm, context: context)+ end++ let(:context) { DiscourseAi::Personas::BotContext.new }++ it "locks the post when locked is true" do+ result = tool(post_id: post.id, locked: true, reason: "Preventing edits").invoke++ expect(result[:status]).to eq("success")+ expect(post.reload.locked_by_id).to eq((bot_user || Discourse.system_user).id)+ end++ it "unlocks the post when locked is false" do+ post.update!(locked_by_id: Discourse.system_user.id)++ result = tool(post_id: post.id, locked: false, reason: "Allowing edits again").invoke++ expect(result[:status]).to eq("success")+ expect(post.reload.locked_by_id).to be_nil+ end++ it "returns an error when post is not found" do+ result = tool(post_id: -1, locked: true, reason: "Test").invoke++ expect(result[:status]).to eq("error")+ end++ it "returns an error when reason is blank" do+ result = tool(post_id: post.id, locked: true, reason: " ").invoke++ expect(result[:status]).to eq("error")+ end++ it "returns an error when context user lacks permission" do+ regular_user = Fabricate(:user, trust_level: TrustLevel[0])+ ctx = DiscourseAi::Personas::BotContext.new(user: regular_user)+ t =+ described_class.new(+ { post_id: post.id, locked: true, reason: "test" },+ bot_user: bot_user,+ llm: llm,+ context: ctx,+ )+ result = t.invoke++ expect(result[:status]).to eq("error")+ expect(result[:error]).to include("not allowed")+ end+enddiff --git a/plugins/discourse-ai/spec/lib/personas/tools/mark_as_solved_spec.rb b/plugins/discourse-ai/spec/lib/personas/tools/mark_as_solved_spec.rbnew file mode 100644index 00000000000..89a5b264800--- /dev/null+++ b/plugins/discourse-ai/spec/lib/personas/tools/mark_as_solved_spec.rb@@ -0,0 +1,71 @@+# frozen_string_literal: true++return unless defined?(::DiscourseSolved)++RSpec.describe DiscourseAi::Personas::Tools::MarkAsSolved do+ fab!(:llm_model)+ let(:bot_user) { DiscourseAi::AiBot::EntryPoint.find_user_from_model(llm_model.name) }+ let(:llm) { DiscourseAi::Completions::Llm.proxy(llm_model) }+ fab!(:topic)+ fab!(:post) { Fabricate(:post, topic: topic) }+ fab!(:reply, :post) { Fabricate(:post, topic: topic) }++ before do+ enable_current_plugin+ SiteSetting.ai_bot_enabled = true+ SiteSetting.solved_enabled = true+ SiteSetting.allow_solved_on_all_topics = true+ end++ def tool(params = nil, **kwargs)+ params ||= kwargs+ described_class.new(params, bot_user: bot_user, llm: llm, context: context)+ end++ let(:context) { DiscourseAi::Personas::BotContext.new }++ it "marks a post as the accepted solution" do+ result = tool(post_id: reply.id, solved: true, reason: "This answers the question").invoke++ expect(result[:status]).to eq("success")+ expect(topic.reload.solved).to be_present+ expect(topic.solved.answer_post).to eq(reply)+ end++ it "unmarks a post as the accepted solution" do+ DiscourseSolved::AcceptAnswer.call!(+ params: {+ post_id: reply.id,+ },+ guardian: Guardian.new(Discourse.system_user),+ )++ result = tool(post_id: reply.id, solved: false, reason: "Not the right answer").invoke++ expect(result[:status]).to eq("success")+ expect(topic.reload.solved).to be_nil+ end++ it "returns an error when reason is blank" do+ result = tool(post_id: reply.id, solved: true, reason: " ").invoke++ expect(result[:status]).to eq("error")+ end++ it "respects context user permissions via guardian" do+ return unless defined?(::DiscourseSolved)++ regular_user = Fabricate(:user, trust_level: TrustLevel[0])+ ctx = DiscourseAi::Personas::BotContext.new(user: regular_user)+ t =+ described_class.new(+ { post_id: reply.id, solved: true, reason: "test" },+ bot_user: bot_user,+ llm: llm,+ context: ctx,+ )+ result = t.invoke++ expect(result[:status]).to eq("error")+ end+enddiff --git a/plugins/discourse-ai/spec/lib/personas/tools/move_posts_spec.rb b/plugins/discourse-ai/spec/lib/personas/tools/move_posts_spec.rbnew file mode 100644index 00000000000..722f93f786a--- /dev/null+++ b/plugins/discourse-ai/spec/lib/personas/tools/move_posts_spec.rb@@ -0,0 +1,114 @@+# frozen_string_literal: true++RSpec.describe DiscourseAi::Personas::Tools::MovePosts do+ fab!(:llm_model)+ let(:bot_user) { DiscourseAi::AiBot::EntryPoint.find_user_from_model(llm_model.name) }+ let(:llm) { DiscourseAi::Completions::Llm.proxy(llm_model) }+ fab!(:topic)+ fab!(:post1, :post) { Fabricate(:post, topic: topic) }+ fab!(:post2, :post) { Fabricate(:post, topic: topic) }+ fab!(:destination_topic, :topic)++ before do+ enable_current_plugin+ SiteSetting.ai_bot_enabled = true+ end++ def tool(params = nil, **kwargs)+ params ||= kwargs+ described_class.new(params, bot_user: bot_user, llm: llm, context: context)+ end++ let(:context) { DiscourseAi::Personas::BotContext.new }++ it "moves posts to an existing topic" do+ result =+ tool(+ topic_id: topic.id,+ post_ids: [post2.id],+ destination_topic_id: destination_topic.id,+ reason: "Off-topic posts",+ ).invoke++ expect(result[:status]).to eq("success")+ expect(result[:destination_topic_id]).to eq(destination_topic.id)+ expect(post2.reload.topic_id).to eq(destination_topic.id)+ end++ it "moves posts to a new topic" do+ result =+ tool(+ topic_id: topic.id,+ post_ids: [post2.id],+ new_title: "Split discussion about something else",+ reason: "Splitting off-topic discussion",+ ).invoke++ expect(result[:status]).to eq("success")+ expect(result[:destination_topic_id]).to be_present+ expect(post2.reload.topic_id).to eq(result[:destination_topic_id])+ end++ it "returns an error when neither destination_topic_id nor new_title is provided" do+ result = tool(topic_id: topic.id, post_ids: [post2.id], reason: "Test").invoke++ expect(result[:status]).to eq("error")+ end++ it "returns an error when topic is not found" do+ result =+ tool(+ topic_id: -1,+ post_ids: [post2.id],+ destination_topic_id: destination_topic.id,+ reason: "Test",+ ).invoke++ expect(result[:status]).to eq("error")+ end++ it "returns an error when post_ids is empty" do+ result =+ tool(+ topic_id: topic.id,+ post_ids: [],+ destination_topic_id: destination_topic.id,+ reason: "Test",+ ).invoke++ expect(result[:status]).to eq("error")+ end++ it "returns an error when reason is blank" do+ result =+ tool(+ topic_id: topic.id,+ post_ids: [post2.id],+ destination_topic_id: destination_topic.id,+ reason: " ",+ ).invoke++ expect(result[:status]).to eq("error")+ end++ it "returns an error when context user lacks permission" do+ regular_user = Fabricate(:user, trust_level: TrustLevel[0])+ ctx = DiscourseAi::Personas::BotContext.new(user: regular_user)+ t =+ described_class.new(+ {+ topic_id: topic.id,+ post_ids: [post2.id],+ destination_topic_id: destination_topic.id,+ reason: "test",+ },+ bot_user: bot_user,+ llm: llm,+ context: ctx,+ )+ result = t.invoke++ expect(result[:status]).to eq("error")+ expect(result[:error]).to include("not allowed")+ end+enddiff --git a/plugins/discourse-ai/spec/lib/personas/tools/set_slow_mode_spec.rb b/plugins/discourse-ai/spec/lib/personas/tools/set_slow_mode_spec.rbnew file mode 100644index 00000000000..e57ba2341e1--- /dev/null+++ b/plugins/discourse-ai/spec/lib/personas/tools/set_slow_mode_spec.rb@@ -0,0 +1,78 @@+# frozen_string_literal: true++RSpec.describe DiscourseAi::Personas::Tools::SetSlowMode do+ fab!(:llm_model)+ let(:bot_user) { DiscourseAi::AiBot::EntryPoint.find_user_from_model(llm_model.name) }+ let(:llm) { DiscourseAi::Completions::Llm.proxy(llm_model) }+ fab!(:topic)++ before do+ enable_current_plugin+ SiteSetting.ai_bot_enabled = true+ end++ def tool(params = nil, **kwargs)+ params ||= kwargs+ described_class.new(params, bot_user: bot_user, llm: llm, context: context)+ end++ let(:context) { DiscourseAi::Personas::BotContext.new }++ it "enables slow mode on the topic" do+ result = tool(topic_id: topic.id, slow_mode_seconds: 600, reason: "Heated discussion").invoke++ expect(result[:status]).to eq("success")+ expect(topic.reload.slow_mode_seconds).to eq(600)+ end++ it "disables slow mode when slow_mode_seconds is 0" do+ topic.update!(slow_mode_seconds: 600)++ result = tool(topic_id: topic.id, slow_mode_seconds: 0, reason: "Discussion calmed").invoke++ expect(result[:status]).to eq("success")+ expect(topic.reload.slow_mode_seconds).to eq(0)+ end++ it "sets an expiration timer when duration_hours is provided" do+ result =+ tool(+ topic_id: topic.id,+ slow_mode_seconds: 300,+ duration_hours: 2,+ reason: "Temporary cooldown",+ ).invoke++ expect(result[:status]).to eq("success")+ timer = TopicTimer.find_by(topic: topic, status_type: TopicTimer.types[:clear_slow_mode])+ expect(timer).to be_present+ end++ it "returns an error when topic is not found" do+ result = tool(topic_id: -1, slow_mode_seconds: 600, reason: "Test").invoke++ expect(result[:status]).to eq("error")+ end++ it "returns an error when reason is blank" do+ result = tool(topic_id: topic.id, slow_mode_seconds: 600, reason: " ").invoke++ expect(result[:status]).to eq("error")+ end++ it "returns an error when context user lacks permission" do+ regular_user = Fabricate(:user, trust_level: TrustLevel[0])+ ctx = DiscourseAi::Personas::BotContext.new(user: regular_user)+ t =+ described_class.new(+ { topic_id: topic.id, slow_mode_seconds: 600, reason: "test" },+ bot_user: bot_user,+ llm: llm,+ context: ctx,+ )+ result = t.invoke++ expect(result[:status]).to eq("error")+ expect(result[:error]).to include("not allowed")+ end+enddiff --git a/plugins/discourse-ai/spec/lib/personas/tools/set_topic_timer_spec.rb b/plugins/discourse-ai/spec/lib/personas/tools/set_topic_timer_spec.rbnew file mode 100644index 00000000000..0647c705082--- /dev/null+++ b/plugins/discourse-ai/spec/lib/personas/tools/set_topic_timer_spec.rb@@ -0,0 +1,85 @@+# frozen_string_literal: true++RSpec.describe DiscourseAi::Personas::Tools::SetTopicTimer do+ fab!(:llm_model)+ let(:bot_user) { DiscourseAi::AiBot::EntryPoint.find_user_from_model(llm_model.name) }+ let(:llm) { DiscourseAi::Completions::Llm.proxy(llm_model) }+ fab!(:topic)++ before do+ enable_current_plugin+ SiteSetting.ai_bot_enabled = true+ end++ def tool(params = nil, **kwargs)+ params ||= kwargs+ described_class.new(params, bot_user: bot_user, llm: llm, context: context)+ end++ let(:context) { DiscourseAi::Personas::BotContext.new }++ it "sets a close timer on the topic" do+ result =+ tool(+ topic_id: topic.id,+ timer_type: "close",+ duration_hours: 24,+ reason: "Cooling off period",+ ).invoke++ expect(result[:status]).to eq("success")+ timer = topic.reload.public_topic_timer+ expect(timer).to be_present+ expect(timer.status_type).to eq(TopicTimer.types[:close])+ end++ it "removes a timer when duration_hours is null" do+ topic.set_or_create_timer(TopicTimer.types[:close], 24, by_user: Discourse.system_user)++ result =+ tool(+ topic_id: topic.id,+ timer_type: "close",+ duration_hours: nil,+ reason: "No longer needed",+ ).invoke++ expect(result[:status]).to eq("success")+ expect(topic.reload.public_topic_timer).to be_nil+ end++ it "returns an error for invalid timer type" do+ result =+ tool(topic_id: topic.id, timer_type: "invalid", duration_hours: 1, reason: "Test").invoke++ expect(result[:status]).to eq("error")+ end++ it "returns an error when topic is not found" do+ result = tool(topic_id: -1, timer_type: "close", duration_hours: 1, reason: "Test").invoke++ expect(result[:status]).to eq("error")+ end++ it "returns an error when reason is blank" do+ result = tool(topic_id: topic.id, timer_type: "close", duration_hours: 1, reason: " ").invoke++ expect(result[:status]).to eq("error")+ end++ it "returns an error when context user lacks permission" do+ regular_user = Fabricate(:user, trust_level: TrustLevel[0])+ ctx = DiscourseAi::Personas::BotContext.new(user: regular_user)+ t =+ described_class.new(+ { topic_id: topic.id, timer_type: "close", duration_hours: 1, reason: "test" },+ bot_user: bot_user,+ llm: llm,+ context: ctx,+ )+ result = t.invoke++ expect(result[:status]).to eq("error")+ expect(result[:error]).to include("not allowed")+ end+enddiff --git a/plugins/discourse-ai/spec/lib/personas/tools/unlist_topic_spec.rb b/plugins/discourse-ai/spec/lib/personas/tools/unlist_topic_spec.rbnew file mode 100644index 00000000000..d7ffd47a7c4--- /dev/null+++ b/plugins/discourse-ai/spec/lib/personas/tools/unlist_topic_spec.rb@@ -0,0 +1,83 @@+# frozen_string_literal: true++RSpec.describe DiscourseAi::Personas::Tools::UnlistTopic do+ fab!(:llm_model)+ let(:bot_user) { DiscourseAi::AiBot::EntryPoint.find_user_from_model(llm_model.name) }+ let(:llm) { DiscourseAi::Completions::Llm.proxy(llm_model) }+ fab!(:topic)++ before do+ enable_current_plugin+ SiteSetting.ai_bot_enabled = true+ end++ def tool(params = nil, **kwargs)+ params ||= kwargs+ described_class.new(params, bot_user: bot_user, llm: llm, context: context)+ end++ let(:context) { DiscourseAi::Personas::BotContext.new }++ it "unlists the topic when unlisted is true" do+ result = tool(topic_id: topic.id, unlisted: true, reason: "Needs cleanup").invoke++ expect(result[:status]).to eq("success")+ expect(topic.reload.visible).to eq(false)+ end++ it "lists the topic when unlisted is false" do+ topic.update!(visible: false)++ result = tool(topic_id: topic.id, unlisted: false, reason: "Ready to publish").invoke++ expect(result[:status]).to eq("success")+ expect(topic.reload.visible).to eq(true)+ end++ it "posts the reason as a small action when public_reason is true" do+ result =+ tool(topic_id: topic.id, unlisted: true, reason: "Needs cleanup", public_reason: true).invoke++ expect(result[:status]).to eq("success")+ small_action = topic.ordered_posts.last+ expect(small_action.post_type).to eq(Post.types[:small_action])+ expect(small_action.raw).to eq("Needs cleanup")+ end++ it "does not post the reason when public_reason is false" do+ result =+ tool(topic_id: topic.id, unlisted: true, reason: "Needs cleanup", public_reason: false).invoke++ expect(result[:status]).to eq("success")+ small_action = topic.ordered_posts.last+ expect(small_action.raw).to be_blank+ end++ it "returns an error when topic is not found" do+ result = tool(topic_id: -1, unlisted: true, reason: "Test").invoke++ expect(result[:status]).to eq("error")+ end++ it "returns an error when reason is blank" do+ result = tool(topic_id: topic.id, unlisted: true, reason: " ").invoke++ expect(result[:status]).to eq("error")+ end++ it "returns an error when context user lacks permission" do+ regular_user = Fabricate(:user, trust_level: TrustLevel[0])+ ctx = DiscourseAi::Personas::BotContext.new(user: regular_user)+ t =+ described_class.new(+ { topic_id: topic.id, unlisted: true, reason: "test" },+ bot_user: bot_user,+ llm: llm,+ context: ctx,+ )+ result = t.invoke++ expect(result[:status]).to eq("error")+ expect(result[:error]).to include("not allowed")+ end+end