Skip to content

Commit ea3fb46

Browse files
committed
refactor: yaml-driven bridge architecture with adapter pattern
Replace three standalone bridge scripts with a generic bridge runner that reads YAML configs from nemoclaw-blueprint/bridges/ and loads thin platform adapters. Structure: - nemoclaw-blueprint/bridges/messaging/*.yaml — declarative config (token env, session prefix, chunk size, adapter name) - scripts/adapters/messaging/*.js — thin SDK wrappers that implement connect + send/receive for each platform - scripts/bridge.js — generic runner that reads YAML, loads adapter, and drives the shared message flow (typing → agent → chunk → reply) Adding a new messaging platform is now a YAML file + small adapter. The bridge type system (messaging/) is extensible to future bridge types (e.g., MCP bridges).
1 parent 04537f1 commit ea3fb46

12 files changed

Lines changed: 512 additions & 378 deletions

File tree

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
bridge:
5+
name: discord
6+
type: messaging
7+
adapter: discord
8+
description: "Discord bot bridge"
9+
10+
credentials:
11+
token_env: DISCORD_BOT_TOKEN
12+
allowed_env: ALLOWED_CHANNEL_IDS
13+
14+
messaging:
15+
session_prefix: dc
16+
max_chunk_size: 1900
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
bridge:
5+
name: slack
6+
type: messaging
7+
adapter: slack
8+
description: "Slack bot bridge (Socket Mode)"
9+
10+
credentials:
11+
token_env: SLACK_BOT_TOKEN
12+
extra_env:
13+
- SLACK_APP_TOKEN
14+
allowed_env: ALLOWED_CHANNEL_IDS
15+
16+
messaging:
17+
session_prefix: sl
18+
max_chunk_size: 3000
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
bridge:
5+
name: telegram
6+
type: messaging
7+
adapter: telegram
8+
description: "Telegram Bot API bridge"
9+
10+
credentials:
11+
token_env: TELEGRAM_BOT_TOKEN
12+
allowed_env: ALLOWED_CHAT_IDS
13+
14+
messaging:
15+
session_prefix: tg
16+
max_chunk_size: 4000
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
/**
5+
* Discord messaging adapter.
6+
*
7+
* Uses discord.js for WebSocket-based gateway connection.
8+
*/
9+
10+
const { Client, GatewayIntentBits } = require("discord.js");
11+
12+
module.exports = function createAdapter(config) {
13+
const TOKEN = process.env[config.credentials.token_env];
14+
const ALLOWED = process.env[config.credentials.allowed_env]
15+
? process.env[config.credentials.allowed_env].split(",").map((s) => s.trim())
16+
: null;
17+
18+
return {
19+
name: "discord",
20+
21+
start(onMessage) {
22+
return new Promise((resolve, reject) => {
23+
const client = new Client({
24+
intents: [
25+
GatewayIntentBits.Guilds,
26+
GatewayIntentBits.GuildMessages,
27+
GatewayIntentBits.MessageContent,
28+
GatewayIntentBits.DirectMessages,
29+
],
30+
});
31+
32+
client.on("messageCreate", async (message) => {
33+
if (message.author.bot) return;
34+
35+
const channelId = message.channel.id;
36+
if (ALLOWED && !ALLOWED.includes(channelId)) return;
37+
if (!message.content) return;
38+
39+
await onMessage({
40+
channelId,
41+
userName: message.author.username,
42+
text: message.content,
43+
async sendTyping() {
44+
await message.channel.sendTyping().catch(() => {});
45+
},
46+
async reply(text) {
47+
await message.reply(text);
48+
},
49+
});
50+
});
51+
52+
client.once("ready", () => resolve(client.user.tag));
53+
client.login(TOKEN).catch(reject);
54+
});
55+
},
56+
};
57+
};
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
/**
5+
* Slack messaging adapter.
6+
*
7+
* Uses @slack/bolt in Socket Mode (requires SLACK_APP_TOKEN).
8+
*/
9+
10+
const { App } = require("@slack/bolt");
11+
12+
module.exports = function createAdapter(config) {
13+
const BOT_TOKEN = process.env[config.credentials.token_env];
14+
const APP_TOKEN = process.env.SLACK_APP_TOKEN;
15+
const ALLOWED = process.env[config.credentials.allowed_env]
16+
? process.env[config.credentials.allowed_env].split(",").map((s) => s.trim())
17+
: null;
18+
19+
if (!APP_TOKEN) {
20+
console.error("SLACK_APP_TOKEN required (xapp-... for Socket Mode)");
21+
process.exit(1);
22+
}
23+
24+
return {
25+
name: "slack",
26+
27+
async start(onMessage) {
28+
const app = new App({
29+
token: BOT_TOKEN,
30+
appToken: APP_TOKEN,
31+
socketMode: true,
32+
});
33+
34+
app.message(async ({ message, say }) => {
35+
if (message.subtype) return;
36+
if (!message.text) return;
37+
38+
const channelId = message.channel;
39+
if (ALLOWED && !ALLOWED.includes(channelId)) return;
40+
41+
await onMessage({
42+
channelId,
43+
userName: message.user || "someone",
44+
text: message.text,
45+
async sendTyping() {
46+
// Slack doesn't have a direct typing indicator API for bots
47+
},
48+
async reply(text) {
49+
await say({ text, thread_ts: message.ts });
50+
},
51+
});
52+
});
53+
54+
await app.start();
55+
return "Slack Bot (Socket Mode)";
56+
},
57+
};
58+
};
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
/**
5+
* Telegram messaging adapter.
6+
*
7+
* Uses the Telegram Bot API via long polling (no external dependencies).
8+
*/
9+
10+
const https = require("https");
11+
12+
module.exports = function createAdapter(config) {
13+
const TOKEN = process.env[config.credentials.token_env];
14+
const ALLOWED = process.env[config.credentials.allowed_env]
15+
? process.env[config.credentials.allowed_env].split(",").map((s) => s.trim())
16+
: null;
17+
18+
let offset = 0;
19+
20+
function tgApi(method, body) {
21+
return new Promise((resolve, reject) => {
22+
const data = JSON.stringify(body);
23+
const req = https.request(
24+
{
25+
hostname: "api.telegram.org",
26+
path: `/bot${TOKEN}/${method}`,
27+
method: "POST",
28+
headers: { "Content-Type": "application/json", "Content-Length": Buffer.byteLength(data) },
29+
},
30+
(res) => {
31+
let buf = "";
32+
res.on("data", (c) => (buf += c));
33+
res.on("end", () => {
34+
try { resolve(JSON.parse(buf)); } catch { resolve({ ok: false, error: buf }); }
35+
});
36+
},
37+
);
38+
req.on("error", reject);
39+
req.write(data);
40+
req.end();
41+
});
42+
}
43+
44+
return {
45+
name: "telegram",
46+
47+
async start(onMessage) {
48+
const me = await tgApi("getMe", {});
49+
if (!me.ok) {
50+
throw new Error(`Failed to connect to Telegram: ${JSON.stringify(me)}`);
51+
}
52+
53+
const botName = `@${me.result.username}`;
54+
55+
async function poll() {
56+
try {
57+
const res = await tgApi("getUpdates", { offset, timeout: 30 });
58+
if (res.ok && res.result?.length > 0) {
59+
for (const update of res.result) {
60+
offset = update.update_id + 1;
61+
const msg = update.message;
62+
if (!msg?.text) continue;
63+
64+
const channelId = String(msg.chat.id);
65+
if (ALLOWED && !ALLOWED.includes(channelId)) continue;
66+
67+
const userName = msg.from?.first_name || "someone";
68+
69+
await onMessage({
70+
channelId,
71+
userName,
72+
text: msg.text,
73+
async sendTyping() {
74+
await tgApi("sendChatAction", { chat_id: channelId, action: "typing" }).catch(() => {});
75+
},
76+
async reply(text) {
77+
await tgApi("sendMessage", {
78+
chat_id: channelId,
79+
text,
80+
reply_to_message_id: msg.message_id,
81+
parse_mode: "Markdown",
82+
}).catch(() =>
83+
tgApi("sendMessage", { chat_id: channelId, text, reply_to_message_id: msg.message_id }),
84+
);
85+
},
86+
});
87+
}
88+
}
89+
} catch (err) {
90+
console.error("Poll error:", err.message);
91+
}
92+
setTimeout(poll, 100);
93+
}
94+
95+
poll();
96+
return botName;
97+
},
98+
};
99+
};

0 commit comments

Comments
 (0)