From 9dad0ee1cd59310ad2c1197940f7c2098afd2d67 Mon Sep 17 00:00:00 2001
From: AiraNadih <128119996+AiraNadih@users.noreply.github.com>
Date: Wed, 15 Nov 2023 19:52:51 +0800
Subject: [PATCH 1/4] Wrap `selection` with Fenced Code Block
---
src/content-script/selection-tools/index.mjs | 22 ++++++++++----------
1 file changed, 11 insertions(+), 11 deletions(-)
diff --git a/src/content-script/selection-tools/index.mjs b/src/content-script/selection-tools/index.mjs
index eaef9ca9..53f19c41 100644
--- a/src/content-script/selection-tools/index.mjs
+++ b/src/content-script/selection-tools/index.mjs
@@ -17,7 +17,7 @@ export const config = {
label: 'Explain',
genPrompt: async (selection) => {
const preferredLanguage = await getPreferredLanguage()
- return `Reply in ${preferredLanguage}.Explain the following:\n"${selection}"`
+ return `Reply in ${preferredLanguage}.Explain the following:\n"""\n${selection}\n"""`
},
},
translate: {
@@ -25,21 +25,21 @@ export const config = {
label: 'Translate',
genPrompt: async (selection) => {
const preferredLanguage = await getPreferredLanguage()
- return `Translate the following into ${preferredLanguage} and only show me the translated content:\n${selection}`
+ return `Translate the following into ${preferredLanguage} and only show me the translated content:\n"""\n${selection}\n"""`
},
},
translateToEn: {
icon: ,
label: 'Translate (To English)',
genPrompt: async (selection) => {
- return `Translate the following into English and only show me the translated content:\n${selection}`
+ return `Translate the following into English and only show me the translated content:\n"""\n${selection}\n"""`
},
},
translateToZh: {
icon: ,
label: 'Translate (To Chinese)',
genPrompt: async (selection) => {
- return `Translate the following into Chinese and only show me the translated content:\n${selection}`
+ return `Translate the following into Chinese and only show me the translated content:\n"""\n${selection}\n"""`
},
},
translateBidi: {
@@ -50,7 +50,7 @@ export const config = {
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}`
+ `translate it into English and only show me the translated content:\n"""\n${selection}\n"""`
)
},
},
@@ -59,35 +59,35 @@ export const config = {
label: 'Summary',
genPrompt: async (selection) => {
const preferredLanguage = await getPreferredLanguage()
- return `Reply in ${preferredLanguage}.Summarize the following as concisely as possible:\n"${selection}"`
+ return `Reply in ${preferredLanguage}.Summarize the following as concisely as possible:\n"""\n${selection}\n"""`
},
},
polish: {
icon: ,
label: 'Polish',
genPrompt: async (selection) =>
- `Check the following content for possible diction and grammar problems,and polish it carefully:\n"${selection}"`,
+ `Check the following content for possible diction and grammar problems,and polish it carefully:\n"""\n${selection}\n"""`,
},
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}"`
+ return `Reply in ${preferredLanguage}.Analyze the sentiments expressed in the following content and make a brief summary of the sentiments:\n"""\n${selection}\n"""`
},
},
divide: {
icon: ,
label: 'Divide Paragraphs',
genPrompt: async (selection) =>
- `Divide the following into paragraphs that are easy to read and understand:\n"${selection}"`,
+ `Divide the following into paragraphs that are easy to read and understand:\n"""\n${selection}\n"""`,
},
code: {
icon: ,
label: 'Code Explain',
genPrompt: async (selection) => {
const preferredLanguage = await getPreferredLanguage()
- return `Reply in ${preferredLanguage}.Explain the following code:\n"${selection}"`
+ return `Reply in ${preferredLanguage}.Explain the following code:\n"""\n${selection}\n"""`
},
},
ask: {
@@ -95,7 +95,7 @@ export const config = {
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}"`
+ return `Reply in ${preferredLanguage}.Analyze the following content and express your opinion,or give your answer:\n"""\n${selection}\n"""`
},
},
}
From 8d2995970fd14f5ae5b79a93d1837265ed3c8792 Mon Sep 17 00:00:00 2001
From: AiraNadih <128119996+AiraNadih@users.noreply.github.com>
Date: Thu, 16 Nov 2023 04:05:53 +0800
Subject: [PATCH 2/4] Refactor genPrompt functions and improve code readability
---
src/content-script/selection-tools/index.mjs | 88 ++++++++++----------
1 file changed, 46 insertions(+), 42 deletions(-)
diff --git a/src/content-script/selection-tools/index.mjs b/src/content-script/selection-tools/index.mjs
index 53f19c41..8d27d464 100644
--- a/src/content-script/selection-tools/index.mjs
+++ b/src/content-script/selection-tools/index.mjs
@@ -11,91 +11,95 @@ import {
} from 'react-bootstrap-icons'
import { getPreferredLanguage } from '../../config/language.mjs'
+const createGenPrompt =
+ (message, isTranslation = false, targetLanguage = '') =>
+ async (selection) => {
+ const preferredLanguage = isTranslation
+ ? targetLanguage
+ : await getPreferredLanguage()
+ return `Reply in ${preferredLanguage}.${message}:\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"""\n${selection}\n"""`
- },
+ genPrompt: createGenPrompt('Explain the following'),
},
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"""\n${selection}\n"""`
- },
+ genPrompt: createGenPrompt(
+ 'Translate the following into ${preferredLanguage} and only show me the translated content',
+ true
+ ),
},
translateToEn: {
icon: ,
label: 'Translate (To English)',
- genPrompt: async (selection) => {
- return `Translate the following into English and only show me the translated content:\n"""\n${selection}\n"""`
- },
+ genPrompt: createGenPrompt(
+ 'Translate the following into English and only show me the translated content',
+ true,
+ 'English'
+ ),
},
translateToZh: {
icon: ,
label: 'Translate (To Chinese)',
- genPrompt: async (selection) => {
- return `Translate the following into Chinese and only show me the translated content:\n"""\n${selection}\n"""`
- },
+ genPrompt: createGenPrompt(
+ 'Translate the following into Chinese and only show me the translated content',
+ true,
+ '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"""\n${selection}\n"""`
- )
- },
+ genPrompt: createGenPrompt(
+ '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',
+ true
+ ),
},
summary: {
icon: ,
label: 'Summary',
- genPrompt: async (selection) => {
- const preferredLanguage = await getPreferredLanguage()
- return `Reply in ${preferredLanguage}.Summarize the following as concisely as possible:\n"""\n${selection}\n"""`
- },
+ genPrompt: createGenPrompt(
+ 'Summarize the following as concisely as possible'
+ ),
},
polish: {
icon: ,
label: 'Polish',
- genPrompt: async (selection) =>
- `Check the following content for possible diction and grammar problems,and polish it carefully:\n"""\n${selection}\n"""`,
+ genPrompt: createGenPrompt(
+ 'Check the following content for possible diction and grammar problems, and polish it carefully',
+ false
+ ),
},
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"""\n${selection}\n"""`
- },
+ genPrompt: createGenPrompt(
+ 'Analyze the sentiments expressed in the following content and make a brief summary of the sentiments'
+ ),
},
divide: {
icon: ,
label: 'Divide Paragraphs',
- genPrompt: async (selection) =>
- `Divide the following into paragraphs that are easy to read and understand:\n"""\n${selection}\n"""`,
+ genPrompt: createGenPrompt(
+ 'Divide the following into paragraphs that are easy to read and understand',
+ false
+ ),
},
code: {
icon: ,
label: 'Code Explain',
- genPrompt: async (selection) => {
- const preferredLanguage = await getPreferredLanguage()
- return `Reply in ${preferredLanguage}.Explain the following code:\n"""\n${selection}\n"""`
- },
+ genPrompt: createGenPrompt('Explain the following code'),
},
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"""\n${selection}\n"""`
- },
+ genPrompt: createGenPrompt(
+ 'Analyze the following content and express your opinion, or give your answer'
+ ),
},
}
From bcdeeef7ebb4697c7a212c081fcfafa92213fbd8 Mon Sep 17 00:00:00 2001
From: AiraNadih <128119996+AiraNadih@users.noreply.github.com>
Date: Thu, 16 Nov 2023 05:53:05 +0800
Subject: [PATCH 3/4] Fix the logic of `createGenPrompt`
---
src/content-script/selection-tools/index.mjs | 44 ++++++++------------
1 file changed, 18 insertions(+), 26 deletions(-)
diff --git a/src/content-script/selection-tools/index.mjs b/src/content-script/selection-tools/index.mjs
index 8d27d464..6ced10c8 100644
--- a/src/content-script/selection-tools/index.mjs
+++ b/src/content-script/selection-tools/index.mjs
@@ -12,13 +12,19 @@ import {
import { getPreferredLanguage } from '../../config/language.mjs'
const createGenPrompt =
- (message, isTranslation = false, targetLanguage = '') =>
- async (selection) => {
- const preferredLanguage = isTranslation
- ? targetLanguage
- : await getPreferredLanguage()
- return `Reply in ${preferredLanguage}.${message}:\n'''\n${selection}\n'''`
- }
+ (message, isTranslation = false, targetLanguage = '', enableBidirectional = 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`
+ }
+ return `Reply in ${preferredLanguage}.${fullMessage}:\n'''\n${selection}\n'''`
+ }
export const config = {
explain: {
@@ -29,36 +35,22 @@ export const config = {
translate: {
icon: ,
label: 'Translate',
- genPrompt: createGenPrompt(
- 'Translate the following into ${preferredLanguage} and only show me the translated content',
- true
- ),
+ genPrompt: createGenPrompt('', true),
},
translateToEn: {
icon: ,
label: 'Translate (To English)',
- genPrompt: createGenPrompt(
- 'Translate the following into English and only show me the translated content',
- true,
- 'English'
- ),
+ genPrompt: createGenPrompt('', true, 'English'),
},
translateToZh: {
icon: ,
label: 'Translate (To Chinese)',
- genPrompt: createGenPrompt(
- 'Translate the following into Chinese and only show me the translated content',
- true,
- 'Chinese'
- ),
+ genPrompt: createGenPrompt('', true, 'Chinese'),
},
translateBidi: {
icon: ,
label: 'Translate (Bidirectional)',
- genPrompt: createGenPrompt(
- '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',
- true
- ),
+ genPrompt: createGenPrompt('', true, '', true),
},
summary: {
icon: ,
@@ -102,4 +94,4 @@ export const config = {
'Analyze the following content and express your opinion, or give your answer'
),
},
-}
+}
\ No newline at end of file
From 48773a50bef0e5cdcb88c615aa2d4588303f1baf Mon Sep 17 00:00:00 2001
From: AiraNadih <128119996+AiraNadih@users.noreply.github.com>
Date: Thu, 16 Nov 2023 06:26:59 +0800
Subject: [PATCH 4/4] Fix the logic of `createGenPrompt`
---
src/content-script/selection-tools/index.mjs | 85 +++++++++++++-------
1 file changed, 58 insertions(+), 27 deletions(-)
diff --git a/src/content-script/selection-tools/index.mjs b/src/content-script/selection-tools/index.mjs
index 6ced10c8..bceac93f 100644
--- a/src/content-script/selection-tools/index.mjs
+++ b/src/content-script/selection-tools/index.mjs
@@ -12,86 +12,117 @@ import {
import { getPreferredLanguage } from '../../config/language.mjs'
const createGenPrompt =
- (message, isTranslation = false, targetLanguage = '', enableBidirectional = false) =>
+ ({
+ 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;
+ : message
if (enableBidirectional) {
fullMessage += `. If it is already in ${preferredLanguage}, translate it into English and only show me the translated content`
}
- return `Reply in ${preferredLanguage}.${fullMessage}:\n'''\n${selection}\n'''`
+ const prefix = includeLanguagePrefix
+ ? `Reply in ${preferredLanguage}.`
+ : ''
+ return `${prefix}${fullMessage}:\n'''\n${selection}\n'''`
}
export const config = {
explain: {
icon: ,
label: 'Explain',
- genPrompt: createGenPrompt('Explain the following'),
+ genPrompt: createGenPrompt({
+ message: 'Explain the following',
+ includeLanguagePrefix: true
+ }),
},
translate: {
icon: ,
label: 'Translate',
- genPrompt: createGenPrompt('', true),
+ genPrompt: createGenPrompt({
+ isTranslation: true
+ }),
},
translateToEn: {
icon: ,
label: 'Translate (To English)',
- genPrompt: createGenPrompt('', true, 'English'),
+ genPrompt: createGenPrompt({
+ isTranslation: true,
+ targetLanguage: 'English'
+ }),
},
translateToZh: {
icon: ,
label: 'Translate (To Chinese)',
- genPrompt: createGenPrompt('', true, 'Chinese'),
+ genPrompt: createGenPrompt({
+ isTranslation: true,
+ targetLanguage: 'Chinese'
+ }),
},
translateBidi: {
icon: ,
label: 'Translate (Bidirectional)',
- genPrompt: createGenPrompt('', true, '', true),
+ genPrompt: createGenPrompt({
+ isTranslation: true,
+ enableBidirectional: true
+ }),
},
summary: {
icon: ,
label: 'Summary',
- genPrompt: createGenPrompt(
- 'Summarize the following as concisely as possible'
- ),
+ genPrompt: createGenPrompt({
+ message: 'Summarize the following as concisely as possible',
+ includeLanguagePrefix: true
+ }),
},
polish: {
icon: ,
label: 'Polish',
- genPrompt: createGenPrompt(
- 'Check the following content for possible diction and grammar problems, and polish it carefully',
- false
- ),
+ genPrompt: createGenPrompt({
+ message:
+ 'Check the following content for possible diction and grammar problems, and polish it carefully'
+ }),
},
sentiment: {
icon: ,
label: 'Sentiment Analysis',
- genPrompt: createGenPrompt(
- 'Analyze the sentiments expressed in the following content and make a brief summary of the sentiments'
- ),
+ 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: createGenPrompt(
- 'Divide the following into paragraphs that are easy to read and understand',
- false
- ),
+ genPrompt: createGenPrompt({
+ message:
+ 'Divide the following into paragraphs that are easy to read and understand'
+ }),
},
code: {
icon: ,
label: 'Code Explain',
- genPrompt: createGenPrompt('Explain the following code'),
+ genPrompt: createGenPrompt({
+ message: 'Explain the following code',
+ includeLanguagePrefix: true
+ }),
},
ask: {
icon: ,
label: 'Ask',
- genPrompt: createGenPrompt(
- 'Analyze the following content and express your opinion, or give your answer'
- ),
+ genPrompt: createGenPrompt({
+ message:
+ 'Analyze the following content and express your opinion, or give your answer',
+ includeLanguagePrefix: true
+ }),
},
-}
\ No newline at end of file
+}