Skip to content

World's first open-source, agentic video production system. 12 production pipelines, 100+ tools, 700+ agent skill and production-knowledge files. Turn your AI coding assistant into a full video production studio.

24.2 KB
Raw Blame
"""Base tool class implementing the expanded ToolContract.

Every tool in OpenMontage inherits from BaseTool. This enforces a uniform
interface for discovery, execution, cost estimation, and health reporting.
"""

from __future__ import annotations

import functools
import hashlib
import inspect
import json
import os
import platform
import subprocess
import shutil
import time
from abc import ABC, abstractmethod
from dataclasses import dataclass, field
from enum import Enum
from pathlib import Path
from typing import Any, Callable, Optional


def _load_dotenv() -> None:
    """Load .env into os.environ once at import time.

    This ensures API keys are available before any tool is instantiated,
    even when tools are imported directly without going through the registry.
    Only sets variables that are not already in the environment.
    """
    env_path = Path(__file__).resolve().parent.parent / ".env"
    if not env_path.is_file():
        return
    import re

    with open(env_path, encoding="utf-8", errors="ignore") as f:
        for line in f:
            line = line.strip()
            if not line or line.startswith("#") or "=" not in line:
                continue
            key, _, value = line.partition("=")
            key = key.strip()
            value = value.strip()
            # Quoted value: take the content inside the quotes verbatim.
            if value[:1] in ("'", '"'):
                quote = value[0]
                end = value.find(quote, 1)
                value = value[1:end] if end != -1 else value[1:]
            else:
                # Strip an inline comment ('#' at line start or after
                # whitespace) so "VAR=   # note" yields "" not "# note".
                match = re.search(r"(^|\s)#", value)
                if match:
                    value = value[: match.start()]
                value = value.strip()
            if key and key not in os.environ:
                os.environ[key] = value


_load_dotenv()


class ToolTier(str, Enum):
    CORE = "core"
    VOICE = "voice"
    ENHANCE = "enhance"
    GENERATE = "generate"
    SOURCE = "source"
    ANALYZE = "analyze"
    PUBLISH = "publish"


class ToolStability(str, Enum):
    EXPERIMENTAL = "experimental"
    BETA = "beta"
    PRODUCTION = "production"


class ToolStatus(str, Enum):
    AVAILABLE = "available"
    UNAVAILABLE = "unavailable"
    DEGRADED = "degraded"


class ToolRuntime(str, Enum):
    """Where and how a tool executes."""

    LOCAL = "local"  # Runs entirely on-device, free, no network
    LOCAL_GPU = "local_gpu"  # Runs on-device but needs GPU (VRAM)
    API = "api"  # Calls an external API, requires API key, costs money
    HYBRID = "hybrid"  # Can run locally OR via API (e.g., image_selector)


class ExecutionMode(str, Enum):
    SYNC = "sync"
    ASYNC = "async"


class Determinism(str, Enum):
    DETERMINISTIC = "deterministic"
    SEEDED = "seeded"
    STOCHASTIC = "stochastic"


class ResumeSupport(str, Enum):
    NONE = "none"
    FROM_START = "from_start"
    FROM_CHECKPOINT = "from_checkpoint"


@dataclass
class ResourceProfile:
    """Hardware resource envelope for a tool."""

    cpu_cores: int = 1
    ram_mb: int = 512
    vram_mb: int = 0
    disk_mb: int = 100
    network_required: bool = False


@dataclass
class RetryPolicy:
    """Safe retry behavior for a tool."""

    max_retries: int = 0
    backoff_seconds: float = 1.0
    retryable_errors: list[str] = field(default_factory=list)


@dataclass
class ToolResult:
    """Standard result returned by tool execution."""

    success: bool
    data: dict[str, Any] = field(default_factory=dict)
    artifacts: list[str] = field(default_factory=list)
    error: Optional[str] = None
    cost_usd: Optional[float] = 0.0
    duration_seconds: float = 0.0
    seed: Optional[int] = None
    model: Optional[str] = None
    from_cache: bool = False


import threading as _threading

# Shared nesting counter for instrumented execute() calls (thread-local so
# parallel tool threads don't see each other's depth).
_EXECUTE_DEPTH = _threading.local()


