Spaces:
Paused
Paused
File size: 12,384 Bytes
9792ea7 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 | # -*- coding: utf-8 -*-
"""The write tool in agentscope."""
import difflib
import fnmatch
from pathlib import Path
from typing import Any, List
from .._base import ToolBase, ToolMiddlewareBase
from .._constants import (
DEFAULT_DANGEROUS_FILES,
DEFAULT_DANGEROUS_DIRECTORIES,
)
from ...permission import (
PermissionContext,
PermissionDecision,
PermissionBehavior,
PermissionMode,
PermissionRule,
)
from .._response import ToolChunk
from ...message import TextBlock, ToolResultState
from ...state import AgentState
from ._backend import BackendBase
class Write(ToolBase):
"""The write tool."""
name: str = "Write"
"""The tool name presented to the agent."""
# pylint: disable=line-too-long
description: str = """Writes a file to the local filesystem.
Usage:
- This tool will overwrite the existing file if there is one at the provided path.
- If this is an existing file, you MUST use the Read tool first to read the file's contents. This tool will fail if you did not read the file first.
- ALWAYS prefer editing existing files in the codebase. NEVER write new files unless explicitly required.
- NEVER proactively create documentation files (*.md) or README files. Only create documentation files if explicitly requested by the User.
- Only use emojis if the user explicitly requests it. Avoid writing emojis to files unless asked.""" # noqa: E501
"""The description presented to the agent."""
input_schema: dict[str, Any] = {
"type": "object",
"properties": {
"file_path": {
"type": "string",
"description": "The absolute path to the file to write "
"(must be absolute, not relative)",
},
"content": {
"type": "string",
"description": "The content to write to the file",
},
},
"required": ["file_path", "content"],
}
is_mcp: bool = False
is_read_only: bool = False
is_concurrency_safe: bool = False
is_external_tool: bool = False
is_state_injected: bool = True
def __init__( # pylint: disable=dangerous-default-value
self,
dangerous_files: list[str] = DEFAULT_DANGEROUS_FILES,
dangerous_directories: list[str] = DEFAULT_DANGEROUS_DIRECTORIES,
middlewares: List[ToolMiddlewareBase] | None = None,
backend: BackendBase | None = None,
) -> None:
"""Initialize the write tool.
Args:
dangerous_files (`list[str]`, optional):
Sensitive files that require explicit user confirmation,
even in BYPASS mode. Matched by basename
(case-insensitive). Defaults to `DEFAULT_DANGEROUS_FILES`.
Pass a custom list to fully replace the defaults, or `[]`
to disable the filename check.
dangerous_directories (`list[str]`, optional):
Sensitive directories that require explicit user
confirmation. Matched when any path segment equals an
entry (case-insensitive). Defaults to
`DEFAULT_DANGEROUS_DIRECTORIES`. Pass a custom list to
fully replace the defaults, or `[]` to disable the
directory check.
middlewares (`List[ToolMiddlewareBase] | None`, optional):
Tool middlewares wrapping the tool execution.
backend (`BackendBase | None`, optional):
The sandbox backend to use for file I/O. When ``None``,
a :class:`LocalBackend` is created.
"""
from ._backend import LocalBackend
super().__init__(middlewares=middlewares)
self.dangerous_files = list(dangerous_files)
self.dangerous_directories = list(dangerous_directories)
self._backend = backend or LocalBackend()
async def check_permissions(
self,
tool_input: dict[str, Any],
context: PermissionContext,
) -> PermissionDecision:
"""Check permissions for file writing.
This method implements Write-specific permission checks:
1. Dangerous path check (safety check, bypass-immune)
2. ACCEPT_EDITS mode check for files in working directories
Args:
tool_input (`dict[str, Any]`):
The tool input containing "file_path" key
context (`PermissionContext`):
The permission context with mode and rules
Returns:
`PermissionDecision`:
ASK for dangerous paths, ALLOW for safe operations in
ACCEPT_EDITS mode, PASSTHROUGH otherwise
"""
file_path = tool_input.get("file_path")
if not file_path:
return PermissionDecision(
behavior=PermissionBehavior.PASSTHROUGH,
message="No file path provided",
)
# 1. Check for dangerous paths (safety check, bypass-immune)
if self._is_dangerous_path(file_path):
return PermissionDecision(
behavior=PermissionBehavior.ASK,
message=f"Permission required: Write operation on "
f"sensitive file {file_path}",
decision_reason="Safety check: dangerous file or directory",
bypass_immune=True,
)
# 2. Check ACCEPT_EDITS mode for files in working directories
if context.mode == PermissionMode.ACCEPT_EDITS:
if self._path_in_allowed_working_path(file_path, context):
return PermissionDecision(
behavior=PermissionBehavior.ALLOW,
message=f"Permission granted for writing {file_path} "
f"(accept edits mode - in working directory)",
decision_reason="File is in working directory and not "
"a dangerous path",
)
# 3. Return PASSTHROUGH to let PermissionEngine check allow rules
# This ensures allow rules can grant Write permissions
return PermissionDecision(
behavior=PermissionBehavior.PASSTHROUGH,
message="",
)
async def match_rule(
self,
rule_content: str | None,
tool_input: dict[str, Any],
) -> bool:
"""Check if a permission rule matches the file path.
Matches rule_content as a glob pattern against the "file_path"
parameter using fnmatch. If rule_content is None, matches all
invocations (tool-name-level rule).
Args:
rule_content (`str | None`):
Glob pattern to match against the file path (e.g., "src/**"),
or None to match all invocations
tool_input (`dict[str, Any]`):
The tool input data containing "file_path" key
Returns:
`bool`:
True if the glob pattern matches the file path, False otherwise
"""
if rule_content is None:
return True
file_path = tool_input.get("file_path", "")
if not file_path:
return False
return fnmatch.fnmatch(file_path, rule_content)
async def generate_suggestions(
self,
tool_input: dict[str, Any],
) -> List[PermissionRule]:
"""Generate suggested permission rules for the file path.
Suggests a glob pattern covering the parent directory of the file,
allowing the user to grant permission for the entire directory at once.
Args:
tool_input (`dict[str, Any]`):
The tool input data containing "file_path" key
Returns:
`List[PermissionRule]`:
A single suggested rule covering the parent directory
(e.g., file "/src/main.py" -> rule "src/**")
"""
file_path = tool_input.get("file_path", "")
if not file_path:
return []
parent = self._backend.dirname(file_path)
# Glob patterns are POSIX-style strings (matched by fnmatch),
# not real filesystem paths — do NOT use backend.join_path here.
pattern = (parent.rstrip("/\\") + "/**") if parent else "**"
return [
PermissionRule(
tool_name=self.name,
rule_content=pattern,
behavior=PermissionBehavior.ALLOW,
source="suggested",
),
]
async def call( # type: ignore[override]
self,
file_path: str,
content: str,
_agent_state: AgentState | None = None,
) -> ToolChunk:
"""Write content to a file and return the result."""
# Validate that file_path is absolute
if not self._backend.isabs(file_path):
return ToolChunk(
content=[
TextBlock(
text=f"Error: file_path must be an absolute path, "
f"got: {file_path}",
),
],
state=ToolResultState.ERROR,
is_last=True,
)
# Check if file exists, it must be read first if it exists
if (
await self._backend.file_exists(file_path)
and _agent_state is not None
):
cache = await _agent_state.tool_context.get_cache(file_path)
if cache is None:
return ToolChunk(
content=[
TextBlock(
text=f"Error: File {file_path} exists but has not "
f"been read yet. You must read the file first "
f"before writing to it.",
),
],
state=ToolResultState.ERROR,
is_last=True,
)
# Capture the pre-write content (if any) so we can compute a unified
# diff for the web UI. For brand-new files this stays as an empty
# string, which produces a clean "new file" diff (``--- /dev/null``).
# Track ``file_existed`` separately from ``previous_content`` because
# an *existing* empty file overwrite is not the same as creating a
# new file — the diff header must reflect that.
file_existed = await self._backend.file_exists(file_path)
previous_content = ""
if file_existed:
try:
previous_content = (
await self._backend.read_file(file_path)
).decode("utf-8")
except Exception: # pylint: disable=broad-except
# Binary or unreadable file — fall back to empty so we still
# render a best-effort "add" diff in the UI.
previous_content = ""
# Create parent directories if they don't exist
parent_dir = Path(file_path).parent
await self._backend.exec_shell(
["mkdir", "-p", str(parent_dir)],
)
# Write content to file (backend handles parent dir creation)
await self._backend.write_file(
file_path,
content.encode("utf-8"),
)
# Count lines in content
line_count = len(content.split("\n"))
# Build the unified diff between previous and new content. When the
# file is brand new, ``unified_diff`` over an empty old side naturally
# produces a single "all add" hunk starting at line 1.
diff_text = "".join(
difflib.unified_diff(
previous_content.splitlines(keepends=True),
content.splitlines(keepends=True),
fromfile=(
"/dev/null" if not file_existed else f"a/{file_path}"
),
tofile=f"b/{file_path}",
n=3,
),
)
# Return success message
return ToolChunk(
content=[
TextBlock(
text=f"The file {file_path} has been written successfully "
f"({line_count} lines).",
),
],
state=ToolResultState.RUNNING,
is_last=True,
metadata={
"diff": diff_text,
"file_path": file_path,
"occurrences": 1,
},
)
|