install.sh 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. #!/bin/sh
  2. # dsh one-line installer.
  3. #
  4. # curl -fsSL https://raw.githubusercontent.com/deepseek-harness/deepseek-harness/master/scripts/install.sh | sh
  5. #
  6. # It clones the harness under ~/.dsh/source (the master clone at
  7. # ~/.dsh/source/master), adds a per-install staging worktree at
  8. # ~/.dsh/source/staging-<timestamp> on branch dsh-staging/<timestamp>, checks
  9. # host dependencies (git, Node, pnpm) and offers to install a missing pnpm, runs
  10. # `pnpm install`, points the stable `~/.dsh/source/current` symlink
  11. # at that staging worktree and symlinks `dsh` onto PATH at `current/bin/dsh`,
  12. # records your API credentials in the Harness home (`~/.dsh`) dsh reads at boot,
  13. # and lets you launch the Web UI or TUI. The Web choice builds the repository
  14. # artifacts first; the TUI runs directly from TypeScript source through the
  15. # repo's own tsx. Keeping every checkout under ~/.dsh/source keeps successive
  16. # upgrades in one place instead of scattered sibling clones, and lets staging
  17. # worktrees share the master clone's object store. The PATH symlink resolves through
  18. # `current`, so an upgrade repoints one stable symlink instead of relinking PATH:
  19. # the `dsh` on PATH never moves and can never dangle.
  20. #
  21. # When run from inside an existing checkout (e.g. `sh scripts/install.sh` rather
  22. # than `curl ... | sh`) it never clones and never touches that working tree;
  23. # DSH_REF is ignored. Instead it *adopts* the checkout: `git rev-parse
  24. # --git-common-dir` resolves the repository behind it (for a linked worktree that
  25. # is the real clone, not the worktree), and a fresh staging worktree branched
  26. # from the checkout's HEAD lands in the source container beside `current`. The
  27. # container owns staging worktrees and `current`; the clone is discovered, not
  28. # owned, so an arbitrary clone (~/src/dsh) and a managed one converge on one
  29. # layout and stay upgradable. Adoption carries committed work only: the staging
  30. # worktree branches from HEAD, so uncommitted changes stay in the checkout.
  31. # Setting DSH_SOURCE to a different directory opts back into the normal
  32. # clone/worktree path.
  33. #
  34. # Adopting an arbitrary clone leaves the container not self-contained: its
  35. # staging worktrees hold an absolute gitdir pointer into that clone, so deleting
  36. # it breaks them. `git worktree list` in that clone is the record of which
  37. # worktrees depend on it.
  38. #
  39. # When run through `curl | sh` the script text arrives on stdin, so every
  40. # prompt and the final launch read the controlling terminal (/dev/tty) directly;
  41. # with no terminal the script prints the manual next steps instead.
  42. #
  43. # Overridable via environment:
  44. # DSH_REF branch or tag to clone/checkout (default: master)
  45. # DSH_REPO clone URL (default: the GitHub repo)
  46. # DSH_SOURCE source container directory (default: ~/.dsh/source)
  47. # DSH_MASTER master clone directory (default: $DSH_SOURCE/master)
  48. # DSH_CURRENT stable symlink to the active worktree (default: $DSH_SOURCE/current)
  49. # DSH_BIN_DIR directory the `dsh` symlink lands in (default: ~/.local/bin)
  50. # DSH_HOME Harness home holding the personal config (default: ~/.dsh)
  51. # FIXME(install-ts): Move the post-checkout workflow into a tested TypeScript
  52. # entrypoint; keep this POSIX shell file as the curl/source bootstrap.
  53. set -eu
  54. DSH_REF=${DSH_REF:-master}
  55. DSH_REPO=${DSH_REPO:-https://github.com/deepseek-harness/deepseek-harness.git}
  56. # DSH_SOURCE is the staging-worktree container and the default home of `current`.
  57. # DSH_MASTER names the main clone: clone mode defaults it inside DSH_SOURCE,
  58. # while adoption discovers an existing clone anywhere on disk. Remember whether
  59. # DSH_SOURCE was explicit so a different path selects clone mode.
  60. if [ -n "${DSH_SOURCE:-}" ]; then DSH_SOURCE_EXPLICIT=1; else DSH_SOURCE_EXPLICIT=0; fi
  61. DSH_SOURCE=${DSH_SOURCE:-$HOME/.dsh/source}
  62. DSH_MASTER=${DSH_MASTER:-$DSH_SOURCE/master}
  63. # The stable symlink the PATH launcher resolves through: PATH/dsh ->
  64. # current/bin/dsh -> <staging>/bin/dsh. Installs and upgrades repoint `current`;
  65. # the PATH target remains current/bin/dsh.
  66. DSH_CURRENT=${DSH_CURRENT:-$DSH_SOURCE/current}
  67. DSH_BIN_DIR=${DSH_BIN_DIR:-$HOME/.local/bin}
  68. # One UTC basic timestamp names this install's staging branch and worktree.
  69. DSH_STAMP=$(date -u +%Y%m%dT%H%M%SZ)
  70. DSH_STAGING_BRANCH=dsh-staging/$DSH_STAMP
  71. DSH_STAGING=$DSH_SOURCE/staging-$DSH_STAMP
  72. # --- path helpers ---------------------------------------------------------------
  73. # Every path comparison below runs on physical paths. Git always reports resolved
  74. # paths, so comparing one against an unresolved path disagrees whenever a symlink
  75. # sits anywhere above the checkout — a symlinked home directory is enough, and
  76. # macOS reaches every mktemp path that way through /var -> private/var. The
  77. # mismatch silently misclassifies an existing managed install as a foreign clone
  78. # and builds a second container beside the real one.
  79. # `git rev-parse --path-format=absolute` would do this, but it needs git 2.31+.
  80. #
  81. # A not-yet-created directory (the container on a fresh install) has no physical
  82. # path. Falling back here rather than at each call site keeps every caller a
  83. # plain assignment, so no site can compare against an empty path by forgetting
  84. # its own fallback.
  85. resolve_dir() { CDPATH= cd -- "$1" 2>/dev/null && pwd -P || printf '%s\n' "$1"; }
  86. # --- in-repo detection ---------------------------------------------------------
  87. # Under `curl ... | sh` the script text arrives on stdin, so $0 is the shell
  88. # name and no file path resolves; running a checked-out copy (`sh
  89. # scripts/install.sh`) makes $0 the script file. When $0 is a readable file whose
  90. # parent is a scripts/ dir inside a real dsh checkout (bin/dsh launcher present),
  91. # this is in-repo mode: never clone, never touch that working tree. An explicit
  92. # DSH_SOURCE pointing elsewhere opts back into the clone/worktree path.
  93. IN_REPO=0
  94. DSH_CHECKOUT=''
  95. if [ -f "$0" ]; then
  96. _self_dir=$(resolve_dir "$(dirname -- "$0")")
  97. if [ -n "$_self_dir" ]; then
  98. # Physical without its own resolve_dir: dirname is textual, so trimming a
  99. # resolved path leaves one. The comparison below depends on that.
  100. _repo_root=$(dirname -- "$_self_dir")
  101. if [ "$(basename -- "$_self_dir")" = scripts ] \
  102. && [ -x "$_repo_root/bin/dsh" ] && [ -f "$_repo_root/scripts/install.sh" ]; then
  103. # Compare the explicit DSH_SOURCE physically: an unresolved but equivalent
  104. # path must still count as "the caller meant this checkout".
  105. _src_resolved=$(resolve_dir "$DSH_SOURCE")
  106. if [ "$DSH_SOURCE_EXPLICIT" = 0 ] || [ "$_src_resolved" = "$_repo_root" ]; then
  107. IN_REPO=1
  108. DSH_CHECKOUT=$_repo_root
  109. fi
  110. fi
  111. fi
  112. fi
  113. # --- terminal-aware prompting --------------------------------------------------
  114. # stdin is the piped script, so read the controlling terminal for input.
  115. if { true </dev/tty; } 2>/dev/null; then
  116. HAS_TTY=1
  117. # Restore terminal echo on exit or interrupt: ask_secret disables echo between
  118. # its stty toggles, and dash (a common `sh`) does not run an EXIT trap when the
  119. # shell is killed by a signal, so the fatal signals need their own handler. A
  120. # successful run ends in exec, which replaces this process and drops the traps.
  121. trap 'stty echo </dev/tty 2>/dev/null || true' EXIT
  122. trap 'stty echo </dev/tty 2>/dev/null || true; exit 130' INT TERM HUP
  123. else
  124. HAS_TTY=0
  125. fi
  126. # Colour only when writing to a terminal.
  127. if [ -t 1 ]; then
  128. B=$(printf '\033[1m'); DIM=$(printf '\033[2m'); RED=$(printf '\033[31m')
  129. GRN=$(printf '\033[32m'); YEL=$(printf '\033[33m'); RST=$(printf '\033[0m')
  130. else
  131. B=''; DIM=''; RED=''; GRN=''; YEL=''; RST=''
  132. fi
  133. info() { printf '%s==>%s %s\n' "$GRN" "$RST" "$1"; }
  134. step() { printf '\n%s==>%s %s%s%s\n' "$GRN" "$RST" "$B" "$1" "$RST"; }
  135. warn() { printf '%s warn%s %s\n' "$YEL" "$RST" "$1" >&2; }
  136. die() { printf '%serror%s %s\n' "$RED" "$RST" "$1" >&2; exit 1; }
  137. # ask PROMPT [DEFAULT] -> answer on stdout (plain-text line).
  138. ask() {
  139. [ "$HAS_TTY" = 1 ] || die "no terminal available for input; re-run in an interactive shell"
  140. printf '%s%s%s ' "$B" "$1" "$RST" >/dev/tty
  141. IFS= read -r _ans </dev/tty || _ans=''
  142. [ -n "$_ans" ] || _ans=${2:-}
  143. printf '%s' "$_ans"
  144. }
  145. # ask_secret PROMPT -> answer on stdout, with terminal echo suppressed.
  146. ask_secret() {
  147. [ "$HAS_TTY" = 1 ] || die "no terminal available for input; re-run in an interactive shell"
  148. printf '%s%s%s ' "$B" "$1" "$RST" >/dev/tty
  149. stty -echo </dev/tty 2>/dev/null || true
  150. IFS= read -r _sec </dev/tty || _sec=''
  151. stty echo </dev/tty 2>/dev/null || true
  152. printf '\n' >/dev/tty
  153. printf '%s' "$_sec"
  154. }
  155. # confirm PROMPT [Y] -> exit 0 on yes. Default is no unless second arg is "Y".
  156. confirm() {
  157. _def=${2:-N}
  158. if [ "$HAS_TTY" != 1 ]; then
  159. [ "$_def" = Y ] # non-interactive: take the default
  160. return
  161. fi
  162. if [ "$_def" = Y ]; then _hint='[Y/n]'; else _hint='[y/N]'; fi
  163. printf '%s%s%s %s ' "$B" "$1" "$RST" "$_hint" >/dev/tty
  164. IFS= read -r _r </dev/tty || _r=''
  165. [ -n "$_r" ] || _r=$_def
  166. case "$_r" in [yY]|[yY][eE][sS]) return 0 ;; *) return 1 ;; esac
  167. }
  168. printf '%s\n' "${B}DeepSeek Harness — dsh installer${RST}"
  169. if [ "$IN_REPO" = 1 ]; then
  170. printf '%scheckout %s%s\n' "$DIM" "$DSH_CHECKOUT" "$RST"
  171. else
  172. printf '%smaster %s @ %s%s\n' "$DIM" "$DSH_MASTER" "$DSH_REF" "$RST"
  173. printf '%sstaging %s%s\n' "$DIM" "$DSH_STAGING" "$RST"
  174. printf '%scurrent %s%s\n' "$DIM" "$DSH_CURRENT" "$RST"
  175. fi
  176. # --- 1. dependency check -------------------------------------------------------
  177. step "Checking dependencies"
  178. command -v git >/dev/null 2>&1 || die "git is required but not found. Install git, then re-run."
  179. info "git ... ok"
  180. # Node ^22.19.0 || >=24.0.0 (see the root package.json "engines" field).
  181. node_ok() {
  182. command -v node >/dev/null 2>&1 || return 1
  183. _v=$(node -v 2>/dev/null) || return 1
  184. _v=${_v#v}
  185. _major=${_v%%.*}
  186. _rest=${_v#*.}
  187. _minor=${_rest%%.*}
  188. case "$_major" in ''|*[!0-9]*) return 1 ;; esac
  189. case "$_minor" in ''|*[!0-9]*) _minor=0 ;; esac
  190. [ "$_major" -ge 24 ] && return 0
  191. [ "$_major" -eq 22 ] && [ "$_minor" -ge 19 ] && return 0
  192. return 1
  193. }
  194. if node_ok; then
  195. info "node $(node -v) ... ok"
  196. else
  197. if command -v node >/dev/null 2>&1; then
  198. die "Node $(node -v) is unsupported. dsh needs ^22.19.0 || >=24.0.0 — upgrade Node, then re-run."
  199. fi
  200. die "Node is required but not found. Install Node ^22.19.0 || >=24, then re-run."
  201. fi
  202. # pnpm is the only dependency we offer to install for you.
  203. if command -v pnpm >/dev/null 2>&1; then
  204. info "pnpm $(pnpm --version) ... ok"
  205. else
  206. warn "pnpm is not installed."
  207. if confirm "Install pnpm now?" Y; then
  208. if command -v corepack >/dev/null 2>&1 && corepack enable pnpm >/dev/null 2>&1; then
  209. info "enabled pnpm via corepack"
  210. elif command -v npm >/dev/null 2>&1 && npm install -g pnpm >/dev/null 2>&1; then
  211. info "installed pnpm via npm"
  212. else
  213. die "could not install pnpm automatically. Install it (https://pnpm.io/installation), then re-run."
  214. fi
  215. command -v pnpm >/dev/null 2>&1 || die "pnpm still not on PATH after install. Open a new shell, then re-run."
  216. else
  217. die "pnpm is required. Install it (https://pnpm.io/installation), then re-run."
  218. fi
  219. fi
  220. # --- 2. resolve the repository and lay out the staging worktree ---------------
  221. # The source container owns staging worktrees and `current`; the repository is
  222. # *discovered*, not owned. A curl install discovers it by cloning to $DSH_MASTER;
  223. # in-repo adoption discovers it from the checkout. Both then run one shared
  224. # worktree/exclude/lock path, so an arbitrary clone and a managed install
  225. # converge on the same layout.
  226. #
  227. # REPO_COMMON is the shared git directory every worktree of the repository
  228. # points at; REPO_ROOT is the working tree that owns it (the master clone).
  229. REPO_COMMON=''
  230. REPO_ROOT=''
  231. if [ "$IN_REPO" = 1 ]; then
  232. step "Using existing checkout at $DSH_CHECKOUT"
  233. info "running from inside the repo — never cloning, and DSH_REF is ignored"
  234. # Resolve the repository behind the checkout. --git-common-dir returns the
  235. # SHARED git dir, so a linked worktree resolves to the real clone rather than
  236. # itself; it is relative for a plain clone, so anchor it before resolving.
  237. # Require the resolved git dir to exist: resolve_dir echoes its argument back
  238. # for a missing path, so test the directory rather than the returned string.
  239. if _common=$(git -C "$DSH_CHECKOUT" rev-parse --git-common-dir 2>/dev/null) && [ -n "$_common" ]; then
  240. case "$_common" in /*) ;; *) _common=$DSH_CHECKOUT/$_common ;; esac
  241. [ -d "$_common" ] && REPO_COMMON=$(resolve_dir "$_common")
  242. fi
  243. [ -n "$REPO_COMMON" ] || die "$DSH_CHECKOUT is not a git repository — cannot adopt it."
  244. REPO_ROOT=$(dirname -- "$REPO_COMMON")
  245. # Reuse the container when the repository already lives inside it (the normal
  246. # managed install re-running its own script); otherwise treat that clone as
  247. # its own master and keep worktrees in the default container.
  248. _src_resolved=$(resolve_dir "$DSH_SOURCE")
  249. case "$REPO_ROOT/" in
  250. "$_src_resolved"/*) info "repository $REPO_ROOT is already inside $DSH_SOURCE" ;;
  251. *) info "adopting clone $REPO_ROOT as its own master" ;;
  252. esac
  253. DSH_MASTER=$REPO_ROOT
  254. else
  255. step "Fetching source into $DSH_MASTER"
  256. if [ -d "$DSH_MASTER/.git" ]; then
  257. info "existing master clone found — updating"
  258. git -C "$DSH_MASTER" fetch origin "$DSH_REF"
  259. # Reset the master checkout to the freshly fetched tip. FETCH_HEAD (not
  260. # origin/<ref>) so this resolves for a tag as well as a branch, and -B makes
  261. # the re-run idempotent whether or not DSH_REF changed since the last install.
  262. git -C "$DSH_MASTER" checkout -q -B "$DSH_REF" FETCH_HEAD
  263. else
  264. mkdir -p "$DSH_SOURCE"
  265. git clone --branch "$DSH_REF" "$DSH_REPO" "$DSH_MASTER"
  266. fi
  267. # Physical on both branches: REPO_ROOT is compared against resolved paths
  268. # below, and REPO_COMMON stays symmetric with it so neither can be read as
  269. # carrying a different kind of path.
  270. REPO_COMMON=$(resolve_dir "$DSH_MASTER/.git")
  271. REPO_ROOT=$(resolve_dir "$DSH_MASTER")
  272. fi
  273. step "Adding staging worktree at $DSH_STAGING"
  274. [ -e "$DSH_STAGING" ] && die "staging path $DSH_STAGING already exists — remove it or set DSH_SOURCE elsewhere, then re-run."
  275. mkdir -p "$DSH_SOURCE"
  276. # The staging worktree owns the branch dsh runs from; the repository stays as
  277. # the fetch/upgrade base and is never a launcher target. A clone install
  278. # branches from the ref it just fetched; adoption branches from the checkout's
  279. # HEAD so the contributor's committed work is what runs.
  280. if [ "$IN_REPO" = 1 ]; then
  281. git -C "$DSH_CHECKOUT" worktree add -b "$DSH_STAGING_BRANCH" "$DSH_STAGING" HEAD
  282. else
  283. git -C "$DSH_MASTER" worktree add -b "$DSH_STAGING_BRANCH" "$DSH_STAGING" FETCH_HEAD 2>/dev/null \
  284. || git -C "$DSH_MASTER" worktree add -b "$DSH_STAGING_BRANCH" "$DSH_STAGING" HEAD
  285. fi
  286. # Exclude the per-worktree merge lock in the shared git dir's info/exclude,
  287. # which every linked worktree inherits.
  288. _exclude="$REPO_COMMON/info/exclude"
  289. if [ -f "$_exclude" ] && ! grep -qxF '.agents/merge.lock' "$_exclude" 2>/dev/null; then
  290. printf '.agents/merge.lock\n' >>"$_exclude"
  291. fi
  292. mkdir -p "$DSH_STAGING/.agents"
  293. : >"$DSH_STAGING/.agents/merge.lock"
  294. # --- 3. install dependencies (no build; the launcher runs from source) --------
  295. step "Installing dependencies with pnpm (this can take a while)"
  296. ( cd "$DSH_STAGING" && pnpm install )
  297. [ -x "$DSH_STAGING/bin/dsh" ] || die "launcher $DSH_STAGING/bin/dsh missing after install — is DSH_REF a branch that ships apps/cli?"
  298. # --- 4. put `dsh` on PATH ------------------------------------------------------
  299. # Every install goes through a stable `current` symlink so an upgrade repoints
  300. # one symlink (current -> new worktree) and the PATH launcher never moves:
  301. # PATH/dsh -> current/bin/dsh -> <staging>/bin/dsh.
  302. step "Linking dsh into $DSH_BIN_DIR"
  303. mkdir -p "$DSH_BIN_DIR"
  304. # The launcher must resolve to a staging worktree, never to the repository
  305. # itself: an upgrade repoints `current`, so aliasing it onto the master clone
  306. # would make every upgrade rewrite the fetch/upgrade base. Compare physical
  307. # paths — a symlinked or unresolved path would slip past a string compare.
  308. _staging_resolved=$(resolve_dir "$DSH_STAGING")
  309. [ "$_staging_resolved" = "$REPO_ROOT" ] \
  310. && die "refusing to point $DSH_CURRENT at the repository $REPO_ROOT — the launcher must resolve to a staging worktree."
  311. # Point `current` at this staging worktree with `ln -sfn`: -f replaces an
  312. # existing `current` (re-run or upgrade) and -n stops `ln` from dereferencing
  313. # an existing symlink-to-directory and dropping the new link *inside* the old
  314. # worktree. `mv` is unusable here — BSD/macOS `mv` follows the existing dir
  315. # symlink the same way. The swap is one unlink+symlink pair on a local fs; the
  316. # installer holds no other process racing this path.
  317. ln -sfn "$DSH_STAGING" "$DSH_CURRENT"
  318. info "pointed $DSH_CURRENT -> $DSH_STAGING"
  319. DSH_LAUNCH_TARGET=$DSH_CURRENT/bin/dsh
  320. ln -sf "$DSH_LAUNCH_TARGET" "$DSH_BIN_DIR/dsh"
  321. info "linked $DSH_BIN_DIR/dsh -> $DSH_LAUNCH_TARGET"
  322. case ":$PATH:" in
  323. *":$DSH_BIN_DIR:"*) ON_PATH=1 ;;
  324. *) ON_PATH=0 ;;
  325. esac
  326. if [ "$ON_PATH" = 0 ]; then
  327. warn "$DSH_BIN_DIR is not on your PATH."
  328. _line="export PATH=\"$DSH_BIN_DIR:\$PATH\""
  329. _rc=''
  330. _sh=${SHELL:-} # SHELL may be unset; word-removal on an unset var trips set -u under dash.
  331. case "${_sh##*/}" in
  332. zsh) _rc="$HOME/.zshrc" ;;
  333. bash) _rc="$HOME/.bashrc" ;;
  334. esac
  335. if [ -n "$_rc" ] && [ -f "$_rc" ] && grep -qF "$_line" "$_rc" 2>/dev/null; then
  336. info "$_rc already exports $DSH_BIN_DIR — open a new shell to pick it up"
  337. elif [ -n "$_rc" ] && confirm "Add it to $_rc?" Y; then
  338. printf '\n# Added by the dsh installer\n%s\n' "$_line" >>"$_rc"
  339. info "updated $_rc — run 'source $_rc' or open a new shell to pick it up"
  340. else
  341. warn "add this line to your shell profile yourself:"
  342. printf ' %s\n' "$_line"
  343. fi
  344. fi
  345. # --- 5. credentials ------------------------------------------------------------
  346. # Mirror app-boot's resolveDshHome precedence ($DSH_HOME, else ~/.dsh) so creds land where dsh reads them.
  347. if [ -n "${DSH_HOME:-}" ]; then
  348. CONF="$DSH_HOME"
  349. else
  350. CONF="$HOME/.dsh"
  351. fi
  352. ENV_FILE="$CONF/.env"
  353. step "Configuring credentials"
  354. if [ -f "$ENV_FILE" ] && grep -q '^DEEPSEEK_API_KEY=' "$ENV_FILE" 2>/dev/null; then
  355. info "DEEPSEEK_API_KEY already set in $ENV_FILE"
  356. if ! confirm "Replace it?" N; then
  357. SKIP_CREDS=1
  358. fi
  359. fi
  360. if [ "${SKIP_CREDS:-0}" != 1 ]; then
  361. if [ "$HAS_TTY" = 1 ]; then
  362. API_KEY=$(ask_secret "DeepSeek API key (input hidden):")
  363. if [ -z "$API_KEY" ]; then
  364. warn "no key entered — skipping. Set DEEPSEEK_API_KEY in $ENV_FILE before using dsh."
  365. else
  366. BASE_URL=$(ask "DeepSeek base URL (optional, Enter to skip):")
  367. mkdir -p "$CONF"
  368. # The installer owns exactly the two DEEPSEEK_* lines; any other lines the
  369. # user keeps in this .env are preserved. The rewrite happens in a subshell
  370. # so umask 077 (which closes the create-time permission race) does not leak
  371. # into the exec'd dsh, and lands atomically via a same-dir temp + mv.
  372. _tmp="$ENV_FILE.dsh.$$"
  373. (
  374. umask 077
  375. if [ -f "$ENV_FILE" ]; then
  376. grep -v -e '^DEEPSEEK_API_KEY=' -e '^DEEPSEEK_BASE_URL=' "$ENV_FILE" >"$_tmp" || true
  377. else
  378. : >"$_tmp"
  379. fi
  380. printf 'DEEPSEEK_API_KEY=%s\n' "$API_KEY" >>"$_tmp"
  381. if [ -n "$BASE_URL" ]; then printf 'DEEPSEEK_BASE_URL=%s\n' "$BASE_URL" >>"$_tmp"; fi
  382. )
  383. mv "$_tmp" "$ENV_FILE"
  384. chmod 600 "$ENV_FILE" 2>/dev/null || true
  385. info "wrote $ENV_FILE"
  386. fi
  387. else
  388. warn "no terminal for credential input — set DEEPSEEK_API_KEY in $ENV_FILE before using dsh."
  389. fi
  390. fi
  391. # --- 6. choose and launch an interface -----------------------------------------
  392. step "Done"
  393. if [ "$HAS_TTY" = 1 ]; then
  394. printf ' 1) Web UI (recommended)\n'
  395. printf ' 2) TUI\n'
  396. while :; do
  397. LAUNCH_INTERFACE=$(ask "Choose an interface [1/2]:" 1)
  398. case "$LAUNCH_INTERFACE" in
  399. 1|web|Web|WEB)
  400. step "Building DeepSeek Harness for Web UI"
  401. ( cd "$DSH_STAGING" && pnpm run build )
  402. info "launching Web UI — run 'dsh web' anytime to start again"
  403. exec "$DSH_BIN_DIR/dsh" web </dev/tty
  404. ;;
  405. 2|tui|Tui|TUI)
  406. info "launching TUI — run 'dsh' anytime to start again"
  407. exec "$DSH_BIN_DIR/dsh" </dev/tty
  408. ;;
  409. *)
  410. warn "choose 1 for Web UI or 2 for TUI"
  411. ;;
  412. esac
  413. done
  414. else
  415. info "install complete. Build and start the Web UI with:"
  416. printf ' (cd %s && pnpm run build)\n' "$DSH_STAGING"
  417. printf ' %s web\n' "$DSH_BIN_DIR/dsh"
  418. info "or start the TUI with:"
  419. printf ' %s\n' "$DSH_BIN_DIR/dsh"
  420. fi