brainiac-discord 0.0.25 → 0.0.27

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b993813772110a77d5ac815a314c8eb7853083ac9cbd0c3322579e1f1b80ee40
4
- data.tar.gz: 63d422ec1ffe6e8d3c12b267c5f14061d23651e863e69f891f6d558a7e553010
3
+ metadata.gz: 7563493f0aae0dd1ad5202d8678cfcf8fef45eaac9a2658bcc2a9d42e010724e
4
+ data.tar.gz: 793c7afe6632a53152147e67ea9d71addd7c6ff785d4a3b2a76df3102a237f6e
5
5
  SHA512:
6
- metadata.gz: 72e8c931e9fb970da65743314407deebb8096f6f6e53263432ccf34fa637eed3e72b814df9de299f55f4ef162bf7bd50192c2c9a96cc077bb96defca272e06da
7
- data.tar.gz: 476d43b17b45b289e661da974062445f1ff1a660410ab5679fd3a41777fbfaecccc7abe010b1d272cc848dc3610108fd1561b9517c140576ff58d5cff3efa51a
6
+ metadata.gz: 0a59d8cf3ad1d071e987a45ae79ef2364f5409f072354d127bbf8e69b6656888319bd13f86f7c600a9a615ef82f858ef9b69306a0f768cb889fe1eedec91137c
7
+ data.tar.gz: 3074e7b2d22895f1218de2bf088ff5e9cd7c441c0836b59011081f04d6cb8c0ae573b8b4d705d31843b1191afa0d01eb3db1b7c8914338fe7f56593a1e79e892
data/README.md CHANGED
@@ -33,30 +33,52 @@ brainiac restart
33
33
 
34
34
  ## Setup
35
35
 
36
- ### 1. Create Discord Applications
36
+ ### Adding an agent (recommended)
37
+
38
+ ```bash
39
+ brainiac agent create "Galen" --local --avatar galen.png --description-file galen.md
40
+ ```
41
+
42
+ Discord has no API for creating applications, so there's one manual step: the plugin opens the [developer portal](https://discord.com/developers/applications), you click **New Application** (name it after the agent) → **Bot** → **Reset Token**, and paste the token once. Everything else is automated:
43
+
44
+ - Bot username and avatar, app icon and description (from `--avatar` / `--description-file`)
45
+ - **Message Content** intent enabled
46
+ - `DISCORD_BOT_TOKEN` saved to the agent's `env` in `~/.brainiac/agents.json`
47
+ - The bot's user ID added to `user_mappings` in `~/.brainiac/discord.json`
48
+ - Server invite link printed and opened
49
+
50
+ A running server starts the new bot immediately — no restart needed.
51
+
52
+ For an existing agent, an interrupted create, or after installing this plugin, run `brainiac agent setup galen` (add `--only discord` to run just this plugin). It's safe to re-run: steps already done are skipped, and if the token is lost it sends you straight to the existing app's Bot tab. Use `--no-browser` to print links instead of opening them, or `--skip discord` to leave Discord out.
53
+
54
+ ### Manual setup (alternative)
55
+
56
+ #### 1. Create Discord Applications
37
57
 
38
58
  Create one Discord application per agent at https://discord.com/developers/applications:
39
59
 
40
60
  1. Click "New Application", name it after the agent
41
61
  2. Go to "Bot" tab → enable **Message Content Intent**
42
- 3. Copy the bot token
62
+ 3. Click "Reset Token" and copy the bot token
43
63
 
44
- ### 2. Register Tokens
64
+ #### 2. Register Tokens
45
65
 
46
66
  ```bash
47
67
  brainiac discord token galen "BOT_TOKEN_FOR_GALEN"
48
68
  brainiac discord token glados "BOT_TOKEN_FOR_GLADOS"
49
69
  ```
50
70
 
51
- ### 3. Invite Bots
71
+ This only sets `DISCORD_BOT_TOKEN`; add the bot's user ID to `user_mappings` (see [Configuration](#configuration)) so other agents can @mention it. Restart a running server (`brainiac restart`) to pick up new tokens.
72
+
73
+ #### 3. Invite Bots
52
74
 
