Files
DeepTutor/deeptutor/capabilities/ima/capability.py
Bingxi Zhao (Frank) 7c6bcfaef7 release: v1.5.14
Immersive Reading, self-configuration, and a two-way IMA library.

- Immersive Reading: a new capability that opens a document beside the
  thread. New reading engine (PDF pages / slides / page-sized sections),
  five reading tools, a deterministic locate pre-pass that grounds the
  turn without a second LLM call, /api/v1/reading, and a reader pane with
  selection, highlights, notes, and export to an annotated PDF or Markdown.
- Setup capability: inspect the install, apply a setting, install a
  parsing engine, or request a credential from a chat turn. New settings
  spec table; a candidate value is probed before it is committed, and API
  keys never enter the model's context.
- Tencent IMA: pipeline split into transport/envelope/models/sources/
  notes/media/inventory, an additive capability with five tools, and
  connected libraries that can finally be enumerated (bounded listings
  are reported as a lower bound).
- Retrieval: supports_rag_retrieval() replaces is_connected_kb() for the
  "can I sweep this?" question, so linked / lightrag_server / ima sources
  stop being silently dropped from every book.
- Notebooks: their own console at /notebook, per-notebook locks and atomic
  writes, copy / move / export routes, damaged files surfaced instead of
  dropped, and renaming a record no longer clears its knowledge-base link.
- Settings persistence: one lock and one atomic replace shared by every
  writer of interface.json; the PUT endpoints stop writing the
  defaults-merged view back to disk.

Release notes: assets/releases/ver1-5-14.md
2026-08-19 17:34:52 +08:00

104 lines
3.7 KiB
Python

"""IMA loop capability — inventory, source reading and authoring over a live library.
Active whenever one of the turn's selected knowledge bases is a connected Tencent
IMA library (resolved by :mod:`deeptutor.capabilities.ima.binding`).
Unlike the Obsidian and subagent capabilities this is a *plain*
:class:`~deeptutor.capabilities.protocol.LoopCapability`, not a
:class:`~deeptutor.capabilities.protocol.KnowledgeCapability`: it **adds** its
tools to chat's normal surface instead of replacing it. That difference is not
stylistic — it follows from what the KB actually is. An Obsidian vault has no
index at all, so ``rag`` is useless against it and the capability must own the
turn. An IMA library is genuinely searchable over HTTP: the ``ima`` RAG provider
answers ``rag`` calls for it. Retrieval therefore stays where it belongs, and
these tools add only what retrieval cannot do — enumerate the library, read a
whole source, search notes by recency, and write back.
Keeping the surface additive also means a turn that attaches an IMA library
alongside other knowledge bases (or that needs web search) does not lose those
abilities, which an exclusive capability would strip.
"""
from __future__ import annotations
from importlib import resources
from typing import Any
from deeptutor.capabilities.ima.binding import ima_bindings
from deeptutor.capabilities.ima.tools import BINDINGS_KWARG, IMA_TOOL_NAMES
from deeptutor.capabilities.protocol import PromptBlock
from deeptutor.core.context import UnifiedContext
class ImaCapability:
"""Turn-scoped integration for connected Tencent IMA knowledge bases."""
name = "ima"
owned_tools = IMA_TOOL_NAMES
def is_active(self, context: UnifiedContext) -> bool:
return bool(ima_bindings(context))
def system_block(
self,
context: UnifiedContext,
*,
language: str,
prompts: dict[str, Any],
) -> PromptBlock | None:
bindings = ima_bindings(context)
if not bindings:
return None
override = _prompt_text(prompts, ("ima", "system"))
content = override or _load_system_prompt(language)
names = (
"、".join(binding.name for binding in bindings)
if _is_zh(language)
else ", ".join(binding.name for binding in bindings)
)
return PromptBlock("ima", content.replace("{kb_names}", names))
def augment_kwargs(
self,
tool_name: str,
kwargs: dict[str, Any],
context: UnifiedContext,
) -> dict[str, Any]:
if tool_name not in IMA_TOOL_NAMES:
return kwargs
bindings = ima_bindings(context)
if not bindings:
return kwargs
# Server-owned: overwrite any model-supplied value so a turn can only
# reach the libraries the user actually attached. Credentials are not
# part of this payload — the tools load them per call.
updated = dict(kwargs)
updated[BINDINGS_KWARG] = bindings
return updated
def pre_loop_seed(self, context: UnifiedContext) -> str:
_ = context
return ""
def _prompt_text(prompts: dict[str, Any], path: tuple[str, ...]) -> str:
value: Any = prompts
for key in path:
if not isinstance(value, dict):
return ""
value = value.get(key)
return value if isinstance(value, str) and value else ""
def _is_zh(language: str) -> bool:
return str(language or "en").lower().startswith("zh")
def _load_system_prompt(language: str) -> str:
lang = "zh" if _is_zh(language) else "en"
prompt = resources.files(__package__).joinpath("prompts", lang, "system.md")
return prompt.read_text(encoding="utf-8").strip()
__all__ = ["ImaCapability"]