Skip to content

Commit

Permalink
Added ToYaml.com/GET API support ↞ [auto-sync from https://github.com…
Browse files Browse the repository at this point in the history
kudo-sync-bot committed Jan 14, 2025
1 parent efe7e6f commit 6999617
Showing 1 changed file with 57 additions and 33 deletions.
90 changes: 57 additions & 33 deletions chatgpt/bravegpt/bravegpt.user.js
Original file line number Diff line number Diff line change
@@ -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.14.4
// @version 2025.1.14.5
// @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
@@ -179,6 +179,7 @@
// @connect chatgpt.com
// @connect update.greasyfork.org
// @connect fanyi.sogou.com
// @connect toyaml.com
// @require https://cdn.jsdelivr.net/npm/@kudoai/chatgpt.js@3.5.0/dist/chatgpt.min.js#sha256-+C0x4BOFQc38aZB3pvUC2THu+ZSvuCxRphGdtRLjCDg=
// @require https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.2.0/crypto-js.min.js#sha256-dppVXeVTurw1ozOPNE3XqhYmDJPOosfbKQcHyQSE58w=
// @require https://cdn.jsdelivr.net/npm/generate-ip@2.4.4/dist/generate-ip.min.js#sha256-aQQKAQcMgCu8IpJp9HKs387x0uYxngO+Fb4pc5nSF4I=
@@ -603,6 +604,11 @@
},
expectedOrigin: { url: 'https://chatgpt.com', headers: { 'Priority': 'u=4' }},
method: 'POST', streamable: true
},
'ToYaml.com': {
endpoint: 'https://toyaml.com/streams',
expectedOrigin: { url: 'https://toyaml.com/chat.html', headers: { 'x-requested-with': 'XMLHttpRequest' }},
method: 'GET', streamable: true, watermark: '【本答案来自 toyaml.com】'
}
}

@@ -3153,14 +3159,18 @@
const ip = ipv4.generate({ verbose: false })
const headers = {
'Accept': '*/*', 'Accept-Encoding': 'gzip, deflate, br, zstd',
'Connection': 'keep-alive', 'Content-Type': 'application/json', 'DNT': '1',
'Host': new URL(apis[api].endpoints?.completions || apis[api].endpoint).hostname,
'Origin': apis[api].expectedOrigin.url, 'Sec-Fetch-Site': 'same-origin',
'Sec-Fetch-Dest': 'empty', 'Sec-Fetch-Mode': 'cors', 'X-Forwarded-For': ip, 'X-Real-IP': ip
'Connection': 'keep-alive', 'DNT': '1',
'Origin': apis[api].expectedOrigin.url, 'X-Forwarded-For': ip, 'X-Real-IP': ip
}
headers.Referer = headers.Origin + '/'
if (api == 'OpenAI') headers.Authorization = 'Bearer ' + config.openAIkey
Object.assign(headers, apis[api].expectedOrigin.headers)
if (apis[api].method == 'POST') Object.assign(headers, {
'Content-Type': 'application/json',
'Host': new URL(apis[api].endpoints?.completions || apis[api].endpoint).hostname,
'Sec-Fetch-Site': 'same-origin', 'Sec-Fetch-Dest': 'empty', 'Sec-Fetch-Mode': 'cors'
})
else if (apis[api].method == 'GET') headers['x-requested-with'] = 'XMLHttpRequest'
Object.assign(headers, apis[api].expectedOrigin.headers) // API-specific ones
if (api == 'OpenAI') headers.Authorization = `Bearer ${config.openAIkey}`
return headers
},

@@ -3284,19 +3294,22 @@
}

// Get/show answer from AI
xhr({
method: apis[get.reply.api].method,
url: apis[get.reply.api].endpoints?.completions || apis[get.reply.api].endpoint,
const reqMethod = apis[get.reply.api].method
const xhrConfig = {
headers: api.createHeaders(get.reply.api), method: reqMethod,
responseType: config.streamingDisabled || !config.proxyAPIenabled ? 'text' : 'stream',
headers: api.createHeaders(get.reply.api), data: await api.createPayload(get.reply.api, msgChain),
onload: resp => dataProcess.text(get.reply, resp),
onloadstart: resp => dataProcess.stream(get.reply, resp),
onerror: err => { log.error(err)
if (!config.proxyAPIenabled)
appAlert(!config.openAIkey ? 'login' : ['openAInotWorking', 'suggestProxy'])
else api.tryNew(get.reply)
}
})
},
onload: resp => dataProcess.text(get.reply, resp),
onloadstart: resp => dataProcess.stream(get.reply, resp),
url: ( apis[get.reply.api].endpoints?.completions || apis[get.reply.api].endpoint )
+ ( reqMethod == 'GET' ? `?q=${encodeURIComponent(msgChain[msgChain.length -1].content)}` : '' )
}
if (reqMethod == 'POST') xhrConfig.data = await api.createPayload(get.reply.api, msgChain)
xhr(xhrConfig)