def _instrument_execute(fn: Callable) -> Callable:
    """Wrap a tool's execute() with Backlot event emission.

    Appends start/finish/error entries to the owning project's events.jsonl
    when the call can be attributed to a project (explicit project_dir input
    or any path input under projects/). Powers the board's live activity
    ticker and per-scene generating states with zero agent involvement.

    Instrumentation is strictly non-fatal: any failure inside the event layer
    is swallowed and the tool call proceeds untouched.
    """
    if getattr(fn, "_backlot_instrumented", False):
        return fn

    depth_state = _EXECUTE_DEPTH  # shared across all tools (selector → provider)

    @functools.wraps(fn)
    def wrapper(self, inputs: Any, *args: Any, **kwargs: Any):
        # Event layer is fully optional: if it can't import, run untouched.
        try:
            from lib.events import emit_event, infer_project_dir
        except Exception:
            return fn(self, inputs, *args, **kwargs)

        tool_name = getattr(self, "name", "") or self.__class__.__name__
        scene_id = inputs.get("scene_id") if isinstance(inputs, dict) else None
        output_path = inputs.get("output_path") if isinstance(inputs, dict) else None
        # Nesting depth: selector tools delegate to provider tools' execute().
        # Both emit (the ticker wants the provider name too), but depth lets
        # consumers dedupe — e.g. sum cost_usd only at depth 0.
        depth = getattr(depth_state, "value", 0)
        depth_state.value = depth + 1
        project_dir = infer_project_dir(inputs)

        base = {
            "tool": tool_name,
            "scene_id": scene_id,
            "depth": depth if depth else None,
        }
        if project_dir is not None:
            emit_event(
                project_dir,
                {
                    **base,
                    "event": "start",
                    "output_path": str(output_path) if output_path else None,
                },
            )

        started = time.monotonic()
        try:
            result = fn(self, inputs, *args, **kwargs)
        except Exception as exc:
            if project_dir is not None:
                emit_event(
                    project_dir,
                    {
                        **base,
                        "event": "error",
                        "error": str(exc)[:300],
                        "duration_s": round(time.monotonic() - started, 2),
                    },
                )
            raise
        finally:
            depth_state.value = depth

        if project_dir is None:
            # The tool may have created its own project dir during execute
            # (first call of a run) — attribute the finish if possible.
            project_dir = infer_project_dir(inputs)
        if project_dir is not None:
            cost = getattr(result, "cost_usd", None)
            emit_event(
                project_dir,
                {
                    **base,
                    "event": "finish",
                    "output_path": str(output_path) if output_path else None,
                    "success": getattr(result, "success", None),
                    # NOTE: 0.0 is meaningful (ran for free) — only None is dropped.
                    "cost_usd": cost if isinstance(cost, (int, float)) else None,
                    "duration_s": round(time.monotonic() - started, 2),
                },
            )
        return result

    wrapper._backlot_instrumented = True  # type: ignore[attr-defined]
    return wrapper


_FILE_HASH_CHUNK = 1024 * 1024


def _hash_file(path: Path) -> Optional[str]:
    """SHA-256 of a file's bytes, read in chunks. None if it can't be read."""
    try:
        h = hashlib.sha256()
        with open(path, "rb") as f:
            for chunk in iter(lambda: f.read(_FILE_HASH_CHUNK), b""):
                h.update(chunk)
        return h.hexdigest()
    except OSError:
        return None


def _fingerprint_value(value: Any) -> Any:
    """Make one cache-key value content-addressed.

    A value that points at an existing file is replaced by a hash of the file's
    bytes, so rewriting the file at the same path changes the key. A path string
    on its own would not, which would let a rerun restore output derived from
    stale input bytes. Lists and dicts are walked so nested asset paths are
    covered too; everything else passes through unchanged.
    """
    if isinstance(value, (str, os.PathLike)):
        try:
            path = Path(value)
            if path.is_file():
                digest = _hash_file(path)
                if digest is not None:
                    return {"__file_sha256__": digest}
        except (OSError, ValueError):
            pass
        return value
    if isinstance(value, (list, tuple)):
        return [_fingerprint_value(v) for v in value]
    if isinstance(value, dict):
        return {k: _fingerprint_value(v) for k, v in value.items()}
    return value


def _is_cacheable(tool: "BaseTool", inputs: dict[str, Any]) -> bool:
    """Whether a call may be served from / written to the asset cache.

    Cache only reproducible, single-output calls whose sole side effect is the
    file they write: a declared idempotency contract, a concrete output_path to
    materialize into, not a publisher, and a determinism class that guarantees
    the same bytes: deterministic always, seeded only when a seed is pinned
    (an unpinned seed is effectively stochastic), stochastic never.
    """
    if not tool.idempotency_key_fields:
        return False
    if not inputs.get("output_path"):
        return False
    if tool.tier == ToolTier.PUBLISH:
        return False
    det = tool.determinism
    if det == Determinism.STOCHASTIC:
        return False
    if det == Determinism.SEEDED and inputs.get("seed") is None:
        return False
    return True


