feat: 知识库文件夹/模板、技能压缩设置与摘要窗口调优
- 知识库新增文件夹与模板支持(模型/仓储/服务/迁移),前端知识管理组件 - 岗位管理模型与接口完善 - 技能新增压缩设置页与相关存储/工具能力 - 系统设置扩展配置项 - 摘要中间件 keep 窗口改用 token 预算(16000),减少重复压缩 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
e6662cf673
commit
5ebbb82d66
54 files changed
+2658
-100
No files matched your search
+77
-31
@@ -758,51 +758,83 @@ def _get_memory_context(agent_name: str | None = None, *, app_config: AppConfig
|
||||
from deerflow.config.memory_config import get_effective_memory_config
|
||||
from deerflow.runtime.user_context import get_effective_user_id
|
||||
|
||||
user_id = get_effective_user_id()
|
||||
config = get_effective_memory_config(user_id)
|
||||
|
||||
manager = build_memory_manager(
|
||||
user_id=user_id,
|
||||
agent_id=agent_name or "default",
|
||||
config=config,
|
||||
app_config=app_config,
|
||||
)
|
||||
user_id = get_effective_user_id()
|
||||
config = get_effective_memory_config(user_id)
|
||||
|
||||
manager = build_memory_manager(
|
||||
user_id=user_id,
|
||||
agent_id=agent_name or "default",
|
||||
config=config,
|
||||
app_config=app_config,
|
||||
)
|
||||
# 只取静态块 (builtin 的 frozen snapshot + Hindsight 的模式说明)。
|
||||
# Hindsight 的动态召回需要当前 query,走 MemoryMiddleware.before_model。
|
||||
context_content = manager.build_system_prompt()
|
||||
|
||||
if not context_content.strip():
|
||||
return ""
|
||||
|
||||
return f"{context_content}\n"
|
||||
context_content = manager.build_system_prompt()
|
||||
|
||||
if not context_content.strip():
|
||||
return ""
|
||||
|
||||
return f"{context_content}\n"
|
||||
except Exception:
|
||||
logger.exception("Failed to load memory context")
|
||||
return ""
|
||||
|
||||
|
||||
def _render_skill_block(name: str, description: str, category: str, location: str, display_hint: str) -> str:
|
||||
return f" <skill>\n <name>{name}</name>\n <description>{description} {_skill_mutability_label(category)}</description>\n <location>{location}</location>{display_hint}\n </skill>"
|
||||
|
||||
|
||||
@lru_cache(maxsize=32)
|
||||
def _get_cached_skills_prompt_section(
|
||||
skill_signature: tuple[tuple[str, str, str, str], ...],
|
||||
skill_signature: tuple[tuple[str, str, str, str, bool], ...],
|
||||
display_signature: tuple[tuple[str, str, str], ...],
|
||||
available_skills_key: tuple[str, ...] | None,
|
||||
container_base_path: str,
|
||||
skill_evolution_section: str,
|
||||
compression_enabled: bool,
|
||||
keep_index: bool,
|
||||
) -> str:
|
||||
display_by_skill = {name: (mode, custom_instruction) for name, mode, custom_instruction in display_signature}
|
||||
filtered = [(name, description, category, location) for name, description, category, location in skill_signature if available_skills_key is None or name in available_skills_key]
|
||||
filtered = [(name, description, category, location, always_on) for name, description, category, location, always_on in skill_signature if available_skills_key is None or name in available_skills_key]
|
||||
|
||||
def _display_hint(name: str) -> str:
|
||||
display_mode, custom_instruction = display_by_skill.get(name, ("", ""))
|
||||
if display_mode:
|
||||
return f"\n <display mode=\"{display_mode}\">{custom_instruction}</display>"
|
||||
return ""
|
||||
|
||||
skills_list = ""
|
||||
compression_note = ""
|
||||
if filtered:
|
||||
items = []
|
||||
for name, description, category, location in filtered:
|
||||
display_mode, custom_instruction = display_by_skill.get(name, ("", ""))
|
||||
display_hint = ""
|
||||
if display_mode:
|
||||
display_hint = f"\n <display mode=\"{display_mode}\">{custom_instruction}</display>"
|
||||
items.append(
|
||||
f" <skill>\n <name>{name}</name>\n <description>{description} {_skill_mutability_label(category)}</description>\n <location>{location}</location>{display_hint}\n </skill>"
|
||||
)
|
||||
skill_items = "\n".join(items)
|
||||
skills_list = f"<available_skills>\n{skill_items}\n</available_skills>"
|
||||
if compression_enabled:
|
||||
# 压缩开启:常驻(always_on)技能全文注入;其余技能从提示词正文剔除,
|
||||
# 仅保留可选的"名字索引",由 agent 通过 search_skills 工具按需检索。
|
||||
resident = [s for s in filtered if s[4]]
|
||||
compressed = [s for s in filtered if not s[4]]
|
||||
blocks: list[str] = []
|
||||
for name, description, category, location, _ in resident:
|
||||
blocks.append(_render_skill_block(name, description, category, location, _display_hint(name)))
|
||||
if blocks:
|
||||
skills_list = "<available_skills>\n" + "\n".join(blocks) + "\n</available_skills>"
|
||||
if compressed:
|
||||
index_block = ""
|
||||
if keep_index:
|
||||
index_items = "\n".join(f" <skill><name>{name}</name></skill>" for name, _, _, _, _ in compressed)
|
||||
index_block = f"\n<compressed_skills>\n{index_items}\n</compressed_skills>"
|
||||
compression_note = (
|
||||
"\n**Additional skills are available but not listed in full above to keep this prompt compact.**\n"
|
||||
"When a user query might match a skill that is NOT fully described above, call the `search_skills` "
|
||||
"tool with a short keyword query to discover matching skills (it returns each match's description and "
|
||||
f"`location`), then `read_file` the returned `location` and follow the skill. There are {len(compressed)} "
|
||||
"such compressed skill(s)."
|
||||
f"{index_block}\n"
|
||||
)
|
||||
else:
|
||||
items = [
|
||||
_render_skill_block(name, description, category, location, _display_hint(name))
|
||||
for name, description, category, location, _ in filtered
|
||||
]
|
||||
skills_list = "<available_skills>\n" + "\n".join(items) + "\n</available_skills>"
|
||||
|
||||
# Restricted mode: agent has an explicit skill allowlist.
|
||||
# We deliberately omit the "Skills are located at: <path>" hint and the
|
||||
@@ -842,7 +874,7 @@ You have access to skills that provide optimized workflows for specific tasks. E
|
||||
4. Load referenced resources only when needed during execution
|
||||
5. Follow the skill's instructions precisely
|
||||
{display_pattern}
|
||||
{location_hint}{evolution_block}
|
||||
{location_hint}{evolution_block}{compression_note}
|
||||
{skills_list}
|
||||
|
||||
</skill_system>"""
|
||||
@@ -877,13 +909,27 @@ def get_skills_prompt_section(
|
||||
is_restricted = available_skills is not None
|
||||
effective_skill_evolution_enabled = skill_evolution_enabled and not is_restricted
|
||||
|
||||
# 技能压缩仅作用于默认 lead agent 的全局技能集;自定义 agent 已有显式白名单
|
||||
# (技能数量本就受控),不参与压缩。
|
||||
compression_enabled = False
|
||||
keep_index = True
|
||||
if not is_restricted:
|
||||
try:
|
||||
from deerflow.config.system_settings import get_skill_compression_settings
|
||||
|
||||
compression_settings = get_skill_compression_settings()
|
||||
compression_enabled = compression_settings.enabled
|
||||
keep_index = compression_settings.keep_index_in_prompt
|
||||
except Exception:
|
||||
logger.debug("Failed to read skill compression settings for prompt injection", exc_info=True)
|
||||
|
||||
if not skills and not effective_skill_evolution_enabled:
|
||||
return ""
|
||||
|
||||
if available_skills is not None and not any(skill.name in available_skills for skill in skills):
|
||||
return ""
|
||||
|
||||
skill_signature = tuple((skill.name, skill.description, skill.category, skill.get_container_file_path(container_base_path)) for skill in skills)
|
||||
skill_signature = tuple((skill.name, skill.description, skill.category, skill.get_container_file_path(container_base_path), skill.always_on) for skill in skills)
|
||||
display_signature: tuple[tuple[str, str, str], ...] = ()
|
||||
if display_enabled:
|
||||
try:
|
||||
@@ -907,7 +953,7 @@ def get_skills_prompt_section(
|
||||
if not skill_signature and available_key is not None:
|
||||
return ""
|
||||
skill_evolution_section = _build_skill_evolution_section(effective_skill_evolution_enabled)
|
||||
return _get_cached_skills_prompt_section(skill_signature, display_signature, available_key, container_base_path, skill_evolution_section)
|
||||
return _get_cached_skills_prompt_section(skill_signature, display_signature, available_key, container_base_path, skill_evolution_section, compression_enabled, keep_index)
|
||||
|
||||
|
||||
def get_agent_soul(agent_id: str | None) -> str:
|
||||
|
||||
+74
@@ -37,6 +37,15 @@ logger = logging.getLogger(__name__)
|
||||
# many threads can't grow it without limit. Oldest entries are evicted first.
|
||||
_SUMMARY_CACHE_MAX_THREADS = 512
|
||||
|
||||
# After compaction the preserved window must land *comfortably* below every
|
||||
# trigger threshold, otherwise the "compressed" context is still over the
|
||||
# trigger and re-summarizes on the very next turn (the sticky boundary never
|
||||
# sticks). We force the kept suffix down to this fraction of each trigger so a
|
||||
# re-compaction only happens once meaningfully *new* content accumulates — not
|
||||
# on every follow-up question. ``keep`` only sets a *target*; this is the hard
|
||||
# headroom guarantee on top of it.
|
||||
_KEEP_HEADROOM_FRACTION = 0.75
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class SummarizationEvent:
|
||||
@@ -319,6 +328,15 @@ class DeerFlowSummarizationMiddleware(SummarizationMiddleware):
|
||||
if cutoff_index <= 0:
|
||||
return None
|
||||
|
||||
# Force the preserved window below every trigger (with headroom) so the
|
||||
# post-compaction context doesn't immediately re-trigger. Without this,
|
||||
# a ``keep`` expressed in *messages* (e.g. 20) can itself blow past a
|
||||
# *token* trigger (e.g. 60000) when recent messages are large (tool
|
||||
# outputs, pasted docs) — and then every follow-up question re-summarizes.
|
||||
cutoff_index = self._apply_stickiness_headroom(messages, cutoff_index)
|
||||
if cutoff_index <= 0:
|
||||
return None
|
||||
|
||||
messages_to_summarize, preserved = self._partition_with_skill_rescue(messages, cutoff_index)
|
||||
if not messages_to_summarize:
|
||||
return None
|
||||
@@ -368,6 +386,62 @@ class DeerFlowSummarizationMiddleware(SummarizationMiddleware):
|
||||
while len(self._summary_cache) > _SUMMARY_CACHE_MAX_THREADS:
|
||||
self._summary_cache.popitem(last=False)
|
||||
|
||||
def _apply_stickiness_headroom(self, messages: list[AnyMessage], cutoff_index: int) -> int:
|
||||
"""Advance ``cutoff_index`` (keep fewer recent messages) so the preserved
|
||||
suffix lands below every trigger threshold with headroom.
|
||||
|
||||
Only ever *raises* the cutoff (summarizes more / keeps less); never keeps
|
||||
more than the caller's ``keep`` policy already chose. This is what makes
|
||||
the sticky boundary actually stick: after compaction the kept context is
|
||||
guaranteed under ~``_KEEP_HEADROOM_FRACTION`` of each trigger, so a
|
||||
re-compaction waits for genuinely new content instead of firing every turn.
|
||||
"""
|
||||
n = len(messages)
|
||||
if n <= 1:
|
||||
return cutoff_index
|
||||
|
||||
for kind, value in getattr(self, "_trigger_conditions", []):
|
||||
if kind == "messages":
|
||||
keep_cap = max(1, int(int(value) * _KEEP_HEADROOM_FRACTION))
|
||||
cutoff_index = max(cutoff_index, n - keep_cap)
|
||||
elif kind == "tokens":
|
||||
target = max(1, int(int(value) * _KEEP_HEADROOM_FRACTION))
|
||||
cutoff_index = max(cutoff_index, self._suffix_token_cutoff(messages, target))
|
||||
# "fraction" triggers depend on model profile limits; the message/token
|
||||
# caps above (when present) already bound the window, so skip them here.
|
||||
|
||||
# Always keep at least the final message, and re-snap to a safe AI/Tool
|
||||
# boundary so we never orphan a tool result.
|
||||
cutoff_index = min(cutoff_index, n - 1)
|
||||
return self._find_safe_cutoff_point(messages, cutoff_index)
|
||||
|
||||
def _suffix_token_cutoff(self, messages: list[AnyMessage], target_tokens: int) -> int:
|
||||
"""Smallest index ``i`` such that ``token_counter(messages[i:]) <= target_tokens``.
|
||||
|
||||
Binary search mirroring the stock token-based cutoff; returns 0 when the
|
||||
whole list already fits, and never returns the full length (keeps ≥1).
|
||||
"""
|
||||
n = len(messages)
|
||||
if n == 0:
|
||||
return 0
|
||||
if self.token_counter(messages) <= target_tokens:
|
||||
return 0
|
||||
|
||||
left, right = 0, n
|
||||
cutoff = n
|
||||
for _ in range(n.bit_length() + 1):
|
||||
if left >= right:
|
||||
break
|
||||
mid = (left + right) // 2
|
||||
if self.token_counter(messages[mid:]) <= target_tokens:
|
||||
cutoff = mid
|
||||
right = mid
|
||||
else:
|
||||
left = mid + 1
|
||||
if cutoff == n:
|
||||
cutoff = left
|
||||
return min(cutoff, n - 1)
|
||||
|
||||
@staticmethod
|
||||
def _resolve_request_thread_id(request: ModelRequest) -> str | None:
|
||||
"""Best-effort thread id from the model request's runtime."""
|
||||
|
||||
Reference in new issue
Block a user