Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

refactor(agent-tools): tie each tool's handler to its own input schema

Yudhi Armyndharis3d139a541334
diff --git a/src/core/agent-tools/tool-descriptor.ts b/src/core/agent-tools/tool-descriptor.ts
index 0d8c8d3d8..1c8a31f82 100644
--- a/src/core/agent-tools/tool-descriptor.ts
+++ b/src/core/agent-tools/tool-descriptor.ts
@@ -41,3 +41,16 @@ export type AnyToolDescriptor = Omit<ToolDescriptor, 'inputSchema' | 'handler'>
inputSchema: z.ZodType;
handler: (input: never, apiKey: ApiKey) => Promise<unknown>;
};
+
+/**
+ * Declare one tool, tying its handler to its own schema.
+ *
+ * Annotating a list as `ToolDescriptor[]` cannot do this: it pins `I` to `unknown` for every entry,
+ * so each tool's schema and handler are only ever checked against `unknown` and never against each
+ * other. Inferring `I` per tool here means a handler that reads a field its schema does not declare
+ * is a compile error, and the handler's parameter no longer has to be written out by hand — it is
+ * whatever the schema produces, so the two cannot drift apart.
+ */
+export function defineTool<I>(descriptor: ToolDescriptor<I>): AnyToolDescriptor {
+ return descriptor;
+}
diff --git a/src/core/agent-tools/tools/contact.tools.ts b/src/core/agent-tools/tools/contact.tools.ts
index 49abdbe73..a4ff60972 100644
--- a/src/core/agent-tools/tools/contact.tools.ts
+++ b/src/core/agent-tools/tools/contact.tools.ts
@@ -1,13 +1,13 @@
import { z } from 'zod';
import { ApiKeyRole } from '../../../modules/auth/entities/api-key.entity';
import type { ContactService } from '../../../modules/contact/contact.service';
-import type { AnyToolDescriptor } from '../tool-descriptor';
+import { defineTool, type AnyToolDescriptor } from '../tool-descriptor';
const sessionId = z.string().min(1).describe('Session UUID (the session id, not the name)');
export function contactTools(contact: ContactService): AnyToolDescriptor[] {
return [
- {
+ defineTool({
name: 'ContactFindAll',
description: 'List all contacts for a session. Use limit/offset to page through large contact lists.',
tier: 'read',
@@ -17,10 +17,9 @@ export function contactTools(contact: ContactService): AnyToolDescriptor[] {
limit: z.number().int().min(1).max(1000).optional(),
offset: z.number().int().min(0).optional(),
}),
- handler: (input: { sessionId: string; limit?: number; offset?: number }) =>
- contact.getContacts(input.sessionId, { limit: input.limit, offset: input.offset }),
- },
- {
+ handler: input => contact.getContacts(input.sessionId, { limit: input.limit, offset: input.offset }),
+ }),
+ defineTool({
name: 'ContactFindOne',
description: 'Get details for a specific contact by JID (e.g. [email protected]).',
tier: 'read',
@@ -29,10 +28,9 @@ export function contactTools(contact: ContactService): AnyToolDescriptor[] {
sessionId,
contactId: z.string().describe('Contact JID (e.g. [email protected])'),
}),
- handler: (input: { sessionId: string; contactId: string }) =>
- contact.getContactById(input.sessionId, input.contactId),
- },
- {
+ handler: input => contact.getContactById(input.sessionId, input.contactId),
+ }),
+ defineTool({
name: 'ContactCheckNumber',
description:
'Check whether a phone number is registered on WhatsApp. Returns exists flag and the WhatsApp JID if found.',
@@ -42,12 +40,12 @@ export function contactTools(contact: ContactService): AnyToolDescriptor[] {
sessionId,
number: z.string().describe('Phone number to check (e.g. 628123456789, digits only)'),
}),
- handler: async (input: { sessionId: string; number: string }) => {
+ handler: async input => {
const whatsappId = await contact.getNumberId(input.sessionId, input.number);
return { number: input.number, exists: whatsappId !== null, whatsappId };
},
- },
- {
+ }),
+ defineTool({
name: 'ContactResolvePhone',
description:
'Resolve a contact JID (e.g. an @lid) to a phone number. Best-effort — returns null when the engine cannot map it.',
@@ -57,12 +55,12 @@ export function contactTools(contact: ContactService): AnyToolDescriptor[] {
sessionId,
contactId: z.string().describe('Contact JID to resolve (e.g. an @lid)'),
}),
- handler: async (input: { sessionId: string; contactId: string }) => {
+ handler: async input => {
const phone = await contact.resolveContactPhone(input.sessionId, input.contactId);
return { contactId: input.contactId, phone };
},
- },
- {
+ }),
+ defineTool({
name: 'ContactGetProfilePicture',
description: 'Get the profile picture URL for a contact.',
tier: 'read',
@@ -71,12 +69,12 @@ export function contactTools(contact: ContactService): AnyToolDescriptor[] {
sessionId,
contactId: z.string().describe('Contact JID (e.g. [email protected])'),
}),
- handler: async (input: { sessionId: string; contactId: string }) => {
+ handler: async input => {
const url = await contact.getProfilePicture(input.sessionId, input.contactId);
return { url };
},
- },
- {
+ }),
+ defineTool({
name: 'ContactBlock',
description: 'Block a contact. The contact will no longer be able to send messages. Requires OPERATOR role.',
tier: 'write',
@@ -86,12 +84,12 @@ export function contactTools(contact: ContactService): AnyToolDescriptor[] {
sessionId,
contactId: z.string().describe('Contact JID (e.g. [email protected])'),
}),
- handler: async (input: { sessionId: string; contactId: string }) => {
+ handler: async input => {
await contact.blockContact(input.sessionId, input.contactId);
return { success: true, message: 'Contact blocked' };
},
- },
- {
+ }),
+ defineTool({
name: 'ContactUnblock',
description: 'Unblock a previously blocked contact. Requires OPERATOR role.',
tier: 'write',
@@ -101,10 +99,10 @@ export function contactTools(contact: ContactService): AnyToolDescriptor[] {
sessionId,
contactId: z.string().describe('Contact JID (e.g. [email protected])'),
}),
- handler: async (input: { sessionId: string; contactId: string }) => {
+ handler: async input => {
await contact.unblockContact(input.sessionId, input.contactId);
return { success: true, message: 'Contact unblocked' };
},
- },
+ }),
];
}
diff --git a/src/core/agent-tools/tools/group.tools.ts b/src/core/agent-tools/tools/group.tools.ts
index bfe8f845d..582f46f66 100644
--- a/src/core/agent-tools/tools/group.tools.ts
+++ b/src/core/agent-tools/tools/group.tools.ts
@@ -6,13 +6,13 @@ import {
GROUP_NAME_MAX_LENGTH,
GROUP_PARTICIPANTS_MAX,
} from '../../../modules/group/dto/group.dto';
-import type { AnyToolDescriptor } from '../tool-descriptor';
+import { defineTool, type AnyToolDescriptor } from '../tool-descriptor';
const sessionId = z.string().min(1).describe('Session UUID (the session id, not the name)');
export function groupTools(group: GroupService): AnyToolDescriptor[] {
return [
- {
+ defineTool({
name: 'GroupFindAll',
description: 'List all groups the session is a member of. Use limit/offset to page.',
tier: 'read',
@@ -22,10 +22,9 @@ export function groupTools(group: GroupService): AnyToolDescriptor[] {
limit: z.number().int().min(1).max(1000).optional(),
offset: z.number().int().min(0).optional(),
}),
- handler: (input: { sessionId: string; limit?: number; offset?: number }) =>
- group.getGroups(input.sessionId, { limit: input.limit, offset: input.offset }),
- },
- {
+ handler: input => group.getGroups(input.sessionId, { limit: input.limit, offset: input.offset }),
+ }),
+ defineTool({
name: 'GroupFindOne',
description: 'Get detailed info for a specific group including participants list.',
tier: 'read',
@@ -34,9 +33,9 @@ export function groupTools(group: GroupService): AnyToolDescriptor[] {
sessionId,
groupId: z.string().describe('Group JID (e.g. [email protected])'),
}),
- handler: (input: { sessionId: string; groupId: string }) => group.getGroupInfo(input.sessionId, input.groupId),
- },
- {
+ handler: input => group.getGroupInfo(input.sessionId, input.groupId),
+ }),
+ defineTool({
name: 'GroupGetInviteCode',
description: 'Get the invite code and link for a group.',
tier: 'read',
@@ -45,12 +44,12 @@ export function groupTools(group: GroupService): AnyToolDescriptor[] {
sessionId,
groupId: z.string().describe('Group JID (e.g. [email protected])'),
}),
- handler: async (input: { sessionId: string; groupId: string }) => {
+ handler: async input => {
const inviteCode = await group.getGroupInviteCode(input.sessionId, input.groupId);
return { inviteCode, inviteLink: `https://chat.whatsapp.com/${inviteCode}` };
},
- },
- {
+ }),
+ defineTool({
name: 'GroupCreate',
description: 'Create a new WhatsApp group with a name and initial participants. Requires OPERATOR role.',
tier: 'write',
@@ -65,10 +64,9 @@ export function groupTools(group: GroupService): AnyToolDescriptor[] {
.max(GROUP_PARTICIPANTS_MAX)
.describe('Participant WhatsApp JIDs (e.g. [email protected])'),
}),
- handler: (input: { sessionId: string; name: string; participants: string[] }) =>
- group.createGroup(input.sessionId, input.name, input.participants),
- },
- {
+ handler: input => group.createGroup(input.sessionId, input.name, input.participants),
+ }),
+ defineTool({
name: 'GroupAddParticipants',
description:
'Add participants to an existing group. The returned `results` carry the per-participant outcome (a partial refusal does not fail the batch). Requires OPERATOR role.',
@@ -84,7 +82,7 @@ export function groupTools(group: GroupService): AnyToolDescriptor[] {
.max(GROUP_PARTICIPANTS_MAX)
.describe('Participant WhatsApp JIDs to add'),
}),
- handler: async (input: { sessionId: string; groupId: string; participants: string[] }) => {
+ handler: async input => {
const results = await group.addParticipants(input.sessionId, input.groupId, input.participants);
// Derive the verdict from the per-participant outcomes instead of asserting it: a batch in
// which every participant was refused (not registered, already a member) must not report
@@ -99,8 +97,8 @@ export function groupTools(group: GroupService): AnyToolDescriptor[] {
results,
};
},
- },
- {
+ }),
+ defineTool({
name: 'GroupSetSubject',
description: 'Change the group name/subject. Requires OPERATOR role.',
tier: 'write',
@@ -112,12 +110,12 @@ export function groupTools(group: GroupService): AnyToolDescriptor[] {
groupId: z.string().describe('Group JID (e.g. [email protected])'),
subject: z.string().min(1).max(GROUP_NAME_MAX_LENGTH).describe('New group subject/name'),
}),
- handler: async (input: { sessionId: string; groupId: string; subject: string }) => {
+ handler: async input => {
await group.setGroupSubject(input.sessionId, input.groupId, input.subject);
return { success: true, message: 'Group subject updated' };
},
- },
- {
+ }),
+ defineTool({
name: 'GroupSetDescription',
description: 'Change the group description. Pass empty string to clear it. Requires OPERATOR role.',
tier: 'write',
@@ -132,10 +130,10 @@ export function groupTools(group: GroupService): AnyToolDescriptor[] {
.max(GROUP_DESCRIPTION_MAX_LENGTH)
.describe('New group description (may be empty to clear)'),
}),
- handler: async (input: { sessionId: string; groupId: string; description: string }) => {
+ handler: async input => {
await group.setGroupDescription(input.sessionId, input.groupId, input.description);
return { success: true, message: 'Group description updated' };
},
- },
+ }),
];
}
diff --git a/src/core/agent-tools/tools/message.tools.ts b/src/core/agent-tools/tools/message.tools.ts
index 7e524f265..70e8b771a 100644
--- a/src/core/agent-tools/tools/message.tools.ts
+++ b/src/core/agent-tools/tools/message.tools.ts
@@ -8,13 +8,13 @@ import {
LOCATION_TEXT_MAX_LENGTH,
REACTION_EMOJI_MAX_LENGTH,
} from '../../../modules/message/dto/message-actions.dto';
-import type { AnyToolDescriptor } from '../tool-descriptor';
+import { defineTool, type AnyToolDescriptor } from '../tool-descriptor';
const sessionId = z.string().min(1).describe('Session UUID (the session id, not the name)');
export function messageTools(message: MessageService): AnyToolDescriptor[] {
return [
- {
+ defineTool({
name: 'MessageList',
description:
'List persisted messages for a session, optionally filtered by chatId or sender. Reads from the local DB.',
@@ -27,15 +27,15 @@ export function messageTools(message: MessageService): AnyToolDescriptor[] {
limit: z.number().int().min(1).max(100).optional(),
offset: z.number().int().min(0).optional(),
}),
- handler: (input: { sessionId: string; chatId?: string; from?: string; limit?: number; offset?: number }) =>
+ handler: input =>
message.getMessages(input.sessionId, {
chatId: input.chatId,
from: input.from,
limit: input.limit,
offset: input.offset,
}),
- },
- {
+ }),
+ defineTool({
name: 'MessageHistory',
description:
'Fetch live chat history from WhatsApp for a specific chat. Bypasses the local DB — useful for messages that arrived before the gateway started.',
@@ -54,10 +54,10 @@ export function messageTools(message: MessageService): AnyToolDescriptor[] {
includeMedia: z.boolean().optional().describe('Download media as base64 (slower)'),
deep: z.boolean().optional().describe('Raise limit ceiling to 2000 for reaching further back in history'),
}),
- handler: (input: { sessionId: string; chatId: string; limit?: number; includeMedia?: boolean; deep?: boolean }) =>
+ handler: input =>
message.getChatHistory(input.sessionId, input.chatId, input.limit, input.includeMedia, input.deep),
- },
- {
+ }),
+ defineTool({
name: 'MessageGetReactions',
description: 'Get reactions for a specific message, including which contacts sent which emoji.',
tier: 'read',
@@ -67,10 +67,9 @@ export function messageTools(message: MessageService): AnyToolDescriptor[] {
chatId: z.string().describe('Chat JID containing the message'),
messageId: z.string().describe('Message ID to get reactions for'),
}),
- handler: (input: { sessionId: string; chatId: string; messageId: string }) =>
- message.getMessageReactions(input.sessionId, input.chatId, input.messageId),
- },
- {
+ handler: input => message.getMessageReactions(input.sessionId, input.chatId, input.messageId),
+ }),
+ defineTool({
name: 'MessageSendText',
description: 'Send a plain text message to a chat or group. Requires OPERATOR role.',
tier: 'write',
@@ -88,14 +87,14 @@ export function messageTools(message: MessageService): AnyToolDescriptor[] {
'unset means the engine default, and the engines differ.',
),
}),
- handler: (input: { sessionId: string; chatId: string; text: string; linkPreview?: boolean }) =>
+ handler: input =>
message.sendText(input.sessionId, {
chatId: input.chatId,
text: input.text,
...(input.linkPreview === undefined ? {} : { linkPreview: input.linkPreview }),
}),
- },
- {
+ }),
+ defineTool({
name: 'MessageSendImage',
description: 'Send an image message via URL or base64. Requires OPERATOR role.',
tier: 'write',
@@ -110,15 +109,7 @@ export function messageTools(message: MessageService): AnyToolDescriptor[] {
filename: z.string().max(255).optional(),
caption: z.string().max(1024).optional(),
}),
- handler: (input: {
- sessionId: string;
- chatId: string;
- url?: string;
- base64?: string;
- mimetype?: string;
- filename?: string;
- caption?: string;
- }) =>
+ handler: input =>
message.sendImage(input.sessionId, {
chatId: input.chatId,
url: input.url,
@@ -127,8 +118,8 @@ export function messageTools(message: MessageService): AnyToolDescriptor[] {
filename: input.filename,
caption: input.caption,
}),
- },
- {
+ }),
+ defineTool({
name: 'MessageSendVideo',
description: 'Send a video message via URL or base64. Requires OPERATOR role.',
tier: 'write',
@@ -143,15 +134,7 @@ export function messageTools(message: MessageService): AnyToolDescriptor[] {
filename: z.string().max(255).optional(),
caption: z.string().max(1024).optional(),
}),
- handler: (input: {
- sessionId: string;
- chatId: string;
- url?: string;
- base64?: string;
- mimetype?: string;
- filename?: string;
- caption?: string;
- }) =>
+ handler: input =>
message.sendVideo(input.sessionId, {
chatId: input.chatId,
url: input.url,
@@ -160,8 +143,8 @@ export function messageTools(message: MessageService): AnyToolDescriptor[] {
filename: input.filename,
caption: input.caption,
}),
- },
- {
+ }),
+ defineTool({
name: 'MessageSendAudio',
description: 'Send an audio/voice message via URL or base64. Requires OPERATOR role.',
tier: 'write',
@@ -177,16 +160,7 @@ export function messageTools(message: MessageService): AnyToolDescriptor[] {
caption: z.string().max(1024).optional(),
ptt: z.boolean().optional().describe('Send as a WhatsApp voice note (PTT)'),
}),
- handler: (input: {
- sessionId: string;
- chatId: string;
- url?: string;
- base64?: string;
- mimetype?: string;
- filename?: string;
- caption?: string;
- ptt?: boolean;
- }) =>
+ handler: input =>
message.sendAudio(input.sessionId, {
chatId: input.chatId,
url: input.url,
@@ -196,8 +170,8 @@ export function messageTools(message: MessageService): AnyToolDescriptor[] {
caption: input.caption,
ptt: input.ptt,
}),
- },
- {
+ }),
+ defineTool({
name: 'MessageSendDocument',
description: 'Send a document/file message via URL or base64. Requires OPERATOR role.',
tier: 'write',
@@ -212,15 +186,7 @@ export function messageTools(message: MessageService): AnyToolDescriptor[] {
filename: z.string().max(255).optional(),
caption: z.string().max(1024).optional(),
}),
- handler: (input: {
- sessionId: string;
- chatId: string;
- url?: string;
- base64?: string;
- mimetype?: string;
- filename?: string;
- caption?: string;
- }) =>
+ handler: input =>
message.sendDocument(input.sessionId, {
chatId: input.chatId,
url: input.url,
@@ -229,8 +195,8 @@ export function messageTools(message: MessageService): AnyToolDescriptor[] {
filename: input.filename,
caption: input.caption,
}),
- },
- {
+ }),
+ defineTool({
name: 'MessageSendLocation',
description: 'Send a location pin message. Requires OPERATOR role.',
tier: 'write',
@@ -244,14 +210,7 @@ export function messageTools(message: MessageService): AnyToolDescriptor[] {
description: z.string().max(LOCATION_TEXT_MAX_LENGTH).optional().describe('Location label/description'),
address: z.string().max(LOCATION_TEXT_MAX_LENGTH).optional().describe('Street address'),
}),
- handler: (input: {
- sessionId: string;
- chatId: string;
- latitude: number;
- longitude: number;
- description?: string;
- address?: string;
- }) =>
+ handler: input =>
message.sendLocation(input.sessionId, {
chatId: input.chatId,
latitude: input.latitude,
@@ -259,8 +218,8 @@ export function messageTools(message: MessageService): AnyToolDescriptor[] {
description: input.description,
address: input.address,
}),
- },
- {
+ }),
+ defineTool({
name: 'MessageSendContact',
description: 'Send a contact card message. Requires OPERATOR role.',
tier: 'write',
@@ -276,14 +235,14 @@ export function messageTools(message: MessageService): AnyToolDescriptor[] {
.max(CONTACT_NUMBER_MAX_LENGTH)
.describe('Phone number of the contact to share'),
}),
- handler: (input: { sessionId: string; chatId: string; contactName: string; contactNumber: string }) =>
+ handler: input =>
message.sendContact(input.sessionId, {
chatId: input.chatId,
contactName: input.contactName,
contactNumber: input.contactNumber,
}),
- },
- {
+ }),
+ defineTool({
name: 'MessageSendSticker',
description: 'Send a sticker message via URL or base64. Requires OPERATOR role.',
tier: 'write',
@@ -298,15 +257,7 @@ export function messageTools(message: MessageService): AnyToolDescriptor[] {
filename: z.string().max(255).optional(),
caption: z.string().max(1024).optional(),
}),
- handler: (input: {
- sessionId: string;
- chatId: string;
- url?: string;
- base64?: string;
- mimetype?: string;
- filename?: string;
- caption?: string;
- }) =>
+ handler: input =>
message.sendSticker(input.sessionId, {
chatId: input.chatId,
url: input.url,
@@ -315,8 +266,8 @@ export function messageTools(message: MessageService): AnyToolDescriptor[] {
filename: input.filename,
caption: input.caption,
}),
- },
- {
+ }),
+ defineTool({
name: 'MessageSendTemplate',
description:
'Render a stored text template and send it as a text message. Provide either templateId or templateName. Requires OPERATOR role.',
@@ -333,21 +284,15 @@ export function messageTools(message: MessageService): AnyToolDescriptor[] {
.optional()
.describe('Variables to substitute into {{placeholder}} tokens'),
}),
- handler: (input: {
- sessionId: string;
- chatId: string;
- templateId?: string;
- templateName?: string;
- vars?: Record<string, string>;
- }) =>
+ handler: input =>
message.sendTemplate(input.sessionId, {
chatId: input.chatId,
templateId: input.templateId,
templateName: input.templateName,
vars: input.vars,
}),
- },
- {
+ }),
+ defineTool({
name: 'MessageReply',
description: 'Reply to a specific message (quoted reply). Requires OPERATOR role.',
tier: 'write',
@@ -359,14 +304,14 @@ export function messageTools(message: MessageService): AnyToolDescriptor[] {
quotedMessageId: z.string().describe('ID of the message to quote/reply to'),
text: z.string().min(1).max(MESSAGE_TEXT_MAX_LENGTH).describe('Reply text content'),
}),
- handler: (input: { sessionId: string; chatId: string; quotedMessageId: string; text: string }) =>
+ handler: input =>
message.reply(input.sessionId, {
chatId: input.chatId,
quotedMessageId: input.quotedMessageId,
text: input.text,
}),
- },
- {
+ }),
+ defineTool({
name: 'MessageForward',
description: 'Forward a message from one chat to another. Requires OPERATOR role.',
tier: 'write',
@@ -378,14 +323,14 @@ export function messageTools(message: MessageService): AnyToolDescriptor[] {
toChatId: z.string().describe('Destination chat JID'),
messageId: z.string().describe('ID of the message to forward'),
}),
- handler: (input: { sessionId: string; fromChatId: string; toChatId: string; messageId: string }) =>
+ handler: input =>
message.forward(input.sessionId, {
fromChatId: input.fromChatId,
toChatId: input.toChatId,
messageId: input.messageId,
}),
- },
- {
+ }),
+ defineTool({
name: 'MessageReact',
description:
'Add or remove a reaction emoji on a message. Send empty string emoji to remove. Requires OPERATOR role.',
@@ -401,10 +346,10 @@ export function messageTools(message: MessageService): AnyToolDescriptor[] {
.max(REACTION_EMOJI_MAX_LENGTH)
.describe('Emoji to react with. Empty string removes the reaction.'),
}),
- handler: (input: { sessionId: string; chatId: string; messageId: string; emoji: string }) =>
+ handler: input =>
message
.reactToMessage(input.sessionId, { chatId: input.chatId, messageId: input.messageId, emoji: input.emoji })
.then(() => ({ success: true })),
- },
+ }),
];
}
diff --git a/src/core/agent-tools/tools/session.tools.ts b/src/core/agent-tools/tools/session.tools.ts
index 7a747a39a..0d9767a52 100644
--- a/src/core/agent-tools/tools/session.tools.ts
+++ b/src/core/agent-tools/tools/session.tools.ts
@@ -1,15 +1,14 @@
import { z } from 'zod';
import { ApiKeyRole } from '../../../modules/auth/entities/api-key.entity';
-import type { ChatState } from '../../../engine/interfaces/whatsapp-engine.interface';
import type { SessionService } from '../../../modules/session/session.service';
import { SessionResponseDto } from '../../../modules/session/dto/session-response.dto';
-import type { AnyToolDescriptor } from '../tool-descriptor';
+import { defineTool, type AnyToolDescriptor } from '../tool-descriptor';
const sessionId = z.string().min(1).describe('Session UUID (the session id, not the name)');
export function sessionTools(session: SessionService): AnyToolDescriptor[] {
return [
- {
+ defineTool({
name: 'SessionFindAll',
description:
'List the WhatsApp sessions this API key may access (id, name, status). Use to discover available sessions before calling session-scoped tools. Supports limit/offset paging.',
@@ -18,21 +17,21 @@ export function sessionTools(session: SessionService): AnyToolDescriptor[] {
limit: z.number().int().min(1).max(1000).optional(),
offset: z.number().int().min(0).optional(),
}),
- handler: (input: { limit?: number; offset?: number }, apiKey) =>
+ handler: (input, apiKey) =>
session
.findAll(apiKey.allowedSessions, { limit: input.limit, offset: input.offset })
.then(ss => ss.map(s => SessionResponseDto.fromEntity(s, session.isActive(s.id)))),
- },
- {
+ }),
+ defineTool({
name: 'SessionFindOne',
description: 'Get one session by its UUID, including connection status and phone number.',
tier: 'read',
sessionScoped: true,
inputSchema: z.object({ sessionId }),
- handler: (input: { sessionId: string }) =>
+ handler: input =>
session.findOne(input.sessionId).then(s => SessionResponseDto.fromEntity(s, session.isActive(s.id))),
- },
- {
+ }),
+ defineTool({
name: 'SessionGetChats',
description: 'List recent chats for a session (most recent first). Use limit/offset to page through large lists.',
tier: 'read',
@@ -42,17 +41,16 @@ export function sessionTools(session: SessionService): AnyToolDescriptor[] {
limit: z.number().int().min(1).max(1000).optional(),
offset: z.number().int().min(0).optional(),
}),
- handler: (input: { sessionId: string; limit?: number; offset?: number }) =>
- session.getChats(input.sessionId, { limit: input.limit, offset: input.offset }),
- },
- {
+ handler: input => session.getChats(input.sessionId, { limit: input.limit, offset: input.offset }),
+ }),
+ defineTool({
name: 'SessionGetStats',
description: 'Aggregate session counts (total, active, ready, disconnected) the key is allowed to see.',
tier: 'read',
inputSchema: z.object({}),
handler: (_input, apiKey) => session.getStats(apiKey.allowedSessions),
- },
- {
+ }),
+ defineTool({
name: 'SessionSubscribePresence',
description:
"Subscribe to a chat's presence (online / typing / recording). Updates then arrive as " +
@@ -66,10 +64,9 @@ export function sessionTools(session: SessionService): AnyToolDescriptor[] {
sessionId,
chatId: z.string().describe('Chat JID (e.g. [email protected])'),
}),
- handler: (input: { sessionId: string; chatId: string }) =>
- session.subscribeToPresence(input.sessionId, input.chatId).then(() => ({ success: true })),
- },
- {
+ handler: input => session.subscribeToPresence(input.sessionId, input.chatId).then(() => ({ success: true })),
+ }),
+ defineTool({
name: 'SessionGetPresence',
description:
'The last presence reported for a chat, or null when none has been — the chat was never ' +
@@ -81,9 +78,9 @@ export function sessionTools(session: SessionService): AnyToolDescriptor[] {
sessionId,
chatId: z.string().describe('Chat JID (e.g. [email protected])'),
}),
- handler: (input: { sessionId: string; chatId: string }) => session.getPresence(input.sessionId, input.chatId),
- },
- {
+ handler: input => session.getPresence(input.sessionId, input.chatId),
+ }),
+ defineTool({
name: 'SessionMarkChatRead',
description: 'Mark a chat as read (clears unread count). Requires OPERATOR role.',
tier: 'write',
@@ -93,10 +90,9 @@ export function sessionTools(session: SessionService): AnyToolDescriptor[] {
sessionId,
chatId: z.string().describe('Chat JID (e.g. [email protected])'),
}),
- handler: (input: { sessionId: string; chatId: string }) =>
- session.sendSeen(input.sessionId, input.chatId).then(success => ({ success })),
- },
- {
+ handler: input => session.sendSeen(input.sessionId, input.chatId).then(success => ({ success })),
+ }),
+ defineTool({
name: 'SessionMarkChatUnread',
description: 'Mark a chat as unread. Requires OPERATOR role.',
tier: 'write',
@@ -106,10 +102,9 @@ export function sessionTools(session: SessionService): AnyToolDescriptor[] {
sessionId,
chatId: z.string().describe('Chat JID (e.g. [email protected])'),
}),
- handler: (input: { sessionId: string; chatId: string }) =>
- session.markUnread(input.sessionId, input.chatId).then(success => ({ success })),
- },
- {
+ handler: input => session.markUnread(input.sessionId, input.chatId).then(success => ({ success })),
+ }),
+ defineTool({
name: 'SessionSendChatState',
description: "Show a typing/recording indicator in a chat, or clear it with 'paused'. Requires OPERATOR role.",
tier: 'write',
@@ -122,8 +117,8 @@ export function sessionTools(session: SessionService): AnyToolDescriptor[] {
.enum(['typing', 'recording', 'paused'])
.describe("'typing' or 'recording' shows the indicator; 'paused' clears it"),
}),
- handler: (input: { sessionId: string; chatId: string; state: ChatState }) =>
+ handler: input =>
session.sendChatState(input.sessionId, input.chatId, input.state).then(() => ({ success: true })),
- },
+ }),
];
}
diff --git a/src/core/agent-tools/tools/webhook.tools.ts b/src/core/agent-tools/tools/webhook.tools.ts
index cb7d043da..a31aa0c34 100644
--- a/src/core/agent-tools/tools/webhook.tools.ts
+++ b/src/core/agent-tools/tools/webhook.tools.ts
@@ -2,13 +2,13 @@ import { z } from 'zod';
import { ApiKeyRole } from '../../../modules/auth/entities/api-key.entity';
import type { WebhookService } from '../../../modules/webhook/webhook.service';
import { WebhookResponseDto } from '../../../modules/webhook/dto/webhook.dto';
-import type { AnyToolDescriptor } from '../tool-descriptor';
+import { defineTool, type AnyToolDescriptor } from '../tool-descriptor';
const sessionId = z.string().min(1).describe('Session UUID (the session id, not the name)');
export function webhookTools(webhook: WebhookService): AnyToolDescriptor[] {
return [
- {
+ defineTool({
name: 'WebhooksList',
description:
'List all webhooks the API key is allowed to see, across all its accessible sessions. Supports limit/offset paging.',
@@ -18,22 +18,21 @@ export function webhookTools(webhook: WebhookService): AnyToolDescriptor[] {
limit: z.number().int().min(1).max(1000).optional(),
offset: z.number().int().min(0).optional(),
}),
- handler: (input: { limit?: number; offset?: number }, apiKey) =>
+ handler: (input, apiKey) =>
webhook
.findAll(apiKey.allowedSessions, { limit: input.limit, offset: input.offset })
.then(ws => WebhookResponseDto.fromEntities(ws)),
- },
- {
+ }),
+ defineTool({
name: 'WebhookFindBySession',
description: 'List all webhooks registered for a specific session.',
tier: 'read',
requiredRole: ApiKeyRole.OPERATOR,
sessionScoped: true,
inputSchema: z.object({ sessionId }),
- handler: (input: { sessionId: string }) =>
- webhook.findBySession(input.sessionId).then(ws => WebhookResponseDto.fromEntities(ws)),
- },
- {
+ handler: input => webhook.findBySession(input.sessionId).then(ws => WebhookResponseDto.fromEntities(ws)),
+ }),
+ defineTool({
name: 'WebhookFindOne',
description: 'Get details for a specific webhook by ID within a session.',
tier: 'read',
@@ -43,8 +42,7 @@ export function webhookTools(webhook: WebhookService): AnyToolDescriptor[] {
sessionId,
webhookId: z.string().describe('Webhook UUID'),
}),
- handler: (input: { sessionId: string; webhookId: string }) =>
- webhook.findOne(input.sessionId, input.webhookId).then(w => WebhookResponseDto.fromEntity(w)),
- },
+ handler: input => webhook.findOne(input.sessionId, input.webhookId).then(w => WebhookResponseDto.fromEntity(w)),
+ }),
];
}