@@ -11,91 +11,118 @@ import {
11
11
} from 'react-bootstrap-icons'
12
12
import { getPreferredLanguage } from '../../config/language.mjs'
13
13
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
+
14
38
export const config = {
15
39
explain : {
16
40
icon : < ChatText /> ,
17
41
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
+ } ) ,
22
46
} ,
23
47
translate : {
24
48
icon : < Translate /> ,
25
49
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
+ } ) ,
30
53
} ,
31
54
translateToEn : {
32
55
icon : < Globe /> ,
33
56
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
+ } ) ,
37
61
} ,
38
62
translateToZh : {
39
63
icon : < Globe /> ,
40
64
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
+ } ) ,
44
69
} ,
45
70
translateBidi : {
46
71
icon : < Globe /> ,
47
72
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
+ } ) ,
56
77
} ,
57
78
summary : {
58
79
icon : < CardHeading /> ,
59
80
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
+ } ) ,
64
85
} ,
65
86
polish : {
66
87
icon : < Palette /> ,
67
88
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
+ } ) ,
70
93
} ,
71
94
sentiment : {
72
95
icon : < EmojiSmile /> ,
73
96
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
+ } ) ,
78
102
} ,
79
103
divide : {
80
104
icon : < CardList /> ,
81
105
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
+ } ) ,
84
110
} ,
85
111
code : {
86
112
icon : < Braces /> ,
87
113
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
+ } ) ,
92
118
} ,
93
119
ask : {
94
120
icon : < QuestionCircle /> ,
95
121
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
+ } ) ,
100
127
} ,
101
128
}
0 commit comments