-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathui.html
More file actions
964 lines (814 loc) · 33.7 KB
/
ui.html
File metadata and controls
964 lines (814 loc) · 33.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Layout Helper</title>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Layout Helper</title>
<style>
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;600&display=swap');
:root {
--button-color: #000;
--button-hover-color: #333;
--button-active-color: #666;
--input-focus-color: #000;
--border-color: #eaeaea;
--background-color: #f9f9f9;
--text-color: #333;
--font-family: 'Inter', sans-serif;
--box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
--primary-color: #1662f3;
--light-color: #d0dffc;
--dark-color: #283b70;
}
body {
font-family: var(--font-family);
margin: 0;
padding: 20px;
background: #ffffff;
color: var(--text-color);
line-height: 1.6;
}
h1 {
font-size: 28px;
color: var(--text-color);
font-weight: 600;
margin-bottom: 20px;
}
p {
color: var(--text-color);
margin-bottom: 10px;
}
.button {
background-color: var(--button-color);
color: #ffffff;
border: none;
padding: 12px 24px;
border-radius: 5px;
cursor: pointer;
font-size: 16px;
font-weight: 200;
transition: background-color 0.3s, box-shadow 0.3s, transform 0.2s;
margin-top: 20px;
width: 100%;
}
.button.hidden {
display: none;
}
.button.shown {
display: inline;
}
.button:hover {
background-color: var(--button-hover-color);
transform: translateY(-2px);
box-shadow: var(--box-shadow);
}
.button:active {
background-color: var(--button-active-color);
box-shadow: none;
transform: translateY(0);
}
/* Thisx will apply when the button has the 'active' class added to it */
.button.active {
/* Styles for when the button is in the 'active' state */
background-color: var(--button-active-color);
box-shadow: none;
transform: translateY(0);
}
.icon-button {
display: inline-flex;
align-items: center;
justify-content: center;
padding: 8px 16px;
font-size: 14px;
width: auto;
/* Adjust width as needed, or remove it to let the content define the size */
/* Adjust height as needed */
}
.icon-button i {
stroke-width: 2;
/* Adjust the icon thickness */
width: 20px;
/* Adjust icon size */
height: 20px;
/* Adjust icon size */
margin-right: 8px;
/* Space between icon and text */
}
.output {
height: 120px;
}
.output,
.input-field {
width: 100%;
padding: 12px;
border-radius: 5px;
border: 1px solid var(--border-color);
background: #fff;
box-shadow: none;
transition: border-color 0.3s, box-shadow 0.3s;
resize: none;
margin-top: 15px;
}
.input-field:focus,
.output:focus {
border-color: var(--input-focus-color);
box-shadow: 0 0 0 2px rgba(0, 0, 0, 0.1);
outline: none;
}
.label {
display: block;
margin-top: 20px;
color: var(--text-color);
font-size: 14px;
}
.config-section>.input-field {
margin-top: 0px;
}
.welcome-section {
margin-bottom: 20px;
}
.button-container {
text-align: center;
/* centering the copy button */
margin-top: 20px;
}
.button-container .button {
display: inline-block;
/* for better control over button width */
width: auto;
/* auto width based on content */
margin-right: 10px;
/* spacing between buttons */
}
/* Handling last button margin */
.button-container .button:last-child {
margin-right: 0;
}
/* Divider */
hr {
border: none;
border-top: 1px solid var(--border-color);
margin: 20px 0;
}
.modal {
display: none;
position: fixed;
z-index: 1000;
left: 0;
top: 0;
width: 100%;
height: 100%;
overflow: auto;
background-color: rgba(0, 0, 0, 0.4);
padding-top: 100px;
}
.modal-content {
display: flex;
flex-direction: column;
flex-grow: 1;
background-color: #fff;
margin: auto;
padding: 20px;
border: 1px solid #eee;
width: 80%;
height: 75vh;
box-shadow: 0 8px 24px rgba(0, 0, 0, 0.1);
overflow: scroll;
}
.modal-content h2 {
color: #000;
}
.modal-content p {
font-size: small;
}
.modal-content>.node-tree {
flex-shrink: 0;
/* Prevents these elements from shrinking */
}
.close {
color: #aaa;
float: right;
font-size: 28px;
font-weight: bold;
}
.close:hover,
.close:focus {
color: black;
text-decoration: none;
cursor: pointer;
}
.node-tree {
list-style-type: none;
overflow-y: auto;
flex: 1;
margin: 0;
padding: 0;
min-height: 100px;
overflow-y: auto;
margin-top: 20px;
border: 1px solid #eee;
background: #FFF;
padding: 10px;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
}
.node-button {
display: block;
background-color: #fff;
padding: 5px 30px;
margin: 1px 0;
color: #333;
text-align: left;
cursor: pointer;
transition: all 0.3s;
}
.node-button:hover {
background-color: #f9f9f9;
}
#nodeInstruction {
/* width: 100%; */
min-height: 100px;
/* max-width: 100%; */
margin-top: 15px;
padding: 20px;
/* background: #fff; */
/* color: #333; */
/* border: 1px solid #eee; */
/* border-radius: 7px; */
margin-bottom: 20px;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
}
.chip-container {
display: flex;
gap: 10px;
margin-bottom: 20px;
}
.chip {
font-family: var(--font-family);
font-size: 1rem;
height: 30px;
width: 150px;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
font-size: small;
border-radius: 5px;
box-shadow: 0 0 0 2px var(--button-color);
/* font-weight: 400; */
/* padding:5px; */
transition: background-color 250ms ease, box-shadow 250ms ease;
/* Adjusted padding */
cursor: pointer;
margin-top: 20px;
position: relative;
/* To position the dot absolutely inside the chip */
}
.chip:hover {
/* background-color: blue; */
box-shadow: 0 0 0 calc(2px + 0.05rem) grey;
}
.chip.active {
box-shadow: 0 0 0 calc(2px + 0.05rem) black;
}
</style>
</head>
<body>
<!DOCTYPE html>
<html lang="en">
<body>
<div class="main">
<div class="welcome-section">
<h1>Welcome to Layout Helper</h1>
<p>This tool generates instructions for ChatGPT to make code responsive with TailwindCSS, focusing only on the
layout.</p>
<p><strong>Note:</strong> SVG content handling varies per case.</p>
</div>
<hr>
<div class="generate-section">
<h3>Get the layout</h3>
<p>`generate()` button will recursively crawl your selected page and generate the layout css along with
description of the name/position/child id</p>
<div style="display: flex; flex-direction: row">
<button id="useRawSvgButton" class="button">useRawSvg</button>
</div>
<textarea id="output-layout" class="output" readonly
placeholder="Generated instructions will appear here..."></textarea>
<div style="display:flex; flex-direction:row; gap: 10px;">
<button id="generate-layout" class="button">Layout</button>
<button id="fineTunePrompt" class="button">Finetune</button>
<button id="copy-layout" class="button icon-button">
<i data-feather="copy"></i>
</button>
</div>
</div>
<div id="myModal" class="modal">
<div class="modal-content">
<div style="display: flex; flex-direction: row; align-items: center; position: relative;">
<div style="width: 100%;">
<h2>Finetune prompt</h2>
<p>This modal allows you to select a node and specify instructions GPT should listen to. Start by
selecting a node from the drop down below, then add custom instructions in the textarea on the bottom of
the modal</p>
</div>
<span class="close" style="position: absolute; top: 10px; right: 10px">×</span>
</div>
<h3 style="margin-bottom:-10px;">Select a node below</h3>
<div id="nodeTree" class="node-tree" style="margin-bottom:10px;"></div>
<h3>Specify Instructions</h3>
<p>Now that you have the selected node, specify it's instructions below</p>
<textarea id="nodeInstruction" class="output"
placeholder="Select a node to edit its AI instructions"></textarea>
<button id="saveNodeConfig" class="button">Save Node Config</button>
</div>
</div>
<hr>
<!-- Additional fields for user to fill in -->
<div class="config-section">
<h3>AI Configuration</h3>
<p>`save()` button will save your API key in localstorage so you don't have to get them from your ai portals
currently we only support Azure OpenAI but we plan on supporting multiple profiles, and other AI vendors like
Anthropic 100k</p>
<div class="chip-container">
<!-- Chips for selecting the AI service -->
<div class="chip" style="--primary-color: #efd81d;" data-service="AZURE_OPEN_AI">Azure OpenAI</div>
<div class="chip" style="--primary-color: #41b883;" data-service="OPEN_AI">OpenAI</div>
<div class="chip" style="--primary-color: #ff3e00;" data-service="ANTHROPIC">Anthropic</div>
</div>
<div id="configOptions"></div>
</div>
<button id="save" onclick="updateConfig()" class="button hidden">Save Configuration</button>
<hr>
<div class="generate-ai-section">
<h3>Ask GPT</h3>
<p>`generateCode()` button will send your instructions to chatgpt, we recommend gpt4 32k for large, full app
designs, but component-by-component gpt3 does well</p>
<!-- Textarea for the openai output -->
<textarea id="output-ai" class="output" readonly
placeholder="Generated openai code will appear here..."></textarea>
<div style="display:flex; flex-direction:row; gap: 60px;">
<button id="generate-ai" class="button">AI Generate Code</button>
<button id="copy-ai" class="button icon-button">
<i data-feather="copy"></i>
</button>
</div>
</div>
</div>
<script src="https://unpkg.com/feather-icons"></script>
<script>
var SVGS = {};
function updateConfig() {
// Collect configuration from the input fields
console.log("in updateConfig()");
const service = document.querySelector('.chip.active').dataset.service; // Active service based on chip selection
const inputs = document.querySelectorAll('#configOptions .input-field');
const configData = {};
inputs.forEach(input => {
configData[input.id] = input.value;
});
// Send the collected data to the Figma plugin code to save the configuration
console.log("service: html", service);
parent.postMessage({
pluginMessage: {
type: 'save-settings',
service: service,
configData: configData
}
}, '*');
}
const chips = document.querySelectorAll('.chip');
chips.forEach(chip => {
chip.addEventListener('click', function () {
// Remove 'active' class from all chips
chips.forEach(c => c.classList.remove('active'));
// Add 'active' class to the clicked chip
this.classList.add('active');
// Assuming generateConfigOptions is defined and properly generates the config fields
const optionsHtml = generateConfigOptions(this.dataset.service);
document.getElementById('configOptions').innerHTML = optionsHtml;
// Assuming there's a function to handle loading settings (not shown in this snippet)
loadSettings(this.dataset.service);
document.getElementById("save").className = "button shown";
});
});
// Function to load settings for a given service
function loadSettings(service) {
// Post a message to the Figma plugin to load settings for the specified service
parent.postMessage({
pluginMessage: {
type: 'load-settings',
service: service
}
}, '*');
}
function generateConfigOptions(service) {
// This function will generate HTML based on the selected service
// For simplicity, we're just returning different inputs for each service
let optionsHtml = '';
switch (service) {
case 'AZURE_OPEN_AI':
optionsHtml = `
<label class="label">Azure Resource Name</label>
<input type="text" id="AZURE_OPEN_AI-resourceName" class="input-field" placeholder="Enter Resource Name" />
<label class="label">Azure Deployment Name</label>
<input type="text" id="AZURE_OPEN_AI-deploymentName" class="input-field" placeholder="Enter Deployment Name" />
<label class="label">Azure OpenAI APIKey</label>
<input type="password" id="AZURE_OPEN_AI-apiKey" class="input-field" placeholder="Enter API Key" />
`;
break;
case 'OPEN_AI':
optionsHtml = `
<label class="label">OpenAI Key</label>
<input type="password" id="OPEN_AI-apiKey" class="input-field" placeholder="Enter API Key \"SK-...\"" />
`;
break;
case 'ANTHROPIC':
optionsHtml = `
<label class="label">Anthropic not supported yet</label>
<input type="password" id="ANTHROPIC-apiKey" class="input-field" placeholder="Enter API Key \"SK-...\"" />
`;
break;
// Add more cases as needed
}
return optionsHtml;
}
feather.replace();
let svgButton = document.getElementById("useRawSvgButton");
svgButton.addEventListener('click', () => {
// Check if button is active
svgButton.classList.toggle('active');
// Update the dataset.useRawSvg based on the presence of the 'active' class
svgButton.dataset.useRawSvg = svgButton.classList.contains('active');
// Log the current state to the console (optional)
console.log(`Button active: ${svgButton.classList.contains('active')}`);
console.log(`useRawSvg: ${svgButton.dataset.useRawSvg}`);
});
document.getElementById('generate-layout').onclick = () => {
let useRawSvg = svgButton.classList.contains('active');
console.log(`generate-layout.onclick()::{ type: 'generate-layout-instructions', useRawSvg}`, { type: 'generate-layout-instructions', useRawSvg })
parent.postMessage({ pluginMessage: { type: 'generate-layout-instructions', useRawSvg } }, '*');
}
// Copy button event listener
document.getElementById('copy-layout').onclick = () => {
// Select the text field
const output = document.getElementById('output-layout');
output.select();
output.setSelectionRange(0, 99999); // For mobile devices
// Copy the text inside the text field
document.execCommand('copy');
// Alert the copied text
alert('Instructions copied to clipboard! You can put this straight into GPT or configure the API call in the extension and run it within our app!');
};
function aiRequest(config, messages) {
let url;
let body;
let headers;
let apiKey;
switch (config.type) {
case "OPEN_AI":
console.log("aiRequest::document.getElementById(OPEN_AI-apiKey)", document.getElementById('OPEN_AI-apiKey'));
console.log("aiRequest::document.getElementById(OPEN_AI-apiKey)::value", document.getElementById('OPEN_AI-apiKey').value);
apiKey = document.getElementById('OPEN_AI-apiKey').value;
url = `https://api.openai.com/v1/chat/completions`;
body = {
model: "gpt-4-1106-preview",
messages: config.messages,
temperature: 0.3,
top_p: 0.3,
stream: true,
max_tokens: 4096
}
headers = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${apiKey}`
}
};
return { url, body, headers };
break;
case "AZURE_OPEN_AI":
console.log("aiRequest::document.getElementById(AZURE_OPEN_AI-resourceName)", document.getElementById('AZURE_OPEN_AI-resourceName'));
console.log("aiRequest::document.getElementById(AZURE_OPEN_AI-resourceName)::value", document.getElementById('AZURE_OPEN_AI-resourceName').value);
const resourceName = document.getElementById('AZURE_OPEN_AI-resourceName').value;
const deploymentName = document.getElementById('AZURE_OPEN_AI-deploymentName').value;
console.log("aiRequest::document.getElementById(AZURE_OPEN_AI-deploymentName)", document.getElementById('AZURE_OPEN_AI-deploymentName'));
console.log("aiRequest::document.getElementById(AZURE_OPEN_AI-deploymentName)::value", document.getElementById('AZURE_OPEN_AI-deploymentName').value);
apiKey = document.getElementById('AZURE_OPEN_AI-apiKey').value;
console.log("aiRequest::document.getElementById(AZURE_OPEN_AI-apiKey)", document.getElementById('AZURE_OPEN_AI-apiKey'));
console.log("aiRequest::document.getElementById(AZURE_OPEN_AI-apiKey)::value", document.getElementById('AZURE_OPEN_AI-apiKey').value);
const apiVersion = '2023-07-01-preview';
// Construct the URL using template literals
url = `https://${resourceName}.openai.azure.com/openai/deployments/${deploymentName}/chat/completions?api-version=${apiVersion}`;
headers = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'api-key': apiKey
}
};
// Define the data to be sent
body = {
messages: config.messages,
temperature: 0.3,
top_p: 0.3,
max_tokens: 20000,
stream: true
};
return { url, body, headers };
break;
case "ANTHROPIC":
break;
}
return { url, body, headers };
}
document.getElementById('generate-ai').onclick = () => {
// parent.postMessage({ pluginMessage: { type: 'generate-ai' } }, '*');
document.getElementById("output-ai").value = "";
let messages = [
{ role: "system", content: "You are a brilliant frontend software engineer that takes in a highly detailed description of an app layout/component layout/ etc and generates full HTML CSS JS code in a single HTML page, you make it responsive by leveraging tailwindcss loaded via CDN, you follow the instructions set out by the user. You respond only in code, never preface anything with anything, just pure code. You make the dimensions as exact as possible, approximating with tailwind css sizes. You always generate responsive code implementing resizing like so: sm:, md:, lg:, xl:, 2xl: " },
// { role: "user", content: `#Button { "display": "flex", "width": "258.99px", "height": "64px", "justify-content": "center", "align-items": "center", "flex-shrink": "0", "border-radius": "20px", "background": "#FFF" } // Node name: ButtonLabel "Schedule Quiz" #Button:nth-child(1) { "color": "#7209B7", "font-family": "Poppins", "font-size": "20.519px", "font-style": "normal", "font-weight": "500", "line-height": "normal" } ` },
// { role: "assistant", content: `` },
// { role: "user", content: `` },
// { role: "assistant", content: `` },
{ role: "user", content: `${document.getElementById("output-layout").value}` },
]
let config = {
type: document.querySelector('.chip.active').dataset.service,
messages
}
const { url, body, headers } = aiRequest(config);
console.log("aiRequest(config)::url", url);
console.log("aiRequest(config)::body", body);
console.log("aiRequest(config)::headers", headers);
console.log("req", {
method: headers.method,
headers: headers.headers,
body: JSON.stringify(body),
})
let startPrinting = false;
fetch(url, {
method: headers.method,
headers: headers.headers,
body: JSON.stringify(body),
})
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
const reader = response.body.getReader();
const decoder = new TextDecoder();
let previousPart = "";
reader.read().then(function pump({ done, value }) {
if (done) {
const output = document.getElementById('output-ai');
let content = output.value; // Initialize content variable with the textarea value
// Now that content is defined, you can log it
// console.log(":::::::::::: REPLACING :::::::::::\n\n\nCONTENT:\n\n\n", content);
Object.keys(SVGS).forEach((key) => {
const regex = new RegExp(`${key}`, 'g');
content = content.replace(regex, SVGS[key]);
});
// console.log(":::::::::::: IS NOW :::::::::::\n\n\nCONTENT:\n\n\n", content);
output.value = content; // Update the textarea with the new content
// Alert the copied text
alert('Instructions copied to clipboard! You can put this straight into GPT or configure the API call in the extension and run it within our app!');
SVGS = {};
return;
}
let chunk = decoder.decode(value);
let parts = chunk.split('\n'); // assuming newline-delimited JSON
let failed = false;
try {
JSON.parse(parts[0].slice(6));
} catch (error) {
failed = true;
console.error("Caught error trying to parse JSON for part:\n\n\n", parts[0].slice(6));
}
if (failed) {
try {
let concat = ""
if (previousPart != "") {
console.log("\n\n\n:::::::: previousPart ::::::::\n\n\n", previousPart);
console.log("\n\n\n:::::::: parts[0] :::::::::::::\n\n\n", parts[0]);
let latestPart = parts[0].slice(6);
let prevPart = previousPart.slice(6);
if (!parts[0].startsWith("data: ")) {
latestPart = parts[0];
}
if (!previousPart.startsWith("data: ")) {
prevPart = previousPart;
}
concat = prevPart + latestPart;
console.log(`\n\n\n:::::::: concat ::::::::\n\n\n${concat}\n\n\n`);
JSON.parse(concat);
parts[0] = "data: " + concat;
previousPart = "";
}
} catch {
console.error("Concatenation did not result in valid JSON. Keeping previous part for next attempt.");
}
}
parts.forEach(part => {
if (part) {
// console.log(part);
// part comes in the form "data: <JSON HERE>", so we ignore first 6 chars
let data = part.slice(6);
if (!data.includes("[DONE]")) {
let json = undefined;
try {
json = JSON.parse(data);
}
catch {
startPrinting = true;
console.error("\n\n\nERROR WHILE PARSING PART\n\n\n", part);
previousPart = part;
}
if (json && json.choices && json.choices[0] && json.choices[0].delta && json.choices[0].delta.content) {
let textarea = document.getElementById("output-ai");
textarea.value += json.choices[0].delta.content;
textarea.scrollTop = textarea.scrollHeight;
}
}
}
});
// Read some more, and call this function again
return reader.read().then(pump);
});
})
.catch(error => {
console.error('Fetch error:', error);
});
}
// Copy button event listener
document.getElementById('copy-ai').onclick = () => {
// Select the text field
const output = document.getElementById('output-ai');
output.select();
output.setSelectionRange(0, 99999); // For mobile devices
// Copy the text inside the text field
document.execCommand('copy');
// Alert the copied text
alert('Instructions copied to clipboard! You can put this straight into GPT or configure the API call in the extension and run it within our app!');
};
// // Request the plugin to load settings when the UI is loaded
// parent.postMessage({ pluginMessage: { type: 'load-settings' } }, '*');
const textArea = document.getElementById('nodeInstruction');
textArea.addEventListener('input', function () {
const selectedNodeId = textArea.dataset.selectedNodeId;
if (selectedNodeId) {
// Save on each keystroke
parent.postMessage({
pluginMessage: {
type: 'save-node-config',
nodes: [{
node_id: selectedNodeId,
node_instructions: textArea.value
}]
}
}, '*');
}
});
const modal = document.getElementById("myModal");
const btn = document.getElementById("fineTunePrompt");
const span = document.getElementsByClassName("close")[0];
const saveBtn = document.getElementById("saveNodeConfig");
btn.onclick = function () {
modal.style.display = "inline-block";
parent.postMessage({ pluginMessage: { type: 'get-nodes' } }, '*');
}
span.onclick = function () {
modal.style.display = "none";
parent.postMessage({ pluginMessage: { type: 'close-modal' } }, '*');
}
window.onclick = function (event) {
if (event.target == modal) {
modal.style.display = "none";
parent.postMessage({ pluginMessage: { type: 'close-modal' } }, '*');
}
}
function createNodeElement(nodeData, depth = 0) {
const nodeButton = document.createElement('div');
nodeButton.className = 'node-button';
nodeButton.textContent = nodeData.name;
nodeButton.style.paddingLeft = `${depth * 20}px`; // Increase padding for children
nodeButton.dataset.nodeId = nodeData.id;
// Click event to select a node
nodeButton.addEventListener('click', function (event) {
event.stopPropagation(); // Prevent triggering click events on parent elements
// Send message to the plugin to select the node
parent.postMessage({ pluginMessage: { type: 'select-node', node_id: nodeData.id } }, '*');
// Display the instruction for the selected node in the textarea
// const textArea = document.getElementById('nodeInstruction');
// textArea.value = nodeData.instruction;
// textArea.dataset.selectedNodeId = nodeData.id; // Store the selected node ID
textArea.value = "";
// Request the saved instruction for the selected node
parent.postMessage({ pluginMessage: { type: 'get-node-instruction', node_id: nodeData.id } }, '*');
});
return nodeButton;
}
function appendNodeElements(nodeData, container, depth = 0) {
// Create the current node element
const nodeElement = createNodeElement(nodeData, depth);
container.appendChild(nodeElement);
// Recursively append child nodes
if (nodeData.children.length > 0) {
for (const childNode of nodeData.children) {
appendNodeElements(childNode, container, depth + 1);
}
}
}
// Listen for messages sent from the plugin to the UI
window.onmessage = event => {
const pluginMessage = event.data.pluginMessage;
if (pluginMessage.type === 'set-instructions') {
// Set the text in the output box
document.getElementById('output-layout').value = pluginMessage.instructions;
SVGS = pluginMessage.svgs;
}
if (pluginMessage.type === 'set-openai') {
// Set the text in the output box
document.getElementById('output-ai').value = pluginMessage.instructions;
}
if (pluginMessage.type === 'load-settings') {
// If settings are received, set the input fields with these values
// handle whatever chip is active and load their fields
// If settings are received, set the input fields with these values
const service = pluginMessage.service;
const configData = pluginMessage.configData;
console.log("pluginMessage::service", service);
console.log("pluginMessage::configData", configData);
// Find the active chip and make sure it matches the service we are loading settings for
const activeChip = document.querySelector('.chip.active[data-service="' + service + '"]');
if (activeChip) {
// If there is an active chip for the service, load settings into the input fields
for (const key in configData) {
/*
key will be either of:
AZURE_OPEN_AI-apiKey
AZURE_OPEN_AI-resourceName
AZURE_OPEN_AI-deploymentName
OPEN_AI-apiKey
ANTHROPIC-apiKey
*/
const input = document.getElementById(key);
if (input) {
input.value = configData[key];
}
}
} else {
// If there isn't an active chip for the service, log an error or handle accordingly
console.error('Active chip does not match the service for which settings were loaded.');
}
}
if (pluginMessage.type === 'nodes-data') {
const nodeTree = document.getElementById("nodeTree");
nodeTree.innerHTML = ''; // Clear existing content
pluginMessage.data.forEach(nodeData => {
// Call the function with the root node data and depth 0
appendNodeElements(nodeData, nodeTree, 0);
});
modal.style.display = "block";
}
if (pluginMessage.type === 'save-complete') {
console.log("saverino");
}
if (pluginMessage.type === 'node-instruction-data') {
const textArea = document.getElementById('nodeInstruction');
// Set the value of the text area to the instruction fetched from storage
textArea.value = pluginMessage.data.instruction || ''; // If there's no instruction, default to empty string
textArea.dataset.selectedNodeId = pluginMessage.data.node_id; // Store the selected node ID
}
// more events from server here
};
// document.getElementById('save').onclick = () => {
// // Get the values from the input fields
// const configData = {
// resourceName: document.getElementById('resourceName').value,
// deploymentName: document.getElementById('deploymentName').value,
// apiKey: document.getElementById('apiKey').value
// };
// // Send the values to the main plugin code to save
// parent.postMessage({ pluginMessage: { type: 'save-settings', configData } }, '*');
// };
saveBtn.onclick = function () {
const selectedNodeId = textArea.dataset.selectedNodeId;
// Push only the selected node and its instructions
if (selectedNodeId) {
let nodes = [{
node_id: selectedNodeId,
node_instructions: textArea.value
}];
// Post the message with the selected node's instructions
parent.postMessage({ pluginMessage: { type: 'save-node-config', nodes } }, '*');
}
}
</script>
</body>
</html>