zett_agent.tools.base 源代码

"""Typed tool definitions, docstring parsing, and prompt guidance."""

import asyncio
import inspect
import json
from collections.abc import Awaitable, Callable, Collection, Mapping, Sequence
from contextvars import ContextVar, Token
from dataclasses import dataclass
from functools import lru_cache
from typing import Any, ParamSpec, Protocol, TypeVar, cast, get_type_hints, overload

from .._compat import StrEnum, TypeAliasType
from ..model import ToolDefinition
from ..sync_runtime import SyncMethodsMixin

_CURRENT_TOOL_CALL_ID: ContextVar[str | None] = ContextVar("zett_agent_tool_call_id", default=None)
# A composing execution environment may supply non-pooling workers so a sync
# parent waiting on another sync tool cannot exhaust the default thread pool.
_SYNC_TOOL_RUNNER: ContextVar[Callable[..., Awaitable[Any]] | None] = ContextVar("zett_sync_tool_runner", default=None)

#: Raw result of one local tool invocation, before the runtime turns it into a
#: ``ToolMessage``. ``TextContent`` and ``ImageContent`` parts (or a list of
#: them) become multimodal content, strings pass through unchanged, and every
#: other value is serialized by Pydantic, which covers JSON values, Pydantic
#: models, and dataclasses. The runtime never inspects the value itself, so this
#: alias stays wide instead of claiming an exhaustive union.
ToolResult = TypeAliasType("ToolResult", Any)

_DecoratedParameters = ParamSpec("_DecoratedParameters")
_DecoratedResult = TypeVar("_DecoratedResult")


@lru_cache(maxsize=1)
def _result_serializer() -> Any:
    """Build the permissive JSON adapter once, on the first serialized result.

    Pydantic is imported here rather than at module scope so importing the tool
    framework does not pay for it; only serializing a non-string result does.
    """
    from pydantic import TypeAdapter

    return TypeAdapter(Any)


def _bind_tool_call(call_id: str) -> Token[str | None]:
    """Bind one invocation ID to its isolated asyncio task context."""
    return _CURRENT_TOOL_CALL_ID.set(call_id)


def _reset_tool_call(token: Token[str | None]) -> None:
    """Restore the task's previous invocation identity."""
    _CURRENT_TOOL_CALL_ID.reset(token)


def current_tool_call_id() -> str | None:
    """Return the tool call currently executing in this task, when present."""
    return _CURRENT_TOOL_CALL_ID.get()


