Skip to content

Self-evolving Context Database for AI Agents. Unify Agent Memory, Knowledge RAG and Skills.

167 KB
Raw Blame
#!/usr/bin/env bash
#
# OpenViking Memory Plugin shared installer for Claude Code, Codex, Cursor,
# TRAE / TRAE CN, TraeCode CLI 2.0, ZCode, Kimi Code, CodeBuddy Code,
# OpenCode, pi, DeepSeek Harness (dsh) and DeepSeek Harness Desktop.
#
# In short:
#   - Open source. This script and every OpenViking file it installs are
#     built from https://github.com/volcengine/OpenViking (AGPL-3.0, 38,930
#     GitHub stars on 2026-09-29) by its GitHub Actions workflows.
#   - It installs the OpenViking memory plugin into the agents you pick and
#     points it at your OpenViking server, and installs the OpenViking CLI (ov)
#     from npm unless you uncheck it.
#   - It lists every change and asks before making any. With --yes, or without
#     a terminal, it prints the same list and proceeds. No sudo. With default
#     settings nothing is written outside $HOME except temporary files and the
#     CLI, which npm puts in its global directory (~/.local when that needs
#     sudo).
#
# Install without prompts:
#   curl -fsSL https://openviking.ai/install | bash -s -- --yes --url <server-url> --api-key <api-key>
# (https://openviking.ai/install is a short script that downloads this one and
# runs it; https://openviking.net/install serves the same.)
# The server URL and API key come from the user; without them it uses an
# existing ~/.openviking/ovcli.conf, then http://127.0.0.1:1933 without a key.
# It installs into the agents it detects (claude,codex when it detects none)
# and installs the CLI; --harness claude,codex,cli picks them, and --help lists
# every option and environment variable.
#
# Needs macOS or Linux, bash 3.2+, Node.js 18+ and curl; unzip when a harness
# installs from the plugin bundle, npm for the CLI.
#
# Hosts it contacts:
#   docs.openviking.net, docs.openviking.ai  The documentation site, which
#       serves the downloads under /dl from both hosts: this script, the plugin
#       bundle (memory-plugin-marketplace.zip), the Claude Code URL marketplace
#       and the Codex git marketplace. It asks both for one small file and
#       uses the host that answers first. Claude Code and Codex fetch their
#       marketplace from that host themselves, at install and on their own
#       update checks.
#   openviking.ai  The version check described below.
#   The OpenViking server given: GET /health, and /api/v1/system/status with
#       the API key.
#   The npm registry npm is configured with: the CLI (@openviking/cli and its
#       binary for this platform), pi's dependencies (npm ci) and the dsh
#       package (dsh plugin add, for DeepSeek Harness Desktop too).
# docs.openviking.ai is hosted on GitHub Pages. The script itself never
# contacts github.com and never runs git.
#
# What it writes:
#   every run  ~/.openviking/ovcli.conf (the previous one kept as .bak.<time>)
#   claude     claude plugin marketplace add + plugin install; ~/.claude/settings.json
#              (marketplace auto-update, statusline)
#   codex      codex plugin marketplace add + plugin add; ~/.codex/config.toml
#              (plugin enabled, plugin hooks on)
#   trae-cli   the same through the TraeCode CLI, then removes the hooks an older
#              installer added to ~/.trae/cli/hooks.json and ~/.trae/traecli.toml
#   cursor     ~/.cursor/hooks.json, ~/.cursor/mcp.json, ~/.cursor/rules, ~/.cursor/skills
#   trae       ~/.trae/hooks.json and TRAE's mcp.json (trae-cn: ~/.trae-cn, TRAE CN)
#   zcode      ~/.zcode/cli/config.json, ~/.zcode/hooks.json, ~/.zcode/mcp.json
#   kimicode   ~/.kimi-code/plugins
#   codebuddy  a local marketplace registered with codebuddy, installed from ~/.codebuddy/plugins
#   opencode   ~/.config/opencode/plugins/openviking, openviking.js, opencode.json(c)
#   pi         ~/.pi/agent/extensions/openviking
#   dsh        dsh plugin add into the chosen profile
#   dsh-desktop (macOS) dsh plugin add into Desktop's profile, with the dsh
#              command inside DeepSeek Harness.app, while Desktop is quit
#   cli        npm install -g @openviking/cli: ov in npm's global bin directory,
#              or in ~/.local/bin when that needs sudo
# Cursor, TRAE, TRAE CN and ZCode run their hooks from
# ~/.openviking/agent-integrations, where Kimi Code keeps its uninstall helper and
# CodeBuddy Code its managed marketplace.
# The plugin bundle is unpacked to ~/.openviking/memory-plugin-marketplace when
# a harness installs from it.
#
# Version check: after the confirmation, when installing from the release, it
# asks https://openviking.ai/install/v1/<harness>.json which release to
# install, once per selected harness. OPENVIKING_SKIP_VERSION_CHECK=1 skips the
# check and installs the latest release, which is also what happens when the
# check fails. The docs site holds the latest release only, so there the answer
# names it.
#
# Run from a repository checkout, it installs that checkout's plugins and skips
# the release (--source archive installs the release instead). A copy that is
# neither a published build nor part of a checkout downloads and runs the
# published copy of this script.
#
# Uninstall: Cursor, TRAE, TRAE CN, ZCode, Kimi Code and CodeBuddy Code with
#   curl -fsSL https://openviking.ai/install | bash -s -- --uninstall --yes --harness <list>
# The other harnesses with the commands the install prints at the end, such as
#   claude plugin uninstall openviking-memory@openviking && claude plugin marketplace remove openviking
#
# Optional check of this script (needs github.com; the install does not): a
# published copy is the file examples/memory-plugin-shared/install.sh of the
# repository at the commit it was built from, with only the INSTALLER_VERSION
# line changed. The docs site publishes it on every change to the main branch
# as <date>-<commit>, so for the commit at the end of INSTALLER_VERSION:
#   diff <(curl -fsSL https://docs.openviking.net/dl/memory-plugin-shared/install.sh) \
#     <(curl -fsSL https://raw.githubusercontent.com/volcengine/OpenViking/<commit>/examples/memory-plugin-shared/install.sh)
# Star count today: gh api repos/volcengine/OpenViking --jq .stargazers_count
# Documentation: https://docs.openviking.net or https://docs.openviking.ai

set -Eeuo pipefail

# Replaced with the published version when a pipeline publishes this file.
INSTALLER_VERSION="dev"
OV_HOME="${OPENVIKING_HOME:-$HOME/.openviking}"
MKT_ARCHIVE_URL="${OPENVIKING_MARKETPLACE_ARCHIVE_URL:-}"
# The docs site serves the downloads from two hosts with the same content.
# OPENVIKING_TOS_BASE is the name earlier installers read.
DOWNLOAD_BASES="${OPENVIKING_DOWNLOAD_BASE:-${OPENVIKING_TOS_BASE:-https://docs.openviking.net/dl https://docs.openviking.ai/dl}}"
DOWNLOAD_BASE=""       # the one of them in use; set by select_download_base
CODEX_GIT_URL=""       # set by select_download_base
CC_MARKETPLACE_URL=""  # set by select_download_base
INSTALL_SITE="${OPENVIKING_INSTALL_SITE:-https://openviking.ai}"
INSTALL_SITE="${INSTALL_SITE%/}"
INSTALL_COMMAND="curl -fsSL $INSTALL_SITE/install | bash -s --"
# First Claude Code release with the `archive` plugin source.
CC_ARCHIVE_SOURCE_MIN_VERSION="2.1.224"
ARCHIVE_MARKER='.openviking-archive-source'
OVCLI_CONF="${OPENVIKING_CLI_CONFIG_FILE:-$OV_HOME/ovcli.conf}"

# One marketplace name everywhere. Claude Code and Codex keep separate
# registries, and within one harness the source modes are alternative channels
# for the same plugin — a single name keeps the plugin id
# (openviking-memory@openviking) and its per-id config stable across modes.
MARKETPLACE_NAME="${OPENVIKING_MARKETPLACE_NAME:-openviking}"
PLUGIN_NAME="openviking-memory"
DSH_PACKAGE="@openviking/dsh-memory-plugin"
CLI_PACKAGE="@openviking/cli"
CLI_PREFIX=""       # npm prefix the CLI goes to; set by choose_cli_prefix
# Until a display language is saved here, ov asks for one, and without a
# terminal it exits instead of running the command.
CLI_SETTINGS="$HOME/.openviking/ovcli.settings.conf"
CLI_INSTALLED=0
PLUGIN_ID="${PLUGIN_NAME}@${MARKETPLACE_NAME}"
# The marketplace installers used before the unified name.
LEGACY_MARKETPLACE_NAME="openviking-plugins-local"

CODEX_DIR="${CODEX_HOME:-$HOME/.codex}"
CODEX_CONFIG="${CODEX_CONFIG_FILE:-$CODEX_DIR/config.toml}"
CC_CONFIG_DIR="${CLAUDE_CONFIG_DIR:-$HOME/.claude}"
CC_SETTINGS="$CC_CONFIG_DIR/settings.json"
CC_KNOWN_MARKETPLACES="$CC_CONFIG_DIR/plugins/known_marketplaces.json"
MKT_DIR_ARCHIVE="$OV_HOME/memory-plugin-marketplace"

REQUESTED_HARNESSES=""
PUBLIC_SELECTED_HARNESSES=""
TRAECODE_CLI_BIN=""
CLAUDE_BINS_ARG="${OPENVIKING_CLAUDE_BINS:-${OPENVIKING_CLAUDE_BIN:-}}"
CODEX_BINS_ARG="${OPENVIKING_CODEX_BINS:-${OPENVIKING_CODEX_BIN:-}}"
DSH_PROFILE_ARG="${OPENVIKING_DSH_PROFILE:-}"
DSH_PROFILE_DEFAULT="web"
# dsh exports the running session's profile to the shells its agent starts.
DSH_SESSION_PROFILE=""
[ "${DSH_SHELL:-}" != "1" ] || DSH_SESSION_PROFILE="${DSH_PROFILE:-}"
DSH_PROFILE=""
# What was asked for but not done, one line each; repeated at the end of the
# output, where an agent reading it looks first.
NOT_DONE=""
# DeepSeek Harness Desktop keeps its own profile, which only the dsh command
# inside its app bundle may change (npm dsh refuses it), and no dsh can boot it.
DSH_DESKTOP_APP="DeepSeek Harness.app"
DSH_DESKTOP_LAUNCHER="Contents/Resources/runtime/cli/bin/dsh"
DSH_DESKTOP_CLI=""        # Desktop's dsh command; set by select_dsh_profile
DSH_DESKTOP_INSTALLED=0   # 1 once its plugin add succeeded
DSH_DESKTOP_QUIT_WAIT=20  # seconds a quit Desktop gets to shut its Host down
SOURCE_ARG=""
DIST_ARG=""
LANG_ARG=""
URL_ARG=""
API_KEY_ARG="__OPENVIKING_UNSET__"
ACCOUNT_ARG="__OPENVIKING_UNSET__"
USER_ARG="__OPENVIKING_UNSET__"
STATUSLINE_ARG=""   # "", yes, no
STATUSLINE_WRITE=0  # 1 once choose_statusline settled on writing it
YES=0
UNINSTALL=0
NODE_BIN=""

CHECKOUT_DIR=""     # repo checkout the script itself lives in, when applicable
MKT_DIR=""          # plugin bundle root once ensure_bundle ran (checkout examples/ in dev mode)
SOURCE_MODE=""
UI_LANG="en"
RELEASE_VERSION=""  # release the resolver named, when it answered
BUNDLE_URL=""       # bundle the resolver named, with the checksum it must have
BUNDLE_SHA256=""

if [ -t 1 ]; then
  CYAN=$'\033[0;36m'; GREEN=$'\033[0;32m'; YELLOW=$'\033[1;33m'; RED=$'\033[0;31m'; BOLD=$'\033[1m'; RESET=$'\033[0m'
else
  CYAN=''; GREEN=''; YELLOW=''; RED=''; BOLD=''; RESET=''
fi
info()    { printf '%s==>%s %s\n' "$GREEN" "$RESET" "$*"; }
warn()    { printf '%s!!%s  %s\n' "$YELLOW" "$RESET" "$*"; }
# not_done <message>: warn now and list the message again under "Not done".
not_done() { warn "$1"; NOT_DONE="${NOT_DONE}$1
"; }
err()     { printf '%sxx%s  %s\n' "$RED" "$RESET" "$*" >&2; }
ask()     { printf '%s??%s  %s' "$CYAN" "$RESET" "$*"; }
heading() { printf '\n%s%s%s\n' "$BOLD" "$*" "$RESET"; }

STEP=0
STEP_TOTAL=0
step_heading() {
  STEP=$((STEP + 1))
  heading "[$STEP/$STEP_TOTAL] $*"
}

# t <english> <chinese> — pick the UI language variant.
t() { if [ "$UI_LANG" = "zh" ]; then printf '%s' "$2"; else printf '%s' "$1"; fi; }

report_unexpected_error() { # report_unexpected_error <status> <line> <command>
  local status="$1" line="$2" command="$3"
  # Avoid duplicate reports while the same failure unwinds through nested
  # functions. EXIT traps still run afterwards and restore any active TUI.
  trap - ERR
  if [ "${BASH_SUBSHELL:-0}" -gt 0 ]; then
    return "$status"
  fi
  printf '\033[?25h' 2>/dev/null >/dev/tty || true
  printf '\n' >&2
  err "$(t 'OpenViking installer stopped unexpectedly.' 'OpenViking 安装程序意外退出。')"
  printf '    %s: %s\n' "$(t 'Exit status' '状态码')" "$status" >&2
  printf '    %s: %s\n' "$(t 'Script line' '脚本行号')" "$line" >&2
  printf '    %s: %s\n' "$(t 'Command' '失败命令')" "$command" >&2
  return "$status"
}

trap 'report_unexpected_error "$?" "$LINENO" "$BASH_COMMAND"' ERR

# Pure-bash substring test. Never pipe `claude/codex plugin list` into
# `grep -q`: with pipefail, grep exiting early SIGPIPEs the producer and the
# pipeline reads as a miss even though the entry is there.
str_contains() { case "$1" in *"$2"*) return 0 ;; *) return 1 ;; esac; }

usage() {
  cat <<EOF
Usage: install.sh [options]

Options:
  --harness LIST     Comma-separated harnesses: claude, codex, cursor, trae, trae-cn, trae-cli, zcode, kimicode, codebuddy, opencode, pi, dsh, dsh-desktop, cli.
                     Use trae-cli for TraeCode CLI 2.0 (installed through its Codex-compatible plugin format).
                     dsh is the dsh CLI from npm; dsh-desktop is DeepSeek Harness Desktop (macOS only).
                     cli is the OpenViking CLI (ov), installed from npm. Without --harness: the detected ones and cli.
  --claude-bin LIST  Comma-separated Claude-format CLI commands (default: claude).
  --codex-bin LIST   Comma-separated Codex-format CLI commands (default: codex).
  --dsh-profile NAME DeepSeek Harness profile to install into (default: web). desktop selects dsh-desktop instead.
  --lang LANG        en | zh (default: asked first, with the system locale preselected;
                     with --yes or no terminal, the system locale).
  --source MODE      Advanced: archive (the OpenViking release) | dev (this checkout's plugins).
                     Default: dev when run from a repository checkout, archive otherwise.
  --dist CHANNEL     Ignored; kept so older commands keep working.
  --url URL          OpenViking server base URL.
  --api-key KEY      OpenViking API key. Pass '' for unauthenticated local mode.
  --account ID       Optional OpenViking account.
  --user ID          Optional OpenViking user.
  --statusline       Register the Claude Code statusline without asking.
  --no-statusline    Skip the statusline prompt.
  --uninstall        Remove the Cursor, TRAE, TRAE CN, ZCode, Kimi Code and CodeBuddy Code integrations,
                     plus any legacy TraeCode CLI hook config. Claude Code, Codex, OpenCode,
                     pi, dsh and dsh-desktop are removed with the commands the install prints at the end.
  --yes, -y          Ask nothing: use defaults and install without asking for confirmation.

Environment:
  OPENVIKING_HOME                       Installer home (default: ~/.openviking).
  OPENVIKING_CLI_CONFIG_FILE            Connection file to write (default: ovcli.conf in OPENVIKING_HOME).
  OPENVIKING_LANG                       Same as --lang.
  OPENVIKING_CLAUDE_BINS                Same as --claude-bin.
  OPENVIKING_CODEX_BINS                 Same as --codex-bin.
  OPENVIKING_DSH_PROFILE                Same as --dsh-profile.
  OPENVIKING_DSH_DESKTOP_CLI            The dsh command inside DeepSeek Harness.app to use for dsh-desktop
                                        (default: found under /Applications, ~/Applications or on PATH).
  DSH_HOME                              DeepSeek Harness home holding Desktop's profile (default: ~/.dsh).
  OPENVIKING_DOWNLOAD_BASE              Download locations, separated by spaces; the first to answer is used
                                        (default: https://docs.openviking.net/dl https://docs.openviking.ai/dl).
                                        OPENVIKING_TOS_BASE is read as well.
  OPENVIKING_MARKETPLACE_ARCHIVE_URL    Plugin bundle to install instead of the release's; not checksum-verified.
  OPENVIKING_CLAUDE_MARKETPLACE_URL     Claude Code URL marketplace to register.
  OPENVIKING_CODEX_TOS_GIT_URL          Codex git marketplace to register.
  OPENVIKING_INSTALL_SITE               Site the version check asks which release to install (default: https://openviking.ai).
  OPENVIKING_SKIP_VERSION_CHECK         Any value but 0 skips the version check; the latest release is installed.
  OPENVIKING_INSTALLER_REEXEC           0 runs this copy even when it is not a release build.
EOF
}

INSTALLER_ARGS=("$@")
while [ "$#" -gt 0 ]; do
  case "$1" in
    --harness) REQUESTED_HARNESSES="${2:-}"; shift 2 ;;
    --claude-bin|--claude-bins) CLAUDE_BINS_ARG="${2:-}"; shift 2 ;;
    --codex-bin|--codex-bins) CODEX_BINS_ARG="${2:-}"; shift 2 ;;
    --dsh-profile) DSH_PROFILE_ARG="${2:-}"; shift 2 ;;
    --dist) DIST_ARG="${2:-}"; shift 2 ;;
    --lang) LANG_ARG="${2:-}"; shift 2 ;;
    --source) SOURCE_ARG="${2:-}"; shift 2 ;;
    --url) URL_ARG="${2:-}"; shift 2 ;;
    --api-key) API_KEY_ARG="${2-}"; shift 2 ;;
    --account) ACCOUNT_ARG="${2-}"; shift 2 ;;
    --user) USER_ARG="${2-}"; shift 2 ;;
    --statusline) STATUSLINE_ARG="yes"; shift ;;
    --no-statusline) STATUSLINE_ARG="no"; shift ;;
    --uninstall) UNINSTALL=1; shift ;;
    --yes|-y) YES=1; shift ;;
    --help|-h) usage; exit 0 ;;
    *) err "Unknown argument: $1"; usage; exit 2 ;;
  esac
done

# Interactive prompts read from /dev/tty (fd 3) so `bash <(curl ...)` and even
# `curl | bash` keep their prompts. No tty -> non-interactive defaults.
# Probe in a subshell first: a failed `exec` redirection would abort the script.
INTERACTIVE=0
if [ "$YES" -ne 1 ] && ( exec 3</dev/tty ) 2>/dev/null; then
  exec 3</dev/tty
  INTERACTIVE=1
fi

read_tty() { # read_tty <varname> [-s]
  local __var="$1" __val=""
  if [ "${2:-}" = "-s" ]; then
    if IFS= read -rs __val <&3 2>/dev/null; then printf '\n'; else __val=""; fi
  else
    IFS= read -r __val <&3 || __val=""
  fi
  eval "$__var=\$__val"
}

# ---------------------------------------------------------------------------
# Single-select TUI menu (arrow keys + digit shortcuts; numbered fallback when
# /dev/tty can't be drawn on; default choice when non-interactive)
# ---------------------------------------------------------------------------

TUI_MENU_CHOICE=0

# q picks TUI_MENU_QUIT when the caller sets it, otherwise the default.
tui_menu() { # tui_menu <title> <default-index> <option...>  -> TUI_MENU_CHOICE
  local title="$1" def="$2"
  shift 2
  local opts
  opts=("$@")
  local n=${#opts[@]} cursor="$def" key rest reply i lines=0
  TUI_MENU_CHOICE="$def"
  [ "$INTERACTIVE" -eq 1 ] || return 0
  if [ ! -w /dev/tty ]; then
    printf '%s%s%s\n' "$BOLD" "$title" "$RESET"
    i=0
    while [ "$i" -lt "$n" ]; do
      printf '  %d) %s\n' $((i + 1)) "${opts[$i]}"
      i=$((i + 1))
    done
    ask "[1-$n, $(t 'default' '默认') $((def + 1))]: "
    read_tty reply
    case "$reply" in
      ''|*[!0-9]*) ;;
      *) [ "$reply" -ge 1 ] && [ "$reply" -le "$n" ] && TUI_MENU_CHOICE=$((reply - 1)) ;;
    esac
    return 0
  fi
  # The keys sit beside the question, where the eye already is.
  printf '%s%s  %s%s%s\n' "$BOLD" "$title" "$CYAN" \
    "${TUI_MENU_HINT:-$(t '(↑/↓ to choose, Enter to confirm)' '(↑/↓ 选择,回车确认)')}" "$RESET" >/dev/tty
  printf '\033[?25l' >/dev/tty
  trap 'printf "\033[?25h" >/dev/tty' EXIT
  while :; do
    [ "$lines" -gt 0 ] && printf '\033[%dA' "$lines" >/dev/tty
    i=0
    while [ "$i" -lt "$n" ]; do
      if [ "$i" -eq "$cursor" ]; then
        printf '\r\033[K %s>%s (%s•%s) %s\n' "$CYAN" "$RESET" "$GREEN" "$RESET" "${opts[$i]}" >/dev/tty
      else
        printf '\r\033[K   ( ) %s\n' "${opts[$i]}" >/dev/tty
      fi
      i=$((i + 1))
    done
    lines="$n"
    IFS= read -rsn1 key <&3 || key=""
    case "$key" in
      $'\x1b')
        rest=""
        IFS= read -rsn2 -t 1 rest <&3 || rest=""
        case "$rest" in
          '[A') cursor=$(( (cursor + n - 1) % n )) ;;
          '[B') cursor=$(( (cursor + 1) % n )) ;;
        esac
        ;;
      k) cursor=$(( (cursor + n - 1) % n )) ;;
      j) cursor=$(( (cursor + 1) % n )) ;;
      [1-9])
        # Jump only. Confirming on the digit leaves the Enter most users press
        # right after it in the tty buffer, where the next prompt reads it as
        # an empty answer.
        if [ "$key" -le "$n" ]; then
          cursor=$((key - 1))
        fi
        ;;
      ''|$'\n'|$'\r') break ;;
      q|Q) cursor="${TUI_MENU_QUIT:-$def}"; break ;;
    esac
  done
  printf '\033[?25h' >/dev/tty
  trap - EXIT
  TUI_MENU_CHOICE="$cursor"
}

# ---------------------------------------------------------------------------
# UI language
# ---------------------------------------------------------------------------

detect_lang_default() {
  local locale="${OPENVIKING_LANG:-${LC_ALL:-${LANG:-}}}"
  # A terminal started from the macOS GUI often has no locale variables, or
  # just C; the system language is then only in the user defaults.
  case "$locale" in
    ''|C|C.*|POSIX)
      if [ "$(uname -s)" = "Darwin" ]; then
        locale="$(defaults read -g AppleLocale 2>/dev/null || true)"
      fi
      ;;
  esac
  case "$locale" in
    zh*|*zh_CN*|*zh_TW*|*zh_HK*) printf 'zh' ;;
    *) printf 'en' ;;
  esac
}

select_language() {
  UI_LANG="${LANG_ARG:-$(detect_lang_default)}"
  if [ -z "$LANG_ARG" ] && [ -z "${OPENVIKING_LANG:-}" ] && [ "$INTERACTIVE" -eq 1 ]; then
    local def=0
    [ "$UI_LANG" = "zh" ] && def=1
    TUI_MENU_HINT="(↑/↓ + Enter / 上下键选择,回车确认)" tui_menu "Language / 语言" "$def" "English" "中文"
    if [ "$TUI_MENU_CHOICE" -eq 1 ]; then UI_LANG="zh"; else UI_LANG="en"; fi
  fi
  case "$UI_LANG" in
    en|zh) ;;
    *) err "Invalid --lang: $UI_LANG (expected en or zh)"; exit 2 ;;
  esac
}

print_intro() {
  [ "$INTERACTIVE" -eq 1 ] && [ "$UNINSTALL" -eq 0 ] || return 0
  heading "$(t 'OpenViking memory plugin installer' 'OpenViking 记忆插件安装')"
  info "$(t 'Installs the OpenViking memory plugin into the AI tools on this computer.' '为这台电脑上的 AI 工具安装 OpenViking 记忆插件。')"
  info "$(t 'Every change is listed for you to confirm before anything is written.' '写入任何内容前,会先列出全部改动请你确认。')"
}

