Add stopStream parameter to tool permission rejection with stream cancellation

This commit is contained in:
Nate 2025-08-14 10:02:20 -07:00
parent d383a749c5
commit ced38f24ee
5 changed files with 103 additions and 21 deletions

View file

@ -262,6 +262,7 @@ interface ScreenContentProps {
requestId: string,
approved: boolean,
createPolicy?: boolean,
stopStream?: boolean,
) => void;
handleUserMessage: (message: string) => void;
isWaitingForResponse: boolean;

View file

@ -37,11 +37,12 @@ describe("TUIChat - Stream Stopping on Tool Rejection", () => {
await vi.advanceTimersByTimeAsync(50);
// Verify that the tool was rejected
// Verify that the tool was rejected with stop stream
expect(handleResponse).toHaveBeenCalledWith(
"test-request-123",
false, // approved = false
false // createPolicy = false
false, // createPolicy = false
true // stopStream = true
);
});
@ -67,11 +68,12 @@ describe("TUIChat - Stream Stopping on Tool Rejection", () => {
await vi.advanceTimersByTimeAsync(50);
// Verify rejection
// Verify rejection with stop stream
expect(handleResponse).toHaveBeenCalledWith(
"dangerous-request",
false, // rejected
false
false, // createPolicy = false
true // stopStream = true
);
});

View file

