Files
wehub-resource-sync 4b6817381b
CI (OpenClaw E2E) / openclaw test (push) Has been cancelled
CI / coverage-report (push) Has been cancelled
CI / test-kubernetes (push) Has been cancelled
CI / should-run-thorough (push) Has been cancelled
CI / test-thorough (cloudwatch-demo) (push) Has been cancelled
CI / test-thorough (flink-ecs) (push) Has been cancelled
CI / test-thorough (upstream-lambda) (push) Has been cancelled
CI / test-thorough (prefect-ecs-fargate) (push) Has been cancelled
Release / build-binaries (zip, opensre.exe, onefile, windows-latest, windows-x64) (push) Has been cancelled
Benchmark image — build + push to ECR (any adapter) / build + push (push) Has been cancelled
CI / quality (ubuntu-latest) (push) Has been cancelled
CI / test (tools-runtime) (push) Has been cancelled
CI / test (e2e-general) (push) Has been cancelled
CI / test (cli-runtime) (push) Has been cancelled
CI / test (e2e-provider-and-openclaw) (push) Has been cancelled
CI / test (integrations-and-misc) (push) Has been cancelled
Release / verify (push) Has been cancelled
Release / build-python-dist (push) Has been cancelled
Release / build-binaries (tar.gz, opensre, onedir, macos-15-intel, darwin-x64) (push) Has been cancelled
Release / build-binaries (tar.gz, opensre, onedir, macos-latest, darwin-arm64) (push) Has been cancelled
Release / build-binaries (tar.gz, opensre, onedir, ubuntu-22.04, linux-x64) (push) Has been cancelled
Release / publish-release (push) Has been cancelled
Release / publish-main-release (push) Has been cancelled
Interactive Shell Live (PR + post-merge) / turn-checks (no-LLM) (push) Has been cancelled
CodeQL / Analyze (python) (push) Has been cancelled
Interactive Shell Live (PR + post-merge) / turn-live shard ${{ matrix.shard_index }} (push) Has been cancelled
Release / prepare (push) Has been cancelled
Release / build-binaries (tar.gz, opensre, onedir, ubuntu-22.04-arm, linux-arm64) (push) Has been cancelled
Synthetic Deterministic Tests / Synthetic offline (deterministic) (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:10:45 +08:00

95 lines
3.7 KiB
Python

"""Rendering for the shell tool-calling turn.
This module owns the terminal-facing action observer. Planner tool calls are
internal state by default: the observer records them for history/storage while
the concrete action executors render user-facing command output. The execution
orchestration that drives it lives in
:func:`interactive_shell.runtime.action_turn.run_action_tool_turn`.
Keeping rendering here means the shell turn-entry adapter stays focused on
binding core ports while terminal formatting stays in ``ui/``.
"""
from __future__ import annotations
import contextlib
import json
from typing import Any
from rich.console import Console
from core.agent_harness.turns.action_driver import SELF_RECORDING_ACTION_TOOL_NAMES
from surfaces.interactive_shell.runtime import Session
# Tools whose preview is just ``(label, single-arg)``. The display content is the
# stripped string value of that single argument. Anything that needs to combine
# multiple arguments (``slash_invoke``, ``synthetic_run``) keeps a custom branch
# in :func:`tool_call_display`.
_SIMPLE_TOOL_LABELS: dict[str, tuple[str, str]] = {
"llm_set_provider": ("LLM provider", "target"),
"alert_sample": ("sample alert", "template"),
"investigation_start": ("investigation", "alert_text"),
"task_cancel": ("cancel task", "target"),
"cli_exec": ("opensre", "payload"),
"code_implement": ("implementation", "task"),
"shell_run": ("shell", "command"),
}
def tool_call_display(tool_name: str, args: dict[str, Any]) -> tuple[str, str]:
"""Return a ``(label, content)`` pair describing a planned tool call."""
if tool_name == "slash_invoke":
command = str(args.get("command", "")).strip()
raw_args = args.get("args")
parsed_args = [str(item).strip() for item in raw_args] if isinstance(raw_args, list) else []
return "command", " ".join([command, *parsed_args]).strip()
if tool_name == "synthetic_run":
suite = str(args.get("suite", "")).strip()
scenario = str(args.get("scenario", "")).strip()
return "synthetic test", f"{suite}:{scenario}" if scenario else suite
simple = _SIMPLE_TOOL_LABELS.get(tool_name)
if simple is not None:
label, arg_key = simple
return label, str(args.get(arg_key, "")).strip()
return tool_name, json.dumps(args, default=str, sort_keys=True)
class ActionRenderObserver:
"""Agent event observer that records planner turns not owned by action tools.
Self-recording tools (``slash_invoke``, ``shell_run``, etc.) append their own
history row; chat turns are recorded later by turn accounting when the
assistant runs.
"""
def __init__(self, *, session: Session, console: Console, message: str) -> None:
self.session = session
self.console = console
self.message = message
self.planned_count = 0
def __call__(self, kind: str, data: dict[str, Any]) -> None:
if kind == "tool_update":
with contextlib.suppress(Exception):
self.session.storage.append_tool_update(
self.session.session_id,
tool=str(data.get("name") or "tool"),
update=data.get("update"),
tool_call_id=str(data.get("id") or "") or None,
)
return
if kind != "tool_start":
return
name = str(data.get("name", "")).strip()
if not name or name == "assistant_handoff":
return
if self.planned_count == 0 and name not in SELF_RECORDING_ACTION_TOOL_NAMES:
self.session.record("cli_agent", self.message)
self.planned_count += 1
__all__ = [
"ActionRenderObserver",
"tool_call_display",
]