|
| 1 | +# Copyright 2025 © BeeAI a Series of LF Projects, LLC |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +from __future__ import annotations |
| 5 | + |
| 6 | +from typing import TYPE_CHECKING, Any, Self |
| 7 | + |
| 8 | +from pydantic import BaseModel, Field |
| 9 | + |
| 10 | +from beeai_framework.context import RunContext |
| 11 | +from beeai_framework.emitter.emitter import Emitter |
| 12 | +from beeai_framework.tools import JSONToolOutput |
| 13 | +from beeai_framework.tools.errors import ToolError |
| 14 | +from beeai_framework.tools.tool import Tool |
| 15 | +from beeai_framework.tools.types import ToolRunOptions |
| 16 | + |
| 17 | +try: |
| 18 | + from acp.schema import EnvVariable |
| 19 | +except ModuleNotFoundError as e: |
| 20 | + raise ModuleNotFoundError( |
| 21 | + "Optional module [acp_zed] not found.\nRun 'pip install \"beeai-framework[acp-zed]\"' to install." |
| 22 | + ) from e |
| 23 | + |
| 24 | +if TYPE_CHECKING: |
| 25 | + from beeai_framework.adapters.acp_zed.serve.server import ACPZedServer |
| 26 | + |
| 27 | + |
| 28 | +class ACPZedTerminalToolInput(BaseModel): |
| 29 | + command: str = Field(description="Program to run (e.g. 'pytest', 'npm').") |
| 30 | + args: list[str] = Field(default_factory=list, description="Arguments passed to the program.") |
| 31 | + cwd: str | None = Field(default=None, description="Working directory (absolute path).") |
| 32 | + env: dict[str, str] | None = Field(default=None, description="Extra environment variables.") |
| 33 | + output_byte_limit: int | None = Field(default=None, description="Cap captured output in bytes. Editor-enforced.") |
| 34 | + wait: bool = Field( |
| 35 | + default=True, |
| 36 | + description=( |
| 37 | + "Wait for the process to exit and return its output. If false, returns the terminal id immediately." |
| 38 | + ), |
| 39 | + ) |
| 40 | + |
| 41 | + |
| 42 | +class ACPZedTerminalTool(Tool[ACPZedTerminalToolInput, ToolRunOptions, JSONToolOutput[dict[str, Any]]]): |
| 43 | + """Run a command in the editor's terminal via ACP `terminal/*`. |
| 44 | +
|
| 45 | + Live output shows in the Zed terminal widget while the process is running; the |
| 46 | + agent blocks on exit (by default) and gets back `{exit_code, stdout, stderr, |
| 47 | + terminal_id}`. Requires the client to advertise `terminal` capability. |
| 48 | + """ |
| 49 | + |
| 50 | + name = "acp_zed_terminal" |
| 51 | + description = ( |
| 52 | + "Run a command in the user's editor terminal. Returns exit code, stdout, and stderr after the " |
| 53 | + "process exits. Prefer this over generic shell when running under Zed." |
| 54 | + ) |
| 55 | + input_schema = ACPZedTerminalToolInput |
| 56 | + |
| 57 | + def __init__(self, server: ACPZedServer[Any], options: dict[str, Any] | None = None) -> None: |
| 58 | + super().__init__(options=options) |
| 59 | + self._server = server |
| 60 | + |
| 61 | + async def clone(self) -> Self: |
| 62 | + return type(self)(server=self._server, options=self.options) |
| 63 | + |
| 64 | + def _create_emitter(self) -> Emitter: |
| 65 | + return Emitter.root().child(namespace=["tool", "acp_zed", "terminal"], creator=self) |
| 66 | + |
| 67 | + async def _run( |
| 68 | + self, input: ACPZedTerminalToolInput, options: ToolRunOptions | None, context: RunContext |
| 69 | + ) -> JSONToolOutput[dict[str, Any]]: |
| 70 | + bridge = self._server.bridge |
| 71 | + if not bridge.can_terminal: |
| 72 | + raise ToolError("ACP client does not advertise terminal capability") |
| 73 | + |
| 74 | + env_list = [EnvVariable(name=k, value=v) for k, v in (input.env or {}).items()] or None |
| 75 | + terminal_id = await bridge.create_terminal( |
| 76 | + input.command, |
| 77 | + args=input.args, |
| 78 | + cwd=input.cwd, |
| 79 | + env=env_list, |
| 80 | + output_byte_limit=input.output_byte_limit, |
| 81 | + ) |
| 82 | + |
| 83 | + if not input.wait: |
| 84 | + return JSONToolOutput({"terminal_id": terminal_id, "waited": False}) |
| 85 | + |
| 86 | + try: |
| 87 | + exit_info = await bridge.wait_for_terminal_exit(terminal_id) |
| 88 | + output = await bridge.terminal_output(terminal_id) |
| 89 | + finally: |
| 90 | + await bridge.release_terminal(terminal_id) |
| 91 | + |
| 92 | + return JSONToolOutput( |
| 93 | + { |
| 94 | + "terminal_id": terminal_id, |
| 95 | + "exit_code": getattr(exit_info, "exit_code", None), |
| 96 | + "signal": getattr(exit_info, "signal", None), |
| 97 | + "stdout": getattr(output, "output", "") or "", |
| 98 | + "truncated": getattr(output, "truncated", False), |
| 99 | + "waited": True, |
| 100 | + } |
| 101 | + ) |
0 commit comments