Module: Brainiac::Plugins::Discord::Message
- Defined in:
- lib/brainiac/plugins/discord/message.rb
Overview
Discord message handler — the main dispatch function.
Handles incoming messages: authorization, cross-agent routing, project detection, worktree management, prompt building, agent spawning, and response monitoring.
Class Method Summary collapse
Class Method Details
.handle(message, agent_key, bot_token, bot_user_id) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/brainiac/plugins/discord/message.rb', line 15 def handle(, agent_key, bot_token, bot_user_id) channel_id = ["channel_id"] = ["id"] = ["author"] content = ["content"] || "" is_bot = !["bot"].nil? sender_agent_key = Gateway.detect_sender_agent(, agent_key) if is_bot return if is_bot && !sender_agent_key mentions = ["mentions"] || [] mentioned = mentions.any? { |m| m["id"].to_s == bot_user_id.to_s } || content.match?(/<@!?#{Regexp.escape(bot_user_id.to_s)}>/) || role_mentioned?(, content, agent_key) return if sender_agent_key && !validate_cross_agent_dispatch(sender_agent_key, agent_key, mentioned, content, channel_id) is_reply_to_bot, = detect_reply_to_bot(, channel_id, mentioned, bot_token, bot_user_id) channel_info, is_thread, is_dm, in_own_thread = detect_channel_context(, channel_id, mentioned, is_reply_to_bot, bot_token, bot_user_id) return if should_stand_down?(in_own_thread, mentioned, is_reply_to_bot, is_bot, agent_key, mentions, content) is_thread_participant = !mentioned && !in_own_thread && is_thread && thread_participant?(channel_id, , bot_user_id, bot_token) return unless mentioned || in_own_thread || is_dm || is_reply_to_bot || is_thread_participant # When another agent is explicitly mentioned, thread participants who weren't # addressed should not activate — the message was directed at someone specific. return if is_thread_participant && other_agent_mentioned?(mentions, content, agent_key) record_human_comment("discord-#{channel_id}") unless is_bot clean_content = prepare_content(content, bot_user_id) = (, , agent_key) clean_content = (clean_content, ) return if clean_content.empty? && .empty? reply_context = build_reply_context() discord_user = ["username"] discord_user_id = ["id"] agent_name = agent_display_name(agent_key) || agent_key.capitalize channel_info, is_thread, is_dm = ensure_channel_info(channel_info, is_thread, is_dm, channel_id, bot_token) parent_channel_id = is_thread ? channel_info&.dig("parent_id") || channel_id : channel_id channel_history = Api.fetch_channel_history(channel_id, , token: bot_token, limit: is_thread ? 25 : 10) location = is_dm ? "DM" : (is_thread ? "thread" : "channel") # rubocop:disable Style/NestedTernaryOperator LOG.info "[Discord:#{agent_name}] Message from #{discord_user} in #{location} #{channel_id}: #{clean_content[0..100]}" if defined?(LOG) reload_projects! reload_agent_registry! Config.reload! return unless (discord_user, discord_user_id, , channel_id, , agent_name, bot_token) = (clean_content) project_key, project_config = resolve_project([:project], parent_channel_id, agent_name, channel_id, , bot_token) # Thread owners bypass intent checking ONLY when they're the sole agent in # the thread (1-on-1 with the human). If other agents have participated, # everyone goes through intent checking unless explicitly @mentioned. solo_thread_owner = in_own_thread && solo_in_thread?(channel_history, agent_key) if in_own_thread && defined?(LOG) LOG.info "[Discord:#{agent_name}] Own thread — #{solo_thread_owner ? "solo (bypassing intent)" : "multi-agent (intent check applies)"}" end route_dispatch( agent_key: agent_key, agent_name: agent_name, bot_token: bot_token, is_bot: is_bot, channel_id: channel_id, message_id: , message: , clean_content: clean_content, clean_content_for_prompt: [:clean_text], chat_mode: [:chat_mode], is_thread: is_thread, is_dm: is_dm, channel_info: channel_info, parent_channel_id: parent_channel_id, discord_user: discord_user, reply_context: reply_context, channel_history: channel_history, project_key: project_key, project_config: project_config, attachment_paths: , directly_addressed: mentioned || is_reply_to_bot || is_dm || solo_thread_owner ) end |