@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.
- package/LICENSE +170 -0
- package/cli/commands/export-commands/cloudformation.js +1 -1
- package/cli/commands/run.js +68 -29
- package/cli/commands/test.js +1 -1
- package/package.json +12 -9
- package/src/app/lib/ai/chatai.js +179 -0
- package/src/app/lib/stores/settings-ai-store.js +194 -0
- package/src/app/preload.js +12 -4
- package/src/app/ui/editor-client/public/red/red.js +836 -216
- package/src/app/ui/editor-client/public/red/red.min.js +2 -2
- package/src/app/ui/editor-client/public/red/style.min.css +1 -1
- package/src/app/ui/editor-client/src/js/red.js +1 -0
- package/src/app/ui/editor-client/src/js/ui/aitab.js +90 -0
- package/src/app/ui/editor-client/src/js/ui/editor.js +115 -1
- package/src/app/ui/editor-client/src/js/ui/palette-aiassistant.js +527 -0
- package/src/app/ui/editor-client/src/js/ui/palette.js +132 -0
- package/src/app/ui/editor-client/src/js/ui/tray.js +10 -0
- package/src/app/ui/editor-client/src/js/ui/ui-settings.js +50 -5
- package/src/app/ui/editor-client/src/sass/palette.scss +41 -1
- package/src/app/ui/editor-client/src/sass/ui-settings.scss +11 -0
- package/src/app/ui/editor-client/templates/index.mst +45 -3
- package/src/server/TaskServer.js +68 -0
- package/cli/utils/fs/create-zip-file.js +0 -39
- package/src/app/ui/editor-client/src/js/ui/tab-ai.js +0 -210
|
@@ -0,0 +1,527 @@
|
|
|
1
|
+
RED.palette.aiassistant = (function () {
|
|
2
|
+
|
|
3
|
+
let conversation = [];
|
|
4
|
+
|
|
5
|
+
// returns array of sections defined in the ai config file (all ai configurations)
|
|
6
|
+
function getSettingsFromConfig() {
|
|
7
|
+
const aiConfigStore = window.__kumologica.settings.aiConfigStore;
|
|
8
|
+
const aiConfig = aiConfigStore.getSections();
|
|
9
|
+
return aiConfig;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
// returns defined provider for the given alias
|
|
13
|
+
function getProvider(alias) {
|
|
14
|
+
const aiConfigStore = window.__kumologica.settings.aiConfigStore;
|
|
15
|
+
return {
|
|
16
|
+
provider: aiConfigStore.getAliasInfo(alias, 'provider'),
|
|
17
|
+
model: aiConfigStore.getAliasInfo(alias, 'model'),
|
|
18
|
+
api_key: aiConfigStore.getAliasInfo(alias, 'api_key'),
|
|
19
|
+
temperature: parseFloat(aiConfigStore.getAliasInfo(alias, 'temperature')) || 0.7,
|
|
20
|
+
max_tokens: parseInt(aiConfigStore.getAliasInfo(alias, 'max_tokens')) || 4096,
|
|
21
|
+
systemPrompt: aiConfigStore.getPrompt() || null,
|
|
22
|
+
region: aiConfigStore.getAliasInfo(alias, 'region') || 'us-east-1', // for AWS Bedrock
|
|
23
|
+
profile: aiConfigStore.getAliasInfo(alias, 'profile') || null, // for AWS Bedrock
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function applyCode(code) {
|
|
28
|
+
try {
|
|
29
|
+
// First, escape newlines and tabs within quoted strings
|
|
30
|
+
code = code.replace(/("(?:[^"\\]|\\.)*")/g, function(match) {
|
|
31
|
+
// Process content inside quotes
|
|
32
|
+
return match
|
|
33
|
+
.replace(/\n/g, '\\n') // Replace actual newlines with \n
|
|
34
|
+
.replace(/\r/g, '\\r') // Replace carriage returns with \r
|
|
35
|
+
.replace(/\t/g, '\\t'); // Replace tabs with \t
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
// Validate that the code is a valid JSON array (nodes)
|
|
39
|
+
if (!/^\[[\s\S]*\]$/m.test(code.trim())) {
|
|
40
|
+
alert('Error: Code is not a valid JSON array format');
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
var nodes = JSON.parse(code.trim());
|
|
45
|
+
|
|
46
|
+
// Validate that it contains valid node objects
|
|
47
|
+
for (var i = 0; i < nodes.length; i++) {
|
|
48
|
+
if (typeof nodes[i] !== "object") {
|
|
49
|
+
alert('Error: Input not a valid flow');
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
if (!nodes[i].hasOwnProperty('id')) {
|
|
53
|
+
alert('Error: Input not a valid flow');
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
if (!nodes[i].hasOwnProperty('type')) {
|
|
57
|
+
alert('Error: Input not a valid flow');
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// If validation passes, import the nodes into the desktop
|
|
63
|
+
RED.view.importNodes(code.trim(), false);
|
|
64
|
+
RED.notify('Nodes imported from AI Assistant', {id: "ai-assistant-import"});
|
|
65
|
+
|
|
66
|
+
} catch(err) {
|
|
67
|
+
alert('Error applying code: ' + err.toString());
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
function convertMarkdownToHtml(text) {
|
|
71
|
+
// Escape HTML special characters first
|
|
72
|
+
text = $('<div>').text(text).html();
|
|
73
|
+
|
|
74
|
+
// Convert headings: # text, ## text, etc.
|
|
75
|
+
text = text.replace(/^### (.+)$/gm, '<h5 style="font-size:14px;font-weight:bold;margin:8px 0;font-family:\'JetBrains Mono\', \'Fira Code\', \'Courier New\', monospace;">$1</h5>');
|
|
76
|
+
text = text.replace(/^## (.+)$/gm, '<h4 style="font-size:16px;font-weight:bold;margin:8px 0;font-family:\'JetBrains Mono\', \'Fira Code\', \'Courier New\', monospace;">$1</h4>');
|
|
77
|
+
text = text.replace(/^# (.+)$/gm, '<h3 style="font-size:18px;font-weight:bold;margin:8px 0;font-family:\'JetBrains Mono\', \'Fira Code\', \'Courier New\', monospace;">$1</h3>');
|
|
78
|
+
|
|
79
|
+
// Convert bold: **text** or __text__
|
|
80
|
+
text = text.replace(/\*\*(.+?)\*\*/g, '<strong>$1</strong>');
|
|
81
|
+
text = text.replace(/__(.+?)__/g, '<strong>$1</strong>');
|
|
82
|
+
|
|
83
|
+
// Convert italic: *text* or _text_
|
|
84
|
+
text = text.replace(/\*(.+?)\*/g, '<em>$1</em>');
|
|
85
|
+
text = text.replace(/_(.+?)_/g, '<em>$1</em>');
|
|
86
|
+
|
|
87
|
+
// Convert inline code: `text`
|
|
88
|
+
text = text.replace(/`(.+?)`/g, '<code style="background:#f0f0f0;color:#d63384;padding:2px 6px;border-radius:3px;font-family:\'JetBrains Mono\', \'Fira Code\', \'Courier New\', monospace;font-size:14px;font-weight:bold;">$1</code>');
|
|
89
|
+
|
|
90
|
+
// Convert newlines to <br> tags
|
|
91
|
+
text = text.replace(/\n/g, '<br>');
|
|
92
|
+
|
|
93
|
+
// Convert unordered lists: - item or * item
|
|
94
|
+
text = text.replace(/^[\*\-]\s+(.+)$/gm, '<li>$1</li>');
|
|
95
|
+
text = text.replace(/(<li>.*<\/li>)/s, '<ul style="margin:8px 0;padding-left:20px;">$1</ul>');
|
|
96
|
+
|
|
97
|
+
return text;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function getLanguageStyles(language) {
|
|
101
|
+
const styles = {
|
|
102
|
+
javascript: { keywords: '#569cd6', string: '#ce9178', comment: '#6a9955', function: '#dcdcaa', number: '#b5cea8' },
|
|
103
|
+
python: { keywords: '#569cd6', string: '#ce9178', comment: '#6a9955', function: '#dcdcaa', number: '#b5cea8' },
|
|
104
|
+
sql: { keywords: '#569cd6', string: '#ce9178', comment: '#6a9955', function: '#dcdcaa', number: '#b5cea8' },
|
|
105
|
+
html: { keywords: '#569cd6', string: '#ce9178', comment: '#6a9955', tag: '#4ec9b0', number: '#b5cea8' },
|
|
106
|
+
css: { keywords: '#569cd6', string: '#ce9178', comment: '#6a9955', property: '#9cdcfe', number: '#b5cea8' },
|
|
107
|
+
java: { keywords: '#569cd6', string: '#ce9178', comment: '#6a9955', function: '#dcdcaa', number: '#b5cea8' },
|
|
108
|
+
csharp: { keywords: '#569cd6', string: '#ce9178', comment: '#6a9955', function: '#dcdcaa', number: '#b5cea8' },
|
|
109
|
+
go: { keywords: '#569cd6', string: '#ce9178', comment: '#6a9955', function: '#dcdcaa', number: '#b5cea8' },
|
|
110
|
+
rust: { keywords: '#569cd6', string: '#ce9178', comment: '#6a9955', function: '#dcdcaa', number: '#b5cea8' },
|
|
111
|
+
json: { key: '#9cdcfe', string: '#ce9178', number: '#b5cea8', boolean: '#569cd6', null: '#569cd6' },
|
|
112
|
+
xml: { tag: '#569cd6', attribute: '#9cdcfe', string: '#ce9178', comment: '#6a9955' },
|
|
113
|
+
};
|
|
114
|
+
return styles[language] || styles.javascript;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function highlightCode(code, language) {
|
|
118
|
+
// Escape HTML FIRST - before any highlighting
|
|
119
|
+
let highlighted = $('<div>').text(code).html();
|
|
120
|
+
|
|
121
|
+
const keywordPatterns = {
|
|
122
|
+
javascript: /\b(function|const|let|var|if|else|for|while|return|class|async|await|import|export|try|catch|new|throw)\b/g,
|
|
123
|
+
python: /\b(def|class|if|else|for|while|return|import|from|async|await|with|try|except)\b/g,
|
|
124
|
+
sql: /\b(SELECT|FROM|WHERE|INSERT|UPDATE|DELETE|CREATE|DROP|ALTER|JOIN|ON|GROUP|ORDER|BY|HAVING)\b/gi,
|
|
125
|
+
html: /(<\/?[a-z][a-z0-9]*\b[^>]*>)/gi,
|
|
126
|
+
css: /([a-z-]+)(\s*:)/gi,
|
|
127
|
+
java: /\b(public|private|class|interface|void|int|String|if|else|for|while|return|static|final)\b/g,
|
|
128
|
+
csharp: /\b(public|private|class|interface|void|int|string|if|else|for|while|return|static|async|await)\b/g,
|
|
129
|
+
go: /\b(func|package|import|const|var|if|else|for|return|type|interface|struct)\b/g,
|
|
130
|
+
rust: /\b(fn|let|mut|const|struct|impl|trait|if|else|for|while|return|pub|async|await)\b/g,
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
const commentPatterns = {
|
|
134
|
+
javascript: ///.+$/gm,
|
|
135
|
+
python: /#.+$/gm,
|
|
136
|
+
sql: /--.+$/gm,
|
|
137
|
+
java: ///.+$/gm,
|
|
138
|
+
csharp: ///.+$/gm,
|
|
139
|
+
go: ///.+$/gm,
|
|
140
|
+
rust: ///.+$/gm,
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
const stringPattern = /(['"`])(.*?)\1/g;
|
|
144
|
+
const numberPattern = /\b(\d+\.?\d*)\b/g;
|
|
145
|
+
|
|
146
|
+
const styles = getLanguageStyles(language);
|
|
147
|
+
|
|
148
|
+
// JSON specific highlighting
|
|
149
|
+
if (language === 'json') {
|
|
150
|
+
highlighted = highlighted.replace(/"([^"]+)"\s*:/g,
|
|
151
|
+
`<span style="color:${styles.key};">"$1"</span>:`);
|
|
152
|
+
highlighted = highlighted.replace(/:\s*"([^"]*)"/g,
|
|
153
|
+
`:<span style="color:${styles.string};"> "$1"</span>`);
|
|
154
|
+
highlighted = highlighted.replace(/:\s*(\d+\.?\d*)/g,
|
|
155
|
+
`:<span style="color:${styles.number};"> $1</span>`);
|
|
156
|
+
highlighted = highlighted.replace(/\b(true|false|null)\b/g,
|
|
157
|
+
`<span style="color:${styles.boolean};">$1</span>`);
|
|
158
|
+
return highlighted;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
// XML specific highlighting
|
|
162
|
+
if (language === 'xml') {
|
|
163
|
+
highlighted = highlighted.replace(/(<!--[\s\S]*?-->)/g,
|
|
164
|
+
match => `<span style="color:${styles.comment};">${match}</span>`);
|
|
165
|
+
highlighted = highlighted.replace(/(<\/?)\b([a-zA-Z_][\w.-]*)\b/g,
|
|
166
|
+
`<span style="color:${styles.tag};">$1$2</span>`);
|
|
167
|
+
highlighted = highlighted.replace(/="([^"]*)"/g,
|
|
168
|
+
`="<span style="color:${styles.string};">$1</span>"`);
|
|
169
|
+
return highlighted;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
// For all other languages: apply highlighting in correct order
|
|
173
|
+
// 1. Comments FIRST (before strings, keywords, numbers)
|
|
174
|
+
if (commentPatterns[language]) {
|
|
175
|
+
highlighted = highlighted.replace(commentPatterns[language],
|
|
176
|
+
match => `<span style="color:${styles.comment};font-style:italic;">${match}</span>`);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
// 2. Strings (to protect them from keyword/number replacements)
|
|
180
|
+
highlighted = highlighted.replace(stringPattern,
|
|
181
|
+
match => `<span style="color:${styles.string};">${match}</span>`);
|
|
182
|
+
|
|
183
|
+
// 3. Numbers (after strings to avoid conflicts)
|
|
184
|
+
highlighted = highlighted.replace(numberPattern,
|
|
185
|
+
match => `<span style="color:${styles.number};">${match}</span>`);
|
|
186
|
+
|
|
187
|
+
// 4. Keywords last (lowest priority)
|
|
188
|
+
if (keywordPatterns[language]) {
|
|
189
|
+
highlighted = highlighted.replace(keywordPatterns[language],
|
|
190
|
+
match => `<span style="color:${styles.keywords};font-weight:bold;">${match}</span>`);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
return highlighted;
|
|
194
|
+
}
|
|
195
|
+
async function chatWithAi(conversation, question) {
|
|
196
|
+
const $chat = $('#palette-aiassistant-chat');
|
|
197
|
+
|
|
198
|
+
// Add user question to chat (right aligned)
|
|
199
|
+
const userMsgHtml = `
|
|
200
|
+
<div class="ai-chat-msg ai-chat-user" style="text-align: right; margin: 5px;">
|
|
201
|
+
<span style="background: #f0f1f1; color: #333; padding: 8px 12px; border-radius: 4px; display: inline-block; max-width: 80%; text-align: left; user-select: text; cursor: text;">${$('<div>').text(question).html()}</span>
|
|
202
|
+
</div>
|
|
203
|
+
`;
|
|
204
|
+
$chat.append(userMsgHtml);
|
|
205
|
+
|
|
206
|
+
// Auto-scroll to bottom after user message
|
|
207
|
+
$chat.scrollTop($chat[0].scrollHeight);
|
|
208
|
+
|
|
209
|
+
// Add question to conversation history
|
|
210
|
+
conversation.push({ role: 'user', content: question });
|
|
211
|
+
|
|
212
|
+
// Get selected model and provider (if needed)
|
|
213
|
+
const model = $('#palette-aiassistant-model').val();
|
|
214
|
+
const provider = getProvider(model);
|
|
215
|
+
|
|
216
|
+
const options = {
|
|
217
|
+
provider: provider.provider,
|
|
218
|
+
model: provider.model,
|
|
219
|
+
apiKey: provider.api_key,
|
|
220
|
+
messages: conversation, // Include full conversation history
|
|
221
|
+
systemPrompt: provider.systemPrompt,
|
|
222
|
+
region: provider.region,
|
|
223
|
+
profile: provider.profile,
|
|
224
|
+
params: { temperature: provider.temperature, maxTokens: provider.max_tokens }
|
|
225
|
+
};
|
|
226
|
+
|
|
227
|
+
try {
|
|
228
|
+
console.log("Calling chatAi with options:", options);
|
|
229
|
+
const res = await window.__kumologica.libs.chatAi(options);
|
|
230
|
+
|
|
231
|
+
console.log("response:", JSON.stringify(res));
|
|
232
|
+
|
|
233
|
+
|
|
234
|
+
let responseHtml = res.content || '[No response]';
|
|
235
|
+
responseHtml = responseHtml.replace(/\\n/g, '\n')
|
|
236
|
+
.replace(/\\t/g, '\t')
|
|
237
|
+
.replace(/\\r/g, '\r')
|
|
238
|
+
.replace(/\\\`/g, '`');
|
|
239
|
+
|
|
240
|
+
console.log("Unescaped responseHtml:", responseHtml);
|
|
241
|
+
|
|
242
|
+
let assistantMsgHtml = ''; // INITIALIZE HERE
|
|
243
|
+
const codeBlockRegex = /```(\w+)?\s*([\s\S]*?)(?:```|$)/g;
|
|
244
|
+
|
|
245
|
+
let lastIndex = 0;
|
|
246
|
+
let match;
|
|
247
|
+
let hasCode = false;
|
|
248
|
+
|
|
249
|
+
// Process all code blocks and surrounding text
|
|
250
|
+
while ((match = codeBlockRegex.exec(responseHtml)) !== null) {
|
|
251
|
+
hasCode = true;
|
|
252
|
+
// Text before code block
|
|
253
|
+
const textBefore = responseHtml.slice(lastIndex, match.index);
|
|
254
|
+
if (textBefore.trim()) {
|
|
255
|
+
const formattedText = convertMarkdownToHtml(textBefore);
|
|
256
|
+
assistantMsgHtml += `
|
|
257
|
+
<div class="ai-chat-msg ai-chat-assistant" style="text-align: left; margin: 8px 0;">
|
|
258
|
+
<span style="color: #222; padding: 8px 12px; border-radius: 5px; display: inline-block; max-width: 100%;">${formattedText}</span>
|
|
259
|
+
</div>
|
|
260
|
+
`;
|
|
261
|
+
}
|
|
262
|
+
// Code block
|
|
263
|
+
const language = match[1] || 'plaintext';
|
|
264
|
+
const code = match[2].trim();
|
|
265
|
+
const codeLines = code.split('\n').map(line => line.trimEnd()); // Trim trailing whitespace from each line
|
|
266
|
+
const displayedCode = codeLines.join('\n');
|
|
267
|
+
const highlightedCode = language === 'plaintext' ? $('<div>').text(displayedCode).html() : highlightCode(displayedCode, language);
|
|
268
|
+
const codeId = `ai-code-${Date.now()}-${Math.floor(Math.random()*10000)}`;
|
|
269
|
+
|
|
270
|
+
assistantMsgHtml += `
|
|
271
|
+
<div class="ai-chat-msg ai-chat-assistant" style="text-align: left; margin: 8px 0;">
|
|
272
|
+
<span style="color: #222; padding: 8px 12px; border-radius: 5px; display: inline-block; max-width: 85%; margin: 5px; border: 1px solid darkgrey;">
|
|
273
|
+
<div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 8px;">
|
|
274
|
+
<div style="font-weight: bold;">${language}:</div>
|
|
275
|
+
<div style="display: flex; gap: 8px; align-items: center;">
|
|
276
|
+
<button class="ai-apply-btn" data-full-code="${btoa(code)}" style="background: none; border: none; cursor: pointer; padding: 4px 8px; color: #000000; transition: opacity 0.2s;" title="Apply code">
|
|
277
|
+
<i class="fa fa-refresh" style="font-size: 14px;"></i>
|
|
278
|
+
</button>
|
|
279
|
+
<button class="ai-copy-btn" data-full-code="${btoa(code)}" style="background: none; border: none; cursor: pointer; padding: 4px 8px; color: #000000; transition: opacity 0.2s;" title="Copy code">
|
|
280
|
+
<i class="fa fa-clone" style="font-size: 14px;"></i>
|
|
281
|
+
</button>
|
|
282
|
+
</div>
|
|
283
|
+
</div>
|
|
284
|
+
<pre style="
|
|
285
|
+
margin: 0;
|
|
286
|
+
padding: 5px 0;
|
|
287
|
+
background: #1e1e1e;
|
|
288
|
+
color: #d4d4d4;
|
|
289
|
+
border-radius: 8px;
|
|
290
|
+
overflow: auto;
|
|
291
|
+
max-height: 500px;
|
|
292
|
+
white-space: pre;
|
|
293
|
+
scrollbar-width: thin;
|
|
294
|
+
scrollbar-color: #555 #2d2d2d;
|
|
295
|
+
user-select: text;
|
|
296
|
+
cursor: text;
|
|
297
|
+
">
|
|
298
|
+
<code id="${codeId}"
|
|
299
|
+
class="language-${language}"
|
|
300
|
+
style="
|
|
301
|
+
font-size: 12px !important;
|
|
302
|
+
line-height: 1.45;
|
|
303
|
+
font-family: 'JetBrains Mono', 'Fira Code', 'Courier New', monospace;
|
|
304
|
+
display: block;
|
|
305
|
+
white-space: pre !important;
|
|
306
|
+
margin: 0 !important;
|
|
307
|
+
padding: 0 12px !important;
|
|
308
|
+
overflow-x: auto;
|
|
309
|
+
min-width: 100%;
|
|
310
|
+
box-sizing: border-box;
|
|
311
|
+
tab-size: 2;
|
|
312
|
+
user-select: text;
|
|
313
|
+
">
|
|
314
|
+
${highlightedCode}
|
|
315
|
+
</code>
|
|
316
|
+
</pre>
|
|
317
|
+
</span>
|
|
318
|
+
</div>
|
|
319
|
+
`;
|
|
320
|
+
|
|
321
|
+
lastIndex = codeBlockRegex.lastIndex;
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
// Text after last code block
|
|
325
|
+
const textAfter = responseHtml.slice(lastIndex);
|
|
326
|
+
if (textAfter.trim()) {
|
|
327
|
+
const formattedText = convertMarkdownToHtml(textAfter);
|
|
328
|
+
assistantMsgHtml += `
|
|
329
|
+
<div class="ai-chat-msg ai-chat-assistant" style="text-align: left; margin: 8px 0;">
|
|
330
|
+
<span style="color: #222; padding: 8px 12px; border-radius: 5px; display: inline-block; max-width: 100%;">${formattedText}</span>
|
|
331
|
+
</div>
|
|
332
|
+
`;
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
if (!hasCode) {
|
|
336
|
+
console.log("No code blocks detected in response.");
|
|
337
|
+
const formattedText = convertMarkdownToHtml(responseHtml);
|
|
338
|
+
assistantMsgHtml = `
|
|
339
|
+
<div class="ai-chat-msg ai-chat-assistant" style="text-align: left; margin: 8px 0;">
|
|
340
|
+
<span style="color: #222; padding: 8px 12px; border-radius: 5px; display: inline-block; max-width: 100%;">${formattedText}</span>
|
|
341
|
+
</div>
|
|
342
|
+
`;
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
console.log("Appending assistant message HTML:", assistantMsgHtml);
|
|
346
|
+
$chat.append(assistantMsgHtml);
|
|
347
|
+
|
|
348
|
+
// Auto-scroll to bottom after assistant message with 250ms delay
|
|
349
|
+
setTimeout(() => {
|
|
350
|
+
$chat.scrollTop($chat[0].scrollHeight);
|
|
351
|
+
}, 250);
|
|
352
|
+
|
|
353
|
+
// Check if response was truncated
|
|
354
|
+
if (res.finishReason === 'MAX_TOKENS') {
|
|
355
|
+
const warningHtml = `
|
|
356
|
+
<div style="background-color: #fff3cd; border: 1px solid #ffc107; color: #856404; padding: 12px; border-radius: 4px; margin: 8px 0; text-align: left;">
|
|
357
|
+
<strong>⚠️ Response Truncated:</strong> The response was cut off due to token limit. Consider rephrasing your question or breaking it into smaller parts.
|
|
358
|
+
</div>
|
|
359
|
+
`;
|
|
360
|
+
$chat.append(warningHtml);
|
|
361
|
+
|
|
362
|
+
// Auto-scroll again after warning
|
|
363
|
+
$chat.scrollTop($chat[0].scrollHeight);
|
|
364
|
+
}
|
|
365
|
+
// Add event listeners for copy and apply buttons AFTER appending to DOM
|
|
366
|
+
// Use event delegation to avoid duplicate listeners
|
|
367
|
+
$chat.off('click', '.ai-copy-btn').on('click', '.ai-copy-btn', function() {
|
|
368
|
+
const btn = $(this);
|
|
369
|
+
const encodedCode = btn.data('fullCode');
|
|
370
|
+
const fullCode = atob(encodedCode);
|
|
371
|
+
|
|
372
|
+
navigator.clipboard.writeText(fullCode).then(() => {
|
|
373
|
+
const icon = btn.find('i');
|
|
374
|
+
icon.removeClass('fa-clone').addClass('fa-check');
|
|
375
|
+
setTimeout(() => icon.removeClass('fa-check').addClass('fa-clone'), 1000);
|
|
376
|
+
}).catch(err => console.error('Copy failed:', err));
|
|
377
|
+
});
|
|
378
|
+
|
|
379
|
+
// Add event listeners for apply button
|
|
380
|
+
$chat.off('click', '.ai-apply-btn').on('click', '.ai-apply-btn', function() {
|
|
381
|
+
const btn = $(this);
|
|
382
|
+
const encodedCode = btn.data('fullCode');
|
|
383
|
+
const fullCode = atob(encodedCode);
|
|
384
|
+
applyCode(fullCode);
|
|
385
|
+
|
|
386
|
+
const icon = btn.find('i');
|
|
387
|
+
icon.removeClass('fa-refresh').addClass('fa-check');
|
|
388
|
+
setTimeout(() => icon.removeClass('fa-check').addClass('fa-refresh'), 1000);
|
|
389
|
+
});
|
|
390
|
+
|
|
391
|
+
// Add assistant response to conversation history
|
|
392
|
+
conversation.push({ role: 'assistant', content: responseHtml });
|
|
393
|
+
} catch(err) {
|
|
394
|
+
$chat.append(`<div style="color:red; text-align:left;">Error: ${err.message}</div>`);
|
|
395
|
+
|
|
396
|
+
// Auto-scroll on error
|
|
397
|
+
$chat.scrollTop($chat[0].scrollHeight);
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
function initializeModelSelect() {
|
|
402
|
+
|
|
403
|
+
console.log("Initializing AI Assistant model select");
|
|
404
|
+
const $select = $('#palette-aiassistant-model');
|
|
405
|
+
$select.empty(); // Clear everything
|
|
406
|
+
|
|
407
|
+
const sections = getSettingsFromConfig();
|
|
408
|
+
|
|
409
|
+
if (sections.length === 0) {
|
|
410
|
+
// No models defined → show placeholder
|
|
411
|
+
$select.append(
|
|
412
|
+
$('<option>', {
|
|
413
|
+
value: '-',
|
|
414
|
+
text: 'No values defined, configure first',
|
|
415
|
+
disabled: true,
|
|
416
|
+
selected: true
|
|
417
|
+
})
|
|
418
|
+
);
|
|
419
|
+
} else {
|
|
420
|
+
// Add each section as a selectable option
|
|
421
|
+
$.each(sections, function(i, section) {
|
|
422
|
+
console.log("Adding model option:", section);
|
|
423
|
+
$select.append(
|
|
424
|
+
$('<option>', {
|
|
425
|
+
value: section,
|
|
426
|
+
text: section
|
|
427
|
+
})
|
|
428
|
+
);
|
|
429
|
+
});
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
|
|
434
|
+
function setupAiAssistantFooterActions() {
|
|
435
|
+
const $chat = $('#palette-aiassistant-chat');
|
|
436
|
+
const $questionInput = $('#palette-aiassistant-question-input');
|
|
437
|
+
const $newBtn = $('#palette-aiassistant-add-btn');
|
|
438
|
+
const $settingsBtn = $('#palette-aiassistant-settings-btn');
|
|
439
|
+
const $modelSelect = $('#palette-aiassistant-model');
|
|
440
|
+
|
|
441
|
+
// Handle "New" button - clear conversation and chat
|
|
442
|
+
$newBtn.on('click', function() {
|
|
443
|
+
conversation = []; // Clear conversation history
|
|
444
|
+
$chat.empty(); // Clear chat display
|
|
445
|
+
$questionInput.val(''); // Clear input field
|
|
446
|
+
$questionInput.focus(); // Focus on input field
|
|
447
|
+
console.log("New conversation started");
|
|
448
|
+
});
|
|
449
|
+
|
|
450
|
+
$settingsBtn.on('click', (e) => {
|
|
451
|
+
e.stopPropagation();
|
|
452
|
+
RED.actions.invoke('core:settings:select-ai');
|
|
453
|
+
});
|
|
454
|
+
|
|
455
|
+
// Handle textarea auto-expand on input
|
|
456
|
+
$questionInput.on('input', function() {
|
|
457
|
+
// Reset height to auto to get the correct scrollHeight
|
|
458
|
+
this.style.height = 'auto';
|
|
459
|
+
|
|
460
|
+
// Calculate new height (line-height is 1.4 * font-size)
|
|
461
|
+
// Assuming default font-size, each line is approximately 24px
|
|
462
|
+
const newHeight = Math.min(this.scrollHeight, 112); // 112px = 5 lines * ~24px
|
|
463
|
+
this.style.height = newHeight + 'px';
|
|
464
|
+
});
|
|
465
|
+
|
|
466
|
+
// Allow pressing Enter to send message, Shift+Enter for new line
|
|
467
|
+
$questionInput.on('keypress', function(e) {
|
|
468
|
+
if (e.which === 13) { // Enter key
|
|
469
|
+
if (e.shiftKey) {
|
|
470
|
+
// Shift+Enter: Allow new line (let browser handle it naturally)
|
|
471
|
+
return true;
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
// Regular Enter: Send message
|
|
475
|
+
const model = $modelSelect.val();
|
|
476
|
+
|
|
477
|
+
// Check if model is selected
|
|
478
|
+
if (!model || model === '') {
|
|
479
|
+
$chat.append(`
|
|
480
|
+
<div style="background-color: #f8d7da; border: 1px solid #f5c6cb; color: #721c24; padding: 12px; border-radius: 4px; margin: 8px 0; text-align: left;">
|
|
481
|
+
<strong>⚠️ Model Required:</strong> Please select an AI model from the dropdown before asking a question.
|
|
482
|
+
</div>
|
|
483
|
+
`);
|
|
484
|
+
$modelSelect.focus();
|
|
485
|
+
return false;
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
const question = $questionInput.val().trim();
|
|
489
|
+
if (question) {
|
|
490
|
+
chatWithAi(conversation, question);
|
|
491
|
+
$questionInput.val(''); // Clear textarea
|
|
492
|
+
$questionInput.css('height', '48px'); // Reset height to single line
|
|
493
|
+
$questionInput.focus();
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
return false; // Prevent default Enter behavior
|
|
497
|
+
}
|
|
498
|
+
});
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
function init() {
|
|
502
|
+
|
|
503
|
+
initializeModelSelect();
|
|
504
|
+
setupAiAssistantFooterActions();
|
|
505
|
+
|
|
506
|
+
RED.events.on('settings:ai-config-changed', initializeModelSelect);
|
|
507
|
+
|
|
508
|
+
/* var def = RED.nodes.getType(nodeType);
|
|
509
|
+
addNodeType(nodeType, def);
|
|
510
|
+
if (def.onpaletteadd && typeof def.onpaletteadd === 'function') {
|
|
511
|
+
def.onpaletteadd.call(def);
|
|
512
|
+
}
|
|
513
|
+
});*/
|
|
514
|
+
|
|
515
|
+
// $('#palette > .palette-spinner').show();
|
|
516
|
+
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
|
|
520
|
+
|
|
521
|
+
function refresh() {}
|
|
522
|
+
|
|
523
|
+
return {
|
|
524
|
+
init: init,
|
|
525
|
+
refresh: refresh
|
|
526
|
+
};
|
|
527
|
+
})();
|
|
@@ -13,6 +13,13 @@ RED.palette = (function () {
|
|
|
13
13
|
onSelect: () => {
|
|
14
14
|
RED.palette.explorer.refresh()
|
|
15
15
|
}
|
|
16
|
+
},
|
|
17
|
+
aiassist: {
|
|
18
|
+
header: 'palette-option-aiassist-a',
|
|
19
|
+
panel: 'palette-aiassistant',
|
|
20
|
+
onSelect: () => {
|
|
21
|
+
RED.palette.aiassist.refresh()
|
|
22
|
+
}
|
|
16
23
|
}
|
|
17
24
|
}
|
|
18
25
|
|
|
@@ -26,11 +33,17 @@ RED.palette = (function () {
|
|
|
26
33
|
deselectAll();
|
|
27
34
|
select(options.explorer);
|
|
28
35
|
});
|
|
36
|
+
|
|
37
|
+
$(`#${options.aiassist.header}`).click(e=> {
|
|
38
|
+
deselectAll();
|
|
39
|
+
select(options.aiassist);
|
|
40
|
+
});
|
|
29
41
|
}
|
|
30
42
|
|
|
31
43
|
const deselectAll = () => {
|
|
32
44
|
deselect(options.nodes);
|
|
33
45
|
deselect(options.explorer);
|
|
46
|
+
deselect(options.aiassist);
|
|
34
47
|
|
|
35
48
|
}
|
|
36
49
|
|
|
@@ -48,9 +61,128 @@ RED.palette = (function () {
|
|
|
48
61
|
|
|
49
62
|
}
|
|
50
63
|
|
|
64
|
+
$('#palette-separator').draggable({
|
|
65
|
+
axis: 'x',
|
|
66
|
+
start: function (event, ui) {
|
|
67
|
+
sidebarDragging = true;
|
|
68
|
+
sidebarWidthP0 = $('#sidebar').width();
|
|
69
|
+
delta = 0;
|
|
70
|
+
},
|
|
71
|
+
drag: function (event, ui) {
|
|
72
|
+
let d = ui.position.left;
|
|
73
|
+
delta = sidebarWidthP0 - d;
|
|
74
|
+
$('#sidebar').width(delta);
|
|
75
|
+
if ($('#sidebar').width() > maxWidthSidebar()) {
|
|
76
|
+
$('#sidebar').width(maxWidthSidebar());
|
|
77
|
+
}
|
|
78
|
+
event.stopImmediatePropagation();
|
|
79
|
+
RED.events.emit('palette:resize');
|
|
80
|
+
},
|
|
81
|
+
stop: function (event, ui) {
|
|
82
|
+
setTimeout(function () {
|
|
83
|
+
sidebarDragging = false;
|
|
84
|
+
}, 1000);
|
|
85
|
+
RED.events.emit('palette:resize');
|
|
86
|
+
},
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
var sidebarControls = $(
|
|
90
|
+
'<div class="sidebar-control-right"><i class="fa fa-chevron-right"</div>'
|
|
91
|
+
).appendTo($('#palette-separator'));
|
|
92
|
+
sidebarControls.click(function () {
|
|
93
|
+
sidebarControls.hide();
|
|
94
|
+
RED.menu.toggleSelected('menu-item-sidebar');
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
function maxWidthSidebar() {
|
|
98
|
+
const totalWidth = $('#main-container').width();
|
|
99
|
+
const shortcutsWidth = 50;
|
|
100
|
+
const paletteWidth = $('#palette').width();
|
|
101
|
+
const sidebarSeparatorWidth = $('#palette-separator').width()
|
|
102
|
+
|
|
103
|
+
return totalWidth - shortcutsWidth - paletteWidth - sidebarSeparatorWidth;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function maximizeSidebar() {
|
|
107
|
+
const targetWidth = maxWidthSidebar();
|
|
108
|
+
$('#sidebar').width(targetWidth);
|
|
109
|
+
|
|
110
|
+
// Hack: Make sure the workspace if reset before maximizing the sidebar. This is because when dragging the sidebar the workspace width is also modified
|
|
111
|
+
$('#workspace').css('right', 485);
|
|
112
|
+
RED.events.emit('sidebar:resize');
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function restoreSidebar() {
|
|
116
|
+
const offsetWidth = 435;
|
|
117
|
+
|
|
118
|
+
$('#sidebar').width(offsetWidth);
|
|
119
|
+
// $('#sidebar-separator').css('left', '');
|
|
120
|
+
RED.events.emit('sidebar:resize');
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function isSidebarVisible() {
|
|
124
|
+
return !$('#main-container').hasClass('sidebar-closed');
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
function toggleSidebar(state) {
|
|
128
|
+
let checkIfscrollWorkspaceRequired = false;
|
|
129
|
+
if (!state) {
|
|
130
|
+
// Hide the sidebar and sidebar-separator
|
|
131
|
+
// $('#sidebar-separator').hide();
|
|
132
|
+
$('#main-container').addClass('sidebar-closed');
|
|
133
|
+
$('#shelve-right').removeClass('shelf-selected');
|
|
134
|
+
} else {
|
|
135
|
+
if (!$('#shelve-right').hasClass('shelf-selected')) {
|
|
136
|
+
checkIfscrollWorkspaceRequired = true;
|
|
137
|
+
}
|
|
138
|
+
// Show the sidebar and sidebar-separator
|
|
139
|
+
// $('#sidebar-separator').show();
|
|
140
|
+
$('#main-container').removeClass('sidebar-closed');
|
|
141
|
+
$('#shelve-right').addClass('shelf-selected');
|
|
142
|
+
sidebar_tabs.resize();
|
|
143
|
+
}
|
|
144
|
+
RED.events.emit('sidebar:resize');
|
|
145
|
+
if (checkIfscrollWorkspaceRequired && RED.editor.getLatestNodeSelected()) {
|
|
146
|
+
// Reposition the workspace if the node may be left hidden behind the sidebar
|
|
147
|
+
const safeDistNodeSidebar = 300;
|
|
148
|
+
let nodeSelectedPosX = RED.editor.getLatestNodeSelected().x;
|
|
149
|
+
let initialScrollLeft = $('#chart').scrollLeft();
|
|
150
|
+
let sidebarPosLeftX = $('#sidebar').position().left;
|
|
151
|
+
// console.log(`[sidebar]
|
|
152
|
+
// - initalScrollLeft: ${initialScrollLeft}
|
|
153
|
+
// - nodeSelectedPosX: ${nodeSelectedPosX}
|
|
154
|
+
// - sidebarPosLeftX: ${sidebarPosLeftX}
|
|
155
|
+
// ----------------------------------------
|
|
156
|
+
// `);
|
|
157
|
+
if (nodeSelectedPosX - initialScrollLeft + safeDistNodeSidebar > sidebarPosLeftX) {
|
|
158
|
+
let targetScrollLeft = nodeSelectedPosX - sidebarPosLeftX + safeDistNodeSidebar;
|
|
159
|
+
$('#chart').animate({ scrollLeft: targetScrollLeft })
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
function isSidebarDragging() {
|
|
165
|
+
return sidebarDragging;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
function showSidebar(id) {
|
|
169
|
+
if (id) {
|
|
170
|
+
if (!containsTab(id)) {
|
|
171
|
+
sidebar_tabs.addTab(knownTabs[id]);
|
|
172
|
+
}
|
|
173
|
+
sidebar_tabs.activateTab(id);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
function containsTab(id) {
|
|
178
|
+
return sidebar_tabs.contains(id);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
|
|
51
182
|
const init = () => {
|
|
52
183
|
// Preselection
|
|
53
184
|
deselect(options.explorer);
|
|
185
|
+
deselect(options.aiassist);
|
|
54
186
|
select(options.nodes);
|
|
55
187
|
|
|
56
188
|
// Handle the click events
|