Skip to content

Commit 70d6b79

Browse files
authored
Optimize Logic in selection-tools Module (#564)
* Wrap `selection` with Fenced Code Block * Refactor genPrompt functions and improve code readability * Fix the logic of `createGenPrompt` * Fix the logic of `createGenPrompt`
1 parent d9a6f23 commit 70d6b79

File tree

1 file changed

+69
-42
lines changed

1 file changed

+69
-42
lines changed

src/content-script/selection-tools/index.mjs

Lines changed: 69 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -11,91 +11,118 @@ import {
1111
} from 'react-bootstrap-icons'
1212
import { getPreferredLanguage } from '../../config/language.mjs'
1313

14+
const createGenPrompt =
15+
({
16+
message = '',
17+
isTranslation = false,
18+
targetLanguage = '',
19+
enableBidirectional = false,
20+
includeLanguagePrefix = false
21+
}) =>
22+
async (selection) => {
23+
const preferredLanguage = isTranslation
24+
? targetLanguage
25+
: await getPreferredLanguage()
26+
let fullMessage = isTranslation
27+
? `Translate the following into ${preferredLanguage} and only show me the translated content`
28+
: message
29+
if (enableBidirectional) {
30+
fullMessage += `. If it is already in ${preferredLanguage}, translate it into English and only show me the translated content`
31+
}
32+
const prefix = includeLanguagePrefix
33+
? `Reply in ${preferredLanguage}.`
34+
: ''
35+
return `${prefix}${fullMessage}:\n'''\n${selection}\n'''`
36+
}
37+
1438
export const config = {
1539
explain: {
1640
icon: <ChatText />,
1741
label: 'Explain',
18-
genPrompt: async (selection) => {
19-
const preferredLanguage = await getPreferredLanguage()
20-
return `Reply in ${preferredLanguage}.Explain the following:\n"${selection}"`
21-
},
42+
genPrompt: createGenPrompt({
43+
message: 'Explain the following',
44+
includeLanguagePrefix: true
45+
}),
2246
},
2347
translate: {
2448
icon: <Translate />,
2549
label: 'Translate',
26-
genPrompt: async (selection) => {
27-
const preferredLanguage = await getPreferredLanguage()
28-
return `Translate the following into ${preferredLanguage} and only show me the translated content:\n${selection}`
29-
},
50+
genPrompt: createGenPrompt({
51+
isTranslation: true
52+
}),
3053
},
3154
translateToEn: {
3255
icon: <Globe />,
3356
label: 'Translate (To English)',
34-
genPrompt: async (selection) => {
35-
return `Translate the following into English and only show me the translated content:\n${selection}`
36-
},
57+
genPrompt: createGenPrompt({
58+
isTranslation: true,
59+
targetLanguage: 'English'
60+
}),
3761
},
3862
translateToZh: {
3963
icon: <Globe />,
4064
label: 'Translate (To Chinese)',
41-
genPrompt: async (selection) => {
42-
return `Translate the following into Chinese and only show me the translated content:\n${selection}`
43-
},
65+
genPrompt: createGenPrompt({
66+
isTranslation: true,
67+
targetLanguage: 'Chinese'
68+
}),
4469
},
4570
translateBidi: {
4671
icon: <Globe />,
4772
label: 'Translate (Bidirectional)',
48-
genPrompt: async (selection) => {
49-
const preferredLanguage = await getPreferredLanguage()
50-
return (
51-
`Translate the following into ${preferredLanguage} and only show me the translated content.` +
52-
`If it is already in ${preferredLanguage},` +
53-
`translate it into English and only show me the translated content:\n${selection}`
54-
)
55-
},
73+
genPrompt: createGenPrompt({
74+
isTranslation: true,
75+
enableBidirectional: true
76+
}),
5677
},
5778
summary: {
5879
icon: <CardHeading />,
5980
label: 'Summary',
60-
genPrompt: async (selection) => {
61-
const preferredLanguage = await getPreferredLanguage()
62-
return `Reply in ${preferredLanguage}.Summarize the following as concisely as possible:\n"${selection}"`
63-
},
81+
genPrompt: createGenPrompt({
82+
message: 'Summarize the following as concisely as possible',
83+
includeLanguagePrefix: true
84+
}),
6485
},
6586
polish: {
6687
icon: <Palette />,
6788
label: 'Polish',
68-
genPrompt: async (selection) =>
69-
`Check the following content for possible diction and grammar problems,and polish it carefully:\n"${selection}"`,
89+
genPrompt: createGenPrompt({
90+
message:
91+
'Check the following content for possible diction and grammar problems, and polish it carefully'
92+
}),
7093
},
7194
sentiment: {
7295
icon: <EmojiSmile />,
7396
label: 'Sentiment Analysis',
74-
genPrompt: async (selection) => {
75-
const preferredLanguage = await getPreferredLanguage()
76-
return `Reply in ${preferredLanguage}.Analyze the sentiments expressed in the following content and make a brief summary of the sentiments:\n"${selection}"`
77-
},
97+
genPrompt: createGenPrompt({
98+
message:
99+
'Analyze the sentiments expressed in the following content and make a brief summary of the sentiments',
100+
includeLanguagePrefix: true
101+
}),
78102
},
79103
divide: {
80104
icon: <CardList />,
81105
label: 'Divide Paragraphs',
82-
genPrompt: async (selection) =>
83-
`Divide the following into paragraphs that are easy to read and understand:\n"${selection}"`,
106+
genPrompt: createGenPrompt({
107+
message:
108+
'Divide the following into paragraphs that are easy to read and understand'
109+
}),
84110
},
85111
code: {
86112
icon: <Braces />,
87113
label: 'Code Explain',
88-
genPrompt: async (selection) => {
89-
const preferredLanguage = await getPreferredLanguage()
90-
return `Reply in ${preferredLanguage}.Explain the following code:\n"${selection}"`
91-
},
114+
genPrompt: createGenPrompt({
115+
message: 'Explain the following code',
116+
includeLanguagePrefix: true
117+
}),
92118
},
93119
ask: {
94120
icon: <QuestionCircle />,
95121
label: 'Ask',
96-
genPrompt: async (selection) => {
97-
const preferredLanguage = await getPreferredLanguage()
98-
return `Reply in ${preferredLanguage}.Analyze the following content and express your opinion,or give your answer:\n"${selection}"`
99-
},
122+
genPrompt: createGenPrompt({
123+
message:
124+
'Analyze the following content and express your opinion, or give your answer',
125+
includeLanguagePrefix: true
126+
}),
100127
},
101128
}

0 commit comments

Comments
 (0)