Skip to content

Commit

Permalink
Bumped ai-chat-apis.json, utilized parsingRequired
Browse files Browse the repository at this point in the history
  • Loading branch information
adamlui committed Jan 26, 2025
1 parent 556cbfe commit 5856fd9
Show file tree
Hide file tree
Showing 4 changed files with 172 additions and 156 deletions.
82 changes: 43 additions & 39 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 2025.1.26.1
// @version 2025.1.26.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 @@ -226,7 +226,7 @@

// Init API data
const apis = Object.assign(Object.create(null), await new Promise(resolve => xhr({
method: 'GET', url: 'https://assets.aiwebextensions.com/data/ai-chat-apis.json?v=b529a64',
method: 'GET', url: 'https://assets.aiwebextensions.com/data/ai-chat-apis.json?v=556cbfe',
onload: resp => resolve(JSON.parse(resp.responseText))
})))
apis.AIchatOS.userID = '#/chat/' + Date.now()
Expand Down Expand Up @@ -2655,25 +2655,27 @@
clearTimeout(this.timeout) ; this.timeout = setTimeout(handleProcessCompletion, 1500) }

// Process/accumulate reply chunk
let replyChunk = ''
if (callerAPI == 'GPTforLove') { // extract parentID + deltas
const chunkObjs = respChunk.trim().split('\n').map(line => JSON.parse(line))
if (typeof chunkObjs[0].text == 'undefined') // error response
replyChunk = JSON.stringify(chunkObjs[0]) // for fail flag check
else { // AI response
apis.GPTforLove.parentID = chunkObjs[0].id || null // for contextual replies
chunkObjs.forEach(obj => replyChunk += obj.delta || '') // accumulate AI reply text
if (respChunk.includes('"finish_reason":"stop"')) isDone = true
}
} else if (callerAPI == 'MixerBox AI') { // extract/normalize AI reply data
replyChunk = [...respChunk.matchAll(/data:(.*)/g)] // arrayify data
.filter(match => !/message_(?:start|end)|done/.test(match)) // exclude signals
.map(match => // normalize whitespace
match[1].replace(/\[SPACE\]/g, ' ').replace(/\[NEWLINE\]/g, '\n'))
.join('') // stringify AI reply text
if (/data:(?:message_end|done)/.test(respChunk)) isDone = true
} else replyChunk = respChunk // no processing required for all other APIs
textToShow += replyChunk
if (!apis[callerAPI].parsingRequired) textToShow += respChunk
else { // parse structured chunk(s)
let replyChunk = ''
if (callerAPI == 'GPTforLove') { // extract parentID + deltas
const chunkObjs = respChunk.trim().split('\n').map(line => JSON.parse(line))
if (typeof chunkObjs[0].text == 'undefined') // error response
replyChunk = JSON.stringify(chunkObjs[0]) // for fail flag check
else { // AI response
apis.GPTforLove.parentID = chunkObjs[0].id || null // for contextual replies
chunkObjs.forEach(obj => replyChunk += obj.delta || '') // accumulate AI reply text
if (respChunk.includes('"finish_reason":"stop"')) isDone = true
}
} else if (callerAPI == 'MixerBox AI') { // extract/normalize AI reply data
replyChunk = [...respChunk.matchAll(/data:(.*)/g)] // arrayify data
.filter(match => !/message_(?:start|end)|done/.test(match)) // exclude signals
.map(match => // normalize whitespace
match[1].replace(/\[SPACE\]/g, ' ').replace(/\[NEWLINE\]/g, '\n'))
.join('') // stringify AI reply text
if (/data:(?:message_end|done)/.test(respChunk)) isDone = true
} textToShow += replyChunk
}

