@jackice/pi-approval-gate 0.1.5 → 0.1.6
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 +5 -0
- package/README.md +1 -1
- package/package.json +1 -1
- package/src/dialog.ts +2 -1
- package/src/native-dialog.ts +38 -30
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.1.6
|
|
4
|
+
|
|
5
|
+
- Use a full-width rounded approval dialog with a warning heading and a highlighted selection.
|
|
6
|
+
- Select Allow once by default. Enter still requires the confirmation delay, and Esc denies immediately.
|
|
7
|
+
|
|
3
8
|
## 0.1.5
|
|
4
9
|
|
|
5
10
|
- Add OMP-style borders, spacing, and keyboard hints to the default approval dialog. Adapt the command preview to fit the choices and bottom border in short terminals.
|
package/README.md
CHANGED
|
@@ -32,7 +32,7 @@ PI_APPROVAL_ARM_MS
|
|
|
32
32
|
|
|
33
33
|
Pi parses extension flags after loading the extension. Settings are read when each request is created.
|
|
34
34
|
|
|
35
|
-
The native-list view is the default.
|
|
35
|
+
The native-list view is the default. Both views start on **Allow once** and require Enter after the confirmation delay. Press Down to select **Deny**, or Esc to deny immediately. Use Page Up and Page Down to inspect long commands.
|
|
36
36
|
|
|
37
37
|
### Optional uv guard
|
|
38
38
|
|
package/package.json
CHANGED
package/src/dialog.ts
CHANGED
|
@@ -36,6 +36,7 @@ export function createApprovalPrompt(
|
|
|
36
36
|
const view = options.ui === "native"
|
|
37
37
|
? (nativeView = createNativeApprovalView(request, tui, theme, options.maxCommandLines))
|
|
38
38
|
: new ApprovalView(fromPiTheme(theme), {
|
|
39
|
+
selected: "allow_once",
|
|
39
40
|
request: {
|
|
40
41
|
toolName: request.toolName,
|
|
41
42
|
command: request.command,
|
|
@@ -70,7 +71,7 @@ export function createApprovalPrompt(
|
|
|
70
71
|
blocked: { kind: "permission", message: "bash approval" },
|
|
71
72
|
...(options.ui === "native" ? {
|
|
72
73
|
overlay: true,
|
|
73
|
-
overlayOptions: { width: "
|
|
74
|
+
overlayOptions: { width: "100%", maxHeight: "90%", anchor: "bottom-center", margin: { top: 1, bottom: 1 } },
|
|
74
75
|
onHandle: (handle) => nativeView?.setOverlayHandle(handle),
|
|
75
76
|
} : {}),
|
|
76
77
|
},
|
package/src/native-dialog.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { ExtensionUIContext, Theme } from "@earendil-works/pi-coding-agent";
|
|
2
|
-
import { Key,
|
|
2
|
+
import { Key, matchesKey, truncateToWidth, visibleWidth, type OverlayHandle, type TUI } from "@earendil-works/pi-tui";
|
|
3
3
|
|
|
4
4
|
import type { ApprovalRequest, PromptChoice } from "./gate.ts";
|
|
5
5
|
|
|
@@ -13,19 +13,14 @@ export function createNativeApprovalView(
|
|
|
13
13
|
theme: Theme,
|
|
14
14
|
maxCommandLines: number | undefined,
|
|
15
15
|
) {
|
|
16
|
-
|
|
17
|
-
|
|
16
|
+
let selected: PromptChoice = "allow_once";
|
|
17
|
+
const choices = [
|
|
18
18
|
{ label: "Allow once", value: "allow_once" },
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
selectedText: (text) => theme.fg("accent", text),
|
|
22
|
-
description: (text) => theme.fg("muted", text),
|
|
23
|
-
scrollInfo: (text) => theme.fg("muted", text),
|
|
24
|
-
noMatch: (text) => theme.fg("muted", text),
|
|
25
|
-
});
|
|
19
|
+
{ label: "Deny", value: "deny" },
|
|
20
|
+
] as const;
|
|
26
21
|
const limit = maxCommandLines !== undefined && Number.isFinite(maxCommandLines) && maxCommandLines >= 1
|
|
27
22
|
? Math.floor(maxCommandLines) : 8;
|
|
28
|
-
const title =
|
|
23
|
+
const title = "⚠ Dangerous command";
|
|
29
24
|
const risk = `Risk: ${request.risk.toUpperCase()}`;
|
|
30
25
|
const fields = [
|
|
31
26
|
`Reason: ${escapeControls(request.reason)}`,
|
|
@@ -57,12 +52,12 @@ export function createNativeApprovalView(
|
|
|
57
52
|
overlayHandle = handle;
|
|
58
53
|
},
|
|
59
54
|
render(width: number): readonly string[] {
|
|
60
|
-
if (width
|
|
55
|
+
if (width < 3) {
|
|
61
56
|
renderedContextFits = false;
|
|
62
57
|
return [];
|
|
63
58
|
}
|
|
64
|
-
const inset = width
|
|
65
|
-
const innerWidth = width - 2 * inset.length;
|
|
59
|
+
const inset = width >= 12 ? " " : "";
|
|
60
|
+
const innerWidth = width - 2 - 2 * inset.length;
|
|
66
61
|
if (lastWidth !== width) {
|
|
67
62
|
lastWidth = width;
|
|
68
63
|
lines = fields.flatMap((field) => wrapCodeText(field, innerWidth));
|
|
@@ -71,8 +66,9 @@ export function createNativeApprovalView(
|
|
|
71
66
|
// Match the overlay's height cap, including its top and bottom margins.
|
|
72
67
|
const availableRows = Math.min(Math.floor(terminalRows * 0.9), terminalRows - 2);
|
|
73
68
|
const spacer = availableRows >= 15 ? [""] : [];
|
|
74
|
-
const
|
|
75
|
-
|
|
69
|
+
const previewRows = availableRows - 7 - 4 * spacer.length;
|
|
70
|
+
const paginated = lines.length > Math.min(limit, previewRows);
|
|
71
|
+
pageRows = Math.max(1, Math.min(limit, previewRows - Number(paginated)));
|
|
76
72
|
offset = Math.min(offset, Math.max(0, lines.length - pageRows));
|
|
77
73
|
const end = Math.min(lines.length, offset + pageRows);
|
|
78
74
|
const fullPage = `Page ${offset + 1}-${end}/${lines.length}`;
|
|
@@ -90,28 +86,40 @@ export function createNativeApprovalView(
|
|
|
90
86
|
: visibleWidth(`Risk: ${request.risk[0]!.toUpperCase()}`) <= innerWidth
|
|
91
87
|
? `Risk: ${request.risk[0]!.toUpperCase()}`
|
|
92
88
|
: request.risk[0]!.toUpperCase();
|
|
93
|
-
const controls = "↑/↓ navigate
|
|
94
|
-
const
|
|
95
|
-
const
|
|
96
|
-
const
|
|
89
|
+
const controls = "↑/↓ navigate ↵ select Esc cancel";
|
|
90
|
+
const fullHint = paginated ? controls + " PgUp/PgDn scroll" : controls;
|
|
91
|
+
const hint = visibleWidth(fullHint) <= innerWidth ? fullHint : paginated ? "↑/↓ ↵ Esc PgUp/PgDn" : "↑/↓ ↵ Esc";
|
|
92
|
+
const optionRows = choices.map(({ label, value }) => {
|
|
93
|
+
const active = value === selected;
|
|
94
|
+
const row = `${active ? "❯" : " "} ${label}`;
|
|
95
|
+
if (!active || visibleWidth(row) > innerWidth) return row;
|
|
96
|
+
return theme.bg("selectedBg", theme.fg("warning", row) + " ".repeat(innerWidth - visibleWidth(row)));
|
|
97
|
+
});
|
|
97
98
|
const body = [
|
|
98
99
|
...spacer,
|
|
99
|
-
theme.fg("accent", title),
|
|
100
100
|
theme.fg("warning", riskLabel),
|
|
101
|
-
theme.fg("muted", page),
|
|
102
|
-
...lines.slice(offset, end),
|
|
101
|
+
...(paginated ? [theme.fg("muted", page)] : []),
|
|
102
|
+
...lines.slice(offset, end).map((line) => theme.fg("warning", line)),
|
|
103
103
|
...spacer,
|
|
104
|
-
|
|
104
|
+
theme.fg("warning", "Allow?"),
|
|
105
|
+
...optionRows,
|
|
105
106
|
...spacer,
|
|
106
107
|
theme.fg("dim", hint),
|
|
107
108
|
...spacer,
|
|
108
109
|
];
|
|
109
|
-
const
|
|
110
|
+
const border = (text: string) => theme.fg("accent", text);
|
|
111
|
+
const heading = truncateToWidth(` ${title} `, width - 3, "");
|
|
112
|
+
const top = border("╭─") + theme.fg("warning", theme.bold(heading))
|
|
113
|
+
+ border("─".repeat(width - 3 - visibleWidth(heading)) + "╮");
|
|
114
|
+
const side = border("│");
|
|
115
|
+
const output = [top, ...body.map((line) => {
|
|
116
|
+
const content = truncateToWidth(line, innerWidth);
|
|
117
|
+
return side + inset + content + " ".repeat(innerWidth - visibleWidth(content)) + inset + side;
|
|
118
|
+
}), border("╰" + "─".repeat(width - 2) + "╯")];
|
|
110
119
|
renderedWidth = width;
|
|
111
120
|
renderedRows = output.length;
|
|
112
121
|
renderedContextFits = body.every((line) => visibleWidth(line) <= innerWidth)
|
|
113
|
-
&&
|
|
114
|
-
&& choices[1]?.includes("Allow once") === true;
|
|
122
|
+
&& visibleWidth(title) <= width - 3;
|
|
115
123
|
return output;
|
|
116
124
|
},
|
|
117
125
|
invalidate(): void {
|
|
@@ -121,10 +129,10 @@ export function createNativeApprovalView(
|
|
|
121
129
|
handleInput(data: string, keys: ApprovalKeys): PromptChoice | undefined {
|
|
122
130
|
if (keys.matches(data, "tui.select.cancel")) return "deny";
|
|
123
131
|
if (keys.matches(data, "tui.select.confirm")) {
|
|
124
|
-
return canAllow() &&
|
|
132
|
+
return canAllow() && selected === "allow_once" ? "allow_once" : "deny";
|
|
125
133
|
}
|
|
126
|
-
if (keys.matches(data, "tui.select.up"))
|
|
127
|
-
else if (keys.matches(data, "tui.select.down"))
|
|
134
|
+
if (keys.matches(data, "tui.select.up")) selected = "allow_once";
|
|
135
|
+
else if (keys.matches(data, "tui.select.down")) selected = "deny";
|
|
128
136
|
else if (matchesKey(data, Key.pageUp) || keys.matches(data, "tui.select.pageUp")) offset = Math.max(0, offset - pageRows);
|
|
129
137
|
else if (matchesKey(data, Key.pageDown) || keys.matches(data, "tui.select.pageDown")) offset = Math.min(Math.max(0, lines.length - pageRows), offset + pageRows);
|
|
130
138
|
return undefined;
|