Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
184 changes: 8 additions & 176 deletions src/team-relay/adapters/comments.mjs
Original file line number Diff line number Diff line change
@@ -1,177 +1,9 @@
// SPDX-License-Identifier: AGPL-3.0-only

import { execFileSync } from 'node:child_process';
import { randomUUID } from 'node:crypto';

/**
* Comments adapter — polls Moltbook posts and GitHub issues/discussions
* for new comments. Composite adapter covering two sources.
*/

function fetchMoltbookComments(postId, baseUrl = 'https://www.moltbook.com') {
try {
const url = `${baseUrl}/api/v1/posts/${postId}/comments`;
const result = execFileSync('curl', ['-sS', url], {
encoding: 'utf8', timeout: 15000
});
const data = JSON.parse(result);
return Array.isArray(data) ? data : (data.comments || []);
} catch (e) {
console.error(` moltbook ${postId.slice(0, 8)} failed: ${e.message}`);
return [];
}
}

function fetchGitHubComments(owner, repo, number, type = 'issues', token = '') {
const endpoint = type === 'discussion'
? `https://api.github.com/repos/${owner}/${repo}/discussions/${number}/comments`
: `https://api.github.com/repos/${owner}/${repo}/issues/${number}/comments`;
const curlArgs = ['-sS'];
if (token) curlArgs.push('-H', `Authorization: token ${token}`);
curlArgs.push(`${endpoint}?per_page=50&sort=created&direction=desc`);
try {
const result = execFileSync('curl', curlArgs, {
encoding: 'utf8', timeout: 15000
});
const data = JSON.parse(result);
return Array.isArray(data) ? data : [];
} catch (e) {
console.error(` github ${owner}/${repo}#${number} failed: ${e.message}`);
return [];
}
}

function fetchGitHubIssues(owner, repo, token = '') {
const curlArgs = ['-sS'];
if (token) curlArgs.push('-H', `Authorization: token ${token}`);
curlArgs.push(`https://api.github.com/repos/${owner}/${repo}/issues?state=open&sort=updated&direction=desc&per_page=10`);
try {
const result = execFileSync('curl', curlArgs, {
encoding: 'utf8', timeout: 15000
});
const data = JSON.parse(result);
return Array.isArray(data) ? data : [];
} catch {
return [];
}
}

