agentydragon(tasks): implement command snippet truncation in session approval labels

This commit is contained in:
Rai (Michael Pokorny)
2025-06-24 20:55:43 -07:00
parent 2a015a2464
commit fca6766935
5 changed files with 159 additions and 23 deletions

View File

@@ -6,6 +6,7 @@ import { ReviewDecision } from "../../utils/agent/review";
import { Select } from "../vendor/ink-select/select";
import TextInput from "../vendor/ink-text-input";
import { Box, Text, useInput } from "ink";
import { sessionScopedApprovalLabel } from "../../utils/string-utils";
import React from "react";
// default denyreason:
@@ -86,10 +87,17 @@ export function TerminalChatCommandReview({
];
if (showAlwaysApprove) {
opts.push({
label: "Always allow this command for the remainder of the session (a)",
value: ReviewDecision.ALWAYS,
});
let label: string;
if (
React.isValidElement(confirmationPrompt) &&
typeof (confirmationPrompt as any).props?.commandForDisplay === "string"
) {
const cmd: string = (confirmationPrompt as any).props.commandForDisplay;
label = sessionScopedApprovalLabel(cmd, 30);
} else {
label = "Always allow this command for the remainder of the session (a)";
}
opts.push({ label, value: ReviewDecision.ALWAYS });
}
opts.push(
@@ -117,7 +125,7 @@ export function TerminalChatCommandReview({
);
return opts;
}, [showAlwaysApprove]);
}, [showAlwaysApprove, confirmationPrompt]);
useInput(
(input, key) => {

View File

@@ -0,0 +1,27 @@
/**
* Truncate a string in the middle to ensure its length does not exceed maxLength.
* If the input is longer than maxLength, replaces the middle with a single-character ellipsis '…'.
*/
export function truncateMiddle(text: string, maxLength: number): string {
if (text.length <= maxLength) {
return text;
}
const ellipsis = '…';
const trimLength = maxLength - ellipsis.length;
const startLength = Math.ceil(trimLength / 2);
const endLength = Math.floor(trimLength / 2);
return text.slice(0, startLength) + ellipsis + text.slice(text.length - endLength);
}
/**
* Generate a session-scoped approval label for a given command.
* Embeds a truncated snippet of the first line of commandForDisplay.
*/
export function sessionScopedApprovalLabel(
commandForDisplay: string,
maxLength: number,
): string {
const firstLine = commandForDisplay.split('\n')[0].trim();
const snippet = truncateMiddle(firstLine, maxLength);
return `Yes, always allow running \`${snippet}\` for this session (a)`;
}