agent-zero/plugins/_text_editor/helpers/patch_request.py
Alessandro 4c2bc3d783 Add context-based patch_text support to text_editor
Introduces patch_text editing for the Docker-local text_editor, sharing request validation and freshness-state logic with text_editor_remote while preserving legacy line-number edits. Adds anchored context patching, safer state handling after context edits, updated model guidance, live remote wrapper reuse, and focused regression coverage for chained patches and Python replacement cases.
2026-04-21 18:18:59 +02:00

37 lines
983 B
Python

from __future__ import annotations
from dataclasses import dataclass
from typing import Any, Literal
PatchMode = Literal["edits", "patch_text"]
@dataclass(frozen=True)
class PatchRequest:
mode: PatchMode
edits: Any = None
patch_text: str = ""
def parse_patch_request(
edits: Any,
patch_text: Any,
*,
both_error: str = "provide either edits or patch_text, not both",
missing_error: str = "edits or patch_text is required for patch",
) -> tuple[PatchRequest | None, str]:
"""Validate the mutually-exclusive patch request shape."""
if edits is not None and patch_text is not None:
return None, both_error
if patch_text is not None:
text = str(patch_text)
if not text.strip():
return None, "patch_text must not be empty"
return PatchRequest(mode="patch_text", patch_text=text), ""
if not edits:
return None, missing_error
return PatchRequest(mode="edits", edits=edits), ""