Skip to content

Commit

Permalink
Added caller/function log prefix to dataProcess.stream()
Browse files Browse the repository at this point in the history
  • Loading branch information
adamlui committed Aug 18, 2024
1 parent 493229b commit 159a487
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 24 deletions.
13 changes: 7 additions & 6 deletions amazongpt/greasemonkey/amazongpt.user.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// @description Adds the magic of AI to Amazon shopping
// @author KudoAI
// @namespace https://kudoai.com
// @version 2024.8.18.1
// @version 2024.8.18.2
// @license MIT
// @icon https://amazongpt.kudoai.com/assets/images/icons/amazongpt/black-gold-teal/icon48.png?v=0fddfc7
// @icon64 https://amazongpt.kudoai.com/assets/images/icons/amazongpt/black-gold-teal/icon64.png?v=0fddfc7
Expand Down Expand Up @@ -1962,19 +1962,20 @@

stream(caller, stream) {
if (config.streamingDisabled || !config.proxyAPIenabled) return
const { failFlags = [], endpoint = apis[caller.api].endpoints.completions, expectedOrigin } = apis[caller.api],
const logPrefix = `get.${caller.name}() » dataProcess.stream() » `,
{ failFlags = [], endpoint = apis[caller.api].endpoints.completions, expectedOrigin } = apis[caller.api],
escapedAPIurls = [endpoint, expectedOrigin.url].map(str => str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')),
failFlagsURLs = new RegExp([...failFlags, ...escapedAPIurls].join('|')),
reader = stream.response.getReader() ; let accumulatedChunks = ''
reader.read().then(processStreamText).catch(err => consoleErr('Error processing stream', err.message))
reader.read().then(processStreamText).catch(err => consoleErr(logPrefix + 'Error processing stream', err.message))

function processStreamText({ done, value }) {
if (done) {
show.copyBtns() ; caller.status = 'done' ; caller.sender = null
api.clearTimedOut(caller.triedAPIs) ; caller.attemptCnt = null
return
} else if (failFlagsURLs.test(accumulatedChunks)) {
consoleErr('Response', accumulatedChunks)
consoleErr(logPrefix + 'Response', accumulatedChunks)
if (caller.status != 'done' && !caller.sender) api.tryNew(caller)
return
}
Expand All @@ -1998,11 +1999,11 @@
if (!caller.sender) caller.sender = caller.api // app is waiting, become sender
if (caller.sender == caller.api) show.reply(textToShow)
}
} catch (err) { consoleErr('Error showing stream', err.message) }
} catch (err) { consoleErr(logPrefix + 'Error showing stream', err.message) }
return reader.read().then(({ done, value }) => {
if (caller.sender == caller.api) // am designated sender, recurse
processStreamText({ done, value })
}).catch(err => consoleErr('Error reading stream', err.message))
}).catch(err => consoleErr(logPrefix + 'Error reading stream', err.message))
}
},

Expand Down
13 changes: 7 additions & 6 deletions bravegpt/greasemonkey/bravegpt.user.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@
// @description:zu Yengeza izimpendulo ze-AI ku-Brave Search (inikwa amandla yi-GPT-4o!)
// @author KudoAI
// @namespace https://kudoai.com
// @version 2024.8.18.1
// @version 2024.8.18.2
// @license MIT
// @icon https://media.bravegpt.com/images/icons/bravegpt/icon48.png?0a9e287
// @icon64 https://media.bravegpt.com/images/icons/bravegpt/icon64.png?0a9e287
Expand Down Expand Up @@ -2590,19 +2590,20 @@ setTimeout(async () => {

stream(caller, stream) {
if (config.streamingDisabled || !config.proxyAPIenabled) return
const { failFlags = [], endpoint = apis[caller.api].endpoints.completions, expectedOrigin } = apis[caller.api],
const logPrefix = `get.${caller.name}() » dataProcess.stream() » `,
{ failFlags = [], endpoint = apis[caller.api].endpoints.completions, expectedOrigin } = apis[caller.api],
escapedAPIurls = [endpoint, expectedOrigin.url].map(str => str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')),
failFlagsURLs = new RegExp([...failFlags, ...escapedAPIurls].join('|')),
reader = stream.response.getReader() ; let accumulatedChunks = ''
reader.read().then(processStreamText).catch(err => consoleErr('Error processing stream', err.message))
reader.read().then(processStreamText).catch(err => consoleErr(logPrefix + 'Error processing stream', err.message))

function processStreamText({ done, value }) {
if (done) {
show.copyBtns() ; caller.status = 'done' ; caller.sender = null
api.clearTimedOut(caller.triedAPIs) ; caller.attemptCnt = null
return
} else if (failFlagsURLs.test(accumulatedChunks)) {
consoleErr('Response', accumulatedChunks)
consoleErr(logPrefix + 'Response', accumulatedChunks)
if (caller.status != 'done' && !caller.sender) api.tryNew(caller)
return
}
Expand All @@ -2626,11 +2627,11 @@ setTimeout(async () => {
if (!caller.sender) caller.sender = caller.api // app is waiting, become sender
if (caller.sender == caller.api) show.reply(textToShow, footerContent)
}
} catch (err) { consoleErr('Error showing stream', err.message) }
} catch (err) { consoleErr(logPrefix + 'Error showing stream', err.message) }
return reader.read().then(({ done, value }) => {
if (caller.sender == caller.api) // am designated sender, recurse
processStreamText({ done, value })
}).catch(err => consoleErr('Error reading stream', err.message))
}).catch(err => consoleErr(logPrefix + 'Error reading stream', err.message))
}
},