split_harnesses() {
  printf '%s\n' "$1" | tr ',' '\n' | while IFS= read -r h; do
    h=$(printf '%s' "$h" | tr '[:upper:]' '[:lower:]' | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')
    if [ -n "$h" ]; then
      printf '%s\n' "$h"
    fi
  done
}

split_csv_list() {
  printf '%s\n' "$1" | tr ',' '\n' | while IFS= read -r item; do
    item=$(printf '%s' "$item" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')
    # An `&& ...` here would make an empty last element the loop's -- and so the
    # function's -- exit status, which `set -e` turns into an abort at the call.
    if [ -n "$item" ]; then
      printf '%s\n' "$item"
    fi
  done
}

normalize_bin_list() { # normalize_bin_list <raw-list> <default-command>
  local raw="${1:-}" def="$2"
  [ -n "$raw" ] || raw="$def"
  split_csv_list "$raw"
}

append_csv_list() { # append_csv_list <existing-lines> <extra-csv>
  local existing="$1" extra="$2" item out=""
  while IFS= read -r item; do
    [ -n "$item" ] || continue
    list_contains_line "$out" "$item" || out="${out:+$out
}$item"
  done <<EOF
$existing
$(split_csv_list "$extra")
EOF
  printf '%s\n' "$out"
}

list_contains_line() {
  local list="$1" want="$2" item
  while IFS= read -r item; do
    [ "$item" = "$want" ] && return 0
  done <<EOF
$list
EOF
  return 1
}

list_words() {
  local item out=""
  while IFS= read -r item; do
    [ -n "$item" ] || continue
    out="${out:+$out }$item"
  done <<EOF
$1
EOF
  printf '%s' "$out"
}

has_available_bin() {
  local bins="$1" bin
  while IFS= read -r bin; do
    [ -n "$bin" ] || continue
    command -v "$bin" >/dev/null 2>&1 && return 0
  done <<EOF
$bins
EOF
  return 1
}

dsh_home() { printf '%s' "${DSH_HOME:-$HOME/.dsh}"; }

follow_symlinks() { # follow_symlinks <path> -> the file it finally points at
  local path="$1" dir target hops=0
  while [ -L "$path" ] && [ "$hops" -lt 40 ]; do
    dir="$(cd "$(dirname "$path")" && pwd -P)" || break
    target="$(readlink "$path")" || break
    case "$target" in
      /*) path="$target" ;;
      *) path="$dir/$target" ;;
    esac
    hops=$((hops + 1))
  done
  printf '%s' "$path"
}

# Desktop's "Manage dsh Command…" links /usr/local/bin/dsh to the launcher in
# its app bundle. That dsh is Desktop, not the npm CLI: what it adds to the
# CLI's profiles never reaches Desktop.
is_dsh_desktop_launcher() { # is_dsh_desktop_launcher <path>
  case "$(follow_symlinks "$1")" in
    *.app/"$DSH_DESKTOP_LAUNCHER") return 0 ;;
  esac
  return 1
}

have_npm_dsh() {
  local found
  found="$(command -v dsh 2>/dev/null)" || return 1
  ! is_dsh_desktop_launcher "$found"
}

# The launcher works from its absolute path, so registering it on PATH is
# optional. Desktop ships for macOS and Windows only.
resolve_dsh_desktop_cli() {
  [ "$DSH_DESKTOP_OFFERED" -eq 1 ] || return 1
  local candidate
  for candidate in "${OPENVIKING_DSH_DESKTOP_CLI:-}" \
    "/Applications/$DSH_DESKTOP_APP/$DSH_DESKTOP_LAUNCHER" \
    "$HOME/Applications/$DSH_DESKTOP_APP/$DSH_DESKTOP_LAUNCHER"; do
    if [ -n "$candidate" ] && [ -x "$candidate" ]; then
      printf '%s' "$candidate"
      return 0
    fi
  done
  candidate="$(command -v dsh 2>/dev/null)" || return 1
  is_dsh_desktop_launcher "$candidate" || return 1
  printf '%s' "$candidate"
}

refresh_available_harnesses() {
  HAVE_CLAUDE=0; HAVE_CODEX=0; HAVE_CURSOR=0; HAVE_TRAE=0; HAVE_TRAE_CN=0; HAVE_TRAE_CLI=0; HAVE_OPENCODE=0; HAVE_PI=0; HAVE_ZCODE=0; HAVE_KIMICODE=0; HAVE_CODEBUDDY=0; HAVE_DSH=0; HAVE_DSH_DESKTOP=0
  has_available_bin "$CLAUDE_BINS" && HAVE_CLAUDE=1
  has_available_bin "$CODEX_BINS" && HAVE_CODEX=1
  { command -v cursor >/dev/null 2>&1 || command -v cursor-agent >/dev/null 2>&1 || [ -d "/Applications/Cursor.app" ] || [ -d "$HOME/.cursor" ]; } && HAVE_CURSOR=1
  { [ -d "/Applications/Trae.app" ] || [ -d "/Applications/TRAE.app" ] || [ -d "$HOME/.trae" ]; } && HAVE_TRAE=1
  { [ -d "/Applications/Trae CN.app" ] || [ -d "/Applications/TRAE SOLO CN.app" ] || [ -d "$HOME/.trae-cn" ]; } && HAVE_TRAE_CN=1
  { command -v trae-cli >/dev/null 2>&1 || command -v traecli >/dev/null 2>&1 || command -v traex >/dev/null 2>&1; } && HAVE_TRAE_CLI=1
  command -v opencode >/dev/null 2>&1 && HAVE_OPENCODE=1
  command -v pi >/dev/null 2>&1 && HAVE_PI=1
  have_npm_dsh && HAVE_DSH=1
  { resolve_dsh_desktop_cli >/dev/null || { [ "$DSH_DESKTOP_OFFERED" -eq 1 ] && [ -d "$(dsh_home)/profiles/desktop" ]; }; } && HAVE_DSH_DESKTOP=1
  { command -v zcode >/dev/null 2>&1 || [ -d "$HOME/.zcode" ]; } && HAVE_ZCODE=1
  { command -v kimi >/dev/null 2>&1 || [ -d "$HOME/.kimi-code" ]; } && HAVE_KIMICODE=1
  { command -v codebuddy >/dev/null 2>&1 || [ -d "$HOME/.codebuddy" ]; } && HAVE_CODEBUDDY=1
  return 0
}

resolve_traecode_cli_bin() {
  local bin
  for bin in trae-cli traecli traex; do
    if command -v "$bin" >/dev/null 2>&1; then
      printf '%s' "$bin"
      return 0
    fi
  done
  printf '%s' 'trae-cli'
}

normalize_trae_cli_harness() {
  local h normalized="" found=0 trae_cli_bin
  while IFS= read -r h; do
    [ -n "$h" ] || continue
    if [ "$h" = "trae-cli" ]; then
      found=1
      continue
    fi
    list_contains_line "$(split_harnesses "$normalized")" "$h" \
      || normalized="${normalized:+$normalized,}$h"
  done <<EOF
$(split_harnesses "$SELECTED_HARNESSES")
EOF
  [ "$found" -eq 1 ] || return 0
  PUBLIC_SELECTED_HARNESSES="$SELECTED_HARNESSES"
  [ "$UNINSTALL" -eq 0 ] || return 0

  trae_cli_bin="$(resolve_traecode_cli_bin)"
  TRAECODE_CLI_BIN="$trae_cli_bin"
  if [ -z "$CODEX_BINS_ARG" ] \
    && ! list_contains_line "$(split_harnesses "$normalized")" codex; then
    CODEX_BINS="$trae_cli_bin"
    TUI_CODEX_BINS="$trae_cli_bin"
  else
    CODEX_BINS="$(append_csv_list "$CODEX_BINS" "$trae_cli_bin")"
    TUI_CODEX_BINS="$(append_csv_list "$TUI_CODEX_BINS" "$trae_cli_bin")"
  fi
  if list_contains_line "$(split_harnesses "$normalized")" codex; then
    SELECTED_HARNESSES="$normalized"
  else
    SELECTED_HARNESSES="${normalized:+$normalized,}codex"
  fi
}

add_detected_traecode_cli_alias() {
  local trae_cli_bin
  [ -z "$CODEX_BINS_ARG" ] || return 0
  [ "$HAVE_TRAE_CLI" -eq 1 ] || return 0
  [ -z "$REQUESTED_HARNESSES" ] || return 0
  trae_cli_bin="$(resolve_traecode_cli_bin)"
  CODEX_BINS="$(append_csv_list "$CODEX_BINS" "$trae_cli_bin")"
  TUI_CODEX_BINS="$(append_csv_list "$TUI_CODEX_BINS" "$trae_cli_bin")"
}

bin_basename() {
  local bin="$1"
  bin="${bin##*/}"
  printf '%s' "$bin"
}

contains_harness() {
  local want="$1" h
  while IFS= read -r h; do
    [ "$h" = "$want" ] && return 0
  done <<EOF
$(split_harnesses "$SELECTED_HARNESSES")
EOF
  return 1
}

json_get() {
  local file="$1" key="$2"
  [ -f "$file" ] || return 0
  node -e '
    try {
      const c = JSON.parse(require("node:fs").readFileSync(process.argv[1], "utf8"));
      const v = c[process.argv[2]];
      if (v != null && v !== "") process.stdout.write(String(v));
    } catch {}
  ' "$file" "$key" 2>/dev/null || true
}

mask_secret() {
  local s="$1"
  if [ -z "$s" ]; then printf '%s' "$(t '(not set)' '(未设置)')"; return; fi
  if [ "${#s}" -le 8 ]; then printf '****'; return; fi
  printf '%s…%s (%s)' "$(printf '%s' "$s" | cut -c1-4)" "$(printf '%s' "$s" | tail -c 4)" "${#s}"
}

json_merge_ovcli() {
  local file="$1" url="$2" key="$3" account="$4" user="$5"
  # Not as arguments: any user can read those in the process list, and one of
  # the values is the API key.
  OVCLI_URL="$url" OVCLI_API_KEY="$key" OVCLI_ACCOUNT="$account" OVCLI_USER="$user" node - "$file" <<'NODE'
const fs = require("node:fs");
const file = process.argv[2];
const { OVCLI_URL: url, OVCLI_API_KEY: apiKey, OVCLI_ACCOUNT: account, OVCLI_USER: user } = process.env;
let c = {};
try { c = JSON.parse(fs.readFileSync(file, "utf8")); } catch {}
if (url) c.url = url;
if (apiKey !== "__OPENVIKING_KEEP__") c.api_key = apiKey;
if (account !== "__OPENVIKING_KEEP__") {
  if (account) c.account = account; else delete c.account;
}
if (user !== "__OPENVIKING_KEEP__") {
  if (user) c.user = user; else delete c.user;
}
fs.mkdirSync(require("node:path").dirname(file), { recursive: true });
fs.writeFileSync(file, JSON.stringify(c, null, 2) + "\n", { mode: 0o600 });
NODE
  chmod 600 "$file" 2>/dev/null || true
}

# ---------------------------------------------------------------------------
# Harness selection (checkbox TUI on a tty, text fallback otherwise)
# ---------------------------------------------------------------------------

CLAUDE_BINS="$(normalize_bin_list "$CLAUDE_BINS_ARG" claude)"
CODEX_BINS="$(normalize_bin_list "$CODEX_BINS_ARG" codex)"

HAVE_CLAUDE=0; HAVE_CODEX=0; HAVE_CURSOR=0; HAVE_TRAE=0; HAVE_TRAE_CN=0; HAVE_TRAE_CLI=0; HAVE_OPENCODE=0; HAVE_PI=0; HAVE_ZCODE=0; HAVE_DSH=0; HAVE_DSH_DESKTOP=0
# DeepSeek Harness Desktop is released for macOS and Windows; of the systems
# this script runs on, only macOS ever offers it.
DSH_DESKTOP_OFFERED=0
[ "$(uname -s)" != "Darwin" ] || DSH_DESKTOP_OFFERED=1
refresh_available_harnesses

TUI_CLAUDE_BINS="$CLAUDE_BINS"
TUI_CODEX_BINS="$CODEX_BINS"
add_detected_traecode_cli_alias
refresh_available_harnesses
SEL_CLAUDE_BINS=""
SEL_CODEX_BINS=""
SEL_OPENCODE=0
SEL_PI=0
SEL_DSH=0
SEL_DSH_DESKTOP=0
SEL_CURSOR_APP=0
SEL_TRAE=0
SEL_TRAE_CN=0
SEL_ZCODE=0
SEL_KIMICODE=0
SEL_CODEBUDDY=0
SEL_CLI=0
TUI_CURSOR=0; TUI_LINES=0

list_count() {
  local list="$1" item n=0
  while IFS= read -r item; do
    [ -n "$item" ] && n=$((n + 1))
  done <<EOF
$list
EOF
  printf '%s' "$n"
}

tui_selectable_count() {
  printf '%s' $(( $(list_count "$TUI_CLAUDE_BINS") + $(list_count "$TUI_CODEX_BINS") + 10 + DSH_DESKTOP_OFFERED ))
}

tui_total_count() {
  printf '%s' $(( $(tui_selectable_count) + 1 ))
}

tui_item_at() { # tui_item_at <index> -> kind|bin, or add|
  local idx="$1" i=0 bin
  while IFS= read -r bin; do
    [ -n "$bin" ] || continue
    if [ "$i" -eq "$idx" ]; then printf 'claude|%s' "$bin"; return 0; fi
    i=$((i + 1))
  done <<EOF
$TUI_CLAUDE_BINS
EOF
  while IFS= read -r bin; do
    [ -n "$bin" ] || continue
    if [ "$i" -eq "$idx" ]; then printf 'codex|%s' "$bin"; return 0; fi
    i=$((i + 1))
  done <<EOF
$TUI_CODEX_BINS
EOF
  if [ "$i" -eq "$idx" ]; then printf 'opencode|opencode'; return 0; fi
  i=$((i + 1))
  if [ "$i" -eq "$idx" ]; then printf 'pi|pi'; return 0; fi
  i=$((i + 1))
  if [ "$i" -eq "$idx" ]; then printf 'dsh|dsh'; return 0; fi
  i=$((i + 1))
  if [ "$DSH_DESKTOP_OFFERED" -eq 1 ]; then
    if [ "$i" -eq "$idx" ]; then printf 'dsh-desktop|dsh-desktop'; return 0; fi
    i=$((i + 1))
  fi
  if [ "$i" -eq "$idx" ]; then printf 'cursor|cursor'; return 0; fi
  i=$((i + 1))
  if [ "$i" -eq "$idx" ]; then printf 'trae|trae'; return 0; fi
  i=$((i + 1))
  if [ "$i" -eq "$idx" ]; then printf 'trae-cn|trae-cn'; return 0; fi
  i=$((i + 1))
  if [ "$i" -eq "$idx" ]; then printf 'zcode|zcode'; return 0; fi
  i=$((i + 1))
  if [ "$i" -eq "$idx" ]; then printf 'kimicode|kimi'; return 0; fi
  i=$((i + 1))
  if [ "$i" -eq "$idx" ]; then printf 'codebuddy|codebuddy'; return 0; fi
  i=$((i + 1))
  if [ "$i" -eq "$idx" ]; then printf 'cli|ov'; return 0; fi
  printf 'add|'
}

tui_find_bin_index() { # tui_find_bin_index <kind> <bin>
  local want_kind="$1" want_bin="$2" idx=0 spec kind bin total
  total="$(tui_selectable_count)"
  while [ "$idx" -lt "$total" ]; do
    spec="$(tui_item_at "$idx")"
    kind="${spec%%|*}"
    bin="${spec#*|}"
    if [ "$kind" = "$want_kind" ] && [ "$bin" = "$want_bin" ]; then
      printf '%s' "$idx"
      return 0
    fi
    idx=$((idx + 1))
  done
  printf '0'
}

tui_bin_label() {
  local kind="$1" bin="$2"
  case "$kind:$bin" in
    claude:claude) printf 'Claude Code' ;;
    codex:codex) printf 'Codex' ;;
    codex:trae-cli|codex:traecli|codex:traex) printf 'TraeCode CLI 2.0' ;;
    opencode:*) printf 'OpenCode' ;;
    pi:*) printf 'pi' ;;
    dsh:*) printf 'DeepSeek Harness' ;;
    dsh-desktop:*) printf 'DeepSeek Harness Desktop' ;;
    cursor:*) printf 'Cursor' ;;
    trae:*) printf 'TRAE' ;;
    trae-cn:*) printf 'TRAE CN' ;;
    zcode:*) printf 'ZCode' ;;
    kimicode:*) printf 'Kimi Code' ;;
    codebuddy:*) printf 'CodeBuddy Code' ;;
    cli:*) printf 'CLI (ov)' ;;
    claude:*) printf '%s %s' "$bin" "$(t '(Claude-format)' '(Claude 格式)')" ;;
    codex:*) printf '%s %s' "$bin" "$(t '(Codex-format)' '(Codex 格式)')" ;;
  esac
}

tui_bin_selected() {
  local kind="$1" bin="$2"
  if [ "$kind" = "claude" ]; then
    list_contains_line "$SEL_CLAUDE_BINS" "$bin"
  elif [ "$kind" = "codex" ]; then
    list_contains_line "$SEL_CODEX_BINS" "$bin"
  elif [ "$kind" = "opencode" ]; then
    [ "$SEL_OPENCODE" -eq 1 ]
  elif [ "$kind" = "pi" ]; then
    [ "$SEL_PI" -eq 1 ]
  elif [ "$kind" = "dsh" ]; then
    [ "$SEL_DSH" -eq 1 ]
  elif [ "$kind" = "dsh-desktop" ]; then
    [ "$SEL_DSH_DESKTOP" -eq 1 ]
  elif [ "$kind" = "cursor" ]; then
    [ "$SEL_CURSOR_APP" -eq 1 ]
  elif [ "$kind" = "trae" ]; then
    [ "$SEL_TRAE" -eq 1 ]
  elif [ "$kind" = "trae-cn" ]; then
    [ "$SEL_TRAE_CN" -eq 1 ]
  elif [ "$kind" = "zcode" ]; then
    [ "$SEL_ZCODE" -eq 1 ]
  elif [ "$kind" = "kimicode" ]; then
    [ "$SEL_KIMICODE" -eq 1 ]
  elif [ "$kind" = "codebuddy" ]; then
    [ "$SEL_CODEBUDDY" -eq 1 ]
  elif [ "$kind" = "cli" ]; then
    [ "$SEL_CLI" -eq 1 ]
  else
    [ "$SEL_KIMICODE" -eq 1 ]
  fi
}

tui_bin_detected() { # tui_bin_detected <kind> <bin>
  case "$1" in
    cursor) [ "$HAVE_CURSOR" -eq 1 ] ;;
    trae) [ "$HAVE_TRAE" -eq 1 ] ;;
    trae-cn) [ "$HAVE_TRAE_CN" -eq 1 ] ;;
    zcode) [ "$HAVE_ZCODE" -eq 1 ] ;;
    kimicode) [ "$HAVE_KIMICODE" -eq 1 ] ;;
    codebuddy) [ "$HAVE_CODEBUDDY" -eq 1 ] ;;
    dsh) [ "$HAVE_DSH" -eq 1 ] ;;
    dsh-desktop) [ "$HAVE_DSH_DESKTOP" -eq 1 ] ;;
    *) command -v "$2" >/dev/null 2>&1 ;;
  esac
}

tui_set_all_bins() {
  SEL_CLAUDE_BINS="$TUI_CLAUDE_BINS"
  SEL_CODEX_BINS="$TUI_CODEX_BINS"
  SEL_OPENCODE=1
  SEL_PI=1
  SEL_DSH=1
  SEL_DSH_DESKTOP="$DSH_DESKTOP_OFFERED"
  SEL_CURSOR_APP=1
  SEL_TRAE=1
  SEL_TRAE_CN=1
  SEL_ZCODE=1
  SEL_KIMICODE=1
  SEL_CODEBUDDY=1
  SEL_CLI=1
}

tui_toggle_bin() {
  local kind="$1" bin="$2" item out="" selected
  if [ "$kind" = "claude" ]; then
    selected="$SEL_CLAUDE_BINS"
  elif [ "$kind" = "codex" ]; then
    selected="$SEL_CODEX_BINS"
  elif [ "$kind" = "opencode" ]; then
    SEL_OPENCODE=$((1 - SEL_OPENCODE))
    return 0
  elif [ "$kind" = "pi" ]; then
    SEL_PI=$((1 - SEL_PI))
    return 0
  elif [ "$kind" = "dsh" ]; then
    SEL_DSH=$((1 - SEL_DSH))
    return 0
  elif [ "$kind" = "dsh-desktop" ]; then
    SEL_DSH_DESKTOP=$((1 - SEL_DSH_DESKTOP)); return 0
  elif [ "$kind" = "cursor" ]; then
    SEL_CURSOR_APP=$((1 - SEL_CURSOR_APP)); return 0
  elif [ "$kind" = "trae" ]; then
    SEL_TRAE=$((1 - SEL_TRAE)); return 0
  elif [ "$kind" = "trae-cn" ]; then
    SEL_TRAE_CN=$((1 - SEL_TRAE_CN)); return 0
  elif [ "$kind" = "zcode" ]; then
    SEL_ZCODE=$((1 - SEL_ZCODE)); return 0
  elif [ "$kind" = "kimicode" ]; then
    SEL_KIMICODE=$((1 - SEL_KIMICODE)); return 0
  elif [ "$kind" = "codebuddy" ]; then
    SEL_CODEBUDDY=$((1 - SEL_CODEBUDDY)); return 0
  elif [ "$kind" = "cli" ]; then
    SEL_CLI=$((1 - SEL_CLI)); return 0
  else
    SEL_KIMICODE=$((1 - SEL_KIMICODE)); return 0
  fi
  if list_contains_line "$selected" "$bin"; then
    while IFS= read -r item; do
      [ -n "$item" ] || continue
      [ "$item" = "$bin" ] && continue
      out="${out:+$out
}$item"
    done <<EOF
$selected
EOF
  else
    out="$(append_csv_list "$selected" "$bin")"
  fi
  if [ "$kind" = "claude" ]; then
    SEL_CLAUDE_BINS="$out"
  else
    SEL_CODEX_BINS="$out"
  fi
}

tui_item_line() { # tui_item_line <index> <kind> <bin>
  local idx="$1" kind="$2" bin="$3" mark='[ ]' cur='  ' note='' label
  label="$(tui_bin_label "$kind" "$bin")"
  tui_bin_selected "$kind" "$bin" && mark="[${GREEN}x${RESET}]"
  [ "$TUI_CURSOR" -eq "$idx" ] && cur="${CYAN}>${RESET} "
  if tui_bin_detected "$kind" "$bin"; then
    note="  ${GREEN}$(t '(detected)' '(已检测到)')${RESET}"
  elif [ "$kind" = "cli" ] && command -v npm >/dev/null 2>&1; then
    note="  ${CYAN}$(t '(installs from npm)' '(通过 npm 安装)')${RESET}"
  elif [ "$kind" = "cli" ]; then
    note="  ${YELLOW}$(t '(npm not found)' '(未找到 npm)')${RESET}"
  else
    note="  ${YELLOW}$(t '(not installed)' '(未安装)')${RESET}"
  fi
  printf '\r\033[K %s%s %s%s\n' "$cur" "$mark" "$label" "$note" >/dev/tty
}

tui_add_item_line() {
  local idx="$1" cur='  '
  [ "$TUI_CURSOR" -eq "$idx" ] && cur="${CYAN}>${RESET} "
  printf '\r\033[K %s%s %s\n' "$cur" "${CYAN}+${RESET}" "$(t 'Other compatible command (advanced)...' '其他兼容命令(高级)…')" >/dev/tty
}

tui_select_title() {
  printf '%s%s  %s%s%s\n' "$BOLD" "$(t 'Select the AI tools to install for' '选择要安装的 AI 工具')" "$CYAN" \
    "$(t '(↑/↓ to move, Space to check or uncheck, Enter to confirm)' '(↑/↓ 移动,空格 勾选/取消,回车 确认)')" "$RESET" >/dev/tty
}

tui_draw() {
  local idx=0 spec kind bin total
  [ "$TUI_LINES" -gt 0 ] && printf '\033[%dA' "$TUI_LINES" >/dev/tty
  total="$(tui_total_count)"
  while [ "$idx" -lt "$total" ]; do
    spec="$(tui_item_at "$idx")"
    kind="${spec%%|*}"
    bin="${spec#*|}"
    if [ "$kind" = "add" ]; then
      add_idx="$idx"
      tui_add_item_line "$idx"
    else
      tui_item_line "$idx" "$kind" "$bin"
    fi
    idx=$((idx + 1))
  done
  printf '\r\033[K   %s%s%s\n' "$CYAN" "$(t 'a: check all · Enter on +: add another command' 'a:全选 · 在 + 上按回车:添加其他命令')" "$RESET" >/dev/tty
  TUI_LINES=$((total + 1))
}

tui_reset_bin_selection() {
  local bin any=0
  SEL_CLAUDE_BINS=""
  SEL_CODEX_BINS=""
  SEL_OPENCODE=0
  SEL_PI=0
  SEL_DSH=0
  SEL_DSH_DESKTOP=0
  SEL_CURSOR_APP=0
  SEL_TRAE=0
  SEL_TRAE_CN=0
  SEL_ZCODE=0
  SEL_KIMICODE=0
  SEL_CODEBUDDY=0
  SEL_CLI=$((1 - UNINSTALL))
  while IFS= read -r bin; do
    [ -n "$bin" ] || continue
    if command -v "$bin" >/dev/null 2>&1; then
      SEL_CLAUDE_BINS="${SEL_CLAUDE_BINS:+$SEL_CLAUDE_BINS
}$bin"; any=1
    fi
  done <<EOF
$TUI_CLAUDE_BINS
EOF
  while IFS= read -r bin; do
    [ -n "$bin" ] || continue
    if command -v "$bin" >/dev/null 2>&1; then
      SEL_CODEX_BINS="${SEL_CODEX_BINS:+$SEL_CODEX_BINS
}$bin"; any=1
    fi
  done <<EOF
$TUI_CODEX_BINS
EOF
  if command -v opencode >/dev/null 2>&1; then SEL_OPENCODE=1; any=1; fi
  if command -v pi >/dev/null 2>&1; then SEL_PI=1; any=1; fi
  if [ "$HAVE_DSH" -eq 1 ]; then SEL_DSH=1; any=1; fi
  if [ "$HAVE_DSH_DESKTOP" -eq 1 ]; then SEL_DSH_DESKTOP=1; any=1; fi
  if [ "$HAVE_CURSOR" -eq 1 ]; then SEL_CURSOR_APP=1; any=1; fi
  if [ "$HAVE_TRAE" -eq 1 ]; then SEL_TRAE=1; any=1; fi
  if [ "$HAVE_TRAE_CN" -eq 1 ]; then SEL_TRAE_CN=1; any=1; fi
  if [ "$HAVE_ZCODE" -eq 1 ]; then SEL_ZCODE=1; any=1; fi
  if [ "$HAVE_KIMICODE" -eq 1 ]; then SEL_KIMICODE=1; any=1; fi
  if [ "$HAVE_CODEBUDDY" -eq 1 ]; then SEL_CODEBUDDY=1; any=1; fi
  if [ "$any" -ne 1 ]; then
    SEL_CLAUDE_BINS="$TUI_CLAUDE_BINS"
    SEL_CODEX_BINS="$TUI_CODEX_BINS"
  fi
}

