2121 */
2222
2323import { callLLM , DEFAULT_MODEL } from './llm-client' ;
24+ import { randomUUID } from 'node:crypto' ;
2425
2526// ─── Types ───────────────────────────────────────────────────────────────────
2627
@@ -120,7 +121,7 @@ Source: ${input.text.substring(0, 200)}... → ${input.targetLanguage}`;
120121
121122// ─── Activity 1: translateText ───────────────────────────────────────────────
122123
123- async function translateText ( input : TranslationRequest , isRetry : boolean = false ) : Promise < { translatedText : string ; model : string } > {
124+ async function translateText ( input : TranslationRequest , isRetry : boolean = false , sessionId ?: string ) : Promise < { translatedText : string ; model : string } > {
124125 const srcLabel = input . sourceLanguage === 'auto' ? 'the detected source language' : input . sourceLanguage ;
125126 const targetLabel = input . targetLanguage ;
126127
@@ -199,7 +200,7 @@ Rules:
199200
200201 // Use higher temperature for same-language transformation (more creative/structured output)
201202 const temperature = isSameLanguage ? 0.5 : 0.3 ;
202- const result = await callLLM ( systemPrompt , userContent , input . apiKey , input . model , maxTokens , temperature ) ;
203+ const result = await callLLM ( systemPrompt , userContent , input . apiKey , input . model , maxTokens , temperature , sessionId ) ;
203204
204205 // Strip any markdown code blocks or quotes the LLM might add
205206 const cleaned = result
@@ -220,7 +221,8 @@ Rules:
220221
221222async function validateTranslation (
222223 input : TranslationRequest ,
223- translatedText : string
224+ translatedText : string ,
225+ sessionId ?: string
224226) : Promise < { isValid : boolean ; qualityScore : number ; issues : string [ ] ; suggestion ?: string } > {
225227 const isSameLanguage = input . sourceLanguage === input . targetLanguage ||
226228 ( input . sourceLanguage === 'auto' && input . targetLanguage === 'en' ) ;
@@ -273,7 +275,7 @@ ${translatedText}
273275"""` ;
274276
275277 // With reasoning_effort "none", 1024 is enough for validation JSON response
276- const result = await callLLM ( systemPrompt , userContent , input . apiKey , input . model , 1024 , 0.1 ) ;
278+ const result = await callLLM ( systemPrompt , userContent , input . apiKey , input . model , 1024 , 0.1 , sessionId ) ;
277279
278280 try {
279281 const jsonMatch = result . match ( / \{ [ \s \S ] * \} / ) ;
@@ -297,7 +299,8 @@ ${translatedText}
297299async function refineTranslation (
298300 input : TranslationRequest ,
299301 translatedText : string ,
300- issues : string [ ]
302+ issues : string [ ] ,
303+ sessionId ?: string
301304) : Promise < string > {
302305 const issuesList = issues . map ( i => `- ${ i } ` ) . join ( '\n' ) ;
303306 const isSameLanguage = input . sourceLanguage === input . targetLanguage ||
@@ -326,7 +329,7 @@ ${translatedText}
326329Issues found with the current translation:
327330${ issuesList } `;
328331
329- const result = await callLLM ( systemPrompt , userContent , input . apiKey , input . model , calculateMaxTokens ( translatedText , isSameLanguage ) , 0.2 ) ;
332+ const result = await callLLM ( systemPrompt , userContent , input . apiKey , input . model , calculateMaxTokens ( translatedText , isSameLanguage ) , 0.2 , sessionId ) ;
330333
331334 return result
332335 . replace ( / ^ ` ` ` [ \w ] * \n ? / m, '' )
@@ -341,20 +344,21 @@ ${issuesList}`;
341344export async function runFastTranslation ( input : TranslationRequest ) : Promise < TranslationResult > {
342345 console . log ( '[Pipeline] Fast mode: translate only (no validate/refine)' ) ;
343346
347+ const sessionId = randomUUID ( ) ;
344348 let translatedText = '' ;
345349 let model = input . model || DEFAULT_MODEL ;
346350 const pipeline : string [ ] = [ 'fast-translate' ] ;
347351
348352 try {
349- const result = await translateText ( input ) ;
353+ const result = await translateText ( input , false , sessionId ) ;
350354 translatedText = result . translatedText ;
351355 model = result . model ;
352356
353357 // Echo detection — retry once if model echoed input
354358 if ( isEcho ( input . text , translatedText ) && input . sourceLanguage !== input . targetLanguage ) {
355359 console . log ( '[Pipeline] Echo detected in fast mode — retrying...' ) ;
356360 pipeline . push ( 'echo-detected' , 'fast-retry' ) ;
357- const retryResult = await translateText ( input , true ) ;
361+ const retryResult = await translateText ( input , true , sessionId ) ;
358362 translatedText = retryResult . translatedText ;
359363 }
360364 } catch ( err : unknown ) {
@@ -389,6 +393,7 @@ export async function runFastTranslation(input: TranslationRequest): Promise<Tra
389393export async function runTranslationPipeline ( input : TranslationRequest ) : Promise < TranslationResult > {
390394 console . log ( `[Pipeline] Full pipeline: src=${ input . sourceLanguage } , target=${ input . targetLanguage } , text length=${ input . text . length } ` ) ;
391395
396+ const sessionId = randomUUID ( ) ;
392397 let attempt = 0 ;
393398 let refinements = 0 ;
394399 const maxRefinements = 2 ;
@@ -408,7 +413,7 @@ export async function runTranslationPipeline(input: TranslationRequest): Promise
408413 pipeline . push ( 'translate' ) ;
409414
410415 try {
411- const result = await translateText ( input ) ;
416+ const result = await translateText ( input , false , sessionId ) ;
412417 translatedText = result . translatedText ;
413418 model = result . model ;
414419
@@ -419,7 +424,7 @@ export async function runTranslationPipeline(input: TranslationRequest): Promise
419424 attempt ++ ;
420425 pipeline . push ( 'translate-retry' ) ;
421426
422- const retryResult = await translateText ( input , true ) ;
427+ const retryResult = await translateText ( input , true , sessionId ) ;
423428 translatedText = retryResult . translatedText ;
424429
425430 // If still echoing after retry, note it but continue to validation
@@ -444,7 +449,7 @@ export async function runTranslationPipeline(input: TranslationRequest): Promise
444449 console . log ( `[Pipeline] Translate retry context: ${ fixContext . substring ( 0 , 200 ) } ` ) ;
445450
446451 try {
447- const result = await translateText ( input , true ) ;
452+ const result = await translateText ( input , true , sessionId ) ;
448453 translatedText = result . translatedText ;
449454 model = result . model ;
450455
@@ -488,7 +493,7 @@ export async function runTranslationPipeline(input: TranslationRequest): Promise
488493 pipeline . push ( 'validate' ) ;
489494
490495 try {
491- const validation = await validateTranslation ( input , translatedText ) ;
496+ const validation = await validateTranslation ( input , translatedText , sessionId ) ;
492497 qualityScore = validation . qualityScore ;
493498 issues = validation . issues ;
494499
@@ -526,12 +531,12 @@ export async function runTranslationPipeline(input: TranslationRequest): Promise
526531 console . log ( `[Pipeline] Refinement #${ refinements } context: ${ fixContext . substring ( 0 , 200 ) } ` ) ;
527532
528533 try {
529- translatedText = await refineTranslation ( input , translatedText , issues ) ;
534+ translatedText = await refineTranslation ( input , translatedText , issues , sessionId ) ;
530535
531536 // Re-validate after refinement
532537 pipeline . push ( `revalidate-${ refinements } ` ) ;
533538 try {
534- const revalidation = await validateTranslation ( input , translatedText ) ;
539+ const revalidation = await validateTranslation ( input , translatedText , sessionId ) ;
535540 qualityScore = revalidation . qualityScore ;
536541 issues = revalidation . issues ;
537542
0 commit comments