Files
DeepTutor/tests/knowledge/test_ima_kb.py
Bingxi Zhao (Frank) e1c669f02a feat(knowledge): add Tencent IMA as a connected knowledge engine
IMA (ima.qq.com) is where a lot of users already keep their library, and
its OpenAPI exposes retrieval over it. Adding it as a sixth engine follows
the `lightrag-server` shape exactly: a KB bound to `ima` is a connection
pointer, not an index. Nothing is copied and nothing is stored locally —
documents are added in IMA, retrieval calls IMA, and DeepTutor's chat loop
still writes the answer.

The wire is plain HTTP POST to `/openapi/wiki/v1/<method>` with two auth
headers and a `{code,msg,data}` envelope; `search_knowledge` returns
cursor-paginated items carrying a `highlight_content` snippet. Only the two
read-only calls we need are wrapped, and pagination is bounded so a server
that never reports the end cannot spin.

Credentials (`client_id` + `api_key`) and the library id live per-KB in
`kb_config.json` rather than as one global account key. IMA credentials
identify a person, so per-KB means each user on a shared deployment
retrieves through their own account, and connecting stays self-contained
with no separate settings surface to fill in first. Both metadata paths
already work off an allowlist, so the credentials cannot leak into
surfaced metadata; only the library id is exposed, for the UI to show.

`connect-ima` re-probes server-side and never trusts the client's verdict,
matching the LightRAG-server route. IMA KBs are registered as a connected
type — no folder under `base_dir`, no embedding reconcile, deletion drops
only our pointer — and are reported as non-enumerable in the manifest,
since claiming "0 documents" for a library held behind an API would be a
lie.

Backend only; the connect flow in the knowledge center UI is not wired up
yet.
2026-07-29 00:38:10 +08:00

123 lines
4.0 KiB
Python

"""Manager handling of Tencent IMA KBs (``type: ima`` pointers).
An IMA KB is a connection pointer to a library the user keeps in IMA: no on-disk
folder under ``base_dir``, no local index, and deleting it must only drop our
pointer (never touch the user's IMA library). The stored credentials must never
leak into surfaced metadata. Mirrors the LightRAG-server pointer guarantees.
"""
from __future__ import annotations
import pytest
from deeptutor.knowledge.kb_types import CONNECTED_KB_TYPES, IMA_KB_TYPE
from deeptutor.knowledge.manager import KnowledgeBaseManager
def _register(manager: KnowledgeBaseManager, name: str = "IMA") -> dict:
return manager.register_ima_kb(name, "cid", "secret", "kb-1")
def test_ima_is_a_connected_type() -> None:
assert IMA_KB_TYPE in CONNECTED_KB_TYPES
def test_register_writes_pointer(tmp_path) -> None:
manager = KnowledgeBaseManager(base_dir=str(tmp_path / "kbs"))
entry = manager.register_ima_kb("IMA", " cid ", " secret ", " kb-1 ")
assert entry["type"] == IMA_KB_TYPE
assert entry["rag_provider"] == "ima"
assert entry["client_id"] == "cid" # trimmed
assert entry["api_key"] == "secret"
assert entry["knowledge_base_id"] == "kb-1"
assert entry["status"] == "ready"
assert entry["needs_reindex"] is False
# No KB folder is created under base_dir.
assert not (manager.base_dir / "IMA").exists()
def test_register_keeps_the_probed_description(tmp_path) -> None:
manager = KnowledgeBaseManager(base_dir=str(tmp_path / "kbs"))
entry = manager.register_ima_kb("IMA", "cid", "secret", "kb-1", description="My notes")
assert entry["description"] == "My notes"
@pytest.mark.parametrize(
"args",
[
("", "cid", "secret", "kb-1"),
("IMA", "", "secret", "kb-1"),
("IMA", "cid", "", "kb-1"),
("IMA", "cid", "secret", ""),
],
)
def test_register_rejects_missing_fields(tmp_path, args) -> None:
manager = KnowledgeBaseManager(base_dir=str(tmp_path / "kbs"))
with pytest.raises(ValueError):
manager.register_ima_kb(*args)
def test_register_rejects_duplicate_name(tmp_path) -> None:
manager = KnowledgeBaseManager(base_dir=str(tmp_path / "kbs"))
_register(manager)
with pytest.raises(ValueError, match="already exists"):
_register(manager)
def test_get_metadata_hides_credentials(tmp_path) -> None:
manager = KnowledgeBaseManager(base_dir=str(tmp_path / "kbs"))
_register(manager)
meta = manager.get_metadata("IMA")
assert meta["type"] == IMA_KB_TYPE
assert meta["knowledge_base_id"] == "kb-1"
assert "api_key" not in meta
assert "client_id" not in meta
def test_get_info_hides_credentials(tmp_path) -> None:
manager = KnowledgeBaseManager(base_dir=str(tmp_path / "kbs"))
_register(manager)
metadata = manager.get_info("IMA")["metadata"]
assert metadata["knowledge_base_id"] == "kb-1"
assert "api_key" not in metadata
assert "client_id" not in metadata
def test_delete_only_drops_the_pointer(tmp_path) -> None:
manager = KnowledgeBaseManager(base_dir=str(tmp_path / "kbs"))
_register(manager)
assert manager.delete_knowledge_base("IMA", confirm=True) is True
assert "IMA" not in manager.list_knowledge_bases()
def test_has_no_local_document_root(tmp_path) -> None:
# Nothing of an IMA KB lives on this machine, so there is no folder to walk.
from deeptutor.knowledge.manifest import document_root
manager = KnowledgeBaseManager(base_dir=str(tmp_path / "kbs"))
entry = _register(manager)
assert document_root(manager.base_dir / "IMA", entry) is None
def test_documents_are_reported_as_non_enumerable(tmp_path) -> None:
# IMA holds the documents behind its API; reporting "0 documents" would lie.
from deeptutor.knowledge.manifest import UNAVAILABLE_REMOTE, build_manifest
manager = KnowledgeBaseManager(base_dir=str(tmp_path / "kbs"))
entry = _register(manager)
manifest = build_manifest(name="IMA", kb_dir=manager.base_dir / "IMA", entry=entry)
assert manifest.unavailable == UNAVAILABLE_REMOTE
assert manifest.total == 0