tui_choose_cli_format() {
  local cursor=0 key rest lines=0
  TUI_FORMAT_CHOICE=""
  printf '%s%s%s\n' "$BOLD" "$(t 'Choose compatible format:' '选择兼容格式:')" "$RESET" >/dev/tty
  printf '\033[?25l' >/dev/tty
  while :; do
    [ "$lines" -gt 0 ] && printf '\033[%dA' "$lines" >/dev/tty
    if [ "$cursor" -eq 0 ]; then
      printf '\r\033[K %s>%s (%s•%s) %s\n' "$CYAN" "$RESET" "$GREEN" "$RESET" "$(t 'Claude-format' 'Claude 格式')" >/dev/tty
      printf '\r\033[K   ( ) %s\n' "$(t 'Codex-format' 'Codex 格式')" >/dev/tty
    else
      printf '\r\033[K   ( ) %s\n' "$(t 'Claude-format' 'Claude 格式')" >/dev/tty
      printf '\r\033[K %s>%s (%s•%s) %s\n' "$CYAN" "$RESET" "$GREEN" "$RESET" "$(t 'Codex-format' 'Codex 格式')" >/dev/tty
    fi
    printf '\r\033[K   %s%s%s\n' "$CYAN" "$(t '↑/↓ move · enter confirm · q cancel' '↑/↓ 移动 · 回车确认 · q 取消')" "$RESET" >/dev/tty
    lines=3
    if ! IFS= read -rsn1 key <&3; then
      continue
    fi
    case "$key" in
      $'\x1b')
        rest=""
        IFS= read -rsn2 -t 1 rest <&3 || rest=""
        case "$rest" in
          '[A'|'[B') cursor=$((1 - cursor)) ;;
        esac
        ;;
      k|j) cursor=$((1 - cursor)) ;;
      1) cursor=0 ;;
      2) cursor=1 ;;
      ''|$'\n'|$'\r')
        if [ "$cursor" -eq 0 ]; then TUI_FORMAT_CHOICE="claude"; else TUI_FORMAT_CHOICE="codex"; fi
        break
        ;;
      q|Q) TUI_FORMAT_CHOICE=""; break ;;
    esac
  done
  printf '\033[?25h' >/dev/tty
}

tui_add_compatible_cli() {
  local kind bin
  printf '\033[?25h' >/dev/tty
  printf '\n%s%s%s\n' "$BOLD" "$(t 'Add compatible CLI' '新增兼容 CLI')" "$RESET" >/dev/tty
  tui_choose_cli_format
  kind="$TUI_FORMAT_CHOICE"
  if [ -z "$kind" ]; then
    warn "$(t 'Skipped adding compatible CLI.' '已跳过新增兼容 CLI。')"
    TUI_LINES=0
    tui_select_title
    printf '\033[?25l' >/dev/tty
    return 0
  fi
  ask "$(t 'Command name or path: ' '命令名或路径: ')"
  read_tty bin
  bin="$(printf '%s' "$bin" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')"
  if [ -z "$bin" ]; then
    warn "$(t 'Skipped adding compatible CLI.' '已跳过新增兼容 CLI。')"
    TUI_LINES=0
    tui_select_title
    printf '\033[?25l' >/dev/tty
    return 0
  fi
  if [ "$kind" = "claude" ]; then
    TUI_CLAUDE_BINS="$(append_csv_list "$TUI_CLAUDE_BINS" "$bin")"
    SEL_CLAUDE_BINS="$(append_csv_list "$SEL_CLAUDE_BINS" "$bin")"
    info "$(t 'Added Claude-format CLI:' '已新增 Claude 格式 CLI:') $bin"
  else
    TUI_CODEX_BINS="$(append_csv_list "$TUI_CODEX_BINS" "$bin")"
    SEL_CODEX_BINS="$(append_csv_list "$SEL_CODEX_BINS" "$bin")"
    info "$(t 'Added Codex-format CLI:' '已新增 Codex 格式 CLI:') $bin"
  fi
  TUI_CURSOR="$(tui_find_bin_index "$kind" "$bin")"
  TUI_LINES=0
  tui_select_title
  printf '\033[?25l' >/dev/tty
}

tui_has_selection() {
  [ -n "$(list_words "$SEL_CLAUDE_BINS")" ] || [ -n "$(list_words "$SEL_CODEX_BINS")" ] \
    || [ "$SEL_OPENCODE" -eq 1 ] || [ "$SEL_PI" -eq 1 ] || [ "$SEL_DSH" -eq 1 ] || [ "$SEL_DSH_DESKTOP" -eq 1 ] || [ "$SEL_CURSOR_APP" -eq 1 ] \
    || [ "$SEL_TRAE" -eq 1 ] || [ "$SEL_TRAE_CN" -eq 1 ] || [ "$SEL_ZCODE" -eq 1 ] \
    || [ "$SEL_KIMICODE" -eq 1 ] || [ "$SEL_CODEBUDDY" -eq 1 ] || [ "$SEL_CLI" -eq 1 ]
}

tui_finish_selection() {
  CLAUDE_BINS="$SEL_CLAUDE_BINS"
  CODEX_BINS="$SEL_CODEX_BINS"
  SELECTED_HARNESSES=""
  [ -n "$(list_words "$CLAUDE_BINS")" ] && SELECTED_HARNESSES="claude"
  [ -n "$(list_words "$CODEX_BINS")" ] && SELECTED_HARNESSES="${SELECTED_HARNESSES:+$SELECTED_HARNESSES,}codex"
  [ "$SEL_OPENCODE" -eq 1 ] && SELECTED_HARNESSES="${SELECTED_HARNESSES:+$SELECTED_HARNESSES,}opencode"
  [ "$SEL_PI" -eq 1 ] && SELECTED_HARNESSES="${SELECTED_HARNESSES:+$SELECTED_HARNESSES,}pi"
  [ "$SEL_DSH" -eq 1 ] && SELECTED_HARNESSES="${SELECTED_HARNESSES:+$SELECTED_HARNESSES,}dsh"
  [ "$SEL_DSH_DESKTOP" -eq 1 ] && SELECTED_HARNESSES="${SELECTED_HARNESSES:+$SELECTED_HARNESSES,}dsh-desktop"
  [ "$SEL_CURSOR_APP" -eq 1 ] && SELECTED_HARNESSES="${SELECTED_HARNESSES:+$SELECTED_HARNESSES,}cursor"
  [ "$SEL_TRAE" -eq 1 ] && SELECTED_HARNESSES="${SELECTED_HARNESSES:+$SELECTED_HARNESSES,}trae"
  [ "$SEL_TRAE_CN" -eq 1 ] && SELECTED_HARNESSES="${SELECTED_HARNESSES:+$SELECTED_HARNESSES,}trae-cn"
  [ "$SEL_ZCODE" -eq 1 ] && SELECTED_HARNESSES="${SELECTED_HARNESSES:+$SELECTED_HARNESSES,}zcode"
  [ "$SEL_KIMICODE" -eq 1 ] && SELECTED_HARNESSES="${SELECTED_HARNESSES:+$SELECTED_HARNESSES,}kimicode"
  [ "$SEL_CODEBUDDY" -eq 1 ] && SELECTED_HARNESSES="${SELECTED_HARNESSES:+$SELECTED_HARNESSES,}codebuddy"
  [ "$SEL_CLI" -eq 1 ] && SELECTED_HARNESSES="${SELECTED_HARNESSES:+$SELECTED_HARNESSES,}cli"
  return 0
}

tui_select_harnesses() {
  local key rest spec kind bin total add_idx
  tui_reset_bin_selection
  tui_select_title
  printf '\033[?25l' >/dev/tty
  trap 'printf "\033[?25h" >/dev/tty' EXIT
  TUI_LINES=0
  tui_draw
  while :; do
    total="$(tui_total_count)"
    add_idx=$((total - 1))
    if ! IFS= read -rsn1 key <&3; then
      tui_draw
      continue
    fi
    case "$key" in
      $'\x1b')
        rest=""
        IFS= read -rsn2 -t 1 rest <&3 || rest=""
        case "$rest" in
          '[A') TUI_CURSOR=$(( (TUI_CURSOR + total - 1) % total )) ;;
          '[B') TUI_CURSOR=$(( (TUI_CURSOR + 1) % total )) ;;
        esac
        ;;
      k) TUI_CURSOR=$(( (TUI_CURSOR + total - 1) % total )) ;;
      j) TUI_CURSOR=$(( (TUI_CURSOR + 1) % total )) ;;
      ' ')
        if [ "$TUI_CURSOR" -eq "$add_idx" ]; then
          tui_add_compatible_cli
        else
          spec="$(tui_item_at "$TUI_CURSOR")"; kind="${spec%%|*}"; bin="${spec#*|}"
          tui_toggle_bin "$kind" "$bin"
        fi
        ;;
      a|A) tui_set_all_bins ;;
      n|N|+)
        TUI_CURSOR="$add_idx"
        tui_add_compatible_cli
        ;;
      ''|$'\n'|$'\r')
        if [ "$TUI_CURSOR" -eq "$add_idx" ]; then
          tui_add_compatible_cli
        else
          tui_has_selection || { tui_draw; continue; }
          break
        fi
        ;;
      q|Q) tui_has_selection && break ;;
    esac
    total="$(tui_total_count)"
    [ "$TUI_CURSOR" -lt "$total" ] || TUI_CURSOR=$((total - 1))
    tui_draw
  done
  printf '\033[?25h' >/dev/tty
  trap - EXIT
  tui_finish_selection
}

select_harnesses() {
  local detected="" reply default
  [ "$HAVE_CLAUDE" -eq 1 ] && detected="claude"
  [ "$HAVE_CODEX" -eq 1 ] && detected="${detected:+$detected,}codex"
  [ "$HAVE_CURSOR" -eq 1 ] && detected="${detected:+$detected,}cursor"
  [ "$HAVE_TRAE" -eq 1 ] && detected="${detected:+$detected,}trae"
  [ "$HAVE_TRAE_CN" -eq 1 ] && detected="${detected:+$detected,}trae-cn"
  [ "$HAVE_OPENCODE" -eq 1 ] && detected="${detected:+$detected,}opencode"
  [ "$HAVE_PI" -eq 1 ] && detected="${detected:+$detected,}pi"
  [ "$HAVE_DSH" -eq 1 ] && detected="${detected:+$detected,}dsh"
  [ "$HAVE_DSH_DESKTOP" -eq 1 ] && detected="${detected:+$detected,}dsh-desktop"
  [ "$HAVE_ZCODE" -eq 1 ] && detected="${detected:+$detected,}zcode"
  [ "$HAVE_KIMICODE" -eq 1 ] && detected="${detected:+$detected,}kimicode"
  [ "$HAVE_CODEBUDDY" -eq 1 ] && detected="${detected:+$detected,}codebuddy"

  if [ -n "$REQUESTED_HARNESSES" ]; then
    SELECTED_HARNESSES="$REQUESTED_HARNESSES"
    normalize_trae_cli_harness
    return
  fi
  default="${detected:-claude,codex}"
  [ "$UNINSTALL" -eq 1 ] || default="$default,cli"
  if [ "$INTERACTIVE" -eq 1 ] && [ -w /dev/tty ]; then
    tui_select_harnesses
  elif [ "$INTERACTIVE" -eq 1 ]; then
    info "$(t 'Detected AI tools:' '检测到的 AI 工具:') ${detected:-none}"
    ask "$(t 'AI tools to install' '要安装的 AI 工具') [${default}]: "
    read_tty reply
    SELECTED_HARNESSES="${reply:-$default}"
  else
    SELECTED_HARNESSES="$default"
  fi
}

route_dsh_to_desktop() { # route_dsh_to_desktop <harness-list> -> the list with dsh-desktop for dsh
  local h out=""
  while IFS= read -r h; do
    [ -n "$h" ] || continue
    if [ "$h" = "dsh" ]; then h="dsh-desktop"; fi
    case ",$out," in
      *",$h,"*) ;;
      *) out="${out:+$out,}$h" ;;
    esac
  done <<EOF
$(split_harnesses "$1")
EOF
  printf '%s' "$out"
}

select_dsh_profile() {
  # npm dsh refuses Desktop's profile, so `--dsh-profile desktop` means the
  # dsh-desktop harness, which goes through Desktop's own dsh command.
  if [ "$DSH_PROFILE_ARG" = "desktop" ] && contains_harness dsh; then
    SELECTED_HARNESSES="$(route_dsh_to_desktop "$SELECTED_HARNESSES")"
    [ -z "$PUBLIC_SELECTED_HARNESSES" ] || PUBLIC_SELECTED_HARNESSES="$(route_dsh_to_desktop "$PUBLIC_SELECTED_HARNESSES")"
  fi
  if contains_harness dsh-desktop; then
    DSH_DESKTOP_CLI="$(resolve_dsh_desktop_cli || true)"
  fi
  contains_harness dsh || return 0
  if [ -n "$DSH_PROFILE_ARG" ]; then
    DSH_PROFILE="$DSH_PROFILE_ARG"
    return 0
  fi
  # Run from inside a dsh session, install into the profile that session uses.
  DSH_PROFILE="${DSH_SESSION_PROFILE:-$DSH_PROFILE_DEFAULT}"
}

# Sets DSH_SPEC and DSH_ORIGIN to what `<command> plugin add` installs.
dsh_select_spec() { # dsh_select_spec <dsh-command> <profile>
  local cmd="$1" profile="$2" local_dir packed
  # `@latest` rather than a bare name: pnpm keeps an already-satisfying install
  # when the name carries no version, so a profile holding a dev build would
  # never fall back to the published package.
  DSH_SPEC="$DSH_PACKAGE@latest"
  DSH_ORIGIN="npm"
  # npm is the bundle's only distribution channel; only dev mode installs
  # something other than the published package. It still has to arrive as a
  # real package rather than a link: a linked source tree resolves its dsh
  # peers from its own realpath and misses the profile's hoisted node_modules,
  # so the checkout gets packed first.
  if [ "$SOURCE_MODE" = "dev" ] && ensure_bundle && local_dir="$(plugin_dir_on_disk dsh-memory-plugin)"; then
    if packed="$(dsh_pack_local "$local_dir")"; then
      DSH_SPEC="$packed"
      DSH_ORIGIN="$local_dir"
      # A dev re-install usually carries the same version, and pnpm treats an
      # already-satisfied version as a no-op no matter which tarball it is
      # pointed at, so the edited sources would never reach the profile.
      # Dropping the package first forces the reinstall. Only done for local
      # sources: it is a downgrade in robustness when `add` can fail on network.
      "$cmd" plugin --profile "$profile" rm "$DSH_PACKAGE" >/dev/null 2>&1 || true
    fi
  fi
}

install_dsh() {
  step_heading "$(t 'DeepSeek Harness bundle' 'DeepSeek Harness 插件')"
  if ! have_npm_dsh; then
    if command -v dsh >/dev/null 2>&1; then
      not_done "$(t 'The dsh on PATH is the one inside DeepSeek Harness Desktop, not the npm dsh CLI; skipping DeepSeek Harness install (use --harness dsh-desktop for Desktop).' 'PATH 中的 dsh 是 DeepSeek Harness Desktop 自带的,不是 npm 安装的 dsh CLI,跳过 DeepSeek Harness 安装(Desktop 请用 --harness dsh-desktop)。')"
    else
      not_done "$(t 'dsh CLI not found; skipping DeepSeek Harness install.' '未找到 dsh 命令,跳过 DeepSeek Harness 安装。')"
    fi
    return 0
  fi
  local profile="${DSH_PROFILE:-$DSH_PROFILE_DEFAULT}"
  dsh_select_spec dsh "$profile"
  if dsh plugin --profile "$profile" add "$DSH_SPEC" >/dev/null 2>&1; then
    info "$(t 'DeepSeek Harness bundle installed into profile:' 'DeepSeek Harness 插件已安装到 profile:') $profile ($(t 'source' '来源'): $DSH_ORIGIN)"
  else
    not_done "$(t 'dsh plugin add failed; run it manually:' 'dsh plugin add 失败;请手动执行:') dsh plugin --profile $profile add $DSH_SPEC"
  fi
}

# Desktop creates its profile on first launch; its dsh refuses to run before.
dsh_desktop_initialized() { [ -f "$(dsh_home)/profiles/desktop/package.json" ]; }

# Closing Desktop's window only hides it. The main process runs the app's
# executable with no arguments (older macOS adds -psn_*); its Host and any dsh
# command pass some. Only this user's Desktop counts.
dsh_desktop_running() {
  if command -v pgrep >/dev/null 2>&1; then
    pgrep -u "$(id -u)" -f "DeepSeek Harness\.app/Contents/MacOS/DeepSeek Harness( -psn_[0-9_]+)?\$" >/dev/null 2>&1
  elif command -v osascript >/dev/null 2>&1; then
    [ "$(osascript -e 'application "DeepSeek Harness" is running' 2>/dev/null)" = "true" ]
  else
    return 1
  fi
}

# Desktop does not lock its profile against the dsh command, and only loads
# a change when it starts, so it should be quit first. Fails when it is still
# running after the user was asked to quit it.
dsh_desktop_quit_first() {
  local reply waited=0
  dsh_desktop_running || return 0
  if [ "$INTERACTIVE" -ne 1 ]; then
    warn "$(t 'DeepSeek Harness Desktop is running; installing anyway. Quit it with ⌘Q (closing the window only hides it) and reopen it to load the plugin.' 'DeepSeek Harness Desktop 正在运行,仍继续安装。请用 ⌘Q 退出(关闭窗口只是隐藏)后重新打开,插件才会加载。')"
    return 0
  fi
  warn "$(t 'DeepSeek Harness Desktop is running. Closing its window only hides it.' 'DeepSeek Harness Desktop 正在运行。关闭窗口只是隐藏它。')"
  ask "$(t 'Quit it with ⌘Q, then press Enter: ' '请用 ⌘Q 退出后按回车:')"
  read_tty reply
  # Quitting first shuts its Host down, which may take it a few seconds.
  while dsh_desktop_running && [ "$waited" -lt "$DSH_DESKTOP_QUIT_WAIT" ]; do
    [ "$waited" -gt 0 ] || info "$(t 'Waiting for DeepSeek Harness Desktop to exit...' '等待 DeepSeek Harness Desktop 退出…')"
    sleep 1
    waited=$((waited + 1))
  done
  dsh_desktop_running || return 0
  not_done "$(t 'DeepSeek Harness Desktop is still running; it was not changed.' 'DeepSeek Harness Desktop 仍在运行,未做修改。')"
  return 1
}

dsh_desktop_plugins_page_steps() {
  info "$(t 'Install it from DeepSeek Harness Desktop instead: Plugins → Add plugin → enter' '也可以在 DeepSeek Harness Desktop 中安装:插件 → 添加插件 → 输入') $DSH_PACKAGE → $(t 'Install, then turn it on if its switch is off' '安装,开关未打开时再打开')"
}

install_dsh_desktop() {
  step_heading "$(t 'DeepSeek Harness Desktop bundle' 'DeepSeek Harness Desktop 插件')"
  local cli="$DSH_DESKTOP_CLI" output
  if [ -z "$cli" ]; then
    not_done "$(t "The dsh command inside $DSH_DESKTOP_APP was not found (set OPENVIKING_DSH_DESKTOP_CLI to it)." "未找到 $DSH_DESKTOP_APP 中的 dsh 命令(可用 OPENVIKING_DSH_DESKTOP_CLI 指定)。")"
    dsh_desktop_plugins_page_steps
    return 0
  fi
  if ! dsh_desktop_initialized; then
    not_done "$(t 'DeepSeek Harness Desktop has not set up its profile yet: open it once, quit it with ⌘Q and re-run this installer.' 'DeepSeek Harness Desktop 尚未初始化其 profile:请先打开一次,用 ⌘Q 退出后重新运行本安装脚本。') ($(t 'not found:' '未找到:') $(short_path "$(dsh_home)/profiles/desktop/package.json"))"
    dsh_desktop_plugins_page_steps
    return 0
  fi
  if ! dsh_desktop_quit_first; then
    dsh_desktop_plugins_page_steps
    return 0
  fi
  dsh_select_spec "$cli" desktop
  if output="$("$cli" plugin --profile desktop add "$DSH_SPEC" 2>&1)"; then
    DSH_DESKTOP_INSTALLED=1
    info "$(t 'DeepSeek Harness Desktop bundle installed' 'DeepSeek Harness Desktop 插件已安装') ($(t 'source' '来源'): $DSH_ORIGIN)"
    info "$(t 'Reopen DeepSeek Harness Desktop to load it.' '重新打开 DeepSeek Harness Desktop 后生效。')"
  else
    warn "$(t 'dsh plugin add failed for DeepSeek Harness Desktop:' 'DeepSeek Harness Desktop 的 dsh plugin add 失败:')"
    printf '%s\n' "$output" | tail -n 5 | sed 's/^/      /'
    not_done "$(t 'With Desktop quit, run it manually:' '退出 Desktop 后手动执行:') \"$cli\" plugin --profile desktop add $DSH_SPEC"
    dsh_desktop_plugins_page_steps
  fi
}

# Fingerprint of the checkout's shipped sources. pnpm keys a file: dependency by
# path, so a re-pack under the same name is treated as already satisfied and the
# edited sources never reach the profile. Naming the tarball after its content
# means an unchanged checkout stays a no-op while an edited one reinstalls.
dsh_sha256() {
  if command -v shasum >/dev/null 2>&1; then
    shasum -a 256
  elif command -v sha256sum >/dev/null 2>&1; then
    sha256sum
  else
    return 1
  fi
}

dsh_have_sha256() {
  command -v shasum >/dev/null 2>&1 || command -v sha256sum >/dev/null 2>&1
}

dsh_source_files() { # dsh_source_files <plugin-dir>
  ( cd "$1" 2>/dev/null && find . -type f \
      -not -path "./node_modules/*" -not -name "*.tgz" -print0 ) | LC_ALL=C sort -z
}

dsh_source_fingerprint() { # dsh_source_fingerprint <plugin-dir>
  local dir="$1"
  dsh_have_sha256 || return 1
  {
    dsh_source_files "$dir" | tr '\0' '\n'
    dsh_source_files "$dir" | ( cd "$dir" && xargs -0 cat 2>/dev/null )
  } | dsh_sha256 | cut -c1-12
}

dsh_pack_local() { # dsh_pack_local <plugin-dir> -> tarball path
  local dir="$1" dest="$OV_HOME/dsh-memory-plugin" name fingerprint target
  command -v npm >/dev/null 2>&1 || {
    warn "$(t 'npm not found; installing the published dsh package instead of the local checkout.' '未找到 npm,将安装已发布的 dsh 包而非本地 checkout。')" >&2
    return 1
  }
  fingerprint="$(dsh_source_fingerprint "$dir")" || {
    warn "$(t 'no sha256 tool found; installing the published dsh package instead of the local checkout.' '未找到 sha256 工具,将安装已发布的 dsh 包而非本地 checkout。')" >&2
    return 1
  }
  target="$dest/$fingerprint/openviking-dsh-memory-plugin.tgz"
  if [ -f "$target" ]; then
    printf '%s' "$target"
    return 0
  fi
  rm -rf "$dest"
  mkdir -p "$dest/$fingerprint" || return 1
  name="$( (cd "$dir" && npm pack --pack-destination "$dest/$fingerprint" 2>/dev/null) | tail -1 )"
  [ -n "$name" ] && [ -f "$dest/$fingerprint/$name" ] || {
    warn "$(t 'npm pack failed for the local dsh checkout; installing the published package instead.' '本地 dsh checkout 打包失败,将改装已发布的包。')" >&2
    return 1
  }
  mv "$dest/$fingerprint/$name" "$target" || return 1
  printf '%s' "$target"
}

select_compatible_bins() {
  local reply
  [ "$INTERACTIVE" -eq 1 ] || return 0
  [ -w /dev/tty ] && return 0
  if contains_harness claude && [ -z "$CLAUDE_BINS_ARG" ]; then
    ask "$(t 'Extra Claude-format CLI commands, comma-separated (e.g. seed)' '额外 Claude 格式 CLI 命令,逗号分隔(如 seed)') [$(t 'none' '无')]: "
    read_tty reply
    if [ -n "$reply" ]; then
      CLAUDE_BINS="$(append_csv_list "$CLAUDE_BINS" "$reply")"
      info "$(t 'Claude-format commands:' 'Claude 格式命令:') $(list_words "$CLAUDE_BINS")"
    fi
  fi
  if contains_harness codex && [ -z "$CODEX_BINS_ARG" ]; then
    ask "$(t 'Extra Codex-format CLI commands, comma-separated (e.g. traex)' '额外 Codex 格式 CLI 命令,逗号分隔(如 traex)') [$(t 'none' '无')]: "
    read_tty reply
    if [ -n "$reply" ]; then
      CODEX_BINS="$(append_csv_list "$CODEX_BINS" "$reply")"
      info "$(t 'Codex-format commands:' 'Codex 格式命令:') $(list_words "$CODEX_BINS")"
    fi
  fi
  refresh_available_harnesses
}

validate_selected_harnesses() {
  local h bad=0
  while IFS= read -r h; do
    case "$h" in
      claude|codex|cursor|trae|trae-cn|opencode|pi|zcode|kimicode|codebuddy|dsh|cli) ;;
      dsh-desktop)
        [ "$DSH_DESKTOP_OFFERED" -eq 1 ] || {
          err "$(t 'DeepSeek Harness Desktop (dsh-desktop) is installed by this script on macOS only; on Windows, add the plugin from its Plugins page.' '本脚本仅在 macOS 上安装 DeepSeek Harness Desktop(dsh-desktop);Windows 上请在其“插件”页添加。')"
          bad=1
        } ;;
      trae-cli) [ "$UNINSTALL" -eq 1 ] || bad=1 ;;
      *) err "Unsupported harness: $h"; bad=1 ;;
    esac
  done <<EOF
$(split_harnesses "$SELECTED_HARNESSES")
EOF
  [ "$bad" -eq 0 ] || exit 2
}

