diff options
| -rw-r--r-- | pkgs/claude-remote.nix | 120 |
1 files changed, 87 insertions, 33 deletions
diff --git a/pkgs/claude-remote.nix b/pkgs/claude-remote.nix index 9429413..501edcd 100644 --- a/pkgs/claude-remote.nix +++ b/pkgs/claude-remote.nix @@ -57,15 +57,19 @@ let inspect NAME A remote, in more detail: its unit, directory, trust, claude.ai session, and its terminal's last lines. - start NAME [--interactive] - Start a remote; a dynamic one lasts, across reboots, until stop. - --interactive: a new remote, as an interactive session (which it - stays), with a session of its own, resumed at each start (more - memory and CPU; its session is archived on claude.ai while it's - stopped). Not for one that has had sessions, as a Remote Control - server: they wouldn't go on. + start NAME [--type=remote|interactive] + restart NAME [--type=remote|interactive] + Start a remote (or restart it); a dynamic one lasts, across + reboots, until stop. --type: a Remote Control server (remote), + or an interactive session with Remote Control (interactive; + --interactive for short), with more memory and CPU, and its + session archived on claude.ai while it's stopped. A new one is + made so (default: remote); an existing one of the other type + changes (stopped, if it runs): to interactive, it resumes its + latest session; to remote, the session it had as one. Without + --type, a remote starts as the type it is. Not for a static + one, whose type is the system's configuration's. stop NAME - restart NAME attach [NAME] Its terminal (tmux; default: main; C-b d to leave). log NAME [JOURNALCTL-OPTION...] @@ -136,7 +140,7 @@ let -e "s/^([A-Z][A-Za-z ]*:)$/$C_HEAD\1$C_OFF/" \ -e "s/^( )($c)($x|$)/\1$C_CMD\2$C_OFF\3/" \ -e ":a; s/(claude-remote |, |^usage: claude-remote \[)($c|COMMAND)($x|$)/\1$C_CMD\2$C_OFF\3/; ta" \ - -e ":b; s/(^|$x)(--stop|--interactive|-f|--force|-h|--help|JOURNALCTL-OPTION)($x|$)/\1$C_OPT\2$C_OFF\3/; tb" \ + -e ":b; s/(^|$x)(--stop|--interactive|--type|-f|--force|-h|--help|JOURNALCTL-OPTION)($x|$)/\1$C_OPT\2$C_OFF\3/; tb" \ -e ":c; s/(^|$x)(NAME|OLD|NEW)($x|$)/\1$C_NAME\2$C_OFF\3/; tc" } die() { @@ -326,6 +330,48 @@ let read -r -p "$1 [y/N] " a case $a in y | Y | yes) ;; *) die "not done" ;; esac } + # NAME, as a remote of TYPE (remote, interactive) from now on: stopped, + # if it runs, and left so. To an interactive session, it resumes its + # latest session (its latest transcript), or else starts a new one; to + # a Remote Control server, its session id goes (kept, as a dot file, + # which names() passes over), and it resumes the session its link + # points to. (On claude.ai, the session of the other type stays, or + # gets archived, as Remote Control has it.) Not for a static one, + # whose type is the system's configuration's. + change_type() { + local name=$1 type=$2 t id + test "$(id -u)" = 0 || die "--type: as root" + if is_static "$name"; then + die "$name is static, as $(type_of "$name"): its type is the system's configuration's" + fi + if grep -qs "/$(unit "$name")" /proc/self/cgroup; then + die "this runs in $name: change its type from elsewhere (a root shell, another remote)" + fi + # (global, for take_back, which runs on exit) + doing="$name, as $type" undos=() finished= backup= + trap take_back EXIT + case $(active "$name") in + inactive | failed | "") ;; + *) + undoable systemctl start "$(unit "$name")" + systemctl stop "$(unit "$name")" + esac + if test "$type" = interactive; then + t=$(ls -t "$(project "$name")"/*.jsonl 2>/dev/null | head -n 1 || :) + if test -n "$t"; then + id=$(basename "$t" .jsonl) + else + id=$(cat /proc/sys/kernel/random/uuid) + fi + undoable rm -f "$sessions/$name" + printf '%s\n' "$id" > "$sessions/$name" + else + undoable mv -f "$sessions/.$name.old" "$sessions/$name" + mv -f "$sessions/$name" "$sessions/.$name.old" + fi + finished=1 + trap - EXIT + } # Remote NAME's claude.ai session forgotten (its link to it in its # project moved aside, to bridge-pointer.json.old), so that it starts # with a new one: stopped, if it runs, and started again. @@ -624,39 +670,38 @@ let fi hint_attach "$n" ;; - start) - interactive= + start | restart) + type= args=() for a; do case $a in - --interactive) interactive=1 ;; - -*) die "start: $a: no such option (claude-remote -h)" ;; + --interactive) type=interactive ;; + --type=remote | --type=interactive) type=''${a#--type=} ;; + --type=*) die "$cmd: $a: the type is remote or interactive" ;; + -*) die "$cmd: $a: no such option (claude-remote -h)" ;; *) args+=("$a") esac done - test ''${#args[@]} = 1 || die "start NAME [--interactive] (claude-remote -h)" + test ''${#args[@]} = 1 || die "$cmd NAME [--type=remote|interactive] (claude-remote -h)" n=''${args[0]} name_arg "$n" # before the login, the unit would be skipped, and not come back test -e "$home/.claude/.credentials.json" || die "there's no login yet: runuser -l claude -c 'claude auth login' (then the remotes start)" - # an interactive session, from now on: a session id of its own - # (the kind sticks, so that start, stop and the rest find it) - if test -n "$interactive" && ! is_interactive "$n"; then - test "$(id -u)" = 0 || die "start --interactive: as root" - case $(active "$n") in - inactive | failed | "") ;; - *) die "$n runs as a Remote Control server: claude-remote stop $n first" ;; - esac - test ! -e "$state/$n" || die "$n is a dynamic Remote Control server ($state/$n): claude-remote stop $n first" - # only a new one: an interactive session wouldn't go on with the - # sessions it had as a Remote Control server, but start anew - if compgen -G "$(project "$n")/*.jsonl" >/dev/null; then - die "start --interactive is for a new remote: $n has had sessions already, as a Remote Control server, which an interactive session wouldn't go on with" + # of the type asked for: a new one, made so; an existing one of the + # other type, changed (stopped, if it runs); without --type, of the + # type it is (a new one, remote) + if test -n "$type" && test "$(type_of "$n")" != "$type"; then + if test -d "$remotes/$n" || test -d "$(project "$n")"; then + change_type "$n" "$type" + cmd=start + else + ! is_static "$n" || die "$n is static, as $(type_of "$n"): its type is the system's configuration's" + test "$(id -u)" = 0 || die "--type=interactive: as root" + cat /proc/sys/kernel/random/uuid > "$sessions/$n" fi - cat /proc/sys/kernel/random/uuid > "$sessions/$n" fi - systemctl start "$(unit "$n")" + systemctl "$cmd" "$(unit "$n")" if ! is_static "$n"; then echo "$n is dynamic: it lasts, across reboots, until claude-remote stop $n." fi @@ -665,9 +710,9 @@ let fi hint_attach "$n" ;; - stop | restart) + stop) name_arg "''${1-}" - systemctl "$cmd" "$(unit "$1")" + systemctl stop "$(unit "$1")" ;; attach) n=''${1:-main} @@ -705,8 +750,17 @@ let esac elif [ "''${COMP_WORDS[1]}" = reset ] || [ "''${COMP_WORDS[1]}" = remove ]; then COMPREPLY=($(compgen -W "-f --force" -- "$cur")) - elif [ "''${COMP_WORDS[1]}" = start ]; then - COMPREPLY=($(compgen -W "--interactive" -- "$cur")) + elif [ "''${COMP_WORDS[1]}" = start ] || [ "''${COMP_WORDS[1]}" = restart ]; then + # --type=TYPE (which bash splits into --type, =, TYPE) + local prev=''${COMP_WORDS[COMP_CWORD-1]} + if [ "$cur" = = ] && [ "$prev" = --type ]; then + COMPREPLY=(remote interactive) + elif [ "$prev" = = ] && [ "''${COMP_WORDS[COMP_CWORD-2]}" = --type ]; then + COMPREPLY=($(compgen -W "remote interactive" -- "$cur")) + else + COMPREPLY=($(compgen -W "--type= --interactive" -- "$cur")) + [ "''${COMPREPLY[*]}" != --type= ] || compopt -o nospace + fi elif [ "''${COMP_WORDS[1]}" = rename ]; then COMPREPLY=($(compgen -W "--stop" -- "$cur")) fi |
