Skip to content

Open source CapCut alternative.

TypeScript
4.5 KB
Raw Blame
#!/usr/bin/env python3
"""Create file:// runtime manifests for local Donkey development."""

from __future__ import annotations

import json
import os
import shlex
import sys
from pathlib import Path
from typing import Any


ROOT_DIR = Path(__file__).resolve().parents[1]
PACKAGE_DIR = Path(os.environ.get("DONKEY_DEV_RUNTIME_PACKAGE_DIR", ROOT_DIR / "dist" / "LocalRuntimePackages"))
MANIFEST_DIR = Path(os.environ.get("DONKEY_DEV_RUNTIME_MANIFEST_DIR", ROOT_DIR / "dist" / "LocalRuntimeDevManifests"))
ENV_FILE = Path(os.environ.get("DONKEY_DEV_RUNTIME_ENV_FILE", ROOT_DIR / "dist" / "donkey-dev-runtime-manifests.env"))

RUNTIME_ENV_NAMES = {
    "parakeet-transcriber": "DONKEY_PARAKEET_RUNTIME_MANIFEST_URL",
    "ui-understander": "DONKEY_UI_UNDERSTANDER_RUNTIME_MANIFEST_URL",
    "local-llm": "DONKEY_LOCAL_LLM_RUNTIME_MANIFEST_URL",
}


def main() -> int:
    if not PACKAGE_DIR.exists():
        print(
            f"Runtime package directory not found: {PACKAGE_DIR}\n"
            "Run ./scripts/package-donkey-app.sh once to create dist/LocalRuntimePackages.",
            file=sys.stderr,
        )
        return 2

    manifest_urls: dict[str, str] = {}
    warnings: list[str] = []
    for manifest_path in sorted(PACKAGE_DIR.glob("*/manifest.json")):
        manifest = json.loads(manifest_path.read_text())
        runtime_id = str(manifest.get("runtimeID") or manifest_path.parent.name)
        package_root = manifest_path.parent

        files = manifest.get("files")
        if not isinstance(files, list):
            print(f"Manifest has no files array: {manifest_path}", file=sys.stderr)
            return 2
        for item in files:
            if not isinstance(item, dict):
                print(f"Manifest file entry is not an object in {manifest_path}", file=sys.stderr)
                return 2
            relative_path = str(item.get("relativePath") or "")
            if not relative_path:
                print(f"Manifest file entry is missing relativePath in {manifest_path}", file=sys.stderr)
                return 2
            file_path = package_root / relative_path
            if not file_path.exists():
                print(f"Manifest references missing local file: {file_path}", file=sys.stderr)
                return 2
            item["downloadURL"] = file_path.resolve().as_uri()

        metadata = manifest.get("metadata")
        if runtime_id == "local-llm" and isinstance(metadata, dict):
            if not str(metadata.get("modelWeights.downloadURL") or "").strip():
                warnings.append(
                    "local-llm manifest has no modelWeights.downloadURL; setup will install the sidecar "
                    "but model preparation/health will fail until DONKEY_LOCAL_LLM_MODEL_URL is provided "
                    "or scripts/run-donkey-dev.sh refreshes the local runtime packages."
                )
            if not str(metadata.get("modelWeights.sha256") or "").strip():
                warnings.append(
                    "local-llm manifest has no modelWeights.sha256; setup cannot verify downloaded model "
                    "weights until DONKEY_LOCAL_LLM_MODEL_SHA256 is provided or the dev package defaults "
                    "are refreshed."
                )

        dev_manifest_path = MANIFEST_DIR / runtime_id / "manifest.json"
        dev_manifest_path.parent.mkdir(parents=True, exist_ok=True)
        dev_manifest_path.write_text(json.dumps(manifest, indent=2, sort_keys=True) + "\n")
        manifest_urls[runtime_id] = dev_manifest_path.resolve().as_uri()

    if not manifest_urls:
        print(f"No runtime manifests found under {PACKAGE_DIR}", file=sys.stderr)
        return 2

    ENV_FILE.parent.mkdir(parents=True, exist_ok=True)
    joined_manifest_urls = ",".join(f"{runtime_id}={url}" for runtime_id, url in sorted(manifest_urls.items()))
    lines = [
        "# Generated by scripts/create-dev-runtime-manifests.py.",
        "# This file is local build output under dist/ and should not be committed.",
        f"export DONKEY_RUNTIME_PACKAGE_MANIFEST_URLS={shlex.quote(joined_manifest_urls)}",
    ]
    for runtime_id, env_name in sorted(RUNTIME_ENV_NAMES.items()):
        url = manifest_urls.get(runtime_id)
        if url:
            lines.append(f"export {env_name}={shlex.quote(url)}")
    ENV_FILE.write_text("\n".join(lines) + "\n")

    print(f"Wrote {ENV_FILE}")
    print(f"Wrote dev manifests under {MANIFEST_DIR}")
    for warning in warnings:
        print(f"warning: {warning}", file=sys.stderr)
    return 0


if __name__ == "__main__":
    raise SystemExit(main())