Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize Logic in selection-tools Module #564

Merged
merged 4 commits into from
Nov 20, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}),
},
}