@tian.zuo/pi-rex 0.0.0-stage → 0.1.1
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 +19 -0
- package/LICENSE +21 -0
- package/README.md +74 -2
- package/index.ts +77 -0
- package/lib/prompt.ts +27 -0
- package/lib/rex-output.ts +27 -0
- package/package.json +45 -3
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# @tian.zuo/pi-rex
|
|
2
|
+
|
|
3
|
+
## 0.1.1
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- Ship `lib/` in the published package. Version 0.1.0 did not load: `index.ts` imports `./lib/rex-output.ts` and `./lib/prompt.ts`, but `files` left `lib/` out of the tarball.
|
|
8
|
+
|
|
9
|
+
## 0.1.0
|
|
10
|
+
|
|
11
|
+
### Minor Changes
|
|
12
|
+
|
|
13
|
+
- [#188](https://github.com/TianZuo555/pi-extensions/pull/188) [`dae7bfa`](https://github.com/TianZuo555/pi-extensions/commit/dae7bfab074274318518b7e66601848db7a26b3c) Thanks [@TianZuo555](https://github.com/TianZuo555)! - First published release.
|
|
14
|
+
|
|
15
|
+
Rex installs a SessionStart hook for Claude Code and Codex, but not for pi. This package supplies that hook. In a Rex terminal, it runs `rex __internal session-start --agent pi` when a session starts. It adds the output to the system prompt as a `<rex>` section, so pi knows its Rex session and pane without a search.
|
|
16
|
+
|
|
17
|
+
Set `rex.renameTab` to `true` in the pi settings to also tell pi to name the Rex tab. With tab naming on, the first tab of a session takes the session label and a later tab gets its own label.
|
|
18
|
+
|
|
19
|
+
When the session already has more than one tab, the extension keeps the session label: it removes the Rex session-naming paragraph and adds an instruction to keep the current label. This stops a new tab from renaming its parent session. A failed `rex session inspect` leaves the Rex text unchanged.
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Tian Zuo
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,75 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @tian.zuo/pi-rex
|
|
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
|
+
|
|
5
|
+
The [Rex](https://www.superlogical.com/rex/docs/automate/agent-skill) session-start hook for the [pi coding agent](https://pi.dev).
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pi install npm:@tian.zuo/pi-rex
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## How it works
|
|
12
|
+
|
|
13
|
+
`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 supplies the session ID and the pane ID. Rex writes the skill for pi, but pi has no command hooks.
|
|
14
|
+
|
|
15
|
+
This extension supplies the hook for pi. When a session starts and `REX_SESSION` is set, the extension runs this command:
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
rex __internal session-start --agent pi
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
The extension adds the output to the system prompt as a `<rex>` section. Outside a Rex terminal, the extension does nothing. If the command fails, pi shows a warning. The system prompt does not change.
|
|
22
|
+
|
|
23
|
+
Run `rex skill setup` to install the skill. This extension does not install the skill.
|
|
24
|
+
|
|
25
|
+
## Settings
|
|
26
|
+
|
|
27
|
+
The extension reads the `rex` section of the pi settings file. Add the section to `~/.pi/agent/settings.json`.
|
|
28
|
+
|
|
29
|
+
| Key | Type | Default | Description |
|
|
30
|
+
| --------------- | ------- | ------- | ---------------------------- |
|
|
31
|
+
| `rex.renameTab` | boolean | `false` | Tell pi to name the Rex tab. |
|
|
32
|
+
|
|
33
|
+
Example:
|
|
34
|
+
|
|
35
|
+
```json
|
|
36
|
+
{
|
|
37
|
+
"rex": {
|
|
38
|
+
"renameTab": true
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
Any value other than `true` keeps tab naming off.
|
|
44
|
+
|
|
45
|
+
## Naming
|
|
46
|
+
|
|
47
|
+
Rex gives each session and each tab a generated name. The agent can replace these names with task names.
|
|
48
|
+
|
|
49
|
+
### Session naming
|
|
50
|
+
|
|
51
|
+
Session naming is on for a session with one tab. Rex supplies the naming instructions in the session-start output. The extension does not run `rex session rename`. No setting controls session naming.
|
|
52
|
+
|
|
53
|
+
When the session has more than one tab, the extension removes the session-naming paragraph. It adds a second instruction instead: keep the session label and do not rename the session. A session is the parent of its tabs, so a tab must not rename the parent.
|
|
54
|
+
|
|
55
|
+
The extension counts the tabs with `rex session inspect "$REX_SESSION" --json`. If that command fails, the Rex text stays unchanged.
|
|
56
|
+
|
|
57
|
+
### Tab naming
|
|
58
|
+
|
|
59
|
+
Set `rex.renameTab` to `true` to let pi name the tab. The extension then adds tab instructions to the `<rex>` section. The instructions tell pi to run:
|
|
60
|
+
|
|
61
|
+
```bash
|
|
62
|
+
rex window rename -s "$REX_SESSION" WINDOW_ID LABEL
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
A session is the parent. A tab is the child. pi names the session first. Then pi reads its window ID with `rex whoami --json`. It compares that ID with the window IDs of the session from `rex window ls`. The first tab takes the session label. A later tab gets its own label. pi keeps a label that already fits the work. If the rename fails, pi continues the task and does not show a message.
|
|
66
|
+
|
|
67
|
+
## Known limits
|
|
68
|
+
|
|
69
|
+
- The Rex CLI does not report whether a label is generated or set by hand. The extension decides from the tab count only. A session with one tab and a hand-set label can still lose that label.
|
|
70
|
+
- The session rule reads one string from the Rex output: the paragraph that holds `rex session rename`. A change to that wording stops the removal.
|
|
71
|
+
- The two instructions are prompt text. An agent can ignore them. The extension never runs a rename command itself.
|
|
72
|
+
|
|
73
|
+
## License
|
|
74
|
+
|
|
75
|
+
[MIT](../../LICENSE) © Tian Zuo
|
package/index.ts
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
// pi-rex — the Rex SessionStart hook for the pi coding agent.
|
|
2
|
+
//
|
|
3
|
+
// `rex skill setup` writes a SessionStart hook for Claude Code and Codex that
|
|
4
|
+
// tells the agent it runs in a Rex terminal and which session and pane it is
|
|
5
|
+
// in. Rex has no hook for pi, so this extension runs the same command and adds
|
|
6
|
+
// its output to the system prompt as a `<rex>` section.
|
|
7
|
+
//
|
|
8
|
+
// Two additions on top of the Rex output:
|
|
9
|
+
// - When the session has more than one tab, the session label is kept. A tab
|
|
10
|
+
// must not rename its parent session.
|
|
11
|
+
// - Set `rex.renameTab` to true in the pi settings to also tell pi to name the
|
|
12
|
+
// Rex tab. Tab naming is off by default.
|
|
13
|
+
|
|
14
|
+
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
15
|
+
import { countWindows, stripSessionNaming } from "./lib/rex-output.ts";
|
|
16
|
+
import { KEEP_SESSION_INSTRUCTION, REX_SECTION, TAB_RENAME_INSTRUCTION } from "./lib/prompt.ts";
|
|
17
|
+
|
|
18
|
+
interface RexSettings {
|
|
19
|
+
renameTab?: boolean;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** Read `rex.renameTab`. Anything other than `true` keeps tab naming off. */
|
|
23
|
+
function renameTabEnabled(pi: ExtensionAPI): boolean {
|
|
24
|
+
const rex = (pi.getSettings() as { rex?: unknown }).rex;
|
|
25
|
+
if (typeof rex !== "object" || rex === null) return false;
|
|
26
|
+
return (rex as RexSettings).renameTab === true;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Count the tabs of the session. A missing answer keeps the Rex behavior, so a
|
|
31
|
+
* failure never hides the session-naming instructions.
|
|
32
|
+
*/
|
|
33
|
+
async function countSessionWindows(
|
|
34
|
+
pi: ExtensionAPI,
|
|
35
|
+
session: string,
|
|
36
|
+
cwd: string,
|
|
37
|
+
): Promise<number | undefined> {
|
|
38
|
+
const result = await pi.exec("rex", ["session", "inspect", session, "--json"], {
|
|
39
|
+
cwd,
|
|
40
|
+
timeout: 5000,
|
|
41
|
+
});
|
|
42
|
+
if (result.code !== 0) return undefined;
|
|
43
|
+
return countWindows(result.stdout);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export default function rexExtension(pi: ExtensionAPI): void {
|
|
47
|
+
let context = "";
|
|
48
|
+
|
|
49
|
+
pi.on("session_start", async (_event, ctx) => {
|
|
50
|
+
context = "";
|
|
51
|
+
const session = process.env.REX_SESSION;
|
|
52
|
+
if (!session) return;
|
|
53
|
+
const result = await pi.exec("rex", ["__internal", "session-start", "--agent", "pi"], {
|
|
54
|
+
cwd: ctx.cwd,
|
|
55
|
+
timeout: 5000,
|
|
56
|
+
});
|
|
57
|
+
if (result.code !== 0) {
|
|
58
|
+
ctx.ui.notify(
|
|
59
|
+
`rex session-start failed (exit ${result.code}): ${result.stderr.trim()}`,
|
|
60
|
+
"warning",
|
|
61
|
+
);
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
let output = result.stdout.trim();
|
|
65
|
+
if (!output) return;
|
|
66
|
+
const windows = await countSessionWindows(pi, session, ctx.cwd);
|
|
67
|
+
if (windows !== undefined && windows > 1) {
|
|
68
|
+
output = `${stripSessionNaming(output)}\n\n${KEEP_SESSION_INSTRUCTION}`;
|
|
69
|
+
}
|
|
70
|
+
if (renameTabEnabled(pi)) output = `${output}\n\n${TAB_RENAME_INSTRUCTION}`;
|
|
71
|
+
context = output;
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
pi.on("before_agent_start", (event) => {
|
|
75
|
+
if (context) event.systemPromptOptions.sections[REX_SECTION] = context;
|
|
76
|
+
});
|
|
77
|
+
}
|
package/lib/prompt.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
// Model-facing text for pi-rex. Keep every prompt string here, apart from the
|
|
2
|
+
// runtime that injects it.
|
|
3
|
+
|
|
4
|
+
/** System prompt section name. */
|
|
5
|
+
export const REX_SECTION = "rex";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Replaces the Rex session-naming paragraph when the session has more than one
|
|
9
|
+
* tab. A session is the parent of its tabs, so a later tab must not rename it.
|
|
10
|
+
*/
|
|
11
|
+
export const KEEP_SESSION_INSTRUCTION = `The session already has more than one tab. Do not rename the session. Do not run \`rex session rename\`. Keep the current session label.`;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Appended to the Rex session-start output when `rex.renameTab` is true.
|
|
15
|
+
*
|
|
16
|
+
* A session is the parent and a tab is the child: the first tab carries the
|
|
17
|
+
* session label, later tabs get their own label.
|
|
18
|
+
*/
|
|
19
|
+
export const TAB_RENAME_INSTRUCTION = `Name the tab (window) too. Do it in the same step as the session rename. Do not wait for the user to ask.
|
|
20
|
+
|
|
21
|
+
A Rex session is the parent and a tab is the child. The first tab of a session takes the session label. A later tab gets its own label.
|
|
22
|
+
|
|
23
|
+
1. Run \`rex whoami --json\` and read \`.window_id\`.
|
|
24
|
+
2. Run \`rex window ls -s "$REX_SESSION" --json\` and sort the window IDs.
|
|
25
|
+
3. If your window ID is the first ID, use the session label. If not, choose a label for the work in this tab. Use two to four lowercase words joined by hyphens.
|
|
26
|
+
4. Rename with \`rex window rename -s "$REX_SESSION" WINDOW_ID LABEL\`.
|
|
27
|
+
5. Keep a label that already fits the work. If the rename fails, continue the task and do not mention it.`;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
// Helpers that read and reshape the Rex session-start output.
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Remove the paragraph that asks pi to rename the session.
|
|
5
|
+
*
|
|
6
|
+
* Rex marks that paragraph with `rex session rename`, so the match does not
|
|
7
|
+
* depend on the rest of the wording.
|
|
8
|
+
*/
|
|
9
|
+
export function stripSessionNaming(output: string): string {
|
|
10
|
+
return output
|
|
11
|
+
.split(/\n{2,}/)
|
|
12
|
+
.filter((paragraph) => !paragraph.includes("rex session rename"))
|
|
13
|
+
.join("\n\n")
|
|
14
|
+
.trim();
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** Read `.windows` from `rex session inspect --json` output. */
|
|
18
|
+
export function countWindows(json: string): number | undefined {
|
|
19
|
+
try {
|
|
20
|
+
const parsed: unknown = JSON.parse(json);
|
|
21
|
+
if (typeof parsed !== "object" || parsed === null) return undefined;
|
|
22
|
+
const windows = (parsed as { windows?: unknown }).windows;
|
|
23
|
+
return Array.isArray(windows) ? windows.length : undefined;
|
|
24
|
+
} catch {
|
|
25
|
+
return undefined;
|
|
26
|
+
}
|
|
27
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,48 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tian.zuo/pi-rex",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "Tells the pi coding agent that it runs in a Rex terminal, like the Rex hook for Claude Code and Codex.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"pi",
|
|
8
|
+
"pi-package",
|
|
9
|
+
"pi-extension",
|
|
10
|
+
"pi-coding-agent",
|
|
11
|
+
"rex"
|
|
12
|
+
],
|
|
13
|
+
"author": "Tian Zuo",
|
|
14
|
+
"license": "MIT",
|
|
15
|
+
"repository": {
|
|
16
|
+
"type": "git",
|
|
17
|
+
"url": "git+https://github.com/TianZuo555/pi-extensions.git",
|
|
18
|
+
"directory": "packages/pi-rex"
|
|
19
|
+
},
|
|
20
|
+
"homepage": "https://github.com/TianZuo555/pi-extensions#pi-rex",
|
|
21
|
+
"bugs": {
|
|
22
|
+
"url": "https://github.com/TianZuo555/pi-extensions/issues"
|
|
23
|
+
},
|
|
24
|
+
"files": [
|
|
25
|
+
"index.ts",
|
|
26
|
+
"lib",
|
|
27
|
+
"CHANGELOG.md"
|
|
28
|
+
],
|
|
29
|
+
"pi": {
|
|
30
|
+
"extensions": [
|
|
31
|
+
"./index.ts"
|
|
32
|
+
]
|
|
33
|
+
},
|
|
34
|
+
"peerDependencies": {
|
|
35
|
+
"@earendil-works/pi-coding-agent": "*"
|
|
36
|
+
},
|
|
37
|
+
"devDependencies": {
|
|
38
|
+
"@effect/language-service": "^0.87.5",
|
|
39
|
+
"typescript": "^7.0.2"
|
|
40
|
+
},
|
|
41
|
+
"publishConfig": {
|
|
42
|
+
"access": "public"
|
|
43
|
+
},
|
|
44
|
+
"scripts": {
|
|
45
|
+
"check": "tsc --noEmit -p .",
|
|
46
|
+
"test": "node --test --experimental-strip-types test/*.test.ts"
|
|
47
|
+
}
|
|
6
48
|
}
|