@ -38,10 +38,12 @@ describe("TUIChat - Tool Permission Tests", () => {
expect(frame).toContain("Would you like to continue?");
expect(frame).toContain("Continue");
expect(frame).toContain("Continue + don't ask again");
expect(frame).toContain("Cancel");
expect(frame).toContain("No, and tell Continue what to do differently");
expect(frame).toContain("(tab)");
expect(frame).toContain("(shift+tab)");
expect(frame).toContain("(esc)");
// Verify old Cancel option is not shown
expect(frame).not.toContain("Cancel");
});
it("handles keyboard shortcuts for permission approval and rejection", async () => {
@ -69,6 +71,7 @@ describe("TUIChat - Tool Permission Tests", () => {
expect(handleResponse).toHaveBeenCalledWith(
"test-request-123",
true,
false,
false
);
});
@ -97,7 +100,8 @@ describe("TUIChat - Tool Permission Tests", () => {
expect(handleResponse).toHaveBeenCalledWith(
"test-request-456",
false,
false
false,
true
);
});
@ -125,7 +129,8 @@ describe("TUIChat - Tool Permission Tests", () => {
expect(handleResponse).toHaveBeenCalledWith(
"test-request-789",
false,
false
false,
true
);
});
@ -156,6 +161,7 @@ describe("TUIChat - Tool Permission Tests", () => {
expect(handleResponse).toHaveBeenCalledWith(
"test-request-123",
true,
undefined,
undefined
);
});
@ -185,7 +191,53 @@ describe("TUIChat - Tool Permission Tests", () => {
expect(handleResponse).toHaveBeenCalledWith(
"test-request-policy",
true,
true
true,
false
);
});
it("shows the new 'No, and tell Continue what to do differently' option in the UI", async () => {
const handleResponse = vi.fn();
const { lastFrame } = render(
<ToolPermissionSelector
toolName="Edit"
toolArgs={{
file_path: "/path/to/test.txt",
old_string: "old content",
new_string: "new content",
}}
requestId="test-request-stop"
onResponse={handleResponse}
/>
);
await vi.advanceTimersByTimeAsync(50);
// Verify the new option exists in the rendered output
const frame = lastFrame();
expect(frame).toContain("No, and tell Continue what to do differently");
// Verify all 3 options are present
expect(frame).toContain("Continue");
expect(frame).toContain("Continue + don't ask again");
expect(frame).toContain("No, and tell Continue what to do differently");
// Verify old Cancel option is not shown
expect(frame).not.toContain("Cancel");
});
it("can trigger the new stop stream option programmatically", () => {
const handleResponse = vi.fn();
// Test the function signature directly - this verifies that our interface supports the new parameter
handleResponse("test-request", false, undefined, true);
// Verify the handler was called with stopStream = true
expect(handleResponse).toHaveBeenCalledWith(
"test-request",
false, // approved = false
undefined, // createPolicy = undefined
true // stopStream = true
);
});

View file

@ -12,6 +12,7 @@ interface PermissionOption {
color: string;
approved: boolean;
createPolicy?: boolean;
stopStream?: boolean;
}
interface ToolPermissionSelectorProps {
@ -23,6 +24,7 @@ interface ToolPermissionSelectorProps {
requestId: string,
approved: boolean,
createPolicy?: boolean,
stopStream?: boolean,
) => void;
}
@ -35,7 +37,13 @@ const PERMISSION_OPTIONS: PermissionOption[] = [
approved: true,
createPolicy: true,
},
{ id: "deny", name: "Cancel", color: "red", approved: false },
{
id: "deny_stop",
name: "No, and tell Continue what to do differently",
color: "yellow",
approved: false,
stopStream: true,
},
];
export const ToolPermissionSelector: React.FC<ToolPermissionSelectorProps> = ({
@ -54,25 +62,26 @@ export const ToolPermissionSelector: React.FC<ToolPermissionSelectorProps> = ({
requestId,
selectedOption.approved,
selectedOption.createPolicy,
selectedOption.stopStream,
);
return;
}
// Tab to continue (approve)
if (key.tab && !key.shift) {
onResponse(requestId, true, false);
onResponse(requestId, true, false, false);
return;
}
// Shift+Tab to continue with policy creation
if (key.tab && key.shift) {
onResponse(requestId, true, true);
onResponse(requestId, true, true, false);
return;
}
// Escape to reject (deny)
// Escape to reject with stop stream
if (key.escape) {
onResponse(requestId, false, false);
onResponse(requestId, false, false, true);
return;
}
@ -86,9 +95,9 @@ export const ToolPermissionSelector: React.FC<ToolPermissionSelectorProps> = ({
// Also support y/n for quick responses
if (input === "y" || input === "Y") {
onResponse(requestId, true, false);
onResponse(requestId, true, false, false);
} else if (input === "n" || input === "N") {
onResponse(requestId, false, false);
onResponse(requestId, false, false, true);
}
});
@ -121,7 +130,7 @@ export const ToolPermissionSelector: React.FC<ToolPermissionSelectorProps> = ({
let shortcut = "";
if (option.id === "approve") shortcut = "(tab)";
else if (option.id === "approve_policy") shortcut = "(shift+tab)";
else if (option.id === "deny") shortcut = "(esc)";
else if (option.id === "deny_stop") shortcut = "(esc)";
return (
<Box key={option.id} marginTop={index === 0 ? 1 : 0}>

View file

@ -12,13 +12,13 @@ import { logger } from "../../util/logger.js";
import { DisplayMessage } from "../types.js";
import {
formatMessageWithFiles,
handleAutoCompaction,
handleCompactCommand,
handleSpecialCommands,
initChatHistory,
processSlashCommandResult,
trackUserMessage,
handleAutoCompaction,
formatMessageWithFiles,
handleSpecialCommands,
} from "./useChat.helpers.js";
import {
handleRemoteMessage,
@ -29,9 +29,9 @@ import {
executeStreaming,
} from "./useChat.stream.helpers.js";
import {
UseChatProps,
AttachedFile,
ActivePermissionRequest,
AttachedFile,
UseChatProps,
} from "./useChat.types.js";
export function useChat({
@ -367,6 +367,7 @@ export function useChat({
requestId: string,
approved: boolean,
createPolicy?: boolean,
stopStream?: boolean,
) => {
// Capture the current permission request before clearing it
const currentRequest = activePermissionRequest;
@ -402,6 +403,23 @@ export function useChat({
toolPermissionManager.approveRequest(requestId);
} else {
toolPermissionManager.rejectRequest(requestId);
// If this is a "stop stream" rejection, abort the current request
if (stopStream && abortController && isWaitingForResponse) {
abortController.abort();
setMessages((prev) => [
...prev,
{
role: "system",
content:
"[Tool canceled - please tell Continue what to do differently]",
messageType: "system" as const,
},
]);
setIsWaitingForResponse(false);
setResponseStartTime(null);
setInputMode(true);
}
}
};