Skip to content

Commit

Permalink
Optimize Logic in selection-tools Module (#564)
Browse files Browse the repository at this point in the history
* Wrap `selection` with Fenced Code Block

* Refactor genPrompt functions and improve code readability

* Fix the logic of `createGenPrompt`

* Fix the logic of `createGenPrompt`
  • Loading branch information
AiraNadih committed Nov 20, 2023
1 parent d9a6f23 commit 70d6b79
Showing 1 changed file with 69 additions and 42 deletions.
111 changes: 69 additions & 42 deletions src/content-script/selection-tools/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -11,91 +11,118 @@ import {
} from 'react-bootstrap-icons'
import { getPreferredLanguage } from '../../config/language.mjs'

const createGenPrompt =
({
message = '',
isTranslation = false,
targetLanguage = '',
enableBidirectional = false,
includeLanguagePrefix = false
}) =>
async (selection) => {
const preferredLanguage = isTranslation
? targetLanguage
: await getPreferredLanguage()
let fullMessage = isTranslation
? `Translate the following into ${preferredLanguage} and only show me the translated content`
: message
if (enableBidirectional) {
fullMessage += `. If it is already in ${preferredLanguage}, translate it into English and only show me the translated content`
}
const prefix = includeLanguagePrefix
? `Reply in ${preferredLanguage}.`
: ''
return `${prefix}${fullMessage}:\n'''\n${selection}\n'''`
}

export const config = {
explain: {
icon: <ChatText />,
label: 'Explain',
genPrompt: async (selection) => {
const preferredLanguage = await getPreferredLanguage()
return `Reply in ${preferredLanguage}.Explain the following:\n"${selection}"`
},
genPrompt: createGenPrompt({
message: 'Explain the following',
includeLanguagePrefix: true
}),
},
translate: {
icon: <Translate />,
label: 'Translate',
genPrompt: async (selection) => {
const preferredLanguage = await getPreferredLanguage()
return `Translate the following into ${preferredLanguage} and only show me the translated content:\n${selection}`
},
genPrompt: createGenPrompt({
isTranslation: true
}),
},
translateToEn: {
icon: <Globe />,
label: 'Translate (To English)',
genPrompt: async (selection) => {
return `Translate the following into English and only show me the translated content:\n${selection}`
},
genPrompt: createGenPrompt({
isTranslation: true,
targetLanguage: 'English'
}),
},
translateToZh: {
icon: <Globe />,
label: 'Translate (To Chinese)',
genPrompt: async (selection) => {
return `Translate the following into Chinese and only show me the translated content:\n${selection}`
},
genPrompt: createGenPrompt({
isTranslation: true,
targetLanguage: 'Chinese'
}),
},
translateBidi: {
icon: <Globe />,
label: 'Translate (Bidirectional)',
genPrompt: async (selection) => {
const preferredLanguage = await getPreferredLanguage()
return (
`Translate the following into ${preferredLanguage} and only show me the translated content.` +
`If it is already in ${preferredLanguage},` +
`translate it into English and only show me the translated content:\n${selection}`
)
},
genPrompt: createGenPrompt({
isTranslation: true,
enableBidirectional: true
}),
},
summary: {
icon: <CardHeading />,
label: 'Summary',
genPrompt: async (selection) => {
const preferredLanguage = await getPreferredLanguage()
return `Reply in ${preferredLanguage}.Summarize the following as concisely as possible:\n"${selection}"`
},
genPrompt: createGenPrompt({
message: 'Summarize the following as concisely as possible',
includeLanguagePrefix: true
}),
},
polish: {
icon: <Palette />,
label: 'Polish',
genPrompt: async (selection) =>
`Check the following content for possible diction and grammar problems,and polish it carefully:\n"${selection}"`,
genPrompt: createGenPrompt({
message:
'Check the following content for possible diction and grammar problems, and polish it carefully'
}),
},
sentiment: {
icon: <EmojiSmile />,
label: 'Sentiment Analysis',
genPrompt: async (selection) => {
const preferredLanguage = await getPreferredLanguage()
return `Reply in ${preferredLanguage}.Analyze the sentiments expressed in the following content and make a brief summary of the sentiments:\n"${selection}"`
},
genPrompt: createGenPrompt({
message:
'Analyze the sentiments expressed in the following content and make a brief summary of the sentiments',
includeLanguagePrefix: true
}),
},
divide: {
icon: <CardList />,
label: 'Divide Paragraphs',
genPrompt: async (selection) =>
`Divide the following into paragraphs that are easy to read and understand:\n"${selection}"`,
genPrompt: createGenPrompt({
message:
'Divide the following into paragraphs that are easy to read and understand'
}),
},
code: {
icon: <Braces />,
label: 'Code Explain',
genPrompt: async (selection) => {
const preferredLanguage = await getPreferredLanguage()
return `Reply in ${preferredLanguage}.Explain the following code:\n"${selection}"`
},
genPrompt: createGenPrompt({
message: 'Explain the following code',
includeLanguagePrefix: true
}),
},
ask: {
icon: <QuestionCircle />,
label: 'Ask',
genPrompt: async (selection) => {
const preferredLanguage = await getPreferredLanguage()
return `Reply in ${preferredLanguage}.Analyze the following content and express your opinion,or give your answer:\n"${selection}"`
},
genPrompt: createGenPrompt({
message:
'Analyze the following content and express your opinion, or give your answer',
includeLanguagePrefix: true
}),
},
}

0 comments on commit 70d6b79

Please sign in to comment.