Skip to content

Operations

chimera.core.operations defines four Protocol interfaces for the low-level I/O operations used by built-in tools. Swapping in a custom implementation lets tools work against Docker containers, remote servers, or in-memory fixtures.

ProtocolMethodsDescription
ReadOpsread_file(path), file_exists(path)File reading backend
WriteOpswrite_file(path, content)File writing backend
BashOpsrun_command(command, timeout, cwd)Command execution backend
SearchOpssearch_files(pattern, path), list_files(pattern)File search backend

All four are @runtime_checkable, so isinstance() checks work without subclassing.

ClassProtocolNotes
LocalReadOpsReadOpsopen() relative to cwd
LocalWriteOpsWriteOpsmkdir -p before writing
LocalBashOpsBashOpssubprocess.run(shell=True), returns CommandResult
LocalSearchOpsSearchOpsPath.rglob with regex matching

Each constructor accepts an optional cwd: str = "." argument.

from chimera.core.operations import ReadOps, BashOps
from chimera.types import CommandResult
class DockerReadOps:
def __init__(self, container: str) -> None:
self.container = container
def read_file(self, path: str) -> str:
import subprocess
return subprocess.check_output(
["docker", "exec", self.container, "cat", path], text=True
)
def file_exists(self, path: str) -> bool:
import subprocess
r = subprocess.run(
["docker", "exec", self.container, "test", "-f", path]
)
return r.returncode == 0
# Pass to tools that accept a read_ops parameter
from chimera.tools import ReadTool
tool = ReadTool(read_ops=DockerReadOps("my-container"))

BashOps.run_command returns a CommandResult (from chimera.types):

FieldTypeDescription
stdoutstrStandard output
stderrstrStandard error
exit_codeintProcess exit code (-1 on timeout)