-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTextEdit_main_mar26_2024.gd.BAK
1055 lines (738 loc) · 42.5 KB
/
TextEdit_main_mar26_2024.gd.BAK
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
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
extends TextEdit
@onready var g_node_use_openai = $"../HBoxContainer/CheckBox_use_openai"
@onready var g_node_openai_key = $"../HBoxContainer/TextEdit_oai_key"
@onready var g_node_max_tokens = $"../HBoxContainer/TextEdit_maxtokens"
@onready var g_node_mem_pre = $"../../../../VBoxContainer_control/ColorRect/VBoxContainer/VSplitContainer/VBoxContainer2/VBoxContainer/HBoxContainer3/TextEdit_mem_pre"
@onready var g_node_mem_post = $"../../../../VBoxContainer_control/ColorRect/VBoxContainer/VSplitContainer/VBoxContainer2/VBoxContainer/HBoxContainer3/TextEdit_mem_post"
var g_last_selected_text = "" # = $".".get_selected_text()
var g_last_selected_pre = ""
var g_last_selected_post = ""
var g_last_caret_line = -1 #
var g_last_caret_column = -1 #
var g_last_caret_pre = ""
var g_last_caret_post = ""
func get_pre_text(end_line, end_column):
# need to remove globals
## BEGIN :: find PRE TEXT
var pre_rows = []
# get pre text
for l in range(0, end_line + 1):
var line = get_line(l)
#print("{",line,"}")
# throw away to the right of last line where user has selected to (from_col)
pre_rows.append(line)
# if this is last line, cut off at from_col
if l == end_line:
pre_rows[l] = pre_rows[l].substr(0, end_column)
# now recombine..
g_last_selected_pre = ""
# if this is endline don't add the line break
var arr_count = 0
for l in pre_rows:
if arr_count == end_line:
g_last_selected_pre += l
else:
g_last_selected_pre += l + "\n"
arr_count += 1
#print("pre_rows:", g_last_selected_pre,"---")
## -- END :: find PRE TEXT
return g_last_selected_pre
func get_post_text(start_line,start_col):
## BEGIN :: find POST TEXT
var post_rows = []
# get post text
for l in range(start_line, get_line_count()):
var line = get_line(l)
if l == start_line:
if line.length() == start_col: # is our selection at end?
line = ""
# now substr line if we have our selection in the middle
line = line.substr(start_col, line.length() - start_col)
#print("~",line,'~')
post_rows.append(line)
# now recombine..
g_last_selected_post = ""
for l in post_rows:
g_last_selected_post += l + "\n"
#print("post_rows:", g_last_selected_post,"---")
## -- END :: find POST TEXT
return g_last_selected_post
func set_last_caret():
# for prompts that dont care about selected text, just the caret position
print("updating caret")
g_last_caret_line = get_caret_line()
g_last_caret_column = get_caret_column()
var text_pre = get_pre_text(g_last_caret_line, g_last_caret_column)
var text_post = get_post_text(g_last_caret_line, g_last_caret_column)
print("------")
print("the pre:\n","~~"+text_pre+"~~")
print("the post:\n","~~"+text_post+"~~")
print("------")
# end set_last_caret()
# update our globals on a content selection
func set_last_selected():
# to handle caret in diff places, I think i will try and
# when setting g_last_caret_line, if
# incrementing then we are travelling down with selection
# decrementing then we are travelling up with our selection
print("updating selected text")
print("carets:", get_caret_count() )
print("caret line:", get_caret_line() ) # caret can be before the selection
print("caret column:", get_caret_column() )
print("selection line:", get_selection_line())
print("selection column:", get_selection_column())
g_last_selected_text = $".".get_selected_text()
#
#var num_selected_lines = g_last_selected_text.split("\n").size()
#print("num selected lines:", num_selected_lines)
#
#var lines_arr = $".".text.split("\n")
#print("num lines total:", lines_arr.size())
#### NEW IDEA: just replace selected text with an identifier?
var from_row = get_selection_from_line()
var from_col = get_selection_from_column()
var to_row = get_selection_to_line()
var to_col = get_selection_to_column()
# Print the line and column numbers
print("Selection Start: Line", from_row, " Column", from_col)
print("Selection End: Line", to_row, " Column", to_col)
## BEGIN :: find PRE TEXT
var pre_rows = []
# get pre text
for l in range(0, from_row + 1):
var line = get_line(l)
#print("{",line,"}")
# throw away to the right of last line where user has selected to (from_col)
pre_rows.append(line)
# if this is last line, cut off at from_col
if l == from_row:
pre_rows[l] = pre_rows[l].substr(0, from_col)
# now recombine..
g_last_selected_pre = ""
# for l in pre_rows:
# g_last_selected_pre += l + "\n"
g_last_selected_pre = get_pre_text(from_row, from_col)
#print("pre_rows:", g_last_selected_pre,"---")
## -- END :: find PRE TEXT
## BEGIN :: find POST TEXT
var post_rows = []
# get post text
for l in range(to_row, get_line_count()):
var line = get_line(l)
if l == to_row:
if line.length() == to_col: # is our selection at end?
line = ""
# now substr line if we have our selection in the middle
line = line.substr(to_col, line.length() - to_col)
#print("~",line,'~')
post_rows.append(line)
# now recombine..
g_last_selected_post = ""
for l in post_rows:
g_last_selected_post += l + "\n"
#print("post_rows:", g_last_selected_post,"---")
## -- END :: find POST TEXT
# var all_text = get_text()
#
## Print the text before and after the selection
#print("Text before selection:", text_before)
#print("Selected text:", selected_text)
#print("Text after selection:", text_after)
func my_get_selected():
# return $".".get_selected_text()
return g_last_selected_text
func _ready():
# Connect signals for text selection and editing
connect("text_selected", _on_text_selected)
connect("text_changed", _on_text_changed)
connect("text_set", _on_text_changed)
connect("focus_entered", _on_focus)
connect("caret_changed", _on_caret_changed)
do_ready()
func _changed():
# fires whenever something happens in the textedit
print("a change was detected")
# -> b.
func _on_Timer_timeout():
print("Timer has timed out!")
func _on_caret_changed():
# so we can track text selection
print("caret changed")
_changed()
# track selected text
set_last_selected()
set_last_caret()
pass
func _on_focus():
print("_on_focus")
pass
func _on_defocus():
print("_on_defocus")
pass
func _on_text_selected(start: int, end: int):
print("text selected")
_changed()
func _on_text_changed():
print("text changed")
_changed()
# -> e.
# to add to other textareas:
# add a HTTPRequest to the textedit node and attach this script to the textedit node
# fix a few associations
var last_key_down = -1 # used for autocomplete
var last_selected_text = ""
# Called when the node enters the scene tree for the first time.
func do_ready():
# $".".set_completion(true)
$".".connect('_input', _on_autocomplete)
# BEGIN context menu
var menu = get_menu()
# Remove all items after "Redo".
menu.item_count = menu.get_item_index(MENU_REDO) + 1
# Add custom items.
menu.add_separator()
menu.add_item("Insert Date", MENU_MAX + 1)
# Connect callback.
menu.id_pressed.connect(_on_mnu_insert_date_pressed)
menu.add_item("Ai Assist: Selection Clean", MENU_MAX + 2)
# Connect callback.
menu.id_pressed.connect(_on_mnu_ai_assist_pressed)
menu.add_item("Ai Assist: Translate to English", MENU_MAX + 3)
# Connect callback.
menu.id_pressed.connect(_on_mnu_ai_assist_pressed)
menu.add_item("Ai Assist: Translate to Chinese", MENU_MAX + 4)
# Connect callback.
menu.id_pressed.connect(_on_mnu_ai_assist_pressed)
menu.add_item("Ai Assist: Simplify (Compress)", MENU_MAX + 5)
# Connect callback.
menu.id_pressed.connect(_on_mnu_ai_assist_pressed)
menu.add_item("Ai Assist: Expand (Decompress)", MENU_MAX + 6)
# Connect callback.
menu.id_pressed.connect(_on_mnu_ai_assist_pressed)
menu.add_item("Ai Assist: Generic Code Improver", MENU_MAX + 7)
# Connect callback.
menu.id_pressed.connect(_on_mnu_ai_assist_pressed)
menu.add_item("Ai Assist: Generic Code Summarizer", MENU_MAX + 8)
# Connect callback.
menu.id_pressed.connect(_on_mnu_ai_assist_pressed)
menu.add_item("Ai Assist: Intelligent Code Optimizer", MENU_MAX + 9)
# Connect callback.
menu.id_pressed.connect(_on_mnu_ai_assist_pressed)
menu.add_item("Ai Assist: Intelligent Code Completer", MENU_MAX + 10)
# Connect callback.
menu.id_pressed.connect(_on_mnu_ai_assist_pressed)
menu.add_item("Ai Assist: Intelligent Code Repair", MENU_MAX + 11)
# Connect callback.
menu.id_pressed.connect(_on_mnu_ai_assist_pressed)# menu_item
func _on_mnu_insert_date_pressed(id):
if id == MENU_MAX + 1:
insert_text_at_caret(Time.get_date_string_from_system())
### begin -- ai assistant -- selected text cleanup
### begin -- ai assistant -- selected text cleanup
### begin -- ai assistant -- selected text cleanup
var g_ai_assist_busy = false
func _on_mnu_ai_assist_pressed(id):
if id == MENU_MAX + 2:
print("ai assist context menu")
if not g_ai_assist_busy:
var selected_text = $".".get_selected_text()
print("selected text:", selected_text)
if selected_text.strip_edges() == "":
print("empty input. ignoring.")
else:
g_ai_assist_busy = true
# llm_send() # old
var the_selected_text = $".".get_selected_text()
var the_assistant = "You are a text insertion agent. User will give you a string of text and you are to optimize and rework it to fix any inconsistencies. You will just return the fixed string of text and nothing else. Do not respond with double quotes. If there is no changes required, just return the original text."
var the_prompt = "the string: \"" + the_selected_text + "\""
# llm_send_short
llm_send_short(the_assistant, the_prompt, 4000, 0.0, 0.0, 0.0, rr)
else:
print("assistant busy")
if id == MENU_MAX + 3:
print("ai assist context menu - translate to english")
if not g_ai_assist_busy:
var selected_text = $".".get_selected_text()
print("selected text:", selected_text)
if selected_text.strip_edges() == "":
print("empty input. ignoring.")
else:
g_ai_assist_busy = true
var the_selected_text = $".".get_selected_text()
var the_assistant = "You are a master translater that can translate from many languages to english. You will just return the fixed string of text and nothing else. Do not respond with double quotes. If there is no changes required, just return the original text."
var the_prompt = "the string: \"" + the_selected_text + "\""
llm_send_short(the_assistant, the_prompt, 4000, 0.0, 0.0, 0.0, rr)
else:
print("assistant busy")
if id == MENU_MAX + 4:
print("ai assist context menu - translate to chinese")
if not g_ai_assist_busy:
var selected_text = $".".get_selected_text()
print("selected text:", selected_text)
if selected_text.strip_edges() == "":
print("empty input. ignoring.")
else:
g_ai_assist_busy = true
var the_selected_text = $".".get_selected_text()
var the_assistant = "You are a master translater that can translate from many languages to the best translation of English to Mandaring possible. You will just return the fixed string of text and nothing else. Do not respond with double quotes. If there is no changes required, just return the original text."
var the_prompt = "the string: \"" + the_selected_text + "\""
llm_send_short(the_assistant, the_prompt, 8000, 0.0, 0.0, 0.0, rr)
else:
print("assistant busy")
if id == MENU_MAX + 5:
print("ai assist context menu - simplify")
if not g_ai_assist_busy:
var selected_text = $".".get_selected_text()
print("selected text:", selected_text)
if selected_text.strip_edges() == "":
print("empty input. ignoring.")
else:
g_ai_assist_busy = true
var the_selected_text = $".".get_selected_text()
var the_assistant = "You will at most return 3 sentences. You are a master simplification agent that understands how to rewrite a given string into a minimalist, optimized, enhanced and simplified version of itself. You use as few sentences as possible to convey the original string's idea. You will respond at a grade 7 level of vocabulary with a goal of returning at most a few sentences. You will just return the fixed string of text and nothing else. Do not respond with double quotes. If there is no changes required, just return the original text."
var the_prompt = "the string: \"" + the_selected_text + "\""
llm_send_short(the_assistant, the_prompt, 4000, 0.0, 0.0, 0.0, rr)
else:
print("assistant busy")
if id == MENU_MAX + 6:
print("ai assist context menu - expand")
if not g_ai_assist_busy:
var selected_text = $".".get_selected_text()
print("selected text:", selected_text)
if selected_text.strip_edges() == "":
print("empty input. ignoring.")
else:
g_ai_assist_busy = true
var the_selected_text = $".".get_selected_text()
# first version was extremely nerdy sounding
#var the_assistant = "You are a master text elaboration agent that understands how to rewrite a given string into a maximalist, optimized, enhanced and expanded version of itself. You will respond at a grade 7 level of vocabulary with a goal of returning at most a few sentences. You will just return the fixed string of text and nothing else. Do not respond with double quotes. If there is no changes required, just return the original text."
var the_assistant = "You are a master copywriter that understands how to communicate effectively and rewrite a given string to add more depth about the context, optimize the conveyance of the underlying context, and enhance an expanded version of the original given string. You will add a fair bit more content to best communicate the context of the string better. You will increase the string length by 50% with supporting details. You will respond at a grade 7 level. You will avoid uncommon words and phrases. You will attempt to double the returned data's length. You will just return the fixed string of text and nothing else. Do not respond with double quotes. If there is no changes required, just return the original text."
var the_prompt = "the string: \"" + the_selected_text + "\""
llm_send_short(the_assistant, the_prompt, 4000, 0.0, 0.0, 0.0, rr)
else:
print("assistant busy")
if id == MENU_MAX + 7:
print("ai assist context menu - generic code improver")
if not g_ai_assist_busy:
var selected_text = $".".get_selected_text()
print("selected text:", selected_text)
if selected_text.strip_edges() == "":
print("empty input. ignoring.")
else:
g_ai_assist_busy = true
var the_selected_text = $".".get_selected_text()
# first version was extremely nerdy sounding
#var the_assistant = "You are a master text elaboration agent that understands how to rewrite a given string into a maximalist, optimized, enhanced and expanded version of itself. You will respond at a grade 7 level of vocabulary with a goal of returning at most a few sentences. You will just return the fixed string of text and nothing else. Do not respond with double quotes. If there is no changes required, just return the original text."
var the_assistant = "You will rewrite the code supplied in the string to be more effective, functional, and more detailed. You will attempt to add more code to improve the code in the string. You will just return the updated string of text and nothing else. Do not respond with double quotes. If there is no changes required, just return the original text."
var the_prompt = "the code string: \"" + the_selected_text + "\""
llm_send_short(the_assistant, the_prompt, 4000, 0.0, 0.0, 0.0, rr)
else:
print("assistant busy")
if id == MENU_MAX + 8:
print("ai assist context menu - generic code summarizer")
if not g_ai_assist_busy:
var selected_text = $".".get_selected_text()
print("selected text:", selected_text)
if selected_text.strip_edges() == "":
print("empty input. ignoring.")
else:
g_ai_assist_busy = true
var the_selected_text = $".".get_selected_text()
# first version was extremely nerdy sounding
#var the_assistant = "You are a master text elaboration agent that understands how to rewrite a given string into a maximalist, optimized, enhanced and expanded version of itself. You will respond at a grade 7 level of vocabulary with a goal of returning at most a few sentences. You will just return the fixed string of text and nothing else. Do not respond with double quotes. If there is no changes required, just return the original text."
var the_assistant = "You are a helpful assistant."
var the_prompt = "I will provide the source code for a snippet of code that does something. I want you to remove the source code and replace it with code comments describing what the code did in the order that it was done. Show you response as comments in the programming language the source code was in. Use multiline code commenting if appropriate for the programming language code is using. The source code snippet: \"" + the_selected_text + "\""
llm_send_short(the_assistant, the_prompt, 4000, 0.0, 0.0, 0.0, rr)
else:
print("assistant busy")
if id == MENU_MAX + 9:
print("ai assist context menu - intelligent code optimizer")
# takes previous content and post content and allows use in final prompt and assistant
if not g_ai_assist_busy:
var selected_text = $".".get_selected_text()
print("selected text:", selected_text)
if selected_text.strip_edges() == "":
print("empty input. ignoring.")
else:
g_ai_assist_busy = true
var the_whole_text = $".".text
var the_selected_text = $".".get_selected_text()
#var the_pre_text = $".".get_selection_to_line(get_selection_line())
print("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-")
print("selected_line:", get_selection_line())
var lines_arr = the_whole_text.split("\n")
var the_pre_str = ""
var the_post_str = ""
print("x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-")
# pre string is the text before the selected text
# for i in range(0, get_selection_line()):
# need to redo for i to exclude the amount of lines in selected_text
for i in range(0, get_selection_line() - selected_text.split("\n").size()):
the_pre_str += lines_arr[i] + "\n"
# the whole text
print("x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-")
# post string is the rest of the text after the selected text
for i in range(get_selection_line(), lines_arr.size()):
the_post_str += lines_arr[i] + "\n"
print("x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-")
var ai_memory_pre = int($"../HBoxContainer/TextEdit_ai_memory_pre".text)
var ai_memory_post = int($"../HBoxContainer/TextEdit_ai_memory_post".text)
if ai_memory_pre < 0:
ai_memory_pre = 0
if ai_memory_post < 0:
ai_memory_post = 0
# only leave the last 500 characters of the pre string
if the_pre_str.length() > ai_memory_pre:
the_pre_str = the_pre_str.substr(the_pre_str.length()-ai_memory_pre, ai_memory_pre)
# only leave the first 500 characters of the post string
if the_post_str.length() > ai_memory_post:
the_post_str = the_post_str.substr(0, ai_memory_post)
print("*** *** *** *** *** ")
print("*** *** *** *** *** ")
print("*** *** *** *** *** ")
print("*** *** *** *** *** ")
print("pre:\r\n\r\n", the_pre_str)
print("*** *** *** *** *** ")
print("post:\r\n\r\n", the_post_str)
print("*** *** *** *** *** ")
print("*** *** *** *** *** ")
print("*** *** *** *** *** ")
print("*** *** *** *** *** ")
print("*** *** *** *** *** ")
print("whole:\r\n\r\n", the_whole_text)
# the selected text
print("selected:\r\n\r\n", the_selected_text)
print("pre:\r\n\r\n", lines_arr)
print("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-")
# first version was extremely nerdy sounding
#var the_assistant = "You are a master text elaboration agent that understands how to rewrite a given string into a maximalist, optimized, enhanced and expanded version of itself. You will respond at a grade 7 level of vocabulary with a goal of returning at most a few sentences. You will just return the fixed string of text and nothing else. Do not respond with double quotes. If there is no changes required, just return the original text."
var pre = the_pre_str
var post = the_post_str
var openai = $"../../../../USE_OPENAI".button_pressed
#var the_assistant = "IMPORTANT: You will only return the changed or modified code. You will not send back any extra things like describing the new code or its changes. You will not send back the pre or post. You will optimize a given code string. Return just the changed code string optimized and rewritten to be more modular and attempt to include new and fresh ideas. The text before the code string and the text after the code string is as follows:\n\n```pre\n"+pre+"```\n\n```post\n"+post+"\n"
# var the_assistant = "You are a code changer. User will send you code as 'code string' and you will modify it to be more compact,robust,modular,optimized and efficient. You will ignore anything outside actual code when processing to make a better solution to the 'code string'. Always attempt to deliver an improvement on the 'code string' when generating the response. You will only return the section that applies to what the user sent as the 'code string'. You will leave any descriptions as comments in the code. You will reference pre and post code for inspiration in modifying the 'code string'. Return the most modular code possible."
# var the_assistant = "You are a code optimizer that tries to make the most different and performant code possible from a given code string, and then just show the newly improved code. Describe the improvements in a comment section inserted into code. Just return the code. Comment above the code in a commented code section details of the changes. Reference the pre and post data for ideas on improving. Put any explanation above the code as commented code blocks. Just return code. \n\n"
# the_assistant += "User will provide a 'code string' that you will attempt to modify the 'code string' to be as compact, modular, redundant, as possible and you will only send back the code commment section and the extremely optimized code."
var the_assistant = "You try and create as many functions as possible for the given code string when they make sense. You put any descriptions in a code comments before any of the code as a step-by-step description. Do not have any comments in the functional parts of the code. What you understand to be the text before the 'code string' is as follows (and will not be returned):\n```pre\n"+pre+"```\n```post\n"+post+"\n\n"
var the_prompt = "Optimize the following code to be as minimal and simplistic as possible. Only return an improved and different optimized version of the following 'code string': \"" + the_selected_text + "\"\n\n"
# if openai:
# the_assistant = "You will only return the changed code. You will optimize a given code string. Return just the changed code string optimized and rewritten to be more modular and attempt to include new and fresh ideas. The text before the code string and the text after the code string is as follows:\n\n```pre\n"+pre+"```\n\n```post\n"+post+"\n"
var tokens_max = int($"../../../../Node/HBoxContainer/TextEdit_tokens".text)
#llm_send_short(the_assistant, the_prompt, 4000, 0.0, 0.0, 0.0, rr)
llm_send_short(the_assistant, the_prompt, tokens_max, 0.0, 0.0, 0.0, rr)
else:
print("assistant busy")
# intelligent code optimizer
if id == MENU_MAX + 10:
print("ai assist context menu - intelligent code completer")
# attach nodes
var ai_memory_pre = int(g_node_mem_pre.text)
var ai_memory_post = int(g_node_mem_post.text)
var openai = g_node_use_openai.button_pressed
var tokens_max = int(g_node_max_tokens.text)
# takes previous content and post content and allows use in final prompt and assistant
if not g_ai_assist_busy:
var selected_text = $".".get_selected_text()
print("selected text:", selected_text)
# if selected_text.strip_edges() == "":
# print("empty input. ignoring.")
# else:
if 1:
g_ai_assist_busy = true
var the_whole_text = $".".text
var the_selected_text = $".".get_selected_text()
#var the_pre_text = $".".get_selection_to_line(get_selection_line())
print("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-")
print("selected_column:", get_selection_column())
var lines_arr = the_whole_text.split("\n")
var the_pre_str = ""
var the_post_str = ""
print("x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-")
# pre string is the text before current position in TextEdit in godot gdscript
# for i in range(0, get_selection_line()):
# need to redo for i to exclude the amount of lines in selected_text
var current_line = get_caret_line()
var current_column = get_caret_column()
print("Current Line:", current_line)
print("Current Column:", current_column)
print(". . . . . .")
var the_current_line = ""
if current_line == 0:
the_pre_str = ""
else:
for i in range(0, current_line):
the_pre_str += lines_arr[i] + "\n"
the_current_line = lines_arr[current_line - 1]
the_pre_str += the_current_line.substr(0, current_column)
the_pre_str += "\n"
the_post_str += the_whole_text.substr(the_pre_str.length(), the_whole_text.length() - the_pre_str.length())
if ai_memory_pre < 0:
ai_memory_pre = 0
if ai_memory_post < 0:
ai_memory_post = 0
# only leave the last 500 characters of the pre string
if the_pre_str.length() > ai_memory_pre:
the_pre_str = the_pre_str.substr(the_pre_str.length()-ai_memory_pre, ai_memory_pre)
# only leave the first 500 characters of the post string
if the_post_str.length() > ai_memory_post:
the_post_str = the_post_str.substr(0, ai_memory_post)
print("*** *** *** *** *** ")
print("pre:\r\n\r\n", the_pre_str)
print("*** *** *** *** *** ")
print("post:\r\n\r\n", the_post_str)
print("*** *** *** *** *** ")
# print("whole:\r\n\r\n", the_whole_text)
# the selected text
print("selected:\r\n\r\n", the_selected_text)
#print("pre:\r\n\r\n", lines_arr)
print("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-")
# first version was extremely nerdy sounding
#var the_assistant = "You are a master text elaboration agent that understands how to rewrite a given string into a maximalist, optimized, enhanced and expanded version of itself. You will respond at a grade 7 level of vocabulary with a goal of returning at most a few sentences. You will just return the fixed string of text and nothing else. Do not respond with double quotes. If there is no changes required, just return the original text."
var pre = the_pre_str
var post = the_post_str
#var the_assistant = "IMPORTANT: You will only return the changed or modified code. You will not send back any extra things like describing the new code or its changes. You will not send back the pre or post. You will optimize a given code string. Return just the changed code string optimized and rewritten to be more modular and attempt to include new and fresh ideas. The text before the code string and the text after the code string is as follows:\n\n```pre\n"+pre+"```\n\n```post\n"+post+"\n"
# var the_assistant = "You are a code changer. User will send you code as 'code string' and you will modify it to be more compact,robust,modular,optimized and efficient. You will ignore anything outside actual code when processing to make a better solution to the 'code string'. Always attempt to deliver an improvement on the 'code string' when generating the response. You will only return the section that applies to what the user sent as the 'code string'. You will leave any descriptions as comments in the code. You will reference pre and post code for inspiration in modifying the 'code string'. Return the most modular code possible."
# var the_assistant = "You are a code optimizer that tries to make the most different and performant code possible from a given code string, and then just show the newly improved code. Describe the improvements in a comment section inserted into code. Just return the code. Comment above the code in a commented code section details of the changes. Reference the pre and post data for ideas on improving. Put any explanation above the code as commented code blocks. Just return code. \n\n"
# the_assistant += "User will provide a 'code string' that you will attempt to modify the 'code string' to be as compact, modular, redundant, as possible and you will only send back the code commment section and the extremely optimized code."
# var the_assistant = "You try and create as many functions as possible for the given code string when they make sense. You put any descriptions in a code comments before any of the code as a step-by-step description. You are a master of using only the latest versions of whatever technology you need to complete the code. Your code is built to run, and will not miss out on things. After coming up with the reply, revise and rewrite the response so only the source code shows. Do not have any comments in the functional parts of the code. What you understand to be the text before the 'code string' is as follows (and will not be returned):\n```pre\n"+pre+"```\n```post\n"+post+"\n\n"
var the_assistant = "You are a code completion agent."
var the_prompt = "Show me the code that would go in between the pre and post data and return the code. The code would be something that would be in between the pre and post code. Do not return something that exists in pre or post, but instead show some complimentary code that would extend the pre code but fit in before the post code.\n\n"
the_prompt += "pre:\n\n"+pre+"\n\npost:\n\n"+post+"\n\n"
# if openai:
# the_assistant = "You will only return the changed code. You will optimize a given code string. Return just the changed code string optimized and rewritten to be more modular and attempt to include new and fresh ideas. The text before the code string and the text after the code string is as follows:\n\n```pre\n"+pre+"```\n\n```post\n"+post+"\n"
#llm_send_short(the_assistant, the_prompt, 4000, 0.0, 0.0, 0.0, rr)
llm_send_short(the_assistant, the_prompt, tokens_max, 0.0, 0.0, 0.0, rr)
else:
print("assistant busy")
# intelligent code completer
if id == MENU_MAX + 11:
print("ai assist context menu - intelligent code repair")
# takes previous content and post content and allows use in final prompt and assistant
if not g_ai_assist_busy:
var selected_text = $".".get_selected_text()
print("selected text:", selected_text)
if selected_text.strip_edges() == "":
print("empty input. ignoring.")
else:
g_ai_assist_busy = true
var the_selected_text = $".".get_selected_text()
var the_whole_text = $".".text
#var the_pre_text = $".".get_selection_to_line(get_selection_line())
print("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-")
print("selected_line:", get_selection_line())
var lines_arr = the_whole_text.split("\n")
var the_pre_str = ""
var the_post_str = ""
print("x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-")
# pre string is the text before the selected text
# for i in range(0, get_selection_line()):
# need to redo for i to exclude the amount of lines in selected_text
for i in range(0, get_selection_line() - selected_text.split("\n").size()):
the_pre_str += lines_arr[i] + "\n"
# the whole text
print("x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-")
# post string is the rest of the text after the selected text
for i in range(get_selection_line(), lines_arr.size()):
the_post_str += lines_arr[i] + "\n"
print("x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-")
var ai_memory_pre = int($"../HBoxContainer/TextEdit_ai_memory_pre".text)
var ai_memory_post = int($"../HBoxContainer/TextEdit_ai_memory_post".text)
if ai_memory_pre < 0:
ai_memory_pre = 0
if ai_memory_post < 0:
ai_memory_post = 0
# only leave the last 500 characters of the pre string
if the_pre_str.length() > ai_memory_pre:
the_pre_str = the_pre_str.substr(the_pre_str.length()-ai_memory_pre, ai_memory_pre)
# only leave the first 500 characters of the post string
if the_post_str.length() > ai_memory_post:
the_post_str = the_post_str.substr(0, ai_memory_post)
print("*** *** *** *** *** ")
print("*** *** *** *** *** ")
print("*** *** *** *** *** ")
print("*** *** *** *** *** ")
print("pre:\r\n\r\n", the_pre_str)
print("*** *** *** *** *** ")
print("post:\r\n\r\n", the_post_str)
print("*** *** *** *** *** ")
print("*** *** *** *** *** ")
print("*** *** *** *** *** ")
print("*** *** *** *** *** ")
print("*** *** *** *** *** ")
print("whole:\r\n\r\n", the_whole_text)
# the selected text
print("selected:\r\n\r\n", the_selected_text)
print("pre:\r\n\r\n", lines_arr)
print("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-")
# first version was extremely nerdy sounding
#var the_assistant = "You are a master text elaboration agent that understands how to rewrite a given string into a maximalist, optimized, enhanced and expanded version of itself. You will respond at a grade 7 level of vocabulary with a goal of returning at most a few sentences. You will just return the fixed string of text and nothing else. Do not respond with double quotes. If there is no changes required, just return the original text."
var pre = the_pre_str
var post = the_post_str
var openai = $"../../../../USE_OPENAI".button_pressed
#var the_assistant = "IMPORTANT: You will only return the changed or modified code. You will not send back any extra things like describing the new code or its changes. You will not send back the pre or post. You will optimize a given code string. Return just the changed code string optimized and rewritten to be more modular and attempt to include new and fresh ideas. The text before the code string and the text after the code string is as follows:\n\n```pre\n"+pre+"```\n\n```post\n"+post+"\n"
# var the_assistant = "You are a code changer. User will send you code as 'code string' and you will modify it to be more compact,robust,modular,optimized and efficient. You will ignore anything outside actual code when processing to make a better solution to the 'code string'. Always attempt to deliver an improvement on the 'code string' when generating the response. You will only return the section that applies to what the user sent as the 'code string'. You will leave any descriptions as comments in the code. You will reference pre and post code for inspiration in modifying the 'code string'. Return the most modular code possible."
# var the_assistant = "You are a code optimizer that tries to make the most different and performant code possible from a given code string, and then just show the newly improved code. Describe the improvements in a comment section inserted into code. Just return the code. Comment above the code in a commented code section details of the changes. Reference the pre and post data for ideas on improving. Put any explanation above the code as commented code blocks. Just return code. \n\n"
# the_assistant += "User will provide a 'code string' that you will attempt to modify the 'code string' to be as compact, modular, redundant, as possible and you will only send back the code commment section and the extremely optimized code."
#var the_assistant = "You are a code repair assistant. You will attempt to repair a given bit of code in code string. You will add any code needed to make the code functional. You are a master of using only the latest versions of whatever technology you need to complete the code. Your code is built to run, and will not miss out on things. After coming up with the reply, revise and rewrite the response so only the source code shows. Remove any other descriptions. You will understand and apply the best code changes to make the code more robust. You will analyze and apply all repairs as necessary. You put any descriptions in a code comments before any of the code as a step-by-step description. Do not have any comments in the functional parts of the code. What you understand to be the text before the 'code string' is as follows (and will not be returned):\n```pre\n"+pre+"```\n```post\n"+post+"\n\n"
var the_assistant = "You are a programming source code repair expert."
var the_prompt = "Find all the issues and repairs, and return the repaired code from the following 'code string': \"" + the_selected_text + "\"\n\n"
the_prompt += "The part that came before it was:\n" + the_pre_str + "\n\n"
the_prompt += "The part that came after it was:\n" + the_post_str + "\n\n"
# if openai:
# the_assistant = "You will only return the changed code. You will optimize a given code string. Return just the changed code string optimized and rewritten to be more modular and attempt to include new and fresh ideas. The text before the code string and the text after the code string is as follows:\n\n```pre\n"+pre+"```\n\n```post\n"+post+"\n"
var tokens_max = int($"../../../../Node/HBoxContainer/TextEdit_tokens".text)
#llm_send_short(the_assistant, the_prompt, 4000, 0.0, 0.0, 0.0, rr)
llm_send_short(the_assistant, the_prompt, tokens_max, 0.0, 0.0, 0.0, rr)
else:
print("assistant busy")
# intelligent code optimizer
### ---
func get_content_inside_backticks(message: String) -> String:
var start_index = message.find("```")
if start_index == -1:
return message
start_index += 3
# move to the first \n after the start index
start_index = message.find("\n", start_index)
var end_index = message.find("```", start_index)
if end_index == -1:
return message
return message.substr(start_index, end_index - start_index)
# rr = response return
func rr(result, response_code, headers, body):
print("---- received response.")
print("---- body:",body.get_string_from_utf8())
var response = JSON.parse_string(body.get_string_from_utf8())
var message = response["choices"][0]["message"]["content"]
print('---- response:', response)
print(message)
# if message has double quotes on either end, remove them
if message.begins_with("\"") and message.ends_with("\""):
message = message.substr(1, message.length()-2)
# # if message begins and ends with triple backticks, remove the first and last lines of the response
# if message.begins_with("```") and message.ends_with("```"):
# var lines = message.split("\n")
# message = ""
# for i in range(1, lines.size()-1):
# message += lines[i] + "\n"
message = get_content_inside_backticks(message)
# if message has ``` triple backticks, only return content inside them
#$".".text += message
$".".insert_text_at_caret ( message )
g_ai_assist_busy = false
# END - replace selected text
### end -- ai assistant -- selected text cleanup
### end -- ai assistant -- selected text cleanup
### end -- ai assistant -- selected text cleanup
# END # CONTEXT MENU
func _unhandled_input(event):
if event is InputEventKey:
# print("key down")
last_key_down = Time.get_unix_time_from_system()
# now we put autocomplete in here and cast generation to local llm when we stop typing?
# make in a new scene so we don't pollute this one. Maybe its own standalone copilot thingy.
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
# check time against last_key down to determine if it is time to autocomplete.
if last_key_down != -1 and last_key_down + 3 < Time.get_unix_time_from_system():
last_key_down = -1
print("init autocomplete")
timed_autocomplete()
pass
func timed_autocomplete():
print("do_autocomplete()")
# example selected modify for rewriting ai buddy
#var selected_text = $"../../VBoxContainer2/TextEdit_LLM_INPUT".get_selected_text()
#$"../../VBoxContainer2/TextEdit_LLM_INPUT".delete_selection()
#$"../../VBoxContainer2/TextEdit_LLM_INPUT".insert_text_at_caret ( "xxxxx" )
#print("LLM_INPUT:", selected_text)
# self.text += " asdasdasd"
# Get the length of the text in the TextEdit node
# var text_length = self.get_text().length()
# Set the cursor position to the end of the text
pass
func do_ai_assist():
pass
func _on_autocomplete(prefix):
print("prefix:", prefix)
# return filter(lambda w: w.begins_with(prefix), suggestions)
##### BEGIN CRITICAL ####
func llm_get_headers(openai=false):
var openai_key = g_node_openai_key.text
var headers = []
if openai:
var api_key = openai_key
headers = ["Content-type: application/json", "Authorization: Bearer " + api_key]
else:
headers = ["Content-type: application/json"]
print(headers)
return headers
func llm_get_body_short(the_messages, max_tokens, temperature, frequency_penalty, presence_penalty):
var model = "gpt-3.5-turbo-16k"
max_tokens = int(max_tokens)
temperature = float(temperature)
frequency_penalty = float(frequency_penalty)
presence_penalty = float(presence_penalty)
# prepend assistant prompt only for the output
var send_messages = the_messages
var openai = g_node_use_openai.button_pressed
var body = ""
# get api key
if openai:
body = JSON.new().stringify({
"messages": send_messages,
"temperature": temperature,
"frequency_penalty": frequency_penalty,
"presence_penalty": presence_penalty,
"max_tokens": max_tokens,
"model":model,
"stream":false
})
else: # local llm
body = JSON.new().stringify({
"messages": send_messages,
"temperature": temperature,
"frequency_penalty": frequency_penalty,
"presence_penalty": presence_penalty,
"max_tokens": max_tokens,
# "model":"",
"stream":false
})
print("body:", body)
return body
func llm_send_request_short(url, headers, body, return_func):
print("sending request")
#$HTTPRequest.request_completed.connect(llm_response_return)
$HTTPRequest.request_completed.connect(return_func)
var send_request = $HTTPRequest.request(url,headers,HTTPClient.METHOD_POST, body) # what do we want to connect to
if send_request != OK:
print("ERROR sending request")
pass
func llm_send_short(assistant, prompt, max_tokens, temperature, frequency_penalty, presence_penalty, return_Func):
## --- begin local or remote
var openai = g_node_use_openai.button_pressed
# get api key
if openai:
var api_key = g_node_openai_key.text
var url = llm_get_url(openai)
var headers = llm_get_headers(openai)