gptrans 1.9.0 ā 1.9.2
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/index.js +46 -8
- package/package.json +1 -1
- package/prompt/translate.md +10 -0
package/index.js
CHANGED
|
@@ -218,15 +218,45 @@ class GPTrans {
|
|
|
218
218
|
const textsToTranslate = batch.map(([_, text]) => text).join(`\n${this.divider}\n`);
|
|
219
219
|
try {
|
|
220
220
|
const translations = await this._translate(textsToTranslate, batch, batchReferences, this.preloadBaseLanguage);
|
|
221
|
-
|
|
221
|
+
|
|
222
|
+
// Try different split strategies to be more robust
|
|
223
|
+
let translatedTexts = translations.split(`\n${this.divider}\n`);
|
|
224
|
+
|
|
225
|
+
// If split doesn't match batch size, try alternative separators
|
|
226
|
+
if (translatedTexts.length !== batch.length) {
|
|
227
|
+
// Try without newlines around divider
|
|
228
|
+
translatedTexts = translations.split(this.divider);
|
|
229
|
+
|
|
230
|
+
// If still doesn't match, try with just newline
|
|
231
|
+
if (translatedTexts.length !== batch.length) {
|
|
232
|
+
translatedTexts = translations.split(/\n{2,}/); // Split by multiple newlines
|
|
233
|
+
}
|
|
234
|
+
}
|
|
222
235
|
|
|
223
236
|
const contextHash = this._hash(context);
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
237
|
+
|
|
238
|
+
// Validate we have the right number of translations
|
|
239
|
+
if (translatedTexts.length !== batch.length) {
|
|
240
|
+
console.error(`ā Translation count mismatch:`);
|
|
241
|
+
console.error(` Expected: ${batch.length} translations`);
|
|
242
|
+
console.error(` Received: ${translatedTexts.length} translations`);
|
|
243
|
+
console.error(` Batch keys: ${batch.map(([key]) => key).join(', ')}`);
|
|
244
|
+
console.error(`\nš Full response from model:\n${translations}\n`);
|
|
245
|
+
|
|
246
|
+
// Try to save what we can
|
|
247
|
+
const minLength = Math.min(translatedTexts.length, batch.length);
|
|
248
|
+
for (let i = 0; i < minLength; i++) {
|
|
249
|
+
if (translatedTexts[i] && translatedTexts[i].trim()) {
|
|
250
|
+
this.dbTarget.set(contextHash, batch[i][0], translatedTexts[i].trim());
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
return;
|
|
254
|
+
}
|
|
229
255
|
|
|
256
|
+
batch.forEach(([key], index) => {
|
|
257
|
+
if (!translatedTexts[index] || !translatedTexts[index].trim()) {
|
|
258
|
+
console.error(`ā No translation found for ${key} at index ${index}`);
|
|
259
|
+
console.error(` Original text: ${batch[index][1]}`);
|
|
230
260
|
return;
|
|
231
261
|
}
|
|
232
262
|
|
|
@@ -234,7 +264,8 @@ class GPTrans {
|
|
|
234
264
|
});
|
|
235
265
|
|
|
236
266
|
} catch (e) {
|
|
237
|
-
console.error(e);
|
|
267
|
+
console.error('ā Error in _processBatch:', e.message);
|
|
268
|
+
console.error(e.stack);
|
|
238
269
|
}
|
|
239
270
|
}
|
|
240
271
|
|
|
@@ -319,7 +350,14 @@ class GPTrans {
|
|
|
319
350
|
|
|
320
351
|
model.addText(promptContent);
|
|
321
352
|
|
|
322
|
-
|
|
353
|
+
const response = await model.message();
|
|
354
|
+
|
|
355
|
+
// Extract content from code block if present
|
|
356
|
+
const codeBlockRegex = /```(?:\w*\n)?([\s\S]*?)```/;
|
|
357
|
+
const match = response.match(codeBlockRegex);
|
|
358
|
+
const translatedText = match ? match[1].trim() : response.trim();
|
|
359
|
+
|
|
360
|
+
return translatedText;
|
|
323
361
|
|
|
324
362
|
} finally {
|
|
325
363
|
// Always release the lock
|
package/package.json
CHANGED
package/prompt/translate.md
CHANGED
|
@@ -14,6 +14,16 @@ REFERENCES
|
|
|
14
14
|
# Return Format
|
|
15
15
|
- Provide the final translation within a code block using ```.
|
|
16
16
|
- Do not include alternative translations, only provide the best translation.
|
|
17
|
+
- **IMPORTANT:** If the input contains multiple texts separated by `------`, you MUST return the translations in the SAME ORDER, separated by the EXACT SAME separator:
|
|
18
|
+
```
|
|
19
|
+
Translation 1
|
|
20
|
+
------
|
|
21
|
+
Translation 2
|
|
22
|
+
------
|
|
23
|
+
Translation 3
|
|
24
|
+
```
|
|
25
|
+
- Do not add extra newlines or modify the separator format.
|
|
26
|
+
- Each translation should correspond to each input text in the same order.
|
|
17
27
|
|
|
18
28
|
# Warnings
|
|
19
29
|
- **Context:** I will provide you with a text in FROM_DENONYM FROM_LANG. The goal is to translate it to TARGET_ISO (TARGET_DENONYM TARGET_LANG) while maintaining the essence, style, intention, and tone of the original.
|