Skip to content

Commit 33a46c4

Browse files
committed
Improved dataProcess.text()
± Remove unneeded processing for APIs that don't require it ± Renamed `respText` to `textToShow` to distinguish from `resp.responseText` ± Changed `let lastObj` in GPTforLove processing to `const lastChunk` ± Shortened `extractedData` init in MixerBox AI processing
1 parent 06646ba commit 33a46c4

File tree

4 files changed

+75
-111
lines changed

4 files changed

+75
-111
lines changed

amazongpt/greasemonkey/amazongpt.user.js

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// @description Adds the magic of AI to Amazon shopping
44
// @author KudoAI
55
// @namespace https://kudoai.com
6-
// @version 2025.1.19.3
6+
// @version 2025.1.19.4
77
// @license MIT
88
// @icon https://amazongpt.kudoai.com/assets/images/icons/amazongpt/black-gold-teal/icon48.png?v=0fddfc7
99
// @icon64 https://amazongpt.kudoai.com/assets/images/icons/amazongpt/black-gold-teal/icon64.png?v=0fddfc7
@@ -2659,7 +2659,7 @@
26592659
} else textToShow = accumulatedChunks
26602660
const failMatch = failFlagsAndURLs.exec(textToShow)
26612661
if (failMatch) {
2662-
log.dev('Response text', textToShow)
2662+
log.dev('Text to show', textToShow)
26632663
log.error('Fail flag detected', `'${failMatch[0]}'`)
26642664
if (caller.status != 'done' && !caller.sender) api.tryNew(caller)
26652665
return
@@ -2692,7 +2692,7 @@
26922692
if (caller == get.reply && config.proxyAPIenabled && !config.streamingDisabled
26932693
|| caller.status == 'done') return
26942694
log.caller = `get.${caller.name}() » dataProcess.text()`
2695-
const failFlagsAndURLs = this.initFailFlags(callerAPI) ; let respText = ''
2695+
const failFlagsAndURLs = this.initFailFlags(callerAPI) ; let textToShow = ''
26962696
if (resp.status != 200) {
26972697
log.error('Response status', resp.status)
26982698
log.info('Response text', resp.response || resp.responseText)
@@ -2710,50 +2710,41 @@
27102710
appAlert('openAInotWorking', 'suggestProxy')
27112711
} else {
27122712
try { // to show response
2713-
respText = JSON.parse(resp.response).choices[0].message.content
2713+
textToShow = JSON.parse(resp.response).choices[0].message.content
27142714
handleProcessCompletion()
27152715
} catch (err) { handleProcessError(err) }
27162716
}
27172717
} else if (resp.responseText) { // show response
2718-
if (/AIchatOS|FREEGPT|ToYaml/.test(callerAPI)) {
2718+
if (callerAPI == 'GPTforLove') {
27192719
try {
2720-
const text = resp.responseText, chunkSize = 1024
2721-
let currentIdx = 0
2722-
while (currentIdx < text.length) {
2723-
const chunk = text.substring(currentIdx, currentIdx + chunkSize)
2724-
respText += chunk ; currentIdx += chunkSize
2725-
}
2726-
handleProcessCompletion()
2727-
} catch (err) { handleProcessError(err) }
2728-
} else if (callerAPI == 'GPTforLove') {
2729-
try {
2730-
let chunks = resp.responseText.trim().split('\n'),
2731-
lastObj = JSON.parse(chunks[chunks.length - 1])
2732-
if (lastObj.id) apis.GPTforLove.parentID = lastObj.id
2733-
respText = lastObj.text ; handleProcessCompletion()
2720+
let chunks = resp.responseText.trim().split('\n')
2721+
const lastChunk = JSON.parse(chunks[chunks.length -1])
2722+
if (lastChunk.id) apis.GPTforLove.parentID = lastChunk.id
2723+
textToShow = lastChunk.text ; handleProcessCompletion()
27342724
} catch (err) { handleProcessError(err) }
27352725
} else if (callerAPI == 'MixerBox AI') {
27362726
try {
2737-
const extractedData = Array.from(resp.responseText.matchAll(/data:(.*)/g), match => match[1]
2727+
const extractedData = [...resp.responseText.matchAll(/data:(.*)/g)].map(match => match[1]
27382728
.replace(/\[SPACE\]/g, ' ').replace(/\[NEWLINE\]/g, '\n'))
27392729
.filter(match => !/message_(?:start|end)|done/.test(match))
2740-
respText = extractedData.join('') ; handleProcessCompletion()
2730+
textToShow = extractedData.join('') ; handleProcessCompletion()
27412731
} catch (err) { handleProcessError(err) }
2742-
}
2732+
} else { // no processing required for all other APIs
2733+
textToShow = resp.responseText ; handleProcessCompletion() }
27432734
} else if (caller.status != 'done') { // proxy 200 response failure
27442735
log.info('Response text', resp.responseText) ; api.tryNew(caller) }
27452736

27462737
function handleProcessCompletion() {
27472738
if (caller.status != 'done') {
2748-
log.dev('Response text', respText)
2749-
const failMatch = failFlagsAndURLs.exec(respText)
2750-
if (!respText || failMatch || /^(?:\{|event:)/.test(respText)) {
2739+
log.dev('Text to show', textToShow)
2740+
const failMatch = failFlagsAndURLs.exec(textToShow)
2741+
if (!textToShow || failMatch || /^(?:\{|event:)/.test(textToShow)) {
27512742
if (failMatch) log.error('Fail flag detected', `'${failMatch[0]}'`)
27522743
api.tryNew(caller)
27532744
} else {
27542745
caller.status = 'done' ; api.clearTimedOut(caller.triedAPIs) ; caller.attemptCnt = null
2755-
respText = respText.replace(apis[callerAPI].watermark, '').trim()
2756-
show.reply(respText) ; show.replyCornerBtns()
2746+
textToShow = textToShow.replace(apis[callerAPI].watermark, '').trim()
2747+
show.reply(textToShow) ; show.replyCornerBtns()
27572748
}}}
27582749

27592750
function handleProcessError(err) { // suggest proxy or try diff API

bravegpt/greasemonkey/bravegpt.user.js

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@
148148
// @description:zu Yengeza izimpendulo ze-AI ku-Brave Search (inikwa amandla yi-GPT-4o!)
149149
// @author KudoAI
150150
// @namespace https://kudoai.com
151-
// @version 2025.1.19.3
151+
// @version 2025.1.19.4
152152
// @license MIT
153153
// @icon https://assets.bravegpt.com/images/icons/bravegpt/icon48.png?v=df624b0
154154
// @icon64 https://assets.bravegpt.com/images/icons/bravegpt/icon64.png?v=df624b0
@@ -3410,7 +3410,7 @@
34103410
} else textToShow = accumulatedChunks
34113411
const failMatch = failFlagsAndURLs.exec(textToShow)
34123412
if (failMatch) {
3413-
log.dev('Response text', textToShow)
3413+
log.dev('Text to show', textToShow)
34143414
log.error('Fail flag detected', `'${failMatch[0]}'`)
34153415
if (caller.status != 'done' && !caller.sender) api.tryNew(caller)
34163416
return
@@ -3443,7 +3443,7 @@
34433443
if (caller == get.reply && config.proxyAPIenabled && !config.streamingDisabled
34443444
|| caller.status == 'done') return
34453445
log.caller = `get.${caller.name}() » dataProcess.text()`
3446-
const failFlagsAndURLs = this.initFailFlags(callerAPI) ; let respText = ''
3446+
const failFlagsAndURLs = this.initFailFlags(callerAPI) ; let textToShow = ''
34473447
if (resp.status != 200) {
34483448
log.error('Response status', resp.status)
34493449
log.info('Response text', resp.response || resp.responseText)
@@ -3462,51 +3462,42 @@
34623462
else api.tryNew(caller)
34633463
} else {
34643464
try { // to show response or return related queries
3465-
respText = JSON.parse(resp.response).choices[0].message.content
3465+
textToShow = JSON.parse(resp.response).choices[0].message.content
34663466
handleProcessCompletion()
34673467
} catch (err) { handleProcessError(err) }
34683468
}
34693469
} else if (resp.responseText) { // show response or return related queries
3470-
if (/AIchatOS|FREEGPT|ToYaml/.test(callerAPI)) {
3470+
if (callerAPI == 'GPTforLove') {
34713471
try {
3472-
const text = resp.responseText, chunkSize = 1024
3473-
let currentIdx = 0
3474-
while (currentIdx < text.length) {
3475-
const chunk = text.substring(currentIdx, currentIdx + chunkSize)
3476-
respText += chunk ; currentIdx += chunkSize
3477-
}
3478-
handleProcessCompletion()
3479-
} catch (err) { handleProcessError(err) }
3480-
} else if (callerAPI == 'GPTforLove') {
3481-
try {
3482-
let chunks = resp.responseText.trim().split('\n'),
3483-
lastObj = JSON.parse(chunks[chunks.length - 1])
3484-
if (lastObj.id) apis.GPTforLove.parentID = lastObj.id
3485-
respText = lastObj.text ; handleProcessCompletion()
3472+
let chunks = resp.responseText.trim().split('\n')
3473+
const lastChunk = JSON.parse(chunks[chunks.length -1])
3474+
if (lastChunk.id) apis.GPTforLove.parentID = lastChunk.id
3475+
textToShow = lastChunk.text ; handleProcessCompletion()
34863476
} catch (err) { handleProcessError(err) }
34873477
} else if (callerAPI == 'MixerBox AI') {
34883478
try {
3489-
const extractedData = Array.from(resp.responseText.matchAll(/data:(.*)/g), match => match[1]
3479+
const extractedData = [...resp.responseText.matchAll(/data:(.*)/g)].map(match => match[1]
34903480
.replace(/\[SPACE\]/g, ' ').replace(/\[NEWLINE\]/g, '\n'))
34913481
.filter(match => !/message_(?:start|end)|done/.test(match))
3492-
respText = extractedData.join('') ; handleProcessCompletion()
3482+
textToShow = extractedData.join('') ; handleProcessCompletion()
34933483
} catch (err) { handleProcessError(err) }
3494-
}
3484+
} else { // no processing required for all other APIs
3485+
textToShow = resp.responseText ; handleProcessCompletion() }
34953486
} else if (caller.status != 'done') { // proxy 200 response failure
34963487
log.info('Response text', resp.responseText) ; api.tryNew(caller) }
34973488

34983489
function handleProcessCompletion() {
34993490
if (caller.status != 'done') {
3500-
log.dev('Response text', respText)
3501-
const failMatch = failFlagsAndURLs.exec(respText)
3502-
if (!respText || failMatch || /^(?:\{|event:)/.test(respText)) {
3491+
log.dev('Text to show', textToShow)
3492+
const failMatch = failFlagsAndURLs.exec(textToShow)
3493+
if (!textToShow || failMatch || /^(?:\{|event:)/.test(textToShow)) {
35033494
if (failMatch) log.error('Fail flag detected', `'${failMatch[0]}'`)
35043495
api.tryNew(caller)
35053496
} else {
35063497
caller.status = 'done' ; api.clearTimedOut(caller.triedAPIs) ; caller.attemptCnt = null
3507-
respText = respText.replace(apis[callerAPI].watermark, '').trim()
3508-
if (caller == get.reply) { show.reply(respText, footerContent) ; show.replyCornerBtns() }
3509-
else resolve(arrayify(respText))
3498+
textToShow = textToShow.replace(apis[callerAPI].watermark, '').trim()
3499+
if (caller == get.reply) { show.reply(textToShow, footerContent) ; show.replyCornerBtns() }
3500+
else resolve(arrayify(textToShow))
35103501
}
35113502
}
35123503
}

duckduckgpt/greasemonkey/duckduckgpt.user.js

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@
148148
// @description:zu Yengeza izimpendulo ze-AI ku-DuckDuckGo (inikwa amandla yi-GPT-4o!)
149149
// @author KudoAI
150150
// @namespace https://kudoai.com
151-
// @version 2025.1.19.3
151+
// @version 2025.1.19.4
152152
// @license MIT
153153
// @icon https://assets.ddgpt.com/images/icons/duckduckgpt/icon48.png?v=06af076
154154
// @icon64 https://assets.ddgpt.com/images/icons/duckduckgpt/icon64.png?v=06af076
@@ -3295,7 +3295,7 @@
32953295
} else textToShow = accumulatedChunks
32963296
const failMatch = failFlagsAndURLs.exec(textToShow)
32973297
if (failMatch) {
3298-
log.dev('Response text', textToShow)
3298+
log.dev('Text to show', textToShow)
32993299
log.error('Fail flag detected', `'${failMatch[0]}'`)
33003300
if (caller.status != 'done' && !caller.sender) api.tryNew(caller)
33013301
return
@@ -3328,7 +3328,7 @@
33283328
if (caller == get.reply && config.proxyAPIenabled && !config.streamingDisabled
33293329
|| caller.status == 'done') return
33303330
log.caller = `get.${caller.name}() » dataProcess.text()`
3331-
const failFlagsAndURLs = this.initFailFlags(callerAPI) ; let respText = ''
3331+
const failFlagsAndURLs = this.initFailFlags(callerAPI) ; let textToShow = ''
33323332
if (resp.status != 200) {
33333333
log.error('Response status', resp.status)
33343334
log.info('Response text', resp.response || resp.responseText)
@@ -3347,51 +3347,42 @@
33473347
else api.tryNew(caller)
33483348
} else {
33493349
try { // to show response or return related queries
3350-
respText = JSON.parse(resp.response).choices[0].message.content
3350+
textToShow = JSON.parse(resp.response).choices[0].message.content
33513351
handleProcessCompletion()
33523352
} catch (err) { handleProcessError(err) }
33533353
}
33543354
} else if (resp.responseText) { // show response or return related queries
3355-
if (/AIchatOS|FREEGPT|ToYaml/.test(callerAPI)) {
3355+
if (callerAPI == 'GPTforLove') {
33563356
try {
3357-
const text = resp.responseText, chunkSize = 1024
3358-
let currentIdx = 0
3359-
while (currentIdx < text.length) {
3360-
const chunk = text.substring(currentIdx, currentIdx + chunkSize)
3361-
respText += chunk ; currentIdx += chunkSize
3362-
}
3363-
handleProcessCompletion()
3364-
} catch (err) { handleProcessError(err) }
3365-
} else if (callerAPI == 'GPTforLove') {
3366-
try {
3367-
let chunks = resp.responseText.trim().split('\n'),
3368-
lastObj = JSON.parse(chunks[chunks.length - 1])
3369-
if (lastObj.id) apis.GPTforLove.parentID = lastObj.id
3370-
respText = lastObj.text ; handleProcessCompletion()
3357+
let chunks = resp.responseText.trim().split('\n')
3358+
const lastChunk = JSON.parse(chunks[chunks.length -1])
3359+
if (lastChunk.id) apis.GPTforLove.parentID = lastChunk.id
3360+
textToShow = lastChunk.text ; handleProcessCompletion()
33713361
} catch (err) { handleProcessError(err) }
33723362
} else if (callerAPI == 'MixerBox AI') {
33733363
try {
3374-
const extractedData = Array.from(resp.responseText.matchAll(/data:(.*)/g), match => match[1]
3364+
const extractedData = [...resp.responseText.matchAll(/data:(.*)/g)].map(match => match[1]
33753365
.replace(/\[SPACE\]/g, ' ').replace(/\[NEWLINE\]/g, '\n'))
33763366
.filter(match => !/message_(?:start|end)|done/.test(match))
3377-
respText = extractedData.join('') ; handleProcessCompletion()
3367+
textToShow = extractedData.join('') ; handleProcessCompletion()
33783368
} catch (err) { handleProcessError(err) }
3379-
}
3369+
} else { // no processing required for all other APIs
3370+
textToShow = resp.responseText ; handleProcessCompletion() }
33803371
} else if (caller.status != 'done') { // proxy 200 response failure
33813372
log.info('Response text', resp.responseText) ; api.tryNew(caller) }
33823373

33833374
function handleProcessCompletion() {
33843375
if (caller.status != 'done') {
3385-
log.dev('Response text', respText)
3386-
const failMatch = failFlagsAndURLs.exec(respText)
3387-
if (!respText || failMatch || /^(?:\{|event:)/.test(respText)) {
3376+
log.dev('Text to show', textToShow)
3377+
const failMatch = failFlagsAndURLs.exec(textToShow)
3378+
if (!textToShow || failMatch || /^(?:\{|event:)/.test(textToShow)) {
33883379
if (failMatch) log.error('Fail flag detected', `'${failMatch[0]}'`)
33893380
api.tryNew(caller)
33903381
} else {
33913382
caller.status = 'done' ; api.clearTimedOut(caller.triedAPIs) ; caller.attemptCnt = null
3392-
respText = respText.replace(apis[callerAPI].watermark, '').trim()
3393-
if (caller == get.reply) { show.reply(respText) ; show.replyCornerBtns() }
3394-
else resolve(arrayify(respText))
3383+
textToShow = textToShow.replace(apis[callerAPI].watermark, '').trim()
3384+
if (caller == get.reply) { show.reply(textToShow) ; show.replyCornerBtns() }
3385+
else resolve(arrayify(textToShow))
33953386
}
33963387
}
33973388
}

0 commit comments

Comments
 (0)