feat: add ai writing imitation mode

This commit is contained in:
董宏凯 committed 2026-07-01 11:21:41 +08:00
1 parent 9268c73776
commit bb6b9bd84b
22 files changed
+714 -30

No files matched your search

@@ -5,11 +5,15 @@
"""
from __future__ import annotations
from io import BytesIO
from types import SimpleNamespace
import pytest
from fastapi import UploadFile
from app.gateway.routers.ai_writing import _extract_sample_text
from deerflow.agents.ai_writing.nodes import researcher as R
from deerflow.agents.ai_writing.nodes import sample_analyzer as S
from deerflow.agents.ai_writing.search.base import SearchResult
@@ -107,3 +111,75 @@ async def test_knowledge_base_does_not_orchestrate_skills(monkeypatch):
# 不抛异常即证明没走 _SkillProviderShouldNotRun
out = await R.researcher_node(_state(), {"configurable": {}})
assert out["material_package"]["materials"]
@pytest.mark.asyncio
async def test_sample_analyzer_skips_normal_mode():
out = await S.sample_analyzer_node({"writing_mode_type": "normal"}, {"configurable": {}})
assert out == {}
@pytest.mark.asyncio
async def test_sample_analyzer_builds_style_profile(monkeypatch):
async def fake_llm_json(*_args, **_kwargs):
return {
"summary": "短段落、评论式表达",
"tone": "克制、坚定",
"structure": ["先提出问题", "再分层论证"],
"language_features": ["多用判断句"],
"sentence_style": "短句为主",
"opening_style": "开门见山",
"ending_style": "总结升华",
"imitation_guidance": ["保持评论式节奏"],
"avoid_copying": ["不要照抄原句"],
"sample_outline": ["问题提出", "原因分析"],
}
monkeypatch.setattr(S, "llm_json", fake_llm_json)
out = await S.sample_analyzer_node({
"writing_mode_type": "imitate",
"sample_text": "这是样文第一段。\n\n这是样文第二段。",
"sample_filename": "demo.md",
"user_intent": "写一篇新文章",
}, {"configurable": {"model_name": "m1"}})
assert out["status"] == "researching"
assert out["sample_style_profile"]["summary"] == "短段落、评论式表达"
assert out["sample_style_profile"]["tone"] == "克制、坚定"
assert any(e["type"] == "sample_profile_ready" for e in out["progress_events"])
def test_render_sample_style_section_contains_copy_guardrails():
section = S.render_sample_style_section({
"writing_mode_type": "imitate",
"imitate_strength": "strong",
"preserve_sample_structure": True,
"sample_style_profile": {
"summary": "评论体",
"tone": "正式",
"structure": ["开头提出问题"],
"language_features": ["短句"],
"sentence_style": "长短句结合",
"opening_style": "开门见山",
"ending_style": "总结",
"imitation_guidance": ["保留节奏"],
"avoid_copying": ["不要照抄"],
"sample_outline": ["引入", "展开"],
},
})
assert "样文仿写要求" in section
assert "不得连续照抄样文原句" in section
assert "评论体" in section
@pytest.mark.asyncio
async def test_extract_sample_text_reads_txt_upload():
upload = UploadFile(file=BytesIO(" 样文内容 ".encode("utf-8")), filename="sample.txt")
name, text = await _extract_sample_text(upload)
assert name == "sample.txt"
assert text == "样文内容"