// Get/show related queries if enabled on 1st get.reply()
if (!config.rqDisabled && get.reply.attemptCnt == 1)
@@ -3355,14 +3368,18 @@

// Get queries
const payload = await api.createPayload(get.related.api, [{ role: 'user', content: rqPrompt }])
return new Promise(resolve => xhr({
method: apis[get.related.api].method,
url: apis[get.related.api].endpoints?.completions || apis[get.related.api].endpoint,
responseType: 'text', headers: api.createHeaders(get.related.api),
data: payload,
onload: resp => dataProcess.text(get.related, resp).then(resolve),
onerror: err => { log.error(err) ; api.tryNew(get.related) }
}))
return new Promise(resolve => {
const reqMethod = apis[get.related.api].method
const xhrConfig = {
headers: api.createHeaders(get.related.api), method: reqMethod, responseType: 'text',
onerror: err => { log.error(err) ; api.tryNew(get.related) },
onload: resp => dataProcess.text(get.related, resp).then(resolve),
url: ( apis[get.related.api].endpoints?.completions || apis[get.related.api].endpoint )
+ ( reqMethod == 'GET' ? `?q=${rqPrompt}` : '' )
}
if (reqMethod == 'POST') xhrConfig.data = payload
xhr(xhrConfig)
})
}
}

@@ -3384,15 +3401,9 @@
reader.read().then(processStreamText).catch(err => log.error('Error processing stream', err.message))

function processStreamText({ done, value }) {
if (done) { caller.sender = null
if (appDiv.querySelector('.loading')) // no text shown
api.tryNew(caller)
else { // text was shown
caller.status = 'done' ; caller.attemptCnt = null
show.replyCornerBtns() ; api.clearTimedOut(caller.triedAPIs)
} return
}
if (done) { handleProcessCompletion() ; return }
let chunk = new TextDecoder('utf8').decode(new Uint8Array(value))
if (chunk.includes(apis[caller.api].watermark)) { handleProcessCompletion() ; return }
if (caller.api == 'MixerBox AI') { // pre-process chunks
const extractedChunks = Array.from(chunk.matchAll(/data:(.*)/g), match => match[1]
.replace(/\[SPACE\]/g, ' ').replace(/\[NEWLINE\]/g, '\n'))
@@ -3427,6 +3438,16 @@
processStreamText({ done, value })
}).catch(err => log.error('Error reading stream', err.message))
}

function handleProcessCompletion() {
caller.sender = null
if (appDiv.querySelector('.loading')) // no text shown
api.tryNew(caller)
else { // text was shown
caller.status = 'done' ; caller.attemptCnt = null
show.replyCornerBtns() ; api.clearTimedOut(caller.triedAPIs)
} return
}
},

text(caller, resp) {
@@ -3458,7 +3479,7 @@
} catch (err) { handleProcessError(err) }
}
} else if (resp.responseText) {
if (/AIchatOS|FREEGPT/.test(caller.api)) {
if (/AIchatOS|ToYaml.com|FREEGPT/.test(caller.api)) {
try { // to show response or return related queries
const text = resp.responseText, chunkSize = 1024
let currentIdx = 0
@@ -3495,9 +3516,12 @@
api.tryNew(caller)
} else {
caller.status = 'done' ; api.clearTimedOut(caller.triedAPIs) ; caller.attemptCnt = null
respText = respText.replace(apis[caller.api].watermark, '').trim()
if (caller == get.reply) { show.reply(respText, footerContent) ; show.replyCornerBtns() }
else resolve(arrayify(respText))
}}}
}
}
}

function handleProcessError(err) { // suggest proxy or try diff API
log.debug('Response text', resp.response)

0 comments on commit 6999617

Please sign in to comment.