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

@@ -184,6 +184,14 @@ async def langgraph_runtime(app: FastAPI) -> AsyncGenerator[None, None]:
app.state.knowledge_service = make_knowledge_service(sf, config.knowledge)
# Seed the built-in extraction prompts as editable templates so operators
# can immediately view/edit them (idempotent; never overwrites edits).
if app.state.knowledge_service is not None:
try:
await app.state.knowledge_service.seed_default_templates()
except Exception:
logger.exception("Failed to seed knowledge extraction templates")
# Background auto-sedimentation queue (phase 3) — drains finished
# conversations off the chat path. Worker only does work when
# ``knowledge.auto_ingest_enabled`` is set; the queue is always wired so
@@ -54,6 +54,8 @@ class IngestThreadRequest(BaseModel):
description="When true, sediment the thread asynchronously in a background task and return immediately.",
)
model_name: str | None = Field(default=None, description="可选:指定用于知识提炼的大模型名称;留空使用默认配置")
template_id: str | None = Field(default=None, description="可选:抽取提示词模板 id;留空使用对话默认模板")
folder: str | None = Field(default=None, description="可选:沉淀结果归入的目录(斜杠分隔路径)")
class BatchIngestRequest(BaseModel):
@@ -64,6 +66,8 @@ class BatchIngestRequest(BaseModel):
skip_existing: bool = True
mode: str = "summary"
model_name: str | None = Field(default=None, description="可选:指定用于知识提炼的大模型名称;留空使用默认配置")
template_id: str | None = Field(default=None, description="可选:抽取提示词模板 id;留空使用对话默认模板")
folder: str | None = Field(default=None, description="可选:沉淀结果归入的目录(斜杠分隔路径)")
class SearchResultItem(BaseModel):
@@ -90,6 +94,7 @@ class CreateNoteRequest(BaseModel):
summary: str | None = None
tags: list[str] = Field(default_factory=list)
status: str = "approved"
folder: str | None = None
class UpdateNoteRequest(BaseModel):
@@ -98,6 +103,34 @@ class UpdateNoteRequest(BaseModel):
summary: str | None = None
tags: list[str] | None = None
status: str | None = None
# Present (even as null) ⇒ (re)assign the note's directory; absent ⇒ leave as-is.
folder: str | None = None
class CreateFolderRequest(BaseModel):
path: str = Field(description="目录全路径,斜杠分隔,如 投研/行业")
class RenameFolderRequest(BaseModel):
path: str = Field(description="新的目录全路径")
class ExtractTemplateRequest(BaseModel):
name: str
system_prompt: str
scope: str = Field(default="both", description="thread | document | both")
description: str | None = None
is_default: bool = False
enabled: bool = True
class UpdateExtractTemplateRequest(BaseModel):
name: str | None = None
system_prompt: str | None = None
scope: str | None = None
description: str | None = None
is_default: bool | None = None
enabled: bool | None = None
class SearchRequest(BaseModel):
@@ -187,6 +220,8 @@ async def _ingest_thread_background(service, checkpointer, thread_id: str, body:
reference_batches=batches,
created_by=created_by,
model_name=body.model_name,
template_id=body.template_id,
folder=body.folder,
)
except EmptyThreadError:
logger.warning("Background knowledge ingest skipped: thread %s has no sediment-able content", sanitize_log_param(thread_id))
@@ -237,6 +272,8 @@ async def ingest_thread(thread_id: str, body: IngestThreadRequest, request: Requ
reference_batches=batches,
created_by=get_effective_user_id(),
model_name=body.model_name,
template_id=body.template_id,
folder=body.folder,
)
except EmptyThreadError:
raise HTTPException(status_code=400, detail="Thread has no sediment-able content")
@@ -285,6 +322,8 @@ async def ingest_threads_batch(body: BatchIngestRequest, request: Request) -> di
created_by=get_effective_user_id(),
skip_existing=body.skip_existing,
model_name=body.model_name,
template_id=body.template_id,
folder=body.folder,
)
if result.get("llm_fallback"):
llm_fallback += 1
@@ -352,6 +391,8 @@ async def ingest_file(
tags: str | None = Form(default=None),
status: str = Form(default="approved"),
model_name: str | None = Form(default=None),
template_id: str | None = Form(default=None),
folder: str | None = Form(default=None),
) -> dict[str, Any]:
"""Import an uploaded file into the knowledge base as wiki note(s).
@@ -371,6 +412,8 @@ async def ingest_file(
status=status,
created_by=get_effective_user_id(),
model_name=(model_name or None),
template_id=(template_id or None),
folder=(folder or None),
)
except EmptyThreadError:
raise HTTPException(status_code=422, detail="文件内容无可沉淀的知识")
@@ -413,6 +456,7 @@ async def create_note(body: CreateNoteRequest, request: Request) -> dict[str, An
summary=body.summary,
tags=body.tags,
status=body.status,
folder=body.folder,
created_by=get_effective_user_id(),
)
return {"note": note}
@@ -427,13 +471,14 @@ async def list_notes(
entity: str | None = None,
source_type: str | None = None,
status: str | None = None,
folder: str | None = None,
limit: int = 20,
offset: int = 0,
) -> dict[str, Any]:
service = get_knowledge_service(request)
limit = max(1, min(limit, 100))
offset = max(0, offset)
return await service.list_notes(keyword=keyword, tag=tag, entity=entity, source_type=source_type, status=status, limit=limit, offset=offset)
return await service.list_notes(keyword=keyword, tag=tag, entity=entity, source_type=source_type, status=status, folder=folder, limit=limit, offset=offset)
@router.get("/notes/{note_id}")
@@ -451,6 +496,9 @@ async def get_note(note_id: str, request: Request) -> dict[str, Any]:
@require_auth
async def update_note(note_id: str, body: UpdateNoteRequest, request: Request) -> dict[str, Any]:
service = get_knowledge_service(request)
# ``folder`` present in the request body (even as null) ⇒ (re)assign the
# directory; absent ⇒ leave it untouched.
folder_provided = "folder" in body.model_fields_set
note = await service.update_note(
note_id,
title=body.title,
@@ -458,6 +506,8 @@ async def update_note(note_id: str, body: UpdateNoteRequest, request: Request) -
summary=body.summary,
tags=body.tags,
status=body.status,
folder=body.folder,
set_folder=folder_provided,
updated_by=get_effective_user_id(),
)
if note is None:
@@ -634,3 +684,119 @@ async def export_file(filename: str, request: Request) -> FileResponse:
raise HTTPException(status_code=404, detail="Export file not found")
media_type = mimetypes.guess_type(filename)[0] or "application/octet-stream"
return FileResponse(path, media_type=media_type)
# --------------------------------------------------------------------------- #
# Folders (user-managed directories)
# --------------------------------------------------------------------------- #
@router.get("/folders")
@require_auth
async def list_folders(request: Request) -> dict[str, Any]:
"""List every managed knowledge directory (for the directory tree + picker)."""
service = get_knowledge_service(request)
return {"items": await service.list_folders()}
@router.post("/folders")
@require_auth
async def create_folder(body: CreateFolderRequest, request: Request) -> dict[str, Any]:
"""Create a directory (parents auto-created). Notes are filed into it later."""
service = get_knowledge_service(request)
try:
folder = await service.create_folder(body.path, created_by=get_effective_user_id())
except ValueError as exc:
raise HTTPException(status_code=400, detail=str(exc))
return {"folder": folder}
@router.put("/folders/{folder_id}")
@require_auth
async def rename_folder(folder_id: str, body: RenameFolderRequest, request: Request) -> dict[str, Any]:
"""Rename/move a directory; descendants + filed notes follow the new prefix."""
service = get_knowledge_service(request)
try:
folder = await service.rename_folder(folder_id, body.path)
except ValueError as exc:
raise HTTPException(status_code=400, detail=str(exc))
if folder is None:
raise HTTPException(status_code=404, detail="Folder not found")
return {"folder": folder}
@router.delete("/folders/{folder_id}")
@require_auth
async def delete_folder(folder_id: str, request: Request, reassign_to: str | None = None) -> dict[str, Any]:
"""Delete a directory (and sub-dirs); notes inside move to ``reassign_to`` or unfiled."""
service = get_knowledge_service(request)
if not await service.delete_folder(folder_id, reassign_to=reassign_to):
raise HTTPException(status_code=404, detail="Folder not found")
return {"ok": True}
# --------------------------------------------------------------------------- #
# Extraction prompt templates
# --------------------------------------------------------------------------- #
@router.get("/extract-templates")
@require_auth
async def list_extract_templates(request: Request, scope: str | None = None, enabled_only: bool = False) -> dict[str, Any]:
"""List editable extraction-prompt templates (optionally filtered by scope)."""
service = get_knowledge_service(request)
return {"items": await service.list_extract_templates(scope=scope, enabled_only=enabled_only)}
@router.get("/extract-templates/{template_id}")
@require_auth
async def get_extract_template(template_id: str, request: Request) -> dict[str, Any]:
service = get_knowledge_service(request)
tpl = await service.get_extract_template(template_id)
if tpl is None:
raise HTTPException(status_code=404, detail="Template not found")
return {"template": tpl}
@router.post("/extract-templates")
@require_auth
async def create_extract_template(body: ExtractTemplateRequest, request: Request) -> dict[str, Any]:
service = get_knowledge_service(request)
if await service.template_name_taken(body.name):
raise HTTPException(status_code=409, detail="同名模板已存在")
tpl = await service.create_extract_template(
name=body.name,
system_prompt=body.system_prompt,
scope=body.scope,
description=body.description,
is_default=body.is_default,
enabled=body.enabled,
created_by=get_effective_user_id(),
)
return {"template": tpl}
@router.put("/extract-templates/{template_id}")
@require_auth
async def update_extract_template(template_id: str, body: UpdateExtractTemplateRequest, request: Request) -> dict[str, Any]:
service = get_knowledge_service(request)
if body.name is not None and await service.template_name_taken(body.name, exclude_id=template_id):
raise HTTPException(status_code=409, detail="同名模板已存在")
tpl = await service.update_extract_template(
template_id,
name=body.name,
system_prompt=body.system_prompt,
scope=body.scope,
description=body.description,
is_default=body.is_default,
enabled=body.enabled,
updated_by=get_effective_user_id(),
)
if tpl is None:
raise HTTPException(status_code=404, detail="Template not found")
return {"template": tpl}
@router.delete("/extract-templates/{template_id}")
@require_auth
async def delete_extract_template(template_id: str, request: Request) -> dict[str, Any]:
service = get_knowledge_service(request)
if not await service.delete_extract_template(template_id):
raise HTTPException(status_code=404, detail="Template not found")
return {"ok": True}
@@ -110,6 +110,10 @@ class PositionUsersResponse(BaseModel):
users: list[PositionUserResponse]
class MyPositionResponse(BaseModel):
position: PositionResponse | None = None
# ----------------------------------------------------------------- position CRUD
@@ -127,6 +131,28 @@ async def list_position_users(request: Request, store: PositionStore = Depends(g
return PositionUsersResponse(users=[PositionUserResponse(**u) for u in users])
@router.get("/me", response_model=MyPositionResponse, summary="Get My Effective Position")
async def get_my_position(request: Request, store: PositionStore = Depends(get_position_store)) -> MyPositionResponse:
"""The caller's own position — open to any authenticated user (not admin).
Returns the explicitly-assigned position, falling back to the default
position (which unassigned users inherit). ``null`` when neither exists or
the caller is anonymous. Powers the top-bar 岗位 label.
"""
user = await get_optional_user_from_request(request)
if user is None:
return MyPositionResponse(position=None)
record = None
pid = await store.get_user_position_id(str(user.id))
if pid:
record = await store.get_position(pid)
if record is None:
default_id = await store.get_default_position_id()
if default_id:
record = await store.get_position(default_id)
return MyPositionResponse(position=PositionResponse(**record) if record else None)
@router.post("", response_model=PositionResponse, status_code=201, summary="Create Position")
async def create_position(request: Request, body: PositionCreateRequest, store: PositionStore = Depends(get_position_store)) -> PositionResponse:
await _require_admin(request)
@@ -154,6 +154,7 @@ class SkillResponse(BaseModel):
license: str | None = Field(None, description="License information")
category: SkillCategory = Field(..., description="Category of the skill (public or custom)")
enabled: bool = Field(default=True, description="Whether this skill is enabled")
always_on: bool = Field(default=False, description="常驻(免压缩):开启技能压缩后仍全文注入系统提示词")
use_count: int = Field(default=0, description="Number of times the skill has been used")
view_count: int = Field(default=0, description="Number of times the skill has been viewed")
patch_count: int = Field(default=0, description="Number of times the skill has been patched")
@@ -186,6 +187,12 @@ class SkillUpdateRequest(BaseModel):
enabled: bool = Field(..., description="Whether to enable or disable the skill")
class SkillAlwaysOnUpdateRequest(BaseModel):
"""Request model for updating a skill's 常驻(免压缩) flag."""
always_on: bool = Field(..., description="Whether this skill bypasses skill compression (full-text injected)")
class SkillDisplayUpdateRequest(BaseModel):
"""Request model for updating a skill's frontend display adapter."""
@@ -334,6 +341,7 @@ def _skill_to_response(
license=skill.license,
category=skill.category,
enabled=skill.enabled,
always_on=skill.always_on,
use_count=entry.get("use_count", 0),
view_count=entry.get("view_count", 0),
patch_count=entry.get("patch_count", 0),
@@ -2186,6 +2194,7 @@ async def update_skill(skill_name: str, request: SkillUpdateRequest, config: App
current_skill_config = extensions_config.skills.get(skill_name)
extensions_config.skills[skill_name] = SkillStateConfig(
enabled=request.enabled,
always_on=current_skill_config.always_on if current_skill_config else skill.always_on,
display=current_skill_config.display if current_skill_config else None,
)
@@ -2214,6 +2223,55 @@ async def update_skill(skill_name: str, request: SkillUpdateRequest, config: App
raise HTTPException(status_code=500, detail=f"Failed to update skill: {str(e)}")
@router.put(
"/skills/{skill_name}/always-on",
response_model=SkillResponse,
summary="Update Skill 常驻(免压缩) Flag",
description="Mark a skill as 常驻(免压缩) so it is injected full-text into the system prompt even when skill compression is enabled.",
)
async def update_skill_always_on(skill_name: str, request: SkillAlwaysOnUpdateRequest, config: AppConfig = Depends(get_config)) -> SkillResponse:
try:
skill_name = skill_name.replace("\r\n", "").replace("\n", "")
skills = get_or_new_skill_storage(app_config=config).load_skills(enabled_only=False)
skill = next((s for s in skills if s.name == skill_name), None)
if skill is None:
raise HTTPException(status_code=404, detail=f"Skill '{skill_name}' not found")
config_path = ExtensionsConfig.resolve_config_path()
if config_path is None:
config_path = Path.cwd().parent / "extensions_config.json"
logger.info(f"No existing extensions config found. Creating new config at: {config_path}")
extensions_config = get_extensions_config()
current_skill_config = extensions_config.skills.get(skill_name)
extensions_config.skills[skill_name] = SkillStateConfig(
enabled=current_skill_config.enabled if current_skill_config else skill.enabled,
always_on=request.always_on,
display=current_skill_config.display if current_skill_config else None,
)
with open(config_path, "w", encoding="utf-8") as f:
json.dump(_dump_extensions_config(extensions_config), f, indent=2)
reload_extensions_config()
await refresh_skills_system_prompt_cache_async()
skills = get_or_new_skill_storage(app_config=config).load_skills(enabled_only=False)
updated_skill = next((s for s in skills if s.name == skill_name), None)
if updated_skill is None:
raise HTTPException(status_code=500, detail=f"Failed to reload skill '{skill_name}' after update")
logger.info(f"Skill '{skill_name}' always_on updated to {request.always_on}")
return _skill_to_response(updated_skill)
except HTTPException:
raise
except Exception as e:
logger.error(f"Failed to update skill always_on {skill_name}: {e}", exc_info=True)
raise HTTPException(status_code=500, detail=f"Failed to update skill: {str(e)}")
@router.put(
"/skills/{skill_name}/display",
response_model=SkillResponse,
@@ -2242,6 +2300,7 @@ async def update_skill_display(
current_skill_config = extensions_config.skills.get(skill_name)
extensions_config.skills[skill_name] = SkillStateConfig(
enabled=current_skill_config.enabled if current_skill_config else skill.enabled,
always_on=current_skill_config.always_on if current_skill_config else skill.always_on,
display=request.display,
)
@@ -16,6 +16,7 @@ from deerflow.config.system_settings import (
PromptPrefixSettings,
PublishSquare,
PublishSquaresSettings,
SkillCompressionSettings,
SystemSettings,
list_publish_squares,
load_system_settings,
@@ -113,6 +114,64 @@ async def update_prompt_prefix(body: PromptPrefixUpdateRequest, request: Request
return _to_response(saved.prompt_prefix)
class SkillCompressionResponse(BaseModel):
enabled: bool = Field(..., description="Whether skill compression is active")
search_top_n: int = Field(..., description="Max skills returned by the search_skills tool")
keep_index_in_prompt: bool = Field(..., description="Whether compressed skills keep a compact name index in the prompt")
class SkillCompressionUpdateRequest(BaseModel):
enabled: bool | None = None
search_top_n: int | None = Field(default=None, ge=1, le=50)
keep_index_in_prompt: bool | None = None
def _skill_compression_to_response(settings: SkillCompressionSettings) -> SkillCompressionResponse:
return SkillCompressionResponse(
enabled=settings.enabled,
search_top_n=settings.search_top_n,
keep_index_in_prompt=settings.keep_index_in_prompt,
)
@router.get("/skill-compression", response_model=SkillCompressionResponse)
async def get_skill_compression() -> SkillCompressionResponse:
"""Return the skill-compression configuration (readable by any user).
The frontend reads this to decide whether to surface the per-skill 常驻
(免压缩) toggle on the skill detail page.
"""
return _skill_compression_to_response(load_system_settings().skill_compression)
@router.put("/skill-compression", response_model=SkillCompressionResponse)
async def update_skill_compression(body: SkillCompressionUpdateRequest, request: Request) -> SkillCompressionResponse:
"""Update the skill-compression configuration. Admin only."""
await _require_admin(request)
current = load_system_settings()
fields = body.model_dump(exclude_unset=True)
update: dict[str, object] = {}
if "enabled" in fields and fields["enabled"] is not None:
update["enabled"] = bool(fields["enabled"])
if "keep_index_in_prompt" in fields and fields["keep_index_in_prompt"] is not None:
update["keep_index_in_prompt"] = bool(fields["keep_index_in_prompt"])
if "search_top_n" in fields and fields["search_top_n"] is not None:
update["search_top_n"] = max(1, min(int(fields["search_top_n"]), 50))
compression = current.skill_compression.model_copy(update=update)
saved = save_system_settings(current.model_copy(update={"skill_compression": compression}))
# 压缩开关/参数变化会改变系统提示词的技能注入,清掉提示词缓存让其立即生效。
try:
from deerflow.agents.lead_agent.prompt import refresh_skills_system_prompt_cache_async
await refresh_skills_system_prompt_cache_async()
except Exception:
logger.debug("Failed to refresh skills prompt cache after compression update", exc_info=True)
return _skill_compression_to_response(saved.skill_compression)
def _leaderboard_to_response(settings: LeaderboardSettings) -> LeaderboardSettingsResponse:
return LeaderboardSettingsResponse(
exclude_user_ids=list(dict.fromkeys([uid for uid in settings.exclude_user_ids if uid])),