Files
agent-radar 1a3a4ccde1
Creator App Tests / PR Spam Gate (push) Skipped
Frontend Tests / PR Spam Gate (push) Skipped
NPM Format (website & console) / PR Spam Gate (push) Skipped
Pre-commit Checks / PR Spam Gate (push) Skipped
QwenPaw Creator Plugin Release / check-version (push) Canceled after 0s
QwenPaw Creator Plugin Release / upload-oss (push) Canceled after 0s
Tests / PR Spam Gate (push) Skipped
CodeQL / CodeQL (python) (push) Canceled after 0s
CodeQL / CodeQL (typescript) (push) Canceled after 0s
Creator App Tests / Creator Backend Tests (push) Canceled after 0s
Creator App Tests / Creator UI Build & Tests (push) Canceled after 0s
E2E Smoke Tests / Playwright UI Smoke (push) Canceled after 0s
Frontend Tests / Detect console changes (push) Canceled after 0s
NPM Format (website & console) / website format check (push) Canceled after 0s
NPM Format (website & console) / console format check (push) Canceled after 0s
Pre-commit Checks / Pre-commit (push) Canceled after 0s
QwenPaw Data Plugin Release / check-version (push) Canceled after 0s
Tests / Detect code changes (push) Canceled after 0s
Frontend Tests / Vitest Unit Tests (push) Canceled after 0s
Frontend Tests / Frontend Summary (push) Canceled after 0s
QwenPaw Data Plugin Release / upload-oss (push) Canceled after 0s
Tests / Maintainer Approval (push) Canceled after 0s
Tests / Unit Tests - py3.11 - macos-latest (push) Canceled after 0s
Tests / Unit Tests - py3.11 - ubuntu-latest (push) Canceled after 0s
Tests / Unit Tests - py3.13 - ubuntu-latest (push) Canceled after 0s
Tests / Unit Tests - py3.11 - windows-latest (push) Canceled after 0s
Tests / Contract Tests - py3.11 - macos-latest (push) Canceled after 0s
Tests / Contract Tests - py3.11 - ubuntu-latest (push) Canceled after 0s
Tests / Contract Tests - py3.13 - ubuntu-latest (push) Canceled after 0s
Tests / Contract Tests - py3.11 - windows-latest (push) Canceled after 0s
Tests / Integrated Tests - py3.11 - macos-latest - p0 (push) Canceled after 0s
Tests / Integrated Tests - py3.11 - macos-latest - p1 (push) Canceled after 0s
Tests / Integrated Tests - py3.11 - macos-latest - p2 (push) Canceled after 0s
Tests / Integrated Tests - py3.11 - ubuntu-latest - fallback (push) Canceled after 0s
Tests / Integrated Tests - py3.11 - ubuntu-latest - p0 (push) Canceled after 0s
Tests / Integrated Tests - py3.11 - ubuntu-latest - p1 (push) Canceled after 0s
Tests / Integrated Tests - py3.11 - ubuntu-latest - p2 (push) Canceled after 0s
Tests / Integrated Tests - py3.13 - ubuntu-latest - p0 (push) Canceled after 0s
Tests / Integrated Tests - py3.13 - ubuntu-latest - p1 (push) Canceled after 0s
Tests / Integrated Tests - py3.13 - ubuntu-latest - p2 (push) Canceled after 0s
Tests / Integrated Tests - py3.11 - windows-latest - p0 (push) Canceled after 0s
Tests / Integrated Tests - py3.11 - windows-latest - p1 (push) Canceled after 0s
Tests / Integrated Tests - py3.11 - windows-latest - p2 (push) Canceled after 0s
Tests / Coverage Report (push) Canceled after 0s
Tests / Test Summary (push) Canceled after 0s
源码快照 @ 2026-09-01 (source-only, 剔除非源码素材)
2026-09-01 20:01:50 +08:00

80 lines
2.4 KiB
Python

# -*- coding: utf-8 -*-
"""Tracing Middleware Demo Plugin.
Demonstrates `on_acting` middleware that logs every tool call with
timing information to a trace file in the workspace.
The middleware factory conditionally activates: it returns None (skip)
unless the ``QWENPAW_TRACE`` environment variable is set.
"""
import logging
import os
import time
from pathlib import Path
from typing import Any, AsyncGenerator, Callable
from agentscope.middleware import MiddlewareBase
from qwenpaw.plugins.api import PluginApi
logger = logging.getLogger(__name__)
class TracingMiddleware(MiddlewareBase):
"""Logs tool call name, input, and execution duration."""
def __init__(self, trace_file: Path) -> None:
self._trace_file = trace_file
self._trace_file.parent.mkdir(parents=True, exist_ok=True)
async def on_acting( # pylint: disable=unused-argument
self,
agent: Any,
input_kwargs: dict[str, Any],
next_handler: Callable[..., AsyncGenerator[Any, None]],
) -> AsyncGenerator[Any, None]:
tool_call = input_kwargs["tool_call"]
tool_name = getattr(tool_call, "name", str(tool_call))
tool_input = getattr(tool_call, "input", "")
start = time.perf_counter()
try:
async for item in next_handler():
yield item
finally:
elapsed_ms = (time.perf_counter() - start) * 1000
line = (
f"[{time.strftime('%H:%M:%S')}] "
f"{tool_name}({tool_input[:100]}) — {elapsed_ms:.1f}ms\n"
)
try:
with open(self._trace_file, "a", encoding="utf-8") as f:
f.write(line)
except OSError:
logger.debug("Failed to write trace log")
def _tracing_factory(ctx: Any, agent_config: Any) -> TracingMiddleware | None:
"""Create TracingMiddleware when QWENPAW_TRACE env var is set."""
del agent_config
if not os.environ.get("QWENPAW_TRACE"):
return None
workspace_dir = getattr(ctx, "workspace_dir", None)
if workspace_dir is None:
return None
trace_file = Path(workspace_dir) / ".qwenpaw" / "trace.log"
return TracingMiddleware(trace_file=trace_file)
class TracingPlugin:
"""Plugin entry point."""
def register(self, api: PluginApi) -> None:
api.register_middleware(_tracing_factory, priority=50)
plugin = TracingPlugin()