Prechádzať zdrojové kódy

fix: tolerate corrupt style sample tags

lingfengQAQ 3 týždňov pred
rodič
commit
41c93cc6de

+ 10 - 1
webnovel-writer/scripts/data_modules/style_sampler.py

@@ -143,6 +143,15 @@ class StyleSampler:
 
             return [self._row_to_sample(row) for row in cursor.fetchall()]
 
+    def _safe_tags(self, raw) -> List[str]:
+        if not raw:
+            return []
+        try:
+            value = json.loads(raw)
+        except (TypeError, json.JSONDecodeError):
+            return []
+        return value if isinstance(value, list) else []
+
     def _row_to_sample(self, row) -> StyleSample:
         """将数据库行转换为样本对象"""
         return StyleSample(
@@ -151,7 +160,7 @@ class StyleSampler:
             scene_type=row[2],
             content=row[3],
             score=row[4],
-            tags=json.loads(row[5]) if row[5] else [],
+            tags=self._safe_tags(row[5]),
             created_at=row[6]
         )
 

+ 19 - 0
webnovel-writer/scripts/data_modules/tests/test_style_sampler_cli.py

@@ -56,6 +56,25 @@ def test_style_sampler_more(temp_project):
     assert "战斗" in tags
 
 
+def test_style_sampler_ignores_corrupt_tag_json(temp_project):
+    sampler = StyleSampler(temp_project)
+    with sampler._get_conn() as conn:
+        conn.execute(
+            """
+            INSERT INTO samples
+            (id, chapter, scene_type, content, score, tags, created_at)
+            VALUES (?, ?, ?, ?, ?, ?, CURRENT_TIMESTAMP)
+            """,
+            ("bad-tags", 1, SceneType.BATTLE.value, "战斗描写" * 50, 0.8, "[bad-json"),
+        )
+        conn.commit()
+
+    samples = sampler.get_best_samples(limit=5)
+
+    assert samples[0].id == "bad-tags"
+    assert samples[0].tags == []
+
+
 def test_style_sampler_cli(temp_project, monkeypatch, capsys):
     root = str(temp_project.project_root)