@henryqw/pi-herdr-clone 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/LICENSE +21 -0
- package/README.md +37 -0
- package/extensions/clone-tab.ts +135 -0
- package/package.json +49 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Henry Wang
|
|
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
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
# `@henryqw/pi-herdr-clone`
|
|
2
|
+
|
|
3
|
+
Pi extension that clones the current conversation path into a new Pi process in a new tab of the current Herdr workspace. Requires Pi Coding Agent 0.84.x (minimum 0.84.2) and a Pi session running inside Herdr.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pi install npm:@henryqw/pi-herdr-clone
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
Remove with:
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
pi remove npm:@henryqw/pi-herdr-clone
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## `/clone-tab`
|
|
18
|
+
|
|
19
|
+
The command waits until Pi is idle, then:
|
|
20
|
+
|
|
21
|
+
1. Validates the current Herdr pane and resolves its live workspace.
|
|
22
|
+
2. Copies only the current Pi session path into a new persisted session file. Sibling branches are excluded and the original Pi session is not switched.
|
|
23
|
+
3. Creates an unfocused Herdr tab in that workspace with the current working directory.
|
|
24
|
+
4. Starts Pi in the tab's root pane with `--session <absolute-clone-file>`.
|
|
25
|
+
5. Focuses the new tab after Pi starts successfully.
|
|
26
|
+
|
|
27
|
+
The command requires `HERDR_ENV=1`, `HERDR_PANE_ID`, an existing persisted session file, and a current session leaf. It has no configuration or worktree behavior.
|
|
28
|
+
|
|
29
|
+
If tab creation itself fails, the cloned session file is removed. If Herdr creates the tab but the response is incomplete, or once agent start is attempted, the tab and session file are retained because the launch outcome can be unknown; the error reports any known IDs for recovery. A later focus failure is shown as a warning and does not report the already-started clone as failed.
|
|
30
|
+
|
|
31
|
+
## Development
|
|
32
|
+
|
|
33
|
+
```bash
|
|
34
|
+
npm test --workspace @henryqw/pi-herdr-clone
|
|
35
|
+
npm run typecheck --workspace @henryqw/pi-herdr-clone
|
|
36
|
+
npm run pack:check --workspace @henryqw/pi-herdr-clone
|
|
37
|
+
```
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import { stat, unlink } from "node:fs/promises";
|
|
3
|
+
import { resolve } from "node:path";
|
|
4
|
+
import { setTimeout as delay } from "node:timers/promises";
|
|
5
|
+
import {
|
|
6
|
+
SessionManager,
|
|
7
|
+
type ExtensionAPI,
|
|
8
|
+
} from "@earendil-works/pi-coding-agent";
|
|
9
|
+
import { createHerdrClient, herdrCommandFailure, hasHerdrErrorCode } from "@henryqw/pi-herdr";
|
|
10
|
+
|
|
11
|
+
const errorMessage = (error: unknown) => error instanceof Error ? error.message : String(error);
|
|
12
|
+
|
|
13
|
+
function requiredString(value: unknown, label: string): string {
|
|
14
|
+
if (typeof value !== "string" || !value.trim()) throw new Error(`${label} is missing.`);
|
|
15
|
+
return value;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export default function herdrCloneExtension(pi: ExtensionAPI): void {
|
|
19
|
+
const herdr = createHerdrClient<{ cwd: string }>((command, args, options) =>
|
|
20
|
+
pi.exec(command, [...args], options));
|
|
21
|
+
|
|
22
|
+
pi.registerCommand("clone-tab", {
|
|
23
|
+
description: "Clone the current conversation path into a new Herdr tab",
|
|
24
|
+
handler: async (_args, ctx) => {
|
|
25
|
+
await ctx.waitForIdle();
|
|
26
|
+
|
|
27
|
+
if (process.env.HERDR_ENV !== "1") {
|
|
28
|
+
throw new Error("/clone-tab requires the current Pi session inside Herdr (HERDR_ENV=1).");
|
|
29
|
+
}
|
|
30
|
+
const requestedPaneId = requiredString(process.env.HERDR_PANE_ID, "HERDR_PANE_ID");
|
|
31
|
+
const currentSessionFile = requiredString(
|
|
32
|
+
ctx.sessionManager.getSessionFile(),
|
|
33
|
+
"Persisted Pi session file",
|
|
34
|
+
);
|
|
35
|
+
const leafId = requiredString(ctx.sessionManager.getLeafId(), "Current Pi session leaf");
|
|
36
|
+
const sessionFile = resolve(currentSessionFile);
|
|
37
|
+
let sourceStat;
|
|
38
|
+
try {
|
|
39
|
+
sourceStat = await stat(sessionFile);
|
|
40
|
+
} catch (error) {
|
|
41
|
+
throw new Error(`Persisted Pi session file does not exist: ${sessionFile}`, { cause: error });
|
|
42
|
+
}
|
|
43
|
+
if (!sourceStat.isFile()) throw new Error(`Persisted Pi session path is not a file: ${sessionFile}`);
|
|
44
|
+
|
|
45
|
+
const paneResponse = await herdr.json(["pane", "get", requestedPaneId], { cwd: ctx.cwd });
|
|
46
|
+
const pane = (paneResponse as { result?: { pane?: { pane_id?: unknown; workspace_id?: unknown } } }).result?.pane;
|
|
47
|
+
requiredString(pane?.pane_id, "Herdr pane response pane_id");
|
|
48
|
+
const workspaceId = requiredString(pane?.workspace_id, "Herdr pane response workspace_id");
|
|
49
|
+
|
|
50
|
+
const session = SessionManager.open(sessionFile, ctx.sessionManager.getSessionDir(), ctx.cwd);
|
|
51
|
+
const createdClone = session.createBranchedSession(leafId);
|
|
52
|
+
if (!createdClone) throw new Error("Pi did not create a persisted clone session file.");
|
|
53
|
+
const cloneFile = resolve(createdClone);
|
|
54
|
+
let cloneStat;
|
|
55
|
+
try {
|
|
56
|
+
cloneStat = await stat(cloneFile);
|
|
57
|
+
} catch (error) {
|
|
58
|
+
throw new Error(`Pi clone session file was not created: ${cloneFile}`, { cause: error });
|
|
59
|
+
}
|
|
60
|
+
if (!cloneStat.isFile()) throw new Error(`Pi clone session path is not a file: ${cloneFile}`);
|
|
61
|
+
|
|
62
|
+
const tabCreateArgs = ["tab", "create", "--workspace", workspaceId, "--cwd", ctx.cwd, "--no-focus"] as const;
|
|
63
|
+
const createdTab = await herdr.exec(tabCreateArgs, { cwd: ctx.cwd });
|
|
64
|
+
if (createdTab.code !== 0 || createdTab.killed) {
|
|
65
|
+
const createError = new Error(herdrCommandFailure(tabCreateArgs, createdTab));
|
|
66
|
+
try {
|
|
67
|
+
await unlink(cloneFile);
|
|
68
|
+
} catch (cleanupError) {
|
|
69
|
+
throw new AggregateError(
|
|
70
|
+
[createError, cleanupError],
|
|
71
|
+
`${createError.message} Clone cleanup also failed for ${cloneFile}: ${errorMessage(cleanupError)}`,
|
|
72
|
+
);
|
|
73
|
+
}
|
|
74
|
+
throw createError;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
let tabId: string | undefined;
|
|
78
|
+
let rootPaneId: string | undefined;
|
|
79
|
+
try {
|
|
80
|
+
const tabResponse: unknown = JSON.parse(createdTab.stdout);
|
|
81
|
+
if (!tabResponse || typeof tabResponse !== "object" || Array.isArray(tabResponse)) {
|
|
82
|
+
throw new Error("Herdr tab create returned invalid JSON");
|
|
83
|
+
}
|
|
84
|
+
const result = (tabResponse as {
|
|
85
|
+
result?: { tab?: { tab_id?: unknown }; root_pane?: { pane_id?: unknown } };
|
|
86
|
+
}).result;
|
|
87
|
+
tabId = requiredString(result?.tab?.tab_id, "Herdr tab response tab_id");
|
|
88
|
+
rootPaneId = requiredString(result?.root_pane?.pane_id, `Herdr tab ${tabId} root pane_id`);
|
|
89
|
+
} catch (error) {
|
|
90
|
+
throw new Error(
|
|
91
|
+
`Clone launch could not be confirmed after creating a Herdr tab; retained${tabId ? ` Herdr tab ${tabId} and` : ""} session ${cloneFile}: ${errorMessage(error)}`,
|
|
92
|
+
{ cause: error },
|
|
93
|
+
);
|
|
94
|
+
}
|
|
95
|
+
if (!tabId || !rootPaneId) {
|
|
96
|
+
throw new Error(`Clone launch could not be confirmed after creating a Herdr tab; retained session ${cloneFile}.`);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const agentName = `clone-${randomUUID().replaceAll("-", "").slice(0, 24)}`;
|
|
100
|
+
const startArgs = [
|
|
101
|
+
"agent", "start", agentName, "--kind", "pi", "--pane", rootPaneId,
|
|
102
|
+
"--", "--session", cloneFile,
|
|
103
|
+
];
|
|
104
|
+
try {
|
|
105
|
+
for (let attempt = 1; attempt <= 5; attempt += 1) {
|
|
106
|
+
const result = await herdr.exec(startArgs, { cwd: ctx.cwd });
|
|
107
|
+
if (result.code === 0 && !result.killed) break;
|
|
108
|
+
if (!hasHerdrErrorCode(result, "agent_pane_busy") || attempt === 5) {
|
|
109
|
+
throw new Error(herdrCommandFailure(startArgs, result));
|
|
110
|
+
}
|
|
111
|
+
await delay(250);
|
|
112
|
+
}
|
|
113
|
+
} catch (error) {
|
|
114
|
+
throw new Error(
|
|
115
|
+
`Clone launch could not be confirmed after starting agent ${agentName}; retained Herdr tab ${tabId}, root pane ${rootPaneId}, and session ${cloneFile}: ${errorMessage(error)}`,
|
|
116
|
+
{ cause: error },
|
|
117
|
+
);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
try {
|
|
121
|
+
await herdr.run(["tab", "focus", tabId], { cwd: ctx.cwd });
|
|
122
|
+
} catch (error) {
|
|
123
|
+
ctx.ui.notify(
|
|
124
|
+
`Clone agent ${agentName} started in Herdr tab ${tabId} (root pane ${rootPaneId}), but focus failed: ${errorMessage(error)}`,
|
|
125
|
+
"warning",
|
|
126
|
+
);
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
ctx.ui.notify(
|
|
130
|
+
`Cloned current conversation into Herdr tab ${tabId} (root pane ${rootPaneId}, agent ${agentName}).`,
|
|
131
|
+
"info",
|
|
132
|
+
);
|
|
133
|
+
},
|
|
134
|
+
});
|
|
135
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@henryqw/pi-herdr-clone",
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "Clone the current Pi conversation path into a new Herdr tab.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"pi-package",
|
|
7
|
+
"pi",
|
|
8
|
+
"herdr",
|
|
9
|
+
"session",
|
|
10
|
+
"clone"
|
|
11
|
+
],
|
|
12
|
+
"type": "module",
|
|
13
|
+
"engines": {
|
|
14
|
+
"node": ">=22.19.0"
|
|
15
|
+
},
|
|
16
|
+
"license": "MIT",
|
|
17
|
+
"files": [
|
|
18
|
+
"extensions",
|
|
19
|
+
"README.md",
|
|
20
|
+
"LICENSE"
|
|
21
|
+
],
|
|
22
|
+
"scripts": {
|
|
23
|
+
"test": "node --test test/*.test.ts",
|
|
24
|
+
"typecheck": "tsc --noEmit --allowImportingTsExtensions --target ES2022 --module NodeNext --moduleResolution NodeNext --skipLibCheck extensions/*.ts test/*.test.ts",
|
|
25
|
+
"pack:check": "npm pack --dry-run"
|
|
26
|
+
},
|
|
27
|
+
"peerDependencies": {
|
|
28
|
+
"@earendil-works/pi-coding-agent": "^0.84.2"
|
|
29
|
+
},
|
|
30
|
+
"dependencies": {
|
|
31
|
+
"@henryqw/pi-herdr": "^0.1.1"
|
|
32
|
+
},
|
|
33
|
+
"repository": {
|
|
34
|
+
"type": "git",
|
|
35
|
+
"url": "git+https://github.com/HenryQW/pi-packages.git",
|
|
36
|
+
"directory": "packages/pi-herdr-clone"
|
|
37
|
+
},
|
|
38
|
+
"bugs": {
|
|
39
|
+
"url": "https://github.com/HenryQW/pi-packages/issues"
|
|
40
|
+
},
|
|
41
|
+
"publishConfig": {
|
|
42
|
+
"access": "public"
|
|
43
|
+
},
|
|
44
|
+
"pi": {
|
|
45
|
+
"extensions": [
|
|
46
|
+
"./extensions"
|
|
47
|
+
]
|
|
48
|
+
}
|
|
49
|
+
}
|