// Show accumulated reply chunks
try {
Expand Down Expand Up @@ -2737,24 +2739,26 @@
} catch (err) { handleProcessError(err) }
}
} else if (resp.responseText) { // show response from proxy API
if (callerAPI == 'GPTforLove') {
try {
const chunkLines = resp.responseText.trim().split('\n'),
lastChunkObj = JSON.parse(chunkLines[chunkLines.length -1])
apis.GPTforLove.parentID = lastChunkObj.id || null
textToShow = lastChunkObj.text ; handleProcessCompletion()
} catch (err) { handleProcessError(err) }
} else if (callerAPI == 'MixerBox AI') {
try {
textToShow = [...resp.responseText.matchAll(/data:(.*)/g)] // arrayify data
.filter(match => !/message_(?:start|end)|done/.test(match)) // exclude signals
.map(match => // normalize whitespace
match[1].replace(/\[SPACE\]/g, ' ').replace(/\[NEWLINE\]/g, '\n'))
.join('') // stringify AI reply text
handleProcessCompletion()
} catch (err) { handleProcessError(err) }
} else { // no processing required for all other APIs
textToShow = resp.responseText ; handleProcessCompletion() }
if (!apis[callerAPI].parsingRequired) { textToShow = resp.responseText ; handleProcessCompletion }
else { // parse structured responseText
if (callerAPI == 'GPTforLove') {
try {
const chunkLines = resp.responseText.trim().split('\n'),
lastChunkObj = JSON.parse(chunkLines[chunkLines.length -1])
apis.GPTforLove.parentID = lastChunkObj.id || null
textToShow = lastChunkObj.text ; handleProcessCompletion()
} catch (err) { handleProcessError(err) }
} else if (callerAPI == 'MixerBox AI') {
try {
textToShow = [...resp.responseText.matchAll(/data:(.*)/g)] // arrayify data
.filter(match => !/message_(?:start|end)|done/.test(match)) // exclude signals
.map(match => // normalize whitespace
match[1].replace(/\[SPACE\]/g, ' ').replace(/\[NEWLINE\]/g, '\n'))
.join('') // stringify AI reply text
handleProcessCompletion()
} catch (err) { handleProcessError(err) }
}
}
} else if (caller.status != 'done') { // proxy 200 response failure
log.info('Response text', resp.responseText) ; api.tryNew(caller) }

Expand Down
82 changes: 43 additions & 39 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 2025.1.26.1
// @version 2025.1.26.2
// @license MIT
// @icon https://assets.bravegpt.com/images/icons/bravegpt/icon48.png?v=df624b0
// @icon64 https://assets.bravegpt.com/images/icons/bravegpt/icon64.png?v=df624b0
Expand Down Expand Up @@ -380,7 +380,7 @@

// Init API data
const apis = Object.assign(Object.create(null), await new Promise(resolve => xhr({
method: 'GET', url: 'https://assets.aiwebextensions.com/data/ai-chat-apis.json?v=b529a64',
method: 'GET', url: 'https://assets.aiwebextensions.com/data/ai-chat-apis.json?v=556cbfe',
onload: resp => resolve(JSON.parse(resp.responseText))
})))
apis.AIchatOS.userID = '#/chat/' + Date.now()
Expand Down Expand Up @@ -3405,25 +3405,27 @@
clearTimeout(this.timeout) ; this.timeout = setTimeout(handleProcessCompletion, 1500) }

