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
@@ -145,6 +145,7 @@ class KnowledgeService:
|
||||
source_id=draft.source_id,
|
||||
status=draft.status,
|
||||
confidence=draft.confidence,
|
||||
folder=draft.folder,
|
||||
tags=draft.tags,
|
||||
created_by=created_by,
|
||||
)
|
||||
@@ -223,15 +224,21 @@ class KnowledgeService:
|
||||
created_by: str | None = None,
|
||||
skip_existing: bool = False,
|
||||
model_name: str | None = None,
|
||||
template_id: str | None = None,
|
||||
folder: str | None = None,
|
||||
) -> dict[str, Any]:
|
||||
"""Sediment a thread into a knowledge note.
|
||||
|
||||
``model_name`` optionally overrides the configured extraction model for
|
||||
this one capture (falls back to ``config.extract_model_name``).
|
||||
``template_id`` selects an operator-edited extraction prompt template
|
||||
(falls back to the ``thread`` default, then the built-in prompt).
|
||||
``folder`` files the resulting note(s) under a user directory.
|
||||
|
||||
Returns ``{"note": <note>, "created": bool}``. Raises
|
||||
:class:`EmptyThreadError` when there is nothing worth keeping.
|
||||
"""
|
||||
extract_prompt = await self._resolve_extract_prompt(template_id, "thread")
|
||||
# Always-on entry log: proves a capture was triggered for this thread,
|
||||
# regardless of how it ends (success / skip / fallback). Lets you tell
|
||||
# "capture never ran" apart from "logs not visible" via one grep.
|
||||
@@ -276,6 +283,7 @@ class KnowledgeService:
|
||||
model_name=model_name or self.config.extract_model_name,
|
||||
max_input_chars=self.config.extract_max_input_chars,
|
||||
allow_multi=self.config.multi_note_enabled,
|
||||
system_prompt=extract_prompt,
|
||||
)
|
||||
except llm_extractor.LLMRejectedIngest as exc:
|
||||
# The model judged this conversation worthless (greeting / meta
|
||||
@@ -324,6 +332,8 @@ class KnowledgeService:
|
||||
normalized = normalize_status(effective_status)
|
||||
for draft in drafts:
|
||||
draft.status = normalized
|
||||
if folder:
|
||||
draft.folder = folder
|
||||
# Cross-link sibling notes from the same conversation by title, so the
|
||||
# split pages reference each other (resolved once all are persisted).
|
||||
if len(drafts) > 1:
|
||||
@@ -370,6 +380,7 @@ class KnowledgeService:
|
||||
tags: list[str] | None = None,
|
||||
status: str = "approved",
|
||||
summary: str | None = None,
|
||||
folder: str | None = None,
|
||||
created_by: str | None = None,
|
||||
) -> dict[str, Any]:
|
||||
"""Create a note directly from product-edited Markdown."""
|
||||
@@ -388,6 +399,7 @@ class KnowledgeService:
|
||||
source_type="manual",
|
||||
source_id=None,
|
||||
status=normalize_status(status),
|
||||
folder=(folder or None),
|
||||
tags=base_tags,
|
||||
confidence=0.5,
|
||||
related=extractor.related_links(self.project_name),
|
||||
@@ -431,6 +443,8 @@ class KnowledgeService:
|
||||
created_by: str | None = None,
|
||||
model_name: str | None = None,
|
||||
skip_existing: bool = False,
|
||||
template_id: str | None = None,
|
||||
folder: str | None = None,
|
||||
) -> dict[str, Any]:
|
||||
"""Import a file (already converted to Markdown/text) into knowledge notes.
|
||||
|
||||
@@ -468,6 +482,7 @@ class KnowledgeService:
|
||||
logger.info("[knowledge] file '%s' already imported (note %s); skipping", filename, existing.get("id"))
|
||||
return {"note": existing, "created": False, "notes": [existing], "llm_fallback": False}
|
||||
|
||||
extract_prompt = await self._resolve_extract_prompt(template_id, "document")
|
||||
drafts: list[NoteDraft] = []
|
||||
llm_fallback = False
|
||||
if self.config.llm_extract_enabled:
|
||||
@@ -482,6 +497,7 @@ class KnowledgeService:
|
||||
model_name=model_name or self.config.extract_model_name,
|
||||
max_input_chars=self.config.extract_max_input_chars,
|
||||
allow_multi=self.config.multi_note_enabled,
|
||||
system_prompt=extract_prompt,
|
||||
)
|
||||
except llm_extractor.LLMRejectedIngest as exc:
|
||||
raise EmptyThreadError(f"File {filename} judged not worth ingesting: {exc}") from exc
|
||||
@@ -496,6 +512,8 @@ class KnowledgeService:
|
||||
normalized = normalize_status(effective_status)
|
||||
for draft in drafts:
|
||||
draft.status = normalized
|
||||
if folder:
|
||||
draft.folder = folder
|
||||
|
||||
# Cross-link sibling notes split from the same document (resolved by title
|
||||
# once all are persisted), mirroring multi-note thread capture.
|
||||
@@ -526,6 +544,7 @@ class KnowledgeService:
|
||||
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]:
|
||||
@@ -535,6 +554,7 @@ class KnowledgeService:
|
||||
entity=entity,
|
||||
source_type=normalize_source_type(source_type) if source_type else None,
|
||||
status=status,
|
||||
folder=folder,
|
||||
limit=limit,
|
||||
offset=offset,
|
||||
)
|
||||
@@ -555,6 +575,8 @@ class KnowledgeService:
|
||||
tags: list[str] | None = None,
|
||||
status: str | None = None,
|
||||
summary: str | None = None,
|
||||
folder: str | None = None,
|
||||
set_folder: bool = False,
|
||||
updated_by: str | None = None,
|
||||
) -> dict[str, Any] | None:
|
||||
current = await self.repo.get_note(note_id, with_sources=False)
|
||||
@@ -576,6 +598,8 @@ class KnowledgeService:
|
||||
content_md=body,
|
||||
status=normalize_status(status) if status else None,
|
||||
tags=tags,
|
||||
folder=folder,
|
||||
set_folder=set_folder,
|
||||
updated_by=updated_by,
|
||||
)
|
||||
if note is None:
|
||||
@@ -604,6 +628,84 @@ class KnowledgeService:
|
||||
async def list_versions(self, note_id: str) -> list[dict[str, Any]]:
|
||||
return await self.repo.list_versions(note_id)
|
||||
|
||||
# -- folders ----------------------------------------------------------
|
||||
async def list_folders(self) -> list[dict[str, Any]]:
|
||||
return await self.repo.list_folders()
|
||||
|
||||
async def create_folder(self, path: str, *, created_by: str | None = None) -> dict[str, Any]:
|
||||
return await self.repo.create_folder(path, created_by=created_by)
|
||||
|
||||
async def rename_folder(self, folder_id: str, new_path: str) -> dict[str, Any] | None:
|
||||
return await self.repo.rename_folder(folder_id, new_path)
|
||||
|
||||
async def delete_folder(self, folder_id: str, *, reassign_to: str | None = None) -> bool:
|
||||
return await self.repo.delete_folder(folder_id, reassign_to=reassign_to)
|
||||
|
||||
# -- extraction templates ---------------------------------------------
|
||||
async def _resolve_extract_prompt(self, template_id: str | None, scope: str) -> str | None:
|
||||
"""Return the system prompt for a capture, or ``None`` for the built-in default.
|
||||
|
||||
Resolution: explicit ``template_id`` → the scope's default template →
|
||||
``None`` (the extractor then uses its hard-coded prompt). Best-effort: any
|
||||
lookup failure degrades to the built-in prompt so capture never breaks.
|
||||
"""
|
||||
try:
|
||||
if template_id:
|
||||
tpl = await self.repo.get_template(template_id)
|
||||
if tpl and tpl.get("enabled") and (tpl.get("system_prompt") or "").strip():
|
||||
return tpl["system_prompt"]
|
||||
default = await self.repo.get_default_template(scope)
|
||||
if default and (default.get("system_prompt") or "").strip():
|
||||
return default["system_prompt"]
|
||||
except Exception:
|
||||
logger.exception("Failed to resolve knowledge extraction template (id=%s, scope=%s)", template_id, scope)
|
||||
return None
|
||||
|
||||
async def list_extract_templates(self, *, scope: str | None = None, enabled_only: bool = False) -> list[dict[str, Any]]:
|
||||
return await self.repo.list_templates(scope=scope, enabled_only=enabled_only)
|
||||
|
||||
async def get_extract_template(self, template_id: str) -> dict[str, Any] | None:
|
||||
return await self.repo.get_template(template_id)
|
||||
|
||||
async def create_extract_template(self, **kwargs: Any) -> dict[str, Any]:
|
||||
return await self.repo.create_template(**kwargs)
|
||||
|
||||
async def update_extract_template(self, template_id: str, **kwargs: Any) -> dict[str, Any] | None:
|
||||
return await self.repo.update_template(template_id, **kwargs)
|
||||
|
||||
async def delete_extract_template(self, template_id: str) -> bool:
|
||||
return await self.repo.delete_template(template_id)
|
||||
|
||||
async def template_name_taken(self, name: str, *, exclude_id: str | None = None) -> bool:
|
||||
return await self.repo.template_name_taken(name, exclude_id=exclude_id)
|
||||
|
||||
async def seed_default_templates(self, *, created_by: str | None = None) -> int:
|
||||
"""Seed the two built-in distillation prompts as editable templates (idempotent).
|
||||
|
||||
Runs once on startup so an operator immediately sees — and can edit — the
|
||||
prompts that drive 对话 / 文档 extraction. Existing templates with the same
|
||||
name are left untouched. Returns the number of templates created.
|
||||
"""
|
||||
from deerflow.knowledge import llm_extractor
|
||||
|
||||
guidance = llm_extractor.WIKI_CAPTURE_GUIDANCE
|
||||
seeds = [
|
||||
("默认 · 对话提炼", "thread", llm_extractor._SYSTEM_PROMPT + "\n\n" + guidance, "对话沉淀的默认提炼提示词(wiki-capture 规范)。"),
|
||||
("默认 · 文档提炼", "document", llm_extractor._DOC_SYSTEM_PROMPT + "\n\n" + guidance, "文档导入的默认提炼提示词(wiki-capture 规范)。"),
|
||||
]
|
||||
created = 0
|
||||
for name, scope, prompt, desc in seeds:
|
||||
try:
|
||||
if await self.repo.template_name_taken(name):
|
||||
continue
|
||||
await self.repo.create_template(name=name, system_prompt=prompt, scope=scope, description=desc, is_default=True, enabled=True, created_by=created_by)
|
||||
created += 1
|
||||
except Exception:
|
||||
logger.exception("Failed to seed knowledge extraction template '%s'", name)
|
||||
if created:
|
||||
logger.info("[knowledge] seeded %d default extraction template(s)", created)
|
||||
return created
|
||||
|
||||
async def delete_note(self, note_id: str, *, updated_by: str | None = None, hard: bool = False) -> bool:
|
||||
"""Soft delete (archive) by default; hard delete removes the vault file."""
|
||||
current = await self.repo.get_note(note_id, with_sources=False)
|
||||
|
||||
Reference in new issue
Block a user