export const commentsAdapter = {
name: 'comments',

async fetch(config) {
const commentsCfg = config?.comments || {};
const all = [];

// Moltbook comments
const moltbookCfg = commentsCfg.moltbook || {};
const posts = moltbookCfg.posts || [];
const moltBaseUrl = moltbookCfg.base_url || 'https://www.moltbook.com';

for (const postId of posts) {
const comments = fetchMoltbookComments(postId, moltBaseUrl);
for (const c of comments) {
c._source = 'moltbook';
c._postId = postId;
c._baseUrl = moltBaseUrl;
}
all.push(...comments);
}

// GitHub comments
const githubCfg = commentsCfg.github || {};
const repos = githubCfg.repos || [];
const ghToken = githubCfg.token || '';

for (const repoCfg of repos) {
const { owner, repo, type } = repoCfg;

if (repoCfg.number) {
const comments = fetchGitHubComments(owner, repo, repoCfg.number, type, ghToken);
for (const c of comments) {
c._source = 'github';
c._owner = owner;
c._repo = repo;
c._number = repoCfg.number;
c._type = type || 'issues';
}
all.push(...comments);
} else if (type === 'issues') {
const issues = fetchGitHubIssues(owner, repo, ghToken);
for (const issue of issues) {
if (!issue.comments || issue.comments === 0) continue;
const comments = fetchGitHubComments(owner, repo, issue.number, 'issues', ghToken);
for (const c of comments) {
c._source = 'github';
c._owner = owner;
c._repo = repo;
c._number = issue.number;
c._type = 'issues';
}
all.push(...comments);
}
}
}

return all;
},

getKey(msg) {
const cid = msg.id || '';
if (!cid) return null;
if (msg._source === 'moltbook') return `moltbook:${cid}`;
if (msg._source === 'github') return `github:${msg._owner}/${msg._repo}:${cid}`;
return `comment:${cid}`;
},

shouldSkip() {
return false;
},

normalize(msg) {
if (msg._source === 'moltbook') {
const author = typeof msg.author === 'object' ? msg.author?.name : (msg.author || '?');
return {
trace_id: randomUUID(),
event_id: msg.id,
source: 'moltbook',
kind: 'moltbook.comment.created',
timestamp: msg.created_at || msg.createdAt || new Date().toISOString(),
actor: { login: author },
payload: {
body: (msg.body || msg.content || '').slice(0, 500),
post_id: msg._postId,
comment_id: msg.id,
url: `${msg._baseUrl}/post/${msg._postId}#comment-${msg.id}`
}
};
}

// GitHub
return {
trace_id: randomUUID(),
event_id: String(msg.id),
source: 'github',
kind: `github.${msg._type}.comment.created`,
timestamp: msg.created_at || new Date().toISOString(),
actor: { login: msg.user?.login || '?' },
payload: {
body: (msg.body || '').slice(0, 500),
repo: `${msg._owner}/${msg._repo}`,
number: msg._number,
comment_id: String(msg.id),
url: msg.html_url || `https://github.com/${msg._owner}/${msg._repo}/issues/${msg._number}`
}
};
},

formatLine(event) {
const ts = (event.timestamp || '').slice(0, 19);
const sender = event.actor?.login || '?';
const body = (event.payload?.body || '').replace(/\n/g, ' ').slice(0, 200);
if (event.source === 'moltbook') {
return `[${ts}] [moltbook/${event.payload?.post_id?.slice(0, 8)}] ${sender}: ${body}`;
}
return `[${ts}] [${event.payload?.repo}#${event.payload?.number}] ${sender}: ${body}`;
}
};
//
// RE-EXPORT SHIM - the canonical implementation lives in src/adapters/.
// This file used to be a full COPY, and the two trees drifted: the CLI
// imported this stale copy, so fixes landed in src/adapters/ (e.g. the
// reply-target work, PR #72) never reached the running poller. Discovered
// 27 Aug 2026, the same day the same disease was found in the webhook
// servers. One implementation, importable from both historical paths.
export { commentsAdapter } from '../../adapters/comments.mjs';
107 changes: 8 additions & 99 deletions src/team-relay/adapters/discord.mjs
Original file line number Diff line number Diff line change
@@ -1,100 +1,9 @@
// SPDX-License-Identifier: AGPL-3.0-only

import { execFileSync } from 'node:child_process';
import { randomUUID } from 'node:crypto';

/**
* Discord adapter — polls Discord channels via OpenClaw CLI.
*/

const OC_DEFAULTS = {
home: process.env.OPENCLAW_HOME || '',
bin: process.env.OPENCLAW_BIN || 'openclaw',
ssh: process.env.OPENCLAW_SSH || ''
};

function resolveOC(config) {
const oc = config?.openclaw || {};
return {
home: oc.home || OC_DEFAULTS.home,
bin: oc.bin || OC_DEFAULTS.bin,
ssh: oc.ssh || OC_DEFAULTS.ssh
};
}

