Skip to content

A platform for community discussion. Free, open, simple.

DEV: Continue in same post after tool approval (#43687)

Penar Musaraj51f3559f10a7
diff --git a/plugins/discourse-ai/app/jobs/regular/resume_ai_tool_approval.rb b/plugins/discourse-ai/app/jobs/regular/resume_ai_tool_approval.rb
index 46ee10778db..539aa7414ce 100644
--- a/plugins/discourse-ai/app/jobs/regular/resume_ai_tool_approval.rb
+++ b/plugins/discourse-ai/app/jobs/regular/resume_ai_tool_approval.rb
@@ -95,6 +95,8 @@ module Jobs
authorization_user_id: user.id,
feature_name: "bot",
auto_set_title: false,
+ existing_reply_post: post,
+ append_to_existing_reply: true,
custom_instructions: custom_instructions,
additional_messages: additional_messages,
)
diff --git a/plugins/discourse-ai/lib/agents/discourse_admin_assistant.rb b/plugins/discourse-ai/lib/agents/discourse_admin_assistant.rb
index eb4a343eafd..c8fd6c43198 100644
--- a/plugins/discourse-ai/lib/agents/discourse_admin_assistant.rb
+++ b/plugins/discourse-ai/lib/agents/discourse_admin_assistant.rb
@@ -52,6 +52,7 @@ module DiscourseAi
- Give practical, concise answers that help an administrator complete the task. Start with the direct answer and do not describe your search process.
- For "how do I" questions, use a short numbered list of the relevant steps. Include alternative workflows only when they are materially useful.
- When directing an administrator to an area of this site, use a descriptive Markdown link with the exact absolute URL returned by a site tool. Use `search_discourse_navigation` when you do not already have a URL from a tool. Never invent a path, rewrite a Meta URL onto this site's hostname, or respond with a bare URL. If no verified destination is found, explain that you could not find a link instead of guessing one.
+ - Copy tool-provided URLs unchanged: never append an ID or number, another path segment, a query parameter, or a fragment. Category URLs already include the category ID. If you are uncertain about a navigation or category link, pass it as `url` to `search_discourse_navigation` with keywords for the destination. Use its `canonical_url` unchanged; if none is returned, omit the unverified link.
- Use Meta search results to support factual guidance and link to the most useful source with descriptive link text. Do not overwhelm the answer with sources.
- Mention permission requirements, trade-offs, or relevant site settings only when they affect the requested action.
- Prefer a compact answer: a direct recommendation, two to five actionable steps, and at most one short note for an important caveat.
diff --git a/plugins/discourse-ai/lib/agents/tools/create_category.rb b/plugins/discourse-ai/lib/agents/tools/create_category.rb
index 1d64019c373..427a47b5aa5 100644
--- a/plugins/discourse-ai/lib/agents/tools/create_category.rb
+++ b/plugins/discourse-ai/lib/agents/tools/create_category.rb
@@ -8,7 +8,7 @@ module DiscourseAi
{
name: name,
description:
- "Creates a new category. Create parent categories before their subcategories, and use list_categories to find parent category IDs.",
+ "Creates a new category and returns its ID and full URL to link to it. Copy the returned URL unchanged; it already includes the category ID, so never append an ID or number. Create parent categories before their subcategories, and use list_categories to find parent category IDs.",
parameters: [
{
name: "name",
@@ -138,7 +138,7 @@ module DiscourseAi
{
status: "success",
category_id: category.id,
- url: category.url,
+ url: "#{Discourse.base_url_no_prefix}#{category.url}",
message: I18n.t("discourse_ai.ai_bot.create_category.success", name: category.name),
}
else
diff --git a/plugins/discourse-ai/lib/agents/tools/search_discourse_navigation.rb b/plugins/discourse-ai/lib/agents/tools/search_discourse_navigation.rb
index 4408f2ae107..c486a2510a3 100644
--- a/plugins/discourse-ai/lib/agents/tools/search_discourse_navigation.rb
+++ b/plugins/discourse-ai/lib/agents/tools/search_discourse_navigation.rb
@@ -24,7 +24,7 @@ module DiscourseAi
{
name: name,
description:
- "Find verified navigation URLs on this site, including available plugin pages. Use concise keywords for the destination or task, such as 'billing subscription' or 'themes'. Reuse returned URLs exactly. No results means no verified destination was found; never invent a URL.",
+ "Find verified navigation URLs on this site, including available plugin pages. Use concise keywords for the destination or task, such as 'billing subscription' or 'themes'. Reuse returned URLs exactly. Optionally provide a URL to verify against available navigation destinations and category URLs. If verification returns a canonical_url, use it unchanged. If neither a destination nor a canonical_url is returned, no verified destination was found; never invent a URL.",
parameters: [
{
name: "query",
@@ -32,6 +32,12 @@ module DiscourseAi
type: "string",
required: true,
},
+ {
+ name: "url",
+ description:
+ "An absolute site URL to verify before linking to it. Verification requires an exact match, including the absence of extra IDs, path segments, query parameters, or fragments.",
+ type: "string",
+ },
],
}
end
@@ -60,10 +66,10 @@ module DiscourseAi
end
terms = query.downcase.split(/\s+/).uniq
+ available_destinations =
+ destinations.select { |destination| destination.available?(guardian) }
matches =
- destinations.filter_map do |destination|
- next if !destination.available?(guardian)
-
+ available_destinations.filter_map do |destination|
search_text = destination.search_text
score = terms.count { |term| search_text.include?(term) }
next if score.zero?
@@ -78,11 +84,40 @@ module DiscourseAi
.map { |_, destination| destination.to_h }
@result_count = results.length
- { destinations: results }
+ result = { destinations: results }
+ if parameters[:url].present?
+ result[:url_verification] = verify_url(parameters[:url].to_s, available_destinations)
+ end
+ result
end
private
+ def verify_url(url, available_destinations)
+ canonical_urls = available_destinations.map { |destination| destination.to_h[:url] }
+
+ category_prefix = "#{Discourse.base_url}/c/"
+ if url.start_with?(category_prefix)
+ category_ids = url.delete_prefix(category_prefix).split(%r{[/?#]}).grep(/\A\d+\z/)
+ canonical_urls.concat(
+ Category
+ .secured(guardian)
+ .where(id: category_ids)
+ .map { |category| "#{Discourse.base_url_no_prefix}#{category.url}" },
+ )
+ end
+
+ canonical_url = canonical_urls.find { |candidate| candidate == url }
+ canonical_url ||=
+ canonical_urls
+ .sort_by { |candidate| -candidate.length }
+ .find do |candidate|
+ %w[/ ? #].any? { |separator| url.start_with?("#{candidate}#{separator}") }
+ end
+
+ { verified: canonical_url == url, canonical_url: canonical_url }
+ end
+
def destinations
core =
CORE_PATHS.map do |id, path|
diff --git a/plugins/discourse-ai/lib/ai_bot/playground.rb b/plugins/discourse-ai/lib/ai_bot/playground.rb
index d1111f1ea1a..4e4e4b75fb0 100644
--- a/plugins/discourse-ai/lib/ai_bot/playground.rb
+++ b/plugins/discourse-ai/lib/ai_bot/playground.rb
@@ -509,6 +509,7 @@ module DiscourseAi
silent_mode: false,
feature_name: nil,
existing_reply_post: nil,
+ append_to_existing_reply: false,
cancel_manager: nil,
attributed_user: nil,
feature_context: nil,
@@ -577,6 +578,13 @@ module DiscourseAi
if existing_reply_post.user_id != reply_user.id
raise Discourse::InvalidParameters.new(:reply_post_id)
end
+
+ if append_to_existing_reply
+ reply << existing_reply_post.raw << "\n\n"
+ previous_custom_prompts =
+ existing_reply_post.post_custom_prompt&.custom_prompt.presence ||
+ [[existing_reply_post.raw, reply_user.username]]
+ end
end
stream_reply = post.topic.private_message? if stream_reply.nil?
@@ -602,8 +610,10 @@ module DiscourseAi
reply_post = existing_reply_post
if reply_post
- reply_post.update_columns(raw: "", cooked: "")
- reply_post.post_custom_prompt = nil
+ if !append_to_existing_reply
+ reply_post.update_columns(raw: "", cooked: "")
+ reply_post.post_custom_prompt = nil
+ end
else
reply_post =
PostCreator.create!(
@@ -626,7 +636,7 @@ module DiscourseAi
publish_update(
reply_post,
payload: {
- raw: "",
+ raw: reply.dup,
},
user_ids: stream_user_ids,
group_ids: stream_group_ids,
@@ -720,7 +730,7 @@ module DiscourseAi
)
elsif existing_reply_post
reply_post = existing_reply_post
- reply_post.post_custom_prompt = nil
+ reply_post.post_custom_prompt = nil if !append_to_existing_reply
reply_post.revise(
bot.bot_user,
{ raw: reply },
@@ -745,9 +755,10 @@ module DiscourseAi
# a bit messy internally, but this is how we tell
is_thinking = new_custom_prompts.any? { |prompt| prompt[4].present? }
- if is_thinking || new_custom_prompts.length > 1
+ if previous_custom_prompts || is_thinking || new_custom_prompts.length > 1
reply_post.post_custom_prompt ||= reply_post.build_post_custom_prompt(custom_prompt: [])
- prompt = reply_post.post_custom_prompt.custom_prompt || []
+ prompt =
+ (previous_custom_prompts || reply_post.post_custom_prompt.custom_prompt || []).dup
prompt.concat(new_custom_prompts)
reply_post.post_custom_prompt.update!(custom_prompt: prompt)
end
diff --git a/plugins/discourse-ai/spec/lib/agents/tools/create_category_spec.rb b/plugins/discourse-ai/spec/lib/agents/tools/create_category_spec.rb
index 66bfcc4c6f9..421a99ea8e8 100644
--- a/plugins/discourse-ai/spec/lib/agents/tools/create_category_spec.rb
+++ b/plugins/discourse-ai/spec/lib/agents/tools/create_category_spec.rb
@@ -28,17 +28,22 @@ RSpec.describe DiscourseAi::Agents::Tools::CreateCategory do
expect(category.name).to eq("What we do")
expect(category.user_id).to eq(admin.id)
expect(category.description).to include("All about our mission")
- expect(result[:url]).to eq(category.url)
+ expect(result[:url]).to eq("#{Discourse.base_url}/c/what-we-do/#{category.id}")
end
- it "creates a subcategory under the given parent" do
- parent = Fabricate(:category)
+ it "creates a subcategory with a full URL on a subfolder installation" do
+ set_subfolder "/forum"
+ parent = Fabricate(:category, name: "Support")
result =
tool(name: "How to use the forum", parent_category_id: parent.id, reason: "Setup").invoke
expect(result[:status]).to eq("success")
- expect(Category.find(result[:category_id]).parent_category_id).to eq(parent.id)
+ category = Category.find(result[:category_id])
+ expect(category.parent_category_id).to eq(parent.id)
+ expect(result[:url]).to eq(
+ "#{Discourse.base_url_no_prefix}/forum/c/support/how-to-use-the-forum/#{category.id}",
+ )
end
it "applies custom colors, stripping a leading # from hex codes" do
diff --git a/plugins/discourse-ai/spec/lib/agents/tools/search_discourse_navigation_spec.rb b/plugins/discourse-ai/spec/lib/agents/tools/search_discourse_navigation_spec.rb
index 6153094a8c6..f7630ed37d3 100644
--- a/plugins/discourse-ai/spec/lib/agents/tools/search_discourse_navigation_spec.rb
+++ b/plugins/discourse-ai/spec/lib/agents/tools/search_discourse_navigation_spec.rb
@@ -6,9 +6,9 @@ RSpec.describe DiscourseAi::Agents::Tools::SearchDiscourseNavigation do
before { enable_current_plugin }
- def search(query, user: admin)
+ def search(query, user: admin, url: nil)
described_class.new(
- { query: query },
+ { query: query, url: url },
bot_user: Discourse.system_user,
llm: nil,
context: DiscourseAi::Agents::BotContext.new(user: user),
@@ -40,6 +40,55 @@ RSpec.describe DiscourseAi::Agents::Tools::SearchDiscourseNavigation do
expect(search("backups")[:destinations].first[:url]).not_to eq(original_url)
end
+ it "verifies exact navigation URLs and corrects appended numbers" do
+ url = "#{Discourse.base_url}/admin/backups"
+
+ expect(search("backups", url: url)[:url_verification]).to eq(
+ verified: true,
+ canonical_url: url,
+ )
+ expect(search("backups", url: "#{url}/123")[:url_verification]).to eq(
+ verified: false,
+ canonical_url: url,
+ )
+ end
+
+ it "verifies subcategory URLs without allowing appended IDs, queries, or fragments" do
+ set_subfolder "/community"
+ parent = Fabricate(:category, name: "Support")
+ category = Fabricate(:category, name: "Questions", parent_category: parent)
+ url = "#{Discourse.base_url}/c/support/questions/#{category.id}"
+
+ expect(search("Questions", url: url)[:url_verification]).to eq(
+ verified: true,
+ canonical_url: url,
+ )
+ ["/#{category.id}", "?page=2", "#123"].each do |suffix|
+ expect(search("Questions", url: "#{url}#{suffix}")[:url_verification]).to eq(
+ verified: false,
+ canonical_url: url,
+ )
+ end
+ end
+
+ it "leaves unavailable, unknown, and external destinations unverified" do
+ SiteSetting.tagging_enabled = false
+ category = Fabricate(:category)
+
+ [
+ "#{Discourse.base_url}/tags",
+ "#{Discourse.base_url}/admin/unknown",
+ "#{Discourse.base_url}/admin/backups123",
+ "https://example.com#{category.url}",
+ "#{Discourse.base_url}/c/wrong-slug/#{category.id}",
+ ].each do |url|
+ expect(search("categories", url: url)[:url_verification]).to eq(
+ verified: false,
+ canonical_url: nil,
+ )
+ end
+ end
+
it "omits destinations disabled on this site" do
SiteSetting.tagging_enabled = false
diff --git a/plugins/discourse-ai/spec/lib/modules/ai_bot/jobs/regular/resume_ai_tool_approval_spec.rb b/plugins/discourse-ai/spec/lib/modules/ai_bot/jobs/regular/resume_ai_tool_approval_spec.rb
index 731dce58af6..79b3f3e4e6a 100644
--- a/plugins/discourse-ai/spec/lib/modules/ai_bot/jobs/regular/resume_ai_tool_approval_spec.rb
+++ b/plugins/discourse-ai/spec/lib/modules/ai_bot/jobs/regular/resume_ai_tool_approval_spec.rb
@@ -62,28 +62,44 @@ RSpec.describe Jobs::ResumeAiToolApproval do
end
describe "#execute" do
- it "continues once with the actual result and the original user's authorization" do
+ it "continues once in the approval post with the actual result and original authorization" do
reviewable.perform(admin, :approve, post_id: approval_post.id)
category = Category.find_by!(name: "Bug reports")
+ original_raw = approval_post.reload.raw
prompts = nil
- DiscourseAi::Completions::Llm.with_prepared_responses(
- ["The category is ready."],
- ) do |_, _, captured_prompts|
- job.execute(reviewable_id: reviewable.id)
- job.execute(reviewable_id: reviewable.id)
- prompts = captured_prompts
- end
+ messages =
+ MessageBus.track_publish do
+ expect {
+ DiscourseAi::Completions::Llm.with_prepared_responses(
+ ["The category is ready."],
+ ) do |_, _, captured_prompts|
+ job.execute(reviewable_id: reviewable.id)
+ job.execute(reviewable_id: reviewable.id)
+ prompts = captured_prompts
+ end
+ }.not_to change { topic.posts.count }
+ end
expect(prompts.size).to eq(1)
decision = JSON.parse(prompts.first.messages.last[:content])
expect(decision).to include(
"tool" => "create_category",
"decision" => "approved",
- "result" => include("category_id" => category.id),
+ "result" =>
+ include(
+ "category_id" => category.id,
+ "url" => "#{Discourse.base_url}/c/bug-reports/#{category.id}",
+ ),
)
reply = topic.posts.order(:post_number).last
- expect(reply.raw).to eq("The category is ready.")
+ expect(reply.id).to eq(approval_post.id)
+ expect(reply.raw).to eq("#{original_raw}\n\nThe category is ready.")
+ updates =
+ messages.select { |message| message.channel == "discourse-ai/ai-bot/topic/#{topic.id}" }
+ expect(updates).to be_present
+ expect(updates.map { |message| message.data[:post_id] }.uniq).to eq([approval_post.id])
+ expect(updates.filter_map { |message| message.data[:raw] }).to all(start_with(original_raw))
expect(reply.user_id).to eq(ai_agent.user_id)
expect(
reply.custom_fields[DiscourseAi::AiBot::POST_AI_AGENT_AUTHORIZATION_USER_ID_FIELD].to_i,
@@ -93,6 +109,7 @@ RSpec.describe Jobs::ResumeAiToolApproval do
it "continues after rejection without executing the tool" do
reviewable.perform(admin, :reject, post_id: approval_post.id)
+ original_raw = approval_post.reload.raw
prompts = nil
DiscourseAi::Completions::Llm.with_prepared_responses(
@@ -106,8 +123,9 @@ RSpec.describe Jobs::ResumeAiToolApproval do
expect(decision).to include("decision" => "rejected", "result" => nil)
expect(prompts.first.messages.first[:content]).to include("do not retry it")
expect(topic.posts.order(:post_number).last.raw).to eq(
- "I will leave the categories unchanged.",
+ "#{original_raw}\n\nI will leave the categories unchanged.",
)
+ expect(topic.posts.order(:post_number).last.id).to eq(approval_post.id)
expect(Category.exists?(name: "Bug reports")).to eq(false)
end
@@ -132,7 +150,9 @@ RSpec.describe Jobs::ResumeAiToolApproval do
next_reviewable = ReviewableAiToolAction.order(:id).last
next_approval_post = topic.posts.order(:post_number).last
expect(next_reviewable).to be_pending
+ expect(next_approval_post.id).to eq(approval_post.id)
expect(next_approval_post.raw).to include(
+ "data-ai-tool-approval-reviewable-id='#{reviewable.id}'",
"data-ai-tool-approval-reviewable-id='#{next_reviewable.id}'",
)
expect(Category.exists?(name: "Feature requests")).to eq(false)
@@ -144,12 +164,34 @@ RSpec.describe Jobs::ResumeAiToolApproval do
expect(Category.exists?(name: "Feature requests")).to eq(true)
reply = topic.posts.order(:post_number).last
- expect(reply.raw).to eq("Both categories are ready.")
+ expect(reply.id).to eq(approval_post.id)
+ expect(reply.raw).to start_with(next_approval_post.raw)
+ expect(reply.raw).to end_with("Both categories are ready.")
+ expect(reply.post_custom_prompt.custom_prompt.last.first).to eq("Both categories are ready.")
expect(
reply.custom_fields[DiscourseAi::AiBot::POST_AI_AGENT_AUTHORIZATION_USER_ID_FIELD].to_i,
).to eq(requester.id)
end
+ it "appends in public topics and preserves the previous custom prompt" do
+ topic.update!(archetype: Archetype.default)
+ original_raw = approval_post.raw
+ previous_prompt = [["I will create the category.", ai_agent.user.username]]
+ approval_post.create_post_custom_prompt!(custom_prompt: previous_prompt)
+ reviewable.perform(admin, :approve, post_id: approval_post.id)
+
+ expect {
+ DiscourseAi::Completions::Llm.with_prepared_responses(["The category is ready."]) do
+ job.execute(reviewable_id: reviewable.id)
+ end
+ }.not_to change { topic.posts.count }
+
+ expect(approval_post.reload.raw).to eq("#{original_raw}\n\nThe category is ready.")
+ prompt = approval_post.post_custom_prompt.custom_prompt
+ expect(prompt.first).to eq(previous_prompt.first)
+ expect(prompt.last.first).to eq("The category is ready.")
+ end
+
it "skips pending approvals" do
approval_post