Repository navigation
feat: improve gate approvals and web tool rendering #109
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
29d2a82
fix(pi-gate): match native approval dialog styling
zeldrisho 7854218
feat: improve extension notices and rendering
zeldrisho 7154367
chore(release): prepare gate and web package releases
zeldrisho 1d63fbb
fix(pi-web-search): omit web fallback notice in answers mode
zeldrisho File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,140 @@ | ||
| import type { Component } from "@earendil-works/pi-tui"; | ||
| import { truncateToWidth, visibleWidth } from "@earendil-works/pi-tui"; | ||
|
|
||
| const ACTIONS = ["Allow", "Deny"] as const; | ||
|
|
||
| type Action = (typeof ACTIONS)[number]; | ||
|
|
||
| type Foreground = "text" | "muted" | "accent" | "toolOutput" | "border"; | ||
|
|
||
| export interface ApprovalPromptTheme { | ||
| style(text: string, options: { fg?: Foreground; bold?: boolean }): string; | ||
| bold(text: string): string; | ||
| } | ||
|
|
||
| export interface ApprovalPromptKeybindings { | ||
| matches( | ||
| data: string, | ||
| action: "tui.select.up" | "tui.select.down" | "tui.select.confirm" | "tui.select.cancel", | ||
| ): boolean; | ||
| } | ||
|
|
||
| export interface ApprovalPromptTui { | ||
| requestRender(): void; | ||
| } | ||
|
|
||
| /** Theme-aware approval dialog used by the interactive TUI. */ | ||
| export function createApprovalPrompt( | ||
| tui: ApprovalPromptTui, | ||
| theme: ApprovalPromptTheme, | ||
| keybindings: ApprovalPromptKeybindings, | ||
| done: (result: Action | undefined) => void, | ||
| command: string, | ||
| rule: string, | ||
| ): Component { | ||
| let selectedIndex = 0; | ||
|
|
||
| const invalidate = () => {}; | ||
|
|
||
| const innerWidth = (width: number) => Math.max(1, width - 4); | ||
|
|
||
| const panelLine = ( | ||
| text: string, | ||
| width: number, | ||
| color: "text" | "muted" | "accent" | "toolOutput" = "text", | ||
| ) => { | ||
| const contentWidth = innerWidth(width); | ||
| const content = truncateToWidth(text, contentWidth, "…"); | ||
| const padding = " ".repeat(Math.max(0, contentWidth - visibleWidth(content))); | ||
|
|
||
| const styled = theme.style(`${content}${padding}`, { fg: color }); | ||
|
|
||
| return ` ${styled} `; | ||
| }; | ||
|
|
||
| const component: Component = { | ||
| render(width: number): string[] { | ||
| const available = innerWidth(width); | ||
| const border = theme.style("─".repeat(available + 4), { fg: "border" }); | ||
|
|
||
| const lines = [ | ||
| border, | ||
| panelLine(theme.bold("pi-gate: allow this command?"), width, "accent"), | ||
| panelLine("Review the requested command or file change:", width, "muted"), | ||
| panelLine("", width), | ||
| ...command.split("\n").map((line) => { | ||
| const contentWidth = innerWidth(width); | ||
| const parts = line.split(/([»«])/u); | ||
| let matched = false; | ||
|
|
||
| const rendered = parts | ||
| .map((part) => { | ||
| if (part === "»") { | ||
| matched = true; | ||
|
|
||
| return ""; | ||
| } | ||
|
|
||
| if (part === "«") { | ||
| matched = false; | ||
|
|
||
| return ""; | ||
| } | ||
|
|
||
| return theme.style(part, { fg: matched ? "accent" : "toolOutput", bold: matched }); | ||
| }) | ||
| .join(""); | ||
|
|
||
| const visible = line.replace(/[»«]/gu, ""); | ||
| const content = truncateToWidth(visible, contentWidth, "…"); | ||
| const padding = " ".repeat(Math.max(0, contentWidth - visibleWidth(content))); | ||
|
|
||
| return ` ${rendered}${theme.style(padding, { fg: "toolOutput" })} `; | ||
| }), | ||
| panelLine("", width), | ||
| panelLine(`Matched rule: ${rule}`, width, "muted"), | ||
| panelLine("", width), | ||
| ...ACTIONS.map((action, index) => { | ||
| const selected = index === selectedIndex; | ||
| const label = `${selected ? "→ " : " "}${action}`; | ||
| const row = truncateToWidth(label, available); | ||
| const padded = `${row}${" ".repeat(Math.max(0, available - visibleWidth(row)))}`; | ||
|
|
||
| return theme.style(` ${padded} `, { | ||
| fg: selected ? "accent" : "text", | ||
| }); | ||
| }), | ||
| panelLine("", width), | ||
| panelLine("↑↓ move · enter confirm · esc cancel", width, "muted"), | ||
| border, | ||
| ]; | ||
|
|
||
| return lines.map((line) => truncateToWidth(line, width)); | ||
| }, | ||
| handleInput(data: string): void { | ||
| if (keybindings.matches(data, "tui.select.up") || data === "k") { | ||
| selectedIndex = Math.max(0, selectedIndex - 1); | ||
| } else if (keybindings.matches(data, "tui.select.down") || data === "j") { | ||
| selectedIndex = Math.min(ACTIONS.length - 1, selectedIndex + 1); | ||
| } else if (keybindings.matches(data, "tui.select.confirm")) { | ||
| done(ACTIONS[selectedIndex]); | ||
|
|
||
| return; | ||
| } else if (keybindings.matches(data, "tui.select.cancel")) { | ||
| done(undefined); | ||
|
|
||
| return; | ||
| } else { | ||
| return; | ||
| } | ||
|
|
||
| component.invalidate(); | ||
| tui.requestRender(); | ||
| }, | ||
| invalidate, | ||
| }; | ||
|
|
||
| return component; | ||
| } | ||
|
|
||
| export type ApprovalAction = Action; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.