Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions chatbot/src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2130,7 +2130,7 @@ export default function App() {
e.target.value = "";
}}
/>
{chat.executors.length > 1 && !pinnedExecutor ? (
{chat.executors.length > 1 ? (
<PermissionPicker
label="CLI"
options={chat.executors.map((executor) => {
Expand All @@ -2142,7 +2142,11 @@ export default function App() {
: null,
};
})}
value={chat.executor}
value={pinnedExecutor ? pinnedExecutor.id : chat.executor}
disabled={Boolean(pinnedExecutor)}
title={pinnedExecutor
? `当前对话已绑定 ${pinnedExecutor.label},对话开始后不能切换 CLI;换 CLI 请新建对话`
: undefined}
onSelect={(id) => {
chat.setExecutor(id);
input.current?.focus();
Expand Down
36 changes: 24 additions & 12 deletions chatbot/src/PermissionPicker.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,16 @@ export function rememberApprovalMode(userId, value) {
}

// `glyph` is a rendered brand mark. Options that only set `icon` keep the Harness icon.
export default function PermissionPicker({ value, onSelect, open, onOpenChange, label = "操作确认方式", icon, options = modes }) {
export default function PermissionPicker({ value, onSelect, open, onOpenChange, label = "操作确认方式", icon, options = modes, disabled = false, title }) {
const trigger = useRef(null);
const panel = useRef(null);
const initialFocus = useRef(null);
const callbacks = useRef(null);
callbacks.current = { onSelect, onOpenChange };
const identity = `permission-picker-${useId().replaceAll(":", "")}`;
const presence = usePanelPresence(open, 160);
// A disabled picker keeps its current value visible but must never open its menu.
const effectiveOpen = disabled ? false : open;
const presence = usePanelPresence(effectiveOpen, 160);
const [position, setPosition] = useState({ top: 0, left: 0, width: 260, maxHeight: 360, placement: "top" });
const current = options.find((mode) => mode.id === value) || options[0];

Expand Down Expand Up @@ -65,7 +67,7 @@ export default function PermissionPicker({ value, onSelect, open, onOpenChange,
}, [presence.mounted, options.length, place]);

useEffect(() => {
if (!open || !presence.mounted) return;
if (!effectiveOpen || !presence.mounted) return;
const frame = requestAnimationFrame(() => {
const items = [...(panel.current?.querySelectorAll('[role="menuitemradio"]') || [])];
const selected = initialFocus.current === "first" ? items[0] :
Expand All @@ -75,7 +77,7 @@ export default function PermissionPicker({ value, onSelect, open, onOpenChange,
selected?.focus({ preventScroll: true });
});
return () => cancelAnimationFrame(frame);
}, [open, presence.mounted]);
}, [effectiveOpen, presence.mounted]);

useEffect(() => {
if (!presence.mounted) return;
Expand All @@ -99,7 +101,7 @@ export default function PermissionPicker({ value, onSelect, open, onOpenChange,
}, [presence.mounted, place]);

useEffect(() => {
if (!open) return;
if (!effectiveOpen) return;
const inside = (target) => trigger.current?.contains(target) || panel.current?.contains(target);
const outside = (event) => {
if (!inside(event.target)) callbacks.current.onOpenChange(false);
Expand All @@ -125,13 +127,20 @@ export default function PermissionPicker({ value, onSelect, open, onOpenChange,
parentDocument?.removeEventListener("pointerdown", parentOutside, true);
parentDocument?.removeEventListener("focusin", parentOutside, true);
};
}, [open]);
}, [effectiveOpen]);

// A picker that becomes disabled while its menu is open must close it, so
// re-enabling later cannot reopen a menu the user did not ask for.
useEffect(() => {
if (disabled && open) callbacks.current.onOpenChange(false);
}, [disabled, open]);

function close(returnFocus = false) {
if (returnFocus) trigger.current?.focus({ preventScroll: true });
callbacks.current.onOpenChange(false);
}
function choose(id) {
if (disabled) return;
callbacks.current.onSelect(id);
close(true);
}
Expand Down Expand Up @@ -169,19 +178,22 @@ export default function PermissionPicker({ value, onSelect, open, onOpenChange,
className="permission-selector"
data-tone={current.tone}
aria-label={label}
aria-disabled={disabled || undefined}
aria-haspopup="menu"
aria-expanded={open}
aria-expanded={effectiveOpen}
aria-controls={identity}
onClick={() => onOpenChange(!open)}
title={title}
disabled={disabled}
onClick={() => onOpenChange(!effectiveOpen)}
onKeyDown={(event) => {
if (["ArrowDown", "ArrowUp", "Home", "End"].includes(event.key)) {
if (open && presence.mounted) keyDown(event);
if (effectiveOpen && presence.mounted) keyDown(event);
else {
event.preventDefault();
initialFocus.current = ["ArrowUp", "End"].includes(event.key) ? "last" : "first";
onOpenChange(true);
}
} else if (open && ["Escape", "Tab"].includes(event.key)) keyDown(event);
} else if (effectiveOpen && ["Escape", "Tab"].includes(event.key)) keyDown(event);
}}
>
{current.glyph || <HarnessIcon name={icon || current.icon} size={16} aria-hidden="true" />}
Expand All @@ -195,8 +207,8 @@ export default function PermissionPicker({ value, onSelect, open, onOpenChange,
className="permission-menu"
role="menu"
aria-label={label}
aria-hidden={!open || undefined}
inert={!open}
aria-hidden={!effectiveOpen || undefined}
inert={!effectiveOpen}
data-phase={presence.phase}
data-placement={position.placement}
style={{ top: position.top, left: position.left, width: position.width, maxHeight: position.maxHeight }}
Expand Down
10 changes: 10 additions & 0 deletions chatbot/src/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -1306,6 +1306,16 @@ small {
[data-theme="dark"] .chat-shell button.permission-selector {
--permission-trigger-hover: rgb(237 237 240 / 0.055);
}
/* A locked selector stays visible but reads as non-interactive. */
.chat-shell button.permission-selector:disabled {
cursor: not-allowed;
opacity: .55;
color: var(--muted);
}
.chat-shell button.permission-selector:disabled:is(:hover, [aria-expanded="true"]) {
background: transparent;
color: var(--muted);
}
.permission-selector > .harness-icon[data-icon="chevron-down"] {
transition: rotate 160ms ease;
}
Expand Down
36 changes: 34 additions & 2 deletions chatbot/tests/app-render.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,8 @@ describe("App", () => {
},
});
view.rerender(<App />);
expect(screen.queryByRole("button", { name: "CLI" })).not.toBeInTheDocument();
const lockedCli = screen.getByRole("button", { name: "CLI" });
expect(lockedCli).toBeDisabled();
expect(document.querySelector(".conversation-engine")).toHaveTextContent("Codex");
// The first reply, still running, locks it the same way.
Object.assign(chat, {
Expand All @@ -586,11 +587,42 @@ describe("App", () => {
session: { id: "s2", title: "生成中", executor: "codebuddy", messages: [] },
});
view.rerender(<App />);
expect(screen.queryByRole("button", { name: "CLI" })).not.toBeInTheDocument();
expect(screen.getByRole("button", { name: "CLI" })).toBeDisabled();
expect(document.querySelector(".conversation-engine")).toHaveTextContent("CodeBuddy");
Object.assign(chat, previous);
});

test("a locked CLI selector stays visible, disabled, and cannot open", async () => {
const user = userEvent.setup();
const previous = { ...chat };
chat.executors = [
...previous.executors,
{ id: "pi", icon: "terminal", label: "Pi", detail: "Pi" },
];
Object.assign(chat, {
executor: "pi",
session: {
id: "s1",
title: "已开始",
executor: "pi",
messages: [{ id: "a1", role: "assistant", model: "m1", content: "hi" }],
},
});
const view = render(<App />);
const cli = screen.getByRole("button", { name: "CLI" });
expect(cli).toBeDisabled();
expect(cli).toHaveAttribute(
"title",
"当前对话已绑定 Pi,对话开始后不能切换 CLI;换 CLI 请新建对话",
);
// The disabled selector never opens its menu.
await user.click(cli);
expect(screen.queryByRole("menu")).not.toBeInTheDocument();
expect(screen.queryByRole("menuitemradio", { name: "Pi" })).not.toBeInTheDocument();
view.unmount();
Object.assign(chat, previous);
});

test("covers credential, local upload, read-only, and interrupted states", async () => {
const previous = { ...chat };
Object.assign(chat, {
Expand Down
56 changes: 56 additions & 0 deletions chatbot/tests/permission-disabled.test.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import React from "react";
import { test, expect, vi } from "vitest";
import { render, screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import PermissionPicker from "../src/PermissionPicker.jsx";

const options = [
{ id: "pi", label: "Pi", detail: "Pi", icon: "terminal" },
{ id: "codex", label: "Codex", detail: "Codex", icon: "terminal" },
];

// Regression for the locked-CLI selector (#1118): a picker that is disabled
// while its menu is open must close the menu immediately and never allow a
// choice from the closing menu. These cover the open -> disabled transition,
// not just an initially-disabled picker.

test("a picker disabled while open immediately hides and inerts its closing menu", () => {
const props = { value: "pi", options, open: true, onOpenChange: vi.fn(), onSelect: vi.fn(), label: "CLI" };
const view = render(<PermissionPicker {...props} />);
const menu = screen.getByRole("menu");
view.rerender(<PermissionPicker {...props} disabled />);
expect(menu).toHaveAttribute("aria-hidden", "true");
expect(menu).toHaveAttribute("inert");
});

test("disabling an open picker tells the parent to close it", () => {
const onOpenChange = vi.fn();
const props = { value: "pi", options, open: true, onOpenChange, onSelect: vi.fn(), label: "CLI" };
const view = render(<PermissionPicker {...props} />);
view.rerender(<PermissionPicker {...props} disabled />);
expect(onOpenChange).toHaveBeenCalledWith(false);
});

test("a focused closing menu cannot choose another executor after being disabled", async () => {
const user = userEvent.setup();
const onSelect = vi.fn();
const props = { value: "pi", options, open: true, onOpenChange: vi.fn(), onSelect, label: "CLI" };
const view = render(<PermissionPicker {...props} />);
const item = screen.getByRole("menuitemradio", { name: "Codex" });
item.focus();
view.rerender(<PermissionPicker {...props} disabled />);
await user.keyboard("{Enter}");
expect(onSelect).not.toHaveBeenCalled();
});

test("a disabled picker never opens its menu from the trigger", async () => {
const user = userEvent.setup();
const onOpenChange = vi.fn();
const view = render(
<PermissionPicker value="pi" options={options} disabled onOpenChange={onOpenChange} onSelect={vi.fn()} label="CLI" />,
);
await user.click(screen.getByRole("button", { name: "CLI" }));
expect(onOpenChange).not.toHaveBeenCalled();
expect(screen.queryByRole("menu")).not.toBeInTheDocument();
view.unmount();
});