#!/usr/bin/env bash set -euo pipefail repository=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd) inherited_names=() inherited_values=() for setting in DIM_OAUTH_CLIENT_ID_CN DIM_OAUTH_CLIENT_ID_GLOBAL DIMCODE_HOME DIMCODE_MODEL_CONFIG DIMCODE_PROVIDER DIMCODE_MODEL DIMCODE_WEB_ASSETS DIMCODE_DEFAULT_PLUGINS; do if declare -p "$setting" >/dev/null 2>&1; then inherited_names+=("$setting") inherited_values+=("${!setting}") fi done if [[ -f "$repository/.env.dev" ]]; then set -a source "$repository/.env.dev" set +a fi for ((index=0; index<${#inherited_names[@]}; index++)); do printf -v "${inherited_names[$index]}" '%s' "${inherited_values[$index]}" export "${inherited_names[$index]}" done action=run if [[ $# -gt 0 ]]; then case "$1" in restart|stop|check|help) action=$1; shift ;; esac fi if [[ "$action" == help ]]; then cat <<'USAGE' Usage: ./dev [CLI arguments] ./dev restart [CLI arguments] | stop | check ./dev migration With DIM_OAUTH_CLIENT_ID_CN and DIM_OAUTH_CLIENT_ID_GLOBAL set, dimcode is built as the account build; otherwise as the API-key build. Use --model-config PATH for independent API-key development. A run that can start the daemon stages the default plugins into staging/dev-plugins when Node and npm are available, and passes them on. Migration follows the built version's channel; alpha imports .dimcode/v2 into .dimcode-alpha. See docs/design/cli-commands.md for migration routing. Other commands use the isolated development home. Optional local configuration: .env.dev (see .env.example). A new development home runs commands without OS isolation: the Sandbox switch defaults to off, and no host-isolation.json is written. USAGE exit 0 fi if [[ "$action" == check ]]; then [[ $# == 0 ]] || { echo 'check takes no arguments.' >&2; exit 1; } ( cd -- "$repository" cargo fmt --all -- --check --config error_on_line_overflow=true cargo clippy --locked -p dim-cli -p dim-app-server -p dim-oauth -p dimcode --all-targets -- -D warnings ) >&2 exit 0 fi caller_args=("$@") model_config=${DIMCODE_MODEL_CONFIG:-} product_home=${DIMCODE_HOME:-} explicit_model_config=false explicit_selection=false chat=true migration=false explicit_home=false native_help=false resumed=false while [[ $# -gt 0 ]]; do case "$1" in --model-config|--home) [[ $# -ge 2 ]] || { echo "$1 requires a path." >&2; exit 1; } if [[ "$1" == --model-config ]]; then model_config=$2 explicit_model_config=true else product_home=$2 explicit_home=true fi shift ;; --provider|--model) [[ $# -ge 2 ]] || { echo "$1 requires a value." >&2; exit 1; } explicit_selection=true shift ;; --resume) [[ $# -ge 2 ]] || { echo '--resume requires a value.' >&2; exit 1; } resumed=true shift ;; --source) [[ $# -ge 2 ]] || { echo "$1 requires a path." >&2; exit 1; } shift ;; --prompt|-p|--cwd|--server|--directory|--redirect-uri) [[ $# -ge 2 ]] || { echo "$1 requires a value." >&2; exit 1; } shift ;; login|account|logout|models|daemon|web) chat=false ;; migration) chat=false; migration=true ;; --help|-h|--version|-V) native_help=true ;; esac shift done if [[ -z "$product_home" ]]; then [[ -n "${HOME:-}" ]] || { echo 'Set DIMCODE_HOME when HOME is absent.' >&2; exit 1; } scope=$(printf '%s' "$repository" | git hash-object --stdin) product_home="$HOME/.dimcode-dev/${scope:0:10}" fi binary="$repository/target/debug/dimcode" if [[ "$action" == stop ]]; then [[ ${#caller_args[@]} == 0 ]] || { echo 'Use DIMCODE_HOME to select the home for stop.' >&2; exit 1; } if [[ ! -x "$binary" ]]; then if [[ ! -e "$product_home/rust" ]]; then echo 'Daemon is not running.' >&2 exit 0 fi echo 'A built CLI is required to stop an existing development home. Run ./dev first.' >&2 exit 1 fi exec "$binary" --home "$product_home" daemon stop fi options=() if [[ "$migration" == false || "$explicit_home" == true ]]; then options+=(--home "$product_home") fi if [[ "$native_help" == false && "$migration" == false && -n "$model_config" && "$explicit_model_config" == false ]]; then options+=(--model-config "$model_config") fi if [[ "$chat" == true && "$resumed" == false && "$explicit_selection" == false ]]; then if [[ -n "${DIMCODE_PROVIDER:-}" || -n "${DIMCODE_MODEL:-}" ]]; then [[ -n "${DIMCODE_PROVIDER:-}" && -n "${DIMCODE_MODEL:-}" ]] || { echo 'Set DIMCODE_PROVIDER and DIMCODE_MODEL together.' >&2 exit 1 } options+=(--provider "$DIMCODE_PROVIDER" --model "$DIMCODE_MODEL") fi fi # A build cache that shares recorded build-script results between checkouts # replays the first commit it saw for this crate, because a script's declared # inputs are package-local. Resolving HEAD here and passing it keeps the # version label naming the tree that runs. DIM_BUILD_COMMIT=$(git -C "$repository" rev-parse --short=8 HEAD 2>/dev/null) || DIM_BUILD_COMMIT= (cd -- "$repository" && DIM_BUILD_COMMIT="$DIM_BUILD_COMMIT" cargo build --locked -p dimcode) >&2 # The run path can start the daemon; only --help/--version, migration and # `daemon stop` cannot, and they stage nothing. if [[ "$native_help" == false && "$migration" == false && -z "${DIMCODE_DEFAULT_PLUGINS:-}" ]]; then if [[ "${caller_args[0]:-}" == daemon && "${caller_args[1]:-}" == stop ]]; then : elif ! command -v node >/dev/null 2>&1 || ! command -v npm >/dev/null 2>&1; then echo 'Node or npm is missing; running without the default plugins.' >&2 elif node "$repository/scripts/stage-plugins.mjs" "$repository/staging/dev-plugins" --update >&2; then DIMCODE_DEFAULT_PLUGINS="$repository/staging/dev-plugins/default-plugins.json" export DIMCODE_DEFAULT_PLUGINS else echo 'The default plugins were not staged; running without them.' >&2 fi fi if [[ "$native_help" == false && "$migration" == false ]]; then mkdir -p -m 700 -- "$product_home" product_home=$(cd -- "$product_home" && pwd) "$binary" --home "$product_home" daemon stop >&2 fi # Bash 3.2 (macOS) treats an empty array as unset under `set -u`. exec "$binary" ${options[@]+"${options[@]}"} ${caller_args[@]+"${caller_args[@]}"}