feat(turbo): add direct LLM chat mode with runtime toggle
Add 极速 mode that streams via /api/direct-chat without the lead agent stack, plus VITE_TURBO_MODE_VISIBLE in runtime-config.js (default off). Also disable global memory, tighten Qwen/vLLM thinking control for flash mode, and force thinking off on the direct-chat endpoint. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
4f7f07504a
commit
d765afe2ac
23 files changed
+1295
-394
No files matched your search
+132
-130
@@ -40,77 +40,77 @@ logger = logging.getLogger(__name__)
|
||||
WRITING_SETUP_MAX_ROUNDS = 8
|
||||
REPORT_STRUCTURE_SETUP_MAX_ROUNDS = 8
|
||||
|
||||
_ES_QUERY_ROUTING_OPEN_TAG = '<skill_routing skill="es_query">'
|
||||
|
||||
_SKILLS_BLOCKED_BY_EXCLUDED_TOOL: dict[str, frozenset[str]] = {
|
||||
# 内置 tool 与历史 custom skill 目录存在两种拼写;禁用编排工具时两者都必须从系统提示和
|
||||
# runtime skill allowlist 中消失,防止误配的席位仍看到「加载编排技能」。
|
||||
"agent_orchestration": frozenset({"agent_orchestration", "agent-orchestration"}),
|
||||
}
|
||||
|
||||
_ROUNDTABLE_COORDINATOR_AGENT_ID = "roundtable-coordinator"
|
||||
|
||||
_NON_COORDINATOR_ORCHESTRATION_BOUNDARY = """
|
||||
<orchestration_capability_boundary>
|
||||
你不是多智能体圆桌总控,因此没有编排、派活或调度其它智能体的权限。
|
||||
不得加载、读取、检索或调用 agent_orchestration / agent-orchestration,
|
||||
不得声称准备使用编排技能;请直接完成当前问答或你的本职任务。
|
||||
</orchestration_capability_boundary>
|
||||
""".strip()
|
||||
|
||||
|
||||
def _enforce_coordinator_only_tool_exclusions(
|
||||
agent_id: str | None,
|
||||
excluded_tools: list[str] | None,
|
||||
) -> list[str] | None:
|
||||
"""Make orchestration unavailable to every agent except the fixed roundtable coordinator."""
|
||||
if agent_id == _ROUNDTABLE_COORDINATOR_AGENT_ID:
|
||||
return excluded_tools
|
||||
effective = list(excluded_tools or [])
|
||||
if "agent_orchestration" not in effective:
|
||||
effective.append("agent_orchestration")
|
||||
return effective
|
||||
|
||||
|
||||
def _append_orchestration_capability_boundary(system_prompt: str, agent_id: str | None) -> str:
|
||||
"""Reinforce the physical tool/skill gate in every non-coordinator system prompt."""
|
||||
if agent_id == _ROUNDTABLE_COORDINATOR_AGENT_ID:
|
||||
return system_prompt
|
||||
return f"{system_prompt}\n\n{_NON_COORDINATOR_ORCHESTRATION_BOUNDARY}"
|
||||
|
||||
|
||||
def _filter_available_skills_for_excluded_tools(
|
||||
available_skills: list[str] | None,
|
||||
excluded_tools: list[str] | None,
|
||||
) -> list[str] | None:
|
||||
"""让工具级禁用同步收紧 skill allowlist,必要时把「全部」展开为安全列表。"""
|
||||
if not excluded_tools:
|
||||
return available_skills
|
||||
excluded = {name.strip().lower() for name in excluded_tools if name}
|
||||
blocked = {
|
||||
skill_name
|
||||
for tool_name, skill_names in _SKILLS_BLOCKED_BY_EXCLUDED_TOOL.items()
|
||||
if tool_name in excluded
|
||||
for skill_name in skill_names
|
||||
}
|
||||
if not blocked:
|
||||
return available_skills
|
||||
if available_skills is None:
|
||||
# ``None`` 原本表示「全部启用技能可见」,无法表达「除了总控专属技能之外的全部」。
|
||||
# 此时显式展开存储中的启用技能;扫描失败则 fail closed,避免角色隔离失效。
|
||||
try:
|
||||
from deerflow.skills.storage import get_or_new_skill_storage
|
||||
|
||||
available_skills = [
|
||||
skill.name for skill in get_or_new_skill_storage().load_skills(enabled_only=True)
|
||||
]
|
||||
except Exception: # noqa: BLE001
|
||||
logger.warning("Failed to expand skills while applying excluded-tool boundary", exc_info=True)
|
||||
available_skills = []
|
||||
return [name for name in available_skills if name.strip().lower() not in blocked]
|
||||
_ES_QUERY_ROUTING_OPEN_TAG = '<skill_routing skill="es_query">'
|
||||
|
||||
|
||||
def _log_es_query_routing_registration(
|
||||
_SKILLS_BLOCKED_BY_EXCLUDED_TOOL: dict[str, frozenset[str]] = {
|
||||
# 内置 tool 与历史 custom skill 目录存在两种拼写;禁用编排工具时两者都必须从系统提示和
|
||||
# runtime skill allowlist 中消失,防止误配的席位仍看到「加载编排技能」。
|
||||
"agent_orchestration": frozenset({"agent_orchestration", "agent-orchestration"}),
|
||||
}
|
||||
|
||||
_ROUNDTABLE_COORDINATOR_AGENT_ID = "roundtable-coordinator"
|
||||
|
||||
_NON_COORDINATOR_ORCHESTRATION_BOUNDARY = """
|
||||
<orchestration_capability_boundary>
|
||||
你不是多智能体圆桌总控,因此没有编排、派活或调度其它智能体的权限。
|
||||
不得加载、读取、检索或调用 agent_orchestration / agent-orchestration,
|
||||
不得声称准备使用编排技能;请直接完成当前问答或你的本职任务。
|
||||
</orchestration_capability_boundary>
|
||||
""".strip()
|
||||
|
||||
|
||||
def _enforce_coordinator_only_tool_exclusions(
|
||||
agent_id: str | None,
|
||||
excluded_tools: list[str] | None,
|
||||
) -> list[str] | None:
|
||||
"""Make orchestration unavailable to every agent except the fixed roundtable coordinator."""
|
||||
if agent_id == _ROUNDTABLE_COORDINATOR_AGENT_ID:
|
||||
return excluded_tools
|
||||
effective = list(excluded_tools or [])
|
||||
if "agent_orchestration" not in effective:
|
||||
effective.append("agent_orchestration")
|
||||
return effective
|
||||
|
||||
|
||||
def _append_orchestration_capability_boundary(system_prompt: str, agent_id: str | None) -> str:
|
||||
"""Reinforce the physical tool/skill gate in every non-coordinator system prompt."""
|
||||
if agent_id == _ROUNDTABLE_COORDINATOR_AGENT_ID:
|
||||
return system_prompt
|
||||
return f"{system_prompt}\n\n{_NON_COORDINATOR_ORCHESTRATION_BOUNDARY}"
|
||||
|
||||
|
||||
def _filter_available_skills_for_excluded_tools(
|
||||
available_skills: list[str] | None,
|
||||
excluded_tools: list[str] | None,
|
||||
) -> list[str] | None:
|
||||
"""让工具级禁用同步收紧 skill allowlist,必要时把「全部」展开为安全列表。"""
|
||||
if not excluded_tools:
|
||||
return available_skills
|
||||
excluded = {name.strip().lower() for name in excluded_tools if name}
|
||||
blocked = {
|
||||
skill_name
|
||||
for tool_name, skill_names in _SKILLS_BLOCKED_BY_EXCLUDED_TOOL.items()
|
||||
if tool_name in excluded
|
||||
for skill_name in skill_names
|
||||
}
|
||||
if not blocked:
|
||||
return available_skills
|
||||
if available_skills is None:
|
||||
# ``None`` 原本表示「全部启用技能可见」,无法表达「除了总控专属技能之外的全部」。
|
||||
# 此时显式展开存储中的启用技能;扫描失败则 fail closed,避免角色隔离失效。
|
||||
try:
|
||||
from deerflow.skills.storage import get_or_new_skill_storage
|
||||
|
||||
available_skills = [
|
||||
skill.name for skill in get_or_new_skill_storage().load_skills(enabled_only=True)
|
||||
]
|
||||
except Exception: # noqa: BLE001
|
||||
logger.warning("Failed to expand skills while applying excluded-tool boundary", exc_info=True)
|
||||
available_skills = []
|
||||
return [name for name in available_skills if name.strip().lower() not in blocked]
|
||||
|
||||
|
||||
def _log_es_query_routing_registration(
|
||||
*,
|
||||
system_prompt: str,
|
||||
app_config: AppConfig,
|
||||
@@ -401,9 +401,9 @@ def _build_middlewares(
|
||||
if summarization_middleware is not None:
|
||||
middlewares.append(summarization_middleware)
|
||||
|
||||
# Add TodoList middleware if plan mode is enabled
|
||||
is_plan_mode = cfg.get("is_plan_mode", False)
|
||||
is_bootstrap = cfg.get("is_bootstrap", False)
|
||||
# Add TodoList middleware if plan mode is enabled
|
||||
is_plan_mode = cfg.get("is_plan_mode", False)
|
||||
is_bootstrap = cfg.get("is_bootstrap", False)
|
||||
is_writing_setup = cfg.get("is_writing_setup", False)
|
||||
is_report_structure_setup = cfg.get("is_report_structure_setup", False)
|
||||
todo_list_middleware = _create_todo_list_middleware(is_plan_mode)
|
||||
@@ -433,7 +433,9 @@ def _build_middlewares(
|
||||
|
||||
# Add MemoryMiddleware (after TitleMiddleware) — skip in bootstrap mode (agent creation flow) and ephemeral runs
|
||||
memory_injection_enabled = cfg.get("memory_injection_enabled", True)
|
||||
if not is_bootstrap and not is_writing_setup and not is_report_structure_setup and not is_ephemeral:
|
||||
memory_cfg = getattr(resolved_app_config, "memory", None)
|
||||
memory_globally_enabled = memory_cfg is not None and getattr(memory_cfg, "enabled", False)
|
||||
if memory_globally_enabled and not is_bootstrap and not is_writing_setup and not is_report_structure_setup and not is_ephemeral:
|
||||
middlewares.append(MemoryMiddleware(agent_name=agent_name, memory_config=resolved_app_config.memory, injection_enabled=memory_injection_enabled))
|
||||
|
||||
# KnowledgeRagMiddleware — inject knowledge-base recall (phase 2). Gated
|
||||
@@ -574,10 +576,10 @@ def _make_lead_agent(config: RunnableConfig, *, app_config: AppConfig):
|
||||
# actually injected. Default off → no behavior change for normal chats.
|
||||
thinking_force_disabled = bool(cfg.get("thinking_force_disabled", False))
|
||||
reasoning_effort = cfg.get("reasoning_effort", None)
|
||||
requested_model_name: str | None = cfg.get("model_name") or cfg.get("model")
|
||||
is_plan_mode = cfg.get("is_plan_mode", False)
|
||||
disable_tools = bool(cfg.get("disable_tools", False))
|
||||
subagent_enabled = cfg.get("subagent_enabled", False)
|
||||
requested_model_name: str | None = cfg.get("model_name") or cfg.get("model")
|
||||
is_plan_mode = cfg.get("is_plan_mode", False)
|
||||
disable_tools = bool(cfg.get("disable_tools", False))
|
||||
subagent_enabled = cfg.get("subagent_enabled", False)
|
||||
max_concurrent_subagents = cfg.get("max_concurrent_subagents", 3)
|
||||
is_bootstrap = cfg.get("is_bootstrap", False)
|
||||
# 写作配置 Q&A:与 is_bootstrap 同构的另一种「最小化 bootstrap」——挂 setup_writing 工具、
|
||||
@@ -603,11 +605,11 @@ def _make_lead_agent(config: RunnableConfig, *, app_config: AppConfig):
|
||||
|
||||
if not is_writing_mode_report_path(writing_artifact_path):
|
||||
writing_artifact_path = None
|
||||
agent_id = validate_agent_id(cfg.get("agent_id") or cfg.get("agent_name"))
|
||||
requested_excluded_tools = _enforce_coordinator_only_tool_exclusions(
|
||||
agent_id,
|
||||
cfg.get("excluded_tools") or None,
|
||||
)
|
||||
agent_id = validate_agent_id(cfg.get("agent_id") or cfg.get("agent_name"))
|
||||
requested_excluded_tools = _enforce_coordinator_only_tool_exclusions(
|
||||
agent_id,
|
||||
cfg.get("excluded_tools") or None,
|
||||
)
|
||||
|
||||
try:
|
||||
agent_config = load_agent_config(agent_id) if not is_bootstrap and not is_writing_setup and not is_report_structure_setup else None
|
||||
@@ -656,14 +658,14 @@ def _make_lead_agent(config: RunnableConfig, *, app_config: AppConfig):
|
||||
|
||||
from deerflow.agents.deep_research.retrieval import merge_collector_skill_allowlist, runtime_extra_skills
|
||||
|
||||
runtime_available_skills = merge_collector_skill_allowlist(
|
||||
agent_id, runtime_available_skills, runtime_extra_skills(cfg)
|
||||
)
|
||||
runtime_available_skills = _filter_available_skills_for_excluded_tools(
|
||||
runtime_available_skills,
|
||||
requested_excluded_tools,
|
||||
)
|
||||
prompt_available_skills = set(runtime_available_skills) if runtime_available_skills is not None else None
|
||||
runtime_available_skills = merge_collector_skill_allowlist(
|
||||
agent_id, runtime_available_skills, runtime_extra_skills(cfg)
|
||||
)
|
||||
runtime_available_skills = _filter_available_skills_for_excluded_tools(
|
||||
runtime_available_skills,
|
||||
requested_excluded_tools,
|
||||
)
|
||||
prompt_available_skills = set(runtime_available_skills) if runtime_available_skills is not None else None
|
||||
|
||||
config["metadata"].update(
|
||||
{
|
||||
@@ -744,14 +746,14 @@ def _make_lead_agent(config: RunnableConfig, *, app_config: AppConfig):
|
||||
# Special bootstrap agent with minimal prompt for initial custom agent creation flow
|
||||
return create_agent(
|
||||
model=create_chat_model(name=model_name, thinking_enabled=thinking_enabled, force_disable_thinking=thinking_force_disabled, app_config=resolved_app_config),
|
||||
tools=get_available_tools(
|
||||
model_name=model_name,
|
||||
subagent_enabled=subagent_enabled,
|
||||
is_scheduled_run=cfg.get("is_scheduled_run", False),
|
||||
excluded_tools=requested_excluded_tools,
|
||||
allow_agent_orchestration=agent_id == _ROUNDTABLE_COORDINATOR_AGENT_ID,
|
||||
app_config=resolved_app_config,
|
||||
)
|
||||
tools=get_available_tools(
|
||||
model_name=model_name,
|
||||
subagent_enabled=subagent_enabled,
|
||||
is_scheduled_run=cfg.get("is_scheduled_run", False),
|
||||
excluded_tools=requested_excluded_tools,
|
||||
allow_agent_orchestration=agent_id == _ROUNDTABLE_COORDINATOR_AGENT_ID,
|
||||
app_config=resolved_app_config,
|
||||
)
|
||||
+ [setup_agent],
|
||||
middleware=_build_middlewares(config, model_name=model_name, app_config=resolved_app_config),
|
||||
system_prompt=apply_prompt_template(
|
||||
@@ -771,19 +773,19 @@ def _make_lead_agent(config: RunnableConfig, *, app_config: AppConfig):
|
||||
# Default lead agent (unchanged behavior)
|
||||
is_notebook_mode = bool(cfg.get("notebook_search_space_id"))
|
||||
notebook_source_ids: list[int] = cfg.get("notebook_source_ids") or []
|
||||
default_tools = (
|
||||
[]
|
||||
if disable_tools
|
||||
else get_available_tools(
|
||||
model_name=model_name,
|
||||
groups=agent_config.tool_groups if agent_config else None,
|
||||
subagent_enabled=subagent_enabled,
|
||||
is_scheduled_run=cfg.get("is_scheduled_run", False),
|
||||
excluded_tools=requested_excluded_tools,
|
||||
allow_agent_orchestration=agent_id == _ROUNDTABLE_COORDINATOR_AGENT_ID,
|
||||
app_config=resolved_app_config,
|
||||
)
|
||||
)
|
||||
default_tools = (
|
||||
[]
|
||||
if disable_tools
|
||||
else get_available_tools(
|
||||
model_name=model_name,
|
||||
groups=agent_config.tool_groups if agent_config else None,
|
||||
subagent_enabled=subagent_enabled,
|
||||
is_scheduled_run=cfg.get("is_scheduled_run", False),
|
||||
excluded_tools=requested_excluded_tools,
|
||||
allow_agent_orchestration=agent_id == _ROUNDTABLE_COORDINATOR_AGENT_ID,
|
||||
app_config=resolved_app_config,
|
||||
)
|
||||
)
|
||||
if agent_id == FORCED_RESEARCH_AGENT_ID:
|
||||
default_tools = filter_forced_research_tools(default_tools)
|
||||
if is_notebook_mode:
|
||||
@@ -825,8 +827,8 @@ def _make_lead_agent(config: RunnableConfig, *, app_config: AppConfig):
|
||||
writing_mode=writing_mode,
|
||||
writing_artifact_path=writing_artifact_path,
|
||||
)
|
||||
else:
|
||||
system_prompt = apply_prompt_template(
|
||||
else:
|
||||
system_prompt = apply_prompt_template(
|
||||
subagent_enabled=subagent_enabled,
|
||||
max_concurrent_subagents=max_concurrent_subagents,
|
||||
agent_id=agent_id,
|
||||
@@ -839,22 +841,22 @@ def _make_lead_agent(config: RunnableConfig, *, app_config: AppConfig):
|
||||
rag_mode_enabled=rag_mode_enabled,
|
||||
is_notebook_mode=is_notebook_mode,
|
||||
notebook_source_ids=notebook_source_ids,
|
||||
is_scheduled_run=bool(cfg.get("is_scheduled_run", False)),
|
||||
)
|
||||
|
||||
# 前端可提供当前视觉明暗模式给页面生成类任务。把值放入系统提示而不是
|
||||
# 指望模型从 ToolRuntime.context 猜测,确保自定义 SOUL 也能稳定使用。
|
||||
presentation_theme = str(cfg.get("presentation_theme") or "").strip().lower()
|
||||
if presentation_theme in {"dark", "light"}:
|
||||
system_prompt += (
|
||||
"\n\n【当前界面主题】\n"
|
||||
f"当前页面呈现主题为 `{presentation_theme}`。若本轮生成汇报页面,请优先选择同色系 HTML 模板;"
|
||||
"除非用户明确要求覆盖该主题。"
|
||||
)
|
||||
|
||||
system_prompt = _append_orchestration_capability_boundary(system_prompt, agent_id)
|
||||
|
||||
_log_es_query_routing_registration(
|
||||
is_scheduled_run=bool(cfg.get("is_scheduled_run", False)),
|
||||
)
|
||||
|
||||
# 前端可提供当前视觉明暗模式给页面生成类任务。把值放入系统提示而不是
|
||||
# 指望模型从 ToolRuntime.context 猜测,确保自定义 SOUL 也能稳定使用。
|
||||
presentation_theme = str(cfg.get("presentation_theme") or "").strip().lower()
|
||||
if presentation_theme in {"dark", "light"}:
|
||||
system_prompt += (
|
||||
"\n\n【当前界面主题】\n"
|
||||
f"当前页面呈现主题为 `{presentation_theme}`。若本轮生成汇报页面,请优先选择同色系 HTML 模板;"
|
||||
"除非用户明确要求覆盖该主题。"
|
||||
)
|
||||
|
||||
system_prompt = _append_orchestration_capability_boundary(system_prompt, agent_id)
|
||||
|
||||
_log_es_query_routing_registration(
|
||||
system_prompt=system_prompt,
|
||||
app_config=resolved_app_config,
|
||||
agent_id=agent_id,
|
||||
|
||||
+29
-24
@@ -9,8 +9,8 @@ from functools import lru_cache
|
||||
from typing import TYPE_CHECKING
|
||||
from zoneinfo import ZoneInfo
|
||||
|
||||
from deerflow.config.agents_config import load_agent_soul
|
||||
from deerflow.config.system_settings import load_system_settings
|
||||
from deerflow.config.agents_config import load_agent_soul
|
||||
from deerflow.config.system_settings import load_system_settings
|
||||
from deerflow.skills.storage import get_or_new_skill_storage
|
||||
from deerflow.skills.types import Skill, SkillCategory
|
||||
from deerflow.subagents import get_available_subagent_names
|
||||
@@ -487,7 +487,7 @@ task(description="Oracle Cloud analysis", prompt="...", subagent_type="general-p
|
||||
</subagent_system>"""
|
||||
|
||||
|
||||
_ORDINARY_QA_MARKDOWN_FORMAT_SECTION = """
|
||||
_ORDINARY_QA_MARKDOWN_FORMAT_SECTION = """
|
||||
<ordinary_qa_markdown_format>
|
||||
当普通问答需要输出带章节结构的较长内容时,必须采用以下 Markdown 规范,以便下载为 Word 后得到正确的自动编号和公文标题格式:
|
||||
- 文档主标题使用 `# 主标题`;主标题只写标题文字,不添加章节编号。
|
||||
@@ -496,19 +496,19 @@ _ORDINARY_QA_MARKDOWN_FORMAT_SECTION = """
|
||||
- 默认禁止使用以 `-`、`*`、`+` 或 `•` 开头的无序圆点列表;并列内容优先改写为完整自然段,确需逐项表达时使用有序列表。只有用户明确要求无序列表时才使用。
|
||||
- 简短问答不必为了套用格式而强行添加标题;本规范只约束实际使用的标题和列表。
|
||||
</ordinary_qa_markdown_format>
|
||||
"""
|
||||
|
||||
|
||||
def _ordinary_qa_markdown_format_enabled() -> bool:
|
||||
"""Return the admin-controlled formal Markdown prompt switch."""
|
||||
try:
|
||||
return bool(load_system_settings().prompt_prefix.ordinary_qa_markdown_format_enabled)
|
||||
except Exception:
|
||||
logger.debug("Failed to read the ordinary Q&A Markdown format switch; using disabled", exc_info=True)
|
||||
return False
|
||||
|
||||
|
||||
SYSTEM_PROMPT_TEMPLATE = """
|
||||
"""
|
||||
|
||||
|
||||
def _ordinary_qa_markdown_format_enabled() -> bool:
|
||||
"""Return the admin-controlled formal Markdown prompt switch."""
|
||||
try:
|
||||
return bool(load_system_settings().prompt_prefix.ordinary_qa_markdown_format_enabled)
|
||||
except Exception:
|
||||
logger.debug("Failed to read the ordinary Q&A Markdown format switch; using disabled", exc_info=True)
|
||||
return False
|
||||
|
||||
|
||||
SYSTEM_PROMPT_TEMPLATE = """
|
||||
<role>
|
||||
You are {agent_name}, an internal super agent.
|
||||
</role>
|
||||
@@ -883,11 +883,16 @@ def _get_memory_context(agent_name: str | None = None, *, app_config: AppConfig
|
||||
return ""
|
||||
try:
|
||||
from deerflow.agents.memory.manager import build_memory_manager
|
||||
from deerflow.config.memory_config import get_effective_memory_config
|
||||
from deerflow.config.memory_config import get_effective_memory_config, get_memory_config
|
||||
from deerflow.runtime.user_context import get_effective_user_id
|
||||
|
||||
if not get_memory_config().enabled:
|
||||
return ""
|
||||
|
||||
user_id = get_effective_user_id()
|
||||
config = get_effective_memory_config(user_id)
|
||||
if not config.enabled or not config.injection.enabled:
|
||||
return ""
|
||||
|
||||
manager = build_memory_manager(
|
||||
user_id=user_id,
|
||||
@@ -1547,13 +1552,13 @@ When a search, knowledge, skill, or MCP tool returns a result list:
|
||||
if rag_mode_enabled
|
||||
else ""
|
||||
)
|
||||
ordinary_qa_format_section = (
|
||||
_ORDINARY_QA_MARKDOWN_FORMAT_SECTION
|
||||
if _ordinary_qa_markdown_format_enabled()
|
||||
and not is_bootstrap
|
||||
and not writing_mode
|
||||
and not is_notebook_mode
|
||||
and not is_scheduled_run
|
||||
ordinary_qa_format_section = (
|
||||
_ORDINARY_QA_MARKDOWN_FORMAT_SECTION
|
||||
if _ordinary_qa_markdown_format_enabled()
|
||||
and not is_bootstrap
|
||||
and not writing_mode
|
||||
and not is_notebook_mode
|
||||
and not is_scheduled_run
|
||||
else ""
|
||||
)
|
||||
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
============
|
||||
``get_effective_memory_config(user_id)`` 按优先级合并 (高 -> 低):
|
||||
1. per-user 覆盖 数据库 ``user_memory_configs`` 表 (经进程内 cache 读取)
|
||||
(有 per-user 记录时自动强制 enabled=True)
|
||||
(仅白名单字段,不能覆盖 ``enabled``)
|
||||
2. config.yaml ``memory:`` 段 (即 ``get_memory_config()``)
|
||||
3. Pydantic 默认值
|
||||
|
||||
@@ -254,13 +254,12 @@ def get_effective_memory_config(user_id: str) -> MemoryConfig:
|
||||
"""返回某用户的生效记忆配置 (同步,从进程内 cache 读取 per-user 覆盖)。
|
||||
|
||||
优先级 (高 -> 低):per-user DB 覆盖 (经 cache) > config.yaml > 默认值。
|
||||
有 per-user 记录时自动强制 enabled=True。
|
||||
``enabled`` 始终以 config.yaml 为准,用户覆盖不能把它重新打开。
|
||||
"""
|
||||
base = _memory_config.model_dump()
|
||||
recall_budget = _recall_budget_cache.get(user_id)
|
||||
if recall_budget is not None:
|
||||
base.setdefault("hindsight", {})["recall_budget"] = recall_budget
|
||||
base["enabled"] = True
|
||||
try:
|
||||
return MemoryConfig(**base)
|
||||
except Exception as exc: # noqa: BLE001 - 覆盖产生非法配置时降级
|
||||
|
||||
@@ -31,6 +31,96 @@ def _vllm_disable_chat_template_kwargs(chat_template_kwargs: dict) -> dict:
|
||||
return disable_kwargs
|
||||
|
||||
|
||||
# Qwen3.8 chat templates only accept these three effort names.
|
||||
_QWEN_REASONING_EFFORT = {
|
||||
"minimal": "low",
|
||||
"low": "low",
|
||||
"medium": "medium",
|
||||
"high": "xhigh",
|
||||
"xhigh": "xhigh",
|
||||
}
|
||||
|
||||
_OPENAI_THINKING_OFF_EXTRA_BODY = {
|
||||
"enable_thinking": False,
|
||||
"chat_template_kwargs": {"enable_thinking": False},
|
||||
"thinking": {"type": "disabled"},
|
||||
}
|
||||
|
||||
|
||||
def _is_vllm_provider(model_use: str) -> bool:
|
||||
lowered = (model_use or "").lower()
|
||||
return "vllm_provider" in lowered or lowered.endswith(":vllmchatmodel")
|
||||
|
||||
|
||||
def _is_qwen38_model(model_config) -> bool:
|
||||
blob = f"{getattr(model_config, 'name', '')} {getattr(model_config, 'model', '')}".lower()
|
||||
return "qwen3.8" in blob
|
||||
|
||||
|
||||
def _should_control_openai_thinking(model_config) -> bool:
|
||||
"""Whether this OpenAI-compatible model needs explicit thinking on/off flags.
|
||||
|
||||
Qwen3.8 (and current vLLM reasoning parsers) default to thinking at ``xhigh``.
|
||||
If DeerFlow does not send ``enable_thinking: false``, the first visible answer
|
||||
token waits until that hidden reasoning finishes — third-party chat UIs that
|
||||
disable thinking or stream the reasoning field therefore feel much faster.
|
||||
"""
|
||||
if getattr(model_config, "supports_thinking", False):
|
||||
return True
|
||||
if _is_vllm_provider(getattr(model_config, "use", "")):
|
||||
return True
|
||||
return _is_qwen38_model(model_config)
|
||||
|
||||
|
||||
def _map_qwen_reasoning_effort(effort: object) -> str | None:
|
||||
if effort is None:
|
||||
return None
|
||||
return _QWEN_REASONING_EFFORT.get(str(effort).strip().lower())
|
||||
|
||||
|
||||
def _apply_openai_thinking_control(
|
||||
model_config,
|
||||
model_class,
|
||||
model_settings_from_config: dict,
|
||||
*,
|
||||
thinking_enabled: bool,
|
||||
reasoning_effort: object,
|
||||
) -> None:
|
||||
"""Inject vLLM/Qwen thinking flags when the UI toggle is on or off."""
|
||||
if "extra_body" not in getattr(model_class, "model_fields", {}):
|
||||
return
|
||||
qwen_vllm = _is_vllm_provider(getattr(model_config, "use", "")) or _is_qwen38_model(model_config)
|
||||
if thinking_enabled:
|
||||
if not qwen_vllm:
|
||||
return
|
||||
enable_body: dict = {
|
||||
"enable_thinking": True,
|
||||
"chat_template_kwargs": {"enable_thinking": True},
|
||||
}
|
||||
mapped_effort = _map_qwen_reasoning_effort(reasoning_effort)
|
||||
if mapped_effort:
|
||||
enable_body["chat_template_kwargs"]["reasoning_effort"] = mapped_effort
|
||||
model_settings_from_config["extra_body"] = _deep_merge_dicts(
|
||||
model_settings_from_config.get("extra_body"),
|
||||
enable_body,
|
||||
)
|
||||
return
|
||||
if not _should_control_openai_thinking(model_config):
|
||||
return
|
||||
disable_body = (
|
||||
{
|
||||
"enable_thinking": False,
|
||||
"chat_template_kwargs": {"enable_thinking": False},
|
||||
}
|
||||
if qwen_vllm
|
||||
else _OPENAI_THINKING_OFF_EXTRA_BODY
|
||||
)
|
||||
model_settings_from_config["extra_body"] = _deep_merge_dicts(
|
||||
model_settings_from_config.get("extra_body"),
|
||||
disable_body,
|
||||
)
|
||||
|
||||
|
||||
def _enable_stream_usage_by_default(model_use_path: str, model_settings_from_config: dict) -> None:
|
||||
"""Enable stream usage for OpenAI-compatible models unless explicitly configured.
|
||||
|
||||
@@ -127,10 +217,19 @@ def create_chat_model(name: str | None = None, thinking_enabled: bool = False, *
|
||||
elif has_thinking_settings and effective_wte.get("thinking", {}).get("type"):
|
||||
# Native langchain_anthropic: thinking is a direct constructor parameter
|
||||
model_settings_from_config["thinking"] = {"type": "disabled"}
|
||||
requested_reasoning_effort = kwargs.get("reasoning_effort")
|
||||
if not model_config.supports_reasoning_effort:
|
||||
kwargs.pop("reasoning_effort", None)
|
||||
model_settings_from_config.pop("reasoning_effort", None)
|
||||
|
||||
_apply_openai_thinking_control(
|
||||
model_config,
|
||||
model_class,
|
||||
model_settings_from_config,
|
||||
thinking_enabled=thinking_enabled,
|
||||
reasoning_effort=requested_reasoning_effort,
|
||||
)
|
||||
|
||||
# Hard override: force-disable thinking regardless of the per-model config shape.
|
||||
# Injected only for OpenAI-compatible classes (those declaring `extra_body`);
|
||||
# other providers would reject the kwarg, so they are skipped (they ignore it).
|
||||
@@ -139,15 +238,15 @@ def create_chat_model(name: str | None = None, thinking_enabled: bool = False, *
|
||||
# (DeepSeek-V3.1+/Qwen3/GLM hybrid models on api.siliconflow.cn — the knob the
|
||||
# nested ``chat_template_kwargs`` shape does NOT reach), vLLM/Qwen native
|
||||
# ``chat_template_kwargs.enable_thinking``, and Zhipu GLM ``thinking.type``.
|
||||
# Only models that explicitly opt into thinking should receive vendor-specific
|
||||
# disable payloads. Generic OpenAI-compatible gateways (including LiteLLM)
|
||||
# may reject these otherwise-unknown fields instead of forwarding them.
|
||||
if (
|
||||
force_disable_thinking
|
||||
and not thinking_enabled
|
||||
and model_config.supports_thinking
|
||||
and "extra_body" in getattr(model_class, "model_fields", {})
|
||||
):
|
||||
# Only models that explicitly opt into thinking should receive vendor-specific
|
||||
# disable payloads. Generic OpenAI-compatible gateways (including LiteLLM)
|
||||
# may reject these otherwise-unknown fields instead of forwarding them.
|
||||
if (
|
||||
force_disable_thinking
|
||||
and not thinking_enabled
|
||||
and model_config.supports_thinking
|
||||
and "extra_body" in getattr(model_class, "model_fields", {})
|
||||
):
|
||||
model_settings_from_config["extra_body"] = _deep_merge_dicts(
|
||||
model_settings_from_config.get("extra_body"),
|
||||
{"enable_thinking": False, "chat_template_kwargs": {"enable_thinking": False}, "thinking": {"type": "disabled"}},
|
||||
|
||||
+27
-17
@@ -29,11 +29,11 @@ from typing import Any
|
||||
|
||||
from langchain_core.language_models import LanguageModelInput
|
||||
from langchain_core.messages import AIMessage, AIMessageChunk
|
||||
from langchain_core.outputs import ChatGeneration, ChatGenerationChunk, ChatResult
|
||||
from langchain_openai import ChatOpenAI
|
||||
|
||||
from deerflow.models.message_ordering import normalize_chat_payload_messages
|
||||
|
||||
from langchain_core.outputs import ChatGeneration, ChatGenerationChunk, ChatResult
|
||||
from langchain_openai import ChatOpenAI
|
||||
|
||||
from deerflow.models.message_ordering import normalize_chat_payload_messages
|
||||
|
||||
|
||||
class PatchedChatOpenAI(ChatOpenAI):
|
||||
"""ChatOpenAI with ``thought_signature`` preservation for Gemini thinking via OpenAI gateway.
|
||||
@@ -89,16 +89,16 @@ class PatchedChatOpenAI(ChatOpenAI):
|
||||
if payload_msg.get("role") == "assistant" and isinstance(orig_msg, AIMessage):
|
||||
_restore_tool_call_signatures(payload_msg, orig_msg)
|
||||
_restore_reasoning_content(payload_msg, orig_msg)
|
||||
else:
|
||||
# Fallback: match assistant-role entries positionally against AIMessages.
|
||||
ai_messages = [m for m in original_messages if isinstance(m, AIMessage)]
|
||||
assistant_payloads = [(i, m) for i, m in enumerate(payload_messages) if m.get("role") == "assistant"]
|
||||
for (_, payload_msg), ai_msg in zip(assistant_payloads, ai_messages):
|
||||
_restore_tool_call_signatures(payload_msg, ai_msg)
|
||||
_restore_reasoning_content(payload_msg, ai_msg)
|
||||
|
||||
normalize_chat_payload_messages(payload)
|
||||
return payload
|
||||
else:
|
||||
# Fallback: match assistant-role entries positionally against AIMessages.
|
||||
ai_messages = [m for m in original_messages if isinstance(m, AIMessage)]
|
||||
assistant_payloads = [(i, m) for i, m in enumerate(payload_messages) if m.get("role") == "assistant"]
|
||||
for (_, payload_msg), ai_msg in zip(assistant_payloads, ai_messages):
|
||||
_restore_tool_call_signatures(payload_msg, ai_msg)
|
||||
_restore_reasoning_content(payload_msg, ai_msg)
|
||||
|
||||
normalize_chat_payload_messages(payload)
|
||||
return payload
|
||||
|
||||
def _convert_chunk_to_generation_chunk(
|
||||
self,
|
||||
@@ -208,11 +208,21 @@ def _restore_reasoning_content(payload_msg: dict, orig_msg: AIMessage) -> None:
|
||||
|
||||
|
||||
def _extract_reasoning_content(payload: dict | None) -> str | None:
|
||||
"""Extract reasoning text from provider payload delta/message objects."""
|
||||
"""Extract reasoning text from provider payload delta/message objects.
|
||||
|
||||
vLLM Qwen reasoning parsers use ``reasoning_content`` on streaming deltas
|
||||
and ``reasoning`` on some non-stream / older builds. Read both so the UI
|
||||
can show the first thinking token instead of waiting for answer content.
|
||||
"""
|
||||
if not isinstance(payload, dict):
|
||||
return None
|
||||
reasoning_content = payload.get("reasoning_content")
|
||||
return reasoning_content if isinstance(reasoning_content, str) and reasoning_content else None
|
||||
if isinstance(reasoning_content, str) and reasoning_content:
|
||||
return reasoning_content
|
||||
reasoning = payload.get("reasoning")
|
||||
if isinstance(reasoning, str) and reasoning:
|
||||
return reasoning
|
||||
return None
|
||||
|
||||
|
||||
def _append_reasoning_content(message: AIMessage | AIMessageChunk, reasoning_content: str) -> AIMessage | AIMessageChunk:
|
||||
|
||||
+15
-13
@@ -32,11 +32,11 @@ from langchain_core.messages import (
|
||||
)
|
||||
from langchain_core.messages.tool import tool_call_chunk
|
||||
from langchain_core.outputs import ChatGeneration, ChatGenerationChunk, ChatResult
|
||||
from langchain_openai import ChatOpenAI
|
||||
from langchain_openai.chat_models.base import _create_usage_metadata
|
||||
|
||||
from deerflow.models.message_ordering import normalize_chat_payload_messages
|
||||
|
||||
from langchain_openai import ChatOpenAI
|
||||
from langchain_openai.chat_models.base import _create_usage_metadata
|
||||
|
||||
from deerflow.models.message_ordering import normalize_chat_payload_messages
|
||||
|
||||
|
||||
def _normalize_vllm_chat_template_kwargs(payload: dict[str, Any]) -> None:
|
||||
"""Map DeerFlow's legacy ``thinking`` toggle to vLLM/Qwen's ``enable_thinking``.
|
||||
@@ -107,6 +107,8 @@ def _convert_delta_to_message_chunk_with_reasoning(_dict: Mapping[str, Any], def
|
||||
additional_kwargs["function_call"] = function_call
|
||||
|
||||
reasoning = _dict.get("reasoning")
|
||||
if reasoning is None:
|
||||
reasoning = _dict.get("reasoning_content")
|
||||
if reasoning is not None:
|
||||
additional_kwargs["reasoning"] = reasoning
|
||||
reasoning_text = _reasoning_to_text(reasoning)
|
||||
@@ -184,14 +186,14 @@ class VllmChatModel(ChatOpenAI):
|
||||
for payload_msg, orig_msg in zip(payload_messages, original_messages):
|
||||
if payload_msg.get("role") == "assistant" and isinstance(orig_msg, AIMessage):
|
||||
_restore_reasoning_field(payload_msg, orig_msg)
|
||||
else:
|
||||
ai_messages = [message for message in original_messages if isinstance(message, AIMessage)]
|
||||
assistant_payloads = [message for message in payload_messages if message.get("role") == "assistant"]
|
||||
for payload_msg, ai_msg in zip(assistant_payloads, ai_messages):
|
||||
_restore_reasoning_field(payload_msg, ai_msg)
|
||||
|
||||
normalize_chat_payload_messages(payload)
|
||||
return payload
|
||||
else:
|
||||
ai_messages = [message for message in original_messages if isinstance(message, AIMessage)]
|
||||
assistant_payloads = [message for message in payload_messages if message.get("role") == "assistant"]
|
||||
for payload_msg, ai_msg in zip(assistant_payloads, ai_messages):
|
||||
_restore_reasoning_field(payload_msg, ai_msg)
|
||||
|
||||
normalize_chat_payload_messages(payload)
|
||||
return payload
|
||||
|
||||
def _create_chat_result(self, response: dict | openai.BaseModel, generation_info: dict | None = None) -> ChatResult:
|
||||
"""Preserve vLLM reasoning on non-streaming responses."""
|
||||
|
||||
Reference in new issue
Block a user