diff --git a/src/git_draft/drafter.py b/src/git_draft/drafter.py index c240eec..04dd0b7 100644 --- a/src/git_draft/drafter.py +++ b/src/git_draft/drafter.py @@ -202,7 +202,6 @@ async def generate_draft( { "prompt_id": prompt_id, "tool": o.tool, - "reason": o.reason, "details": json.dumps(o.details), "started_at": o.start, } @@ -440,40 +439,32 @@ def __init__(self, progress: Progress) -> None: self.operations = list[_Operation]() self._progress = progress - def on_list_files( - self, paths: Sequence[PurePosixPath], reason: str | None - ) -> None: + def on_list_files(self, paths: Sequence[PurePosixPath]) -> None: count = len(paths) self._progress.report("Listed available files.", count=count) - self._record(reason, "list_files", count=count) + self._record("list_files", count=count) - def on_read_file( - self, path: PurePosixPath, contents: str | None, reason: str | None - ) -> None: + def on_read_file(self, path: PurePosixPath, contents: str | None) -> None: size = -1 if contents is None else len(contents) self._progress.report(f"Read {path}.", length=size) - self._record(reason, "read_file", path=str(path), size=size) + self._record("read_file", path=str(path), size=size) - def on_write_file( - self, path: PurePosixPath, contents: str, reason: str | None - ) -> None: + def on_write_file(self, path: PurePosixPath, contents: str) -> None: size = len(contents) self._progress.report(f"Wrote {path}.", length=size) - self._record(reason, "write_file", path=str(path), size=size) + self._record("write_file", path=str(path), size=size) - def on_delete_file(self, path: PurePosixPath, reason: str | None) -> None: + def on_delete_file(self, path: PurePosixPath) -> None: self._progress.report(f"Deleted {path}.") - self._record(reason, "delete_file", path=str(path)) + self._record("delete_file", path=str(path)) def on_rename_file( self, src_path: PurePosixPath, dst_path: PurePosixPath, - reason: str | None, ) -> None: self._progress.report(f"Renamed {src_path} to {dst_path}.") self._record( - reason, "rename_file", src_path=str(src_path), dst_path=str(dst_path), @@ -481,12 +472,10 @@ def on_rename_file( def on_expose_files(self) -> None: self._progress.report("Exposed files.") - self._record(None, "expose_files") + self._record("expose_files") - def _record(self, reason: str | None, tool: str, **kwargs) -> None: - op = _Operation( - tool=tool, details=kwargs, reason=reason, start=datetime.now() - ) + def _record(self, tool: str, **kwargs) -> None: + op = _Operation(tool=tool, details=kwargs, start=datetime.now()) _logger.debug("Recorded operation. [op=%s]", op) self.operations.append(op) @@ -497,7 +486,6 @@ class _Operation: tool: str details: JSONObject - reason: str | None start: datetime diff --git a/src/git_draft/queries/add-operation.sql b/src/git_draft/queries/add-operation.sql index 7d68cc3..76c275b 100644 --- a/src/git_draft/queries/add-operation.sql +++ b/src/git_draft/queries/add-operation.sql @@ -1,2 +1,2 @@ -insert into operations (prompt_id, tool, reason, details, started_at) - values (:prompt_id, :tool, :reason, :details, :started_at) +insert into operations (prompt_id, tool, details, started_at) + values (:prompt_id, :tool, :details, :started_at) diff --git a/src/git_draft/queries/create-tables.sql b/src/git_draft/queries/create-tables.sql index 622746e..2c5e562 100644 --- a/src/git_draft/queries/create-tables.sql +++ b/src/git_draft/queries/create-tables.sql @@ -34,7 +34,6 @@ create table if not exists operations ( id integer primary key, prompt_id integer not null, tool text not null, - reason text, details text not null, started_at timestamp not null, foreign key (prompt_id) references actions (prompt_id) on delete cascade diff --git a/src/git_draft/store.py b/src/git_draft/store.py index 26f8785..07030b0 100644 --- a/src/git_draft/store.py +++ b/src/git_draft/store.py @@ -19,7 +19,7 @@ class Store: """Lightweight sqlite wrapper""" - _name = "v3.sqlite3" + _name = "v4.sqlite3" def __init__(self, conn: sqlite3.Connection) -> None: self._connection = conn diff --git a/src/git_draft/toolbox.py b/src/git_draft/toolbox.py index a0ffbb1..bc95d86 100644 --- a/src/git_draft/toolbox.py +++ b/src/git_draft/toolbox.py @@ -36,9 +36,6 @@ class Toolbox: # feedback more than once during a bot action, which leads to a better # experience when used interactively. - # TODO: Remove all reason arguments. They are not currently used, and there - # is no obvious use-case at the moment. - def __init__(self, visitors: Sequence[ToolVisitor] | None = None) -> None: self._visitors = visitors or [] @@ -46,48 +43,34 @@ def _dispatch(self, effect: Callable[[ToolVisitor], None]) -> None: for visitor in self._visitors: effect(visitor) - def list_files(self, reason: str | None = None) -> Sequence[PurePosixPath]: + def list_files(self) -> Sequence[PurePosixPath]: paths = self._list() - self._dispatch(lambda v: v.on_list_files(paths, reason)) + self._dispatch(lambda v: v.on_list_files(paths)) return paths - def read_file( - self, - path: PurePosixPath, - reason: str | None = None, - ) -> str | None: + def read_file(self, path: PurePosixPath) -> str | None: try: contents = self._read(path) except FileNotFoundError: contents = None - self._dispatch(lambda v: v.on_read_file(path, contents, reason)) + self._dispatch(lambda v: v.on_read_file(path, contents)) return contents - def write_file( - self, - path: PurePosixPath, - contents: str, - reason: str | None = None, - ) -> None: - self._dispatch(lambda v: v.on_write_file(path, contents, reason)) + def write_file(self, path: PurePosixPath, contents: str) -> None: + self._dispatch(lambda v: v.on_write_file(path, contents)) return self._write(path, contents) - def delete_file( - self, - path: PurePosixPath, - reason: str | None = None, - ) -> None: - self._dispatch(lambda v: v.on_delete_file(path, reason)) + def delete_file(self, path: PurePosixPath) -> None: + self._dispatch(lambda v: v.on_delete_file(path)) self._delete(path) def rename_file( self, src_path: PurePosixPath, dst_path: PurePosixPath, - reason: str | None = None, ) -> None: """Rename a single file""" - self._dispatch(lambda v: v.on_rename_file(src_path, dst_path, reason)) + self._dispatch(lambda v: v.on_rename_file(src_path, dst_path)) self._rename(src_path, dst_path) def expose_files( @@ -134,26 +117,25 @@ class ToolVisitor(Protocol): """Tool usage hook""" def on_list_files( - self, paths: Sequence[PurePosixPath], reason: str | None + self, paths: Sequence[PurePosixPath] ) -> None: ... # pragma: no cover def on_read_file( - self, path: PurePosixPath, contents: str | None, reason: str | None + self, path: PurePosixPath, contents: str | None ) -> None: ... # pragma: no cover def on_write_file( - self, path: PurePosixPath, contents: str, reason: str | None + self, path: PurePosixPath, contents: str ) -> None: ... # pragma: no cover def on_delete_file( - self, path: PurePosixPath, reason: str | None + self, path: PurePosixPath ) -> None: ... # pragma: no cover def on_rename_file( self, src_path: PurePosixPath, dst_path: PurePosixPath, - reason: str | None, ) -> None: ... # pragma: no cover def on_expose_files(self) -> None: ... # pragma: no cover @@ -266,24 +248,6 @@ def _write(self, path: PurePosixPath, contents: str) -> None: temp.close() self._write_from_disk(path, Path(temp.name)) - @override - @contextlib.contextmanager - def _expose(self) -> Iterator[Path]: - tree_sha = self.tree_sha() - commit_sha = self._repo.git( - "commit-tree", "-m", "draft! worktree", tree_sha - ).stdout - with tempfile.TemporaryDirectory() as path_str: - try: - self._repo.git( - "worktree", "add", "--detach", path_str, commit_sha - ) - path = Path(path_str) - yield path - self._sync_updates(worktree_path=path) - finally: - self._repo.git("worktree", "remove", "-f", path_str) - def _write_from_disk( self, path: PurePosixPath, contents_path: Path ) -> None: @@ -300,6 +264,23 @@ def _write_from_disk( def _delete(self, path: PurePosixPath) -> None: self._tree_updates.append(_DeleteBlob(path)) + @override + @contextlib.contextmanager + def _expose(self) -> Iterator[Path]: + commit_sha = self._repo.git( + "commit-tree", "-m", "draft! worktree", self.tree_sha() + ).stdout + with tempfile.TemporaryDirectory() as path_str: + try: + self._repo.git( + "worktree", "add", "--detach", path_str, commit_sha + ) + path = Path(path_str) + yield path + self._sync_updates(worktree_path=path) + finally: + self._repo.git("worktree", "remove", "-f", path_str) + class _TreeUpdate: """Generic tree update"""