From 0fe4ded2906cd6986b40cce8a03a1695abb7bbe0 Mon Sep 17 00:00:00 2001 From: c836360238-dev Date: Sat, 22 Aug 2026 10:58:57 +0800 Subject: [PATCH] fix: filter NUL characters from chat records --- apps/application/models/application_chat.py | 5 +++++ apps/application/tests.py | 18 ++++++++++++++++-- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/apps/application/models/application_chat.py b/apps/application/models/application_chat.py index e9a3efbc697..20c6e1af20e 100644 --- a/apps/application/models/application_chat.py +++ b/apps/application/models/application_chat.py @@ -109,6 +109,11 @@ class ChatRecord(AppModelMixin): source = models.JSONField(verbose_name="来源", default=dict) ip_address = models.CharField(max_length=128, verbose_name="ip地址", default='') + def save(self, *args, **kwargs): + self.problem_text = self.problem_text.replace("\x00", "") + self.answer_text = self.answer_text.replace("\x00", "") + return super().save(*args, **kwargs) + def get_human_message(self): if 'problem_padding' in self.details: return HumanMessage(content=self.details.get('problem_padding').get('padding_problem_text')) diff --git a/apps/application/tests.py b/apps/application/tests.py index 7ce503c2dd9..b70a266b456 100644 --- a/apps/application/tests.py +++ b/apps/application/tests.py @@ -1,3 +1,17 @@ -from django.test import TestCase +from unittest import mock -# Create your tests here. +from django.test import SimpleTestCase + +from application.models import ChatRecord + + +class ChatRecordTestCase(SimpleTestCase): + def test_save_removes_nul_characters(self): + chat_record = ChatRecord(problem_text="question\x00text", answer_text="answer\x00text") + + with mock.patch("django.db.models.Model.save") as model_save: + chat_record.save() + + self.assertEqual(chat_record.problem_text, "questiontext") + self.assertEqual(chat_record.answer_text, "answertext") + model_save.assert_called_once_with()