mirror of
https://github.com/openai/codex.git
synced 2026-09-13 11:47:17 +00:00
agentydragon(tasks): implement command snippet truncation in session approval labels
This commit is contained in:
@@ -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 deny‑reason:
|
||||
@@ -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) => {
|
||||
|
||||
27
codex-cli/src/utils/string-utils.ts
Normal file
27
codex-cli/src/utils/string-utils.ts
Normal 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)`;
|
||||
}
|
||||
39
codex-cli/tests/string-utils.test.ts
Normal file
39
codex-cli/tests/string-utils.test.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
import { truncateMiddle, sessionScopedApprovalLabel } from "../src/utils/string-utils";
|
||||
|
||||
describe("truncateMiddle", () => {
|
||||
it("returns the original string when shorter than max length", () => {
|
||||
expect(truncateMiddle("short", 10)).toBe("short");
|
||||
});
|
||||
|
||||
it("returns the original string when equal to max length", () => {
|
||||
expect(truncateMiddle("exactlen", 8)).toBe("exactlen");
|
||||
});
|
||||
|
||||
it("truncates the middle of a longer string", () => {
|
||||
const text = "abcdefghij"; // length 10
|
||||
// maxLength 7 => trimLength=6, startLen=3, endLen=3 => "abc…hij"
|
||||
expect(truncateMiddle(text, 7)).toBe("abc…hij");
|
||||
});
|
||||
|
||||
it("handles odd max lengths correctly", () => {
|
||||
const text = "abcdefghijkl"; // length 12
|
||||
// maxLength 8 => trimLength=7, startLen=4, endLen=3 => "abcd…ijk"
|
||||
expect(truncateMiddle(text, 8)).toBe("abcd…ijk");
|
||||
});
|
||||
});
|
||||
|
||||
describe("sessionScopedApprovalLabel", () => {
|
||||
const cmd = "echo hello world";
|
||||
|
||||
it("embeds the full command when shorter than max length", () => {
|
||||
expect(sessionScopedApprovalLabel(cmd, 50)).toBe(
|
||||
"Yes, always allow running `echo hello world` for this session (a)",
|
||||
);
|
||||
});
|
||||
|
||||
it("embeds a truncated command when longer than max length", () => {
|
||||
const longCmd = "cat " + "a".repeat(100) + " end";
|
||||
const label = sessionScopedApprovalLabel(longCmd, 20);
|
||||
expect(label).toMatch(/^Yes, always allow running `.{0,20}` for this session \(a\)$/);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user