@narumitw/pi-worktree 0.24.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/src/session.ts ADDED
@@ -0,0 +1,103 @@
1
+ import { existsSync, writeFileSync } from "node:fs";
2
+ import type { ExtensionCommandContext, SessionEntry } from "@earendil-works/pi-coding-agent";
3
+ import { SessionManager } from "@earendil-works/pi-coding-agent";
4
+ import { stripTerminalControls } from "./git.js";
5
+
6
+ export type WorktreeSwitchResult = "switched" | "cancelled" | "failed";
7
+
8
+ export async function switchToWorktree(
9
+ ctx: ExtensionCommandContext,
10
+ targetPath: string,
11
+ ): Promise<WorktreeSwitchResult> {
12
+ let sessionPath: string | undefined;
13
+ try {
14
+ sessionPath = createTargetSession(ctx, targetPath);
15
+ const result = await ctx.switchSession(sessionPath, {
16
+ withSession: async (replacementCtx) => {
17
+ replacementCtx.ui.notify(
18
+ stripTerminalControls(`Switched Pi workspace to ${targetPath}.`),
19
+ "info",
20
+ );
21
+ },
22
+ });
23
+ if (result.cancelled) {
24
+ ctx.ui.notify(
25
+ stripTerminalControls(
26
+ `Workspace switch was cancelled. The prepared target session was retained at ${sessionPath}.`,
27
+ ),
28
+ "info",
29
+ );
30
+ return "cancelled";
31
+ }
32
+ return "switched";
33
+ } catch (error) {
34
+ const retained = sessionPath ? " The prepared target session was retained." : "";
35
+ const message = stripTerminalControls(
36
+ `Could not switch Pi workspace to ${targetPath}.${retained} The worktree was retained. Retry from /worktree. ${formatError(error)}`,
37
+ );
38
+ try {
39
+ ctx.ui.notify(message, "error");
40
+ } catch {
41
+ console.error(message);
42
+ }
43
+ return "failed";
44
+ }
45
+ }
46
+
47
+ export function createTargetSession(ctx: ExtensionCommandContext, targetPath: string): string {
48
+ const sourceFile = ctx.sessionManager.getSessionFile();
49
+ if (sourceFile && existsSync(sourceFile)) {
50
+ const persisted = SessionManager.open(sourceFile);
51
+ const activeLeaf = ctx.sessionManager.getLeafId();
52
+ if (persisted.getLeafId() === activeLeaf) {
53
+ const forked = SessionManager.forkFrom(sourceFile, targetPath);
54
+ const targetFile = forked.getSessionFile();
55
+ if (!targetFile || !existsSync(targetFile)) {
56
+ throw new Error("Pi did not create the target worktree session file.");
57
+ }
58
+ return targetFile;
59
+ }
60
+ if (activeLeaf !== null && !persisted.getEntry(activeLeaf)) {
61
+ throw new Error("The active Pi session branch is not present in the persisted source file.");
62
+ }
63
+ return writeTargetSession(targetPath, ctx.sessionManager.getBranch(), sourceFile, activeLeaf);
64
+ }
65
+
66
+ if (ctx.sessionManager.getEntries().length > 0) {
67
+ return writeTargetSession(
68
+ targetPath,
69
+ ctx.sessionManager.getBranch(),
70
+ undefined,
71
+ ctx.sessionManager.getLeafId(),
72
+ );
73
+ }
74
+
75
+ return writeTargetSession(targetPath, [], undefined, null);
76
+ }
77
+
78
+ function writeTargetSession(
79
+ targetPath: string,
80
+ entries: readonly SessionEntry[],
81
+ parentSession: string | undefined,
82
+ expectedLeaf: string | null,
83
+ ): string {
84
+ const target = SessionManager.create(targetPath, undefined, { parentSession });
85
+ const targetFile = target.getSessionFile();
86
+ const header = target.getHeader();
87
+ if (!targetFile || !header) throw new Error("Pi could not prepare a target session.");
88
+ const document = [header, ...entries].map((entry) => JSON.stringify(entry)).join("\n");
89
+ writeFileSync(targetFile, `${document}\n`, {
90
+ encoding: "utf8",
91
+ flag: "wx",
92
+ mode: 0o600,
93
+ });
94
+ const verified = SessionManager.open(targetFile);
95
+ if (verified.getCwd() !== targetPath || verified.getLeafId() !== expectedLeaf) {
96
+ throw new Error("Pi could not verify the target session cwd and active branch.");
97
+ }
98
+ return targetFile;
99
+ }
100
+
101
+ function formatError(error: unknown): string {
102
+ return error instanceof Error ? error.message : String(error);
103
+ }
@@ -0,0 +1,6 @@
1
+ import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
2
+ import { registerWorktreeCommand } from "./command.js";
3
+
4
+ export default function worktreeExtension(pi: ExtensionAPI): void {
5
+ registerWorktreeCommand(pi);
6
+ }