Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

edit-attention.js 4.6 KB

You have to be logged in to leave a comment. Sign In
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
  1. function updateInput(target) {
  2. let e = new Event("input", {bubbles: true});
  3. Object.defineProperty(e, "target", {value: target});
  4. target.dispatchEvent(e);
  5. }
  6. function keyupEditAttention(event) {
  7. let target = event.originalTarget || event.composedPath()[0];
  8. if (!target.matches("*:is([id*='_prompt'], .prompt) textarea")) return;
  9. if (!(event.metaKey || event.ctrlKey)) return;
  10. let isPlus = event.key == "ArrowUp";
  11. let isMinus = event.key == "ArrowDown";
  12. if (!isPlus && !isMinus) return;
  13. let selectionStart = target.selectionStart;
  14. let selectionEnd = target.selectionEnd;
  15. let text = target.value;
  16. function selectCurrentParenthesisBlock(OPEN, CLOSE) {
  17. if (selectionStart !== selectionEnd) return false;
  18. // Find opening parenthesis around current cursor
  19. const before = text.substring(0, selectionStart);
  20. let beforeParen = before.lastIndexOf(OPEN);
  21. if (beforeParen == -1) return false;
  22. let beforeParenClose = before.lastIndexOf(CLOSE);
  23. while (beforeParenClose !== -1 && beforeParenClose > beforeParen) {
  24. beforeParen = before.lastIndexOf(OPEN, beforeParen - 1);
  25. beforeParenClose = before.lastIndexOf(CLOSE, beforeParenClose - 1);
  26. }
  27. // Find closing parenthesis around current cursor
  28. const after = text.substring(selectionStart);
  29. let afterParen = after.indexOf(CLOSE);
  30. if (afterParen == -1) return false;
  31. let afterParenOpen = after.indexOf(OPEN);
  32. while (afterParenOpen !== -1 && afterParen > afterParenOpen) {
  33. afterParen = after.indexOf(CLOSE, afterParen + 1);
  34. afterParenOpen = after.indexOf(OPEN, afterParenOpen + 1);
  35. }
  36. if (beforeParen === -1 || afterParen === -1) return false;
  37. // Set the selection to the text between the parenthesis
  38. const parenContent = text.substring(beforeParen + 1, selectionStart + afterParen);
  39. const lastColon = parenContent.lastIndexOf(":");
  40. selectionStart = beforeParen + 1;
  41. selectionEnd = selectionStart + lastColon;
  42. target.setSelectionRange(selectionStart, selectionEnd);
  43. return true;
  44. }
  45. function selectCurrentWord() {
  46. if (selectionStart !== selectionEnd) return false;
  47. const delimiters = ".,\\/!?%^*;:{}=`~() \r\n\t";
  48. // seek backward until to find beggining
  49. while (!delimiters.includes(text[selectionStart - 1]) && selectionStart > 0) {
  50. selectionStart--;
  51. }
  52. // seek forward to find end
  53. while (!delimiters.includes(text[selectionEnd]) && selectionEnd < text.length) {
  54. selectionEnd++;
  55. }
  56. target.setSelectionRange(selectionStart, selectionEnd);
  57. return true;
  58. }
  59. // If the user hasn't selected anything, let's select their current parenthesis block or word
  60. if (!selectCurrentParenthesisBlock('<', '>') && !selectCurrentParenthesisBlock('(', ')')) {
  61. selectCurrentWord();
  62. }
  63. event.preventDefault();
  64. var closeCharacter = ')';
  65. var delta = 0.1;
  66. if (selectionStart > 0 && text[selectionStart - 1] == '<') {
  67. closeCharacter = '>';
  68. delta = 0.05;
  69. } else if (selectionStart == 0 || text[selectionStart - 1] != "(") {
  70. // do not include spaces at the end
  71. while (selectionEnd > selectionStart && text[selectionEnd - 1] == ' ') {
  72. selectionEnd -= 1;
  73. }
  74. if (selectionStart == selectionEnd) {
  75. return;
  76. }
  77. text = text.slice(0, selectionStart) + "(" + text.slice(selectionStart, selectionEnd) + ":1.0)" + text.slice(selectionEnd);
  78. selectionStart += 1;
  79. selectionEnd += 1;
  80. }
  81. var end = text.slice(selectionEnd + 1).indexOf(closeCharacter) + 1;
  82. var weight = parseFloat(text.slice(selectionEnd + 1, selectionEnd + 1 + end));
  83. if (isNaN(weight)) return;
  84. weight += isPlus ? delta : -delta;
  85. weight = parseFloat(weight.toPrecision(12));
  86. if (String(weight).length == 1) weight += ".0";
  87. if (closeCharacter == ')' && weight == 1) {
  88. var endParenPos = text.substring(selectionEnd).indexOf(')');
  89. text = text.slice(0, selectionStart - 1) + text.slice(selectionStart, selectionEnd) + text.slice(selectionEnd + endParenPos + 1);
  90. selectionStart--;
  91. selectionEnd--;
  92. } else {
  93. text = text.slice(0, selectionEnd + 1) + weight + text.slice(selectionEnd + end);
  94. }
  95. target.focus();
  96. target.value = text;
  97. target.selectionStart = selectionStart;
  98. target.selectionEnd = selectionEnd;
  99. updateInput(target);
  100. }
  101. addEventListener('keydown', (event) => {
  102. keyupEditAttention(event);
  103. });
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...