53
75
  Use the OAuth2 URL Generator with `bot` scope and these permissions:
54
- - Send Messages, Create Public Threads, Send Messages in Threads
55
- - Add Reactions, Read Message History
76
+ - View Channels, Send Messages, Embed Links, Attach Files, Read Message History
77
+ - Add Reactions, Manage Threads, Create Public Threads, Send Messages in Threads
56
78
 
57
- Permission integer: `326417591296`
79
+ Permission integer: `326417632320`
58
80
 
59
- ### 4. Configure
81
+ ### Configure
60
82
 
61
83
  Set a default project:
62
84
  ```bash
@@ -68,7 +90,7 @@ Map channels to projects:
68
90
  brainiac discord map 1234567890 brainiac
69
91
  ```
70
92
 
71
- ### 5. Start
93
+ ### Start
72
94
 
73
95
  ```bash
74
96
  brainiac server
@@ -76,6 +98,18 @@ brainiac server
76
98
 
77
99
  All bots connect automatically as background threads.
78
100
 
101
+ ## Commands
102
+
103
+ ```
104
+ brainiac discord setup Check config and print next steps
105
+ brainiac discord config Show discord.json
106
+ brainiac discord default <project> Set default project for all channels
107
+ brainiac discord map <channel-id> <project> Map a specific channel to a project
108
+ brainiac discord owner [<discord-user-id>] Set/show machine owner
109
+ brainiac discord token <agent-key> <token> Set an agent's DISCORD_BOT_TOKEN
110
+ brainiac discord status Bot status (via the running server)
111
+ ```
112
+
79
113
  ## Configuration
80
114
 
81
115
  Stored in `~/.brainiac/discord.json`:
@@ -97,6 +131,13 @@ Stored in `~/.brainiac/discord.json`:
97
131
  }
98
132
  ```
99
133
 
