-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
148 lines (128 loc) · 3.83 KB
/
index.ts
File metadata and controls
148 lines (128 loc) · 3.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/**
* Nvim extension - open nvim, using the git changed files picker when there are changes.
*
* Suspends pi's TUI, gives nvim full terminal control, restores pi when nvim exits,
* and if agent-review.nvim exported comments for this session, prepares them in pi's
* input editor.
*/
import { mkdtempSync, readFileSync, rmSync, existsSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { randomUUID } from "node:crypto";
import { spawnSync } from "node:child_process";
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
function shouldOpenGitPicker(cwd: string): boolean {
const isGitRepo = spawnSync("git", ["rev-parse", "--is-inside-work-tree"], {
cwd,
env: process.env,
encoding: "utf8",
});
if (isGitRepo.status !== 0 || isGitRepo.stdout.trim() !== "true") {
return false;
}
const hasHead = spawnSync("git", ["rev-parse", "--verify", "HEAD"], {
cwd,
env: process.env,
stdio: "ignore",
});
if (hasHead.status !== 0) {
return false;
}
const status = spawnSync("git", ["status", "--short", "--untracked-files=all"], {
cwd,
env: process.env,
encoding: "utf8",
});
return status.status === 0 && status.stdout.trim().length > 0;
}
type ExportPayload = {
version?: number;
token?: string;
root?: string;
text?: string;
};
function readAgentReviewExport(exportPath: string, token: string): string | undefined {
if (!existsSync(exportPath)) {
return undefined;
}
try {
const raw = readFileSync(exportPath, "utf8");
if (!raw.trim()) {
return undefined;
}
const payload = JSON.parse(raw) as ExportPayload;
if (payload.token !== token) {
return undefined;
}
const text = typeof payload.text === "string" ? payload.text.trim() : "";
return text || undefined;
} catch {
return undefined;
}
}
function runNvim(ctx: {
hasUI: boolean;
cwd: string;
ui: any;
}): Promise<void> {
if (!ctx.hasUI) {
ctx.ui.notify("Requires interactive mode", "error");
return Promise.resolve();
}
return ctx.ui.custom(
(tui: any, _theme: any, _kb: any, done: (val: number | null) => void) => {
tui.stop();
process.stdout.write("\x1b[2J\x1b[H");
const sessionDir = mkdtempSync(join(tmpdir(), "agent-review-"));
const exportPath = join(sessionDir, "export.json");
const exportToken = randomUUID();
let resultStatus: number | null = null;
let exportedText: string | undefined;
try {
const args = shouldOpenGitPicker(ctx.cwd)
? ["-c", "lua Snacks.picker.git_status()"]
: [];
const result = spawnSync("nvim", args, {
stdio: "inherit",
cwd: ctx.cwd,
env: {
...process.env,
AGENT_REVIEW_EXPORT_PATH: exportPath,
AGENT_REVIEW_EXPORT_TOKEN: exportToken,
AGENT_REVIEW_EXPORT_ROOT: ctx.cwd,
},
});
resultStatus = result.status;
exportedText = readAgentReviewExport(exportPath, exportToken);
} finally {
rmSync(sessionDir, { recursive: true, force: true });
}
tui.start();
done(resultStatus);
if (exportedText) {
setTimeout(() => {
ctx.ui.setEditorText(exportedText);
ctx.ui.notify("Loaded agent-review comments into the input editor", "info");
tui.requestRender(true);
}, 0);
} else {
tui.requestRender(true);
}
return { render: () => [], invalidate: () => {} };
},
);
}
export default function (pi: ExtensionAPI) {
pi.registerCommand("nvim", {
description: "Open nvim",
handler: async (_args: any, ctx: any) => {
await runNvim(ctx);
},
});
pi.registerShortcut("ctrl+shift+e", {
description: "Open nvim",
handler: async (ctx: any) => {
await runNvim(ctx);
},
});
}