Skip to content

AI, fully local on your computer.

12.1 KB
Raw Blame
#!/usr/bin/env bash
# Lay out a HilbertRaum portable drive (spec §6 / Phase 11).
#
# Creates the directory tree the app actually reads (workspace/, models/{chat,embeddings}/,
# model-manifests/, runtime/llama.cpp/{win,mac,linux}/, logs/, config/, docs/), copies the
# committed model manifests + user docs onto the drive, and generates config/{drive,policy}.json.
# It does NOT download model weights or sidecar binaries (git-ignored, not in the repo, R5).
#
# Canonical, unit-tested reference: apps/desktop/src/main/services/drive.ts — keep in sync.
#
# --with-assets downloads + verifies a launch-ready default asset set (Phase 12), so one
# command yields a usable drive (build-time network; the app stays offline). To keep setup
# fast the default set is small but complete for the core features: the default chat model
# (Ministral 3 8B), the embeddings model, the reranker, the Whisper transcriber model, and
# the Qwen2.5-VL image-description model (GGUF + mmproj), PLUS both sidecar runtimes
# (llama.cpp + whisper.cpp) and the OCR language files (deu/eng traineddata plus the osd
# orientation data, for scanned-PDF and photo text recognition, Phase 38 — OS-independent, ~8 MB). The user downloads any
# other models (larger chat models) from inside the app. Pass --all-models to fetch every
# model instead (the sidecar runtimes + OCR files are fetched either way). The OPTIONAL
# knowledge-pack tools (kiwix-tools, #339 P8-3) are never fetched here — run
# `fetch-runtime.sh --family kiwix_tools` separately once you want ZIM knowledge packs.
#
# Usage:
#   scripts/prepare-drive.sh --target /Volumes/HILBERTRAUM [--dry-run] [--force] \
#       [--dev] [--with-assets] [--all-models] [--accept-license]
set -euo pipefail

# The models --with-assets provisions by default (fast setup): the default chat model plus
# the embeddings model, reranker, Whisper transcriber, and the Qwen2.5-VL image-description
# model, so chat, document Q&A, retrieval quality, audio/dictation, and image understanding
# all work out of the box. Every OTHER model (larger chat models) is downloaded by the user
# from inside the app. Pass --all-models to fetch everything. The whisper.cpp runtime and the
# OCR language files are fetched alongside these (see the --with-assets block). Keep these ids
# in sync with the manifests under model-manifests/.
DEFAULT_MODEL_IDS=(
  ministral3-8b-instruct-2512-q4   # chat (benchmark-winning 8B)
  multilingual-e5-small-q8         # embeddings (document Q&A)
  bge-reranker-v2-m3-f16           # reranker (retrieval quality)
  whisper-small-multilingual       # transcriber (audio / dictation)
  qwen2.5-vl-3b-instruct-q4        # vision (image description; GGUF + mmproj, two files)
)

TARGET=""
DRY_RUN=0
FORCE=0
DEV=0
WITH_ASSETS=0
ALL_MODELS=0
ACCEPT_LICENSE=0

while [[ $# -gt 0 ]]; do
  case "$1" in
    --target) TARGET="${2:-}"; shift 2 ;;
    --target=*) TARGET="${1#*=}"; shift ;;
    --dry-run) DRY_RUN=1; shift ;;
    --force) FORCE=1; shift ;;
    --dev) DEV=1; shift ;;
    --with-assets) WITH_ASSETS=1; shift ;;
    --all-models) ALL_MODELS=1; shift ;;
    --accept-license) ACCEPT_LICENSE=1; shift ;;
    -h|--help) grep '^#' "$0" | sed 's/^# \{0,1\}//'; exit 0 ;;
    *) echo "Unknown argument: $1" >&2; exit 2 ;;
  esac
done

if [[ -z "$TARGET" ]]; then
  echo "Error: --target <drive-root> is required" >&2
  exit 2
fi

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REPO_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"

# Directory tree (must match drive.ts DRIVE_LAYOUT_DIRS / sidecar.ts llamaOsDir).
DIRS=(
  workspace
  app-skills
  user-skills
  models/chat
  models/embeddings
  models/reranker
  models/transcriber
  models/vision
  models/translation
  model-manifests
  model-manifests/vision
  model-manifests/translation
  runtime/llama.cpp/win
  runtime/llama.cpp/mac
  runtime/llama.cpp/linux
  runtime/whisper.cpp/win
  runtime/whisper.cpp/mac
  runtime/whisper.cpp/linux
  # Third sidecar family, OPTIONAL -- #339 P8-3: the kiwix-tools knowledge-pack tools. Never
  # fetched by --with-assets -- run fetch-runtime.sh --family kiwix_tools separately.
  runtime/kiwix-tools/win
  runtime/kiwix-tools/mac
  runtime/kiwix-tools/linux
  ocr
  zim
  logs
  config
  docs
)

if [[ $DEV -eq 1 ]]; then
  ENC_REQUIRED=false; PLAINTEXT=true; ALLOW_UNVERIFIED=true; REQUIRE_SHA=false; ALLOW_LOCAL_API=true
