@@ -918,9 +918,11 @@ export async function handleClaudeRoutes(ctx: RouteContext): Promise<boolean> {
918918 const userCodexPath = join ( homedir ( ) , '.codex' , 'AGENTS.md' ) ;
919919 const chineseRefPattern = / @ .* c h i n e s e - r e s p o n s e \. m d / i;
920920 const chineseSectionPattern = / # # 中 文 回 复 / ; // For Codex direct content
921+ const oldCodexRefPattern = / - \* \* 中 文 回 复 准 则 \* \* : \s * @ .* c h i n e s e - r e s p o n s e \. m d / i; // Old Codex format
921922
922923 let claudeEnabled = false ;
923924 let codexEnabled = false ;
925+ let codexNeedsMigration = false ;
924926 let guidelinesPath = '' ;
925927
926928 // Check if user CLAUDE.md exists and contains Chinese response reference
@@ -934,6 +936,10 @@ export async function handleClaudeRoutes(ctx: RouteContext): Promise<boolean> {
934936 if ( existsSync ( userCodexPath ) ) {
935937 const content = readFileSync ( userCodexPath , 'utf8' ) ;
936938 codexEnabled = chineseSectionPattern . test ( content ) ;
939+ // Check if Codex has old @ reference format that needs migration
940+ if ( codexEnabled && oldCodexRefPattern . test ( content ) ) {
941+ codexNeedsMigration = true ;
942+ }
937943 }
938944
939945 // Find guidelines file path - always use user-level path
@@ -948,6 +954,7 @@ export async function handleClaudeRoutes(ctx: RouteContext): Promise<boolean> {
948954 enabled : claudeEnabled , // backward compatibility
949955 claudeEnabled,
950956 codexEnabled,
957+ codexNeedsMigration, // New field: true if Codex has old @ reference format
951958 guidelinesPath,
952959 guidelinesExists : ! ! guidelinesPath ,
953960 userClaudeMdExists : existsSync ( userClaudePath ) ,
@@ -1002,11 +1009,36 @@ export async function handleClaudeRoutes(ctx: RouteContext): Promise<boolean> {
10021009 if ( isCodex ) {
10031010 // Codex: Direct content concatenation (does not support @ references)
10041011 const chineseSectionPattern = / \n * # # 中 文 回 复 \n [ \s \S ] * ?(? = \n # # | $ ) / ;
1012+ const oldRefPattern = / - \* \* 中 文 回 复 准 则 \* \* : \s * @ .* c h i n e s e - r e s p o n s e \. m d / i; // Old @ reference format
10051013
10061014 if ( enabled ) {
1007- // Check if section already exists
1008- if ( chineseSectionPattern . test ( content ) ) {
1009- return { success : true , message : 'Already enabled' } ;
1015+ // Check if section exists and if it needs migration
1016+ const hasSection = chineseSectionPattern . test ( content ) ;
1017+
1018+ if ( hasSection ) {
1019+ // Check if it's the old format with @ reference
1020+ const hasOldRef = oldRefPattern . test ( content ) ;
1021+
1022+ if ( hasOldRef ) {
1023+ // Migrate: remove old section and add new content
1024+ content = content . replace ( chineseSectionPattern , '\n' ) ;
1025+ content = content . replace ( / \n { 3 , } / g, '\n\n' ) . trim ( ) ;
1026+ if ( content ) content += '\n' ;
1027+
1028+ // Read chinese-response.md content
1029+ const chineseResponseContent = readFileSync ( userGuidelinesPath , 'utf8' ) ;
1030+
1031+ // Add new section with direct content
1032+ const newSection = `\n## 中文回复\n\n${ chineseResponseContent } \n` ;
1033+ content = content . trimEnd ( ) + '\n' + newSection ;
1034+
1035+ writeFileSync ( targetFile , content , 'utf8' ) ;
1036+
1037+ return { success : true , enabled, migrated : true , message : 'Migrated from @ reference to direct content' } ;
1038+ }
1039+
1040+ // Already has correct format
1041+ return { success : true , message : 'Already enabled with correct format' } ;
10101042 }
10111043
10121044 // Read chinese-response.md content
@@ -1016,7 +1048,7 @@ export async function handleClaudeRoutes(ctx: RouteContext): Promise<boolean> {
10161048 const newSection = `\n## 中文回复\n\n${ chineseResponseContent } \n` ;
10171049 content = content . trimEnd ( ) + '\n' + newSection ;
10181050 } else {
1019- // Remove Chinese response section
1051+ // Remove Chinese response section (both old and new format)
10201052 content = content . replace ( chineseSectionPattern , '\n' ) ;
10211053 content = content . replace ( / \n { 3 , } / g, '\n\n' ) . trim ( ) ;
10221054 if ( content ) content += '\n' ;
0 commit comments