@tian.zuo/pi-rex 0.0.0-stage → 0.1.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 ADDED
@@ -0,0 +1,13 @@
1
+ # @tian.zuo/pi-rex
2
+
3
+ ## 0.1.0
4
+
5
+ ### Minor Changes
6
+
7
+ - [#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.
8
+
9
+ 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.
10
+
11
+ 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.
12
+
13
+ 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
- # Temporary Holding Version
1
+ # @tian.zuo/pi-rex
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
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/package.json CHANGED
@@ -1,6 +1,47 @@
1
1
  {
2
2
  "name": "@tian.zuo/pi-rex",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.1.0",
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
+ "CHANGELOG.md"
27
+ ],
28
+ "pi": {
29
+ "extensions": [
30
+ "./index.ts"
31
+ ]
32
+ },
33
+ "peerDependencies": {
34
+ "@earendil-works/pi-coding-agent": "*"
35
+ },
36
+ "devDependencies": {
37
+ "@effect/language-service": "^0.87.5",
38
+ "typescript": "^7.0.2"
39
+ },
40
+ "publishConfig": {
41
+ "access": "public"
42
+ },
43
+ "scripts": {
44
+ "check": "tsc --noEmit -p .",
45
+ "test": "node --test --experimental-strip-types test/*.test.ts"
46
+ }
6
47
  }