-
Notifications
You must be signed in to change notification settings - Fork 1
/
sidebar.html
462 lines (436 loc) · 22.4 KB
/
sidebar.html
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
<!DOCTYPE html>
<html>
<head>
<base target="_top">
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
rel="stylesheet" integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN"
crossorigin="anonymous">
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css"
rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"
integrity="sha384-C6RzsynM9kWDrMNeT87bh95OGNyZPhcTNXj1NW7RuBCsyN/o0jlpcV8Qyq46cDfL"
crossorigin="anonymous"></script>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/hint.min.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/font/bootstrap-icons.css">
</head>
<style>
.clear-btn {
border-radius: 50%;
width: 35px;
height: 35px;
padding: 0;
text-align: center;
border-color: #CCCCCC;
}
.clear-btn:hover {
color: inherit;
background-color: inherit;
border-color: inherit;
}
.accordion-item {
border: none;
}
.accordion-button {
width: 100%;
text-align: left;
border: none;
border-radius: 0;
background-color: transparent;
padding: 1rem 0;
}
.accordion-body {
border-top: none;
padding: 0;
}
.accordion-button:not(.collapsed) {
background-color: transparent;
color: #000;
}
.accordion-button:focus {
box-shadow: none;
}
.smaller-height-input {
height: 30px;
font-size: 13px;
}
.smaller-label {
font-size: 14px;
}
.vertical-line {
border-left: 1px solid #ccc;
height: 37px;
}
.fixed-top {
position: sticky;
top: 0;
background-color: white;
}
</style>
<body class="bg-body">
<div class="m-3">
<div id="error-message-holder" class="fixed-top">
</div>
<div class="card mb-2">
<div class="card-body">
<p class="fs-6">Suggestion Actions</p>
<div class="d-flex justify-content-center">
<button type="button" id="accept" class="btn btn-primary btn-sm me-1">
<span class="spinner-border spinner-border-sm" id="accept-spinner" style="display: none" aria-hidden="true"></span>
<i id="accept-icon" class="bi bi-check2"></i> Accept
</button>
<button type="button" id="reject" class="btn btn-secondary btn-sm">
<span class="spinner-border spinner-border-sm" id="reject-spinner" style="display: none" aria-hidden="true"></span>
<i class="bi bi-x" id="reject-icon"></i> Reject
</button>
</div>
</div>
</div>
<div class="card mb-2">
<div class="card-body">
<p class="fs-6">Prompting Toolbar</p>
<div class="d-flex justify-content-center">
<span class="hint--top" id="tooltip1"> <button type="button" id = "favorite_1" class="btn btn-outline-secondary shadow-sm clear-btn me-1"></button></span>
<span class="hint--top" id="tooltip2"> <button type="button" id = "favorite_2" class="btn btn-outline-secondary shadow-sm clear-btn me-1"></button></span>
<span class="hint--top" id="tooltip3"> <button type="button" id = "favorite_3" class="btn btn-outline-secondary shadow-sm clear-btn me-1"></button></span>
<div class="vertical-line mx-1"></div>
<span class="hint--top" aria-label="Prompt Manager"><button type="button" id="promptman_btn" class="btn btn-outline-secondary shadow-sm clear-btn">
<i class="bi bi-house-door-fill"></i></span>
</button>
</div>
</div>
</div>
<div class="card mb-2">
<div class="card-body">
<p class="fs-6">New Private Prompt</p>
<form>
<div class="row">
<div class="col-8">
<div class="mb-1">
<label for="formTitle" class="form-label smaller-label text-secondary">Title <span class="text-danger">*</span> </label>
<input class="form-control smaller-height-input" id="formTitle" maxlength="40" required>
</div>
</div>
<div class="col-4">
<div class="mb-1">
<label for="formEmoji" class="form-label smaller-label text-secondary">Icon <span class="text-danger">*</span></label>
<input class="form-control rounded-circle smaller-height-input" id="formEmoji" value="😎" oninput="validateEmoji(this)" style="height: 39px; width: 39px;" required>
</div>
</div>
</div>
<label for="prompt" class="form-label smaller-label text-secondary">Prompt <span class="text-danger">*</span></label>
<textarea class="form-control form-control-sm" placeholder="Ex. Correct spelling mistakes"
id="prompt" rows="3" required></textarea>
</form>
<div class="accordion" id="advancedSettings">
<div class="accordion-item">
<h2 class="accordion-header accordion-header-sm" style="height: 62px; outline: none;">
<button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo">
<p class="fs-6 text-secondary">Advanced Settings</p>
</button>
</h2>
<div id="collapseTwo" class="accordion-collapse collapse" data-bs-parent="#advancedSettings">
<div class="accordion-body">
<label for="temperature" class="form-label smaller-label text-secondary">Temperature <span class="text-danger">*</span></label>
<input class="form-control smaller-height-input" id="temperature" type="number" value="0.2" min="0"
max="1" step="0.1" required>
<label for="parsingOutputPattern" class="form-label smaller-label text-secondary">Output Parsing- Pattern </label>
<input class="form-control smaller-height-input" id="parsingOutputPattern" placeholder="(.*)" required>
<label for="parsingOutputReplacement" class="form-label smaller-label text-secondary">Output Parsing- Replacement </label>
<input class="form-control smaller-height-input" id="parsingOutputReplacement" placeholder="$1" required>
<label for="injectionMode" class="form-label smaller-label text-secondary">Injection Mode</label>
<select id="injectionMode" class="form-select smaller-height-input mb-2">
<option value="replace" selected>Replace input text</option>
<option value="append">Append after input text</option>
</select>
<label for="modelType" class="form-label smaller-label text-secondary">Model Name</label>
<select id="modelType" class="form-select smaller-height-input mb-2">
<option value="gpt3.5-free" selected>
gpt-3.5 (Free)
</option>
<option value="gpt-3.5-turbo">
gpt-3.5-turbo
</option>
<option value="gpt-4">
gpt-4
</option>
<option value="gpt-3.5-turbo-1106">
gpt-3.5-turbo-1106
</option>
<option value="geminiPro">
Gemini Pro
</option>
</select>
</div>
</div>
</div>
</div>
<div>
<button type="button" id="llm-submit" class="btn btn-primary btn-sm">
<span class="spinner-border spinner-border-sm" id="run-spinner" style="display: none" aria-hidden="true"></span>
<i id="run-icon" class="bi bi-caret-right"></i> Run
</button>
<span class="hint--top" aria-label="run prompt"><i class="bi bi-info-circle" style="font-size: 0.75rem; color: #6c757d;"></i></span>
<button type="button" id="save-local-prompt" class="btn btn-primary btn-sm">
<span class="spinner-border spinner-border-sm" id="save-spinner" style="display: none" aria-hidden="true"></span>
<i id="save-icon" class="bi bi-save"></i> Save
</button>
<span class="hint--top" aria-label="save prompt to library"><i class="bi bi-info-circle" style="font-size: 0.75rem; color: #6c757d;"></i></span>
</div>
</div>
</div>
</div>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"> </script>
<script>
/**
* On document load, assign click handlers to each button and try to load the
* user's origin and destination language preferences if previously set.
*/
$(function() {
$('#promptman_btn').click(displayPromptManager);
$('#accept').click(acceptSuggestions);
$('#reject').click(rejectSuggestions);
$('#llm-submit').click(runLLM);
$('#save-local-prompt').click(saveLocalPrompt);
initializePromptToolBar();
$(window).on('storage', function(event) {
if (event.originalEvent.key === 'favorites') {
initializePromptToolBar();
}
});
})
function saveLocalPrompt() {
beginLoading("save-local-prompt", "save-spinner", "save-icon");
var title = $('#formTitle').val();
var emoji = $('#formEmoji').val();
var prompt = $('#prompt').val();
var temperature = $('#temperature').val();
var parsingOutputPattern = $('#parsingOutputPattern').val();
var parsingOutputReplacement = $('#parsingOutputReplacement').val();
var injectionMode = $('#injectionMode').val();
var modelType = $("#modelType").val();
if (!title || !emoji || !prompt || !temperature) {
showErrorMessage("Please fill in all required fields to save prompt!");
endLoading("save-local-prompt", "save-spinner", "save-icon");
return;
}
temperature = parseFloat(temperature);
google.script.run.withSuccessHandler(function(arrU) {
uiud = arrU[0];
userId = arrU[1];
var finalInfo = {
"prompt": prompt,
"tags": [],
"temperature": temperature,
"userID": userId,
"userName": "",
"description": "",
"icon": emoji,
"forkFrom": "",
"promptRunCount": 0,
"created": new Date().toISOString(),
"title": title,
"outputParsingPattern": parsingOutputPattern,
"outputParsingReplacement": parsingOutputReplacement,
"recommendedModels": [],
"injectionMode": injectionMode
};
google.script.run.withSuccessHandler(function() {
endLoading("save-local-prompt", "save-spinner", "save-icon");
}).withFailureHandler(function() {
showErrorMessage("Failed to save prompt. Please try again.")
endLoading("save-local-prompt", "save-spinner", "save-icon");
}).addPrompt(uiud, finalInfo);
}).getUiudAndUserId();
}
function initializePromptToolBar() {
google.script.run.withSuccessHandler(function(out) {
favorites = out[0];
localPrompts = out[1];
if(favorites[0]) {
$('#favorite_1').removeAttr('disabled');
$("#favorite_1").text(localPrompts[favorites[0]].icon);
$("#tooltip1").attr("aria-label", localPrompts[favorites[0]].title);
$("#favorite_1").off("click");
var fav1 = localPrompts[favorites[0]];
$("#favorite_1").click(function() {
beginLoadingToolbar("favorite_1");
google.script.run.withSuccessHandler(function(prefModel) {
google.script.run.withSuccessHandler(function() {
endLoadingToolbar("favorite_1", localPrompts[favorites[0]].icon);
google.script.run.addPromptRun(favorites[0]);
}).withFailureHandler(function() {
showErrorMessage("Could not get output. Please check settings to validate the preferred model and API Key.");
endLoadingToolbar("favorite_1", localPrompts[favorites[0]].icon);
}).generateIdeas(fav1.title, fav1.icon, fav1.prompt, fav1.temperature, fav1.outputParsingPattern, fav1.outputParsingReplacement, fav1.injectionMode, prefModel);
}).withFailureHandler(function(){
showErrorMessage("Error. Please try again.");
}).getPrefModel();
});
} else {
$("#favorite_1").empty();
$("#favorite_1").off("click");
$('#favorite_1').attr('disabled', 'disabled');
$("#tooltip1").attr("aria-label", "");
}
if (favorites[1]) {
$('#favorite_2').removeAttr('disabled');
$("#favorite_2").text(localPrompts[favorites[1]].icon);
$("#tooltip2").attr("aria-label", localPrompts[favorites[1]].title);
$("#favorite_2").off("click");
var fav2 = localPrompts[favorites[1]];
$("#favorite_2").click(function() {
beginLoadingToolbar("favorite_2");
google.script.run.withSuccessHandler(function(prefModel) {
google.script.run.withSuccessHandler(function() {
endLoadingToolbar("favorite_2", localPrompts[favorites[1]].icon);
google.script.run.addPromptRun(favorites[1]);
}).withFailureHandler(function() {
showErrorMessage("Could not get output. Please check settings to validate the preferred model and API Key.");
endLoadingToolbar("favorite_2", localPrompts[favorites[1]].icon);
}).generateIdeas(fav2.title, fav2.icon, fav2.prompt, fav2.temperature, fav2.outputParsingPattern, fav2.outputParsingReplacement, fav2.injectionMode, prefModel);
}).withFailureHandler(function(){
showErrorMessage("Error. Please try again.");
}).getPrefModel();
});
} else {
$("#favorite_2").empty();
$("#favorite_2").off("click");
$('#favorite_2').attr('disabled', 'disabled');
$("#tooltip2").attr("aria-label", "");
}
if (favorites[2]) {
$('#favorite_3').removeAttr('disabled');
$("#favorite_3").text(localPrompts[favorites[2]].icon);
$("#tooltip3").attr("aria-label", localPrompts[favorites[2]].title);
var fav3 = localPrompts[favorites[2]];
$("#favorite_3").off("click");
$("#favorite_3").click(function() {
beginLoadingToolbar("favorite_3");
google.script.run.withSuccessHandler(function(prefModel) {
google.script.run.withSuccessHandler(function() {
endLoadingToolbar("favorite_3", localPrompts[favorites[2]].icon);
google.script.run.addPromptRun(favorites[2]);
}).withFailureHandler(function() {
showErrorMessage("Could not get output. Please check settings to validate the preferred model and API Key.");
endLoadingToolbar("favorite_3", localPrompts[favorites[2]].icon);
}).generateIdeas(fav3.title, fav3.icon, fav3.prompt, fav3.temperature, fav3.outputParsingPattern, fav3.outputParsingReplacement, fav3.injectionMode, prefModel);
}).withFailureHandler(function(){
showErrorMessage("Error. Please try again.");
}).getPrefModel();
});
} else {
$("#favorite_3").empty();
$("#favorite_3").off("click");
$('#favorite_3').attr('disabled', 'disabled');
$("#tooltip3").attr("aria-label", "");
}
}).withFailureHandler(function(favorites) {
}).getFavoritesForToolbar();
}
function runLLM() {
beginLoading("llm-submit", "run-spinner", "run-icon");
var title = $('#formTitle').val();
var emoji = $('#formEmoji').val();
var prompt = $('#prompt').val();
var temperature = $('#temperature').val();
var parsingOutputPattern = $('#parsingOutputPattern').val();
var parsingOutputReplacement = $('#parsingOutputReplacement').val();
var injectionMode = $('#injectionMode').val();
var modelType = $("#modelType").val();
if (!title || !emoji || !prompt || !temperature) {
showErrorMessage("Please fill in all required fields to run prompt!");
//$('#llm-submit').removeAttr('disabled');
endLoading("llm-submit", "run-spinner", "run-icon");
return;
}
temperature = parseFloat(temperature);
google.script.run.withSuccessHandler(function() {
endLoading("llm-submit", "run-spinner", "run-icon");
}).withFailureHandler(function() {
endLoading("llm-submit", "run-spinner", "run-icon");
showErrorMessage("Could not get output. Please try again.")
}).generateIdeas(title, emoji, prompt, temperature, parsingOutputPattern, parsingOutputReplacement, injectionMode, modelType);
}
function showErrorMessage(message) {
const alertPlaceholder = $('#error-message-holder');
const alert = $(
`<div class="alert alert-danger d-flex align-items-center justify-content-between" style="font-size: 14px; border-radius: 8px; padding: 6px; color: #c62828; background-color: #ffebee;" role="alert">
<i class="bi bi-x-circle" style="font-size: 1.2em; margin-right: 5px;"></i>
<span>${message}</span>
<div class="d-flex align-items-center">
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
</div>`
);
alertPlaceholder.append(alert);
setTimeout(function() {
alert.fadeOut(function() {
$(this).remove(); // Remove the alert from the DOM after fading out
});
}, 5000);
}
function displayPromptManager() {
google.script.run.showPromptManager();
}
/**
* Accepts text suggestions within selected portion of Google Document
* */
function acceptSuggestions() {
beginLoading("accept", "accept-spinner", "accept-icon");
google.script.run.withSuccessHandler(function() {
endLoading("accept", "accept-spinner", "accept-icon");
}).withFailureHandler(function() {
showErrorMessage("Error. Please try again!")
endLoading("accept", "accept-spinner", "accept-icon");
}).acceptSuggested();
}
/**
* Rejects text suggestions within selected portion of Google document
* */
function rejectSuggestions() {
beginLoading("reject", "reject-spinner", "reject-icon");
google.script.run.withSuccessHandler(function() {
endLoading("reject", "reject-spinner", "reject-icon");
}).withFailureHandler(function() {
showErrorMessage("Error. Please try again!")
endLoading("reject", "reject-spinner", "reject-icon");
}).rejectSuggested();
}
function validateEmoji(input) {
let value = input.value;
let unicodeLength = Array.from(value).length;
const emojiRegex = /^\p{Emoji}/u;
const matches = value.match(emojiRegex);
if (!matches) {
input.value = value.charAt(0);
} else {
input.value = matches[0];
}
}
function beginLoading(buttonId, loadingId, iconId) {
$("#"+loadingId).show();
$("#"+buttonId).attr('disabled', 'disabled');
if (iconId) {
$("#"+iconId).hide();
}
}
function endLoading(buttonId, loadingId, iconId) {
$("#"+loadingId).hide();
$("#"+buttonId).removeAttr('disabled');
if (iconId) {
$("#"+iconId).show();
}
}
function beginLoadingToolbar(buttonId) {
var loadingElement = $('<span class="spinner-border spinner-border-sm" aria-hidden="true"></span>');
$("#" + buttonId).empty();
$("#" + buttonId).append(loadingElement);
$("#" + buttonId).attr('disabled', 'disabled');
}
function endLoadingToolbar(buttonId, icon) {
$("#"+buttonId).text(icon);
$("#"+buttonId).removeAttr('disabled');
}
</script>
</body>
</html>