feat: loadable openclaw channel plugin v1 (agent=account)

Rewritten against the real openclaw v2026.5.7 plugin SDK (generic
third-party channel path): createChannelPluginBase + createChatChannelPlugin
with required capabilities, minimal ChannelSetupAdapter, agent=account
config resolution, attached outbound -> Fabric POST, inbound socket per
account -> runtime.channel.turn (wakeup->admission). Compat notes mark
SDK-coupled seams for future openclaw upgrades. Verified: builds clean,
installs, 'openclaw channels list' -> Fabric installed/configured/enabled.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
h z
2026-05-15 17:54:32 +01:00
parent 7fed6d07f6
commit ece77ff2c7
8 changed files with 412 additions and 159 deletions

41
dist/fabric/src/accounts.js vendored Normal file
View File

@@ -0,0 +1,41 @@
// agent = openclaw channel account.
// Config shape:
// channels.fabric.centerApiBase = "http://localhost:7001/api" (shared)
// channels.fabric.accounts.<agentId> = { fabricApiKey, centerApiBase? }
// Each account id IS the openclaw agentId that owns that Fabric identity.
const DEFAULT_CENTER = 'http://localhost:7001/api';
function section(cfg) {
return cfg.channels?.fabric ?? {};
}
export function listFabricAccountIds(cfg) {
const accts = section(cfg).accounts ?? {};
const ids = Object.keys(accts);
return ids.length ? ids : ['default'];
}
export function resolveDefaultFabricAccountId(cfg) {
const s = section(cfg);
if (s.defaultAccount)
return s.defaultAccount;
const ids = listFabricAccountIds(cfg);
return ids[0] ?? 'default';
}
export function resolveFabricAccount(cfg, accountId) {
const s = section(cfg);
const id = accountId ?? resolveDefaultFabricAccountId(cfg);
const acc = s.accounts?.[id] ?? {};
const fabricApiKey = (acc.fabricApiKey ?? '').trim();
const centerApiBase = (acc.centerApiBase ?? s.centerApiBase ?? DEFAULT_CENTER).trim();
return {
accountId: id,
enabled: acc.enabled !== false && s.enabled !== false,
centerApiBase,
fabricApiKey,
allowFrom: acc.allowFrom ?? s.allowFrom ?? [],
dmPolicy: acc.dmPolicy ?? s.dmPolicy,
};
}
export function listEnabledFabricAccounts(cfg) {
return listFabricAccountIds(cfg)
.map((id) => resolveFabricAccount(cfg, id))
.filter((a) => a.enabled && a.fabricApiKey);
}

View File

