45 lines
1.3 KiB
Python
45 lines
1.3 KiB
Python
"""工具输出防护示例 Skill。"""
|
|
|
|
from dataclasses import replace
|
|
|
|
from app.application.agent.extensions import (
|
|
AfterToolCallDecision,
|
|
AgentRunContext,
|
|
ExtensionRegistry,
|
|
ToolCallContext,
|
|
)
|
|
from app.application.agent.python_skills import PythonSkillDefinition
|
|
from app.modules.tool_runtime.definition import ToolResult
|
|
|
|
_MAX_OUTPUT_CHARS = 4000
|
|
|
|
|
|
async def _limit_tool_output(
|
|
_call: ToolCallContext,
|
|
result: ToolResult,
|
|
_run: AgentRunContext,
|
|
) -> AfterToolCallDecision | None:
|
|
if len(result.stdout) <= _MAX_OUTPUT_CHARS and len(result.stderr) <= _MAX_OUTPUT_CHARS:
|
|
return None
|
|
return AfterToolCallDecision(
|
|
result=replace(
|
|
result,
|
|
stdout=result.stdout[:_MAX_OUTPUT_CHARS],
|
|
stderr=result.stderr[:_MAX_OUTPUT_CHARS],
|
|
truncated=True,
|
|
)
|
|
)
|
|
|
|
|
|
def _setup(registry: ExtensionRegistry) -> None:
|
|
registry.add_after_tool_hook(_limit_tool_output)
|
|
|
|
|
|
SKILL = PythonSkillDefinition(
|
|
name="response_guard",
|
|
description="限制过长工具输出,避免无关内容挤占模型上下文。",
|
|
triggers=("长输出", "大量输出", "response guard"),
|
|
instructions="工具输出可能很长;优先分段读取,只保留完成当前任务所需的内容。",
|
|
setup=_setup,
|
|
)
|