generaltranslation 1.1.6 → 1.1.7

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/prompts/prompts.js +30 -25
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "generaltranslation",
3
- "version": "1.1.6",
3
+ "version": "1.1.7",
4
4
  "description": "A language toolkit for AI developers",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -1,30 +1,10 @@
1
1
  // ----- PROMPT INTERNATIONALIZATION ----- //
2
2
 
3
- // Turn a prompt (which could be a string, array, or object) into a string
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
- // Redacted items are not sent in the API call
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 = [];
@@ -63,6 +43,31 @@ const _processPrompt = (prompt) => {
63
43
  }
64
44
  }
65
45
 
46
+ // Build prompt string from array or single item
47
+ const _constructPrompt = (translated, redacted = null) => {
48
+ if (Array.isArray(translated)) {
49
+ let final = '';
50
+ for (const item of translated) {
51
+ if (typeof item === 'string') final += item;
52
+ else if (_shouldTranslate(item)) final += item?.text || '';
53
+ else {
54
+ if (redacted?.length > 0) {
55
+ final += redacted?.shift().text || '';
56
+ } else {
57
+ final += item?.text || '';
58
+ }
59
+ }
60
+ }
61
+ return final;
62
+ } else if (typeof translated === 'string') {
63
+ return translated;
64
+ } else {
65
+ return translated?.text || '';
66
+ }
67
+ }
68
+
69
+ // Get a translated prompt via General Translation API
70
+ // Returns prompt string
66
71
  const _getPrompt = async (prompt, language, defaultLanguage, apiKey) => {
67
72
  if (!apiKey) {
68
73
  throw new Error('Missing API Key!')
@@ -71,7 +76,7 @@ const _getPrompt = async (prompt, language, defaultLanguage, apiKey) => {
71
76
  return _constructPrompt(prompt);
72
77
  }
73
78
  const { processed, redacted } = _processPrompt(prompt);
74
- const response = await fetch('https://api.gtx.dev/prompt', {
79
+ const response = await fetch('https://prompts.gtx.dev/internationalize', {
75
80
  method: 'POST',
76
81
  headers: {
77
82
  'Content-Type': 'application/json',