validate_selected_bins() {
  local bin ok=0
  if contains_harness claude; then
    while IFS= read -r bin; do
      [ -n "$bin" ] || continue
      if command -v "$bin" >/dev/null 2>&1; then
        ok=1
      else
        warn "$(t 'Selected Claude-format CLI not found in PATH:' '已选择的 Claude 格式 CLI 不在 PATH 中:') $bin"
      fi
    done <<EOF
$CLAUDE_BINS
EOF
  fi
  if contains_harness codex; then
    while IFS= read -r bin; do
      [ -n "$bin" ] || continue
      if command -v "$bin" >/dev/null 2>&1; then
        ok=1
      else
        warn "$(t 'Selected Codex-format CLI not found in PATH:' '已选择的 Codex 格式 CLI 不在 PATH 中:') $bin"
      fi
    done <<EOF
$CODEX_BINS
EOF
  fi
  if contains_harness opencode && command -v opencode >/dev/null 2>&1; then ok=1; fi
  if contains_harness pi && command -v pi >/dev/null 2>&1; then ok=1; fi
  if contains_harness dsh && have_npm_dsh; then ok=1; fi
  # Without its dsh command, Desktop still gets the steps for its Plugins page.
  if contains_harness dsh-desktop; then ok=1; fi
  if contains_harness cli; then ok=1; fi
  # Cursor and TRAE are config-driven integrations. They may be installed
  # before the desktop app itself, so a CLI in PATH is not required.
  if contains_harness cursor || contains_harness trae || contains_harness trae-cn || contains_harness trae-cli || contains_harness zcode || contains_harness kimicode || contains_harness codebuddy; then ok=1; fi
  if [ "$ok" -ne 1 ]; then
    err "$(t 'No selected compatible CLI command was found in PATH.' '未在 PATH 中找到任何已选择的兼容 CLI 命令。')"
    exit 2
  fi
}

# ---------------------------------------------------------------------------
# ovcli.conf wizard
# ---------------------------------------------------------------------------

prompt_connection() { # sets WIZ_URL / WIZ_KEY (WIZ_KEY may stay __OPENVIKING_KEEP__)
  local current_url="$1" current_key="$2" url_input reply def=2 custom hidden
  custom="$(t 'Custom URL' '自定义 URL')"
  if [ -n "$current_url" ]; then
    custom="$(t 'Custom URL / keep current' '自定义 URL / 保持当前')  [$current_url]"
  else
    def=0
  fi
  tui_menu "$(t 'Where do you connect to OpenViking?' '连接到哪个 OpenViking 服务?')" "$def" \
    "$(t 'Self-hosted / local' '自建 / 本地')  [http://127.0.0.1:1933]" \
    "$(t 'Volcengine OpenViking Cloud' '火山引擎 OpenViking 云服务')  [api.vikingdb.cn-beijing.volces.com]" \
    "$custom"
  case "$TUI_MENU_CHOICE" in
    0) WIZ_URL="http://127.0.0.1:1933" ;;
    1) WIZ_URL="https://api.vikingdb.cn-beijing.volces.com/openviking" ;;
    *)
      ask "$(t 'Server URL' '服务地址') [${current_url:-http://127.0.0.1:1933}]: "
      read_tty url_input
      WIZ_URL="${url_input:-${current_url:-http://127.0.0.1:1933}}"
      ;;
  esac

  hidden="$(t 'typing is not shown' '输入内容不会显示')"
  if [ -n "$current_key" ]; then
    ask "$(t "API key [Enter = keep $(mask_secret "$current_key"), '-' = clear; $hidden]: " "API key [回车 = 保留 $(mask_secret "$current_key"),输入 '-' 清空;$hidden]: ")"
  elif [ "$TUI_MENU_CHOICE" -eq 1 ]; then
    info "$(t 'Get an API key in the OpenViking console, under User Management → API Key:' '在 OpenViking 控制台的「用户管理 → API Key」获取密钥:')"
    info "  https://console.volcengine.com/vikingdb/openviking/region:openviking+cn-beijing"
    ask "$(t "API key ($hidden): " "API key(${hidden}): ")"
  else
    ask "$(t "API key (press Enter if the server needs none; $hidden): " "API key(服务未开启鉴权可直接回车;${hidden}): ")"
  fi
  read_tty reply -s
  if [ "$reply" = "-" ]; then
    WIZ_KEY=""
  elif [ -n "$reply" ]; then
    WIZ_KEY="$reply"
  else
    # Not `[ -z ... ] && ...`: as the function's last command a false test makes
    # prompt_connection return 1, and `set -e` aborts the whole installer.
    WIZ_KEY="__OPENVIKING_KEEP__"
    if [ -z "$current_key" ]; then
      WIZ_KEY=""
    fi
  fi
}

# What the credentials step settled on. Nothing is written until the plan is
# confirmed; key, account and user stay __OPENVIKING_KEEP__ to keep the stored
# value.
CUR_URL=""; CUR_KEY=""; CUR_ACCOUNT=""; CUR_USER=""
CRED_URL=""; CRED_KEY="__OPENVIKING_KEEP__"; CRED_ACCOUNT="__OPENVIKING_KEEP__"; CRED_USER="__OPENVIKING_KEEP__"
PROBE_RESULT=""
PROBE_CODE=""

cred_value() { # cred_value <chosen> <stored>
  if [ "$1" = "__OPENVIKING_KEEP__" ]; then printf '%s' "$2"; else printf '%s' "$1"; fi
}

gather_credentials() {
  step_heading "$(t 'OpenViking credentials' 'OpenViking 凭据配置') ($OVCLI_CONF)"
  CUR_URL="$(json_get "$OVCLI_CONF" url)"
  CUR_KEY="$(json_get "$OVCLI_CONF" api_key)"
  CUR_ACCOUNT="$(json_get "$OVCLI_CONF" account)"
  CUR_USER="$(json_get "$OVCLI_CONF" user)"

  CRED_URL="${URL_ARG:-$CUR_URL}"
  [ "$API_KEY_ARG" = "__OPENVIKING_UNSET__" ] || CRED_KEY="$API_KEY_ARG"
  [ "$ACCOUNT_ARG" = "__OPENVIKING_UNSET__" ] || CRED_ACCOUNT="$ACCOUNT_ARG"
  [ "$USER_ARG" = "__OPENVIKING_UNSET__" ] || CRED_USER="$USER_ARG"

  # Show what is configured today, then offer to keep or reconfigure.
  if [ -n "$CUR_URL" ] || [ -n "$CUR_KEY" ]; then
    info "$(t 'Current config:' '当前配置:')"
    info "  url:     ${CUR_URL:-$(t '(not set)' '(未设置)')}"
    info "  api_key: $(mask_secret "$CUR_KEY")"
    [ -n "$CUR_ACCOUNT" ] && info "  account: $CUR_ACCOUNT"
    [ -n "$CUR_USER" ] && info "  user:    $CUR_USER"
  else
    info "$(t 'No existing config found.' '未找到已有配置。')"
  fi

  if [ "$INTERACTIVE" -eq 1 ] && [ -z "$URL_ARG" ] && [ "$API_KEY_ARG" = "__OPENVIKING_UNSET__" ]; then
    if [ -n "$CUR_URL" ] || [ -n "$CUR_KEY" ]; then
      tui_menu "$(t 'Existing credentials found — what next?' '检测到已有凭据——如何处理?')" 0 \
        "$(t 'Keep current credentials' '沿用当前凭据')" \
        "$(t 'Reconfigure (server URL / API key)' '重新配置(服务地址 / API key)')"
      if [ "$TUI_MENU_CHOICE" -eq 1 ]; then
        prompt_connection "$CUR_URL" "$CUR_KEY"
        CRED_URL="$WIZ_URL"; CRED_KEY="$WIZ_KEY"
      fi
    else
      prompt_connection "" ""
      CRED_URL="$WIZ_URL"; CRED_KEY="$WIZ_KEY"
    fi
  fi
  if [ -z "$CRED_URL" ]; then
    CRED_URL="http://127.0.0.1:1933"
  fi
}

# One `header = "..."` line for `curl -K -`: headers read from stdin keep the
# API key out of the process list.
curl_header_line() { # curl_header_line <name> <value>
  local value="${2//\\/\\\\}"
  value="${value//\"/\\\"}"
  printf 'header = "%s: %s"\n' "$1" "$value"
}

probe_server() { # probe_server <url> <key> <account> <user> -> PROBE_RESULT, PROBE_CODE
  local url="${1%/}" key="$2" account="$3" user="$4"
  PROBE_CODE="$(curl -sS -m 5 -o /dev/null -w '%{http_code}' "$url/health" 2>/dev/null || true)"
  case "$PROBE_CODE" in
    ''|000) PROBE_RESULT="unreachable"; return 0 ;;
  esac
  PROBE_CODE="$(
    {
      [ -z "$key" ] || curl_header_line Authorization "Bearer $key"
      [ -z "$account" ] || curl_header_line X-OpenViking-Account "$account"
      [ -z "$user" ] || curl_header_line X-OpenViking-User "$user"
    } | curl -sS -m 5 -o /dev/null -w '%{http_code}' -K - "$url/api/v1/system/status" 2>/dev/null || true
  )"
  case "$PROBE_CODE" in
    2??) PROBE_RESULT="ok" ;;
    401|403) if [ -n "$key" ]; then PROBE_RESULT="rejected"; else PROBE_RESULT="key-required"; fi ;;
    ''|000) PROBE_RESULT="unreachable" ;;
    *) PROBE_RESULT="http" ;;
  esac
}

check_server() {
  local key def
  command -v curl >/dev/null 2>&1 || return 0
  while :; do
    key="$(cred_value "$CRED_KEY" "$CUR_KEY")"
    probe_server "$CRED_URL" "$key" "$(cred_value "$CRED_ACCOUNT" "$CUR_ACCOUNT")" "$(cred_value "$CRED_USER" "$CUR_USER")"
    def=0
    case "$PROBE_RESULT" in
      ok)
        info "$(t 'Server check passed:' '服务检查通过:') $CRED_URL"
        return 0
        ;;
      unreachable)
        # Usual for a local server that has not been started yet.
        not_done "$(t "Cannot reach $CRED_URL. If the server is not running yet, start it before using the plugin." "无法连接 ${CRED_URL}。如果服务还没启动,请在使用插件前启动它。")"
        def=1
        ;;
      rejected) not_done "$(t "The server rejected the API key (HTTP $PROBE_CODE)." "服务拒绝了该 API key(HTTP ${PROBE_CODE})。")" ;;
      key-required) not_done "$(t "The server requires an API key (HTTP $PROBE_CODE)." "该服务需要 API key(HTTP ${PROBE_CODE})。")" ;;
      *) not_done "$(t "The server status check returned HTTP $PROBE_CODE." "服务状态检查返回 HTTP ${PROBE_CODE}。")" ;;
    esac
    [ "$INTERACTIVE" -eq 1 ] || return 0
    tui_menu "$(t 'What next?' '接下来怎么做?')" "$def" \
      "$(t 'Re-enter server URL and API key' '重新输入服务地址和 API key')" \
      "$(t 'Continue anyway' '仍然继续')"
    [ "$TUI_MENU_CHOICE" -eq 0 ] || return 0
    prompt_connection "$CRED_URL" "$key"
    CRED_URL="$WIZ_URL"
    [ "$WIZ_KEY" = "__OPENVIKING_KEEP__" ] || CRED_KEY="$WIZ_KEY"
  done
}

write_ovcli() {
  mkdir -p "$OV_HOME"
  chmod 700 "$OV_HOME" 2>/dev/null || true
  if [ -f "$OVCLI_CONF" ]; then
    cp "$OVCLI_CONF" "$OVCLI_CONF.bak.$(date +%s)"
  fi
  json_merge_ovcli "$OVCLI_CONF" "$CRED_URL" "$CRED_KEY" "$CRED_ACCOUNT" "$CRED_USER"
  if [ "$CRED_URL" != "$CUR_URL" ]; then
    info "$(t 'Updated:' '已更新:') url: ${CUR_URL:-—} -> $CRED_URL"
  fi
  if [ "$CRED_KEY" != "__OPENVIKING_KEEP__" ] && [ "$CRED_KEY" != "$CUR_KEY" ]; then
    info "$(t 'Updated:' '已更新:') api_key: $(mask_secret "$CUR_KEY") -> $(mask_secret "$CRED_KEY")"
  fi
  info "$(t 'Credentials saved:' '凭据已保存:') $OVCLI_CONF"
}

# ---------------------------------------------------------------------------
# Source acquisition
# ---------------------------------------------------------------------------

