Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

datasette-agent-edit

Storage-agnostic file-editing tools (view / str_replace / insert / batch edit) for Datasette Agent plugins. The same tool behaviour can sit on top of any storage layer — SQLite, the local filesystem, S3, the GitHub contents API, …

The three layers

  1. operations — pure, synchronous string surgery (view_lines, str_replace, insert, apply_edits). No I/O, no await, no Datasette.
  2. EditStore — the storage seam. The defining method is edit(ref, transform): the backend reads the current content, runs your pure transform inside whatever critical section it needs, and persists the result atomically. A failing transform persists nothing.
    • SqliteVersionedStore runs the transform inside Datasette's write thread (execute_write_fn) and keeps full version history.
    • DiskStore uses a lock + atomic os.replace.
    • S3 (If-Match) and GitHub (sha) backends fit the same shape with a compare-and-set retry loop.
  3. EditToolset — turns any EditStore into Datasette Agent tools with one consistent JSON envelope. Two hooks absorb the plugin-specific parts:
    • id_codec maps internal refs to the ids the model sees (e.g. an artifact- prefix).
    • render optionally injects presentation (e.g. an _html iframe preview); omit it and no *_render tool is registered.

What the tools do

With name_prefix="file" the toolset registers file_view, file_str_replace, file_insert and file_edit, plus file_render if you pass a render hook.

Every tool returns a JSON object. Success includes the id (under id_field) and the current version; any failure is {"error": "..."} and saves nothing. Bad input from the model — a malformed range, an out-of-range line number, an edit with missing keys, a ref the store rejects — comes back as an error rather than raising.

  • view returns the content with N:<tab> line-number prefixes, plus metadata and version. view_range is "start,end", 1-indexed and inclusive, with -1 meaning end-of-file. Trailing whitespace is shown as-is so the text can be pasted into str_replace.
  • str_replace replaces old_str, which must appear exactly once.
  • insert adds insert_text as whole lines after line insert_line (0 for the start, the line count for the end). A line break is added if the text would otherwise run into a neighbouring line.
  • edit applies an array of str_replace / insert operations in order as one atomic change. If any of them fails, none are saved and the error names the failing edit.

Only \n (and so \r\n) ends a line, so line numbers match what an editor shows.

The render hook's result is merged into the output of edit and render. It cannot override the id, version or status fields. Because the hook runs after a batch edit has been saved, a hook that raises does not fail the edit: the result reports the edits as saved and adds a render_error key.

Backend notes

Both bundled backends raise FileExistsError from create() if the ref is taken, and leave version untouched when an edit returns the content unchanged.

SqliteVersionedStore appends a row to its versions table for every change.

DiskStore treats a ref as a path under its root directory:

  • Symlinks are resolved first, so a ref cannot reach outside the root.
  • Metadata lives in a <path>.meta.json sidecar, so refs ending in .meta.json are reserved and rejected.
  • Files must be UTF-8 text. Line endings are preserved byte-for-byte and an edited file keeps its permission bits.
  • Locking is per-process; see the module docstring for cross-process use.

Why transform is synchronous

The transform sits between a backend's awaits, never inside them — the SQLite backend literally cannot await on its write thread, and the S3/GitHub backends must not re-run network calls on every compare-and-set retry. If an edit decision needs async work, resolve it first and close over the result:

resolved = await registry.lookup(name)
await store.edit(ref, lambda c: rewrite(c, resolved))

A rare backend that genuinely needs in-transaction async can implement the optional AsyncTransformStore.aedit capability; the toolset never requires it.

Example

from datasette_agent_edit import EditToolset, SqliteVersionedStore, PrefixCodec

store = SqliteVersionedStore(datasette.get_internal_database())
toolset = EditToolset(
    store,
    name_prefix="artifact",
    id_field="artifact_id",
    id_codec=PrefixCodec("artifact-"),
    render=lambda editable: {"_html": build_iframe(editable.content, editable.metadata)},
)
agent_tools = toolset.tools()   # list of AgentTool, ready to register

Development

uv run pytest

The tests need Python 3.11 or later. datasette-agent is a development dependency, used to test EditToolset.tools() against the real AgentTool.

Metadata

Release files for datasette-agent-edit 0.2a0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for datasette-agent-edit 0.2a0
File Size Uploaded Provenance Actions
datasette_agent_edit-0.2a0.tar.gz 25.8 kB With provenance Details for datasette_agent_edit-0.2a0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for datasette-agent-edit 0.2a0
File Interpreter ABI Platform Actions
datasette_agent_edit-0.2a0-py3-none-any.whl Python 3 none any Details for datasette_agent_edit-0.2a0-py3-none-any.whl

Total release size: 47.5 kB

Release files / datasette_agent_edit-0.2a0.tar.gz

Download URL datasette_agent_edit-0.2a0.tar.gz
Size 25.8 kB
Tags Source
SHA-256 checksum
How to use checksums
0f31b5bccd2c7532843b6ba3ea62d28f17cee7b621df1a58990e1a28eaf573df
BLAKE2b-256 checksum
How to use checksums
de6fc443c325d203971d44ddea97ea60ab27e845dacac993c0a1fdcf1ffddacd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 23, 2026.

Transparency log

Release files / datasette_agent_edit-0.2a0-py3-none-any.whl

Download URL datasette_agent_edit-0.2a0-py3-none-any.whl
Size 21.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
011d297c42c895e30ac5ad9b3e9d7a56e554bd7b590616a0fc6ee4df111607f6
BLAKE2b-256 checksum
How to use checksums
5f92341bd2a839a32ac50b53e168bb5c1b73c6227a9202e00f7362b514c07115
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 23, 2026.

Transparency log

Security for datasette-agent-edit 0.2a0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for datasette_agent_edit-0.2a0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 23, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page