[文档] class ToolExecutionMode(StrEnum): """Local scheduling policy for one Agent tool.""" #: Execute alone and only after every parallel tool in the response finishes. SERIAL = "serial" #: The handler may run concurrently with other parallel handlers. PARALLEL = "parallel"
[文档] @dataclass(slots=True) class AgentTool(SyncMethodsMixin): """A tool is a name, an input schema, and an async callable.""" #: Unique name exposed to the model and used for dispatch. name: str #: Natural-language purpose used by the model to select this tool. description: str #: JSON Schema describing this tool's keyword arguments. parameters: dict[str, Any] #: Async handler that validates arguments and performs the local operation. handler: Callable[..., Awaitable[ToolResult]] #: Model-facing rules rendered beside this tool in prompt guidance. Tools #: that share one rule through another channel, such as a server-level system #: message, may leave this empty so the rule is not repeated per tool. guidelines: tuple[str, ...] #: Optional invocation examples rendered into prompt guidance. snippet: str = "" #: JSON Schema describing this tool's return value, when its function has a return #: annotation that can be described. ``None`` means the tool says nothing about its #: result. The value is never validated against a result; it only tells the model what #: to expect, and only when a renderer asks for it. return_schema: dict[str, Any] | None = None #: Whether this local handler runs serially or may run in parallel. execution_mode: ToolExecutionMode = ToolExecutionMode.PARALLEL #: Whether Responses API providers may defer loading this tool's schema #: through tool search. Other provider protocols ignore this flag. deferred: bool = False #: Whether this tool answers client-side tool search for one request. #: #: A search tool is internal: the model never calls it directly. The #: Responses adapter declares ``tool_search`` with ``execution: "client"`` #: and routes every search request to this handler, whose result decides #: which tools the model may load. Other provider protocols reject it. local_tool_search: bool = False #: Prose describing the return value, taken from the docstring's ``Returns`` section. #: Empty means the tool says nothing about its result in words. Like #: :attr:`return_schema` it is shown only to renderers that ask for return information; #: it ends the tool's description, after its snippet and guidelines. It stays last #: among the fields so a caller that passes the others positionally keeps working. returns_description: str = "" def __post_init__(self) -> None: if not self.description.strip(): raise ValueError("A tool description cannot be empty") if any(not guideline.strip() for guideline in self.guidelines): raise ValueError("Tool guidelines cannot be blank") if not isinstance(self.execution_mode, ToolExecutionMode): raise ValueError("execution_mode must be a ToolExecutionMode") if not isinstance(self.deferred, bool): raise ValueError("deferred must be a boolean") if not isinstance(self.local_tool_search, bool): raise ValueError("local_tool_search must be a boolean") if self.return_schema is not None and not isinstance(self.return_schema, dict): raise ValueError("return_schema must be a JSON schema object or None") if not isinstance(self.returns_description, str): raise ValueError("returns_description must be a string") if self.local_tool_search and self.deferred: raise ValueError("A local tool search tool cannot also defer its own schema") @property def definition(self) -> ToolDefinition: return ToolDefinition( self.name, self.description, self.parameters, deferred=self.deferred, local_tool_search=self.local_tool_search, )
[文档] async def __call__(self, arguments: Mapping[str, Any]) -> ToolResult: """Execute with a mapping of model-provided keyword arguments. Args: arguments: Input object validated by the decorated function's schema. Returns: The function's typed result before JSON serialization. Examples: Invoke a decorated tool from Python:: result = await read_file({"file_path": "README.md"}) Note: Decorated synchronous functions run in a worker thread; async functions are awaited directly. A manually supplied handler owns its validation policy. """ return await self.handler(**arguments)
[文档] def serialize_result(self, value: ToolResult) -> str: """Convert Pydantic models, dataclasses, and plain values into JSON. Strings pass through unchanged so plain-text tool results reach the model without JSON quoting. """ if isinstance(value, str): return value return json.dumps(_result_serializer().dump_python(value, mode="json"), ensure_ascii=False)
class _ToolDecorator(Protocol): """Decorator returned by configured ``@tool(...)`` usage.""" def __call__(self, function: Callable[_DecoratedParameters, _DecoratedResult], /) -> AgentTool: ... @dataclass(frozen=True, slots=True) class _ToolDocumentation: """Structured metadata parsed from one tool function docstring.""" description: str parameter_descriptions: dict[str, str] snippet: str guidelines: tuple[str, ...] returns_description: str def _parse_tool_docstring(function: Callable[..., Any]) -> _ToolDocumentation: """Parse Description, Args, Returns, Snippet, and Guidelines from a Google-style docstring. A recognized section runs until the next left-margin heading, which may state a first line after its colon (``Returns: the sum``). An unrecognized one, such as ``Raises:`` or ``Examples:``, ends the section before it, with or without text after its colon, so its lines never reach the model as part of a description or an argument. """ document = inspect.getdoc(function) or "" lines = document.splitlines() description_lines = [] for line in lines: if not line.strip(): break description_lines.append(line.strip()) description = " ".join(description_lines) if not description: raise ValueError("A tool needs a docstring description") sections: dict[str, list[str]] = {"args": [], "snippet": [], "guidelines": [], "returns": []} aliases = {"arguments": "args", "parameters": "args", "return": "returns"} current: str | None = None for line in lines[len(description_lines) :]: stripped = line.strip() heading, colon, inline = stripped.partition(":") key = aliases.get(heading.strip().lower(), heading.strip().lower()) # A heading sits at the left margin; a bullet is content even when it ends with a # colon. ``Returns: the sum`` states a heading and its first line together. An # unrecognized heading (``Raises:``, ``Examples:``, ``Note:``) ends the section # before it, with or without text after its colon: otherwise ``Args:`` would # invent a parameter named after the heading, and prose would join a description # it does not belong to. if not line[:1].isspace() and colon and not stripped.startswith("-"): if key in sections: current = key if inline.strip(): sections[key].append(inline.strip()) else: current = None continue if current is not None: sections[current].append(line) parameter_descriptions: dict[str, str] = {} current_parameter: str | None = None for line in sections["args"]: stripped = line.strip() if not stripped: continue if ":" in stripped: parameter, description_part = stripped.split(":", 1) current_parameter = parameter.split("(", 1)[0].strip() parameter_descriptions[current_parameter] = description_part.strip() elif current_parameter is not None: parameter_descriptions[current_parameter] = ( f"{parameter_descriptions[current_parameter]} {stripped}".strip() ) snippet = "\n".join(line.strip() for line in sections["snippet"] if line.strip()) guidelines = tuple(line.strip().removeprefix("-").strip() for line in sections["guidelines"] if line.strip()) # A Returns section is prose about the result, not a parameter: it is kept on one # line so a renderer can end a tool's description with it. returns_description = " ".join(line.strip() for line in sections["returns"] if line.strip()) return _ToolDocumentation(description, parameter_descriptions, snippet, guidelines, returns_description)
[文档] def get_tool_snippet(function: Callable[..., Any]) -> str: """Extract the ``Snippet`` section from a function docstring. Args: function: Undecorated Python function whose docstring follows the tool documentation format. Returns: The normalized snippet, or an empty string when the section is absent. """ return _parse_tool_docstring(function).snippet
[文档] def get_tool_guidelines(function: Callable[..., Any]) -> tuple[str, ...]: """Extract immutable ``Guidelines`` entries from a function docstring. Args: function: Undecorated Python function whose docstring follows the tool documentation format. """ return _parse_tool_docstring(function).guidelines
def _inline_schema_references(schema: dict[str, Any]) -> dict[str, Any]: """Expand every local ``$ref`` so a tool schema stands on its own. Pydantic publishes nested argument models under ``$defs`` and points at them with ``$ref``. Some provider gateways reject that indirection, so tool schemas ship the expanded form instead. A recursive model cannot be expanded at all and is rejected with an explicit error. """ raw_definitions = schema.get("$defs") definitions = raw_definitions if isinstance(raw_definitions, Mapping) else {} prefix = "#/$defs/" def expand(node: Any, stack: tuple[str, ...]) -> Any: if isinstance(node, list): return [expand(item, stack) for item in node] if not isinstance(node, Mapping): return node reference = node.get("$ref") # Pydantic adds an OpenAPI ``discriminator`` whose ``mapping`` values are # ``$ref`` strings. Those cannot survive inlining, and the ``oneOf`` # members keep their own ``const`` constraints, so the keyword is dropped. siblings = {key: value for key, value in node.items() if key not in {"$ref", "$defs", "discriminator"}} if reference is None: return {key: expand(value, stack) for key, value in siblings.items()} if not isinstance(reference, str) or not reference.startswith(prefix): raise ValueError(f"Tool arguments use an unsupported schema reference: {reference!r}") name = reference[len(prefix) :] if name in stack: raise ValueError( f"Tool arguments cannot use the recursive model {name!r}: a self-referencing schema " "cannot be expanded for providers that reject $ref" ) target = definitions.get(name) if not isinstance(target, Mapping): raise ValueError(f"Tool arguments reference a missing schema definition: {name!r}") # Sibling keywords such as a field description override the definition. return {**expand(target, (*stack, name)), **expand(siblings, stack)} return expand({key: value for key, value in schema.items() if key != "$defs"}, ()) def _build_input_schema( function: Callable[..., Any], documentation: _ToolDocumentation ) -> tuple[type[Any], tuple[str, ...], dict[str, Any], dict[str, Any]]: """Build the validator and JSON schema for one typed function. The resolved type hints come back with the schema, so the return annotation can be described from the same lookup instead of a second ``get_type_hints`` call. That call resolves every annotation at once, so a broken one fails here, with the arguments' error, whoever asks about the return value. """ from pydantic import ConfigDict, create_model hints = get_type_hints(function, include_extras=True) fields: dict[str, Any] = {} # name -> (annotation, default), as ``create_model`` takes them for parameter in inspect.signature(function).parameters.values(): if parameter.kind not in (inspect.Parameter.POSITIONAL_OR_KEYWORD, inspect.Parameter.KEYWORD_ONLY): raise ValueError("Tools accept only named parameters") if parameter.name not in hints: raise ValueError(f"Missing annotation for {parameter.name}") default = ... if parameter.default is inspect.Parameter.empty else parameter.default fields[parameter.name] = (hints[parameter.name], default) # Build the validator in the tool's own module so a forward reference in its # annotations resolves there instead of inside this framework module. inputs = create_model( "ToolInput", __config__=ConfigDict(extra="forbid"), __module__=getattr(function, "__module__", __name__), **fields, ) parameters = _strip_schema_keywords(_inline_schema_references(inputs.model_json_schema()), _TITLE_KEYWORDS) unknown_parameters = documentation.parameter_descriptions.keys() - fields.keys() if unknown_parameters: unknown = ", ".join(sorted(unknown_parameters)) raise ValueError(f"Docstring Args contains unknown parameters: {unknown}") for described, description in documentation.parameter_descriptions.items(): parameters["properties"][described]["description"] = description return inputs, tuple(fields), parameters, hints def _resolve_guidelines(documentation: _ToolDocumentation, configured: str | Sequence[str] | None) -> tuple[str, ...]: """Resolve explicit decorator guidance before docstring guidance.""" match configured: case None: return documentation.guidelines case str(): return (configured,) case _: return tuple(configured) def _build_agent_tool( function: Callable[..., Any], *, name: str | None, snippet: str | None, guidelines: str | Sequence[str] | None, execution_mode: ToolExecutionMode, deferred: bool, local_tool_search: bool, ) -> AgentTool: """Create an AgentTool from one function and decorator overrides.""" documentation = _parse_tool_docstring(function) inputs, field_names, parameters, hints = _build_input_schema(function, documentation) async def invoke(**arguments: Any) -> Any: parsed = inputs.model_validate(arguments) values = {field: getattr(parsed, field) for field in field_names} if inspect.iscoroutinefunction(function): return await function(**values) if (runner := _SYNC_TOOL_RUNNER.get()) is not None: return await runner(function, **values) return await asyncio.to_thread(function, **values) # The handler stands in for the user's function, so it reports the module # that function was defined in. Loaders use this to tell a tool a package # defines from one it merely imports. invoke.__module__ = getattr(function, "__module__", invoke.__module__) resolved_guidelines = _resolve_guidelines(documentation, guidelines) if not resolved_guidelines: raise ValueError("A tool needs at least one non-empty guideline") return AgentTool( name=name or function.__name__, description=documentation.description, parameters=parameters, handler=invoke, guidelines=resolved_guidelines, snippet=snippet if snippet is not None else documentation.snippet, return_schema=_build_return_schema(hints), returns_description=documentation.returns_description, execution_mode=execution_mode, deferred=deferred, local_tool_search=local_tool_search, ) def _build_return_schema(hints: Mapping[str, Any]) -> dict[str, Any] | None: """Describe a tool's return value as JSON Schema, or None when it cannot be described. The hint is optional metadata, and nothing validates a result against it, so every way of failing to describe it ends in None rather than in an error that would keep a working tool from being registered: no return annotation, an annotation Pydantic cannot build a schema for, a recursive model the inliner refuses, or a schema with nothing in it (``-> object`` and ``-> Any`` describe nothing). Title annotations are dropped because they cost prompt tokens on every request and say only the class name; a field named ``title`` is a name and stays. """ hint = hints.get("return") if hint is None: return None from pydantic import TypeAdapter try: schema = TypeAdapter(hint).json_schema() except Exception: # an annotation pydantic cannot describe is simply no hint return None if not schema: return None try: return _strip_schema_keywords(_inline_schema_references(schema), _TITLE_KEYWORDS) except Exception: # a recursive model cannot be inlined, and the return is optional return None # JSON Schema applicator keywords whose value holds sub-schemas: one, a list of them, # or a map of them keyed by names. The walk decides how to descend by the value's own # shape, so a keyword stays in one set no matter whether a draft lets it hold a single # schema, a list, or both. The keys of a name map are property, definition, or schema # names, so the walk recurses into their values instead of filtering their keys. # ``dependencies`` also accepts a list of property names, which ``clean`` passes through, # and ``dependentRequired`` maps names to names, so it holds no annotations at all. _NAME_MAPS = frozenset({"properties", "patternProperties", "dependentSchemas", "$defs", "definitions", "dependencies"}) _SUBSCHEMA_KEYWORDS = frozenset( { "items", "additionalItems", "unevaluatedItems", "additionalProperties", "unevaluatedProperties", "not", "if", "then", "else", "contains", "propertyNames", "contentSchema", "anyOf", "oneOf", "allOf", "prefixItems", } ) # Pydantic titles every model and property with the name the property already has as # its key, so the annotation repeats the contract instead of adding to it. _TITLE_KEYWORDS = frozenset({"title"}) def _strip_schema_keywords(schema: Mapping[str, Any], keywords: Collection[str]) -> dict[str, Any]: """Drop annotation keywords from a schema while keeping every name it declares. Pydantic titles every model and property with its own name, and a model sees the property name as the argument's key already, so ``title`` only costs prompt tokens. A property *called* ``title`` is a name, not an annotation: it survives with its own entry, and only the ``title`` annotation beside it goes. The caller owns ``keywords`` because MCP schemas also carry a ``$schema`` URL to drop. Schema nodes are copied; data values such as ``examples``, ``enum``, and ``required`` are shared with the input. """ def clean(node: Any) -> Any: if not isinstance(node, Mapping): return node result: dict[str, Any] = {} for key, value in node.items(): if key in keywords: continue if key in _NAME_MAPS and isinstance(value, Mapping): result[key] = {name: clean(child) for name, child in value.items()} elif key in _SUBSCHEMA_KEYWORDS: # One keyword holds a single schema in some drafts and a list of them in # others, so the value's shape picks the descent. result[key] = [clean(item) for item in value] if isinstance(value, list) else clean(value) else: result[key] = value return result return cast(dict[str, Any], clean(schema)) @overload def tool( function: Callable[..., Any], *, name: str | None = None, snippet: str | None = None, guidelines: str | Sequence[str] | None = None, execution_mode: ToolExecutionMode = ToolExecutionMode.PARALLEL, deferred: bool = False, local_tool_search: bool = False, ) -> AgentTool: ... @overload def tool( function: None = None, *, name: str | None = None, snippet: str | None = None, guidelines: str | Sequence[str] | None = None, execution_mode: ToolExecutionMode = ToolExecutionMode.PARALLEL, deferred: bool = False, local_tool_search: bool = False, ) -> _ToolDecorator: ...
[文档] def tool( function: Callable[..., Any] | None = None, *, name: str | None = None, snippet: str | None = None, guidelines: str | Sequence[str] | None = None, execution_mode: ToolExecutionMode = ToolExecutionMode.PARALLEL, deferred: bool = False, local_tool_search: bool = False, ) -> AgentTool | _ToolDecorator: """Turn a typed function and its structured docstring into an AgentTool. Args: function: Typed function for bare @tool use; omitted for @tool(...). name: Optional public name overriding the Python function name. snippet: Optional usage snippet overriding the docstring section. guidelines: Optional model guidance overriding docstring Guidelines. execution_mode: Whether the local handler must run serially or may run concurrently with other parallel handlers. The default is parallel; use SERIAL for handlers that require exclusive ordered execution. deferred: Whether Responses API providers may load this tool's schema through tool search instead of injecting it up front. Other provider protocols ignore this flag. Tool search requires a compatible model such as gpt-5.4 or later. local_tool_search: Whether this tool answers client-side tool search. The Responses adapter declares ``tool_search`` with ``execution: "client"`` and hands every search query to this handler; it must return ``list[FunctionToolParam]`` (OpenAI's Responses function definition), which becomes the ``tool_search_output`` the model loads from. The model never calls this tool as a function, and other provider protocols reject the flag. Returns: An AgentTool for direct decoration, or a decorator when configured first. Raises: ValueError: If annotations, guidance, or documented parameter names are invalid. Note: A normal exception raised by the decorated handler is caught by the Agent instead of ending the run. The Agent creates an unsuccessful ``ToolMessage`` whose ``content`` is ``str(exception)``; that string is what the next model request sees. The original exception object remains available separately on the ``TOOL_FAILED`` event and after-tool hooks. Cancellation and other ``BaseException`` subclasses are not converted. Examples: Register a typed function:: @tool def read_file(path: str) -> str: \"\"\"Read a text file. Args: path: File path relative to the workspace. Snippet: read_file(path=\"README.md\") Guidelines: - Read a file before changing it. Returns: The file's text, decoded as UTF-8. \"\"\" return path """ def decorate(target: Callable[..., Any]) -> AgentTool: return _build_agent_tool( target, name=name, snippet=snippet, guidelines=guidelines, execution_mode=execution_mode, deferred=deferred, local_tool_search=local_tool_search, ) return decorate(function) if function is not None else decorate
[文档] def render_tool_guidance( tools: Sequence[AgentTool], *, snippets: bool = True, guidelines: bool = True, returns: bool = False ) -> str: """Render grouped tool metadata for insertion into a system prompt. Snippets are presented together so the model can scan the available call shapes first. Guidelines follow in one named group per tool, which avoids repeating a tool name for every rule. ``snippets`` and ``guidelines`` switch either section off. ``returns`` adds one sentence after each snippet saying what the tool returns, as JSON Schema, and ends that tool's entry with the prose from its docstring ``Returns`` section; it is off by default, because most tools return plain text the model already reads. Return information belongs to the snippets section, so ``snippets=False`` removes it. """ rendered_snippets = [ _render_tool_snippet(registered, returns=returns) for registered in tools if snippets and (registered.snippet or (returns and _describes_return(registered))) ] guideline_groups = [ f"## {registered.name}\n" + "\n".join(f"- {guideline}" for guideline in registered.guidelines) for registered in tools if guidelines and registered.guidelines ] sections = [] if rendered_snippets: sections.append("# Tool snippets\n" + "\n".join(rendered_snippets)) if guideline_groups: sections.append("# Tool guidelines\n" + "\n\n".join(guideline_groups)) return "\n\n".join(sections)
[文档] def render_tool_description( tool: AgentTool, *, snippet: bool = True, guidelines: bool = True, returns: bool = False, base: str | None = None ) -> str: """Return a tool description that carries its snippet and guidelines. Some endpoints show the model only a tool's ``description``, and a deferred tool reaches the model through tool search with no system prompt beside it. Folding the guidance into the description keeps it next to the tool in both cases. The result keeps the guidance on one line of plain prose: no section headings, no bullets, and no line breaks. Snippet lines follow the description as call shapes after ``You can call it as``, with every line of a multi-line snippet listed and joined by ``or``, and each guideline follows as a sentence, all separated by spaces. A guideline gets a full stop unless it already ends with sentence punctuation; text ending in a CJK ideograph or kana gets ``。`` instead of ``.``. When ``returns`` is on, one more sentence says what the tool returns as JSON Schema, and the prose from its ``Returns`` section ends the description, after the guidelines. Like the schema sentence, that prose belongs to the snippet part, so ``snippet=False`` leaves it out. When ``base`` already spans lines, as a tool-search catalog does, the guidance starts on its own line instead of continuing the base's last one, and the base keeps its formatting. The result is the plain description when the tool has nothing to add, so it is safe to call for every tool. Args: tool: Tool whose metadata is rendered. snippet: Include the tool's snippet when it has one. guidelines: Include the tool's guidelines when it has any. returns: Add one sentence saying what the tool returns, after the snippet, and its docstring ``Returns`` prose at the end of the description. base: Text to put before the guidance, instead of ``tool.description``. Extensions that each add to a request's descriptions pass the text they received, so one does not overwrite what another appended. Examples: Render everything the model should know in one string:: text = render_tool_description(read_file) """ head = (tool.description if base is None else base).rstrip() show_returns = returns and snippet guidance_parts = [] if snippet and (tool.snippet or (show_returns and _describes_return(tool))): calls = [line.strip() for line in tool.snippet.splitlines() if line.strip()] if calls: guidance_parts.append(f"You can call it as {_or_list(calls)}.") if show_returns and tool.return_schema is not None: guidance_parts.append(f"It returns {_schema_text(tool.return_schema)}.") if guidelines and tool.guidelines: guidance_parts.append(" ".join(_as_sentence(guideline) for guideline in tool.guidelines)) if show_returns and tool.returns_description: # The prose about the result closes the whole description: a schema says what # shape it has, and this says what it means. guidance_parts.append(_as_sentence(tool.returns_description)) guidance = " ".join(guidance_parts) if not head: return guidance if not guidance: return head return ("\n" if "\n" in head else " ").join((head, guidance))
#: Characters that may follow a sentence's final punctuation and must not attract another stop. _CLOSERS = ")]}\"'”’)】」』》〉〕}" _SENTENCE_ENDINGS = (".", "!", "?", "。", "!", "?", "…") def _or_list(items: Sequence[str]) -> str: """Join call shapes the way an English sentence lists alternatives.""" if len(items) == 1: return items[0] if len(items) == 2: return f"{items[0]} or {items[1]}" return f"{', '.join(items[:-1])}, or {items[-1]}" def _as_sentence(guideline: str) -> str: """End one guideline like a sentence, without doubling its punctuation or breaking the line.""" text = " ".join(guideline.split()) core = text.rstrip(_CLOSERS) if core.endswith(_SENTENCE_ENDINGS): return text return f"{text}{'。' if _is_cjk(core[-1:]) else '.'}" def _is_cjk(text: str) -> bool: """Whether the last character is a CJK ideograph or kana, whose prose ends with ``。``.""" if not text: return False character = text[-1] return "\u3400" <= character <= "\u9fff" or "\u3040" <= character <= "\u30ff"
[文档] def render_tool_search_text(tool: AgentTool) -> str: """Return every model-facing text field of one tool for search indexing. The result includes the normalized tool name, description, parameter descriptions from its JSON Schema, snippet, guidelines, and the prose about its result. Search extensions can index this text without knowing how ``AgentTool`` stores or renders those fields. """ properties = tool.parameters.get("properties") parameter_descriptions = ( " ".join(str(value.get("description", "")) for value in properties.values() if isinstance(value, Mapping)) if isinstance(properties, Mapping) else "" ) return " ".join( ( tool.name.replace("_", " "), tool.description, parameter_descriptions, tool.snippet, " ".join(tool.guidelines), tool.returns_description, ) )
def _schema_text(schema: Mapping[str, Any]) -> str: """Render a JSON schema on one line, so it stays inside its tool bullet.""" return json.dumps(schema, ensure_ascii=False, sort_keys=True, separators=(",", ":")) def _describes_return(tool: AgentTool) -> bool: """Whether the tool says anything about its result: a shape, prose, or both.""" return tool.return_schema is not None or bool(tool.returns_description) def _render_tool_snippet(tool: AgentTool, *, returns: bool = False) -> str: """Keep every line of a multiline snippet inside its tool bullet. Blank lines are dropped: a snippet is a handful of call shapes, and the gesture between two of them says nothing to the model. A single shape stays on the tool's line; several are listed one per line, each introduced as a way to call the tool. The return hint is one sentence after them, so its schema cannot run into the next tool's bullet. The docstring's ``Returns`` prose follows that sentence, ending the tool's own entry. """ lines = [line for line in tool.snippet.splitlines() if line.strip()] hint = f"It returns {_schema_text(tool.return_schema)}." if returns and tool.return_schema is not None else "" prose = _as_sentence(tool.returns_description) if returns and tool.returns_description else "" tail = " ".join(part for part in (hint, prose) if part) if not lines: return f"- {tool.name}:" + (f" {tail}" if tail else "") if len(lines) == 1: text = f"- {tool.name}: You can call it as {lines[0]}." return f"{text} {tail}" if tail else text body = "\n".join(f" {line}" for line in lines) text = f"- {tool.name}:\n You can call it as:\n{body}" return f"{text}\n {tail}" if tail else text