Skip to content

Commit

Permalink
Centralized augment methods into prompts ↞ [auto-sync from https://…
Browse files Browse the repository at this point in the history
  • Loading branch information
kudo-sync-bot committed Jan 16, 2025
1 parent 4389d43 commit 5f91a67
Showing 1 changed file with 24 additions and 26 deletions.
50 changes: 24 additions & 26 deletions chatgpt/bravegpt/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.15.21
// @version 2025.1.15.22
// @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 @@ -2673,11 +2673,11 @@

// Modify/submit msg chain
if (msgChain.length > 2) msgChain.splice(0, 2) // keep token usage maintainable
msgChain = stripQueryAugments(msgChain)
msgChain = prompts.stripAugments(msgChain)
const prevReplyTrimmed = appDiv.querySelector('pre')
?.textContent.substring(0, 250 - chatTextarea.value.length) || ''
msgChain.push({ role: 'assistant', content: prevReplyTrimmed })
msgChain.push({ role: 'user', content: augmentQuery(chatTextarea.value) })
msgChain.push({ role: 'user', content: prompts.augment(chatTextarea.value) })
get.reply(msgChain)

// Hide/remove elems
Expand Down Expand Up @@ -2717,7 +2717,7 @@
// Add button listeners
appDiv.querySelectorAll(`.${app.cssPrefix}-chatbar-btn`).forEach(btn => {
if (btn.id.endsWith('shuffle-btn')) btn.onclick = () => {
chatTextarea.value = augmentQuery(prompts.create({ type: 'randomQA' }))
chatTextarea.value = prompts.augment(prompts.create({ type: 'randomQA' }))
chatTextarea.dispatchEvent(new KeyboardEvent('keydown',
{ key: 'Enter', bubbles: true, cancelable: true }))
show.reply.src = 'shuffle'
Expand Down Expand Up @@ -2838,10 +2838,12 @@
}
}

// Define PROMPTS props/function
// Define PROMPT functions

const prompts = {

augment(prompt) { return `${prompt} {{reply in ${config.replyLang}}}` },

create({ type, prevQuery }) {
const promptSrc = this[type],
modsToApply = promptSrc.mods?.flatMap(mod => typeof mod == 'string' ? mod : mod.mods) || [],
Expand All @@ -2851,6 +2853,16 @@
return builtPrompt
},

stripAugments(msgChain) {
return msgChain.map(msg => { // stripped chain
if (msg.role == 'user') {
let content = msg.content
content.match(/\{\{[^}]+\}\}/g)?.forEach(augment => content = content.replace(augment, ''))
return { ...msg, content: content.trim() }
} else return msg // agent's unstripped
})
},

randomQA: {
base: 'Generate a single random question on any topic then answer it',
mods: [
Expand Down Expand Up @@ -3034,7 +3046,7 @@
settings.save('rqDisabled', !config.rqDisabled)
update.rqVisibility()
if (!config.rqDisabled && !appDiv.querySelector(`.${app.cssPrefix}-related-queries`)) // get related queries for 1st time
get.related(stripQueryAugments(msgChain)[msgChain.length - 1].content)
get.related(prompts.stripAugments(msgChain)[msgChain.length - 1].content)
.then(queries => show.related(queries))
.catch(err => { log.error(err.message) ; api.tryNew(get.related) })
update.answerPreMaxHeight()
Expand Down Expand Up @@ -3296,20 +3308,6 @@
}
}

// Define QUERY AUGMENT functions

function augmentQuery(query) { return `${query} {{reply in ${config.replyLang}}}` }

function stripQueryAugments(msgChain) {
return msgChain.map(msg => { // stripped chain
if (msg.role == 'user') {
let content = msg.content
content.match(/\{\{[^}]+\}\}/g)?.forEach(augment => content = content.replace(augment, ''))
return { ...msg, content: content.trim() }
} else return msg // agent's unstripped
})
}

// Define GET functions

const get = {
Expand Down Expand Up @@ -3372,7 +3370,7 @@

// Get/show related queries if enabled on 1st get.reply()
if (!config.rqDisabled && get.reply.attemptCnt == 1)
get.related(stripQueryAugments(msgChain)[msgChain.length - 1].content)
get.related(prompts.stripAugments(msgChain)[msgChain.length - 1].content)
.then(queries => show.related(queries))
.catch(err => { log.error(err.message) ; api.tryNew(get.related) })

Expand Down Expand Up @@ -3406,7 +3404,7 @@
}, 7000)

// Get related queries
const rqPrompt = augmentQuery(prompts.create({ type: 'relatedQueries', prevQuery: query })),
const rqPrompt = prompts.augment(prompts.create({ type: 'relatedQueries', prevQuery: query })),
payload = await api.createPayload(get.related.api, [{ role: 'user', content: rqPrompt }])
return new Promise(resolve => {
const reqMethod = apis[get.related.api].method
Expand Down Expand Up @@ -3765,8 +3763,8 @@
appDiv.append(standbyBtn)
show.reply.standbyBtnClickHandler = function() {
appAlert('waitingResponse')
msgChain.push(
{ role: 'user', content: augmentQuery(new URL(location.href).searchParams.get('q')) })
msgChain.push({ role: 'user', content:
prompts.augment(new URL(location.href).searchParams.get('q')) })
show.reply.userInteracted = true ; show.reply.chatbarFocused = false
menus.pin.topPos = menus.pin.rightPos = null
get.reply(msgChain)
Expand Down Expand Up @@ -4064,13 +4062,13 @@
}), 1500)

// Show STANDBY mode or get/show ANSWER
let msgChain = [{ role: 'user', content: augmentQuery(new URL(location.href).searchParams.get('q')) }]
let msgChain = [{ role: 'user', content: prompts.augment(new URL(location.href).searchParams.get('q')) }]
if ( config.autoGetDisabled // Auto-Get disabled
|| config.prefixEnabled && !/.*q=%2F/.test(location.href) // prefix required but not present
|| config.suffixEnabled && !/.*q=.*(?:%3F||%EF%BC%9F)(?:&|$)/.test(location.href)) { // suffix required but not present
show.reply('standby', footerContent)
if (!config.rqDisabled)
get.related(stripQueryAugments(msgChain)[msgChain.length - 1].content)
get.related(prompts.stripAugments(msgChain)[msgChain.length - 1].content)
.then(queries => show.related(queries))
.catch(err => { log.error(err.message) ; api.tryNew(get.related) })
} else { appAlert('waitingResponse') ; get.reply(msgChain) }
Expand Down

0 comments on commit 5f91a67

Please sign in to comment.