n8n-nodes-evolution-message-search 2.0.1 → 2.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4,7 +4,6 @@ Object.defineProperty(exports, '__esModule', { value: true });
4
4
  exports.SearchMessages = void 0;
5
5
 
6
6
  const n8n_workflow_1 = require('n8n-workflow');
7
- const core_tools_1 = require('@langchain/core/tools');
8
7
 
9
8
  class SearchMessages {
10
9
  constructor() {
@@ -19,9 +18,9 @@ class SearchMessages {
19
18
  defaults: {
20
19
  name: 'Evolution API - Search Messages',
21
20
  },
22
- inputs: [],
23
- outputs: [n8n_workflow_1.NodeConnectionTypes.AiTool],
24
- outputNames: ['Tool'],
21
+ usableAsTool: true,
22
+ inputs: [n8n_workflow_1.NodeConnectionTypes.Main],
23
+ outputs: [n8n_workflow_1.NodeConnectionTypes.Main],
25
24
  credentials: [
26
25
  {
27
26
  name: 'evolutionApi',
@@ -43,7 +42,7 @@ class SearchMessages {
43
42
  type: 'string',
44
43
  required: false,
45
44
  default: '',
46
- description: 'WhatsApp conversation ID (JID). If empty, searches all conversations. Model can set if allowed.',
45
+ description: 'WhatsApp conversation ID (JID). If empty, searches all conversations.',
47
46
  placeholder: '5511999999999@s.whatsapp.net',
48
47
  },
49
48
  {
@@ -51,7 +50,33 @@ class SearchMessages {
51
50
  name: 'allowModelSetConversation',
52
51
  type: 'boolean',
53
52
  default: false,
54
- description: 'If enabled, the AI model can set the conversation ID',
53
+ description: 'If enabled, the AI model can set the conversation ID via $fromAI()',
54
+ },
55
+ {
56
+ displayName: 'Keywords',
57
+ name: 'keywords',
58
+ type: 'string',
59
+ required: false,
60
+ default: '',
61
+ description: 'Search text in message content (model can set)',
62
+ },
63
+ {
64
+ displayName: 'Date From',
65
+ name: 'dateFrom',
66
+ type: 'string',
67
+ required: false,
68
+ default: '',
69
+ description: 'Start date for message search (model can set)',
70
+ placeholder: '2024-01-01T00:00:00Z',
71
+ },
72
+ {
73
+ displayName: 'Date To',
74
+ name: 'dateTo',
75
+ type: 'string',
76
+ required: false,
77
+ default: '',
78
+ description: 'End date for message search (model can set)',
79
+ placeholder: '2024-12-31T23:59:59Z',
55
80
  },
56
81
  {
57
82
  displayName: 'Items per Page',
@@ -64,88 +89,109 @@ class SearchMessages {
64
89
  },
65
90
  description: 'Number of results per page (defined by user, model cannot change)',
66
91
  },
92
+ {
93
+ displayName: 'Page Number',
94
+ name: 'page',
95
+ type: 'number',
96
+ default: 1,
97
+ typeOptions: {
98
+ minValue: 1,
99
+ },
100
+ description: 'Page number to retrieve (model can navigate)',
101
+ },
67
102
  ],
68
103
  };
69
104
  }
70
105
 
71
- async supplyData(itemIndex) {
106
+ async execute() {
107
+ const items = this.getInputData ? this.getInputData() : [];
108
+ const returnData = [];
109
+
110
+ for (let itemIndex = 0; itemIndex < (items.length || 1); itemIndex++) {
111
+ try {
112
+ const result = await this.executeSingle.call(this, itemIndex);
113
+ returnData.push({
114
+ json: result,
115
+ pairedItem: { item: itemIndex },
116
+ });
117
+ } catch (err) {
118
+ if (this.continueOnFail && this.continueOnFail()) {
119
+ returnData.push({
120
+ json: { error: err.message },
121
+ pairedItem: { item: itemIndex },
122
+ });
123
+ } else {
124
+ throw err;
125
+ }
126
+ }
127
+ }
128
+
129
+ return [returnData];
130
+ }
131
+
132
+ async executeSingle(itemIndex) {
72
133
  const credentials = await this.getCredentials('evolutionApi');
73
134
  const serverUrl = credentials['server-url'];
74
135
  const apiKey = credentials.apikey;
136
+
75
137
  const instanceName = this.getNodeParameter('instanceName', itemIndex);
76
138
  const userConversationId = this.getNodeParameter('conversationId', itemIndex) || '';
77
- const allowModelSetConversation = this.getNodeParameter('allowModelSetConversation', itemIndex);
78
- const pageSizeDefault = this.getNodeParameter('pageSize', itemIndex) || 10;
79
- const ctx = this;
80
-
81
- const tool = new core_tools_1.DynamicTool({
82
- name: n8n_workflow_1.nodeNameToToolName(this.getNode()),
83
- description: 'Search WhatsApp message history. Input must be a JSON object with: keywords (string, optional), dateFrom (ISO date string, optional), dateTo (ISO date string, optional), page (number, default 1), pageSize (number, default 10), conversationId (string, optional). Returns paginated message search results.',
84
- func: async (input) => {
85
- const params = typeof input === 'string' ? JSON.parse(input) : (input || {});
86
- let conversationId = userConversationId;
87
- if (!conversationId && allowModelSetConversation && params.conversationId) {
88
- conversationId = params.conversationId;
89
- }
90
- const keywords = params.keywords || '';
91
- const dateFrom = params.dateFrom || '';
92
- const dateTo = params.dateTo || '';
93
- const pageSize = params.pageSize || pageSizeDefault;
94
- const page = params.page || 1;
95
- const skip = (page - 1) * pageSize;
139
+ const keywords = this.getNodeParameter('keywords', itemIndex) || '';
140
+ const dateFrom = this.getNodeParameter('dateFrom', itemIndex) || '';
141
+ const dateTo = this.getNodeParameter('dateTo', itemIndex) || '';
142
+ const pageSize = this.getNodeParameter('pageSize', itemIndex) || 10;
143
+ const page = this.getNodeParameter('page', itemIndex) || 1;
96
144
 
97
- const whereClause = {};
98
- if (conversationId) {
99
- whereClause['key.remote'] = conversationId;
100
- }
101
- if (keywords) {
102
- whereClause['message.conversationMessage.text'] = { contains: keywords };
103
- }
104
- if (dateFrom || dateTo) {
105
- whereClause['timestamp'] = {};
106
- if (dateFrom) {
107
- whereClause['timestamp']['gte'] = new Date(dateFrom).getTime() / 1000;
108
- }
109
- if (dateTo) {
110
- whereClause['timestamp']['lte'] = new Date(dateTo).getTime() / 1000;
111
- }
112
- }
145
+ const skip = (page - 1) * pageSize;
113
146
 
114
- const body = {
115
- where: whereClause,
116
- take: pageSize,
117
- skip: skip,
118
- orderBy: { timestamp: 'desc' },
119
- };
120
-
121
- const cleanUrl = serverUrl.replace(/\/$/, '');
122
- const response = await ctx.helpers.request({
123
- method: 'POST',
124
- uri: `${cleanUrl}/chat/findMessages/${instanceName}`,
125
- body,
126
- headers: {
127
- 'Content-Type': 'application/json',
128
- Accept: 'application/json',
129
- apikey: apiKey,
130
- },
131
- json: true,
132
- });
147
+ const whereClause = {};
148
+ if (userConversationId) {
149
+ whereClause['key.remote'] = userConversationId;
150
+ }
151
+ if (keywords) {
152
+ whereClause['message.conversationMessage.text'] = { contains: keywords };
153
+ }
154
+ if (dateFrom || dateTo) {
155
+ whereClause['timestamp'] = {};
156
+ if (dateFrom) {
157
+ whereClause['timestamp']['gte'] = new Date(dateFrom).getTime() / 1000;
158
+ }
159
+ if (dateTo) {
160
+ whereClause['timestamp']['lte'] = new Date(dateTo).getTime() / 1000;
161
+ }
162
+ }
133
163
 
134
- const messages = response.messages || response;
135
- const total = messages.total || messages.length || 0;
136
- const totalPages = Math.ceil(total / pageSize);
164
+ const body = {
165
+ where: whereClause,
166
+ take: pageSize,
167
+ skip: skip,
168
+ orderBy: { timestamp: 'desc' },
169
+ };
137
170
 
138
- return JSON.stringify({
139
- total,
140
- pages: totalPages,
141
- currentPage: page,
142
- pageSize,
143
- records: Array.isArray(messages.records) ? messages.records : messages,
144
- });
171
+ const cleanUrl = serverUrl.replace(/\/$/, '');
172
+ const response = await this.helpers.request({
173
+ method: 'POST',
174
+ uri: `${cleanUrl}/chat/findMessages/${instanceName}`,
175
+ body,
176
+ headers: {
177
+ 'Content-Type': 'application/json',
178
+ Accept: 'application/json',
179
+ apikey: apiKey,
145
180
  },
181
+ json: true,
146
182
  });
147
183
 
148
- return { response: tool };
184
+ const messages = response.messages || response;
185
+ const total = messages.total || messages.length || 0;
186
+ const totalPages = Math.ceil(total / pageSize);
187
+
188
+ return {
189
+ total,
190
+ pages: totalPages,
191
+ currentPage: page,
192
+ pageSize,
193
+ records: Array.isArray(messages.records) ? messages.records : messages,
194
+ };
149
195
  }
150
196
  }
151
197
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "n8n-nodes-evolution-message-search",
3
- "version": "2.0.1",
3
+ "version": "2.0.2",
4
4
  "description": "Evolution API - Search Messages node for n8n. Search WhatsApp message history by keywords, dates, and conversation.",
5
5
  "keywords": [
6
6
  "n8n-community-node-package",
@@ -24,7 +24,6 @@
24
24
  ]
25
25
  },
26
26
  "peerDependencies": {
27
- "n8n-workflow": "*",
28
- "@langchain/core": "*"
27
+ "n8n-workflow": "*"
29
28
  }
30
29
  }