mirror of
https://github.com/HKUDS/DeepTutor.git
synced 2026-12-04 20:19:29 +08:00
* chore: remove dead Python modules and helpers Delete unreferenced P0 code identified by the dead-code audit: - services/llm/registry.py and its test-only coverage - logging/adapters package (LlamaIndex log forwarding) - textbook_struct.chapter_rebuild.detect_frames - load_config_with_main_async and its async file helper - memory.trace.iter_by_ids / latest_ts - TokenTracker.format_summary - WeixinChannel._api_get - ArxivSearchTool.extract_arxiv_id_from_url - FileLibraryStore migration scaffolding (_run_migrations et al.) - eight small dead helpers in services, api/utils, and knowledge - redundant runtime C2CMessage import in the QQ channel * chore: drop the prompt-language shim and a dead storage helper The book agents were the last in-tree users of blocks/_language.py, which only re-exported services.prompt.language. Import the real module and delete the shim. storage._storage_path_from_version_entry had no caller. Verified: ruff F401/F811/F841/F821 on changed files; py_compile. --------- Co-authored-by: Antonio-Faure <209759495+Antonio-Faure@users.noreply.github.com>
88 lines
3.3 KiB
Python
88 lines
3.3 KiB
Python
"""One resolution order for skill manifests and on-demand reads."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from contextvars import ContextVar
|
|
|
|
from deeptutor.services.skill.service import (
|
|
SkillService,
|
|
get_admin_skill_service,
|
|
render_skills_manifest,
|
|
)
|
|
|
|
library_workspace: ContextVar[str] = ContextVar("skill_library_workspace", default="")
|
|
|
|
|
|
def workspace_skill_service(workspace_id: str) -> SkillService:
|
|
from deeptutor.services.workspace import get_content_workspace_service
|
|
|
|
binding = get_content_workspace_service().validate_chat_binding(workspace_id, existing=True)
|
|
return SkillService(root=binding.root / "skills", builtin_root=None)
|
|
|
|
|
|
def skill_sources(*, workspace_id: str = ""):
|
|
from deeptutor.multi_user.context import get_current_user
|
|
from deeptutor.multi_user.skill_access import assigned_skill_ids
|
|
|
|
if workspace_id:
|
|
yield workspace_skill_service(workspace_id), None, "workspace"
|
|
from deeptutor.services.skill import get_skill_service
|
|
|
|
own = get_skill_service()
|
|
yield SkillService(root=own.root, builtin_root=None), None, "account"
|
|
if not get_current_user().is_admin:
|
|
granted = assigned_skill_ids(get_current_user().id)
|
|
if granted:
|
|
yield (
|
|
SkillService(root=get_admin_skill_service().root, builtin_root=None),
|
|
granted,
|
|
"admin",
|
|
)
|
|
# Keep builtins last, including when a granted admin skill shadows one.
|
|
if own._builtin_root is not None:
|
|
yield SkillService(root=own._builtin_root, builtin_root=None), None, "builtin"
|
|
|
|
|
|
def runtime_skills() -> dict[str, SkillService]:
|
|
from deeptutor.services.workspace.context import current_workspace_id
|
|
from deeptutor.services.workspace.resources import current_resources
|
|
|
|
selected = current_resources().skills
|
|
resolved: dict[str, SkillService] = {}
|
|
for service, granted, _source in skill_sources(workspace_id=current_workspace_id()):
|
|
for info in service.list_skills():
|
|
name = info.name
|
|
if selected is not None and name not in selected:
|
|
continue
|
|
if granted is not None and name not in granted:
|
|
continue
|
|
resolved.setdefault(name, service)
|
|
return resolved
|
|
|
|
|
|
def skill_context() -> tuple[str, str]:
|
|
"""Return ``(active_skills, skills_manifest)`` for the current scope.
|
|
|
|
``active_skills`` holds the full bodies of available ``always`` skills —
|
|
standing instructions that belong in the system prompt. ``skills_manifest``
|
|
lists the on-demand skills; it changes whenever a skill is installed,
|
|
enabled, or selected, so the loop replays it as a runtime snapshot.
|
|
"""
|
|
entries = []
|
|
always = []
|
|
resolved = runtime_skills()
|
|
for service in dict.fromkeys(resolved.values()):
|
|
for entry in service.summary_entries():
|
|
if resolved.get(entry.name) is not service:
|
|
continue
|
|
entries.append(entry)
|
|
if entry.always and entry.available:
|
|
always.append(service.load_for_context([entry.name]))
|
|
active = "\n\n".join(part for part in always if part)
|
|
return active, render_skills_manifest(entries)
|
|
|
|
|
|
def skill_manifest() -> str:
|
|
"""Both skill parts joined, for callers that need a single text."""
|
|
return "\n\n".join(part for part in skill_context() if part)
|