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:
@@ -11,6 +11,8 @@
|
||||
import {
|
||||
createChatChannelPlugin,
|
||||
createChannelPluginBase,
|
||||
buildChannelOutboundSessionRoute,
|
||||
type ChannelOutboundSessionRouteParams,
|
||||
} from 'openclaw/plugin-sdk/core';
|
||||
import { FabricClient } from './fabric-client.js';
|
||||
import {
|
||||
@@ -22,6 +24,39 @@ import {
|
||||
|
||||
type AnyCfg = { channels?: { fabric?: unknown }; [k: string]: unknown };
|
||||
|
||||
// ---- target grammar: fabric:<channelId> ----
|
||||
export function stripFabricTargetPrefix(raw: string): string | undefined {
|
||||
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: string): string | undefined {
|
||||
const id = stripFabricTargetPrefix(raw);
|
||||
return id ? `fabric:${id}`.toLowerCase() : undefined;
|
||||
}
|
||||
export function looksLikeFabricTargetId(raw: string): boolean {
|
||||
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: ChannelOutboundSessionRouteParams) {
|
||||
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.
|
||||
@@ -31,6 +66,7 @@ async function sendToFabric(
|
||||
to: string,
|
||||
text: string,
|
||||
): Promise<{ messageId: string }> {
|
||||
const channelId = stripFabricTargetPrefix(to) ?? to;
|
||||
const acc = resolveFabricAccount(cfg as never, accountId);
|
||||
if (!acc.fabricApiKey) throw new Error(`fabric account ${acc.accountId} has no fabricApiKey`);
|
||||
const client = new FabricClient(acc.centerApiBase);
|
||||
@@ -43,23 +79,24 @@ async function sendToFabric(
|
||||
headers: { authorization: `Bearer ${gt}` },
|
||||
});
|
||||
const channels = res.ok ? ((await res.json()) as Array<{ id: string }>) : [];
|
||||
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<ResolvedFabricAccount>({
|
||||
base: createChannelPluginBase<ResolvedFabricAccount>({
|
||||
base: {
|
||||
...createChannelPluginBase<ResolvedFabricAccount>({
|
||||
id: 'fabric',
|
||||
meta: { id: 'fabric', label: 'Fabric', blurb: 'Connect OpenClaw agents to a Fabric guild.' },
|
||||
capabilities: {
|
||||
@@ -83,7 +120,15 @@ export const fabricChannelPlugin = createChatChannelPlugin<ResolvedFabricAccount
|
||||
setup: {
|
||||
applyAccountConfig: ({ cfg }: { cfg: unknown }) => cfg as never,
|
||||
} as never,
|
||||
}) as never,
|
||||
}),
|
||||
messaging: {
|
||||
normalizeTarget: normalizeFabricTarget,
|
||||
resolveSessionTarget: ({ id }: { id: string }) => normalizeFabricTarget(`fabric:${id}`),
|
||||
resolveOutboundSessionRoute: (params: ChannelOutboundSessionRouteParams) =>
|
||||
resolveFabricOutboundSessionRoute(params),
|
||||
targetResolver: { looksLikeId: looksLikeFabricTargetId, hint: '<channelId|fabric:ID>' },
|
||||
},
|
||||
} as never,
|
||||
|
||||
security: {
|
||||
dm: {
|
||||
@@ -100,9 +145,23 @@ export const fabricChannelPlugin = createChatChannelPlugin<ResolvedFabricAccount
|
||||
base: {},
|
||||
attachedResults: {
|
||||
channel: 'fabric',
|
||||
sendText: async (ctx: { accountId?: string | null; to: string; text: string; cfg?: unknown }) => {
|
||||
const cfg = (ctx.cfg ?? {}) as AnyCfg;
|
||||
return sendToFabric(cfg, ctx.accountId ?? null, ctx.to, ctx.text);
|
||||
sendText: async (ctx: {
|
||||
accountId?: string | null;
|
||||
to: string;
|
||||
text: string;
|
||||
cfg?: unknown;
|
||||
config?: unknown;
|
||||
}) => {
|
||||
// openclaw passes config under cfg or config depending on path
|
||||
const cfg = (ctx.cfg ?? ctx.config ?? {}) as AnyCfg;
|
||||
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;
|
||||
}
|
||||
},
|
||||
},
|
||||
} as never,
|
||||
|
||||
Reference in New Issue
Block a user