generaltranslation 1.1.6 → 1.1.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/prompts/prompts.js +34 -30
package/package.json
CHANGED
package/prompts/prompts.js
CHANGED
|
@@ -1,34 +1,15 @@
|
|
|
1
1
|
// ----- PROMPT INTERNATIONALIZATION ----- //
|
|
2
2
|
|
|
3
|
-
//
|
|
4
|
-
const _constructPrompt = (prompt, redacted = null) => {
|
|
5
|
-
if (Array.isArray(prompt)) {
|
|
6
|
-
let final = '';
|
|
7
|
-
for (const item of prompt) {
|
|
8
|
-
if (typeof item === 'string') final += item;
|
|
9
|
-
else if (_shouldTranslate(item)) final += item?.text || '';
|
|
10
|
-
else {
|
|
11
|
-
if (redacted?.length > 0) {
|
|
12
|
-
final += redacted?.shift().text || '';
|
|
13
|
-
} else {
|
|
14
|
-
final += item?.text || '';
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
return final;
|
|
19
|
-
} else if (typeof prompt === 'string') {
|
|
20
|
-
return prompt;
|
|
21
|
-
} else {
|
|
22
|
-
return prompt?.text || '';
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
|
|
3
|
+
// Decides whether a prompt object should be translated
|
|
26
4
|
const _shouldTranslate = item => typeof item?.translate === 'boolean' ? item.translate : true;
|
|
27
|
-
|
|
5
|
+
|
|
6
|
+
// Pre-processes prompt to send to the API
|
|
7
|
+
// Separates out text that shouldn't be translated.
|
|
28
8
|
const _processPrompt = (prompt) => {
|
|
29
9
|
const processed = [];
|
|
30
10
|
const redacted = [];
|
|
31
11
|
if (Array.isArray(prompt)) {
|
|
12
|
+
console.log(prompt)
|
|
32
13
|
for (const item of prompt) {
|
|
33
14
|
if (typeof item === 'string') {
|
|
34
15
|
processed.push({
|
|
@@ -41,10 +22,6 @@ const _processPrompt = (prompt) => {
|
|
|
41
22
|
redacted.push(item);
|
|
42
23
|
}
|
|
43
24
|
}
|
|
44
|
-
return {
|
|
45
|
-
processed: processed,
|
|
46
|
-
redacted: redacted.length > 0 ? redacted : null
|
|
47
|
-
}
|
|
48
25
|
} else {
|
|
49
26
|
if (typeof prompt === 'string') {
|
|
50
27
|
processed.push({
|
|
@@ -63,6 +40,31 @@ const _processPrompt = (prompt) => {
|
|
|
63
40
|
}
|
|
64
41
|
}
|
|
65
42
|
|
|
43
|
+
// Build prompt string from array or single item
|
|
44
|
+
const _constructPrompt = ({translated, redacted = null}) => {
|
|
45
|
+
if (Array.isArray(translated)) {
|
|
46
|
+
let final = '';
|
|
47
|
+
for (const item of translated) {
|
|
48
|
+
if (typeof item === 'string') final += item;
|
|
49
|
+
else if (_shouldTranslate(item)) final += item?.text || '';
|
|
50
|
+
else {
|
|
51
|
+
if (redacted?.length > 0) {
|
|
52
|
+
final += redacted?.shift().text || '';
|
|
53
|
+
} else {
|
|
54
|
+
final += item?.text || '';
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
return final;
|
|
59
|
+
} else if (typeof translated === 'string') {
|
|
60
|
+
return translated;
|
|
61
|
+
} else {
|
|
62
|
+
return translated?.text || '';
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// Get a translated prompt via General Translation API
|
|
67
|
+
// Returns prompt string
|
|
66
68
|
const _getPrompt = async (prompt, language, defaultLanguage, apiKey) => {
|
|
67
69
|
if (!apiKey) {
|
|
68
70
|
throw new Error('Missing API Key!')
|
|
@@ -71,7 +73,9 @@ const _getPrompt = async (prompt, language, defaultLanguage, apiKey) => {
|
|
|
71
73
|
return _constructPrompt(prompt);
|
|
72
74
|
}
|
|
73
75
|
const { processed, redacted } = _processPrompt(prompt);
|
|
74
|
-
|
|
76
|
+
console.log(processed)
|
|
77
|
+
console.log(redacted)
|
|
78
|
+
const response = await fetch('http://prompts.gtx.dev/internationalize', {
|
|
75
79
|
method: 'POST',
|
|
76
80
|
headers: {
|
|
77
81
|
'Content-Type': 'application/json',
|
|
@@ -88,7 +92,7 @@ const _getPrompt = async (prompt, language, defaultLanguage, apiKey) => {
|
|
|
88
92
|
throw new Error(`${result || response.status}`);
|
|
89
93
|
} else {
|
|
90
94
|
const result = await response.json();
|
|
91
|
-
return _constructPrompt(result, redacted);
|
|
95
|
+
return _constructPrompt({translated: result, redacted: redacted});
|
|
92
96
|
}
|
|
93
97
|
}
|
|
94
98
|
|