export const discordAdapter = {
name: 'discord',

async fetch(config) {
const discordCfg = config?.discord || {};
const channels = discordCfg.channels || [];
const oc = resolveOC(config);
const all = [];

for (const ch of channels) {
const channelId = typeof ch === 'string' ? ch : ch.id;
const channelName = typeof ch === 'string' ? ch : (ch.name || ch.id);
const envPrefix = `export PATH=/opt/homebrew/bin:$PATH && export OPENCLAW_HOME=${oc.home}`;
const ocCmd = `${oc.bin} message read --channel discord --target ${channelId} --limit 20 --json`;
try {
let result;
if (oc.ssh) {
result = execFileSync('ssh', [oc.ssh, `${envPrefix} && ${ocCmd}`], {
encoding: 'utf8', timeout: 30000, stdio: ['pipe', 'pipe', 'pipe']
});
} else {
result = execFileSync(oc.bin, [
'message', 'read', '--channel', 'discord', '--target', channelId, '--limit', '20', '--json'
], {
encoding: 'utf8', timeout: 30000, stdio: ['pipe', 'pipe', 'pipe'],
env: { ...process.env, OPENCLAW_HOME: oc.home }
});
}
const data = JSON.parse(result);
if (!data?.payload?.ok) continue;
const msgs = data.payload.messages || [];
for (const m of msgs) {
m._channelName = channelName;
m._channelId = channelId;
}
all.push(...msgs);
} catch (e) {
console.error(` discord fetch ${channelName} failed: ${e.message?.slice(0, 120) || 'unknown error'}`);
}
}
return all;
},

getKey(msg) {
return msg.id ? `discord:${msg.id}` : null;
},

shouldSkip(msg, config) {
const selfId = config?.discord?.self_id || '';
const skipBots = config?.discord?.skip_bots || false;
if (selfId && msg.author?.id === selfId) return true;
if (skipBots && msg.author?.bot) return true;
return false;
},

normalize(msg) {
return {
trace_id: randomUUID(),
event_id: msg.id,
source: 'discord',
kind: 'discord.message.created',
timestamp: msg.timestamp || new Date().toISOString(),
channel: msg._channelName,
actor: { login: msg.author?.username || '?' },
payload: { body: (msg.content || '').slice(0, 500), channel_id: msg._channelId }
};
},

formatLine(event) {
const ts = (event.timestamp || '').slice(0, 19);
const sender = event.actor?.login || '?';
const channel = event.channel || '';
const body = (event.payload?.body || '').replace(/\n/g, ' ').slice(0, 200);
return `[${ts}] [discord/${channel}] ${sender}: ${body}`;
}
};
//
// RE-EXPORT SHIM - the canonical implementation lives in src/adapters/.
// This file used to be a full COPY, and the two trees drifted: the CLI
// imported this stale copy, so fixes landed in src/adapters/ (e.g. the
// reply-target work, PR #72) never reached the running poller. Discovered
// 27 Aug 2026, the same day the same disease was found in the webhook
// servers. One implementation, importable from both historical paths.
export { discordAdapter } from '../../adapters/discord.mjs';
81 changes: 8 additions & 73 deletions src/team-relay/adapters/groupmind.mjs
Original file line number Diff line number Diff line change
@@ -1,74 +1,9 @@
// SPDX-License-Identifier: AGPL-3.0-only

import { execFileSync } from 'node:child_process';
import { randomUUID } from 'node:crypto';

/**
* GroupMind adapter — polls GroupMind rooms for new messages.
*/

export const groupmindAdapter = {
name: 'groupmind',

async fetch(config, opts = {}) {
const poller = config?.poller || {};
const rooms = poller.rooms || [];
const apiKey = poller.api_key;
const limit = opts.seed ? 50 : 10;
const all = [];

for (const room of rooms) {
const url = `https://groupmind.one/api/v1/rooms/${room}/messages?limit=${limit}`;
try {
const result = execFileSync('curl', ['-sS', '-H', `X-API-Key: ${apiKey}`, url], {
encoding: 'utf8', timeout: 15000
});
const data = JSON.parse(result);
const msgs = data.messages || (Array.isArray(data) ? data : []);
for (const m of msgs) {
m._room = room;
}
all.push(...msgs);
} catch (e) {
console.error(` groupmind fetch ${room} failed: ${e.message}`);
}
}
return all;
},

getKey(msg) {
return msg.id || null;
},

shouldSkip(msg, config) {
const selfHandle = config?.poller?.handle || '@unknown';
const sender = msg.from || msg.sender || '?';
return sender === selfHandle || sender === selfHandle.replace('@', '');
},

normalize(msg, config) {
const sender = msg.from || msg.sender || '?';
const body = (msg.body || '').slice(0, 500);
const ts = msg.created_at || new Date().toISOString();
const room = msg._room || '';

return {
trace_id: randomUUID(),
event_id: msg.id,
source: 'groupmind',
kind: 'groupmind.message.created',
timestamp: ts,
room,
actor: { login: sender },
payload: { body, room }
};
},

formatLine(event) {
const ts = (event.timestamp || '').slice(0, 19);
const sender = event.actor?.login || '?';
const room = event.room || '';
const body = (event.payload?.body || '').replace(/\n/g, ' ').slice(0, 200);
return `[${ts}] [${room}] ${sender}: ${body}`;
}
};
//
// RE-EXPORT SHIM - the canonical implementation lives in src/adapters/.
// This file used to be a full COPY, and the two trees drifted: the CLI
// imported this stale copy, so fixes landed in src/adapters/ (e.g. the
// reply-target work, PR #72) never reached the running poller. Discovered
// 27 Aug 2026, the same day the same disease was found in the webhook
// servers. One implementation, importable from both historical paths.
export { groupmindAdapter } from '../../adapters/groupmind.mjs';
Loading
Loading