@@ -6,13 +6,16 @@ import { CollectionRefTypes } from '~/constants/db.constant'
66import { ErrorCodeEnum } from '~/constants/error-code.constant'
77import { DatabaseService } from '~/processors/database/database.service'
88import { EventManagerService } from '~/processors/helper/helper.event.service'
9+ import { LexicalService } from '~/processors/helper/helper.lexical.service'
910import {
1011 TaskQueueProcessor ,
1112 TaskQueueService ,
1213 TaskStatus ,
1314 type TaskExecuteContext ,
1415} from '~/processors/task-queue'
16+ import { ContentFormat } from '~/shared/types/content-format.type'
1517import { InjectModel } from '~/transformers/model.transformer'
18+ import { computeContentHash as computeContentHashUtil } from '~/utils/content.util'
1619import { scheduleManager } from '~/utils/schedule.util'
1720import { md5 } from '~/utils/tool.util'
1821import dayjs from 'dayjs'
@@ -47,6 +50,8 @@ interface ArticleContent {
4750 summary ?: string | null
4851 tags ?: string [ ]
4952 meta ?: { lang ?: string }
53+ contentFormat ?: string
54+ content ?: string
5055}
5156
5257type ArticleDocument = PostModel | NoteModel | PageModel
@@ -80,6 +85,7 @@ export class AiTranslationService implements OnModuleInit {
8085 private readonly eventManager : EventManagerService ,
8186 private readonly taskProcessor : TaskQueueProcessor ,
8287 private readonly taskQueueService : TaskQueueService ,
88+ private readonly lexicalService : LexicalService ,
8389 ) { }
8490
8591 onModuleInit ( ) {
@@ -439,6 +445,8 @@ export class AiTranslationService implements OnModuleInit {
439445 summary :
440446 'summary' in document ? ( document . summary ?? undefined ) : undefined ,
441447 tags : 'tags' in document ? document . tags : undefined ,
448+ contentFormat : document . contentFormat ,
449+ content : document . content ,
442450 }
443451 }
444452
@@ -559,6 +567,19 @@ export class AiTranslationService implements OnModuleInit {
559567 document : ArticleContent ,
560568 sourceLang : string ,
561569 ) : string {
570+ if ( document . contentFormat === ContentFormat . Lexical ) {
571+ return computeContentHashUtil (
572+ {
573+ title : document . title ,
574+ text : document . text ,
575+ contentFormat : document . contentFormat ,
576+ content : document . content ,
577+ summary : document . summary ,
578+ tags : document . tags ,
579+ } ,
580+ sourceLang ,
581+ )
582+ }
562583 return md5 (
563584 JSON . stringify ( {
564585 title : document . title ,
@@ -580,8 +601,10 @@ export class AiTranslationService implements OnModuleInit {
580601 feature : 'translation' ,
581602 articleId,
582603 targetLang,
604+ contentFormat : content . contentFormat ,
583605 title : content . title ,
584606 text : content . text ,
607+ content : content . content ?? null ,
585608 summary : content . summary ?? null ,
586609 tags : content . tags ?? null ,
587610 } ) ,
@@ -600,16 +623,26 @@ export class AiTranslationService implements OnModuleInit {
600623 tags : string [ ] | null
601624 aiModel : string
602625 aiProvider : string
626+ contentFormat ?: string
627+ content ?: string
603628 } > {
604629 const { runtime, info } = await this . aiService . getTranslationModelWithInfo ( )
605630
606- const { systemPrompt, prompt, reasoningEffort } =
607- AI_PROMPTS . translationStream ( targetLang , {
608- title : content . title ,
609- text : content . text ,
610- summary : content . summary ?? undefined ,
611- tags : content . tags ,
612- } )
631+ const isLexical = content . contentFormat === ContentFormat . Lexical
632+
633+ const { systemPrompt, prompt, reasoningEffort } = isLexical
634+ ? AI_PROMPTS . translationStreamLexical ( targetLang , {
635+ title : content . title ,
636+ content : content . content ! ,
637+ summary : content . summary ?? undefined ,
638+ tags : content . tags ,
639+ } )
640+ : AI_PROMPTS . translationStream ( targetLang , {
641+ title : content . title ,
642+ text : content . text ,
643+ summary : content . summary ?? undefined ,
644+ tags : content . tags ,
645+ } )
613646
614647 const messages = [
615648 { role : 'system' as const , content : systemPrompt } ,
@@ -652,10 +685,29 @@ export class AiTranslationService implements OnModuleInit {
652685 sourceLang ?: string
653686 title ?: string
654687 text ?: string
688+ content ?: any
655689 summary ?: string | null
656690 tags ?: string [ ] | null
657691 }
658692
693+ if ( isLexical ) {
694+ if ( ! parsed ?. title || ! parsed ?. content || ! parsed ?. sourceLang ) {
695+ throw new Error ( 'Invalid Lexical translation JSON response' )
696+ }
697+ const translatedContent = JSON . stringify ( parsed . content )
698+ return {
699+ sourceLang : parsed . sourceLang ,
700+ title : parsed . title ,
701+ text : this . lexicalService . lexicalToMarkdown ( translatedContent ) ,
702+ contentFormat : ContentFormat . Lexical ,
703+ content : translatedContent ,
704+ summary : parsed . summary ?? null ,
705+ tags : parsed . tags ?? null ,
706+ aiModel : info . model ,
707+ aiProvider : info . provider ,
708+ }
709+ }
710+
659711 if ( ! parsed ?. title || ! parsed ?. text || ! parsed ?. sourceLang ) {
660712 throw new Error ( 'Invalid translation JSON response' )
661713 }
@@ -757,6 +809,8 @@ export class AiTranslationService implements OnModuleInit {
757809 existing . text = translated . text
758810 existing . summary = translated . summary ?? undefined
759811 existing . tags = translated . tags ?? undefined
812+ existing . contentFormat = translated . contentFormat
813+ existing . content = translated . content
760814 if ( sourceModified ) {
761815 existing . sourceModified = sourceModified
762816 }
@@ -783,6 +837,8 @@ export class AiTranslationService implements OnModuleInit {
783837 text : translated . text ,
784838 summary : translated . summary ?? undefined ,
785839 tags : translated . tags ?? undefined ,
840+ contentFormat : translated . contentFormat ,
841+ content : translated . content ,
786842 sourceModified,
787843 aiModel : translated . aiModel ,
788844 aiProvider : translated . aiProvider ,
0 commit comments