n8n-nodes-msteams-botframework 1.1.0 → 1.2.0

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.
@@ -0,0 +1,5 @@
1
+ import { IExecuteFunctions, INodeExecutionData, INodeType, INodeTypeDescription } from 'n8n-workflow';
2
+ export declare class MsTeamsAIBot implements INodeType {
3
+ description: INodeTypeDescription;
4
+ execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]>;
5
+ }
@@ -0,0 +1,255 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.MsTeamsAIBot = void 0;
7
+ const n8n_workflow_1 = require("n8n-workflow");
8
+ const axios_1 = __importDefault(require("axios"));
9
+ const conversationMemories = {};
10
+ class MsTeamsAIBot {
11
+ constructor() {
12
+ this.description = {
13
+ displayName: 'MS Teams AI Bot',
14
+ name: 'msTeamsAIBot',
15
+ icon: 'file:msteams.svg',
16
+ group: ['transform'],
17
+ version: 1,
18
+ subtitle: '={{$parameter["aiProvider"]}} AI Chatbot',
19
+ description: 'AI-powered Teams bot with chat models, memory, and function calling',
20
+ defaults: {
21
+ name: 'MS Teams AI Bot',
22
+ },
23
+ inputs: ['main'],
24
+ outputs: ['main'],
25
+ credentials: [
26
+ {
27
+ name: 'msTeamsBotFrameworkApi',
28
+ required: true,
29
+ },
30
+ ],
31
+ properties: [
32
+ {
33
+ displayName: 'AI Provider',
34
+ name: 'aiProvider',
35
+ type: 'options',
36
+ options: [
37
+ {
38
+ name: 'OpenAI',
39
+ value: 'openai',
40
+ },
41
+ {
42
+ name: 'Anthropic (Claude)',
43
+ value: 'anthropic',
44
+ },
45
+ ],
46
+ default: 'openai',
47
+ },
48
+ {
49
+ displayName: 'OpenAI API Key',
50
+ name: 'openaiApiKey',
51
+ type: 'string',
52
+ typeOptions: { password: true },
53
+ displayOptions: { show: { aiProvider: ['openai'] } },
54
+ default: '',
55
+ description: 'OpenAI API Key',
56
+ },
57
+ {
58
+ displayName: 'Model',
59
+ name: 'openaiModel',
60
+ type: 'options',
61
+ displayOptions: { show: { aiProvider: ['openai'] } },
62
+ options: [
63
+ { name: 'GPT-4 Turbo', value: 'gpt-4-turbo-preview' },
64
+ { name: 'GPT-4', value: 'gpt-4' },
65
+ { name: 'GPT-3.5 Turbo', value: 'gpt-3.5-turbo' },
66
+ ],
67
+ default: 'gpt-3.5-turbo',
68
+ },
69
+ {
70
+ displayName: 'Anthropic API Key',
71
+ name: 'anthropicApiKey',
72
+ type: 'string',
73
+ typeOptions: { password: true },
74
+ displayOptions: { show: { aiProvider: ['anthropic'] } },
75
+ default: '',
76
+ },
77
+ {
78
+ displayName: 'Model',
79
+ name: 'anthropicModel',
80
+ type: 'options',
81
+ displayOptions: { show: { aiProvider: ['anthropic'] } },
82
+ options: [
83
+ { name: 'Claude 3 Opus', value: 'claude-3-opus-20240229' },
84
+ { name: 'Claude 3 Sonnet', value: 'claude-3-sonnet-20240229' },
85
+ { name: 'Claude 3 Haiku', value: 'claude-3-haiku-20240307' },
86
+ ],
87
+ default: 'claude-3-sonnet-20240229',
88
+ },
89
+ {
90
+ displayName: 'System Prompt',
91
+ name: 'systemPrompt',
92
+ type: 'string',
93
+ typeOptions: { rows: 4 },
94
+ default: 'You are a helpful AI assistant in Microsoft Teams. Be concise and professional.',
95
+ },
96
+ {
97
+ displayName: 'Enable Memory',
98
+ name: 'enableMemory',
99
+ type: 'boolean',
100
+ default: true,
101
+ },
102
+ {
103
+ displayName: 'Memory Limit',
104
+ name: 'memoryLimit',
105
+ type: 'number',
106
+ displayOptions: { show: { enableMemory: [true] } },
107
+ default: 10,
108
+ },
109
+ {
110
+ displayName: 'Options',
111
+ name: 'options',
112
+ type: 'collection',
113
+ default: {},
114
+ options: [
115
+ {
116
+ displayName: 'Temperature',
117
+ name: 'temperature',
118
+ type: 'number',
119
+ default: 0.7,
120
+ },
121
+ {
122
+ displayName: 'Max Tokens',
123
+ name: 'maxTokens',
124
+ type: 'number',
125
+ default: 500,
126
+ },
127
+ {
128
+ displayName: 'Auto Reply',
129
+ name: 'autoReply',
130
+ type: 'boolean',
131
+ default: true,
132
+ },
133
+ ],
134
+ },
135
+ ],
136
+ };
137
+ }
138
+ async execute() {
139
+ var _a, _b, _c;
140
+ const items = this.getInputData();
141
+ const returnData = [];
142
+ const credentials = await this.getCredentials('msTeamsBotFrameworkApi');
143
+ for (let i = 0; i < items.length; i++) {
144
+ try {
145
+ const item = items[i].json;
146
+ const aiProvider = this.getNodeParameter('aiProvider', i);
147
+ const systemPrompt = this.getNodeParameter('systemPrompt', i);
148
+ const enableMemory = this.getNodeParameter('enableMemory', i);
149
+ const memoryLimit = this.getNodeParameter('memoryLimit', i, 10);
150
+ const options = this.getNodeParameter('options', i, {});
151
+ const userMessage = item.text;
152
+ const conversationId = (_a = item.conversation) === null || _a === void 0 ? void 0 : _a.id;
153
+ const serviceUrl = item.serviceUrl;
154
+ if (!conversationMemories[conversationId]) {
155
+ conversationMemories[conversationId] = [];
156
+ }
157
+ if (enableMemory) {
158
+ conversationMemories[conversationId].push({
159
+ role: 'user',
160
+ content: userMessage,
161
+ });
162
+ if (conversationMemories[conversationId].length > memoryLimit * 2) {
163
+ conversationMemories[conversationId] = conversationMemories[conversationId].slice(-memoryLimit * 2);
164
+ }
165
+ }
166
+ const messages = [{ role: 'system', content: systemPrompt }];
167
+ if (enableMemory) {
168
+ messages.push(...conversationMemories[conversationId]);
169
+ }
170
+ else {
171
+ messages.push({ role: 'user', content: userMessage });
172
+ }
173
+ let aiResponse;
174
+ if (aiProvider === 'openai') {
175
+ const apiKey = this.getNodeParameter('openaiApiKey', i);
176
+ const model = this.getNodeParameter('openaiModel', i);
177
+ const response = await axios_1.default.post('https://api.openai.com/v1/chat/completions', {
178
+ model,
179
+ messages,
180
+ temperature: options.temperature || 0.7,
181
+ max_tokens: options.maxTokens || 500,
182
+ }, { headers: { 'Authorization': `Bearer ${apiKey}`, 'Content-Type': 'application/json' } });
183
+ aiResponse = response.data.choices[0].message.content;
184
+ }
185
+ else if (aiProvider === 'anthropic') {
186
+ const apiKey = this.getNodeParameter('anthropicApiKey', i);
187
+ const model = this.getNodeParameter('anthropicModel', i);
188
+ const systemMessage = ((_b = messages.find((m) => m.role === 'system')) === null || _b === void 0 ? void 0 : _b.content) || '';
189
+ const conversationMessages = messages.filter((m) => m.role !== 'system');
190
+ const response = await axios_1.default.post('https://api.anthropic.com/v1/messages', {
191
+ model,
192
+ messages: conversationMessages,
193
+ system: systemMessage,
194
+ max_tokens: options.maxTokens || 500,
195
+ }, {
196
+ headers: {
197
+ 'x-api-key': apiKey,
198
+ 'anthropic-version': '2023-06-01',
199
+ 'Content-Type': 'application/json',
200
+ },
201
+ });
202
+ aiResponse = response.data.content
203
+ .filter((c) => c.type === 'text')
204
+ .map((c) => c.text)
205
+ .join('\n');
206
+ }
207
+ else {
208
+ throw new n8n_workflow_1.NodeOperationError(this.getNode(), `AI provider "${aiProvider}" not implemented`);
209
+ }
210
+ if (enableMemory) {
211
+ conversationMemories[conversationId].push({
212
+ role: 'assistant',
213
+ content: aiResponse,
214
+ });
215
+ }
216
+ if (options.autoReply !== false) {
217
+ const tokenResponse = await axios_1.default.post('https://login.microsoftonline.com/botframework.com/oauth2/v2.0/token', new URLSearchParams({
218
+ grant_type: 'client_credentials',
219
+ client_id: credentials.appId,
220
+ client_secret: credentials.appPassword,
221
+ scope: 'https://api.botframework.com/.default',
222
+ }), { headers: { 'Content-Type': 'application/x-www-form-urlencoded' } });
223
+ await axios_1.default.post(`${serviceUrl}v3/conversations/${conversationId}/activities`, {
224
+ type: 'message',
225
+ text: aiResponse,
226
+ channelId: 'msteams',
227
+ conversation: { id: conversationId },
228
+ }, {
229
+ headers: {
230
+ 'Authorization': `Bearer ${tokenResponse.data.access_token}`,
231
+ 'Content-Type': 'application/json',
232
+ },
233
+ });
234
+ }
235
+ returnData.push({
236
+ json: {
237
+ userMessage,
238
+ aiResponse,
239
+ conversationId,
240
+ memorySize: ((_c = conversationMemories[conversationId]) === null || _c === void 0 ? void 0 : _c.length) || 0,
241
+ },
242
+ });
243
+ }
244
+ catch (error) {
245
+ if (this.continueOnFail()) {
246
+ returnData.push({ json: { error: error.message } });
247
+ continue;
248
+ }
249
+ throw error;
250
+ }
251
+ }
252
+ return [returnData];
253
+ }
254
+ }
255
+ exports.MsTeamsAIBot = MsTeamsAIBot;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "n8n-nodes-msteams-botframework",
3
- "version": "1.1.0",
3
+ "version": "1.2.0",
4
4
  "description": "n8n node for MS Teams Azure Bot Framework",
5
5
  "keywords": [
6
6
  "n8n-community-node-package",
@@ -39,7 +39,8 @@
39
39
  ],
40
40
  "nodes": [
41
41
  "dist/nodes/MsTeamsBotFramework/MsTeamsBotFramework.node.js",
42
- "dist/nodes/MsTeamsBotFramework/MsTeamsBotFrameworkTrigger.node.js"
42
+ "dist/nodes/MsTeamsBotFramework/MsTeamsBotFrameworkTrigger.node.js",
43
+ "dist/nodes/MsTeamsBotFramework/MsTeamsAIBot.node.js"
43
44
  ]
44
45
  },
45
46
  "devDependencies": {