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:
2
dist/fabric/index.js
vendored
2
dist/fabric/index.js
vendored
@@ -54,7 +54,7 @@ export default defineChannelPluginEntry({
|
|||||||
api.logger.warn('fabric: runtime not set; inbound disabled');
|
api.logger.warn('fabric: runtime not set; inbound disabled');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
inbound = new FabricInbound(runtimeRef, client, identity, api.logger, accounts);
|
inbound = new FabricInbound(runtimeRef, api.config, client, identity, api.logger, accounts);
|
||||||
void inbound.start();
|
void inbound.start();
|
||||||
api.logger.info(`fabric: inbound started for ${accounts.length} account(s)`);
|
api.logger.info(`fabric: inbound started for ${accounts.length} account(s)`);
|
||||||
});
|
});
|
||||||
|
|||||||
74
dist/fabric/src/channel.js
vendored
74
dist/fabric/src/channel.js
vendored
@@ -8,13 +8,52 @@
|
|||||||
// "channel"> (so `messageId: string` is required)
|
// "channel"> (so `messageId: string` is required)
|
||||||
// Casts at the createChatChannelPlugin boundary are intentional and
|
// Casts at the createChatChannelPlugin boundary are intentional and
|
||||||
// localized; keep them here so upgrades touch one file.
|
// 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 { FabricClient } from './fabric-client.js';
|
||||||
import { listFabricAccountIds, resolveFabricAccount, resolveDefaultFabricAccountId, } from './accounts.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`
|
// Posts an agent's reply to Fabric. `to` is the Fabric channelId; `accountId`
|
||||||
// is the agentId (= Fabric identity). One auth concept: account apiKey ->
|
// is the agentId (= Fabric identity). One auth concept: account apiKey ->
|
||||||
// agent/login -> guild token -> POST message.
|
// agent/login -> guild token -> POST message.
|
||||||
async function sendToFabric(cfg, accountId, to, text) {
|
async function sendToFabric(cfg, accountId, to, text) {
|
||||||
|
const channelId = stripFabricTargetPrefix(to) ?? to;
|
||||||
const acc = resolveFabricAccount(cfg, accountId);
|
const acc = resolveFabricAccount(cfg, accountId);
|
||||||
if (!acc.fabricApiKey)
|
if (!acc.fabricApiKey)
|
||||||
throw new Error(`fabric account ${acc.accountId} has no fabricApiKey`);
|
throw new Error(`fabric account ${acc.accountId} has no fabricApiKey`);
|
||||||
@@ -29,22 +68,23 @@ async function sendToFabric(cfg, accountId, to, text) {
|
|||||||
headers: { authorization: `Bearer ${gt}` },
|
headers: { authorization: `Bearer ${gt}` },
|
||||||
});
|
});
|
||||||
const channels = res.ok ? (await res.json()) : [];
|
const channels = res.ok ? (await res.json()) : [];
|
||||||
if (channels.some((c) => c.id === to)) {
|
if (channels.some((c) => c.id === channelId)) {
|
||||||
await client.postMessage(g.endpoint, gt, to, text, session.user.id);
|
await client.postMessage(g.endpoint, gt, channelId, text, session.user.id);
|
||||||
return { messageId: `${to}:${Date.now()}` };
|
return { messageId: `${channelId}:${Date.now()}` };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// fallback: first guild
|
// fallback: first guild
|
||||||
const g = session.guilds[0];
|
const g = session.guilds[0];
|
||||||
const gt = session.guildAccessTokens.find((t) => t.guildNodeId === g?.nodeId)?.token;
|
const gt = session.guildAccessTokens.find((t) => t.guildNodeId === g?.nodeId)?.token;
|
||||||
if (g && gt) {
|
if (g && gt) {
|
||||||
await client.postMessage(g.endpoint, gt, to, text, session.user.id);
|
await client.postMessage(g.endpoint, gt, channelId, text, session.user.id);
|
||||||
return { messageId: `${to}:${Date.now()}` };
|
return { messageId: `${channelId}:${Date.now()}` };
|
||||||
}
|
}
|
||||||
throw new Error('fabric: no guild available to deliver');
|
throw new Error('fabric: no guild available to deliver');
|
||||||
}
|
}
|
||||||
export const fabricChannelPlugin = createChatChannelPlugin({
|
export const fabricChannelPlugin = createChatChannelPlugin({
|
||||||
base: createChannelPluginBase({
|
base: {
|
||||||
|
...createChannelPluginBase({
|
||||||
id: 'fabric',
|
id: 'fabric',
|
||||||
meta: { id: 'fabric', label: 'Fabric', blurb: 'Connect OpenClaw agents to a Fabric guild.' },
|
meta: { id: 'fabric', label: 'Fabric', blurb: 'Connect OpenClaw agents to a Fabric guild.' },
|
||||||
capabilities: {
|
capabilities: {
|
||||||
@@ -69,6 +109,13 @@ export const fabricChannelPlugin = createChatChannelPlugin({
|
|||||||
applyAccountConfig: ({ cfg }) => cfg,
|
applyAccountConfig: ({ cfg }) => cfg,
|
||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
|
messaging: {
|
||||||
|
normalizeTarget: normalizeFabricTarget,
|
||||||
|
resolveSessionTarget: ({ id }) => normalizeFabricTarget(`fabric:${id}`),
|
||||||
|
resolveOutboundSessionRoute: (params) => resolveFabricOutboundSessionRoute(params),
|
||||||
|
targetResolver: { looksLikeId: looksLikeFabricTargetId, hint: '<channelId|fabric:ID>' },
|
||||||
|
},
|
||||||
|
},
|
||||||
security: {
|
security: {
|
||||||
dm: {
|
dm: {
|
||||||
channelKey: 'fabric',
|
channelKey: 'fabric',
|
||||||
@@ -83,8 +130,17 @@ export const fabricChannelPlugin = createChatChannelPlugin({
|
|||||||
attachedResults: {
|
attachedResults: {
|
||||||
channel: 'fabric',
|
channel: 'fabric',
|
||||||
sendText: async (ctx) => {
|
sendText: async (ctx) => {
|
||||||
const cfg = (ctx.cfg ?? {});
|
// openclaw passes config under cfg or config depending on path
|
||||||
return sendToFabric(cfg, ctx.accountId ?? null, ctx.to, ctx.text);
|
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;
|
||||||
|
}
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|||||||
132
dist/fabric/src/inbound.js
vendored
132
dist/fabric/src/inbound.js
vendored
@@ -1,18 +1,19 @@
|
|||||||
import { io } from 'socket.io-client';
|
import { io } from 'socket.io-client';
|
||||||
// One live Fabric connection per agent identity (Phase 1 = B1). Lives in the
|
import { dispatchInboundReplyWithBase } from 'openclaw/plugin-sdk/inbound-reply-dispatch';
|
||||||
// channel-plugin runtime (no separate sidecar). Firehose (B2) would replace
|
|
||||||
// this class behind the same dispatch() call.
|
|
||||||
export class FabricInbound {
|
export class FabricInbound {
|
||||||
runtime;
|
core;
|
||||||
|
cfg;
|
||||||
client;
|
client;
|
||||||
identity;
|
identity;
|
||||||
log;
|
log;
|
||||||
accounts;
|
accounts;
|
||||||
sockets = [];
|
sockets = [];
|
||||||
timers = [];
|
|
||||||
seen = new Set();
|
seen = new Set();
|
||||||
constructor(runtime, client, identity, log, accounts = []) {
|
constructor(core, // PluginRuntime
|
||||||
this.runtime = runtime;
|
cfg, // OpenClawConfig
|
||||||
|
client, identity, log, accounts = []) {
|
||||||
|
this.core = core;
|
||||||
|
this.cfg = cfg;
|
||||||
this.client = client;
|
this.client = client;
|
||||||
this.identity = identity;
|
this.identity = identity;
|
||||||
this.log = log;
|
this.log = log;
|
||||||
@@ -40,12 +41,9 @@ export class FabricInbound {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
stop() {
|
stop() {
|
||||||
for (const t of this.timers)
|
|
||||||
clearInterval(t);
|
|
||||||
for (const s of this.sockets)
|
for (const s of this.sockets)
|
||||||
s.disconnect();
|
s.disconnect();
|
||||||
this.sockets = [];
|
this.sockets = [];
|
||||||
this.timers = [];
|
|
||||||
}
|
}
|
||||||
async connectAgent(agentId, session) {
|
async connectAgent(agentId, session) {
|
||||||
const selfUserId = session.user.id;
|
const selfUserId = session.user.id;
|
||||||
@@ -60,12 +58,11 @@ export class FabricInbound {
|
|||||||
});
|
});
|
||||||
const joinAll = async () => {
|
const joinAll = async () => {
|
||||||
try {
|
try {
|
||||||
const res = await fetch(`${g.endpoint}/api/channels?guildId=${encodeURIComponent(g.nodeId)}`, {
|
const res = await fetch(`${g.endpoint}/api/channels?guildId=${encodeURIComponent(g.nodeId)}`, { headers: { authorization: `Bearer ${tok}` } });
|
||||||
headers: { authorization: `Bearer ${tok}` },
|
|
||||||
});
|
|
||||||
const channels = res.ok ? (await res.json()) : [];
|
const channels = res.ok ? (await res.json()) : [];
|
||||||
for (const c of channels)
|
for (const c of channels)
|
||||||
socket.emit('join_channel', { channelId: c.id });
|
socket.emit('join_channel', { channelId: c.id });
|
||||||
|
this.log.info(`fabric: agent ${agentId} joined ${channels.length} channel(s) on ${g.nodeId}`);
|
||||||
}
|
}
|
||||||
catch {
|
catch {
|
||||||
/* best effort */
|
/* best effort */
|
||||||
@@ -76,7 +73,6 @@ export class FabricInbound {
|
|||||||
const channelId = m.channelId ?? '';
|
const channelId = m.channelId ?? '';
|
||||||
if (!channelId)
|
if (!channelId)
|
||||||
return;
|
return;
|
||||||
// self-echo guard + dedupe
|
|
||||||
if (m.authorUserId && m.authorUserId === selfUserId)
|
if (m.authorUserId && m.authorUserId === selfUserId)
|
||||||
return;
|
return;
|
||||||
const key = `${agentId}:${m.messageId}`;
|
const key = `${agentId}:${m.messageId}`;
|
||||||
@@ -85,67 +81,75 @@ export class FabricInbound {
|
|||||||
this.seen.add(key);
|
this.seen.add(key);
|
||||||
if (this.seen.size > 5000)
|
if (this.seen.size > 5000)
|
||||||
this.seen.clear();
|
this.seen.clear();
|
||||||
void this.dispatch(agentId, g, channelId, m);
|
void this.dispatch(agentId, g, channelId, m, session);
|
||||||
});
|
});
|
||||||
socket.connect();
|
socket.connect();
|
||||||
this.sockets.push(socket);
|
this.sockets.push(socket);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Hand the inbound Fabric message to OpenClaw's channel-turn kernel.
|
async dispatch(agentId, guild, channelId, m, session) {
|
||||||
// wakeup === true -> dispatch (agent runs, may reply)
|
// wakeup === false -> drop (Fabric already decided this agent is silent)
|
||||||
// wakeup !== true -> drop but keep as group history/context
|
if (m.wakeup !== true) {
|
||||||
async dispatch(agentId, guild, channelId, m) {
|
this.log.info(`fabric: drop (no wakeup) agent=${agentId} channel=${channelId}`);
|
||||||
const admit = m.wakeup === true;
|
return;
|
||||||
|
}
|
||||||
|
this.log.info(`fabric: dispatch agent=${agentId} channel=${channelId}`);
|
||||||
|
const core = this.core;
|
||||||
|
const cfg = this.cfg;
|
||||||
try {
|
try {
|
||||||
await this.runtime.channel.turn.run({
|
const route = core.channel.routing.resolveAgentRoute({
|
||||||
|
cfg: this.cfg,
|
||||||
channel: 'fabric',
|
channel: 'fabric',
|
||||||
accountId: agentId,
|
accountId: agentId,
|
||||||
raw: m,
|
peer: { kind: 'group', id: channelId },
|
||||||
adapter: {
|
|
||||||
ingest: (raw) => ({
|
|
||||||
id: raw.messageId,
|
|
||||||
timestamp: raw.createdAt ? Date.parse(raw.createdAt) : Date.now(),
|
|
||||||
rawText: raw.content,
|
|
||||||
textForAgent: raw.content,
|
|
||||||
}),
|
|
||||||
classify: () => ({ kind: 'message', canStartAgentTurn: admit }),
|
|
||||||
preflight: () => admit ? {} : { admission: { kind: 'drop', reason: 'no-wakeup', recordHistory: true } },
|
|
||||||
resolveTurn: (input) => ({
|
|
||||||
route: {
|
|
||||||
agentId,
|
|
||||||
routeSessionKey: `agent:${agentId}:fabric:channel:${channelId}`,
|
|
||||||
createIfMissing: true,
|
|
||||||
},
|
|
||||||
conversation: { kind: 'channel', id: channelId, label: `fabric:${guild.nodeId}` },
|
|
||||||
reply: { to: channelId, nativeChannelId: channelId },
|
|
||||||
message: {
|
|
||||||
body: m.content,
|
|
||||||
rawBody: m.content,
|
|
||||||
bodyForAgent: m.content,
|
|
||||||
envelopeFrom: m.authorUserId ?? 'fabric',
|
|
||||||
},
|
|
||||||
delivery: {
|
|
||||||
deliver: async (payload) => {
|
|
||||||
const text = typeof payload?.text === 'string' ? payload.text : '';
|
|
||||||
if (!text.trim())
|
|
||||||
return { visibleReplySent: false };
|
|
||||||
const entry = this.identity.findByAgentId(agentId);
|
|
||||||
const session = entry ? await this.client.agentLogin(entry.fabricApiKey) : null;
|
|
||||||
const gt = session?.guildAccessTokens.find((t) => t.guildNodeId === guild.nodeId)?.token;
|
|
||||||
if (!session || !gt)
|
|
||||||
return { visibleReplySent: false };
|
|
||||||
await this.client.postMessage(guild.endpoint, gt, channelId, text, session.user.id);
|
|
||||||
return { visibleReplySent: true };
|
|
||||||
},
|
|
||||||
},
|
|
||||||
meta: { admission: admit ? { kind: 'dispatch' } : { kind: 'drop', recordHistory: true } },
|
|
||||||
}),
|
|
||||||
},
|
|
||||||
log: (e) => this.runtime.log?.debug?.(`fabric.turn.${e?.stage}`),
|
|
||||||
});
|
});
|
||||||
|
const storePath = core.channel.session.resolveStorePath(cfg.session?.store, {
|
||||||
|
agentId: route.agentId,
|
||||||
|
});
|
||||||
|
const ctxPayload = core.channel.reply.finalizeInboundContext({
|
||||||
|
Body: m.content,
|
||||||
|
BodyForAgent: m.content,
|
||||||
|
RawBody: m.content,
|
||||||
|
CommandBody: m.content,
|
||||||
|
From: `fabric:channel:${channelId}`,
|
||||||
|
To: `fabric:${channelId}`,
|
||||||
|
SessionKey: route.sessionKey,
|
||||||
|
AccountId: route.accountId ?? agentId,
|
||||||
|
ChatType: 'group',
|
||||||
|
ConversationLabel: `fabric:${guild.nodeId}`,
|
||||||
|
SenderId: m.authorUserId ?? 'fabric',
|
||||||
|
Provider: 'fabric',
|
||||||
|
Surface: 'fabric',
|
||||||
|
MessageSid: m.messageId,
|
||||||
|
Timestamp: m.createdAt ? Date.parse(m.createdAt) : Date.now(),
|
||||||
|
OriginatingChannel: 'fabric',
|
||||||
|
OriginatingTo: `fabric:${channelId}`,
|
||||||
|
});
|
||||||
|
const gt = session.guildAccessTokens.find((t) => t.guildNodeId === guild.nodeId)?.token;
|
||||||
|
await dispatchInboundReplyWithBase({
|
||||||
|
cfg: this.cfg,
|
||||||
|
channel: 'fabric',
|
||||||
|
accountId: agentId,
|
||||||
|
route,
|
||||||
|
storePath,
|
||||||
|
ctxPayload: ctxPayload,
|
||||||
|
core: this.core,
|
||||||
|
deliver: async (payload) => {
|
||||||
|
const text = (payload?.text ?? '').trim();
|
||||||
|
this.log.info(`fabric: deliver agent=${agentId} channel=${channelId} len=${text.length}`);
|
||||||
|
if (!text || !gt)
|
||||||
|
return;
|
||||||
|
await this.client.postMessage(guild.endpoint, gt, channelId, text, session.user.id);
|
||||||
|
this.log.info(`fabric: posted reply agent=${agentId} channel=${channelId}`);
|
||||||
|
},
|
||||||
|
onRecordError: (err) => this.log.warn(`fabric: session record failed agent=${agentId}: ${String(err)}`),
|
||||||
|
onDispatchError: (err, info) => this.log.warn(`fabric: ${info.kind} dispatch failed agent=${agentId}: ${String(err)}`),
|
||||||
|
replyOptions: {},
|
||||||
|
});
|
||||||
|
this.log.info(`fabric: dispatch returned agent=${agentId} channel=${channelId}`);
|
||||||
}
|
}
|
||||||
catch (err) {
|
catch (err) {
|
||||||
this.log.warn(`fabric: turn.run failed agent=${agentId} channel=${channelId}: ${String(err)}`);
|
this.log.warn(`fabric: dispatch failed agent=${agentId} channel=${channelId}: ${String(err)}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
12
dist/fabric/src/tools.js
vendored
12
dist/fabric/src/tools.js
vendored
@@ -21,7 +21,7 @@ export function registerFabricTools(api, client, identity) {
|
|||||||
api.registerTool((ctx) => ({
|
api.registerTool((ctx) => ({
|
||||||
name: 'fabric-register',
|
name: 'fabric-register',
|
||||||
description: "Register this agent's Fabric API key (minted via Center CLI `user apikey`).",
|
description: "Register this agent's Fabric API key (minted via Center CLI `user apikey`).",
|
||||||
inputSchema: {
|
parameters: {
|
||||||
type: 'object',
|
type: 'object',
|
||||||
additionalProperties: false,
|
additionalProperties: false,
|
||||||
required: ['fabricApiKey'],
|
required: ['fabricApiKey'],
|
||||||
@@ -29,7 +29,7 @@ export function registerFabricTools(api, client, identity) {
|
|||||||
fabricApiKey: { type: 'string', description: 'Fabric Center API key (fak_…)' },
|
fabricApiKey: { type: 'string', description: 'Fabric Center API key (fak_…)' },
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
handler: async (params) => {
|
execute: async (params) => {
|
||||||
const agentId = ctx.agentId;
|
const agentId = ctx.agentId;
|
||||||
if (!agentId)
|
if (!agentId)
|
||||||
return { ok: false, error: 'no agent context' };
|
return { ok: false, error: 'no agent context' };
|
||||||
@@ -46,7 +46,7 @@ export function registerFabricTools(api, client, identity) {
|
|||||||
const makeCreate = (kind) => api.registerTool((ctx) => ({
|
const makeCreate = (kind) => api.registerTool((ctx) => ({
|
||||||
name: `create-${kind}-channel`,
|
name: `create-${kind}-channel`,
|
||||||
description: `Create a Fabric ${kind} channel (x_type=${X_BY_KIND[kind]}).`,
|
description: `Create a Fabric ${kind} channel (x_type=${X_BY_KIND[kind]}).`,
|
||||||
inputSchema: {
|
parameters: {
|
||||||
type: 'object',
|
type: 'object',
|
||||||
additionalProperties: false,
|
additionalProperties: false,
|
||||||
required: ['guildNodeId', 'name'],
|
required: ['guildNodeId', 'name'],
|
||||||
@@ -59,7 +59,7 @@ export function registerFabricTools(api, client, identity) {
|
|||||||
listeners: { type: 'array', items: { type: 'string' } },
|
listeners: { type: 'array', items: { type: 'string' } },
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
handler: async (p) => {
|
execute: async (p) => {
|
||||||
const agentId = ctx.agentId;
|
const agentId = ctx.agentId;
|
||||||
if (!agentId)
|
if (!agentId)
|
||||||
return { ok: false, error: 'no agent context' };
|
return { ok: false, error: 'no agent context' };
|
||||||
@@ -83,7 +83,7 @@ export function registerFabricTools(api, client, identity) {
|
|||||||
api.registerTool((ctx) => ({
|
api.registerTool((ctx) => ({
|
||||||
name: 'discussion-complete',
|
name: 'discussion-complete',
|
||||||
description: 'Conclude a discussion: post a summary then close the channel.',
|
description: 'Conclude a discussion: post a summary then close the channel.',
|
||||||
inputSchema: {
|
parameters: {
|
||||||
type: 'object',
|
type: 'object',
|
||||||
additionalProperties: false,
|
additionalProperties: false,
|
||||||
required: ['guildNodeId', 'channelId', 'summary'],
|
required: ['guildNodeId', 'channelId', 'summary'],
|
||||||
@@ -94,7 +94,7 @@ export function registerFabricTools(api, client, identity) {
|
|||||||
callbackChannelId: { type: 'string', description: 'optional channel to also post the summary to' },
|
callbackChannelId: { type: 'string', description: 'optional channel to also post the summary to' },
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
handler: async (p) => {
|
execute: async (p) => {
|
||||||
const agentId = ctx.agentId;
|
const agentId = ctx.agentId;
|
||||||
if (!agentId)
|
if (!agentId)
|
||||||
return { ok: false, error: 'no agent context' };
|
return { ok: false, error: 'no agent context' };
|
||||||
|
|||||||
9
index.ts
9
index.ts
@@ -72,7 +72,14 @@ export default defineChannelPluginEntry({
|
|||||||
api.logger.warn('fabric: runtime not set; inbound disabled');
|
api.logger.warn('fabric: runtime not set; inbound disabled');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
inbound = new FabricInbound(runtimeRef as never, client, identity, api.logger, accounts);
|
inbound = new FabricInbound(
|
||||||
|
runtimeRef,
|
||||||
|
api.config,
|
||||||
|
client,
|
||||||
|
identity,
|
||||||
|
api.logger,
|
||||||
|
accounts,
|
||||||
|
);
|
||||||
void inbound.start();
|
void inbound.start();
|
||||||
api.logger.info(`fabric: inbound started for ${accounts.length} account(s)`);
|
api.logger.info(`fabric: inbound started for ${accounts.length} account(s)`);
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -40,8 +40,26 @@
|
|||||||
"description": "Fabric Center API base, e.g. http://localhost:7001/api"
|
"description": "Fabric Center API base, e.g. http://localhost:7001/api"
|
||||||
},
|
},
|
||||||
"dmSecurity": { "type": "string" },
|
"dmSecurity": { "type": "string" },
|
||||||
|
"dmPolicy": { "type": "string" },
|
||||||
|
"enabled": { "type": "boolean" },
|
||||||
|
"allowFrom": { "type": "array", "items": { "type": "string" } },
|
||||||
|
"defaultAccount": { "type": "string" },
|
||||||
|
"accounts": {
|
||||||
|
"type": "object",
|
||||||
|
"description": "agent = account; key is the openclaw agentId",
|
||||||
|
"additionalProperties": {
|
||||||
|
"type": "object",
|
||||||
|
"additionalProperties": false,
|
||||||
|
"properties": {
|
||||||
|
"fabricApiKey": { "type": "string" },
|
||||||
|
"centerApiBase": { "type": "string" },
|
||||||
|
"enabled": { "type": "boolean" },
|
||||||
|
"dmPolicy": { "type": "string" },
|
||||||
"allowFrom": { "type": "array", "items": { "type": "string" } }
|
"allowFrom": { "type": "array", "items": { "type": "string" } }
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
},
|
},
|
||||||
"uiHints": {
|
"uiHints": {
|
||||||
"centerApiBase": { "label": "Center API base" }
|
"centerApiBase": { "label": "Center API base" }
|
||||||
|
|||||||
@@ -11,6 +11,8 @@
|
|||||||
import {
|
import {
|
||||||
createChatChannelPlugin,
|
createChatChannelPlugin,
|
||||||
createChannelPluginBase,
|
createChannelPluginBase,
|
||||||
|
buildChannelOutboundSessionRoute,
|
||||||
|
type ChannelOutboundSessionRouteParams,
|
||||||
} from 'openclaw/plugin-sdk/core';
|
} from 'openclaw/plugin-sdk/core';
|
||||||
import { FabricClient } from './fabric-client.js';
|
import { FabricClient } from './fabric-client.js';
|
||||||
import {
|
import {
|
||||||
@@ -22,6 +24,39 @@ import {
|
|||||||
|
|
||||||
type AnyCfg = { channels?: { fabric?: unknown }; [k: string]: unknown };
|
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`
|
// Posts an agent's reply to Fabric. `to` is the Fabric channelId; `accountId`
|
||||||
// is the agentId (= Fabric identity). One auth concept: account apiKey ->
|
// is the agentId (= Fabric identity). One auth concept: account apiKey ->
|
||||||
// agent/login -> guild token -> POST message.
|
// agent/login -> guild token -> POST message.
|
||||||
@@ -31,6 +66,7 @@ async function sendToFabric(
|
|||||||
to: string,
|
to: string,
|
||||||
text: string,
|
text: string,
|
||||||
): Promise<{ messageId: string }> {
|
): Promise<{ messageId: string }> {
|
||||||
|
const channelId = stripFabricTargetPrefix(to) ?? to;
|
||||||
const acc = resolveFabricAccount(cfg as never, accountId);
|
const acc = resolveFabricAccount(cfg as never, accountId);
|
||||||
if (!acc.fabricApiKey) throw new Error(`fabric account ${acc.accountId} has no fabricApiKey`);
|
if (!acc.fabricApiKey) throw new Error(`fabric account ${acc.accountId} has no fabricApiKey`);
|
||||||
const client = new FabricClient(acc.centerApiBase);
|
const client = new FabricClient(acc.centerApiBase);
|
||||||
@@ -43,23 +79,24 @@ async function sendToFabric(
|
|||||||
headers: { authorization: `Bearer ${gt}` },
|
headers: { authorization: `Bearer ${gt}` },
|
||||||
});
|
});
|
||||||
const channels = res.ok ? ((await res.json()) as Array<{ id: string }>) : [];
|
const channels = res.ok ? ((await res.json()) as Array<{ id: string }>) : [];
|
||||||
if (channels.some((c) => c.id === to)) {
|
if (channels.some((c) => c.id === channelId)) {
|
||||||
await client.postMessage(g.endpoint, gt, to, text, session.user.id);
|
await client.postMessage(g.endpoint, gt, channelId, text, session.user.id);
|
||||||
return { messageId: `${to}:${Date.now()}` };
|
return { messageId: `${channelId}:${Date.now()}` };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// fallback: first guild
|
// fallback: first guild
|
||||||
const g = session.guilds[0];
|
const g = session.guilds[0];
|
||||||
const gt = session.guildAccessTokens.find((t) => t.guildNodeId === g?.nodeId)?.token;
|
const gt = session.guildAccessTokens.find((t) => t.guildNodeId === g?.nodeId)?.token;
|
||||||
if (g && gt) {
|
if (g && gt) {
|
||||||
await client.postMessage(g.endpoint, gt, to, text, session.user.id);
|
await client.postMessage(g.endpoint, gt, channelId, text, session.user.id);
|
||||||
return { messageId: `${to}:${Date.now()}` };
|
return { messageId: `${channelId}:${Date.now()}` };
|
||||||
}
|
}
|
||||||
throw new Error('fabric: no guild available to deliver');
|
throw new Error('fabric: no guild available to deliver');
|
||||||
}
|
}
|
||||||
|
|
||||||
export const fabricChannelPlugin = createChatChannelPlugin<ResolvedFabricAccount>({
|
export const fabricChannelPlugin = createChatChannelPlugin<ResolvedFabricAccount>({
|
||||||
base: createChannelPluginBase<ResolvedFabricAccount>({
|
base: {
|
||||||
|
...createChannelPluginBase<ResolvedFabricAccount>({
|
||||||
id: 'fabric',
|
id: 'fabric',
|
||||||
meta: { id: 'fabric', label: 'Fabric', blurb: 'Connect OpenClaw agents to a Fabric guild.' },
|
meta: { id: 'fabric', label: 'Fabric', blurb: 'Connect OpenClaw agents to a Fabric guild.' },
|
||||||
capabilities: {
|
capabilities: {
|
||||||
@@ -83,7 +120,15 @@ export const fabricChannelPlugin = createChatChannelPlugin<ResolvedFabricAccount
|
|||||||
setup: {
|
setup: {
|
||||||
applyAccountConfig: ({ cfg }: { cfg: unknown }) => cfg as never,
|
applyAccountConfig: ({ cfg }: { cfg: unknown }) => cfg as never,
|
||||||
} 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: {
|
security: {
|
||||||
dm: {
|
dm: {
|
||||||
@@ -100,9 +145,23 @@ export const fabricChannelPlugin = createChatChannelPlugin<ResolvedFabricAccount
|
|||||||
base: {},
|
base: {},
|
||||||
attachedResults: {
|
attachedResults: {
|
||||||
channel: 'fabric',
|
channel: 'fabric',
|
||||||
sendText: async (ctx: { accountId?: string | null; to: string; text: string; cfg?: unknown }) => {
|
sendText: async (ctx: {
|
||||||
const cfg = (ctx.cfg ?? {}) as AnyCfg;
|
accountId?: string | null;
|
||||||
return sendToFabric(cfg, ctx.accountId ?? null, ctx.to, ctx.text);
|
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,
|
} as never,
|
||||||
|
|||||||
158
src/inbound.ts
158
src/inbound.ts
@@ -1,20 +1,24 @@
|
|||||||
import { io, type Socket } from 'socket.io-client';
|
import { io, type Socket } from 'socket.io-client';
|
||||||
|
import { dispatchInboundReplyWithBase } from 'openclaw/plugin-sdk/inbound-reply-dispatch';
|
||||||
import type { FabricClient, FabricSession } from './fabric-client.js';
|
import type { FabricClient, FabricSession } from './fabric-client.js';
|
||||||
import type { IdentityRegistry } from './identity.js';
|
import type { IdentityRegistry } from './identity.js';
|
||||||
|
|
||||||
// OpenClaw plugin runtime — only the channel-turn kernel surface we use.
|
// COMPAT NOTE (openclaw v2026.5.7): the inbound path mirrors how bundled
|
||||||
// Typed loosely on purpose: the concrete shapes come from
|
// channels (nextcloud-talk) drive the kernel:
|
||||||
// openclaw/plugin-sdk/core at the host's SDK version.
|
// core = PluginRuntime (from setRuntime)
|
||||||
type PluginRuntime = {
|
// route = core.channel.routing.resolveAgentRoute(...)
|
||||||
|
// ctx = core.channel.reply.finalizeInboundContext(...) // has SessionKey
|
||||||
|
// dispatch= dispatchInboundReplyWithBase({ cfg, route, ctxPayload, core, deliver })
|
||||||
|
// `core.channel.*` is accessed loosely so unrelated SDK drift won't break us.
|
||||||
|
type Core = {
|
||||||
channel: {
|
channel: {
|
||||||
turn: {
|
routing: { resolveAgentRoute: (p: unknown) => { agentId: string; sessionKey: string; accountId?: string } };
|
||||||
run(args: unknown): Promise<unknown>;
|
session: { resolveStorePath: (store: unknown, o: { agentId: string }) => string };
|
||||||
|
reply: { finalizeInboundContext: (p: Record<string, unknown>) => unknown };
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
log?: { debug?: (m: string, x?: unknown) => void };
|
|
||||||
};
|
|
||||||
|
|
||||||
type Logger = { info: (m: string) => void; warn: (m: string) => void };
|
type Logger = { info: (m: string) => void; warn: (m: string) => void; error?: (m: string) => void };
|
||||||
|
|
||||||
type FabricMessage = {
|
type FabricMessage = {
|
||||||
messageId: string;
|
messageId: string;
|
||||||
@@ -22,20 +26,17 @@ type FabricMessage = {
|
|||||||
content: string;
|
content: string;
|
||||||
authorUserId?: string;
|
authorUserId?: string;
|
||||||
createdAt?: string;
|
createdAt?: string;
|
||||||
// per-recipient metadata Fabric attaches at push time (this agent's view)
|
channelId?: string;
|
||||||
wakeup?: boolean;
|
wakeup?: boolean;
|
||||||
};
|
};
|
||||||
|
|
||||||
// One live Fabric connection per agent identity (Phase 1 = B1). Lives in the
|
|
||||||
// channel-plugin runtime (no separate sidecar). Firehose (B2) would replace
|
|
||||||
// this class behind the same dispatch() call.
|
|
||||||
export class FabricInbound {
|
export class FabricInbound {
|
||||||
private sockets: Socket[] = [];
|
private sockets: Socket[] = [];
|
||||||
private timers: NodeJS.Timeout[] = [];
|
|
||||||
private seen = new Set<string>();
|
private seen = new Set<string>();
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
private readonly runtime: PluginRuntime,
|
private readonly core: unknown, // PluginRuntime
|
||||||
|
private readonly cfg: unknown, // OpenClawConfig
|
||||||
private readonly client: FabricClient,
|
private readonly client: FabricClient,
|
||||||
private readonly identity: IdentityRegistry,
|
private readonly identity: IdentityRegistry,
|
||||||
private readonly log: Logger,
|
private readonly log: Logger,
|
||||||
@@ -65,10 +66,8 @@ export class FabricInbound {
|
|||||||
}
|
}
|
||||||
|
|
||||||
stop(): void {
|
stop(): void {
|
||||||
for (const t of this.timers) clearInterval(t);
|
|
||||||
for (const s of this.sockets) s.disconnect();
|
for (const s of this.sockets) s.disconnect();
|
||||||
this.sockets = [];
|
this.sockets = [];
|
||||||
this.timers = [];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private async connectAgent(agentId: string, session: FabricSession): Promise<void> {
|
private async connectAgent(agentId: string, session: FabricSession): Promise<void> {
|
||||||
@@ -76,101 +75,112 @@ export class FabricInbound {
|
|||||||
for (const g of session.guilds) {
|
for (const g of session.guilds) {
|
||||||
const tok = session.guildAccessTokens.find((t) => t.guildNodeId === g.nodeId)?.token;
|
const tok = session.guildAccessTokens.find((t) => t.guildNodeId === g.nodeId)?.token;
|
||||||
if (!tok) continue;
|
if (!tok) continue;
|
||||||
|
|
||||||
const socket = io(`${g.endpoint}/realtime`, {
|
const socket = io(`${g.endpoint}/realtime`, {
|
||||||
transports: ['websocket'],
|
transports: ['websocket'],
|
||||||
auth: { token: tok },
|
auth: { token: tok },
|
||||||
autoConnect: false,
|
autoConnect: false,
|
||||||
});
|
});
|
||||||
|
|
||||||
const joinAll = async () => {
|
const joinAll = async () => {
|
||||||
try {
|
try {
|
||||||
const res = await fetch(`${g.endpoint}/api/channels?guildId=${encodeURIComponent(g.nodeId)}`, {
|
const res = await fetch(
|
||||||
headers: { authorization: `Bearer ${tok}` },
|
`${g.endpoint}/api/channels?guildId=${encodeURIComponent(g.nodeId)}`,
|
||||||
});
|
{ headers: { authorization: `Bearer ${tok}` } },
|
||||||
|
);
|
||||||
const channels = res.ok ? ((await res.json()) as Array<{ id: string }>) : [];
|
const channels = res.ok ? ((await res.json()) as Array<{ id: string }>) : [];
|
||||||
for (const c of channels) socket.emit('join_channel', { channelId: c.id });
|
for (const c of channels) socket.emit('join_channel', { channelId: c.id });
|
||||||
|
this.log.info(`fabric: agent ${agentId} joined ${channels.length} channel(s) on ${g.nodeId}`);
|
||||||
} catch {
|
} catch {
|
||||||
/* best effort */
|
/* best effort */
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
socket.on('connect', () => void joinAll());
|
socket.on('connect', () => void joinAll());
|
||||||
socket.on('message.created', (m: FabricMessage & { channelId?: string }) => {
|
socket.on('message.created', (m: FabricMessage) => {
|
||||||
const channelId = m.channelId ?? '';
|
const channelId = m.channelId ?? '';
|
||||||
if (!channelId) return;
|
if (!channelId) return;
|
||||||
// self-echo guard + dedupe
|
|
||||||
if (m.authorUserId && m.authorUserId === selfUserId) return;
|
if (m.authorUserId && m.authorUserId === selfUserId) return;
|
||||||
const key = `${agentId}:${m.messageId}`;
|
const key = `${agentId}:${m.messageId}`;
|
||||||
if (this.seen.has(key)) return;
|
if (this.seen.has(key)) return;
|
||||||
this.seen.add(key);
|
this.seen.add(key);
|
||||||
if (this.seen.size > 5000) this.seen.clear();
|
if (this.seen.size > 5000) this.seen.clear();
|
||||||
void this.dispatch(agentId, g, channelId, m);
|
void this.dispatch(agentId, g, channelId, m, session);
|
||||||
});
|
});
|
||||||
|
|
||||||
socket.connect();
|
socket.connect();
|
||||||
this.sockets.push(socket);
|
this.sockets.push(socket);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Hand the inbound Fabric message to OpenClaw's channel-turn kernel.
|
|
||||||
// wakeup === true -> dispatch (agent runs, may reply)
|
|
||||||
// wakeup !== true -> drop but keep as group history/context
|
|
||||||
private async dispatch(
|
private async dispatch(
|
||||||
agentId: string,
|
agentId: string,
|
||||||
guild: { nodeId: string; endpoint: string },
|
guild: { nodeId: string; endpoint: string },
|
||||||
channelId: string,
|
channelId: string,
|
||||||
m: FabricMessage,
|
m: FabricMessage,
|
||||||
|
session: FabricSession,
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
const admit = m.wakeup === true;
|
// wakeup === false -> drop (Fabric already decided this agent is silent)
|
||||||
|
if (m.wakeup !== true) {
|
||||||
|
this.log.info(`fabric: drop (no wakeup) agent=${agentId} channel=${channelId}`);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
this.log.info(`fabric: dispatch agent=${agentId} channel=${channelId}`);
|
||||||
|
|
||||||
|
const core = this.core as Core & Record<string, unknown>;
|
||||||
|
const cfg = this.cfg as { session?: { store?: unknown } };
|
||||||
try {
|
try {
|
||||||
await this.runtime.channel.turn.run({
|
const route = core.channel.routing.resolveAgentRoute({
|
||||||
|
cfg: this.cfg,
|
||||||
channel: 'fabric',
|
channel: 'fabric',
|
||||||
accountId: agentId,
|
accountId: agentId,
|
||||||
raw: m,
|
peer: { kind: 'group', id: channelId },
|
||||||
adapter: {
|
|
||||||
ingest: (raw: FabricMessage) => ({
|
|
||||||
id: raw.messageId,
|
|
||||||
timestamp: raw.createdAt ? Date.parse(raw.createdAt) : Date.now(),
|
|
||||||
rawText: raw.content,
|
|
||||||
textForAgent: raw.content,
|
|
||||||
}),
|
|
||||||
classify: () => ({ kind: 'message', canStartAgentTurn: admit }),
|
|
||||||
preflight: () =>
|
|
||||||
admit ? {} : { admission: { kind: 'drop', reason: 'no-wakeup', recordHistory: true } },
|
|
||||||
resolveTurn: (input: { id: string }) => ({
|
|
||||||
route: {
|
|
||||||
agentId,
|
|
||||||
routeSessionKey: `agent:${agentId}:fabric:channel:${channelId}`,
|
|
||||||
createIfMissing: true,
|
|
||||||
},
|
|
||||||
conversation: { kind: 'channel', id: channelId, label: `fabric:${guild.nodeId}` },
|
|
||||||
reply: { to: channelId, nativeChannelId: channelId },
|
|
||||||
message: {
|
|
||||||
body: m.content,
|
|
||||||
rawBody: m.content,
|
|
||||||
bodyForAgent: m.content,
|
|
||||||
envelopeFrom: m.authorUserId ?? 'fabric',
|
|
||||||
},
|
|
||||||
delivery: {
|
|
||||||
deliver: async (payload: { text?: string }) => {
|
|
||||||
const text = typeof payload?.text === 'string' ? payload.text : '';
|
|
||||||
if (!text.trim()) return { visibleReplySent: false };
|
|
||||||
const entry = this.identity.findByAgentId(agentId);
|
|
||||||
const session = entry ? await this.client.agentLogin(entry.fabricApiKey) : null;
|
|
||||||
const gt = session?.guildAccessTokens.find((t) => t.guildNodeId === guild.nodeId)?.token;
|
|
||||||
if (!session || !gt) return { visibleReplySent: false };
|
|
||||||
await this.client.postMessage(guild.endpoint, gt, channelId, text, session.user.id);
|
|
||||||
return { visibleReplySent: true };
|
|
||||||
},
|
|
||||||
},
|
|
||||||
meta: { admission: admit ? { kind: 'dispatch' } : { kind: 'drop', recordHistory: true } },
|
|
||||||
}),
|
|
||||||
},
|
|
||||||
log: (e: { stage?: string }) => this.runtime.log?.debug?.(`fabric.turn.${e?.stage}`),
|
|
||||||
});
|
});
|
||||||
|
const storePath = core.channel.session.resolveStorePath(cfg.session?.store, {
|
||||||
|
agentId: route.agentId,
|
||||||
|
});
|
||||||
|
const ctxPayload = core.channel.reply.finalizeInboundContext({
|
||||||
|
Body: m.content,
|
||||||
|
BodyForAgent: m.content,
|
||||||
|
RawBody: m.content,
|
||||||
|
CommandBody: m.content,
|
||||||
|
From: `fabric:channel:${channelId}`,
|
||||||
|
To: `fabric:${channelId}`,
|
||||||
|
SessionKey: route.sessionKey,
|
||||||
|
AccountId: route.accountId ?? agentId,
|
||||||
|
ChatType: 'group',
|
||||||
|
ConversationLabel: `fabric:${guild.nodeId}`,
|
||||||
|
SenderId: m.authorUserId ?? 'fabric',
|
||||||
|
Provider: 'fabric',
|
||||||
|
Surface: 'fabric',
|
||||||
|
MessageSid: m.messageId,
|
||||||
|
Timestamp: m.createdAt ? Date.parse(m.createdAt) : Date.now(),
|
||||||
|
OriginatingChannel: 'fabric',
|
||||||
|
OriginatingTo: `fabric:${channelId}`,
|
||||||
|
});
|
||||||
|
|
||||||
|
const gt = session.guildAccessTokens.find((t) => t.guildNodeId === guild.nodeId)?.token;
|
||||||
|
|
||||||
|
await dispatchInboundReplyWithBase({
|
||||||
|
cfg: this.cfg as never,
|
||||||
|
channel: 'fabric',
|
||||||
|
accountId: agentId,
|
||||||
|
route,
|
||||||
|
storePath,
|
||||||
|
ctxPayload: ctxPayload as never,
|
||||||
|
core: this.core as never,
|
||||||
|
deliver: async (payload: { text?: string }) => {
|
||||||
|
const text = (payload?.text ?? '').trim();
|
||||||
|
this.log.info(`fabric: deliver agent=${agentId} channel=${channelId} len=${text.length}`);
|
||||||
|
if (!text || !gt) return;
|
||||||
|
await this.client.postMessage(guild.endpoint, gt, channelId, text, session.user.id);
|
||||||
|
this.log.info(`fabric: posted reply agent=${agentId} channel=${channelId}`);
|
||||||
|
},
|
||||||
|
onRecordError: (err: unknown) =>
|
||||||
|
this.log.warn(`fabric: session record failed agent=${agentId}: ${String(err)}`),
|
||||||
|
onDispatchError: (err: unknown, info: { kind: string }) =>
|
||||||
|
this.log.warn(`fabric: ${info.kind} dispatch failed agent=${agentId}: ${String(err)}`),
|
||||||
|
replyOptions: {},
|
||||||
|
});
|
||||||
|
this.log.info(`fabric: dispatch returned agent=${agentId} channel=${channelId}`);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
this.log.warn(`fabric: turn.run failed agent=${agentId} channel=${channelId}: ${String(err)}`);
|
this.log.warn(`fabric: dispatch failed agent=${agentId} channel=${channelId}: ${String(err)}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
12
src/tools.ts
12
src/tools.ts
@@ -37,7 +37,7 @@ export function registerFabricTools(
|
|||||||
api.registerTool((ctx: Ctx) => ({
|
api.registerTool((ctx: Ctx) => ({
|
||||||
name: 'fabric-register',
|
name: 'fabric-register',
|
||||||
description: "Register this agent's Fabric API key (minted via Center CLI `user apikey`).",
|
description: "Register this agent's Fabric API key (minted via Center CLI `user apikey`).",
|
||||||
inputSchema: {
|
parameters: {
|
||||||
type: 'object',
|
type: 'object',
|
||||||
additionalProperties: false,
|
additionalProperties: false,
|
||||||
required: ['fabricApiKey'],
|
required: ['fabricApiKey'],
|
||||||
@@ -45,7 +45,7 @@ export function registerFabricTools(
|
|||||||
fabricApiKey: { type: 'string', description: 'Fabric Center API key (fak_…)' },
|
fabricApiKey: { type: 'string', description: 'Fabric Center API key (fak_…)' },
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
handler: async (params: { fabricApiKey: string }) => {
|
execute: async (params: { fabricApiKey: string }) => {
|
||||||
const agentId = ctx.agentId;
|
const agentId = ctx.agentId;
|
||||||
if (!agentId) return { ok: false, error: 'no agent context' };
|
if (!agentId) return { ok: false, error: 'no agent context' };
|
||||||
const session = await client.agentLogin(params.fabricApiKey);
|
const session = await client.agentLogin(params.fabricApiKey);
|
||||||
@@ -63,7 +63,7 @@ export function registerFabricTools(
|
|||||||
api.registerTool((ctx: Ctx) => ({
|
api.registerTool((ctx: Ctx) => ({
|
||||||
name: `create-${kind}-channel`,
|
name: `create-${kind}-channel`,
|
||||||
description: `Create a Fabric ${kind} channel (x_type=${X_BY_KIND[kind]}).`,
|
description: `Create a Fabric ${kind} channel (x_type=${X_BY_KIND[kind]}).`,
|
||||||
inputSchema: {
|
parameters: {
|
||||||
type: 'object',
|
type: 'object',
|
||||||
additionalProperties: false,
|
additionalProperties: false,
|
||||||
required: ['guildNodeId', 'name'],
|
required: ['guildNodeId', 'name'],
|
||||||
@@ -76,7 +76,7 @@ export function registerFabricTools(
|
|||||||
listeners: { type: 'array', items: { type: 'string' } },
|
listeners: { type: 'array', items: { type: 'string' } },
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
handler: async (p: {
|
execute: async (p: {
|
||||||
guildNodeId: string;
|
guildNodeId: string;
|
||||||
name: string;
|
name: string;
|
||||||
isPublic?: boolean;
|
isPublic?: boolean;
|
||||||
@@ -106,7 +106,7 @@ export function registerFabricTools(
|
|||||||
api.registerTool((ctx: Ctx) => ({
|
api.registerTool((ctx: Ctx) => ({
|
||||||
name: 'discussion-complete',
|
name: 'discussion-complete',
|
||||||
description: 'Conclude a discussion: post a summary then close the channel.',
|
description: 'Conclude a discussion: post a summary then close the channel.',
|
||||||
inputSchema: {
|
parameters: {
|
||||||
type: 'object',
|
type: 'object',
|
||||||
additionalProperties: false,
|
additionalProperties: false,
|
||||||
required: ['guildNodeId', 'channelId', 'summary'],
|
required: ['guildNodeId', 'channelId', 'summary'],
|
||||||
@@ -117,7 +117,7 @@ export function registerFabricTools(
|
|||||||
callbackChannelId: { type: 'string', description: 'optional channel to also post the summary to' },
|
callbackChannelId: { type: 'string', description: 'optional channel to also post the summary to' },
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
handler: async (p: {
|
execute: async (p: {
|
||||||
guildNodeId: string;
|
guildNodeId: string;
|
||||||
channelId: string;
|
channelId: string;
|
||||||
summary: string;
|
summary: string;
|
||||||
|
|||||||
Reference in New Issue
Block a user