-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeeplTranslate.ts
92 lines (85 loc) · 2.19 KB
/
deeplTranslate.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import { ActionDefinition, ActionContext } from 'connery';
import axios from 'axios';
const actionDefinition: ActionDefinition = {
key: 'deeplTranslate',
name: 'DeepL Translate',
description:
'Translates text using DeepL API for a specified language with optional instructions. Note: user needs to explicitly mention DeepL and the target language in the request.',
type: 'read',
inputParameters: [
{
key: 'apiKey',
name: 'DeepL API Key',
description: 'Your DeepL API key',
type: 'string',
validation: {
required: true,
},
},
{
key: 'text',
name: 'Text to Translate',
description: 'The text you want to translate',
type: 'string',
validation: {
required: true,
},
},
{
key: 'targetLanguage',
name: 'Target Language',
description: 'The language code to translate to (e.g., EN, DE, FR)',
type: 'string',
validation: {
required: true,
},
},
{
key: 'instructions',
name: 'Instructions',
description: 'Optional instructions for processing the translated text',
type: 'string',
validation: {
required: false,
},
},
],
operation: {
handler: handler,
},
outputParameters: [
{
key: 'translatedText',
name: 'Translated Text',
description: 'The translated text with optional instructions',
type: 'string',
validation: {
required: true,
},
},
],
};
export default actionDefinition;
export async function handler({ input }: ActionContext): Promise<{ translatedText: string }> {
const { apiKey, text, targetLanguage, instructions } = input;
const response = await axios.post(
'https://api-free.deepl.com/v2/translate',
{
text: [text],
target_lang: targetLanguage,
},
{
headers: {
Authorization: `DeepL-Auth-Key ${apiKey}`,
'Content-Type': 'application/json',
},
},
);
let translatedText = response.data.translations[0].text;
if (instructions) {
translatedText = `Instructions for the following content: ${instructions}\n\n${translatedText}`;
}
return {
translatedText,
};
}