feat: 知识库文件夹/模板、技能压缩设置与摘要窗口调优

- 知识库新增文件夹与模板支持(模型/仓储/服务/迁移),前端知识管理组件
- 岗位管理模型与接口完善
- 技能新增压缩设置页与相关存储/工具能力
- 系统设置扩展配置项
- 摘要中间件 keep 窗口改用 token 预算(16000),减少重复压缩

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
吉鑫升andClaude Opus 4.8 committed 2026-06-16 15:23:54 +08:00
1 parent e6662cf673
commit 5ebbb82d66
54 files changed
+2658 -100

No files matched your search

@@ -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: