Files
DeepTutor/deeptutor/capabilities/protocol.py
T
Bingxi Zhao (Frank) bd80a4d28a release: v1.5.17
Partners stop being a deployment-wide admin resource and become
per-user: three nested rights (manage / use / neither) replace the
blanket admin gate on the router, each member can build and own their
own, and every person's conversations with a partner stay private to
their account. Grants keep working and now offer only admin-managed
partners. Channel runtime state moves from one directory per channel to
one per partner — copied, not moved, and only when its owner is
unambiguous — so two partners can finally run the same channel.

Around that: a knowledge base can track GitHub repositories and re-sync
their Markdown (#903); Antigravity CLI joins My Agents now that Gemini
CLI is retired (#828); personal WeChat signs in from a QR code drawn in
the browser instead of the server log (#951); a chat account can be
linked to a DeepTutor account with /link; containers re-apply the extras
they declare instead of losing them at compose down (#762); and
`deeptutor doctor` answers whether a workspace is ready to run (#959).

Reliability, largely from community PRs: embeddings survive a
rate-limited provider through key rotation, a retry budget and a global
throttle (#962); MinerU parsing and code execution work on Windows
(#844, #964); indexing fails bounded instead of hanging (#946); MinerU
page-layout blocks stop being indexed as content (#949); voice uploads
carry a MIME the transcription API accepts (#945); and reasoning effort
can be overridden per conversation (#641).

Release plumbing: notes in assets/releases/ver1-5-17.md, v1.5.13
archived, __version__ 1.5.17, README Releases row and all eleven
translations synced.

Two defects found by the release gate and fixed here:

- Six partner routes — the four channel-onboarding endpoints and both
  WeChat QR endpoints — kept the _auth-only mount they had inherited
  from the blanket admin gate, so any signed-in account could start a
  QR login for someone else's partner and write a bot token into its
  channel config. All six now declare _MANAGEABLE, and a sweep test
  asserts no {partner_id} route is left ungated.
- A configured api_key became `str | list[str]` when key rotation
  landed, but ten call sites pass it somewhere that takes one key; a
  list would have rendered into an Authorization header verbatim.
  keypool.primary_api_key reduces at each boundary.

Gates: pre-commit (all 15 hooks), ruff check + format, pytest
5276 passed, web 605 node tests, docs build.
2026-08-25 02:24:35 +08:00

156 lines
6.4 KiB
Python

"""Protocol shared by the chat loop and its loop capabilities."""
from __future__ import annotations
from dataclasses import dataclass
from typing import Any, Protocol
from deeptutor.core.context import UnifiedContext
# ── Keys a capability may write into ``context.metadata`` for the runtime ──────
#
# Turn metadata is a shared scratchpad, so the handful of keys the runtime
# itself reads back are named here, next to the interface a capability author is
# already reading. Anything else in there is private to whoever put it.
#: Set truthy during ``on_user_resume`` to end the turn without another LLM
#: round — the capability owns the final message from that point on. The user's
#: reply still reaches the transcript; only feeding it back to the model stops.
END_LOOP = "end_loop"
#: The body to publish as ``agent_output`` on the turn's CAPABILITY_COMPLETE
#: event.
AGENT_OUTPUT = "agent_output"
#: A dict of extras to publish alongside it. Only this sub-dict is forwarded —
#: never ``context.metadata`` whole, which holds live callables and the user's
#: own answers — so a capability states exactly what may leave the turn.
EVENT_METADATA = "event_metadata"
@dataclass(frozen=True, slots=True)
class PromptBlock:
"""One named prompt fragment contributed to the loop system prompt."""
name: str
content: str
class LoopCapability(Protocol):
"""Optional per-turn extension point for the chat agent loop.
A loop capability reuses the *full* chat tool surface — every built-in,
with the user's composer toggles respected exactly as in plain chat — and
adds its own :attr:`owned_tools` on top when active. It does not curate or
suppress the reused surface: a solve / mastery turn sees the same built-ins
a chat turn would, plus the capability's own tools.
The exception is the *knowledge* category (:class:`KnowledgeCapability`),
which sets :attr:`exclusive_tools` and replaces the surface instead of
augmenting it. Plain capabilities leave the attribute absent (read with a
``getattr(cap, "exclusive_tools", False)`` default) so this default — and
the augment-don't-suppress invariant above — stays true for them.
Optional async ``pre_loop`` hook
--------------------------------
A capability MAY define::
async def pre_loop(
self, context, stream, *, usage=None
) -> PromptBlock | None: ...
which the chat pipeline awaits **once, before the answer loop's first LLM
call**, when the capability is active. Its returned block is folded into
the loop's user-message seed (alongside the KB seed) so the answer loop
treats it as grounding context for the turn. Use it for a bounded
pre-pass that produces context the loop should have up front — e.g.
:class:`~deeptutor.capabilities.explore_context.ExploreContextCapability`
briefs the turn's attached sources objectively before the model answers.
This hook is **optional** and not part of the required structural surface:
the pipeline reads it with a ``getattr(cap, "pre_loop", None)`` default
(mirroring :attr:`exclusive_tools`), so plain capabilities that omit it are
unaffected. ``usage`` is the turn's token tracker, passed so a pre-pass can
fold its own LLM cost into the turn total.
Capabilities that own a durable user interaction MAY also define async
``on_user_pause(context, ask_user)`` and ``on_user_resume(context,
ask_user, *, reply_text, answers)`` hooks. The pipeline invokes them on
the two sides of an ``ask_user`` wait so state can be committed before a
disconnect or another LLM round.
A capability whose tools can repoint the turn at a different target MAY
declare those tool names as ``rebinding_tools: tuple[str, ...]`` (read with
a ``getattr`` default, like ``pre_loop``). The dispatcher runs them before
the round's other calls and re-binds those calls afterwards, so a switch
and a write issued in the same round cannot land on different targets.
"""
name: str
# Tools this capability registers and contributes when active (added on top
# of chat's standard composition). Static — so the settings UI can group
# them under their owning capability without a turn context.
owned_tools: tuple[str, ...]
def is_active(self, context: UnifiedContext) -> bool:
"""Whether this capability participates in the current turn."""
def system_block(
self,
context: UnifiedContext,
*,
language: str,
prompts: dict[str, Any],
) -> PromptBlock | None:
"""Optional system prompt block contributed by the capability."""
def augment_kwargs(
self,
tool_name: str,
kwargs: dict[str, Any],
context: UnifiedContext,
) -> dict[str, Any]:
"""Inject server-owned private kwargs for this capability's tools."""
def pre_loop_seed(self, context: UnifiedContext) -> str:
"""Optional text appended to the initial user message seed."""
class KnowledgeCapability:
"""Base for capabilities bound to an agentic knowledge base.
Unlike a plain :class:`LoopCapability` (which augments chat's full tool
surface), a knowledge capability *owns the turn*: when active it replaces
the surface with its own :attr:`owned_tools` plus the ``ask_user`` floor —
no chat built-ins, no user composer toggles. Its retrieval/authoring is the
model reasoning over the KB through these tools, not a fixed pipeline.
The exclusivity is decided by **category membership**, not a per-instance
knob: subclassing this sets :attr:`exclusive_tools`. Subclasses still
satisfy :class:`LoopCapability` structurally (``name`` / ``owned_tools`` /
``is_active`` / ``system_block`` / ``augment_kwargs`` / ``pre_loop_seed``).
"""
exclusive_tools: bool = True
def owned_kbs(self, context: UnifiedContext) -> set[str]:
"""Selected KB refs this capability consumes through its own tools.
Returned refs are excluded from the ``rag`` surface so that co-selected
KBs the capability does NOT own (e.g. plain LlamaIndex KBs alongside an
Obsidian vault) stay reachable via ``rag`` instead of being silently
dropped when the capability owns the turn (issue #650). Default: none.
"""
_ = context
return set()
__all__ = [
"AGENT_OUTPUT",
"END_LOOP",
"EVENT_METADATA",
"KnowledgeCapability",
"LoopCapability",
"PromptBlock",
]