mirror of
https://github.com/openclaw/openclaw.git
synced 2026-03-12 07:20:45 +00:00
277 lines
8.4 KiB
TypeScript
277 lines
8.4 KiB
TypeScript
import {
|
|
createActionGate,
|
|
readNumberParam,
|
|
readStringParam,
|
|
type ChannelMessageActionAdapter,
|
|
type ChannelMessageActionContext,
|
|
type ChannelMessageActionName,
|
|
type ChannelToolSend,
|
|
} from "openclaw/plugin-sdk/matrix";
|
|
import { resolveDefaultMatrixAccountId, resolveMatrixAccount } from "./matrix/accounts.js";
|
|
import { handleMatrixAction } from "./tool-actions.js";
|
|
import type { CoreConfig } from "./types.js";
|
|
|
|
const MATRIX_PLUGIN_HANDLED_ACTIONS = new Set<ChannelMessageActionName>([
|
|
"send",
|
|
"poll-vote",
|
|
"react",
|
|
"reactions",
|
|
"read",
|
|
"edit",
|
|
"delete",
|
|
"pin",
|
|
"unpin",
|
|
"list-pins",
|
|
"set-profile",
|
|
"member-info",
|
|
"channel-info",
|
|
"permissions",
|
|
]);
|
|
|
|
function createMatrixExposedActions(params: {
|
|
gate: ReturnType<typeof createActionGate>;
|
|
encryptionEnabled: boolean;
|
|
}) {
|
|
const actions = new Set<ChannelMessageActionName>(["poll", "poll-vote"]);
|
|
if (params.gate("messages")) {
|
|
actions.add("send");
|
|
actions.add("read");
|
|
actions.add("edit");
|
|
actions.add("delete");
|
|
}
|
|
if (params.gate("reactions")) {
|
|
actions.add("react");
|
|
actions.add("reactions");
|
|
}
|
|
if (params.gate("pins")) {
|
|
actions.add("pin");
|
|
actions.add("unpin");
|
|
actions.add("list-pins");
|
|
}
|
|
if (params.gate("profile")) {
|
|
actions.add("set-profile");
|
|
}
|
|
if (params.gate("memberInfo")) {
|
|
actions.add("member-info");
|
|
}
|
|
if (params.gate("channelInfo")) {
|
|
actions.add("channel-info");
|
|
}
|
|
if (params.encryptionEnabled && params.gate("verification")) {
|
|
actions.add("permissions");
|
|
}
|
|
return actions;
|
|
}
|
|
|
|
export const matrixMessageActions: ChannelMessageActionAdapter = {
|
|
listActions: ({ cfg }) => {
|
|
const resolvedCfg = cfg as CoreConfig;
|
|
const account = resolveMatrixAccount({
|
|
cfg: resolvedCfg,
|
|
accountId: resolveDefaultMatrixAccountId(resolvedCfg),
|
|
});
|
|
if (!account.enabled || !account.configured) {
|
|
return [];
|
|
}
|
|
const gate = createActionGate(account.config.actions);
|
|
const actions = createMatrixExposedActions({
|
|
gate,
|
|
encryptionEnabled: account.config.encryption === true,
|
|
});
|
|
return Array.from(actions);
|
|
},
|
|
supportsAction: ({ action }) => MATRIX_PLUGIN_HANDLED_ACTIONS.has(action),
|
|
extractToolSend: ({ args }): ChannelToolSend | null => {
|
|
const action = typeof args.action === "string" ? args.action.trim() : "";
|
|
if (action !== "sendMessage") {
|
|
return null;
|
|
}
|
|
const to = typeof args.to === "string" ? args.to : undefined;
|
|
if (!to) {
|
|
return null;
|
|
}
|
|
return { to };
|
|
},
|
|
handleAction: async (ctx: ChannelMessageActionContext) => {
|
|
const { action, params, cfg, accountId, mediaLocalRoots } = ctx;
|
|
const dispatch = async (actionParams: Record<string, unknown>) =>
|
|
await handleMatrixAction(
|
|
{
|
|
...actionParams,
|
|
...(accountId ? { accountId } : {}),
|
|
},
|
|
cfg as CoreConfig,
|
|
{ mediaLocalRoots },
|
|
);
|
|
const resolveRoomId = () =>
|
|
readStringParam(params, "roomId") ??
|
|
readStringParam(params, "channelId") ??
|
|
readStringParam(params, "to", { required: true });
|
|
|
|
if (action === "send") {
|
|
const to = readStringParam(params, "to", { required: true });
|
|
const content = readStringParam(params, "message", {
|
|
required: true,
|
|
allowEmpty: true,
|
|
});
|
|
const mediaUrl = readStringParam(params, "media", { trim: false });
|
|
const replyTo = readStringParam(params, "replyTo");
|
|
const threadId = readStringParam(params, "threadId");
|
|
return await dispatch({
|
|
action: "sendMessage",
|
|
to,
|
|
content,
|
|
mediaUrl: mediaUrl ?? undefined,
|
|
replyToId: replyTo ?? undefined,
|
|
threadId: threadId ?? undefined,
|
|
});
|
|
}
|
|
|
|
if (action === "poll-vote") {
|
|
return await dispatch({
|
|
...params,
|
|
action: "pollVote",
|
|
});
|
|
}
|
|
|
|
if (action === "react") {
|
|
const messageId = readStringParam(params, "messageId", { required: true });
|
|
const emoji = readStringParam(params, "emoji", { allowEmpty: true });
|
|
const remove = typeof params.remove === "boolean" ? params.remove : undefined;
|
|
return await dispatch({
|
|
action: "react",
|
|
roomId: resolveRoomId(),
|
|
messageId,
|
|
emoji,
|
|
remove,
|
|
});
|
|
}
|
|
|
|
if (action === "reactions") {
|
|
const messageId = readStringParam(params, "messageId", { required: true });
|
|
const limit = readNumberParam(params, "limit", { integer: true });
|
|
return await dispatch({
|
|
action: "reactions",
|
|
roomId: resolveRoomId(),
|
|
messageId,
|
|
limit,
|
|
});
|
|
}
|
|
|
|
if (action === "read") {
|
|
const limit = readNumberParam(params, "limit", { integer: true });
|
|
return await dispatch({
|
|
action: "readMessages",
|
|
roomId: resolveRoomId(),
|
|
limit,
|
|
before: readStringParam(params, "before"),
|
|
after: readStringParam(params, "after"),
|
|
});
|
|
}
|
|
|
|
if (action === "edit") {
|
|
const messageId = readStringParam(params, "messageId", { required: true });
|
|
const content = readStringParam(params, "message", { required: true });
|
|
return await dispatch({
|
|
action: "editMessage",
|
|
roomId: resolveRoomId(),
|
|
messageId,
|
|
content,
|
|
});
|
|
}
|
|
|
|
if (action === "delete") {
|
|
const messageId = readStringParam(params, "messageId", { required: true });
|
|
return await dispatch({
|
|
action: "deleteMessage",
|
|
roomId: resolveRoomId(),
|
|
messageId,
|
|
});
|
|
}
|
|
|
|
if (action === "pin" || action === "unpin" || action === "list-pins") {
|
|
const messageId =
|
|
action === "list-pins"
|
|
? undefined
|
|
: readStringParam(params, "messageId", { required: true });
|
|
return await dispatch({
|
|
action: action === "pin" ? "pinMessage" : action === "unpin" ? "unpinMessage" : "listPins",
|
|
roomId: resolveRoomId(),
|
|
messageId,
|
|
});
|
|
}
|
|
|
|
if (action === "set-profile") {
|
|
const avatarPath =
|
|
readStringParam(params, "avatarPath") ??
|
|
readStringParam(params, "path") ??
|
|
readStringParam(params, "filePath");
|
|
return await dispatch({
|
|
action: "setProfile",
|
|
displayName: readStringParam(params, "displayName") ?? readStringParam(params, "name"),
|
|
avatarUrl: readStringParam(params, "avatarUrl"),
|
|
avatarPath,
|
|
});
|
|
}
|
|
|
|
if (action === "member-info") {
|
|
const userId = readStringParam(params, "userId", { required: true });
|
|
return await dispatch({
|
|
action: "memberInfo",
|
|
userId,
|
|
roomId: readStringParam(params, "roomId") ?? readStringParam(params, "channelId"),
|
|
});
|
|
}
|
|
|
|
if (action === "channel-info") {
|
|
return await dispatch({
|
|
action: "channelInfo",
|
|
roomId: resolveRoomId(),
|
|
});
|
|
}
|
|
|
|
if (action === "permissions") {
|
|
const operation = (
|
|
readStringParam(params, "operation") ??
|
|
readStringParam(params, "mode") ??
|
|
"verification-list"
|
|
)
|
|
.trim()
|
|
.toLowerCase();
|
|
const operationToAction: Record<string, string> = {
|
|
"encryption-status": "encryptionStatus",
|
|
"verification-status": "verificationStatus",
|
|
"verification-bootstrap": "verificationBootstrap",
|
|
"verification-recovery-key": "verificationRecoveryKey",
|
|
"verification-backup-status": "verificationBackupStatus",
|
|
"verification-backup-restore": "verificationBackupRestore",
|
|
"verification-list": "verificationList",
|
|
"verification-request": "verificationRequest",
|
|
"verification-accept": "verificationAccept",
|
|
"verification-cancel": "verificationCancel",
|
|
"verification-start": "verificationStart",
|
|
"verification-generate-qr": "verificationGenerateQr",
|
|
"verification-scan-qr": "verificationScanQr",
|
|
"verification-sas": "verificationSas",
|
|
"verification-confirm": "verificationConfirm",
|
|
"verification-mismatch": "verificationMismatch",
|
|
"verification-confirm-qr": "verificationConfirmQr",
|
|
};
|
|
const resolvedAction = operationToAction[operation];
|
|
if (!resolvedAction) {
|
|
throw new Error(
|
|
`Unsupported Matrix permissions operation: ${operation}. Supported values: ${Object.keys(
|
|
operationToAction,
|
|
).join(", ")}`,
|
|
);
|
|
}
|
|
return await dispatch({
|
|
...params,
|
|
action: resolvedAction,
|
|
});
|
|
}
|
|
|
|
throw new Error(`Action ${action} is not supported for provider matrix.`);
|
|
},
|
|
};
|