Skip to content
This repository was archived by the owner on Jul 22, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/ai_bot/playground.rb
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,8 @@ def self.reply_to_post(
add_user_to_pm: false,
stream_reply: false,
auto_set_title: false,
silent_mode: false
silent_mode: false,
feature_name: nil
)
ai_persona = AiPersona.find_by(id: persona_id)
raise Discourse::InvalidParameters.new(:persona_id) if !ai_persona
Expand All @@ -210,6 +211,7 @@ def self.reply_to_post(
stream_reply: stream_reply,
auto_set_title: auto_set_title,
silent_mode: silent_mode,
feature_name: feature_name,
)
rescue => e
if Rails.env.test?
Expand Down Expand Up @@ -380,6 +382,7 @@ def reply_to(
stream_reply: nil,
auto_set_title: true,
silent_mode: false,
feature_name: nil,
&blk
)
# this is a multithreading issue
Expand Down Expand Up @@ -414,6 +417,7 @@ def reply_to(
DiscourseAi::Personas::BotContext.new(
post: post,
custom_instructions: custom_instructions,
feature_name: feature_name,
messages:
DiscourseAi::Completions::PromptMessagesBuilder.messages_from_post(
post,
Expand Down
1 change: 1 addition & 0 deletions lib/automation/llm_persona_triage.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ def self.handle(post:, persona_id:, whisper: false, silent_mode: false, automati
persona_id: persona_id,
whisper: whisper,
silent_mode: silent_mode,
feature_name: "automation - #{automation&.name}",
)
rescue => e
Discourse.warn_exception(
Expand Down
46 changes: 37 additions & 9 deletions spec/lib/discourse_automation/llm_persona_triage_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
fab!(:user)
fab!(:bot_user) { Fabricate(:user) }

fab!(:llm_model) do
Fabricate(:llm_model, provider: "anthropic", name: "claude-3-opus", enabled_chat_bot: true)
end
fab!(:llm_model) { Fabricate(:anthropic_model, name: "claude-3-opus", enabled_chat_bot: true) }

fab!(:ai_persona) do
persona =
Expand All @@ -25,7 +23,9 @@
persona
end

let(:automation) { Fabricate(:automation, script: "llm_persona_triage", enabled: true) }
let(:automation) do
Fabricate(:automation, name: "my automation", script: "llm_persona_triage", enabled: true)
end

def add_automation_field(name, value, type: "text")
automation.fields.create!(
Expand All @@ -49,12 +49,40 @@ def add_automation_field(name, value, type: "text")
it "can respond to a post using the specified persona" do
post = Fabricate(:post, raw: "This is a test post that needs triage")

response_text = "I've analyzed your post and can help with that."
response_text = "I analyzed your post and can help with that."

DiscourseAi::Completions::Llm.with_prepared_responses([response_text]) do
automation.running_in_background!
automation.trigger!({ "post" => post })
end
body = (<<~STRING).strip
event: message_start
data: {"type": "message_start", "message": {"id": "msg_1nZdL29xx5MUA1yADyHTEsnR8uuvGzszyY", "type": "message", "role": "assistant", "content": [], "model": "claude-3-opus-20240229", "stop_reason": null, "stop_sequence": null, "usage": {"input_tokens": 25, "output_tokens": 1}}}

event: content_block_start
data: {"type": "content_block_start", "index":0, "content_block": {"type": "text", "text": ""}}

event: ping
data: {"type": "ping"}

event: content_block_delta
data: {"type": "content_block_delta", "index": 0, "delta": {"type": "text_delta", "text": "#{response_text}"}}

event: content_block_stop
data: {"type": "content_block_stop", "index": 0}

event: message_delta
data: {"type": "message_delta", "delta": {"stop_reason": "end_turn", "stop_sequence":null, "usage":{"output_tokens": 15}}}

event: message_stop
data: {"type": "message_stop"}
STRING

stub_request(:post, "https://api.anthropic.com/v1/messages").to_return(body: body)

automation.running_in_background!
automation.trigger!({ "post" => post })

log = AiApiAuditLog.last
expect(log).to be_present
expect(log.user_id).to eq(post.user_id)
expect(log.feature_name).to eq("automation - #{automation.name}")

topic = post.topic.reload
last_post = topic.posts.order(:post_number).last
Expand Down
Loading