def _result_from_cache(restored: dict[str, Any], dest: Any) -> "ToolResult":
    """Build a ToolResult for a cache hit that was materialized to ``dest``."""
    data = dict(restored.get("data") or {})
    # Repoint the stored output reference to where the caller asked for it.
    if "output" in data:
        data["output"] = str(dest)
    data["cached"] = True
    return ToolResult(
        success=True,
        data=data,
        artifacts=[str(dest)],
        cost_usd=0.0,
        duration_seconds=0.0,
        seed=restored.get("seed"),
        model=restored.get("model"),
        from_cache=True,
    )


def _cache_execute(fn: Callable) -> Callable:
    """Wrap a tool's execute() with content-addressed output caching.

    When the asset cache is enabled and the call is reproducible, a repeat of a
    prior call links the cached artifact back into the requested output path and
    returns a free ToolResult without invoking fn or paying a provider. On a
    miss, fn runs and a single-file success is ingested for next time.

    The cache layer is strictly optional and non-fatal: any failure importing or
    using it falls through to the live call untouched. It is also off unless
    OPENMONTAGE_ASSET_CACHE is set, so an existing run behaves exactly as before.
    """
    if getattr(fn, "_asset_cached", False):
        return fn

    @functools.wraps(fn)
    def wrapper(self, inputs: Any, *args: Any, **kwargs: Any):
        try:
            from lib.asset_cache import asset_cache_enabled, get_default_asset_cache
        except Exception:
            return fn(self, inputs, *args, **kwargs)

        if not asset_cache_enabled() or not isinstance(inputs, dict):
            return fn(self, inputs, *args, **kwargs)
        if not _is_cacheable(self, inputs):
            return fn(self, inputs, *args, **kwargs)

        try:
            key = self.cache_key(inputs)
            cache = get_default_asset_cache()
        except Exception:
            return fn(self, inputs, *args, **kwargs)

        dest = inputs["output_path"]
        try:
            restored = cache.try_restore(key, Path(dest))
        except Exception:
            restored = None
        if restored is not None:
            return _result_from_cache(restored, dest)

        result = fn(self, inputs, *args, **kwargs)

        try:
            artifacts = getattr(result, "artifacts", None) or []
            if getattr(result, "success", False) and len(artifacts) == 1:
                art = Path(artifacts[0])
                if art.exists():
                    cache.store(
                        key,
                        art,
                        tool_name=self.name,
                        tool_version=self.version,
                        model=getattr(result, "model", None) or "",
                        seed=getattr(result, "seed", None),
                        cost_usd=float(getattr(result, "cost_usd", 0.0) or 0.0),
                        data=getattr(result, "data", None),
                    )
        except Exception:
            pass
        return result

    wrapper._asset_cached = True  # type: ignore[attr-defined]
    return wrapper


