feat(v0.11): 写作教练 #12 P0+P1 + 缓存刷新 #8 + 连读 KB #6#18
Merged
Conversation
后端:
- polish_cards 表(含 FSRS 字段,P0 不主动调度)
- /polish/text 一次性润色 · 返回 polished + segments[] + overall_note
- /polish/chat 多轮对话润色(更口语 / 更正式 / 更简洁)
- /polish/cards CRUD + /polish/cards/{id}/rate(FSRS)+ /polish/due
- 兜底:LLM 返回非 JSON 时退回原文 + 空 segments,不报错
- 14 个单测覆盖 dedup / revive / 评分 / 解析 / 兜底
前端:
- /polish 页面 · 输入区 + 结果区(polished + 逐处 diff + 解释 + 整体 note)
- 多轮 chat(4 个 quick 指令 + 自定义指令)
- 单条 segment ⭐ 收藏 + 整体 ⭐ 保存
- 已保存卡片列表 + 删除
- 复用 AskPanel(scope='polish')
- Home 加导航入口
P1 待做:FSRS 自动入队、写作偏好提取、system prompt 注入、due 复习页面。
Closes #12 (P0)
https://claude.ai/code/session_01NzskDVa4Gcbp5P7fgBFK8C
后端:
- _PROMPT_HASH 计算 EXPLAIN_*_PROMPT 的 sha256 前 8 位,纳入 _hash_text
→ prompt 一改 hash 一变,旧 cache 自然 miss,无需手动 bump _SCHEMA_VERSION
- explain_text(force=True) 跳过缓存重新生成 + 覆盖旧值
- stream_sentence_explanation(force=True) 同理
- _cache_set(overwrite=True) 支持覆盖(命中既存行时清零 hit_count)
- 防滥用:同 (user, text_hash) force 3s 内 debounce,超频抛 ValueError
- 5 个单测覆盖 hash / overwrite / debounce / 跨用户隔离
API:
- POST /practice/explain · body 加 refresh: bool = False
- POST /practice/explain/stream · 同理
- 流式 force 走 debounce 时返回 {"_error": "刷新过于频繁..."}
前端:
- ExplanationModal 头部新增 🔄 按钮(在 🎧 左侧)
- 点击 confirm() 二次确认 → fetchExplanation(refresh=true)
- refreshing 态时图标转 + 禁用
Closes #8
https://claude.ai/code/session_01NzskDVa4Gcbp5P7fgBFK8C
新建:
- app/knowledge/liaison_patterns.json · 10 个核心连读模式
vv_j / vv_w / cv_linking / t_flap / consonant_absorption / ndt_release
/ h_deletion / weak_form / yod_coalescence / contraction
每条含:pattern_id, name, trigger, plain(通俗解释), feel(听感比喻),
examples(2-5 个), tip
- app/knowledge/__init__.py · load_liaison_patterns() + liaison_prompt_block()
把 KB 渲染成插入 explain prompt 的紧凑 block
修改:
- EXPLAIN_SENTENCE_PROMPT 加 {liaison_kb} 占位 + 强制要求 tip 用「听感比喻 +
通俗类比」+ 末尾 [pattern_id] 标注
- explain_service _SCHEMA_VERSION 5 → 6(叠加 #8 的 _PROMPT_HASH,旧 cache
自动失效)
- explain_text + stream_sentence_explanation 把 KB 注入到 prompt
- 7 个单测覆盖 KB 完整性 / 字段 / 唯一 ID / prompt format / schema bump
效果:
- 之前 tip:「元音 + 元音 用 j 衔接」(教材腔)
- 之后 tip:「t 夹在元音里软化成 d,听起来像 ge-rit [t_flap]」(听感)
Closes #6
https://claude.ai/code/session_01NzskDVa4Gcbp5P7fgBFK8C
后端:
- app/services/polish_facts.py
· extract_and_save_writing_facts: 从 polish chat 上下文提炼 1-3 条写作偏好
→ user_facts 表,source_session_id 用 'polish:' 前缀,幂等
· get_writing_facts: 取该用户最近 polish 偏好
· get_top_categories: 按 polish_cards 出现频率排序的高频问题类型
· build_polish_addon: 拼接 system prompt 补充块(写作偏好 + 高频错误)
- polish_service.polish_text / polish_chat_once 接受 user_id 参数 · 自动注入 addon
- polish_service.save_card 默认 init_fsrs=True(之前 False)
- polish.py /polish/chat 走 BackgroundTasks 异步抽取偏好(不阻塞响应)
- polish.py PolishCardCreate.init_fsrs 默认 True
- 11 个 P1 单测覆盖:
· 提取触发条件(≥2 轮才抽)、幂等、garbage 兜底
· category 频次排序
· addon 空 / 注入两类信息
· polish_text 注入 spy 验证
· save_card init_fsrs 默认行为
前端:
- ReviewHome 新增「写作卡片」section,列出 due polish_cards
· diff 形式 (original → polished) + 解释 + 4 档评分
· ratePolish 调 /polish/cards/{id}/rate
- totalDue 把 polish 也算进去
闭环:
写作 → polish 多轮 → 抽偏好 fact → 下次自动注入 prompt
收藏 segment → FSRS 自动入队 → 到期出现在 ReviewHome → 评分推进
Closes #12 (P1)
https://claude.ai/code/session_01NzskDVa4Gcbp5P7fgBFK8C
main 上后续合入了 model_router(PR #10),与本分支 polish_router 在 main.py 同位置冲突。两者并存即可。 https://claude.ai/code/session_01NzskDVa4Gcbp5P7fgBFK8C
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
V0.11 一周 issue 集中实现:写作教练(#12 P0+P1)、解读缓存刷新(#8)、连读通俗化知识库(#6)。共 4 个 commit、约 1900 insertions,38 个新单测全绿。
4642dca/polish页面 + 6 个 API + Polish.vue + 14 测试0501021_PROMPT_HASH)+ 手动 🔄 重生成(3s debounce + 二次确认)+ 5 测试5471e7bliaison_patterns.json10 模式 + prompt 注入 + 通俗化 tip 风格 + 7 测试394c79d关键改动点
Backend
polish_cards表(V0.11 迁移脚本scripts/migrate_v0_11_polish.py)app/routers/polish.py:/polish/text、/polish/chat、/polish/cardsCRUD、/polish/cards/{id}/rate、/polish/duepolish_service+polish_facts(写作偏好异步提取 → 注入下次润色 prompt)explain_service:_PROMPT_HASH自动失效缓存 +force参数手动重生成 + 防滥用 debounce_SCHEMA_VERSION5 → 6(叠加_PROMPT_HASH双保险)app/knowledge/liaison_patterns.json+liaison_prompt_block()helper · 注入 EXPLAIN_SENTENCE_PROMPTFrontend
/polish页面(Polish.vue)· 输入区 + diff 结果 + chat 多轮 + AskPanel 复用Test plan
pytest tests/test_v011_*.py→ 38 passednpm run build→ 全部 chunk 干净生成Out of scope (待你确认)
🤖 Generated with Claude Code
Generated by Claude Code