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
# File 'lib/brainiac/plugins/discord/message.rb', line 15

def handle(message, agent_key, bot_token, bot_user_id)
  channel_id = message["channel_id"]
  message_id = message["id"]
  author = message["author"]
  content = message["content"] || ""
  is_bot = !author["bot"].nil?

  sender_agent_key = Gateway.detect_sender_agent(author, agent_key) if is_bot
  return if is_bot && !sender_agent_key

  mentions = message["mentions"] || []
  mentioned = mentions.any? { |m| m["id"].to_s == bot_user_id.to_s } ||
              content.match?(/<@!?#{Regexp.escape(bot_user_id.to_s)}>/)
  return if sender_agent_key && !validate_cross_agent_dispatch(sender_agent_key, agent_key, mentioned, content, channel_id)

  is_reply_to_bot, referenced_message = detect_reply_to_bot(message, channel_id, mentioned, bot_token, bot_user_id)
  channel_info, is_thread, is_dm, in_own_thread = detect_channel_context(message, 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)
  return unless mentioned || in_own_thread || is_dm || is_reply_to_bot

  record_human_comment("discord-#{channel_id}") unless is_bot

  clean_content = prepare_content(content, bot_user_id)
  attachment_paths = download_attachments(message, message_id, agent_key)
  clean_content = append_attachments(clean_content, attachment_paths)
  return if clean_content.empty? && attachment_paths.empty?

  reply_context = build_reply_context(referenced_message)
  discord_user = author["username"]
  discord_user_id = author["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, message_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 authorize_user(discord_user, discord_user_id, message, channel_id, message_id, agent_name, bot_token)

  tags = parse_inline_tags(clean_content)
  project_key, project_config = resolve_project(tags[:project], parent_channel_id, agent_name, channel_id, message_id, bot_token)

  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_id, message: message,
    clean_content: clean_content, clean_content_for_prompt: tags[:clean_text],
    chat_mode: tags[: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: attachment_paths
  )
end