feat: working v1 — full Fabric<->openclaw round-trip verified

Real channel-turn dispatch (resolveAgentRoute + finalizeInboundContext +
dispatchInboundReplyWithBase), wakeup->drop/dispatch, messaging target
grammar (fabric:<id>) + outbound.sendText, tools use execute/parameters.
Verified live: human msg in Fabric -> wakeup -> openclaw agent runs ->
reply posted back into the Fabric channel as the agent.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
h z
2026-05-15 18:24:35 +01:00
parent ece77ff2c7
commit 9cb262367e
9 changed files with 349 additions and 195 deletions

View File

@@ -8,13 +8,52 @@
// "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 { createChatChannelPlugin, createChannelPluginBase, buildChannelOutboundSessionRoute, } from 'openclaw/plugin-sdk/core';
import { FabricClient } from './fabric-client.js';
import { listFabricAccountIds, resolveFabricAccount, resolveDefaultFabricAccountId, } from './accounts.js';
// ---- target grammar: fabric:<channelId> ----
export function stripFabricTargetPrefix(raw) {
let s = (raw ?? '').trim();
if (!s)
return undefined;
if (s.toLowerCase().startsWith('fabric:'))
s = s.slice('fabric:'.length).trim();
if (s.toLowerCase().startsWith('channel:'))
s = s.slice('channel:'.length).trim();
return s || undefined;
}
export function normalizeFabricTarget(raw) {
const id = stripFabricTargetPrefix(raw);
return id ? `fabric:${id}`.toLowerCase() : undefined;
}
export function looksLikeFabricTargetId(raw) {
const t = (raw ?? '').trim();
if (!t)
return false;
if (/^fabric:/i.test(t))
return true;
return /^[a-z0-9-]{8,}$/i.test(t);
}
export function resolveFabricOutboundSessionRoute(params) {
const id = stripFabricTargetPrefix(params.target);
if (!id)
return null;
return buildChannelOutboundSessionRoute({
cfg: params.cfg,
agentId: params.agentId,
channel: 'fabric',
accountId: params.accountId,
peer: { kind: 'group', id },
chatType: 'group',
from: `fabric:channel:${id}`,
to: `fabric:${id}`,
});
}
// 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 channelId = stripFabricTargetPrefix(to) ?? to;
const acc = resolveFabricAccount(cfg, accountId);
if (!acc.fabricApiKey)
throw new Error(`fabric account ${acc.accountId} has no fabricApiKey`);
@@ -29,46 +68,54 @@ async function sendToFabric(cfg, accountId, to, text) {
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()}` };
if (channels.some((c) => c.id === channelId)) {
await client.postMessage(g.endpoint, gt, channelId, text, session.user.id);
return { messageId: `${channelId}:${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()}` };
await client.postMessage(g.endpoint, gt, channelId, text, session.user.id);
return { messageId: `${channelId}:${Date.now()}` };
}
throw new Error('fabric: no guild available to deliver');
}
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,
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,
},
}),
messaging: {
normalizeTarget: normalizeFabricTarget,
resolveSessionTarget: ({ id }) => normalizeFabricTarget(`fabric:${id}`),
resolveOutboundSessionRoute: (params) => resolveFabricOutboundSessionRoute(params),
targetResolver: { looksLikeId: looksLikeFabricTargetId, hint: '<channelId|fabric:ID>' },
},
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',
@@ -83,8 +130,17 @@ export const fabricChannelPlugin = createChatChannelPlugin({
attachedResults: {
channel: 'fabric',
sendText: async (ctx) => {
const cfg = (ctx.cfg ?? {});
return sendToFabric(cfg, ctx.accountId ?? null, ctx.to, ctx.text);
// openclaw passes config under cfg or config depending on path
const cfg = (ctx.cfg ?? ctx.config ?? {});
try {
const r = await sendToFabric(cfg, ctx.accountId ?? null, ctx.to, ctx.text);
console.log(`[fabric] outbound.sendText -> ${ctx.to} ok`);
return r;
}
catch (e) {
console.log(`[fabric] outbound.sendText FAILED to=${ctx.to}: ${String(e)}`);
throw e;
}
},
},
},