Files
pydantic--pydantic-ai/.github/scripts/pydantic_ai_gh_aw_shim/write.py
T
wehub-resource-sync 9201ef759e
CI / lint (push) Waiting to run
CI / mypy (push) Waiting to run
CI / docs (push) Waiting to run
CI / test on 3.10 (standard) (push) Waiting to run
CI / test on 3.11 (standard) (push) Waiting to run
CI / test on 3.12 (standard) (push) Waiting to run
CI / test on 3.10 (all-extras) (push) Waiting to run
CI / test on 3.11 (all-extras) (push) Waiting to run
CI / test on 3.12 (all-extras) (push) Waiting to run
CI / test on 3.13 (all-extras) (push) Waiting to run
CI / test on 3.14 (pydantic-evals) (push) Waiting to run
Harness Compat / harness compat (push) Waiting to run
CI / test on 3.13 (standard) (push) Waiting to run
CI / test on 3.14 (standard) (push) Waiting to run
CI / test on 3.14 (all-extras) (push) Waiting to run
CI / test on 3.10 (pydantic-ai-slim) (push) Waiting to run
CI / test on 3.11 (pydantic-ai-slim) (push) Waiting to run
CI / test on 3.12 (pydantic-ai-slim) (push) Waiting to run
CI / test on 3.13 (pydantic-ai-slim) (push) Waiting to run
CI / test on 3.14 (pydantic-ai-slim) (push) Waiting to run
CI / test on 3.10 (pydantic-evals) (push) Waiting to run
CI / test on 3.11 (pydantic-evals) (push) Waiting to run
CI / test on 3.12 (pydantic-evals) (push) Waiting to run
CI / test on 3.13 (pydantic-evals) (push) Waiting to run
CI / test on 3.10 (lowest-versions) (push) Waiting to run
CI / test on 3.11 (lowest-versions) (push) Waiting to run
CI / test on 3.12 (lowest-versions) (push) Waiting to run
CI / test on 3.13 (lowest-versions) (push) Waiting to run
CI / test on 3.14 (lowest-versions) (push) Waiting to run
CI / test examples on 3.11 (push) Waiting to run
CI / test examples on 3.12 (push) Waiting to run
CI / test examples on 3.13 (push) Waiting to run
CI / test examples on 3.14 (push) Waiting to run
CI / coverage (push) Blocked by required conditions
CI / check (push) Blocked by required conditions
CI / deploy-docs (push) Blocked by required conditions
CI / deploy-docs-preview (push) Blocked by required conditions
CI / build release artifacts (push) Blocked by required conditions
CI / publish to PyPI (push) Blocked by required conditions
CI / Send tweet (push) Blocked by required conditions
chore: import upstream snapshot with attribution
2026-07-13 13:27:52 +08:00

33 lines
1.3 KiB
Python

"""Claude's `Write` tool -- create or overwrite a workspace text file.
Backed by pydantic-ai-harness's `FileSystemToolset.write_file` (path
containment, symlink resolution). Claude's `Write` creates missing parent
directories, and the harness `write_file` requires the parent to exist, so the
adapter calls `create_directory` first to keep that behavior.
"""
import os
from pydantic_ai.exceptions import ModelRetry
from ._backends import filesystem
from .shared import attach_context
async def write_file(file_path: str, content: str) -> str:
"""Create or overwrite a UTF-8 text file under the workspace."""
fs = filesystem()
parent = os.path.dirname(file_path)
try:
if parent:
await fs.create_directory(parent)
result = await fs.write_file(file_path, content)
except (ModelRetry, OSError) as exc:
# The harness converts its own recoverable errors to `ModelRetry`, but
# `create_directory` -> `Path.mkdir(exist_ok=True)` still raises a bare
# `FileExistsError` when a path segment is an existing file. The old
# hand-rolled `Write` caught `OSError`, so keep doing that: a bad path is
# a returned error, not a run-aborting exception.
return f'error: {exc}'
return attach_context(file_path) + result