else
  # Local API ships policy-DISABLED explicitly on commercial drives (owner decision O4,
  # 2026-08-18) — the drive's policy author flips it on deliberately.
  ENC_REQUIRED=true; PLAINTEXT=false; ALLOW_UNVERIFIED=false; REQUIRE_SHA=true; ALLOW_LOCAL_API=false
fi

CREATED_AT="$(date -u +%Y-%m-%dT%H:%M:%SZ)"

echo "Prepare drive at: $TARGET"
[[ $DRY_RUN -eq 1 ]] && echo "(dry run — nothing will be created)"
echo

echo "Directories:"
for d in "${DIRS[@]}"; do
  full="$TARGET/$d"
  if [[ $DRY_RUN -eq 1 ]]; then
    echo "  + $full"
  else
    mkdir -p "$full"
    echo "  ok $d"
  fi
done
echo

echo "Model manifests:"
if [[ -d "$REPO_ROOT/model-manifests" ]]; then
  if [[ $DRY_RUN -eq 1 ]]; then
    echo "  copy $REPO_ROOT/model-manifests -> $TARGET/model-manifests"
  else
    cp -R "$REPO_ROOT/model-manifests/." "$TARGET/model-manifests/"
    echo "  copied manifests to model-manifests/"
  fi
else
  echo "  WARNING: $REPO_ROOT/model-manifests not found (run from a repo clone)"
fi
echo

# Copy the committed product skills (text-only: SKILL.md + JSON schemas + examples) from the
# repo app-skills/ tree onto the drive, the same wholesale copy as model-manifests/ (skills
# plan S9 / DS17). user-skills/ is left EMPTY (the buyer fills it).
echo "App skills:"
if [[ -d "$REPO_ROOT/app-skills" ]]; then
  if [[ $DRY_RUN -eq 1 ]]; then
    echo "  copy $REPO_ROOT/app-skills -> $TARGET/app-skills (product skills)"
  else
    cp -R "$REPO_ROOT/app-skills/." "$TARGET/app-skills/"
    echo "  copied app skills to app-skills/"
  fi
else
  echo "  WARNING: $REPO_ROOT/app-skills not found (run from a repo clone)"
fi
echo

echo "User docs:"
for rel in docs/user-guide.md docs/troubleshooting.md PRIVACY.md; do
  src="$REPO_ROOT/$rel"
  if [[ -f "$src" ]]; then
    if [[ $DRY_RUN -eq 1 ]]; then echo "  copy $rel -> docs/"
    else cp "$src" "$TARGET/docs/"; echo "  copied $rel"; fi
  fi
done
echo

# License / attribution notices (LIC-1, full-audit 2026-07-12b): distribution-level
# notices go to the DRIVE ROOT (not docs/) — the app's own GPLv3 text, the bundled-
# npm-package notices, and the GENERATED drive-wide notices for runtime binaries +
# model weights (regenerate with `node scripts/generate-drive-notices.mjs`; the
# committed file is copied here so no Node is needed at drive-build time). Copied
# unconditionally — dev drives get them too (harmless). The commercial SELLABLE gate
# (build-commercial-drive step 7 / assertCommercialDrive) requires all three. Keep this
# list in sync with commercial-drive.ts DRIVE_LICENSE_ARTIFACTS (script-drift test).
LICENSE_ARTIFACTS=(
  LICENSE
  THIRD-PARTY-NOTICES.md
  DRIVE-NOTICES.md
)
echo "License notices:"
for lic in "${LICENSE_ARTIFACTS[@]}"; do
  src="$REPO_ROOT/$lic"
  if [[ -f "$src" ]]; then
    if [[ $DRY_RUN -eq 1 ]]; then echo "  copy $lic -> drive root"
    else cp "$src" "$TARGET/$lic"; echo "  copied $lic"; fi
  else
    echo "  WARNING: $lic not found at the repo root (run from a repo clone)"
  fi
done
echo

write_json() {
  local rel="$1" content="$2" full="$TARGET/$1"
  if [[ $DRY_RUN -eq 1 ]]; then echo "  + $rel"; return; fi
  if [[ -f "$full" && $FORCE -eq 0 ]]; then
    echo "  skip $rel (exists; use --force to overwrite)"; return
  fi
  printf '%s\n' "$content" > "$full"
  echo "  wrote $rel"
}

DRIVE_JSON=$(cat <<EOF
{
  "product": "HilbertRaum",
  "drive_format_version": 1,
  "created_at": "$CREATED_AT",
  "edition": "lite",
  "offline_by_default": true,
  "models_dir": "models",
  "workspace_dir": "workspace",
  "allow_network_by_default": false
}
EOF
)

POLICY_JSON=$(cat <<EOF
{
  "network": {
    "allow_model_downloads": true,
    "allow_update_checks": false,
    "allow_local_api": $ALLOW_LOCAL_API
  },
  "workspace": {
    "encryption_required": $ENC_REQUIRED,
    "allow_plaintext_dev_mode": $PLAINTEXT
  },
  "models": {
    "allow_unverified_models": $ALLOW_UNVERIFIED,
    "require_manifest": true,
    "require_sha256_match": $REQUIRE_SHA
  }
}
EOF
)