@@ -1,46 +1,91 @@
import { createChatChannelPlugin, createChannelPluginBase, } from 'openclaw/plugin-sdk/channel-core';
export function resolveFabricAccount(cfg, accountId) {
const section = cfg.channels?.['fabric'];
const centerApiBase = section?.centerApiBase;
if (!centerApiBase)
throw new Error('fabric: channels.fabric.centerApiBase is required');
return {
accountId: accountId ?? null,
centerApiBase,
allowFrom: section?.allowFrom ?? [],
dmPolicy: section?.dmSecurity,
};
// Fabric channel plugin object (third-party `createChatChannelPlugin` path).
//
// COMPAT NOTE (openclaw v2026.5.7 plugin SDK):
// We depend on these generic SDK shapes — re-verify on openclaw upgrade:
// - createChannelPluginBase requires `capabilities`
// - ChannelSetupAdapter: only `applyAccountConfig` is required
// - outbound attachedResults.sendText returns Omit<OutboundDeliveryResult,
// "channel"> (so `messageId: string` is required)
// Casts at the createChatChannelPlugin boundary are intentional and
// localized; keep them here so upgrades touch one file.
import { createChatChannelPlugin, createChannelPluginBase, } from 'openclaw/plugin-sdk/core';
import { FabricClient } from './fabric-client.js';
import { listFabricAccountIds, resolveFabricAccount, resolveDefaultFabricAccountId, } from './accounts.js';
// Posts an agent's reply to Fabric. `to` is the Fabric channelId; `accountId`
// is the agentId (= Fabric identity). One auth concept: account apiKey ->
// agent/login -> guild token -> POST message.
async function sendToFabric(cfg, accountId, to, text) {
const acc = resolveFabricAccount(cfg, accountId);
if (!acc.fabricApiKey)
throw new Error(`fabric account ${acc.accountId} has no fabricApiKey`);
const client = new FabricClient(acc.centerApiBase);
const session = await client.agentLogin(acc.fabricApiKey);
// find which guild owns this channel by probing each guild's channel list
for (const g of session.guilds) {
const gt = session.guildAccessTokens.find((t) => t.guildNodeId === g.nodeId)?.token;
if (!gt)
continue;
const res = await fetch(`${g.endpoint}/api/channels?guildId=${encodeURIComponent(g.nodeId)}`, {
headers: { authorization: `Bearer ${gt}` },
});
const channels = res.ok ? (await res.json()) : [];
if (channels.some((c) => c.id === to)) {
await client.postMessage(g.endpoint, gt, to, text, session.user.id);
return { messageId: `${to}:${Date.now()}` };
}
}
// fallback: first guild
const g = session.guilds[0];
const gt = session.guildAccessTokens.find((t) => t.guildNodeId === g?.nodeId)?.token;
if (g && gt) {
await client.postMessage(g.endpoint, gt, to, text, session.user.id);
return { messageId: `${to}:${Date.now()}` };
}
throw new Error('fabric: no guild available to deliver');
}
// Outbound is wired by the entry (it needs the identity registry + client to
// post as the right agent). Channel-turn visible replies go through the
// inbound adapter's delivery callback; this object owns config/security only.
export function buildFabricChannelPlugin(sendText) {
return createChatChannelPlugin({
base: createChannelPluginBase({
id: 'fabric',
setup: {
resolveAccount: resolveFabricAccount,
inspectAccount(cfg, accountId) {
const section = cfg.channels?.['fabric'];
const ok = Boolean(section?.centerApiBase);
return { enabled: ok, configured: ok, tokenStatus: ok ? 'available' : 'missing' };
},
},
}),
security: {
dm: {
channelKey: 'fabric',
resolvePolicy: (a) => a.dmPolicy,
resolveAllowFrom: (a) => a.allowFrom,
defaultPolicy: 'allowlist',
export const fabricChannelPlugin = createChatChannelPlugin({
base: createChannelPluginBase({
id: 'fabric',
meta: { id: 'fabric', label: 'Fabric', blurb: 'Connect OpenClaw agents to a Fabric guild.' },
capabilities: {
chatTypes: ['channel', 'group', 'direct'],
reactions: false,
threads: false,
media: false,
nativeCommands: false,
blockStreaming: true,
},
reload: { configPrefixes: ['channels.fabric'] },
config: {
listAccountIds: (cfg) => listFabricAccountIds(cfg),
resolveAccount: (cfg, accountId) => resolveFabricAccount(cfg, accountId),
defaultAccountId: (cfg) => resolveDefaultFabricAccountId(cfg),
isConfigured: (account) => Boolean(account.fabricApiKey),
},
// Minimal setup adapter: Fabric is configured directly under
// channels.fabric.* (no interactive wizard). applyAccountConfig is the
// only required member.
setup: {
applyAccountConfig: ({ cfg }) => cfg,
},
}),
security: {
dm: {
channelKey: 'fabric',
resolvePolicy: (a) => a.dmPolicy,
resolveAllowFrom: (a) => a.allowFrom,
defaultPolicy: 'allowlist',
},
},
threading: { topLevelReplyToMode: 'channel' },
outbound: {
base: {},
attachedResults: {
channel: 'fabric',
sendText: async (ctx) => {
const cfg = (ctx.cfg ?? {});
return sendToFabric(cfg, ctx.accountId ?? null, ctx.to, ctx.text);
},
},
// Fabric replies thread by being posted into the same channel.
threading: { topLevelReplyToMode: 'channel' },
outbound: {
attachedResults: {
sendText: async (params) => sendText(params),
},
},
});
}
},
});

View File

@@ -7,17 +7,22 @@ export class FabricInbound {
client;
identity;
log;
accounts;
sockets = [];
timers = [];
seen = new Set();
constructor(runtime, client, identity, log) {
constructor(runtime, client, identity, log, accounts = []) {
this.runtime = runtime;
this.client = client;
this.identity = identity;
this.log = log;
this.accounts = accounts;
}
async start() {
for (const entry of this.identity.list()) {
const entries = this.accounts.length > 0
? this.accounts.map((a) => ({ agentId: a.agentId, fabricApiKey: a.fabricApiKey }))
: this.identity.list();
for (const entry of entries) {
try {
const session = await this.client.agentLogin(entry.fabricApiKey);
this.identity.upsert({