@@ -25,14 +25,15 @@ const ignoredTags = [
25
25
"SCHEMEINLINE" ,
26
26
"SCHEME" ,
27
27
"LONG_PAGE" ,
28
- "LABEL"
28
+ "LABEL" ,
29
+ "HISTORY"
29
30
] ;
30
31
31
32
const MAXLEN = Number ( process . env . MAX_LEN ) || 3000 ;
32
33
33
34
// change to true to avoid calling openai api, useful for troubleshooting
34
35
// chunking logic
35
- const troubleshoot = true ;
36
+ const troubleshoot = false ;
36
37
37
38
// Centralized logging to prevent duplicate messages
38
39
const errorMessages = new Set ( ) ;
@@ -412,33 +413,46 @@ async function recursivelyTranslate(
412
413
try {
413
414
await ai . beta . threads . messages . create ( thread . id , {
414
415
role : "user" ,
415
- content : `Translate this content to ${ language } .
416
- IMPORTANT: You MUST search the uploaded reference file for any technical terms and use EXACTLY the translations specified there.
417
- If a term exists in the reference file, use that translation without deviation.
418
- Do not modify XML tags, attributes of XML tags and structure. Do not say anything else.
419
- Content to translate:
420
- <TRANSLATE> ${ chunk } </TRANSLATE>`
416
+ content : `<TRANSLATE> ${ chunk } </TRANSLATE>`
421
417
} ) ;
422
418
423
419
const run = await ai . beta . threads . runs . createAndPoll ( thread . id , {
424
- assistant_id : assistant_id
420
+ assistant_id : assistant_id ,
421
+ truncation_strategy : {
422
+ type : "last_messages" ,
423
+ last_messages : Number ( process . env . CONTEXT ) || 3
424
+ }
425
425
} ) ;
426
426
427
427
const messages = await ai . beta . threads . messages . list ( thread . id , {
428
- run_id : run . id
428
+ run_id : run . id ,
429
+ limit : Number ( process . env . CONTEXT || 3 )
429
430
} ) ;
430
431
431
- const message = messages . data . pop ( ) ! ;
432
- const messageContent = message ?. content [ 0 ] ;
432
+ const message = messages . data . pop ( ) ;
433
+
434
+ if ( message ) {
435
+ if ( message . status === "incomplete" ) {
436
+ throw new Error (
437
+ "AI Error: incomplete response: " + message . incomplete_details
438
+ ) ;
439
+ }
440
+
441
+ if ( message . content [ 0 ] . type !== "text" ) {
442
+ throw new Error ( "AI Error: Unexpected response format" ) ;
443
+ }
433
444
434
- if ( ! messageContent ) throw new Error ( `undefined AI response` ) ;
435
- if ( messageContent . type !== "text" ) {
436
- throw new Error (
437
- `Unexpected message content type: ${ messageContent . type } `
438
- ) ;
445
+ if (
446
+ run . usage &&
447
+ run . usage . total_tokens > Number ( process . env . TOKEN_WARNING || 10000 )
448
+ ) {
449
+ console . warn ( "warning: high token usage" , run . usage ) ;
450
+ }
451
+ } else {
452
+ throw new Error ( "AI Error: Undefined Response" ) ;
439
453
}
440
454
441
- const text = messageContent . text ;
455
+ const text = message . content [ 0 ] . text ;
442
456
443
457
const safeText = escapeXML ( text . value ) ;
444
458
const textStream = Readable . from ( "<WRAPPER>" + safeText + "</WRAPPER>" ) ;
0 commit comments