echo "Config files:"
write_json "config/drive.json" "$DRIVE_JSON"
write_json "config/policy.json" "$POLICY_JSON"
echo

if [[ $WITH_ASSETS -eq 1 ]]; then
  echo "Fetching assets (build-time network; the app itself stays offline):"

  # Common model args (license + dry-run) shared by every fetch-models call below. In the
  # natural DIY invocation (--with-assets with neither --accept-license nor --dry-run — every
  # default-set model is already license-approved) this array is EMPTY, and Bash 3.2 + `set -u`
  # (macOS stock /bin/bash) aborts an unguarded empty-array expansion with "unbound variable"
  # (M23 — the same class guarded in fetch-models.sh / verify-models.sh). Expand it via the
  # `${arr[@]+"${arr[@]}"}` idiom so an empty array vanishes instead of erroring (F-03).
  COMMON_MODEL_ARGS=()
  [[ $ACCEPT_LICENSE -eq 1 ]] && COMMON_MODEL_ARGS+=(--accept-license)
  [[ $DRY_RUN -eq 1 ]] && COMMON_MODEL_ARGS+=(--dry-run)

  # A failed child ends the run with exit 1 and names the step, as the .ps1 twin does (#769):
  # under `set -e` alone it ended with the child's code and no line of its own.
  # fetch-models takes a single --only id, so the default set is fetched one id at a time;
  # --all-models fetches every manifest in one pass (a single call with no --only).
  if [[ $ALL_MODELS -eq 1 ]]; then
    bash "$SCRIPT_DIR/fetch-models.sh" --target "$TARGET" ${COMMON_MODEL_ARGS[@]+"${COMMON_MODEL_ARGS[@]}"} ||
      { echo "fetch-models failed." >&2; exit 1; }
  else
    echo "  (default set: ${DEFAULT_MODEL_IDS[*]}; pass --all-models for every model)"
    for id in "${DEFAULT_MODEL_IDS[@]}"; do
      bash "$SCRIPT_DIR/fetch-models.sh" --target "$TARGET" --only "$id" ${COMMON_MODEL_ARGS[@]+"${COMMON_MODEL_ARGS[@]}"} ||
        { echo "fetch-models failed." >&2; exit 1; }
    done
  fi

  # llama.cpp sidecar (the chat + embeddings engine) — always.
  RUNTIME_ARGS=(--target "$TARGET")
  [[ $DRY_RUN -eq 1 ]] && RUNTIME_ARGS+=(--dry-run)
  bash "$SCRIPT_DIR/fetch-runtime.sh" "${RUNTIME_ARGS[@]}" || { echo "fetch-runtime (llama.cpp) failed." >&2; exit 1; }

  # whisper.cpp sidecar (the transcriber engine) — always, to match the bundled Whisper
  # model. Best-effort: prebuilt whisper.cpp binaries exist for Windows only, so on a
  # mac/linux build host there is no build to fetch — a miss is a warning, not a failure
  # (those drives build whisper.cpp from source; see docs/packaging.md).
  if ! bash "$SCRIPT_DIR/fetch-runtime.sh" "${RUNTIME_ARGS[@]}" --family whisper_cpp; then
    echo "  note: whisper.cpp runtime not provisioned (no prebuilt build for this host — build from source on mac/linux)."
  fi

  # OCR language files (Phase 38, D32): the ocr/ asset class — plain sha256-verified
  # traineddata files, OS-independent (one run covers every shipped OS). Unlike the whisper
  # build there is nothing host-specific to miss, so this is fetched unconditionally like
  # llama.cpp (a failure aborts). Without it the DIY drive's ocr/ dir stays empty and
  # scanned-PDF/photo OCR (Phase 38) is silently unavailable — the provisioning root cause
  # of issue #59 (F-05, full audit 2026-07-16). build-commercial-drive already fetches it.
  bash "$SCRIPT_DIR/fetch-runtime.sh" "${RUNTIME_ARGS[@]}" --family ocr || { echo "fetch-runtime (ocr) failed." >&2; exit 1; }

  echo
  echo "Now capture real hashes: scripts/verify-models.sh --target \"$TARGET\" --generate"
else
  echo "Next steps (artifacts NOT provisioned without --with-assets):"
  echo "  1. Drop GGUF weights into models/chat/ and models/embeddings/ (see manifest local_path),"
  echo "     or re-run with --with-assets to download + verify them (scripts/fetch-models.sh)."
  echo "  2. Drop llama-server binaries into runtime/llama.cpp/{win,mac,linux}/ (or --with-assets)."
  echo "  3. Run scripts/verify-models.sh --target \"$TARGET\" to verify checksums."
fi
echo
echo "Launch from the drive with HILBERTRAUM_DRIVE_ROOT set to the drive root:"
echo "  export HILBERTRAUM_DRIVE_ROOT=\"$TARGET\""