transduck 0.2.2 → 0.2.4
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/dist/handler.js +6 -3
- package/dist/providers/prompts.js +4 -3
- package/package.json +1 -1
- package/src/handler.ts +7 -4
- package/src/providers/prompts.ts +4 -3
package/dist/handler.js
CHANGED
|
@@ -30,7 +30,7 @@ export async function handleTranslationRequest(body, configPath) {
|
|
|
30
30
|
const translations = {};
|
|
31
31
|
const plurals = {};
|
|
32
32
|
// Translate regular strings
|
|
33
|
-
for (const item of body.strings) {
|
|
33
|
+
for (const item of body.strings ?? []) {
|
|
34
34
|
const stringContextHash = hash(item.context ?? '');
|
|
35
35
|
const key = `${item.text}||${item.context ?? ''}`;
|
|
36
36
|
// Cache lookup
|
|
@@ -70,7 +70,7 @@ export async function handleTranslationRequest(body, configPath) {
|
|
|
70
70
|
}
|
|
71
71
|
}
|
|
72
72
|
// Translate plurals
|
|
73
|
-
for (const item of body.plurals) {
|
|
73
|
+
for (const item of body.plurals ?? []) {
|
|
74
74
|
const stringContextHash = hash(item.context ?? '');
|
|
75
75
|
const sourceKey = item.one + '\x00' + item.other;
|
|
76
76
|
const responseKey = `${sourceKey}||${item.context ?? ''}`;
|
|
@@ -122,7 +122,10 @@ export function createTransDuckHandler(configPath) {
|
|
|
122
122
|
}
|
|
123
123
|
catch (err) {
|
|
124
124
|
console.error('[transduck] Handler error:', err);
|
|
125
|
-
return new Response(JSON.stringify({
|
|
125
|
+
return new Response(JSON.stringify({
|
|
126
|
+
error: 'Translation failed',
|
|
127
|
+
detail: process.env.NODE_ENV === 'development' ? err?.message : undefined,
|
|
128
|
+
}), {
|
|
126
129
|
status: 500,
|
|
127
130
|
headers: { 'Content-Type': 'application/json' },
|
|
128
131
|
});
|
|
@@ -6,11 +6,12 @@
|
|
|
6
6
|
* prompt text contains ${value} patterns that must be preserved literally.
|
|
7
7
|
*/
|
|
8
8
|
const SYSTEM_TEMPLATE = 'You are a professional translator. Translate the given text from {source_lang} ' +
|
|
9
|
-
'to {target_lang}. Return ONLY the translated text, nothing else.
|
|
10
|
-
'
|
|
9
|
+
'to {target_lang}. Return ONLY the translated text, nothing else. ' +
|
|
10
|
+
'Do NOT add quotation marks, brackets, or any wrapper characters that are not in the original. ' +
|
|
11
|
+
'Preserve any placeholders like {name}, {{count}}, %s, ${value} exactly as they appear. ' +
|
|
11
12
|
'Preserve brand names. Match the tone and formality of the original.\n\n' +
|
|
12
13
|
'Project context: {project_context}';
|
|
13
|
-
const USER_TEMPLATE = 'Translate
|
|
14
|
+
const USER_TEMPLATE = 'Translate the following text:\n{source_text}\n\nString context: {string_context}';
|
|
14
15
|
const PLURAL_SYSTEM_TEMPLATE = 'You are a professional translator. You will be given two plural forms ' +
|
|
15
16
|
'(one and other) in {source_lang}. Generate ALL plural forms needed in ' +
|
|
16
17
|
'{target_lang} according to CLDR plural rules. Return ONLY a JSON object ' +
|
package/package.json
CHANGED
package/src/handler.ts
CHANGED
|
@@ -63,7 +63,7 @@ export async function handleTranslationRequest(
|
|
|
63
63
|
const plurals: Record<string, Record<string, string>> = {};
|
|
64
64
|
|
|
65
65
|
// Translate regular strings
|
|
66
|
-
for (const item of body.strings) {
|
|
66
|
+
for (const item of body.strings ?? []) {
|
|
67
67
|
const stringContextHash = hash(item.context ?? '');
|
|
68
68
|
const key = `${item.text}||${item.context ?? ''}`;
|
|
69
69
|
|
|
@@ -113,7 +113,7 @@ export async function handleTranslationRequest(
|
|
|
113
113
|
}
|
|
114
114
|
|
|
115
115
|
// Translate plurals
|
|
116
|
-
for (const item of body.plurals) {
|
|
116
|
+
for (const item of body.plurals ?? []) {
|
|
117
117
|
const stringContextHash = hash(item.context ?? '');
|
|
118
118
|
const sourceKey = item.one + '\x00' + item.other;
|
|
119
119
|
const responseKey = `${sourceKey}||${item.context ?? ''}`;
|
|
@@ -175,9 +175,12 @@ export function createTransDuckHandler(configPath?: string) {
|
|
|
175
175
|
status: 200,
|
|
176
176
|
headers: { 'Content-Type': 'application/json' },
|
|
177
177
|
});
|
|
178
|
-
} catch (err) {
|
|
178
|
+
} catch (err: any) {
|
|
179
179
|
console.error('[transduck] Handler error:', err);
|
|
180
|
-
return new Response(JSON.stringify({
|
|
180
|
+
return new Response(JSON.stringify({
|
|
181
|
+
error: 'Translation failed',
|
|
182
|
+
detail: process.env.NODE_ENV === 'development' ? err?.message : undefined,
|
|
183
|
+
}), {
|
|
181
184
|
status: 500,
|
|
182
185
|
headers: { 'Content-Type': 'application/json' },
|
|
183
186
|
});
|
package/src/providers/prompts.ts
CHANGED
|
@@ -9,13 +9,14 @@
|
|
|
9
9
|
|
|
10
10
|
const SYSTEM_TEMPLATE =
|
|
11
11
|
'You are a professional translator. Translate the given text from {source_lang} ' +
|
|
12
|
-
'to {target_lang}. Return ONLY the translated text, nothing else.
|
|
13
|
-
'
|
|
12
|
+
'to {target_lang}. Return ONLY the translated text, nothing else. ' +
|
|
13
|
+
'Do NOT add quotation marks, brackets, or any wrapper characters that are not in the original. ' +
|
|
14
|
+
'Preserve any placeholders like {name}, {{count}}, %s, ${value} exactly as they appear. ' +
|
|
14
15
|
'Preserve brand names. Match the tone and formality of the original.\n\n' +
|
|
15
16
|
'Project context: {project_context}';
|
|
16
17
|
|
|
17
18
|
const USER_TEMPLATE =
|
|
18
|
-
'Translate
|
|
19
|
+
'Translate the following text:\n{source_text}\n\nString context: {string_context}';
|
|
19
20
|
|
|
20
21
|
const PLURAL_SYSTEM_TEMPLATE =
|
|
21
22
|
'You are a professional translator. You will be given two plural forms ' +
|