sync update from LAN repo (source dhk-1 @ c21a2a4a)
This commit is contained in:
1 parent
264e2200d6
commit
a1fa13a238
122 files changed
+8881
-623
No files matched your search
+71
-71
@@ -264,17 +264,17 @@ def _get_enabled_skills():
|
||||
def _get_enabled_skills_for_config(app_config: AppConfig | None = None) -> list[Skill]:
|
||||
"""Return enabled skills using the caller's config source.
|
||||
|
||||
Resolve storage from the caller's config and reuse the parsed catalogue only
|
||||
when its root matches. A cold cache or a different root falls back to that
|
||||
storage, keeping request-scoped skill directories isolated.
|
||||
Resolve storage from the caller's config and reuse the parsed catalogue only
|
||||
when its root matches. A cold cache or a different root falls back to that
|
||||
storage, keeping request-scoped skill directories isolated.
|
||||
"""
|
||||
if app_config is None:
|
||||
return _get_enabled_skills()
|
||||
storage = get_or_new_skill_storage(app_config=app_config)
|
||||
warm = get_warm_enabled_skills(storage)
|
||||
if warm is not None:
|
||||
return warm
|
||||
return _apply_always_on(_drop_archived_skills(list(storage.load_skills(enabled_only=True))))
|
||||
storage = get_or_new_skill_storage(app_config=app_config)
|
||||
warm = get_warm_enabled_skills(storage)
|
||||
if warm is not None:
|
||||
return warm
|
||||
return _apply_always_on(_drop_archived_skills(list(storage.load_skills(enabled_only=True))))
|
||||
|
||||
|
||||
def _skill_mutability_label(category: SkillCategory | str) -> str:
|
||||
@@ -617,44 +617,44 @@ task(description="Oracle Cloud analysis", prompt="...", subagent_type="general-p
|
||||
</subagent_system>"""
|
||||
|
||||
|
||||
_ORDINARY_QA_HEADING_RULES = {
|
||||
"hybrid_decimal": """- 一级章节使用 `## 一、标题`,二级章节使用 `### 1.1 标题`,三级章节使用 `#### 1.1.1 标题`。
|
||||
- 二级章节按 `1.1`、`1.2`、`2.1` 的形式编号,三级及更深层级继续使用完整多级阿拉伯数字。
|
||||
- 确需更深层级时,四级章节使用 `##### 1.1.1.1 标题`,五级章节使用 `###### 1.1.1.1.1 标题`。""",
|
||||
"official_chinese": """- 一级章节使用 `## 一、标题`,二级章节使用 `### (一) 标题`,三级章节使用 `#### 1. 标题`。
|
||||
- 确需更深层级时,四级章节使用 `##### (1) 标题`,五级章节使用 `###### 1) 标题`。""",
|
||||
"decimal": """- 一级章节使用 `## 1 标题`,二级章节使用 `### 1.1 标题`,三级章节使用 `#### 1.1.1 标题`。
|
||||
- 确需更深层级时,继续使用 `##### 1.1.1.1 标题`、`###### 1.1.1.1.1 标题`。""",
|
||||
"chapter_section": """- 一级章节使用 `## 第一章 标题`,二级章节使用 `### 第一节 标题`,三级章节使用 `#### 一、标题`。
|
||||
- 确需更深层级时,四级章节使用 `##### (一) 标题`,五级章节使用 `###### 1. 标题`。""",
|
||||
"none": """- 各级章节只写标题文字,不手写编号:`## 一级标题`、`### 二级标题`、`#### 三级标题`。
|
||||
- 确需更深层级时使用 `##### 四级标题`、`###### 五级标题`,仍不添加编号。""",
|
||||
}
|
||||
|
||||
|
||||
def _word_heading_numbering_style() -> str:
|
||||
"""Return the configured Word heading-numbering preset with a safe default."""
|
||||
try:
|
||||
return load_system_settings().prompt_prefix.word_heading_numbering_style
|
||||
except Exception:
|
||||
logger.debug("Failed to read the Word heading-numbering style; using default", exc_info=True)
|
||||
return "hybrid_decimal"
|
||||
|
||||
|
||||
def _build_ordinary_qa_markdown_format_section() -> str:
|
||||
rules = _ORDINARY_QA_HEADING_RULES.get(
|
||||
_word_heading_numbering_style(),
|
||||
_ORDINARY_QA_HEADING_RULES["hybrid_decimal"],
|
||||
)
|
||||
return f"""
|
||||
<ordinary_qa_markdown_format>
|
||||
当普通问答需要输出带章节结构的较长内容时,必须采用以下 Markdown 规范,以便下载为 Word 后得到正确的自动编号和公文标题格式:
|
||||
- 文档主标题使用 `# 主标题`;主标题只写标题文字,不添加章节编号。
|
||||
{rules}
|
||||
- 默认禁止使用以 `-`、`*`、`+` 或 `•` 开头的无序圆点列表;并列内容优先改写为完整自然段,确需逐项表达时使用有序列表。只有用户明确要求无序列表时才使用。
|
||||
- 简短问答不必为了套用格式而强行添加标题;本规范只约束实际使用的标题和列表。
|
||||
</ordinary_qa_markdown_format>
|
||||
"""
|
||||
_ORDINARY_QA_HEADING_RULES = {
|
||||
"hybrid_decimal": """- 一级章节使用 `## 一、标题`,二级章节使用 `### 1.1 标题`,三级章节使用 `#### 1.1.1 标题`。
|
||||
- 二级章节按 `1.1`、`1.2`、`2.1` 的形式编号,三级及更深层级继续使用完整多级阿拉伯数字。
|
||||
- 确需更深层级时,四级章节使用 `##### 1.1.1.1 标题`,五级章节使用 `###### 1.1.1.1.1 标题`。""",
|
||||
"official_chinese": """- 一级章节使用 `## 一、标题`,二级章节使用 `### (一) 标题`,三级章节使用 `#### 1. 标题`。
|
||||
- 确需更深层级时,四级章节使用 `##### (1) 标题`,五级章节使用 `###### 1) 标题`。""",
|
||||
"decimal": """- 一级章节使用 `## 1 标题`,二级章节使用 `### 1.1 标题`,三级章节使用 `#### 1.1.1 标题`。
|
||||
- 确需更深层级时,继续使用 `##### 1.1.1.1 标题`、`###### 1.1.1.1.1 标题`。""",
|
||||
"chapter_section": """- 一级章节使用 `## 第一章 标题`,二级章节使用 `### 第一节 标题`,三级章节使用 `#### 一、标题`。
|
||||
- 确需更深层级时,四级章节使用 `##### (一) 标题`,五级章节使用 `###### 1. 标题`。""",
|
||||
"none": """- 各级章节只写标题文字,不手写编号:`## 一级标题`、`### 二级标题`、`#### 三级标题`。
|
||||
- 确需更深层级时使用 `##### 四级标题`、`###### 五级标题`,仍不添加编号。""",
|
||||
}
|
||||
|
||||
|
||||
def _word_heading_numbering_style() -> str:
|
||||
"""Return the configured Word heading-numbering preset with a safe default."""
|
||||
try:
|
||||
return load_system_settings().prompt_prefix.word_heading_numbering_style
|
||||
except Exception:
|
||||
logger.debug("Failed to read the Word heading-numbering style; using default", exc_info=True)
|
||||
return "hybrid_decimal"
|
||||
|
||||
|
||||
def _build_ordinary_qa_markdown_format_section() -> str:
|
||||
rules = _ORDINARY_QA_HEADING_RULES.get(
|
||||
_word_heading_numbering_style(),
|
||||
_ORDINARY_QA_HEADING_RULES["hybrid_decimal"],
|
||||
)
|
||||
return f"""
|
||||
<ordinary_qa_markdown_format>
|
||||
当普通问答需要输出带章节结构的较长内容时,必须采用以下 Markdown 规范,以便下载为 Word 后得到正确的自动编号和公文标题格式:
|
||||
- 文档主标题使用 `# 主标题`;主标题只写标题文字,不添加章节编号。
|
||||
{rules}
|
||||
- 默认禁止使用以 `-`、`*`、`+` 或 `•` 开头的无序圆点列表;并列内容优先改写为完整自然段,确需逐项表达时使用有序列表。只有用户明确要求无序列表时才使用。
|
||||
- 简短问答不必为了套用格式而强行添加标题;本规范只约束实际使用的标题和列表。
|
||||
</ordinary_qa_markdown_format>
|
||||
"""
|
||||
|
||||
|
||||
_ORDINARY_QA_VISUALIZATION_SECTION = """
|
||||
@@ -1261,13 +1261,13 @@ RAG/reference display mode is enabled.
|
||||
return prompt
|
||||
|
||||
|
||||
def _get_memory_context(
|
||||
agent_name: str | None = None,
|
||||
*,
|
||||
app_config: AppConfig | None = None,
|
||||
injection_enabled: bool = True,
|
||||
include_legacy_knowledge_routing: bool = True,
|
||||
) -> str:
|
||||
def _get_memory_context(
|
||||
agent_name: str | None = None,
|
||||
*,
|
||||
app_config: AppConfig | None = None,
|
||||
injection_enabled: bool = True,
|
||||
include_legacy_knowledge_routing: bool = True,
|
||||
) -> str:
|
||||
"""组装注入系统提示词的记忆上下文 (Memory V2)。
|
||||
|
||||
通过 :class:`MemoryManager` 收集各 provider 的记忆块。P1 期只有 builtin
|
||||
@@ -1278,10 +1278,10 @@ def _get_memory_context(
|
||||
agent_name: agent 标识;``None`` 时按 "default" 处理。
|
||||
app_config: 显式应用配置 (本函数实际按 per-user 覆盖解析生效配置)。
|
||||
injection_enabled: 运行时开关;``False`` 时跳过注入直接返回空串。
|
||||
include_legacy_knowledge_routing: 是否注入 ``/mnt/knowledge/routing.md``。
|
||||
智能普通知识库检索已有数据库授权范围和专用检索工具时必须关闭,
|
||||
避免旧文件路由把“这个库”错误引向沙箱目录;其它记忆 provider
|
||||
仍按原配置保留。
|
||||
include_legacy_knowledge_routing: 是否注入 ``/mnt/knowledge/routing.md``。
|
||||
智能普通知识库检索已有数据库授权范围和专用检索工具时必须关闭,
|
||||
避免旧文件路由把“这个库”错误引向沙箱目录;其它记忆 provider
|
||||
仍按原配置保留。
|
||||
|
||||
Returns:
|
||||
包在 ``<memory>`` 标签里的记忆上下文;未启用或为空时返回空串。
|
||||
@@ -1306,11 +1306,11 @@ def _get_memory_context(
|
||||
)
|
||||
# 只取静态块 (builtin 的 frozen snapshot + Hindsight 的模式说明)。
|
||||
# Hindsight 的动态召回需要当前 query,走 MemoryMiddleware.before_model。
|
||||
context_content = manager.build_system_prompt(
|
||||
excluded_provider_names=(
|
||||
None if include_legacy_knowledge_routing else {"knowledge"}
|
||||
)
|
||||
)
|
||||
context_content = manager.build_system_prompt(
|
||||
excluded_provider_names=(
|
||||
None if include_legacy_knowledge_routing else {"knowledge"}
|
||||
)
|
||||
)
|
||||
|
||||
if not context_content.strip():
|
||||
return ""
|
||||
@@ -1878,7 +1878,7 @@ def apply_prompt_template(
|
||||
is_bootstrap: bool = False,
|
||||
app_config: AppConfig | None = None,
|
||||
memory_injection_enabled: bool = True,
|
||||
include_legacy_knowledge_routing: bool = True,
|
||||
include_legacy_knowledge_routing: bool = True,
|
||||
rag_mode_enabled: bool = False,
|
||||
is_notebook_mode: bool = False,
|
||||
notebook_source_ids: list[int] | None = None,
|
||||
@@ -1887,12 +1887,12 @@ def apply_prompt_template(
|
||||
storage_agent_id = agent_id if agent_id is not None else agent_name
|
||||
|
||||
# Get memory context
|
||||
memory_context = _get_memory_context(
|
||||
storage_agent_id,
|
||||
app_config=app_config,
|
||||
injection_enabled=memory_injection_enabled,
|
||||
include_legacy_knowledge_routing=include_legacy_knowledge_routing,
|
||||
)
|
||||
memory_context = _get_memory_context(
|
||||
storage_agent_id,
|
||||
app_config=app_config,
|
||||
injection_enabled=memory_injection_enabled,
|
||||
include_legacy_knowledge_routing=include_legacy_knowledge_routing,
|
||||
)
|
||||
|
||||
# Include subagent section only if enabled (from runtime parameter)
|
||||
n = max_concurrent_subagents
|
||||
@@ -2004,8 +2004,8 @@ When a search, knowledge, skill, or MCP tool returns a result list:
|
||||
if rag_mode_enabled
|
||||
else ""
|
||||
)
|
||||
ordinary_qa_format_section = (
|
||||
_build_ordinary_qa_markdown_format_section()
|
||||
ordinary_qa_format_section = (
|
||||
_build_ordinary_qa_markdown_format_section()
|
||||
if _ordinary_qa_markdown_format_enabled()
|
||||
and not is_bootstrap
|
||||
and not writing_mode
|
||||
|
||||
+29
-24
@@ -136,13 +136,18 @@ def _build_runtime_middlewares(
|
||||
# wrap_tool_call chain: its handler is the raw tool, so it observes the
|
||||
# unconverted exception (before ToolErrorHandlingMiddleware turns it into
|
||||
# an error ToolMessage) and the raw return value.
|
||||
# Omit the recorder entirely while paused: no record allocation, skill-merge
|
||||
# lookup, or fire-and-forget DB task is created by any tool invocation.
|
||||
# Keep tool execution/error handling and LLM metrics independent of this flag.
|
||||
if app_config.runtime_performance.tool_metrics_enabled:
|
||||
from deerflow.agents.middlewares.tool_metrics_middleware import ToolMetricsMiddleware
|
||||
|
||||
middlewares.append(ToolMetricsMiddleware())
|
||||
# Full auditing can stay paused under high concurrency while the low-volume
|
||||
# skill-only recorder remains active. In skill-only mode non-skill calls are
|
||||
# passed through without a merge query or fire-and-forget DB write.
|
||||
performance = app_config.runtime_performance
|
||||
if performance.tool_metrics_enabled or performance.skill_metrics_enabled:
|
||||
from deerflow.agents.middlewares.tool_metrics_middleware import ToolMetricsMiddleware
|
||||
|
||||
middlewares.append(
|
||||
ToolMetricsMiddleware(
|
||||
skill_only=not performance.tool_metrics_enabled,
|
||||
)
|
||||
)
|
||||
return middlewares
|
||||
|
||||
|
||||
@@ -179,20 +184,20 @@ def build_subagent_runtime_middlewares(
|
||||
model_name = app_config.models[0].name
|
||||
|
||||
model_config = app_config.get_model_config(model_name) if model_name else None
|
||||
if model_config is not None and model_config.supports_vision:
|
||||
from deerflow.agents.middlewares.view_image_middleware import ViewImageMiddleware
|
||||
|
||||
middlewares.append(ViewImageMiddleware())
|
||||
|
||||
# Must remain the innermost model-call wrapper: subagent-specific context,
|
||||
# vision payloads, system prompts and tool schemas all need to be included
|
||||
# in the final input-size decision.
|
||||
from deerflow.agents.middlewares.context_window_guard_middleware import ContextWindowGuardMiddleware
|
||||
|
||||
middlewares.append(
|
||||
ContextWindowGuardMiddleware(
|
||||
max_input_tokens=model_config.max_input_tokens if model_config is not None else None,
|
||||
)
|
||||
)
|
||||
|
||||
return middlewares
|
||||
if model_config is not None and model_config.supports_vision:
|
||||
from deerflow.agents.middlewares.view_image_middleware import ViewImageMiddleware
|
||||
|
||||
middlewares.append(ViewImageMiddleware())
|
||||
|
||||
# Must remain the innermost model-call wrapper: subagent-specific context,
|
||||
# vision payloads, system prompts and tool schemas all need to be included
|
||||
# in the final input-size decision.
|
||||
from deerflow.agents.middlewares.context_window_guard_middleware import ContextWindowGuardMiddleware
|
||||
|
||||
middlewares.append(
|
||||
ContextWindowGuardMiddleware(
|
||||
max_input_tokens=model_config.max_input_tokens if model_config is not None else None,
|
||||
)
|
||||
)
|
||||
|
||||
return middlewares
|
||||
+28
-5
@@ -1,6 +1,7 @@
|
||||
"""Middleware that records per-tool-call metrics for the admin audit page.
|
||||
|
||||
Every tool / skill invocation ends with one row in ``tool_call_metrics``:
|
||||
While the administrator-controlled call-metrics switch is enabled, every tool
|
||||
/ skill invocation ends with one row in ``tool_call_metrics``:
|
||||
- Successes record the wall-clock duration.
|
||||
- Failures record a coarse ``error_category`` plus a one-line message.
|
||||
|
||||
@@ -40,6 +41,7 @@ from langgraph.errors import GraphBubbleUp
|
||||
from langgraph.prebuilt.tool_node import ToolCallRequest
|
||||
from langgraph.types import Command
|
||||
|
||||
from deerflow.config.system_settings import get_call_metrics_settings
|
||||
from deerflow.persistence.engine import fire_and_forget_db_write, get_session_factory
|
||||
from deerflow.persistence.tool_metrics import ToolMetricRecord, make_tool_metrics_store
|
||||
from deerflow.persistence.tool_metrics.base import ToolMetricsStore
|
||||
@@ -188,11 +190,18 @@ def _evaluate_result(result: Any, tool_name: str | None) -> tuple[str, str | Non
|
||||
|
||||
|
||||
class ToolMetricsMiddleware(AgentMiddleware[AgentState]):
|
||||
"""Wrap each tool call to time it and persist a metric row."""
|
||||
"""Time tool calls, optionally persisting only attributable skill calls."""
|
||||
|
||||
def __init__(self, *, store: ToolMetricsStore | None = None, **kwargs: Any) -> None:
|
||||
def __init__(
|
||||
self,
|
||||
*,
|
||||
store: ToolMetricsStore | None = None,
|
||||
skill_only: bool = False,
|
||||
**kwargs: Any,
|
||||
) -> None:
|
||||
super().__init__(**kwargs)
|
||||
self._store_override = store
|
||||
self._skill_only = skill_only
|
||||
|
||||
# -- store / context ----------------------------------------------------
|
||||
|
||||
@@ -205,6 +214,12 @@ class ToolMetricsMiddleware(AgentMiddleware[AgentState]):
|
||||
return None
|
||||
return make_tool_metrics_store(sf)
|
||||
|
||||
@staticmethod
|
||||
def _metrics_enabled() -> bool:
|
||||
"""Read the administrator switch at call time so no restart is needed."""
|
||||
|
||||
return bool(get_call_metrics_settings().enabled)
|
||||
|
||||
@staticmethod
|
||||
def _runtime_lookup(request: ToolCallRequest, keys: tuple[str, ...]) -> str | None:
|
||||
runtime = getattr(request, "runtime", None)
|
||||
@@ -280,7 +295,11 @@ class ToolMetricsMiddleware(AgentMiddleware[AgentState]):
|
||||
handler: Callable[[ToolCallRequest], ToolMessage | Command],
|
||||
) -> ToolMessage | Command:
|
||||
record = self._build_base_record(request)
|
||||
if is_internal_tool_name(record.tool_name):
|
||||
if (
|
||||
not self._metrics_enabled()
|
||||
or is_internal_tool_name(record.tool_name)
|
||||
or (self._skill_only and not record.skill_name)
|
||||
):
|
||||
return handler(request)
|
||||
started = time.monotonic()
|
||||
try:
|
||||
@@ -303,7 +322,11 @@ class ToolMetricsMiddleware(AgentMiddleware[AgentState]):
|
||||
handler: Callable[[ToolCallRequest], Awaitable[ToolMessage | Command]],
|
||||
) -> ToolMessage | Command:
|
||||
record = self._build_base_record(request)
|
||||
if is_internal_tool_name(record.tool_name):
|
||||
if (
|
||||
not self._metrics_enabled()
|
||||
or is_internal_tool_name(record.tool_name)
|
||||
or (self._skill_only and not record.skill_name)
|
||||
):
|
||||
return await handler(request)
|
||||
started = time.monotonic()
|
||||
try:
|
||||
|
||||
Reference in new issue
Block a user