@kumologica/sdk 4.0.0-beta7 → 4.0.0-beta9

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.
@@ -8,10 +8,12 @@ RED.tray = (function () {
8
8
  $('#optionSettings').removeClass('node-property-selected');
9
9
  $('#optionHelp').removeClass('node-property-selected');
10
10
  $('#optionDocumentation').removeClass('node-property-selected');
11
+ $('#optionAi').removeClass('node-property-selected');
11
12
 
12
13
  $('#properties-tab').hide();
13
14
  $('#help-tab').hide();
14
15
  $('#documentation-tab').hide();
16
+ $('#ai-tab').hide();
15
17
 
16
18
  domEl.addClass('node-property-selected');
17
19
 
@@ -25,6 +27,9 @@ RED.tray = (function () {
25
27
  case 'notes':
26
28
  $('#documentation-tab').show();
27
29
  break;
30
+ case 'ai':
31
+ $('#ai-tab').show();
32
+ break;
28
33
  }
29
34
  }
30
35
 
@@ -47,6 +52,7 @@ RED.tray = (function () {
47
52
  <div id="optionSettings" class="node-property-option node-property-selected">Settings</div>
48
53
  <div id="optionHelp" class="node-property-option">Help</div>
49
54
  <div id="optionDocumentation" class="node-property-option">Notes</div>
55
+ <!--<div id="optionAi" class="node-property-option">AI</div>-->
50
56
  </div>
51
57
  `).appendTo(el);
52
58
  }
@@ -263,6 +269,10 @@ RED.tray = (function () {
263
269
  let $this = $(e.currentTarget);
264
270
  selectOption($this, 'help');
265
271
  });
272
+ $('#optionAi').click((e) => {
273
+ let $this = $(e.currentTarget);
274
+ selectOption($this, 'ai');
275
+ });
266
276
  $('#optionDocumentation').click((e) => {
267
277
  let $this = $(e.currentTarget);
268
278
  selectOption($this, 'notes');
@@ -1,11 +1,13 @@
1
1
  RED.uiSettings = (function () {
2
2
  let testConfigStore;
3
3
  let cloudConfigStore = window.__kumologica.settings.cloudConfigStore;
4
+ let aiConfigStore = window.__kumologica.settings.aiConfigStore;
4
5
  let networkConfigStore = window.__kumologica.settings.networkConfigStore;
5
6
  let shell = window.__kumologica.electron.shell;
6
7
  let dialog = null;
7
8
  let visible = false;
8
9
  let cmInstanceCloud = null;
10
+ let cmInstanceAI = null;
9
11
  let cmInstanceEnvvars = null;
10
12
  let totalHeightAvailable;
11
13
 
@@ -48,6 +50,7 @@ RED.uiSettings = (function () {
48
50
  },
49
51
  onSave: () => {
50
52
  cloudConfigStore.writeCloudConfigFile(cmInstanceCloud.getValue());
53
+ console.log("Emitting cloud-config-changed event");
51
54
  RED.events.emit("settings:cloud-config-changed");
52
55
  },
53
56
  },
@@ -135,6 +138,42 @@ RED.uiSettings = (function () {
135
138
  RED.events.emit("settings:environment-variables-changed");
136
139
  },
137
140
  },
141
+ {
142
+ selected: false,
143
+ id: "ui-settings-ai",
144
+ label: "AI Settings",
145
+ body: {
146
+ title: "AI Settings",
147
+ subtitle: `
148
+ Configure all of your ai providers and its settings that can be used by Kumologica Designer AI assistant for flow creation and node specific settings.
149
+ <a href="https://docs.kumologica.com/docs/guide/AI.html" target="_blank">Learn more</a>`,
150
+ html: `
151
+ <div>
152
+ <div id="aifileLocationPath">
153
+ <span id="aisettingFilePath">${aiConfigStore.getAiConfigFileFullPath()}</span>
154
+ </div>
155
+ <textarea id="ai-settings-editor" rows="20">
156
+ </textarea>
157
+ </div>`,
158
+ },
159
+ onInit: () => {
160
+ // load content of config credentials file: ~/.kumologica/ai
161
+ let aiConfigContent = aiConfigStore.readAiConfigFile();
162
+ cmInstanceAI = initEditor(
163
+ "ai-settings-editor",
164
+ aiConfigContent
165
+ );
166
+ },
167
+ onSelect: () => {
168
+ cmInstanceAI.refresh();
169
+ },
170
+ onSave: () => {
171
+ aiConfigStore.writeAiConfigFile(cmInstanceAI.getValue());
172
+
173
+ console.log("Emitting ai-config-changed event");
174
+ RED.events.emit("settings:ai-config-changed");
175
+ },
176
+ },
138
177
  ];
139
178
  }
140
179
  return screen;
@@ -150,12 +189,13 @@ RED.uiSettings = (function () {
150
189
  }).appendTo("#main-container");
151
190
 
152
191
  let dialogContainer = $(
153
- `<div id="ui-settings-container" class="ui-settings-conatiner" style="display:flex; height: 100%" </div>`
192
+ `<div id="ui-settings-container" class="ui-settings-container" style="display:flex; height: 100%" </div>`
154
193
  ).appendTo(dialog);
194
+
155
195
  // Programmatically calculate the height of the ui-settings window
156
196
  totalHeightAvailable = $("#main-container").height();
157
- let offset = 74;
158
- $("#ui-settings").height(totalHeightAvailable - 200);
197
+
198
+ // $("#ui-settings").height(totalHeightAvailable - 200);
159
199
 
160
200
  // Left pannel of settings
161
201
  let optionsLeft = $(`<div id="ui-settings-left"></div>`).appendTo(
@@ -223,6 +263,7 @@ RED.uiSettings = (function () {
223
263
  $(`#${option.id}-body`).show();
224
264
  option.onSelect();
225
265
  });
266
+
226
267
  // Specific functionality
227
268
  option.onInit();
228
269
 
@@ -415,7 +456,7 @@ RED.uiSettings = (function () {
415
456
  //console.log(`lineCount: ${cmInstance.lineCount()}`)
416
457
  // Set the cursor at the end of existing content
417
458
  cmInstance.setCursor(cmInstance.lineCount(), 0);
418
- let codeMirrorHeight = totalHeightAvailable - 440;
459
+ let codeMirrorHeight = totalHeightAvailable - 340;
419
460
 
420
461
  cmInstance.setSize(null, codeMirrorHeight);
421
462
  // cmInstance.refresh();
@@ -461,7 +502,10 @@ RED.uiSettings = (function () {
461
502
  show();
462
503
  $("#ui-settings-envvars").click();
463
504
  }
464
-
505
+ function showAi() {
506
+ show();
507
+ $("#ui-settings-ai").click();
508
+ }
465
509
  function hide() {
466
510
  if (visible) {
467
511
  $("#main-container").off("click");
@@ -503,6 +547,7 @@ RED.uiSettings = (function () {
503
547
  RED.actions.add("core:settings:open", show);
504
548
  RED.actions.add("core:settings:close", hide);
505
549
  RED.actions.add("core:settings:select-environments", showEnvironments);
550
+ RED.actions.add("core:settings:select-ai", showAi);
506
551
  }
507
552
 
508
553
  return {
@@ -372,6 +372,35 @@
372
372
  width: 25px;
373
373
  }
374
374
 
375
+ /*----------------
376
+ * AI tab
377
+ * ----------------
378
+ */
379
+
380
+ #palette-aiassistant {
381
+ display: flex;
382
+ flex-direction: column;
383
+ height: 100%;
384
+ overflow-y: auto;
385
+ }
386
+
387
+ #palette-aiassistant-top {
388
+ display: flex;
389
+ margin-top: 6px;
390
+ }
391
+
392
+ #palette-aiassistant-bottom {
393
+ display: flex;
394
+ margin-top: 5px;
395
+ padding-top: 6px;
396
+ border-top: 1px solid #cccbcb;
397
+ }
398
+
399
+ #aiassist-workspace{
400
+ display: flex;
401
+ width: 100%;
402
+ }
403
+
375
404
 
376
405
 
377
406
  /*
@@ -441,5 +470,16 @@
441
470
  font-family: monospace;
442
471
  }
443
472
 
473
+ #palette-separator {
474
+ position: sticky !important;
475
+ right: 0 !important;
476
+ z-index: 11;
477
+ background: $background-color url(images/grip.png) no-repeat 50% 50%;
478
+ cursor: col-resize;
479
+ border-right: 4px solid #f3f3f3;
480
+ height: 100%;
444
481
 
445
-
482
+ &:hover {
483
+ border-right: 4px solid #2979ff;
484
+ }
485
+ }
@@ -130,3 +130,14 @@
130
130
  font-family: monospace;
131
131
  padding: 4px 15px;
132
132
  }
133
+
134
+ #aifileLocationPath {
135
+ background-color: #272822;
136
+ box-shadow: 0 4px 0 #000000;
137
+ margin-bottom: 2px;
138
+ }
139
+ #aisettingFilePath {
140
+ color: grey;
141
+ font-family: monospace;
142
+ padding: 4px 15px;
143
+ }
@@ -97,8 +97,10 @@
97
97
  </div>
98
98
 
99
99
  <div id="main-container" class="hide" tabindex="-1">
100
-
101
100
  <div id="palette" style="height: calc(100% - 23px) !important;">
101
+ <!--
102
+ <div id="palette-separator" ></div>
103
+ -->
102
104
  <img src="red/images/spin.svg" class="palette-spinner hide"/>
103
105
  <div id="palette-options">
104
106
  <div id="palette-option-nodes">
@@ -111,6 +113,9 @@
111
113
  <a id="palette-option-explorer-a" class="palette-option-button" href="#">
112
114
  <span>Explorer</span>
113
115
  </a>
116
+ <a id="palette-option-aiassist-a" class="palette-option-button" href="#">
117
+ <span>AI Assistant</span>
118
+ </a>
114
119
 
115
120
  </div>
116
121
  </div>
@@ -163,6 +168,7 @@
163
168
  <input type="text" placeholder="Filter nodes"></input>
164
169
  </div>
165
170
  <div id="palette-container" class="palette-scroll hide"></div>
171
+
166
172
  <!--
167
173
  <div id="palette-footer" style="display:flex; height: 40px; border:none; justify-content: center; overflow: hidden;">
168
174
  <div style="display: flex; width:90%;height: 32px; padding-top:3px;">
@@ -174,7 +180,39 @@
174
180
  </div>
175
181
  -->
176
182
  </div>
177
- <div id="palette-shade" class="hide"></div>
183
+
184
+ <!-- AI -->
185
+ <div id="palette-aiassistant" style="display: flex; flex-direction: column; height: 100%;">
186
+ <div id="palette-aiassistant-header" class="palette-search" style="display: flex; align-items: center; justify-content: flex-end; gap: 8px;">
187
+ <span>Chat</span>
188
+ <select id="palette-aiassistant-history-dropdown" class="palette-textinput" style="padding: 0px;">
189
+ <option value="history">History</option>
190
+ </select>
191
+ <button id="palette-aiassistant-add-btn" class="ai-header-btn" title="New Chat" style="background: none; border: none; cursor: pointer;">
192
+ <i class="fa fa-plus"></i>
193
+ </button>
194
+ <button id="palette-aiassistant-settings-btn" class="ai-header-btn" title="Settings" style="background: none; border: none; cursor: pointer;">
195
+ <i class="fa fa-gear"></i>
196
+ </button>
197
+ </div>
198
+
199
+ <div id="palette-aiassistant-chat" class="palette-scroll" style="flex: 1 1 auto;"></div>
200
+
201
+ <div id="palette-aiassistant-footer" class="palette-aiassistant-footer ">
202
+ <div id="palette-aiassistant-question" style="padding: 5px; display: flex; align-items: center;">
203
+ <textarea
204
+ id="palette-aiassistant-question-input"
205
+ placeholder="Type your question..."
206
+ style="width: 100%; margin: 0; padding: 10px; font-size: 13px; border-radius: 4px; border: 1px solid #ccc; resize: none; overflow-y: auto; line-height: 1.4; min-height: 24px; max-height: 112px;"
207
+ ></textarea>
208
+ </div>
209
+ <div style="padding: 5px;">
210
+ <select id="palette-aiassistant-model" class="palette-textinput" style="width:100%; height: 28px !important; margin: 0px; font-size: 12px;">
211
+ <option value="" disabled selected>Select an AI model</option>
212
+ </select>
213
+ </div>
214
+ </div>
215
+ </div>
178
216
  </div>
179
217
 
180
218
  <div id="workspace" class="left-shadow" style="height: calc(100% - 23px) !important;">
@@ -284,12 +322,14 @@
284
322
  <span id="footer-test-value">No TestCase selected</span>
285
323
  </div>
286
324
  </div>
325
+
287
326
  <div id="footer-ai" style="display:none">
288
327
  <div id="ask-configure-ai" style="display:none"><i class="fa fa-magic" aria-hidden="true"></i> Configure AI</div>
289
328
  <div id="generate-code-ai" style="display:none">Generate Code</div>
290
329
  <div id="explain-code-ai" style="display:none">Explain</div>
291
330
  <div id="fix-code-ai" style="display:none">Fix</div>
292
331
  </div>
332
+
293
333
  <div id="footer-services">
294
334
  <div id="footer-terminal">
295
335
  <i class="icofont-terminal" style="font-size:16px;font-weight:bold"></i>
@@ -297,7 +337,7 @@
297
337
  </div>
298
338
 
299
339
  </div>
300
-
340
+ <!--div id="palette-separator" ></div-->
301
341
  </div>
302
342
 
303
343
  <div id="full-shade" class="hide"></div>
@@ -328,3 +368,5 @@
328
368
 
329
369
  </body>
330
370
  </html>
371
+
372
+
@@ -0,0 +1,68 @@
1
+ // @ts-nocheck
2
+ const {
3
+ AbstractServerfulServer,
4
+ ConfigBuilder,
5
+ PLATFORMS,
6
+ } = require("@kumologica/runtime");
7
+
8
+ const tcpPortUsed = require("tcp-port-used");
9
+
10
+ /**
11
+ * This class will encapsulate the adminApp and FlowApp into two servers.
12
+ * It will be the one running on the development box of the user (either local or remote),
13
+ * and it will serve all the functioanlity to the Kumologica Cloud Editor.
14
+ */
15
+
16
+ class TaskServer {
17
+ /**
18
+ * @param {string} flow - FlowFile's path. It supports relative and absolute paths.
19
+ * If flow does not exist, the process will be exited.
20
+ * @param {object} params - Kumologica Runtime CLI Params to override the config and environment variables
21
+ */
22
+ constructor(flowPath, params) {
23
+ if (!flowPath) {
24
+ console.log(
25
+ '[ERROR] Flow is not defined. Make sure you pass the flow file to the constructor of FlowBuilder'
26
+ );
27
+ os.exit(1);
28
+ }
29
+
30
+ const baseConfig = ConfigBuilder.getInstance().buildConfig(
31
+ flowPath,
32
+ PLATFORMS.NODE_JS,
33
+ { noadmin: true }
34
+ );
35
+
36
+ this.config = { ...baseConfig, ...params };
37
+
38
+ console.log(`Runtime configuration:${JSON.stringify(this.config)}`);
39
+ }
40
+
41
+ async allocatePort(port) {
42
+ let inUse = await tcpPortUsed.check(port);
43
+ if (inUse) {
44
+ let newPort = port + 1;
45
+ console.log(`> Port: ${port} in use. Trying with port: ${newPort}...`);
46
+ return await this.allocatePort(newPort);
47
+ } else {
48
+ return port;
49
+ }
50
+ }
51
+
52
+ async listen() {
53
+ this.config.port = await this.allocatePort(this.config.port || 1880);
54
+ this.flowServer = new AbstractServerfulServer(this.config);
55
+
56
+ await this.flowServer.start();
57
+ this.flowServer.log.debug(`Runtime configuration:${JSON.stringify(this.config)}`);
58
+ }
59
+
60
+ async stop() {
61
+ await this.flowServer.stop();
62
+ this.flowServer.log.debug(`Server stopped`);
63
+ }
64
+ }
65
+
66
+ module.exports = {
67
+ TaskServer,
68
+ };
@@ -1,39 +0,0 @@
1
- 'use strict';
2
-
3
- const fs = require('fs');
4
- const path = require('path');
5
- const archiver = require('archiver');
6
- const BbPromise = require('bluebird');
7
- const walkDirSync = require('./walk-dir-sync');
8
-
9
- async function createZipFile(srcDirPath, outputFilePath) {
10
- const files = walkDirSync(srcDirPath).map((file) => ({
11
- input: file,
12
- output: file.replace(path.join(srcDirPath, path.sep), ''),
13
- }));
14
-
15
- return new BbPromise((resolve, reject) => {
16
- const output = fs.createWriteStream(outputFilePath);
17
- const archive = archiver('zip', {
18
- zlib: { level: 9 },
19
- });
20
-
21
- output.on('open', () => {
22
- archive.pipe(output);
23
-
24
- files.forEach((file) => {
25
- // TODO: update since this is REALLY slow
26
- if (fs.lstatSync(file.input).isFile()) {
27
- archive.append(fs.createReadStream(file.input), { name: file.output });
28
- }
29
- });
30
-
31
- archive.finalize();
32
- });
33
-
34
- archive.on('error', (err) => reject(err));
35
- output.on('close', () => resolve(outputFilePath));
36
- });
37
- }
38
-
39
- module.exports = createZipFile;
@@ -1,210 +0,0 @@
1
- RED.sidebar.ai = (function () {
2
- let tabContent;
3
- let sections;
4
- let stackContainer;
5
- let latestCodeGenerated = '[]';
6
-
7
- const INITIAL_MESSAGE = `Generate flow or node that ... (ctrl+enter to generate)`;
8
-
9
- const openAIClient = window.__kumologica.libs.openAIClient; // TODO: javier
10
-
11
- function initHTML() {
12
- tabContent = document.createElement('div');
13
- tabContent.className = 'sidebar-ai';
14
- tabContent.style = "height: 100%";
15
-
16
- RED.actions.add('core:show-ai-tab', show);
17
-
18
- stackContainer = $('<div>', {
19
- class: 'sidebar-node-info-stack',
20
- style: 'height: 100%'
21
- }).appendTo(tabContent);
22
-
23
- // Append the header
24
- let headerContentHtml = `
25
- <div class="sidebar-ai-title-wrapper">
26
- <div style="display:flex;flex-direction:column;padding-right:10px; width:100%; height: 100%">
27
- <div id="sidebar-title-wrapper" style="display:flex">
28
- <span>Generative AI</span>
29
- </div>
30
-
31
- <div id="sidebar-ai-body">
32
- <div id="sidebar-ai-error-wrapper" style="display:none">
33
- <span><b>An error occurred during processing your request</b>
34
- <span>Please check the logs for additional details or contact support for assistance</span>
35
- </div>
36
- <div id="sidebar-ai-response-wrapper" style="display:none">
37
- <div class="pt-0.5">
38
- <div class="gizmo-shadow-stroke flex h-6 w-6 items-center justify-center overflow-hidden rounded-full">
39
- <div class="relative p-1 rounded-sm h-9 w-9 text-white flex items-center justify-center" style="background-color: rgb(25, 195, 125); width: 24px; height: 24px; border-radius: 9999px; justify-content: center; align-items: center; color: white; margin-bottom: 7px">
40
- <svg width="41" height="41" viewBox="0 0 41 41" fill="none" xmlns="http://www.w3.org/2000/svg" class="icon-sm" role="img"><text x="-9999" y="-9999">ChatGPT</text><path d="M37.5324 16.8707C37.9808 15.5241 38.1363 14.0974 37.9886 12.6859C37.8409 11.2744 37.3934 9.91076 36.676 8.68622C35.6126 6.83404 33.9882 5.3676 32.0373 4.4985C30.0864 3.62941 27.9098 3.40259 25.8215 3.85078C24.8796 2.7893 23.7219 1.94125 22.4257 1.36341C21.1295 0.785575 19.7249 0.491269 18.3058 0.500197C16.1708 0.495044 14.0893 1.16803 12.3614 2.42214C10.6335 3.67624 9.34853 5.44666 8.6917 7.47815C7.30085 7.76286 5.98686 8.3414 4.8377 9.17505C3.68854 10.0087 2.73073 11.0782 2.02839 12.312C0.956464 14.1591 0.498905 16.2988 0.721698 18.4228C0.944492 20.5467 1.83612 22.5449 3.268 24.1293C2.81966 25.4759 2.66413 26.9026 2.81182 28.3141C2.95951 29.7256 3.40701 31.0892 4.12437 32.3138C5.18791 34.1659 6.8123 35.6322 8.76321 36.5013C10.7141 37.3704 12.8907 37.5973 14.9789 37.1492C15.9208 38.2107 17.0786 39.0587 18.3747 39.6366C19.6709 40.2144 21.0755 40.5087 22.4946 40.4998C24.6307 40.5054 26.7133 39.8321 28.4418 38.5772C30.1704 37.3223 31.4556 35.5506 32.1119 33.5179C33.5027 33.2332 34.8167 32.6547 35.9659 31.821C37.115 30.9874 38.0728 29.9178 38.7752 28.684C39.8458 26.8371 40.3023 24.6979 40.0789 22.5748C39.8556 20.4517 38.9639 18.4544 37.5324 16.8707ZM22.4978 37.8849C20.7443 37.8874 19.0459 37.2733 17.6994 36.1501C17.7601 36.117 17.8666 36.0586 17.936 36.0161L25.9004 31.4156C26.1003 31.3019 26.2663 31.137 26.3813 30.9378C26.4964 30.7386 26.5563 30.5124 26.5549 30.2825V19.0542L29.9213 20.998C29.9389 21.0068 29.9541 21.0198 29.9656 21.0359C29.977 21.052 29.9842 21.0707 29.9867 21.0902V30.3889C29.9842 32.375 29.1946 34.2791 27.7909 35.6841C26.3872 37.0892 24.4838 37.8806 22.4978 37.8849ZM6.39227 31.0064C5.51397 29.4888 5.19742 27.7107 5.49804 25.9832C5.55718 26.0187 5.66048 26.0818 5.73461 26.1244L13.699 30.7248C13.8975 30.8408 14.1233 30.902 14.3532 30.902C14.583 30.902 14.8088 30.8408 15.0073 30.7248L24.731 25.1103V28.9979C24.7321 29.0177 24.7283 29.0376 24.7199 29.0556C24.7115 29.0736 24.6988 29.0893 24.6829 29.1012L16.6317 33.7497C14.9096 34.7416 12.8643 35.0097 10.9447 34.4954C9.02506 33.9811 7.38785 32.7263 6.39227 31.0064ZM4.29707 13.6194C5.17156 12.0998 6.55279 10.9364 8.19885 10.3327C8.19885 10.4013 8.19491 10.5228 8.19491 10.6071V19.808C8.19351 20.0378 8.25334 20.2638 8.36823 20.4629C8.48312 20.6619 8.64893 20.8267 8.84863 20.9404L18.5723 26.5542L15.206 28.4979C15.1894 28.5089 15.1703 28.5155 15.1505 28.5173C15.1307 28.5191 15.1107 28.516 15.0924 28.5082L7.04046 23.8557C5.32135 22.8601 4.06716 21.2235 3.55289 19.3046C3.03862 17.3858 3.30624 15.3413 4.29707 13.6194ZM31.955 20.0556L22.2312 14.4411L25.5976 12.4981C25.6142 12.4872 25.6333 12.4805 25.6531 12.4787C25.6729 12.4769 25.6928 12.4801 25.7111 12.4879L33.7631 17.1364C34.9967 17.849 36.0017 18.8982 36.6606 20.1613C37.3194 21.4244 37.6047 22.849 37.4832 24.2684C37.3617 25.6878 36.8382 27.0432 35.9743 28.1759C35.1103 29.3086 33.9415 30.1717 32.6047 30.6641C32.6047 30.5947 32.6047 30.4733 32.6047 30.3889V21.188C32.6066 20.9586 32.5474 20.7328 32.4332 20.5338C32.319 20.3348 32.154 20.1698 31.955 20.0556ZM35.3055 15.0128C35.2464 14.9765 35.1431 14.9142 35.069 14.8717L27.1045 10.2712C26.906 10.1554 26.6803 10.0943 26.4504 10.0943C26.2206 10.0943 25.9948 10.1554 25.7963 10.2712L16.0726 15.8858V11.9982C16.0715 11.9783 16.0753 11.9585 16.0837 11.9405C16.0921 11.9225 16.1048 11.9068 16.1207 11.8949L24.1719 7.25025C25.4053 6.53903 26.8158 6.19376 28.2383 6.25482C29.6608 6.31589 31.0364 6.78077 32.2044 7.59508C33.3723 8.40939 34.2842 9.53945 34.8334 10.8531C35.3826 12.1667 35.5464 13.6095 35.3055 15.0128ZM14.2424 21.9419L10.8752 19.9981C10.8576 19.9893 10.8423 19.9763 10.8309 19.9602C10.8195 19.9441 10.8122 19.9254 10.8098 19.9058V10.6071C10.8107 9.18295 11.2173 7.78848 11.9819 6.58696C12.7466 5.38544 13.8377 4.42659 15.1275 3.82264C16.4173 3.21869 17.8524 2.99464 19.2649 3.1767C20.6775 3.35876 22.0089 3.93941 23.1034 4.85067C23.0427 4.88379 22.937 4.94215 22.8668 4.98473L14.9024 9.58517C14.7025 9.69878 14.5366 9.86356 14.4215 10.0626C14.3065 10.2616 14.2466 10.4877 14.2479 10.7175L14.2424 21.9419ZM16.071 17.9991L20.4018 15.4978L24.7325 17.9975V22.9985L20.4018 25.4983L16.071 22.9985V17.9991Z" fill="currentColor"></path>
41
- </svg>
42
- </div>
43
- </div>
44
- </div>
45
- <div id="sidebar-ai-response-message">
46
- <div id="sidebar-ai-response-action-wrapper">
47
- <a id="sidebar-ai-import-flow" class="sidebar-header-button" href="#" style="margin-right:5px">
48
- Import Flow
49
- </a>
50
- </div>
51
- <span id="sidebar-ai-response-flow-value"></span>
52
- </div>
53
-
54
- </div>
55
- </div>
56
-
57
- <div id="sidebar-ai-prompt">
58
- <div id="sidebar-ai-prompt-message-wrapper">
59
- <textarea id="sidebar-ai-prompt-message" rows="4" placeholder="${INITIAL_MESSAGE}" style="min-height:130px"></textarea>
60
- </div>
61
- <div id="sidebar-ai-prompt-actions">
62
- <a id="sidebar-ai-clear-btn" class="sidebar-header-button" href="#" style="margin-right:5px">
63
- Clear
64
- </a>
65
- <a id="sidebar-ai-generate-btn" class="sidebar-header-button" href="#" style="color: white !important; background: #2979ff;">
66
- Generate
67
- </a>
68
- </div>
69
- <div id="sidebar-ai-prompt-noactions" style="display:none">
70
- <a id="sidebar-ai-generating" class="sidebar-header-button" href="#" style="color: white !important; height: 24px;">
71
- <img src="red/images/spin.svg" class="palette-spinner" style="height: 16px; width: 57px" />
72
- </a>
73
- </div>
74
- </div>
75
-
76
- <div id="ai-not-initialized" style="display:none">
77
- <div style="display:flex; flex-direction: column; align-items: center; margin-top: 150px">
78
- <div><i class="fa fa-magic" style="font-size: 56px; color: lightgrey;"></i></div>
79
- <div style="font-weight: bold; font-size: 22px; margin-top: 15px;color: grey;">OpenAI API Key not found</div>
80
- <div style="margin-top: 5px; color: grey;font-weight: 400; font-size: 13px;">To use this feature, you need to add your OpenAI key. </div>
81
- <div style="margin-top: 10px"><span id="add-ai-key" style="font-weight: 500;font-size: 12px; color: #2979ff">Add key</span></div>
82
- </div>
83
- </div>
84
- </div>
85
- </div>
86
- `;
87
-
88
- $(headerContentHtml).appendTo(stackContainer);
89
-
90
- RED.sidebar.addTab({
91
- id: 'ai',
92
- label: 'Gen AI',
93
- name: 'Generative AI',
94
- iconClass: 'fa fa-magic',
95
- action: 'core:show-git-tab',
96
- content: tabContent,
97
- pinned: true,
98
- enableOnEdit: true,
99
- onchange: refreshTab,
100
- onsame: onsame
101
- })
102
- }
103
- function displayGeneratedCode(code) {
104
- // update the latest code generated
105
- latestCodeGenerated = JSON.stringify(code, null, 2);
106
-
107
- $('#sidebar-ai-response-wrapper').show();
108
- let $response = $('#sidebar-ai-response-flow-value');
109
-
110
- RED.utils
111
- .createObjectElement({ "code": code }, {
112
- key: /*true*/ null,
113
- typeHint: 'object',
114
- hideKey: false,
115
- path: '',
116
- sourceId: 0,
117
- rootPath: '',
118
- })
119
- .appendTo($response);
120
- }
121
-
122
- function changeStateToGenerating() {
123
- latestCodeGenerated = '[]';
124
- // Clear the previous response if required
125
- $('#sidebar-ai-response-wrapper').hide();
126
- $('#sidebar-ai-response-flow-value').empty();
127
-
128
- // Control buttons
129
- $('#sidebar-ai-prompt-actions').hide();
130
- $('#sidebar-ai-prompt-noactions').show();
131
- }
132
-
133
- function changeStateToGenerated() {
134
- $('#sidebar-ai-prompt-actions').show();
135
- $('#sidebar-ai-prompt-noactions').hide();
136
-
137
- }
138
-
139
- function initJS() {
140
- // Generate action
141
- $('#sidebar-ai-generate-btn').click(async e => {
142
- console.log('Asking AI to generate code...');
143
- // Change the icon to loading
144
- changeStateToGenerating();
145
-
146
- let userPrompt = $('#sidebar-ai-prompt-message').val();
147
- if (userPrompt) {
148
- let response = await openAIClient.t2c(userPrompt);
149
- displayGeneratedCode(response);
150
- changeStateToGenerated();
151
- } else {
152
- changeStateToGenerated();
153
- }
154
-
155
-
156
- })
157
- // Trigger generate action from ctrl+click
158
- $('#sidebar-ai-prompt-message').keydown(e => {
159
- if ((e.ctrlKey || e.metaKey) && (e.keyCode == 13 || e.keyCode == 10)) {
160
- $('#sidebar-ai-generate-btn').click();
161
- }
162
- })
163
-
164
- // Clear button
165
- $('#sidebar-ai-clear-btn').click(async e => {
166
- // Clear all messages
167
- $('#sidebar-ai-response-wrapper').hide();
168
- $('#sidebar-ai-response-flow-value').empty();
169
-
170
- // Clear user prompt
171
- $('#sidebar-ai-prompt-message').val('');
172
- });
173
-
174
- $('#sidebar-ai-import-flow').click(e => {
175
- // Copy code to clipboard
176
- const { clipboard } = window.__kumologica.electron;
177
- clipboard.writeText(latestCodeGenerated);
178
-
179
- // Open the import flow dialog
180
- RED.actions.invoke('core:show-import-dialog')
181
-
182
- // automatically trigger the import-from-clipboard option
183
- setTimeout(() => { $('#import-from-clipboard').click() }, 100);
184
- })
185
- }
186
-
187
- function init() {
188
- initHTML();
189
- initJS();
190
- }
191
-
192
- function show() {
193
- RED.sidebar.show('ai');
194
- }
195
-
196
- function refreshTab(silently) {
197
- if (!silently) {
198
- RED.actions.invoke('core:show-sidebar');
199
- }
200
- }
201
-
202
- function onsame() {
203
- RED.actions.invoke('core:hide-sidebar');
204
- }
205
-
206
- return {
207
- init: init,
208
- show: show,
209
- }
210
- })();