diff --git a/src/content-script/selection-tools/index.mjs b/src/content-script/selection-tools/index.mjs index eaef9ca..bceac93 100644 --- a/src/content-script/selection-tools/index.mjs +++ b/src/content-script/selection-tools/index.mjs @@ -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: , 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: , 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: , 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: , 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: , 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: , 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: , 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: , 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: , 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: , 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: , 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 + }), }, }