2024-01-04 10:44:07 -03:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module DiscourseAi
|
|
|
|
module AiBot
|
|
|
|
class Playground
|
2024-10-30 20:24:39 +11:00
|
|
|
BYPASS_AI_REPLY_CUSTOM_FIELD = "discourse_ai_bypass_ai_reply"
|
2025-03-12 18:32:02 +11:00
|
|
|
BOT_USER_PREF_ID_CUSTOM_FIELD = "discourse_ai_bot_user_pref_id"
|
2025-04-24 17:22:19 +11:00
|
|
|
# 10 minutes is enough for vast majority of cases
|
|
|
|
# there is a small chance that some reasoning models may take longer
|
|
|
|
MAX_STREAM_DELAY_SECONDS = 600
|
2024-10-30 20:24:39 +11:00
|
|
|
|
2024-02-15 16:37:59 +11:00
|
|
|
attr_reader :bot
|
|
|
|
|
2024-01-04 10:44:07 -03:00
|
|
|
# An abstraction to manage the bot and topic interactions.
|
|
|
|
# The bot will take care of completions while this class updates the topic title
|
|
|
|
# and stream replies.
|
|
|
|
|
2024-05-07 10:30:39 +10:00
|
|
|
def self.find_chat_persona(message, channel, user)
|
2024-05-06 09:49:02 +10:00
|
|
|
if channel.direct_message_channel?
|
2024-10-16 07:20:31 +11:00
|
|
|
AiPersona
|
|
|
|
.allowed_modalities(allow_chat_direct_messages: true)
|
|
|
|
.find do |p|
|
|
|
|
p[:user_id].in?(channel.allowed_user_ids) && (user.group_ids & p[:allowed_group_ids])
|
|
|
|
end
|
2024-05-07 10:30:39 +10:00
|
|
|
else
|
|
|
|
# let's defer on the parse if there is no @ in the message
|
|
|
|
if message.message.include?("@")
|
|
|
|
mentions = message.parsed_mentions.parsed_direct_mentions
|
|
|
|
if mentions.present?
|
2024-10-16 07:20:31 +11:00
|
|
|
AiPersona
|
|
|
|
.allowed_modalities(allow_chat_channel_mentions: true)
|
|
|
|
.find { |p| p[:username].in?(mentions) && (user.group_ids & p[:allowed_group_ids]) }
|
2024-05-06 09:49:02 +10:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-05-07 10:30:39 +10:00
|
|
|
def self.schedule_chat_reply(message, channel, user, context)
|
|
|
|
return if !SiteSetting.ai_bot_enabled
|
2024-10-16 07:20:31 +11:00
|
|
|
|
|
|
|
all_chat =
|
|
|
|
AiPersona.allowed_modalities(
|
|
|
|
allow_chat_channel_mentions: true,
|
|
|
|
allow_chat_direct_messages: true,
|
|
|
|
)
|
|
|
|
return if all_chat.blank?
|
|
|
|
return if all_chat.any? { |m| m[:user_id] == user.id }
|
2024-05-07 10:30:39 +10:00
|
|
|
|
|
|
|
persona = find_chat_persona(message, channel, user)
|
|
|
|
return if !persona
|
|
|
|
|
2024-05-21 17:17:02 +10:00
|
|
|
post_ids = nil
|
|
|
|
post_ids = context.dig(:context, :post_ids) if context.is_a?(Hash)
|
|
|
|
|
2024-05-07 10:30:39 +10:00
|
|
|
::Jobs.enqueue(
|
|
|
|
:create_ai_chat_reply,
|
|
|
|
channel_id: channel.id,
|
|
|
|
message_id: message.id,
|
|
|
|
persona_id: persona[:id],
|
2024-05-21 17:17:02 +10:00
|
|
|
context_post_ids: post_ids,
|
2024-05-07 10:30:39 +10:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2024-02-28 16:46:32 +11:00
|
|
|
def self.is_bot_user_id?(user_id)
|
2024-03-05 10:02:49 +11:00
|
|
|
# this will catch everything and avoid any feedback loops
|
|
|
|
# we could get feedback loops between say discobot and ai-bot or third party plugins
|
|
|
|
# and bots
|
|
|
|
user_id.to_i <= 0
|
2024-02-28 16:46:32 +11:00
|
|
|
end
|
2024-02-15 16:37:59 +11:00
|
|
|
|
2025-03-12 18:32:02 +11:00
|
|
|
def self.get_bot_user(post:, all_llm_users:, mentionables:)
|
|
|
|
bot_user = nil
|
|
|
|
if post.topic.private_message?
|
|
|
|
# this ensures that we reply using the correct llm
|
|
|
|
# 1. if we have a preferred llm user we use that
|
|
|
|
# 2. if we don't just take first topic allowed user
|
|
|
|
# 3. if we don't have that we take the first mentionable
|
|
|
|
bot_user = nil
|
|
|
|
if preferred_user =
|
|
|
|
all_llm_users.find { |id, username|
|
|
|
|
id == post.topic.custom_fields[BOT_USER_PREF_ID_CUSTOM_FIELD].to_i
|
|
|
|
}
|
|
|
|
bot_user = User.find_by(id: preferred_user[0])
|
|
|
|
end
|
|
|
|
bot_user ||=
|
|
|
|
post.topic.topic_allowed_users.where(user_id: all_llm_users.map(&:first)).first&.user
|
|
|
|
bot_user ||=
|
|
|
|
post
|
|
|
|
.topic
|
|
|
|
.topic_allowed_users
|
|
|
|
.where(user_id: mentionables.map { |m| m[:user_id] })
|
|
|
|
.first
|
|
|
|
&.user
|
|
|
|
end
|
|
|
|
bot_user
|
|
|
|
end
|
|
|
|
|
2024-02-28 16:46:32 +11:00
|
|
|
def self.schedule_reply(post)
|
|
|
|
return if is_bot_user_id?(post.user_id)
|
2024-10-16 07:20:31 +11:00
|
|
|
mentionables = nil
|
2024-02-28 16:46:32 +11:00
|
|
|
|
2024-10-16 07:20:31 +11:00
|
|
|
if post.topic.private_message?
|
|
|
|
mentionables =
|
|
|
|
AiPersona.allowed_modalities(user: post.user, allow_personal_messages: true)
|
|
|
|
else
|
|
|
|
mentionables = AiPersona.allowed_modalities(user: post.user, allow_topic_mentions: true)
|
|
|
|
end
|
2024-02-15 16:37:59 +11:00
|
|
|
|
|
|
|
mentioned = nil
|
|
|
|
|
2024-11-26 07:19:56 +11:00
|
|
|
all_llm_users =
|
|
|
|
LlmModel
|
|
|
|
.where(enabled_chat_bot: true)
|
|
|
|
.joins(:user)
|
|
|
|
.pluck("users.id", "users.username_lower")
|
2024-10-16 07:20:31 +11:00
|
|
|
|
2025-03-12 18:32:02 +11:00
|
|
|
bot_user =
|
|
|
|
get_bot_user(post: post, all_llm_users: all_llm_users, mentionables: mentionables)
|
2024-02-15 16:37:59 +11:00
|
|
|
|
2024-11-26 07:19:56 +11:00
|
|
|
mentions = nil
|
|
|
|
if mentionables.present? || (bot_user && post.topic.private_message?)
|
2024-02-15 16:37:59 +11:00
|
|
|
mentions = post.mentions.map(&:downcase)
|
2024-03-19 20:15:12 +11:00
|
|
|
|
|
|
|
# in case we are replying to a post by a bot
|
2024-08-07 14:22:32 -03:00
|
|
|
if post.reply_to_post_number && post.reply_to_post&.user
|
2024-03-19 20:15:12 +11:00
|
|
|
mentions << post.reply_to_post.user.username_lower
|
|
|
|
end
|
2024-11-26 07:19:56 +11:00
|
|
|
end
|
2024-03-19 20:15:12 +11:00
|
|
|
|
2024-11-26 07:19:56 +11:00
|
|
|
if mentionables.present?
|
2024-02-28 16:46:32 +11:00
|
|
|
mentioned = mentionables.find { |mentionable| mentions.include?(mentionable[:username]) }
|
2024-02-15 16:37:59 +11:00
|
|
|
|
2024-02-28 16:46:32 +11:00
|
|
|
# direct PM to mentionable
|
|
|
|
if !mentioned && bot_user
|
|
|
|
mentioned = mentionables.find { |mentionable| bot_user.id == mentionable[:user_id] }
|
2024-02-15 16:37:59 +11:00
|
|
|
end
|
2024-02-28 16:46:32 +11:00
|
|
|
|
|
|
|
# public topic so we need to use the persona user
|
|
|
|
bot_user ||= User.find_by(id: mentioned[:user_id]) if mentioned
|
2024-02-15 16:37:59 +11:00
|
|
|
end
|
|
|
|
|
2025-03-04 12:22:30 +11:00
|
|
|
if !mentioned && bot_user && post.reply_to_post_number && !post.reply_to_post.user&.bot?
|
2024-03-19 20:15:12 +11:00
|
|
|
# replying to a non-bot user
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2024-02-15 16:37:59 +11:00
|
|
|
if bot_user
|
2024-11-26 07:19:56 +11:00
|
|
|
topic_persona_id = post.topic.custom_fields["ai_persona_id"]
|
2025-03-12 18:32:02 +11:00
|
|
|
topic_persona_id = topic_persona_id.to_i if topic_persona_id.present?
|
|
|
|
|
2024-11-26 07:19:56 +11:00
|
|
|
persona_id = mentioned&.dig(:id) || topic_persona_id
|
|
|
|
|
2024-02-15 16:37:59 +11:00
|
|
|
persona = nil
|
|
|
|
|
|
|
|
if persona_id
|
2025-03-31 14:42:33 -03:00
|
|
|
persona = DiscourseAi::Personas::Persona.find_by(user: post.user, id: persona_id.to_i)
|
2024-02-15 16:37:59 +11:00
|
|
|
end
|
|
|
|
|
|
|
|
if !persona && persona_name = post.topic.custom_fields["ai_persona"]
|
2025-03-31 14:42:33 -03:00
|
|
|
persona = DiscourseAi::Personas::Persona.find_by(user: post.user, name: persona_name)
|
2024-02-15 16:37:59 +11:00
|
|
|
end
|
|
|
|
|
2024-11-26 07:19:56 +11:00
|
|
|
# edge case, llm was mentioned in an ai persona conversation
|
2025-03-12 18:32:02 +11:00
|
|
|
if persona_id == topic_persona_id && post.topic.private_message? && persona &&
|
2024-11-26 07:19:56 +11:00
|
|
|
all_llm_users.present?
|
|
|
|
if !persona.force_default_llm && mentions.present?
|
|
|
|
mentioned_llm_user_id, _ =
|
|
|
|
all_llm_users.find { |id, username| mentions.include?(username) }
|
|
|
|
|
|
|
|
if mentioned_llm_user_id
|
|
|
|
bot_user = User.find_by(id: mentioned_llm_user_id) || bot_user
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2025-03-31 14:42:33 -03:00
|
|
|
persona ||= DiscourseAi::Personas::General
|
2024-02-15 16:37:59 +11:00
|
|
|
|
2024-10-16 07:20:31 +11:00
|
|
|
bot_user = User.find(persona.user_id) if persona && persona.force_default_llm
|
|
|
|
|
2025-03-31 14:42:33 -03:00
|
|
|
bot = DiscourseAi::Personas::Bot.as(bot_user, persona: persona.new)
|
2024-02-15 16:37:59 +11:00
|
|
|
new(bot).update_playground_with(post)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2025-03-12 18:32:02 +11:00
|
|
|
def self.reply_to_post(
|
|
|
|
post:,
|
|
|
|
user: nil,
|
|
|
|
persona_id: nil,
|
|
|
|
whisper: nil,
|
|
|
|
add_user_to_pm: false,
|
|
|
|
stream_reply: false,
|
2025-03-17 15:14:09 +11:00
|
|
|
auto_set_title: false,
|
|
|
|
silent_mode: false
|
2025-03-12 18:32:02 +11:00
|
|
|
)
|
2025-03-06 17:18:15 +11:00
|
|
|
ai_persona = AiPersona.find_by(id: persona_id)
|
|
|
|
raise Discourse::InvalidParameters.new(:persona_id) if !ai_persona
|
|
|
|
persona_class = ai_persona.class_instance
|
|
|
|
persona = persona_class.new
|
|
|
|
|
|
|
|
bot_user = user || ai_persona.user
|
|
|
|
raise Discourse::InvalidParameters.new(:user) if bot_user.nil?
|
2025-03-31 14:42:33 -03:00
|
|
|
bot = DiscourseAi::Personas::Bot.as(bot_user, persona: persona)
|
|
|
|
playground = new(bot)
|
2025-03-06 17:18:15 +11:00
|
|
|
|
2025-03-11 14:26:07 +11:00
|
|
|
playground.reply_to(
|
|
|
|
post,
|
|
|
|
whisper: whisper,
|
|
|
|
context_style: :topic,
|
|
|
|
add_user_to_pm: add_user_to_pm,
|
2025-03-12 18:32:02 +11:00
|
|
|
stream_reply: stream_reply,
|
|
|
|
auto_set_title: auto_set_title,
|
2025-03-17 15:14:09 +11:00
|
|
|
silent_mode: silent_mode,
|
2025-03-11 14:26:07 +11:00
|
|
|
)
|
2025-03-17 15:14:09 +11:00
|
|
|
rescue => e
|
|
|
|
if Rails.env.test?
|
|
|
|
p e
|
|
|
|
puts e.backtrace[0..10]
|
|
|
|
else
|
|
|
|
raise e
|
|
|
|
end
|
2025-03-06 17:18:15 +11:00
|
|
|
end
|
|
|
|
|
2024-01-04 10:44:07 -03:00
|
|
|
def initialize(bot)
|
|
|
|
@bot = bot
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_playground_with(post)
|
2024-09-03 15:52:20 +10:00
|
|
|
schedule_bot_reply(post) if can_attach?(post)
|
2024-01-04 10:44:07 -03:00
|
|
|
end
|
|
|
|
|
2024-11-29 06:26:48 +11:00
|
|
|
def title_playground(post, user)
|
2025-04-01 02:39:07 +11:00
|
|
|
messages =
|
|
|
|
DiscourseAi::Completions::PromptMessagesBuilder.messages_from_post(
|
|
|
|
post,
|
|
|
|
max_posts: 5,
|
|
|
|
bot_usernames: available_bot_usernames,
|
|
|
|
include_uploads: bot.persona.class.vision_enabled,
|
|
|
|
)
|
2024-01-04 10:44:07 -03:00
|
|
|
|
2025-03-31 14:42:33 -03:00
|
|
|
# conversation context may contain tool calls, and confusing user names
|
|
|
|
# clean it up
|
|
|
|
conversation = +""
|
|
|
|
messages.each do |context|
|
|
|
|
if context[:type] == :user
|
|
|
|
conversation << "User said:\n#{context[:content]}\n\n"
|
|
|
|
elsif context[:type] == :model
|
|
|
|
conversation << "Model said:\n#{context[:content]}\n\n"
|
2024-01-04 10:44:07 -03:00
|
|
|
end
|
2025-03-31 14:42:33 -03:00
|
|
|
end
|
|
|
|
|
|
|
|
system_insts = <<~TEXT.strip
|
|
|
|
You are titlebot. Given a conversation, you will suggest a title.
|
2025-04-15 16:33:44 +10:00
|
|
|
|
2025-03-31 14:42:33 -03:00
|
|
|
- You will never respond with anything but the suggested title.
|
|
|
|
- You will always match the conversation language in your title suggestion.
|
|
|
|
- Title will capture the essence of the conversation.
|
|
|
|
TEXT
|
|
|
|
|
|
|
|
instruction = <<~TEXT.strip
|
|
|
|
Given the following conversation:
|
2025-04-15 16:33:44 +10:00
|
|
|
|
2025-03-31 14:42:33 -03:00
|
|
|
{{{
|
|
|
|
#{conversation}
|
|
|
|
}}}
|
2025-04-15 16:33:44 +10:00
|
|
|
|
2025-03-31 14:42:33 -03:00
|
|
|
Reply only with a title that is 7 words or less.
|
|
|
|
TEXT
|
|
|
|
|
|
|
|
title_prompt =
|
|
|
|
DiscourseAi::Completions::Prompt.new(
|
|
|
|
system_insts,
|
|
|
|
messages: [type: :user, content: instruction],
|
|
|
|
topic_id: post.topic_id,
|
|
|
|
)
|
|
|
|
|
|
|
|
new_title =
|
|
|
|
bot
|
|
|
|
.llm
|
|
|
|
.generate(title_prompt, user: user, feature_name: "bot_title")
|
|
|
|
.strip
|
|
|
|
.split("\n")
|
|
|
|
.last
|
|
|
|
|
|
|
|
PostRevisor.new(post.topic.first_post, post.topic).revise!(
|
|
|
|
bot.bot_user,
|
|
|
|
title: new_title.sub(/\A"/, "").sub(/"\Z/, ""),
|
|
|
|
)
|
2024-09-03 15:52:20 +10:00
|
|
|
|
|
|
|
allowed_users = post.topic.topic_allowed_users.pluck(:user_id)
|
|
|
|
MessageBus.publish(
|
|
|
|
"/discourse-ai/ai-bot/topic/#{post.topic.id}",
|
|
|
|
{ title: post.topic.title },
|
|
|
|
user_ids: allowed_users,
|
|
|
|
)
|
2024-01-04 10:44:07 -03:00
|
|
|
end
|
|
|
|
|
2025-04-01 02:39:07 +11:00
|
|
|
def reply_to_chat_message(message, channel, context_post_ids)
|
|
|
|
persona_user = User.find(bot.persona.class.user_id)
|
2024-05-08 18:44:04 +10:00
|
|
|
|
2025-04-01 02:39:07 +11:00
|
|
|
participants = channel.user_chat_channel_memberships.map { |m| m.user.username }
|
2024-01-04 10:44:07 -03:00
|
|
|
|
2025-04-01 02:39:07 +11:00
|
|
|
context_post_ids = nil if !channel.direct_message_channel?
|
2024-02-15 16:37:59 +11:00
|
|
|
|
2025-04-01 02:39:07 +11:00
|
|
|
max_chat_messages = 40
|
2024-05-06 09:49:02 +10:00
|
|
|
if bot.persona.class.respond_to?(:max_context_posts)
|
2025-04-01 02:39:07 +11:00
|
|
|
max_chat_messages = bot.persona.class.max_context_posts || 40
|
2024-05-21 17:17:02 +10:00
|
|
|
end
|
|
|
|
|
2025-04-01 02:39:07 +11:00
|
|
|
if !channel.direct_message_channel?
|
|
|
|
# we are interacting via mentions ... strip mention
|
|
|
|
instruction_message = message.message.gsub(/@#{bot.bot_user.username}/i, "").strip
|
2024-05-06 09:49:02 +10:00
|
|
|
end
|
|
|
|
|
|
|
|
context =
|
2025-03-31 14:42:33 -03:00
|
|
|
DiscourseAi::Personas::BotContext.new(
|
2025-04-01 02:39:07 +11:00
|
|
|
participants: participants,
|
|
|
|
message_id: message.id,
|
|
|
|
channel_id: channel.id,
|
|
|
|
context_post_ids: context_post_ids,
|
|
|
|
messages:
|
|
|
|
DiscourseAi::Completions::PromptMessagesBuilder.messages_from_chat(
|
|
|
|
message,
|
|
|
|
channel: channel,
|
|
|
|
context_post_ids: context_post_ids,
|
|
|
|
include_uploads: bot.persona.class.vision_enabled,
|
|
|
|
max_messages: max_chat_messages,
|
|
|
|
bot_user_ids: available_bot_user_ids,
|
|
|
|
instruction_message: instruction_message,
|
|
|
|
),
|
2024-05-06 09:49:02 +10:00
|
|
|
user: message.user,
|
|
|
|
skip_tool_details: true,
|
|
|
|
)
|
|
|
|
|
|
|
|
reply = nil
|
|
|
|
guardian = Guardian.new(persona_user)
|
|
|
|
|
2024-05-08 18:44:04 +10:00
|
|
|
force_thread = message.thread_id.nil? && channel.direct_message_channel?
|
|
|
|
in_reply_to_id = channel.direct_message_channel? ? message.id : nil
|
|
|
|
|
2025-04-24 17:22:19 +11:00
|
|
|
streamer =
|
|
|
|
ChatStreamer.new(
|
|
|
|
message: message,
|
|
|
|
channel: channel,
|
|
|
|
guardian: guardian,
|
|
|
|
thread_id: message.thread_id,
|
|
|
|
in_reply_to_id: in_reply_to_id,
|
|
|
|
force_thread: force_thread,
|
|
|
|
)
|
|
|
|
|
2024-05-06 09:49:02 +10:00
|
|
|
new_prompts =
|
2025-04-15 17:13:20 +10:00
|
|
|
bot.reply(context) do |partial, cancel, placeholder, type|
|
2025-04-15 16:33:44 +10:00
|
|
|
# no support for tools or thinking by design
|
2025-04-15 17:13:20 +10:00
|
|
|
next if type == :thinking || type == :tool_details || type == :partial_tool
|
2025-04-24 17:22:19 +11:00
|
|
|
streamer.cancel = cancel
|
|
|
|
streamer << partial
|
|
|
|
break if streamer.cancelled
|
2024-05-06 09:49:02 +10:00
|
|
|
end
|
|
|
|
|
2025-04-24 17:22:19 +11:00
|
|
|
reply = streamer.reply
|
|
|
|
if new_prompts.length > 1 && reply
|
2024-05-06 09:49:02 +10:00
|
|
|
ChatMessageCustomPrompt.create!(message_id: reply.id, custom_prompt: new_prompts)
|
|
|
|
end
|
|
|
|
|
2025-04-24 17:22:19 +11:00
|
|
|
if streamer
|
|
|
|
streamer.done
|
|
|
|
streamer = nil
|
|
|
|
end
|
2024-05-06 09:49:02 +10:00
|
|
|
|
|
|
|
reply
|
2025-04-24 17:22:19 +11:00
|
|
|
ensure
|
|
|
|
streamer.done if streamer
|
2024-05-06 09:49:02 +10:00
|
|
|
end
|
|
|
|
|
2025-03-11 14:26:07 +11:00
|
|
|
def reply_to(
|
|
|
|
post,
|
|
|
|
custom_instructions: nil,
|
|
|
|
whisper: nil,
|
|
|
|
context_style: nil,
|
|
|
|
add_user_to_pm: true,
|
2025-03-12 18:32:02 +11:00
|
|
|
stream_reply: nil,
|
|
|
|
auto_set_title: true,
|
2025-03-17 15:14:09 +11:00
|
|
|
silent_mode: false,
|
2025-03-11 14:26:07 +11:00
|
|
|
&blk
|
|
|
|
)
|
2024-10-30 10:28:20 +11:00
|
|
|
# this is a multithreading issue
|
|
|
|
# post custom prompt is needed and it may not
|
|
|
|
# be properly loaded, ensure it is loaded
|
|
|
|
PostCustomPrompt.none
|
|
|
|
|
2025-03-17 15:14:09 +11:00
|
|
|
if silent_mode
|
|
|
|
auto_set_title = false
|
|
|
|
stream_reply = false
|
|
|
|
end
|
|
|
|
|
2024-05-06 09:49:02 +10:00
|
|
|
reply = +""
|
2024-11-19 09:22:39 +11:00
|
|
|
post_streamer = nil
|
2024-05-06 09:49:02 +10:00
|
|
|
|
|
|
|
post_type =
|
2025-03-06 09:41:09 +11:00
|
|
|
(
|
|
|
|
if (whisper || post.post_type == Post.types[:whisper])
|
|
|
|
Post.types[:whisper]
|
|
|
|
else
|
|
|
|
Post.types[:regular]
|
|
|
|
end
|
|
|
|
)
|
2024-05-06 09:49:02 +10:00
|
|
|
|
2025-04-01 02:39:07 +11:00
|
|
|
# safeguard
|
|
|
|
max_context_posts = 40
|
|
|
|
if bot.persona.class.respond_to?(:max_context_posts)
|
|
|
|
max_context_posts = bot.persona.class.max_context_posts || 40
|
|
|
|
end
|
|
|
|
|
2024-05-06 09:49:02 +10:00
|
|
|
context =
|
2025-03-31 14:42:33 -03:00
|
|
|
DiscourseAi::Personas::BotContext.new(
|
2025-04-01 02:39:07 +11:00
|
|
|
post: post,
|
|
|
|
custom_instructions: custom_instructions,
|
|
|
|
messages:
|
|
|
|
DiscourseAi::Completions::PromptMessagesBuilder.messages_from_post(
|
|
|
|
post,
|
|
|
|
style: context_style,
|
|
|
|
max_posts: max_context_posts,
|
|
|
|
include_uploads: bot.persona.class.vision_enabled,
|
|
|
|
bot_usernames: available_bot_usernames,
|
|
|
|
),
|
2024-05-06 09:49:02 +10:00
|
|
|
)
|
|
|
|
|
2024-02-15 16:37:59 +11:00
|
|
|
reply_user = bot.bot_user
|
|
|
|
if bot.persona.class.respond_to?(:user_id)
|
|
|
|
reply_user = User.find_by(id: bot.persona.class.user_id) || reply_user
|
|
|
|
end
|
|
|
|
|
2025-03-12 18:32:02 +11:00
|
|
|
stream_reply = post.topic.private_message? if stream_reply.nil?
|
2024-02-15 16:37:59 +11:00
|
|
|
|
|
|
|
# we need to ensure persona user is allowed to reply to the pm
|
2025-03-11 14:26:07 +11:00
|
|
|
if post.topic.private_message? && add_user_to_pm
|
2024-02-15 16:37:59 +11:00
|
|
|
if !post.topic.topic_allowed_users.exists?(user_id: reply_user.id)
|
|
|
|
post.topic.topic_allowed_users.create!(user_id: reply_user.id)
|
|
|
|
end
|
2025-03-12 18:32:02 +11:00
|
|
|
# edge case, maybe the llm user is missing?
|
|
|
|
if !post.topic.topic_allowed_users.exists?(user_id: bot.bot_user.id)
|
|
|
|
post.topic.topic_allowed_users.create!(user_id: bot.bot_user.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
# we store the id of the last bot_user, this is then used to give it preference
|
|
|
|
if post.topic.custom_fields[BOT_USER_PREF_ID_CUSTOM_FIELD].to_i != bot.bot_user.id
|
|
|
|
post.topic.custom_fields[BOT_USER_PREF_ID_CUSTOM_FIELD] = bot.bot_user.id
|
|
|
|
post.topic.save_custom_fields
|
|
|
|
end
|
2024-02-15 16:37:59 +11:00
|
|
|
end
|
2024-01-04 10:44:07 -03:00
|
|
|
|
2024-02-15 16:37:59 +11:00
|
|
|
if stream_reply
|
|
|
|
reply_post =
|
|
|
|
PostCreator.create!(
|
|
|
|
reply_user,
|
|
|
|
topic_id: post.topic_id,
|
|
|
|
raw: "",
|
|
|
|
skip_validations: true,
|
|
|
|
skip_jobs: true,
|
|
|
|
post_type: post_type,
|
2025-03-11 14:26:07 +11:00
|
|
|
skip_guardian: true,
|
2025-04-30 16:36:38 +10:00
|
|
|
custom_fields: {
|
2025-05-01 13:24:53 +10:00
|
|
|
DiscourseAi::AiBot::POST_AI_LLM_NAME_FIELD => bot.llm.llm_model.display_name,
|
2025-04-30 16:36:38 +10:00
|
|
|
},
|
2024-02-15 16:37:59 +11:00
|
|
|
)
|
2024-01-09 23:20:28 +11:00
|
|
|
|
2024-02-15 16:37:59 +11:00
|
|
|
publish_update(reply_post, { raw: reply_post.cooked })
|
|
|
|
|
|
|
|
redis_stream_key = "gpt_cancel:#{reply_post.id}"
|
2025-04-24 17:22:19 +11:00
|
|
|
Discourse.redis.setex(redis_stream_key, MAX_STREAM_DELAY_SECONDS, 1)
|
2024-02-15 16:37:59 +11:00
|
|
|
end
|
2024-01-04 10:44:07 -03:00
|
|
|
|
2025-04-01 02:39:07 +11:00
|
|
|
context.skip_tool_details ||= !bot.persona.class.tool_details
|
2024-06-11 18:14:14 +10:00
|
|
|
|
2024-11-19 09:22:39 +11:00
|
|
|
post_streamer = PostStreamer.new(delay: Rails.env.test? ? 0 : 0.5) if stream_reply
|
|
|
|
|
2025-03-04 12:22:30 +11:00
|
|
|
started_thinking = false
|
|
|
|
|
2024-01-04 10:44:07 -03:00
|
|
|
new_custom_prompts =
|
2024-10-30 10:28:20 +11:00
|
|
|
bot.reply(context) do |partial, cancel, placeholder, type|
|
2025-03-04 12:22:30 +11:00
|
|
|
if type == :thinking && !started_thinking
|
|
|
|
reply << "<details><summary>#{I18n.t("discourse_ai.ai_bot.thinking")}</summary>"
|
|
|
|
started_thinking = true
|
|
|
|
end
|
|
|
|
|
|
|
|
if type != :thinking && started_thinking
|
|
|
|
reply << "</details>\n\n"
|
|
|
|
started_thinking = false
|
|
|
|
end
|
|
|
|
|
2024-01-04 10:44:07 -03:00
|
|
|
reply << partial
|
|
|
|
raw = reply.dup
|
2024-11-14 06:58:24 +11:00
|
|
|
raw << "\n\n" << placeholder if placeholder.present?
|
2024-01-04 10:44:07 -03:00
|
|
|
|
DEV: artifact system update (#1096)
### Why
This pull request fundamentally restructures how AI bots create and update web artifacts to address critical limitations in the previous approach:
1. **Improved Artifact Context for LLMs**: Previously, artifact creation and update tools included the *entire* artifact source code directly in the tool arguments. This overloaded the Language Model (LLM) with raw code, making it difficult for the LLM to maintain a clear understanding of the artifact's current state when applying changes. The LLM would struggle to differentiate between the base artifact and the requested modifications, leading to confusion and less effective updates.
2. **Reduced Token Usage and History Bloat**: Including the full artifact source code in every tool interaction was extremely token-inefficient. As conversations progressed, this redundant code in the history consumed a significant number of tokens unnecessarily. This not only increased costs but also diluted the context for the LLM with less relevant historical information.
3. **Enabling Updates for Large Artifacts**: The lack of a practical diff or targeted update mechanism made it nearly impossible to efficiently update larger web artifacts. Sending the entire source code for every minor change was both computationally expensive and prone to errors, effectively blocking the use of AI bots for meaningful modifications of complex artifacts.
**This pull request addresses these core issues by**:
* Introducing methods for the AI bot to explicitly *read* and understand the current state of an artifact.
* Implementing efficient update strategies that send *targeted* changes rather than the entire artifact source code.
* Providing options to control the level of artifact context included in LLM prompts, optimizing token usage.
### What
The main changes implemented in this PR to resolve the above issues are:
1. **`Read Artifact` Tool for Contextual Awareness**:
- A new `read_artifact` tool is introduced, enabling AI bots to fetch and process the current content of a web artifact from a given URL (local or external).
- This provides the LLM with a clear and up-to-date representation of the artifact's HTML, CSS, and JavaScript, improving its understanding of the base to be modified.
- By cloning local artifacts, it allows the bot to work with a fresh copy, further enhancing context and control.
2. **Refactored `Update Artifact` Tool with Efficient Strategies**:
- The `update_artifact` tool is redesigned to employ more efficient update strategies, minimizing token usage and improving update precision:
- **`diff` strategy**: Utilizes a search-and-replace diff algorithm to apply only the necessary, targeted changes to the artifact's code. This significantly reduces the amount of code sent to the LLM and focuses its attention on the specific modifications.
- **`full` strategy**: Provides the option to replace the entire content sections (HTML, CSS, JavaScript) when a complete rewrite is required.
- Tool options enhance the control over the update process:
- `editor_llm`: Allows selection of a specific LLM for artifact updates, potentially optimizing for code editing tasks.
- `update_algorithm`: Enables choosing between `diff` and `full` update strategies based on the nature of the required changes.
- `do_not_echo_artifact`: Defaults to true, and by *not* echoing the artifact in prompts, it further reduces token consumption in scenarios where the LLM might not need the full artifact context for every update step (though effectiveness might be slightly reduced in certain update scenarios).
3. **System and General Persona Tool Option Visibility and Customization**:
- Tool options, including those for system personas, are made visible and editable in the admin UI. This allows administrators to fine-tune the behavior of all personas and their tools, including setting specific LLMs or update algorithms. This was previously limited or hidden for system personas.
4. **Centralized and Improved Content Security Policy (CSP) Management**:
- The CSP for AI artifacts is consolidated and made more maintainable through the `ALLOWED_CDN_SOURCES` constant. This improves code organization and future updates to the allowed CDN list, while maintaining the existing security posture.
5. **Codebase Improvements**:
- Refactoring of diff utilities, introduction of strategy classes, enhanced error handling, new locales, and comprehensive testing all contribute to a more robust, efficient, and maintainable artifact management system.
By addressing the issues of LLM context confusion, token inefficiency, and the limitations of updating large artifacts, this pull request significantly improves the practicality and effectiveness of AI bots in managing web artifacts within Discourse.
2025-02-04 16:27:27 +11:00
|
|
|
if blk && type != :tool_details && type != :partial_tool && type != :partial_invoke
|
|
|
|
blk.call(partial)
|
|
|
|
end
|
2024-10-30 10:28:20 +11:00
|
|
|
|
2024-02-15 16:37:59 +11:00
|
|
|
if stream_reply && !Discourse.redis.get(redis_stream_key)
|
2024-01-04 10:44:07 -03:00
|
|
|
cancel&.call
|
|
|
|
reply_post.update!(raw: reply, cooked: PrettyText.cook(reply))
|
2024-11-21 17:51:45 +11:00
|
|
|
# we do not break out, cause if we do
|
|
|
|
# we will not get results from bot
|
|
|
|
# leading to broken context
|
|
|
|
# we need to trust it to cancel at the endpoint
|
2024-01-04 10:44:07 -03:00
|
|
|
end
|
|
|
|
|
2024-11-19 09:22:39 +11:00
|
|
|
if post_streamer
|
|
|
|
post_streamer.run_later do
|
2025-04-24 17:22:19 +11:00
|
|
|
Discourse.redis.expire(redis_stream_key, MAX_STREAM_DELAY_SECONDS)
|
2024-11-19 09:22:39 +11:00
|
|
|
publish_update(reply_post, { raw: raw })
|
2024-02-15 16:37:59 +11:00
|
|
|
end
|
|
|
|
end
|
2024-01-04 10:44:07 -03:00
|
|
|
end
|
|
|
|
|
2025-03-17 15:14:09 +11:00
|
|
|
return if reply.blank? || silent_mode
|
2024-01-04 10:44:07 -03:00
|
|
|
|
2024-02-15 16:37:59 +11:00
|
|
|
if stream_reply
|
2024-11-19 09:22:39 +11:00
|
|
|
post_streamer.finish
|
|
|
|
post_streamer = nil
|
|
|
|
|
2024-02-15 16:37:59 +11:00
|
|
|
# land the final message prior to saving so we don't clash
|
|
|
|
reply_post.cooked = PrettyText.cook(reply)
|
|
|
|
publish_final_update(reply_post)
|
2024-01-04 10:44:07 -03:00
|
|
|
|
2024-02-15 16:37:59 +11:00
|
|
|
reply_post.revise(
|
|
|
|
bot.bot_user,
|
|
|
|
{ raw: reply },
|
|
|
|
skip_validations: true,
|
|
|
|
skip_revision: true,
|
|
|
|
)
|
|
|
|
else
|
|
|
|
reply_post =
|
|
|
|
PostCreator.create!(
|
|
|
|
reply_user,
|
|
|
|
topic_id: post.topic_id,
|
|
|
|
raw: reply,
|
|
|
|
skip_validations: true,
|
|
|
|
post_type: post_type,
|
2025-03-11 14:26:07 +11:00
|
|
|
skip_guardian: true,
|
2024-02-15 16:37:59 +11:00
|
|
|
)
|
|
|
|
end
|
2024-01-04 10:44:07 -03:00
|
|
|
|
2025-03-04 12:22:30 +11:00
|
|
|
# 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
|
2024-01-06 05:21:14 +11:00
|
|
|
reply_post.post_custom_prompt ||= reply_post.build_post_custom_prompt(custom_prompt: [])
|
|
|
|
prompt = reply_post.post_custom_prompt.custom_prompt || []
|
2024-01-04 10:44:07 -03:00
|
|
|
prompt.concat(new_custom_prompts)
|
2024-01-06 05:21:14 +11:00
|
|
|
reply_post.post_custom_prompt.update!(custom_prompt: prompt)
|
2024-01-04 10:44:07 -03:00
|
|
|
end
|
2024-01-06 05:21:14 +11:00
|
|
|
|
2024-12-10 05:59:19 +11:00
|
|
|
reply_post
|
|
|
|
rescue => e
|
|
|
|
if reply_post
|
|
|
|
details = e.message.to_s
|
|
|
|
reply = "#{reply}\n\n#{I18n.t("discourse_ai.ai_bot.reply_error", details: details)}"
|
|
|
|
reply_post.revise(
|
|
|
|
bot.bot_user,
|
|
|
|
{ raw: reply },
|
|
|
|
skip_validations: true,
|
|
|
|
skip_revision: true,
|
|
|
|
)
|
|
|
|
end
|
|
|
|
raise e
|
|
|
|
ensure
|
2024-03-04 09:56:59 +11:00
|
|
|
# since we are skipping validations and jobs we
|
|
|
|
# may need to fix participant count
|
2024-12-10 05:59:19 +11:00
|
|
|
if reply_post && reply_post.topic && reply_post.topic.private_message? &&
|
|
|
|
reply_post.topic.participant_count < 2
|
2024-03-04 09:56:59 +11:00
|
|
|
reply_post.topic.update!(participant_count: 2)
|
|
|
|
end
|
2024-11-19 09:22:39 +11:00
|
|
|
post_streamer&.finish(skip_callback: true)
|
2024-02-15 16:37:59 +11:00
|
|
|
publish_final_update(reply_post) if stream_reply
|
2025-03-12 18:32:02 +11:00
|
|
|
if reply_post && post.post_number == 1 && post.topic.private_message? && auto_set_title
|
2024-11-29 06:26:48 +11:00
|
|
|
title_playground(reply_post, post.user)
|
2024-09-03 15:52:20 +10:00
|
|
|
end
|
2024-01-04 10:44:07 -03:00
|
|
|
end
|
|
|
|
|
2024-03-08 06:37:23 +11:00
|
|
|
def available_bot_usernames
|
|
|
|
@bot_usernames ||=
|
2024-06-18 14:32:14 -03:00
|
|
|
AiPersona.joins(:user).pluck(:username).concat(available_bot_users.map(&:username))
|
2024-03-08 06:37:23 +11:00
|
|
|
end
|
|
|
|
|
2024-05-06 09:49:02 +10:00
|
|
|
def available_bot_user_ids
|
2024-06-18 14:32:14 -03:00
|
|
|
@bot_ids ||= AiPersona.joins(:user).pluck("users.id").concat(available_bot_users.map(&:id))
|
2024-05-06 09:49:02 +10:00
|
|
|
end
|
|
|
|
|
2024-01-04 10:44:07 -03:00
|
|
|
private
|
|
|
|
|
2024-06-18 14:32:14 -03:00
|
|
|
def available_bot_users
|
|
|
|
@available_bots ||=
|
|
|
|
User.joins("INNER JOIN llm_models llm ON llm.user_id = users.id").where(active: true)
|
|
|
|
end
|
|
|
|
|
2024-01-15 18:51:14 +11:00
|
|
|
def publish_final_update(reply_post)
|
|
|
|
return if @published_final_update
|
|
|
|
if reply_post
|
|
|
|
publish_update(reply_post, { cooked: reply_post.cooked, done: true })
|
|
|
|
# we subscribe at position -2 so we will always get this message
|
|
|
|
# moving all cooked on every page load is wasteful ... this means
|
|
|
|
# we have a benign message at the end, 2 is set to ensure last message
|
|
|
|
# is delivered
|
|
|
|
publish_update(reply_post, { noop: true })
|
|
|
|
@published_final_update = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-01-04 10:44:07 -03:00
|
|
|
def can_attach?(post)
|
|
|
|
return false if bot.bot_user.nil?
|
2024-02-15 16:37:59 +11:00
|
|
|
return false if post.topic.private_message? && post.post_type != Post.types[:regular]
|
2024-01-04 10:44:07 -03:00
|
|
|
return false if (SiteSetting.ai_bot_allowed_groups_map & post.user.group_ids).blank?
|
2024-10-30 20:24:39 +11:00
|
|
|
return false if post.custom_fields[BYPASS_AI_REPLY_CUSTOM_FIELD].present?
|
2024-01-04 10:44:07 -03:00
|
|
|
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2024-02-15 16:37:59 +11:00
|
|
|
def schedule_bot_reply(post)
|
|
|
|
persona_id =
|
2025-03-31 14:42:33 -03:00
|
|
|
DiscourseAi::Personas::Persona.system_personas[bot.persona.class] || bot.persona.class.id
|
2024-02-15 16:37:59 +11:00
|
|
|
::Jobs.enqueue(
|
|
|
|
:create_ai_reply,
|
2024-01-04 10:44:07 -03:00
|
|
|
post_id: post.id,
|
2024-02-15 16:37:59 +11:00
|
|
|
bot_user_id: bot.bot_user.id,
|
|
|
|
persona_id: persona_id,
|
2024-01-04 10:44:07 -03:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def context(topic)
|
|
|
|
{
|
|
|
|
site_url: Discourse.base_url,
|
|
|
|
site_title: SiteSetting.title,
|
|
|
|
site_description: SiteSetting.site_description,
|
|
|
|
time: Time.zone.now,
|
|
|
|
participants: topic.allowed_users.map(&:username).join(", "),
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def publish_update(bot_reply_post, payload)
|
2024-01-15 18:51:14 +11:00
|
|
|
payload = { post_id: bot_reply_post.id, post_number: bot_reply_post.post_number }.merge(
|
|
|
|
payload,
|
|
|
|
)
|
2024-01-04 10:44:07 -03:00
|
|
|
MessageBus.publish(
|
|
|
|
"discourse-ai/ai-bot/topic/#{bot_reply_post.topic_id}",
|
2024-01-15 18:51:14 +11:00
|
|
|
payload,
|
2024-01-04 10:44:07 -03:00
|
|
|
user_ids: bot_reply_post.topic.allowed_user_ids,
|
2024-01-15 18:51:14 +11:00
|
|
|
max_backlog_size: 2,
|
|
|
|
max_backlog_age: 60,
|
2024-01-04 10:44:07 -03:00
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|