|
| 1 | +import { createActionGate } from "openclaw/plugin-sdk/channel-actions"; |
| 2 | +import type { ChannelAgentTool } from "openclaw/plugin-sdk/channel-contract"; |
| 3 | +import type { OpenClawConfig } from "openclaw/plugin-sdk/config-contracts"; |
| 4 | +import { getRuntimeConfig } from "openclaw/plugin-sdk/runtime-config-snapshot"; |
| 5 | +import { Type } from "typebox"; |
| 6 | +import { getActiveWebListener, resolveWebAccountId } from "./active-listener.js"; |
| 7 | +import type { ActiveWebListener } from "./inbound/types.js"; |
| 8 | + |
| 9 | +const GROUP_JID_PATTERN = "@g.us"; |
| 10 | + |
| 11 | +function areWhatsAppGroupActionsEnabled(cfg: OpenClawConfig): boolean { |
| 12 | + if (!cfg.channels?.whatsapp) { |
| 13 | + return false; |
| 14 | + } |
| 15 | + // Default false: group creation/participant changes can add arbitrary people |
| 16 | + // to chats, so they are opt-in via channels.whatsapp.actions.groups. |
| 17 | + return createActionGate(cfg.channels.whatsapp.actions)("groups", false); |
| 18 | +} |
| 19 | + |
| 20 | +/** |
| 21 | + * Resolve the active, linked WhatsApp listener for the given account, enforcing |
| 22 | + * the `groups` action gate. Reads live config so a runtime toggle is honored |
| 23 | + * without a gateway restart. Throws a clear, agent-readable error otherwise. |
| 24 | + */ |
| 25 | +function requireGroupCapableListener(accountIdArg: unknown): ActiveWebListener { |
| 26 | + const cfg = getRuntimeConfig(); |
| 27 | + if (!areWhatsAppGroupActionsEnabled(cfg)) { |
| 28 | + throw new Error( |
| 29 | + "WhatsApp group actions are disabled. Set channels.whatsapp.actions.groups = true to enable them.", |
| 30 | + ); |
| 31 | + } |
| 32 | + const accountId = resolveWebAccountId({ |
| 33 | + cfg, |
| 34 | + accountId: typeof accountIdArg === "string" ? accountIdArg : null, |
| 35 | + }); |
| 36 | + const listener = getActiveWebListener(accountId); |
| 37 | + if (!listener) { |
| 38 | + throw new Error(`WhatsApp is not linked (account: ${accountId}). Link it first, then retry.`); |
| 39 | + } |
| 40 | + return listener; |
| 41 | +} |
| 42 | + |
| 43 | +function readParticipants(value: unknown): string[] { |
| 44 | + if (!Array.isArray(value) || value.length === 0) { |
| 45 | + throw new Error("participants must be a non-empty array of phone numbers (E.164)."); |
| 46 | + } |
| 47 | + const participants = value |
| 48 | + .map((entry) => (typeof entry === "string" ? entry.trim() : "")) |
| 49 | + .filter(Boolean); |
| 50 | + if (participants.length === 0) { |
| 51 | + throw new Error("participants must contain at least one phone number (E.164)."); |
| 52 | + } |
| 53 | + return participants; |
| 54 | +} |
| 55 | + |
| 56 | +function readGroupJid(value: unknown): string { |
| 57 | + const jid = typeof value === "string" ? value.trim() : ""; |
| 58 | + if (!jid.endsWith(GROUP_JID_PATTERN)) { |
| 59 | + throw new Error("group_jid must be a WhatsApp group JID, e.g. [email protected]."); |
| 60 | + } |
| 61 | + return jid; |
| 62 | +} |
| 63 | + |
| 64 | +function createWhatsAppGroupCreateTool(): ChannelAgentTool { |
| 65 | + return { |
| 66 | + label: "WhatsApp Create Group", |
| 67 | + name: "whatsapp_group_create", |
| 68 | + description: |
| 69 | + "Create a new WhatsApp group with the given subject and participants. Returns the new group JID — use it for future group operations.", |
| 70 | + parameters: Type.Object({ |
| 71 | + subject: Type.String({ description: "Group name." }), |
| 72 | + participants: Type.Array(Type.String(), { |
| 73 | + description: "Phone numbers in E.164 format, e.g. +15551234567.", |
| 74 | + }), |
| 75 | + accountId: Type.Optional(Type.String()), |
| 76 | + }), |
| 77 | + execute: async (_toolCallId, args) => { |
| 78 | + const { subject, participants, accountId } = args as { |
| 79 | + subject?: string; |
| 80 | + participants?: unknown; |
| 81 | + accountId?: string; |
| 82 | + }; |
| 83 | + const trimmedSubject = typeof subject === "string" ? subject.trim() : ""; |
| 84 | + if (!trimmedSubject) { |
| 85 | + throw new Error("subject is required."); |
| 86 | + } |
| 87 | + const listener = requireGroupCapableListener(accountId); |
| 88 | + const result = await listener.createGroup(trimmedSubject, readParticipants(participants)); |
| 89 | + return { |
| 90 | + content: [ |
| 91 | + { |
| 92 | + type: "text", |
| 93 | + text: `Created WhatsApp group "${result.subject}" (${result.groupJid}).`, |
| 94 | + }, |
| 95 | + ], |
| 96 | + details: result, |
| 97 | + }; |
| 98 | + }, |
| 99 | + }; |
| 100 | +} |
| 101 | + |
| 102 | +function createWhatsAppGroupAddTool(): ChannelAgentTool { |
| 103 | + return { |
| 104 | + label: "WhatsApp Add Group Participants", |
| 105 | + name: "whatsapp_group_add_participants", |
| 106 | + description: "Add participants to an existing WhatsApp group.", |
| 107 | + parameters: Type.Object({ |
| 108 | + group_jid: Type.String({ |
| 109 | + description: "Group JID, e.g. [email protected].", |
| 110 | + }), |
| 111 | + participants: Type.Array(Type.String(), { |
| 112 | + description: "Phone numbers in E.164 format to add.", |
| 113 | + }), |
| 114 | + accountId: Type.Optional(Type.String()), |
| 115 | + }), |
| 116 | + execute: async (_toolCallId, args) => { |
| 117 | + const { group_jid, participants, accountId } = args as { |
| 118 | + group_jid?: unknown; |
| 119 | + participants?: unknown; |
| 120 | + accountId?: string; |
| 121 | + }; |
| 122 | + const groupJid = readGroupJid(group_jid); |
| 123 | + const listener = requireGroupCapableListener(accountId); |
| 124 | + const results = await listener.addGroupParticipants(groupJid, readParticipants(participants)); |
| 125 | + return { |
| 126 | + content: [ |
| 127 | + { |
| 128 | + type: "text", |
| 129 | + text: results.map((r) => `${r.jid ?? "(unknown)"}: ${r.status}`).join("\n"), |
| 130 | + }, |
| 131 | + ], |
| 132 | + details: { groupJid, results }, |
| 133 | + }; |
| 134 | + }, |
| 135 | + }; |
| 136 | +} |
| 137 | + |
| 138 | +function createWhatsAppGroupInviteLinkTool(): ChannelAgentTool { |
| 139 | + return { |
| 140 | + label: "WhatsApp Group Invite Link", |
| 141 | + name: "whatsapp_group_invite_link", |
| 142 | + description: "Get the invite link for a WhatsApp group the bot administers.", |
| 143 | + parameters: Type.Object({ |
| 144 | + group_jid: Type.String({ |
| 145 | + description: "Group JID, e.g. [email protected].", |
| 146 | + }), |
| 147 | + accountId: Type.Optional(Type.String()), |
| 148 | + }), |
| 149 | + execute: async (_toolCallId, args) => { |
| 150 | + const { group_jid, accountId } = args as { |
| 151 | + group_jid?: unknown; |
| 152 | + accountId?: string; |
| 153 | + }; |
| 154 | + const groupJid = readGroupJid(group_jid); |
| 155 | + const listener = requireGroupCapableListener(accountId); |
| 156 | + const result = await listener.getGroupInviteCode(groupJid); |
| 157 | + return { |
| 158 | + content: [{ type: "text", text: result.inviteLink }], |
| 159 | + details: { groupJid, ...result }, |
| 160 | + }; |
| 161 | + }, |
| 162 | + }; |
| 163 | +} |
| 164 | + |
| 165 | +/** |
| 166 | + * WhatsApp group-management agent tools. Returned only when |
| 167 | + * `channels.whatsapp.actions.groups` is enabled, so disabled installs don't |
| 168 | + * advertise tools the agent can't use. Each tool also re-checks the gate at |
| 169 | + * execute time and requires an active linked listener. |
| 170 | + */ |
| 171 | +export function createWhatsAppGroupTools(params: { cfg?: OpenClawConfig }): ChannelAgentTool[] { |
| 172 | + if (!params.cfg || !areWhatsAppGroupActionsEnabled(params.cfg)) { |
| 173 | + return []; |
| 174 | + } |
| 175 | + return [ |
| 176 | + createWhatsAppGroupCreateTool(), |
| 177 | + createWhatsAppGroupAddTool(), |
| 178 | + createWhatsAppGroupInviteLinkTool(), |
| 179 | + ]; |
| 180 | +} |
0 commit comments