Skip to content

Commit 31a45b0

Browse files
committed
Condensed processStreamText(), improved comments
1 parent 505635c commit 31a45b0

File tree

4 files changed

+48
-52
lines changed

4 files changed

+48
-52
lines changed

amazongpt/greasemonkey/amazongpt.user.js

Lines changed: 12 additions & 13 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.21.5
6+
// @version 2025.1.21.6
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
@@ -2656,23 +2656,22 @@
26562656
// Handle stream done
26572657
const chunk = new TextDecoder('utf8').decode(new Uint8Array(value))
26582658
if (done || chunk.includes(apis[callerAPI].watermark)) return handleProcessCompletion()
2659-
if (env.browser.isChromium) { // clear/add timeout since reader.read() doesn't signal done
2659+
if (env.browser.isChromium) { // clear/add timeout since ReadableStream.getReader() doesn't signal done
26602660
clearTimeout(this.timeout) ; this.timeout = setTimeout(handleProcessCompletion, 500) }
26612661

26622662
// Process/accumulate chunk
26632663
let replyChunk = ''
2664-
if (callerAPI == 'GPTforLove') { // extract parentID + chunk.delta
2665-
const chunkLines = chunk.trim().split('\n'),
2666-
chunkObjs = chunkLines.map(line => JSON.parse(line))
2667-
if (chunkObjs[0].id) apis.GPTforLove.parentID = chunkObjs[0].id // for contextual replies
2668-
chunkObjs.forEach(obj =>
2669-
replyChunk += obj.delta // AI reply
2664+
if (callerAPI == 'GPTforLove') { // extract parentID + deltas
2665+
const chunkObjs = chunk.trim().split('\n').map(line => JSON.parse(line))
2666+
apis.GPTforLove.parentID = chunkObjs[0].id || null // for contextual replies
2667+
chunkObjs.forEach(obj => // accumulate replyChunk
2668+
replyChunk += obj.delta // AI reply text
26702669
|| JSON.stringify(obj)) // error response for fail flag check
2671-
} else if (callerAPI == 'MixerBox AI') { // extract/normalize chunk.data
2672-
const extractedChunks = Array.from(chunk.matchAll(/data:(.*)/g), match => match[1]
2673-
.replace(/\[SPACE\]/g, ' ').replace(/\[NEWLINE\]/g, '\n'))
2674-
.filter(match => !/message_(?:start|end)|done/.test(match))
2675-
replyChunk = extractedChunks.join('')
2670+
} else if (callerAPI == 'MixerBox AI') { // extract/normalize AI reply data
2671+
replyChunk = Array.from(chunk.matchAll(/data:(.*)/g), match => match[1] // arrayify data
2672+
.replace(/\[SPACE\]/g, ' ').replace(/\[NEWLINE\]/g, '\n')) // normalize whitespace
2673+
.filter(match => !/message_(?:start|end)|done/.test(match)) // exclude signals
2674+
.join('') // stringify AI reply text
26762675
}
26772676
textToShow += replyChunk
26782677

bravegpt/greasemonkey/bravegpt.user.js

Lines changed: 12 additions & 13 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.21.5
151+
// @version 2025.1.21.6
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
@@ -3406,23 +3406,22 @@
34063406
// Handle stream done
34073407
const chunk = new TextDecoder('utf8').decode(new Uint8Array(value))
34083408
if (done || chunk.includes(apis[callerAPI].watermark)) return handleProcessCompletion()
3409-
if (env.browser.isChromium) { // clear/add timeout since reader.read() doesn't signal done
3409+
if (env.browser.isChromium) { // clear/add timeout since ReadableStream.getReader() doesn't signal done
34103410
clearTimeout(this.timeout) ; this.timeout = setTimeout(handleProcessCompletion, 500) }
34113411

34123412
// Process/accumulate chunk
34133413
let replyChunk = ''
3414-
if (callerAPI == 'GPTforLove') { // extract parentID + chunk.delta
3415-
const chunkLines = chunk.trim().split('\n'),
3416-
chunkObjs = chunkLines.map(line => JSON.parse(line))
3417-
if (chunkObjs[0].id) apis.GPTforLove.parentID = chunkObjs[0].id // for contextual replies
3418-
chunkObjs.forEach(obj =>
3419-
replyChunk += obj.delta // AI reply
3414+
if (callerAPI == 'GPTforLove') { // extract parentID + deltas
3415+
const chunkObjs = chunk.trim().split('\n').map(line => JSON.parse(line))
3416+
apis.GPTforLove.parentID = chunkObjs[0].id || null // for contextual replies
3417+
chunkObjs.forEach(obj => // accumulate replyChunk
3418+
replyChunk += obj.delta // AI reply text
34203419
|| JSON.stringify(obj)) // error response for fail flag check
3421-
} else if (callerAPI == 'MixerBox AI') { // extract/normalize chunk.data
3422-
const extractedChunks = Array.from(chunk.matchAll(/data:(.*)/g), match => match[1]
3423-
.replace(/\[SPACE\]/g, ' ').replace(/\[NEWLINE\]/g, '\n'))
3424-
.filter(match => !/message_(?:start|end)|done/.test(match))
3425-
replyChunk = extractedChunks.join('')
3420+
} else if (callerAPI == 'MixerBox AI') { // extract/normalize AI reply data
3421+
replyChunk = Array.from(chunk.matchAll(/data:(.*)/g), match => match[1] // arrayify data
3422+
.replace(/\[SPACE\]/g, ' ').replace(/\[NEWLINE\]/g, '\n')) // normalize whitespace
3423+
.filter(match => !/message_(?:start|end)|done/.test(match)) // exclude signals
3424+
.join('') // stringify AI reply text
34263425
}
34273426
textToShow += replyChunk
34283427

duckduckgpt/greasemonkey/duckduckgpt.user.js

Lines changed: 12 additions & 13 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.21.7
151+
// @version 2025.1.21.8
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
@@ -3290,23 +3290,22 @@
32903290
// Handle stream done
32913291
const chunk = new TextDecoder('utf8').decode(new Uint8Array(value))
32923292
if (done || chunk.includes(apis[callerAPI].watermark)) return handleProcessCompletion()
3293-
if (env.browser.isChromium) { // clear/add timeout since reader.read() doesn't signal done
3293+
if (env.browser.isChromium) { // clear/add timeout since ReadableStream.getReader() doesn't signal done
32943294
clearTimeout(this.timeout) ; this.timeout = setTimeout(handleProcessCompletion, 500) }
32953295

32963296
// Process/accumulate chunk
32973297
let replyChunk = ''
3298-
if (callerAPI == 'GPTforLove') { // extract parentID + chunk.delta
3299-
const chunkLines = chunk.trim().split('\n'),
3300-
chunkObjs = chunkLines.map(line => JSON.parse(line))
3301-
if (chunkObjs[0].id) apis.GPTforLove.parentID = chunkObjs[0].id // for contextual replies
3302-
chunkObjs.forEach(obj =>
3303-
replyChunk += obj.delta // AI reply
3298+
if (callerAPI == 'GPTforLove') { // extract parentID + deltas
3299+
const chunkObjs = chunk.trim().split('\n').map(line => JSON.parse(line))
3300+
apis.GPTforLove.parentID = chunkObjs[0].id || null // for contextual replies
3301+
chunkObjs.forEach(obj => // accumulate replyChunk
3302+
replyChunk += obj.delta // AI reply text
33043303
|| JSON.stringify(obj)) // error response for fail flag check
3305-
} else if (callerAPI == 'MixerBox AI') { // extract/normalize chunk.data
3306-
const extractedChunks = Array.from(chunk.matchAll(/data:(.*)/g), match => match[1]
3307-
.replace(/\[SPACE\]/g, ' ').replace(/\[NEWLINE\]/g, '\n'))
3308-
.filter(match => !/message_(?:start|end)|done/.test(match))
3309-
replyChunk = extractedChunks.join('')
3304+
} else if (callerAPI == 'MixerBox AI') { // extract/normalize AI reply data
3305+
replyChunk = Array.from(chunk.matchAll(/data:(.*)/g), match => match[1] // arrayify data
3306+
.replace(/\[SPACE\]/g, ' ').replace(/\[NEWLINE\]/g, '\n')) // normalize whitespace
3307+
.filter(match => !/message_(?:start|end)|done/.test(match)) // exclude signals
3308+
.join('') // stringify AI reply text
33103309
}
33113310
textToShow += replyChunk
33123311

googlegpt/greasemonkey/googlegpt.user.js

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@
149149
// @description:zu Yengeza izimpendulo ze-AI ku-Google Search (inikwa amandla yi-Google Gemma + GPT-4o!)
150150
// @author KudoAI
151151
// @namespace https://kudoai.com
152-
// @version 2025.1.21.5
152+
// @version 2025.1.21.6
153153
// @license MIT
154154
// @icon https://assets.googlegpt.io/images/icons/googlegpt/black/icon48.png?v=59409b2
155155
// @icon64 https://assets.googlegpt.io/images/icons/googlegpt/black/icon64.png?v=59409b2
@@ -3586,23 +3586,22 @@
35863586
// Handle stream done
35873587
const chunk = new TextDecoder('utf8').decode(new Uint8Array(value))
35883588
if (done || chunk.includes(apis[callerAPI].watermark)) return handleProcessCompletion()
3589-
if (env.browser.isChromium) { // clear/add timeout since reader.read() doesn't signal done
3589+
if (env.browser.isChromium) { // clear/add timeout since ReadableStream.getReader() doesn't signal done
35903590
clearTimeout(this.timeout) ; this.timeout = setTimeout(handleProcessCompletion, 500) }
35913591

35923592
// Process/accumulate chunk
35933593
let replyChunk = ''
3594-
if (callerAPI == 'GPTforLove') { // extract parentID + chunk.delta
3595-
const chunkLines = chunk.trim().split('\n'),
3596-
chunkObjs = chunkLines.map(line => JSON.parse(line))
3597-
if (chunkObjs[0].id) apis.GPTforLove.parentID = chunkObjs[0].id // for contextual replies
3598-
chunkObjs.forEach(obj =>
3599-
replyChunk += obj.delta // AI reply
3594+
if (callerAPI == 'GPTforLove') { // extract parentID + deltas
3595+
const chunkObjs = chunk.trim().split('\n').map(line => JSON.parse(line))
3596+
apis.GPTforLove.parentID = chunkObjs[0].id || null // for contextual replies
3597+
chunkObjs.forEach(obj => // accumulate replyChunk
3598+
replyChunk += obj.delta // AI reply text
36003599
|| JSON.stringify(obj)) // error response for fail flag check
3601-
} else if (callerAPI == 'MixerBox AI') { // extract/normalize chunk.data
3602-
const extractedChunks = Array.from(chunk.matchAll(/data:(.*)/g), match => match[1]
3603-
.replace(/\[SPACE\]/g, ' ').replace(/\[NEWLINE\]/g, '\n'))
3604-
.filter(match => !/message_(?:start|end)|done/.test(match))
3605-
replyChunk = extractedChunks.join('')
3600+
} else if (callerAPI == 'MixerBox AI') { // extract/normalize AI reply data
3601+
replyChunk = Array.from(chunk.matchAll(/data:(.*)/g), match => match[1] // arrayify data
3602+
.replace(/\[SPACE\]/g, ' ').replace(/\[NEWLINE\]/g, '\n')) // normalize whitespace
3603+
.filter(match => !/message_(?:start|end)|done/.test(match)) // exclude signals
3604+
.join('') // stringify AI reply text
36063605
}
36073606
textToShow += replyChunk
36083607

0 commit comments

Comments
 (0)