@tian.zuo/pi-rex 0.3.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 +24 -0
- package/README.md +175 -66
- package/index.ts +101 -15
- 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 +17 -0
- package/lib/worktree-move.ts +447 -0
- package/package.json +4 -3
- package/lib/resume.ts +0 -90
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,29 @@
|
|
|
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
|
+
|
|
17
|
+
## 0.4.0
|
|
18
|
+
|
|
19
|
+
### Minor Changes
|
|
20
|
+
|
|
21
|
+
- [#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.
|
|
22
|
+
|
|
23
|
+
### Patch Changes
|
|
24
|
+
|
|
25
|
+
- [#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.
|
|
26
|
+
|
|
3
27
|
## 0.3.0
|
|
4
28
|
|
|
5
29
|
### Minor Changes
|
package/README.md
CHANGED
|
@@ -2,99 +2,208 @@
|
|
|
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
|
+
```
|
|
19
|
+
|
|
20
|
+
2. In each Rex app, open Settings > General.
|
|
21
|
+
3. Turn on Remote Control.
|
|
10
22
|
|
|
11
|
-
|
|
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.
|
|
12
24
|
|
|
13
|
-
|
|
25
|
+
Optional: to install the Rex skill, run `rex skill setup`.
|
|
14
26
|
|
|
15
|
-
|
|
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`.
|
|
43
|
+
|
|
44
|
+
## Names
|
|
45
|
+
|
|
46
|
+
Rex gives each session and each tab a generated name. The extension replaces these names:
|
|
22
47
|
|
|
23
|
-
|
|
48
|
+
- The session gets the name of the project folder.
|
|
49
|
+
- Each tab gets a task name from the first prompt.
|
|
24
50
|
|
|
25
|
-
|
|
51
|
+
The agent does not run a rename command.
|
|
26
52
|
|
|
27
|
-
|
|
53
|
+
### Session name
|
|
28
54
|
|
|
29
|
-
The session name is the current folder
|
|
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.
|
|
30
56
|
|
|
31
|
-
|
|
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.
|
|
77
|
+
|
|
78
|
+
The model call runs in the background. Thus the first response does not wait for the call.
|
|
79
|
+
|
|
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.
|
|
81
|
+
|
|
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.
|
|
40
83
|
|
|
41
|
-
|
|
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.
|
|
42
85
|
|
|
43
|
-
|
|
86
|
+
### Generated and hand-set labels
|
|
44
87
|
|
|
45
|
-
The extension replaces a label only when the label is still generated:
|
|
88
|
+
The extension replaces a label only when the label is still a generated label:
|
|
46
89
|
|
|
47
|
-
- A session label is generated when it is two lowercase words, such as `shy-strait
|
|
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.
|
|
94
|
+
|
|
95
|
+
Naming runs one time for each pi session. If a Rex command fails, the old name stays, and the agent continues.
|
|
96
|
+
|
|
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.
|
|
98
|
+
|
|
99
|
+
## Program activity
|
|
100
|
+
|
|
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.
|
|
102
|
+
|
|
103
|
+
| Run result | Reported state |
|
|
104
|
+
| ---------- | -------------- |
|
|
105
|
+
| Cancelled | `idle` |
|
|
106
|
+
| Completed | `done` |
|
|
107
|
+
| Failed | `error` |
|
|
51
108
|
|
|
52
|
-
|
|
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.
|
|
53
110
|
|
|
54
|
-
|
|
111
|
+
To stop the activity reports, set `PI_PROGRAM_STATUS=0`.
|
|
55
112
|
|
|
56
|
-
|
|
113
|
+
## Move to a worktree
|
|
57
114
|
|
|
58
|
-
|
|
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.
|
|
59
116
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
/
|
|
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:
|
|
118
|
+
|
|
119
|
+
```js
|
|
120
|
+
await tools.rex_worktree_move({
|
|
121
|
+
path: "~/Workspace/worktrees/repo-pin-theme",
|
|
122
|
+
label: "pin-theme",
|
|
123
|
+
});
|
|
65
124
|
```
|
|
66
125
|
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
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. |
|
|
130
|
+
|
|
131
|
+
### Procedure
|
|
132
|
+
|
|
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
|
|
87
158
|
```
|
|
88
159
|
|
|
89
|
-
|
|
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:
|
|
171
|
+
|
|
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.
|
|
90
199
|
|
|
91
200
|
## Settings
|
|
92
201
|
|
|
93
202
|
The extension reads the `rex` section of the pi settings file. Add the section to `~/.pi/agent/settings.json`.
|
|
94
203
|
|
|
95
|
-
| Key | Type | Default | Description
|
|
96
|
-
| -------------- | ------- | ------- |
|
|
97
|
-
| `rex.autoName` | boolean | `true` |
|
|
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. |
|
|
98
207
|
|
|
99
208
|
Example:
|
|
100
209
|
|
|
@@ -106,20 +215,20 @@ Example:
|
|
|
106
215
|
}
|
|
107
216
|
```
|
|
108
217
|
|
|
109
|
-
|
|
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.
|
|
110
221
|
|
|
111
222
|
## Known limits
|
|
112
223
|
|
|
113
|
-
- The extension
|
|
114
|
-
- A one-word folder label is not
|
|
115
|
-
- The model reply must be one tab label.
|
|
116
|
-
- The naming call uses the current session model. A reasoning model can
|
|
117
|
-
- The
|
|
118
|
-
-
|
|
119
|
-
- The
|
|
120
|
-
-
|
|
121
|
-
- 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.
|
|
122
|
-
- 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.
|
|
123
232
|
|
|
124
233
|
## License
|
|
125
234
|
|
package/index.ts
CHANGED
|
@@ -12,25 +12,30 @@
|
|
|
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";
|
|
33
|
+
import { rexSetting } from "./lib/settings.ts";
|
|
34
|
+
import { registerWorktreeMove } from "./lib/worktree-move.ts";
|
|
23
35
|
|
|
24
36
|
/** Timeout for the naming model call. */
|
|
25
37
|
const NAMING_TIMEOUT_MS = 15000;
|
|
26
38
|
|
|
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
39
|
/**
|
|
35
40
|
* Ask the session model for one label. Returns an empty string when the model
|
|
36
41
|
* is missing or the request fails.
|
|
@@ -65,19 +70,52 @@ async function askModel(ctx: ExtensionContext, prompt: string): Promise<string>
|
|
|
65
70
|
}
|
|
66
71
|
}
|
|
67
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
|
+
|
|
68
83
|
export default function rexExtension(pi: ExtensionAPI): void {
|
|
84
|
+
const activity = createRexActivityReporter();
|
|
69
85
|
let context = "";
|
|
70
86
|
let autoName = true;
|
|
71
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
|
+
};
|
|
72
98
|
|
|
73
|
-
|
|
99
|
+
registerWorktreeMove(pi);
|
|
74
100
|
|
|
75
101
|
pi.on("session_start", async (_event, ctx) => {
|
|
76
102
|
context = "";
|
|
77
103
|
autoName = true;
|
|
78
104
|
naming = undefined;
|
|
79
|
-
|
|
105
|
+
activityRunning = false;
|
|
106
|
+
activityTask = "pi";
|
|
107
|
+
activityError = undefined;
|
|
108
|
+
openPrompts = 0;
|
|
109
|
+
const session = rexSessionSelector();
|
|
110
|
+
activityEnabled = Boolean(session) && ctx.mode === "tui";
|
|
80
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
|
+
}
|
|
81
119
|
const result = await pi.exec("rex", ["__internal", "session-start", "--agent", "pi"], {
|
|
82
120
|
cwd: ctx.cwd,
|
|
83
121
|
timeout: REX_TIMEOUT_MS,
|
|
@@ -91,14 +129,22 @@ export default function rexExtension(pi: ExtensionAPI): void {
|
|
|
91
129
|
}
|
|
92
130
|
let output = result.stdout.trim();
|
|
93
131
|
if (!output) return;
|
|
94
|
-
autoName =
|
|
132
|
+
autoName = rexSetting(pi, "autoName");
|
|
95
133
|
if (autoName) output = `${stripSessionNaming(output)}\n\n${AUTO_NAME_INSTRUCTION}`;
|
|
96
134
|
context = output;
|
|
97
135
|
});
|
|
98
136
|
|
|
99
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
|
+
|
|
100
146
|
if (context) event.systemPromptOptions.sections[REX_SECTION] = context;
|
|
101
|
-
const session =
|
|
147
|
+
const session = rexSessionSelector();
|
|
102
148
|
if (!context || !autoName || naming || !session) return;
|
|
103
149
|
if (event.messageType !== "user" || event.prompt.trim().length === 0) return;
|
|
104
150
|
naming = nameSession({
|
|
@@ -112,4 +158,44 @@ export default function rexExtension(pi: ExtensionAPI): void {
|
|
|
112
158
|
ask: (prompt) => askModel(ctx, prompt),
|
|
113
159
|
}).catch(() => undefined);
|
|
114
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
|
+
});
|
|
115
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
|
+
}
|