fix: improve ai writing chat streaming and intent handling
This commit is contained in:
1 parent
845c87e3ee
commit
da856f2b58
5 files changed
+1633
-158
No files matched your search
+175
-11
@@ -17,7 +17,7 @@ from __future__ import annotations
|
||||
|
||||
import logging
|
||||
import re
|
||||
from typing import Any
|
||||
from typing import Any, AsyncIterator
|
||||
|
||||
from .prompts.intent_router_prompts import (
|
||||
INTENT_ROUTER_SYSTEM,
|
||||
@@ -48,6 +48,73 @@ ACTION_CONFIDENCE = 0.5
|
||||
_SECTION_DECISIONS = {"supplement", "loose", "delete"}
|
||||
|
||||
_THINK_RE = re.compile(r"<think>.*?</think>", re.S)
|
||||
_WRITING_ACTION_VERB_RE = re.compile(
|
||||
r"(新增|增加|添加|加入|加上|补充|补上|补个|补一个|生成|撰写|写一|写个|写成|扩写|缩写|精简|改写|润色|调整|修改|重写|删除|删掉|拆成|合并|挪到|移动|规划|重新规划|设计|安排)"
|
||||
)
|
||||
_WRITING_ACTION_TARGET_RE = re.compile(
|
||||
r"(第[一二三四五六七八九十\d]+[章节段]|章节|章|小节|段落|段|结尾|开头|标题|大纲|内容|案例|表格|总结章|总结章节|结论|摘要|观点|部分)"
|
||||
)
|
||||
_INFO_QUESTION_RE = re.compile(r"(哪些|什么|为什么|是什么|有哪些|多少|是否)")
|
||||
|
||||
|
||||
def _fallback_intent() -> dict:
|
||||
return {
|
||||
"intent": "clarify",
|
||||
"action": None,
|
||||
"payload": {},
|
||||
"need_research": False,
|
||||
"research_query": "",
|
||||
"answer": "",
|
||||
"confidence": 0.0,
|
||||
"clarify": "我没有理解你的意思,能换个说法再试一次吗?也可以直接点击上方卡片里的按钮操作。",
|
||||
}
|
||||
|
||||
|
||||
def _looks_like_writing_change_request(text: str) -> bool:
|
||||
text = (text or "").strip()
|
||||
if _INFO_QUESTION_RE.search(text):
|
||||
return False
|
||||
return bool(_WRITING_ACTION_VERB_RE.search(text) and _WRITING_ACTION_TARGET_RE.search(text))
|
||||
|
||||
|
||||
def _rule_based_intent(text: str, pause_point: str | None) -> dict | None:
|
||||
"""Deterministic guard for writing-operation requests that look like Q&A."""
|
||||
if not _looks_like_writing_change_request(text):
|
||||
return None
|
||||
if pause_point == "outline_confirm":
|
||||
return {
|
||||
"intent": "revise_outline",
|
||||
"action": "re_outline",
|
||||
"payload": {"outlineFeedback": text.strip()},
|
||||
"need_research": False,
|
||||
"research_query": "",
|
||||
"answer": "",
|
||||
"confidence": 1.0,
|
||||
"clarify": "",
|
||||
}
|
||||
if pause_point == "draft_confirm":
|
||||
return {
|
||||
"intent": "revise_draft",
|
||||
"action": "user_revise",
|
||||
"payload": {"userRevisionNotes": text.strip()},
|
||||
"need_research": False,
|
||||
"research_query": "",
|
||||
"answer": "",
|
||||
"confidence": 1.0,
|
||||
"clarify": "",
|
||||
}
|
||||
if pause_point == "review_confirm":
|
||||
return {
|
||||
"intent": "accept_review_with_user_notes",
|
||||
"action": "accept_review",
|
||||
"payload": {"userRevisionNotes": text.strip()},
|
||||
"need_research": False,
|
||||
"research_query": "",
|
||||
"answer": "",
|
||||
"confidence": 1.0,
|
||||
"clarify": "",
|
||||
}
|
||||
return None
|
||||
|
||||
|
||||
# ── 上下文快照 ────────────────────────────────────────────────────────────────
|
||||
@@ -362,16 +429,10 @@ async def parse_user_intent(
|
||||
"""
|
||||
from .nodes._utils import _content_to_text, parse_json
|
||||
|
||||
fallback = {
|
||||
"intent": "clarify",
|
||||
"action": None,
|
||||
"payload": {},
|
||||
"need_research": False,
|
||||
"research_query": "",
|
||||
"answer": "",
|
||||
"confidence": 0.0,
|
||||
"clarify": "我没有理解你的意思,能换个说法再试一次吗?也可以直接点击上方卡片里的按钮操作。",
|
||||
}
|
||||
fallback = _fallback_intent()
|
||||
rule = _rule_based_intent(text, pause_point)
|
||||
if rule is not None:
|
||||
return normalize_intent_result(rule, pause_point, values, interrupt_value)
|
||||
|
||||
messages = _build_messages(text, pause_point, values, interrupt_value)
|
||||
|
||||
@@ -407,3 +468,106 @@ async def parse_user_intent(
|
||||
|
||||
parsed = parse_json(raw_text, fallback)
|
||||
return normalize_intent_result(parsed, pause_point, values, interrupt_value)
|
||||
|
||||
|
||||
async def stream_user_intent(
|
||||
*,
|
||||
text: str,
|
||||
pause_point: str | None,
|
||||
values: dict,
|
||||
interrupt_value: dict | None = None,
|
||||
model_name: str | None = None,
|
||||
model: Any | None = None,
|
||||
) -> AsyncIterator[dict]:
|
||||
"""流式解析用户自由文本。
|
||||
|
||||
事件契约:
|
||||
- ``{"type": "chunk", "chunk": "...", "is_thinking": bool}``
|
||||
- ``{"type": "result", "result": normalized_intent}``
|
||||
|
||||
解析阶段仍以结构化 JSON 为最终产物;流式输出用于前端实时展示模型的
|
||||
思考/解析过程。``<think>...</think>`` 内容和 provider reasoning 字段都会作为
|
||||
``is_thinking=True`` 分流,正文 JSON 只参与最终解析。
|
||||
"""
|
||||
from .nodes._utils import ThinkTagParser, _content_to_text, parse_json, reasoning_delta
|
||||
|
||||
fallback = _fallback_intent()
|
||||
rule = _rule_based_intent(text, pause_point)
|
||||
if rule is not None:
|
||||
yield {
|
||||
"type": "result",
|
||||
"result": normalize_intent_result(rule, pause_point, values, interrupt_value),
|
||||
}
|
||||
return
|
||||
|
||||
messages = _build_messages(text, pause_point, values, interrupt_value)
|
||||
|
||||
def content_events(parser: ThinkTagParser, raw_parts: list[str], delta: str) -> list[dict]:
|
||||
events = []
|
||||
for is_thinking, part in parser.feed(delta):
|
||||
if not is_thinking:
|
||||
raw_parts.append(part)
|
||||
events.append({"type": "chunk", "chunk": part, "is_thinking": is_thinking})
|
||||
return events
|
||||
|
||||
async def run_model(m: Any) -> AsyncIterator[dict]:
|
||||
parser = ThinkTagParser()
|
||||
raw_parts: list[str] = []
|
||||
if hasattr(m, "astream"):
|
||||
async for chunk in m.astream(messages):
|
||||
reasoning = reasoning_delta(chunk)
|
||||
if reasoning:
|
||||
yield {"type": "chunk", "chunk": reasoning, "is_thinking": True}
|
||||
content = _content_to_text(getattr(chunk, "content", "") or "")
|
||||
if content:
|
||||
for event in content_events(parser, raw_parts, content):
|
||||
yield event
|
||||
else:
|
||||
resp = await m.ainvoke(messages)
|
||||
content = _content_to_text(getattr(resp, "content", "") or "")
|
||||
if content:
|
||||
for event in content_events(parser, raw_parts, content):
|
||||
yield event
|
||||
yield {"type": "_raw_text", "raw_text": "".join(raw_parts)}
|
||||
|
||||
raw_text = ""
|
||||
if model is not None:
|
||||
try:
|
||||
async for event in run_model(model):
|
||||
if event["type"] == "_raw_text":
|
||||
raw_text = str(event.get("raw_text") or "")
|
||||
else:
|
||||
yield event
|
||||
except Exception:
|
||||
raw_text = ""
|
||||
logger.exception("intent_router: injected stream model failed")
|
||||
else:
|
||||
from deerflow.models import create_chat_model
|
||||
|
||||
from .nodes._model_fallback import classify_exception, fallback_chain
|
||||
|
||||
chain = fallback_chain((model_name or "").strip() or None) or [None]
|
||||
for candidate in chain:
|
||||
try:
|
||||
m = create_chat_model(name=candidate) if candidate else create_chat_model()
|
||||
async for event in run_model(m):
|
||||
if event["type"] == "_raw_text":
|
||||
raw_text = str(event.get("raw_text") or "")
|
||||
else:
|
||||
yield event
|
||||
break
|
||||
except Exception as exc: # noqa: BLE001 — 换下一个模型继续
|
||||
raw_text = ""
|
||||
logger.warning(
|
||||
"intent_router: stream model=%s failed (reason=%s): %s",
|
||||
candidate, classify_exception(exc), exc,
|
||||
)
|
||||
continue
|
||||
|
||||
raw_text = _THINK_RE.sub("", raw_text or "").strip()
|
||||
if not raw_text:
|
||||
result = normalize_intent_result(fallback, pause_point, values, interrupt_value)
|
||||
else:
|
||||
parsed = parse_json(raw_text, fallback)
|
||||
result = normalize_intent_result(parsed, pause_point, values, interrupt_value)
|
||||
yield {"type": "result", "result": result}
|
||||
Reference in new issue
Block a user