@tian.zuo/pi-rex 0.4.0 → 0.5.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 +14 -0
- package/README.md +168 -82
- package/index.ts +99 -7
- package/lib/activity.ts +78 -0
- package/lib/cleanup.ts +104 -0
- package/lib/naming.ts +11 -4
- package/lib/prompt.ts +49 -0
- package/lib/rex-output.ts +46 -0
- package/lib/settings.ts +10 -10
- package/lib/worktree-move.ts +447 -0
- package/package.json +4 -3
- package/lib/hook.ts +0 -257
- package/lib/resume.ts +0 -105
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,19 @@
|
|
|
1
1
|
# @tian.zuo/pi-rex
|
|
2
2
|
|
|
3
|
+
## 0.5.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- [#222](https://github.com/TianZuo555/pi-extensions/pull/222) [`0f2572d`](https://github.com/TianZuo555/pi-extensions/commit/0f2572dee4b3d0729d6e18d13779dc2056cb85c7) Thanks [@TianZuo555](https://github.com/TianZuo555)! - Report the current Pi prompt to Rex through OSC 7501 program activity. Rex now shows working, completed, cancelled, and failed Pi runs in its activity view.
|
|
8
|
+
|
|
9
|
+
- [#228](https://github.com/TianZuo555/pi-extensions/pull/228) [`10e3e97`](https://github.com/TianZuo555/pi-extensions/commit/10e3e97006e36edd97e502830050a9a2604d0cd1) Thanks [@TianZuo555](https://github.com/TianZuo555)! - Add the `rex_worktree_move` tool. The agent calls it from codemode after it creates a git worktree. The tool opens a Rex session for the worktree, moves only the agent pane into it, and names the pane after the feature. When the turn ends, pi switches to the worktree, and the pi process changes to the worktree folder. The conversation stays. Each Rex app that showed the old session goes to the moved pane, when its Remote Control setting is on.
|
|
10
|
+
|
|
11
|
+
Remove the resume feature, because Rex now starts pi again in a restored pane. The `rex.resume` and `rex.resumeCommand` settings are removed. In a Rex pane, the extension removes the old shell hook and the records in `~/.pi/agent/rex-resume`. It removes only text that it wrote.
|
|
12
|
+
|
|
13
|
+
### Patch Changes
|
|
14
|
+
|
|
15
|
+
- [#222](https://github.com/TianZuo555/pi-extensions/pull/222) [`0f2572d`](https://github.com/TianZuo555/pi-extensions/commit/0f2572dee4b3d0729d6e18d13779dc2056cb85c7) Thanks [@TianZuo555](https://github.com/TianZuo555)! - Use `REX_BLOCK` for Rex context, naming, and activity when `REX_SESSION` is absent.
|
|
16
|
+
|
|
3
17
|
## 0.4.0
|
|
4
18
|
|
|
5
19
|
### Minor Changes
|
package/README.md
CHANGED
|
@@ -2,147 +2,233 @@
|
|
|
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
|
-
|
|
5
|
+
This extension connects the [pi coding agent](https://pi.dev) to [Rex](https://www.superlogical.com/rex/docs/automate/agent-skill). It does four things:
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
7
|
+
- It tells pi that pi runs in a Rex terminal.
|
|
8
|
+
- It gives a name to the Rex session and to the tab.
|
|
9
|
+
- It reports the pi prompt to the Rex activity view.
|
|
10
|
+
- It gives the agent one tool that moves the agent pane into a git worktree.
|
|
11
|
+
|
|
12
|
+
## Setup
|
|
13
|
+
|
|
14
|
+
1. Install the extension:
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
pi install npm:@tian.zuo/pi-rex
|
|
18
|
+
```
|
|
10
19
|
|
|
11
|
-
|
|
20
|
+
2. In each Rex app, open Settings > General.
|
|
21
|
+
3. Turn on Remote Control.
|
|
12
22
|
|
|
13
|
-
|
|
23
|
+
Step 3 is necessary for [Move to a worktree](#move-to-a-worktree). After the move, the Rex app shows the moved pane. If Remote Control is off, the app continues to show the old session, and you must select the new session yourself.
|
|
14
24
|
|
|
15
|
-
|
|
25
|
+
Optional: to install the Rex skill, run `rex skill setup`.
|
|
26
|
+
|
|
27
|
+
## Session start
|
|
28
|
+
|
|
29
|
+
The command `rex skill setup` writes a SessionStart hook for Claude Code and Codex. The hook tells the agent that it runs in a Rex terminal. The hook also gives the session ID and the pane ID. Pi has no command hooks, so Rex writes no hook for pi.
|
|
30
|
+
|
|
31
|
+
This extension is the hook for pi. When a pi session starts, the extension runs this command:
|
|
16
32
|
|
|
17
33
|
```bash
|
|
18
34
|
rex __internal session-start --agent pi
|
|
19
35
|
```
|
|
20
36
|
|
|
21
|
-
The extension adds the output to the system prompt as a `<rex>` section.
|
|
37
|
+
The extension adds the output to the system prompt as a `<rex>` section.
|
|
38
|
+
|
|
39
|
+
- The extension runs only when `REX_SESSION` or `REX_BLOCK` is set. Outside a Rex terminal, the extension does nothing.
|
|
40
|
+
- If the command fails, pi shows a warning. The system prompt does not change.
|
|
41
|
+
|
|
42
|
+
The extension does not install the Rex skill. To install the skill, run `rex skill setup`.
|
|
22
43
|
|
|
23
|
-
|
|
44
|
+
## Names
|
|
24
45
|
|
|
25
|
-
|
|
46
|
+
Rex gives each session and each tab a generated name. The extension replaces these names:
|
|
26
47
|
|
|
27
|
-
|
|
48
|
+
- The session gets the name of the project folder.
|
|
49
|
+
- Each tab gets a task name from the first prompt.
|
|
28
50
|
|
|
29
|
-
The
|
|
51
|
+
The agent does not run a rename command.
|
|
30
52
|
|
|
31
|
-
|
|
53
|
+
### Session name
|
|
54
|
+
|
|
55
|
+
The session name is the name of the current folder. In a linked git worktree, the name has this format: `Wrktree—<main repository folder>—<branch>`. An example is `Wrktree—pi-tian-extensions—docs/pi-layerfs-spec`. A detached worktree uses the first seven characters of the commit.
|
|
56
|
+
|
|
57
|
+
The extension finds the worktree with `git worktree list --porcelain`. If git fails, or if the folder is not a git repository, the extension uses the folder name.
|
|
58
|
+
|
|
59
|
+
### Tab name
|
|
60
|
+
|
|
61
|
+
On the first user prompt of a pi session, the extension does these steps:
|
|
62
|
+
|
|
63
|
+
1. It runs `rex whoami --json`. This is a fast check that the Rex server answers. The output also gives the tab ID and the session ID.
|
|
64
|
+
2. It reads the folder and the worktree.
|
|
65
|
+
3. It makes one model call on the current session model. The call asks for a tab label: 2 to 4 lowercase English words, with hyphens between the words.
|
|
66
|
+
4. It reads the current labels.
|
|
67
|
+
5. It runs the rename commands:
|
|
32
68
|
|
|
33
69
|
```bash
|
|
34
|
-
|
|
35
|
-
rex session
|
|
36
|
-
rex
|
|
70
|
+
session=SESSION_ID # .session_id from step 1
|
|
71
|
+
rex session inspect "$session" --json
|
|
72
|
+
rex session rename "$session" SESSION_LABEL # first tab only
|
|
73
|
+
rex window rename -s "$session" WINDOW_ID TAB_LABEL
|
|
37
74
|
```
|
|
38
75
|
|
|
39
|
-
The session
|
|
76
|
+
The extension uses the session ID from step 1, not `REX_BLOCK`. A block ID follows the tab to a different session. If `rex_worktree_move` moves the tab during the model call, the tab is not in the old session. Thus the extension renames nothing. If `rex whoami` gives no session ID, the extension uses `REX_SESSION`, or `REX_BLOCK` when `REX_SESSION` is not set.
|
|
40
77
|
|
|
41
|
-
The
|
|
78
|
+
The model call runs in the background. Thus the first response does not wait for the call.
|
|
42
79
|
|
|
43
|
-
|
|
80
|
+
The session name does not come from the model. If the model call fails, or if the reply is not a label, the extension still renames the session. Only the tab keeps its old name.
|
|
44
81
|
|
|
45
|
-
The extension
|
|
82
|
+
The extension reads the labels after the model call. If a person renames the session or the tab during the call, that name stays.
|
|
46
83
|
|
|
47
|
-
|
|
84
|
+
Rex sorts tabs by ID, and the ID comes from the time of creation. Only the first tab of a session renames the session. A later tab renames only itself.
|
|
85
|
+
|
|
86
|
+
### Generated and hand-set labels
|
|
87
|
+
|
|
88
|
+
The extension replaces a label only when the label is still a generated label:
|
|
89
|
+
|
|
90
|
+
- A session label is generated when it is two lowercase words, such as `shy-strait`. A worktree label from this extension, which starts with `Wrktree—`, is also generated.
|
|
48
91
|
- A tab label is generated when it is `Window <id>`, such as `Window 629d2113`.
|
|
49
92
|
|
|
50
|
-
|
|
93
|
+
The extension keeps a hand-set label. A one-word folder label, such as `repo`, is not a generated label. Thus a later pi session in the same tab keeps that label.
|
|
51
94
|
|
|
52
|
-
|
|
95
|
+
Naming runs one time for each pi session. If a Rex command fails, the old name stays, and the agent continues.
|
|
53
96
|
|
|
54
|
-
|
|
97
|
+
The extension names the session, so it removes the Rex naming paragraph from the `<rex>` section. It adds one line in its place. The line tells the agent that the names are automatic. The line also tells the agent not to rename them, unless the user asks. This line also overrides the rule in the `rex` skill about renames.
|
|
55
98
|
|
|
56
|
-
|
|
99
|
+
## Program activity
|
|
57
100
|
|
|
58
|
-
|
|
101
|
+
In a Rex TUI session, the extension reports the current pi prompt to Rex through OSC 7501. Rex then shows the prompt while pi works and after the run ends. The extension shortens the message to one safe line for the activity view.
|
|
59
102
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
```
|
|
103
|
+
| Run result | Reported state |
|
|
104
|
+
| ---------- | -------------- |
|
|
105
|
+
| Cancelled | `idle` |
|
|
106
|
+
| Completed | `done` |
|
|
107
|
+
| Failed | `error` |
|
|
66
108
|
|
|
67
|
-
The
|
|
109
|
+
The prompt has its own status record, `pi-prompt`. Pi continues to report its root status. The root status includes open dialogs and login screens.
|
|
68
110
|
|
|
69
|
-
|
|
111
|
+
To stop the activity reports, set `PI_PROGRAM_STATUS=0`.
|
|
70
112
|
|
|
71
|
-
|
|
113
|
+
## Move to a worktree
|
|
72
114
|
|
|
73
|
-
|
|
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 |
|
|
115
|
+
The extension registers one tool, `rex_worktree_move`. The tool moves the pane of the running agent into a new Rex session for a git worktree. Pi then works in the worktree. The conversation stays. Use the tool when the agent starts a task in a new worktree.
|
|
79
116
|
|
|
80
|
-
The
|
|
117
|
+
The tool has `codemode` exposure. Thus the model does not see the tool as a separate declaration. The agent calls the tool from a codemode script:
|
|
81
118
|
|
|
82
|
-
|
|
119
|
+
```js
|
|
120
|
+
await tools.rex_worktree_move({
|
|
121
|
+
path: "~/Workspace/worktrees/repo-pin-theme",
|
|
122
|
+
label: "pin-theme",
|
|
123
|
+
});
|
|
124
|
+
```
|
|
83
125
|
|
|
84
|
-
|
|
126
|
+
| Parameter | Required | Description |
|
|
127
|
+
| --------- | -------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
128
|
+
| `path` | Yes | The worktree folder. A leading `~` is the home folder. The folder must exist, so create the worktree first. |
|
|
129
|
+
| `label` | No | The short name of the feature. The tool gives this name to the moved pane. If you do not give a label, the pane gets the name of the worktree branch. If the worktree has no branch, the pane gets the folder name. |
|
|
85
130
|
|
|
86
|
-
|
|
131
|
+
### Procedure
|
|
87
132
|
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
end
|
|
133
|
+
The tool does these steps:
|
|
134
|
+
|
|
135
|
+
1. It runs `rex whoami --json` to find the current pane, tab, and session.
|
|
136
|
+
2. It runs `rex client ls -s <session> --json` to find the Rex apps that have the current session open.
|
|
137
|
+
3. It runs `rex session inspect <session> --json` to find the other panes in the tab.
|
|
138
|
+
4. It makes the session label from the worktree, as in [Session name](#session-name).
|
|
139
|
+
5. It runs `rex new <label> --cwd <path> --json`. This command makes a new session with one shell tab.
|
|
140
|
+
6. If the tab has other panes, it moves only this pane to a new tab in the old session. The new tab gets the label of the old tab. The other panes stay in the old tab.
|
|
141
|
+
7. It runs `rex window move <tab> --to <session> --focus`. Pi continues to run during the move.
|
|
142
|
+
8. It closes the shell tab of the new session.
|
|
143
|
+
9. It gives the pane its new name with `rex block rename`.
|
|
144
|
+
10. If the old session has no tabs, it removes the old session with `rex kill`.
|
|
145
|
+
11. It tells each Rex app from step 2 to show the moved pane, with `rex -C <app> do session.select`.
|
|
146
|
+
12. If `REX_SESSION` is set, it sets `REX_SESSION` to the new session ID.
|
|
147
|
+
13. It starts the internal command `/rex-worktree-switch <path>`.
|
|
148
|
+
14. It tells the agent to end the turn.
|
|
149
|
+
|
|
150
|
+
If step 6 or step 7 fails, the tool removes the new session. The pane stays in the old session. A failed rename in step 9 does not stop the move.
|
|
151
|
+
|
|
152
|
+
Step 6 uses three Rex commands, because Rex moves only a full tab to a different session:
|
|
153
|
+
|
|
154
|
+
```bash
|
|
155
|
+
rex window new -s OLD_SESSION TAB_LABEL --focus=false --json # new tab with a shell pane
|
|
156
|
+
rex move -s OLD_SESSION PANE right SHELL_PANE # this pane goes into the new tab
|
|
157
|
+
rex block close -s OLD_SESSION SHELL_PANE # the shell pane closes
|
|
104
158
|
```
|
|
105
159
|
|
|
106
|
-
|
|
160
|
+
### Rex app view
|
|
161
|
+
|
|
162
|
+
A Rex app continues to show the session that it showed before the move. Thus, without step 11, the pane goes out of view, and the app shows the old session.
|
|
163
|
+
|
|
164
|
+
A Rex app does step 11 only when its Remote Control setting is on. See [Setup](#setup). If an app refuses, the tool result gives the name of the app and the reason. The agent then tells you to select the new session.
|
|
165
|
+
|
|
166
|
+
Rex does not tell which app shows the pane. Thus all Rex apps that have the old session open go to the moved pane. This includes the apps on your other computers.
|
|
167
|
+
|
|
168
|
+
### The session switch
|
|
169
|
+
|
|
170
|
+
A move of the pane does not change the folder of pi. Only a command can replace the pi session, so the tool starts the command `/rex-worktree-switch`. You do not type this command. The command does these steps:
|
|
107
171
|
|
|
108
|
-
|
|
172
|
+
1. It waits until the turn ends.
|
|
173
|
+
2. It copies the conversation into a new session for the worktree, with `SessionManager.forkFrom`.
|
|
174
|
+
3. It changes the folder of the pi process to the worktree, and sets `PWD`.
|
|
175
|
+
4. It switches pi to the new session.
|
|
176
|
+
5. It sends one user message that tells the agent to continue in the worktree.
|
|
177
|
+
|
|
178
|
+
After the switch, pi makes its tools, its system prompt, and its context files again for the worktree. The session start runs again, so the naming and the `<rex>` section use the worktree.
|
|
179
|
+
|
|
180
|
+
Pi does not change the folder of its process when it switches the session. Thus step 3 is necessary for an extension that reads `process.cwd()`. Step 3 occurs before step 4, so the new extension instances see the worktree when the session starts. If the switch fails, or if an extension cancels the switch, the process goes back to the old folder.
|
|
181
|
+
|
|
182
|
+
The old session file stays in the session list of the old folder.
|
|
183
|
+
|
|
184
|
+
## Removal of the old resume hook
|
|
185
|
+
|
|
186
|
+
Version 0.4 recorded the pi session of each pane. It also installed a shell hook that started pi again after a Rex restart. Rex now does this work itself, so the extension no longer does it.
|
|
187
|
+
|
|
188
|
+
The old hook and its records can start pi a second time, or open an old session. Thus the extension removes them. This occurs one time for each pi process, on the first session start in a Rex TUI pane. The extension removes only text that it wrote:
|
|
189
|
+
|
|
190
|
+
| Item | Location | Removal condition |
|
|
191
|
+
| --------------- | ---------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------- |
|
|
192
|
+
| fish hook | `~/.config/fish/conf.d/rex-resume.fish`, below `XDG_CONFIG_HOME` | The file starts with `# Generated by @tian.zuo/pi-rex.` |
|
|
193
|
+
| zsh hook | `.zshrc` in `ZDOTDIR` or in the home folder | The block between `# >>> tian.zuo/pi-rex >>>` and `# <<< tian.zuo/pi-rex <<<` |
|
|
194
|
+
| bash hook | `.bash_profile`, `.bash_login`, and `.profile` | The same block |
|
|
195
|
+
| PowerShell hook | `profile.ps1` in `Documents\PowerShell`, `Documents\WindowsPowerShell`, and the `powershell` config folder | The same block |
|
|
196
|
+
| Records | `~/.pi/agent/rex-resume`, below `PI_CODING_AGENT_DIR` when it is set | Always |
|
|
197
|
+
|
|
198
|
+
For each changed hook file, pi shows one notice. A print, JSON, or RPC run changes no file.
|
|
109
199
|
|
|
110
200
|
## Settings
|
|
111
201
|
|
|
112
202
|
The extension reads the `rex` section of the pi settings file. Add the section to `~/.pi/agent/settings.json`.
|
|
113
203
|
|
|
114
|
-
| Key | Type | Default | Description
|
|
115
|
-
| -------------- | ------- | ------- |
|
|
116
|
-
| `rex.autoName` | boolean | `true` |
|
|
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. |
|
|
204
|
+
| Key | Type | Default | Description |
|
|
205
|
+
| -------------- | ------- | ------- | ------------------------------------------------------------------------------------------------ |
|
|
206
|
+
| `rex.autoName` | boolean | `true` | Gives names to the Rex session and the tab. `false` keeps the Rex names and the Rex naming text. |
|
|
118
207
|
|
|
119
208
|
Example:
|
|
120
209
|
|
|
121
210
|
```json
|
|
122
211
|
{
|
|
123
212
|
"rex": {
|
|
124
|
-
"autoName": false
|
|
125
|
-
"resume": false
|
|
213
|
+
"autoName": false
|
|
126
214
|
}
|
|
127
215
|
}
|
|
128
216
|
```
|
|
129
217
|
|
|
130
|
-
|
|
218
|
+
Only the value `false` stops the feature.
|
|
219
|
+
|
|
220
|
+
Version 0.5 removes the `rex.resume` and `rex.resumeCommand` settings. The extension ignores them.
|
|
131
221
|
|
|
132
222
|
## Known limits
|
|
133
223
|
|
|
134
|
-
- The extension
|
|
135
|
-
- A one-word folder label is not
|
|
136
|
-
- The model reply must be one tab label.
|
|
137
|
-
- The naming call uses the current session model. A reasoning model can
|
|
138
|
-
- The
|
|
139
|
-
-
|
|
140
|
-
- The
|
|
141
|
-
- The
|
|
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.
|
|
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.
|
|
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.
|
|
145
|
-
- A pi started by the fish hook runs inside a prompt event, so Ctrl+Z may not suspend it.
|
|
224
|
+
- The extension uses the shape of a label to find a generated label. Thus it replaces a hand-set label of two words, such as `fix-login`.
|
|
225
|
+
- A one-word folder label is not a generated label. If you start a new pi session in a different folder in the same Rex tab, the session keeps the old folder name.
|
|
226
|
+
- The model reply must be one tab label. If the reply is a sentence, the tab keeps its old name. The extension still renames the session.
|
|
227
|
+
- The naming call uses the current session model. A reasoning model can use part of the token limit before it writes the label.
|
|
228
|
+
- The extension finds the Rex naming paragraph from one text: `rex session rename`. If Rex changes that text, the extension does not remove the paragraph.
|
|
229
|
+
- Only a codemode script can call `rex_worktree_move`. If codemode is off, the agent cannot use the tool. You can type `/rex-worktree-switch <path>` to switch the session without a move of the pane.
|
|
230
|
+
- The tool gives the session name from the worktree. If a session with that name exists, Rex makes a second session with the same name.
|
|
231
|
+
- The command `/rex-worktree-switch` shows in the `/` command list.
|
|
146
232
|
|
|
147
233
|
## License
|
|
148
234
|
|
package/index.ts
CHANGED
|
@@ -12,15 +12,26 @@
|
|
|
12
12
|
// `rex session rename`. Naming runs in the background, so the first response is
|
|
13
13
|
// not delayed. Set `rex.autoName` to false to turn it off.
|
|
14
14
|
//
|
|
15
|
-
// The extension also
|
|
16
|
-
//
|
|
15
|
+
// The extension also gives the agent one tool, `rex_worktree_move`, that moves
|
|
16
|
+
// this Rex pane and the pi session into a git worktree. See
|
|
17
|
+
// `lib/worktree-move.ts`.
|
|
18
|
+
//
|
|
19
|
+
// Rex starts pi again in a restored pane by itself. The extension removes the
|
|
20
|
+
// resume hook that version 0.4 installed. See `lib/cleanup.ts`.
|
|
17
21
|
|
|
18
|
-
import
|
|
22
|
+
import {
|
|
23
|
+
getAgentDir,
|
|
24
|
+
type AgentEndEvent,
|
|
25
|
+
type ExtensionAPI,
|
|
26
|
+
type ExtensionContext,
|
|
27
|
+
} from "@earendil-works/pi-coding-agent";
|
|
28
|
+
import { createRexActivityReporter, summarizeActivity } from "./lib/activity.ts";
|
|
29
|
+
import { removeLegacyResume } from "./lib/cleanup.ts";
|
|
19
30
|
import { nameSession, REX_TIMEOUT_MS, type ExecResult } from "./lib/naming.ts";
|
|
20
31
|
import { AUTO_NAME_INSTRUCTION, REX_SECTION } from "./lib/prompt.ts";
|
|
21
|
-
import { registerResume } from "./lib/resume.ts";
|
|
22
32
|
import { stripSessionNaming } from "./lib/rex-output.ts";
|
|
23
33
|
import { rexSetting } from "./lib/settings.ts";
|
|
34
|
+
import { registerWorktreeMove } from "./lib/worktree-move.ts";
|
|
24
35
|
|
|
25
36
|
/** Timeout for the naming model call. */
|
|
26
37
|
const NAMING_TIMEOUT_MS = 15000;
|
|
@@ -59,19 +70,52 @@ async function askModel(ctx: ExtensionContext, prompt: string): Promise<string>
|
|
|
59
70
|
}
|
|
60
71
|
}
|
|
61
72
|
|
|
73
|
+
function rexSessionSelector(): string | undefined {
|
|
74
|
+
return process.env.REX_SESSION || process.env.REX_BLOCK;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function latestAssistantError(messages: AgentEndEvent["messages"]): string | undefined {
|
|
78
|
+
const assistant = [...messages].reverse().find((message) => message.role === "assistant");
|
|
79
|
+
if (assistant?.stopReason !== "error") return undefined;
|
|
80
|
+
return summarizeActivity(assistant.errorMessage ?? "Pi request failed");
|
|
81
|
+
}
|
|
82
|
+
|
|
62
83
|
export default function rexExtension(pi: ExtensionAPI): void {
|
|
84
|
+
const activity = createRexActivityReporter();
|
|
63
85
|
let context = "";
|
|
64
86
|
let autoName = true;
|
|
65
87
|
let naming: Promise<void> | undefined;
|
|
88
|
+
let activityEnabled = false;
|
|
89
|
+
let activityRunning = false;
|
|
90
|
+
let activityTask = "pi";
|
|
91
|
+
let activityError: string | undefined;
|
|
92
|
+
let openPrompts = 0;
|
|
93
|
+
let cleaned = false;
|
|
94
|
+
|
|
95
|
+
const reportWorking = (): void => {
|
|
96
|
+
if (activityRunning && openPrompts === 0) activity.working(activityTask);
|
|
97
|
+
};
|
|
66
98
|
|
|
67
|
-
|
|
99
|
+
registerWorktreeMove(pi);
|
|
68
100
|
|
|
69
101
|
pi.on("session_start", async (_event, ctx) => {
|
|
70
102
|
context = "";
|
|
71
103
|
autoName = true;
|
|
72
104
|
naming = undefined;
|
|
73
|
-
|
|
105
|
+
activityRunning = false;
|
|
106
|
+
activityTask = "pi";
|
|
107
|
+
activityError = undefined;
|
|
108
|
+
openPrompts = 0;
|
|
109
|
+
const session = rexSessionSelector();
|
|
110
|
+
activityEnabled = Boolean(session) && ctx.mode === "tui";
|
|
74
111
|
if (!session) return;
|
|
112
|
+
// Once per process, in the TUI only: a print run must not change files.
|
|
113
|
+
if (!cleaned && ctx.mode === "tui") {
|
|
114
|
+
cleaned = true;
|
|
115
|
+
for (const file of removeLegacyResume({ agentDir: getAgentDir() })) {
|
|
116
|
+
ctx.ui.notify(`pi-rex: removed the old resume hook from ${file}`, "info");
|
|
117
|
+
}
|
|
118
|
+
}
|
|
75
119
|
const result = await pi.exec("rex", ["__internal", "session-start", "--agent", "pi"], {
|
|
76
120
|
cwd: ctx.cwd,
|
|
77
121
|
timeout: REX_TIMEOUT_MS,
|
|
@@ -91,8 +135,16 @@ export default function rexExtension(pi: ExtensionAPI): void {
|
|
|
91
135
|
});
|
|
92
136
|
|
|
93
137
|
pi.on("before_agent_start", (event, ctx) => {
|
|
138
|
+
if (activityEnabled) {
|
|
139
|
+
const prompt = event.messageType === "user" ? summarizeActivity(event.prompt) : "";
|
|
140
|
+
activityTask = prompt || ctx.sessionManager.getSessionName() || "pi";
|
|
141
|
+
activityError = undefined;
|
|
142
|
+
activityRunning = true;
|
|
143
|
+
reportWorking();
|
|
144
|
+
}
|
|
145
|
+
|
|
94
146
|
if (context) event.systemPromptOptions.sections[REX_SECTION] = context;
|
|
95
|
-
const session =
|
|
147
|
+
const session = rexSessionSelector();
|
|
96
148
|
if (!context || !autoName || naming || !session) return;
|
|
97
149
|
if (event.messageType !== "user" || event.prompt.trim().length === 0) return;
|
|
98
150
|
naming = nameSession({
|
|
@@ -106,4 +158,44 @@ export default function rexExtension(pi: ExtensionAPI): void {
|
|
|
106
158
|
ask: (prompt) => askModel(ctx, prompt),
|
|
107
159
|
}).catch(() => undefined);
|
|
108
160
|
});
|
|
161
|
+
|
|
162
|
+
pi.on("agent_start", () => {
|
|
163
|
+
activityError = undefined;
|
|
164
|
+
reportWorking();
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
pi.on("agent_end", (event) => {
|
|
168
|
+
if (activityRunning) activityError = latestAssistantError(event.messages);
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
pi.on("ui_prompt_start", () => {
|
|
172
|
+
if (!activityEnabled) return;
|
|
173
|
+
openPrompts += 1;
|
|
174
|
+
if (activityRunning) activity.clear();
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
pi.on("ui_prompt_end", () => {
|
|
178
|
+
if (openPrompts === 0) return;
|
|
179
|
+
openPrompts -= 1;
|
|
180
|
+
reportWorking();
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
pi.on("agent_settled", (event) => {
|
|
184
|
+
if (!activityRunning) return;
|
|
185
|
+
activityRunning = false;
|
|
186
|
+
if (event.aborted) {
|
|
187
|
+
activity.idle();
|
|
188
|
+
} else if (activityError) {
|
|
189
|
+
activity.error(activityError);
|
|
190
|
+
} else {
|
|
191
|
+
activity.done(activityTask);
|
|
192
|
+
}
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
pi.on("session_shutdown", (event) => {
|
|
196
|
+
activityEnabled = false;
|
|
197
|
+
activityRunning = false;
|
|
198
|
+
openPrompts = 0;
|
|
199
|
+
if (event.reason !== "quit") activity.clear();
|
|
200
|
+
});
|
|
109
201
|
}
|
package/lib/activity.ts
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
const CONTROL_CHARACTERS = /[\u0000-\u001f\u007f-\u009f]/g;
|
|
2
|
+
const MAX_MESSAGE_BYTES = 192;
|
|
3
|
+
|
|
4
|
+
type ActivityState = "idle" | "working" | "done" | "error" | "clear";
|
|
5
|
+
|
|
6
|
+
type Write = (data: string) => void;
|
|
7
|
+
|
|
8
|
+
export interface RexActivityReporter {
|
|
9
|
+
clear(): void;
|
|
10
|
+
working(message: string): void;
|
|
11
|
+
done(message: string): void;
|
|
12
|
+
error(message: string): void;
|
|
13
|
+
idle(): void;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function truncateUtf8(text: string): string {
|
|
17
|
+
if (Buffer.byteLength(text, "utf8") <= MAX_MESSAGE_BYTES) return text;
|
|
18
|
+
|
|
19
|
+
let bytes = 0;
|
|
20
|
+
let end = 0;
|
|
21
|
+
for (const character of text) {
|
|
22
|
+
const size = Buffer.byteLength(character, "utf8");
|
|
23
|
+
if (bytes + size > MAX_MESSAGE_BYTES) break;
|
|
24
|
+
bytes += size;
|
|
25
|
+
end += character.length;
|
|
26
|
+
}
|
|
27
|
+
return text.slice(0, end);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** Make one-line text safe for the OSC 7501 message field. */
|
|
31
|
+
export function summarizeActivity(text: string): string {
|
|
32
|
+
return truncateUtf8(text.replace(CONTROL_CHARACTERS, " ").replace(/\s+/g, " ").trim());
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** Address a separate record so Pi can keep reporting its own root status. */
|
|
36
|
+
export function formatRexActivityStatus(state: ActivityState, message?: string): string {
|
|
37
|
+
const pairs = [`state=${state}`, "id=pi-prompt", "app=pi"];
|
|
38
|
+
const summary = message ? summarizeActivity(message) : "";
|
|
39
|
+
if (summary) pairs.push(`msg=${Buffer.from(summary, "utf8").toString("base64")}`);
|
|
40
|
+
return `\x1b]7501;${pairs.join(":")}\x1b\\`;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function canReportActivity(): boolean {
|
|
44
|
+
return process.env.PI_PROGRAM_STATUS !== "0" && process.stdout.isTTY === true;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function createRexActivityReporter(
|
|
48
|
+
write: Write = process.stdout.write.bind(process.stdout),
|
|
49
|
+
enabled: () => boolean = canReportActivity,
|
|
50
|
+
): RexActivityReporter {
|
|
51
|
+
let lastReport = "";
|
|
52
|
+
|
|
53
|
+
const report = (state: ActivityState, message?: string): void => {
|
|
54
|
+
if (!enabled()) return;
|
|
55
|
+
const sequence = formatRexActivityStatus(state, message);
|
|
56
|
+
if (sequence === lastReport) return;
|
|
57
|
+
lastReport = sequence;
|
|
58
|
+
write(sequence);
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
return {
|
|
62
|
+
clear() {
|
|
63
|
+
if (lastReport) report("clear");
|
|
64
|
+
},
|
|
65
|
+
working(message) {
|
|
66
|
+
report("working", message);
|
|
67
|
+
},
|
|
68
|
+
done(message) {
|
|
69
|
+
report("done", message);
|
|
70
|
+
},
|
|
71
|
+
error(message) {
|
|
72
|
+
report("error", message);
|
|
73
|
+
},
|
|
74
|
+
idle() {
|
|
75
|
+
report("idle");
|
|
76
|
+
},
|
|
77
|
+
};
|
|
78
|
+
}
|