@tian.zuo/pi-rex 0.3.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +10 -0
- package/README.md +30 -7
- package/index.ts +2 -8
- package/lib/hook.ts +257 -0
- package/lib/resume.ts +19 -4
- package/lib/settings.ts +17 -0
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,15 @@
|
|
|
1
1
|
# @tian.zuo/pi-rex
|
|
2
2
|
|
|
3
|
+
## 0.4.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- [#211](https://github.com/TianZuo555/pi-extensions/pull/211) [`6d55a59`](https://github.com/TianZuo555/pi-extensions/commit/6d55a595c99d06ad6a2bc9bd2ae2742d6df46083) Thanks [@TianZuo555](https://github.com/TianZuo555)! - Install the shell hook that resumes pi after a Rex restart. On the first pi session inside a Rex pane, the extension writes the hook for `fish`, `zsh`, `bash`, or PowerShell, and changes only text it wrote itself. The new `rex.resume` setting turns the record and the hook off.
|
|
8
|
+
|
|
9
|
+
### Patch Changes
|
|
10
|
+
|
|
11
|
+
- [#212](https://github.com/TianZuo555/pi-extensions/pull/212) [`7b88eb3`](https://github.com/TianZuo555/pi-extensions/commit/7b88eb36e34534c79adf587a4623c2233d9dbd04) Thanks [@TianZuo555](https://github.com/TianZuo555)! - Fix three problems in the resume hook install. The bash block now uses POSIX syntax, so a `/bin/sh` that reads `.profile` can parse it. The Windows PowerShell 5.1 profile goes to `Documents\WindowsPowerShell`. Turning `rex.resume` off removes the pane's record, so the hook does not reopen the session.
|
|
12
|
+
|
|
3
13
|
## 0.3.0
|
|
4
14
|
|
|
5
15
|
### Minor Changes
|
package/README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
Release notes: [changelog](https://github.com/TianZuo555/pi-extensions/blob/main/packages/pi-rex/CHANGELOG.md) · [GitHub releases](https://github.com/TianZuo555/pi-extensions/releases)
|
|
4
4
|
|
|
5
|
-
The [Rex](https://www.superlogical.com/rex/docs/automate/agent-skill) session-start hook for the [pi coding agent](https://pi.dev). The extension also names the Rex session after the project folder or worktree, names the tab from the first prompt, and
|
|
5
|
+
The [Rex](https://www.superlogical.com/rex/docs/automate/agent-skill) session-start hook for the [pi coding agent](https://pi.dev). The extension also names the Rex session after the project folder or worktree, names the tab from the first prompt, and resumes pi in each pane after a Rex restart. You install nothing else: the extension also writes the shell hook that resumes pi.
|
|
6
6
|
|
|
7
7
|
```bash
|
|
8
8
|
pi install npm:@tian.zuo/pi-rex
|
|
@@ -53,7 +53,7 @@ Because the extension names the session, it removes the Rex naming paragraph fro
|
|
|
53
53
|
|
|
54
54
|
## Resume after a Rex restart
|
|
55
55
|
|
|
56
|
-
Rex restores sessions, tabs, panes, and scrollback after a restart. Each pane keeps its block ID, but Rex starts a new shell in the pane, not pi. The extension records the pi session of each pane,
|
|
56
|
+
Rex restores sessions, tabs, panes, and scrollback after a restart. Each pane keeps its block ID, but Rex starts a new shell in the pane, not pi. A dead process cannot start itself, so the work needs two parts. The extension records the pi session of each pane, and it writes a shell hook. The hook starts pi again in the restored pane.
|
|
57
57
|
|
|
58
58
|
When `REX_BLOCK` is set in TUI mode, the extension writes `~/.pi/agent/rex-resume/<block id>` (below `PI_CODING_AGENT_DIR` when it is set). It writes the record when a session starts and after each prompt, because pi creates the session file only after the first message. The record holds the working directory on line 1, then the command, one argument per line:
|
|
59
59
|
|
|
@@ -66,7 +66,24 @@ pi
|
|
|
66
66
|
|
|
67
67
|
The record stays when pi stops because of SIGHUP or SIGTERM, or because the terminal is gone. A Rex restart does one of these. The record stays for `/reload`, `/resume`, and `/fork`, and the next session writes its own record. `/new` removes the record until the new session has a file, so a restart right after `/new` does not reopen the previous session. The record is removed when you quit pi (Ctrl+D, Ctrl+C twice, `/quit`). Print, JSON, and RPC runs write no record.
|
|
68
68
|
|
|
69
|
-
The
|
|
69
|
+
### The shell hook
|
|
70
|
+
|
|
71
|
+
On the first pi session in a Rex pane, the extension writes the hook for the pane's shell and shows one notice. It reads the shell from `SHELL`. A Windows pane has no `SHELL`, so there it reads `PSModulePath`.
|
|
72
|
+
|
|
73
|
+
| Shell | File | How the hook is stored |
|
|
74
|
+
| ---------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------- |
|
|
75
|
+
| fish | `~/.config/fish/conf.d/rex-resume.fish`, below `XDG_CONFIG_HOME` | The whole file. It starts with the marker line `# Generated by @tian.zuo/pi-rex.` |
|
|
76
|
+
| zsh | `.zshrc` in `ZDOTDIR` or the home folder | A block between the lines `# >>> tian.zuo/pi-rex >>>` and `# <<< tian.zuo/pi-rex <<<` |
|
|
77
|
+
| bash | The startup file a login shell reads first: `.bash_profile`, `.bash_login`, or `.profile`; `.profile` when none exists | The same marked block |
|
|
78
|
+
| PowerShell | `profile.ps1` in `Documents\PowerShell` for PowerShell 7 and `Documents\WindowsPowerShell` for Windows PowerShell 5.1, both on Windows; the `powershell` folder of the config home on macOS and Linux | The same marked block |
|
|
79
|
+
|
|
80
|
+
The extension never creates `.bash_profile`, because that file would hide an existing `.profile`. It changes only its own text: a fish file without the marker, or a startup file without the block, stays as it is.
|
|
81
|
+
|
|
82
|
+
The bash block uses POSIX syntax only. A desktop session can run `.profile` through `/bin/sh`, and that shell must parse the file even when it never starts pi.
|
|
83
|
+
|
|
84
|
+
The hook runs when a new shell starts in a restored pane. A new pane has a new block ID, so it has no record and the hook does nothing. The hook removes the record before it starts pi, and pi writes a new one. The hook always runs `pi --session <file>`: it does not restore the flags of the first launch, so a pane that ran `pi -e ./packages/pi-rex` resumes with plain `pi`.
|
|
85
|
+
|
|
86
|
+
This is the fish hook the extension writes. The `zsh`, `bash`, and PowerShell hooks do the same work in the syntax of their shell.
|
|
70
87
|
|
|
71
88
|
```fish
|
|
72
89
|
if status is-interactive; and set -q REX_BLOCK
|
|
@@ -86,7 +103,9 @@ if status is-interactive; and set -q REX_BLOCK
|
|
|
86
103
|
end
|
|
87
104
|
```
|
|
88
105
|
|
|
89
|
-
The hook runs
|
|
106
|
+
The hook runs before the first prompt, after the rest of the config is loaded. For fish it waits at that prompt; for zsh and bash it runs at the end of the startup file.
|
|
107
|
+
|
|
108
|
+
A bash hook runs only in an interactive shell, so a script that sources `.profile` does not start pi.
|
|
90
109
|
|
|
91
110
|
## Settings
|
|
92
111
|
|
|
@@ -95,18 +114,20 @@ The extension reads the `rex` section of the pi settings file. Add the section t
|
|
|
95
114
|
| Key | Type | Default | Description |
|
|
96
115
|
| -------------- | ------- | ------- | ------------------------------------------------------------------------------------------------------------------------------------------ |
|
|
97
116
|
| `rex.autoName` | boolean | `true` | Name the Rex session after the folder or worktree, and the tab from the first prompt. `false` keeps the Rex names and the Rex naming text. |
|
|
117
|
+
| `rex.resume` | boolean | `true` | Record each pane's pi session, and write the fish hook that resumes pi after a Rex restart. `false` writes no record and no hook file. |
|
|
98
118
|
|
|
99
119
|
Example:
|
|
100
120
|
|
|
101
121
|
```json
|
|
102
122
|
{
|
|
103
123
|
"rex": {
|
|
104
|
-
"autoName": false
|
|
124
|
+
"autoName": false,
|
|
125
|
+
"resume": false
|
|
105
126
|
}
|
|
106
127
|
}
|
|
107
128
|
```
|
|
108
129
|
|
|
109
|
-
Any value other than `false` keeps
|
|
130
|
+
Any value other than `false` keeps the feature on.
|
|
110
131
|
|
|
111
132
|
## Known limits
|
|
112
133
|
|
|
@@ -115,7 +136,9 @@ Any value other than `false` keeps automatic naming on.
|
|
|
115
136
|
- The model reply must be one tab label. A prose reply leaves the old tab name in place; the session is still renamed.
|
|
116
137
|
- The naming call uses the current session model. A reasoning model can spend part of the token budget before it writes the label.
|
|
117
138
|
- The `<rex>` removal reads one string from the Rex output: the paragraph that holds `rex session rename`. A change to that wording stops the removal.
|
|
118
|
-
- Resume
|
|
139
|
+
- Resume writes a hook for `fish`, `zsh`, `bash`, and PowerShell. A pane that runs `sh`, `cmd.exe`, or another shell starts no pi after a restart. The records still exist, so a shell hook you write by hand can use them.
|
|
140
|
+
- The fish, zsh, and bash hooks ran in real shells. The PowerShell hook did not: it is written from the PowerShell syntax alone. A Windows pane needs `pi` on `PATH`, which is the `.cmd` shim that pi installs.
|
|
141
|
+
- The extension writes the hook only in the TUI, inside a Rex pane. A machine that never starts pi in a Rex pane gets no hook file.
|
|
119
142
|
- The hook starts `pi --session <file>`, so it does not restore the flags of the first launch. A pane that ran `pi -e ./packages/pi-rex` resumes without the local extension.
|
|
120
143
|
- A second interactive fish in the same pane, started while pi runs, also reads the record. It can start a second pi on the same session and remove the record; the next prompt in the first pi writes the record again.
|
|
121
144
|
- When you close a pane while pi runs in it, pi gets SIGHUP and its record stays. The file is small, and Rex never reuses a block ID.
|
package/index.ts
CHANGED
|
@@ -20,17 +20,11 @@ import { nameSession, REX_TIMEOUT_MS, type ExecResult } from "./lib/naming.ts";
|
|
|
20
20
|
import { AUTO_NAME_INSTRUCTION, REX_SECTION } from "./lib/prompt.ts";
|
|
21
21
|
import { registerResume } from "./lib/resume.ts";
|
|
22
22
|
import { stripSessionNaming } from "./lib/rex-output.ts";
|
|
23
|
+
import { rexSetting } from "./lib/settings.ts";
|
|
23
24
|
|
|
24
25
|
/** Timeout for the naming model call. */
|
|
25
26
|
const NAMING_TIMEOUT_MS = 15000;
|
|
26
27
|
|
|
27
|
-
/** Name one session and tab. `true` unless the user turns it off. */
|
|
28
|
-
function autoNameEnabled(pi: ExtensionAPI): boolean {
|
|
29
|
-
const rex = (pi.getSettings() as { rex?: unknown }).rex;
|
|
30
|
-
if (typeof rex !== "object" || rex === null) return true;
|
|
31
|
-
return (rex as { autoName?: unknown }).autoName !== false;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
28
|
/**
|
|
35
29
|
* Ask the session model for one label. Returns an empty string when the model
|
|
36
30
|
* is missing or the request fails.
|
|
@@ -91,7 +85,7 @@ export default function rexExtension(pi: ExtensionAPI): void {
|
|
|
91
85
|
}
|
|
92
86
|
let output = result.stdout.trim();
|
|
93
87
|
if (!output) return;
|
|
94
|
-
autoName =
|
|
88
|
+
autoName = rexSetting(pi, "autoName");
|
|
95
89
|
if (autoName) output = `${stripSessionNaming(output)}\n\n${AUTO_NAME_INSTRUCTION}`;
|
|
96
90
|
context = output;
|
|
97
91
|
});
|
package/lib/hook.ts
ADDED
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
// Install the shell hook that resumes pi in its Rex pane.
|
|
2
|
+
//
|
|
3
|
+
// pi cannot start itself after a Rex restart: Rex starts a new shell in each
|
|
4
|
+
// restored pane, so the pi process that ran there is gone. The shell reads the
|
|
5
|
+
// record that `lib/resume.ts` writes, and starts `pi --session <file>`.
|
|
6
|
+
//
|
|
7
|
+
// The user should not write that hook by hand. On the first pi session inside a
|
|
8
|
+
// Rex pane, this module writes the hook for the pane's shell:
|
|
9
|
+
//
|
|
10
|
+
// - fish: a drop-in file below `conf.d`.
|
|
11
|
+
// - zsh and bash: a marked block at the end of the shell's startup file.
|
|
12
|
+
//
|
|
13
|
+
// A file that this extension wrote starts with HOOK_MARKER or holds a marked
|
|
14
|
+
// block. Everything else belongs to the user and stays unchanged.
|
|
15
|
+
|
|
16
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
17
|
+
import os from "node:os";
|
|
18
|
+
import path from "node:path";
|
|
19
|
+
|
|
20
|
+
/** First line of a fish hook file that this extension wrote. */
|
|
21
|
+
export const HOOK_MARKER =
|
|
22
|
+
"# Generated by @tian.zuo/pi-rex. Edit or delete this file to stop the hook.";
|
|
23
|
+
|
|
24
|
+
/** Start of the block this extension appends to a shell startup file. */
|
|
25
|
+
export const BLOCK_START = "# >>> tian.zuo/pi-rex >>>";
|
|
26
|
+
|
|
27
|
+
/** End of the block this extension appends to a shell startup file. */
|
|
28
|
+
export const BLOCK_END = "# <<< tian.zuo/pi-rex <<<";
|
|
29
|
+
|
|
30
|
+
/** Shells that get a hook. */
|
|
31
|
+
export type Shell = "fish" | "zsh" | "bash" | "powershell";
|
|
32
|
+
|
|
33
|
+
const FISH_HOOK = `# Start pi again after the Rex server restarts this pane.
|
|
34
|
+
# The pi-rex extension writes <agent dir>/rex-resume/<block id>.
|
|
35
|
+
if status is-interactive; and set -q REX_BLOCK
|
|
36
|
+
function __rex_resume --on-event fish_prompt
|
|
37
|
+
functions -e __rex_resume
|
|
38
|
+
set -l agent_dir $HOME/.pi/agent
|
|
39
|
+
# Expand ~ as pi does: fish does not expand it inside a variable.
|
|
40
|
+
set -q PI_CODING_AGENT_DIR; and set agent_dir (string replace -r -- '^~(?=/|$)' $HOME $PI_CODING_AGENT_DIR)
|
|
41
|
+
set -l file $agent_dir/rex-resume/(string replace -- 'block:' '' $REX_BLOCK)
|
|
42
|
+
test -f $file; or return
|
|
43
|
+
set -l record (cat $file)
|
|
44
|
+
test -n "$record[1]"; or return
|
|
45
|
+
cd -- $record[1]; or return
|
|
46
|
+
rm -f $file
|
|
47
|
+
$record[2..]
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
`;
|
|
51
|
+
|
|
52
|
+
const ZSH_HOOK = `# Start pi again after the Rex server restarts this pane.
|
|
53
|
+
if [[ -n \${REX_BLOCK:-} ]]; then
|
|
54
|
+
__rex_dir=\${PI_CODING_AGENT_DIR:-$HOME/.pi/agent}
|
|
55
|
+
[[ $__rex_dir == '~'/* ]] && __rex_dir=$HOME/\${__rex_dir#\\~/}
|
|
56
|
+
__rex_file=$__rex_dir/rex-resume/\${REX_BLOCK#block:}
|
|
57
|
+
if [[ -f $__rex_file ]]; then
|
|
58
|
+
__rex_lines=("\${(@f)$(<$__rex_file)}")
|
|
59
|
+
if [[ -n \${__rex_lines[1]:-} ]] && cd -- \${__rex_lines[1]} 2>/dev/null; then
|
|
60
|
+
__rex_cmd=("\${__rex_lines[@]:1}")
|
|
61
|
+
rm -f $__rex_file
|
|
62
|
+
(( \${#__rex_cmd} )) && "\${__rex_cmd[@]}"
|
|
63
|
+
fi
|
|
64
|
+
fi
|
|
65
|
+
unset __rex_dir __rex_file __rex_lines __rex_cmd
|
|
66
|
+
fi`;
|
|
67
|
+
|
|
68
|
+
const BASH_HOOK = `# Start pi again after the Rex server restarts this pane.
|
|
69
|
+
# Keep this block POSIX: a desktop session can run .profile through /bin/sh.
|
|
70
|
+
case $- in
|
|
71
|
+
*i*)
|
|
72
|
+
__rex_resume() {
|
|
73
|
+
[ -n "\${REX_BLOCK:-}" ] || return 0
|
|
74
|
+
__rex_dir="\${PI_CODING_AGENT_DIR:-$HOME/.pi/agent}"
|
|
75
|
+
case "$__rex_dir" in
|
|
76
|
+
"~"/*) __rex_dir="$HOME/\${__rex_dir#\\~/}" ;;
|
|
77
|
+
esac
|
|
78
|
+
__rex_file="$__rex_dir/rex-resume/\${REX_BLOCK#block:}"
|
|
79
|
+
[ -f "$__rex_file" ] || return 0
|
|
80
|
+
__rex_cwd=""
|
|
81
|
+
__rex_first=1
|
|
82
|
+
set --
|
|
83
|
+
while IFS= read -r __rex_line || [ -n "$__rex_line" ]; do
|
|
84
|
+
if [ "$__rex_first" = 1 ]; then
|
|
85
|
+
__rex_cwd="$__rex_line"
|
|
86
|
+
__rex_first=0
|
|
87
|
+
else
|
|
88
|
+
set -- "$@" "$__rex_line"
|
|
89
|
+
fi
|
|
90
|
+
done < "$__rex_file"
|
|
91
|
+
[ -n "$__rex_cwd" ] || return 0
|
|
92
|
+
cd -- "$__rex_cwd" 2>/dev/null || return 0
|
|
93
|
+
rm -f "$__rex_file"
|
|
94
|
+
if [ "$#" -gt 0 ]; then
|
|
95
|
+
"$@"
|
|
96
|
+
fi
|
|
97
|
+
}
|
|
98
|
+
__rex_resume
|
|
99
|
+
unset -f __rex_resume
|
|
100
|
+
unset __rex_dir __rex_file __rex_cwd __rex_first __rex_line
|
|
101
|
+
;;
|
|
102
|
+
esac`;
|
|
103
|
+
|
|
104
|
+
const POWERSHELL_HOOK = `# Start pi again after the Rex server restarts this pane.
|
|
105
|
+
if ($env:REX_BLOCK) {
|
|
106
|
+
$dir = if ($env:PI_CODING_AGENT_DIR) {
|
|
107
|
+
$env:PI_CODING_AGENT_DIR -replace '^~(?=[\\\\/]|$)', $HOME
|
|
108
|
+
} else {
|
|
109
|
+
Join-Path $HOME '.pi/agent'
|
|
110
|
+
}
|
|
111
|
+
$file = Join-Path (Join-Path $dir 'rex-resume') ($env:REX_BLOCK -replace '^block:', '')
|
|
112
|
+
if (Test-Path -LiteralPath $file) {
|
|
113
|
+
$lines = @(Get-Content -LiteralPath $file)
|
|
114
|
+
if ($lines.Count -ge 2 -and (Test-Path -LiteralPath $lines[0])) {
|
|
115
|
+
$arguments = @()
|
|
116
|
+
if ($lines.Count -gt 2) { $arguments = @($lines[2..($lines.Count - 1)]) }
|
|
117
|
+
Set-Location -LiteralPath $lines[0]
|
|
118
|
+
Remove-Item -LiteralPath $file -Force
|
|
119
|
+
& $lines[1] @arguments
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}`;
|
|
123
|
+
|
|
124
|
+
/** Hook source for each shell, below the marker or between the block markers. */
|
|
125
|
+
export const HOOKS: Readonly<Record<Shell, string>> = {
|
|
126
|
+
fish: FISH_HOOK,
|
|
127
|
+
zsh: ZSH_HOOK,
|
|
128
|
+
bash: BASH_HOOK,
|
|
129
|
+
powershell: POWERSHELL_HOOK,
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
/** What happened to the hook file. */
|
|
133
|
+
export type HookInstall = {
|
|
134
|
+
readonly path: string;
|
|
135
|
+
readonly status: "created" | "updated" | "kept";
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
/** Where the hook goes, and how this extension recognizes its own text there. */
|
|
139
|
+
export type HookTarget = {
|
|
140
|
+
readonly path: string;
|
|
141
|
+
/** `drop-in` owns the whole file; `block` adds a marked block to it. */
|
|
142
|
+
readonly kind: "drop-in" | "block";
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
/** What this module reads from its surroundings. */
|
|
146
|
+
export interface HookContext {
|
|
147
|
+
readonly env?: NodeJS.ProcessEnv;
|
|
148
|
+
readonly home?: string;
|
|
149
|
+
readonly platform?: NodeJS.Platform;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/** Shell file name from `$SHELL`, without a folder, a `.exe`, or a case difference. */
|
|
153
|
+
function shellName(env: NodeJS.ProcessEnv): string {
|
|
154
|
+
// Windows paths use a backslash, which path.basename ignores on POSIX.
|
|
155
|
+
return path
|
|
156
|
+
.basename((env.SHELL ?? "").replace(/\\/g, "/"))
|
|
157
|
+
.replace(/\.exe$/i, "")
|
|
158
|
+
.toLowerCase();
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/** Shell of the pane, from `$SHELL`. Undefined for a shell without a hook. */
|
|
162
|
+
export function detectShell(context: HookContext = {}): Shell | undefined {
|
|
163
|
+
const env = context.env ?? process.env;
|
|
164
|
+
const name = shellName(env);
|
|
165
|
+
if (name === "fish" || name === "zsh" || name === "bash") return name;
|
|
166
|
+
if (name === "pwsh" || name === "powershell") return "powershell";
|
|
167
|
+
// A Windows pane has no SHELL. PowerShell exports PSModulePath to the
|
|
168
|
+
// programs it starts, and cmd.exe panes keep the one Windows sets.
|
|
169
|
+
if ((context.platform ?? process.platform) === "win32" && env.PSModulePath?.trim())
|
|
170
|
+
return "powershell";
|
|
171
|
+
return undefined;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* Startup file for a bash login shell: the file bash reads first, and
|
|
176
|
+
* `~/.profile` when none exists. A new `~/.bash_profile` would hide the files a
|
|
177
|
+
* user already has, so this never creates one.
|
|
178
|
+
*/
|
|
179
|
+
function bashStartupFile(home: string): string {
|
|
180
|
+
for (const name of [".bash_profile", ".bash_login", ".profile"]) {
|
|
181
|
+
const file = path.join(home, name);
|
|
182
|
+
if (existsSync(file)) return file;
|
|
183
|
+
}
|
|
184
|
+
return path.join(home, ".profile");
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/** File that holds the hook for a shell, and how the hook is kept there. */
|
|
188
|
+
export function hookTarget(shell: Shell, context: HookContext = {}): HookTarget {
|
|
189
|
+
const env = context.env ?? process.env;
|
|
190
|
+
const home = context.home ?? os.homedir();
|
|
191
|
+
const platform = context.platform ?? process.platform;
|
|
192
|
+
if (shell === "fish") {
|
|
193
|
+
const configHome = env.XDG_CONFIG_HOME?.trim() || path.join(home, ".config");
|
|
194
|
+
return { path: path.join(configHome, "fish", "conf.d", "rex-resume.fish"), kind: "drop-in" };
|
|
195
|
+
}
|
|
196
|
+
if (shell === "zsh") {
|
|
197
|
+
// Zsh reads .zshrc for interactive shells, and ZDOTDIR moves that file.
|
|
198
|
+
const dotdir = env.ZDOTDIR?.trim() || home;
|
|
199
|
+
return { path: path.join(dotdir, ".zshrc"), kind: "block" };
|
|
200
|
+
}
|
|
201
|
+
if (shell === "powershell") {
|
|
202
|
+
// PowerShell reads `$PROFILE.CurrentUserAllHosts`: Documents on Windows,
|
|
203
|
+
// and the config folder on macOS and Linux. Windows PowerShell 5.1 reads a
|
|
204
|
+
// different folder from PowerShell 7.
|
|
205
|
+
if (platform === "win32") {
|
|
206
|
+
const folder = shellName(env) === "powershell" ? "WindowsPowerShell" : "PowerShell";
|
|
207
|
+
return { path: path.join(home, "Documents", folder, "profile.ps1"), kind: "block" };
|
|
208
|
+
}
|
|
209
|
+
const configHome = env.XDG_CONFIG_HOME?.trim() || path.join(home, ".config");
|
|
210
|
+
return { path: path.join(configHome, "powershell", "profile.ps1"), kind: "block" };
|
|
211
|
+
}
|
|
212
|
+
return { path: bashStartupFile(home), kind: "block" };
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/** Add or replace the marked block in a shell startup file. */
|
|
216
|
+
export function upsertBlock(current: string, hook: string): string {
|
|
217
|
+
const block = `${BLOCK_START}\n${hook}\n${BLOCK_END}`;
|
|
218
|
+
const start = current.indexOf(BLOCK_START);
|
|
219
|
+
const end = current.indexOf(BLOCK_END);
|
|
220
|
+
if (start !== -1 && end > start) {
|
|
221
|
+
return `${current.slice(0, start)}${block}${current.slice(end + BLOCK_END.length)}`;
|
|
222
|
+
}
|
|
223
|
+
if (current === "") return `${block}\n`;
|
|
224
|
+
const separator = current.endsWith("\n") ? "\n" : "\n\n";
|
|
225
|
+
return `${current}${separator}${block}\n`;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* Write the hook for the pane's shell. `created` and `updated` mean this call
|
|
230
|
+
* changed a file; `kept` means the file already holds the hook, or the user
|
|
231
|
+
* wrote it. Returns undefined for a shell without a hook, and when the write
|
|
232
|
+
* fails.
|
|
233
|
+
*/
|
|
234
|
+
export function installShellHook(context: HookContext = {}): HookInstall | undefined {
|
|
235
|
+
const shell = detectShell(context);
|
|
236
|
+
if (!shell) return undefined;
|
|
237
|
+
const target = hookTarget(shell, context);
|
|
238
|
+
try {
|
|
239
|
+
const current = existsSync(target.path) ? readFileSync(target.path, "utf8") : undefined;
|
|
240
|
+
// A drop-in file without the marker belongs to the user.
|
|
241
|
+
if (target.kind === "drop-in" && current !== undefined && !current.startsWith(HOOK_MARKER)) {
|
|
242
|
+
return { path: target.path, status: "kept" };
|
|
243
|
+
}
|
|
244
|
+
const next =
|
|
245
|
+
target.kind === "drop-in"
|
|
246
|
+
? `${HOOK_MARKER}\n${HOOKS[shell]}`
|
|
247
|
+
: upsertBlock(current ?? "", HOOKS[shell]);
|
|
248
|
+
if (current === next) return { path: target.path, status: "kept" };
|
|
249
|
+
mkdirSync(path.dirname(target.path), { recursive: true });
|
|
250
|
+
writeFileSync(target.path, next);
|
|
251
|
+
return { path: target.path, status: current === undefined ? "created" : "updated" };
|
|
252
|
+
} catch {
|
|
253
|
+
// A hook that cannot be written must not break the agent. The records stay,
|
|
254
|
+
// and the user can add the hook by hand.
|
|
255
|
+
return undefined;
|
|
256
|
+
}
|
|
257
|
+
}
|
package/lib/resume.ts
CHANGED
|
@@ -3,8 +3,8 @@
|
|
|
3
3
|
// Rex restores sessions, tabs, panes, and scrollback after a restart. Each
|
|
4
4
|
// pane keeps its block ID, but Rex starts a new shell in it, not the program
|
|
5
5
|
// that ran there. This module records the pi session of the pane in
|
|
6
|
-
// `<agent dir>/rex-resume/<block id
|
|
7
|
-
//
|
|
6
|
+
// `<agent dir>/rex-resume/<block id>`, and installs the shell hook that reads
|
|
7
|
+
// the record (see `lib/hook.ts`).
|
|
8
8
|
//
|
|
9
9
|
// The record is the working directory on line 1, then the command, one
|
|
10
10
|
// argument per line. It stays when pi stops because of SIGHUP or SIGTERM, or
|
|
@@ -18,6 +18,8 @@ import {
|
|
|
18
18
|
type ExtensionAPI,
|
|
19
19
|
type ExtensionContext,
|
|
20
20
|
} from "@earendil-works/pi-coding-agent";
|
|
21
|
+
import { installShellHook } from "./hook.ts";
|
|
22
|
+
import { rexSetting } from "./settings.ts";
|
|
21
23
|
|
|
22
24
|
/** Folder below the pi agent directory that holds one record per Rex pane. */
|
|
23
25
|
export const RESUME_DIR = "rex-resume";
|
|
@@ -68,8 +70,21 @@ export function registerResume(pi: ExtensionAPI, agentDir: string = getAgentDir(
|
|
|
68
70
|
const block = process.env.REX_BLOCK;
|
|
69
71
|
// Print, JSON, and RPC runs inherit REX_BLOCK from the agent's shell, but
|
|
70
72
|
// they are not the agent of the pane. `hasUI` is also true in RPC mode.
|
|
71
|
-
|
|
72
|
-
|
|
73
|
+
const file =
|
|
74
|
+
block !== undefined && ctx.mode === "tui" ? recordPath(agentDir, block) : undefined;
|
|
75
|
+
const enabled = file !== undefined && rexSetting(pi, "resume");
|
|
76
|
+
// Turning resume off in a live session must remove the record too, or the
|
|
77
|
+
// hook would reopen the session on the next Rex restart.
|
|
78
|
+
if (file !== undefined && !enabled) rmSync(file, { force: true });
|
|
79
|
+
record = enabled ? file : undefined;
|
|
80
|
+
if (record === undefined) return;
|
|
81
|
+
// The extension is the only part of the resume chain that runs on its own,
|
|
82
|
+
// so it installs the shell half too.
|
|
83
|
+
const hook = installShellHook();
|
|
84
|
+
if (hook && hook.status !== "kept") {
|
|
85
|
+
const verb = hook.status === "created" ? "installed" : "updated";
|
|
86
|
+
ctx.ui.notify(`pi-rex: ${verb} the resume hook ${hook.path}`, "info");
|
|
87
|
+
}
|
|
73
88
|
signaled = false;
|
|
74
89
|
for (const signal of SIGNALS) process.on(signal, onSignal);
|
|
75
90
|
save(ctx);
|
package/lib/settings.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
// The pi settings that pi-rex reads. One place, so both features read the same
|
|
2
|
+
// `rex` section and the same on/off rule: only `false` turns a feature off.
|
|
3
|
+
|
|
4
|
+
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
5
|
+
|
|
6
|
+
/** Names of the on/off switches in the `rex` settings section. */
|
|
7
|
+
export type RexSwitch = "autoName" | "resume";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Read one `rex` switch. Returns `false` only for a JSON `false`; a missing
|
|
11
|
+
* section or any other value keeps the feature on.
|
|
12
|
+
*/
|
|
13
|
+
export function rexSetting(pi: ExtensionAPI, key: RexSwitch): boolean {
|
|
14
|
+
const rex = (pi.getSettings() as { rex?: unknown }).rex;
|
|
15
|
+
if (typeof rex !== "object" || rex === null) return true;
|
|
16
|
+
return (rex as Record<string, unknown>)[key] !== false;
|
|
17
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tian.zuo/pi-rex",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "Tells the pi coding agent that it runs in a Rex terminal, names the Rex session after the project folder or worktree, names the tab from the first prompt, and resumes pi after a Rex restart.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"keywords": [
|