class BaseTool(ABC):
    """Abstract base class for all OpenMontage tools."""

    def __init_subclass__(cls, **kwargs: Any) -> None:
        """Auto-instrument every concrete execute() with Backlot events."""
        super().__init_subclass__(**kwargs)
        impl = cls.__dict__.get("execute")
        if impl is not None and not getattr(impl, "__isabstractmethod__", False):
            # Cache is outermost: a hit skips both the live call and its event
            # emission. Instrumentation wraps the raw implementation beneath it.
            cls.execute = _cache_execute(_instrument_execute(impl))

    # --- Identity (override in subclasses) ---
    name: str = ""
    version: str = "0.1.0"
    tier: ToolTier = ToolTier.CORE
    stability: ToolStability = ToolStability.EXPERIMENTAL
    execution_mode: ExecutionMode = ExecutionMode.SYNC
    determinism: Determinism = Determinism.DETERMINISTIC
    runtime: ToolRuntime = ToolRuntime.LOCAL

    # --- Dependencies ---
    # For API tools, add "env:ENVVAR_NAME" to signal required API keys
    dependencies: list[str] = []
    install_instructions: str = ""

    # --- Capabilities ---
    capability: str = "generic"
    provider: str = "openmontage"
    capabilities: list[str] = []
    input_schema: dict = {}
    output_schema: dict = {}
    artifact_schema: dict = {}
    progress_schema: Optional[dict] = None
    supports: dict[str, Any] = {}
    best_for: list[str] = []
    not_good_for: list[str] = []
    provider_matrix: dict[str, Any] = {}

    # --- Resource & retry ---
    resource_profile: ResourceProfile = ResourceProfile()
    retry_policy: RetryPolicy = RetryPolicy()

    # --- Resume & idempotency ---
    resume_support: ResumeSupport = ResumeSupport.NONE
    idempotency_key_fields: list[str] = []

    # --- Side effects & fallback ---
    side_effects: list[str] = []
    fallback: Optional[str] = None
    fallback_tools: list[str] = []

    # --- Agent skills (Layer 3 references) ---
    # Names of installed agent skills in .agents/skills/ that teach the
    # underlying technology. The orchestrator uses these to load relevant
    # API knowledge when planning tool usage.
    agent_skills: list[str] = []

    # --- Verification ---
    user_visible_verification: list[str] = []

    # --- Optional telemetry / quality hints for the scoring engine ---
    # If set (0.0-1.0), lib/scoring.py uses these directly instead of falling
    # back to stability-based heuristics. Leave unset unless the tool has a
    # real measured or well-calibrated value.
    quality_score: Optional[float] = None
    historical_success_rate: Optional[float] = None
    latency_p50_seconds: Optional[float] = None

    # ---- Status reporting ----

    def get_status(self) -> ToolStatus:
        """Check if this tool's dependencies are satisfied."""
        try:
            self.check_dependencies()
            return ToolStatus.AVAILABLE
        except DependencyError:
            return ToolStatus.UNAVAILABLE

    def check_dependencies(self) -> None:
        """Verify all dependencies are installed. Raises DependencyError if not."""
        for dep in self.dependencies:
            if dep.startswith(("cmd:", "binary:")):
                prefix = "cmd:" if dep.startswith("cmd:") else "binary:"
                cmd_name = dep[len(prefix) :]
                if shutil.which(cmd_name) is None:
                    raise DependencyError(
                        f"Command {cmd_name!r} not found. {self.install_instructions}"
                    )
            elif dep.startswith("env:"):
                env_name = dep[4:]
                if not os.environ.get(env_name):
                    raise DependencyError(
                        f"Environment variable {env_name!r} not set. {self.install_instructions}"
                    )
            elif dep.startswith("python:"):
                module_name = dep[7:]
                try:
                    __import__(module_name)
                except ImportError:
                    raise DependencyError(
                        f"Python module {module_name!r} not installed. {self.install_instructions}"
                    )

    def get_info(self) -> dict[str, Any]:
        """Return full tool contract info for registry/discovery."""
        usage_location = inspect.getfile(self.__class__)
        return {
            "name": self.name,
            "version": self.version,
            "tier": self.tier.value,
            "capability": self.capability,
            "provider": self.provider,
            "stability": self.stability.value,
            "status": self.get_status().value,
            "execution_mode": self.execution_mode.value,
            "determinism": self.determinism.value,
            "runtime": self.runtime.value,
            "module_path": self.__class__.__module__,
            "usage_location": usage_location,
            "dependencies": self.dependencies,
            "install_instructions": self.install_instructions,
            "capabilities": self.capabilities,
            "input_schema": self.input_schema,
            "output_schema": self.output_schema,
            "artifact_schema": self.artifact_schema,
            "supports": self.supports,
            "best_for": self.best_for,
            "not_good_for": self.not_good_for,
            "provider_matrix": self.provider_matrix,
            "resource_profile": {
                "cpu_cores": self.resource_profile.cpu_cores,
                "ram_mb": self.resource_profile.ram_mb,
                "vram_mb": self.resource_profile.vram_mb,
                "disk_mb": self.resource_profile.disk_mb,
                "network_required": self.resource_profile.network_required,
            },
            "resume_support": self.resume_support.value,
            "side_effects": self.side_effects,
            "fallback": self.fallback,
            "fallback_tools": self.fallback_tools
            or ([self.fallback] if self.fallback else []),
            "agent_skills": self.agent_skills,
            "related_skills": self.agent_skills,
            "user_visible_verification": self.user_visible_verification,
            "quality_score": self.quality_score,
            "historical_success_rate": self.historical_success_rate,
            "latency_p50_seconds": self.latency_p50_seconds,
        }

    # ---- Cost estimation ----

    def estimate_cost(self, inputs: dict[str, Any]) -> float:
        """Estimate cost in USD for the given inputs. Override for paid tools."""
        return 0.0

    def estimate_runtime(self, inputs: dict[str, Any]) -> float:
        """Estimate runtime in seconds. Override for long-running tools."""
        return 0.0

    # ---- Idempotency ----

    def idempotency_key(self, inputs: dict[str, Any]) -> str:
        """Compute a cache key from idempotency fields."""
        key_data = {k: inputs.get(k) for k in self.idempotency_key_fields}
        raw = json.dumps(key_data, sort_keys=True)
        return hashlib.sha256(raw.encode()).hexdigest()[:16]

    def cache_key(self, inputs: dict[str, Any]) -> str:
        """Content-addressed key for this call, used by the asset cache.

        Combines tool identity and version with the declared
        ``idempotency_key_fields`` so identical inputs to the same tool version
        resolve to the same key, while a version bump or a different tool never
        collides. Returns the full SHA-256 hex; the cache stores blobs under it.

        File-valued fields are fingerprinted by content, not by path string, so
        rewriting an input file at the same path changes the key rather than
        serving output derived from the old bytes. Output-path fields are not in
        this set, so the same content is a hit regardless of where the caller
        wants the file to land.
        """
        key_data = {
            "tool": self.name,
            "version": self.version,
            "fields": {
                k: _fingerprint_value(inputs.get(k))
                for k in self.idempotency_key_fields
            },
        }
        raw = json.dumps(key_data, sort_keys=True, default=str)
        return hashlib.sha256(raw.encode()).hexdigest()

    # ---- Execution ----

    @abstractmethod
    def execute(self, inputs: dict[str, Any]) -> ToolResult:
        """Run the tool. Subclasses must implement this."""
        ...

    def dry_run(self, inputs: dict[str, Any]) -> dict[str, Any]:
        """Preflight check without side effects. Override for paid/publishing tools."""
        from tools.provider_pricing import PriceQuoteRequired

        quote_error = None
        try:
            estimated_cost = self.estimate_cost(inputs)
        except PriceQuoteRequired as exc:
            estimated_cost = None
            quote_error = str(exc)
        return {
            "tool": self.name,
            "estimated_cost_usd": estimated_cost,
            "cost_status": "quote_required" if quote_error else "estimated",
            "quote_reason": quote_error,
            "estimated_runtime_seconds": self.estimate_runtime(inputs),
            "status": self.get_status().value,
            "would_execute": True,
        }

    # ---- CLI helper ----

    def run_command(
        self,
        cmd: list[str],
        *,
        timeout: Optional[int] = None,
        cwd: Optional[Path] = None,
    ) -> subprocess.CompletedProcess:
        """Run a subprocess command with standard error handling.

        On Windows, resolves .cmd/.bat wrappers (e.g. npx, npm) via
        shutil.which() so subprocess.run() can find them without shell=True.
        """
        resolved_cmd = list(cmd)
        if platform.system() == "Windows" and resolved_cmd:
            exe = shutil.which(resolved_cmd[0])
            if exe:
                resolved_cmd[0] = exe
        try:
            return subprocess.run(
                resolved_cmd,
                capture_output=True,
                text=True,
                # Force UTF-8 decoding. The default uses the OS locale (cp1252 on
                # Windows), which raises UnicodeDecodeError on a subprocess that
                # emits Unicode/emoji (e.g. Remotion's progress output), killing the
                # reader thread and potentially swallowing the real error text.
                encoding="utf-8",
                errors="replace",
                timeout=timeout,
                cwd=cwd,
                check=True,
            )
        except subprocess.CalledProcessError as exc:
            stderr = (exc.stderr or "").strip()
            stdout = (exc.stdout or "").strip()
            detail = stderr or stdout or str(exc)
            raise ToolCommandError(
                exc.returncode,
                exc.cmd,
                output=exc.output,
                stderr=exc.stderr,
                detail=detail,
            ) from exc


class ToolCommandError(subprocess.CalledProcessError):
    """CalledProcessError with stderr/stdout surfaced in str(error)."""

    def __init__(
        self,
        returncode: int,
        cmd: list[str],
        *,
        output: Optional[str] = None,
        stderr: Optional[str] = None,
        detail: str = "",
    ) -> None:
        super().__init__(returncode, cmd, output=output, stderr=stderr)
        self.detail = detail

    def __str__(self) -> str:
        base = super().__str__()
        if self.detail:
            return f"{base}\n{self.detail}"
        return base


class DependencyError(Exception):
    """Raised when a tool's dependency is not satisfied."""

    pass