Skip to content

Commit

Permalink
Centralized augment methods into prompts
Browse files Browse the repository at this point in the history
  • Loading branch information
adamlui committed Jan 16, 2025
1 parent 538232b commit a823f02
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 97 deletions.
40 changes: 19 additions & 21 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.15.18
// @version 2025.1.15.19
// @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 @@ -2115,11 +2115,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 @@ -2156,7 +2156,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 }))
}
Expand Down Expand Up @@ -2276,10 +2276,12 @@
}
}

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

const prompts = {

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

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

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 @@ -2622,28 +2634,14 @@
if (caller.attemptCnt < Object.keys(apis).length -+(caller == get.reply)) {
log.debug('Trying another endpoint...')
caller.attemptCnt++
caller(caller == get.reply ? msgChain : stripQueryAugments(msgChain)[msgChain.length - 1].content)
caller(caller == get.reply ? msgChain : prompts.stripAugments(msgChain)[msgChain.length - 1].content)
} else {
log.debug('No remaining untried endpoints')
if (caller == get.reply) appAlert('proxyNotWorking', 'suggestOpenAI')
}
}
}

// 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 @@ -3200,7 +3198,7 @@
const pageType = /\/(?:dp|product)\//.test(location.href) ? 'Product'
: /\/b\//.test(location.href) ? 'Category' : 'Other'
const firstQuery = pageType == 'Other' ? 'Hi there' : prompts.create({ type: `inform${pageType}` })
let msgChain = [{ role: 'user', content: augmentQuery(firstQuery) }]
let msgChain = [{ role: 'user', content: prompts.augment(firstQuery) }]
appAlert('waitingResponse') ; get.reply(msgChain)

})()
50 changes: 24 additions & 26 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.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
49 changes: 24 additions & 25 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 2025.1.15.21
// @version 2025.1.15.22
// @license MIT
// @icon https://assets.ddgpt.com/images/icons/duckduckgpt/icon48.png?v=06af076
// @icon64 https://assets.ddgpt.com/images/icons/duckduckgpt/icon64.png?v=06af076
Expand Down Expand Up @@ -2548,11 +2548,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 @@ -2591,7 +2591,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 @@ -2713,10 +2713,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 @@ -2726,6 +2728,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 @@ -2909,7 +2921,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 @@ -3171,20 +3183,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 @@ -3247,7 +3245,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 @@ -3279,7 +3277,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 @@ -3638,7 +3636,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 @@ -4039,13 +4038,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.autoGet // 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')
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
Loading

0 comments on commit a823f02

Please sign in to comment.