-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtuicr.ts
More file actions
142 lines (128 loc) · 3.56 KB
/
tuicr.ts
File metadata and controls
142 lines (128 loc) · 3.56 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
/**
* tuicr Extension
*
* Registers a `tuicr` tool that the LLM can call to launch the tuicr TUI
* for interactive code review. Suspends the pi TUI and runs tuicr full-screen
* with terminal access. Captures exported instructions via --stdout.
*/
import { execSync, spawnSync } from "node:child_process";
import { closeSync, openSync, readFileSync, rmSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import type { ExtensionAPI } from "@mariozechner/pi-coding-agent";
import { Type } from "@sinclair/typebox";
export default function (pi: ExtensionAPI) {
pi.registerTool({
name: "tuicr",
label: "tuicr",
description: "Launch code review. Get feedback from the User.",
promptGuidelines: [
"Use tuicr after completing implementation work so the user can review changes interactively",
"The user's exported review instructions are returned as the tool result",
],
parameters: Type.Object({
directory: Type.Optional(
Type.String({ description: "Git repo path (default: cwd)" }),
),
revisions: Type.Optional(
Type.String({ description: "Commit range (e.g. HEAD~3..HEAD)" }),
),
}),
async execute(_toolCallId, params, _signal, _onUpdate, ctx) {
// Check tuicr is installed
try {
execSync("command -v tuicr", { stdio: "ignore" });
} catch {
return {
content: [
{
type: "text",
text: "tuicr is not installed. Install via: nix run github:agavra/tuicr",
},
],
};
}
const targetDir = params.directory || ctx.cwd;
// Check it's a git repo
try {
execSync("git rev-parse --git-dir", {
cwd: targetDir,
stdio: "ignore",
});
} catch {
return {
content: [{
type: "text",
text: `Not a git repository: ${targetDir}`,
}],
};
}
if (!ctx.hasUI) {
return {
content: [{
type: "text",
text: "(tuicr requires interactive TUI mode)",
}],
};
}
// Build tuicr args
const tuicrArgs: string[] = ["--stdout"];
if (params.revisions) {
tuicrArgs.push("-r", params.revisions);
}
// --stdout writes export to stdout; redirect to file so TUI uses stderr
const outputFile = join(tmpdir(), `tuicr-${Date.now()}.md`);
// Run tuicr with full terminal access
try {
await ctx.ui.custom<number | null>((tui, _theme, _kb, done) => {
tui.stop();
process.stdout.write("\x1b[2J\x1b[H");
const fd = openSync(outputFile, "w");
try {
const result = spawnSync("tuicr", tuicrArgs, {
stdio: ["inherit", fd, "inherit"],
env: process.env,
cwd: targetDir,
});
tui.start();
tui.requestRender(true);
done(result.status);
} finally {
closeSync(fd);
}
return { render: () => [], invalidate: () => {} };
});
} catch {
// ui.custom can throw on cancel
}
// Read captured instructions, stripping terminal escape sequences
const ansiRegex =
/(?:\x1b\].*?(?:\x1b\\|\x07)|\x1b[\[()#;?]*[0-9;]*[A-Za-z@`\^\[\]{}|~=><]|\x9b[0-9;]*[A-Za-z@`\^\[\]{}|~=><])/g;
let instructions = "";
try {
instructions = readFileSync(outputFile, "utf-8").replace(ansiRegex, "").trim();
} catch {}
try {
rmSync(outputFile, { force: true });
} catch {}
if (instructions) {
return {
content: [
{
type: "text",
text: `Review completed:\n${instructions}`,
},
],
};
}
return {
content: [
{
type: "text",
text: "Review completed. No instructions were exported. The user may paste instructions.",
},
],
};
},
});
}