-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkomple.js
654 lines (522 loc) · 19.2 KB
/
komple.js
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
// Komple: A chrome extension that displays an autocomplete suggestion in the currently active element, taking the suggestion from an external API.
let logMode = ''
const log = (mode, ...what) => (
mode.split(',').includes(logMode) && console.log(...what),
what[what.length - 1]
)
let autocompleteTimer = null
let autocompleteInProgress = null
let modifierPressed = null
function isHotkey(keydownEvent, hotkeyName) {
const { key, modifier } = settings.hotkeys[hotkeyName]
// console.log(key, modifier, modifierPressed)
return key === keydownEvent.key && modifierPressed === modifier
}
const modifierListener = ['keydown', e => {
if ( ['Control', 'Alt', 'Shift', 'Meta'].includes(e.key) ) {
// console.log('Modifier pressed:', e.key)
modifierPressed = e.key
}
}]
const clearModifierListener = ['keyup', e => {
if ( ['Control', 'Alt', 'Shift', 'Meta'].includes(e.key) ) {
// console.log('Modifier released:', modifierPressed)
modifierPressed = null
}
}]
// Clear modifier if the user navigates away from the tab
document.addEventListener('visibilitychange', () => {
if (document.visibilityState === 'hidden') {
modifierPressed = null
}
})
const autocompleteListener = ( e ) => {
// if the hotkey is pressed, autocomplete
if ( e.key == settings.hotkeys.autocomplete.key && settings.hotkeys.autocomplete.modifier && modifierPressed === settings.hotkeys.autocomplete.modifier ) {
autocomplete()
}
// if the activateOnHangingChar setting is on, autocomplete after a hanging character is typed
else if ( settings.activateOnHangingChar ) {
// if a hanging character is typed, start the autocomplete timer
if ( e.key.match(/^[\[\(\{\s“,]$/) ) {
autocompleteTimer = setTimeout(
() => autocomplete(),
500)
// console.log('Autocomplete timer started')
// if a non-hanging character is typed, cancel the autocomplete timer
} else {
if ( autocompleteTimer || autocompleteInProgress )
cancelAutocomplete()
}
}
}
function cancelAutocomplete() {
if ( autocompleteTimer || autocompleteInProgress ) {
clearTimeout(autocompleteTimer),
autocompleteTimer = null,
autocompleteInProgress = null,
document.getElementById('komple-thinking')?.remove()
}
}
function toggleConfigModal() {
let
modal = document.getElementById('komple-config')
if ( modal )
modal.style.display = modal.style.display === 'none' ? 'block' : 'none'
else
createConfigModal()
}
const pickerListener = ( e ) => {
if ( isHotkey(e, 'apiPicker') ) {
let apiPicker = document.getElementById('komple-api-picker')
let configModal = document.getElementById('komple-config')
let configVisible = configModal && configModal.style.display !== 'none'
if ( apiPicker || configVisible ) {
apiPicker?.remove()
// console.log({ configVisible })
if ( configVisible )
configModal.style.display = 'none'
} else {
// If current element is not contenteditable or an input/textarea, return.
let currentElement = getCurrentElement()
if ( !currentElement.isContentEditable && !currentElement.matches('input, textarea') )
return
apiPicker = createDivUnderCurrentElement({ id: 'komple-api-picker' }, div => {
let index = 0
// 'Choose an API'
div.appendChild(document.createElement('div')).innerHTML = '<b>Choose an API</b>'
let { modifier } = settings.hotkeys.apiPicker
const kbd = key => `<kbd style="background-color: #ccc; color: #000;">${key}</kbd>`
for ( let api of settings.apis ) {
index++
let apiDiv = document.createElement('div')
apiDiv.innerHTML = `${kbd(index)} ${api.name}`
apiDiv.className = 'komple-api-picker-item'
apiDiv.style['font-weight'] = api === settings.api ? 'bold' : 'normal'
apiDiv.style['margin-bottom'] = '5px'
div.appendChild(apiDiv)
}
// Add listener for alt+numeric keys that will select the corresponding API
let nextListener = ['keydown', event => {
let { key } = event
// console.log('Picker listener:', key)
// If no API picker exists, delete the listener and return
if ( !document.getElementById('komple-api-picker') )
return document.removeEventListener(...nextListener)
if ( key.match(/^[1-9]$/) ) {
settings.currentApiName = settings.apis[key - 1].name
saveSettings()
autocomplete()
}
if ( key === 'c' ) {
navigator.clipboard.writeText(getPrompt().prompt)
}
removeApiPicker()
event.preventDefault()
}]
let copyDiv = document.createElement('div')
copyDiv.innerHTML = `${kbd('C')} Copy current prompt`
div.appendChild(copyDiv)
let keyupListener = ['keyup', e => {
if ( e.key === modifier )
removeApiPicker()
}]
function removeApiPicker() {
apiPicker.remove()
document.removeEventListener(...nextListener)
document.removeEventListener('click', removeApiPicker)
document.removeEventListener(...keyupListener)
}
document.addEventListener(...nextListener)
// Remove the API picker when the user clicks anywhere in the document
document.addEventListener('click', removeApiPicker)
document.addEventListener(...keyupListener)
})
}
}
}
const escapeListener = ({ key }) => {
if ( key === 'Escape' ) {
// Remove picker and modal, if either exists
document.getElementById('komple-api-picker')?.remove()
document.getElementById('komple-config')?.remove()
}
}
function enable() {
document.addEventListener('keydown', autocompleteListener)
document.addEventListener('keydown', pickerListener)
// cancel autocomplete on mouse click
document.addEventListener('click', cancelAutocomplete)
document.addEventListener('keydown', escapeListener)
document.addEventListener(...modifierListener)
document.addEventListener(...clearModifierListener)
// Load extension config from chrome storage
chrome.storage.sync.get('settings', data => {
// console.log('Loaded config from chrome storage:', data.settings)
if ( data.settings )
for ( let key in settings )
settings[key] = data.settings[key]
// console.log('Loaded settings:', settings)
})
// Listen to chrome storage to update settings
chrome.storage.onChanged.addListener(event => {
let { newValue } = event.settings
Object.assign(settings, newValue)
console.log('Settings changed:', settings)
})
}
function disable() {
document.removeEventListener('keydown', autocompleteListener)
document.removeEventListener('keydown', pickerListener)
document.removeEventListener('click', cancelAutocomplete)
document.removeEventListener('keydown', escapeListener)
document.removeEventListener(...modifierListener)
document.removeEventListener(...clearModifierListener)
}
// Recursively go through children of an element to the deepest child whose textContent ends with a double backslash.
function deepestMatchingChild(element) {
if ( element.textContent/*.includes('\\')*/ ) {
// Scan children. If none, return the element.
if ( !element.children.length )
return element
// If there are children, recurse.
else
for ( let child of element.children ) {
let result = deepestMatchingChild(child)
if ( result )
return result
}
}
}
function getCurrentElement() {
// If active element is a textarea or input, return that element
if ( ['INPUT', 'TEXTAREA'].includes(document.activeElement.tagName) )
return document.activeElement
let { parentElement } = document.getSelection()?.focusNode
return parentElement
}
function createDivUnderCurrentElement(attributes, callback) {
let div = document.createElement('div')
Object.assign(div, attributes)
div.style.position = 'fixed'
div.style.color = 'rgba(0,0,0,0.7)'
let currentElement = getCurrentElement()
// console.log('currentElement:', currentElement)
let { bottom, left } = currentElement.getBoundingClientRect()
div.style.top = bottom + 'px'
div.style.left = left + 'px'
div.style.zIndex = '9999'
div.style.backgroundColor = '#fff'
div.style['border-radius'] = '5px'
div.style.padding = '5px'
div.style['font-family'] = 'sans-serif'
div.style['font-size'] = '0.8em'
// Cool shadow
div.style.boxShadow = '0px 2px 5px -1px rgba(50, 50, 93, 0.25), 0px 1px 3px -1px rgba(0, 0, 0, 0.3) '
callback?.(div)
document.body.appendChild(div)
// console.log('Created div:', div)
return div
}
async function autocomplete() {
if ( autocompleteInProgress )
cancelAutocomplete()
// Assign a random id to this autocomplete
let id = Math.random().toString(36).substring(2, 15)
autocompleteInProgress = id
// console.log('Autocomplete started, id = ' + id)
// // Get the deepest matching child.
// let element = deepestMatchingChild(document.activeElement)
let element = document.activeElement
// console.log('Enclosing element:', element)
if ( element ) {
let { prompt, feeder, suffix } = getPrompt(element)
prompt ||= ''
try {
startThinking( suffix ? 'Inserting' : 'Completing' )
let completion = await getSuggestion(prompt.trimRight(), { feeder, suffix })
if ( feeder ) completion = feeder + completion
if ( autocompleteInProgress === id ) {
// If the prompt's last character isn't alphanumeric, remove the leading space from the completion
prompt.match(/\W$/) && completion.replace(/^\s+/, '')
// // Remove any leading and trailing newlines
// completion = completion.replace(/^\n+/, '').replace(/\n+$/, '')
//
// Replace any newlines (in any quantity) with a space, if settings.removeNewlines is true
completion = completion.replace(/\n+/g, settings.removeNewlines ? ' ' : '\n')
//
// // Remove everything after and including the first newline
// completion = completion.replace(/\n.*/g, '')
console.log('Completion:', completion)
simulateTextInput(completion)
cancelAutocomplete()
}
} catch (e) {
console.log('Error:', e)
cancelAutocomplete()
}
}
}
function startThinking(action = 'Completing') {
let thinking = document.getElementById('komple-thinking')
thinking = createDivUnderCurrentElement({
id: 'komple-thinking',
innerHTML: `${action} with <b>${settings.currentApiName}</b>...`
})
// Add another thinking emoji to the end of the thinking element every second
let thinkingInterval = setInterval(() => {
document.getElementById('komple-thinking') ?
thinking.innerHTML += '.'
: clearInterval(thinkingInterval)
}, 1000)
}
function getPrompt(element = getCurrentElement()) {
let builders = {
'twitter.com': getTwitterPrompt,
'reddit.com': {
scraperVersion: 'v1',
pieces: {
author: {
selector: '[data-click-id="user"]',
last: true
},
title: {
selector: 'title'
},
post: {
selector: '[data-click-id="text"]',
last: true
},
comments: {
many: true,
crawl: true,
stop: {
// stop at the first comment in *this* thread, its padding-left is 16px
style: {
'padding-left': '16px'
}
},
extract: {
author: {
test: {
attributes: {
'data-testid': {
value: 'comment_author_link'
}
}
}
},
comment: {
test: {
attributes: {
'data-testid': {
value: 'comment'
}
}
}
}
},
output: `
u/%author%: %comment%
`
},
self: {
// First <a> element that is a descendant of an element with style="max-width:100%"
selector: '[class="header-user-dropdown"] > button > span > span > span > span'
}
},
output: `
%title%
Posted by %author%
%post%
Comments:
%comments%
u/%self%:`
},
'mail.google.com': {
scraperVersion: 'v2',
stop: {
selector: 'h2' // Stop at conversation title
},
whatIsScraped: 'conversation',
whatIsInputed: 'user reply',
},
'quora.com': {
scraperVersion: 'v2',
whatIsScraped: 'question',
whatIsInputed: 'insightful answer',
instruction: 'Here is an insightful answer on Quora',
}
}
let host = document.location.hostname.replace(/^(www\.)?/, '')
let builder = builders[host]
// console.log('Builder:', builder)
let prompt, input, feeder, suffix
if ( element.textContent ) {
// Get the selection
let selection = window.getSelection()
// Get the caret position for the beginning and end of the selection
let {
anchorOffset, focusOffset,
anchorNode, focusNode,
} = selection
// Split the selection before and after the caret, assigning the values to input and suffix, respectively
input = anchorNode.textContent.slice(0, anchorOffset).trimEnd()
suffix = focusNode.textContent.slice(focusOffset).trimStart()
} else if ( element.tagName === 'TEXTAREA' || element.tagName === 'INPUT' ) {
input = element.value.slice(0, element.selectionStart).trimEnd()
suffix = element.value.slice(element.selectionEnd).trimStart()
} else {
feeder = input = builder?.feeder || ''
}
if ( suffix ) suffix = ' ' + suffix
try {
prompt = typeof builder === 'function' ?
builder({ input, feeder, suffix })
: (
scrape[builder?.scraperVersion || 'default'](builder)
)
// If it's an object, it will return { prompt, suffix }, which we need to reassign
if ( typeof prompt === 'object' ) {
({ prompt, suffix } = prompt)
suffix = suffix.trimRight()
prompt = prompt.trim()
} else {
prompt += input
}
} catch (e) {
console.log('Error:', e)
;( { prompt, suffix } = scrape.default() )
}
process = text => {
if (!text) return
// Remove all {{...}} bits, unless the {{ is directly followed by "...". In that case, keep the ... bit.
text = text.replace(/\{\{(\.\.\.)?[\s\S]*?\}\}/g, "$1")
// Remove all bits within "/*-...*/"
text = text.replace(/\/\*-[\s\S]+?\*\//g, '')
// For all bits formatted as "/*!...*/", remove the enclosing "/*!" and "*/" and trim the inner text
text = text.replace(/\/\*!\s*([\s\S]+?)\s*\*\//g, '$1')
// Replace any number of newlines with 2
text = text.replace(/\n+/g, '\n\n')
text = text.trimRight()
return text
}
// Remove everything in the prompt before and including '//start'
prompt = prompt.split('\n//start').pop()
// Remove everything in the suffix after and including '//stop'
if ( suffix) suffix = suffix.split('\n//stop').shift()
prompt = process(prompt)
suffix = process(suffix)
console.log('Prompt:', prompt)
console.log('Suffix:', suffix)
return { prompt, feeder, suffix }
}
function getTwitterPrompt({ input }) {
// function to extract Twitter handle from href
const getHandle = href => href.replace(/^.*?(\w+)$/, '@$1')
// Find element with aria-label "Profile" and extract the Twitter handle from its href
let myHandle = getHandle( document.querySelector('[aria-label="Profile"]').href )
// Find element with aria-label of Timeline: Conversation
let conversation = document.querySelector('[aria-label="Timeline: Conversation"]')
let output = `Here is a conversation with an insightful reply by ${myHandle}:\n\n`
// Scan through all its decendants, adding items if it's an <article> element
for ( let element of conversation.querySelectorAll('*') ) {
// If it's the current active element, exit the loop
if ( element === document.activeElement )
break
// If it's an <article> element, add it to the list of messages
if ( element.tagName === 'ARTICLE' ) {
let handle = getHandle( element.querySelector('a[role="link"]').href )
let content = element.querySelector('[lang]')?.textContent || '[image]'
output += `${handle}: ${content}\n\n`
}
}
// Add my handle to the end of the list, plus any existing content of the active element
output += `${myHandle}: `
return output
}
function getText(element, { property, replace } = {}) {
if (!element) return ''
let text = element[property || 'textContent']
if ( replace )
text = text.replace(new RegExp(replace[0], 'g'), replace[1])
return text
}
function testObject(object, test) {
for ( let property in test ) {
// If Object, recurse; otherwise, test the value
let
value = object?.[property],
testValue = test[property],
passed =
( typeof testValue === 'object' ) ?
testObject(value, testValue)
: value === testValue
if ( !passed )
return false
}
return true
}
function setCaretPosition(element, position) {
let range = document.createRange()
let sel = window.getSelection()
range.setStart(element.firstChild, position)
range.collapse(true)
sel.removeAllRanges()
sel.addRange(range)
}
async function simulateTextInput(text) {
document.execCommand('insertText', false, text)
// // Split by newlines, exec insertText for each line, plus insertParagraph between each
// let lines = text.split(/\n+/)
// // console.log('Lines:', lines)
// while ( lines.length ) {
// document.execCommand('insertText', false, lines.shift())
// if ( lines.length )
// // document.insertHTML('<br/>')
// document.insertText(' / ')
// }
}
let tokensByEndpoint = {}
async function getSuggestion(prompt, { suffix }) {
let {
endpoint, auth, promptKey, otherBodyParams, arrayKey, resultKey, suffixKey
} = settings.api
// console.log({suffixKey, suffix})
// Get the suggestion from the external API.
let json = await(
await fetch(
endpoint,
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `${auth}`
},
body: JSON.stringify({
[promptKey]: prompt,
...( suffixKey && suffix ) ? { [suffixKey]: suffix } : {},
...otherBodyParams
})
}
)
).json()
let completion = get(arrayKey ? json[arrayKey][0] : json, resultKey)
// Log token stats via encode(...).length
console.log('Token stats:')
console.log('Prompt:', encode(prompt).length)
suffix && console.log('Suffix:', encode(suffix).length)
console.log('Completion:', encode(completion).length)
let totalTokens = [prompt, suffix, completion].map(encode).map(s => s.length).reduce((a, b) => a + b)
console.log('Total tokens:', totalTokens)
tokensByEndpoint[endpoint] = ( tokensByEndpoint[endpoint] || 0 ) + totalTokens
console.log('Tokens by endpoint:', tokensByEndpoint)
return completion
}
function saveSettings() {
// Save to chrome storage
chrome.storage.sync.set({ settings }, () => {
// console.log('Saved config to chrome storage:', settings)
})
}
enable()