Expand Down
13 changes: 7 additions & 6 deletions duckduckgpt/greasemonkey/duckduckgpt.user.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@
// @description:zu Yengeza izimpendulo ze-AI ku-DuckDuckGo (inikwa amandla yi-GPT-4o!)
// @author KudoAI
// @namespace https://kudoai.com
// @version 2024.8.18.1
// @version 2024.8.18.2
// @license MIT
// @icon https://media.ddgpt.com/images/icons/duckduckgpt/icon48.png?af89302
// @icon64 https://media.ddgpt.com/images/icons/duckduckgpt/icon64.png?af89302
Expand Down Expand Up @@ -2490,19 +2490,20 @@

stream(caller, stream) {
if (config.streamingDisabled || !config.proxyAPIenabled) return
const { failFlags = [], endpoint = apis[caller.api].endpoints.completions, expectedOrigin } = apis[caller.api],
const logPrefix = `get.${caller.name}() » dataProcess.stream() » `,
{ failFlags = [], endpoint = apis[caller.api].endpoints.completions, expectedOrigin } = apis[caller.api],
escapedAPIurls = [endpoint, expectedOrigin.url].map(str => str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')),
failFlagsURLs = new RegExp([...failFlags, ...escapedAPIurls].join('|')),
reader = stream.response.getReader() ; let accumulatedChunks = ''
reader.read().then(processStreamText).catch(err => consoleErr('Error processing stream', err.message))
reader.read().then(processStreamText).catch(err => consoleErr(logPrefix + 'Error processing stream', err.message))

function processStreamText({ done, value }) {
if (done) {
show.copyBtns() ; caller.status = 'done' ; caller.sender = null
api.clearTimedOut(caller.triedAPIs) ; caller.attemptCnt = null
return
} else if (failFlagsURLs.test(accumulatedChunks)) {
consoleErr('Response', accumulatedChunks)
consoleErr(logPrefix + 'Response', accumulatedChunks)
if (caller.status != 'done' && !caller.sender) api.tryNew(caller)
return
}
Expand All @@ -2526,11 +2527,11 @@
if (!caller.sender) caller.sender = caller.api // app is waiting, become sender
if (caller.sender == caller.api) show.reply(textToShow)
}
} catch (err) { consoleErr('Error showing stream', err.message) }
} catch (err) { consoleErr(logPrefix + 'Error showing stream', err.message) }
return reader.read().then(({ done, value }) => {
if (caller.sender == caller.api) // am designated sender, recurse
processStreamText({ done, value })
}).catch(err => consoleErr('Error reading stream', err.message))
}).catch(err => consoleErr(logPrefix + 'Error reading stream', err.message))
}
},

Expand Down
13 changes: 7 additions & 6 deletions googlegpt/greasemonkey/googlegpt.user.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@
// @description:zu Yengeza izimpendulo ze-AI ku-Google Search (inikwa amandla yi-Google Gemma + GPT-4o!)
// @author KudoAI
// @namespace https://kudoai.com
// @version 2024.8.18.1
// @version 2024.8.18.2
// @license MIT
// @icon https://media.googlegpt.io/images/icons/googlegpt/black/icon48.png?8652a6e
// @icon64 https://media.googlegpt.io/images/icons/googlegpt/black/icon64.png?8652a6e
Expand Down Expand Up @@ -2807,19 +2807,20 @@

stream(caller, stream) {
if (config.streamingDisabled || !config.proxyAPIenabled) return
const { failFlags = [], endpoint = apis[caller.api].endpoints.completions, expectedOrigin } = apis[caller.api],
const logPrefix = `get.${caller.name}() » dataProcess.stream() » `,
{ failFlags = [], endpoint = apis[caller.api].endpoints.completions, expectedOrigin } = apis[caller.api],
escapedAPIurls = [endpoint, expectedOrigin.url].map(str => str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')),
failFlagsURLs = new RegExp([...failFlags, ...escapedAPIurls].join('|')),
reader = stream.response.getReader() ; let accumulatedChunks = ''
reader.read().then(processStreamText).catch(err => consoleErr('Error processing stream', err.message))
reader.read().then(processStreamText).catch(err => consoleErr(logPrefix + 'Error processing stream', err.message))

function processStreamText({ done, value }) {
if (done) {
show.copyBtns() ; caller.status = 'done' ; caller.sender = null
api.clearTimedOut(caller.triedAPIs) ; caller.attemptCnt = null
return
} else if (failFlagsURLs.test(accumulatedChunks)) {
consoleErr('Response', accumulatedChunks)
consoleErr(logPrefix + 'Response', accumulatedChunks)
if (caller.status != 'done' && !caller.sender) api.tryNew(caller)
return
}
Expand All @@ -2843,11 +2844,11 @@
if (!caller.sender) caller.sender = caller.api // app is waiting, become sender
if (caller.sender == caller.api) show.reply(textToShow, footerContent)
}
} catch (err) { consoleErr('Error showing stream', err.message) }
} catch (err) { consoleErr(logPrefix + 'Error showing stream', err.message) }
return reader.read().then(({ done, value }) => {
if (caller.sender == caller.api) // am designated sender, recurse
processStreamText({ done, value })
}).catch(err => consoleErr('Error reading stream', err.message))
}).catch(err => consoleErr(logPrefix + 'Error reading stream', err.message))
}
},

Expand Down

0 comments on commit 159a487

Please sign in to comment.