134
+ - `user_mappings` — display name → Discord user ID, for humans and agent bots (`agent create` adds the bot's entry)
135
+ - `authorized_user_ids` / `authorized_role_ids` — who may talk to the agents; both empty allows everyone
136
+ - `role_mappings` — optional agent display name → Discord role ID, so @mentioning a role reaches that agent (when set, these role IDs replace `authorized_role_ids`)
137
+ - `klipy_api_key` — enables GIF search (`giphy_api_key` is still read as a fallback)
138
+
139
+ Bot tokens live in `~/.brainiac/agents.json` as each agent's `env.DISCORD_BOT_TOKEN`, not in `discord.json`.
140
+
100
141
  ## Development
101
142
 
102
143
  ```bash
@@ -41,6 +41,7 @@ module Brainiac
41
41
 
42
42
  if token && discord_config.dig("user_mappings", display_name)
43
43
  puts " [Discord] ✓ #{display_name} already has a bot (skipping)"
44
+ sync_installed_apps(token)
44
45
  return
45
46
  end
46
47
 
@@ -65,10 +66,61 @@ module Brainiac
65
66
  puts " [Discord] Invite #{display_name} to your server:"
66
67
  puts " #{invite_url(app["id"] || me["id"])}"
67
68
  open_url(invite_url(app["id"] || me["id"])) if entry["open_browser"]
69
+
70
+ sync_installed_apps(token)
68
71
  end
69
72
 
70
73
  private
71
74
 
75
+ # Backfill user_mappings for every OTHER app/bot already installed in the
76
+ # guild — the ones this brainiac instance didn't create. Without this an
77
+ # agent only knows about bots it provisioned itself, so a tag like
78
+ # "@Judge Dredd" for an app from another instance renders as plain text
79
+ # with no ID to route to. Sweeping the guild's members (bots included)
80
+ # gives each installed app a name→id mapping so the mention roster and
81
+ # tag detection at least know it exists.
82
+ #
83
+ # Best-effort and non-fatal: needs the SERVER MEMBERS intent and a
84
+ # guild_id in discord.json. Only adds names not already mapped; never
85
+ # overwrites a hand-set entry.
86
+ def sync_installed_apps(token)
87
+ guild_id = discord_config["guild_id"]
88
+ return unless guild_id
89
+
90
+ bots = fetch_guild_bots(guild_id, token)
91
+ return if bots.nil?
92
+
93
+ existing = discord_config["user_mappings"] || {}
94
+ added = bots.reject { |name, _id| existing.key?(name) }
95
+ if added.empty?
96
+ puts " [Discord] ✓ Installed apps already mapped (#{bots.size} bot(s) in server)"
97
+ return
98
+ end
99
+
100
+ added.each { |name, id| save_user_mapping(name, id) }
101
+ puts " [Discord] ✓ Discovered #{added.size} installed app(s): #{added.keys.join(", ")}"
102
+ end
103
+
104
+ # Returns { bot_display_name => user_id } for every bot in the guild, or
105
+ # nil when the member list can't be fetched (missing intent, bad token,
106
+ # no permission). Humans are skipped — only user["bot"] members count.
107
+ def fetch_guild_bots(guild_id, token)
108
+ members = request(:get, "/guilds/#{guild_id}/members?limit=1000", token)
109
+ unless members.is_a?(Array)
110
+ puts " [Discord] ⚠ Couldn't list installed apps — enable the SERVER MEMBERS intent (Bot tab → Privileged Gateway Intents)"
111
+ return nil
112
+ end
113
+
114
+ members.each_with_object({}) do |member, acc|
115
+ user = member["user"]
116
+ next unless user && user["bot"]
117
+
118
+ name = user["global_name"] || user["username"]
119
+ id = user["id"]
120
+ acc[name] = id if name && id
121
+ end
122
+ end
123
+
72
124
  # With a user_mappings entry the app already exists (the token was lost
73
125
  # or rotated), so send the user straight to its Bot tab instead of
74
126
  # "New Application". A bot's user ID is its application ID.
@@ -76,6 +76,9 @@ module Brainiac
76
76
  content = msg["content"]&.strip || ""
77
77
  next if content.empty?
78
78
 
79
+ # Tag bot authors so intent can see agents this machine doesn't run.
80
+ # Webhook posts (GitHub, CI) are bots too, but they aren't agents.
81
+ author = "#{author} [bot]" if msg.dig("author", "bot") && !msg["webhook_id"]
79
82
  "#{author}: #{content}"
80
83
  end
81
84
 
@@ -179,6 +179,8 @@ module Brainiac
179
179
  # in this message. When a user @mentions someone specific, thread participants
180
180
  # who weren't mentioned should stay silent — no intent check needed.
181
181
  def other_agent_mentioned?(mentions, content, agent_key)
182
+ return true if remote_bot_mentioned?(mentions)
183
+
182
184
  mention_roles = []
183
185
 
184
186
  Gateway.each_bot do |key, info|
@@ -206,6 +208,13 @@ module Brainiac
206
208
  false
207
209
  end
208
210
 
211
+ # Any bot in the mentions array counts as another agent, even one this machine
212
+ # has never heard of (agents run on different machines, each with its own roster).
213
+ # Callers only get here when this bot wasn't mentioned, so self is never in the list.
214
+ def remote_bot_mentioned?(mentions)
215
+ mentions.any? { |m| m["bot"] }
216
+ end
217
+
209
218
  def validate_cross_agent_dispatch(sender_agent_key, agent_key, mentioned, content, channel_id)
210
219
  return false unless mentioned
211
220
 
@@ -262,15 +271,17 @@ module Brainiac
262
271
  def should_stand_down?(in_own_thread, mentioned, is_reply_to_bot, is_bot, agent_key, mentions, content)
263
272
  return false unless in_own_thread && !mentioned && !is_reply_to_bot && !is_bot
264
273
 
265
- other_bot_mentioned = false
266
- Gateway.each_bot do |key, info|
267
- next if key == agent_key
268
- next unless info[:user_id]
269
- next unless mentions.any? { |m| m["id"].to_s == info[:user_id].to_s } ||
270
- content.match?(/<@!?#{Regexp.escape(info[:user_id].to_s)}>/)
274
+ other_bot_mentioned = remote_bot_mentioned?(mentions)
275
+ unless other_bot_mentioned
276
+ Gateway.each_bot do |key, info|
277
+ next if key == agent_key
278
+ next unless info[:user_id]
279
+ next unless mentions.any? { |m| m["id"].to_s == info[:user_id].to_s } ||
280
+ content.match?(/<@!?#{Regexp.escape(info[:user_id].to_s)}>/)
271
281
 
272
- other_bot_mentioned = true
273
- break
282
+ other_bot_mentioned = true
283
+ break
284
+ end
274
285
  end
275
286
 
276
287
  unless other_bot_mentioned
@@ -311,39 +322,18 @@ module Brainiac
311
322
 
312
323
  # Check if this agent is the only bot in the thread (1-on-1 with the human).
313
324
  # Uses the already-fetched channel_history to avoid extra API calls.
314
- # channel_history is a formatted string ("Username: message\n...") from fetch_channel_history.
315
- # Returns true when no other agent bots have posted in the thread history,
316
- # meaning it's safe to skip intent checking (the message is obviously for us).
325
+ # channel_history is a formatted string ("Username: message\n...") from fetch_channel_history,
326
+ # with bot authors tagged "Username [bot]: ". Any bot other than us counts, including
327
+ # agents running on another machine that this one doesn't know about.
317
328
  def solo_in_thread?(channel_history, agent_key)
318
329
  return true unless channel_history.is_a?(String) && !channel_history.empty?
319
330
 
320
- other_bot_usernames = []
321
- Gateway.each_bot do |key, info|
322
- next if key == agent_key
323
- next unless info[:username]
324
-
325
- other_bot_usernames << info[:username]
326
- end
327
-
328
- # Also check user_mappings for remote agent bots not connected locally
329
- agent_keys = []
330
- Gateway.each_bot { |key, _| agent_keys << key }
331
- Config.user_mappings.each_key do |name|
332
- normalized = name.downcase.gsub(/[^a-z0-9]/, "-")
333
- next if normalized == agent_key
334
- next if agent_keys.include?(normalized)
331
+ own_username = nil
332
+ Gateway.each_bot { |key, info| own_username = info[:username] if key == agent_key }
335
333
 
336
- # Only include names that look like agent bots (present in agent registry)
337
- next unless defined?(AGENT_REGISTRY) && AGENT_REGISTRY.key?(normalized)
338
-
339
- other_bot_usernames << name
340
- end
341
-
342
- return true if other_bot_usernames.empty?
343
-
344
- history_lines = channel_history.split("\n")
345
- history_lines.none? do |line|
346
- other_bot_usernames.any? { |username| line.start_with?("#{username}: ") }
334
+ channel_history.split("\n").none? do |line|
335
+ author = line[/\A(.+?) \[bot\]: /, 1]
336
+ author && author != own_username
347
337
  end
348
338
  end
349
339
 
@@ -656,6 +646,11 @@ module Brainiac
656
646
  project_context: project_context, response_file: response_file, card_id: card_id,
657
647
  brain_context: brain_context, agent_name: agent_name, supersede_key: supersede_key
658
648
  )
649
+ # Discord spawns its own process, so fill core's per-provider Subagents slot here
650
+ # (run_agent does it for every other channel).
651
+ if defined?(fill_subagent_prompt)
652
+ prompt = fill_subagent_prompt(prompt, resolved, model: model || resolved["agent_model"])
653
+ end
659
654
 
660
655
  prompt_file = File.join(response_dir, "discord-prompt-#{timestamp}-#{agent_key}-#{message_id}.md")
661
656
  File.write(prompt_file, prompt)
@@ -3,7 +3,7 @@
3
3
  module Brainiac
4
4
  module Plugins
5
5
  module Discord
6
- VERSION = "0.0.25"
6
+ VERSION = "0.0.27"
7
7
  end
8
8
  end
9
9
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: brainiac-discord
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.25
4
+ version: 0.0.27
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andy Davis