zett_agent.extensions.tool_guidelines 源代码

"""Tool prompt guidance exposed as an Agent extension."""

from __future__ import annotations

from collections.abc import AsyncIterator, Sequence
from dataclasses import replace

from .._compat import StrEnum, aclosing_stream
from ..agent import AgentRunContext
from ..messages import SystemMessage
from ..model import ModelEvent, ModelRequest, ToolDefinition
from ..tools.base import render_tool_description, render_tool_guidance
from .base import AgentExtension, ModelRequestNext


[文档] class ToolGuidancePlacement(StrEnum): """Where a tool's snippet and guidelines are shown to the model.""" #: Two sections of the system instructions: ``# Tool snippets`` and ``# Tool guidelines``. SYSTEM_PROMPT = "system_prompt" #: Appended to each tool's own ``description`` as one line of plain prose: #: snippet call shapes introduced by ``You can call it as``, followed by #: guideline sentences, with no headings or bullets. A multi-line snippet lists #: every shape, joined by ``or``. When the description already spans lines, the #: guidance starts on a new line instead of continuing the last one. DESCRIPTION = "description"
[文档] class ToolGuidelinesExtension(AgentExtension): """Show registered tool snippets and guidelines to the model. By default the guidance becomes system instructions, grouped so the model can scan every call shape first. Set ``placement`` to :attr:`ToolGuidancePlacement.DESCRIPTION` to attach each tool's snippet and guidelines to that tool's own description instead; the system prompt then carries nothing. There the guidance stays one line of prose: snippet lines appear as bare call shapes and guidelines as sentences, with no headings or bullets. A description that already spans lines, such as the tool-search catalog, keeps its own formatting and gets the guidance on a new line. Use it for endpoints and gateways that surface only tool definitions, or when the guidance should travel with the tool wherever the definition goes. The blocks other extensions add to a request follow both choices: Plan Mode and a registered :class:`~zett_agent.jit_harness.extension.JITHarnessExtension` render the parts enabled here, and leave tool guidance out entirely in this placement, so no rule is sent twice. Tools marked ``deferred=True`` are skipped in both modes: their definitions reach the model through tool search, so rendering their guidance would announce a tool that is not callable yet. Tool search appends their guidelines to the definition it returns. Args: placement: Where the guidance goes; the system prompt by default. snippets: Include each tool's ``Snippet`` section. guidelines: Include each tool's ``Guidelines`` section. Placement into descriptions renders only the parts enabled here. returns: Say what each tool returns, in one sentence after its snippet, as JSON Schema. Off by default: most tools return text the model reads directly, and the schema costs a sentence per tool in every request. A tool whose docstring has a ``Returns`` section also ends with that prose, so the model reads what the result means and not only its shape. Examples: Keep guidance in the tool definitions:: agent = await Agent.create( model, extensions=[ CodingExtension(), ToolGuidelinesExtension(placement=ToolGuidancePlacement.DESCRIPTION), ], ) .. note:: Description placement edits only the request sent to the provider. The registered tools, stored history, and the persisted system messages are unchanged, so switching placement between runs never rewrites a session. """ def __init__( self, *, placement: ToolGuidancePlacement = ToolGuidancePlacement.SYSTEM_PROMPT, snippets: bool = True, guidelines: bool = True, returns: bool = False, ) -> None: self.placement = ToolGuidancePlacement(placement) self.snippets = snippets self.guidelines = guidelines self.returns = returns @property def in_system_prompt(self) -> bool: """Whether this extension writes guidance into the system instructions.""" return self.placement is ToolGuidancePlacement.SYSTEM_PROMPT @property def in_descriptions(self) -> bool: """Whether this extension writes guidance into each tool's own description.""" return self.placement is ToolGuidancePlacement.DESCRIPTION
[文档] @classmethod def registered(cls, extensions: Sequence[AgentExtension]) -> ToolGuidelinesExtension | None: """Return the guidance extension whose placement governs a request, or None. Extensions that render guidance into a block of their own ask this, and follow :attr:`in_system_prompt`, :attr:`in_descriptions` and the three switches, so the request carries each rule once. Two instances may be registered under different names, because a name is unique while a type is not; then the first in ``extensions`` decides, since one placement has to govern a request. The other still places its guidance where its own placement says, so duplicate rules are the cost of that configuration. """ for extension in extensions: if isinstance(extension, cls): return extension return None
[文档] async def on_state(self, context: AgentRunContext) -> None: """Place guidance after existing system instructions and before dialogue.""" if not self.in_system_prompt: return state = context.state visible = tuple(candidate for candidate in context.tools.values() if not candidate.deferred) message = SystemMessage( content=render_tool_guidance( visible, snippets=self.snippets, guidelines=self.guidelines, returns=self.returns ), attributes={"tool_guidelines": True}, ) if message.content and message not in state.messages: instructions = [message for message in state.messages if isinstance(message, SystemMessage)] context.add_message(message, index=len(instructions))
[文档] async def on_model_request( self, context: AgentRunContext, request: ModelRequest, call_next: ModelRequestNext, ) -> AsyncIterator[ModelEvent]: """Fold guidance into tool descriptions when that placement is selected.""" if not self.in_system_prompt: request = replace( request, tools=tuple(self._described(context, definition) for definition in request.tools) ) async with aclosing_stream(call_next(request)) as events: async for event in events: yield event
def _described(self, context: AgentRunContext, definition: ToolDefinition) -> ToolDefinition: """Return one tool definition whose description includes its guidance.""" registered = context.tools.get(definition.name) if registered is None or registered.deferred: return definition # Build on the description the request already carries: another # extension (tool search adds a catalog to its own tool) may have # appended to it, and replacing it would discard that text. description = render_tool_description( registered, snippet=self.snippets, guidelines=self.guidelines, returns=self.returns, base=definition.description, ) return replace(definition, description=description)