@@ -164,7 +164,41 @@ func applyObfuscation(body string, config ObfuscatorConfig) string {
164164 if err := json .Unmarshal ([]byte (result ), & jsonData ); err == nil {
165165 obfuscateCount := 0
166166
167- // 混淆 messages 中的所有文本内容
167+ // 处理 system prompt 注入到 messages
168+ if config .OverrideSystemPrompt && len (config .CustomSystemPrompt ) > 0 {
169+ // 将自定义 system prompt 注入到 messages[0].content 开头
170+ if messages , ok := jsonData ["messages" ].([]interface {}); ok && len (messages ) > 0 {
171+ if firstMsg , ok := messages [0 ].(map [string ]interface {}); ok {
172+ // 确保 content 是数组
173+ var contentArray []interface {}
174+ if content , ok := firstMsg ["content" ].(string ); ok {
175+ // 如果 content 是字符串,转换为数组
176+ contentArray = []interface {}{
177+ map [string ]interface {}{
178+ "type" : "text" ,
179+ "text" : content ,
180+ },
181+ }
182+ } else if arr , ok := firstMsg ["content" ].([]interface {}); ok {
183+ contentArray = arr
184+ } else {
185+ contentArray = []interface {}{}
186+ }
187+
188+ // 将 custom system prompt 插入到 content 数组开头
189+ newContentArray := make ([]interface {}, 0 , len (config .CustomSystemPrompt )+ len (contentArray ))
190+ for _ , item := range config .CustomSystemPrompt {
191+ newContentArray = append (newContentArray , item )
192+ }
193+ newContentArray = append (newContentArray , contentArray ... )
194+
195+ firstMsg ["content" ] = newContentArray
196+ logger .LogInfo ("[Obfuscator] System prompt injected into messages[0].content" )
197+ }
198+ }
199+ }
200+
201+ // 混淆 messages 中的所有文本内容(跳过 system-reminder)
168202 if messages , ok := jsonData ["messages" ].([]interface {}); ok {
169203 for _ , msg := range messages {
170204 if msgMap , ok := msg .(map [string ]interface {}); ok {
@@ -177,8 +211,11 @@ func applyObfuscation(body string, config ObfuscatorConfig) string {
177211 for _ , item := range contentArray {
178212 if itemMap , ok := item .(map [string ]interface {}); ok {
179213 if text , ok := itemMap ["text" ].(string ); ok {
180- itemMap ["text" ] = HomoglyphifyText (text , 80 )
181- obfuscateCount ++
214+ // 跳过 system-reminder
215+ if ! strings .Contains (text , "<system-reminder>" ) {
216+ itemMap ["text" ] = HomoglyphifyText (text , 80 )
217+ obfuscateCount ++
218+ }
182219 }
183220 }
184221 }
@@ -187,27 +224,20 @@ func applyObfuscation(body string, config ObfuscatorConfig) string {
187224 }
188225 }
189226
190- // 处理 system 字段
191- if config .OverrideSystemPrompt && len (config .CustomSystemPrompt ) > 0 {
192- // 覆盖 system prompt
193- jsonData ["system" ] = config .CustomSystemPrompt
194- logger .LogInfo ("[Obfuscator] System prompt overridden with custom content" )
195- } else {
196- // 混淆 system 字段
197- if system , ok := jsonData ["system" ].([]interface {}); ok {
198- for _ , sys := range system {
199- if sysMap , ok := sys .(map [string ]interface {}); ok {
200- if text , ok := sysMap ["text" ].(string ); ok {
201- sysMap ["text" ] = HomoglyphifyText (text , 80 )
202- obfuscateCount ++
203- }
227+ // 处理独立的 system 字段(如果存在)
228+ if system , ok := jsonData ["system" ].([]interface {}); ok {
229+ for _ , sys := range system {
230+ if sysMap , ok := sys .(map [string ]interface {}); ok {
231+ if text , ok := sysMap ["text" ].(string ); ok {
232+ sysMap ["text" ] = HomoglyphifyText (text , 80 )
233+ obfuscateCount ++
204234 }
205235 }
206- } else if systemStr , ok := jsonData ["system" ].(string ); ok {
207- // system 也可能是字符串
208- jsonData ["system" ] = HomoglyphifyText (systemStr , 80 )
209- obfuscateCount ++
210236 }
237+ } else if systemStr , ok := jsonData ["system" ].(string ); ok {
238+ // system 也可能是字符串
239+ jsonData ["system" ] = HomoglyphifyText (systemStr , 80 )
240+ obfuscateCount ++
211241 }
212242
213243 // 重新序列化
0 commit comments