@spinabot/brigade 1.6.0 → 1.7.0
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.
- package/dist/agents/agent-loop.d.ts.map +1 -1
- package/dist/agents/agent-loop.js +51 -1
- package/dist/agents/agent-loop.js.map +1 -1
- package/dist/agents/channels/bundled-channel-metas.d.ts +2 -0
- package/dist/agents/channels/bundled-channel-metas.d.ts.map +1 -1
- package/dist/agents/channels/bundled-channel-metas.js +11 -0
- package/dist/agents/channels/bundled-channel-metas.js.map +1 -1
- package/dist/agents/channels/discord/account-config.d.ts +177 -0
- package/dist/agents/channels/discord/account-config.d.ts.map +1 -0
- package/dist/agents/channels/discord/account-config.js +349 -0
- package/dist/agents/channels/discord/account-config.js.map +1 -0
- package/dist/agents/channels/discord/adapter.d.ts +79 -0
- package/dist/agents/channels/discord/adapter.d.ts.map +1 -0
- package/dist/agents/channels/discord/adapter.js +693 -0
- package/dist/agents/channels/discord/adapter.js.map +1 -0
- package/dist/agents/channels/discord/approval-authorize.d.ts +43 -0
- package/dist/agents/channels/discord/approval-authorize.d.ts.map +1 -0
- package/dist/agents/channels/discord/approval-authorize.js +71 -0
- package/dist/agents/channels/discord/approval-authorize.js.map +1 -0
- package/dist/agents/channels/discord/approval-native.d.ts +68 -0
- package/dist/agents/channels/discord/approval-native.d.ts.map +1 -0
- package/dist/agents/channels/discord/approval-native.js +81 -0
- package/dist/agents/channels/discord/approval-native.js.map +1 -0
- package/dist/agents/channels/discord/command-menu.d.ts +49 -0
- package/dist/agents/channels/discord/command-menu.d.ts.map +1 -0
- package/dist/agents/channels/discord/command-menu.js +73 -0
- package/dist/agents/channels/discord/command-menu.js.map +1 -0
- package/dist/agents/channels/discord/component-blocks.d.ts +108 -0
- package/dist/agents/channels/discord/component-blocks.d.ts.map +1 -0
- package/dist/agents/channels/discord/component-blocks.js +113 -0
- package/dist/agents/channels/discord/component-blocks.js.map +1 -0
- package/dist/agents/channels/discord/components.d.ts +175 -0
- package/dist/agents/channels/discord/components.d.ts.map +1 -0
- package/dist/agents/channels/discord/components.js +220 -0
- package/dist/agents/channels/discord/components.js.map +1 -0
- package/dist/agents/channels/discord/connection.d.ts +570 -0
- package/dist/agents/channels/discord/connection.d.ts.map +1 -0
- package/dist/agents/channels/discord/connection.js +1600 -0
- package/dist/agents/channels/discord/connection.js.map +1 -0
- package/dist/agents/channels/discord/directory-cache.d.ts +47 -0
- package/dist/agents/channels/discord/directory-cache.d.ts.map +1 -0
- package/dist/agents/channels/discord/directory-cache.js +131 -0
- package/dist/agents/channels/discord/directory-cache.js.map +1 -0
- package/dist/agents/channels/discord/directory-live.d.ts +61 -0
- package/dist/agents/channels/discord/directory-live.d.ts.map +1 -0
- package/dist/agents/channels/discord/directory-live.js +140 -0
- package/dist/agents/channels/discord/directory-live.js.map +1 -0
- package/dist/agents/channels/discord/draft-stream.d.ts +92 -0
- package/dist/agents/channels/discord/draft-stream.d.ts.map +1 -0
- package/dist/agents/channels/discord/draft-stream.js +213 -0
- package/dist/agents/channels/discord/draft-stream.js.map +1 -0
- package/dist/agents/channels/discord/format.d.ts +70 -0
- package/dist/agents/channels/discord/format.d.ts.map +1 -0
- package/dist/agents/channels/discord/format.js +303 -0
- package/dist/agents/channels/discord/format.js.map +1 -0
- package/dist/agents/channels/discord/guilds.d.ts +25 -0
- package/dist/agents/channels/discord/guilds.d.ts.map +1 -0
- package/dist/agents/channels/discord/guilds.js +46 -0
- package/dist/agents/channels/discord/guilds.js.map +1 -0
- package/dist/agents/channels/discord/inbound-extras.d.ts +377 -0
- package/dist/agents/channels/discord/inbound-extras.d.ts.map +1 -0
- package/dist/agents/channels/discord/inbound-extras.js +589 -0
- package/dist/agents/channels/discord/inbound-extras.js.map +1 -0
- package/dist/agents/channels/discord/index.d.ts +21 -0
- package/dist/agents/channels/discord/index.d.ts.map +1 -0
- package/dist/agents/channels/discord/index.js +21 -0
- package/dist/agents/channels/discord/index.js.map +1 -0
- package/dist/agents/channels/discord/media.d.ts +85 -0
- package/dist/agents/channels/discord/media.d.ts.map +1 -0
- package/dist/agents/channels/discord/media.js +242 -0
- package/dist/agents/channels/discord/media.js.map +1 -0
- package/dist/agents/channels/discord/modal-registry.d.ts +89 -0
- package/dist/agents/channels/discord/modal-registry.d.ts.map +1 -0
- package/dist/agents/channels/discord/modal-registry.js +104 -0
- package/dist/agents/channels/discord/modal-registry.js.map +1 -0
- package/dist/agents/channels/discord/modals.d.ts +100 -0
- package/dist/agents/channels/discord/modals.d.ts.map +1 -0
- package/dist/agents/channels/discord/modals.js +124 -0
- package/dist/agents/channels/discord/modals.js.map +1 -0
- package/dist/agents/channels/discord/module.d.ts +15 -0
- package/dist/agents/channels/discord/module.d.ts.map +1 -0
- package/dist/agents/channels/discord/module.js +22 -0
- package/dist/agents/channels/discord/module.js.map +1 -0
- package/dist/agents/channels/discord/permission-audit.d.ts +43 -0
- package/dist/agents/channels/discord/permission-audit.d.ts.map +1 -0
- package/dist/agents/channels/discord/permission-audit.js +192 -0
- package/dist/agents/channels/discord/permission-audit.js.map +1 -0
- package/dist/agents/channels/discord/plugin.d.ts +89 -0
- package/dist/agents/channels/discord/plugin.d.ts.map +1 -0
- package/dist/agents/channels/discord/plugin.js +372 -0
- package/dist/agents/channels/discord/plugin.js.map +1 -0
- package/dist/agents/channels/discord/probe.d.ts +115 -0
- package/dist/agents/channels/discord/probe.d.ts.map +1 -0
- package/dist/agents/channels/discord/probe.js +193 -0
- package/dist/agents/channels/discord/probe.js.map +1 -0
- package/dist/agents/channels/discord/reasoning-lane.d.ts +42 -0
- package/dist/agents/channels/discord/reasoning-lane.d.ts.map +1 -0
- package/dist/agents/channels/discord/reasoning-lane.js +68 -0
- package/dist/agents/channels/discord/reasoning-lane.js.map +1 -0
- package/dist/agents/channels/discord/rest-actions.d.ts +346 -0
- package/dist/agents/channels/discord/rest-actions.d.ts.map +1 -0
- package/dist/agents/channels/discord/rest-actions.js +559 -0
- package/dist/agents/channels/discord/rest-actions.js.map +1 -0
- package/dist/agents/channels/discord/rest-components.d.ts +122 -0
- package/dist/agents/channels/discord/rest-components.d.ts.map +1 -0
- package/dist/agents/channels/discord/rest-components.js +243 -0
- package/dist/agents/channels/discord/rest-components.js.map +1 -0
- package/dist/agents/channels/discord/security-audit.d.ts +29 -0
- package/dist/agents/channels/discord/security-audit.d.ts.map +1 -0
- package/dist/agents/channels/discord/security-audit.js +94 -0
- package/dist/agents/channels/discord/security-audit.js.map +1 -0
- package/dist/agents/channels/discord/security-doctor.d.ts +43 -0
- package/dist/agents/channels/discord/security-doctor.d.ts.map +1 -0
- package/dist/agents/channels/discord/security-doctor.js +83 -0
- package/dist/agents/channels/discord/security-doctor.js.map +1 -0
- package/dist/agents/channels/discord/status-issues.d.ts +37 -0
- package/dist/agents/channels/discord/status-issues.d.ts.map +1 -0
- package/dist/agents/channels/discord/status-issues.js +66 -0
- package/dist/agents/channels/discord/status-issues.js.map +1 -0
- package/dist/agents/channels/discord/subagent-thread-binding-store.d.ts +57 -0
- package/dist/agents/channels/discord/subagent-thread-binding-store.d.ts.map +1 -0
- package/dist/agents/channels/discord/subagent-thread-binding-store.js +98 -0
- package/dist/agents/channels/discord/subagent-thread-binding-store.js.map +1 -0
- package/dist/agents/channels/discord/subagent-thread-binding.d.ts +95 -0
- package/dist/agents/channels/discord/subagent-thread-binding.d.ts.map +1 -0
- package/dist/agents/channels/discord/subagent-thread-binding.js +208 -0
- package/dist/agents/channels/discord/subagent-thread-binding.js.map +1 -0
- package/dist/agents/channels/discord/system-events.d.ts +31 -0
- package/dist/agents/channels/discord/system-events.d.ts.map +1 -0
- package/dist/agents/channels/discord/system-events.js +74 -0
- package/dist/agents/channels/discord/system-events.js.map +1 -0
- package/dist/agents/channels/general-callback.d.ts +12 -0
- package/dist/agents/channels/general-callback.d.ts.map +1 -1
- package/dist/agents/channels/general-callback.js +18 -0
- package/dist/agents/channels/general-callback.js.map +1 -1
- package/dist/agents/channels/inbound-pipeline.d.ts.map +1 -1
- package/dist/agents/channels/inbound-pipeline.js +70 -10
- package/dist/agents/channels/inbound-pipeline.js.map +1 -1
- package/dist/agents/channels/sdk.d.ts +2 -0
- package/dist/agents/channels/sdk.d.ts.map +1 -1
- package/dist/agents/channels/sdk.js +2 -0
- package/dist/agents/channels/sdk.js.map +1 -1
- package/dist/agents/extensions/modules/index.d.ts.map +1 -1
- package/dist/agents/extensions/modules/index.js +5 -0
- package/dist/agents/extensions/modules/index.js.map +1 -1
- package/dist/agents/extensions/types.d.ts +7 -0
- package/dist/agents/extensions/types.d.ts.map +1 -1
- package/dist/agents/extensions/types.js.map +1 -1
- package/dist/agents/subagent-announce-delivery.d.ts +10 -0
- package/dist/agents/subagent-announce-delivery.d.ts.map +1 -1
- package/dist/agents/subagent-announce-delivery.js +1 -0
- package/dist/agents/subagent-announce-delivery.js.map +1 -1
- package/dist/agents/subagent-completion-bridge.d.ts.map +1 -1
- package/dist/agents/subagent-completion-bridge.js +81 -0
- package/dist/agents/subagent-completion-bridge.js.map +1 -1
- package/dist/agents/subagent-spawn.d.ts.map +1 -1
- package/dist/agents/subagent-spawn.js +57 -4
- package/dist/agents/subagent-spawn.js.map +1 -1
- package/dist/agents/tools/cron-tool.d.ts.map +1 -1
- package/dist/agents/tools/cron-tool.js +4 -1
- package/dist/agents/tools/cron-tool.js.map +1 -1
- package/dist/agents/tools/discord-action-tool.d.ts +224 -0
- package/dist/agents/tools/discord-action-tool.d.ts.map +1 -0
- package/dist/agents/tools/discord-action-tool.js +848 -0
- package/dist/agents/tools/discord-action-tool.js.map +1 -0
- package/dist/agents/tools/registry.d.ts.map +1 -1
- package/dist/agents/tools/registry.js +21 -0
- package/dist/agents/tools/registry.js.map +1 -1
- package/dist/agents/tools/sessions/index.d.ts +8 -0
- package/dist/agents/tools/sessions/index.d.ts.map +1 -1
- package/dist/agents/tools/sessions/index.js +15 -3
- package/dist/agents/tools/sessions/index.js.map +1 -1
- package/dist/buildstamp.json +1 -1
- package/dist/cli/commands/channels.d.ts +2 -0
- package/dist/cli/commands/channels.d.ts.map +1 -1
- package/dist/cli/commands/channels.js +58 -1
- package/dist/cli/commands/channels.js.map +1 -1
- package/dist/core/auth-bridge.d.ts +1 -0
- package/dist/core/auth-bridge.d.ts.map +1 -1
- package/dist/core/auth-bridge.js +46 -1
- package/dist/core/auth-bridge.js.map +1 -1
- package/dist/core/server.d.ts.map +1 -1
- package/dist/core/server.js +18 -2
- package/dist/core/server.js.map +1 -1
- package/dist/cron/isolated-agent/run-executor.d.ts +11 -0
- package/dist/cron/isolated-agent/run-executor.d.ts.map +1 -1
- package/dist/cron/isolated-agent/run-executor.js +20 -4
- package/dist/cron/isolated-agent/run-executor.js.map +1 -1
- package/dist/cron/types.d.ts +8 -0
- package/dist/cron/types.d.ts.map +1 -1
- package/dist/system-prompt/assembler.d.ts.map +1 -1
- package/dist/system-prompt/assembler.js +4 -2
- package/dist/system-prompt/assembler.js.map +1 -1
- package/package.json +2 -1
|
@@ -0,0 +1,693 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Discord channel adapter.
|
|
3
|
+
*
|
|
4
|
+
* Implements the Brigade `ChannelAdapter` contract on top of the discord.js
|
|
5
|
+
* Gateway + REST connection. Like Slack, Discord is TOKEN-based: the operator
|
|
6
|
+
* pastes a bot token from the Discord Developer Portal, so this adapter declares
|
|
7
|
+
* a `setup` wizard (one credential) and has NO QR/link flow. Enablement is
|
|
8
|
+
* explicit — `channels.discord.enabled: true` plus a resolvable bot token.
|
|
9
|
+
*
|
|
10
|
+
* Modeled directly on `slack/adapter.ts`: same health-flag mirroring, same
|
|
11
|
+
* deferred-media passthrough on inbound, same chunk-then-send outbound shape
|
|
12
|
+
* (chunk markdown ≤2000, convert each chunk to Discord markup, send). Discord
|
|
13
|
+
* markup never "fails to parse" the way Telegram HTML can, so the outbound path
|
|
14
|
+
* is simple — an empty rendered chunk falls back to the raw chunk.
|
|
15
|
+
*
|
|
16
|
+
* Capabilities: edit (message.edit), unsend (message.delete), reactions
|
|
17
|
+
* (message.react), reply (reply reference + threads), threads, media
|
|
18
|
+
* (AttachmentBuilder), buttons (ActionRow + Button), and NATIVE slash commands
|
|
19
|
+
* (registered via REST application commands on connect). Unlike Slack, Discord's
|
|
20
|
+
* command menu IS pushed programmatically — `nativeCommands: true`.
|
|
21
|
+
*/
|
|
22
|
+
import { loadConfig } from "../../../core/config.js";
|
|
23
|
+
// Channel SDK barrel — the single import surface for the channel-authoring
|
|
24
|
+
// contract + shared helpers. Contract types + `chunkText` + `buildBundledCommands`
|
|
25
|
+
// come from one place instead of scattered paths.
|
|
26
|
+
import { buildBundledCommands, chunkText, } from "../sdk.js";
|
|
27
|
+
import { readAllowFrom } from "../access-control/store.js";
|
|
28
|
+
import { discordChannelEnabled, discordLiveStreamEnabled, discordReactionNotifications, discordStreamThrottleMs, discordSurfaceReasoning, listDiscordAccountIds, resolveDiscordAutoThread, resolveDiscordBotToken, resolveDiscordPresence, resolveDiscordProxyUrl, DISCORD_CHANNEL_ID, DISCORD_DEFAULT_ACCOUNT_ID, } from "./account-config.js";
|
|
29
|
+
import { resolveDiscordApprover } from "./approval-authorize.js";
|
|
30
|
+
import { buildDiscordApprovalMessage } from "./approval-native.js";
|
|
31
|
+
import { buildDiscordButtonRows } from "./components.js";
|
|
32
|
+
import { buildDiscordCommandManifest } from "./command-menu.js";
|
|
33
|
+
import { connectDiscord, sanitizeThreadName, } from "./connection.js";
|
|
34
|
+
import { resolveDiscordHandle } from "./directory-cache.js";
|
|
35
|
+
import { createDraftStream } from "./draft-stream.js";
|
|
36
|
+
import { discordTextIsEmpty, markdownToDiscord, rewriteKnownMentions } from "./format.js";
|
|
37
|
+
import { splitDiscordReasoning } from "./reasoning-lane.js";
|
|
38
|
+
/** Discord's per-message text limit (chars) for chunked sends. */
|
|
39
|
+
const DISCORD_TEXT_LIMIT = 2_000;
|
|
40
|
+
/**
|
|
41
|
+
* Map a resolved presence config into the discord.js `PresenceData` payload the
|
|
42
|
+
* connection applies on (re)connect (Phase 5). A `custom` activity (type 4)
|
|
43
|
+
* carries its text in the `state` field (Discord renders custom status from the
|
|
44
|
+
* state); every other type uses `name`. A `streaming` activity (type 1) adds the
|
|
45
|
+
* `url`. Returns `null` when no presence is configured.
|
|
46
|
+
*/
|
|
47
|
+
export function mapDiscordPresencePayload(presence) {
|
|
48
|
+
if (!presence)
|
|
49
|
+
return null;
|
|
50
|
+
const payload = { status: presence.status };
|
|
51
|
+
if (presence.activityTypeCode !== undefined) {
|
|
52
|
+
const isCustom = presence.activityTypeCode === 4;
|
|
53
|
+
const text = presence.activityText ?? "";
|
|
54
|
+
const activity = {
|
|
55
|
+
// A custom activity needs a non-empty `name` per Discord; the visible text
|
|
56
|
+
// rides in `state`. Other types put the text in `name`.
|
|
57
|
+
name: isCustom ? "Custom Status" : text,
|
|
58
|
+
type: presence.activityTypeCode,
|
|
59
|
+
};
|
|
60
|
+
if (isCustom && text)
|
|
61
|
+
activity.state = text;
|
|
62
|
+
if (presence.activityTypeCode === 1 && presence.activityUrl)
|
|
63
|
+
activity.url = presence.activityUrl;
|
|
64
|
+
payload.activities = [activity];
|
|
65
|
+
}
|
|
66
|
+
return payload;
|
|
67
|
+
}
|
|
68
|
+
export function createDiscordAdapter(opts = {}) {
|
|
69
|
+
const accountId = opts.accountId?.trim() || DISCORD_DEFAULT_ACCOUNT_ID;
|
|
70
|
+
const connectImpl = opts.connectImpl ?? connectDiscord;
|
|
71
|
+
// Resolver bound to THIS adapter's account, handed to `rewriteKnownMentions`
|
|
72
|
+
// so a plain `@handle` the agent typed becomes a `<@id>` ping when (and only
|
|
73
|
+
// when) the inbound directory cache has seen that handle for this account.
|
|
74
|
+
const resolveMention = (handle) => resolveDiscordHandle(accountId, handle);
|
|
75
|
+
// Render an outbound chunk: rewrite known `@handle` mentions to `<@id>` FIRST
|
|
76
|
+
// (so the converter sees a real mention token), then markdown→Discord, with the
|
|
77
|
+
// raw chunk as the empty-render fallback (a syntax-only chunk must still send).
|
|
78
|
+
const renderOutbound = (chunk) => {
|
|
79
|
+
const withMentions = rewriteKnownMentions(chunk, resolveMention);
|
|
80
|
+
const rendered = markdownToDiscord(withMentions);
|
|
81
|
+
return discordTextIsEmpty(rendered) ? withMentions : rendered;
|
|
82
|
+
};
|
|
83
|
+
let connection = null;
|
|
84
|
+
// The ChannelStartContext doesn't carry the config, but the manager ALWAYS
|
|
85
|
+
// calls `isConfigured(cfg, env)` immediately before `start(ctx)` — so we
|
|
86
|
+
// capture the config + env it passed there and read the token from them in
|
|
87
|
+
// start(). This avoids a second config load and keeps the adapter pure.
|
|
88
|
+
let lastConfig = null;
|
|
89
|
+
let lastEnv = process.env;
|
|
90
|
+
// Health flags mirrored from the connection lifecycle so health() never has to
|
|
91
|
+
// round-trip Discord on the hot path (cron timer / send pre-flight).
|
|
92
|
+
// - `connected` flips true on a successful login + ready.
|
|
93
|
+
// - `tokenInvalid` is STICKY: an auth error means the token is dead and the
|
|
94
|
+
// only recovery is `brigade channels add --channel discord` with a new token.
|
|
95
|
+
let connected = false;
|
|
96
|
+
let tokenInvalid = false;
|
|
97
|
+
/**
|
|
98
|
+
* Cheap SYNCHRONOUS gate: should this inbound trigger autoThread creation? True
|
|
99
|
+
* only when the feature is on, the message isn't already in a thread, and it's
|
|
100
|
+
* a guild text message carrying text + the ids needed to anchor a thread. Keeps
|
|
101
|
+
* the non-autoThread inbound path fully synchronous.
|
|
102
|
+
*/
|
|
103
|
+
const shouldAutoThread = (msg) => {
|
|
104
|
+
if (msg.threadId)
|
|
105
|
+
return false; // already in a thread
|
|
106
|
+
const cfg = lastConfig;
|
|
107
|
+
if (!cfg || !connection)
|
|
108
|
+
return false;
|
|
109
|
+
if (!resolveDiscordAutoThread(cfg).enabled)
|
|
110
|
+
return false;
|
|
111
|
+
// Guild text message only (a DM has no guildId; a reaction/callback carries no text).
|
|
112
|
+
return Boolean((msg.guildId ?? "").trim() &&
|
|
113
|
+
(msg.conversationId ?? "").trim() &&
|
|
114
|
+
(msg.messageId ?? "").trim() &&
|
|
115
|
+
(msg.text ?? "").trim());
|
|
116
|
+
};
|
|
117
|
+
/**
|
|
118
|
+
* Phase 5 autoThread: create a thread off an inbound guild text message and
|
|
119
|
+
* return the new thread id (caller guards with {@link shouldAutoThread}).
|
|
120
|
+
* Returns the message's existing `threadId` (undefined) on any failure so the
|
|
121
|
+
* reply stays un-threaded.
|
|
122
|
+
*
|
|
123
|
+
* Thread naming: `"first-message"` uses the inbound's first line; `"generated"`
|
|
124
|
+
* would use an LLM-titled name, but Brigade has no simple-completion helper —
|
|
125
|
+
* so `"generated"` FALLS BACK to the first-message name here (no completion
|
|
126
|
+
* runtime is built).
|
|
127
|
+
*/
|
|
128
|
+
const maybeAutoThread = async (msg) => {
|
|
129
|
+
const existing = msg.threadId;
|
|
130
|
+
const cfg = lastConfig;
|
|
131
|
+
if (!cfg || !connection)
|
|
132
|
+
return existing;
|
|
133
|
+
const auto = resolveDiscordAutoThread(cfg);
|
|
134
|
+
const channelId = (msg.conversationId ?? "").trim();
|
|
135
|
+
const messageId = (msg.messageId ?? "").trim();
|
|
136
|
+
const text = (msg.text ?? "").trim();
|
|
137
|
+
// Name source: first-message (or generated → fallback to first-message until
|
|
138
|
+
// a completion runtime exists).
|
|
139
|
+
const name = sanitizeThreadName(text, messageId);
|
|
140
|
+
try {
|
|
141
|
+
const created = await connection.createThreadFromMessage(channelId, messageId, {
|
|
142
|
+
name,
|
|
143
|
+
autoArchiveMinutes: auto.autoArchiveMinutes,
|
|
144
|
+
});
|
|
145
|
+
return created ?? existing;
|
|
146
|
+
}
|
|
147
|
+
catch {
|
|
148
|
+
return existing;
|
|
149
|
+
}
|
|
150
|
+
};
|
|
151
|
+
const adapter = {
|
|
152
|
+
id: DISCORD_CHANNEL_ID,
|
|
153
|
+
label: "Discord",
|
|
154
|
+
isConfigured(cfg, env) {
|
|
155
|
+
// Capture for start() — the manager calls this right before start(ctx).
|
|
156
|
+
lastConfig = cfg;
|
|
157
|
+
lastEnv = env ?? process.env;
|
|
158
|
+
if (!discordChannelEnabled(cfg))
|
|
159
|
+
return false;
|
|
160
|
+
// Need a resolvable bot token (config `${VAR}` ref, sealed token, or
|
|
161
|
+
// DISCORD_BOT_TOKEN env).
|
|
162
|
+
if (!resolveDiscordBotToken(cfg, accountId, env ?? process.env))
|
|
163
|
+
return false;
|
|
164
|
+
// Multi-account follow-up: when the operator declares >1 account, the
|
|
165
|
+
// plugin path owns lifecycle and the legacy single adapter steps aside.
|
|
166
|
+
const isLegacyAdapter = accountId === DISCORD_DEFAULT_ACCOUNT_ID;
|
|
167
|
+
if (isLegacyAdapter && listDiscordAccountIds(cfg).length > 1)
|
|
168
|
+
return false;
|
|
169
|
+
return true;
|
|
170
|
+
},
|
|
171
|
+
async start(ctx) {
|
|
172
|
+
// Resolve the token from the config the manager handed isConfigured().
|
|
173
|
+
// Fall back to a fresh load defensively (e.g. a direct start() in a test
|
|
174
|
+
// that skipped isConfigured).
|
|
175
|
+
const cfg = lastConfig ?? (await loadStartConfig());
|
|
176
|
+
const botToken = resolveDiscordBotToken(cfg, accountId, lastEnv);
|
|
177
|
+
if (!botToken) {
|
|
178
|
+
ctx.log("Discord not started — no bot token resolved (set channels.discord.botToken or DISCORD_BOT_TOKEN).");
|
|
179
|
+
return;
|
|
180
|
+
}
|
|
181
|
+
// Optional proxy — routes the REST + Gateway websocket through it on
|
|
182
|
+
// networks where discord.com is blocked. Empty → direct (unchanged).
|
|
183
|
+
const proxyUrl = resolveDiscordProxyUrl(cfg, accountId, lastEnv);
|
|
184
|
+
// The native slash-command manifest, derived from Brigade's central
|
|
185
|
+
// channel commands; registered right after the connection is live (below).
|
|
186
|
+
const commandManifest = buildDiscordCommandManifest(buildBundledCommands(adapter));
|
|
187
|
+
// Resolve the optional bot presence to apply on (re)connect (Phase 5).
|
|
188
|
+
const presencePayload = mapDiscordPresencePayload(resolveDiscordPresence(cfg, accountId));
|
|
189
|
+
const conn = await connectImpl({
|
|
190
|
+
botToken,
|
|
191
|
+
...(proxyUrl ? { proxyUrl } : {}),
|
|
192
|
+
...(presencePayload ? { presence: presencePayload } : {}),
|
|
193
|
+
accountId,
|
|
194
|
+
log: ctx.log,
|
|
195
|
+
onConnected: () => {
|
|
196
|
+
connected = true;
|
|
197
|
+
tokenInvalid = false;
|
|
198
|
+
ctx.log("Discord ready");
|
|
199
|
+
ctx.onConnected?.();
|
|
200
|
+
},
|
|
201
|
+
onTokenInvalid: () => {
|
|
202
|
+
connected = false;
|
|
203
|
+
tokenInvalid = true;
|
|
204
|
+
ctx.log("Discord token was rejected. Run `brigade channels add --channel discord` with a fresh bot token.");
|
|
205
|
+
ctx.onLoggedOut?.();
|
|
206
|
+
},
|
|
207
|
+
onMessage: (msg) => {
|
|
208
|
+
// Build the inbound with a resolved thread id. The dispatch is
|
|
209
|
+
// SYNCHRONOUS when no thread needs creating (the common path, unchanged
|
|
210
|
+
// behavior); only autoThread creation defers to a microtask.
|
|
211
|
+
const dispatch = (threadId) => {
|
|
212
|
+
void ctx.onInbound({
|
|
213
|
+
channel: DISCORD_CHANNEL_ID,
|
|
214
|
+
accountId,
|
|
215
|
+
conversationId: msg.conversationId,
|
|
216
|
+
messageId: msg.messageId,
|
|
217
|
+
messageTimestampMs: msg.messageTimestampMs,
|
|
218
|
+
from: msg.from,
|
|
219
|
+
fromName: msg.fromName,
|
|
220
|
+
text: msg.text,
|
|
221
|
+
chatType: msg.chatType,
|
|
222
|
+
isGroup: msg.chatType === "group",
|
|
223
|
+
threadId,
|
|
224
|
+
// Discord routes on guildId + member role ids (NOT teamId — that
|
|
225
|
+
// is Slack's workspace tier; setting it would risk colliding with
|
|
226
|
+
// a Slack team binding).
|
|
227
|
+
guildId: msg.guildId,
|
|
228
|
+
memberRoleIds: msg.memberRoleIds,
|
|
229
|
+
mentions: msg.mentions,
|
|
230
|
+
replyTo: msg.replyTo,
|
|
231
|
+
// Edit provenance rides through so the central pipeline / agent see
|
|
232
|
+
// "this was an edit".
|
|
233
|
+
...(msg.edited ? { edited: true } : {}),
|
|
234
|
+
// Deferred media thunk rides through untouched — the pipeline
|
|
235
|
+
// resolves it only after the access gate admits the sender.
|
|
236
|
+
resolveMedia: msg.resolveMedia,
|
|
237
|
+
raw: msg.raw,
|
|
238
|
+
});
|
|
239
|
+
};
|
|
240
|
+
// Phase 5 autoThread: when enabled (and this is a fresh guild text
|
|
241
|
+
// message), spawn a thread off the message and route the reply into it.
|
|
242
|
+
// `shouldAutoThread` is a cheap synchronous gate so the non-autoThread
|
|
243
|
+
// path stays fully synchronous.
|
|
244
|
+
if (shouldAutoThread(msg)) {
|
|
245
|
+
void maybeAutoThread(msg).then(dispatch).catch(() => dispatch(msg.threadId));
|
|
246
|
+
}
|
|
247
|
+
else {
|
|
248
|
+
dispatch(msg.threadId);
|
|
249
|
+
}
|
|
250
|
+
},
|
|
251
|
+
// Inbound reaction → synthesise a short note and route it through the
|
|
252
|
+
// SAME inbound pipeline as a normal message so the access gate + routing
|
|
253
|
+
// apply uniformly. The note carries the added emoji(s) + the target id.
|
|
254
|
+
//
|
|
255
|
+
// GATED by `channels.discord.reactionNotifications` (default "own") so a
|
|
256
|
+
// stranger's reaction in an admitted channel no longer spams the agent:
|
|
257
|
+
// off → drop all; own → only reactions on the bot's own messages;
|
|
258
|
+
// all → route every reaction; allowlist → only allow-listed reactors.
|
|
259
|
+
onReaction: (msg) => {
|
|
260
|
+
if (!msg.reaction)
|
|
261
|
+
return;
|
|
262
|
+
if (!shouldNotifyReaction(msg, lastConfig, connection?.selfId() ?? undefined, accountId))
|
|
263
|
+
return;
|
|
264
|
+
const note = buildReactionNote(msg.reaction.emojis, msg.reaction.targetMessageId, msg.fromName);
|
|
265
|
+
void ctx.onInbound({
|
|
266
|
+
channel: DISCORD_CHANNEL_ID,
|
|
267
|
+
accountId,
|
|
268
|
+
conversationId: msg.conversationId,
|
|
269
|
+
from: msg.from,
|
|
270
|
+
...(msg.fromName !== undefined ? { fromName: msg.fromName } : {}),
|
|
271
|
+
text: note,
|
|
272
|
+
chatType: msg.chatType,
|
|
273
|
+
isGroup: msg.chatType === "group",
|
|
274
|
+
...(msg.threadId !== undefined ? { threadId: msg.threadId } : {}),
|
|
275
|
+
...(msg.guildId !== undefined ? { guildId: msg.guildId } : {}),
|
|
276
|
+
...(msg.memberRoleIds !== undefined ? { memberRoleIds: msg.memberRoleIds } : {}),
|
|
277
|
+
reaction: msg.reaction,
|
|
278
|
+
raw: msg.raw,
|
|
279
|
+
});
|
|
280
|
+
},
|
|
281
|
+
// Button press → emit an InboundMessage carrying `callbackQuery` so the
|
|
282
|
+
// central pipeline's approval-callback path resolves it. The connection
|
|
283
|
+
// has already acked the press.
|
|
284
|
+
onCallbackQuery: (msg) => {
|
|
285
|
+
if (!msg.callbackQuery)
|
|
286
|
+
return;
|
|
287
|
+
void ctx.onInbound({
|
|
288
|
+
channel: DISCORD_CHANNEL_ID,
|
|
289
|
+
accountId,
|
|
290
|
+
conversationId: msg.conversationId,
|
|
291
|
+
from: msg.from,
|
|
292
|
+
...(msg.fromName !== undefined ? { fromName: msg.fromName } : {}),
|
|
293
|
+
text: "",
|
|
294
|
+
chatType: msg.chatType,
|
|
295
|
+
isGroup: msg.chatType === "group",
|
|
296
|
+
...(msg.threadId !== undefined ? { threadId: msg.threadId } : {}),
|
|
297
|
+
...(msg.guildId !== undefined ? { guildId: msg.guildId } : {}),
|
|
298
|
+
...(msg.memberRoleIds !== undefined ? { memberRoleIds: msg.memberRoleIds } : {}),
|
|
299
|
+
callbackQuery: msg.callbackQuery,
|
|
300
|
+
raw: msg.raw,
|
|
301
|
+
});
|
|
302
|
+
},
|
|
303
|
+
});
|
|
304
|
+
connection = conn;
|
|
305
|
+
// Register the native slash commands now that the connection exists.
|
|
306
|
+
// `connectDiscord` resolves once the first connect (or terminal failure)
|
|
307
|
+
// settles, so a successful boot is already live here. Best-effort: a
|
|
308
|
+
// registration failure is logged inside `registerCommands` and never
|
|
309
|
+
// blocks startup. When the token was rejected we skip (nothing to push).
|
|
310
|
+
if (connected && !tokenInvalid) {
|
|
311
|
+
void conn.registerCommands(commandManifest).catch(() => { });
|
|
312
|
+
}
|
|
313
|
+
},
|
|
314
|
+
async stop() {
|
|
315
|
+
await connection?.close();
|
|
316
|
+
connection = null;
|
|
317
|
+
connected = false;
|
|
318
|
+
},
|
|
319
|
+
/**
|
|
320
|
+
* Synchronous read of the cached connection state:
|
|
321
|
+
* - `{ ok: true }` once the Gateway is live.
|
|
322
|
+
* - `{ ok: false, kind: "logged-out" }` after an auth error (sticky; re-token).
|
|
323
|
+
* - `{ ok: false, kind: "starting" }` between start() and first connect.
|
|
324
|
+
* - `{ ok: false, kind: "disconnected" }` for a transient drop mid-reconnect.
|
|
325
|
+
*/
|
|
326
|
+
health() {
|
|
327
|
+
if (tokenInvalid || connection?.isTokenInvalid()) {
|
|
328
|
+
return {
|
|
329
|
+
ok: false,
|
|
330
|
+
kind: "logged-out",
|
|
331
|
+
reason: "Discord token was rejected — Brigade can't send until a new token is set.",
|
|
332
|
+
remediation: "Run `brigade channels add --channel discord` and paste a fresh bot token.",
|
|
333
|
+
};
|
|
334
|
+
}
|
|
335
|
+
if (!connection) {
|
|
336
|
+
return { ok: false, kind: "starting", reason: "Discord adapter is not started yet." };
|
|
337
|
+
}
|
|
338
|
+
if (!connected || !connection.isConnected()) {
|
|
339
|
+
return {
|
|
340
|
+
ok: false,
|
|
341
|
+
kind: "disconnected",
|
|
342
|
+
reason: "Discord is reconnecting — sends will fail until the Gateway resumes.",
|
|
343
|
+
};
|
|
344
|
+
}
|
|
345
|
+
return { ok: true };
|
|
346
|
+
},
|
|
347
|
+
async sendText(conversationId, text, opts) {
|
|
348
|
+
if (!connection)
|
|
349
|
+
throw new Error("Discord channel is not started");
|
|
350
|
+
if (tokenInvalid || connection.isTokenInvalid()) {
|
|
351
|
+
throw new Error("Discord token is invalid — run `brigade channels add --channel discord` with a new token, then retry.");
|
|
352
|
+
}
|
|
353
|
+
const threadId = opts?.threadId;
|
|
354
|
+
// Native reply target — applied to the FIRST chunk only (threading every
|
|
355
|
+
// chunk of a long reply is redundant once the first lands). Omitted →
|
|
356
|
+
// unthreaded send (unchanged).
|
|
357
|
+
const replyToMessageId = opts?.replyToId;
|
|
358
|
+
const sendExtras = {};
|
|
359
|
+
if (threadId)
|
|
360
|
+
sendExtras.threadId = threadId;
|
|
361
|
+
// Chunk on the RAW markdown so fences/paragraphs aren't shredded, then
|
|
362
|
+
// convert each chunk to Discord markup and send. A chunk whose rendered
|
|
363
|
+
// markup is empty (syntax-only) is re-sent as the raw chunk.
|
|
364
|
+
const chunks = chunkText(text, { limit: DISCORD_TEXT_LIMIT });
|
|
365
|
+
// A silent send rides through on every chunk (SuppressNotifications).
|
|
366
|
+
const silentOpt = opts?.silent ? { silent: true } : {};
|
|
367
|
+
let first = true;
|
|
368
|
+
for (const chunk of chunks) {
|
|
369
|
+
const replyOpt = first && replyToMessageId ? { replyToMessageId } : {};
|
|
370
|
+
const body = renderOutbound(chunk);
|
|
371
|
+
if (body.trim().length === 0)
|
|
372
|
+
continue;
|
|
373
|
+
await connection.sendText(conversationId, body, { ...sendExtras, ...silentOpt, ...replyOpt });
|
|
374
|
+
first = false;
|
|
375
|
+
}
|
|
376
|
+
},
|
|
377
|
+
/**
|
|
378
|
+
* Open a LIVE reply stream — the gateway feeds the accumulating answer text
|
|
379
|
+
* via `update()`, this edits one Discord message in place (throttled
|
|
380
|
+
* ~1×/sec), and `finalize()` settles it on turn end. Returns `null` when
|
|
381
|
+
* streaming is disabled in config (`channels.discord.liveStream` is not true)
|
|
382
|
+
* OR the connection isn't live, so the pipeline falls back to the single
|
|
383
|
+
* final `sendText` — byte-unchanged from before streaming existed.
|
|
384
|
+
*
|
|
385
|
+
* Each draft chunk is rendered through the SAME markdown→Discord converter
|
|
386
|
+
* the final path uses. When the running answer exceeds the limit the stream
|
|
387
|
+
* finalizes the current message at a boundary and rolls overflow into a new
|
|
388
|
+
* message.
|
|
389
|
+
*/
|
|
390
|
+
beginReplyStream(conversationId, sendOpts) {
|
|
391
|
+
if (!connection)
|
|
392
|
+
return null;
|
|
393
|
+
if (tokenInvalid || connection.isTokenInvalid())
|
|
394
|
+
return null;
|
|
395
|
+
const cfg = lastConfig;
|
|
396
|
+
if (!cfg || !discordLiveStreamEnabled(cfg))
|
|
397
|
+
return null;
|
|
398
|
+
const conn = connection;
|
|
399
|
+
const threadId = sendOpts?.threadId;
|
|
400
|
+
const stream = createDraftStream({
|
|
401
|
+
transport: {
|
|
402
|
+
async postMessage(text, o) {
|
|
403
|
+
const sent = await conn.sendText(conversationId, text, {
|
|
404
|
+
...(o.threadId !== undefined ? { threadId: o.threadId } : {}),
|
|
405
|
+
});
|
|
406
|
+
return { id: sent.messageId };
|
|
407
|
+
},
|
|
408
|
+
async editMessage(id, text) {
|
|
409
|
+
await conn.editMessageText(conversationId, id, text);
|
|
410
|
+
},
|
|
411
|
+
},
|
|
412
|
+
...(threadId !== undefined ? { threadId } : {}),
|
|
413
|
+
throttleMs: discordStreamThrottleMs(cfg),
|
|
414
|
+
maxChars: DISCORD_TEXT_LIMIT,
|
|
415
|
+
// Render each draft chunk to Discord markup (incl. known-mention rewrite);
|
|
416
|
+
// fall back to the plain chunk when it renders empty (syntax-only).
|
|
417
|
+
renderText: (chunk) => {
|
|
418
|
+
return { text: renderOutbound(chunk) };
|
|
419
|
+
},
|
|
420
|
+
});
|
|
421
|
+
return {
|
|
422
|
+
update: (text) => stream.update(text),
|
|
423
|
+
async finalize(finalText) {
|
|
424
|
+
await stream.finalize(finalText);
|
|
425
|
+
const ids = stream.messageIds();
|
|
426
|
+
const last = ids[ids.length - 1];
|
|
427
|
+
return last !== undefined ? { messageId: last } : undefined;
|
|
428
|
+
},
|
|
429
|
+
stop: () => stream.stop(),
|
|
430
|
+
};
|
|
431
|
+
},
|
|
432
|
+
/**
|
|
433
|
+
* OPTIONAL reasoning lane (default OFF). When `channels.discord.
|
|
434
|
+
* surfaceReasoning` is true, split the raw reply's `<think>` trace out and
|
|
435
|
+
* send it as a separate `🧠 Reasoning:` message BEFORE the answer. When the
|
|
436
|
+
* config gate is off (the default) OR the reply carried no reasoning, this
|
|
437
|
+
* sends NOTHING — the answer message the pipeline sends afterward is
|
|
438
|
+
* byte-identical either way.
|
|
439
|
+
*/
|
|
440
|
+
async deliverReasoning(conversationId, rawReply, sendOpts) {
|
|
441
|
+
if (!connection)
|
|
442
|
+
return;
|
|
443
|
+
if (tokenInvalid || connection.isTokenInvalid())
|
|
444
|
+
return;
|
|
445
|
+
const cfg = lastConfig;
|
|
446
|
+
if (!cfg || !discordSurfaceReasoning(cfg))
|
|
447
|
+
return;
|
|
448
|
+
const { reasoningText } = splitDiscordReasoning(rawReply ?? "");
|
|
449
|
+
if (!reasoningText)
|
|
450
|
+
return;
|
|
451
|
+
// Reuse the adapter's own chunk+render send path so a long reasoning trace
|
|
452
|
+
// is chunked at 2000 and formatted consistently with replies.
|
|
453
|
+
await adapter.sendText(conversationId, reasoningText, sendOpts);
|
|
454
|
+
},
|
|
455
|
+
// Discord ids are user snowflakes; the pairing challenge card uses the
|
|
456
|
+
// "account" label. The bot is a SEPARATE account from the operator (its own
|
|
457
|
+
// app), so ownership is bootstrapped from the first CLI `pairing approve` —
|
|
458
|
+
// see `botIsSeparateFromOperator`.
|
|
459
|
+
pairing: { idLabel: "account", botIsSeparateFromOperator: true },
|
|
460
|
+
// Token-based setup wizard — `brigade channels add --channel discord` prompts
|
|
461
|
+
// for the bot token and writes `channels.discord.botToken`. The OAuth invite
|
|
462
|
+
// URL + the privileged "Message Content" gateway intent toggle CAN'T be
|
|
463
|
+
// granted programmatically, so the two setup steps the operator must do by
|
|
464
|
+
// hand are baked into the bot-token prompt copy:
|
|
465
|
+
// 1. Enable the MESSAGE CONTENT intent (Bot → Privileged Gateway Intents),
|
|
466
|
+
// or Brigade can't read message text.
|
|
467
|
+
// 2. Invite the bot with the `bot` + `applications.commands` scopes (OAuth2
|
|
468
|
+
// → URL Generator) + Send Messages / Read Message History / Add Reactions.
|
|
469
|
+
setup: {
|
|
470
|
+
credentialKeys: [
|
|
471
|
+
{
|
|
472
|
+
key: "botToken",
|
|
473
|
+
prompt: "Discord bot token (Developer Portal → Bot → Reset Token). Also: enable the MESSAGE CONTENT intent (Bot → Privileged Gateway Intents) and invite the bot with the bot + applications.commands scopes.",
|
|
474
|
+
secret: true,
|
|
475
|
+
envVar: "DISCORD_BOT_TOKEN",
|
|
476
|
+
docsUrl: "https://discord.com/developers/docs/topics/oauth2#bots",
|
|
477
|
+
},
|
|
478
|
+
],
|
|
479
|
+
validateInput(key, value) {
|
|
480
|
+
const v = value.trim();
|
|
481
|
+
// Allow a `${VAR}` ref through (resolved at runtime).
|
|
482
|
+
if (/^\$\{[A-Z_][A-Z0-9_]*\}$/.test(v))
|
|
483
|
+
return null;
|
|
484
|
+
if (key === "botToken") {
|
|
485
|
+
// Discord bot tokens are `<id>.<ts>.<secret>` (optionally `Bot `-prefixed).
|
|
486
|
+
if (/^(Bot\s+)?[A-Za-z0-9_-]{20,}\.[A-Za-z0-9_-]{4,}\.[A-Za-z0-9_-]{20,}$/.test(v))
|
|
487
|
+
return null;
|
|
488
|
+
return "That doesn't look like a Discord bot token — expected the `…. …. …` token from the Developer Portal.";
|
|
489
|
+
}
|
|
490
|
+
return null;
|
|
491
|
+
},
|
|
492
|
+
},
|
|
493
|
+
async sendMedia(conversationId, media) {
|
|
494
|
+
if (!connection)
|
|
495
|
+
throw new Error("Discord channel is not started");
|
|
496
|
+
await connection.sendMedia(conversationId, media);
|
|
497
|
+
},
|
|
498
|
+
async react(conversationId, messageId, emoji) {
|
|
499
|
+
if (!connection)
|
|
500
|
+
return; // cosmetic — refuse silently when not started
|
|
501
|
+
await connection.react(conversationId, messageId, emoji);
|
|
502
|
+
},
|
|
503
|
+
async setComposing(conversationId, state) {
|
|
504
|
+
if (!connection)
|
|
505
|
+
return;
|
|
506
|
+
await connection.setComposing(conversationId, state);
|
|
507
|
+
},
|
|
508
|
+
// Static capability flags. The central `message_action` tool PRE-CHECKS the
|
|
509
|
+
// relevant flag here before calling `handleAction`, so an unsupported action
|
|
510
|
+
// fails cleanly without touching the adapter.
|
|
511
|
+
capabilities: DISCORD_CAPABILITIES,
|
|
512
|
+
// Native component-button approvals. When a channel-routed turn raises an
|
|
513
|
+
// approval, the central router calls `sendApprovalPrompt` to render the
|
|
514
|
+
// question as buttons (payloads from the central codec); the press comes back
|
|
515
|
+
// as `InboundMessage.callbackQuery` and is resolved centrally. A pathological
|
|
516
|
+
// approval id that can't be encoded falls back to the text prompt.
|
|
517
|
+
approvalCapability: {
|
|
518
|
+
async sendApprovalPrompt(params) {
|
|
519
|
+
if (!connection)
|
|
520
|
+
throw new Error("Discord channel is not started");
|
|
521
|
+
const message = buildDiscordApprovalMessage({
|
|
522
|
+
approvalId: params.approvalId,
|
|
523
|
+
command: params.command,
|
|
524
|
+
approvalKind: params.approvalKind,
|
|
525
|
+
...(params.toolName !== undefined ? { toolName: params.toolName } : {}),
|
|
526
|
+
});
|
|
527
|
+
if (!message) {
|
|
528
|
+
// Couldn't build byte-safe buttons — let the router fall back to text.
|
|
529
|
+
throw new Error("discord approval prompt: approval id too long for buttons");
|
|
530
|
+
}
|
|
531
|
+
await connection.sendInteractive(params.conversationId, message.text, message.rows, {
|
|
532
|
+
...(params.threadId !== undefined ? { threadId: params.threadId } : {}),
|
|
533
|
+
});
|
|
534
|
+
},
|
|
535
|
+
authorizeApprover(p) {
|
|
536
|
+
return resolveDiscordApprover({
|
|
537
|
+
cfg: p.cfg,
|
|
538
|
+
...(p.senderId !== undefined ? { senderId: p.senderId } : {}),
|
|
539
|
+
...(p.accountId !== undefined ? { accountId: p.accountId } : {}),
|
|
540
|
+
});
|
|
541
|
+
},
|
|
542
|
+
},
|
|
543
|
+
// Edit / delete / react / reply a message + attach buttons. The manager
|
|
544
|
+
// pre-checks the capability flag (above) before calling, so an action only
|
|
545
|
+
// reaches here when Discord advertised support for it.
|
|
546
|
+
async handleAction(p) {
|
|
547
|
+
if (!connection)
|
|
548
|
+
return { ok: false, error: "Discord channel is not started" };
|
|
549
|
+
if (tokenInvalid || connection.isTokenInvalid()) {
|
|
550
|
+
return { ok: false, error: "Discord token is invalid — re-token before acting on messages." };
|
|
551
|
+
}
|
|
552
|
+
const a = p.action;
|
|
553
|
+
try {
|
|
554
|
+
switch (a.kind) {
|
|
555
|
+
case "edit": {
|
|
556
|
+
const body = renderOutbound(a.text);
|
|
557
|
+
await connection.editMessageText(p.conversationId, a.messageId, body);
|
|
558
|
+
return { ok: true, messageId: a.messageId };
|
|
559
|
+
}
|
|
560
|
+
case "delete":
|
|
561
|
+
await connection.deleteMessage(p.conversationId, a.messageId);
|
|
562
|
+
return { ok: true, messageId: a.messageId };
|
|
563
|
+
case "pin":
|
|
564
|
+
await connection.pinMessage(p.conversationId, a.messageId);
|
|
565
|
+
return { ok: true, messageId: a.messageId };
|
|
566
|
+
case "unpin":
|
|
567
|
+
await connection.unpinMessage(p.conversationId, a.messageId);
|
|
568
|
+
return { ok: true, messageId: a.messageId };
|
|
569
|
+
case "react":
|
|
570
|
+
// An EMPTY emoji means "clear" (parity with WhatsApp/Telegram/Slack):
|
|
571
|
+
// remove the bot's OWN reactions on this message; a non-empty emoji
|
|
572
|
+
// adds as before.
|
|
573
|
+
if (a.emoji.trim() === "") {
|
|
574
|
+
await connection.removeOwnReactions(p.conversationId, a.messageId);
|
|
575
|
+
}
|
|
576
|
+
else {
|
|
577
|
+
await connection.react(p.conversationId, a.messageId, a.emoji);
|
|
578
|
+
}
|
|
579
|
+
return { ok: true, messageId: a.messageId };
|
|
580
|
+
case "reply": {
|
|
581
|
+
// A reply is a send with a native reply reference; surface the new id.
|
|
582
|
+
const sent = await connection.sendText(p.conversationId, a.text, {
|
|
583
|
+
...(a.threadId !== undefined ? { threadId: a.threadId } : {}),
|
|
584
|
+
});
|
|
585
|
+
return { ok: true, messageId: sent.messageId };
|
|
586
|
+
}
|
|
587
|
+
case "buttons": {
|
|
588
|
+
// Send a NEW message with a general button keyboard. The button ids
|
|
589
|
+
// are prefixed/sanitized by the builder; a press arrives as
|
|
590
|
+
// `callbackQuery` and routes through the pipeline as a turn (the
|
|
591
|
+
// central approval path declines a general payload).
|
|
592
|
+
const rows = buildDiscordButtonRows(a.buttons.map((row) => row.map((b) => ({ text: b.text, data: b.data }))));
|
|
593
|
+
if (!rows) {
|
|
594
|
+
return { ok: false, error: "no usable buttons (each needs a label + a data token ≤ 100 chars)" };
|
|
595
|
+
}
|
|
596
|
+
const body = renderOutbound(a.text);
|
|
597
|
+
const sent = await connection.sendInteractive(p.conversationId, body, rows, {
|
|
598
|
+
...(a.threadId !== undefined ? { threadId: a.threadId } : {}),
|
|
599
|
+
});
|
|
600
|
+
return { ok: true, messageId: sent.messageId };
|
|
601
|
+
}
|
|
602
|
+
default:
|
|
603
|
+
return { ok: false, error: `unsupported action kind` };
|
|
604
|
+
}
|
|
605
|
+
}
|
|
606
|
+
catch (err) {
|
|
607
|
+
return { ok: false, error: err instanceof Error ? err.message : String(err) };
|
|
608
|
+
}
|
|
609
|
+
},
|
|
610
|
+
selfId() {
|
|
611
|
+
return connection?.selfId() ?? undefined;
|
|
612
|
+
},
|
|
613
|
+
connectedAt() {
|
|
614
|
+
return connection?.connectedAt() ?? null;
|
|
615
|
+
},
|
|
616
|
+
lastEventAt() {
|
|
617
|
+
return connection?.lastEventAt() ?? null;
|
|
618
|
+
},
|
|
619
|
+
};
|
|
620
|
+
return adapter;
|
|
621
|
+
}
|
|
622
|
+
/**
|
|
623
|
+
* Decide whether an inbound reaction-add should wake the agent, per the
|
|
624
|
+
* `channels.discord.reactionNotifications` mode (default `"own"`):
|
|
625
|
+
* - `"off"` → never;
|
|
626
|
+
* - `"own"` → only when the reacted message was authored by the bot
|
|
627
|
+
* (`reaction.targetAuthorId === selfId`);
|
|
628
|
+
* - `"all"` → always (legacy behavior);
|
|
629
|
+
* - `"allowlist"` → only when the reactor (`msg.from`) is on the channel
|
|
630
|
+
* allow-list — the central store list ∪ config `allowFrom`.
|
|
631
|
+
* A null config defensively falls back to `"own"`. Reaction-REMOVE is unaffected
|
|
632
|
+
* (handled in the connection's dedupe-release path).
|
|
633
|
+
*/
|
|
634
|
+
export function shouldNotifyReaction(msg, cfg, selfId, accountId) {
|
|
635
|
+
const mode = cfg ? discordReactionNotifications(cfg) : "own";
|
|
636
|
+
switch (mode) {
|
|
637
|
+
case "off":
|
|
638
|
+
return false;
|
|
639
|
+
case "all":
|
|
640
|
+
return true;
|
|
641
|
+
case "allowlist": {
|
|
642
|
+
const reactor = msg.from?.trim();
|
|
643
|
+
if (!reactor)
|
|
644
|
+
return false;
|
|
645
|
+
const acct = accountId?.trim() || undefined;
|
|
646
|
+
const storeAllow = readAllowFrom(DISCORD_CHANNEL_ID, acct);
|
|
647
|
+
const configAllow = readDiscordAllowFrom(cfg);
|
|
648
|
+
const allow = new Set([...storeAllow, ...configAllow].map((id) => id.trim()).filter(Boolean));
|
|
649
|
+
return allow.has(reactor);
|
|
650
|
+
}
|
|
651
|
+
case "own":
|
|
652
|
+
default: {
|
|
653
|
+
const targetAuthor = msg.reaction?.targetAuthorId?.trim();
|
|
654
|
+
return Boolean(selfId && targetAuthor && targetAuthor === selfId.trim());
|
|
655
|
+
}
|
|
656
|
+
}
|
|
657
|
+
}
|
|
658
|
+
/** Read `channels.discord.allowFrom` (config-declared allow-list ids) defensively. */
|
|
659
|
+
function readDiscordAllowFrom(cfg) {
|
|
660
|
+
const slot = cfg?.channels?.[DISCORD_CHANNEL_ID];
|
|
661
|
+
const list = slot?.allowFrom;
|
|
662
|
+
return Array.isArray(list) ? list.map((x) => String(x)) : [];
|
|
663
|
+
}
|
|
664
|
+
/**
|
|
665
|
+
* Synthesise the agent-facing note for an inbound reaction. The reaction itself
|
|
666
|
+
* carries no text, so the note ("<who> reacted :emoji: to message <id>") is what
|
|
667
|
+
* the central pipeline routes through dispatchTurn so the agent has context.
|
|
668
|
+
*/
|
|
669
|
+
export function buildReactionNote(emojis, targetMessageId, fromName) {
|
|
670
|
+
const who = fromName?.trim() || "Someone";
|
|
671
|
+
// A custom emoji surfaces as `name:id` — show just the name for the note.
|
|
672
|
+
const emoji = emojis.map((e) => `:${e.includes(":") ? e.split(":")[0] : e}:`).join(" ");
|
|
673
|
+
return `${who} reacted ${emoji} to message ${targetMessageId}.`;
|
|
674
|
+
}
|
|
675
|
+
/** Static Discord capability flags (shared by the legacy adapter + plugin meta). */
|
|
676
|
+
export const DISCORD_CAPABILITIES = {
|
|
677
|
+
chatTypes: ["direct", "group", "thread"],
|
|
678
|
+
reactions: true,
|
|
679
|
+
edit: true,
|
|
680
|
+
unsend: true,
|
|
681
|
+
reply: true,
|
|
682
|
+
threads: true,
|
|
683
|
+
media: true,
|
|
684
|
+
nativeCommands: true,
|
|
685
|
+
};
|
|
686
|
+
/**
|
|
687
|
+
* Defensive config fallback for a direct `start()` that skipped `isConfigured`
|
|
688
|
+
* (the manager always calls isConfigured first, so this is the rare path).
|
|
689
|
+
*/
|
|
690
|
+
async function loadStartConfig() {
|
|
691
|
+
return loadConfig();
|
|
692
|
+
}
|
|
693
|
+
//# sourceMappingURL=adapter.js.map
|