// Process/accumulate reply chunk
let replyChunk = ''
if (callerAPI == 'GPTforLove') { // extract parentID + deltas
const chunkObjs = respChunk.trim().split('\n').map(line => JSON.parse(line))
if (typeof chunkObjs[0].text == 'undefined') // error response
replyChunk = JSON.stringify(chunkObjs[0]) // for fail flag check
else { // AI response
apis.GPTforLove.parentID = chunkObjs[0].id || null // for contextual replies
chunkObjs.forEach(obj => replyChunk += obj.delta || '') // accumulate AI reply text
if (respChunk.includes('"finish_reason":"stop"')) isDone = true
}
} else if (callerAPI == 'MixerBox AI') { // extract/normalize AI reply data
replyChunk = [...respChunk.matchAll(/data:(.*)/g)] // arrayify data
.filter(match => !/message_(?:start|end)|done/.test(match)) // exclude signals
.map(match => // normalize whitespace
match[1].replace(/\[SPACE\]/g, ' ').replace(/\[NEWLINE\]/g, '\n'))
.join('') // stringify AI reply text
if (/data:(?:message_end|done)/.test(respChunk)) isDone = true
} else replyChunk = respChunk // no processing required for all other APIs
textToShow += replyChunk
if (!apis[callerAPI].parsingRequired) textToShow += respChunk
else { // parse structured chunk(s)
let replyChunk = ''
if (callerAPI == 'GPTforLove') { // extract parentID + deltas
const chunkObjs = respChunk.trim().split('\n').map(line => JSON.parse(line))
if (typeof chunkObjs[0].text == 'undefined') // error response
replyChunk = JSON.stringify(chunkObjs[0]) // for fail flag check
else { // AI response
apis.GPTforLove.parentID = chunkObjs[0].id || null // for contextual replies
chunkObjs.forEach(obj => replyChunk += obj.delta || '') // accumulate AI reply text
if (respChunk.includes('"finish_reason":"stop"')) isDone = true
}
} else if (callerAPI == 'MixerBox AI') { // extract/normalize AI reply data
replyChunk = [...respChunk.matchAll(/data:(.*)/g)] // arrayify data
.filter(match => !/message_(?:start|end)|done/.test(match)) // exclude signals
.map(match => // normalize whitespace
match[1].replace(/\[SPACE\]/g, ' ').replace(/\[NEWLINE\]/g, '\n'))
.join('') // stringify AI reply text
if (/data:(?:message_end|done)/.test(respChunk)) isDone = true
} textToShow += replyChunk
}

// Show accumulated reply chunks
try {
Expand Down Expand Up @@ -3488,24 +3490,26 @@
} catch (err) { handleProcessError(err) }
}
} else if (resp.responseText) { // show response or return RQs from proxy API
if (callerAPI == 'GPTforLove') {
try {
const chunkLines = resp.responseText.trim().split('\n'),
lastChunkObj = JSON.parse(chunkLines[chunkLines.length -1])
apis.GPTforLove.parentID = lastChunkObj.id || null
textToShow = lastChunkObj.text ; handleProcessCompletion()
} catch (err) { handleProcessError(err) }
} else if (callerAPI == 'MixerBox AI') {
try {
textToShow = [...resp.responseText.matchAll(/data:(.*)/g)] // arrayify data
.filter(match => !/message_(?:start|end)|done/.test(match)) // exclude signals
.map(match => // normalize whitespace
match[1].replace(/\[SPACE\]/g, ' ').replace(/\[NEWLINE\]/g, '\n'))
.join('') // stringify AI reply text
handleProcessCompletion()
} catch (err) { handleProcessError(err) }
} else { // no processing required for all other APIs
textToShow = resp.responseText ; handleProcessCompletion() }
if (!apis[callerAPI].parsingRequired) { textToShow = resp.responseText ; handleProcessCompletion }
else { // parse structured responseText
if (callerAPI == 'GPTforLove') {
try {
const chunkLines = resp.responseText.trim().split('\n'),
lastChunkObj = JSON.parse(chunkLines[chunkLines.length -1])
apis.GPTforLove.parentID = lastChunkObj.id || null
textToShow = lastChunkObj.text ; handleProcessCompletion()
} catch (err) { handleProcessError(err) }
} else if (callerAPI == 'MixerBox AI') {
try {
textToShow = [...resp.responseText.matchAll(/data:(.*)/g)] // arrayify data
.filter(match => !/message_(?:start|end)|done/.test(match)) // exclude signals
.map(match => // normalize whitespace
match[1].replace(/\[SPACE\]/g, ' ').replace(/\[NEWLINE\]/g, '\n'))
.join('') // stringify AI reply text
handleProcessCompletion()
} catch (err) { handleProcessError(err) }
}
}
} else if (caller.status != 'done') { // proxy 200 response failure
log.info('Response text', resp.responseText) ; api.tryNew(caller) }

Expand Down
Loading

0 comments on commit 5856fd9

Please sign in to comment.