fetch_archive() { # fetch_archive <url> <dest> <required-subpath> [sha256]
  local url="$1" dest="$2" need="$3" sha256="${4:-}" tmp_zip tmp_dir top actual
  command -v unzip >/dev/null 2>&1 || { err 'unzip not found; required to install from an archive.'; exit 1; }
  tmp_zip=$(mktemp "${TMPDIR:-/tmp}/ov-src.XXXXXX") || { err 'mktemp failed'; exit 1; }
  tmp_dir=$(mktemp -d "${TMPDIR:-/tmp}/ov-src.XXXXXX") || { err 'mktemp failed'; rm -f "$tmp_zip"; exit 1; }
  info "$(t 'Downloading archive' '下载归档')"
  info "  $url"
  curl -fsSL -o "$tmp_zip" "$url" || { rm -rf "$tmp_zip" "$tmp_dir"; return 1; }
  if [ -n "$sha256" ]; then
    actual="$(node -e '
      const data = require("node:fs").readFileSync(process.argv[1]);
      process.stdout.write(require("node:crypto").createHash("sha256").update(data).digest("hex"));
    ' "$tmp_zip")" || actual=""
    if [ "$actual" != "$sha256" ]; then
      err "$(t 'The download does not match the checksum the release published; nothing was installed from it.' '下载内容与发布的校验和不一致,未使用该文件安装。')"
      err "  $url"
      err "  $(t 'expected' '期望') sha256 $sha256, $(t 'got' '实际') ${actual:-?}"
      rm -rf "$tmp_zip" "$tmp_dir"; exit 1
    fi
  fi
  unzip -q "$tmp_zip" -d "$tmp_dir" || { err 'unzip failed'; rm -rf "$tmp_zip" "$tmp_dir"; exit 1; }
  top=$(find "$tmp_dir" -mindepth 1 -maxdepth 1 -type d | head -n 1)
  if [ -n "$top" ] && [ ! -e "$top/$need" ] && [ -e "$tmp_dir/$need" ]; then
    top="$tmp_dir"
  fi
  if [ -z "$top" ] || [ ! -e "$top/$need" ]; then
    err "unexpected archive layout (missing $need)"
    rm -rf "$tmp_zip" "$tmp_dir"; exit 1
  fi
  if [ -e "$dest" ] && [ ! -f "$dest/$ARCHIVE_MARKER" ]; then
    err "$dest exists and is not an OpenViking archive."
    rm -rf "$tmp_zip" "$tmp_dir"; exit 1
  fi
  rm -rf "$dest"
  mkdir -p "$(dirname "$dest")"
  if [ "$top" = "$tmp_dir" ]; then
    mkdir -p "$dest"
    ( shopt -s dotglob; mv "$tmp_dir"/* "$dest"/ )
  else
    mv "$top" "$dest"
  fi
  : > "$dest/$ARCHIVE_MARKER"
  rm -rf "$tmp_zip" "$tmp_dir"
}

resolve_self_checkout() {
  local src dir
  # Unset when bash reads the script from stdin (`curl ... | bash`).
  src="${BASH_SOURCE[0]:-}"
  [ -n "$src" ] || return 0
  dir="$(cd "$(dirname "$src")" >/dev/null 2>&1 && pwd -P)" || return 0
  # A linked worktree keeps `.git` as a file pointing at the real gitdir, so
  # test for existence rather than for a directory.
  if [ -e "$dir/../../.git" ] && [ -d "$dir/../claude-code-memory-plugin" ]; then
    CHECKOUT_DIR="$(cd "$dir/../.." >/dev/null 2>&1 && pwd -P)"
  fi
}

# Sets DOWNLOAD_BASE and the marketplace URLs under it. The download locations
# are asked for the same small file at once and the first to answer serves the
# whole install: it is the one that is near, and Claude Code and Codex keep its
# marketplace URL for their own updates.
select_download_base() {
  [ -z "$DOWNLOAD_BASE" ] || return 0
  local dir base asked=0
  # shellcheck disable=SC2086
  set -- $DOWNLOAD_BASES
  if [ "$#" -gt 1 ] && command -v curl >/dev/null 2>&1 \
    && dir="$(mktemp -d "${TMPDIR:-/tmp}/ov-base.XXXXXX")"; then
    : >"$dir/up"; : >"$dir/down"
    for base in "$@"; do
      asked=$((asked + 1))
      (
        if curl -fsS -m 5 -o /dev/null "${base%/}/plugins/claude/marketplace.json"; then
          printf '%s\n' "$base" >>"$dir/up"
        else
          echo >>"$dir/down"
        fi
      ) >/dev/null 2>&1 &
    done
    while [ ! -s "$dir/up" ] && [ "$(wc -l <"$dir/down")" -lt "$asked" ]; do
      sleep 0.1
    done
    IFS= read -r DOWNLOAD_BASE <"$dir/up" || true
    rm -rf "$dir"
  fi
  [ -n "$DOWNLOAD_BASE" ] || DOWNLOAD_BASE="$1"
  DOWNLOAD_BASE="${DOWNLOAD_BASE%/}"
  CODEX_GIT_URL="${OPENVIKING_CODEX_TOS_GIT_URL:-$DOWNLOAD_BASE/plugins/memory-plugins.git}"
  CC_MARKETPLACE_URL="${OPENVIKING_CLAUDE_MARKETPLACE_URL:-$DOWNLOAD_BASE/plugins/claude/marketplace.json}"
}

# A copy that is neither a release build nor part of a checkout (one fetched
# from GitHub, say) would run against helpers and plugins from the release
# bundle. Running the release copy instead keeps the script and the bundle on
# one release.
reexec_release_installer() { # reexec_release_installer <original-args...>
  [ "$INSTALLER_VERSION" = "dev" ] && [ -z "$CHECKOUT_DIR" ] \
    && [ "${OPENVIKING_INSTALLER_REEXEC:-}" != "0" ] || return 0
  local dir status=0
  select_download_base
  # A directory of its own: install_lib_dir takes a lib/install found beside
  # the running script.
  dir="$(mktemp -d "${TMPDIR:-/tmp}/ov-install.XXXXXX")" || return 0
  if curl -fsSL --connect-timeout 10 -o "$dir/install.sh" "$DOWNLOAD_BASE/memory-plugin-shared/install.sh" 2>/dev/null; then
    case "$(head -n 5 "$dir/install.sh")" in
      '#!'*'OpenViking Memory Plugin shared installer'*)
        OPENVIKING_INSTALLER_REEXEC=0 OPENVIKING_DOWNLOAD_BASE="$DOWNLOAD_BASE" \
          bash "$dir/install.sh" --dist tos "$@" || status=$?
        rm -rf "$dir"
        exit "$status"
        ;;
    esac
  fi
  rm -rf "$dir"
  warn "$(t 'Could not fetch the release installer; continuing with this copy.' '无法获取发布版安装脚本,继续使用当前脚本。')"
}

# The names the release resolver knows: a Codex-format CLI counts as the
# TraeCode CLI when it is one, otherwise as Codex, and Desktop installs the
# same dsh package.
resolver_harnesses() {
  local h bin out=""
  while IFS= read -r h; do
    [ -n "$h" ] && [ "$h" != "cli" ] || continue
    if [ "$h" = "dsh-desktop" ]; then
      out="$(append_csv_list "$out" dsh)"
    elif [ "$h" = "codex" ]; then
      while IFS= read -r bin; do
        [ -n "$bin" ] || continue
        case "$(bin_basename "$bin")" in
          trae-cli|traecli|traex) out="$(append_csv_list "$out" trae-cli)" ;;
          *) out="$(append_csv_list "$out" codex)" ;;
        esac
      done <<EOF
$CODEX_BINS
EOF
    else
      out="$(append_csv_list "$out" "$h")"
    fi
  done <<EOF
$(split_harnesses "$SELECTED_HARNESSES")
EOF
  printf '%s\n' "$out"
}

# Asks the install site which release to install, one request per harness in
# parallel. The answer names one download location; the same paths are read
# from the one in use. Anything short of a well-formed answer that points into
# a download location keeps the built-in latest-release URLs, without a word: the answer
# only pins the release and must never slow down or break the install.
resolve_release() {
  [ "$SOURCE_MODE" = "archive" ] || return 0
  case "${OPENVIKING_SKIP_VERSION_CHECK:-}" in ''|0) ;; *) return 0 ;; esac
  command -v curl >/dev/null 2>&1 || return 0
  local dir agent names h key value
  dir="$(mktemp -d "${TMPDIR:-/tmp}/ov-resolve.XXXXXX")" || return 0
  agent="openviking-installer/$INSTALLER_VERSION ($(uname -s | tr '[:upper:]' '[:lower:]'); $(uname -m))"
  names="$(resolver_harnesses)"
  while IFS= read -r h; do
    [ -n "$h" ] || continue
    curl -fsS -m 3 -A "$agent" -o "$dir/$h.json" "$INSTALL_SITE/install/v1/$h.json" >/dev/null 2>&1 || true &
  done <<EOF
$names
EOF
  wait
  while IFS='=' read -r key value; do
    case "$key" in
      version) RELEASE_VERSION="$value" ;;
      bundle_url) BUNDLE_URL="$value" ;;
      sha256) BUNDLE_SHA256="$value" ;;
      git_url) [ -n "${OPENVIKING_CODEX_TOS_GIT_URL:-}" ] || CODEX_GIT_URL="$value" ;;
    esac
  done <<EOF
$(node -e '
  const fs = require("node:fs");
  const [dir, base, bases, names] = process.argv.slice(1);
  const known = bases.split(/\s+/).filter(Boolean).map((b) => b.replace(/\/+$/, "") + "/");
  const out = {};
  for (const name of names.split("\n").filter(Boolean)) {
    let r;
    try { r = JSON.parse(fs.readFileSync(dir + "/" + name + ".json", "utf8")); } catch { continue; }
    if (!r || r.schema !== 1) continue;
    const urls = Object.keys(r).filter((k) => k.endsWith("_url"));
    if (urls.some((k) => typeof r[k] !== "string" || /\s/.test(r[k]) || !known.some((b) => r[k].startsWith(b)))) continue;
    for (const k of urls) r[k] = base + "/" + r[k].slice(known.find((b) => r[k].startsWith(b)).length);
    if (!out.version && typeof r.version === "string" && /^[\w.+-]+$/.test(r.version)) out.version = r.version;
    if (!out.bundle_url && r.bundle_url && /^[0-9a-f]{64}$/i.test(r.sha256 ?? "")) {
      out.bundle_url = r.bundle_url;
      out.sha256 = r.sha256.toLowerCase();
    }
    if (!out.git_url && r.git_url) out.git_url = r.git_url;
  }
  for (const [key, value] of Object.entries(out)) process.stdout.write(key + "=" + value + "\n");
' "$dir" "$DOWNLOAD_BASE" "$DOWNLOAD_BASES" "$names" 2>/dev/null || true)
EOF
  rm -rf "$dir"
}

resolve_source_mode() {
  local retired=0
  case "$DIST_ARG" in
    ''|tos) ;;
    github) retired=1 ;;
    *) err "Invalid --dist: $DIST_ARG (expected tos)"; exit 2 ;;
  esac
  case "$SOURCE_ARG" in
    ''|archive|dev) SOURCE_MODE="$SOURCE_ARG" ;;
    remote) SOURCE_MODE="archive"; retired=1 ;;
    *) err "Invalid --source: $SOURCE_ARG (expected archive or dev)"; exit 2 ;;
  esac
  if [ -n "${OPENVIKING_REPO_URL:-}${OPENVIKING_REPO_REF:-}${OPENVIKING_REPO_BRANCH:-}" ]; then
    retired=1
  fi
  if [ -z "$SOURCE_MODE" ]; then
    if [ -n "$CHECKOUT_DIR" ] && [ "$DIST_ARG" != "tos" ] && [ -z "$MKT_ARCHIVE_URL" ]; then
      SOURCE_MODE="dev"
    else
      SOURCE_MODE="archive"
    fi
  fi
  if [ "$retired" -eq 1 ]; then
    warn "$(t 'The GitHub install channel was removed and its options are ignored. To install a branch, run install.sh from a checkout of it.' 'GitHub 安装渠道已移除,相关选项不再生效。要安装某个分支,请在该分支的 checkout 中运行 install.sh。')"
  fi
}

# The release bundle every file-based harness installs from, and the directory
# marketplace Claude-format and Codex-format CLIs use when their remote source
# is unavailable. Fetched on first need, so an install that uses none of it
# downloads nothing and needs no unzip. Call it at top level only: a caller
# inside `$(...)` runs in a subshell, where MKT_DIR is lost and the next call
# would download the bundle again.
ensure_bundle() {
  [ -z "$MKT_DIR" ] || return 0
  if [ "$SOURCE_MODE" = "dev" ]; then
    MKT_DIR="$CHECKOUT_DIR/examples"
  else
    local url="${BUNDLE_URL:-$DOWNLOAD_BASE/releases/latest/memory-plugin-marketplace.zip}" sha256="$BUNDLE_SHA256"
    if [ -n "$MKT_ARCHIVE_URL" ]; then
      url="$MKT_ARCHIVE_URL"
      sha256=""
    fi
    fetch_archive "$url" "$MKT_DIR_ARCHIVE" ".claude-plugin/marketplace.json" "$sha256" || {
      err "$(t 'Plugin bundle download failed.' '插件包下载失败。')"
      exit 1
    }
    MKT_DIR="$MKT_DIR_ARCHIVE"
  fi
  if [ ! -f "$MKT_DIR/.claude-plugin/marketplace.json" ]; then
    err "marketplace dir $MKT_DIR is missing .claude-plugin/marketplace.json"
    exit 1
  fi
}

plugin_dir_on_disk() { # plugin_dir_on_disk <plugin-subdir>
  [ -n "$MKT_DIR" ] && [ -d "$MKT_DIR/$1" ] || return 1
  printf '%s' "$MKT_DIR/$1"
}

# The installer's own JavaScript: the JSONC editor OpenCode's config needs and
# the hooks/mcp merge every config-driven host installs through. It is not part
# of the runtime the plugins ship, so it travels with whatever copy of this
# script is running rather than with `lib/MANIFEST`.
#
# `--uninstall` runs before any bundle is fetched, and the documented uninstall
# pipes this script from a URL, where there is no sibling directory to read. So
# this only ever looks at what is already on disk: the running script's
# sibling, the bundle an install step fetched, and the copy an earlier install
# assembled. The bundle goes ahead of that copy because it comes from the same
# release as this script, while the assembled copy may be older.
install_lib_dir() {
  local src self candidate
  src="${BASH_SOURCE[0]:-}"
  self=""
  if [ -n "$src" ]; then
    self="$(cd "$(dirname "$src")" >/dev/null 2>&1 && pwd -P)" || self=""
  fi
  for candidate in \
    "${self:+$self/lib/install}" \
    "${MKT_DIR:+$MKT_DIR/memory-plugin-shared/lib/install}" \
    "$OV_HOME/agent-integrations/memory-plugin-shared/lib/install"; do
    [ -n "$candidate" ] && [ -d "$candidate" ] || continue
    printf '%s' "$candidate"
    return 0
  done
  return 1
}

require_install_lib_dir() {
  install_lib_dir && return 0
  err "$(t 'Installer runtime not found:' '未找到安装器运行时:') memory-plugin-shared/lib/install"
  return 1
}

# ---------------------------------------------------------------------------
# Claude Code
# ---------------------------------------------------------------------------

CLAUDE_BIN="claude"
CLAUDE_URL_BINS=""  # Claude-format CLIs registered on the URL marketplace

is_native_claude_bin() {
  [ "$(bin_basename "$CLAUDE_BIN")" = "claude" ]
}

# Not `command "$CLAUDE_BIN"`: when `command` runs an external program that
# fails, bash 3.2 fires the ERR trap even where the caller handles the failure.
claude_cmd() {
  "$CLAUDE_BIN" "$@"
}

has_plugin_subcommand() {
  claude_cmd plugin --help >/dev/null 2>&1
}

# Current registered source string for our Claude marketplace ("" if absent).
claude_marketplace_current_source() {
  local raw
  raw="$(claude_cmd plugin marketplace list --json 2>/dev/null || true)"
  if [ -n "$raw" ]; then
    printf '%s' "$raw" | node -e '
      let raw = "";
      process.stdin.on("data", (d) => { raw += d; });
      process.stdin.on("end", () => {
        try {
          const parsed = JSON.parse(raw);
          const list = Array.isArray(parsed) ? parsed : (parsed.marketplaces || []);
          const m = list.find((x) => x.name === process.argv[1]);
          if (m) process.stdout.write(String(m.path || m.repo || m.url || m.source || ""));
        } catch {}
      });
    ' "$MARKETPLACE_NAME" 2>/dev/null || true
    return 0
  fi
  is_native_claude_bin || return 0
  node -e '
    try {
      const m = JSON.parse(require("node:fs").readFileSync(process.argv[1], "utf8"))[process.argv[2]];
      const s = m && m.source ? m.source : null;
      if (s) process.stdout.write(String(s.path || s.repo || s.url || ""));
    } catch {}
  ' "$CC_KNOWN_MARKETPLACES" "$MARKETPLACE_NAME" 2>/dev/null || true
}

claude_marketplace_sync() { # claude_marketplace_sync <source>
  local source="$1" current
  current="$(claude_marketplace_current_source)"
  if [ -n "$current" ] && [ "$current" = "$source" ]; then
    info "$CLAUDE_BIN plugin marketplace update ($MARKETPLACE_NAME)"
    claude_cmd plugin marketplace update "$MARKETPLACE_NAME" || \
      warn 'marketplace update returned non-zero — continuing'
    return 0
  fi
  if [ -n "$current" ]; then
    info "$(t 'Marketplace points elsewhere; re-registering' 'marketplace 指向其他来源,重新注册') ($current)"
    claude_cmd plugin uninstall "$PLUGIN_ID" >/dev/null 2>&1 || true
    claude_cmd plugin marketplace remove "$MARKETPLACE_NAME" >/dev/null 2>&1 || true
  elif ! is_native_claude_bin; then
    claude_cmd plugin uninstall "$PLUGIN_ID" >/dev/null 2>&1 || true
    claude_cmd plugin marketplace remove "$MARKETPLACE_NAME" >/dev/null 2>&1 || true
  fi
  info "$CLAUDE_BIN plugin marketplace add ($source)"
  claude_cmd plugin marketplace add "$source" || {
    err "$CLAUDE_BIN plugin marketplace add failed"
    return 1
  }
}

# The URL marketplace lists the plugin as an `archive` source (a zip over
# HTTPS). Custom Claude-format CLIs don't share Claude Code's version line, so
# only the native command qualifies, and a custom one that already lists the
# URL marketplace, such as a wrapper sharing Claude Code's config: replacing it
# with the directory marketplace would turn off auto-update for Claude Code too.
claude_supports_archive_source() {
  if ! is_native_claude_bin; then
    local current base
    current="$(claude_marketplace_current_source)"
    [ "$current" != "$CC_MARKETPLACE_URL" ] || return 0
    for base in $DOWNLOAD_BASES; do
      [ "$current" != "${base%/}/plugins/claude/marketplace.json" ] || return 0
    done
    return 1
  fi
  local version
  version="$(claude_cmd --version 2>/dev/null || true)"
  node -e '
    const parse = (s) => (/(\d+)\.(\d+)\.(\d+)/.exec(s) || []).slice(1).map(Number);
    const have = parse(process.argv[1]);
    const need = parse(process.argv[2]);
    const diff = have.length ? have.map((n, i) => n - need[i]).find((d) => d !== 0) ?? 0 : -1;
    process.exit(diff >= 0 ? 0 : 1);
  ' "$version" "$CC_ARCHIVE_SOURCE_MIN_VERSION" 2>/dev/null
}

# Claude Code leaves auto-update off for third-party marketplaces. The flag goes
# beside the declaration `marketplace add` writes to user settings, where the
# /plugin toggle keeps it too, so a user who switched it off stays off.
claude_enable_marketplace_autoupdate() { # claude_enable_marketplace_autoupdate <url>
  mkdir -p "$CC_CONFIG_DIR"
  [ -f "$CC_SETTINGS" ] || echo '{}' > "$CC_SETTINGS"
  node - "$CC_SETTINGS" "$MARKETPLACE_NAME" "$1" <<'NODE' || warn "$(t 'could not enable marketplace auto-update in' '无法在以下文件中开启 marketplace 自动更新:') $CC_SETTINGS"
const fs = require("node:fs");
const [settingsPath, name, url] = process.argv.slice(2);
const settings = JSON.parse(fs.readFileSync(settingsPath, "utf8"));
const known = settings.extraKnownMarketplaces || {};
const entry = known[name] || { source: { source: "url", url } };
if (entry.autoUpdate === undefined) {
  settings.extraKnownMarketplaces = { ...known, [name]: { ...entry, autoUpdate: true } };
  fs.writeFileSync(settingsPath, JSON.stringify(settings, null, 2) + "\n");
}
NODE
}

# Claude Code keeps each plugin version in a directory of its own and records
# the current one in installed_plugins.json. The command looks that path up on
# every run, so the statusline follows plugin updates and needs no copy of its
# own. Any failure prints nothing, which leaves the status line empty.
statusline_command() {
  local script='const fs=require("fs"),os=require("os"),path=require("path"),url=require("url");try{const d=process.env.CLAUDE_CONFIG_DIR||path.join(os.homedir(),".claude");const e=JSON.parse(fs.readFileSync(path.join(d,"plugins","installed_plugins.json"),"utf8")).plugins[process.argv[1]]||[];const p=(e.find(x=>x.scope==="user")||e[0]).installPath;import(url.pathToFileURL(path.join(p,"scripts","statusline.mjs")).href).catch(()=>{})}catch{}'
  printf "node -e '%s' '%s'" "$script" "$PLUGIN_ID"
}

# Earlier installers pointed the statusline at a copy under ~/.openviking with
# exactly this command. Anything composed around it is the user's own.
is_own_statusline() {
  case "$1" in
    'node "'*.openviking/*claude-code-memory-plugin/scripts/statusline.mjs'"') ;;
    *) return 1 ;;
  esac
  case "${1#node \"}" in *\"*\"*) return 1 ;; esac
}

# Only the native `claude` gets the statusline, and only when it can install
# the plugin.
installs_native_claude() {
  contains_harness claude || return 1
  while IFS= read -r CLAUDE_BIN; do
    [ -n "$CLAUDE_BIN" ] && is_native_claude_bin && command -v "$CLAUDE_BIN" >/dev/null 2>&1 \
      && has_plugin_subcommand && return 0
  done <<EOF
$CLAUDE_BINS
EOF
  return 1
}

# Settled before the review, so the plan names the statusline only when the
# install writes it. Without a tty nothing is asked: only --statusline, or a
# statusline an earlier installer registered, gets written.
choose_statusline() {
  local existing
  [ "$STATUSLINE_ARG" != "no" ] && installs_native_claude || return 0
  existing=$(node -e '
    try {
      const s = JSON.parse(require("node:fs").readFileSync(process.argv[1], "utf8"));
      if (s.statusLine && s.statusLine.command) process.stdout.write(String(s.statusLine.command));
    } catch {}
  ' "$CC_SETTINGS" 2>/dev/null || true)
  [ "$existing" != "$(statusline_command)" ] || return 0
  # A statusline this installer registered before is repointed without asking:
  # the user already chose it, only its location changes.
  if [ "$STATUSLINE_ARG" = "yes" ] || is_own_statusline "$existing"; then
    STATUSLINE_WRITE=1
    return 0
  fi
  [ "$INTERACTIVE" -eq 1 ] || return 0
  heading "$(t 'Claude Code statusline (optional)' 'Claude Code 状态栏(可选)')"
  info "$(t 'OpenViking can show a one-line server/recall status under the input box.' 'OpenViking 可以在输入框下方显示一行服务/召回状态。')"
  info "$(t 'Sample:' '示例:') \"OV ✓ │ Fable 5 · ctx 42% │ ↩ 6 mem (0.92) · 50ms │ ✎ 573/20k · 2 arch │ +3 today\""
  tui_menu "$(t 'Enable the OpenViking statusline?' '启用 OpenViking statusline?')" 1 \
    "$(t 'Enable' '启用')" \
    "$(t 'Skip' '跳过')"
  if [ "$TUI_MENU_CHOICE" -ne 0 ]; then
    info "$(t 'Skipped statusline registration. Re-run the installer to enable it later.' '跳过 statusline 注册,之后重跑安装脚本可启用。')"
    return 0
  fi
  if [ -n "$existing" ]; then
    warn "$(t 'Existing statusline detected:' '检测到已有 statusline:') $existing"
    tui_menu "$(t 'Replace it with the OpenViking statusline?' '替换为 OpenViking statusline?')" 1 \
      "$(t 'Replace' '替换')" \
      "$(t 'Keep existing' '保留现有')"
    if [ "$TUI_MENU_CHOICE" -ne 0 ]; then
      info "$(t 'Kept the existing statusline.' '保留了已有 statusline。')"
      return 0
    fi
  fi
  STATUSLINE_WRITE=1
}

register_statusline() {
  [ "$STATUSLINE_WRITE" -eq 1 ] || return 0
  local ts
  mkdir -p "$CC_CONFIG_DIR"
  [ -f "$CC_SETTINGS" ] || echo '{}' > "$CC_SETTINGS"
  ts=$(date +%Y%m%d-%H%M%S)
  cp -p "$CC_SETTINGS" "$CC_SETTINGS.bak.$ts"
  node - "$CC_SETTINGS" "$(statusline_command)" <<'NODE' || { err "writing statusline into $CC_SETTINGS failed"; return 1; }
const fs = require("node:fs");
const [settingsPath, cmd] = process.argv.slice(2);
const settings = JSON.parse(fs.readFileSync(settingsPath, "utf8"));
settings.statusLine = { type: "command", command: cmd, padding: 0 };
fs.writeFileSync(settingsPath, JSON.stringify(settings, null, 2) + "\n");
NODE
  info "$(t "Statusline registered (backup: $CC_SETTINGS.bak.$ts)" "Statusline 已注册(备份:$CC_SETTINGS.bak.${ts})")"
  info "$(t 'Turn it off anytime with: export OPENVIKING_STATUSLINE=off' '可随时用 export OPENVIKING_STATUSLINE=off 关闭')"
}

# What installers before the unified marketplace left behind: a marketplace of
# the old name with its plugin, and, from their fallback for older Claude Code,
# hooks merged into settings.json plus a user-scope `openviking` MCP server
# running the plugin's proxy. Any of them runs next to the plugin and repeats
# every hook or tool.
claude_legacy_leftovers() {
  if str_contains "$(claude_cmd plugin marketplace list 2>/dev/null || true)
$(claude_cmd plugin list 2>/dev/null || true)" "$LEGACY_MARKETPLACE_NAME"; then
    echo plugin
  fi
  is_native_claude_bin || return 0
  node - "$CC_SETTINGS" "${CLAUDE_CONFIG_DIR:-$HOME}/.claude.json" <<'NODE' 2>/dev/null || true
const fs = require("node:fs");
const read = (file) => { try { return JSON.parse(fs.readFileSync(file, "utf8")); } catch { return {}; } };
const hooks = read(process.argv[2]).hooks || {};
if (JSON.stringify(hooks).includes("claude-code-memory-plugin/scripts/")) console.log("hooks");
const server = (read(process.argv[3]).mcpServers || {}).openviking;
if (JSON.stringify(server || {}).includes("claude-code-memory-plugin/servers/mcp-proxy.mjs")) console.log("mcp");
NODE
}

claude_remove_legacy() {
  local leftovers id
  leftovers="$(claude_legacy_leftovers)"
  [ -n "$leftovers" ] || return 0
  info "$(t 'Removing the OpenViking integration an earlier installer set up' '移除旧版安装器留下的 OpenViking 集成')"
  if list_contains_line "$leftovers" plugin; then
    for id in "claude-code-memory-plugin@$LEGACY_MARKETPLACE_NAME" "$PLUGIN_NAME@$LEGACY_MARKETPLACE_NAME"; do
      claude_cmd plugin uninstall "$id" >/dev/null 2>&1 || true
    done
    claude_cmd plugin marketplace remove "$LEGACY_MARKETPLACE_NAME" >/dev/null 2>&1 || true
  fi
  if list_contains_line "$leftovers" mcp; then
    claude_cmd mcp remove openviking -s user >/dev/null 2>&1 || true
  fi
  if list_contains_line "$leftovers" hooks; then
    node - "$CC_SETTINGS" <<'NODE' || warn "$(t 'could not remove the old hooks from' '无法移除以下文件中的旧 hook:') $CC_SETTINGS"
const fs = require("node:fs");
const file = process.argv[2];
const settings = JSON.parse(fs.readFileSync(file, "utf8"));
for (const [event, groups] of Object.entries(settings.hooks || {})) {
  if (!Array.isArray(groups)) continue;
  const kept = groups.filter((group) => !JSON.stringify(group).includes("claude-code-memory-plugin/scripts/"));
  if (kept.length) settings.hooks[event] = kept;
  else delete settings.hooks[event];
}
if (settings.hooks && !Object.keys(settings.hooks).length) delete settings.hooks;
fs.writeFileSync(file, JSON.stringify(settings, null, 2) + "\n");
NODE
  fi
}

claude_install_plugin() {
  if str_contains "$(claude_cmd plugin list 2>/dev/null || true)" "$PLUGIN_ID"; then
    info "$CLAUDE_BIN plugin update ($PLUGIN_ID)"
    claude_cmd plugin update "$PLUGIN_ID" || warn "$CLAUDE_BIN plugin update returned non-zero"
  else
    info "$CLAUDE_BIN plugin install ($PLUGIN_ID)"
    claude_cmd plugin install "$PLUGIN_ID" || { err "$CLAUDE_BIN plugin install failed"; return 1; }
  fi
}

# Claude Code's shallow clones can't use the dumb-HTTP git repo, but a URL
# marketplace whose entry downloads the plugin zip installs and auto-updates
# with no git at all. Registering it already removed any earlier registration
# and its plugin, so an install that fails from it falls back as well.
install_claude() {
  step_heading "$(t 'Claude Code plugin' 'Claude Code 插件')"
  command -v "$CLAUDE_BIN" >/dev/null 2>&1 || {
    not_done "$(t 'Claude-format CLI not found; skipping:' '未找到 Claude 格式 CLI,跳过:') $CLAUDE_BIN"
    return 0
  }
  if ! has_plugin_subcommand; then
    not_done "$(t "Skipping $CLAUDE_BIN: it has no 'plugin' command. The plugin needs Claude Code 2.0 or newer; upgrade it and re-run this installer." "跳过 ${CLAUDE_BIN}:它没有 plugin 子命令。插件需要 Claude Code 2.0 或更新版本,请升级后重跑本安装脚本。")"
    return 0
  fi
  claude_remove_legacy
  local url=0
  if [ "$SOURCE_MODE" = "archive" ] && claude_supports_archive_source; then
    if claude_marketplace_sync "$CC_MARKETPLACE_URL" && claude_install_plugin; then
      url=1
    else
      warn "$(t 'URL marketplace unavailable; falling back to the archive directory.' 'URL marketplace 不可用,回退到归档目录方式。')"
    fi
  fi
  if [ "$url" -eq 1 ]; then
    if is_native_claude_bin; then
      claude_enable_marketplace_autoupdate "$CC_MARKETPLACE_URL"
    fi
    CLAUDE_URL_BINS="$(append_csv_list "$CLAUDE_URL_BINS" "$CLAUDE_BIN")"
  else
    ensure_bundle
    claude_marketplace_sync "$MKT_DIR" || return 1
    claude_install_plugin || return 1
  fi
  claude_cmd plugin enable "$PLUGIN_ID" >/dev/null 2>&1 || true
  info "$(t 'Claude-format plugin installed:' 'Claude 格式插件已安装:') $CLAUDE_BIN -> $PLUGIN_ID"
  if is_native_claude_bin; then
    register_statusline || true
  fi
}

# ---------------------------------------------------------------------------
# Codex
# ---------------------------------------------------------------------------

CODEX_BIN="codex"
CODEX_GIT_BINS=""   # Codex-format CLIs registered on the git marketplace

is_native_codex_bin() {
  [ "$(bin_basename "$CODEX_BIN")" = "codex" ]
}

codex_cmd() {
  "$CODEX_BIN" "$@"
}

codex_bin_label() {
  case "$(bin_basename "$CODEX_BIN")" in
    trae-cli|traecli|traex) printf 'TraeCode CLI 2.0' ;;
    codex) printf 'Codex' ;;
    *) printf '%s %s' "$CODEX_BIN" "$(t '(Codex-format)' '(Codex 格式)')" ;;
  esac
}

has_legacy_trae_cli_integration() {
  case "$(bin_basename "$CODEX_BIN")" in
    trae-cli|traecli|traex) ;;
    *) return 1 ;;
  esac
  local trae_home="${TRAE_HOME:-$HOME/.trae}"
  grep -qi 'openviking' "${TRAECLI_HOME:-$trae_home/cli}/hooks.json" 2>/dev/null \
    || [ -d "$OV_HOME/agent-integrations/trae-cli" ] \
    || grep -qF '[mcp_servers."openviking-memory"]' "$trae_home/traecli.toml" 2>/dev/null
}

remove_legacy_trae_cli_integration() {
  case "$(bin_basename "$CODEX_BIN")" in
    trae-cli|traecli|traex) ;;
    *) return 0 ;;
  esac
  local trae_home="${TRAE_HOME:-$HOME/.trae}"
  local trae_cli_home="${TRAECLI_HOME:-$trae_home/cli}"
  if has_legacy_trae_cli_integration; then
    agent_remove_trae_cli_configs "$trae_cli_home/hooks.json" "$trae_home/traecli.toml"
    rm -rf "$OV_HOME/agent-integrations/trae-cli"
    info "$(t 'Removed the deprecated TRAE CLI Hooks integration after installing the TraeCode CLI 2.0 plugin.' 'TraeCode CLI 2.0 插件安装成功后,已移除弃用的 TRAE CLI Hooks 集成。')"
  fi
  if [ ! -d "$OV_HOME/agent-integrations/cursor" ] \
    && [ ! -d "$OV_HOME/agent-integrations/trae" ] \
    && [ ! -d "$OV_HOME/agent-integrations/trae-cn" ] \
    && [ ! -d "$OV_HOME/agent-integrations/trae-cli" ] \
    && [ ! -d "$OV_HOME/agent-integrations/zcode" ]; then
    rm -rf "$OV_HOME/agent-integrations/memory-plugin-shared"
  fi
}

codex_marketplace_current_source() {
  local raw
  raw="$(codex_cmd plugin marketplace list --json 2>/dev/null || true)"
  [ -n "$raw" ] || return 0
  printf '%s' "$raw" | node -e '
    let raw = "";
    process.stdin.on("data", (d) => { raw += d; });
    process.stdin.on("end", () => {
      try {
        const parsed = JSON.parse(raw);
        const list = Array.isArray(parsed) ? parsed : (parsed.marketplaces || []);
        const m = list.find((x) => x.name === process.argv[1]);
        if (m && m.marketplaceSource) process.stdout.write(String(m.marketplaceSource.source || ""));
        else if (m) process.stdout.write(String(m.path || m.repo || m.url || m.source || ""));
      } catch {}
    });
  ' "$MARKETPLACE_NAME" 2>/dev/null || true
}

# Once the directory of the old marketplace is gone, Codex fails every
# `marketplace list`, which hides the registration codex_marketplace_sync has
# to replace; while it is there, its plugin runs next to this one.
codex_has_legacy() {
  if is_native_codex_bin; then
    grep -qF "$LEGACY_MARKETPLACE_NAME" "$CODEX_CONFIG" 2>/dev/null
  else
    str_contains "$(codex_cmd plugin marketplace list 2>&1 || true)" "$LEGACY_MARKETPLACE_NAME"
  fi
}

codex_remove_legacy() {
  codex_has_legacy || return 0
  info "$(t 'Removing the OpenViking marketplace an earlier installer registered' '移除旧版安装器注册的 OpenViking marketplace') ($LEGACY_MARKETPLACE_NAME)"
  codex_cmd plugin remove "$PLUGIN_NAME@$LEGACY_MARKETPLACE_NAME" >/dev/null 2>&1 || true
  codex_cmd plugin uninstall "$PLUGIN_NAME@$LEGACY_MARKETPLACE_NAME" >/dev/null 2>&1 || true
  codex_cmd plugin marketplace remove "$LEGACY_MARKETPLACE_NAME" >/dev/null 2>&1 || true
  if is_native_codex_bin; then
    rm -rf "$CODEX_DIR/$LEGACY_MARKETPLACE_NAME-marketplace" "$CODEX_DIR/plugins/cache/$LEGACY_MARKETPLACE_NAME"
  fi
}

codex_unregister() {
  codex_cmd plugin remove "$PLUGIN_ID" >/dev/null 2>&1 || true
  codex_cmd plugin uninstall "$PLUGIN_ID" >/dev/null 2>&1 || true
  codex_cmd plugin marketplace remove "$MARKETPLACE_NAME" >/dev/null 2>&1 || true
}

codex_marketplace_sync() { # codex_marketplace_sync <source>
  local source="$1" current
  current="$(codex_marketplace_current_source)"
  if [ -n "$current" ] && [ "$current" = "$source" ]; then
    info "$CODEX_BIN plugin marketplace upgrade ($MARKETPLACE_NAME)"
    codex_cmd plugin marketplace upgrade "$MARKETPLACE_NAME" >/dev/null 2>&1 || true
    return 0
  fi
  if [ -n "$current" ]; then
    info "$(t 'Marketplace points elsewhere; re-registering' 'marketplace 指向其他来源,重新注册') ($current)"
    codex_unregister
  elif ! is_native_codex_bin; then
    codex_unregister
  fi
  info "$CODEX_BIN plugin marketplace add $source"
  local out
  out="$(codex_cmd plugin marketplace add "$source" 2>&1 >/dev/null)" && return 0
  # A registration `marketplace list` did not report still holds the name.
  if ! str_contains "$out" "different source"; then
    printf '%s\n' "$out" >&2
    return 1
  fi
  codex_unregister
  codex_cmd plugin marketplace add "$source" >/dev/null
}

ensure_codex_config() {
  node - "$CODEX_CONFIG" "$PLUGIN_ID" <<'NODE'
const fs = require("node:fs");
const path = process.argv[2];
const pluginId = process.argv[3];
let text = "";
try { text = fs.readFileSync(path, "utf8"); } catch {}
function ensureSectionLine(src, section, key, value) {
  const lines = src.split(/\n/);
  const header = `[${section}]`;
  const start = lines.findIndex((line) => line.trim() === header);
  if (start === -1) {
    const prefix = src.trimEnd();
    return `${prefix}${prefix ? "\n\n" : ""}${header}\n${key} = ${value}\n`;
  }
  let end = lines.length;
  for (let i = start + 1; i < lines.length; i += 1) if (/^\s*\[/.test(lines[i])) { end = i; break; }
  for (let i = start + 1; i < end; i += 1) {
    if (new RegExp(`^\\s*${key}\\s*=`).test(lines[i])) {
      lines[i] = `${key} = ${value}`;
      return lines.join("\n").replace(/\n*$/, "\n");
    }
  }
  lines.splice(end, 0, `${key} = ${value}`);
  return lines.join("\n").replace(/\n*$/, "\n");
}
text = ensureSectionLine(text, `plugins."${pluginId}"`, "enabled", "true");
text = ensureSectionLine(text, "features", "plugin_hooks", "true");
fs.mkdirSync(require("node:path").dirname(path), { recursive: true });
fs.writeFileSync(path, text);
NODE
}

install_codex() {
  local plugin_installed=0
  if is_native_codex_bin; then
    step_heading "$(t 'Codex plugin' 'Codex 插件')"
  else
    step_heading "$(codex_bin_label)"
  fi
  command -v "$CODEX_BIN" >/dev/null 2>&1 || {
    not_done "$(t 'Codex-format CLI not found; skipping:' '未找到 Codex 格式 CLI,跳过:') $CODEX_BIN"
    return 0
  }
  codex_remove_legacy
  # Codex clones git marketplaces served over dumb HTTP from static hosting,
  # so the release carries a slim marketplace git repo that Codex upgrades.
  if [ "$SOURCE_MODE" = "dev" ] || ! codex_marketplace_sync "$CODEX_GIT_URL" 2>/dev/null; then
    [ "$SOURCE_MODE" = "dev" ] \
      || warn "$(t 'Git marketplace unavailable; falling back to the archive directory.' 'Git marketplace 不可用,回退到归档目录方式。')"
    ensure_bundle
    codex_marketplace_sync "$MKT_DIR" || {
      err "$CODEX_BIN plugin marketplace add failed"
      return 1
    }
  else
    CODEX_GIT_BINS="$(append_csv_list "$CODEX_GIT_BINS" "$CODEX_BIN")"
  fi
  if codex_cmd plugin add "$PLUGIN_ID" >/dev/null 2>&1; then
    plugin_installed=1
  elif codex_cmd plugin install "$PLUGIN_ID" >/dev/null 2>&1; then
    plugin_installed=1
  else
    warn "$CODEX_BIN plugin add/install returned non-zero for $PLUGIN_ID"
  fi
  if [ "$plugin_installed" -eq 1 ]; then
    if codex_cmd plugin enable "$PLUGIN_ID" >/dev/null 2>&1; then
      remove_legacy_trae_cli_integration
    elif ! is_native_codex_bin; then
      warn "$(t 'Plugin installed but could not be enabled; keeping the deprecated TRAE CLI Hooks integration.' '插件已安装但未能启用;保留弃用的 TRAE CLI Hooks 集成。')"
    fi
  fi
  if is_native_codex_bin; then
    ensure_codex_config
    info "$(t 'Codex plugin enabled in' 'Codex 插件已在配置中启用:') $CODEX_CONFIG"
  else
    info "$(t 'Codex-format plugin installed:' 'Codex 格式插件已安装:') $CODEX_BIN -> $PLUGIN_ID"
  fi
}

# ---------------------------------------------------------------------------
# Cursor / TRAE lifecycle hooks
# ---------------------------------------------------------------------------

AGENT_HOOK_HOSTS="cursor trae zcode kimicode codebuddy"

copy_agent_integration() { # copy_agent_integration <host> <dest-name> [destination]
  local host="$1" dest_name="$2" explicit_dest="${3:-}" source dest tmp other
  source="$(plugin_dir_on_disk agent-hook-plugin)" || {
    err "$(t 'Agent integration sources not found:' '未找到 Agent 接入源码:') agent-hook-plugin"
    return 1
  }
  dest="${explicit_dest:-$OV_HOME/agent-integrations/$dest_name}"
  tmp="$dest.tmp"
  rm -rf "$tmp"
  mkdir -p "$tmp"
  (cd "$source" && tar --exclude node_modules --exclude .git -cf - .) | (cd "$tmp" && tar -xf -)
  # One plugin serves every config-driven host; an installation is for one
  # client, so the other hosts' configuration directories do not travel with it.
  for other in $AGENT_HOOK_HOSTS; do
    [ "$other" = "$host" ] || rm -rf "$tmp/hosts/$other"
  done
  # Preserve the first-install timestamp across managed upgrades. The package
  # descriptor is copied from source; integration.json records this machine's
  # installation and must survive replacing the runtime directory.
  [ -f "$dest/integration.json" ] && cp "$dest/integration.json" "$tmp/integration.json"
  rm -rf "$dest"
  mkdir -p "$(dirname "$dest")"
  mv "$tmp" "$dest"
  printf '%s' "$dest"
}

# Thin hook hosts keep only their client-specific adapters in the repository.
# Assemble a self-contained installation by adding the canonical shared runtime
# at install time instead of committing generated copies for every client.
assemble_agent_integration() { # assemble_agent_integration <host> <dest-name> [bundle-root]
  local host="$1" dest_name="$2" bundle_root="${3:-}" root_dest root shared shared_dest manifest file
  if [ -n "$bundle_root" ]; then
    root_dest="$bundle_root/agent-integrations/$dest_name"
    shared_dest="$bundle_root/agent-integrations/memory-plugin-shared/lib"
  else
    root_dest="$OV_HOME/agent-integrations/$dest_name"
    shared_dest="$OV_HOME/agent-integrations/memory-plugin-shared/lib"
  fi
  root="$(copy_agent_integration "$host" "$dest_name" "$root_dest")" || return 1
  shared="$(plugin_dir_on_disk memory-plugin-shared)" || {
    err "$(t 'Shared agent runtime not found.' '未找到共享 Agent 运行时。')"
    return 1
  }
  # The closure of what the hook hosts import, written by sync.mjs and
  # shipped beside the modules. Regenerating it here is not an option: the
  # generator finds no plugin sources in a flat marketplace archive.
  manifest="$shared/lib/MANIFEST"
  [ -s "$manifest" ] || {
    err "$(t 'Shared runtime manifest is missing or empty:' '共享运行时清单缺失或为空:') $manifest"
    return 1
  }
  rm -rf "$shared_dest.tmp"
  mkdir -p "$shared_dest.tmp"
  while read -r file || [ -n "$file" ]; do
    [ -n "$file" ] || continue
    cp "$shared/lib/$file" "$shared_dest.tmp/$file" || return 1
  done < "$manifest"
  cp "$manifest" "$shared_dest.tmp/MANIFEST"
  # Config-driven installs retain the uninstall helpers beside the shared
  # runtime. A native Kimi bundle persists its one helper outside the plugin,
  # so carrying every installer module inside that bundle would be dead weight.
  if [ -z "$bundle_root" ]; then
    cp -R "$shared/lib/install" "$shared_dest.tmp/install" || return 1
  fi
  rm -rf "$shared_dest"
  mkdir -p "$(dirname "$shared_dest")"
  mv "$shared_dest.tmp" "$shared_dest"
  printf '%s' "$root"
}

agent_write_json_configs() { # agent_write_json_configs <kind> <hooks> <mcp> <root> <client-id> <node-bin>
  local lib
  lib="$(require_install_lib_dir)" || return 1
  "$NODE_BIN" "$lib/host-json-config.mjs" write "$1" "$2" "$3" "$4" "$5" "$6" "$SOURCE_MODE"
}

agent_remove_json_configs() { # agent_remove_json_configs <hooks> [mcp]
  local lib
  # An uninstall that cannot find the runtime still has to remove everything it
  # can and tell the user what it left behind; aborting here would leave both
  # the host's entries and the integration directory they point at.
  lib="$(install_lib_dir)" || {
    warn "$(t 'Installer runtime not found; remove the OpenViking hook and MCP entries by hand from:' '未找到安装器运行时,请手动移除以下文件中的 OpenViking hook 与 MCP 条目:') $1${2:+, $2}"
    return 0
  }
  "$NODE_BIN" "$lib/host-json-config.mjs" remove "$1" "${2:-}"
}

agent_remove_trae_cli_configs() { # agent_remove_trae_cli_configs <hooks> <traecli.toml>
  local hooks_path="$1" config_path="$2"
  # The hooks file answers to the same "is this entry ours" the other hosts use;
  # only the TOML this host keeps its MCP servers in is its own problem.
  agent_remove_json_configs "$hooks_path"
  [ -f "$config_path" ] || return 0
  local stripped tmp="$config_path.$$.tmp"
  stripped="$(awk -v target='mcp_servers."openviking-memory"' '
    BEGIN { prefix = target "." }
    /^[[:space:]]*\[[^]]+\][[:space:]]*$/ {
      name = $0
      sub(/^[[:space:]]*\[/, "", name)
      sub(/\][[:space:]]*$/, "", name)
      skip = (name == target || index(name, prefix) == 1)
    }
    skip { next }
    /^[[:space:]]*$/ { blank = 1; next }
    { if (started && blank) print ""; print; started = 1; blank = 0 }
  ' "$config_path")"
  ( umask 077; printf '%s\n' "$stripped" >"$tmp" )
  mv "$tmp" "$config_path"
}

uninstall_agent_integrations() {
  if contains_harness cursor; then
    agent_remove_json_configs "$HOME/.cursor/hooks.json" "$(cursor_mcp_path)"
    rm -f "$HOME/.cursor/rules/openviking-memory.mdc"
    rm -rf "$HOME/.cursor/skills/openviking-memory" "$HOME/.cursor/skills/openviking-skills" "$HOME/.cursor/skills/ov-experience-memory"
    rm -rf "$OV_HOME/agent-integrations/cursor"
    info "$(t 'Removed the Cursor OpenViking integration.' '已移除 Cursor OpenViking 集成。')"
  fi
  if contains_harness trae; then
    agent_remove_json_configs "$HOME/.trae/hooks.json" "$(trae_mcp_path trae)"
    rm -rf "$OV_HOME/agent-integrations/trae"
    info "$(t 'Removed TRAE OpenViking hooks and MCP config.' '已移除 TRAE OpenViking hooks 与 MCP 配置。')"
  fi
  if contains_harness trae-cn; then
    agent_remove_json_configs "$HOME/.trae-cn/hooks.json" "$(trae_mcp_path trae-cn)"
    rm -rf "$OV_HOME/agent-integrations/trae-cn"
    info "$(t 'Removed TRAE CN OpenViking hooks and MCP config.' '已移除 TRAE CN OpenViking hooks 与 MCP 配置。')"
  fi
  if contains_harness trae-cli; then
    local trae_home="${TRAE_HOME:-$HOME/.trae}"
    local trae_cli_home="${TRAECLI_HOME:-$trae_home/cli}"
    agent_remove_trae_cli_configs "$trae_cli_home/hooks.json" "$trae_home/traecli.toml"
    rm -rf "$OV_HOME/agent-integrations/trae-cli"
    info "$(t 'Removed TRAE CLI OpenViking hooks and MCP config.' '已移除 TRAE CLI OpenViking hooks 与 MCP 配置。')"
  fi
  if contains_harness zcode; then
    # ZCode reads hooks/MCP from config.json, not standalone files.
    # Use a node script to strip openviking entries from config.json.
    # Safe read: ENOENT → skip; parse error → skip (do NOT overwrite user data).
    "$NODE_BIN" - "$HOME/.zcode/cli/config.json" <<'CLEAN_NODE' 2>/dev/null || true
const fs = require("node:fs");
const configPath = process.argv[2];
if (!fs.existsSync(configPath)) process.exit(0);
let config;
try {
  config = JSON.parse(fs.readFileSync(configPath, "utf8"));
} catch {
  // Malformed config — do NOT overwrite; skip cleanup silently.
  process.exit(0);
}
if (typeof config !== "object" || config === null || Array.isArray(config)) process.exit(0);
// Remove openviking hooks from each event
if (config.hooks?.events) {
  for (const [event, handlers] of Object.entries(config.hooks.events)) {
    config.hooks.events[event] = (Array.isArray(handlers) ? handlers : []).filter(
      (group) => !JSON.stringify(group).includes("openviking-memory"),
    );
    if (config.hooks.events[event].length === 0) delete config.hooks.events[event];
  }
  if (Object.keys(config.hooks.events).length === 0) delete config.hooks.events;
}
// Remove openviking MCP server — ONLY if it's managed by us (contains openviking-memory tag)
if (config.mcp?.servers?.openviking) {
  if (JSON.stringify(config.mcp.servers.openviking).includes("openviking-memory")) {
    delete config.mcp.servers.openviking;
    if (Object.keys(config.mcp.servers).length === 0) delete config.mcp.servers;
    if (Object.keys(config.mcp).length === 0) delete config.mcp;
  }
}
const tmp = `${configPath}.${process.pid}.tmp`;
fs.writeFileSync(tmp, JSON.stringify(config, null, 2) + "\n");
fs.renameSync(tmp, configPath);
CLEAN_NODE
    # Clean up intermediate files generated by agent_write_json_configs
    rm -f "$HOME/.zcode/hooks.json" "$HOME/.zcode/mcp.json" 2>/dev/null
    rm -rf "$OV_HOME/agent-integrations/zcode"
    info "$(t 'Removed ZCode OpenViking hooks and MCP config.' '已移除 ZCode OpenViking hooks 与 MCP 配置。')"
  fi
  if contains_harness kimicode; then
    local kimi_home="${KIMI_CODE_HOME:-$HOME/.kimi-code}" kimicode_lib kimicode_installed
    kimicode_lib="$OV_HOME/agent-integrations/kimicode/lib/install"
    kimicode_installed="$($NODE_BIN - "$kimi_home" <<'NODE'
const fs = require("node:fs");
const path = require("node:path");
const file = path.join(process.argv[2], "plugins", "installed.json");
if (!fs.existsSync(file)) process.stdout.write("no");
else {
  try {
    const value = JSON.parse(fs.readFileSync(file, "utf8"));
    process.stdout.write(Array.isArray(value.plugins)
      && value.plugins.some((plugin) => plugin?.id === "openviking-memory") ? "yes" : "no");
  } catch { process.stdout.write("invalid"); }
}
NODE
)"
    [ -e "$kimi_home/plugins/managed/openviking-memory" ] && kimicode_installed="yes"
    if [ "$kimicode_installed" = "invalid" ]; then
      err "$(t 'Kimi Code plugin registry is invalid; refusing to overwrite it.' 'Kimi Code 插件注册表无效,拒绝覆盖。')"
      return 1
    fi
    if [ "$kimicode_installed" = "yes" ]; then
      [ -f "$kimicode_lib/kimicode-plugin.mjs" ] || {
        err "$(t 'Kimi Code uninstall runtime is missing.' 'Kimi Code 卸载运行时缺失。')"
        return 1
      }
      "$NODE_BIN" "$kimicode_lib/kimicode-plugin.mjs" remove "$kimi_home" || return 1
    fi
    rm -rf "$OV_HOME/agent-integrations/kimicode"
    info "$(t 'Removed the native Kimi Code plugin.' '已移除 Kimi Code 原生插件。')"
  fi
  if contains_harness codebuddy; then
    # The host CLI resolves a plugin by name@marketplace, so the plugin leaves
    # before the marketplace that served it. Its files stay until the host has
    # let go of it: removing them under an enabled plugin breaks every hook.
    local codebuddy_removed=0 codebuddy_list
    if command -v codebuddy >/dev/null 2>&1; then
      if codebuddy plugin uninstall openviking-memory@openviking >/dev/null 2>&1; then
        codebuddy_removed=1
      elif codebuddy_list="$(codebuddy plugin list 2>/dev/null)"; then
        case "$codebuddy_list" in
          *openviking-memory*) ;;
          *) codebuddy_removed=1 ;;
        esac
      fi
    fi
    if [ "$codebuddy_removed" = 1 ]; then
      codebuddy plugin marketplace remove openviking >/dev/null 2>&1 || true
      # The host CLI unregisters a plugin but keeps its cached copy, so the
      # marketplace's cache directory goes too.
      rm -rf "$HOME/.codebuddy/plugins/cache/openviking"
      rm -rf "$OV_HOME/agent-integrations/codebuddy"
      info "$(t 'Removed the CodeBuddy Code plugin and its local marketplace.' '已移除 CodeBuddy Code 插件及其本地 marketplace。')"
    elif ! command -v codebuddy >/dev/null 2>&1 \
      && [ ! -e "$HOME/.codebuddy/plugins/cache/openviking" ] \
      && [ ! -e "$OV_HOME/agent-integrations/codebuddy" ]; then
      :
    else
      warn "$(t 'Could not uninstall the CodeBuddy Code plugin through the codebuddy CLI; its files were left in place. Remove it with:' '未能通过 codebuddy CLI 卸载 CodeBuddy Code 插件,其文件已保留。用这些命令移除:') codebuddy plugin uninstall openviking-memory@openviking; codebuddy plugin marketplace remove openviking; rm -rf \"$HOME/.codebuddy/plugins/cache/openviking\" \"$OV_HOME/agent-integrations/codebuddy\""
    fi
  fi
  if [ ! -d "$OV_HOME/agent-integrations/cursor" ] \
    && [ ! -d "$OV_HOME/agent-integrations/trae" ] \
    && [ ! -d "$OV_HOME/agent-integrations/trae-cn" ] \
    && [ ! -d "$OV_HOME/agent-integrations/trae-cli" ] \
    && [ ! -d "$OV_HOME/agent-integrations/zcode" ] \
    && [ ! -d "$OV_HOME/agent-integrations/kimicode" ] \
    && [ ! -d "$OV_HOME/agent-integrations/codebuddy" ]; then
    rm -rf "$OV_HOME/agent-integrations/memory-plugin-shared"
  fi
  if contains_harness cli; then
    info "$(t 'The CLI is removed with:' 'CLI 用这条命令卸载:') npm uninstall -g $CLI_PACKAGE"
  fi
}

cursor_mcp_path() {
  printf '%s' "$HOME/.cursor/mcp.json"
}

cursor_legacy_claude_plugins() {
  local registry="$CC_CONFIG_DIR/plugins/installed_plugins.json"
  [ -f "$registry" ] || return 0
  "$NODE_BIN" - "$registry" "$PLUGIN_ID" <<'NODE' 2>/dev/null || true
const fs = require("node:fs");
const [file, currentId] = process.argv.slice(2);
const parsed = JSON.parse(fs.readFileSync(file, "utf8"));
const ids = Object.keys(parsed.plugins || {}).filter((id) => /openviking/i.test(id) && id !== currentId);
process.stdout.write(ids.join(", "));
NODE
}

trae_mcp_path() { # trae_mcp_path <client-id>
  local client_id="$1"
  if [ "$(uname -s)" = "Darwin" ]; then
    if [ "$client_id" = "trae-cn" ]; then
      printf '%s' "$HOME/Library/Application Support/Trae CN/User/mcp.json"
    else
      printf '%s' "$HOME/Library/Application Support/Trae/User/mcp.json"
    fi
  elif [ "$client_id" = "trae-cn" ]; then
    printf '%s' "$HOME/.trae-cn/mcp.json"
  else
    printf '%s' "$HOME/.trae/mcp.json"
  fi
}

install_cursor() {
  step_heading "$(t 'Cursor integration' 'Cursor 集成')"
  local root hooks_path mcp_path skill skill_tmp legacy_plugins
  ensure_bundle
  root="$(assemble_agent_integration cursor cursor)" || return 1
  hooks_path="$HOME/.cursor/hooks.json"
  mcp_path="$(cursor_mcp_path)"
  agent_write_json_configs cursor "$hooks_path" "$mcp_path" "$root" cursor "$NODE_BIN"
  mkdir -p "$HOME/.cursor/rules" "$HOME/.cursor/skills"
  cp "$root/hosts/cursor/rules/openviking-memory.mdc" "$HOME/.cursor/rules/openviking-memory.mdc"
  for skill in openviking-memory openviking-skills ov-experience-memory; do
    skill_tmp="$HOME/.cursor/skills/$skill.tmp"
    rm -rf "$skill_tmp"
    cp -R "$root/hosts/cursor/skills/$skill" "$skill_tmp"
    rm -rf "$HOME/.cursor/skills/$skill"
    mv "$skill_tmp" "$HOME/.cursor/skills/$skill"
  done
  info "$(t 'Cursor hooks installed:' 'Cursor hooks 已安装:') $hooks_path"
  info "$(t 'Cursor MCP installed:' 'Cursor MCP 已安装:') $mcp_path"
  info "$(t 'Cursor Rule and Skill installed under ~/.cursor.' 'Cursor Rule 与 Skill 已安装到 ~/.cursor。')"
  legacy_plugins="$(cursor_legacy_claude_plugins)"
  if [ -n "$legacy_plugins" ]; then
    warn "$(t 'Cursor may also import these older Claude OpenViking plugins and run duplicate Hooks:' 'Cursor 还可能导入以下旧版 Claude OpenViking 插件并重复执行 Hook:') $legacy_plugins"
    warn "$(t 'Upgrade or remove those legacy plugin ids, then restart Cursor.' '请升级或移除这些旧插件 id,然后重启 Cursor。')"
  fi
}

zcode_mcp_path() {
  printf '%s' "$HOME/.zcode/mcp.json"
}

zcode_merge_config() { # zcode_merge_config <config_path> <hooks_path> <mcp_path>
  local lib
  lib="$(require_install_lib_dir)" || return 1
  "$NODE_BIN" "$lib/host-json-config.mjs" merge-zcode "$1" "$2" "$3"
}

install_zcode() {
  step_heading "$(t 'ZCode integration' 'ZCode 集成')"
  local root hooks_path mcp_path config_path
  ensure_bundle
  root="$(assemble_agent_integration zcode zcode)" || return 1
  hooks_path="$HOME/.zcode/hooks.json"
  mcp_path="$(zcode_mcp_path)"
  config_path="$HOME/.zcode/cli/config.json"
  mkdir -p "$HOME/.zcode/cli"
  agent_write_json_configs zcode "$hooks_path" "$mcp_path" "$root" zcode "$NODE_BIN"
  # ZCode reads hooks from config.json → hooks.events, not a standalone hooks.json.
  # Merge the generated hooks.json and mcp.json into config.json so ZCode picks them up.
  zcode_merge_config "$config_path" "$hooks_path" "$mcp_path" \
    || { warn "$(t 'Failed to merge ZCode config' 'ZCode 配置合并失败')"; return 1; }
  info "$(t 'ZCode hooks installed:' 'ZCode hooks 已安装:') $config_path (hooks.events)"
  info "$(t 'ZCode MCP installed:' 'ZCode MCP 已安装:') $config_path (mcp.servers)"
}

install_kimicode() {
  step_heading "$(t 'Kimi Code CLI integration' 'Kimi Code CLI 集成')"
  local bundle kimi_home install_lib persisted_dir persisted_tmp persisted_backup root
  ensure_bundle
  bundle="$OV_HOME/agent-integrations/kimicode-bundle.$$"
  rm -rf "$bundle"
  mkdir -p "$bundle"
  root="$(assemble_agent_integration kimicode kimicode "$bundle")" || {
    rm -rf "$bundle"
    return 1
  }
  rm -rf "$root/tests"
  rm -f "$root/README.md" "$root/README_CN.md" "$root/DESIGN.md" "$root/plugin.json"
  cp "$root/hosts/kimicode/kimi.plugin.json" "$bundle/kimi.plugin.json" || {
    rm -rf "$bundle"
    return 1
  }
  cp "$root/hosts/kimicode/runtime-index.mjs" "$root/hosts/index.mjs" || {
    rm -rf "$bundle"
    return 1
  }
  local host_module
  for host_module in "$root"/hosts/*.mjs; do
    case "$(basename "$host_module")" in
      index.mjs|incremental-turn-capture.mjs|kimicode.mjs|kimicode-turns.mjs) ;;
      *) rm -f "$host_module" ;;
    esac
  done
  rm -rf "$root/hosts/kimicode"
  kimi_home="${KIMI_CODE_HOME:-$HOME/.kimi-code}"
  install_lib="$(require_install_lib_dir)" || { rm -rf "$bundle"; return 1; }
  persisted_dir="$OV_HOME/agent-integrations/kimicode/lib/install"
  persisted_tmp="$persisted_dir.tmp.$$"
  persisted_backup="$persisted_dir.previous.$$"
  rm -rf "$persisted_tmp"
  mkdir -p "$persisted_tmp"
  cp "$install_lib/kimicode-plugin.mjs" "$persisted_tmp/kimicode-plugin.mjs" || {
    rm -rf "$bundle" "$persisted_tmp"
    return 1
  }
  mkdir -p "$(dirname "$persisted_dir")"
  rm -rf "$persisted_backup"
  [ ! -e "$persisted_dir" ] || mv "$persisted_dir" "$persisted_backup"
  mv "$persisted_tmp" "$persisted_dir" || {
    [ ! -e "$persisted_backup" ] || mv "$persisted_backup" "$persisted_dir"
    rm -rf "$bundle"
    return 1
  }
  "$NODE_BIN" "$persisted_dir/kimicode-plugin.mjs" install "$kimi_home" "$bundle" || {
    rm -rf "$bundle" "$persisted_dir"
    [ ! -e "$persisted_backup" ] || mv "$persisted_backup" "$persisted_dir"
    return 1
  }
  rm -rf "$persisted_backup"
  rm -rf "$bundle"
  info "$(t 'Kimi Code native plugin installed:' 'Kimi Code 原生插件已安装:') openviking-memory"
}

install_codebuddy() {
  step_heading "$(t 'CodeBuddy Code CLI integration' 'CodeBuddy Code CLI 集成')"
  local bundle root config marketplace version host_module
  ensure_bundle
  bundle="$OV_HOME/agent-integrations/codebuddy-bundle.$$"
  rm -rf "$bundle"
  mkdir -p "$bundle"
  root="$(assemble_agent_integration codebuddy codebuddy "$bundle")" || { rm -rf "$bundle"; return 1; }
  rm -rf "$root/tests"
  rm -f "$root/README.md" "$root/README_CN.md" "$root/DESIGN.md" "$root/plugin.json"
  # Keep this host's config directory aside before the adapters are pruned.
  config="$bundle/codebuddy-config"
  cp -R "$root/hosts/codebuddy" "$config" || { rm -rf "$bundle"; return 1; }
  cp "$config/runtime-index.mjs" "$root/hosts/index.mjs" || { rm -rf "$bundle"; return 1; }
  for host_module in "$root"/hosts/*.mjs; do
    case "$(basename "$host_module")" in
      index.mjs|incremental-turn-capture.mjs|codebuddy.mjs|codebuddy-turns.mjs) ;;
      *) rm -f "$host_module" ;;
    esac
  done
  rm -rf "$root/hosts/codebuddy"
  # The plugin directory CodeBuddy copies into its cache has to carry the whole
  # agent-hook layout: the adapters import `../../memory-plugin-shared/lib`,
  # which only resolves when the shared runtime sits beside them under
  # agent-integrations/ (the shape the Kimi Code bundle uses).
  mkdir -p "$bundle/openviking-memory"
  mv "$bundle/agent-integrations" "$bundle/openviking-memory/agent-integrations" || { rm -rf "$bundle"; return 1; }
  root="$bundle/openviking-memory"
  # Flatten the config directory into the layout CodeBuddy reads: the manifest
  # under .codebuddy-plugin/, hooks under hooks/, MCP at the root.
  mkdir -p "$root/.codebuddy-plugin" "$root/hooks"
  cp "$config/plugin.json" "$root/.codebuddy-plugin/plugin.json" || { rm -rf "$bundle"; return 1; }
  cp "$config/hooks.json" "$root/hooks/hooks.json" || { rm -rf "$bundle"; return 1; }
  cp "$config/.mcp.json" "$root/.mcp.json" || { rm -rf "$bundle"; return 1; }
  cp -R "$config/skills" "$root/skills" || { rm -rf "$bundle"; return 1; }
  rm -rf "$config"
  # A marketplace cannot reference a source outside itself, so the marketplace
  # root is the plugin's parent directory (hosts/codebuddy/DESIGN.md §9).
  # CodeBuddy resolves an installed plugin by the manifest version, so the
  # marketplace entry pins the same one the plugin manifest reports.
  marketplace="$bundle"
  version="$("$NODE_BIN" -e 'process.stdout.write(String(JSON.parse(require("node:fs").readFileSync(process.argv[1],"utf8")).version||""))' "$root/.codebuddy-plugin/plugin.json")"
  "$NODE_BIN" - "$marketplace" "$version" <<'NODE' || { rm -rf "$bundle"; return 1; }
const fs = require("node:fs");
const path = require("node:path");
const root = process.argv[2];
const version = process.argv[3];
fs.mkdirSync(path.join(root, ".codebuddy-plugin"), { recursive: true });
fs.writeFileSync(path.join(root, ".codebuddy-plugin", "marketplace.json"), JSON.stringify({
  name: "openviking",
  description: "OpenViking plugins for the CodeBuddy Code CLI (managed by the OpenViking installer).",
  owner: { name: "OpenViking" },
  plugins: [{
    name: "openviking-memory",
    description: "Long-term semantic memory for CodeBuddy Code, powered by OpenViking.",
    version,
    source: "./openviking-memory",
    category: "productivity",
  }],
}, null, 2) + "\n");
NODE
  # Swap the assembled marketplace into place, then register and install
  # through the host CLI when it is on PATH.
  rm -rf "$OV_HOME/agent-integrations/codebuddy"
  mkdir -p "$OV_HOME/agent-integrations/codebuddy"
  mv "$bundle" "$OV_HOME/agent-integrations/codebuddy/marketplace" || { rm -rf "$bundle"; return 1; }
  marketplace="$OV_HOME/agent-integrations/codebuddy/marketplace"
  if command -v codebuddy >/dev/null 2>&1; then
    codebuddy plugin marketplace add "$marketplace" >/dev/null 2>&1 \
      || warn "$(t 'Could not register the local marketplace with codebuddy.' '未能向 codebuddy 注册本地 marketplace。')"
    if codebuddy plugin install openviking-memory@openviking -s user >/dev/null 2>&1; then
      info "$(t 'CodeBuddy Code plugin installed:' 'CodeBuddy Code 插件已安装:') openviking-memory"
    else
      warn "$(t 'Marketplace registered; install the plugin with:' 'marketplace 已注册;用这条命令安装插件:') codebuddy plugin install openviking-memory@openviking"
    fi
  else
    warn "$(t 'codebuddy CLI not found in PATH. Register the marketplace with' 'PATH 中未找到 codebuddy CLI。用这条命令注册 marketplace:') codebuddy plugin marketplace add $marketplace"
  fi
}
install_trae_variant() { # install_trae_variant <trae|trae-cn>
  local client_id="$1" root hooks_path mcp_path
  step_heading "$(tui_bin_label "$client_id" "$client_id") $(t 'integration' '集成')"
  ensure_bundle
  root="$(assemble_agent_integration trae "$client_id")" || return 1
  hooks_path="$HOME/.$client_id/hooks.json"
  mcp_path="$(trae_mcp_path "$client_id")"
  agent_write_json_configs trae "$hooks_path" "$mcp_path" "$root" "$client_id" "$NODE_BIN"
  info "$client_id hooks: $hooks_path"
  info "$client_id MCP: $mcp_path"
}

# ---------------------------------------------------------------------------
# OpenCode
# ---------------------------------------------------------------------------

install_opencode() {
  step_heading "$(t 'OpenCode plugin' 'OpenCode 插件')"
  if ! command -v opencode >/dev/null 2>&1; then
    not_done "$(t 'opencode CLI not found; skipping OpenCode install.' '未找到 opencode 命令,跳过 OpenCode 安装。')"
    return 0
  fi
  local plugin_dir dest
  ensure_bundle
  plugin_dir="$(plugin_dir_on_disk opencode-plugin)" || {
    warn "$(t 'OpenCode plugin sources not found; skipping.' '未找到 OpenCode 插件源码,跳过。')"
    return 0
  }
  sync_shared_runtime || return 1
  # Import resolves the complete dependency graph; node --check only parses.
  "$NODE_BIN" --input-type=module -e 'import { pathToFileURL } from "node:url"; await import(pathToFileURL(process.argv[2]));' \
    check-runtime "$plugin_dir/servers/mcp-proxy.mjs" || return 1
  dest="$HOME/.config/opencode/plugins/openviking"
  mkdir -p "$(dirname "$dest")"
  if [ "$SOURCE_MODE" = "dev" ]; then
    rm -rf "$dest"
    ln -sfn "$plugin_dir" "$dest"
  else
    rm -rf "$dest.tmp" "$dest"
    mkdir -p "$dest.tmp"
    (cd "$plugin_dir" && tar --exclude node_modules --exclude .git -cf - .) | (cd "$dest.tmp" && tar -xf -)
    mv "$dest.tmp" "$dest"
  fi
  if [ -f "$plugin_dir/wrappers/openviking.js" ]; then
    cp "$plugin_dir/wrappers/openviking.js" "$HOME/.config/opencode/plugins/openviking.js"
  else
    printf '%s\n' 'export { OpenVikingPlugin, default } from "./openviking/index.mjs"' > "$HOME/.config/opencode/plugins/openviking.js"
  fi
  opencode_write_config "$(opencode_config_file)" "$dest/servers/mcp-proxy.mjs"
  info "$(t 'OpenCode file plugin installed:' 'OpenCode 文件插件已安装:') $dest"
}

opencode_config_file() {
  local json="$HOME/.config/opencode/opencode.json" jsonc="$HOME/.config/opencode/opencode.jsonc"
  if [ -f "$jsonc" ] && grep -q '"plugin"' "$jsonc" 2>/dev/null; then printf '%s' "$jsonc"; return; fi
  if [ -f "$json" ]; then printf '%s' "$json"; return; fi
  if [ -f "$jsonc" ]; then printf '%s' "$jsonc"; return; fi
  printf '%s' "$json"
}

opencode_write_config() {
  local cfg="$1" mcp_proxy="$2" lib
  lib="$(require_install_lib_dir)" || return 1
  mkdir -p "$(dirname "$cfg")"
  [ -f "$cfg" ] || printf '{\n}\n' > "$cfg"
  cp "$cfg" "$cfg.bak.$(date +%Y%m%d-%H%M%S)"
  # No npm plugin spec, so jsonc-edit drops one an older install registered.
  "$NODE_BIN" "$lib/jsonc-edit.mjs" "$cfg" "" "$mcp_proxy"
}

# ---------------------------------------------------------------------------
# pi
# ---------------------------------------------------------------------------

# Whole-directory installs need generated dependencies before copying. Flat
# marketplace archives already contain them and have no source generator.
sync_shared_runtime() {
  local shared root
  shared="$(plugin_dir_on_disk memory-plugin-shared)" || return 0
  shared="$(cd "$shared" && pwd -P)" || return 1
  root="$(cd "$shared/../.." 2>/dev/null && pwd)" || return 0
  [ -f "$shared/sync.mjs" ] && [ -d "$root/examples/memory-plugin-shared" ] || return 0
  "$NODE_BIN" "$shared/sync.mjs" >/dev/null
}

install_pi() {
  step_heading "$(t 'pi extension' 'pi 扩展')"
  if ! command -v pi >/dev/null 2>&1; then
    not_done "$(t 'pi CLI not found; skipping pi extension install.' '未找到 pi 命令,跳过 pi 扩展安装。')"
    return 0
  fi
  command -v npm >/dev/null 2>&1 || { err "pi: npm is required to install the MCP client"; return 1; }
  "$NODE_BIN" -e 'const [major, minor] = process.versions.node.split(".").map(Number); process.exit(major > 22 || (major === 22 && minor >= 19) ? 0 : 1)' || {
    err "pi: Node.js 22.19.0 or newer is required"
    return 1
  }
  local plugin_dir dest tmp
  ensure_bundle
  plugin_dir="$(plugin_dir_on_disk pi-coding-agent-extension)" || {
    warn "$(t 'pi extension sources not found; skipping.' '未找到 pi 扩展源码,跳过。')"
    return 0
  }
  sync_shared_runtime
  if [ ! -f "$plugin_dir/shared/credentials.mjs" ] \
    || [ ! -f "$plugin_dir/shared/ov-http.mjs" ] \
    || [ ! -f "$plugin_dir/shared/mcp-proxy-config.mjs" ]; then
    warn "$(t 'pi extension shared runtime is missing; run node examples/memory-plugin-shared/sync.mjs and retry.' '未找到 pi 扩展的共享运行时;请先运行 node examples/memory-plugin-shared/sync.mjs 再重试。')"
    return 0
  fi
  dest="$HOME/.pi/agent/extensions/openviking"
  tmp="$dest.tmp"
  rm -rf "$tmp"
  mkdir -p "$tmp"
  (cd "$plugin_dir" && tar --exclude node_modules --exclude .git -cf - .) | (cd "$tmp" && tar -xf -)
  if ! (cd "$tmp" && npm ci --omit=dev --ignore-scripts --no-audit --no-fund \
      && "$NODE_BIN" --input-type=module -e 'await import("./lib/mcp-bridge.mjs")'); then
    err "pi: dependency installation failed; the existing extension was kept"
    rm -rf "$tmp"
    return 1
  fi
  rm -rf "$dest"
  mkdir -p "$(dirname "$dest")"
  mv "$tmp" "$dest"
  # ~/.pi/agent/extensions/ is one of pi's auto-discovery roots, so copying the
  # extension there is enough for pi to load it. Do NOT also `pi install` the
  # same path: that adds a "packages" entry pointing at the directory while
  # auto-discovery already found its index.ts, and pi dedupes on the canonical
  # path — a directory and its index.ts don't match, so the extension loads
  # twice (duplicate /viking command). Instead, purge any stale packages entry
  # left by older installer versions; `pi remove` on a local path only edits
  # settings and never deletes the copied files.
  pi remove "$dest" >/dev/null 2>&1 || true
  info "$(t 'pi extension installed (auto-discovered):' 'pi 扩展已安装(自动发现):') $dest"
}

# ---------------------------------------------------------------------------
# Validation
# ---------------------------------------------------------------------------

# Cursor, TRAE, TRAE CN and ZCode run the same hook runtime; they differ only
# in where their hooks and MCP entries live and in the session-start event name.
validate_hook_host() { # validate_hook_host <client>
  local client="$1" root="$OV_HOME/agent-integrations/$1" shared="$OV_HOME/agent-integrations/memory-plugin-shared/lib"
  local hooks mcp event file complete=1
  set -- "$root/scripts/hook.mjs" "$root/scripts/uri-guard.mjs" "$root/integration.json" "$shared/agent-hook-runtime.mjs"
  case "$client" in
    cursor)
      hooks="$HOME/.cursor/hooks.json"; mcp="$(cursor_mcp_path)"; event=sessionStart
      set -- "$@" "$root/plugin.json" "$shared/uri-guard.mjs" "$HOME/.cursor/rules/openviking-memory.mdc" \
        "$HOME/.cursor/skills/openviking-memory/SKILL.md" "$HOME/.cursor/skills/openviking-skills/SKILL.md" \
        "$HOME/.cursor/skills/ov-experience-memory/SKILL.md"
      ;;
    zcode) hooks="$HOME/.zcode/cli/config.json"; mcp="$hooks"; event=session-start ;;
    *) hooks="$HOME/.$client/hooks.json"; mcp="$(trae_mcp_path "$client")"; event=session-start ;;
  esac
  for file in "$@"; do
    [ -f "$file" ] || complete=0
  done
  grep -q 'scripts/hook.mjs' "$hooks" 2>/dev/null \
    && grep -q 'scripts/uri-guard.mjs' "$hooks" 2>/dev/null \
    && grep -q 'OPENVIKING_INTEGRATION_ID' "$hooks" 2>/dev/null \
    && grep -q 'mcp-proxy.mjs' "$mcp" 2>/dev/null || complete=0
  if [ "$complete" -eq 0 ]; then
    warn "$client: $(t 'OpenViking hook or MCP config is incomplete' 'OpenViking hook 或 MCP 配置不完整')"
    return 1
  fi
  "$NODE_BIN" --check "$root/scripts/hook.mjs" && "$NODE_BIN" --check "$root/scripts/uri-guard.mjs" || return 1
  if ! printf '%s' '{}' | OPENVIKING_MEMORY_ENABLED=0 "$NODE_BIN" "$root/scripts/hook.mjs" "$event" "$client" >/dev/null; then
    warn "$client: $(t 'installed Hook runtime failed its smoke test' '已安装的 Hook 运行时 smoke test 失败')"
    return 1
  fi
  info "$client: $(t 'hooks and MCP are configured' 'hooks 与 MCP 已配置')"
}

validate_install() {
  step_heading "$(t 'Validation' '安装校验')"
  local ok=1 agent_fatal=0 cached list bin client
  if contains_harness claude; then
    while IFS= read -r bin; do
      [ -n "$bin" ] || continue
      command -v "$bin" >/dev/null 2>&1 || continue
      CLAUDE_BIN="$bin"
      if has_plugin_subcommand; then
        list="$(claude_cmd plugin list 2>/dev/null || true)"
        if str_contains "$list" "$PLUGIN_NAME"; then
          info "$CLAUDE_BIN: $PLUGIN_NAME $(t 'visible in plugin list' '已出现在插件列表')"
        else
          warn "$CLAUDE_BIN: $PLUGIN_NAME $(t 'not visible in plugin list' '未出现在插件列表')"
          ok=0
        fi
      fi
    done <<EOF
$CLAUDE_BINS
EOF
  fi
  if contains_harness codex; then
    while IFS= read -r bin; do
      [ -n "$bin" ] || continue
      command -v "$bin" >/dev/null 2>&1 || continue
      CODEX_BIN="$bin"
      list="$(codex_cmd plugin list 2>/dev/null || true)"
      if str_contains "$list" "$PLUGIN_NAME"; then
        info "$CODEX_BIN: $PLUGIN_NAME $(t 'visible in plugin list' '已出现在插件列表')"
      else
        warn "$CODEX_BIN: $PLUGIN_NAME $(t 'not visible in plugin list' '未出现在插件列表')"
        ok=0
      fi
      if is_native_codex_bin; then
        cached=$(find "$CODEX_DIR/plugins/cache/$MARKETPLACE_NAME/$PLUGIN_NAME" -name 'mcp-proxy.mjs' -path '*/servers/*' 2>/dev/null | sort | tail -n 1 || true)
        if [ -n "$cached" ]; then
          node --check "$cached" && info "codex: $(t 'cached stdio proxy parses' '缓存中的 stdio 代理语法正常') ($cached)" || ok=0
        fi
      fi
    done <<EOF
$CODEX_BINS
EOF
  fi
  for client in cursor trae trae-cn zcode; do
    contains_harness "$client" || continue
    validate_hook_host "$client" || { ok=0; agent_fatal=1; }
  done
  if contains_harness kimicode; then
    local kimi_home="${KIMI_CODE_HOME:-$HOME/.kimi-code}"
    local kimi_root="$kimi_home/plugins/managed/openviking-memory"
    local kimi_installer="$OV_HOME/agent-integrations/kimicode/lib/install/kimicode-plugin.mjs"
    if [ -f "$kimi_installer" ] \
      && "$NODE_BIN" "$kimi_installer" verify "$kimi_home" \
      && [ -f "$kimi_root/kimi.plugin.json" ] \
      && [ -f "$kimi_root/agent-integrations/kimicode/scripts/hook.mjs" ] \
      && [ -f "$kimi_root/agent-integrations/kimicode/scripts/uri-guard.mjs" ] \
      && [ -f "$kimi_root/agent-integrations/kimicode/servers/mcp-proxy.mjs" ] \
      && [ -f "$kimi_root/agent-integrations/memory-plugin-shared/lib/agent-hook-runtime.mjs" ]; then
      "$NODE_BIN" --check "$kimi_root/agent-integrations/kimicode/scripts/hook.mjs" \
        || { ok=0; agent_fatal=1; }
      if ! printf '%s' '{}' | env HOME="$HOME" OPENVIKING_MEMORY_ENABLED=0 \
        "$NODE_BIN" "$kimi_root/agent-integrations/kimicode/scripts/hook.mjs" session-start kimicode >/dev/null; then
        warn "kimicode: $(t 'installed Hook runtime failed its smoke test' '已安装的 Hook 运行时 smoke test 失败')"
        ok=0; agent_fatal=1
      fi
      info "kimicode: $(t 'native plugin installed (Hooks + MCP)' '原生插件已安装(Hooks + MCP)')"
    else
      warn "kimicode: $(t 'native plugin installation is incomplete' '原生插件安装不完整')"
      ok=0; agent_fatal=1
    fi
  fi
  if contains_harness codebuddy; then
    local cb_root="$OV_HOME/agent-integrations/codebuddy/marketplace/openviking-memory"
    local cb_ai="$cb_root/agent-integrations/codebuddy"
    if [ -f "$cb_root/.codebuddy-plugin/plugin.json" ] \
      && [ -f "$cb_root/hooks/hooks.json" ] \
      && [ -f "$cb_root/.mcp.json" ] \
      && [ -f "$cb_ai/scripts/hook.mjs" ] \
      && [ -f "$cb_ai/scripts/uri-guard.mjs" ] \
      && [ -f "$cb_ai/servers/mcp-proxy.mjs" ] \
      && [ -f "$cb_root/agent-integrations/memory-plugin-shared/lib/agent-hook-runtime.mjs" ]; then
      "$NODE_BIN" --check "$cb_ai/scripts/hook.mjs" || { ok=0; agent_fatal=1; }
      if ! printf '%s' '{}' | OPENVIKING_MEMORY_ENABLED=0 \
        "$NODE_BIN" "$cb_ai/scripts/hook.mjs" session-start codebuddy >/dev/null; then
        warn "codebuddy: $(t 'installed Hook runtime failed its smoke test' '已安装的 Hook 运行时 smoke test 失败')"
        ok=0; agent_fatal=1
      fi
      if command -v codebuddy >/dev/null 2>&1 \
        && ! codebuddy plugin list 2>/dev/null | grep -q 'openviking-memory'; then
        warn "codebuddy: $(t 'plugin is assembled but not installed through the host CLI' '插件已组装但未通过宿主 CLI 安装')"
        ok=0; agent_fatal=1
      fi
      info "codebuddy: $(t 'plugin assembled (Hooks + MCP + skills)' '插件已组装(Hooks + MCP + skills)')"
    else
      warn "codebuddy: $(t 'plugin assembly is incomplete' '插件组装不完整')"
      ok=0; agent_fatal=1
    fi
  fi
  if contains_harness opencode; then
    local ocfg="$HOME/.config/opencode/opencode.json"
    local ocfgc="$HOME/.config/opencode/opencode.jsonc"
    if [ -f "$HOME/.config/opencode/plugins/openviking.js" ] && [ -f "$HOME/.config/opencode/plugins/openviking/index.mjs" ]; then
      info "opencode: $PLUGIN_NAME $(t 'appears installed' '看起来已安装')"
    else
      warn "opencode: $PLUGIN_NAME $(t 'not found in config/plugin dir' '未在配置或插件目录中找到')"
      ok=0
    fi
    if [ -f "$HOME/.config/opencode/plugins/openviking.js" ]; then
      node --check "$HOME/.config/opencode/plugins/openviking.js" || ok=0
    fi
    if [ -f "$HOME/.config/opencode/plugins/openviking/index.mjs" ]; then
      node --check "$HOME/.config/opencode/plugins/openviking/index.mjs" || ok=0
    fi
    if [ -f "$HOME/.config/opencode/plugins/openviking/servers/mcp-proxy.mjs" ]; then
      node --check "$HOME/.config/opencode/plugins/openviking/servers/mcp-proxy.mjs" || ok=0
    else
      warn "opencode: $(t 'OpenViking MCP proxy not found' '未找到 OpenViking MCP proxy')"
      ok=0
    fi
    if grep -q '"openviking"' "$ocfg" "$ocfgc" 2>/dev/null && grep -q '"mcp"' "$ocfg" "$ocfgc" 2>/dev/null; then
      info "opencode: $(t 'MCP server registered' 'MCP server 已注册')"
    else
      warn "opencode: $(t 'MCP server not found in config' '配置中未找到 MCP server')"
      ok=0
    fi
  fi
  if contains_harness pi; then
    if [ -f "$HOME/.pi/agent/extensions/openviking/index.ts" ] || [ -f "$HOME/.pi/agent/extensions/openviking/index.js" ]; then
      info "pi: $PLUGIN_NAME $(t 'extension files present (auto-discovered)' '扩展文件已存在(自动发现)')"
    else
      warn "pi: $PLUGIN_NAME $(t 'extension files not found' '未找到扩展文件')"
      ok=0
    fi
    if command -v pi >/dev/null 2>&1; then
      # The extension lives under pi's auto-discovery root, so it must NOT appear
      # as a configured "packages" entry — a stale entry there loads it twice.
      if pi list 2>/dev/null | grep -q 'extensions/openviking'; then
        warn "pi: $PLUGIN_NAME $(t 'still registered as a package (duplicate load); run pi remove ~/.pi/agent/extensions/openviking' '仍作为 package 注册(会重复加载);请运行 pi remove ~/.pi/agent/extensions/openviking')"
        ok=0
      fi
    fi
    if [ -f "$HOME/.pi/agent/extensions/openviking/shared/recall-core.mjs" ]; then
      node --check "$HOME/.pi/agent/extensions/openviking/shared/recall-core.mjs" || ok=0
    fi
    (cd "$HOME/.pi/agent/extensions/openviking" && "$NODE_BIN" --input-type=module \
      -e 'await import("./lib/mcp-bridge.mjs")') || ok=0
    if [ -f "$HOME/.pi/agent/extensions/openviking/shared/mcp-proxy-config.mjs" ]; then
      node --check "$HOME/.pi/agent/extensions/openviking/shared/mcp-proxy-config.mjs" || ok=0
    fi
  fi
  if contains_harness dsh && have_npm_dsh; then
    local dsh_profile="${DSH_PROFILE:-$DSH_PROFILE_DEFAULT}"
    if dsh plugin --profile "$dsh_profile" ls 2>/dev/null | grep -q "$DSH_PACKAGE"; then
      info "dsh: $DSH_PACKAGE $(t 'installed in profile' '已安装到 profile') $dsh_profile"
    else
      warn "dsh: $DSH_PACKAGE $(t 'not found in profile' '未在 profile 中找到') $dsh_profile"
      ok=0
    fi
    if dsh --profile "$dsh_profile" --dump-config 2>/dev/null | grep -q 'openviking-memory-runtime'; then
      info "dsh: $(t 'plugin composed into the profile' '插件已合入 profile')"
    else
      warn "dsh: $(t 'plugin not present in the composed profile' '合成后的 profile 中没有该插件')"
      ok=0
    fi
  fi
  if contains_harness dsh-desktop && [ "$DSH_DESKTOP_INSTALLED" -eq 1 ]; then
    if str_contains "$("$DSH_DESKTOP_CLI" plugin --profile desktop ls 2>/dev/null || true)" "$DSH_PACKAGE"; then
      info "dsh-desktop: $DSH_PACKAGE $(t 'installed in DeepSeek Harness Desktop' '已安装到 DeepSeek Harness Desktop')"
    else
      warn "dsh-desktop: $DSH_PACKAGE $(t 'not found in DeepSeek Harness Desktop' '未在 DeepSeek Harness Desktop 中找到')"
      ok=0
    fi
    # No dsh boots Desktop's profile, so there is no --dump-config to read;
    # Desktop loads the bundles its profile manifest lists.
    if "$NODE_BIN" -e '
      const m = JSON.parse(require("node:fs").readFileSync(process.argv[1], "utf8"));
      process.exit((m.dsh?.profile?.bundles ?? []).includes(process.argv[2]) ? 0 : 1);
    ' "$(dsh_home)/profiles/desktop/package.json" "$DSH_PACKAGE" 2>/dev/null; then
      info "dsh-desktop: $(t 'plugin enabled in the Desktop profile' '插件已在 Desktop profile 中启用')"
    else
      warn "dsh-desktop: $(t 'plugin not enabled in the Desktop profile; enable it on the Plugins page' 'Desktop profile 中未启用该插件;请在“插件”页启用')"
      ok=0
    fi
  fi
  if [ -n "$MKT_DIR" ] && [ -f "$MKT_DIR/claude-code-memory-plugin/scripts/marketplace.test.mjs" ] && [ -d "$MKT_DIR/../.git" ]; then
    node --test "$MKT_DIR/claude-code-memory-plugin/scripts/marketplace.test.mjs" \
      "$MKT_DIR/codex-memory-plugin/scripts/marketplace.test.mjs" || ok=0
  fi
  if [ "$agent_fatal" -ne 0 ]; then
    err "$(t 'Installation validation failed. No success result will be reported.' '安装校验失败,不会报告安装成功。')"
    return 1
  fi
  if [ "$ok" -ne 1 ]; then
    warn "$(t 'Validation reported issues — the install may still work; check the messages above.' '校验发现问题——安装可能仍然可用,请检查上方输出。')"
  fi
}

# ---------------------------------------------------------------------------
# OpenViking CLI (npm only)
# ---------------------------------------------------------------------------

dir_writable() { # dir_writable <dir>: the directory, or the nearest one above it that exists, is writable
  local d="$1"
  while [ ! -e "$d" ]; do d="$(dirname "$d")"; done
  [ -d "$d" ] && [ -w "$d" ]
}

# npm's global prefix, unless writing there needs sudo (a system Node.js on
# Linux, say); then ~/.local, so ov lands in ~/.local/bin.
choose_cli_prefix() {
  contains_harness cli && command -v npm >/dev/null 2>&1 || return 0
  local prefix
  prefix="$(npm prefix -g 2>/dev/null || true)"
  if [ -n "$prefix" ] && dir_writable "$prefix/lib/node_modules" && dir_writable "$prefix/bin"; then
    CLI_PREFIX="$prefix"
  else
    CLI_PREFIX="$HOME/.local"
  fi
}

cli_uninstall_command() {
  if [ "$CLI_PREFIX" = "$HOME/.local" ]; then
    printf 'npm uninstall -g --prefix ~/.local %s' "$CLI_PACKAGE"
  else
    printf 'npm uninstall -g %s' "$CLI_PACKAGE"
  fi
}

install_cli() {
  step_heading "CLI (ov)"
  if [ -z "$CLI_PREFIX" ]; then
    not_done "$(t "npm not found; skipped the CLI. Install it later with: npm install -g $CLI_PACKAGE" "未找到 npm,跳过 CLI。之后可以运行:npm install -g $CLI_PACKAGE")"
    return 0
  fi
  local ov="$CLI_PREFIX/bin/ov" found version args
  # The platform binary comes in as an optional dependency; the package's own
  # postinstall only prints a banner.
  args=(install -g "$CLI_PACKAGE@latest" --ignore-scripts --no-audit --no-fund)
  # npm's own prefix is not handed back: npm masks anything UUID-like in what
  # it prints, paths included.
  [ "$CLI_PREFIX" != "$HOME/.local" ] || args+=(--prefix "$CLI_PREFIX")
  if ! npm "${args[@]}"; then
    not_done "$(t "npm could not install the CLI. Try again later with: npm install -g $CLI_PACKAGE" "npm 安装 CLI 失败。之后可以重试:npm install -g $CLI_PACKAGE")"
    return 0
  fi
  [ -f "$CLI_SETTINGS" ] || "$ov" language "$(t en zh-CN)" >/dev/null 2>&1 || true
  if ! version="$("$ov" --version 2>/dev/null)"; then
    not_done "$(t "The CLI was installed but $ov --version failed." "CLI 已安装,但 $ov --version 运行失败。")"
    return 0
  fi
  CLI_INSTALLED=1
  info "$(t 'CLI installed:' 'CLI 已安装:') $ov ($version)"
  hash -r
  found="$(command -v ov || true)"
  if [ -z "$found" ]; then
    warn "$(t "$(short_path "$CLI_PREFIX/bin") is not on PATH; add it to run ov." "$(short_path "$CLI_PREFIX/bin") 不在 PATH 中,加进去后才能直接运行 ov。")"
  elif [ "$found" != "$ov" ]; then
    warn "$(t "ov on PATH is $found, not the one just installed; move $(short_path "$CLI_PREFIX/bin") ahead in PATH or remove the other." "PATH 中的 ov 是 ${found},不是刚装的这个;请把 $(short_path "$CLI_PREFIX/bin") 在 PATH 中前移,或删掉另一个。")"
  fi
}

# ---------------------------------------------------------------------------
# Plan, confirmation and next steps
# ---------------------------------------------------------------------------

# Credentials, review and validation, plus one step per install.
count_steps() {
  local h
  STEP_TOTAL=3
  if contains_harness claude; then STEP_TOTAL=$((STEP_TOTAL + $(list_count "$CLAUDE_BINS"))); fi
  if contains_harness codex; then STEP_TOTAL=$((STEP_TOTAL + $(list_count "$CODEX_BINS"))); fi
  for h in cursor trae trae-cn zcode kimicode codebuddy opencode pi dsh dsh-desktop cli; do
    if contains_harness "$h"; then STEP_TOTAL=$((STEP_TOTAL + 1)); fi
  done
}

short_path() {
  case "$1" in
    "$HOME"/*) printf '%s%s' '~' "${1#"$HOME"}" ;;
    *) printf '%s' "$1" ;;
  esac
}

print_selection() {
  info "$(t 'Selected harnesses:' '已选择:') $(printf '%s' "${PUBLIC_SELECTED_HARNESSES:-$SELECTED_HARNESSES}" | tr ',' ' ')"
  if contains_harness claude; then info "$(t 'Claude-format commands:' 'Claude 格式命令:') $(list_words "$CLAUDE_BINS")"; fi
  if [ -n "$TRAECODE_CLI_BIN" ]; then info "TraeCode CLI 2.0: $TRAECODE_CLI_BIN"; fi
  if contains_harness codex && [ -z "$TRAECODE_CLI_BIN" ]; then info "$(t 'Codex-format commands:' 'Codex 格式命令:') $(list_words "$CODEX_BINS")"; fi
  if contains_harness dsh; then info "$(t 'DeepSeek Harness profile:' 'DeepSeek Harness profile:') ${DSH_PROFILE:-$DSH_PROFILE_DEFAULT}"; fi
  if contains_harness dsh-desktop && [ -n "$DSH_DESKTOP_CLI" ]; then info "$(t 'DeepSeek Harness Desktop command:' 'DeepSeek Harness Desktop 命令:') $(short_path "$DSH_DESKTOP_CLI")"; fi
}

plan_item() { printf '      %s\n' "$*"; }

plan_paths() { # plan_paths <label> <path...>
  local label="$1" out="" path
  shift
  for path in "$@"; do out="${out:+$out, }$(short_path "$path")"; done
  plan_item "$label: $out"
}

claude_plan_legacy() {
  local leftovers
  leftovers="$(claude_legacy_leftovers)"
  if list_contains_line "$leftovers" plugin; then
    plan_item "$(t 'Removes the old marketplace' '移除旧 marketplace') $LEGACY_MARKETPLACE_NAME ($CLAUDE_BIN plugin marketplace remove)"
  fi
  if list_contains_line "$leftovers" mcp; then
    plan_item "$(t 'Removes the old MCP server' '移除旧 MCP 服务') openviking ($CLAUDE_BIN mcp remove -s user)"
  fi
  if list_contains_line "$leftovers" hooks; then
    plan_item "$(short_path "$CC_SETTINGS") ($(t 'removes the old OpenViking hooks' '移除旧的 OpenViking hook'))"
  fi
}

print_plan() {
  local bundle origin fallback account user trae_home
  if [ "$SOURCE_MODE" = "dev" ]; then
    bundle="$(short_path "$CHECKOUT_DIR/examples")"
    origin="$(t 'this checkout' '当前 checkout')"
  else
    bundle="$(short_path "$MKT_DIR_ARCHIVE")"
    origin="$(t 'OpenViking release' 'OpenViking 发布版')"
  fi
  fallback="$(t 'fallback:' '失败时改用') $bundle"
  account="$(cred_value "$CRED_ACCOUNT" "$CUR_ACCOUNT")"
  user="$(cred_value "$CRED_USER" "$CUR_USER")"
  step_heading "$(t 'Review' '确认安装内容')"
  print_selection
  info "$(t 'Server:' '服务地址:') $CRED_URL"
  info "API key: $(mask_secret "$(cred_value "$CRED_KEY" "$CUR_KEY")")"
  if [ -n "$account" ]; then info "account: $account"; fi
  if [ -n "$user" ]; then info "user: $user"; fi
  info "$(t 'Source:' '安装来源:') $origin"
  info "$(t 'The install will change:' '安装将修改:')"
  plan_item "$(short_path "$OVCLI_CONF")"
  if contains_harness claude; then
    while IFS= read -r CLAUDE_BIN; do
      [ -n "$CLAUDE_BIN" ] || continue
      command -v "$CLAUDE_BIN" >/dev/null 2>&1 || continue
      has_plugin_subcommand || continue
      if [ "$SOURCE_MODE" = "archive" ] && claude_supports_archive_source; then
        plan_item "$CLAUDE_BIN plugin marketplace add/update $CC_MARKETPLACE_URL ($fallback)"
      else
        plan_item "$CLAUDE_BIN plugin marketplace add/update $bundle"
      fi
      plan_item "$CLAUDE_BIN plugin install/update $PLUGIN_ID"
      claude_plan_legacy
      if is_native_claude_bin && [ "$STATUSLINE_WRITE" -eq 1 ]; then
        plan_item "$(short_path "$CC_SETTINGS") ($(t 'marketplace auto-update, statusline' 'marketplace 自动更新、statusline'))"
      elif is_native_claude_bin; then
        plan_item "$(short_path "$CC_SETTINGS") ($(t 'marketplace auto-update' 'marketplace 自动更新'))"
      fi
    done <<EOF
$CLAUDE_BINS
EOF
  fi
  if contains_harness codex; then
    while IFS= read -r CODEX_BIN; do
      [ -n "$CODEX_BIN" ] || continue
      command -v "$CODEX_BIN" >/dev/null 2>&1 || continue
      if [ "$SOURCE_MODE" = "archive" ]; then
        plan_item "$CODEX_BIN plugin marketplace add/upgrade $CODEX_GIT_URL ($fallback)"
      else
        plan_item "$CODEX_BIN plugin marketplace add/upgrade $bundle"
      fi
      plan_item "$CODEX_BIN plugin add $PLUGIN_ID"
      if codex_has_legacy; then
        plan_item "$(t 'Removes the old marketplace' '移除旧 marketplace') $LEGACY_MARKETPLACE_NAME ($CODEX_BIN plugin marketplace remove)"
      fi
      if is_native_codex_bin; then plan_item "$(short_path "$CODEX_CONFIG")"; fi
      if has_legacy_trae_cli_integration; then
        trae_home="${TRAE_HOME:-$HOME/.trae}"
        plan_paths "$(t 'Removes the deprecated TRAE CLI Hooks integration' '移除弃用的 TRAE CLI Hooks 集成')" \
          "${TRAECLI_HOME:-$trae_home/cli}/hooks.json" "$trae_home/traecli.toml" "$OV_HOME/agent-integrations/trae-cli"
      fi
    done <<EOF
$CODEX_BINS
EOF
  fi
  if [ "$SOURCE_MODE" = "archive" ] && { contains_harness cursor || contains_harness trae || contains_harness trae-cn \
    || contains_harness zcode || contains_harness kimicode || contains_harness codebuddy || contains_harness opencode || contains_harness pi; }; then
    plan_item "$bundle ($(t 'plugin bundle download' '下载插件包'))"
  fi
  if contains_harness cursor; then
    plan_paths Cursor "$HOME/.cursor/hooks.json" "$(cursor_mcp_path)" "$HOME/.cursor/rules/openviking-memory.mdc" \
      "$HOME/.cursor/skills/openviking-memory" "$HOME/.cursor/skills/openviking-skills" \
      "$HOME/.cursor/skills/ov-experience-memory" "$OV_HOME/agent-integrations/cursor"
  fi
  if contains_harness trae; then
    plan_paths TRAE "$HOME/.trae/hooks.json" "$(trae_mcp_path trae)" "$OV_HOME/agent-integrations/trae"
  fi
  if contains_harness trae-cn; then
    plan_paths "TRAE CN" "$HOME/.trae-cn/hooks.json" "$(trae_mcp_path trae-cn)" "$OV_HOME/agent-integrations/trae-cn"
  fi
  if contains_harness zcode; then
    plan_paths ZCode "$HOME/.zcode/cli/config.json" "$HOME/.zcode/hooks.json" "$(zcode_mcp_path)" "$OV_HOME/agent-integrations/zcode"
  fi
  if contains_harness cursor || contains_harness trae || contains_harness trae-cn || contains_harness zcode; then
    plan_paths "$(t 'Shared hook runtime' '共享 hook 运行时')" "$OV_HOME/agent-integrations/memory-plugin-shared"
  fi
  if contains_harness kimicode; then
    plan_paths "Kimi Code" "${KIMI_CODE_HOME:-$HOME/.kimi-code}/plugins" "$OV_HOME/agent-integrations/kimicode"
  fi
  if contains_harness codebuddy; then
    plan_paths "CodeBuddy Code" "$HOME/.codebuddy/plugins" "$OV_HOME/agent-integrations/codebuddy"
  fi
  if contains_harness opencode && command -v opencode >/dev/null 2>&1; then
    plan_paths OpenCode "$HOME/.config/opencode/plugins/openviking" "$HOME/.config/opencode/plugins/openviking.js" "$(opencode_config_file)"
  fi
  if contains_harness pi && command -v pi >/dev/null 2>&1; then
    plan_paths pi "$HOME/.pi/agent/extensions/openviking"
  fi
  if contains_harness dsh && have_npm_dsh; then
    plan_item "dsh plugin --profile ${DSH_PROFILE:-$DSH_PROFILE_DEFAULT} add $DSH_PACKAGE@latest"
  fi
  if contains_harness dsh-desktop; then
    if [ -n "$DSH_DESKTOP_CLI" ] && dsh_desktop_initialized; then
      plan_item "\"$(short_path "$DSH_DESKTOP_CLI")\" plugin --profile desktop add $DSH_PACKAGE@latest ($(t 'DeepSeek Harness Desktop, which must be quit' 'DeepSeek Harness Desktop,需先退出'))"
    else
      plan_item "DeepSeek Harness Desktop: $(t 'nothing; the steps for its Plugins page are printed' '不修改;改为给出其“插件”页的安装步骤')"
    fi
  fi
  if contains_harness cli && [ -n "$CLI_PREFIX" ]; then
    plan_item "npm install -g $CLI_PACKAGE@latest ($(short_path "$CLI_PREFIX/bin/ov"))"
    if [ ! -f "$CLI_SETTINGS" ]; then
      plan_item "$(short_path "$CLI_SETTINGS") ($(t 'CLI display language: English' 'CLI 显示语言:中文'))"
    fi
  elif contains_harness cli; then
    plan_item "$(t 'CLI (ov): skipped, npm not found' 'CLI (ov):未找到 npm,跳过')"
  fi
}

# An ov from pip, cargo or another npm prefix stays where it is, and PATH
# decides which of the two runs.
warn_other_cli() {
  [ -n "$CLI_PREFIX" ] || return 0
  local found
  found="$(command -v ov || true)"
  [ -n "$found" ] && [ "$found" != "$CLI_PREFIX/bin/ov" ] || return 0
  warn "$(t "Another ov is already installed at $found. After the install there will be two, and PATH decides which one runs." "已有另一个 ov:${found}。安装后会有两个 ov,实际运行哪个取决于 PATH 顺序。")"
}

# Without a tty nobody saw the connection menu, so a run that got no server
# says loudly what it assumed and how to fix it.
warn_default_connection() {
  [ "$INTERACTIVE" -eq 0 ] && [ -z "$URL_ARG" ] && [ "$API_KEY_ARG" = "__OPENVIKING_UNSET__" ] \
    && [ -z "$CUR_URL" ] && [ -z "$CUR_KEY" ] || return 0
  local bins=""
  [ -z "$CLAUDE_BINS_ARG" ] || bins="$bins --claude-bin $CLAUDE_BINS_ARG"
  [ -z "$CODEX_BINS_ARG" ] || bins="$bins --codex-bin $CODEX_BINS_ARG"
  printf '\n'
  warn "${BOLD}$(t 'No server was given: the plugin is configured for http://127.0.0.1:1933 without an API key.' '未指定服务:插件将连接 http://127.0.0.1:1933,且不带 API key。')${RESET}"
  warn "$(t 'To use another server, re-run with its URL and API key:' '如需连接其他服务,请带上服务地址和 API key 重新运行:')"
  printf '      %s --yes --harness %s%s --url <server-url> --api-key <api-key>\n\n' \
    "$INSTALL_COMMAND" "${PUBLIC_SELECTED_HARNESSES:-$SELECTED_HARNESSES}" "$bins"
}

confirm_plan() {
  TUI_MENU_QUIT=1 tui_menu "$(t 'Proceed with the install?' '开始安装?')" 0 "$(t 'Proceed' '继续')" "$(t 'Cancel' '取消')"
  if [ "$TUI_MENU_CHOICE" -ne 0 ]; then
    info "$(t 'Cancelled; nothing was changed.' '已取消,未做任何修改。')"
    exit 0
  fi
}

next_steps() { # next_steps <name> <next> <updates> <verify> <uninstall>
  printf '\n  %s%s%s\n' "$BOLD" "$1" "$RESET"
  printf '    %s %s\n' "$(t 'Next:' '下一步:')" "$2"
  printf '    %s %s\n' "$(t 'Updates:' '更新:')" "$3"
  [ -z "$4" ] || printf '    %s %s\n' "$(t 'Verify:' '验证:')" "$4"
  printf '    %s %s\n' "$(t 'Uninstall:' '卸载:')" "$5"
}

# The agent onboarding guide on the docs host the downloads came from, so a
# machine that reached only docs.openviking.net is not sent to .ai. Any other
# download location falls back by language.
onboarding_url() {
  local host=docs.openviking.ai lang=en
  [ "$UI_LANG" != "zh" ] || lang=zh
  case "$DOWNLOAD_BASE" in
    *://docs.openviking.net/*|*://docs.openviking.net) host=docs.openviking.net ;;
    *://docs.openviking.ai/*|*://docs.openviking.ai) ;;
    *) [ "$lang" != "zh" ] || host=docs.openviking.net ;;
  esac
  printf 'https://%s/agents/%s/onboarding.md\n' "$host" "$lang"
}

print_next_steps() {
  local label updates rerun uninstall doctor client remove line
  rerun="$(t 're-run this installer' '重新运行本安装脚本')"
  uninstall="$INSTALL_COMMAND --uninstall --yes --harness"
  heading "$(t 'Done' '完成')"
  if [ -n "$RELEASE_VERSION" ]; then info "$(t 'Release:' '发布版本:') $RELEASE_VERSION"; fi
  info "$(t 'Credentials:' '凭据:') $OVCLI_CONF"
  info "$(t 'Reconfigure later by re-running this installer.' '之后可重跑本安装脚本重新配置。')"
  if contains_harness claude; then
    while IFS= read -r CLAUDE_BIN; do
      [ -n "$CLAUDE_BIN" ] || continue
      command -v "$CLAUDE_BIN" >/dev/null 2>&1 || continue
      has_plugin_subcommand || continue
      label="$(tui_bin_label claude "$CLAUDE_BIN")"
      updates="$rerun"
      if list_contains_line "$CLAUDE_URL_BINS" "$CLAUDE_BIN"; then
        updates="$(t 'automatic; Claude Code checks in the background and loads a new version after a restart' '自动;Claude Code 在后台检查,重启后加载新版本')"
      fi
      next_steps "$label" "$(t "restart $label" "重启 $label")" "$updates" \
        "$(t 'run /openviking-memory:ov in a session' '在会话中运行 /openviking-memory:ov')" \
        "$CLAUDE_BIN plugin uninstall $PLUGIN_ID && $CLAUDE_BIN plugin marketplace remove $MARKETPLACE_NAME"
    done <<EOF
$CLAUDE_BINS
EOF
  fi
  if contains_harness codex; then
    while IFS= read -r CODEX_BIN; do
      [ -n "$CODEX_BIN" ] || continue
      command -v "$CODEX_BIN" >/dev/null 2>&1 || continue
      label="$(codex_bin_label)"
      updates="$rerun"
      if list_contains_line "$CODEX_GIT_BINS" "$CODEX_BIN"; then
        updates="$(t "automatic; $label upgrades the marketplace when it starts" "自动;$label 启动时升级 marketplace")"
      fi
      # Codex has only `plugin remove`, TraeCode CLI only `plugin uninstall`.
      remove=remove
      case "$(bin_basename "$CODEX_BIN")" in trae-cli|traecli|traex) remove=uninstall ;; esac
      next_steps "$label" "$(t "restart $label" "重启 $label")" "$updates" \
        "$(t "run the \$ov-memory-doctor skill in a session" "在会话中运行 \$ov-memory-doctor skill")" \
        "$CODEX_BIN plugin $remove $PLUGIN_ID && $CODEX_BIN plugin marketplace remove $MARKETPLACE_NAME"
    done <<EOF
$CODEX_BINS
EOF
  fi
  for client in cursor trae trae-cn zcode; do
    contains_harness "$client" || continue
    label="$(tui_bin_label "$client" "$client")"
    doctor="node $(short_path "$OV_HOME/agent-integrations/$client/scripts/ov-memory-doctor.mjs")"
    next_steps "$label" "$(t "restart $label" "重启 $label")" "$rerun" "$doctor" "$uninstall $client"
  done
  if contains_harness kimicode; then
    next_steps "Kimi Code" "$(t 'start a new session (not /reload)' '新建会话(不要用 /reload)')" "$rerun" "" "$uninstall kimicode"
  fi
  if contains_harness codebuddy; then
    next_steps "CodeBuddy Code" "$(t 'run /reload-plugins or start a new session' '运行 /reload-plugins 或新建会话')" "$rerun" "" "$uninstall codebuddy"
  fi
  if contains_harness opencode && command -v opencode >/dev/null 2>&1; then
    next_steps OpenCode "$(t 'restart OpenCode' '重启 OpenCode')" "$rerun" "" \
      "$(t "rm -rf ~/.config/opencode/plugins/openviking ~/.config/opencode/plugins/openviking.js, then remove mcp.openviking from $(short_path "$(opencode_config_file)")" "rm -rf ~/.config/opencode/plugins/openviking ~/.config/opencode/plugins/openviking.js,再删除 $(short_path "$(opencode_config_file)") 中的 mcp.openviking")"
  fi
  if contains_harness pi && command -v pi >/dev/null 2>&1; then
    next_steps pi "$(t 'restart pi' '重启 pi')" "$rerun" "" "rm -rf ~/.pi/agent/extensions/openviking"
  fi
  if contains_harness dsh && have_npm_dsh; then
    next_steps "DeepSeek Harness" "$(t "start a new dsh session: dsh --profile ${DSH_PROFILE:-$DSH_PROFILE_DEFAULT}" "新开一个 dsh 会话:dsh --profile ${DSH_PROFILE:-$DSH_PROFILE_DEFAULT}")" "$rerun" "" \
      "dsh plugin --profile ${DSH_PROFILE:-$DSH_PROFILE_DEFAULT} rm $DSH_PACKAGE"
  fi
  if contains_harness dsh-desktop && [ "$DSH_DESKTOP_INSTALLED" -eq 1 ]; then
    next_steps "DeepSeek Harness Desktop" \
      "$(t 'quit DeepSeek Harness Desktop with ⌘Q if it is open, reopen it and start a new conversation' '如 DeepSeek Harness Desktop 已打开,用 ⌘Q 退出后重新打开,再新建对话')" \
      "$(t 're-run this installer, or update it on the Plugins page' '重新运行本安装脚本,或在“插件”页更新')" \
      "\"$DSH_DESKTOP_CLI\" plugin --profile desktop ls" \
      "$(t "with Desktop quit, \"$DSH_DESKTOP_CLI\" plugin --profile desktop rm $DSH_PACKAGE; or Plugins page → $DSH_PACKAGE → Uninstall" "退出 Desktop 后运行 \"$DSH_DESKTOP_CLI\" plugin --profile desktop rm $DSH_PACKAGE;或在“插件”页 → $DSH_PACKAGE → 卸载")"
  elif contains_harness dsh-desktop; then
    next_steps "DeepSeek Harness Desktop" \
      "$(t "open DeepSeek Harness Desktop: Plugins → Add plugin → enter $DSH_PACKAGE → Install, then turn it on if its switch is off" "打开 DeepSeek Harness Desktop:插件 → 添加插件 → 输入 $DSH_PACKAGE → 安装,开关未打开时再打开")" \
      "$(t 'on the Plugins page' '在“插件”页')" "" \
      "$(t "Plugins page → $DSH_PACKAGE → Uninstall" "“插件”页 → $DSH_PACKAGE → 卸载")"
  fi
  if [ "$CLI_INSTALLED" -eq 1 ]; then
    next_steps "CLI (ov)" "$(t 'run ov health, which uses the same credentials' '运行 ov health,它使用同一份凭据')" "$rerun" "" \
      "$(cli_uninstall_command)"
  fi
  printf '\n'
  if [ -n "$NOT_DONE" ]; then
    warn "$(t 'Not done:' '未完成:')"
    while IFS= read -r line; do
      [ -z "$line" ] || printf '      - %s\n' "$line"
    done <<EOF
$NOT_DONE
EOF
  fi
  info "$(t 'Onboarding guide (for your agent):' '接入引导(给 Agent 看):') $(onboarding_url)"
}

# ---------------------------------------------------------------------------
# Main
# ---------------------------------------------------------------------------

UI_LANG="${LANG_ARG:-$(detect_lang_default)}"
resolve_self_checkout
reexec_release_installer ${INSTALLER_ARGS[@]+"${INSTALLER_ARGS[@]}"}
select_language
print_intro

case "$(uname -s)" in
  Darwin|Linux) ;;
  *) err "Unsupported OS: $(uname -s). Only macOS and Linux are supported."; exit 1 ;;
esac
command -v node >/dev/null 2>&1 || {
  err "$(t 'node not found. Install Node.js 18+.' '未找到 node,请先安装 Node.js 18+。')"
  # Desktop brings its own runtime, so a Mac with only Desktop has no Node.js.
  if [ "$HAVE_DSH_DESKTOP" -eq 1 ]; then
    dsh_desktop_plugins_page_steps
    info "$(t 'Then write the server to' '再把服务写入') $OVCLI_CONF: {\"url\": \"<server-url>\", \"api_key\": \"<api-key>\"}"
  fi
  exit 1
}
NODE_BIN="$(command -v node)"
NODE_MAJOR="$("$NODE_BIN" -p 'Number(process.versions.node.split(".")[0])')"
[ "$NODE_MAJOR" -ge 18 ] || { err "Node.js 18+ required; found $("$NODE_BIN" --version)."; exit 1; }
command -v curl >/dev/null 2>&1 || warn "$(t 'curl not found; the server check is skipped and downloads will fail.' '未找到 curl:将跳过服务检查,下载也会失败。')"

select_harnesses
select_dsh_profile
validate_selected_harnesses
select_compatible_bins
refresh_available_harnesses
validate_selected_bins
if [ "$UNINSTALL" -eq 1 ]; then
  print_selection
  uninstall_agent_integrations
  exit 0
fi
resolve_source_mode
if [ "$SOURCE_MODE" = "archive" ]; then select_download_base; fi
count_steps
gather_credentials
check_server
choose_statusline
choose_cli_prefix
print_plan
warn_other_cli
warn_default_connection
# Nothing under $OV_HOME or in any harness config is written before this point.
confirm_plan
resolve_release
write_ovcli

if contains_harness claude; then
  while IFS= read -r CLAUDE_BIN; do
    [ -n "$CLAUDE_BIN" ] || continue
    install_claude
  done <<EOF
$CLAUDE_BINS
EOF
fi
if contains_harness codex; then
  while IFS= read -r CODEX_BIN; do
    [ -n "$CODEX_BIN" ] || continue
    install_codex
  done <<EOF
$CODEX_BINS
EOF
fi
if contains_harness cursor; then install_cursor; fi
if contains_harness trae; then install_trae_variant trae; fi
if contains_harness trae-cn; then install_trae_variant trae-cn; fi
if contains_harness zcode; then install_zcode; fi
if contains_harness kimicode; then install_kimicode; fi
if contains_harness codebuddy; then install_codebuddy; fi
if contains_harness opencode; then install_opencode; fi
if contains_harness pi; then install_pi; fi
if contains_harness dsh; then install_dsh; fi
if contains_harness dsh-desktop; then install_dsh_desktop; fi
if contains_harness cli; then install_cli; fi
validate_install
print_next_steps
# Earlier installers cloned the repository here; nothing uses it any more
# unless a legacy hook or statusline in settings.json still points into it.
if [ -d "$OV_HOME/openviking-repo" ] && ! grep -qF "$OV_HOME/openviking-repo" "$CC_SETTINGS" 2>/dev/null; then
  info "$(t 'No longer used by the installer; you can delete it:' '安装程序已不再使用该目录,可以手动删除:') $OV_HOME/openviking-repo"
fi